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 (args
== error_mark_node
)
1015 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1017 /* There should be as many levels of arguments as there are
1018 levels of parameters. */
1019 gcc_assert (TMPL_ARGS_DEPTH (args
)
1020 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1022 if (optimize_specialization_lookup_p (tmpl
))
1024 tree class_template
;
1025 tree class_specialization
;
1026 vec
<tree
, va_gc
> *methods
;
1030 /* The template arguments actually apply to the containing
1031 class. Find the class specialization with those
1033 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1034 class_specialization
1035 = retrieve_specialization (class_template
, args
, 0);
1036 if (!class_specialization
)
1038 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1039 for the specialization. */
1040 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1043 /* Iterate through the methods with the indicated name, looking
1044 for the one that has an instance of TMPL. */
1045 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1046 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1048 tree fn
= OVL_CURRENT (fns
);
1049 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1050 /* using-declarations can add base methods to the method vec,
1051 and we don't want those here. */
1052 && DECL_CONTEXT (fn
) == class_specialization
)
1061 htab_t specializations
;
1065 elt
.spec
= NULL_TREE
;
1067 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1068 specializations
= type_specializations
;
1070 specializations
= decl_specializations
;
1073 hash
= hash_specialization (&elt
);
1074 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1082 /* Like retrieve_specialization, but for local declarations. */
1085 retrieve_local_specialization (tree tmpl
)
1089 if (local_specializations
== NULL
)
1092 slot
= pointer_map_contains (local_specializations
, tmpl
);
1093 return slot
? (tree
) *slot
: NULL_TREE
;
1096 /* Returns nonzero iff DECL is a specialization of TMPL. */
1099 is_specialization_of (tree decl
, tree tmpl
)
1103 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1107 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1113 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1115 for (t
= TREE_TYPE (decl
);
1117 t
= CLASSTYPE_USE_TEMPLATE (t
)
1118 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1119 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1126 /* Returns nonzero iff DECL is a specialization of friend declaration
1127 FRIEND_DECL according to [temp.friend]. */
1130 is_specialization_of_friend (tree decl
, tree friend_decl
)
1132 bool need_template
= true;
1135 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1136 || TREE_CODE (decl
) == TYPE_DECL
);
1138 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1139 of a template class, we want to check if DECL is a specialization
1141 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1142 && DECL_TEMPLATE_INFO (friend_decl
)
1143 && !DECL_USE_TEMPLATE (friend_decl
))
1145 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1146 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1147 need_template
= false;
1149 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1150 && !PRIMARY_TEMPLATE_P (friend_decl
))
1151 need_template
= false;
1153 /* There is nothing to do if this is not a template friend. */
1154 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1157 if (is_specialization_of (decl
, friend_decl
))
1161 A member of a class template may be declared to be a friend of a
1162 non-template class. In this case, the corresponding member of
1163 every specialization of the class template is a friend of the
1164 class granting friendship.
1166 For example, given a template friend declaration
1168 template <class T> friend void A<T>::f();
1170 the member function below is considered a friend
1172 template <> struct A<int> {
1176 For this type of template friend, TEMPLATE_DEPTH below will be
1177 nonzero. To determine if DECL is a friend of FRIEND, we first
1178 check if the enclosing class is a specialization of another. */
1180 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1182 && DECL_CLASS_SCOPE_P (decl
)
1183 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1184 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1186 /* Next, we check the members themselves. In order to handle
1187 a few tricky cases, such as when FRIEND_DECL's are
1189 template <class T> friend void A<T>::g(T t);
1190 template <class T> template <T t> friend void A<T>::h();
1194 void A<int>::g(int);
1195 template <int> void A<int>::h();
1197 we need to figure out ARGS, the template arguments from
1198 the context of DECL. This is required for template substitution
1199 of `T' in the function parameter of `g' and template parameter
1200 of `h' in the above examples. Here ARGS corresponds to `int'. */
1202 tree context
= DECL_CONTEXT (decl
);
1203 tree args
= NULL_TREE
;
1204 int current_depth
= 0;
1206 while (current_depth
< template_depth
)
1208 if (CLASSTYPE_TEMPLATE_INFO (context
))
1210 if (current_depth
== 0)
1211 args
= TYPE_TI_ARGS (context
);
1213 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1216 context
= TYPE_CONTEXT (context
);
1219 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1224 tree friend_args_type
;
1225 tree decl_args_type
;
1227 /* Make sure that both DECL and FRIEND_DECL are templates or
1229 is_template
= DECL_TEMPLATE_INFO (decl
)
1230 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1231 if (need_template
^ is_template
)
1233 else if (is_template
)
1235 /* If both are templates, check template parameter list. */
1237 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1239 if (!comp_template_parms
1240 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1244 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1247 decl_type
= TREE_TYPE (decl
);
1249 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1250 tf_none
, NULL_TREE
);
1251 if (friend_type
== error_mark_node
)
1254 /* Check if return types match. */
1255 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1258 /* Check if function parameter types match, ignoring the
1259 `this' parameter. */
1260 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1261 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1263 friend_args_type
= TREE_CHAIN (friend_args_type
);
1264 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1265 decl_args_type
= TREE_CHAIN (decl_args_type
);
1267 return compparms (decl_args_type
, friend_args_type
);
1271 /* DECL is a TYPE_DECL */
1273 tree decl_type
= TREE_TYPE (decl
);
1275 /* Make sure that both DECL and FRIEND_DECL are templates or
1278 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1279 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1281 if (need_template
^ is_template
)
1283 else if (is_template
)
1286 /* If both are templates, check the name of the two
1287 TEMPLATE_DECL's first because is_friend didn't. */
1288 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1289 != DECL_NAME (friend_decl
))
1292 /* Now check template parameter list. */
1294 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1296 return comp_template_parms
1297 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1301 return (DECL_NAME (decl
)
1302 == DECL_NAME (friend_decl
));
1308 /* Register the specialization SPEC as a specialization of TMPL with
1309 the indicated ARGS. IS_FRIEND indicates whether the specialization
1310 is actually just a friend declaration. Returns SPEC, or an
1311 equivalent prior declaration, if available. */
1314 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1321 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1323 if (TREE_CODE (spec
) == FUNCTION_DECL
1324 && uses_template_parms (DECL_TI_ARGS (spec
)))
1325 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1326 register it; we want the corresponding TEMPLATE_DECL instead.
1327 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1328 the more obvious `uses_template_parms (spec)' to avoid problems
1329 with default function arguments. In particular, given
1330 something like this:
1332 template <class T> void f(T t1, T t = T())
1334 the default argument expression is not substituted for in an
1335 instantiation unless and until it is actually needed. */
1338 if (optimize_specialization_lookup_p (tmpl
))
1339 /* We don't put these specializations in the hash table, but we might
1340 want to give an error about a mismatch. */
1341 fn
= retrieve_specialization (tmpl
, args
, 0);
1349 hash
= hash_specialization (&elt
);
1352 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1354 fn
= ((spec_entry
*) *slot
)->spec
;
1359 /* We can sometimes try to re-register a specialization that we've
1360 already got. In particular, regenerate_decl_from_template calls
1361 duplicate_decls which will update the specialization list. But,
1362 we'll still get called again here anyhow. It's more convenient
1363 to simply allow this than to try to prevent it. */
1366 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1368 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1370 if (DECL_ODR_USED (fn
)
1371 || DECL_EXPLICIT_INSTANTIATION (fn
))
1373 error ("specialization of %qD after instantiation",
1375 return error_mark_node
;
1380 /* This situation should occur only if the first
1381 specialization is an implicit instantiation, the
1382 second is an explicit specialization, and the
1383 implicit instantiation has not yet been used. That
1384 situation can occur if we have implicitly
1385 instantiated a member function and then specialized
1388 We can also wind up here if a friend declaration that
1389 looked like an instantiation turns out to be a
1392 template <class T> void foo(T);
1393 class S { friend void foo<>(int) };
1394 template <> void foo(int);
1396 We transform the existing DECL in place so that any
1397 pointers to it become pointers to the updated
1400 If there was a definition for the template, but not
1401 for the specialization, we want this to look as if
1402 there were no definition, and vice versa. */
1403 DECL_INITIAL (fn
) = NULL_TREE
;
1404 duplicate_decls (spec
, fn
, is_friend
);
1405 /* The call to duplicate_decls will have applied
1408 An explicit specialization of a function template
1409 is inline only if it is explicitly declared to be,
1410 and independently of whether its function template
1413 to the primary function; now copy the inline bits to
1414 the various clones. */
1415 FOR_EACH_CLONE (clone
, fn
)
1417 DECL_DECLARED_INLINE_P (clone
)
1418 = DECL_DECLARED_INLINE_P (fn
);
1419 DECL_SOURCE_LOCATION (clone
)
1420 = DECL_SOURCE_LOCATION (fn
);
1422 check_specialization_namespace (tmpl
);
1427 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1429 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1430 /* Dup decl failed, but this is a new definition. Set the
1431 line number so any errors match this new
1433 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1439 return duplicate_decls (spec
, fn
, is_friend
);
1441 /* A specialization must be declared in the same namespace as the
1442 template it is specializing. */
1443 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1444 && !check_specialization_namespace (tmpl
))
1445 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1447 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1449 spec_entry
*entry
= ggc_alloc_spec_entry ();
1450 gcc_assert (tmpl
&& args
&& spec
);
1453 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1454 && PRIMARY_TEMPLATE_P (tmpl
)
1455 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1456 /* TMPL is a forward declaration of a template function; keep a list
1457 of all specializations in case we need to reassign them to a friend
1458 template later in tsubst_friend_function. */
1459 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1460 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1466 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1467 TMPL and ARGS members, ignores SPEC. */
1469 int comparing_specializations
;
1472 eq_specializations (const void *p1
, const void *p2
)
1474 const spec_entry
*e1
= (const spec_entry
*)p1
;
1475 const spec_entry
*e2
= (const spec_entry
*)p2
;
1478 ++comparing_specializations
;
1479 equal
= (e1
->tmpl
== e2
->tmpl
1480 && comp_template_args (e1
->args
, e2
->args
));
1481 --comparing_specializations
;
1486 /* Returns a hash for a template TMPL and template arguments ARGS. */
1489 hash_tmpl_and_args (tree tmpl
, tree args
)
1491 hashval_t val
= DECL_UID (tmpl
);
1492 return iterative_hash_template_arg (args
, val
);
1495 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1499 hash_specialization (const void *p
)
1501 const spec_entry
*e
= (const spec_entry
*)p
;
1502 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1505 /* Recursively calculate a hash value for a template argument ARG, for use
1506 in the hash tables of template specializations. */
1509 iterative_hash_template_arg (tree arg
, hashval_t val
)
1511 unsigned HOST_WIDE_INT i
;
1512 enum tree_code code
;
1515 if (arg
== NULL_TREE
)
1516 return iterative_hash_object (arg
, val
);
1521 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1522 /* We can get one of these when re-hashing a previous entry in the middle
1523 of substituting into a pack expansion. Just look through it. */
1524 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1526 code
= TREE_CODE (arg
);
1527 tclass
= TREE_CODE_CLASS (code
);
1529 val
= iterative_hash_object (code
, val
);
1536 case IDENTIFIER_NODE
:
1537 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1541 int i
, len
= TREE_VEC_LENGTH (arg
);
1542 for (i
= 0; i
< len
; ++i
)
1543 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1547 case TYPE_PACK_EXPANSION
:
1548 case EXPR_PACK_EXPANSION
:
1549 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1550 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1552 case TYPE_ARGUMENT_PACK
:
1553 case NONTYPE_ARGUMENT_PACK
:
1554 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1557 for (; arg
; arg
= TREE_CHAIN (arg
))
1558 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1562 for (; arg
; arg
= OVL_NEXT (arg
))
1563 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1569 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1571 val
= iterative_hash_template_arg (field
, val
);
1572 val
= iterative_hash_template_arg (value
, val
);
1578 if (!DECL_ARTIFICIAL (arg
))
1580 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1581 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1583 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1586 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1589 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1590 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1592 case TEMPLATE_PARM_INDEX
:
1593 val
= iterative_hash_template_arg
1594 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1595 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1596 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1599 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1600 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1601 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1604 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1606 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1610 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1611 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1612 val
= iterative_hash_object (code
, val
);
1613 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1616 /* A lambda can't appear in a template arg, but don't crash on
1618 gcc_assert (seen_error ());
1622 case IMPLICIT_CONV_EXPR
:
1623 case STATIC_CAST_EXPR
:
1624 case REINTERPRET_CAST_EXPR
:
1625 case CONST_CAST_EXPR
:
1626 case DYNAMIC_CAST_EXPR
:
1628 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1629 /* Now hash operands as usual. */
1639 if (TYPE_CANONICAL (arg
))
1640 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1642 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1643 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1644 /* Otherwise just compare the types during lookup. */
1647 case tcc_declaration
:
1649 return iterative_hash_expr (arg
, val
);
1652 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1654 unsigned n
= cp_tree_operand_length (arg
);
1655 for (i
= 0; i
< n
; ++i
)
1656 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1664 /* Unregister the specialization SPEC as a specialization of TMPL.
1665 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1666 if the SPEC was listed as a specialization of TMPL.
1668 Note that SPEC has been ggc_freed, so we can't look inside it. */
1671 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1676 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1677 elt
.args
= TI_ARGS (tinfo
);
1678 elt
.spec
= NULL_TREE
;
1680 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1683 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1684 gcc_assert (new_spec
!= NULL_TREE
);
1685 entry
->spec
= new_spec
;
1692 /* Like register_specialization, but for local declarations. We are
1693 registering SPEC, an instantiation of TMPL. */
1696 register_local_specialization (tree spec
, tree tmpl
)
1700 slot
= pointer_map_insert (local_specializations
, tmpl
);
1704 /* TYPE is a class type. Returns true if TYPE is an explicitly
1705 specialized class. */
1708 explicit_class_specialization_p (tree type
)
1710 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1712 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1715 /* Print the list of functions at FNS, going through all the overloads
1716 for each element of the list. Alternatively, FNS can not be a
1717 TREE_LIST, in which case it will be printed together with all the
1720 MORE and *STR should respectively be FALSE and NULL when the function
1721 is called from the outside. They are used internally on recursive
1722 calls. print_candidates manages the two parameters and leaves NULL
1723 in *STR when it ends. */
1726 print_candidates_1 (tree fns
, bool more
, const char **str
)
1729 char *spaces
= NULL
;
1731 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1732 if (TREE_CODE (fn
) == TREE_LIST
)
1734 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1735 print_candidates_1 (TREE_VALUE (fn2
),
1736 TREE_CHAIN (fn2
) || more
, str
);
1740 tree cand
= OVL_CURRENT (fn
);
1743 /* Pick the prefix string. */
1744 if (!more
&& !OVL_NEXT (fns
))
1746 inform (DECL_SOURCE_LOCATION (cand
),
1747 "candidate is: %#D", cand
);
1751 *str
= _("candidates are:");
1752 spaces
= get_spaces (*str
);
1754 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1755 *str
= spaces
? spaces
: *str
;
1765 /* Print the list of candidate FNS in an error message. FNS can also
1766 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1769 print_candidates (tree fns
)
1771 const char *str
= NULL
;
1772 print_candidates_1 (fns
, false, &str
);
1773 gcc_assert (str
== NULL
);
1776 /* Returns the template (one of the functions given by TEMPLATE_ID)
1777 which can be specialized to match the indicated DECL with the
1778 explicit template args given in TEMPLATE_ID. The DECL may be
1779 NULL_TREE if none is available. In that case, the functions in
1780 TEMPLATE_ID are non-members.
1782 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1783 specialization of a member template.
1785 The TEMPLATE_COUNT is the number of references to qualifying
1786 template classes that appeared in the name of the function. See
1787 check_explicit_specialization for a more accurate description.
1789 TSK indicates what kind of template declaration (if any) is being
1790 declared. TSK_TEMPLATE indicates that the declaration given by
1791 DECL, though a FUNCTION_DECL, has template parameters, and is
1792 therefore a template function.
1794 The template args (those explicitly specified and those deduced)
1795 are output in a newly created vector *TARGS_OUT.
1797 If it is impossible to determine the result, an error message is
1798 issued. The error_mark_node is returned to indicate failure. */
1801 determine_specialization (tree template_id
,
1804 int need_member_template
,
1810 tree explicit_targs
;
1811 tree candidates
= NULL_TREE
;
1812 /* A TREE_LIST of templates of which DECL may be a specialization.
1813 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1814 corresponding TREE_PURPOSE is the set of template arguments that,
1815 when used to instantiate the template, would produce a function
1816 with the signature of DECL. */
1817 tree templates
= NULL_TREE
;
1819 cp_binding_level
*b
;
1821 *targs_out
= NULL_TREE
;
1823 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1824 return error_mark_node
;
1826 /* We shouldn't be specializing a member template of an
1827 unspecialized class template; we already gave an error in
1828 check_specialization_scope, now avoid crashing. */
1829 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1830 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1832 gcc_assert (errorcount
);
1833 return error_mark_node
;
1836 fns
= TREE_OPERAND (template_id
, 0);
1837 explicit_targs
= TREE_OPERAND (template_id
, 1);
1839 if (fns
== error_mark_node
)
1840 return error_mark_node
;
1842 /* Check for baselinks. */
1843 if (BASELINK_P (fns
))
1844 fns
= BASELINK_FUNCTIONS (fns
);
1846 if (!is_overloaded_fn (fns
))
1848 error ("%qD is not a function template", fns
);
1849 return error_mark_node
;
1852 /* Count the number of template headers specified for this
1855 for (b
= current_binding_level
;
1856 b
->kind
== sk_template_parms
;
1860 for (; fns
; fns
= OVL_NEXT (fns
))
1862 tree fn
= OVL_CURRENT (fns
);
1864 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1866 tree decl_arg_types
;
1870 /* In case of explicit specialization, we need to check if
1871 the number of template headers appearing in the specialization
1872 is correct. This is usually done in check_explicit_specialization,
1873 but the check done there cannot be exhaustive when specializing
1874 member functions. Consider the following code:
1876 template <> void A<int>::f(int);
1877 template <> template <> void A<int>::f(int);
1879 Assuming that A<int> is not itself an explicit specialization
1880 already, the first line specializes "f" which is a non-template
1881 member function, whilst the second line specializes "f" which
1882 is a template member function. So both lines are syntactically
1883 correct, and check_explicit_specialization does not reject
1886 Here, we can do better, as we are matching the specialization
1887 against the declarations. We count the number of template
1888 headers, and we check if they match TEMPLATE_COUNT + 1
1889 (TEMPLATE_COUNT is the number of qualifying template classes,
1890 plus there must be another header for the member template
1893 Notice that if header_count is zero, this is not a
1894 specialization but rather a template instantiation, so there
1895 is no check we can perform here. */
1896 if (header_count
&& header_count
!= template_count
+ 1)
1899 /* Check that the number of template arguments at the
1900 innermost level for DECL is the same as for FN. */
1901 if (current_binding_level
->kind
== sk_template_parms
1902 && !current_binding_level
->explicit_spec_p
1903 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1904 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1905 (current_template_parms
))))
1908 /* DECL might be a specialization of FN. */
1909 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1910 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1912 /* For a non-static member function, we need to make sure
1913 that the const qualification is the same. Since
1914 get_bindings does not try to merge the "this" parameter,
1915 we must do the comparison explicitly. */
1916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1917 && !same_type_p (TREE_VALUE (fn_arg_types
),
1918 TREE_VALUE (decl_arg_types
)))
1921 /* Skip the "this" parameter and, for constructors of
1922 classes with virtual bases, the VTT parameter. A
1923 full specialization of a constructor will have a VTT
1924 parameter, but a template never will. */
1926 = skip_artificial_parms_for (decl
, decl_arg_types
);
1928 = skip_artificial_parms_for (fn
, fn_arg_types
);
1930 /* Function templates cannot be specializations; there are
1931 no partial specializations of functions. Therefore, if
1932 the type of DECL does not match FN, there is no
1934 if (tsk
== tsk_template
)
1936 if (compparms (fn_arg_types
, decl_arg_types
))
1937 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1941 /* See whether this function might be a specialization of this
1942 template. Suppress access control because we might be trying
1943 to make this specialization a friend, and we have already done
1944 access control for the declaration of the specialization. */
1945 push_deferring_access_checks (dk_no_check
);
1946 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1947 pop_deferring_access_checks ();
1950 /* We cannot deduce template arguments that when used to
1951 specialize TMPL will produce DECL. */
1954 /* Make sure that the deduced arguments actually work. */
1955 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1956 if (insttype
== error_mark_node
)
1959 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1960 if (!compparms (fn_arg_types
, decl_arg_types
))
1963 /* Save this template, and the arguments deduced. */
1964 templates
= tree_cons (targs
, fn
, templates
);
1966 else if (need_member_template
)
1967 /* FN is an ordinary member function, and we need a
1968 specialization of a member template. */
1970 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1971 /* We can get IDENTIFIER_NODEs here in certain erroneous
1974 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1975 /* This is just an ordinary non-member function. Nothing can
1976 be a specialization of that. */
1978 else if (DECL_ARTIFICIAL (fn
))
1979 /* Cannot specialize functions that are created implicitly. */
1983 tree decl_arg_types
;
1985 /* This is an ordinary member function. However, since
1986 we're here, we can assume it's enclosing class is a
1987 template class. For example,
1989 template <typename T> struct S { void f(); };
1990 template <> void S<int>::f() {}
1992 Here, S<int>::f is a non-template, but S<int> is a
1993 template class. If FN has the same type as DECL, we
1994 might be in business. */
1996 if (!DECL_TEMPLATE_INFO (fn
))
1997 /* Its enclosing class is an explicit specialization
1998 of a template class. This is not a candidate. */
2001 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2002 TREE_TYPE (TREE_TYPE (fn
))))
2003 /* The return types differ. */
2006 /* Adjust the type of DECL in case FN is a static member. */
2007 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2008 if (DECL_STATIC_FUNCTION_P (fn
)
2009 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2010 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2012 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2015 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2019 if (templates
&& TREE_CHAIN (templates
))
2025 It is possible for a specialization with a given function
2026 signature to be instantiated from more than one function
2027 template. In such cases, explicit specification of the
2028 template arguments must be used to uniquely identify the
2029 function template specialization being specialized.
2031 Note that here, there's no suggestion that we're supposed to
2032 determine which of the candidate templates is most
2033 specialized. However, we, also have:
2037 Partial ordering of overloaded function template
2038 declarations is used in the following contexts to select
2039 the function template to which a function template
2040 specialization refers:
2042 -- when an explicit specialization refers to a function
2045 So, we do use the partial ordering rules, at least for now.
2046 This extension can only serve to make invalid programs valid,
2047 so it's safe. And, there is strong anecdotal evidence that
2048 the committee intended the partial ordering rules to apply;
2049 the EDG front end has that behavior, and John Spicer claims
2050 that the committee simply forgot to delete the wording in
2051 [temp.expl.spec]. */
2052 tree tmpl
= most_specialized_instantiation (templates
);
2053 if (tmpl
!= error_mark_node
)
2056 TREE_CHAIN (templates
) = NULL_TREE
;
2060 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2062 error ("template-id %qD for %q+D does not match any template "
2063 "declaration", template_id
, decl
);
2064 if (header_count
&& header_count
!= template_count
+ 1)
2065 inform (input_location
, "saw %d %<template<>%>, need %d for "
2066 "specializing a member function template",
2067 header_count
, template_count
+ 1);
2068 return error_mark_node
;
2070 else if ((templates
&& TREE_CHAIN (templates
))
2071 || (candidates
&& TREE_CHAIN (candidates
))
2072 || (templates
&& candidates
))
2074 error ("ambiguous template specialization %qD for %q+D",
2076 candidates
= chainon (candidates
, templates
);
2077 print_candidates (candidates
);
2078 return error_mark_node
;
2081 /* We have one, and exactly one, match. */
2084 tree fn
= TREE_VALUE (candidates
);
2085 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2086 /* DECL is a re-declaration or partial instantiation of a template
2088 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2090 /* It was a specialization of an ordinary member function in a
2092 return DECL_TI_TEMPLATE (fn
);
2095 /* It was a specialization of a template. */
2096 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2097 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2099 *targs_out
= copy_node (targs
);
2100 SET_TMPL_ARGS_LEVEL (*targs_out
,
2101 TMPL_ARGS_DEPTH (*targs_out
),
2102 TREE_PURPOSE (templates
));
2105 *targs_out
= TREE_PURPOSE (templates
);
2106 return TREE_VALUE (templates
);
2109 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2110 but with the default argument values filled in from those in the
2114 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2117 tree new_spec_types
;
2122 if (spec_types
== void_list_node
)
2123 return void_list_node
;
2125 /* Substitute into the rest of the list. */
2127 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2128 TREE_CHAIN (tmpl_types
));
2130 /* Add the default argument for this parameter. */
2131 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2132 TREE_VALUE (spec_types
),
2136 /* DECL is an explicit specialization. Replicate default arguments
2137 from the template it specializes. (That way, code like:
2139 template <class T> void f(T = 3);
2140 template <> void f(double);
2143 works, as required.) An alternative approach would be to look up
2144 the correct default arguments at the call-site, but this approach
2145 is consistent with how implicit instantiations are handled. */
2148 copy_default_args_to_explicit_spec (tree decl
)
2153 tree new_spec_types
;
2157 tree object_type
= NULL_TREE
;
2158 tree in_charge
= NULL_TREE
;
2159 tree vtt
= NULL_TREE
;
2161 /* See if there's anything we need to do. */
2162 tmpl
= DECL_TI_TEMPLATE (decl
);
2163 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2164 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2165 if (TREE_PURPOSE (t
))
2170 old_type
= TREE_TYPE (decl
);
2171 spec_types
= TYPE_ARG_TYPES (old_type
);
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2175 /* Remove the this pointer, but remember the object's type for
2177 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2178 spec_types
= TREE_CHAIN (spec_types
);
2179 tmpl_types
= TREE_CHAIN (tmpl_types
);
2181 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2183 /* DECL may contain more parameters than TMPL due to the extra
2184 in-charge parameter in constructors and destructors. */
2185 in_charge
= spec_types
;
2186 spec_types
= TREE_CHAIN (spec_types
);
2188 if (DECL_HAS_VTT_PARM_P (decl
))
2191 spec_types
= TREE_CHAIN (spec_types
);
2195 /* Compute the merged default arguments. */
2197 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2199 /* Compute the new FUNCTION_TYPE. */
2203 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2208 /* Put the in-charge parameter back. */
2209 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2210 TREE_VALUE (in_charge
),
2213 new_type
= build_method_type_directly (object_type
,
2214 TREE_TYPE (old_type
),
2218 new_type
= build_function_type (TREE_TYPE (old_type
),
2220 new_type
= cp_build_type_attribute_variant (new_type
,
2221 TYPE_ATTRIBUTES (old_type
));
2222 new_type
= build_exception_variant (new_type
,
2223 TYPE_RAISES_EXCEPTIONS (old_type
));
2224 TREE_TYPE (decl
) = new_type
;
2227 /* Return the number of template headers we expect to see for a definition
2228 or specialization of CTYPE or one of its non-template members. */
2231 num_template_headers_for_class (tree ctype
)
2233 int num_templates
= 0;
2235 while (ctype
&& CLASS_TYPE_P (ctype
))
2237 /* You're supposed to have one `template <...>' for every
2238 template class, but you don't need one for a full
2239 specialization. For example:
2241 template <class T> struct S{};
2242 template <> struct S<int> { void f(); };
2243 void S<int>::f () {}
2245 is correct; there shouldn't be a `template <>' for the
2246 definition of `S<int>::f'. */
2247 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2248 /* If CTYPE does not have template information of any
2249 kind, then it is not a template, nor is it nested
2250 within a template. */
2252 if (explicit_class_specialization_p (ctype
))
2254 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2257 ctype
= TYPE_CONTEXT (ctype
);
2260 return num_templates
;
2263 /* Do a simple sanity check on the template headers that precede the
2264 variable declaration DECL. */
2267 check_template_variable (tree decl
)
2269 tree ctx
= CP_DECL_CONTEXT (decl
);
2270 int wanted
= num_template_headers_for_class (ctx
);
2271 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2272 permerror (DECL_SOURCE_LOCATION (decl
),
2273 "%qD is not a static data member of a class template", decl
);
2274 else if (template_header_count
> wanted
)
2276 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2277 "too many template headers for %D (should be %d)",
2279 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2280 inform (DECL_SOURCE_LOCATION (decl
),
2281 "members of an explicitly specialized class are defined "
2282 "without a template header");
2286 /* Check to see if the function just declared, as indicated in
2287 DECLARATOR, and in DECL, is a specialization of a function
2288 template. We may also discover that the declaration is an explicit
2289 instantiation at this point.
2291 Returns DECL, or an equivalent declaration that should be used
2292 instead if all goes well. Issues an error message if something is
2293 amiss. Returns error_mark_node if the error is not easily
2296 FLAGS is a bitmask consisting of the following flags:
2298 2: The function has a definition.
2299 4: The function is a friend.
2301 The TEMPLATE_COUNT is the number of references to qualifying
2302 template classes that appeared in the name of the function. For
2305 template <class T> struct S { void f(); };
2308 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2309 classes are not counted in the TEMPLATE_COUNT, so that in
2311 template <class T> struct S {};
2312 template <> struct S<int> { void f(); }
2313 template <> void S<int>::f();
2315 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2316 invalid; there should be no template <>.)
2318 If the function is a specialization, it is marked as such via
2319 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2320 is set up correctly, and it is added to the list of specializations
2321 for that template. */
2324 check_explicit_specialization (tree declarator
,
2329 int have_def
= flags
& 2;
2330 int is_friend
= flags
& 4;
2331 int specialization
= 0;
2332 int explicit_instantiation
= 0;
2333 int member_specialization
= 0;
2334 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2335 tree dname
= DECL_NAME (decl
);
2340 if (!processing_specialization
)
2343 tsk
= tsk_excessive_parms
;
2346 tsk
= current_tmpl_spec_kind (template_count
);
2351 if (processing_specialization
)
2354 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2356 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2359 /* This could be something like:
2361 template <class T> void f(T);
2362 class S { friend void f<>(int); } */
2366 /* This case handles bogus declarations like template <>
2367 template <class T> void f<int>(); */
2369 error ("template-id %qD in declaration of primary template",
2376 case tsk_invalid_member_spec
:
2377 /* The error has already been reported in
2378 check_specialization_scope. */
2379 return error_mark_node
;
2381 case tsk_invalid_expl_inst
:
2382 error ("template parameter list used in explicit instantiation");
2388 error ("definition provided for explicit instantiation");
2390 explicit_instantiation
= 1;
2393 case tsk_excessive_parms
:
2394 case tsk_insufficient_parms
:
2395 if (tsk
== tsk_excessive_parms
)
2396 error ("too many template parameter lists in declaration of %qD",
2398 else if (template_header_count
)
2399 error("too few template parameter lists in declaration of %qD", decl
);
2401 error("explicit specialization of %qD must be introduced by "
2402 "%<template <>%>", decl
);
2406 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2408 member_specialization
= 1;
2414 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2416 /* This case handles bogus declarations like template <>
2417 template <class T> void f<int>(); */
2419 if (uses_template_parms (declarator
))
2420 error ("function template partial specialization %qD "
2421 "is not allowed", declarator
);
2423 error ("template-id %qD in declaration of primary template",
2428 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2429 /* This is a specialization of a member template, without
2430 specialization the containing class. Something like:
2432 template <class T> struct S {
2433 template <class U> void f (U);
2435 template <> template <class U> void S<int>::f(U) {}
2437 That's a specialization -- but of the entire template. */
2445 if (specialization
|| member_specialization
)
2447 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2448 for (; t
; t
= TREE_CHAIN (t
))
2449 if (TREE_PURPOSE (t
))
2451 permerror (input_location
,
2452 "default argument specified in explicit specialization");
2457 if (specialization
|| member_specialization
|| explicit_instantiation
)
2459 tree tmpl
= NULL_TREE
;
2460 tree targs
= NULL_TREE
;
2462 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2463 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2467 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2472 /* If there is no class context, the explicit instantiation
2473 must be at namespace scope. */
2474 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2476 /* Find the namespace binding, using the declaration
2478 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2480 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2482 error ("%qD is not a template function", dname
);
2483 fns
= error_mark_node
;
2487 tree fn
= OVL_CURRENT (fns
);
2488 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2489 CP_DECL_CONTEXT (fn
)))
2490 error ("%qD is not declared in %qD",
2491 decl
, current_namespace
);
2495 declarator
= lookup_template_function (fns
, NULL_TREE
);
2498 if (declarator
== error_mark_node
)
2499 return error_mark_node
;
2501 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2503 if (!explicit_instantiation
)
2504 /* A specialization in class scope. This is invalid,
2505 but the error will already have been flagged by
2506 check_specialization_scope. */
2507 return error_mark_node
;
2510 /* It's not valid to write an explicit instantiation in
2513 class C { template void f(); }
2515 This case is caught by the parser. However, on
2518 template class C { void f(); };
2520 (which is invalid) we can get here. The error will be
2527 else if (ctype
!= NULL_TREE
2528 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2531 /* Find the list of functions in ctype that have the same
2532 name as the declared function. */
2533 tree name
= TREE_OPERAND (declarator
, 0);
2534 tree fns
= NULL_TREE
;
2537 if (constructor_name_p (name
, ctype
))
2539 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2541 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2542 : !CLASSTYPE_DESTRUCTORS (ctype
))
2544 /* From [temp.expl.spec]:
2546 If such an explicit specialization for the member
2547 of a class template names an implicitly-declared
2548 special member function (clause _special_), the
2549 program is ill-formed.
2551 Similar language is found in [temp.explicit]. */
2552 error ("specialization of implicitly-declared special member function");
2553 return error_mark_node
;
2556 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2559 if (!DECL_CONV_FN_P (decl
))
2561 idx
= lookup_fnfields_1 (ctype
, name
);
2563 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2567 vec
<tree
, va_gc
> *methods
;
2570 /* For a type-conversion operator, we cannot do a
2571 name-based lookup. We might be looking for `operator
2572 int' which will be a specialization of `operator T'.
2573 So, we find *all* the conversion operators, and then
2574 select from them. */
2577 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2579 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2580 methods
->iterate (idx
, &ovl
);
2583 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2584 /* There are no more conversion functions. */
2587 /* Glue all these conversion functions together
2588 with those we already have. */
2589 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2590 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2594 if (fns
== NULL_TREE
)
2596 error ("no member function %qD declared in %qT", name
, ctype
);
2597 return error_mark_node
;
2600 TREE_OPERAND (declarator
, 0) = fns
;
2603 /* Figure out what exactly is being specialized at this point.
2604 Note that for an explicit instantiation, even one for a
2605 member function, we cannot tell apriori whether the
2606 instantiation is for a member template, or just a member
2607 function of a template class. Even if a member template is
2608 being instantiated, the member template arguments may be
2609 elided if they can be deduced from the rest of the
2611 tmpl
= determine_specialization (declarator
, decl
,
2613 member_specialization
,
2617 if (!tmpl
|| tmpl
== error_mark_node
)
2618 /* We couldn't figure out what this declaration was
2620 return error_mark_node
;
2623 tree gen_tmpl
= most_general_template (tmpl
);
2625 if (explicit_instantiation
)
2627 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2628 is done by do_decl_instantiation later. */
2630 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2631 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2633 if (arg_depth
> parm_depth
)
2635 /* If TMPL is not the most general template (for
2636 example, if TMPL is a friend template that is
2637 injected into namespace scope), then there will
2638 be too many levels of TARGS. Remove some of them
2643 new_targs
= make_tree_vec (parm_depth
);
2644 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2645 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2646 = TREE_VEC_ELT (targs
, i
);
2650 return instantiate_template (tmpl
, targs
, tf_error
);
2653 /* If we thought that the DECL was a member function, but it
2654 turns out to be specializing a static member function,
2655 make DECL a static member function as well. */
2656 if (DECL_STATIC_FUNCTION_P (tmpl
)
2657 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2658 revert_static_member_fn (decl
);
2660 /* If this is a specialization of a member template of a
2661 template class, we want to return the TEMPLATE_DECL, not
2662 the specialization of it. */
2663 if (tsk
== tsk_template
)
2665 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2666 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2667 DECL_INITIAL (result
) = NULL_TREE
;
2671 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2672 DECL_SOURCE_LOCATION (result
)
2673 = DECL_SOURCE_LOCATION (decl
);
2674 /* We want to use the argument list specified in the
2675 definition, not in the original declaration. */
2676 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2677 for (parm
= DECL_ARGUMENTS (result
); parm
;
2678 parm
= DECL_CHAIN (parm
))
2679 DECL_CONTEXT (parm
) = result
;
2681 return register_specialization (tmpl
, gen_tmpl
, targs
,
2685 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2686 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2688 /* Inherit default function arguments from the template
2689 DECL is specializing. */
2690 copy_default_args_to_explicit_spec (decl
);
2692 /* This specialization has the same protection as the
2693 template it specializes. */
2694 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2695 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2697 /* 7.1.1-1 [dcl.stc]
2699 A storage-class-specifier shall not be specified in an
2700 explicit specialization...
2702 The parser rejects these, so unless action is taken here,
2703 explicit function specializations will always appear with
2706 The action recommended by the C++ CWG in response to C++
2707 defect report 605 is to make the storage class and linkage
2708 of the explicit specialization match the templated function:
2710 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2712 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2714 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2715 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2717 /* This specialization has the same linkage and visibility as
2718 the function template it specializes. */
2719 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2720 if (! TREE_PUBLIC (decl
))
2722 DECL_INTERFACE_KNOWN (decl
) = 1;
2723 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2725 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2726 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2728 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2729 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2733 /* If DECL is a friend declaration, declared using an
2734 unqualified name, the namespace associated with DECL may
2735 have been set incorrectly. For example, in:
2737 template <typename T> void f(T);
2739 struct S { friend void f<int>(int); }
2742 we will have set the DECL_CONTEXT for the friend
2743 declaration to N, rather than to the global namespace. */
2744 if (DECL_NAMESPACE_SCOPE_P (decl
))
2745 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2747 if (is_friend
&& !have_def
)
2748 /* This is not really a declaration of a specialization.
2749 It's just the name of an instantiation. But, it's not
2750 a request for an instantiation, either. */
2751 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2752 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2753 /* This is indeed a specialization. In case of constructors
2754 and destructors, we need in-charge and not-in-charge
2755 versions in V3 ABI. */
2756 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2758 /* Register this specialization so that we can find it
2760 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2767 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2768 parameters. These are represented in the same format used for
2769 DECL_TEMPLATE_PARMS. */
2772 comp_template_parms (const_tree parms1
, const_tree parms2
)
2777 if (parms1
== parms2
)
2780 for (p1
= parms1
, p2
= parms2
;
2781 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2782 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2784 tree t1
= TREE_VALUE (p1
);
2785 tree t2
= TREE_VALUE (p2
);
2788 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2789 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2791 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2794 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2796 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2797 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2799 /* If either of the template parameters are invalid, assume
2800 they match for the sake of error recovery. */
2801 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2804 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2807 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2808 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2809 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2811 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2816 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2817 /* One set of parameters has more parameters lists than the
2824 /* Determine whether PARM is a parameter pack. */
2827 template_parameter_pack_p (const_tree parm
)
2829 /* Determine if we have a non-type template parameter pack. */
2830 if (TREE_CODE (parm
) == PARM_DECL
)
2831 return (DECL_TEMPLATE_PARM_P (parm
)
2832 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2833 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2834 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2836 /* If this is a list of template parameters, we could get a
2837 TYPE_DECL or a TEMPLATE_DECL. */
2838 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2839 parm
= TREE_TYPE (parm
);
2841 /* Otherwise it must be a type template parameter. */
2842 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2843 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2844 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2847 /* Determine if T is a function parameter pack. */
2850 function_parameter_pack_p (const_tree t
)
2852 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2853 return FUNCTION_PARAMETER_PACK_P (t
);
2857 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2858 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2861 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2863 if (! primary_func_tmpl_inst
2864 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2865 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2868 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2871 /* Return true iff the function parameter PARAM_DECL was expanded
2872 from the function parameter pack PACK. */
2875 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2877 if (DECL_ARTIFICIAL (param_decl
)
2878 || !function_parameter_pack_p (pack
))
2881 /* The parameter pack and its pack arguments have the same
2883 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2886 /* Determine whether ARGS describes a variadic template args list,
2887 i.e., one that is terminated by a template argument pack. */
2890 template_args_variadic_p (tree args
)
2895 if (args
== NULL_TREE
)
2898 args
= INNERMOST_TEMPLATE_ARGS (args
);
2899 nargs
= TREE_VEC_LENGTH (args
);
2904 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2906 return ARGUMENT_PACK_P (last_parm
);
2909 /* Generate a new name for the parameter pack name NAME (an
2910 IDENTIFIER_NODE) that incorporates its */
2913 make_ith_pack_parameter_name (tree name
, int i
)
2915 /* Munge the name to include the parameter index. */
2916 #define NUMBUF_LEN 128
2917 char numbuf
[NUMBUF_LEN
];
2921 if (name
== NULL_TREE
)
2923 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2924 newname_len
= IDENTIFIER_LENGTH (name
)
2925 + strlen (numbuf
) + 2;
2926 newname
= (char*)alloca (newname_len
);
2927 snprintf (newname
, newname_len
,
2928 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2929 return get_identifier (newname
);
2932 /* Return true if T is a primary function, class or alias template
2936 primary_template_instantiation_p (const_tree t
)
2941 if (TREE_CODE (t
) == FUNCTION_DECL
)
2942 return DECL_LANG_SPECIFIC (t
)
2943 && DECL_TEMPLATE_INSTANTIATION (t
)
2944 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2945 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2946 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2947 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2948 else if (alias_template_specialization_p (t
))
2953 /* Return true if PARM is a template template parameter. */
2956 template_template_parameter_p (const_tree parm
)
2958 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2961 /* Return true iff PARM is a DECL representing a type template
2965 template_type_parameter_p (const_tree parm
)
2968 && (TREE_CODE (parm
) == TYPE_DECL
2969 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2970 && DECL_TEMPLATE_PARM_P (parm
));
2973 /* Return the template parameters of T if T is a
2974 primary template instantiation, NULL otherwise. */
2977 get_primary_template_innermost_parameters (const_tree t
)
2979 tree parms
= NULL
, template_info
= NULL
;
2981 if ((template_info
= get_template_info (t
))
2982 && primary_template_instantiation_p (t
))
2983 parms
= INNERMOST_TEMPLATE_PARMS
2984 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2989 /* Return the template parameters of the LEVELth level from the full list
2990 of template parameters PARMS. */
2993 get_template_parms_at_level (tree parms
, int level
)
2997 || TREE_CODE (parms
) != TREE_LIST
2998 || level
> TMPL_PARMS_DEPTH (parms
))
3001 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3002 if (TMPL_PARMS_DEPTH (p
) == level
)
3008 /* Returns the template arguments of T if T is a template instantiation,
3012 get_template_innermost_arguments (const_tree t
)
3014 tree args
= NULL
, template_info
= NULL
;
3016 if ((template_info
= get_template_info (t
))
3017 && TI_ARGS (template_info
))
3018 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3023 /* Return the argument pack elements of T if T is a template argument pack,
3027 get_template_argument_pack_elems (const_tree t
)
3029 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3030 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3033 return ARGUMENT_PACK_ARGS (t
);
3036 /* Structure used to track the progress of find_parameter_packs_r. */
3037 struct find_parameter_pack_data
3039 /* TREE_LIST that will contain all of the parameter packs found by
3041 tree
* parameter_packs
;
3043 /* Set of AST nodes that have been visited by the traversal. */
3044 struct pointer_set_t
*visited
;
3047 /* Identifies all of the argument packs that occur in a template
3048 argument and appends them to the TREE_LIST inside DATA, which is a
3049 find_parameter_pack_data structure. This is a subroutine of
3050 make_pack_expansion and uses_parameter_packs. */
3052 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3055 struct find_parameter_pack_data
* ppd
=
3056 (struct find_parameter_pack_data
*)data
;
3057 bool parameter_pack_p
= false;
3059 /* Handle type aliases/typedefs. */
3062 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
3063 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3065 if (TYPE_TEMPLATE_INFO (t
))
3066 cp_walk_tree (&TYPE_TI_ARGS (t
),
3067 &find_parameter_packs_r
,
3073 /* Identify whether this is a parameter pack or not. */
3074 switch (TREE_CODE (t
))
3076 case TEMPLATE_PARM_INDEX
:
3077 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3078 parameter_pack_p
= true;
3081 case TEMPLATE_TYPE_PARM
:
3082 t
= TYPE_MAIN_VARIANT (t
);
3083 case TEMPLATE_TEMPLATE_PARM
:
3084 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3085 parameter_pack_p
= true;
3089 if (FUNCTION_PARAMETER_PACK_P (t
))
3091 /* We don't want to walk into the type of a PARM_DECL,
3092 because we don't want to see the type parameter pack. */
3094 parameter_pack_p
= true;
3099 parameter_pack_p
= true;
3102 /* Not a parameter pack. */
3106 if (parameter_pack_p
)
3108 /* Add this parameter pack to the list. */
3109 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3113 cp_walk_tree (&TYPE_CONTEXT (t
),
3114 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3116 /* This switch statement will return immediately if we don't find a
3118 switch (TREE_CODE (t
))
3120 case TEMPLATE_PARM_INDEX
:
3123 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3124 /* Check the template itself. */
3125 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3126 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3127 /* Check the template arguments. */
3128 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3133 case TEMPLATE_TYPE_PARM
:
3134 case TEMPLATE_TEMPLATE_PARM
:
3141 if (TYPE_PTRMEMFUNC_P (t
))
3147 if (TYPE_TEMPLATE_INFO (t
))
3148 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3149 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3156 cp_walk_tree (&TREE_TYPE (t
),
3157 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3161 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3166 case TYPE_PACK_EXPANSION
:
3167 case EXPR_PACK_EXPANSION
:
3172 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3177 case IDENTIFIER_NODE
:
3178 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3190 /* Determines if the expression or type T uses any parameter packs. */
3192 uses_parameter_packs (tree t
)
3194 tree parameter_packs
= NULL_TREE
;
3195 struct find_parameter_pack_data ppd
;
3196 ppd
.parameter_packs
= ¶meter_packs
;
3197 ppd
.visited
= pointer_set_create ();
3198 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3199 pointer_set_destroy (ppd
.visited
);
3200 return parameter_packs
!= NULL_TREE
;
3203 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3204 representation a base-class initializer into a parameter pack
3205 expansion. If all goes well, the resulting node will be an
3206 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3209 make_pack_expansion (tree arg
)
3212 tree parameter_packs
= NULL_TREE
;
3213 bool for_types
= false;
3214 struct find_parameter_pack_data ppd
;
3216 if (!arg
|| arg
== error_mark_node
)
3219 if (TREE_CODE (arg
) == TREE_LIST
)
3221 /* The only time we will see a TREE_LIST here is for a base
3222 class initializer. In this case, the TREE_PURPOSE will be a
3223 _TYPE node (representing the base class expansion we're
3224 initializing) and the TREE_VALUE will be a TREE_LIST
3225 containing the initialization arguments.
3227 The resulting expansion looks somewhat different from most
3228 expansions. Rather than returning just one _EXPANSION, we
3229 return a TREE_LIST whose TREE_PURPOSE is a
3230 TYPE_PACK_EXPANSION containing the bases that will be
3231 initialized. The TREE_VALUE will be identical to the
3232 original TREE_VALUE, which is a list of arguments that will
3233 be passed to each base. We do not introduce any new pack
3234 expansion nodes into the TREE_VALUE (although it is possible
3235 that some already exist), because the TREE_PURPOSE and
3236 TREE_VALUE all need to be expanded together with the same
3237 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3238 resulting TREE_PURPOSE will mention the parameter packs in
3239 both the bases and the arguments to the bases. */
3242 tree parameter_packs
= NULL_TREE
;
3244 /* Determine which parameter packs will be used by the base
3246 ppd
.visited
= pointer_set_create ();
3247 ppd
.parameter_packs
= ¶meter_packs
;
3248 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3251 if (parameter_packs
== NULL_TREE
)
3253 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3254 pointer_set_destroy (ppd
.visited
);
3255 return error_mark_node
;
3258 if (TREE_VALUE (arg
) != void_type_node
)
3260 /* Collect the sets of parameter packs used in each of the
3261 initialization arguments. */
3262 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3264 /* Determine which parameter packs will be expanded in this
3266 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3271 pointer_set_destroy (ppd
.visited
);
3273 /* Create the pack expansion type for the base type. */
3274 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3275 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3276 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3278 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3279 they will rarely be compared to anything. */
3280 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3282 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3285 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3288 /* Build the PACK_EXPANSION_* node. */
3290 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3291 : make_node (EXPR_PACK_EXPANSION
);
3292 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3293 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3295 /* Propagate type and const-expression information. */
3296 TREE_TYPE (result
) = TREE_TYPE (arg
);
3297 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3300 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3301 they will rarely be compared to anything. */
3302 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3304 /* Determine which parameter packs will be expanded. */
3305 ppd
.parameter_packs
= ¶meter_packs
;
3306 ppd
.visited
= pointer_set_create ();
3307 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3308 pointer_set_destroy (ppd
.visited
);
3310 /* Make sure we found some parameter packs. */
3311 if (parameter_packs
== NULL_TREE
)
3314 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3316 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3317 return error_mark_node
;
3319 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3321 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3326 /* Checks T for any "bare" parameter packs, which have not yet been
3327 expanded, and issues an error if any are found. This operation can
3328 only be done on full expressions or types (e.g., an expression
3329 statement, "if" condition, etc.), because we could have expressions like:
3331 foo(f(g(h(args)))...)
3333 where "args" is a parameter pack. check_for_bare_parameter_packs
3334 should not be called for the subexpressions args, h(args),
3335 g(h(args)), or f(g(h(args))), because we would produce erroneous
3338 Returns TRUE and emits an error if there were bare parameter packs,
3339 returns FALSE otherwise. */
3341 check_for_bare_parameter_packs (tree t
)
3343 tree parameter_packs
= NULL_TREE
;
3344 struct find_parameter_pack_data ppd
;
3346 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3349 if (TREE_CODE (t
) == TYPE_DECL
)
3352 ppd
.parameter_packs
= ¶meter_packs
;
3353 ppd
.visited
= pointer_set_create ();
3354 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3355 pointer_set_destroy (ppd
.visited
);
3357 if (parameter_packs
)
3359 error ("parameter packs not expanded with %<...%>:");
3360 while (parameter_packs
)
3362 tree pack
= TREE_VALUE (parameter_packs
);
3363 tree name
= NULL_TREE
;
3365 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3366 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3367 name
= TYPE_NAME (pack
);
3368 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3369 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3371 name
= DECL_NAME (pack
);
3374 inform (input_location
, " %qD", name
);
3376 inform (input_location
, " <anonymous>");
3378 parameter_packs
= TREE_CHAIN (parameter_packs
);
3387 /* Expand any parameter packs that occur in the template arguments in
3390 expand_template_argument_pack (tree args
)
3392 tree result_args
= NULL_TREE
;
3393 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3394 int num_result_args
= -1;
3395 int non_default_args_count
= -1;
3397 /* First, determine if we need to expand anything, and the number of
3398 slots we'll need. */
3399 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3401 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3402 if (arg
== NULL_TREE
)
3404 if (ARGUMENT_PACK_P (arg
))
3406 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3407 if (num_result_args
< 0)
3408 num_result_args
= in_arg
+ num_packed
;
3410 num_result_args
+= num_packed
;
3414 if (num_result_args
>= 0)
3419 /* If no expansion is necessary, we're done. */
3420 if (num_result_args
< 0)
3423 /* Expand arguments. */
3424 result_args
= make_tree_vec (num_result_args
);
3425 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3426 non_default_args_count
=
3427 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3428 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3430 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3431 if (ARGUMENT_PACK_P (arg
))
3433 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3434 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3435 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3436 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3437 if (non_default_args_count
> 0)
3438 non_default_args_count
+= num_packed
;
3442 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3446 if (non_default_args_count
>= 0)
3447 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3451 /* Checks if DECL shadows a template parameter.
3453 [temp.local]: A template-parameter shall not be redeclared within its
3454 scope (including nested scopes).
3456 Emits an error and returns TRUE if the DECL shadows a parameter,
3457 returns FALSE otherwise. */
3460 check_template_shadow (tree decl
)
3464 /* If we're not in a template, we can't possibly shadow a template
3466 if (!current_template_parms
)
3469 /* Figure out what we're shadowing. */
3470 if (TREE_CODE (decl
) == OVERLOAD
)
3471 decl
= OVL_CURRENT (decl
);
3472 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3474 /* If there's no previous binding for this name, we're not shadowing
3475 anything, let alone a template parameter. */
3479 /* If we're not shadowing a template parameter, we're done. Note
3480 that OLDDECL might be an OVERLOAD (or perhaps even an
3481 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3483 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3486 /* We check for decl != olddecl to avoid bogus errors for using a
3487 name inside a class. We check TPFI to avoid duplicate errors for
3488 inline member templates. */
3490 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3493 error ("declaration of %q+#D", decl
);
3494 error (" shadows template parm %q+#D", olddecl
);
3498 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3499 ORIG_LEVEL, DECL, and TYPE. */
3502 build_template_parm_index (int index
,
3508 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3509 TEMPLATE_PARM_IDX (t
) = index
;
3510 TEMPLATE_PARM_LEVEL (t
) = level
;
3511 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3512 TEMPLATE_PARM_DECL (t
) = decl
;
3513 TREE_TYPE (t
) = type
;
3514 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3515 TREE_READONLY (t
) = TREE_READONLY (decl
);
3520 /* Find the canonical type parameter for the given template type
3521 parameter. Returns the canonical type parameter, which may be TYPE
3522 if no such parameter existed. */
3525 canonical_type_parameter (tree type
)
3528 int idx
= TEMPLATE_TYPE_IDX (type
);
3529 if (!canonical_template_parms
)
3530 vec_alloc (canonical_template_parms
, idx
+1);
3532 while (canonical_template_parms
->length () <= (unsigned)idx
)
3533 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3535 list
= (*canonical_template_parms
)[idx
];
3536 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3537 list
= TREE_CHAIN (list
);
3540 return TREE_VALUE (list
);
3543 (*canonical_template_parms
)[idx
]
3544 = tree_cons (NULL_TREE
, type
,
3545 (*canonical_template_parms
)[idx
]);
3550 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3551 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3552 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3553 new one is created. */
3556 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3557 tsubst_flags_t complain
)
3559 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3560 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3561 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3562 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3564 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3567 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3568 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3569 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3570 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3571 DECL_ARTIFICIAL (decl
) = 1;
3572 SET_DECL_TEMPLATE_PARM_P (decl
);
3574 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3575 TEMPLATE_PARM_LEVEL (index
) - levels
,
3576 TEMPLATE_PARM_ORIG_LEVEL (index
),
3578 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3579 TEMPLATE_PARM_PARAMETER_PACK (t
)
3580 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3582 /* Template template parameters need this. */
3583 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3584 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3585 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3589 return TEMPLATE_PARM_DESCENDANTS (index
);
3592 /* Process information from new template parameter PARM and append it
3593 to the LIST being built. This new parameter is a non-type
3594 parameter iff IS_NON_TYPE is true. This new parameter is a
3595 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3596 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3597 parameter list PARM belongs to. This is used used to create a
3598 proper canonical type for the type of PARM that is to be created,
3599 iff PARM is a type. If the size is not known, this parameter shall
3603 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3604 bool is_non_type
, bool is_parameter_pack
)
3611 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3612 defval
= TREE_PURPOSE (parm
);
3616 tree p
= tree_last (list
);
3618 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3621 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3622 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3624 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3634 parm
= TREE_VALUE (parm
);
3636 SET_DECL_TEMPLATE_PARM_P (parm
);
3638 if (TREE_TYPE (parm
) == error_mark_node
)
3640 err_parm_list
= build_tree_list (defval
, parm
);
3641 TREE_VALUE (err_parm_list
) = error_mark_node
;
3642 return chainon (list
, err_parm_list
);
3648 The top-level cv-qualifiers on the template-parameter are
3649 ignored when determining its type. */
3650 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3651 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3653 err_parm_list
= build_tree_list (defval
, parm
);
3654 TREE_VALUE (err_parm_list
) = error_mark_node
;
3655 return chainon (list
, err_parm_list
);
3658 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3660 /* This template parameter is not a parameter pack, but it
3661 should be. Complain about "bare" parameter packs. */
3662 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3664 /* Recover by calling this a parameter pack. */
3665 is_parameter_pack
= true;
3669 /* A template parameter is not modifiable. */
3670 TREE_CONSTANT (parm
) = 1;
3671 TREE_READONLY (parm
) = 1;
3672 decl
= build_decl (parm_loc
,
3673 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3674 TREE_CONSTANT (decl
) = 1;
3675 TREE_READONLY (decl
) = 1;
3676 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3677 = build_template_parm_index (idx
, processing_template_decl
,
3678 processing_template_decl
,
3679 decl
, TREE_TYPE (parm
));
3681 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3682 = is_parameter_pack
;
3687 parm
= TREE_VALUE (TREE_VALUE (parm
));
3689 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3691 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3692 /* This is for distinguishing between real templates and template
3693 template parameters */
3694 TREE_TYPE (parm
) = t
;
3695 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3700 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3701 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3702 decl
= build_decl (parm_loc
,
3703 TYPE_DECL
, parm
, t
);
3706 TYPE_NAME (t
) = decl
;
3707 TYPE_STUB_DECL (t
) = decl
;
3709 TEMPLATE_TYPE_PARM_INDEX (t
)
3710 = build_template_parm_index (idx
, processing_template_decl
,
3711 processing_template_decl
,
3712 decl
, TREE_TYPE (parm
));
3713 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3714 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3716 DECL_ARTIFICIAL (decl
) = 1;
3717 SET_DECL_TEMPLATE_PARM_P (decl
);
3719 parm
= build_tree_list (defval
, parm
);
3720 return chainon (list
, parm
);
3723 /* The end of a template parameter list has been reached. Process the
3724 tree list into a parameter vector, converting each parameter into a more
3725 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3729 end_template_parm_list (tree parms
)
3733 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3735 current_template_parms
3736 = tree_cons (size_int (processing_template_decl
),
3737 saved_parmlist
, current_template_parms
);
3739 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3741 next
= TREE_CHAIN (parm
);
3742 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3743 TREE_CHAIN (parm
) = NULL_TREE
;
3746 --processing_template_parmlist
;
3748 return saved_parmlist
;
3751 /* end_template_decl is called after a template declaration is seen. */
3754 end_template_decl (void)
3756 reset_specialization ();
3758 if (! processing_template_decl
)
3761 /* This matches the pushlevel in begin_template_parm_list. */
3764 --processing_template_decl
;
3765 current_template_parms
= TREE_CHAIN (current_template_parms
);
3768 /* Takes a TREE_LIST representing a template parameter and convert it
3769 into an argument suitable to be passed to the type substitution
3770 functions. Note that If the TREE_LIST contains an error_mark
3771 node, the returned argument is error_mark_node. */
3774 template_parm_to_arg (tree t
)
3778 || TREE_CODE (t
) != TREE_LIST
)
3781 if (error_operand_p (TREE_VALUE (t
)))
3782 return error_mark_node
;
3786 if (TREE_CODE (t
) == TYPE_DECL
3787 || TREE_CODE (t
) == TEMPLATE_DECL
)
3791 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3793 /* Turn this argument into a TYPE_ARGUMENT_PACK
3794 with a single element, which expands T. */
3795 tree vec
= make_tree_vec (1);
3796 #ifdef ENABLE_CHECKING
3797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3798 (vec
, TREE_VEC_LENGTH (vec
));
3800 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3802 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3803 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3808 t
= DECL_INITIAL (t
);
3810 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3812 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3813 with a single element, which expands T. */
3814 tree vec
= make_tree_vec (1);
3815 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3816 #ifdef ENABLE_CHECKING
3817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3818 (vec
, TREE_VEC_LENGTH (vec
));
3820 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3822 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3823 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3824 TREE_TYPE (t
) = type
;
3830 /* Given a set of template parameters, return them as a set of template
3831 arguments. The template parameters are represented as a TREE_VEC, in
3832 the form documented in cp-tree.h for template arguments. */
3835 template_parms_to_args (tree parms
)
3838 tree args
= NULL_TREE
;
3839 int length
= TMPL_PARMS_DEPTH (parms
);
3842 /* If there is only one level of template parameters, we do not
3843 create a TREE_VEC of TREE_VECs. Instead, we return a single
3844 TREE_VEC containing the arguments. */
3846 args
= make_tree_vec (length
);
3848 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3850 tree a
= copy_node (TREE_VALUE (header
));
3853 TREE_TYPE (a
) = NULL_TREE
;
3854 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3855 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3857 #ifdef ENABLE_CHECKING
3858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3862 TREE_VEC_ELT (args
, --l
) = a
;
3867 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3868 /* This can happen for template parms of a template template
3871 template<template<class T, class U> class TT> struct S;
3873 Consider the level of the parms of TT; T and U both have
3874 level 2; TT has no template parm of level 1. So in this case
3875 the first element of full_template_args is NULL_TREE. If we
3876 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3877 of 2. This will make tsubst wrongly consider that T and U
3878 have level 1. Instead, let's create a dummy vector as the
3879 first element of full_template_args so that TMPL_ARG_DEPTH
3880 returns the correct depth for args. */
3881 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3885 /* Within the declaration of a template, return the currently active
3886 template parameters as an argument TREE_VEC. */
3889 current_template_args (void)
3891 return template_parms_to_args (current_template_parms
);
3894 /* Update the declared TYPE by doing any lookups which were thought to be
3895 dependent, but are not now that we know the SCOPE of the declarator. */
3898 maybe_update_decl_type (tree orig_type
, tree scope
)
3900 tree type
= orig_type
;
3902 if (type
== NULL_TREE
)
3905 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3906 type
= TREE_TYPE (type
);
3908 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3909 && dependent_type_p (type
)
3910 /* Don't bother building up the args in this case. */
3911 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3913 /* tsubst in the args corresponding to the template parameters,
3914 including auto if present. Most things will be unchanged, but
3915 make_typename_type and tsubst_qualified_id will resolve
3916 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3917 tree args
= current_template_args ();
3918 tree auto_node
= type_uses_auto (type
);
3922 tree auto_vec
= make_tree_vec (1);
3923 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3924 args
= add_to_template_args (args
, auto_vec
);
3926 pushed
= push_scope (scope
);
3927 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3932 if (type
== error_mark_node
)
3935 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3937 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3940 type
= TYPE_NAME (type
);
3945 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3946 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3947 a member template. Used by push_template_decl below. */
3950 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3952 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3953 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3954 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3955 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3956 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3961 struct template_parm_data
3963 /* The level of the template parameters we are currently
3967 /* The index of the specialization argument we are currently
3971 /* An array whose size is the number of template parameters. The
3972 elements are nonzero if the parameter has been used in any one
3973 of the arguments processed so far. */
3976 /* An array whose size is the number of template arguments. The
3977 elements are nonzero if the argument makes use of template
3978 parameters of this level. */
3979 int* arg_uses_template_parms
;
3982 /* Subroutine of push_template_decl used to see if each template
3983 parameter in a partial specialization is used in the explicit
3984 argument list. If T is of the LEVEL given in DATA (which is
3985 treated as a template_parm_data*), then DATA->PARMS is marked
3989 mark_template_parm (tree t
, void* data
)
3993 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3995 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3997 level
= TEMPLATE_PARM_LEVEL (t
);
3998 idx
= TEMPLATE_PARM_IDX (t
);
4002 level
= TEMPLATE_TYPE_LEVEL (t
);
4003 idx
= TEMPLATE_TYPE_IDX (t
);
4006 if (level
== tpd
->level
)
4008 tpd
->parms
[idx
] = 1;
4009 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4012 /* Return zero so that for_each_template_parm will continue the
4013 traversal of the tree; we want to mark *every* template parm. */
4017 /* Process the partial specialization DECL. */
4020 process_partial_specialization (tree decl
)
4022 tree type
= TREE_TYPE (decl
);
4023 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4024 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4025 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4026 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4029 int nargs
= TREE_VEC_LENGTH (inner_args
);
4032 bool did_error_intro
= false;
4033 struct template_parm_data tpd
;
4034 struct template_parm_data tpd2
;
4036 gcc_assert (current_template_parms
);
4038 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4039 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4041 /* We check that each of the template parameters given in the
4042 partial specialization is used in the argument list to the
4043 specialization. For example:
4045 template <class T> struct S;
4046 template <class T> struct S<T*>;
4048 The second declaration is OK because `T*' uses the template
4049 parameter T, whereas
4051 template <class T> struct S<int>;
4053 is no good. Even trickier is:
4064 The S2<T> declaration is actually invalid; it is a
4065 full-specialization. Of course,
4068 struct S2<T (*)(U)>;
4070 or some such would have been OK. */
4071 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4072 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4073 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4075 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4076 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4077 for (i
= 0; i
< nargs
; ++i
)
4079 tpd
.current_arg
= i
;
4080 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4081 &mark_template_parm
,
4084 /*include_nondeduced_p=*/false);
4086 for (i
= 0; i
< ntparms
; ++i
)
4087 if (tpd
.parms
[i
] == 0)
4089 /* One of the template parms was not used in the
4091 if (!did_error_intro
)
4093 error ("template parameters not used in partial specialization:");
4094 did_error_intro
= true;
4097 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4100 if (did_error_intro
)
4101 return error_mark_node
;
4103 /* [temp.class.spec]
4105 The argument list of the specialization shall not be identical to
4106 the implicit argument list of the primary template. */
4107 if (comp_template_args
4109 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4111 error ("partial specialization %qT does not specialize any template arguments", type
);
4113 /* A partial specialization that replaces multiple parameters of the
4114 primary template with a pack expansion is less specialized for those
4116 if (nargs
< DECL_NTPARMS (maintmpl
))
4118 error ("partial specialization is not more specialized than the "
4119 "primary template because it replaces multiple parameters "
4120 "with a pack expansion");
4121 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4125 /* [temp.class.spec]
4127 A partially specialized non-type argument expression shall not
4128 involve template parameters of the partial specialization except
4129 when the argument expression is a simple identifier.
4131 The type of a template parameter corresponding to a specialized
4132 non-type argument shall not be dependent on a parameter of the
4135 Also, we verify that pack expansions only occur at the
4136 end of the argument list. */
4137 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4139 for (i
= 0; i
< nargs
; ++i
)
4141 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4142 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4143 tree packed_args
= NULL_TREE
;
4146 if (ARGUMENT_PACK_P (arg
))
4148 /* Extract the arguments from the argument pack. We'll be
4149 iterating over these in the following loop. */
4150 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4151 len
= TREE_VEC_LENGTH (packed_args
);
4154 for (j
= 0; j
< len
; j
++)
4157 /* Get the Jth argument in the parameter pack. */
4158 arg
= TREE_VEC_ELT (packed_args
, j
);
4160 if (PACK_EXPANSION_P (arg
))
4162 /* Pack expansions must come at the end of the
4164 if ((packed_args
&& j
< len
- 1)
4165 || (!packed_args
&& i
< nargs
- 1))
4167 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4168 error ("parameter pack argument %qE must be at the "
4169 "end of the template argument list", arg
);
4171 error ("parameter pack argument %qT must be at the "
4172 "end of the template argument list", arg
);
4176 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4177 /* We only care about the pattern. */
4178 arg
= PACK_EXPANSION_PATTERN (arg
);
4180 if (/* These first two lines are the `non-type' bit. */
4182 && TREE_CODE (arg
) != TEMPLATE_DECL
4183 /* This next line is the `argument expression is not just a
4184 simple identifier' condition and also the `specialized
4185 non-type argument' bit. */
4186 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4188 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4189 || (packed_args
&& uses_template_parms (arg
)))
4190 error ("template argument %qE involves template parameter(s)",
4194 /* Look at the corresponding template parameter,
4195 marking which template parameters its type depends
4197 tree type
= TREE_TYPE (parm
);
4201 /* We haven't yet initialized TPD2. Do so now. */
4202 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4203 /* The number of parameters here is the number in the
4204 main template, which, as checked in the assertion
4206 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4208 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4211 /* Mark the template parameters. But this time, we're
4212 looking for the template parameters of the main
4213 template, not in the specialization. */
4214 tpd2
.current_arg
= i
;
4215 tpd2
.arg_uses_template_parms
[i
] = 0;
4216 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4217 for_each_template_parm (type
,
4218 &mark_template_parm
,
4221 /*include_nondeduced_p=*/false);
4223 if (tpd2
.arg_uses_template_parms
[i
])
4225 /* The type depended on some template parameters.
4226 If they are fully specialized in the
4227 specialization, that's OK. */
4230 for (j
= 0; j
< nargs
; ++j
)
4231 if (tpd2
.parms
[j
] != 0
4232 && tpd
.arg_uses_template_parms
[j
])
4235 error_n (input_location
, count
,
4236 "type %qT of template argument %qE depends "
4237 "on a template parameter",
4238 "type %qT of template argument %qE depends "
4239 "on template parameters",
4248 /* We should only get here once. */
4249 gcc_assert (!COMPLETE_TYPE_P (type
));
4251 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4252 = tree_cons (specargs
, inner_parms
,
4253 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4254 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4256 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4257 inst
= TREE_CHAIN (inst
))
4259 tree inst_type
= TREE_VALUE (inst
);
4260 if (COMPLETE_TYPE_P (inst_type
)
4261 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4263 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4264 if (spec
&& TREE_TYPE (spec
) == type
)
4265 permerror (input_location
,
4266 "partial specialization of %qT after instantiation "
4267 "of %qT", type
, inst_type
);
4274 /* Check that a template declaration's use of default arguments and
4275 parameter packs is not invalid. Here, PARMS are the template
4276 parameters. IS_PRIMARY is true if DECL is the thing declared by
4277 a primary template. IS_PARTIAL is true if DECL is a partial
4280 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4281 declaration (but not a definition); 1 indicates a declaration, 2
4282 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4283 emitted for extraneous default arguments.
4285 Returns TRUE if there were no errors found, FALSE otherwise. */
4288 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4289 bool is_partial
, int is_friend_decl
)
4292 int last_level_to_check
;
4294 bool no_errors
= true;
4298 A default template-argument shall not be specified in a
4299 function template declaration or a function template definition, nor
4300 in the template-parameter-list of the definition of a member of a
4303 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4304 /* You can't have a function template declaration in a local
4305 scope, nor you can you define a member of a class template in a
4309 if (current_class_type
4310 && !TYPE_BEING_DEFINED (current_class_type
)
4311 && DECL_LANG_SPECIFIC (decl
)
4312 && DECL_DECLARES_FUNCTION_P (decl
)
4313 /* If this is either a friend defined in the scope of the class
4314 or a member function. */
4315 && (DECL_FUNCTION_MEMBER_P (decl
)
4316 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4317 : DECL_FRIEND_CONTEXT (decl
)
4318 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4320 /* And, if it was a member function, it really was defined in
4321 the scope of the class. */
4322 && (!DECL_FUNCTION_MEMBER_P (decl
)
4323 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4324 /* We already checked these parameters when the template was
4325 declared, so there's no need to do it again now. This function
4326 was defined in class scope, but we're processing it's body now
4327 that the class is complete. */
4330 /* Core issue 226 (C++0x only): the following only applies to class
4333 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4337 If a template-parameter has a default template-argument, all
4338 subsequent template-parameters shall have a default
4339 template-argument supplied. */
4340 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4342 tree inner_parms
= TREE_VALUE (parm_level
);
4343 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4344 int seen_def_arg_p
= 0;
4347 for (i
= 0; i
< ntparms
; ++i
)
4349 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4351 if (parm
== error_mark_node
)
4354 if (TREE_PURPOSE (parm
))
4356 else if (seen_def_arg_p
4357 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4359 error ("no default argument for %qD", TREE_VALUE (parm
));
4360 /* For better subsequent error-recovery, we indicate that
4361 there should have been a default argument. */
4362 TREE_PURPOSE (parm
) = error_mark_node
;
4365 else if (!is_partial
4367 /* Don't complain about an enclosing partial
4369 && parm_level
== parms
4370 && TREE_CODE (decl
) == TYPE_DECL
4372 && template_parameter_pack_p (TREE_VALUE (parm
)))
4374 /* A primary class template can only have one
4375 parameter pack, at the end of the template
4378 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4379 error ("parameter pack %qE must be at the end of the"
4380 " template parameter list", TREE_VALUE (parm
));
4382 error ("parameter pack %qT must be at the end of the"
4383 " template parameter list",
4384 TREE_TYPE (TREE_VALUE (parm
)));
4386 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4394 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4398 /* For an ordinary class template, default template arguments are
4399 allowed at the innermost level, e.g.:
4400 template <class T = int>
4402 but, in a partial specialization, they're not allowed even
4403 there, as we have in [temp.class.spec]:
4405 The template parameter list of a specialization shall not
4406 contain default template argument values.
4408 So, for a partial specialization, or for a function template
4409 (in C++98/C++03), we look at all of them. */
4412 /* But, for a primary class template that is not a partial
4413 specialization we look at all template parameters except the
4415 parms
= TREE_CHAIN (parms
);
4417 /* Figure out what error message to issue. */
4418 if (is_friend_decl
== 2)
4419 msg
= G_("default template arguments may not be used in function template "
4420 "friend re-declaration");
4421 else if (is_friend_decl
)
4422 msg
= G_("default template arguments may not be used in function template "
4423 "friend declarations");
4424 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4425 msg
= G_("default template arguments may not be used in function templates "
4426 "without -std=c++11 or -std=gnu++11");
4427 else if (is_partial
)
4428 msg
= G_("default template arguments may not be used in "
4429 "partial specializations");
4431 msg
= G_("default argument for template parameter for class enclosing %qD");
4433 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4434 /* If we're inside a class definition, there's no need to
4435 examine the parameters to the class itself. On the one
4436 hand, they will be checked when the class is defined, and,
4437 on the other, default arguments are valid in things like:
4438 template <class T = double>
4439 struct S { template <class U> void f(U); };
4440 Here the default argument for `S' has no bearing on the
4441 declaration of `f'. */
4442 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4444 /* Check everything. */
4445 last_level_to_check
= 0;
4447 for (parm_level
= parms
;
4448 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4449 parm_level
= TREE_CHAIN (parm_level
))
4451 tree inner_parms
= TREE_VALUE (parm_level
);
4455 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4456 for (i
= 0; i
< ntparms
; ++i
)
4458 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4461 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4466 if (is_friend_decl
== 2)
4473 /* Clear out the default argument so that we are not
4475 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4479 /* At this point, if we're still interested in issuing messages,
4480 they must apply to classes surrounding the object declared. */
4482 msg
= G_("default argument for template parameter for class "
4489 /* Worker for push_template_decl_real, called via
4490 for_each_template_parm. DATA is really an int, indicating the
4491 level of the parameters we are interested in. If T is a template
4492 parameter of that level, return nonzero. */
4495 template_parm_this_level_p (tree t
, void* data
)
4497 int this_level
= *(int *)data
;
4500 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4501 level
= TEMPLATE_PARM_LEVEL (t
);
4503 level
= TEMPLATE_TYPE_LEVEL (t
);
4504 return level
== this_level
;
4507 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4508 parameters given by current_template_args, or reuses a
4509 previously existing one, if appropriate. Returns the DECL, or an
4510 equivalent one, if it is replaced via a call to duplicate_decls.
4512 If IS_FRIEND is true, DECL is a friend declaration. */
4515 push_template_decl_real (tree decl
, bool is_friend
)
4523 int new_template_p
= 0;
4524 /* True if the template is a member template, in the sense of
4526 bool member_template_p
= false;
4528 if (decl
== error_mark_node
|| !current_template_parms
)
4529 return error_mark_node
;
4531 /* See if this is a partial specialization. */
4532 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4533 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4534 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4536 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4540 /* For a friend, we want the context of the friend function, not
4541 the type of which it is a friend. */
4542 ctx
= CP_DECL_CONTEXT (decl
);
4543 else if (CP_DECL_CONTEXT (decl
)
4544 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4545 /* In the case of a virtual function, we want the class in which
4547 ctx
= CP_DECL_CONTEXT (decl
);
4549 /* Otherwise, if we're currently defining some class, the DECL
4550 is assumed to be a member of the class. */
4551 ctx
= current_scope ();
4553 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4556 if (!DECL_CONTEXT (decl
))
4557 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4559 /* See if this is a primary template. */
4560 if (is_friend
&& ctx
)
4561 /* A friend template that specifies a class context, i.e.
4562 template <typename T> friend void A<T>::f();
4566 is_primary
= template_parm_scope_p ();
4570 if (DECL_CLASS_SCOPE_P (decl
))
4571 member_template_p
= true;
4572 if (TREE_CODE (decl
) == TYPE_DECL
4573 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4575 error ("template class without a name");
4576 return error_mark_node
;
4578 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4580 if (DECL_DESTRUCTOR_P (decl
))
4584 A destructor shall not be a member template. */
4585 error ("destructor %qD declared as member template", decl
);
4586 return error_mark_node
;
4588 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4589 && (!prototype_p (TREE_TYPE (decl
))
4590 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4591 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4592 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4593 == void_list_node
)))
4595 /* [basic.stc.dynamic.allocation]
4597 An allocation function can be a function
4598 template. ... Template allocation functions shall
4599 have two or more parameters. */
4600 error ("invalid template declaration of %qD", decl
);
4601 return error_mark_node
;
4604 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4605 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4607 else if (TREE_CODE (decl
) == TYPE_DECL
4608 && TYPE_DECL_ALIAS_P (decl
))
4609 /* alias-declaration */
4610 gcc_assert (!DECL_ARTIFICIAL (decl
));
4613 error ("template declaration of %q#D", decl
);
4614 return error_mark_node
;
4618 /* Check to see that the rules regarding the use of default
4619 arguments are not being violated. */
4620 check_default_tmpl_args (decl
, current_template_parms
,
4621 is_primary
, is_partial
, /*is_friend_decl=*/0);
4623 /* Ensure that there are no parameter packs in the type of this
4624 declaration that have not been expanded. */
4625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4627 /* Check each of the arguments individually to see if there are
4628 any bare parameter packs. */
4629 tree type
= TREE_TYPE (decl
);
4630 tree arg
= DECL_ARGUMENTS (decl
);
4631 tree argtype
= TYPE_ARG_TYPES (type
);
4633 while (arg
&& argtype
)
4635 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4636 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4638 /* This is a PARM_DECL that contains unexpanded parameter
4639 packs. We have already complained about this in the
4640 check_for_bare_parameter_packs call, so just replace
4641 these types with ERROR_MARK_NODE. */
4642 TREE_TYPE (arg
) = error_mark_node
;
4643 TREE_VALUE (argtype
) = error_mark_node
;
4646 arg
= DECL_CHAIN (arg
);
4647 argtype
= TREE_CHAIN (argtype
);
4650 /* Check for bare parameter packs in the return type and the
4651 exception specifiers. */
4652 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4653 /* Errors were already issued, set return type to int
4654 as the frontend doesn't expect error_mark_node as
4656 TREE_TYPE (type
) = integer_type_node
;
4657 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4658 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4660 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl
))
4662 ? DECL_ORIGINAL_TYPE (decl
)
4663 : TREE_TYPE (decl
)))
4665 TREE_TYPE (decl
) = error_mark_node
;
4666 return error_mark_node
;
4670 return process_partial_specialization (decl
);
4672 args
= current_template_args ();
4675 || TREE_CODE (ctx
) == FUNCTION_DECL
4676 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4677 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4679 if (DECL_LANG_SPECIFIC (decl
)
4680 && DECL_TEMPLATE_INFO (decl
)
4681 && DECL_TI_TEMPLATE (decl
))
4682 tmpl
= DECL_TI_TEMPLATE (decl
);
4683 /* If DECL is a TYPE_DECL for a class-template, then there won't
4684 be DECL_LANG_SPECIFIC. The information equivalent to
4685 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4686 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4687 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4688 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4690 /* Since a template declaration already existed for this
4691 class-type, we must be redeclaring it here. Make sure
4692 that the redeclaration is valid. */
4693 redeclare_class_template (TREE_TYPE (decl
),
4694 current_template_parms
);
4695 /* We don't need to create a new TEMPLATE_DECL; just use the
4696 one we already had. */
4697 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4701 tmpl
= build_template_decl (decl
, current_template_parms
,
4705 if (DECL_LANG_SPECIFIC (decl
)
4706 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4708 /* A specialization of a member template of a template
4710 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4711 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4712 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4718 tree a
, t
, current
, parms
;
4720 tree tinfo
= get_template_info (decl
);
4724 error ("template definition of non-template %q#D", decl
);
4725 return error_mark_node
;
4728 tmpl
= TI_TEMPLATE (tinfo
);
4730 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4731 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4732 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4733 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4737 /* The declaration is a specialization of a member
4738 template, declared outside the class. Therefore, the
4739 innermost template arguments will be NULL, so we
4740 replace them with the arguments determined by the
4741 earlier call to check_explicit_specialization. */
4742 args
= DECL_TI_ARGS (decl
);
4745 = build_template_decl (decl
, current_template_parms
,
4747 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4748 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4749 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4750 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4751 DECL_TEMPLATE_INFO (new_tmpl
)
4752 = build_template_info (tmpl
, args
);
4754 register_specialization (new_tmpl
,
4755 most_general_template (tmpl
),
4761 /* Make sure the template headers we got make sense. */
4763 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4764 i
= TMPL_PARMS_DEPTH (parms
);
4765 if (TMPL_ARGS_DEPTH (args
) != i
)
4767 error ("expected %d levels of template parms for %q#D, got %d",
4768 i
, decl
, TMPL_ARGS_DEPTH (args
));
4771 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4773 a
= TMPL_ARGS_LEVEL (args
, i
);
4774 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4776 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4778 if (current
== decl
)
4779 error ("got %d template parameters for %q#D",
4780 TREE_VEC_LENGTH (a
), decl
);
4782 error ("got %d template parameters for %q#T",
4783 TREE_VEC_LENGTH (a
), current
);
4784 error (" but %d required", TREE_VEC_LENGTH (t
));
4785 /* Avoid crash in import_export_decl. */
4786 DECL_INTERFACE_KNOWN (decl
) = 1;
4787 return error_mark_node
;
4790 if (current
== decl
)
4792 else if (current
== NULL_TREE
)
4793 /* Can happen in erroneous input. */
4796 current
= (TYPE_P (current
)
4797 ? TYPE_CONTEXT (current
)
4798 : DECL_CONTEXT (current
));
4801 /* Check that the parms are used in the appropriate qualifying scopes
4802 in the declarator. */
4803 if (!comp_template_args
4805 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4808 template arguments to %qD do not match original template %qD",
4809 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4810 if (!uses_template_parms (TI_ARGS (tinfo
)))
4811 inform (input_location
, "use template<> for an explicit specialization");
4812 /* Avoid crash in import_export_decl. */
4813 DECL_INTERFACE_KNOWN (decl
) = 1;
4814 return error_mark_node
;
4818 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4819 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4821 /* Push template declarations for global functions and types. Note
4822 that we do not try to push a global template friend declared in a
4823 template class; such a thing may well depend on the template
4824 parameters of the class. */
4825 if (new_template_p
&& !ctx
4826 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4828 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4829 if (tmpl
== error_mark_node
)
4830 return error_mark_node
;
4832 /* Hide template friend classes that haven't been declared yet. */
4833 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4835 DECL_ANTICIPATED (tmpl
) = 1;
4836 DECL_FRIEND_P (tmpl
) = 1;
4842 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4845 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4846 if (DECL_CONV_FN_P (tmpl
))
4848 int depth
= TMPL_PARMS_DEPTH (parms
);
4850 /* It is a conversion operator. See if the type converted to
4851 depends on innermost template operands. */
4853 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4855 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4858 /* Give template template parms a DECL_CONTEXT of the template
4859 for which they are a parameter. */
4860 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4861 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4863 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4864 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4865 DECL_CONTEXT (parm
) = tmpl
;
4869 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4870 back to its most general template. If TMPL is a specialization,
4871 ARGS may only have the innermost set of arguments. Add the missing
4872 argument levels if necessary. */
4873 if (DECL_TEMPLATE_INFO (tmpl
))
4874 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4876 info
= build_template_info (tmpl
, args
);
4878 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4879 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4882 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4883 retrofit_lang_decl (decl
);
4884 if (DECL_LANG_SPECIFIC (decl
))
4885 DECL_TEMPLATE_INFO (decl
) = info
;
4888 return DECL_TEMPLATE_RESULT (tmpl
);
4892 push_template_decl (tree decl
)
4894 return push_template_decl_real (decl
, false);
4897 /* FN is an inheriting constructor that inherits from the constructor
4898 template INHERITED; turn FN into a constructor template with a matching
4902 add_inherited_template_parms (tree fn
, tree inherited
)
4905 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4906 inner_parms
= copy_node (inner_parms
);
4908 = tree_cons (size_int (processing_template_decl
+ 1),
4909 inner_parms
, current_template_parms
);
4910 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4911 tree args
= template_parms_to_args (parms
);
4912 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4913 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4914 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4915 DECL_ARTIFICIAL (tmpl
) = true;
4916 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4920 /* Called when a class template TYPE is redeclared with the indicated
4921 template PARMS, e.g.:
4923 template <class T> struct S;
4924 template <class T> struct S {}; */
4927 redeclare_class_template (tree type
, tree parms
)
4933 if (!TYPE_TEMPLATE_INFO (type
))
4935 error ("%qT is not a template type", type
);
4939 tmpl
= TYPE_TI_TEMPLATE (type
);
4940 if (!PRIMARY_TEMPLATE_P (tmpl
))
4941 /* The type is nested in some template class. Nothing to worry
4942 about here; there are no new template parameters for the nested
4948 error ("template specifiers not specified in declaration of %qD",
4953 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4954 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4956 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4958 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4959 "redeclared with %d template parameter",
4960 "redeclared with %d template parameters",
4961 TREE_VEC_LENGTH (parms
));
4962 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4963 "previous declaration %q+D used %d template parameter",
4964 "previous declaration %q+D used %d template parameters",
4965 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4969 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4976 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4977 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4980 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4981 if (tmpl_parm
== error_mark_node
)
4984 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4985 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4986 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4988 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4990 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4991 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4992 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4993 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4994 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4995 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4996 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4997 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4998 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5000 error ("template parameter %q+#D", tmpl_parm
);
5001 error ("redeclared here as %q#D", parm
);
5005 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5007 /* We have in [temp.param]:
5009 A template-parameter may not be given default arguments
5010 by two different declarations in the same scope. */
5011 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5012 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5013 "original definition appeared here");
5017 if (parm_default
!= NULL_TREE
)
5018 /* Update the previous template parameters (which are the ones
5019 that will really count) with the new default value. */
5020 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5021 else if (tmpl_default
!= NULL_TREE
)
5022 /* Update the new parameters, too; they'll be used as the
5023 parameters for any members. */
5024 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5030 /* Simplify EXPR if it is a non-dependent expression. Returns the
5031 (possibly simplified) expression. */
5034 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5036 if (expr
== NULL_TREE
)
5039 /* If we're in a template, but EXPR isn't value dependent, simplify
5040 it. We're supposed to treat:
5042 template <typename T> void f(T[1 + 1]);
5043 template <typename T> void f(T[2]);
5045 as two declarations of the same function, for example. */
5046 if (processing_template_decl
5047 && !type_dependent_expression_p (expr
)
5048 && potential_constant_expression (expr
)
5049 && !value_dependent_expression_p (expr
))
5051 HOST_WIDE_INT saved_processing_template_decl
;
5053 saved_processing_template_decl
= processing_template_decl
;
5054 processing_template_decl
= 0;
5055 expr
= tsubst_copy_and_build (expr
,
5058 /*in_decl=*/NULL_TREE
,
5059 /*function_p=*/false,
5060 /*integral_constant_expression_p=*/true);
5061 processing_template_decl
= saved_processing_template_decl
;
5067 fold_non_dependent_expr (tree expr
)
5069 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5072 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5073 template declaration, or a TYPE_DECL for an alias declaration. */
5076 alias_type_or_template_p (tree t
)
5080 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5083 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5084 || DECL_ALIAS_TEMPLATE_P (t
));
5087 /* Return TRUE iff is a specialization of an alias template. */
5090 alias_template_specialization_p (const_tree t
)
5096 && TYPE_TEMPLATE_INFO (t
)
5097 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5098 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5101 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5102 must be a function or a pointer-to-function type, as specified
5103 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5104 and check that the resulting function has external linkage. */
5107 convert_nontype_argument_function (tree type
, tree expr
)
5111 linkage_kind linkage
;
5113 fn
= instantiate_type (type
, fns
, tf_none
);
5114 if (fn
== error_mark_node
)
5115 return error_mark_node
;
5118 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5119 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5120 if (BASELINK_P (fn_no_ptr
))
5121 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5123 /* [temp.arg.nontype]/1
5125 A template-argument for a non-type, non-template template-parameter
5128 -- the address of an object or function with external [C++11: or
5129 internal] linkage. */
5131 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5133 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5134 if (TREE_CODE (type
) == POINTER_TYPE
)
5135 error ("it must be the address of a function with external linkage");
5137 error ("it must be the name of a function with external linkage");
5141 linkage
= decl_linkage (fn_no_ptr
);
5142 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5144 if (cxx_dialect
>= cxx0x
)
5145 error ("%qE is not a valid template argument for type %qT "
5146 "because %qD has no linkage",
5147 expr
, type
, fn_no_ptr
);
5149 error ("%qE is not a valid template argument for type %qT "
5150 "because %qD does not have external linkage",
5151 expr
, type
, fn_no_ptr
);
5158 /* Subroutine of convert_nontype_argument.
5159 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5160 Emit an error otherwise. */
5163 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5164 tsubst_flags_t complain
)
5167 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5169 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5171 if (complain
& tf_error
)
5173 error ("%qE is not a valid template argument for type %qT",
5175 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5180 /* Returns TRUE iff the address of OP is value-dependent.
5182 14.6.2.4 [temp.dep.temp]:
5183 A non-integral non-type template-argument is dependent if its type is
5184 dependent or it has either of the following forms
5187 and contains a nested-name-specifier which specifies a class-name that
5188 names a dependent type.
5190 We generalize this to just say that the address of a member of a
5191 dependent class is value-dependent; the above doesn't cover the
5192 address of a static data member named with an unqualified-id. */
5195 has_value_dependent_address (tree op
)
5197 /* We could use get_inner_reference here, but there's no need;
5198 this is only relevant for template non-type arguments, which
5199 can only be expressed as &id-expression. */
5202 tree ctx
= CP_DECL_CONTEXT (op
);
5203 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5210 /* The next set of functions are used for providing helpful explanatory
5211 diagnostics for failed overload resolution. Their messages should be
5212 indented by two spaces for consistency with the messages in
5216 unify_success (bool /*explain_p*/)
5222 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5225 inform (input_location
,
5226 " couldn't deduce template parameter %qD", parm
);
5231 unify_invalid (bool /*explain_p*/)
5237 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5240 inform (input_location
,
5241 " types %qT and %qT have incompatible cv-qualifiers",
5247 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5250 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5255 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5258 inform (input_location
,
5259 " template parameter %qD is not a parameter pack, but "
5266 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5269 inform (input_location
,
5270 " template argument %qE does not match "
5271 "pointer-to-member constant %qE",
5277 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5280 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5285 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5288 inform (input_location
,
5289 " inconsistent parameter pack deduction with %qT and %qT",
5295 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5300 inform (input_location
,
5301 " deduced conflicting types for parameter %qT (%qT and %qT)",
5302 parm
, first
, second
);
5304 inform (input_location
,
5305 " deduced conflicting values for non-type parameter "
5306 "%qE (%qE and %qE)", parm
, first
, second
);
5312 unify_vla_arg (bool explain_p
, tree arg
)
5315 inform (input_location
,
5316 " variable-sized array type %qT is not "
5317 "a valid template argument",
5323 unify_method_type_error (bool explain_p
, tree arg
)
5326 inform (input_location
,
5327 " member function type %qT is not a valid template argument",
5333 unify_arity (bool explain_p
, int have
, int wanted
)
5336 inform_n (input_location
, wanted
,
5337 " candidate expects %d argument, %d provided",
5338 " candidate expects %d arguments, %d provided",
5344 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5346 return unify_arity (explain_p
, have
, wanted
);
5350 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5352 return unify_arity (explain_p
, have
, wanted
);
5356 unify_arg_conversion (bool explain_p
, tree to_type
,
5357 tree from_type
, tree arg
)
5360 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5361 arg
, from_type
, to_type
);
5366 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5367 tree parm
, tree arg
)
5372 case tbr_ambiguous_baseclass
:
5373 inform (input_location
, " %qT is an ambiguous base class of %qT",
5377 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5384 unify_inconsistent_template_template_parameters (bool explain_p
)
5387 inform (input_location
,
5388 " template parameters of a template template argument are "
5389 "inconsistent with other deduced template arguments");
5394 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5397 inform (input_location
,
5398 " can't deduce a template for %qT from non-template type %qT",
5404 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5407 inform (input_location
,
5408 " template argument %qE does not match %qD", arg
, parm
);
5413 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5416 inform (input_location
,
5417 " could not resolve address from overloaded function %qE",
5422 /* Attempt to convert the non-type template parameter EXPR to the
5423 indicated TYPE. If the conversion is successful, return the
5424 converted value. If the conversion is unsuccessful, return
5425 NULL_TREE if we issued an error message, or error_mark_node if we
5426 did not. We issue error messages for out-and-out bad template
5427 parameters, but not simply because the conversion failed, since we
5428 might be just trying to do argument deduction. Both TYPE and EXPR
5429 must be non-dependent.
5431 The conversion follows the special rules described in
5432 [temp.arg.nontype], and it is much more strict than an implicit
5435 This function is called twice for each template argument (see
5436 lookup_template_class for a more accurate description of this
5437 problem). This means that we need to handle expressions which
5438 are not valid in a C++ source, but can be created from the
5439 first call (for instance, casts to perform conversions). These
5440 hacks can go away after we fix the double coercion problem. */
5443 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5447 /* Detect immediately string literals as invalid non-type argument.
5448 This special-case is not needed for correctness (we would easily
5449 catch this later), but only to provide better diagnostic for this
5450 common user mistake. As suggested by DR 100, we do not mention
5451 linkage issues in the diagnostic as this is not the point. */
5452 /* FIXME we're making this OK. */
5453 if (TREE_CODE (expr
) == STRING_CST
)
5455 if (complain
& tf_error
)
5456 error ("%qE is not a valid template argument for type %qT "
5457 "because string literals can never be used in this context",
5462 /* Add the ADDR_EXPR now for the benefit of
5463 value_dependent_expression_p. */
5464 if (TYPE_PTROBV_P (type
)
5465 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5467 expr
= decay_conversion (expr
, complain
);
5468 if (expr
== error_mark_node
)
5469 return error_mark_node
;
5472 /* If we are in a template, EXPR may be non-dependent, but still
5473 have a syntactic, rather than semantic, form. For example, EXPR
5474 might be a SCOPE_REF, rather than the VAR_DECL to which the
5475 SCOPE_REF refers. Preserving the qualifying scope is necessary
5476 so that access checking can be performed when the template is
5477 instantiated -- but here we need the resolved form so that we can
5478 convert the argument. */
5479 if (TYPE_REF_OBJ_P (type
)
5480 && has_value_dependent_address (expr
))
5481 /* If we want the address and it's value-dependent, don't fold. */;
5482 else if (!type_unknown_p (expr
))
5483 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5484 if (error_operand_p (expr
))
5485 return error_mark_node
;
5486 expr_type
= TREE_TYPE (expr
);
5487 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5488 expr
= mark_lvalue_use (expr
);
5490 expr
= mark_rvalue_use (expr
);
5492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5493 to a non-type argument of "nullptr". */
5494 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5495 expr
= convert (type
, expr
);
5497 /* In C++11, integral or enumeration non-type template arguments can be
5498 arbitrary constant expressions. Pointer and pointer to
5499 member arguments can be general constant expressions that evaluate
5500 to a null value, but otherwise still need to be of a specific form. */
5501 if (cxx_dialect
>= cxx0x
)
5503 if (TREE_CODE (expr
) == PTRMEM_CST
)
5504 /* A PTRMEM_CST is already constant, and a valid template
5505 argument for a parameter of pointer to member type, we just want
5506 to leave it in that form rather than lower it to a
5508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5509 expr
= maybe_constant_value (expr
);
5510 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5512 tree folded
= maybe_constant_value (expr
);
5513 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5514 : null_member_pointer_value_p (folded
))
5519 /* HACK: Due to double coercion, we can get a
5520 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5521 which is the tree that we built on the first call (see
5522 below when coercing to reference to object or to reference to
5523 function). We just strip everything and get to the arg.
5524 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5526 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5528 tree probe_type
, probe
= expr
;
5529 if (REFERENCE_REF_P (probe
))
5530 probe
= TREE_OPERAND (probe
, 0);
5531 probe_type
= TREE_TYPE (probe
);
5532 if (TREE_CODE (probe
) == NOP_EXPR
)
5534 /* ??? Maybe we could use convert_from_reference here, but we
5535 would need to relax its constraints because the NOP_EXPR
5536 could actually change the type to something more cv-qualified,
5537 and this is not folded by convert_from_reference. */
5538 tree addr
= TREE_OPERAND (probe
, 0);
5539 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5540 && TREE_CODE (addr
) == ADDR_EXPR
5541 && TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
5542 && (same_type_ignoring_top_level_qualifiers_p
5543 (TREE_TYPE (probe_type
),
5544 TREE_TYPE (TREE_TYPE (addr
)))))
5546 expr
= TREE_OPERAND (addr
, 0);
5547 expr_type
= TREE_TYPE (expr
);
5552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5553 parameter is a pointer to object, through decay and
5554 qualification conversion. Let's strip everything. */
5555 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5558 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5559 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5560 /* Skip the ADDR_EXPR only if it is part of the decay for
5561 an array. Otherwise, it is part of the original argument
5562 in the source code. */
5563 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5564 expr
= TREE_OPERAND (expr
, 0);
5565 expr_type
= TREE_TYPE (expr
);
5568 /* [temp.arg.nontype]/5, bullet 1
5570 For a non-type template-parameter of integral or enumeration type,
5571 integral promotions (_conv.prom_) and integral conversions
5572 (_conv.integral_) are applied. */
5573 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5575 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5576 t
= maybe_constant_value (t
);
5577 if (t
!= error_mark_node
)
5580 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5581 return error_mark_node
;
5583 /* Notice that there are constant expressions like '4 % 0' which
5584 do not fold into integer constants. */
5585 if (TREE_CODE (expr
) != INTEGER_CST
)
5587 if (complain
& tf_error
)
5589 int errs
= errorcount
, warns
= warningcount
;
5590 if (processing_template_decl
5591 && !require_potential_constant_expression (expr
))
5593 expr
= cxx_constant_value (expr
);
5594 if (errorcount
> errs
|| warningcount
> warns
)
5595 inform (EXPR_LOC_OR_HERE (expr
),
5596 "in template argument for type %qT ", type
);
5597 if (expr
== error_mark_node
)
5599 /* else cxx_constant_value complained but gave us
5600 a real constant, so go ahead. */
5601 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5607 /* [temp.arg.nontype]/5, bullet 2
5609 For a non-type template-parameter of type pointer to object,
5610 qualification conversions (_conv.qual_) and the array-to-pointer
5611 conversion (_conv.array_) are applied. */
5612 else if (TYPE_PTROBV_P (type
))
5614 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5616 A template-argument for a non-type, non-template template-parameter
5617 shall be one of: [...]
5619 -- the name of a non-type template-parameter;
5620 -- the address of an object or function with external linkage, [...]
5621 expressed as "& id-expression" where the & is optional if the name
5622 refers to a function or array, or if the corresponding
5623 template-parameter is a reference.
5625 Here, we do not care about functions, as they are invalid anyway
5626 for a parameter of type pointer-to-object. */
5628 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5629 /* Non-type template parameters are OK. */
5631 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5632 /* Null pointer values are OK in C++11. */;
5633 else if (TREE_CODE (expr
) != ADDR_EXPR
5634 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5636 if (TREE_CODE (expr
) == VAR_DECL
)
5638 error ("%qD is not a valid template argument "
5639 "because %qD is a variable, not the address of "
5644 /* Other values, like integer constants, might be valid
5645 non-type arguments of some other type. */
5646 return error_mark_node
;
5652 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5653 ? TREE_OPERAND (expr
, 0) : expr
);
5654 if (TREE_CODE (decl
) != VAR_DECL
)
5656 error ("%qE is not a valid template argument of type %qT "
5657 "because %qE is not a variable",
5661 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5663 error ("%qE is not a valid template argument of type %qT "
5664 "because %qD does not have external linkage",
5668 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5670 error ("%qE is not a valid template argument of type %qT "
5671 "because %qD has no linkage",
5677 expr
= decay_conversion (expr
, complain
);
5678 if (expr
== error_mark_node
)
5679 return error_mark_node
;
5681 expr
= perform_qualification_conversions (type
, expr
);
5682 if (expr
== error_mark_node
)
5683 return error_mark_node
;
5685 /* [temp.arg.nontype]/5, bullet 3
5687 For a non-type template-parameter of type reference to object, no
5688 conversions apply. The type referred to by the reference may be more
5689 cv-qualified than the (otherwise identical) type of the
5690 template-argument. The template-parameter is bound directly to the
5691 template-argument, which must be an lvalue. */
5692 else if (TYPE_REF_OBJ_P (type
))
5694 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5696 return error_mark_node
;
5698 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5700 error ("%qE is not a valid template argument for type %qT "
5701 "because of conflicts in cv-qualification", expr
, type
);
5705 if (!real_lvalue_p (expr
))
5707 error ("%qE is not a valid template argument for type %qT "
5708 "because it is not an lvalue", expr
, type
);
5712 /* [temp.arg.nontype]/1
5714 A template-argument for a non-type, non-template template-parameter
5715 shall be one of: [...]
5717 -- the address of an object or function with external linkage. */
5718 if (TREE_CODE (expr
) == INDIRECT_REF
5719 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5721 expr
= TREE_OPERAND (expr
, 0);
5724 error ("%q#D is not a valid template argument for type %qT "
5725 "because a reference variable does not have a constant "
5726 "address", expr
, type
);
5733 error ("%qE is not a valid template argument for type %qT "
5734 "because it is not an object with external linkage",
5739 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5741 error ("%qE is not a valid template argument for type %qT "
5742 "because object %qD has not external linkage",
5747 expr
= build_nop (type
, build_address (expr
));
5749 /* [temp.arg.nontype]/5, bullet 4
5751 For a non-type template-parameter of type pointer to function, only
5752 the function-to-pointer conversion (_conv.func_) is applied. If the
5753 template-argument represents a set of overloaded functions (or a
5754 pointer to such), the matching function is selected from the set
5756 else if (TYPE_PTRFN_P (type
))
5758 /* If the argument is a template-id, we might not have enough
5759 context information to decay the pointer. */
5760 if (!type_unknown_p (expr_type
))
5762 expr
= decay_conversion (expr
, complain
);
5763 if (expr
== error_mark_node
)
5764 return error_mark_node
;
5767 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5768 /* Null pointer values are OK in C++11. */
5769 return perform_qualification_conversions (type
, expr
);
5771 expr
= convert_nontype_argument_function (type
, expr
);
5772 if (!expr
|| expr
== error_mark_node
)
5775 /* [temp.arg.nontype]/5, bullet 5
5777 For a non-type template-parameter of type reference to function, no
5778 conversions apply. If the template-argument represents a set of
5779 overloaded functions, the matching function is selected from the set
5781 else if (TYPE_REFFN_P (type
))
5783 if (TREE_CODE (expr
) == ADDR_EXPR
)
5785 error ("%qE is not a valid template argument for type %qT "
5786 "because it is a pointer", expr
, type
);
5787 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5791 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5792 if (!expr
|| expr
== error_mark_node
)
5795 expr
= build_nop (type
, build_address (expr
));
5797 /* [temp.arg.nontype]/5, bullet 6
5799 For a non-type template-parameter of type pointer to member function,
5800 no conversions apply. If the template-argument represents a set of
5801 overloaded member functions, the matching member function is selected
5802 from the set (_over.over_). */
5803 else if (TYPE_PTRMEMFUNC_P (type
))
5805 expr
= instantiate_type (type
, expr
, tf_none
);
5806 if (expr
== error_mark_node
)
5807 return error_mark_node
;
5809 /* [temp.arg.nontype] bullet 1 says the pointer to member
5810 expression must be a pointer-to-member constant. */
5811 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5812 return error_mark_node
;
5814 /* There is no way to disable standard conversions in
5815 resolve_address_of_overloaded_function (called by
5816 instantiate_type). It is possible that the call succeeded by
5817 converting &B::I to &D::I (where B is a base of D), so we need
5818 to reject this conversion here.
5820 Actually, even if there was a way to disable standard conversions,
5821 it would still be better to reject them here so that we can
5822 provide a superior diagnostic. */
5823 if (!same_type_p (TREE_TYPE (expr
), type
))
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because it is of type %qT", expr
, type
,
5828 /* If we are just one standard conversion off, explain. */
5829 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5830 inform (input_location
,
5831 "standard conversions are not allowed in this context");
5835 /* [temp.arg.nontype]/5, bullet 7
5837 For a non-type template-parameter of type pointer to data member,
5838 qualification conversions (_conv.qual_) are applied. */
5839 else if (TYPE_PTRDATAMEM_P (type
))
5841 /* [temp.arg.nontype] bullet 1 says the pointer to member
5842 expression must be a pointer-to-member constant. */
5843 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5844 return error_mark_node
;
5846 expr
= perform_qualification_conversions (type
, expr
);
5847 if (expr
== error_mark_node
)
5850 else if (NULLPTR_TYPE_P (type
))
5852 if (expr
!= nullptr_node
)
5854 error ("%qE is not a valid template argument for type %qT "
5855 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5860 /* A template non-type parameter must be one of the above. */
5864 /* Sanity check: did we actually convert the argument to the
5866 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5867 (type
, TREE_TYPE (expr
)));
5871 /* Subroutine of coerce_template_template_parms, which returns 1 if
5872 PARM_PARM and ARG_PARM match using the rule for the template
5873 parameters of template template parameters. Both PARM and ARG are
5874 template parameters; the rest of the arguments are the same as for
5875 coerce_template_template_parms.
5878 coerce_template_template_parm (tree parm
,
5880 tsubst_flags_t complain
,
5884 if (arg
== NULL_TREE
|| arg
== error_mark_node
5885 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5888 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5891 switch (TREE_CODE (parm
))
5894 /* We encounter instantiations of templates like
5895 template <template <template <class> class> class TT>
5898 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5899 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5901 if (!coerce_template_template_parms
5902 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5908 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5909 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5910 /* Argument is a parameter pack but parameter is not. */
5915 /* The tsubst call is used to handle cases such as
5917 template <int> class C {};
5918 template <class T, template <T> class TT> class D {};
5921 i.e. the parameter list of TT depends on earlier parameters. */
5922 if (!uses_template_parms (TREE_TYPE (arg
))
5924 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5928 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5929 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5930 /* Argument is a parameter pack but parameter is not. */
5943 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5944 template template parameters. Both PARM_PARMS and ARG_PARMS are
5945 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5948 Consider the example:
5949 template <class T> class A;
5950 template<template <class U> class TT> class B;
5952 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5953 the parameters to A, and OUTER_ARGS contains A. */
5956 coerce_template_template_parms (tree parm_parms
,
5958 tsubst_flags_t complain
,
5962 int nparms
, nargs
, i
;
5966 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5967 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5969 nparms
= TREE_VEC_LENGTH (parm_parms
);
5970 nargs
= TREE_VEC_LENGTH (arg_parms
);
5972 /* Determine whether we have a parameter pack at the end of the
5973 template template parameter's template parameter list. */
5974 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5976 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5978 if (parm
== error_mark_node
)
5981 switch (TREE_CODE (parm
))
5985 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5990 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6000 && !(variadic_p
&& nargs
>= nparms
- 1))
6003 /* Check all of the template parameters except the parameter pack at
6004 the end (if any). */
6005 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6007 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6008 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6011 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6012 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6014 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6022 /* Check each of the template parameters in the template
6023 argument against the template parameter pack at the end of
6024 the template template parameter. */
6025 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6028 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6030 for (; i
< nargs
; ++i
)
6032 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6035 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6037 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6046 /* Verifies that the deduced template arguments (in TARGS) for the
6047 template template parameters (in TPARMS) represent valid bindings,
6048 by comparing the template parameter list of each template argument
6049 to the template parameter list of its corresponding template
6050 template parameter, in accordance with DR150. This
6051 routine can only be called after all template arguments have been
6052 deduced. It will return TRUE if all of the template template
6053 parameter bindings are okay, FALSE otherwise. */
6055 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6057 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6060 /* We're dealing with template parms in this process. */
6061 ++processing_template_decl
;
6063 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6065 for (i
= 0; i
< ntparms
; ++i
)
6067 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6068 tree targ
= TREE_VEC_ELT (targs
, i
);
6070 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6072 tree packed_args
= NULL_TREE
;
6075 if (ARGUMENT_PACK_P (targ
))
6077 /* Look inside the argument pack. */
6078 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6079 len
= TREE_VEC_LENGTH (packed_args
);
6082 for (idx
= 0; idx
< len
; ++idx
)
6084 tree targ_parms
= NULL_TREE
;
6087 /* Extract the next argument from the argument
6089 targ
= TREE_VEC_ELT (packed_args
, idx
);
6091 if (PACK_EXPANSION_P (targ
))
6092 /* Look at the pattern of the pack expansion. */
6093 targ
= PACK_EXPANSION_PATTERN (targ
);
6095 /* Extract the template parameters from the template
6097 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6098 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6099 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6100 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6102 /* Verify that we can coerce the template template
6103 parameters from the template argument to the template
6104 parameter. This requires an exact match. */
6106 && !coerce_template_template_parms
6107 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6122 --processing_template_decl
;
6126 /* Since type attributes aren't mangled, we need to strip them from
6127 template type arguments. */
6130 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6133 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6135 mv
= TYPE_MAIN_VARIANT (arg
);
6136 arg
= strip_typedefs (arg
);
6137 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6138 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6140 if (complain
& tf_warning
)
6141 warning (0, "ignoring attributes on template argument %qT", arg
);
6142 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6143 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6148 /* Convert the indicated template ARG as necessary to match the
6149 indicated template PARM. Returns the converted ARG, or
6150 error_mark_node if the conversion was unsuccessful. Error and
6151 warning messages are issued under control of COMPLAIN. This
6152 conversion is for the Ith parameter in the parameter list. ARGS is
6153 the full set of template arguments deduced so far. */
6156 convert_template_argument (tree parm
,
6159 tsubst_flags_t complain
,
6165 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6167 if (TREE_CODE (arg
) == TREE_LIST
6168 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6170 /* The template argument was the name of some
6171 member function. That's usually
6172 invalid, but static members are OK. In any
6173 case, grab the underlying fields/functions
6174 and issue an error later if required. */
6175 orig_arg
= TREE_VALUE (arg
);
6176 TREE_TYPE (arg
) = unknown_type_node
;
6181 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6182 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6183 || requires_tmpl_type
);
6185 /* When determining whether an argument pack expansion is a template,
6186 look at the pattern. */
6187 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6188 arg
= PACK_EXPANSION_PATTERN (arg
);
6190 /* Deal with an injected-class-name used as a template template arg. */
6191 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6193 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6194 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6196 if (cxx_dialect
>= cxx0x
)
6197 /* OK under DR 1004. */;
6198 else if (complain
& tf_warning_or_error
)
6199 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6200 " used as template template argument", TYPE_NAME (arg
));
6201 else if (flag_pedantic_errors
)
6209 ((TREE_CODE (arg
) == TEMPLATE_DECL
6210 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6211 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6212 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6213 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6216 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6217 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6218 arg
= TYPE_STUB_DECL (arg
);
6220 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6222 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6223 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6225 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6227 if (complain
& tf_error
)
6228 error ("invalid use of destructor %qE as a type", orig_arg
);
6229 return error_mark_node
;
6232 permerror (input_location
,
6233 "to refer to a type member of a template parameter, "
6234 "use %<typename %E%>", orig_arg
);
6236 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6237 TREE_OPERAND (arg
, 1),
6243 if (is_type
!= requires_type
)
6247 if (complain
& tf_error
)
6249 error ("type/value mismatch at argument %d in template "
6250 "parameter list for %qD",
6253 error (" expected a constant of type %qT, got %qT",
6255 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6256 else if (requires_tmpl_type
)
6257 error (" expected a class template, got %qE", orig_arg
);
6259 error (" expected a type, got %qE", orig_arg
);
6262 return error_mark_node
;
6264 if (is_tmpl_type
^ requires_tmpl_type
)
6266 if (in_decl
&& (complain
& tf_error
))
6268 error ("type/value mismatch at argument %d in template "
6269 "parameter list for %qD",
6272 error (" expected a type, got %qT", DECL_NAME (arg
));
6274 error (" expected a class template, got %qT", orig_arg
);
6276 return error_mark_node
;
6281 if (requires_tmpl_type
)
6283 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6285 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6286 /* The number of argument required is not known yet.
6287 Just accept it for now. */
6288 val
= TREE_TYPE (arg
);
6291 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6294 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6296 if (coerce_template_template_parms (parmparm
, argparm
,
6302 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6304 if (val
!= error_mark_node
)
6306 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6307 val
= TREE_TYPE (val
);
6308 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6309 val
= make_pack_expansion (val
);
6314 if (in_decl
&& (complain
& tf_error
))
6316 error ("type/value mismatch at argument %d in "
6317 "template parameter list for %qD",
6319 error (" expected a template of type %qD, got %qT",
6323 val
= error_mark_node
;
6329 /* We only form one instance of each template specialization.
6330 Therefore, if we use a non-canonical variant (i.e., a
6331 typedef), any future messages referring to the type will use
6332 the typedef, which is confusing if those future uses do not
6333 themselves also use the typedef. */
6335 val
= canonicalize_type_argument (val
, complain
);
6339 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6341 if (invalid_nontype_parm_type_p (t
, complain
))
6342 return error_mark_node
;
6344 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6346 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6350 /* Not sure if this is reachable, but it doesn't hurt
6352 error ("type mismatch in nontype parameter pack");
6353 val
= error_mark_node
;
6356 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6357 /* We used to call digest_init here. However, digest_init
6358 will report errors, which we don't want when complain
6359 is zero. More importantly, digest_init will try too
6360 hard to convert things: for example, `0' should not be
6361 converted to pointer type at this point according to
6362 the standard. Accepting this is not merely an
6363 extension, since deciding whether or not these
6364 conversions can occur is part of determining which
6365 function template to call, or whether a given explicit
6366 argument specification is valid. */
6367 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6369 val
= strip_typedefs_expr (orig_arg
);
6371 if (val
== NULL_TREE
)
6372 val
= error_mark_node
;
6373 else if (val
== error_mark_node
&& (complain
& tf_error
))
6374 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6376 if (TREE_CODE (val
) == SCOPE_REF
)
6378 /* Strip typedefs from the SCOPE_REF. */
6379 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6380 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6382 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6383 QUALIFIED_NAME_IS_TEMPLATE (val
));
6390 /* Coerces the remaining template arguments in INNER_ARGS (from
6391 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6392 Returns the coerced argument pack. PARM_IDX is the position of this
6393 parameter in the template parameter list. ARGS is the original
6394 template argument list. */
6396 coerce_template_parameter_pack (tree parms
,
6404 tsubst_flags_t complain
)
6406 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6407 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6410 tree packed_types
= NULL_TREE
;
6412 if (arg_idx
> nargs
)
6415 packed_args
= make_tree_vec (nargs
- arg_idx
);
6417 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6418 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6420 /* When the template parameter is a non-type template
6421 parameter pack whose type uses parameter packs, we need
6422 to look at each of the template arguments
6423 separately. Build a vector of the types for these
6424 non-type template parameters in PACKED_TYPES. */
6426 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6427 packed_types
= tsubst_pack_expansion (expansion
, args
,
6430 if (packed_types
== error_mark_node
)
6431 return error_mark_node
;
6433 /* Check that we have the right number of arguments. */
6435 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6436 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6439 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6440 error ("wrong number of template arguments (%d, should be %d)",
6441 nargs
, needed_parms
);
6442 return error_mark_node
;
6445 /* If we aren't able to check the actual arguments now
6446 (because they haven't been expanded yet), we can at least
6447 verify that all of the types used for the non-type
6448 template parameter pack are, in fact, valid for non-type
6449 template parameters. */
6451 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6453 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6454 for (j
= 0; j
< len
; ++j
)
6456 tree t
= TREE_VEC_ELT (packed_types
, j
);
6457 if (invalid_nontype_parm_type_p (t
, complain
))
6458 return error_mark_node
;
6463 /* Convert the remaining arguments, which will be a part of the
6464 parameter pack "parm". */
6465 for (; arg_idx
< nargs
; ++arg_idx
)
6467 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6468 tree actual_parm
= TREE_VALUE (parm
);
6470 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6472 /* When we have a vector of types (corresponding to the
6473 non-type template parameter pack that uses parameter
6474 packs in its type, as mention above), and the
6475 argument is not an expansion (which expands to a
6476 currently unknown number of arguments), clone the
6477 parm and give it the next type in PACKED_TYPES. */
6478 actual_parm
= copy_node (actual_parm
);
6479 TREE_TYPE (actual_parm
) =
6480 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6483 if (arg
== error_mark_node
)
6485 if (complain
& tf_error
)
6486 error ("template argument %d is invalid", arg_idx
+ 1);
6489 arg
= convert_template_argument (actual_parm
,
6490 arg
, new_args
, complain
, parm_idx
,
6492 if (arg
== error_mark_node
)
6494 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6497 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6498 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6499 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6502 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6503 TREE_TYPE (argument_pack
)
6504 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6505 TREE_CONSTANT (argument_pack
) = 1;
6508 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6509 #ifdef ENABLE_CHECKING
6510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6511 TREE_VEC_LENGTH (packed_args
));
6513 return argument_pack
;
6516 /* Returns true if the template argument vector ARGS contains
6517 any pack expansions, false otherwise. */
6520 any_pack_expanson_args_p (tree args
)
6524 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6525 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6530 /* Convert all template arguments to their appropriate types, and
6531 return a vector containing the innermost resulting template
6532 arguments. If any error occurs, return error_mark_node. Error and
6533 warning messages are issued under control of COMPLAIN.
6535 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6536 for arguments not specified in ARGS. Otherwise, if
6537 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6538 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6539 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6543 coerce_template_parms (tree parms
,
6546 tsubst_flags_t complain
,
6547 bool require_all_args
,
6548 bool use_default_args
)
6550 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6553 tree new_inner_args
;
6554 int saved_unevaluated_operand
;
6555 int saved_inhibit_evaluation_warnings
;
6557 /* When used as a boolean value, indicates whether this is a
6558 variadic template parameter list. Since it's an int, we can also
6559 subtract it from nparms to get the number of non-variadic
6562 int post_variadic_parms
= 0;
6564 if (args
== error_mark_node
)
6565 return error_mark_node
;
6567 nparms
= TREE_VEC_LENGTH (parms
);
6569 /* Determine if there are any parameter packs. */
6570 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6572 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6574 ++post_variadic_parms
;
6575 if (template_parameter_pack_p (tparm
))
6579 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6580 /* If there are no parameters that follow a parameter pack, we need to
6581 expand any argument packs so that we can deduce a parameter pack from
6582 some non-packed args followed by an argument pack, as in variadic85.C.
6583 If there are such parameters, we need to leave argument packs intact
6584 so the arguments are assigned properly. This can happen when dealing
6585 with a nested class inside a partial specialization of a class
6586 template, as in variadic92.C, or when deducing a template parameter pack
6587 from a sub-declarator, as in variadic114.C. */
6588 if (!post_variadic_parms
)
6589 inner_args
= expand_template_argument_pack (inner_args
);
6591 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6592 if ((nargs
> nparms
&& !variadic_p
)
6593 || (nargs
< nparms
- variadic_p
6595 && !any_pack_expanson_args_p (inner_args
)
6596 && (!use_default_args
6597 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6598 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6600 if (complain
& tf_error
)
6604 nparms
-= variadic_p
;
6605 error ("wrong number of template arguments "
6606 "(%d, should be %d or more)", nargs
, nparms
);
6609 error ("wrong number of template arguments "
6610 "(%d, should be %d)", nargs
, nparms
);
6613 error ("provided for %q+D", in_decl
);
6616 return error_mark_node
;
6619 /* We need to evaluate the template arguments, even though this
6620 template-id may be nested within a "sizeof". */
6621 saved_unevaluated_operand
= cp_unevaluated_operand
;
6622 cp_unevaluated_operand
= 0;
6623 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6624 c_inhibit_evaluation_warnings
= 0;
6625 new_inner_args
= make_tree_vec (nparms
);
6626 new_args
= add_outermost_template_args (args
, new_inner_args
);
6627 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6632 /* Get the Ith template parameter. */
6633 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6635 if (parm
== error_mark_node
)
6637 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6641 /* Calculate the next argument. */
6642 if (arg_idx
< nargs
)
6643 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6647 if (template_parameter_pack_p (TREE_VALUE (parm
))
6648 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6650 /* All remaining arguments will be placed in the
6651 template parameter pack PARM. */
6652 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6653 inner_args
, arg_idx
,
6657 /* Store this argument. */
6658 if (arg
== error_mark_node
)
6660 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6662 /* We are done with all of the arguments. */
6669 if (PACK_EXPANSION_P (arg
))
6671 /* We don't know how many args we have yet, just
6672 use the unconverted ones for now. */
6673 new_inner_args
= inner_args
;
6677 else if (require_all_args
)
6679 /* There must be a default arg in this case. */
6680 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6682 /* The position of the first default template argument,
6683 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6685 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6691 if (arg
== error_mark_node
)
6693 if (complain
& tf_error
)
6694 error ("template argument %d is invalid", arg_idx
+ 1);
6697 /* This only occurs if there was an error in the template
6698 parameter list itself (which we would already have
6699 reported) that we are trying to recover from, e.g., a class
6700 template with a parameter list such as
6701 template<typename..., typename>. */
6704 arg
= convert_template_argument (TREE_VALUE (parm
),
6705 arg
, new_args
, complain
,
6708 if (arg
== error_mark_node
)
6710 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6712 cp_unevaluated_operand
= saved_unevaluated_operand
;
6713 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6716 return error_mark_node
;
6718 #ifdef ENABLE_CHECKING
6719 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6721 TREE_VEC_LENGTH (new_inner_args
));
6724 return new_inner_args
;
6727 /* Like coerce_template_parms. If PARMS represents all template
6728 parameters levels, this function returns a vector of vectors
6729 representing all the resulting argument levels. Note that in this
6730 case, only the innermost arguments are coerced because the
6731 outermost ones are supposed to have been coerced already.
6733 Otherwise, if PARMS represents only (the innermost) vector of
6734 parameters, this function returns a vector containing just the
6735 innermost resulting arguments. */
6738 coerce_innermost_template_parms (tree parms
,
6741 tsubst_flags_t complain
,
6742 bool require_all_args
,
6743 bool use_default_args
)
6745 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6746 int args_depth
= TMPL_ARGS_DEPTH (args
);
6749 if (parms_depth
> 1)
6751 coerced_args
= make_tree_vec (parms_depth
);
6755 for (level
= parms
, cur_depth
= parms_depth
;
6756 parms_depth
> 0 && level
!= NULL_TREE
;
6757 level
= TREE_CHAIN (level
), --cur_depth
)
6760 if (cur_depth
== args_depth
)
6761 l
= coerce_template_parms (TREE_VALUE (level
),
6762 args
, in_decl
, complain
,
6766 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6768 if (l
== error_mark_node
)
6769 return error_mark_node
;
6771 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6775 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6776 args
, in_decl
, complain
,
6779 return coerced_args
;
6782 /* Returns 1 if template args OT and NT are equivalent. */
6785 template_args_equal (tree ot
, tree nt
)
6789 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6792 if (TREE_CODE (nt
) == TREE_VEC
)
6793 /* For member templates */
6794 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6795 else if (PACK_EXPANSION_P (ot
))
6796 return (PACK_EXPANSION_P (nt
)
6797 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6798 PACK_EXPANSION_PATTERN (nt
))
6799 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6800 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6801 else if (ARGUMENT_PACK_P (ot
))
6806 if (!ARGUMENT_PACK_P (nt
))
6809 opack
= ARGUMENT_PACK_ARGS (ot
);
6810 npack
= ARGUMENT_PACK_ARGS (nt
);
6811 len
= TREE_VEC_LENGTH (opack
);
6812 if (TREE_VEC_LENGTH (npack
) != len
)
6814 for (i
= 0; i
< len
; ++i
)
6815 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6816 TREE_VEC_ELT (npack
, i
)))
6820 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6822 /* We get here probably because we are in the middle of substituting
6823 into the pattern of a pack expansion. In that case the
6824 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6825 interested in. So we want to use the initial pack argument for
6827 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6828 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6829 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6830 return template_args_equal (ot
, nt
);
6832 else if (TYPE_P (nt
))
6833 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6834 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6837 return cp_tree_equal (ot
, nt
);
6840 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6841 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6842 NEWARG_PTR with the offending arguments if they are non-NULL. */
6845 comp_template_args_with_info (tree oldargs
, tree newargs
,
6846 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6850 if (oldargs
== newargs
)
6853 if (!oldargs
|| !newargs
)
6856 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6859 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6861 tree nt
= TREE_VEC_ELT (newargs
, i
);
6862 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6864 if (! template_args_equal (ot
, nt
))
6866 if (oldarg_ptr
!= NULL
)
6868 if (newarg_ptr
!= NULL
)
6876 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6877 of template arguments. Returns 0 otherwise. */
6880 comp_template_args (tree oldargs
, tree newargs
)
6882 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6886 add_pending_template (tree d
)
6888 tree ti
= (TYPE_P (d
)
6889 ? CLASSTYPE_TEMPLATE_INFO (d
)
6890 : DECL_TEMPLATE_INFO (d
));
6891 struct pending_template
*pt
;
6894 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6897 /* We are called both from instantiate_decl, where we've already had a
6898 tinst_level pushed, and instantiate_template, where we haven't.
6900 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6903 push_tinst_level (d
);
6905 pt
= ggc_alloc_pending_template ();
6907 pt
->tinst
= current_tinst_level
;
6908 if (last_pending_template
)
6909 last_pending_template
->next
= pt
;
6911 pending_templates
= pt
;
6913 last_pending_template
= pt
;
6915 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6922 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6923 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6924 documentation for TEMPLATE_ID_EXPR. */
6927 lookup_template_function (tree fns
, tree arglist
)
6931 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6932 return error_mark_node
;
6934 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6936 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6938 error ("%q#D is not a function template", fns
);
6939 return error_mark_node
;
6942 if (BASELINK_P (fns
))
6944 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6946 BASELINK_FUNCTIONS (fns
),
6951 type
= TREE_TYPE (fns
);
6952 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6953 type
= unknown_type_node
;
6955 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6958 /* Within the scope of a template class S<T>, the name S gets bound
6959 (in build_self_reference) to a TYPE_DECL for the class, not a
6960 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6961 or one of its enclosing classes, and that type is a template,
6962 return the associated TEMPLATE_DECL. Otherwise, the original
6965 Also handle the case when DECL is a TREE_LIST of ambiguous
6966 injected-class-names from different bases. */
6969 maybe_get_template_decl_from_type_decl (tree decl
)
6971 if (decl
== NULL_TREE
)
6974 /* DR 176: A lookup that finds an injected-class-name (10.2
6975 [class.member.lookup]) can result in an ambiguity in certain cases
6976 (for example, if it is found in more than one base class). If all of
6977 the injected-class-names that are found refer to specializations of
6978 the same class template, and if the name is followed by a
6979 template-argument-list, the reference refers to the class template
6980 itself and not a specialization thereof, and is not ambiguous. */
6981 if (TREE_CODE (decl
) == TREE_LIST
)
6983 tree t
, tmpl
= NULL_TREE
;
6984 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6986 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6989 else if (tmpl
!= elt
)
6992 if (tmpl
&& t
== NULL_TREE
)
6998 return (decl
!= NULL_TREE
6999 && DECL_SELF_REFERENCE_P (decl
)
7000 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7001 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7004 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7005 parameters, find the desired type.
7007 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7009 IN_DECL, if non-NULL, is the template declaration we are trying to
7012 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7013 the class we are looking up.
7015 Issue error and warning messages under control of COMPLAIN.
7017 If the template class is really a local class in a template
7018 function, then the FUNCTION_CONTEXT is the function in which it is
7021 ??? Note that this function is currently called *twice* for each
7022 template-id: the first time from the parser, while creating the
7023 incomplete type (finish_template_type), and the second type during the
7024 real instantiation (instantiate_template_class). This is surely something
7025 that we want to avoid. It also causes some problems with argument
7026 coercion (see convert_nontype_argument for more information on this). */
7029 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7030 int entering_scope
, tsubst_flags_t complain
)
7032 tree templ
= NULL_TREE
, parmlist
;
7039 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7041 tree value
= innermost_non_namespace_value (d1
);
7042 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7047 push_decl_namespace (context
);
7048 templ
= lookup_name (d1
);
7049 templ
= maybe_get_template_decl_from_type_decl (templ
);
7051 pop_decl_namespace ();
7054 context
= DECL_CONTEXT (templ
);
7056 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7058 tree type
= TREE_TYPE (d1
);
7060 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7061 an implicit typename for the second A. Deal with it. */
7062 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7063 type
= TREE_TYPE (type
);
7065 if (CLASSTYPE_TEMPLATE_INFO (type
))
7067 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7068 d1
= DECL_NAME (templ
);
7071 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7072 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7074 templ
= TYPE_TI_TEMPLATE (d1
);
7075 d1
= DECL_NAME (templ
);
7077 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7078 && DECL_TEMPLATE_RESULT (d1
)
7079 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7082 d1
= DECL_NAME (templ
);
7083 context
= DECL_CONTEXT (templ
);
7086 /* Issue an error message if we didn't find a template. */
7089 if (complain
& tf_error
)
7090 error ("%qT is not a template", d1
);
7091 return error_mark_node
;
7094 if (TREE_CODE (templ
) != TEMPLATE_DECL
7095 /* Make sure it's a user visible template, if it was named by
7097 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7098 && !PRIMARY_TEMPLATE_P (templ
)))
7100 if (complain
& tf_error
)
7102 error ("non-template type %qT used as a template", d1
);
7104 error ("for template declaration %q+D", in_decl
);
7106 return error_mark_node
;
7109 complain
&= ~tf_user
;
7111 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7113 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7114 template arguments */
7120 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7122 /* Consider an example where a template template parameter declared as
7124 template <class T, class U = std::allocator<T> > class TT
7126 The template parameter level of T and U are one level larger than
7127 of TT. To proper process the default argument of U, say when an
7128 instantiation `TT<int>' is seen, we need to build the full
7129 arguments containing {int} as the innermost level. Outer levels,
7130 available when not appearing as default template argument, can be
7131 obtained from the arguments of the enclosing template.
7133 Suppose that TT is later substituted with std::vector. The above
7134 instantiation is `TT<int, std::allocator<T> >' with TT at
7135 level 1, and T at level 2, while the template arguments at level 1
7136 becomes {std::vector} and the inner level 2 is {int}. */
7138 outer
= DECL_CONTEXT (templ
);
7140 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7141 else if (current_template_parms
)
7142 /* This is an argument of the current template, so we haven't set
7143 DECL_CONTEXT yet. */
7144 outer
= current_template_args ();
7147 arglist
= add_to_template_args (outer
, arglist
);
7149 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7151 /*require_all_args=*/true,
7152 /*use_default_args=*/true);
7153 if (arglist2
== error_mark_node
7154 || (!uses_template_parms (arglist2
)
7155 && check_instantiated_args (templ
, arglist2
, complain
)))
7156 return error_mark_node
;
7158 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7163 tree template_type
= TREE_TYPE (templ
);
7166 tree found
= NULL_TREE
;
7169 int is_dependent_type
;
7170 int use_partial_inst_tmpl
= false;
7172 if (template_type
== error_mark_node
)
7173 /* An error occured while building the template TEMPL, and a
7174 diagnostic has most certainly been emitted for that
7175 already. Let's propagate that error. */
7176 return error_mark_node
;
7178 gen_tmpl
= most_general_template (templ
);
7179 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7180 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7181 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7183 if (arg_depth
== 1 && parm_depth
> 1)
7185 /* We've been given an incomplete set of template arguments.
7188 template <class T> struct S1 {
7189 template <class U> struct S2 {};
7190 template <class U> struct S2<U*> {};
7193 we will be called with an ARGLIST of `U*', but the
7194 TEMPLATE will be `template <class T> template
7195 <class U> struct S1<T>::S2'. We must fill in the missing
7198 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7200 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7203 /* Now we should have enough arguments. */
7204 gcc_assert (parm_depth
== arg_depth
);
7206 /* From here on, we're only interested in the most general
7209 /* Calculate the BOUND_ARGS. These will be the args that are
7210 actually tsubst'd into the definition to create the
7214 /* We have multiple levels of arguments to coerce, at once. */
7216 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7218 tree bound_args
= make_tree_vec (parm_depth
);
7220 for (i
= saved_depth
,
7221 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7222 i
> 0 && t
!= NULL_TREE
;
7223 --i
, t
= TREE_CHAIN (t
))
7226 if (i
== saved_depth
)
7227 a
= coerce_template_parms (TREE_VALUE (t
),
7230 /*require_all_args=*/true,
7231 /*use_default_args=*/true);
7233 /* Outer levels should have already been coerced. */
7234 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7236 /* Don't process further if one of the levels fails. */
7237 if (a
== error_mark_node
)
7239 /* Restore the ARGLIST to its full size. */
7240 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7241 return error_mark_node
;
7244 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7246 /* We temporarily reduce the length of the ARGLIST so
7247 that coerce_template_parms will see only the arguments
7248 corresponding to the template parameters it is
7250 TREE_VEC_LENGTH (arglist
)--;
7253 /* Restore the ARGLIST to its full size. */
7254 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7256 arglist
= bound_args
;
7260 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7261 INNERMOST_TEMPLATE_ARGS (arglist
),
7264 /*require_all_args=*/true,
7265 /*use_default_args=*/true);
7267 if (arglist
== error_mark_node
)
7268 /* We were unable to bind the arguments. */
7269 return error_mark_node
;
7271 /* In the scope of a template class, explicit references to the
7272 template class refer to the type of the template, not any
7273 instantiation of it. For example, in:
7275 template <class T> class C { void f(C<T>); }
7277 the `C<T>' is just the same as `C'. Outside of the
7278 class, however, such a reference is an instantiation. */
7280 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7281 || currently_open_class (template_type
))
7282 /* comp_template_args is expensive, check it last. */
7283 && comp_template_args (TYPE_TI_ARGS (template_type
),
7285 return template_type
;
7287 /* If we already have this specialization, return it. */
7288 elt
.tmpl
= gen_tmpl
;
7290 hash
= hash_specialization (&elt
);
7291 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7297 is_dependent_type
= uses_template_parms (arglist
);
7299 /* If the deduced arguments are invalid, then the binding
7301 if (!is_dependent_type
7302 && check_instantiated_args (gen_tmpl
,
7303 INNERMOST_TEMPLATE_ARGS (arglist
),
7305 return error_mark_node
;
7307 if (!is_dependent_type
7308 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7309 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7310 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7312 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7313 DECL_NAME (gen_tmpl
),
7314 /*tag_scope=*/ts_global
);
7318 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7320 if (context
== error_mark_node
)
7321 return error_mark_node
;
7324 context
= global_namespace
;
7326 /* Create the type. */
7327 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7329 if (!is_dependent_type
)
7331 set_current_access_from_decl (TYPE_NAME (template_type
));
7332 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7333 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7334 arglist
, complain
, in_decl
),
7335 SCOPED_ENUM_P (template_type
), NULL
);
7339 /* We don't want to call start_enum for this type, since
7340 the values for the enumeration constants may involve
7341 template parameters. And, no one should be interested
7342 in the enumeration constants for such a type. */
7343 t
= cxx_make_type (ENUMERAL_TYPE
);
7344 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7346 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7347 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7348 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7350 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7352 /* The user referred to a specialization of an alias
7353 template represented by GEN_TMPL.
7355 [temp.alias]/2 says:
7357 When a template-id refers to the specialization of an
7358 alias template, it is equivalent to the associated
7359 type obtained by substitution of its
7360 template-arguments for the template-parameters in the
7361 type-id of the alias template. */
7363 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7364 /* Note that the call above (by indirectly calling
7365 register_specialization in tsubst_decl) registers the
7366 TYPE_DECL representing the specialization of the alias
7367 template. So next time someone substitutes ARGLIST for
7368 the template parms into the alias template (GEN_TMPL),
7369 she'll get that TYPE_DECL back. */
7371 if (t
== error_mark_node
)
7374 else if (CLASS_TYPE_P (template_type
))
7376 t
= make_class_type (TREE_CODE (template_type
));
7377 CLASSTYPE_DECLARED_CLASS (t
)
7378 = CLASSTYPE_DECLARED_CLASS (template_type
);
7379 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7380 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7382 /* A local class. Make sure the decl gets registered properly. */
7383 if (context
== current_function_decl
)
7384 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7386 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7387 /* This instantiation is another name for the primary
7388 template type. Set the TYPE_CANONICAL field
7390 TYPE_CANONICAL (t
) = template_type
;
7391 else if (any_template_arguments_need_structural_equality_p (arglist
))
7392 /* Some of the template arguments require structural
7393 equality testing, so this template class requires
7394 structural equality testing. */
7395 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7400 /* If we called start_enum or pushtag above, this information
7401 will already be set up. */
7404 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7406 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7407 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7408 DECL_SOURCE_LOCATION (type_decl
)
7409 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7412 type_decl
= TYPE_NAME (t
);
7414 if (CLASS_TYPE_P (template_type
))
7416 TREE_PRIVATE (type_decl
)
7417 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7418 TREE_PROTECTED (type_decl
)
7419 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7420 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7422 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7423 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7427 /* Let's consider the explicit specialization of a member
7428 of a class template specialization that is implicitely instantiated,
7433 template<class U> struct M {}; //#0
7438 struct S<int>::M<char> //#1
7442 [temp.expl.spec]/4 says this is valid.
7444 In this case, when we write:
7447 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7450 When we encounter #1, we want to store the partial instantiation
7451 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7453 For all cases other than this "explicit specialization of member of a
7454 class template", we just want to store the most general template into
7455 the CLASSTYPE_TI_TEMPLATE of M.
7457 This case of "explicit specialization of member of a class template"
7459 1/ the enclosing class is an instantiation of, and therefore not
7460 the same as, the context of the most general template, and
7461 2/ we aren't looking at the partial instantiation itself, i.e.
7462 the innermost arguments are not the same as the innermost parms of
7463 the most general template.
7465 So it's only when 1/ and 2/ happens that we want to use the partial
7466 instantiation of the member template in lieu of its most general
7469 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7470 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7471 /* the enclosing class must be an instantiation... */
7472 && CLASS_TYPE_P (context
)
7473 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7475 tree partial_inst_args
;
7476 TREE_VEC_LENGTH (arglist
)--;
7477 ++processing_template_decl
;
7479 tsubst (INNERMOST_TEMPLATE_ARGS
7480 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7481 arglist
, complain
, NULL_TREE
);
7482 --processing_template_decl
;
7483 TREE_VEC_LENGTH (arglist
)++;
7484 use_partial_inst_tmpl
=
7485 /*...and we must not be looking at the partial instantiation
7487 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7491 if (!use_partial_inst_tmpl
)
7492 /* This case is easy; there are no member templates involved. */
7496 /* This is a full instantiation of a member template. Find
7497 the partial instantiation of which this is an instance. */
7499 /* Temporarily reduce by one the number of levels in the ARGLIST
7500 so as to avoid comparing the last set of arguments. */
7501 TREE_VEC_LENGTH (arglist
)--;
7502 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7503 TREE_VEC_LENGTH (arglist
)++;
7504 /* FOUND is either a proper class type, or an alias
7505 template specialization. In the later case, it's a
7506 TYPE_DECL, resulting from the substituting of arguments
7507 for parameters in the TYPE_DECL of the alias template
7508 done earlier. So be careful while getting the template
7510 found
= TREE_CODE (found
) == TYPE_DECL
7511 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7512 : CLASSTYPE_TI_TEMPLATE (found
);
7515 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7518 slot
= htab_find_slot_with_hash (type_specializations
,
7519 &elt
, hash
, INSERT
);
7520 entry
= ggc_alloc_spec_entry ();
7524 /* Note this use of the partial instantiation so we can check it
7525 later in maybe_process_partial_specialization. */
7526 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7527 = tree_cons (arglist
, t
,
7528 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7530 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7531 /* Now that the type has been registered on the instantiations
7532 list, we set up the enumerators. Because the enumeration
7533 constants may involve the enumeration type itself, we make
7534 sure to register the type first, and then create the
7535 constants. That way, doing tsubst_expr for the enumeration
7536 constants won't result in recursive calls here; we'll find
7537 the instantiation and exit above. */
7538 tsubst_enum (template_type
, t
, arglist
);
7540 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7541 /* If the type makes use of template parameters, the
7542 code that generates debugging information will crash. */
7543 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7545 /* Possibly limit visibility based on template args. */
7546 TREE_PUBLIC (type_decl
) = 1;
7547 determine_visibility (type_decl
);
7553 /* Wrapper for lookup_template_class_1. */
7556 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7557 int entering_scope
, tsubst_flags_t complain
)
7560 timevar_push (TV_TEMPLATE_INST
);
7561 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7562 entering_scope
, complain
);
7563 timevar_pop (TV_TEMPLATE_INST
);
7571 /* True when we should also visit template parameters that occur in
7572 non-deduced contexts. */
7573 bool include_nondeduced_p
;
7574 struct pointer_set_t
*visited
;
7577 /* Called from for_each_template_parm via walk_tree. */
7580 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7583 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7584 tree_fn_t fn
= pfd
->fn
;
7585 void *data
= pfd
->data
;
7588 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7589 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7590 pfd
->include_nondeduced_p
))
7591 return error_mark_node
;
7593 switch (TREE_CODE (t
))
7596 if (TYPE_PTRMEMFUNC_P (t
))
7602 if (!TYPE_TEMPLATE_INFO (t
))
7604 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7605 fn
, data
, pfd
->visited
,
7606 pfd
->include_nondeduced_p
))
7607 return error_mark_node
;
7611 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7612 fn
, data
, pfd
->visited
,
7613 pfd
->include_nondeduced_p
)
7614 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7615 fn
, data
, pfd
->visited
,
7616 pfd
->include_nondeduced_p
))
7617 return error_mark_node
;
7621 /* Since we're not going to walk subtrees, we have to do this
7623 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7624 pfd
->visited
, pfd
->include_nondeduced_p
))
7625 return error_mark_node
;
7629 /* Check the return type. */
7630 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7631 pfd
->include_nondeduced_p
))
7632 return error_mark_node
;
7634 /* Check the parameter types. Since default arguments are not
7635 instantiated until they are needed, the TYPE_ARG_TYPES may
7636 contain expressions that involve template parameters. But,
7637 no-one should be looking at them yet. And, once they're
7638 instantiated, they don't contain template parameters, so
7639 there's no point in looking at them then, either. */
7643 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7644 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7645 pfd
->visited
, pfd
->include_nondeduced_p
))
7646 return error_mark_node
;
7648 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7649 want walk_tree walking into them itself. */
7655 case UNDERLYING_TYPE
:
7656 if (pfd
->include_nondeduced_p
7657 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7659 pfd
->include_nondeduced_p
))
7660 return error_mark_node
;
7665 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7666 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7667 pfd
->visited
, pfd
->include_nondeduced_p
))
7668 return error_mark_node
;
7673 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7674 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7675 pfd
->visited
, pfd
->include_nondeduced_p
))
7676 return error_mark_node
;
7677 if (DECL_CONTEXT (t
)
7678 && pfd
->include_nondeduced_p
7679 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7680 pfd
->visited
, pfd
->include_nondeduced_p
))
7681 return error_mark_node
;
7684 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7685 /* Record template parameters such as `T' inside `TT<T>'. */
7686 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7687 pfd
->include_nondeduced_p
))
7688 return error_mark_node
;
7691 case TEMPLATE_TEMPLATE_PARM
:
7692 case TEMPLATE_TYPE_PARM
:
7693 case TEMPLATE_PARM_INDEX
:
7694 if (fn
&& (*fn
)(t
, data
))
7695 return error_mark_node
;
7697 return error_mark_node
;
7701 /* A template template parameter is encountered. */
7702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7703 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7704 pfd
->include_nondeduced_p
))
7705 return error_mark_node
;
7707 /* Already substituted template template parameter */
7713 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7715 pfd
->include_nondeduced_p
))
7716 return error_mark_node
;
7720 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7721 && pfd
->include_nondeduced_p
7722 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7723 (TREE_TYPE (t
)), fn
, data
,
7724 pfd
->visited
, pfd
->include_nondeduced_p
))
7725 return error_mark_node
;
7730 /* If there's no type, then this thing must be some expression
7731 involving template parameters. */
7732 if (!fn
&& !TREE_TYPE (t
))
7733 return error_mark_node
;
7738 case IMPLICIT_CONV_EXPR
:
7739 case REINTERPRET_CAST_EXPR
:
7740 case CONST_CAST_EXPR
:
7741 case STATIC_CAST_EXPR
:
7742 case DYNAMIC_CAST_EXPR
:
7746 case PSEUDO_DTOR_EXPR
:
7748 return error_mark_node
;
7755 /* We didn't find any template parameters we liked. */
7759 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7760 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7761 call FN with the parameter and the DATA.
7762 If FN returns nonzero, the iteration is terminated, and
7763 for_each_template_parm returns 1. Otherwise, the iteration
7764 continues. If FN never returns a nonzero value, the value
7765 returned by for_each_template_parm is 0. If FN is NULL, it is
7766 considered to be the function which always returns 1.
7768 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7769 parameters that occur in non-deduced contexts. When false, only
7770 visits those template parameters that can be deduced. */
7773 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7774 struct pointer_set_t
*visited
,
7775 bool include_nondeduced_p
)
7777 struct pair_fn_data pfd
;
7783 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7785 /* Walk the tree. (Conceptually, we would like to walk without
7786 duplicates, but for_each_template_parm_r recursively calls
7787 for_each_template_parm, so we would need to reorganize a fair
7788 bit to use walk_tree_without_duplicates, so we keep our own
7791 pfd
.visited
= visited
;
7793 pfd
.visited
= pointer_set_create ();
7794 result
= cp_walk_tree (&t
,
7795 for_each_template_parm_r
,
7797 pfd
.visited
) != NULL_TREE
;
7802 pointer_set_destroy (pfd
.visited
);
7809 /* Returns true if T depends on any template parameter. */
7812 uses_template_parms (tree t
)
7815 int saved_processing_template_decl
;
7817 saved_processing_template_decl
= processing_template_decl
;
7818 if (!saved_processing_template_decl
)
7819 processing_template_decl
= 1;
7821 dependent_p
= dependent_type_p (t
);
7822 else if (TREE_CODE (t
) == TREE_VEC
)
7823 dependent_p
= any_dependent_template_arguments_p (t
);
7824 else if (TREE_CODE (t
) == TREE_LIST
)
7825 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7826 || uses_template_parms (TREE_CHAIN (t
)));
7827 else if (TREE_CODE (t
) == TYPE_DECL
)
7828 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7831 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7832 || TREE_CODE (t
) == OVERLOAD
7834 || TREE_CODE (t
) == IDENTIFIER_NODE
7835 || TREE_CODE (t
) == TRAIT_EXPR
7836 || TREE_CODE (t
) == CONSTRUCTOR
7837 || CONSTANT_CLASS_P (t
))
7838 dependent_p
= (type_dependent_expression_p (t
)
7839 || value_dependent_expression_p (t
));
7842 gcc_assert (t
== error_mark_node
);
7843 dependent_p
= false;
7846 processing_template_decl
= saved_processing_template_decl
;
7851 /* Returns true iff current_function_decl is an incompletely instantiated
7852 template. Useful instead of processing_template_decl because the latter
7853 is set to 0 during fold_non_dependent_expr. */
7856 in_template_function (void)
7858 tree fn
= current_function_decl
;
7860 ++processing_template_decl
;
7861 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7862 && DECL_TEMPLATE_INFO (fn
)
7863 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7864 --processing_template_decl
;
7868 /* Returns true if T depends on any template parameter with level LEVEL. */
7871 uses_template_parms_level (tree t
, int level
)
7873 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7874 /*include_nondeduced_p=*/true);
7877 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7878 ill-formed translation unit, i.e. a variable or function that isn't
7879 usable in a constant expression. */
7882 neglectable_inst_p (tree d
)
7885 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7886 : decl_maybe_constant_var_p (d
)));
7889 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7890 neglectable and instantiated from within an erroneous instantiation. */
7893 limit_bad_template_recursion (tree decl
)
7895 struct tinst_level
*lev
= current_tinst_level
;
7896 int errs
= errorcount
+ sorrycount
;
7897 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7900 for (; lev
; lev
= lev
->next
)
7901 if (neglectable_inst_p (lev
->decl
))
7904 return (lev
&& errs
> lev
->errors
);
7907 static int tinst_depth
;
7908 extern int max_tinst_depth
;
7911 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7913 /* We're starting to instantiate D; record the template instantiation context
7914 for diagnostics and to restore it later. */
7917 push_tinst_level (tree d
)
7919 struct tinst_level
*new_level
;
7921 if (tinst_depth
>= max_tinst_depth
)
7923 last_error_tinst_level
= current_tinst_level
;
7924 if (TREE_CODE (d
) == TREE_LIST
)
7925 error ("template instantiation depth exceeds maximum of %d (use "
7926 "-ftemplate-depth= to increase the maximum) substituting %qS",
7927 max_tinst_depth
, d
);
7929 error ("template instantiation depth exceeds maximum of %d (use "
7930 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7931 max_tinst_depth
, d
);
7933 print_instantiation_context ();
7938 /* If the current instantiation caused problems, don't let it instantiate
7939 anything else. Do allow deduction substitution and decls usable in
7940 constant expressions. */
7941 if (limit_bad_template_recursion (d
))
7944 new_level
= ggc_alloc_tinst_level ();
7945 new_level
->decl
= d
;
7946 new_level
->locus
= input_location
;
7947 new_level
->errors
= errorcount
+sorrycount
;
7948 new_level
->in_system_header_p
= in_system_header
;
7949 new_level
->next
= current_tinst_level
;
7950 current_tinst_level
= new_level
;
7953 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7954 depth_reached
= tinst_depth
;
7959 /* We're done instantiating this template; return to the instantiation
7963 pop_tinst_level (void)
7965 /* Restore the filename and line number stashed away when we started
7966 this instantiation. */
7967 input_location
= current_tinst_level
->locus
;
7968 current_tinst_level
= current_tinst_level
->next
;
7972 /* We're instantiating a deferred template; restore the template
7973 instantiation context in which the instantiation was requested, which
7974 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7977 reopen_tinst_level (struct tinst_level
*level
)
7979 struct tinst_level
*t
;
7982 for (t
= level
; t
; t
= t
->next
)
7985 current_tinst_level
= level
;
7987 if (current_tinst_level
)
7988 current_tinst_level
->errors
= errorcount
+sorrycount
;
7992 /* Returns the TINST_LEVEL which gives the original instantiation
7995 struct tinst_level
*
7996 outermost_tinst_level (void)
7998 struct tinst_level
*level
= current_tinst_level
;
8001 level
= level
->next
;
8005 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8006 vector of template arguments, as for tsubst.
8008 Returns an appropriate tsubst'd friend declaration. */
8011 tsubst_friend_function (tree decl
, tree args
)
8015 if (TREE_CODE (decl
) == FUNCTION_DECL
8016 && DECL_TEMPLATE_INSTANTIATION (decl
)
8017 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8018 /* This was a friend declared with an explicit template
8019 argument list, e.g.:
8023 to indicate that f was a template instantiation, not a new
8024 function declaration. Now, we have to figure out what
8025 instantiation of what template. */
8027 tree template_id
, arglist
, fns
;
8030 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8032 /* Friend functions are looked up in the containing namespace scope.
8033 We must enter that scope, to avoid finding member functions of the
8034 current class with same name. */
8035 push_nested_namespace (ns
);
8036 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8037 tf_warning_or_error
, NULL_TREE
,
8038 /*integral_constant_expression_p=*/false);
8039 pop_nested_namespace (ns
);
8040 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8041 tf_warning_or_error
, NULL_TREE
);
8042 template_id
= lookup_template_function (fns
, arglist
);
8044 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8045 tmpl
= determine_specialization (template_id
, new_friend
,
8047 /*need_member_template=*/0,
8048 TREE_VEC_LENGTH (args
),
8050 return instantiate_template (tmpl
, new_args
, tf_error
);
8053 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8055 /* The NEW_FRIEND will look like an instantiation, to the
8056 compiler, but is not an instantiation from the point of view of
8057 the language. For example, we might have had:
8059 template <class T> struct S {
8060 template <class U> friend void f(T, U);
8063 Then, in S<int>, template <class U> void f(int, U) is not an
8064 instantiation of anything. */
8065 if (new_friend
== error_mark_node
)
8066 return error_mark_node
;
8068 DECL_USE_TEMPLATE (new_friend
) = 0;
8069 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8071 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8072 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8073 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8076 /* The mangled name for the NEW_FRIEND is incorrect. The function
8077 is not a template instantiation and should not be mangled like
8078 one. Therefore, we forget the mangling here; we'll recompute it
8079 later if we need it. */
8080 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8082 SET_DECL_RTL (new_friend
, NULL
);
8083 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8086 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8089 tree new_friend_template_info
;
8090 tree new_friend_result_template_info
;
8092 int new_friend_is_defn
;
8094 /* We must save some information from NEW_FRIEND before calling
8095 duplicate decls since that function will free NEW_FRIEND if
8097 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8098 new_friend_is_defn
=
8099 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8100 (template_for_substitution (new_friend
)))
8102 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8104 /* This declaration is a `primary' template. */
8105 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8107 new_friend_result_template_info
8108 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8111 new_friend_result_template_info
= NULL_TREE
;
8113 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8114 if (new_friend_is_defn
)
8115 DECL_INITIAL (new_friend
) = error_mark_node
;
8117 /* Inside pushdecl_namespace_level, we will push into the
8118 current namespace. However, the friend function should go
8119 into the namespace of the template. */
8120 ns
= decl_namespace_context (new_friend
);
8121 push_nested_namespace (ns
);
8122 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8123 pop_nested_namespace (ns
);
8125 if (old_decl
== error_mark_node
)
8126 return error_mark_node
;
8128 if (old_decl
!= new_friend
)
8130 /* This new friend declaration matched an existing
8131 declaration. For example, given:
8133 template <class T> void f(T);
8134 template <class U> class C {
8135 template <class T> friend void f(T) {}
8138 the friend declaration actually provides the definition
8139 of `f', once C has been instantiated for some type. So,
8140 old_decl will be the out-of-class template declaration,
8141 while new_friend is the in-class definition.
8143 But, if `f' was called before this point, the
8144 instantiation of `f' will have DECL_TI_ARGS corresponding
8145 to `T' but not to `U', references to which might appear
8146 in the definition of `f'. Previously, the most general
8147 template for an instantiation of `f' was the out-of-class
8148 version; now it is the in-class version. Therefore, we
8149 run through all specialization of `f', adding to their
8150 DECL_TI_ARGS appropriately. In particular, they need a
8151 new set of outer arguments, corresponding to the
8152 arguments for this class instantiation.
8154 The same situation can arise with something like this:
8157 template <class T> class C {
8161 when `C<int>' is instantiated. Now, `f(int)' is defined
8164 if (!new_friend_is_defn
)
8165 /* On the other hand, if the in-class declaration does
8166 *not* provide a definition, then we don't want to alter
8167 existing definitions. We can just leave everything
8172 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8173 tree new_args
= TI_ARGS (new_friend_template_info
);
8175 /* Overwrite whatever template info was there before, if
8176 any, with the new template information pertaining to
8178 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8180 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8182 /* We should have called reregister_specialization in
8184 gcc_assert (retrieve_specialization (new_template
,
8188 /* Instantiate it if the global has already been used. */
8189 if (DECL_ODR_USED (old_decl
))
8190 instantiate_decl (old_decl
, /*defer_ok=*/true,
8191 /*expl_inst_class_mem_p=*/false);
8197 /* Indicate that the old function template is a partial
8199 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8200 = new_friend_result_template_info
;
8202 gcc_assert (new_template
8203 == most_general_template (new_template
));
8204 gcc_assert (new_template
!= old_decl
);
8206 /* Reassign any specializations already in the hash table
8207 to the new more general template, and add the
8208 additional template args. */
8209 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8213 tree spec
= TREE_VALUE (t
);
8216 elt
.tmpl
= old_decl
;
8217 elt
.args
= DECL_TI_ARGS (spec
);
8218 elt
.spec
= NULL_TREE
;
8220 htab_remove_elt (decl_specializations
, &elt
);
8223 = add_outermost_template_args (new_args
,
8224 DECL_TI_ARGS (spec
));
8226 register_specialization
8227 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8230 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8234 /* The information from NEW_FRIEND has been merged into OLD_DECL
8235 by duplicate_decls. */
8236 new_friend
= old_decl
;
8241 tree context
= DECL_CONTEXT (new_friend
);
8245 template <class T> class C {
8246 template <class U> friend void C1<U>::f (); // case 1
8247 friend void C2<T>::f (); // case 2
8249 we only need to make sure CONTEXT is a complete type for
8250 case 2. To distinguish between the two cases, we note that
8251 CONTEXT of case 1 remains dependent type after tsubst while
8252 this isn't true for case 2. */
8253 ++processing_template_decl
;
8254 dependent_p
= dependent_type_p (context
);
8255 --processing_template_decl
;
8258 && !complete_type_or_else (context
, NULL_TREE
))
8259 return error_mark_node
;
8261 if (COMPLETE_TYPE_P (context
))
8263 /* Check to see that the declaration is really present, and,
8264 possibly obtain an improved declaration. */
8265 tree fn
= check_classfn (context
,
8266 new_friend
, NULL_TREE
);
8276 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8277 template arguments, as for tsubst.
8279 Returns an appropriate tsubst'd friend type or error_mark_node on
8283 tsubst_friend_class (tree friend_tmpl
, tree args
)
8289 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8291 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8292 return TREE_TYPE (t
);
8295 context
= CP_DECL_CONTEXT (friend_tmpl
);
8297 if (context
!= global_namespace
)
8299 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8300 push_nested_namespace (context
);
8302 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8305 /* Look for a class template declaration. We look for hidden names
8306 because two friend declarations of the same template are the
8307 same. For example, in:
8310 template <typename> friend class F;
8312 template <typename> struct B {
8313 template <typename> friend class F;
8316 both F templates are the same. */
8317 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8318 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8320 /* But, if we don't find one, it might be because we're in a
8321 situation like this:
8329 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8330 for `S<int>', not the TEMPLATE_DECL. */
8331 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8333 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8334 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8337 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8339 /* The friend template has already been declared. Just
8340 check to see that the declarations match, and install any new
8341 default parameters. We must tsubst the default parameters,
8342 of course. We only need the innermost template parameters
8343 because that is all that redeclare_class_template will look
8345 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8346 > TMPL_ARGS_DEPTH (args
))
8349 location_t saved_input_location
;
8350 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8351 args
, tf_warning_or_error
);
8353 saved_input_location
= input_location
;
8354 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8355 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8356 input_location
= saved_input_location
;
8360 friend_type
= TREE_TYPE (tmpl
);
8364 /* The friend template has not already been declared. In this
8365 case, the instantiation of the template class will cause the
8366 injection of this template into the global scope. */
8367 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8368 if (tmpl
== error_mark_node
)
8369 return error_mark_node
;
8371 /* The new TMPL is not an instantiation of anything, so we
8372 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8373 the new type because that is supposed to be the corresponding
8374 template decl, i.e., TMPL. */
8375 DECL_USE_TEMPLATE (tmpl
) = 0;
8376 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8377 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8378 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8379 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8381 /* Inject this template into the global scope. */
8382 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8385 if (context
!= global_namespace
)
8387 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8388 pop_nested_namespace (context
);
8390 pop_nested_class ();
8396 /* Returns zero if TYPE cannot be completed later due to circularity.
8397 Otherwise returns one. */
8400 can_complete_type_without_circularity (tree type
)
8402 if (type
== NULL_TREE
|| type
== error_mark_node
)
8404 else if (COMPLETE_TYPE_P (type
))
8406 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8407 return can_complete_type_without_circularity (TREE_TYPE (type
));
8408 else if (CLASS_TYPE_P (type
)
8409 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8415 /* Apply any attributes which had to be deferred until instantiation
8416 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8417 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8420 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8421 tree args
, tsubst_flags_t complain
, tree in_decl
)
8423 tree last_dep
= NULL_TREE
;
8427 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8428 if (ATTR_IS_DEPENDENT (t
))
8431 attributes
= copy_list (attributes
);
8435 if (DECL_P (*decl_p
))
8437 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8439 p
= &DECL_ATTRIBUTES (*decl_p
);
8442 p
= &TYPE_ATTRIBUTES (*decl_p
);
8446 tree late_attrs
= NULL_TREE
;
8447 tree
*q
= &late_attrs
;
8449 for (*p
= attributes
; *p
; )
8452 if (ATTR_IS_DEPENDENT (t
))
8454 *p
= TREE_CHAIN (t
);
8455 TREE_CHAIN (t
) = NULL_TREE
;
8456 /* If the first attribute argument is an identifier, don't
8457 pass it through tsubst. Attributes like mode, format,
8458 cleanup and several target specific attributes expect it
8461 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8462 && TREE_VALUE (TREE_VALUE (t
))
8463 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8464 == IDENTIFIER_NODE
))
8467 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8469 /*integral_constant_expression_p=*/false);
8470 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8472 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8477 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8478 /*integral_constant_expression_p=*/false);
8480 q
= &TREE_CHAIN (t
);
8483 p
= &TREE_CHAIN (t
);
8486 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8490 /* Perform (or defer) access check for typedefs that were referenced
8491 from within the template TMPL code.
8492 This is a subroutine of instantiate_decl and instantiate_class_template.
8493 TMPL is the template to consider and TARGS is the list of arguments of
8497 perform_typedefs_access_check (tree tmpl
, tree targs
)
8499 location_t saved_location
;
8501 qualified_typedef_usage_t
*iter
;
8504 || (!CLASS_TYPE_P (tmpl
)
8505 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8508 saved_location
= input_location
;
8509 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8511 tree type_decl
= iter
->typedef_decl
;
8512 tree type_scope
= iter
->context
;
8514 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8517 if (uses_template_parms (type_decl
))
8518 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8519 if (uses_template_parms (type_scope
))
8520 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8522 /* Make access check error messages point to the location
8523 of the use of the typedef. */
8524 input_location
= iter
->locus
;
8525 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8526 type_decl
, type_decl
,
8527 tf_warning_or_error
);
8529 input_location
= saved_location
;
8533 instantiate_class_template_1 (tree type
)
8535 tree templ
, args
, pattern
, t
, member
;
8539 unsigned int saved_maximum_field_alignment
;
8542 if (type
== error_mark_node
)
8543 return error_mark_node
;
8545 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8546 || uses_template_parms (type
))
8549 /* Figure out which template is being instantiated. */
8550 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8551 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8553 /* Determine what specialization of the original template to
8555 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8556 if (t
== error_mark_node
)
8558 TYPE_BEING_DEFINED (type
) = 1;
8559 return error_mark_node
;
8563 /* This TYPE is actually an instantiation of a partial
8564 specialization. We replace the innermost set of ARGS with
8565 the arguments appropriate for substitution. For example,
8568 template <class T> struct S {};
8569 template <class T> struct S<T*> {};
8571 and supposing that we are instantiating S<int*>, ARGS will
8572 presently be {int*} -- but we need {int}. */
8573 pattern
= TREE_TYPE (t
);
8574 args
= TREE_PURPOSE (t
);
8578 pattern
= TREE_TYPE (templ
);
8579 args
= CLASSTYPE_TI_ARGS (type
);
8582 /* If the template we're instantiating is incomplete, then clearly
8583 there's nothing we can do. */
8584 if (!COMPLETE_TYPE_P (pattern
))
8587 /* If we've recursively instantiated too many templates, stop. */
8588 if (! push_tinst_level (type
))
8591 /* Now we're really doing the instantiation. Mark the type as in
8592 the process of being defined. */
8593 TYPE_BEING_DEFINED (type
) = 1;
8595 /* We may be in the middle of deferred access check. Disable
8597 push_deferring_access_checks (dk_no_deferred
);
8599 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8601 push_to_top_level ();
8602 /* Use #pragma pack from the template context. */
8603 saved_maximum_field_alignment
= maximum_field_alignment
;
8604 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8606 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8608 /* Set the input location to the most specialized template definition.
8609 This is needed if tsubsting causes an error. */
8610 typedecl
= TYPE_MAIN_DECL (pattern
);
8611 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8612 DECL_SOURCE_LOCATION (typedecl
);
8614 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8615 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8616 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8617 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8618 if (ANON_AGGR_TYPE_P (pattern
))
8619 SET_ANON_AGGR_TYPE_P (type
);
8620 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8622 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8623 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8624 /* Adjust visibility for template arguments. */
8625 determine_visibility (TYPE_MAIN_DECL (type
));
8627 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8629 pbinfo
= TYPE_BINFO (pattern
);
8631 /* We should never instantiate a nested class before its enclosing
8632 class; we need to look up the nested class by name before we can
8633 instantiate it, and that lookup should instantiate the enclosing
8635 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8636 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8638 base_list
= NULL_TREE
;
8639 if (BINFO_N_BASE_BINFOS (pbinfo
))
8645 /* We must enter the scope containing the type, as that is where
8646 the accessibility of types named in dependent bases are
8648 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8650 /* Substitute into each of the bases to determine the actual
8652 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8655 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8656 tree expanded_bases
= NULL_TREE
;
8659 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8662 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8663 args
, tf_error
, NULL_TREE
);
8664 if (expanded_bases
== error_mark_node
)
8667 len
= TREE_VEC_LENGTH (expanded_bases
);
8670 for (idx
= 0; idx
< len
; idx
++)
8673 /* Extract the already-expanded base class. */
8674 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8676 /* Substitute to figure out the base class. */
8677 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8680 if (base
== error_mark_node
)
8683 base_list
= tree_cons (access
, base
, base_list
);
8684 if (BINFO_VIRTUAL_P (pbase_binfo
))
8685 TREE_TYPE (base_list
) = integer_type_node
;
8689 /* The list is now in reverse order; correct that. */
8690 base_list
= nreverse (base_list
);
8693 pop_scope (pushed_scope
);
8695 /* Now call xref_basetypes to set up all the base-class
8697 xref_basetypes (type
, base_list
);
8699 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8700 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8701 args
, tf_error
, NULL_TREE
);
8702 fixup_attribute_variants (type
);
8704 /* Now that our base classes are set up, enter the scope of the
8705 class, so that name lookups into base classes, etc. will work
8706 correctly. This is precisely analogous to what we do in
8707 begin_class_definition when defining an ordinary non-template
8708 class, except we also need to push the enclosing classes. */
8709 push_nested_class (type
);
8711 /* Now members are processed in the order of declaration. */
8712 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8713 member
; member
= TREE_CHAIN (member
))
8715 tree t
= TREE_VALUE (member
);
8717 if (TREE_PURPOSE (member
))
8721 /* Build new CLASSTYPE_NESTED_UTDS. */
8724 bool class_template_p
;
8726 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8727 && TYPE_LANG_SPECIFIC (t
)
8728 && CLASSTYPE_IS_TEMPLATE (t
));
8729 /* If the member is a class template, then -- even after
8730 substitution -- there may be dependent types in the
8731 template argument list for the class. We increment
8732 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8733 that function will assume that no types are dependent
8734 when outside of a template. */
8735 if (class_template_p
)
8736 ++processing_template_decl
;
8737 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8738 if (class_template_p
)
8739 --processing_template_decl
;
8740 if (newtag
== error_mark_node
)
8743 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8745 tree name
= TYPE_IDENTIFIER (t
);
8747 if (class_template_p
)
8748 /* Unfortunately, lookup_template_class sets
8749 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8750 instantiation (i.e., for the type of a member
8751 template class nested within a template class.)
8752 This behavior is required for
8753 maybe_process_partial_specialization to work
8754 correctly, but is not accurate in this case;
8755 the TAG is not an instantiation of anything.
8756 (The corresponding TEMPLATE_DECL is an
8757 instantiation, but the TYPE is not.) */
8758 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8760 /* Now, we call pushtag to put this NEWTAG into the scope of
8761 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8762 pushtag calling push_template_decl. We don't have to do
8763 this for enums because it will already have been done in
8766 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8767 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8770 else if (TREE_CODE (t
) == FUNCTION_DECL
8771 || DECL_FUNCTION_TEMPLATE_P (t
))
8773 /* Build new TYPE_METHODS. */
8776 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8777 ++processing_template_decl
;
8778 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8779 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8780 --processing_template_decl
;
8781 set_current_access_from_decl (r
);
8782 finish_member_declaration (r
);
8783 /* Instantiate members marked with attribute used. */
8784 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8789 /* Build new TYPE_FIELDS. */
8790 if (TREE_CODE (t
) == STATIC_ASSERT
)
8794 ++c_inhibit_evaluation_warnings
;
8796 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8797 tf_warning_or_error
, NULL_TREE
,
8798 /*integral_constant_expression_p=*/true);
8799 --c_inhibit_evaluation_warnings
;
8801 finish_static_assert (condition
,
8802 STATIC_ASSERT_MESSAGE (t
),
8803 STATIC_ASSERT_SOURCE_LOCATION (t
),
8806 else if (TREE_CODE (t
) != CONST_DECL
)
8810 /* The file and line for this declaration, to
8811 assist in error message reporting. Since we
8812 called push_tinst_level above, we don't need to
8814 input_location
= DECL_SOURCE_LOCATION (t
);
8816 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8817 ++processing_template_decl
;
8818 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8819 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8820 --processing_template_decl
;
8821 if (TREE_CODE (r
) == VAR_DECL
)
8825 [t]he initialization (and any associated
8826 side-effects) of a static data member does
8827 not occur unless the static data member is
8828 itself used in a way that requires the
8829 definition of the static data member to
8832 Therefore, we do not substitute into the
8833 initialized for the static data member here. */
8834 finish_static_data_member_decl
8837 /*init_const_expr_p=*/false,
8838 /*asmspec_tree=*/NULL_TREE
,
8840 /* Instantiate members marked with attribute used. */
8841 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8844 else if (TREE_CODE (r
) == FIELD_DECL
)
8846 /* Determine whether R has a valid type and can be
8847 completed later. If R is invalid, then it is
8848 replaced by error_mark_node so that it will not be
8849 added to TYPE_FIELDS. */
8850 tree rtype
= TREE_TYPE (r
);
8851 if (can_complete_type_without_circularity (rtype
))
8852 complete_type (rtype
);
8854 if (!COMPLETE_TYPE_P (rtype
))
8856 cxx_incomplete_type_error (r
, rtype
);
8857 r
= error_mark_node
;
8861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8862 such a thing will already have been added to the field
8863 list by tsubst_enum in finish_member_declaration in the
8864 CLASSTYPE_NESTED_UTDS case above. */
8865 if (!(TREE_CODE (r
) == TYPE_DECL
8866 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8867 && DECL_ARTIFICIAL (r
)))
8869 set_current_access_from_decl (r
);
8870 finish_member_declaration (r
);
8877 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8878 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8880 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8882 tree friend_type
= t
;
8883 bool adjust_processing_template_decl
= false;
8885 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8887 /* template <class T> friend class C; */
8888 friend_type
= tsubst_friend_class (friend_type
, args
);
8889 adjust_processing_template_decl
= true;
8891 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8893 /* template <class T> friend class C::D; */
8894 friend_type
= tsubst (friend_type
, args
,
8895 tf_warning_or_error
, NULL_TREE
);
8896 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8897 friend_type
= TREE_TYPE (friend_type
);
8898 adjust_processing_template_decl
= true;
8900 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8901 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8903 /* This could be either
8907 when dependent_type_p is false or
8909 template <class U> friend class T::C;
8912 friend_type
= tsubst (friend_type
, args
,
8913 tf_warning_or_error
, NULL_TREE
);
8914 /* Bump processing_template_decl for correct
8915 dependent_type_p calculation. */
8916 ++processing_template_decl
;
8917 if (dependent_type_p (friend_type
))
8918 adjust_processing_template_decl
= true;
8919 --processing_template_decl
;
8921 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8922 && hidden_name_p (TYPE_NAME (friend_type
)))
8926 where C hasn't been declared yet. Let's lookup name
8927 from namespace scope directly, bypassing any name that
8928 come from dependent base class. */
8929 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8931 /* The call to xref_tag_from_type does injection for friend
8933 push_nested_namespace (ns
);
8935 xref_tag_from_type (friend_type
, NULL_TREE
,
8936 /*tag_scope=*/ts_current
);
8937 pop_nested_namespace (ns
);
8939 else if (uses_template_parms (friend_type
))
8940 /* friend class C<T>; */
8941 friend_type
= tsubst (friend_type
, args
,
8942 tf_warning_or_error
, NULL_TREE
);
8947 where C is already declared or
8949 friend class C<int>;
8951 We don't have to do anything in these cases. */
8953 if (adjust_processing_template_decl
)
8954 /* Trick make_friend_class into realizing that the friend
8955 we're adding is a template, not an ordinary class. It's
8956 important that we use make_friend_class since it will
8957 perform some error-checking and output cross-reference
8959 ++processing_template_decl
;
8961 if (friend_type
!= error_mark_node
)
8962 make_friend_class (type
, friend_type
, /*complain=*/false);
8964 if (adjust_processing_template_decl
)
8965 --processing_template_decl
;
8969 /* Build new DECL_FRIENDLIST. */
8972 /* The file and line for this declaration, to
8973 assist in error message reporting. Since we
8974 called push_tinst_level above, we don't need to
8976 input_location
= DECL_SOURCE_LOCATION (t
);
8978 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8980 ++processing_template_decl
;
8981 push_deferring_access_checks (dk_no_check
);
8984 r
= tsubst_friend_function (t
, args
);
8985 add_friend (type
, r
, /*complain=*/false);
8986 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8988 pop_deferring_access_checks ();
8989 --processing_template_decl
;
8995 if (CLASSTYPE_LAMBDA_EXPR (type
))
8997 tree decl
= lambda_function (type
);
9000 instantiate_decl (decl
, false, false);
9001 maybe_add_lambda_conv_op (type
);
9004 gcc_assert (errorcount
);
9007 /* Set the file and line number information to whatever is given for
9008 the class itself. This puts error messages involving generated
9009 implicit functions at a predictable point, and the same point
9010 that would be used for non-template classes. */
9011 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9013 unreverse_member_declarations (type
);
9014 finish_struct_1 (type
);
9015 TYPE_BEING_DEFINED (type
) = 0;
9017 /* We don't instantiate default arguments for member functions. 14.7.1:
9019 The implicit instantiation of a class template specialization causes
9020 the implicit instantiation of the declarations, but not of the
9021 definitions or default arguments, of the class member functions,
9022 member classes, static data members and member templates.... */
9024 /* Some typedefs referenced from within the template code need to be access
9025 checked at template instantiation time, i.e now. These types were
9026 added to the template at parsing time. Let's get those and perform
9027 the access checks then. */
9028 perform_typedefs_access_check (pattern
, args
);
9029 perform_deferred_access_checks (tf_warning_or_error
);
9030 pop_nested_class ();
9031 maximum_field_alignment
= saved_maximum_field_alignment
;
9033 pop_from_top_level ();
9034 pop_deferring_access_checks ();
9037 /* The vtable for a template class can be emitted in any translation
9038 unit in which the class is instantiated. When there is no key
9039 method, however, finish_struct_1 will already have added TYPE to
9040 the keyed_classes list. */
9041 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9042 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9047 /* Wrapper for instantiate_class_template_1. */
9050 instantiate_class_template (tree type
)
9053 timevar_push (TV_TEMPLATE_INST
);
9054 ret
= instantiate_class_template_1 (type
);
9055 timevar_pop (TV_TEMPLATE_INST
);
9060 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9066 else if (TYPE_P (t
))
9067 r
= tsubst (t
, args
, complain
, in_decl
);
9070 if (!(complain
& tf_warning
))
9071 ++c_inhibit_evaluation_warnings
;
9072 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9073 /*integral_constant_expression_p=*/true);
9074 if (!(complain
& tf_warning
))
9075 --c_inhibit_evaluation_warnings
;
9076 /* Preserve the raw-reference nature of T. */
9077 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9078 && REFERENCE_REF_P (r
))
9079 r
= TREE_OPERAND (r
, 0);
9084 /* Given a function parameter pack TMPL_PARM and some function parameters
9085 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9086 and set *SPEC_P to point at the next point in the list. */
9089 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9091 /* Collect all of the extra "packed" parameters into an
9095 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9096 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9097 tree spec_parm
= *spec_p
;
9100 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9102 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9105 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9106 parmvec
= make_tree_vec (len
);
9107 parmtypevec
= make_tree_vec (len
);
9108 spec_parm
= *spec_p
;
9109 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9111 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9112 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9115 /* Build the argument packs. */
9116 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9117 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9118 TREE_TYPE (argpack
) = argtypepack
;
9119 *spec_p
= spec_parm
;
9124 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9125 NONTYPE_ARGUMENT_PACK. */
9128 make_fnparm_pack (tree spec_parm
)
9130 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9133 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9137 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9139 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9140 if (i
>= TREE_VEC_LENGTH (vec
))
9142 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9149 make_argument_pack_select (tree arg_pack
, unsigned index
)
9151 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9153 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9154 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9159 /* This is a subroutine of tsubst_pack_expansion.
9161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9162 mechanism to store the (non complete list of) arguments of the
9163 substitution and return a non substituted pack expansion, in order
9164 to wait for when we have enough arguments to really perform the
9168 use_pack_expansion_extra_args_p (tree parm_packs
,
9172 if (parm_packs
== NULL_TREE
)
9175 bool has_expansion_arg
= false;
9176 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9178 bool has_non_expansion_arg
= false;
9179 for (tree parm_pack
= parm_packs
;
9181 parm_pack
= TREE_CHAIN (parm_pack
))
9183 tree arg
= TREE_VALUE (parm_pack
);
9185 if (argument_pack_element_is_expansion_p (arg
, i
))
9186 has_expansion_arg
= true;
9188 has_non_expansion_arg
= true;
9191 /* If one pack has an expansion and another pack has a normal
9192 argument or if one pack has an empty argument another one
9193 hasn't then tsubst_pack_expansion cannot perform the
9194 substitution and need to fall back on the
9195 PACK_EXPANSION_EXTRA mechanism. */
9196 if ((has_expansion_arg
&& has_non_expansion_arg
)
9197 || (has_empty_arg
&& (has_expansion_arg
|| has_non_expansion_arg
)))
9203 /* [temp.variadic]/6 says that:
9205 The instantiation of a pack expansion [...]
9206 produces a list E1,E2, ..., En, where N is the number of elements
9207 in the pack expansion parameters.
9209 This subroutine of tsubst_pack_expansion produces one of these Ei.
9211 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9212 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9213 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9214 INDEX is the index 'i' of the element Ei to produce. ARGS,
9215 COMPLAIN, and IN_DECL are the same parameters as for the
9216 tsubst_pack_expansion function.
9218 The function returns the resulting Ei upon successful completion,
9221 Note that this function possibly modifies the ARGS parameter, so
9222 it's the responsibility of the caller to restore it. */
9225 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9228 tree args
/* This parm gets
9230 tsubst_flags_t complain
,
9234 bool ith_elem_is_expansion
= false;
9236 /* For each parameter pack, change the substitution of the parameter
9237 pack to the ith argument in its argument pack, then expand the
9239 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9241 tree parm
= TREE_PURPOSE (pack
);
9242 tree arg_pack
= TREE_VALUE (pack
);
9243 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9245 ith_elem_is_expansion
|=
9246 argument_pack_element_is_expansion_p (arg_pack
, index
);
9248 /* Select the Ith argument from the pack. */
9249 if (TREE_CODE (parm
) == PARM_DECL
)
9253 aps
= make_argument_pack_select (arg_pack
, index
);
9255 register_local_specialization (aps
, parm
);
9258 aps
= retrieve_local_specialization (parm
);
9263 template_parm_level_and_index (parm
, &level
, &idx
);
9267 aps
= make_argument_pack_select (arg_pack
, index
);
9268 /* Update the corresponding argument. */
9269 TMPL_ARG (args
, level
, idx
) = aps
;
9272 /* Re-use the ARGUMENT_PACK_SELECT. */
9273 aps
= TMPL_ARG (args
, level
, idx
);
9275 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9278 /* Substitute into the PATTERN with the (possibly altered)
9280 if (!TYPE_P (pattern
))
9281 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9282 /*integral_constant_expression_p=*/false);
9284 t
= tsubst (pattern
, args
, complain
, in_decl
);
9286 /* If the Ith argument pack element is a pack expansion, then
9287 the Ith element resulting from the substituting is going to
9288 be a pack expansion as well. */
9289 if (ith_elem_is_expansion
)
9290 t
= make_pack_expansion (t
);
9295 /* Substitute ARGS into T, which is an pack expansion
9296 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9297 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9298 (if only a partial substitution could be performed) or
9299 ERROR_MARK_NODE if there was an error. */
9301 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9305 tree pack
, packs
= NULL_TREE
;
9306 bool unsubstituted_packs
= false;
9309 struct pointer_map_t
*saved_local_specializations
= NULL
;
9310 bool need_local_specializations
= false;
9313 gcc_assert (PACK_EXPANSION_P (t
));
9314 pattern
= PACK_EXPANSION_PATTERN (t
);
9316 /* Add in any args remembered from an earlier partial instantiation. */
9317 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9319 levels
= TMPL_ARGS_DEPTH (args
);
9321 /* Determine the argument packs that will instantiate the parameter
9322 packs used in the expansion expression. While we're at it,
9323 compute the number of arguments to be expanded and make sure it
9325 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9326 pack
= TREE_CHAIN (pack
))
9328 tree parm_pack
= TREE_VALUE (pack
);
9329 tree arg_pack
= NULL_TREE
;
9330 tree orig_arg
= NULL_TREE
;
9333 if (TREE_CODE (parm_pack
) == BASES
)
9335 if (BASES_DIRECT (parm_pack
))
9336 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9337 args
, complain
, in_decl
, false));
9339 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9340 args
, complain
, in_decl
, false));
9342 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9344 if (PACK_EXPANSION_LOCAL_P (t
))
9345 arg_pack
= retrieve_local_specialization (parm_pack
);
9348 /* We can't rely on local_specializations for a parameter
9349 name used later in a function declaration (such as in a
9350 late-specified return type). Even if it exists, it might
9351 have the wrong value for a recursive call. Just make a
9352 dummy decl, since it's only used for its type. */
9353 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9354 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9355 /* Partial instantiation of the parm_pack, we can't build
9356 up an argument pack yet. */
9357 arg_pack
= NULL_TREE
;
9359 arg_pack
= make_fnparm_pack (arg_pack
);
9360 need_local_specializations
= true;
9366 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9368 if (level
<= levels
)
9369 arg_pack
= TMPL_ARG (args
, level
, idx
);
9372 orig_arg
= arg_pack
;
9373 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9374 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9376 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9377 /* This can only happen if we forget to expand an argument
9378 pack somewhere else. Just return an error, silently. */
9380 result
= make_tree_vec (1);
9381 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9388 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9390 /* Don't bother trying to do a partial substitution with
9391 incomplete packs; we'll try again after deduction. */
9392 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9397 else if (len
!= my_len
)
9399 if (!(complain
& tf_error
))
9400 /* Fail quietly. */;
9401 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9402 error ("mismatched argument pack lengths while expanding "
9406 error ("mismatched argument pack lengths while expanding "
9409 return error_mark_node
;
9412 /* Keep track of the parameter packs and their corresponding
9414 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9415 TREE_TYPE (packs
) = orig_arg
;
9419 /* We can't substitute for this parameter pack. We use a flag as
9420 well as the missing_level counter because function parameter
9421 packs don't have a level. */
9422 unsubstituted_packs
= true;
9426 /* We cannot expand this expansion expression, because we don't have
9427 all of the argument packs we need. */
9428 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9430 /* We got some full packs, but we can't substitute them in until we
9431 have values for all the packs. So remember these until then. */
9433 t
= make_pack_expansion (pattern
);
9434 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9437 else if (unsubstituted_packs
)
9439 /* There were no real arguments, we're just replacing a parameter
9440 pack with another version of itself. Substitute into the
9441 pattern and return a PACK_EXPANSION_*. The caller will need to
9443 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9444 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9445 /*integral_constant_expression_p=*/false);
9447 t
= tsubst (pattern
, args
, complain
, in_decl
);
9448 t
= make_pack_expansion (t
);
9452 gcc_assert (len
>= 0);
9454 if (need_local_specializations
)
9456 /* We're in a late-specified return type, so create our own local
9457 specializations map; the current map is either NULL or (in the
9458 case of recursive unification) might have bindings that we don't
9459 want to use or alter. */
9460 saved_local_specializations
= local_specializations
;
9461 local_specializations
= pointer_map_create ();
9464 /* For each argument in each argument pack, substitute into the
9466 result
= make_tree_vec (len
);
9467 for (i
= 0; i
< len
; ++i
)
9469 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9473 TREE_VEC_ELT (result
, i
) = t
;
9474 if (t
== error_mark_node
)
9476 result
= error_mark_node
;
9481 /* Update ARGS to restore the substitution from parameter packs to
9482 their argument packs. */
9483 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9485 tree parm
= TREE_PURPOSE (pack
);
9487 if (TREE_CODE (parm
) == PARM_DECL
)
9488 register_local_specialization (TREE_TYPE (pack
), parm
);
9493 if (TREE_VALUE (pack
) == NULL_TREE
)
9496 template_parm_level_and_index (parm
, &level
, &idx
);
9498 /* Update the corresponding argument. */
9499 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9500 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9503 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9507 if (need_local_specializations
)
9509 pointer_map_destroy (local_specializations
);
9510 local_specializations
= saved_local_specializations
;
9516 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9517 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9518 parameter packs; all parms generated from a function parameter pack will
9519 have the same DECL_PARM_INDEX. */
9522 get_pattern_parm (tree parm
, tree tmpl
)
9524 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9527 if (DECL_ARTIFICIAL (parm
))
9529 for (patparm
= DECL_ARGUMENTS (pattern
);
9530 patparm
; patparm
= DECL_CHAIN (patparm
))
9531 if (DECL_ARTIFICIAL (patparm
)
9532 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9537 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9538 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9539 gcc_assert (DECL_PARM_INDEX (patparm
)
9540 == DECL_PARM_INDEX (parm
));
9546 /* Substitute ARGS into the vector or list of template arguments T. */
9549 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9552 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9555 if (t
== error_mark_node
)
9556 return error_mark_node
;
9558 len
= TREE_VEC_LENGTH (t
);
9559 elts
= XALLOCAVEC (tree
, len
);
9561 for (i
= 0; i
< len
; i
++)
9563 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9566 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9567 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9568 else if (PACK_EXPANSION_P (orig_arg
))
9570 /* Substitute into an expansion expression. */
9571 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9573 if (TREE_CODE (new_arg
) == TREE_VEC
)
9574 /* Add to the expanded length adjustment the number of
9575 expanded arguments. We subtract one from this
9576 measurement, because the argument pack expression
9577 itself is already counted as 1 in
9578 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9579 the argument pack is empty. */
9580 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9582 else if (ARGUMENT_PACK_P (orig_arg
))
9584 /* Substitute into each of the arguments. */
9585 new_arg
= TYPE_P (orig_arg
)
9586 ? cxx_make_type (TREE_CODE (orig_arg
))
9587 : make_node (TREE_CODE (orig_arg
));
9589 SET_ARGUMENT_PACK_ARGS (
9591 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9592 args
, complain
, in_decl
));
9594 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9595 new_arg
= error_mark_node
;
9597 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9598 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9600 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9602 if (TREE_TYPE (new_arg
) == error_mark_node
)
9603 new_arg
= error_mark_node
;
9607 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9609 if (new_arg
== error_mark_node
)
9610 return error_mark_node
;
9613 if (new_arg
!= orig_arg
)
9620 /* Make space for the expanded arguments coming from template
9622 t
= make_tree_vec (len
+ expanded_len_adjust
);
9623 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9624 arguments for a member template.
9625 In that case each TREE_VEC in ORIG_T represents a level of template
9626 arguments, and ORIG_T won't carry any non defaulted argument count.
9627 It will rather be the nested TREE_VECs that will carry one.
9628 In other words, ORIG_T carries a non defaulted argument count only
9629 if it doesn't contain any nested TREE_VEC. */
9630 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9632 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9633 count
+= expanded_len_adjust
;
9634 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9636 for (i
= 0, out
= 0; i
< len
; i
++)
9638 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9639 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9640 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9644 /* Now expand the template argument pack "in place". */
9645 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9646 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9650 TREE_VEC_ELT (t
, out
) = elts
[i
];
9658 /* Return the result of substituting ARGS into the template parameters
9659 given by PARMS. If there are m levels of ARGS and m + n levels of
9660 PARMS, then the result will contain n levels of PARMS. For
9661 example, if PARMS is `template <class T> template <class U>
9662 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9663 result will be `template <int*, double, class V>'. */
9666 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9671 /* When substituting into a template, we must set
9672 PROCESSING_TEMPLATE_DECL as the template parameters may be
9673 dependent if they are based on one-another, and the dependency
9674 predicates are short-circuit outside of templates. */
9675 ++processing_template_decl
;
9677 for (new_parms
= &r
;
9678 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9679 new_parms
= &(TREE_CHAIN (*new_parms
)),
9680 parms
= TREE_CHAIN (parms
))
9683 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9686 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9690 if (parms
== error_mark_node
)
9693 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9695 if (tuple
== error_mark_node
)
9698 TREE_VEC_ELT (new_vec
, i
) =
9699 tsubst_template_parm (tuple
, args
, complain
);
9703 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9704 - TMPL_ARGS_DEPTH (args
)),
9705 new_vec
, NULL_TREE
);
9708 --processing_template_decl
;
9713 /* Return the result of substituting ARGS into one template parameter
9714 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9715 parameter and which TREE_PURPOSE is the default argument of the
9716 template parameter. */
9719 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9721 tree default_value
, parm_decl
;
9723 if (args
== NULL_TREE
9725 || t
== error_mark_node
)
9728 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9730 default_value
= TREE_PURPOSE (t
);
9731 parm_decl
= TREE_VALUE (t
);
9733 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9734 if (TREE_CODE (parm_decl
) == PARM_DECL
9735 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9736 parm_decl
= error_mark_node
;
9737 default_value
= tsubst_template_arg (default_value
, args
,
9738 complain
, NULL_TREE
);
9740 return build_tree_list (default_value
, parm_decl
);
9743 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9744 type T. If T is not an aggregate or enumeration type, it is
9745 handled as if by tsubst. IN_DECL is as for tsubst. If
9746 ENTERING_SCOPE is nonzero, T is the context for a template which
9747 we are presently tsubst'ing. Return the substituted value. */
9750 tsubst_aggr_type (tree t
,
9752 tsubst_flags_t complain
,
9759 switch (TREE_CODE (t
))
9762 if (TYPE_PTRMEMFUNC_P (t
))
9763 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9765 /* Else fall through. */
9768 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9773 int saved_unevaluated_operand
;
9774 int saved_inhibit_evaluation_warnings
;
9776 /* In "sizeof(X<I>)" we need to evaluate "I". */
9777 saved_unevaluated_operand
= cp_unevaluated_operand
;
9778 cp_unevaluated_operand
= 0;
9779 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9780 c_inhibit_evaluation_warnings
= 0;
9782 /* First, determine the context for the type we are looking
9784 context
= TYPE_CONTEXT (t
);
9785 if (context
&& TYPE_P (context
))
9787 context
= tsubst_aggr_type (context
, args
, complain
,
9788 in_decl
, /*entering_scope=*/1);
9789 /* If context is a nested class inside a class template,
9790 it may still need to be instantiated (c++/33959). */
9791 context
= complete_type (context
);
9794 /* Then, figure out what arguments are appropriate for the
9795 type we are trying to find. For example, given:
9797 template <class T> struct S;
9798 template <class T, class U> void f(T, U) { S<U> su; }
9800 and supposing that we are instantiating f<int, double>,
9801 then our ARGS will be {int, double}, but, when looking up
9802 S we only want {double}. */
9803 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9805 if (argvec
== error_mark_node
)
9806 r
= error_mark_node
;
9809 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9810 entering_scope
, complain
);
9811 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9814 cp_unevaluated_operand
= saved_unevaluated_operand
;
9815 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9820 /* This is not a template type, so there's nothing to do. */
9824 return tsubst (t
, args
, complain
, in_decl
);
9828 /* Substitute into the default argument ARG (a default argument for
9829 FN), which has the indicated TYPE. */
9832 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9834 tree saved_class_ptr
= NULL_TREE
;
9835 tree saved_class_ref
= NULL_TREE
;
9836 int errs
= errorcount
+ sorrycount
;
9838 /* This can happen in invalid code. */
9839 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9842 /* This default argument came from a template. Instantiate the
9843 default argument here, not in tsubst. In the case of
9852 we must be careful to do name lookup in the scope of S<T>,
9853 rather than in the current class. */
9854 push_access_scope (fn
);
9855 /* The "this" pointer is not valid in a default argument. */
9858 saved_class_ptr
= current_class_ptr
;
9859 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9860 saved_class_ref
= current_class_ref
;
9861 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9864 push_deferring_access_checks(dk_no_deferred
);
9865 /* The default argument expression may cause implicitly defined
9866 member functions to be synthesized, which will result in garbage
9867 collection. We must treat this situation as if we were within
9868 the body of function so as to avoid collecting live data on the
9871 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9872 tf_warning_or_error
, NULL_TREE
,
9873 /*integral_constant_expression_p=*/false);
9875 pop_deferring_access_checks();
9877 /* Restore the "this" pointer. */
9880 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9881 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9884 if (errorcount
+sorrycount
> errs
)
9885 inform (input_location
,
9886 " when instantiating default argument for call to %D", fn
);
9888 /* Make sure the default argument is reasonable. */
9889 arg
= check_default_argument (type
, arg
);
9891 pop_access_scope (fn
);
9896 /* Substitute into all the default arguments for FN. */
9899 tsubst_default_arguments (tree fn
)
9904 tmpl_args
= DECL_TI_ARGS (fn
);
9906 /* If this function is not yet instantiated, we certainly don't need
9907 its default arguments. */
9908 if (uses_template_parms (tmpl_args
))
9910 /* Don't do this again for clones. */
9911 if (DECL_CLONED_FUNCTION_P (fn
))
9914 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9916 arg
= TREE_CHAIN (arg
))
9917 if (TREE_PURPOSE (arg
))
9918 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9920 TREE_PURPOSE (arg
));
9923 /* Substitute the ARGS into the T, which is a _DECL. Return the
9924 result of the substitution. Issue error and warning messages under
9925 control of COMPLAIN. */
9928 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9930 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9931 location_t saved_loc
;
9936 /* Set the filename and linenumber to improve error-reporting. */
9937 saved_loc
= input_location
;
9938 input_location
= DECL_SOURCE_LOCATION (t
);
9940 switch (TREE_CODE (t
))
9944 /* We can get here when processing a member function template,
9945 member class template, or template template parameter. */
9946 tree decl
= DECL_TEMPLATE_RESULT (t
);
9951 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9953 /* Template template parameter is treated here. */
9954 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9955 if (new_type
== error_mark_node
)
9956 RETURN (error_mark_node
);
9957 /* If we get a real template back, return it. This can happen in
9958 the context of most_specialized_class. */
9959 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
9963 DECL_CHAIN (r
) = NULL_TREE
;
9964 TREE_TYPE (r
) = new_type
;
9965 DECL_TEMPLATE_RESULT (r
)
9966 = build_decl (DECL_SOURCE_LOCATION (decl
),
9967 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9968 DECL_TEMPLATE_PARMS (r
)
9969 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9971 TYPE_NAME (new_type
) = r
;
9975 /* We might already have an instance of this template.
9976 The ARGS are for the surrounding class type, so the
9977 full args contain the tsubst'd args for the context,
9978 plus the innermost args from the template decl. */
9979 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9980 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9981 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9982 /* Because this is a template, the arguments will still be
9983 dependent, even after substitution. If
9984 PROCESSING_TEMPLATE_DECL is not set, the dependency
9985 predicates will short-circuit. */
9986 ++processing_template_decl
;
9987 full_args
= tsubst_template_args (tmpl_args
, args
,
9989 --processing_template_decl
;
9990 if (full_args
== error_mark_node
)
9991 RETURN (error_mark_node
);
9993 /* If this is a default template template argument,
9994 tsubst might not have changed anything. */
9995 if (full_args
== tmpl_args
)
9998 hash
= hash_tmpl_and_args (t
, full_args
);
9999 spec
= retrieve_specialization (t
, full_args
, hash
);
10000 if (spec
!= NULL_TREE
)
10006 /* Make a new template decl. It will be similar to the
10007 original, but will record the current template arguments.
10008 We also create a new function declaration, which is just
10009 like the old one, but points to this new template, rather
10010 than the old one. */
10012 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10013 DECL_CHAIN (r
) = NULL_TREE
;
10015 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10017 if (TREE_CODE (decl
) == TYPE_DECL
10018 && !TYPE_DECL_ALIAS_P (decl
))
10021 ++processing_template_decl
;
10022 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10023 --processing_template_decl
;
10024 if (new_type
== error_mark_node
)
10025 RETURN (error_mark_node
);
10027 TREE_TYPE (r
) = new_type
;
10028 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10029 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10030 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10031 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10036 ++processing_template_decl
;
10037 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10038 --processing_template_decl
;
10039 if (new_decl
== error_mark_node
)
10040 RETURN (error_mark_node
);
10042 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10043 DECL_TI_TEMPLATE (new_decl
) = r
;
10044 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10045 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10046 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10049 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10050 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10051 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10053 /* The template parameters for this new template are all the
10054 template parameters for the old template, except the
10055 outermost level of parameters. */
10056 DECL_TEMPLATE_PARMS (r
)
10057 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10060 if (PRIMARY_TEMPLATE_P (t
))
10061 DECL_PRIMARY_TEMPLATE (r
) = r
;
10063 if (TREE_CODE (decl
) != TYPE_DECL
)
10064 /* Record this non-type partial instantiation. */
10065 register_specialization (r
, t
,
10066 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10071 case FUNCTION_DECL
:
10074 tree argvec
= NULL_TREE
;
10082 /* Nobody should be tsubst'ing into non-template functions. */
10083 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10085 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10090 /* If T is not dependent, just return it. We have to
10091 increment PROCESSING_TEMPLATE_DECL because
10092 value_dependent_expression_p assumes that nothing is
10093 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10094 ++processing_template_decl
;
10095 dependent_p
= value_dependent_expression_p (t
);
10096 --processing_template_decl
;
10100 /* Calculate the most general template of which R is a
10101 specialization, and the complete set of arguments used to
10103 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10104 argvec
= tsubst_template_args (DECL_TI_ARGS
10105 (DECL_TEMPLATE_RESULT
10106 (DECL_TI_TEMPLATE (t
))),
10107 args
, complain
, in_decl
);
10108 if (argvec
== error_mark_node
)
10109 RETURN (error_mark_node
);
10111 /* Check to see if we already have this specialization. */
10112 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10113 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10121 /* We can see more levels of arguments than parameters if
10122 there was a specialization of a member template, like
10125 template <class T> struct S { template <class U> void f(); }
10126 template <> template <class U> void S<int>::f(U);
10128 Here, we'll be substituting into the specialization,
10129 because that's where we can find the code we actually
10130 want to generate, but we'll have enough arguments for
10131 the most general template.
10133 We also deal with the peculiar case:
10135 template <class T> struct S {
10136 template <class U> friend void f();
10138 template <class U> void f() {}
10140 template void f<double>();
10142 Here, the ARGS for the instantiation of will be {int,
10143 double}. But, we only need as many ARGS as there are
10144 levels of template parameters in CODE_PATTERN. We are
10145 careful not to get fooled into reducing the ARGS in
10148 template <class T> struct S { template <class U> void f(U); }
10149 template <class T> template <> void S<T>::f(int) {}
10151 which we can spot because the pattern will be a
10152 specialization in this case. */
10153 args_depth
= TMPL_ARGS_DEPTH (args
);
10155 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10156 if (args_depth
> parms_depth
10157 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10158 args
= get_innermost_template_args (args
, parms_depth
);
10162 /* This special case arises when we have something like this:
10164 template <class T> struct S {
10165 friend void f<int>(int, double);
10168 Here, the DECL_TI_TEMPLATE for the friend declaration
10169 will be an IDENTIFIER_NODE. We are being called from
10170 tsubst_friend_function, and we want only to create a
10171 new decl (R) with appropriate types so that we can call
10172 determine_specialization. */
10173 gen_tmpl
= NULL_TREE
;
10176 if (DECL_CLASS_SCOPE_P (t
))
10178 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10182 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10183 complain
, t
, /*entering_scope=*/1);
10188 ctx
= DECL_CONTEXT (t
);
10190 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10191 if (type
== error_mark_node
)
10192 RETURN (error_mark_node
);
10194 /* If we hit excessive deduction depth, the type is bogus even if
10195 it isn't error_mark_node, so don't build a decl. */
10196 if (excessive_deduction_depth
)
10197 RETURN (error_mark_node
);
10199 /* We do NOT check for matching decls pushed separately at this
10200 point, as they may not represent instantiations of this
10201 template, and in any case are considered separate under the
10204 DECL_USE_TEMPLATE (r
) = 0;
10205 TREE_TYPE (r
) = type
;
10206 /* Clear out the mangled name and RTL for the instantiation. */
10207 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10208 SET_DECL_RTL (r
, NULL
);
10209 /* Leave DECL_INITIAL set on deleted instantiations. */
10210 if (!DECL_DELETED_FN (r
))
10211 DECL_INITIAL (r
) = NULL_TREE
;
10212 DECL_CONTEXT (r
) = ctx
;
10214 if (member
&& DECL_CONV_FN_P (r
))
10215 /* Type-conversion operator. Reconstruct the name, in
10216 case it's the name of one of the template's parameters. */
10217 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10219 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10221 DECL_RESULT (r
) = NULL_TREE
;
10223 TREE_STATIC (r
) = 0;
10224 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10225 DECL_EXTERNAL (r
) = 1;
10226 /* If this is an instantiation of a function with internal
10227 linkage, we already know what object file linkage will be
10228 assigned to the instantiation. */
10229 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10230 DECL_DEFER_OUTPUT (r
) = 0;
10231 DECL_CHAIN (r
) = NULL_TREE
;
10232 DECL_PENDING_INLINE_INFO (r
) = 0;
10233 DECL_PENDING_INLINE_P (r
) = 0;
10234 DECL_SAVED_TREE (r
) = NULL_TREE
;
10235 DECL_STRUCT_FUNCTION (r
) = NULL
;
10237 /* We'll re-clone as appropriate in instantiate_template. */
10238 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10240 /* If we aren't complaining now, return on error before we register
10241 the specialization so that we'll complain eventually. */
10242 if ((complain
& tf_error
) == 0
10243 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10244 && !grok_op_properties (r
, /*complain=*/false))
10245 RETURN (error_mark_node
);
10247 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10248 this in the special friend case mentioned above where
10249 GEN_TMPL is NULL. */
10252 DECL_TEMPLATE_INFO (r
)
10253 = build_template_info (gen_tmpl
, argvec
);
10254 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10255 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10257 /* We're not supposed to instantiate default arguments
10258 until they are called, for a template. But, for a
10261 template <class T> void f ()
10262 { extern void g(int i = T()); }
10264 we should do the substitution when the template is
10265 instantiated. We handle the member function case in
10266 instantiate_class_template since the default arguments
10267 might refer to other members of the class. */
10269 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10270 && !uses_template_parms (argvec
))
10271 tsubst_default_arguments (r
);
10274 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10276 /* Copy the list of befriending classes. */
10277 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10279 friends
= &TREE_CHAIN (*friends
))
10281 *friends
= copy_node (*friends
);
10282 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10287 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10289 maybe_retrofit_in_chrg (r
);
10290 if (DECL_CONSTRUCTOR_P (r
))
10291 grok_ctor_properties (ctx
, r
);
10292 if (DECL_INHERITED_CTOR_BASE (r
))
10293 deduce_inheriting_ctor (r
);
10294 /* If this is an instantiation of a member template, clone it.
10295 If it isn't, that'll be handled by
10296 clone_constructors_and_destructors. */
10297 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10298 clone_function_decl (r
, /*update_method_vec_p=*/0);
10300 else if ((complain
& tf_error
) != 0
10301 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10302 && !grok_op_properties (r
, /*complain=*/true))
10303 RETURN (error_mark_node
);
10305 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10306 SET_DECL_FRIEND_CONTEXT (r
,
10307 tsubst (DECL_FRIEND_CONTEXT (t
),
10308 args
, complain
, in_decl
));
10310 /* Possibly limit visibility based on template args. */
10311 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10312 if (DECL_VISIBILITY_SPECIFIED (t
))
10314 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10315 DECL_ATTRIBUTES (r
)
10316 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10318 determine_visibility (r
);
10319 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10320 && !processing_template_decl
)
10321 defaulted_late_check (r
);
10323 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10324 args
, complain
, in_decl
);
10330 tree type
= NULL_TREE
;
10332 tree expanded_types
= NULL_TREE
;
10333 tree prev_r
= NULL_TREE
;
10334 tree first_r
= NULL_TREE
;
10336 if (FUNCTION_PARAMETER_PACK_P (t
))
10338 /* If there is a local specialization that isn't a
10339 parameter pack, it means that we're doing a "simple"
10340 substitution from inside tsubst_pack_expansion. Just
10341 return the local specialization (which will be a single
10343 tree spec
= retrieve_local_specialization (t
);
10345 && TREE_CODE (spec
) == PARM_DECL
10346 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10349 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10350 the parameters in this function parameter pack. */
10351 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10352 complain
, in_decl
);
10353 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10355 len
= TREE_VEC_LENGTH (expanded_types
);
10357 /* Zero-length parameter packs are boring. Just substitute
10360 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10365 /* All we did was update the type. Make a note of that. */
10366 type
= expanded_types
;
10367 expanded_types
= NULL_TREE
;
10371 /* Loop through all of the parameter's we'll build. When T is
10372 a function parameter pack, LEN is the number of expanded
10373 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10375 for (i
= 0; i
< len
; ++i
)
10379 if (DECL_TEMPLATE_PARM_P (t
))
10380 SET_DECL_TEMPLATE_PARM_P (r
);
10382 if (expanded_types
)
10383 /* We're on the Ith parameter of the function parameter
10386 /* An argument of a function parameter pack is not a parameter
10388 FUNCTION_PARAMETER_PACK_P (r
) = false;
10390 /* Get the Ith type. */
10391 type
= TREE_VEC_ELT (expanded_types
, i
);
10393 /* Rename the parameter to include the index. */
10395 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10398 /* We're dealing with a normal parameter. */
10399 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10401 type
= type_decays_to (type
);
10402 TREE_TYPE (r
) = type
;
10403 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10405 if (DECL_INITIAL (r
))
10407 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10408 DECL_INITIAL (r
) = TREE_TYPE (r
);
10410 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10411 complain
, in_decl
);
10414 DECL_CONTEXT (r
) = NULL_TREE
;
10416 if (!DECL_TEMPLATE_PARM_P (r
))
10417 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10419 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10420 args
, complain
, in_decl
);
10422 /* Keep track of the first new parameter we
10423 generate. That's what will be returned to the
10428 /* Build a proper chain of parameters when substituting
10429 into a function parameter pack. */
10431 DECL_CHAIN (prev_r
) = r
;
10434 /* If cp_unevaluated_operand is set, we're just looking for a
10435 single dummy parameter, so don't keep going. */
10436 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10437 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10438 complain
, DECL_CHAIN (t
));
10440 /* FIRST_R contains the start of the chain we've built. */
10450 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10451 if (type
== error_mark_node
)
10452 RETURN (error_mark_node
);
10453 TREE_TYPE (r
) = type
;
10454 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10456 if (DECL_C_BIT_FIELD (r
))
10457 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10458 non-bit-fields DECL_INITIAL is a non-static data member
10459 initializer, which gets deferred instantiation. */
10461 = tsubst_expr (DECL_INITIAL (t
), args
,
10463 /*integral_constant_expression_p=*/true);
10464 else if (DECL_INITIAL (t
))
10466 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10467 NSDMI in perform_member_init. Still set DECL_INITIAL
10468 so that we know there is one. */
10469 DECL_INITIAL (r
) = void_zero_node
;
10470 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10471 retrofit_lang_decl (r
);
10472 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10474 /* We don't have to set DECL_CONTEXT here; it is set by
10475 finish_member_declaration. */
10476 DECL_CHAIN (r
) = NULL_TREE
;
10477 if (VOID_TYPE_P (type
))
10478 error ("instantiation of %q+D as type %qT", r
, type
);
10480 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10481 args
, complain
, in_decl
);
10486 /* We reach here only for member using decls. We also need to check
10487 uses_template_parms because DECL_DEPENDENT_P is not set for a
10488 using-declaration that designates a member of the current
10489 instantiation (c++/53549). */
10490 if (DECL_DEPENDENT_P (t
)
10491 || uses_template_parms (USING_DECL_SCOPE (t
)))
10493 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10494 complain
, in_decl
);
10495 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10496 r
= do_class_using_decl (inst_scope
, name
);
10498 r
= error_mark_node
;
10501 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10502 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10508 DECL_CHAIN (r
) = NULL_TREE
;
10515 tree argvec
= NULL_TREE
;
10516 tree gen_tmpl
= NULL_TREE
;
10518 tree tmpl
= NULL_TREE
;
10520 tree type
= NULL_TREE
;
10523 if (TREE_CODE (t
) == TYPE_DECL
10524 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10526 /* If this is the canonical decl, we don't have to
10527 mess with instantiations, and often we can't (for
10528 typename, template type parms and such). Note that
10529 TYPE_NAME is not correct for the above test if
10530 we've copied the type for a typedef. */
10531 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10532 if (type
== error_mark_node
)
10533 RETURN (error_mark_node
);
10534 r
= TYPE_NAME (type
);
10538 /* Check to see if we already have the specialization we
10541 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10543 /* T is a static data member or namespace-scope entity.
10544 We have to substitute into namespace-scope variables
10545 (even though such entities are never templates) because
10548 template <class T> void f() { extern T t; }
10550 where the entity referenced is not known until
10551 instantiation time. */
10553 ctx
= DECL_CONTEXT (t
);
10554 if (DECL_CLASS_SCOPE_P (t
))
10556 ctx
= tsubst_aggr_type (ctx
, args
,
10558 in_decl
, /*entering_scope=*/1);
10559 /* If CTX is unchanged, then T is in fact the
10560 specialization we want. That situation occurs when
10561 referencing a static data member within in its own
10562 class. We can use pointer equality, rather than
10563 same_type_p, because DECL_CONTEXT is always
10565 if (ctx
== DECL_CONTEXT (t
)
10566 && (TREE_CODE (t
) != TYPE_DECL
10567 /* ... unless T is a member template; in which
10568 case our caller can be willing to create a
10569 specialization of that template represented
10571 || !(DECL_TI_TEMPLATE (t
)
10572 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10578 tmpl
= DECL_TI_TEMPLATE (t
);
10579 gen_tmpl
= most_general_template (tmpl
);
10580 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10581 if (argvec
== error_mark_node
)
10582 RETURN (error_mark_node
);
10583 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10584 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10589 /* A local variable. */
10591 /* Subsequent calls to pushdecl will fill this in. */
10593 spec
= retrieve_local_specialization (t
);
10595 /* If we already have the specialization we need, there is
10596 nothing more to do. */
10603 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10605 /* Just use name lookup to find a member alias for an anonymous
10606 union, but then add it to the hash table. */
10607 r
= lookup_name (DECL_NAME (t
));
10608 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10609 register_local_specialization (r
, t
);
10613 /* Create a new node for the specialization we need. */
10615 if (type
== NULL_TREE
)
10617 if (is_typedef_decl (t
))
10618 type
= DECL_ORIGINAL_TYPE (t
);
10620 type
= TREE_TYPE (t
);
10621 if (TREE_CODE (t
) == VAR_DECL
10622 && VAR_HAD_UNKNOWN_BOUND (t
)
10623 && type
!= error_mark_node
)
10624 type
= strip_array_domain (type
);
10625 type
= tsubst (type
, args
, complain
, in_decl
);
10627 if (TREE_CODE (r
) == VAR_DECL
)
10629 /* Even if the original location is out of scope, the
10630 newly substituted one is not. */
10631 DECL_DEAD_FOR_LOCAL (r
) = 0;
10632 DECL_INITIALIZED_P (r
) = 0;
10633 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10634 if (type
== error_mark_node
)
10635 RETURN (error_mark_node
);
10636 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10638 /* It may seem that this case cannot occur, since:
10643 declares a function, not a variable. However:
10646 template <typename T> void g() { T t; }
10647 template void g<f>();
10649 is an attempt to declare a variable with function
10651 error ("variable %qD has function type",
10652 /* R is not yet sufficiently initialized, so we
10653 just use its name. */
10655 RETURN (error_mark_node
);
10657 type
= complete_type (type
);
10658 /* Wait until cp_finish_decl to set this again, to handle
10659 circular dependency (template/instantiate6.C). */
10660 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10661 type
= check_var_type (DECL_NAME (r
), type
);
10663 if (DECL_HAS_VALUE_EXPR_P (t
))
10665 tree ve
= DECL_VALUE_EXPR (t
);
10666 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10667 /*constant_expression_p=*/false);
10668 if (REFERENCE_REF_P (ve
))
10670 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10671 ve
= TREE_OPERAND (ve
, 0);
10673 SET_DECL_VALUE_EXPR (r
, ve
);
10676 else if (DECL_SELF_REFERENCE_P (t
))
10677 SET_DECL_SELF_REFERENCE_P (r
);
10678 TREE_TYPE (r
) = type
;
10679 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10680 DECL_CONTEXT (r
) = ctx
;
10681 /* Clear out the mangled name and RTL for the instantiation. */
10682 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10683 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10684 SET_DECL_RTL (r
, NULL
);
10685 /* The initializer must not be expanded until it is required;
10686 see [temp.inst]. */
10687 DECL_INITIAL (r
) = NULL_TREE
;
10688 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10689 SET_DECL_RTL (r
, NULL
);
10690 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10691 if (TREE_CODE (r
) == VAR_DECL
)
10693 /* Possibly limit visibility based on template args. */
10694 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10695 if (DECL_VISIBILITY_SPECIFIED (t
))
10697 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10698 DECL_ATTRIBUTES (r
)
10699 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10701 determine_visibility (r
);
10706 /* A static data member declaration is always marked
10707 external when it is declared in-class, even if an
10708 initializer is present. We mimic the non-template
10709 processing here. */
10710 DECL_EXTERNAL (r
) = 1;
10712 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10713 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10714 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10716 else if (cp_unevaluated_operand
)
10718 /* We're substituting this var in a decltype outside of its
10719 scope, such as for a lambda return type. Don't add it to
10720 local_specializations, do perform auto deduction. */
10721 tree auto_node
= type_uses_auto (type
);
10725 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10726 /*constant_expression_p=*/false);
10727 init
= resolve_nondeduced_context (init
);
10728 TREE_TYPE (r
) = type
10729 = do_auto_deduction (type
, init
, auto_node
);
10733 register_local_specialization (r
, t
);
10735 DECL_CHAIN (r
) = NULL_TREE
;
10737 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10739 args
, complain
, in_decl
);
10741 /* Preserve a typedef that names a type. */
10742 if (is_typedef_decl (r
))
10744 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10745 set_underlying_type (r
);
10748 layout_decl (r
, 0);
10753 gcc_unreachable ();
10758 /* Restore the file and line information. */
10759 input_location
= saved_loc
;
10764 /* Substitute into the ARG_TYPES of a function type.
10765 If END is a TREE_CHAIN, leave it and any following types
10769 tsubst_arg_types (tree arg_types
,
10772 tsubst_flags_t complain
,
10775 tree remaining_arg_types
;
10776 tree type
= NULL_TREE
;
10778 tree expanded_args
= NULL_TREE
;
10781 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10784 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10785 args
, end
, complain
, in_decl
);
10786 if (remaining_arg_types
== error_mark_node
)
10787 return error_mark_node
;
10789 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10791 /* For a pack expansion, perform substitution on the
10792 entire expression. Later on, we'll handle the arguments
10794 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10795 args
, complain
, in_decl
);
10797 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10798 /* So that we'll spin through the parameters, one by one. */
10799 i
= TREE_VEC_LENGTH (expanded_args
);
10802 /* We only partially substituted into the parameter
10803 pack. Our type is TYPE_PACK_EXPANSION. */
10804 type
= expanded_args
;
10805 expanded_args
= NULL_TREE
;
10813 type
= TREE_VEC_ELT (expanded_args
, i
);
10815 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10817 if (type
== error_mark_node
)
10818 return error_mark_node
;
10819 if (VOID_TYPE_P (type
))
10821 if (complain
& tf_error
)
10823 error ("invalid parameter type %qT", type
);
10825 error ("in declaration %q+D", in_decl
);
10827 return error_mark_node
;
10830 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10831 top-level qualifiers as required. */
10832 type
= cv_unqualified (type_decays_to (type
));
10834 /* We do not substitute into default arguments here. The standard
10835 mandates that they be instantiated only when needed, which is
10836 done in build_over_call. */
10837 default_arg
= TREE_PURPOSE (arg_types
);
10839 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10841 /* We've instantiated a template before its default arguments
10842 have been parsed. This can happen for a nested template
10843 class, and is not an error unless we require the default
10844 argument in a call of this function. */
10845 remaining_arg_types
=
10846 tree_cons (default_arg
, type
, remaining_arg_types
);
10847 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
10850 remaining_arg_types
=
10851 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10854 return remaining_arg_types
;
10857 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10858 *not* handle the exception-specification for FNTYPE, because the
10859 initial substitution of explicitly provided template parameters
10860 during argument deduction forbids substitution into the
10861 exception-specification:
10865 All references in the function type of the function template to the
10866 corresponding template parameters are replaced by the specified tem-
10867 plate argument values. If a substitution in a template parameter or
10868 in the function type of the function template results in an invalid
10869 type, type deduction fails. [Note: The equivalent substitution in
10870 exception specifications is done only when the function is instanti-
10871 ated, at which point a program is ill-formed if the substitution
10872 results in an invalid type.] */
10875 tsubst_function_type (tree t
,
10877 tsubst_flags_t complain
,
10884 /* The TYPE_CONTEXT is not used for function/method types. */
10885 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10887 /* Substitute the return type. */
10888 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10889 if (return_type
== error_mark_node
)
10890 return error_mark_node
;
10891 /* The standard does not presently indicate that creation of a
10892 function type with an invalid return type is a deduction failure.
10893 However, that is clearly analogous to creating an array of "void"
10894 or a reference to a reference. This is core issue #486. */
10895 if (TREE_CODE (return_type
) == ARRAY_TYPE
10896 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10898 if (complain
& tf_error
)
10900 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10901 error ("function returning an array");
10903 error ("function returning a function");
10905 return error_mark_node
;
10908 /* Substitute the argument types. */
10909 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10910 complain
, in_decl
);
10911 if (arg_types
== error_mark_node
)
10912 return error_mark_node
;
10914 /* Construct a new type node and return it. */
10915 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10917 fntype
= build_function_type (return_type
, arg_types
);
10918 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10922 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10923 if (! MAYBE_CLASS_TYPE_P (r
))
10927 Type deduction may fail for any of the following
10930 -- Attempting to create "pointer to member of T" when T
10931 is not a class type. */
10932 if (complain
& tf_error
)
10933 error ("creating pointer to member function of non-class type %qT",
10935 return error_mark_node
;
10938 fntype
= build_method_type_directly (r
, return_type
,
10939 TREE_CHAIN (arg_types
));
10941 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10946 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10947 ARGS into that specification, and return the substituted
10948 specification. If there is no specification, return NULL_TREE. */
10951 tsubst_exception_specification (tree fntype
,
10953 tsubst_flags_t complain
,
10960 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10961 new_specs
= NULL_TREE
;
10962 if (specs
&& TREE_PURPOSE (specs
))
10964 /* A noexcept-specifier. */
10965 tree expr
= TREE_PURPOSE (specs
);
10966 if (TREE_CODE (expr
) == INTEGER_CST
)
10970 /* Defer instantiation of noexcept-specifiers to avoid
10971 excessive instantiations (c++/49107). */
10972 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10973 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10975 /* We already partially instantiated this member template,
10976 so combine the new args with the old. */
10977 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10978 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10979 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10980 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10984 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10985 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10989 new_specs
= tsubst_copy_and_build
10990 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10991 /*integral_constant_expression_p=*/true);
10992 new_specs
= build_noexcept_spec (new_specs
, complain
);
10996 if (! TREE_VALUE (specs
))
11003 tree expanded_specs
= NULL_TREE
;
11005 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11007 /* Expand the pack expansion type. */
11008 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11012 if (expanded_specs
== error_mark_node
)
11013 return error_mark_node
;
11014 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11015 len
= TREE_VEC_LENGTH (expanded_specs
);
11018 /* We're substituting into a member template, so
11019 we got a TYPE_PACK_EXPANSION back. Add that
11020 expansion and move on. */
11021 gcc_assert (TREE_CODE (expanded_specs
)
11022 == TYPE_PACK_EXPANSION
);
11023 new_specs
= add_exception_specifier (new_specs
,
11026 specs
= TREE_CHAIN (specs
);
11031 for (i
= 0; i
< len
; ++i
)
11033 if (expanded_specs
)
11034 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11036 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11037 if (spec
== error_mark_node
)
11039 new_specs
= add_exception_specifier (new_specs
, spec
,
11043 specs
= TREE_CHAIN (specs
);
11049 /* Take the tree structure T and replace template parameters used
11050 therein with the argument vector ARGS. IN_DECL is an associated
11051 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11052 Issue error and warning messages under control of COMPLAIN. Note
11053 that we must be relatively non-tolerant of extensions here, in
11054 order to preserve conformance; if we allow substitutions that
11055 should not be allowed, we may allow argument deductions that should
11056 not succeed, and therefore report ambiguous overload situations
11057 where there are none. In theory, we could allow the substitution,
11058 but indicate that it should have failed, and allow our caller to
11059 make sure that the right thing happens, but we don't try to do this
11062 This function is used for dealing with types, decls and the like;
11063 for expressions, use tsubst_expr or tsubst_copy. */
11066 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11068 enum tree_code code
;
11069 tree type
, r
= NULL_TREE
;
11071 if (t
== NULL_TREE
|| t
== error_mark_node
11072 || t
== integer_type_node
11073 || t
== void_type_node
11074 || t
== char_type_node
11075 || t
== unknown_type_node
11076 || TREE_CODE (t
) == NAMESPACE_DECL
11077 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11081 return tsubst_decl (t
, args
, complain
);
11083 if (args
== NULL_TREE
)
11086 code
= TREE_CODE (t
);
11088 if (code
== IDENTIFIER_NODE
)
11089 type
= IDENTIFIER_TYPE_VALUE (t
);
11091 type
= TREE_TYPE (t
);
11093 gcc_assert (type
!= unknown_type_node
);
11095 /* Reuse typedefs. We need to do this to handle dependent attributes,
11096 such as attribute aligned. */
11098 && typedef_variant_p (t
))
11100 tree decl
= TYPE_NAME (t
);
11102 if (alias_template_specialization_p (t
))
11104 /* DECL represents an alias template and we want to
11106 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11107 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11108 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11110 else if (DECL_CLASS_SCOPE_P (decl
)
11111 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11112 && uses_template_parms (DECL_CONTEXT (decl
)))
11114 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11115 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11116 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11118 else if (DECL_FUNCTION_SCOPE_P (decl
)
11119 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11120 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11121 r
= retrieve_local_specialization (decl
);
11123 /* The typedef is from a non-template context. */
11129 r
= cp_build_qualified_type_real
11130 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11131 complain
| tf_ignore_bad_quals
);
11136 /* We don't have an instantiation yet, so drop the typedef. */
11137 int quals
= cp_type_quals (t
);
11138 t
= DECL_ORIGINAL_TYPE (decl
);
11139 t
= cp_build_qualified_type_real (t
, quals
,
11140 complain
| tf_ignore_bad_quals
);
11145 && code
!= TYPENAME_TYPE
11146 && code
!= TEMPLATE_TYPE_PARM
11147 && code
!= IDENTIFIER_NODE
11148 && code
!= FUNCTION_TYPE
11149 && code
!= METHOD_TYPE
)
11150 type
= tsubst (type
, args
, complain
, in_decl
);
11151 if (type
== error_mark_node
)
11152 return error_mark_node
;
11158 case ENUMERAL_TYPE
:
11159 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11160 /*entering_scope=*/0);
11163 case IDENTIFIER_NODE
:
11174 if (t
== integer_type_node
)
11177 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11178 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11182 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11184 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11185 /*integral_constant_expression_p=*/false);
11187 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11189 if (TREE_CODE (max
) == NOP_EXPR
11190 && TREE_SIDE_EFFECTS (omax
)
11191 && !TREE_TYPE (max
))
11192 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11194 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11195 with TREE_SIDE_EFFECTS that indicates this is not an integral
11196 constant expression. */
11197 if (processing_template_decl
11198 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11200 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11201 TREE_SIDE_EFFECTS (max
) = 1;
11204 return compute_array_index_type (NULL_TREE
, max
, complain
);
11207 case TEMPLATE_TYPE_PARM
:
11208 case TEMPLATE_TEMPLATE_PARM
:
11209 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11210 case TEMPLATE_PARM_INDEX
:
11215 tree arg
= NULL_TREE
;
11219 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11220 template_parm_level_and_index (t
, &level
, &idx
);
11222 levels
= TMPL_ARGS_DEPTH (args
);
11223 if (level
<= levels
)
11225 arg
= TMPL_ARG (args
, level
, idx
);
11227 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11229 /* See through ARGUMENT_PACK_SELECT arguments. */
11230 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11231 /* If the selected argument is an expansion E, that most
11232 likely means we were called from
11233 gen_elem_of_pack_expansion_instantiation during the
11234 substituting of pack an argument pack (which Ith
11235 element is a pack expansion, where I is
11236 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11237 In this case, the Ith element resulting from this
11238 substituting is going to be a pack expansion, which
11239 pattern is the pattern of E. Let's return the
11241 gen_elem_of_pack_expansion_instantiation will
11242 build the resulting pack expansion from it. */
11243 if (PACK_EXPANSION_P (arg
))
11244 arg
= PACK_EXPANSION_PATTERN (arg
);
11248 if (arg
== error_mark_node
)
11249 return error_mark_node
;
11250 else if (arg
!= NULL_TREE
)
11252 if (ARGUMENT_PACK_P (arg
))
11253 /* If ARG is an argument pack, we don't actually want to
11254 perform a substitution here, because substitutions
11255 for argument packs are only done
11256 element-by-element. We can get to this point when
11257 substituting the type of a non-type template
11258 parameter pack, when that type actually contains
11259 template parameter packs from an outer template, e.g.,
11261 template<typename... Types> struct A {
11262 template<Types... Values> struct B { };
11266 if (code
== TEMPLATE_TYPE_PARM
)
11269 gcc_assert (TYPE_P (arg
));
11271 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11273 return cp_build_qualified_type_real
11274 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11276 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11278 /* We are processing a type constructed from a
11279 template template parameter. */
11280 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11281 args
, complain
, in_decl
);
11282 if (argvec
== error_mark_node
)
11283 return error_mark_node
;
11285 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11286 || TREE_CODE (arg
) == TEMPLATE_DECL
11287 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11289 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11290 /* Consider this code:
11292 template <template <class> class Template>
11294 template <class Arg> using Bind = Template<Arg>;
11297 template <template <class> class Template, class Arg>
11298 using Instantiate = Template<Arg>; //#0
11300 template <template <class> class Template,
11303 Instantiate<Internal<Template>::template Bind,
11306 When #1 is parsed, the
11307 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11308 parameter `Template' in #0 matches the
11309 UNBOUND_CLASS_TEMPLATE representing the argument
11310 `Internal<Template>::template Bind'; We then want
11311 to assemble the type `Bind<Argument>' that can't
11312 be fully created right now, because
11313 `Internal<Template>' not being complete, the Bind
11314 template cannot be looked up in that context. So
11315 we need to "store" `Bind<Argument>' for later
11316 when the context of Bind becomes complete. Let's
11317 store that in a TYPENAME_TYPE. */
11318 return make_typename_type (TYPE_CONTEXT (arg
),
11319 build_nt (TEMPLATE_ID_EXPR
,
11320 TYPE_IDENTIFIER (arg
),
11325 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11326 are resolving nested-types in the signature of a
11327 member function templates. Otherwise ARG is a
11328 TEMPLATE_DECL and is the real template to be
11330 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11331 arg
= TYPE_NAME (arg
);
11333 r
= lookup_template_class (arg
,
11335 DECL_CONTEXT (arg
),
11336 /*entering_scope=*/0,
11338 return cp_build_qualified_type_real
11339 (r
, cp_type_quals (t
), complain
);
11342 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11343 return convert_from_reference (unshare_expr (arg
));
11347 /* This can happen during the attempted tsubst'ing in
11348 unify. This means that we don't yet have any information
11349 about the template parameter in question. */
11352 /* Early in template argument deduction substitution, we don't
11353 want to reduce the level of 'auto', or it will be confused
11354 with a normal template parm in subsequent deduction. */
11355 if (is_auto (t
) && (complain
& tf_partial
))
11358 /* If we get here, we must have been looking at a parm for a
11359 more deeply nested template. Make a new version of this
11360 template parameter, but with a lower level. */
11363 case TEMPLATE_TYPE_PARM
:
11364 case TEMPLATE_TEMPLATE_PARM
:
11365 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11366 if (cp_type_quals (t
))
11368 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11369 r
= cp_build_qualified_type_real
11370 (r
, cp_type_quals (t
),
11371 complain
| (code
== TEMPLATE_TYPE_PARM
11372 ? tf_ignore_bad_quals
: 0));
11377 TEMPLATE_TYPE_PARM_INDEX (r
)
11378 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11379 r
, levels
, args
, complain
);
11380 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11381 TYPE_MAIN_VARIANT (r
) = r
;
11382 TYPE_POINTER_TO (r
) = NULL_TREE
;
11383 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11385 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11386 /* We have reduced the level of the template
11387 template parameter, but not the levels of its
11388 template parameters, so canonical_type_parameter
11389 will not be able to find the canonical template
11390 template parameter for this level. Thus, we
11391 require structural equality checking to compare
11392 TEMPLATE_TEMPLATE_PARMs. */
11393 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11394 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11395 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11397 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11399 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11401 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11402 complain
, in_decl
);
11403 if (argvec
== error_mark_node
)
11404 return error_mark_node
;
11406 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11407 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11412 case TEMPLATE_PARM_INDEX
:
11413 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11417 gcc_unreachable ();
11425 tree purpose
, value
, chain
;
11427 if (t
== void_list_node
)
11430 purpose
= TREE_PURPOSE (t
);
11433 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11434 if (purpose
== error_mark_node
)
11435 return error_mark_node
;
11437 value
= TREE_VALUE (t
);
11440 value
= tsubst (value
, args
, complain
, in_decl
);
11441 if (value
== error_mark_node
)
11442 return error_mark_node
;
11444 chain
= TREE_CHAIN (t
);
11445 if (chain
&& chain
!= void_type_node
)
11447 chain
= tsubst (chain
, args
, complain
, in_decl
);
11448 if (chain
== error_mark_node
)
11449 return error_mark_node
;
11451 if (purpose
== TREE_PURPOSE (t
)
11452 && value
== TREE_VALUE (t
)
11453 && chain
== TREE_CHAIN (t
))
11455 return hash_tree_cons (purpose
, value
, chain
);
11459 /* We should never be tsubsting a binfo. */
11460 gcc_unreachable ();
11463 /* A vector of template arguments. */
11464 gcc_assert (!type
);
11465 return tsubst_template_args (t
, args
, complain
, in_decl
);
11468 case REFERENCE_TYPE
:
11470 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11475 Type deduction may fail for any of the following
11478 -- Attempting to create a pointer to reference type.
11479 -- Attempting to create a reference to a reference type or
11480 a reference to void.
11482 Core issue 106 says that creating a reference to a reference
11483 during instantiation is no longer a cause for failure. We
11484 only enforce this check in strict C++98 mode. */
11485 if ((TREE_CODE (type
) == REFERENCE_TYPE
11486 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11487 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11489 static location_t last_loc
;
11491 /* We keep track of the last time we issued this error
11492 message to avoid spewing a ton of messages during a
11493 single bad template instantiation. */
11494 if (complain
& tf_error
11495 && last_loc
!= input_location
)
11497 if (TREE_CODE (type
) == VOID_TYPE
)
11498 error ("forming reference to void");
11499 else if (code
== POINTER_TYPE
)
11500 error ("forming pointer to reference type %qT", type
);
11502 error ("forming reference to reference type %qT", type
);
11503 last_loc
= input_location
;
11506 return error_mark_node
;
11508 else if (code
== POINTER_TYPE
)
11510 r
= build_pointer_type (type
);
11511 if (TREE_CODE (type
) == METHOD_TYPE
)
11512 r
= build_ptrmemfunc_type (r
);
11514 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11515 /* In C++0x, during template argument substitution, when there is an
11516 attempt to create a reference to a reference type, reference
11517 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11519 "If a template-argument for a template-parameter T names a type
11520 that is a reference to a type A, an attempt to create the type
11521 'lvalue reference to cv T' creates the type 'lvalue reference to
11522 A,' while an attempt to create the type type rvalue reference to
11523 cv T' creates the type T"
11525 r
= cp_build_reference_type
11527 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11529 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11530 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11532 if (r
!= error_mark_node
)
11533 /* Will this ever be needed for TYPE_..._TO values? */
11540 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11541 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11545 Type deduction may fail for any of the following
11548 -- Attempting to create "pointer to member of T" when T
11549 is not a class type. */
11550 if (complain
& tf_error
)
11551 error ("creating pointer to member of non-class type %qT", r
);
11552 return error_mark_node
;
11554 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11556 if (complain
& tf_error
)
11557 error ("creating pointer to member reference type %qT", type
);
11558 return error_mark_node
;
11560 if (TREE_CODE (type
) == VOID_TYPE
)
11562 if (complain
& tf_error
)
11563 error ("creating pointer to member of type void");
11564 return error_mark_node
;
11566 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11567 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11569 /* The type of the implicit object parameter gets its
11570 cv-qualifiers from the FUNCTION_TYPE. */
11572 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11573 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11574 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11578 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11582 case FUNCTION_TYPE
:
11587 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11588 if (fntype
== error_mark_node
)
11589 return error_mark_node
;
11591 /* Substitute the exception specification. */
11592 specs
= tsubst_exception_specification (t
, args
, complain
,
11593 in_decl
, /*defer_ok*/true);
11594 if (specs
== error_mark_node
)
11595 return error_mark_node
;
11597 fntype
= build_exception_variant (fntype
, specs
);
11602 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11603 if (domain
== error_mark_node
)
11604 return error_mark_node
;
11606 /* As an optimization, we avoid regenerating the array type if
11607 it will obviously be the same as T. */
11608 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11611 /* These checks should match the ones in grokdeclarator.
11615 The deduction may fail for any of the following reasons:
11617 -- Attempting to create an array with an element type that
11618 is void, a function type, or a reference type, or [DR337]
11619 an abstract class type. */
11620 if (TREE_CODE (type
) == VOID_TYPE
11621 || TREE_CODE (type
) == FUNCTION_TYPE
11622 || TREE_CODE (type
) == REFERENCE_TYPE
)
11624 if (complain
& tf_error
)
11625 error ("creating array of %qT", type
);
11626 return error_mark_node
;
11628 if (ABSTRACT_CLASS_TYPE_P (type
))
11630 if (complain
& tf_error
)
11631 error ("creating array of %qT, which is an abstract class type",
11633 return error_mark_node
;
11636 r
= build_cplus_array_type (type
, domain
);
11638 if (TYPE_USER_ALIGN (t
))
11640 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11641 TYPE_USER_ALIGN (r
) = 1;
11647 case TYPENAME_TYPE
:
11649 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11650 in_decl
, /*entering_scope=*/1);
11651 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11652 complain
, in_decl
);
11654 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11655 return error_mark_node
;
11657 if (!MAYBE_CLASS_TYPE_P (ctx
))
11659 if (complain
& tf_error
)
11660 error ("%qT is not a class, struct, or union type", ctx
);
11661 return error_mark_node
;
11663 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11665 /* Normally, make_typename_type does not require that the CTX
11666 have complete type in order to allow things like:
11668 template <class T> struct S { typename S<T>::X Y; };
11670 But, such constructs have already been resolved by this
11671 point, so here CTX really should have complete type, unless
11672 it's a partial instantiation. */
11673 ctx
= complete_type (ctx
);
11674 if (!COMPLETE_TYPE_P (ctx
))
11676 if (complain
& tf_error
)
11677 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11678 return error_mark_node
;
11682 f
= make_typename_type (ctx
, f
, typename_type
,
11683 complain
| tf_keep_type_decl
);
11684 if (f
== error_mark_node
)
11686 if (TREE_CODE (f
) == TYPE_DECL
)
11688 complain
|= tf_ignore_bad_quals
;
11692 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11694 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11696 if (complain
& tf_error
)
11697 error ("%qT resolves to %qT, which is not an enumeration type",
11700 return error_mark_node
;
11702 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11704 if (complain
& tf_error
)
11705 error ("%qT resolves to %qT, which is is not a class type",
11708 return error_mark_node
;
11712 return cp_build_qualified_type_real
11713 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11716 case UNBOUND_CLASS_TEMPLATE
:
11718 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11719 in_decl
, /*entering_scope=*/1);
11720 tree name
= TYPE_IDENTIFIER (t
);
11721 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11723 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11724 return error_mark_node
;
11727 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11728 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11735 ++cp_unevaluated_operand
;
11736 ++c_inhibit_evaluation_warnings
;
11738 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11740 /*integral_constant_expression_p=*/false);
11742 --cp_unevaluated_operand
;
11743 --c_inhibit_evaluation_warnings
;
11745 type
= finish_typeof (type
);
11746 return cp_build_qualified_type_real (type
,
11748 | cp_type_quals (type
),
11752 case DECLTYPE_TYPE
:
11756 ++cp_unevaluated_operand
;
11757 ++c_inhibit_evaluation_warnings
;
11759 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11761 /*integral_constant_expression_p=*/false);
11763 --cp_unevaluated_operand
;
11764 --c_inhibit_evaluation_warnings
;
11766 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11767 type
= lambda_capture_field_type (type
);
11768 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11769 type
= lambda_proxy_type (type
);
11771 type
= finish_decltype_type
11772 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11773 return cp_build_qualified_type_real (type
,
11775 | cp_type_quals (type
),
11779 case UNDERLYING_TYPE
:
11781 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11782 complain
, in_decl
);
11783 return finish_underlying_type (type
);
11786 case TYPE_ARGUMENT_PACK
:
11787 case NONTYPE_ARGUMENT_PACK
:
11789 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11791 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11795 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11797 /* For template nontype argument packs, also substitute into
11799 if (code
== NONTYPE_ARGUMENT_PACK
)
11800 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11818 /* We should use one of the expression tsubsts for these codes. */
11819 gcc_unreachable ();
11822 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11823 return error_mark_node
;
11827 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11828 type of the expression on the left-hand side of the "." or "->"
11832 tsubst_baselink (tree baselink
, tree object_type
,
11833 tree args
, tsubst_flags_t complain
, tree in_decl
)
11836 tree qualifying_scope
;
11839 tree template_args
= 0;
11840 bool template_id_p
= false;
11841 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11843 /* A baselink indicates a function from a base class. Both the
11844 BASELINK_ACCESS_BINFO and the base class referenced may
11845 indicate bases of the template class, rather than the
11846 instantiated class. In addition, lookups that were not
11847 ambiguous before may be ambiguous now. Therefore, we perform
11848 the lookup again. */
11849 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11850 qualifying_scope
= tsubst (qualifying_scope
, args
,
11851 complain
, in_decl
);
11852 fns
= BASELINK_FUNCTIONS (baselink
);
11853 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11854 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11856 template_id_p
= true;
11857 template_args
= TREE_OPERAND (fns
, 1);
11858 fns
= TREE_OPERAND (fns
, 0);
11860 template_args
= tsubst_template_args (template_args
, args
,
11861 complain
, in_decl
);
11863 name
= DECL_NAME (get_first_fn (fns
));
11864 if (IDENTIFIER_TYPENAME_P (name
))
11865 name
= mangle_conv_op_name_for_type (optype
);
11866 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11868 return error_mark_node
;
11870 /* If lookup found a single function, mark it as used at this
11871 point. (If it lookup found multiple functions the one selected
11872 later by overload resolution will be marked as used at that
11874 if (BASELINK_P (baselink
))
11875 fns
= BASELINK_FUNCTIONS (baselink
);
11876 if (!template_id_p
&& !really_overloaded_fn (fns
))
11877 mark_used (OVL_CURRENT (fns
));
11879 /* Add back the template arguments, if present. */
11880 if (BASELINK_P (baselink
) && template_id_p
)
11881 BASELINK_FUNCTIONS (baselink
)
11882 = build_nt (TEMPLATE_ID_EXPR
,
11883 BASELINK_FUNCTIONS (baselink
),
11885 /* Update the conversion operator type. */
11886 BASELINK_OPTYPE (baselink
) = optype
;
11889 object_type
= current_class_type
;
11892 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11898 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11899 true if the qualified-id will be a postfix-expression in-and-of
11900 itself; false if more of the postfix-expression follows the
11901 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11905 tsubst_qualified_id (tree qualified_id
, tree args
,
11906 tsubst_flags_t complain
, tree in_decl
,
11907 bool done
, bool address_p
)
11913 tree template_args
;
11914 location_t loc
= UNKNOWN_LOCATION
;
11916 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11918 /* Figure out what name to look up. */
11919 name
= TREE_OPERAND (qualified_id
, 1);
11920 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11922 is_template
= true;
11923 loc
= EXPR_LOCATION (name
);
11924 template_args
= TREE_OPERAND (name
, 1);
11926 template_args
= tsubst_template_args (template_args
, args
,
11927 complain
, in_decl
);
11928 name
= TREE_OPERAND (name
, 0);
11932 is_template
= false;
11933 template_args
= NULL_TREE
;
11936 /* Substitute into the qualifying scope. When there are no ARGS, we
11937 are just trying to simplify a non-dependent expression. In that
11938 case the qualifying scope may be dependent, and, in any case,
11939 substituting will not help. */
11940 scope
= TREE_OPERAND (qualified_id
, 0);
11943 scope
= tsubst (scope
, args
, complain
, in_decl
);
11944 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11949 if (dependent_scope_p (scope
))
11952 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11953 return build_qualified_name (NULL_TREE
, scope
, expr
,
11954 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11957 if (!BASELINK_P (name
) && !DECL_P (expr
))
11959 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11961 /* A BIT_NOT_EXPR is used to represent a destructor. */
11962 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11964 error ("qualifying type %qT does not match destructor name ~%qT",
11965 scope
, TREE_OPERAND (expr
, 0));
11966 expr
= error_mark_node
;
11969 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11970 /*is_type_p=*/0, false);
11973 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11974 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11975 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11977 if (complain
& tf_error
)
11979 error ("dependent-name %qE is parsed as a non-type, but "
11980 "instantiation yields a type", qualified_id
);
11981 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11983 return error_mark_node
;
11989 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11991 /* Remember that there was a reference to this entity. */
11995 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11997 if (complain
& tf_error
)
11998 qualified_name_lookup_error (scope
,
11999 TREE_OPERAND (qualified_id
, 1),
12000 expr
, input_location
);
12001 return error_mark_node
;
12005 expr
= lookup_template_function (expr
, template_args
);
12007 if (expr
== error_mark_node
&& complain
& tf_error
)
12008 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12009 expr
, input_location
);
12010 else if (TYPE_P (scope
))
12012 expr
= (adjust_result_of_qualified_name_lookup
12013 (expr
, scope
, current_class_type
));
12014 expr
= (finish_qualified_id_expr
12015 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12016 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12017 /*template_arg_p=*/false));
12020 /* Expressions do not generally have reference type. */
12021 if (TREE_CODE (expr
) != SCOPE_REF
12022 /* However, if we're about to form a pointer-to-member, we just
12023 want the referenced member referenced. */
12024 && TREE_CODE (expr
) != OFFSET_REF
)
12025 expr
= convert_from_reference (expr
);
12030 /* Like tsubst, but deals with expressions. This function just replaces
12031 template parms; to finish processing the resultant expression, use
12032 tsubst_copy_and_build or tsubst_expr. */
12035 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12037 enum tree_code code
;
12040 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12043 code
= TREE_CODE (t
);
12048 r
= retrieve_local_specialization (t
);
12050 if (r
== NULL_TREE
)
12052 /* We get here for a use of 'this' in an NSDMI. */
12053 if (DECL_NAME (t
) == this_identifier
12054 && at_function_scope_p ()
12055 && DECL_CONSTRUCTOR_P (current_function_decl
))
12056 return current_class_ptr
;
12058 /* This can happen for a parameter name used later in a function
12059 declaration (such as in a late-specified return type). Just
12060 make a dummy decl, since it's only used for its type. */
12061 gcc_assert (cp_unevaluated_operand
!= 0);
12062 r
= tsubst_decl (t
, args
, complain
);
12063 /* Give it the template pattern as its context; its true context
12064 hasn't been instantiated yet and this is good enough for
12066 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12069 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12070 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12079 if (DECL_TEMPLATE_PARM_P (t
))
12080 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12081 /* There is no need to substitute into namespace-scope
12083 if (DECL_NAMESPACE_SCOPE_P (t
))
12085 /* If ARGS is NULL, then T is known to be non-dependent. */
12086 if (args
== NULL_TREE
)
12087 return integral_constant_value (t
);
12089 /* Unfortunately, we cannot just call lookup_name here.
12092 template <int I> int f() {
12094 struct S { void g() { E e = a; } };
12097 When we instantiate f<7>::S::g(), say, lookup_name is not
12098 clever enough to find f<7>::a. */
12100 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12101 /*entering_scope=*/0);
12103 for (v
= TYPE_VALUES (enum_type
);
12105 v
= TREE_CHAIN (v
))
12106 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12107 return TREE_VALUE (v
);
12109 /* We didn't find the name. That should never happen; if
12110 name-lookup found it during preliminary parsing, we
12111 should find it again here during instantiation. */
12112 gcc_unreachable ();
12117 if (DECL_CONTEXT (t
))
12121 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12122 /*entering_scope=*/1);
12123 if (ctx
!= DECL_CONTEXT (t
))
12125 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12128 if (complain
& tf_error
)
12129 error ("using invalid field %qD", t
);
12130 return error_mark_node
;
12139 case FUNCTION_DECL
:
12140 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12141 || local_variable_p (t
))
12142 t
= tsubst (t
, args
, complain
, in_decl
);
12146 case NAMESPACE_DECL
:
12150 /* An OVERLOAD will always be a non-dependent overload set; an
12151 overload set from function scope will just be represented with an
12152 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12153 gcc_assert (!uses_template_parms (t
));
12157 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12159 case TEMPLATE_DECL
:
12160 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12161 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12162 args
, complain
, in_decl
);
12163 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12164 return tsubst (t
, args
, complain
, in_decl
);
12165 else if (DECL_CLASS_SCOPE_P (t
)
12166 && uses_template_parms (DECL_CONTEXT (t
)))
12168 /* Template template argument like the following example need
12171 template <template <class> class TT> struct C {};
12172 template <class T> struct D {
12173 template <class U> struct E {};
12178 We are processing the template argument `E' in #1 for
12179 the template instantiation #2. Originally, `E' is a
12180 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12181 have to substitute this with one having context `D<int>'. */
12183 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12184 return lookup_field (context
, DECL_NAME(t
), 0, false);
12187 /* Ordinary template template argument. */
12191 case REINTERPRET_CAST_EXPR
:
12192 case CONST_CAST_EXPR
:
12193 case STATIC_CAST_EXPR
:
12194 case DYNAMIC_CAST_EXPR
:
12195 case IMPLICIT_CONV_EXPR
:
12199 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12200 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12203 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12206 tree expanded
, op
= TREE_OPERAND (t
, 0);
12209 if (SIZEOF_EXPR_TYPE_P (t
))
12210 op
= TREE_TYPE (op
);
12212 ++cp_unevaluated_operand
;
12213 ++c_inhibit_evaluation_warnings
;
12214 /* We only want to compute the number of arguments. */
12215 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12216 --cp_unevaluated_operand
;
12217 --c_inhibit_evaluation_warnings
;
12219 if (TREE_CODE (expanded
) == TREE_VEC
)
12220 len
= TREE_VEC_LENGTH (expanded
);
12222 if (expanded
== error_mark_node
)
12223 return error_mark_node
;
12224 else if (PACK_EXPANSION_P (expanded
)
12225 || (TREE_CODE (expanded
) == TREE_VEC
12227 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12229 if (TREE_CODE (expanded
) == TREE_VEC
)
12230 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12232 if (TYPE_P (expanded
))
12233 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12234 complain
& tf_error
);
12236 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12237 complain
& tf_error
);
12240 return build_int_cst (size_type_node
, len
);
12242 if (SIZEOF_EXPR_TYPE_P (t
))
12244 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12245 args
, complain
, in_decl
);
12246 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12247 r
= build1 (SIZEOF_EXPR
,
12248 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12249 SIZEOF_EXPR_TYPE_P (r
) = 1;
12256 case TRUTH_NOT_EXPR
:
12259 case UNARY_PLUS_EXPR
: /* Unary + */
12261 case AT_ENCODE_EXPR
:
12265 case REALPART_EXPR
:
12266 case IMAGPART_EXPR
:
12268 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12269 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12271 case COMPONENT_REF
:
12276 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12277 name
= TREE_OPERAND (t
, 1);
12278 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12280 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12281 complain
, in_decl
);
12282 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12284 else if (TREE_CODE (name
) == SCOPE_REF
12285 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12287 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12288 complain
, in_decl
);
12289 name
= TREE_OPERAND (name
, 1);
12290 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12291 complain
, in_decl
);
12292 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12293 name
= build_qualified_name (/*type=*/NULL_TREE
,
12295 /*template_p=*/false);
12297 else if (BASELINK_P (name
))
12298 name
= tsubst_baselink (name
,
12299 non_reference (TREE_TYPE (object
)),
12303 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12304 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12310 case TRUNC_DIV_EXPR
:
12311 case CEIL_DIV_EXPR
:
12312 case FLOOR_DIV_EXPR
:
12313 case ROUND_DIV_EXPR
:
12314 case EXACT_DIV_EXPR
:
12318 case TRUNC_MOD_EXPR
:
12319 case FLOOR_MOD_EXPR
:
12320 case TRUTH_ANDIF_EXPR
:
12321 case TRUTH_ORIF_EXPR
:
12322 case TRUTH_AND_EXPR
:
12323 case TRUTH_OR_EXPR
:
12336 case COMPOUND_EXPR
:
12339 case PREDECREMENT_EXPR
:
12340 case PREINCREMENT_EXPR
:
12341 case POSTDECREMENT_EXPR
:
12342 case POSTINCREMENT_EXPR
:
12344 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12345 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12348 return build_qualified_name (/*type=*/NULL_TREE
,
12349 tsubst_copy (TREE_OPERAND (t
, 0),
12350 args
, complain
, in_decl
),
12351 tsubst_copy (TREE_OPERAND (t
, 1),
12352 args
, complain
, in_decl
),
12353 QUALIFIED_NAME_IS_TEMPLATE (t
));
12358 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12359 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12360 NULL_TREE
, NULL_TREE
);
12364 int n
= VL_EXP_OPERAND_LENGTH (t
);
12365 tree result
= build_vl_exp (CALL_EXPR
, n
);
12367 for (i
= 0; i
< n
; i
++)
12368 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12369 complain
, in_decl
);
12375 case PSEUDO_DTOR_EXPR
:
12378 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12379 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12380 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12381 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12388 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12389 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12390 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12391 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12398 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12399 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12400 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12401 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12405 case TEMPLATE_ID_EXPR
:
12407 /* Substituted template arguments */
12408 tree fn
= TREE_OPERAND (t
, 0);
12409 tree targs
= TREE_OPERAND (t
, 1);
12411 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12413 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12415 return lookup_template_function (fn
, targs
);
12420 tree purpose
, value
, chain
;
12422 if (t
== void_list_node
)
12425 purpose
= TREE_PURPOSE (t
);
12427 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12428 value
= TREE_VALUE (t
);
12430 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12431 chain
= TREE_CHAIN (t
);
12432 if (chain
&& chain
!= void_type_node
)
12433 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12434 if (purpose
== TREE_PURPOSE (t
)
12435 && value
== TREE_VALUE (t
)
12436 && chain
== TREE_CHAIN (t
))
12438 return tree_cons (purpose
, value
, chain
);
12443 case ENUMERAL_TYPE
:
12445 case TEMPLATE_TYPE_PARM
:
12446 case TEMPLATE_TEMPLATE_PARM
:
12447 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12448 case TEMPLATE_PARM_INDEX
:
12450 case REFERENCE_TYPE
:
12452 case FUNCTION_TYPE
:
12455 case TYPENAME_TYPE
:
12456 case UNBOUND_CLASS_TEMPLATE
:
12458 case DECLTYPE_TYPE
:
12460 return tsubst (t
, args
, complain
, in_decl
);
12462 case IDENTIFIER_NODE
:
12463 if (IDENTIFIER_TYPENAME_P (t
))
12465 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12466 return mangle_conv_op_name_for_type (new_type
);
12472 /* This is handled by tsubst_copy_and_build. */
12473 gcc_unreachable ();
12476 return build_x_va_arg (EXPR_LOCATION (t
),
12477 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12479 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12481 case CLEANUP_POINT_EXPR
:
12482 /* We shouldn't have built any of these during initial template
12483 generation. Instead, they should be built during instantiation
12484 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12485 gcc_unreachable ();
12489 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12490 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12491 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12492 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12493 mark_used (TREE_OPERAND (r
, 1));
12496 case EXPR_PACK_EXPANSION
:
12497 error ("invalid use of pack expansion expression");
12498 return error_mark_node
;
12500 case NONTYPE_ARGUMENT_PACK
:
12501 error ("use %<...%> to expand argument pack");
12502 return error_mark_node
;
12509 /* Instantiate any typedefs in the type. */
12510 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12511 r
= fold_convert (type
, t
);
12512 gcc_assert (TREE_CODE (r
) == code
);
12517 /* These can sometimes show up in a partial instantiation, but never
12518 involve template parms. */
12519 gcc_assert (!uses_template_parms (t
));
12523 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12524 gcc_checking_assert (false);
12529 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12532 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12535 tree new_clauses
= NULL
, nc
, oc
;
12537 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12539 nc
= copy_node (oc
);
12540 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12543 switch (OMP_CLAUSE_CODE (nc
))
12545 case OMP_CLAUSE_LASTPRIVATE
:
12546 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12548 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12549 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12550 in_decl
, /*integral_constant_expression_p=*/false);
12551 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12552 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12555 case OMP_CLAUSE_PRIVATE
:
12556 case OMP_CLAUSE_SHARED
:
12557 case OMP_CLAUSE_FIRSTPRIVATE
:
12558 case OMP_CLAUSE_REDUCTION
:
12559 case OMP_CLAUSE_COPYIN
:
12560 case OMP_CLAUSE_COPYPRIVATE
:
12561 case OMP_CLAUSE_IF
:
12562 case OMP_CLAUSE_NUM_THREADS
:
12563 case OMP_CLAUSE_SCHEDULE
:
12564 case OMP_CLAUSE_COLLAPSE
:
12565 case OMP_CLAUSE_FINAL
:
12566 OMP_CLAUSE_OPERAND (nc
, 0)
12567 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12568 in_decl
, /*integral_constant_expression_p=*/false);
12570 case OMP_CLAUSE_NOWAIT
:
12571 case OMP_CLAUSE_ORDERED
:
12572 case OMP_CLAUSE_DEFAULT
:
12573 case OMP_CLAUSE_UNTIED
:
12574 case OMP_CLAUSE_MERGEABLE
:
12577 gcc_unreachable ();
12581 return finish_omp_clauses (nreverse (new_clauses
));
12584 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12587 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12590 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12592 tree purpose
, value
, chain
;
12597 if (TREE_CODE (t
) != TREE_LIST
)
12598 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12599 /*function_p=*/false,
12600 /*integral_constant_expression_p=*/false);
12602 if (t
== void_list_node
)
12605 purpose
= TREE_PURPOSE (t
);
12607 purpose
= RECUR (purpose
);
12608 value
= TREE_VALUE (t
);
12611 if (TREE_CODE (value
) != LABEL_DECL
)
12612 value
= RECUR (value
);
12615 value
= lookup_label (DECL_NAME (value
));
12616 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12617 TREE_USED (value
) = 1;
12620 chain
= TREE_CHAIN (t
);
12621 if (chain
&& chain
!= void_type_node
)
12622 chain
= RECUR (chain
);
12623 return tree_cons (purpose
, value
, chain
);
12627 /* Substitute one OMP_FOR iterator. */
12630 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12631 tree condv
, tree incrv
, tree
*clauses
,
12632 tree args
, tsubst_flags_t complain
, tree in_decl
,
12633 bool integral_constant_expression_p
)
12635 #define RECUR(NODE) \
12636 tsubst_expr ((NODE), args, complain, in_decl, \
12637 integral_constant_expression_p)
12638 tree decl
, init
, cond
, incr
;
12641 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12642 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12643 decl
= TREE_OPERAND (init
, 0);
12644 init
= TREE_OPERAND (init
, 1);
12645 /* Do this before substituting into decl to handle 'auto'. */
12646 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12647 init
= RECUR (init
);
12648 decl
= RECUR (decl
);
12651 gcc_assert (!processing_template_decl
);
12652 init
= DECL_INITIAL (decl
);
12653 DECL_INITIAL (decl
) = NULL_TREE
;
12656 gcc_assert (!type_dependent_expression_p (decl
));
12658 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12660 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12661 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12662 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12663 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12664 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12665 RECUR (TREE_OPERAND (incr
, 1)),
12668 incr
= RECUR (incr
);
12669 TREE_VEC_ELT (declv
, i
) = decl
;
12670 TREE_VEC_ELT (initv
, i
) = init
;
12671 TREE_VEC_ELT (condv
, i
) = cond
;
12672 TREE_VEC_ELT (incrv
, i
) = incr
;
12676 if (init
&& !init_decl
)
12679 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12681 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12682 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12683 && OMP_CLAUSE_DECL (c
) == decl
)
12685 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12686 && OMP_CLAUSE_DECL (c
) == decl
)
12687 error ("iteration variable %qD should not be firstprivate", decl
);
12688 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12689 && OMP_CLAUSE_DECL (c
) == decl
)
12690 error ("iteration variable %qD should not be reduction", decl
);
12694 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12695 OMP_CLAUSE_DECL (c
) = decl
;
12696 c
= finish_omp_clauses (c
);
12699 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12704 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12705 if (COMPARISON_CLASS_P (cond
))
12706 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12707 RECUR (TREE_OPERAND (cond
, 0)),
12708 RECUR (TREE_OPERAND (cond
, 1)));
12710 cond
= RECUR (cond
);
12711 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12712 switch (TREE_CODE (incr
))
12714 case PREINCREMENT_EXPR
:
12715 case PREDECREMENT_EXPR
:
12716 case POSTINCREMENT_EXPR
:
12717 case POSTDECREMENT_EXPR
:
12718 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12719 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12722 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12723 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12725 tree rhs
= TREE_OPERAND (incr
, 1);
12726 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12727 RECUR (TREE_OPERAND (incr
, 0)),
12728 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12729 RECUR (TREE_OPERAND (rhs
, 0)),
12730 RECUR (TREE_OPERAND (rhs
, 1))));
12733 incr
= RECUR (incr
);
12736 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12737 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12739 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12740 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12741 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12742 TREE_TYPE (decl
), lhs
,
12743 RECUR (TREE_OPERAND (incr
, 2))));
12745 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12746 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12747 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12749 tree rhs
= TREE_OPERAND (incr
, 2);
12750 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12751 RECUR (TREE_OPERAND (incr
, 0)),
12752 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12753 RECUR (TREE_OPERAND (rhs
, 0)),
12754 RECUR (TREE_OPERAND (rhs
, 1))));
12757 incr
= RECUR (incr
);
12760 incr
= RECUR (incr
);
12764 TREE_VEC_ELT (declv
, i
) = decl
;
12765 TREE_VEC_ELT (initv
, i
) = init
;
12766 TREE_VEC_ELT (condv
, i
) = cond
;
12767 TREE_VEC_ELT (incrv
, i
) = incr
;
12771 /* Like tsubst_copy for expressions, etc. but also does semantic
12775 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12776 bool integral_constant_expression_p
)
12778 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12779 #define RECUR(NODE) \
12780 tsubst_expr ((NODE), args, complain, in_decl, \
12781 integral_constant_expression_p)
12787 if (t
== NULL_TREE
|| t
== error_mark_node
)
12790 loc
= input_location
;
12791 if (EXPR_HAS_LOCATION (t
))
12792 input_location
= EXPR_LOCATION (t
);
12793 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12794 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12796 switch (TREE_CODE (t
))
12798 case STATEMENT_LIST
:
12800 tree_stmt_iterator i
;
12801 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12802 RECUR (tsi_stmt (i
));
12806 case CTOR_INITIALIZER
:
12807 finish_mem_initializers (tsubst_initializer_list
12808 (TREE_OPERAND (t
, 0), args
));
12812 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12816 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12817 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12818 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12820 finish_expr_stmt (tmp
);
12824 do_using_directive (USING_STMT_NAMESPACE (t
));
12829 tree decl
, pattern_decl
;
12832 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12833 if (TREE_CODE (decl
) == LABEL_DECL
)
12834 finish_label_decl (DECL_NAME (decl
));
12835 else if (TREE_CODE (decl
) == USING_DECL
)
12837 tree scope
= USING_DECL_SCOPE (decl
);
12838 tree name
= DECL_NAME (decl
);
12841 scope
= tsubst (scope
, args
, complain
, in_decl
);
12842 decl
= lookup_qualified_name (scope
, name
,
12843 /*is_type_p=*/false,
12844 /*complain=*/false);
12845 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12846 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12848 do_local_using_decl (decl
, scope
, name
);
12852 init
= DECL_INITIAL (decl
);
12853 decl
= tsubst (decl
, args
, complain
, in_decl
);
12854 if (decl
!= error_mark_node
)
12856 /* By marking the declaration as instantiated, we avoid
12857 trying to instantiate it. Since instantiate_decl can't
12858 handle local variables, and since we've already done
12859 all that needs to be done, that's the right thing to
12861 if (TREE_CODE (decl
) == VAR_DECL
)
12862 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12863 if (TREE_CODE (decl
) == VAR_DECL
12864 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12865 /* Anonymous aggregates are a special case. */
12866 finish_anon_union (decl
);
12867 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12869 DECL_CONTEXT (decl
) = current_function_decl
;
12870 if (DECL_NAME (decl
) == this_identifier
)
12872 tree lam
= DECL_CONTEXT (current_function_decl
);
12873 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
12874 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
12876 insert_capture_proxy (decl
);
12878 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12879 /* We already did a pushtag. */;
12882 int const_init
= false;
12883 maybe_push_decl (decl
);
12884 if (TREE_CODE (decl
) == VAR_DECL
12885 && DECL_PRETTY_FUNCTION_P (decl
))
12887 /* For __PRETTY_FUNCTION__ we have to adjust the
12889 const char *const name
12890 = cxx_printable_name (current_function_decl
, 2);
12891 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12895 tree t
= RECUR (init
);
12899 /* If we had an initializer but it
12900 instantiated to nothing,
12901 value-initialize the object. This will
12902 only occur when the initializer was a
12903 pack expansion where the parameter packs
12904 used in that expansion were of length
12906 init
= build_value_init (TREE_TYPE (decl
),
12908 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12909 init
= get_target_expr_sfinae (init
, complain
);
12915 if (TREE_CODE (decl
) == VAR_DECL
)
12916 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12918 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12927 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12928 RECUR (FOR_INIT_STMT (t
));
12929 finish_for_init_stmt (stmt
);
12930 tmp
= RECUR (FOR_COND (t
));
12931 finish_for_cond (tmp
, stmt
);
12932 tmp
= RECUR (FOR_EXPR (t
));
12933 finish_for_expr (tmp
, stmt
);
12934 RECUR (FOR_BODY (t
));
12935 finish_for_stmt (stmt
);
12938 case RANGE_FOR_STMT
:
12941 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12942 decl
= RANGE_FOR_DECL (t
);
12943 decl
= tsubst (decl
, args
, complain
, in_decl
);
12944 maybe_push_decl (decl
);
12945 expr
= RECUR (RANGE_FOR_EXPR (t
));
12946 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12947 RECUR (RANGE_FOR_BODY (t
));
12948 finish_for_stmt (stmt
);
12953 stmt
= begin_while_stmt ();
12954 tmp
= RECUR (WHILE_COND (t
));
12955 finish_while_stmt_cond (tmp
, stmt
);
12956 RECUR (WHILE_BODY (t
));
12957 finish_while_stmt (stmt
);
12961 stmt
= begin_do_stmt ();
12962 RECUR (DO_BODY (t
));
12963 finish_do_body (stmt
);
12964 tmp
= RECUR (DO_COND (t
));
12965 finish_do_stmt (tmp
, stmt
);
12969 stmt
= begin_if_stmt ();
12970 tmp
= RECUR (IF_COND (t
));
12971 finish_if_stmt_cond (tmp
, stmt
);
12972 RECUR (THEN_CLAUSE (t
));
12973 finish_then_clause (stmt
);
12975 if (ELSE_CLAUSE (t
))
12977 begin_else_clause (stmt
);
12978 RECUR (ELSE_CLAUSE (t
));
12979 finish_else_clause (stmt
);
12982 finish_if_stmt (stmt
);
12986 if (BIND_EXPR_BODY_BLOCK (t
))
12987 stmt
= begin_function_body ();
12989 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12990 ? BCS_TRY_BLOCK
: 0);
12992 RECUR (BIND_EXPR_BODY (t
));
12994 if (BIND_EXPR_BODY_BLOCK (t
))
12995 finish_function_body (stmt
);
12997 finish_compound_stmt (stmt
);
13001 finish_break_stmt ();
13004 case CONTINUE_STMT
:
13005 finish_continue_stmt ();
13009 stmt
= begin_switch_stmt ();
13010 tmp
= RECUR (SWITCH_STMT_COND (t
));
13011 finish_switch_cond (tmp
, stmt
);
13012 RECUR (SWITCH_STMT_BODY (t
));
13013 finish_switch_stmt (stmt
);
13016 case CASE_LABEL_EXPR
:
13017 finish_case_label (EXPR_LOCATION (t
),
13018 RECUR (CASE_LOW (t
)),
13019 RECUR (CASE_HIGH (t
)));
13024 tree decl
= LABEL_EXPR_LABEL (t
);
13027 label
= finish_label_stmt (DECL_NAME (decl
));
13028 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13029 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13034 tmp
= GOTO_DESTINATION (t
);
13035 if (TREE_CODE (tmp
) != LABEL_DECL
)
13036 /* Computed goto's must be tsubst'd into. On the other hand,
13037 non-computed gotos must not be; the identifier in question
13038 will have no binding. */
13041 tmp
= DECL_NAME (tmp
);
13042 finish_goto_stmt (tmp
);
13046 tmp
= finish_asm_stmt
13047 (ASM_VOLATILE_P (t
),
13048 RECUR (ASM_STRING (t
)),
13049 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13050 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13051 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13052 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13054 tree asm_expr
= tmp
;
13055 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13056 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13057 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13064 stmt
= begin_try_block ();
13065 RECUR (TRY_STMTS (t
));
13066 finish_cleanup_try_block (stmt
);
13067 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13071 tree compound_stmt
= NULL_TREE
;
13073 if (FN_TRY_BLOCK_P (t
))
13074 stmt
= begin_function_try_block (&compound_stmt
);
13076 stmt
= begin_try_block ();
13078 RECUR (TRY_STMTS (t
));
13080 if (FN_TRY_BLOCK_P (t
))
13081 finish_function_try_block (stmt
);
13083 finish_try_block (stmt
);
13085 RECUR (TRY_HANDLERS (t
));
13086 if (FN_TRY_BLOCK_P (t
))
13087 finish_function_handler_sequence (stmt
, compound_stmt
);
13089 finish_handler_sequence (stmt
);
13095 tree decl
= HANDLER_PARMS (t
);
13099 decl
= tsubst (decl
, args
, complain
, in_decl
);
13100 /* Prevent instantiate_decl from trying to instantiate
13101 this variable. We've already done all that needs to be
13103 if (decl
!= error_mark_node
)
13104 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13106 stmt
= begin_handler ();
13107 finish_handler_parms (decl
, stmt
);
13108 RECUR (HANDLER_BODY (t
));
13109 finish_handler (stmt
);
13114 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13115 if (CLASS_TYPE_P (tmp
))
13117 /* Local classes are not independent templates; they are
13118 instantiated along with their containing function. And this
13119 way we don't have to deal with pushing out of one local class
13120 to instantiate a member of another local class. */
13122 /* Closures are handled by the LAMBDA_EXPR. */
13123 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13124 complete_type (tmp
);
13125 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13126 if (!DECL_ARTIFICIAL (fn
))
13127 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13131 case STATIC_ASSERT
:
13135 ++c_inhibit_evaluation_warnings
;
13137 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13140 /*integral_constant_expression_p=*/true);
13141 --c_inhibit_evaluation_warnings
;
13143 finish_static_assert (condition
,
13144 STATIC_ASSERT_MESSAGE (t
),
13145 STATIC_ASSERT_SOURCE_LOCATION (t
),
13146 /*member_p=*/false);
13151 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13152 args
, complain
, in_decl
);
13153 stmt
= begin_omp_parallel ();
13154 RECUR (OMP_PARALLEL_BODY (t
));
13155 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13156 = OMP_PARALLEL_COMBINED (t
);
13160 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13161 args
, complain
, in_decl
);
13162 stmt
= begin_omp_task ();
13163 RECUR (OMP_TASK_BODY (t
));
13164 finish_omp_task (tmp
, stmt
);
13169 tree clauses
, body
, pre_body
;
13170 tree declv
, initv
, condv
, incrv
;
13173 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13174 args
, complain
, in_decl
);
13175 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13176 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13177 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13178 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13180 stmt
= begin_omp_structured_block ();
13182 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13183 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13184 &clauses
, args
, complain
, in_decl
,
13185 integral_constant_expression_p
);
13187 pre_body
= push_stmt_list ();
13188 RECUR (OMP_FOR_PRE_BODY (t
));
13189 pre_body
= pop_stmt_list (pre_body
);
13191 body
= push_stmt_list ();
13192 RECUR (OMP_FOR_BODY (t
));
13193 body
= pop_stmt_list (body
);
13195 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13196 body
, pre_body
, clauses
);
13198 add_stmt (finish_omp_structured_block (stmt
));
13204 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13205 stmt
= push_stmt_list ();
13206 RECUR (OMP_BODY (t
));
13207 stmt
= pop_stmt_list (stmt
);
13210 OMP_BODY (t
) = stmt
;
13211 OMP_CLAUSES (t
) = tmp
;
13219 stmt
= push_stmt_list ();
13220 RECUR (OMP_BODY (t
));
13221 stmt
= pop_stmt_list (stmt
);
13224 OMP_BODY (t
) = stmt
;
13229 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13230 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13232 tree op1
= TREE_OPERAND (t
, 1);
13233 tree rhs1
= NULL_TREE
;
13235 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13237 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13238 op1
= TREE_OPERAND (op1
, 1);
13240 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13241 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13242 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13243 NULL_TREE
, NULL_TREE
, rhs1
);
13247 tree op1
= TREE_OPERAND (t
, 1);
13248 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13249 tree rhs1
= NULL_TREE
;
13250 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13251 enum tree_code opcode
= NOP_EXPR
;
13252 if (code
== OMP_ATOMIC_READ
)
13254 v
= RECUR (TREE_OPERAND (op1
, 0));
13255 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13257 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13258 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13260 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13261 v
= RECUR (TREE_OPERAND (op1
, 0));
13262 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13263 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13265 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13266 op11
= TREE_OPERAND (op11
, 1);
13268 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13269 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13270 opcode
= TREE_CODE (op11
);
13275 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13276 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13278 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13282 case TRANSACTION_EXPR
:
13285 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13286 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13288 if (TRANSACTION_EXPR_IS_STMT (t
))
13290 tree body
= TRANSACTION_EXPR_BODY (t
);
13291 tree noex
= NULL_TREE
;
13292 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13294 noex
= MUST_NOT_THROW_COND (body
);
13295 if (noex
== NULL_TREE
)
13296 noex
= boolean_true_node
;
13297 body
= TREE_OPERAND (body
, 0);
13299 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13301 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13305 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13306 RECUR (TRANSACTION_EXPR_BODY (t
)),
13313 case MUST_NOT_THROW_EXPR
:
13314 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13315 RECUR (MUST_NOT_THROW_COND (t
))));
13317 case EXPR_PACK_EXPANSION
:
13318 error ("invalid use of pack expansion expression");
13319 RETURN (error_mark_node
);
13321 case NONTYPE_ARGUMENT_PACK
:
13322 error ("use %<...%> to expand argument pack");
13323 RETURN (error_mark_node
);
13325 case COMPOUND_EXPR
:
13326 tmp
= RECUR (TREE_OPERAND (t
, 0));
13327 if (tmp
== NULL_TREE
)
13328 /* If the first operand was a statement, we're done with it. */
13329 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13330 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13331 RECUR (TREE_OPERAND (t
, 1)),
13335 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13337 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13338 /*function_p=*/false,
13339 integral_constant_expression_p
));
13342 RETURN (NULL_TREE
);
13344 input_location
= loc
;
13350 /* T is a postfix-expression that is not being used in a function
13351 call. Return the substituted version of T. */
13354 tsubst_non_call_postfix_expression (tree t
, tree args
,
13355 tsubst_flags_t complain
,
13358 if (TREE_CODE (t
) == SCOPE_REF
)
13359 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13360 /*done=*/false, /*address_p=*/false);
13362 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13363 /*function_p=*/false,
13364 /*integral_constant_expression_p=*/false);
13369 /* Like tsubst but deals with expressions and performs semantic
13370 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13373 tsubst_copy_and_build (tree t
,
13375 tsubst_flags_t complain
,
13378 bool integral_constant_expression_p
)
13380 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13381 #define RECUR(NODE) \
13382 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13383 /*function_p=*/false, \
13384 integral_constant_expression_p)
13389 if (t
== NULL_TREE
|| t
== error_mark_node
)
13392 loc
= input_location
;
13393 if (EXPR_HAS_LOCATION (t
))
13394 input_location
= EXPR_LOCATION (t
);
13396 switch (TREE_CODE (t
))
13400 /* Fall through. */
13401 case IDENTIFIER_NODE
:
13405 bool non_integral_constant_expression_p
;
13406 const char *error_msg
;
13408 if (IDENTIFIER_TYPENAME_P (t
))
13410 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13411 t
= mangle_conv_op_name_for_type (new_type
);
13414 /* Look up the name. */
13415 decl
= lookup_name (t
);
13417 /* By convention, expressions use ERROR_MARK_NODE to indicate
13418 failure, not NULL_TREE. */
13419 if (decl
== NULL_TREE
)
13420 decl
= error_mark_node
;
13422 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13424 integral_constant_expression_p
,
13425 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13426 &non_integral_constant_expression_p
,
13427 /*template_p=*/false,
13429 /*address_p=*/false,
13430 /*template_arg_p=*/false,
13435 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13437 if (complain
& tf_error
)
13438 unqualified_name_lookup_error (decl
);
13439 decl
= error_mark_node
;
13444 case TEMPLATE_ID_EXPR
:
13447 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13448 tree targs
= TREE_OPERAND (t
, 1);
13451 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13453 if (TREE_CODE (templ
) == COMPONENT_REF
)
13455 object
= TREE_OPERAND (templ
, 0);
13456 templ
= TREE_OPERAND (templ
, 1);
13459 object
= NULL_TREE
;
13460 templ
= lookup_template_function (templ
, targs
);
13463 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13464 object
, templ
, NULL_TREE
));
13466 RETURN (baselink_for_fns (templ
));
13471 tree r
= RECUR (TREE_OPERAND (t
, 0));
13473 if (REFERENCE_REF_P (t
))
13475 /* A type conversion to reference type will be enclosed in
13476 such an indirect ref, but the substitution of the cast
13477 will have also added such an indirect ref. */
13478 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13479 r
= convert_from_reference (r
);
13482 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13488 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13489 RECUR (TREE_OPERAND (t
, 0))));
13491 case IMPLICIT_CONV_EXPR
:
13493 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13494 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13495 int flags
= LOOKUP_IMPLICIT
;
13496 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13497 flags
= LOOKUP_NORMAL
;
13498 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13505 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13506 RECUR (TREE_OPERAND (t
, 0))));
13509 case REINTERPRET_CAST_EXPR
:
13510 case CONST_CAST_EXPR
:
13511 case DYNAMIC_CAST_EXPR
:
13512 case STATIC_CAST_EXPR
:
13515 tree op
, r
= NULL_TREE
;
13517 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13518 if (integral_constant_expression_p
13519 && !cast_valid_in_integral_constant_expression_p (type
))
13521 if (complain
& tf_error
)
13522 error ("a cast to a type other than an integral or "
13523 "enumeration type cannot appear in a constant-expression");
13524 RETURN (error_mark_node
);
13527 op
= RECUR (TREE_OPERAND (t
, 0));
13529 ++c_inhibit_evaluation_warnings
;
13530 switch (TREE_CODE (t
))
13533 r
= build_functional_cast (type
, op
, complain
);
13535 case REINTERPRET_CAST_EXPR
:
13536 r
= build_reinterpret_cast (type
, op
, complain
);
13538 case CONST_CAST_EXPR
:
13539 r
= build_const_cast (type
, op
, complain
);
13541 case DYNAMIC_CAST_EXPR
:
13542 r
= build_dynamic_cast (type
, op
, complain
);
13544 case STATIC_CAST_EXPR
:
13545 r
= build_static_cast (type
, op
, complain
);
13548 gcc_unreachable ();
13550 --c_inhibit_evaluation_warnings
;
13555 case POSTDECREMENT_EXPR
:
13556 case POSTINCREMENT_EXPR
:
13557 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13558 args
, complain
, in_decl
);
13559 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13561 case PREDECREMENT_EXPR
:
13562 case PREINCREMENT_EXPR
:
13566 case TRUTH_NOT_EXPR
:
13567 case UNARY_PLUS_EXPR
: /* Unary + */
13568 case REALPART_EXPR
:
13569 case IMAGPART_EXPR
:
13570 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13571 RECUR (TREE_OPERAND (t
, 0)), complain
));
13573 case FIX_TRUNC_EXPR
:
13574 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13578 op1
= TREE_OPERAND (t
, 0);
13579 if (TREE_CODE (op1
) == LABEL_DECL
)
13580 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13581 EXPR_LOCATION (op1
)));
13582 if (TREE_CODE (op1
) == SCOPE_REF
)
13583 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13584 /*done=*/true, /*address_p=*/true);
13586 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13588 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13593 case TRUNC_DIV_EXPR
:
13594 case CEIL_DIV_EXPR
:
13595 case FLOOR_DIV_EXPR
:
13596 case ROUND_DIV_EXPR
:
13597 case EXACT_DIV_EXPR
:
13601 case TRUNC_MOD_EXPR
:
13602 case FLOOR_MOD_EXPR
:
13603 case TRUTH_ANDIF_EXPR
:
13604 case TRUTH_ORIF_EXPR
:
13605 case TRUTH_AND_EXPR
:
13606 case TRUTH_OR_EXPR
:
13624 ++c_inhibit_evaluation_warnings
;
13626 r
= build_x_binary_op
13627 (input_location
, TREE_CODE (t
),
13628 RECUR (TREE_OPERAND (t
, 0)),
13629 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13631 : TREE_CODE (TREE_OPERAND (t
, 0))),
13632 RECUR (TREE_OPERAND (t
, 1)),
13633 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13635 : TREE_CODE (TREE_OPERAND (t
, 1))),
13638 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13639 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13641 --c_inhibit_evaluation_warnings
;
13647 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13648 /*address_p=*/false));
13650 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13651 args
, complain
, in_decl
);
13652 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13653 RECUR (TREE_OPERAND (t
, 1)), complain
));
13656 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13657 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13664 op1
= TREE_OPERAND (t
, 0);
13665 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13666 op1
= TREE_TYPE (op1
);
13669 /* When there are no ARGS, we are trying to evaluate a
13670 non-dependent expression from the parser. Trying to do
13671 the substitutions may not work. */
13673 op1
= TREE_TYPE (op1
);
13677 ++cp_unevaluated_operand
;
13678 ++c_inhibit_evaluation_warnings
;
13680 op1
= tsubst (op1
, args
, complain
, in_decl
);
13682 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13683 /*function_p=*/false,
13684 /*integral_constant_expression_p=*/
13686 --cp_unevaluated_operand
;
13687 --c_inhibit_evaluation_warnings
;
13690 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13691 complain
& tf_error
);
13693 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13694 complain
& tf_error
);
13695 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
13697 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
13699 if (!processing_template_decl
&& TYPE_P (op1
))
13701 r
= build_min (SIZEOF_EXPR
, size_type_node
,
13702 build1 (NOP_EXPR
, op1
, error_mark_node
));
13703 SIZEOF_EXPR_TYPE_P (r
) = 1;
13706 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
13707 TREE_SIDE_EFFECTS (r
) = 0;
13708 TREE_READONLY (r
) = 1;
13710 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
13715 case AT_ENCODE_EXPR
:
13717 op1
= TREE_OPERAND (t
, 0);
13718 ++cp_unevaluated_operand
;
13719 ++c_inhibit_evaluation_warnings
;
13720 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13721 /*function_p=*/false,
13722 /*integral_constant_expression_p=*/false);
13723 --cp_unevaluated_operand
;
13724 --c_inhibit_evaluation_warnings
;
13725 RETURN (objc_build_encode_expr (op1
));
13728 case NOEXCEPT_EXPR
:
13729 op1
= TREE_OPERAND (t
, 0);
13730 ++cp_unevaluated_operand
;
13731 ++c_inhibit_evaluation_warnings
;
13732 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13733 /*function_p=*/false,
13734 /*integral_constant_expression_p=*/false);
13735 --cp_unevaluated_operand
;
13736 --c_inhibit_evaluation_warnings
;
13737 RETURN (finish_noexcept_expr (op1
, complain
));
13741 tree r
= build_x_modify_expr
13742 (EXPR_LOCATION (t
),
13743 RECUR (TREE_OPERAND (t
, 0)),
13744 TREE_CODE (TREE_OPERAND (t
, 1)),
13745 RECUR (TREE_OPERAND (t
, 2)),
13747 /* TREE_NO_WARNING must be set if either the expression was
13748 parenthesized or it uses an operator such as >>= rather
13749 than plain assignment. In the former case, it was already
13750 set and must be copied. In the latter case,
13751 build_x_modify_expr sets it and it must not be reset
13753 if (TREE_NO_WARNING (t
))
13754 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13759 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13760 args
, complain
, in_decl
);
13761 /* Remember that there was a reference to this entity. */
13764 RETURN (build_x_arrow (input_location
, op1
, complain
));
13768 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13769 tree init
= RECUR (TREE_OPERAND (t
, 3));
13770 vec
<tree
, va_gc
> *placement_vec
;
13771 vec
<tree
, va_gc
> *init_vec
;
13774 if (placement
== NULL_TREE
)
13775 placement_vec
= NULL
;
13778 placement_vec
= make_tree_vector ();
13779 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13780 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
13783 /* If there was an initializer in the original tree, but it
13784 instantiated to an empty list, then we should pass a
13785 non-NULL empty vector to tell build_new that it was an
13786 empty initializer() rather than no initializer. This can
13787 only happen when the initializer is a pack expansion whose
13788 parameter packs are of length zero. */
13789 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13793 init_vec
= make_tree_vector ();
13794 if (init
== void_zero_node
)
13795 gcc_assert (init_vec
!= NULL
);
13798 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13799 vec_safe_push (init_vec
, TREE_VALUE (init
));
13803 ret
= build_new (&placement_vec
,
13804 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13805 RECUR (TREE_OPERAND (t
, 2)),
13807 NEW_EXPR_USE_GLOBAL (t
),
13810 if (placement_vec
!= NULL
)
13811 release_tree_vector (placement_vec
);
13812 if (init_vec
!= NULL
)
13813 release_tree_vector (init_vec
);
13819 RETURN (delete_sanity
13820 (RECUR (TREE_OPERAND (t
, 0)),
13821 RECUR (TREE_OPERAND (t
, 1)),
13822 DELETE_EXPR_USE_VEC (t
),
13823 DELETE_EXPR_USE_GLOBAL (t
),
13826 case COMPOUND_EXPR
:
13827 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13828 RECUR (TREE_OPERAND (t
, 0)),
13829 RECUR (TREE_OPERAND (t
, 1)),
13835 vec
<tree
, va_gc
> *call_args
;
13836 unsigned int nargs
, i
;
13841 function
= CALL_EXPR_FN (t
);
13842 /* When we parsed the expression, we determined whether or
13843 not Koenig lookup should be performed. */
13844 koenig_p
= KOENIG_LOOKUP_P (t
);
13845 if (TREE_CODE (function
) == SCOPE_REF
)
13847 qualified_p
= true;
13848 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13850 /*address_p=*/false);
13852 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13854 /* Do nothing; calling tsubst_copy_and_build on an identifier
13855 would incorrectly perform unqualified lookup again.
13857 Note that we can also have an IDENTIFIER_NODE if the earlier
13858 unqualified lookup found a member function; in that case
13859 koenig_p will be false and we do want to do the lookup
13860 again to find the instantiated member function.
13862 FIXME but doing that causes c++/15272, so we need to stop
13863 using IDENTIFIER_NODE in that situation. */
13864 qualified_p
= false;
13868 if (TREE_CODE (function
) == COMPONENT_REF
)
13870 tree op
= TREE_OPERAND (function
, 1);
13872 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13873 || (BASELINK_P (op
)
13874 && BASELINK_QUALIFIED_P (op
)));
13877 qualified_p
= false;
13879 if (TREE_CODE (function
) == ADDR_EXPR
13880 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
13881 /* Avoid error about taking the address of a constructor. */
13882 function
= TREE_OPERAND (function
, 0);
13884 function
= tsubst_copy_and_build (function
, args
, complain
,
13887 integral_constant_expression_p
);
13889 if (BASELINK_P (function
))
13890 qualified_p
= true;
13893 nargs
= call_expr_nargs (t
);
13894 call_args
= make_tree_vector ();
13895 for (i
= 0; i
< nargs
; ++i
)
13897 tree arg
= CALL_EXPR_ARG (t
, i
);
13899 if (!PACK_EXPANSION_P (arg
))
13900 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
13903 /* Expand the pack expansion and push each entry onto
13905 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13906 if (TREE_CODE (arg
) == TREE_VEC
)
13908 unsigned int len
, j
;
13910 len
= TREE_VEC_LENGTH (arg
);
13911 for (j
= 0; j
< len
; ++j
)
13913 tree value
= TREE_VEC_ELT (arg
, j
);
13914 if (value
!= NULL_TREE
)
13915 value
= convert_from_reference (value
);
13916 vec_safe_push (call_args
, value
);
13921 /* A partial substitution. Add one entry. */
13922 vec_safe_push (call_args
, arg
);
13927 /* We do not perform argument-dependent lookup if normal
13928 lookup finds a non-function, in accordance with the
13929 expected resolution of DR 218. */
13931 && ((is_overloaded_fn (function
)
13932 /* If lookup found a member function, the Koenig lookup is
13933 not appropriate, even if an unqualified-name was used
13934 to denote the function. */
13935 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13936 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13937 /* Only do this when substitution turns a dependent call
13938 into a non-dependent call. */
13939 && type_dependent_expression_p_push (t
)
13940 && !any_type_dependent_arguments_p (call_args
))
13941 function
= perform_koenig_lookup (function
, call_args
, false,
13944 if (TREE_CODE (function
) == IDENTIFIER_NODE
13945 && !any_type_dependent_arguments_p (call_args
))
13947 if (koenig_p
&& (complain
& tf_warning_or_error
))
13949 /* For backwards compatibility and good diagnostics, try
13950 the unqualified lookup again if we aren't in SFINAE
13952 tree unq
= (tsubst_copy_and_build
13953 (function
, args
, complain
, in_decl
, true,
13954 integral_constant_expression_p
));
13955 if (unq
== error_mark_node
)
13956 RETURN (error_mark_node
);
13958 if (unq
!= function
)
13961 if (TREE_CODE (fn
) == INDIRECT_REF
)
13962 fn
= TREE_OPERAND (fn
, 0);
13963 if (TREE_CODE (fn
) == COMPONENT_REF
)
13964 fn
= TREE_OPERAND (fn
, 1);
13965 if (is_overloaded_fn (fn
))
13966 fn
= get_first_fn (fn
);
13967 permerror (EXPR_LOC_OR_HERE (t
),
13968 "%qD was not declared in this scope, "
13969 "and no declarations were found by "
13970 "argument-dependent lookup at the point "
13971 "of instantiation", function
);
13973 /* Can't say anything more. */;
13974 else if (DECL_CLASS_SCOPE_P (fn
))
13976 inform (EXPR_LOC_OR_HERE (t
),
13977 "declarations in dependent base %qT are "
13978 "not found by unqualified lookup",
13979 DECL_CLASS_CONTEXT (fn
));
13980 if (current_class_ptr
)
13981 inform (EXPR_LOC_OR_HERE (t
),
13982 "use %<this->%D%> instead", function
);
13984 inform (EXPR_LOC_OR_HERE (t
),
13985 "use %<%T::%D%> instead",
13986 current_class_name
, function
);
13989 inform (0, "%q+D declared here, later in the "
13990 "translation unit", fn
);
13994 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13996 if (complain
& tf_error
)
13997 unqualified_name_lookup_error (function
);
13998 release_tree_vector (call_args
);
13999 RETURN (error_mark_node
);
14003 /* Remember that there was a reference to this entity. */
14004 if (DECL_P (function
))
14005 mark_used (function
);
14007 if (TREE_CODE (function
) == OFFSET_REF
)
14008 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14010 else if (TREE_CODE (function
) == COMPONENT_REF
)
14012 tree instance
= TREE_OPERAND (function
, 0);
14013 tree fn
= TREE_OPERAND (function
, 1);
14015 if (processing_template_decl
14016 && (type_dependent_expression_p (instance
)
14017 || (!BASELINK_P (fn
)
14018 && TREE_CODE (fn
) != FIELD_DECL
)
14019 || type_dependent_expression_p (fn
)
14020 || any_type_dependent_arguments_p (call_args
)))
14021 ret
= build_nt_call_vec (function
, call_args
);
14022 else if (!BASELINK_P (fn
))
14023 ret
= finish_call_expr (function
, &call_args
,
14024 /*disallow_virtual=*/false,
14025 /*koenig_p=*/false,
14028 ret
= (build_new_method_call
14030 &call_args
, NULL_TREE
,
14031 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14036 ret
= finish_call_expr (function
, &call_args
,
14037 /*disallow_virtual=*/qualified_p
,
14041 release_tree_vector (call_args
);
14048 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14051 if (TREE_CODE (cond
) == INTEGER_CST
)
14053 if (integer_zerop (cond
))
14055 ++c_inhibit_evaluation_warnings
;
14056 exp1
= RECUR (TREE_OPERAND (t
, 1));
14057 --c_inhibit_evaluation_warnings
;
14058 exp2
= RECUR (TREE_OPERAND (t
, 2));
14062 exp1
= RECUR (TREE_OPERAND (t
, 1));
14063 ++c_inhibit_evaluation_warnings
;
14064 exp2
= RECUR (TREE_OPERAND (t
, 2));
14065 --c_inhibit_evaluation_warnings
;
14070 exp1
= RECUR (TREE_OPERAND (t
, 1));
14071 exp2
= RECUR (TREE_OPERAND (t
, 2));
14074 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14075 cond
, exp1
, exp2
, complain
));
14078 case PSEUDO_DTOR_EXPR
:
14079 RETURN (finish_pseudo_destructor_expr
14080 (RECUR (TREE_OPERAND (t
, 0)),
14081 RECUR (TREE_OPERAND (t
, 1)),
14082 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
14086 tree purpose
, value
, chain
;
14088 if (t
== void_list_node
)
14091 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14092 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14094 /* We have pack expansions, so expand those and
14095 create a new list out of it. */
14096 tree purposevec
= NULL_TREE
;
14097 tree valuevec
= NULL_TREE
;
14101 /* Expand the argument expressions. */
14102 if (TREE_PURPOSE (t
))
14103 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14104 complain
, in_decl
);
14105 if (TREE_VALUE (t
))
14106 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14107 complain
, in_decl
);
14109 /* Build the rest of the list. */
14110 chain
= TREE_CHAIN (t
);
14111 if (chain
&& chain
!= void_type_node
)
14112 chain
= RECUR (chain
);
14114 /* Determine the number of arguments. */
14115 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14117 len
= TREE_VEC_LENGTH (purposevec
);
14118 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14120 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14121 len
= TREE_VEC_LENGTH (valuevec
);
14124 /* Since we only performed a partial substitution into
14125 the argument pack, we only RETURN (a single list
14127 if (purposevec
== TREE_PURPOSE (t
)
14128 && valuevec
== TREE_VALUE (t
)
14129 && chain
== TREE_CHAIN (t
))
14132 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14135 /* Convert the argument vectors into a TREE_LIST */
14139 /* Grab the Ith values. */
14141 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14144 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14147 /* Build the list (backwards). */
14148 chain
= tree_cons (purpose
, value
, chain
);
14154 purpose
= TREE_PURPOSE (t
);
14156 purpose
= RECUR (purpose
);
14157 value
= TREE_VALUE (t
);
14159 value
= RECUR (value
);
14160 chain
= TREE_CHAIN (t
);
14161 if (chain
&& chain
!= void_type_node
)
14162 chain
= RECUR (chain
);
14163 if (purpose
== TREE_PURPOSE (t
)
14164 && value
== TREE_VALUE (t
)
14165 && chain
== TREE_CHAIN (t
))
14167 RETURN (tree_cons (purpose
, value
, chain
));
14170 case COMPONENT_REF
:
14176 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14177 args
, complain
, in_decl
);
14178 /* Remember that there was a reference to this entity. */
14179 if (DECL_P (object
))
14180 mark_used (object
);
14181 object_type
= TREE_TYPE (object
);
14183 member
= TREE_OPERAND (t
, 1);
14184 if (BASELINK_P (member
))
14185 member
= tsubst_baselink (member
,
14186 non_reference (TREE_TYPE (object
)),
14187 args
, complain
, in_decl
);
14189 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14190 if (member
== error_mark_node
)
14191 RETURN (error_mark_node
);
14193 if (type_dependent_expression_p (object
))
14194 /* We can't do much here. */;
14195 else if (!CLASS_TYPE_P (object_type
))
14197 if (scalarish_type_p (object_type
))
14199 tree s
= NULL_TREE
;
14200 tree dtor
= member
;
14202 if (TREE_CODE (dtor
) == SCOPE_REF
)
14204 s
= TREE_OPERAND (dtor
, 0);
14205 dtor
= TREE_OPERAND (dtor
, 1);
14207 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14209 dtor
= TREE_OPERAND (dtor
, 0);
14211 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
14215 else if (TREE_CODE (member
) == SCOPE_REF
14216 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14218 /* Lookup the template functions now that we know what the
14220 tree scope
= TREE_OPERAND (member
, 0);
14221 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14222 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14223 member
= lookup_qualified_name (scope
, tmpl
,
14224 /*is_type_p=*/false,
14225 /*complain=*/false);
14226 if (BASELINK_P (member
))
14228 BASELINK_FUNCTIONS (member
)
14229 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14231 member
= (adjust_result_of_qualified_name_lookup
14232 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14237 qualified_name_lookup_error (scope
, tmpl
, member
,
14239 RETURN (error_mark_node
);
14242 else if (TREE_CODE (member
) == SCOPE_REF
14243 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14244 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14246 if (complain
& tf_error
)
14248 if (TYPE_P (TREE_OPERAND (member
, 0)))
14249 error ("%qT is not a class or namespace",
14250 TREE_OPERAND (member
, 0));
14252 error ("%qD is not a class or namespace",
14253 TREE_OPERAND (member
, 0));
14255 RETURN (error_mark_node
);
14257 else if (TREE_CODE (member
) == FIELD_DECL
)
14258 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14260 RETURN (finish_class_member_access_expr (object
, member
,
14261 /*template_p=*/false,
14266 RETURN (build_throw
14267 (RECUR (TREE_OPERAND (t
, 0))));
14271 vec
<constructor_elt
, va_gc
> *n
;
14272 constructor_elt
*ce
;
14273 unsigned HOST_WIDE_INT idx
;
14274 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14275 bool process_index_p
;
14277 bool need_copy_p
= false;
14280 if (type
== error_mark_node
)
14281 RETURN (error_mark_node
);
14283 /* digest_init will do the wrong thing if we let it. */
14284 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14287 /* We do not want to process the index of aggregate
14288 initializers as they are identifier nodes which will be
14289 looked up by digest_init. */
14290 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14292 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14293 newlen
= vec_safe_length (n
);
14294 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14296 if (ce
->index
&& process_index_p
)
14297 ce
->index
= RECUR (ce
->index
);
14299 if (PACK_EXPANSION_P (ce
->value
))
14301 /* Substitute into the pack expansion. */
14302 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14305 if (ce
->value
== error_mark_node
14306 || PACK_EXPANSION_P (ce
->value
))
14308 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14309 /* Just move the argument into place. */
14310 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14313 /* Update the length of the final CONSTRUCTOR
14314 arguments vector, and note that we will need to
14316 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14317 need_copy_p
= true;
14321 ce
->value
= RECUR (ce
->value
);
14326 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14328 vec_alloc (n
, newlen
);
14329 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14331 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14333 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14334 for (i
= 0; i
< len
; ++i
)
14335 CONSTRUCTOR_APPEND_ELT (n
, 0,
14336 TREE_VEC_ELT (ce
->value
, i
));
14339 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14343 r
= build_constructor (init_list_type_node
, n
);
14344 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14346 if (TREE_HAS_CONSTRUCTOR (t
))
14347 RETURN (finish_compound_literal (type
, r
, complain
));
14349 TREE_TYPE (r
) = type
;
14355 tree operand_0
= TREE_OPERAND (t
, 0);
14356 if (TYPE_P (operand_0
))
14358 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14359 RETURN (get_typeid (operand_0
, complain
));
14363 operand_0
= RECUR (operand_0
);
14364 RETURN (build_typeid (operand_0
, complain
));
14375 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14377 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14378 /* If the original type was a reference, we'll be wrapped in
14379 the appropriate INDIRECT_REF. */
14380 r
= convert_from_reference (r
);
14385 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14386 RECUR (TREE_OPERAND (t
, 0)),
14387 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14389 case OFFSETOF_EXPR
:
14390 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14394 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14395 complain
, in_decl
);
14397 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14399 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14401 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14406 tree old_stmt_expr
= cur_stmt_expr
;
14407 tree stmt_expr
= begin_stmt_expr ();
14409 cur_stmt_expr
= stmt_expr
;
14410 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14411 integral_constant_expression_p
);
14412 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14413 cur_stmt_expr
= old_stmt_expr
;
14415 /* If the resulting list of expression statement is empty,
14416 fold it further into void_zero_node. */
14417 if (empty_expr_stmt_p (stmt_expr
))
14418 stmt_expr
= void_zero_node
;
14420 RETURN (stmt_expr
);
14425 tree r
= build_lambda_expr ();
14427 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14428 LAMBDA_EXPR_CLOSURE (r
) = type
;
14429 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14431 LAMBDA_EXPR_LOCATION (r
)
14432 = LAMBDA_EXPR_LOCATION (t
);
14433 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14434 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14435 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14436 LAMBDA_EXPR_DISCRIMINATOR (r
)
14437 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14438 /* For a function scope, we want to use tsubst so that we don't
14439 complain about referring to an auto function before its return
14440 type has been deduced. Otherwise, we want to use tsubst_copy so
14441 that we look up the existing field/parameter/variable rather
14442 than build a new one. */
14443 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14444 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14445 scope
= tsubst (scope
, args
, complain
, in_decl
);
14446 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14448 /* Look up the parameter we want directly, as tsubst_copy
14449 doesn't do what we need. */
14450 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14451 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14452 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14453 parm
= DECL_CHAIN (parm
);
14455 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14456 if (DECL_CONTEXT (scope
) == NULL_TREE
)
14457 DECL_CONTEXT (scope
) = fn
;
14460 scope
= RECUR (scope
);
14461 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14462 LAMBDA_EXPR_RETURN_TYPE (r
)
14463 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14465 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14466 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14468 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14469 determine_visibility (TYPE_NAME (type
));
14470 /* Now that we know visibility, instantiate the type so we have a
14471 declaration of the op() for later calls to lambda_function. */
14472 complete_type (type
);
14474 /* The capture list refers to closure members, so this needs to
14475 wait until after we finish instantiating the type. Also keep
14476 any captures that may have been added during instantiation. */
14477 LAMBDA_EXPR_CAPTURE_LIST (r
)
14478 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
)),
14479 LAMBDA_EXPR_CAPTURE_LIST (r
));
14480 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14482 RETURN (build_lambda_object (r
));
14486 /* We can get here for a constant initializer of non-dependent type.
14487 FIXME stop folding in cp_parser_initializer_clause. */
14489 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14494 case TRANSACTION_EXPR
:
14495 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14496 integral_constant_expression_p
));
14499 /* Handle Objective-C++ constructs, if appropriate. */
14502 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14503 in_decl
, /*function_p=*/false);
14507 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14513 input_location
= loc
;
14517 /* Verify that the instantiated ARGS are valid. For type arguments,
14518 make sure that the type's linkage is ok. For non-type arguments,
14519 make sure they are constants if they are integral or enumerations.
14520 Emit an error under control of COMPLAIN, and return TRUE on error. */
14523 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14525 if (dependent_template_arg_p (t
))
14527 if (ARGUMENT_PACK_P (t
))
14529 tree vec
= ARGUMENT_PACK_ARGS (t
);
14530 int len
= TREE_VEC_LENGTH (vec
);
14531 bool result
= false;
14534 for (i
= 0; i
< len
; ++i
)
14535 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14539 else if (TYPE_P (t
))
14541 /* [basic.link]: A name with no linkage (notably, the name
14542 of a class or enumeration declared in a local scope)
14543 shall not be used to declare an entity with linkage.
14544 This implies that names with no linkage cannot be used as
14547 DR 757 relaxes this restriction for C++0x. */
14548 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14549 : no_linkage_check (t
, /*relaxed_p=*/false));
14553 /* DR 488 makes use of a type with no linkage cause
14554 type deduction to fail. */
14555 if (complain
& tf_error
)
14557 if (TYPE_ANONYMOUS_P (nt
))
14558 error ("%qT is/uses anonymous type", t
);
14560 error ("template argument for %qD uses local type %qT",
14565 /* In order to avoid all sorts of complications, we do not
14566 allow variably-modified types as template arguments. */
14567 else if (variably_modified_type_p (t
, NULL_TREE
))
14569 if (complain
& tf_error
)
14570 error ("%qT is a variably modified type", t
);
14574 /* Class template and alias template arguments should be OK. */
14575 else if (DECL_TYPE_TEMPLATE_P (t
))
14577 /* A non-type argument of integral or enumerated type must be a
14579 else if (TREE_TYPE (t
)
14580 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14581 && !TREE_CONSTANT (t
))
14583 if (complain
& tf_error
)
14584 error ("integral expression %qE is not constant", t
);
14591 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14593 int ix
, len
= DECL_NTPARMS (tmpl
);
14594 bool result
= false;
14596 for (ix
= 0; ix
!= len
; ix
++)
14598 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14601 if (result
&& (complain
& tf_error
))
14602 error (" trying to instantiate %qD", tmpl
);
14606 /* We're out of SFINAE context now, so generate diagnostics for the access
14607 errors we saw earlier when instantiating D from TMPL and ARGS. */
14610 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14612 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14613 tree type
= TREE_TYPE (pattern
);
14614 location_t loc
= input_location
;
14616 push_access_scope (d
);
14617 push_deferring_access_checks (dk_no_deferred
);
14618 input_location
= DECL_SOURCE_LOCATION (pattern
);
14619 tsubst (type
, args
, tf_warning_or_error
, d
);
14620 input_location
= loc
;
14621 pop_deferring_access_checks ();
14622 pop_access_scope (d
);
14625 /* Instantiate the indicated variable, function, or alias template TMPL with
14626 the template arguments in TARG_PTR. */
14629 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14631 tree targ_ptr
= orig_args
;
14635 bool access_ok
= true;
14637 if (tmpl
== error_mark_node
)
14638 return error_mark_node
;
14640 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14642 /* If this function is a clone, handle it specially. */
14643 if (DECL_CLONED_FUNCTION_P (tmpl
))
14648 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14649 DECL_CLONED_FUNCTION. */
14650 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14651 targ_ptr
, complain
);
14652 if (spec
== error_mark_node
)
14653 return error_mark_node
;
14655 /* Look for the clone. */
14656 FOR_EACH_CLONE (clone
, spec
)
14657 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14659 /* We should always have found the clone by now. */
14660 gcc_unreachable ();
14664 /* Check to see if we already have this specialization. */
14665 gen_tmpl
= most_general_template (tmpl
);
14666 if (tmpl
!= gen_tmpl
)
14667 /* The TMPL is a partial instantiation. To get a full set of
14668 arguments we must add the arguments used to perform the
14669 partial instantiation. */
14670 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14673 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14674 but it doesn't seem to be on the hot path. */
14675 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14677 gcc_assert (tmpl
== gen_tmpl
14678 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14680 || fndecl
== NULL_TREE
);
14682 if (spec
!= NULL_TREE
)
14684 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14686 if (complain
& tf_error
)
14687 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14688 return error_mark_node
;
14693 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14695 return error_mark_node
;
14697 /* We are building a FUNCTION_DECL, during which the access of its
14698 parameters and return types have to be checked. However this
14699 FUNCTION_DECL which is the desired context for access checking
14700 is not built yet. We solve this chicken-and-egg problem by
14701 deferring all checks until we have the FUNCTION_DECL. */
14702 push_deferring_access_checks (dk_deferred
);
14704 /* Instantiation of the function happens in the context of the function
14705 template, not the context of the overload resolution we're doing. */
14706 push_to_top_level ();
14707 /* If there are dependent arguments, e.g. because we're doing partial
14708 ordering, make sure processing_template_decl stays set. */
14709 if (uses_template_parms (targ_ptr
))
14710 ++processing_template_decl
;
14711 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14713 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14714 complain
, gen_tmpl
);
14715 push_nested_class (ctx
);
14717 /* Substitute template parameters to obtain the specialization. */
14718 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14719 targ_ptr
, complain
, gen_tmpl
);
14720 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14721 pop_nested_class ();
14722 pop_from_top_level ();
14724 if (fndecl
== error_mark_node
)
14726 pop_deferring_access_checks ();
14727 return error_mark_node
;
14730 /* The DECL_TI_TEMPLATE should always be the immediate parent
14731 template, not the most general template. */
14732 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14734 /* Now we know the specialization, compute access previously
14736 push_access_scope (fndecl
);
14737 if (!perform_deferred_access_checks (complain
))
14739 pop_access_scope (fndecl
);
14740 pop_deferring_access_checks ();
14742 /* If we've just instantiated the main entry point for a function,
14743 instantiate all the alternate entry points as well. We do this
14744 by cloning the instantiation of the main entry point, not by
14745 instantiating the template clones. */
14746 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14747 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14751 if (!(complain
& tf_error
))
14753 /* Remember to reinstantiate when we're out of SFINAE so the user
14754 can see the errors. */
14755 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14757 return error_mark_node
;
14762 /* Wrapper for instantiate_template_1. */
14765 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14768 timevar_push (TV_TEMPLATE_INST
);
14769 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14770 timevar_pop (TV_TEMPLATE_INST
);
14774 /* Instantiate the alias template TMPL with ARGS. Also push a template
14775 instantiation level, which instantiate_template doesn't do because
14776 functions and variables have sufficient context established by the
14780 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14782 struct pending_template
*old_last_pend
= last_pending_template
;
14783 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14784 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14785 return error_mark_node
;
14786 tree tinst
= build_tree_list (tmpl
, args
);
14787 if (!push_tinst_level (tinst
))
14790 return error_mark_node
;
14794 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
14795 args
, tmpl
, complain
,
14796 /*require_all_args=*/true,
14797 /*use_default_args=*/true);
14799 tree r
= instantiate_template (tmpl
, args
, complain
);
14800 pop_tinst_level ();
14801 /* We can't free this if a pending_template entry or last_error_tinst_level
14802 is pointing at it. */
14803 if (last_pending_template
== old_last_pend
14804 && last_error_tinst_level
== old_error_tinst
)
14810 /* PARM is a template parameter pack for FN. Returns true iff
14811 PARM is used in a deducible way in the argument list of FN. */
14814 pack_deducible_p (tree parm
, tree fn
)
14816 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14817 for (; t
; t
= TREE_CHAIN (t
))
14819 tree type
= TREE_VALUE (t
);
14821 if (!PACK_EXPANSION_P (type
))
14823 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14824 packs
; packs
= TREE_CHAIN (packs
))
14825 if (TREE_VALUE (packs
) == parm
)
14827 /* The template parameter pack is used in a function parameter
14828 pack. If this is the end of the parameter list, the
14829 template parameter pack is deducible. */
14830 if (TREE_CHAIN (t
) == void_list_node
)
14833 /* Otherwise, not. Well, it could be deduced from
14834 a non-pack parameter, but doing so would end up with
14835 a deduction mismatch, so don't bother. */
14839 /* The template parameter pack isn't used in any function parameter
14840 packs, but it might be used deeper, e.g. tuple<Args...>. */
14844 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14845 NARGS elements of the arguments that are being used when calling
14846 it. TARGS is a vector into which the deduced template arguments
14849 Return zero for success, 2 for an incomplete match that doesn't resolve
14850 all the types, and 1 for complete failure. An error message will be
14851 printed only for an incomplete match.
14853 If FN is a conversion operator, or we are trying to produce a specific
14854 specialization, RETURN_TYPE is the return type desired.
14856 The EXPLICIT_TARGS are explicit template arguments provided via a
14859 The parameter STRICT is one of:
14862 We are deducing arguments for a function call, as in
14863 [temp.deduct.call].
14866 We are deducing arguments for a conversion function, as in
14867 [temp.deduct.conv].
14870 We are deducing arguments when doing an explicit instantiation
14871 as in [temp.explicit], when determining an explicit specialization
14872 as in [temp.expl.spec], or when taking the address of a function
14873 template, as in [temp.deduct.funcaddr]. */
14876 fn_type_unification (tree fn
,
14877 tree explicit_targs
,
14880 unsigned int nargs
,
14882 unification_kind_t strict
,
14888 tree decl
= NULL_TREE
;
14889 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14891 static int deduction_depth
;
14892 struct pending_template
*old_last_pend
= last_pending_template
;
14893 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14894 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14896 tree r
= error_mark_node
;
14898 /* In C++0x, it's possible to have a function template whose type depends
14899 on itself recursively. This is most obvious with decltype, but can also
14900 occur with enumeration scope (c++/48969). So we need to catch infinite
14901 recursion and reject the substitution at deduction time; this function
14902 will return error_mark_node for any repeated substitution.
14904 This also catches excessive recursion such as when f<N> depends on
14905 f<N-1> across all integers, and returns error_mark_node for all the
14906 substitutions back up to the initial one.
14908 This is, of course, not reentrant. */
14909 if (excessive_deduction_depth
)
14910 return error_mark_node
;
14911 tinst
= build_tree_list (fn
, NULL_TREE
);
14913 push_deferring_access_checks (dk_deferred
);
14915 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14917 fntype
= TREE_TYPE (fn
);
14918 if (explicit_targs
)
14922 The specified template arguments must match the template
14923 parameters in kind (i.e., type, nontype, template), and there
14924 must not be more arguments than there are parameters;
14925 otherwise type deduction fails.
14927 Nontype arguments must match the types of the corresponding
14928 nontype template parameters, or must be convertible to the
14929 types of the corresponding nontype parameters as specified in
14930 _temp.arg.nontype_, otherwise type deduction fails.
14932 All references in the function type of the function template
14933 to the corresponding template parameters are replaced by the
14934 specified template argument values. If a substitution in a
14935 template parameter or in the function type of the function
14936 template results in an invalid type, type deduction fails. */
14937 int i
, len
= TREE_VEC_LENGTH (tparms
);
14938 location_t loc
= input_location
;
14939 bool incomplete
= false;
14941 /* Adjust any explicit template arguments before entering the
14942 substitution context. */
14944 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14946 /*require_all_args=*/false,
14947 /*use_default_args=*/false));
14948 if (explicit_targs
== error_mark_node
)
14951 /* Substitute the explicit args into the function type. This is
14952 necessary so that, for instance, explicitly declared function
14953 arguments can match null pointed constants. If we were given
14954 an incomplete set of explicit args, we must not do semantic
14955 processing during substitution as we could create partial
14957 for (i
= 0; i
< len
; i
++)
14959 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14960 bool parameter_pack
= false;
14961 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
14963 /* Dig out the actual parm. */
14964 if (TREE_CODE (parm
) == TYPE_DECL
14965 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14967 parm
= TREE_TYPE (parm
);
14968 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14970 else if (TREE_CODE (parm
) == PARM_DECL
)
14972 parm
= DECL_INITIAL (parm
);
14973 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14976 if (!parameter_pack
&& targ
== NULL_TREE
)
14977 /* No explicit argument for this template parameter. */
14980 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14982 /* Mark the argument pack as "incomplete". We could
14983 still deduce more arguments during unification.
14984 We remove this mark in type_unification_real. */
14987 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14988 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14989 = ARGUMENT_PACK_ARGS (targ
);
14992 /* We have some incomplete argument packs. */
14997 TREE_VALUE (tinst
) = explicit_targs
;
14998 if (!push_tinst_level (tinst
))
15000 excessive_deduction_depth
= true;
15003 processing_template_decl
+= incomplete
;
15004 input_location
= DECL_SOURCE_LOCATION (fn
);
15005 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15006 complain
| tf_partial
, NULL_TREE
);
15007 input_location
= loc
;
15008 processing_template_decl
-= incomplete
;
15009 pop_tinst_level ();
15011 if (fntype
== error_mark_node
)
15014 /* Throw away these access checks; we'll see them again in
15015 instantiate_template and they might have the wrong
15016 access path at this point. */
15017 pop_deferring_access_checks ();
15018 push_deferring_access_checks (dk_deferred
);
15020 /* Place the explicitly specified arguments in TARGS. */
15021 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15022 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15025 /* Never do unification on the 'this' parameter. */
15026 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15032 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15033 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15034 new_args
[0] = return_type
;
15035 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15040 /* We allow incomplete unification without an error message here
15041 because the standard doesn't seem to explicitly prohibit it. Our
15042 callers must be ready to deal with unification failures in any
15045 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15046 targs
, parms
, args
, nargs
, /*subr=*/0,
15047 strict
, flags
, explain_p
);
15051 /* Now that we have bindings for all of the template arguments,
15052 ensure that the arguments deduced for the template template
15053 parameters have compatible template parameter lists. We cannot
15054 check this property before we have deduced all template
15055 arguments, because the template parameter types of a template
15056 template parameter might depend on prior template parameters
15057 deduced after the template template parameter. The following
15058 ill-formed example illustrates this issue:
15060 template<typename T, template<T> class C> void f(C<5>, T);
15062 template<int N> struct X {};
15065 f(X<5>(), 5l); // error: template argument deduction fails
15068 The template parameter list of 'C' depends on the template type
15069 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15070 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15071 time that we deduce 'C'. */
15072 if (!template_template_parm_bindings_ok_p
15073 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15075 unify_inconsistent_template_template_parameters (explain_p
);
15079 /* All is well so far. Now, check:
15083 When all template arguments have been deduced, all uses of
15084 template parameters in nondeduced contexts are replaced with
15085 the corresponding deduced argument values. If the
15086 substitution results in an invalid type, as described above,
15087 type deduction fails. */
15088 TREE_VALUE (tinst
) = targs
;
15089 if (!push_tinst_level (tinst
))
15091 excessive_deduction_depth
= true;
15094 decl
= instantiate_template (fn
, targs
, complain
);
15095 pop_tinst_level ();
15097 if (decl
== error_mark_node
)
15100 /* Now perform any access checks encountered during deduction, such as
15101 for default template arguments. */
15102 push_access_scope (decl
);
15103 ok
= perform_deferred_access_checks (complain
);
15104 pop_access_scope (decl
);
15108 /* If we're looking for an exact match, check that what we got
15109 is indeed an exact match. It might not be if some template
15110 parameters are used in non-deduced contexts. */
15111 if (strict
== DEDUCE_EXACT
)
15113 tree substed
= TREE_TYPE (decl
);
15117 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15119 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15120 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15121 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15123 unify_type_mismatch (explain_p
, args
[i
],
15124 TREE_VALUE (sarg
));
15132 pop_deferring_access_checks ();
15134 if (excessive_deduction_depth
)
15136 if (deduction_depth
== 0)
15137 /* Reset once we're all the way out. */
15138 excessive_deduction_depth
= false;
15141 /* We can't free this if a pending_template entry or last_error_tinst_level
15142 is pointing at it. */
15143 if (last_pending_template
== old_last_pend
15144 && last_error_tinst_level
== old_error_tinst
)
15150 /* Adjust types before performing type deduction, as described in
15151 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15152 sections are symmetric. PARM is the type of a function parameter
15153 or the return type of the conversion function. ARG is the type of
15154 the argument passed to the call, or the type of the value
15155 initialized with the result of the conversion function.
15156 ARG_EXPR is the original argument expression, which may be null. */
15159 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15173 /* Swap PARM and ARG throughout the remainder of this
15174 function; the handling is precisely symmetric since PARM
15175 will initialize ARG rather than vice versa. */
15183 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15184 too, but here handle it by stripping the reference from PARM
15185 rather than by adding it to ARG. */
15186 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15187 && TYPE_REF_IS_RVALUE (*parm
)
15188 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15189 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15190 && TREE_CODE (*arg
) == REFERENCE_TYPE
15191 && !TYPE_REF_IS_RVALUE (*arg
))
15192 *parm
= TREE_TYPE (*parm
);
15193 /* Nothing else to do in this case. */
15197 gcc_unreachable ();
15200 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15202 /* [temp.deduct.call]
15204 If P is not a reference type:
15206 --If A is an array type, the pointer type produced by the
15207 array-to-pointer standard conversion (_conv.array_) is
15208 used in place of A for type deduction; otherwise,
15210 --If A is a function type, the pointer type produced by
15211 the function-to-pointer standard conversion
15212 (_conv.func_) is used in place of A for type deduction;
15215 --If A is a cv-qualified type, the top level
15216 cv-qualifiers of A's type are ignored for type
15218 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15219 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15220 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15221 *arg
= build_pointer_type (*arg
);
15223 *arg
= TYPE_MAIN_VARIANT (*arg
);
15226 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15227 of the form T&&, where T is a template parameter, and the argument
15228 is an lvalue, T is deduced as A& */
15229 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15230 && TYPE_REF_IS_RVALUE (*parm
)
15231 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15232 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15233 && (arg_expr
? real_lvalue_p (arg_expr
)
15234 /* try_one_overload doesn't provide an arg_expr, but
15235 functions are always lvalues. */
15236 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15237 *arg
= build_reference_type (*arg
);
15239 /* [temp.deduct.call]
15241 If P is a cv-qualified type, the top level cv-qualifiers
15242 of P's type are ignored for type deduction. If P is a
15243 reference type, the type referred to by P is used for
15245 *parm
= TYPE_MAIN_VARIANT (*parm
);
15246 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15248 *parm
= TREE_TYPE (*parm
);
15249 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15252 /* DR 322. For conversion deduction, remove a reference type on parm
15253 too (which has been swapped into ARG). */
15254 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15255 *arg
= TREE_TYPE (*arg
);
15260 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15261 template which does contain any deducible template parameters; check if
15262 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15263 unify_one_argument. */
15266 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15267 int flags
, bool explain_p
)
15272 type
= TREE_TYPE (arg
);
15276 if (same_type_p (parm
, type
))
15277 return unify_success (explain_p
);
15279 if (strict
== DEDUCE_CONV
)
15281 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15282 explain_p
? tf_warning_or_error
: tf_none
))
15283 return unify_success (explain_p
);
15285 else if (strict
!= DEDUCE_EXACT
)
15287 if (can_convert_arg (parm
, type
,
15288 TYPE_P (arg
) ? NULL_TREE
: arg
,
15289 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15290 return unify_success (explain_p
);
15293 if (strict
== DEDUCE_EXACT
)
15294 return unify_type_mismatch (explain_p
, parm
, arg
);
15296 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15299 /* Subroutine of type_unification_real and unify_pack_expansion to
15300 handle unification of a single P/A pair. Parameters are as
15301 for those functions. */
15304 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15305 int subr
, unification_kind_t strict
, int flags
,
15308 tree arg_expr
= NULL_TREE
;
15311 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15312 return unify_invalid (explain_p
);
15313 if (arg
== unknown_type_node
)
15314 /* We can't deduce anything from this, but we might get all the
15315 template args from other function args. */
15316 return unify_success (explain_p
);
15318 /* FIXME uses_deducible_template_parms */
15319 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15320 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15326 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15327 | UNIFY_ALLOW_MORE_CV_QUAL
15328 | UNIFY_ALLOW_DERIVED
);
15332 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15336 arg_strict
= UNIFY_ALLOW_NONE
;
15340 gcc_unreachable ();
15343 /* We only do these transformations if this is the top-level
15344 parameter_type_list in a call or declaration matching; in other
15345 situations (nested function declarators, template argument lists) we
15346 won't be comparing a type to an expression, and we don't do any type
15352 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15353 if (type_unknown_p (arg
))
15355 /* [temp.deduct.type] A template-argument can be
15356 deduced from a pointer to function or pointer
15357 to member function argument if the set of
15358 overloaded functions does not contain function
15359 templates and at most one of a set of
15360 overloaded functions provides a unique
15363 if (resolve_overloaded_unification
15364 (tparms
, targs
, parm
, arg
, strict
,
15365 arg_strict
, explain_p
))
15366 return unify_success (explain_p
);
15367 return unify_overload_resolution_failure (explain_p
, arg
);
15371 arg
= unlowered_expr_type (arg
);
15372 if (arg
== error_mark_node
)
15373 return unify_invalid (explain_p
);
15377 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15380 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15381 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15383 /* For deduction from an init-list we need the actual list. */
15384 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15386 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15389 /* Most parms like fn_type_unification.
15391 If SUBR is 1, we're being called recursively (to unify the
15392 arguments of a function or method parameter of a function
15396 type_unification_real (tree tparms
,
15400 unsigned int xnargs
,
15402 unification_kind_t strict
,
15408 int ntparms
= TREE_VEC_LENGTH (tparms
);
15409 int saw_undeduced
= 0;
15412 unsigned int nargs
;
15415 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15416 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15417 gcc_assert (ntparms
> 0);
15419 /* Reset the number of non-defaulted template arguments contained
15421 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15429 while (parms
&& parms
!= void_list_node
15432 parm
= TREE_VALUE (parms
);
15434 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15435 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15436 /* For a function parameter pack that occurs at the end of the
15437 parameter-declaration-list, the type A of each remaining
15438 argument of the call is compared with the type P of the
15439 declarator-id of the function parameter pack. */
15442 parms
= TREE_CHAIN (parms
);
15444 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15445 /* For a function parameter pack that does not occur at the
15446 end of the parameter-declaration-list, the type of the
15447 parameter pack is a non-deduced context. */
15453 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15459 && parms
!= void_list_node
15460 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15462 /* Unify the remaining arguments with the pack expansion type. */
15464 tree parmvec
= make_tree_vec (1);
15466 /* Allocate a TREE_VEC and copy in all of the arguments */
15467 argvec
= make_tree_vec (nargs
- ia
);
15468 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15469 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15471 /* Copy the parameter into parmvec. */
15472 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15473 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15474 /*subr=*/subr
, explain_p
))
15477 /* Advance to the end of the list of parameters. */
15478 parms
= TREE_CHAIN (parms
);
15481 /* Fail if we've reached the end of the parm list, and more args
15482 are present, and the parm list isn't variadic. */
15483 if (ia
< nargs
&& parms
== void_list_node
)
15484 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15485 /* Fail if parms are left and they don't have default values. */
15486 if (parms
&& parms
!= void_list_node
15487 && TREE_PURPOSE (parms
) == NULL_TREE
)
15489 unsigned int count
= nargs
;
15491 while (p
&& p
!= void_list_node
)
15494 p
= TREE_CHAIN (p
);
15496 return unify_too_few_arguments (explain_p
, ia
, count
);
15501 tsubst_flags_t complain
= (explain_p
15502 ? tf_warning_or_error
15505 for (i
= 0; i
< ntparms
; i
++)
15507 tree targ
= TREE_VEC_ELT (targs
, i
);
15508 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15510 /* Clear the "incomplete" flags on all argument packs now so that
15511 substituting them into later default arguments works. */
15512 if (targ
&& ARGUMENT_PACK_P (targ
))
15514 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15515 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15518 if (targ
|| tparm
== error_mark_node
)
15520 tparm
= TREE_VALUE (tparm
);
15522 /* If this is an undeduced nontype parameter that depends on
15523 a type parameter, try another pass; its type may have been
15524 deduced from a later argument than the one from which
15525 this parameter can be deduced. */
15526 if (TREE_CODE (tparm
) == PARM_DECL
15527 && uses_template_parms (TREE_TYPE (tparm
))
15528 && !saw_undeduced
++)
15531 /* Core issue #226 (C++0x) [temp.deduct]:
15533 If a template argument has not been deduced, its
15534 default template argument, if any, is used.
15536 When we are in C++98 mode, TREE_PURPOSE will either
15537 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15538 to explicitly check cxx_dialect here. */
15539 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15541 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15542 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15543 location_t save_loc
= input_location
;
15545 input_location
= DECL_SOURCE_LOCATION (parm
);
15546 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15547 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15549 input_location
= save_loc
;
15550 if (arg
== error_mark_node
)
15554 TREE_VEC_ELT (targs
, i
) = arg
;
15555 /* The position of the first default template argument,
15556 is also the number of non-defaulted arguments in TARGS.
15558 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15559 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15564 /* If the type parameter is a parameter pack, then it will
15565 be deduced to an empty parameter pack. */
15566 if (template_parameter_pack_p (tparm
))
15570 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15572 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15573 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15574 TREE_CONSTANT (arg
) = 1;
15577 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15579 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15581 TREE_VEC_ELT (targs
, i
) = arg
;
15585 return unify_parameter_deduction_failure (explain_p
, tparm
);
15588 #ifdef ENABLE_CHECKING
15589 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15590 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15593 return unify_success (explain_p
);
15596 /* Subroutine of type_unification_real. Args are like the variables
15597 at the call site. ARG is an overloaded function (or template-id);
15598 we try deducing template args from each of the overloads, and if
15599 only one succeeds, we go with that. Modifies TARGS and returns
15600 true on success. */
15603 resolve_overloaded_unification (tree tparms
,
15607 unification_kind_t strict
,
15611 tree tempargs
= copy_node (targs
);
15613 tree goodfn
= NULL_TREE
;
15616 if (TREE_CODE (arg
) == ADDR_EXPR
)
15618 arg
= TREE_OPERAND (arg
, 0);
15624 if (TREE_CODE (arg
) == COMPONENT_REF
)
15625 /* Handle `&x' where `x' is some static or non-static member
15627 arg
= TREE_OPERAND (arg
, 1);
15629 if (TREE_CODE (arg
) == OFFSET_REF
)
15630 arg
= TREE_OPERAND (arg
, 1);
15632 /* Strip baselink information. */
15633 if (BASELINK_P (arg
))
15634 arg
= BASELINK_FUNCTIONS (arg
);
15636 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15638 /* If we got some explicit template args, we need to plug them into
15639 the affected templates before we try to unify, in case the
15640 explicit args will completely resolve the templates in question. */
15643 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15644 arg
= TREE_OPERAND (arg
, 0);
15646 for (; arg
; arg
= OVL_NEXT (arg
))
15648 tree fn
= OVL_CURRENT (arg
);
15649 tree subargs
, elem
;
15651 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15654 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15655 expl_subargs
, NULL_TREE
, tf_none
,
15656 /*require_all_args=*/true,
15657 /*use_default_args=*/true);
15658 if (subargs
!= error_mark_node
15659 && !any_dependent_template_arguments_p (subargs
))
15661 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15662 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15663 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15664 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15673 /* If no templates (or more than one) are fully resolved by the
15674 explicit arguments, this template-id is a non-deduced context; it
15675 could still be OK if we deduce all template arguments for the
15676 enclosing call through other arguments. */
15680 else if (TREE_CODE (arg
) != OVERLOAD
15681 && TREE_CODE (arg
) != FUNCTION_DECL
)
15682 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15683 -- but the deduction does not succeed because the expression is
15684 not just the function on its own. */
15687 for (; arg
; arg
= OVL_NEXT (arg
))
15688 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15689 TREE_TYPE (OVL_CURRENT (arg
)),
15690 strict
, sub_strict
, addr_p
, explain_p
)
15691 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15693 goodfn
= OVL_CURRENT (arg
);
15697 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15698 to function or pointer to member function argument if the set of
15699 overloaded functions does not contain function templates and at most
15700 one of a set of overloaded functions provides a unique match.
15702 So if we found multiple possibilities, we return success but don't
15703 deduce anything. */
15707 int i
= TREE_VEC_LENGTH (targs
);
15709 if (TREE_VEC_ELT (tempargs
, i
))
15710 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15718 /* Core DR 115: In contexts where deduction is done and fails, or in
15719 contexts where deduction is not done, if a template argument list is
15720 specified and it, along with any default template arguments, identifies
15721 a single function template specialization, then the template-id is an
15722 lvalue for the function template specialization. */
15725 resolve_nondeduced_context (tree orig_expr
)
15727 tree expr
, offset
, baselink
;
15730 if (!type_unknown_p (orig_expr
))
15735 offset
= NULL_TREE
;
15736 baselink
= NULL_TREE
;
15738 if (TREE_CODE (expr
) == ADDR_EXPR
)
15740 expr
= TREE_OPERAND (expr
, 0);
15743 if (TREE_CODE (expr
) == OFFSET_REF
)
15746 expr
= TREE_OPERAND (expr
, 1);
15748 if (BASELINK_P (expr
))
15751 expr
= BASELINK_FUNCTIONS (expr
);
15754 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15757 tree goodfn
= NULL_TREE
;
15759 /* If we got some explicit template args, we need to plug them into
15760 the affected templates before we try to unify, in case the
15761 explicit args will completely resolve the templates in question. */
15763 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15764 tree arg
= TREE_OPERAND (expr
, 0);
15765 tree badfn
= NULL_TREE
;
15766 tree badargs
= NULL_TREE
;
15768 for (; arg
; arg
= OVL_NEXT (arg
))
15770 tree fn
= OVL_CURRENT (arg
);
15771 tree subargs
, elem
;
15773 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15776 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15777 expl_subargs
, NULL_TREE
, tf_none
,
15778 /*require_all_args=*/true,
15779 /*use_default_args=*/true);
15780 if (subargs
!= error_mark_node
15781 && !any_dependent_template_arguments_p (subargs
))
15783 elem
= instantiate_template (fn
, subargs
, tf_none
);
15784 if (elem
== error_mark_node
)
15789 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15798 mark_used (goodfn
);
15801 expr
= build_baselink (BASELINK_BINFO (baselink
),
15802 BASELINK_ACCESS_BINFO (baselink
),
15803 expr
, BASELINK_OPTYPE (baselink
));
15807 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15808 expr
= build_offset_ref (base
, expr
, addr
);
15811 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15814 else if (good
== 0 && badargs
)
15815 /* There were no good options and at least one bad one, so let the
15816 user know what the problem is. */
15817 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15822 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15823 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15824 different overloads deduce different arguments for a given parm.
15825 ADDR_P is true if the expression for which deduction is being
15826 performed was of the form "& fn" rather than simply "fn".
15828 Returns 1 on success. */
15831 try_one_overload (tree tparms
,
15836 unification_kind_t strict
,
15845 if (arg
== error_mark_node
)
15848 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15849 to function or pointer to member function argument if the set of
15850 overloaded functions does not contain function templates and at most
15851 one of a set of overloaded functions provides a unique match.
15853 So if this is a template, just return success. */
15855 if (uses_template_parms (arg
))
15858 if (TREE_CODE (arg
) == METHOD_TYPE
)
15859 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15861 arg
= build_pointer_type (arg
);
15863 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15865 /* We don't copy orig_targs for this because if we have already deduced
15866 some template args from previous args, unify would complain when we
15867 try to deduce a template parameter for the same argument, even though
15868 there isn't really a conflict. */
15869 nargs
= TREE_VEC_LENGTH (targs
);
15870 tempargs
= make_tree_vec (nargs
);
15872 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15875 /* First make sure we didn't deduce anything that conflicts with
15876 explicitly specified args. */
15877 for (i
= nargs
; i
--; )
15879 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15880 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15884 else if (uses_template_parms (elt
))
15885 /* Since we're unifying against ourselves, we will fill in
15886 template args used in the function parm list with our own
15887 template parms. Discard them. */
15888 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15889 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15893 for (i
= nargs
; i
--; )
15895 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15898 TREE_VEC_ELT (targs
, i
) = elt
;
15904 /* PARM is a template class (perhaps with unbound template
15905 parameters). ARG is a fully instantiated type. If ARG can be
15906 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15907 TARGS are as for unify. */
15910 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15913 tree copy_of_targs
;
15915 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15916 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15917 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15920 /* We need to make a new template argument vector for the call to
15921 unify. If we used TARGS, we'd clutter it up with the result of
15922 the attempted unification, even if this class didn't work out.
15923 We also don't want to commit ourselves to all the unifications
15924 we've already done, since unification is supposed to be done on
15925 an argument-by-argument basis. In other words, consider the
15926 following pathological case:
15928 template <int I, int J, int K>
15931 template <int I, int J>
15932 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15934 template <int I, int J, int K>
15935 void f(S<I, J, K>, S<I, I, I>);
15944 Now, by the time we consider the unification involving `s2', we
15945 already know that we must have `f<0, 0, 0>'. But, even though
15946 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15947 because there are two ways to unify base classes of S<0, 1, 2>
15948 with S<I, I, I>. If we kept the already deduced knowledge, we
15949 would reject the possibility I=1. */
15950 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15952 /* If unification failed, we're done. */
15953 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15954 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15960 /* Given a template type PARM and a class type ARG, find the unique
15961 base type in ARG that is an instance of PARM. We do not examine
15962 ARG itself; only its base-classes. If there is not exactly one
15963 appropriate base class, return NULL_TREE. PARM may be the type of
15964 a partial specialization, as well as a plain template type. Used
15967 static enum template_base_result
15968 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15969 bool explain_p
, tree
*result
)
15971 tree rval
= NULL_TREE
;
15974 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15976 binfo
= TYPE_BINFO (complete_type (arg
));
15979 /* The type could not be completed. */
15980 *result
= NULL_TREE
;
15981 return tbr_incomplete_type
;
15984 /* Walk in inheritance graph order. The search order is not
15985 important, and this avoids multiple walks of virtual bases. */
15986 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15988 tree r
= try_class_unification (tparms
, targs
, parm
,
15989 BINFO_TYPE (binfo
), explain_p
);
15993 /* If there is more than one satisfactory baseclass, then:
15997 If they yield more than one possible deduced A, the type
16001 if (rval
&& !same_type_p (r
, rval
))
16003 *result
= NULL_TREE
;
16004 return tbr_ambiguous_baseclass
;
16012 return tbr_success
;
16015 /* Returns the level of DECL, which declares a template parameter. */
16018 template_decl_level (tree decl
)
16020 switch (TREE_CODE (decl
))
16023 case TEMPLATE_DECL
:
16024 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16027 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16030 gcc_unreachable ();
16035 /* Decide whether ARG can be unified with PARM, considering only the
16036 cv-qualifiers of each type, given STRICT as documented for unify.
16037 Returns nonzero iff the unification is OK on that basis. */
16040 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16042 int arg_quals
= cp_type_quals (arg
);
16043 int parm_quals
= cp_type_quals (parm
);
16045 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16046 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16048 /* Although a CVR qualifier is ignored when being applied to a
16049 substituted template parameter ([8.3.2]/1 for example), that
16050 does not allow us to unify "const T" with "int&" because both
16051 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16052 It is ok when we're allowing additional CV qualifiers
16053 at the outer level [14.8.2.1]/3,1st bullet. */
16054 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16055 || TREE_CODE (arg
) == FUNCTION_TYPE
16056 || TREE_CODE (arg
) == METHOD_TYPE
)
16057 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16060 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16061 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16065 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16066 && (arg_quals
& parm_quals
) != parm_quals
)
16069 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16070 && (parm_quals
& arg_quals
) != arg_quals
)
16076 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16078 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16080 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16081 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16082 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16084 *index
= TEMPLATE_TYPE_IDX (parm
);
16085 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16089 *index
= TEMPLATE_PARM_IDX (parm
);
16090 *level
= TEMPLATE_PARM_LEVEL (parm
);
16094 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16096 if (unify (TP, TA, P, A, S, EP)) \
16100 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16101 expansion at the end of PACKED_PARMS. Returns 0 if the type
16102 deduction succeeds, 1 otherwise. STRICT is the same as in
16103 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16104 call argument list. We'll need to adjust the arguments to make them
16105 types. SUBR tells us if this is from a recursive call to
16106 type_unification_real, or for comparing two template argument
16110 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16111 tree packed_args
, unification_kind_t strict
,
16112 bool subr
, bool explain_p
)
16115 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16116 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16117 tree pack
, packs
= NULL_TREE
;
16118 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16119 int len
= TREE_VEC_LENGTH (packed_args
);
16121 /* Determine the parameter packs we will be deducing from the
16122 pattern, and record their current deductions. */
16123 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16124 pack
; pack
= TREE_CHAIN (pack
))
16126 tree parm_pack
= TREE_VALUE (pack
);
16129 /* Determine the index and level of this parameter pack. */
16130 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16132 /* Keep track of the parameter packs and their corresponding
16134 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16135 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16138 /* Loop through all of the arguments that have not yet been
16139 unified and unify each with the pattern. */
16140 for (i
= start
; i
< len
; i
++)
16143 bool any_explicit
= false;
16144 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16146 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16147 or the element of its argument pack at the current index if
16148 this argument was explicitly specified. */
16149 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16153 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16156 if (TREE_VALUE (pack
)
16157 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16158 && (i
< TREE_VEC_LENGTH (pargs
)))
16160 any_explicit
= true;
16161 arg
= TREE_VEC_ELT (pargs
, i
);
16163 TMPL_ARG (targs
, level
, idx
) = arg
;
16166 /* If we had explicit template arguments, substitute them into the
16167 pattern before deduction. */
16170 /* Some arguments might still be unspecified or dependent. */
16172 ++processing_template_decl
;
16173 dependent
= any_dependent_template_arguments_p (targs
);
16175 --processing_template_decl
;
16176 parm
= tsubst (pattern
, targs
,
16177 explain_p
? tf_warning_or_error
: tf_none
,
16180 --processing_template_decl
;
16181 if (parm
== error_mark_node
)
16187 /* Unify the pattern with the current argument. */
16188 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16189 LOOKUP_IMPLICIT
, explain_p
))
16192 /* For each parameter pack, collect the deduced value. */
16193 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16196 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16198 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16199 TMPL_ARG (targs
, level
, idx
);
16203 /* Verify that the results of unification with the parameter packs
16204 produce results consistent with what we've seen before, and make
16205 the deduced argument packs available. */
16206 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16208 tree old_pack
= TREE_VALUE (pack
);
16209 tree new_args
= TREE_TYPE (pack
);
16210 int i
, len
= TREE_VEC_LENGTH (new_args
);
16212 bool nondeduced_p
= false;
16214 /* By default keep the original deduced argument pack.
16215 If necessary, more specific code is going to update the
16216 resulting deduced argument later down in this function. */
16217 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16218 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16220 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16221 actually deduce anything. */
16222 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16223 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16224 nondeduced_p
= true;
16228 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16230 /* If we had fewer function args than explicit template args,
16231 just use the explicits. */
16232 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16233 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16234 if (len
< explicit_len
)
16235 new_args
= explicit_args
;
16241 /* Build the deduced *_ARGUMENT_PACK. */
16242 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16244 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16245 TREE_TYPE (result
) =
16246 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16247 TREE_CONSTANT (result
) = 1;
16250 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16252 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16254 /* Note the deduced argument packs for this parameter
16256 TMPL_ARG (targs
, level
, idx
) = result
;
16258 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16259 && (ARGUMENT_PACK_ARGS (old_pack
)
16260 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16262 /* We only had the explicitly-provided arguments before, but
16263 now we have a complete set of arguments. */
16264 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16266 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16267 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16268 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16272 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16273 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16275 if (!comp_template_args_with_info (old_args
, new_args
,
16276 &bad_old_arg
, &bad_new_arg
))
16277 /* Inconsistent unification of this parameter pack. */
16278 return unify_parameter_pack_inconsistent (explain_p
,
16284 return unify_success (explain_p
);
16287 /* Deduce the value of template parameters. TPARMS is the (innermost)
16288 set of template parameters to a template. TARGS is the bindings
16289 for those template parameters, as determined thus far; TARGS may
16290 include template arguments for outer levels of template parameters
16291 as well. PARM is a parameter to a template function, or a
16292 subcomponent of that parameter; ARG is the corresponding argument.
16293 This function attempts to match PARM with ARG in a manner
16294 consistent with the existing assignments in TARGS. If more values
16295 are deduced, then TARGS is updated.
16297 Returns 0 if the type deduction succeeds, 1 otherwise. The
16298 parameter STRICT is a bitwise or of the following flags:
16301 Require an exact match between PARM and ARG.
16302 UNIFY_ALLOW_MORE_CV_QUAL:
16303 Allow the deduced ARG to be more cv-qualified (by qualification
16304 conversion) than ARG.
16305 UNIFY_ALLOW_LESS_CV_QUAL:
16306 Allow the deduced ARG to be less cv-qualified than ARG.
16307 UNIFY_ALLOW_DERIVED:
16308 Allow the deduced ARG to be a template base class of ARG,
16309 or a pointer to a template base class of the type pointed to by
16311 UNIFY_ALLOW_INTEGER:
16312 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16313 case for more information.
16314 UNIFY_ALLOW_OUTER_LEVEL:
16315 This is the outermost level of a deduction. Used to determine validity
16316 of qualification conversions. A valid qualification conversion must
16317 have const qualified pointers leading up to the inner type which
16318 requires additional CV quals, except at the outer level, where const
16319 is not required [conv.qual]. It would be normal to set this flag in
16320 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16321 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16322 This is the outermost level of a deduction, and PARM can be more CV
16323 qualified at this point.
16324 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16325 This is the outermost level of a deduction, and PARM can be less CV
16326 qualified at this point. */
16329 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16335 int strict_in
= strict
;
16337 /* I don't think this will do the right thing with respect to types.
16338 But the only case I've seen it in so far has been array bounds, where
16339 signedness is the only information lost, and I think that will be
16341 while (TREE_CODE (parm
) == NOP_EXPR
)
16342 parm
= TREE_OPERAND (parm
, 0);
16344 if (arg
== error_mark_node
)
16345 return unify_invalid (explain_p
);
16346 if (arg
== unknown_type_node
16347 || arg
== init_list_type_node
)
16348 /* We can't deduce anything from this, but we might get all the
16349 template args from other function args. */
16350 return unify_success (explain_p
);
16352 /* If PARM uses template parameters, then we can't bail out here,
16353 even if ARG == PARM, since we won't record unifications for the
16354 template parameters. We might need them if we're trying to
16355 figure out which of two things is more specialized. */
16356 if (arg
== parm
&& !uses_template_parms (parm
))
16357 return unify_success (explain_p
);
16359 /* Handle init lists early, so the rest of the function can assume
16360 we're dealing with a type. */
16361 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16365 tree orig_parm
= parm
;
16367 /* Replace T with std::initializer_list<T> for deduction. */
16368 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16369 && flag_deduce_init_list
)
16370 parm
= listify (parm
);
16372 if (!is_std_init_list (parm
))
16373 /* We can only deduce from an initializer list argument if the
16374 parameter is std::initializer_list; otherwise this is a
16375 non-deduced context. */
16376 return unify_success (explain_p
);
16378 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16380 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16382 int elt_strict
= strict
;
16384 if (elt
== error_mark_node
)
16385 return unify_invalid (explain_p
);
16387 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16389 tree type
= TREE_TYPE (elt
);
16390 /* It should only be possible to get here for a call. */
16391 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16392 elt_strict
|= maybe_adjust_types_for_deduction
16393 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16397 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16401 /* If the std::initializer_list<T> deduction worked, replace the
16402 deduced A with std::initializer_list<A>. */
16403 if (orig_parm
!= parm
)
16405 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16406 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16407 targ
= listify (targ
);
16408 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16410 return unify_success (explain_p
);
16413 /* Immediately reject some pairs that won't unify because of
16414 cv-qualification mismatches. */
16415 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16417 /* It is the elements of the array which hold the cv quals of an array
16418 type, and the elements might be template type parms. We'll check
16419 when we recurse. */
16420 && TREE_CODE (arg
) != ARRAY_TYPE
16421 /* We check the cv-qualifiers when unifying with template type
16422 parameters below. We want to allow ARG `const T' to unify with
16423 PARM `T' for example, when computing which of two templates
16424 is more specialized, for example. */
16425 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16426 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16427 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16429 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16430 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16431 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16432 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16433 strict
&= ~UNIFY_ALLOW_DERIVED
;
16434 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16435 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16437 switch (TREE_CODE (parm
))
16439 case TYPENAME_TYPE
:
16441 case UNBOUND_CLASS_TEMPLATE
:
16442 /* In a type which contains a nested-name-specifier, template
16443 argument values cannot be deduced for template parameters used
16444 within the nested-name-specifier. */
16445 return unify_success (explain_p
);
16447 case TEMPLATE_TYPE_PARM
:
16448 case TEMPLATE_TEMPLATE_PARM
:
16449 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16450 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16451 if (tparm
== error_mark_node
)
16452 return unify_invalid (explain_p
);
16454 if (TEMPLATE_TYPE_LEVEL (parm
)
16455 != template_decl_level (tparm
))
16456 /* The PARM is not one we're trying to unify. Just check
16457 to see if it matches ARG. */
16459 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16460 && (is_auto (parm
) ? is_auto (arg
)
16461 : same_type_p (parm
, arg
)))
16462 return unify_success (explain_p
);
16464 return unify_type_mismatch (explain_p
, parm
, arg
);
16466 idx
= TEMPLATE_TYPE_IDX (parm
);
16467 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16468 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16469 if (tparm
== error_mark_node
)
16470 return unify_invalid (explain_p
);
16472 /* Check for mixed types and values. */
16473 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16474 && TREE_CODE (tparm
) != TYPE_DECL
)
16475 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16476 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16477 gcc_unreachable ();
16479 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16481 /* ARG must be constructed from a template class or a template
16482 template parameter. */
16483 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16484 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16485 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16488 tree parmvec
= TYPE_TI_ARGS (parm
);
16489 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16490 tree full_argvec
= add_to_template_args (targs
, argvec
);
16492 = DECL_INNERMOST_TEMPLATE_PARMS
16493 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16495 int parm_variadic_p
= 0;
16497 /* The resolution to DR150 makes clear that default
16498 arguments for an N-argument may not be used to bind T
16499 to a template template parameter with fewer than N
16500 parameters. It is not safe to permit the binding of
16501 default arguments as an extension, as that may change
16502 the meaning of a conforming program. Consider:
16504 struct Dense { static const unsigned int dim = 1; };
16506 template <template <typename> class View,
16508 void operator+(float, View<Block> const&);
16510 template <typename Block,
16511 unsigned int Dim = Block::dim>
16512 struct Lvalue_proxy { operator float() const; };
16516 Lvalue_proxy<Dense> p;
16521 Here, if Lvalue_proxy is permitted to bind to View, then
16522 the global operator+ will be used; if they are not, the
16523 Lvalue_proxy will be converted to float. */
16524 if (coerce_template_parms (parm_parms
,
16526 TYPE_TI_TEMPLATE (parm
),
16528 ? tf_warning_or_error
16530 /*require_all_args=*/true,
16531 /*use_default_args=*/false)
16532 == error_mark_node
)
16535 /* Deduce arguments T, i from TT<T> or TT<i>.
16536 We check each element of PARMVEC and ARGVEC individually
16537 rather than the whole TREE_VEC since they can have
16538 different number of elements. */
16540 parmvec
= expand_template_argument_pack (parmvec
);
16541 argvec
= expand_template_argument_pack (argvec
);
16543 len
= TREE_VEC_LENGTH (parmvec
);
16545 /* Check if the parameters end in a pack, making them
16548 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16549 parm_variadic_p
= 1;
16551 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16552 /* If the template argument list of P contains a pack
16553 expansion that is not the last template argument, the
16554 entire template argument list is a non-deduced
16556 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
16557 return unify_success (explain_p
);
16559 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16560 return unify_too_few_arguments (explain_p
,
16561 TREE_VEC_LENGTH (argvec
), len
);
16563 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16565 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16566 TREE_VEC_ELT (parmvec
, i
),
16567 TREE_VEC_ELT (argvec
, i
),
16568 UNIFY_ALLOW_NONE
, explain_p
);
16571 if (parm_variadic_p
16572 && unify_pack_expansion (tparms
, targs
,
16575 /*subr=*/true, explain_p
))
16578 arg
= TYPE_TI_TEMPLATE (arg
);
16580 /* Fall through to deduce template name. */
16583 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16584 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16586 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16588 /* Simple cases: Value already set, does match or doesn't. */
16589 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16590 return unify_success (explain_p
);
16592 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16596 /* If PARM is `const T' and ARG is only `int', we don't have
16597 a match unless we are allowing additional qualification.
16598 If ARG is `const int' and PARM is just `T' that's OK;
16599 that binds `const int' to `T'. */
16600 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16602 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16604 /* Consider the case where ARG is `const volatile int' and
16605 PARM is `const T'. Then, T should be `volatile int'. */
16606 arg
= cp_build_qualified_type_real
16607 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16608 if (arg
== error_mark_node
)
16609 return unify_invalid (explain_p
);
16611 /* Simple cases: Value already set, does match or doesn't. */
16612 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16613 return unify_success (explain_p
);
16615 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16617 /* Make sure that ARG is not a variable-sized array. (Note
16618 that were talking about variable-sized arrays (like
16619 `int[n]'), rather than arrays of unknown size (like
16620 `int[]').) We'll get very confused by such a type since
16621 the bound of the array is not constant, and therefore
16622 not mangleable. Besides, such types are not allowed in
16623 ISO C++, so we can do as we please here. We do allow
16624 them for 'auto' deduction, since that isn't ABI-exposed. */
16625 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16626 return unify_vla_arg (explain_p
, arg
);
16628 /* Strip typedefs as in convert_template_argument. */
16629 arg
= canonicalize_type_argument (arg
, tf_none
);
16632 /* If ARG is a parameter pack or an expansion, we cannot unify
16633 against it unless PARM is also a parameter pack. */
16634 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16635 && !template_parameter_pack_p (parm
))
16636 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16638 /* If the argument deduction results is a METHOD_TYPE,
16639 then there is a problem.
16640 METHOD_TYPE doesn't map to any real C++ type the result of
16641 the deduction can not be of that type. */
16642 if (TREE_CODE (arg
) == METHOD_TYPE
)
16643 return unify_method_type_error (explain_p
, arg
);
16645 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16646 return unify_success (explain_p
);
16648 case TEMPLATE_PARM_INDEX
:
16649 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16650 if (tparm
== error_mark_node
)
16651 return unify_invalid (explain_p
);
16653 if (TEMPLATE_PARM_LEVEL (parm
)
16654 != template_decl_level (tparm
))
16656 /* The PARM is not one we're trying to unify. Just check
16657 to see if it matches ARG. */
16658 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16659 && cp_tree_equal (parm
, arg
));
16661 unify_expression_unequal (explain_p
, parm
, arg
);
16665 idx
= TEMPLATE_PARM_IDX (parm
);
16666 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16670 int x
= !cp_tree_equal (targ
, arg
);
16672 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16676 /* [temp.deduct.type] If, in the declaration of a function template
16677 with a non-type template-parameter, the non-type
16678 template-parameter is used in an expression in the function
16679 parameter-list and, if the corresponding template-argument is
16680 deduced, the template-argument type shall match the type of the
16681 template-parameter exactly, except that a template-argument
16682 deduced from an array bound may be of any integral type.
16683 The non-type parameter might use already deduced type parameters. */
16684 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16685 if (!TREE_TYPE (arg
))
16686 /* Template-parameter dependent expression. Just accept it for now.
16687 It will later be processed in convert_template_argument. */
16689 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16691 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16692 && (TREE_CODE (tparm
) == INTEGER_TYPE
16693 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16694 /* Convert the ARG to the type of PARM; the deduced non-type
16695 template argument must exactly match the types of the
16696 corresponding parameter. */
16697 arg
= fold (build_nop (tparm
, arg
));
16698 else if (uses_template_parms (tparm
))
16699 /* We haven't deduced the type of this parameter yet. Try again
16701 return unify_success (explain_p
);
16703 return unify_type_mismatch (explain_p
, tparm
, arg
);
16705 /* If ARG is a parameter pack or an expansion, we cannot unify
16706 against it unless PARM is also a parameter pack. */
16707 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16708 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16709 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16711 arg
= strip_typedefs_expr (arg
);
16712 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16713 return unify_success (explain_p
);
16717 /* A pointer-to-member constant can be unified only with
16718 another constant. */
16719 if (TREE_CODE (arg
) != PTRMEM_CST
)
16720 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16722 /* Just unify the class member. It would be useless (and possibly
16723 wrong, depending on the strict flags) to unify also
16724 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16725 arg refer to the same variable, even if through different
16726 classes. For instance:
16728 struct A { int x; };
16731 Unification of &A::x and &B::x must succeed. */
16732 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16733 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16738 if (TREE_CODE (arg
) != POINTER_TYPE
)
16739 return unify_type_mismatch (explain_p
, parm
, arg
);
16741 /* [temp.deduct.call]
16743 A can be another pointer or pointer to member type that can
16744 be converted to the deduced A via a qualification
16745 conversion (_conv.qual_).
16747 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16748 This will allow for additional cv-qualification of the
16749 pointed-to types if appropriate. */
16751 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16752 /* The derived-to-base conversion only persists through one
16753 level of pointers. */
16754 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16756 return unify (tparms
, targs
, TREE_TYPE (parm
),
16757 TREE_TYPE (arg
), strict
, explain_p
);
16760 case REFERENCE_TYPE
:
16761 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16762 return unify_type_mismatch (explain_p
, parm
, arg
);
16763 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16764 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16767 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16768 return unify_type_mismatch (explain_p
, parm
, arg
);
16769 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16770 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16771 return unify_type_mismatch (explain_p
, parm
, arg
);
16772 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16773 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16774 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16781 /* Our representation of array types uses "N - 1" as the
16782 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16783 not an integer constant. We cannot unify arbitrarily
16784 complex expressions, so we eliminate the MINUS_EXPRs
16786 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16787 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16790 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16791 parm_max
= TREE_OPERAND (parm_max
, 0);
16793 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16794 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16797 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16798 trying to unify the type of a variable with the type
16799 of a template parameter. For example:
16801 template <unsigned int N>
16802 void f (char (&) [N]);
16809 Here, the type of the ARG will be "int [g(i)]", and
16810 may be a SAVE_EXPR, etc. */
16811 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16812 return unify_vla_arg (explain_p
, arg
);
16813 arg_max
= TREE_OPERAND (arg_max
, 0);
16816 /* If only one of the bounds used a MINUS_EXPR, compensate
16817 by adding one to the other bound. */
16818 if (parm_cst
&& !arg_cst
)
16819 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16823 else if (arg_cst
&& !parm_cst
)
16824 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16829 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16830 UNIFY_ALLOW_INTEGER
, explain_p
);
16832 return unify_success (explain_p
);
16839 case ENUMERAL_TYPE
:
16842 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16843 return unify_type_mismatch (explain_p
, parm
, arg
);
16845 /* We have already checked cv-qualification at the top of the
16847 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16848 return unify_type_mismatch (explain_p
, parm
, arg
);
16850 /* As far as unification is concerned, this wins. Later checks
16851 will invalidate it if necessary. */
16852 return unify_success (explain_p
);
16854 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16855 /* Type INTEGER_CST can come from ordinary constant template args. */
16857 while (TREE_CODE (arg
) == NOP_EXPR
)
16858 arg
= TREE_OPERAND (arg
, 0);
16860 if (TREE_CODE (arg
) != INTEGER_CST
)
16861 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16862 return (tree_int_cst_equal (parm
, arg
)
16863 ? unify_success (explain_p
)
16864 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16868 int i
, len
, argslen
;
16869 int parm_variadic_p
= 0;
16871 if (TREE_CODE (arg
) != TREE_VEC
)
16872 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16874 len
= TREE_VEC_LENGTH (parm
);
16875 argslen
= TREE_VEC_LENGTH (arg
);
16877 /* Check for pack expansions in the parameters. */
16878 for (i
= 0; i
< len
; ++i
)
16880 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16883 /* We can unify against something with a trailing
16885 parm_variadic_p
= 1;
16887 /* [temp.deduct.type]/9: If the template argument list of
16888 P contains a pack expansion that is not the last
16889 template argument, the entire template argument list
16890 is a non-deduced context. */
16891 return unify_success (explain_p
);
16895 /* If we don't have enough arguments to satisfy the parameters
16896 (not counting the pack expression at the end), or we have
16897 too many arguments for a parameter list that doesn't end in
16898 a pack expression, we can't unify. */
16899 if (parm_variadic_p
16900 ? argslen
< len
- parm_variadic_p
16902 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16904 /* Unify all of the parameters that precede the (optional)
16905 pack expression. */
16906 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16908 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16909 TREE_VEC_ELT (parm
, i
),
16910 TREE_VEC_ELT (arg
, i
),
16911 UNIFY_ALLOW_NONE
, explain_p
);
16913 if (parm_variadic_p
)
16914 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16916 /*subr=*/true, explain_p
);
16917 return unify_success (explain_p
);
16922 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16923 return unify_type_mismatch (explain_p
, parm
, arg
);
16925 if (TYPE_PTRMEMFUNC_P (parm
))
16927 if (!TYPE_PTRMEMFUNC_P (arg
))
16928 return unify_type_mismatch (explain_p
, parm
, arg
);
16930 return unify (tparms
, targs
,
16931 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16932 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16933 strict
, explain_p
);
16936 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16938 tree t
= NULL_TREE
;
16940 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16942 /* First, we try to unify the PARM and ARG directly. */
16943 t
= try_class_unification (tparms
, targs
,
16944 parm
, arg
, explain_p
);
16948 /* Fallback to the special case allowed in
16949 [temp.deduct.call]:
16951 If P is a class, and P has the form
16952 template-id, then A can be a derived class of
16953 the deduced A. Likewise, if P is a pointer to
16954 a class of the form template-id, A can be a
16955 pointer to a derived class pointed to by the
16957 enum template_base_result r
;
16958 r
= get_template_base (tparms
, targs
, parm
, arg
,
16962 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16965 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16966 && (CLASSTYPE_TI_TEMPLATE (parm
)
16967 == CLASSTYPE_TI_TEMPLATE (arg
)))
16968 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16969 Then, we should unify `int' and `U'. */
16972 /* There's no chance of unification succeeding. */
16973 return unify_type_mismatch (explain_p
, parm
, arg
);
16975 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16976 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16978 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16979 return unify_type_mismatch (explain_p
, parm
, arg
);
16980 return unify_success (explain_p
);
16983 case FUNCTION_TYPE
:
16985 unsigned int nargs
;
16990 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16991 return unify_type_mismatch (explain_p
, parm
, arg
);
16993 /* CV qualifications for methods can never be deduced, they must
16994 match exactly. We need to check them explicitly here,
16995 because type_unification_real treats them as any other
16996 cv-qualified parameter. */
16997 if (TREE_CODE (parm
) == METHOD_TYPE
16998 && (!check_cv_quals_for_unify
17000 class_of_this_parm (arg
),
17001 class_of_this_parm (parm
))))
17002 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17004 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17005 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17007 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17008 args
= XALLOCAVEC (tree
, nargs
);
17009 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17010 a
!= NULL_TREE
&& a
!= void_list_node
;
17011 a
= TREE_CHAIN (a
), ++i
)
17012 args
[i
] = TREE_VALUE (a
);
17015 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17016 args
, nargs
, 1, DEDUCE_EXACT
,
17017 LOOKUP_NORMAL
, explain_p
);
17021 /* Unify a pointer to member with a pointer to member function, which
17022 deduces the type of the member as a function type. */
17023 if (TYPE_PTRMEMFUNC_P (arg
))
17028 /* Check top-level cv qualifiers */
17029 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17030 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17032 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17033 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17034 UNIFY_ALLOW_NONE
, explain_p
);
17036 /* Determine the type of the function we are unifying against. */
17037 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
17039 build_function_type (TREE_TYPE (method_type
),
17040 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
17042 /* Extract the cv-qualifiers of the member function from the
17043 implicit object parameter and place them on the function
17044 type to be restored later. */
17045 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
17046 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17049 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17050 return unify_type_mismatch (explain_p
, parm
, arg
);
17051 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17052 TYPE_OFFSET_BASETYPE (arg
),
17053 UNIFY_ALLOW_NONE
, explain_p
);
17054 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17055 strict
, explain_p
);
17058 if (DECL_TEMPLATE_PARM_P (parm
))
17059 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17060 if (arg
!= integral_constant_value (parm
))
17061 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17062 return unify_success (explain_p
);
17065 case TEMPLATE_DECL
:
17066 /* Matched cases are handled by the ARG == PARM test above. */
17067 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17070 /* A non-type template parameter that is a variable should be a
17071 an integral constant, in which case, it whould have been
17072 folded into its (constant) value. So we should not be getting
17073 a variable here. */
17074 gcc_unreachable ();
17076 case TYPE_ARGUMENT_PACK
:
17077 case NONTYPE_ARGUMENT_PACK
:
17078 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17079 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17082 case DECLTYPE_TYPE
:
17083 case UNDERLYING_TYPE
:
17084 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17085 or UNDERLYING_TYPE nodes. */
17086 return unify_success (explain_p
);
17089 /* Unification fails if we hit an error node. */
17090 return unify_invalid (explain_p
);
17093 /* An unresolved overload is a nondeduced context. */
17094 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17095 return unify_success (explain_p
);
17096 gcc_assert (EXPR_P (parm
));
17098 /* We must be looking at an expression. This can happen with
17102 void foo(S<I>, S<I + 2>);
17104 This is a "nondeduced context":
17108 The nondeduced contexts are:
17110 --A type that is a template-id in which one or more of
17111 the template-arguments is an expression that references
17112 a template-parameter.
17114 In these cases, we assume deduction succeeded, but don't
17115 actually infer any unifications. */
17117 if (!uses_template_parms (parm
)
17118 && !template_args_equal (parm
, arg
))
17119 return unify_expression_unequal (explain_p
, parm
, arg
);
17121 return unify_success (explain_p
);
17124 #undef RECUR_AND_CHECK_FAILURE
17126 /* Note that DECL can be defined in this translation unit, if
17130 mark_definable (tree decl
)
17133 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17134 FOR_EACH_CLONE (clone
, decl
)
17135 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17138 /* Called if RESULT is explicitly instantiated, or is a member of an
17139 explicitly instantiated class. */
17142 mark_decl_instantiated (tree result
, int extern_p
)
17144 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17146 /* If this entity has already been written out, it's too late to
17147 make any modifications. */
17148 if (TREE_ASM_WRITTEN (result
))
17151 if (TREE_CODE (result
) != FUNCTION_DECL
)
17152 /* The TREE_PUBLIC flag for function declarations will have been
17153 set correctly by tsubst. */
17154 TREE_PUBLIC (result
) = 1;
17156 /* This might have been set by an earlier implicit instantiation. */
17157 DECL_COMDAT (result
) = 0;
17160 DECL_NOT_REALLY_EXTERN (result
) = 0;
17163 mark_definable (result
);
17164 /* Always make artificials weak. */
17165 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17166 comdat_linkage (result
);
17167 /* For WIN32 we also want to put explicit instantiations in
17168 linkonce sections. */
17169 else if (TREE_PUBLIC (result
))
17170 maybe_make_one_only (result
);
17173 /* If EXTERN_P, then this function will not be emitted -- unless
17174 followed by an explicit instantiation, at which point its linkage
17175 will be adjusted. If !EXTERN_P, then this function will be
17176 emitted here. In neither circumstance do we want
17177 import_export_decl to adjust the linkage. */
17178 DECL_INTERFACE_KNOWN (result
) = 1;
17181 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17182 important template arguments. If any are missing, we check whether
17183 they're important by using error_mark_node for substituting into any
17184 args that were used for partial ordering (the ones between ARGS and END)
17185 and seeing if it bubbles up. */
17188 check_undeduced_parms (tree targs
, tree args
, tree end
)
17190 bool found
= false;
17192 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17193 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17196 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17200 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17201 if (substed
== error_mark_node
)
17207 /* Given two function templates PAT1 and PAT2, return:
17209 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17210 -1 if PAT2 is more specialized than PAT1.
17211 0 if neither is more specialized.
17213 LEN indicates the number of parameters we should consider
17214 (defaulted parameters should not be considered).
17216 The 1998 std underspecified function template partial ordering, and
17217 DR214 addresses the issue. We take pairs of arguments, one from
17218 each of the templates, and deduce them against each other. One of
17219 the templates will be more specialized if all the *other*
17220 template's arguments deduce against its arguments and at least one
17221 of its arguments *does* *not* deduce against the other template's
17222 corresponding argument. Deduction is done as for class templates.
17223 The arguments used in deduction have reference and top level cv
17224 qualifiers removed. Iff both arguments were originally reference
17225 types *and* deduction succeeds in both directions, the template
17226 with the more cv-qualified argument wins for that pairing (if
17227 neither is more cv-qualified, they both are equal). Unlike regular
17228 deduction, after all the arguments have been deduced in this way,
17229 we do *not* verify the deduced template argument values can be
17230 substituted into non-deduced contexts.
17232 The logic can be a bit confusing here, because we look at deduce1 and
17233 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17234 can find template arguments for pat1 to make arg1 look like arg2, that
17235 means that arg2 is at least as specialized as arg1. */
17238 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17240 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17241 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17242 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17243 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17244 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17245 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17246 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17247 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17248 tree origs1
, origs2
;
17249 bool lose1
= false;
17250 bool lose2
= false;
17252 /* Remove the this parameter from non-static member functions. If
17253 one is a non-static member function and the other is not a static
17254 member function, remove the first parameter from that function
17255 also. This situation occurs for operator functions where we
17256 locate both a member function (with this pointer) and non-member
17257 operator (with explicit first operand). */
17258 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17260 len
--; /* LEN is the number of significant arguments for DECL1 */
17261 args1
= TREE_CHAIN (args1
);
17262 if (!DECL_STATIC_FUNCTION_P (decl2
))
17263 args2
= TREE_CHAIN (args2
);
17265 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17267 args2
= TREE_CHAIN (args2
);
17268 if (!DECL_STATIC_FUNCTION_P (decl1
))
17271 args1
= TREE_CHAIN (args1
);
17275 /* If only one is a conversion operator, they are unordered. */
17276 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17279 /* Consider the return type for a conversion function */
17280 if (DECL_CONV_FN_P (decl1
))
17282 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17283 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17287 processing_template_decl
++;
17293 /* Stop when an ellipsis is seen. */
17294 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17296 tree arg1
= TREE_VALUE (args1
);
17297 tree arg2
= TREE_VALUE (args2
);
17298 int deduce1
, deduce2
;
17302 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17303 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17305 /* When both arguments are pack expansions, we need only
17306 unify the patterns themselves. */
17307 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17308 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17310 /* This is the last comparison we need to do. */
17314 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17316 arg1
= TREE_TYPE (arg1
);
17317 quals1
= cp_type_quals (arg1
);
17320 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17322 arg2
= TREE_TYPE (arg2
);
17323 quals2
= cp_type_quals (arg2
);
17326 arg1
= TYPE_MAIN_VARIANT (arg1
);
17327 arg2
= TYPE_MAIN_VARIANT (arg2
);
17329 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17331 int i
, len2
= list_length (args2
);
17332 tree parmvec
= make_tree_vec (1);
17333 tree argvec
= make_tree_vec (len2
);
17336 /* Setup the parameter vector, which contains only ARG1. */
17337 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17339 /* Setup the argument vector, which contains the remaining
17341 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17342 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17344 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17345 argvec
, DEDUCE_EXACT
,
17346 /*subr=*/true, /*explain_p=*/false)
17349 /* We cannot deduce in the other direction, because ARG1 is
17350 a pack expansion but ARG2 is not. */
17353 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17355 int i
, len1
= list_length (args1
);
17356 tree parmvec
= make_tree_vec (1);
17357 tree argvec
= make_tree_vec (len1
);
17360 /* Setup the parameter vector, which contains only ARG1. */
17361 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17363 /* Setup the argument vector, which contains the remaining
17365 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17366 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17368 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17369 argvec
, DEDUCE_EXACT
,
17370 /*subr=*/true, /*explain_p=*/false)
17373 /* We cannot deduce in the other direction, because ARG2 is
17374 a pack expansion but ARG1 is not.*/
17380 /* The normal case, where neither argument is a pack
17382 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17383 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17385 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17386 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17390 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17391 arg2, then arg2 is not as specialized as arg1. */
17397 /* "If, for a given type, deduction succeeds in both directions
17398 (i.e., the types are identical after the transformations above)
17399 and if the type from the argument template is more cv-qualified
17400 than the type from the parameter template (as described above)
17401 that type is considered to be more specialized than the other. If
17402 neither type is more cv-qualified than the other then neither type
17403 is more specialized than the other." */
17405 if (deduce1
&& deduce2
17406 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17408 if ((quals1
& quals2
) == quals2
)
17410 if ((quals1
& quals2
) == quals1
)
17414 if (lose1
&& lose2
)
17415 /* We've failed to deduce something in either direction.
17416 These must be unordered. */
17419 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17420 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17421 /* We have already processed all of the arguments in our
17422 handing of the pack expansion type. */
17425 args1
= TREE_CHAIN (args1
);
17426 args2
= TREE_CHAIN (args2
);
17429 /* "In most cases, all template parameters must have values in order for
17430 deduction to succeed, but for partial ordering purposes a template
17431 parameter may remain without a value provided it is not used in the
17432 types being used for partial ordering."
17434 Thus, if we are missing any of the targs1 we need to substitute into
17435 origs1, then pat2 is not as specialized as pat1. This can happen when
17436 there is a nondeduced context. */
17437 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17439 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17442 processing_template_decl
--;
17444 /* All things being equal, if the next argument is a pack expansion
17445 for one function but not for the other, prefer the
17446 non-variadic function. FIXME this is bogus; see c++/41958. */
17448 && args1
&& TREE_VALUE (args1
)
17449 && args2
&& TREE_VALUE (args2
))
17451 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17452 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17455 if (lose1
== lose2
)
17463 /* Determine which of two partial specializations of MAIN_TMPL is more
17466 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17467 to the first partial specialization. The TREE_VALUE is the
17468 innermost set of template parameters for the partial
17469 specialization. PAT2 is similar, but for the second template.
17471 Return 1 if the first partial specialization is more specialized;
17472 -1 if the second is more specialized; 0 if neither is more
17475 See [temp.class.order] for information about determining which of
17476 two templates is more specialized. */
17479 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17484 bool any_deductions
= false;
17486 tmpl1
= TREE_TYPE (pat1
);
17487 tmpl2
= TREE_TYPE (pat2
);
17489 /* Just like what happens for functions, if we are ordering between
17490 different class template specializations, we may encounter dependent
17491 types in the arguments, and we need our dependency check functions
17492 to behave correctly. */
17493 ++processing_template_decl
;
17494 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17495 CLASSTYPE_TI_ARGS (tmpl1
),
17496 CLASSTYPE_TI_ARGS (tmpl2
));
17500 any_deductions
= true;
17503 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17504 CLASSTYPE_TI_ARGS (tmpl2
),
17505 CLASSTYPE_TI_ARGS (tmpl1
));
17509 any_deductions
= true;
17511 --processing_template_decl
;
17513 /* In the case of a tie where at least one of the class templates
17514 has a parameter pack at the end, the template with the most
17515 non-packed parameters wins. */
17518 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17519 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17521 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17522 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17523 int len1
= TREE_VEC_LENGTH (args1
);
17524 int len2
= TREE_VEC_LENGTH (args2
);
17526 /* We don't count the pack expansion at the end. */
17527 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17529 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17534 else if (len1
< len2
)
17541 /* Return the template arguments that will produce the function signature
17542 DECL from the function template FN, with the explicit template
17543 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17544 also match. Return NULL_TREE if no satisfactory arguments could be
17548 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17550 int ntparms
= DECL_NTPARMS (fn
);
17551 tree targs
= make_tree_vec (ntparms
);
17552 tree decl_type
= TREE_TYPE (decl
);
17553 tree decl_arg_types
;
17555 unsigned int nargs
, ix
;
17558 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17560 /* Never do unification on the 'this' parameter. */
17561 decl_arg_types
= skip_artificial_parms_for (decl
,
17562 TYPE_ARG_TYPES (decl_type
));
17564 nargs
= list_length (decl_arg_types
);
17565 args
= XALLOCAVEC (tree
, nargs
);
17566 for (arg
= decl_arg_types
, ix
= 0;
17567 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17568 arg
= TREE_CHAIN (arg
), ++ix
)
17569 args
[ix
] = TREE_VALUE (arg
);
17571 if (fn_type_unification (fn
, explicit_args
, targs
,
17573 (check_rettype
|| DECL_CONV_FN_P (fn
)
17574 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17575 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17576 == error_mark_node
)
17582 /* Return the innermost template arguments that, when applied to a partial
17583 specialization of MAIN_TMPL whose innermost template parameters are
17584 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17587 For example, suppose we have:
17589 template <class T, class U> struct S {};
17590 template <class T> struct S<T*, int> {};
17592 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17593 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17594 int}. The resulting vector will be {double}, indicating that `T'
17595 is bound to `double'. */
17598 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17600 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17602 tree innermost_deduced_args
;
17604 innermost_deduced_args
= make_tree_vec (ntparms
);
17605 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17607 deduced_args
= copy_node (args
);
17608 SET_TMPL_ARGS_LEVEL (deduced_args
,
17609 TMPL_ARGS_DEPTH (deduced_args
),
17610 innermost_deduced_args
);
17613 deduced_args
= innermost_deduced_args
;
17615 if (unify (tparms
, deduced_args
,
17616 INNERMOST_TEMPLATE_ARGS (spec_args
),
17617 INNERMOST_TEMPLATE_ARGS (args
),
17618 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17621 for (i
= 0; i
< ntparms
; ++i
)
17622 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17625 /* Verify that nondeduced template arguments agree with the type
17626 obtained from argument deduction.
17630 struct A { typedef int X; };
17631 template <class T, class U> struct C {};
17632 template <class T> struct C<T, typename T::X> {};
17634 Then with the instantiation `C<A, int>', we can deduce that
17635 `T' is `A' but unify () does not check whether `typename T::X'
17637 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17638 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17639 spec_args
, main_tmpl
,
17640 tf_none
, false, false);
17641 if (spec_args
== error_mark_node
17642 /* We only need to check the innermost arguments; the other
17643 arguments will always agree. */
17644 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17645 INNERMOST_TEMPLATE_ARGS (args
)))
17648 /* Now that we have bindings for all of the template arguments,
17649 ensure that the arguments deduced for the template template
17650 parameters have compatible template parameter lists. See the use
17651 of template_template_parm_bindings_ok_p in fn_type_unification
17652 for more information. */
17653 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17656 return deduced_args
;
17659 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17660 Return the TREE_LIST node with the most specialized template, if
17661 any. If there is no most specialized template, the error_mark_node
17664 Note that this function does not look at, or modify, the
17665 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17666 returned is one of the elements of INSTANTIATIONS, callers may
17667 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17668 and retrieve it from the value returned. */
17671 most_specialized_instantiation (tree templates
)
17675 ++processing_template_decl
;
17678 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17682 if (get_bindings (TREE_VALUE (champ
),
17683 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17684 NULL_TREE
, /*check_ret=*/true))
17687 if (get_bindings (TREE_VALUE (fn
),
17688 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17689 NULL_TREE
, /*check_ret=*/true))
17696 /* Equally specialized, move to next function. If there
17697 is no next function, nothing's most specialized. */
17698 fn
= TREE_CHAIN (fn
);
17706 /* Now verify that champ is better than everything earlier in the
17707 instantiation list. */
17708 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17709 if (get_bindings (TREE_VALUE (champ
),
17710 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17711 NULL_TREE
, /*check_ret=*/true)
17712 || !get_bindings (TREE_VALUE (fn
),
17713 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17714 NULL_TREE
, /*check_ret=*/true))
17720 processing_template_decl
--;
17723 return error_mark_node
;
17728 /* If DECL is a specialization of some template, return the most
17729 general such template. Otherwise, returns NULL_TREE.
17731 For example, given:
17733 template <class T> struct S { template <class U> void f(U); };
17735 if TMPL is `template <class U> void S<int>::f(U)' this will return
17736 the full template. This function will not trace past partial
17737 specializations, however. For example, given in addition:
17739 template <class T> struct S<T*> { template <class U> void f(U); };
17741 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17742 `template <class T> template <class U> S<T*>::f(U)'. */
17745 most_general_template (tree decl
)
17747 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17748 an immediate specialization. */
17749 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17751 if (DECL_TEMPLATE_INFO (decl
)) {
17752 decl
= DECL_TI_TEMPLATE (decl
);
17754 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17755 template friend. */
17756 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17762 /* Look for more and more general templates. */
17763 while (DECL_TEMPLATE_INFO (decl
))
17765 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17766 (See cp-tree.h for details.) */
17767 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17770 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17771 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17774 /* Stop if we run into an explicitly specialized class template. */
17775 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17776 && DECL_CONTEXT (decl
)
17777 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17780 decl
= DECL_TI_TEMPLATE (decl
);
17786 /* Return the most specialized of the class template partial
17787 specializations of TMPL which can produce TYPE, a specialization of
17788 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17789 a _TYPE node corresponding to the partial specialization, while the
17790 TREE_PURPOSE is the set of template arguments that must be
17791 substituted into the TREE_TYPE in order to generate TYPE.
17793 If the choice of partial specialization is ambiguous, a diagnostic
17794 is issued, and the error_mark_node is returned. If there are no
17795 partial specializations of TMPL matching TYPE, then NULL_TREE is
17799 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17801 tree list
= NULL_TREE
;
17807 tree outer_args
= NULL_TREE
;
17809 tmpl
= most_general_template (tmpl
);
17810 args
= CLASSTYPE_TI_ARGS (type
);
17812 /* For determining which partial specialization to use, only the
17813 innermost args are interesting. */
17814 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17816 outer_args
= strip_innermost_template_args (args
, 1);
17817 args
= INNERMOST_TEMPLATE_ARGS (args
);
17820 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17822 tree partial_spec_args
;
17824 tree parms
= TREE_VALUE (t
);
17826 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17828 ++processing_template_decl
;
17834 /* Discard the outer levels of args, and then substitute in the
17835 template args from the enclosing class. */
17836 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17837 partial_spec_args
= tsubst_template_args
17838 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17840 /* PARMS already refers to just the innermost parms, but the
17841 template parms in partial_spec_args had their levels lowered
17842 by tsubst, so we need to do the same for the parm list. We
17843 can't just tsubst the TREE_VEC itself, as tsubst wants to
17844 treat a TREE_VEC as an argument vector. */
17845 parms
= copy_node (parms
);
17846 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17847 TREE_VEC_ELT (parms
, i
) =
17848 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17852 partial_spec_args
=
17853 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17854 add_to_template_args (outer_args
,
17855 partial_spec_args
),
17857 /*require_all_args=*/true,
17858 /*use_default_args=*/true);
17860 --processing_template_decl
;
17862 if (partial_spec_args
== error_mark_node
)
17863 return error_mark_node
;
17865 spec_args
= get_class_bindings (tmpl
, parms
,
17871 spec_args
= add_to_template_args (outer_args
, spec_args
);
17872 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17873 TREE_TYPE (list
) = TREE_TYPE (t
);
17880 ambiguous_p
= false;
17883 t
= TREE_CHAIN (t
);
17884 for (; t
; t
= TREE_CHAIN (t
))
17886 fate
= more_specialized_class (tmpl
, champ
, t
);
17893 t
= TREE_CHAIN (t
);
17896 ambiguous_p
= true;
17905 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17907 fate
= more_specialized_class (tmpl
, champ
, t
);
17910 ambiguous_p
= true;
17918 char *spaces
= NULL
;
17919 if (!(complain
& tf_error
))
17920 return error_mark_node
;
17921 error ("ambiguous class template instantiation for %q#T", type
);
17922 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17923 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17925 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17926 spaces
= spaces
? spaces
: get_spaces (str
);
17929 return error_mark_node
;
17935 /* Explicitly instantiate DECL. */
17938 do_decl_instantiation (tree decl
, tree storage
)
17940 tree result
= NULL_TREE
;
17943 if (!decl
|| decl
== error_mark_node
)
17944 /* An error occurred, for which grokdeclarator has already issued
17945 an appropriate message. */
17947 else if (! DECL_LANG_SPECIFIC (decl
))
17949 error ("explicit instantiation of non-template %q#D", decl
);
17952 else if (TREE_CODE (decl
) == VAR_DECL
)
17954 /* There is an asymmetry here in the way VAR_DECLs and
17955 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17956 the latter, the DECL we get back will be marked as a
17957 template instantiation, and the appropriate
17958 DECL_TEMPLATE_INFO will be set up. This does not happen for
17959 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17960 should handle VAR_DECLs as it currently handles
17962 if (!DECL_CLASS_SCOPE_P (decl
))
17964 error ("%qD is not a static data member of a class template", decl
);
17967 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17968 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17970 error ("no matching template for %qD found", decl
);
17973 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17975 error ("type %qT for explicit instantiation %qD does not match "
17976 "declared type %qT", TREE_TYPE (result
), decl
,
17981 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17983 error ("explicit instantiation of %q#D", decl
);
17989 /* Check for various error cases. Note that if the explicit
17990 instantiation is valid the RESULT will currently be marked as an
17991 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17992 until we get here. */
17994 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17996 /* DR 259 [temp.spec].
17998 Both an explicit instantiation and a declaration of an explicit
17999 specialization shall not appear in a program unless the explicit
18000 instantiation follows a declaration of the explicit specialization.
18002 For a given set of template parameters, if an explicit
18003 instantiation of a template appears after a declaration of an
18004 explicit specialization for that template, the explicit
18005 instantiation has no effect. */
18008 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18012 No program shall explicitly instantiate any template more
18015 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18016 the first instantiation was `extern' and the second is not,
18017 and EXTERN_P for the opposite case. */
18018 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18019 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18020 /* If an "extern" explicit instantiation follows an ordinary
18021 explicit instantiation, the template is instantiated. */
18025 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18027 error ("no matching template for %qD found", result
);
18030 else if (!DECL_TEMPLATE_INFO (result
))
18032 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18036 if (storage
== NULL_TREE
)
18038 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18040 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18041 pedwarn (input_location
, OPT_Wpedantic
,
18042 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18047 error ("storage class %qD applied to template instantiation", storage
);
18049 check_explicit_instantiation_namespace (result
);
18050 mark_decl_instantiated (result
, extern_p
);
18052 instantiate_decl (result
, /*defer_ok=*/1,
18053 /*expl_inst_class_mem_p=*/false);
18057 mark_class_instantiated (tree t
, int extern_p
)
18059 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18060 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18061 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18062 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18065 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18066 rest_of_type_compilation (t
, 1);
18070 /* Called from do_type_instantiation through binding_table_foreach to
18071 do recursive instantiation for the type bound in ENTRY. */
18073 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18075 tree storage
= *(tree
*) data
;
18077 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18078 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18079 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18082 /* Called from do_type_instantiation to instantiate a member
18083 (a member function or a static member variable) of an
18084 explicitly instantiated class template. */
18086 instantiate_class_member (tree decl
, int extern_p
)
18088 mark_decl_instantiated (decl
, extern_p
);
18090 instantiate_decl (decl
, /*defer_ok=*/1,
18091 /*expl_inst_class_mem_p=*/true);
18094 /* Perform an explicit instantiation of template class T. STORAGE, if
18095 non-null, is the RID for extern, inline or static. COMPLAIN is
18096 nonzero if this is called from the parser, zero if called recursively,
18097 since the standard is unclear (as detailed below). */
18100 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18105 int previous_instantiation_extern_p
= 0;
18107 if (TREE_CODE (t
) == TYPE_DECL
)
18110 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18113 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18115 error ("explicit instantiation of non-class template %qD", tmpl
);
18117 error ("explicit instantiation of non-template type %qT", t
);
18123 if (!COMPLETE_TYPE_P (t
))
18125 if (complain
& tf_error
)
18126 error ("explicit instantiation of %q#T before definition of template",
18131 if (storage
!= NULL_TREE
)
18133 if (!in_system_header
)
18135 if (storage
== ridpointers
[(int) RID_EXTERN
])
18137 if (cxx_dialect
== cxx98
)
18138 pedwarn (input_location
, OPT_Wpedantic
,
18139 "ISO C++ 1998 forbids the use of %<extern%> on "
18140 "explicit instantiations");
18143 pedwarn (input_location
, OPT_Wpedantic
,
18144 "ISO C++ forbids the use of %qE"
18145 " on explicit instantiations", storage
);
18148 if (storage
== ridpointers
[(int) RID_INLINE
])
18150 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18152 else if (storage
== ridpointers
[(int) RID_STATIC
])
18156 error ("storage class %qD applied to template instantiation",
18162 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18164 /* DR 259 [temp.spec].
18166 Both an explicit instantiation and a declaration of an explicit
18167 specialization shall not appear in a program unless the explicit
18168 instantiation follows a declaration of the explicit specialization.
18170 For a given set of template parameters, if an explicit
18171 instantiation of a template appears after a declaration of an
18172 explicit specialization for that template, the explicit
18173 instantiation has no effect. */
18176 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18180 No program shall explicitly instantiate any template more
18183 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18184 instantiation was `extern'. If EXTERN_P then the second is.
18185 These cases are OK. */
18186 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18188 if (!previous_instantiation_extern_p
&& !extern_p
18189 && (complain
& tf_error
))
18190 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18192 /* If we've already instantiated the template, just return now. */
18193 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18197 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18198 mark_class_instantiated (t
, extern_p
);
18206 /* In contrast to implicit instantiation, where only the
18207 declarations, and not the definitions, of members are
18208 instantiated, we have here:
18212 The explicit instantiation of a class template specialization
18213 implies the instantiation of all of its members not
18214 previously explicitly specialized in the translation unit
18215 containing the explicit instantiation.
18217 Of course, we can't instantiate member template classes, since
18218 we don't have any arguments for them. Note that the standard
18219 is unclear on whether the instantiation of the members are
18220 *explicit* instantiations or not. However, the most natural
18221 interpretation is that it should be an explicit instantiation. */
18224 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18225 if (TREE_CODE (tmp
) == FUNCTION_DECL
18226 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18227 instantiate_class_member (tmp
, extern_p
);
18229 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18230 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18231 instantiate_class_member (tmp
, extern_p
);
18233 if (CLASSTYPE_NESTED_UTDS (t
))
18234 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18235 bt_instantiate_type_proc
, &storage
);
18239 /* Given a function DECL, which is a specialization of TMPL, modify
18240 DECL to be a re-instantiation of TMPL with the same template
18241 arguments. TMPL should be the template into which tsubst'ing
18242 should occur for DECL, not the most general template.
18244 One reason for doing this is a scenario like this:
18247 void f(const T&, int i);
18249 void g() { f(3, 7); }
18252 void f(const T& t, const int i) { }
18254 Note that when the template is first instantiated, with
18255 instantiate_template, the resulting DECL will have no name for the
18256 first parameter, and the wrong type for the second. So, when we go
18257 to instantiate the DECL, we regenerate it. */
18260 regenerate_decl_from_template (tree decl
, tree tmpl
)
18262 /* The arguments used to instantiate DECL, from the most general
18267 args
= DECL_TI_ARGS (decl
);
18268 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18270 /* Make sure that we can see identifiers, and compute access
18272 push_access_scope (decl
);
18274 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18282 args_depth
= TMPL_ARGS_DEPTH (args
);
18283 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18284 if (args_depth
> parms_depth
)
18285 args
= get_innermost_template_args (args
, parms_depth
);
18287 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18288 args
, tf_error
, NULL_TREE
,
18289 /*defer_ok*/false);
18290 if (specs
&& specs
!= error_mark_node
)
18291 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18294 /* Merge parameter declarations. */
18295 decl_parm
= skip_artificial_parms_for (decl
,
18296 DECL_ARGUMENTS (decl
));
18298 = skip_artificial_parms_for (code_pattern
,
18299 DECL_ARGUMENTS (code_pattern
));
18300 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18305 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18306 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18307 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18309 parm_type
= type_decays_to (parm_type
);
18310 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18311 TREE_TYPE (decl_parm
) = parm_type
;
18312 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18313 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18315 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18316 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18318 decl_parm
= DECL_CHAIN (decl_parm
);
18319 pattern_parm
= DECL_CHAIN (pattern_parm
);
18321 /* Merge any parameters that match with the function parameter
18323 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18326 tree expanded_types
;
18327 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18328 the parameters in this function parameter pack. */
18329 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18330 args
, tf_error
, NULL_TREE
);
18331 len
= TREE_VEC_LENGTH (expanded_types
);
18332 for (i
= 0; i
< len
; i
++)
18337 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18338 /* Rename the parameter to include the index. */
18339 DECL_NAME (decl_parm
) =
18340 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18341 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18342 parm_type
= type_decays_to (parm_type
);
18343 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18344 TREE_TYPE (decl_parm
) = parm_type
;
18345 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18346 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18348 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18349 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18351 decl_parm
= DECL_CHAIN (decl_parm
);
18354 /* Merge additional specifiers from the CODE_PATTERN. */
18355 if (DECL_DECLARED_INLINE_P (code_pattern
)
18356 && !DECL_DECLARED_INLINE_P (decl
))
18357 DECL_DECLARED_INLINE_P (decl
) = 1;
18359 else if (TREE_CODE (decl
) == VAR_DECL
)
18361 DECL_INITIAL (decl
) =
18362 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18363 tf_error
, DECL_TI_TEMPLATE (decl
),
18364 /*integral_constant_expression_p=*/false);
18365 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18366 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18367 tf_error
, DECL_TI_TEMPLATE (decl
));
18370 gcc_unreachable ();
18372 pop_access_scope (decl
);
18375 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18376 substituted to get DECL. */
18379 template_for_substitution (tree decl
)
18381 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18383 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18384 for the instantiation. This is not always the most general
18385 template. Consider, for example:
18388 struct S { template <class U> void f();
18389 template <> void f<int>(); };
18391 and an instantiation of S<double>::f<int>. We want TD to be the
18392 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18393 while (/* An instantiation cannot have a definition, so we need a
18394 more general template. */
18395 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18396 /* We must also deal with friend templates. Given:
18398 template <class T> struct S {
18399 template <class U> friend void f() {};
18402 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18403 so far as the language is concerned, but that's still
18404 where we get the pattern for the instantiation from. On
18405 other hand, if the definition comes outside the class, say:
18407 template <class T> struct S {
18408 template <class U> friend void f();
18410 template <class U> friend void f() {}
18412 we don't need to look any further. That's what the check for
18413 DECL_INITIAL is for. */
18414 || (TREE_CODE (decl
) == FUNCTION_DECL
18415 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18416 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18418 /* The present template, TD, should not be a definition. If it
18419 were a definition, we should be using it! Note that we
18420 cannot restructure the loop to just keep going until we find
18421 a template with a definition, since that might go too far if
18422 a specialization was declared, but not defined. */
18423 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18424 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18426 /* Fetch the more general template. */
18427 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18433 /* Returns true if we need to instantiate this template instance even if we
18434 know we aren't going to emit it.. */
18437 always_instantiate_p (tree decl
)
18439 /* We always instantiate inline functions so that we can inline them. An
18440 explicit instantiation declaration prohibits implicit instantiation of
18441 non-inline functions. With high levels of optimization, we would
18442 normally inline non-inline functions -- but we're not allowed to do
18443 that for "extern template" functions. Therefore, we check
18444 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18445 return ((TREE_CODE (decl
) == FUNCTION_DECL
18446 && (DECL_DECLARED_INLINE_P (decl
)
18447 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18448 /* And we need to instantiate static data members so that
18449 their initializers are available in integral constant
18451 || (TREE_CODE (decl
) == VAR_DECL
18452 && decl_maybe_constant_var_p (decl
)));
18455 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18456 instantiate it now, modifying TREE_TYPE (fn). */
18459 maybe_instantiate_noexcept (tree fn
)
18461 tree fntype
, spec
, noex
, clone
;
18463 if (DECL_CLONED_FUNCTION_P (fn
))
18464 fn
= DECL_CLONED_FUNCTION (fn
);
18465 fntype
= TREE_TYPE (fn
);
18466 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18468 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18471 noex
= TREE_PURPOSE (spec
);
18473 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18475 if (push_tinst_level (fn
))
18477 push_access_scope (fn
);
18478 push_deferring_access_checks (dk_no_deferred
);
18479 input_location
= DECL_SOURCE_LOCATION (fn
);
18480 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18481 DEFERRED_NOEXCEPT_ARGS (noex
),
18482 tf_warning_or_error
, fn
,
18483 /*function_p=*/false,
18484 /*integral_constant_expression_p=*/true);
18485 pop_deferring_access_checks ();
18486 pop_access_scope (fn
);
18487 pop_tinst_level ();
18488 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18489 if (spec
== error_mark_node
)
18490 spec
= noexcept_false_spec
;
18493 spec
= noexcept_false_spec
;
18497 /* This is an implicitly declared function, so NOEX is a list of
18498 other functions to evaluate and merge. */
18500 spec
= noexcept_true_spec
;
18501 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18503 tree fn
= OVL_CURRENT (elt
);
18505 maybe_instantiate_noexcept (fn
);
18506 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18507 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18511 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18513 FOR_EACH_CLONE (clone
, fn
)
18515 if (TREE_TYPE (clone
) == fntype
)
18516 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18518 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18522 /* Produce the definition of D, a _DECL generated from a template. If
18523 DEFER_OK is nonzero, then we don't have to actually do the
18524 instantiation now; we just have to do it sometime. Normally it is
18525 an error if this is an explicit instantiation but D is undefined.
18526 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18527 explicitly instantiated class template. */
18530 instantiate_decl (tree d
, int defer_ok
,
18531 bool expl_inst_class_mem_p
)
18533 tree tmpl
= DECL_TI_TEMPLATE (d
);
18540 bool pattern_defined
;
18541 location_t saved_loc
= input_location
;
18546 /* This function should only be used to instantiate templates for
18547 functions and static member variables. */
18548 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18549 || TREE_CODE (d
) == VAR_DECL
);
18551 /* Variables are never deferred; if instantiation is required, they
18552 are instantiated right away. That allows for better code in the
18553 case that an expression refers to the value of the variable --
18554 if the variable has a constant value the referring expression can
18555 take advantage of that fact. */
18556 if (TREE_CODE (d
) == VAR_DECL
18557 || DECL_DECLARED_CONSTEXPR_P (d
))
18560 /* Don't instantiate cloned functions. Instead, instantiate the
18561 functions they cloned. */
18562 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18563 d
= DECL_CLONED_FUNCTION (d
);
18565 if (DECL_TEMPLATE_INSTANTIATED (d
)
18566 || (TREE_CODE (d
) == FUNCTION_DECL
18567 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18568 || DECL_TEMPLATE_SPECIALIZATION (d
))
18569 /* D has already been instantiated or explicitly specialized, so
18570 there's nothing for us to do here.
18572 It might seem reasonable to check whether or not D is an explicit
18573 instantiation, and, if so, stop here. But when an explicit
18574 instantiation is deferred until the end of the compilation,
18575 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18576 the instantiation. */
18579 /* Check to see whether we know that this template will be
18580 instantiated in some other file, as with "extern template"
18582 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18584 /* In general, we do not instantiate such templates. */
18585 if (external_p
&& !always_instantiate_p (d
))
18588 gen_tmpl
= most_general_template (tmpl
);
18589 gen_args
= DECL_TI_ARGS (d
);
18591 if (tmpl
!= gen_tmpl
)
18592 /* We should already have the extra args. */
18593 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18594 == TMPL_ARGS_DEPTH (gen_args
));
18595 /* And what's in the hash table should match D. */
18596 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18597 || spec
== NULL_TREE
);
18599 /* This needs to happen before any tsubsting. */
18600 if (! push_tinst_level (d
))
18603 timevar_push (TV_TEMPLATE_INST
);
18605 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18606 for the instantiation. */
18607 td
= template_for_substitution (d
);
18608 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18610 /* We should never be trying to instantiate a member of a class
18611 template or partial specialization. */
18612 gcc_assert (d
!= code_pattern
);
18614 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18615 || DECL_TEMPLATE_SPECIALIZATION (td
))
18616 /* In the case of a friend template whose definition is provided
18617 outside the class, we may have too many arguments. Drop the
18618 ones we don't need. The same is true for specializations. */
18619 args
= get_innermost_template_args
18620 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18624 if (TREE_CODE (d
) == FUNCTION_DECL
)
18625 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18626 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18628 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18630 /* We may be in the middle of deferred access check. Disable it now. */
18631 push_deferring_access_checks (dk_no_deferred
);
18633 /* Unless an explicit instantiation directive has already determined
18634 the linkage of D, remember that a definition is available for
18636 if (pattern_defined
18637 && !DECL_INTERFACE_KNOWN (d
)
18638 && !DECL_NOT_REALLY_EXTERN (d
))
18639 mark_definable (d
);
18641 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18642 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18643 input_location
= DECL_SOURCE_LOCATION (d
);
18645 /* If D is a member of an explicitly instantiated class template,
18646 and no definition is available, treat it like an implicit
18648 if (!pattern_defined
&& expl_inst_class_mem_p
18649 && DECL_EXPLICIT_INSTANTIATION (d
))
18651 /* Leave linkage flags alone on instantiations with anonymous
18653 if (TREE_PUBLIC (d
))
18655 DECL_NOT_REALLY_EXTERN (d
) = 0;
18656 DECL_INTERFACE_KNOWN (d
) = 0;
18658 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18661 if (TREE_CODE (d
) == FUNCTION_DECL
)
18662 maybe_instantiate_noexcept (d
);
18664 /* Defer all other templates, unless we have been explicitly
18665 forbidden from doing so. */
18666 if (/* If there is no definition, we cannot instantiate the
18669 /* If it's OK to postpone instantiation, do so. */
18671 /* If this is a static data member that will be defined
18672 elsewhere, we don't want to instantiate the entire data
18673 member, but we do want to instantiate the initializer so that
18674 we can substitute that elsewhere. */
18675 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18677 /* The definition of the static data member is now required so
18678 we must substitute the initializer. */
18679 if (TREE_CODE (d
) == VAR_DECL
18680 && !DECL_INITIAL (d
)
18681 && DECL_INITIAL (code_pattern
))
18685 bool const_init
= false;
18687 ns
= decl_namespace_context (d
);
18688 push_nested_namespace (ns
);
18689 push_nested_class (DECL_CONTEXT (d
));
18690 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18692 tf_warning_or_error
, NULL_TREE
,
18693 /*integral_constant_expression_p=*/false);
18694 /* Make sure the initializer is still constant, in case of
18695 circular dependency (template/instantiate6.C). */
18697 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18698 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18699 /*asmspec_tree=*/NULL_TREE
,
18700 LOOKUP_ONLYCONVERTING
);
18701 pop_nested_class ();
18702 pop_nested_namespace (ns
);
18705 /* We restore the source position here because it's used by
18706 add_pending_template. */
18707 input_location
= saved_loc
;
18709 if (at_eof
&& !pattern_defined
18710 && DECL_EXPLICIT_INSTANTIATION (d
)
18711 && DECL_NOT_REALLY_EXTERN (d
))
18714 The definition of a non-exported function template, a
18715 non-exported member function template, or a non-exported
18716 member function or static data member of a class template
18717 shall be present in every translation unit in which it is
18718 explicitly instantiated. */
18719 permerror (input_location
, "explicit instantiation of %qD "
18720 "but no definition available", d
);
18722 /* If we're in unevaluated context, we just wanted to get the
18723 constant value; this isn't an odr use, so don't queue
18724 a full instantiation. */
18725 if (cp_unevaluated_operand
!= 0)
18727 /* ??? Historically, we have instantiated inline functions, even
18728 when marked as "extern template". */
18729 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18730 add_pending_template (d
);
18733 /* Tell the repository that D is available in this translation unit
18734 -- and see if it is supposed to be instantiated here. */
18735 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18737 /* In a PCH file, despite the fact that the repository hasn't
18738 requested instantiation in the PCH it is still possible that
18739 an instantiation will be required in a file that includes the
18742 add_pending_template (d
);
18743 /* Instantiate inline functions so that the inliner can do its
18744 job, even though we'll not be emitting a copy of this
18746 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18750 fn_context
= decl_function_context (d
);
18751 nested
= (current_function_decl
!= NULL_TREE
);
18753 push_to_top_level ();
18755 push_function_context ();
18757 /* Mark D as instantiated so that recursive calls to
18758 instantiate_decl do not try to instantiate it again. */
18759 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18761 /* Regenerate the declaration in case the template has been modified
18762 by a subsequent redeclaration. */
18763 regenerate_decl_from_template (d
, td
);
18765 /* We already set the file and line above. Reset them now in case
18766 they changed as a result of calling regenerate_decl_from_template. */
18767 input_location
= DECL_SOURCE_LOCATION (d
);
18769 if (TREE_CODE (d
) == VAR_DECL
)
18772 bool const_init
= false;
18774 /* Clear out DECL_RTL; whatever was there before may not be right
18775 since we've reset the type of the declaration. */
18776 SET_DECL_RTL (d
, NULL
);
18777 DECL_IN_AGGR_P (d
) = 0;
18779 /* The initializer is placed in DECL_INITIAL by
18780 regenerate_decl_from_template so we don't need to
18781 push/pop_access_scope again here. Pull it out so that
18782 cp_finish_decl can process it. */
18783 init
= DECL_INITIAL (d
);
18784 DECL_INITIAL (d
) = NULL_TREE
;
18785 DECL_INITIALIZED_P (d
) = 0;
18787 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18788 initializer. That function will defer actual emission until
18789 we have a chance to determine linkage. */
18790 DECL_EXTERNAL (d
) = 0;
18792 /* Enter the scope of D so that access-checking works correctly. */
18793 push_nested_class (DECL_CONTEXT (d
));
18794 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18795 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18796 pop_nested_class ();
18798 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18799 synthesize_method (d
);
18800 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18802 struct pointer_map_t
*saved_local_specializations
;
18807 /* Save away the current list, in case we are instantiating one
18808 template from within the body of another. */
18809 saved_local_specializations
= local_specializations
;
18811 /* Set up the list of local specializations. */
18812 local_specializations
= pointer_map_create ();
18814 /* Set up context. */
18815 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18817 /* Some typedefs referenced from within the template code need to be
18818 access checked at template instantiation time, i.e now. These
18819 types were added to the template at parsing time. Let's get those
18820 and perform the access checks then. */
18821 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18824 /* Create substitution entries for the parameters. */
18825 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18826 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18827 spec_parm
= DECL_ARGUMENTS (d
);
18828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18830 register_local_specialization (spec_parm
, tmpl_parm
);
18831 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18832 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18834 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18836 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18838 register_local_specialization (spec_parm
, tmpl_parm
);
18839 spec_parm
= DECL_CHAIN (spec_parm
);
18843 /* Register the (value) argument pack as a specialization of
18844 TMPL_PARM, then move on. */
18845 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18846 register_local_specialization (argpack
, tmpl_parm
);
18849 gcc_assert (!spec_parm
);
18851 /* Substitute into the body of the function. */
18852 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18853 tf_warning_or_error
, tmpl
,
18854 /*integral_constant_expression_p=*/false);
18856 /* Set the current input_location to the end of the function
18857 so that finish_function knows where we are. */
18858 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18860 /* We don't need the local specializations any more. */
18861 pointer_map_destroy (local_specializations
);
18862 local_specializations
= saved_local_specializations
;
18864 /* Finish the function. */
18865 d
= finish_function (0);
18866 expand_or_defer_fn (d
);
18869 /* We're not deferring instantiation any more. */
18870 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18873 pop_from_top_level ();
18875 pop_function_context ();
18878 input_location
= saved_loc
;
18879 pop_deferring_access_checks ();
18880 pop_tinst_level ();
18882 timevar_pop (TV_TEMPLATE_INST
);
18887 /* Run through the list of templates that we wish we could
18888 instantiate, and instantiate any we can. RETRIES is the
18889 number of times we retry pending template instantiation. */
18892 instantiate_pending_templates (int retries
)
18895 location_t saved_loc
= input_location
;
18897 /* Instantiating templates may trigger vtable generation. This in turn
18898 may require further template instantiations. We place a limit here
18899 to avoid infinite loop. */
18900 if (pending_templates
&& retries
>= max_tinst_depth
)
18902 tree decl
= pending_templates
->tinst
->decl
;
18904 error ("template instantiation depth exceeds maximum of %d"
18905 " instantiating %q+D, possibly from virtual table generation"
18906 " (use -ftemplate-depth= to increase the maximum)",
18907 max_tinst_depth
, decl
);
18908 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18909 /* Pretend that we defined it. */
18910 DECL_INITIAL (decl
) = error_mark_node
;
18916 struct pending_template
**t
= &pending_templates
;
18917 struct pending_template
*last
= NULL
;
18921 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18922 bool complete
= false;
18924 if (TYPE_P (instantiation
))
18928 if (!COMPLETE_TYPE_P (instantiation
))
18930 instantiate_class_template (instantiation
);
18931 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18932 for (fn
= TYPE_METHODS (instantiation
);
18934 fn
= TREE_CHAIN (fn
))
18935 if (! DECL_ARTIFICIAL (fn
))
18936 instantiate_decl (fn
,
18938 /*expl_inst_class_mem_p=*/false);
18939 if (COMPLETE_TYPE_P (instantiation
))
18943 complete
= COMPLETE_TYPE_P (instantiation
);
18947 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18948 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18951 = instantiate_decl (instantiation
,
18953 /*expl_inst_class_mem_p=*/false);
18954 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18958 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18959 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18963 /* If INSTANTIATION has been instantiated, then we don't
18964 need to consider it again in the future. */
18972 current_tinst_level
= NULL
;
18974 last_pending_template
= last
;
18976 while (reconsider
);
18978 input_location
= saved_loc
;
18981 /* Substitute ARGVEC into T, which is a list of initializers for
18982 either base class or a non-static data member. The TREE_PURPOSEs
18983 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18984 instantiate_decl. */
18987 tsubst_initializer_list (tree t
, tree argvec
)
18989 tree inits
= NULL_TREE
;
18991 for (; t
; t
= TREE_CHAIN (t
))
18995 tree expanded_bases
= NULL_TREE
;
18996 tree expanded_arguments
= NULL_TREE
;
18999 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19004 /* Expand the base class expansion type into separate base
19006 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19007 tf_warning_or_error
,
19009 if (expanded_bases
== error_mark_node
)
19012 /* We'll be building separate TREE_LISTs of arguments for
19014 len
= TREE_VEC_LENGTH (expanded_bases
);
19015 expanded_arguments
= make_tree_vec (len
);
19016 for (i
= 0; i
< len
; i
++)
19017 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19019 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19020 expand each argument in the TREE_VALUE of t. */
19021 expr
= make_node (EXPR_PACK_EXPANSION
);
19022 PACK_EXPANSION_LOCAL_P (expr
) = true;
19023 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19024 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19026 if (TREE_VALUE (t
) == void_type_node
)
19027 /* VOID_TYPE_NODE is used to indicate
19028 value-initialization. */
19030 for (i
= 0; i
< len
; i
++)
19031 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19035 /* Substitute parameter packs into each argument in the
19037 in_base_initializer
= 1;
19038 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19040 tree expanded_exprs
;
19042 /* Expand the argument. */
19043 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19045 = tsubst_pack_expansion (expr
, argvec
,
19046 tf_warning_or_error
,
19048 if (expanded_exprs
== error_mark_node
)
19051 /* Prepend each of the expanded expressions to the
19052 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19053 for (i
= 0; i
< len
; i
++)
19055 TREE_VEC_ELT (expanded_arguments
, i
) =
19056 tree_cons (NULL_TREE
,
19057 TREE_VEC_ELT (expanded_exprs
, i
),
19058 TREE_VEC_ELT (expanded_arguments
, i
));
19061 in_base_initializer
= 0;
19063 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19064 since we built them backwards. */
19065 for (i
= 0; i
< len
; i
++)
19067 TREE_VEC_ELT (expanded_arguments
, i
) =
19068 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19073 for (i
= 0; i
< len
; ++i
)
19075 if (expanded_bases
)
19077 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19078 decl
= expand_member_init (decl
);
19079 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19084 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19085 tf_warning_or_error
, NULL_TREE
);
19087 decl
= expand_member_init (decl
);
19088 if (decl
&& !DECL_P (decl
))
19089 in_base_initializer
= 1;
19091 init
= TREE_VALUE (t
);
19093 if (init
!= void_type_node
)
19094 init
= tsubst_expr (init
, argvec
,
19095 tf_warning_or_error
, NULL_TREE
,
19096 /*integral_constant_expression_p=*/false);
19097 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19098 /* If we had an initializer but it instantiated to nothing,
19099 value-initialize the object. This will only occur when
19100 the initializer was a pack expansion where the parameter
19101 packs used in that expansion were of length zero. */
19102 init
= void_type_node
;
19103 in_base_initializer
= 0;
19108 init
= build_tree_list (decl
, init
);
19109 TREE_CHAIN (init
) = inits
;
19117 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19120 set_current_access_from_decl (tree decl
)
19122 if (TREE_PRIVATE (decl
))
19123 current_access_specifier
= access_private_node
;
19124 else if (TREE_PROTECTED (decl
))
19125 current_access_specifier
= access_protected_node
;
19127 current_access_specifier
= access_public_node
;
19130 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19131 is the instantiation (which should have been created with
19132 start_enum) and ARGS are the template arguments to use. */
19135 tsubst_enum (tree tag
, tree newtag
, tree args
)
19139 if (SCOPED_ENUM_P (newtag
))
19140 begin_scope (sk_scoped_enum
, newtag
);
19142 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19147 decl
= TREE_VALUE (e
);
19148 /* Note that in a template enum, the TREE_VALUE is the
19149 CONST_DECL, not the corresponding INTEGER_CST. */
19150 value
= tsubst_expr (DECL_INITIAL (decl
),
19151 args
, tf_warning_or_error
, NULL_TREE
,
19152 /*integral_constant_expression_p=*/true);
19154 /* Give this enumeration constant the correct access. */
19155 set_current_access_from_decl (decl
);
19157 /* Actually build the enumerator itself. */
19159 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19162 if (SCOPED_ENUM_P (newtag
))
19165 finish_enum_value_list (newtag
);
19166 finish_enum (newtag
);
19168 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19169 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19172 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19173 its type -- but without substituting the innermost set of template
19174 arguments. So, innermost set of template parameters will appear in
19178 get_mostly_instantiated_function_type (tree decl
)
19186 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19187 targs
= DECL_TI_ARGS (decl
);
19188 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19189 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19191 /* There should be as many levels of arguments as there are levels
19193 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19195 fn_type
= TREE_TYPE (tmpl
);
19197 if (parm_depth
== 1)
19198 /* No substitution is necessary. */
19205 /* Replace the innermost level of the TARGS with NULL_TREEs to
19206 let tsubst know not to substitute for those parameters. */
19207 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19208 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19209 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19210 TMPL_ARGS_LEVEL (targs
, i
));
19211 SET_TMPL_ARGS_LEVEL (partial_args
,
19212 TMPL_ARGS_DEPTH (targs
),
19213 make_tree_vec (DECL_NTPARMS (tmpl
)));
19215 /* Make sure that we can see identifiers, and compute access
19217 push_access_scope (decl
);
19219 ++processing_template_decl
;
19220 /* Now, do the (partial) substitution to figure out the
19221 appropriate function type. */
19222 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19223 --processing_template_decl
;
19225 /* Substitute into the template parameters to obtain the real
19226 innermost set of parameters. This step is important if the
19227 innermost set of template parameters contains value
19228 parameters whose types depend on outer template parameters. */
19229 TREE_VEC_LENGTH (partial_args
)--;
19230 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19232 pop_access_scope (decl
);
19238 /* Return truthvalue if we're processing a template different from
19239 the last one involved in diagnostics. */
19241 problematic_instantiation_changed (void)
19243 return current_tinst_level
!= last_error_tinst_level
;
19246 /* Remember current template involved in diagnostics. */
19248 record_last_problematic_instantiation (void)
19250 last_error_tinst_level
= current_tinst_level
;
19253 struct tinst_level
*
19254 current_instantiation (void)
19256 return current_tinst_level
;
19259 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19260 type. Return zero for ok, nonzero for disallowed. Issue error and
19261 warning messages under control of COMPLAIN. */
19264 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19266 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19268 else if (POINTER_TYPE_P (type
))
19270 else if (TYPE_PTRMEM_P (type
))
19272 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19274 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19276 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19278 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19281 if (complain
& tf_error
)
19283 if (type
== error_mark_node
)
19284 inform (input_location
, "invalid template non-type parameter");
19286 error ("%q#T is not a valid type for a template non-type parameter",
19292 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19293 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19296 dependent_type_p_r (tree type
)
19302 A type is dependent if it is:
19304 -- a template parameter. Template template parameters are types
19305 for us (since TYPE_P holds true for them) so we handle
19307 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19308 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19310 /* -- a qualified-id with a nested-name-specifier which contains a
19311 class-name that names a dependent type or whose unqualified-id
19312 names a dependent type. */
19313 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19315 /* -- a cv-qualified type where the cv-unqualified type is
19317 type
= TYPE_MAIN_VARIANT (type
);
19318 /* -- a compound type constructed from any dependent type. */
19319 if (TYPE_PTRMEM_P (type
))
19320 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19321 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19323 else if (TREE_CODE (type
) == POINTER_TYPE
19324 || TREE_CODE (type
) == REFERENCE_TYPE
)
19325 return dependent_type_p (TREE_TYPE (type
));
19326 else if (TREE_CODE (type
) == FUNCTION_TYPE
19327 || TREE_CODE (type
) == METHOD_TYPE
)
19331 if (dependent_type_p (TREE_TYPE (type
)))
19333 for (arg_type
= TYPE_ARG_TYPES (type
);
19335 arg_type
= TREE_CHAIN (arg_type
))
19336 if (dependent_type_p (TREE_VALUE (arg_type
)))
19340 /* -- an array type constructed from any dependent type or whose
19341 size is specified by a constant expression that is
19344 We checked for type- and value-dependence of the bounds in
19345 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19346 if (TREE_CODE (type
) == ARRAY_TYPE
)
19348 if (TYPE_DOMAIN (type
)
19349 && dependent_type_p (TYPE_DOMAIN (type
)))
19351 return dependent_type_p (TREE_TYPE (type
));
19354 /* -- a template-id in which either the template name is a template
19356 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19358 /* ... or any of the template arguments is a dependent type or
19359 an expression that is type-dependent or value-dependent. */
19360 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19361 && (any_dependent_template_arguments_p
19362 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19365 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19366 dependent; if the argument of the `typeof' expression is not
19367 type-dependent, then it should already been have resolved. */
19368 if (TREE_CODE (type
) == TYPEOF_TYPE
19369 || TREE_CODE (type
) == DECLTYPE_TYPE
19370 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19373 /* A template argument pack is dependent if any of its packed
19375 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19377 tree args
= ARGUMENT_PACK_ARGS (type
);
19378 int i
, len
= TREE_VEC_LENGTH (args
);
19379 for (i
= 0; i
< len
; ++i
)
19380 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19384 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19385 be template parameters. */
19386 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19389 /* The standard does not specifically mention types that are local
19390 to template functions or local classes, but they should be
19391 considered dependent too. For example:
19393 template <int I> void f() {
19398 The size of `E' cannot be known until the value of `I' has been
19399 determined. Therefore, `E' must be considered dependent. */
19400 scope
= TYPE_CONTEXT (type
);
19401 if (scope
&& TYPE_P (scope
))
19402 return dependent_type_p (scope
);
19403 /* Don't use type_dependent_expression_p here, as it can lead
19404 to infinite recursion trying to determine whether a lambda
19405 nested in a lambda is dependent (c++/47687). */
19406 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19407 && DECL_LANG_SPECIFIC (scope
)
19408 && DECL_TEMPLATE_INFO (scope
)
19409 && (any_dependent_template_arguments_p
19410 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19413 /* Other types are non-dependent. */
19417 /* Returns TRUE if TYPE is dependent, in the sense of
19418 [temp.dep.type]. Note that a NULL type is considered dependent. */
19421 dependent_type_p (tree type
)
19423 /* If there are no template parameters in scope, then there can't be
19424 any dependent types. */
19425 if (!processing_template_decl
)
19427 /* If we are not processing a template, then nobody should be
19428 providing us with a dependent type. */
19430 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19434 /* If the type is NULL, we have not computed a type for the entity
19435 in question; in that case, the type is dependent. */
19439 /* Erroneous types can be considered non-dependent. */
19440 if (type
== error_mark_node
)
19443 /* If we have not already computed the appropriate value for TYPE,
19445 if (!TYPE_DEPENDENT_P_VALID (type
))
19447 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19448 TYPE_DEPENDENT_P_VALID (type
) = 1;
19451 return TYPE_DEPENDENT_P (type
);
19454 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19455 lookup. In other words, a dependent type that is not the current
19459 dependent_scope_p (tree scope
)
19461 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19462 && !currently_open_class (scope
));
19465 /* T is a SCOPE_REF; return whether we need to consider it
19466 instantiation-dependent so that we can check access at instantiation
19467 time even though we know which member it resolves to. */
19470 instantiation_dependent_scope_ref_p (tree t
)
19472 if (DECL_P (TREE_OPERAND (t
, 1))
19473 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
19474 && accessible_in_template_p (TREE_OPERAND (t
, 0),
19475 TREE_OPERAND (t
, 1)))
19481 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19482 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19485 /* Note that this predicate is not appropriate for general expressions;
19486 only constant expressions (that satisfy potential_constant_expression)
19487 can be tested for value dependence. */
19490 value_dependent_expression_p (tree expression
)
19492 if (!processing_template_decl
)
19495 /* A name declared with a dependent type. */
19496 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19499 switch (TREE_CODE (expression
))
19501 case IDENTIFIER_NODE
:
19502 /* A name that has not been looked up -- must be dependent. */
19505 case TEMPLATE_PARM_INDEX
:
19506 /* A non-type template parm. */
19510 /* A non-type template parm. */
19511 if (DECL_TEMPLATE_PARM_P (expression
))
19513 return value_dependent_expression_p (DECL_INITIAL (expression
));
19516 /* A constant with literal type and is initialized
19517 with an expression that is value-dependent.
19519 Note that a non-dependent parenthesized initializer will have
19520 already been replaced with its constant value, so if we see
19521 a TREE_LIST it must be dependent. */
19522 if (DECL_INITIAL (expression
)
19523 && decl_constant_var_p (expression
)
19524 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19525 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19529 case DYNAMIC_CAST_EXPR
:
19530 case STATIC_CAST_EXPR
:
19531 case CONST_CAST_EXPR
:
19532 case REINTERPRET_CAST_EXPR
:
19534 /* These expressions are value-dependent if the type to which
19535 the cast occurs is dependent or the expression being casted
19536 is value-dependent. */
19538 tree type
= TREE_TYPE (expression
);
19540 if (dependent_type_p (type
))
19543 /* A functional cast has a list of operands. */
19544 expression
= TREE_OPERAND (expression
, 0);
19547 /* If there are no operands, it must be an expression such
19548 as "int()". This should not happen for aggregate types
19549 because it would form non-constant expressions. */
19550 gcc_assert (cxx_dialect
>= cxx0x
19551 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19556 if (TREE_CODE (expression
) == TREE_LIST
)
19557 return any_value_dependent_elements_p (expression
);
19559 return value_dependent_expression_p (expression
);
19563 if (SIZEOF_EXPR_TYPE_P (expression
))
19564 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
19568 /* A `sizeof' expression is value-dependent if the operand is
19569 type-dependent or is a pack expansion. */
19570 expression
= TREE_OPERAND (expression
, 0);
19571 if (PACK_EXPANSION_P (expression
))
19573 else if (TYPE_P (expression
))
19574 return dependent_type_p (expression
);
19575 return instantiation_dependent_expression_p (expression
);
19577 case AT_ENCODE_EXPR
:
19578 /* An 'encode' expression is value-dependent if the operand is
19580 expression
= TREE_OPERAND (expression
, 0);
19581 return dependent_type_p (expression
);
19583 case NOEXCEPT_EXPR
:
19584 expression
= TREE_OPERAND (expression
, 0);
19585 return instantiation_dependent_expression_p (expression
);
19588 /* All instantiation-dependent expressions should also be considered
19589 value-dependent. */
19590 return instantiation_dependent_scope_ref_p (expression
);
19592 case COMPONENT_REF
:
19593 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19594 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19596 case NONTYPE_ARGUMENT_PACK
:
19597 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19598 is value-dependent. */
19600 tree values
= ARGUMENT_PACK_ARGS (expression
);
19601 int i
, len
= TREE_VEC_LENGTH (values
);
19603 for (i
= 0; i
< len
; ++i
)
19604 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19612 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19613 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19614 || (type2
? dependent_type_p (type2
) : false));
19618 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19619 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19622 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19623 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19627 tree op
= TREE_OPERAND (expression
, 0);
19628 return (value_dependent_expression_p (op
)
19629 || has_value_dependent_address (op
));
19634 tree fn
= get_callee_fndecl (expression
);
19636 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19638 nargs
= call_expr_nargs (expression
);
19639 for (i
= 0; i
< nargs
; ++i
)
19641 tree op
= CALL_EXPR_ARG (expression
, i
);
19642 /* In a call to a constexpr member function, look through the
19643 implicit ADDR_EXPR on the object argument so that it doesn't
19644 cause the call to be considered value-dependent. We also
19645 look through it in potential_constant_expression. */
19646 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19647 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19648 && TREE_CODE (op
) == ADDR_EXPR
)
19649 op
= TREE_OPERAND (op
, 0);
19650 if (value_dependent_expression_p (op
))
19656 case TEMPLATE_ID_EXPR
:
19657 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19659 return type_dependent_expression_p (expression
);
19665 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19666 if (value_dependent_expression_p (val
))
19672 /* Treat a GNU statement expression as dependent to avoid crashing
19673 under fold_non_dependent_expr; it can't be constant. */
19677 /* A constant expression is value-dependent if any subexpression is
19678 value-dependent. */
19679 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19681 case tcc_reference
:
19683 case tcc_comparison
:
19685 case tcc_expression
:
19688 int i
, len
= cp_tree_operand_length (expression
);
19690 for (i
= 0; i
< len
; i
++)
19692 tree t
= TREE_OPERAND (expression
, i
);
19694 /* In some cases, some of the operands may be missing.l
19695 (For example, in the case of PREDECREMENT_EXPR, the
19696 amount to increment by may be missing.) That doesn't
19697 make the expression dependent. */
19698 if (t
&& value_dependent_expression_p (t
))
19709 /* The expression is not value-dependent. */
19713 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19714 [temp.dep.expr]. Note that an expression with no type is
19715 considered dependent. Other parts of the compiler arrange for an
19716 expression with type-dependent subexpressions to have no type, so
19717 this function doesn't have to be fully recursive. */
19720 type_dependent_expression_p (tree expression
)
19722 if (!processing_template_decl
)
19725 if (expression
== error_mark_node
)
19728 /* An unresolved name is always dependent. */
19729 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19730 || TREE_CODE (expression
) == USING_DECL
)
19733 /* Some expression forms are never type-dependent. */
19734 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19735 || TREE_CODE (expression
) == SIZEOF_EXPR
19736 || TREE_CODE (expression
) == ALIGNOF_EXPR
19737 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19738 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19739 || TREE_CODE (expression
) == TRAIT_EXPR
19740 || TREE_CODE (expression
) == TYPEID_EXPR
19741 || TREE_CODE (expression
) == DELETE_EXPR
19742 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19743 || TREE_CODE (expression
) == THROW_EXPR
)
19746 /* The types of these expressions depends only on the type to which
19747 the cast occurs. */
19748 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19749 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19750 || TREE_CODE (expression
) == CONST_CAST_EXPR
19751 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19752 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19753 || TREE_CODE (expression
) == CAST_EXPR
)
19754 return dependent_type_p (TREE_TYPE (expression
));
19756 /* The types of these expressions depends only on the type created
19757 by the expression. */
19758 if (TREE_CODE (expression
) == NEW_EXPR
19759 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19761 /* For NEW_EXPR tree nodes created inside a template, either
19762 the object type itself or a TREE_LIST may appear as the
19764 tree type
= TREE_OPERAND (expression
, 1);
19765 if (TREE_CODE (type
) == TREE_LIST
)
19766 /* This is an array type. We need to check array dimensions
19768 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19769 || value_dependent_expression_p
19770 (TREE_OPERAND (TREE_VALUE (type
), 1));
19772 return dependent_type_p (type
);
19775 if (TREE_CODE (expression
) == SCOPE_REF
)
19777 tree scope
= TREE_OPERAND (expression
, 0);
19778 tree name
= TREE_OPERAND (expression
, 1);
19780 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19781 contains an identifier associated by name lookup with one or more
19782 declarations declared with a dependent type, or...a
19783 nested-name-specifier or qualified-id that names a member of an
19784 unknown specialization. */
19785 return (type_dependent_expression_p (name
)
19786 || dependent_scope_p (scope
));
19789 if (TREE_CODE (expression
) == FUNCTION_DECL
19790 && DECL_LANG_SPECIFIC (expression
)
19791 && DECL_TEMPLATE_INFO (expression
)
19792 && (any_dependent_template_arguments_p
19793 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19796 if (TREE_CODE (expression
) == TEMPLATE_DECL
19797 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19800 if (TREE_CODE (expression
) == STMT_EXPR
)
19801 expression
= stmt_expr_value_expr (expression
);
19803 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19808 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19810 if (type_dependent_expression_p (elt
))
19816 /* A static data member of the current instantiation with incomplete
19817 array type is type-dependent, as the definition and specializations
19818 can have different bounds. */
19819 if (TREE_CODE (expression
) == VAR_DECL
19820 && DECL_CLASS_SCOPE_P (expression
)
19821 && dependent_type_p (DECL_CONTEXT (expression
))
19822 && VAR_HAD_UNKNOWN_BOUND (expression
))
19825 if (TREE_TYPE (expression
) == unknown_type_node
)
19827 if (TREE_CODE (expression
) == ADDR_EXPR
)
19828 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19829 if (TREE_CODE (expression
) == COMPONENT_REF
19830 || TREE_CODE (expression
) == OFFSET_REF
)
19832 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19834 expression
= TREE_OPERAND (expression
, 1);
19835 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19838 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19839 if (TREE_CODE (expression
) == SCOPE_REF
)
19842 if (BASELINK_P (expression
))
19843 expression
= BASELINK_FUNCTIONS (expression
);
19845 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19847 if (any_dependent_template_arguments_p
19848 (TREE_OPERAND (expression
, 1)))
19850 expression
= TREE_OPERAND (expression
, 0);
19852 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19853 || TREE_CODE (expression
) == FUNCTION_DECL
);
19857 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19859 expression
= OVL_NEXT (expression
);
19864 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19866 return (dependent_type_p (TREE_TYPE (expression
)));
19869 /* walk_tree callback function for instantiation_dependent_expression_p,
19870 below. Returns non-zero if a dependent subexpression is found. */
19873 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19878 /* We don't have to worry about decltype currently because decltype
19879 of an instantiation-dependent expr is a dependent type. This
19880 might change depending on the resolution of DR 1172. */
19881 *walk_subtrees
= false;
19884 enum tree_code code
= TREE_CODE (*tp
);
19887 /* Don't treat an argument list as dependent just because it has no
19893 case TEMPLATE_PARM_INDEX
:
19896 /* Handle expressions with type operands. */
19900 case AT_ENCODE_EXPR
:
19902 tree op
= TREE_OPERAND (*tp
, 0);
19903 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
19904 op
= TREE_TYPE (op
);
19907 if (dependent_type_p (op
))
19911 *walk_subtrees
= false;
19919 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
19920 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
)))
19922 *walk_subtrees
= false;
19925 case COMPONENT_REF
:
19926 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19927 /* In a template, finish_class_member_access_expr creates a
19928 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19929 type-dependent, so that we can check access control at
19930 instantiation time (PR 42277). See also Core issue 1273. */
19935 if (instantiation_dependent_scope_ref_p (*tp
))
19940 /* Treat statement-expressions as dependent. */
19948 if (type_dependent_expression_p (*tp
))
19954 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19955 sense defined by the ABI:
19957 "An expression is instantiation-dependent if it is type-dependent
19958 or value-dependent, or it has a subexpression that is type-dependent
19959 or value-dependent." */
19962 instantiation_dependent_expression_p (tree expression
)
19966 if (!processing_template_decl
)
19969 if (expression
== error_mark_node
)
19972 result
= cp_walk_tree_without_duplicates (&expression
,
19973 instantiation_dependent_r
, NULL
);
19974 return result
!= NULL_TREE
;
19977 /* Like type_dependent_expression_p, but it also works while not processing
19978 a template definition, i.e. during substitution or mangling. */
19981 type_dependent_expression_p_push (tree expr
)
19984 ++processing_template_decl
;
19985 b
= type_dependent_expression_p (expr
);
19986 --processing_template_decl
;
19990 /* Returns TRUE if ARGS contains a type-dependent expression. */
19993 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
19998 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20000 if (type_dependent_expression_p (arg
))
20006 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20007 expressions) contains any type-dependent expressions. */
20010 any_type_dependent_elements_p (const_tree list
)
20012 for (; list
; list
= TREE_CHAIN (list
))
20013 if (value_dependent_expression_p (TREE_VALUE (list
)))
20019 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20020 expressions) contains any value-dependent expressions. */
20023 any_value_dependent_elements_p (const_tree list
)
20025 for (; list
; list
= TREE_CHAIN (list
))
20026 if (value_dependent_expression_p (TREE_VALUE (list
)))
20032 /* Returns TRUE if the ARG (a template argument) is dependent. */
20035 dependent_template_arg_p (tree arg
)
20037 if (!processing_template_decl
)
20040 /* Assume a template argument that was wrongly written by the user
20041 is dependent. This is consistent with what
20042 any_dependent_template_arguments_p [that calls this function]
20044 if (!arg
|| arg
== error_mark_node
)
20047 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20048 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20050 if (TREE_CODE (arg
) == TEMPLATE_DECL
20051 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20052 return dependent_template_p (arg
);
20053 else if (ARGUMENT_PACK_P (arg
))
20055 tree args
= ARGUMENT_PACK_ARGS (arg
);
20056 int i
, len
= TREE_VEC_LENGTH (args
);
20057 for (i
= 0; i
< len
; ++i
)
20059 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20065 else if (TYPE_P (arg
))
20066 return dependent_type_p (arg
);
20068 return (type_dependent_expression_p (arg
)
20069 || value_dependent_expression_p (arg
));
20072 /* Returns true if ARGS (a collection of template arguments) contains
20073 any types that require structural equality testing. */
20076 any_template_arguments_need_structural_equality_p (tree args
)
20083 if (args
== error_mark_node
)
20086 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20088 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20089 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20091 tree arg
= TREE_VEC_ELT (level
, j
);
20092 tree packed_args
= NULL_TREE
;
20095 if (ARGUMENT_PACK_P (arg
))
20097 /* Look inside the argument pack. */
20098 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20099 len
= TREE_VEC_LENGTH (packed_args
);
20102 for (k
= 0; k
< len
; ++k
)
20105 arg
= TREE_VEC_ELT (packed_args
, k
);
20107 if (error_operand_p (arg
))
20109 else if (TREE_CODE (arg
) == TEMPLATE_DECL
20110 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20112 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20114 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20115 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20124 /* Returns true if ARGS (a collection of template arguments) contains
20125 any dependent arguments. */
20128 any_dependent_template_arguments_p (const_tree args
)
20135 if (args
== error_mark_node
)
20138 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20140 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20141 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20142 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20149 /* Returns TRUE if the template TMPL is dependent. */
20152 dependent_template_p (tree tmpl
)
20154 if (TREE_CODE (tmpl
) == OVERLOAD
)
20158 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20160 tmpl
= OVL_NEXT (tmpl
);
20165 /* Template template parameters are dependent. */
20166 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20167 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20169 /* So are names that have not been looked up. */
20170 if (TREE_CODE (tmpl
) == SCOPE_REF
20171 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
20173 /* So are member templates of dependent classes. */
20174 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20175 return dependent_type_p (DECL_CONTEXT (tmpl
));
20179 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20182 dependent_template_id_p (tree tmpl
, tree args
)
20184 return (dependent_template_p (tmpl
)
20185 || any_dependent_template_arguments_p (args
));
20188 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20192 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20196 if (!processing_template_decl
)
20199 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20201 tree decl
= TREE_VEC_ELT (declv
, i
);
20202 tree init
= TREE_VEC_ELT (initv
, i
);
20203 tree cond
= TREE_VEC_ELT (condv
, i
);
20204 tree incr
= TREE_VEC_ELT (incrv
, i
);
20206 if (type_dependent_expression_p (decl
))
20209 if (init
&& type_dependent_expression_p (init
))
20212 if (type_dependent_expression_p (cond
))
20215 if (COMPARISON_CLASS_P (cond
)
20216 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20217 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20220 if (TREE_CODE (incr
) == MODOP_EXPR
)
20222 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20223 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20226 else if (type_dependent_expression_p (incr
))
20228 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20230 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20232 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20234 tree t
= TREE_OPERAND (incr
, 1);
20235 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20236 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20245 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20246 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20247 no such TYPE can be found. Note that this function peers inside
20248 uninstantiated templates and therefore should be used only in
20249 extremely limited situations. ONLY_CURRENT_P restricts this
20250 peering to the currently open classes hierarchy (which is required
20251 when comparing types). */
20254 resolve_typename_type (tree type
, bool only_current_p
)
20263 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20265 scope
= TYPE_CONTEXT (type
);
20266 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20267 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20268 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20269 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20270 identifier of the TYPENAME_TYPE anymore.
20271 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20272 TYPENAME_TYPE instead, we avoid messing up with a possible
20273 typedef variant case. */
20274 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20276 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20277 it first before we can figure out what NAME refers to. */
20278 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20280 if (TYPENAME_IS_RESOLVING_P (scope
))
20281 /* Given a class template A with a dependent base with nested type C,
20282 typedef typename A::C::C C will land us here, as trying to resolve
20283 the initial A::C leads to the local C typedef, which leads back to
20284 A::C::C. So we break the recursion now. */
20287 scope
= resolve_typename_type (scope
, only_current_p
);
20289 /* If we don't know what SCOPE refers to, then we cannot resolve the
20291 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20293 /* If the SCOPE is a template type parameter, we have no way of
20294 resolving the name. */
20295 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20297 /* If the SCOPE is not the current instantiation, there's no reason
20298 to look inside it. */
20299 if (only_current_p
&& !currently_open_class (scope
))
20301 /* If this is a typedef, we don't want to look inside (c++/11987). */
20302 if (typedef_variant_p (type
))
20304 /* If SCOPE isn't the template itself, it will not have a valid
20305 TYPE_FIELDS list. */
20306 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20307 /* scope is either the template itself or a compatible instantiation
20308 like X<T>, so look up the name in the original template. */
20309 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20311 /* scope is a partial instantiation, so we can't do the lookup or we
20312 will lose the template arguments. */
20314 /* Enter the SCOPE so that name lookup will be resolved as if we
20315 were in the class definition. In particular, SCOPE will no
20316 longer be considered a dependent type. */
20317 pushed_scope
= push_scope (scope
);
20318 /* Look up the declaration. */
20319 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20320 tf_warning_or_error
);
20322 result
= NULL_TREE
;
20324 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20325 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20328 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20329 && TREE_CODE (decl
) == TYPE_DECL
)
20331 result
= TREE_TYPE (decl
);
20332 if (result
== error_mark_node
)
20333 result
= NULL_TREE
;
20335 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20336 && DECL_CLASS_TEMPLATE_P (decl
))
20340 /* Obtain the template and the arguments. */
20341 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20342 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20343 /* Instantiate the template. */
20344 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20345 /*entering_scope=*/0,
20346 tf_error
| tf_user
);
20347 if (result
== error_mark_node
)
20348 result
= NULL_TREE
;
20351 /* Leave the SCOPE. */
20353 pop_scope (pushed_scope
);
20355 /* If we failed to resolve it, return the original typename. */
20359 /* If lookup found a typename type, resolve that too. */
20360 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20362 /* Ill-formed programs can cause infinite recursion here, so we
20363 must catch that. */
20364 TYPENAME_IS_RESOLVING_P (type
) = 1;
20365 result
= resolve_typename_type (result
, only_current_p
);
20366 TYPENAME_IS_RESOLVING_P (type
) = 0;
20369 /* Qualify the resulting type. */
20370 quals
= cp_type_quals (type
);
20372 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20377 /* EXPR is an expression which is not type-dependent. Return a proxy
20378 for EXPR that can be used to compute the types of larger
20379 expressions containing EXPR. */
20382 build_non_dependent_expr (tree expr
)
20386 #ifdef ENABLE_CHECKING
20387 /* Try to get a constant value for all non-dependent expressions in
20388 order to expose bugs in *_dependent_expression_p and constexpr. */
20389 if (cxx_dialect
>= cxx0x
20390 && !instantiation_dependent_expression_p (expr
))
20391 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20394 /* Preserve OVERLOADs; the functions must be available to resolve
20397 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20398 inner_expr
= stmt_expr_value_expr (inner_expr
);
20399 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20400 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20401 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20402 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20403 if (is_overloaded_fn (inner_expr
)
20404 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20406 /* There is no need to return a proxy for a variable. */
20407 if (TREE_CODE (expr
) == VAR_DECL
)
20409 /* Preserve string constants; conversions from string constants to
20410 "char *" are allowed, even though normally a "const char *"
20411 cannot be used to initialize a "char *". */
20412 if (TREE_CODE (expr
) == STRING_CST
)
20414 /* Preserve arithmetic constants, as an optimization -- there is no
20415 reason to create a new node. */
20416 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20418 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20419 There is at least one place where we want to know that a
20420 particular expression is a throw-expression: when checking a ?:
20421 expression, there are special rules if the second or third
20422 argument is a throw-expression. */
20423 if (TREE_CODE (expr
) == THROW_EXPR
)
20426 /* Don't wrap an initializer list, we need to be able to look inside. */
20427 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20430 /* Don't wrap a dummy object, we need to be able to test for it. */
20431 if (is_dummy_object (expr
))
20434 if (TREE_CODE (expr
) == COND_EXPR
)
20435 return build3 (COND_EXPR
,
20437 TREE_OPERAND (expr
, 0),
20438 (TREE_OPERAND (expr
, 1)
20439 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20440 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20441 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20442 if (TREE_CODE (expr
) == COMPOUND_EXPR
20443 && !COMPOUND_EXPR_OVERLOADED (expr
))
20444 return build2 (COMPOUND_EXPR
,
20446 TREE_OPERAND (expr
, 0),
20447 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20449 /* If the type is unknown, it can't really be non-dependent */
20450 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20452 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20453 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20456 /* ARGS is a vector of expressions as arguments to a function call.
20457 Replace the arguments with equivalent non-dependent expressions.
20458 This modifies ARGS in place. */
20461 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
20466 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
20468 tree newarg
= build_non_dependent_expr (arg
);
20470 (*args
)[ix
] = newarg
;
20474 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20475 with a level one deeper than the actual template parms. */
20480 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20481 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20482 TYPE_DECL
, get_identifier ("auto"), au
);
20483 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20484 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20485 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20486 TYPE_NAME (au
), NULL_TREE
);
20487 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20488 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20489 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20494 /* Given type ARG, return std::initializer_list<ARG>. */
20499 tree std_init_list
= namespace_binding
20500 (get_identifier ("initializer_list"), std_node
);
20502 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20504 error ("deducing from brace-enclosed initializer list requires "
20505 "#include <initializer_list>");
20506 return error_mark_node
;
20508 argvec
= make_tree_vec (1);
20509 TREE_VEC_ELT (argvec
, 0) = arg
;
20510 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20511 NULL_TREE
, 0, tf_warning_or_error
);
20514 /* Replace auto in TYPE with std::initializer_list<auto>. */
20517 listify_autos (tree type
, tree auto_node
)
20519 tree init_auto
= listify (auto_node
);
20520 tree argvec
= make_tree_vec (1);
20521 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20522 if (processing_template_decl
)
20523 argvec
= add_to_template_args (current_template_args (), argvec
);
20524 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20527 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20528 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20531 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20533 tree parms
, tparms
, targs
;
20537 if (init
== error_mark_node
)
20538 return error_mark_node
;
20540 if (type_dependent_expression_p (init
))
20541 /* Defining a subset of type-dependent expressions that we can deduce
20542 from ahead of time isn't worth the trouble. */
20545 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20546 with either a new invented type template parameter U or, if the
20547 initializer is a braced-init-list (8.5.4), with
20548 std::initializer_list<U>. */
20549 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20550 type
= listify_autos (type
, auto_node
);
20552 init
= resolve_nondeduced_context (init
);
20554 parms
= build_tree_list (NULL_TREE
, type
);
20556 tparms
= make_tree_vec (1);
20557 targs
= make_tree_vec (1);
20558 TREE_VEC_ELT (tparms
, 0)
20559 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20560 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20561 DEDUCE_CALL
, LOOKUP_NORMAL
,
20562 /*explain_p=*/false);
20565 if (processing_template_decl
)
20566 /* Try again at instantiation time. */
20568 if (type
&& type
!= error_mark_node
)
20569 /* If type is error_mark_node a diagnostic must have been
20570 emitted by now. Also, having a mention to '<type error>'
20571 in the diagnostic is not really useful to the user. */
20573 if (cfun
&& auto_node
== current_function_auto_return_pattern
20574 && LAMBDA_FUNCTION_P (current_function_decl
))
20575 error ("unable to deduce lambda return type from %qE", init
);
20577 error ("unable to deduce %qT from %qE", type
, init
);
20579 return error_mark_node
;
20582 /* If the list of declarators contains more than one declarator, the type
20583 of each declared variable is determined as described above. If the
20584 type deduced for the template parameter U is not the same in each
20585 deduction, the program is ill-formed. */
20586 if (TREE_TYPE (auto_node
)
20587 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20589 if (cfun
&& auto_node
== current_function_auto_return_pattern
20590 && LAMBDA_FUNCTION_P (current_function_decl
))
20591 error ("inconsistent types %qT and %qT deduced for "
20592 "lambda return type", TREE_TYPE (auto_node
),
20593 TREE_VEC_ELT (targs
, 0));
20595 error ("inconsistent deduction for %qT: %qT and then %qT",
20596 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20597 return error_mark_node
;
20599 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20601 if (processing_template_decl
)
20602 targs
= add_to_template_args (current_template_args (), targs
);
20603 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20606 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20610 splice_late_return_type (tree type
, tree late_return_type
)
20614 if (late_return_type
== NULL_TREE
)
20616 argvec
= make_tree_vec (1);
20617 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20618 if (processing_template_parmlist
)
20619 /* For a late-specified return type in a template type-parameter, we
20620 need to add a dummy argument level for its parmlist. */
20621 argvec
= add_to_template_args
20622 (make_tree_vec (processing_template_parmlist
), argvec
);
20623 if (current_template_parms
)
20624 argvec
= add_to_template_args (current_template_args (), argvec
);
20625 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20628 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20631 is_auto (const_tree type
)
20633 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20634 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20640 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20641 appear as a type-specifier for the declaration in question, we don't
20642 have to look through the whole type. */
20645 type_uses_auto (tree type
)
20647 enum tree_code code
;
20648 if (is_auto (type
))
20651 code
= TREE_CODE (type
);
20653 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20654 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20655 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20656 return type_uses_auto (TREE_TYPE (type
));
20658 if (TYPE_PTRMEMFUNC_P (type
))
20659 return type_uses_auto (TREE_TYPE (TREE_TYPE
20660 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20665 /* For a given template T, return the vector of typedefs referenced
20666 in T for which access check is needed at T instantiation time.
20667 T is either a FUNCTION_DECL or a RECORD_TYPE.
20668 Those typedefs were added to T by the function
20669 append_type_to_template_for_access_check. */
20671 vec
<qualified_typedef_usage_t
, va_gc
> *
20672 get_types_needing_access_check (tree t
)
20675 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
20677 if (!t
|| t
== error_mark_node
)
20680 if (!(ti
= get_template_info (t
)))
20683 if (CLASS_TYPE_P (t
)
20684 || TREE_CODE (t
) == FUNCTION_DECL
)
20686 if (!TI_TEMPLATE (ti
))
20689 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20695 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20696 tied to T. That list of typedefs will be access checked at
20697 T instantiation time.
20698 T is either a FUNCTION_DECL or a RECORD_TYPE.
20699 TYPE_DECL is a TYPE_DECL node representing a typedef.
20700 SCOPE is the scope through which TYPE_DECL is accessed.
20701 LOCATION is the location of the usage point of TYPE_DECL.
20703 This function is a subroutine of
20704 append_type_to_template_for_access_check. */
20707 append_type_to_template_for_access_check_1 (tree t
,
20710 location_t location
)
20712 qualified_typedef_usage_t typedef_usage
;
20715 if (!t
|| t
== error_mark_node
)
20718 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20719 || CLASS_TYPE_P (t
))
20721 && TREE_CODE (type_decl
) == TYPE_DECL
20724 if (!(ti
= get_template_info (t
)))
20727 gcc_assert (TI_TEMPLATE (ti
));
20729 typedef_usage
.typedef_decl
= type_decl
;
20730 typedef_usage
.context
= scope
;
20731 typedef_usage
.locus
= location
;
20733 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
20736 /* Append TYPE_DECL to the template TEMPL.
20737 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20738 At TEMPL instanciation time, TYPE_DECL will be checked to see
20739 if it can be accessed through SCOPE.
20740 LOCATION is the location of the usage point of TYPE_DECL.
20742 e.g. consider the following code snippet:
20749 template<class U> struct S
20751 C::myint mi; // <-- usage point of the typedef C::myint
20756 At S<char> instantiation time, we need to check the access of C::myint
20757 In other words, we need to check the access of the myint typedef through
20758 the C scope. For that purpose, this function will add the myint typedef
20759 and the scope C through which its being accessed to a list of typedefs
20760 tied to the template S. That list will be walked at template instantiation
20761 time and access check performed on each typedefs it contains.
20762 Note that this particular code snippet should yield an error because
20763 myint is private to C. */
20766 append_type_to_template_for_access_check (tree templ
,
20769 location_t location
)
20771 qualified_typedef_usage_t
*iter
;
20774 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20776 /* Make sure we don't append the type to the template twice. */
20777 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
20778 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20781 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20785 /* Set up the hash tables for template instantiations. */
20788 init_template_processing (void)
20790 decl_specializations
= htab_create_ggc (37,
20791 hash_specialization
,
20792 eq_specializations
,
20794 type_specializations
= htab_create_ggc (37,
20795 hash_specialization
,
20796 eq_specializations
,
20800 /* Print stats about the template hash tables for -fstats. */
20803 print_template_statistics (void)
20805 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20806 "%f collisions\n", (long) htab_size (decl_specializations
),
20807 (long) htab_elements (decl_specializations
),
20808 htab_collisions (decl_specializations
));
20809 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20810 "%f collisions\n", (long) htab_size (type_specializations
),
20811 (long) htab_elements (type_specializations
),
20812 htab_collisions (type_specializations
));
20815 #include "gt-cp-pt.h"