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 return error_mark_node
;
4788 if (current
== decl
)
4790 else if (current
== NULL_TREE
)
4791 /* Can happen in erroneous input. */
4794 current
= (TYPE_P (current
)
4795 ? TYPE_CONTEXT (current
)
4796 : DECL_CONTEXT (current
));
4799 /* Check that the parms are used in the appropriate qualifying scopes
4800 in the declarator. */
4801 if (!comp_template_args
4803 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4806 template arguments to %qD do not match original template %qD",
4807 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4808 if (!uses_template_parms (TI_ARGS (tinfo
)))
4809 inform (input_location
, "use template<> for an explicit specialization");
4810 /* Avoid crash in import_export_decl. */
4811 DECL_INTERFACE_KNOWN (decl
) = 1;
4812 return error_mark_node
;
4816 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4817 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4819 /* Push template declarations for global functions and types. Note
4820 that we do not try to push a global template friend declared in a
4821 template class; such a thing may well depend on the template
4822 parameters of the class. */
4823 if (new_template_p
&& !ctx
4824 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4826 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4827 if (tmpl
== error_mark_node
)
4828 return error_mark_node
;
4830 /* Hide template friend classes that haven't been declared yet. */
4831 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4833 DECL_ANTICIPATED (tmpl
) = 1;
4834 DECL_FRIEND_P (tmpl
) = 1;
4840 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4843 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4844 if (DECL_CONV_FN_P (tmpl
))
4846 int depth
= TMPL_PARMS_DEPTH (parms
);
4848 /* It is a conversion operator. See if the type converted to
4849 depends on innermost template operands. */
4851 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4853 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4859 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4861 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4862 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4863 DECL_CONTEXT (parm
) = tmpl
;
4867 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4868 back to its most general template. If TMPL is a specialization,
4869 ARGS may only have the innermost set of arguments. Add the missing
4870 argument levels if necessary. */
4871 if (DECL_TEMPLATE_INFO (tmpl
))
4872 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4874 info
= build_template_info (tmpl
, args
);
4876 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4877 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4880 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4881 retrofit_lang_decl (decl
);
4882 if (DECL_LANG_SPECIFIC (decl
))
4883 DECL_TEMPLATE_INFO (decl
) = info
;
4886 return DECL_TEMPLATE_RESULT (tmpl
);
4890 push_template_decl (tree decl
)
4892 return push_template_decl_real (decl
, false);
4895 /* FN is an inheriting constructor that inherits from the constructor
4896 template INHERITED; turn FN into a constructor template with a matching
4900 add_inherited_template_parms (tree fn
, tree inherited
)
4903 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4904 inner_parms
= copy_node (inner_parms
);
4906 = tree_cons (size_int (processing_template_decl
+ 1),
4907 inner_parms
, current_template_parms
);
4908 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4909 tree args
= template_parms_to_args (parms
);
4910 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4911 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4912 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4913 DECL_ARTIFICIAL (tmpl
) = true;
4914 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4918 /* Called when a class template TYPE is redeclared with the indicated
4919 template PARMS, e.g.:
4921 template <class T> struct S;
4922 template <class T> struct S {}; */
4925 redeclare_class_template (tree type
, tree parms
)
4931 if (!TYPE_TEMPLATE_INFO (type
))
4933 error ("%qT is not a template type", type
);
4937 tmpl
= TYPE_TI_TEMPLATE (type
);
4938 if (!PRIMARY_TEMPLATE_P (tmpl
))
4939 /* The type is nested in some template class. Nothing to worry
4940 about here; there are no new template parameters for the nested
4946 error ("template specifiers not specified in declaration of %qD",
4951 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4952 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4954 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4956 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4957 "redeclared with %d template parameter",
4958 "redeclared with %d template parameters",
4959 TREE_VEC_LENGTH (parms
));
4960 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4961 "previous declaration %q+D used %d template parameter",
4962 "previous declaration %q+D used %d template parameters",
4963 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4967 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4974 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4975 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4978 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4979 if (tmpl_parm
== error_mark_node
)
4982 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4983 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4984 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4986 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4988 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4989 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4990 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4991 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4992 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4993 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4994 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4995 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4996 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
4998 error ("template parameter %q+#D", tmpl_parm
);
4999 error ("redeclared here as %q#D", parm
);
5003 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5005 /* We have in [temp.param]:
5007 A template-parameter may not be given default arguments
5008 by two different declarations in the same scope. */
5009 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5010 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5011 "original definition appeared here");
5015 if (parm_default
!= NULL_TREE
)
5016 /* Update the previous template parameters (which are the ones
5017 that will really count) with the new default value. */
5018 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5019 else if (tmpl_default
!= NULL_TREE
)
5020 /* Update the new parameters, too; they'll be used as the
5021 parameters for any members. */
5022 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5028 /* Simplify EXPR if it is a non-dependent expression. Returns the
5029 (possibly simplified) expression. */
5032 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5034 if (expr
== NULL_TREE
)
5037 /* If we're in a template, but EXPR isn't value dependent, simplify
5038 it. We're supposed to treat:
5040 template <typename T> void f(T[1 + 1]);
5041 template <typename T> void f(T[2]);
5043 as two declarations of the same function, for example. */
5044 if (processing_template_decl
5045 && !type_dependent_expression_p (expr
)
5046 && potential_constant_expression (expr
)
5047 && !value_dependent_expression_p (expr
))
5049 HOST_WIDE_INT saved_processing_template_decl
;
5051 saved_processing_template_decl
= processing_template_decl
;
5052 processing_template_decl
= 0;
5053 expr
= tsubst_copy_and_build (expr
,
5056 /*in_decl=*/NULL_TREE
,
5057 /*function_p=*/false,
5058 /*integral_constant_expression_p=*/true);
5059 processing_template_decl
= saved_processing_template_decl
;
5065 fold_non_dependent_expr (tree expr
)
5067 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5070 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5071 template declaration, or a TYPE_DECL for an alias declaration. */
5074 alias_type_or_template_p (tree t
)
5078 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5081 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5082 || DECL_ALIAS_TEMPLATE_P (t
));
5085 /* Return TRUE iff is a specialization of an alias template. */
5088 alias_template_specialization_p (const_tree t
)
5094 && TYPE_TEMPLATE_INFO (t
)
5095 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5096 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5099 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5100 must be a function or a pointer-to-function type, as specified
5101 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5102 and check that the resulting function has external linkage. */
5105 convert_nontype_argument_function (tree type
, tree expr
)
5109 linkage_kind linkage
;
5111 fn
= instantiate_type (type
, fns
, tf_none
);
5112 if (fn
== error_mark_node
)
5113 return error_mark_node
;
5116 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5117 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5118 if (BASELINK_P (fn_no_ptr
))
5119 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5121 /* [temp.arg.nontype]/1
5123 A template-argument for a non-type, non-template template-parameter
5126 -- the address of an object or function with external [C++11: or
5127 internal] linkage. */
5129 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5131 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5132 if (TREE_CODE (type
) == POINTER_TYPE
)
5133 error ("it must be the address of a function with external linkage");
5135 error ("it must be the name of a function with external linkage");
5139 linkage
= decl_linkage (fn_no_ptr
);
5140 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5142 if (cxx_dialect
>= cxx0x
)
5143 error ("%qE is not a valid template argument for type %qT "
5144 "because %qD has no linkage",
5145 expr
, type
, fn_no_ptr
);
5147 error ("%qE is not a valid template argument for type %qT "
5148 "because %qD does not have external linkage",
5149 expr
, type
, fn_no_ptr
);
5156 /* Subroutine of convert_nontype_argument.
5157 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5158 Emit an error otherwise. */
5161 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5162 tsubst_flags_t complain
)
5165 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5167 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5169 if (complain
& tf_error
)
5171 error ("%qE is not a valid template argument for type %qT",
5173 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5178 /* Returns TRUE iff the address of OP is value-dependent.
5180 14.6.2.4 [temp.dep.temp]:
5181 A non-integral non-type template-argument is dependent if its type is
5182 dependent or it has either of the following forms
5185 and contains a nested-name-specifier which specifies a class-name that
5186 names a dependent type.
5188 We generalize this to just say that the address of a member of a
5189 dependent class is value-dependent; the above doesn't cover the
5190 address of a static data member named with an unqualified-id. */
5193 has_value_dependent_address (tree op
)
5195 /* We could use get_inner_reference here, but there's no need;
5196 this is only relevant for template non-type arguments, which
5197 can only be expressed as &id-expression. */
5200 tree ctx
= CP_DECL_CONTEXT (op
);
5201 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5208 /* The next set of functions are used for providing helpful explanatory
5209 diagnostics for failed overload resolution. Their messages should be
5210 indented by two spaces for consistency with the messages in
5214 unify_success (bool /*explain_p*/)
5220 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5223 inform (input_location
,
5224 " couldn't deduce template parameter %qD", parm
);
5229 unify_invalid (bool /*explain_p*/)
5235 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5238 inform (input_location
,
5239 " types %qT and %qT have incompatible cv-qualifiers",
5245 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5248 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5253 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5256 inform (input_location
,
5257 " template parameter %qD is not a parameter pack, but "
5264 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5267 inform (input_location
,
5268 " template argument %qE does not match "
5269 "pointer-to-member constant %qE",
5275 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5278 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5283 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5286 inform (input_location
,
5287 " inconsistent parameter pack deduction with %qT and %qT",
5293 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5298 inform (input_location
,
5299 " deduced conflicting types for parameter %qT (%qT and %qT)",
5300 parm
, first
, second
);
5302 inform (input_location
,
5303 " deduced conflicting values for non-type parameter "
5304 "%qE (%qE and %qE)", parm
, first
, second
);
5310 unify_vla_arg (bool explain_p
, tree arg
)
5313 inform (input_location
,
5314 " variable-sized array type %qT is not "
5315 "a valid template argument",
5321 unify_method_type_error (bool explain_p
, tree arg
)
5324 inform (input_location
,
5325 " member function type %qT is not a valid template argument",
5331 unify_arity (bool explain_p
, int have
, int wanted
)
5334 inform_n (input_location
, wanted
,
5335 " candidate expects %d argument, %d provided",
5336 " candidate expects %d arguments, %d provided",
5342 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5344 return unify_arity (explain_p
, have
, wanted
);
5348 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5350 return unify_arity (explain_p
, have
, wanted
);
5354 unify_arg_conversion (bool explain_p
, tree to_type
,
5355 tree from_type
, tree arg
)
5358 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5359 arg
, from_type
, to_type
);
5364 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5365 tree parm
, tree arg
)
5370 case tbr_ambiguous_baseclass
:
5371 inform (input_location
, " %qT is an ambiguous base class of %qT",
5375 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5382 unify_inconsistent_template_template_parameters (bool explain_p
)
5385 inform (input_location
,
5386 " template parameters of a template template argument are "
5387 "inconsistent with other deduced template arguments");
5392 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5395 inform (input_location
,
5396 " can't deduce a template for %qT from non-template type %qT",
5402 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5405 inform (input_location
,
5406 " template argument %qE does not match %qD", arg
, parm
);
5411 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5414 inform (input_location
,
5415 " could not resolve address from overloaded function %qE",
5420 /* Attempt to convert the non-type template parameter EXPR to the
5421 indicated TYPE. If the conversion is successful, return the
5422 converted value. If the conversion is unsuccessful, return
5423 NULL_TREE if we issued an error message, or error_mark_node if we
5424 did not. We issue error messages for out-and-out bad template
5425 parameters, but not simply because the conversion failed, since we
5426 might be just trying to do argument deduction. Both TYPE and EXPR
5427 must be non-dependent.
5429 The conversion follows the special rules described in
5430 [temp.arg.nontype], and it is much more strict than an implicit
5433 This function is called twice for each template argument (see
5434 lookup_template_class for a more accurate description of this
5435 problem). This means that we need to handle expressions which
5436 are not valid in a C++ source, but can be created from the
5437 first call (for instance, casts to perform conversions). These
5438 hacks can go away after we fix the double coercion problem. */
5441 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5445 /* Detect immediately string literals as invalid non-type argument.
5446 This special-case is not needed for correctness (we would easily
5447 catch this later), but only to provide better diagnostic for this
5448 common user mistake. As suggested by DR 100, we do not mention
5449 linkage issues in the diagnostic as this is not the point. */
5450 /* FIXME we're making this OK. */
5451 if (TREE_CODE (expr
) == STRING_CST
)
5453 if (complain
& tf_error
)
5454 error ("%qE is not a valid template argument for type %qT "
5455 "because string literals can never be used in this context",
5460 /* Add the ADDR_EXPR now for the benefit of
5461 value_dependent_expression_p. */
5462 if (TYPE_PTROBV_P (type
)
5463 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5465 expr
= decay_conversion (expr
, complain
);
5466 if (expr
== error_mark_node
)
5467 return error_mark_node
;
5470 /* If we are in a template, EXPR may be non-dependent, but still
5471 have a syntactic, rather than semantic, form. For example, EXPR
5472 might be a SCOPE_REF, rather than the VAR_DECL to which the
5473 SCOPE_REF refers. Preserving the qualifying scope is necessary
5474 so that access checking can be performed when the template is
5475 instantiated -- but here we need the resolved form so that we can
5476 convert the argument. */
5477 if (TYPE_REF_OBJ_P (type
)
5478 && has_value_dependent_address (expr
))
5479 /* If we want the address and it's value-dependent, don't fold. */;
5480 else if (!type_unknown_p (expr
))
5481 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5482 if (error_operand_p (expr
))
5483 return error_mark_node
;
5484 expr_type
= TREE_TYPE (expr
);
5485 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5486 expr
= mark_lvalue_use (expr
);
5488 expr
= mark_rvalue_use (expr
);
5490 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5491 to a non-type argument of "nullptr". */
5492 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5493 expr
= convert (type
, expr
);
5495 /* In C++11, integral or enumeration non-type template arguments can be
5496 arbitrary constant expressions. Pointer and pointer to
5497 member arguments can be general constant expressions that evaluate
5498 to a null value, but otherwise still need to be of a specific form. */
5499 if (cxx_dialect
>= cxx0x
)
5501 if (TREE_CODE (expr
) == PTRMEM_CST
)
5502 /* A PTRMEM_CST is already constant, and a valid template
5503 argument for a parameter of pointer to member type, we just want
5504 to leave it in that form rather than lower it to a
5506 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5507 expr
= maybe_constant_value (expr
);
5508 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5510 tree folded
= maybe_constant_value (expr
);
5511 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5512 : null_member_pointer_value_p (folded
))
5517 /* HACK: Due to double coercion, we can get a
5518 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5519 which is the tree that we built on the first call (see
5520 below when coercing to reference to object or to reference to
5521 function). We just strip everything and get to the arg.
5522 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5524 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5526 tree probe_type
, probe
= expr
;
5527 if (REFERENCE_REF_P (probe
))
5528 probe
= TREE_OPERAND (probe
, 0);
5529 probe_type
= TREE_TYPE (probe
);
5530 if (TREE_CODE (probe
) == NOP_EXPR
)
5532 /* ??? Maybe we could use convert_from_reference here, but we
5533 would need to relax its constraints because the NOP_EXPR
5534 could actually change the type to something more cv-qualified,
5535 and this is not folded by convert_from_reference. */
5536 tree addr
= TREE_OPERAND (probe
, 0);
5537 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5538 && TREE_CODE (addr
) == ADDR_EXPR
5539 && TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
5540 && (same_type_ignoring_top_level_qualifiers_p
5541 (TREE_TYPE (probe_type
),
5542 TREE_TYPE (TREE_TYPE (addr
)))))
5544 expr
= TREE_OPERAND (addr
, 0);
5545 expr_type
= TREE_TYPE (expr
);
5550 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5551 parameter is a pointer to object, through decay and
5552 qualification conversion. Let's strip everything. */
5553 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5556 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5557 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5558 /* Skip the ADDR_EXPR only if it is part of the decay for
5559 an array. Otherwise, it is part of the original argument
5560 in the source code. */
5561 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5562 expr
= TREE_OPERAND (expr
, 0);
5563 expr_type
= TREE_TYPE (expr
);
5566 /* [temp.arg.nontype]/5, bullet 1
5568 For a non-type template-parameter of integral or enumeration type,
5569 integral promotions (_conv.prom_) and integral conversions
5570 (_conv.integral_) are applied. */
5571 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5573 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5574 t
= maybe_constant_value (t
);
5575 if (t
!= error_mark_node
)
5578 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5579 return error_mark_node
;
5581 /* Notice that there are constant expressions like '4 % 0' which
5582 do not fold into integer constants. */
5583 if (TREE_CODE (expr
) != INTEGER_CST
)
5585 if (complain
& tf_error
)
5587 int errs
= errorcount
, warns
= warningcount
;
5588 if (processing_template_decl
5589 && !require_potential_constant_expression (expr
))
5591 expr
= cxx_constant_value (expr
);
5592 if (errorcount
> errs
|| warningcount
> warns
)
5593 inform (EXPR_LOC_OR_HERE (expr
),
5594 "in template argument for type %qT ", type
);
5595 if (expr
== error_mark_node
)
5597 /* else cxx_constant_value complained but gave us
5598 a real constant, so go ahead. */
5599 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5605 /* [temp.arg.nontype]/5, bullet 2
5607 For a non-type template-parameter of type pointer to object,
5608 qualification conversions (_conv.qual_) and the array-to-pointer
5609 conversion (_conv.array_) are applied. */
5610 else if (TYPE_PTROBV_P (type
))
5612 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5614 A template-argument for a non-type, non-template template-parameter
5615 shall be one of: [...]
5617 -- the name of a non-type template-parameter;
5618 -- the address of an object or function with external linkage, [...]
5619 expressed as "& id-expression" where the & is optional if the name
5620 refers to a function or array, or if the corresponding
5621 template-parameter is a reference.
5623 Here, we do not care about functions, as they are invalid anyway
5624 for a parameter of type pointer-to-object. */
5626 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5627 /* Non-type template parameters are OK. */
5629 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5630 /* Null pointer values are OK in C++11. */;
5631 else if (TREE_CODE (expr
) != ADDR_EXPR
5632 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5634 if (TREE_CODE (expr
) == VAR_DECL
)
5636 error ("%qD is not a valid template argument "
5637 "because %qD is a variable, not the address of "
5642 /* Other values, like integer constants, might be valid
5643 non-type arguments of some other type. */
5644 return error_mark_node
;
5650 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5651 ? TREE_OPERAND (expr
, 0) : expr
);
5652 if (TREE_CODE (decl
) != VAR_DECL
)
5654 error ("%qE is not a valid template argument of type %qT "
5655 "because %qE is not a variable",
5659 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5661 error ("%qE is not a valid template argument of type %qT "
5662 "because %qD does not have external linkage",
5666 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5668 error ("%qE is not a valid template argument of type %qT "
5669 "because %qD has no linkage",
5675 expr
= decay_conversion (expr
, complain
);
5676 if (expr
== error_mark_node
)
5677 return error_mark_node
;
5679 expr
= perform_qualification_conversions (type
, expr
);
5680 if (expr
== error_mark_node
)
5681 return error_mark_node
;
5683 /* [temp.arg.nontype]/5, bullet 3
5685 For a non-type template-parameter of type reference to object, no
5686 conversions apply. The type referred to by the reference may be more
5687 cv-qualified than the (otherwise identical) type of the
5688 template-argument. The template-parameter is bound directly to the
5689 template-argument, which must be an lvalue. */
5690 else if (TYPE_REF_OBJ_P (type
))
5692 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5694 return error_mark_node
;
5696 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5698 error ("%qE is not a valid template argument for type %qT "
5699 "because of conflicts in cv-qualification", expr
, type
);
5703 if (!real_lvalue_p (expr
))
5705 error ("%qE is not a valid template argument for type %qT "
5706 "because it is not an lvalue", expr
, type
);
5710 /* [temp.arg.nontype]/1
5712 A template-argument for a non-type, non-template template-parameter
5713 shall be one of: [...]
5715 -- the address of an object or function with external linkage. */
5716 if (TREE_CODE (expr
) == INDIRECT_REF
5717 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5719 expr
= TREE_OPERAND (expr
, 0);
5722 error ("%q#D is not a valid template argument for type %qT "
5723 "because a reference variable does not have a constant "
5724 "address", expr
, type
);
5731 error ("%qE is not a valid template argument for type %qT "
5732 "because it is not an object with external linkage",
5737 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5739 error ("%qE is not a valid template argument for type %qT "
5740 "because object %qD has not external linkage",
5745 expr
= build_nop (type
, build_address (expr
));
5747 /* [temp.arg.nontype]/5, bullet 4
5749 For a non-type template-parameter of type pointer to function, only
5750 the function-to-pointer conversion (_conv.func_) is applied. If the
5751 template-argument represents a set of overloaded functions (or a
5752 pointer to such), the matching function is selected from the set
5754 else if (TYPE_PTRFN_P (type
))
5756 /* If the argument is a template-id, we might not have enough
5757 context information to decay the pointer. */
5758 if (!type_unknown_p (expr_type
))
5760 expr
= decay_conversion (expr
, complain
);
5761 if (expr
== error_mark_node
)
5762 return error_mark_node
;
5765 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5766 /* Null pointer values are OK in C++11. */
5767 return perform_qualification_conversions (type
, expr
);
5769 expr
= convert_nontype_argument_function (type
, expr
);
5770 if (!expr
|| expr
== error_mark_node
)
5773 /* [temp.arg.nontype]/5, bullet 5
5775 For a non-type template-parameter of type reference to function, no
5776 conversions apply. If the template-argument represents a set of
5777 overloaded functions, the matching function is selected from the set
5779 else if (TYPE_REFFN_P (type
))
5781 if (TREE_CODE (expr
) == ADDR_EXPR
)
5783 error ("%qE is not a valid template argument for type %qT "
5784 "because it is a pointer", expr
, type
);
5785 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5789 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5790 if (!expr
|| expr
== error_mark_node
)
5793 expr
= build_nop (type
, build_address (expr
));
5795 /* [temp.arg.nontype]/5, bullet 6
5797 For a non-type template-parameter of type pointer to member function,
5798 no conversions apply. If the template-argument represents a set of
5799 overloaded member functions, the matching member function is selected
5800 from the set (_over.over_). */
5801 else if (TYPE_PTRMEMFUNC_P (type
))
5803 expr
= instantiate_type (type
, expr
, tf_none
);
5804 if (expr
== error_mark_node
)
5805 return error_mark_node
;
5807 /* [temp.arg.nontype] bullet 1 says the pointer to member
5808 expression must be a pointer-to-member constant. */
5809 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5810 return error_mark_node
;
5812 /* There is no way to disable standard conversions in
5813 resolve_address_of_overloaded_function (called by
5814 instantiate_type). It is possible that the call succeeded by
5815 converting &B::I to &D::I (where B is a base of D), so we need
5816 to reject this conversion here.
5818 Actually, even if there was a way to disable standard conversions,
5819 it would still be better to reject them here so that we can
5820 provide a superior diagnostic. */
5821 if (!same_type_p (TREE_TYPE (expr
), type
))
5823 error ("%qE is not a valid template argument for type %qT "
5824 "because it is of type %qT", expr
, type
,
5826 /* If we are just one standard conversion off, explain. */
5827 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5828 inform (input_location
,
5829 "standard conversions are not allowed in this context");
5833 /* [temp.arg.nontype]/5, bullet 7
5835 For a non-type template-parameter of type pointer to data member,
5836 qualification conversions (_conv.qual_) are applied. */
5837 else if (TYPE_PTRDATAMEM_P (type
))
5839 /* [temp.arg.nontype] bullet 1 says the pointer to member
5840 expression must be a pointer-to-member constant. */
5841 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5842 return error_mark_node
;
5844 expr
= perform_qualification_conversions (type
, expr
);
5845 if (expr
== error_mark_node
)
5848 else if (NULLPTR_TYPE_P (type
))
5850 if (expr
!= nullptr_node
)
5852 error ("%qE is not a valid template argument for type %qT "
5853 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5858 /* A template non-type parameter must be one of the above. */
5862 /* Sanity check: did we actually convert the argument to the
5864 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5865 (type
, TREE_TYPE (expr
)));
5869 /* Subroutine of coerce_template_template_parms, which returns 1 if
5870 PARM_PARM and ARG_PARM match using the rule for the template
5871 parameters of template template parameters. Both PARM and ARG are
5872 template parameters; the rest of the arguments are the same as for
5873 coerce_template_template_parms.
5876 coerce_template_template_parm (tree parm
,
5878 tsubst_flags_t complain
,
5882 if (arg
== NULL_TREE
|| arg
== error_mark_node
5883 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5886 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5889 switch (TREE_CODE (parm
))
5892 /* We encounter instantiations of templates like
5893 template <template <template <class> class> class TT>
5896 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5897 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5899 if (!coerce_template_template_parms
5900 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5906 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5907 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5908 /* Argument is a parameter pack but parameter is not. */
5913 /* The tsubst call is used to handle cases such as
5915 template <int> class C {};
5916 template <class T, template <T> class TT> class D {};
5919 i.e. the parameter list of TT depends on earlier parameters. */
5920 if (!uses_template_parms (TREE_TYPE (arg
))
5922 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5926 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5927 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5928 /* Argument is a parameter pack but parameter is not. */
5941 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5942 template template parameters. Both PARM_PARMS and ARG_PARMS are
5943 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5946 Consider the example:
5947 template <class T> class A;
5948 template<template <class U> class TT> class B;
5950 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5951 the parameters to A, and OUTER_ARGS contains A. */
5954 coerce_template_template_parms (tree parm_parms
,
5956 tsubst_flags_t complain
,
5960 int nparms
, nargs
, i
;
5964 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5965 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5967 nparms
= TREE_VEC_LENGTH (parm_parms
);
5968 nargs
= TREE_VEC_LENGTH (arg_parms
);
5970 /* Determine whether we have a parameter pack at the end of the
5971 template template parameter's template parameter list. */
5972 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5974 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5976 if (parm
== error_mark_node
)
5979 switch (TREE_CODE (parm
))
5983 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5988 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5998 && !(variadic_p
&& nargs
>= nparms
- 1))
6001 /* Check all of the template parameters except the parameter pack at
6002 the end (if any). */
6003 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6005 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6006 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6009 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6010 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6012 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6020 /* Check each of the template parameters in the template
6021 argument against the template parameter pack at the end of
6022 the template template parameter. */
6023 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6026 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6028 for (; i
< nargs
; ++i
)
6030 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6033 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6035 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6044 /* Verifies that the deduced template arguments (in TARGS) for the
6045 template template parameters (in TPARMS) represent valid bindings,
6046 by comparing the template parameter list of each template argument
6047 to the template parameter list of its corresponding template
6048 template parameter, in accordance with DR150. This
6049 routine can only be called after all template arguments have been
6050 deduced. It will return TRUE if all of the template template
6051 parameter bindings are okay, FALSE otherwise. */
6053 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6055 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6058 /* We're dealing with template parms in this process. */
6059 ++processing_template_decl
;
6061 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6063 for (i
= 0; i
< ntparms
; ++i
)
6065 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6066 tree targ
= TREE_VEC_ELT (targs
, i
);
6068 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6070 tree packed_args
= NULL_TREE
;
6073 if (ARGUMENT_PACK_P (targ
))
6075 /* Look inside the argument pack. */
6076 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6077 len
= TREE_VEC_LENGTH (packed_args
);
6080 for (idx
= 0; idx
< len
; ++idx
)
6082 tree targ_parms
= NULL_TREE
;
6085 /* Extract the next argument from the argument
6087 targ
= TREE_VEC_ELT (packed_args
, idx
);
6089 if (PACK_EXPANSION_P (targ
))
6090 /* Look at the pattern of the pack expansion. */
6091 targ
= PACK_EXPANSION_PATTERN (targ
);
6093 /* Extract the template parameters from the template
6095 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6096 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6097 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6098 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6100 /* Verify that we can coerce the template template
6101 parameters from the template argument to the template
6102 parameter. This requires an exact match. */
6104 && !coerce_template_template_parms
6105 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6120 --processing_template_decl
;
6124 /* Since type attributes aren't mangled, we need to strip them from
6125 template type arguments. */
6128 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6131 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6133 mv
= TYPE_MAIN_VARIANT (arg
);
6134 arg
= strip_typedefs (arg
);
6135 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6136 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6138 if (complain
& tf_warning
)
6139 warning (0, "ignoring attributes on template argument %qT", arg
);
6140 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6141 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6146 /* Convert the indicated template ARG as necessary to match the
6147 indicated template PARM. Returns the converted ARG, or
6148 error_mark_node if the conversion was unsuccessful. Error and
6149 warning messages are issued under control of COMPLAIN. This
6150 conversion is for the Ith parameter in the parameter list. ARGS is
6151 the full set of template arguments deduced so far. */
6154 convert_template_argument (tree parm
,
6157 tsubst_flags_t complain
,
6163 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6165 if (TREE_CODE (arg
) == TREE_LIST
6166 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6168 /* The template argument was the name of some
6169 member function. That's usually
6170 invalid, but static members are OK. In any
6171 case, grab the underlying fields/functions
6172 and issue an error later if required. */
6173 orig_arg
= TREE_VALUE (arg
);
6174 TREE_TYPE (arg
) = unknown_type_node
;
6179 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6180 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6181 || requires_tmpl_type
);
6183 /* When determining whether an argument pack expansion is a template,
6184 look at the pattern. */
6185 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6186 arg
= PACK_EXPANSION_PATTERN (arg
);
6188 /* Deal with an injected-class-name used as a template template arg. */
6189 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6191 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6192 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6194 if (cxx_dialect
>= cxx0x
)
6195 /* OK under DR 1004. */;
6196 else if (complain
& tf_warning_or_error
)
6197 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6198 " used as template template argument", TYPE_NAME (arg
));
6199 else if (flag_pedantic_errors
)
6207 ((TREE_CODE (arg
) == TEMPLATE_DECL
6208 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6209 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6210 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6211 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6214 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6215 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6216 arg
= TYPE_STUB_DECL (arg
);
6218 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6220 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6221 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6223 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6225 if (complain
& tf_error
)
6226 error ("invalid use of destructor %qE as a type", orig_arg
);
6227 return error_mark_node
;
6230 permerror (input_location
,
6231 "to refer to a type member of a template parameter, "
6232 "use %<typename %E%>", orig_arg
);
6234 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6235 TREE_OPERAND (arg
, 1),
6241 if (is_type
!= requires_type
)
6245 if (complain
& tf_error
)
6247 error ("type/value mismatch at argument %d in template "
6248 "parameter list for %qD",
6251 error (" expected a constant of type %qT, got %qT",
6253 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6254 else if (requires_tmpl_type
)
6255 error (" expected a class template, got %qE", orig_arg
);
6257 error (" expected a type, got %qE", orig_arg
);
6260 return error_mark_node
;
6262 if (is_tmpl_type
^ requires_tmpl_type
)
6264 if (in_decl
&& (complain
& tf_error
))
6266 error ("type/value mismatch at argument %d in template "
6267 "parameter list for %qD",
6270 error (" expected a type, got %qT", DECL_NAME (arg
));
6272 error (" expected a class template, got %qT", orig_arg
);
6274 return error_mark_node
;
6279 if (requires_tmpl_type
)
6281 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6283 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6284 /* The number of argument required is not known yet.
6285 Just accept it for now. */
6286 val
= TREE_TYPE (arg
);
6289 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6292 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6294 if (coerce_template_template_parms (parmparm
, argparm
,
6300 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6302 if (val
!= error_mark_node
)
6304 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6305 val
= TREE_TYPE (val
);
6306 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6307 val
= make_pack_expansion (val
);
6312 if (in_decl
&& (complain
& tf_error
))
6314 error ("type/value mismatch at argument %d in "
6315 "template parameter list for %qD",
6317 error (" expected a template of type %qD, got %qT",
6321 val
= error_mark_node
;
6327 /* We only form one instance of each template specialization.
6328 Therefore, if we use a non-canonical variant (i.e., a
6329 typedef), any future messages referring to the type will use
6330 the typedef, which is confusing if those future uses do not
6331 themselves also use the typedef. */
6333 val
= canonicalize_type_argument (val
, complain
);
6337 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6339 if (invalid_nontype_parm_type_p (t
, complain
))
6340 return error_mark_node
;
6342 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6344 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6348 /* Not sure if this is reachable, but it doesn't hurt
6350 error ("type mismatch in nontype parameter pack");
6351 val
= error_mark_node
;
6354 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6355 /* We used to call digest_init here. However, digest_init
6356 will report errors, which we don't want when complain
6357 is zero. More importantly, digest_init will try too
6358 hard to convert things: for example, `0' should not be
6359 converted to pointer type at this point according to
6360 the standard. Accepting this is not merely an
6361 extension, since deciding whether or not these
6362 conversions can occur is part of determining which
6363 function template to call, or whether a given explicit
6364 argument specification is valid. */
6365 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6367 val
= strip_typedefs_expr (orig_arg
);
6369 if (val
== NULL_TREE
)
6370 val
= error_mark_node
;
6371 else if (val
== error_mark_node
&& (complain
& tf_error
))
6372 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6374 if (TREE_CODE (val
) == SCOPE_REF
)
6376 /* Strip typedefs from the SCOPE_REF. */
6377 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6378 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6380 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6381 QUALIFIED_NAME_IS_TEMPLATE (val
));
6388 /* Coerces the remaining template arguments in INNER_ARGS (from
6389 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6390 Returns the coerced argument pack. PARM_IDX is the position of this
6391 parameter in the template parameter list. ARGS is the original
6392 template argument list. */
6394 coerce_template_parameter_pack (tree parms
,
6402 tsubst_flags_t complain
)
6404 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6405 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6408 tree packed_types
= NULL_TREE
;
6410 if (arg_idx
> nargs
)
6413 packed_args
= make_tree_vec (nargs
- arg_idx
);
6415 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6416 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6418 /* When the template parameter is a non-type template
6419 parameter pack whose type uses parameter packs, we need
6420 to look at each of the template arguments
6421 separately. Build a vector of the types for these
6422 non-type template parameters in PACKED_TYPES. */
6424 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6425 packed_types
= tsubst_pack_expansion (expansion
, args
,
6428 if (packed_types
== error_mark_node
)
6429 return error_mark_node
;
6431 /* Check that we have the right number of arguments. */
6433 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6434 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6437 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6438 error ("wrong number of template arguments (%d, should be %d)",
6439 nargs
, needed_parms
);
6440 return error_mark_node
;
6443 /* If we aren't able to check the actual arguments now
6444 (because they haven't been expanded yet), we can at least
6445 verify that all of the types used for the non-type
6446 template parameter pack are, in fact, valid for non-type
6447 template parameters. */
6449 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6451 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6452 for (j
= 0; j
< len
; ++j
)
6454 tree t
= TREE_VEC_ELT (packed_types
, j
);
6455 if (invalid_nontype_parm_type_p (t
, complain
))
6456 return error_mark_node
;
6461 /* Convert the remaining arguments, which will be a part of the
6462 parameter pack "parm". */
6463 for (; arg_idx
< nargs
; ++arg_idx
)
6465 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6466 tree actual_parm
= TREE_VALUE (parm
);
6468 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6470 /* When we have a vector of types (corresponding to the
6471 non-type template parameter pack that uses parameter
6472 packs in its type, as mention above), and the
6473 argument is not an expansion (which expands to a
6474 currently unknown number of arguments), clone the
6475 parm and give it the next type in PACKED_TYPES. */
6476 actual_parm
= copy_node (actual_parm
);
6477 TREE_TYPE (actual_parm
) =
6478 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6481 if (arg
== error_mark_node
)
6483 if (complain
& tf_error
)
6484 error ("template argument %d is invalid", arg_idx
+ 1);
6487 arg
= convert_template_argument (actual_parm
,
6488 arg
, new_args
, complain
, parm_idx
,
6490 if (arg
== error_mark_node
)
6492 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6495 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6496 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6497 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6500 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6501 TREE_TYPE (argument_pack
)
6502 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6503 TREE_CONSTANT (argument_pack
) = 1;
6506 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6507 #ifdef ENABLE_CHECKING
6508 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6509 TREE_VEC_LENGTH (packed_args
));
6511 return argument_pack
;
6514 /* Returns true if the template argument vector ARGS contains
6515 any pack expansions, false otherwise. */
6518 any_pack_expanson_args_p (tree args
)
6522 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6523 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6528 /* Convert all template arguments to their appropriate types, and
6529 return a vector containing the innermost resulting template
6530 arguments. If any error occurs, return error_mark_node. Error and
6531 warning messages are issued under control of COMPLAIN.
6533 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6534 for arguments not specified in ARGS. Otherwise, if
6535 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6536 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6537 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6541 coerce_template_parms (tree parms
,
6544 tsubst_flags_t complain
,
6545 bool require_all_args
,
6546 bool use_default_args
)
6548 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6551 tree new_inner_args
;
6552 int saved_unevaluated_operand
;
6553 int saved_inhibit_evaluation_warnings
;
6555 /* When used as a boolean value, indicates whether this is a
6556 variadic template parameter list. Since it's an int, we can also
6557 subtract it from nparms to get the number of non-variadic
6560 int post_variadic_parms
= 0;
6562 if (args
== error_mark_node
)
6563 return error_mark_node
;
6565 nparms
= TREE_VEC_LENGTH (parms
);
6567 /* Determine if there are any parameter packs. */
6568 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6570 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6572 ++post_variadic_parms
;
6573 if (template_parameter_pack_p (tparm
))
6577 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6578 /* If there are no parameters that follow a parameter pack, we need to
6579 expand any argument packs so that we can deduce a parameter pack from
6580 some non-packed args followed by an argument pack, as in variadic85.C.
6581 If there are such parameters, we need to leave argument packs intact
6582 so the arguments are assigned properly. This can happen when dealing
6583 with a nested class inside a partial specialization of a class
6584 template, as in variadic92.C, or when deducing a template parameter pack
6585 from a sub-declarator, as in variadic114.C. */
6586 if (!post_variadic_parms
)
6587 inner_args
= expand_template_argument_pack (inner_args
);
6589 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6590 if ((nargs
> nparms
&& !variadic_p
)
6591 || (nargs
< nparms
- variadic_p
6593 && !any_pack_expanson_args_p (inner_args
)
6594 && (!use_default_args
6595 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6596 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6598 if (complain
& tf_error
)
6602 nparms
-= variadic_p
;
6603 error ("wrong number of template arguments "
6604 "(%d, should be %d or more)", nargs
, nparms
);
6607 error ("wrong number of template arguments "
6608 "(%d, should be %d)", nargs
, nparms
);
6611 error ("provided for %q+D", in_decl
);
6614 return error_mark_node
;
6617 /* We need to evaluate the template arguments, even though this
6618 template-id may be nested within a "sizeof". */
6619 saved_unevaluated_operand
= cp_unevaluated_operand
;
6620 cp_unevaluated_operand
= 0;
6621 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6622 c_inhibit_evaluation_warnings
= 0;
6623 new_inner_args
= make_tree_vec (nparms
);
6624 new_args
= add_outermost_template_args (args
, new_inner_args
);
6625 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6630 /* Get the Ith template parameter. */
6631 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6633 if (parm
== error_mark_node
)
6635 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6639 /* Calculate the next argument. */
6640 if (arg_idx
< nargs
)
6641 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6645 if (template_parameter_pack_p (TREE_VALUE (parm
))
6646 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6648 /* All remaining arguments will be placed in the
6649 template parameter pack PARM. */
6650 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6651 inner_args
, arg_idx
,
6655 /* Store this argument. */
6656 if (arg
== error_mark_node
)
6658 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6660 /* We are done with all of the arguments. */
6667 if (PACK_EXPANSION_P (arg
))
6669 /* We don't know how many args we have yet, just
6670 use the unconverted ones for now. */
6671 new_inner_args
= inner_args
;
6675 else if (require_all_args
)
6677 /* There must be a default arg in this case. */
6678 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6680 /* The position of the first default template argument,
6681 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6683 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6684 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6689 if (arg
== error_mark_node
)
6691 if (complain
& tf_error
)
6692 error ("template argument %d is invalid", arg_idx
+ 1);
6695 /* This only occurs if there was an error in the template
6696 parameter list itself (which we would already have
6697 reported) that we are trying to recover from, e.g., a class
6698 template with a parameter list such as
6699 template<typename..., typename>. */
6702 arg
= convert_template_argument (TREE_VALUE (parm
),
6703 arg
, new_args
, complain
,
6706 if (arg
== error_mark_node
)
6708 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6710 cp_unevaluated_operand
= saved_unevaluated_operand
;
6711 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6714 return error_mark_node
;
6716 #ifdef ENABLE_CHECKING
6717 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6718 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6719 TREE_VEC_LENGTH (new_inner_args
));
6722 return new_inner_args
;
6725 /* Like coerce_template_parms. If PARMS represents all template
6726 parameters levels, this function returns a vector of vectors
6727 representing all the resulting argument levels. Note that in this
6728 case, only the innermost arguments are coerced because the
6729 outermost ones are supposed to have been coerced already.
6731 Otherwise, if PARMS represents only (the innermost) vector of
6732 parameters, this function returns a vector containing just the
6733 innermost resulting arguments. */
6736 coerce_innermost_template_parms (tree parms
,
6739 tsubst_flags_t complain
,
6740 bool require_all_args
,
6741 bool use_default_args
)
6743 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6744 int args_depth
= TMPL_ARGS_DEPTH (args
);
6747 if (parms_depth
> 1)
6749 coerced_args
= make_tree_vec (parms_depth
);
6753 for (level
= parms
, cur_depth
= parms_depth
;
6754 parms_depth
> 0 && level
!= NULL_TREE
;
6755 level
= TREE_CHAIN (level
), --cur_depth
)
6758 if (cur_depth
== args_depth
)
6759 l
= coerce_template_parms (TREE_VALUE (level
),
6760 args
, in_decl
, complain
,
6764 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6766 if (l
== error_mark_node
)
6767 return error_mark_node
;
6769 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6773 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6774 args
, in_decl
, complain
,
6777 return coerced_args
;
6780 /* Returns 1 if template args OT and NT are equivalent. */
6783 template_args_equal (tree ot
, tree nt
)
6787 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6790 if (TREE_CODE (nt
) == TREE_VEC
)
6791 /* For member templates */
6792 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6793 else if (PACK_EXPANSION_P (ot
))
6794 return (PACK_EXPANSION_P (nt
)
6795 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6796 PACK_EXPANSION_PATTERN (nt
))
6797 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6798 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6799 else if (ARGUMENT_PACK_P (ot
))
6804 if (!ARGUMENT_PACK_P (nt
))
6807 opack
= ARGUMENT_PACK_ARGS (ot
);
6808 npack
= ARGUMENT_PACK_ARGS (nt
);
6809 len
= TREE_VEC_LENGTH (opack
);
6810 if (TREE_VEC_LENGTH (npack
) != len
)
6812 for (i
= 0; i
< len
; ++i
)
6813 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6814 TREE_VEC_ELT (npack
, i
)))
6818 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6820 /* We get here probably because we are in the middle of substituting
6821 into the pattern of a pack expansion. In that case the
6822 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6823 interested in. So we want to use the initial pack argument for
6825 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6826 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6827 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6828 return template_args_equal (ot
, nt
);
6830 else if (TYPE_P (nt
))
6831 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6832 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6835 return cp_tree_equal (ot
, nt
);
6838 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6839 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6840 NEWARG_PTR with the offending arguments if they are non-NULL. */
6843 comp_template_args_with_info (tree oldargs
, tree newargs
,
6844 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6848 if (oldargs
== newargs
)
6851 if (!oldargs
|| !newargs
)
6854 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6857 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6859 tree nt
= TREE_VEC_ELT (newargs
, i
);
6860 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6862 if (! template_args_equal (ot
, nt
))
6864 if (oldarg_ptr
!= NULL
)
6866 if (newarg_ptr
!= NULL
)
6874 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6875 of template arguments. Returns 0 otherwise. */
6878 comp_template_args (tree oldargs
, tree newargs
)
6880 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6884 add_pending_template (tree d
)
6886 tree ti
= (TYPE_P (d
)
6887 ? CLASSTYPE_TEMPLATE_INFO (d
)
6888 : DECL_TEMPLATE_INFO (d
));
6889 struct pending_template
*pt
;
6892 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6895 /* We are called both from instantiate_decl, where we've already had a
6896 tinst_level pushed, and instantiate_template, where we haven't.
6898 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6901 push_tinst_level (d
);
6903 pt
= ggc_alloc_pending_template ();
6905 pt
->tinst
= current_tinst_level
;
6906 if (last_pending_template
)
6907 last_pending_template
->next
= pt
;
6909 pending_templates
= pt
;
6911 last_pending_template
= pt
;
6913 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6920 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6921 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6922 documentation for TEMPLATE_ID_EXPR. */
6925 lookup_template_function (tree fns
, tree arglist
)
6929 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6930 return error_mark_node
;
6932 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6934 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6936 error ("%q#D is not a function template", fns
);
6937 return error_mark_node
;
6940 if (BASELINK_P (fns
))
6942 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6944 BASELINK_FUNCTIONS (fns
),
6949 type
= TREE_TYPE (fns
);
6950 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6951 type
= unknown_type_node
;
6953 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6956 /* Within the scope of a template class S<T>, the name S gets bound
6957 (in build_self_reference) to a TYPE_DECL for the class, not a
6958 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6959 or one of its enclosing classes, and that type is a template,
6960 return the associated TEMPLATE_DECL. Otherwise, the original
6963 Also handle the case when DECL is a TREE_LIST of ambiguous
6964 injected-class-names from different bases. */
6967 maybe_get_template_decl_from_type_decl (tree decl
)
6969 if (decl
== NULL_TREE
)
6972 /* DR 176: A lookup that finds an injected-class-name (10.2
6973 [class.member.lookup]) can result in an ambiguity in certain cases
6974 (for example, if it is found in more than one base class). If all of
6975 the injected-class-names that are found refer to specializations of
6976 the same class template, and if the name is followed by a
6977 template-argument-list, the reference refers to the class template
6978 itself and not a specialization thereof, and is not ambiguous. */
6979 if (TREE_CODE (decl
) == TREE_LIST
)
6981 tree t
, tmpl
= NULL_TREE
;
6982 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6984 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6987 else if (tmpl
!= elt
)
6990 if (tmpl
&& t
== NULL_TREE
)
6996 return (decl
!= NULL_TREE
6997 && DECL_SELF_REFERENCE_P (decl
)
6998 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6999 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7002 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7003 parameters, find the desired type.
7005 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7007 IN_DECL, if non-NULL, is the template declaration we are trying to
7010 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7011 the class we are looking up.
7013 Issue error and warning messages under control of COMPLAIN.
7015 If the template class is really a local class in a template
7016 function, then the FUNCTION_CONTEXT is the function in which it is
7019 ??? Note that this function is currently called *twice* for each
7020 template-id: the first time from the parser, while creating the
7021 incomplete type (finish_template_type), and the second type during the
7022 real instantiation (instantiate_template_class). This is surely something
7023 that we want to avoid. It also causes some problems with argument
7024 coercion (see convert_nontype_argument for more information on this). */
7027 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7028 int entering_scope
, tsubst_flags_t complain
)
7030 tree templ
= NULL_TREE
, parmlist
;
7037 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7039 tree value
= innermost_non_namespace_value (d1
);
7040 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7045 push_decl_namespace (context
);
7046 templ
= lookup_name (d1
);
7047 templ
= maybe_get_template_decl_from_type_decl (templ
);
7049 pop_decl_namespace ();
7052 context
= DECL_CONTEXT (templ
);
7054 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7056 tree type
= TREE_TYPE (d1
);
7058 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7059 an implicit typename for the second A. Deal with it. */
7060 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7061 type
= TREE_TYPE (type
);
7063 if (CLASSTYPE_TEMPLATE_INFO (type
))
7065 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7066 d1
= DECL_NAME (templ
);
7069 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7070 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7072 templ
= TYPE_TI_TEMPLATE (d1
);
7073 d1
= DECL_NAME (templ
);
7075 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7076 && DECL_TEMPLATE_RESULT (d1
)
7077 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7080 d1
= DECL_NAME (templ
);
7081 context
= DECL_CONTEXT (templ
);
7084 /* Issue an error message if we didn't find a template. */
7087 if (complain
& tf_error
)
7088 error ("%qT is not a template", d1
);
7089 return error_mark_node
;
7092 if (TREE_CODE (templ
) != TEMPLATE_DECL
7093 /* Make sure it's a user visible template, if it was named by
7095 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7096 && !PRIMARY_TEMPLATE_P (templ
)))
7098 if (complain
& tf_error
)
7100 error ("non-template type %qT used as a template", d1
);
7102 error ("for template declaration %q+D", in_decl
);
7104 return error_mark_node
;
7107 complain
&= ~tf_user
;
7109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7111 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7112 template arguments */
7118 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7120 /* Consider an example where a template template parameter declared as
7122 template <class T, class U = std::allocator<T> > class TT
7124 The template parameter level of T and U are one level larger than
7125 of TT. To proper process the default argument of U, say when an
7126 instantiation `TT<int>' is seen, we need to build the full
7127 arguments containing {int} as the innermost level. Outer levels,
7128 available when not appearing as default template argument, can be
7129 obtained from the arguments of the enclosing template.
7131 Suppose that TT is later substituted with std::vector. The above
7132 instantiation is `TT<int, std::allocator<T> >' with TT at
7133 level 1, and T at level 2, while the template arguments at level 1
7134 becomes {std::vector} and the inner level 2 is {int}. */
7136 outer
= DECL_CONTEXT (templ
);
7138 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7139 else if (current_template_parms
)
7140 /* This is an argument of the current template, so we haven't set
7141 DECL_CONTEXT yet. */
7142 outer
= current_template_args ();
7145 arglist
= add_to_template_args (outer
, arglist
);
7147 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7149 /*require_all_args=*/true,
7150 /*use_default_args=*/true);
7151 if (arglist2
== error_mark_node
7152 || (!uses_template_parms (arglist2
)
7153 && check_instantiated_args (templ
, arglist2
, complain
)))
7154 return error_mark_node
;
7156 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7161 tree template_type
= TREE_TYPE (templ
);
7164 tree found
= NULL_TREE
;
7167 int is_dependent_type
;
7168 int use_partial_inst_tmpl
= false;
7170 if (template_type
== error_mark_node
)
7171 /* An error occured while building the template TEMPL, and a
7172 diagnostic has most certainly been emitted for that
7173 already. Let's propagate that error. */
7174 return error_mark_node
;
7176 gen_tmpl
= most_general_template (templ
);
7177 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7178 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7179 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7181 if (arg_depth
== 1 && parm_depth
> 1)
7183 /* We've been given an incomplete set of template arguments.
7186 template <class T> struct S1 {
7187 template <class U> struct S2 {};
7188 template <class U> struct S2<U*> {};
7191 we will be called with an ARGLIST of `U*', but the
7192 TEMPLATE will be `template <class T> template
7193 <class U> struct S1<T>::S2'. We must fill in the missing
7196 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7198 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7201 /* Now we should have enough arguments. */
7202 gcc_assert (parm_depth
== arg_depth
);
7204 /* From here on, we're only interested in the most general
7207 /* Calculate the BOUND_ARGS. These will be the args that are
7208 actually tsubst'd into the definition to create the
7212 /* We have multiple levels of arguments to coerce, at once. */
7214 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7216 tree bound_args
= make_tree_vec (parm_depth
);
7218 for (i
= saved_depth
,
7219 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7220 i
> 0 && t
!= NULL_TREE
;
7221 --i
, t
= TREE_CHAIN (t
))
7224 if (i
== saved_depth
)
7225 a
= coerce_template_parms (TREE_VALUE (t
),
7228 /*require_all_args=*/true,
7229 /*use_default_args=*/true);
7231 /* Outer levels should have already been coerced. */
7232 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7234 /* Don't process further if one of the levels fails. */
7235 if (a
== error_mark_node
)
7237 /* Restore the ARGLIST to its full size. */
7238 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7239 return error_mark_node
;
7242 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7244 /* We temporarily reduce the length of the ARGLIST so
7245 that coerce_template_parms will see only the arguments
7246 corresponding to the template parameters it is
7248 TREE_VEC_LENGTH (arglist
)--;
7251 /* Restore the ARGLIST to its full size. */
7252 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7254 arglist
= bound_args
;
7258 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7259 INNERMOST_TEMPLATE_ARGS (arglist
),
7262 /*require_all_args=*/true,
7263 /*use_default_args=*/true);
7265 if (arglist
== error_mark_node
)
7266 /* We were unable to bind the arguments. */
7267 return error_mark_node
;
7269 /* In the scope of a template class, explicit references to the
7270 template class refer to the type of the template, not any
7271 instantiation of it. For example, in:
7273 template <class T> class C { void f(C<T>); }
7275 the `C<T>' is just the same as `C'. Outside of the
7276 class, however, such a reference is an instantiation. */
7278 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7279 || currently_open_class (template_type
))
7280 /* comp_template_args is expensive, check it last. */
7281 && comp_template_args (TYPE_TI_ARGS (template_type
),
7283 return template_type
;
7285 /* If we already have this specialization, return it. */
7286 elt
.tmpl
= gen_tmpl
;
7288 hash
= hash_specialization (&elt
);
7289 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7295 is_dependent_type
= uses_template_parms (arglist
);
7297 /* If the deduced arguments are invalid, then the binding
7299 if (!is_dependent_type
7300 && check_instantiated_args (gen_tmpl
,
7301 INNERMOST_TEMPLATE_ARGS (arglist
),
7303 return error_mark_node
;
7305 if (!is_dependent_type
7306 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7307 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7308 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7310 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7311 DECL_NAME (gen_tmpl
),
7312 /*tag_scope=*/ts_global
);
7316 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7318 if (context
== error_mark_node
)
7319 return error_mark_node
;
7322 context
= global_namespace
;
7324 /* Create the type. */
7325 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7327 if (!is_dependent_type
)
7329 set_current_access_from_decl (TYPE_NAME (template_type
));
7330 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7331 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7332 arglist
, complain
, in_decl
),
7333 SCOPED_ENUM_P (template_type
), NULL
);
7337 /* We don't want to call start_enum for this type, since
7338 the values for the enumeration constants may involve
7339 template parameters. And, no one should be interested
7340 in the enumeration constants for such a type. */
7341 t
= cxx_make_type (ENUMERAL_TYPE
);
7342 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7344 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7345 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7346 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7348 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7350 /* The user referred to a specialization of an alias
7351 template represented by GEN_TMPL.
7353 [temp.alias]/2 says:
7355 When a template-id refers to the specialization of an
7356 alias template, it is equivalent to the associated
7357 type obtained by substitution of its
7358 template-arguments for the template-parameters in the
7359 type-id of the alias template. */
7361 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7362 /* Note that the call above (by indirectly calling
7363 register_specialization in tsubst_decl) registers the
7364 TYPE_DECL representing the specialization of the alias
7365 template. So next time someone substitutes ARGLIST for
7366 the template parms into the alias template (GEN_TMPL),
7367 she'll get that TYPE_DECL back. */
7369 if (t
== error_mark_node
)
7372 else if (CLASS_TYPE_P (template_type
))
7374 t
= make_class_type (TREE_CODE (template_type
));
7375 CLASSTYPE_DECLARED_CLASS (t
)
7376 = CLASSTYPE_DECLARED_CLASS (template_type
);
7377 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7378 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7380 /* A local class. Make sure the decl gets registered properly. */
7381 if (context
== current_function_decl
)
7382 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7384 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7385 /* This instantiation is another name for the primary
7386 template type. Set the TYPE_CANONICAL field
7388 TYPE_CANONICAL (t
) = template_type
;
7389 else if (any_template_arguments_need_structural_equality_p (arglist
))
7390 /* Some of the template arguments require structural
7391 equality testing, so this template class requires
7392 structural equality testing. */
7393 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7398 /* If we called start_enum or pushtag above, this information
7399 will already be set up. */
7402 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7404 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7405 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7406 DECL_SOURCE_LOCATION (type_decl
)
7407 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7410 type_decl
= TYPE_NAME (t
);
7412 if (CLASS_TYPE_P (template_type
))
7414 TREE_PRIVATE (type_decl
)
7415 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7416 TREE_PROTECTED (type_decl
)
7417 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7418 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7420 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7421 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7425 /* Let's consider the explicit specialization of a member
7426 of a class template specialization that is implicitely instantiated,
7431 template<class U> struct M {}; //#0
7436 struct S<int>::M<char> //#1
7440 [temp.expl.spec]/4 says this is valid.
7442 In this case, when we write:
7445 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7448 When we encounter #1, we want to store the partial instantiation
7449 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7451 For all cases other than this "explicit specialization of member of a
7452 class template", we just want to store the most general template into
7453 the CLASSTYPE_TI_TEMPLATE of M.
7455 This case of "explicit specialization of member of a class template"
7457 1/ the enclosing class is an instantiation of, and therefore not
7458 the same as, the context of the most general template, and
7459 2/ we aren't looking at the partial instantiation itself, i.e.
7460 the innermost arguments are not the same as the innermost parms of
7461 the most general template.
7463 So it's only when 1/ and 2/ happens that we want to use the partial
7464 instantiation of the member template in lieu of its most general
7467 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7468 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7469 /* the enclosing class must be an instantiation... */
7470 && CLASS_TYPE_P (context
)
7471 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7473 tree partial_inst_args
;
7474 TREE_VEC_LENGTH (arglist
)--;
7475 ++processing_template_decl
;
7477 tsubst (INNERMOST_TEMPLATE_ARGS
7478 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7479 arglist
, complain
, NULL_TREE
);
7480 --processing_template_decl
;
7481 TREE_VEC_LENGTH (arglist
)++;
7482 use_partial_inst_tmpl
=
7483 /*...and we must not be looking at the partial instantiation
7485 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7489 if (!use_partial_inst_tmpl
)
7490 /* This case is easy; there are no member templates involved. */
7494 /* This is a full instantiation of a member template. Find
7495 the partial instantiation of which this is an instance. */
7497 /* Temporarily reduce by one the number of levels in the ARGLIST
7498 so as to avoid comparing the last set of arguments. */
7499 TREE_VEC_LENGTH (arglist
)--;
7500 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7501 TREE_VEC_LENGTH (arglist
)++;
7502 /* FOUND is either a proper class type, or an alias
7503 template specialization. In the later case, it's a
7504 TYPE_DECL, resulting from the substituting of arguments
7505 for parameters in the TYPE_DECL of the alias template
7506 done earlier. So be careful while getting the template
7508 found
= TREE_CODE (found
) == TYPE_DECL
7509 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7510 : CLASSTYPE_TI_TEMPLATE (found
);
7513 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7516 slot
= htab_find_slot_with_hash (type_specializations
,
7517 &elt
, hash
, INSERT
);
7518 entry
= ggc_alloc_spec_entry ();
7522 /* Note this use of the partial instantiation so we can check it
7523 later in maybe_process_partial_specialization. */
7524 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7525 = tree_cons (arglist
, t
,
7526 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7528 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7529 /* Now that the type has been registered on the instantiations
7530 list, we set up the enumerators. Because the enumeration
7531 constants may involve the enumeration type itself, we make
7532 sure to register the type first, and then create the
7533 constants. That way, doing tsubst_expr for the enumeration
7534 constants won't result in recursive calls here; we'll find
7535 the instantiation and exit above. */
7536 tsubst_enum (template_type
, t
, arglist
);
7538 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7539 /* If the type makes use of template parameters, the
7540 code that generates debugging information will crash. */
7541 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7543 /* Possibly limit visibility based on template args. */
7544 TREE_PUBLIC (type_decl
) = 1;
7545 determine_visibility (type_decl
);
7551 /* Wrapper for lookup_template_class_1. */
7554 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7555 int entering_scope
, tsubst_flags_t complain
)
7558 timevar_push (TV_TEMPLATE_INST
);
7559 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7560 entering_scope
, complain
);
7561 timevar_pop (TV_TEMPLATE_INST
);
7569 /* True when we should also visit template parameters that occur in
7570 non-deduced contexts. */
7571 bool include_nondeduced_p
;
7572 struct pointer_set_t
*visited
;
7575 /* Called from for_each_template_parm via walk_tree. */
7578 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7581 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7582 tree_fn_t fn
= pfd
->fn
;
7583 void *data
= pfd
->data
;
7586 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7587 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7588 pfd
->include_nondeduced_p
))
7589 return error_mark_node
;
7591 switch (TREE_CODE (t
))
7594 if (TYPE_PTRMEMFUNC_P (t
))
7600 if (!TYPE_TEMPLATE_INFO (t
))
7602 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7603 fn
, data
, pfd
->visited
,
7604 pfd
->include_nondeduced_p
))
7605 return error_mark_node
;
7609 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7610 fn
, data
, pfd
->visited
,
7611 pfd
->include_nondeduced_p
)
7612 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7613 fn
, data
, pfd
->visited
,
7614 pfd
->include_nondeduced_p
))
7615 return error_mark_node
;
7619 /* Since we're not going to walk subtrees, we have to do this
7621 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7622 pfd
->visited
, pfd
->include_nondeduced_p
))
7623 return error_mark_node
;
7627 /* Check the return type. */
7628 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7629 pfd
->include_nondeduced_p
))
7630 return error_mark_node
;
7632 /* Check the parameter types. Since default arguments are not
7633 instantiated until they are needed, the TYPE_ARG_TYPES may
7634 contain expressions that involve template parameters. But,
7635 no-one should be looking at them yet. And, once they're
7636 instantiated, they don't contain template parameters, so
7637 there's no point in looking at them then, either. */
7641 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7642 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7643 pfd
->visited
, pfd
->include_nondeduced_p
))
7644 return error_mark_node
;
7646 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7647 want walk_tree walking into them itself. */
7653 case UNDERLYING_TYPE
:
7654 if (pfd
->include_nondeduced_p
7655 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7657 pfd
->include_nondeduced_p
))
7658 return error_mark_node
;
7663 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7664 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7665 pfd
->visited
, pfd
->include_nondeduced_p
))
7666 return error_mark_node
;
7671 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7672 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7673 pfd
->visited
, pfd
->include_nondeduced_p
))
7674 return error_mark_node
;
7675 if (DECL_CONTEXT (t
)
7676 && pfd
->include_nondeduced_p
7677 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7678 pfd
->visited
, pfd
->include_nondeduced_p
))
7679 return error_mark_node
;
7682 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7683 /* Record template parameters such as `T' inside `TT<T>'. */
7684 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7685 pfd
->include_nondeduced_p
))
7686 return error_mark_node
;
7689 case TEMPLATE_TEMPLATE_PARM
:
7690 case TEMPLATE_TYPE_PARM
:
7691 case TEMPLATE_PARM_INDEX
:
7692 if (fn
&& (*fn
)(t
, data
))
7693 return error_mark_node
;
7695 return error_mark_node
;
7699 /* A template template parameter is encountered. */
7700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7701 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7702 pfd
->include_nondeduced_p
))
7703 return error_mark_node
;
7705 /* Already substituted template template parameter */
7711 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7713 pfd
->include_nondeduced_p
))
7714 return error_mark_node
;
7718 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7719 && pfd
->include_nondeduced_p
7720 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7721 (TREE_TYPE (t
)), fn
, data
,
7722 pfd
->visited
, pfd
->include_nondeduced_p
))
7723 return error_mark_node
;
7728 /* If there's no type, then this thing must be some expression
7729 involving template parameters. */
7730 if (!fn
&& !TREE_TYPE (t
))
7731 return error_mark_node
;
7736 case IMPLICIT_CONV_EXPR
:
7737 case REINTERPRET_CAST_EXPR
:
7738 case CONST_CAST_EXPR
:
7739 case STATIC_CAST_EXPR
:
7740 case DYNAMIC_CAST_EXPR
:
7744 case PSEUDO_DTOR_EXPR
:
7746 return error_mark_node
;
7753 /* We didn't find any template parameters we liked. */
7757 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7758 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7759 call FN with the parameter and the DATA.
7760 If FN returns nonzero, the iteration is terminated, and
7761 for_each_template_parm returns 1. Otherwise, the iteration
7762 continues. If FN never returns a nonzero value, the value
7763 returned by for_each_template_parm is 0. If FN is NULL, it is
7764 considered to be the function which always returns 1.
7766 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7767 parameters that occur in non-deduced contexts. When false, only
7768 visits those template parameters that can be deduced. */
7771 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7772 struct pointer_set_t
*visited
,
7773 bool include_nondeduced_p
)
7775 struct pair_fn_data pfd
;
7781 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7783 /* Walk the tree. (Conceptually, we would like to walk without
7784 duplicates, but for_each_template_parm_r recursively calls
7785 for_each_template_parm, so we would need to reorganize a fair
7786 bit to use walk_tree_without_duplicates, so we keep our own
7789 pfd
.visited
= visited
;
7791 pfd
.visited
= pointer_set_create ();
7792 result
= cp_walk_tree (&t
,
7793 for_each_template_parm_r
,
7795 pfd
.visited
) != NULL_TREE
;
7800 pointer_set_destroy (pfd
.visited
);
7807 /* Returns true if T depends on any template parameter. */
7810 uses_template_parms (tree t
)
7813 int saved_processing_template_decl
;
7815 saved_processing_template_decl
= processing_template_decl
;
7816 if (!saved_processing_template_decl
)
7817 processing_template_decl
= 1;
7819 dependent_p
= dependent_type_p (t
);
7820 else if (TREE_CODE (t
) == TREE_VEC
)
7821 dependent_p
= any_dependent_template_arguments_p (t
);
7822 else if (TREE_CODE (t
) == TREE_LIST
)
7823 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7824 || uses_template_parms (TREE_CHAIN (t
)));
7825 else if (TREE_CODE (t
) == TYPE_DECL
)
7826 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7829 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7830 || TREE_CODE (t
) == OVERLOAD
7832 || TREE_CODE (t
) == IDENTIFIER_NODE
7833 || TREE_CODE (t
) == TRAIT_EXPR
7834 || TREE_CODE (t
) == CONSTRUCTOR
7835 || CONSTANT_CLASS_P (t
))
7836 dependent_p
= (type_dependent_expression_p (t
)
7837 || value_dependent_expression_p (t
));
7840 gcc_assert (t
== error_mark_node
);
7841 dependent_p
= false;
7844 processing_template_decl
= saved_processing_template_decl
;
7849 /* Returns true iff current_function_decl is an incompletely instantiated
7850 template. Useful instead of processing_template_decl because the latter
7851 is set to 0 during fold_non_dependent_expr. */
7854 in_template_function (void)
7856 tree fn
= current_function_decl
;
7858 ++processing_template_decl
;
7859 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7860 && DECL_TEMPLATE_INFO (fn
)
7861 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7862 --processing_template_decl
;
7866 /* Returns true if T depends on any template parameter with level LEVEL. */
7869 uses_template_parms_level (tree t
, int level
)
7871 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7872 /*include_nondeduced_p=*/true);
7875 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7876 ill-formed translation unit, i.e. a variable or function that isn't
7877 usable in a constant expression. */
7880 neglectable_inst_p (tree d
)
7883 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7884 : decl_maybe_constant_var_p (d
)));
7887 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7888 neglectable and instantiated from within an erroneous instantiation. */
7891 limit_bad_template_recursion (tree decl
)
7893 struct tinst_level
*lev
= current_tinst_level
;
7894 int errs
= errorcount
+ sorrycount
;
7895 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7898 for (; lev
; lev
= lev
->next
)
7899 if (neglectable_inst_p (lev
->decl
))
7902 return (lev
&& errs
> lev
->errors
);
7905 static int tinst_depth
;
7906 extern int max_tinst_depth
;
7909 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7911 /* We're starting to instantiate D; record the template instantiation context
7912 for diagnostics and to restore it later. */
7915 push_tinst_level (tree d
)
7917 struct tinst_level
*new_level
;
7919 if (tinst_depth
>= max_tinst_depth
)
7921 last_error_tinst_level
= current_tinst_level
;
7922 if (TREE_CODE (d
) == TREE_LIST
)
7923 error ("template instantiation depth exceeds maximum of %d (use "
7924 "-ftemplate-depth= to increase the maximum) substituting %qS",
7925 max_tinst_depth
, d
);
7927 error ("template instantiation depth exceeds maximum of %d (use "
7928 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7929 max_tinst_depth
, d
);
7931 print_instantiation_context ();
7936 /* If the current instantiation caused problems, don't let it instantiate
7937 anything else. Do allow deduction substitution and decls usable in
7938 constant expressions. */
7939 if (limit_bad_template_recursion (d
))
7942 new_level
= ggc_alloc_tinst_level ();
7943 new_level
->decl
= d
;
7944 new_level
->locus
= input_location
;
7945 new_level
->errors
= errorcount
+sorrycount
;
7946 new_level
->in_system_header_p
= in_system_header
;
7947 new_level
->next
= current_tinst_level
;
7948 current_tinst_level
= new_level
;
7951 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7952 depth_reached
= tinst_depth
;
7957 /* We're done instantiating this template; return to the instantiation
7961 pop_tinst_level (void)
7963 /* Restore the filename and line number stashed away when we started
7964 this instantiation. */
7965 input_location
= current_tinst_level
->locus
;
7966 current_tinst_level
= current_tinst_level
->next
;
7970 /* We're instantiating a deferred template; restore the template
7971 instantiation context in which the instantiation was requested, which
7972 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7975 reopen_tinst_level (struct tinst_level
*level
)
7977 struct tinst_level
*t
;
7980 for (t
= level
; t
; t
= t
->next
)
7983 current_tinst_level
= level
;
7985 if (current_tinst_level
)
7986 current_tinst_level
->errors
= errorcount
+sorrycount
;
7990 /* Returns the TINST_LEVEL which gives the original instantiation
7993 struct tinst_level
*
7994 outermost_tinst_level (void)
7996 struct tinst_level
*level
= current_tinst_level
;
7999 level
= level
->next
;
8003 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8004 vector of template arguments, as for tsubst.
8006 Returns an appropriate tsubst'd friend declaration. */
8009 tsubst_friend_function (tree decl
, tree args
)
8013 if (TREE_CODE (decl
) == FUNCTION_DECL
8014 && DECL_TEMPLATE_INSTANTIATION (decl
)
8015 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8016 /* This was a friend declared with an explicit template
8017 argument list, e.g.:
8021 to indicate that f was a template instantiation, not a new
8022 function declaration. Now, we have to figure out what
8023 instantiation of what template. */
8025 tree template_id
, arglist
, fns
;
8028 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8030 /* Friend functions are looked up in the containing namespace scope.
8031 We must enter that scope, to avoid finding member functions of the
8032 current class with same name. */
8033 push_nested_namespace (ns
);
8034 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8035 tf_warning_or_error
, NULL_TREE
,
8036 /*integral_constant_expression_p=*/false);
8037 pop_nested_namespace (ns
);
8038 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8039 tf_warning_or_error
, NULL_TREE
);
8040 template_id
= lookup_template_function (fns
, arglist
);
8042 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8043 tmpl
= determine_specialization (template_id
, new_friend
,
8045 /*need_member_template=*/0,
8046 TREE_VEC_LENGTH (args
),
8048 return instantiate_template (tmpl
, new_args
, tf_error
);
8051 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8053 /* The NEW_FRIEND will look like an instantiation, to the
8054 compiler, but is not an instantiation from the point of view of
8055 the language. For example, we might have had:
8057 template <class T> struct S {
8058 template <class U> friend void f(T, U);
8061 Then, in S<int>, template <class U> void f(int, U) is not an
8062 instantiation of anything. */
8063 if (new_friend
== error_mark_node
)
8064 return error_mark_node
;
8066 DECL_USE_TEMPLATE (new_friend
) = 0;
8067 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8069 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8070 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8071 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8074 /* The mangled name for the NEW_FRIEND is incorrect. The function
8075 is not a template instantiation and should not be mangled like
8076 one. Therefore, we forget the mangling here; we'll recompute it
8077 later if we need it. */
8078 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8080 SET_DECL_RTL (new_friend
, NULL
);
8081 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8084 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8087 tree new_friend_template_info
;
8088 tree new_friend_result_template_info
;
8090 int new_friend_is_defn
;
8092 /* We must save some information from NEW_FRIEND before calling
8093 duplicate decls since that function will free NEW_FRIEND if
8095 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8096 new_friend_is_defn
=
8097 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8098 (template_for_substitution (new_friend
)))
8100 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8102 /* This declaration is a `primary' template. */
8103 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8105 new_friend_result_template_info
8106 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8109 new_friend_result_template_info
= NULL_TREE
;
8111 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8112 if (new_friend_is_defn
)
8113 DECL_INITIAL (new_friend
) = error_mark_node
;
8115 /* Inside pushdecl_namespace_level, we will push into the
8116 current namespace. However, the friend function should go
8117 into the namespace of the template. */
8118 ns
= decl_namespace_context (new_friend
);
8119 push_nested_namespace (ns
);
8120 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8121 pop_nested_namespace (ns
);
8123 if (old_decl
== error_mark_node
)
8124 return error_mark_node
;
8126 if (old_decl
!= new_friend
)
8128 /* This new friend declaration matched an existing
8129 declaration. For example, given:
8131 template <class T> void f(T);
8132 template <class U> class C {
8133 template <class T> friend void f(T) {}
8136 the friend declaration actually provides the definition
8137 of `f', once C has been instantiated for some type. So,
8138 old_decl will be the out-of-class template declaration,
8139 while new_friend is the in-class definition.
8141 But, if `f' was called before this point, the
8142 instantiation of `f' will have DECL_TI_ARGS corresponding
8143 to `T' but not to `U', references to which might appear
8144 in the definition of `f'. Previously, the most general
8145 template for an instantiation of `f' was the out-of-class
8146 version; now it is the in-class version. Therefore, we
8147 run through all specialization of `f', adding to their
8148 DECL_TI_ARGS appropriately. In particular, they need a
8149 new set of outer arguments, corresponding to the
8150 arguments for this class instantiation.
8152 The same situation can arise with something like this:
8155 template <class T> class C {
8159 when `C<int>' is instantiated. Now, `f(int)' is defined
8162 if (!new_friend_is_defn
)
8163 /* On the other hand, if the in-class declaration does
8164 *not* provide a definition, then we don't want to alter
8165 existing definitions. We can just leave everything
8170 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8171 tree new_args
= TI_ARGS (new_friend_template_info
);
8173 /* Overwrite whatever template info was there before, if
8174 any, with the new template information pertaining to
8176 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8178 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8180 /* We should have called reregister_specialization in
8182 gcc_assert (retrieve_specialization (new_template
,
8186 /* Instantiate it if the global has already been used. */
8187 if (DECL_ODR_USED (old_decl
))
8188 instantiate_decl (old_decl
, /*defer_ok=*/true,
8189 /*expl_inst_class_mem_p=*/false);
8195 /* Indicate that the old function template is a partial
8197 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8198 = new_friend_result_template_info
;
8200 gcc_assert (new_template
8201 == most_general_template (new_template
));
8202 gcc_assert (new_template
!= old_decl
);
8204 /* Reassign any specializations already in the hash table
8205 to the new more general template, and add the
8206 additional template args. */
8207 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8211 tree spec
= TREE_VALUE (t
);
8214 elt
.tmpl
= old_decl
;
8215 elt
.args
= DECL_TI_ARGS (spec
);
8216 elt
.spec
= NULL_TREE
;
8218 htab_remove_elt (decl_specializations
, &elt
);
8221 = add_outermost_template_args (new_args
,
8222 DECL_TI_ARGS (spec
));
8224 register_specialization
8225 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8228 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8232 /* The information from NEW_FRIEND has been merged into OLD_DECL
8233 by duplicate_decls. */
8234 new_friend
= old_decl
;
8239 tree context
= DECL_CONTEXT (new_friend
);
8243 template <class T> class C {
8244 template <class U> friend void C1<U>::f (); // case 1
8245 friend void C2<T>::f (); // case 2
8247 we only need to make sure CONTEXT is a complete type for
8248 case 2. To distinguish between the two cases, we note that
8249 CONTEXT of case 1 remains dependent type after tsubst while
8250 this isn't true for case 2. */
8251 ++processing_template_decl
;
8252 dependent_p
= dependent_type_p (context
);
8253 --processing_template_decl
;
8256 && !complete_type_or_else (context
, NULL_TREE
))
8257 return error_mark_node
;
8259 if (COMPLETE_TYPE_P (context
))
8261 /* Check to see that the declaration is really present, and,
8262 possibly obtain an improved declaration. */
8263 tree fn
= check_classfn (context
,
8264 new_friend
, NULL_TREE
);
8274 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8275 template arguments, as for tsubst.
8277 Returns an appropriate tsubst'd friend type or error_mark_node on
8281 tsubst_friend_class (tree friend_tmpl
, tree args
)
8287 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8289 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8290 return TREE_TYPE (t
);
8293 context
= CP_DECL_CONTEXT (friend_tmpl
);
8295 if (context
!= global_namespace
)
8297 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8298 push_nested_namespace (context
);
8300 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8303 /* Look for a class template declaration. We look for hidden names
8304 because two friend declarations of the same template are the
8305 same. For example, in:
8308 template <typename> friend class F;
8310 template <typename> struct B {
8311 template <typename> friend class F;
8314 both F templates are the same. */
8315 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8316 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8318 /* But, if we don't find one, it might be because we're in a
8319 situation like this:
8327 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8328 for `S<int>', not the TEMPLATE_DECL. */
8329 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8331 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8332 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8335 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8337 /* The friend template has already been declared. Just
8338 check to see that the declarations match, and install any new
8339 default parameters. We must tsubst the default parameters,
8340 of course. We only need the innermost template parameters
8341 because that is all that redeclare_class_template will look
8343 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8344 > TMPL_ARGS_DEPTH (args
))
8347 location_t saved_input_location
;
8348 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8349 args
, tf_warning_or_error
);
8351 saved_input_location
= input_location
;
8352 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8353 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8354 input_location
= saved_input_location
;
8358 friend_type
= TREE_TYPE (tmpl
);
8362 /* The friend template has not already been declared. In this
8363 case, the instantiation of the template class will cause the
8364 injection of this template into the global scope. */
8365 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8366 if (tmpl
== error_mark_node
)
8367 return error_mark_node
;
8369 /* The new TMPL is not an instantiation of anything, so we
8370 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8371 the new type because that is supposed to be the corresponding
8372 template decl, i.e., TMPL. */
8373 DECL_USE_TEMPLATE (tmpl
) = 0;
8374 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8375 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8376 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8377 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8379 /* Inject this template into the global scope. */
8380 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8383 if (context
!= global_namespace
)
8385 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8386 pop_nested_namespace (context
);
8388 pop_nested_class ();
8394 /* Returns zero if TYPE cannot be completed later due to circularity.
8395 Otherwise returns one. */
8398 can_complete_type_without_circularity (tree type
)
8400 if (type
== NULL_TREE
|| type
== error_mark_node
)
8402 else if (COMPLETE_TYPE_P (type
))
8404 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8405 return can_complete_type_without_circularity (TREE_TYPE (type
));
8406 else if (CLASS_TYPE_P (type
)
8407 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8413 /* Apply any attributes which had to be deferred until instantiation
8414 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8415 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8418 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8419 tree args
, tsubst_flags_t complain
, tree in_decl
)
8421 tree last_dep
= NULL_TREE
;
8425 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8426 if (ATTR_IS_DEPENDENT (t
))
8429 attributes
= copy_list (attributes
);
8433 if (DECL_P (*decl_p
))
8435 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8437 p
= &DECL_ATTRIBUTES (*decl_p
);
8440 p
= &TYPE_ATTRIBUTES (*decl_p
);
8444 tree late_attrs
= NULL_TREE
;
8445 tree
*q
= &late_attrs
;
8447 for (*p
= attributes
; *p
; )
8450 if (ATTR_IS_DEPENDENT (t
))
8452 *p
= TREE_CHAIN (t
);
8453 TREE_CHAIN (t
) = NULL_TREE
;
8454 /* If the first attribute argument is an identifier, don't
8455 pass it through tsubst. Attributes like mode, format,
8456 cleanup and several target specific attributes expect it
8459 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8460 && TREE_VALUE (TREE_VALUE (t
))
8461 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8462 == IDENTIFIER_NODE
))
8465 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8467 /*integral_constant_expression_p=*/false);
8468 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8470 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8475 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8476 /*integral_constant_expression_p=*/false);
8478 q
= &TREE_CHAIN (t
);
8481 p
= &TREE_CHAIN (t
);
8484 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8488 /* Perform (or defer) access check for typedefs that were referenced
8489 from within the template TMPL code.
8490 This is a subroutine of instantiate_decl and instantiate_class_template.
8491 TMPL is the template to consider and TARGS is the list of arguments of
8495 perform_typedefs_access_check (tree tmpl
, tree targs
)
8497 location_t saved_location
;
8499 qualified_typedef_usage_t
*iter
;
8502 || (!CLASS_TYPE_P (tmpl
)
8503 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8506 saved_location
= input_location
;
8507 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8509 tree type_decl
= iter
->typedef_decl
;
8510 tree type_scope
= iter
->context
;
8512 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8515 if (uses_template_parms (type_decl
))
8516 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8517 if (uses_template_parms (type_scope
))
8518 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8520 /* Make access check error messages point to the location
8521 of the use of the typedef. */
8522 input_location
= iter
->locus
;
8523 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8524 type_decl
, type_decl
,
8525 tf_warning_or_error
);
8527 input_location
= saved_location
;
8531 instantiate_class_template_1 (tree type
)
8533 tree templ
, args
, pattern
, t
, member
;
8537 unsigned int saved_maximum_field_alignment
;
8540 if (type
== error_mark_node
)
8541 return error_mark_node
;
8543 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8544 || uses_template_parms (type
))
8547 /* Figure out which template is being instantiated. */
8548 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8549 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8551 /* Determine what specialization of the original template to
8553 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8554 if (t
== error_mark_node
)
8556 TYPE_BEING_DEFINED (type
) = 1;
8557 return error_mark_node
;
8561 /* This TYPE is actually an instantiation of a partial
8562 specialization. We replace the innermost set of ARGS with
8563 the arguments appropriate for substitution. For example,
8566 template <class T> struct S {};
8567 template <class T> struct S<T*> {};
8569 and supposing that we are instantiating S<int*>, ARGS will
8570 presently be {int*} -- but we need {int}. */
8571 pattern
= TREE_TYPE (t
);
8572 args
= TREE_PURPOSE (t
);
8576 pattern
= TREE_TYPE (templ
);
8577 args
= CLASSTYPE_TI_ARGS (type
);
8580 /* If the template we're instantiating is incomplete, then clearly
8581 there's nothing we can do. */
8582 if (!COMPLETE_TYPE_P (pattern
))
8585 /* If we've recursively instantiated too many templates, stop. */
8586 if (! push_tinst_level (type
))
8589 /* Now we're really doing the instantiation. Mark the type as in
8590 the process of being defined. */
8591 TYPE_BEING_DEFINED (type
) = 1;
8593 /* We may be in the middle of deferred access check. Disable
8595 push_deferring_access_checks (dk_no_deferred
);
8597 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8599 push_to_top_level ();
8600 /* Use #pragma pack from the template context. */
8601 saved_maximum_field_alignment
= maximum_field_alignment
;
8602 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8604 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8606 /* Set the input location to the most specialized template definition.
8607 This is needed if tsubsting causes an error. */
8608 typedecl
= TYPE_MAIN_DECL (pattern
);
8609 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8610 DECL_SOURCE_LOCATION (typedecl
);
8612 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8613 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8614 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8615 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8616 if (ANON_AGGR_TYPE_P (pattern
))
8617 SET_ANON_AGGR_TYPE_P (type
);
8618 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8620 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8621 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8622 /* Adjust visibility for template arguments. */
8623 determine_visibility (TYPE_MAIN_DECL (type
));
8625 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8627 pbinfo
= TYPE_BINFO (pattern
);
8629 /* We should never instantiate a nested class before its enclosing
8630 class; we need to look up the nested class by name before we can
8631 instantiate it, and that lookup should instantiate the enclosing
8633 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8634 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8636 base_list
= NULL_TREE
;
8637 if (BINFO_N_BASE_BINFOS (pbinfo
))
8643 /* We must enter the scope containing the type, as that is where
8644 the accessibility of types named in dependent bases are
8646 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8648 /* Substitute into each of the bases to determine the actual
8650 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8653 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8654 tree expanded_bases
= NULL_TREE
;
8657 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8660 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8661 args
, tf_error
, NULL_TREE
);
8662 if (expanded_bases
== error_mark_node
)
8665 len
= TREE_VEC_LENGTH (expanded_bases
);
8668 for (idx
= 0; idx
< len
; idx
++)
8671 /* Extract the already-expanded base class. */
8672 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8674 /* Substitute to figure out the base class. */
8675 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8678 if (base
== error_mark_node
)
8681 base_list
= tree_cons (access
, base
, base_list
);
8682 if (BINFO_VIRTUAL_P (pbase_binfo
))
8683 TREE_TYPE (base_list
) = integer_type_node
;
8687 /* The list is now in reverse order; correct that. */
8688 base_list
= nreverse (base_list
);
8691 pop_scope (pushed_scope
);
8693 /* Now call xref_basetypes to set up all the base-class
8695 xref_basetypes (type
, base_list
);
8697 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8698 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8699 args
, tf_error
, NULL_TREE
);
8700 fixup_attribute_variants (type
);
8702 /* Now that our base classes are set up, enter the scope of the
8703 class, so that name lookups into base classes, etc. will work
8704 correctly. This is precisely analogous to what we do in
8705 begin_class_definition when defining an ordinary non-template
8706 class, except we also need to push the enclosing classes. */
8707 push_nested_class (type
);
8709 /* Now members are processed in the order of declaration. */
8710 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8711 member
; member
= TREE_CHAIN (member
))
8713 tree t
= TREE_VALUE (member
);
8715 if (TREE_PURPOSE (member
))
8719 /* Build new CLASSTYPE_NESTED_UTDS. */
8722 bool class_template_p
;
8724 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8725 && TYPE_LANG_SPECIFIC (t
)
8726 && CLASSTYPE_IS_TEMPLATE (t
));
8727 /* If the member is a class template, then -- even after
8728 substitution -- there may be dependent types in the
8729 template argument list for the class. We increment
8730 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8731 that function will assume that no types are dependent
8732 when outside of a template. */
8733 if (class_template_p
)
8734 ++processing_template_decl
;
8735 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8736 if (class_template_p
)
8737 --processing_template_decl
;
8738 if (newtag
== error_mark_node
)
8741 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8743 tree name
= TYPE_IDENTIFIER (t
);
8745 if (class_template_p
)
8746 /* Unfortunately, lookup_template_class sets
8747 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8748 instantiation (i.e., for the type of a member
8749 template class nested within a template class.)
8750 This behavior is required for
8751 maybe_process_partial_specialization to work
8752 correctly, but is not accurate in this case;
8753 the TAG is not an instantiation of anything.
8754 (The corresponding TEMPLATE_DECL is an
8755 instantiation, but the TYPE is not.) */
8756 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8758 /* Now, we call pushtag to put this NEWTAG into the scope of
8759 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8760 pushtag calling push_template_decl. We don't have to do
8761 this for enums because it will already have been done in
8764 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8765 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8768 else if (TREE_CODE (t
) == FUNCTION_DECL
8769 || DECL_FUNCTION_TEMPLATE_P (t
))
8771 /* Build new TYPE_METHODS. */
8774 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8775 ++processing_template_decl
;
8776 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8777 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8778 --processing_template_decl
;
8779 set_current_access_from_decl (r
);
8780 finish_member_declaration (r
);
8781 /* Instantiate members marked with attribute used. */
8782 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8787 /* Build new TYPE_FIELDS. */
8788 if (TREE_CODE (t
) == STATIC_ASSERT
)
8792 ++c_inhibit_evaluation_warnings
;
8794 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8795 tf_warning_or_error
, NULL_TREE
,
8796 /*integral_constant_expression_p=*/true);
8797 --c_inhibit_evaluation_warnings
;
8799 finish_static_assert (condition
,
8800 STATIC_ASSERT_MESSAGE (t
),
8801 STATIC_ASSERT_SOURCE_LOCATION (t
),
8804 else if (TREE_CODE (t
) != CONST_DECL
)
8808 /* The file and line for this declaration, to
8809 assist in error message reporting. Since we
8810 called push_tinst_level above, we don't need to
8812 input_location
= DECL_SOURCE_LOCATION (t
);
8814 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8815 ++processing_template_decl
;
8816 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8817 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8818 --processing_template_decl
;
8819 if (TREE_CODE (r
) == VAR_DECL
)
8823 [t]he initialization (and any associated
8824 side-effects) of a static data member does
8825 not occur unless the static data member is
8826 itself used in a way that requires the
8827 definition of the static data member to
8830 Therefore, we do not substitute into the
8831 initialized for the static data member here. */
8832 finish_static_data_member_decl
8835 /*init_const_expr_p=*/false,
8836 /*asmspec_tree=*/NULL_TREE
,
8838 /* Instantiate members marked with attribute used. */
8839 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8842 else if (TREE_CODE (r
) == FIELD_DECL
)
8844 /* Determine whether R has a valid type and can be
8845 completed later. If R is invalid, then it is
8846 replaced by error_mark_node so that it will not be
8847 added to TYPE_FIELDS. */
8848 tree rtype
= TREE_TYPE (r
);
8849 if (can_complete_type_without_circularity (rtype
))
8850 complete_type (rtype
);
8852 if (!COMPLETE_TYPE_P (rtype
))
8854 cxx_incomplete_type_error (r
, rtype
);
8855 r
= error_mark_node
;
8859 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8860 such a thing will already have been added to the field
8861 list by tsubst_enum in finish_member_declaration in the
8862 CLASSTYPE_NESTED_UTDS case above. */
8863 if (!(TREE_CODE (r
) == TYPE_DECL
8864 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8865 && DECL_ARTIFICIAL (r
)))
8867 set_current_access_from_decl (r
);
8868 finish_member_declaration (r
);
8875 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8876 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8878 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8880 tree friend_type
= t
;
8881 bool adjust_processing_template_decl
= false;
8883 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8885 /* template <class T> friend class C; */
8886 friend_type
= tsubst_friend_class (friend_type
, args
);
8887 adjust_processing_template_decl
= true;
8889 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8891 /* template <class T> friend class C::D; */
8892 friend_type
= tsubst (friend_type
, args
,
8893 tf_warning_or_error
, NULL_TREE
);
8894 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8895 friend_type
= TREE_TYPE (friend_type
);
8896 adjust_processing_template_decl
= true;
8898 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8899 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8901 /* This could be either
8905 when dependent_type_p is false or
8907 template <class U> friend class T::C;
8910 friend_type
= tsubst (friend_type
, args
,
8911 tf_warning_or_error
, NULL_TREE
);
8912 /* Bump processing_template_decl for correct
8913 dependent_type_p calculation. */
8914 ++processing_template_decl
;
8915 if (dependent_type_p (friend_type
))
8916 adjust_processing_template_decl
= true;
8917 --processing_template_decl
;
8919 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8920 && hidden_name_p (TYPE_NAME (friend_type
)))
8924 where C hasn't been declared yet. Let's lookup name
8925 from namespace scope directly, bypassing any name that
8926 come from dependent base class. */
8927 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8929 /* The call to xref_tag_from_type does injection for friend
8931 push_nested_namespace (ns
);
8933 xref_tag_from_type (friend_type
, NULL_TREE
,
8934 /*tag_scope=*/ts_current
);
8935 pop_nested_namespace (ns
);
8937 else if (uses_template_parms (friend_type
))
8938 /* friend class C<T>; */
8939 friend_type
= tsubst (friend_type
, args
,
8940 tf_warning_or_error
, NULL_TREE
);
8945 where C is already declared or
8947 friend class C<int>;
8949 We don't have to do anything in these cases. */
8951 if (adjust_processing_template_decl
)
8952 /* Trick make_friend_class into realizing that the friend
8953 we're adding is a template, not an ordinary class. It's
8954 important that we use make_friend_class since it will
8955 perform some error-checking and output cross-reference
8957 ++processing_template_decl
;
8959 if (friend_type
!= error_mark_node
)
8960 make_friend_class (type
, friend_type
, /*complain=*/false);
8962 if (adjust_processing_template_decl
)
8963 --processing_template_decl
;
8967 /* Build new DECL_FRIENDLIST. */
8970 /* The file and line for this declaration, to
8971 assist in error message reporting. Since we
8972 called push_tinst_level above, we don't need to
8974 input_location
= DECL_SOURCE_LOCATION (t
);
8976 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8978 ++processing_template_decl
;
8979 push_deferring_access_checks (dk_no_check
);
8982 r
= tsubst_friend_function (t
, args
);
8983 add_friend (type
, r
, /*complain=*/false);
8984 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8986 pop_deferring_access_checks ();
8987 --processing_template_decl
;
8993 if (CLASSTYPE_LAMBDA_EXPR (type
))
8995 tree decl
= lambda_function (type
);
8998 instantiate_decl (decl
, false, false);
8999 maybe_add_lambda_conv_op (type
);
9002 gcc_assert (errorcount
);
9005 /* Set the file and line number information to whatever is given for
9006 the class itself. This puts error messages involving generated
9007 implicit functions at a predictable point, and the same point
9008 that would be used for non-template classes. */
9009 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9011 unreverse_member_declarations (type
);
9012 finish_struct_1 (type
);
9013 TYPE_BEING_DEFINED (type
) = 0;
9015 /* We don't instantiate default arguments for member functions. 14.7.1:
9017 The implicit instantiation of a class template specialization causes
9018 the implicit instantiation of the declarations, but not of the
9019 definitions or default arguments, of the class member functions,
9020 member classes, static data members and member templates.... */
9022 /* Some typedefs referenced from within the template code need to be access
9023 checked at template instantiation time, i.e now. These types were
9024 added to the template at parsing time. Let's get those and perform
9025 the access checks then. */
9026 perform_typedefs_access_check (pattern
, args
);
9027 perform_deferred_access_checks (tf_warning_or_error
);
9028 pop_nested_class ();
9029 maximum_field_alignment
= saved_maximum_field_alignment
;
9031 pop_from_top_level ();
9032 pop_deferring_access_checks ();
9035 /* The vtable for a template class can be emitted in any translation
9036 unit in which the class is instantiated. When there is no key
9037 method, however, finish_struct_1 will already have added TYPE to
9038 the keyed_classes list. */
9039 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9040 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9045 /* Wrapper for instantiate_class_template_1. */
9048 instantiate_class_template (tree type
)
9051 timevar_push (TV_TEMPLATE_INST
);
9052 ret
= instantiate_class_template_1 (type
);
9053 timevar_pop (TV_TEMPLATE_INST
);
9058 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9064 else if (TYPE_P (t
))
9065 r
= tsubst (t
, args
, complain
, in_decl
);
9068 if (!(complain
& tf_warning
))
9069 ++c_inhibit_evaluation_warnings
;
9070 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9071 /*integral_constant_expression_p=*/true);
9072 if (!(complain
& tf_warning
))
9073 --c_inhibit_evaluation_warnings
;
9074 /* Preserve the raw-reference nature of T. */
9075 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9076 && REFERENCE_REF_P (r
))
9077 r
= TREE_OPERAND (r
, 0);
9082 /* Given a function parameter pack TMPL_PARM and some function parameters
9083 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9084 and set *SPEC_P to point at the next point in the list. */
9087 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9089 /* Collect all of the extra "packed" parameters into an
9093 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9094 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9095 tree spec_parm
= *spec_p
;
9098 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9100 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9103 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9104 parmvec
= make_tree_vec (len
);
9105 parmtypevec
= make_tree_vec (len
);
9106 spec_parm
= *spec_p
;
9107 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9109 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9110 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9113 /* Build the argument packs. */
9114 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9115 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9116 TREE_TYPE (argpack
) = argtypepack
;
9117 *spec_p
= spec_parm
;
9122 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9123 NONTYPE_ARGUMENT_PACK. */
9126 make_fnparm_pack (tree spec_parm
)
9128 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9131 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9135 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9137 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9138 if (i
>= TREE_VEC_LENGTH (vec
))
9140 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9144 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9147 make_argument_pack_select (tree arg_pack
, unsigned index
)
9149 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9151 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9152 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9157 /* This is a subroutine of tsubst_pack_expansion.
9159 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9160 mechanism to store the (non complete list of) arguments of the
9161 substitution and return a non substituted pack expansion, in order
9162 to wait for when we have enough arguments to really perform the
9166 use_pack_expansion_extra_args_p (tree parm_packs
,
9170 if (parm_packs
== NULL_TREE
)
9173 bool has_expansion_arg
= false;
9174 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9176 bool has_non_expansion_arg
= false;
9177 for (tree parm_pack
= parm_packs
;
9179 parm_pack
= TREE_CHAIN (parm_pack
))
9181 tree arg
= TREE_VALUE (parm_pack
);
9183 if (argument_pack_element_is_expansion_p (arg
, i
))
9184 has_expansion_arg
= true;
9186 has_non_expansion_arg
= true;
9189 /* If one pack has an expansion and another pack has a normal
9190 argument or if one pack has an empty argument another one
9191 hasn't then tsubst_pack_expansion cannot perform the
9192 substitution and need to fall back on the
9193 PACK_EXPANSION_EXTRA mechanism. */
9194 if ((has_expansion_arg
&& has_non_expansion_arg
)
9195 || (has_empty_arg
&& (has_expansion_arg
|| has_non_expansion_arg
)))
9201 /* [temp.variadic]/6 says that:
9203 The instantiation of a pack expansion [...]
9204 produces a list E1,E2, ..., En, where N is the number of elements
9205 in the pack expansion parameters.
9207 This subroutine of tsubst_pack_expansion produces one of these Ei.
9209 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9210 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9211 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9212 INDEX is the index 'i' of the element Ei to produce. ARGS,
9213 COMPLAIN, and IN_DECL are the same parameters as for the
9214 tsubst_pack_expansion function.
9216 The function returns the resulting Ei upon successful completion,
9219 Note that this function possibly modifies the ARGS parameter, so
9220 it's the responsibility of the caller to restore it. */
9223 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9226 tree args
/* This parm gets
9228 tsubst_flags_t complain
,
9232 bool ith_elem_is_expansion
= false;
9234 /* For each parameter pack, change the substitution of the parameter
9235 pack to the ith argument in its argument pack, then expand the
9237 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9239 tree parm
= TREE_PURPOSE (pack
);
9240 tree arg_pack
= TREE_VALUE (pack
);
9241 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9243 ith_elem_is_expansion
|=
9244 argument_pack_element_is_expansion_p (arg_pack
, index
);
9246 /* Select the Ith argument from the pack. */
9247 if (TREE_CODE (parm
) == PARM_DECL
)
9251 aps
= make_argument_pack_select (arg_pack
, index
);
9253 register_local_specialization (aps
, parm
);
9256 aps
= retrieve_local_specialization (parm
);
9261 template_parm_level_and_index (parm
, &level
, &idx
);
9265 aps
= make_argument_pack_select (arg_pack
, index
);
9266 /* Update the corresponding argument. */
9267 TMPL_ARG (args
, level
, idx
) = aps
;
9270 /* Re-use the ARGUMENT_PACK_SELECT. */
9271 aps
= TMPL_ARG (args
, level
, idx
);
9273 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9276 /* Substitute into the PATTERN with the (possibly altered)
9278 if (!TYPE_P (pattern
))
9279 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9280 /*integral_constant_expression_p=*/false);
9282 t
= tsubst (pattern
, args
, complain
, in_decl
);
9284 /* If the Ith argument pack element is a pack expansion, then
9285 the Ith element resulting from the substituting is going to
9286 be a pack expansion as well. */
9287 if (ith_elem_is_expansion
)
9288 t
= make_pack_expansion (t
);
9293 /* Substitute ARGS into T, which is an pack expansion
9294 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9295 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9296 (if only a partial substitution could be performed) or
9297 ERROR_MARK_NODE if there was an error. */
9299 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9303 tree pack
, packs
= NULL_TREE
;
9304 bool unsubstituted_packs
= false;
9307 struct pointer_map_t
*saved_local_specializations
= NULL
;
9308 bool need_local_specializations
= false;
9311 gcc_assert (PACK_EXPANSION_P (t
));
9312 pattern
= PACK_EXPANSION_PATTERN (t
);
9314 /* Add in any args remembered from an earlier partial instantiation. */
9315 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9317 levels
= TMPL_ARGS_DEPTH (args
);
9319 /* Determine the argument packs that will instantiate the parameter
9320 packs used in the expansion expression. While we're at it,
9321 compute the number of arguments to be expanded and make sure it
9323 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9324 pack
= TREE_CHAIN (pack
))
9326 tree parm_pack
= TREE_VALUE (pack
);
9327 tree arg_pack
= NULL_TREE
;
9328 tree orig_arg
= NULL_TREE
;
9331 if (TREE_CODE (parm_pack
) == BASES
)
9333 if (BASES_DIRECT (parm_pack
))
9334 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9335 args
, complain
, in_decl
, false));
9337 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9338 args
, complain
, in_decl
, false));
9340 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9342 if (PACK_EXPANSION_LOCAL_P (t
))
9343 arg_pack
= retrieve_local_specialization (parm_pack
);
9346 /* We can't rely on local_specializations for a parameter
9347 name used later in a function declaration (such as in a
9348 late-specified return type). Even if it exists, it might
9349 have the wrong value for a recursive call. Just make a
9350 dummy decl, since it's only used for its type. */
9351 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9352 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9353 /* Partial instantiation of the parm_pack, we can't build
9354 up an argument pack yet. */
9355 arg_pack
= NULL_TREE
;
9357 arg_pack
= make_fnparm_pack (arg_pack
);
9358 need_local_specializations
= true;
9364 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9366 if (level
<= levels
)
9367 arg_pack
= TMPL_ARG (args
, level
, idx
);
9370 orig_arg
= arg_pack
;
9371 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9372 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9374 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9375 /* This can only happen if we forget to expand an argument
9376 pack somewhere else. Just return an error, silently. */
9378 result
= make_tree_vec (1);
9379 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9386 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9388 /* Don't bother trying to do a partial substitution with
9389 incomplete packs; we'll try again after deduction. */
9390 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9395 else if (len
!= my_len
)
9397 if (!(complain
& tf_error
))
9398 /* Fail quietly. */;
9399 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9400 error ("mismatched argument pack lengths while expanding "
9404 error ("mismatched argument pack lengths while expanding "
9407 return error_mark_node
;
9410 /* Keep track of the parameter packs and their corresponding
9412 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9413 TREE_TYPE (packs
) = orig_arg
;
9417 /* We can't substitute for this parameter pack. We use a flag as
9418 well as the missing_level counter because function parameter
9419 packs don't have a level. */
9420 unsubstituted_packs
= true;
9424 /* We cannot expand this expansion expression, because we don't have
9425 all of the argument packs we need. */
9426 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9428 /* We got some full packs, but we can't substitute them in until we
9429 have values for all the packs. So remember these until then. */
9431 t
= make_pack_expansion (pattern
);
9432 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9435 else if (unsubstituted_packs
)
9437 /* There were no real arguments, we're just replacing a parameter
9438 pack with another version of itself. Substitute into the
9439 pattern and return a PACK_EXPANSION_*. The caller will need to
9441 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9442 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9443 /*integral_constant_expression_p=*/false);
9445 t
= tsubst (pattern
, args
, complain
, in_decl
);
9446 t
= make_pack_expansion (t
);
9450 gcc_assert (len
>= 0);
9452 if (need_local_specializations
)
9454 /* We're in a late-specified return type, so create our own local
9455 specializations map; the current map is either NULL or (in the
9456 case of recursive unification) might have bindings that we don't
9457 want to use or alter. */
9458 saved_local_specializations
= local_specializations
;
9459 local_specializations
= pointer_map_create ();
9462 /* For each argument in each argument pack, substitute into the
9464 result
= make_tree_vec (len
);
9465 for (i
= 0; i
< len
; ++i
)
9467 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9471 TREE_VEC_ELT (result
, i
) = t
;
9472 if (t
== error_mark_node
)
9474 result
= error_mark_node
;
9479 /* Update ARGS to restore the substitution from parameter packs to
9480 their argument packs. */
9481 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9483 tree parm
= TREE_PURPOSE (pack
);
9485 if (TREE_CODE (parm
) == PARM_DECL
)
9486 register_local_specialization (TREE_TYPE (pack
), parm
);
9491 if (TREE_VALUE (pack
) == NULL_TREE
)
9494 template_parm_level_and_index (parm
, &level
, &idx
);
9496 /* Update the corresponding argument. */
9497 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9498 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9501 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9505 if (need_local_specializations
)
9507 pointer_map_destroy (local_specializations
);
9508 local_specializations
= saved_local_specializations
;
9514 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9515 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9516 parameter packs; all parms generated from a function parameter pack will
9517 have the same DECL_PARM_INDEX. */
9520 get_pattern_parm (tree parm
, tree tmpl
)
9522 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9525 if (DECL_ARTIFICIAL (parm
))
9527 for (patparm
= DECL_ARGUMENTS (pattern
);
9528 patparm
; patparm
= DECL_CHAIN (patparm
))
9529 if (DECL_ARTIFICIAL (patparm
)
9530 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9535 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9536 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9537 gcc_assert (DECL_PARM_INDEX (patparm
)
9538 == DECL_PARM_INDEX (parm
));
9544 /* Substitute ARGS into the vector or list of template arguments T. */
9547 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9550 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9553 if (t
== error_mark_node
)
9554 return error_mark_node
;
9556 len
= TREE_VEC_LENGTH (t
);
9557 elts
= XALLOCAVEC (tree
, len
);
9559 for (i
= 0; i
< len
; i
++)
9561 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9564 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9565 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9566 else if (PACK_EXPANSION_P (orig_arg
))
9568 /* Substitute into an expansion expression. */
9569 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9571 if (TREE_CODE (new_arg
) == TREE_VEC
)
9572 /* Add to the expanded length adjustment the number of
9573 expanded arguments. We subtract one from this
9574 measurement, because the argument pack expression
9575 itself is already counted as 1 in
9576 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9577 the argument pack is empty. */
9578 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9580 else if (ARGUMENT_PACK_P (orig_arg
))
9582 /* Substitute into each of the arguments. */
9583 new_arg
= TYPE_P (orig_arg
)
9584 ? cxx_make_type (TREE_CODE (orig_arg
))
9585 : make_node (TREE_CODE (orig_arg
));
9587 SET_ARGUMENT_PACK_ARGS (
9589 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9590 args
, complain
, in_decl
));
9592 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9593 new_arg
= error_mark_node
;
9595 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9596 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9598 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9600 if (TREE_TYPE (new_arg
) == error_mark_node
)
9601 new_arg
= error_mark_node
;
9605 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9607 if (new_arg
== error_mark_node
)
9608 return error_mark_node
;
9611 if (new_arg
!= orig_arg
)
9618 /* Make space for the expanded arguments coming from template
9620 t
= make_tree_vec (len
+ expanded_len_adjust
);
9621 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9622 arguments for a member template.
9623 In that case each TREE_VEC in ORIG_T represents a level of template
9624 arguments, and ORIG_T won't carry any non defaulted argument count.
9625 It will rather be the nested TREE_VECs that will carry one.
9626 In other words, ORIG_T carries a non defaulted argument count only
9627 if it doesn't contain any nested TREE_VEC. */
9628 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9630 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9631 count
+= expanded_len_adjust
;
9632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9634 for (i
= 0, out
= 0; i
< len
; i
++)
9636 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9637 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9638 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9642 /* Now expand the template argument pack "in place". */
9643 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9644 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9648 TREE_VEC_ELT (t
, out
) = elts
[i
];
9656 /* Return the result of substituting ARGS into the template parameters
9657 given by PARMS. If there are m levels of ARGS and m + n levels of
9658 PARMS, then the result will contain n levels of PARMS. For
9659 example, if PARMS is `template <class T> template <class U>
9660 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9661 result will be `template <int*, double, class V>'. */
9664 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9669 /* When substituting into a template, we must set
9670 PROCESSING_TEMPLATE_DECL as the template parameters may be
9671 dependent if they are based on one-another, and the dependency
9672 predicates are short-circuit outside of templates. */
9673 ++processing_template_decl
;
9675 for (new_parms
= &r
;
9676 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9677 new_parms
= &(TREE_CHAIN (*new_parms
)),
9678 parms
= TREE_CHAIN (parms
))
9681 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9684 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9688 if (parms
== error_mark_node
)
9691 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9693 if (tuple
== error_mark_node
)
9696 TREE_VEC_ELT (new_vec
, i
) =
9697 tsubst_template_parm (tuple
, args
, complain
);
9701 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9702 - TMPL_ARGS_DEPTH (args
)),
9703 new_vec
, NULL_TREE
);
9706 --processing_template_decl
;
9711 /* Return the result of substituting ARGS into one template parameter
9712 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9713 parameter and which TREE_PURPOSE is the default argument of the
9714 template parameter. */
9717 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9719 tree default_value
, parm_decl
;
9721 if (args
== NULL_TREE
9723 || t
== error_mark_node
)
9726 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9728 default_value
= TREE_PURPOSE (t
);
9729 parm_decl
= TREE_VALUE (t
);
9731 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9732 if (TREE_CODE (parm_decl
) == PARM_DECL
9733 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9734 parm_decl
= error_mark_node
;
9735 default_value
= tsubst_template_arg (default_value
, args
,
9736 complain
, NULL_TREE
);
9738 return build_tree_list (default_value
, parm_decl
);
9741 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9742 type T. If T is not an aggregate or enumeration type, it is
9743 handled as if by tsubst. IN_DECL is as for tsubst. If
9744 ENTERING_SCOPE is nonzero, T is the context for a template which
9745 we are presently tsubst'ing. Return the substituted value. */
9748 tsubst_aggr_type (tree t
,
9750 tsubst_flags_t complain
,
9757 switch (TREE_CODE (t
))
9760 if (TYPE_PTRMEMFUNC_P (t
))
9761 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9763 /* Else fall through. */
9766 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9771 int saved_unevaluated_operand
;
9772 int saved_inhibit_evaluation_warnings
;
9774 /* In "sizeof(X<I>)" we need to evaluate "I". */
9775 saved_unevaluated_operand
= cp_unevaluated_operand
;
9776 cp_unevaluated_operand
= 0;
9777 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9778 c_inhibit_evaluation_warnings
= 0;
9780 /* First, determine the context for the type we are looking
9782 context
= TYPE_CONTEXT (t
);
9783 if (context
&& TYPE_P (context
))
9785 context
= tsubst_aggr_type (context
, args
, complain
,
9786 in_decl
, /*entering_scope=*/1);
9787 /* If context is a nested class inside a class template,
9788 it may still need to be instantiated (c++/33959). */
9789 context
= complete_type (context
);
9792 /* Then, figure out what arguments are appropriate for the
9793 type we are trying to find. For example, given:
9795 template <class T> struct S;
9796 template <class T, class U> void f(T, U) { S<U> su; }
9798 and supposing that we are instantiating f<int, double>,
9799 then our ARGS will be {int, double}, but, when looking up
9800 S we only want {double}. */
9801 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9803 if (argvec
== error_mark_node
)
9804 r
= error_mark_node
;
9807 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9808 entering_scope
, complain
);
9809 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9812 cp_unevaluated_operand
= saved_unevaluated_operand
;
9813 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9818 /* This is not a template type, so there's nothing to do. */
9822 return tsubst (t
, args
, complain
, in_decl
);
9826 /* Substitute into the default argument ARG (a default argument for
9827 FN), which has the indicated TYPE. */
9830 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9832 tree saved_class_ptr
= NULL_TREE
;
9833 tree saved_class_ref
= NULL_TREE
;
9834 int errs
= errorcount
+ sorrycount
;
9836 /* This can happen in invalid code. */
9837 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9840 /* This default argument came from a template. Instantiate the
9841 default argument here, not in tsubst. In the case of
9850 we must be careful to do name lookup in the scope of S<T>,
9851 rather than in the current class. */
9852 push_access_scope (fn
);
9853 /* The "this" pointer is not valid in a default argument. */
9856 saved_class_ptr
= current_class_ptr
;
9857 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9858 saved_class_ref
= current_class_ref
;
9859 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9862 push_deferring_access_checks(dk_no_deferred
);
9863 /* The default argument expression may cause implicitly defined
9864 member functions to be synthesized, which will result in garbage
9865 collection. We must treat this situation as if we were within
9866 the body of function so as to avoid collecting live data on the
9869 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9870 tf_warning_or_error
, NULL_TREE
,
9871 /*integral_constant_expression_p=*/false);
9873 pop_deferring_access_checks();
9875 /* Restore the "this" pointer. */
9878 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9879 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9882 if (errorcount
+sorrycount
> errs
)
9883 inform (input_location
,
9884 " when instantiating default argument for call to %D", fn
);
9886 /* Make sure the default argument is reasonable. */
9887 arg
= check_default_argument (type
, arg
);
9889 pop_access_scope (fn
);
9894 /* Substitute into all the default arguments for FN. */
9897 tsubst_default_arguments (tree fn
)
9902 tmpl_args
= DECL_TI_ARGS (fn
);
9904 /* If this function is not yet instantiated, we certainly don't need
9905 its default arguments. */
9906 if (uses_template_parms (tmpl_args
))
9908 /* Don't do this again for clones. */
9909 if (DECL_CLONED_FUNCTION_P (fn
))
9912 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9914 arg
= TREE_CHAIN (arg
))
9915 if (TREE_PURPOSE (arg
))
9916 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9918 TREE_PURPOSE (arg
));
9921 /* Substitute the ARGS into the T, which is a _DECL. Return the
9922 result of the substitution. Issue error and warning messages under
9923 control of COMPLAIN. */
9926 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9928 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9929 location_t saved_loc
;
9934 /* Set the filename and linenumber to improve error-reporting. */
9935 saved_loc
= input_location
;
9936 input_location
= DECL_SOURCE_LOCATION (t
);
9938 switch (TREE_CODE (t
))
9942 /* We can get here when processing a member function template,
9943 member class template, or template template parameter. */
9944 tree decl
= DECL_TEMPLATE_RESULT (t
);
9949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9951 /* Template template parameter is treated here. */
9952 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9953 if (new_type
== error_mark_node
)
9954 RETURN (error_mark_node
);
9955 /* If we get a real template back, return it. This can happen in
9956 the context of most_specialized_class. */
9957 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
9961 DECL_CHAIN (r
) = NULL_TREE
;
9962 TREE_TYPE (r
) = new_type
;
9963 DECL_TEMPLATE_RESULT (r
)
9964 = build_decl (DECL_SOURCE_LOCATION (decl
),
9965 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9966 DECL_TEMPLATE_PARMS (r
)
9967 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9969 TYPE_NAME (new_type
) = r
;
9973 /* We might already have an instance of this template.
9974 The ARGS are for the surrounding class type, so the
9975 full args contain the tsubst'd args for the context,
9976 plus the innermost args from the template decl. */
9977 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9978 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9979 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9980 /* Because this is a template, the arguments will still be
9981 dependent, even after substitution. If
9982 PROCESSING_TEMPLATE_DECL is not set, the dependency
9983 predicates will short-circuit. */
9984 ++processing_template_decl
;
9985 full_args
= tsubst_template_args (tmpl_args
, args
,
9987 --processing_template_decl
;
9988 if (full_args
== error_mark_node
)
9989 RETURN (error_mark_node
);
9991 /* If this is a default template template argument,
9992 tsubst might not have changed anything. */
9993 if (full_args
== tmpl_args
)
9996 hash
= hash_tmpl_and_args (t
, full_args
);
9997 spec
= retrieve_specialization (t
, full_args
, hash
);
9998 if (spec
!= NULL_TREE
)
10004 /* Make a new template decl. It will be similar to the
10005 original, but will record the current template arguments.
10006 We also create a new function declaration, which is just
10007 like the old one, but points to this new template, rather
10008 than the old one. */
10010 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10011 DECL_CHAIN (r
) = NULL_TREE
;
10013 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10015 if (TREE_CODE (decl
) == TYPE_DECL
10016 && !TYPE_DECL_ALIAS_P (decl
))
10019 ++processing_template_decl
;
10020 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10021 --processing_template_decl
;
10022 if (new_type
== error_mark_node
)
10023 RETURN (error_mark_node
);
10025 TREE_TYPE (r
) = new_type
;
10026 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10027 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10028 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10029 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10034 ++processing_template_decl
;
10035 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10036 --processing_template_decl
;
10037 if (new_decl
== error_mark_node
)
10038 RETURN (error_mark_node
);
10040 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10041 DECL_TI_TEMPLATE (new_decl
) = r
;
10042 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10043 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10044 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10047 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10048 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10049 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10051 /* The template parameters for this new template are all the
10052 template parameters for the old template, except the
10053 outermost level of parameters. */
10054 DECL_TEMPLATE_PARMS (r
)
10055 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10058 if (PRIMARY_TEMPLATE_P (t
))
10059 DECL_PRIMARY_TEMPLATE (r
) = r
;
10061 if (TREE_CODE (decl
) != TYPE_DECL
)
10062 /* Record this non-type partial instantiation. */
10063 register_specialization (r
, t
,
10064 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10069 case FUNCTION_DECL
:
10072 tree argvec
= NULL_TREE
;
10080 /* Nobody should be tsubst'ing into non-template functions. */
10081 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10083 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10088 /* If T is not dependent, just return it. We have to
10089 increment PROCESSING_TEMPLATE_DECL because
10090 value_dependent_expression_p assumes that nothing is
10091 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10092 ++processing_template_decl
;
10093 dependent_p
= value_dependent_expression_p (t
);
10094 --processing_template_decl
;
10098 /* Calculate the most general template of which R is a
10099 specialization, and the complete set of arguments used to
10101 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10102 argvec
= tsubst_template_args (DECL_TI_ARGS
10103 (DECL_TEMPLATE_RESULT
10104 (DECL_TI_TEMPLATE (t
))),
10105 args
, complain
, in_decl
);
10106 if (argvec
== error_mark_node
)
10107 RETURN (error_mark_node
);
10109 /* Check to see if we already have this specialization. */
10110 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10111 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10119 /* We can see more levels of arguments than parameters if
10120 there was a specialization of a member template, like
10123 template <class T> struct S { template <class U> void f(); }
10124 template <> template <class U> void S<int>::f(U);
10126 Here, we'll be substituting into the specialization,
10127 because that's where we can find the code we actually
10128 want to generate, but we'll have enough arguments for
10129 the most general template.
10131 We also deal with the peculiar case:
10133 template <class T> struct S {
10134 template <class U> friend void f();
10136 template <class U> void f() {}
10138 template void f<double>();
10140 Here, the ARGS for the instantiation of will be {int,
10141 double}. But, we only need as many ARGS as there are
10142 levels of template parameters in CODE_PATTERN. We are
10143 careful not to get fooled into reducing the ARGS in
10146 template <class T> struct S { template <class U> void f(U); }
10147 template <class T> template <> void S<T>::f(int) {}
10149 which we can spot because the pattern will be a
10150 specialization in this case. */
10151 args_depth
= TMPL_ARGS_DEPTH (args
);
10153 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10154 if (args_depth
> parms_depth
10155 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10156 args
= get_innermost_template_args (args
, parms_depth
);
10160 /* This special case arises when we have something like this:
10162 template <class T> struct S {
10163 friend void f<int>(int, double);
10166 Here, the DECL_TI_TEMPLATE for the friend declaration
10167 will be an IDENTIFIER_NODE. We are being called from
10168 tsubst_friend_function, and we want only to create a
10169 new decl (R) with appropriate types so that we can call
10170 determine_specialization. */
10171 gen_tmpl
= NULL_TREE
;
10174 if (DECL_CLASS_SCOPE_P (t
))
10176 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10180 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10181 complain
, t
, /*entering_scope=*/1);
10186 ctx
= DECL_CONTEXT (t
);
10188 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10189 if (type
== error_mark_node
)
10190 RETURN (error_mark_node
);
10192 /* If we hit excessive deduction depth, the type is bogus even if
10193 it isn't error_mark_node, so don't build a decl. */
10194 if (excessive_deduction_depth
)
10195 RETURN (error_mark_node
);
10197 /* We do NOT check for matching decls pushed separately at this
10198 point, as they may not represent instantiations of this
10199 template, and in any case are considered separate under the
10202 DECL_USE_TEMPLATE (r
) = 0;
10203 TREE_TYPE (r
) = type
;
10204 /* Clear out the mangled name and RTL for the instantiation. */
10205 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10206 SET_DECL_RTL (r
, NULL
);
10207 /* Leave DECL_INITIAL set on deleted instantiations. */
10208 if (!DECL_DELETED_FN (r
))
10209 DECL_INITIAL (r
) = NULL_TREE
;
10210 DECL_CONTEXT (r
) = ctx
;
10212 if (member
&& DECL_CONV_FN_P (r
))
10213 /* Type-conversion operator. Reconstruct the name, in
10214 case it's the name of one of the template's parameters. */
10215 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10217 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10219 DECL_RESULT (r
) = NULL_TREE
;
10221 TREE_STATIC (r
) = 0;
10222 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10223 DECL_EXTERNAL (r
) = 1;
10224 /* If this is an instantiation of a function with internal
10225 linkage, we already know what object file linkage will be
10226 assigned to the instantiation. */
10227 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10228 DECL_DEFER_OUTPUT (r
) = 0;
10229 DECL_CHAIN (r
) = NULL_TREE
;
10230 DECL_PENDING_INLINE_INFO (r
) = 0;
10231 DECL_PENDING_INLINE_P (r
) = 0;
10232 DECL_SAVED_TREE (r
) = NULL_TREE
;
10233 DECL_STRUCT_FUNCTION (r
) = NULL
;
10235 /* We'll re-clone as appropriate in instantiate_template. */
10236 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10238 /* If we aren't complaining now, return on error before we register
10239 the specialization so that we'll complain eventually. */
10240 if ((complain
& tf_error
) == 0
10241 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10242 && !grok_op_properties (r
, /*complain=*/false))
10243 RETURN (error_mark_node
);
10245 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10246 this in the special friend case mentioned above where
10247 GEN_TMPL is NULL. */
10250 DECL_TEMPLATE_INFO (r
)
10251 = build_template_info (gen_tmpl
, argvec
);
10252 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10253 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10255 /* We're not supposed to instantiate default arguments
10256 until they are called, for a template. But, for a
10259 template <class T> void f ()
10260 { extern void g(int i = T()); }
10262 we should do the substitution when the template is
10263 instantiated. We handle the member function case in
10264 instantiate_class_template since the default arguments
10265 might refer to other members of the class. */
10267 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10268 && !uses_template_parms (argvec
))
10269 tsubst_default_arguments (r
);
10272 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10274 /* Copy the list of befriending classes. */
10275 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10277 friends
= &TREE_CHAIN (*friends
))
10279 *friends
= copy_node (*friends
);
10280 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10285 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10287 maybe_retrofit_in_chrg (r
);
10288 if (DECL_CONSTRUCTOR_P (r
))
10289 grok_ctor_properties (ctx
, r
);
10290 if (DECL_INHERITED_CTOR_BASE (r
))
10291 deduce_inheriting_ctor (r
);
10292 /* If this is an instantiation of a member template, clone it.
10293 If it isn't, that'll be handled by
10294 clone_constructors_and_destructors. */
10295 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10296 clone_function_decl (r
, /*update_method_vec_p=*/0);
10298 else if ((complain
& tf_error
) != 0
10299 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10300 && !grok_op_properties (r
, /*complain=*/true))
10301 RETURN (error_mark_node
);
10303 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10304 SET_DECL_FRIEND_CONTEXT (r
,
10305 tsubst (DECL_FRIEND_CONTEXT (t
),
10306 args
, complain
, in_decl
));
10308 /* Possibly limit visibility based on template args. */
10309 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10310 if (DECL_VISIBILITY_SPECIFIED (t
))
10312 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10313 DECL_ATTRIBUTES (r
)
10314 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10316 determine_visibility (r
);
10317 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10318 && !processing_template_decl
)
10319 defaulted_late_check (r
);
10321 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10322 args
, complain
, in_decl
);
10328 tree type
= NULL_TREE
;
10330 tree expanded_types
= NULL_TREE
;
10331 tree prev_r
= NULL_TREE
;
10332 tree first_r
= NULL_TREE
;
10334 if (FUNCTION_PARAMETER_PACK_P (t
))
10336 /* If there is a local specialization that isn't a
10337 parameter pack, it means that we're doing a "simple"
10338 substitution from inside tsubst_pack_expansion. Just
10339 return the local specialization (which will be a single
10341 tree spec
= retrieve_local_specialization (t
);
10343 && TREE_CODE (spec
) == PARM_DECL
10344 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10347 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10348 the parameters in this function parameter pack. */
10349 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10350 complain
, in_decl
);
10351 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10353 len
= TREE_VEC_LENGTH (expanded_types
);
10355 /* Zero-length parameter packs are boring. Just substitute
10358 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10363 /* All we did was update the type. Make a note of that. */
10364 type
= expanded_types
;
10365 expanded_types
= NULL_TREE
;
10369 /* Loop through all of the parameter's we'll build. When T is
10370 a function parameter pack, LEN is the number of expanded
10371 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10373 for (i
= 0; i
< len
; ++i
)
10377 if (DECL_TEMPLATE_PARM_P (t
))
10378 SET_DECL_TEMPLATE_PARM_P (r
);
10380 if (expanded_types
)
10381 /* We're on the Ith parameter of the function parameter
10384 /* An argument of a function parameter pack is not a parameter
10386 FUNCTION_PARAMETER_PACK_P (r
) = false;
10388 /* Get the Ith type. */
10389 type
= TREE_VEC_ELT (expanded_types
, i
);
10391 /* Rename the parameter to include the index. */
10393 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10396 /* We're dealing with a normal parameter. */
10397 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10399 type
= type_decays_to (type
);
10400 TREE_TYPE (r
) = type
;
10401 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10403 if (DECL_INITIAL (r
))
10405 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10406 DECL_INITIAL (r
) = TREE_TYPE (r
);
10408 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10409 complain
, in_decl
);
10412 DECL_CONTEXT (r
) = NULL_TREE
;
10414 if (!DECL_TEMPLATE_PARM_P (r
))
10415 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10417 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10418 args
, complain
, in_decl
);
10420 /* Keep track of the first new parameter we
10421 generate. That's what will be returned to the
10426 /* Build a proper chain of parameters when substituting
10427 into a function parameter pack. */
10429 DECL_CHAIN (prev_r
) = r
;
10432 /* If cp_unevaluated_operand is set, we're just looking for a
10433 single dummy parameter, so don't keep going. */
10434 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10435 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10436 complain
, DECL_CHAIN (t
));
10438 /* FIRST_R contains the start of the chain we've built. */
10448 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10449 if (type
== error_mark_node
)
10450 RETURN (error_mark_node
);
10451 TREE_TYPE (r
) = type
;
10452 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10454 if (DECL_C_BIT_FIELD (r
))
10455 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10456 non-bit-fields DECL_INITIAL is a non-static data member
10457 initializer, which gets deferred instantiation. */
10459 = tsubst_expr (DECL_INITIAL (t
), args
,
10461 /*integral_constant_expression_p=*/true);
10462 else if (DECL_INITIAL (t
))
10464 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10465 NSDMI in perform_member_init. Still set DECL_INITIAL
10466 so that we know there is one. */
10467 DECL_INITIAL (r
) = void_zero_node
;
10468 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10469 retrofit_lang_decl (r
);
10470 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10472 /* We don't have to set DECL_CONTEXT here; it is set by
10473 finish_member_declaration. */
10474 DECL_CHAIN (r
) = NULL_TREE
;
10475 if (VOID_TYPE_P (type
))
10476 error ("instantiation of %q+D as type %qT", r
, type
);
10478 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10479 args
, complain
, in_decl
);
10484 /* We reach here only for member using decls. We also need to check
10485 uses_template_parms because DECL_DEPENDENT_P is not set for a
10486 using-declaration that designates a member of the current
10487 instantiation (c++/53549). */
10488 if (DECL_DEPENDENT_P (t
)
10489 || uses_template_parms (USING_DECL_SCOPE (t
)))
10491 tree scope
= USING_DECL_SCOPE (t
);
10492 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10493 complain
, in_decl
);
10494 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10495 /* Handle 'using T::T'. */
10496 if (TYPE_NAME (scope
)
10497 && name
== TYPE_IDENTIFIER (scope
))
10498 name
= TYPE_IDENTIFIER (inst_scope
);
10499 r
= do_class_using_decl (inst_scope
, name
);
10501 r
= error_mark_node
;
10504 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10505 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10511 DECL_CHAIN (r
) = NULL_TREE
;
10518 tree argvec
= NULL_TREE
;
10519 tree gen_tmpl
= NULL_TREE
;
10521 tree tmpl
= NULL_TREE
;
10523 tree type
= NULL_TREE
;
10526 if (TREE_CODE (t
) == TYPE_DECL
10527 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10529 /* If this is the canonical decl, we don't have to
10530 mess with instantiations, and often we can't (for
10531 typename, template type parms and such). Note that
10532 TYPE_NAME is not correct for the above test if
10533 we've copied the type for a typedef. */
10534 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10535 if (type
== error_mark_node
)
10536 RETURN (error_mark_node
);
10537 r
= TYPE_NAME (type
);
10541 /* Check to see if we already have the specialization we
10544 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10546 /* T is a static data member or namespace-scope entity.
10547 We have to substitute into namespace-scope variables
10548 (even though such entities are never templates) because
10551 template <class T> void f() { extern T t; }
10553 where the entity referenced is not known until
10554 instantiation time. */
10556 ctx
= DECL_CONTEXT (t
);
10557 if (DECL_CLASS_SCOPE_P (t
))
10559 ctx
= tsubst_aggr_type (ctx
, args
,
10561 in_decl
, /*entering_scope=*/1);
10562 /* If CTX is unchanged, then T is in fact the
10563 specialization we want. That situation occurs when
10564 referencing a static data member within in its own
10565 class. We can use pointer equality, rather than
10566 same_type_p, because DECL_CONTEXT is always
10568 if (ctx
== DECL_CONTEXT (t
)
10569 && (TREE_CODE (t
) != TYPE_DECL
10570 /* ... unless T is a member template; in which
10571 case our caller can be willing to create a
10572 specialization of that template represented
10574 || !(DECL_TI_TEMPLATE (t
)
10575 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10581 tmpl
= DECL_TI_TEMPLATE (t
);
10582 gen_tmpl
= most_general_template (tmpl
);
10583 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10584 if (argvec
== error_mark_node
)
10585 RETURN (error_mark_node
);
10586 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10587 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10592 /* A local variable. */
10594 /* Subsequent calls to pushdecl will fill this in. */
10596 spec
= retrieve_local_specialization (t
);
10598 /* If we already have the specialization we need, there is
10599 nothing more to do. */
10606 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10608 /* Just use name lookup to find a member alias for an anonymous
10609 union, but then add it to the hash table. */
10610 r
= lookup_name (DECL_NAME (t
));
10611 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10612 register_local_specialization (r
, t
);
10616 /* Create a new node for the specialization we need. */
10618 if (type
== NULL_TREE
)
10620 if (is_typedef_decl (t
))
10621 type
= DECL_ORIGINAL_TYPE (t
);
10623 type
= TREE_TYPE (t
);
10624 if (TREE_CODE (t
) == VAR_DECL
10625 && VAR_HAD_UNKNOWN_BOUND (t
)
10626 && type
!= error_mark_node
)
10627 type
= strip_array_domain (type
);
10628 type
= tsubst (type
, args
, complain
, in_decl
);
10630 if (TREE_CODE (r
) == VAR_DECL
)
10632 /* Even if the original location is out of scope, the
10633 newly substituted one is not. */
10634 DECL_DEAD_FOR_LOCAL (r
) = 0;
10635 DECL_INITIALIZED_P (r
) = 0;
10636 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10637 if (type
== error_mark_node
)
10638 RETURN (error_mark_node
);
10639 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10641 /* It may seem that this case cannot occur, since:
10646 declares a function, not a variable. However:
10649 template <typename T> void g() { T t; }
10650 template void g<f>();
10652 is an attempt to declare a variable with function
10654 error ("variable %qD has function type",
10655 /* R is not yet sufficiently initialized, so we
10656 just use its name. */
10658 RETURN (error_mark_node
);
10660 type
= complete_type (type
);
10661 /* Wait until cp_finish_decl to set this again, to handle
10662 circular dependency (template/instantiate6.C). */
10663 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10664 type
= check_var_type (DECL_NAME (r
), type
);
10666 if (DECL_HAS_VALUE_EXPR_P (t
))
10668 tree ve
= DECL_VALUE_EXPR (t
);
10669 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10670 /*constant_expression_p=*/false);
10671 if (REFERENCE_REF_P (ve
))
10673 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10674 ve
= TREE_OPERAND (ve
, 0);
10676 SET_DECL_VALUE_EXPR (r
, ve
);
10679 else if (DECL_SELF_REFERENCE_P (t
))
10680 SET_DECL_SELF_REFERENCE_P (r
);
10681 TREE_TYPE (r
) = type
;
10682 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10683 DECL_CONTEXT (r
) = ctx
;
10684 /* Clear out the mangled name and RTL for the instantiation. */
10685 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10686 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10687 SET_DECL_RTL (r
, NULL
);
10688 /* The initializer must not be expanded until it is required;
10689 see [temp.inst]. */
10690 DECL_INITIAL (r
) = NULL_TREE
;
10691 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10692 SET_DECL_RTL (r
, NULL
);
10693 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10694 if (TREE_CODE (r
) == VAR_DECL
)
10696 /* Possibly limit visibility based on template args. */
10697 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10698 if (DECL_VISIBILITY_SPECIFIED (t
))
10700 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10701 DECL_ATTRIBUTES (r
)
10702 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10704 determine_visibility (r
);
10709 /* A static data member declaration is always marked
10710 external when it is declared in-class, even if an
10711 initializer is present. We mimic the non-template
10712 processing here. */
10713 DECL_EXTERNAL (r
) = 1;
10715 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10716 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10717 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10719 else if (cp_unevaluated_operand
)
10721 /* We're substituting this var in a decltype outside of its
10722 scope, such as for a lambda return type. Don't add it to
10723 local_specializations, do perform auto deduction. */
10724 tree auto_node
= type_uses_auto (type
);
10728 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10729 /*constant_expression_p=*/false);
10730 init
= resolve_nondeduced_context (init
);
10731 TREE_TYPE (r
) = type
10732 = do_auto_deduction (type
, init
, auto_node
);
10736 register_local_specialization (r
, t
);
10738 DECL_CHAIN (r
) = NULL_TREE
;
10740 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10742 args
, complain
, in_decl
);
10744 /* Preserve a typedef that names a type. */
10745 if (is_typedef_decl (r
))
10747 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10748 set_underlying_type (r
);
10751 layout_decl (r
, 0);
10756 gcc_unreachable ();
10761 /* Restore the file and line information. */
10762 input_location
= saved_loc
;
10767 /* Substitute into the ARG_TYPES of a function type.
10768 If END is a TREE_CHAIN, leave it and any following types
10772 tsubst_arg_types (tree arg_types
,
10775 tsubst_flags_t complain
,
10778 tree remaining_arg_types
;
10779 tree type
= NULL_TREE
;
10781 tree expanded_args
= NULL_TREE
;
10784 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10787 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10788 args
, end
, complain
, in_decl
);
10789 if (remaining_arg_types
== error_mark_node
)
10790 return error_mark_node
;
10792 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10794 /* For a pack expansion, perform substitution on the
10795 entire expression. Later on, we'll handle the arguments
10797 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10798 args
, complain
, in_decl
);
10800 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10801 /* So that we'll spin through the parameters, one by one. */
10802 i
= TREE_VEC_LENGTH (expanded_args
);
10805 /* We only partially substituted into the parameter
10806 pack. Our type is TYPE_PACK_EXPANSION. */
10807 type
= expanded_args
;
10808 expanded_args
= NULL_TREE
;
10816 type
= TREE_VEC_ELT (expanded_args
, i
);
10818 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10820 if (type
== error_mark_node
)
10821 return error_mark_node
;
10822 if (VOID_TYPE_P (type
))
10824 if (complain
& tf_error
)
10826 error ("invalid parameter type %qT", type
);
10828 error ("in declaration %q+D", in_decl
);
10830 return error_mark_node
;
10833 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10834 top-level qualifiers as required. */
10835 type
= cv_unqualified (type_decays_to (type
));
10837 /* We do not substitute into default arguments here. The standard
10838 mandates that they be instantiated only when needed, which is
10839 done in build_over_call. */
10840 default_arg
= TREE_PURPOSE (arg_types
);
10842 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10844 /* We've instantiated a template before its default arguments
10845 have been parsed. This can happen for a nested template
10846 class, and is not an error unless we require the default
10847 argument in a call of this function. */
10848 remaining_arg_types
=
10849 tree_cons (default_arg
, type
, remaining_arg_types
);
10850 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
10853 remaining_arg_types
=
10854 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10857 return remaining_arg_types
;
10860 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10861 *not* handle the exception-specification for FNTYPE, because the
10862 initial substitution of explicitly provided template parameters
10863 during argument deduction forbids substitution into the
10864 exception-specification:
10868 All references in the function type of the function template to the
10869 corresponding template parameters are replaced by the specified tem-
10870 plate argument values. If a substitution in a template parameter or
10871 in the function type of the function template results in an invalid
10872 type, type deduction fails. [Note: The equivalent substitution in
10873 exception specifications is done only when the function is instanti-
10874 ated, at which point a program is ill-formed if the substitution
10875 results in an invalid type.] */
10878 tsubst_function_type (tree t
,
10880 tsubst_flags_t complain
,
10887 /* The TYPE_CONTEXT is not used for function/method types. */
10888 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10890 /* Substitute the return type. */
10891 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10892 if (return_type
== error_mark_node
)
10893 return error_mark_node
;
10894 /* The standard does not presently indicate that creation of a
10895 function type with an invalid return type is a deduction failure.
10896 However, that is clearly analogous to creating an array of "void"
10897 or a reference to a reference. This is core issue #486. */
10898 if (TREE_CODE (return_type
) == ARRAY_TYPE
10899 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10901 if (complain
& tf_error
)
10903 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10904 error ("function returning an array");
10906 error ("function returning a function");
10908 return error_mark_node
;
10911 /* Substitute the argument types. */
10912 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10913 complain
, in_decl
);
10914 if (arg_types
== error_mark_node
)
10915 return error_mark_node
;
10917 /* Construct a new type node and return it. */
10918 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10920 fntype
= build_function_type (return_type
, arg_types
);
10921 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10925 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10926 if (! MAYBE_CLASS_TYPE_P (r
))
10930 Type deduction may fail for any of the following
10933 -- Attempting to create "pointer to member of T" when T
10934 is not a class type. */
10935 if (complain
& tf_error
)
10936 error ("creating pointer to member function of non-class type %qT",
10938 return error_mark_node
;
10941 fntype
= build_method_type_directly (r
, return_type
,
10942 TREE_CHAIN (arg_types
));
10944 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10949 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10950 ARGS into that specification, and return the substituted
10951 specification. If there is no specification, return NULL_TREE. */
10954 tsubst_exception_specification (tree fntype
,
10956 tsubst_flags_t complain
,
10963 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10964 new_specs
= NULL_TREE
;
10965 if (specs
&& TREE_PURPOSE (specs
))
10967 /* A noexcept-specifier. */
10968 tree expr
= TREE_PURPOSE (specs
);
10969 if (TREE_CODE (expr
) == INTEGER_CST
)
10973 /* Defer instantiation of noexcept-specifiers to avoid
10974 excessive instantiations (c++/49107). */
10975 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10976 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10978 /* We already partially instantiated this member template,
10979 so combine the new args with the old. */
10980 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10981 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10982 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10983 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10987 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10988 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10992 new_specs
= tsubst_copy_and_build
10993 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10994 /*integral_constant_expression_p=*/true);
10995 new_specs
= build_noexcept_spec (new_specs
, complain
);
10999 if (! TREE_VALUE (specs
))
11006 tree expanded_specs
= NULL_TREE
;
11008 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11010 /* Expand the pack expansion type. */
11011 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11015 if (expanded_specs
== error_mark_node
)
11016 return error_mark_node
;
11017 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11018 len
= TREE_VEC_LENGTH (expanded_specs
);
11021 /* We're substituting into a member template, so
11022 we got a TYPE_PACK_EXPANSION back. Add that
11023 expansion and move on. */
11024 gcc_assert (TREE_CODE (expanded_specs
)
11025 == TYPE_PACK_EXPANSION
);
11026 new_specs
= add_exception_specifier (new_specs
,
11029 specs
= TREE_CHAIN (specs
);
11034 for (i
= 0; i
< len
; ++i
)
11036 if (expanded_specs
)
11037 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11039 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11040 if (spec
== error_mark_node
)
11042 new_specs
= add_exception_specifier (new_specs
, spec
,
11046 specs
= TREE_CHAIN (specs
);
11052 /* Take the tree structure T and replace template parameters used
11053 therein with the argument vector ARGS. IN_DECL is an associated
11054 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11055 Issue error and warning messages under control of COMPLAIN. Note
11056 that we must be relatively non-tolerant of extensions here, in
11057 order to preserve conformance; if we allow substitutions that
11058 should not be allowed, we may allow argument deductions that should
11059 not succeed, and therefore report ambiguous overload situations
11060 where there are none. In theory, we could allow the substitution,
11061 but indicate that it should have failed, and allow our caller to
11062 make sure that the right thing happens, but we don't try to do this
11065 This function is used for dealing with types, decls and the like;
11066 for expressions, use tsubst_expr or tsubst_copy. */
11069 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11071 enum tree_code code
;
11072 tree type
, r
= NULL_TREE
;
11074 if (t
== NULL_TREE
|| t
== error_mark_node
11075 || t
== integer_type_node
11076 || t
== void_type_node
11077 || t
== char_type_node
11078 || t
== unknown_type_node
11079 || TREE_CODE (t
) == NAMESPACE_DECL
11080 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11084 return tsubst_decl (t
, args
, complain
);
11086 if (args
== NULL_TREE
)
11089 code
= TREE_CODE (t
);
11091 if (code
== IDENTIFIER_NODE
)
11092 type
= IDENTIFIER_TYPE_VALUE (t
);
11094 type
= TREE_TYPE (t
);
11096 gcc_assert (type
!= unknown_type_node
);
11098 /* Reuse typedefs. We need to do this to handle dependent attributes,
11099 such as attribute aligned. */
11101 && typedef_variant_p (t
))
11103 tree decl
= TYPE_NAME (t
);
11105 if (alias_template_specialization_p (t
))
11107 /* DECL represents an alias template and we want to
11109 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11110 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11111 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11113 else if (DECL_CLASS_SCOPE_P (decl
)
11114 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11115 && uses_template_parms (DECL_CONTEXT (decl
)))
11117 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11118 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11119 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11121 else if (DECL_FUNCTION_SCOPE_P (decl
)
11122 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11123 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11124 r
= retrieve_local_specialization (decl
);
11126 /* The typedef is from a non-template context. */
11132 r
= cp_build_qualified_type_real
11133 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11134 complain
| tf_ignore_bad_quals
);
11139 /* We don't have an instantiation yet, so drop the typedef. */
11140 int quals
= cp_type_quals (t
);
11141 t
= DECL_ORIGINAL_TYPE (decl
);
11142 t
= cp_build_qualified_type_real (t
, quals
,
11143 complain
| tf_ignore_bad_quals
);
11148 && code
!= TYPENAME_TYPE
11149 && code
!= TEMPLATE_TYPE_PARM
11150 && code
!= IDENTIFIER_NODE
11151 && code
!= FUNCTION_TYPE
11152 && code
!= METHOD_TYPE
)
11153 type
= tsubst (type
, args
, complain
, in_decl
);
11154 if (type
== error_mark_node
)
11155 return error_mark_node
;
11161 case ENUMERAL_TYPE
:
11162 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11163 /*entering_scope=*/0);
11166 case IDENTIFIER_NODE
:
11177 if (t
== integer_type_node
)
11180 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11181 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11185 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11187 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11188 /*integral_constant_expression_p=*/false);
11190 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11192 if (TREE_CODE (max
) == NOP_EXPR
11193 && TREE_SIDE_EFFECTS (omax
)
11194 && !TREE_TYPE (max
))
11195 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11197 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11198 with TREE_SIDE_EFFECTS that indicates this is not an integral
11199 constant expression. */
11200 if (processing_template_decl
11201 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11203 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11204 TREE_SIDE_EFFECTS (max
) = 1;
11207 return compute_array_index_type (NULL_TREE
, max
, complain
);
11210 case TEMPLATE_TYPE_PARM
:
11211 case TEMPLATE_TEMPLATE_PARM
:
11212 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11213 case TEMPLATE_PARM_INDEX
:
11218 tree arg
= NULL_TREE
;
11222 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11223 template_parm_level_and_index (t
, &level
, &idx
);
11225 levels
= TMPL_ARGS_DEPTH (args
);
11226 if (level
<= levels
)
11228 arg
= TMPL_ARG (args
, level
, idx
);
11230 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11232 /* See through ARGUMENT_PACK_SELECT arguments. */
11233 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11234 /* If the selected argument is an expansion E, that most
11235 likely means we were called from
11236 gen_elem_of_pack_expansion_instantiation during the
11237 substituting of pack an argument pack (which Ith
11238 element is a pack expansion, where I is
11239 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11240 In this case, the Ith element resulting from this
11241 substituting is going to be a pack expansion, which
11242 pattern is the pattern of E. Let's return the
11244 gen_elem_of_pack_expansion_instantiation will
11245 build the resulting pack expansion from it. */
11246 if (PACK_EXPANSION_P (arg
))
11247 arg
= PACK_EXPANSION_PATTERN (arg
);
11251 if (arg
== error_mark_node
)
11252 return error_mark_node
;
11253 else if (arg
!= NULL_TREE
)
11255 if (ARGUMENT_PACK_P (arg
))
11256 /* If ARG is an argument pack, we don't actually want to
11257 perform a substitution here, because substitutions
11258 for argument packs are only done
11259 element-by-element. We can get to this point when
11260 substituting the type of a non-type template
11261 parameter pack, when that type actually contains
11262 template parameter packs from an outer template, e.g.,
11264 template<typename... Types> struct A {
11265 template<Types... Values> struct B { };
11269 if (code
== TEMPLATE_TYPE_PARM
)
11272 gcc_assert (TYPE_P (arg
));
11274 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11276 return cp_build_qualified_type_real
11277 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11279 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11281 /* We are processing a type constructed from a
11282 template template parameter. */
11283 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11284 args
, complain
, in_decl
);
11285 if (argvec
== error_mark_node
)
11286 return error_mark_node
;
11288 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11289 || TREE_CODE (arg
) == TEMPLATE_DECL
11290 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11292 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11293 /* Consider this code:
11295 template <template <class> class Template>
11297 template <class Arg> using Bind = Template<Arg>;
11300 template <template <class> class Template, class Arg>
11301 using Instantiate = Template<Arg>; //#0
11303 template <template <class> class Template,
11306 Instantiate<Internal<Template>::template Bind,
11309 When #1 is parsed, the
11310 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11311 parameter `Template' in #0 matches the
11312 UNBOUND_CLASS_TEMPLATE representing the argument
11313 `Internal<Template>::template Bind'; We then want
11314 to assemble the type `Bind<Argument>' that can't
11315 be fully created right now, because
11316 `Internal<Template>' not being complete, the Bind
11317 template cannot be looked up in that context. So
11318 we need to "store" `Bind<Argument>' for later
11319 when the context of Bind becomes complete. Let's
11320 store that in a TYPENAME_TYPE. */
11321 return make_typename_type (TYPE_CONTEXT (arg
),
11322 build_nt (TEMPLATE_ID_EXPR
,
11323 TYPE_IDENTIFIER (arg
),
11328 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11329 are resolving nested-types in the signature of a
11330 member function templates. Otherwise ARG is a
11331 TEMPLATE_DECL and is the real template to be
11333 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11334 arg
= TYPE_NAME (arg
);
11336 r
= lookup_template_class (arg
,
11338 DECL_CONTEXT (arg
),
11339 /*entering_scope=*/0,
11341 return cp_build_qualified_type_real
11342 (r
, cp_type_quals (t
), complain
);
11345 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11346 return convert_from_reference (unshare_expr (arg
));
11350 /* This can happen during the attempted tsubst'ing in
11351 unify. This means that we don't yet have any information
11352 about the template parameter in question. */
11355 /* Early in template argument deduction substitution, we don't
11356 want to reduce the level of 'auto', or it will be confused
11357 with a normal template parm in subsequent deduction. */
11358 if (is_auto (t
) && (complain
& tf_partial
))
11361 /* If we get here, we must have been looking at a parm for a
11362 more deeply nested template. Make a new version of this
11363 template parameter, but with a lower level. */
11366 case TEMPLATE_TYPE_PARM
:
11367 case TEMPLATE_TEMPLATE_PARM
:
11368 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11369 if (cp_type_quals (t
))
11371 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11372 r
= cp_build_qualified_type_real
11373 (r
, cp_type_quals (t
),
11374 complain
| (code
== TEMPLATE_TYPE_PARM
11375 ? tf_ignore_bad_quals
: 0));
11380 TEMPLATE_TYPE_PARM_INDEX (r
)
11381 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11382 r
, levels
, args
, complain
);
11383 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11384 TYPE_MAIN_VARIANT (r
) = r
;
11385 TYPE_POINTER_TO (r
) = NULL_TREE
;
11386 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11388 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11389 /* We have reduced the level of the template
11390 template parameter, but not the levels of its
11391 template parameters, so canonical_type_parameter
11392 will not be able to find the canonical template
11393 template parameter for this level. Thus, we
11394 require structural equality checking to compare
11395 TEMPLATE_TEMPLATE_PARMs. */
11396 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11397 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11398 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11400 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11402 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11404 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11405 complain
, in_decl
);
11406 if (argvec
== error_mark_node
)
11407 return error_mark_node
;
11409 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11410 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11415 case TEMPLATE_PARM_INDEX
:
11416 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11420 gcc_unreachable ();
11428 tree purpose
, value
, chain
;
11430 if (t
== void_list_node
)
11433 purpose
= TREE_PURPOSE (t
);
11436 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11437 if (purpose
== error_mark_node
)
11438 return error_mark_node
;
11440 value
= TREE_VALUE (t
);
11443 value
= tsubst (value
, args
, complain
, in_decl
);
11444 if (value
== error_mark_node
)
11445 return error_mark_node
;
11447 chain
= TREE_CHAIN (t
);
11448 if (chain
&& chain
!= void_type_node
)
11450 chain
= tsubst (chain
, args
, complain
, in_decl
);
11451 if (chain
== error_mark_node
)
11452 return error_mark_node
;
11454 if (purpose
== TREE_PURPOSE (t
)
11455 && value
== TREE_VALUE (t
)
11456 && chain
== TREE_CHAIN (t
))
11458 return hash_tree_cons (purpose
, value
, chain
);
11462 /* We should never be tsubsting a binfo. */
11463 gcc_unreachable ();
11466 /* A vector of template arguments. */
11467 gcc_assert (!type
);
11468 return tsubst_template_args (t
, args
, complain
, in_decl
);
11471 case REFERENCE_TYPE
:
11473 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11478 Type deduction may fail for any of the following
11481 -- Attempting to create a pointer to reference type.
11482 -- Attempting to create a reference to a reference type or
11483 a reference to void.
11485 Core issue 106 says that creating a reference to a reference
11486 during instantiation is no longer a cause for failure. We
11487 only enforce this check in strict C++98 mode. */
11488 if ((TREE_CODE (type
) == REFERENCE_TYPE
11489 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11490 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11492 static location_t last_loc
;
11494 /* We keep track of the last time we issued this error
11495 message to avoid spewing a ton of messages during a
11496 single bad template instantiation. */
11497 if (complain
& tf_error
11498 && last_loc
!= input_location
)
11500 if (TREE_CODE (type
) == VOID_TYPE
)
11501 error ("forming reference to void");
11502 else if (code
== POINTER_TYPE
)
11503 error ("forming pointer to reference type %qT", type
);
11505 error ("forming reference to reference type %qT", type
);
11506 last_loc
= input_location
;
11509 return error_mark_node
;
11511 else if (code
== POINTER_TYPE
)
11513 r
= build_pointer_type (type
);
11514 if (TREE_CODE (type
) == METHOD_TYPE
)
11515 r
= build_ptrmemfunc_type (r
);
11517 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11518 /* In C++0x, during template argument substitution, when there is an
11519 attempt to create a reference to a reference type, reference
11520 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11522 "If a template-argument for a template-parameter T names a type
11523 that is a reference to a type A, an attempt to create the type
11524 'lvalue reference to cv T' creates the type 'lvalue reference to
11525 A,' while an attempt to create the type type rvalue reference to
11526 cv T' creates the type T"
11528 r
= cp_build_reference_type
11530 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11532 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11533 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11535 if (r
!= error_mark_node
)
11536 /* Will this ever be needed for TYPE_..._TO values? */
11543 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11544 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11548 Type deduction may fail for any of the following
11551 -- Attempting to create "pointer to member of T" when T
11552 is not a class type. */
11553 if (complain
& tf_error
)
11554 error ("creating pointer to member of non-class type %qT", r
);
11555 return error_mark_node
;
11557 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11559 if (complain
& tf_error
)
11560 error ("creating pointer to member reference type %qT", type
);
11561 return error_mark_node
;
11563 if (TREE_CODE (type
) == VOID_TYPE
)
11565 if (complain
& tf_error
)
11566 error ("creating pointer to member of type void");
11567 return error_mark_node
;
11569 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11570 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11572 /* The type of the implicit object parameter gets its
11573 cv-qualifiers from the FUNCTION_TYPE. */
11575 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11576 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11577 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11581 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11585 case FUNCTION_TYPE
:
11590 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11591 if (fntype
== error_mark_node
)
11592 return error_mark_node
;
11594 /* Substitute the exception specification. */
11595 specs
= tsubst_exception_specification (t
, args
, complain
,
11596 in_decl
, /*defer_ok*/true);
11597 if (specs
== error_mark_node
)
11598 return error_mark_node
;
11600 fntype
= build_exception_variant (fntype
, specs
);
11605 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11606 if (domain
== error_mark_node
)
11607 return error_mark_node
;
11609 /* As an optimization, we avoid regenerating the array type if
11610 it will obviously be the same as T. */
11611 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11614 /* These checks should match the ones in grokdeclarator.
11618 The deduction may fail for any of the following reasons:
11620 -- Attempting to create an array with an element type that
11621 is void, a function type, or a reference type, or [DR337]
11622 an abstract class type. */
11623 if (TREE_CODE (type
) == VOID_TYPE
11624 || TREE_CODE (type
) == FUNCTION_TYPE
11625 || TREE_CODE (type
) == REFERENCE_TYPE
)
11627 if (complain
& tf_error
)
11628 error ("creating array of %qT", type
);
11629 return error_mark_node
;
11631 if (ABSTRACT_CLASS_TYPE_P (type
))
11633 if (complain
& tf_error
)
11634 error ("creating array of %qT, which is an abstract class type",
11636 return error_mark_node
;
11639 r
= build_cplus_array_type (type
, domain
);
11641 if (TYPE_USER_ALIGN (t
))
11643 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11644 TYPE_USER_ALIGN (r
) = 1;
11650 case TYPENAME_TYPE
:
11652 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11653 in_decl
, /*entering_scope=*/1);
11654 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11655 complain
, in_decl
);
11657 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11658 return error_mark_node
;
11660 if (!MAYBE_CLASS_TYPE_P (ctx
))
11662 if (complain
& tf_error
)
11663 error ("%qT is not a class, struct, or union type", ctx
);
11664 return error_mark_node
;
11666 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11668 /* Normally, make_typename_type does not require that the CTX
11669 have complete type in order to allow things like:
11671 template <class T> struct S { typename S<T>::X Y; };
11673 But, such constructs have already been resolved by this
11674 point, so here CTX really should have complete type, unless
11675 it's a partial instantiation. */
11676 ctx
= complete_type (ctx
);
11677 if (!COMPLETE_TYPE_P (ctx
))
11679 if (complain
& tf_error
)
11680 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11681 return error_mark_node
;
11685 f
= make_typename_type (ctx
, f
, typename_type
,
11686 complain
| tf_keep_type_decl
);
11687 if (f
== error_mark_node
)
11689 if (TREE_CODE (f
) == TYPE_DECL
)
11691 complain
|= tf_ignore_bad_quals
;
11695 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11697 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11699 if (complain
& tf_error
)
11700 error ("%qT resolves to %qT, which is not an enumeration type",
11703 return error_mark_node
;
11705 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11707 if (complain
& tf_error
)
11708 error ("%qT resolves to %qT, which is is not a class type",
11711 return error_mark_node
;
11715 return cp_build_qualified_type_real
11716 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11719 case UNBOUND_CLASS_TEMPLATE
:
11721 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11722 in_decl
, /*entering_scope=*/1);
11723 tree name
= TYPE_IDENTIFIER (t
);
11724 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11726 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11727 return error_mark_node
;
11730 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11731 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11738 ++cp_unevaluated_operand
;
11739 ++c_inhibit_evaluation_warnings
;
11741 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11743 /*integral_constant_expression_p=*/false);
11745 --cp_unevaluated_operand
;
11746 --c_inhibit_evaluation_warnings
;
11748 type
= finish_typeof (type
);
11749 return cp_build_qualified_type_real (type
,
11751 | cp_type_quals (type
),
11755 case DECLTYPE_TYPE
:
11759 ++cp_unevaluated_operand
;
11760 ++c_inhibit_evaluation_warnings
;
11762 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11764 /*integral_constant_expression_p=*/false);
11766 --cp_unevaluated_operand
;
11767 --c_inhibit_evaluation_warnings
;
11769 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11770 type
= lambda_capture_field_type (type
);
11771 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11772 type
= lambda_proxy_type (type
);
11774 type
= finish_decltype_type
11775 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11776 return cp_build_qualified_type_real (type
,
11778 | cp_type_quals (type
),
11782 case UNDERLYING_TYPE
:
11784 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11785 complain
, in_decl
);
11786 return finish_underlying_type (type
);
11789 case TYPE_ARGUMENT_PACK
:
11790 case NONTYPE_ARGUMENT_PACK
:
11792 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11794 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11798 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11800 /* For template nontype argument packs, also substitute into
11802 if (code
== NONTYPE_ARGUMENT_PACK
)
11803 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11821 /* We should use one of the expression tsubsts for these codes. */
11822 gcc_unreachable ();
11825 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11826 return error_mark_node
;
11830 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11831 type of the expression on the left-hand side of the "." or "->"
11835 tsubst_baselink (tree baselink
, tree object_type
,
11836 tree args
, tsubst_flags_t complain
, tree in_decl
)
11839 tree qualifying_scope
;
11842 tree template_args
= 0;
11843 bool template_id_p
= false;
11844 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11846 /* A baselink indicates a function from a base class. Both the
11847 BASELINK_ACCESS_BINFO and the base class referenced may
11848 indicate bases of the template class, rather than the
11849 instantiated class. In addition, lookups that were not
11850 ambiguous before may be ambiguous now. Therefore, we perform
11851 the lookup again. */
11852 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11853 qualifying_scope
= tsubst (qualifying_scope
, args
,
11854 complain
, in_decl
);
11855 fns
= BASELINK_FUNCTIONS (baselink
);
11856 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11857 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11859 template_id_p
= true;
11860 template_args
= TREE_OPERAND (fns
, 1);
11861 fns
= TREE_OPERAND (fns
, 0);
11863 template_args
= tsubst_template_args (template_args
, args
,
11864 complain
, in_decl
);
11866 name
= DECL_NAME (get_first_fn (fns
));
11867 if (IDENTIFIER_TYPENAME_P (name
))
11868 name
= mangle_conv_op_name_for_type (optype
);
11869 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11871 return error_mark_node
;
11873 /* If lookup found a single function, mark it as used at this
11874 point. (If it lookup found multiple functions the one selected
11875 later by overload resolution will be marked as used at that
11877 if (BASELINK_P (baselink
))
11878 fns
= BASELINK_FUNCTIONS (baselink
);
11879 if (!template_id_p
&& !really_overloaded_fn (fns
))
11880 mark_used (OVL_CURRENT (fns
));
11882 /* Add back the template arguments, if present. */
11883 if (BASELINK_P (baselink
) && template_id_p
)
11884 BASELINK_FUNCTIONS (baselink
)
11885 = build_nt (TEMPLATE_ID_EXPR
,
11886 BASELINK_FUNCTIONS (baselink
),
11888 /* Update the conversion operator type. */
11889 BASELINK_OPTYPE (baselink
) = optype
;
11892 object_type
= current_class_type
;
11895 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11901 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11902 true if the qualified-id will be a postfix-expression in-and-of
11903 itself; false if more of the postfix-expression follows the
11904 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11908 tsubst_qualified_id (tree qualified_id
, tree args
,
11909 tsubst_flags_t complain
, tree in_decl
,
11910 bool done
, bool address_p
)
11916 tree template_args
;
11917 location_t loc
= UNKNOWN_LOCATION
;
11919 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11921 /* Figure out what name to look up. */
11922 name
= TREE_OPERAND (qualified_id
, 1);
11923 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11925 is_template
= true;
11926 loc
= EXPR_LOCATION (name
);
11927 template_args
= TREE_OPERAND (name
, 1);
11929 template_args
= tsubst_template_args (template_args
, args
,
11930 complain
, in_decl
);
11931 name
= TREE_OPERAND (name
, 0);
11935 is_template
= false;
11936 template_args
= NULL_TREE
;
11939 /* Substitute into the qualifying scope. When there are no ARGS, we
11940 are just trying to simplify a non-dependent expression. In that
11941 case the qualifying scope may be dependent, and, in any case,
11942 substituting will not help. */
11943 scope
= TREE_OPERAND (qualified_id
, 0);
11946 scope
= tsubst (scope
, args
, complain
, in_decl
);
11947 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11952 if (dependent_scope_p (scope
))
11955 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11956 return build_qualified_name (NULL_TREE
, scope
, expr
,
11957 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11960 if (!BASELINK_P (name
) && !DECL_P (expr
))
11962 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11964 /* A BIT_NOT_EXPR is used to represent a destructor. */
11965 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11967 error ("qualifying type %qT does not match destructor name ~%qT",
11968 scope
, TREE_OPERAND (expr
, 0));
11969 expr
= error_mark_node
;
11972 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11973 /*is_type_p=*/0, false);
11976 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11977 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11978 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11980 if (complain
& tf_error
)
11982 error ("dependent-name %qE is parsed as a non-type, but "
11983 "instantiation yields a type", qualified_id
);
11984 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11986 return error_mark_node
;
11992 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11994 /* Remember that there was a reference to this entity. */
11998 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12000 if (complain
& tf_error
)
12001 qualified_name_lookup_error (scope
,
12002 TREE_OPERAND (qualified_id
, 1),
12003 expr
, input_location
);
12004 return error_mark_node
;
12008 expr
= lookup_template_function (expr
, template_args
);
12010 if (expr
== error_mark_node
&& complain
& tf_error
)
12011 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12012 expr
, input_location
);
12013 else if (TYPE_P (scope
))
12015 expr
= (adjust_result_of_qualified_name_lookup
12016 (expr
, scope
, current_class_type
));
12017 expr
= (finish_qualified_id_expr
12018 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12019 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12020 /*template_arg_p=*/false));
12023 /* Expressions do not generally have reference type. */
12024 if (TREE_CODE (expr
) != SCOPE_REF
12025 /* However, if we're about to form a pointer-to-member, we just
12026 want the referenced member referenced. */
12027 && TREE_CODE (expr
) != OFFSET_REF
)
12028 expr
= convert_from_reference (expr
);
12033 /* Like tsubst, but deals with expressions. This function just replaces
12034 template parms; to finish processing the resultant expression, use
12035 tsubst_copy_and_build or tsubst_expr. */
12038 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12040 enum tree_code code
;
12043 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12046 code
= TREE_CODE (t
);
12051 r
= retrieve_local_specialization (t
);
12053 if (r
== NULL_TREE
)
12055 /* We get here for a use of 'this' in an NSDMI. */
12056 if (DECL_NAME (t
) == this_identifier
12057 && at_function_scope_p ()
12058 && DECL_CONSTRUCTOR_P (current_function_decl
))
12059 return current_class_ptr
;
12061 /* This can happen for a parameter name used later in a function
12062 declaration (such as in a late-specified return type). Just
12063 make a dummy decl, since it's only used for its type. */
12064 gcc_assert (cp_unevaluated_operand
!= 0);
12065 r
= tsubst_decl (t
, args
, complain
);
12066 /* Give it the template pattern as its context; its true context
12067 hasn't been instantiated yet and this is good enough for
12069 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12072 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12073 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12082 if (DECL_TEMPLATE_PARM_P (t
))
12083 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12084 /* There is no need to substitute into namespace-scope
12086 if (DECL_NAMESPACE_SCOPE_P (t
))
12088 /* If ARGS is NULL, then T is known to be non-dependent. */
12089 if (args
== NULL_TREE
)
12090 return integral_constant_value (t
);
12092 /* Unfortunately, we cannot just call lookup_name here.
12095 template <int I> int f() {
12097 struct S { void g() { E e = a; } };
12100 When we instantiate f<7>::S::g(), say, lookup_name is not
12101 clever enough to find f<7>::a. */
12103 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12104 /*entering_scope=*/0);
12106 for (v
= TYPE_VALUES (enum_type
);
12108 v
= TREE_CHAIN (v
))
12109 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12110 return TREE_VALUE (v
);
12112 /* We didn't find the name. That should never happen; if
12113 name-lookup found it during preliminary parsing, we
12114 should find it again here during instantiation. */
12115 gcc_unreachable ();
12120 if (DECL_CONTEXT (t
))
12124 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12125 /*entering_scope=*/1);
12126 if (ctx
!= DECL_CONTEXT (t
))
12128 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12131 if (complain
& tf_error
)
12132 error ("using invalid field %qD", t
);
12133 return error_mark_node
;
12142 case FUNCTION_DECL
:
12143 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12144 || local_variable_p (t
))
12145 t
= tsubst (t
, args
, complain
, in_decl
);
12149 case NAMESPACE_DECL
:
12153 /* An OVERLOAD will always be a non-dependent overload set; an
12154 overload set from function scope will just be represented with an
12155 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12156 gcc_assert (!uses_template_parms (t
));
12160 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12162 case TEMPLATE_DECL
:
12163 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12164 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12165 args
, complain
, in_decl
);
12166 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12167 return tsubst (t
, args
, complain
, in_decl
);
12168 else if (DECL_CLASS_SCOPE_P (t
)
12169 && uses_template_parms (DECL_CONTEXT (t
)))
12171 /* Template template argument like the following example need
12174 template <template <class> class TT> struct C {};
12175 template <class T> struct D {
12176 template <class U> struct E {};
12181 We are processing the template argument `E' in #1 for
12182 the template instantiation #2. Originally, `E' is a
12183 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12184 have to substitute this with one having context `D<int>'. */
12186 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12187 return lookup_field (context
, DECL_NAME(t
), 0, false);
12190 /* Ordinary template template argument. */
12194 case REINTERPRET_CAST_EXPR
:
12195 case CONST_CAST_EXPR
:
12196 case STATIC_CAST_EXPR
:
12197 case DYNAMIC_CAST_EXPR
:
12198 case IMPLICIT_CONV_EXPR
:
12202 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12203 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12206 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12209 tree expanded
, op
= TREE_OPERAND (t
, 0);
12212 if (SIZEOF_EXPR_TYPE_P (t
))
12213 op
= TREE_TYPE (op
);
12215 ++cp_unevaluated_operand
;
12216 ++c_inhibit_evaluation_warnings
;
12217 /* We only want to compute the number of arguments. */
12218 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12219 --cp_unevaluated_operand
;
12220 --c_inhibit_evaluation_warnings
;
12222 if (TREE_CODE (expanded
) == TREE_VEC
)
12223 len
= TREE_VEC_LENGTH (expanded
);
12225 if (expanded
== error_mark_node
)
12226 return error_mark_node
;
12227 else if (PACK_EXPANSION_P (expanded
)
12228 || (TREE_CODE (expanded
) == TREE_VEC
12230 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12232 if (TREE_CODE (expanded
) == TREE_VEC
)
12233 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12235 if (TYPE_P (expanded
))
12236 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12237 complain
& tf_error
);
12239 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12240 complain
& tf_error
);
12243 return build_int_cst (size_type_node
, len
);
12245 if (SIZEOF_EXPR_TYPE_P (t
))
12247 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12248 args
, complain
, in_decl
);
12249 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12250 r
= build1 (SIZEOF_EXPR
,
12251 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12252 SIZEOF_EXPR_TYPE_P (r
) = 1;
12259 case TRUTH_NOT_EXPR
:
12262 case UNARY_PLUS_EXPR
: /* Unary + */
12264 case AT_ENCODE_EXPR
:
12268 case REALPART_EXPR
:
12269 case IMAGPART_EXPR
:
12271 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12272 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12274 case COMPONENT_REF
:
12279 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12280 name
= TREE_OPERAND (t
, 1);
12281 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12283 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12284 complain
, in_decl
);
12285 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12287 else if (TREE_CODE (name
) == SCOPE_REF
12288 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12290 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12291 complain
, in_decl
);
12292 name
= TREE_OPERAND (name
, 1);
12293 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12294 complain
, in_decl
);
12295 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12296 name
= build_qualified_name (/*type=*/NULL_TREE
,
12298 /*template_p=*/false);
12300 else if (BASELINK_P (name
))
12301 name
= tsubst_baselink (name
,
12302 non_reference (TREE_TYPE (object
)),
12306 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12307 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12313 case TRUNC_DIV_EXPR
:
12314 case CEIL_DIV_EXPR
:
12315 case FLOOR_DIV_EXPR
:
12316 case ROUND_DIV_EXPR
:
12317 case EXACT_DIV_EXPR
:
12321 case TRUNC_MOD_EXPR
:
12322 case FLOOR_MOD_EXPR
:
12323 case TRUTH_ANDIF_EXPR
:
12324 case TRUTH_ORIF_EXPR
:
12325 case TRUTH_AND_EXPR
:
12326 case TRUTH_OR_EXPR
:
12339 case COMPOUND_EXPR
:
12342 case PREDECREMENT_EXPR
:
12343 case PREINCREMENT_EXPR
:
12344 case POSTDECREMENT_EXPR
:
12345 case POSTINCREMENT_EXPR
:
12347 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12348 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12351 return build_qualified_name (/*type=*/NULL_TREE
,
12352 tsubst_copy (TREE_OPERAND (t
, 0),
12353 args
, complain
, in_decl
),
12354 tsubst_copy (TREE_OPERAND (t
, 1),
12355 args
, complain
, in_decl
),
12356 QUALIFIED_NAME_IS_TEMPLATE (t
));
12361 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12362 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12363 NULL_TREE
, NULL_TREE
);
12367 int n
= VL_EXP_OPERAND_LENGTH (t
);
12368 tree result
= build_vl_exp (CALL_EXPR
, n
);
12370 for (i
= 0; i
< n
; i
++)
12371 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12372 complain
, in_decl
);
12378 case PSEUDO_DTOR_EXPR
:
12381 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12382 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12383 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12384 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12391 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12392 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12393 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12394 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12401 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12402 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12403 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12404 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12408 case TEMPLATE_ID_EXPR
:
12410 /* Substituted template arguments */
12411 tree fn
= TREE_OPERAND (t
, 0);
12412 tree targs
= TREE_OPERAND (t
, 1);
12414 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12416 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12418 return lookup_template_function (fn
, targs
);
12423 tree purpose
, value
, chain
;
12425 if (t
== void_list_node
)
12428 purpose
= TREE_PURPOSE (t
);
12430 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12431 value
= TREE_VALUE (t
);
12433 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12434 chain
= TREE_CHAIN (t
);
12435 if (chain
&& chain
!= void_type_node
)
12436 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12437 if (purpose
== TREE_PURPOSE (t
)
12438 && value
== TREE_VALUE (t
)
12439 && chain
== TREE_CHAIN (t
))
12441 return tree_cons (purpose
, value
, chain
);
12446 case ENUMERAL_TYPE
:
12448 case TEMPLATE_TYPE_PARM
:
12449 case TEMPLATE_TEMPLATE_PARM
:
12450 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12451 case TEMPLATE_PARM_INDEX
:
12453 case REFERENCE_TYPE
:
12455 case FUNCTION_TYPE
:
12458 case TYPENAME_TYPE
:
12459 case UNBOUND_CLASS_TEMPLATE
:
12461 case DECLTYPE_TYPE
:
12463 return tsubst (t
, args
, complain
, in_decl
);
12465 case IDENTIFIER_NODE
:
12466 if (IDENTIFIER_TYPENAME_P (t
))
12468 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12469 return mangle_conv_op_name_for_type (new_type
);
12475 /* This is handled by tsubst_copy_and_build. */
12476 gcc_unreachable ();
12479 return build_x_va_arg (EXPR_LOCATION (t
),
12480 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12482 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12484 case CLEANUP_POINT_EXPR
:
12485 /* We shouldn't have built any of these during initial template
12486 generation. Instead, they should be built during instantiation
12487 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12488 gcc_unreachable ();
12492 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12493 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12494 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12495 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12496 mark_used (TREE_OPERAND (r
, 1));
12499 case EXPR_PACK_EXPANSION
:
12500 error ("invalid use of pack expansion expression");
12501 return error_mark_node
;
12503 case NONTYPE_ARGUMENT_PACK
:
12504 error ("use %<...%> to expand argument pack");
12505 return error_mark_node
;
12512 /* Instantiate any typedefs in the type. */
12513 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12514 r
= fold_convert (type
, t
);
12515 gcc_assert (TREE_CODE (r
) == code
);
12520 /* These can sometimes show up in a partial instantiation, but never
12521 involve template parms. */
12522 gcc_assert (!uses_template_parms (t
));
12526 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12527 gcc_checking_assert (false);
12532 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12535 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12538 tree new_clauses
= NULL
, nc
, oc
;
12540 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12542 nc
= copy_node (oc
);
12543 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12546 switch (OMP_CLAUSE_CODE (nc
))
12548 case OMP_CLAUSE_LASTPRIVATE
:
12549 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12551 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12552 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12553 in_decl
, /*integral_constant_expression_p=*/false);
12554 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12555 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12558 case OMP_CLAUSE_PRIVATE
:
12559 case OMP_CLAUSE_SHARED
:
12560 case OMP_CLAUSE_FIRSTPRIVATE
:
12561 case OMP_CLAUSE_REDUCTION
:
12562 case OMP_CLAUSE_COPYIN
:
12563 case OMP_CLAUSE_COPYPRIVATE
:
12564 case OMP_CLAUSE_IF
:
12565 case OMP_CLAUSE_NUM_THREADS
:
12566 case OMP_CLAUSE_SCHEDULE
:
12567 case OMP_CLAUSE_COLLAPSE
:
12568 case OMP_CLAUSE_FINAL
:
12569 OMP_CLAUSE_OPERAND (nc
, 0)
12570 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12571 in_decl
, /*integral_constant_expression_p=*/false);
12573 case OMP_CLAUSE_NOWAIT
:
12574 case OMP_CLAUSE_ORDERED
:
12575 case OMP_CLAUSE_DEFAULT
:
12576 case OMP_CLAUSE_UNTIED
:
12577 case OMP_CLAUSE_MERGEABLE
:
12580 gcc_unreachable ();
12584 return finish_omp_clauses (nreverse (new_clauses
));
12587 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12590 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12593 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12595 tree purpose
, value
, chain
;
12600 if (TREE_CODE (t
) != TREE_LIST
)
12601 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12602 /*function_p=*/false,
12603 /*integral_constant_expression_p=*/false);
12605 if (t
== void_list_node
)
12608 purpose
= TREE_PURPOSE (t
);
12610 purpose
= RECUR (purpose
);
12611 value
= TREE_VALUE (t
);
12614 if (TREE_CODE (value
) != LABEL_DECL
)
12615 value
= RECUR (value
);
12618 value
= lookup_label (DECL_NAME (value
));
12619 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12620 TREE_USED (value
) = 1;
12623 chain
= TREE_CHAIN (t
);
12624 if (chain
&& chain
!= void_type_node
)
12625 chain
= RECUR (chain
);
12626 return tree_cons (purpose
, value
, chain
);
12630 /* Substitute one OMP_FOR iterator. */
12633 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12634 tree condv
, tree incrv
, tree
*clauses
,
12635 tree args
, tsubst_flags_t complain
, tree in_decl
,
12636 bool integral_constant_expression_p
)
12638 #define RECUR(NODE) \
12639 tsubst_expr ((NODE), args, complain, in_decl, \
12640 integral_constant_expression_p)
12641 tree decl
, init
, cond
, incr
;
12644 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12645 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12646 decl
= TREE_OPERAND (init
, 0);
12647 init
= TREE_OPERAND (init
, 1);
12648 /* Do this before substituting into decl to handle 'auto'. */
12649 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12650 init
= RECUR (init
);
12651 decl
= RECUR (decl
);
12654 gcc_assert (!processing_template_decl
);
12655 init
= DECL_INITIAL (decl
);
12656 DECL_INITIAL (decl
) = NULL_TREE
;
12659 gcc_assert (!type_dependent_expression_p (decl
));
12661 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12663 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12664 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12665 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12666 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12667 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12668 RECUR (TREE_OPERAND (incr
, 1)),
12671 incr
= RECUR (incr
);
12672 TREE_VEC_ELT (declv
, i
) = decl
;
12673 TREE_VEC_ELT (initv
, i
) = init
;
12674 TREE_VEC_ELT (condv
, i
) = cond
;
12675 TREE_VEC_ELT (incrv
, i
) = incr
;
12679 if (init
&& !init_decl
)
12682 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12684 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12685 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12686 && OMP_CLAUSE_DECL (c
) == decl
)
12688 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12689 && OMP_CLAUSE_DECL (c
) == decl
)
12690 error ("iteration variable %qD should not be firstprivate", decl
);
12691 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12692 && OMP_CLAUSE_DECL (c
) == decl
)
12693 error ("iteration variable %qD should not be reduction", decl
);
12697 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12698 OMP_CLAUSE_DECL (c
) = decl
;
12699 c
= finish_omp_clauses (c
);
12702 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12707 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12708 if (COMPARISON_CLASS_P (cond
))
12709 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12710 RECUR (TREE_OPERAND (cond
, 0)),
12711 RECUR (TREE_OPERAND (cond
, 1)));
12713 cond
= RECUR (cond
);
12714 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12715 switch (TREE_CODE (incr
))
12717 case PREINCREMENT_EXPR
:
12718 case PREDECREMENT_EXPR
:
12719 case POSTINCREMENT_EXPR
:
12720 case POSTDECREMENT_EXPR
:
12721 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12722 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12725 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12726 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12728 tree rhs
= TREE_OPERAND (incr
, 1);
12729 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12730 RECUR (TREE_OPERAND (incr
, 0)),
12731 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12732 RECUR (TREE_OPERAND (rhs
, 0)),
12733 RECUR (TREE_OPERAND (rhs
, 1))));
12736 incr
= RECUR (incr
);
12739 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12740 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12742 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12743 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12744 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12745 TREE_TYPE (decl
), lhs
,
12746 RECUR (TREE_OPERAND (incr
, 2))));
12748 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12749 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12750 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12752 tree rhs
= TREE_OPERAND (incr
, 2);
12753 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12754 RECUR (TREE_OPERAND (incr
, 0)),
12755 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12756 RECUR (TREE_OPERAND (rhs
, 0)),
12757 RECUR (TREE_OPERAND (rhs
, 1))));
12760 incr
= RECUR (incr
);
12763 incr
= RECUR (incr
);
12767 TREE_VEC_ELT (declv
, i
) = decl
;
12768 TREE_VEC_ELT (initv
, i
) = init
;
12769 TREE_VEC_ELT (condv
, i
) = cond
;
12770 TREE_VEC_ELT (incrv
, i
) = incr
;
12774 /* Like tsubst_copy for expressions, etc. but also does semantic
12778 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12779 bool integral_constant_expression_p
)
12781 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12782 #define RECUR(NODE) \
12783 tsubst_expr ((NODE), args, complain, in_decl, \
12784 integral_constant_expression_p)
12790 if (t
== NULL_TREE
|| t
== error_mark_node
)
12793 loc
= input_location
;
12794 if (EXPR_HAS_LOCATION (t
))
12795 input_location
= EXPR_LOCATION (t
);
12796 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12797 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12799 switch (TREE_CODE (t
))
12801 case STATEMENT_LIST
:
12803 tree_stmt_iterator i
;
12804 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12805 RECUR (tsi_stmt (i
));
12809 case CTOR_INITIALIZER
:
12810 finish_mem_initializers (tsubst_initializer_list
12811 (TREE_OPERAND (t
, 0), args
));
12815 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12819 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12820 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12821 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12823 finish_expr_stmt (tmp
);
12827 do_using_directive (USING_STMT_NAMESPACE (t
));
12832 tree decl
, pattern_decl
;
12835 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12836 if (TREE_CODE (decl
) == LABEL_DECL
)
12837 finish_label_decl (DECL_NAME (decl
));
12838 else if (TREE_CODE (decl
) == USING_DECL
)
12840 tree scope
= USING_DECL_SCOPE (decl
);
12841 tree name
= DECL_NAME (decl
);
12844 scope
= tsubst (scope
, args
, complain
, in_decl
);
12845 decl
= lookup_qualified_name (scope
, name
,
12846 /*is_type_p=*/false,
12847 /*complain=*/false);
12848 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12849 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12851 do_local_using_decl (decl
, scope
, name
);
12855 init
= DECL_INITIAL (decl
);
12856 decl
= tsubst (decl
, args
, complain
, in_decl
);
12857 if (decl
!= error_mark_node
)
12859 /* By marking the declaration as instantiated, we avoid
12860 trying to instantiate it. Since instantiate_decl can't
12861 handle local variables, and since we've already done
12862 all that needs to be done, that's the right thing to
12864 if (TREE_CODE (decl
) == VAR_DECL
)
12865 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12866 if (TREE_CODE (decl
) == VAR_DECL
12867 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12868 /* Anonymous aggregates are a special case. */
12869 finish_anon_union (decl
);
12870 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12872 DECL_CONTEXT (decl
) = current_function_decl
;
12873 if (DECL_NAME (decl
) == this_identifier
)
12875 tree lam
= DECL_CONTEXT (current_function_decl
);
12876 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
12877 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
12879 insert_capture_proxy (decl
);
12881 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12882 /* We already did a pushtag. */;
12885 int const_init
= false;
12886 maybe_push_decl (decl
);
12887 if (TREE_CODE (decl
) == VAR_DECL
12888 && DECL_PRETTY_FUNCTION_P (decl
))
12890 /* For __PRETTY_FUNCTION__ we have to adjust the
12892 const char *const name
12893 = cxx_printable_name (current_function_decl
, 2);
12894 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12898 tree t
= RECUR (init
);
12902 /* If we had an initializer but it
12903 instantiated to nothing,
12904 value-initialize the object. This will
12905 only occur when the initializer was a
12906 pack expansion where the parameter packs
12907 used in that expansion were of length
12909 init
= build_value_init (TREE_TYPE (decl
),
12911 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12912 init
= get_target_expr_sfinae (init
, complain
);
12918 if (TREE_CODE (decl
) == VAR_DECL
)
12919 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12921 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12930 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12931 RECUR (FOR_INIT_STMT (t
));
12932 finish_for_init_stmt (stmt
);
12933 tmp
= RECUR (FOR_COND (t
));
12934 finish_for_cond (tmp
, stmt
);
12935 tmp
= RECUR (FOR_EXPR (t
));
12936 finish_for_expr (tmp
, stmt
);
12937 RECUR (FOR_BODY (t
));
12938 finish_for_stmt (stmt
);
12941 case RANGE_FOR_STMT
:
12944 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12945 decl
= RANGE_FOR_DECL (t
);
12946 decl
= tsubst (decl
, args
, complain
, in_decl
);
12947 maybe_push_decl (decl
);
12948 expr
= RECUR (RANGE_FOR_EXPR (t
));
12949 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12950 RECUR (RANGE_FOR_BODY (t
));
12951 finish_for_stmt (stmt
);
12956 stmt
= begin_while_stmt ();
12957 tmp
= RECUR (WHILE_COND (t
));
12958 finish_while_stmt_cond (tmp
, stmt
);
12959 RECUR (WHILE_BODY (t
));
12960 finish_while_stmt (stmt
);
12964 stmt
= begin_do_stmt ();
12965 RECUR (DO_BODY (t
));
12966 finish_do_body (stmt
);
12967 tmp
= RECUR (DO_COND (t
));
12968 finish_do_stmt (tmp
, stmt
);
12972 stmt
= begin_if_stmt ();
12973 tmp
= RECUR (IF_COND (t
));
12974 finish_if_stmt_cond (tmp
, stmt
);
12975 RECUR (THEN_CLAUSE (t
));
12976 finish_then_clause (stmt
);
12978 if (ELSE_CLAUSE (t
))
12980 begin_else_clause (stmt
);
12981 RECUR (ELSE_CLAUSE (t
));
12982 finish_else_clause (stmt
);
12985 finish_if_stmt (stmt
);
12989 if (BIND_EXPR_BODY_BLOCK (t
))
12990 stmt
= begin_function_body ();
12992 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12993 ? BCS_TRY_BLOCK
: 0);
12995 RECUR (BIND_EXPR_BODY (t
));
12997 if (BIND_EXPR_BODY_BLOCK (t
))
12998 finish_function_body (stmt
);
13000 finish_compound_stmt (stmt
);
13004 finish_break_stmt ();
13007 case CONTINUE_STMT
:
13008 finish_continue_stmt ();
13012 stmt
= begin_switch_stmt ();
13013 tmp
= RECUR (SWITCH_STMT_COND (t
));
13014 finish_switch_cond (tmp
, stmt
);
13015 RECUR (SWITCH_STMT_BODY (t
));
13016 finish_switch_stmt (stmt
);
13019 case CASE_LABEL_EXPR
:
13020 finish_case_label (EXPR_LOCATION (t
),
13021 RECUR (CASE_LOW (t
)),
13022 RECUR (CASE_HIGH (t
)));
13027 tree decl
= LABEL_EXPR_LABEL (t
);
13030 label
= finish_label_stmt (DECL_NAME (decl
));
13031 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13032 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13037 tmp
= GOTO_DESTINATION (t
);
13038 if (TREE_CODE (tmp
) != LABEL_DECL
)
13039 /* Computed goto's must be tsubst'd into. On the other hand,
13040 non-computed gotos must not be; the identifier in question
13041 will have no binding. */
13044 tmp
= DECL_NAME (tmp
);
13045 finish_goto_stmt (tmp
);
13049 tmp
= finish_asm_stmt
13050 (ASM_VOLATILE_P (t
),
13051 RECUR (ASM_STRING (t
)),
13052 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13053 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13054 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13055 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13057 tree asm_expr
= tmp
;
13058 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13059 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13060 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13067 stmt
= begin_try_block ();
13068 RECUR (TRY_STMTS (t
));
13069 finish_cleanup_try_block (stmt
);
13070 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13074 tree compound_stmt
= NULL_TREE
;
13076 if (FN_TRY_BLOCK_P (t
))
13077 stmt
= begin_function_try_block (&compound_stmt
);
13079 stmt
= begin_try_block ();
13081 RECUR (TRY_STMTS (t
));
13083 if (FN_TRY_BLOCK_P (t
))
13084 finish_function_try_block (stmt
);
13086 finish_try_block (stmt
);
13088 RECUR (TRY_HANDLERS (t
));
13089 if (FN_TRY_BLOCK_P (t
))
13090 finish_function_handler_sequence (stmt
, compound_stmt
);
13092 finish_handler_sequence (stmt
);
13098 tree decl
= HANDLER_PARMS (t
);
13102 decl
= tsubst (decl
, args
, complain
, in_decl
);
13103 /* Prevent instantiate_decl from trying to instantiate
13104 this variable. We've already done all that needs to be
13106 if (decl
!= error_mark_node
)
13107 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13109 stmt
= begin_handler ();
13110 finish_handler_parms (decl
, stmt
);
13111 RECUR (HANDLER_BODY (t
));
13112 finish_handler (stmt
);
13117 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13118 if (CLASS_TYPE_P (tmp
))
13120 /* Local classes are not independent templates; they are
13121 instantiated along with their containing function. And this
13122 way we don't have to deal with pushing out of one local class
13123 to instantiate a member of another local class. */
13125 /* Closures are handled by the LAMBDA_EXPR. */
13126 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13127 complete_type (tmp
);
13128 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13129 if (!DECL_ARTIFICIAL (fn
))
13130 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13134 case STATIC_ASSERT
:
13138 ++c_inhibit_evaluation_warnings
;
13140 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13143 /*integral_constant_expression_p=*/true);
13144 --c_inhibit_evaluation_warnings
;
13146 finish_static_assert (condition
,
13147 STATIC_ASSERT_MESSAGE (t
),
13148 STATIC_ASSERT_SOURCE_LOCATION (t
),
13149 /*member_p=*/false);
13154 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13155 args
, complain
, in_decl
);
13156 stmt
= begin_omp_parallel ();
13157 RECUR (OMP_PARALLEL_BODY (t
));
13158 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13159 = OMP_PARALLEL_COMBINED (t
);
13163 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13164 args
, complain
, in_decl
);
13165 stmt
= begin_omp_task ();
13166 RECUR (OMP_TASK_BODY (t
));
13167 finish_omp_task (tmp
, stmt
);
13172 tree clauses
, body
, pre_body
;
13173 tree declv
, initv
, condv
, incrv
;
13176 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13177 args
, complain
, in_decl
);
13178 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13179 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13180 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13181 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13183 stmt
= begin_omp_structured_block ();
13185 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13186 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13187 &clauses
, args
, complain
, in_decl
,
13188 integral_constant_expression_p
);
13190 pre_body
= push_stmt_list ();
13191 RECUR (OMP_FOR_PRE_BODY (t
));
13192 pre_body
= pop_stmt_list (pre_body
);
13194 body
= push_stmt_list ();
13195 RECUR (OMP_FOR_BODY (t
));
13196 body
= pop_stmt_list (body
);
13198 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13199 body
, pre_body
, clauses
);
13201 add_stmt (finish_omp_structured_block (stmt
));
13207 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13208 stmt
= push_stmt_list ();
13209 RECUR (OMP_BODY (t
));
13210 stmt
= pop_stmt_list (stmt
);
13213 OMP_BODY (t
) = stmt
;
13214 OMP_CLAUSES (t
) = tmp
;
13222 stmt
= push_stmt_list ();
13223 RECUR (OMP_BODY (t
));
13224 stmt
= pop_stmt_list (stmt
);
13227 OMP_BODY (t
) = stmt
;
13232 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13233 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13235 tree op1
= TREE_OPERAND (t
, 1);
13236 tree rhs1
= NULL_TREE
;
13238 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13240 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13241 op1
= TREE_OPERAND (op1
, 1);
13243 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13244 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13245 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13246 NULL_TREE
, NULL_TREE
, rhs1
);
13250 tree op1
= TREE_OPERAND (t
, 1);
13251 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13252 tree rhs1
= NULL_TREE
;
13253 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13254 enum tree_code opcode
= NOP_EXPR
;
13255 if (code
== OMP_ATOMIC_READ
)
13257 v
= RECUR (TREE_OPERAND (op1
, 0));
13258 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13260 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13261 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13263 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13264 v
= RECUR (TREE_OPERAND (op1
, 0));
13265 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13266 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13268 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13269 op11
= TREE_OPERAND (op11
, 1);
13271 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13272 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13273 opcode
= TREE_CODE (op11
);
13278 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13279 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13281 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13285 case TRANSACTION_EXPR
:
13288 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13289 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13291 if (TRANSACTION_EXPR_IS_STMT (t
))
13293 tree body
= TRANSACTION_EXPR_BODY (t
);
13294 tree noex
= NULL_TREE
;
13295 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13297 noex
= MUST_NOT_THROW_COND (body
);
13298 if (noex
== NULL_TREE
)
13299 noex
= boolean_true_node
;
13300 body
= TREE_OPERAND (body
, 0);
13302 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13304 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13308 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13309 RECUR (TRANSACTION_EXPR_BODY (t
)),
13316 case MUST_NOT_THROW_EXPR
:
13317 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13318 RECUR (MUST_NOT_THROW_COND (t
))));
13320 case EXPR_PACK_EXPANSION
:
13321 error ("invalid use of pack expansion expression");
13322 RETURN (error_mark_node
);
13324 case NONTYPE_ARGUMENT_PACK
:
13325 error ("use %<...%> to expand argument pack");
13326 RETURN (error_mark_node
);
13328 case COMPOUND_EXPR
:
13329 tmp
= RECUR (TREE_OPERAND (t
, 0));
13330 if (tmp
== NULL_TREE
)
13331 /* If the first operand was a statement, we're done with it. */
13332 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13333 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13334 RECUR (TREE_OPERAND (t
, 1)),
13338 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13340 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13341 /*function_p=*/false,
13342 integral_constant_expression_p
));
13345 RETURN (NULL_TREE
);
13347 input_location
= loc
;
13353 /* T is a postfix-expression that is not being used in a function
13354 call. Return the substituted version of T. */
13357 tsubst_non_call_postfix_expression (tree t
, tree args
,
13358 tsubst_flags_t complain
,
13361 if (TREE_CODE (t
) == SCOPE_REF
)
13362 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13363 /*done=*/false, /*address_p=*/false);
13365 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13366 /*function_p=*/false,
13367 /*integral_constant_expression_p=*/false);
13372 /* Like tsubst but deals with expressions and performs semantic
13373 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13376 tsubst_copy_and_build (tree t
,
13378 tsubst_flags_t complain
,
13381 bool integral_constant_expression_p
)
13383 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13384 #define RECUR(NODE) \
13385 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13386 /*function_p=*/false, \
13387 integral_constant_expression_p)
13392 if (t
== NULL_TREE
|| t
== error_mark_node
)
13395 loc
= input_location
;
13396 if (EXPR_HAS_LOCATION (t
))
13397 input_location
= EXPR_LOCATION (t
);
13399 switch (TREE_CODE (t
))
13403 /* Fall through. */
13404 case IDENTIFIER_NODE
:
13408 bool non_integral_constant_expression_p
;
13409 const char *error_msg
;
13411 if (IDENTIFIER_TYPENAME_P (t
))
13413 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13414 t
= mangle_conv_op_name_for_type (new_type
);
13417 /* Look up the name. */
13418 decl
= lookup_name (t
);
13420 /* By convention, expressions use ERROR_MARK_NODE to indicate
13421 failure, not NULL_TREE. */
13422 if (decl
== NULL_TREE
)
13423 decl
= error_mark_node
;
13425 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13427 integral_constant_expression_p
,
13428 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13429 &non_integral_constant_expression_p
,
13430 /*template_p=*/false,
13432 /*address_p=*/false,
13433 /*template_arg_p=*/false,
13438 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13440 if (complain
& tf_error
)
13441 unqualified_name_lookup_error (decl
);
13442 decl
= error_mark_node
;
13447 case TEMPLATE_ID_EXPR
:
13450 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13451 tree targs
= TREE_OPERAND (t
, 1);
13454 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13456 if (TREE_CODE (templ
) == COMPONENT_REF
)
13458 object
= TREE_OPERAND (templ
, 0);
13459 templ
= TREE_OPERAND (templ
, 1);
13462 object
= NULL_TREE
;
13463 templ
= lookup_template_function (templ
, targs
);
13466 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13467 object
, templ
, NULL_TREE
));
13469 RETURN (baselink_for_fns (templ
));
13474 tree r
= RECUR (TREE_OPERAND (t
, 0));
13476 if (REFERENCE_REF_P (t
))
13478 /* A type conversion to reference type will be enclosed in
13479 such an indirect ref, but the substitution of the cast
13480 will have also added such an indirect ref. */
13481 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13482 r
= convert_from_reference (r
);
13485 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13491 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13492 RECUR (TREE_OPERAND (t
, 0))));
13494 case IMPLICIT_CONV_EXPR
:
13496 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13497 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13498 int flags
= LOOKUP_IMPLICIT
;
13499 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13500 flags
= LOOKUP_NORMAL
;
13501 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13508 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13509 RECUR (TREE_OPERAND (t
, 0))));
13512 case REINTERPRET_CAST_EXPR
:
13513 case CONST_CAST_EXPR
:
13514 case DYNAMIC_CAST_EXPR
:
13515 case STATIC_CAST_EXPR
:
13518 tree op
, r
= NULL_TREE
;
13520 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13521 if (integral_constant_expression_p
13522 && !cast_valid_in_integral_constant_expression_p (type
))
13524 if (complain
& tf_error
)
13525 error ("a cast to a type other than an integral or "
13526 "enumeration type cannot appear in a constant-expression");
13527 RETURN (error_mark_node
);
13530 op
= RECUR (TREE_OPERAND (t
, 0));
13532 ++c_inhibit_evaluation_warnings
;
13533 switch (TREE_CODE (t
))
13536 r
= build_functional_cast (type
, op
, complain
);
13538 case REINTERPRET_CAST_EXPR
:
13539 r
= build_reinterpret_cast (type
, op
, complain
);
13541 case CONST_CAST_EXPR
:
13542 r
= build_const_cast (type
, op
, complain
);
13544 case DYNAMIC_CAST_EXPR
:
13545 r
= build_dynamic_cast (type
, op
, complain
);
13547 case STATIC_CAST_EXPR
:
13548 r
= build_static_cast (type
, op
, complain
);
13551 gcc_unreachable ();
13553 --c_inhibit_evaluation_warnings
;
13558 case POSTDECREMENT_EXPR
:
13559 case POSTINCREMENT_EXPR
:
13560 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13561 args
, complain
, in_decl
);
13562 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13564 case PREDECREMENT_EXPR
:
13565 case PREINCREMENT_EXPR
:
13569 case TRUTH_NOT_EXPR
:
13570 case UNARY_PLUS_EXPR
: /* Unary + */
13571 case REALPART_EXPR
:
13572 case IMAGPART_EXPR
:
13573 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13574 RECUR (TREE_OPERAND (t
, 0)), complain
));
13576 case FIX_TRUNC_EXPR
:
13577 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13581 op1
= TREE_OPERAND (t
, 0);
13582 if (TREE_CODE (op1
) == LABEL_DECL
)
13583 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13584 EXPR_LOCATION (op1
)));
13585 if (TREE_CODE (op1
) == SCOPE_REF
)
13586 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13587 /*done=*/true, /*address_p=*/true);
13589 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13591 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13596 case TRUNC_DIV_EXPR
:
13597 case CEIL_DIV_EXPR
:
13598 case FLOOR_DIV_EXPR
:
13599 case ROUND_DIV_EXPR
:
13600 case EXACT_DIV_EXPR
:
13604 case TRUNC_MOD_EXPR
:
13605 case FLOOR_MOD_EXPR
:
13606 case TRUTH_ANDIF_EXPR
:
13607 case TRUTH_ORIF_EXPR
:
13608 case TRUTH_AND_EXPR
:
13609 case TRUTH_OR_EXPR
:
13627 ++c_inhibit_evaluation_warnings
;
13629 r
= build_x_binary_op
13630 (input_location
, TREE_CODE (t
),
13631 RECUR (TREE_OPERAND (t
, 0)),
13632 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13634 : TREE_CODE (TREE_OPERAND (t
, 0))),
13635 RECUR (TREE_OPERAND (t
, 1)),
13636 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13638 : TREE_CODE (TREE_OPERAND (t
, 1))),
13641 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13642 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13644 --c_inhibit_evaluation_warnings
;
13650 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13651 /*address_p=*/false));
13653 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13654 args
, complain
, in_decl
);
13655 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13656 RECUR (TREE_OPERAND (t
, 1)), complain
));
13659 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13660 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13667 op1
= TREE_OPERAND (t
, 0);
13668 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13669 op1
= TREE_TYPE (op1
);
13672 /* When there are no ARGS, we are trying to evaluate a
13673 non-dependent expression from the parser. Trying to do
13674 the substitutions may not work. */
13676 op1
= TREE_TYPE (op1
);
13680 ++cp_unevaluated_operand
;
13681 ++c_inhibit_evaluation_warnings
;
13683 op1
= tsubst (op1
, args
, complain
, in_decl
);
13685 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13686 /*function_p=*/false,
13687 /*integral_constant_expression_p=*/
13689 --cp_unevaluated_operand
;
13690 --c_inhibit_evaluation_warnings
;
13693 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13694 complain
& tf_error
);
13696 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13697 complain
& tf_error
);
13698 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
13700 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
13702 if (!processing_template_decl
&& TYPE_P (op1
))
13704 r
= build_min (SIZEOF_EXPR
, size_type_node
,
13705 build1 (NOP_EXPR
, op1
, error_mark_node
));
13706 SIZEOF_EXPR_TYPE_P (r
) = 1;
13709 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
13710 TREE_SIDE_EFFECTS (r
) = 0;
13711 TREE_READONLY (r
) = 1;
13713 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
13718 case AT_ENCODE_EXPR
:
13720 op1
= TREE_OPERAND (t
, 0);
13721 ++cp_unevaluated_operand
;
13722 ++c_inhibit_evaluation_warnings
;
13723 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13724 /*function_p=*/false,
13725 /*integral_constant_expression_p=*/false);
13726 --cp_unevaluated_operand
;
13727 --c_inhibit_evaluation_warnings
;
13728 RETURN (objc_build_encode_expr (op1
));
13731 case NOEXCEPT_EXPR
:
13732 op1
= TREE_OPERAND (t
, 0);
13733 ++cp_unevaluated_operand
;
13734 ++c_inhibit_evaluation_warnings
;
13735 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13736 /*function_p=*/false,
13737 /*integral_constant_expression_p=*/false);
13738 --cp_unevaluated_operand
;
13739 --c_inhibit_evaluation_warnings
;
13740 RETURN (finish_noexcept_expr (op1
, complain
));
13744 tree r
= build_x_modify_expr
13745 (EXPR_LOCATION (t
),
13746 RECUR (TREE_OPERAND (t
, 0)),
13747 TREE_CODE (TREE_OPERAND (t
, 1)),
13748 RECUR (TREE_OPERAND (t
, 2)),
13750 /* TREE_NO_WARNING must be set if either the expression was
13751 parenthesized or it uses an operator such as >>= rather
13752 than plain assignment. In the former case, it was already
13753 set and must be copied. In the latter case,
13754 build_x_modify_expr sets it and it must not be reset
13756 if (TREE_NO_WARNING (t
))
13757 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13762 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13763 args
, complain
, in_decl
);
13764 /* Remember that there was a reference to this entity. */
13767 RETURN (build_x_arrow (input_location
, op1
, complain
));
13771 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13772 tree init
= RECUR (TREE_OPERAND (t
, 3));
13773 vec
<tree
, va_gc
> *placement_vec
;
13774 vec
<tree
, va_gc
> *init_vec
;
13777 if (placement
== NULL_TREE
)
13778 placement_vec
= NULL
;
13781 placement_vec
= make_tree_vector ();
13782 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13783 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
13786 /* If there was an initializer in the original tree, but it
13787 instantiated to an empty list, then we should pass a
13788 non-NULL empty vector to tell build_new that it was an
13789 empty initializer() rather than no initializer. This can
13790 only happen when the initializer is a pack expansion whose
13791 parameter packs are of length zero. */
13792 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13796 init_vec
= make_tree_vector ();
13797 if (init
== void_zero_node
)
13798 gcc_assert (init_vec
!= NULL
);
13801 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13802 vec_safe_push (init_vec
, TREE_VALUE (init
));
13806 ret
= build_new (&placement_vec
,
13807 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13808 RECUR (TREE_OPERAND (t
, 2)),
13810 NEW_EXPR_USE_GLOBAL (t
),
13813 if (placement_vec
!= NULL
)
13814 release_tree_vector (placement_vec
);
13815 if (init_vec
!= NULL
)
13816 release_tree_vector (init_vec
);
13822 RETURN (delete_sanity
13823 (RECUR (TREE_OPERAND (t
, 0)),
13824 RECUR (TREE_OPERAND (t
, 1)),
13825 DELETE_EXPR_USE_VEC (t
),
13826 DELETE_EXPR_USE_GLOBAL (t
),
13829 case COMPOUND_EXPR
:
13830 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13831 RECUR (TREE_OPERAND (t
, 0)),
13832 RECUR (TREE_OPERAND (t
, 1)),
13838 vec
<tree
, va_gc
> *call_args
;
13839 unsigned int nargs
, i
;
13844 function
= CALL_EXPR_FN (t
);
13845 /* When we parsed the expression, we determined whether or
13846 not Koenig lookup should be performed. */
13847 koenig_p
= KOENIG_LOOKUP_P (t
);
13848 if (TREE_CODE (function
) == SCOPE_REF
)
13850 qualified_p
= true;
13851 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13853 /*address_p=*/false);
13855 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13857 /* Do nothing; calling tsubst_copy_and_build on an identifier
13858 would incorrectly perform unqualified lookup again.
13860 Note that we can also have an IDENTIFIER_NODE if the earlier
13861 unqualified lookup found a member function; in that case
13862 koenig_p will be false and we do want to do the lookup
13863 again to find the instantiated member function.
13865 FIXME but doing that causes c++/15272, so we need to stop
13866 using IDENTIFIER_NODE in that situation. */
13867 qualified_p
= false;
13871 if (TREE_CODE (function
) == COMPONENT_REF
)
13873 tree op
= TREE_OPERAND (function
, 1);
13875 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13876 || (BASELINK_P (op
)
13877 && BASELINK_QUALIFIED_P (op
)));
13880 qualified_p
= false;
13882 if (TREE_CODE (function
) == ADDR_EXPR
13883 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
13884 /* Avoid error about taking the address of a constructor. */
13885 function
= TREE_OPERAND (function
, 0);
13887 function
= tsubst_copy_and_build (function
, args
, complain
,
13890 integral_constant_expression_p
);
13892 if (BASELINK_P (function
))
13893 qualified_p
= true;
13896 nargs
= call_expr_nargs (t
);
13897 call_args
= make_tree_vector ();
13898 for (i
= 0; i
< nargs
; ++i
)
13900 tree arg
= CALL_EXPR_ARG (t
, i
);
13902 if (!PACK_EXPANSION_P (arg
))
13903 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
13906 /* Expand the pack expansion and push each entry onto
13908 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13909 if (TREE_CODE (arg
) == TREE_VEC
)
13911 unsigned int len
, j
;
13913 len
= TREE_VEC_LENGTH (arg
);
13914 for (j
= 0; j
< len
; ++j
)
13916 tree value
= TREE_VEC_ELT (arg
, j
);
13917 if (value
!= NULL_TREE
)
13918 value
= convert_from_reference (value
);
13919 vec_safe_push (call_args
, value
);
13924 /* A partial substitution. Add one entry. */
13925 vec_safe_push (call_args
, arg
);
13930 /* We do not perform argument-dependent lookup if normal
13931 lookup finds a non-function, in accordance with the
13932 expected resolution of DR 218. */
13934 && ((is_overloaded_fn (function
)
13935 /* If lookup found a member function, the Koenig lookup is
13936 not appropriate, even if an unqualified-name was used
13937 to denote the function. */
13938 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13939 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13940 /* Only do this when substitution turns a dependent call
13941 into a non-dependent call. */
13942 && type_dependent_expression_p_push (t
)
13943 && !any_type_dependent_arguments_p (call_args
))
13944 function
= perform_koenig_lookup (function
, call_args
, false,
13947 if (TREE_CODE (function
) == IDENTIFIER_NODE
13948 && !any_type_dependent_arguments_p (call_args
))
13950 if (koenig_p
&& (complain
& tf_warning_or_error
))
13952 /* For backwards compatibility and good diagnostics, try
13953 the unqualified lookup again if we aren't in SFINAE
13955 tree unq
= (tsubst_copy_and_build
13956 (function
, args
, complain
, in_decl
, true,
13957 integral_constant_expression_p
));
13958 if (unq
== error_mark_node
)
13959 RETURN (error_mark_node
);
13961 if (unq
!= function
)
13964 if (TREE_CODE (fn
) == INDIRECT_REF
)
13965 fn
= TREE_OPERAND (fn
, 0);
13966 if (TREE_CODE (fn
) == COMPONENT_REF
)
13967 fn
= TREE_OPERAND (fn
, 1);
13968 if (is_overloaded_fn (fn
))
13969 fn
= get_first_fn (fn
);
13970 permerror (EXPR_LOC_OR_HERE (t
),
13971 "%qD was not declared in this scope, "
13972 "and no declarations were found by "
13973 "argument-dependent lookup at the point "
13974 "of instantiation", function
);
13976 /* Can't say anything more. */;
13977 else if (DECL_CLASS_SCOPE_P (fn
))
13979 inform (EXPR_LOC_OR_HERE (t
),
13980 "declarations in dependent base %qT are "
13981 "not found by unqualified lookup",
13982 DECL_CLASS_CONTEXT (fn
));
13983 if (current_class_ptr
)
13984 inform (EXPR_LOC_OR_HERE (t
),
13985 "use %<this->%D%> instead", function
);
13987 inform (EXPR_LOC_OR_HERE (t
),
13988 "use %<%T::%D%> instead",
13989 current_class_name
, function
);
13992 inform (0, "%q+D declared here, later in the "
13993 "translation unit", fn
);
13997 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13999 if (complain
& tf_error
)
14000 unqualified_name_lookup_error (function
);
14001 release_tree_vector (call_args
);
14002 RETURN (error_mark_node
);
14006 /* Remember that there was a reference to this entity. */
14007 if (DECL_P (function
))
14008 mark_used (function
);
14010 if (TREE_CODE (function
) == OFFSET_REF
)
14011 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14013 else if (TREE_CODE (function
) == COMPONENT_REF
)
14015 tree instance
= TREE_OPERAND (function
, 0);
14016 tree fn
= TREE_OPERAND (function
, 1);
14018 if (processing_template_decl
14019 && (type_dependent_expression_p (instance
)
14020 || (!BASELINK_P (fn
)
14021 && TREE_CODE (fn
) != FIELD_DECL
)
14022 || type_dependent_expression_p (fn
)
14023 || any_type_dependent_arguments_p (call_args
)))
14024 ret
= build_nt_call_vec (function
, call_args
);
14025 else if (!BASELINK_P (fn
))
14026 ret
= finish_call_expr (function
, &call_args
,
14027 /*disallow_virtual=*/false,
14028 /*koenig_p=*/false,
14031 ret
= (build_new_method_call
14033 &call_args
, NULL_TREE
,
14034 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14039 ret
= finish_call_expr (function
, &call_args
,
14040 /*disallow_virtual=*/qualified_p
,
14044 release_tree_vector (call_args
);
14051 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14054 if (TREE_CODE (cond
) == INTEGER_CST
)
14056 if (integer_zerop (cond
))
14058 ++c_inhibit_evaluation_warnings
;
14059 exp1
= RECUR (TREE_OPERAND (t
, 1));
14060 --c_inhibit_evaluation_warnings
;
14061 exp2
= RECUR (TREE_OPERAND (t
, 2));
14065 exp1
= RECUR (TREE_OPERAND (t
, 1));
14066 ++c_inhibit_evaluation_warnings
;
14067 exp2
= RECUR (TREE_OPERAND (t
, 2));
14068 --c_inhibit_evaluation_warnings
;
14073 exp1
= RECUR (TREE_OPERAND (t
, 1));
14074 exp2
= RECUR (TREE_OPERAND (t
, 2));
14077 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14078 cond
, exp1
, exp2
, complain
));
14081 case PSEUDO_DTOR_EXPR
:
14082 RETURN (finish_pseudo_destructor_expr
14083 (RECUR (TREE_OPERAND (t
, 0)),
14084 RECUR (TREE_OPERAND (t
, 1)),
14085 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
14089 tree purpose
, value
, chain
;
14091 if (t
== void_list_node
)
14094 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14095 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14097 /* We have pack expansions, so expand those and
14098 create a new list out of it. */
14099 tree purposevec
= NULL_TREE
;
14100 tree valuevec
= NULL_TREE
;
14104 /* Expand the argument expressions. */
14105 if (TREE_PURPOSE (t
))
14106 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14107 complain
, in_decl
);
14108 if (TREE_VALUE (t
))
14109 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14110 complain
, in_decl
);
14112 /* Build the rest of the list. */
14113 chain
= TREE_CHAIN (t
);
14114 if (chain
&& chain
!= void_type_node
)
14115 chain
= RECUR (chain
);
14117 /* Determine the number of arguments. */
14118 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14120 len
= TREE_VEC_LENGTH (purposevec
);
14121 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14123 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14124 len
= TREE_VEC_LENGTH (valuevec
);
14127 /* Since we only performed a partial substitution into
14128 the argument pack, we only RETURN (a single list
14130 if (purposevec
== TREE_PURPOSE (t
)
14131 && valuevec
== TREE_VALUE (t
)
14132 && chain
== TREE_CHAIN (t
))
14135 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14138 /* Convert the argument vectors into a TREE_LIST */
14142 /* Grab the Ith values. */
14144 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14147 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14150 /* Build the list (backwards). */
14151 chain
= tree_cons (purpose
, value
, chain
);
14157 purpose
= TREE_PURPOSE (t
);
14159 purpose
= RECUR (purpose
);
14160 value
= TREE_VALUE (t
);
14162 value
= RECUR (value
);
14163 chain
= TREE_CHAIN (t
);
14164 if (chain
&& chain
!= void_type_node
)
14165 chain
= RECUR (chain
);
14166 if (purpose
== TREE_PURPOSE (t
)
14167 && value
== TREE_VALUE (t
)
14168 && chain
== TREE_CHAIN (t
))
14170 RETURN (tree_cons (purpose
, value
, chain
));
14173 case COMPONENT_REF
:
14179 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14180 args
, complain
, in_decl
);
14181 /* Remember that there was a reference to this entity. */
14182 if (DECL_P (object
))
14183 mark_used (object
);
14184 object_type
= TREE_TYPE (object
);
14186 member
= TREE_OPERAND (t
, 1);
14187 if (BASELINK_P (member
))
14188 member
= tsubst_baselink (member
,
14189 non_reference (TREE_TYPE (object
)),
14190 args
, complain
, in_decl
);
14192 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14193 if (member
== error_mark_node
)
14194 RETURN (error_mark_node
);
14196 if (type_dependent_expression_p (object
))
14197 /* We can't do much here. */;
14198 else if (!CLASS_TYPE_P (object_type
))
14200 if (scalarish_type_p (object_type
))
14202 tree s
= NULL_TREE
;
14203 tree dtor
= member
;
14205 if (TREE_CODE (dtor
) == SCOPE_REF
)
14207 s
= TREE_OPERAND (dtor
, 0);
14208 dtor
= TREE_OPERAND (dtor
, 1);
14210 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14212 dtor
= TREE_OPERAND (dtor
, 0);
14214 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
14218 else if (TREE_CODE (member
) == SCOPE_REF
14219 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14221 /* Lookup the template functions now that we know what the
14223 tree scope
= TREE_OPERAND (member
, 0);
14224 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14225 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14226 member
= lookup_qualified_name (scope
, tmpl
,
14227 /*is_type_p=*/false,
14228 /*complain=*/false);
14229 if (BASELINK_P (member
))
14231 BASELINK_FUNCTIONS (member
)
14232 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14234 member
= (adjust_result_of_qualified_name_lookup
14235 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14240 qualified_name_lookup_error (scope
, tmpl
, member
,
14242 RETURN (error_mark_node
);
14245 else if (TREE_CODE (member
) == SCOPE_REF
14246 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14247 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14249 if (complain
& tf_error
)
14251 if (TYPE_P (TREE_OPERAND (member
, 0)))
14252 error ("%qT is not a class or namespace",
14253 TREE_OPERAND (member
, 0));
14255 error ("%qD is not a class or namespace",
14256 TREE_OPERAND (member
, 0));
14258 RETURN (error_mark_node
);
14260 else if (TREE_CODE (member
) == FIELD_DECL
)
14261 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14263 RETURN (finish_class_member_access_expr (object
, member
,
14264 /*template_p=*/false,
14269 RETURN (build_throw
14270 (RECUR (TREE_OPERAND (t
, 0))));
14274 vec
<constructor_elt
, va_gc
> *n
;
14275 constructor_elt
*ce
;
14276 unsigned HOST_WIDE_INT idx
;
14277 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14278 bool process_index_p
;
14280 bool need_copy_p
= false;
14283 if (type
== error_mark_node
)
14284 RETURN (error_mark_node
);
14286 /* digest_init will do the wrong thing if we let it. */
14287 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14290 /* We do not want to process the index of aggregate
14291 initializers as they are identifier nodes which will be
14292 looked up by digest_init. */
14293 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14295 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14296 newlen
= vec_safe_length (n
);
14297 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14299 if (ce
->index
&& process_index_p
)
14300 ce
->index
= RECUR (ce
->index
);
14302 if (PACK_EXPANSION_P (ce
->value
))
14304 /* Substitute into the pack expansion. */
14305 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14308 if (ce
->value
== error_mark_node
14309 || PACK_EXPANSION_P (ce
->value
))
14311 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14312 /* Just move the argument into place. */
14313 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14316 /* Update the length of the final CONSTRUCTOR
14317 arguments vector, and note that we will need to
14319 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14320 need_copy_p
= true;
14324 ce
->value
= RECUR (ce
->value
);
14329 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14331 vec_alloc (n
, newlen
);
14332 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14334 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14336 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14337 for (i
= 0; i
< len
; ++i
)
14338 CONSTRUCTOR_APPEND_ELT (n
, 0,
14339 TREE_VEC_ELT (ce
->value
, i
));
14342 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14346 r
= build_constructor (init_list_type_node
, n
);
14347 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14349 if (TREE_HAS_CONSTRUCTOR (t
))
14350 RETURN (finish_compound_literal (type
, r
, complain
));
14352 TREE_TYPE (r
) = type
;
14358 tree operand_0
= TREE_OPERAND (t
, 0);
14359 if (TYPE_P (operand_0
))
14361 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14362 RETURN (get_typeid (operand_0
, complain
));
14366 operand_0
= RECUR (operand_0
);
14367 RETURN (build_typeid (operand_0
, complain
));
14378 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14380 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14381 /* If the original type was a reference, we'll be wrapped in
14382 the appropriate INDIRECT_REF. */
14383 r
= convert_from_reference (r
);
14388 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14389 RECUR (TREE_OPERAND (t
, 0)),
14390 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14392 case OFFSETOF_EXPR
:
14393 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14397 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14398 complain
, in_decl
);
14400 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14402 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14404 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14409 tree old_stmt_expr
= cur_stmt_expr
;
14410 tree stmt_expr
= begin_stmt_expr ();
14412 cur_stmt_expr
= stmt_expr
;
14413 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14414 integral_constant_expression_p
);
14415 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14416 cur_stmt_expr
= old_stmt_expr
;
14418 /* If the resulting list of expression statement is empty,
14419 fold it further into void_zero_node. */
14420 if (empty_expr_stmt_p (stmt_expr
))
14421 stmt_expr
= void_zero_node
;
14423 RETURN (stmt_expr
);
14428 tree r
= build_lambda_expr ();
14430 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14431 LAMBDA_EXPR_CLOSURE (r
) = type
;
14432 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14434 LAMBDA_EXPR_LOCATION (r
)
14435 = LAMBDA_EXPR_LOCATION (t
);
14436 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14437 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14438 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14439 LAMBDA_EXPR_DISCRIMINATOR (r
)
14440 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14441 /* For a function scope, we want to use tsubst so that we don't
14442 complain about referring to an auto function before its return
14443 type has been deduced. Otherwise, we want to use tsubst_copy so
14444 that we look up the existing field/parameter/variable rather
14445 than build a new one. */
14446 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14447 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14448 scope
= tsubst (scope
, args
, complain
, in_decl
);
14449 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14451 /* Look up the parameter we want directly, as tsubst_copy
14452 doesn't do what we need. */
14453 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14454 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14455 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14456 parm
= DECL_CHAIN (parm
);
14458 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14459 if (DECL_CONTEXT (scope
) == NULL_TREE
)
14460 DECL_CONTEXT (scope
) = fn
;
14463 scope
= RECUR (scope
);
14464 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14465 LAMBDA_EXPR_RETURN_TYPE (r
)
14466 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14468 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14469 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14471 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14472 determine_visibility (TYPE_NAME (type
));
14473 /* Now that we know visibility, instantiate the type so we have a
14474 declaration of the op() for later calls to lambda_function. */
14475 complete_type (type
);
14477 /* The capture list refers to closure members, so this needs to
14478 wait until after we finish instantiating the type. Also keep
14479 any captures that may have been added during instantiation. */
14480 LAMBDA_EXPR_CAPTURE_LIST (r
)
14481 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
)),
14482 LAMBDA_EXPR_CAPTURE_LIST (r
));
14483 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14485 RETURN (build_lambda_object (r
));
14489 /* We can get here for a constant initializer of non-dependent type.
14490 FIXME stop folding in cp_parser_initializer_clause. */
14492 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14497 case TRANSACTION_EXPR
:
14498 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14499 integral_constant_expression_p
));
14502 /* Handle Objective-C++ constructs, if appropriate. */
14505 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14506 in_decl
, /*function_p=*/false);
14510 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14516 input_location
= loc
;
14520 /* Verify that the instantiated ARGS are valid. For type arguments,
14521 make sure that the type's linkage is ok. For non-type arguments,
14522 make sure they are constants if they are integral or enumerations.
14523 Emit an error under control of COMPLAIN, and return TRUE on error. */
14526 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14528 if (dependent_template_arg_p (t
))
14530 if (ARGUMENT_PACK_P (t
))
14532 tree vec
= ARGUMENT_PACK_ARGS (t
);
14533 int len
= TREE_VEC_LENGTH (vec
);
14534 bool result
= false;
14537 for (i
= 0; i
< len
; ++i
)
14538 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14542 else if (TYPE_P (t
))
14544 /* [basic.link]: A name with no linkage (notably, the name
14545 of a class or enumeration declared in a local scope)
14546 shall not be used to declare an entity with linkage.
14547 This implies that names with no linkage cannot be used as
14550 DR 757 relaxes this restriction for C++0x. */
14551 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14552 : no_linkage_check (t
, /*relaxed_p=*/false));
14556 /* DR 488 makes use of a type with no linkage cause
14557 type deduction to fail. */
14558 if (complain
& tf_error
)
14560 if (TYPE_ANONYMOUS_P (nt
))
14561 error ("%qT is/uses anonymous type", t
);
14563 error ("template argument for %qD uses local type %qT",
14568 /* In order to avoid all sorts of complications, we do not
14569 allow variably-modified types as template arguments. */
14570 else if (variably_modified_type_p (t
, NULL_TREE
))
14572 if (complain
& tf_error
)
14573 error ("%qT is a variably modified type", t
);
14577 /* Class template and alias template arguments should be OK. */
14578 else if (DECL_TYPE_TEMPLATE_P (t
))
14580 /* A non-type argument of integral or enumerated type must be a
14582 else if (TREE_TYPE (t
)
14583 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14584 && !TREE_CONSTANT (t
))
14586 if (complain
& tf_error
)
14587 error ("integral expression %qE is not constant", t
);
14594 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14596 int ix
, len
= DECL_NTPARMS (tmpl
);
14597 bool result
= false;
14599 for (ix
= 0; ix
!= len
; ix
++)
14601 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14604 if (result
&& (complain
& tf_error
))
14605 error (" trying to instantiate %qD", tmpl
);
14609 /* We're out of SFINAE context now, so generate diagnostics for the access
14610 errors we saw earlier when instantiating D from TMPL and ARGS. */
14613 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14615 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14616 tree type
= TREE_TYPE (pattern
);
14617 location_t loc
= input_location
;
14619 push_access_scope (d
);
14620 push_deferring_access_checks (dk_no_deferred
);
14621 input_location
= DECL_SOURCE_LOCATION (pattern
);
14622 tsubst (type
, args
, tf_warning_or_error
, d
);
14623 input_location
= loc
;
14624 pop_deferring_access_checks ();
14625 pop_access_scope (d
);
14628 /* Instantiate the indicated variable, function, or alias template TMPL with
14629 the template arguments in TARG_PTR. */
14632 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14634 tree targ_ptr
= orig_args
;
14638 bool access_ok
= true;
14640 if (tmpl
== error_mark_node
)
14641 return error_mark_node
;
14643 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14645 /* If this function is a clone, handle it specially. */
14646 if (DECL_CLONED_FUNCTION_P (tmpl
))
14651 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14652 DECL_CLONED_FUNCTION. */
14653 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14654 targ_ptr
, complain
);
14655 if (spec
== error_mark_node
)
14656 return error_mark_node
;
14658 /* Look for the clone. */
14659 FOR_EACH_CLONE (clone
, spec
)
14660 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14662 /* We should always have found the clone by now. */
14663 gcc_unreachable ();
14667 /* Check to see if we already have this specialization. */
14668 gen_tmpl
= most_general_template (tmpl
);
14669 if (tmpl
!= gen_tmpl
)
14670 /* The TMPL is a partial instantiation. To get a full set of
14671 arguments we must add the arguments used to perform the
14672 partial instantiation. */
14673 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14676 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14677 but it doesn't seem to be on the hot path. */
14678 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14680 gcc_assert (tmpl
== gen_tmpl
14681 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14683 || fndecl
== NULL_TREE
);
14685 if (spec
!= NULL_TREE
)
14687 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14689 if (complain
& tf_error
)
14690 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14691 return error_mark_node
;
14696 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14698 return error_mark_node
;
14700 /* We are building a FUNCTION_DECL, during which the access of its
14701 parameters and return types have to be checked. However this
14702 FUNCTION_DECL which is the desired context for access checking
14703 is not built yet. We solve this chicken-and-egg problem by
14704 deferring all checks until we have the FUNCTION_DECL. */
14705 push_deferring_access_checks (dk_deferred
);
14707 /* Instantiation of the function happens in the context of the function
14708 template, not the context of the overload resolution we're doing. */
14709 push_to_top_level ();
14710 /* If there are dependent arguments, e.g. because we're doing partial
14711 ordering, make sure processing_template_decl stays set. */
14712 if (uses_template_parms (targ_ptr
))
14713 ++processing_template_decl
;
14714 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14716 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14717 complain
, gen_tmpl
);
14718 push_nested_class (ctx
);
14720 /* Substitute template parameters to obtain the specialization. */
14721 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14722 targ_ptr
, complain
, gen_tmpl
);
14723 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14724 pop_nested_class ();
14725 pop_from_top_level ();
14727 if (fndecl
== error_mark_node
)
14729 pop_deferring_access_checks ();
14730 return error_mark_node
;
14733 /* The DECL_TI_TEMPLATE should always be the immediate parent
14734 template, not the most general template. */
14735 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14737 /* Now we know the specialization, compute access previously
14739 push_access_scope (fndecl
);
14740 if (!perform_deferred_access_checks (complain
))
14742 pop_access_scope (fndecl
);
14743 pop_deferring_access_checks ();
14745 /* If we've just instantiated the main entry point for a function,
14746 instantiate all the alternate entry points as well. We do this
14747 by cloning the instantiation of the main entry point, not by
14748 instantiating the template clones. */
14749 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14750 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14754 if (!(complain
& tf_error
))
14756 /* Remember to reinstantiate when we're out of SFINAE so the user
14757 can see the errors. */
14758 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14760 return error_mark_node
;
14765 /* Wrapper for instantiate_template_1. */
14768 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14771 timevar_push (TV_TEMPLATE_INST
);
14772 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14773 timevar_pop (TV_TEMPLATE_INST
);
14777 /* Instantiate the alias template TMPL with ARGS. Also push a template
14778 instantiation level, which instantiate_template doesn't do because
14779 functions and variables have sufficient context established by the
14783 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14785 struct pending_template
*old_last_pend
= last_pending_template
;
14786 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14787 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14788 return error_mark_node
;
14789 tree tinst
= build_tree_list (tmpl
, args
);
14790 if (!push_tinst_level (tinst
))
14793 return error_mark_node
;
14797 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
14798 args
, tmpl
, complain
,
14799 /*require_all_args=*/true,
14800 /*use_default_args=*/true);
14802 tree r
= instantiate_template (tmpl
, args
, complain
);
14803 pop_tinst_level ();
14804 /* We can't free this if a pending_template entry or last_error_tinst_level
14805 is pointing at it. */
14806 if (last_pending_template
== old_last_pend
14807 && last_error_tinst_level
== old_error_tinst
)
14813 /* PARM is a template parameter pack for FN. Returns true iff
14814 PARM is used in a deducible way in the argument list of FN. */
14817 pack_deducible_p (tree parm
, tree fn
)
14819 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14820 for (; t
; t
= TREE_CHAIN (t
))
14822 tree type
= TREE_VALUE (t
);
14824 if (!PACK_EXPANSION_P (type
))
14826 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14827 packs
; packs
= TREE_CHAIN (packs
))
14828 if (TREE_VALUE (packs
) == parm
)
14830 /* The template parameter pack is used in a function parameter
14831 pack. If this is the end of the parameter list, the
14832 template parameter pack is deducible. */
14833 if (TREE_CHAIN (t
) == void_list_node
)
14836 /* Otherwise, not. Well, it could be deduced from
14837 a non-pack parameter, but doing so would end up with
14838 a deduction mismatch, so don't bother. */
14842 /* The template parameter pack isn't used in any function parameter
14843 packs, but it might be used deeper, e.g. tuple<Args...>. */
14847 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14848 NARGS elements of the arguments that are being used when calling
14849 it. TARGS is a vector into which the deduced template arguments
14852 Return zero for success, 2 for an incomplete match that doesn't resolve
14853 all the types, and 1 for complete failure. An error message will be
14854 printed only for an incomplete match.
14856 If FN is a conversion operator, or we are trying to produce a specific
14857 specialization, RETURN_TYPE is the return type desired.
14859 The EXPLICIT_TARGS are explicit template arguments provided via a
14862 The parameter STRICT is one of:
14865 We are deducing arguments for a function call, as in
14866 [temp.deduct.call].
14869 We are deducing arguments for a conversion function, as in
14870 [temp.deduct.conv].
14873 We are deducing arguments when doing an explicit instantiation
14874 as in [temp.explicit], when determining an explicit specialization
14875 as in [temp.expl.spec], or when taking the address of a function
14876 template, as in [temp.deduct.funcaddr]. */
14879 fn_type_unification (tree fn
,
14880 tree explicit_targs
,
14883 unsigned int nargs
,
14885 unification_kind_t strict
,
14891 tree decl
= NULL_TREE
;
14892 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14894 static int deduction_depth
;
14895 struct pending_template
*old_last_pend
= last_pending_template
;
14896 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14897 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14899 tree r
= error_mark_node
;
14901 /* Adjust any explicit template arguments before entering the
14902 substitution context. */
14903 if (explicit_targs
)
14906 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14908 /*require_all_args=*/false,
14909 /*use_default_args=*/false));
14910 if (explicit_targs
== error_mark_node
)
14911 return error_mark_node
;
14914 /* In C++0x, it's possible to have a function template whose type depends
14915 on itself recursively. This is most obvious with decltype, but can also
14916 occur with enumeration scope (c++/48969). So we need to catch infinite
14917 recursion and reject the substitution at deduction time; this function
14918 will return error_mark_node for any repeated substitution.
14920 This also catches excessive recursion such as when f<N> depends on
14921 f<N-1> across all integers, and returns error_mark_node for all the
14922 substitutions back up to the initial one.
14924 This is, of course, not reentrant. */
14925 if (excessive_deduction_depth
)
14926 return error_mark_node
;
14927 tinst
= build_tree_list (fn
, targs
);
14928 if (!push_tinst_level (tinst
))
14930 excessive_deduction_depth
= true;
14932 return error_mark_node
;
14935 push_deferring_access_checks (dk_deferred
);
14937 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14939 fntype
= TREE_TYPE (fn
);
14940 if (explicit_targs
)
14944 The specified template arguments must match the template
14945 parameters in kind (i.e., type, nontype, template), and there
14946 must not be more arguments than there are parameters;
14947 otherwise type deduction fails.
14949 Nontype arguments must match the types of the corresponding
14950 nontype template parameters, or must be convertible to the
14951 types of the corresponding nontype parameters as specified in
14952 _temp.arg.nontype_, otherwise type deduction fails.
14954 All references in the function type of the function template
14955 to the corresponding template parameters are replaced by the
14956 specified template argument values. If a substitution in a
14957 template parameter or in the function type of the function
14958 template results in an invalid type, type deduction fails. */
14959 int i
, len
= TREE_VEC_LENGTH (tparms
);
14960 location_t loc
= input_location
;
14961 bool incomplete
= false;
14963 /* Substitute the explicit args into the function type. This is
14964 necessary so that, for instance, explicitly declared function
14965 arguments can match null pointed constants. If we were given
14966 an incomplete set of explicit args, we must not do semantic
14967 processing during substitution as we could create partial
14969 for (i
= 0; i
< len
; i
++)
14971 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14972 bool parameter_pack
= false;
14973 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
14975 /* Dig out the actual parm. */
14976 if (TREE_CODE (parm
) == TYPE_DECL
14977 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14979 parm
= TREE_TYPE (parm
);
14980 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14982 else if (TREE_CODE (parm
) == PARM_DECL
)
14984 parm
= DECL_INITIAL (parm
);
14985 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14988 if (!parameter_pack
&& targ
== NULL_TREE
)
14989 /* No explicit argument for this template parameter. */
14992 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14994 /* Mark the argument pack as "incomplete". We could
14995 still deduce more arguments during unification.
14996 We remove this mark in type_unification_real. */
14999 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15000 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15001 = ARGUMENT_PACK_ARGS (targ
);
15004 /* We have some incomplete argument packs. */
15009 processing_template_decl
+= incomplete
;
15010 input_location
= DECL_SOURCE_LOCATION (fn
);
15011 TREE_VALUE (tinst
) = explicit_targs
;
15012 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15013 complain
| tf_partial
, NULL_TREE
);
15014 TREE_VALUE (tinst
) = targs
;
15015 input_location
= loc
;
15016 processing_template_decl
-= incomplete
;
15018 if (fntype
== error_mark_node
)
15021 /* Throw away these access checks; we'll see them again in
15022 instantiate_template and they might have the wrong
15023 access path at this point. */
15024 pop_deferring_access_checks ();
15025 push_deferring_access_checks (dk_deferred
);
15027 /* Place the explicitly specified arguments in TARGS. */
15028 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15029 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15032 /* Never do unification on the 'this' parameter. */
15033 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15039 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15040 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15041 new_args
[0] = return_type
;
15042 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15047 /* We allow incomplete unification without an error message here
15048 because the standard doesn't seem to explicitly prohibit it. Our
15049 callers must be ready to deal with unification failures in any
15052 pop_tinst_level ();
15053 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15054 targs
, parms
, args
, nargs
, /*subr=*/0,
15055 strict
, flags
, explain_p
);
15056 push_tinst_level (tinst
);
15060 /* Now that we have bindings for all of the template arguments,
15061 ensure that the arguments deduced for the template template
15062 parameters have compatible template parameter lists. We cannot
15063 check this property before we have deduced all template
15064 arguments, because the template parameter types of a template
15065 template parameter might depend on prior template parameters
15066 deduced after the template template parameter. The following
15067 ill-formed example illustrates this issue:
15069 template<typename T, template<T> class C> void f(C<5>, T);
15071 template<int N> struct X {};
15074 f(X<5>(), 5l); // error: template argument deduction fails
15077 The template parameter list of 'C' depends on the template type
15078 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15079 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15080 time that we deduce 'C'. */
15081 if (!template_template_parm_bindings_ok_p
15082 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15084 unify_inconsistent_template_template_parameters (explain_p
);
15088 /* All is well so far. Now, check:
15092 When all template arguments have been deduced, all uses of
15093 template parameters in nondeduced contexts are replaced with
15094 the corresponding deduced argument values. If the
15095 substitution results in an invalid type, as described above,
15096 type deduction fails. */
15097 decl
= instantiate_template (fn
, targs
, complain
);
15098 if (decl
== error_mark_node
)
15101 /* Now perform any access checks encountered during deduction, such as
15102 for default template arguments. */
15103 push_access_scope (decl
);
15104 ok
= perform_deferred_access_checks (complain
);
15105 pop_access_scope (decl
);
15109 /* If we're looking for an exact match, check that what we got
15110 is indeed an exact match. It might not be if some template
15111 parameters are used in non-deduced contexts. */
15112 if (strict
== DEDUCE_EXACT
)
15114 tree substed
= TREE_TYPE (decl
);
15118 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15120 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15121 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15122 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15124 unify_type_mismatch (explain_p
, args
[i
],
15125 TREE_VALUE (sarg
));
15133 pop_deferring_access_checks ();
15135 if (excessive_deduction_depth
)
15137 if (deduction_depth
== 0)
15138 /* Reset once we're all the way out. */
15139 excessive_deduction_depth
= false;
15142 pop_tinst_level ();
15143 /* We can't free this if a pending_template entry or last_error_tinst_level
15144 is pointing at it. */
15145 if (last_pending_template
== old_last_pend
15146 && last_error_tinst_level
== old_error_tinst
)
15152 /* Adjust types before performing type deduction, as described in
15153 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15154 sections are symmetric. PARM is the type of a function parameter
15155 or the return type of the conversion function. ARG is the type of
15156 the argument passed to the call, or the type of the value
15157 initialized with the result of the conversion function.
15158 ARG_EXPR is the original argument expression, which may be null. */
15161 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15175 /* Swap PARM and ARG throughout the remainder of this
15176 function; the handling is precisely symmetric since PARM
15177 will initialize ARG rather than vice versa. */
15185 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15186 too, but here handle it by stripping the reference from PARM
15187 rather than by adding it to ARG. */
15188 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15189 && TYPE_REF_IS_RVALUE (*parm
)
15190 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15191 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15192 && TREE_CODE (*arg
) == REFERENCE_TYPE
15193 && !TYPE_REF_IS_RVALUE (*arg
))
15194 *parm
= TREE_TYPE (*parm
);
15195 /* Nothing else to do in this case. */
15199 gcc_unreachable ();
15202 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15204 /* [temp.deduct.call]
15206 If P is not a reference type:
15208 --If A is an array type, the pointer type produced by the
15209 array-to-pointer standard conversion (_conv.array_) is
15210 used in place of A for type deduction; otherwise,
15212 --If A is a function type, the pointer type produced by
15213 the function-to-pointer standard conversion
15214 (_conv.func_) is used in place of A for type deduction;
15217 --If A is a cv-qualified type, the top level
15218 cv-qualifiers of A's type are ignored for type
15220 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15221 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15222 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15223 *arg
= build_pointer_type (*arg
);
15225 *arg
= TYPE_MAIN_VARIANT (*arg
);
15228 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15229 of the form T&&, where T is a template parameter, and the argument
15230 is an lvalue, T is deduced as A& */
15231 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15232 && TYPE_REF_IS_RVALUE (*parm
)
15233 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15234 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15235 && (arg_expr
? real_lvalue_p (arg_expr
)
15236 /* try_one_overload doesn't provide an arg_expr, but
15237 functions are always lvalues. */
15238 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15239 *arg
= build_reference_type (*arg
);
15241 /* [temp.deduct.call]
15243 If P is a cv-qualified type, the top level cv-qualifiers
15244 of P's type are ignored for type deduction. If P is a
15245 reference type, the type referred to by P is used for
15247 *parm
= TYPE_MAIN_VARIANT (*parm
);
15248 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15250 *parm
= TREE_TYPE (*parm
);
15251 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15254 /* DR 322. For conversion deduction, remove a reference type on parm
15255 too (which has been swapped into ARG). */
15256 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15257 *arg
= TREE_TYPE (*arg
);
15262 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15263 template which does contain any deducible template parameters; check if
15264 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15265 unify_one_argument. */
15268 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15269 int flags
, bool explain_p
)
15274 type
= TREE_TYPE (arg
);
15278 if (same_type_p (parm
, type
))
15279 return unify_success (explain_p
);
15281 if (strict
== DEDUCE_CONV
)
15283 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15284 explain_p
? tf_warning_or_error
: tf_none
))
15285 return unify_success (explain_p
);
15287 else if (strict
!= DEDUCE_EXACT
)
15289 if (can_convert_arg (parm
, type
,
15290 TYPE_P (arg
) ? NULL_TREE
: arg
,
15291 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15292 return unify_success (explain_p
);
15295 if (strict
== DEDUCE_EXACT
)
15296 return unify_type_mismatch (explain_p
, parm
, arg
);
15298 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15301 /* Subroutine of type_unification_real and unify_pack_expansion to
15302 handle unification of a single P/A pair. Parameters are as
15303 for those functions. */
15306 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15307 int subr
, unification_kind_t strict
, int flags
,
15310 tree arg_expr
= NULL_TREE
;
15313 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15314 return unify_invalid (explain_p
);
15315 if (arg
== unknown_type_node
)
15316 /* We can't deduce anything from this, but we might get all the
15317 template args from other function args. */
15318 return unify_success (explain_p
);
15320 /* FIXME uses_deducible_template_parms */
15321 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15322 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15328 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15329 | UNIFY_ALLOW_MORE_CV_QUAL
15330 | UNIFY_ALLOW_DERIVED
);
15334 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15338 arg_strict
= UNIFY_ALLOW_NONE
;
15342 gcc_unreachable ();
15345 /* We only do these transformations if this is the top-level
15346 parameter_type_list in a call or declaration matching; in other
15347 situations (nested function declarators, template argument lists) we
15348 won't be comparing a type to an expression, and we don't do any type
15354 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15355 if (type_unknown_p (arg
))
15357 /* [temp.deduct.type] A template-argument can be
15358 deduced from a pointer to function or pointer
15359 to member function argument if the set of
15360 overloaded functions does not contain function
15361 templates and at most one of a set of
15362 overloaded functions provides a unique
15365 if (resolve_overloaded_unification
15366 (tparms
, targs
, parm
, arg
, strict
,
15367 arg_strict
, explain_p
))
15368 return unify_success (explain_p
);
15369 return unify_overload_resolution_failure (explain_p
, arg
);
15373 arg
= unlowered_expr_type (arg
);
15374 if (arg
== error_mark_node
)
15375 return unify_invalid (explain_p
);
15379 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15382 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15383 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15385 /* For deduction from an init-list we need the actual list. */
15386 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15388 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15391 /* Most parms like fn_type_unification.
15393 If SUBR is 1, we're being called recursively (to unify the
15394 arguments of a function or method parameter of a function
15398 type_unification_real (tree tparms
,
15402 unsigned int xnargs
,
15404 unification_kind_t strict
,
15410 int ntparms
= TREE_VEC_LENGTH (tparms
);
15411 int saw_undeduced
= 0;
15414 unsigned int nargs
;
15417 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15418 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15419 gcc_assert (ntparms
> 0);
15421 /* Reset the number of non-defaulted template arguments contained
15423 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15431 while (parms
&& parms
!= void_list_node
15434 parm
= TREE_VALUE (parms
);
15436 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15437 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15438 /* For a function parameter pack that occurs at the end of the
15439 parameter-declaration-list, the type A of each remaining
15440 argument of the call is compared with the type P of the
15441 declarator-id of the function parameter pack. */
15444 parms
= TREE_CHAIN (parms
);
15446 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15447 /* For a function parameter pack that does not occur at the
15448 end of the parameter-declaration-list, the type of the
15449 parameter pack is a non-deduced context. */
15455 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15461 && parms
!= void_list_node
15462 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15464 /* Unify the remaining arguments with the pack expansion type. */
15466 tree parmvec
= make_tree_vec (1);
15468 /* Allocate a TREE_VEC and copy in all of the arguments */
15469 argvec
= make_tree_vec (nargs
- ia
);
15470 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15471 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15473 /* Copy the parameter into parmvec. */
15474 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15475 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15476 /*subr=*/subr
, explain_p
))
15479 /* Advance to the end of the list of parameters. */
15480 parms
= TREE_CHAIN (parms
);
15483 /* Fail if we've reached the end of the parm list, and more args
15484 are present, and the parm list isn't variadic. */
15485 if (ia
< nargs
&& parms
== void_list_node
)
15486 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15487 /* Fail if parms are left and they don't have default values. */
15488 if (parms
&& parms
!= void_list_node
15489 && TREE_PURPOSE (parms
) == NULL_TREE
)
15491 unsigned int count
= nargs
;
15493 while (p
&& p
!= void_list_node
)
15496 p
= TREE_CHAIN (p
);
15498 return unify_too_few_arguments (explain_p
, ia
, count
);
15503 tsubst_flags_t complain
= (explain_p
15504 ? tf_warning_or_error
15507 for (i
= 0; i
< ntparms
; i
++)
15509 tree targ
= TREE_VEC_ELT (targs
, i
);
15510 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15512 /* Clear the "incomplete" flags on all argument packs now so that
15513 substituting them into later default arguments works. */
15514 if (targ
&& ARGUMENT_PACK_P (targ
))
15516 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15517 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15520 if (targ
|| tparm
== error_mark_node
)
15522 tparm
= TREE_VALUE (tparm
);
15524 /* If this is an undeduced nontype parameter that depends on
15525 a type parameter, try another pass; its type may have been
15526 deduced from a later argument than the one from which
15527 this parameter can be deduced. */
15528 if (TREE_CODE (tparm
) == PARM_DECL
15529 && uses_template_parms (TREE_TYPE (tparm
))
15530 && !saw_undeduced
++)
15533 /* Core issue #226 (C++0x) [temp.deduct]:
15535 If a template argument has not been deduced, its
15536 default template argument, if any, is used.
15538 When we are in C++98 mode, TREE_PURPOSE will either
15539 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15540 to explicitly check cxx_dialect here. */
15541 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15543 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15544 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15545 location_t save_loc
= input_location
;
15547 input_location
= DECL_SOURCE_LOCATION (parm
);
15548 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15549 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15551 input_location
= save_loc
;
15552 if (arg
== error_mark_node
)
15556 TREE_VEC_ELT (targs
, i
) = arg
;
15557 /* The position of the first default template argument,
15558 is also the number of non-defaulted arguments in TARGS.
15560 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15561 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15566 /* If the type parameter is a parameter pack, then it will
15567 be deduced to an empty parameter pack. */
15568 if (template_parameter_pack_p (tparm
))
15572 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15574 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15575 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15576 TREE_CONSTANT (arg
) = 1;
15579 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15581 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15583 TREE_VEC_ELT (targs
, i
) = arg
;
15587 return unify_parameter_deduction_failure (explain_p
, tparm
);
15590 #ifdef ENABLE_CHECKING
15591 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15592 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15595 return unify_success (explain_p
);
15598 /* Subroutine of type_unification_real. Args are like the variables
15599 at the call site. ARG is an overloaded function (or template-id);
15600 we try deducing template args from each of the overloads, and if
15601 only one succeeds, we go with that. Modifies TARGS and returns
15602 true on success. */
15605 resolve_overloaded_unification (tree tparms
,
15609 unification_kind_t strict
,
15613 tree tempargs
= copy_node (targs
);
15615 tree goodfn
= NULL_TREE
;
15618 if (TREE_CODE (arg
) == ADDR_EXPR
)
15620 arg
= TREE_OPERAND (arg
, 0);
15626 if (TREE_CODE (arg
) == COMPONENT_REF
)
15627 /* Handle `&x' where `x' is some static or non-static member
15629 arg
= TREE_OPERAND (arg
, 1);
15631 if (TREE_CODE (arg
) == OFFSET_REF
)
15632 arg
= TREE_OPERAND (arg
, 1);
15634 /* Strip baselink information. */
15635 if (BASELINK_P (arg
))
15636 arg
= BASELINK_FUNCTIONS (arg
);
15638 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15640 /* If we got some explicit template args, we need to plug them into
15641 the affected templates before we try to unify, in case the
15642 explicit args will completely resolve the templates in question. */
15645 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15646 arg
= TREE_OPERAND (arg
, 0);
15648 for (; arg
; arg
= OVL_NEXT (arg
))
15650 tree fn
= OVL_CURRENT (arg
);
15651 tree subargs
, elem
;
15653 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15656 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15657 expl_subargs
, NULL_TREE
, tf_none
,
15658 /*require_all_args=*/true,
15659 /*use_default_args=*/true);
15660 if (subargs
!= error_mark_node
15661 && !any_dependent_template_arguments_p (subargs
))
15663 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15664 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15665 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15666 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15675 /* If no templates (or more than one) are fully resolved by the
15676 explicit arguments, this template-id is a non-deduced context; it
15677 could still be OK if we deduce all template arguments for the
15678 enclosing call through other arguments. */
15682 else if (TREE_CODE (arg
) != OVERLOAD
15683 && TREE_CODE (arg
) != FUNCTION_DECL
)
15684 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15685 -- but the deduction does not succeed because the expression is
15686 not just the function on its own. */
15689 for (; arg
; arg
= OVL_NEXT (arg
))
15690 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15691 TREE_TYPE (OVL_CURRENT (arg
)),
15692 strict
, sub_strict
, addr_p
, explain_p
)
15693 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15695 goodfn
= OVL_CURRENT (arg
);
15699 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15700 to function or pointer to member function argument if the set of
15701 overloaded functions does not contain function templates and at most
15702 one of a set of overloaded functions provides a unique match.
15704 So if we found multiple possibilities, we return success but don't
15705 deduce anything. */
15709 int i
= TREE_VEC_LENGTH (targs
);
15711 if (TREE_VEC_ELT (tempargs
, i
))
15712 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15720 /* Core DR 115: In contexts where deduction is done and fails, or in
15721 contexts where deduction is not done, if a template argument list is
15722 specified and it, along with any default template arguments, identifies
15723 a single function template specialization, then the template-id is an
15724 lvalue for the function template specialization. */
15727 resolve_nondeduced_context (tree orig_expr
)
15729 tree expr
, offset
, baselink
;
15732 if (!type_unknown_p (orig_expr
))
15737 offset
= NULL_TREE
;
15738 baselink
= NULL_TREE
;
15740 if (TREE_CODE (expr
) == ADDR_EXPR
)
15742 expr
= TREE_OPERAND (expr
, 0);
15745 if (TREE_CODE (expr
) == OFFSET_REF
)
15748 expr
= TREE_OPERAND (expr
, 1);
15750 if (BASELINK_P (expr
))
15753 expr
= BASELINK_FUNCTIONS (expr
);
15756 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15759 tree goodfn
= NULL_TREE
;
15761 /* If we got some explicit template args, we need to plug them into
15762 the affected templates before we try to unify, in case the
15763 explicit args will completely resolve the templates in question. */
15765 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15766 tree arg
= TREE_OPERAND (expr
, 0);
15767 tree badfn
= NULL_TREE
;
15768 tree badargs
= NULL_TREE
;
15770 for (; arg
; arg
= OVL_NEXT (arg
))
15772 tree fn
= OVL_CURRENT (arg
);
15773 tree subargs
, elem
;
15775 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15778 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15779 expl_subargs
, NULL_TREE
, tf_none
,
15780 /*require_all_args=*/true,
15781 /*use_default_args=*/true);
15782 if (subargs
!= error_mark_node
15783 && !any_dependent_template_arguments_p (subargs
))
15785 elem
= instantiate_template (fn
, subargs
, tf_none
);
15786 if (elem
== error_mark_node
)
15791 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15800 mark_used (goodfn
);
15803 expr
= build_baselink (BASELINK_BINFO (baselink
),
15804 BASELINK_ACCESS_BINFO (baselink
),
15805 expr
, BASELINK_OPTYPE (baselink
));
15809 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15810 expr
= build_offset_ref (base
, expr
, addr
);
15813 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15816 else if (good
== 0 && badargs
)
15817 /* There were no good options and at least one bad one, so let the
15818 user know what the problem is. */
15819 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15824 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15825 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15826 different overloads deduce different arguments for a given parm.
15827 ADDR_P is true if the expression for which deduction is being
15828 performed was of the form "& fn" rather than simply "fn".
15830 Returns 1 on success. */
15833 try_one_overload (tree tparms
,
15838 unification_kind_t strict
,
15847 if (arg
== error_mark_node
)
15850 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15851 to function or pointer to member function argument if the set of
15852 overloaded functions does not contain function templates and at most
15853 one of a set of overloaded functions provides a unique match.
15855 So if this is a template, just return success. */
15857 if (uses_template_parms (arg
))
15860 if (TREE_CODE (arg
) == METHOD_TYPE
)
15861 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15863 arg
= build_pointer_type (arg
);
15865 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15867 /* We don't copy orig_targs for this because if we have already deduced
15868 some template args from previous args, unify would complain when we
15869 try to deduce a template parameter for the same argument, even though
15870 there isn't really a conflict. */
15871 nargs
= TREE_VEC_LENGTH (targs
);
15872 tempargs
= make_tree_vec (nargs
);
15874 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15877 /* First make sure we didn't deduce anything that conflicts with
15878 explicitly specified args. */
15879 for (i
= nargs
; i
--; )
15881 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15882 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15886 else if (uses_template_parms (elt
))
15887 /* Since we're unifying against ourselves, we will fill in
15888 template args used in the function parm list with our own
15889 template parms. Discard them. */
15890 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15891 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15895 for (i
= nargs
; i
--; )
15897 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15900 TREE_VEC_ELT (targs
, i
) = elt
;
15906 /* PARM is a template class (perhaps with unbound template
15907 parameters). ARG is a fully instantiated type. If ARG can be
15908 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15909 TARGS are as for unify. */
15912 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15915 tree copy_of_targs
;
15917 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15918 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15919 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15922 /* We need to make a new template argument vector for the call to
15923 unify. If we used TARGS, we'd clutter it up with the result of
15924 the attempted unification, even if this class didn't work out.
15925 We also don't want to commit ourselves to all the unifications
15926 we've already done, since unification is supposed to be done on
15927 an argument-by-argument basis. In other words, consider the
15928 following pathological case:
15930 template <int I, int J, int K>
15933 template <int I, int J>
15934 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15936 template <int I, int J, int K>
15937 void f(S<I, J, K>, S<I, I, I>);
15946 Now, by the time we consider the unification involving `s2', we
15947 already know that we must have `f<0, 0, 0>'. But, even though
15948 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15949 because there are two ways to unify base classes of S<0, 1, 2>
15950 with S<I, I, I>. If we kept the already deduced knowledge, we
15951 would reject the possibility I=1. */
15952 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15954 /* If unification failed, we're done. */
15955 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15956 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15962 /* Given a template type PARM and a class type ARG, find the unique
15963 base type in ARG that is an instance of PARM. We do not examine
15964 ARG itself; only its base-classes. If there is not exactly one
15965 appropriate base class, return NULL_TREE. PARM may be the type of
15966 a partial specialization, as well as a plain template type. Used
15969 static enum template_base_result
15970 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15971 bool explain_p
, tree
*result
)
15973 tree rval
= NULL_TREE
;
15976 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15978 binfo
= TYPE_BINFO (complete_type (arg
));
15981 /* The type could not be completed. */
15982 *result
= NULL_TREE
;
15983 return tbr_incomplete_type
;
15986 /* Walk in inheritance graph order. The search order is not
15987 important, and this avoids multiple walks of virtual bases. */
15988 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15990 tree r
= try_class_unification (tparms
, targs
, parm
,
15991 BINFO_TYPE (binfo
), explain_p
);
15995 /* If there is more than one satisfactory baseclass, then:
15999 If they yield more than one possible deduced A, the type
16003 if (rval
&& !same_type_p (r
, rval
))
16005 *result
= NULL_TREE
;
16006 return tbr_ambiguous_baseclass
;
16014 return tbr_success
;
16017 /* Returns the level of DECL, which declares a template parameter. */
16020 template_decl_level (tree decl
)
16022 switch (TREE_CODE (decl
))
16025 case TEMPLATE_DECL
:
16026 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16029 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16032 gcc_unreachable ();
16037 /* Decide whether ARG can be unified with PARM, considering only the
16038 cv-qualifiers of each type, given STRICT as documented for unify.
16039 Returns nonzero iff the unification is OK on that basis. */
16042 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16044 int arg_quals
= cp_type_quals (arg
);
16045 int parm_quals
= cp_type_quals (parm
);
16047 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16048 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16050 /* Although a CVR qualifier is ignored when being applied to a
16051 substituted template parameter ([8.3.2]/1 for example), that
16052 does not allow us to unify "const T" with "int&" because both
16053 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16054 It is ok when we're allowing additional CV qualifiers
16055 at the outer level [14.8.2.1]/3,1st bullet. */
16056 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16057 || TREE_CODE (arg
) == FUNCTION_TYPE
16058 || TREE_CODE (arg
) == METHOD_TYPE
)
16059 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16062 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16063 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16067 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16068 && (arg_quals
& parm_quals
) != parm_quals
)
16071 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16072 && (parm_quals
& arg_quals
) != arg_quals
)
16078 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16080 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16082 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16083 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16084 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16086 *index
= TEMPLATE_TYPE_IDX (parm
);
16087 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16091 *index
= TEMPLATE_PARM_IDX (parm
);
16092 *level
= TEMPLATE_PARM_LEVEL (parm
);
16096 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16098 if (unify (TP, TA, P, A, S, EP)) \
16102 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16103 expansion at the end of PACKED_PARMS. Returns 0 if the type
16104 deduction succeeds, 1 otherwise. STRICT is the same as in
16105 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16106 call argument list. We'll need to adjust the arguments to make them
16107 types. SUBR tells us if this is from a recursive call to
16108 type_unification_real, or for comparing two template argument
16112 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16113 tree packed_args
, unification_kind_t strict
,
16114 bool subr
, bool explain_p
)
16117 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16118 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16119 tree pack
, packs
= NULL_TREE
;
16120 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16121 int len
= TREE_VEC_LENGTH (packed_args
);
16123 /* Determine the parameter packs we will be deducing from the
16124 pattern, and record their current deductions. */
16125 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16126 pack
; pack
= TREE_CHAIN (pack
))
16128 tree parm_pack
= TREE_VALUE (pack
);
16131 /* Determine the index and level of this parameter pack. */
16132 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16134 /* Keep track of the parameter packs and their corresponding
16136 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16137 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16140 /* Loop through all of the arguments that have not yet been
16141 unified and unify each with the pattern. */
16142 for (i
= start
; i
< len
; i
++)
16145 bool any_explicit
= false;
16146 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16148 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16149 or the element of its argument pack at the current index if
16150 this argument was explicitly specified. */
16151 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16155 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16158 if (TREE_VALUE (pack
)
16159 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16160 && (i
< TREE_VEC_LENGTH (pargs
)))
16162 any_explicit
= true;
16163 arg
= TREE_VEC_ELT (pargs
, i
);
16165 TMPL_ARG (targs
, level
, idx
) = arg
;
16168 /* If we had explicit template arguments, substitute them into the
16169 pattern before deduction. */
16172 /* Some arguments might still be unspecified or dependent. */
16174 ++processing_template_decl
;
16175 dependent
= any_dependent_template_arguments_p (targs
);
16177 --processing_template_decl
;
16178 parm
= tsubst (pattern
, targs
,
16179 explain_p
? tf_warning_or_error
: tf_none
,
16182 --processing_template_decl
;
16183 if (parm
== error_mark_node
)
16189 /* Unify the pattern with the current argument. */
16190 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16191 LOOKUP_IMPLICIT
, explain_p
))
16194 /* For each parameter pack, collect the deduced value. */
16195 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16198 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16200 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16201 TMPL_ARG (targs
, level
, idx
);
16205 /* Verify that the results of unification with the parameter packs
16206 produce results consistent with what we've seen before, and make
16207 the deduced argument packs available. */
16208 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16210 tree old_pack
= TREE_VALUE (pack
);
16211 tree new_args
= TREE_TYPE (pack
);
16212 int i
, len
= TREE_VEC_LENGTH (new_args
);
16214 bool nondeduced_p
= false;
16216 /* By default keep the original deduced argument pack.
16217 If necessary, more specific code is going to update the
16218 resulting deduced argument later down in this function. */
16219 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16220 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16222 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16223 actually deduce anything. */
16224 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16225 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16226 nondeduced_p
= true;
16230 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16232 /* If we had fewer function args than explicit template args,
16233 just use the explicits. */
16234 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16235 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16236 if (len
< explicit_len
)
16237 new_args
= explicit_args
;
16243 /* Build the deduced *_ARGUMENT_PACK. */
16244 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16246 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16247 TREE_TYPE (result
) =
16248 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16249 TREE_CONSTANT (result
) = 1;
16252 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16254 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16256 /* Note the deduced argument packs for this parameter
16258 TMPL_ARG (targs
, level
, idx
) = result
;
16260 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16261 && (ARGUMENT_PACK_ARGS (old_pack
)
16262 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16264 /* We only had the explicitly-provided arguments before, but
16265 now we have a complete set of arguments. */
16266 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16268 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16269 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16270 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16274 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16275 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16277 if (!comp_template_args_with_info (old_args
, new_args
,
16278 &bad_old_arg
, &bad_new_arg
))
16279 /* Inconsistent unification of this parameter pack. */
16280 return unify_parameter_pack_inconsistent (explain_p
,
16286 return unify_success (explain_p
);
16289 /* Deduce the value of template parameters. TPARMS is the (innermost)
16290 set of template parameters to a template. TARGS is the bindings
16291 for those template parameters, as determined thus far; TARGS may
16292 include template arguments for outer levels of template parameters
16293 as well. PARM is a parameter to a template function, or a
16294 subcomponent of that parameter; ARG is the corresponding argument.
16295 This function attempts to match PARM with ARG in a manner
16296 consistent with the existing assignments in TARGS. If more values
16297 are deduced, then TARGS is updated.
16299 Returns 0 if the type deduction succeeds, 1 otherwise. The
16300 parameter STRICT is a bitwise or of the following flags:
16303 Require an exact match between PARM and ARG.
16304 UNIFY_ALLOW_MORE_CV_QUAL:
16305 Allow the deduced ARG to be more cv-qualified (by qualification
16306 conversion) than ARG.
16307 UNIFY_ALLOW_LESS_CV_QUAL:
16308 Allow the deduced ARG to be less cv-qualified than ARG.
16309 UNIFY_ALLOW_DERIVED:
16310 Allow the deduced ARG to be a template base class of ARG,
16311 or a pointer to a template base class of the type pointed to by
16313 UNIFY_ALLOW_INTEGER:
16314 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16315 case for more information.
16316 UNIFY_ALLOW_OUTER_LEVEL:
16317 This is the outermost level of a deduction. Used to determine validity
16318 of qualification conversions. A valid qualification conversion must
16319 have const qualified pointers leading up to the inner type which
16320 requires additional CV quals, except at the outer level, where const
16321 is not required [conv.qual]. It would be normal to set this flag in
16322 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16323 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16324 This is the outermost level of a deduction, and PARM can be more CV
16325 qualified at this point.
16326 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16327 This is the outermost level of a deduction, and PARM can be less CV
16328 qualified at this point. */
16331 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16337 int strict_in
= strict
;
16339 /* I don't think this will do the right thing with respect to types.
16340 But the only case I've seen it in so far has been array bounds, where
16341 signedness is the only information lost, and I think that will be
16343 while (TREE_CODE (parm
) == NOP_EXPR
)
16344 parm
= TREE_OPERAND (parm
, 0);
16346 if (arg
== error_mark_node
)
16347 return unify_invalid (explain_p
);
16348 if (arg
== unknown_type_node
16349 || arg
== init_list_type_node
)
16350 /* We can't deduce anything from this, but we might get all the
16351 template args from other function args. */
16352 return unify_success (explain_p
);
16354 /* If PARM uses template parameters, then we can't bail out here,
16355 even if ARG == PARM, since we won't record unifications for the
16356 template parameters. We might need them if we're trying to
16357 figure out which of two things is more specialized. */
16358 if (arg
== parm
&& !uses_template_parms (parm
))
16359 return unify_success (explain_p
);
16361 /* Handle init lists early, so the rest of the function can assume
16362 we're dealing with a type. */
16363 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16367 tree orig_parm
= parm
;
16369 /* Replace T with std::initializer_list<T> for deduction. */
16370 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16371 && flag_deduce_init_list
)
16372 parm
= listify (parm
);
16374 if (!is_std_init_list (parm
))
16375 /* We can only deduce from an initializer list argument if the
16376 parameter is std::initializer_list; otherwise this is a
16377 non-deduced context. */
16378 return unify_success (explain_p
);
16380 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16382 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16384 int elt_strict
= strict
;
16386 if (elt
== error_mark_node
)
16387 return unify_invalid (explain_p
);
16389 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16391 tree type
= TREE_TYPE (elt
);
16392 /* It should only be possible to get here for a call. */
16393 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16394 elt_strict
|= maybe_adjust_types_for_deduction
16395 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16399 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16403 /* If the std::initializer_list<T> deduction worked, replace the
16404 deduced A with std::initializer_list<A>. */
16405 if (orig_parm
!= parm
)
16407 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16408 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16409 targ
= listify (targ
);
16410 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16412 return unify_success (explain_p
);
16415 /* Immediately reject some pairs that won't unify because of
16416 cv-qualification mismatches. */
16417 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16419 /* It is the elements of the array which hold the cv quals of an array
16420 type, and the elements might be template type parms. We'll check
16421 when we recurse. */
16422 && TREE_CODE (arg
) != ARRAY_TYPE
16423 /* We check the cv-qualifiers when unifying with template type
16424 parameters below. We want to allow ARG `const T' to unify with
16425 PARM `T' for example, when computing which of two templates
16426 is more specialized, for example. */
16427 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16428 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16429 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16431 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16432 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16433 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16434 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16435 strict
&= ~UNIFY_ALLOW_DERIVED
;
16436 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16437 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16439 switch (TREE_CODE (parm
))
16441 case TYPENAME_TYPE
:
16443 case UNBOUND_CLASS_TEMPLATE
:
16444 /* In a type which contains a nested-name-specifier, template
16445 argument values cannot be deduced for template parameters used
16446 within the nested-name-specifier. */
16447 return unify_success (explain_p
);
16449 case TEMPLATE_TYPE_PARM
:
16450 case TEMPLATE_TEMPLATE_PARM
:
16451 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16452 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16453 if (tparm
== error_mark_node
)
16454 return unify_invalid (explain_p
);
16456 if (TEMPLATE_TYPE_LEVEL (parm
)
16457 != template_decl_level (tparm
))
16458 /* The PARM is not one we're trying to unify. Just check
16459 to see if it matches ARG. */
16461 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16462 && (is_auto (parm
) ? is_auto (arg
)
16463 : same_type_p (parm
, arg
)))
16464 return unify_success (explain_p
);
16466 return unify_type_mismatch (explain_p
, parm
, arg
);
16468 idx
= TEMPLATE_TYPE_IDX (parm
);
16469 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16470 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16471 if (tparm
== error_mark_node
)
16472 return unify_invalid (explain_p
);
16474 /* Check for mixed types and values. */
16475 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16476 && TREE_CODE (tparm
) != TYPE_DECL
)
16477 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16478 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16479 gcc_unreachable ();
16481 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16483 /* ARG must be constructed from a template class or a template
16484 template parameter. */
16485 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16486 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16487 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16490 tree parmvec
= TYPE_TI_ARGS (parm
);
16491 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16492 tree full_argvec
= add_to_template_args (targs
, argvec
);
16494 = DECL_INNERMOST_TEMPLATE_PARMS
16495 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16497 int parm_variadic_p
= 0;
16499 /* The resolution to DR150 makes clear that default
16500 arguments for an N-argument may not be used to bind T
16501 to a template template parameter with fewer than N
16502 parameters. It is not safe to permit the binding of
16503 default arguments as an extension, as that may change
16504 the meaning of a conforming program. Consider:
16506 struct Dense { static const unsigned int dim = 1; };
16508 template <template <typename> class View,
16510 void operator+(float, View<Block> const&);
16512 template <typename Block,
16513 unsigned int Dim = Block::dim>
16514 struct Lvalue_proxy { operator float() const; };
16518 Lvalue_proxy<Dense> p;
16523 Here, if Lvalue_proxy is permitted to bind to View, then
16524 the global operator+ will be used; if they are not, the
16525 Lvalue_proxy will be converted to float. */
16526 if (coerce_template_parms (parm_parms
,
16528 TYPE_TI_TEMPLATE (parm
),
16530 ? tf_warning_or_error
16532 /*require_all_args=*/true,
16533 /*use_default_args=*/false)
16534 == error_mark_node
)
16537 /* Deduce arguments T, i from TT<T> or TT<i>.
16538 We check each element of PARMVEC and ARGVEC individually
16539 rather than the whole TREE_VEC since they can have
16540 different number of elements. */
16542 parmvec
= expand_template_argument_pack (parmvec
);
16543 argvec
= expand_template_argument_pack (argvec
);
16545 len
= TREE_VEC_LENGTH (parmvec
);
16547 /* Check if the parameters end in a pack, making them
16550 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16551 parm_variadic_p
= 1;
16553 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16554 /* If the template argument list of P contains a pack
16555 expansion that is not the last template argument, the
16556 entire template argument list is a non-deduced
16558 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
16559 return unify_success (explain_p
);
16561 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16562 return unify_too_few_arguments (explain_p
,
16563 TREE_VEC_LENGTH (argvec
), len
);
16565 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16567 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16568 TREE_VEC_ELT (parmvec
, i
),
16569 TREE_VEC_ELT (argvec
, i
),
16570 UNIFY_ALLOW_NONE
, explain_p
);
16573 if (parm_variadic_p
16574 && unify_pack_expansion (tparms
, targs
,
16577 /*subr=*/true, explain_p
))
16580 arg
= TYPE_TI_TEMPLATE (arg
);
16582 /* Fall through to deduce template name. */
16585 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16586 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16588 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16590 /* Simple cases: Value already set, does match or doesn't. */
16591 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16592 return unify_success (explain_p
);
16594 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16598 /* If PARM is `const T' and ARG is only `int', we don't have
16599 a match unless we are allowing additional qualification.
16600 If ARG is `const int' and PARM is just `T' that's OK;
16601 that binds `const int' to `T'. */
16602 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16604 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16606 /* Consider the case where ARG is `const volatile int' and
16607 PARM is `const T'. Then, T should be `volatile int'. */
16608 arg
= cp_build_qualified_type_real
16609 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16610 if (arg
== error_mark_node
)
16611 return unify_invalid (explain_p
);
16613 /* Simple cases: Value already set, does match or doesn't. */
16614 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16615 return unify_success (explain_p
);
16617 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16619 /* Make sure that ARG is not a variable-sized array. (Note
16620 that were talking about variable-sized arrays (like
16621 `int[n]'), rather than arrays of unknown size (like
16622 `int[]').) We'll get very confused by such a type since
16623 the bound of the array is not constant, and therefore
16624 not mangleable. Besides, such types are not allowed in
16625 ISO C++, so we can do as we please here. We do allow
16626 them for 'auto' deduction, since that isn't ABI-exposed. */
16627 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16628 return unify_vla_arg (explain_p
, arg
);
16630 /* Strip typedefs as in convert_template_argument. */
16631 arg
= canonicalize_type_argument (arg
, tf_none
);
16634 /* If ARG is a parameter pack or an expansion, we cannot unify
16635 against it unless PARM is also a parameter pack. */
16636 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16637 && !template_parameter_pack_p (parm
))
16638 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16640 /* If the argument deduction results is a METHOD_TYPE,
16641 then there is a problem.
16642 METHOD_TYPE doesn't map to any real C++ type the result of
16643 the deduction can not be of that type. */
16644 if (TREE_CODE (arg
) == METHOD_TYPE
)
16645 return unify_method_type_error (explain_p
, arg
);
16647 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16648 return unify_success (explain_p
);
16650 case TEMPLATE_PARM_INDEX
:
16651 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16652 if (tparm
== error_mark_node
)
16653 return unify_invalid (explain_p
);
16655 if (TEMPLATE_PARM_LEVEL (parm
)
16656 != template_decl_level (tparm
))
16658 /* The PARM is not one we're trying to unify. Just check
16659 to see if it matches ARG. */
16660 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16661 && cp_tree_equal (parm
, arg
));
16663 unify_expression_unequal (explain_p
, parm
, arg
);
16667 idx
= TEMPLATE_PARM_IDX (parm
);
16668 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16672 int x
= !cp_tree_equal (targ
, arg
);
16674 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16678 /* [temp.deduct.type] If, in the declaration of a function template
16679 with a non-type template-parameter, the non-type
16680 template-parameter is used in an expression in the function
16681 parameter-list and, if the corresponding template-argument is
16682 deduced, the template-argument type shall match the type of the
16683 template-parameter exactly, except that a template-argument
16684 deduced from an array bound may be of any integral type.
16685 The non-type parameter might use already deduced type parameters. */
16686 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16687 if (!TREE_TYPE (arg
))
16688 /* Template-parameter dependent expression. Just accept it for now.
16689 It will later be processed in convert_template_argument. */
16691 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16693 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16694 && (TREE_CODE (tparm
) == INTEGER_TYPE
16695 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16696 /* Convert the ARG to the type of PARM; the deduced non-type
16697 template argument must exactly match the types of the
16698 corresponding parameter. */
16699 arg
= fold (build_nop (tparm
, arg
));
16700 else if (uses_template_parms (tparm
))
16701 /* We haven't deduced the type of this parameter yet. Try again
16703 return unify_success (explain_p
);
16705 return unify_type_mismatch (explain_p
, tparm
, arg
);
16707 /* If ARG is a parameter pack or an expansion, we cannot unify
16708 against it unless PARM is also a parameter pack. */
16709 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16710 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16711 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16713 arg
= strip_typedefs_expr (arg
);
16714 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16715 return unify_success (explain_p
);
16719 /* A pointer-to-member constant can be unified only with
16720 another constant. */
16721 if (TREE_CODE (arg
) != PTRMEM_CST
)
16722 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16724 /* Just unify the class member. It would be useless (and possibly
16725 wrong, depending on the strict flags) to unify also
16726 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16727 arg refer to the same variable, even if through different
16728 classes. For instance:
16730 struct A { int x; };
16733 Unification of &A::x and &B::x must succeed. */
16734 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16735 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16740 if (TREE_CODE (arg
) != POINTER_TYPE
)
16741 return unify_type_mismatch (explain_p
, parm
, arg
);
16743 /* [temp.deduct.call]
16745 A can be another pointer or pointer to member type that can
16746 be converted to the deduced A via a qualification
16747 conversion (_conv.qual_).
16749 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16750 This will allow for additional cv-qualification of the
16751 pointed-to types if appropriate. */
16753 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16754 /* The derived-to-base conversion only persists through one
16755 level of pointers. */
16756 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16758 return unify (tparms
, targs
, TREE_TYPE (parm
),
16759 TREE_TYPE (arg
), strict
, explain_p
);
16762 case REFERENCE_TYPE
:
16763 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16764 return unify_type_mismatch (explain_p
, parm
, arg
);
16765 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16766 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16769 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16770 return unify_type_mismatch (explain_p
, parm
, arg
);
16771 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16772 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16773 return unify_type_mismatch (explain_p
, parm
, arg
);
16774 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16775 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16776 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16783 /* Our representation of array types uses "N - 1" as the
16784 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16785 not an integer constant. We cannot unify arbitrarily
16786 complex expressions, so we eliminate the MINUS_EXPRs
16788 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16789 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16792 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16793 parm_max
= TREE_OPERAND (parm_max
, 0);
16795 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16796 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16799 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16800 trying to unify the type of a variable with the type
16801 of a template parameter. For example:
16803 template <unsigned int N>
16804 void f (char (&) [N]);
16811 Here, the type of the ARG will be "int [g(i)]", and
16812 may be a SAVE_EXPR, etc. */
16813 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16814 return unify_vla_arg (explain_p
, arg
);
16815 arg_max
= TREE_OPERAND (arg_max
, 0);
16818 /* If only one of the bounds used a MINUS_EXPR, compensate
16819 by adding one to the other bound. */
16820 if (parm_cst
&& !arg_cst
)
16821 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16825 else if (arg_cst
&& !parm_cst
)
16826 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16831 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16832 UNIFY_ALLOW_INTEGER
, explain_p
);
16834 return unify_success (explain_p
);
16841 case ENUMERAL_TYPE
:
16844 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16845 return unify_type_mismatch (explain_p
, parm
, arg
);
16847 /* We have already checked cv-qualification at the top of the
16849 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16850 return unify_type_mismatch (explain_p
, parm
, arg
);
16852 /* As far as unification is concerned, this wins. Later checks
16853 will invalidate it if necessary. */
16854 return unify_success (explain_p
);
16856 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16857 /* Type INTEGER_CST can come from ordinary constant template args. */
16859 while (TREE_CODE (arg
) == NOP_EXPR
)
16860 arg
= TREE_OPERAND (arg
, 0);
16862 if (TREE_CODE (arg
) != INTEGER_CST
)
16863 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16864 return (tree_int_cst_equal (parm
, arg
)
16865 ? unify_success (explain_p
)
16866 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16870 int i
, len
, argslen
;
16871 int parm_variadic_p
= 0;
16873 if (TREE_CODE (arg
) != TREE_VEC
)
16874 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16876 len
= TREE_VEC_LENGTH (parm
);
16877 argslen
= TREE_VEC_LENGTH (arg
);
16879 /* Check for pack expansions in the parameters. */
16880 for (i
= 0; i
< len
; ++i
)
16882 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16885 /* We can unify against something with a trailing
16887 parm_variadic_p
= 1;
16889 /* [temp.deduct.type]/9: If the template argument list of
16890 P contains a pack expansion that is not the last
16891 template argument, the entire template argument list
16892 is a non-deduced context. */
16893 return unify_success (explain_p
);
16897 /* If we don't have enough arguments to satisfy the parameters
16898 (not counting the pack expression at the end), or we have
16899 too many arguments for a parameter list that doesn't end in
16900 a pack expression, we can't unify. */
16901 if (parm_variadic_p
16902 ? argslen
< len
- parm_variadic_p
16904 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16906 /* Unify all of the parameters that precede the (optional)
16907 pack expression. */
16908 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16910 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16911 TREE_VEC_ELT (parm
, i
),
16912 TREE_VEC_ELT (arg
, i
),
16913 UNIFY_ALLOW_NONE
, explain_p
);
16915 if (parm_variadic_p
)
16916 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16918 /*subr=*/true, explain_p
);
16919 return unify_success (explain_p
);
16924 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16925 return unify_type_mismatch (explain_p
, parm
, arg
);
16927 if (TYPE_PTRMEMFUNC_P (parm
))
16929 if (!TYPE_PTRMEMFUNC_P (arg
))
16930 return unify_type_mismatch (explain_p
, parm
, arg
);
16932 return unify (tparms
, targs
,
16933 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16934 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16935 strict
, explain_p
);
16938 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16940 tree t
= NULL_TREE
;
16942 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16944 /* First, we try to unify the PARM and ARG directly. */
16945 t
= try_class_unification (tparms
, targs
,
16946 parm
, arg
, explain_p
);
16950 /* Fallback to the special case allowed in
16951 [temp.deduct.call]:
16953 If P is a class, and P has the form
16954 template-id, then A can be a derived class of
16955 the deduced A. Likewise, if P is a pointer to
16956 a class of the form template-id, A can be a
16957 pointer to a derived class pointed to by the
16959 enum template_base_result r
;
16960 r
= get_template_base (tparms
, targs
, parm
, arg
,
16964 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16967 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16968 && (CLASSTYPE_TI_TEMPLATE (parm
)
16969 == CLASSTYPE_TI_TEMPLATE (arg
)))
16970 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16971 Then, we should unify `int' and `U'. */
16974 /* There's no chance of unification succeeding. */
16975 return unify_type_mismatch (explain_p
, parm
, arg
);
16977 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16978 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16980 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16981 return unify_type_mismatch (explain_p
, parm
, arg
);
16982 return unify_success (explain_p
);
16985 case FUNCTION_TYPE
:
16987 unsigned int nargs
;
16992 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16993 return unify_type_mismatch (explain_p
, parm
, arg
);
16995 /* CV qualifications for methods can never be deduced, they must
16996 match exactly. We need to check them explicitly here,
16997 because type_unification_real treats them as any other
16998 cv-qualified parameter. */
16999 if (TREE_CODE (parm
) == METHOD_TYPE
17000 && (!check_cv_quals_for_unify
17002 class_of_this_parm (arg
),
17003 class_of_this_parm (parm
))))
17004 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17006 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17007 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17009 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17010 args
= XALLOCAVEC (tree
, nargs
);
17011 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17012 a
!= NULL_TREE
&& a
!= void_list_node
;
17013 a
= TREE_CHAIN (a
), ++i
)
17014 args
[i
] = TREE_VALUE (a
);
17017 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17018 args
, nargs
, 1, DEDUCE_EXACT
,
17019 LOOKUP_NORMAL
, explain_p
);
17023 /* Unify a pointer to member with a pointer to member function, which
17024 deduces the type of the member as a function type. */
17025 if (TYPE_PTRMEMFUNC_P (arg
))
17030 /* Check top-level cv qualifiers */
17031 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17032 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17034 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17035 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17036 UNIFY_ALLOW_NONE
, explain_p
);
17038 /* Determine the type of the function we are unifying against. */
17039 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
17041 build_function_type (TREE_TYPE (method_type
),
17042 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
17044 /* Extract the cv-qualifiers of the member function from the
17045 implicit object parameter and place them on the function
17046 type to be restored later. */
17047 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
17048 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17051 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17052 return unify_type_mismatch (explain_p
, parm
, arg
);
17053 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17054 TYPE_OFFSET_BASETYPE (arg
),
17055 UNIFY_ALLOW_NONE
, explain_p
);
17056 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17057 strict
, explain_p
);
17060 if (DECL_TEMPLATE_PARM_P (parm
))
17061 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17062 if (arg
!= integral_constant_value (parm
))
17063 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17064 return unify_success (explain_p
);
17067 case TEMPLATE_DECL
:
17068 /* Matched cases are handled by the ARG == PARM test above. */
17069 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17072 /* A non-type template parameter that is a variable should be a
17073 an integral constant, in which case, it whould have been
17074 folded into its (constant) value. So we should not be getting
17075 a variable here. */
17076 gcc_unreachable ();
17078 case TYPE_ARGUMENT_PACK
:
17079 case NONTYPE_ARGUMENT_PACK
:
17080 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17081 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17084 case DECLTYPE_TYPE
:
17085 case UNDERLYING_TYPE
:
17086 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17087 or UNDERLYING_TYPE nodes. */
17088 return unify_success (explain_p
);
17091 /* Unification fails if we hit an error node. */
17092 return unify_invalid (explain_p
);
17095 /* An unresolved overload is a nondeduced context. */
17096 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17097 return unify_success (explain_p
);
17098 gcc_assert (EXPR_P (parm
));
17100 /* We must be looking at an expression. This can happen with
17104 void foo(S<I>, S<I + 2>);
17106 This is a "nondeduced context":
17110 The nondeduced contexts are:
17112 --A type that is a template-id in which one or more of
17113 the template-arguments is an expression that references
17114 a template-parameter.
17116 In these cases, we assume deduction succeeded, but don't
17117 actually infer any unifications. */
17119 if (!uses_template_parms (parm
)
17120 && !template_args_equal (parm
, arg
))
17121 return unify_expression_unequal (explain_p
, parm
, arg
);
17123 return unify_success (explain_p
);
17126 #undef RECUR_AND_CHECK_FAILURE
17128 /* Note that DECL can be defined in this translation unit, if
17132 mark_definable (tree decl
)
17135 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17136 FOR_EACH_CLONE (clone
, decl
)
17137 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17140 /* Called if RESULT is explicitly instantiated, or is a member of an
17141 explicitly instantiated class. */
17144 mark_decl_instantiated (tree result
, int extern_p
)
17146 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17148 /* If this entity has already been written out, it's too late to
17149 make any modifications. */
17150 if (TREE_ASM_WRITTEN (result
))
17153 if (TREE_CODE (result
) != FUNCTION_DECL
)
17154 /* The TREE_PUBLIC flag for function declarations will have been
17155 set correctly by tsubst. */
17156 TREE_PUBLIC (result
) = 1;
17158 /* This might have been set by an earlier implicit instantiation. */
17159 DECL_COMDAT (result
) = 0;
17162 DECL_NOT_REALLY_EXTERN (result
) = 0;
17165 mark_definable (result
);
17166 /* Always make artificials weak. */
17167 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17168 comdat_linkage (result
);
17169 /* For WIN32 we also want to put explicit instantiations in
17170 linkonce sections. */
17171 else if (TREE_PUBLIC (result
))
17172 maybe_make_one_only (result
);
17175 /* If EXTERN_P, then this function will not be emitted -- unless
17176 followed by an explicit instantiation, at which point its linkage
17177 will be adjusted. If !EXTERN_P, then this function will be
17178 emitted here. In neither circumstance do we want
17179 import_export_decl to adjust the linkage. */
17180 DECL_INTERFACE_KNOWN (result
) = 1;
17183 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17184 important template arguments. If any are missing, we check whether
17185 they're important by using error_mark_node for substituting into any
17186 args that were used for partial ordering (the ones between ARGS and END)
17187 and seeing if it bubbles up. */
17190 check_undeduced_parms (tree targs
, tree args
, tree end
)
17192 bool found
= false;
17194 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17195 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17198 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17202 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17203 if (substed
== error_mark_node
)
17209 /* Given two function templates PAT1 and PAT2, return:
17211 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17212 -1 if PAT2 is more specialized than PAT1.
17213 0 if neither is more specialized.
17215 LEN indicates the number of parameters we should consider
17216 (defaulted parameters should not be considered).
17218 The 1998 std underspecified function template partial ordering, and
17219 DR214 addresses the issue. We take pairs of arguments, one from
17220 each of the templates, and deduce them against each other. One of
17221 the templates will be more specialized if all the *other*
17222 template's arguments deduce against its arguments and at least one
17223 of its arguments *does* *not* deduce against the other template's
17224 corresponding argument. Deduction is done as for class templates.
17225 The arguments used in deduction have reference and top level cv
17226 qualifiers removed. Iff both arguments were originally reference
17227 types *and* deduction succeeds in both directions, the template
17228 with the more cv-qualified argument wins for that pairing (if
17229 neither is more cv-qualified, they both are equal). Unlike regular
17230 deduction, after all the arguments have been deduced in this way,
17231 we do *not* verify the deduced template argument values can be
17232 substituted into non-deduced contexts.
17234 The logic can be a bit confusing here, because we look at deduce1 and
17235 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17236 can find template arguments for pat1 to make arg1 look like arg2, that
17237 means that arg2 is at least as specialized as arg1. */
17240 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17242 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17243 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17244 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17245 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17246 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17247 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17248 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17249 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17250 tree origs1
, origs2
;
17251 bool lose1
= false;
17252 bool lose2
= false;
17254 /* Remove the this parameter from non-static member functions. If
17255 one is a non-static member function and the other is not a static
17256 member function, remove the first parameter from that function
17257 also. This situation occurs for operator functions where we
17258 locate both a member function (with this pointer) and non-member
17259 operator (with explicit first operand). */
17260 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17262 len
--; /* LEN is the number of significant arguments for DECL1 */
17263 args1
= TREE_CHAIN (args1
);
17264 if (!DECL_STATIC_FUNCTION_P (decl2
))
17265 args2
= TREE_CHAIN (args2
);
17267 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17269 args2
= TREE_CHAIN (args2
);
17270 if (!DECL_STATIC_FUNCTION_P (decl1
))
17273 args1
= TREE_CHAIN (args1
);
17277 /* If only one is a conversion operator, they are unordered. */
17278 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17281 /* Consider the return type for a conversion function */
17282 if (DECL_CONV_FN_P (decl1
))
17284 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17285 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17289 processing_template_decl
++;
17295 /* Stop when an ellipsis is seen. */
17296 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17298 tree arg1
= TREE_VALUE (args1
);
17299 tree arg2
= TREE_VALUE (args2
);
17300 int deduce1
, deduce2
;
17304 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17305 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17307 /* When both arguments are pack expansions, we need only
17308 unify the patterns themselves. */
17309 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17310 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17312 /* This is the last comparison we need to do. */
17316 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17318 arg1
= TREE_TYPE (arg1
);
17319 quals1
= cp_type_quals (arg1
);
17322 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17324 arg2
= TREE_TYPE (arg2
);
17325 quals2
= cp_type_quals (arg2
);
17328 arg1
= TYPE_MAIN_VARIANT (arg1
);
17329 arg2
= TYPE_MAIN_VARIANT (arg2
);
17331 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17333 int i
, len2
= list_length (args2
);
17334 tree parmvec
= make_tree_vec (1);
17335 tree argvec
= make_tree_vec (len2
);
17338 /* Setup the parameter vector, which contains only ARG1. */
17339 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17341 /* Setup the argument vector, which contains the remaining
17343 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17344 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17346 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17347 argvec
, DEDUCE_EXACT
,
17348 /*subr=*/true, /*explain_p=*/false)
17351 /* We cannot deduce in the other direction, because ARG1 is
17352 a pack expansion but ARG2 is not. */
17355 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17357 int i
, len1
= list_length (args1
);
17358 tree parmvec
= make_tree_vec (1);
17359 tree argvec
= make_tree_vec (len1
);
17362 /* Setup the parameter vector, which contains only ARG1. */
17363 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17365 /* Setup the argument vector, which contains the remaining
17367 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17368 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17370 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17371 argvec
, DEDUCE_EXACT
,
17372 /*subr=*/true, /*explain_p=*/false)
17375 /* We cannot deduce in the other direction, because ARG2 is
17376 a pack expansion but ARG1 is not.*/
17382 /* The normal case, where neither argument is a pack
17384 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17385 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17387 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17388 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17392 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17393 arg2, then arg2 is not as specialized as arg1. */
17399 /* "If, for a given type, deduction succeeds in both directions
17400 (i.e., the types are identical after the transformations above)
17401 and if the type from the argument template is more cv-qualified
17402 than the type from the parameter template (as described above)
17403 that type is considered to be more specialized than the other. If
17404 neither type is more cv-qualified than the other then neither type
17405 is more specialized than the other." */
17407 if (deduce1
&& deduce2
17408 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17410 if ((quals1
& quals2
) == quals2
)
17412 if ((quals1
& quals2
) == quals1
)
17416 if (lose1
&& lose2
)
17417 /* We've failed to deduce something in either direction.
17418 These must be unordered. */
17421 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17422 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17423 /* We have already processed all of the arguments in our
17424 handing of the pack expansion type. */
17427 args1
= TREE_CHAIN (args1
);
17428 args2
= TREE_CHAIN (args2
);
17431 /* "In most cases, all template parameters must have values in order for
17432 deduction to succeed, but for partial ordering purposes a template
17433 parameter may remain without a value provided it is not used in the
17434 types being used for partial ordering."
17436 Thus, if we are missing any of the targs1 we need to substitute into
17437 origs1, then pat2 is not as specialized as pat1. This can happen when
17438 there is a nondeduced context. */
17439 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17441 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17444 processing_template_decl
--;
17446 /* All things being equal, if the next argument is a pack expansion
17447 for one function but not for the other, prefer the
17448 non-variadic function. FIXME this is bogus; see c++/41958. */
17450 && args1
&& TREE_VALUE (args1
)
17451 && args2
&& TREE_VALUE (args2
))
17453 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17454 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17457 if (lose1
== lose2
)
17465 /* Determine which of two partial specializations of MAIN_TMPL is more
17468 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17469 to the first partial specialization. The TREE_VALUE is the
17470 innermost set of template parameters for the partial
17471 specialization. PAT2 is similar, but for the second template.
17473 Return 1 if the first partial specialization is more specialized;
17474 -1 if the second is more specialized; 0 if neither is more
17477 See [temp.class.order] for information about determining which of
17478 two templates is more specialized. */
17481 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17486 bool any_deductions
= false;
17488 tmpl1
= TREE_TYPE (pat1
);
17489 tmpl2
= TREE_TYPE (pat2
);
17491 /* Just like what happens for functions, if we are ordering between
17492 different class template specializations, we may encounter dependent
17493 types in the arguments, and we need our dependency check functions
17494 to behave correctly. */
17495 ++processing_template_decl
;
17496 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17497 CLASSTYPE_TI_ARGS (tmpl1
),
17498 CLASSTYPE_TI_ARGS (tmpl2
));
17502 any_deductions
= true;
17505 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17506 CLASSTYPE_TI_ARGS (tmpl2
),
17507 CLASSTYPE_TI_ARGS (tmpl1
));
17511 any_deductions
= true;
17513 --processing_template_decl
;
17515 /* In the case of a tie where at least one of the class templates
17516 has a parameter pack at the end, the template with the most
17517 non-packed parameters wins. */
17520 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17521 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17523 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17524 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17525 int len1
= TREE_VEC_LENGTH (args1
);
17526 int len2
= TREE_VEC_LENGTH (args2
);
17528 /* We don't count the pack expansion at the end. */
17529 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17531 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17536 else if (len1
< len2
)
17543 /* Return the template arguments that will produce the function signature
17544 DECL from the function template FN, with the explicit template
17545 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17546 also match. Return NULL_TREE if no satisfactory arguments could be
17550 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17552 int ntparms
= DECL_NTPARMS (fn
);
17553 tree targs
= make_tree_vec (ntparms
);
17554 tree decl_type
= TREE_TYPE (decl
);
17555 tree decl_arg_types
;
17557 unsigned int nargs
, ix
;
17560 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17562 /* Never do unification on the 'this' parameter. */
17563 decl_arg_types
= skip_artificial_parms_for (decl
,
17564 TYPE_ARG_TYPES (decl_type
));
17566 nargs
= list_length (decl_arg_types
);
17567 args
= XALLOCAVEC (tree
, nargs
);
17568 for (arg
= decl_arg_types
, ix
= 0;
17569 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17570 arg
= TREE_CHAIN (arg
), ++ix
)
17571 args
[ix
] = TREE_VALUE (arg
);
17573 if (fn_type_unification (fn
, explicit_args
, targs
,
17575 (check_rettype
|| DECL_CONV_FN_P (fn
)
17576 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17577 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17578 == error_mark_node
)
17584 /* Return the innermost template arguments that, when applied to a partial
17585 specialization of MAIN_TMPL whose innermost template parameters are
17586 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17589 For example, suppose we have:
17591 template <class T, class U> struct S {};
17592 template <class T> struct S<T*, int> {};
17594 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17595 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17596 int}. The resulting vector will be {double}, indicating that `T'
17597 is bound to `double'. */
17600 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17602 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17604 tree innermost_deduced_args
;
17606 innermost_deduced_args
= make_tree_vec (ntparms
);
17607 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17609 deduced_args
= copy_node (args
);
17610 SET_TMPL_ARGS_LEVEL (deduced_args
,
17611 TMPL_ARGS_DEPTH (deduced_args
),
17612 innermost_deduced_args
);
17615 deduced_args
= innermost_deduced_args
;
17617 if (unify (tparms
, deduced_args
,
17618 INNERMOST_TEMPLATE_ARGS (spec_args
),
17619 INNERMOST_TEMPLATE_ARGS (args
),
17620 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17623 for (i
= 0; i
< ntparms
; ++i
)
17624 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17627 /* Verify that nondeduced template arguments agree with the type
17628 obtained from argument deduction.
17632 struct A { typedef int X; };
17633 template <class T, class U> struct C {};
17634 template <class T> struct C<T, typename T::X> {};
17636 Then with the instantiation `C<A, int>', we can deduce that
17637 `T' is `A' but unify () does not check whether `typename T::X'
17639 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17640 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17641 spec_args
, main_tmpl
,
17642 tf_none
, false, false);
17643 if (spec_args
== error_mark_node
17644 /* We only need to check the innermost arguments; the other
17645 arguments will always agree. */
17646 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17647 INNERMOST_TEMPLATE_ARGS (args
)))
17650 /* Now that we have bindings for all of the template arguments,
17651 ensure that the arguments deduced for the template template
17652 parameters have compatible template parameter lists. See the use
17653 of template_template_parm_bindings_ok_p in fn_type_unification
17654 for more information. */
17655 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17658 return deduced_args
;
17661 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17662 Return the TREE_LIST node with the most specialized template, if
17663 any. If there is no most specialized template, the error_mark_node
17666 Note that this function does not look at, or modify, the
17667 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17668 returned is one of the elements of INSTANTIATIONS, callers may
17669 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17670 and retrieve it from the value returned. */
17673 most_specialized_instantiation (tree templates
)
17677 ++processing_template_decl
;
17680 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17684 if (get_bindings (TREE_VALUE (champ
),
17685 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17686 NULL_TREE
, /*check_ret=*/true))
17689 if (get_bindings (TREE_VALUE (fn
),
17690 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17691 NULL_TREE
, /*check_ret=*/true))
17698 /* Equally specialized, move to next function. If there
17699 is no next function, nothing's most specialized. */
17700 fn
= TREE_CHAIN (fn
);
17708 /* Now verify that champ is better than everything earlier in the
17709 instantiation list. */
17710 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17711 if (get_bindings (TREE_VALUE (champ
),
17712 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17713 NULL_TREE
, /*check_ret=*/true)
17714 || !get_bindings (TREE_VALUE (fn
),
17715 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17716 NULL_TREE
, /*check_ret=*/true))
17722 processing_template_decl
--;
17725 return error_mark_node
;
17730 /* If DECL is a specialization of some template, return the most
17731 general such template. Otherwise, returns NULL_TREE.
17733 For example, given:
17735 template <class T> struct S { template <class U> void f(U); };
17737 if TMPL is `template <class U> void S<int>::f(U)' this will return
17738 the full template. This function will not trace past partial
17739 specializations, however. For example, given in addition:
17741 template <class T> struct S<T*> { template <class U> void f(U); };
17743 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17744 `template <class T> template <class U> S<T*>::f(U)'. */
17747 most_general_template (tree decl
)
17749 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17750 an immediate specialization. */
17751 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17753 if (DECL_TEMPLATE_INFO (decl
)) {
17754 decl
= DECL_TI_TEMPLATE (decl
);
17756 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17757 template friend. */
17758 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17764 /* Look for more and more general templates. */
17765 while (DECL_TEMPLATE_INFO (decl
))
17767 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17768 (See cp-tree.h for details.) */
17769 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17772 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17773 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17776 /* Stop if we run into an explicitly specialized class template. */
17777 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17778 && DECL_CONTEXT (decl
)
17779 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17782 decl
= DECL_TI_TEMPLATE (decl
);
17788 /* Return the most specialized of the class template partial
17789 specializations of TMPL which can produce TYPE, a specialization of
17790 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17791 a _TYPE node corresponding to the partial specialization, while the
17792 TREE_PURPOSE is the set of template arguments that must be
17793 substituted into the TREE_TYPE in order to generate TYPE.
17795 If the choice of partial specialization is ambiguous, a diagnostic
17796 is issued, and the error_mark_node is returned. If there are no
17797 partial specializations of TMPL matching TYPE, then NULL_TREE is
17801 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17803 tree list
= NULL_TREE
;
17809 tree outer_args
= NULL_TREE
;
17811 tmpl
= most_general_template (tmpl
);
17812 args
= CLASSTYPE_TI_ARGS (type
);
17814 /* For determining which partial specialization to use, only the
17815 innermost args are interesting. */
17816 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17818 outer_args
= strip_innermost_template_args (args
, 1);
17819 args
= INNERMOST_TEMPLATE_ARGS (args
);
17822 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17824 tree partial_spec_args
;
17826 tree parms
= TREE_VALUE (t
);
17828 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17830 ++processing_template_decl
;
17836 /* Discard the outer levels of args, and then substitute in the
17837 template args from the enclosing class. */
17838 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17839 partial_spec_args
= tsubst_template_args
17840 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17842 /* PARMS already refers to just the innermost parms, but the
17843 template parms in partial_spec_args had their levels lowered
17844 by tsubst, so we need to do the same for the parm list. We
17845 can't just tsubst the TREE_VEC itself, as tsubst wants to
17846 treat a TREE_VEC as an argument vector. */
17847 parms
= copy_node (parms
);
17848 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17849 TREE_VEC_ELT (parms
, i
) =
17850 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17854 partial_spec_args
=
17855 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17856 add_to_template_args (outer_args
,
17857 partial_spec_args
),
17859 /*require_all_args=*/true,
17860 /*use_default_args=*/true);
17862 --processing_template_decl
;
17864 if (partial_spec_args
== error_mark_node
)
17865 return error_mark_node
;
17867 spec_args
= get_class_bindings (tmpl
, parms
,
17873 spec_args
= add_to_template_args (outer_args
, spec_args
);
17874 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17875 TREE_TYPE (list
) = TREE_TYPE (t
);
17882 ambiguous_p
= false;
17885 t
= TREE_CHAIN (t
);
17886 for (; t
; t
= TREE_CHAIN (t
))
17888 fate
= more_specialized_class (tmpl
, champ
, t
);
17895 t
= TREE_CHAIN (t
);
17898 ambiguous_p
= true;
17907 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17909 fate
= more_specialized_class (tmpl
, champ
, t
);
17912 ambiguous_p
= true;
17920 char *spaces
= NULL
;
17921 if (!(complain
& tf_error
))
17922 return error_mark_node
;
17923 error ("ambiguous class template instantiation for %q#T", type
);
17924 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17925 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17927 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17928 spaces
= spaces
? spaces
: get_spaces (str
);
17931 return error_mark_node
;
17937 /* Explicitly instantiate DECL. */
17940 do_decl_instantiation (tree decl
, tree storage
)
17942 tree result
= NULL_TREE
;
17945 if (!decl
|| decl
== error_mark_node
)
17946 /* An error occurred, for which grokdeclarator has already issued
17947 an appropriate message. */
17949 else if (! DECL_LANG_SPECIFIC (decl
))
17951 error ("explicit instantiation of non-template %q#D", decl
);
17954 else if (TREE_CODE (decl
) == VAR_DECL
)
17956 /* There is an asymmetry here in the way VAR_DECLs and
17957 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17958 the latter, the DECL we get back will be marked as a
17959 template instantiation, and the appropriate
17960 DECL_TEMPLATE_INFO will be set up. This does not happen for
17961 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17962 should handle VAR_DECLs as it currently handles
17964 if (!DECL_CLASS_SCOPE_P (decl
))
17966 error ("%qD is not a static data member of a class template", decl
);
17969 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17970 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17972 error ("no matching template for %qD found", decl
);
17975 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17977 error ("type %qT for explicit instantiation %qD does not match "
17978 "declared type %qT", TREE_TYPE (result
), decl
,
17983 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17985 error ("explicit instantiation of %q#D", decl
);
17991 /* Check for various error cases. Note that if the explicit
17992 instantiation is valid the RESULT will currently be marked as an
17993 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17994 until we get here. */
17996 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17998 /* DR 259 [temp.spec].
18000 Both an explicit instantiation and a declaration of an explicit
18001 specialization shall not appear in a program unless the explicit
18002 instantiation follows a declaration of the explicit specialization.
18004 For a given set of template parameters, if an explicit
18005 instantiation of a template appears after a declaration of an
18006 explicit specialization for that template, the explicit
18007 instantiation has no effect. */
18010 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18014 No program shall explicitly instantiate any template more
18017 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18018 the first instantiation was `extern' and the second is not,
18019 and EXTERN_P for the opposite case. */
18020 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18021 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18022 /* If an "extern" explicit instantiation follows an ordinary
18023 explicit instantiation, the template is instantiated. */
18027 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18029 error ("no matching template for %qD found", result
);
18032 else if (!DECL_TEMPLATE_INFO (result
))
18034 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18038 if (storage
== NULL_TREE
)
18040 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18042 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18043 pedwarn (input_location
, OPT_Wpedantic
,
18044 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18049 error ("storage class %qD applied to template instantiation", storage
);
18051 check_explicit_instantiation_namespace (result
);
18052 mark_decl_instantiated (result
, extern_p
);
18054 instantiate_decl (result
, /*defer_ok=*/1,
18055 /*expl_inst_class_mem_p=*/false);
18059 mark_class_instantiated (tree t
, int extern_p
)
18061 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18062 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18063 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18064 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18067 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18068 rest_of_type_compilation (t
, 1);
18072 /* Called from do_type_instantiation through binding_table_foreach to
18073 do recursive instantiation for the type bound in ENTRY. */
18075 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18077 tree storage
= *(tree
*) data
;
18079 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18080 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18081 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18084 /* Called from do_type_instantiation to instantiate a member
18085 (a member function or a static member variable) of an
18086 explicitly instantiated class template. */
18088 instantiate_class_member (tree decl
, int extern_p
)
18090 mark_decl_instantiated (decl
, extern_p
);
18092 instantiate_decl (decl
, /*defer_ok=*/1,
18093 /*expl_inst_class_mem_p=*/true);
18096 /* Perform an explicit instantiation of template class T. STORAGE, if
18097 non-null, is the RID for extern, inline or static. COMPLAIN is
18098 nonzero if this is called from the parser, zero if called recursively,
18099 since the standard is unclear (as detailed below). */
18102 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18107 int previous_instantiation_extern_p
= 0;
18109 if (TREE_CODE (t
) == TYPE_DECL
)
18112 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18115 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18117 error ("explicit instantiation of non-class template %qD", tmpl
);
18119 error ("explicit instantiation of non-template type %qT", t
);
18125 if (!COMPLETE_TYPE_P (t
))
18127 if (complain
& tf_error
)
18128 error ("explicit instantiation of %q#T before definition of template",
18133 if (storage
!= NULL_TREE
)
18135 if (!in_system_header
)
18137 if (storage
== ridpointers
[(int) RID_EXTERN
])
18139 if (cxx_dialect
== cxx98
)
18140 pedwarn (input_location
, OPT_Wpedantic
,
18141 "ISO C++ 1998 forbids the use of %<extern%> on "
18142 "explicit instantiations");
18145 pedwarn (input_location
, OPT_Wpedantic
,
18146 "ISO C++ forbids the use of %qE"
18147 " on explicit instantiations", storage
);
18150 if (storage
== ridpointers
[(int) RID_INLINE
])
18152 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18154 else if (storage
== ridpointers
[(int) RID_STATIC
])
18158 error ("storage class %qD applied to template instantiation",
18164 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18166 /* DR 259 [temp.spec].
18168 Both an explicit instantiation and a declaration of an explicit
18169 specialization shall not appear in a program unless the explicit
18170 instantiation follows a declaration of the explicit specialization.
18172 For a given set of template parameters, if an explicit
18173 instantiation of a template appears after a declaration of an
18174 explicit specialization for that template, the explicit
18175 instantiation has no effect. */
18178 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18182 No program shall explicitly instantiate any template more
18185 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18186 instantiation was `extern'. If EXTERN_P then the second is.
18187 These cases are OK. */
18188 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18190 if (!previous_instantiation_extern_p
&& !extern_p
18191 && (complain
& tf_error
))
18192 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18194 /* If we've already instantiated the template, just return now. */
18195 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18199 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18200 mark_class_instantiated (t
, extern_p
);
18208 /* In contrast to implicit instantiation, where only the
18209 declarations, and not the definitions, of members are
18210 instantiated, we have here:
18214 The explicit instantiation of a class template specialization
18215 implies the instantiation of all of its members not
18216 previously explicitly specialized in the translation unit
18217 containing the explicit instantiation.
18219 Of course, we can't instantiate member template classes, since
18220 we don't have any arguments for them. Note that the standard
18221 is unclear on whether the instantiation of the members are
18222 *explicit* instantiations or not. However, the most natural
18223 interpretation is that it should be an explicit instantiation. */
18226 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18227 if (TREE_CODE (tmp
) == FUNCTION_DECL
18228 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18229 instantiate_class_member (tmp
, extern_p
);
18231 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18232 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18233 instantiate_class_member (tmp
, extern_p
);
18235 if (CLASSTYPE_NESTED_UTDS (t
))
18236 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18237 bt_instantiate_type_proc
, &storage
);
18241 /* Given a function DECL, which is a specialization of TMPL, modify
18242 DECL to be a re-instantiation of TMPL with the same template
18243 arguments. TMPL should be the template into which tsubst'ing
18244 should occur for DECL, not the most general template.
18246 One reason for doing this is a scenario like this:
18249 void f(const T&, int i);
18251 void g() { f(3, 7); }
18254 void f(const T& t, const int i) { }
18256 Note that when the template is first instantiated, with
18257 instantiate_template, the resulting DECL will have no name for the
18258 first parameter, and the wrong type for the second. So, when we go
18259 to instantiate the DECL, we regenerate it. */
18262 regenerate_decl_from_template (tree decl
, tree tmpl
)
18264 /* The arguments used to instantiate DECL, from the most general
18269 args
= DECL_TI_ARGS (decl
);
18270 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18272 /* Make sure that we can see identifiers, and compute access
18274 push_access_scope (decl
);
18276 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18284 args_depth
= TMPL_ARGS_DEPTH (args
);
18285 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18286 if (args_depth
> parms_depth
)
18287 args
= get_innermost_template_args (args
, parms_depth
);
18289 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18290 args
, tf_error
, NULL_TREE
,
18291 /*defer_ok*/false);
18292 if (specs
&& specs
!= error_mark_node
)
18293 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18296 /* Merge parameter declarations. */
18297 decl_parm
= skip_artificial_parms_for (decl
,
18298 DECL_ARGUMENTS (decl
));
18300 = skip_artificial_parms_for (code_pattern
,
18301 DECL_ARGUMENTS (code_pattern
));
18302 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18307 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18308 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18309 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18311 parm_type
= type_decays_to (parm_type
);
18312 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18313 TREE_TYPE (decl_parm
) = parm_type
;
18314 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18315 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18317 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18318 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18320 decl_parm
= DECL_CHAIN (decl_parm
);
18321 pattern_parm
= DECL_CHAIN (pattern_parm
);
18323 /* Merge any parameters that match with the function parameter
18325 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18328 tree expanded_types
;
18329 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18330 the parameters in this function parameter pack. */
18331 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18332 args
, tf_error
, NULL_TREE
);
18333 len
= TREE_VEC_LENGTH (expanded_types
);
18334 for (i
= 0; i
< len
; i
++)
18339 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18340 /* Rename the parameter to include the index. */
18341 DECL_NAME (decl_parm
) =
18342 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18343 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18344 parm_type
= type_decays_to (parm_type
);
18345 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18346 TREE_TYPE (decl_parm
) = parm_type
;
18347 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18348 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18350 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18351 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18353 decl_parm
= DECL_CHAIN (decl_parm
);
18356 /* Merge additional specifiers from the CODE_PATTERN. */
18357 if (DECL_DECLARED_INLINE_P (code_pattern
)
18358 && !DECL_DECLARED_INLINE_P (decl
))
18359 DECL_DECLARED_INLINE_P (decl
) = 1;
18361 else if (TREE_CODE (decl
) == VAR_DECL
)
18363 DECL_INITIAL (decl
) =
18364 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18365 tf_error
, DECL_TI_TEMPLATE (decl
),
18366 /*integral_constant_expression_p=*/false);
18367 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18368 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18369 tf_error
, DECL_TI_TEMPLATE (decl
));
18372 gcc_unreachable ();
18374 pop_access_scope (decl
);
18377 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18378 substituted to get DECL. */
18381 template_for_substitution (tree decl
)
18383 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18385 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18386 for the instantiation. This is not always the most general
18387 template. Consider, for example:
18390 struct S { template <class U> void f();
18391 template <> void f<int>(); };
18393 and an instantiation of S<double>::f<int>. We want TD to be the
18394 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18395 while (/* An instantiation cannot have a definition, so we need a
18396 more general template. */
18397 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18398 /* We must also deal with friend templates. Given:
18400 template <class T> struct S {
18401 template <class U> friend void f() {};
18404 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18405 so far as the language is concerned, but that's still
18406 where we get the pattern for the instantiation from. On
18407 other hand, if the definition comes outside the class, say:
18409 template <class T> struct S {
18410 template <class U> friend void f();
18412 template <class U> friend void f() {}
18414 we don't need to look any further. That's what the check for
18415 DECL_INITIAL is for. */
18416 || (TREE_CODE (decl
) == FUNCTION_DECL
18417 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18418 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18420 /* The present template, TD, should not be a definition. If it
18421 were a definition, we should be using it! Note that we
18422 cannot restructure the loop to just keep going until we find
18423 a template with a definition, since that might go too far if
18424 a specialization was declared, but not defined. */
18425 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18426 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18428 /* Fetch the more general template. */
18429 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18435 /* Returns true if we need to instantiate this template instance even if we
18436 know we aren't going to emit it.. */
18439 always_instantiate_p (tree decl
)
18441 /* We always instantiate inline functions so that we can inline them. An
18442 explicit instantiation declaration prohibits implicit instantiation of
18443 non-inline functions. With high levels of optimization, we would
18444 normally inline non-inline functions -- but we're not allowed to do
18445 that for "extern template" functions. Therefore, we check
18446 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18447 return ((TREE_CODE (decl
) == FUNCTION_DECL
18448 && (DECL_DECLARED_INLINE_P (decl
)
18449 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18450 /* And we need to instantiate static data members so that
18451 their initializers are available in integral constant
18453 || (TREE_CODE (decl
) == VAR_DECL
18454 && decl_maybe_constant_var_p (decl
)));
18457 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18458 instantiate it now, modifying TREE_TYPE (fn). */
18461 maybe_instantiate_noexcept (tree fn
)
18463 tree fntype
, spec
, noex
, clone
;
18465 if (DECL_CLONED_FUNCTION_P (fn
))
18466 fn
= DECL_CLONED_FUNCTION (fn
);
18467 fntype
= TREE_TYPE (fn
);
18468 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18470 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18473 noex
= TREE_PURPOSE (spec
);
18475 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18477 if (push_tinst_level (fn
))
18479 push_access_scope (fn
);
18480 push_deferring_access_checks (dk_no_deferred
);
18481 input_location
= DECL_SOURCE_LOCATION (fn
);
18482 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18483 DEFERRED_NOEXCEPT_ARGS (noex
),
18484 tf_warning_or_error
, fn
,
18485 /*function_p=*/false,
18486 /*integral_constant_expression_p=*/true);
18487 pop_deferring_access_checks ();
18488 pop_access_scope (fn
);
18489 pop_tinst_level ();
18490 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18491 if (spec
== error_mark_node
)
18492 spec
= noexcept_false_spec
;
18495 spec
= noexcept_false_spec
;
18499 /* This is an implicitly declared function, so NOEX is a list of
18500 other functions to evaluate and merge. */
18502 spec
= noexcept_true_spec
;
18503 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18505 tree fn
= OVL_CURRENT (elt
);
18507 maybe_instantiate_noexcept (fn
);
18508 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18509 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18513 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18515 FOR_EACH_CLONE (clone
, fn
)
18517 if (TREE_TYPE (clone
) == fntype
)
18518 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18520 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18524 /* Produce the definition of D, a _DECL generated from a template. If
18525 DEFER_OK is nonzero, then we don't have to actually do the
18526 instantiation now; we just have to do it sometime. Normally it is
18527 an error if this is an explicit instantiation but D is undefined.
18528 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18529 explicitly instantiated class template. */
18532 instantiate_decl (tree d
, int defer_ok
,
18533 bool expl_inst_class_mem_p
)
18535 tree tmpl
= DECL_TI_TEMPLATE (d
);
18542 bool pattern_defined
;
18543 location_t saved_loc
= input_location
;
18548 /* This function should only be used to instantiate templates for
18549 functions and static member variables. */
18550 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18551 || TREE_CODE (d
) == VAR_DECL
);
18553 /* Variables are never deferred; if instantiation is required, they
18554 are instantiated right away. That allows for better code in the
18555 case that an expression refers to the value of the variable --
18556 if the variable has a constant value the referring expression can
18557 take advantage of that fact. */
18558 if (TREE_CODE (d
) == VAR_DECL
18559 || DECL_DECLARED_CONSTEXPR_P (d
))
18562 /* Don't instantiate cloned functions. Instead, instantiate the
18563 functions they cloned. */
18564 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18565 d
= DECL_CLONED_FUNCTION (d
);
18567 if (DECL_TEMPLATE_INSTANTIATED (d
)
18568 || (TREE_CODE (d
) == FUNCTION_DECL
18569 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18570 || DECL_TEMPLATE_SPECIALIZATION (d
))
18571 /* D has already been instantiated or explicitly specialized, so
18572 there's nothing for us to do here.
18574 It might seem reasonable to check whether or not D is an explicit
18575 instantiation, and, if so, stop here. But when an explicit
18576 instantiation is deferred until the end of the compilation,
18577 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18578 the instantiation. */
18581 /* Check to see whether we know that this template will be
18582 instantiated in some other file, as with "extern template"
18584 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18586 /* In general, we do not instantiate such templates. */
18587 if (external_p
&& !always_instantiate_p (d
))
18590 gen_tmpl
= most_general_template (tmpl
);
18591 gen_args
= DECL_TI_ARGS (d
);
18593 if (tmpl
!= gen_tmpl
)
18594 /* We should already have the extra args. */
18595 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18596 == TMPL_ARGS_DEPTH (gen_args
));
18597 /* And what's in the hash table should match D. */
18598 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18599 || spec
== NULL_TREE
);
18601 /* This needs to happen before any tsubsting. */
18602 if (! push_tinst_level (d
))
18605 timevar_push (TV_TEMPLATE_INST
);
18607 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18608 for the instantiation. */
18609 td
= template_for_substitution (d
);
18610 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18612 /* We should never be trying to instantiate a member of a class
18613 template or partial specialization. */
18614 gcc_assert (d
!= code_pattern
);
18616 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18617 || DECL_TEMPLATE_SPECIALIZATION (td
))
18618 /* In the case of a friend template whose definition is provided
18619 outside the class, we may have too many arguments. Drop the
18620 ones we don't need. The same is true for specializations. */
18621 args
= get_innermost_template_args
18622 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18626 if (TREE_CODE (d
) == FUNCTION_DECL
)
18627 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18628 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18630 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18632 /* We may be in the middle of deferred access check. Disable it now. */
18633 push_deferring_access_checks (dk_no_deferred
);
18635 /* Unless an explicit instantiation directive has already determined
18636 the linkage of D, remember that a definition is available for
18638 if (pattern_defined
18639 && !DECL_INTERFACE_KNOWN (d
)
18640 && !DECL_NOT_REALLY_EXTERN (d
))
18641 mark_definable (d
);
18643 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18644 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18645 input_location
= DECL_SOURCE_LOCATION (d
);
18647 /* If D is a member of an explicitly instantiated class template,
18648 and no definition is available, treat it like an implicit
18650 if (!pattern_defined
&& expl_inst_class_mem_p
18651 && DECL_EXPLICIT_INSTANTIATION (d
))
18653 /* Leave linkage flags alone on instantiations with anonymous
18655 if (TREE_PUBLIC (d
))
18657 DECL_NOT_REALLY_EXTERN (d
) = 0;
18658 DECL_INTERFACE_KNOWN (d
) = 0;
18660 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18663 if (TREE_CODE (d
) == FUNCTION_DECL
)
18664 maybe_instantiate_noexcept (d
);
18666 /* Defer all other templates, unless we have been explicitly
18667 forbidden from doing so. */
18668 if (/* If there is no definition, we cannot instantiate the
18671 /* If it's OK to postpone instantiation, do so. */
18673 /* If this is a static data member that will be defined
18674 elsewhere, we don't want to instantiate the entire data
18675 member, but we do want to instantiate the initializer so that
18676 we can substitute that elsewhere. */
18677 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18679 /* The definition of the static data member is now required so
18680 we must substitute the initializer. */
18681 if (TREE_CODE (d
) == VAR_DECL
18682 && !DECL_INITIAL (d
)
18683 && DECL_INITIAL (code_pattern
))
18687 bool const_init
= false;
18689 ns
= decl_namespace_context (d
);
18690 push_nested_namespace (ns
);
18691 push_nested_class (DECL_CONTEXT (d
));
18692 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18694 tf_warning_or_error
, NULL_TREE
,
18695 /*integral_constant_expression_p=*/false);
18696 /* Make sure the initializer is still constant, in case of
18697 circular dependency (template/instantiate6.C). */
18699 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18700 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18701 /*asmspec_tree=*/NULL_TREE
,
18702 LOOKUP_ONLYCONVERTING
);
18703 pop_nested_class ();
18704 pop_nested_namespace (ns
);
18707 /* We restore the source position here because it's used by
18708 add_pending_template. */
18709 input_location
= saved_loc
;
18711 if (at_eof
&& !pattern_defined
18712 && DECL_EXPLICIT_INSTANTIATION (d
)
18713 && DECL_NOT_REALLY_EXTERN (d
))
18716 The definition of a non-exported function template, a
18717 non-exported member function template, or a non-exported
18718 member function or static data member of a class template
18719 shall be present in every translation unit in which it is
18720 explicitly instantiated. */
18721 permerror (input_location
, "explicit instantiation of %qD "
18722 "but no definition available", d
);
18724 /* If we're in unevaluated context, we just wanted to get the
18725 constant value; this isn't an odr use, so don't queue
18726 a full instantiation. */
18727 if (cp_unevaluated_operand
!= 0)
18729 /* ??? Historically, we have instantiated inline functions, even
18730 when marked as "extern template". */
18731 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18732 add_pending_template (d
);
18735 /* Tell the repository that D is available in this translation unit
18736 -- and see if it is supposed to be instantiated here. */
18737 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18739 /* In a PCH file, despite the fact that the repository hasn't
18740 requested instantiation in the PCH it is still possible that
18741 an instantiation will be required in a file that includes the
18744 add_pending_template (d
);
18745 /* Instantiate inline functions so that the inliner can do its
18746 job, even though we'll not be emitting a copy of this
18748 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18752 fn_context
= decl_function_context (d
);
18753 nested
= (current_function_decl
!= NULL_TREE
);
18755 push_to_top_level ();
18757 push_function_context ();
18759 /* Mark D as instantiated so that recursive calls to
18760 instantiate_decl do not try to instantiate it again. */
18761 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18763 /* Regenerate the declaration in case the template has been modified
18764 by a subsequent redeclaration. */
18765 regenerate_decl_from_template (d
, td
);
18767 /* We already set the file and line above. Reset them now in case
18768 they changed as a result of calling regenerate_decl_from_template. */
18769 input_location
= DECL_SOURCE_LOCATION (d
);
18771 if (TREE_CODE (d
) == VAR_DECL
)
18774 bool const_init
= false;
18776 /* Clear out DECL_RTL; whatever was there before may not be right
18777 since we've reset the type of the declaration. */
18778 SET_DECL_RTL (d
, NULL
);
18779 DECL_IN_AGGR_P (d
) = 0;
18781 /* The initializer is placed in DECL_INITIAL by
18782 regenerate_decl_from_template so we don't need to
18783 push/pop_access_scope again here. Pull it out so that
18784 cp_finish_decl can process it. */
18785 init
= DECL_INITIAL (d
);
18786 DECL_INITIAL (d
) = NULL_TREE
;
18787 DECL_INITIALIZED_P (d
) = 0;
18789 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18790 initializer. That function will defer actual emission until
18791 we have a chance to determine linkage. */
18792 DECL_EXTERNAL (d
) = 0;
18794 /* Enter the scope of D so that access-checking works correctly. */
18795 push_nested_class (DECL_CONTEXT (d
));
18796 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18797 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18798 pop_nested_class ();
18800 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18801 synthesize_method (d
);
18802 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18804 struct pointer_map_t
*saved_local_specializations
;
18809 /* Save away the current list, in case we are instantiating one
18810 template from within the body of another. */
18811 saved_local_specializations
= local_specializations
;
18813 /* Set up the list of local specializations. */
18814 local_specializations
= pointer_map_create ();
18816 /* Set up context. */
18817 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18819 /* Some typedefs referenced from within the template code need to be
18820 access checked at template instantiation time, i.e now. These
18821 types were added to the template at parsing time. Let's get those
18822 and perform the access checks then. */
18823 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18826 /* Create substitution entries for the parameters. */
18827 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18828 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18829 spec_parm
= DECL_ARGUMENTS (d
);
18830 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18832 register_local_specialization (spec_parm
, tmpl_parm
);
18833 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18834 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18836 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18838 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18840 register_local_specialization (spec_parm
, tmpl_parm
);
18841 spec_parm
= DECL_CHAIN (spec_parm
);
18845 /* Register the (value) argument pack as a specialization of
18846 TMPL_PARM, then move on. */
18847 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18848 register_local_specialization (argpack
, tmpl_parm
);
18851 gcc_assert (!spec_parm
);
18853 /* Substitute into the body of the function. */
18854 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18855 tf_warning_or_error
, tmpl
,
18856 /*integral_constant_expression_p=*/false);
18858 /* Set the current input_location to the end of the function
18859 so that finish_function knows where we are. */
18860 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18862 /* We don't need the local specializations any more. */
18863 pointer_map_destroy (local_specializations
);
18864 local_specializations
= saved_local_specializations
;
18866 /* Finish the function. */
18867 d
= finish_function (0);
18868 expand_or_defer_fn (d
);
18871 /* We're not deferring instantiation any more. */
18872 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18875 pop_from_top_level ();
18877 pop_function_context ();
18880 input_location
= saved_loc
;
18881 pop_deferring_access_checks ();
18882 pop_tinst_level ();
18884 timevar_pop (TV_TEMPLATE_INST
);
18889 /* Run through the list of templates that we wish we could
18890 instantiate, and instantiate any we can. RETRIES is the
18891 number of times we retry pending template instantiation. */
18894 instantiate_pending_templates (int retries
)
18897 location_t saved_loc
= input_location
;
18899 /* Instantiating templates may trigger vtable generation. This in turn
18900 may require further template instantiations. We place a limit here
18901 to avoid infinite loop. */
18902 if (pending_templates
&& retries
>= max_tinst_depth
)
18904 tree decl
= pending_templates
->tinst
->decl
;
18906 error ("template instantiation depth exceeds maximum of %d"
18907 " instantiating %q+D, possibly from virtual table generation"
18908 " (use -ftemplate-depth= to increase the maximum)",
18909 max_tinst_depth
, decl
);
18910 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18911 /* Pretend that we defined it. */
18912 DECL_INITIAL (decl
) = error_mark_node
;
18918 struct pending_template
**t
= &pending_templates
;
18919 struct pending_template
*last
= NULL
;
18923 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18924 bool complete
= false;
18926 if (TYPE_P (instantiation
))
18930 if (!COMPLETE_TYPE_P (instantiation
))
18932 instantiate_class_template (instantiation
);
18933 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18934 for (fn
= TYPE_METHODS (instantiation
);
18936 fn
= TREE_CHAIN (fn
))
18937 if (! DECL_ARTIFICIAL (fn
))
18938 instantiate_decl (fn
,
18940 /*expl_inst_class_mem_p=*/false);
18941 if (COMPLETE_TYPE_P (instantiation
))
18945 complete
= COMPLETE_TYPE_P (instantiation
);
18949 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18950 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18953 = instantiate_decl (instantiation
,
18955 /*expl_inst_class_mem_p=*/false);
18956 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18960 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18961 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18965 /* If INSTANTIATION has been instantiated, then we don't
18966 need to consider it again in the future. */
18974 current_tinst_level
= NULL
;
18976 last_pending_template
= last
;
18978 while (reconsider
);
18980 input_location
= saved_loc
;
18983 /* Substitute ARGVEC into T, which is a list of initializers for
18984 either base class or a non-static data member. The TREE_PURPOSEs
18985 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18986 instantiate_decl. */
18989 tsubst_initializer_list (tree t
, tree argvec
)
18991 tree inits
= NULL_TREE
;
18993 for (; t
; t
= TREE_CHAIN (t
))
18997 tree expanded_bases
= NULL_TREE
;
18998 tree expanded_arguments
= NULL_TREE
;
19001 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19006 /* Expand the base class expansion type into separate base
19008 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19009 tf_warning_or_error
,
19011 if (expanded_bases
== error_mark_node
)
19014 /* We'll be building separate TREE_LISTs of arguments for
19016 len
= TREE_VEC_LENGTH (expanded_bases
);
19017 expanded_arguments
= make_tree_vec (len
);
19018 for (i
= 0; i
< len
; i
++)
19019 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19021 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19022 expand each argument in the TREE_VALUE of t. */
19023 expr
= make_node (EXPR_PACK_EXPANSION
);
19024 PACK_EXPANSION_LOCAL_P (expr
) = true;
19025 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19026 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19028 if (TREE_VALUE (t
) == void_type_node
)
19029 /* VOID_TYPE_NODE is used to indicate
19030 value-initialization. */
19032 for (i
= 0; i
< len
; i
++)
19033 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19037 /* Substitute parameter packs into each argument in the
19039 in_base_initializer
= 1;
19040 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19042 tree expanded_exprs
;
19044 /* Expand the argument. */
19045 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19047 = tsubst_pack_expansion (expr
, argvec
,
19048 tf_warning_or_error
,
19050 if (expanded_exprs
== error_mark_node
)
19053 /* Prepend each of the expanded expressions to the
19054 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19055 for (i
= 0; i
< len
; i
++)
19057 TREE_VEC_ELT (expanded_arguments
, i
) =
19058 tree_cons (NULL_TREE
,
19059 TREE_VEC_ELT (expanded_exprs
, i
),
19060 TREE_VEC_ELT (expanded_arguments
, i
));
19063 in_base_initializer
= 0;
19065 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19066 since we built them backwards. */
19067 for (i
= 0; i
< len
; i
++)
19069 TREE_VEC_ELT (expanded_arguments
, i
) =
19070 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19075 for (i
= 0; i
< len
; ++i
)
19077 if (expanded_bases
)
19079 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19080 decl
= expand_member_init (decl
);
19081 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19086 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19087 tf_warning_or_error
, NULL_TREE
);
19089 decl
= expand_member_init (decl
);
19090 if (decl
&& !DECL_P (decl
))
19091 in_base_initializer
= 1;
19093 init
= TREE_VALUE (t
);
19095 if (init
!= void_type_node
)
19096 init
= tsubst_expr (init
, argvec
,
19097 tf_warning_or_error
, NULL_TREE
,
19098 /*integral_constant_expression_p=*/false);
19099 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19100 /* If we had an initializer but it instantiated to nothing,
19101 value-initialize the object. This will only occur when
19102 the initializer was a pack expansion where the parameter
19103 packs used in that expansion were of length zero. */
19104 init
= void_type_node
;
19105 in_base_initializer
= 0;
19110 init
= build_tree_list (decl
, init
);
19111 TREE_CHAIN (init
) = inits
;
19119 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19122 set_current_access_from_decl (tree decl
)
19124 if (TREE_PRIVATE (decl
))
19125 current_access_specifier
= access_private_node
;
19126 else if (TREE_PROTECTED (decl
))
19127 current_access_specifier
= access_protected_node
;
19129 current_access_specifier
= access_public_node
;
19132 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19133 is the instantiation (which should have been created with
19134 start_enum) and ARGS are the template arguments to use. */
19137 tsubst_enum (tree tag
, tree newtag
, tree args
)
19141 if (SCOPED_ENUM_P (newtag
))
19142 begin_scope (sk_scoped_enum
, newtag
);
19144 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19149 decl
= TREE_VALUE (e
);
19150 /* Note that in a template enum, the TREE_VALUE is the
19151 CONST_DECL, not the corresponding INTEGER_CST. */
19152 value
= tsubst_expr (DECL_INITIAL (decl
),
19153 args
, tf_warning_or_error
, NULL_TREE
,
19154 /*integral_constant_expression_p=*/true);
19156 /* Give this enumeration constant the correct access. */
19157 set_current_access_from_decl (decl
);
19159 /* Actually build the enumerator itself. */
19161 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19164 if (SCOPED_ENUM_P (newtag
))
19167 finish_enum_value_list (newtag
);
19168 finish_enum (newtag
);
19170 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19171 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19174 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19175 its type -- but without substituting the innermost set of template
19176 arguments. So, innermost set of template parameters will appear in
19180 get_mostly_instantiated_function_type (tree decl
)
19188 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19189 targs
= DECL_TI_ARGS (decl
);
19190 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19191 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19193 /* There should be as many levels of arguments as there are levels
19195 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19197 fn_type
= TREE_TYPE (tmpl
);
19199 if (parm_depth
== 1)
19200 /* No substitution is necessary. */
19207 /* Replace the innermost level of the TARGS with NULL_TREEs to
19208 let tsubst know not to substitute for those parameters. */
19209 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19210 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19211 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19212 TMPL_ARGS_LEVEL (targs
, i
));
19213 SET_TMPL_ARGS_LEVEL (partial_args
,
19214 TMPL_ARGS_DEPTH (targs
),
19215 make_tree_vec (DECL_NTPARMS (tmpl
)));
19217 /* Make sure that we can see identifiers, and compute access
19219 push_access_scope (decl
);
19221 ++processing_template_decl
;
19222 /* Now, do the (partial) substitution to figure out the
19223 appropriate function type. */
19224 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19225 --processing_template_decl
;
19227 /* Substitute into the template parameters to obtain the real
19228 innermost set of parameters. This step is important if the
19229 innermost set of template parameters contains value
19230 parameters whose types depend on outer template parameters. */
19231 TREE_VEC_LENGTH (partial_args
)--;
19232 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19234 pop_access_scope (decl
);
19240 /* Return truthvalue if we're processing a template different from
19241 the last one involved in diagnostics. */
19243 problematic_instantiation_changed (void)
19245 return current_tinst_level
!= last_error_tinst_level
;
19248 /* Remember current template involved in diagnostics. */
19250 record_last_problematic_instantiation (void)
19252 last_error_tinst_level
= current_tinst_level
;
19255 struct tinst_level
*
19256 current_instantiation (void)
19258 return current_tinst_level
;
19261 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19262 type. Return zero for ok, nonzero for disallowed. Issue error and
19263 warning messages under control of COMPLAIN. */
19266 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19268 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19270 else if (POINTER_TYPE_P (type
))
19272 else if (TYPE_PTRMEM_P (type
))
19274 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19276 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19278 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19280 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19283 if (complain
& tf_error
)
19285 if (type
== error_mark_node
)
19286 inform (input_location
, "invalid template non-type parameter");
19288 error ("%q#T is not a valid type for a template non-type parameter",
19294 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19295 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19298 dependent_type_p_r (tree type
)
19304 A type is dependent if it is:
19306 -- a template parameter. Template template parameters are types
19307 for us (since TYPE_P holds true for them) so we handle
19309 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19310 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19312 /* -- a qualified-id with a nested-name-specifier which contains a
19313 class-name that names a dependent type or whose unqualified-id
19314 names a dependent type. */
19315 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19317 /* -- a cv-qualified type where the cv-unqualified type is
19319 type
= TYPE_MAIN_VARIANT (type
);
19320 /* -- a compound type constructed from any dependent type. */
19321 if (TYPE_PTRMEM_P (type
))
19322 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19323 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19325 else if (TREE_CODE (type
) == POINTER_TYPE
19326 || TREE_CODE (type
) == REFERENCE_TYPE
)
19327 return dependent_type_p (TREE_TYPE (type
));
19328 else if (TREE_CODE (type
) == FUNCTION_TYPE
19329 || TREE_CODE (type
) == METHOD_TYPE
)
19333 if (dependent_type_p (TREE_TYPE (type
)))
19335 for (arg_type
= TYPE_ARG_TYPES (type
);
19337 arg_type
= TREE_CHAIN (arg_type
))
19338 if (dependent_type_p (TREE_VALUE (arg_type
)))
19342 /* -- an array type constructed from any dependent type or whose
19343 size is specified by a constant expression that is
19346 We checked for type- and value-dependence of the bounds in
19347 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19348 if (TREE_CODE (type
) == ARRAY_TYPE
)
19350 if (TYPE_DOMAIN (type
)
19351 && dependent_type_p (TYPE_DOMAIN (type
)))
19353 return dependent_type_p (TREE_TYPE (type
));
19356 /* -- a template-id in which either the template name is a template
19358 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19360 /* ... or any of the template arguments is a dependent type or
19361 an expression that is type-dependent or value-dependent. */
19362 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19363 && (any_dependent_template_arguments_p
19364 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19367 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19368 dependent; if the argument of the `typeof' expression is not
19369 type-dependent, then it should already been have resolved. */
19370 if (TREE_CODE (type
) == TYPEOF_TYPE
19371 || TREE_CODE (type
) == DECLTYPE_TYPE
19372 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19375 /* A template argument pack is dependent if any of its packed
19377 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19379 tree args
= ARGUMENT_PACK_ARGS (type
);
19380 int i
, len
= TREE_VEC_LENGTH (args
);
19381 for (i
= 0; i
< len
; ++i
)
19382 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19386 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19387 be template parameters. */
19388 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19391 /* The standard does not specifically mention types that are local
19392 to template functions or local classes, but they should be
19393 considered dependent too. For example:
19395 template <int I> void f() {
19400 The size of `E' cannot be known until the value of `I' has been
19401 determined. Therefore, `E' must be considered dependent. */
19402 scope
= TYPE_CONTEXT (type
);
19403 if (scope
&& TYPE_P (scope
))
19404 return dependent_type_p (scope
);
19405 /* Don't use type_dependent_expression_p here, as it can lead
19406 to infinite recursion trying to determine whether a lambda
19407 nested in a lambda is dependent (c++/47687). */
19408 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19409 && DECL_LANG_SPECIFIC (scope
)
19410 && DECL_TEMPLATE_INFO (scope
)
19411 && (any_dependent_template_arguments_p
19412 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19415 /* Other types are non-dependent. */
19419 /* Returns TRUE if TYPE is dependent, in the sense of
19420 [temp.dep.type]. Note that a NULL type is considered dependent. */
19423 dependent_type_p (tree type
)
19425 /* If there are no template parameters in scope, then there can't be
19426 any dependent types. */
19427 if (!processing_template_decl
)
19429 /* If we are not processing a template, then nobody should be
19430 providing us with a dependent type. */
19432 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19436 /* If the type is NULL, we have not computed a type for the entity
19437 in question; in that case, the type is dependent. */
19441 /* Erroneous types can be considered non-dependent. */
19442 if (type
== error_mark_node
)
19445 /* If we have not already computed the appropriate value for TYPE,
19447 if (!TYPE_DEPENDENT_P_VALID (type
))
19449 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19450 TYPE_DEPENDENT_P_VALID (type
) = 1;
19453 return TYPE_DEPENDENT_P (type
);
19456 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19457 lookup. In other words, a dependent type that is not the current
19461 dependent_scope_p (tree scope
)
19463 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19464 && !currently_open_class (scope
));
19467 /* T is a SCOPE_REF; return whether we need to consider it
19468 instantiation-dependent so that we can check access at instantiation
19469 time even though we know which member it resolves to. */
19472 instantiation_dependent_scope_ref_p (tree t
)
19474 if (DECL_P (TREE_OPERAND (t
, 1))
19475 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
19476 && accessible_in_template_p (TREE_OPERAND (t
, 0),
19477 TREE_OPERAND (t
, 1)))
19483 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19484 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19487 /* Note that this predicate is not appropriate for general expressions;
19488 only constant expressions (that satisfy potential_constant_expression)
19489 can be tested for value dependence. */
19492 value_dependent_expression_p (tree expression
)
19494 if (!processing_template_decl
)
19497 /* A name declared with a dependent type. */
19498 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19501 switch (TREE_CODE (expression
))
19503 case IDENTIFIER_NODE
:
19504 /* A name that has not been looked up -- must be dependent. */
19507 case TEMPLATE_PARM_INDEX
:
19508 /* A non-type template parm. */
19512 /* A non-type template parm. */
19513 if (DECL_TEMPLATE_PARM_P (expression
))
19515 return value_dependent_expression_p (DECL_INITIAL (expression
));
19518 /* A constant with literal type and is initialized
19519 with an expression that is value-dependent.
19521 Note that a non-dependent parenthesized initializer will have
19522 already been replaced with its constant value, so if we see
19523 a TREE_LIST it must be dependent. */
19524 if (DECL_INITIAL (expression
)
19525 && decl_constant_var_p (expression
)
19526 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19527 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19531 case DYNAMIC_CAST_EXPR
:
19532 case STATIC_CAST_EXPR
:
19533 case CONST_CAST_EXPR
:
19534 case REINTERPRET_CAST_EXPR
:
19536 /* These expressions are value-dependent if the type to which
19537 the cast occurs is dependent or the expression being casted
19538 is value-dependent. */
19540 tree type
= TREE_TYPE (expression
);
19542 if (dependent_type_p (type
))
19545 /* A functional cast has a list of operands. */
19546 expression
= TREE_OPERAND (expression
, 0);
19549 /* If there are no operands, it must be an expression such
19550 as "int()". This should not happen for aggregate types
19551 because it would form non-constant expressions. */
19552 gcc_assert (cxx_dialect
>= cxx0x
19553 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19558 if (TREE_CODE (expression
) == TREE_LIST
)
19559 return any_value_dependent_elements_p (expression
);
19561 return value_dependent_expression_p (expression
);
19565 if (SIZEOF_EXPR_TYPE_P (expression
))
19566 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
19570 /* A `sizeof' expression is value-dependent if the operand is
19571 type-dependent or is a pack expansion. */
19572 expression
= TREE_OPERAND (expression
, 0);
19573 if (PACK_EXPANSION_P (expression
))
19575 else if (TYPE_P (expression
))
19576 return dependent_type_p (expression
);
19577 return instantiation_dependent_expression_p (expression
);
19579 case AT_ENCODE_EXPR
:
19580 /* An 'encode' expression is value-dependent if the operand is
19582 expression
= TREE_OPERAND (expression
, 0);
19583 return dependent_type_p (expression
);
19585 case NOEXCEPT_EXPR
:
19586 expression
= TREE_OPERAND (expression
, 0);
19587 return instantiation_dependent_expression_p (expression
);
19590 /* All instantiation-dependent expressions should also be considered
19591 value-dependent. */
19592 return instantiation_dependent_scope_ref_p (expression
);
19594 case COMPONENT_REF
:
19595 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19596 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19598 case NONTYPE_ARGUMENT_PACK
:
19599 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19600 is value-dependent. */
19602 tree values
= ARGUMENT_PACK_ARGS (expression
);
19603 int i
, len
= TREE_VEC_LENGTH (values
);
19605 for (i
= 0; i
< len
; ++i
)
19606 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19614 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19615 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19616 || (type2
? dependent_type_p (type2
) : false));
19620 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19621 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19624 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19625 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19629 tree op
= TREE_OPERAND (expression
, 0);
19630 return (value_dependent_expression_p (op
)
19631 || has_value_dependent_address (op
));
19636 tree fn
= get_callee_fndecl (expression
);
19638 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19640 nargs
= call_expr_nargs (expression
);
19641 for (i
= 0; i
< nargs
; ++i
)
19643 tree op
= CALL_EXPR_ARG (expression
, i
);
19644 /* In a call to a constexpr member function, look through the
19645 implicit ADDR_EXPR on the object argument so that it doesn't
19646 cause the call to be considered value-dependent. We also
19647 look through it in potential_constant_expression. */
19648 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19649 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19650 && TREE_CODE (op
) == ADDR_EXPR
)
19651 op
= TREE_OPERAND (op
, 0);
19652 if (value_dependent_expression_p (op
))
19658 case TEMPLATE_ID_EXPR
:
19659 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19661 return type_dependent_expression_p (expression
);
19667 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19668 if (value_dependent_expression_p (val
))
19674 /* Treat a GNU statement expression as dependent to avoid crashing
19675 under fold_non_dependent_expr; it can't be constant. */
19679 /* A constant expression is value-dependent if any subexpression is
19680 value-dependent. */
19681 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19683 case tcc_reference
:
19685 case tcc_comparison
:
19687 case tcc_expression
:
19690 int i
, len
= cp_tree_operand_length (expression
);
19692 for (i
= 0; i
< len
; i
++)
19694 tree t
= TREE_OPERAND (expression
, i
);
19696 /* In some cases, some of the operands may be missing.l
19697 (For example, in the case of PREDECREMENT_EXPR, the
19698 amount to increment by may be missing.) That doesn't
19699 make the expression dependent. */
19700 if (t
&& value_dependent_expression_p (t
))
19711 /* The expression is not value-dependent. */
19715 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19716 [temp.dep.expr]. Note that an expression with no type is
19717 considered dependent. Other parts of the compiler arrange for an
19718 expression with type-dependent subexpressions to have no type, so
19719 this function doesn't have to be fully recursive. */
19722 type_dependent_expression_p (tree expression
)
19724 if (!processing_template_decl
)
19727 if (expression
== error_mark_node
)
19730 /* An unresolved name is always dependent. */
19731 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19732 || TREE_CODE (expression
) == USING_DECL
)
19735 /* Some expression forms are never type-dependent. */
19736 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19737 || TREE_CODE (expression
) == SIZEOF_EXPR
19738 || TREE_CODE (expression
) == ALIGNOF_EXPR
19739 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19740 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19741 || TREE_CODE (expression
) == TRAIT_EXPR
19742 || TREE_CODE (expression
) == TYPEID_EXPR
19743 || TREE_CODE (expression
) == DELETE_EXPR
19744 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19745 || TREE_CODE (expression
) == THROW_EXPR
)
19748 /* The types of these expressions depends only on the type to which
19749 the cast occurs. */
19750 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19751 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19752 || TREE_CODE (expression
) == CONST_CAST_EXPR
19753 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19754 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19755 || TREE_CODE (expression
) == CAST_EXPR
)
19756 return dependent_type_p (TREE_TYPE (expression
));
19758 /* The types of these expressions depends only on the type created
19759 by the expression. */
19760 if (TREE_CODE (expression
) == NEW_EXPR
19761 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19763 /* For NEW_EXPR tree nodes created inside a template, either
19764 the object type itself or a TREE_LIST may appear as the
19766 tree type
= TREE_OPERAND (expression
, 1);
19767 if (TREE_CODE (type
) == TREE_LIST
)
19768 /* This is an array type. We need to check array dimensions
19770 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19771 || value_dependent_expression_p
19772 (TREE_OPERAND (TREE_VALUE (type
), 1));
19774 return dependent_type_p (type
);
19777 if (TREE_CODE (expression
) == SCOPE_REF
)
19779 tree scope
= TREE_OPERAND (expression
, 0);
19780 tree name
= TREE_OPERAND (expression
, 1);
19782 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19783 contains an identifier associated by name lookup with one or more
19784 declarations declared with a dependent type, or...a
19785 nested-name-specifier or qualified-id that names a member of an
19786 unknown specialization. */
19787 return (type_dependent_expression_p (name
)
19788 || dependent_scope_p (scope
));
19791 if (TREE_CODE (expression
) == FUNCTION_DECL
19792 && DECL_LANG_SPECIFIC (expression
)
19793 && DECL_TEMPLATE_INFO (expression
)
19794 && (any_dependent_template_arguments_p
19795 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19798 if (TREE_CODE (expression
) == TEMPLATE_DECL
19799 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19802 if (TREE_CODE (expression
) == STMT_EXPR
)
19803 expression
= stmt_expr_value_expr (expression
);
19805 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19810 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19812 if (type_dependent_expression_p (elt
))
19818 /* A static data member of the current instantiation with incomplete
19819 array type is type-dependent, as the definition and specializations
19820 can have different bounds. */
19821 if (TREE_CODE (expression
) == VAR_DECL
19822 && DECL_CLASS_SCOPE_P (expression
)
19823 && dependent_type_p (DECL_CONTEXT (expression
))
19824 && VAR_HAD_UNKNOWN_BOUND (expression
))
19827 if (TREE_TYPE (expression
) == unknown_type_node
)
19829 if (TREE_CODE (expression
) == ADDR_EXPR
)
19830 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19831 if (TREE_CODE (expression
) == COMPONENT_REF
19832 || TREE_CODE (expression
) == OFFSET_REF
)
19834 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19836 expression
= TREE_OPERAND (expression
, 1);
19837 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19840 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19841 if (TREE_CODE (expression
) == SCOPE_REF
)
19844 if (BASELINK_P (expression
))
19845 expression
= BASELINK_FUNCTIONS (expression
);
19847 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19849 if (any_dependent_template_arguments_p
19850 (TREE_OPERAND (expression
, 1)))
19852 expression
= TREE_OPERAND (expression
, 0);
19854 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19855 || TREE_CODE (expression
) == FUNCTION_DECL
);
19859 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19861 expression
= OVL_NEXT (expression
);
19866 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19868 return (dependent_type_p (TREE_TYPE (expression
)));
19871 /* walk_tree callback function for instantiation_dependent_expression_p,
19872 below. Returns non-zero if a dependent subexpression is found. */
19875 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19880 /* We don't have to worry about decltype currently because decltype
19881 of an instantiation-dependent expr is a dependent type. This
19882 might change depending on the resolution of DR 1172. */
19883 *walk_subtrees
= false;
19886 enum tree_code code
= TREE_CODE (*tp
);
19889 /* Don't treat an argument list as dependent just because it has no
19895 case TEMPLATE_PARM_INDEX
:
19898 /* Handle expressions with type operands. */
19902 case AT_ENCODE_EXPR
:
19904 tree op
= TREE_OPERAND (*tp
, 0);
19905 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
19906 op
= TREE_TYPE (op
);
19909 if (dependent_type_p (op
))
19913 *walk_subtrees
= false;
19921 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
19922 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
)))
19924 *walk_subtrees
= false;
19927 case COMPONENT_REF
:
19928 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19929 /* In a template, finish_class_member_access_expr creates a
19930 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19931 type-dependent, so that we can check access control at
19932 instantiation time (PR 42277). See also Core issue 1273. */
19937 if (instantiation_dependent_scope_ref_p (*tp
))
19942 /* Treat statement-expressions as dependent. */
19950 if (type_dependent_expression_p (*tp
))
19956 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19957 sense defined by the ABI:
19959 "An expression is instantiation-dependent if it is type-dependent
19960 or value-dependent, or it has a subexpression that is type-dependent
19961 or value-dependent." */
19964 instantiation_dependent_expression_p (tree expression
)
19968 if (!processing_template_decl
)
19971 if (expression
== error_mark_node
)
19974 result
= cp_walk_tree_without_duplicates (&expression
,
19975 instantiation_dependent_r
, NULL
);
19976 return result
!= NULL_TREE
;
19979 /* Like type_dependent_expression_p, but it also works while not processing
19980 a template definition, i.e. during substitution or mangling. */
19983 type_dependent_expression_p_push (tree expr
)
19986 ++processing_template_decl
;
19987 b
= type_dependent_expression_p (expr
);
19988 --processing_template_decl
;
19992 /* Returns TRUE if ARGS contains a type-dependent expression. */
19995 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20000 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20002 if (type_dependent_expression_p (arg
))
20008 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20009 expressions) contains any type-dependent expressions. */
20012 any_type_dependent_elements_p (const_tree list
)
20014 for (; list
; list
= TREE_CHAIN (list
))
20015 if (value_dependent_expression_p (TREE_VALUE (list
)))
20021 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20022 expressions) contains any value-dependent expressions. */
20025 any_value_dependent_elements_p (const_tree list
)
20027 for (; list
; list
= TREE_CHAIN (list
))
20028 if (value_dependent_expression_p (TREE_VALUE (list
)))
20034 /* Returns TRUE if the ARG (a template argument) is dependent. */
20037 dependent_template_arg_p (tree arg
)
20039 if (!processing_template_decl
)
20042 /* Assume a template argument that was wrongly written by the user
20043 is dependent. This is consistent with what
20044 any_dependent_template_arguments_p [that calls this function]
20046 if (!arg
|| arg
== error_mark_node
)
20049 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20050 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20052 if (TREE_CODE (arg
) == TEMPLATE_DECL
20053 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20054 return dependent_template_p (arg
);
20055 else if (ARGUMENT_PACK_P (arg
))
20057 tree args
= ARGUMENT_PACK_ARGS (arg
);
20058 int i
, len
= TREE_VEC_LENGTH (args
);
20059 for (i
= 0; i
< len
; ++i
)
20061 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20067 else if (TYPE_P (arg
))
20068 return dependent_type_p (arg
);
20070 return (type_dependent_expression_p (arg
)
20071 || value_dependent_expression_p (arg
));
20074 /* Returns true if ARGS (a collection of template arguments) contains
20075 any types that require structural equality testing. */
20078 any_template_arguments_need_structural_equality_p (tree args
)
20085 if (args
== error_mark_node
)
20088 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20090 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20091 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20093 tree arg
= TREE_VEC_ELT (level
, j
);
20094 tree packed_args
= NULL_TREE
;
20097 if (ARGUMENT_PACK_P (arg
))
20099 /* Look inside the argument pack. */
20100 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20101 len
= TREE_VEC_LENGTH (packed_args
);
20104 for (k
= 0; k
< len
; ++k
)
20107 arg
= TREE_VEC_ELT (packed_args
, k
);
20109 if (error_operand_p (arg
))
20111 else if (TREE_CODE (arg
) == TEMPLATE_DECL
20112 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20114 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20116 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20117 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20126 /* Returns true if ARGS (a collection of template arguments) contains
20127 any dependent arguments. */
20130 any_dependent_template_arguments_p (const_tree args
)
20137 if (args
== error_mark_node
)
20140 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20142 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20143 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20144 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20151 /* Returns TRUE if the template TMPL is dependent. */
20154 dependent_template_p (tree tmpl
)
20156 if (TREE_CODE (tmpl
) == OVERLOAD
)
20160 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20162 tmpl
= OVL_NEXT (tmpl
);
20167 /* Template template parameters are dependent. */
20168 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20169 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20171 /* So are names that have not been looked up. */
20172 if (TREE_CODE (tmpl
) == SCOPE_REF
20173 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
20175 /* So are member templates of dependent classes. */
20176 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20177 return dependent_type_p (DECL_CONTEXT (tmpl
));
20181 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20184 dependent_template_id_p (tree tmpl
, tree args
)
20186 return (dependent_template_p (tmpl
)
20187 || any_dependent_template_arguments_p (args
));
20190 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20194 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20198 if (!processing_template_decl
)
20201 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20203 tree decl
= TREE_VEC_ELT (declv
, i
);
20204 tree init
= TREE_VEC_ELT (initv
, i
);
20205 tree cond
= TREE_VEC_ELT (condv
, i
);
20206 tree incr
= TREE_VEC_ELT (incrv
, i
);
20208 if (type_dependent_expression_p (decl
))
20211 if (init
&& type_dependent_expression_p (init
))
20214 if (type_dependent_expression_p (cond
))
20217 if (COMPARISON_CLASS_P (cond
)
20218 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20219 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20222 if (TREE_CODE (incr
) == MODOP_EXPR
)
20224 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20225 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20228 else if (type_dependent_expression_p (incr
))
20230 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20232 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20234 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20236 tree t
= TREE_OPERAND (incr
, 1);
20237 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20238 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20247 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20248 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20249 no such TYPE can be found. Note that this function peers inside
20250 uninstantiated templates and therefore should be used only in
20251 extremely limited situations. ONLY_CURRENT_P restricts this
20252 peering to the currently open classes hierarchy (which is required
20253 when comparing types). */
20256 resolve_typename_type (tree type
, bool only_current_p
)
20265 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20267 scope
= TYPE_CONTEXT (type
);
20268 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20269 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20270 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20271 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20272 identifier of the TYPENAME_TYPE anymore.
20273 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20274 TYPENAME_TYPE instead, we avoid messing up with a possible
20275 typedef variant case. */
20276 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20278 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20279 it first before we can figure out what NAME refers to. */
20280 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20282 if (TYPENAME_IS_RESOLVING_P (scope
))
20283 /* Given a class template A with a dependent base with nested type C,
20284 typedef typename A::C::C C will land us here, as trying to resolve
20285 the initial A::C leads to the local C typedef, which leads back to
20286 A::C::C. So we break the recursion now. */
20289 scope
= resolve_typename_type (scope
, only_current_p
);
20291 /* If we don't know what SCOPE refers to, then we cannot resolve the
20293 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20295 /* If the SCOPE is a template type parameter, we have no way of
20296 resolving the name. */
20297 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20299 /* If the SCOPE is not the current instantiation, there's no reason
20300 to look inside it. */
20301 if (only_current_p
&& !currently_open_class (scope
))
20303 /* If this is a typedef, we don't want to look inside (c++/11987). */
20304 if (typedef_variant_p (type
))
20306 /* If SCOPE isn't the template itself, it will not have a valid
20307 TYPE_FIELDS list. */
20308 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20309 /* scope is either the template itself or a compatible instantiation
20310 like X<T>, so look up the name in the original template. */
20311 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20313 /* scope is a partial instantiation, so we can't do the lookup or we
20314 will lose the template arguments. */
20316 /* Enter the SCOPE so that name lookup will be resolved as if we
20317 were in the class definition. In particular, SCOPE will no
20318 longer be considered a dependent type. */
20319 pushed_scope
= push_scope (scope
);
20320 /* Look up the declaration. */
20321 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20322 tf_warning_or_error
);
20324 result
= NULL_TREE
;
20326 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20327 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20330 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20331 && TREE_CODE (decl
) == TYPE_DECL
)
20333 result
= TREE_TYPE (decl
);
20334 if (result
== error_mark_node
)
20335 result
= NULL_TREE
;
20337 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20338 && DECL_CLASS_TEMPLATE_P (decl
))
20342 /* Obtain the template and the arguments. */
20343 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20344 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20345 /* Instantiate the template. */
20346 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20347 /*entering_scope=*/0,
20348 tf_error
| tf_user
);
20349 if (result
== error_mark_node
)
20350 result
= NULL_TREE
;
20353 /* Leave the SCOPE. */
20355 pop_scope (pushed_scope
);
20357 /* If we failed to resolve it, return the original typename. */
20361 /* If lookup found a typename type, resolve that too. */
20362 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20364 /* Ill-formed programs can cause infinite recursion here, so we
20365 must catch that. */
20366 TYPENAME_IS_RESOLVING_P (type
) = 1;
20367 result
= resolve_typename_type (result
, only_current_p
);
20368 TYPENAME_IS_RESOLVING_P (type
) = 0;
20371 /* Qualify the resulting type. */
20372 quals
= cp_type_quals (type
);
20374 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20379 /* EXPR is an expression which is not type-dependent. Return a proxy
20380 for EXPR that can be used to compute the types of larger
20381 expressions containing EXPR. */
20384 build_non_dependent_expr (tree expr
)
20388 #ifdef ENABLE_CHECKING
20389 /* Try to get a constant value for all non-dependent expressions in
20390 order to expose bugs in *_dependent_expression_p and constexpr. */
20391 if (cxx_dialect
>= cxx0x
20392 && !instantiation_dependent_expression_p (expr
))
20393 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20396 /* Preserve OVERLOADs; the functions must be available to resolve
20399 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20400 inner_expr
= stmt_expr_value_expr (inner_expr
);
20401 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20402 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20403 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20404 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20405 if (is_overloaded_fn (inner_expr
)
20406 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20408 /* There is no need to return a proxy for a variable. */
20409 if (TREE_CODE (expr
) == VAR_DECL
)
20411 /* Preserve string constants; conversions from string constants to
20412 "char *" are allowed, even though normally a "const char *"
20413 cannot be used to initialize a "char *". */
20414 if (TREE_CODE (expr
) == STRING_CST
)
20416 /* Preserve arithmetic constants, as an optimization -- there is no
20417 reason to create a new node. */
20418 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20420 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20421 There is at least one place where we want to know that a
20422 particular expression is a throw-expression: when checking a ?:
20423 expression, there are special rules if the second or third
20424 argument is a throw-expression. */
20425 if (TREE_CODE (expr
) == THROW_EXPR
)
20428 /* Don't wrap an initializer list, we need to be able to look inside. */
20429 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20432 /* Don't wrap a dummy object, we need to be able to test for it. */
20433 if (is_dummy_object (expr
))
20436 if (TREE_CODE (expr
) == COND_EXPR
)
20437 return build3 (COND_EXPR
,
20439 TREE_OPERAND (expr
, 0),
20440 (TREE_OPERAND (expr
, 1)
20441 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20442 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20443 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20444 if (TREE_CODE (expr
) == COMPOUND_EXPR
20445 && !COMPOUND_EXPR_OVERLOADED (expr
))
20446 return build2 (COMPOUND_EXPR
,
20448 TREE_OPERAND (expr
, 0),
20449 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20451 /* If the type is unknown, it can't really be non-dependent */
20452 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20454 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20455 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20458 /* ARGS is a vector of expressions as arguments to a function call.
20459 Replace the arguments with equivalent non-dependent expressions.
20460 This modifies ARGS in place. */
20463 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
20468 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
20470 tree newarg
= build_non_dependent_expr (arg
);
20472 (*args
)[ix
] = newarg
;
20476 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20477 with a level one deeper than the actual template parms. */
20482 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20483 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20484 TYPE_DECL
, get_identifier ("auto"), au
);
20485 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20486 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20487 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20488 TYPE_NAME (au
), NULL_TREE
);
20489 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20490 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20491 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20496 /* Given type ARG, return std::initializer_list<ARG>. */
20501 tree std_init_list
= namespace_binding
20502 (get_identifier ("initializer_list"), std_node
);
20504 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20506 error ("deducing from brace-enclosed initializer list requires "
20507 "#include <initializer_list>");
20508 return error_mark_node
;
20510 argvec
= make_tree_vec (1);
20511 TREE_VEC_ELT (argvec
, 0) = arg
;
20512 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20513 NULL_TREE
, 0, tf_warning_or_error
);
20516 /* Replace auto in TYPE with std::initializer_list<auto>. */
20519 listify_autos (tree type
, tree auto_node
)
20521 tree init_auto
= listify (auto_node
);
20522 tree argvec
= make_tree_vec (1);
20523 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20524 if (processing_template_decl
)
20525 argvec
= add_to_template_args (current_template_args (), argvec
);
20526 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20529 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20530 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20533 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20535 tree parms
, tparms
, targs
;
20539 if (init
== error_mark_node
)
20540 return error_mark_node
;
20542 if (type_dependent_expression_p (init
))
20543 /* Defining a subset of type-dependent expressions that we can deduce
20544 from ahead of time isn't worth the trouble. */
20547 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20548 with either a new invented type template parameter U or, if the
20549 initializer is a braced-init-list (8.5.4), with
20550 std::initializer_list<U>. */
20551 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20552 type
= listify_autos (type
, auto_node
);
20554 init
= resolve_nondeduced_context (init
);
20556 parms
= build_tree_list (NULL_TREE
, type
);
20558 tparms
= make_tree_vec (1);
20559 targs
= make_tree_vec (1);
20560 TREE_VEC_ELT (tparms
, 0)
20561 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20562 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20563 DEDUCE_CALL
, LOOKUP_NORMAL
,
20564 /*explain_p=*/false);
20567 if (processing_template_decl
)
20568 /* Try again at instantiation time. */
20570 if (type
&& type
!= error_mark_node
)
20571 /* If type is error_mark_node a diagnostic must have been
20572 emitted by now. Also, having a mention to '<type error>'
20573 in the diagnostic is not really useful to the user. */
20575 if (cfun
&& auto_node
== current_function_auto_return_pattern
20576 && LAMBDA_FUNCTION_P (current_function_decl
))
20577 error ("unable to deduce lambda return type from %qE", init
);
20579 error ("unable to deduce %qT from %qE", type
, init
);
20581 return error_mark_node
;
20584 /* If the list of declarators contains more than one declarator, the type
20585 of each declared variable is determined as described above. If the
20586 type deduced for the template parameter U is not the same in each
20587 deduction, the program is ill-formed. */
20588 if (TREE_TYPE (auto_node
)
20589 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20591 if (cfun
&& auto_node
== current_function_auto_return_pattern
20592 && LAMBDA_FUNCTION_P (current_function_decl
))
20593 error ("inconsistent types %qT and %qT deduced for "
20594 "lambda return type", TREE_TYPE (auto_node
),
20595 TREE_VEC_ELT (targs
, 0));
20597 error ("inconsistent deduction for %qT: %qT and then %qT",
20598 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20599 return error_mark_node
;
20601 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20603 if (processing_template_decl
)
20604 targs
= add_to_template_args (current_template_args (), targs
);
20605 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20608 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20612 splice_late_return_type (tree type
, tree late_return_type
)
20616 if (late_return_type
== NULL_TREE
)
20618 argvec
= make_tree_vec (1);
20619 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20620 if (processing_template_parmlist
)
20621 /* For a late-specified return type in a template type-parameter, we
20622 need to add a dummy argument level for its parmlist. */
20623 argvec
= add_to_template_args
20624 (make_tree_vec (processing_template_parmlist
), argvec
);
20625 if (current_template_parms
)
20626 argvec
= add_to_template_args (current_template_args (), argvec
);
20627 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20630 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20633 is_auto (const_tree type
)
20635 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20636 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20642 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20643 appear as a type-specifier for the declaration in question, we don't
20644 have to look through the whole type. */
20647 type_uses_auto (tree type
)
20649 enum tree_code code
;
20650 if (is_auto (type
))
20653 code
= TREE_CODE (type
);
20655 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20656 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20657 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20658 return type_uses_auto (TREE_TYPE (type
));
20660 if (TYPE_PTRMEMFUNC_P (type
))
20661 return type_uses_auto (TREE_TYPE (TREE_TYPE
20662 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20667 /* For a given template T, return the vector of typedefs referenced
20668 in T for which access check is needed at T instantiation time.
20669 T is either a FUNCTION_DECL or a RECORD_TYPE.
20670 Those typedefs were added to T by the function
20671 append_type_to_template_for_access_check. */
20673 vec
<qualified_typedef_usage_t
, va_gc
> *
20674 get_types_needing_access_check (tree t
)
20677 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
20679 if (!t
|| t
== error_mark_node
)
20682 if (!(ti
= get_template_info (t
)))
20685 if (CLASS_TYPE_P (t
)
20686 || TREE_CODE (t
) == FUNCTION_DECL
)
20688 if (!TI_TEMPLATE (ti
))
20691 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20697 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20698 tied to T. That list of typedefs will be access checked at
20699 T instantiation time.
20700 T is either a FUNCTION_DECL or a RECORD_TYPE.
20701 TYPE_DECL is a TYPE_DECL node representing a typedef.
20702 SCOPE is the scope through which TYPE_DECL is accessed.
20703 LOCATION is the location of the usage point of TYPE_DECL.
20705 This function is a subroutine of
20706 append_type_to_template_for_access_check. */
20709 append_type_to_template_for_access_check_1 (tree t
,
20712 location_t location
)
20714 qualified_typedef_usage_t typedef_usage
;
20717 if (!t
|| t
== error_mark_node
)
20720 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20721 || CLASS_TYPE_P (t
))
20723 && TREE_CODE (type_decl
) == TYPE_DECL
20726 if (!(ti
= get_template_info (t
)))
20729 gcc_assert (TI_TEMPLATE (ti
));
20731 typedef_usage
.typedef_decl
= type_decl
;
20732 typedef_usage
.context
= scope
;
20733 typedef_usage
.locus
= location
;
20735 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
20738 /* Append TYPE_DECL to the template TEMPL.
20739 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20740 At TEMPL instanciation time, TYPE_DECL will be checked to see
20741 if it can be accessed through SCOPE.
20742 LOCATION is the location of the usage point of TYPE_DECL.
20744 e.g. consider the following code snippet:
20751 template<class U> struct S
20753 C::myint mi; // <-- usage point of the typedef C::myint
20758 At S<char> instantiation time, we need to check the access of C::myint
20759 In other words, we need to check the access of the myint typedef through
20760 the C scope. For that purpose, this function will add the myint typedef
20761 and the scope C through which its being accessed to a list of typedefs
20762 tied to the template S. That list will be walked at template instantiation
20763 time and access check performed on each typedefs it contains.
20764 Note that this particular code snippet should yield an error because
20765 myint is private to C. */
20768 append_type_to_template_for_access_check (tree templ
,
20771 location_t location
)
20773 qualified_typedef_usage_t
*iter
;
20776 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20778 /* Make sure we don't append the type to the template twice. */
20779 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
20780 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20783 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20787 /* Set up the hash tables for template instantiations. */
20790 init_template_processing (void)
20792 decl_specializations
= htab_create_ggc (37,
20793 hash_specialization
,
20794 eq_specializations
,
20796 type_specializations
= htab_create_ggc (37,
20797 hash_specialization
,
20798 eq_specializations
,
20802 /* Print stats about the template hash tables for -fstats. */
20805 print_template_statistics (void)
20807 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20808 "%f collisions\n", (long) htab_size (decl_specializations
),
20809 (long) htab_elements (decl_specializations
),
20810 htab_collisions (decl_specializations
));
20811 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20812 "%f collisions\n", (long) htab_size (type_specializations
),
20813 (long) htab_elements (type_specializations
),
20814 htab_collisions (type_specializations
));
20817 #include "gt-cp-pt.h"