1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
33 #include "pointer-set.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
43 #include "tree-iterator.h"
45 /* The type of functions taking a tree, and some additional data, and
47 typedef int (*tree_fn_t
) (tree
, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template
{
53 struct pending_template
*next
;
54 struct tinst_level
*tinst
;
57 static GTY(()) struct pending_template
*pending_templates
;
58 static GTY(()) struct pending_template
*last_pending_template
;
60 int processing_template_parmlist
;
61 static int template_header_count
;
63 static GTY(()) tree saved_trees
;
64 static vec
<int> inline_parm_levels
;
66 static GTY(()) struct tinst_level
*current_tinst_level
;
68 static GTY(()) tree saved_access_scope
;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr
;
75 /* A map from local variable declarations in the body of the template
76 presently being instantiated to the corresponding instantiated
78 static struct pointer_map_t
*local_specializations
;
80 /* True if we've recursed into fn_type_unification too many times. */
81 static bool excessive_deduction_depth
;
83 typedef struct GTY(()) spec_entry
90 static GTY ((param_is (spec_entry
)))
91 htab_t decl_specializations
;
93 static GTY ((param_is (spec_entry
)))
94 htab_t type_specializations
;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 enum template_base_result
{
113 tbr_ambiguous_baseclass
,
117 static void push_access_scope (tree
);
118 static void pop_access_scope (tree
);
119 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
120 unification_kind_t
, int,
122 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
123 unification_kind_t
, int, bool, bool);
124 static int unify (tree
, tree
, tree
, tree
, int, bool);
125 static void add_pending_template (tree
);
126 static tree
reopen_tinst_level (struct tinst_level
*);
127 static tree
tsubst_initializer_list (tree
, tree
);
128 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
129 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
131 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
133 static void tsubst_enum (tree
, tree
, tree
);
134 static tree
add_to_template_args (tree
, tree
);
135 static tree
add_outermost_template_args (tree
, tree
);
136 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
137 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
139 static int type_unification_real (tree
, tree
, tree
, const tree
*,
140 unsigned int, int, unification_kind_t
, int,
142 static void note_template_header (int);
143 static tree
convert_nontype_argument_function (tree
, tree
);
144 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
145 static tree
convert_template_argument (tree
, tree
, tree
,
146 tsubst_flags_t
, int, tree
);
147 static int for_each_template_parm (tree
, tree_fn_t
, void*,
148 struct pointer_set_t
*, bool);
149 static tree
expand_template_argument_pack (tree
);
150 static tree
build_template_parm_index (int, int, int, tree
, tree
);
151 static bool inline_needs_template_parms (tree
);
152 static void push_inline_template_parms_recursive (tree
, int);
153 static tree
retrieve_local_specialization (tree
);
154 static void register_local_specialization (tree
, tree
);
155 static hashval_t
hash_specialization (const void *p
);
156 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
157 static int mark_template_parm (tree
, void *);
158 static int template_parm_this_level_p (tree
, void *);
159 static tree
tsubst_friend_function (tree
, tree
);
160 static tree
tsubst_friend_class (tree
, tree
);
161 static int can_complete_type_without_circularity (tree
);
162 static tree
get_bindings (tree
, tree
, tree
, bool);
163 static int template_decl_level (tree
);
164 static int check_cv_quals_for_unify (int, tree
, tree
);
165 static void template_parm_level_and_index (tree
, int*, int*);
166 static int unify_pack_expansion (tree
, tree
, tree
,
167 tree
, unification_kind_t
, bool, bool);
168 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
169 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
171 static void regenerate_decl_from_template (tree
, tree
);
172 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
173 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
174 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
175 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
176 static bool check_specialization_scope (void);
177 static tree
process_partial_specialization (tree
);
178 static void set_current_access_from_decl (tree
);
179 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
181 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
182 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
184 static bool template_template_parm_bindings_ok_p (tree
, tree
);
185 static int template_args_equal (tree
, tree
);
186 static void tsubst_default_arguments (tree
);
187 static tree
for_each_template_parm_r (tree
*, int *, void *);
188 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
189 static void copy_default_args_to_explicit_spec (tree
);
190 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
191 static bool dependent_template_arg_p (tree
);
192 static bool any_template_arguments_need_structural_equality_p (tree
);
193 static bool dependent_type_p_r (tree
);
194 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
195 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
198 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
199 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
201 static tree
listify (tree
);
202 static tree
listify_autos (tree
, tree
);
203 static tree
template_parm_to_arg (tree t
);
204 static tree
current_template_args (void);
205 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
206 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
208 /* Make the current scope suitable for access checking when we are
209 processing T. T can be FUNCTION_DECL for instantiated function
210 template, VAR_DECL for static member variable, or TYPE_DECL for
211 alias template (needed by instantiate_decl). */
214 push_access_scope (tree t
)
216 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
217 || TREE_CODE (t
) == TYPE_DECL
218 || TREE_CODE (t
) == VAR_DECL
);
220 if (DECL_FRIEND_CONTEXT (t
))
221 push_nested_class (DECL_FRIEND_CONTEXT (t
));
222 else if (DECL_CLASS_SCOPE_P (t
))
223 push_nested_class (DECL_CONTEXT (t
));
225 push_to_top_level ();
227 if (TREE_CODE (t
) == FUNCTION_DECL
)
229 saved_access_scope
= tree_cons
230 (NULL_TREE
, current_function_decl
, saved_access_scope
);
231 current_function_decl
= t
;
235 /* Restore the scope set up by push_access_scope. T is the node we
239 pop_access_scope (tree t
)
241 if (TREE_CODE (t
) == FUNCTION_DECL
)
243 current_function_decl
= TREE_VALUE (saved_access_scope
);
244 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
247 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
250 pop_from_top_level ();
253 /* Do any processing required when DECL (a member template
254 declaration) is finished. Returns the TEMPLATE_DECL corresponding
255 to DECL, unless it is a specialization, in which case the DECL
256 itself is returned. */
259 finish_member_template_decl (tree decl
)
261 if (decl
== error_mark_node
)
262 return error_mark_node
;
264 gcc_assert (DECL_P (decl
));
266 if (TREE_CODE (decl
) == TYPE_DECL
)
270 type
= TREE_TYPE (decl
);
271 if (type
== error_mark_node
)
272 return error_mark_node
;
273 if (MAYBE_CLASS_TYPE_P (type
)
274 && CLASSTYPE_TEMPLATE_INFO (type
)
275 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
277 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
278 check_member_template (tmpl
);
283 else if (TREE_CODE (decl
) == FIELD_DECL
)
284 error ("data member %qD cannot be a member template", decl
);
285 else if (DECL_TEMPLATE_INFO (decl
))
287 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
289 check_member_template (DECL_TI_TEMPLATE (decl
));
290 return DECL_TI_TEMPLATE (decl
);
296 error ("invalid member template declaration %qD", decl
);
298 return error_mark_node
;
301 /* Create a template info node. */
304 build_template_info (tree template_decl
, tree template_args
)
306 tree result
= make_node (TEMPLATE_INFO
);
307 TI_TEMPLATE (result
) = template_decl
;
308 TI_ARGS (result
) = template_args
;
312 /* Return the template info node corresponding to T, whatever T is. */
315 get_template_info (const_tree t
)
317 tree tinfo
= NULL_TREE
;
319 if (!t
|| t
== error_mark_node
)
322 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
323 tinfo
= DECL_TEMPLATE_INFO (t
);
325 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
328 if (TAGGED_TYPE_P (t
))
329 tinfo
= TYPE_TEMPLATE_INFO (t
);
330 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
331 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
336 /* Returns the template nesting level of the indicated class TYPE.
346 A<T>::B<U> has depth two, while A<T> has depth one.
347 Both A<T>::B<int> and A<int>::B<U> have depth one, if
348 they are instantiations, not specializations.
350 This function is guaranteed to return 0 if passed NULL_TREE so
351 that, for example, `template_class_depth (current_class_type)' is
355 template_class_depth (tree type
)
360 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
361 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
362 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
364 tree tinfo
= get_template_info (type
);
366 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
367 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
374 /* Subroutine of maybe_begin_member_template_processing.
375 Returns true if processing DECL needs us to push template parms. */
378 inline_needs_template_parms (tree decl
)
380 if (! DECL_TEMPLATE_INFO (decl
))
383 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
384 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
387 /* Subroutine of maybe_begin_member_template_processing.
388 Push the template parms in PARMS, starting from LEVELS steps into the
389 chain, and ending at the beginning, since template parms are listed
393 push_inline_template_parms_recursive (tree parmlist
, int levels
)
395 tree parms
= TREE_VALUE (parmlist
);
399 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
401 ++processing_template_decl
;
402 current_template_parms
403 = tree_cons (size_int (processing_template_decl
),
404 parms
, current_template_parms
);
405 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
407 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
409 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
411 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
413 if (parm
== error_mark_node
)
416 gcc_assert (DECL_P (parm
));
418 switch (TREE_CODE (parm
))
427 /* Make a CONST_DECL as is done in process_template_parm.
428 It is ugly that we recreate this here; the original
429 version built in process_template_parm is no longer
431 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
432 CONST_DECL
, DECL_NAME (parm
),
434 DECL_ARTIFICIAL (decl
) = 1;
435 TREE_CONSTANT (decl
) = 1;
436 TREE_READONLY (decl
) = 1;
437 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
438 SET_DECL_TEMPLATE_PARM_P (decl
);
449 /* Restore the template parameter context for a member template or
450 a friend template defined in a class definition. */
453 maybe_begin_member_template_processing (tree decl
)
458 if (inline_needs_template_parms (decl
))
460 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
461 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
463 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
466 parms
= TREE_CHAIN (parms
);
469 push_inline_template_parms_recursive (parms
, levels
);
472 /* Remember how many levels of template parameters we pushed so that
473 we can pop them later. */
474 inline_parm_levels
.safe_push (levels
);
477 /* Undo the effects of maybe_begin_member_template_processing. */
480 maybe_end_member_template_processing (void)
485 if (inline_parm_levels
.length () == 0)
488 last
= inline_parm_levels
.pop ();
489 for (i
= 0; i
< last
; ++i
)
491 --processing_template_decl
;
492 current_template_parms
= TREE_CHAIN (current_template_parms
);
497 /* Return a new template argument vector which contains all of ARGS,
498 but has as its innermost set of arguments the EXTRA_ARGS. */
501 add_to_template_args (tree args
, tree extra_args
)
508 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
511 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
512 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
514 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
515 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
517 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
518 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
523 /* Like add_to_template_args, but only the outermost ARGS are added to
524 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
525 (EXTRA_ARGS) levels are added. This function is used to combine
526 the template arguments from a partial instantiation with the
527 template arguments used to attain the full instantiation from the
528 partial instantiation. */
531 add_outermost_template_args (tree args
, tree extra_args
)
535 /* If there are more levels of EXTRA_ARGS than there are ARGS,
536 something very fishy is going on. */
537 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
539 /* If *all* the new arguments will be the EXTRA_ARGS, just return
541 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
544 /* For the moment, we make ARGS look like it contains fewer levels. */
545 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
547 new_args
= add_to_template_args (args
, extra_args
);
549 /* Now, we restore ARGS to its full dimensions. */
550 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
555 /* Return the N levels of innermost template arguments from the ARGS. */
558 get_innermost_template_args (tree args
, int n
)
566 /* If N is 1, just return the innermost set of template arguments. */
568 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
570 /* If we're not removing anything, just return the arguments we were
572 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
573 gcc_assert (extra_levels
>= 0);
574 if (extra_levels
== 0)
577 /* Make a new set of arguments, not containing the outer arguments. */
578 new_args
= make_tree_vec (n
);
579 for (i
= 1; i
<= n
; ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
,
581 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
586 /* The inverse of get_innermost_template_args: Return all but the innermost
587 EXTRA_LEVELS levels of template arguments from the ARGS. */
590 strip_innermost_template_args (tree args
, int extra_levels
)
593 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
598 /* If N is 1, just return the outermost set of template arguments. */
600 return TMPL_ARGS_LEVEL (args
, 1);
602 /* If we're not removing anything, just return the arguments we were
604 gcc_assert (extra_levels
>= 0);
605 if (extra_levels
== 0)
608 /* Make a new set of arguments, not containing the inner arguments. */
609 new_args
= make_tree_vec (n
);
610 for (i
= 1; i
<= n
; ++i
)
611 SET_TMPL_ARGS_LEVEL (new_args
, i
,
612 TMPL_ARGS_LEVEL (args
, i
));
617 /* We've got a template header coming up; push to a new level for storing
621 begin_template_parm_list (void)
623 /* We use a non-tag-transparent scope here, which causes pushtag to
624 put tags in this scope, rather than in the enclosing class or
625 namespace scope. This is the right thing, since we want
626 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
627 global template class, push_template_decl handles putting the
628 TEMPLATE_DECL into top-level scope. For a nested template class,
631 template <class T> struct S1 {
632 template <class T> struct S2 {};
635 pushtag contains special code to call pushdecl_with_scope on the
636 TEMPLATE_DECL for S2. */
637 begin_scope (sk_template_parms
, NULL
);
638 ++processing_template_decl
;
639 ++processing_template_parmlist
;
640 note_template_header (0);
643 /* This routine is called when a specialization is declared. If it is
644 invalid to declare a specialization here, an error is reported and
645 false is returned, otherwise this routine will return true. */
648 check_specialization_scope (void)
650 tree scope
= current_scope ();
654 An explicit specialization shall be declared in the namespace of
655 which the template is a member, or, for member templates, in the
656 namespace of which the enclosing class or enclosing class
657 template is a member. An explicit specialization of a member
658 function, member class or static data member of a class template
659 shall be declared in the namespace of which the class template
661 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
663 error ("explicit specialization in non-namespace scope %qD", scope
);
669 In an explicit specialization declaration for a member of a class
670 template or a member template that appears in namespace scope,
671 the member template and some of its enclosing class templates may
672 remain unspecialized, except that the declaration shall not
673 explicitly specialize a class member template if its enclosing
674 class templates are not explicitly specialized as well. */
675 if (current_template_parms
)
677 error ("enclosing class templates are not explicitly specialized");
684 /* We've just seen template <>. */
687 begin_specialization (void)
689 begin_scope (sk_template_spec
, NULL
);
690 note_template_header (1);
691 return check_specialization_scope ();
694 /* Called at then end of processing a declaration preceded by
698 end_specialization (void)
701 reset_specialization ();
704 /* Any template <>'s that we have seen thus far are not referring to a
705 function specialization. */
708 reset_specialization (void)
710 processing_specialization
= 0;
711 template_header_count
= 0;
714 /* We've just seen a template header. If SPECIALIZATION is nonzero,
715 it was of the form template <>. */
718 note_template_header (int specialization
)
720 processing_specialization
= specialization
;
721 template_header_count
++;
724 /* We're beginning an explicit instantiation. */
727 begin_explicit_instantiation (void)
729 gcc_assert (!processing_explicit_instantiation
);
730 processing_explicit_instantiation
= true;
735 end_explicit_instantiation (void)
737 gcc_assert (processing_explicit_instantiation
);
738 processing_explicit_instantiation
= false;
741 /* An explicit specialization or partial specialization of TMPL is being
742 declared. Check that the namespace in which the specialization is
743 occurring is permissible. Returns false iff it is invalid to
744 specialize TMPL in the current namespace. */
747 check_specialization_namespace (tree tmpl
)
749 tree tpl_ns
= decl_namespace_context (tmpl
);
753 An explicit specialization shall be declared in the namespace of
754 which the template is a member, or, for member templates, in the
755 namespace of which the enclosing class or enclosing class
756 template is a member. An explicit specialization of a member
757 function, member class or static data member of a class template
758 shall be declared in the namespace of which the class template is
760 if (current_scope() != DECL_CONTEXT (tmpl
)
761 && !at_namespace_scope_p ())
763 error ("specialization of %qD must appear at namespace scope", tmpl
);
766 if (is_associated_namespace (current_namespace
, tpl_ns
))
767 /* Same or super-using namespace. */
771 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
772 permerror (input_location
, " from definition of %q+#D", tmpl
);
777 /* SPEC is an explicit instantiation. Check that it is valid to
778 perform this explicit instantiation in the current namespace. */
781 check_explicit_instantiation_namespace (tree spec
)
785 /* DR 275: An explicit instantiation shall appear in an enclosing
786 namespace of its template. */
787 ns
= decl_namespace_context (spec
);
788 if (!is_ancestor (current_namespace
, ns
))
789 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
790 "(which does not enclose namespace %qD)",
791 spec
, current_namespace
, ns
);
794 /* The TYPE is being declared. If it is a template type, that means it
795 is a partial specialization. Do appropriate error-checking. */
798 maybe_process_partial_specialization (tree type
)
802 if (type
== error_mark_node
)
803 return error_mark_node
;
805 /* A lambda that appears in specialization context is not itself a
807 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
810 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
812 error ("name of class shadows template template parameter %qD",
814 return error_mark_node
;
817 context
= TYPE_CONTEXT (type
);
819 if (TYPE_ALIAS_P (type
))
821 if (TYPE_TEMPLATE_INFO (type
)
822 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
823 error ("specialization of alias template %qD",
824 TYPE_TI_TEMPLATE (type
));
826 error ("explicit specialization of non-template %qT", type
);
827 return error_mark_node
;
829 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
831 /* This is for ordinary explicit specialization and partial
832 specialization of a template class such as:
834 template <> class C<int>;
838 template <class T> class C<T*>;
840 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
843 && !COMPLETE_TYPE_P (type
))
845 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
846 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
847 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
848 if (processing_template_decl
)
850 if (push_template_decl (TYPE_MAIN_DECL (type
))
852 return error_mark_node
;
855 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
856 error ("specialization of %qT after instantiation", type
);
857 else if (errorcount
&& !processing_specialization
858 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
859 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
860 /* Trying to define a specialization either without a template<> header
861 or in an inappropriate place. We've already given an error, so just
862 bail now so we don't actually define the specialization. */
863 return error_mark_node
;
865 else if (CLASS_TYPE_P (type
)
866 && !CLASSTYPE_USE_TEMPLATE (type
)
867 && CLASSTYPE_TEMPLATE_INFO (type
)
868 && context
&& CLASS_TYPE_P (context
)
869 && CLASSTYPE_TEMPLATE_INFO (context
))
871 /* This is for an explicit specialization of member class
872 template according to [temp.expl.spec/18]:
874 template <> template <class U> class C<int>::D;
876 The context `C<int>' must be an implicit instantiation.
877 Otherwise this is just a member class template declared
880 template <> class C<int> { template <class U> class D; };
881 template <> template <class U> class C<int>::D;
883 In the first case, `C<int>::D' is a specialization of `C<T>::D'
884 while in the second case, `C<int>::D' is a primary template
885 and `C<T>::D' may not exist. */
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
888 && !COMPLETE_TYPE_P (type
))
891 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
893 if (current_namespace
894 != decl_namespace_context (tmpl
))
896 permerror (input_location
, "specializing %q#T in different namespace", type
);
897 permerror (input_location
, " from definition of %q+#D", tmpl
);
900 /* Check for invalid specialization after instantiation:
902 template <> template <> class C<int>::D<int>;
903 template <> template <class U> class C<int>::D; */
905 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
906 t
; t
= TREE_CHAIN (t
))
908 tree inst
= TREE_VALUE (t
);
909 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
911 /* We already have a full specialization of this partial
912 instantiation. Reassign it to the new member
913 specialization template. */
918 elt
.tmpl
= most_general_template (tmpl
);
919 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
922 htab_remove_elt (type_specializations
, &elt
);
925 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
927 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
928 entry
= ggc_alloc_spec_entry ();
932 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
933 /* But if we've had an implicit instantiation, that's a
934 problem ([temp.expl.spec]/6). */
935 error ("specialization %qT after instantiation %qT",
939 /* Mark TYPE as a specialization. And as a result, we only
940 have one level of template argument for the innermost
942 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
943 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
944 CLASSTYPE_TI_ARGS (type
)
945 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
948 else if (processing_specialization
)
950 /* Someday C++0x may allow for enum template specialization. */
951 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
952 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
953 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
954 "of %qD not allowed by ISO C++", type
);
957 error ("explicit specialization of non-template %qT", type
);
958 return error_mark_node
;
965 /* Returns nonzero if we can optimize the retrieval of specializations
966 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
967 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
970 optimize_specialization_lookup_p (tree tmpl
)
972 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
973 && DECL_CLASS_SCOPE_P (tmpl
)
974 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
976 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
977 /* The optimized lookup depends on the fact that the
978 template arguments for the member function template apply
979 purely to the containing class, which is not true if the
980 containing class is an explicit or partial
982 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
983 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
984 && !DECL_CONV_FN_P (tmpl
)
985 /* It is possible to have a template that is not a member
986 template and is not a member of a template class:
988 template <typename T>
989 struct S { friend A::f(); };
991 Here, the friend function is a template, but the context does
992 not have template information. The optimized lookup relies
993 on having ARGS be the template arguments for both the class
994 and the function template. */
995 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
998 /* Retrieve the specialization (in the sense of [temp.spec] - a
999 specialization is either an instantiation or an explicit
1000 specialization) of TMPL for the given template ARGS. If there is
1001 no such specialization, return NULL_TREE. The ARGS are a vector of
1002 arguments, or a vector of vectors of arguments, in the case of
1003 templates with more than one level of parameters.
1005 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1006 then we search for a partial specialization matching ARGS. This
1007 parameter is ignored if TMPL is not a class template. */
1010 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1012 if (args
== error_mark_node
)
1015 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1017 /* There should be as many levels of arguments as there are
1018 levels of parameters. */
1019 gcc_assert (TMPL_ARGS_DEPTH (args
)
1020 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1022 if (optimize_specialization_lookup_p (tmpl
))
1024 tree class_template
;
1025 tree class_specialization
;
1026 vec
<tree
, va_gc
> *methods
;
1030 /* The template arguments actually apply to the containing
1031 class. Find the class specialization with those
1033 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1034 class_specialization
1035 = retrieve_specialization (class_template
, args
, 0);
1036 if (!class_specialization
)
1038 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1039 for the specialization. */
1040 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1043 /* Iterate through the methods with the indicated name, looking
1044 for the one that has an instance of TMPL. */
1045 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1046 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1048 tree fn
= OVL_CURRENT (fns
);
1049 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1050 /* using-declarations can add base methods to the method vec,
1051 and we don't want those here. */
1052 && DECL_CONTEXT (fn
) == class_specialization
)
1061 htab_t specializations
;
1065 elt
.spec
= NULL_TREE
;
1067 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1068 specializations
= type_specializations
;
1070 specializations
= decl_specializations
;
1073 hash
= hash_specialization (&elt
);
1074 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1082 /* Like retrieve_specialization, but for local declarations. */
1085 retrieve_local_specialization (tree tmpl
)
1089 if (local_specializations
== NULL
)
1092 slot
= pointer_map_contains (local_specializations
, tmpl
);
1093 return slot
? (tree
) *slot
: NULL_TREE
;
1096 /* Returns nonzero iff DECL is a specialization of TMPL. */
1099 is_specialization_of (tree decl
, tree tmpl
)
1103 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1107 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1113 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1115 for (t
= TREE_TYPE (decl
);
1117 t
= CLASSTYPE_USE_TEMPLATE (t
)
1118 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1119 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1126 /* Returns nonzero iff DECL is a specialization of friend declaration
1127 FRIEND_DECL according to [temp.friend]. */
1130 is_specialization_of_friend (tree decl
, tree friend_decl
)
1132 bool need_template
= true;
1135 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1136 || TREE_CODE (decl
) == TYPE_DECL
);
1138 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1139 of a template class, we want to check if DECL is a specialization
1141 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1142 && DECL_TEMPLATE_INFO (friend_decl
)
1143 && !DECL_USE_TEMPLATE (friend_decl
))
1145 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1146 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1147 need_template
= false;
1149 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1150 && !PRIMARY_TEMPLATE_P (friend_decl
))
1151 need_template
= false;
1153 /* There is nothing to do if this is not a template friend. */
1154 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1157 if (is_specialization_of (decl
, friend_decl
))
1161 A member of a class template may be declared to be a friend of a
1162 non-template class. In this case, the corresponding member of
1163 every specialization of the class template is a friend of the
1164 class granting friendship.
1166 For example, given a template friend declaration
1168 template <class T> friend void A<T>::f();
1170 the member function below is considered a friend
1172 template <> struct A<int> {
1176 For this type of template friend, TEMPLATE_DEPTH below will be
1177 nonzero. To determine if DECL is a friend of FRIEND, we first
1178 check if the enclosing class is a specialization of another. */
1180 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1182 && DECL_CLASS_SCOPE_P (decl
)
1183 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1184 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1186 /* Next, we check the members themselves. In order to handle
1187 a few tricky cases, such as when FRIEND_DECL's are
1189 template <class T> friend void A<T>::g(T t);
1190 template <class T> template <T t> friend void A<T>::h();
1194 void A<int>::g(int);
1195 template <int> void A<int>::h();
1197 we need to figure out ARGS, the template arguments from
1198 the context of DECL. This is required for template substitution
1199 of `T' in the function parameter of `g' and template parameter
1200 of `h' in the above examples. Here ARGS corresponds to `int'. */
1202 tree context
= DECL_CONTEXT (decl
);
1203 tree args
= NULL_TREE
;
1204 int current_depth
= 0;
1206 while (current_depth
< template_depth
)
1208 if (CLASSTYPE_TEMPLATE_INFO (context
))
1210 if (current_depth
== 0)
1211 args
= TYPE_TI_ARGS (context
);
1213 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1216 context
= TYPE_CONTEXT (context
);
1219 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1224 tree friend_args_type
;
1225 tree decl_args_type
;
1227 /* Make sure that both DECL and FRIEND_DECL are templates or
1229 is_template
= DECL_TEMPLATE_INFO (decl
)
1230 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1231 if (need_template
^ is_template
)
1233 else if (is_template
)
1235 /* If both are templates, check template parameter list. */
1237 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1239 if (!comp_template_parms
1240 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1244 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1247 decl_type
= TREE_TYPE (decl
);
1249 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1250 tf_none
, NULL_TREE
);
1251 if (friend_type
== error_mark_node
)
1254 /* Check if return types match. */
1255 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1258 /* Check if function parameter types match, ignoring the
1259 `this' parameter. */
1260 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1261 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1263 friend_args_type
= TREE_CHAIN (friend_args_type
);
1264 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1265 decl_args_type
= TREE_CHAIN (decl_args_type
);
1267 return compparms (decl_args_type
, friend_args_type
);
1271 /* DECL is a TYPE_DECL */
1273 tree decl_type
= TREE_TYPE (decl
);
1275 /* Make sure that both DECL and FRIEND_DECL are templates or
1278 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1279 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1281 if (need_template
^ is_template
)
1283 else if (is_template
)
1286 /* If both are templates, check the name of the two
1287 TEMPLATE_DECL's first because is_friend didn't. */
1288 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1289 != DECL_NAME (friend_decl
))
1292 /* Now check template parameter list. */
1294 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1296 return comp_template_parms
1297 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1301 return (DECL_NAME (decl
)
1302 == DECL_NAME (friend_decl
));
1308 /* Register the specialization SPEC as a specialization of TMPL with
1309 the indicated ARGS. IS_FRIEND indicates whether the specialization
1310 is actually just a friend declaration. Returns SPEC, or an
1311 equivalent prior declaration, if available. */
1314 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1321 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1323 if (TREE_CODE (spec
) == FUNCTION_DECL
1324 && uses_template_parms (DECL_TI_ARGS (spec
)))
1325 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1326 register it; we want the corresponding TEMPLATE_DECL instead.
1327 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1328 the more obvious `uses_template_parms (spec)' to avoid problems
1329 with default function arguments. In particular, given
1330 something like this:
1332 template <class T> void f(T t1, T t = T())
1334 the default argument expression is not substituted for in an
1335 instantiation unless and until it is actually needed. */
1338 if (optimize_specialization_lookup_p (tmpl
))
1339 /* We don't put these specializations in the hash table, but we might
1340 want to give an error about a mismatch. */
1341 fn
= retrieve_specialization (tmpl
, args
, 0);
1349 hash
= hash_specialization (&elt
);
1352 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1354 fn
= ((spec_entry
*) *slot
)->spec
;
1359 /* We can sometimes try to re-register a specialization that we've
1360 already got. In particular, regenerate_decl_from_template calls
1361 duplicate_decls which will update the specialization list. But,
1362 we'll still get called again here anyhow. It's more convenient
1363 to simply allow this than to try to prevent it. */
1366 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1368 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1370 if (DECL_ODR_USED (fn
)
1371 || DECL_EXPLICIT_INSTANTIATION (fn
))
1373 error ("specialization of %qD after instantiation",
1375 return error_mark_node
;
1380 /* This situation should occur only if the first
1381 specialization is an implicit instantiation, the
1382 second is an explicit specialization, and the
1383 implicit instantiation has not yet been used. That
1384 situation can occur if we have implicitly
1385 instantiated a member function and then specialized
1388 We can also wind up here if a friend declaration that
1389 looked like an instantiation turns out to be a
1392 template <class T> void foo(T);
1393 class S { friend void foo<>(int) };
1394 template <> void foo(int);
1396 We transform the existing DECL in place so that any
1397 pointers to it become pointers to the updated
1400 If there was a definition for the template, but not
1401 for the specialization, we want this to look as if
1402 there were no definition, and vice versa. */
1403 DECL_INITIAL (fn
) = NULL_TREE
;
1404 duplicate_decls (spec
, fn
, is_friend
);
1405 /* The call to duplicate_decls will have applied
1408 An explicit specialization of a function template
1409 is inline only if it is explicitly declared to be,
1410 and independently of whether its function template
1413 to the primary function; now copy the inline bits to
1414 the various clones. */
1415 FOR_EACH_CLONE (clone
, fn
)
1417 DECL_DECLARED_INLINE_P (clone
)
1418 = DECL_DECLARED_INLINE_P (fn
);
1419 DECL_SOURCE_LOCATION (clone
)
1420 = DECL_SOURCE_LOCATION (fn
);
1422 check_specialization_namespace (tmpl
);
1427 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1429 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1430 /* Dup decl failed, but this is a new definition. Set the
1431 line number so any errors match this new
1433 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1439 return duplicate_decls (spec
, fn
, is_friend
);
1441 /* A specialization must be declared in the same namespace as the
1442 template it is specializing. */
1443 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1444 && !check_specialization_namespace (tmpl
))
1445 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1447 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1449 spec_entry
*entry
= ggc_alloc_spec_entry ();
1450 gcc_assert (tmpl
&& args
&& spec
);
1453 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1454 && PRIMARY_TEMPLATE_P (tmpl
)
1455 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1456 /* TMPL is a forward declaration of a template function; keep a list
1457 of all specializations in case we need to reassign them to a friend
1458 template later in tsubst_friend_function. */
1459 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1460 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1466 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1467 TMPL and ARGS members, ignores SPEC. */
1469 int comparing_specializations
;
1472 eq_specializations (const void *p1
, const void *p2
)
1474 const spec_entry
*e1
= (const spec_entry
*)p1
;
1475 const spec_entry
*e2
= (const spec_entry
*)p2
;
1478 ++comparing_specializations
;
1479 equal
= (e1
->tmpl
== e2
->tmpl
1480 && comp_template_args (e1
->args
, e2
->args
));
1481 --comparing_specializations
;
1486 /* Returns a hash for a template TMPL and template arguments ARGS. */
1489 hash_tmpl_and_args (tree tmpl
, tree args
)
1491 hashval_t val
= DECL_UID (tmpl
);
1492 return iterative_hash_template_arg (args
, val
);
1495 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1499 hash_specialization (const void *p
)
1501 const spec_entry
*e
= (const spec_entry
*)p
;
1502 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1505 /* Recursively calculate a hash value for a template argument ARG, for use
1506 in the hash tables of template specializations. */
1509 iterative_hash_template_arg (tree arg
, hashval_t val
)
1511 unsigned HOST_WIDE_INT i
;
1512 enum tree_code code
;
1515 if (arg
== NULL_TREE
)
1516 return iterative_hash_object (arg
, val
);
1521 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1522 /* We can get one of these when re-hashing a previous entry in the middle
1523 of substituting into a pack expansion. Just look through it. */
1524 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1526 code
= TREE_CODE (arg
);
1527 tclass
= TREE_CODE_CLASS (code
);
1529 val
= iterative_hash_object (code
, val
);
1536 case IDENTIFIER_NODE
:
1537 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1541 int i
, len
= TREE_VEC_LENGTH (arg
);
1542 for (i
= 0; i
< len
; ++i
)
1543 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1547 case TYPE_PACK_EXPANSION
:
1548 case EXPR_PACK_EXPANSION
:
1549 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1550 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1552 case TYPE_ARGUMENT_PACK
:
1553 case NONTYPE_ARGUMENT_PACK
:
1554 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1557 for (; arg
; arg
= TREE_CHAIN (arg
))
1558 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1562 for (; arg
; arg
= OVL_NEXT (arg
))
1563 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1569 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1571 val
= iterative_hash_template_arg (field
, val
);
1572 val
= iterative_hash_template_arg (value
, val
);
1578 if (!DECL_ARTIFICIAL (arg
))
1580 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1581 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1583 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1586 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1589 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1590 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1592 case TEMPLATE_PARM_INDEX
:
1593 val
= iterative_hash_template_arg
1594 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1595 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1596 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1599 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1600 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1601 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1604 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1606 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1610 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1611 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1612 val
= iterative_hash_object (code
, val
);
1613 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1616 /* A lambda can't appear in a template arg, but don't crash on
1618 gcc_assert (seen_error ());
1622 case IMPLICIT_CONV_EXPR
:
1623 case STATIC_CAST_EXPR
:
1624 case REINTERPRET_CAST_EXPR
:
1625 case CONST_CAST_EXPR
:
1626 case DYNAMIC_CAST_EXPR
:
1628 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1629 /* Now hash operands as usual. */
1639 if (TYPE_CANONICAL (arg
))
1640 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1642 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1643 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1644 /* Otherwise just compare the types during lookup. */
1647 case tcc_declaration
:
1649 return iterative_hash_expr (arg
, val
);
1652 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1654 unsigned n
= cp_tree_operand_length (arg
);
1655 for (i
= 0; i
< n
; ++i
)
1656 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1664 /* Unregister the specialization SPEC as a specialization of TMPL.
1665 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1666 if the SPEC was listed as a specialization of TMPL.
1668 Note that SPEC has been ggc_freed, so we can't look inside it. */
1671 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1676 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1677 elt
.args
= TI_ARGS (tinfo
);
1678 elt
.spec
= NULL_TREE
;
1680 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1683 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1684 gcc_assert (new_spec
!= NULL_TREE
);
1685 entry
->spec
= new_spec
;
1692 /* Like register_specialization, but for local declarations. We are
1693 registering SPEC, an instantiation of TMPL. */
1696 register_local_specialization (tree spec
, tree tmpl
)
1700 slot
= pointer_map_insert (local_specializations
, tmpl
);
1704 /* TYPE is a class type. Returns true if TYPE is an explicitly
1705 specialized class. */
1708 explicit_class_specialization_p (tree type
)
1710 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1712 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1715 /* Print the list of functions at FNS, going through all the overloads
1716 for each element of the list. Alternatively, FNS can not be a
1717 TREE_LIST, in which case it will be printed together with all the
1720 MORE and *STR should respectively be FALSE and NULL when the function
1721 is called from the outside. They are used internally on recursive
1722 calls. print_candidates manages the two parameters and leaves NULL
1723 in *STR when it ends. */
1726 print_candidates_1 (tree fns
, bool more
, const char **str
)
1729 char *spaces
= NULL
;
1731 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1732 if (TREE_CODE (fn
) == TREE_LIST
)
1734 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1735 print_candidates_1 (TREE_VALUE (fn2
),
1736 TREE_CHAIN (fn2
) || more
, str
);
1740 tree cand
= OVL_CURRENT (fn
);
1743 /* Pick the prefix string. */
1744 if (!more
&& !OVL_NEXT (fns
))
1746 inform (DECL_SOURCE_LOCATION (cand
),
1747 "candidate is: %#D", cand
);
1751 *str
= _("candidates are:");
1752 spaces
= get_spaces (*str
);
1754 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1755 *str
= spaces
? spaces
: *str
;
1765 /* Print the list of candidate FNS in an error message. FNS can also
1766 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1769 print_candidates (tree fns
)
1771 const char *str
= NULL
;
1772 print_candidates_1 (fns
, false, &str
);
1773 gcc_assert (str
== NULL
);
1776 /* Returns the template (one of the functions given by TEMPLATE_ID)
1777 which can be specialized to match the indicated DECL with the
1778 explicit template args given in TEMPLATE_ID. The DECL may be
1779 NULL_TREE if none is available. In that case, the functions in
1780 TEMPLATE_ID are non-members.
1782 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1783 specialization of a member template.
1785 The TEMPLATE_COUNT is the number of references to qualifying
1786 template classes that appeared in the name of the function. See
1787 check_explicit_specialization for a more accurate description.
1789 TSK indicates what kind of template declaration (if any) is being
1790 declared. TSK_TEMPLATE indicates that the declaration given by
1791 DECL, though a FUNCTION_DECL, has template parameters, and is
1792 therefore a template function.
1794 The template args (those explicitly specified and those deduced)
1795 are output in a newly created vector *TARGS_OUT.
1797 If it is impossible to determine the result, an error message is
1798 issued. The error_mark_node is returned to indicate failure. */
1801 determine_specialization (tree template_id
,
1804 int need_member_template
,
1810 tree explicit_targs
;
1811 tree candidates
= NULL_TREE
;
1812 /* A TREE_LIST of templates of which DECL may be a specialization.
1813 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1814 corresponding TREE_PURPOSE is the set of template arguments that,
1815 when used to instantiate the template, would produce a function
1816 with the signature of DECL. */
1817 tree templates
= NULL_TREE
;
1819 cp_binding_level
*b
;
1821 *targs_out
= NULL_TREE
;
1823 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1824 return error_mark_node
;
1826 /* We shouldn't be specializing a member template of an
1827 unspecialized class template; we already gave an error in
1828 check_specialization_scope, now avoid crashing. */
1829 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1830 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1832 gcc_assert (errorcount
);
1833 return error_mark_node
;
1836 fns
= TREE_OPERAND (template_id
, 0);
1837 explicit_targs
= TREE_OPERAND (template_id
, 1);
1839 if (fns
== error_mark_node
)
1840 return error_mark_node
;
1842 /* Check for baselinks. */
1843 if (BASELINK_P (fns
))
1844 fns
= BASELINK_FUNCTIONS (fns
);
1846 if (!is_overloaded_fn (fns
))
1848 error ("%qD is not a function template", fns
);
1849 return error_mark_node
;
1852 /* Count the number of template headers specified for this
1855 for (b
= current_binding_level
;
1856 b
->kind
== sk_template_parms
;
1860 for (; fns
; fns
= OVL_NEXT (fns
))
1862 tree fn
= OVL_CURRENT (fns
);
1864 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1866 tree decl_arg_types
;
1870 /* In case of explicit specialization, we need to check if
1871 the number of template headers appearing in the specialization
1872 is correct. This is usually done in check_explicit_specialization,
1873 but the check done there cannot be exhaustive when specializing
1874 member functions. Consider the following code:
1876 template <> void A<int>::f(int);
1877 template <> template <> void A<int>::f(int);
1879 Assuming that A<int> is not itself an explicit specialization
1880 already, the first line specializes "f" which is a non-template
1881 member function, whilst the second line specializes "f" which
1882 is a template member function. So both lines are syntactically
1883 correct, and check_explicit_specialization does not reject
1886 Here, we can do better, as we are matching the specialization
1887 against the declarations. We count the number of template
1888 headers, and we check if they match TEMPLATE_COUNT + 1
1889 (TEMPLATE_COUNT is the number of qualifying template classes,
1890 plus there must be another header for the member template
1893 Notice that if header_count is zero, this is not a
1894 specialization but rather a template instantiation, so there
1895 is no check we can perform here. */
1896 if (header_count
&& header_count
!= template_count
+ 1)
1899 /* Check that the number of template arguments at the
1900 innermost level for DECL is the same as for FN. */
1901 if (current_binding_level
->kind
== sk_template_parms
1902 && !current_binding_level
->explicit_spec_p
1903 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1904 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1905 (current_template_parms
))))
1908 /* DECL might be a specialization of FN. */
1909 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1910 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1912 /* For a non-static member function, we need to make sure
1913 that the const qualification is the same. Since
1914 get_bindings does not try to merge the "this" parameter,
1915 we must do the comparison explicitly. */
1916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1917 && !same_type_p (TREE_VALUE (fn_arg_types
),
1918 TREE_VALUE (decl_arg_types
)))
1921 /* Skip the "this" parameter and, for constructors of
1922 classes with virtual bases, the VTT parameter. A
1923 full specialization of a constructor will have a VTT
1924 parameter, but a template never will. */
1926 = skip_artificial_parms_for (decl
, decl_arg_types
);
1928 = skip_artificial_parms_for (fn
, fn_arg_types
);
1930 /* Function templates cannot be specializations; there are
1931 no partial specializations of functions. Therefore, if
1932 the type of DECL does not match FN, there is no
1934 if (tsk
== tsk_template
)
1936 if (compparms (fn_arg_types
, decl_arg_types
))
1937 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1941 /* See whether this function might be a specialization of this
1942 template. Suppress access control because we might be trying
1943 to make this specialization a friend, and we have already done
1944 access control for the declaration of the specialization. */
1945 push_deferring_access_checks (dk_no_check
);
1946 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1947 pop_deferring_access_checks ();
1950 /* We cannot deduce template arguments that when used to
1951 specialize TMPL will produce DECL. */
1954 /* Make sure that the deduced arguments actually work. */
1955 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1956 if (insttype
== error_mark_node
)
1959 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1960 if (!compparms (fn_arg_types
, decl_arg_types
))
1963 /* Save this template, and the arguments deduced. */
1964 templates
= tree_cons (targs
, fn
, templates
);
1966 else if (need_member_template
)
1967 /* FN is an ordinary member function, and we need a
1968 specialization of a member template. */
1970 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1971 /* We can get IDENTIFIER_NODEs here in certain erroneous
1974 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1975 /* This is just an ordinary non-member function. Nothing can
1976 be a specialization of that. */
1978 else if (DECL_ARTIFICIAL (fn
))
1979 /* Cannot specialize functions that are created implicitly. */
1983 tree decl_arg_types
;
1985 /* This is an ordinary member function. However, since
1986 we're here, we can assume it's enclosing class is a
1987 template class. For example,
1989 template <typename T> struct S { void f(); };
1990 template <> void S<int>::f() {}
1992 Here, S<int>::f is a non-template, but S<int> is a
1993 template class. If FN has the same type as DECL, we
1994 might be in business. */
1996 if (!DECL_TEMPLATE_INFO (fn
))
1997 /* Its enclosing class is an explicit specialization
1998 of a template class. This is not a candidate. */
2001 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2002 TREE_TYPE (TREE_TYPE (fn
))))
2003 /* The return types differ. */
2006 /* Adjust the type of DECL in case FN is a static member. */
2007 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2008 if (DECL_STATIC_FUNCTION_P (fn
)
2009 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2010 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2012 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2015 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2019 if (templates
&& TREE_CHAIN (templates
))
2025 It is possible for a specialization with a given function
2026 signature to be instantiated from more than one function
2027 template. In such cases, explicit specification of the
2028 template arguments must be used to uniquely identify the
2029 function template specialization being specialized.
2031 Note that here, there's no suggestion that we're supposed to
2032 determine which of the candidate templates is most
2033 specialized. However, we, also have:
2037 Partial ordering of overloaded function template
2038 declarations is used in the following contexts to select
2039 the function template to which a function template
2040 specialization refers:
2042 -- when an explicit specialization refers to a function
2045 So, we do use the partial ordering rules, at least for now.
2046 This extension can only serve to make invalid programs valid,
2047 so it's safe. And, there is strong anecdotal evidence that
2048 the committee intended the partial ordering rules to apply;
2049 the EDG front end has that behavior, and John Spicer claims
2050 that the committee simply forgot to delete the wording in
2051 [temp.expl.spec]. */
2052 tree tmpl
= most_specialized_instantiation (templates
);
2053 if (tmpl
!= error_mark_node
)
2056 TREE_CHAIN (templates
) = NULL_TREE
;
2060 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2062 error ("template-id %qD for %q+D does not match any template "
2063 "declaration", template_id
, decl
);
2064 if (header_count
&& header_count
!= template_count
+ 1)
2065 inform (input_location
, "saw %d %<template<>%>, need %d for "
2066 "specializing a member function template",
2067 header_count
, template_count
+ 1);
2068 return error_mark_node
;
2070 else if ((templates
&& TREE_CHAIN (templates
))
2071 || (candidates
&& TREE_CHAIN (candidates
))
2072 || (templates
&& candidates
))
2074 error ("ambiguous template specialization %qD for %q+D",
2076 candidates
= chainon (candidates
, templates
);
2077 print_candidates (candidates
);
2078 return error_mark_node
;
2081 /* We have one, and exactly one, match. */
2084 tree fn
= TREE_VALUE (candidates
);
2085 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2086 /* DECL is a re-declaration or partial instantiation of a template
2088 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2090 /* It was a specialization of an ordinary member function in a
2092 return DECL_TI_TEMPLATE (fn
);
2095 /* It was a specialization of a template. */
2096 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2097 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2099 *targs_out
= copy_node (targs
);
2100 SET_TMPL_ARGS_LEVEL (*targs_out
,
2101 TMPL_ARGS_DEPTH (*targs_out
),
2102 TREE_PURPOSE (templates
));
2105 *targs_out
= TREE_PURPOSE (templates
);
2106 return TREE_VALUE (templates
);
2109 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2110 but with the default argument values filled in from those in the
2114 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2117 tree new_spec_types
;
2122 if (spec_types
== void_list_node
)
2123 return void_list_node
;
2125 /* Substitute into the rest of the list. */
2127 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2128 TREE_CHAIN (tmpl_types
));
2130 /* Add the default argument for this parameter. */
2131 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2132 TREE_VALUE (spec_types
),
2136 /* DECL is an explicit specialization. Replicate default arguments
2137 from the template it specializes. (That way, code like:
2139 template <class T> void f(T = 3);
2140 template <> void f(double);
2143 works, as required.) An alternative approach would be to look up
2144 the correct default arguments at the call-site, but this approach
2145 is consistent with how implicit instantiations are handled. */
2148 copy_default_args_to_explicit_spec (tree decl
)
2153 tree new_spec_types
;
2157 tree object_type
= NULL_TREE
;
2158 tree in_charge
= NULL_TREE
;
2159 tree vtt
= NULL_TREE
;
2161 /* See if there's anything we need to do. */
2162 tmpl
= DECL_TI_TEMPLATE (decl
);
2163 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2164 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2165 if (TREE_PURPOSE (t
))
2170 old_type
= TREE_TYPE (decl
);
2171 spec_types
= TYPE_ARG_TYPES (old_type
);
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2175 /* Remove the this pointer, but remember the object's type for
2177 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2178 spec_types
= TREE_CHAIN (spec_types
);
2179 tmpl_types
= TREE_CHAIN (tmpl_types
);
2181 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2183 /* DECL may contain more parameters than TMPL due to the extra
2184 in-charge parameter in constructors and destructors. */
2185 in_charge
= spec_types
;
2186 spec_types
= TREE_CHAIN (spec_types
);
2188 if (DECL_HAS_VTT_PARM_P (decl
))
2191 spec_types
= TREE_CHAIN (spec_types
);
2195 /* Compute the merged default arguments. */
2197 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2199 /* Compute the new FUNCTION_TYPE. */
2203 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2208 /* Put the in-charge parameter back. */
2209 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2210 TREE_VALUE (in_charge
),
2213 new_type
= build_method_type_directly (object_type
,
2214 TREE_TYPE (old_type
),
2218 new_type
= build_function_type (TREE_TYPE (old_type
),
2220 new_type
= cp_build_type_attribute_variant (new_type
,
2221 TYPE_ATTRIBUTES (old_type
));
2222 new_type
= build_exception_variant (new_type
,
2223 TYPE_RAISES_EXCEPTIONS (old_type
));
2224 TREE_TYPE (decl
) = new_type
;
2227 /* Return the number of template headers we expect to see for a definition
2228 or specialization of CTYPE or one of its non-template members. */
2231 num_template_headers_for_class (tree ctype
)
2233 int num_templates
= 0;
2235 while (ctype
&& CLASS_TYPE_P (ctype
))
2237 /* You're supposed to have one `template <...>' for every
2238 template class, but you don't need one for a full
2239 specialization. For example:
2241 template <class T> struct S{};
2242 template <> struct S<int> { void f(); };
2243 void S<int>::f () {}
2245 is correct; there shouldn't be a `template <>' for the
2246 definition of `S<int>::f'. */
2247 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2248 /* If CTYPE does not have template information of any
2249 kind, then it is not a template, nor is it nested
2250 within a template. */
2252 if (explicit_class_specialization_p (ctype
))
2254 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2257 ctype
= TYPE_CONTEXT (ctype
);
2260 return num_templates
;
2263 /* Do a simple sanity check on the template headers that precede the
2264 variable declaration DECL. */
2267 check_template_variable (tree decl
)
2269 tree ctx
= CP_DECL_CONTEXT (decl
);
2270 int wanted
= num_template_headers_for_class (ctx
);
2271 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2272 permerror (DECL_SOURCE_LOCATION (decl
),
2273 "%qD is not a static data member of a class template", decl
);
2274 else if (template_header_count
> wanted
)
2276 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2277 "too many template headers for %D (should be %d)",
2279 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2280 inform (DECL_SOURCE_LOCATION (decl
),
2281 "members of an explicitly specialized class are defined "
2282 "without a template header");
2286 /* Check to see if the function just declared, as indicated in
2287 DECLARATOR, and in DECL, is a specialization of a function
2288 template. We may also discover that the declaration is an explicit
2289 instantiation at this point.
2291 Returns DECL, or an equivalent declaration that should be used
2292 instead if all goes well. Issues an error message if something is
2293 amiss. Returns error_mark_node if the error is not easily
2296 FLAGS is a bitmask consisting of the following flags:
2298 2: The function has a definition.
2299 4: The function is a friend.
2301 The TEMPLATE_COUNT is the number of references to qualifying
2302 template classes that appeared in the name of the function. For
2305 template <class T> struct S { void f(); };
2308 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2309 classes are not counted in the TEMPLATE_COUNT, so that in
2311 template <class T> struct S {};
2312 template <> struct S<int> { void f(); }
2313 template <> void S<int>::f();
2315 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2316 invalid; there should be no template <>.)
2318 If the function is a specialization, it is marked as such via
2319 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2320 is set up correctly, and it is added to the list of specializations
2321 for that template. */
2324 check_explicit_specialization (tree declarator
,
2329 int have_def
= flags
& 2;
2330 int is_friend
= flags
& 4;
2331 int specialization
= 0;
2332 int explicit_instantiation
= 0;
2333 int member_specialization
= 0;
2334 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2335 tree dname
= DECL_NAME (decl
);
2340 if (!processing_specialization
)
2343 tsk
= tsk_excessive_parms
;
2346 tsk
= current_tmpl_spec_kind (template_count
);
2351 if (processing_specialization
)
2354 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2356 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2359 /* This could be something like:
2361 template <class T> void f(T);
2362 class S { friend void f<>(int); } */
2366 /* This case handles bogus declarations like template <>
2367 template <class T> void f<int>(); */
2369 error ("template-id %qD in declaration of primary template",
2376 case tsk_invalid_member_spec
:
2377 /* The error has already been reported in
2378 check_specialization_scope. */
2379 return error_mark_node
;
2381 case tsk_invalid_expl_inst
:
2382 error ("template parameter list used in explicit instantiation");
2388 error ("definition provided for explicit instantiation");
2390 explicit_instantiation
= 1;
2393 case tsk_excessive_parms
:
2394 case tsk_insufficient_parms
:
2395 if (tsk
== tsk_excessive_parms
)
2396 error ("too many template parameter lists in declaration of %qD",
2398 else if (template_header_count
)
2399 error("too few template parameter lists in declaration of %qD", decl
);
2401 error("explicit specialization of %qD must be introduced by "
2402 "%<template <>%>", decl
);
2406 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2408 member_specialization
= 1;
2414 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2416 /* This case handles bogus declarations like template <>
2417 template <class T> void f<int>(); */
2419 if (uses_template_parms (declarator
))
2420 error ("function template partial specialization %qD "
2421 "is not allowed", declarator
);
2423 error ("template-id %qD in declaration of primary template",
2428 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2429 /* This is a specialization of a member template, without
2430 specialization the containing class. Something like:
2432 template <class T> struct S {
2433 template <class U> void f (U);
2435 template <> template <class U> void S<int>::f(U) {}
2437 That's a specialization -- but of the entire template. */
2445 if (specialization
|| member_specialization
)
2447 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2448 for (; t
; t
= TREE_CHAIN (t
))
2449 if (TREE_PURPOSE (t
))
2451 permerror (input_location
,
2452 "default argument specified in explicit specialization");
2457 if (specialization
|| member_specialization
|| explicit_instantiation
)
2459 tree tmpl
= NULL_TREE
;
2460 tree targs
= NULL_TREE
;
2462 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2463 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2467 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2472 /* If there is no class context, the explicit instantiation
2473 must be at namespace scope. */
2474 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2476 /* Find the namespace binding, using the declaration
2478 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2480 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2482 error ("%qD is not a template function", dname
);
2483 fns
= error_mark_node
;
2487 tree fn
= OVL_CURRENT (fns
);
2488 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2489 CP_DECL_CONTEXT (fn
)))
2490 error ("%qD is not declared in %qD",
2491 decl
, current_namespace
);
2495 declarator
= lookup_template_function (fns
, NULL_TREE
);
2498 if (declarator
== error_mark_node
)
2499 return error_mark_node
;
2501 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2503 if (!explicit_instantiation
)
2504 /* A specialization in class scope. This is invalid,
2505 but the error will already have been flagged by
2506 check_specialization_scope. */
2507 return error_mark_node
;
2510 /* It's not valid to write an explicit instantiation in
2513 class C { template void f(); }
2515 This case is caught by the parser. However, on
2518 template class C { void f(); };
2520 (which is invalid) we can get here. The error will be
2527 else if (ctype
!= NULL_TREE
2528 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2531 /* Find the list of functions in ctype that have the same
2532 name as the declared function. */
2533 tree name
= TREE_OPERAND (declarator
, 0);
2534 tree fns
= NULL_TREE
;
2537 if (constructor_name_p (name
, ctype
))
2539 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2541 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2542 : !CLASSTYPE_DESTRUCTORS (ctype
))
2544 /* From [temp.expl.spec]:
2546 If such an explicit specialization for the member
2547 of a class template names an implicitly-declared
2548 special member function (clause _special_), the
2549 program is ill-formed.
2551 Similar language is found in [temp.explicit]. */
2552 error ("specialization of implicitly-declared special member function");
2553 return error_mark_node
;
2556 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2559 if (!DECL_CONV_FN_P (decl
))
2561 idx
= lookup_fnfields_1 (ctype
, name
);
2563 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2567 vec
<tree
, va_gc
> *methods
;
2570 /* For a type-conversion operator, we cannot do a
2571 name-based lookup. We might be looking for `operator
2572 int' which will be a specialization of `operator T'.
2573 So, we find *all* the conversion operators, and then
2574 select from them. */
2577 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2579 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2580 methods
->iterate (idx
, &ovl
);
2583 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2584 /* There are no more conversion functions. */
2587 /* Glue all these conversion functions together
2588 with those we already have. */
2589 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2590 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2594 if (fns
== NULL_TREE
)
2596 error ("no member function %qD declared in %qT", name
, ctype
);
2597 return error_mark_node
;
2600 TREE_OPERAND (declarator
, 0) = fns
;
2603 /* Figure out what exactly is being specialized at this point.
2604 Note that for an explicit instantiation, even one for a
2605 member function, we cannot tell apriori whether the
2606 instantiation is for a member template, or just a member
2607 function of a template class. Even if a member template is
2608 being instantiated, the member template arguments may be
2609 elided if they can be deduced from the rest of the
2611 tmpl
= determine_specialization (declarator
, decl
,
2613 member_specialization
,
2617 if (!tmpl
|| tmpl
== error_mark_node
)
2618 /* We couldn't figure out what this declaration was
2620 return error_mark_node
;
2623 tree gen_tmpl
= most_general_template (tmpl
);
2625 if (explicit_instantiation
)
2627 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2628 is done by do_decl_instantiation later. */
2630 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2631 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2633 if (arg_depth
> parm_depth
)
2635 /* If TMPL is not the most general template (for
2636 example, if TMPL is a friend template that is
2637 injected into namespace scope), then there will
2638 be too many levels of TARGS. Remove some of them
2643 new_targs
= make_tree_vec (parm_depth
);
2644 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2645 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2646 = TREE_VEC_ELT (targs
, i
);
2650 return instantiate_template (tmpl
, targs
, tf_error
);
2653 /* If we thought that the DECL was a member function, but it
2654 turns out to be specializing a static member function,
2655 make DECL a static member function as well. */
2656 if (DECL_STATIC_FUNCTION_P (tmpl
)
2657 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2658 revert_static_member_fn (decl
);
2660 /* If this is a specialization of a member template of a
2661 template class, we want to return the TEMPLATE_DECL, not
2662 the specialization of it. */
2663 if (tsk
== tsk_template
)
2665 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2666 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2667 DECL_INITIAL (result
) = NULL_TREE
;
2671 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2672 DECL_SOURCE_LOCATION (result
)
2673 = DECL_SOURCE_LOCATION (decl
);
2674 /* We want to use the argument list specified in the
2675 definition, not in the original declaration. */
2676 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2677 for (parm
= DECL_ARGUMENTS (result
); parm
;
2678 parm
= DECL_CHAIN (parm
))
2679 DECL_CONTEXT (parm
) = result
;
2681 return register_specialization (tmpl
, gen_tmpl
, targs
,
2685 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2686 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2688 /* Inherit default function arguments from the template
2689 DECL is specializing. */
2690 copy_default_args_to_explicit_spec (decl
);
2692 /* This specialization has the same protection as the
2693 template it specializes. */
2694 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2695 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2697 /* 7.1.1-1 [dcl.stc]
2699 A storage-class-specifier shall not be specified in an
2700 explicit specialization...
2702 The parser rejects these, so unless action is taken here,
2703 explicit function specializations will always appear with
2706 The action recommended by the C++ CWG in response to C++
2707 defect report 605 is to make the storage class and linkage
2708 of the explicit specialization match the templated function:
2710 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2712 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2714 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2715 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2717 /* This specialization has the same linkage and visibility as
2718 the function template it specializes. */
2719 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2720 if (! TREE_PUBLIC (decl
))
2722 DECL_INTERFACE_KNOWN (decl
) = 1;
2723 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2725 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2726 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2728 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2729 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2733 /* If DECL is a friend declaration, declared using an
2734 unqualified name, the namespace associated with DECL may
2735 have been set incorrectly. For example, in:
2737 template <typename T> void f(T);
2739 struct S { friend void f<int>(int); }
2742 we will have set the DECL_CONTEXT for the friend
2743 declaration to N, rather than to the global namespace. */
2744 if (DECL_NAMESPACE_SCOPE_P (decl
))
2745 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2747 if (is_friend
&& !have_def
)
2748 /* This is not really a declaration of a specialization.
2749 It's just the name of an instantiation. But, it's not
2750 a request for an instantiation, either. */
2751 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2752 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2753 /* This is indeed a specialization. In case of constructors
2754 and destructors, we need in-charge and not-in-charge
2755 versions in V3 ABI. */
2756 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2758 /* Register this specialization so that we can find it
2760 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2767 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2768 parameters. These are represented in the same format used for
2769 DECL_TEMPLATE_PARMS. */
2772 comp_template_parms (const_tree parms1
, const_tree parms2
)
2777 if (parms1
== parms2
)
2780 for (p1
= parms1
, p2
= parms2
;
2781 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2782 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2784 tree t1
= TREE_VALUE (p1
);
2785 tree t2
= TREE_VALUE (p2
);
2788 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2789 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2791 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2794 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2796 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2797 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2799 /* If either of the template parameters are invalid, assume
2800 they match for the sake of error recovery. */
2801 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2804 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2807 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2808 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2809 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2811 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2816 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2817 /* One set of parameters has more parameters lists than the
2824 /* Determine whether PARM is a parameter pack. */
2827 template_parameter_pack_p (const_tree parm
)
2829 /* Determine if we have a non-type template parameter pack. */
2830 if (TREE_CODE (parm
) == PARM_DECL
)
2831 return (DECL_TEMPLATE_PARM_P (parm
)
2832 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2833 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2834 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2836 /* If this is a list of template parameters, we could get a
2837 TYPE_DECL or a TEMPLATE_DECL. */
2838 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2839 parm
= TREE_TYPE (parm
);
2841 /* Otherwise it must be a type template parameter. */
2842 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2843 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2844 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2847 /* Determine if T is a function parameter pack. */
2850 function_parameter_pack_p (const_tree t
)
2852 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2853 return FUNCTION_PARAMETER_PACK_P (t
);
2857 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2858 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2861 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2863 if (! primary_func_tmpl_inst
2864 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2865 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2868 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2871 /* Return true iff the function parameter PARAM_DECL was expanded
2872 from the function parameter pack PACK. */
2875 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2877 if (DECL_ARTIFICIAL (param_decl
)
2878 || !function_parameter_pack_p (pack
))
2881 /* The parameter pack and its pack arguments have the same
2883 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2886 /* Determine whether ARGS describes a variadic template args list,
2887 i.e., one that is terminated by a template argument pack. */
2890 template_args_variadic_p (tree args
)
2895 if (args
== NULL_TREE
)
2898 args
= INNERMOST_TEMPLATE_ARGS (args
);
2899 nargs
= TREE_VEC_LENGTH (args
);
2904 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2906 return ARGUMENT_PACK_P (last_parm
);
2909 /* Generate a new name for the parameter pack name NAME (an
2910 IDENTIFIER_NODE) that incorporates its */
2913 make_ith_pack_parameter_name (tree name
, int i
)
2915 /* Munge the name to include the parameter index. */
2916 #define NUMBUF_LEN 128
2917 char numbuf
[NUMBUF_LEN
];
2921 if (name
== NULL_TREE
)
2923 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2924 newname_len
= IDENTIFIER_LENGTH (name
)
2925 + strlen (numbuf
) + 2;
2926 newname
= (char*)alloca (newname_len
);
2927 snprintf (newname
, newname_len
,
2928 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2929 return get_identifier (newname
);
2932 /* Return true if T is a primary function, class or alias template
2936 primary_template_instantiation_p (const_tree t
)
2941 if (TREE_CODE (t
) == FUNCTION_DECL
)
2942 return DECL_LANG_SPECIFIC (t
)
2943 && DECL_TEMPLATE_INSTANTIATION (t
)
2944 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2945 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2946 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2947 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2948 else if (alias_template_specialization_p (t
))
2953 /* Return true if PARM is a template template parameter. */
2956 template_template_parameter_p (const_tree parm
)
2958 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2961 /* Return true iff PARM is a DECL representing a type template
2965 template_type_parameter_p (const_tree parm
)
2968 && (TREE_CODE (parm
) == TYPE_DECL
2969 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2970 && DECL_TEMPLATE_PARM_P (parm
));
2973 /* Return the template parameters of T if T is a
2974 primary template instantiation, NULL otherwise. */
2977 get_primary_template_innermost_parameters (const_tree t
)
2979 tree parms
= NULL
, template_info
= NULL
;
2981 if ((template_info
= get_template_info (t
))
2982 && primary_template_instantiation_p (t
))
2983 parms
= INNERMOST_TEMPLATE_PARMS
2984 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2989 /* Return the template parameters of the LEVELth level from the full list
2990 of template parameters PARMS. */
2993 get_template_parms_at_level (tree parms
, int level
)
2997 || TREE_CODE (parms
) != TREE_LIST
2998 || level
> TMPL_PARMS_DEPTH (parms
))
3001 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3002 if (TMPL_PARMS_DEPTH (p
) == level
)
3008 /* Returns the template arguments of T if T is a template instantiation,
3012 get_template_innermost_arguments (const_tree t
)
3014 tree args
= NULL
, template_info
= NULL
;
3016 if ((template_info
= get_template_info (t
))
3017 && TI_ARGS (template_info
))
3018 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3023 /* Return the argument pack elements of T if T is a template argument pack,
3027 get_template_argument_pack_elems (const_tree t
)
3029 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3030 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3033 return ARGUMENT_PACK_ARGS (t
);
3036 /* Structure used to track the progress of find_parameter_packs_r. */
3037 struct find_parameter_pack_data
3039 /* TREE_LIST that will contain all of the parameter packs found by
3041 tree
* parameter_packs
;
3043 /* Set of AST nodes that have been visited by the traversal. */
3044 struct pointer_set_t
*visited
;
3047 /* Identifies all of the argument packs that occur in a template
3048 argument and appends them to the TREE_LIST inside DATA, which is a
3049 find_parameter_pack_data structure. This is a subroutine of
3050 make_pack_expansion and uses_parameter_packs. */
3052 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3055 struct find_parameter_pack_data
* ppd
=
3056 (struct find_parameter_pack_data
*)data
;
3057 bool parameter_pack_p
= false;
3059 /* Handle type aliases/typedefs. */
3062 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
3063 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3065 if (TYPE_TEMPLATE_INFO (t
))
3066 cp_walk_tree (&TYPE_TI_ARGS (t
),
3067 &find_parameter_packs_r
,
3073 /* Identify whether this is a parameter pack or not. */
3074 switch (TREE_CODE (t
))
3076 case TEMPLATE_PARM_INDEX
:
3077 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3078 parameter_pack_p
= true;
3081 case TEMPLATE_TYPE_PARM
:
3082 t
= TYPE_MAIN_VARIANT (t
);
3083 case TEMPLATE_TEMPLATE_PARM
:
3084 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3085 parameter_pack_p
= true;
3089 if (FUNCTION_PARAMETER_PACK_P (t
))
3091 /* We don't want to walk into the type of a PARM_DECL,
3092 because we don't want to see the type parameter pack. */
3094 parameter_pack_p
= true;
3099 parameter_pack_p
= true;
3102 /* Not a parameter pack. */
3106 if (parameter_pack_p
)
3108 /* Add this parameter pack to the list. */
3109 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3113 cp_walk_tree (&TYPE_CONTEXT (t
),
3114 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3116 /* This switch statement will return immediately if we don't find a
3118 switch (TREE_CODE (t
))
3120 case TEMPLATE_PARM_INDEX
:
3123 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3124 /* Check the template itself. */
3125 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3126 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3127 /* Check the template arguments. */
3128 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3133 case TEMPLATE_TYPE_PARM
:
3134 case TEMPLATE_TEMPLATE_PARM
:
3141 if (TYPE_PTRMEMFUNC_P (t
))
3147 if (TYPE_TEMPLATE_INFO (t
))
3148 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3149 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3156 cp_walk_tree (&TREE_TYPE (t
),
3157 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3161 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3166 case TYPE_PACK_EXPANSION
:
3167 case EXPR_PACK_EXPANSION
:
3172 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3177 case IDENTIFIER_NODE
:
3178 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3190 /* Determines if the expression or type T uses any parameter packs. */
3192 uses_parameter_packs (tree t
)
3194 tree parameter_packs
= NULL_TREE
;
3195 struct find_parameter_pack_data ppd
;
3196 ppd
.parameter_packs
= ¶meter_packs
;
3197 ppd
.visited
= pointer_set_create ();
3198 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3199 pointer_set_destroy (ppd
.visited
);
3200 return parameter_packs
!= NULL_TREE
;
3203 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3204 representation a base-class initializer into a parameter pack
3205 expansion. If all goes well, the resulting node will be an
3206 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3209 make_pack_expansion (tree arg
)
3212 tree parameter_packs
= NULL_TREE
;
3213 bool for_types
= false;
3214 struct find_parameter_pack_data ppd
;
3216 if (!arg
|| arg
== error_mark_node
)
3219 if (TREE_CODE (arg
) == TREE_LIST
)
3221 /* The only time we will see a TREE_LIST here is for a base
3222 class initializer. In this case, the TREE_PURPOSE will be a
3223 _TYPE node (representing the base class expansion we're
3224 initializing) and the TREE_VALUE will be a TREE_LIST
3225 containing the initialization arguments.
3227 The resulting expansion looks somewhat different from most
3228 expansions. Rather than returning just one _EXPANSION, we
3229 return a TREE_LIST whose TREE_PURPOSE is a
3230 TYPE_PACK_EXPANSION containing the bases that will be
3231 initialized. The TREE_VALUE will be identical to the
3232 original TREE_VALUE, which is a list of arguments that will
3233 be passed to each base. We do not introduce any new pack
3234 expansion nodes into the TREE_VALUE (although it is possible
3235 that some already exist), because the TREE_PURPOSE and
3236 TREE_VALUE all need to be expanded together with the same
3237 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3238 resulting TREE_PURPOSE will mention the parameter packs in
3239 both the bases and the arguments to the bases. */
3242 tree parameter_packs
= NULL_TREE
;
3244 /* Determine which parameter packs will be used by the base
3246 ppd
.visited
= pointer_set_create ();
3247 ppd
.parameter_packs
= ¶meter_packs
;
3248 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3251 if (parameter_packs
== NULL_TREE
)
3253 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3254 pointer_set_destroy (ppd
.visited
);
3255 return error_mark_node
;
3258 if (TREE_VALUE (arg
) != void_type_node
)
3260 /* Collect the sets of parameter packs used in each of the
3261 initialization arguments. */
3262 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3264 /* Determine which parameter packs will be expanded in this
3266 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3271 pointer_set_destroy (ppd
.visited
);
3273 /* Create the pack expansion type for the base type. */
3274 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3275 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3276 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3278 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3279 they will rarely be compared to anything. */
3280 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3282 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3285 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3288 /* Build the PACK_EXPANSION_* node. */
3290 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3291 : make_node (EXPR_PACK_EXPANSION
);
3292 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3293 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3295 /* Propagate type and const-expression information. */
3296 TREE_TYPE (result
) = TREE_TYPE (arg
);
3297 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3300 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3301 they will rarely be compared to anything. */
3302 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3304 /* Determine which parameter packs will be expanded. */
3305 ppd
.parameter_packs
= ¶meter_packs
;
3306 ppd
.visited
= pointer_set_create ();
3307 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3308 pointer_set_destroy (ppd
.visited
);
3310 /* Make sure we found some parameter packs. */
3311 if (parameter_packs
== NULL_TREE
)
3314 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3316 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3317 return error_mark_node
;
3319 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3321 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3326 /* Checks T for any "bare" parameter packs, which have not yet been
3327 expanded, and issues an error if any are found. This operation can
3328 only be done on full expressions or types (e.g., an expression
3329 statement, "if" condition, etc.), because we could have expressions like:
3331 foo(f(g(h(args)))...)
3333 where "args" is a parameter pack. check_for_bare_parameter_packs
3334 should not be called for the subexpressions args, h(args),
3335 g(h(args)), or f(g(h(args))), because we would produce erroneous
3338 Returns TRUE and emits an error if there were bare parameter packs,
3339 returns FALSE otherwise. */
3341 check_for_bare_parameter_packs (tree t
)
3343 tree parameter_packs
= NULL_TREE
;
3344 struct find_parameter_pack_data ppd
;
3346 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3349 if (TREE_CODE (t
) == TYPE_DECL
)
3352 ppd
.parameter_packs
= ¶meter_packs
;
3353 ppd
.visited
= pointer_set_create ();
3354 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3355 pointer_set_destroy (ppd
.visited
);
3357 if (parameter_packs
)
3359 error ("parameter packs not expanded with %<...%>:");
3360 while (parameter_packs
)
3362 tree pack
= TREE_VALUE (parameter_packs
);
3363 tree name
= NULL_TREE
;
3365 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3366 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3367 name
= TYPE_NAME (pack
);
3368 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3369 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3371 name
= DECL_NAME (pack
);
3374 inform (input_location
, " %qD", name
);
3376 inform (input_location
, " <anonymous>");
3378 parameter_packs
= TREE_CHAIN (parameter_packs
);
3387 /* Expand any parameter packs that occur in the template arguments in
3390 expand_template_argument_pack (tree args
)
3392 tree result_args
= NULL_TREE
;
3393 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3394 int num_result_args
= -1;
3395 int non_default_args_count
= -1;
3397 /* First, determine if we need to expand anything, and the number of
3398 slots we'll need. */
3399 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3401 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3402 if (arg
== NULL_TREE
)
3404 if (ARGUMENT_PACK_P (arg
))
3406 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3407 if (num_result_args
< 0)
3408 num_result_args
= in_arg
+ num_packed
;
3410 num_result_args
+= num_packed
;
3414 if (num_result_args
>= 0)
3419 /* If no expansion is necessary, we're done. */
3420 if (num_result_args
< 0)
3423 /* Expand arguments. */
3424 result_args
= make_tree_vec (num_result_args
);
3425 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3426 non_default_args_count
=
3427 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3428 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3430 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3431 if (ARGUMENT_PACK_P (arg
))
3433 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3434 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3435 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3436 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3437 if (non_default_args_count
> 0)
3438 non_default_args_count
+= num_packed
;
3442 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3446 if (non_default_args_count
>= 0)
3447 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3451 /* Checks if DECL shadows a template parameter.
3453 [temp.local]: A template-parameter shall not be redeclared within its
3454 scope (including nested scopes).
3456 Emits an error and returns TRUE if the DECL shadows a parameter,
3457 returns FALSE otherwise. */
3460 check_template_shadow (tree decl
)
3464 /* If we're not in a template, we can't possibly shadow a template
3466 if (!current_template_parms
)
3469 /* Figure out what we're shadowing. */
3470 if (TREE_CODE (decl
) == OVERLOAD
)
3471 decl
= OVL_CURRENT (decl
);
3472 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3474 /* If there's no previous binding for this name, we're not shadowing
3475 anything, let alone a template parameter. */
3479 /* If we're not shadowing a template parameter, we're done. Note
3480 that OLDDECL might be an OVERLOAD (or perhaps even an
3481 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3483 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3486 /* We check for decl != olddecl to avoid bogus errors for using a
3487 name inside a class. We check TPFI to avoid duplicate errors for
3488 inline member templates. */
3490 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3493 error ("declaration of %q+#D", decl
);
3494 error (" shadows template parm %q+#D", olddecl
);
3498 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3499 ORIG_LEVEL, DECL, and TYPE. */
3502 build_template_parm_index (int index
,
3508 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3509 TEMPLATE_PARM_IDX (t
) = index
;
3510 TEMPLATE_PARM_LEVEL (t
) = level
;
3511 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3512 TEMPLATE_PARM_DECL (t
) = decl
;
3513 TREE_TYPE (t
) = type
;
3514 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3515 TREE_READONLY (t
) = TREE_READONLY (decl
);
3520 /* Find the canonical type parameter for the given template type
3521 parameter. Returns the canonical type parameter, which may be TYPE
3522 if no such parameter existed. */
3525 canonical_type_parameter (tree type
)
3528 int idx
= TEMPLATE_TYPE_IDX (type
);
3529 if (!canonical_template_parms
)
3530 vec_alloc (canonical_template_parms
, idx
+1);
3532 while (canonical_template_parms
->length () <= (unsigned)idx
)
3533 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3535 list
= (*canonical_template_parms
)[idx
];
3536 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3537 list
= TREE_CHAIN (list
);
3540 return TREE_VALUE (list
);
3543 (*canonical_template_parms
)[idx
]
3544 = tree_cons (NULL_TREE
, type
,
3545 (*canonical_template_parms
)[idx
]);
3550 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3551 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3552 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3553 new one is created. */
3556 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3557 tsubst_flags_t complain
)
3559 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3560 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3561 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3562 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3564 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3567 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3568 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3569 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3570 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3571 DECL_ARTIFICIAL (decl
) = 1;
3572 SET_DECL_TEMPLATE_PARM_P (decl
);
3574 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3575 TEMPLATE_PARM_LEVEL (index
) - levels
,
3576 TEMPLATE_PARM_ORIG_LEVEL (index
),
3578 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3579 TEMPLATE_PARM_PARAMETER_PACK (t
)
3580 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3582 /* Template template parameters need this. */
3583 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3584 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3585 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3589 return TEMPLATE_PARM_DESCENDANTS (index
);
3592 /* Process information from new template parameter PARM and append it
3593 to the LIST being built. This new parameter is a non-type
3594 parameter iff IS_NON_TYPE is true. This new parameter is a
3595 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3596 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3597 parameter list PARM belongs to. This is used used to create a
3598 proper canonical type for the type of PARM that is to be created,
3599 iff PARM is a type. If the size is not known, this parameter shall
3603 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3604 bool is_non_type
, bool is_parameter_pack
)
3611 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3612 defval
= TREE_PURPOSE (parm
);
3616 tree p
= tree_last (list
);
3618 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3621 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3622 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3624 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3634 parm
= TREE_VALUE (parm
);
3636 SET_DECL_TEMPLATE_PARM_P (parm
);
3638 if (TREE_TYPE (parm
) == error_mark_node
)
3640 err_parm_list
= build_tree_list (defval
, parm
);
3641 TREE_VALUE (err_parm_list
) = error_mark_node
;
3642 return chainon (list
, err_parm_list
);
3648 The top-level cv-qualifiers on the template-parameter are
3649 ignored when determining its type. */
3650 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3651 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3653 err_parm_list
= build_tree_list (defval
, parm
);
3654 TREE_VALUE (err_parm_list
) = error_mark_node
;
3655 return chainon (list
, err_parm_list
);
3658 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3660 /* This template parameter is not a parameter pack, but it
3661 should be. Complain about "bare" parameter packs. */
3662 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3664 /* Recover by calling this a parameter pack. */
3665 is_parameter_pack
= true;
3669 /* A template parameter is not modifiable. */
3670 TREE_CONSTANT (parm
) = 1;
3671 TREE_READONLY (parm
) = 1;
3672 decl
= build_decl (parm_loc
,
3673 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3674 TREE_CONSTANT (decl
) = 1;
3675 TREE_READONLY (decl
) = 1;
3676 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3677 = build_template_parm_index (idx
, processing_template_decl
,
3678 processing_template_decl
,
3679 decl
, TREE_TYPE (parm
));
3681 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3682 = is_parameter_pack
;
3687 parm
= TREE_VALUE (TREE_VALUE (parm
));
3689 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3691 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3692 /* This is for distinguishing between real templates and template
3693 template parameters */
3694 TREE_TYPE (parm
) = t
;
3695 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3700 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3701 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3702 decl
= build_decl (parm_loc
,
3703 TYPE_DECL
, parm
, t
);
3706 TYPE_NAME (t
) = decl
;
3707 TYPE_STUB_DECL (t
) = decl
;
3709 TEMPLATE_TYPE_PARM_INDEX (t
)
3710 = build_template_parm_index (idx
, processing_template_decl
,
3711 processing_template_decl
,
3712 decl
, TREE_TYPE (parm
));
3713 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3714 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3716 DECL_ARTIFICIAL (decl
) = 1;
3717 SET_DECL_TEMPLATE_PARM_P (decl
);
3719 parm
= build_tree_list (defval
, parm
);
3720 return chainon (list
, parm
);
3723 /* The end of a template parameter list has been reached. Process the
3724 tree list into a parameter vector, converting each parameter into a more
3725 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3729 end_template_parm_list (tree parms
)
3733 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3735 current_template_parms
3736 = tree_cons (size_int (processing_template_decl
),
3737 saved_parmlist
, current_template_parms
);
3739 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3741 next
= TREE_CHAIN (parm
);
3742 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3743 TREE_CHAIN (parm
) = NULL_TREE
;
3746 --processing_template_parmlist
;
3748 return saved_parmlist
;
3751 /* end_template_decl is called after a template declaration is seen. */
3754 end_template_decl (void)
3756 reset_specialization ();
3758 if (! processing_template_decl
)
3761 /* This matches the pushlevel in begin_template_parm_list. */
3764 --processing_template_decl
;
3765 current_template_parms
= TREE_CHAIN (current_template_parms
);
3768 /* Takes a TREE_LIST representing a template parameter and convert it
3769 into an argument suitable to be passed to the type substitution
3770 functions. Note that If the TREE_LIST contains an error_mark
3771 node, the returned argument is error_mark_node. */
3774 template_parm_to_arg (tree t
)
3778 || TREE_CODE (t
) != TREE_LIST
)
3781 if (error_operand_p (TREE_VALUE (t
)))
3782 return error_mark_node
;
3786 if (TREE_CODE (t
) == TYPE_DECL
3787 || TREE_CODE (t
) == TEMPLATE_DECL
)
3791 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3793 /* Turn this argument into a TYPE_ARGUMENT_PACK
3794 with a single element, which expands T. */
3795 tree vec
= make_tree_vec (1);
3796 #ifdef ENABLE_CHECKING
3797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3798 (vec
, TREE_VEC_LENGTH (vec
));
3800 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3802 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3803 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3808 t
= DECL_INITIAL (t
);
3810 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3812 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3813 with a single element, which expands T. */
3814 tree vec
= make_tree_vec (1);
3815 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3816 #ifdef ENABLE_CHECKING
3817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3818 (vec
, TREE_VEC_LENGTH (vec
));
3820 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3822 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3823 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3824 TREE_TYPE (t
) = type
;
3830 /* Given a set of template parameters, return them as a set of template
3831 arguments. The template parameters are represented as a TREE_VEC, in
3832 the form documented in cp-tree.h for template arguments. */
3835 template_parms_to_args (tree parms
)
3838 tree args
= NULL_TREE
;
3839 int length
= TMPL_PARMS_DEPTH (parms
);
3842 /* If there is only one level of template parameters, we do not
3843 create a TREE_VEC of TREE_VECs. Instead, we return a single
3844 TREE_VEC containing the arguments. */
3846 args
= make_tree_vec (length
);
3848 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3850 tree a
= copy_node (TREE_VALUE (header
));
3853 TREE_TYPE (a
) = NULL_TREE
;
3854 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3855 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3857 #ifdef ENABLE_CHECKING
3858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3862 TREE_VEC_ELT (args
, --l
) = a
;
3867 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3868 /* This can happen for template parms of a template template
3871 template<template<class T, class U> class TT> struct S;
3873 Consider the level of the parms of TT; T and U both have
3874 level 2; TT has no template parm of level 1. So in this case
3875 the first element of full_template_args is NULL_TREE. If we
3876 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3877 of 2. This will make tsubst wrongly consider that T and U
3878 have level 1. Instead, let's create a dummy vector as the
3879 first element of full_template_args so that TMPL_ARG_DEPTH
3880 returns the correct depth for args. */
3881 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3885 /* Within the declaration of a template, return the currently active
3886 template parameters as an argument TREE_VEC. */
3889 current_template_args (void)
3891 return template_parms_to_args (current_template_parms
);
3894 /* Update the declared TYPE by doing any lookups which were thought to be
3895 dependent, but are not now that we know the SCOPE of the declarator. */
3898 maybe_update_decl_type (tree orig_type
, tree scope
)
3900 tree type
= orig_type
;
3902 if (type
== NULL_TREE
)
3905 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3906 type
= TREE_TYPE (type
);
3908 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3909 && dependent_type_p (type
)
3910 /* Don't bother building up the args in this case. */
3911 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3913 /* tsubst in the args corresponding to the template parameters,
3914 including auto if present. Most things will be unchanged, but
3915 make_typename_type and tsubst_qualified_id will resolve
3916 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3917 tree args
= current_template_args ();
3918 tree auto_node
= type_uses_auto (type
);
3922 tree auto_vec
= make_tree_vec (1);
3923 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3924 args
= add_to_template_args (args
, auto_vec
);
3926 pushed
= push_scope (scope
);
3927 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3932 if (type
== error_mark_node
)
3935 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3937 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3940 type
= TYPE_NAME (type
);
3945 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3946 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3947 a member template. Used by push_template_decl below. */
3950 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3952 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3953 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3954 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3955 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3956 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3961 struct template_parm_data
3963 /* The level of the template parameters we are currently
3967 /* The index of the specialization argument we are currently
3971 /* An array whose size is the number of template parameters. The
3972 elements are nonzero if the parameter has been used in any one
3973 of the arguments processed so far. */
3976 /* An array whose size is the number of template arguments. The
3977 elements are nonzero if the argument makes use of template
3978 parameters of this level. */
3979 int* arg_uses_template_parms
;
3982 /* Subroutine of push_template_decl used to see if each template
3983 parameter in a partial specialization is used in the explicit
3984 argument list. If T is of the LEVEL given in DATA (which is
3985 treated as a template_parm_data*), then DATA->PARMS is marked
3989 mark_template_parm (tree t
, void* data
)
3993 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3995 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3997 level
= TEMPLATE_PARM_LEVEL (t
);
3998 idx
= TEMPLATE_PARM_IDX (t
);
4002 level
= TEMPLATE_TYPE_LEVEL (t
);
4003 idx
= TEMPLATE_TYPE_IDX (t
);
4006 if (level
== tpd
->level
)
4008 tpd
->parms
[idx
] = 1;
4009 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4012 /* Return zero so that for_each_template_parm will continue the
4013 traversal of the tree; we want to mark *every* template parm. */
4017 /* Process the partial specialization DECL. */
4020 process_partial_specialization (tree decl
)
4022 tree type
= TREE_TYPE (decl
);
4023 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4024 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4025 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4026 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4029 int nargs
= TREE_VEC_LENGTH (inner_args
);
4032 bool did_error_intro
= false;
4033 struct template_parm_data tpd
;
4034 struct template_parm_data tpd2
;
4036 gcc_assert (current_template_parms
);
4038 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4039 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4041 /* We check that each of the template parameters given in the
4042 partial specialization is used in the argument list to the
4043 specialization. For example:
4045 template <class T> struct S;
4046 template <class T> struct S<T*>;
4048 The second declaration is OK because `T*' uses the template
4049 parameter T, whereas
4051 template <class T> struct S<int>;
4053 is no good. Even trickier is:
4064 The S2<T> declaration is actually invalid; it is a
4065 full-specialization. Of course,
4068 struct S2<T (*)(U)>;
4070 or some such would have been OK. */
4071 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4072 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4073 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4075 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4076 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4077 for (i
= 0; i
< nargs
; ++i
)
4079 tpd
.current_arg
= i
;
4080 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4081 &mark_template_parm
,
4084 /*include_nondeduced_p=*/false);
4086 for (i
= 0; i
< ntparms
; ++i
)
4087 if (tpd
.parms
[i
] == 0)
4089 /* One of the template parms was not used in the
4091 if (!did_error_intro
)
4093 error ("template parameters not used in partial specialization:");
4094 did_error_intro
= true;
4097 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4100 if (did_error_intro
)
4101 return error_mark_node
;
4103 /* [temp.class.spec]
4105 The argument list of the specialization shall not be identical to
4106 the implicit argument list of the primary template. */
4107 if (comp_template_args
4109 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4111 error ("partial specialization %qT does not specialize any template arguments", type
);
4113 /* A partial specialization that replaces multiple parameters of the
4114 primary template with a pack expansion is less specialized for those
4116 if (nargs
< DECL_NTPARMS (maintmpl
))
4118 error ("partial specialization is not more specialized than the "
4119 "primary template because it replaces multiple parameters "
4120 "with a pack expansion");
4121 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4125 /* [temp.class.spec]
4127 A partially specialized non-type argument expression shall not
4128 involve template parameters of the partial specialization except
4129 when the argument expression is a simple identifier.
4131 The type of a template parameter corresponding to a specialized
4132 non-type argument shall not be dependent on a parameter of the
4135 Also, we verify that pack expansions only occur at the
4136 end of the argument list. */
4137 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4139 for (i
= 0; i
< nargs
; ++i
)
4141 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4142 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4143 tree packed_args
= NULL_TREE
;
4146 if (ARGUMENT_PACK_P (arg
))
4148 /* Extract the arguments from the argument pack. We'll be
4149 iterating over these in the following loop. */
4150 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4151 len
= TREE_VEC_LENGTH (packed_args
);
4154 for (j
= 0; j
< len
; j
++)
4157 /* Get the Jth argument in the parameter pack. */
4158 arg
= TREE_VEC_ELT (packed_args
, j
);
4160 if (PACK_EXPANSION_P (arg
))
4162 /* Pack expansions must come at the end of the
4164 if ((packed_args
&& j
< len
- 1)
4165 || (!packed_args
&& i
< nargs
- 1))
4167 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4168 error ("parameter pack argument %qE must be at the "
4169 "end of the template argument list", arg
);
4171 error ("parameter pack argument %qT must be at the "
4172 "end of the template argument list", arg
);
4176 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4177 /* We only care about the pattern. */
4178 arg
= PACK_EXPANSION_PATTERN (arg
);
4180 if (/* These first two lines are the `non-type' bit. */
4182 && TREE_CODE (arg
) != TEMPLATE_DECL
4183 /* This next line is the `argument expression is not just a
4184 simple identifier' condition and also the `specialized
4185 non-type argument' bit. */
4186 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4188 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4189 || (packed_args
&& uses_template_parms (arg
)))
4190 error ("template argument %qE involves template parameter(s)",
4194 /* Look at the corresponding template parameter,
4195 marking which template parameters its type depends
4197 tree type
= TREE_TYPE (parm
);
4201 /* We haven't yet initialized TPD2. Do so now. */
4202 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4203 /* The number of parameters here is the number in the
4204 main template, which, as checked in the assertion
4206 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4208 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4211 /* Mark the template parameters. But this time, we're
4212 looking for the template parameters of the main
4213 template, not in the specialization. */
4214 tpd2
.current_arg
= i
;
4215 tpd2
.arg_uses_template_parms
[i
] = 0;
4216 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4217 for_each_template_parm (type
,
4218 &mark_template_parm
,
4221 /*include_nondeduced_p=*/false);
4223 if (tpd2
.arg_uses_template_parms
[i
])
4225 /* The type depended on some template parameters.
4226 If they are fully specialized in the
4227 specialization, that's OK. */
4230 for (j
= 0; j
< nargs
; ++j
)
4231 if (tpd2
.parms
[j
] != 0
4232 && tpd
.arg_uses_template_parms
[j
])
4235 error_n (input_location
, count
,
4236 "type %qT of template argument %qE depends "
4237 "on a template parameter",
4238 "type %qT of template argument %qE depends "
4239 "on template parameters",
4248 /* We should only get here once. */
4249 gcc_assert (!COMPLETE_TYPE_P (type
));
4251 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4252 = tree_cons (specargs
, inner_parms
,
4253 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4254 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4256 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4257 inst
= TREE_CHAIN (inst
))
4259 tree inst_type
= TREE_VALUE (inst
);
4260 if (COMPLETE_TYPE_P (inst_type
)
4261 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4263 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4264 if (spec
&& TREE_TYPE (spec
) == type
)
4265 permerror (input_location
,
4266 "partial specialization of %qT after instantiation "
4267 "of %qT", type
, inst_type
);
4274 /* Check that a template declaration's use of default arguments and
4275 parameter packs is not invalid. Here, PARMS are the template
4276 parameters. IS_PRIMARY is true if DECL is the thing declared by
4277 a primary template. IS_PARTIAL is true if DECL is a partial
4280 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4281 declaration (but not a definition); 1 indicates a declaration, 2
4282 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4283 emitted for extraneous default arguments.
4285 Returns TRUE if there were no errors found, FALSE otherwise. */
4288 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4289 bool is_partial
, int is_friend_decl
)
4292 int last_level_to_check
;
4294 bool no_errors
= true;
4298 A default template-argument shall not be specified in a
4299 function template declaration or a function template definition, nor
4300 in the template-parameter-list of the definition of a member of a
4303 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4304 /* You can't have a function template declaration in a local
4305 scope, nor you can you define a member of a class template in a
4309 if (current_class_type
4310 && !TYPE_BEING_DEFINED (current_class_type
)
4311 && DECL_LANG_SPECIFIC (decl
)
4312 && DECL_DECLARES_FUNCTION_P (decl
)
4313 /* If this is either a friend defined in the scope of the class
4314 or a member function. */
4315 && (DECL_FUNCTION_MEMBER_P (decl
)
4316 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4317 : DECL_FRIEND_CONTEXT (decl
)
4318 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4320 /* And, if it was a member function, it really was defined in
4321 the scope of the class. */
4322 && (!DECL_FUNCTION_MEMBER_P (decl
)
4323 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4324 /* We already checked these parameters when the template was
4325 declared, so there's no need to do it again now. This function
4326 was defined in class scope, but we're processing it's body now
4327 that the class is complete. */
4330 /* Core issue 226 (C++0x only): the following only applies to class
4333 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4337 If a template-parameter has a default template-argument, all
4338 subsequent template-parameters shall have a default
4339 template-argument supplied. */
4340 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4342 tree inner_parms
= TREE_VALUE (parm_level
);
4343 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4344 int seen_def_arg_p
= 0;
4347 for (i
= 0; i
< ntparms
; ++i
)
4349 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4351 if (parm
== error_mark_node
)
4354 if (TREE_PURPOSE (parm
))
4356 else if (seen_def_arg_p
4357 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4359 error ("no default argument for %qD", TREE_VALUE (parm
));
4360 /* For better subsequent error-recovery, we indicate that
4361 there should have been a default argument. */
4362 TREE_PURPOSE (parm
) = error_mark_node
;
4365 else if (!is_partial
4367 /* Don't complain about an enclosing partial
4369 && parm_level
== parms
4370 && TREE_CODE (decl
) == TYPE_DECL
4372 && template_parameter_pack_p (TREE_VALUE (parm
)))
4374 /* A primary class template can only have one
4375 parameter pack, at the end of the template
4378 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4379 error ("parameter pack %qE must be at the end of the"
4380 " template parameter list", TREE_VALUE (parm
));
4382 error ("parameter pack %qT must be at the end of the"
4383 " template parameter list",
4384 TREE_TYPE (TREE_VALUE (parm
)));
4386 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4394 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4398 /* For an ordinary class template, default template arguments are
4399 allowed at the innermost level, e.g.:
4400 template <class T = int>
4402 but, in a partial specialization, they're not allowed even
4403 there, as we have in [temp.class.spec]:
4405 The template parameter list of a specialization shall not
4406 contain default template argument values.
4408 So, for a partial specialization, or for a function template
4409 (in C++98/C++03), we look at all of them. */
4412 /* But, for a primary class template that is not a partial
4413 specialization we look at all template parameters except the
4415 parms
= TREE_CHAIN (parms
);
4417 /* Figure out what error message to issue. */
4418 if (is_friend_decl
== 2)
4419 msg
= G_("default template arguments may not be used in function template "
4420 "friend re-declaration");
4421 else if (is_friend_decl
)
4422 msg
= G_("default template arguments may not be used in function template "
4423 "friend declarations");
4424 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4425 msg
= G_("default template arguments may not be used in function templates "
4426 "without -std=c++11 or -std=gnu++11");
4427 else if (is_partial
)
4428 msg
= G_("default template arguments may not be used in "
4429 "partial specializations");
4431 msg
= G_("default argument for template parameter for class enclosing %qD");
4433 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4434 /* If we're inside a class definition, there's no need to
4435 examine the parameters to the class itself. On the one
4436 hand, they will be checked when the class is defined, and,
4437 on the other, default arguments are valid in things like:
4438 template <class T = double>
4439 struct S { template <class U> void f(U); };
4440 Here the default argument for `S' has no bearing on the
4441 declaration of `f'. */
4442 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4444 /* Check everything. */
4445 last_level_to_check
= 0;
4447 for (parm_level
= parms
;
4448 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4449 parm_level
= TREE_CHAIN (parm_level
))
4451 tree inner_parms
= TREE_VALUE (parm_level
);
4455 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4456 for (i
= 0; i
< ntparms
; ++i
)
4458 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4461 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4466 if (is_friend_decl
== 2)
4473 /* Clear out the default argument so that we are not
4475 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4479 /* At this point, if we're still interested in issuing messages,
4480 they must apply to classes surrounding the object declared. */
4482 msg
= G_("default argument for template parameter for class "
4489 /* Worker for push_template_decl_real, called via
4490 for_each_template_parm. DATA is really an int, indicating the
4491 level of the parameters we are interested in. If T is a template
4492 parameter of that level, return nonzero. */
4495 template_parm_this_level_p (tree t
, void* data
)
4497 int this_level
= *(int *)data
;
4500 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4501 level
= TEMPLATE_PARM_LEVEL (t
);
4503 level
= TEMPLATE_TYPE_LEVEL (t
);
4504 return level
== this_level
;
4507 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4508 parameters given by current_template_args, or reuses a
4509 previously existing one, if appropriate. Returns the DECL, or an
4510 equivalent one, if it is replaced via a call to duplicate_decls.
4512 If IS_FRIEND is true, DECL is a friend declaration. */
4515 push_template_decl_real (tree decl
, bool is_friend
)
4523 int new_template_p
= 0;
4524 /* True if the template is a member template, in the sense of
4526 bool member_template_p
= false;
4528 if (decl
== error_mark_node
|| !current_template_parms
)
4529 return error_mark_node
;
4531 /* See if this is a partial specialization. */
4532 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4533 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4534 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4536 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4540 /* For a friend, we want the context of the friend function, not
4541 the type of which it is a friend. */
4542 ctx
= CP_DECL_CONTEXT (decl
);
4543 else if (CP_DECL_CONTEXT (decl
)
4544 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4545 /* In the case of a virtual function, we want the class in which
4547 ctx
= CP_DECL_CONTEXT (decl
);
4549 /* Otherwise, if we're currently defining some class, the DECL
4550 is assumed to be a member of the class. */
4551 ctx
= current_scope ();
4553 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4556 if (!DECL_CONTEXT (decl
))
4557 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4559 /* See if this is a primary template. */
4560 if (is_friend
&& ctx
)
4561 /* A friend template that specifies a class context, i.e.
4562 template <typename T> friend void A<T>::f();
4566 is_primary
= template_parm_scope_p ();
4570 if (DECL_CLASS_SCOPE_P (decl
))
4571 member_template_p
= true;
4572 if (TREE_CODE (decl
) == TYPE_DECL
4573 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4575 error ("template class without a name");
4576 return error_mark_node
;
4578 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4580 if (DECL_DESTRUCTOR_P (decl
))
4584 A destructor shall not be a member template. */
4585 error ("destructor %qD declared as member template", decl
);
4586 return error_mark_node
;
4588 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4589 && (!prototype_p (TREE_TYPE (decl
))
4590 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4591 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4592 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4593 == void_list_node
)))
4595 /* [basic.stc.dynamic.allocation]
4597 An allocation function can be a function
4598 template. ... Template allocation functions shall
4599 have two or more parameters. */
4600 error ("invalid template declaration of %qD", decl
);
4601 return error_mark_node
;
4604 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4605 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4607 else if (TREE_CODE (decl
) == TYPE_DECL
4608 && TYPE_DECL_ALIAS_P (decl
))
4609 /* alias-declaration */
4610 gcc_assert (!DECL_ARTIFICIAL (decl
));
4613 error ("template declaration of %q#D", decl
);
4614 return error_mark_node
;
4618 /* Check to see that the rules regarding the use of default
4619 arguments are not being violated. */
4620 check_default_tmpl_args (decl
, current_template_parms
,
4621 is_primary
, is_partial
, /*is_friend_decl=*/0);
4623 /* Ensure that there are no parameter packs in the type of this
4624 declaration that have not been expanded. */
4625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4627 /* Check each of the arguments individually to see if there are
4628 any bare parameter packs. */
4629 tree type
= TREE_TYPE (decl
);
4630 tree arg
= DECL_ARGUMENTS (decl
);
4631 tree argtype
= TYPE_ARG_TYPES (type
);
4633 while (arg
&& argtype
)
4635 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4636 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4638 /* This is a PARM_DECL that contains unexpanded parameter
4639 packs. We have already complained about this in the
4640 check_for_bare_parameter_packs call, so just replace
4641 these types with ERROR_MARK_NODE. */
4642 TREE_TYPE (arg
) = error_mark_node
;
4643 TREE_VALUE (argtype
) = error_mark_node
;
4646 arg
= DECL_CHAIN (arg
);
4647 argtype
= TREE_CHAIN (argtype
);
4650 /* Check for bare parameter packs in the return type and the
4651 exception specifiers. */
4652 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4653 /* Errors were already issued, set return type to int
4654 as the frontend doesn't expect error_mark_node as
4656 TREE_TYPE (type
) = integer_type_node
;
4657 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4658 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4660 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl
))
4662 ? DECL_ORIGINAL_TYPE (decl
)
4663 : TREE_TYPE (decl
)))
4665 TREE_TYPE (decl
) = error_mark_node
;
4666 return error_mark_node
;
4670 return process_partial_specialization (decl
);
4672 args
= current_template_args ();
4675 || TREE_CODE (ctx
) == FUNCTION_DECL
4676 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4677 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4679 if (DECL_LANG_SPECIFIC (decl
)
4680 && DECL_TEMPLATE_INFO (decl
)
4681 && DECL_TI_TEMPLATE (decl
))
4682 tmpl
= DECL_TI_TEMPLATE (decl
);
4683 /* If DECL is a TYPE_DECL for a class-template, then there won't
4684 be DECL_LANG_SPECIFIC. The information equivalent to
4685 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4686 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4687 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4688 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4690 /* Since a template declaration already existed for this
4691 class-type, we must be redeclaring it here. Make sure
4692 that the redeclaration is valid. */
4693 redeclare_class_template (TREE_TYPE (decl
),
4694 current_template_parms
);
4695 /* We don't need to create a new TEMPLATE_DECL; just use the
4696 one we already had. */
4697 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4701 tmpl
= build_template_decl (decl
, current_template_parms
,
4705 if (DECL_LANG_SPECIFIC (decl
)
4706 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4708 /* A specialization of a member template of a template
4710 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4711 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4712 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4718 tree a
, t
, current
, parms
;
4720 tree tinfo
= get_template_info (decl
);
4724 error ("template definition of non-template %q#D", decl
);
4725 return error_mark_node
;
4728 tmpl
= TI_TEMPLATE (tinfo
);
4730 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4731 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4732 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4733 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4737 /* The declaration is a specialization of a member
4738 template, declared outside the class. Therefore, the
4739 innermost template arguments will be NULL, so we
4740 replace them with the arguments determined by the
4741 earlier call to check_explicit_specialization. */
4742 args
= DECL_TI_ARGS (decl
);
4745 = build_template_decl (decl
, current_template_parms
,
4747 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4748 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4749 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4750 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4751 DECL_TEMPLATE_INFO (new_tmpl
)
4752 = build_template_info (tmpl
, args
);
4754 register_specialization (new_tmpl
,
4755 most_general_template (tmpl
),
4761 /* Make sure the template headers we got make sense. */
4763 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4764 i
= TMPL_PARMS_DEPTH (parms
);
4765 if (TMPL_ARGS_DEPTH (args
) != i
)
4767 error ("expected %d levels of template parms for %q#D, got %d",
4768 i
, decl
, TMPL_ARGS_DEPTH (args
));
4771 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4773 a
= TMPL_ARGS_LEVEL (args
, i
);
4774 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4776 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4778 if (current
== decl
)
4779 error ("got %d template parameters for %q#D",
4780 TREE_VEC_LENGTH (a
), decl
);
4782 error ("got %d template parameters for %q#T",
4783 TREE_VEC_LENGTH (a
), current
);
4784 error (" but %d required", TREE_VEC_LENGTH (t
));
4785 /* Avoid crash in import_export_decl. */
4786 DECL_INTERFACE_KNOWN (decl
) = 1;
4787 return error_mark_node
;
4790 if (current
== decl
)
4792 else if (current
== NULL_TREE
)
4793 /* Can happen in erroneous input. */
4796 current
= (TYPE_P (current
)
4797 ? TYPE_CONTEXT (current
)
4798 : DECL_CONTEXT (current
));
4801 /* Check that the parms are used in the appropriate qualifying scopes
4802 in the declarator. */
4803 if (!comp_template_args
4805 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4808 template arguments to %qD do not match original template %qD",
4809 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4810 if (!uses_template_parms (TI_ARGS (tinfo
)))
4811 inform (input_location
, "use template<> for an explicit specialization");
4812 /* Avoid crash in import_export_decl. */
4813 DECL_INTERFACE_KNOWN (decl
) = 1;
4814 return error_mark_node
;
4818 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4819 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4821 /* Push template declarations for global functions and types. Note
4822 that we do not try to push a global template friend declared in a
4823 template class; such a thing may well depend on the template
4824 parameters of the class. */
4825 if (new_template_p
&& !ctx
4826 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4828 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4829 if (tmpl
== error_mark_node
)
4830 return error_mark_node
;
4832 /* Hide template friend classes that haven't been declared yet. */
4833 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4835 DECL_ANTICIPATED (tmpl
) = 1;
4836 DECL_FRIEND_P (tmpl
) = 1;
4842 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4845 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4846 if (DECL_CONV_FN_P (tmpl
))
4848 int depth
= TMPL_PARMS_DEPTH (parms
);
4850 /* It is a conversion operator. See if the type converted to
4851 depends on innermost template operands. */
4853 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4855 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4858 /* Give template template parms a DECL_CONTEXT of the template
4859 for which they are a parameter. */
4860 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4861 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4863 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4864 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4865 DECL_CONTEXT (parm
) = tmpl
;
4869 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4870 back to its most general template. If TMPL is a specialization,
4871 ARGS may only have the innermost set of arguments. Add the missing
4872 argument levels if necessary. */
4873 if (DECL_TEMPLATE_INFO (tmpl
))
4874 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4876 info
= build_template_info (tmpl
, args
);
4878 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4879 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4882 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4883 retrofit_lang_decl (decl
);
4884 if (DECL_LANG_SPECIFIC (decl
))
4885 DECL_TEMPLATE_INFO (decl
) = info
;
4888 return DECL_TEMPLATE_RESULT (tmpl
);
4892 push_template_decl (tree decl
)
4894 return push_template_decl_real (decl
, false);
4897 /* FN is an inheriting constructor that inherits from the constructor
4898 template INHERITED; turn FN into a constructor template with a matching
4902 add_inherited_template_parms (tree fn
, tree inherited
)
4905 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4906 inner_parms
= copy_node (inner_parms
);
4908 = tree_cons (size_int (processing_template_decl
+ 1),
4909 inner_parms
, current_template_parms
);
4910 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4911 tree args
= template_parms_to_args (parms
);
4912 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4913 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4914 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4915 DECL_ARTIFICIAL (tmpl
) = true;
4916 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4920 /* Called when a class template TYPE is redeclared with the indicated
4921 template PARMS, e.g.:
4923 template <class T> struct S;
4924 template <class T> struct S {}; */
4927 redeclare_class_template (tree type
, tree parms
)
4933 if (!TYPE_TEMPLATE_INFO (type
))
4935 error ("%qT is not a template type", type
);
4939 tmpl
= TYPE_TI_TEMPLATE (type
);
4940 if (!PRIMARY_TEMPLATE_P (tmpl
))
4941 /* The type is nested in some template class. Nothing to worry
4942 about here; there are no new template parameters for the nested
4948 error ("template specifiers not specified in declaration of %qD",
4953 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4954 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4956 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4958 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4959 "redeclared with %d template parameter",
4960 "redeclared with %d template parameters",
4961 TREE_VEC_LENGTH (parms
));
4962 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4963 "previous declaration %q+D used %d template parameter",
4964 "previous declaration %q+D used %d template parameters",
4965 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4969 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4976 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4977 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4980 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4981 if (tmpl_parm
== error_mark_node
)
4984 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4985 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4986 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4988 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4990 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4991 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4992 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4993 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4994 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4995 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4996 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4997 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4998 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5000 error ("template parameter %q+#D", tmpl_parm
);
5001 error ("redeclared here as %q#D", parm
);
5005 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5007 /* We have in [temp.param]:
5009 A template-parameter may not be given default arguments
5010 by two different declarations in the same scope. */
5011 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5012 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5013 "original definition appeared here");
5017 if (parm_default
!= NULL_TREE
)
5018 /* Update the previous template parameters (which are the ones
5019 that will really count) with the new default value. */
5020 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5021 else if (tmpl_default
!= NULL_TREE
)
5022 /* Update the new parameters, too; they'll be used as the
5023 parameters for any members. */
5024 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5030 /* Simplify EXPR if it is a non-dependent expression. Returns the
5031 (possibly simplified) expression. */
5034 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5036 if (expr
== NULL_TREE
)
5039 /* If we're in a template, but EXPR isn't value dependent, simplify
5040 it. We're supposed to treat:
5042 template <typename T> void f(T[1 + 1]);
5043 template <typename T> void f(T[2]);
5045 as two declarations of the same function, for example. */
5046 if (processing_template_decl
5047 && !type_dependent_expression_p (expr
)
5048 && potential_constant_expression (expr
)
5049 && !value_dependent_expression_p (expr
))
5051 HOST_WIDE_INT saved_processing_template_decl
;
5053 saved_processing_template_decl
= processing_template_decl
;
5054 processing_template_decl
= 0;
5055 expr
= tsubst_copy_and_build (expr
,
5058 /*in_decl=*/NULL_TREE
,
5059 /*function_p=*/false,
5060 /*integral_constant_expression_p=*/true);
5061 processing_template_decl
= saved_processing_template_decl
;
5067 fold_non_dependent_expr (tree expr
)
5069 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5072 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5073 template declaration, or a TYPE_DECL for an alias declaration. */
5076 alias_type_or_template_p (tree t
)
5080 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5083 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5084 || DECL_ALIAS_TEMPLATE_P (t
));
5087 /* Return TRUE iff is a specialization of an alias template. */
5090 alias_template_specialization_p (const_tree t
)
5096 && TYPE_TEMPLATE_INFO (t
)
5097 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5098 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5101 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5102 must be a function or a pointer-to-function type, as specified
5103 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5104 and check that the resulting function has external linkage. */
5107 convert_nontype_argument_function (tree type
, tree expr
)
5111 linkage_kind linkage
;
5113 fn
= instantiate_type (type
, fns
, tf_none
);
5114 if (fn
== error_mark_node
)
5115 return error_mark_node
;
5118 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5119 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5120 if (BASELINK_P (fn_no_ptr
))
5121 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5123 /* [temp.arg.nontype]/1
5125 A template-argument for a non-type, non-template template-parameter
5128 -- the address of an object or function with external [C++11: or
5129 internal] linkage. */
5131 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5133 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5134 if (TREE_CODE (type
) == POINTER_TYPE
)
5135 error ("it must be the address of a function with external linkage");
5137 error ("it must be the name of a function with external linkage");
5141 linkage
= decl_linkage (fn_no_ptr
);
5142 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5144 if (cxx_dialect
>= cxx0x
)
5145 error ("%qE is not a valid template argument for type %qT "
5146 "because %qD has no linkage",
5147 expr
, type
, fn_no_ptr
);
5149 error ("%qE is not a valid template argument for type %qT "
5150 "because %qD does not have external linkage",
5151 expr
, type
, fn_no_ptr
);
5158 /* Subroutine of convert_nontype_argument.
5159 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5160 Emit an error otherwise. */
5163 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5164 tsubst_flags_t complain
)
5167 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5169 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5171 if (complain
& tf_error
)
5173 error ("%qE is not a valid template argument for type %qT",
5175 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5180 /* Returns TRUE iff the address of OP is value-dependent.
5182 14.6.2.4 [temp.dep.temp]:
5183 A non-integral non-type template-argument is dependent if its type is
5184 dependent or it has either of the following forms
5187 and contains a nested-name-specifier which specifies a class-name that
5188 names a dependent type.
5190 We generalize this to just say that the address of a member of a
5191 dependent class is value-dependent; the above doesn't cover the
5192 address of a static data member named with an unqualified-id. */
5195 has_value_dependent_address (tree op
)
5197 /* We could use get_inner_reference here, but there's no need;
5198 this is only relevant for template non-type arguments, which
5199 can only be expressed as &id-expression. */
5202 tree ctx
= CP_DECL_CONTEXT (op
);
5203 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5210 /* The next set of functions are used for providing helpful explanatory
5211 diagnostics for failed overload resolution. Their messages should be
5212 indented by two spaces for consistency with the messages in
5216 unify_success (bool /*explain_p*/)
5222 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5225 inform (input_location
,
5226 " couldn't deduce template parameter %qD", parm
);
5231 unify_invalid (bool /*explain_p*/)
5237 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5240 inform (input_location
,
5241 " types %qT and %qT have incompatible cv-qualifiers",
5247 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5250 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5255 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5258 inform (input_location
,
5259 " template parameter %qD is not a parameter pack, but "
5266 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5269 inform (input_location
,
5270 " template argument %qE does not match "
5271 "pointer-to-member constant %qE",
5277 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5280 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5285 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5288 inform (input_location
,
5289 " inconsistent parameter pack deduction with %qT and %qT",
5295 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5300 inform (input_location
,
5301 " deduced conflicting types for parameter %qT (%qT and %qT)",
5302 parm
, first
, second
);
5304 inform (input_location
,
5305 " deduced conflicting values for non-type parameter "
5306 "%qE (%qE and %qE)", parm
, first
, second
);
5312 unify_vla_arg (bool explain_p
, tree arg
)
5315 inform (input_location
,
5316 " variable-sized array type %qT is not "
5317 "a valid template argument",
5323 unify_method_type_error (bool explain_p
, tree arg
)
5326 inform (input_location
,
5327 " member function type %qT is not a valid template argument",
5333 unify_arity (bool explain_p
, int have
, int wanted
)
5336 inform_n (input_location
, wanted
,
5337 " candidate expects %d argument, %d provided",
5338 " candidate expects %d arguments, %d provided",
5344 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5346 return unify_arity (explain_p
, have
, wanted
);
5350 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5352 return unify_arity (explain_p
, have
, wanted
);
5356 unify_arg_conversion (bool explain_p
, tree to_type
,
5357 tree from_type
, tree arg
)
5360 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5361 arg
, from_type
, to_type
);
5366 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5367 tree parm
, tree arg
)
5372 case tbr_ambiguous_baseclass
:
5373 inform (input_location
, " %qT is an ambiguous base class of %qT",
5377 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5384 unify_inconsistent_template_template_parameters (bool explain_p
)
5387 inform (input_location
,
5388 " template parameters of a template template argument are "
5389 "inconsistent with other deduced template arguments");
5394 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5397 inform (input_location
,
5398 " can't deduce a template for %qT from non-template type %qT",
5404 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5407 inform (input_location
,
5408 " template argument %qE does not match %qD", arg
, parm
);
5413 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5416 inform (input_location
,
5417 " could not resolve address from overloaded function %qE",
5422 /* Attempt to convert the non-type template parameter EXPR to the
5423 indicated TYPE. If the conversion is successful, return the
5424 converted value. If the conversion is unsuccessful, return
5425 NULL_TREE if we issued an error message, or error_mark_node if we
5426 did not. We issue error messages for out-and-out bad template
5427 parameters, but not simply because the conversion failed, since we
5428 might be just trying to do argument deduction. Both TYPE and EXPR
5429 must be non-dependent.
5431 The conversion follows the special rules described in
5432 [temp.arg.nontype], and it is much more strict than an implicit
5435 This function is called twice for each template argument (see
5436 lookup_template_class for a more accurate description of this
5437 problem). This means that we need to handle expressions which
5438 are not valid in a C++ source, but can be created from the
5439 first call (for instance, casts to perform conversions). These
5440 hacks can go away after we fix the double coercion problem. */
5443 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5447 /* Detect immediately string literals as invalid non-type argument.
5448 This special-case is not needed for correctness (we would easily
5449 catch this later), but only to provide better diagnostic for this
5450 common user mistake. As suggested by DR 100, we do not mention
5451 linkage issues in the diagnostic as this is not the point. */
5452 /* FIXME we're making this OK. */
5453 if (TREE_CODE (expr
) == STRING_CST
)
5455 if (complain
& tf_error
)
5456 error ("%qE is not a valid template argument for type %qT "
5457 "because string literals can never be used in this context",
5462 /* Add the ADDR_EXPR now for the benefit of
5463 value_dependent_expression_p. */
5464 if (TYPE_PTROBV_P (type
)
5465 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5467 expr
= decay_conversion (expr
, complain
);
5468 if (expr
== error_mark_node
)
5469 return error_mark_node
;
5472 /* If we are in a template, EXPR may be non-dependent, but still
5473 have a syntactic, rather than semantic, form. For example, EXPR
5474 might be a SCOPE_REF, rather than the VAR_DECL to which the
5475 SCOPE_REF refers. Preserving the qualifying scope is necessary
5476 so that access checking can be performed when the template is
5477 instantiated -- but here we need the resolved form so that we can
5478 convert the argument. */
5479 if (TYPE_REF_OBJ_P (type
)
5480 && has_value_dependent_address (expr
))
5481 /* If we want the address and it's value-dependent, don't fold. */;
5482 else if (!type_unknown_p (expr
))
5483 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5484 if (error_operand_p (expr
))
5485 return error_mark_node
;
5486 expr_type
= TREE_TYPE (expr
);
5487 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5488 expr
= mark_lvalue_use (expr
);
5490 expr
= mark_rvalue_use (expr
);
5492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5493 to a non-type argument of "nullptr". */
5494 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5495 expr
= convert (type
, expr
);
5497 /* In C++11, integral or enumeration non-type template arguments can be
5498 arbitrary constant expressions. Pointer and pointer to
5499 member arguments can be general constant expressions that evaluate
5500 to a null value, but otherwise still need to be of a specific form. */
5501 if (cxx_dialect
>= cxx0x
)
5503 if (TREE_CODE (expr
) == PTRMEM_CST
)
5504 /* A PTRMEM_CST is already constant, and a valid template
5505 argument for a parameter of pointer to member type, we just want
5506 to leave it in that form rather than lower it to a
5508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5509 expr
= maybe_constant_value (expr
);
5510 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5512 tree folded
= maybe_constant_value (expr
);
5513 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5514 : null_member_pointer_value_p (folded
))
5519 /* HACK: Due to double coercion, we can get a
5520 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5521 which is the tree that we built on the first call (see
5522 below when coercing to reference to object or to reference to
5523 function). We just strip everything and get to the arg.
5524 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5526 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5528 tree probe_type
, probe
= expr
;
5529 if (REFERENCE_REF_P (probe
))
5530 probe
= TREE_OPERAND (probe
, 0);
5531 probe_type
= TREE_TYPE (probe
);
5532 if (TREE_CODE (probe
) == NOP_EXPR
)
5534 /* ??? Maybe we could use convert_from_reference here, but we
5535 would need to relax its constraints because the NOP_EXPR
5536 could actually change the type to something more cv-qualified,
5537 and this is not folded by convert_from_reference. */
5538 tree addr
= TREE_OPERAND (probe
, 0);
5539 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5540 && TREE_CODE (addr
) == ADDR_EXPR
5541 && TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
5542 && (same_type_ignoring_top_level_qualifiers_p
5543 (TREE_TYPE (probe_type
),
5544 TREE_TYPE (TREE_TYPE (addr
)))))
5546 expr
= TREE_OPERAND (addr
, 0);
5547 expr_type
= TREE_TYPE (expr
);
5552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5553 parameter is a pointer to object, through decay and
5554 qualification conversion. Let's strip everything. */
5555 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5558 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5559 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5560 /* Skip the ADDR_EXPR only if it is part of the decay for
5561 an array. Otherwise, it is part of the original argument
5562 in the source code. */
5563 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5564 expr
= TREE_OPERAND (expr
, 0);
5565 expr_type
= TREE_TYPE (expr
);
5568 /* [temp.arg.nontype]/5, bullet 1
5570 For a non-type template-parameter of integral or enumeration type,
5571 integral promotions (_conv.prom_) and integral conversions
5572 (_conv.integral_) are applied. */
5573 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5575 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5576 t
= maybe_constant_value (t
);
5577 if (t
!= error_mark_node
)
5580 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5581 return error_mark_node
;
5583 /* Notice that there are constant expressions like '4 % 0' which
5584 do not fold into integer constants. */
5585 if (TREE_CODE (expr
) != INTEGER_CST
)
5587 if (complain
& tf_error
)
5589 int errs
= errorcount
, warns
= warningcount
;
5590 if (processing_template_decl
5591 && !require_potential_constant_expression (expr
))
5593 expr
= cxx_constant_value (expr
);
5594 if (errorcount
> errs
|| warningcount
> warns
)
5595 inform (EXPR_LOC_OR_HERE (expr
),
5596 "in template argument for type %qT ", type
);
5597 if (expr
== error_mark_node
)
5599 /* else cxx_constant_value complained but gave us
5600 a real constant, so go ahead. */
5601 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5607 /* [temp.arg.nontype]/5, bullet 2
5609 For a non-type template-parameter of type pointer to object,
5610 qualification conversions (_conv.qual_) and the array-to-pointer
5611 conversion (_conv.array_) are applied. */
5612 else if (TYPE_PTROBV_P (type
))
5614 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5616 A template-argument for a non-type, non-template template-parameter
5617 shall be one of: [...]
5619 -- the name of a non-type template-parameter;
5620 -- the address of an object or function with external linkage, [...]
5621 expressed as "& id-expression" where the & is optional if the name
5622 refers to a function or array, or if the corresponding
5623 template-parameter is a reference.
5625 Here, we do not care about functions, as they are invalid anyway
5626 for a parameter of type pointer-to-object. */
5628 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5629 /* Non-type template parameters are OK. */
5631 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5632 /* Null pointer values are OK in C++11. */;
5633 else if (TREE_CODE (expr
) != ADDR_EXPR
5634 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5636 if (TREE_CODE (expr
) == VAR_DECL
)
5638 error ("%qD is not a valid template argument "
5639 "because %qD is a variable, not the address of "
5644 /* Other values, like integer constants, might be valid
5645 non-type arguments of some other type. */
5646 return error_mark_node
;
5652 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5653 ? TREE_OPERAND (expr
, 0) : expr
);
5654 if (TREE_CODE (decl
) != VAR_DECL
)
5656 error ("%qE is not a valid template argument of type %qT "
5657 "because %qE is not a variable",
5661 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5663 error ("%qE is not a valid template argument of type %qT "
5664 "because %qD does not have external linkage",
5668 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5670 error ("%qE is not a valid template argument of type %qT "
5671 "because %qD has no linkage",
5677 expr
= decay_conversion (expr
, complain
);
5678 if (expr
== error_mark_node
)
5679 return error_mark_node
;
5681 expr
= perform_qualification_conversions (type
, expr
);
5682 if (expr
== error_mark_node
)
5683 return error_mark_node
;
5685 /* [temp.arg.nontype]/5, bullet 3
5687 For a non-type template-parameter of type reference to object, no
5688 conversions apply. The type referred to by the reference may be more
5689 cv-qualified than the (otherwise identical) type of the
5690 template-argument. The template-parameter is bound directly to the
5691 template-argument, which must be an lvalue. */
5692 else if (TYPE_REF_OBJ_P (type
))
5694 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5696 return error_mark_node
;
5698 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5700 error ("%qE is not a valid template argument for type %qT "
5701 "because of conflicts in cv-qualification", expr
, type
);
5705 if (!real_lvalue_p (expr
))
5707 error ("%qE is not a valid template argument for type %qT "
5708 "because it is not an lvalue", expr
, type
);
5712 /* [temp.arg.nontype]/1
5714 A template-argument for a non-type, non-template template-parameter
5715 shall be one of: [...]
5717 -- the address of an object or function with external linkage. */
5718 if (TREE_CODE (expr
) == INDIRECT_REF
5719 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5721 expr
= TREE_OPERAND (expr
, 0);
5724 error ("%q#D is not a valid template argument for type %qT "
5725 "because a reference variable does not have a constant "
5726 "address", expr
, type
);
5733 error ("%qE is not a valid template argument for type %qT "
5734 "because it is not an object with external linkage",
5739 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5741 error ("%qE is not a valid template argument for type %qT "
5742 "because object %qD has not external linkage",
5747 expr
= build_nop (type
, build_address (expr
));
5749 /* [temp.arg.nontype]/5, bullet 4
5751 For a non-type template-parameter of type pointer to function, only
5752 the function-to-pointer conversion (_conv.func_) is applied. If the
5753 template-argument represents a set of overloaded functions (or a
5754 pointer to such), the matching function is selected from the set
5756 else if (TYPE_PTRFN_P (type
))
5758 /* If the argument is a template-id, we might not have enough
5759 context information to decay the pointer. */
5760 if (!type_unknown_p (expr_type
))
5762 expr
= decay_conversion (expr
, complain
);
5763 if (expr
== error_mark_node
)
5764 return error_mark_node
;
5767 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5768 /* Null pointer values are OK in C++11. */
5769 return perform_qualification_conversions (type
, expr
);
5771 expr
= convert_nontype_argument_function (type
, expr
);
5772 if (!expr
|| expr
== error_mark_node
)
5775 /* [temp.arg.nontype]/5, bullet 5
5777 For a non-type template-parameter of type reference to function, no
5778 conversions apply. If the template-argument represents a set of
5779 overloaded functions, the matching function is selected from the set
5781 else if (TYPE_REFFN_P (type
))
5783 if (TREE_CODE (expr
) == ADDR_EXPR
)
5785 error ("%qE is not a valid template argument for type %qT "
5786 "because it is a pointer", expr
, type
);
5787 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5791 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5792 if (!expr
|| expr
== error_mark_node
)
5795 expr
= build_nop (type
, build_address (expr
));
5797 /* [temp.arg.nontype]/5, bullet 6
5799 For a non-type template-parameter of type pointer to member function,
5800 no conversions apply. If the template-argument represents a set of
5801 overloaded member functions, the matching member function is selected
5802 from the set (_over.over_). */
5803 else if (TYPE_PTRMEMFUNC_P (type
))
5805 expr
= instantiate_type (type
, expr
, tf_none
);
5806 if (expr
== error_mark_node
)
5807 return error_mark_node
;
5809 /* [temp.arg.nontype] bullet 1 says the pointer to member
5810 expression must be a pointer-to-member constant. */
5811 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5812 return error_mark_node
;
5814 /* There is no way to disable standard conversions in
5815 resolve_address_of_overloaded_function (called by
5816 instantiate_type). It is possible that the call succeeded by
5817 converting &B::I to &D::I (where B is a base of D), so we need
5818 to reject this conversion here.
5820 Actually, even if there was a way to disable standard conversions,
5821 it would still be better to reject them here so that we can
5822 provide a superior diagnostic. */
5823 if (!same_type_p (TREE_TYPE (expr
), type
))
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because it is of type %qT", expr
, type
,
5828 /* If we are just one standard conversion off, explain. */
5829 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5830 inform (input_location
,
5831 "standard conversions are not allowed in this context");
5835 /* [temp.arg.nontype]/5, bullet 7
5837 For a non-type template-parameter of type pointer to data member,
5838 qualification conversions (_conv.qual_) are applied. */
5839 else if (TYPE_PTRDATAMEM_P (type
))
5841 /* [temp.arg.nontype] bullet 1 says the pointer to member
5842 expression must be a pointer-to-member constant. */
5843 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5844 return error_mark_node
;
5846 expr
= perform_qualification_conversions (type
, expr
);
5847 if (expr
== error_mark_node
)
5850 else if (NULLPTR_TYPE_P (type
))
5852 if (expr
!= nullptr_node
)
5854 error ("%qE is not a valid template argument for type %qT "
5855 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5860 /* A template non-type parameter must be one of the above. */
5864 /* Sanity check: did we actually convert the argument to the
5866 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5867 (type
, TREE_TYPE (expr
)));
5871 /* Subroutine of coerce_template_template_parms, which returns 1 if
5872 PARM_PARM and ARG_PARM match using the rule for the template
5873 parameters of template template parameters. Both PARM and ARG are
5874 template parameters; the rest of the arguments are the same as for
5875 coerce_template_template_parms.
5878 coerce_template_template_parm (tree parm
,
5880 tsubst_flags_t complain
,
5884 if (arg
== NULL_TREE
|| arg
== error_mark_node
5885 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5888 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5891 switch (TREE_CODE (parm
))
5894 /* We encounter instantiations of templates like
5895 template <template <template <class> class> class TT>
5898 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5899 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5901 if (!coerce_template_template_parms
5902 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5908 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5909 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5910 /* Argument is a parameter pack but parameter is not. */
5915 /* The tsubst call is used to handle cases such as
5917 template <int> class C {};
5918 template <class T, template <T> class TT> class D {};
5921 i.e. the parameter list of TT depends on earlier parameters. */
5922 if (!uses_template_parms (TREE_TYPE (arg
))
5924 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5928 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5929 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5930 /* Argument is a parameter pack but parameter is not. */
5943 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5944 template template parameters. Both PARM_PARMS and ARG_PARMS are
5945 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5948 Consider the example:
5949 template <class T> class A;
5950 template<template <class U> class TT> class B;
5952 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5953 the parameters to A, and OUTER_ARGS contains A. */
5956 coerce_template_template_parms (tree parm_parms
,
5958 tsubst_flags_t complain
,
5962 int nparms
, nargs
, i
;
5966 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5967 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5969 nparms
= TREE_VEC_LENGTH (parm_parms
);
5970 nargs
= TREE_VEC_LENGTH (arg_parms
);
5972 /* Determine whether we have a parameter pack at the end of the
5973 template template parameter's template parameter list. */
5974 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5976 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5978 if (parm
== error_mark_node
)
5981 switch (TREE_CODE (parm
))
5985 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5990 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6000 && !(variadic_p
&& nargs
>= nparms
- 1))
6003 /* Check all of the template parameters except the parameter pack at
6004 the end (if any). */
6005 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6007 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6008 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6011 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6012 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6014 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6022 /* Check each of the template parameters in the template
6023 argument against the template parameter pack at the end of
6024 the template template parameter. */
6025 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6028 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6030 for (; i
< nargs
; ++i
)
6032 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6035 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6037 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6046 /* Verifies that the deduced template arguments (in TARGS) for the
6047 template template parameters (in TPARMS) represent valid bindings,
6048 by comparing the template parameter list of each template argument
6049 to the template parameter list of its corresponding template
6050 template parameter, in accordance with DR150. This
6051 routine can only be called after all template arguments have been
6052 deduced. It will return TRUE if all of the template template
6053 parameter bindings are okay, FALSE otherwise. */
6055 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6057 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6060 /* We're dealing with template parms in this process. */
6061 ++processing_template_decl
;
6063 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6065 for (i
= 0; i
< ntparms
; ++i
)
6067 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6068 tree targ
= TREE_VEC_ELT (targs
, i
);
6070 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6072 tree packed_args
= NULL_TREE
;
6075 if (ARGUMENT_PACK_P (targ
))
6077 /* Look inside the argument pack. */
6078 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6079 len
= TREE_VEC_LENGTH (packed_args
);
6082 for (idx
= 0; idx
< len
; ++idx
)
6084 tree targ_parms
= NULL_TREE
;
6087 /* Extract the next argument from the argument
6089 targ
= TREE_VEC_ELT (packed_args
, idx
);
6091 if (PACK_EXPANSION_P (targ
))
6092 /* Look at the pattern of the pack expansion. */
6093 targ
= PACK_EXPANSION_PATTERN (targ
);
6095 /* Extract the template parameters from the template
6097 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6098 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6099 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6100 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6102 /* Verify that we can coerce the template template
6103 parameters from the template argument to the template
6104 parameter. This requires an exact match. */
6106 && !coerce_template_template_parms
6107 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6122 --processing_template_decl
;
6126 /* Since type attributes aren't mangled, we need to strip them from
6127 template type arguments. */
6130 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6133 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6135 mv
= TYPE_MAIN_VARIANT (arg
);
6136 arg
= strip_typedefs (arg
);
6137 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6138 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6140 if (complain
& tf_warning
)
6141 warning (0, "ignoring attributes on template argument %qT", arg
);
6142 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6143 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6148 /* Convert the indicated template ARG as necessary to match the
6149 indicated template PARM. Returns the converted ARG, or
6150 error_mark_node if the conversion was unsuccessful. Error and
6151 warning messages are issued under control of COMPLAIN. This
6152 conversion is for the Ith parameter in the parameter list. ARGS is
6153 the full set of template arguments deduced so far. */
6156 convert_template_argument (tree parm
,
6159 tsubst_flags_t complain
,
6165 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6167 if (TREE_CODE (arg
) == TREE_LIST
6168 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6170 /* The template argument was the name of some
6171 member function. That's usually
6172 invalid, but static members are OK. In any
6173 case, grab the underlying fields/functions
6174 and issue an error later if required. */
6175 orig_arg
= TREE_VALUE (arg
);
6176 TREE_TYPE (arg
) = unknown_type_node
;
6181 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6182 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6183 || requires_tmpl_type
);
6185 /* When determining whether an argument pack expansion is a template,
6186 look at the pattern. */
6187 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6188 arg
= PACK_EXPANSION_PATTERN (arg
);
6190 /* Deal with an injected-class-name used as a template template arg. */
6191 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6193 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6194 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6196 if (cxx_dialect
>= cxx0x
)
6197 /* OK under DR 1004. */;
6198 else if (complain
& tf_warning_or_error
)
6199 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6200 " used as template template argument", TYPE_NAME (arg
));
6201 else if (flag_pedantic_errors
)
6209 ((TREE_CODE (arg
) == TEMPLATE_DECL
6210 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6211 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6212 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6213 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6216 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6217 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6218 arg
= TYPE_STUB_DECL (arg
);
6220 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6222 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6223 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6225 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6227 if (complain
& tf_error
)
6228 error ("invalid use of destructor %qE as a type", orig_arg
);
6229 return error_mark_node
;
6232 permerror (input_location
,
6233 "to refer to a type member of a template parameter, "
6234 "use %<typename %E%>", orig_arg
);
6236 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6237 TREE_OPERAND (arg
, 1),
6243 if (is_type
!= requires_type
)
6247 if (complain
& tf_error
)
6249 error ("type/value mismatch at argument %d in template "
6250 "parameter list for %qD",
6253 error (" expected a constant of type %qT, got %qT",
6255 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6256 else if (requires_tmpl_type
)
6257 error (" expected a class template, got %qE", orig_arg
);
6259 error (" expected a type, got %qE", orig_arg
);
6262 return error_mark_node
;
6264 if (is_tmpl_type
^ requires_tmpl_type
)
6266 if (in_decl
&& (complain
& tf_error
))
6268 error ("type/value mismatch at argument %d in template "
6269 "parameter list for %qD",
6272 error (" expected a type, got %qT", DECL_NAME (arg
));
6274 error (" expected a class template, got %qT", orig_arg
);
6276 return error_mark_node
;
6281 if (requires_tmpl_type
)
6283 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6285 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6286 /* The number of argument required is not known yet.
6287 Just accept it for now. */
6288 val
= TREE_TYPE (arg
);
6291 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6294 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6296 if (coerce_template_template_parms (parmparm
, argparm
,
6302 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6304 if (val
!= error_mark_node
)
6306 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6307 val
= TREE_TYPE (val
);
6308 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6309 val
= make_pack_expansion (val
);
6314 if (in_decl
&& (complain
& tf_error
))
6316 error ("type/value mismatch at argument %d in "
6317 "template parameter list for %qD",
6319 error (" expected a template of type %qD, got %qT",
6323 val
= error_mark_node
;
6329 /* We only form one instance of each template specialization.
6330 Therefore, if we use a non-canonical variant (i.e., a
6331 typedef), any future messages referring to the type will use
6332 the typedef, which is confusing if those future uses do not
6333 themselves also use the typedef. */
6335 val
= canonicalize_type_argument (val
, complain
);
6339 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6341 if (invalid_nontype_parm_type_p (t
, complain
))
6342 return error_mark_node
;
6344 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6346 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6350 /* Not sure if this is reachable, but it doesn't hurt
6352 error ("type mismatch in nontype parameter pack");
6353 val
= error_mark_node
;
6356 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6357 /* We used to call digest_init here. However, digest_init
6358 will report errors, which we don't want when complain
6359 is zero. More importantly, digest_init will try too
6360 hard to convert things: for example, `0' should not be
6361 converted to pointer type at this point according to
6362 the standard. Accepting this is not merely an
6363 extension, since deciding whether or not these
6364 conversions can occur is part of determining which
6365 function template to call, or whether a given explicit
6366 argument specification is valid. */
6367 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6369 val
= strip_typedefs_expr (orig_arg
);
6371 if (val
== NULL_TREE
)
6372 val
= error_mark_node
;
6373 else if (val
== error_mark_node
&& (complain
& tf_error
))
6374 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6376 if (TREE_CODE (val
) == SCOPE_REF
)
6378 /* Strip typedefs from the SCOPE_REF. */
6379 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6380 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6382 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6383 QUALIFIED_NAME_IS_TEMPLATE (val
));
6390 /* Coerces the remaining template arguments in INNER_ARGS (from
6391 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6392 Returns the coerced argument pack. PARM_IDX is the position of this
6393 parameter in the template parameter list. ARGS is the original
6394 template argument list. */
6396 coerce_template_parameter_pack (tree parms
,
6404 tsubst_flags_t complain
)
6406 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6407 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6410 tree packed_types
= NULL_TREE
;
6412 if (arg_idx
> nargs
)
6415 packed_args
= make_tree_vec (nargs
- arg_idx
);
6417 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6418 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6420 /* When the template parameter is a non-type template
6421 parameter pack whose type uses parameter packs, we need
6422 to look at each of the template arguments
6423 separately. Build a vector of the types for these
6424 non-type template parameters in PACKED_TYPES. */
6426 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6427 packed_types
= tsubst_pack_expansion (expansion
, args
,
6430 if (packed_types
== error_mark_node
)
6431 return error_mark_node
;
6433 /* Check that we have the right number of arguments. */
6435 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6436 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6439 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6440 error ("wrong number of template arguments (%d, should be %d)",
6441 nargs
, needed_parms
);
6442 return error_mark_node
;
6445 /* If we aren't able to check the actual arguments now
6446 (because they haven't been expanded yet), we can at least
6447 verify that all of the types used for the non-type
6448 template parameter pack are, in fact, valid for non-type
6449 template parameters. */
6451 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6453 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6454 for (j
= 0; j
< len
; ++j
)
6456 tree t
= TREE_VEC_ELT (packed_types
, j
);
6457 if (invalid_nontype_parm_type_p (t
, complain
))
6458 return error_mark_node
;
6463 /* Convert the remaining arguments, which will be a part of the
6464 parameter pack "parm". */
6465 for (; arg_idx
< nargs
; ++arg_idx
)
6467 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6468 tree actual_parm
= TREE_VALUE (parm
);
6470 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6472 /* When we have a vector of types (corresponding to the
6473 non-type template parameter pack that uses parameter
6474 packs in its type, as mention above), and the
6475 argument is not an expansion (which expands to a
6476 currently unknown number of arguments), clone the
6477 parm and give it the next type in PACKED_TYPES. */
6478 actual_parm
= copy_node (actual_parm
);
6479 TREE_TYPE (actual_parm
) =
6480 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6483 if (arg
== error_mark_node
)
6485 if (complain
& tf_error
)
6486 error ("template argument %d is invalid", arg_idx
+ 1);
6489 arg
= convert_template_argument (actual_parm
,
6490 arg
, new_args
, complain
, parm_idx
,
6492 if (arg
== error_mark_node
)
6494 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6497 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6498 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6499 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6502 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6503 TREE_TYPE (argument_pack
)
6504 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6505 TREE_CONSTANT (argument_pack
) = 1;
6508 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6509 #ifdef ENABLE_CHECKING
6510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6511 TREE_VEC_LENGTH (packed_args
));
6513 return argument_pack
;
6516 /* Returns true if the template argument vector ARGS contains
6517 any pack expansions, false otherwise. */
6520 any_pack_expanson_args_p (tree args
)
6524 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6525 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6530 /* Convert all template arguments to their appropriate types, and
6531 return a vector containing the innermost resulting template
6532 arguments. If any error occurs, return error_mark_node. Error and
6533 warning messages are issued under control of COMPLAIN.
6535 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6536 for arguments not specified in ARGS. Otherwise, if
6537 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6538 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6539 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6543 coerce_template_parms (tree parms
,
6546 tsubst_flags_t complain
,
6547 bool require_all_args
,
6548 bool use_default_args
)
6550 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6553 tree new_inner_args
;
6554 int saved_unevaluated_operand
;
6555 int saved_inhibit_evaluation_warnings
;
6557 /* When used as a boolean value, indicates whether this is a
6558 variadic template parameter list. Since it's an int, we can also
6559 subtract it from nparms to get the number of non-variadic
6562 int post_variadic_parms
= 0;
6564 if (args
== error_mark_node
)
6565 return error_mark_node
;
6567 nparms
= TREE_VEC_LENGTH (parms
);
6569 /* Determine if there are any parameter packs. */
6570 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6572 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6574 ++post_variadic_parms
;
6575 if (template_parameter_pack_p (tparm
))
6579 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6580 /* If there are no parameters that follow a parameter pack, we need to
6581 expand any argument packs so that we can deduce a parameter pack from
6582 some non-packed args followed by an argument pack, as in variadic85.C.
6583 If there are such parameters, we need to leave argument packs intact
6584 so the arguments are assigned properly. This can happen when dealing
6585 with a nested class inside a partial specialization of a class
6586 template, as in variadic92.C, or when deducing a template parameter pack
6587 from a sub-declarator, as in variadic114.C. */
6588 if (!post_variadic_parms
)
6589 inner_args
= expand_template_argument_pack (inner_args
);
6591 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6592 if ((nargs
> nparms
&& !variadic_p
)
6593 || (nargs
< nparms
- variadic_p
6595 && !any_pack_expanson_args_p (inner_args
)
6596 && (!use_default_args
6597 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6598 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6600 if (complain
& tf_error
)
6604 nparms
-= variadic_p
;
6605 error ("wrong number of template arguments "
6606 "(%d, should be %d or more)", nargs
, nparms
);
6609 error ("wrong number of template arguments "
6610 "(%d, should be %d)", nargs
, nparms
);
6613 error ("provided for %q+D", in_decl
);
6616 return error_mark_node
;
6619 /* We need to evaluate the template arguments, even though this
6620 template-id may be nested within a "sizeof". */
6621 saved_unevaluated_operand
= cp_unevaluated_operand
;
6622 cp_unevaluated_operand
= 0;
6623 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6624 c_inhibit_evaluation_warnings
= 0;
6625 new_inner_args
= make_tree_vec (nparms
);
6626 new_args
= add_outermost_template_args (args
, new_inner_args
);
6627 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6632 /* Get the Ith template parameter. */
6633 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6635 if (parm
== error_mark_node
)
6637 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6641 /* Calculate the next argument. */
6642 if (arg_idx
< nargs
)
6643 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6647 if (template_parameter_pack_p (TREE_VALUE (parm
))
6648 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6650 /* All remaining arguments will be placed in the
6651 template parameter pack PARM. */
6652 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6653 inner_args
, arg_idx
,
6657 /* Store this argument. */
6658 if (arg
== error_mark_node
)
6660 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6662 /* We are done with all of the arguments. */
6669 if (PACK_EXPANSION_P (arg
))
6671 /* We don't know how many args we have yet, just
6672 use the unconverted ones for now. */
6673 new_inner_args
= inner_args
;
6677 else if (require_all_args
)
6679 /* There must be a default arg in this case. */
6680 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6682 /* The position of the first default template argument,
6683 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6685 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6691 if (arg
== error_mark_node
)
6693 if (complain
& tf_error
)
6694 error ("template argument %d is invalid", arg_idx
+ 1);
6697 /* This only occurs if there was an error in the template
6698 parameter list itself (which we would already have
6699 reported) that we are trying to recover from, e.g., a class
6700 template with a parameter list such as
6701 template<typename..., typename>. */
6704 arg
= convert_template_argument (TREE_VALUE (parm
),
6705 arg
, new_args
, complain
,
6708 if (arg
== error_mark_node
)
6710 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6712 cp_unevaluated_operand
= saved_unevaluated_operand
;
6713 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6716 return error_mark_node
;
6718 #ifdef ENABLE_CHECKING
6719 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6721 TREE_VEC_LENGTH (new_inner_args
));
6724 return new_inner_args
;
6727 /* Like coerce_template_parms. If PARMS represents all template
6728 parameters levels, this function returns a vector of vectors
6729 representing all the resulting argument levels. Note that in this
6730 case, only the innermost arguments are coerced because the
6731 outermost ones are supposed to have been coerced already.
6733 Otherwise, if PARMS represents only (the innermost) vector of
6734 parameters, this function returns a vector containing just the
6735 innermost resulting arguments. */
6738 coerce_innermost_template_parms (tree parms
,
6741 tsubst_flags_t complain
,
6742 bool require_all_args
,
6743 bool use_default_args
)
6745 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6746 int args_depth
= TMPL_ARGS_DEPTH (args
);
6749 if (parms_depth
> 1)
6751 coerced_args
= make_tree_vec (parms_depth
);
6755 for (level
= parms
, cur_depth
= parms_depth
;
6756 parms_depth
> 0 && level
!= NULL_TREE
;
6757 level
= TREE_CHAIN (level
), --cur_depth
)
6760 if (cur_depth
== args_depth
)
6761 l
= coerce_template_parms (TREE_VALUE (level
),
6762 args
, in_decl
, complain
,
6766 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6768 if (l
== error_mark_node
)
6769 return error_mark_node
;
6771 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6775 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6776 args
, in_decl
, complain
,
6779 return coerced_args
;
6782 /* Returns 1 if template args OT and NT are equivalent. */
6785 template_args_equal (tree ot
, tree nt
)
6789 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6792 if (TREE_CODE (nt
) == TREE_VEC
)
6793 /* For member templates */
6794 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6795 else if (PACK_EXPANSION_P (ot
))
6796 return (PACK_EXPANSION_P (nt
)
6797 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6798 PACK_EXPANSION_PATTERN (nt
))
6799 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6800 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6801 else if (ARGUMENT_PACK_P (ot
))
6806 if (!ARGUMENT_PACK_P (nt
))
6809 opack
= ARGUMENT_PACK_ARGS (ot
);
6810 npack
= ARGUMENT_PACK_ARGS (nt
);
6811 len
= TREE_VEC_LENGTH (opack
);
6812 if (TREE_VEC_LENGTH (npack
) != len
)
6814 for (i
= 0; i
< len
; ++i
)
6815 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6816 TREE_VEC_ELT (npack
, i
)))
6820 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6822 /* We get here probably because we are in the middle of substituting
6823 into the pattern of a pack expansion. In that case the
6824 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6825 interested in. So we want to use the initial pack argument for
6827 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6828 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6829 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6830 return template_args_equal (ot
, nt
);
6832 else if (TYPE_P (nt
))
6833 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6834 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6837 return cp_tree_equal (ot
, nt
);
6840 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6841 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6842 NEWARG_PTR with the offending arguments if they are non-NULL. */
6845 comp_template_args_with_info (tree oldargs
, tree newargs
,
6846 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6850 if (oldargs
== newargs
)
6853 if (!oldargs
|| !newargs
)
6856 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6859 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6861 tree nt
= TREE_VEC_ELT (newargs
, i
);
6862 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6864 if (! template_args_equal (ot
, nt
))
6866 if (oldarg_ptr
!= NULL
)
6868 if (newarg_ptr
!= NULL
)
6876 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6877 of template arguments. Returns 0 otherwise. */
6880 comp_template_args (tree oldargs
, tree newargs
)
6882 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6886 add_pending_template (tree d
)
6888 tree ti
= (TYPE_P (d
)
6889 ? CLASSTYPE_TEMPLATE_INFO (d
)
6890 : DECL_TEMPLATE_INFO (d
));
6891 struct pending_template
*pt
;
6894 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6897 /* We are called both from instantiate_decl, where we've already had a
6898 tinst_level pushed, and instantiate_template, where we haven't.
6900 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6903 push_tinst_level (d
);
6905 pt
= ggc_alloc_pending_template ();
6907 pt
->tinst
= current_tinst_level
;
6908 if (last_pending_template
)
6909 last_pending_template
->next
= pt
;
6911 pending_templates
= pt
;
6913 last_pending_template
= pt
;
6915 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6922 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6923 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6924 documentation for TEMPLATE_ID_EXPR. */
6927 lookup_template_function (tree fns
, tree arglist
)
6931 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6932 return error_mark_node
;
6934 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6936 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6938 error ("%q#D is not a function template", fns
);
6939 return error_mark_node
;
6942 if (BASELINK_P (fns
))
6944 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6946 BASELINK_FUNCTIONS (fns
),
6951 type
= TREE_TYPE (fns
);
6952 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6953 type
= unknown_type_node
;
6955 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6958 /* Within the scope of a template class S<T>, the name S gets bound
6959 (in build_self_reference) to a TYPE_DECL for the class, not a
6960 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6961 or one of its enclosing classes, and that type is a template,
6962 return the associated TEMPLATE_DECL. Otherwise, the original
6965 Also handle the case when DECL is a TREE_LIST of ambiguous
6966 injected-class-names from different bases. */
6969 maybe_get_template_decl_from_type_decl (tree decl
)
6971 if (decl
== NULL_TREE
)
6974 /* DR 176: A lookup that finds an injected-class-name (10.2
6975 [class.member.lookup]) can result in an ambiguity in certain cases
6976 (for example, if it is found in more than one base class). If all of
6977 the injected-class-names that are found refer to specializations of
6978 the same class template, and if the name is followed by a
6979 template-argument-list, the reference refers to the class template
6980 itself and not a specialization thereof, and is not ambiguous. */
6981 if (TREE_CODE (decl
) == TREE_LIST
)
6983 tree t
, tmpl
= NULL_TREE
;
6984 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6986 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6989 else if (tmpl
!= elt
)
6992 if (tmpl
&& t
== NULL_TREE
)
6998 return (decl
!= NULL_TREE
6999 && DECL_SELF_REFERENCE_P (decl
)
7000 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7001 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7004 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7005 parameters, find the desired type.
7007 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7009 IN_DECL, if non-NULL, is the template declaration we are trying to
7012 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7013 the class we are looking up.
7015 Issue error and warning messages under control of COMPLAIN.
7017 If the template class is really a local class in a template
7018 function, then the FUNCTION_CONTEXT is the function in which it is
7021 ??? Note that this function is currently called *twice* for each
7022 template-id: the first time from the parser, while creating the
7023 incomplete type (finish_template_type), and the second type during the
7024 real instantiation (instantiate_template_class). This is surely something
7025 that we want to avoid. It also causes some problems with argument
7026 coercion (see convert_nontype_argument for more information on this). */
7029 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7030 int entering_scope
, tsubst_flags_t complain
)
7032 tree templ
= NULL_TREE
, parmlist
;
7039 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7041 tree value
= innermost_non_namespace_value (d1
);
7042 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7047 push_decl_namespace (context
);
7048 templ
= lookup_name (d1
);
7049 templ
= maybe_get_template_decl_from_type_decl (templ
);
7051 pop_decl_namespace ();
7054 context
= DECL_CONTEXT (templ
);
7056 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7058 tree type
= TREE_TYPE (d1
);
7060 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7061 an implicit typename for the second A. Deal with it. */
7062 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7063 type
= TREE_TYPE (type
);
7065 if (CLASSTYPE_TEMPLATE_INFO (type
))
7067 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7068 d1
= DECL_NAME (templ
);
7071 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7072 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7074 templ
= TYPE_TI_TEMPLATE (d1
);
7075 d1
= DECL_NAME (templ
);
7077 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7078 && DECL_TEMPLATE_RESULT (d1
)
7079 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7082 d1
= DECL_NAME (templ
);
7083 context
= DECL_CONTEXT (templ
);
7086 /* Issue an error message if we didn't find a template. */
7089 if (complain
& tf_error
)
7090 error ("%qT is not a template", d1
);
7091 return error_mark_node
;
7094 if (TREE_CODE (templ
) != TEMPLATE_DECL
7095 /* Make sure it's a user visible template, if it was named by
7097 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7098 && !PRIMARY_TEMPLATE_P (templ
)))
7100 if (complain
& tf_error
)
7102 error ("non-template type %qT used as a template", d1
);
7104 error ("for template declaration %q+D", in_decl
);
7106 return error_mark_node
;
7109 complain
&= ~tf_user
;
7111 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7113 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7114 template arguments */
7120 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7122 /* Consider an example where a template template parameter declared as
7124 template <class T, class U = std::allocator<T> > class TT
7126 The template parameter level of T and U are one level larger than
7127 of TT. To proper process the default argument of U, say when an
7128 instantiation `TT<int>' is seen, we need to build the full
7129 arguments containing {int} as the innermost level. Outer levels,
7130 available when not appearing as default template argument, can be
7131 obtained from the arguments of the enclosing template.
7133 Suppose that TT is later substituted with std::vector. The above
7134 instantiation is `TT<int, std::allocator<T> >' with TT at
7135 level 1, and T at level 2, while the template arguments at level 1
7136 becomes {std::vector} and the inner level 2 is {int}. */
7138 outer
= DECL_CONTEXT (templ
);
7140 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7141 else if (current_template_parms
)
7142 /* This is an argument of the current template, so we haven't set
7143 DECL_CONTEXT yet. */
7144 outer
= current_template_args ();
7147 arglist
= add_to_template_args (outer
, arglist
);
7149 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7151 /*require_all_args=*/true,
7152 /*use_default_args=*/true);
7153 if (arglist2
== error_mark_node
7154 || (!uses_template_parms (arglist2
)
7155 && check_instantiated_args (templ
, arglist2
, complain
)))
7156 return error_mark_node
;
7158 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7163 tree template_type
= TREE_TYPE (templ
);
7166 tree found
= NULL_TREE
;
7169 int is_dependent_type
;
7170 int use_partial_inst_tmpl
= false;
7172 if (template_type
== error_mark_node
)
7173 /* An error occured while building the template TEMPL, and a
7174 diagnostic has most certainly been emitted for that
7175 already. Let's propagate that error. */
7176 return error_mark_node
;
7178 gen_tmpl
= most_general_template (templ
);
7179 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7180 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7181 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7183 if (arg_depth
== 1 && parm_depth
> 1)
7185 /* We've been given an incomplete set of template arguments.
7188 template <class T> struct S1 {
7189 template <class U> struct S2 {};
7190 template <class U> struct S2<U*> {};
7193 we will be called with an ARGLIST of `U*', but the
7194 TEMPLATE will be `template <class T> template
7195 <class U> struct S1<T>::S2'. We must fill in the missing
7198 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7200 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7203 /* Now we should have enough arguments. */
7204 gcc_assert (parm_depth
== arg_depth
);
7206 /* From here on, we're only interested in the most general
7209 /* Calculate the BOUND_ARGS. These will be the args that are
7210 actually tsubst'd into the definition to create the
7214 /* We have multiple levels of arguments to coerce, at once. */
7216 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7218 tree bound_args
= make_tree_vec (parm_depth
);
7220 for (i
= saved_depth
,
7221 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7222 i
> 0 && t
!= NULL_TREE
;
7223 --i
, t
= TREE_CHAIN (t
))
7226 if (i
== saved_depth
)
7227 a
= coerce_template_parms (TREE_VALUE (t
),
7230 /*require_all_args=*/true,
7231 /*use_default_args=*/true);
7233 /* Outer levels should have already been coerced. */
7234 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7236 /* Don't process further if one of the levels fails. */
7237 if (a
== error_mark_node
)
7239 /* Restore the ARGLIST to its full size. */
7240 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7241 return error_mark_node
;
7244 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7246 /* We temporarily reduce the length of the ARGLIST so
7247 that coerce_template_parms will see only the arguments
7248 corresponding to the template parameters it is
7250 TREE_VEC_LENGTH (arglist
)--;
7253 /* Restore the ARGLIST to its full size. */
7254 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7256 arglist
= bound_args
;
7260 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7261 INNERMOST_TEMPLATE_ARGS (arglist
),
7264 /*require_all_args=*/true,
7265 /*use_default_args=*/true);
7267 if (arglist
== error_mark_node
)
7268 /* We were unable to bind the arguments. */
7269 return error_mark_node
;
7271 /* In the scope of a template class, explicit references to the
7272 template class refer to the type of the template, not any
7273 instantiation of it. For example, in:
7275 template <class T> class C { void f(C<T>); }
7277 the `C<T>' is just the same as `C'. Outside of the
7278 class, however, such a reference is an instantiation. */
7280 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7281 || currently_open_class (template_type
))
7282 /* comp_template_args is expensive, check it last. */
7283 && comp_template_args (TYPE_TI_ARGS (template_type
),
7285 return template_type
;
7287 /* If we already have this specialization, return it. */
7288 elt
.tmpl
= gen_tmpl
;
7290 hash
= hash_specialization (&elt
);
7291 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7297 is_dependent_type
= uses_template_parms (arglist
);
7299 /* If the deduced arguments are invalid, then the binding
7301 if (!is_dependent_type
7302 && check_instantiated_args (gen_tmpl
,
7303 INNERMOST_TEMPLATE_ARGS (arglist
),
7305 return error_mark_node
;
7307 if (!is_dependent_type
7308 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7309 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7310 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7312 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7313 DECL_NAME (gen_tmpl
),
7314 /*tag_scope=*/ts_global
);
7318 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7320 if (context
== error_mark_node
)
7321 return error_mark_node
;
7324 context
= global_namespace
;
7326 /* Create the type. */
7327 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7329 if (!is_dependent_type
)
7331 set_current_access_from_decl (TYPE_NAME (template_type
));
7332 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7333 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7334 arglist
, complain
, in_decl
),
7335 SCOPED_ENUM_P (template_type
), NULL
);
7339 /* We don't want to call start_enum for this type, since
7340 the values for the enumeration constants may involve
7341 template parameters. And, no one should be interested
7342 in the enumeration constants for such a type. */
7343 t
= cxx_make_type (ENUMERAL_TYPE
);
7344 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7346 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7347 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7348 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7350 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7352 /* The user referred to a specialization of an alias
7353 template represented by GEN_TMPL.
7355 [temp.alias]/2 says:
7357 When a template-id refers to the specialization of an
7358 alias template, it is equivalent to the associated
7359 type obtained by substitution of its
7360 template-arguments for the template-parameters in the
7361 type-id of the alias template. */
7363 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7364 /* Note that the call above (by indirectly calling
7365 register_specialization in tsubst_decl) registers the
7366 TYPE_DECL representing the specialization of the alias
7367 template. So next time someone substitutes ARGLIST for
7368 the template parms into the alias template (GEN_TMPL),
7369 she'll get that TYPE_DECL back. */
7371 if (t
== error_mark_node
)
7374 else if (CLASS_TYPE_P (template_type
))
7376 t
= make_class_type (TREE_CODE (template_type
));
7377 CLASSTYPE_DECLARED_CLASS (t
)
7378 = CLASSTYPE_DECLARED_CLASS (template_type
);
7379 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7380 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7382 /* A local class. Make sure the decl gets registered properly. */
7383 if (context
== current_function_decl
)
7384 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7386 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7387 /* This instantiation is another name for the primary
7388 template type. Set the TYPE_CANONICAL field
7390 TYPE_CANONICAL (t
) = template_type
;
7391 else if (any_template_arguments_need_structural_equality_p (arglist
))
7392 /* Some of the template arguments require structural
7393 equality testing, so this template class requires
7394 structural equality testing. */
7395 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7400 /* If we called start_enum or pushtag above, this information
7401 will already be set up. */
7404 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7406 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7407 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7408 DECL_SOURCE_LOCATION (type_decl
)
7409 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7412 type_decl
= TYPE_NAME (t
);
7414 if (CLASS_TYPE_P (template_type
))
7416 TREE_PRIVATE (type_decl
)
7417 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7418 TREE_PROTECTED (type_decl
)
7419 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7420 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7422 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7423 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7427 /* Let's consider the explicit specialization of a member
7428 of a class template specialization that is implicitely instantiated,
7433 template<class U> struct M {}; //#0
7438 struct S<int>::M<char> //#1
7442 [temp.expl.spec]/4 says this is valid.
7444 In this case, when we write:
7447 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7450 When we encounter #1, we want to store the partial instantiation
7451 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7453 For all cases other than this "explicit specialization of member of a
7454 class template", we just want to store the most general template into
7455 the CLASSTYPE_TI_TEMPLATE of M.
7457 This case of "explicit specialization of member of a class template"
7459 1/ the enclosing class is an instantiation of, and therefore not
7460 the same as, the context of the most general template, and
7461 2/ we aren't looking at the partial instantiation itself, i.e.
7462 the innermost arguments are not the same as the innermost parms of
7463 the most general template.
7465 So it's only when 1/ and 2/ happens that we want to use the partial
7466 instantiation of the member template in lieu of its most general
7469 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7470 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7471 /* the enclosing class must be an instantiation... */
7472 && CLASS_TYPE_P (context
)
7473 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7475 tree partial_inst_args
;
7476 TREE_VEC_LENGTH (arglist
)--;
7477 ++processing_template_decl
;
7479 tsubst (INNERMOST_TEMPLATE_ARGS
7480 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7481 arglist
, complain
, NULL_TREE
);
7482 --processing_template_decl
;
7483 TREE_VEC_LENGTH (arglist
)++;
7484 use_partial_inst_tmpl
=
7485 /*...and we must not be looking at the partial instantiation
7487 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7491 if (!use_partial_inst_tmpl
)
7492 /* This case is easy; there are no member templates involved. */
7496 /* This is a full instantiation of a member template. Find
7497 the partial instantiation of which this is an instance. */
7499 /* Temporarily reduce by one the number of levels in the ARGLIST
7500 so as to avoid comparing the last set of arguments. */
7501 TREE_VEC_LENGTH (arglist
)--;
7502 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7503 TREE_VEC_LENGTH (arglist
)++;
7504 /* FOUND is either a proper class type, or an alias
7505 template specialization. In the later case, it's a
7506 TYPE_DECL, resulting from the substituting of arguments
7507 for parameters in the TYPE_DECL of the alias template
7508 done earlier. So be careful while getting the template
7510 found
= TREE_CODE (found
) == TYPE_DECL
7511 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7512 : CLASSTYPE_TI_TEMPLATE (found
);
7515 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7518 slot
= htab_find_slot_with_hash (type_specializations
,
7519 &elt
, hash
, INSERT
);
7520 entry
= ggc_alloc_spec_entry ();
7524 /* Note this use of the partial instantiation so we can check it
7525 later in maybe_process_partial_specialization. */
7526 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7527 = tree_cons (arglist
, t
,
7528 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7530 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7531 /* Now that the type has been registered on the instantiations
7532 list, we set up the enumerators. Because the enumeration
7533 constants may involve the enumeration type itself, we make
7534 sure to register the type first, and then create the
7535 constants. That way, doing tsubst_expr for the enumeration
7536 constants won't result in recursive calls here; we'll find
7537 the instantiation and exit above. */
7538 tsubst_enum (template_type
, t
, arglist
);
7540 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7541 /* If the type makes use of template parameters, the
7542 code that generates debugging information will crash. */
7543 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7545 /* Possibly limit visibility based on template args. */
7546 TREE_PUBLIC (type_decl
) = 1;
7547 determine_visibility (type_decl
);
7553 /* Wrapper for lookup_template_class_1. */
7556 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7557 int entering_scope
, tsubst_flags_t complain
)
7560 timevar_push (TV_TEMPLATE_INST
);
7561 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7562 entering_scope
, complain
);
7563 timevar_pop (TV_TEMPLATE_INST
);
7571 /* True when we should also visit template parameters that occur in
7572 non-deduced contexts. */
7573 bool include_nondeduced_p
;
7574 struct pointer_set_t
*visited
;
7577 /* Called from for_each_template_parm via walk_tree. */
7580 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7583 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7584 tree_fn_t fn
= pfd
->fn
;
7585 void *data
= pfd
->data
;
7588 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7589 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7590 pfd
->include_nondeduced_p
))
7591 return error_mark_node
;
7593 switch (TREE_CODE (t
))
7596 if (TYPE_PTRMEMFUNC_P (t
))
7602 if (!TYPE_TEMPLATE_INFO (t
))
7604 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7605 fn
, data
, pfd
->visited
,
7606 pfd
->include_nondeduced_p
))
7607 return error_mark_node
;
7611 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7612 fn
, data
, pfd
->visited
,
7613 pfd
->include_nondeduced_p
)
7614 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7615 fn
, data
, pfd
->visited
,
7616 pfd
->include_nondeduced_p
))
7617 return error_mark_node
;
7621 /* Since we're not going to walk subtrees, we have to do this
7623 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7624 pfd
->visited
, pfd
->include_nondeduced_p
))
7625 return error_mark_node
;
7629 /* Check the return type. */
7630 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7631 pfd
->include_nondeduced_p
))
7632 return error_mark_node
;
7634 /* Check the parameter types. Since default arguments are not
7635 instantiated until they are needed, the TYPE_ARG_TYPES may
7636 contain expressions that involve template parameters. But,
7637 no-one should be looking at them yet. And, once they're
7638 instantiated, they don't contain template parameters, so
7639 there's no point in looking at them then, either. */
7643 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7644 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7645 pfd
->visited
, pfd
->include_nondeduced_p
))
7646 return error_mark_node
;
7648 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7649 want walk_tree walking into them itself. */
7655 case UNDERLYING_TYPE
:
7656 if (pfd
->include_nondeduced_p
7657 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7659 pfd
->include_nondeduced_p
))
7660 return error_mark_node
;
7665 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7666 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7667 pfd
->visited
, pfd
->include_nondeduced_p
))
7668 return error_mark_node
;
7673 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7674 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7675 pfd
->visited
, pfd
->include_nondeduced_p
))
7676 return error_mark_node
;
7677 if (DECL_CONTEXT (t
)
7678 && pfd
->include_nondeduced_p
7679 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7680 pfd
->visited
, pfd
->include_nondeduced_p
))
7681 return error_mark_node
;
7684 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7685 /* Record template parameters such as `T' inside `TT<T>'. */
7686 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7687 pfd
->include_nondeduced_p
))
7688 return error_mark_node
;
7691 case TEMPLATE_TEMPLATE_PARM
:
7692 case TEMPLATE_TYPE_PARM
:
7693 case TEMPLATE_PARM_INDEX
:
7694 if (fn
&& (*fn
)(t
, data
))
7695 return error_mark_node
;
7697 return error_mark_node
;
7701 /* A template template parameter is encountered. */
7702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7703 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7704 pfd
->include_nondeduced_p
))
7705 return error_mark_node
;
7707 /* Already substituted template template parameter */
7713 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7715 pfd
->include_nondeduced_p
))
7716 return error_mark_node
;
7720 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7721 && pfd
->include_nondeduced_p
7722 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7723 (TREE_TYPE (t
)), fn
, data
,
7724 pfd
->visited
, pfd
->include_nondeduced_p
))
7725 return error_mark_node
;
7730 /* If there's no type, then this thing must be some expression
7731 involving template parameters. */
7732 if (!fn
&& !TREE_TYPE (t
))
7733 return error_mark_node
;
7738 case IMPLICIT_CONV_EXPR
:
7739 case REINTERPRET_CAST_EXPR
:
7740 case CONST_CAST_EXPR
:
7741 case STATIC_CAST_EXPR
:
7742 case DYNAMIC_CAST_EXPR
:
7746 case PSEUDO_DTOR_EXPR
:
7748 return error_mark_node
;
7755 /* We didn't find any template parameters we liked. */
7759 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7760 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7761 call FN with the parameter and the DATA.
7762 If FN returns nonzero, the iteration is terminated, and
7763 for_each_template_parm returns 1. Otherwise, the iteration
7764 continues. If FN never returns a nonzero value, the value
7765 returned by for_each_template_parm is 0. If FN is NULL, it is
7766 considered to be the function which always returns 1.
7768 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7769 parameters that occur in non-deduced contexts. When false, only
7770 visits those template parameters that can be deduced. */
7773 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7774 struct pointer_set_t
*visited
,
7775 bool include_nondeduced_p
)
7777 struct pair_fn_data pfd
;
7783 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7785 /* Walk the tree. (Conceptually, we would like to walk without
7786 duplicates, but for_each_template_parm_r recursively calls
7787 for_each_template_parm, so we would need to reorganize a fair
7788 bit to use walk_tree_without_duplicates, so we keep our own
7791 pfd
.visited
= visited
;
7793 pfd
.visited
= pointer_set_create ();
7794 result
= cp_walk_tree (&t
,
7795 for_each_template_parm_r
,
7797 pfd
.visited
) != NULL_TREE
;
7802 pointer_set_destroy (pfd
.visited
);
7809 /* Returns true if T depends on any template parameter. */
7812 uses_template_parms (tree t
)
7815 int saved_processing_template_decl
;
7817 saved_processing_template_decl
= processing_template_decl
;
7818 if (!saved_processing_template_decl
)
7819 processing_template_decl
= 1;
7821 dependent_p
= dependent_type_p (t
);
7822 else if (TREE_CODE (t
) == TREE_VEC
)
7823 dependent_p
= any_dependent_template_arguments_p (t
);
7824 else if (TREE_CODE (t
) == TREE_LIST
)
7825 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7826 || uses_template_parms (TREE_CHAIN (t
)));
7827 else if (TREE_CODE (t
) == TYPE_DECL
)
7828 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7831 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7832 || TREE_CODE (t
) == OVERLOAD
7834 || TREE_CODE (t
) == IDENTIFIER_NODE
7835 || TREE_CODE (t
) == TRAIT_EXPR
7836 || TREE_CODE (t
) == CONSTRUCTOR
7837 || CONSTANT_CLASS_P (t
))
7838 dependent_p
= (type_dependent_expression_p (t
)
7839 || value_dependent_expression_p (t
));
7842 gcc_assert (t
== error_mark_node
);
7843 dependent_p
= false;
7846 processing_template_decl
= saved_processing_template_decl
;
7851 /* Returns true iff current_function_decl is an incompletely instantiated
7852 template. Useful instead of processing_template_decl because the latter
7853 is set to 0 during fold_non_dependent_expr. */
7856 in_template_function (void)
7858 tree fn
= current_function_decl
;
7860 ++processing_template_decl
;
7861 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7862 && DECL_TEMPLATE_INFO (fn
)
7863 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7864 --processing_template_decl
;
7868 /* Returns true if T depends on any template parameter with level LEVEL. */
7871 uses_template_parms_level (tree t
, int level
)
7873 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7874 /*include_nondeduced_p=*/true);
7877 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7878 ill-formed translation unit, i.e. a variable or function that isn't
7879 usable in a constant expression. */
7882 neglectable_inst_p (tree d
)
7885 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7886 : decl_maybe_constant_var_p (d
)));
7889 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7890 neglectable and instantiated from within an erroneous instantiation. */
7893 limit_bad_template_recursion (tree decl
)
7895 struct tinst_level
*lev
= current_tinst_level
;
7896 int errs
= errorcount
+ sorrycount
;
7897 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7900 for (; lev
; lev
= lev
->next
)
7901 if (neglectable_inst_p (lev
->decl
))
7904 return (lev
&& errs
> lev
->errors
);
7907 static int tinst_depth
;
7908 extern int max_tinst_depth
;
7911 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7913 /* We're starting to instantiate D; record the template instantiation context
7914 for diagnostics and to restore it later. */
7917 push_tinst_level (tree d
)
7919 struct tinst_level
*new_level
;
7921 if (tinst_depth
>= max_tinst_depth
)
7923 last_error_tinst_level
= current_tinst_level
;
7924 if (TREE_CODE (d
) == TREE_LIST
)
7925 error ("template instantiation depth exceeds maximum of %d (use "
7926 "-ftemplate-depth= to increase the maximum) substituting %qS",
7927 max_tinst_depth
, d
);
7929 error ("template instantiation depth exceeds maximum of %d (use "
7930 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7931 max_tinst_depth
, d
);
7933 print_instantiation_context ();
7938 /* If the current instantiation caused problems, don't let it instantiate
7939 anything else. Do allow deduction substitution and decls usable in
7940 constant expressions. */
7941 if (limit_bad_template_recursion (d
))
7944 new_level
= ggc_alloc_tinst_level ();
7945 new_level
->decl
= d
;
7946 new_level
->locus
= input_location
;
7947 new_level
->errors
= errorcount
+sorrycount
;
7948 new_level
->in_system_header_p
= in_system_header
;
7949 new_level
->next
= current_tinst_level
;
7950 current_tinst_level
= new_level
;
7953 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7954 depth_reached
= tinst_depth
;
7959 /* We're done instantiating this template; return to the instantiation
7963 pop_tinst_level (void)
7965 /* Restore the filename and line number stashed away when we started
7966 this instantiation. */
7967 input_location
= current_tinst_level
->locus
;
7968 current_tinst_level
= current_tinst_level
->next
;
7972 /* We're instantiating a deferred template; restore the template
7973 instantiation context in which the instantiation was requested, which
7974 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7977 reopen_tinst_level (struct tinst_level
*level
)
7979 struct tinst_level
*t
;
7982 for (t
= level
; t
; t
= t
->next
)
7985 current_tinst_level
= level
;
7987 if (current_tinst_level
)
7988 current_tinst_level
->errors
= errorcount
+sorrycount
;
7992 /* Returns the TINST_LEVEL which gives the original instantiation
7995 struct tinst_level
*
7996 outermost_tinst_level (void)
7998 struct tinst_level
*level
= current_tinst_level
;
8001 level
= level
->next
;
8005 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8006 vector of template arguments, as for tsubst.
8008 Returns an appropriate tsubst'd friend declaration. */
8011 tsubst_friend_function (tree decl
, tree args
)
8015 if (TREE_CODE (decl
) == FUNCTION_DECL
8016 && DECL_TEMPLATE_INSTANTIATION (decl
)
8017 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8018 /* This was a friend declared with an explicit template
8019 argument list, e.g.:
8023 to indicate that f was a template instantiation, not a new
8024 function declaration. Now, we have to figure out what
8025 instantiation of what template. */
8027 tree template_id
, arglist
, fns
;
8030 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8032 /* Friend functions are looked up in the containing namespace scope.
8033 We must enter that scope, to avoid finding member functions of the
8034 current class with same name. */
8035 push_nested_namespace (ns
);
8036 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8037 tf_warning_or_error
, NULL_TREE
,
8038 /*integral_constant_expression_p=*/false);
8039 pop_nested_namespace (ns
);
8040 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8041 tf_warning_or_error
, NULL_TREE
);
8042 template_id
= lookup_template_function (fns
, arglist
);
8044 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8045 tmpl
= determine_specialization (template_id
, new_friend
,
8047 /*need_member_template=*/0,
8048 TREE_VEC_LENGTH (args
),
8050 return instantiate_template (tmpl
, new_args
, tf_error
);
8053 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8055 /* The NEW_FRIEND will look like an instantiation, to the
8056 compiler, but is not an instantiation from the point of view of
8057 the language. For example, we might have had:
8059 template <class T> struct S {
8060 template <class U> friend void f(T, U);
8063 Then, in S<int>, template <class U> void f(int, U) is not an
8064 instantiation of anything. */
8065 if (new_friend
== error_mark_node
)
8066 return error_mark_node
;
8068 DECL_USE_TEMPLATE (new_friend
) = 0;
8069 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8071 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8072 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8073 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8076 /* The mangled name for the NEW_FRIEND is incorrect. The function
8077 is not a template instantiation and should not be mangled like
8078 one. Therefore, we forget the mangling here; we'll recompute it
8079 later if we need it. */
8080 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8082 SET_DECL_RTL (new_friend
, NULL
);
8083 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8086 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8089 tree new_friend_template_info
;
8090 tree new_friend_result_template_info
;
8092 int new_friend_is_defn
;
8094 /* We must save some information from NEW_FRIEND before calling
8095 duplicate decls since that function will free NEW_FRIEND if
8097 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8098 new_friend_is_defn
=
8099 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8100 (template_for_substitution (new_friend
)))
8102 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8104 /* This declaration is a `primary' template. */
8105 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8107 new_friend_result_template_info
8108 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8111 new_friend_result_template_info
= NULL_TREE
;
8113 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8114 if (new_friend_is_defn
)
8115 DECL_INITIAL (new_friend
) = error_mark_node
;
8117 /* Inside pushdecl_namespace_level, we will push into the
8118 current namespace. However, the friend function should go
8119 into the namespace of the template. */
8120 ns
= decl_namespace_context (new_friend
);
8121 push_nested_namespace (ns
);
8122 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8123 pop_nested_namespace (ns
);
8125 if (old_decl
== error_mark_node
)
8126 return error_mark_node
;
8128 if (old_decl
!= new_friend
)
8130 /* This new friend declaration matched an existing
8131 declaration. For example, given:
8133 template <class T> void f(T);
8134 template <class U> class C {
8135 template <class T> friend void f(T) {}
8138 the friend declaration actually provides the definition
8139 of `f', once C has been instantiated for some type. So,
8140 old_decl will be the out-of-class template declaration,
8141 while new_friend is the in-class definition.
8143 But, if `f' was called before this point, the
8144 instantiation of `f' will have DECL_TI_ARGS corresponding
8145 to `T' but not to `U', references to which might appear
8146 in the definition of `f'. Previously, the most general
8147 template for an instantiation of `f' was the out-of-class
8148 version; now it is the in-class version. Therefore, we
8149 run through all specialization of `f', adding to their
8150 DECL_TI_ARGS appropriately. In particular, they need a
8151 new set of outer arguments, corresponding to the
8152 arguments for this class instantiation.
8154 The same situation can arise with something like this:
8157 template <class T> class C {
8161 when `C<int>' is instantiated. Now, `f(int)' is defined
8164 if (!new_friend_is_defn
)
8165 /* On the other hand, if the in-class declaration does
8166 *not* provide a definition, then we don't want to alter
8167 existing definitions. We can just leave everything
8172 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8173 tree new_args
= TI_ARGS (new_friend_template_info
);
8175 /* Overwrite whatever template info was there before, if
8176 any, with the new template information pertaining to
8178 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8180 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8182 /* We should have called reregister_specialization in
8184 gcc_assert (retrieve_specialization (new_template
,
8188 /* Instantiate it if the global has already been used. */
8189 if (DECL_ODR_USED (old_decl
))
8190 instantiate_decl (old_decl
, /*defer_ok=*/true,
8191 /*expl_inst_class_mem_p=*/false);
8197 /* Indicate that the old function template is a partial
8199 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8200 = new_friend_result_template_info
;
8202 gcc_assert (new_template
8203 == most_general_template (new_template
));
8204 gcc_assert (new_template
!= old_decl
);
8206 /* Reassign any specializations already in the hash table
8207 to the new more general template, and add the
8208 additional template args. */
8209 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8213 tree spec
= TREE_VALUE (t
);
8216 elt
.tmpl
= old_decl
;
8217 elt
.args
= DECL_TI_ARGS (spec
);
8218 elt
.spec
= NULL_TREE
;
8220 htab_remove_elt (decl_specializations
, &elt
);
8223 = add_outermost_template_args (new_args
,
8224 DECL_TI_ARGS (spec
));
8226 register_specialization
8227 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8230 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8234 /* The information from NEW_FRIEND has been merged into OLD_DECL
8235 by duplicate_decls. */
8236 new_friend
= old_decl
;
8241 tree context
= DECL_CONTEXT (new_friend
);
8245 template <class T> class C {
8246 template <class U> friend void C1<U>::f (); // case 1
8247 friend void C2<T>::f (); // case 2
8249 we only need to make sure CONTEXT is a complete type for
8250 case 2. To distinguish between the two cases, we note that
8251 CONTEXT of case 1 remains dependent type after tsubst while
8252 this isn't true for case 2. */
8253 ++processing_template_decl
;
8254 dependent_p
= dependent_type_p (context
);
8255 --processing_template_decl
;
8258 && !complete_type_or_else (context
, NULL_TREE
))
8259 return error_mark_node
;
8261 if (COMPLETE_TYPE_P (context
))
8263 /* Check to see that the declaration is really present, and,
8264 possibly obtain an improved declaration. */
8265 tree fn
= check_classfn (context
,
8266 new_friend
, NULL_TREE
);
8276 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8277 template arguments, as for tsubst.
8279 Returns an appropriate tsubst'd friend type or error_mark_node on
8283 tsubst_friend_class (tree friend_tmpl
, tree args
)
8289 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8291 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8292 return TREE_TYPE (t
);
8295 context
= CP_DECL_CONTEXT (friend_tmpl
);
8297 if (context
!= global_namespace
)
8299 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8300 push_nested_namespace (context
);
8302 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8305 /* Look for a class template declaration. We look for hidden names
8306 because two friend declarations of the same template are the
8307 same. For example, in:
8310 template <typename> friend class F;
8312 template <typename> struct B {
8313 template <typename> friend class F;
8316 both F templates are the same. */
8317 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8318 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8320 /* But, if we don't find one, it might be because we're in a
8321 situation like this:
8329 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8330 for `S<int>', not the TEMPLATE_DECL. */
8331 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8333 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8334 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8337 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8339 /* The friend template has already been declared. Just
8340 check to see that the declarations match, and install any new
8341 default parameters. We must tsubst the default parameters,
8342 of course. We only need the innermost template parameters
8343 because that is all that redeclare_class_template will look
8345 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8346 > TMPL_ARGS_DEPTH (args
))
8349 location_t saved_input_location
;
8350 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8351 args
, tf_warning_or_error
);
8353 saved_input_location
= input_location
;
8354 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8355 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8356 input_location
= saved_input_location
;
8360 friend_type
= TREE_TYPE (tmpl
);
8364 /* The friend template has not already been declared. In this
8365 case, the instantiation of the template class will cause the
8366 injection of this template into the global scope. */
8367 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8368 if (tmpl
== error_mark_node
)
8369 return error_mark_node
;
8371 /* The new TMPL is not an instantiation of anything, so we
8372 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8373 the new type because that is supposed to be the corresponding
8374 template decl, i.e., TMPL. */
8375 DECL_USE_TEMPLATE (tmpl
) = 0;
8376 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8377 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8378 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8379 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8381 /* Inject this template into the global scope. */
8382 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8385 if (context
!= global_namespace
)
8387 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8388 pop_nested_namespace (context
);
8390 pop_nested_class ();
8396 /* Returns zero if TYPE cannot be completed later due to circularity.
8397 Otherwise returns one. */
8400 can_complete_type_without_circularity (tree type
)
8402 if (type
== NULL_TREE
|| type
== error_mark_node
)
8404 else if (COMPLETE_TYPE_P (type
))
8406 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8407 return can_complete_type_without_circularity (TREE_TYPE (type
));
8408 else if (CLASS_TYPE_P (type
)
8409 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8415 /* Apply any attributes which had to be deferred until instantiation
8416 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8417 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8420 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8421 tree args
, tsubst_flags_t complain
, tree in_decl
)
8423 tree last_dep
= NULL_TREE
;
8427 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8428 if (ATTR_IS_DEPENDENT (t
))
8431 attributes
= copy_list (attributes
);
8435 if (DECL_P (*decl_p
))
8437 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8439 p
= &DECL_ATTRIBUTES (*decl_p
);
8442 p
= &TYPE_ATTRIBUTES (*decl_p
);
8446 tree late_attrs
= NULL_TREE
;
8447 tree
*q
= &late_attrs
;
8449 for (*p
= attributes
; *p
; )
8452 if (ATTR_IS_DEPENDENT (t
))
8454 *p
= TREE_CHAIN (t
);
8455 TREE_CHAIN (t
) = NULL_TREE
;
8456 /* If the first attribute argument is an identifier, don't
8457 pass it through tsubst. Attributes like mode, format,
8458 cleanup and several target specific attributes expect it
8461 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8462 && TREE_VALUE (TREE_VALUE (t
))
8463 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8464 == IDENTIFIER_NODE
))
8467 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8469 /*integral_constant_expression_p=*/false);
8470 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8472 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8477 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8478 /*integral_constant_expression_p=*/false);
8480 q
= &TREE_CHAIN (t
);
8483 p
= &TREE_CHAIN (t
);
8486 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8490 /* Perform (or defer) access check for typedefs that were referenced
8491 from within the template TMPL code.
8492 This is a subroutine of instantiate_decl and instantiate_class_template.
8493 TMPL is the template to consider and TARGS is the list of arguments of
8497 perform_typedefs_access_check (tree tmpl
, tree targs
)
8499 location_t saved_location
;
8501 qualified_typedef_usage_t
*iter
;
8504 || (!CLASS_TYPE_P (tmpl
)
8505 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8508 saved_location
= input_location
;
8509 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8511 tree type_decl
= iter
->typedef_decl
;
8512 tree type_scope
= iter
->context
;
8514 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8517 if (uses_template_parms (type_decl
))
8518 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8519 if (uses_template_parms (type_scope
))
8520 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8522 /* Make access check error messages point to the location
8523 of the use of the typedef. */
8524 input_location
= iter
->locus
;
8525 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8526 type_decl
, type_decl
,
8527 tf_warning_or_error
);
8529 input_location
= saved_location
;
8533 instantiate_class_template_1 (tree type
)
8535 tree templ
, args
, pattern
, t
, member
;
8539 unsigned int saved_maximum_field_alignment
;
8542 if (type
== error_mark_node
)
8543 return error_mark_node
;
8545 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8546 || uses_template_parms (type
))
8549 /* Figure out which template is being instantiated. */
8550 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8551 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8553 /* Determine what specialization of the original template to
8555 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8556 if (t
== error_mark_node
)
8558 TYPE_BEING_DEFINED (type
) = 1;
8559 return error_mark_node
;
8563 /* This TYPE is actually an instantiation of a partial
8564 specialization. We replace the innermost set of ARGS with
8565 the arguments appropriate for substitution. For example,
8568 template <class T> struct S {};
8569 template <class T> struct S<T*> {};
8571 and supposing that we are instantiating S<int*>, ARGS will
8572 presently be {int*} -- but we need {int}. */
8573 pattern
= TREE_TYPE (t
);
8574 args
= TREE_PURPOSE (t
);
8578 pattern
= TREE_TYPE (templ
);
8579 args
= CLASSTYPE_TI_ARGS (type
);
8582 /* If the template we're instantiating is incomplete, then clearly
8583 there's nothing we can do. */
8584 if (!COMPLETE_TYPE_P (pattern
))
8587 /* If we've recursively instantiated too many templates, stop. */
8588 if (! push_tinst_level (type
))
8591 /* Now we're really doing the instantiation. Mark the type as in
8592 the process of being defined. */
8593 TYPE_BEING_DEFINED (type
) = 1;
8595 /* We may be in the middle of deferred access check. Disable
8597 push_deferring_access_checks (dk_no_deferred
);
8599 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8601 push_to_top_level ();
8602 /* Use #pragma pack from the template context. */
8603 saved_maximum_field_alignment
= maximum_field_alignment
;
8604 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8606 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8608 /* Set the input location to the most specialized template definition.
8609 This is needed if tsubsting causes an error. */
8610 typedecl
= TYPE_MAIN_DECL (pattern
);
8611 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8612 DECL_SOURCE_LOCATION (typedecl
);
8614 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8615 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8616 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8617 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8618 if (ANON_AGGR_TYPE_P (pattern
))
8619 SET_ANON_AGGR_TYPE_P (type
);
8620 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8622 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8623 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8624 /* Adjust visibility for template arguments. */
8625 determine_visibility (TYPE_MAIN_DECL (type
));
8627 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8629 pbinfo
= TYPE_BINFO (pattern
);
8631 /* We should never instantiate a nested class before its enclosing
8632 class; we need to look up the nested class by name before we can
8633 instantiate it, and that lookup should instantiate the enclosing
8635 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8636 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8638 base_list
= NULL_TREE
;
8639 if (BINFO_N_BASE_BINFOS (pbinfo
))
8645 /* We must enter the scope containing the type, as that is where
8646 the accessibility of types named in dependent bases are
8648 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8650 /* Substitute into each of the bases to determine the actual
8652 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8655 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8656 tree expanded_bases
= NULL_TREE
;
8659 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8662 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8663 args
, tf_error
, NULL_TREE
);
8664 if (expanded_bases
== error_mark_node
)
8667 len
= TREE_VEC_LENGTH (expanded_bases
);
8670 for (idx
= 0; idx
< len
; idx
++)
8673 /* Extract the already-expanded base class. */
8674 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8676 /* Substitute to figure out the base class. */
8677 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8680 if (base
== error_mark_node
)
8683 base_list
= tree_cons (access
, base
, base_list
);
8684 if (BINFO_VIRTUAL_P (pbase_binfo
))
8685 TREE_TYPE (base_list
) = integer_type_node
;
8689 /* The list is now in reverse order; correct that. */
8690 base_list
= nreverse (base_list
);
8693 pop_scope (pushed_scope
);
8695 /* Now call xref_basetypes to set up all the base-class
8697 xref_basetypes (type
, base_list
);
8699 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8700 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8701 args
, tf_error
, NULL_TREE
);
8702 fixup_attribute_variants (type
);
8704 /* Now that our base classes are set up, enter the scope of the
8705 class, so that name lookups into base classes, etc. will work
8706 correctly. This is precisely analogous to what we do in
8707 begin_class_definition when defining an ordinary non-template
8708 class, except we also need to push the enclosing classes. */
8709 push_nested_class (type
);
8711 /* Now members are processed in the order of declaration. */
8712 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8713 member
; member
= TREE_CHAIN (member
))
8715 tree t
= TREE_VALUE (member
);
8717 if (TREE_PURPOSE (member
))
8721 /* Build new CLASSTYPE_NESTED_UTDS. */
8724 bool class_template_p
;
8726 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8727 && TYPE_LANG_SPECIFIC (t
)
8728 && CLASSTYPE_IS_TEMPLATE (t
));
8729 /* If the member is a class template, then -- even after
8730 substitution -- there may be dependent types in the
8731 template argument list for the class. We increment
8732 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8733 that function will assume that no types are dependent
8734 when outside of a template. */
8735 if (class_template_p
)
8736 ++processing_template_decl
;
8737 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8738 if (class_template_p
)
8739 --processing_template_decl
;
8740 if (newtag
== error_mark_node
)
8743 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8745 tree name
= TYPE_IDENTIFIER (t
);
8747 if (class_template_p
)
8748 /* Unfortunately, lookup_template_class sets
8749 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8750 instantiation (i.e., for the type of a member
8751 template class nested within a template class.)
8752 This behavior is required for
8753 maybe_process_partial_specialization to work
8754 correctly, but is not accurate in this case;
8755 the TAG is not an instantiation of anything.
8756 (The corresponding TEMPLATE_DECL is an
8757 instantiation, but the TYPE is not.) */
8758 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8760 /* Now, we call pushtag to put this NEWTAG into the scope of
8761 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8762 pushtag calling push_template_decl. We don't have to do
8763 this for enums because it will already have been done in
8766 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8767 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8770 else if (TREE_CODE (t
) == FUNCTION_DECL
8771 || DECL_FUNCTION_TEMPLATE_P (t
))
8773 /* Build new TYPE_METHODS. */
8776 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8777 ++processing_template_decl
;
8778 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8779 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8780 --processing_template_decl
;
8781 set_current_access_from_decl (r
);
8782 finish_member_declaration (r
);
8783 /* Instantiate members marked with attribute used. */
8784 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8789 /* Build new TYPE_FIELDS. */
8790 if (TREE_CODE (t
) == STATIC_ASSERT
)
8794 ++c_inhibit_evaluation_warnings
;
8796 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8797 tf_warning_or_error
, NULL_TREE
,
8798 /*integral_constant_expression_p=*/true);
8799 --c_inhibit_evaluation_warnings
;
8801 finish_static_assert (condition
,
8802 STATIC_ASSERT_MESSAGE (t
),
8803 STATIC_ASSERT_SOURCE_LOCATION (t
),
8806 else if (TREE_CODE (t
) != CONST_DECL
)
8810 /* The file and line for this declaration, to
8811 assist in error message reporting. Since we
8812 called push_tinst_level above, we don't need to
8814 input_location
= DECL_SOURCE_LOCATION (t
);
8816 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8817 ++processing_template_decl
;
8818 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8819 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8820 --processing_template_decl
;
8821 if (TREE_CODE (r
) == VAR_DECL
)
8825 [t]he initialization (and any associated
8826 side-effects) of a static data member does
8827 not occur unless the static data member is
8828 itself used in a way that requires the
8829 definition of the static data member to
8832 Therefore, we do not substitute into the
8833 initialized for the static data member here. */
8834 finish_static_data_member_decl
8837 /*init_const_expr_p=*/false,
8838 /*asmspec_tree=*/NULL_TREE
,
8840 /* Instantiate members marked with attribute used. */
8841 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8844 else if (TREE_CODE (r
) == FIELD_DECL
)
8846 /* Determine whether R has a valid type and can be
8847 completed later. If R is invalid, then it is
8848 replaced by error_mark_node so that it will not be
8849 added to TYPE_FIELDS. */
8850 tree rtype
= TREE_TYPE (r
);
8851 if (can_complete_type_without_circularity (rtype
))
8852 complete_type (rtype
);
8854 if (!COMPLETE_TYPE_P (rtype
))
8856 cxx_incomplete_type_error (r
, rtype
);
8857 r
= error_mark_node
;
8861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8862 such a thing will already have been added to the field
8863 list by tsubst_enum in finish_member_declaration in the
8864 CLASSTYPE_NESTED_UTDS case above. */
8865 if (!(TREE_CODE (r
) == TYPE_DECL
8866 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8867 && DECL_ARTIFICIAL (r
)))
8869 set_current_access_from_decl (r
);
8870 finish_member_declaration (r
);
8877 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8878 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8880 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8882 tree friend_type
= t
;
8883 bool adjust_processing_template_decl
= false;
8885 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8887 /* template <class T> friend class C; */
8888 friend_type
= tsubst_friend_class (friend_type
, args
);
8889 adjust_processing_template_decl
= true;
8891 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8893 /* template <class T> friend class C::D; */
8894 friend_type
= tsubst (friend_type
, args
,
8895 tf_warning_or_error
, NULL_TREE
);
8896 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8897 friend_type
= TREE_TYPE (friend_type
);
8898 adjust_processing_template_decl
= true;
8900 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8901 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8903 /* This could be either
8907 when dependent_type_p is false or
8909 template <class U> friend class T::C;
8912 friend_type
= tsubst (friend_type
, args
,
8913 tf_warning_or_error
, NULL_TREE
);
8914 /* Bump processing_template_decl for correct
8915 dependent_type_p calculation. */
8916 ++processing_template_decl
;
8917 if (dependent_type_p (friend_type
))
8918 adjust_processing_template_decl
= true;
8919 --processing_template_decl
;
8921 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8922 && hidden_name_p (TYPE_NAME (friend_type
)))
8926 where C hasn't been declared yet. Let's lookup name
8927 from namespace scope directly, bypassing any name that
8928 come from dependent base class. */
8929 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8931 /* The call to xref_tag_from_type does injection for friend
8933 push_nested_namespace (ns
);
8935 xref_tag_from_type (friend_type
, NULL_TREE
,
8936 /*tag_scope=*/ts_current
);
8937 pop_nested_namespace (ns
);
8939 else if (uses_template_parms (friend_type
))
8940 /* friend class C<T>; */
8941 friend_type
= tsubst (friend_type
, args
,
8942 tf_warning_or_error
, NULL_TREE
);
8947 where C is already declared or
8949 friend class C<int>;
8951 We don't have to do anything in these cases. */
8953 if (adjust_processing_template_decl
)
8954 /* Trick make_friend_class into realizing that the friend
8955 we're adding is a template, not an ordinary class. It's
8956 important that we use make_friend_class since it will
8957 perform some error-checking and output cross-reference
8959 ++processing_template_decl
;
8961 if (friend_type
!= error_mark_node
)
8962 make_friend_class (type
, friend_type
, /*complain=*/false);
8964 if (adjust_processing_template_decl
)
8965 --processing_template_decl
;
8969 /* Build new DECL_FRIENDLIST. */
8972 /* The file and line for this declaration, to
8973 assist in error message reporting. Since we
8974 called push_tinst_level above, we don't need to
8976 input_location
= DECL_SOURCE_LOCATION (t
);
8978 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8980 ++processing_template_decl
;
8981 push_deferring_access_checks (dk_no_check
);
8984 r
= tsubst_friend_function (t
, args
);
8985 add_friend (type
, r
, /*complain=*/false);
8986 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8988 pop_deferring_access_checks ();
8989 --processing_template_decl
;
8995 if (CLASSTYPE_LAMBDA_EXPR (type
))
8997 tree decl
= lambda_function (type
);
9000 instantiate_decl (decl
, false, false);
9001 maybe_add_lambda_conv_op (type
);
9004 gcc_assert (errorcount
);
9007 /* Set the file and line number information to whatever is given for
9008 the class itself. This puts error messages involving generated
9009 implicit functions at a predictable point, and the same point
9010 that would be used for non-template classes. */
9011 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9013 unreverse_member_declarations (type
);
9014 finish_struct_1 (type
);
9015 TYPE_BEING_DEFINED (type
) = 0;
9017 /* We don't instantiate default arguments for member functions. 14.7.1:
9019 The implicit instantiation of a class template specialization causes
9020 the implicit instantiation of the declarations, but not of the
9021 definitions or default arguments, of the class member functions,
9022 member classes, static data members and member templates.... */
9024 /* Some typedefs referenced from within the template code need to be access
9025 checked at template instantiation time, i.e now. These types were
9026 added to the template at parsing time. Let's get those and perform
9027 the access checks then. */
9028 perform_typedefs_access_check (pattern
, args
);
9029 perform_deferred_access_checks (tf_warning_or_error
);
9030 pop_nested_class ();
9031 maximum_field_alignment
= saved_maximum_field_alignment
;
9033 pop_from_top_level ();
9034 pop_deferring_access_checks ();
9037 /* The vtable for a template class can be emitted in any translation
9038 unit in which the class is instantiated. When there is no key
9039 method, however, finish_struct_1 will already have added TYPE to
9040 the keyed_classes list. */
9041 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9042 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9047 /* Wrapper for instantiate_class_template_1. */
9050 instantiate_class_template (tree type
)
9053 timevar_push (TV_TEMPLATE_INST
);
9054 ret
= instantiate_class_template_1 (type
);
9055 timevar_pop (TV_TEMPLATE_INST
);
9060 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9066 else if (TYPE_P (t
))
9067 r
= tsubst (t
, args
, complain
, in_decl
);
9070 if (!(complain
& tf_warning
))
9071 ++c_inhibit_evaluation_warnings
;
9072 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9073 /*integral_constant_expression_p=*/true);
9074 if (!(complain
& tf_warning
))
9075 --c_inhibit_evaluation_warnings
;
9076 /* Preserve the raw-reference nature of T. */
9077 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9078 && REFERENCE_REF_P (r
))
9079 r
= TREE_OPERAND (r
, 0);
9084 /* Given a function parameter pack TMPL_PARM and some function parameters
9085 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9086 and set *SPEC_P to point at the next point in the list. */
9089 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9091 /* Collect all of the extra "packed" parameters into an
9095 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9096 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9097 tree spec_parm
= *spec_p
;
9100 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9102 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9105 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9106 parmvec
= make_tree_vec (len
);
9107 parmtypevec
= make_tree_vec (len
);
9108 spec_parm
= *spec_p
;
9109 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9111 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9112 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9115 /* Build the argument packs. */
9116 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9117 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9118 TREE_TYPE (argpack
) = argtypepack
;
9119 *spec_p
= spec_parm
;
9124 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9125 NONTYPE_ARGUMENT_PACK. */
9128 make_fnparm_pack (tree spec_parm
)
9130 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9133 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9137 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9139 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9140 if (i
>= TREE_VEC_LENGTH (vec
))
9142 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9149 make_argument_pack_select (tree arg_pack
, unsigned index
)
9151 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9153 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9154 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9159 /* This is a subroutine of tsubst_pack_expansion.
9161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9162 mechanism to store the (non complete list of) arguments of the
9163 substitution and return a non substituted pack expansion, in order
9164 to wait for when we have enough arguments to really perform the
9168 use_pack_expansion_extra_args_p (tree parm_packs
,
9172 if (parm_packs
== NULL_TREE
)
9175 bool has_expansion_arg
= false;
9176 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9178 bool has_non_expansion_arg
= false;
9179 for (tree parm_pack
= parm_packs
;
9181 parm_pack
= TREE_CHAIN (parm_pack
))
9183 tree arg
= TREE_VALUE (parm_pack
);
9185 if (argument_pack_element_is_expansion_p (arg
, i
))
9186 has_expansion_arg
= true;
9188 has_non_expansion_arg
= true;
9191 /* If one pack has an expansion and another pack has a normal
9192 argument or if one pack has an empty argument another one
9193 hasn't then tsubst_pack_expansion cannot perform the
9194 substitution and need to fall back on the
9195 PACK_EXPANSION_EXTRA mechanism. */
9196 if ((has_expansion_arg
&& has_non_expansion_arg
)
9197 || (has_empty_arg
&& (has_expansion_arg
|| has_non_expansion_arg
)))
9203 /* [temp.variadic]/6 says that:
9205 The instantiation of a pack expansion [...]
9206 produces a list E1,E2, ..., En, where N is the number of elements
9207 in the pack expansion parameters.
9209 This subroutine of tsubst_pack_expansion produces one of these Ei.
9211 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9212 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9213 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9214 INDEX is the index 'i' of the element Ei to produce. ARGS,
9215 COMPLAIN, and IN_DECL are the same parameters as for the
9216 tsubst_pack_expansion function.
9218 The function returns the resulting Ei upon successful completion,
9221 Note that this function possibly modifies the ARGS parameter, so
9222 it's the responsibility of the caller to restore it. */
9225 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9228 tree args
/* This parm gets
9230 tsubst_flags_t complain
,
9234 bool ith_elem_is_expansion
= false;
9236 /* For each parameter pack, change the substitution of the parameter
9237 pack to the ith argument in its argument pack, then expand the
9239 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9241 tree parm
= TREE_PURPOSE (pack
);
9242 tree arg_pack
= TREE_VALUE (pack
);
9243 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9245 ith_elem_is_expansion
|=
9246 argument_pack_element_is_expansion_p (arg_pack
, index
);
9248 /* Select the Ith argument from the pack. */
9249 if (TREE_CODE (parm
) == PARM_DECL
)
9253 aps
= make_argument_pack_select (arg_pack
, index
);
9255 register_local_specialization (aps
, parm
);
9258 aps
= retrieve_local_specialization (parm
);
9263 template_parm_level_and_index (parm
, &level
, &idx
);
9267 aps
= make_argument_pack_select (arg_pack
, index
);
9268 /* Update the corresponding argument. */
9269 TMPL_ARG (args
, level
, idx
) = aps
;
9272 /* Re-use the ARGUMENT_PACK_SELECT. */
9273 aps
= TMPL_ARG (args
, level
, idx
);
9275 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9278 /* Substitute into the PATTERN with the (possibly altered)
9280 if (!TYPE_P (pattern
))
9281 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9282 /*integral_constant_expression_p=*/false);
9284 t
= tsubst (pattern
, args
, complain
, in_decl
);
9286 /* If the Ith argument pack element is a pack expansion, then
9287 the Ith element resulting from the substituting is going to
9288 be a pack expansion as well. */
9289 if (ith_elem_is_expansion
)
9290 t
= make_pack_expansion (t
);
9295 /* Substitute ARGS into T, which is an pack expansion
9296 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9297 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9298 (if only a partial substitution could be performed) or
9299 ERROR_MARK_NODE if there was an error. */
9301 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9305 tree pack
, packs
= NULL_TREE
;
9306 bool unsubstituted_packs
= false;
9309 struct pointer_map_t
*saved_local_specializations
= NULL
;
9310 bool need_local_specializations
= false;
9313 gcc_assert (PACK_EXPANSION_P (t
));
9314 pattern
= PACK_EXPANSION_PATTERN (t
);
9316 /* Add in any args remembered from an earlier partial instantiation. */
9317 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9319 levels
= TMPL_ARGS_DEPTH (args
);
9321 /* Determine the argument packs that will instantiate the parameter
9322 packs used in the expansion expression. While we're at it,
9323 compute the number of arguments to be expanded and make sure it
9325 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9326 pack
= TREE_CHAIN (pack
))
9328 tree parm_pack
= TREE_VALUE (pack
);
9329 tree arg_pack
= NULL_TREE
;
9330 tree orig_arg
= NULL_TREE
;
9333 if (TREE_CODE (parm_pack
) == BASES
)
9335 if (BASES_DIRECT (parm_pack
))
9336 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9337 args
, complain
, in_decl
, false));
9339 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9340 args
, complain
, in_decl
, false));
9342 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9344 if (PACK_EXPANSION_LOCAL_P (t
))
9345 arg_pack
= retrieve_local_specialization (parm_pack
);
9348 /* We can't rely on local_specializations for a parameter
9349 name used later in a function declaration (such as in a
9350 late-specified return type). Even if it exists, it might
9351 have the wrong value for a recursive call. Just make a
9352 dummy decl, since it's only used for its type. */
9353 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9354 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9355 /* Partial instantiation of the parm_pack, we can't build
9356 up an argument pack yet. */
9357 arg_pack
= NULL_TREE
;
9359 arg_pack
= make_fnparm_pack (arg_pack
);
9360 need_local_specializations
= true;
9366 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9368 if (level
<= levels
)
9369 arg_pack
= TMPL_ARG (args
, level
, idx
);
9372 orig_arg
= arg_pack
;
9373 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9374 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9376 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9377 /* This can only happen if we forget to expand an argument
9378 pack somewhere else. Just return an error, silently. */
9380 result
= make_tree_vec (1);
9381 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9388 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9390 /* Don't bother trying to do a partial substitution with
9391 incomplete packs; we'll try again after deduction. */
9392 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9397 else if (len
!= my_len
)
9399 if (!(complain
& tf_error
))
9400 /* Fail quietly. */;
9401 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9402 error ("mismatched argument pack lengths while expanding "
9406 error ("mismatched argument pack lengths while expanding "
9409 return error_mark_node
;
9412 /* Keep track of the parameter packs and their corresponding
9414 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9415 TREE_TYPE (packs
) = orig_arg
;
9419 /* We can't substitute for this parameter pack. We use a flag as
9420 well as the missing_level counter because function parameter
9421 packs don't have a level. */
9422 unsubstituted_packs
= true;
9426 /* We cannot expand this expansion expression, because we don't have
9427 all of the argument packs we need. */
9428 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9430 /* We got some full packs, but we can't substitute them in until we
9431 have values for all the packs. So remember these until then. */
9433 t
= make_pack_expansion (pattern
);
9434 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9437 else if (unsubstituted_packs
)
9439 /* There were no real arguments, we're just replacing a parameter
9440 pack with another version of itself. Substitute into the
9441 pattern and return a PACK_EXPANSION_*. The caller will need to
9443 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9444 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9445 /*integral_constant_expression_p=*/false);
9447 t
= tsubst (pattern
, args
, complain
, in_decl
);
9448 t
= make_pack_expansion (t
);
9452 gcc_assert (len
>= 0);
9454 if (need_local_specializations
)
9456 /* We're in a late-specified return type, so create our own local
9457 specializations map; the current map is either NULL or (in the
9458 case of recursive unification) might have bindings that we don't
9459 want to use or alter. */
9460 saved_local_specializations
= local_specializations
;
9461 local_specializations
= pointer_map_create ();
9464 /* For each argument in each argument pack, substitute into the
9466 result
= make_tree_vec (len
);
9467 for (i
= 0; i
< len
; ++i
)
9469 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9473 TREE_VEC_ELT (result
, i
) = t
;
9474 if (t
== error_mark_node
)
9476 result
= error_mark_node
;
9481 /* Update ARGS to restore the substitution from parameter packs to
9482 their argument packs. */
9483 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9485 tree parm
= TREE_PURPOSE (pack
);
9487 if (TREE_CODE (parm
) == PARM_DECL
)
9488 register_local_specialization (TREE_TYPE (pack
), parm
);
9493 if (TREE_VALUE (pack
) == NULL_TREE
)
9496 template_parm_level_and_index (parm
, &level
, &idx
);
9498 /* Update the corresponding argument. */
9499 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9500 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9503 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9507 if (need_local_specializations
)
9509 pointer_map_destroy (local_specializations
);
9510 local_specializations
= saved_local_specializations
;
9516 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9517 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9518 parameter packs; all parms generated from a function parameter pack will
9519 have the same DECL_PARM_INDEX. */
9522 get_pattern_parm (tree parm
, tree tmpl
)
9524 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9527 if (DECL_ARTIFICIAL (parm
))
9529 for (patparm
= DECL_ARGUMENTS (pattern
);
9530 patparm
; patparm
= DECL_CHAIN (patparm
))
9531 if (DECL_ARTIFICIAL (patparm
)
9532 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9537 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9538 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9539 gcc_assert (DECL_PARM_INDEX (patparm
)
9540 == DECL_PARM_INDEX (parm
));
9546 /* Substitute ARGS into the vector or list of template arguments T. */
9549 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9552 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9555 if (t
== error_mark_node
)
9556 return error_mark_node
;
9558 len
= TREE_VEC_LENGTH (t
);
9559 elts
= XALLOCAVEC (tree
, len
);
9561 for (i
= 0; i
< len
; i
++)
9563 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9566 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9567 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9568 else if (PACK_EXPANSION_P (orig_arg
))
9570 /* Substitute into an expansion expression. */
9571 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9573 if (TREE_CODE (new_arg
) == TREE_VEC
)
9574 /* Add to the expanded length adjustment the number of
9575 expanded arguments. We subtract one from this
9576 measurement, because the argument pack expression
9577 itself is already counted as 1 in
9578 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9579 the argument pack is empty. */
9580 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9582 else if (ARGUMENT_PACK_P (orig_arg
))
9584 /* Substitute into each of the arguments. */
9585 new_arg
= TYPE_P (orig_arg
)
9586 ? cxx_make_type (TREE_CODE (orig_arg
))
9587 : make_node (TREE_CODE (orig_arg
));
9589 SET_ARGUMENT_PACK_ARGS (
9591 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9592 args
, complain
, in_decl
));
9594 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9595 new_arg
= error_mark_node
;
9597 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9598 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9600 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9602 if (TREE_TYPE (new_arg
) == error_mark_node
)
9603 new_arg
= error_mark_node
;
9607 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9609 if (new_arg
== error_mark_node
)
9610 return error_mark_node
;
9613 if (new_arg
!= orig_arg
)
9620 /* Make space for the expanded arguments coming from template
9622 t
= make_tree_vec (len
+ expanded_len_adjust
);
9623 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9624 arguments for a member template.
9625 In that case each TREE_VEC in ORIG_T represents a level of template
9626 arguments, and ORIG_T won't carry any non defaulted argument count.
9627 It will rather be the nested TREE_VECs that will carry one.
9628 In other words, ORIG_T carries a non defaulted argument count only
9629 if it doesn't contain any nested TREE_VEC. */
9630 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9632 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9633 count
+= expanded_len_adjust
;
9634 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9636 for (i
= 0, out
= 0; i
< len
; i
++)
9638 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9639 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9640 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9644 /* Now expand the template argument pack "in place". */
9645 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9646 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9650 TREE_VEC_ELT (t
, out
) = elts
[i
];
9658 /* Return the result of substituting ARGS into the template parameters
9659 given by PARMS. If there are m levels of ARGS and m + n levels of
9660 PARMS, then the result will contain n levels of PARMS. For
9661 example, if PARMS is `template <class T> template <class U>
9662 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9663 result will be `template <int*, double, class V>'. */
9666 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9671 /* When substituting into a template, we must set
9672 PROCESSING_TEMPLATE_DECL as the template parameters may be
9673 dependent if they are based on one-another, and the dependency
9674 predicates are short-circuit outside of templates. */
9675 ++processing_template_decl
;
9677 for (new_parms
= &r
;
9678 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9679 new_parms
= &(TREE_CHAIN (*new_parms
)),
9680 parms
= TREE_CHAIN (parms
))
9683 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9686 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9690 if (parms
== error_mark_node
)
9693 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9695 if (tuple
== error_mark_node
)
9698 TREE_VEC_ELT (new_vec
, i
) =
9699 tsubst_template_parm (tuple
, args
, complain
);
9703 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9704 - TMPL_ARGS_DEPTH (args
)),
9705 new_vec
, NULL_TREE
);
9708 --processing_template_decl
;
9713 /* Return the result of substituting ARGS into one template parameter
9714 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9715 parameter and which TREE_PURPOSE is the default argument of the
9716 template parameter. */
9719 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9721 tree default_value
, parm_decl
;
9723 if (args
== NULL_TREE
9725 || t
== error_mark_node
)
9728 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9730 default_value
= TREE_PURPOSE (t
);
9731 parm_decl
= TREE_VALUE (t
);
9733 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9734 if (TREE_CODE (parm_decl
) == PARM_DECL
9735 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9736 parm_decl
= error_mark_node
;
9737 default_value
= tsubst_template_arg (default_value
, args
,
9738 complain
, NULL_TREE
);
9740 return build_tree_list (default_value
, parm_decl
);
9743 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9744 type T. If T is not an aggregate or enumeration type, it is
9745 handled as if by tsubst. IN_DECL is as for tsubst. If
9746 ENTERING_SCOPE is nonzero, T is the context for a template which
9747 we are presently tsubst'ing. Return the substituted value. */
9750 tsubst_aggr_type (tree t
,
9752 tsubst_flags_t complain
,
9759 switch (TREE_CODE (t
))
9762 if (TYPE_PTRMEMFUNC_P (t
))
9763 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9765 /* Else fall through. */
9768 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9773 int saved_unevaluated_operand
;
9774 int saved_inhibit_evaluation_warnings
;
9776 /* In "sizeof(X<I>)" we need to evaluate "I". */
9777 saved_unevaluated_operand
= cp_unevaluated_operand
;
9778 cp_unevaluated_operand
= 0;
9779 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9780 c_inhibit_evaluation_warnings
= 0;
9782 /* First, determine the context for the type we are looking
9784 context
= TYPE_CONTEXT (t
);
9785 if (context
&& TYPE_P (context
))
9787 context
= tsubst_aggr_type (context
, args
, complain
,
9788 in_decl
, /*entering_scope=*/1);
9789 /* If context is a nested class inside a class template,
9790 it may still need to be instantiated (c++/33959). */
9791 context
= complete_type (context
);
9794 /* Then, figure out what arguments are appropriate for the
9795 type we are trying to find. For example, given:
9797 template <class T> struct S;
9798 template <class T, class U> void f(T, U) { S<U> su; }
9800 and supposing that we are instantiating f<int, double>,
9801 then our ARGS will be {int, double}, but, when looking up
9802 S we only want {double}. */
9803 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9805 if (argvec
== error_mark_node
)
9806 r
= error_mark_node
;
9809 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9810 entering_scope
, complain
);
9811 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9814 cp_unevaluated_operand
= saved_unevaluated_operand
;
9815 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9820 /* This is not a template type, so there's nothing to do. */
9824 return tsubst (t
, args
, complain
, in_decl
);
9828 /* Substitute into the default argument ARG (a default argument for
9829 FN), which has the indicated TYPE. */
9832 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9834 tree saved_class_ptr
= NULL_TREE
;
9835 tree saved_class_ref
= NULL_TREE
;
9836 int errs
= errorcount
+ sorrycount
;
9838 /* This can happen in invalid code. */
9839 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9842 /* This default argument came from a template. Instantiate the
9843 default argument here, not in tsubst. In the case of
9852 we must be careful to do name lookup in the scope of S<T>,
9853 rather than in the current class. */
9854 push_access_scope (fn
);
9855 /* The "this" pointer is not valid in a default argument. */
9858 saved_class_ptr
= current_class_ptr
;
9859 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9860 saved_class_ref
= current_class_ref
;
9861 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9864 push_deferring_access_checks(dk_no_deferred
);
9865 /* The default argument expression may cause implicitly defined
9866 member functions to be synthesized, which will result in garbage
9867 collection. We must treat this situation as if we were within
9868 the body of function so as to avoid collecting live data on the
9871 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9872 tf_warning_or_error
, NULL_TREE
,
9873 /*integral_constant_expression_p=*/false);
9875 pop_deferring_access_checks();
9877 /* Restore the "this" pointer. */
9880 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9881 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9884 if (errorcount
+sorrycount
> errs
)
9885 inform (input_location
,
9886 " when instantiating default argument for call to %D", fn
);
9888 /* Make sure the default argument is reasonable. */
9889 arg
= check_default_argument (type
, arg
);
9891 pop_access_scope (fn
);
9896 /* Substitute into all the default arguments for FN. */
9899 tsubst_default_arguments (tree fn
)
9904 tmpl_args
= DECL_TI_ARGS (fn
);
9906 /* If this function is not yet instantiated, we certainly don't need
9907 its default arguments. */
9908 if (uses_template_parms (tmpl_args
))
9910 /* Don't do this again for clones. */
9911 if (DECL_CLONED_FUNCTION_P (fn
))
9914 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9916 arg
= TREE_CHAIN (arg
))
9917 if (TREE_PURPOSE (arg
))
9918 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9920 TREE_PURPOSE (arg
));
9923 /* Substitute the ARGS into the T, which is a _DECL. Return the
9924 result of the substitution. Issue error and warning messages under
9925 control of COMPLAIN. */
9928 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9930 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9931 location_t saved_loc
;
9936 /* Set the filename and linenumber to improve error-reporting. */
9937 saved_loc
= input_location
;
9938 input_location
= DECL_SOURCE_LOCATION (t
);
9940 switch (TREE_CODE (t
))
9944 /* We can get here when processing a member function template,
9945 member class template, or template template parameter. */
9946 tree decl
= DECL_TEMPLATE_RESULT (t
);
9951 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9953 /* Template template parameter is treated here. */
9954 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9955 if (new_type
== error_mark_node
)
9956 RETURN (error_mark_node
);
9957 /* If we get a real template back, return it. This can happen in
9958 the context of most_specialized_class. */
9959 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
9963 DECL_CHAIN (r
) = NULL_TREE
;
9964 TREE_TYPE (r
) = new_type
;
9965 DECL_TEMPLATE_RESULT (r
)
9966 = build_decl (DECL_SOURCE_LOCATION (decl
),
9967 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9968 DECL_TEMPLATE_PARMS (r
)
9969 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9971 TYPE_NAME (new_type
) = r
;
9975 /* We might already have an instance of this template.
9976 The ARGS are for the surrounding class type, so the
9977 full args contain the tsubst'd args for the context,
9978 plus the innermost args from the template decl. */
9979 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9980 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9981 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9982 /* Because this is a template, the arguments will still be
9983 dependent, even after substitution. If
9984 PROCESSING_TEMPLATE_DECL is not set, the dependency
9985 predicates will short-circuit. */
9986 ++processing_template_decl
;
9987 full_args
= tsubst_template_args (tmpl_args
, args
,
9989 --processing_template_decl
;
9990 if (full_args
== error_mark_node
)
9991 RETURN (error_mark_node
);
9993 /* If this is a default template template argument,
9994 tsubst might not have changed anything. */
9995 if (full_args
== tmpl_args
)
9998 hash
= hash_tmpl_and_args (t
, full_args
);
9999 spec
= retrieve_specialization (t
, full_args
, hash
);
10000 if (spec
!= NULL_TREE
)
10006 /* Make a new template decl. It will be similar to the
10007 original, but will record the current template arguments.
10008 We also create a new function declaration, which is just
10009 like the old one, but points to this new template, rather
10010 than the old one. */
10012 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10013 DECL_CHAIN (r
) = NULL_TREE
;
10015 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10017 if (TREE_CODE (decl
) == TYPE_DECL
10018 && !TYPE_DECL_ALIAS_P (decl
))
10021 ++processing_template_decl
;
10022 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10023 --processing_template_decl
;
10024 if (new_type
== error_mark_node
)
10025 RETURN (error_mark_node
);
10027 TREE_TYPE (r
) = new_type
;
10028 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10029 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10030 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10031 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10036 ++processing_template_decl
;
10037 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10038 --processing_template_decl
;
10039 if (new_decl
== error_mark_node
)
10040 RETURN (error_mark_node
);
10042 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10043 DECL_TI_TEMPLATE (new_decl
) = r
;
10044 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10045 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10046 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10049 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10050 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10051 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10053 /* The template parameters for this new template are all the
10054 template parameters for the old template, except the
10055 outermost level of parameters. */
10056 DECL_TEMPLATE_PARMS (r
)
10057 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10060 if (PRIMARY_TEMPLATE_P (t
))
10061 DECL_PRIMARY_TEMPLATE (r
) = r
;
10063 if (TREE_CODE (decl
) != TYPE_DECL
)
10064 /* Record this non-type partial instantiation. */
10065 register_specialization (r
, t
,
10066 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10071 case FUNCTION_DECL
:
10074 tree argvec
= NULL_TREE
;
10082 /* Nobody should be tsubst'ing into non-template functions. */
10083 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10085 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10090 /* If T is not dependent, just return it. We have to
10091 increment PROCESSING_TEMPLATE_DECL because
10092 value_dependent_expression_p assumes that nothing is
10093 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10094 ++processing_template_decl
;
10095 dependent_p
= value_dependent_expression_p (t
);
10096 --processing_template_decl
;
10100 /* Calculate the most general template of which R is a
10101 specialization, and the complete set of arguments used to
10103 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10104 argvec
= tsubst_template_args (DECL_TI_ARGS
10105 (DECL_TEMPLATE_RESULT
10106 (DECL_TI_TEMPLATE (t
))),
10107 args
, complain
, in_decl
);
10108 if (argvec
== error_mark_node
)
10109 RETURN (error_mark_node
);
10111 /* Check to see if we already have this specialization. */
10112 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10113 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10121 /* We can see more levels of arguments than parameters if
10122 there was a specialization of a member template, like
10125 template <class T> struct S { template <class U> void f(); }
10126 template <> template <class U> void S<int>::f(U);
10128 Here, we'll be substituting into the specialization,
10129 because that's where we can find the code we actually
10130 want to generate, but we'll have enough arguments for
10131 the most general template.
10133 We also deal with the peculiar case:
10135 template <class T> struct S {
10136 template <class U> friend void f();
10138 template <class U> void f() {}
10140 template void f<double>();
10142 Here, the ARGS for the instantiation of will be {int,
10143 double}. But, we only need as many ARGS as there are
10144 levels of template parameters in CODE_PATTERN. We are
10145 careful not to get fooled into reducing the ARGS in
10148 template <class T> struct S { template <class U> void f(U); }
10149 template <class T> template <> void S<T>::f(int) {}
10151 which we can spot because the pattern will be a
10152 specialization in this case. */
10153 args_depth
= TMPL_ARGS_DEPTH (args
);
10155 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10156 if (args_depth
> parms_depth
10157 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10158 args
= get_innermost_template_args (args
, parms_depth
);
10162 /* This special case arises when we have something like this:
10164 template <class T> struct S {
10165 friend void f<int>(int, double);
10168 Here, the DECL_TI_TEMPLATE for the friend declaration
10169 will be an IDENTIFIER_NODE. We are being called from
10170 tsubst_friend_function, and we want only to create a
10171 new decl (R) with appropriate types so that we can call
10172 determine_specialization. */
10173 gen_tmpl
= NULL_TREE
;
10176 if (DECL_CLASS_SCOPE_P (t
))
10178 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10182 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10183 complain
, t
, /*entering_scope=*/1);
10188 ctx
= DECL_CONTEXT (t
);
10190 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10191 if (type
== error_mark_node
)
10192 RETURN (error_mark_node
);
10194 /* If we hit excessive deduction depth, the type is bogus even if
10195 it isn't error_mark_node, so don't build a decl. */
10196 if (excessive_deduction_depth
)
10197 RETURN (error_mark_node
);
10199 /* We do NOT check for matching decls pushed separately at this
10200 point, as they may not represent instantiations of this
10201 template, and in any case are considered separate under the
10204 DECL_USE_TEMPLATE (r
) = 0;
10205 TREE_TYPE (r
) = type
;
10206 /* Clear out the mangled name and RTL for the instantiation. */
10207 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10208 SET_DECL_RTL (r
, NULL
);
10209 /* Leave DECL_INITIAL set on deleted instantiations. */
10210 if (!DECL_DELETED_FN (r
))
10211 DECL_INITIAL (r
) = NULL_TREE
;
10212 DECL_CONTEXT (r
) = ctx
;
10214 if (member
&& DECL_CONV_FN_P (r
))
10215 /* Type-conversion operator. Reconstruct the name, in
10216 case it's the name of one of the template's parameters. */
10217 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10219 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10221 DECL_RESULT (r
) = NULL_TREE
;
10223 TREE_STATIC (r
) = 0;
10224 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10225 DECL_EXTERNAL (r
) = 1;
10226 /* If this is an instantiation of a function with internal
10227 linkage, we already know what object file linkage will be
10228 assigned to the instantiation. */
10229 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10230 DECL_DEFER_OUTPUT (r
) = 0;
10231 DECL_CHAIN (r
) = NULL_TREE
;
10232 DECL_PENDING_INLINE_INFO (r
) = 0;
10233 DECL_PENDING_INLINE_P (r
) = 0;
10234 DECL_SAVED_TREE (r
) = NULL_TREE
;
10235 DECL_STRUCT_FUNCTION (r
) = NULL
;
10237 /* We'll re-clone as appropriate in instantiate_template. */
10238 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10240 /* If we aren't complaining now, return on error before we register
10241 the specialization so that we'll complain eventually. */
10242 if ((complain
& tf_error
) == 0
10243 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10244 && !grok_op_properties (r
, /*complain=*/false))
10245 RETURN (error_mark_node
);
10247 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10248 this in the special friend case mentioned above where
10249 GEN_TMPL is NULL. */
10252 DECL_TEMPLATE_INFO (r
)
10253 = build_template_info (gen_tmpl
, argvec
);
10254 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10255 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10257 /* We're not supposed to instantiate default arguments
10258 until they are called, for a template. But, for a
10261 template <class T> void f ()
10262 { extern void g(int i = T()); }
10264 we should do the substitution when the template is
10265 instantiated. We handle the member function case in
10266 instantiate_class_template since the default arguments
10267 might refer to other members of the class. */
10269 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10270 && !uses_template_parms (argvec
))
10271 tsubst_default_arguments (r
);
10274 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10276 /* Copy the list of befriending classes. */
10277 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10279 friends
= &TREE_CHAIN (*friends
))
10281 *friends
= copy_node (*friends
);
10282 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10287 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10289 maybe_retrofit_in_chrg (r
);
10290 if (DECL_CONSTRUCTOR_P (r
))
10291 grok_ctor_properties (ctx
, r
);
10292 if (DECL_INHERITED_CTOR_BASE (r
))
10293 deduce_inheriting_ctor (r
);
10294 /* If this is an instantiation of a member template, clone it.
10295 If it isn't, that'll be handled by
10296 clone_constructors_and_destructors. */
10297 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10298 clone_function_decl (r
, /*update_method_vec_p=*/0);
10300 else if ((complain
& tf_error
) != 0
10301 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10302 && !grok_op_properties (r
, /*complain=*/true))
10303 RETURN (error_mark_node
);
10305 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10306 SET_DECL_FRIEND_CONTEXT (r
,
10307 tsubst (DECL_FRIEND_CONTEXT (t
),
10308 args
, complain
, in_decl
));
10310 /* Possibly limit visibility based on template args. */
10311 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10312 if (DECL_VISIBILITY_SPECIFIED (t
))
10314 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10315 DECL_ATTRIBUTES (r
)
10316 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10318 determine_visibility (r
);
10319 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10320 && !processing_template_decl
)
10321 defaulted_late_check (r
);
10323 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10324 args
, complain
, in_decl
);
10330 tree type
= NULL_TREE
;
10332 tree expanded_types
= NULL_TREE
;
10333 tree prev_r
= NULL_TREE
;
10334 tree first_r
= NULL_TREE
;
10336 if (FUNCTION_PARAMETER_PACK_P (t
))
10338 /* If there is a local specialization that isn't a
10339 parameter pack, it means that we're doing a "simple"
10340 substitution from inside tsubst_pack_expansion. Just
10341 return the local specialization (which will be a single
10343 tree spec
= retrieve_local_specialization (t
);
10345 && TREE_CODE (spec
) == PARM_DECL
10346 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10349 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10350 the parameters in this function parameter pack. */
10351 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10352 complain
, in_decl
);
10353 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10355 len
= TREE_VEC_LENGTH (expanded_types
);
10357 /* Zero-length parameter packs are boring. Just substitute
10360 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10365 /* All we did was update the type. Make a note of that. */
10366 type
= expanded_types
;
10367 expanded_types
= NULL_TREE
;
10371 /* Loop through all of the parameter's we'll build. When T is
10372 a function parameter pack, LEN is the number of expanded
10373 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10375 for (i
= 0; i
< len
; ++i
)
10379 if (DECL_TEMPLATE_PARM_P (t
))
10380 SET_DECL_TEMPLATE_PARM_P (r
);
10382 if (expanded_types
)
10383 /* We're on the Ith parameter of the function parameter
10386 /* An argument of a function parameter pack is not a parameter
10388 FUNCTION_PARAMETER_PACK_P (r
) = false;
10390 /* Get the Ith type. */
10391 type
= TREE_VEC_ELT (expanded_types
, i
);
10393 /* Rename the parameter to include the index. */
10395 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10398 /* We're dealing with a normal parameter. */
10399 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10401 type
= type_decays_to (type
);
10402 TREE_TYPE (r
) = type
;
10403 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10405 if (DECL_INITIAL (r
))
10407 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10408 DECL_INITIAL (r
) = TREE_TYPE (r
);
10410 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10411 complain
, in_decl
);
10414 DECL_CONTEXT (r
) = NULL_TREE
;
10416 if (!DECL_TEMPLATE_PARM_P (r
))
10417 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10419 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10420 args
, complain
, in_decl
);
10422 /* Keep track of the first new parameter we
10423 generate. That's what will be returned to the
10428 /* Build a proper chain of parameters when substituting
10429 into a function parameter pack. */
10431 DECL_CHAIN (prev_r
) = r
;
10434 /* If cp_unevaluated_operand is set, we're just looking for a
10435 single dummy parameter, so don't keep going. */
10436 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10437 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10438 complain
, DECL_CHAIN (t
));
10440 /* FIRST_R contains the start of the chain we've built. */
10450 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10451 if (type
== error_mark_node
)
10452 RETURN (error_mark_node
);
10453 TREE_TYPE (r
) = type
;
10454 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10456 if (DECL_C_BIT_FIELD (r
))
10457 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10458 non-bit-fields DECL_INITIAL is a non-static data member
10459 initializer, which gets deferred instantiation. */
10461 = tsubst_expr (DECL_INITIAL (t
), args
,
10463 /*integral_constant_expression_p=*/true);
10464 else if (DECL_INITIAL (t
))
10466 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10467 NSDMI in perform_member_init. Still set DECL_INITIAL
10468 so that we know there is one. */
10469 DECL_INITIAL (r
) = void_zero_node
;
10470 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10471 retrofit_lang_decl (r
);
10472 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10474 /* We don't have to set DECL_CONTEXT here; it is set by
10475 finish_member_declaration. */
10476 DECL_CHAIN (r
) = NULL_TREE
;
10477 if (VOID_TYPE_P (type
))
10478 error ("instantiation of %q+D as type %qT", r
, type
);
10480 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10481 args
, complain
, in_decl
);
10486 /* We reach here only for member using decls. We also need to check
10487 uses_template_parms because DECL_DEPENDENT_P is not set for a
10488 using-declaration that designates a member of the current
10489 instantiation (c++/53549). */
10490 if (DECL_DEPENDENT_P (t
)
10491 || uses_template_parms (USING_DECL_SCOPE (t
)))
10493 tree scope
= USING_DECL_SCOPE (t
);
10494 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10495 complain
, in_decl
);
10496 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10497 /* Handle 'using T::T'. */
10498 if (TYPE_NAME (scope
)
10499 && name
== TYPE_IDENTIFIER (scope
))
10500 name
= TYPE_IDENTIFIER (inst_scope
);
10501 r
= do_class_using_decl (inst_scope
, name
);
10503 r
= error_mark_node
;
10506 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10507 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10513 DECL_CHAIN (r
) = NULL_TREE
;
10520 tree argvec
= NULL_TREE
;
10521 tree gen_tmpl
= NULL_TREE
;
10523 tree tmpl
= NULL_TREE
;
10525 tree type
= NULL_TREE
;
10528 if (TREE_CODE (t
) == TYPE_DECL
10529 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10531 /* If this is the canonical decl, we don't have to
10532 mess with instantiations, and often we can't (for
10533 typename, template type parms and such). Note that
10534 TYPE_NAME is not correct for the above test if
10535 we've copied the type for a typedef. */
10536 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10537 if (type
== error_mark_node
)
10538 RETURN (error_mark_node
);
10539 r
= TYPE_NAME (type
);
10543 /* Check to see if we already have the specialization we
10546 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10548 /* T is a static data member or namespace-scope entity.
10549 We have to substitute into namespace-scope variables
10550 (even though such entities are never templates) because
10553 template <class T> void f() { extern T t; }
10555 where the entity referenced is not known until
10556 instantiation time. */
10558 ctx
= DECL_CONTEXT (t
);
10559 if (DECL_CLASS_SCOPE_P (t
))
10561 ctx
= tsubst_aggr_type (ctx
, args
,
10563 in_decl
, /*entering_scope=*/1);
10564 /* If CTX is unchanged, then T is in fact the
10565 specialization we want. That situation occurs when
10566 referencing a static data member within in its own
10567 class. We can use pointer equality, rather than
10568 same_type_p, because DECL_CONTEXT is always
10570 if (ctx
== DECL_CONTEXT (t
)
10571 && (TREE_CODE (t
) != TYPE_DECL
10572 /* ... unless T is a member template; in which
10573 case our caller can be willing to create a
10574 specialization of that template represented
10576 || !(DECL_TI_TEMPLATE (t
)
10577 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10583 tmpl
= DECL_TI_TEMPLATE (t
);
10584 gen_tmpl
= most_general_template (tmpl
);
10585 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10586 if (argvec
== error_mark_node
)
10587 RETURN (error_mark_node
);
10588 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10589 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10594 /* A local variable. */
10596 /* Subsequent calls to pushdecl will fill this in. */
10598 spec
= retrieve_local_specialization (t
);
10600 /* If we already have the specialization we need, there is
10601 nothing more to do. */
10608 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10610 /* Just use name lookup to find a member alias for an anonymous
10611 union, but then add it to the hash table. */
10612 r
= lookup_name (DECL_NAME (t
));
10613 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10614 register_local_specialization (r
, t
);
10618 /* Create a new node for the specialization we need. */
10620 if (type
== NULL_TREE
)
10622 if (is_typedef_decl (t
))
10623 type
= DECL_ORIGINAL_TYPE (t
);
10625 type
= TREE_TYPE (t
);
10626 if (TREE_CODE (t
) == VAR_DECL
10627 && VAR_HAD_UNKNOWN_BOUND (t
)
10628 && type
!= error_mark_node
)
10629 type
= strip_array_domain (type
);
10630 type
= tsubst (type
, args
, complain
, in_decl
);
10632 if (TREE_CODE (r
) == VAR_DECL
)
10634 /* Even if the original location is out of scope, the
10635 newly substituted one is not. */
10636 DECL_DEAD_FOR_LOCAL (r
) = 0;
10637 DECL_INITIALIZED_P (r
) = 0;
10638 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10639 if (type
== error_mark_node
)
10640 RETURN (error_mark_node
);
10641 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10643 /* It may seem that this case cannot occur, since:
10648 declares a function, not a variable. However:
10651 template <typename T> void g() { T t; }
10652 template void g<f>();
10654 is an attempt to declare a variable with function
10656 error ("variable %qD has function type",
10657 /* R is not yet sufficiently initialized, so we
10658 just use its name. */
10660 RETURN (error_mark_node
);
10662 type
= complete_type (type
);
10663 /* Wait until cp_finish_decl to set this again, to handle
10664 circular dependency (template/instantiate6.C). */
10665 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10666 type
= check_var_type (DECL_NAME (r
), type
);
10668 if (DECL_HAS_VALUE_EXPR_P (t
))
10670 tree ve
= DECL_VALUE_EXPR (t
);
10671 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10672 /*constant_expression_p=*/false);
10673 if (REFERENCE_REF_P (ve
))
10675 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10676 ve
= TREE_OPERAND (ve
, 0);
10678 SET_DECL_VALUE_EXPR (r
, ve
);
10681 else if (DECL_SELF_REFERENCE_P (t
))
10682 SET_DECL_SELF_REFERENCE_P (r
);
10683 TREE_TYPE (r
) = type
;
10684 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10685 DECL_CONTEXT (r
) = ctx
;
10686 /* Clear out the mangled name and RTL for the instantiation. */
10687 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10688 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10689 SET_DECL_RTL (r
, NULL
);
10690 /* The initializer must not be expanded until it is required;
10691 see [temp.inst]. */
10692 DECL_INITIAL (r
) = NULL_TREE
;
10693 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10694 SET_DECL_RTL (r
, NULL
);
10695 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10696 if (TREE_CODE (r
) == VAR_DECL
)
10698 /* Possibly limit visibility based on template args. */
10699 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10700 if (DECL_VISIBILITY_SPECIFIED (t
))
10702 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10703 DECL_ATTRIBUTES (r
)
10704 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10706 determine_visibility (r
);
10711 /* A static data member declaration is always marked
10712 external when it is declared in-class, even if an
10713 initializer is present. We mimic the non-template
10714 processing here. */
10715 DECL_EXTERNAL (r
) = 1;
10717 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10718 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10719 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10721 else if (cp_unevaluated_operand
)
10723 /* We're substituting this var in a decltype outside of its
10724 scope, such as for a lambda return type. Don't add it to
10725 local_specializations, do perform auto deduction. */
10726 tree auto_node
= type_uses_auto (type
);
10730 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10731 /*constant_expression_p=*/false);
10732 init
= resolve_nondeduced_context (init
);
10733 TREE_TYPE (r
) = type
10734 = do_auto_deduction (type
, init
, auto_node
);
10738 register_local_specialization (r
, t
);
10740 DECL_CHAIN (r
) = NULL_TREE
;
10742 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10744 args
, complain
, in_decl
);
10746 /* Preserve a typedef that names a type. */
10747 if (is_typedef_decl (r
))
10749 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10750 set_underlying_type (r
);
10753 layout_decl (r
, 0);
10758 gcc_unreachable ();
10763 /* Restore the file and line information. */
10764 input_location
= saved_loc
;
10769 /* Substitute into the ARG_TYPES of a function type.
10770 If END is a TREE_CHAIN, leave it and any following types
10774 tsubst_arg_types (tree arg_types
,
10777 tsubst_flags_t complain
,
10780 tree remaining_arg_types
;
10781 tree type
= NULL_TREE
;
10783 tree expanded_args
= NULL_TREE
;
10786 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10789 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10790 args
, end
, complain
, in_decl
);
10791 if (remaining_arg_types
== error_mark_node
)
10792 return error_mark_node
;
10794 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10796 /* For a pack expansion, perform substitution on the
10797 entire expression. Later on, we'll handle the arguments
10799 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10800 args
, complain
, in_decl
);
10802 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10803 /* So that we'll spin through the parameters, one by one. */
10804 i
= TREE_VEC_LENGTH (expanded_args
);
10807 /* We only partially substituted into the parameter
10808 pack. Our type is TYPE_PACK_EXPANSION. */
10809 type
= expanded_args
;
10810 expanded_args
= NULL_TREE
;
10818 type
= TREE_VEC_ELT (expanded_args
, i
);
10820 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10822 if (type
== error_mark_node
)
10823 return error_mark_node
;
10824 if (VOID_TYPE_P (type
))
10826 if (complain
& tf_error
)
10828 error ("invalid parameter type %qT", type
);
10830 error ("in declaration %q+D", in_decl
);
10832 return error_mark_node
;
10835 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10836 top-level qualifiers as required. */
10837 type
= cv_unqualified (type_decays_to (type
));
10839 /* We do not substitute into default arguments here. The standard
10840 mandates that they be instantiated only when needed, which is
10841 done in build_over_call. */
10842 default_arg
= TREE_PURPOSE (arg_types
);
10844 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10846 /* We've instantiated a template before its default arguments
10847 have been parsed. This can happen for a nested template
10848 class, and is not an error unless we require the default
10849 argument in a call of this function. */
10850 remaining_arg_types
=
10851 tree_cons (default_arg
, type
, remaining_arg_types
);
10852 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
10855 remaining_arg_types
=
10856 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10859 return remaining_arg_types
;
10862 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10863 *not* handle the exception-specification for FNTYPE, because the
10864 initial substitution of explicitly provided template parameters
10865 during argument deduction forbids substitution into the
10866 exception-specification:
10870 All references in the function type of the function template to the
10871 corresponding template parameters are replaced by the specified tem-
10872 plate argument values. If a substitution in a template parameter or
10873 in the function type of the function template results in an invalid
10874 type, type deduction fails. [Note: The equivalent substitution in
10875 exception specifications is done only when the function is instanti-
10876 ated, at which point a program is ill-formed if the substitution
10877 results in an invalid type.] */
10880 tsubst_function_type (tree t
,
10882 tsubst_flags_t complain
,
10889 /* The TYPE_CONTEXT is not used for function/method types. */
10890 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10892 /* Substitute the return type. */
10893 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10894 if (return_type
== error_mark_node
)
10895 return error_mark_node
;
10896 /* The standard does not presently indicate that creation of a
10897 function type with an invalid return type is a deduction failure.
10898 However, that is clearly analogous to creating an array of "void"
10899 or a reference to a reference. This is core issue #486. */
10900 if (TREE_CODE (return_type
) == ARRAY_TYPE
10901 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10903 if (complain
& tf_error
)
10905 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10906 error ("function returning an array");
10908 error ("function returning a function");
10910 return error_mark_node
;
10913 /* Substitute the argument types. */
10914 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10915 complain
, in_decl
);
10916 if (arg_types
== error_mark_node
)
10917 return error_mark_node
;
10919 /* Construct a new type node and return it. */
10920 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10922 fntype
= build_function_type (return_type
, arg_types
);
10923 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10927 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10928 if (! MAYBE_CLASS_TYPE_P (r
))
10932 Type deduction may fail for any of the following
10935 -- Attempting to create "pointer to member of T" when T
10936 is not a class type. */
10937 if (complain
& tf_error
)
10938 error ("creating pointer to member function of non-class type %qT",
10940 return error_mark_node
;
10943 fntype
= build_method_type_directly (r
, return_type
,
10944 TREE_CHAIN (arg_types
));
10946 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10951 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10952 ARGS into that specification, and return the substituted
10953 specification. If there is no specification, return NULL_TREE. */
10956 tsubst_exception_specification (tree fntype
,
10958 tsubst_flags_t complain
,
10965 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10966 new_specs
= NULL_TREE
;
10967 if (specs
&& TREE_PURPOSE (specs
))
10969 /* A noexcept-specifier. */
10970 tree expr
= TREE_PURPOSE (specs
);
10971 if (TREE_CODE (expr
) == INTEGER_CST
)
10975 /* Defer instantiation of noexcept-specifiers to avoid
10976 excessive instantiations (c++/49107). */
10977 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10978 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10980 /* We already partially instantiated this member template,
10981 so combine the new args with the old. */
10982 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10983 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10984 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10985 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10989 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10990 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10994 new_specs
= tsubst_copy_and_build
10995 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10996 /*integral_constant_expression_p=*/true);
10997 new_specs
= build_noexcept_spec (new_specs
, complain
);
11001 if (! TREE_VALUE (specs
))
11008 tree expanded_specs
= NULL_TREE
;
11010 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11012 /* Expand the pack expansion type. */
11013 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11017 if (expanded_specs
== error_mark_node
)
11018 return error_mark_node
;
11019 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11020 len
= TREE_VEC_LENGTH (expanded_specs
);
11023 /* We're substituting into a member template, so
11024 we got a TYPE_PACK_EXPANSION back. Add that
11025 expansion and move on. */
11026 gcc_assert (TREE_CODE (expanded_specs
)
11027 == TYPE_PACK_EXPANSION
);
11028 new_specs
= add_exception_specifier (new_specs
,
11031 specs
= TREE_CHAIN (specs
);
11036 for (i
= 0; i
< len
; ++i
)
11038 if (expanded_specs
)
11039 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11041 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11042 if (spec
== error_mark_node
)
11044 new_specs
= add_exception_specifier (new_specs
, spec
,
11048 specs
= TREE_CHAIN (specs
);
11054 /* Take the tree structure T and replace template parameters used
11055 therein with the argument vector ARGS. IN_DECL is an associated
11056 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11057 Issue error and warning messages under control of COMPLAIN. Note
11058 that we must be relatively non-tolerant of extensions here, in
11059 order to preserve conformance; if we allow substitutions that
11060 should not be allowed, we may allow argument deductions that should
11061 not succeed, and therefore report ambiguous overload situations
11062 where there are none. In theory, we could allow the substitution,
11063 but indicate that it should have failed, and allow our caller to
11064 make sure that the right thing happens, but we don't try to do this
11067 This function is used for dealing with types, decls and the like;
11068 for expressions, use tsubst_expr or tsubst_copy. */
11071 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11073 enum tree_code code
;
11074 tree type
, r
= NULL_TREE
;
11076 if (t
== NULL_TREE
|| t
== error_mark_node
11077 || t
== integer_type_node
11078 || t
== void_type_node
11079 || t
== char_type_node
11080 || t
== unknown_type_node
11081 || TREE_CODE (t
) == NAMESPACE_DECL
11082 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11086 return tsubst_decl (t
, args
, complain
);
11088 if (args
== NULL_TREE
)
11091 code
= TREE_CODE (t
);
11093 if (code
== IDENTIFIER_NODE
)
11094 type
= IDENTIFIER_TYPE_VALUE (t
);
11096 type
= TREE_TYPE (t
);
11098 gcc_assert (type
!= unknown_type_node
);
11100 /* Reuse typedefs. We need to do this to handle dependent attributes,
11101 such as attribute aligned. */
11103 && typedef_variant_p (t
))
11105 tree decl
= TYPE_NAME (t
);
11107 if (alias_template_specialization_p (t
))
11109 /* DECL represents an alias template and we want to
11111 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11112 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11113 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11115 else if (DECL_CLASS_SCOPE_P (decl
)
11116 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11117 && uses_template_parms (DECL_CONTEXT (decl
)))
11119 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11120 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11121 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11123 else if (DECL_FUNCTION_SCOPE_P (decl
)
11124 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11125 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11126 r
= retrieve_local_specialization (decl
);
11128 /* The typedef is from a non-template context. */
11134 r
= cp_build_qualified_type_real
11135 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11136 complain
| tf_ignore_bad_quals
);
11141 /* We don't have an instantiation yet, so drop the typedef. */
11142 int quals
= cp_type_quals (t
);
11143 t
= DECL_ORIGINAL_TYPE (decl
);
11144 t
= cp_build_qualified_type_real (t
, quals
,
11145 complain
| tf_ignore_bad_quals
);
11150 && code
!= TYPENAME_TYPE
11151 && code
!= TEMPLATE_TYPE_PARM
11152 && code
!= IDENTIFIER_NODE
11153 && code
!= FUNCTION_TYPE
11154 && code
!= METHOD_TYPE
)
11155 type
= tsubst (type
, args
, complain
, in_decl
);
11156 if (type
== error_mark_node
)
11157 return error_mark_node
;
11163 case ENUMERAL_TYPE
:
11164 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11165 /*entering_scope=*/0);
11168 case IDENTIFIER_NODE
:
11179 if (t
== integer_type_node
)
11182 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11183 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11187 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11189 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11190 /*integral_constant_expression_p=*/false);
11192 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11194 if (TREE_CODE (max
) == NOP_EXPR
11195 && TREE_SIDE_EFFECTS (omax
)
11196 && !TREE_TYPE (max
))
11197 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11199 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11200 with TREE_SIDE_EFFECTS that indicates this is not an integral
11201 constant expression. */
11202 if (processing_template_decl
11203 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11205 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11206 TREE_SIDE_EFFECTS (max
) = 1;
11209 return compute_array_index_type (NULL_TREE
, max
, complain
);
11212 case TEMPLATE_TYPE_PARM
:
11213 case TEMPLATE_TEMPLATE_PARM
:
11214 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11215 case TEMPLATE_PARM_INDEX
:
11220 tree arg
= NULL_TREE
;
11224 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11225 template_parm_level_and_index (t
, &level
, &idx
);
11227 levels
= TMPL_ARGS_DEPTH (args
);
11228 if (level
<= levels
)
11230 arg
= TMPL_ARG (args
, level
, idx
);
11232 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11234 /* See through ARGUMENT_PACK_SELECT arguments. */
11235 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11236 /* If the selected argument is an expansion E, that most
11237 likely means we were called from
11238 gen_elem_of_pack_expansion_instantiation during the
11239 substituting of pack an argument pack (which Ith
11240 element is a pack expansion, where I is
11241 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11242 In this case, the Ith element resulting from this
11243 substituting is going to be a pack expansion, which
11244 pattern is the pattern of E. Let's return the
11246 gen_elem_of_pack_expansion_instantiation will
11247 build the resulting pack expansion from it. */
11248 if (PACK_EXPANSION_P (arg
))
11249 arg
= PACK_EXPANSION_PATTERN (arg
);
11253 if (arg
== error_mark_node
)
11254 return error_mark_node
;
11255 else if (arg
!= NULL_TREE
)
11257 if (ARGUMENT_PACK_P (arg
))
11258 /* If ARG is an argument pack, we don't actually want to
11259 perform a substitution here, because substitutions
11260 for argument packs are only done
11261 element-by-element. We can get to this point when
11262 substituting the type of a non-type template
11263 parameter pack, when that type actually contains
11264 template parameter packs from an outer template, e.g.,
11266 template<typename... Types> struct A {
11267 template<Types... Values> struct B { };
11271 if (code
== TEMPLATE_TYPE_PARM
)
11274 gcc_assert (TYPE_P (arg
));
11276 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11278 return cp_build_qualified_type_real
11279 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11281 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11283 /* We are processing a type constructed from a
11284 template template parameter. */
11285 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11286 args
, complain
, in_decl
);
11287 if (argvec
== error_mark_node
)
11288 return error_mark_node
;
11290 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11291 || TREE_CODE (arg
) == TEMPLATE_DECL
11292 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11294 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11295 /* Consider this code:
11297 template <template <class> class Template>
11299 template <class Arg> using Bind = Template<Arg>;
11302 template <template <class> class Template, class Arg>
11303 using Instantiate = Template<Arg>; //#0
11305 template <template <class> class Template,
11308 Instantiate<Internal<Template>::template Bind,
11311 When #1 is parsed, the
11312 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11313 parameter `Template' in #0 matches the
11314 UNBOUND_CLASS_TEMPLATE representing the argument
11315 `Internal<Template>::template Bind'; We then want
11316 to assemble the type `Bind<Argument>' that can't
11317 be fully created right now, because
11318 `Internal<Template>' not being complete, the Bind
11319 template cannot be looked up in that context. So
11320 we need to "store" `Bind<Argument>' for later
11321 when the context of Bind becomes complete. Let's
11322 store that in a TYPENAME_TYPE. */
11323 return make_typename_type (TYPE_CONTEXT (arg
),
11324 build_nt (TEMPLATE_ID_EXPR
,
11325 TYPE_IDENTIFIER (arg
),
11330 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11331 are resolving nested-types in the signature of a
11332 member function templates. Otherwise ARG is a
11333 TEMPLATE_DECL and is the real template to be
11335 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11336 arg
= TYPE_NAME (arg
);
11338 r
= lookup_template_class (arg
,
11340 DECL_CONTEXT (arg
),
11341 /*entering_scope=*/0,
11343 return cp_build_qualified_type_real
11344 (r
, cp_type_quals (t
), complain
);
11347 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11348 return convert_from_reference (unshare_expr (arg
));
11352 /* This can happen during the attempted tsubst'ing in
11353 unify. This means that we don't yet have any information
11354 about the template parameter in question. */
11357 /* Early in template argument deduction substitution, we don't
11358 want to reduce the level of 'auto', or it will be confused
11359 with a normal template parm in subsequent deduction. */
11360 if (is_auto (t
) && (complain
& tf_partial
))
11363 /* If we get here, we must have been looking at a parm for a
11364 more deeply nested template. Make a new version of this
11365 template parameter, but with a lower level. */
11368 case TEMPLATE_TYPE_PARM
:
11369 case TEMPLATE_TEMPLATE_PARM
:
11370 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11371 if (cp_type_quals (t
))
11373 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11374 r
= cp_build_qualified_type_real
11375 (r
, cp_type_quals (t
),
11376 complain
| (code
== TEMPLATE_TYPE_PARM
11377 ? tf_ignore_bad_quals
: 0));
11382 TEMPLATE_TYPE_PARM_INDEX (r
)
11383 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11384 r
, levels
, args
, complain
);
11385 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11386 TYPE_MAIN_VARIANT (r
) = r
;
11387 TYPE_POINTER_TO (r
) = NULL_TREE
;
11388 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11390 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11391 /* We have reduced the level of the template
11392 template parameter, but not the levels of its
11393 template parameters, so canonical_type_parameter
11394 will not be able to find the canonical template
11395 template parameter for this level. Thus, we
11396 require structural equality checking to compare
11397 TEMPLATE_TEMPLATE_PARMs. */
11398 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11399 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11400 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11402 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11404 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11406 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11407 complain
, in_decl
);
11408 if (argvec
== error_mark_node
)
11409 return error_mark_node
;
11411 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11412 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11417 case TEMPLATE_PARM_INDEX
:
11418 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11422 gcc_unreachable ();
11430 tree purpose
, value
, chain
;
11432 if (t
== void_list_node
)
11435 purpose
= TREE_PURPOSE (t
);
11438 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11439 if (purpose
== error_mark_node
)
11440 return error_mark_node
;
11442 value
= TREE_VALUE (t
);
11445 value
= tsubst (value
, args
, complain
, in_decl
);
11446 if (value
== error_mark_node
)
11447 return error_mark_node
;
11449 chain
= TREE_CHAIN (t
);
11450 if (chain
&& chain
!= void_type_node
)
11452 chain
= tsubst (chain
, args
, complain
, in_decl
);
11453 if (chain
== error_mark_node
)
11454 return error_mark_node
;
11456 if (purpose
== TREE_PURPOSE (t
)
11457 && value
== TREE_VALUE (t
)
11458 && chain
== TREE_CHAIN (t
))
11460 return hash_tree_cons (purpose
, value
, chain
);
11464 /* We should never be tsubsting a binfo. */
11465 gcc_unreachable ();
11468 /* A vector of template arguments. */
11469 gcc_assert (!type
);
11470 return tsubst_template_args (t
, args
, complain
, in_decl
);
11473 case REFERENCE_TYPE
:
11475 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11480 Type deduction may fail for any of the following
11483 -- Attempting to create a pointer to reference type.
11484 -- Attempting to create a reference to a reference type or
11485 a reference to void.
11487 Core issue 106 says that creating a reference to a reference
11488 during instantiation is no longer a cause for failure. We
11489 only enforce this check in strict C++98 mode. */
11490 if ((TREE_CODE (type
) == REFERENCE_TYPE
11491 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11492 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11494 static location_t last_loc
;
11496 /* We keep track of the last time we issued this error
11497 message to avoid spewing a ton of messages during a
11498 single bad template instantiation. */
11499 if (complain
& tf_error
11500 && last_loc
!= input_location
)
11502 if (TREE_CODE (type
) == VOID_TYPE
)
11503 error ("forming reference to void");
11504 else if (code
== POINTER_TYPE
)
11505 error ("forming pointer to reference type %qT", type
);
11507 error ("forming reference to reference type %qT", type
);
11508 last_loc
= input_location
;
11511 return error_mark_node
;
11513 else if (code
== POINTER_TYPE
)
11515 r
= build_pointer_type (type
);
11516 if (TREE_CODE (type
) == METHOD_TYPE
)
11517 r
= build_ptrmemfunc_type (r
);
11519 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11520 /* In C++0x, during template argument substitution, when there is an
11521 attempt to create a reference to a reference type, reference
11522 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11524 "If a template-argument for a template-parameter T names a type
11525 that is a reference to a type A, an attempt to create the type
11526 'lvalue reference to cv T' creates the type 'lvalue reference to
11527 A,' while an attempt to create the type type rvalue reference to
11528 cv T' creates the type T"
11530 r
= cp_build_reference_type
11532 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11534 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11535 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11537 if (r
!= error_mark_node
)
11538 /* Will this ever be needed for TYPE_..._TO values? */
11545 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11546 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11550 Type deduction may fail for any of the following
11553 -- Attempting to create "pointer to member of T" when T
11554 is not a class type. */
11555 if (complain
& tf_error
)
11556 error ("creating pointer to member of non-class type %qT", r
);
11557 return error_mark_node
;
11559 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11561 if (complain
& tf_error
)
11562 error ("creating pointer to member reference type %qT", type
);
11563 return error_mark_node
;
11565 if (TREE_CODE (type
) == VOID_TYPE
)
11567 if (complain
& tf_error
)
11568 error ("creating pointer to member of type void");
11569 return error_mark_node
;
11571 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11572 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11574 /* The type of the implicit object parameter gets its
11575 cv-qualifiers from the FUNCTION_TYPE. */
11577 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11578 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11579 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11583 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11587 case FUNCTION_TYPE
:
11592 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11593 if (fntype
== error_mark_node
)
11594 return error_mark_node
;
11596 /* Substitute the exception specification. */
11597 specs
= tsubst_exception_specification (t
, args
, complain
,
11598 in_decl
, /*defer_ok*/true);
11599 if (specs
== error_mark_node
)
11600 return error_mark_node
;
11602 fntype
= build_exception_variant (fntype
, specs
);
11607 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11608 if (domain
== error_mark_node
)
11609 return error_mark_node
;
11611 /* As an optimization, we avoid regenerating the array type if
11612 it will obviously be the same as T. */
11613 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11616 /* These checks should match the ones in grokdeclarator.
11620 The deduction may fail for any of the following reasons:
11622 -- Attempting to create an array with an element type that
11623 is void, a function type, or a reference type, or [DR337]
11624 an abstract class type. */
11625 if (TREE_CODE (type
) == VOID_TYPE
11626 || TREE_CODE (type
) == FUNCTION_TYPE
11627 || TREE_CODE (type
) == REFERENCE_TYPE
)
11629 if (complain
& tf_error
)
11630 error ("creating array of %qT", type
);
11631 return error_mark_node
;
11633 if (ABSTRACT_CLASS_TYPE_P (type
))
11635 if (complain
& tf_error
)
11636 error ("creating array of %qT, which is an abstract class type",
11638 return error_mark_node
;
11641 r
= build_cplus_array_type (type
, domain
);
11643 if (TYPE_USER_ALIGN (t
))
11645 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11646 TYPE_USER_ALIGN (r
) = 1;
11652 case TYPENAME_TYPE
:
11654 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11655 in_decl
, /*entering_scope=*/1);
11656 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11657 complain
, in_decl
);
11659 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11660 return error_mark_node
;
11662 if (!MAYBE_CLASS_TYPE_P (ctx
))
11664 if (complain
& tf_error
)
11665 error ("%qT is not a class, struct, or union type", ctx
);
11666 return error_mark_node
;
11668 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11670 /* Normally, make_typename_type does not require that the CTX
11671 have complete type in order to allow things like:
11673 template <class T> struct S { typename S<T>::X Y; };
11675 But, such constructs have already been resolved by this
11676 point, so here CTX really should have complete type, unless
11677 it's a partial instantiation. */
11678 ctx
= complete_type (ctx
);
11679 if (!COMPLETE_TYPE_P (ctx
))
11681 if (complain
& tf_error
)
11682 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11683 return error_mark_node
;
11687 f
= make_typename_type (ctx
, f
, typename_type
,
11688 complain
| tf_keep_type_decl
);
11689 if (f
== error_mark_node
)
11691 if (TREE_CODE (f
) == TYPE_DECL
)
11693 complain
|= tf_ignore_bad_quals
;
11697 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11699 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11701 if (complain
& tf_error
)
11702 error ("%qT resolves to %qT, which is not an enumeration type",
11705 return error_mark_node
;
11707 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11709 if (complain
& tf_error
)
11710 error ("%qT resolves to %qT, which is is not a class type",
11713 return error_mark_node
;
11717 return cp_build_qualified_type_real
11718 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11721 case UNBOUND_CLASS_TEMPLATE
:
11723 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11724 in_decl
, /*entering_scope=*/1);
11725 tree name
= TYPE_IDENTIFIER (t
);
11726 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11728 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11729 return error_mark_node
;
11732 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11733 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11740 ++cp_unevaluated_operand
;
11741 ++c_inhibit_evaluation_warnings
;
11743 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11745 /*integral_constant_expression_p=*/false);
11747 --cp_unevaluated_operand
;
11748 --c_inhibit_evaluation_warnings
;
11750 type
= finish_typeof (type
);
11751 return cp_build_qualified_type_real (type
,
11753 | cp_type_quals (type
),
11757 case DECLTYPE_TYPE
:
11761 ++cp_unevaluated_operand
;
11762 ++c_inhibit_evaluation_warnings
;
11764 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11766 /*integral_constant_expression_p=*/false);
11768 --cp_unevaluated_operand
;
11769 --c_inhibit_evaluation_warnings
;
11771 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11772 type
= lambda_capture_field_type (type
);
11773 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11774 type
= lambda_proxy_type (type
);
11776 type
= finish_decltype_type
11777 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11778 return cp_build_qualified_type_real (type
,
11780 | cp_type_quals (type
),
11784 case UNDERLYING_TYPE
:
11786 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11787 complain
, in_decl
);
11788 return finish_underlying_type (type
);
11791 case TYPE_ARGUMENT_PACK
:
11792 case NONTYPE_ARGUMENT_PACK
:
11794 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11796 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11800 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11802 /* For template nontype argument packs, also substitute into
11804 if (code
== NONTYPE_ARGUMENT_PACK
)
11805 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11823 /* We should use one of the expression tsubsts for these codes. */
11824 gcc_unreachable ();
11827 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11828 return error_mark_node
;
11832 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11833 type of the expression on the left-hand side of the "." or "->"
11837 tsubst_baselink (tree baselink
, tree object_type
,
11838 tree args
, tsubst_flags_t complain
, tree in_decl
)
11841 tree qualifying_scope
;
11844 tree template_args
= 0;
11845 bool template_id_p
= false;
11846 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11848 /* A baselink indicates a function from a base class. Both the
11849 BASELINK_ACCESS_BINFO and the base class referenced may
11850 indicate bases of the template class, rather than the
11851 instantiated class. In addition, lookups that were not
11852 ambiguous before may be ambiguous now. Therefore, we perform
11853 the lookup again. */
11854 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11855 qualifying_scope
= tsubst (qualifying_scope
, args
,
11856 complain
, in_decl
);
11857 fns
= BASELINK_FUNCTIONS (baselink
);
11858 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11859 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11861 template_id_p
= true;
11862 template_args
= TREE_OPERAND (fns
, 1);
11863 fns
= TREE_OPERAND (fns
, 0);
11865 template_args
= tsubst_template_args (template_args
, args
,
11866 complain
, in_decl
);
11868 name
= DECL_NAME (get_first_fn (fns
));
11869 if (IDENTIFIER_TYPENAME_P (name
))
11870 name
= mangle_conv_op_name_for_type (optype
);
11871 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11873 return error_mark_node
;
11875 /* If lookup found a single function, mark it as used at this
11876 point. (If it lookup found multiple functions the one selected
11877 later by overload resolution will be marked as used at that
11879 if (BASELINK_P (baselink
))
11880 fns
= BASELINK_FUNCTIONS (baselink
);
11881 if (!template_id_p
&& !really_overloaded_fn (fns
))
11882 mark_used (OVL_CURRENT (fns
));
11884 /* Add back the template arguments, if present. */
11885 if (BASELINK_P (baselink
) && template_id_p
)
11886 BASELINK_FUNCTIONS (baselink
)
11887 = build_nt (TEMPLATE_ID_EXPR
,
11888 BASELINK_FUNCTIONS (baselink
),
11890 /* Update the conversion operator type. */
11891 BASELINK_OPTYPE (baselink
) = optype
;
11894 object_type
= current_class_type
;
11897 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11903 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11904 true if the qualified-id will be a postfix-expression in-and-of
11905 itself; false if more of the postfix-expression follows the
11906 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11910 tsubst_qualified_id (tree qualified_id
, tree args
,
11911 tsubst_flags_t complain
, tree in_decl
,
11912 bool done
, bool address_p
)
11918 tree template_args
;
11919 location_t loc
= UNKNOWN_LOCATION
;
11921 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11923 /* Figure out what name to look up. */
11924 name
= TREE_OPERAND (qualified_id
, 1);
11925 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11927 is_template
= true;
11928 loc
= EXPR_LOCATION (name
);
11929 template_args
= TREE_OPERAND (name
, 1);
11931 template_args
= tsubst_template_args (template_args
, args
,
11932 complain
, in_decl
);
11933 name
= TREE_OPERAND (name
, 0);
11937 is_template
= false;
11938 template_args
= NULL_TREE
;
11941 /* Substitute into the qualifying scope. When there are no ARGS, we
11942 are just trying to simplify a non-dependent expression. In that
11943 case the qualifying scope may be dependent, and, in any case,
11944 substituting will not help. */
11945 scope
= TREE_OPERAND (qualified_id
, 0);
11948 scope
= tsubst (scope
, args
, complain
, in_decl
);
11949 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11954 if (dependent_scope_p (scope
))
11957 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11958 return build_qualified_name (NULL_TREE
, scope
, expr
,
11959 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11962 if (!BASELINK_P (name
) && !DECL_P (expr
))
11964 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11966 /* A BIT_NOT_EXPR is used to represent a destructor. */
11967 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11969 error ("qualifying type %qT does not match destructor name ~%qT",
11970 scope
, TREE_OPERAND (expr
, 0));
11971 expr
= error_mark_node
;
11974 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11975 /*is_type_p=*/0, false);
11978 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11979 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11980 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11982 if (complain
& tf_error
)
11984 error ("dependent-name %qE is parsed as a non-type, but "
11985 "instantiation yields a type", qualified_id
);
11986 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11988 return error_mark_node
;
11994 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11996 /* Remember that there was a reference to this entity. */
12000 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12002 if (complain
& tf_error
)
12003 qualified_name_lookup_error (scope
,
12004 TREE_OPERAND (qualified_id
, 1),
12005 expr
, input_location
);
12006 return error_mark_node
;
12010 expr
= lookup_template_function (expr
, template_args
);
12012 if (expr
== error_mark_node
&& complain
& tf_error
)
12013 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12014 expr
, input_location
);
12015 else if (TYPE_P (scope
))
12017 expr
= (adjust_result_of_qualified_name_lookup
12018 (expr
, scope
, current_class_type
));
12019 expr
= (finish_qualified_id_expr
12020 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12021 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12022 /*template_arg_p=*/false));
12025 /* Expressions do not generally have reference type. */
12026 if (TREE_CODE (expr
) != SCOPE_REF
12027 /* However, if we're about to form a pointer-to-member, we just
12028 want the referenced member referenced. */
12029 && TREE_CODE (expr
) != OFFSET_REF
)
12030 expr
= convert_from_reference (expr
);
12035 /* Like tsubst, but deals with expressions. This function just replaces
12036 template parms; to finish processing the resultant expression, use
12037 tsubst_copy_and_build or tsubst_expr. */
12040 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12042 enum tree_code code
;
12045 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12048 code
= TREE_CODE (t
);
12053 r
= retrieve_local_specialization (t
);
12055 if (r
== NULL_TREE
)
12057 /* We get here for a use of 'this' in an NSDMI. */
12058 if (DECL_NAME (t
) == this_identifier
12059 && at_function_scope_p ()
12060 && DECL_CONSTRUCTOR_P (current_function_decl
))
12061 return current_class_ptr
;
12063 /* This can happen for a parameter name used later in a function
12064 declaration (such as in a late-specified return type). Just
12065 make a dummy decl, since it's only used for its type. */
12066 gcc_assert (cp_unevaluated_operand
!= 0);
12067 r
= tsubst_decl (t
, args
, complain
);
12068 /* Give it the template pattern as its context; its true context
12069 hasn't been instantiated yet and this is good enough for
12071 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12074 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12075 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12084 if (DECL_TEMPLATE_PARM_P (t
))
12085 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12086 /* There is no need to substitute into namespace-scope
12088 if (DECL_NAMESPACE_SCOPE_P (t
))
12090 /* If ARGS is NULL, then T is known to be non-dependent. */
12091 if (args
== NULL_TREE
)
12092 return integral_constant_value (t
);
12094 /* Unfortunately, we cannot just call lookup_name here.
12097 template <int I> int f() {
12099 struct S { void g() { E e = a; } };
12102 When we instantiate f<7>::S::g(), say, lookup_name is not
12103 clever enough to find f<7>::a. */
12105 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12106 /*entering_scope=*/0);
12108 for (v
= TYPE_VALUES (enum_type
);
12110 v
= TREE_CHAIN (v
))
12111 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12112 return TREE_VALUE (v
);
12114 /* We didn't find the name. That should never happen; if
12115 name-lookup found it during preliminary parsing, we
12116 should find it again here during instantiation. */
12117 gcc_unreachable ();
12122 if (DECL_CONTEXT (t
))
12126 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12127 /*entering_scope=*/1);
12128 if (ctx
!= DECL_CONTEXT (t
))
12130 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12133 if (complain
& tf_error
)
12134 error ("using invalid field %qD", t
);
12135 return error_mark_node
;
12144 case FUNCTION_DECL
:
12145 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12146 || local_variable_p (t
))
12147 t
= tsubst (t
, args
, complain
, in_decl
);
12151 case NAMESPACE_DECL
:
12155 /* An OVERLOAD will always be a non-dependent overload set; an
12156 overload set from function scope will just be represented with an
12157 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12158 gcc_assert (!uses_template_parms (t
));
12162 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12164 case TEMPLATE_DECL
:
12165 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12166 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12167 args
, complain
, in_decl
);
12168 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12169 return tsubst (t
, args
, complain
, in_decl
);
12170 else if (DECL_CLASS_SCOPE_P (t
)
12171 && uses_template_parms (DECL_CONTEXT (t
)))
12173 /* Template template argument like the following example need
12176 template <template <class> class TT> struct C {};
12177 template <class T> struct D {
12178 template <class U> struct E {};
12183 We are processing the template argument `E' in #1 for
12184 the template instantiation #2. Originally, `E' is a
12185 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12186 have to substitute this with one having context `D<int>'. */
12188 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12189 return lookup_field (context
, DECL_NAME(t
), 0, false);
12192 /* Ordinary template template argument. */
12196 case REINTERPRET_CAST_EXPR
:
12197 case CONST_CAST_EXPR
:
12198 case STATIC_CAST_EXPR
:
12199 case DYNAMIC_CAST_EXPR
:
12200 case IMPLICIT_CONV_EXPR
:
12204 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12205 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12208 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12211 tree expanded
, op
= TREE_OPERAND (t
, 0);
12214 if (SIZEOF_EXPR_TYPE_P (t
))
12215 op
= TREE_TYPE (op
);
12217 ++cp_unevaluated_operand
;
12218 ++c_inhibit_evaluation_warnings
;
12219 /* We only want to compute the number of arguments. */
12220 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12221 --cp_unevaluated_operand
;
12222 --c_inhibit_evaluation_warnings
;
12224 if (TREE_CODE (expanded
) == TREE_VEC
)
12225 len
= TREE_VEC_LENGTH (expanded
);
12227 if (expanded
== error_mark_node
)
12228 return error_mark_node
;
12229 else if (PACK_EXPANSION_P (expanded
)
12230 || (TREE_CODE (expanded
) == TREE_VEC
12232 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12234 if (TREE_CODE (expanded
) == TREE_VEC
)
12235 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12237 if (TYPE_P (expanded
))
12238 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12239 complain
& tf_error
);
12241 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12242 complain
& tf_error
);
12245 return build_int_cst (size_type_node
, len
);
12247 if (SIZEOF_EXPR_TYPE_P (t
))
12249 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12250 args
, complain
, in_decl
);
12251 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12252 r
= build1 (SIZEOF_EXPR
,
12253 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12254 SIZEOF_EXPR_TYPE_P (r
) = 1;
12261 case TRUTH_NOT_EXPR
:
12264 case UNARY_PLUS_EXPR
: /* Unary + */
12266 case AT_ENCODE_EXPR
:
12270 case REALPART_EXPR
:
12271 case IMAGPART_EXPR
:
12273 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12274 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12276 case COMPONENT_REF
:
12281 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12282 name
= TREE_OPERAND (t
, 1);
12283 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12285 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12286 complain
, in_decl
);
12287 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12289 else if (TREE_CODE (name
) == SCOPE_REF
12290 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12292 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12293 complain
, in_decl
);
12294 name
= TREE_OPERAND (name
, 1);
12295 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12296 complain
, in_decl
);
12297 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12298 name
= build_qualified_name (/*type=*/NULL_TREE
,
12300 /*template_p=*/false);
12302 else if (BASELINK_P (name
))
12303 name
= tsubst_baselink (name
,
12304 non_reference (TREE_TYPE (object
)),
12308 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12309 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12315 case TRUNC_DIV_EXPR
:
12316 case CEIL_DIV_EXPR
:
12317 case FLOOR_DIV_EXPR
:
12318 case ROUND_DIV_EXPR
:
12319 case EXACT_DIV_EXPR
:
12323 case TRUNC_MOD_EXPR
:
12324 case FLOOR_MOD_EXPR
:
12325 case TRUTH_ANDIF_EXPR
:
12326 case TRUTH_ORIF_EXPR
:
12327 case TRUTH_AND_EXPR
:
12328 case TRUTH_OR_EXPR
:
12341 case COMPOUND_EXPR
:
12344 case PREDECREMENT_EXPR
:
12345 case PREINCREMENT_EXPR
:
12346 case POSTDECREMENT_EXPR
:
12347 case POSTINCREMENT_EXPR
:
12349 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12350 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12353 return build_qualified_name (/*type=*/NULL_TREE
,
12354 tsubst_copy (TREE_OPERAND (t
, 0),
12355 args
, complain
, in_decl
),
12356 tsubst_copy (TREE_OPERAND (t
, 1),
12357 args
, complain
, in_decl
),
12358 QUALIFIED_NAME_IS_TEMPLATE (t
));
12363 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12364 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12365 NULL_TREE
, NULL_TREE
);
12369 int n
= VL_EXP_OPERAND_LENGTH (t
);
12370 tree result
= build_vl_exp (CALL_EXPR
, n
);
12372 for (i
= 0; i
< n
; i
++)
12373 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12374 complain
, in_decl
);
12380 case PSEUDO_DTOR_EXPR
:
12383 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12384 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12385 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12386 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12393 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12394 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12395 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12396 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12403 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12404 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12405 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12406 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12410 case TEMPLATE_ID_EXPR
:
12412 /* Substituted template arguments */
12413 tree fn
= TREE_OPERAND (t
, 0);
12414 tree targs
= TREE_OPERAND (t
, 1);
12416 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12418 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12420 return lookup_template_function (fn
, targs
);
12425 tree purpose
, value
, chain
;
12427 if (t
== void_list_node
)
12430 purpose
= TREE_PURPOSE (t
);
12432 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12433 value
= TREE_VALUE (t
);
12435 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12436 chain
= TREE_CHAIN (t
);
12437 if (chain
&& chain
!= void_type_node
)
12438 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12439 if (purpose
== TREE_PURPOSE (t
)
12440 && value
== TREE_VALUE (t
)
12441 && chain
== TREE_CHAIN (t
))
12443 return tree_cons (purpose
, value
, chain
);
12448 case ENUMERAL_TYPE
:
12450 case TEMPLATE_TYPE_PARM
:
12451 case TEMPLATE_TEMPLATE_PARM
:
12452 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12453 case TEMPLATE_PARM_INDEX
:
12455 case REFERENCE_TYPE
:
12457 case FUNCTION_TYPE
:
12460 case TYPENAME_TYPE
:
12461 case UNBOUND_CLASS_TEMPLATE
:
12463 case DECLTYPE_TYPE
:
12465 return tsubst (t
, args
, complain
, in_decl
);
12467 case IDENTIFIER_NODE
:
12468 if (IDENTIFIER_TYPENAME_P (t
))
12470 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12471 return mangle_conv_op_name_for_type (new_type
);
12477 /* This is handled by tsubst_copy_and_build. */
12478 gcc_unreachable ();
12481 return build_x_va_arg (EXPR_LOCATION (t
),
12482 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12484 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12486 case CLEANUP_POINT_EXPR
:
12487 /* We shouldn't have built any of these during initial template
12488 generation. Instead, they should be built during instantiation
12489 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12490 gcc_unreachable ();
12494 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12495 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12496 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12497 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12498 mark_used (TREE_OPERAND (r
, 1));
12501 case EXPR_PACK_EXPANSION
:
12502 error ("invalid use of pack expansion expression");
12503 return error_mark_node
;
12505 case NONTYPE_ARGUMENT_PACK
:
12506 error ("use %<...%> to expand argument pack");
12507 return error_mark_node
;
12514 /* Instantiate any typedefs in the type. */
12515 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12516 r
= fold_convert (type
, t
);
12517 gcc_assert (TREE_CODE (r
) == code
);
12522 /* These can sometimes show up in a partial instantiation, but never
12523 involve template parms. */
12524 gcc_assert (!uses_template_parms (t
));
12528 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12529 gcc_checking_assert (false);
12534 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12537 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12540 tree new_clauses
= NULL
, nc
, oc
;
12542 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12544 nc
= copy_node (oc
);
12545 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12548 switch (OMP_CLAUSE_CODE (nc
))
12550 case OMP_CLAUSE_LASTPRIVATE
:
12551 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12553 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12554 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12555 in_decl
, /*integral_constant_expression_p=*/false);
12556 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12557 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12560 case OMP_CLAUSE_PRIVATE
:
12561 case OMP_CLAUSE_SHARED
:
12562 case OMP_CLAUSE_FIRSTPRIVATE
:
12563 case OMP_CLAUSE_REDUCTION
:
12564 case OMP_CLAUSE_COPYIN
:
12565 case OMP_CLAUSE_COPYPRIVATE
:
12566 case OMP_CLAUSE_IF
:
12567 case OMP_CLAUSE_NUM_THREADS
:
12568 case OMP_CLAUSE_SCHEDULE
:
12569 case OMP_CLAUSE_COLLAPSE
:
12570 case OMP_CLAUSE_FINAL
:
12571 OMP_CLAUSE_OPERAND (nc
, 0)
12572 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12573 in_decl
, /*integral_constant_expression_p=*/false);
12575 case OMP_CLAUSE_NOWAIT
:
12576 case OMP_CLAUSE_ORDERED
:
12577 case OMP_CLAUSE_DEFAULT
:
12578 case OMP_CLAUSE_UNTIED
:
12579 case OMP_CLAUSE_MERGEABLE
:
12582 gcc_unreachable ();
12586 return finish_omp_clauses (nreverse (new_clauses
));
12589 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12592 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12595 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12597 tree purpose
, value
, chain
;
12602 if (TREE_CODE (t
) != TREE_LIST
)
12603 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12604 /*function_p=*/false,
12605 /*integral_constant_expression_p=*/false);
12607 if (t
== void_list_node
)
12610 purpose
= TREE_PURPOSE (t
);
12612 purpose
= RECUR (purpose
);
12613 value
= TREE_VALUE (t
);
12616 if (TREE_CODE (value
) != LABEL_DECL
)
12617 value
= RECUR (value
);
12620 value
= lookup_label (DECL_NAME (value
));
12621 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12622 TREE_USED (value
) = 1;
12625 chain
= TREE_CHAIN (t
);
12626 if (chain
&& chain
!= void_type_node
)
12627 chain
= RECUR (chain
);
12628 return tree_cons (purpose
, value
, chain
);
12632 /* Substitute one OMP_FOR iterator. */
12635 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12636 tree condv
, tree incrv
, tree
*clauses
,
12637 tree args
, tsubst_flags_t complain
, tree in_decl
,
12638 bool integral_constant_expression_p
)
12640 #define RECUR(NODE) \
12641 tsubst_expr ((NODE), args, complain, in_decl, \
12642 integral_constant_expression_p)
12643 tree decl
, init
, cond
, incr
;
12646 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12647 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12648 decl
= TREE_OPERAND (init
, 0);
12649 init
= TREE_OPERAND (init
, 1);
12650 /* Do this before substituting into decl to handle 'auto'. */
12651 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12652 init
= RECUR (init
);
12653 decl
= RECUR (decl
);
12656 gcc_assert (!processing_template_decl
);
12657 init
= DECL_INITIAL (decl
);
12658 DECL_INITIAL (decl
) = NULL_TREE
;
12661 gcc_assert (!type_dependent_expression_p (decl
));
12663 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12665 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12666 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12667 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12668 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12669 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12670 RECUR (TREE_OPERAND (incr
, 1)),
12673 incr
= RECUR (incr
);
12674 TREE_VEC_ELT (declv
, i
) = decl
;
12675 TREE_VEC_ELT (initv
, i
) = init
;
12676 TREE_VEC_ELT (condv
, i
) = cond
;
12677 TREE_VEC_ELT (incrv
, i
) = incr
;
12681 if (init
&& !init_decl
)
12684 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12686 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12687 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12688 && OMP_CLAUSE_DECL (c
) == decl
)
12690 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12691 && OMP_CLAUSE_DECL (c
) == decl
)
12692 error ("iteration variable %qD should not be firstprivate", decl
);
12693 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12694 && OMP_CLAUSE_DECL (c
) == decl
)
12695 error ("iteration variable %qD should not be reduction", decl
);
12699 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12700 OMP_CLAUSE_DECL (c
) = decl
;
12701 c
= finish_omp_clauses (c
);
12704 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12709 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12710 if (COMPARISON_CLASS_P (cond
))
12711 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12712 RECUR (TREE_OPERAND (cond
, 0)),
12713 RECUR (TREE_OPERAND (cond
, 1)));
12715 cond
= RECUR (cond
);
12716 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12717 switch (TREE_CODE (incr
))
12719 case PREINCREMENT_EXPR
:
12720 case PREDECREMENT_EXPR
:
12721 case POSTINCREMENT_EXPR
:
12722 case POSTDECREMENT_EXPR
:
12723 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12724 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12727 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12728 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12730 tree rhs
= TREE_OPERAND (incr
, 1);
12731 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12732 RECUR (TREE_OPERAND (incr
, 0)),
12733 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12734 RECUR (TREE_OPERAND (rhs
, 0)),
12735 RECUR (TREE_OPERAND (rhs
, 1))));
12738 incr
= RECUR (incr
);
12741 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12742 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12744 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12745 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12746 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12747 TREE_TYPE (decl
), lhs
,
12748 RECUR (TREE_OPERAND (incr
, 2))));
12750 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12751 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12752 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12754 tree rhs
= TREE_OPERAND (incr
, 2);
12755 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12756 RECUR (TREE_OPERAND (incr
, 0)),
12757 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12758 RECUR (TREE_OPERAND (rhs
, 0)),
12759 RECUR (TREE_OPERAND (rhs
, 1))));
12762 incr
= RECUR (incr
);
12765 incr
= RECUR (incr
);
12769 TREE_VEC_ELT (declv
, i
) = decl
;
12770 TREE_VEC_ELT (initv
, i
) = init
;
12771 TREE_VEC_ELT (condv
, i
) = cond
;
12772 TREE_VEC_ELT (incrv
, i
) = incr
;
12776 /* Like tsubst_copy for expressions, etc. but also does semantic
12780 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12781 bool integral_constant_expression_p
)
12783 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12784 #define RECUR(NODE) \
12785 tsubst_expr ((NODE), args, complain, in_decl, \
12786 integral_constant_expression_p)
12792 if (t
== NULL_TREE
|| t
== error_mark_node
)
12795 loc
= input_location
;
12796 if (EXPR_HAS_LOCATION (t
))
12797 input_location
= EXPR_LOCATION (t
);
12798 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12799 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12801 switch (TREE_CODE (t
))
12803 case STATEMENT_LIST
:
12805 tree_stmt_iterator i
;
12806 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12807 RECUR (tsi_stmt (i
));
12811 case CTOR_INITIALIZER
:
12812 finish_mem_initializers (tsubst_initializer_list
12813 (TREE_OPERAND (t
, 0), args
));
12817 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12821 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12822 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12823 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12825 finish_expr_stmt (tmp
);
12829 do_using_directive (USING_STMT_NAMESPACE (t
));
12834 tree decl
, pattern_decl
;
12837 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12838 if (TREE_CODE (decl
) == LABEL_DECL
)
12839 finish_label_decl (DECL_NAME (decl
));
12840 else if (TREE_CODE (decl
) == USING_DECL
)
12842 tree scope
= USING_DECL_SCOPE (decl
);
12843 tree name
= DECL_NAME (decl
);
12846 scope
= tsubst (scope
, args
, complain
, in_decl
);
12847 decl
= lookup_qualified_name (scope
, name
,
12848 /*is_type_p=*/false,
12849 /*complain=*/false);
12850 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12851 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12853 do_local_using_decl (decl
, scope
, name
);
12857 init
= DECL_INITIAL (decl
);
12858 decl
= tsubst (decl
, args
, complain
, in_decl
);
12859 if (decl
!= error_mark_node
)
12861 /* By marking the declaration as instantiated, we avoid
12862 trying to instantiate it. Since instantiate_decl can't
12863 handle local variables, and since we've already done
12864 all that needs to be done, that's the right thing to
12866 if (TREE_CODE (decl
) == VAR_DECL
)
12867 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12868 if (TREE_CODE (decl
) == VAR_DECL
12869 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12870 /* Anonymous aggregates are a special case. */
12871 finish_anon_union (decl
);
12872 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12874 DECL_CONTEXT (decl
) = current_function_decl
;
12875 if (DECL_NAME (decl
) == this_identifier
)
12877 tree lam
= DECL_CONTEXT (current_function_decl
);
12878 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
12879 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
12881 insert_capture_proxy (decl
);
12883 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12884 /* We already did a pushtag. */;
12887 int const_init
= false;
12888 maybe_push_decl (decl
);
12889 if (TREE_CODE (decl
) == VAR_DECL
12890 && DECL_PRETTY_FUNCTION_P (decl
))
12892 /* For __PRETTY_FUNCTION__ we have to adjust the
12894 const char *const name
12895 = cxx_printable_name (current_function_decl
, 2);
12896 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12900 tree t
= RECUR (init
);
12904 /* If we had an initializer but it
12905 instantiated to nothing,
12906 value-initialize the object. This will
12907 only occur when the initializer was a
12908 pack expansion where the parameter packs
12909 used in that expansion were of length
12911 init
= build_value_init (TREE_TYPE (decl
),
12913 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12914 init
= get_target_expr_sfinae (init
, complain
);
12920 if (TREE_CODE (decl
) == VAR_DECL
)
12921 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12923 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12932 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12933 RECUR (FOR_INIT_STMT (t
));
12934 finish_for_init_stmt (stmt
);
12935 tmp
= RECUR (FOR_COND (t
));
12936 finish_for_cond (tmp
, stmt
);
12937 tmp
= RECUR (FOR_EXPR (t
));
12938 finish_for_expr (tmp
, stmt
);
12939 RECUR (FOR_BODY (t
));
12940 finish_for_stmt (stmt
);
12943 case RANGE_FOR_STMT
:
12946 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12947 decl
= RANGE_FOR_DECL (t
);
12948 decl
= tsubst (decl
, args
, complain
, in_decl
);
12949 maybe_push_decl (decl
);
12950 expr
= RECUR (RANGE_FOR_EXPR (t
));
12951 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12952 RECUR (RANGE_FOR_BODY (t
));
12953 finish_for_stmt (stmt
);
12958 stmt
= begin_while_stmt ();
12959 tmp
= RECUR (WHILE_COND (t
));
12960 finish_while_stmt_cond (tmp
, stmt
);
12961 RECUR (WHILE_BODY (t
));
12962 finish_while_stmt (stmt
);
12966 stmt
= begin_do_stmt ();
12967 RECUR (DO_BODY (t
));
12968 finish_do_body (stmt
);
12969 tmp
= RECUR (DO_COND (t
));
12970 finish_do_stmt (tmp
, stmt
);
12974 stmt
= begin_if_stmt ();
12975 tmp
= RECUR (IF_COND (t
));
12976 finish_if_stmt_cond (tmp
, stmt
);
12977 RECUR (THEN_CLAUSE (t
));
12978 finish_then_clause (stmt
);
12980 if (ELSE_CLAUSE (t
))
12982 begin_else_clause (stmt
);
12983 RECUR (ELSE_CLAUSE (t
));
12984 finish_else_clause (stmt
);
12987 finish_if_stmt (stmt
);
12991 if (BIND_EXPR_BODY_BLOCK (t
))
12992 stmt
= begin_function_body ();
12994 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12995 ? BCS_TRY_BLOCK
: 0);
12997 RECUR (BIND_EXPR_BODY (t
));
12999 if (BIND_EXPR_BODY_BLOCK (t
))
13000 finish_function_body (stmt
);
13002 finish_compound_stmt (stmt
);
13006 finish_break_stmt ();
13009 case CONTINUE_STMT
:
13010 finish_continue_stmt ();
13014 stmt
= begin_switch_stmt ();
13015 tmp
= RECUR (SWITCH_STMT_COND (t
));
13016 finish_switch_cond (tmp
, stmt
);
13017 RECUR (SWITCH_STMT_BODY (t
));
13018 finish_switch_stmt (stmt
);
13021 case CASE_LABEL_EXPR
:
13022 finish_case_label (EXPR_LOCATION (t
),
13023 RECUR (CASE_LOW (t
)),
13024 RECUR (CASE_HIGH (t
)));
13029 tree decl
= LABEL_EXPR_LABEL (t
);
13032 label
= finish_label_stmt (DECL_NAME (decl
));
13033 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13034 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13039 tmp
= GOTO_DESTINATION (t
);
13040 if (TREE_CODE (tmp
) != LABEL_DECL
)
13041 /* Computed goto's must be tsubst'd into. On the other hand,
13042 non-computed gotos must not be; the identifier in question
13043 will have no binding. */
13046 tmp
= DECL_NAME (tmp
);
13047 finish_goto_stmt (tmp
);
13051 tmp
= finish_asm_stmt
13052 (ASM_VOLATILE_P (t
),
13053 RECUR (ASM_STRING (t
)),
13054 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13055 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13056 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13057 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13059 tree asm_expr
= tmp
;
13060 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13061 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13062 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13069 stmt
= begin_try_block ();
13070 RECUR (TRY_STMTS (t
));
13071 finish_cleanup_try_block (stmt
);
13072 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13076 tree compound_stmt
= NULL_TREE
;
13078 if (FN_TRY_BLOCK_P (t
))
13079 stmt
= begin_function_try_block (&compound_stmt
);
13081 stmt
= begin_try_block ();
13083 RECUR (TRY_STMTS (t
));
13085 if (FN_TRY_BLOCK_P (t
))
13086 finish_function_try_block (stmt
);
13088 finish_try_block (stmt
);
13090 RECUR (TRY_HANDLERS (t
));
13091 if (FN_TRY_BLOCK_P (t
))
13092 finish_function_handler_sequence (stmt
, compound_stmt
);
13094 finish_handler_sequence (stmt
);
13100 tree decl
= HANDLER_PARMS (t
);
13104 decl
= tsubst (decl
, args
, complain
, in_decl
);
13105 /* Prevent instantiate_decl from trying to instantiate
13106 this variable. We've already done all that needs to be
13108 if (decl
!= error_mark_node
)
13109 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13111 stmt
= begin_handler ();
13112 finish_handler_parms (decl
, stmt
);
13113 RECUR (HANDLER_BODY (t
));
13114 finish_handler (stmt
);
13119 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13120 if (CLASS_TYPE_P (tmp
))
13122 /* Local classes are not independent templates; they are
13123 instantiated along with their containing function. And this
13124 way we don't have to deal with pushing out of one local class
13125 to instantiate a member of another local class. */
13127 /* Closures are handled by the LAMBDA_EXPR. */
13128 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13129 complete_type (tmp
);
13130 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13131 if (!DECL_ARTIFICIAL (fn
))
13132 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13136 case STATIC_ASSERT
:
13140 ++c_inhibit_evaluation_warnings
;
13142 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13145 /*integral_constant_expression_p=*/true);
13146 --c_inhibit_evaluation_warnings
;
13148 finish_static_assert (condition
,
13149 STATIC_ASSERT_MESSAGE (t
),
13150 STATIC_ASSERT_SOURCE_LOCATION (t
),
13151 /*member_p=*/false);
13156 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13157 args
, complain
, in_decl
);
13158 stmt
= begin_omp_parallel ();
13159 RECUR (OMP_PARALLEL_BODY (t
));
13160 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13161 = OMP_PARALLEL_COMBINED (t
);
13165 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13166 args
, complain
, in_decl
);
13167 stmt
= begin_omp_task ();
13168 RECUR (OMP_TASK_BODY (t
));
13169 finish_omp_task (tmp
, stmt
);
13174 tree clauses
, body
, pre_body
;
13175 tree declv
, initv
, condv
, incrv
;
13178 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13179 args
, complain
, in_decl
);
13180 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13181 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13182 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13183 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13185 stmt
= begin_omp_structured_block ();
13187 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13188 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13189 &clauses
, args
, complain
, in_decl
,
13190 integral_constant_expression_p
);
13192 pre_body
= push_stmt_list ();
13193 RECUR (OMP_FOR_PRE_BODY (t
));
13194 pre_body
= pop_stmt_list (pre_body
);
13196 body
= push_stmt_list ();
13197 RECUR (OMP_FOR_BODY (t
));
13198 body
= pop_stmt_list (body
);
13200 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13201 body
, pre_body
, clauses
);
13203 add_stmt (finish_omp_structured_block (stmt
));
13209 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13210 stmt
= push_stmt_list ();
13211 RECUR (OMP_BODY (t
));
13212 stmt
= pop_stmt_list (stmt
);
13215 OMP_BODY (t
) = stmt
;
13216 OMP_CLAUSES (t
) = tmp
;
13224 stmt
= push_stmt_list ();
13225 RECUR (OMP_BODY (t
));
13226 stmt
= pop_stmt_list (stmt
);
13229 OMP_BODY (t
) = stmt
;
13234 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13235 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13237 tree op1
= TREE_OPERAND (t
, 1);
13238 tree rhs1
= NULL_TREE
;
13240 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13242 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13243 op1
= TREE_OPERAND (op1
, 1);
13245 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13246 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13247 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13248 NULL_TREE
, NULL_TREE
, rhs1
);
13252 tree op1
= TREE_OPERAND (t
, 1);
13253 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13254 tree rhs1
= NULL_TREE
;
13255 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13256 enum tree_code opcode
= NOP_EXPR
;
13257 if (code
== OMP_ATOMIC_READ
)
13259 v
= RECUR (TREE_OPERAND (op1
, 0));
13260 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13262 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13263 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13265 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13266 v
= RECUR (TREE_OPERAND (op1
, 0));
13267 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13268 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13270 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13271 op11
= TREE_OPERAND (op11
, 1);
13273 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13274 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13275 opcode
= TREE_CODE (op11
);
13280 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13281 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13283 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13287 case TRANSACTION_EXPR
:
13290 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13291 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13293 if (TRANSACTION_EXPR_IS_STMT (t
))
13295 tree body
= TRANSACTION_EXPR_BODY (t
);
13296 tree noex
= NULL_TREE
;
13297 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13299 noex
= MUST_NOT_THROW_COND (body
);
13300 if (noex
== NULL_TREE
)
13301 noex
= boolean_true_node
;
13302 body
= TREE_OPERAND (body
, 0);
13304 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13306 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13310 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13311 RECUR (TRANSACTION_EXPR_BODY (t
)),
13318 case MUST_NOT_THROW_EXPR
:
13319 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13320 RECUR (MUST_NOT_THROW_COND (t
))));
13322 case EXPR_PACK_EXPANSION
:
13323 error ("invalid use of pack expansion expression");
13324 RETURN (error_mark_node
);
13326 case NONTYPE_ARGUMENT_PACK
:
13327 error ("use %<...%> to expand argument pack");
13328 RETURN (error_mark_node
);
13330 case COMPOUND_EXPR
:
13331 tmp
= RECUR (TREE_OPERAND (t
, 0));
13332 if (tmp
== NULL_TREE
)
13333 /* If the first operand was a statement, we're done with it. */
13334 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13335 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13336 RECUR (TREE_OPERAND (t
, 1)),
13340 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13342 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13343 /*function_p=*/false,
13344 integral_constant_expression_p
));
13347 RETURN (NULL_TREE
);
13349 input_location
= loc
;
13355 /* T is a postfix-expression that is not being used in a function
13356 call. Return the substituted version of T. */
13359 tsubst_non_call_postfix_expression (tree t
, tree args
,
13360 tsubst_flags_t complain
,
13363 if (TREE_CODE (t
) == SCOPE_REF
)
13364 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13365 /*done=*/false, /*address_p=*/false);
13367 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13368 /*function_p=*/false,
13369 /*integral_constant_expression_p=*/false);
13374 /* Like tsubst but deals with expressions and performs semantic
13375 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13378 tsubst_copy_and_build (tree t
,
13380 tsubst_flags_t complain
,
13383 bool integral_constant_expression_p
)
13385 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13386 #define RECUR(NODE) \
13387 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13388 /*function_p=*/false, \
13389 integral_constant_expression_p)
13394 if (t
== NULL_TREE
|| t
== error_mark_node
)
13397 loc
= input_location
;
13398 if (EXPR_HAS_LOCATION (t
))
13399 input_location
= EXPR_LOCATION (t
);
13401 switch (TREE_CODE (t
))
13405 /* Fall through. */
13406 case IDENTIFIER_NODE
:
13410 bool non_integral_constant_expression_p
;
13411 const char *error_msg
;
13413 if (IDENTIFIER_TYPENAME_P (t
))
13415 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13416 t
= mangle_conv_op_name_for_type (new_type
);
13419 /* Look up the name. */
13420 decl
= lookup_name (t
);
13422 /* By convention, expressions use ERROR_MARK_NODE to indicate
13423 failure, not NULL_TREE. */
13424 if (decl
== NULL_TREE
)
13425 decl
= error_mark_node
;
13427 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13429 integral_constant_expression_p
,
13430 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13431 &non_integral_constant_expression_p
,
13432 /*template_p=*/false,
13434 /*address_p=*/false,
13435 /*template_arg_p=*/false,
13440 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13442 if (complain
& tf_error
)
13443 unqualified_name_lookup_error (decl
);
13444 decl
= error_mark_node
;
13449 case TEMPLATE_ID_EXPR
:
13452 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13453 tree targs
= TREE_OPERAND (t
, 1);
13456 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13458 if (TREE_CODE (templ
) == COMPONENT_REF
)
13460 object
= TREE_OPERAND (templ
, 0);
13461 templ
= TREE_OPERAND (templ
, 1);
13464 object
= NULL_TREE
;
13465 templ
= lookup_template_function (templ
, targs
);
13468 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13469 object
, templ
, NULL_TREE
));
13471 RETURN (baselink_for_fns (templ
));
13476 tree r
= RECUR (TREE_OPERAND (t
, 0));
13478 if (REFERENCE_REF_P (t
))
13480 /* A type conversion to reference type will be enclosed in
13481 such an indirect ref, but the substitution of the cast
13482 will have also added such an indirect ref. */
13483 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13484 r
= convert_from_reference (r
);
13487 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13493 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13494 RECUR (TREE_OPERAND (t
, 0))));
13496 case IMPLICIT_CONV_EXPR
:
13498 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13499 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13500 int flags
= LOOKUP_IMPLICIT
;
13501 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13502 flags
= LOOKUP_NORMAL
;
13503 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13510 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13511 RECUR (TREE_OPERAND (t
, 0))));
13514 case REINTERPRET_CAST_EXPR
:
13515 case CONST_CAST_EXPR
:
13516 case DYNAMIC_CAST_EXPR
:
13517 case STATIC_CAST_EXPR
:
13520 tree op
, r
= NULL_TREE
;
13522 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13523 if (integral_constant_expression_p
13524 && !cast_valid_in_integral_constant_expression_p (type
))
13526 if (complain
& tf_error
)
13527 error ("a cast to a type other than an integral or "
13528 "enumeration type cannot appear in a constant-expression");
13529 RETURN (error_mark_node
);
13532 op
= RECUR (TREE_OPERAND (t
, 0));
13534 ++c_inhibit_evaluation_warnings
;
13535 switch (TREE_CODE (t
))
13538 r
= build_functional_cast (type
, op
, complain
);
13540 case REINTERPRET_CAST_EXPR
:
13541 r
= build_reinterpret_cast (type
, op
, complain
);
13543 case CONST_CAST_EXPR
:
13544 r
= build_const_cast (type
, op
, complain
);
13546 case DYNAMIC_CAST_EXPR
:
13547 r
= build_dynamic_cast (type
, op
, complain
);
13549 case STATIC_CAST_EXPR
:
13550 r
= build_static_cast (type
, op
, complain
);
13553 gcc_unreachable ();
13555 --c_inhibit_evaluation_warnings
;
13560 case POSTDECREMENT_EXPR
:
13561 case POSTINCREMENT_EXPR
:
13562 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13563 args
, complain
, in_decl
);
13564 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13566 case PREDECREMENT_EXPR
:
13567 case PREINCREMENT_EXPR
:
13571 case TRUTH_NOT_EXPR
:
13572 case UNARY_PLUS_EXPR
: /* Unary + */
13573 case REALPART_EXPR
:
13574 case IMAGPART_EXPR
:
13575 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13576 RECUR (TREE_OPERAND (t
, 0)), complain
));
13578 case FIX_TRUNC_EXPR
:
13579 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13583 op1
= TREE_OPERAND (t
, 0);
13584 if (TREE_CODE (op1
) == LABEL_DECL
)
13585 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13586 EXPR_LOCATION (op1
)));
13587 if (TREE_CODE (op1
) == SCOPE_REF
)
13588 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13589 /*done=*/true, /*address_p=*/true);
13591 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13593 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13598 case TRUNC_DIV_EXPR
:
13599 case CEIL_DIV_EXPR
:
13600 case FLOOR_DIV_EXPR
:
13601 case ROUND_DIV_EXPR
:
13602 case EXACT_DIV_EXPR
:
13606 case TRUNC_MOD_EXPR
:
13607 case FLOOR_MOD_EXPR
:
13608 case TRUTH_ANDIF_EXPR
:
13609 case TRUTH_ORIF_EXPR
:
13610 case TRUTH_AND_EXPR
:
13611 case TRUTH_OR_EXPR
:
13629 ++c_inhibit_evaluation_warnings
;
13631 r
= build_x_binary_op
13632 (input_location
, TREE_CODE (t
),
13633 RECUR (TREE_OPERAND (t
, 0)),
13634 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13636 : TREE_CODE (TREE_OPERAND (t
, 0))),
13637 RECUR (TREE_OPERAND (t
, 1)),
13638 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13640 : TREE_CODE (TREE_OPERAND (t
, 1))),
13643 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13644 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13646 --c_inhibit_evaluation_warnings
;
13652 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13653 /*address_p=*/false));
13655 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13656 args
, complain
, in_decl
);
13657 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13658 RECUR (TREE_OPERAND (t
, 1)), complain
));
13661 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13662 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13669 op1
= TREE_OPERAND (t
, 0);
13670 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13671 op1
= TREE_TYPE (op1
);
13674 /* When there are no ARGS, we are trying to evaluate a
13675 non-dependent expression from the parser. Trying to do
13676 the substitutions may not work. */
13678 op1
= TREE_TYPE (op1
);
13682 ++cp_unevaluated_operand
;
13683 ++c_inhibit_evaluation_warnings
;
13685 op1
= tsubst (op1
, args
, complain
, in_decl
);
13687 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13688 /*function_p=*/false,
13689 /*integral_constant_expression_p=*/
13691 --cp_unevaluated_operand
;
13692 --c_inhibit_evaluation_warnings
;
13695 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13696 complain
& tf_error
);
13698 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13699 complain
& tf_error
);
13700 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
13702 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
13704 if (!processing_template_decl
&& TYPE_P (op1
))
13706 r
= build_min (SIZEOF_EXPR
, size_type_node
,
13707 build1 (NOP_EXPR
, op1
, error_mark_node
));
13708 SIZEOF_EXPR_TYPE_P (r
) = 1;
13711 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
13712 TREE_SIDE_EFFECTS (r
) = 0;
13713 TREE_READONLY (r
) = 1;
13715 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
13720 case AT_ENCODE_EXPR
:
13722 op1
= TREE_OPERAND (t
, 0);
13723 ++cp_unevaluated_operand
;
13724 ++c_inhibit_evaluation_warnings
;
13725 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13726 /*function_p=*/false,
13727 /*integral_constant_expression_p=*/false);
13728 --cp_unevaluated_operand
;
13729 --c_inhibit_evaluation_warnings
;
13730 RETURN (objc_build_encode_expr (op1
));
13733 case NOEXCEPT_EXPR
:
13734 op1
= TREE_OPERAND (t
, 0);
13735 ++cp_unevaluated_operand
;
13736 ++c_inhibit_evaluation_warnings
;
13737 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13738 /*function_p=*/false,
13739 /*integral_constant_expression_p=*/false);
13740 --cp_unevaluated_operand
;
13741 --c_inhibit_evaluation_warnings
;
13742 RETURN (finish_noexcept_expr (op1
, complain
));
13746 tree r
= build_x_modify_expr
13747 (EXPR_LOCATION (t
),
13748 RECUR (TREE_OPERAND (t
, 0)),
13749 TREE_CODE (TREE_OPERAND (t
, 1)),
13750 RECUR (TREE_OPERAND (t
, 2)),
13752 /* TREE_NO_WARNING must be set if either the expression was
13753 parenthesized or it uses an operator such as >>= rather
13754 than plain assignment. In the former case, it was already
13755 set and must be copied. In the latter case,
13756 build_x_modify_expr sets it and it must not be reset
13758 if (TREE_NO_WARNING (t
))
13759 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13764 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13765 args
, complain
, in_decl
);
13766 /* Remember that there was a reference to this entity. */
13769 RETURN (build_x_arrow (input_location
, op1
, complain
));
13773 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13774 tree init
= RECUR (TREE_OPERAND (t
, 3));
13775 vec
<tree
, va_gc
> *placement_vec
;
13776 vec
<tree
, va_gc
> *init_vec
;
13779 if (placement
== NULL_TREE
)
13780 placement_vec
= NULL
;
13783 placement_vec
= make_tree_vector ();
13784 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13785 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
13788 /* If there was an initializer in the original tree, but it
13789 instantiated to an empty list, then we should pass a
13790 non-NULL empty vector to tell build_new that it was an
13791 empty initializer() rather than no initializer. This can
13792 only happen when the initializer is a pack expansion whose
13793 parameter packs are of length zero. */
13794 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13798 init_vec
= make_tree_vector ();
13799 if (init
== void_zero_node
)
13800 gcc_assert (init_vec
!= NULL
);
13803 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13804 vec_safe_push (init_vec
, TREE_VALUE (init
));
13808 ret
= build_new (&placement_vec
,
13809 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13810 RECUR (TREE_OPERAND (t
, 2)),
13812 NEW_EXPR_USE_GLOBAL (t
),
13815 if (placement_vec
!= NULL
)
13816 release_tree_vector (placement_vec
);
13817 if (init_vec
!= NULL
)
13818 release_tree_vector (init_vec
);
13824 RETURN (delete_sanity
13825 (RECUR (TREE_OPERAND (t
, 0)),
13826 RECUR (TREE_OPERAND (t
, 1)),
13827 DELETE_EXPR_USE_VEC (t
),
13828 DELETE_EXPR_USE_GLOBAL (t
),
13831 case COMPOUND_EXPR
:
13832 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13833 RECUR (TREE_OPERAND (t
, 0)),
13834 RECUR (TREE_OPERAND (t
, 1)),
13840 vec
<tree
, va_gc
> *call_args
;
13841 unsigned int nargs
, i
;
13846 function
= CALL_EXPR_FN (t
);
13847 /* When we parsed the expression, we determined whether or
13848 not Koenig lookup should be performed. */
13849 koenig_p
= KOENIG_LOOKUP_P (t
);
13850 if (TREE_CODE (function
) == SCOPE_REF
)
13852 qualified_p
= true;
13853 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13855 /*address_p=*/false);
13857 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13859 /* Do nothing; calling tsubst_copy_and_build on an identifier
13860 would incorrectly perform unqualified lookup again.
13862 Note that we can also have an IDENTIFIER_NODE if the earlier
13863 unqualified lookup found a member function; in that case
13864 koenig_p will be false and we do want to do the lookup
13865 again to find the instantiated member function.
13867 FIXME but doing that causes c++/15272, so we need to stop
13868 using IDENTIFIER_NODE in that situation. */
13869 qualified_p
= false;
13873 if (TREE_CODE (function
) == COMPONENT_REF
)
13875 tree op
= TREE_OPERAND (function
, 1);
13877 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13878 || (BASELINK_P (op
)
13879 && BASELINK_QUALIFIED_P (op
)));
13882 qualified_p
= false;
13884 if (TREE_CODE (function
) == ADDR_EXPR
13885 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
13886 /* Avoid error about taking the address of a constructor. */
13887 function
= TREE_OPERAND (function
, 0);
13889 function
= tsubst_copy_and_build (function
, args
, complain
,
13892 integral_constant_expression_p
);
13894 if (BASELINK_P (function
))
13895 qualified_p
= true;
13898 nargs
= call_expr_nargs (t
);
13899 call_args
= make_tree_vector ();
13900 for (i
= 0; i
< nargs
; ++i
)
13902 tree arg
= CALL_EXPR_ARG (t
, i
);
13904 if (!PACK_EXPANSION_P (arg
))
13905 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
13908 /* Expand the pack expansion and push each entry onto
13910 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13911 if (TREE_CODE (arg
) == TREE_VEC
)
13913 unsigned int len
, j
;
13915 len
= TREE_VEC_LENGTH (arg
);
13916 for (j
= 0; j
< len
; ++j
)
13918 tree value
= TREE_VEC_ELT (arg
, j
);
13919 if (value
!= NULL_TREE
)
13920 value
= convert_from_reference (value
);
13921 vec_safe_push (call_args
, value
);
13926 /* A partial substitution. Add one entry. */
13927 vec_safe_push (call_args
, arg
);
13932 /* We do not perform argument-dependent lookup if normal
13933 lookup finds a non-function, in accordance with the
13934 expected resolution of DR 218. */
13936 && ((is_overloaded_fn (function
)
13937 /* If lookup found a member function, the Koenig lookup is
13938 not appropriate, even if an unqualified-name was used
13939 to denote the function. */
13940 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13941 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13942 /* Only do this when substitution turns a dependent call
13943 into a non-dependent call. */
13944 && type_dependent_expression_p_push (t
)
13945 && !any_type_dependent_arguments_p (call_args
))
13946 function
= perform_koenig_lookup (function
, call_args
, false,
13949 if (TREE_CODE (function
) == IDENTIFIER_NODE
13950 && !any_type_dependent_arguments_p (call_args
))
13952 if (koenig_p
&& (complain
& tf_warning_or_error
))
13954 /* For backwards compatibility and good diagnostics, try
13955 the unqualified lookup again if we aren't in SFINAE
13957 tree unq
= (tsubst_copy_and_build
13958 (function
, args
, complain
, in_decl
, true,
13959 integral_constant_expression_p
));
13960 if (unq
== error_mark_node
)
13961 RETURN (error_mark_node
);
13963 if (unq
!= function
)
13966 if (TREE_CODE (fn
) == INDIRECT_REF
)
13967 fn
= TREE_OPERAND (fn
, 0);
13968 if (TREE_CODE (fn
) == COMPONENT_REF
)
13969 fn
= TREE_OPERAND (fn
, 1);
13970 if (is_overloaded_fn (fn
))
13971 fn
= get_first_fn (fn
);
13972 permerror (EXPR_LOC_OR_HERE (t
),
13973 "%qD was not declared in this scope, "
13974 "and no declarations were found by "
13975 "argument-dependent lookup at the point "
13976 "of instantiation", function
);
13978 /* Can't say anything more. */;
13979 else if (DECL_CLASS_SCOPE_P (fn
))
13981 inform (EXPR_LOC_OR_HERE (t
),
13982 "declarations in dependent base %qT are "
13983 "not found by unqualified lookup",
13984 DECL_CLASS_CONTEXT (fn
));
13985 if (current_class_ptr
)
13986 inform (EXPR_LOC_OR_HERE (t
),
13987 "use %<this->%D%> instead", function
);
13989 inform (EXPR_LOC_OR_HERE (t
),
13990 "use %<%T::%D%> instead",
13991 current_class_name
, function
);
13994 inform (0, "%q+D declared here, later in the "
13995 "translation unit", fn
);
13999 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
14001 if (complain
& tf_error
)
14002 unqualified_name_lookup_error (function
);
14003 release_tree_vector (call_args
);
14004 RETURN (error_mark_node
);
14008 /* Remember that there was a reference to this entity. */
14009 if (DECL_P (function
))
14010 mark_used (function
);
14012 if (TREE_CODE (function
) == OFFSET_REF
)
14013 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14015 else if (TREE_CODE (function
) == COMPONENT_REF
)
14017 tree instance
= TREE_OPERAND (function
, 0);
14018 tree fn
= TREE_OPERAND (function
, 1);
14020 if (processing_template_decl
14021 && (type_dependent_expression_p (instance
)
14022 || (!BASELINK_P (fn
)
14023 && TREE_CODE (fn
) != FIELD_DECL
)
14024 || type_dependent_expression_p (fn
)
14025 || any_type_dependent_arguments_p (call_args
)))
14026 ret
= build_nt_call_vec (function
, call_args
);
14027 else if (!BASELINK_P (fn
))
14028 ret
= finish_call_expr (function
, &call_args
,
14029 /*disallow_virtual=*/false,
14030 /*koenig_p=*/false,
14033 ret
= (build_new_method_call
14035 &call_args
, NULL_TREE
,
14036 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14041 ret
= finish_call_expr (function
, &call_args
,
14042 /*disallow_virtual=*/qualified_p
,
14046 release_tree_vector (call_args
);
14053 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14056 if (TREE_CODE (cond
) == INTEGER_CST
)
14058 if (integer_zerop (cond
))
14060 ++c_inhibit_evaluation_warnings
;
14061 exp1
= RECUR (TREE_OPERAND (t
, 1));
14062 --c_inhibit_evaluation_warnings
;
14063 exp2
= RECUR (TREE_OPERAND (t
, 2));
14067 exp1
= RECUR (TREE_OPERAND (t
, 1));
14068 ++c_inhibit_evaluation_warnings
;
14069 exp2
= RECUR (TREE_OPERAND (t
, 2));
14070 --c_inhibit_evaluation_warnings
;
14075 exp1
= RECUR (TREE_OPERAND (t
, 1));
14076 exp2
= RECUR (TREE_OPERAND (t
, 2));
14079 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14080 cond
, exp1
, exp2
, complain
));
14083 case PSEUDO_DTOR_EXPR
:
14084 RETURN (finish_pseudo_destructor_expr
14085 (RECUR (TREE_OPERAND (t
, 0)),
14086 RECUR (TREE_OPERAND (t
, 1)),
14087 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
14091 tree purpose
, value
, chain
;
14093 if (t
== void_list_node
)
14096 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14097 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14099 /* We have pack expansions, so expand those and
14100 create a new list out of it. */
14101 tree purposevec
= NULL_TREE
;
14102 tree valuevec
= NULL_TREE
;
14106 /* Expand the argument expressions. */
14107 if (TREE_PURPOSE (t
))
14108 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14109 complain
, in_decl
);
14110 if (TREE_VALUE (t
))
14111 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14112 complain
, in_decl
);
14114 /* Build the rest of the list. */
14115 chain
= TREE_CHAIN (t
);
14116 if (chain
&& chain
!= void_type_node
)
14117 chain
= RECUR (chain
);
14119 /* Determine the number of arguments. */
14120 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14122 len
= TREE_VEC_LENGTH (purposevec
);
14123 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14125 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14126 len
= TREE_VEC_LENGTH (valuevec
);
14129 /* Since we only performed a partial substitution into
14130 the argument pack, we only RETURN (a single list
14132 if (purposevec
== TREE_PURPOSE (t
)
14133 && valuevec
== TREE_VALUE (t
)
14134 && chain
== TREE_CHAIN (t
))
14137 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14140 /* Convert the argument vectors into a TREE_LIST */
14144 /* Grab the Ith values. */
14146 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14149 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14152 /* Build the list (backwards). */
14153 chain
= tree_cons (purpose
, value
, chain
);
14159 purpose
= TREE_PURPOSE (t
);
14161 purpose
= RECUR (purpose
);
14162 value
= TREE_VALUE (t
);
14164 value
= RECUR (value
);
14165 chain
= TREE_CHAIN (t
);
14166 if (chain
&& chain
!= void_type_node
)
14167 chain
= RECUR (chain
);
14168 if (purpose
== TREE_PURPOSE (t
)
14169 && value
== TREE_VALUE (t
)
14170 && chain
== TREE_CHAIN (t
))
14172 RETURN (tree_cons (purpose
, value
, chain
));
14175 case COMPONENT_REF
:
14181 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14182 args
, complain
, in_decl
);
14183 /* Remember that there was a reference to this entity. */
14184 if (DECL_P (object
))
14185 mark_used (object
);
14186 object_type
= TREE_TYPE (object
);
14188 member
= TREE_OPERAND (t
, 1);
14189 if (BASELINK_P (member
))
14190 member
= tsubst_baselink (member
,
14191 non_reference (TREE_TYPE (object
)),
14192 args
, complain
, in_decl
);
14194 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14195 if (member
== error_mark_node
)
14196 RETURN (error_mark_node
);
14198 if (type_dependent_expression_p (object
))
14199 /* We can't do much here. */;
14200 else if (!CLASS_TYPE_P (object_type
))
14202 if (scalarish_type_p (object_type
))
14204 tree s
= NULL_TREE
;
14205 tree dtor
= member
;
14207 if (TREE_CODE (dtor
) == SCOPE_REF
)
14209 s
= TREE_OPERAND (dtor
, 0);
14210 dtor
= TREE_OPERAND (dtor
, 1);
14212 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14214 dtor
= TREE_OPERAND (dtor
, 0);
14216 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
14220 else if (TREE_CODE (member
) == SCOPE_REF
14221 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14223 /* Lookup the template functions now that we know what the
14225 tree scope
= TREE_OPERAND (member
, 0);
14226 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14227 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14228 member
= lookup_qualified_name (scope
, tmpl
,
14229 /*is_type_p=*/false,
14230 /*complain=*/false);
14231 if (BASELINK_P (member
))
14233 BASELINK_FUNCTIONS (member
)
14234 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14236 member
= (adjust_result_of_qualified_name_lookup
14237 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14242 qualified_name_lookup_error (scope
, tmpl
, member
,
14244 RETURN (error_mark_node
);
14247 else if (TREE_CODE (member
) == SCOPE_REF
14248 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14249 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14251 if (complain
& tf_error
)
14253 if (TYPE_P (TREE_OPERAND (member
, 0)))
14254 error ("%qT is not a class or namespace",
14255 TREE_OPERAND (member
, 0));
14257 error ("%qD is not a class or namespace",
14258 TREE_OPERAND (member
, 0));
14260 RETURN (error_mark_node
);
14262 else if (TREE_CODE (member
) == FIELD_DECL
)
14263 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14265 RETURN (finish_class_member_access_expr (object
, member
,
14266 /*template_p=*/false,
14271 RETURN (build_throw
14272 (RECUR (TREE_OPERAND (t
, 0))));
14276 vec
<constructor_elt
, va_gc
> *n
;
14277 constructor_elt
*ce
;
14278 unsigned HOST_WIDE_INT idx
;
14279 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14280 bool process_index_p
;
14282 bool need_copy_p
= false;
14285 if (type
== error_mark_node
)
14286 RETURN (error_mark_node
);
14288 /* digest_init will do the wrong thing if we let it. */
14289 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14292 /* We do not want to process the index of aggregate
14293 initializers as they are identifier nodes which will be
14294 looked up by digest_init. */
14295 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14297 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14298 newlen
= vec_safe_length (n
);
14299 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14301 if (ce
->index
&& process_index_p
)
14302 ce
->index
= RECUR (ce
->index
);
14304 if (PACK_EXPANSION_P (ce
->value
))
14306 /* Substitute into the pack expansion. */
14307 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14310 if (ce
->value
== error_mark_node
14311 || PACK_EXPANSION_P (ce
->value
))
14313 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14314 /* Just move the argument into place. */
14315 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14318 /* Update the length of the final CONSTRUCTOR
14319 arguments vector, and note that we will need to
14321 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14322 need_copy_p
= true;
14326 ce
->value
= RECUR (ce
->value
);
14331 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14333 vec_alloc (n
, newlen
);
14334 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14336 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14338 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14339 for (i
= 0; i
< len
; ++i
)
14340 CONSTRUCTOR_APPEND_ELT (n
, 0,
14341 TREE_VEC_ELT (ce
->value
, i
));
14344 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14348 r
= build_constructor (init_list_type_node
, n
);
14349 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14351 if (TREE_HAS_CONSTRUCTOR (t
))
14352 RETURN (finish_compound_literal (type
, r
, complain
));
14354 TREE_TYPE (r
) = type
;
14360 tree operand_0
= TREE_OPERAND (t
, 0);
14361 if (TYPE_P (operand_0
))
14363 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14364 RETURN (get_typeid (operand_0
, complain
));
14368 operand_0
= RECUR (operand_0
);
14369 RETURN (build_typeid (operand_0
, complain
));
14380 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14382 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14383 /* If the original type was a reference, we'll be wrapped in
14384 the appropriate INDIRECT_REF. */
14385 r
= convert_from_reference (r
);
14390 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14391 RECUR (TREE_OPERAND (t
, 0)),
14392 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14394 case OFFSETOF_EXPR
:
14395 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14399 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14400 complain
, in_decl
);
14402 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14404 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14406 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14411 tree old_stmt_expr
= cur_stmt_expr
;
14412 tree stmt_expr
= begin_stmt_expr ();
14414 cur_stmt_expr
= stmt_expr
;
14415 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14416 integral_constant_expression_p
);
14417 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14418 cur_stmt_expr
= old_stmt_expr
;
14420 /* If the resulting list of expression statement is empty,
14421 fold it further into void_zero_node. */
14422 if (empty_expr_stmt_p (stmt_expr
))
14423 stmt_expr
= void_zero_node
;
14425 RETURN (stmt_expr
);
14430 tree r
= build_lambda_expr ();
14432 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14433 LAMBDA_EXPR_CLOSURE (r
) = type
;
14434 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14436 LAMBDA_EXPR_LOCATION (r
)
14437 = LAMBDA_EXPR_LOCATION (t
);
14438 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14439 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14440 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14441 LAMBDA_EXPR_DISCRIMINATOR (r
)
14442 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14443 /* For a function scope, we want to use tsubst so that we don't
14444 complain about referring to an auto function before its return
14445 type has been deduced. Otherwise, we want to use tsubst_copy so
14446 that we look up the existing field/parameter/variable rather
14447 than build a new one. */
14448 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14449 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14450 scope
= tsubst (scope
, args
, complain
, in_decl
);
14451 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14453 /* Look up the parameter we want directly, as tsubst_copy
14454 doesn't do what we need. */
14455 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14456 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14457 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14458 parm
= DECL_CHAIN (parm
);
14460 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14461 if (DECL_CONTEXT (scope
) == NULL_TREE
)
14462 DECL_CONTEXT (scope
) = fn
;
14465 scope
= RECUR (scope
);
14466 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14467 LAMBDA_EXPR_RETURN_TYPE (r
)
14468 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14470 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14471 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14473 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14474 determine_visibility (TYPE_NAME (type
));
14475 /* Now that we know visibility, instantiate the type so we have a
14476 declaration of the op() for later calls to lambda_function. */
14477 complete_type (type
);
14479 /* The capture list refers to closure members, so this needs to
14480 wait until after we finish instantiating the type. Also keep
14481 any captures that may have been added during instantiation. */
14482 LAMBDA_EXPR_CAPTURE_LIST (r
)
14483 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
)),
14484 LAMBDA_EXPR_CAPTURE_LIST (r
));
14485 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14487 RETURN (build_lambda_object (r
));
14491 /* We can get here for a constant initializer of non-dependent type.
14492 FIXME stop folding in cp_parser_initializer_clause. */
14494 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14499 case TRANSACTION_EXPR
:
14500 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14501 integral_constant_expression_p
));
14504 /* Handle Objective-C++ constructs, if appropriate. */
14507 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14508 in_decl
, /*function_p=*/false);
14512 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14518 input_location
= loc
;
14522 /* Verify that the instantiated ARGS are valid. For type arguments,
14523 make sure that the type's linkage is ok. For non-type arguments,
14524 make sure they are constants if they are integral or enumerations.
14525 Emit an error under control of COMPLAIN, and return TRUE on error. */
14528 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14530 if (dependent_template_arg_p (t
))
14532 if (ARGUMENT_PACK_P (t
))
14534 tree vec
= ARGUMENT_PACK_ARGS (t
);
14535 int len
= TREE_VEC_LENGTH (vec
);
14536 bool result
= false;
14539 for (i
= 0; i
< len
; ++i
)
14540 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14544 else if (TYPE_P (t
))
14546 /* [basic.link]: A name with no linkage (notably, the name
14547 of a class or enumeration declared in a local scope)
14548 shall not be used to declare an entity with linkage.
14549 This implies that names with no linkage cannot be used as
14552 DR 757 relaxes this restriction for C++0x. */
14553 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14554 : no_linkage_check (t
, /*relaxed_p=*/false));
14558 /* DR 488 makes use of a type with no linkage cause
14559 type deduction to fail. */
14560 if (complain
& tf_error
)
14562 if (TYPE_ANONYMOUS_P (nt
))
14563 error ("%qT is/uses anonymous type", t
);
14565 error ("template argument for %qD uses local type %qT",
14570 /* In order to avoid all sorts of complications, we do not
14571 allow variably-modified types as template arguments. */
14572 else if (variably_modified_type_p (t
, NULL_TREE
))
14574 if (complain
& tf_error
)
14575 error ("%qT is a variably modified type", t
);
14579 /* Class template and alias template arguments should be OK. */
14580 else if (DECL_TYPE_TEMPLATE_P (t
))
14582 /* A non-type argument of integral or enumerated type must be a
14584 else if (TREE_TYPE (t
)
14585 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14586 && !TREE_CONSTANT (t
))
14588 if (complain
& tf_error
)
14589 error ("integral expression %qE is not constant", t
);
14596 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14598 int ix
, len
= DECL_NTPARMS (tmpl
);
14599 bool result
= false;
14601 for (ix
= 0; ix
!= len
; ix
++)
14603 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14606 if (result
&& (complain
& tf_error
))
14607 error (" trying to instantiate %qD", tmpl
);
14611 /* We're out of SFINAE context now, so generate diagnostics for the access
14612 errors we saw earlier when instantiating D from TMPL and ARGS. */
14615 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14617 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14618 tree type
= TREE_TYPE (pattern
);
14619 location_t loc
= input_location
;
14621 push_access_scope (d
);
14622 push_deferring_access_checks (dk_no_deferred
);
14623 input_location
= DECL_SOURCE_LOCATION (pattern
);
14624 tsubst (type
, args
, tf_warning_or_error
, d
);
14625 input_location
= loc
;
14626 pop_deferring_access_checks ();
14627 pop_access_scope (d
);
14630 /* Instantiate the indicated variable, function, or alias template TMPL with
14631 the template arguments in TARG_PTR. */
14634 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14636 tree targ_ptr
= orig_args
;
14640 bool access_ok
= true;
14642 if (tmpl
== error_mark_node
)
14643 return error_mark_node
;
14645 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14647 /* If this function is a clone, handle it specially. */
14648 if (DECL_CLONED_FUNCTION_P (tmpl
))
14653 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14654 DECL_CLONED_FUNCTION. */
14655 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14656 targ_ptr
, complain
);
14657 if (spec
== error_mark_node
)
14658 return error_mark_node
;
14660 /* Look for the clone. */
14661 FOR_EACH_CLONE (clone
, spec
)
14662 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14664 /* We should always have found the clone by now. */
14665 gcc_unreachable ();
14669 /* Check to see if we already have this specialization. */
14670 gen_tmpl
= most_general_template (tmpl
);
14671 if (tmpl
!= gen_tmpl
)
14672 /* The TMPL is a partial instantiation. To get a full set of
14673 arguments we must add the arguments used to perform the
14674 partial instantiation. */
14675 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14678 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14679 but it doesn't seem to be on the hot path. */
14680 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14682 gcc_assert (tmpl
== gen_tmpl
14683 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14685 || fndecl
== NULL_TREE
);
14687 if (spec
!= NULL_TREE
)
14689 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14691 if (complain
& tf_error
)
14692 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14693 return error_mark_node
;
14698 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14700 return error_mark_node
;
14702 /* We are building a FUNCTION_DECL, during which the access of its
14703 parameters and return types have to be checked. However this
14704 FUNCTION_DECL which is the desired context for access checking
14705 is not built yet. We solve this chicken-and-egg problem by
14706 deferring all checks until we have the FUNCTION_DECL. */
14707 push_deferring_access_checks (dk_deferred
);
14709 /* Instantiation of the function happens in the context of the function
14710 template, not the context of the overload resolution we're doing. */
14711 push_to_top_level ();
14712 /* If there are dependent arguments, e.g. because we're doing partial
14713 ordering, make sure processing_template_decl stays set. */
14714 if (uses_template_parms (targ_ptr
))
14715 ++processing_template_decl
;
14716 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14718 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14719 complain
, gen_tmpl
);
14720 push_nested_class (ctx
);
14722 /* Substitute template parameters to obtain the specialization. */
14723 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14724 targ_ptr
, complain
, gen_tmpl
);
14725 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14726 pop_nested_class ();
14727 pop_from_top_level ();
14729 if (fndecl
== error_mark_node
)
14731 pop_deferring_access_checks ();
14732 return error_mark_node
;
14735 /* The DECL_TI_TEMPLATE should always be the immediate parent
14736 template, not the most general template. */
14737 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14739 /* Now we know the specialization, compute access previously
14741 push_access_scope (fndecl
);
14742 if (!perform_deferred_access_checks (complain
))
14744 pop_access_scope (fndecl
);
14745 pop_deferring_access_checks ();
14747 /* If we've just instantiated the main entry point for a function,
14748 instantiate all the alternate entry points as well. We do this
14749 by cloning the instantiation of the main entry point, not by
14750 instantiating the template clones. */
14751 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14752 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14756 if (!(complain
& tf_error
))
14758 /* Remember to reinstantiate when we're out of SFINAE so the user
14759 can see the errors. */
14760 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14762 return error_mark_node
;
14767 /* Wrapper for instantiate_template_1. */
14770 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14773 timevar_push (TV_TEMPLATE_INST
);
14774 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14775 timevar_pop (TV_TEMPLATE_INST
);
14779 /* Instantiate the alias template TMPL with ARGS. Also push a template
14780 instantiation level, which instantiate_template doesn't do because
14781 functions and variables have sufficient context established by the
14785 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14787 struct pending_template
*old_last_pend
= last_pending_template
;
14788 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14789 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14790 return error_mark_node
;
14791 tree tinst
= build_tree_list (tmpl
, args
);
14792 if (!push_tinst_level (tinst
))
14795 return error_mark_node
;
14799 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
14800 args
, tmpl
, complain
,
14801 /*require_all_args=*/true,
14802 /*use_default_args=*/true);
14804 tree r
= instantiate_template (tmpl
, args
, complain
);
14805 pop_tinst_level ();
14806 /* We can't free this if a pending_template entry or last_error_tinst_level
14807 is pointing at it. */
14808 if (last_pending_template
== old_last_pend
14809 && last_error_tinst_level
== old_error_tinst
)
14815 /* PARM is a template parameter pack for FN. Returns true iff
14816 PARM is used in a deducible way in the argument list of FN. */
14819 pack_deducible_p (tree parm
, tree fn
)
14821 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14822 for (; t
; t
= TREE_CHAIN (t
))
14824 tree type
= TREE_VALUE (t
);
14826 if (!PACK_EXPANSION_P (type
))
14828 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14829 packs
; packs
= TREE_CHAIN (packs
))
14830 if (TREE_VALUE (packs
) == parm
)
14832 /* The template parameter pack is used in a function parameter
14833 pack. If this is the end of the parameter list, the
14834 template parameter pack is deducible. */
14835 if (TREE_CHAIN (t
) == void_list_node
)
14838 /* Otherwise, not. Well, it could be deduced from
14839 a non-pack parameter, but doing so would end up with
14840 a deduction mismatch, so don't bother. */
14844 /* The template parameter pack isn't used in any function parameter
14845 packs, but it might be used deeper, e.g. tuple<Args...>. */
14849 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14850 NARGS elements of the arguments that are being used when calling
14851 it. TARGS is a vector into which the deduced template arguments
14854 Return zero for success, 2 for an incomplete match that doesn't resolve
14855 all the types, and 1 for complete failure. An error message will be
14856 printed only for an incomplete match.
14858 If FN is a conversion operator, or we are trying to produce a specific
14859 specialization, RETURN_TYPE is the return type desired.
14861 The EXPLICIT_TARGS are explicit template arguments provided via a
14864 The parameter STRICT is one of:
14867 We are deducing arguments for a function call, as in
14868 [temp.deduct.call].
14871 We are deducing arguments for a conversion function, as in
14872 [temp.deduct.conv].
14875 We are deducing arguments when doing an explicit instantiation
14876 as in [temp.explicit], when determining an explicit specialization
14877 as in [temp.expl.spec], or when taking the address of a function
14878 template, as in [temp.deduct.funcaddr]. */
14881 fn_type_unification (tree fn
,
14882 tree explicit_targs
,
14885 unsigned int nargs
,
14887 unification_kind_t strict
,
14893 tree decl
= NULL_TREE
;
14894 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14896 static int deduction_depth
;
14897 struct pending_template
*old_last_pend
= last_pending_template
;
14898 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14899 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14901 tree r
= error_mark_node
;
14903 /* Adjust any explicit template arguments before entering the
14904 substitution context. */
14905 if (explicit_targs
)
14908 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14910 /*require_all_args=*/false,
14911 /*use_default_args=*/false));
14912 if (explicit_targs
== error_mark_node
)
14913 return error_mark_node
;
14916 /* In C++0x, it's possible to have a function template whose type depends
14917 on itself recursively. This is most obvious with decltype, but can also
14918 occur with enumeration scope (c++/48969). So we need to catch infinite
14919 recursion and reject the substitution at deduction time; this function
14920 will return error_mark_node for any repeated substitution.
14922 This also catches excessive recursion such as when f<N> depends on
14923 f<N-1> across all integers, and returns error_mark_node for all the
14924 substitutions back up to the initial one.
14926 This is, of course, not reentrant. */
14927 if (excessive_deduction_depth
)
14928 return error_mark_node
;
14929 tinst
= build_tree_list (fn
, targs
);
14930 if (!push_tinst_level (tinst
))
14932 excessive_deduction_depth
= true;
14934 return error_mark_node
;
14937 push_deferring_access_checks (dk_deferred
);
14939 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14941 fntype
= TREE_TYPE (fn
);
14942 if (explicit_targs
)
14946 The specified template arguments must match the template
14947 parameters in kind (i.e., type, nontype, template), and there
14948 must not be more arguments than there are parameters;
14949 otherwise type deduction fails.
14951 Nontype arguments must match the types of the corresponding
14952 nontype template parameters, or must be convertible to the
14953 types of the corresponding nontype parameters as specified in
14954 _temp.arg.nontype_, otherwise type deduction fails.
14956 All references in the function type of the function template
14957 to the corresponding template parameters are replaced by the
14958 specified template argument values. If a substitution in a
14959 template parameter or in the function type of the function
14960 template results in an invalid type, type deduction fails. */
14961 int i
, len
= TREE_VEC_LENGTH (tparms
);
14962 location_t loc
= input_location
;
14963 bool incomplete
= false;
14965 /* Substitute the explicit args into the function type. This is
14966 necessary so that, for instance, explicitly declared function
14967 arguments can match null pointed constants. If we were given
14968 an incomplete set of explicit args, we must not do semantic
14969 processing during substitution as we could create partial
14971 for (i
= 0; i
< len
; i
++)
14973 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14974 bool parameter_pack
= false;
14975 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
14977 /* Dig out the actual parm. */
14978 if (TREE_CODE (parm
) == TYPE_DECL
14979 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14981 parm
= TREE_TYPE (parm
);
14982 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14984 else if (TREE_CODE (parm
) == PARM_DECL
)
14986 parm
= DECL_INITIAL (parm
);
14987 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14990 if (!parameter_pack
&& targ
== NULL_TREE
)
14991 /* No explicit argument for this template parameter. */
14994 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14996 /* Mark the argument pack as "incomplete". We could
14997 still deduce more arguments during unification.
14998 We remove this mark in type_unification_real. */
15001 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15002 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15003 = ARGUMENT_PACK_ARGS (targ
);
15006 /* We have some incomplete argument packs. */
15011 processing_template_decl
+= incomplete
;
15012 input_location
= DECL_SOURCE_LOCATION (fn
);
15013 TREE_VALUE (tinst
) = explicit_targs
;
15014 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15015 complain
| tf_partial
, NULL_TREE
);
15016 TREE_VALUE (tinst
) = targs
;
15017 input_location
= loc
;
15018 processing_template_decl
-= incomplete
;
15020 if (fntype
== error_mark_node
)
15023 /* Throw away these access checks; we'll see them again in
15024 instantiate_template and they might have the wrong
15025 access path at this point. */
15026 pop_deferring_access_checks ();
15027 push_deferring_access_checks (dk_deferred
);
15029 /* Place the explicitly specified arguments in TARGS. */
15030 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15031 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15034 /* Never do unification on the 'this' parameter. */
15035 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15041 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15042 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15043 new_args
[0] = return_type
;
15044 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15049 /* We allow incomplete unification without an error message here
15050 because the standard doesn't seem to explicitly prohibit it. Our
15051 callers must be ready to deal with unification failures in any
15054 pop_tinst_level ();
15055 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15056 targs
, parms
, args
, nargs
, /*subr=*/0,
15057 strict
, flags
, explain_p
);
15058 push_tinst_level (tinst
);
15062 /* Now that we have bindings for all of the template arguments,
15063 ensure that the arguments deduced for the template template
15064 parameters have compatible template parameter lists. We cannot
15065 check this property before we have deduced all template
15066 arguments, because the template parameter types of a template
15067 template parameter might depend on prior template parameters
15068 deduced after the template template parameter. The following
15069 ill-formed example illustrates this issue:
15071 template<typename T, template<T> class C> void f(C<5>, T);
15073 template<int N> struct X {};
15076 f(X<5>(), 5l); // error: template argument deduction fails
15079 The template parameter list of 'C' depends on the template type
15080 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15081 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15082 time that we deduce 'C'. */
15083 if (!template_template_parm_bindings_ok_p
15084 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15086 unify_inconsistent_template_template_parameters (explain_p
);
15090 /* All is well so far. Now, check:
15094 When all template arguments have been deduced, all uses of
15095 template parameters in nondeduced contexts are replaced with
15096 the corresponding deduced argument values. If the
15097 substitution results in an invalid type, as described above,
15098 type deduction fails. */
15099 decl
= instantiate_template (fn
, targs
, complain
);
15100 if (decl
== error_mark_node
)
15103 /* Now perform any access checks encountered during deduction, such as
15104 for default template arguments. */
15105 push_access_scope (decl
);
15106 ok
= perform_deferred_access_checks (complain
);
15107 pop_access_scope (decl
);
15111 /* If we're looking for an exact match, check that what we got
15112 is indeed an exact match. It might not be if some template
15113 parameters are used in non-deduced contexts. */
15114 if (strict
== DEDUCE_EXACT
)
15116 tree substed
= TREE_TYPE (decl
);
15120 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15122 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15123 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15124 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15126 unify_type_mismatch (explain_p
, args
[i
],
15127 TREE_VALUE (sarg
));
15135 pop_deferring_access_checks ();
15137 if (excessive_deduction_depth
)
15139 if (deduction_depth
== 0)
15140 /* Reset once we're all the way out. */
15141 excessive_deduction_depth
= false;
15144 pop_tinst_level ();
15145 /* We can't free this if a pending_template entry or last_error_tinst_level
15146 is pointing at it. */
15147 if (last_pending_template
== old_last_pend
15148 && last_error_tinst_level
== old_error_tinst
)
15154 /* Adjust types before performing type deduction, as described in
15155 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15156 sections are symmetric. PARM is the type of a function parameter
15157 or the return type of the conversion function. ARG is the type of
15158 the argument passed to the call, or the type of the value
15159 initialized with the result of the conversion function.
15160 ARG_EXPR is the original argument expression, which may be null. */
15163 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15177 /* Swap PARM and ARG throughout the remainder of this
15178 function; the handling is precisely symmetric since PARM
15179 will initialize ARG rather than vice versa. */
15187 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15188 too, but here handle it by stripping the reference from PARM
15189 rather than by adding it to ARG. */
15190 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15191 && TYPE_REF_IS_RVALUE (*parm
)
15192 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15193 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15194 && TREE_CODE (*arg
) == REFERENCE_TYPE
15195 && !TYPE_REF_IS_RVALUE (*arg
))
15196 *parm
= TREE_TYPE (*parm
);
15197 /* Nothing else to do in this case. */
15201 gcc_unreachable ();
15204 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15206 /* [temp.deduct.call]
15208 If P is not a reference type:
15210 --If A is an array type, the pointer type produced by the
15211 array-to-pointer standard conversion (_conv.array_) is
15212 used in place of A for type deduction; otherwise,
15214 --If A is a function type, the pointer type produced by
15215 the function-to-pointer standard conversion
15216 (_conv.func_) is used in place of A for type deduction;
15219 --If A is a cv-qualified type, the top level
15220 cv-qualifiers of A's type are ignored for type
15222 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15223 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15224 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15225 *arg
= build_pointer_type (*arg
);
15227 *arg
= TYPE_MAIN_VARIANT (*arg
);
15230 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15231 of the form T&&, where T is a template parameter, and the argument
15232 is an lvalue, T is deduced as A& */
15233 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15234 && TYPE_REF_IS_RVALUE (*parm
)
15235 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15236 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15237 && (arg_expr
? real_lvalue_p (arg_expr
)
15238 /* try_one_overload doesn't provide an arg_expr, but
15239 functions are always lvalues. */
15240 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15241 *arg
= build_reference_type (*arg
);
15243 /* [temp.deduct.call]
15245 If P is a cv-qualified type, the top level cv-qualifiers
15246 of P's type are ignored for type deduction. If P is a
15247 reference type, the type referred to by P is used for
15249 *parm
= TYPE_MAIN_VARIANT (*parm
);
15250 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15252 *parm
= TREE_TYPE (*parm
);
15253 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15256 /* DR 322. For conversion deduction, remove a reference type on parm
15257 too (which has been swapped into ARG). */
15258 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15259 *arg
= TREE_TYPE (*arg
);
15264 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15265 template which does contain any deducible template parameters; check if
15266 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15267 unify_one_argument. */
15270 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15271 int flags
, bool explain_p
)
15276 type
= TREE_TYPE (arg
);
15280 if (same_type_p (parm
, type
))
15281 return unify_success (explain_p
);
15283 if (strict
== DEDUCE_CONV
)
15285 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15286 explain_p
? tf_warning_or_error
: tf_none
))
15287 return unify_success (explain_p
);
15289 else if (strict
!= DEDUCE_EXACT
)
15291 if (can_convert_arg (parm
, type
,
15292 TYPE_P (arg
) ? NULL_TREE
: arg
,
15293 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15294 return unify_success (explain_p
);
15297 if (strict
== DEDUCE_EXACT
)
15298 return unify_type_mismatch (explain_p
, parm
, arg
);
15300 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15303 /* Subroutine of type_unification_real and unify_pack_expansion to
15304 handle unification of a single P/A pair. Parameters are as
15305 for those functions. */
15308 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15309 int subr
, unification_kind_t strict
, int flags
,
15312 tree arg_expr
= NULL_TREE
;
15315 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15316 return unify_invalid (explain_p
);
15317 if (arg
== unknown_type_node
)
15318 /* We can't deduce anything from this, but we might get all the
15319 template args from other function args. */
15320 return unify_success (explain_p
);
15322 /* FIXME uses_deducible_template_parms */
15323 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15324 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15330 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15331 | UNIFY_ALLOW_MORE_CV_QUAL
15332 | UNIFY_ALLOW_DERIVED
);
15336 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15340 arg_strict
= UNIFY_ALLOW_NONE
;
15344 gcc_unreachable ();
15347 /* We only do these transformations if this is the top-level
15348 parameter_type_list in a call or declaration matching; in other
15349 situations (nested function declarators, template argument lists) we
15350 won't be comparing a type to an expression, and we don't do any type
15356 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15357 if (type_unknown_p (arg
))
15359 /* [temp.deduct.type] A template-argument can be
15360 deduced from a pointer to function or pointer
15361 to member function argument if the set of
15362 overloaded functions does not contain function
15363 templates and at most one of a set of
15364 overloaded functions provides a unique
15367 if (resolve_overloaded_unification
15368 (tparms
, targs
, parm
, arg
, strict
,
15369 arg_strict
, explain_p
))
15370 return unify_success (explain_p
);
15371 return unify_overload_resolution_failure (explain_p
, arg
);
15375 arg
= unlowered_expr_type (arg
);
15376 if (arg
== error_mark_node
)
15377 return unify_invalid (explain_p
);
15381 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15384 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15385 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15387 /* For deduction from an init-list we need the actual list. */
15388 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15390 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15393 /* Most parms like fn_type_unification.
15395 If SUBR is 1, we're being called recursively (to unify the
15396 arguments of a function or method parameter of a function
15400 type_unification_real (tree tparms
,
15404 unsigned int xnargs
,
15406 unification_kind_t strict
,
15412 int ntparms
= TREE_VEC_LENGTH (tparms
);
15413 int saw_undeduced
= 0;
15416 unsigned int nargs
;
15419 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15420 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15421 gcc_assert (ntparms
> 0);
15423 /* Reset the number of non-defaulted template arguments contained
15425 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15433 while (parms
&& parms
!= void_list_node
15436 parm
= TREE_VALUE (parms
);
15438 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15439 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15440 /* For a function parameter pack that occurs at the end of the
15441 parameter-declaration-list, the type A of each remaining
15442 argument of the call is compared with the type P of the
15443 declarator-id of the function parameter pack. */
15446 parms
= TREE_CHAIN (parms
);
15448 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15449 /* For a function parameter pack that does not occur at the
15450 end of the parameter-declaration-list, the type of the
15451 parameter pack is a non-deduced context. */
15457 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15463 && parms
!= void_list_node
15464 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15466 /* Unify the remaining arguments with the pack expansion type. */
15468 tree parmvec
= make_tree_vec (1);
15470 /* Allocate a TREE_VEC and copy in all of the arguments */
15471 argvec
= make_tree_vec (nargs
- ia
);
15472 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15473 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15475 /* Copy the parameter into parmvec. */
15476 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15477 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15478 /*subr=*/subr
, explain_p
))
15481 /* Advance to the end of the list of parameters. */
15482 parms
= TREE_CHAIN (parms
);
15485 /* Fail if we've reached the end of the parm list, and more args
15486 are present, and the parm list isn't variadic. */
15487 if (ia
< nargs
&& parms
== void_list_node
)
15488 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15489 /* Fail if parms are left and they don't have default values. */
15490 if (parms
&& parms
!= void_list_node
15491 && TREE_PURPOSE (parms
) == NULL_TREE
)
15493 unsigned int count
= nargs
;
15495 while (p
&& p
!= void_list_node
)
15498 p
= TREE_CHAIN (p
);
15500 return unify_too_few_arguments (explain_p
, ia
, count
);
15505 tsubst_flags_t complain
= (explain_p
15506 ? tf_warning_or_error
15509 for (i
= 0; i
< ntparms
; i
++)
15511 tree targ
= TREE_VEC_ELT (targs
, i
);
15512 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15514 /* Clear the "incomplete" flags on all argument packs now so that
15515 substituting them into later default arguments works. */
15516 if (targ
&& ARGUMENT_PACK_P (targ
))
15518 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15519 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15522 if (targ
|| tparm
== error_mark_node
)
15524 tparm
= TREE_VALUE (tparm
);
15526 /* If this is an undeduced nontype parameter that depends on
15527 a type parameter, try another pass; its type may have been
15528 deduced from a later argument than the one from which
15529 this parameter can be deduced. */
15530 if (TREE_CODE (tparm
) == PARM_DECL
15531 && uses_template_parms (TREE_TYPE (tparm
))
15532 && !saw_undeduced
++)
15535 /* Core issue #226 (C++0x) [temp.deduct]:
15537 If a template argument has not been deduced, its
15538 default template argument, if any, is used.
15540 When we are in C++98 mode, TREE_PURPOSE will either
15541 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15542 to explicitly check cxx_dialect here. */
15543 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15545 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15546 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15547 location_t save_loc
= input_location
;
15549 input_location
= DECL_SOURCE_LOCATION (parm
);
15550 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15551 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15553 input_location
= save_loc
;
15554 if (arg
== error_mark_node
)
15558 TREE_VEC_ELT (targs
, i
) = arg
;
15559 /* The position of the first default template argument,
15560 is also the number of non-defaulted arguments in TARGS.
15562 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15563 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15568 /* If the type parameter is a parameter pack, then it will
15569 be deduced to an empty parameter pack. */
15570 if (template_parameter_pack_p (tparm
))
15574 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15576 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15577 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15578 TREE_CONSTANT (arg
) = 1;
15581 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15583 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15585 TREE_VEC_ELT (targs
, i
) = arg
;
15589 return unify_parameter_deduction_failure (explain_p
, tparm
);
15592 #ifdef ENABLE_CHECKING
15593 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15594 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15597 return unify_success (explain_p
);
15600 /* Subroutine of type_unification_real. Args are like the variables
15601 at the call site. ARG is an overloaded function (or template-id);
15602 we try deducing template args from each of the overloads, and if
15603 only one succeeds, we go with that. Modifies TARGS and returns
15604 true on success. */
15607 resolve_overloaded_unification (tree tparms
,
15611 unification_kind_t strict
,
15615 tree tempargs
= copy_node (targs
);
15617 tree goodfn
= NULL_TREE
;
15620 if (TREE_CODE (arg
) == ADDR_EXPR
)
15622 arg
= TREE_OPERAND (arg
, 0);
15628 if (TREE_CODE (arg
) == COMPONENT_REF
)
15629 /* Handle `&x' where `x' is some static or non-static member
15631 arg
= TREE_OPERAND (arg
, 1);
15633 if (TREE_CODE (arg
) == OFFSET_REF
)
15634 arg
= TREE_OPERAND (arg
, 1);
15636 /* Strip baselink information. */
15637 if (BASELINK_P (arg
))
15638 arg
= BASELINK_FUNCTIONS (arg
);
15640 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15642 /* If we got some explicit template args, we need to plug them into
15643 the affected templates before we try to unify, in case the
15644 explicit args will completely resolve the templates in question. */
15647 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15648 arg
= TREE_OPERAND (arg
, 0);
15650 for (; arg
; arg
= OVL_NEXT (arg
))
15652 tree fn
= OVL_CURRENT (arg
);
15653 tree subargs
, elem
;
15655 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15658 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15659 expl_subargs
, NULL_TREE
, tf_none
,
15660 /*require_all_args=*/true,
15661 /*use_default_args=*/true);
15662 if (subargs
!= error_mark_node
15663 && !any_dependent_template_arguments_p (subargs
))
15665 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15666 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15667 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15668 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15677 /* If no templates (or more than one) are fully resolved by the
15678 explicit arguments, this template-id is a non-deduced context; it
15679 could still be OK if we deduce all template arguments for the
15680 enclosing call through other arguments. */
15684 else if (TREE_CODE (arg
) != OVERLOAD
15685 && TREE_CODE (arg
) != FUNCTION_DECL
)
15686 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15687 -- but the deduction does not succeed because the expression is
15688 not just the function on its own. */
15691 for (; arg
; arg
= OVL_NEXT (arg
))
15692 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15693 TREE_TYPE (OVL_CURRENT (arg
)),
15694 strict
, sub_strict
, addr_p
, explain_p
)
15695 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15697 goodfn
= OVL_CURRENT (arg
);
15701 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15702 to function or pointer to member function argument if the set of
15703 overloaded functions does not contain function templates and at most
15704 one of a set of overloaded functions provides a unique match.
15706 So if we found multiple possibilities, we return success but don't
15707 deduce anything. */
15711 int i
= TREE_VEC_LENGTH (targs
);
15713 if (TREE_VEC_ELT (tempargs
, i
))
15714 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15722 /* Core DR 115: In contexts where deduction is done and fails, or in
15723 contexts where deduction is not done, if a template argument list is
15724 specified and it, along with any default template arguments, identifies
15725 a single function template specialization, then the template-id is an
15726 lvalue for the function template specialization. */
15729 resolve_nondeduced_context (tree orig_expr
)
15731 tree expr
, offset
, baselink
;
15734 if (!type_unknown_p (orig_expr
))
15739 offset
= NULL_TREE
;
15740 baselink
= NULL_TREE
;
15742 if (TREE_CODE (expr
) == ADDR_EXPR
)
15744 expr
= TREE_OPERAND (expr
, 0);
15747 if (TREE_CODE (expr
) == OFFSET_REF
)
15750 expr
= TREE_OPERAND (expr
, 1);
15752 if (BASELINK_P (expr
))
15755 expr
= BASELINK_FUNCTIONS (expr
);
15758 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15761 tree goodfn
= NULL_TREE
;
15763 /* If we got some explicit template args, we need to plug them into
15764 the affected templates before we try to unify, in case the
15765 explicit args will completely resolve the templates in question. */
15767 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15768 tree arg
= TREE_OPERAND (expr
, 0);
15769 tree badfn
= NULL_TREE
;
15770 tree badargs
= NULL_TREE
;
15772 for (; arg
; arg
= OVL_NEXT (arg
))
15774 tree fn
= OVL_CURRENT (arg
);
15775 tree subargs
, elem
;
15777 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15780 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15781 expl_subargs
, NULL_TREE
, tf_none
,
15782 /*require_all_args=*/true,
15783 /*use_default_args=*/true);
15784 if (subargs
!= error_mark_node
15785 && !any_dependent_template_arguments_p (subargs
))
15787 elem
= instantiate_template (fn
, subargs
, tf_none
);
15788 if (elem
== error_mark_node
)
15793 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15802 mark_used (goodfn
);
15805 expr
= build_baselink (BASELINK_BINFO (baselink
),
15806 BASELINK_ACCESS_BINFO (baselink
),
15807 expr
, BASELINK_OPTYPE (baselink
));
15811 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15812 expr
= build_offset_ref (base
, expr
, addr
);
15815 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15818 else if (good
== 0 && badargs
)
15819 /* There were no good options and at least one bad one, so let the
15820 user know what the problem is. */
15821 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15826 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15827 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15828 different overloads deduce different arguments for a given parm.
15829 ADDR_P is true if the expression for which deduction is being
15830 performed was of the form "& fn" rather than simply "fn".
15832 Returns 1 on success. */
15835 try_one_overload (tree tparms
,
15840 unification_kind_t strict
,
15849 if (arg
== error_mark_node
)
15852 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15853 to function or pointer to member function argument if the set of
15854 overloaded functions does not contain function templates and at most
15855 one of a set of overloaded functions provides a unique match.
15857 So if this is a template, just return success. */
15859 if (uses_template_parms (arg
))
15862 if (TREE_CODE (arg
) == METHOD_TYPE
)
15863 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15865 arg
= build_pointer_type (arg
);
15867 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15869 /* We don't copy orig_targs for this because if we have already deduced
15870 some template args from previous args, unify would complain when we
15871 try to deduce a template parameter for the same argument, even though
15872 there isn't really a conflict. */
15873 nargs
= TREE_VEC_LENGTH (targs
);
15874 tempargs
= make_tree_vec (nargs
);
15876 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15879 /* First make sure we didn't deduce anything that conflicts with
15880 explicitly specified args. */
15881 for (i
= nargs
; i
--; )
15883 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15884 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15888 else if (uses_template_parms (elt
))
15889 /* Since we're unifying against ourselves, we will fill in
15890 template args used in the function parm list with our own
15891 template parms. Discard them. */
15892 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15893 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15897 for (i
= nargs
; i
--; )
15899 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15902 TREE_VEC_ELT (targs
, i
) = elt
;
15908 /* PARM is a template class (perhaps with unbound template
15909 parameters). ARG is a fully instantiated type. If ARG can be
15910 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15911 TARGS are as for unify. */
15914 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15917 tree copy_of_targs
;
15919 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15920 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15921 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15924 /* We need to make a new template argument vector for the call to
15925 unify. If we used TARGS, we'd clutter it up with the result of
15926 the attempted unification, even if this class didn't work out.
15927 We also don't want to commit ourselves to all the unifications
15928 we've already done, since unification is supposed to be done on
15929 an argument-by-argument basis. In other words, consider the
15930 following pathological case:
15932 template <int I, int J, int K>
15935 template <int I, int J>
15936 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15938 template <int I, int J, int K>
15939 void f(S<I, J, K>, S<I, I, I>);
15948 Now, by the time we consider the unification involving `s2', we
15949 already know that we must have `f<0, 0, 0>'. But, even though
15950 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15951 because there are two ways to unify base classes of S<0, 1, 2>
15952 with S<I, I, I>. If we kept the already deduced knowledge, we
15953 would reject the possibility I=1. */
15954 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15956 /* If unification failed, we're done. */
15957 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15958 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15964 /* Given a template type PARM and a class type ARG, find the unique
15965 base type in ARG that is an instance of PARM. We do not examine
15966 ARG itself; only its base-classes. If there is not exactly one
15967 appropriate base class, return NULL_TREE. PARM may be the type of
15968 a partial specialization, as well as a plain template type. Used
15971 static enum template_base_result
15972 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15973 bool explain_p
, tree
*result
)
15975 tree rval
= NULL_TREE
;
15978 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15980 binfo
= TYPE_BINFO (complete_type (arg
));
15983 /* The type could not be completed. */
15984 *result
= NULL_TREE
;
15985 return tbr_incomplete_type
;
15988 /* Walk in inheritance graph order. The search order is not
15989 important, and this avoids multiple walks of virtual bases. */
15990 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15992 tree r
= try_class_unification (tparms
, targs
, parm
,
15993 BINFO_TYPE (binfo
), explain_p
);
15997 /* If there is more than one satisfactory baseclass, then:
16001 If they yield more than one possible deduced A, the type
16005 if (rval
&& !same_type_p (r
, rval
))
16007 *result
= NULL_TREE
;
16008 return tbr_ambiguous_baseclass
;
16016 return tbr_success
;
16019 /* Returns the level of DECL, which declares a template parameter. */
16022 template_decl_level (tree decl
)
16024 switch (TREE_CODE (decl
))
16027 case TEMPLATE_DECL
:
16028 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16031 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16034 gcc_unreachable ();
16039 /* Decide whether ARG can be unified with PARM, considering only the
16040 cv-qualifiers of each type, given STRICT as documented for unify.
16041 Returns nonzero iff the unification is OK on that basis. */
16044 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16046 int arg_quals
= cp_type_quals (arg
);
16047 int parm_quals
= cp_type_quals (parm
);
16049 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16050 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16052 /* Although a CVR qualifier is ignored when being applied to a
16053 substituted template parameter ([8.3.2]/1 for example), that
16054 does not allow us to unify "const T" with "int&" because both
16055 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16056 It is ok when we're allowing additional CV qualifiers
16057 at the outer level [14.8.2.1]/3,1st bullet. */
16058 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16059 || TREE_CODE (arg
) == FUNCTION_TYPE
16060 || TREE_CODE (arg
) == METHOD_TYPE
)
16061 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16064 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16065 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16069 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16070 && (arg_quals
& parm_quals
) != parm_quals
)
16073 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16074 && (parm_quals
& arg_quals
) != arg_quals
)
16080 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16082 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16084 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16085 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16086 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16088 *index
= TEMPLATE_TYPE_IDX (parm
);
16089 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16093 *index
= TEMPLATE_PARM_IDX (parm
);
16094 *level
= TEMPLATE_PARM_LEVEL (parm
);
16098 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16100 if (unify (TP, TA, P, A, S, EP)) \
16104 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16105 expansion at the end of PACKED_PARMS. Returns 0 if the type
16106 deduction succeeds, 1 otherwise. STRICT is the same as in
16107 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16108 call argument list. We'll need to adjust the arguments to make them
16109 types. SUBR tells us if this is from a recursive call to
16110 type_unification_real, or for comparing two template argument
16114 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16115 tree packed_args
, unification_kind_t strict
,
16116 bool subr
, bool explain_p
)
16119 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16120 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16121 tree pack
, packs
= NULL_TREE
;
16122 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16123 int len
= TREE_VEC_LENGTH (packed_args
);
16125 /* Determine the parameter packs we will be deducing from the
16126 pattern, and record their current deductions. */
16127 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16128 pack
; pack
= TREE_CHAIN (pack
))
16130 tree parm_pack
= TREE_VALUE (pack
);
16133 /* Determine the index and level of this parameter pack. */
16134 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16136 /* Keep track of the parameter packs and their corresponding
16138 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16139 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16142 /* Loop through all of the arguments that have not yet been
16143 unified and unify each with the pattern. */
16144 for (i
= start
; i
< len
; i
++)
16147 bool any_explicit
= false;
16148 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16150 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16151 or the element of its argument pack at the current index if
16152 this argument was explicitly specified. */
16153 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16157 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16160 if (TREE_VALUE (pack
)
16161 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16162 && (i
< TREE_VEC_LENGTH (pargs
)))
16164 any_explicit
= true;
16165 arg
= TREE_VEC_ELT (pargs
, i
);
16167 TMPL_ARG (targs
, level
, idx
) = arg
;
16170 /* If we had explicit template arguments, substitute them into the
16171 pattern before deduction. */
16174 /* Some arguments might still be unspecified or dependent. */
16176 ++processing_template_decl
;
16177 dependent
= any_dependent_template_arguments_p (targs
);
16179 --processing_template_decl
;
16180 parm
= tsubst (pattern
, targs
,
16181 explain_p
? tf_warning_or_error
: tf_none
,
16184 --processing_template_decl
;
16185 if (parm
== error_mark_node
)
16191 /* Unify the pattern with the current argument. */
16192 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16193 LOOKUP_IMPLICIT
, explain_p
))
16196 /* For each parameter pack, collect the deduced value. */
16197 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16200 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16202 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16203 TMPL_ARG (targs
, level
, idx
);
16207 /* Verify that the results of unification with the parameter packs
16208 produce results consistent with what we've seen before, and make
16209 the deduced argument packs available. */
16210 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16212 tree old_pack
= TREE_VALUE (pack
);
16213 tree new_args
= TREE_TYPE (pack
);
16214 int i
, len
= TREE_VEC_LENGTH (new_args
);
16216 bool nondeduced_p
= false;
16218 /* By default keep the original deduced argument pack.
16219 If necessary, more specific code is going to update the
16220 resulting deduced argument later down in this function. */
16221 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16222 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16224 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16225 actually deduce anything. */
16226 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16227 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16228 nondeduced_p
= true;
16232 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16234 /* If we had fewer function args than explicit template args,
16235 just use the explicits. */
16236 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16237 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16238 if (len
< explicit_len
)
16239 new_args
= explicit_args
;
16245 /* Build the deduced *_ARGUMENT_PACK. */
16246 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16248 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16249 TREE_TYPE (result
) =
16250 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16251 TREE_CONSTANT (result
) = 1;
16254 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16256 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16258 /* Note the deduced argument packs for this parameter
16260 TMPL_ARG (targs
, level
, idx
) = result
;
16262 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16263 && (ARGUMENT_PACK_ARGS (old_pack
)
16264 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16266 /* We only had the explicitly-provided arguments before, but
16267 now we have a complete set of arguments. */
16268 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16270 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16271 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16272 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16276 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16277 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16279 if (!comp_template_args_with_info (old_args
, new_args
,
16280 &bad_old_arg
, &bad_new_arg
))
16281 /* Inconsistent unification of this parameter pack. */
16282 return unify_parameter_pack_inconsistent (explain_p
,
16288 return unify_success (explain_p
);
16291 /* Deduce the value of template parameters. TPARMS is the (innermost)
16292 set of template parameters to a template. TARGS is the bindings
16293 for those template parameters, as determined thus far; TARGS may
16294 include template arguments for outer levels of template parameters
16295 as well. PARM is a parameter to a template function, or a
16296 subcomponent of that parameter; ARG is the corresponding argument.
16297 This function attempts to match PARM with ARG in a manner
16298 consistent with the existing assignments in TARGS. If more values
16299 are deduced, then TARGS is updated.
16301 Returns 0 if the type deduction succeeds, 1 otherwise. The
16302 parameter STRICT is a bitwise or of the following flags:
16305 Require an exact match between PARM and ARG.
16306 UNIFY_ALLOW_MORE_CV_QUAL:
16307 Allow the deduced ARG to be more cv-qualified (by qualification
16308 conversion) than ARG.
16309 UNIFY_ALLOW_LESS_CV_QUAL:
16310 Allow the deduced ARG to be less cv-qualified than ARG.
16311 UNIFY_ALLOW_DERIVED:
16312 Allow the deduced ARG to be a template base class of ARG,
16313 or a pointer to a template base class of the type pointed to by
16315 UNIFY_ALLOW_INTEGER:
16316 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16317 case for more information.
16318 UNIFY_ALLOW_OUTER_LEVEL:
16319 This is the outermost level of a deduction. Used to determine validity
16320 of qualification conversions. A valid qualification conversion must
16321 have const qualified pointers leading up to the inner type which
16322 requires additional CV quals, except at the outer level, where const
16323 is not required [conv.qual]. It would be normal to set this flag in
16324 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16325 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16326 This is the outermost level of a deduction, and PARM can be more CV
16327 qualified at this point.
16328 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16329 This is the outermost level of a deduction, and PARM can be less CV
16330 qualified at this point. */
16333 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16339 int strict_in
= strict
;
16341 /* I don't think this will do the right thing with respect to types.
16342 But the only case I've seen it in so far has been array bounds, where
16343 signedness is the only information lost, and I think that will be
16345 while (TREE_CODE (parm
) == NOP_EXPR
)
16346 parm
= TREE_OPERAND (parm
, 0);
16348 if (arg
== error_mark_node
)
16349 return unify_invalid (explain_p
);
16350 if (arg
== unknown_type_node
16351 || arg
== init_list_type_node
)
16352 /* We can't deduce anything from this, but we might get all the
16353 template args from other function args. */
16354 return unify_success (explain_p
);
16356 /* If PARM uses template parameters, then we can't bail out here,
16357 even if ARG == PARM, since we won't record unifications for the
16358 template parameters. We might need them if we're trying to
16359 figure out which of two things is more specialized. */
16360 if (arg
== parm
&& !uses_template_parms (parm
))
16361 return unify_success (explain_p
);
16363 /* Handle init lists early, so the rest of the function can assume
16364 we're dealing with a type. */
16365 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16369 tree orig_parm
= parm
;
16371 /* Replace T with std::initializer_list<T> for deduction. */
16372 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16373 && flag_deduce_init_list
)
16374 parm
= listify (parm
);
16376 if (!is_std_init_list (parm
))
16377 /* We can only deduce from an initializer list argument if the
16378 parameter is std::initializer_list; otherwise this is a
16379 non-deduced context. */
16380 return unify_success (explain_p
);
16382 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16384 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16386 int elt_strict
= strict
;
16388 if (elt
== error_mark_node
)
16389 return unify_invalid (explain_p
);
16391 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16393 tree type
= TREE_TYPE (elt
);
16394 /* It should only be possible to get here for a call. */
16395 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16396 elt_strict
|= maybe_adjust_types_for_deduction
16397 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16401 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16405 /* If the std::initializer_list<T> deduction worked, replace the
16406 deduced A with std::initializer_list<A>. */
16407 if (orig_parm
!= parm
)
16409 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16410 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16411 targ
= listify (targ
);
16412 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16414 return unify_success (explain_p
);
16417 /* Immediately reject some pairs that won't unify because of
16418 cv-qualification mismatches. */
16419 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16421 /* It is the elements of the array which hold the cv quals of an array
16422 type, and the elements might be template type parms. We'll check
16423 when we recurse. */
16424 && TREE_CODE (arg
) != ARRAY_TYPE
16425 /* We check the cv-qualifiers when unifying with template type
16426 parameters below. We want to allow ARG `const T' to unify with
16427 PARM `T' for example, when computing which of two templates
16428 is more specialized, for example. */
16429 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16430 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16431 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16433 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16434 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16435 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16436 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16437 strict
&= ~UNIFY_ALLOW_DERIVED
;
16438 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16439 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16441 switch (TREE_CODE (parm
))
16443 case TYPENAME_TYPE
:
16445 case UNBOUND_CLASS_TEMPLATE
:
16446 /* In a type which contains a nested-name-specifier, template
16447 argument values cannot be deduced for template parameters used
16448 within the nested-name-specifier. */
16449 return unify_success (explain_p
);
16451 case TEMPLATE_TYPE_PARM
:
16452 case TEMPLATE_TEMPLATE_PARM
:
16453 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16454 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16455 if (tparm
== error_mark_node
)
16456 return unify_invalid (explain_p
);
16458 if (TEMPLATE_TYPE_LEVEL (parm
)
16459 != template_decl_level (tparm
))
16460 /* The PARM is not one we're trying to unify. Just check
16461 to see if it matches ARG. */
16463 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16464 && (is_auto (parm
) ? is_auto (arg
)
16465 : same_type_p (parm
, arg
)))
16466 return unify_success (explain_p
);
16468 return unify_type_mismatch (explain_p
, parm
, arg
);
16470 idx
= TEMPLATE_TYPE_IDX (parm
);
16471 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16472 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16473 if (tparm
== error_mark_node
)
16474 return unify_invalid (explain_p
);
16476 /* Check for mixed types and values. */
16477 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16478 && TREE_CODE (tparm
) != TYPE_DECL
)
16479 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16480 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16481 gcc_unreachable ();
16483 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16485 /* ARG must be constructed from a template class or a template
16486 template parameter. */
16487 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16488 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16489 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16492 tree parmvec
= TYPE_TI_ARGS (parm
);
16493 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16494 tree full_argvec
= add_to_template_args (targs
, argvec
);
16496 = DECL_INNERMOST_TEMPLATE_PARMS
16497 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16499 int parm_variadic_p
= 0;
16501 /* The resolution to DR150 makes clear that default
16502 arguments for an N-argument may not be used to bind T
16503 to a template template parameter with fewer than N
16504 parameters. It is not safe to permit the binding of
16505 default arguments as an extension, as that may change
16506 the meaning of a conforming program. Consider:
16508 struct Dense { static const unsigned int dim = 1; };
16510 template <template <typename> class View,
16512 void operator+(float, View<Block> const&);
16514 template <typename Block,
16515 unsigned int Dim = Block::dim>
16516 struct Lvalue_proxy { operator float() const; };
16520 Lvalue_proxy<Dense> p;
16525 Here, if Lvalue_proxy is permitted to bind to View, then
16526 the global operator+ will be used; if they are not, the
16527 Lvalue_proxy will be converted to float. */
16528 if (coerce_template_parms (parm_parms
,
16530 TYPE_TI_TEMPLATE (parm
),
16532 ? tf_warning_or_error
16534 /*require_all_args=*/true,
16535 /*use_default_args=*/false)
16536 == error_mark_node
)
16539 /* Deduce arguments T, i from TT<T> or TT<i>.
16540 We check each element of PARMVEC and ARGVEC individually
16541 rather than the whole TREE_VEC since they can have
16542 different number of elements. */
16544 parmvec
= expand_template_argument_pack (parmvec
);
16545 argvec
= expand_template_argument_pack (argvec
);
16547 len
= TREE_VEC_LENGTH (parmvec
);
16549 /* Check if the parameters end in a pack, making them
16552 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16553 parm_variadic_p
= 1;
16555 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16556 /* If the template argument list of P contains a pack
16557 expansion that is not the last template argument, the
16558 entire template argument list is a non-deduced
16560 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
16561 return unify_success (explain_p
);
16563 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16564 return unify_too_few_arguments (explain_p
,
16565 TREE_VEC_LENGTH (argvec
), len
);
16567 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16569 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16570 TREE_VEC_ELT (parmvec
, i
),
16571 TREE_VEC_ELT (argvec
, i
),
16572 UNIFY_ALLOW_NONE
, explain_p
);
16575 if (parm_variadic_p
16576 && unify_pack_expansion (tparms
, targs
,
16579 /*subr=*/true, explain_p
))
16582 arg
= TYPE_TI_TEMPLATE (arg
);
16584 /* Fall through to deduce template name. */
16587 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16588 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16590 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16592 /* Simple cases: Value already set, does match or doesn't. */
16593 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16594 return unify_success (explain_p
);
16596 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16600 /* If PARM is `const T' and ARG is only `int', we don't have
16601 a match unless we are allowing additional qualification.
16602 If ARG is `const int' and PARM is just `T' that's OK;
16603 that binds `const int' to `T'. */
16604 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16606 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16608 /* Consider the case where ARG is `const volatile int' and
16609 PARM is `const T'. Then, T should be `volatile int'. */
16610 arg
= cp_build_qualified_type_real
16611 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16612 if (arg
== error_mark_node
)
16613 return unify_invalid (explain_p
);
16615 /* Simple cases: Value already set, does match or doesn't. */
16616 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16617 return unify_success (explain_p
);
16619 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16621 /* Make sure that ARG is not a variable-sized array. (Note
16622 that were talking about variable-sized arrays (like
16623 `int[n]'), rather than arrays of unknown size (like
16624 `int[]').) We'll get very confused by such a type since
16625 the bound of the array is not constant, and therefore
16626 not mangleable. Besides, such types are not allowed in
16627 ISO C++, so we can do as we please here. We do allow
16628 them for 'auto' deduction, since that isn't ABI-exposed. */
16629 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16630 return unify_vla_arg (explain_p
, arg
);
16632 /* Strip typedefs as in convert_template_argument. */
16633 arg
= canonicalize_type_argument (arg
, tf_none
);
16636 /* If ARG is a parameter pack or an expansion, we cannot unify
16637 against it unless PARM is also a parameter pack. */
16638 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16639 && !template_parameter_pack_p (parm
))
16640 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16642 /* If the argument deduction results is a METHOD_TYPE,
16643 then there is a problem.
16644 METHOD_TYPE doesn't map to any real C++ type the result of
16645 the deduction can not be of that type. */
16646 if (TREE_CODE (arg
) == METHOD_TYPE
)
16647 return unify_method_type_error (explain_p
, arg
);
16649 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16650 return unify_success (explain_p
);
16652 case TEMPLATE_PARM_INDEX
:
16653 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16654 if (tparm
== error_mark_node
)
16655 return unify_invalid (explain_p
);
16657 if (TEMPLATE_PARM_LEVEL (parm
)
16658 != template_decl_level (tparm
))
16660 /* The PARM is not one we're trying to unify. Just check
16661 to see if it matches ARG. */
16662 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16663 && cp_tree_equal (parm
, arg
));
16665 unify_expression_unequal (explain_p
, parm
, arg
);
16669 idx
= TEMPLATE_PARM_IDX (parm
);
16670 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16674 int x
= !cp_tree_equal (targ
, arg
);
16676 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16680 /* [temp.deduct.type] If, in the declaration of a function template
16681 with a non-type template-parameter, the non-type
16682 template-parameter is used in an expression in the function
16683 parameter-list and, if the corresponding template-argument is
16684 deduced, the template-argument type shall match the type of the
16685 template-parameter exactly, except that a template-argument
16686 deduced from an array bound may be of any integral type.
16687 The non-type parameter might use already deduced type parameters. */
16688 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16689 if (!TREE_TYPE (arg
))
16690 /* Template-parameter dependent expression. Just accept it for now.
16691 It will later be processed in convert_template_argument. */
16693 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16695 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16696 && (TREE_CODE (tparm
) == INTEGER_TYPE
16697 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16698 /* Convert the ARG to the type of PARM; the deduced non-type
16699 template argument must exactly match the types of the
16700 corresponding parameter. */
16701 arg
= fold (build_nop (tparm
, arg
));
16702 else if (uses_template_parms (tparm
))
16703 /* We haven't deduced the type of this parameter yet. Try again
16705 return unify_success (explain_p
);
16707 return unify_type_mismatch (explain_p
, tparm
, arg
);
16709 /* If ARG is a parameter pack or an expansion, we cannot unify
16710 against it unless PARM is also a parameter pack. */
16711 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16712 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16713 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16715 arg
= strip_typedefs_expr (arg
);
16716 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16717 return unify_success (explain_p
);
16721 /* A pointer-to-member constant can be unified only with
16722 another constant. */
16723 if (TREE_CODE (arg
) != PTRMEM_CST
)
16724 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16726 /* Just unify the class member. It would be useless (and possibly
16727 wrong, depending on the strict flags) to unify also
16728 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16729 arg refer to the same variable, even if through different
16730 classes. For instance:
16732 struct A { int x; };
16735 Unification of &A::x and &B::x must succeed. */
16736 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16737 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16742 if (TREE_CODE (arg
) != POINTER_TYPE
)
16743 return unify_type_mismatch (explain_p
, parm
, arg
);
16745 /* [temp.deduct.call]
16747 A can be another pointer or pointer to member type that can
16748 be converted to the deduced A via a qualification
16749 conversion (_conv.qual_).
16751 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16752 This will allow for additional cv-qualification of the
16753 pointed-to types if appropriate. */
16755 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16756 /* The derived-to-base conversion only persists through one
16757 level of pointers. */
16758 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16760 return unify (tparms
, targs
, TREE_TYPE (parm
),
16761 TREE_TYPE (arg
), strict
, explain_p
);
16764 case REFERENCE_TYPE
:
16765 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16766 return unify_type_mismatch (explain_p
, parm
, arg
);
16767 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16768 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16771 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16772 return unify_type_mismatch (explain_p
, parm
, arg
);
16773 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16774 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16775 return unify_type_mismatch (explain_p
, parm
, arg
);
16776 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16777 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16778 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16785 /* Our representation of array types uses "N - 1" as the
16786 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16787 not an integer constant. We cannot unify arbitrarily
16788 complex expressions, so we eliminate the MINUS_EXPRs
16790 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16791 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16794 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16795 parm_max
= TREE_OPERAND (parm_max
, 0);
16797 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16798 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16801 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16802 trying to unify the type of a variable with the type
16803 of a template parameter. For example:
16805 template <unsigned int N>
16806 void f (char (&) [N]);
16813 Here, the type of the ARG will be "int [g(i)]", and
16814 may be a SAVE_EXPR, etc. */
16815 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16816 return unify_vla_arg (explain_p
, arg
);
16817 arg_max
= TREE_OPERAND (arg_max
, 0);
16820 /* If only one of the bounds used a MINUS_EXPR, compensate
16821 by adding one to the other bound. */
16822 if (parm_cst
&& !arg_cst
)
16823 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16827 else if (arg_cst
&& !parm_cst
)
16828 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16833 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16834 UNIFY_ALLOW_INTEGER
, explain_p
);
16836 return unify_success (explain_p
);
16843 case ENUMERAL_TYPE
:
16846 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16847 return unify_type_mismatch (explain_p
, parm
, arg
);
16849 /* We have already checked cv-qualification at the top of the
16851 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16852 return unify_type_mismatch (explain_p
, parm
, arg
);
16854 /* As far as unification is concerned, this wins. Later checks
16855 will invalidate it if necessary. */
16856 return unify_success (explain_p
);
16858 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16859 /* Type INTEGER_CST can come from ordinary constant template args. */
16861 while (TREE_CODE (arg
) == NOP_EXPR
)
16862 arg
= TREE_OPERAND (arg
, 0);
16864 if (TREE_CODE (arg
) != INTEGER_CST
)
16865 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16866 return (tree_int_cst_equal (parm
, arg
)
16867 ? unify_success (explain_p
)
16868 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16872 int i
, len
, argslen
;
16873 int parm_variadic_p
= 0;
16875 if (TREE_CODE (arg
) != TREE_VEC
)
16876 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16878 len
= TREE_VEC_LENGTH (parm
);
16879 argslen
= TREE_VEC_LENGTH (arg
);
16881 /* Check for pack expansions in the parameters. */
16882 for (i
= 0; i
< len
; ++i
)
16884 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16887 /* We can unify against something with a trailing
16889 parm_variadic_p
= 1;
16891 /* [temp.deduct.type]/9: If the template argument list of
16892 P contains a pack expansion that is not the last
16893 template argument, the entire template argument list
16894 is a non-deduced context. */
16895 return unify_success (explain_p
);
16899 /* If we don't have enough arguments to satisfy the parameters
16900 (not counting the pack expression at the end), or we have
16901 too many arguments for a parameter list that doesn't end in
16902 a pack expression, we can't unify. */
16903 if (parm_variadic_p
16904 ? argslen
< len
- parm_variadic_p
16906 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16908 /* Unify all of the parameters that precede the (optional)
16909 pack expression. */
16910 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16912 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16913 TREE_VEC_ELT (parm
, i
),
16914 TREE_VEC_ELT (arg
, i
),
16915 UNIFY_ALLOW_NONE
, explain_p
);
16917 if (parm_variadic_p
)
16918 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16920 /*subr=*/true, explain_p
);
16921 return unify_success (explain_p
);
16926 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16927 return unify_type_mismatch (explain_p
, parm
, arg
);
16929 if (TYPE_PTRMEMFUNC_P (parm
))
16931 if (!TYPE_PTRMEMFUNC_P (arg
))
16932 return unify_type_mismatch (explain_p
, parm
, arg
);
16934 return unify (tparms
, targs
,
16935 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16936 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16937 strict
, explain_p
);
16940 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16942 tree t
= NULL_TREE
;
16944 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16946 /* First, we try to unify the PARM and ARG directly. */
16947 t
= try_class_unification (tparms
, targs
,
16948 parm
, arg
, explain_p
);
16952 /* Fallback to the special case allowed in
16953 [temp.deduct.call]:
16955 If P is a class, and P has the form
16956 template-id, then A can be a derived class of
16957 the deduced A. Likewise, if P is a pointer to
16958 a class of the form template-id, A can be a
16959 pointer to a derived class pointed to by the
16961 enum template_base_result r
;
16962 r
= get_template_base (tparms
, targs
, parm
, arg
,
16966 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16969 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16970 && (CLASSTYPE_TI_TEMPLATE (parm
)
16971 == CLASSTYPE_TI_TEMPLATE (arg
)))
16972 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16973 Then, we should unify `int' and `U'. */
16976 /* There's no chance of unification succeeding. */
16977 return unify_type_mismatch (explain_p
, parm
, arg
);
16979 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16980 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16982 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16983 return unify_type_mismatch (explain_p
, parm
, arg
);
16984 return unify_success (explain_p
);
16987 case FUNCTION_TYPE
:
16989 unsigned int nargs
;
16994 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16995 return unify_type_mismatch (explain_p
, parm
, arg
);
16997 /* CV qualifications for methods can never be deduced, they must
16998 match exactly. We need to check them explicitly here,
16999 because type_unification_real treats them as any other
17000 cv-qualified parameter. */
17001 if (TREE_CODE (parm
) == METHOD_TYPE
17002 && (!check_cv_quals_for_unify
17004 class_of_this_parm (arg
),
17005 class_of_this_parm (parm
))))
17006 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17008 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17009 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17011 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17012 args
= XALLOCAVEC (tree
, nargs
);
17013 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17014 a
!= NULL_TREE
&& a
!= void_list_node
;
17015 a
= TREE_CHAIN (a
), ++i
)
17016 args
[i
] = TREE_VALUE (a
);
17019 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17020 args
, nargs
, 1, DEDUCE_EXACT
,
17021 LOOKUP_NORMAL
, explain_p
);
17025 /* Unify a pointer to member with a pointer to member function, which
17026 deduces the type of the member as a function type. */
17027 if (TYPE_PTRMEMFUNC_P (arg
))
17032 /* Check top-level cv qualifiers */
17033 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17034 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17036 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17037 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17038 UNIFY_ALLOW_NONE
, explain_p
);
17040 /* Determine the type of the function we are unifying against. */
17041 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
17043 build_function_type (TREE_TYPE (method_type
),
17044 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
17046 /* Extract the cv-qualifiers of the member function from the
17047 implicit object parameter and place them on the function
17048 type to be restored later. */
17049 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
17050 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17053 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17054 return unify_type_mismatch (explain_p
, parm
, arg
);
17055 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17056 TYPE_OFFSET_BASETYPE (arg
),
17057 UNIFY_ALLOW_NONE
, explain_p
);
17058 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17059 strict
, explain_p
);
17062 if (DECL_TEMPLATE_PARM_P (parm
))
17063 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17064 if (arg
!= integral_constant_value (parm
))
17065 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17066 return unify_success (explain_p
);
17069 case TEMPLATE_DECL
:
17070 /* Matched cases are handled by the ARG == PARM test above. */
17071 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17074 /* A non-type template parameter that is a variable should be a
17075 an integral constant, in which case, it whould have been
17076 folded into its (constant) value. So we should not be getting
17077 a variable here. */
17078 gcc_unreachable ();
17080 case TYPE_ARGUMENT_PACK
:
17081 case NONTYPE_ARGUMENT_PACK
:
17082 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17083 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17086 case DECLTYPE_TYPE
:
17087 case UNDERLYING_TYPE
:
17088 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17089 or UNDERLYING_TYPE nodes. */
17090 return unify_success (explain_p
);
17093 /* Unification fails if we hit an error node. */
17094 return unify_invalid (explain_p
);
17097 /* An unresolved overload is a nondeduced context. */
17098 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17099 return unify_success (explain_p
);
17100 gcc_assert (EXPR_P (parm
));
17102 /* We must be looking at an expression. This can happen with
17106 void foo(S<I>, S<I + 2>);
17108 This is a "nondeduced context":
17112 The nondeduced contexts are:
17114 --A type that is a template-id in which one or more of
17115 the template-arguments is an expression that references
17116 a template-parameter.
17118 In these cases, we assume deduction succeeded, but don't
17119 actually infer any unifications. */
17121 if (!uses_template_parms (parm
)
17122 && !template_args_equal (parm
, arg
))
17123 return unify_expression_unequal (explain_p
, parm
, arg
);
17125 return unify_success (explain_p
);
17128 #undef RECUR_AND_CHECK_FAILURE
17130 /* Note that DECL can be defined in this translation unit, if
17134 mark_definable (tree decl
)
17137 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17138 FOR_EACH_CLONE (clone
, decl
)
17139 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17142 /* Called if RESULT is explicitly instantiated, or is a member of an
17143 explicitly instantiated class. */
17146 mark_decl_instantiated (tree result
, int extern_p
)
17148 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17150 /* If this entity has already been written out, it's too late to
17151 make any modifications. */
17152 if (TREE_ASM_WRITTEN (result
))
17155 if (TREE_CODE (result
) != FUNCTION_DECL
)
17156 /* The TREE_PUBLIC flag for function declarations will have been
17157 set correctly by tsubst. */
17158 TREE_PUBLIC (result
) = 1;
17160 /* This might have been set by an earlier implicit instantiation. */
17161 DECL_COMDAT (result
) = 0;
17164 DECL_NOT_REALLY_EXTERN (result
) = 0;
17167 mark_definable (result
);
17168 /* Always make artificials weak. */
17169 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17170 comdat_linkage (result
);
17171 /* For WIN32 we also want to put explicit instantiations in
17172 linkonce sections. */
17173 else if (TREE_PUBLIC (result
))
17174 maybe_make_one_only (result
);
17177 /* If EXTERN_P, then this function will not be emitted -- unless
17178 followed by an explicit instantiation, at which point its linkage
17179 will be adjusted. If !EXTERN_P, then this function will be
17180 emitted here. In neither circumstance do we want
17181 import_export_decl to adjust the linkage. */
17182 DECL_INTERFACE_KNOWN (result
) = 1;
17185 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17186 important template arguments. If any are missing, we check whether
17187 they're important by using error_mark_node for substituting into any
17188 args that were used for partial ordering (the ones between ARGS and END)
17189 and seeing if it bubbles up. */
17192 check_undeduced_parms (tree targs
, tree args
, tree end
)
17194 bool found
= false;
17196 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17197 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17200 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17204 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17205 if (substed
== error_mark_node
)
17211 /* Given two function templates PAT1 and PAT2, return:
17213 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17214 -1 if PAT2 is more specialized than PAT1.
17215 0 if neither is more specialized.
17217 LEN indicates the number of parameters we should consider
17218 (defaulted parameters should not be considered).
17220 The 1998 std underspecified function template partial ordering, and
17221 DR214 addresses the issue. We take pairs of arguments, one from
17222 each of the templates, and deduce them against each other. One of
17223 the templates will be more specialized if all the *other*
17224 template's arguments deduce against its arguments and at least one
17225 of its arguments *does* *not* deduce against the other template's
17226 corresponding argument. Deduction is done as for class templates.
17227 The arguments used in deduction have reference and top level cv
17228 qualifiers removed. Iff both arguments were originally reference
17229 types *and* deduction succeeds in both directions, the template
17230 with the more cv-qualified argument wins for that pairing (if
17231 neither is more cv-qualified, they both are equal). Unlike regular
17232 deduction, after all the arguments have been deduced in this way,
17233 we do *not* verify the deduced template argument values can be
17234 substituted into non-deduced contexts.
17236 The logic can be a bit confusing here, because we look at deduce1 and
17237 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17238 can find template arguments for pat1 to make arg1 look like arg2, that
17239 means that arg2 is at least as specialized as arg1. */
17242 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17244 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17245 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17246 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17247 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17248 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17249 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17250 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17251 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17252 tree origs1
, origs2
;
17253 bool lose1
= false;
17254 bool lose2
= false;
17256 /* Remove the this parameter from non-static member functions. If
17257 one is a non-static member function and the other is not a static
17258 member function, remove the first parameter from that function
17259 also. This situation occurs for operator functions where we
17260 locate both a member function (with this pointer) and non-member
17261 operator (with explicit first operand). */
17262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17264 len
--; /* LEN is the number of significant arguments for DECL1 */
17265 args1
= TREE_CHAIN (args1
);
17266 if (!DECL_STATIC_FUNCTION_P (decl2
))
17267 args2
= TREE_CHAIN (args2
);
17269 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17271 args2
= TREE_CHAIN (args2
);
17272 if (!DECL_STATIC_FUNCTION_P (decl1
))
17275 args1
= TREE_CHAIN (args1
);
17279 /* If only one is a conversion operator, they are unordered. */
17280 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17283 /* Consider the return type for a conversion function */
17284 if (DECL_CONV_FN_P (decl1
))
17286 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17287 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17291 processing_template_decl
++;
17297 /* Stop when an ellipsis is seen. */
17298 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17300 tree arg1
= TREE_VALUE (args1
);
17301 tree arg2
= TREE_VALUE (args2
);
17302 int deduce1
, deduce2
;
17306 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17307 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17309 /* When both arguments are pack expansions, we need only
17310 unify the patterns themselves. */
17311 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17312 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17314 /* This is the last comparison we need to do. */
17318 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17320 arg1
= TREE_TYPE (arg1
);
17321 quals1
= cp_type_quals (arg1
);
17324 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17326 arg2
= TREE_TYPE (arg2
);
17327 quals2
= cp_type_quals (arg2
);
17330 arg1
= TYPE_MAIN_VARIANT (arg1
);
17331 arg2
= TYPE_MAIN_VARIANT (arg2
);
17333 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17335 int i
, len2
= list_length (args2
);
17336 tree parmvec
= make_tree_vec (1);
17337 tree argvec
= make_tree_vec (len2
);
17340 /* Setup the parameter vector, which contains only ARG1. */
17341 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17343 /* Setup the argument vector, which contains the remaining
17345 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17346 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17348 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17349 argvec
, DEDUCE_EXACT
,
17350 /*subr=*/true, /*explain_p=*/false)
17353 /* We cannot deduce in the other direction, because ARG1 is
17354 a pack expansion but ARG2 is not. */
17357 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17359 int i
, len1
= list_length (args1
);
17360 tree parmvec
= make_tree_vec (1);
17361 tree argvec
= make_tree_vec (len1
);
17364 /* Setup the parameter vector, which contains only ARG1. */
17365 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17367 /* Setup the argument vector, which contains the remaining
17369 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17370 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17372 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17373 argvec
, DEDUCE_EXACT
,
17374 /*subr=*/true, /*explain_p=*/false)
17377 /* We cannot deduce in the other direction, because ARG2 is
17378 a pack expansion but ARG1 is not.*/
17384 /* The normal case, where neither argument is a pack
17386 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17387 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17389 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17390 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17394 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17395 arg2, then arg2 is not as specialized as arg1. */
17401 /* "If, for a given type, deduction succeeds in both directions
17402 (i.e., the types are identical after the transformations above)
17403 and if the type from the argument template is more cv-qualified
17404 than the type from the parameter template (as described above)
17405 that type is considered to be more specialized than the other. If
17406 neither type is more cv-qualified than the other then neither type
17407 is more specialized than the other." */
17409 if (deduce1
&& deduce2
17410 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17412 if ((quals1
& quals2
) == quals2
)
17414 if ((quals1
& quals2
) == quals1
)
17418 if (lose1
&& lose2
)
17419 /* We've failed to deduce something in either direction.
17420 These must be unordered. */
17423 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17424 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17425 /* We have already processed all of the arguments in our
17426 handing of the pack expansion type. */
17429 args1
= TREE_CHAIN (args1
);
17430 args2
= TREE_CHAIN (args2
);
17433 /* "In most cases, all template parameters must have values in order for
17434 deduction to succeed, but for partial ordering purposes a template
17435 parameter may remain without a value provided it is not used in the
17436 types being used for partial ordering."
17438 Thus, if we are missing any of the targs1 we need to substitute into
17439 origs1, then pat2 is not as specialized as pat1. This can happen when
17440 there is a nondeduced context. */
17441 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17443 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17446 processing_template_decl
--;
17448 /* All things being equal, if the next argument is a pack expansion
17449 for one function but not for the other, prefer the
17450 non-variadic function. FIXME this is bogus; see c++/41958. */
17452 && args1
&& TREE_VALUE (args1
)
17453 && args2
&& TREE_VALUE (args2
))
17455 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17456 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17459 if (lose1
== lose2
)
17467 /* Determine which of two partial specializations of MAIN_TMPL is more
17470 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17471 to the first partial specialization. The TREE_VALUE is the
17472 innermost set of template parameters for the partial
17473 specialization. PAT2 is similar, but for the second template.
17475 Return 1 if the first partial specialization is more specialized;
17476 -1 if the second is more specialized; 0 if neither is more
17479 See [temp.class.order] for information about determining which of
17480 two templates is more specialized. */
17483 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17488 bool any_deductions
= false;
17490 tmpl1
= TREE_TYPE (pat1
);
17491 tmpl2
= TREE_TYPE (pat2
);
17493 /* Just like what happens for functions, if we are ordering between
17494 different class template specializations, we may encounter dependent
17495 types in the arguments, and we need our dependency check functions
17496 to behave correctly. */
17497 ++processing_template_decl
;
17498 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17499 CLASSTYPE_TI_ARGS (tmpl1
),
17500 CLASSTYPE_TI_ARGS (tmpl2
));
17504 any_deductions
= true;
17507 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17508 CLASSTYPE_TI_ARGS (tmpl2
),
17509 CLASSTYPE_TI_ARGS (tmpl1
));
17513 any_deductions
= true;
17515 --processing_template_decl
;
17517 /* In the case of a tie where at least one of the class templates
17518 has a parameter pack at the end, the template with the most
17519 non-packed parameters wins. */
17522 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17523 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17525 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17526 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17527 int len1
= TREE_VEC_LENGTH (args1
);
17528 int len2
= TREE_VEC_LENGTH (args2
);
17530 /* We don't count the pack expansion at the end. */
17531 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17533 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17538 else if (len1
< len2
)
17545 /* Return the template arguments that will produce the function signature
17546 DECL from the function template FN, with the explicit template
17547 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17548 also match. Return NULL_TREE if no satisfactory arguments could be
17552 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17554 int ntparms
= DECL_NTPARMS (fn
);
17555 tree targs
= make_tree_vec (ntparms
);
17556 tree decl_type
= TREE_TYPE (decl
);
17557 tree decl_arg_types
;
17559 unsigned int nargs
, ix
;
17562 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17564 /* Never do unification on the 'this' parameter. */
17565 decl_arg_types
= skip_artificial_parms_for (decl
,
17566 TYPE_ARG_TYPES (decl_type
));
17568 nargs
= list_length (decl_arg_types
);
17569 args
= XALLOCAVEC (tree
, nargs
);
17570 for (arg
= decl_arg_types
, ix
= 0;
17571 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17572 arg
= TREE_CHAIN (arg
), ++ix
)
17573 args
[ix
] = TREE_VALUE (arg
);
17575 if (fn_type_unification (fn
, explicit_args
, targs
,
17577 (check_rettype
|| DECL_CONV_FN_P (fn
)
17578 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17579 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17580 == error_mark_node
)
17586 /* Return the innermost template arguments that, when applied to a partial
17587 specialization of MAIN_TMPL whose innermost template parameters are
17588 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17591 For example, suppose we have:
17593 template <class T, class U> struct S {};
17594 template <class T> struct S<T*, int> {};
17596 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17597 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17598 int}. The resulting vector will be {double}, indicating that `T'
17599 is bound to `double'. */
17602 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17604 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17606 tree innermost_deduced_args
;
17608 innermost_deduced_args
= make_tree_vec (ntparms
);
17609 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17611 deduced_args
= copy_node (args
);
17612 SET_TMPL_ARGS_LEVEL (deduced_args
,
17613 TMPL_ARGS_DEPTH (deduced_args
),
17614 innermost_deduced_args
);
17617 deduced_args
= innermost_deduced_args
;
17619 if (unify (tparms
, deduced_args
,
17620 INNERMOST_TEMPLATE_ARGS (spec_args
),
17621 INNERMOST_TEMPLATE_ARGS (args
),
17622 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17625 for (i
= 0; i
< ntparms
; ++i
)
17626 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17629 /* Verify that nondeduced template arguments agree with the type
17630 obtained from argument deduction.
17634 struct A { typedef int X; };
17635 template <class T, class U> struct C {};
17636 template <class T> struct C<T, typename T::X> {};
17638 Then with the instantiation `C<A, int>', we can deduce that
17639 `T' is `A' but unify () does not check whether `typename T::X'
17641 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17642 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17643 spec_args
, main_tmpl
,
17644 tf_none
, false, false);
17645 if (spec_args
== error_mark_node
17646 /* We only need to check the innermost arguments; the other
17647 arguments will always agree. */
17648 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17649 INNERMOST_TEMPLATE_ARGS (args
)))
17652 /* Now that we have bindings for all of the template arguments,
17653 ensure that the arguments deduced for the template template
17654 parameters have compatible template parameter lists. See the use
17655 of template_template_parm_bindings_ok_p in fn_type_unification
17656 for more information. */
17657 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17660 return deduced_args
;
17663 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17664 Return the TREE_LIST node with the most specialized template, if
17665 any. If there is no most specialized template, the error_mark_node
17668 Note that this function does not look at, or modify, the
17669 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17670 returned is one of the elements of INSTANTIATIONS, callers may
17671 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17672 and retrieve it from the value returned. */
17675 most_specialized_instantiation (tree templates
)
17679 ++processing_template_decl
;
17682 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17686 if (get_bindings (TREE_VALUE (champ
),
17687 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17688 NULL_TREE
, /*check_ret=*/true))
17691 if (get_bindings (TREE_VALUE (fn
),
17692 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17693 NULL_TREE
, /*check_ret=*/true))
17700 /* Equally specialized, move to next function. If there
17701 is no next function, nothing's most specialized. */
17702 fn
= TREE_CHAIN (fn
);
17710 /* Now verify that champ is better than everything earlier in the
17711 instantiation list. */
17712 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17713 if (get_bindings (TREE_VALUE (champ
),
17714 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17715 NULL_TREE
, /*check_ret=*/true)
17716 || !get_bindings (TREE_VALUE (fn
),
17717 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17718 NULL_TREE
, /*check_ret=*/true))
17724 processing_template_decl
--;
17727 return error_mark_node
;
17732 /* If DECL is a specialization of some template, return the most
17733 general such template. Otherwise, returns NULL_TREE.
17735 For example, given:
17737 template <class T> struct S { template <class U> void f(U); };
17739 if TMPL is `template <class U> void S<int>::f(U)' this will return
17740 the full template. This function will not trace past partial
17741 specializations, however. For example, given in addition:
17743 template <class T> struct S<T*> { template <class U> void f(U); };
17745 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17746 `template <class T> template <class U> S<T*>::f(U)'. */
17749 most_general_template (tree decl
)
17751 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17752 an immediate specialization. */
17753 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17755 if (DECL_TEMPLATE_INFO (decl
)) {
17756 decl
= DECL_TI_TEMPLATE (decl
);
17758 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17759 template friend. */
17760 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17766 /* Look for more and more general templates. */
17767 while (DECL_TEMPLATE_INFO (decl
))
17769 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17770 (See cp-tree.h for details.) */
17771 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17774 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17775 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17778 /* Stop if we run into an explicitly specialized class template. */
17779 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17780 && DECL_CONTEXT (decl
)
17781 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17784 decl
= DECL_TI_TEMPLATE (decl
);
17790 /* Return the most specialized of the class template partial
17791 specializations of TMPL which can produce TYPE, a specialization of
17792 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17793 a _TYPE node corresponding to the partial specialization, while the
17794 TREE_PURPOSE is the set of template arguments that must be
17795 substituted into the TREE_TYPE in order to generate TYPE.
17797 If the choice of partial specialization is ambiguous, a diagnostic
17798 is issued, and the error_mark_node is returned. If there are no
17799 partial specializations of TMPL matching TYPE, then NULL_TREE is
17803 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17805 tree list
= NULL_TREE
;
17811 tree outer_args
= NULL_TREE
;
17813 tmpl
= most_general_template (tmpl
);
17814 args
= CLASSTYPE_TI_ARGS (type
);
17816 /* For determining which partial specialization to use, only the
17817 innermost args are interesting. */
17818 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17820 outer_args
= strip_innermost_template_args (args
, 1);
17821 args
= INNERMOST_TEMPLATE_ARGS (args
);
17824 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17826 tree partial_spec_args
;
17828 tree parms
= TREE_VALUE (t
);
17830 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17832 ++processing_template_decl
;
17838 /* Discard the outer levels of args, and then substitute in the
17839 template args from the enclosing class. */
17840 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17841 partial_spec_args
= tsubst_template_args
17842 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17844 /* PARMS already refers to just the innermost parms, but the
17845 template parms in partial_spec_args had their levels lowered
17846 by tsubst, so we need to do the same for the parm list. We
17847 can't just tsubst the TREE_VEC itself, as tsubst wants to
17848 treat a TREE_VEC as an argument vector. */
17849 parms
= copy_node (parms
);
17850 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17851 TREE_VEC_ELT (parms
, i
) =
17852 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17856 partial_spec_args
=
17857 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17858 add_to_template_args (outer_args
,
17859 partial_spec_args
),
17861 /*require_all_args=*/true,
17862 /*use_default_args=*/true);
17864 --processing_template_decl
;
17866 if (partial_spec_args
== error_mark_node
)
17867 return error_mark_node
;
17869 spec_args
= get_class_bindings (tmpl
, parms
,
17875 spec_args
= add_to_template_args (outer_args
, spec_args
);
17876 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17877 TREE_TYPE (list
) = TREE_TYPE (t
);
17884 ambiguous_p
= false;
17887 t
= TREE_CHAIN (t
);
17888 for (; t
; t
= TREE_CHAIN (t
))
17890 fate
= more_specialized_class (tmpl
, champ
, t
);
17897 t
= TREE_CHAIN (t
);
17900 ambiguous_p
= true;
17909 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17911 fate
= more_specialized_class (tmpl
, champ
, t
);
17914 ambiguous_p
= true;
17922 char *spaces
= NULL
;
17923 if (!(complain
& tf_error
))
17924 return error_mark_node
;
17925 error ("ambiguous class template instantiation for %q#T", type
);
17926 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17927 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17929 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17930 spaces
= spaces
? spaces
: get_spaces (str
);
17933 return error_mark_node
;
17939 /* Explicitly instantiate DECL. */
17942 do_decl_instantiation (tree decl
, tree storage
)
17944 tree result
= NULL_TREE
;
17947 if (!decl
|| decl
== error_mark_node
)
17948 /* An error occurred, for which grokdeclarator has already issued
17949 an appropriate message. */
17951 else if (! DECL_LANG_SPECIFIC (decl
))
17953 error ("explicit instantiation of non-template %q#D", decl
);
17956 else if (TREE_CODE (decl
) == VAR_DECL
)
17958 /* There is an asymmetry here in the way VAR_DECLs and
17959 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17960 the latter, the DECL we get back will be marked as a
17961 template instantiation, and the appropriate
17962 DECL_TEMPLATE_INFO will be set up. This does not happen for
17963 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17964 should handle VAR_DECLs as it currently handles
17966 if (!DECL_CLASS_SCOPE_P (decl
))
17968 error ("%qD is not a static data member of a class template", decl
);
17971 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17972 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17974 error ("no matching template for %qD found", decl
);
17977 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17979 error ("type %qT for explicit instantiation %qD does not match "
17980 "declared type %qT", TREE_TYPE (result
), decl
,
17985 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17987 error ("explicit instantiation of %q#D", decl
);
17993 /* Check for various error cases. Note that if the explicit
17994 instantiation is valid the RESULT will currently be marked as an
17995 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17996 until we get here. */
17998 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18000 /* DR 259 [temp.spec].
18002 Both an explicit instantiation and a declaration of an explicit
18003 specialization shall not appear in a program unless the explicit
18004 instantiation follows a declaration of the explicit specialization.
18006 For a given set of template parameters, if an explicit
18007 instantiation of a template appears after a declaration of an
18008 explicit specialization for that template, the explicit
18009 instantiation has no effect. */
18012 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18016 No program shall explicitly instantiate any template more
18019 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18020 the first instantiation was `extern' and the second is not,
18021 and EXTERN_P for the opposite case. */
18022 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18023 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18024 /* If an "extern" explicit instantiation follows an ordinary
18025 explicit instantiation, the template is instantiated. */
18029 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18031 error ("no matching template for %qD found", result
);
18034 else if (!DECL_TEMPLATE_INFO (result
))
18036 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18040 if (storage
== NULL_TREE
)
18042 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18044 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18045 pedwarn (input_location
, OPT_Wpedantic
,
18046 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18051 error ("storage class %qD applied to template instantiation", storage
);
18053 check_explicit_instantiation_namespace (result
);
18054 mark_decl_instantiated (result
, extern_p
);
18056 instantiate_decl (result
, /*defer_ok=*/1,
18057 /*expl_inst_class_mem_p=*/false);
18061 mark_class_instantiated (tree t
, int extern_p
)
18063 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18064 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18065 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18066 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18069 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18070 rest_of_type_compilation (t
, 1);
18074 /* Called from do_type_instantiation through binding_table_foreach to
18075 do recursive instantiation for the type bound in ENTRY. */
18077 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18079 tree storage
= *(tree
*) data
;
18081 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18082 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18083 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18086 /* Called from do_type_instantiation to instantiate a member
18087 (a member function or a static member variable) of an
18088 explicitly instantiated class template. */
18090 instantiate_class_member (tree decl
, int extern_p
)
18092 mark_decl_instantiated (decl
, extern_p
);
18094 instantiate_decl (decl
, /*defer_ok=*/1,
18095 /*expl_inst_class_mem_p=*/true);
18098 /* Perform an explicit instantiation of template class T. STORAGE, if
18099 non-null, is the RID for extern, inline or static. COMPLAIN is
18100 nonzero if this is called from the parser, zero if called recursively,
18101 since the standard is unclear (as detailed below). */
18104 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18109 int previous_instantiation_extern_p
= 0;
18111 if (TREE_CODE (t
) == TYPE_DECL
)
18114 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18117 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18119 error ("explicit instantiation of non-class template %qD", tmpl
);
18121 error ("explicit instantiation of non-template type %qT", t
);
18127 if (!COMPLETE_TYPE_P (t
))
18129 if (complain
& tf_error
)
18130 error ("explicit instantiation of %q#T before definition of template",
18135 if (storage
!= NULL_TREE
)
18137 if (!in_system_header
)
18139 if (storage
== ridpointers
[(int) RID_EXTERN
])
18141 if (cxx_dialect
== cxx98
)
18142 pedwarn (input_location
, OPT_Wpedantic
,
18143 "ISO C++ 1998 forbids the use of %<extern%> on "
18144 "explicit instantiations");
18147 pedwarn (input_location
, OPT_Wpedantic
,
18148 "ISO C++ forbids the use of %qE"
18149 " on explicit instantiations", storage
);
18152 if (storage
== ridpointers
[(int) RID_INLINE
])
18154 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18156 else if (storage
== ridpointers
[(int) RID_STATIC
])
18160 error ("storage class %qD applied to template instantiation",
18166 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18168 /* DR 259 [temp.spec].
18170 Both an explicit instantiation and a declaration of an explicit
18171 specialization shall not appear in a program unless the explicit
18172 instantiation follows a declaration of the explicit specialization.
18174 For a given set of template parameters, if an explicit
18175 instantiation of a template appears after a declaration of an
18176 explicit specialization for that template, the explicit
18177 instantiation has no effect. */
18180 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18184 No program shall explicitly instantiate any template more
18187 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18188 instantiation was `extern'. If EXTERN_P then the second is.
18189 These cases are OK. */
18190 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18192 if (!previous_instantiation_extern_p
&& !extern_p
18193 && (complain
& tf_error
))
18194 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18196 /* If we've already instantiated the template, just return now. */
18197 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18201 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18202 mark_class_instantiated (t
, extern_p
);
18210 /* In contrast to implicit instantiation, where only the
18211 declarations, and not the definitions, of members are
18212 instantiated, we have here:
18216 The explicit instantiation of a class template specialization
18217 implies the instantiation of all of its members not
18218 previously explicitly specialized in the translation unit
18219 containing the explicit instantiation.
18221 Of course, we can't instantiate member template classes, since
18222 we don't have any arguments for them. Note that the standard
18223 is unclear on whether the instantiation of the members are
18224 *explicit* instantiations or not. However, the most natural
18225 interpretation is that it should be an explicit instantiation. */
18228 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18229 if (TREE_CODE (tmp
) == FUNCTION_DECL
18230 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18231 instantiate_class_member (tmp
, extern_p
);
18233 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18234 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18235 instantiate_class_member (tmp
, extern_p
);
18237 if (CLASSTYPE_NESTED_UTDS (t
))
18238 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18239 bt_instantiate_type_proc
, &storage
);
18243 /* Given a function DECL, which is a specialization of TMPL, modify
18244 DECL to be a re-instantiation of TMPL with the same template
18245 arguments. TMPL should be the template into which tsubst'ing
18246 should occur for DECL, not the most general template.
18248 One reason for doing this is a scenario like this:
18251 void f(const T&, int i);
18253 void g() { f(3, 7); }
18256 void f(const T& t, const int i) { }
18258 Note that when the template is first instantiated, with
18259 instantiate_template, the resulting DECL will have no name for the
18260 first parameter, and the wrong type for the second. So, when we go
18261 to instantiate the DECL, we regenerate it. */
18264 regenerate_decl_from_template (tree decl
, tree tmpl
)
18266 /* The arguments used to instantiate DECL, from the most general
18271 args
= DECL_TI_ARGS (decl
);
18272 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18274 /* Make sure that we can see identifiers, and compute access
18276 push_access_scope (decl
);
18278 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18286 args_depth
= TMPL_ARGS_DEPTH (args
);
18287 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18288 if (args_depth
> parms_depth
)
18289 args
= get_innermost_template_args (args
, parms_depth
);
18291 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18292 args
, tf_error
, NULL_TREE
,
18293 /*defer_ok*/false);
18294 if (specs
&& specs
!= error_mark_node
)
18295 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18298 /* Merge parameter declarations. */
18299 decl_parm
= skip_artificial_parms_for (decl
,
18300 DECL_ARGUMENTS (decl
));
18302 = skip_artificial_parms_for (code_pattern
,
18303 DECL_ARGUMENTS (code_pattern
));
18304 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18309 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18310 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18311 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18313 parm_type
= type_decays_to (parm_type
);
18314 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18315 TREE_TYPE (decl_parm
) = parm_type
;
18316 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18317 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18319 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18320 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18322 decl_parm
= DECL_CHAIN (decl_parm
);
18323 pattern_parm
= DECL_CHAIN (pattern_parm
);
18325 /* Merge any parameters that match with the function parameter
18327 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18330 tree expanded_types
;
18331 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18332 the parameters in this function parameter pack. */
18333 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18334 args
, tf_error
, NULL_TREE
);
18335 len
= TREE_VEC_LENGTH (expanded_types
);
18336 for (i
= 0; i
< len
; i
++)
18341 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18342 /* Rename the parameter to include the index. */
18343 DECL_NAME (decl_parm
) =
18344 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18345 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18346 parm_type
= type_decays_to (parm_type
);
18347 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18348 TREE_TYPE (decl_parm
) = parm_type
;
18349 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18350 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18352 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18353 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18355 decl_parm
= DECL_CHAIN (decl_parm
);
18358 /* Merge additional specifiers from the CODE_PATTERN. */
18359 if (DECL_DECLARED_INLINE_P (code_pattern
)
18360 && !DECL_DECLARED_INLINE_P (decl
))
18361 DECL_DECLARED_INLINE_P (decl
) = 1;
18363 else if (TREE_CODE (decl
) == VAR_DECL
)
18365 DECL_INITIAL (decl
) =
18366 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18367 tf_error
, DECL_TI_TEMPLATE (decl
),
18368 /*integral_constant_expression_p=*/false);
18369 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18370 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18371 tf_error
, DECL_TI_TEMPLATE (decl
));
18374 gcc_unreachable ();
18376 pop_access_scope (decl
);
18379 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18380 substituted to get DECL. */
18383 template_for_substitution (tree decl
)
18385 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18387 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18388 for the instantiation. This is not always the most general
18389 template. Consider, for example:
18392 struct S { template <class U> void f();
18393 template <> void f<int>(); };
18395 and an instantiation of S<double>::f<int>. We want TD to be the
18396 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18397 while (/* An instantiation cannot have a definition, so we need a
18398 more general template. */
18399 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18400 /* We must also deal with friend templates. Given:
18402 template <class T> struct S {
18403 template <class U> friend void f() {};
18406 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18407 so far as the language is concerned, but that's still
18408 where we get the pattern for the instantiation from. On
18409 other hand, if the definition comes outside the class, say:
18411 template <class T> struct S {
18412 template <class U> friend void f();
18414 template <class U> friend void f() {}
18416 we don't need to look any further. That's what the check for
18417 DECL_INITIAL is for. */
18418 || (TREE_CODE (decl
) == FUNCTION_DECL
18419 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18420 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18422 /* The present template, TD, should not be a definition. If it
18423 were a definition, we should be using it! Note that we
18424 cannot restructure the loop to just keep going until we find
18425 a template with a definition, since that might go too far if
18426 a specialization was declared, but not defined. */
18427 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18428 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18430 /* Fetch the more general template. */
18431 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18437 /* Returns true if we need to instantiate this template instance even if we
18438 know we aren't going to emit it.. */
18441 always_instantiate_p (tree decl
)
18443 /* We always instantiate inline functions so that we can inline them. An
18444 explicit instantiation declaration prohibits implicit instantiation of
18445 non-inline functions. With high levels of optimization, we would
18446 normally inline non-inline functions -- but we're not allowed to do
18447 that for "extern template" functions. Therefore, we check
18448 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18449 return ((TREE_CODE (decl
) == FUNCTION_DECL
18450 && (DECL_DECLARED_INLINE_P (decl
)
18451 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18452 /* And we need to instantiate static data members so that
18453 their initializers are available in integral constant
18455 || (TREE_CODE (decl
) == VAR_DECL
18456 && decl_maybe_constant_var_p (decl
)));
18459 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18460 instantiate it now, modifying TREE_TYPE (fn). */
18463 maybe_instantiate_noexcept (tree fn
)
18465 tree fntype
, spec
, noex
, clone
;
18467 if (DECL_CLONED_FUNCTION_P (fn
))
18468 fn
= DECL_CLONED_FUNCTION (fn
);
18469 fntype
= TREE_TYPE (fn
);
18470 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18472 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18475 noex
= TREE_PURPOSE (spec
);
18477 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18479 if (push_tinst_level (fn
))
18481 push_access_scope (fn
);
18482 push_deferring_access_checks (dk_no_deferred
);
18483 input_location
= DECL_SOURCE_LOCATION (fn
);
18484 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18485 DEFERRED_NOEXCEPT_ARGS (noex
),
18486 tf_warning_or_error
, fn
,
18487 /*function_p=*/false,
18488 /*integral_constant_expression_p=*/true);
18489 pop_deferring_access_checks ();
18490 pop_access_scope (fn
);
18491 pop_tinst_level ();
18492 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18493 if (spec
== error_mark_node
)
18494 spec
= noexcept_false_spec
;
18497 spec
= noexcept_false_spec
;
18501 /* This is an implicitly declared function, so NOEX is a list of
18502 other functions to evaluate and merge. */
18504 spec
= noexcept_true_spec
;
18505 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18507 tree fn
= OVL_CURRENT (elt
);
18509 maybe_instantiate_noexcept (fn
);
18510 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18511 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18515 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18517 FOR_EACH_CLONE (clone
, fn
)
18519 if (TREE_TYPE (clone
) == fntype
)
18520 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18522 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18526 /* Produce the definition of D, a _DECL generated from a template. If
18527 DEFER_OK is nonzero, then we don't have to actually do the
18528 instantiation now; we just have to do it sometime. Normally it is
18529 an error if this is an explicit instantiation but D is undefined.
18530 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18531 explicitly instantiated class template. */
18534 instantiate_decl (tree d
, int defer_ok
,
18535 bool expl_inst_class_mem_p
)
18537 tree tmpl
= DECL_TI_TEMPLATE (d
);
18544 bool pattern_defined
;
18545 location_t saved_loc
= input_location
;
18550 /* This function should only be used to instantiate templates for
18551 functions and static member variables. */
18552 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18553 || TREE_CODE (d
) == VAR_DECL
);
18555 /* Variables are never deferred; if instantiation is required, they
18556 are instantiated right away. That allows for better code in the
18557 case that an expression refers to the value of the variable --
18558 if the variable has a constant value the referring expression can
18559 take advantage of that fact. */
18560 if (TREE_CODE (d
) == VAR_DECL
18561 || DECL_DECLARED_CONSTEXPR_P (d
))
18564 /* Don't instantiate cloned functions. Instead, instantiate the
18565 functions they cloned. */
18566 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18567 d
= DECL_CLONED_FUNCTION (d
);
18569 if (DECL_TEMPLATE_INSTANTIATED (d
)
18570 || (TREE_CODE (d
) == FUNCTION_DECL
18571 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18572 || DECL_TEMPLATE_SPECIALIZATION (d
))
18573 /* D has already been instantiated or explicitly specialized, so
18574 there's nothing for us to do here.
18576 It might seem reasonable to check whether or not D is an explicit
18577 instantiation, and, if so, stop here. But when an explicit
18578 instantiation is deferred until the end of the compilation,
18579 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18580 the instantiation. */
18583 /* Check to see whether we know that this template will be
18584 instantiated in some other file, as with "extern template"
18586 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18588 /* In general, we do not instantiate such templates. */
18589 if (external_p
&& !always_instantiate_p (d
))
18592 gen_tmpl
= most_general_template (tmpl
);
18593 gen_args
= DECL_TI_ARGS (d
);
18595 if (tmpl
!= gen_tmpl
)
18596 /* We should already have the extra args. */
18597 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18598 == TMPL_ARGS_DEPTH (gen_args
));
18599 /* And what's in the hash table should match D. */
18600 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18601 || spec
== NULL_TREE
);
18603 /* This needs to happen before any tsubsting. */
18604 if (! push_tinst_level (d
))
18607 timevar_push (TV_TEMPLATE_INST
);
18609 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18610 for the instantiation. */
18611 td
= template_for_substitution (d
);
18612 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18614 /* We should never be trying to instantiate a member of a class
18615 template or partial specialization. */
18616 gcc_assert (d
!= code_pattern
);
18618 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18619 || DECL_TEMPLATE_SPECIALIZATION (td
))
18620 /* In the case of a friend template whose definition is provided
18621 outside the class, we may have too many arguments. Drop the
18622 ones we don't need. The same is true for specializations. */
18623 args
= get_innermost_template_args
18624 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18628 if (TREE_CODE (d
) == FUNCTION_DECL
)
18629 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18630 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18632 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18634 /* We may be in the middle of deferred access check. Disable it now. */
18635 push_deferring_access_checks (dk_no_deferred
);
18637 /* Unless an explicit instantiation directive has already determined
18638 the linkage of D, remember that a definition is available for
18640 if (pattern_defined
18641 && !DECL_INTERFACE_KNOWN (d
)
18642 && !DECL_NOT_REALLY_EXTERN (d
))
18643 mark_definable (d
);
18645 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18646 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18647 input_location
= DECL_SOURCE_LOCATION (d
);
18649 /* If D is a member of an explicitly instantiated class template,
18650 and no definition is available, treat it like an implicit
18652 if (!pattern_defined
&& expl_inst_class_mem_p
18653 && DECL_EXPLICIT_INSTANTIATION (d
))
18655 /* Leave linkage flags alone on instantiations with anonymous
18657 if (TREE_PUBLIC (d
))
18659 DECL_NOT_REALLY_EXTERN (d
) = 0;
18660 DECL_INTERFACE_KNOWN (d
) = 0;
18662 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18665 if (TREE_CODE (d
) == FUNCTION_DECL
)
18666 maybe_instantiate_noexcept (d
);
18668 /* Defer all other templates, unless we have been explicitly
18669 forbidden from doing so. */
18670 if (/* If there is no definition, we cannot instantiate the
18673 /* If it's OK to postpone instantiation, do so. */
18675 /* If this is a static data member that will be defined
18676 elsewhere, we don't want to instantiate the entire data
18677 member, but we do want to instantiate the initializer so that
18678 we can substitute that elsewhere. */
18679 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18681 /* The definition of the static data member is now required so
18682 we must substitute the initializer. */
18683 if (TREE_CODE (d
) == VAR_DECL
18684 && !DECL_INITIAL (d
)
18685 && DECL_INITIAL (code_pattern
))
18689 bool const_init
= false;
18691 ns
= decl_namespace_context (d
);
18692 push_nested_namespace (ns
);
18693 push_nested_class (DECL_CONTEXT (d
));
18694 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18696 tf_warning_or_error
, NULL_TREE
,
18697 /*integral_constant_expression_p=*/false);
18698 /* Make sure the initializer is still constant, in case of
18699 circular dependency (template/instantiate6.C). */
18701 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18702 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18703 /*asmspec_tree=*/NULL_TREE
,
18704 LOOKUP_ONLYCONVERTING
);
18705 pop_nested_class ();
18706 pop_nested_namespace (ns
);
18709 /* We restore the source position here because it's used by
18710 add_pending_template. */
18711 input_location
= saved_loc
;
18713 if (at_eof
&& !pattern_defined
18714 && DECL_EXPLICIT_INSTANTIATION (d
)
18715 && DECL_NOT_REALLY_EXTERN (d
))
18718 The definition of a non-exported function template, a
18719 non-exported member function template, or a non-exported
18720 member function or static data member of a class template
18721 shall be present in every translation unit in which it is
18722 explicitly instantiated. */
18723 permerror (input_location
, "explicit instantiation of %qD "
18724 "but no definition available", d
);
18726 /* If we're in unevaluated context, we just wanted to get the
18727 constant value; this isn't an odr use, so don't queue
18728 a full instantiation. */
18729 if (cp_unevaluated_operand
!= 0)
18731 /* ??? Historically, we have instantiated inline functions, even
18732 when marked as "extern template". */
18733 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18734 add_pending_template (d
);
18737 /* Tell the repository that D is available in this translation unit
18738 -- and see if it is supposed to be instantiated here. */
18739 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18741 /* In a PCH file, despite the fact that the repository hasn't
18742 requested instantiation in the PCH it is still possible that
18743 an instantiation will be required in a file that includes the
18746 add_pending_template (d
);
18747 /* Instantiate inline functions so that the inliner can do its
18748 job, even though we'll not be emitting a copy of this
18750 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18754 fn_context
= decl_function_context (d
);
18755 nested
= (current_function_decl
!= NULL_TREE
);
18757 push_to_top_level ();
18759 push_function_context ();
18761 /* Mark D as instantiated so that recursive calls to
18762 instantiate_decl do not try to instantiate it again. */
18763 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18765 /* Regenerate the declaration in case the template has been modified
18766 by a subsequent redeclaration. */
18767 regenerate_decl_from_template (d
, td
);
18769 /* We already set the file and line above. Reset them now in case
18770 they changed as a result of calling regenerate_decl_from_template. */
18771 input_location
= DECL_SOURCE_LOCATION (d
);
18773 if (TREE_CODE (d
) == VAR_DECL
)
18776 bool const_init
= false;
18778 /* Clear out DECL_RTL; whatever was there before may not be right
18779 since we've reset the type of the declaration. */
18780 SET_DECL_RTL (d
, NULL
);
18781 DECL_IN_AGGR_P (d
) = 0;
18783 /* The initializer is placed in DECL_INITIAL by
18784 regenerate_decl_from_template so we don't need to
18785 push/pop_access_scope again here. Pull it out so that
18786 cp_finish_decl can process it. */
18787 init
= DECL_INITIAL (d
);
18788 DECL_INITIAL (d
) = NULL_TREE
;
18789 DECL_INITIALIZED_P (d
) = 0;
18791 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18792 initializer. That function will defer actual emission until
18793 we have a chance to determine linkage. */
18794 DECL_EXTERNAL (d
) = 0;
18796 /* Enter the scope of D so that access-checking works correctly. */
18797 push_nested_class (DECL_CONTEXT (d
));
18798 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18799 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18800 pop_nested_class ();
18802 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18803 synthesize_method (d
);
18804 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18806 struct pointer_map_t
*saved_local_specializations
;
18811 /* Save away the current list, in case we are instantiating one
18812 template from within the body of another. */
18813 saved_local_specializations
= local_specializations
;
18815 /* Set up the list of local specializations. */
18816 local_specializations
= pointer_map_create ();
18818 /* Set up context. */
18819 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18821 /* Some typedefs referenced from within the template code need to be
18822 access checked at template instantiation time, i.e now. These
18823 types were added to the template at parsing time. Let's get those
18824 and perform the access checks then. */
18825 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18828 /* Create substitution entries for the parameters. */
18829 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18830 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18831 spec_parm
= DECL_ARGUMENTS (d
);
18832 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18834 register_local_specialization (spec_parm
, tmpl_parm
);
18835 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18836 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18838 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18840 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18842 register_local_specialization (spec_parm
, tmpl_parm
);
18843 spec_parm
= DECL_CHAIN (spec_parm
);
18847 /* Register the (value) argument pack as a specialization of
18848 TMPL_PARM, then move on. */
18849 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18850 register_local_specialization (argpack
, tmpl_parm
);
18853 gcc_assert (!spec_parm
);
18855 /* Substitute into the body of the function. */
18856 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18857 tf_warning_or_error
, tmpl
,
18858 /*integral_constant_expression_p=*/false);
18860 /* Set the current input_location to the end of the function
18861 so that finish_function knows where we are. */
18862 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18864 /* We don't need the local specializations any more. */
18865 pointer_map_destroy (local_specializations
);
18866 local_specializations
= saved_local_specializations
;
18868 /* Finish the function. */
18869 d
= finish_function (0);
18870 expand_or_defer_fn (d
);
18873 /* We're not deferring instantiation any more. */
18874 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18877 pop_from_top_level ();
18879 pop_function_context ();
18882 input_location
= saved_loc
;
18883 pop_deferring_access_checks ();
18884 pop_tinst_level ();
18886 timevar_pop (TV_TEMPLATE_INST
);
18891 /* Run through the list of templates that we wish we could
18892 instantiate, and instantiate any we can. RETRIES is the
18893 number of times we retry pending template instantiation. */
18896 instantiate_pending_templates (int retries
)
18899 location_t saved_loc
= input_location
;
18901 /* Instantiating templates may trigger vtable generation. This in turn
18902 may require further template instantiations. We place a limit here
18903 to avoid infinite loop. */
18904 if (pending_templates
&& retries
>= max_tinst_depth
)
18906 tree decl
= pending_templates
->tinst
->decl
;
18908 error ("template instantiation depth exceeds maximum of %d"
18909 " instantiating %q+D, possibly from virtual table generation"
18910 " (use -ftemplate-depth= to increase the maximum)",
18911 max_tinst_depth
, decl
);
18912 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18913 /* Pretend that we defined it. */
18914 DECL_INITIAL (decl
) = error_mark_node
;
18920 struct pending_template
**t
= &pending_templates
;
18921 struct pending_template
*last
= NULL
;
18925 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18926 bool complete
= false;
18928 if (TYPE_P (instantiation
))
18932 if (!COMPLETE_TYPE_P (instantiation
))
18934 instantiate_class_template (instantiation
);
18935 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18936 for (fn
= TYPE_METHODS (instantiation
);
18938 fn
= TREE_CHAIN (fn
))
18939 if (! DECL_ARTIFICIAL (fn
))
18940 instantiate_decl (fn
,
18942 /*expl_inst_class_mem_p=*/false);
18943 if (COMPLETE_TYPE_P (instantiation
))
18947 complete
= COMPLETE_TYPE_P (instantiation
);
18951 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18952 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18955 = instantiate_decl (instantiation
,
18957 /*expl_inst_class_mem_p=*/false);
18958 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18962 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18963 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18967 /* If INSTANTIATION has been instantiated, then we don't
18968 need to consider it again in the future. */
18976 current_tinst_level
= NULL
;
18978 last_pending_template
= last
;
18980 while (reconsider
);
18982 input_location
= saved_loc
;
18985 /* Substitute ARGVEC into T, which is a list of initializers for
18986 either base class or a non-static data member. The TREE_PURPOSEs
18987 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18988 instantiate_decl. */
18991 tsubst_initializer_list (tree t
, tree argvec
)
18993 tree inits
= NULL_TREE
;
18995 for (; t
; t
= TREE_CHAIN (t
))
18999 tree expanded_bases
= NULL_TREE
;
19000 tree expanded_arguments
= NULL_TREE
;
19003 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19008 /* Expand the base class expansion type into separate base
19010 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19011 tf_warning_or_error
,
19013 if (expanded_bases
== error_mark_node
)
19016 /* We'll be building separate TREE_LISTs of arguments for
19018 len
= TREE_VEC_LENGTH (expanded_bases
);
19019 expanded_arguments
= make_tree_vec (len
);
19020 for (i
= 0; i
< len
; i
++)
19021 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19023 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19024 expand each argument in the TREE_VALUE of t. */
19025 expr
= make_node (EXPR_PACK_EXPANSION
);
19026 PACK_EXPANSION_LOCAL_P (expr
) = true;
19027 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19028 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19030 if (TREE_VALUE (t
) == void_type_node
)
19031 /* VOID_TYPE_NODE is used to indicate
19032 value-initialization. */
19034 for (i
= 0; i
< len
; i
++)
19035 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19039 /* Substitute parameter packs into each argument in the
19041 in_base_initializer
= 1;
19042 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19044 tree expanded_exprs
;
19046 /* Expand the argument. */
19047 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19049 = tsubst_pack_expansion (expr
, argvec
,
19050 tf_warning_or_error
,
19052 if (expanded_exprs
== error_mark_node
)
19055 /* Prepend each of the expanded expressions to the
19056 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19057 for (i
= 0; i
< len
; i
++)
19059 TREE_VEC_ELT (expanded_arguments
, i
) =
19060 tree_cons (NULL_TREE
,
19061 TREE_VEC_ELT (expanded_exprs
, i
),
19062 TREE_VEC_ELT (expanded_arguments
, i
));
19065 in_base_initializer
= 0;
19067 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19068 since we built them backwards. */
19069 for (i
= 0; i
< len
; i
++)
19071 TREE_VEC_ELT (expanded_arguments
, i
) =
19072 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19077 for (i
= 0; i
< len
; ++i
)
19079 if (expanded_bases
)
19081 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19082 decl
= expand_member_init (decl
);
19083 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19088 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19089 tf_warning_or_error
, NULL_TREE
);
19091 decl
= expand_member_init (decl
);
19092 if (decl
&& !DECL_P (decl
))
19093 in_base_initializer
= 1;
19095 init
= TREE_VALUE (t
);
19097 if (init
!= void_type_node
)
19098 init
= tsubst_expr (init
, argvec
,
19099 tf_warning_or_error
, NULL_TREE
,
19100 /*integral_constant_expression_p=*/false);
19101 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19102 /* If we had an initializer but it instantiated to nothing,
19103 value-initialize the object. This will only occur when
19104 the initializer was a pack expansion where the parameter
19105 packs used in that expansion were of length zero. */
19106 init
= void_type_node
;
19107 in_base_initializer
= 0;
19112 init
= build_tree_list (decl
, init
);
19113 TREE_CHAIN (init
) = inits
;
19121 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19124 set_current_access_from_decl (tree decl
)
19126 if (TREE_PRIVATE (decl
))
19127 current_access_specifier
= access_private_node
;
19128 else if (TREE_PROTECTED (decl
))
19129 current_access_specifier
= access_protected_node
;
19131 current_access_specifier
= access_public_node
;
19134 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19135 is the instantiation (which should have been created with
19136 start_enum) and ARGS are the template arguments to use. */
19139 tsubst_enum (tree tag
, tree newtag
, tree args
)
19143 if (SCOPED_ENUM_P (newtag
))
19144 begin_scope (sk_scoped_enum
, newtag
);
19146 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19151 decl
= TREE_VALUE (e
);
19152 /* Note that in a template enum, the TREE_VALUE is the
19153 CONST_DECL, not the corresponding INTEGER_CST. */
19154 value
= tsubst_expr (DECL_INITIAL (decl
),
19155 args
, tf_warning_or_error
, NULL_TREE
,
19156 /*integral_constant_expression_p=*/true);
19158 /* Give this enumeration constant the correct access. */
19159 set_current_access_from_decl (decl
);
19161 /* Actually build the enumerator itself. */
19163 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19166 if (SCOPED_ENUM_P (newtag
))
19169 finish_enum_value_list (newtag
);
19170 finish_enum (newtag
);
19172 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19173 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19176 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19177 its type -- but without substituting the innermost set of template
19178 arguments. So, innermost set of template parameters will appear in
19182 get_mostly_instantiated_function_type (tree decl
)
19190 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19191 targs
= DECL_TI_ARGS (decl
);
19192 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19193 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19195 /* There should be as many levels of arguments as there are levels
19197 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19199 fn_type
= TREE_TYPE (tmpl
);
19201 if (parm_depth
== 1)
19202 /* No substitution is necessary. */
19209 /* Replace the innermost level of the TARGS with NULL_TREEs to
19210 let tsubst know not to substitute for those parameters. */
19211 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19212 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19213 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19214 TMPL_ARGS_LEVEL (targs
, i
));
19215 SET_TMPL_ARGS_LEVEL (partial_args
,
19216 TMPL_ARGS_DEPTH (targs
),
19217 make_tree_vec (DECL_NTPARMS (tmpl
)));
19219 /* Make sure that we can see identifiers, and compute access
19221 push_access_scope (decl
);
19223 ++processing_template_decl
;
19224 /* Now, do the (partial) substitution to figure out the
19225 appropriate function type. */
19226 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19227 --processing_template_decl
;
19229 /* Substitute into the template parameters to obtain the real
19230 innermost set of parameters. This step is important if the
19231 innermost set of template parameters contains value
19232 parameters whose types depend on outer template parameters. */
19233 TREE_VEC_LENGTH (partial_args
)--;
19234 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19236 pop_access_scope (decl
);
19242 /* Return truthvalue if we're processing a template different from
19243 the last one involved in diagnostics. */
19245 problematic_instantiation_changed (void)
19247 return current_tinst_level
!= last_error_tinst_level
;
19250 /* Remember current template involved in diagnostics. */
19252 record_last_problematic_instantiation (void)
19254 last_error_tinst_level
= current_tinst_level
;
19257 struct tinst_level
*
19258 current_instantiation (void)
19260 return current_tinst_level
;
19263 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19264 type. Return zero for ok, nonzero for disallowed. Issue error and
19265 warning messages under control of COMPLAIN. */
19268 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19270 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19272 else if (POINTER_TYPE_P (type
))
19274 else if (TYPE_PTRMEM_P (type
))
19276 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19278 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19280 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19282 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19285 if (complain
& tf_error
)
19287 if (type
== error_mark_node
)
19288 inform (input_location
, "invalid template non-type parameter");
19290 error ("%q#T is not a valid type for a template non-type parameter",
19296 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19297 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19300 dependent_type_p_r (tree type
)
19306 A type is dependent if it is:
19308 -- a template parameter. Template template parameters are types
19309 for us (since TYPE_P holds true for them) so we handle
19311 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19312 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19314 /* -- a qualified-id with a nested-name-specifier which contains a
19315 class-name that names a dependent type or whose unqualified-id
19316 names a dependent type. */
19317 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19319 /* -- a cv-qualified type where the cv-unqualified type is
19321 type
= TYPE_MAIN_VARIANT (type
);
19322 /* -- a compound type constructed from any dependent type. */
19323 if (TYPE_PTRMEM_P (type
))
19324 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19325 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19327 else if (TREE_CODE (type
) == POINTER_TYPE
19328 || TREE_CODE (type
) == REFERENCE_TYPE
)
19329 return dependent_type_p (TREE_TYPE (type
));
19330 else if (TREE_CODE (type
) == FUNCTION_TYPE
19331 || TREE_CODE (type
) == METHOD_TYPE
)
19335 if (dependent_type_p (TREE_TYPE (type
)))
19337 for (arg_type
= TYPE_ARG_TYPES (type
);
19339 arg_type
= TREE_CHAIN (arg_type
))
19340 if (dependent_type_p (TREE_VALUE (arg_type
)))
19344 /* -- an array type constructed from any dependent type or whose
19345 size is specified by a constant expression that is
19348 We checked for type- and value-dependence of the bounds in
19349 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19350 if (TREE_CODE (type
) == ARRAY_TYPE
)
19352 if (TYPE_DOMAIN (type
)
19353 && dependent_type_p (TYPE_DOMAIN (type
)))
19355 return dependent_type_p (TREE_TYPE (type
));
19358 /* -- a template-id in which either the template name is a template
19360 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19362 /* ... or any of the template arguments is a dependent type or
19363 an expression that is type-dependent or value-dependent. */
19364 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19365 && (any_dependent_template_arguments_p
19366 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19369 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19370 dependent; if the argument of the `typeof' expression is not
19371 type-dependent, then it should already been have resolved. */
19372 if (TREE_CODE (type
) == TYPEOF_TYPE
19373 || TREE_CODE (type
) == DECLTYPE_TYPE
19374 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19377 /* A template argument pack is dependent if any of its packed
19379 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19381 tree args
= ARGUMENT_PACK_ARGS (type
);
19382 int i
, len
= TREE_VEC_LENGTH (args
);
19383 for (i
= 0; i
< len
; ++i
)
19384 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19388 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19389 be template parameters. */
19390 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19393 /* The standard does not specifically mention types that are local
19394 to template functions or local classes, but they should be
19395 considered dependent too. For example:
19397 template <int I> void f() {
19402 The size of `E' cannot be known until the value of `I' has been
19403 determined. Therefore, `E' must be considered dependent. */
19404 scope
= TYPE_CONTEXT (type
);
19405 if (scope
&& TYPE_P (scope
))
19406 return dependent_type_p (scope
);
19407 /* Don't use type_dependent_expression_p here, as it can lead
19408 to infinite recursion trying to determine whether a lambda
19409 nested in a lambda is dependent (c++/47687). */
19410 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19411 && DECL_LANG_SPECIFIC (scope
)
19412 && DECL_TEMPLATE_INFO (scope
)
19413 && (any_dependent_template_arguments_p
19414 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19417 /* Other types are non-dependent. */
19421 /* Returns TRUE if TYPE is dependent, in the sense of
19422 [temp.dep.type]. Note that a NULL type is considered dependent. */
19425 dependent_type_p (tree type
)
19427 /* If there are no template parameters in scope, then there can't be
19428 any dependent types. */
19429 if (!processing_template_decl
)
19431 /* If we are not processing a template, then nobody should be
19432 providing us with a dependent type. */
19434 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19438 /* If the type is NULL, we have not computed a type for the entity
19439 in question; in that case, the type is dependent. */
19443 /* Erroneous types can be considered non-dependent. */
19444 if (type
== error_mark_node
)
19447 /* If we have not already computed the appropriate value for TYPE,
19449 if (!TYPE_DEPENDENT_P_VALID (type
))
19451 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19452 TYPE_DEPENDENT_P_VALID (type
) = 1;
19455 return TYPE_DEPENDENT_P (type
);
19458 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19459 lookup. In other words, a dependent type that is not the current
19463 dependent_scope_p (tree scope
)
19465 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19466 && !currently_open_class (scope
));
19469 /* T is a SCOPE_REF; return whether we need to consider it
19470 instantiation-dependent so that we can check access at instantiation
19471 time even though we know which member it resolves to. */
19474 instantiation_dependent_scope_ref_p (tree t
)
19476 if (DECL_P (TREE_OPERAND (t
, 1))
19477 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
19478 && accessible_in_template_p (TREE_OPERAND (t
, 0),
19479 TREE_OPERAND (t
, 1)))
19485 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19486 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19489 /* Note that this predicate is not appropriate for general expressions;
19490 only constant expressions (that satisfy potential_constant_expression)
19491 can be tested for value dependence. */
19494 value_dependent_expression_p (tree expression
)
19496 if (!processing_template_decl
)
19499 /* A name declared with a dependent type. */
19500 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19503 switch (TREE_CODE (expression
))
19505 case IDENTIFIER_NODE
:
19506 /* A name that has not been looked up -- must be dependent. */
19509 case TEMPLATE_PARM_INDEX
:
19510 /* A non-type template parm. */
19514 /* A non-type template parm. */
19515 if (DECL_TEMPLATE_PARM_P (expression
))
19517 return value_dependent_expression_p (DECL_INITIAL (expression
));
19520 /* A constant with literal type and is initialized
19521 with an expression that is value-dependent.
19523 Note that a non-dependent parenthesized initializer will have
19524 already been replaced with its constant value, so if we see
19525 a TREE_LIST it must be dependent. */
19526 if (DECL_INITIAL (expression
)
19527 && decl_constant_var_p (expression
)
19528 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19529 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19533 case DYNAMIC_CAST_EXPR
:
19534 case STATIC_CAST_EXPR
:
19535 case CONST_CAST_EXPR
:
19536 case REINTERPRET_CAST_EXPR
:
19538 /* These expressions are value-dependent if the type to which
19539 the cast occurs is dependent or the expression being casted
19540 is value-dependent. */
19542 tree type
= TREE_TYPE (expression
);
19544 if (dependent_type_p (type
))
19547 /* A functional cast has a list of operands. */
19548 expression
= TREE_OPERAND (expression
, 0);
19551 /* If there are no operands, it must be an expression such
19552 as "int()". This should not happen for aggregate types
19553 because it would form non-constant expressions. */
19554 gcc_assert (cxx_dialect
>= cxx0x
19555 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19560 if (TREE_CODE (expression
) == TREE_LIST
)
19561 return any_value_dependent_elements_p (expression
);
19563 return value_dependent_expression_p (expression
);
19567 if (SIZEOF_EXPR_TYPE_P (expression
))
19568 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
19572 /* A `sizeof' expression is value-dependent if the operand is
19573 type-dependent or is a pack expansion. */
19574 expression
= TREE_OPERAND (expression
, 0);
19575 if (PACK_EXPANSION_P (expression
))
19577 else if (TYPE_P (expression
))
19578 return dependent_type_p (expression
);
19579 return instantiation_dependent_expression_p (expression
);
19581 case AT_ENCODE_EXPR
:
19582 /* An 'encode' expression is value-dependent if the operand is
19584 expression
= TREE_OPERAND (expression
, 0);
19585 return dependent_type_p (expression
);
19587 case NOEXCEPT_EXPR
:
19588 expression
= TREE_OPERAND (expression
, 0);
19589 return instantiation_dependent_expression_p (expression
);
19592 /* All instantiation-dependent expressions should also be considered
19593 value-dependent. */
19594 return instantiation_dependent_scope_ref_p (expression
);
19596 case COMPONENT_REF
:
19597 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19598 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19600 case NONTYPE_ARGUMENT_PACK
:
19601 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19602 is value-dependent. */
19604 tree values
= ARGUMENT_PACK_ARGS (expression
);
19605 int i
, len
= TREE_VEC_LENGTH (values
);
19607 for (i
= 0; i
< len
; ++i
)
19608 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19616 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19617 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19618 || (type2
? dependent_type_p (type2
) : false));
19622 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19623 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19626 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19627 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19631 tree op
= TREE_OPERAND (expression
, 0);
19632 return (value_dependent_expression_p (op
)
19633 || has_value_dependent_address (op
));
19638 tree fn
= get_callee_fndecl (expression
);
19640 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19642 nargs
= call_expr_nargs (expression
);
19643 for (i
= 0; i
< nargs
; ++i
)
19645 tree op
= CALL_EXPR_ARG (expression
, i
);
19646 /* In a call to a constexpr member function, look through the
19647 implicit ADDR_EXPR on the object argument so that it doesn't
19648 cause the call to be considered value-dependent. We also
19649 look through it in potential_constant_expression. */
19650 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19651 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19652 && TREE_CODE (op
) == ADDR_EXPR
)
19653 op
= TREE_OPERAND (op
, 0);
19654 if (value_dependent_expression_p (op
))
19660 case TEMPLATE_ID_EXPR
:
19661 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19663 return type_dependent_expression_p (expression
);
19669 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19670 if (value_dependent_expression_p (val
))
19676 /* Treat a GNU statement expression as dependent to avoid crashing
19677 under fold_non_dependent_expr; it can't be constant. */
19681 /* A constant expression is value-dependent if any subexpression is
19682 value-dependent. */
19683 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19685 case tcc_reference
:
19687 case tcc_comparison
:
19689 case tcc_expression
:
19692 int i
, len
= cp_tree_operand_length (expression
);
19694 for (i
= 0; i
< len
; i
++)
19696 tree t
= TREE_OPERAND (expression
, i
);
19698 /* In some cases, some of the operands may be missing.l
19699 (For example, in the case of PREDECREMENT_EXPR, the
19700 amount to increment by may be missing.) That doesn't
19701 make the expression dependent. */
19702 if (t
&& value_dependent_expression_p (t
))
19713 /* The expression is not value-dependent. */
19717 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19718 [temp.dep.expr]. Note that an expression with no type is
19719 considered dependent. Other parts of the compiler arrange for an
19720 expression with type-dependent subexpressions to have no type, so
19721 this function doesn't have to be fully recursive. */
19724 type_dependent_expression_p (tree expression
)
19726 if (!processing_template_decl
)
19729 if (expression
== error_mark_node
)
19732 /* An unresolved name is always dependent. */
19733 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19734 || TREE_CODE (expression
) == USING_DECL
)
19737 /* Some expression forms are never type-dependent. */
19738 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19739 || TREE_CODE (expression
) == SIZEOF_EXPR
19740 || TREE_CODE (expression
) == ALIGNOF_EXPR
19741 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19742 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19743 || TREE_CODE (expression
) == TRAIT_EXPR
19744 || TREE_CODE (expression
) == TYPEID_EXPR
19745 || TREE_CODE (expression
) == DELETE_EXPR
19746 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19747 || TREE_CODE (expression
) == THROW_EXPR
)
19750 /* The types of these expressions depends only on the type to which
19751 the cast occurs. */
19752 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19753 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19754 || TREE_CODE (expression
) == CONST_CAST_EXPR
19755 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19756 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19757 || TREE_CODE (expression
) == CAST_EXPR
)
19758 return dependent_type_p (TREE_TYPE (expression
));
19760 /* The types of these expressions depends only on the type created
19761 by the expression. */
19762 if (TREE_CODE (expression
) == NEW_EXPR
19763 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19765 /* For NEW_EXPR tree nodes created inside a template, either
19766 the object type itself or a TREE_LIST may appear as the
19768 tree type
= TREE_OPERAND (expression
, 1);
19769 if (TREE_CODE (type
) == TREE_LIST
)
19770 /* This is an array type. We need to check array dimensions
19772 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19773 || value_dependent_expression_p
19774 (TREE_OPERAND (TREE_VALUE (type
), 1));
19776 return dependent_type_p (type
);
19779 if (TREE_CODE (expression
) == SCOPE_REF
)
19781 tree scope
= TREE_OPERAND (expression
, 0);
19782 tree name
= TREE_OPERAND (expression
, 1);
19784 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19785 contains an identifier associated by name lookup with one or more
19786 declarations declared with a dependent type, or...a
19787 nested-name-specifier or qualified-id that names a member of an
19788 unknown specialization. */
19789 return (type_dependent_expression_p (name
)
19790 || dependent_scope_p (scope
));
19793 if (TREE_CODE (expression
) == FUNCTION_DECL
19794 && DECL_LANG_SPECIFIC (expression
)
19795 && DECL_TEMPLATE_INFO (expression
)
19796 && (any_dependent_template_arguments_p
19797 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19800 if (TREE_CODE (expression
) == TEMPLATE_DECL
19801 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19804 if (TREE_CODE (expression
) == STMT_EXPR
)
19805 expression
= stmt_expr_value_expr (expression
);
19807 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19812 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19814 if (type_dependent_expression_p (elt
))
19820 /* A static data member of the current instantiation with incomplete
19821 array type is type-dependent, as the definition and specializations
19822 can have different bounds. */
19823 if (TREE_CODE (expression
) == VAR_DECL
19824 && DECL_CLASS_SCOPE_P (expression
)
19825 && dependent_type_p (DECL_CONTEXT (expression
))
19826 && VAR_HAD_UNKNOWN_BOUND (expression
))
19829 if (TREE_TYPE (expression
) == unknown_type_node
)
19831 if (TREE_CODE (expression
) == ADDR_EXPR
)
19832 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19833 if (TREE_CODE (expression
) == COMPONENT_REF
19834 || TREE_CODE (expression
) == OFFSET_REF
)
19836 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19838 expression
= TREE_OPERAND (expression
, 1);
19839 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19842 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19843 if (TREE_CODE (expression
) == SCOPE_REF
)
19846 if (BASELINK_P (expression
))
19847 expression
= BASELINK_FUNCTIONS (expression
);
19849 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19851 if (any_dependent_template_arguments_p
19852 (TREE_OPERAND (expression
, 1)))
19854 expression
= TREE_OPERAND (expression
, 0);
19856 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19857 || TREE_CODE (expression
) == FUNCTION_DECL
);
19861 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19863 expression
= OVL_NEXT (expression
);
19868 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19870 return (dependent_type_p (TREE_TYPE (expression
)));
19873 /* walk_tree callback function for instantiation_dependent_expression_p,
19874 below. Returns non-zero if a dependent subexpression is found. */
19877 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19882 /* We don't have to worry about decltype currently because decltype
19883 of an instantiation-dependent expr is a dependent type. This
19884 might change depending on the resolution of DR 1172. */
19885 *walk_subtrees
= false;
19888 enum tree_code code
= TREE_CODE (*tp
);
19891 /* Don't treat an argument list as dependent just because it has no
19897 case TEMPLATE_PARM_INDEX
:
19900 /* Handle expressions with type operands. */
19904 case AT_ENCODE_EXPR
:
19906 tree op
= TREE_OPERAND (*tp
, 0);
19907 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
19908 op
= TREE_TYPE (op
);
19911 if (dependent_type_p (op
))
19915 *walk_subtrees
= false;
19923 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
19924 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
)))
19926 *walk_subtrees
= false;
19929 case COMPONENT_REF
:
19930 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19931 /* In a template, finish_class_member_access_expr creates a
19932 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19933 type-dependent, so that we can check access control at
19934 instantiation time (PR 42277). See also Core issue 1273. */
19939 if (instantiation_dependent_scope_ref_p (*tp
))
19944 /* Treat statement-expressions as dependent. */
19952 if (type_dependent_expression_p (*tp
))
19958 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19959 sense defined by the ABI:
19961 "An expression is instantiation-dependent if it is type-dependent
19962 or value-dependent, or it has a subexpression that is type-dependent
19963 or value-dependent." */
19966 instantiation_dependent_expression_p (tree expression
)
19970 if (!processing_template_decl
)
19973 if (expression
== error_mark_node
)
19976 result
= cp_walk_tree_without_duplicates (&expression
,
19977 instantiation_dependent_r
, NULL
);
19978 return result
!= NULL_TREE
;
19981 /* Like type_dependent_expression_p, but it also works while not processing
19982 a template definition, i.e. during substitution or mangling. */
19985 type_dependent_expression_p_push (tree expr
)
19988 ++processing_template_decl
;
19989 b
= type_dependent_expression_p (expr
);
19990 --processing_template_decl
;
19994 /* Returns TRUE if ARGS contains a type-dependent expression. */
19997 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20002 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20004 if (type_dependent_expression_p (arg
))
20010 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20011 expressions) contains any type-dependent expressions. */
20014 any_type_dependent_elements_p (const_tree list
)
20016 for (; list
; list
= TREE_CHAIN (list
))
20017 if (value_dependent_expression_p (TREE_VALUE (list
)))
20023 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20024 expressions) contains any value-dependent expressions. */
20027 any_value_dependent_elements_p (const_tree list
)
20029 for (; list
; list
= TREE_CHAIN (list
))
20030 if (value_dependent_expression_p (TREE_VALUE (list
)))
20036 /* Returns TRUE if the ARG (a template argument) is dependent. */
20039 dependent_template_arg_p (tree arg
)
20041 if (!processing_template_decl
)
20044 /* Assume a template argument that was wrongly written by the user
20045 is dependent. This is consistent with what
20046 any_dependent_template_arguments_p [that calls this function]
20048 if (!arg
|| arg
== error_mark_node
)
20051 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20052 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20054 if (TREE_CODE (arg
) == TEMPLATE_DECL
20055 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20056 return dependent_template_p (arg
);
20057 else if (ARGUMENT_PACK_P (arg
))
20059 tree args
= ARGUMENT_PACK_ARGS (arg
);
20060 int i
, len
= TREE_VEC_LENGTH (args
);
20061 for (i
= 0; i
< len
; ++i
)
20063 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20069 else if (TYPE_P (arg
))
20070 return dependent_type_p (arg
);
20072 return (type_dependent_expression_p (arg
)
20073 || value_dependent_expression_p (arg
));
20076 /* Returns true if ARGS (a collection of template arguments) contains
20077 any types that require structural equality testing. */
20080 any_template_arguments_need_structural_equality_p (tree args
)
20087 if (args
== error_mark_node
)
20090 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20092 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20093 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20095 tree arg
= TREE_VEC_ELT (level
, j
);
20096 tree packed_args
= NULL_TREE
;
20099 if (ARGUMENT_PACK_P (arg
))
20101 /* Look inside the argument pack. */
20102 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20103 len
= TREE_VEC_LENGTH (packed_args
);
20106 for (k
= 0; k
< len
; ++k
)
20109 arg
= TREE_VEC_ELT (packed_args
, k
);
20111 if (error_operand_p (arg
))
20113 else if (TREE_CODE (arg
) == TEMPLATE_DECL
20114 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20116 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20118 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20119 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20128 /* Returns true if ARGS (a collection of template arguments) contains
20129 any dependent arguments. */
20132 any_dependent_template_arguments_p (const_tree args
)
20139 if (args
== error_mark_node
)
20142 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20144 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20145 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20146 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20153 /* Returns TRUE if the template TMPL is dependent. */
20156 dependent_template_p (tree tmpl
)
20158 if (TREE_CODE (tmpl
) == OVERLOAD
)
20162 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20164 tmpl
= OVL_NEXT (tmpl
);
20169 /* Template template parameters are dependent. */
20170 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20171 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20173 /* So are names that have not been looked up. */
20174 if (TREE_CODE (tmpl
) == SCOPE_REF
20175 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
20177 /* So are member templates of dependent classes. */
20178 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20179 return dependent_type_p (DECL_CONTEXT (tmpl
));
20183 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20186 dependent_template_id_p (tree tmpl
, tree args
)
20188 return (dependent_template_p (tmpl
)
20189 || any_dependent_template_arguments_p (args
));
20192 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20196 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20200 if (!processing_template_decl
)
20203 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20205 tree decl
= TREE_VEC_ELT (declv
, i
);
20206 tree init
= TREE_VEC_ELT (initv
, i
);
20207 tree cond
= TREE_VEC_ELT (condv
, i
);
20208 tree incr
= TREE_VEC_ELT (incrv
, i
);
20210 if (type_dependent_expression_p (decl
))
20213 if (init
&& type_dependent_expression_p (init
))
20216 if (type_dependent_expression_p (cond
))
20219 if (COMPARISON_CLASS_P (cond
)
20220 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20221 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20224 if (TREE_CODE (incr
) == MODOP_EXPR
)
20226 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20227 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20230 else if (type_dependent_expression_p (incr
))
20232 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20234 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20236 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20238 tree t
= TREE_OPERAND (incr
, 1);
20239 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20240 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20249 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20250 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20251 no such TYPE can be found. Note that this function peers inside
20252 uninstantiated templates and therefore should be used only in
20253 extremely limited situations. ONLY_CURRENT_P restricts this
20254 peering to the currently open classes hierarchy (which is required
20255 when comparing types). */
20258 resolve_typename_type (tree type
, bool only_current_p
)
20267 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20269 scope
= TYPE_CONTEXT (type
);
20270 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20271 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20272 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20273 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20274 identifier of the TYPENAME_TYPE anymore.
20275 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20276 TYPENAME_TYPE instead, we avoid messing up with a possible
20277 typedef variant case. */
20278 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20280 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20281 it first before we can figure out what NAME refers to. */
20282 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20284 if (TYPENAME_IS_RESOLVING_P (scope
))
20285 /* Given a class template A with a dependent base with nested type C,
20286 typedef typename A::C::C C will land us here, as trying to resolve
20287 the initial A::C leads to the local C typedef, which leads back to
20288 A::C::C. So we break the recursion now. */
20291 scope
= resolve_typename_type (scope
, only_current_p
);
20293 /* If we don't know what SCOPE refers to, then we cannot resolve the
20295 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20297 /* If the SCOPE is a template type parameter, we have no way of
20298 resolving the name. */
20299 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20301 /* If the SCOPE is not the current instantiation, there's no reason
20302 to look inside it. */
20303 if (only_current_p
&& !currently_open_class (scope
))
20305 /* If this is a typedef, we don't want to look inside (c++/11987). */
20306 if (typedef_variant_p (type
))
20308 /* If SCOPE isn't the template itself, it will not have a valid
20309 TYPE_FIELDS list. */
20310 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20311 /* scope is either the template itself or a compatible instantiation
20312 like X<T>, so look up the name in the original template. */
20313 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20315 /* scope is a partial instantiation, so we can't do the lookup or we
20316 will lose the template arguments. */
20318 /* Enter the SCOPE so that name lookup will be resolved as if we
20319 were in the class definition. In particular, SCOPE will no
20320 longer be considered a dependent type. */
20321 pushed_scope
= push_scope (scope
);
20322 /* Look up the declaration. */
20323 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20324 tf_warning_or_error
);
20326 result
= NULL_TREE
;
20328 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20329 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20332 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20333 && TREE_CODE (decl
) == TYPE_DECL
)
20335 result
= TREE_TYPE (decl
);
20336 if (result
== error_mark_node
)
20337 result
= NULL_TREE
;
20339 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20340 && DECL_CLASS_TEMPLATE_P (decl
))
20344 /* Obtain the template and the arguments. */
20345 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20346 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20347 /* Instantiate the template. */
20348 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20349 /*entering_scope=*/0,
20350 tf_error
| tf_user
);
20351 if (result
== error_mark_node
)
20352 result
= NULL_TREE
;
20355 /* Leave the SCOPE. */
20357 pop_scope (pushed_scope
);
20359 /* If we failed to resolve it, return the original typename. */
20363 /* If lookup found a typename type, resolve that too. */
20364 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20366 /* Ill-formed programs can cause infinite recursion here, so we
20367 must catch that. */
20368 TYPENAME_IS_RESOLVING_P (type
) = 1;
20369 result
= resolve_typename_type (result
, only_current_p
);
20370 TYPENAME_IS_RESOLVING_P (type
) = 0;
20373 /* Qualify the resulting type. */
20374 quals
= cp_type_quals (type
);
20376 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20381 /* EXPR is an expression which is not type-dependent. Return a proxy
20382 for EXPR that can be used to compute the types of larger
20383 expressions containing EXPR. */
20386 build_non_dependent_expr (tree expr
)
20390 #ifdef ENABLE_CHECKING
20391 /* Try to get a constant value for all non-dependent expressions in
20392 order to expose bugs in *_dependent_expression_p and constexpr. */
20393 if (cxx_dialect
>= cxx0x
20394 && !instantiation_dependent_expression_p (expr
))
20395 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20398 /* Preserve OVERLOADs; the functions must be available to resolve
20401 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20402 inner_expr
= stmt_expr_value_expr (inner_expr
);
20403 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20404 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20405 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20406 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20407 if (is_overloaded_fn (inner_expr
)
20408 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20410 /* There is no need to return a proxy for a variable. */
20411 if (TREE_CODE (expr
) == VAR_DECL
)
20413 /* Preserve string constants; conversions from string constants to
20414 "char *" are allowed, even though normally a "const char *"
20415 cannot be used to initialize a "char *". */
20416 if (TREE_CODE (expr
) == STRING_CST
)
20418 /* Preserve arithmetic constants, as an optimization -- there is no
20419 reason to create a new node. */
20420 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20422 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20423 There is at least one place where we want to know that a
20424 particular expression is a throw-expression: when checking a ?:
20425 expression, there are special rules if the second or third
20426 argument is a throw-expression. */
20427 if (TREE_CODE (expr
) == THROW_EXPR
)
20430 /* Don't wrap an initializer list, we need to be able to look inside. */
20431 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20434 /* Don't wrap a dummy object, we need to be able to test for it. */
20435 if (is_dummy_object (expr
))
20438 if (TREE_CODE (expr
) == COND_EXPR
)
20439 return build3 (COND_EXPR
,
20441 TREE_OPERAND (expr
, 0),
20442 (TREE_OPERAND (expr
, 1)
20443 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20444 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20445 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20446 if (TREE_CODE (expr
) == COMPOUND_EXPR
20447 && !COMPOUND_EXPR_OVERLOADED (expr
))
20448 return build2 (COMPOUND_EXPR
,
20450 TREE_OPERAND (expr
, 0),
20451 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20453 /* If the type is unknown, it can't really be non-dependent */
20454 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20456 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20457 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20460 /* ARGS is a vector of expressions as arguments to a function call.
20461 Replace the arguments with equivalent non-dependent expressions.
20462 This modifies ARGS in place. */
20465 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
20470 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
20472 tree newarg
= build_non_dependent_expr (arg
);
20474 (*args
)[ix
] = newarg
;
20478 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20479 with a level one deeper than the actual template parms. */
20484 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20485 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20486 TYPE_DECL
, get_identifier ("auto"), au
);
20487 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20488 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20489 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20490 TYPE_NAME (au
), NULL_TREE
);
20491 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20492 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20493 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20498 /* Given type ARG, return std::initializer_list<ARG>. */
20503 tree std_init_list
= namespace_binding
20504 (get_identifier ("initializer_list"), std_node
);
20506 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20508 error ("deducing from brace-enclosed initializer list requires "
20509 "#include <initializer_list>");
20510 return error_mark_node
;
20512 argvec
= make_tree_vec (1);
20513 TREE_VEC_ELT (argvec
, 0) = arg
;
20514 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20515 NULL_TREE
, 0, tf_warning_or_error
);
20518 /* Replace auto in TYPE with std::initializer_list<auto>. */
20521 listify_autos (tree type
, tree auto_node
)
20523 tree init_auto
= listify (auto_node
);
20524 tree argvec
= make_tree_vec (1);
20525 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20526 if (processing_template_decl
)
20527 argvec
= add_to_template_args (current_template_args (), argvec
);
20528 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20531 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20532 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20535 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20537 tree parms
, tparms
, targs
;
20541 if (init
== error_mark_node
)
20542 return error_mark_node
;
20544 if (type_dependent_expression_p (init
))
20545 /* Defining a subset of type-dependent expressions that we can deduce
20546 from ahead of time isn't worth the trouble. */
20549 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20550 with either a new invented type template parameter U or, if the
20551 initializer is a braced-init-list (8.5.4), with
20552 std::initializer_list<U>. */
20553 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20554 type
= listify_autos (type
, auto_node
);
20556 init
= resolve_nondeduced_context (init
);
20558 parms
= build_tree_list (NULL_TREE
, type
);
20560 tparms
= make_tree_vec (1);
20561 targs
= make_tree_vec (1);
20562 TREE_VEC_ELT (tparms
, 0)
20563 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20564 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20565 DEDUCE_CALL
, LOOKUP_NORMAL
,
20566 /*explain_p=*/false);
20569 if (processing_template_decl
)
20570 /* Try again at instantiation time. */
20572 if (type
&& type
!= error_mark_node
)
20573 /* If type is error_mark_node a diagnostic must have been
20574 emitted by now. Also, having a mention to '<type error>'
20575 in the diagnostic is not really useful to the user. */
20577 if (cfun
&& auto_node
== current_function_auto_return_pattern
20578 && LAMBDA_FUNCTION_P (current_function_decl
))
20579 error ("unable to deduce lambda return type from %qE", init
);
20581 error ("unable to deduce %qT from %qE", type
, init
);
20583 return error_mark_node
;
20586 /* If the list of declarators contains more than one declarator, the type
20587 of each declared variable is determined as described above. If the
20588 type deduced for the template parameter U is not the same in each
20589 deduction, the program is ill-formed. */
20590 if (TREE_TYPE (auto_node
)
20591 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20593 if (cfun
&& auto_node
== current_function_auto_return_pattern
20594 && LAMBDA_FUNCTION_P (current_function_decl
))
20595 error ("inconsistent types %qT and %qT deduced for "
20596 "lambda return type", TREE_TYPE (auto_node
),
20597 TREE_VEC_ELT (targs
, 0));
20599 error ("inconsistent deduction for %qT: %qT and then %qT",
20600 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20601 return error_mark_node
;
20603 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20605 if (processing_template_decl
)
20606 targs
= add_to_template_args (current_template_args (), targs
);
20607 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20610 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20614 splice_late_return_type (tree type
, tree late_return_type
)
20618 if (late_return_type
== NULL_TREE
)
20620 argvec
= make_tree_vec (1);
20621 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20622 if (processing_template_parmlist
)
20623 /* For a late-specified return type in a template type-parameter, we
20624 need to add a dummy argument level for its parmlist. */
20625 argvec
= add_to_template_args
20626 (make_tree_vec (processing_template_parmlist
), argvec
);
20627 if (current_template_parms
)
20628 argvec
= add_to_template_args (current_template_args (), argvec
);
20629 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20632 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20635 is_auto (const_tree type
)
20637 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20638 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20644 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20645 appear as a type-specifier for the declaration in question, we don't
20646 have to look through the whole type. */
20649 type_uses_auto (tree type
)
20651 enum tree_code code
;
20652 if (is_auto (type
))
20655 code
= TREE_CODE (type
);
20657 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20658 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20659 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20660 return type_uses_auto (TREE_TYPE (type
));
20662 if (TYPE_PTRMEMFUNC_P (type
))
20663 return type_uses_auto (TREE_TYPE (TREE_TYPE
20664 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20669 /* For a given template T, return the vector of typedefs referenced
20670 in T for which access check is needed at T instantiation time.
20671 T is either a FUNCTION_DECL or a RECORD_TYPE.
20672 Those typedefs were added to T by the function
20673 append_type_to_template_for_access_check. */
20675 vec
<qualified_typedef_usage_t
, va_gc
> *
20676 get_types_needing_access_check (tree t
)
20679 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
20681 if (!t
|| t
== error_mark_node
)
20684 if (!(ti
= get_template_info (t
)))
20687 if (CLASS_TYPE_P (t
)
20688 || TREE_CODE (t
) == FUNCTION_DECL
)
20690 if (!TI_TEMPLATE (ti
))
20693 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20699 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20700 tied to T. That list of typedefs will be access checked at
20701 T instantiation time.
20702 T is either a FUNCTION_DECL or a RECORD_TYPE.
20703 TYPE_DECL is a TYPE_DECL node representing a typedef.
20704 SCOPE is the scope through which TYPE_DECL is accessed.
20705 LOCATION is the location of the usage point of TYPE_DECL.
20707 This function is a subroutine of
20708 append_type_to_template_for_access_check. */
20711 append_type_to_template_for_access_check_1 (tree t
,
20714 location_t location
)
20716 qualified_typedef_usage_t typedef_usage
;
20719 if (!t
|| t
== error_mark_node
)
20722 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20723 || CLASS_TYPE_P (t
))
20725 && TREE_CODE (type_decl
) == TYPE_DECL
20728 if (!(ti
= get_template_info (t
)))
20731 gcc_assert (TI_TEMPLATE (ti
));
20733 typedef_usage
.typedef_decl
= type_decl
;
20734 typedef_usage
.context
= scope
;
20735 typedef_usage
.locus
= location
;
20737 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
20740 /* Append TYPE_DECL to the template TEMPL.
20741 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20742 At TEMPL instanciation time, TYPE_DECL will be checked to see
20743 if it can be accessed through SCOPE.
20744 LOCATION is the location of the usage point of TYPE_DECL.
20746 e.g. consider the following code snippet:
20753 template<class U> struct S
20755 C::myint mi; // <-- usage point of the typedef C::myint
20760 At S<char> instantiation time, we need to check the access of C::myint
20761 In other words, we need to check the access of the myint typedef through
20762 the C scope. For that purpose, this function will add the myint typedef
20763 and the scope C through which its being accessed to a list of typedefs
20764 tied to the template S. That list will be walked at template instantiation
20765 time and access check performed on each typedefs it contains.
20766 Note that this particular code snippet should yield an error because
20767 myint is private to C. */
20770 append_type_to_template_for_access_check (tree templ
,
20773 location_t location
)
20775 qualified_typedef_usage_t
*iter
;
20778 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20780 /* Make sure we don't append the type to the template twice. */
20781 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
20782 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20785 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20789 /* Set up the hash tables for template instantiations. */
20792 init_template_processing (void)
20794 decl_specializations
= htab_create_ggc (37,
20795 hash_specialization
,
20796 eq_specializations
,
20798 type_specializations
= htab_create_ggc (37,
20799 hash_specialization
,
20800 eq_specializations
,
20804 /* Print stats about the template hash tables for -fstats. */
20807 print_template_statistics (void)
20809 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20810 "%f collisions\n", (long) htab_size (decl_specializations
),
20811 (long) htab_elements (decl_specializations
),
20812 htab_collisions (decl_specializations
));
20813 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20814 "%f collisions\n", (long) htab_size (type_specializations
),
20815 (long) htab_elements (type_specializations
),
20816 htab_collisions (type_specializations
));
20819 #include "gt-cp-pt.h"