1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2014 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"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "pointer-set.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
51 /* The type of functions taking a tree, and some additional data, and
53 typedef int (*tree_fn_t
) (tree
, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template
{
59 struct pending_template
*next
;
60 struct tinst_level
*tinst
;
63 static GTY(()) struct pending_template
*pending_templates
;
64 static GTY(()) struct pending_template
*last_pending_template
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static GTY(()) tree saved_trees
;
70 static vec
<int> inline_parm_levels
;
72 static GTY(()) struct tinst_level
*current_tinst_level
;
74 static GTY(()) tree saved_access_scope
;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr
;
81 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
84 static struct pointer_map_t
*local_specializations
;
86 /* True if we've recursed into fn_type_unification too many times. */
87 static bool excessive_deduction_depth
;
89 typedef struct GTY(()) spec_entry
96 static GTY ((param_is (spec_entry
)))
97 htab_t decl_specializations
;
99 static GTY ((param_is (spec_entry
)))
100 htab_t type_specializations
;
102 /* Contains canonical template parameter types. The vector is indexed by
103 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
104 TREE_LIST, whose TREE_VALUEs contain the canonical template
105 parameters of various types and levels. */
106 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
108 #define UNIFY_ALLOW_NONE 0
109 #define UNIFY_ALLOW_MORE_CV_QUAL 1
110 #define UNIFY_ALLOW_LESS_CV_QUAL 2
111 #define UNIFY_ALLOW_DERIVED 4
112 #define UNIFY_ALLOW_INTEGER 8
113 #define UNIFY_ALLOW_OUTER_LEVEL 16
114 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
115 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
117 enum template_base_result
{
119 tbr_ambiguous_baseclass
,
123 static void push_access_scope (tree
);
124 static void pop_access_scope (tree
);
125 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
126 unification_kind_t
, int,
128 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
129 unification_kind_t
, int, bool, bool);
130 static int unify (tree
, tree
, tree
, tree
, int, bool);
131 static void add_pending_template (tree
);
132 static tree
reopen_tinst_level (struct tinst_level
*);
133 static tree
tsubst_initializer_list (tree
, tree
);
134 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
135 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
137 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
139 static void tsubst_enum (tree
, tree
, tree
);
140 static tree
add_to_template_args (tree
, tree
);
141 static tree
add_outermost_template_args (tree
, tree
);
142 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
143 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
145 static int type_unification_real (tree
, tree
, tree
, const tree
*,
146 unsigned int, int, unification_kind_t
, int,
147 vec
<deferred_access_check
, va_gc
> **,
149 static void note_template_header (int);
150 static tree
convert_nontype_argument_function (tree
, tree
);
151 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
152 static tree
convert_template_argument (tree
, tree
, tree
,
153 tsubst_flags_t
, int, tree
);
154 static int for_each_template_parm (tree
, tree_fn_t
, void*,
155 struct pointer_set_t
*, bool);
156 static tree
expand_template_argument_pack (tree
);
157 static tree
build_template_parm_index (int, int, int, tree
, tree
);
158 static bool inline_needs_template_parms (tree
, bool);
159 static void push_inline_template_parms_recursive (tree
, int);
160 static tree
retrieve_local_specialization (tree
);
161 static void register_local_specialization (tree
, tree
);
162 static hashval_t
hash_specialization (const void *p
);
163 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
164 static int mark_template_parm (tree
, void *);
165 static int template_parm_this_level_p (tree
, void *);
166 static tree
tsubst_friend_function (tree
, tree
);
167 static tree
tsubst_friend_class (tree
, tree
);
168 static int can_complete_type_without_circularity (tree
);
169 static tree
get_bindings (tree
, tree
, tree
, bool);
170 static int template_decl_level (tree
);
171 static int check_cv_quals_for_unify (int, tree
, tree
);
172 static void template_parm_level_and_index (tree
, int*, int*);
173 static int unify_pack_expansion (tree
, tree
, tree
,
174 tree
, unification_kind_t
, bool, bool);
175 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
176 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
178 static void regenerate_decl_from_template (tree
, tree
);
179 static tree
most_specialized_class (tree
, tsubst_flags_t
);
180 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
181 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
182 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
183 static bool check_specialization_scope (void);
184 static tree
process_partial_specialization (tree
);
185 static void set_current_access_from_decl (tree
);
186 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
188 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
189 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
191 static bool template_template_parm_bindings_ok_p (tree
, tree
);
192 static int template_args_equal (tree
, tree
);
193 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
194 static tree
for_each_template_parm_r (tree
*, int *, void *);
195 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
196 static void copy_default_args_to_explicit_spec (tree
);
197 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
198 static bool dependent_template_arg_p (tree
);
199 static bool any_template_arguments_need_structural_equality_p (tree
);
200 static bool dependent_type_p_r (tree
);
201 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
202 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
203 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
204 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
205 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
206 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
208 static tree
listify (tree
);
209 static tree
listify_autos (tree
, tree
);
210 static tree
template_parm_to_arg (tree t
);
211 static tree
current_template_args (void);
212 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
213 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
215 /* Make the current scope suitable for access checking when we are
216 processing T. T can be FUNCTION_DECL for instantiated function
217 template, VAR_DECL for static member variable, or TYPE_DECL for
218 alias template (needed by instantiate_decl). */
221 push_access_scope (tree t
)
223 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
224 || TREE_CODE (t
) == TYPE_DECL
);
226 if (DECL_FRIEND_CONTEXT (t
))
227 push_nested_class (DECL_FRIEND_CONTEXT (t
));
228 else if (DECL_CLASS_SCOPE_P (t
))
229 push_nested_class (DECL_CONTEXT (t
));
231 push_to_top_level ();
233 if (TREE_CODE (t
) == FUNCTION_DECL
)
235 saved_access_scope
= tree_cons
236 (NULL_TREE
, current_function_decl
, saved_access_scope
);
237 current_function_decl
= t
;
241 /* Restore the scope set up by push_access_scope. T is the node we
245 pop_access_scope (tree t
)
247 if (TREE_CODE (t
) == FUNCTION_DECL
)
249 current_function_decl
= TREE_VALUE (saved_access_scope
);
250 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
253 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
256 pop_from_top_level ();
259 /* Do any processing required when DECL (a member template
260 declaration) is finished. Returns the TEMPLATE_DECL corresponding
261 to DECL, unless it is a specialization, in which case the DECL
262 itself is returned. */
265 finish_member_template_decl (tree decl
)
267 if (decl
== error_mark_node
)
268 return error_mark_node
;
270 gcc_assert (DECL_P (decl
));
272 if (TREE_CODE (decl
) == TYPE_DECL
)
276 type
= TREE_TYPE (decl
);
277 if (type
== error_mark_node
)
278 return error_mark_node
;
279 if (MAYBE_CLASS_TYPE_P (type
)
280 && CLASSTYPE_TEMPLATE_INFO (type
)
281 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
283 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
284 check_member_template (tmpl
);
289 else if (TREE_CODE (decl
) == FIELD_DECL
)
290 error ("data member %qD cannot be a member template", decl
);
291 else if (DECL_TEMPLATE_INFO (decl
))
293 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
295 check_member_template (DECL_TI_TEMPLATE (decl
));
296 return DECL_TI_TEMPLATE (decl
);
302 error ("invalid member template declaration %qD", decl
);
304 return error_mark_node
;
307 /* Create a template info node. */
310 build_template_info (tree template_decl
, tree template_args
)
312 tree result
= make_node (TEMPLATE_INFO
);
313 TI_TEMPLATE (result
) = template_decl
;
314 TI_ARGS (result
) = template_args
;
318 /* Return the template info node corresponding to T, whatever T is. */
321 get_template_info (const_tree t
)
323 tree tinfo
= NULL_TREE
;
325 if (!t
|| t
== error_mark_node
)
328 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
329 tinfo
= DECL_TEMPLATE_INFO (t
);
331 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
334 if (OVERLOAD_TYPE_P (t
))
335 tinfo
= TYPE_TEMPLATE_INFO (t
);
336 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
337 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
342 /* Returns the template nesting level of the indicated class TYPE.
352 A<T>::B<U> has depth two, while A<T> has depth one.
353 Both A<T>::B<int> and A<int>::B<U> have depth one, if
354 they are instantiations, not specializations.
356 This function is guaranteed to return 0 if passed NULL_TREE so
357 that, for example, `template_class_depth (current_class_type)' is
361 template_class_depth (tree type
)
366 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
367 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
368 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
370 tree tinfo
= get_template_info (type
);
372 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
373 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
380 /* Subroutine of maybe_begin_member_template_processing.
381 Returns true if processing DECL needs us to push template parms. */
384 inline_needs_template_parms (tree decl
, bool nsdmi
)
386 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
389 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
390 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
393 /* Subroutine of maybe_begin_member_template_processing.
394 Push the template parms in PARMS, starting from LEVELS steps into the
395 chain, and ending at the beginning, since template parms are listed
399 push_inline_template_parms_recursive (tree parmlist
, int levels
)
401 tree parms
= TREE_VALUE (parmlist
);
405 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
407 ++processing_template_decl
;
408 current_template_parms
409 = tree_cons (size_int (processing_template_decl
),
410 parms
, current_template_parms
);
411 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
413 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
415 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
417 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
419 if (parm
== error_mark_node
)
422 gcc_assert (DECL_P (parm
));
424 switch (TREE_CODE (parm
))
433 /* Make a CONST_DECL as is done in process_template_parm.
434 It is ugly that we recreate this here; the original
435 version built in process_template_parm is no longer
437 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
438 CONST_DECL
, DECL_NAME (parm
),
440 DECL_ARTIFICIAL (decl
) = 1;
441 TREE_CONSTANT (decl
) = 1;
442 TREE_READONLY (decl
) = 1;
443 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
444 SET_DECL_TEMPLATE_PARM_P (decl
);
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
460 maybe_begin_member_template_processing (tree decl
)
464 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
467 decl
= (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
468 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl
))
471 if (inline_needs_template_parms (decl
, nsdmi
))
473 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
474 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
476 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
479 parms
= TREE_CHAIN (parms
);
482 push_inline_template_parms_recursive (parms
, levels
);
485 /* Remember how many levels of template parameters we pushed so that
486 we can pop them later. */
487 inline_parm_levels
.safe_push (levels
);
490 /* Undo the effects of maybe_begin_member_template_processing. */
493 maybe_end_member_template_processing (void)
498 if (inline_parm_levels
.length () == 0)
501 last
= inline_parm_levels
.pop ();
502 for (i
= 0; i
< last
; ++i
)
504 --processing_template_decl
;
505 current_template_parms
= TREE_CHAIN (current_template_parms
);
510 /* Return a new template argument vector which contains all of ARGS,
511 but has as its innermost set of arguments the EXTRA_ARGS. */
514 add_to_template_args (tree args
, tree extra_args
)
521 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
524 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
525 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
527 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
528 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
530 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
531 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
536 /* Like add_to_template_args, but only the outermost ARGS are added to
537 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
538 (EXTRA_ARGS) levels are added. This function is used to combine
539 the template arguments from a partial instantiation with the
540 template arguments used to attain the full instantiation from the
541 partial instantiation. */
544 add_outermost_template_args (tree args
, tree extra_args
)
548 /* If there are more levels of EXTRA_ARGS than there are ARGS,
549 something very fishy is going on. */
550 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
552 /* If *all* the new arguments will be the EXTRA_ARGS, just return
554 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
557 /* For the moment, we make ARGS look like it contains fewer levels. */
558 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
560 new_args
= add_to_template_args (args
, extra_args
);
562 /* Now, we restore ARGS to its full dimensions. */
563 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
568 /* Return the N levels of innermost template arguments from the ARGS. */
571 get_innermost_template_args (tree args
, int n
)
579 /* If N is 1, just return the innermost set of template arguments. */
581 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
583 /* If we're not removing anything, just return the arguments we were
585 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
586 gcc_assert (extra_levels
>= 0);
587 if (extra_levels
== 0)
590 /* Make a new set of arguments, not containing the outer arguments. */
591 new_args
= make_tree_vec (n
);
592 for (i
= 1; i
<= n
; ++i
)
593 SET_TMPL_ARGS_LEVEL (new_args
, i
,
594 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
599 /* The inverse of get_innermost_template_args: Return all but the innermost
600 EXTRA_LEVELS levels of template arguments from the ARGS. */
603 strip_innermost_template_args (tree args
, int extra_levels
)
606 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
611 /* If N is 1, just return the outermost set of template arguments. */
613 return TMPL_ARGS_LEVEL (args
, 1);
615 /* If we're not removing anything, just return the arguments we were
617 gcc_assert (extra_levels
>= 0);
618 if (extra_levels
== 0)
621 /* Make a new set of arguments, not containing the inner arguments. */
622 new_args
= make_tree_vec (n
);
623 for (i
= 1; i
<= n
; ++i
)
624 SET_TMPL_ARGS_LEVEL (new_args
, i
,
625 TMPL_ARGS_LEVEL (args
, i
));
630 /* We've got a template header coming up; push to a new level for storing
634 begin_template_parm_list (void)
636 /* We use a non-tag-transparent scope here, which causes pushtag to
637 put tags in this scope, rather than in the enclosing class or
638 namespace scope. This is the right thing, since we want
639 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
640 global template class, push_template_decl handles putting the
641 TEMPLATE_DECL into top-level scope. For a nested template class,
644 template <class T> struct S1 {
645 template <class T> struct S2 {};
648 pushtag contains special code to call pushdecl_with_scope on the
649 TEMPLATE_DECL for S2. */
650 begin_scope (sk_template_parms
, NULL
);
651 ++processing_template_decl
;
652 ++processing_template_parmlist
;
653 note_template_header (0);
656 /* This routine is called when a specialization is declared. If it is
657 invalid to declare a specialization here, an error is reported and
658 false is returned, otherwise this routine will return true. */
661 check_specialization_scope (void)
663 tree scope
= current_scope ();
667 An explicit specialization shall be declared in the namespace of
668 which the template is a member, or, for member templates, in the
669 namespace of which the enclosing class or enclosing class
670 template is a member. An explicit specialization of a member
671 function, member class or static data member of a class template
672 shall be declared in the namespace of which the class template
674 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
676 error ("explicit specialization in non-namespace scope %qD", scope
);
682 In an explicit specialization declaration for a member of a class
683 template or a member template that appears in namespace scope,
684 the member template and some of its enclosing class templates may
685 remain unspecialized, except that the declaration shall not
686 explicitly specialize a class member template if its enclosing
687 class templates are not explicitly specialized as well. */
688 if (current_template_parms
)
690 error ("enclosing class templates are not explicitly specialized");
697 /* We've just seen template <>. */
700 begin_specialization (void)
702 begin_scope (sk_template_spec
, NULL
);
703 note_template_header (1);
704 return check_specialization_scope ();
707 /* Called at then end of processing a declaration preceded by
711 end_specialization (void)
714 reset_specialization ();
717 /* Any template <>'s that we have seen thus far are not referring to a
718 function specialization. */
721 reset_specialization (void)
723 processing_specialization
= 0;
724 template_header_count
= 0;
727 /* We've just seen a template header. If SPECIALIZATION is nonzero,
728 it was of the form template <>. */
731 note_template_header (int specialization
)
733 processing_specialization
= specialization
;
734 template_header_count
++;
737 /* We're beginning an explicit instantiation. */
740 begin_explicit_instantiation (void)
742 gcc_assert (!processing_explicit_instantiation
);
743 processing_explicit_instantiation
= true;
748 end_explicit_instantiation (void)
750 gcc_assert (processing_explicit_instantiation
);
751 processing_explicit_instantiation
= false;
754 /* An explicit specialization or partial specialization of TMPL is being
755 declared. Check that the namespace in which the specialization is
756 occurring is permissible. Returns false iff it is invalid to
757 specialize TMPL in the current namespace. */
760 check_specialization_namespace (tree tmpl
)
762 tree tpl_ns
= decl_namespace_context (tmpl
);
766 An explicit specialization shall be declared in the namespace of
767 which the template is a member, or, for member templates, in the
768 namespace of which the enclosing class or enclosing class
769 template is a member. An explicit specialization of a member
770 function, member class or static data member of a class template
771 shall be declared in the namespace of which the class template is
773 if (current_scope() != DECL_CONTEXT (tmpl
)
774 && !at_namespace_scope_p ())
776 error ("specialization of %qD must appear at namespace scope", tmpl
);
779 if (is_associated_namespace (current_namespace
, tpl_ns
))
780 /* Same or super-using namespace. */
784 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
785 permerror (input_location
, " from definition of %q+#D", tmpl
);
790 /* SPEC is an explicit instantiation. Check that it is valid to
791 perform this explicit instantiation in the current namespace. */
794 check_explicit_instantiation_namespace (tree spec
)
798 /* DR 275: An explicit instantiation shall appear in an enclosing
799 namespace of its template. */
800 ns
= decl_namespace_context (spec
);
801 if (!is_ancestor (current_namespace
, ns
))
802 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
803 "(which does not enclose namespace %qD)",
804 spec
, current_namespace
, ns
);
807 /* The TYPE is being declared. If it is a template type, that means it
808 is a partial specialization. Do appropriate error-checking. */
811 maybe_process_partial_specialization (tree type
)
815 if (type
== error_mark_node
)
816 return error_mark_node
;
818 /* A lambda that appears in specialization context is not itself a
820 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
823 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
825 error ("name of class shadows template template parameter %qD",
827 return error_mark_node
;
830 context
= TYPE_CONTEXT (type
);
832 if (TYPE_ALIAS_P (type
))
834 if (TYPE_TEMPLATE_INFO (type
)
835 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
836 error ("specialization of alias template %qD",
837 TYPE_TI_TEMPLATE (type
));
839 error ("explicit specialization of non-template %qT", type
);
840 return error_mark_node
;
842 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
844 /* This is for ordinary explicit specialization and partial
845 specialization of a template class such as:
847 template <> class C<int>;
851 template <class T> class C<T*>;
853 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
855 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
856 && !COMPLETE_TYPE_P (type
))
858 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
859 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
860 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
861 if (processing_template_decl
)
863 if (push_template_decl (TYPE_MAIN_DECL (type
))
865 return error_mark_node
;
868 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
869 error ("specialization of %qT after instantiation", type
);
870 else if (errorcount
&& !processing_specialization
871 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
872 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
873 /* Trying to define a specialization either without a template<> header
874 or in an inappropriate place. We've already given an error, so just
875 bail now so we don't actually define the specialization. */
876 return error_mark_node
;
878 else if (CLASS_TYPE_P (type
)
879 && !CLASSTYPE_USE_TEMPLATE (type
)
880 && CLASSTYPE_TEMPLATE_INFO (type
)
881 && context
&& CLASS_TYPE_P (context
)
882 && CLASSTYPE_TEMPLATE_INFO (context
))
884 /* This is for an explicit specialization of member class
885 template according to [temp.expl.spec/18]:
887 template <> template <class U> class C<int>::D;
889 The context `C<int>' must be an implicit instantiation.
890 Otherwise this is just a member class template declared
893 template <> class C<int> { template <class U> class D; };
894 template <> template <class U> class C<int>::D;
896 In the first case, `C<int>::D' is a specialization of `C<T>::D'
897 while in the second case, `C<int>::D' is a primary template
898 and `C<T>::D' may not exist. */
900 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
901 && !COMPLETE_TYPE_P (type
))
904 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
906 if (current_namespace
907 != decl_namespace_context (tmpl
))
909 permerror (input_location
, "specializing %q#T in different namespace", type
);
910 permerror (input_location
, " from definition of %q+#D", tmpl
);
913 /* Check for invalid specialization after instantiation:
915 template <> template <> class C<int>::D<int>;
916 template <> template <class U> class C<int>::D; */
918 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
919 t
; t
= TREE_CHAIN (t
))
921 tree inst
= TREE_VALUE (t
);
922 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
924 /* We already have a full specialization of this partial
925 instantiation. Reassign it to the new member
926 specialization template. */
931 elt
.tmpl
= most_general_template (tmpl
);
932 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
935 htab_remove_elt (type_specializations
, &elt
);
938 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
940 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
941 entry
= ggc_alloc_spec_entry ();
945 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
946 /* But if we've had an implicit instantiation, that's a
947 problem ([temp.expl.spec]/6). */
948 error ("specialization %qT after instantiation %qT",
952 /* Mark TYPE as a specialization. And as a result, we only
953 have one level of template argument for the innermost
955 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
956 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
957 CLASSTYPE_TI_ARGS (type
)
958 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
961 else if (processing_specialization
)
963 /* Someday C++0x may allow for enum template specialization. */
964 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
965 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
966 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
967 "of %qD not allowed by ISO C++", type
);
970 error ("explicit specialization of non-template %qT", type
);
971 return error_mark_node
;
978 /* Returns nonzero if we can optimize the retrieval of specializations
979 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
980 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
983 optimize_specialization_lookup_p (tree tmpl
)
985 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
986 && DECL_CLASS_SCOPE_P (tmpl
)
987 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
989 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
990 /* The optimized lookup depends on the fact that the
991 template arguments for the member function template apply
992 purely to the containing class, which is not true if the
993 containing class is an explicit or partial
995 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
996 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
997 && !DECL_CONV_FN_P (tmpl
)
998 /* It is possible to have a template that is not a member
999 template and is not a member of a template class:
1001 template <typename T>
1002 struct S { friend A::f(); };
1004 Here, the friend function is a template, but the context does
1005 not have template information. The optimized lookup relies
1006 on having ARGS be the template arguments for both the class
1007 and the function template. */
1008 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1011 /* Retrieve the specialization (in the sense of [temp.spec] - a
1012 specialization is either an instantiation or an explicit
1013 specialization) of TMPL for the given template ARGS. If there is
1014 no such specialization, return NULL_TREE. The ARGS are a vector of
1015 arguments, or a vector of vectors of arguments, in the case of
1016 templates with more than one level of parameters.
1018 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1019 then we search for a partial specialization matching ARGS. This
1020 parameter is ignored if TMPL is not a class template.
1022 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1023 result is a NONTYPE_ARGUMENT_PACK. */
1026 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1028 if (tmpl
== NULL_TREE
)
1031 if (args
== error_mark_node
)
1034 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1035 || TREE_CODE (tmpl
) == FIELD_DECL
);
1037 /* There should be as many levels of arguments as there are
1038 levels of parameters. */
1039 gcc_assert (TMPL_ARGS_DEPTH (args
)
1040 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1041 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1042 : template_class_depth (DECL_CONTEXT (tmpl
))));
1044 if (optimize_specialization_lookup_p (tmpl
))
1046 tree class_template
;
1047 tree class_specialization
;
1048 vec
<tree
, va_gc
> *methods
;
1052 /* The template arguments actually apply to the containing
1053 class. Find the class specialization with those
1055 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1056 class_specialization
1057 = retrieve_specialization (class_template
, args
, 0);
1058 if (!class_specialization
)
1060 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1061 for the specialization. */
1062 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1065 /* Iterate through the methods with the indicated name, looking
1066 for the one that has an instance of TMPL. */
1067 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1068 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1070 tree fn
= OVL_CURRENT (fns
);
1071 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1072 /* using-declarations can add base methods to the method vec,
1073 and we don't want those here. */
1074 && DECL_CONTEXT (fn
) == class_specialization
)
1083 htab_t specializations
;
1087 elt
.spec
= NULL_TREE
;
1089 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1090 specializations
= type_specializations
;
1092 specializations
= decl_specializations
;
1095 hash
= hash_specialization (&elt
);
1096 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1104 /* Like retrieve_specialization, but for local declarations. */
1107 retrieve_local_specialization (tree tmpl
)
1111 if (local_specializations
== NULL
)
1114 slot
= pointer_map_contains (local_specializations
, tmpl
);
1115 return slot
? (tree
) *slot
: NULL_TREE
;
1118 /* Returns nonzero iff DECL is a specialization of TMPL. */
1121 is_specialization_of (tree decl
, tree tmpl
)
1125 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1129 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1135 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1137 for (t
= TREE_TYPE (decl
);
1139 t
= CLASSTYPE_USE_TEMPLATE (t
)
1140 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1141 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1148 /* Returns nonzero iff DECL is a specialization of friend declaration
1149 FRIEND_DECL according to [temp.friend]. */
1152 is_specialization_of_friend (tree decl
, tree friend_decl
)
1154 bool need_template
= true;
1157 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1158 || TREE_CODE (decl
) == TYPE_DECL
);
1160 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1161 of a template class, we want to check if DECL is a specialization
1163 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1164 && DECL_TEMPLATE_INFO (friend_decl
)
1165 && !DECL_USE_TEMPLATE (friend_decl
))
1167 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1168 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1169 need_template
= false;
1171 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1172 && !PRIMARY_TEMPLATE_P (friend_decl
))
1173 need_template
= false;
1175 /* There is nothing to do if this is not a template friend. */
1176 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1179 if (is_specialization_of (decl
, friend_decl
))
1183 A member of a class template may be declared to be a friend of a
1184 non-template class. In this case, the corresponding member of
1185 every specialization of the class template is a friend of the
1186 class granting friendship.
1188 For example, given a template friend declaration
1190 template <class T> friend void A<T>::f();
1192 the member function below is considered a friend
1194 template <> struct A<int> {
1198 For this type of template friend, TEMPLATE_DEPTH below will be
1199 nonzero. To determine if DECL is a friend of FRIEND, we first
1200 check if the enclosing class is a specialization of another. */
1202 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1204 && DECL_CLASS_SCOPE_P (decl
)
1205 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1206 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1208 /* Next, we check the members themselves. In order to handle
1209 a few tricky cases, such as when FRIEND_DECL's are
1211 template <class T> friend void A<T>::g(T t);
1212 template <class T> template <T t> friend void A<T>::h();
1216 void A<int>::g(int);
1217 template <int> void A<int>::h();
1219 we need to figure out ARGS, the template arguments from
1220 the context of DECL. This is required for template substitution
1221 of `T' in the function parameter of `g' and template parameter
1222 of `h' in the above examples. Here ARGS corresponds to `int'. */
1224 tree context
= DECL_CONTEXT (decl
);
1225 tree args
= NULL_TREE
;
1226 int current_depth
= 0;
1228 while (current_depth
< template_depth
)
1230 if (CLASSTYPE_TEMPLATE_INFO (context
))
1232 if (current_depth
== 0)
1233 args
= TYPE_TI_ARGS (context
);
1235 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1238 context
= TYPE_CONTEXT (context
);
1241 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1246 tree friend_args_type
;
1247 tree decl_args_type
;
1249 /* Make sure that both DECL and FRIEND_DECL are templates or
1251 is_template
= DECL_TEMPLATE_INFO (decl
)
1252 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1253 if (need_template
^ is_template
)
1255 else if (is_template
)
1257 /* If both are templates, check template parameter list. */
1259 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1261 if (!comp_template_parms
1262 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1266 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1269 decl_type
= TREE_TYPE (decl
);
1271 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1272 tf_none
, NULL_TREE
);
1273 if (friend_type
== error_mark_node
)
1276 /* Check if return types match. */
1277 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1280 /* Check if function parameter types match, ignoring the
1281 `this' parameter. */
1282 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1283 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1284 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1285 friend_args_type
= TREE_CHAIN (friend_args_type
);
1286 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1287 decl_args_type
= TREE_CHAIN (decl_args_type
);
1289 return compparms (decl_args_type
, friend_args_type
);
1293 /* DECL is a TYPE_DECL */
1295 tree decl_type
= TREE_TYPE (decl
);
1297 /* Make sure that both DECL and FRIEND_DECL are templates or
1300 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1301 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1303 if (need_template
^ is_template
)
1305 else if (is_template
)
1308 /* If both are templates, check the name of the two
1309 TEMPLATE_DECL's first because is_friend didn't. */
1310 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1311 != DECL_NAME (friend_decl
))
1314 /* Now check template parameter list. */
1316 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1318 return comp_template_parms
1319 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1323 return (DECL_NAME (decl
)
1324 == DECL_NAME (friend_decl
));
1330 /* Register the specialization SPEC as a specialization of TMPL with
1331 the indicated ARGS. IS_FRIEND indicates whether the specialization
1332 is actually just a friend declaration. Returns SPEC, or an
1333 equivalent prior declaration, if available.
1335 We also store instantiations of field packs in the hash table, even
1336 though they are not themselves templates, to make lookup easier. */
1339 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1346 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1347 || (TREE_CODE (tmpl
) == FIELD_DECL
1348 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1350 if (TREE_CODE (spec
) == FUNCTION_DECL
1351 && uses_template_parms (DECL_TI_ARGS (spec
)))
1352 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1353 register it; we want the corresponding TEMPLATE_DECL instead.
1354 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1355 the more obvious `uses_template_parms (spec)' to avoid problems
1356 with default function arguments. In particular, given
1357 something like this:
1359 template <class T> void f(T t1, T t = T())
1361 the default argument expression is not substituted for in an
1362 instantiation unless and until it is actually needed. */
1365 if (optimize_specialization_lookup_p (tmpl
))
1366 /* We don't put these specializations in the hash table, but we might
1367 want to give an error about a mismatch. */
1368 fn
= retrieve_specialization (tmpl
, args
, 0);
1376 hash
= hash_specialization (&elt
);
1379 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1381 fn
= ((spec_entry
*) *slot
)->spec
;
1386 /* We can sometimes try to re-register a specialization that we've
1387 already got. In particular, regenerate_decl_from_template calls
1388 duplicate_decls which will update the specialization list. But,
1389 we'll still get called again here anyhow. It's more convenient
1390 to simply allow this than to try to prevent it. */
1393 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1395 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1397 if (DECL_ODR_USED (fn
)
1398 || DECL_EXPLICIT_INSTANTIATION (fn
))
1400 error ("specialization of %qD after instantiation",
1402 return error_mark_node
;
1407 /* This situation should occur only if the first
1408 specialization is an implicit instantiation, the
1409 second is an explicit specialization, and the
1410 implicit instantiation has not yet been used. That
1411 situation can occur if we have implicitly
1412 instantiated a member function and then specialized
1415 We can also wind up here if a friend declaration that
1416 looked like an instantiation turns out to be a
1419 template <class T> void foo(T);
1420 class S { friend void foo<>(int) };
1421 template <> void foo(int);
1423 We transform the existing DECL in place so that any
1424 pointers to it become pointers to the updated
1427 If there was a definition for the template, but not
1428 for the specialization, we want this to look as if
1429 there were no definition, and vice versa. */
1430 DECL_INITIAL (fn
) = NULL_TREE
;
1431 duplicate_decls (spec
, fn
, is_friend
);
1432 /* The call to duplicate_decls will have applied
1435 An explicit specialization of a function template
1436 is inline only if it is explicitly declared to be,
1437 and independently of whether its function template
1440 to the primary function; now copy the inline bits to
1441 the various clones. */
1442 FOR_EACH_CLONE (clone
, fn
)
1444 DECL_DECLARED_INLINE_P (clone
)
1445 = DECL_DECLARED_INLINE_P (fn
);
1446 DECL_SOURCE_LOCATION (clone
)
1447 = DECL_SOURCE_LOCATION (fn
);
1449 check_specialization_namespace (tmpl
);
1454 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1456 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1457 /* Dup decl failed, but this is a new definition. Set the
1458 line number so any errors match this new
1460 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1466 return duplicate_decls (spec
, fn
, is_friend
);
1468 /* A specialization must be declared in the same namespace as the
1469 template it is specializing. */
1470 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1471 && !check_specialization_namespace (tmpl
))
1472 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1474 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1476 spec_entry
*entry
= ggc_alloc_spec_entry ();
1477 gcc_assert (tmpl
&& args
&& spec
);
1480 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1481 && PRIMARY_TEMPLATE_P (tmpl
)
1482 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1483 /* TMPL is a forward declaration of a template function; keep a list
1484 of all specializations in case we need to reassign them to a friend
1485 template later in tsubst_friend_function. */
1486 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1487 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1493 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1494 TMPL and ARGS members, ignores SPEC. */
1496 int comparing_specializations
;
1499 eq_specializations (const void *p1
, const void *p2
)
1501 const spec_entry
*e1
= (const spec_entry
*)p1
;
1502 const spec_entry
*e2
= (const spec_entry
*)p2
;
1505 ++comparing_specializations
;
1506 equal
= (e1
->tmpl
== e2
->tmpl
1507 && comp_template_args (e1
->args
, e2
->args
));
1508 --comparing_specializations
;
1513 /* Returns a hash for a template TMPL and template arguments ARGS. */
1516 hash_tmpl_and_args (tree tmpl
, tree args
)
1518 hashval_t val
= DECL_UID (tmpl
);
1519 return iterative_hash_template_arg (args
, val
);
1522 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1526 hash_specialization (const void *p
)
1528 const spec_entry
*e
= (const spec_entry
*)p
;
1529 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1532 /* Recursively calculate a hash value for a template argument ARG, for use
1533 in the hash tables of template specializations. */
1536 iterative_hash_template_arg (tree arg
, hashval_t val
)
1538 unsigned HOST_WIDE_INT i
;
1539 enum tree_code code
;
1542 if (arg
== NULL_TREE
)
1543 return iterative_hash_object (arg
, val
);
1548 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1549 /* We can get one of these when re-hashing a previous entry in the middle
1550 of substituting into a pack expansion. Just look through it. */
1551 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1553 code
= TREE_CODE (arg
);
1554 tclass
= TREE_CODE_CLASS (code
);
1556 val
= iterative_hash_object (code
, val
);
1563 case IDENTIFIER_NODE
:
1564 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1568 int i
, len
= TREE_VEC_LENGTH (arg
);
1569 for (i
= 0; i
< len
; ++i
)
1570 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1574 case TYPE_PACK_EXPANSION
:
1575 case EXPR_PACK_EXPANSION
:
1576 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1577 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1579 case TYPE_ARGUMENT_PACK
:
1580 case NONTYPE_ARGUMENT_PACK
:
1581 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1584 for (; arg
; arg
= TREE_CHAIN (arg
))
1585 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1589 for (; arg
; arg
= OVL_NEXT (arg
))
1590 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1596 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1598 val
= iterative_hash_template_arg (field
, val
);
1599 val
= iterative_hash_template_arg (value
, val
);
1605 if (!DECL_ARTIFICIAL (arg
))
1607 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1608 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1610 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1613 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1616 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1617 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1619 case TEMPLATE_PARM_INDEX
:
1620 val
= iterative_hash_template_arg
1621 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1622 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1623 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1626 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1627 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1628 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1631 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1633 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1637 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1638 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1639 val
= iterative_hash_object (code
, val
);
1640 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1643 /* A lambda can't appear in a template arg, but don't crash on
1645 gcc_assert (seen_error ());
1649 case IMPLICIT_CONV_EXPR
:
1650 case STATIC_CAST_EXPR
:
1651 case REINTERPRET_CAST_EXPR
:
1652 case CONST_CAST_EXPR
:
1653 case DYNAMIC_CAST_EXPR
:
1655 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1656 /* Now hash operands as usual. */
1666 if (TYPE_CANONICAL (arg
))
1667 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1669 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1670 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1671 /* Otherwise just compare the types during lookup. */
1674 case tcc_declaration
:
1676 return iterative_hash_expr (arg
, val
);
1679 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1681 unsigned n
= cp_tree_operand_length (arg
);
1682 for (i
= 0; i
< n
; ++i
)
1683 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1691 /* Unregister the specialization SPEC as a specialization of TMPL.
1692 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1693 if the SPEC was listed as a specialization of TMPL.
1695 Note that SPEC has been ggc_freed, so we can't look inside it. */
1698 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1703 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1704 elt
.args
= TI_ARGS (tinfo
);
1705 elt
.spec
= NULL_TREE
;
1707 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1710 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1711 gcc_assert (new_spec
!= NULL_TREE
);
1712 entry
->spec
= new_spec
;
1719 /* Like register_specialization, but for local declarations. We are
1720 registering SPEC, an instantiation of TMPL. */
1723 register_local_specialization (tree spec
, tree tmpl
)
1727 slot
= pointer_map_insert (local_specializations
, tmpl
);
1731 /* TYPE is a class type. Returns true if TYPE is an explicitly
1732 specialized class. */
1735 explicit_class_specialization_p (tree type
)
1737 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1739 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1742 /* Print the list of functions at FNS, going through all the overloads
1743 for each element of the list. Alternatively, FNS can not be a
1744 TREE_LIST, in which case it will be printed together with all the
1747 MORE and *STR should respectively be FALSE and NULL when the function
1748 is called from the outside. They are used internally on recursive
1749 calls. print_candidates manages the two parameters and leaves NULL
1750 in *STR when it ends. */
1753 print_candidates_1 (tree fns
, bool more
, const char **str
)
1756 char *spaces
= NULL
;
1758 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1759 if (TREE_CODE (fn
) == TREE_LIST
)
1761 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1762 print_candidates_1 (TREE_VALUE (fn2
),
1763 TREE_CHAIN (fn2
) || more
, str
);
1767 tree cand
= OVL_CURRENT (fn
);
1770 /* Pick the prefix string. */
1771 if (!more
&& !OVL_NEXT (fns
))
1773 inform (DECL_SOURCE_LOCATION (cand
),
1774 "candidate is: %#D", cand
);
1778 *str
= _("candidates are:");
1779 spaces
= get_spaces (*str
);
1781 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1782 *str
= spaces
? spaces
: *str
;
1792 /* Print the list of candidate FNS in an error message. FNS can also
1793 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1796 print_candidates (tree fns
)
1798 const char *str
= NULL
;
1799 print_candidates_1 (fns
, false, &str
);
1800 gcc_assert (str
== NULL
);
1803 /* Returns the template (one of the functions given by TEMPLATE_ID)
1804 which can be specialized to match the indicated DECL with the
1805 explicit template args given in TEMPLATE_ID. The DECL may be
1806 NULL_TREE if none is available. In that case, the functions in
1807 TEMPLATE_ID are non-members.
1809 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1810 specialization of a member template.
1812 The TEMPLATE_COUNT is the number of references to qualifying
1813 template classes that appeared in the name of the function. See
1814 check_explicit_specialization for a more accurate description.
1816 TSK indicates what kind of template declaration (if any) is being
1817 declared. TSK_TEMPLATE indicates that the declaration given by
1818 DECL, though a FUNCTION_DECL, has template parameters, and is
1819 therefore a template function.
1821 The template args (those explicitly specified and those deduced)
1822 are output in a newly created vector *TARGS_OUT.
1824 If it is impossible to determine the result, an error message is
1825 issued. The error_mark_node is returned to indicate failure. */
1828 determine_specialization (tree template_id
,
1831 int need_member_template
,
1837 tree explicit_targs
;
1838 tree candidates
= NULL_TREE
;
1839 /* A TREE_LIST of templates of which DECL may be a specialization.
1840 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1841 corresponding TREE_PURPOSE is the set of template arguments that,
1842 when used to instantiate the template, would produce a function
1843 with the signature of DECL. */
1844 tree templates
= NULL_TREE
;
1846 cp_binding_level
*b
;
1848 *targs_out
= NULL_TREE
;
1850 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1851 return error_mark_node
;
1853 /* We shouldn't be specializing a member template of an
1854 unspecialized class template; we already gave an error in
1855 check_specialization_scope, now avoid crashing. */
1856 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1857 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1859 gcc_assert (errorcount
);
1860 return error_mark_node
;
1863 fns
= TREE_OPERAND (template_id
, 0);
1864 explicit_targs
= TREE_OPERAND (template_id
, 1);
1866 if (fns
== error_mark_node
)
1867 return error_mark_node
;
1869 /* Check for baselinks. */
1870 if (BASELINK_P (fns
))
1871 fns
= BASELINK_FUNCTIONS (fns
);
1873 if (!is_overloaded_fn (fns
))
1875 error ("%qD is not a function template", fns
);
1876 return error_mark_node
;
1879 /* Count the number of template headers specified for this
1882 for (b
= current_binding_level
;
1883 b
->kind
== sk_template_parms
;
1887 for (; fns
; fns
= OVL_NEXT (fns
))
1889 tree fn
= OVL_CURRENT (fns
);
1891 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1893 tree decl_arg_types
;
1897 /* In case of explicit specialization, we need to check if
1898 the number of template headers appearing in the specialization
1899 is correct. This is usually done in check_explicit_specialization,
1900 but the check done there cannot be exhaustive when specializing
1901 member functions. Consider the following code:
1903 template <> void A<int>::f(int);
1904 template <> template <> void A<int>::f(int);
1906 Assuming that A<int> is not itself an explicit specialization
1907 already, the first line specializes "f" which is a non-template
1908 member function, whilst the second line specializes "f" which
1909 is a template member function. So both lines are syntactically
1910 correct, and check_explicit_specialization does not reject
1913 Here, we can do better, as we are matching the specialization
1914 against the declarations. We count the number of template
1915 headers, and we check if they match TEMPLATE_COUNT + 1
1916 (TEMPLATE_COUNT is the number of qualifying template classes,
1917 plus there must be another header for the member template
1920 Notice that if header_count is zero, this is not a
1921 specialization but rather a template instantiation, so there
1922 is no check we can perform here. */
1923 if (header_count
&& header_count
!= template_count
+ 1)
1926 /* Check that the number of template arguments at the
1927 innermost level for DECL is the same as for FN. */
1928 if (current_binding_level
->kind
== sk_template_parms
1929 && !current_binding_level
->explicit_spec_p
1930 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1931 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1932 (current_template_parms
))))
1935 /* DECL might be a specialization of FN. */
1936 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1937 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1939 /* For a non-static member function, we need to make sure
1940 that the const qualification is the same. Since
1941 get_bindings does not try to merge the "this" parameter,
1942 we must do the comparison explicitly. */
1943 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1944 && !same_type_p (TREE_VALUE (fn_arg_types
),
1945 TREE_VALUE (decl_arg_types
)))
1948 /* Skip the "this" parameter and, for constructors of
1949 classes with virtual bases, the VTT parameter. A
1950 full specialization of a constructor will have a VTT
1951 parameter, but a template never will. */
1953 = skip_artificial_parms_for (decl
, decl_arg_types
);
1955 = skip_artificial_parms_for (fn
, fn_arg_types
);
1957 /* Function templates cannot be specializations; there are
1958 no partial specializations of functions. Therefore, if
1959 the type of DECL does not match FN, there is no
1961 if (tsk
== tsk_template
)
1963 if (compparms (fn_arg_types
, decl_arg_types
))
1964 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1968 /* See whether this function might be a specialization of this
1969 template. Suppress access control because we might be trying
1970 to make this specialization a friend, and we have already done
1971 access control for the declaration of the specialization. */
1972 push_deferring_access_checks (dk_no_check
);
1973 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1974 pop_deferring_access_checks ();
1977 /* We cannot deduce template arguments that when used to
1978 specialize TMPL will produce DECL. */
1981 /* Make sure that the deduced arguments actually work. */
1982 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1983 if (insttype
== error_mark_node
)
1986 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1987 if (!compparms (fn_arg_types
, decl_arg_types
))
1990 /* Save this template, and the arguments deduced. */
1991 templates
= tree_cons (targs
, fn
, templates
);
1993 else if (need_member_template
)
1994 /* FN is an ordinary member function, and we need a
1995 specialization of a member template. */
1997 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1998 /* We can get IDENTIFIER_NODEs here in certain erroneous
2001 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2002 /* This is just an ordinary non-member function. Nothing can
2003 be a specialization of that. */
2005 else if (DECL_ARTIFICIAL (fn
))
2006 /* Cannot specialize functions that are created implicitly. */
2010 tree decl_arg_types
;
2012 /* This is an ordinary member function. However, since
2013 we're here, we can assume its enclosing class is a
2014 template class. For example,
2016 template <typename T> struct S { void f(); };
2017 template <> void S<int>::f() {}
2019 Here, S<int>::f is a non-template, but S<int> is a
2020 template class. If FN has the same type as DECL, we
2021 might be in business. */
2023 if (!DECL_TEMPLATE_INFO (fn
))
2024 /* Its enclosing class is an explicit specialization
2025 of a template class. This is not a candidate. */
2028 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2029 TREE_TYPE (TREE_TYPE (fn
))))
2030 /* The return types differ. */
2033 /* Adjust the type of DECL in case FN is a static member. */
2034 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2035 if (DECL_STATIC_FUNCTION_P (fn
)
2036 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2037 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2039 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2042 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2046 if (templates
&& TREE_CHAIN (templates
))
2052 It is possible for a specialization with a given function
2053 signature to be instantiated from more than one function
2054 template. In such cases, explicit specification of the
2055 template arguments must be used to uniquely identify the
2056 function template specialization being specialized.
2058 Note that here, there's no suggestion that we're supposed to
2059 determine which of the candidate templates is most
2060 specialized. However, we, also have:
2064 Partial ordering of overloaded function template
2065 declarations is used in the following contexts to select
2066 the function template to which a function template
2067 specialization refers:
2069 -- when an explicit specialization refers to a function
2072 So, we do use the partial ordering rules, at least for now.
2073 This extension can only serve to make invalid programs valid,
2074 so it's safe. And, there is strong anecdotal evidence that
2075 the committee intended the partial ordering rules to apply;
2076 the EDG front end has that behavior, and John Spicer claims
2077 that the committee simply forgot to delete the wording in
2078 [temp.expl.spec]. */
2079 tree tmpl
= most_specialized_instantiation (templates
);
2080 if (tmpl
!= error_mark_node
)
2083 TREE_CHAIN (templates
) = NULL_TREE
;
2087 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2089 error ("template-id %qD for %q+D does not match any template "
2090 "declaration", template_id
, decl
);
2091 if (header_count
&& header_count
!= template_count
+ 1)
2092 inform (input_location
, "saw %d %<template<>%>, need %d for "
2093 "specializing a member function template",
2094 header_count
, template_count
+ 1);
2095 return error_mark_node
;
2097 else if ((templates
&& TREE_CHAIN (templates
))
2098 || (candidates
&& TREE_CHAIN (candidates
))
2099 || (templates
&& candidates
))
2101 error ("ambiguous template specialization %qD for %q+D",
2103 candidates
= chainon (candidates
, templates
);
2104 print_candidates (candidates
);
2105 return error_mark_node
;
2108 /* We have one, and exactly one, match. */
2111 tree fn
= TREE_VALUE (candidates
);
2112 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2113 /* DECL is a re-declaration or partial instantiation of a template
2115 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2117 /* It was a specialization of an ordinary member function in a
2119 return DECL_TI_TEMPLATE (fn
);
2122 /* It was a specialization of a template. */
2123 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2124 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2126 *targs_out
= copy_node (targs
);
2127 SET_TMPL_ARGS_LEVEL (*targs_out
,
2128 TMPL_ARGS_DEPTH (*targs_out
),
2129 TREE_PURPOSE (templates
));
2132 *targs_out
= TREE_PURPOSE (templates
);
2133 return TREE_VALUE (templates
);
2136 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2137 but with the default argument values filled in from those in the
2141 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2144 tree new_spec_types
;
2149 if (spec_types
== void_list_node
)
2150 return void_list_node
;
2152 /* Substitute into the rest of the list. */
2154 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2155 TREE_CHAIN (tmpl_types
));
2157 /* Add the default argument for this parameter. */
2158 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2159 TREE_VALUE (spec_types
),
2163 /* DECL is an explicit specialization. Replicate default arguments
2164 from the template it specializes. (That way, code like:
2166 template <class T> void f(T = 3);
2167 template <> void f(double);
2170 works, as required.) An alternative approach would be to look up
2171 the correct default arguments at the call-site, but this approach
2172 is consistent with how implicit instantiations are handled. */
2175 copy_default_args_to_explicit_spec (tree decl
)
2180 tree new_spec_types
;
2184 tree object_type
= NULL_TREE
;
2185 tree in_charge
= NULL_TREE
;
2186 tree vtt
= NULL_TREE
;
2188 /* See if there's anything we need to do. */
2189 tmpl
= DECL_TI_TEMPLATE (decl
);
2190 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2191 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2192 if (TREE_PURPOSE (t
))
2197 old_type
= TREE_TYPE (decl
);
2198 spec_types
= TYPE_ARG_TYPES (old_type
);
2200 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2202 /* Remove the this pointer, but remember the object's type for
2204 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2205 spec_types
= TREE_CHAIN (spec_types
);
2206 tmpl_types
= TREE_CHAIN (tmpl_types
);
2208 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2210 /* DECL may contain more parameters than TMPL due to the extra
2211 in-charge parameter in constructors and destructors. */
2212 in_charge
= spec_types
;
2213 spec_types
= TREE_CHAIN (spec_types
);
2215 if (DECL_HAS_VTT_PARM_P (decl
))
2218 spec_types
= TREE_CHAIN (spec_types
);
2222 /* Compute the merged default arguments. */
2224 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2226 /* Compute the new FUNCTION_TYPE. */
2230 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2235 /* Put the in-charge parameter back. */
2236 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2237 TREE_VALUE (in_charge
),
2240 new_type
= build_method_type_directly (object_type
,
2241 TREE_TYPE (old_type
),
2245 new_type
= build_function_type (TREE_TYPE (old_type
),
2247 new_type
= cp_build_type_attribute_variant (new_type
,
2248 TYPE_ATTRIBUTES (old_type
));
2249 new_type
= build_exception_variant (new_type
,
2250 TYPE_RAISES_EXCEPTIONS (old_type
));
2251 TREE_TYPE (decl
) = new_type
;
2254 /* Return the number of template headers we expect to see for a definition
2255 or specialization of CTYPE or one of its non-template members. */
2258 num_template_headers_for_class (tree ctype
)
2260 int num_templates
= 0;
2262 while (ctype
&& CLASS_TYPE_P (ctype
))
2264 /* You're supposed to have one `template <...>' for every
2265 template class, but you don't need one for a full
2266 specialization. For example:
2268 template <class T> struct S{};
2269 template <> struct S<int> { void f(); };
2270 void S<int>::f () {}
2272 is correct; there shouldn't be a `template <>' for the
2273 definition of `S<int>::f'. */
2274 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2275 /* If CTYPE does not have template information of any
2276 kind, then it is not a template, nor is it nested
2277 within a template. */
2279 if (explicit_class_specialization_p (ctype
))
2281 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2284 ctype
= TYPE_CONTEXT (ctype
);
2287 return num_templates
;
2290 /* Do a simple sanity check on the template headers that precede the
2291 variable declaration DECL. */
2294 check_template_variable (tree decl
)
2296 tree ctx
= CP_DECL_CONTEXT (decl
);
2297 int wanted
= num_template_headers_for_class (ctx
);
2298 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2299 permerror (DECL_SOURCE_LOCATION (decl
),
2300 "%qD is not a static data member of a class template", decl
);
2301 else if (template_header_count
> wanted
)
2303 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2304 "too many template headers for %D (should be %d)",
2306 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2307 inform (DECL_SOURCE_LOCATION (decl
),
2308 "members of an explicitly specialized class are defined "
2309 "without a template header");
2313 /* Check to see if the function just declared, as indicated in
2314 DECLARATOR, and in DECL, is a specialization of a function
2315 template. We may also discover that the declaration is an explicit
2316 instantiation at this point.
2318 Returns DECL, or an equivalent declaration that should be used
2319 instead if all goes well. Issues an error message if something is
2320 amiss. Returns error_mark_node if the error is not easily
2323 FLAGS is a bitmask consisting of the following flags:
2325 2: The function has a definition.
2326 4: The function is a friend.
2328 The TEMPLATE_COUNT is the number of references to qualifying
2329 template classes that appeared in the name of the function. For
2332 template <class T> struct S { void f(); };
2335 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2336 classes are not counted in the TEMPLATE_COUNT, so that in
2338 template <class T> struct S {};
2339 template <> struct S<int> { void f(); }
2340 template <> void S<int>::f();
2342 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2343 invalid; there should be no template <>.)
2345 If the function is a specialization, it is marked as such via
2346 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2347 is set up correctly, and it is added to the list of specializations
2348 for that template. */
2351 check_explicit_specialization (tree declarator
,
2356 int have_def
= flags
& 2;
2357 int is_friend
= flags
& 4;
2358 int specialization
= 0;
2359 int explicit_instantiation
= 0;
2360 int member_specialization
= 0;
2361 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2362 tree dname
= DECL_NAME (decl
);
2367 if (!processing_specialization
)
2370 tsk
= tsk_excessive_parms
;
2373 tsk
= current_tmpl_spec_kind (template_count
);
2378 if (processing_specialization
)
2381 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2383 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2386 /* This could be something like:
2388 template <class T> void f(T);
2389 class S { friend void f<>(int); } */
2393 /* This case handles bogus declarations like template <>
2394 template <class T> void f<int>(); */
2396 error ("template-id %qD in declaration of primary template",
2403 case tsk_invalid_member_spec
:
2404 /* The error has already been reported in
2405 check_specialization_scope. */
2406 return error_mark_node
;
2408 case tsk_invalid_expl_inst
:
2409 error ("template parameter list used in explicit instantiation");
2415 error ("definition provided for explicit instantiation");
2417 explicit_instantiation
= 1;
2420 case tsk_excessive_parms
:
2421 case tsk_insufficient_parms
:
2422 if (tsk
== tsk_excessive_parms
)
2423 error ("too many template parameter lists in declaration of %qD",
2425 else if (template_header_count
)
2426 error("too few template parameter lists in declaration of %qD", decl
);
2428 error("explicit specialization of %qD must be introduced by "
2429 "%<template <>%>", decl
);
2433 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2435 member_specialization
= 1;
2441 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2443 /* This case handles bogus declarations like template <>
2444 template <class T> void f<int>(); */
2446 if (uses_template_parms (declarator
))
2447 error ("function template partial specialization %qD "
2448 "is not allowed", declarator
);
2450 error ("template-id %qD in declaration of primary template",
2455 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2456 /* This is a specialization of a member template, without
2457 specialization the containing class. Something like:
2459 template <class T> struct S {
2460 template <class U> void f (U);
2462 template <> template <class U> void S<int>::f(U) {}
2464 That's a specialization -- but of the entire template. */
2472 if (specialization
|| member_specialization
)
2474 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2475 for (; t
; t
= TREE_CHAIN (t
))
2476 if (TREE_PURPOSE (t
))
2478 permerror (input_location
,
2479 "default argument specified in explicit specialization");
2484 if (specialization
|| member_specialization
|| explicit_instantiation
)
2486 tree tmpl
= NULL_TREE
;
2487 tree targs
= NULL_TREE
;
2489 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2490 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2494 gcc_assert (identifier_p (declarator
));
2499 /* If there is no class context, the explicit instantiation
2500 must be at namespace scope. */
2501 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2503 /* Find the namespace binding, using the declaration
2505 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2507 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2509 error ("%qD is not a template function", dname
);
2510 fns
= error_mark_node
;
2514 tree fn
= OVL_CURRENT (fns
);
2515 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2516 CP_DECL_CONTEXT (fn
)))
2517 error ("%qD is not declared in %qD",
2518 decl
, current_namespace
);
2522 declarator
= lookup_template_function (fns
, NULL_TREE
);
2525 if (declarator
== error_mark_node
)
2526 return error_mark_node
;
2528 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2530 if (!explicit_instantiation
)
2531 /* A specialization in class scope. This is invalid,
2532 but the error will already have been flagged by
2533 check_specialization_scope. */
2534 return error_mark_node
;
2537 /* It's not valid to write an explicit instantiation in
2540 class C { template void f(); }
2542 This case is caught by the parser. However, on
2545 template class C { void f(); };
2547 (which is invalid) we can get here. The error will be
2554 else if (ctype
!= NULL_TREE
2555 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2557 /* Find the list of functions in ctype that have the same
2558 name as the declared function. */
2559 tree name
= TREE_OPERAND (declarator
, 0);
2560 tree fns
= NULL_TREE
;
2563 if (constructor_name_p (name
, ctype
))
2565 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2567 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2568 : !CLASSTYPE_DESTRUCTORS (ctype
))
2570 /* From [temp.expl.spec]:
2572 If such an explicit specialization for the member
2573 of a class template names an implicitly-declared
2574 special member function (clause _special_), the
2575 program is ill-formed.
2577 Similar language is found in [temp.explicit]. */
2578 error ("specialization of implicitly-declared special member function");
2579 return error_mark_node
;
2582 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2585 if (!DECL_CONV_FN_P (decl
))
2587 idx
= lookup_fnfields_1 (ctype
, name
);
2589 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2593 vec
<tree
, va_gc
> *methods
;
2596 /* For a type-conversion operator, we cannot do a
2597 name-based lookup. We might be looking for `operator
2598 int' which will be a specialization of `operator T'.
2599 So, we find *all* the conversion operators, and then
2600 select from them. */
2603 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2605 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2606 methods
->iterate (idx
, &ovl
);
2609 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2610 /* There are no more conversion functions. */
2613 /* Glue all these conversion functions together
2614 with those we already have. */
2615 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2616 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2620 if (fns
== NULL_TREE
)
2622 error ("no member function %qD declared in %qT", name
, ctype
);
2623 return error_mark_node
;
2626 TREE_OPERAND (declarator
, 0) = fns
;
2629 /* Figure out what exactly is being specialized at this point.
2630 Note that for an explicit instantiation, even one for a
2631 member function, we cannot tell apriori whether the
2632 instantiation is for a member template, or just a member
2633 function of a template class. Even if a member template is
2634 being instantiated, the member template arguments may be
2635 elided if they can be deduced from the rest of the
2637 tmpl
= determine_specialization (declarator
, decl
,
2639 member_specialization
,
2643 if (!tmpl
|| tmpl
== error_mark_node
)
2644 /* We couldn't figure out what this declaration was
2646 return error_mark_node
;
2649 tree gen_tmpl
= most_general_template (tmpl
);
2651 if (explicit_instantiation
)
2653 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2654 is done by do_decl_instantiation later. */
2656 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2657 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2659 if (arg_depth
> parm_depth
)
2661 /* If TMPL is not the most general template (for
2662 example, if TMPL is a friend template that is
2663 injected into namespace scope), then there will
2664 be too many levels of TARGS. Remove some of them
2669 new_targs
= make_tree_vec (parm_depth
);
2670 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2671 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2672 = TREE_VEC_ELT (targs
, i
);
2676 return instantiate_template (tmpl
, targs
, tf_error
);
2679 /* If we thought that the DECL was a member function, but it
2680 turns out to be specializing a static member function,
2681 make DECL a static member function as well. */
2682 if (DECL_STATIC_FUNCTION_P (tmpl
)
2683 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2684 revert_static_member_fn (decl
);
2686 /* If this is a specialization of a member template of a
2687 template class, we want to return the TEMPLATE_DECL, not
2688 the specialization of it. */
2689 if (tsk
== tsk_template
)
2691 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2692 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2693 DECL_INITIAL (result
) = NULL_TREE
;
2697 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2698 DECL_SOURCE_LOCATION (result
)
2699 = DECL_SOURCE_LOCATION (decl
);
2700 /* We want to use the argument list specified in the
2701 definition, not in the original declaration. */
2702 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2703 for (parm
= DECL_ARGUMENTS (result
); parm
;
2704 parm
= DECL_CHAIN (parm
))
2705 DECL_CONTEXT (parm
) = result
;
2707 return register_specialization (tmpl
, gen_tmpl
, targs
,
2711 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2712 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2714 /* Inherit default function arguments from the template
2715 DECL is specializing. */
2716 copy_default_args_to_explicit_spec (decl
);
2718 /* This specialization has the same protection as the
2719 template it specializes. */
2720 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2721 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2723 /* 7.1.1-1 [dcl.stc]
2725 A storage-class-specifier shall not be specified in an
2726 explicit specialization...
2728 The parser rejects these, so unless action is taken here,
2729 explicit function specializations will always appear with
2732 The action recommended by the C++ CWG in response to C++
2733 defect report 605 is to make the storage class and linkage
2734 of the explicit specialization match the templated function:
2736 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2738 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2740 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2741 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2743 /* This specialization has the same linkage and visibility as
2744 the function template it specializes. */
2745 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2746 if (! TREE_PUBLIC (decl
))
2748 DECL_INTERFACE_KNOWN (decl
) = 1;
2749 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2751 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2752 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2754 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2755 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2759 /* If DECL is a friend declaration, declared using an
2760 unqualified name, the namespace associated with DECL may
2761 have been set incorrectly. For example, in:
2763 template <typename T> void f(T);
2765 struct S { friend void f<int>(int); }
2768 we will have set the DECL_CONTEXT for the friend
2769 declaration to N, rather than to the global namespace. */
2770 if (DECL_NAMESPACE_SCOPE_P (decl
))
2771 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2773 if (is_friend
&& !have_def
)
2774 /* This is not really a declaration of a specialization.
2775 It's just the name of an instantiation. But, it's not
2776 a request for an instantiation, either. */
2777 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2778 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2779 /* This is indeed a specialization. In case of constructors
2780 and destructors, we need in-charge and not-in-charge
2781 versions in V3 ABI. */
2782 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2784 /* Register this specialization so that we can find it
2786 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2793 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2794 parameters. These are represented in the same format used for
2795 DECL_TEMPLATE_PARMS. */
2798 comp_template_parms (const_tree parms1
, const_tree parms2
)
2803 if (parms1
== parms2
)
2806 for (p1
= parms1
, p2
= parms2
;
2807 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2808 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2810 tree t1
= TREE_VALUE (p1
);
2811 tree t2
= TREE_VALUE (p2
);
2814 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2815 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2817 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2820 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2822 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2823 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2825 /* If either of the template parameters are invalid, assume
2826 they match for the sake of error recovery. */
2827 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2830 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2833 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2834 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2835 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2837 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2842 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2843 /* One set of parameters has more parameters lists than the
2850 /* Determine whether PARM is a parameter pack. */
2853 template_parameter_pack_p (const_tree parm
)
2855 /* Determine if we have a non-type template parameter pack. */
2856 if (TREE_CODE (parm
) == PARM_DECL
)
2857 return (DECL_TEMPLATE_PARM_P (parm
)
2858 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2859 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2860 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2862 /* If this is a list of template parameters, we could get a
2863 TYPE_DECL or a TEMPLATE_DECL. */
2864 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2865 parm
= TREE_TYPE (parm
);
2867 /* Otherwise it must be a type template parameter. */
2868 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2869 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2870 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2873 /* Determine if T is a function parameter pack. */
2876 function_parameter_pack_p (const_tree t
)
2878 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2879 return DECL_PACK_P (t
);
2883 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2884 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2887 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2889 if (! primary_func_tmpl_inst
2890 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2891 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2894 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2897 /* Return true iff the function parameter PARAM_DECL was expanded
2898 from the function parameter pack PACK. */
2901 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2903 if (DECL_ARTIFICIAL (param_decl
)
2904 || !function_parameter_pack_p (pack
))
2907 /* The parameter pack and its pack arguments have the same
2909 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2912 /* Determine whether ARGS describes a variadic template args list,
2913 i.e., one that is terminated by a template argument pack. */
2916 template_args_variadic_p (tree args
)
2921 if (args
== NULL_TREE
)
2924 args
= INNERMOST_TEMPLATE_ARGS (args
);
2925 nargs
= TREE_VEC_LENGTH (args
);
2930 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2932 return ARGUMENT_PACK_P (last_parm
);
2935 /* Generate a new name for the parameter pack name NAME (an
2936 IDENTIFIER_NODE) that incorporates its */
2939 make_ith_pack_parameter_name (tree name
, int i
)
2941 /* Munge the name to include the parameter index. */
2942 #define NUMBUF_LEN 128
2943 char numbuf
[NUMBUF_LEN
];
2947 if (name
== NULL_TREE
)
2949 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2950 newname_len
= IDENTIFIER_LENGTH (name
)
2951 + strlen (numbuf
) + 2;
2952 newname
= (char*)alloca (newname_len
);
2953 snprintf (newname
, newname_len
,
2954 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2955 return get_identifier (newname
);
2958 /* Return true if T is a primary function, class or alias template
2962 primary_template_instantiation_p (const_tree t
)
2967 if (TREE_CODE (t
) == FUNCTION_DECL
)
2968 return DECL_LANG_SPECIFIC (t
)
2969 && DECL_TEMPLATE_INSTANTIATION (t
)
2970 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2971 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2972 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2973 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2974 else if (alias_template_specialization_p (t
))
2979 /* Return true if PARM is a template template parameter. */
2982 template_template_parameter_p (const_tree parm
)
2984 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2987 /* Return true iff PARM is a DECL representing a type template
2991 template_type_parameter_p (const_tree parm
)
2994 && (TREE_CODE (parm
) == TYPE_DECL
2995 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2996 && DECL_TEMPLATE_PARM_P (parm
));
2999 /* Return the template parameters of T if T is a
3000 primary template instantiation, NULL otherwise. */
3003 get_primary_template_innermost_parameters (const_tree t
)
3005 tree parms
= NULL
, template_info
= NULL
;
3007 if ((template_info
= get_template_info (t
))
3008 && primary_template_instantiation_p (t
))
3009 parms
= INNERMOST_TEMPLATE_PARMS
3010 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3015 /* Return the template parameters of the LEVELth level from the full list
3016 of template parameters PARMS. */
3019 get_template_parms_at_level (tree parms
, int level
)
3023 || TREE_CODE (parms
) != TREE_LIST
3024 || level
> TMPL_PARMS_DEPTH (parms
))
3027 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3028 if (TMPL_PARMS_DEPTH (p
) == level
)
3034 /* Returns the template arguments of T if T is a template instantiation,
3038 get_template_innermost_arguments (const_tree t
)
3040 tree args
= NULL
, template_info
= NULL
;
3042 if ((template_info
= get_template_info (t
))
3043 && TI_ARGS (template_info
))
3044 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3049 /* Return the argument pack elements of T if T is a template argument pack,
3053 get_template_argument_pack_elems (const_tree t
)
3055 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3056 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3059 return ARGUMENT_PACK_ARGS (t
);
3062 /* Structure used to track the progress of find_parameter_packs_r. */
3063 struct find_parameter_pack_data
3065 /* TREE_LIST that will contain all of the parameter packs found by
3067 tree
* parameter_packs
;
3069 /* Set of AST nodes that have been visited by the traversal. */
3070 struct pointer_set_t
*visited
;
3073 /* Identifies all of the argument packs that occur in a template
3074 argument and appends them to the TREE_LIST inside DATA, which is a
3075 find_parameter_pack_data structure. This is a subroutine of
3076 make_pack_expansion and uses_parameter_packs. */
3078 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3081 struct find_parameter_pack_data
* ppd
=
3082 (struct find_parameter_pack_data
*)data
;
3083 bool parameter_pack_p
= false;
3085 /* Handle type aliases/typedefs. */
3086 if (TYPE_ALIAS_P (t
))
3088 if (TYPE_TEMPLATE_INFO (t
))
3089 cp_walk_tree (&TYPE_TI_ARGS (t
),
3090 &find_parameter_packs_r
,
3096 /* Identify whether this is a parameter pack or not. */
3097 switch (TREE_CODE (t
))
3099 case TEMPLATE_PARM_INDEX
:
3100 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3101 parameter_pack_p
= true;
3104 case TEMPLATE_TYPE_PARM
:
3105 t
= TYPE_MAIN_VARIANT (t
);
3106 case TEMPLATE_TEMPLATE_PARM
:
3107 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3108 parameter_pack_p
= true;
3113 if (DECL_PACK_P (t
))
3115 /* We don't want to walk into the type of a PARM_DECL,
3116 because we don't want to see the type parameter pack. */
3118 parameter_pack_p
= true;
3122 /* Look through a lambda capture proxy to the field pack. */
3124 if (DECL_HAS_VALUE_EXPR_P (t
))
3126 tree v
= DECL_VALUE_EXPR (t
);
3128 &find_parameter_packs_r
,
3135 parameter_pack_p
= true;
3138 /* Not a parameter pack. */
3142 if (parameter_pack_p
)
3144 /* Add this parameter pack to the list. */
3145 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3149 cp_walk_tree (&TYPE_CONTEXT (t
),
3150 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3152 /* This switch statement will return immediately if we don't find a
3154 switch (TREE_CODE (t
))
3156 case TEMPLATE_PARM_INDEX
:
3159 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3160 /* Check the template itself. */
3161 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3162 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3163 /* Check the template arguments. */
3164 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3169 case TEMPLATE_TYPE_PARM
:
3170 case TEMPLATE_TEMPLATE_PARM
:
3177 if (TYPE_PTRMEMFUNC_P (t
))
3183 if (TYPE_TEMPLATE_INFO (t
))
3184 cp_walk_tree (&TYPE_TI_ARGS (t
),
3185 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3192 cp_walk_tree (&TREE_TYPE (t
),
3193 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3197 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3202 case TYPE_PACK_EXPANSION
:
3203 case EXPR_PACK_EXPANSION
:
3208 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3213 case IDENTIFIER_NODE
:
3214 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3226 /* Determines if the expression or type T uses any parameter packs. */
3228 uses_parameter_packs (tree t
)
3230 tree parameter_packs
= NULL_TREE
;
3231 struct find_parameter_pack_data ppd
;
3232 ppd
.parameter_packs
= ¶meter_packs
;
3233 ppd
.visited
= pointer_set_create ();
3234 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3235 pointer_set_destroy (ppd
.visited
);
3236 return parameter_packs
!= NULL_TREE
;
3239 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3240 representation a base-class initializer into a parameter pack
3241 expansion. If all goes well, the resulting node will be an
3242 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3245 make_pack_expansion (tree arg
)
3248 tree parameter_packs
= NULL_TREE
;
3249 bool for_types
= false;
3250 struct find_parameter_pack_data ppd
;
3252 if (!arg
|| arg
== error_mark_node
)
3255 if (TREE_CODE (arg
) == TREE_LIST
)
3257 /* The only time we will see a TREE_LIST here is for a base
3258 class initializer. In this case, the TREE_PURPOSE will be a
3259 _TYPE node (representing the base class expansion we're
3260 initializing) and the TREE_VALUE will be a TREE_LIST
3261 containing the initialization arguments.
3263 The resulting expansion looks somewhat different from most
3264 expansions. Rather than returning just one _EXPANSION, we
3265 return a TREE_LIST whose TREE_PURPOSE is a
3266 TYPE_PACK_EXPANSION containing the bases that will be
3267 initialized. The TREE_VALUE will be identical to the
3268 original TREE_VALUE, which is a list of arguments that will
3269 be passed to each base. We do not introduce any new pack
3270 expansion nodes into the TREE_VALUE (although it is possible
3271 that some already exist), because the TREE_PURPOSE and
3272 TREE_VALUE all need to be expanded together with the same
3273 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3274 resulting TREE_PURPOSE will mention the parameter packs in
3275 both the bases and the arguments to the bases. */
3278 tree parameter_packs
= NULL_TREE
;
3280 /* Determine which parameter packs will be used by the base
3282 ppd
.visited
= pointer_set_create ();
3283 ppd
.parameter_packs
= ¶meter_packs
;
3284 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3287 if (parameter_packs
== NULL_TREE
)
3289 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3290 pointer_set_destroy (ppd
.visited
);
3291 return error_mark_node
;
3294 if (TREE_VALUE (arg
) != void_type_node
)
3296 /* Collect the sets of parameter packs used in each of the
3297 initialization arguments. */
3298 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3300 /* Determine which parameter packs will be expanded in this
3302 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3307 pointer_set_destroy (ppd
.visited
);
3309 /* Create the pack expansion type for the base type. */
3310 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3311 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3312 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3314 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3315 they will rarely be compared to anything. */
3316 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3318 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3321 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3324 /* Build the PACK_EXPANSION_* node. */
3326 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3327 : make_node (EXPR_PACK_EXPANSION
);
3328 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3329 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3331 /* Propagate type and const-expression information. */
3332 TREE_TYPE (result
) = TREE_TYPE (arg
);
3333 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3336 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3337 they will rarely be compared to anything. */
3338 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3340 /* Determine which parameter packs will be expanded. */
3341 ppd
.parameter_packs
= ¶meter_packs
;
3342 ppd
.visited
= pointer_set_create ();
3343 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3344 pointer_set_destroy (ppd
.visited
);
3346 /* Make sure we found some parameter packs. */
3347 if (parameter_packs
== NULL_TREE
)
3350 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3352 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3353 return error_mark_node
;
3355 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3357 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3362 /* Checks T for any "bare" parameter packs, which have not yet been
3363 expanded, and issues an error if any are found. This operation can
3364 only be done on full expressions or types (e.g., an expression
3365 statement, "if" condition, etc.), because we could have expressions like:
3367 foo(f(g(h(args)))...)
3369 where "args" is a parameter pack. check_for_bare_parameter_packs
3370 should not be called for the subexpressions args, h(args),
3371 g(h(args)), or f(g(h(args))), because we would produce erroneous
3374 Returns TRUE and emits an error if there were bare parameter packs,
3375 returns FALSE otherwise. */
3377 check_for_bare_parameter_packs (tree t
)
3379 tree parameter_packs
= NULL_TREE
;
3380 struct find_parameter_pack_data ppd
;
3382 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3385 if (TREE_CODE (t
) == TYPE_DECL
)
3388 ppd
.parameter_packs
= ¶meter_packs
;
3389 ppd
.visited
= pointer_set_create ();
3390 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3391 pointer_set_destroy (ppd
.visited
);
3393 if (parameter_packs
)
3395 error ("parameter packs not expanded with %<...%>:");
3396 while (parameter_packs
)
3398 tree pack
= TREE_VALUE (parameter_packs
);
3399 tree name
= NULL_TREE
;
3401 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3402 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3403 name
= TYPE_NAME (pack
);
3404 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3405 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3407 name
= DECL_NAME (pack
);
3410 inform (input_location
, " %qD", name
);
3412 inform (input_location
, " <anonymous>");
3414 parameter_packs
= TREE_CHAIN (parameter_packs
);
3423 /* Expand any parameter packs that occur in the template arguments in
3426 expand_template_argument_pack (tree args
)
3428 tree result_args
= NULL_TREE
;
3429 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3430 int num_result_args
= -1;
3431 int non_default_args_count
= -1;
3433 /* First, determine if we need to expand anything, and the number of
3434 slots we'll need. */
3435 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3437 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3438 if (arg
== NULL_TREE
)
3440 if (ARGUMENT_PACK_P (arg
))
3442 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3443 if (num_result_args
< 0)
3444 num_result_args
= in_arg
+ num_packed
;
3446 num_result_args
+= num_packed
;
3450 if (num_result_args
>= 0)
3455 /* If no expansion is necessary, we're done. */
3456 if (num_result_args
< 0)
3459 /* Expand arguments. */
3460 result_args
= make_tree_vec (num_result_args
);
3461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3462 non_default_args_count
=
3463 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3464 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3466 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3467 if (ARGUMENT_PACK_P (arg
))
3469 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3470 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3471 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3472 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3473 if (non_default_args_count
> 0)
3474 non_default_args_count
+= num_packed
;
3478 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3482 if (non_default_args_count
>= 0)
3483 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3487 /* Checks if DECL shadows a template parameter.
3489 [temp.local]: A template-parameter shall not be redeclared within its
3490 scope (including nested scopes).
3492 Emits an error and returns TRUE if the DECL shadows a parameter,
3493 returns FALSE otherwise. */
3496 check_template_shadow (tree decl
)
3500 /* If we're not in a template, we can't possibly shadow a template
3502 if (!current_template_parms
)
3505 /* Figure out what we're shadowing. */
3506 if (TREE_CODE (decl
) == OVERLOAD
)
3507 decl
= OVL_CURRENT (decl
);
3508 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3510 /* If there's no previous binding for this name, we're not shadowing
3511 anything, let alone a template parameter. */
3515 /* If we're not shadowing a template parameter, we're done. Note
3516 that OLDDECL might be an OVERLOAD (or perhaps even an
3517 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3519 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3522 /* We check for decl != olddecl to avoid bogus errors for using a
3523 name inside a class. We check TPFI to avoid duplicate errors for
3524 inline member templates. */
3526 || (DECL_TEMPLATE_PARM_P (decl
)
3527 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3530 error ("declaration of %q+#D", decl
);
3531 error (" shadows template parm %q+#D", olddecl
);
3535 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3536 ORIG_LEVEL, DECL, and TYPE. */
3539 build_template_parm_index (int index
,
3545 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3546 TEMPLATE_PARM_IDX (t
) = index
;
3547 TEMPLATE_PARM_LEVEL (t
) = level
;
3548 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3549 TEMPLATE_PARM_DECL (t
) = decl
;
3550 TREE_TYPE (t
) = type
;
3551 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3552 TREE_READONLY (t
) = TREE_READONLY (decl
);
3557 /* Find the canonical type parameter for the given template type
3558 parameter. Returns the canonical type parameter, which may be TYPE
3559 if no such parameter existed. */
3562 canonical_type_parameter (tree type
)
3565 int idx
= TEMPLATE_TYPE_IDX (type
);
3566 if (!canonical_template_parms
)
3567 vec_alloc (canonical_template_parms
, idx
+1);
3569 while (canonical_template_parms
->length () <= (unsigned)idx
)
3570 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3572 list
= (*canonical_template_parms
)[idx
];
3573 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3574 list
= TREE_CHAIN (list
);
3577 return TREE_VALUE (list
);
3580 (*canonical_template_parms
)[idx
]
3581 = tree_cons (NULL_TREE
, type
,
3582 (*canonical_template_parms
)[idx
]);
3587 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3588 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3589 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3590 new one is created. */
3593 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3594 tsubst_flags_t complain
)
3596 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3597 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3598 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3599 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3601 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3604 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3605 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3606 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3607 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3608 DECL_ARTIFICIAL (decl
) = 1;
3609 SET_DECL_TEMPLATE_PARM_P (decl
);
3611 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3612 TEMPLATE_PARM_LEVEL (index
) - levels
,
3613 TEMPLATE_PARM_ORIG_LEVEL (index
),
3615 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3616 TEMPLATE_PARM_PARAMETER_PACK (t
)
3617 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3619 /* Template template parameters need this. */
3620 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3621 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3622 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3626 return TEMPLATE_PARM_DESCENDANTS (index
);
3629 /* Process information from new template parameter PARM and append it
3630 to the LIST being built. This new parameter is a non-type
3631 parameter iff IS_NON_TYPE is true. This new parameter is a
3632 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3633 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3634 parameter list PARM belongs to. This is used used to create a
3635 proper canonical type for the type of PARM that is to be created,
3636 iff PARM is a type. If the size is not known, this parameter shall
3640 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3641 bool is_non_type
, bool is_parameter_pack
)
3648 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3649 defval
= TREE_PURPOSE (parm
);
3653 tree p
= tree_last (list
);
3655 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3658 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3659 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3661 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3671 parm
= TREE_VALUE (parm
);
3673 SET_DECL_TEMPLATE_PARM_P (parm
);
3675 if (TREE_TYPE (parm
) == error_mark_node
)
3677 err_parm_list
= build_tree_list (defval
, parm
);
3678 TREE_VALUE (err_parm_list
) = error_mark_node
;
3679 return chainon (list
, err_parm_list
);
3685 The top-level cv-qualifiers on the template-parameter are
3686 ignored when determining its type. */
3687 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3688 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3690 err_parm_list
= build_tree_list (defval
, parm
);
3691 TREE_VALUE (err_parm_list
) = error_mark_node
;
3692 return chainon (list
, err_parm_list
);
3695 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3697 /* This template parameter is not a parameter pack, but it
3698 should be. Complain about "bare" parameter packs. */
3699 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3701 /* Recover by calling this a parameter pack. */
3702 is_parameter_pack
= true;
3706 /* A template parameter is not modifiable. */
3707 TREE_CONSTANT (parm
) = 1;
3708 TREE_READONLY (parm
) = 1;
3709 decl
= build_decl (parm_loc
,
3710 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3711 TREE_CONSTANT (decl
) = 1;
3712 TREE_READONLY (decl
) = 1;
3713 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3714 = build_template_parm_index (idx
, processing_template_decl
,
3715 processing_template_decl
,
3716 decl
, TREE_TYPE (parm
));
3718 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3719 = is_parameter_pack
;
3724 parm
= TREE_VALUE (TREE_VALUE (parm
));
3726 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3728 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3729 /* This is for distinguishing between real templates and template
3730 template parameters */
3731 TREE_TYPE (parm
) = t
;
3732 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3737 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3738 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3739 decl
= build_decl (parm_loc
,
3740 TYPE_DECL
, parm
, t
);
3743 TYPE_NAME (t
) = decl
;
3744 TYPE_STUB_DECL (t
) = decl
;
3746 TEMPLATE_TYPE_PARM_INDEX (t
)
3747 = build_template_parm_index (idx
, processing_template_decl
,
3748 processing_template_decl
,
3749 decl
, TREE_TYPE (parm
));
3750 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3751 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3753 DECL_ARTIFICIAL (decl
) = 1;
3754 SET_DECL_TEMPLATE_PARM_P (decl
);
3756 parm
= build_tree_list (defval
, parm
);
3757 return chainon (list
, parm
);
3760 /* The end of a template parameter list has been reached. Process the
3761 tree list into a parameter vector, converting each parameter into a more
3762 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3766 end_template_parm_list (tree parms
)
3770 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3772 current_template_parms
3773 = tree_cons (size_int (processing_template_decl
),
3774 saved_parmlist
, current_template_parms
);
3776 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3778 next
= TREE_CHAIN (parm
);
3779 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3780 TREE_CHAIN (parm
) = NULL_TREE
;
3783 --processing_template_parmlist
;
3785 return saved_parmlist
;
3788 /* end_template_decl is called after a template declaration is seen. */
3791 end_template_decl (void)
3793 reset_specialization ();
3795 if (! processing_template_decl
)
3798 /* This matches the pushlevel in begin_template_parm_list. */
3801 --processing_template_decl
;
3802 current_template_parms
= TREE_CHAIN (current_template_parms
);
3805 /* Takes a TREE_LIST representing a template parameter and convert it
3806 into an argument suitable to be passed to the type substitution
3807 functions. Note that If the TREE_LIST contains an error_mark
3808 node, the returned argument is error_mark_node. */
3811 template_parm_to_arg (tree t
)
3815 || TREE_CODE (t
) != TREE_LIST
)
3818 if (error_operand_p (TREE_VALUE (t
)))
3819 return error_mark_node
;
3823 if (TREE_CODE (t
) == TYPE_DECL
3824 || TREE_CODE (t
) == TEMPLATE_DECL
)
3828 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3830 /* Turn this argument into a TYPE_ARGUMENT_PACK
3831 with a single element, which expands T. */
3832 tree vec
= make_tree_vec (1);
3833 #ifdef ENABLE_CHECKING
3834 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3835 (vec
, TREE_VEC_LENGTH (vec
));
3837 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3839 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3840 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3845 t
= DECL_INITIAL (t
);
3847 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3849 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3850 with a single element, which expands T. */
3851 tree vec
= make_tree_vec (1);
3852 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3853 #ifdef ENABLE_CHECKING
3854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3855 (vec
, TREE_VEC_LENGTH (vec
));
3857 t
= convert_from_reference (t
);
3858 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3860 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3861 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3862 TREE_TYPE (t
) = type
;
3868 /* Given a set of template parameters, return them as a set of template
3869 arguments. The template parameters are represented as a TREE_VEC, in
3870 the form documented in cp-tree.h for template arguments. */
3873 template_parms_to_args (tree parms
)
3876 tree args
= NULL_TREE
;
3877 int length
= TMPL_PARMS_DEPTH (parms
);
3880 /* If there is only one level of template parameters, we do not
3881 create a TREE_VEC of TREE_VECs. Instead, we return a single
3882 TREE_VEC containing the arguments. */
3884 args
= make_tree_vec (length
);
3886 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3888 tree a
= copy_node (TREE_VALUE (header
));
3891 TREE_TYPE (a
) = NULL_TREE
;
3892 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3893 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3895 #ifdef ENABLE_CHECKING
3896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3900 TREE_VEC_ELT (args
, --l
) = a
;
3905 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3906 /* This can happen for template parms of a template template
3909 template<template<class T, class U> class TT> struct S;
3911 Consider the level of the parms of TT; T and U both have
3912 level 2; TT has no template parm of level 1. So in this case
3913 the first element of full_template_args is NULL_TREE. If we
3914 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3915 of 2. This will make tsubst wrongly consider that T and U
3916 have level 1. Instead, let's create a dummy vector as the
3917 first element of full_template_args so that TMPL_ARGS_DEPTH
3918 returns the correct depth for args. */
3919 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3923 /* Within the declaration of a template, return the currently active
3924 template parameters as an argument TREE_VEC. */
3927 current_template_args (void)
3929 return template_parms_to_args (current_template_parms
);
3932 /* Update the declared TYPE by doing any lookups which were thought to be
3933 dependent, but are not now that we know the SCOPE of the declarator. */
3936 maybe_update_decl_type (tree orig_type
, tree scope
)
3938 tree type
= orig_type
;
3940 if (type
== NULL_TREE
)
3943 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3944 type
= TREE_TYPE (type
);
3946 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3947 && dependent_type_p (type
)
3948 /* Don't bother building up the args in this case. */
3949 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3951 /* tsubst in the args corresponding to the template parameters,
3952 including auto if present. Most things will be unchanged, but
3953 make_typename_type and tsubst_qualified_id will resolve
3954 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3955 tree args
= current_template_args ();
3956 tree auto_node
= type_uses_auto (type
);
3960 tree auto_vec
= make_tree_vec (1);
3961 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3962 args
= add_to_template_args (args
, auto_vec
);
3964 pushed
= push_scope (scope
);
3965 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3970 if (type
== error_mark_node
)
3973 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3975 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3978 type
= TYPE_NAME (type
);
3983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3984 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3985 a member template. Used by push_template_decl below. */
3988 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3990 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3991 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3992 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3993 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3994 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3999 struct template_parm_data
4001 /* The level of the template parameters we are currently
4005 /* The index of the specialization argument we are currently
4009 /* An array whose size is the number of template parameters. The
4010 elements are nonzero if the parameter has been used in any one
4011 of the arguments processed so far. */
4014 /* An array whose size is the number of template arguments. The
4015 elements are nonzero if the argument makes use of template
4016 parameters of this level. */
4017 int* arg_uses_template_parms
;
4020 /* Subroutine of push_template_decl used to see if each template
4021 parameter in a partial specialization is used in the explicit
4022 argument list. If T is of the LEVEL given in DATA (which is
4023 treated as a template_parm_data*), then DATA->PARMS is marked
4027 mark_template_parm (tree t
, void* data
)
4031 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4033 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4035 level
= TEMPLATE_PARM_LEVEL (t
);
4036 idx
= TEMPLATE_PARM_IDX (t
);
4040 level
= TEMPLATE_TYPE_LEVEL (t
);
4041 idx
= TEMPLATE_TYPE_IDX (t
);
4044 if (level
== tpd
->level
)
4046 tpd
->parms
[idx
] = 1;
4047 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4050 /* Return zero so that for_each_template_parm will continue the
4051 traversal of the tree; we want to mark *every* template parm. */
4055 /* Process the partial specialization DECL. */
4058 process_partial_specialization (tree decl
)
4060 tree type
= TREE_TYPE (decl
);
4061 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4062 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4063 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4064 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4067 int nargs
= TREE_VEC_LENGTH (inner_args
);
4070 bool did_error_intro
= false;
4071 struct template_parm_data tpd
;
4072 struct template_parm_data tpd2
;
4074 gcc_assert (current_template_parms
);
4076 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4077 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4079 /* We check that each of the template parameters given in the
4080 partial specialization is used in the argument list to the
4081 specialization. For example:
4083 template <class T> struct S;
4084 template <class T> struct S<T*>;
4086 The second declaration is OK because `T*' uses the template
4087 parameter T, whereas
4089 template <class T> struct S<int>;
4091 is no good. Even trickier is:
4102 The S2<T> declaration is actually invalid; it is a
4103 full-specialization. Of course,
4106 struct S2<T (*)(U)>;
4108 or some such would have been OK. */
4109 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4110 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4111 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4113 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4114 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4115 for (i
= 0; i
< nargs
; ++i
)
4117 tpd
.current_arg
= i
;
4118 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4119 &mark_template_parm
,
4122 /*include_nondeduced_p=*/false);
4124 for (i
= 0; i
< ntparms
; ++i
)
4125 if (tpd
.parms
[i
] == 0)
4127 /* One of the template parms was not used in the
4129 if (!did_error_intro
)
4131 error ("template parameters not used in partial specialization:");
4132 did_error_intro
= true;
4135 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4138 if (did_error_intro
)
4139 return error_mark_node
;
4141 /* [temp.class.spec]
4143 The argument list of the specialization shall not be identical to
4144 the implicit argument list of the primary template. */
4145 if (comp_template_args
4147 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4149 error ("partial specialization %qT does not specialize any template arguments", type
);
4151 /* A partial specialization that replaces multiple parameters of the
4152 primary template with a pack expansion is less specialized for those
4154 if (nargs
< DECL_NTPARMS (maintmpl
))
4156 error ("partial specialization is not more specialized than the "
4157 "primary template because it replaces multiple parameters "
4158 "with a pack expansion");
4159 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4163 /* [temp.class.spec]
4165 A partially specialized non-type argument expression shall not
4166 involve template parameters of the partial specialization except
4167 when the argument expression is a simple identifier.
4169 The type of a template parameter corresponding to a specialized
4170 non-type argument shall not be dependent on a parameter of the
4173 Also, we verify that pack expansions only occur at the
4174 end of the argument list. */
4175 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4177 for (i
= 0; i
< nargs
; ++i
)
4179 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4180 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4181 tree packed_args
= NULL_TREE
;
4184 if (ARGUMENT_PACK_P (arg
))
4186 /* Extract the arguments from the argument pack. We'll be
4187 iterating over these in the following loop. */
4188 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4189 len
= TREE_VEC_LENGTH (packed_args
);
4192 for (j
= 0; j
< len
; j
++)
4195 /* Get the Jth argument in the parameter pack. */
4196 arg
= TREE_VEC_ELT (packed_args
, j
);
4198 if (PACK_EXPANSION_P (arg
))
4200 /* Pack expansions must come at the end of the
4202 if ((packed_args
&& j
< len
- 1)
4203 || (!packed_args
&& i
< nargs
- 1))
4205 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4206 error ("parameter pack argument %qE must be at the "
4207 "end of the template argument list", arg
);
4209 error ("parameter pack argument %qT must be at the "
4210 "end of the template argument list", arg
);
4214 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4215 /* We only care about the pattern. */
4216 arg
= PACK_EXPANSION_PATTERN (arg
);
4218 if (/* These first two lines are the `non-type' bit. */
4220 && TREE_CODE (arg
) != TEMPLATE_DECL
4221 /* This next line is the `argument expression is not just a
4222 simple identifier' condition and also the `specialized
4223 non-type argument' bit. */
4224 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4226 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4227 || (packed_args
&& uses_template_parms (arg
)))
4228 error ("template argument %qE involves template parameter(s)",
4232 /* Look at the corresponding template parameter,
4233 marking which template parameters its type depends
4235 tree type
= TREE_TYPE (parm
);
4239 /* We haven't yet initialized TPD2. Do so now. */
4240 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4241 /* The number of parameters here is the number in the
4242 main template, which, as checked in the assertion
4244 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4246 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4249 /* Mark the template parameters. But this time, we're
4250 looking for the template parameters of the main
4251 template, not in the specialization. */
4252 tpd2
.current_arg
= i
;
4253 tpd2
.arg_uses_template_parms
[i
] = 0;
4254 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4255 for_each_template_parm (type
,
4256 &mark_template_parm
,
4259 /*include_nondeduced_p=*/false);
4261 if (tpd2
.arg_uses_template_parms
[i
])
4263 /* The type depended on some template parameters.
4264 If they are fully specialized in the
4265 specialization, that's OK. */
4268 for (j
= 0; j
< nargs
; ++j
)
4269 if (tpd2
.parms
[j
] != 0
4270 && tpd
.arg_uses_template_parms
[j
])
4273 error_n (input_location
, count
,
4274 "type %qT of template argument %qE depends "
4275 "on a template parameter",
4276 "type %qT of template argument %qE depends "
4277 "on template parameters",
4286 /* We should only get here once. */
4287 gcc_assert (!COMPLETE_TYPE_P (type
));
4289 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4290 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4291 TREE_TYPE (tmpl
) = type
;
4292 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4293 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4294 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4295 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4297 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4298 = tree_cons (specargs
, tmpl
,
4299 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4300 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4302 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4303 inst
= TREE_CHAIN (inst
))
4305 tree inst_type
= TREE_VALUE (inst
);
4306 if (COMPLETE_TYPE_P (inst_type
)
4307 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4309 tree spec
= most_specialized_class (inst_type
, tf_none
);
4310 if (spec
&& TREE_TYPE (spec
) == type
)
4311 permerror (input_location
,
4312 "partial specialization of %qT after instantiation "
4313 "of %qT", type
, inst_type
);
4320 /* Check that a template declaration's use of default arguments and
4321 parameter packs is not invalid. Here, PARMS are the template
4322 parameters. IS_PRIMARY is true if DECL is the thing declared by
4323 a primary template. IS_PARTIAL is true if DECL is a partial
4326 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4327 declaration (but not a definition); 1 indicates a declaration, 2
4328 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4329 emitted for extraneous default arguments.
4331 Returns TRUE if there were no errors found, FALSE otherwise. */
4334 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4335 bool is_partial
, int is_friend_decl
)
4338 int last_level_to_check
;
4340 bool no_errors
= true;
4344 A default template-argument shall not be specified in a
4345 function template declaration or a function template definition, nor
4346 in the template-parameter-list of the definition of a member of a
4349 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4350 /* You can't have a function template declaration in a local
4351 scope, nor you can you define a member of a class template in a
4355 if (TREE_CODE (decl
) == TYPE_DECL
4357 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4358 /* A lambda doesn't have an explicit declaration; don't complain
4359 about the parms of the enclosing class. */
4362 if (current_class_type
4363 && !TYPE_BEING_DEFINED (current_class_type
)
4364 && DECL_LANG_SPECIFIC (decl
)
4365 && DECL_DECLARES_FUNCTION_P (decl
)
4366 /* If this is either a friend defined in the scope of the class
4367 or a member function. */
4368 && (DECL_FUNCTION_MEMBER_P (decl
)
4369 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4370 : DECL_FRIEND_CONTEXT (decl
)
4371 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4373 /* And, if it was a member function, it really was defined in
4374 the scope of the class. */
4375 && (!DECL_FUNCTION_MEMBER_P (decl
)
4376 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4377 /* We already checked these parameters when the template was
4378 declared, so there's no need to do it again now. This function
4379 was defined in class scope, but we're processing its body now
4380 that the class is complete. */
4383 /* Core issue 226 (C++0x only): the following only applies to class
4386 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4390 If a template-parameter has a default template-argument, all
4391 subsequent template-parameters shall have a default
4392 template-argument supplied. */
4393 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4395 tree inner_parms
= TREE_VALUE (parm_level
);
4396 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4397 int seen_def_arg_p
= 0;
4400 for (i
= 0; i
< ntparms
; ++i
)
4402 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4404 if (parm
== error_mark_node
)
4407 if (TREE_PURPOSE (parm
))
4409 else if (seen_def_arg_p
4410 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4412 error ("no default argument for %qD", TREE_VALUE (parm
));
4413 /* For better subsequent error-recovery, we indicate that
4414 there should have been a default argument. */
4415 TREE_PURPOSE (parm
) = error_mark_node
;
4418 else if (!is_partial
4420 /* Don't complain about an enclosing partial
4422 && parm_level
== parms
4423 && TREE_CODE (decl
) == TYPE_DECL
4425 && template_parameter_pack_p (TREE_VALUE (parm
)))
4427 /* A primary class template can only have one
4428 parameter pack, at the end of the template
4431 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4432 error ("parameter pack %qE must be at the end of the"
4433 " template parameter list", TREE_VALUE (parm
));
4435 error ("parameter pack %qT must be at the end of the"
4436 " template parameter list",
4437 TREE_TYPE (TREE_VALUE (parm
)));
4439 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4447 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4451 /* For an ordinary class template, default template arguments are
4452 allowed at the innermost level, e.g.:
4453 template <class T = int>
4455 but, in a partial specialization, they're not allowed even
4456 there, as we have in [temp.class.spec]:
4458 The template parameter list of a specialization shall not
4459 contain default template argument values.
4461 So, for a partial specialization, or for a function template
4462 (in C++98/C++03), we look at all of them. */
4465 /* But, for a primary class template that is not a partial
4466 specialization we look at all template parameters except the
4468 parms
= TREE_CHAIN (parms
);
4470 /* Figure out what error message to issue. */
4471 if (is_friend_decl
== 2)
4472 msg
= G_("default template arguments may not be used in function template "
4473 "friend re-declaration");
4474 else if (is_friend_decl
)
4475 msg
= G_("default template arguments may not be used in function template "
4476 "friend declarations");
4477 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4478 msg
= G_("default template arguments may not be used in function templates "
4479 "without -std=c++11 or -std=gnu++11");
4480 else if (is_partial
)
4481 msg
= G_("default template arguments may not be used in "
4482 "partial specializations");
4484 msg
= G_("default argument for template parameter for class enclosing %qD");
4486 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4487 /* If we're inside a class definition, there's no need to
4488 examine the parameters to the class itself. On the one
4489 hand, they will be checked when the class is defined, and,
4490 on the other, default arguments are valid in things like:
4491 template <class T = double>
4492 struct S { template <class U> void f(U); };
4493 Here the default argument for `S' has no bearing on the
4494 declaration of `f'. */
4495 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4497 /* Check everything. */
4498 last_level_to_check
= 0;
4500 for (parm_level
= parms
;
4501 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4502 parm_level
= TREE_CHAIN (parm_level
))
4504 tree inner_parms
= TREE_VALUE (parm_level
);
4508 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4509 for (i
= 0; i
< ntparms
; ++i
)
4511 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4514 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4519 if (is_friend_decl
== 2)
4526 /* Clear out the default argument so that we are not
4528 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4532 /* At this point, if we're still interested in issuing messages,
4533 they must apply to classes surrounding the object declared. */
4535 msg
= G_("default argument for template parameter for class "
4542 /* Worker for push_template_decl_real, called via
4543 for_each_template_parm. DATA is really an int, indicating the
4544 level of the parameters we are interested in. If T is a template
4545 parameter of that level, return nonzero. */
4548 template_parm_this_level_p (tree t
, void* data
)
4550 int this_level
= *(int *)data
;
4553 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4554 level
= TEMPLATE_PARM_LEVEL (t
);
4556 level
= TEMPLATE_TYPE_LEVEL (t
);
4557 return level
== this_level
;
4560 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4561 parameters given by current_template_args, or reuses a
4562 previously existing one, if appropriate. Returns the DECL, or an
4563 equivalent one, if it is replaced via a call to duplicate_decls.
4565 If IS_FRIEND is true, DECL is a friend declaration. */
4568 push_template_decl_real (tree decl
, bool is_friend
)
4576 int new_template_p
= 0;
4577 /* True if the template is a member template, in the sense of
4579 bool member_template_p
= false;
4581 if (decl
== error_mark_node
|| !current_template_parms
)
4582 return error_mark_node
;
4584 /* See if this is a partial specialization. */
4585 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4586 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4587 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4589 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4593 /* For a friend, we want the context of the friend function, not
4594 the type of which it is a friend. */
4595 ctx
= CP_DECL_CONTEXT (decl
);
4596 else if (CP_DECL_CONTEXT (decl
)
4597 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4598 /* In the case of a virtual function, we want the class in which
4600 ctx
= CP_DECL_CONTEXT (decl
);
4602 /* Otherwise, if we're currently defining some class, the DECL
4603 is assumed to be a member of the class. */
4604 ctx
= current_scope ();
4606 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4609 if (!DECL_CONTEXT (decl
))
4610 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4612 /* See if this is a primary template. */
4613 if (is_friend
&& ctx
)
4614 /* A friend template that specifies a class context, i.e.
4615 template <typename T> friend void A<T>::f();
4619 is_primary
= template_parm_scope_p ();
4623 if (DECL_CLASS_SCOPE_P (decl
))
4624 member_template_p
= true;
4625 if (TREE_CODE (decl
) == TYPE_DECL
4626 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4628 error ("template class without a name");
4629 return error_mark_node
;
4631 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4633 if (DECL_DESTRUCTOR_P (decl
))
4637 A destructor shall not be a member template. */
4638 error ("destructor %qD declared as member template", decl
);
4639 return error_mark_node
;
4641 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4642 && (!prototype_p (TREE_TYPE (decl
))
4643 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4644 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4645 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4646 == void_list_node
)))
4648 /* [basic.stc.dynamic.allocation]
4650 An allocation function can be a function
4651 template. ... Template allocation functions shall
4652 have two or more parameters. */
4653 error ("invalid template declaration of %qD", decl
);
4654 return error_mark_node
;
4657 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4658 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4660 else if (TREE_CODE (decl
) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl
))
4662 /* alias-declaration */
4663 gcc_assert (!DECL_ARTIFICIAL (decl
));
4666 error ("template declaration of %q#D", decl
);
4667 return error_mark_node
;
4671 /* Check to see that the rules regarding the use of default
4672 arguments are not being violated. */
4673 check_default_tmpl_args (decl
, current_template_parms
,
4674 is_primary
, is_partial
, /*is_friend_decl=*/0);
4676 /* Ensure that there are no parameter packs in the type of this
4677 declaration that have not been expanded. */
4678 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4680 /* Check each of the arguments individually to see if there are
4681 any bare parameter packs. */
4682 tree type
= TREE_TYPE (decl
);
4683 tree arg
= DECL_ARGUMENTS (decl
);
4684 tree argtype
= TYPE_ARG_TYPES (type
);
4686 while (arg
&& argtype
)
4688 if (!DECL_PACK_P (arg
)
4689 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4691 /* This is a PARM_DECL that contains unexpanded parameter
4692 packs. We have already complained about this in the
4693 check_for_bare_parameter_packs call, so just replace
4694 these types with ERROR_MARK_NODE. */
4695 TREE_TYPE (arg
) = error_mark_node
;
4696 TREE_VALUE (argtype
) = error_mark_node
;
4699 arg
= DECL_CHAIN (arg
);
4700 argtype
= TREE_CHAIN (argtype
);
4703 /* Check for bare parameter packs in the return type and the
4704 exception specifiers. */
4705 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4706 /* Errors were already issued, set return type to int
4707 as the frontend doesn't expect error_mark_node as
4709 TREE_TYPE (type
) = integer_type_node
;
4710 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4711 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4713 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4714 && TYPE_DECL_ALIAS_P (decl
))
4715 ? DECL_ORIGINAL_TYPE (decl
)
4716 : TREE_TYPE (decl
)))
4718 TREE_TYPE (decl
) = error_mark_node
;
4719 return error_mark_node
;
4723 return process_partial_specialization (decl
);
4725 args
= current_template_args ();
4728 || TREE_CODE (ctx
) == FUNCTION_DECL
4729 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4730 || (TREE_CODE (decl
) == TYPE_DECL
4731 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4732 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4734 if (DECL_LANG_SPECIFIC (decl
)
4735 && DECL_TEMPLATE_INFO (decl
)
4736 && DECL_TI_TEMPLATE (decl
))
4737 tmpl
= DECL_TI_TEMPLATE (decl
);
4738 /* If DECL is a TYPE_DECL for a class-template, then there won't
4739 be DECL_LANG_SPECIFIC. The information equivalent to
4740 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4741 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4742 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4743 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4745 /* Since a template declaration already existed for this
4746 class-type, we must be redeclaring it here. Make sure
4747 that the redeclaration is valid. */
4748 redeclare_class_template (TREE_TYPE (decl
),
4749 current_template_parms
);
4750 /* We don't need to create a new TEMPLATE_DECL; just use the
4751 one we already had. */
4752 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4756 tmpl
= build_template_decl (decl
, current_template_parms
,
4760 if (DECL_LANG_SPECIFIC (decl
)
4761 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4763 /* A specialization of a member template of a template
4765 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4766 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4767 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4773 tree a
, t
, current
, parms
;
4775 tree tinfo
= get_template_info (decl
);
4779 error ("template definition of non-template %q#D", decl
);
4780 return error_mark_node
;
4783 tmpl
= TI_TEMPLATE (tinfo
);
4785 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4786 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4787 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4788 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4792 /* The declaration is a specialization of a member
4793 template, declared outside the class. Therefore, the
4794 innermost template arguments will be NULL, so we
4795 replace them with the arguments determined by the
4796 earlier call to check_explicit_specialization. */
4797 args
= DECL_TI_ARGS (decl
);
4800 = build_template_decl (decl
, current_template_parms
,
4802 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4803 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4804 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4805 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4806 DECL_TEMPLATE_INFO (new_tmpl
)
4807 = build_template_info (tmpl
, args
);
4809 register_specialization (new_tmpl
,
4810 most_general_template (tmpl
),
4816 /* Make sure the template headers we got make sense. */
4818 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4819 i
= TMPL_PARMS_DEPTH (parms
);
4820 if (TMPL_ARGS_DEPTH (args
) != i
)
4822 error ("expected %d levels of template parms for %q#D, got %d",
4823 i
, decl
, TMPL_ARGS_DEPTH (args
));
4826 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4828 a
= TMPL_ARGS_LEVEL (args
, i
);
4829 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4831 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4833 if (current
== decl
)
4834 error ("got %d template parameters for %q#D",
4835 TREE_VEC_LENGTH (a
), decl
);
4837 error ("got %d template parameters for %q#T",
4838 TREE_VEC_LENGTH (a
), current
);
4839 error (" but %d required", TREE_VEC_LENGTH (t
));
4840 /* Avoid crash in import_export_decl. */
4841 DECL_INTERFACE_KNOWN (decl
) = 1;
4842 return error_mark_node
;
4845 if (current
== decl
)
4847 else if (current
== NULL_TREE
)
4848 /* Can happen in erroneous input. */
4851 current
= get_containing_scope (current
);
4854 /* Check that the parms are used in the appropriate qualifying scopes
4855 in the declarator. */
4856 if (!comp_template_args
4858 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4861 template arguments to %qD do not match original template %qD",
4862 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4863 if (!uses_template_parms (TI_ARGS (tinfo
)))
4864 inform (input_location
, "use template<> for an explicit specialization");
4865 /* Avoid crash in import_export_decl. */
4866 DECL_INTERFACE_KNOWN (decl
) = 1;
4867 return error_mark_node
;
4871 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4872 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4874 /* Push template declarations for global functions and types. Note
4875 that we do not try to push a global template friend declared in a
4876 template class; such a thing may well depend on the template
4877 parameters of the class. */
4878 if (new_template_p
&& !ctx
4879 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4881 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4882 if (tmpl
== error_mark_node
)
4883 return error_mark_node
;
4885 /* Hide template friend classes that haven't been declared yet. */
4886 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4888 DECL_ANTICIPATED (tmpl
) = 1;
4889 DECL_FRIEND_P (tmpl
) = 1;
4895 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4898 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4899 if (DECL_CONV_FN_P (tmpl
))
4901 int depth
= TMPL_PARMS_DEPTH (parms
);
4903 /* It is a conversion operator. See if the type converted to
4904 depends on innermost template operands. */
4906 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4908 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4911 /* Give template template parms a DECL_CONTEXT of the template
4912 for which they are a parameter. */
4913 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4914 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4916 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4917 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4918 DECL_CONTEXT (parm
) = tmpl
;
4922 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4923 back to its most general template. If TMPL is a specialization,
4924 ARGS may only have the innermost set of arguments. Add the missing
4925 argument levels if necessary. */
4926 if (DECL_TEMPLATE_INFO (tmpl
))
4927 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4929 info
= build_template_info (tmpl
, args
);
4931 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4932 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4935 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4936 retrofit_lang_decl (decl
);
4937 if (DECL_LANG_SPECIFIC (decl
))
4938 DECL_TEMPLATE_INFO (decl
) = info
;
4941 return DECL_TEMPLATE_RESULT (tmpl
);
4945 push_template_decl (tree decl
)
4947 return push_template_decl_real (decl
, false);
4950 /* FN is an inheriting constructor that inherits from the constructor
4951 template INHERITED; turn FN into a constructor template with a matching
4955 add_inherited_template_parms (tree fn
, tree inherited
)
4958 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4959 inner_parms
= copy_node (inner_parms
);
4961 = tree_cons (size_int (processing_template_decl
+ 1),
4962 inner_parms
, current_template_parms
);
4963 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4964 tree args
= template_parms_to_args (parms
);
4965 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4966 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4967 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4968 DECL_ARTIFICIAL (tmpl
) = true;
4969 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4973 /* Called when a class template TYPE is redeclared with the indicated
4974 template PARMS, e.g.:
4976 template <class T> struct S;
4977 template <class T> struct S {}; */
4980 redeclare_class_template (tree type
, tree parms
)
4986 if (!TYPE_TEMPLATE_INFO (type
))
4988 error ("%qT is not a template type", type
);
4992 tmpl
= TYPE_TI_TEMPLATE (type
);
4993 if (!PRIMARY_TEMPLATE_P (tmpl
))
4994 /* The type is nested in some template class. Nothing to worry
4995 about here; there are no new template parameters for the nested
5001 error ("template specifiers not specified in declaration of %qD",
5006 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5007 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5009 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5011 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5012 "redeclared with %d template parameter",
5013 "redeclared with %d template parameters",
5014 TREE_VEC_LENGTH (parms
));
5015 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5016 "previous declaration %q+D used %d template parameter",
5017 "previous declaration %q+D used %d template parameters",
5018 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5022 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5029 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5030 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5033 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5034 if (tmpl_parm
== error_mark_node
)
5037 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5038 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5039 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5041 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5043 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5044 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5045 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5046 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5047 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5048 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5049 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5050 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5051 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5053 error ("template parameter %q+#D", tmpl_parm
);
5054 error ("redeclared here as %q#D", parm
);
5058 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5060 /* We have in [temp.param]:
5062 A template-parameter may not be given default arguments
5063 by two different declarations in the same scope. */
5064 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5065 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5066 "original definition appeared here");
5070 if (parm_default
!= NULL_TREE
)
5071 /* Update the previous template parameters (which are the ones
5072 that will really count) with the new default value. */
5073 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5074 else if (tmpl_default
!= NULL_TREE
)
5075 /* Update the new parameters, too; they'll be used as the
5076 parameters for any members. */
5077 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5083 /* Simplify EXPR if it is a non-dependent expression. Returns the
5084 (possibly simplified) expression. */
5087 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5089 if (expr
== NULL_TREE
)
5092 /* If we're in a template, but EXPR isn't value dependent, simplify
5093 it. We're supposed to treat:
5095 template <typename T> void f(T[1 + 1]);
5096 template <typename T> void f(T[2]);
5098 as two declarations of the same function, for example. */
5099 if (processing_template_decl
5100 && !instantiation_dependent_expression_p (expr
)
5101 && potential_constant_expression (expr
))
5103 HOST_WIDE_INT saved_processing_template_decl
;
5105 saved_processing_template_decl
= processing_template_decl
;
5106 processing_template_decl
= 0;
5107 expr
= tsubst_copy_and_build (expr
,
5110 /*in_decl=*/NULL_TREE
,
5111 /*function_p=*/false,
5112 /*integral_constant_expression_p=*/true);
5113 processing_template_decl
= saved_processing_template_decl
;
5119 fold_non_dependent_expr (tree expr
)
5121 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5124 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5125 template declaration, or a TYPE_DECL for an alias declaration. */
5128 alias_type_or_template_p (tree t
)
5132 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5135 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5136 || DECL_ALIAS_TEMPLATE_P (t
));
5139 /* Return TRUE iff is a specialization of an alias template. */
5142 alias_template_specialization_p (const_tree t
)
5148 && TYPE_TEMPLATE_INFO (t
)
5149 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5150 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5153 /* Return the number of innermost template parameters in TMPL. */
5156 num_innermost_template_parms (tree tmpl
)
5158 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5159 return TREE_VEC_LENGTH (parms
);
5162 /* Return either TMPL or another template that it is equivalent to under DR
5163 1286: An alias that just changes the name of a template is equivalent to
5164 the other template. */
5167 get_underlying_template (tree tmpl
)
5169 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5170 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5172 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5173 if (TYPE_TEMPLATE_INFO (result
))
5175 tree sub
= TYPE_TI_TEMPLATE (result
);
5176 if (PRIMARY_TEMPLATE_P (sub
)
5177 && (num_innermost_template_parms (tmpl
)
5178 == num_innermost_template_parms (sub
))
5179 && same_type_p (result
, TREE_TYPE (sub
)))
5181 /* The alias type is equivalent to the pattern of the
5182 underlying template, so strip the alias. */
5192 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5193 must be a function or a pointer-to-function type, as specified
5194 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5195 and check that the resulting function has external linkage. */
5198 convert_nontype_argument_function (tree type
, tree expr
)
5202 linkage_kind linkage
;
5204 fn
= instantiate_type (type
, fns
, tf_none
);
5205 if (fn
== error_mark_node
)
5206 return error_mark_node
;
5209 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5210 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5211 if (BASELINK_P (fn_no_ptr
))
5212 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5214 /* [temp.arg.nontype]/1
5216 A template-argument for a non-type, non-template template-parameter
5219 -- the address of an object or function with external [C++11: or
5220 internal] linkage. */
5222 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5224 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5225 if (TYPE_PTR_P (type
))
5226 error ("it must be the address of a function with external linkage");
5228 error ("it must be the name of a function with external linkage");
5232 linkage
= decl_linkage (fn_no_ptr
);
5233 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5235 if (cxx_dialect
>= cxx11
)
5236 error ("%qE is not a valid template argument for type %qT "
5237 "because %qD has no linkage",
5238 expr
, type
, fn_no_ptr
);
5240 error ("%qE is not a valid template argument for type %qT "
5241 "because %qD does not have external linkage",
5242 expr
, type
, fn_no_ptr
);
5249 /* Subroutine of convert_nontype_argument.
5250 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5251 Emit an error otherwise. */
5254 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5255 tsubst_flags_t complain
)
5258 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5260 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5262 if (complain
& tf_error
)
5264 error ("%qE is not a valid template argument for type %qT",
5266 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5271 /* Returns TRUE iff the address of OP is value-dependent.
5273 14.6.2.4 [temp.dep.temp]:
5274 A non-integral non-type template-argument is dependent if its type is
5275 dependent or it has either of the following forms
5278 and contains a nested-name-specifier which specifies a class-name that
5279 names a dependent type.
5281 We generalize this to just say that the address of a member of a
5282 dependent class is value-dependent; the above doesn't cover the
5283 address of a static data member named with an unqualified-id. */
5286 has_value_dependent_address (tree op
)
5288 /* We could use get_inner_reference here, but there's no need;
5289 this is only relevant for template non-type arguments, which
5290 can only be expressed as &id-expression. */
5293 tree ctx
= CP_DECL_CONTEXT (op
);
5294 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5301 /* The next set of functions are used for providing helpful explanatory
5302 diagnostics for failed overload resolution. Their messages should be
5303 indented by two spaces for consistency with the messages in
5307 unify_success (bool /*explain_p*/)
5313 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5316 inform (input_location
,
5317 " couldn't deduce template parameter %qD", parm
);
5322 unify_invalid (bool /*explain_p*/)
5328 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5331 inform (input_location
,
5332 " types %qT and %qT have incompatible cv-qualifiers",
5338 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5341 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5346 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5349 inform (input_location
,
5350 " template parameter %qD is not a parameter pack, but "
5357 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5360 inform (input_location
,
5361 " template argument %qE does not match "
5362 "pointer-to-member constant %qE",
5368 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5371 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5376 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5379 inform (input_location
,
5380 " inconsistent parameter pack deduction with %qT and %qT",
5386 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5391 inform (input_location
,
5392 " deduced conflicting types for parameter %qT (%qT and %qT)",
5393 parm
, first
, second
);
5395 inform (input_location
,
5396 " deduced conflicting values for non-type parameter "
5397 "%qE (%qE and %qE)", parm
, first
, second
);
5403 unify_vla_arg (bool explain_p
, tree arg
)
5406 inform (input_location
,
5407 " variable-sized array type %qT is not "
5408 "a valid template argument",
5414 unify_method_type_error (bool explain_p
, tree arg
)
5417 inform (input_location
,
5418 " member function type %qT is not a valid template argument",
5424 unify_arity (bool explain_p
, int have
, int wanted
)
5427 inform_n (input_location
, wanted
,
5428 " candidate expects %d argument, %d provided",
5429 " candidate expects %d arguments, %d provided",
5435 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5437 return unify_arity (explain_p
, have
, wanted
);
5441 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5443 return unify_arity (explain_p
, have
, wanted
);
5447 unify_arg_conversion (bool explain_p
, tree to_type
,
5448 tree from_type
, tree arg
)
5451 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5452 " cannot convert %qE (type %qT) to type %qT",
5453 arg
, from_type
, to_type
);
5458 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5459 tree parm
, tree arg
)
5464 case tbr_ambiguous_baseclass
:
5465 inform (input_location
, " %qT is an ambiguous base class of %qT",
5469 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5476 unify_inconsistent_template_template_parameters (bool explain_p
)
5479 inform (input_location
,
5480 " template parameters of a template template argument are "
5481 "inconsistent with other deduced template arguments");
5486 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5489 inform (input_location
,
5490 " can't deduce a template for %qT from non-template type %qT",
5496 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5499 inform (input_location
,
5500 " template argument %qE does not match %qD", arg
, parm
);
5505 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5508 inform (input_location
,
5509 " could not resolve address from overloaded function %qE",
5514 /* Attempt to convert the non-type template parameter EXPR to the
5515 indicated TYPE. If the conversion is successful, return the
5516 converted value. If the conversion is unsuccessful, return
5517 NULL_TREE if we issued an error message, or error_mark_node if we
5518 did not. We issue error messages for out-and-out bad template
5519 parameters, but not simply because the conversion failed, since we
5520 might be just trying to do argument deduction. Both TYPE and EXPR
5521 must be non-dependent.
5523 The conversion follows the special rules described in
5524 [temp.arg.nontype], and it is much more strict than an implicit
5527 This function is called twice for each template argument (see
5528 lookup_template_class for a more accurate description of this
5529 problem). This means that we need to handle expressions which
5530 are not valid in a C++ source, but can be created from the
5531 first call (for instance, casts to perform conversions). These
5532 hacks can go away after we fix the double coercion problem. */
5535 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5539 /* Detect immediately string literals as invalid non-type argument.
5540 This special-case is not needed for correctness (we would easily
5541 catch this later), but only to provide better diagnostic for this
5542 common user mistake. As suggested by DR 100, we do not mention
5543 linkage issues in the diagnostic as this is not the point. */
5544 /* FIXME we're making this OK. */
5545 if (TREE_CODE (expr
) == STRING_CST
)
5547 if (complain
& tf_error
)
5548 error ("%qE is not a valid template argument for type %qT "
5549 "because string literals can never be used in this context",
5554 /* Add the ADDR_EXPR now for the benefit of
5555 value_dependent_expression_p. */
5556 if (TYPE_PTROBV_P (type
)
5557 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5559 expr
= decay_conversion (expr
, complain
);
5560 if (expr
== error_mark_node
)
5561 return error_mark_node
;
5564 /* If we are in a template, EXPR may be non-dependent, but still
5565 have a syntactic, rather than semantic, form. For example, EXPR
5566 might be a SCOPE_REF, rather than the VAR_DECL to which the
5567 SCOPE_REF refers. Preserving the qualifying scope is necessary
5568 so that access checking can be performed when the template is
5569 instantiated -- but here we need the resolved form so that we can
5570 convert the argument. */
5571 if (TYPE_REF_OBJ_P (type
)
5572 && has_value_dependent_address (expr
))
5573 /* If we want the address and it's value-dependent, don't fold. */;
5574 else if (!type_unknown_p (expr
))
5575 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5576 if (error_operand_p (expr
))
5577 return error_mark_node
;
5578 expr_type
= TREE_TYPE (expr
);
5579 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5580 expr
= mark_lvalue_use (expr
);
5582 expr
= mark_rvalue_use (expr
);
5584 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5585 to a non-type argument of "nullptr". */
5586 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5587 expr
= convert (type
, expr
);
5589 /* In C++11, integral or enumeration non-type template arguments can be
5590 arbitrary constant expressions. Pointer and pointer to
5591 member arguments can be general constant expressions that evaluate
5592 to a null value, but otherwise still need to be of a specific form. */
5593 if (cxx_dialect
>= cxx11
)
5595 if (TREE_CODE (expr
) == PTRMEM_CST
)
5596 /* A PTRMEM_CST is already constant, and a valid template
5597 argument for a parameter of pointer to member type, we just want
5598 to leave it in that form rather than lower it to a
5600 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5601 expr
= maybe_constant_value (expr
);
5602 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5604 tree folded
= maybe_constant_value (expr
);
5605 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5606 : null_member_pointer_value_p (folded
))
5611 /* HACK: Due to double coercion, we can get a
5612 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5613 which is the tree that we built on the first call (see
5614 below when coercing to reference to object or to reference to
5615 function). We just strip everything and get to the arg.
5616 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5618 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5620 tree probe_type
, probe
= expr
;
5621 if (REFERENCE_REF_P (probe
))
5622 probe
= TREE_OPERAND (probe
, 0);
5623 probe_type
= TREE_TYPE (probe
);
5624 if (TREE_CODE (probe
) == NOP_EXPR
)
5626 /* ??? Maybe we could use convert_from_reference here, but we
5627 would need to relax its constraints because the NOP_EXPR
5628 could actually change the type to something more cv-qualified,
5629 and this is not folded by convert_from_reference. */
5630 tree addr
= TREE_OPERAND (probe
, 0);
5631 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5632 && TREE_CODE (addr
) == ADDR_EXPR
5633 && TYPE_PTR_P (TREE_TYPE (addr
))
5634 && (same_type_ignoring_top_level_qualifiers_p
5635 (TREE_TYPE (probe_type
),
5636 TREE_TYPE (TREE_TYPE (addr
)))))
5638 expr
= TREE_OPERAND (addr
, 0);
5639 expr_type
= TREE_TYPE (probe_type
);
5644 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5645 parameter is a pointer to object, through decay and
5646 qualification conversion. Let's strip everything. */
5647 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5651 if (TREE_CODE (probe
) == ADDR_EXPR
5652 && TYPE_PTR_P (TREE_TYPE (probe
)))
5654 /* Skip the ADDR_EXPR only if it is part of the decay for
5655 an array. Otherwise, it is part of the original argument
5656 in the source code. */
5657 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5658 probe
= TREE_OPERAND (probe
, 0);
5660 expr_type
= TREE_TYPE (expr
);
5664 /* [temp.arg.nontype]/5, bullet 1
5666 For a non-type template-parameter of integral or enumeration type,
5667 integral promotions (_conv.prom_) and integral conversions
5668 (_conv.integral_) are applied. */
5669 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5671 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5672 t
= maybe_constant_value (t
);
5673 if (t
!= error_mark_node
)
5676 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5677 return error_mark_node
;
5679 /* Notice that there are constant expressions like '4 % 0' which
5680 do not fold into integer constants. */
5681 if (TREE_CODE (expr
) != INTEGER_CST
)
5683 if (complain
& tf_error
)
5685 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5686 if (processing_template_decl
5687 && !require_potential_constant_expression (expr
))
5689 expr
= cxx_constant_value (expr
);
5690 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5691 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5692 "in template argument for type %qT ", type
);
5693 if (expr
== error_mark_node
)
5695 /* else cxx_constant_value complained but gave us
5696 a real constant, so go ahead. */
5697 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5703 /* Avoid typedef problems. */
5704 if (TREE_TYPE (expr
) != type
)
5705 expr
= fold_convert (type
, expr
);
5707 /* [temp.arg.nontype]/5, bullet 2
5709 For a non-type template-parameter of type pointer to object,
5710 qualification conversions (_conv.qual_) and the array-to-pointer
5711 conversion (_conv.array_) are applied. */
5712 else if (TYPE_PTROBV_P (type
))
5714 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5716 A template-argument for a non-type, non-template template-parameter
5717 shall be one of: [...]
5719 -- the name of a non-type template-parameter;
5720 -- the address of an object or function with external linkage, [...]
5721 expressed as "& id-expression" where the & is optional if the name
5722 refers to a function or array, or if the corresponding
5723 template-parameter is a reference.
5725 Here, we do not care about functions, as they are invalid anyway
5726 for a parameter of type pointer-to-object. */
5728 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5729 /* Non-type template parameters are OK. */
5731 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5732 /* Null pointer values are OK in C++11. */;
5733 else if (TREE_CODE (expr
) != ADDR_EXPR
5734 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5738 error ("%qD is not a valid template argument "
5739 "because %qD is a variable, not the address of "
5744 if (POINTER_TYPE_P (expr_type
))
5746 error ("%qE is not a valid template argument for %qT "
5747 "because it is not the address of a variable",
5751 /* Other values, like integer constants, might be valid
5752 non-type arguments of some other type. */
5753 return error_mark_node
;
5759 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5760 ? TREE_OPERAND (expr
, 0) : expr
);
5763 error ("%qE is not a valid template argument of type %qT "
5764 "because %qE is not a variable",
5768 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5770 error ("%qE is not a valid template argument of type %qT "
5771 "because %qD does not have external linkage",
5775 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5777 error ("%qE is not a valid template argument of type %qT "
5778 "because %qD has no linkage",
5784 expr
= decay_conversion (expr
, complain
);
5785 if (expr
== error_mark_node
)
5786 return error_mark_node
;
5788 expr
= perform_qualification_conversions (type
, expr
);
5789 if (expr
== error_mark_node
)
5790 return error_mark_node
;
5792 /* [temp.arg.nontype]/5, bullet 3
5794 For a non-type template-parameter of type reference to object, no
5795 conversions apply. The type referred to by the reference may be more
5796 cv-qualified than the (otherwise identical) type of the
5797 template-argument. The template-parameter is bound directly to the
5798 template-argument, which must be an lvalue. */
5799 else if (TYPE_REF_OBJ_P (type
))
5801 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5803 return error_mark_node
;
5805 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5807 error ("%qE is not a valid template argument for type %qT "
5808 "because of conflicts in cv-qualification", expr
, type
);
5812 if (!real_lvalue_p (expr
))
5814 error ("%qE is not a valid template argument for type %qT "
5815 "because it is not an lvalue", expr
, type
);
5819 /* [temp.arg.nontype]/1
5821 A template-argument for a non-type, non-template template-parameter
5822 shall be one of: [...]
5824 -- the address of an object or function with external linkage. */
5825 if (INDIRECT_REF_P (expr
)
5826 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5828 expr
= TREE_OPERAND (expr
, 0);
5831 error ("%q#D is not a valid template argument for type %qT "
5832 "because a reference variable does not have a constant "
5833 "address", expr
, type
);
5840 error ("%qE is not a valid template argument for type %qT "
5841 "because it is not an object with external linkage",
5846 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5848 error ("%qE is not a valid template argument for type %qT "
5849 "because object %qD has not external linkage",
5854 expr
= build_nop (type
, build_address (expr
));
5856 /* [temp.arg.nontype]/5, bullet 4
5858 For a non-type template-parameter of type pointer to function, only
5859 the function-to-pointer conversion (_conv.func_) is applied. If the
5860 template-argument represents a set of overloaded functions (or a
5861 pointer to such), the matching function is selected from the set
5863 else if (TYPE_PTRFN_P (type
))
5865 /* If the argument is a template-id, we might not have enough
5866 context information to decay the pointer. */
5867 if (!type_unknown_p (expr_type
))
5869 expr
= decay_conversion (expr
, complain
);
5870 if (expr
== error_mark_node
)
5871 return error_mark_node
;
5874 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5875 /* Null pointer values are OK in C++11. */
5876 return perform_qualification_conversions (type
, expr
);
5878 expr
= convert_nontype_argument_function (type
, expr
);
5879 if (!expr
|| expr
== error_mark_node
)
5882 /* [temp.arg.nontype]/5, bullet 5
5884 For a non-type template-parameter of type reference to function, no
5885 conversions apply. If the template-argument represents a set of
5886 overloaded functions, the matching function is selected from the set
5888 else if (TYPE_REFFN_P (type
))
5890 if (TREE_CODE (expr
) == ADDR_EXPR
)
5892 error ("%qE is not a valid template argument for type %qT "
5893 "because it is a pointer", expr
, type
);
5894 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5898 expr
= convert_nontype_argument_function (type
, expr
);
5899 if (!expr
|| expr
== error_mark_node
)
5902 expr
= build_nop (type
, build_address (expr
));
5904 /* [temp.arg.nontype]/5, bullet 6
5906 For a non-type template-parameter of type pointer to member function,
5907 no conversions apply. If the template-argument represents a set of
5908 overloaded member functions, the matching member function is selected
5909 from the set (_over.over_). */
5910 else if (TYPE_PTRMEMFUNC_P (type
))
5912 expr
= instantiate_type (type
, expr
, tf_none
);
5913 if (expr
== error_mark_node
)
5914 return error_mark_node
;
5916 /* [temp.arg.nontype] bullet 1 says the pointer to member
5917 expression must be a pointer-to-member constant. */
5918 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5919 return error_mark_node
;
5921 /* There is no way to disable standard conversions in
5922 resolve_address_of_overloaded_function (called by
5923 instantiate_type). It is possible that the call succeeded by
5924 converting &B::I to &D::I (where B is a base of D), so we need
5925 to reject this conversion here.
5927 Actually, even if there was a way to disable standard conversions,
5928 it would still be better to reject them here so that we can
5929 provide a superior diagnostic. */
5930 if (!same_type_p (TREE_TYPE (expr
), type
))
5932 error ("%qE is not a valid template argument for type %qT "
5933 "because it is of type %qT", expr
, type
,
5935 /* If we are just one standard conversion off, explain. */
5936 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5937 inform (input_location
,
5938 "standard conversions are not allowed in this context");
5942 /* [temp.arg.nontype]/5, bullet 7
5944 For a non-type template-parameter of type pointer to data member,
5945 qualification conversions (_conv.qual_) are applied. */
5946 else if (TYPE_PTRDATAMEM_P (type
))
5948 /* [temp.arg.nontype] bullet 1 says the pointer to member
5949 expression must be a pointer-to-member constant. */
5950 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5951 return error_mark_node
;
5953 expr
= perform_qualification_conversions (type
, expr
);
5954 if (expr
== error_mark_node
)
5957 else if (NULLPTR_TYPE_P (type
))
5959 if (expr
!= nullptr_node
)
5961 error ("%qE is not a valid template argument for type %qT "
5962 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5967 /* A template non-type parameter must be one of the above. */
5971 /* Sanity check: did we actually convert the argument to the
5973 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5974 (type
, TREE_TYPE (expr
)));
5978 /* Subroutine of coerce_template_template_parms, which returns 1 if
5979 PARM_PARM and ARG_PARM match using the rule for the template
5980 parameters of template template parameters. Both PARM and ARG are
5981 template parameters; the rest of the arguments are the same as for
5982 coerce_template_template_parms.
5985 coerce_template_template_parm (tree parm
,
5987 tsubst_flags_t complain
,
5991 if (arg
== NULL_TREE
|| arg
== error_mark_node
5992 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5995 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5998 switch (TREE_CODE (parm
))
6001 /* We encounter instantiations of templates like
6002 template <template <template <class> class> class TT>
6005 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6006 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6008 if (!coerce_template_template_parms
6009 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6015 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6016 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6017 /* Argument is a parameter pack but parameter is not. */
6022 /* The tsubst call is used to handle cases such as
6024 template <int> class C {};
6025 template <class T, template <T> class TT> class D {};
6028 i.e. the parameter list of TT depends on earlier parameters. */
6029 if (!uses_template_parms (TREE_TYPE (arg
))
6031 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6035 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6036 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6037 /* Argument is a parameter pack but parameter is not. */
6050 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6051 template template parameters. Both PARM_PARMS and ARG_PARMS are
6052 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6055 Consider the example:
6056 template <class T> class A;
6057 template<template <class U> class TT> class B;
6059 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6060 the parameters to A, and OUTER_ARGS contains A. */
6063 coerce_template_template_parms (tree parm_parms
,
6065 tsubst_flags_t complain
,
6069 int nparms
, nargs
, i
;
6073 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6074 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6076 nparms
= TREE_VEC_LENGTH (parm_parms
);
6077 nargs
= TREE_VEC_LENGTH (arg_parms
);
6079 /* Determine whether we have a parameter pack at the end of the
6080 template template parameter's template parameter list. */
6081 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6083 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6085 if (parm
== error_mark_node
)
6088 switch (TREE_CODE (parm
))
6092 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6097 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6107 && !(variadic_p
&& nargs
>= nparms
- 1))
6110 /* Check all of the template parameters except the parameter pack at
6111 the end (if any). */
6112 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6114 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6115 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6118 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6119 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6121 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6129 /* Check each of the template parameters in the template
6130 argument against the template parameter pack at the end of
6131 the template template parameter. */
6132 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6135 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6137 for (; i
< nargs
; ++i
)
6139 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6142 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6144 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6153 /* Verifies that the deduced template arguments (in TARGS) for the
6154 template template parameters (in TPARMS) represent valid bindings,
6155 by comparing the template parameter list of each template argument
6156 to the template parameter list of its corresponding template
6157 template parameter, in accordance with DR150. This
6158 routine can only be called after all template arguments have been
6159 deduced. It will return TRUE if all of the template template
6160 parameter bindings are okay, FALSE otherwise. */
6162 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6164 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6167 /* We're dealing with template parms in this process. */
6168 ++processing_template_decl
;
6170 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6172 for (i
= 0; i
< ntparms
; ++i
)
6174 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6175 tree targ
= TREE_VEC_ELT (targs
, i
);
6177 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6179 tree packed_args
= NULL_TREE
;
6182 if (ARGUMENT_PACK_P (targ
))
6184 /* Look inside the argument pack. */
6185 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6186 len
= TREE_VEC_LENGTH (packed_args
);
6189 for (idx
= 0; idx
< len
; ++idx
)
6191 tree targ_parms
= NULL_TREE
;
6194 /* Extract the next argument from the argument
6196 targ
= TREE_VEC_ELT (packed_args
, idx
);
6198 if (PACK_EXPANSION_P (targ
))
6199 /* Look at the pattern of the pack expansion. */
6200 targ
= PACK_EXPANSION_PATTERN (targ
);
6202 /* Extract the template parameters from the template
6204 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6205 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6206 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6207 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6209 /* Verify that we can coerce the template template
6210 parameters from the template argument to the template
6211 parameter. This requires an exact match. */
6213 && !coerce_template_template_parms
6214 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6229 --processing_template_decl
;
6233 /* Since type attributes aren't mangled, we need to strip them from
6234 template type arguments. */
6237 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6240 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6242 mv
= TYPE_MAIN_VARIANT (arg
);
6243 arg
= strip_typedefs (arg
);
6244 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6245 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6247 if (complain
& tf_warning
)
6248 warning (0, "ignoring attributes on template argument %qT", arg
);
6249 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6250 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6255 /* Convert the indicated template ARG as necessary to match the
6256 indicated template PARM. Returns the converted ARG, or
6257 error_mark_node if the conversion was unsuccessful. Error and
6258 warning messages are issued under control of COMPLAIN. This
6259 conversion is for the Ith parameter in the parameter list. ARGS is
6260 the full set of template arguments deduced so far. */
6263 convert_template_argument (tree parm
,
6266 tsubst_flags_t complain
,
6272 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6274 if (TREE_CODE (arg
) == TREE_LIST
6275 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6277 /* The template argument was the name of some
6278 member function. That's usually
6279 invalid, but static members are OK. In any
6280 case, grab the underlying fields/functions
6281 and issue an error later if required. */
6282 orig_arg
= TREE_VALUE (arg
);
6283 TREE_TYPE (arg
) = unknown_type_node
;
6288 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6289 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6290 || requires_tmpl_type
);
6292 /* When determining whether an argument pack expansion is a template,
6293 look at the pattern. */
6294 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6295 arg
= PACK_EXPANSION_PATTERN (arg
);
6297 /* Deal with an injected-class-name used as a template template arg. */
6298 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6300 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6301 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6303 if (cxx_dialect
>= cxx11
)
6304 /* OK under DR 1004. */;
6305 else if (complain
& tf_warning_or_error
)
6306 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6307 " used as template template argument", TYPE_NAME (arg
));
6308 else if (flag_pedantic_errors
)
6316 ((TREE_CODE (arg
) == TEMPLATE_DECL
6317 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6318 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6319 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6320 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6323 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6324 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6325 arg
= TYPE_STUB_DECL (arg
);
6327 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6329 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6330 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6332 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6334 if (complain
& tf_error
)
6335 error ("invalid use of destructor %qE as a type", orig_arg
);
6336 return error_mark_node
;
6339 permerror (input_location
,
6340 "to refer to a type member of a template parameter, "
6341 "use %<typename %E%>", orig_arg
);
6343 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6344 TREE_OPERAND (arg
, 1),
6350 if (is_type
!= requires_type
)
6354 if (complain
& tf_error
)
6356 error ("type/value mismatch at argument %d in template "
6357 "parameter list for %qD",
6360 error (" expected a constant of type %qT, got %qT",
6362 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6363 else if (requires_tmpl_type
)
6364 error (" expected a class template, got %qE", orig_arg
);
6366 error (" expected a type, got %qE", orig_arg
);
6369 return error_mark_node
;
6371 if (is_tmpl_type
^ requires_tmpl_type
)
6373 if (in_decl
&& (complain
& tf_error
))
6375 error ("type/value mismatch at argument %d in template "
6376 "parameter list for %qD",
6379 error (" expected a type, got %qT", DECL_NAME (arg
));
6381 error (" expected a class template, got %qT", orig_arg
);
6383 return error_mark_node
;
6388 if (requires_tmpl_type
)
6390 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6392 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6393 /* The number of argument required is not known yet.
6394 Just accept it for now. */
6395 val
= TREE_TYPE (arg
);
6398 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6401 /* Strip alias templates that are equivalent to another
6403 arg
= get_underlying_template (arg
);
6404 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6406 if (coerce_template_template_parms (parmparm
, argparm
,
6412 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6414 if (val
!= error_mark_node
)
6416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6417 val
= TREE_TYPE (val
);
6418 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6419 val
= make_pack_expansion (val
);
6424 if (in_decl
&& (complain
& tf_error
))
6426 error ("type/value mismatch at argument %d in "
6427 "template parameter list for %qD",
6429 error (" expected a template of type %qD, got %qT",
6433 val
= error_mark_node
;
6439 /* We only form one instance of each template specialization.
6440 Therefore, if we use a non-canonical variant (i.e., a
6441 typedef), any future messages referring to the type will use
6442 the typedef, which is confusing if those future uses do not
6443 themselves also use the typedef. */
6445 val
= canonicalize_type_argument (val
, complain
);
6449 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6451 if (invalid_nontype_parm_type_p (t
, complain
))
6452 return error_mark_node
;
6454 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6456 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6460 /* Not sure if this is reachable, but it doesn't hurt
6462 error ("type mismatch in nontype parameter pack");
6463 val
= error_mark_node
;
6466 else if (!dependent_template_arg_p (orig_arg
)
6467 && !uses_template_parms (t
))
6468 /* We used to call digest_init here. However, digest_init
6469 will report errors, which we don't want when complain
6470 is zero. More importantly, digest_init will try too
6471 hard to convert things: for example, `0' should not be
6472 converted to pointer type at this point according to
6473 the standard. Accepting this is not merely an
6474 extension, since deciding whether or not these
6475 conversions can occur is part of determining which
6476 function template to call, or whether a given explicit
6477 argument specification is valid. */
6478 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6480 val
= strip_typedefs_expr (orig_arg
);
6482 if (val
== NULL_TREE
)
6483 val
= error_mark_node
;
6484 else if (val
== error_mark_node
&& (complain
& tf_error
))
6485 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6487 if (TREE_CODE (val
) == SCOPE_REF
)
6489 /* Strip typedefs from the SCOPE_REF. */
6490 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6491 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6493 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6494 QUALIFIED_NAME_IS_TEMPLATE (val
));
6501 /* Coerces the remaining template arguments in INNER_ARGS (from
6502 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6503 Returns the coerced argument pack. PARM_IDX is the position of this
6504 parameter in the template parameter list. ARGS is the original
6505 template argument list. */
6507 coerce_template_parameter_pack (tree parms
,
6515 tsubst_flags_t complain
)
6517 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6518 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6521 tree packed_types
= NULL_TREE
;
6523 if (arg_idx
> nargs
)
6526 packed_args
= make_tree_vec (nargs
- arg_idx
);
6528 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6529 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6531 /* When the template parameter is a non-type template
6532 parameter pack whose type uses parameter packs, we need
6533 to look at each of the template arguments
6534 separately. Build a vector of the types for these
6535 non-type template parameters in PACKED_TYPES. */
6537 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6538 packed_types
= tsubst_pack_expansion (expansion
, args
,
6541 if (packed_types
== error_mark_node
)
6542 return error_mark_node
;
6544 /* Check that we have the right number of arguments. */
6546 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6547 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6550 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6551 error ("wrong number of template arguments (%d, should be %d)",
6552 nargs
, needed_parms
);
6553 return error_mark_node
;
6556 /* If we aren't able to check the actual arguments now
6557 (because they haven't been expanded yet), we can at least
6558 verify that all of the types used for the non-type
6559 template parameter pack are, in fact, valid for non-type
6560 template parameters. */
6562 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6564 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6565 for (j
= 0; j
< len
; ++j
)
6567 tree t
= TREE_VEC_ELT (packed_types
, j
);
6568 if (invalid_nontype_parm_type_p (t
, complain
))
6569 return error_mark_node
;
6574 /* Convert the remaining arguments, which will be a part of the
6575 parameter pack "parm". */
6576 for (; arg_idx
< nargs
; ++arg_idx
)
6578 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6579 tree actual_parm
= TREE_VALUE (parm
);
6581 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6583 /* When we have a vector of types (corresponding to the
6584 non-type template parameter pack that uses parameter
6585 packs in its type, as mention above), and the
6586 argument is not an expansion (which expands to a
6587 currently unknown number of arguments), clone the
6588 parm and give it the next type in PACKED_TYPES. */
6589 actual_parm
= copy_node (actual_parm
);
6590 TREE_TYPE (actual_parm
) =
6591 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6594 if (arg
== error_mark_node
)
6596 if (complain
& tf_error
)
6597 error ("template argument %d is invalid", arg_idx
+ 1);
6600 arg
= convert_template_argument (actual_parm
,
6601 arg
, new_args
, complain
, parm_idx
,
6603 if (arg
== error_mark_node
)
6605 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6608 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6609 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6610 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6613 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6614 TREE_TYPE (argument_pack
)
6615 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6616 TREE_CONSTANT (argument_pack
) = 1;
6619 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6620 #ifdef ENABLE_CHECKING
6621 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6622 TREE_VEC_LENGTH (packed_args
));
6624 return argument_pack
;
6627 /* Returns the number of pack expansions in the template argument vector
6631 pack_expansion_args_count (tree args
)
6636 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6638 tree elt
= TREE_VEC_ELT (args
, i
);
6639 if (elt
&& PACK_EXPANSION_P (elt
))
6645 /* Convert all template arguments to their appropriate types, and
6646 return a vector containing the innermost resulting template
6647 arguments. If any error occurs, return error_mark_node. Error and
6648 warning messages are issued under control of COMPLAIN.
6650 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6651 for arguments not specified in ARGS. Otherwise, if
6652 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6653 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6654 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6658 coerce_template_parms (tree parms
,
6661 tsubst_flags_t complain
,
6662 bool require_all_args
,
6663 bool use_default_args
)
6665 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6668 tree new_inner_args
;
6669 int saved_unevaluated_operand
;
6670 int saved_inhibit_evaluation_warnings
;
6672 /* When used as a boolean value, indicates whether this is a
6673 variadic template parameter list. Since it's an int, we can also
6674 subtract it from nparms to get the number of non-variadic
6677 int variadic_args_p
= 0;
6678 int post_variadic_parms
= 0;
6680 if (args
== error_mark_node
)
6681 return error_mark_node
;
6683 nparms
= TREE_VEC_LENGTH (parms
);
6685 /* Determine if there are any parameter packs. */
6686 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6688 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6690 ++post_variadic_parms
;
6691 if (template_parameter_pack_p (tparm
))
6695 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6696 /* If there are no parameters that follow a parameter pack, we need to
6697 expand any argument packs so that we can deduce a parameter pack from
6698 some non-packed args followed by an argument pack, as in variadic85.C.
6699 If there are such parameters, we need to leave argument packs intact
6700 so the arguments are assigned properly. This can happen when dealing
6701 with a nested class inside a partial specialization of a class
6702 template, as in variadic92.C, or when deducing a template parameter pack
6703 from a sub-declarator, as in variadic114.C. */
6704 if (!post_variadic_parms
)
6705 inner_args
= expand_template_argument_pack (inner_args
);
6707 /* Count any pack expansion args. */
6708 variadic_args_p
= pack_expansion_args_count (inner_args
);
6710 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6711 if ((nargs
> nparms
&& !variadic_p
)
6712 || (nargs
< nparms
- variadic_p
6715 && (!use_default_args
6716 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6717 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6719 if (complain
& tf_error
)
6723 nparms
-= variadic_p
;
6724 error ("wrong number of template arguments "
6725 "(%d, should be %d or more)", nargs
, nparms
);
6728 error ("wrong number of template arguments "
6729 "(%d, should be %d)", nargs
, nparms
);
6732 error ("provided for %q+D", in_decl
);
6735 return error_mark_node
;
6737 /* We can't pass a pack expansion to a non-pack parameter of an alias
6738 template (DR 1430). */
6739 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6741 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6743 if (complain
& tf_error
)
6745 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6747 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6748 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6750 if (PACK_EXPANSION_P (arg
)
6751 && !template_parameter_pack_p (parm
))
6753 error ("pack expansion argument for non-pack parameter "
6754 "%qD of alias template %qD", parm
, in_decl
);
6755 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6762 return error_mark_node
;
6765 /* We need to evaluate the template arguments, even though this
6766 template-id may be nested within a "sizeof". */
6767 saved_unevaluated_operand
= cp_unevaluated_operand
;
6768 cp_unevaluated_operand
= 0;
6769 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6770 c_inhibit_evaluation_warnings
= 0;
6771 new_inner_args
= make_tree_vec (nparms
);
6772 new_args
= add_outermost_template_args (args
, new_inner_args
);
6773 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6778 /* Get the Ith template parameter. */
6779 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6781 if (parm
== error_mark_node
)
6783 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6787 /* Calculate the next argument. */
6788 if (arg_idx
< nargs
)
6789 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6793 if (template_parameter_pack_p (TREE_VALUE (parm
))
6794 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6796 /* All remaining arguments will be placed in the
6797 template parameter pack PARM. */
6798 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6799 inner_args
, arg_idx
,
6803 /* Store this argument. */
6804 if (arg
== error_mark_node
)
6806 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6808 /* We are done with all of the arguments. */
6815 if (PACK_EXPANSION_P (arg
))
6817 /* We don't know how many args we have yet, just
6818 use the unconverted ones for now. */
6819 new_inner_args
= inner_args
;
6823 else if (require_all_args
)
6825 /* There must be a default arg in this case. */
6826 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6828 /* The position of the first default template argument,
6829 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6831 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6832 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6837 if (arg
== error_mark_node
)
6839 if (complain
& tf_error
)
6840 error ("template argument %d is invalid", arg_idx
+ 1);
6843 /* This only occurs if there was an error in the template
6844 parameter list itself (which we would already have
6845 reported) that we are trying to recover from, e.g., a class
6846 template with a parameter list such as
6847 template<typename..., typename>. */
6850 arg
= convert_template_argument (TREE_VALUE (parm
),
6851 arg
, new_args
, complain
,
6854 if (arg
== error_mark_node
)
6856 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6858 cp_unevaluated_operand
= saved_unevaluated_operand
;
6859 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6862 return error_mark_node
;
6864 #ifdef ENABLE_CHECKING
6865 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6866 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6867 TREE_VEC_LENGTH (new_inner_args
));
6870 return new_inner_args
;
6873 /* Like coerce_template_parms. If PARMS represents all template
6874 parameters levels, this function returns a vector of vectors
6875 representing all the resulting argument levels. Note that in this
6876 case, only the innermost arguments are coerced because the
6877 outermost ones are supposed to have been coerced already.
6879 Otherwise, if PARMS represents only (the innermost) vector of
6880 parameters, this function returns a vector containing just the
6881 innermost resulting arguments. */
6884 coerce_innermost_template_parms (tree parms
,
6887 tsubst_flags_t complain
,
6888 bool require_all_args
,
6889 bool use_default_args
)
6891 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6892 int args_depth
= TMPL_ARGS_DEPTH (args
);
6895 if (parms_depth
> 1)
6897 coerced_args
= make_tree_vec (parms_depth
);
6901 for (level
= parms
, cur_depth
= parms_depth
;
6902 parms_depth
> 0 && level
!= NULL_TREE
;
6903 level
= TREE_CHAIN (level
), --cur_depth
)
6906 if (cur_depth
== args_depth
)
6907 l
= coerce_template_parms (TREE_VALUE (level
),
6908 args
, in_decl
, complain
,
6912 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6914 if (l
== error_mark_node
)
6915 return error_mark_node
;
6917 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6921 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6922 args
, in_decl
, complain
,
6925 return coerced_args
;
6928 /* Returns 1 if template args OT and NT are equivalent. */
6931 template_args_equal (tree ot
, tree nt
)
6935 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6938 if (TREE_CODE (nt
) == TREE_VEC
)
6939 /* For member templates */
6940 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6941 else if (PACK_EXPANSION_P (ot
))
6942 return (PACK_EXPANSION_P (nt
)
6943 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6944 PACK_EXPANSION_PATTERN (nt
))
6945 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6946 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6947 else if (ARGUMENT_PACK_P (ot
))
6952 if (!ARGUMENT_PACK_P (nt
))
6955 opack
= ARGUMENT_PACK_ARGS (ot
);
6956 npack
= ARGUMENT_PACK_ARGS (nt
);
6957 len
= TREE_VEC_LENGTH (opack
);
6958 if (TREE_VEC_LENGTH (npack
) != len
)
6960 for (i
= 0; i
< len
; ++i
)
6961 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6962 TREE_VEC_ELT (npack
, i
)))
6966 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6968 /* We get here probably because we are in the middle of substituting
6969 into the pattern of a pack expansion. In that case the
6970 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6971 interested in. So we want to use the initial pack argument for
6973 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6974 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6975 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6976 return template_args_equal (ot
, nt
);
6978 else if (TYPE_P (nt
))
6979 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6980 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6983 return cp_tree_equal (ot
, nt
);
6986 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6987 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6988 NEWARG_PTR with the offending arguments if they are non-NULL. */
6991 comp_template_args_with_info (tree oldargs
, tree newargs
,
6992 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6996 if (oldargs
== newargs
)
6999 if (!oldargs
|| !newargs
)
7002 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7005 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7007 tree nt
= TREE_VEC_ELT (newargs
, i
);
7008 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7010 if (! template_args_equal (ot
, nt
))
7012 if (oldarg_ptr
!= NULL
)
7014 if (newarg_ptr
!= NULL
)
7022 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7023 of template arguments. Returns 0 otherwise. */
7026 comp_template_args (tree oldargs
, tree newargs
)
7028 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7032 add_pending_template (tree d
)
7034 tree ti
= (TYPE_P (d
)
7035 ? CLASSTYPE_TEMPLATE_INFO (d
)
7036 : DECL_TEMPLATE_INFO (d
));
7037 struct pending_template
*pt
;
7040 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7043 /* We are called both from instantiate_decl, where we've already had a
7044 tinst_level pushed, and instantiate_template, where we haven't.
7046 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7049 push_tinst_level (d
);
7051 pt
= ggc_alloc_pending_template ();
7053 pt
->tinst
= current_tinst_level
;
7054 if (last_pending_template
)
7055 last_pending_template
->next
= pt
;
7057 pending_templates
= pt
;
7059 last_pending_template
= pt
;
7061 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7068 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7069 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7070 documentation for TEMPLATE_ID_EXPR. */
7073 lookup_template_function (tree fns
, tree arglist
)
7077 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7078 return error_mark_node
;
7080 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7082 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7084 error ("%q#D is not a function template", fns
);
7085 return error_mark_node
;
7088 if (BASELINK_P (fns
))
7090 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7092 BASELINK_FUNCTIONS (fns
),
7097 type
= TREE_TYPE (fns
);
7098 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7099 type
= unknown_type_node
;
7101 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7104 /* Within the scope of a template class S<T>, the name S gets bound
7105 (in build_self_reference) to a TYPE_DECL for the class, not a
7106 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7107 or one of its enclosing classes, and that type is a template,
7108 return the associated TEMPLATE_DECL. Otherwise, the original
7111 Also handle the case when DECL is a TREE_LIST of ambiguous
7112 injected-class-names from different bases. */
7115 maybe_get_template_decl_from_type_decl (tree decl
)
7117 if (decl
== NULL_TREE
)
7120 /* DR 176: A lookup that finds an injected-class-name (10.2
7121 [class.member.lookup]) can result in an ambiguity in certain cases
7122 (for example, if it is found in more than one base class). If all of
7123 the injected-class-names that are found refer to specializations of
7124 the same class template, and if the name is followed by a
7125 template-argument-list, the reference refers to the class template
7126 itself and not a specialization thereof, and is not ambiguous. */
7127 if (TREE_CODE (decl
) == TREE_LIST
)
7129 tree t
, tmpl
= NULL_TREE
;
7130 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7132 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7135 else if (tmpl
!= elt
)
7138 if (tmpl
&& t
== NULL_TREE
)
7144 return (decl
!= NULL_TREE
7145 && DECL_SELF_REFERENCE_P (decl
)
7146 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7147 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7150 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7151 parameters, find the desired type.
7153 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7155 IN_DECL, if non-NULL, is the template declaration we are trying to
7158 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7159 the class we are looking up.
7161 Issue error and warning messages under control of COMPLAIN.
7163 If the template class is really a local class in a template
7164 function, then the FUNCTION_CONTEXT is the function in which it is
7167 ??? Note that this function is currently called *twice* for each
7168 template-id: the first time from the parser, while creating the
7169 incomplete type (finish_template_type), and the second type during the
7170 real instantiation (instantiate_template_class). This is surely something
7171 that we want to avoid. It also causes some problems with argument
7172 coercion (see convert_nontype_argument for more information on this). */
7175 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7176 int entering_scope
, tsubst_flags_t complain
)
7178 tree templ
= NULL_TREE
, parmlist
;
7185 if (identifier_p (d1
))
7187 tree value
= innermost_non_namespace_value (d1
);
7188 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7193 push_decl_namespace (context
);
7194 templ
= lookup_name (d1
);
7195 templ
= maybe_get_template_decl_from_type_decl (templ
);
7197 pop_decl_namespace ();
7200 context
= DECL_CONTEXT (templ
);
7202 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7204 tree type
= TREE_TYPE (d1
);
7206 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7207 an implicit typename for the second A. Deal with it. */
7208 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7209 type
= TREE_TYPE (type
);
7211 if (CLASSTYPE_TEMPLATE_INFO (type
))
7213 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7214 d1
= DECL_NAME (templ
);
7217 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7218 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7220 templ
= TYPE_TI_TEMPLATE (d1
);
7221 d1
= DECL_NAME (templ
);
7223 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7224 && DECL_TEMPLATE_RESULT (d1
)
7225 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7228 d1
= DECL_NAME (templ
);
7229 context
= DECL_CONTEXT (templ
);
7231 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7234 d1
= DECL_NAME (templ
);
7237 /* Issue an error message if we didn't find a template. */
7240 if (complain
& tf_error
)
7241 error ("%qT is not a template", d1
);
7242 return error_mark_node
;
7245 if (TREE_CODE (templ
) != TEMPLATE_DECL
7246 /* Make sure it's a user visible template, if it was named by
7248 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7249 && !PRIMARY_TEMPLATE_P (templ
)))
7251 if (complain
& tf_error
)
7253 error ("non-template type %qT used as a template", d1
);
7255 error ("for template declaration %q+D", in_decl
);
7257 return error_mark_node
;
7260 complain
&= ~tf_user
;
7262 /* An alias that just changes the name of a template is equivalent to the
7263 other template, so if any of the arguments are pack expansions, strip
7264 the alias to avoid problems with a pack expansion passed to a non-pack
7265 alias template parameter (DR 1430). */
7266 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7267 templ
= get_underlying_template (templ
);
7269 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7271 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7272 template arguments */
7278 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7280 /* Consider an example where a template template parameter declared as
7282 template <class T, class U = std::allocator<T> > class TT
7284 The template parameter level of T and U are one level larger than
7285 of TT. To proper process the default argument of U, say when an
7286 instantiation `TT<int>' is seen, we need to build the full
7287 arguments containing {int} as the innermost level. Outer levels,
7288 available when not appearing as default template argument, can be
7289 obtained from the arguments of the enclosing template.
7291 Suppose that TT is later substituted with std::vector. The above
7292 instantiation is `TT<int, std::allocator<T> >' with TT at
7293 level 1, and T at level 2, while the template arguments at level 1
7294 becomes {std::vector} and the inner level 2 is {int}. */
7296 outer
= DECL_CONTEXT (templ
);
7298 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7299 else if (current_template_parms
)
7300 /* This is an argument of the current template, so we haven't set
7301 DECL_CONTEXT yet. */
7302 outer
= current_template_args ();
7305 arglist
= add_to_template_args (outer
, arglist
);
7307 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7309 /*require_all_args=*/true,
7310 /*use_default_args=*/true);
7311 if (arglist2
== error_mark_node
7312 || (!uses_template_parms (arglist2
)
7313 && check_instantiated_args (templ
, arglist2
, complain
)))
7314 return error_mark_node
;
7316 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7321 tree template_type
= TREE_TYPE (templ
);
7324 tree found
= NULL_TREE
;
7327 int is_dependent_type
;
7328 int use_partial_inst_tmpl
= false;
7330 if (template_type
== error_mark_node
)
7331 /* An error occurred while building the template TEMPL, and a
7332 diagnostic has most certainly been emitted for that
7333 already. Let's propagate that error. */
7334 return error_mark_node
;
7336 gen_tmpl
= most_general_template (templ
);
7337 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7338 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7339 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7341 if (arg_depth
== 1 && parm_depth
> 1)
7343 /* We've been given an incomplete set of template arguments.
7346 template <class T> struct S1 {
7347 template <class U> struct S2 {};
7348 template <class U> struct S2<U*> {};
7351 we will be called with an ARGLIST of `U*', but the
7352 TEMPLATE will be `template <class T> template
7353 <class U> struct S1<T>::S2'. We must fill in the missing
7356 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7358 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7361 /* Now we should have enough arguments. */
7362 gcc_assert (parm_depth
== arg_depth
);
7364 /* From here on, we're only interested in the most general
7367 /* Calculate the BOUND_ARGS. These will be the args that are
7368 actually tsubst'd into the definition to create the
7372 /* We have multiple levels of arguments to coerce, at once. */
7374 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7376 tree bound_args
= make_tree_vec (parm_depth
);
7378 for (i
= saved_depth
,
7379 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7380 i
> 0 && t
!= NULL_TREE
;
7381 --i
, t
= TREE_CHAIN (t
))
7384 if (i
== saved_depth
)
7385 a
= coerce_template_parms (TREE_VALUE (t
),
7388 /*require_all_args=*/true,
7389 /*use_default_args=*/true);
7391 /* Outer levels should have already been coerced. */
7392 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7394 /* Don't process further if one of the levels fails. */
7395 if (a
== error_mark_node
)
7397 /* Restore the ARGLIST to its full size. */
7398 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7399 return error_mark_node
;
7402 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7404 /* We temporarily reduce the length of the ARGLIST so
7405 that coerce_template_parms will see only the arguments
7406 corresponding to the template parameters it is
7408 TREE_VEC_LENGTH (arglist
)--;
7411 /* Restore the ARGLIST to its full size. */
7412 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7414 arglist
= bound_args
;
7418 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7419 INNERMOST_TEMPLATE_ARGS (arglist
),
7422 /*require_all_args=*/true,
7423 /*use_default_args=*/true);
7425 if (arglist
== error_mark_node
)
7426 /* We were unable to bind the arguments. */
7427 return error_mark_node
;
7429 /* In the scope of a template class, explicit references to the
7430 template class refer to the type of the template, not any
7431 instantiation of it. For example, in:
7433 template <class T> class C { void f(C<T>); }
7435 the `C<T>' is just the same as `C'. Outside of the
7436 class, however, such a reference is an instantiation. */
7438 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7439 || currently_open_class (template_type
))
7440 /* comp_template_args is expensive, check it last. */
7441 && comp_template_args (TYPE_TI_ARGS (template_type
),
7443 return template_type
;
7445 /* If we already have this specialization, return it. */
7446 elt
.tmpl
= gen_tmpl
;
7448 hash
= hash_specialization (&elt
);
7449 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7455 is_dependent_type
= uses_template_parms (arglist
);
7457 /* If the deduced arguments are invalid, then the binding
7459 if (!is_dependent_type
7460 && check_instantiated_args (gen_tmpl
,
7461 INNERMOST_TEMPLATE_ARGS (arglist
),
7463 return error_mark_node
;
7465 if (!is_dependent_type
7466 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7467 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7468 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7470 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7471 DECL_NAME (gen_tmpl
),
7472 /*tag_scope=*/ts_global
);
7476 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7478 if (context
== error_mark_node
)
7479 return error_mark_node
;
7482 context
= global_namespace
;
7484 /* Create the type. */
7485 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7487 /* The user referred to a specialization of an alias
7488 template represented by GEN_TMPL.
7490 [temp.alias]/2 says:
7492 When a template-id refers to the specialization of an
7493 alias template, it is equivalent to the associated
7494 type obtained by substitution of its
7495 template-arguments for the template-parameters in the
7496 type-id of the alias template. */
7498 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7499 /* Note that the call above (by indirectly calling
7500 register_specialization in tsubst_decl) registers the
7501 TYPE_DECL representing the specialization of the alias
7502 template. So next time someone substitutes ARGLIST for
7503 the template parms into the alias template (GEN_TMPL),
7504 she'll get that TYPE_DECL back. */
7506 if (t
== error_mark_node
)
7509 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7511 if (!is_dependent_type
)
7513 set_current_access_from_decl (TYPE_NAME (template_type
));
7514 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7515 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7516 arglist
, complain
, in_decl
),
7517 SCOPED_ENUM_P (template_type
), NULL
);
7521 /* We don't want to call start_enum for this type, since
7522 the values for the enumeration constants may involve
7523 template parameters. And, no one should be interested
7524 in the enumeration constants for such a type. */
7525 t
= cxx_make_type (ENUMERAL_TYPE
);
7526 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7528 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7529 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7530 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7532 else if (CLASS_TYPE_P (template_type
))
7534 t
= make_class_type (TREE_CODE (template_type
));
7535 CLASSTYPE_DECLARED_CLASS (t
)
7536 = CLASSTYPE_DECLARED_CLASS (template_type
);
7537 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7538 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7540 /* A local class. Make sure the decl gets registered properly. */
7541 if (context
== current_function_decl
)
7542 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7544 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7545 /* This instantiation is another name for the primary
7546 template type. Set the TYPE_CANONICAL field
7548 TYPE_CANONICAL (t
) = template_type
;
7549 else if (any_template_arguments_need_structural_equality_p (arglist
))
7550 /* Some of the template arguments require structural
7551 equality testing, so this template class requires
7552 structural equality testing. */
7553 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7558 /* If we called start_enum or pushtag above, this information
7559 will already be set up. */
7562 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7564 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7565 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7566 DECL_SOURCE_LOCATION (type_decl
)
7567 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7570 type_decl
= TYPE_NAME (t
);
7572 if (CLASS_TYPE_P (template_type
))
7574 TREE_PRIVATE (type_decl
)
7575 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7576 TREE_PROTECTED (type_decl
)
7577 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7578 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7580 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7581 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7585 /* Let's consider the explicit specialization of a member
7586 of a class template specialization that is implicitely instantiated,
7591 template<class U> struct M {}; //#0
7596 struct S<int>::M<char> //#1
7600 [temp.expl.spec]/4 says this is valid.
7602 In this case, when we write:
7605 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7608 When we encounter #1, we want to store the partial instantiation
7609 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7611 For all cases other than this "explicit specialization of member of a
7612 class template", we just want to store the most general template into
7613 the CLASSTYPE_TI_TEMPLATE of M.
7615 This case of "explicit specialization of member of a class template"
7617 1/ the enclosing class is an instantiation of, and therefore not
7618 the same as, the context of the most general template, and
7619 2/ we aren't looking at the partial instantiation itself, i.e.
7620 the innermost arguments are not the same as the innermost parms of
7621 the most general template.
7623 So it's only when 1/ and 2/ happens that we want to use the partial
7624 instantiation of the member template in lieu of its most general
7627 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7628 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7629 /* the enclosing class must be an instantiation... */
7630 && CLASS_TYPE_P (context
)
7631 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7633 tree partial_inst_args
;
7634 TREE_VEC_LENGTH (arglist
)--;
7635 ++processing_template_decl
;
7637 tsubst (INNERMOST_TEMPLATE_ARGS
7638 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7639 arglist
, complain
, NULL_TREE
);
7640 --processing_template_decl
;
7641 TREE_VEC_LENGTH (arglist
)++;
7642 use_partial_inst_tmpl
=
7643 /*...and we must not be looking at the partial instantiation
7645 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7649 if (!use_partial_inst_tmpl
)
7650 /* This case is easy; there are no member templates involved. */
7654 /* This is a full instantiation of a member template. Find
7655 the partial instantiation of which this is an instance. */
7657 /* Temporarily reduce by one the number of levels in the ARGLIST
7658 so as to avoid comparing the last set of arguments. */
7659 TREE_VEC_LENGTH (arglist
)--;
7660 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7661 TREE_VEC_LENGTH (arglist
)++;
7662 /* FOUND is either a proper class type, or an alias
7663 template specialization. In the later case, it's a
7664 TYPE_DECL, resulting from the substituting of arguments
7665 for parameters in the TYPE_DECL of the alias template
7666 done earlier. So be careful while getting the template
7668 found
= TREE_CODE (found
) == TYPE_DECL
7669 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7670 : CLASSTYPE_TI_TEMPLATE (found
);
7673 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7676 slot
= htab_find_slot_with_hash (type_specializations
,
7677 &elt
, hash
, INSERT
);
7678 entry
= ggc_alloc_spec_entry ();
7682 /* Note this use of the partial instantiation so we can check it
7683 later in maybe_process_partial_specialization. */
7684 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7685 = tree_cons (arglist
, t
,
7686 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7688 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7689 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7690 /* Now that the type has been registered on the instantiations
7691 list, we set up the enumerators. Because the enumeration
7692 constants may involve the enumeration type itself, we make
7693 sure to register the type first, and then create the
7694 constants. That way, doing tsubst_expr for the enumeration
7695 constants won't result in recursive calls here; we'll find
7696 the instantiation and exit above. */
7697 tsubst_enum (template_type
, t
, arglist
);
7699 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7700 /* If the type makes use of template parameters, the
7701 code that generates debugging information will crash. */
7702 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7704 /* Possibly limit visibility based on template args. */
7705 TREE_PUBLIC (type_decl
) = 1;
7706 determine_visibility (type_decl
);
7712 /* Wrapper for lookup_template_class_1. */
7715 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7716 int entering_scope
, tsubst_flags_t complain
)
7719 timevar_push (TV_TEMPLATE_INST
);
7720 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7721 entering_scope
, complain
);
7722 timevar_pop (TV_TEMPLATE_INST
);
7730 /* True when we should also visit template parameters that occur in
7731 non-deduced contexts. */
7732 bool include_nondeduced_p
;
7733 struct pointer_set_t
*visited
;
7736 /* Called from for_each_template_parm via walk_tree. */
7739 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7742 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7743 tree_fn_t fn
= pfd
->fn
;
7744 void *data
= pfd
->data
;
7747 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7748 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7749 pfd
->include_nondeduced_p
))
7750 return error_mark_node
;
7752 switch (TREE_CODE (t
))
7755 if (TYPE_PTRMEMFUNC_P (t
))
7761 if (!TYPE_TEMPLATE_INFO (t
))
7763 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7764 fn
, data
, pfd
->visited
,
7765 pfd
->include_nondeduced_p
))
7766 return error_mark_node
;
7770 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7771 fn
, data
, pfd
->visited
,
7772 pfd
->include_nondeduced_p
)
7773 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7774 fn
, data
, pfd
->visited
,
7775 pfd
->include_nondeduced_p
))
7776 return error_mark_node
;
7780 /* Since we're not going to walk subtrees, we have to do this
7782 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7783 pfd
->visited
, pfd
->include_nondeduced_p
))
7784 return error_mark_node
;
7788 /* Check the return type. */
7789 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7790 pfd
->include_nondeduced_p
))
7791 return error_mark_node
;
7793 /* Check the parameter types. Since default arguments are not
7794 instantiated until they are needed, the TYPE_ARG_TYPES may
7795 contain expressions that involve template parameters. But,
7796 no-one should be looking at them yet. And, once they're
7797 instantiated, they don't contain template parameters, so
7798 there's no point in looking at them then, either. */
7802 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7803 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7804 pfd
->visited
, pfd
->include_nondeduced_p
))
7805 return error_mark_node
;
7807 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7808 want walk_tree walking into them itself. */
7814 case UNDERLYING_TYPE
:
7815 if (pfd
->include_nondeduced_p
7816 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7818 pfd
->include_nondeduced_p
))
7819 return error_mark_node
;
7824 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7825 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7826 pfd
->visited
, pfd
->include_nondeduced_p
))
7827 return error_mark_node
;
7832 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7833 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7834 pfd
->visited
, pfd
->include_nondeduced_p
))
7835 return error_mark_node
;
7836 if (DECL_CONTEXT (t
)
7837 && pfd
->include_nondeduced_p
7838 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7839 pfd
->visited
, pfd
->include_nondeduced_p
))
7840 return error_mark_node
;
7843 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7844 /* Record template parameters such as `T' inside `TT<T>'. */
7845 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7846 pfd
->include_nondeduced_p
))
7847 return error_mark_node
;
7850 case TEMPLATE_TEMPLATE_PARM
:
7851 case TEMPLATE_TYPE_PARM
:
7852 case TEMPLATE_PARM_INDEX
:
7853 if (fn
&& (*fn
)(t
, data
))
7854 return error_mark_node
;
7856 return error_mark_node
;
7860 /* A template template parameter is encountered. */
7861 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7862 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7863 pfd
->include_nondeduced_p
))
7864 return error_mark_node
;
7866 /* Already substituted template template parameter */
7872 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7874 pfd
->include_nondeduced_p
))
7875 return error_mark_node
;
7879 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7880 && pfd
->include_nondeduced_p
7881 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7882 (TREE_TYPE (t
)), fn
, data
,
7883 pfd
->visited
, pfd
->include_nondeduced_p
))
7884 return error_mark_node
;
7889 /* If there's no type, then this thing must be some expression
7890 involving template parameters. */
7891 if (!fn
&& !TREE_TYPE (t
))
7892 return error_mark_node
;
7897 case IMPLICIT_CONV_EXPR
:
7898 case REINTERPRET_CAST_EXPR
:
7899 case CONST_CAST_EXPR
:
7900 case STATIC_CAST_EXPR
:
7901 case DYNAMIC_CAST_EXPR
:
7905 case PSEUDO_DTOR_EXPR
:
7907 return error_mark_node
;
7914 /* We didn't find any template parameters we liked. */
7918 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7919 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7920 call FN with the parameter and the DATA.
7921 If FN returns nonzero, the iteration is terminated, and
7922 for_each_template_parm returns 1. Otherwise, the iteration
7923 continues. If FN never returns a nonzero value, the value
7924 returned by for_each_template_parm is 0. If FN is NULL, it is
7925 considered to be the function which always returns 1.
7927 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7928 parameters that occur in non-deduced contexts. When false, only
7929 visits those template parameters that can be deduced. */
7932 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7933 struct pointer_set_t
*visited
,
7934 bool include_nondeduced_p
)
7936 struct pair_fn_data pfd
;
7942 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7944 /* Walk the tree. (Conceptually, we would like to walk without
7945 duplicates, but for_each_template_parm_r recursively calls
7946 for_each_template_parm, so we would need to reorganize a fair
7947 bit to use walk_tree_without_duplicates, so we keep our own
7950 pfd
.visited
= visited
;
7952 pfd
.visited
= pointer_set_create ();
7953 result
= cp_walk_tree (&t
,
7954 for_each_template_parm_r
,
7956 pfd
.visited
) != NULL_TREE
;
7961 pointer_set_destroy (pfd
.visited
);
7968 /* Returns true if T depends on any template parameter. */
7971 uses_template_parms (tree t
)
7974 int saved_processing_template_decl
;
7976 saved_processing_template_decl
= processing_template_decl
;
7977 if (!saved_processing_template_decl
)
7978 processing_template_decl
= 1;
7980 dependent_p
= dependent_type_p (t
);
7981 else if (TREE_CODE (t
) == TREE_VEC
)
7982 dependent_p
= any_dependent_template_arguments_p (t
);
7983 else if (TREE_CODE (t
) == TREE_LIST
)
7984 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7985 || uses_template_parms (TREE_CHAIN (t
)));
7986 else if (TREE_CODE (t
) == TYPE_DECL
)
7987 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7990 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7991 || TREE_CODE (t
) == OVERLOAD
7994 || TREE_CODE (t
) == TRAIT_EXPR
7995 || TREE_CODE (t
) == CONSTRUCTOR
7996 || CONSTANT_CLASS_P (t
))
7997 dependent_p
= (type_dependent_expression_p (t
)
7998 || value_dependent_expression_p (t
));
8001 gcc_assert (t
== error_mark_node
);
8002 dependent_p
= false;
8005 processing_template_decl
= saved_processing_template_decl
;
8010 /* Returns true iff current_function_decl is an incompletely instantiated
8011 template. Useful instead of processing_template_decl because the latter
8012 is set to 0 during fold_non_dependent_expr. */
8015 in_template_function (void)
8017 tree fn
= current_function_decl
;
8019 ++processing_template_decl
;
8020 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8021 && DECL_TEMPLATE_INFO (fn
)
8022 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8023 --processing_template_decl
;
8027 /* Returns true if T depends on any template parameter with level LEVEL. */
8030 uses_template_parms_level (tree t
, int level
)
8032 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8033 /*include_nondeduced_p=*/true);
8036 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8037 ill-formed translation unit, i.e. a variable or function that isn't
8038 usable in a constant expression. */
8041 neglectable_inst_p (tree d
)
8044 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8045 : decl_maybe_constant_var_p (d
)));
8048 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8049 neglectable and instantiated from within an erroneous instantiation. */
8052 limit_bad_template_recursion (tree decl
)
8054 struct tinst_level
*lev
= current_tinst_level
;
8055 int errs
= errorcount
+ sorrycount
;
8056 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8059 for (; lev
; lev
= lev
->next
)
8060 if (neglectable_inst_p (lev
->decl
))
8063 return (lev
&& errs
> lev
->errors
);
8066 static int tinst_depth
;
8067 extern int max_tinst_depth
;
8070 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8072 /* We're starting to instantiate D; record the template instantiation context
8073 for diagnostics and to restore it later. */
8076 push_tinst_level (tree d
)
8078 struct tinst_level
*new_level
;
8080 if (tinst_depth
>= max_tinst_depth
)
8082 last_error_tinst_level
= current_tinst_level
;
8083 if (TREE_CODE (d
) == TREE_LIST
)
8084 error ("template instantiation depth exceeds maximum of %d (use "
8085 "-ftemplate-depth= to increase the maximum) substituting %qS",
8086 max_tinst_depth
, d
);
8088 error ("template instantiation depth exceeds maximum of %d (use "
8089 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8090 max_tinst_depth
, d
);
8092 print_instantiation_context ();
8097 /* If the current instantiation caused problems, don't let it instantiate
8098 anything else. Do allow deduction substitution and decls usable in
8099 constant expressions. */
8100 if (limit_bad_template_recursion (d
))
8103 new_level
= ggc_alloc_tinst_level ();
8104 new_level
->decl
= d
;
8105 new_level
->locus
= input_location
;
8106 new_level
->errors
= errorcount
+sorrycount
;
8107 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8108 new_level
->next
= current_tinst_level
;
8109 current_tinst_level
= new_level
;
8112 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8113 depth_reached
= tinst_depth
;
8118 /* We're done instantiating this template; return to the instantiation
8122 pop_tinst_level (void)
8124 /* Restore the filename and line number stashed away when we started
8125 this instantiation. */
8126 input_location
= current_tinst_level
->locus
;
8127 current_tinst_level
= current_tinst_level
->next
;
8131 /* We're instantiating a deferred template; restore the template
8132 instantiation context in which the instantiation was requested, which
8133 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8136 reopen_tinst_level (struct tinst_level
*level
)
8138 struct tinst_level
*t
;
8141 for (t
= level
; t
; t
= t
->next
)
8144 current_tinst_level
= level
;
8146 if (current_tinst_level
)
8147 current_tinst_level
->errors
= errorcount
+sorrycount
;
8151 /* Returns the TINST_LEVEL which gives the original instantiation
8154 struct tinst_level
*
8155 outermost_tinst_level (void)
8157 struct tinst_level
*level
= current_tinst_level
;
8160 level
= level
->next
;
8164 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8165 vector of template arguments, as for tsubst.
8167 Returns an appropriate tsubst'd friend declaration. */
8170 tsubst_friend_function (tree decl
, tree args
)
8174 if (TREE_CODE (decl
) == FUNCTION_DECL
8175 && DECL_TEMPLATE_INSTANTIATION (decl
)
8176 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8177 /* This was a friend declared with an explicit template
8178 argument list, e.g.:
8182 to indicate that f was a template instantiation, not a new
8183 function declaration. Now, we have to figure out what
8184 instantiation of what template. */
8186 tree template_id
, arglist
, fns
;
8189 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8191 /* Friend functions are looked up in the containing namespace scope.
8192 We must enter that scope, to avoid finding member functions of the
8193 current class with same name. */
8194 push_nested_namespace (ns
);
8195 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8196 tf_warning_or_error
, NULL_TREE
,
8197 /*integral_constant_expression_p=*/false);
8198 pop_nested_namespace (ns
);
8199 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8200 tf_warning_or_error
, NULL_TREE
);
8201 template_id
= lookup_template_function (fns
, arglist
);
8203 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8204 tmpl
= determine_specialization (template_id
, new_friend
,
8206 /*need_member_template=*/0,
8207 TREE_VEC_LENGTH (args
),
8209 return instantiate_template (tmpl
, new_args
, tf_error
);
8212 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8214 /* The NEW_FRIEND will look like an instantiation, to the
8215 compiler, but is not an instantiation from the point of view of
8216 the language. For example, we might have had:
8218 template <class T> struct S {
8219 template <class U> friend void f(T, U);
8222 Then, in S<int>, template <class U> void f(int, U) is not an
8223 instantiation of anything. */
8224 if (new_friend
== error_mark_node
)
8225 return error_mark_node
;
8227 DECL_USE_TEMPLATE (new_friend
) = 0;
8228 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8230 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8231 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8232 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8235 /* The mangled name for the NEW_FRIEND is incorrect. The function
8236 is not a template instantiation and should not be mangled like
8237 one. Therefore, we forget the mangling here; we'll recompute it
8238 later if we need it. */
8239 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8241 SET_DECL_RTL (new_friend
, NULL
);
8242 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8245 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8248 tree new_friend_template_info
;
8249 tree new_friend_result_template_info
;
8251 int new_friend_is_defn
;
8253 /* We must save some information from NEW_FRIEND before calling
8254 duplicate decls since that function will free NEW_FRIEND if
8256 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8257 new_friend_is_defn
=
8258 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8259 (template_for_substitution (new_friend
)))
8261 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8263 /* This declaration is a `primary' template. */
8264 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8266 new_friend_result_template_info
8267 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8270 new_friend_result_template_info
= NULL_TREE
;
8272 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8273 if (new_friend_is_defn
)
8274 DECL_INITIAL (new_friend
) = error_mark_node
;
8276 /* Inside pushdecl_namespace_level, we will push into the
8277 current namespace. However, the friend function should go
8278 into the namespace of the template. */
8279 ns
= decl_namespace_context (new_friend
);
8280 push_nested_namespace (ns
);
8281 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8282 pop_nested_namespace (ns
);
8284 if (old_decl
== error_mark_node
)
8285 return error_mark_node
;
8287 if (old_decl
!= new_friend
)
8289 /* This new friend declaration matched an existing
8290 declaration. For example, given:
8292 template <class T> void f(T);
8293 template <class U> class C {
8294 template <class T> friend void f(T) {}
8297 the friend declaration actually provides the definition
8298 of `f', once C has been instantiated for some type. So,
8299 old_decl will be the out-of-class template declaration,
8300 while new_friend is the in-class definition.
8302 But, if `f' was called before this point, the
8303 instantiation of `f' will have DECL_TI_ARGS corresponding
8304 to `T' but not to `U', references to which might appear
8305 in the definition of `f'. Previously, the most general
8306 template for an instantiation of `f' was the out-of-class
8307 version; now it is the in-class version. Therefore, we
8308 run through all specialization of `f', adding to their
8309 DECL_TI_ARGS appropriately. In particular, they need a
8310 new set of outer arguments, corresponding to the
8311 arguments for this class instantiation.
8313 The same situation can arise with something like this:
8316 template <class T> class C {
8320 when `C<int>' is instantiated. Now, `f(int)' is defined
8323 if (!new_friend_is_defn
)
8324 /* On the other hand, if the in-class declaration does
8325 *not* provide a definition, then we don't want to alter
8326 existing definitions. We can just leave everything
8331 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8332 tree new_args
= TI_ARGS (new_friend_template_info
);
8334 /* Overwrite whatever template info was there before, if
8335 any, with the new template information pertaining to
8337 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8339 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8341 /* We should have called reregister_specialization in
8343 gcc_assert (retrieve_specialization (new_template
,
8347 /* Instantiate it if the global has already been used. */
8348 if (DECL_ODR_USED (old_decl
))
8349 instantiate_decl (old_decl
, /*defer_ok=*/true,
8350 /*expl_inst_class_mem_p=*/false);
8356 /* Indicate that the old function template is a partial
8358 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8359 = new_friend_result_template_info
;
8361 gcc_assert (new_template
8362 == most_general_template (new_template
));
8363 gcc_assert (new_template
!= old_decl
);
8365 /* Reassign any specializations already in the hash table
8366 to the new more general template, and add the
8367 additional template args. */
8368 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8372 tree spec
= TREE_VALUE (t
);
8375 elt
.tmpl
= old_decl
;
8376 elt
.args
= DECL_TI_ARGS (spec
);
8377 elt
.spec
= NULL_TREE
;
8379 htab_remove_elt (decl_specializations
, &elt
);
8382 = add_outermost_template_args (new_args
,
8383 DECL_TI_ARGS (spec
));
8385 register_specialization
8386 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8389 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8393 /* The information from NEW_FRIEND has been merged into OLD_DECL
8394 by duplicate_decls. */
8395 new_friend
= old_decl
;
8400 tree context
= DECL_CONTEXT (new_friend
);
8404 template <class T> class C {
8405 template <class U> friend void C1<U>::f (); // case 1
8406 friend void C2<T>::f (); // case 2
8408 we only need to make sure CONTEXT is a complete type for
8409 case 2. To distinguish between the two cases, we note that
8410 CONTEXT of case 1 remains dependent type after tsubst while
8411 this isn't true for case 2. */
8412 ++processing_template_decl
;
8413 dependent_p
= dependent_type_p (context
);
8414 --processing_template_decl
;
8417 && !complete_type_or_else (context
, NULL_TREE
))
8418 return error_mark_node
;
8420 if (COMPLETE_TYPE_P (context
))
8422 /* Check to see that the declaration is really present, and,
8423 possibly obtain an improved declaration. */
8424 tree fn
= check_classfn (context
,
8425 new_friend
, NULL_TREE
);
8435 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8436 template arguments, as for tsubst.
8438 Returns an appropriate tsubst'd friend type or error_mark_node on
8442 tsubst_friend_class (tree friend_tmpl
, tree args
)
8448 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8450 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8451 return TREE_TYPE (t
);
8454 context
= CP_DECL_CONTEXT (friend_tmpl
);
8456 if (context
!= global_namespace
)
8458 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8459 push_nested_namespace (context
);
8461 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8464 /* Look for a class template declaration. We look for hidden names
8465 because two friend declarations of the same template are the
8466 same. For example, in:
8469 template <typename> friend class F;
8471 template <typename> struct B {
8472 template <typename> friend class F;
8475 both F templates are the same. */
8476 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8477 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8479 /* But, if we don't find one, it might be because we're in a
8480 situation like this:
8488 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8489 for `S<int>', not the TEMPLATE_DECL. */
8490 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8492 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8493 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8496 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8498 /* The friend template has already been declared. Just
8499 check to see that the declarations match, and install any new
8500 default parameters. We must tsubst the default parameters,
8501 of course. We only need the innermost template parameters
8502 because that is all that redeclare_class_template will look
8504 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8505 > TMPL_ARGS_DEPTH (args
))
8508 location_t saved_input_location
;
8509 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8510 args
, tf_warning_or_error
);
8512 saved_input_location
= input_location
;
8513 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8514 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8515 input_location
= saved_input_location
;
8519 friend_type
= TREE_TYPE (tmpl
);
8523 /* The friend template has not already been declared. In this
8524 case, the instantiation of the template class will cause the
8525 injection of this template into the global scope. */
8526 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8527 if (tmpl
== error_mark_node
)
8528 return error_mark_node
;
8530 /* The new TMPL is not an instantiation of anything, so we
8531 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8532 the new type because that is supposed to be the corresponding
8533 template decl, i.e., TMPL. */
8534 DECL_USE_TEMPLATE (tmpl
) = 0;
8535 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8536 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8537 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8538 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8540 /* Inject this template into the global scope. */
8541 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8544 if (context
!= global_namespace
)
8546 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8547 pop_nested_namespace (context
);
8549 pop_nested_class ();
8555 /* Returns zero if TYPE cannot be completed later due to circularity.
8556 Otherwise returns one. */
8559 can_complete_type_without_circularity (tree type
)
8561 if (type
== NULL_TREE
|| type
== error_mark_node
)
8563 else if (COMPLETE_TYPE_P (type
))
8565 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8566 return can_complete_type_without_circularity (TREE_TYPE (type
));
8567 else if (CLASS_TYPE_P (type
)
8568 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8574 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8576 /* Apply any attributes which had to be deferred until instantiation
8577 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8578 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8581 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8582 tree args
, tsubst_flags_t complain
, tree in_decl
)
8584 tree last_dep
= NULL_TREE
;
8588 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8589 if (ATTR_IS_DEPENDENT (t
))
8592 attributes
= copy_list (attributes
);
8596 if (DECL_P (*decl_p
))
8598 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8600 p
= &DECL_ATTRIBUTES (*decl_p
);
8603 p
= &TYPE_ATTRIBUTES (*decl_p
);
8607 tree late_attrs
= NULL_TREE
;
8608 tree
*q
= &late_attrs
;
8610 for (*p
= attributes
; *p
; )
8613 if (ATTR_IS_DEPENDENT (t
))
8615 *p
= TREE_CHAIN (t
);
8616 TREE_CHAIN (t
) = NULL_TREE
;
8617 if ((flag_openmp
|| flag_cilkplus
)
8618 && is_attribute_p ("omp declare simd",
8619 get_attribute_name (t
))
8622 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8623 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8625 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8626 clauses
= finish_omp_clauses (clauses
);
8627 tree parms
= DECL_ARGUMENTS (*decl_p
);
8629 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8631 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8633 TREE_VALUE (t
) = NULL_TREE
;
8635 /* If the first attribute argument is an identifier, don't
8636 pass it through tsubst. Attributes like mode, format,
8637 cleanup and several target specific attributes expect it
8639 else if (attribute_takes_identifier_p (get_attribute_name (t
))
8643 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8645 /*integral_constant_expression_p=*/false);
8646 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8648 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8653 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8654 /*integral_constant_expression_p=*/false);
8656 q
= &TREE_CHAIN (t
);
8659 p
= &TREE_CHAIN (t
);
8662 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8666 /* Perform (or defer) access check for typedefs that were referenced
8667 from within the template TMPL code.
8668 This is a subroutine of instantiate_decl and instantiate_class_template.
8669 TMPL is the template to consider and TARGS is the list of arguments of
8673 perform_typedefs_access_check (tree tmpl
, tree targs
)
8675 location_t saved_location
;
8677 qualified_typedef_usage_t
*iter
;
8680 || (!CLASS_TYPE_P (tmpl
)
8681 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8684 saved_location
= input_location
;
8685 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8687 tree type_decl
= iter
->typedef_decl
;
8688 tree type_scope
= iter
->context
;
8690 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8693 if (uses_template_parms (type_decl
))
8694 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8695 if (uses_template_parms (type_scope
))
8696 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8698 /* Make access check error messages point to the location
8699 of the use of the typedef. */
8700 input_location
= iter
->locus
;
8701 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8702 type_decl
, type_decl
,
8703 tf_warning_or_error
);
8705 input_location
= saved_location
;
8709 instantiate_class_template_1 (tree type
)
8711 tree templ
, args
, pattern
, t
, member
;
8715 unsigned int saved_maximum_field_alignment
;
8718 if (type
== error_mark_node
)
8719 return error_mark_node
;
8721 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8722 || uses_template_parms (type
))
8725 /* Figure out which template is being instantiated. */
8726 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8727 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8729 /* Determine what specialization of the original template to
8731 t
= most_specialized_class (type
, tf_warning_or_error
);
8732 if (t
== error_mark_node
)
8734 TYPE_BEING_DEFINED (type
) = 1;
8735 return error_mark_node
;
8739 /* This TYPE is actually an instantiation of a partial
8740 specialization. We replace the innermost set of ARGS with
8741 the arguments appropriate for substitution. For example,
8744 template <class T> struct S {};
8745 template <class T> struct S<T*> {};
8747 and supposing that we are instantiating S<int*>, ARGS will
8748 presently be {int*} -- but we need {int}. */
8749 pattern
= TREE_TYPE (t
);
8750 args
= TREE_PURPOSE (t
);
8754 pattern
= TREE_TYPE (templ
);
8755 args
= CLASSTYPE_TI_ARGS (type
);
8758 /* If the template we're instantiating is incomplete, then clearly
8759 there's nothing we can do. */
8760 if (!COMPLETE_TYPE_P (pattern
))
8763 /* If we've recursively instantiated too many templates, stop. */
8764 if (! push_tinst_level (type
))
8767 /* Now we're really doing the instantiation. Mark the type as in
8768 the process of being defined. */
8769 TYPE_BEING_DEFINED (type
) = 1;
8771 /* We may be in the middle of deferred access check. Disable
8773 push_deferring_access_checks (dk_no_deferred
);
8775 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8777 push_to_top_level ();
8778 /* Use #pragma pack from the template context. */
8779 saved_maximum_field_alignment
= maximum_field_alignment
;
8780 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8782 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8784 /* Set the input location to the most specialized template definition.
8785 This is needed if tsubsting causes an error. */
8786 typedecl
= TYPE_MAIN_DECL (pattern
);
8787 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8788 DECL_SOURCE_LOCATION (typedecl
);
8790 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8791 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8792 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8793 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8794 if (ANON_AGGR_TYPE_P (pattern
))
8795 SET_ANON_AGGR_TYPE_P (type
);
8796 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8798 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8799 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8800 /* Adjust visibility for template arguments. */
8801 determine_visibility (TYPE_MAIN_DECL (type
));
8803 if (CLASS_TYPE_P (type
))
8804 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8806 pbinfo
= TYPE_BINFO (pattern
);
8808 /* We should never instantiate a nested class before its enclosing
8809 class; we need to look up the nested class by name before we can
8810 instantiate it, and that lookup should instantiate the enclosing
8812 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8813 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8815 base_list
= NULL_TREE
;
8816 if (BINFO_N_BASE_BINFOS (pbinfo
))
8822 /* We must enter the scope containing the type, as that is where
8823 the accessibility of types named in dependent bases are
8825 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8827 /* Substitute into each of the bases to determine the actual
8829 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8832 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8833 tree expanded_bases
= NULL_TREE
;
8836 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8839 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8840 args
, tf_error
, NULL_TREE
);
8841 if (expanded_bases
== error_mark_node
)
8844 len
= TREE_VEC_LENGTH (expanded_bases
);
8847 for (idx
= 0; idx
< len
; idx
++)
8850 /* Extract the already-expanded base class. */
8851 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8853 /* Substitute to figure out the base class. */
8854 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8857 if (base
== error_mark_node
)
8860 base_list
= tree_cons (access
, base
, base_list
);
8861 if (BINFO_VIRTUAL_P (pbase_binfo
))
8862 TREE_TYPE (base_list
) = integer_type_node
;
8866 /* The list is now in reverse order; correct that. */
8867 base_list
= nreverse (base_list
);
8870 pop_scope (pushed_scope
);
8872 /* Now call xref_basetypes to set up all the base-class
8874 xref_basetypes (type
, base_list
);
8876 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8877 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8878 args
, tf_error
, NULL_TREE
);
8879 fixup_attribute_variants (type
);
8881 /* Now that our base classes are set up, enter the scope of the
8882 class, so that name lookups into base classes, etc. will work
8883 correctly. This is precisely analogous to what we do in
8884 begin_class_definition when defining an ordinary non-template
8885 class, except we also need to push the enclosing classes. */
8886 push_nested_class (type
);
8888 /* Now members are processed in the order of declaration. */
8889 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8890 member
; member
= TREE_CHAIN (member
))
8892 tree t
= TREE_VALUE (member
);
8894 if (TREE_PURPOSE (member
))
8898 /* Build new CLASSTYPE_NESTED_UTDS. */
8901 bool class_template_p
;
8903 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8904 && TYPE_LANG_SPECIFIC (t
)
8905 && CLASSTYPE_IS_TEMPLATE (t
));
8906 /* If the member is a class template, then -- even after
8907 substitution -- there may be dependent types in the
8908 template argument list for the class. We increment
8909 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8910 that function will assume that no types are dependent
8911 when outside of a template. */
8912 if (class_template_p
)
8913 ++processing_template_decl
;
8914 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8915 if (class_template_p
)
8916 --processing_template_decl
;
8917 if (newtag
== error_mark_node
)
8920 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8922 tree name
= TYPE_IDENTIFIER (t
);
8924 if (class_template_p
)
8925 /* Unfortunately, lookup_template_class sets
8926 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8927 instantiation (i.e., for the type of a member
8928 template class nested within a template class.)
8929 This behavior is required for
8930 maybe_process_partial_specialization to work
8931 correctly, but is not accurate in this case;
8932 the TAG is not an instantiation of anything.
8933 (The corresponding TEMPLATE_DECL is an
8934 instantiation, but the TYPE is not.) */
8935 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8937 /* Now, we call pushtag to put this NEWTAG into the scope of
8938 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8939 pushtag calling push_template_decl. We don't have to do
8940 this for enums because it will already have been done in
8943 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8944 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8947 else if (DECL_DECLARES_FUNCTION_P (t
))
8949 /* Build new TYPE_METHODS. */
8952 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8953 ++processing_template_decl
;
8954 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8955 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8956 --processing_template_decl
;
8957 set_current_access_from_decl (r
);
8958 finish_member_declaration (r
);
8959 /* Instantiate members marked with attribute used. */
8960 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8962 if (TREE_CODE (r
) == FUNCTION_DECL
8963 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8964 cp_check_omp_declare_reduction (r
);
8968 /* Build new TYPE_FIELDS. */
8969 if (TREE_CODE (t
) == STATIC_ASSERT
)
8973 ++c_inhibit_evaluation_warnings
;
8975 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8976 tf_warning_or_error
, NULL_TREE
,
8977 /*integral_constant_expression_p=*/true);
8978 --c_inhibit_evaluation_warnings
;
8980 finish_static_assert (condition
,
8981 STATIC_ASSERT_MESSAGE (t
),
8982 STATIC_ASSERT_SOURCE_LOCATION (t
),
8985 else if (TREE_CODE (t
) != CONST_DECL
)
8988 tree vec
= NULL_TREE
;
8991 /* The file and line for this declaration, to
8992 assist in error message reporting. Since we
8993 called push_tinst_level above, we don't need to
8995 input_location
= DECL_SOURCE_LOCATION (t
);
8997 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8998 ++processing_template_decl
;
8999 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9000 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9001 --processing_template_decl
;
9003 if (TREE_CODE (r
) == TREE_VEC
)
9005 /* A capture pack became multiple fields. */
9007 len
= TREE_VEC_LENGTH (vec
);
9010 for (int i
= 0; i
< len
; ++i
)
9013 r
= TREE_VEC_ELT (vec
, i
);
9018 [t]he initialization (and any associated
9019 side-effects) of a static data member does
9020 not occur unless the static data member is
9021 itself used in a way that requires the
9022 definition of the static data member to
9025 Therefore, we do not substitute into the
9026 initialized for the static data member here. */
9027 finish_static_data_member_decl
9030 /*init_const_expr_p=*/false,
9031 /*asmspec_tree=*/NULL_TREE
,
9033 /* Instantiate members marked with attribute used. */
9034 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9037 else if (TREE_CODE (r
) == FIELD_DECL
)
9039 /* Determine whether R has a valid type and can be
9040 completed later. If R is invalid, then its type
9041 is replaced by error_mark_node. */
9042 tree rtype
= TREE_TYPE (r
);
9043 if (can_complete_type_without_circularity (rtype
))
9044 complete_type (rtype
);
9046 if (!COMPLETE_TYPE_P (rtype
))
9048 cxx_incomplete_type_error (r
, rtype
);
9049 TREE_TYPE (r
) = error_mark_node
;
9053 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9054 such a thing will already have been added to the field
9055 list by tsubst_enum in finish_member_declaration in the
9056 CLASSTYPE_NESTED_UTDS case above. */
9057 if (!(TREE_CODE (r
) == TYPE_DECL
9058 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9059 && DECL_ARTIFICIAL (r
)))
9061 set_current_access_from_decl (r
);
9062 finish_member_declaration (r
);
9070 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9071 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9073 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9075 tree friend_type
= t
;
9076 bool adjust_processing_template_decl
= false;
9078 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9080 /* template <class T> friend class C; */
9081 friend_type
= tsubst_friend_class (friend_type
, args
);
9082 adjust_processing_template_decl
= true;
9084 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9086 /* template <class T> friend class C::D; */
9087 friend_type
= tsubst (friend_type
, args
,
9088 tf_warning_or_error
, NULL_TREE
);
9089 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9090 friend_type
= TREE_TYPE (friend_type
);
9091 adjust_processing_template_decl
= true;
9093 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9094 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9096 /* This could be either
9100 when dependent_type_p is false or
9102 template <class U> friend class T::C;
9105 friend_type
= tsubst (friend_type
, args
,
9106 tf_warning_or_error
, NULL_TREE
);
9107 /* Bump processing_template_decl for correct
9108 dependent_type_p calculation. */
9109 ++processing_template_decl
;
9110 if (dependent_type_p (friend_type
))
9111 adjust_processing_template_decl
= true;
9112 --processing_template_decl
;
9114 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9115 && hidden_name_p (TYPE_NAME (friend_type
)))
9119 where C hasn't been declared yet. Let's lookup name
9120 from namespace scope directly, bypassing any name that
9121 come from dependent base class. */
9122 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9124 /* The call to xref_tag_from_type does injection for friend
9126 push_nested_namespace (ns
);
9128 xref_tag_from_type (friend_type
, NULL_TREE
,
9129 /*tag_scope=*/ts_current
);
9130 pop_nested_namespace (ns
);
9132 else if (uses_template_parms (friend_type
))
9133 /* friend class C<T>; */
9134 friend_type
= tsubst (friend_type
, args
,
9135 tf_warning_or_error
, NULL_TREE
);
9140 where C is already declared or
9142 friend class C<int>;
9144 We don't have to do anything in these cases. */
9146 if (adjust_processing_template_decl
)
9147 /* Trick make_friend_class into realizing that the friend
9148 we're adding is a template, not an ordinary class. It's
9149 important that we use make_friend_class since it will
9150 perform some error-checking and output cross-reference
9152 ++processing_template_decl
;
9154 if (friend_type
!= error_mark_node
)
9155 make_friend_class (type
, friend_type
, /*complain=*/false);
9157 if (adjust_processing_template_decl
)
9158 --processing_template_decl
;
9162 /* Build new DECL_FRIENDLIST. */
9165 /* The file and line for this declaration, to
9166 assist in error message reporting. Since we
9167 called push_tinst_level above, we don't need to
9169 input_location
= DECL_SOURCE_LOCATION (t
);
9171 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9173 ++processing_template_decl
;
9174 push_deferring_access_checks (dk_no_check
);
9177 r
= tsubst_friend_function (t
, args
);
9178 add_friend (type
, r
, /*complain=*/false);
9179 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9181 pop_deferring_access_checks ();
9182 --processing_template_decl
;
9188 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9190 tree decl
= lambda_function (type
);
9193 if (!DECL_TEMPLATE_INFO (decl
)
9194 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9195 instantiate_decl (decl
, false, false);
9197 /* We need to instantiate the capture list from the template
9198 after we've instantiated the closure members, but before we
9199 consider adding the conversion op. Also keep any captures
9200 that may have been added during instantiation of the op(). */
9201 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9203 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9204 args
, tf_warning_or_error
, NULL_TREE
,
9207 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9208 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9210 maybe_add_lambda_conv_op (type
);
9213 gcc_assert (errorcount
);
9216 /* Set the file and line number information to whatever is given for
9217 the class itself. This puts error messages involving generated
9218 implicit functions at a predictable point, and the same point
9219 that would be used for non-template classes. */
9220 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9222 unreverse_member_declarations (type
);
9223 finish_struct_1 (type
);
9224 TYPE_BEING_DEFINED (type
) = 0;
9226 /* We don't instantiate default arguments for member functions. 14.7.1:
9228 The implicit instantiation of a class template specialization causes
9229 the implicit instantiation of the declarations, but not of the
9230 definitions or default arguments, of the class member functions,
9231 member classes, static data members and member templates.... */
9233 /* Some typedefs referenced from within the template code need to be access
9234 checked at template instantiation time, i.e now. These types were
9235 added to the template at parsing time. Let's get those and perform
9236 the access checks then. */
9237 perform_typedefs_access_check (pattern
, args
);
9238 perform_deferred_access_checks (tf_warning_or_error
);
9239 pop_nested_class ();
9240 maximum_field_alignment
= saved_maximum_field_alignment
;
9242 pop_from_top_level ();
9243 pop_deferring_access_checks ();
9246 /* The vtable for a template class can be emitted in any translation
9247 unit in which the class is instantiated. When there is no key
9248 method, however, finish_struct_1 will already have added TYPE to
9249 the keyed_classes list. */
9250 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9251 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9256 /* Wrapper for instantiate_class_template_1. */
9259 instantiate_class_template (tree type
)
9262 timevar_push (TV_TEMPLATE_INST
);
9263 ret
= instantiate_class_template_1 (type
);
9264 timevar_pop (TV_TEMPLATE_INST
);
9269 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9275 else if (TYPE_P (t
))
9276 r
= tsubst (t
, args
, complain
, in_decl
);
9279 if (!(complain
& tf_warning
))
9280 ++c_inhibit_evaluation_warnings
;
9281 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9282 /*integral_constant_expression_p=*/true);
9283 if (!(complain
& tf_warning
))
9284 --c_inhibit_evaluation_warnings
;
9289 /* Given a function parameter pack TMPL_PARM and some function parameters
9290 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9291 and set *SPEC_P to point at the next point in the list. */
9294 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9296 /* Collect all of the extra "packed" parameters into an
9300 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9301 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9302 tree spec_parm
= *spec_p
;
9305 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9307 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9310 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9311 parmvec
= make_tree_vec (len
);
9312 parmtypevec
= make_tree_vec (len
);
9313 spec_parm
= *spec_p
;
9314 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9316 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9317 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9320 /* Build the argument packs. */
9321 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9322 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9323 TREE_TYPE (argpack
) = argtypepack
;
9324 *spec_p
= spec_parm
;
9329 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9330 NONTYPE_ARGUMENT_PACK. */
9333 make_fnparm_pack (tree spec_parm
)
9335 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9338 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9342 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9344 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9345 if (i
>= TREE_VEC_LENGTH (vec
))
9347 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9351 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9354 make_argument_pack_select (tree arg_pack
, unsigned index
)
9356 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9358 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9359 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9364 /* This is a subroutine of tsubst_pack_expansion.
9366 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9367 mechanism to store the (non complete list of) arguments of the
9368 substitution and return a non substituted pack expansion, in order
9369 to wait for when we have enough arguments to really perform the
9373 use_pack_expansion_extra_args_p (tree parm_packs
,
9377 /* If one pack has an expansion and another pack has a normal
9378 argument or if one pack has an empty argument and an another
9379 one hasn't then tsubst_pack_expansion cannot perform the
9380 substitution and need to fall back on the
9381 PACK_EXPANSION_EXTRA mechanism. */
9382 if (parm_packs
== NULL_TREE
)
9384 else if (has_empty_arg
)
9387 bool has_expansion_arg
= false;
9388 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9390 bool has_non_expansion_arg
= false;
9391 for (tree parm_pack
= parm_packs
;
9393 parm_pack
= TREE_CHAIN (parm_pack
))
9395 tree arg
= TREE_VALUE (parm_pack
);
9397 if (argument_pack_element_is_expansion_p (arg
, i
))
9398 has_expansion_arg
= true;
9400 has_non_expansion_arg
= true;
9403 if (has_expansion_arg
&& has_non_expansion_arg
)
9409 /* [temp.variadic]/6 says that:
9411 The instantiation of a pack expansion [...]
9412 produces a list E1,E2, ..., En, where N is the number of elements
9413 in the pack expansion parameters.
9415 This subroutine of tsubst_pack_expansion produces one of these Ei.
9417 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9418 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9419 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9420 INDEX is the index 'i' of the element Ei to produce. ARGS,
9421 COMPLAIN, and IN_DECL are the same parameters as for the
9422 tsubst_pack_expansion function.
9424 The function returns the resulting Ei upon successful completion,
9427 Note that this function possibly modifies the ARGS parameter, so
9428 it's the responsibility of the caller to restore it. */
9431 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9434 tree args
/* This parm gets
9436 tsubst_flags_t complain
,
9440 bool ith_elem_is_expansion
= false;
9442 /* For each parameter pack, change the substitution of the parameter
9443 pack to the ith argument in its argument pack, then expand the
9445 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9447 tree parm
= TREE_PURPOSE (pack
);
9448 tree arg_pack
= TREE_VALUE (pack
);
9449 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9451 ith_elem_is_expansion
|=
9452 argument_pack_element_is_expansion_p (arg_pack
, index
);
9454 /* Select the Ith argument from the pack. */
9455 if (TREE_CODE (parm
) == PARM_DECL
9456 || TREE_CODE (parm
) == FIELD_DECL
)
9460 aps
= make_argument_pack_select (arg_pack
, index
);
9462 register_local_specialization (aps
, parm
);
9465 aps
= retrieve_local_specialization (parm
);
9470 template_parm_level_and_index (parm
, &level
, &idx
);
9474 aps
= make_argument_pack_select (arg_pack
, index
);
9475 /* Update the corresponding argument. */
9476 TMPL_ARG (args
, level
, idx
) = aps
;
9479 /* Re-use the ARGUMENT_PACK_SELECT. */
9480 aps
= TMPL_ARG (args
, level
, idx
);
9482 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9485 /* Substitute into the PATTERN with the (possibly altered)
9487 if (!TYPE_P (pattern
))
9488 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9489 /*integral_constant_expression_p=*/false);
9491 t
= tsubst (pattern
, args
, complain
, in_decl
);
9493 /* If the Ith argument pack element is a pack expansion, then
9494 the Ith element resulting from the substituting is going to
9495 be a pack expansion as well. */
9496 if (ith_elem_is_expansion
)
9497 t
= make_pack_expansion (t
);
9502 /* Substitute ARGS into T, which is an pack expansion
9503 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9504 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9505 (if only a partial substitution could be performed) or
9506 ERROR_MARK_NODE if there was an error. */
9508 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9512 tree pack
, packs
= NULL_TREE
;
9513 bool unsubstituted_packs
= false;
9516 struct pointer_map_t
*saved_local_specializations
= NULL
;
9517 bool need_local_specializations
= false;
9520 gcc_assert (PACK_EXPANSION_P (t
));
9521 pattern
= PACK_EXPANSION_PATTERN (t
);
9523 /* Add in any args remembered from an earlier partial instantiation. */
9524 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9526 levels
= TMPL_ARGS_DEPTH (args
);
9528 /* Determine the argument packs that will instantiate the parameter
9529 packs used in the expansion expression. While we're at it,
9530 compute the number of arguments to be expanded and make sure it
9532 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9533 pack
= TREE_CHAIN (pack
))
9535 tree parm_pack
= TREE_VALUE (pack
);
9536 tree arg_pack
= NULL_TREE
;
9537 tree orig_arg
= NULL_TREE
;
9540 if (TREE_CODE (parm_pack
) == BASES
)
9542 if (BASES_DIRECT (parm_pack
))
9543 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9544 args
, complain
, in_decl
, false));
9546 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9547 args
, complain
, in_decl
, false));
9549 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9551 if (PACK_EXPANSION_LOCAL_P (t
))
9552 arg_pack
= retrieve_local_specialization (parm_pack
);
9555 /* We can't rely on local_specializations for a parameter
9556 name used later in a function declaration (such as in a
9557 late-specified return type). Even if it exists, it might
9558 have the wrong value for a recursive call. Just make a
9559 dummy decl, since it's only used for its type. */
9560 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9561 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9562 /* Partial instantiation of the parm_pack, we can't build
9563 up an argument pack yet. */
9564 arg_pack
= NULL_TREE
;
9566 arg_pack
= make_fnparm_pack (arg_pack
);
9567 need_local_specializations
= true;
9570 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9571 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9575 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9577 if (level
<= levels
)
9578 arg_pack
= TMPL_ARG (args
, level
, idx
);
9581 orig_arg
= arg_pack
;
9582 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9583 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9585 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9586 /* This can only happen if we forget to expand an argument
9587 pack somewhere else. Just return an error, silently. */
9589 result
= make_tree_vec (1);
9590 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9597 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9599 /* Don't bother trying to do a partial substitution with
9600 incomplete packs; we'll try again after deduction. */
9601 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9606 else if (len
!= my_len
)
9608 if (!(complain
& tf_error
))
9609 /* Fail quietly. */;
9610 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9611 error ("mismatched argument pack lengths while expanding "
9615 error ("mismatched argument pack lengths while expanding "
9618 return error_mark_node
;
9621 /* Keep track of the parameter packs and their corresponding
9623 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9624 TREE_TYPE (packs
) = orig_arg
;
9628 /* We can't substitute for this parameter pack. We use a flag as
9629 well as the missing_level counter because function parameter
9630 packs don't have a level. */
9631 unsubstituted_packs
= true;
9635 /* We cannot expand this expansion expression, because we don't have
9636 all of the argument packs we need. */
9637 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9639 /* We got some full packs, but we can't substitute them in until we
9640 have values for all the packs. So remember these until then. */
9642 t
= make_pack_expansion (pattern
);
9643 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9646 else if (unsubstituted_packs
)
9648 /* There were no real arguments, we're just replacing a parameter
9649 pack with another version of itself. Substitute into the
9650 pattern and return a PACK_EXPANSION_*. The caller will need to
9652 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9653 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9654 /*integral_constant_expression_p=*/false);
9656 t
= tsubst (pattern
, args
, complain
, in_decl
);
9657 t
= make_pack_expansion (t
);
9661 gcc_assert (len
>= 0);
9663 if (need_local_specializations
)
9665 /* We're in a late-specified return type, so create our own local
9666 specializations map; the current map is either NULL or (in the
9667 case of recursive unification) might have bindings that we don't
9668 want to use or alter. */
9669 saved_local_specializations
= local_specializations
;
9670 local_specializations
= pointer_map_create ();
9673 /* For each argument in each argument pack, substitute into the
9675 result
= make_tree_vec (len
);
9676 for (i
= 0; i
< len
; ++i
)
9678 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9682 TREE_VEC_ELT (result
, i
) = t
;
9683 if (t
== error_mark_node
)
9685 result
= error_mark_node
;
9690 /* Update ARGS to restore the substitution from parameter packs to
9691 their argument packs. */
9692 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9694 tree parm
= TREE_PURPOSE (pack
);
9696 if (TREE_CODE (parm
) == PARM_DECL
9697 || TREE_CODE (parm
) == FIELD_DECL
)
9698 register_local_specialization (TREE_TYPE (pack
), parm
);
9703 if (TREE_VALUE (pack
) == NULL_TREE
)
9706 template_parm_level_and_index (parm
, &level
, &idx
);
9708 /* Update the corresponding argument. */
9709 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9710 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9713 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9717 if (need_local_specializations
)
9719 pointer_map_destroy (local_specializations
);
9720 local_specializations
= saved_local_specializations
;
9726 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9727 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9728 parameter packs; all parms generated from a function parameter pack will
9729 have the same DECL_PARM_INDEX. */
9732 get_pattern_parm (tree parm
, tree tmpl
)
9734 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9737 if (DECL_ARTIFICIAL (parm
))
9739 for (patparm
= DECL_ARGUMENTS (pattern
);
9740 patparm
; patparm
= DECL_CHAIN (patparm
))
9741 if (DECL_ARTIFICIAL (patparm
)
9742 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9747 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9748 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9749 gcc_assert (DECL_PARM_INDEX (patparm
)
9750 == DECL_PARM_INDEX (parm
));
9756 /* Substitute ARGS into the vector or list of template arguments T. */
9759 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9762 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9765 if (t
== error_mark_node
)
9766 return error_mark_node
;
9768 len
= TREE_VEC_LENGTH (t
);
9769 elts
= XALLOCAVEC (tree
, len
);
9771 for (i
= 0; i
< len
; i
++)
9773 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9776 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9777 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9778 else if (PACK_EXPANSION_P (orig_arg
))
9780 /* Substitute into an expansion expression. */
9781 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9783 if (TREE_CODE (new_arg
) == TREE_VEC
)
9784 /* Add to the expanded length adjustment the number of
9785 expanded arguments. We subtract one from this
9786 measurement, because the argument pack expression
9787 itself is already counted as 1 in
9788 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9789 the argument pack is empty. */
9790 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9792 else if (ARGUMENT_PACK_P (orig_arg
))
9794 /* Substitute into each of the arguments. */
9795 new_arg
= TYPE_P (orig_arg
)
9796 ? cxx_make_type (TREE_CODE (orig_arg
))
9797 : make_node (TREE_CODE (orig_arg
));
9799 SET_ARGUMENT_PACK_ARGS (
9801 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9802 args
, complain
, in_decl
));
9804 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9805 new_arg
= error_mark_node
;
9807 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9808 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9810 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9812 if (TREE_TYPE (new_arg
) == error_mark_node
)
9813 new_arg
= error_mark_node
;
9817 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9819 if (new_arg
== error_mark_node
)
9820 return error_mark_node
;
9823 if (new_arg
!= orig_arg
)
9830 /* Make space for the expanded arguments coming from template
9832 t
= make_tree_vec (len
+ expanded_len_adjust
);
9833 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9834 arguments for a member template.
9835 In that case each TREE_VEC in ORIG_T represents a level of template
9836 arguments, and ORIG_T won't carry any non defaulted argument count.
9837 It will rather be the nested TREE_VECs that will carry one.
9838 In other words, ORIG_T carries a non defaulted argument count only
9839 if it doesn't contain any nested TREE_VEC. */
9840 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9842 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9843 count
+= expanded_len_adjust
;
9844 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9846 for (i
= 0, out
= 0; i
< len
; i
++)
9848 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9849 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9850 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9854 /* Now expand the template argument pack "in place". */
9855 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9856 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9860 TREE_VEC_ELT (t
, out
) = elts
[i
];
9868 /* Return the result of substituting ARGS into the template parameters
9869 given by PARMS. If there are m levels of ARGS and m + n levels of
9870 PARMS, then the result will contain n levels of PARMS. For
9871 example, if PARMS is `template <class T> template <class U>
9872 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9873 result will be `template <int*, double, class V>'. */
9876 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9881 /* When substituting into a template, we must set
9882 PROCESSING_TEMPLATE_DECL as the template parameters may be
9883 dependent if they are based on one-another, and the dependency
9884 predicates are short-circuit outside of templates. */
9885 ++processing_template_decl
;
9887 for (new_parms
= &r
;
9888 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9889 new_parms
= &(TREE_CHAIN (*new_parms
)),
9890 parms
= TREE_CHAIN (parms
))
9893 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9896 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9900 if (parms
== error_mark_node
)
9903 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9905 if (tuple
== error_mark_node
)
9908 TREE_VEC_ELT (new_vec
, i
) =
9909 tsubst_template_parm (tuple
, args
, complain
);
9913 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9914 - TMPL_ARGS_DEPTH (args
)),
9915 new_vec
, NULL_TREE
);
9918 --processing_template_decl
;
9923 /* Return the result of substituting ARGS into one template parameter
9924 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9925 parameter and which TREE_PURPOSE is the default argument of the
9926 template parameter. */
9929 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9931 tree default_value
, parm_decl
;
9933 if (args
== NULL_TREE
9935 || t
== error_mark_node
)
9938 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9940 default_value
= TREE_PURPOSE (t
);
9941 parm_decl
= TREE_VALUE (t
);
9943 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9944 if (TREE_CODE (parm_decl
) == PARM_DECL
9945 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9946 parm_decl
= error_mark_node
;
9947 default_value
= tsubst_template_arg (default_value
, args
,
9948 complain
, NULL_TREE
);
9950 return build_tree_list (default_value
, parm_decl
);
9953 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9954 type T. If T is not an aggregate or enumeration type, it is
9955 handled as if by tsubst. IN_DECL is as for tsubst. If
9956 ENTERING_SCOPE is nonzero, T is the context for a template which
9957 we are presently tsubst'ing. Return the substituted value. */
9960 tsubst_aggr_type (tree t
,
9962 tsubst_flags_t complain
,
9969 switch (TREE_CODE (t
))
9972 if (TYPE_PTRMEMFUNC_P (t
))
9973 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9975 /* Else fall through. */
9978 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9983 int saved_unevaluated_operand
;
9984 int saved_inhibit_evaluation_warnings
;
9986 /* In "sizeof(X<I>)" we need to evaluate "I". */
9987 saved_unevaluated_operand
= cp_unevaluated_operand
;
9988 cp_unevaluated_operand
= 0;
9989 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9990 c_inhibit_evaluation_warnings
= 0;
9992 /* First, determine the context for the type we are looking
9994 context
= TYPE_CONTEXT (t
);
9995 if (context
&& TYPE_P (context
))
9997 context
= tsubst_aggr_type (context
, args
, complain
,
9998 in_decl
, /*entering_scope=*/1);
9999 /* If context is a nested class inside a class template,
10000 it may still need to be instantiated (c++/33959). */
10001 context
= complete_type (context
);
10004 /* Then, figure out what arguments are appropriate for the
10005 type we are trying to find. For example, given:
10007 template <class T> struct S;
10008 template <class T, class U> void f(T, U) { S<U> su; }
10010 and supposing that we are instantiating f<int, double>,
10011 then our ARGS will be {int, double}, but, when looking up
10012 S we only want {double}. */
10013 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10014 complain
, in_decl
);
10015 if (argvec
== error_mark_node
)
10016 r
= error_mark_node
;
10019 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10020 entering_scope
, complain
);
10021 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10024 cp_unevaluated_operand
= saved_unevaluated_operand
;
10025 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10030 /* This is not a template type, so there's nothing to do. */
10034 return tsubst (t
, args
, complain
, in_decl
);
10038 /* Substitute into the default argument ARG (a default argument for
10039 FN), which has the indicated TYPE. */
10042 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10044 tree saved_class_ptr
= NULL_TREE
;
10045 tree saved_class_ref
= NULL_TREE
;
10046 int errs
= errorcount
+ sorrycount
;
10048 /* This can happen in invalid code. */
10049 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10052 /* This default argument came from a template. Instantiate the
10053 default argument here, not in tsubst. In the case of
10062 we must be careful to do name lookup in the scope of S<T>,
10063 rather than in the current class. */
10064 push_access_scope (fn
);
10065 /* The "this" pointer is not valid in a default argument. */
10068 saved_class_ptr
= current_class_ptr
;
10069 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10070 saved_class_ref
= current_class_ref
;
10071 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10074 push_deferring_access_checks(dk_no_deferred
);
10075 /* The default argument expression may cause implicitly defined
10076 member functions to be synthesized, which will result in garbage
10077 collection. We must treat this situation as if we were within
10078 the body of function so as to avoid collecting live data on the
10081 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10082 complain
, NULL_TREE
,
10083 /*integral_constant_expression_p=*/false);
10085 pop_deferring_access_checks();
10087 /* Restore the "this" pointer. */
10090 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10091 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10094 if (errorcount
+sorrycount
> errs
10095 && (complain
& tf_warning_or_error
))
10096 inform (input_location
,
10097 " when instantiating default argument for call to %D", fn
);
10099 /* Make sure the default argument is reasonable. */
10100 arg
= check_default_argument (type
, arg
, complain
);
10102 pop_access_scope (fn
);
10107 /* Substitute into all the default arguments for FN. */
10110 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10115 tmpl_args
= DECL_TI_ARGS (fn
);
10117 /* If this function is not yet instantiated, we certainly don't need
10118 its default arguments. */
10119 if (uses_template_parms (tmpl_args
))
10121 /* Don't do this again for clones. */
10122 if (DECL_CLONED_FUNCTION_P (fn
))
10125 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10127 arg
= TREE_CHAIN (arg
))
10128 if (TREE_PURPOSE (arg
))
10129 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10131 TREE_PURPOSE (arg
),
10135 /* Substitute the ARGS into the T, which is a _DECL. Return the
10136 result of the substitution. Issue error and warning messages under
10137 control of COMPLAIN. */
10140 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10142 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10143 location_t saved_loc
;
10144 tree r
= NULL_TREE
;
10146 hashval_t hash
= 0;
10148 /* Set the filename and linenumber to improve error-reporting. */
10149 saved_loc
= input_location
;
10150 input_location
= DECL_SOURCE_LOCATION (t
);
10152 switch (TREE_CODE (t
))
10154 case TEMPLATE_DECL
:
10156 /* We can get here when processing a member function template,
10157 member class template, or template template parameter. */
10158 tree decl
= DECL_TEMPLATE_RESULT (t
);
10163 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10165 /* Template template parameter is treated here. */
10166 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10167 if (new_type
== error_mark_node
)
10168 RETURN (error_mark_node
);
10169 /* If we get a real template back, return it. This can happen in
10170 the context of most_specialized_class. */
10171 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10175 DECL_CHAIN (r
) = NULL_TREE
;
10176 TREE_TYPE (r
) = new_type
;
10177 DECL_TEMPLATE_RESULT (r
)
10178 = build_decl (DECL_SOURCE_LOCATION (decl
),
10179 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10180 DECL_TEMPLATE_PARMS (r
)
10181 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10183 TYPE_NAME (new_type
) = r
;
10187 /* We might already have an instance of this template.
10188 The ARGS are for the surrounding class type, so the
10189 full args contain the tsubst'd args for the context,
10190 plus the innermost args from the template decl. */
10191 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10192 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10193 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10194 /* Because this is a template, the arguments will still be
10195 dependent, even after substitution. If
10196 PROCESSING_TEMPLATE_DECL is not set, the dependency
10197 predicates will short-circuit. */
10198 ++processing_template_decl
;
10199 full_args
= tsubst_template_args (tmpl_args
, args
,
10200 complain
, in_decl
);
10201 --processing_template_decl
;
10202 if (full_args
== error_mark_node
)
10203 RETURN (error_mark_node
);
10205 /* If this is a default template template argument,
10206 tsubst might not have changed anything. */
10207 if (full_args
== tmpl_args
)
10210 hash
= hash_tmpl_and_args (t
, full_args
);
10211 spec
= retrieve_specialization (t
, full_args
, hash
);
10212 if (spec
!= NULL_TREE
)
10218 /* Make a new template decl. It will be similar to the
10219 original, but will record the current template arguments.
10220 We also create a new function declaration, which is just
10221 like the old one, but points to this new template, rather
10222 than the old one. */
10224 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10225 DECL_CHAIN (r
) = NULL_TREE
;
10227 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10229 if (TREE_CODE (decl
) == TYPE_DECL
10230 && !TYPE_DECL_ALIAS_P (decl
))
10233 ++processing_template_decl
;
10234 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10235 --processing_template_decl
;
10236 if (new_type
== error_mark_node
)
10237 RETURN (error_mark_node
);
10239 TREE_TYPE (r
) = new_type
;
10240 /* For a partial specialization, we need to keep pointing to
10241 the primary template. */
10242 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10243 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10244 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10245 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10246 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10251 ++processing_template_decl
;
10252 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10253 --processing_template_decl
;
10254 if (new_decl
== error_mark_node
)
10255 RETURN (error_mark_node
);
10257 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10258 DECL_TI_TEMPLATE (new_decl
) = r
;
10259 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10260 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10261 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10264 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10265 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10266 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10268 /* The template parameters for this new template are all the
10269 template parameters for the old template, except the
10270 outermost level of parameters. */
10271 DECL_TEMPLATE_PARMS (r
)
10272 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10275 if (PRIMARY_TEMPLATE_P (t
))
10276 DECL_PRIMARY_TEMPLATE (r
) = r
;
10278 if (TREE_CODE (decl
) != TYPE_DECL
)
10279 /* Record this non-type partial instantiation. */
10280 register_specialization (r
, t
,
10281 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10286 case FUNCTION_DECL
:
10289 tree argvec
= NULL_TREE
;
10297 /* Nobody should be tsubst'ing into non-template functions. */
10298 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10300 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10305 /* If T is not dependent, just return it. We have to
10306 increment PROCESSING_TEMPLATE_DECL because
10307 value_dependent_expression_p assumes that nothing is
10308 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10309 ++processing_template_decl
;
10310 dependent_p
= value_dependent_expression_p (t
);
10311 --processing_template_decl
;
10315 /* Calculate the most general template of which R is a
10316 specialization, and the complete set of arguments used to
10318 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10319 argvec
= tsubst_template_args (DECL_TI_ARGS
10320 (DECL_TEMPLATE_RESULT
10321 (DECL_TI_TEMPLATE (t
))),
10322 args
, complain
, in_decl
);
10323 if (argvec
== error_mark_node
)
10324 RETURN (error_mark_node
);
10326 /* Check to see if we already have this specialization. */
10327 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10328 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10336 /* We can see more levels of arguments than parameters if
10337 there was a specialization of a member template, like
10340 template <class T> struct S { template <class U> void f(); }
10341 template <> template <class U> void S<int>::f(U);
10343 Here, we'll be substituting into the specialization,
10344 because that's where we can find the code we actually
10345 want to generate, but we'll have enough arguments for
10346 the most general template.
10348 We also deal with the peculiar case:
10350 template <class T> struct S {
10351 template <class U> friend void f();
10353 template <class U> void f() {}
10355 template void f<double>();
10357 Here, the ARGS for the instantiation of will be {int,
10358 double}. But, we only need as many ARGS as there are
10359 levels of template parameters in CODE_PATTERN. We are
10360 careful not to get fooled into reducing the ARGS in
10363 template <class T> struct S { template <class U> void f(U); }
10364 template <class T> template <> void S<T>::f(int) {}
10366 which we can spot because the pattern will be a
10367 specialization in this case. */
10368 args_depth
= TMPL_ARGS_DEPTH (args
);
10370 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10371 if (args_depth
> parms_depth
10372 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10373 args
= get_innermost_template_args (args
, parms_depth
);
10377 /* This special case arises when we have something like this:
10379 template <class T> struct S {
10380 friend void f<int>(int, double);
10383 Here, the DECL_TI_TEMPLATE for the friend declaration
10384 will be an IDENTIFIER_NODE. We are being called from
10385 tsubst_friend_function, and we want only to create a
10386 new decl (R) with appropriate types so that we can call
10387 determine_specialization. */
10388 gen_tmpl
= NULL_TREE
;
10391 if (DECL_CLASS_SCOPE_P (t
))
10393 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10397 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10398 complain
, t
, /*entering_scope=*/1);
10403 ctx
= DECL_CONTEXT (t
);
10405 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10406 if (type
== error_mark_node
)
10407 RETURN (error_mark_node
);
10409 /* If we hit excessive deduction depth, the type is bogus even if
10410 it isn't error_mark_node, so don't build a decl. */
10411 if (excessive_deduction_depth
)
10412 RETURN (error_mark_node
);
10414 /* We do NOT check for matching decls pushed separately at this
10415 point, as they may not represent instantiations of this
10416 template, and in any case are considered separate under the
10419 DECL_USE_TEMPLATE (r
) = 0;
10420 TREE_TYPE (r
) = type
;
10421 /* Clear out the mangled name and RTL for the instantiation. */
10422 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10423 SET_DECL_RTL (r
, NULL
);
10424 /* Leave DECL_INITIAL set on deleted instantiations. */
10425 if (!DECL_DELETED_FN (r
))
10426 DECL_INITIAL (r
) = NULL_TREE
;
10427 DECL_CONTEXT (r
) = ctx
;
10429 /* OpenMP UDRs have the only argument a reference to the declared
10430 type. We want to diagnose if the declared type is a reference,
10431 which is invalid, but as references to references are usually
10432 quietly merged, diagnose it here. */
10433 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10436 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10437 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10438 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10439 error_at (DECL_SOURCE_LOCATION (t
),
10440 "reference type %qT in "
10441 "%<#pragma omp declare reduction%>", argtype
);
10442 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10443 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10447 if (member
&& DECL_CONV_FN_P (r
))
10448 /* Type-conversion operator. Reconstruct the name, in
10449 case it's the name of one of the template's parameters. */
10450 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10452 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10454 DECL_RESULT (r
) = NULL_TREE
;
10456 TREE_STATIC (r
) = 0;
10457 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10458 DECL_EXTERNAL (r
) = 1;
10459 /* If this is an instantiation of a function with internal
10460 linkage, we already know what object file linkage will be
10461 assigned to the instantiation. */
10462 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10463 DECL_DEFER_OUTPUT (r
) = 0;
10464 DECL_CHAIN (r
) = NULL_TREE
;
10465 DECL_PENDING_INLINE_INFO (r
) = 0;
10466 DECL_PENDING_INLINE_P (r
) = 0;
10467 DECL_SAVED_TREE (r
) = NULL_TREE
;
10468 DECL_STRUCT_FUNCTION (r
) = NULL
;
10470 /* We'll re-clone as appropriate in instantiate_template. */
10471 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10473 /* If we aren't complaining now, return on error before we register
10474 the specialization so that we'll complain eventually. */
10475 if ((complain
& tf_error
) == 0
10476 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10477 && !grok_op_properties (r
, /*complain=*/false))
10478 RETURN (error_mark_node
);
10480 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10481 this in the special friend case mentioned above where
10482 GEN_TMPL is NULL. */
10485 DECL_TEMPLATE_INFO (r
)
10486 = build_template_info (gen_tmpl
, argvec
);
10487 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10490 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10492 /* We instantiated this while substituting into
10493 the type earlier (template/friend54.C). */
10496 /* We're not supposed to instantiate default arguments
10497 until they are called, for a template. But, for a
10500 template <class T> void f ()
10501 { extern void g(int i = T()); }
10503 we should do the substitution when the template is
10504 instantiated. We handle the member function case in
10505 instantiate_class_template since the default arguments
10506 might refer to other members of the class. */
10508 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10509 && !uses_template_parms (argvec
))
10510 tsubst_default_arguments (r
, complain
);
10513 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10515 /* Copy the list of befriending classes. */
10516 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10518 friends
= &TREE_CHAIN (*friends
))
10520 *friends
= copy_node (*friends
);
10521 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10526 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10528 maybe_retrofit_in_chrg (r
);
10529 if (DECL_CONSTRUCTOR_P (r
))
10530 grok_ctor_properties (ctx
, r
);
10531 if (DECL_INHERITED_CTOR_BASE (r
))
10532 deduce_inheriting_ctor (r
);
10533 /* If this is an instantiation of a member template, clone it.
10534 If it isn't, that'll be handled by
10535 clone_constructors_and_destructors. */
10536 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10537 clone_function_decl (r
, /*update_method_vec_p=*/0);
10539 else if ((complain
& tf_error
) != 0
10540 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10541 && !grok_op_properties (r
, /*complain=*/true))
10542 RETURN (error_mark_node
);
10544 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10545 SET_DECL_FRIEND_CONTEXT (r
,
10546 tsubst (DECL_FRIEND_CONTEXT (t
),
10547 args
, complain
, in_decl
));
10549 /* Possibly limit visibility based on template args. */
10550 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10551 if (DECL_VISIBILITY_SPECIFIED (t
))
10553 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10554 DECL_ATTRIBUTES (r
)
10555 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10557 determine_visibility (r
);
10558 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10559 && !processing_template_decl
)
10560 defaulted_late_check (r
);
10562 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10563 args
, complain
, in_decl
);
10569 tree type
= NULL_TREE
;
10571 tree expanded_types
= NULL_TREE
;
10572 tree prev_r
= NULL_TREE
;
10573 tree first_r
= NULL_TREE
;
10575 if (DECL_PACK_P (t
))
10577 /* If there is a local specialization that isn't a
10578 parameter pack, it means that we're doing a "simple"
10579 substitution from inside tsubst_pack_expansion. Just
10580 return the local specialization (which will be a single
10582 tree spec
= retrieve_local_specialization (t
);
10584 && TREE_CODE (spec
) == PARM_DECL
10585 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10588 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10589 the parameters in this function parameter pack. */
10590 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10591 complain
, in_decl
);
10592 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10594 len
= TREE_VEC_LENGTH (expanded_types
);
10596 /* Zero-length parameter packs are boring. Just substitute
10599 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10604 /* All we did was update the type. Make a note of that. */
10605 type
= expanded_types
;
10606 expanded_types
= NULL_TREE
;
10610 /* Loop through all of the parameters we'll build. When T is
10611 a function parameter pack, LEN is the number of expanded
10612 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10614 for (i
= 0; i
< len
; ++i
)
10618 if (DECL_TEMPLATE_PARM_P (t
))
10619 SET_DECL_TEMPLATE_PARM_P (r
);
10621 if (expanded_types
)
10622 /* We're on the Ith parameter of the function parameter
10625 /* Get the Ith type. */
10626 type
= TREE_VEC_ELT (expanded_types
, i
);
10628 /* Rename the parameter to include the index. */
10630 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10633 /* We're dealing with a normal parameter. */
10634 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10636 type
= type_decays_to (type
);
10637 TREE_TYPE (r
) = type
;
10638 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10640 if (DECL_INITIAL (r
))
10642 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10643 DECL_INITIAL (r
) = TREE_TYPE (r
);
10645 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10646 complain
, in_decl
);
10649 DECL_CONTEXT (r
) = NULL_TREE
;
10651 if (!DECL_TEMPLATE_PARM_P (r
))
10652 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10654 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10655 args
, complain
, in_decl
);
10657 /* Keep track of the first new parameter we
10658 generate. That's what will be returned to the
10663 /* Build a proper chain of parameters when substituting
10664 into a function parameter pack. */
10666 DECL_CHAIN (prev_r
) = r
;
10669 /* If cp_unevaluated_operand is set, we're just looking for a
10670 single dummy parameter, so don't keep going. */
10671 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10672 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10673 complain
, DECL_CHAIN (t
));
10675 /* FIRST_R contains the start of the chain we've built. */
10682 tree type
= NULL_TREE
;
10683 tree vec
= NULL_TREE
;
10684 tree expanded_types
= NULL_TREE
;
10687 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10689 /* This field is a lambda capture pack. Return a TREE_VEC of
10690 the expanded fields to instantiate_class_template_1 and
10691 store them in the specializations hash table as a
10692 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10693 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10694 complain
, in_decl
);
10695 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10697 len
= TREE_VEC_LENGTH (expanded_types
);
10698 vec
= make_tree_vec (len
);
10702 /* All we did was update the type. Make a note of that. */
10703 type
= expanded_types
;
10704 expanded_types
= NULL_TREE
;
10708 for (int i
= 0; i
< len
; ++i
)
10711 if (expanded_types
)
10713 type
= TREE_VEC_ELT (expanded_types
, i
);
10715 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10718 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10720 if (type
== error_mark_node
)
10721 RETURN (error_mark_node
);
10722 TREE_TYPE (r
) = type
;
10723 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10725 if (DECL_C_BIT_FIELD (r
))
10726 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10727 non-bit-fields DECL_INITIAL is a non-static data member
10728 initializer, which gets deferred instantiation. */
10730 = tsubst_expr (DECL_INITIAL (t
), args
,
10732 /*integral_constant_expression_p=*/true);
10733 else if (DECL_INITIAL (t
))
10735 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10736 NSDMI in perform_member_init. Still set DECL_INITIAL
10737 so that we know there is one. */
10738 DECL_INITIAL (r
) = void_zero_node
;
10739 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10740 retrofit_lang_decl (r
);
10741 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10743 /* We don't have to set DECL_CONTEXT here; it is set by
10744 finish_member_declaration. */
10745 DECL_CHAIN (r
) = NULL_TREE
;
10747 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10748 args
, complain
, in_decl
);
10751 TREE_VEC_ELT (vec
, i
) = r
;
10757 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10758 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10759 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10760 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10761 TREE_TYPE (pack
) = tpack
;
10762 register_specialization (pack
, t
, args
, false, 0);
10768 /* We reach here only for member using decls. We also need to check
10769 uses_template_parms because DECL_DEPENDENT_P is not set for a
10770 using-declaration that designates a member of the current
10771 instantiation (c++/53549). */
10772 if (DECL_DEPENDENT_P (t
)
10773 || uses_template_parms (USING_DECL_SCOPE (t
)))
10775 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10776 complain
, in_decl
);
10777 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10778 r
= do_class_using_decl (inst_scope
, name
);
10780 r
= error_mark_node
;
10783 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10784 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10790 DECL_CHAIN (r
) = NULL_TREE
;
10797 tree argvec
= NULL_TREE
;
10798 tree gen_tmpl
= NULL_TREE
;
10800 tree tmpl
= NULL_TREE
;
10802 tree type
= NULL_TREE
;
10805 if (TREE_CODE (t
) == TYPE_DECL
10806 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10808 /* If this is the canonical decl, we don't have to
10809 mess with instantiations, and often we can't (for
10810 typename, template type parms and such). Note that
10811 TYPE_NAME is not correct for the above test if
10812 we've copied the type for a typedef. */
10813 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10814 if (type
== error_mark_node
)
10815 RETURN (error_mark_node
);
10816 r
= TYPE_NAME (type
);
10820 /* Check to see if we already have the specialization we
10823 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10825 /* T is a static data member or namespace-scope entity.
10826 We have to substitute into namespace-scope variables
10827 (even though such entities are never templates) because
10830 template <class T> void f() { extern T t; }
10832 where the entity referenced is not known until
10833 instantiation time. */
10835 ctx
= DECL_CONTEXT (t
);
10836 if (DECL_CLASS_SCOPE_P (t
))
10838 ctx
= tsubst_aggr_type (ctx
, args
,
10840 in_decl
, /*entering_scope=*/1);
10841 /* If CTX is unchanged, then T is in fact the
10842 specialization we want. That situation occurs when
10843 referencing a static data member within in its own
10844 class. We can use pointer equality, rather than
10845 same_type_p, because DECL_CONTEXT is always
10847 if (ctx
== DECL_CONTEXT (t
)
10848 && (TREE_CODE (t
) != TYPE_DECL
10849 /* ... unless T is a member template; in which
10850 case our caller can be willing to create a
10851 specialization of that template represented
10853 || !(DECL_TI_TEMPLATE (t
)
10854 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10860 tmpl
= DECL_TI_TEMPLATE (t
);
10861 gen_tmpl
= most_general_template (tmpl
);
10862 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10863 if (argvec
== error_mark_node
)
10864 RETURN (error_mark_node
);
10865 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10866 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10871 /* A local variable. */
10873 /* Subsequent calls to pushdecl will fill this in. */
10875 spec
= retrieve_local_specialization (t
);
10877 /* If we already have the specialization we need, there is
10878 nothing more to do. */
10885 /* Create a new node for the specialization we need. */
10887 if (type
== NULL_TREE
)
10889 if (is_typedef_decl (t
))
10890 type
= DECL_ORIGINAL_TYPE (t
);
10892 type
= TREE_TYPE (t
);
10894 && VAR_HAD_UNKNOWN_BOUND (t
)
10895 && type
!= error_mark_node
)
10896 type
= strip_array_domain (type
);
10897 type
= tsubst (type
, args
, complain
, in_decl
);
10901 /* Even if the original location is out of scope, the
10902 newly substituted one is not. */
10903 DECL_DEAD_FOR_LOCAL (r
) = 0;
10904 DECL_INITIALIZED_P (r
) = 0;
10905 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10906 if (type
== error_mark_node
)
10907 RETURN (error_mark_node
);
10908 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10910 /* It may seem that this case cannot occur, since:
10915 declares a function, not a variable. However:
10918 template <typename T> void g() { T t; }
10919 template void g<f>();
10921 is an attempt to declare a variable with function
10923 error ("variable %qD has function type",
10924 /* R is not yet sufficiently initialized, so we
10925 just use its name. */
10927 RETURN (error_mark_node
);
10929 type
= complete_type (type
);
10930 /* Wait until cp_finish_decl to set this again, to handle
10931 circular dependency (template/instantiate6.C). */
10932 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10933 type
= check_var_type (DECL_NAME (r
), type
);
10935 if (DECL_HAS_VALUE_EXPR_P (t
))
10937 tree ve
= DECL_VALUE_EXPR (t
);
10938 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10939 /*constant_expression_p=*/false);
10940 if (REFERENCE_REF_P (ve
))
10942 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10943 ve
= TREE_OPERAND (ve
, 0);
10945 SET_DECL_VALUE_EXPR (r
, ve
);
10948 else if (DECL_SELF_REFERENCE_P (t
))
10949 SET_DECL_SELF_REFERENCE_P (r
);
10950 TREE_TYPE (r
) = type
;
10951 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10952 DECL_CONTEXT (r
) = ctx
;
10953 /* Clear out the mangled name and RTL for the instantiation. */
10954 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10955 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10956 SET_DECL_RTL (r
, NULL
);
10957 /* The initializer must not be expanded until it is required;
10958 see [temp.inst]. */
10959 DECL_INITIAL (r
) = NULL_TREE
;
10960 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10961 SET_DECL_RTL (r
, NULL
);
10962 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10965 /* Possibly limit visibility based on template args. */
10966 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10967 if (DECL_VISIBILITY_SPECIFIED (t
))
10969 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10970 DECL_ATTRIBUTES (r
)
10971 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10973 determine_visibility (r
);
10978 /* A static data member declaration is always marked
10979 external when it is declared in-class, even if an
10980 initializer is present. We mimic the non-template
10981 processing here. */
10982 DECL_EXTERNAL (r
) = 1;
10984 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10985 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10986 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10988 else if (cp_unevaluated_operand
)
10989 gcc_unreachable ();
10991 register_local_specialization (r
, t
);
10993 DECL_CHAIN (r
) = NULL_TREE
;
10995 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10997 args
, complain
, in_decl
);
10999 /* Preserve a typedef that names a type. */
11000 if (is_typedef_decl (r
))
11002 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11003 set_underlying_type (r
);
11006 layout_decl (r
, 0);
11011 gcc_unreachable ();
11016 /* Restore the file and line information. */
11017 input_location
= saved_loc
;
11022 /* Substitute into the ARG_TYPES of a function type.
11023 If END is a TREE_CHAIN, leave it and any following types
11027 tsubst_arg_types (tree arg_types
,
11030 tsubst_flags_t complain
,
11033 tree remaining_arg_types
;
11034 tree type
= NULL_TREE
;
11036 tree expanded_args
= NULL_TREE
;
11039 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11042 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11043 args
, end
, complain
, in_decl
);
11044 if (remaining_arg_types
== error_mark_node
)
11045 return error_mark_node
;
11047 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11049 /* For a pack expansion, perform substitution on the
11050 entire expression. Later on, we'll handle the arguments
11052 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11053 args
, complain
, in_decl
);
11055 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11056 /* So that we'll spin through the parameters, one by one. */
11057 i
= TREE_VEC_LENGTH (expanded_args
);
11060 /* We only partially substituted into the parameter
11061 pack. Our type is TYPE_PACK_EXPANSION. */
11062 type
= expanded_args
;
11063 expanded_args
= NULL_TREE
;
11071 type
= TREE_VEC_ELT (expanded_args
, i
);
11073 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11075 if (type
== error_mark_node
)
11076 return error_mark_node
;
11077 if (VOID_TYPE_P (type
))
11079 if (complain
& tf_error
)
11081 error ("invalid parameter type %qT", type
);
11083 error ("in declaration %q+D", in_decl
);
11085 return error_mark_node
;
11088 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11089 return error_mark_node
;
11091 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11092 top-level qualifiers as required. */
11093 type
= cv_unqualified (type_decays_to (type
));
11095 /* We do not substitute into default arguments here. The standard
11096 mandates that they be instantiated only when needed, which is
11097 done in build_over_call. */
11098 default_arg
= TREE_PURPOSE (arg_types
);
11100 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11102 /* We've instantiated a template before its default arguments
11103 have been parsed. This can happen for a nested template
11104 class, and is not an error unless we require the default
11105 argument in a call of this function. */
11106 remaining_arg_types
=
11107 tree_cons (default_arg
, type
, remaining_arg_types
);
11108 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11111 remaining_arg_types
=
11112 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11115 return remaining_arg_types
;
11118 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11119 *not* handle the exception-specification for FNTYPE, because the
11120 initial substitution of explicitly provided template parameters
11121 during argument deduction forbids substitution into the
11122 exception-specification:
11126 All references in the function type of the function template to the
11127 corresponding template parameters are replaced by the specified tem-
11128 plate argument values. If a substitution in a template parameter or
11129 in the function type of the function template results in an invalid
11130 type, type deduction fails. [Note: The equivalent substitution in
11131 exception specifications is done only when the function is instanti-
11132 ated, at which point a program is ill-formed if the substitution
11133 results in an invalid type.] */
11136 tsubst_function_type (tree t
,
11138 tsubst_flags_t complain
,
11145 /* The TYPE_CONTEXT is not used for function/method types. */
11146 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11148 /* Substitute the return type. */
11149 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11150 if (return_type
== error_mark_node
)
11151 return error_mark_node
;
11152 /* DR 486 clarifies that creation of a function type with an
11153 invalid return type is a deduction failure. */
11154 if (TREE_CODE (return_type
) == ARRAY_TYPE
11155 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11157 if (complain
& tf_error
)
11159 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11160 error ("function returning an array");
11162 error ("function returning a function");
11164 return error_mark_node
;
11167 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11168 return error_mark_node
;
11170 /* Substitute the argument types. */
11171 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11172 complain
, in_decl
);
11173 if (arg_types
== error_mark_node
)
11174 return error_mark_node
;
11176 /* Construct a new type node and return it. */
11177 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11179 fntype
= build_function_type (return_type
, arg_types
);
11180 fntype
= apply_memfn_quals (fntype
,
11181 type_memfn_quals (t
),
11182 type_memfn_rqual (t
));
11186 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11187 if (! MAYBE_CLASS_TYPE_P (r
))
11191 Type deduction may fail for any of the following
11194 -- Attempting to create "pointer to member of T" when T
11195 is not a class type. */
11196 if (complain
& tf_error
)
11197 error ("creating pointer to member function of non-class type %qT",
11199 return error_mark_node
;
11202 fntype
= build_method_type_directly (r
, return_type
,
11203 TREE_CHAIN (arg_types
));
11204 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11206 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11211 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11212 ARGS into that specification, and return the substituted
11213 specification. If there is no specification, return NULL_TREE. */
11216 tsubst_exception_specification (tree fntype
,
11218 tsubst_flags_t complain
,
11225 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11226 new_specs
= NULL_TREE
;
11227 if (specs
&& TREE_PURPOSE (specs
))
11229 /* A noexcept-specifier. */
11230 tree expr
= TREE_PURPOSE (specs
);
11231 if (TREE_CODE (expr
) == INTEGER_CST
)
11235 /* Defer instantiation of noexcept-specifiers to avoid
11236 excessive instantiations (c++/49107). */
11237 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11238 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11240 /* We already partially instantiated this member template,
11241 so combine the new args with the old. */
11242 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11243 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11244 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11245 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11249 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11250 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11254 new_specs
= tsubst_copy_and_build
11255 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11256 /*integral_constant_expression_p=*/true);
11257 new_specs
= build_noexcept_spec (new_specs
, complain
);
11261 if (! TREE_VALUE (specs
))
11268 tree expanded_specs
= NULL_TREE
;
11270 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11272 /* Expand the pack expansion type. */
11273 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11277 if (expanded_specs
== error_mark_node
)
11278 return error_mark_node
;
11279 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11280 len
= TREE_VEC_LENGTH (expanded_specs
);
11283 /* We're substituting into a member template, so
11284 we got a TYPE_PACK_EXPANSION back. Add that
11285 expansion and move on. */
11286 gcc_assert (TREE_CODE (expanded_specs
)
11287 == TYPE_PACK_EXPANSION
);
11288 new_specs
= add_exception_specifier (new_specs
,
11291 specs
= TREE_CHAIN (specs
);
11296 for (i
= 0; i
< len
; ++i
)
11298 if (expanded_specs
)
11299 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11301 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11302 if (spec
== error_mark_node
)
11304 new_specs
= add_exception_specifier (new_specs
, spec
,
11308 specs
= TREE_CHAIN (specs
);
11314 /* Take the tree structure T and replace template parameters used
11315 therein with the argument vector ARGS. IN_DECL is an associated
11316 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11317 Issue error and warning messages under control of COMPLAIN. Note
11318 that we must be relatively non-tolerant of extensions here, in
11319 order to preserve conformance; if we allow substitutions that
11320 should not be allowed, we may allow argument deductions that should
11321 not succeed, and therefore report ambiguous overload situations
11322 where there are none. In theory, we could allow the substitution,
11323 but indicate that it should have failed, and allow our caller to
11324 make sure that the right thing happens, but we don't try to do this
11327 This function is used for dealing with types, decls and the like;
11328 for expressions, use tsubst_expr or tsubst_copy. */
11331 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11333 enum tree_code code
;
11334 tree type
, r
= NULL_TREE
;
11336 if (t
== NULL_TREE
|| t
== error_mark_node
11337 || t
== integer_type_node
11338 || t
== void_type_node
11339 || t
== char_type_node
11340 || t
== unknown_type_node
11341 || TREE_CODE (t
) == NAMESPACE_DECL
11342 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11346 return tsubst_decl (t
, args
, complain
);
11348 if (args
== NULL_TREE
)
11351 code
= TREE_CODE (t
);
11353 if (code
== IDENTIFIER_NODE
)
11354 type
= IDENTIFIER_TYPE_VALUE (t
);
11356 type
= TREE_TYPE (t
);
11358 gcc_assert (type
!= unknown_type_node
);
11360 /* Reuse typedefs. We need to do this to handle dependent attributes,
11361 such as attribute aligned. */
11363 && typedef_variant_p (t
))
11365 tree decl
= TYPE_NAME (t
);
11367 if (alias_template_specialization_p (t
))
11369 /* DECL represents an alias template and we want to
11371 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11372 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11373 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11375 else if (DECL_CLASS_SCOPE_P (decl
)
11376 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11377 && uses_template_parms (DECL_CONTEXT (decl
)))
11379 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11380 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11381 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11383 else if (DECL_FUNCTION_SCOPE_P (decl
)
11384 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11385 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11386 r
= retrieve_local_specialization (decl
);
11388 /* The typedef is from a non-template context. */
11394 r
= cp_build_qualified_type_real
11395 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11396 complain
| tf_ignore_bad_quals
);
11401 /* We don't have an instantiation yet, so drop the typedef. */
11402 int quals
= cp_type_quals (t
);
11403 t
= DECL_ORIGINAL_TYPE (decl
);
11404 t
= cp_build_qualified_type_real (t
, quals
,
11405 complain
| tf_ignore_bad_quals
);
11410 && code
!= TYPENAME_TYPE
11411 && code
!= TEMPLATE_TYPE_PARM
11412 && code
!= IDENTIFIER_NODE
11413 && code
!= FUNCTION_TYPE
11414 && code
!= METHOD_TYPE
)
11415 type
= tsubst (type
, args
, complain
, in_decl
);
11416 if (type
== error_mark_node
)
11417 return error_mark_node
;
11423 case ENUMERAL_TYPE
:
11424 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11425 /*entering_scope=*/0);
11428 case IDENTIFIER_NODE
:
11439 if (t
== integer_type_node
)
11442 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11443 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11447 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11449 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11450 /*integral_constant_expression_p=*/false);
11452 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11454 if (TREE_CODE (max
) == NOP_EXPR
11455 && TREE_SIDE_EFFECTS (omax
)
11456 && !TREE_TYPE (max
))
11457 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11459 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11460 with TREE_SIDE_EFFECTS that indicates this is not an integral
11461 constant expression. */
11462 if (processing_template_decl
11463 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11465 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11466 TREE_SIDE_EFFECTS (max
) = 1;
11469 return compute_array_index_type (NULL_TREE
, max
, complain
);
11472 case TEMPLATE_TYPE_PARM
:
11473 case TEMPLATE_TEMPLATE_PARM
:
11474 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11475 case TEMPLATE_PARM_INDEX
:
11480 tree arg
= NULL_TREE
;
11484 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11485 template_parm_level_and_index (t
, &level
, &idx
);
11487 levels
= TMPL_ARGS_DEPTH (args
);
11488 if (level
<= levels
)
11490 arg
= TMPL_ARG (args
, level
, idx
);
11492 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11494 /* See through ARGUMENT_PACK_SELECT arguments. */
11495 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11496 /* If the selected argument is an expansion E, that most
11497 likely means we were called from
11498 gen_elem_of_pack_expansion_instantiation during the
11499 substituting of pack an argument pack (which Ith
11500 element is a pack expansion, where I is
11501 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11502 In this case, the Ith element resulting from this
11503 substituting is going to be a pack expansion, which
11504 pattern is the pattern of E. Let's return the
11506 gen_elem_of_pack_expansion_instantiation will
11507 build the resulting pack expansion from it. */
11508 if (PACK_EXPANSION_P (arg
))
11509 arg
= PACK_EXPANSION_PATTERN (arg
);
11513 if (arg
== error_mark_node
)
11514 return error_mark_node
;
11515 else if (arg
!= NULL_TREE
)
11517 if (ARGUMENT_PACK_P (arg
))
11518 /* If ARG is an argument pack, we don't actually want to
11519 perform a substitution here, because substitutions
11520 for argument packs are only done
11521 element-by-element. We can get to this point when
11522 substituting the type of a non-type template
11523 parameter pack, when that type actually contains
11524 template parameter packs from an outer template, e.g.,
11526 template<typename... Types> struct A {
11527 template<Types... Values> struct B { };
11531 if (code
== TEMPLATE_TYPE_PARM
)
11534 gcc_assert (TYPE_P (arg
));
11536 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11538 return cp_build_qualified_type_real
11539 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11541 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11543 /* We are processing a type constructed from a
11544 template template parameter. */
11545 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11546 args
, complain
, in_decl
);
11547 if (argvec
== error_mark_node
)
11548 return error_mark_node
;
11550 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11551 || TREE_CODE (arg
) == TEMPLATE_DECL
11552 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11554 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11555 /* Consider this code:
11557 template <template <class> class Template>
11559 template <class Arg> using Bind = Template<Arg>;
11562 template <template <class> class Template, class Arg>
11563 using Instantiate = Template<Arg>; //#0
11565 template <template <class> class Template,
11568 Instantiate<Internal<Template>::template Bind,
11571 When #1 is parsed, the
11572 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11573 parameter `Template' in #0 matches the
11574 UNBOUND_CLASS_TEMPLATE representing the argument
11575 `Internal<Template>::template Bind'; We then want
11576 to assemble the type `Bind<Argument>' that can't
11577 be fully created right now, because
11578 `Internal<Template>' not being complete, the Bind
11579 template cannot be looked up in that context. So
11580 we need to "store" `Bind<Argument>' for later
11581 when the context of Bind becomes complete. Let's
11582 store that in a TYPENAME_TYPE. */
11583 return make_typename_type (TYPE_CONTEXT (arg
),
11584 build_nt (TEMPLATE_ID_EXPR
,
11585 TYPE_IDENTIFIER (arg
),
11590 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11591 are resolving nested-types in the signature of a
11592 member function templates. Otherwise ARG is a
11593 TEMPLATE_DECL and is the real template to be
11595 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11596 arg
= TYPE_NAME (arg
);
11598 r
= lookup_template_class (arg
,
11600 DECL_CONTEXT (arg
),
11601 /*entering_scope=*/0,
11603 return cp_build_qualified_type_real
11604 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11607 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11608 return convert_from_reference (unshare_expr (arg
));
11612 /* This can happen during the attempted tsubst'ing in
11613 unify. This means that we don't yet have any information
11614 about the template parameter in question. */
11617 /* Early in template argument deduction substitution, we don't
11618 want to reduce the level of 'auto', or it will be confused
11619 with a normal template parm in subsequent deduction. */
11620 if (is_auto (t
) && (complain
& tf_partial
))
11623 /* If we get here, we must have been looking at a parm for a
11624 more deeply nested template. Make a new version of this
11625 template parameter, but with a lower level. */
11628 case TEMPLATE_TYPE_PARM
:
11629 case TEMPLATE_TEMPLATE_PARM
:
11630 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11631 if (cp_type_quals (t
))
11633 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11634 r
= cp_build_qualified_type_real
11635 (r
, cp_type_quals (t
),
11636 complain
| (code
== TEMPLATE_TYPE_PARM
11637 ? tf_ignore_bad_quals
: 0));
11642 TEMPLATE_TYPE_PARM_INDEX (r
)
11643 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11644 r
, levels
, args
, complain
);
11645 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11646 TYPE_MAIN_VARIANT (r
) = r
;
11647 TYPE_POINTER_TO (r
) = NULL_TREE
;
11648 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11650 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11651 /* We have reduced the level of the template
11652 template parameter, but not the levels of its
11653 template parameters, so canonical_type_parameter
11654 will not be able to find the canonical template
11655 template parameter for this level. Thus, we
11656 require structural equality checking to compare
11657 TEMPLATE_TEMPLATE_PARMs. */
11658 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11659 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11660 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11662 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11664 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11666 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11667 complain
, in_decl
);
11668 if (argvec
== error_mark_node
)
11669 return error_mark_node
;
11671 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11672 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11677 case TEMPLATE_PARM_INDEX
:
11678 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11682 gcc_unreachable ();
11690 tree purpose
, value
, chain
;
11692 if (t
== void_list_node
)
11695 purpose
= TREE_PURPOSE (t
);
11698 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11699 if (purpose
== error_mark_node
)
11700 return error_mark_node
;
11702 value
= TREE_VALUE (t
);
11705 value
= tsubst (value
, args
, complain
, in_decl
);
11706 if (value
== error_mark_node
)
11707 return error_mark_node
;
11709 chain
= TREE_CHAIN (t
);
11710 if (chain
&& chain
!= void_type_node
)
11712 chain
= tsubst (chain
, args
, complain
, in_decl
);
11713 if (chain
== error_mark_node
)
11714 return error_mark_node
;
11716 if (purpose
== TREE_PURPOSE (t
)
11717 && value
== TREE_VALUE (t
)
11718 && chain
== TREE_CHAIN (t
))
11720 return hash_tree_cons (purpose
, value
, chain
);
11724 /* We should never be tsubsting a binfo. */
11725 gcc_unreachable ();
11728 /* A vector of template arguments. */
11729 gcc_assert (!type
);
11730 return tsubst_template_args (t
, args
, complain
, in_decl
);
11733 case REFERENCE_TYPE
:
11735 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11740 Type deduction may fail for any of the following
11743 -- Attempting to create a pointer to reference type.
11744 -- Attempting to create a reference to a reference type or
11745 a reference to void.
11747 Core issue 106 says that creating a reference to a reference
11748 during instantiation is no longer a cause for failure. We
11749 only enforce this check in strict C++98 mode. */
11750 if ((TREE_CODE (type
) == REFERENCE_TYPE
11751 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11752 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11754 static location_t last_loc
;
11756 /* We keep track of the last time we issued this error
11757 message to avoid spewing a ton of messages during a
11758 single bad template instantiation. */
11759 if (complain
& tf_error
11760 && last_loc
!= input_location
)
11762 if (VOID_TYPE_P (type
))
11763 error ("forming reference to void");
11764 else if (code
== POINTER_TYPE
)
11765 error ("forming pointer to reference type %qT", type
);
11767 error ("forming reference to reference type %qT", type
);
11768 last_loc
= input_location
;
11771 return error_mark_node
;
11773 else if (TREE_CODE (type
) == FUNCTION_TYPE
11774 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11775 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11777 if (complain
& tf_error
)
11779 if (code
== POINTER_TYPE
)
11780 error ("forming pointer to qualified function type %qT",
11783 error ("forming reference to qualified function type %qT",
11786 return error_mark_node
;
11788 else if (code
== POINTER_TYPE
)
11790 r
= build_pointer_type (type
);
11791 if (TREE_CODE (type
) == METHOD_TYPE
)
11792 r
= build_ptrmemfunc_type (r
);
11794 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11795 /* In C++0x, during template argument substitution, when there is an
11796 attempt to create a reference to a reference type, reference
11797 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11799 "If a template-argument for a template-parameter T names a type
11800 that is a reference to a type A, an attempt to create the type
11801 'lvalue reference to cv T' creates the type 'lvalue reference to
11802 A,' while an attempt to create the type type rvalue reference to
11803 cv T' creates the type T"
11805 r
= cp_build_reference_type
11807 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11809 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11810 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11812 if (cxx_dialect
>= cxx1y
11813 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11814 && array_of_runtime_bound_p (type
))
11816 if (complain
& tf_warning_or_error
)
11818 (input_location
, OPT_Wvla
,
11819 code
== REFERENCE_TYPE
11820 ? G_("cannot declare reference to array of runtime bound")
11821 : G_("cannot declare pointer to array of runtime bound"));
11823 r
= error_mark_node
;
11826 if (r
!= error_mark_node
)
11827 /* Will this ever be needed for TYPE_..._TO values? */
11834 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11835 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11839 Type deduction may fail for any of the following
11842 -- Attempting to create "pointer to member of T" when T
11843 is not a class type. */
11844 if (complain
& tf_error
)
11845 error ("creating pointer to member of non-class type %qT", r
);
11846 return error_mark_node
;
11848 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11850 if (complain
& tf_error
)
11851 error ("creating pointer to member reference type %qT", type
);
11852 return error_mark_node
;
11854 if (VOID_TYPE_P (type
))
11856 if (complain
& tf_error
)
11857 error ("creating pointer to member of type void");
11858 return error_mark_node
;
11860 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11861 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11863 /* The type of the implicit object parameter gets its
11864 cv-qualifiers from the FUNCTION_TYPE. */
11867 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11868 type_memfn_rqual (type
));
11869 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11870 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11874 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11878 case FUNCTION_TYPE
:
11883 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11884 if (fntype
== error_mark_node
)
11885 return error_mark_node
;
11887 /* Substitute the exception specification. */
11888 specs
= tsubst_exception_specification (t
, args
, complain
,
11889 in_decl
, /*defer_ok*/true);
11890 if (specs
== error_mark_node
)
11891 return error_mark_node
;
11893 fntype
= build_exception_variant (fntype
, specs
);
11898 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11899 if (domain
== error_mark_node
)
11900 return error_mark_node
;
11902 /* As an optimization, we avoid regenerating the array type if
11903 it will obviously be the same as T. */
11904 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11907 /* These checks should match the ones in grokdeclarator.
11911 The deduction may fail for any of the following reasons:
11913 -- Attempting to create an array with an element type that
11914 is void, a function type, or a reference type, or [DR337]
11915 an abstract class type. */
11916 if (VOID_TYPE_P (type
)
11917 || TREE_CODE (type
) == FUNCTION_TYPE
11918 || TREE_CODE (type
) == REFERENCE_TYPE
)
11920 if (complain
& tf_error
)
11921 error ("creating array of %qT", type
);
11922 return error_mark_node
;
11925 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11926 return error_mark_node
;
11928 r
= build_cplus_array_type (type
, domain
);
11930 if (TYPE_USER_ALIGN (t
))
11932 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11933 TYPE_USER_ALIGN (r
) = 1;
11939 case TYPENAME_TYPE
:
11941 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11942 in_decl
, /*entering_scope=*/1);
11943 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11944 complain
, in_decl
);
11946 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11947 return error_mark_node
;
11949 if (!MAYBE_CLASS_TYPE_P (ctx
))
11951 if (complain
& tf_error
)
11952 error ("%qT is not a class, struct, or union type", ctx
);
11953 return error_mark_node
;
11955 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11957 /* Normally, make_typename_type does not require that the CTX
11958 have complete type in order to allow things like:
11960 template <class T> struct S { typename S<T>::X Y; };
11962 But, such constructs have already been resolved by this
11963 point, so here CTX really should have complete type, unless
11964 it's a partial instantiation. */
11965 ctx
= complete_type (ctx
);
11966 if (!COMPLETE_TYPE_P (ctx
))
11968 if (complain
& tf_error
)
11969 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11970 return error_mark_node
;
11974 f
= make_typename_type (ctx
, f
, typename_type
,
11975 complain
| tf_keep_type_decl
);
11976 if (f
== error_mark_node
)
11978 if (TREE_CODE (f
) == TYPE_DECL
)
11980 complain
|= tf_ignore_bad_quals
;
11984 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11986 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11988 if (complain
& tf_error
)
11989 error ("%qT resolves to %qT, which is not an enumeration type",
11992 return error_mark_node
;
11994 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11996 if (complain
& tf_error
)
11997 error ("%qT resolves to %qT, which is is not a class type",
12000 return error_mark_node
;
12004 return cp_build_qualified_type_real
12005 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12008 case UNBOUND_CLASS_TEMPLATE
:
12010 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12011 in_decl
, /*entering_scope=*/1);
12012 tree name
= TYPE_IDENTIFIER (t
);
12013 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12015 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12016 return error_mark_node
;
12019 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12020 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12027 ++cp_unevaluated_operand
;
12028 ++c_inhibit_evaluation_warnings
;
12030 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12032 /*integral_constant_expression_p=*/false);
12034 --cp_unevaluated_operand
;
12035 --c_inhibit_evaluation_warnings
;
12037 type
= finish_typeof (type
);
12038 return cp_build_qualified_type_real (type
,
12040 | cp_type_quals (type
),
12044 case DECLTYPE_TYPE
:
12048 ++cp_unevaluated_operand
;
12049 ++c_inhibit_evaluation_warnings
;
12051 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12052 complain
|tf_decltype
, in_decl
,
12053 /*function_p*/false,
12054 /*integral_constant_expression*/false);
12056 --cp_unevaluated_operand
;
12057 --c_inhibit_evaluation_warnings
;
12059 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12060 type
= lambda_capture_field_type (type
,
12061 DECLTYPE_FOR_INIT_CAPTURE (t
));
12062 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12063 type
= lambda_proxy_type (type
);
12066 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12067 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12069 /* In a template ~id could be either a complement expression
12070 or an unqualified-id naming a destructor; if instantiating
12071 it produces an expression, it's not an id-expression or
12074 type
= finish_decltype_type (type
, id
, complain
);
12076 return cp_build_qualified_type_real (type
,
12078 | cp_type_quals (type
),
12082 case UNDERLYING_TYPE
:
12084 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12085 complain
, in_decl
);
12086 return finish_underlying_type (type
);
12089 case TYPE_ARGUMENT_PACK
:
12090 case NONTYPE_ARGUMENT_PACK
:
12092 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12094 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12098 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12100 /* For template nontype argument packs, also substitute into
12102 if (code
== NONTYPE_ARGUMENT_PACK
)
12103 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12121 /* We should use one of the expression tsubsts for these codes. */
12122 gcc_unreachable ();
12125 sorry ("use of %qs in template", get_tree_code_name (code
));
12126 return error_mark_node
;
12130 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12131 type of the expression on the left-hand side of the "." or "->"
12135 tsubst_baselink (tree baselink
, tree object_type
,
12136 tree args
, tsubst_flags_t complain
, tree in_decl
)
12139 tree qualifying_scope
;
12142 tree template_args
= 0;
12143 bool template_id_p
= false;
12144 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12146 /* A baselink indicates a function from a base class. Both the
12147 BASELINK_ACCESS_BINFO and the base class referenced may
12148 indicate bases of the template class, rather than the
12149 instantiated class. In addition, lookups that were not
12150 ambiguous before may be ambiguous now. Therefore, we perform
12151 the lookup again. */
12152 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12153 qualifying_scope
= tsubst (qualifying_scope
, args
,
12154 complain
, in_decl
);
12155 fns
= BASELINK_FUNCTIONS (baselink
);
12156 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12157 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12159 template_id_p
= true;
12160 template_args
= TREE_OPERAND (fns
, 1);
12161 fns
= TREE_OPERAND (fns
, 0);
12163 template_args
= tsubst_template_args (template_args
, args
,
12164 complain
, in_decl
);
12166 name
= DECL_NAME (get_first_fn (fns
));
12167 if (IDENTIFIER_TYPENAME_P (name
))
12168 name
= mangle_conv_op_name_for_type (optype
);
12169 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12171 return error_mark_node
;
12173 /* If lookup found a single function, mark it as used at this
12174 point. (If it lookup found multiple functions the one selected
12175 later by overload resolution will be marked as used at that
12177 if (BASELINK_P (baselink
))
12178 fns
= BASELINK_FUNCTIONS (baselink
);
12179 if (!template_id_p
&& !really_overloaded_fn (fns
))
12180 mark_used (OVL_CURRENT (fns
));
12182 /* Add back the template arguments, if present. */
12183 if (BASELINK_P (baselink
) && template_id_p
)
12184 BASELINK_FUNCTIONS (baselink
)
12185 = build_nt (TEMPLATE_ID_EXPR
,
12186 BASELINK_FUNCTIONS (baselink
),
12188 /* Update the conversion operator type. */
12189 BASELINK_OPTYPE (baselink
) = optype
;
12192 object_type
= current_class_type
;
12195 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12201 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12202 true if the qualified-id will be a postfix-expression in-and-of
12203 itself; false if more of the postfix-expression follows the
12204 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12208 tsubst_qualified_id (tree qualified_id
, tree args
,
12209 tsubst_flags_t complain
, tree in_decl
,
12210 bool done
, bool address_p
)
12216 tree template_args
;
12217 location_t loc
= UNKNOWN_LOCATION
;
12219 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12221 /* Figure out what name to look up. */
12222 name
= TREE_OPERAND (qualified_id
, 1);
12223 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12225 is_template
= true;
12226 loc
= EXPR_LOCATION (name
);
12227 template_args
= TREE_OPERAND (name
, 1);
12229 template_args
= tsubst_template_args (template_args
, args
,
12230 complain
, in_decl
);
12231 name
= TREE_OPERAND (name
, 0);
12235 is_template
= false;
12236 template_args
= NULL_TREE
;
12239 /* Substitute into the qualifying scope. When there are no ARGS, we
12240 are just trying to simplify a non-dependent expression. In that
12241 case the qualifying scope may be dependent, and, in any case,
12242 substituting will not help. */
12243 scope
= TREE_OPERAND (qualified_id
, 0);
12246 scope
= tsubst (scope
, args
, complain
, in_decl
);
12247 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12252 if (dependent_scope_p (scope
))
12255 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12256 return build_qualified_name (NULL_TREE
, scope
, expr
,
12257 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12260 if (!BASELINK_P (name
) && !DECL_P (expr
))
12262 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12264 /* A BIT_NOT_EXPR is used to represent a destructor. */
12265 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12267 error ("qualifying type %qT does not match destructor name ~%qT",
12268 scope
, TREE_OPERAND (expr
, 0));
12269 expr
= error_mark_node
;
12272 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12273 /*is_type_p=*/0, false);
12276 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12277 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12278 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12280 if (complain
& tf_error
)
12282 error ("dependent-name %qE is parsed as a non-type, but "
12283 "instantiation yields a type", qualified_id
);
12284 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12286 return error_mark_node
;
12292 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12294 /* Remember that there was a reference to this entity. */
12298 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12300 if (complain
& tf_error
)
12301 qualified_name_lookup_error (scope
,
12302 TREE_OPERAND (qualified_id
, 1),
12303 expr
, input_location
);
12304 return error_mark_node
;
12308 expr
= lookup_template_function (expr
, template_args
);
12310 if (expr
== error_mark_node
&& complain
& tf_error
)
12311 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12312 expr
, input_location
);
12313 else if (TYPE_P (scope
))
12315 expr
= (adjust_result_of_qualified_name_lookup
12316 (expr
, scope
, current_nonlambda_class_type ()));
12317 expr
= (finish_qualified_id_expr
12318 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12319 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12320 /*template_arg_p=*/false, complain
));
12323 /* Expressions do not generally have reference type. */
12324 if (TREE_CODE (expr
) != SCOPE_REF
12325 /* However, if we're about to form a pointer-to-member, we just
12326 want the referenced member referenced. */
12327 && TREE_CODE (expr
) != OFFSET_REF
)
12328 expr
= convert_from_reference (expr
);
12333 /* Like tsubst, but deals with expressions. This function just replaces
12334 template parms; to finish processing the resultant expression, use
12335 tsubst_copy_and_build or tsubst_expr. */
12338 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12340 enum tree_code code
;
12343 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12346 code
= TREE_CODE (t
);
12351 r
= retrieve_local_specialization (t
);
12353 if (r
== NULL_TREE
)
12355 /* We get here for a use of 'this' in an NSDMI. */
12356 if (DECL_NAME (t
) == this_identifier
12357 && at_function_scope_p ()
12358 && DECL_CONSTRUCTOR_P (current_function_decl
))
12359 return current_class_ptr
;
12361 /* This can happen for a parameter name used later in a function
12362 declaration (such as in a late-specified return type). Just
12363 make a dummy decl, since it's only used for its type. */
12364 gcc_assert (cp_unevaluated_operand
!= 0);
12365 r
= tsubst_decl (t
, args
, complain
);
12366 /* Give it the template pattern as its context; its true context
12367 hasn't been instantiated yet and this is good enough for
12369 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12372 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12373 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12382 if (DECL_TEMPLATE_PARM_P (t
))
12383 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12384 /* There is no need to substitute into namespace-scope
12386 if (DECL_NAMESPACE_SCOPE_P (t
))
12388 /* If ARGS is NULL, then T is known to be non-dependent. */
12389 if (args
== NULL_TREE
)
12390 return integral_constant_value (t
);
12392 /* Unfortunately, we cannot just call lookup_name here.
12395 template <int I> int f() {
12397 struct S { void g() { E e = a; } };
12400 When we instantiate f<7>::S::g(), say, lookup_name is not
12401 clever enough to find f<7>::a. */
12403 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12404 /*entering_scope=*/0);
12406 for (v
= TYPE_VALUES (enum_type
);
12408 v
= TREE_CHAIN (v
))
12409 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12410 return TREE_VALUE (v
);
12412 /* We didn't find the name. That should never happen; if
12413 name-lookup found it during preliminary parsing, we
12414 should find it again here during instantiation. */
12415 gcc_unreachable ();
12420 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12422 /* Check for a local specialization set up by
12423 tsubst_pack_expansion. */
12424 tree r
= retrieve_local_specialization (t
);
12427 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12428 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12432 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12433 tsubst_decl put in the hash table. */
12434 return retrieve_specialization (t
, args
, 0);
12437 if (DECL_CONTEXT (t
))
12441 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12442 /*entering_scope=*/1);
12443 if (ctx
!= DECL_CONTEXT (t
))
12445 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12448 if (complain
& tf_error
)
12449 error ("using invalid field %qD", t
);
12450 return error_mark_node
;
12459 case FUNCTION_DECL
:
12460 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12461 r
= tsubst (t
, args
, complain
, in_decl
);
12462 else if (local_variable_p (t
))
12464 r
= retrieve_local_specialization (t
);
12465 if (r
== NULL_TREE
)
12467 if (DECL_ANON_UNION_VAR_P (t
))
12469 /* Just use name lookup to find a member alias for an
12470 anonymous union, but then add it to the hash table. */
12471 r
= lookup_name (DECL_NAME (t
));
12472 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12473 register_local_specialization (r
, t
);
12477 gcc_assert (errorcount
|| sorrycount
);
12478 return error_mark_node
;
12487 case NAMESPACE_DECL
:
12491 /* An OVERLOAD will always be a non-dependent overload set; an
12492 overload set from function scope will just be represented with an
12493 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12494 gcc_assert (!uses_template_parms (t
));
12498 return tsubst_baselink (t
, current_nonlambda_class_type (),
12499 args
, complain
, in_decl
);
12501 case TEMPLATE_DECL
:
12502 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12503 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12504 args
, complain
, in_decl
);
12505 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12506 return tsubst (t
, args
, complain
, in_decl
);
12507 else if (DECL_CLASS_SCOPE_P (t
)
12508 && uses_template_parms (DECL_CONTEXT (t
)))
12510 /* Template template argument like the following example need
12513 template <template <class> class TT> struct C {};
12514 template <class T> struct D {
12515 template <class U> struct E {};
12520 We are processing the template argument `E' in #1 for
12521 the template instantiation #2. Originally, `E' is a
12522 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12523 have to substitute this with one having context `D<int>'. */
12525 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12526 return lookup_field (context
, DECL_NAME(t
), 0, false);
12529 /* Ordinary template template argument. */
12533 case REINTERPRET_CAST_EXPR
:
12534 case CONST_CAST_EXPR
:
12535 case STATIC_CAST_EXPR
:
12536 case DYNAMIC_CAST_EXPR
:
12537 case IMPLICIT_CONV_EXPR
:
12541 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12542 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12545 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12548 tree expanded
, op
= TREE_OPERAND (t
, 0);
12551 if (SIZEOF_EXPR_TYPE_P (t
))
12552 op
= TREE_TYPE (op
);
12554 ++cp_unevaluated_operand
;
12555 ++c_inhibit_evaluation_warnings
;
12556 /* We only want to compute the number of arguments. */
12557 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12558 --cp_unevaluated_operand
;
12559 --c_inhibit_evaluation_warnings
;
12561 if (TREE_CODE (expanded
) == TREE_VEC
)
12562 len
= TREE_VEC_LENGTH (expanded
);
12564 if (expanded
== error_mark_node
)
12565 return error_mark_node
;
12566 else if (PACK_EXPANSION_P (expanded
)
12567 || (TREE_CODE (expanded
) == TREE_VEC
12569 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12571 if (TREE_CODE (expanded
) == TREE_VEC
)
12572 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12574 if (TYPE_P (expanded
))
12575 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12576 complain
& tf_error
);
12578 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12579 complain
& tf_error
);
12582 return build_int_cst (size_type_node
, len
);
12584 if (SIZEOF_EXPR_TYPE_P (t
))
12586 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12587 args
, complain
, in_decl
);
12588 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12589 r
= build1 (SIZEOF_EXPR
,
12590 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12591 SIZEOF_EXPR_TYPE_P (r
) = 1;
12598 case TRUTH_NOT_EXPR
:
12601 case UNARY_PLUS_EXPR
: /* Unary + */
12603 case AT_ENCODE_EXPR
:
12607 case REALPART_EXPR
:
12608 case IMAGPART_EXPR
:
12611 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12612 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12614 case COMPONENT_REF
:
12619 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12620 name
= TREE_OPERAND (t
, 1);
12621 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12623 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12624 complain
, in_decl
);
12625 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12627 else if (TREE_CODE (name
) == SCOPE_REF
12628 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12630 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12631 complain
, in_decl
);
12632 name
= TREE_OPERAND (name
, 1);
12633 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12634 complain
, in_decl
);
12635 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12636 name
= build_qualified_name (/*type=*/NULL_TREE
,
12638 /*template_p=*/false);
12640 else if (BASELINK_P (name
))
12641 name
= tsubst_baselink (name
,
12642 non_reference (TREE_TYPE (object
)),
12646 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12647 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12653 case TRUNC_DIV_EXPR
:
12654 case CEIL_DIV_EXPR
:
12655 case FLOOR_DIV_EXPR
:
12656 case ROUND_DIV_EXPR
:
12657 case EXACT_DIV_EXPR
:
12661 case TRUNC_MOD_EXPR
:
12662 case FLOOR_MOD_EXPR
:
12663 case TRUTH_ANDIF_EXPR
:
12664 case TRUTH_ORIF_EXPR
:
12665 case TRUTH_AND_EXPR
:
12666 case TRUTH_OR_EXPR
:
12679 case COMPOUND_EXPR
:
12682 case PREDECREMENT_EXPR
:
12683 case PREINCREMENT_EXPR
:
12684 case POSTDECREMENT_EXPR
:
12685 case POSTINCREMENT_EXPR
:
12687 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12688 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12691 return build_qualified_name (/*type=*/NULL_TREE
,
12692 tsubst_copy (TREE_OPERAND (t
, 0),
12693 args
, complain
, in_decl
),
12694 tsubst_copy (TREE_OPERAND (t
, 1),
12695 args
, complain
, in_decl
),
12696 QUALIFIED_NAME_IS_TEMPLATE (t
));
12701 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12702 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12703 NULL_TREE
, NULL_TREE
);
12707 int n
= VL_EXP_OPERAND_LENGTH (t
);
12708 tree result
= build_vl_exp (CALL_EXPR
, n
);
12710 for (i
= 0; i
< n
; i
++)
12711 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12712 complain
, in_decl
);
12718 case PSEUDO_DTOR_EXPR
:
12719 case VEC_PERM_EXPR
:
12722 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12723 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12724 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12725 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12732 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12733 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12734 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12735 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12742 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12743 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12744 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12745 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12749 case TEMPLATE_ID_EXPR
:
12751 /* Substituted template arguments */
12752 tree fn
= TREE_OPERAND (t
, 0);
12753 tree targs
= TREE_OPERAND (t
, 1);
12755 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12757 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12759 return lookup_template_function (fn
, targs
);
12764 tree purpose
, value
, chain
;
12766 if (t
== void_list_node
)
12769 purpose
= TREE_PURPOSE (t
);
12771 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12772 value
= TREE_VALUE (t
);
12774 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12775 chain
= TREE_CHAIN (t
);
12776 if (chain
&& chain
!= void_type_node
)
12777 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12778 if (purpose
== TREE_PURPOSE (t
)
12779 && value
== TREE_VALUE (t
)
12780 && chain
== TREE_CHAIN (t
))
12782 return tree_cons (purpose
, value
, chain
);
12787 case ENUMERAL_TYPE
:
12789 case TEMPLATE_TYPE_PARM
:
12790 case TEMPLATE_TEMPLATE_PARM
:
12791 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12792 case TEMPLATE_PARM_INDEX
:
12794 case REFERENCE_TYPE
:
12796 case FUNCTION_TYPE
:
12799 case TYPENAME_TYPE
:
12800 case UNBOUND_CLASS_TEMPLATE
:
12802 case DECLTYPE_TYPE
:
12804 return tsubst (t
, args
, complain
, in_decl
);
12808 /* Fall through. */
12809 case IDENTIFIER_NODE
:
12810 if (IDENTIFIER_TYPENAME_P (t
))
12812 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12813 return mangle_conv_op_name_for_type (new_type
);
12819 /* This is handled by tsubst_copy_and_build. */
12820 gcc_unreachable ();
12823 return build_x_va_arg (EXPR_LOCATION (t
),
12824 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12826 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12828 case CLEANUP_POINT_EXPR
:
12829 /* We shouldn't have built any of these during initial template
12830 generation. Instead, they should be built during instantiation
12831 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12832 gcc_unreachable ();
12836 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12837 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12838 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12839 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12840 mark_used (TREE_OPERAND (r
, 1));
12843 case EXPR_PACK_EXPANSION
:
12844 error ("invalid use of pack expansion expression");
12845 return error_mark_node
;
12847 case NONTYPE_ARGUMENT_PACK
:
12848 error ("use %<...%> to expand argument pack");
12849 return error_mark_node
;
12856 /* Instantiate any typedefs in the type. */
12857 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12858 r
= fold_convert (type
, t
);
12859 gcc_assert (TREE_CODE (r
) == code
);
12864 /* These can sometimes show up in a partial instantiation, but never
12865 involve template parms. */
12866 gcc_assert (!uses_template_parms (t
));
12870 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12871 gcc_checking_assert (false);
12876 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12879 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12880 tree args
, tsubst_flags_t complain
, tree in_decl
)
12882 tree new_clauses
= NULL
, nc
, oc
;
12884 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12886 nc
= copy_node (oc
);
12887 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12890 switch (OMP_CLAUSE_CODE (nc
))
12892 case OMP_CLAUSE_LASTPRIVATE
:
12893 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12895 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12896 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12897 in_decl
, /*integral_constant_expression_p=*/false);
12898 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12899 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12902 case OMP_CLAUSE_PRIVATE
:
12903 case OMP_CLAUSE_SHARED
:
12904 case OMP_CLAUSE_FIRSTPRIVATE
:
12905 case OMP_CLAUSE_COPYIN
:
12906 case OMP_CLAUSE_COPYPRIVATE
:
12907 case OMP_CLAUSE_IF
:
12908 case OMP_CLAUSE_NUM_THREADS
:
12909 case OMP_CLAUSE_SCHEDULE
:
12910 case OMP_CLAUSE_COLLAPSE
:
12911 case OMP_CLAUSE_FINAL
:
12912 case OMP_CLAUSE_DEPEND
:
12913 case OMP_CLAUSE_FROM
:
12914 case OMP_CLAUSE_TO
:
12915 case OMP_CLAUSE_UNIFORM
:
12916 case OMP_CLAUSE_MAP
:
12917 case OMP_CLAUSE_DEVICE
:
12918 case OMP_CLAUSE_DIST_SCHEDULE
:
12919 case OMP_CLAUSE_NUM_TEAMS
:
12920 case OMP_CLAUSE_THREAD_LIMIT
:
12921 case OMP_CLAUSE_SAFELEN
:
12922 case OMP_CLAUSE_SIMDLEN
:
12923 OMP_CLAUSE_OPERAND (nc
, 0)
12924 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12925 in_decl
, /*integral_constant_expression_p=*/false);
12927 case OMP_CLAUSE_REDUCTION
:
12928 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12930 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12931 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12933 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12934 complain
, in_decl
);
12935 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12936 = build_qualified_name (NULL_TREE
, scope
,
12937 TREE_OPERAND (placeholder
, 1),
12941 gcc_assert (identifier_p (placeholder
));
12943 OMP_CLAUSE_OPERAND (nc
, 0)
12944 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12945 in_decl
, /*integral_constant_expression_p=*/false);
12947 case OMP_CLAUSE_LINEAR
:
12948 case OMP_CLAUSE_ALIGNED
:
12949 OMP_CLAUSE_OPERAND (nc
, 0)
12950 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12951 in_decl
, /*integral_constant_expression_p=*/false);
12952 OMP_CLAUSE_OPERAND (nc
, 1)
12953 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12954 in_decl
, /*integral_constant_expression_p=*/false);
12957 case OMP_CLAUSE_NOWAIT
:
12958 case OMP_CLAUSE_ORDERED
:
12959 case OMP_CLAUSE_DEFAULT
:
12960 case OMP_CLAUSE_UNTIED
:
12961 case OMP_CLAUSE_MERGEABLE
:
12962 case OMP_CLAUSE_INBRANCH
:
12963 case OMP_CLAUSE_NOTINBRANCH
:
12964 case OMP_CLAUSE_PROC_BIND
:
12965 case OMP_CLAUSE_FOR
:
12966 case OMP_CLAUSE_PARALLEL
:
12967 case OMP_CLAUSE_SECTIONS
:
12968 case OMP_CLAUSE_TASKGROUP
:
12971 gcc_unreachable ();
12975 new_clauses
= nreverse (new_clauses
);
12977 new_clauses
= finish_omp_clauses (new_clauses
);
12978 return new_clauses
;
12981 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12984 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12987 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12989 tree purpose
, value
, chain
;
12994 if (TREE_CODE (t
) != TREE_LIST
)
12995 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12996 /*function_p=*/false,
12997 /*integral_constant_expression_p=*/false);
12999 if (t
== void_list_node
)
13002 purpose
= TREE_PURPOSE (t
);
13004 purpose
= RECUR (purpose
);
13005 value
= TREE_VALUE (t
);
13008 if (TREE_CODE (value
) != LABEL_DECL
)
13009 value
= RECUR (value
);
13012 value
= lookup_label (DECL_NAME (value
));
13013 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13014 TREE_USED (value
) = 1;
13017 chain
= TREE_CHAIN (t
);
13018 if (chain
&& chain
!= void_type_node
)
13019 chain
= RECUR (chain
);
13020 return tree_cons (purpose
, value
, chain
);
13024 /* Substitute one OMP_FOR iterator. */
13027 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13028 tree condv
, tree incrv
, tree
*clauses
,
13029 tree args
, tsubst_flags_t complain
, tree in_decl
,
13030 bool integral_constant_expression_p
)
13032 #define RECUR(NODE) \
13033 tsubst_expr ((NODE), args, complain, in_decl, \
13034 integral_constant_expression_p)
13035 tree decl
, init
, cond
, incr
;
13038 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13039 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13040 decl
= TREE_OPERAND (init
, 0);
13041 init
= TREE_OPERAND (init
, 1);
13042 /* Do this before substituting into decl to handle 'auto'. */
13043 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13044 init
= RECUR (init
);
13045 decl
= RECUR (decl
);
13047 if (decl
== error_mark_node
|| init
== error_mark_node
)
13052 gcc_assert (!processing_template_decl
);
13053 init
= DECL_INITIAL (decl
);
13054 DECL_INITIAL (decl
) = NULL_TREE
;
13057 gcc_assert (!type_dependent_expression_p (decl
));
13059 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13061 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13062 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13063 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13064 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13065 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13066 RECUR (TREE_OPERAND (incr
, 1)),
13069 incr
= RECUR (incr
);
13070 TREE_VEC_ELT (declv
, i
) = decl
;
13071 TREE_VEC_ELT (initv
, i
) = init
;
13072 TREE_VEC_ELT (condv
, i
) = cond
;
13073 TREE_VEC_ELT (incrv
, i
) = incr
;
13077 if (init
&& !init_decl
)
13080 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13082 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13083 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13084 && OMP_CLAUSE_DECL (c
) == decl
)
13086 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13087 && OMP_CLAUSE_DECL (c
) == decl
)
13088 error ("iteration variable %qD should not be firstprivate", decl
);
13089 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13090 && OMP_CLAUSE_DECL (c
) == decl
)
13091 error ("iteration variable %qD should not be reduction", decl
);
13095 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13096 OMP_CLAUSE_DECL (c
) = decl
;
13097 c
= finish_omp_clauses (c
);
13100 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13105 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13106 if (COMPARISON_CLASS_P (cond
))
13107 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13108 RECUR (TREE_OPERAND (cond
, 0)),
13109 RECUR (TREE_OPERAND (cond
, 1)));
13111 cond
= RECUR (cond
);
13112 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13113 switch (TREE_CODE (incr
))
13115 case PREINCREMENT_EXPR
:
13116 case PREDECREMENT_EXPR
:
13117 case POSTINCREMENT_EXPR
:
13118 case POSTDECREMENT_EXPR
:
13119 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13120 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13123 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13124 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13126 tree rhs
= TREE_OPERAND (incr
, 1);
13127 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13128 RECUR (TREE_OPERAND (incr
, 0)),
13129 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13130 RECUR (TREE_OPERAND (rhs
, 0)),
13131 RECUR (TREE_OPERAND (rhs
, 1))));
13134 incr
= RECUR (incr
);
13137 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13138 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13140 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13141 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13142 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13143 TREE_TYPE (decl
), lhs
,
13144 RECUR (TREE_OPERAND (incr
, 2))));
13146 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13147 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13148 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13150 tree rhs
= TREE_OPERAND (incr
, 2);
13151 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13152 RECUR (TREE_OPERAND (incr
, 0)),
13153 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13154 RECUR (TREE_OPERAND (rhs
, 0)),
13155 RECUR (TREE_OPERAND (rhs
, 1))));
13158 incr
= RECUR (incr
);
13161 incr
= RECUR (incr
);
13165 TREE_VEC_ELT (declv
, i
) = decl
;
13166 TREE_VEC_ELT (initv
, i
) = init
;
13167 TREE_VEC_ELT (condv
, i
) = cond
;
13168 TREE_VEC_ELT (incrv
, i
) = incr
;
13172 /* Like tsubst_copy for expressions, etc. but also does semantic
13176 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13177 bool integral_constant_expression_p
)
13179 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13180 #define RECUR(NODE) \
13181 tsubst_expr ((NODE), args, complain, in_decl, \
13182 integral_constant_expression_p)
13188 if (t
== NULL_TREE
|| t
== error_mark_node
)
13191 loc
= input_location
;
13192 if (EXPR_HAS_LOCATION (t
))
13193 input_location
= EXPR_LOCATION (t
);
13194 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13195 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13197 switch (TREE_CODE (t
))
13199 case STATEMENT_LIST
:
13201 tree_stmt_iterator i
;
13202 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13203 RECUR (tsi_stmt (i
));
13207 case CTOR_INITIALIZER
:
13208 finish_mem_initializers (tsubst_initializer_list
13209 (TREE_OPERAND (t
, 0), args
));
13213 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13217 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13218 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13219 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13221 finish_expr_stmt (tmp
);
13225 do_using_directive (USING_STMT_NAMESPACE (t
));
13230 tree decl
, pattern_decl
;
13233 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13234 if (TREE_CODE (decl
) == LABEL_DECL
)
13235 finish_label_decl (DECL_NAME (decl
));
13236 else if (TREE_CODE (decl
) == USING_DECL
)
13238 tree scope
= USING_DECL_SCOPE (decl
);
13239 tree name
= DECL_NAME (decl
);
13242 scope
= tsubst (scope
, args
, complain
, in_decl
);
13243 decl
= lookup_qualified_name (scope
, name
,
13244 /*is_type_p=*/false,
13245 /*complain=*/false);
13246 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13247 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13249 do_local_using_decl (decl
, scope
, name
);
13251 else if (DECL_PACK_P (decl
))
13253 /* Don't build up decls for a variadic capture proxy, we'll
13254 instantiate the elements directly as needed. */
13259 init
= DECL_INITIAL (decl
);
13260 decl
= tsubst (decl
, args
, complain
, in_decl
);
13261 if (decl
!= error_mark_node
)
13263 /* By marking the declaration as instantiated, we avoid
13264 trying to instantiate it. Since instantiate_decl can't
13265 handle local variables, and since we've already done
13266 all that needs to be done, that's the right thing to
13269 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13271 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13272 /* Anonymous aggregates are a special case. */
13273 finish_anon_union (decl
);
13274 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13276 DECL_CONTEXT (decl
) = current_function_decl
;
13277 if (DECL_NAME (decl
) == this_identifier
)
13279 tree lam
= DECL_CONTEXT (current_function_decl
);
13280 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13281 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13283 insert_capture_proxy (decl
);
13285 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13286 /* We already did a pushtag. */;
13287 else if (TREE_CODE (decl
) == FUNCTION_DECL
13288 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13289 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13291 DECL_CONTEXT (decl
) = NULL_TREE
;
13293 DECL_CONTEXT (decl
) = current_function_decl
;
13294 cp_check_omp_declare_reduction (decl
);
13298 int const_init
= false;
13299 maybe_push_decl (decl
);
13301 && DECL_PRETTY_FUNCTION_P (decl
))
13303 /* For __PRETTY_FUNCTION__ we have to adjust the
13305 const char *const name
13306 = cxx_printable_name (current_function_decl
, 2);
13307 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13311 tree t
= RECUR (init
);
13315 /* If we had an initializer but it
13316 instantiated to nothing,
13317 value-initialize the object. This will
13318 only occur when the initializer was a
13319 pack expansion where the parameter packs
13320 used in that expansion were of length
13322 init
= build_value_init (TREE_TYPE (decl
),
13324 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13325 init
= get_target_expr_sfinae (init
, complain
);
13332 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13334 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13343 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13344 RECUR (FOR_INIT_STMT (t
));
13345 finish_for_init_stmt (stmt
);
13346 tmp
= RECUR (FOR_COND (t
));
13347 finish_for_cond (tmp
, stmt
, false);
13348 tmp
= RECUR (FOR_EXPR (t
));
13349 finish_for_expr (tmp
, stmt
);
13350 RECUR (FOR_BODY (t
));
13351 finish_for_stmt (stmt
);
13354 case RANGE_FOR_STMT
:
13357 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13358 decl
= RANGE_FOR_DECL (t
);
13359 decl
= tsubst (decl
, args
, complain
, in_decl
);
13360 maybe_push_decl (decl
);
13361 expr
= RECUR (RANGE_FOR_EXPR (t
));
13362 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13363 RECUR (RANGE_FOR_BODY (t
));
13364 finish_for_stmt (stmt
);
13369 stmt
= begin_while_stmt ();
13370 tmp
= RECUR (WHILE_COND (t
));
13371 finish_while_stmt_cond (tmp
, stmt
, false);
13372 RECUR (WHILE_BODY (t
));
13373 finish_while_stmt (stmt
);
13377 stmt
= begin_do_stmt ();
13378 RECUR (DO_BODY (t
));
13379 finish_do_body (stmt
);
13380 tmp
= RECUR (DO_COND (t
));
13381 finish_do_stmt (tmp
, stmt
, false);
13385 stmt
= begin_if_stmt ();
13386 tmp
= RECUR (IF_COND (t
));
13387 finish_if_stmt_cond (tmp
, stmt
);
13388 RECUR (THEN_CLAUSE (t
));
13389 finish_then_clause (stmt
);
13391 if (ELSE_CLAUSE (t
))
13393 begin_else_clause (stmt
);
13394 RECUR (ELSE_CLAUSE (t
));
13395 finish_else_clause (stmt
);
13398 finish_if_stmt (stmt
);
13402 if (BIND_EXPR_BODY_BLOCK (t
))
13403 stmt
= begin_function_body ();
13405 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13406 ? BCS_TRY_BLOCK
: 0);
13408 RECUR (BIND_EXPR_BODY (t
));
13410 if (BIND_EXPR_BODY_BLOCK (t
))
13411 finish_function_body (stmt
);
13413 finish_compound_stmt (stmt
);
13417 finish_break_stmt ();
13420 case CONTINUE_STMT
:
13421 finish_continue_stmt ();
13425 stmt
= begin_switch_stmt ();
13426 tmp
= RECUR (SWITCH_STMT_COND (t
));
13427 finish_switch_cond (tmp
, stmt
);
13428 RECUR (SWITCH_STMT_BODY (t
));
13429 finish_switch_stmt (stmt
);
13432 case CASE_LABEL_EXPR
:
13433 finish_case_label (EXPR_LOCATION (t
),
13434 RECUR (CASE_LOW (t
)),
13435 RECUR (CASE_HIGH (t
)));
13440 tree decl
= LABEL_EXPR_LABEL (t
);
13443 label
= finish_label_stmt (DECL_NAME (decl
));
13444 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13445 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13450 tmp
= GOTO_DESTINATION (t
);
13451 if (TREE_CODE (tmp
) != LABEL_DECL
)
13452 /* Computed goto's must be tsubst'd into. On the other hand,
13453 non-computed gotos must not be; the identifier in question
13454 will have no binding. */
13457 tmp
= DECL_NAME (tmp
);
13458 finish_goto_stmt (tmp
);
13462 tmp
= finish_asm_stmt
13463 (ASM_VOLATILE_P (t
),
13464 RECUR (ASM_STRING (t
)),
13465 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13466 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13467 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13468 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13470 tree asm_expr
= tmp
;
13471 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13472 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13473 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13480 stmt
= begin_try_block ();
13481 RECUR (TRY_STMTS (t
));
13482 finish_cleanup_try_block (stmt
);
13483 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13487 tree compound_stmt
= NULL_TREE
;
13489 if (FN_TRY_BLOCK_P (t
))
13490 stmt
= begin_function_try_block (&compound_stmt
);
13492 stmt
= begin_try_block ();
13494 RECUR (TRY_STMTS (t
));
13496 if (FN_TRY_BLOCK_P (t
))
13497 finish_function_try_block (stmt
);
13499 finish_try_block (stmt
);
13501 RECUR (TRY_HANDLERS (t
));
13502 if (FN_TRY_BLOCK_P (t
))
13503 finish_function_handler_sequence (stmt
, compound_stmt
);
13505 finish_handler_sequence (stmt
);
13511 tree decl
= HANDLER_PARMS (t
);
13515 decl
= tsubst (decl
, args
, complain
, in_decl
);
13516 /* Prevent instantiate_decl from trying to instantiate
13517 this variable. We've already done all that needs to be
13519 if (decl
!= error_mark_node
)
13520 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13522 stmt
= begin_handler ();
13523 finish_handler_parms (decl
, stmt
);
13524 RECUR (HANDLER_BODY (t
));
13525 finish_handler (stmt
);
13530 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13531 if (CLASS_TYPE_P (tmp
))
13533 /* Local classes are not independent templates; they are
13534 instantiated along with their containing function. And this
13535 way we don't have to deal with pushing out of one local class
13536 to instantiate a member of another local class. */
13538 /* Closures are handled by the LAMBDA_EXPR. */
13539 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13540 complete_type (tmp
);
13541 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13542 if (!DECL_ARTIFICIAL (fn
))
13543 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13547 case STATIC_ASSERT
:
13551 ++c_inhibit_evaluation_warnings
;
13553 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13556 /*integral_constant_expression_p=*/true);
13557 --c_inhibit_evaluation_warnings
;
13559 finish_static_assert (condition
,
13560 STATIC_ASSERT_MESSAGE (t
),
13561 STATIC_ASSERT_SOURCE_LOCATION (t
),
13562 /*member_p=*/false);
13567 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13568 args
, complain
, in_decl
);
13569 stmt
= begin_omp_parallel ();
13570 RECUR (OMP_PARALLEL_BODY (t
));
13571 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13572 = OMP_PARALLEL_COMBINED (t
);
13576 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13577 args
, complain
, in_decl
);
13578 stmt
= begin_omp_task ();
13579 RECUR (OMP_TASK_BODY (t
));
13580 finish_omp_task (tmp
, stmt
);
13586 case OMP_DISTRIBUTE
:
13588 tree clauses
, body
, pre_body
;
13589 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13590 tree incrv
= NULL_TREE
;
13593 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13594 args
, complain
, in_decl
);
13595 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13597 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13598 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13599 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13600 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13603 stmt
= begin_omp_structured_block ();
13605 pre_body
= push_stmt_list ();
13606 RECUR (OMP_FOR_PRE_BODY (t
));
13607 pre_body
= pop_stmt_list (pre_body
);
13609 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13610 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13611 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13612 &clauses
, args
, complain
, in_decl
,
13613 integral_constant_expression_p
);
13615 body
= push_stmt_list ();
13616 RECUR (OMP_FOR_BODY (t
));
13617 body
= pop_stmt_list (body
);
13619 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13620 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13621 condv
, incrv
, body
, pre_body
, clauses
);
13624 t
= make_node (TREE_CODE (t
));
13625 TREE_TYPE (t
) = void_type_node
;
13626 OMP_FOR_BODY (t
) = body
;
13627 OMP_FOR_PRE_BODY (t
) = pre_body
;
13628 OMP_FOR_CLAUSES (t
) = clauses
;
13629 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13633 add_stmt (finish_omp_structured_block (stmt
));
13640 case OMP_TARGET_DATA
:
13642 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13643 args
, complain
, in_decl
);
13644 stmt
= push_stmt_list ();
13645 RECUR (OMP_BODY (t
));
13646 stmt
= pop_stmt_list (stmt
);
13649 OMP_BODY (t
) = stmt
;
13650 OMP_CLAUSES (t
) = tmp
;
13654 case OMP_TARGET_UPDATE
:
13655 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13656 args
, complain
, in_decl
);
13658 OMP_CLAUSES (t
) = tmp
;
13665 case OMP_TASKGROUP
:
13667 stmt
= push_stmt_list ();
13668 RECUR (OMP_BODY (t
));
13669 stmt
= pop_stmt_list (stmt
);
13672 OMP_BODY (t
) = stmt
;
13677 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13678 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13680 tree op1
= TREE_OPERAND (t
, 1);
13681 tree rhs1
= NULL_TREE
;
13683 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13685 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13686 op1
= TREE_OPERAND (op1
, 1);
13688 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13689 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13690 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13691 NULL_TREE
, NULL_TREE
, rhs1
,
13692 OMP_ATOMIC_SEQ_CST (t
));
13696 tree op1
= TREE_OPERAND (t
, 1);
13697 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13698 tree rhs1
= NULL_TREE
;
13699 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13700 enum tree_code opcode
= NOP_EXPR
;
13701 if (code
== OMP_ATOMIC_READ
)
13703 v
= RECUR (TREE_OPERAND (op1
, 0));
13704 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13706 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13707 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13709 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13710 v
= RECUR (TREE_OPERAND (op1
, 0));
13711 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13712 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13714 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13715 op11
= TREE_OPERAND (op11
, 1);
13717 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13718 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13719 opcode
= TREE_CODE (op11
);
13720 if (opcode
== MODIFY_EXPR
)
13726 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13727 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13729 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13730 OMP_ATOMIC_SEQ_CST (t
));
13734 case TRANSACTION_EXPR
:
13737 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13738 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13740 if (TRANSACTION_EXPR_IS_STMT (t
))
13742 tree body
= TRANSACTION_EXPR_BODY (t
);
13743 tree noex
= NULL_TREE
;
13744 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13746 noex
= MUST_NOT_THROW_COND (body
);
13747 if (noex
== NULL_TREE
)
13748 noex
= boolean_true_node
;
13749 body
= TREE_OPERAND (body
, 0);
13751 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13753 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13757 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13758 RECUR (TRANSACTION_EXPR_BODY (t
)),
13765 case MUST_NOT_THROW_EXPR
:
13766 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13767 RECUR (MUST_NOT_THROW_COND (t
))));
13769 case EXPR_PACK_EXPANSION
:
13770 error ("invalid use of pack expansion expression");
13771 RETURN (error_mark_node
);
13773 case NONTYPE_ARGUMENT_PACK
:
13774 error ("use %<...%> to expand argument pack");
13775 RETURN (error_mark_node
);
13777 case CILK_SPAWN_STMT
:
13778 cfun
->calls_cilk_spawn
= 1;
13779 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
13781 case CILK_SYNC_STMT
:
13782 RETURN (build_cilk_sync ());
13784 case COMPOUND_EXPR
:
13785 tmp
= RECUR (TREE_OPERAND (t
, 0));
13786 if (tmp
== NULL_TREE
)
13787 /* If the first operand was a statement, we're done with it. */
13788 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13789 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13790 RECUR (TREE_OPERAND (t
, 1)),
13794 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13796 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13797 /*function_p=*/false,
13798 integral_constant_expression_p
));
13801 RETURN (NULL_TREE
);
13803 input_location
= loc
;
13809 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13810 function. For description of the body see comment above
13811 cp_parser_omp_declare_reduction_exprs. */
13814 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13816 if (t
== NULL_TREE
|| t
== error_mark_node
)
13819 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13821 tree_stmt_iterator tsi
;
13824 memset (stmts
, 0, sizeof stmts
);
13825 for (i
= 0, tsi
= tsi_start (t
);
13826 i
< 7 && !tsi_end_p (tsi
);
13827 i
++, tsi_next (&tsi
))
13828 stmts
[i
] = tsi_stmt (tsi
);
13829 gcc_assert (tsi_end_p (tsi
));
13833 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13834 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13835 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13836 args
, complain
, in_decl
);
13837 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13838 args
, complain
, in_decl
);
13839 DECL_CONTEXT (omp_out
) = current_function_decl
;
13840 DECL_CONTEXT (omp_in
) = current_function_decl
;
13841 keep_next_level (true);
13842 tree block
= begin_omp_structured_block ();
13843 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13844 block
= finish_omp_structured_block (block
);
13845 block
= maybe_cleanup_point_expr_void (block
);
13846 add_decl_expr (omp_out
);
13847 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13848 TREE_NO_WARNING (omp_out
) = 1;
13849 add_decl_expr (omp_in
);
13850 finish_expr_stmt (block
);
13854 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13855 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13856 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13857 args
, complain
, in_decl
);
13858 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13859 args
, complain
, in_decl
);
13860 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13861 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13862 keep_next_level (true);
13863 tree block
= begin_omp_structured_block ();
13864 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13865 block
= finish_omp_structured_block (block
);
13866 block
= maybe_cleanup_point_expr_void (block
);
13867 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13868 add_decl_expr (omp_priv
);
13869 add_decl_expr (omp_orig
);
13870 finish_expr_stmt (block
);
13872 add_decl_expr (omp_orig
);
13876 /* T is a postfix-expression that is not being used in a function
13877 call. Return the substituted version of T. */
13880 tsubst_non_call_postfix_expression (tree t
, tree args
,
13881 tsubst_flags_t complain
,
13884 if (TREE_CODE (t
) == SCOPE_REF
)
13885 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13886 /*done=*/false, /*address_p=*/false);
13888 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13889 /*function_p=*/false,
13890 /*integral_constant_expression_p=*/false);
13895 /* Like tsubst but deals with expressions and performs semantic
13896 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13899 tsubst_copy_and_build (tree t
,
13901 tsubst_flags_t complain
,
13904 bool integral_constant_expression_p
)
13906 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13907 #define RECUR(NODE) \
13908 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13909 /*function_p=*/false, \
13910 integral_constant_expression_p)
13915 if (t
== NULL_TREE
|| t
== error_mark_node
)
13918 loc
= input_location
;
13919 if (EXPR_HAS_LOCATION (t
))
13920 input_location
= EXPR_LOCATION (t
);
13922 /* N3276 decltype magic only applies to calls at the top level or on the
13923 right side of a comma. */
13924 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13925 complain
&= ~tf_decltype
;
13927 switch (TREE_CODE (t
))
13931 /* Fall through. */
13932 case IDENTIFIER_NODE
:
13936 bool non_integral_constant_expression_p
;
13937 const char *error_msg
;
13939 if (IDENTIFIER_TYPENAME_P (t
))
13941 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13942 t
= mangle_conv_op_name_for_type (new_type
);
13945 /* Look up the name. */
13946 decl
= lookup_name (t
);
13948 /* By convention, expressions use ERROR_MARK_NODE to indicate
13949 failure, not NULL_TREE. */
13950 if (decl
== NULL_TREE
)
13951 decl
= error_mark_node
;
13953 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13955 integral_constant_expression_p
,
13956 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13957 &non_integral_constant_expression_p
,
13958 /*template_p=*/false,
13960 /*address_p=*/false,
13961 /*template_arg_p=*/false,
13966 if (!function_p
&& identifier_p (decl
))
13968 if (complain
& tf_error
)
13969 unqualified_name_lookup_error (decl
);
13970 decl
= error_mark_node
;
13975 case TEMPLATE_ID_EXPR
:
13978 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13979 tree targs
= TREE_OPERAND (t
, 1);
13982 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13984 if (TREE_CODE (templ
) == COMPONENT_REF
)
13986 object
= TREE_OPERAND (templ
, 0);
13987 templ
= TREE_OPERAND (templ
, 1);
13990 object
= NULL_TREE
;
13991 templ
= lookup_template_function (templ
, targs
);
13994 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13995 object
, templ
, NULL_TREE
));
13997 RETURN (baselink_for_fns (templ
));
14002 tree r
= RECUR (TREE_OPERAND (t
, 0));
14004 if (REFERENCE_REF_P (t
))
14006 /* A type conversion to reference type will be enclosed in
14007 such an indirect ref, but the substitution of the cast
14008 will have also added such an indirect ref. */
14009 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14010 r
= convert_from_reference (r
);
14013 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14014 complain
|decltype_flag
);
14020 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14021 RECUR (TREE_OPERAND (t
, 0))));
14023 case IMPLICIT_CONV_EXPR
:
14025 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14026 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14027 int flags
= LOOKUP_IMPLICIT
;
14028 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14029 flags
= LOOKUP_NORMAL
;
14030 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14037 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14038 RECUR (TREE_OPERAND (t
, 0))));
14041 case REINTERPRET_CAST_EXPR
:
14042 case CONST_CAST_EXPR
:
14043 case DYNAMIC_CAST_EXPR
:
14044 case STATIC_CAST_EXPR
:
14047 tree op
, r
= NULL_TREE
;
14049 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14050 if (integral_constant_expression_p
14051 && !cast_valid_in_integral_constant_expression_p (type
))
14053 if (complain
& tf_error
)
14054 error ("a cast to a type other than an integral or "
14055 "enumeration type cannot appear in a constant-expression");
14056 RETURN (error_mark_node
);
14059 op
= RECUR (TREE_OPERAND (t
, 0));
14061 ++c_inhibit_evaluation_warnings
;
14062 switch (TREE_CODE (t
))
14065 r
= build_functional_cast (type
, op
, complain
);
14067 case REINTERPRET_CAST_EXPR
:
14068 r
= build_reinterpret_cast (type
, op
, complain
);
14070 case CONST_CAST_EXPR
:
14071 r
= build_const_cast (type
, op
, complain
);
14073 case DYNAMIC_CAST_EXPR
:
14074 r
= build_dynamic_cast (type
, op
, complain
);
14076 case STATIC_CAST_EXPR
:
14077 r
= build_static_cast (type
, op
, complain
);
14080 gcc_unreachable ();
14082 --c_inhibit_evaluation_warnings
;
14087 case POSTDECREMENT_EXPR
:
14088 case POSTINCREMENT_EXPR
:
14089 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14090 args
, complain
, in_decl
);
14091 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14092 complain
|decltype_flag
));
14094 case PREDECREMENT_EXPR
:
14095 case PREINCREMENT_EXPR
:
14099 case TRUTH_NOT_EXPR
:
14100 case UNARY_PLUS_EXPR
: /* Unary + */
14101 case REALPART_EXPR
:
14102 case IMAGPART_EXPR
:
14103 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14104 RECUR (TREE_OPERAND (t
, 0)),
14105 complain
|decltype_flag
));
14107 case FIX_TRUNC_EXPR
:
14108 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14112 op1
= TREE_OPERAND (t
, 0);
14113 if (TREE_CODE (op1
) == LABEL_DECL
)
14114 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14115 EXPR_LOCATION (op1
)));
14116 if (TREE_CODE (op1
) == SCOPE_REF
)
14117 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14118 /*done=*/true, /*address_p=*/true);
14120 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14122 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14123 complain
|decltype_flag
));
14128 case TRUNC_DIV_EXPR
:
14129 case CEIL_DIV_EXPR
:
14130 case FLOOR_DIV_EXPR
:
14131 case ROUND_DIV_EXPR
:
14132 case EXACT_DIV_EXPR
:
14136 case TRUNC_MOD_EXPR
:
14137 case FLOOR_MOD_EXPR
:
14138 case TRUTH_ANDIF_EXPR
:
14139 case TRUTH_ORIF_EXPR
:
14140 case TRUTH_AND_EXPR
:
14141 case TRUTH_OR_EXPR
:
14159 ++c_inhibit_evaluation_warnings
;
14161 r
= build_x_binary_op
14162 (input_location
, TREE_CODE (t
),
14163 RECUR (TREE_OPERAND (t
, 0)),
14164 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14166 : TREE_CODE (TREE_OPERAND (t
, 0))),
14167 RECUR (TREE_OPERAND (t
, 1)),
14168 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14170 : TREE_CODE (TREE_OPERAND (t
, 1))),
14172 complain
|decltype_flag
);
14173 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14174 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14176 --c_inhibit_evaluation_warnings
;
14181 case POINTER_PLUS_EXPR
:
14182 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t
, 0)),
14183 RECUR (TREE_OPERAND (t
, 1)));
14186 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14187 /*address_p=*/false));
14189 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14190 args
, complain
, in_decl
);
14191 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14192 RECUR (TREE_OPERAND (t
, 1)),
14193 complain
|decltype_flag
));
14195 case ARRAY_NOTATION_REF
:
14197 tree start_index
, length
, stride
;
14198 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14199 args
, complain
, in_decl
);
14200 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14201 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14202 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14203 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14204 length
, stride
, TREE_TYPE (op1
)));
14207 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14208 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14215 op1
= TREE_OPERAND (t
, 0);
14216 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14217 op1
= TREE_TYPE (op1
);
14220 /* When there are no ARGS, we are trying to evaluate a
14221 non-dependent expression from the parser. Trying to do
14222 the substitutions may not work. */
14224 op1
= TREE_TYPE (op1
);
14228 ++cp_unevaluated_operand
;
14229 ++c_inhibit_evaluation_warnings
;
14231 op1
= tsubst (op1
, args
, complain
, in_decl
);
14233 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14234 /*function_p=*/false,
14235 /*integral_constant_expression_p=*/
14237 --cp_unevaluated_operand
;
14238 --c_inhibit_evaluation_warnings
;
14241 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14242 complain
& tf_error
);
14244 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14245 complain
& tf_error
);
14246 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14248 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14250 if (!processing_template_decl
&& TYPE_P (op1
))
14252 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14253 build1 (NOP_EXPR
, op1
, error_mark_node
));
14254 SIZEOF_EXPR_TYPE_P (r
) = 1;
14257 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14258 TREE_SIDE_EFFECTS (r
) = 0;
14259 TREE_READONLY (r
) = 1;
14261 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14266 case AT_ENCODE_EXPR
:
14268 op1
= TREE_OPERAND (t
, 0);
14269 ++cp_unevaluated_operand
;
14270 ++c_inhibit_evaluation_warnings
;
14271 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14272 /*function_p=*/false,
14273 /*integral_constant_expression_p=*/false);
14274 --cp_unevaluated_operand
;
14275 --c_inhibit_evaluation_warnings
;
14276 RETURN (objc_build_encode_expr (op1
));
14279 case NOEXCEPT_EXPR
:
14280 op1
= TREE_OPERAND (t
, 0);
14281 ++cp_unevaluated_operand
;
14282 ++c_inhibit_evaluation_warnings
;
14283 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14284 /*function_p=*/false,
14285 /*integral_constant_expression_p=*/false);
14286 --cp_unevaluated_operand
;
14287 --c_inhibit_evaluation_warnings
;
14288 RETURN (finish_noexcept_expr (op1
, complain
));
14294 ++c_inhibit_evaluation_warnings
;
14296 r
= build_x_modify_expr
14297 (EXPR_LOCATION (t
),
14298 RECUR (TREE_OPERAND (t
, 0)),
14299 TREE_CODE (TREE_OPERAND (t
, 1)),
14300 RECUR (TREE_OPERAND (t
, 2)),
14301 complain
|decltype_flag
);
14302 /* TREE_NO_WARNING must be set if either the expression was
14303 parenthesized or it uses an operator such as >>= rather
14304 than plain assignment. In the former case, it was already
14305 set and must be copied. In the latter case,
14306 build_x_modify_expr sets it and it must not be reset
14308 if (TREE_NO_WARNING (t
))
14309 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14311 --c_inhibit_evaluation_warnings
;
14317 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14318 args
, complain
, in_decl
);
14319 /* Remember that there was a reference to this entity. */
14322 RETURN (build_x_arrow (input_location
, op1
, complain
));
14326 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14327 tree init
= RECUR (TREE_OPERAND (t
, 3));
14328 vec
<tree
, va_gc
> *placement_vec
;
14329 vec
<tree
, va_gc
> *init_vec
;
14332 if (placement
== NULL_TREE
)
14333 placement_vec
= NULL
;
14336 placement_vec
= make_tree_vector ();
14337 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14338 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14341 /* If there was an initializer in the original tree, but it
14342 instantiated to an empty list, then we should pass a
14343 non-NULL empty vector to tell build_new that it was an
14344 empty initializer() rather than no initializer. This can
14345 only happen when the initializer is a pack expansion whose
14346 parameter packs are of length zero. */
14347 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14351 init_vec
= make_tree_vector ();
14352 if (init
== void_zero_node
)
14353 gcc_assert (init_vec
!= NULL
);
14356 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14357 vec_safe_push (init_vec
, TREE_VALUE (init
));
14361 ret
= build_new (&placement_vec
,
14362 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14363 RECUR (TREE_OPERAND (t
, 2)),
14365 NEW_EXPR_USE_GLOBAL (t
),
14368 if (placement_vec
!= NULL
)
14369 release_tree_vector (placement_vec
);
14370 if (init_vec
!= NULL
)
14371 release_tree_vector (init_vec
);
14377 RETURN (delete_sanity
14378 (RECUR (TREE_OPERAND (t
, 0)),
14379 RECUR (TREE_OPERAND (t
, 1)),
14380 DELETE_EXPR_USE_VEC (t
),
14381 DELETE_EXPR_USE_GLOBAL (t
),
14384 case COMPOUND_EXPR
:
14386 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14387 complain
& ~tf_decltype
, in_decl
,
14388 /*function_p=*/false,
14389 integral_constant_expression_p
);
14390 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14392 RECUR (TREE_OPERAND (t
, 1)),
14393 complain
|decltype_flag
));
14399 vec
<tree
, va_gc
> *call_args
;
14400 unsigned int nargs
, i
;
14405 function
= CALL_EXPR_FN (t
);
14406 /* When we parsed the expression, we determined whether or
14407 not Koenig lookup should be performed. */
14408 koenig_p
= KOENIG_LOOKUP_P (t
);
14409 if (TREE_CODE (function
) == SCOPE_REF
)
14411 qualified_p
= true;
14412 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14414 /*address_p=*/false);
14416 else if (koenig_p
&& identifier_p (function
))
14418 /* Do nothing; calling tsubst_copy_and_build on an identifier
14419 would incorrectly perform unqualified lookup again.
14421 Note that we can also have an IDENTIFIER_NODE if the earlier
14422 unqualified lookup found a member function; in that case
14423 koenig_p will be false and we do want to do the lookup
14424 again to find the instantiated member function.
14426 FIXME but doing that causes c++/15272, so we need to stop
14427 using IDENTIFIER_NODE in that situation. */
14428 qualified_p
= false;
14432 if (TREE_CODE (function
) == COMPONENT_REF
)
14434 tree op
= TREE_OPERAND (function
, 1);
14436 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14437 || (BASELINK_P (op
)
14438 && BASELINK_QUALIFIED_P (op
)));
14441 qualified_p
= false;
14443 if (TREE_CODE (function
) == ADDR_EXPR
14444 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14445 /* Avoid error about taking the address of a constructor. */
14446 function
= TREE_OPERAND (function
, 0);
14448 function
= tsubst_copy_and_build (function
, args
, complain
,
14451 integral_constant_expression_p
);
14453 if (BASELINK_P (function
))
14454 qualified_p
= true;
14457 nargs
= call_expr_nargs (t
);
14458 call_args
= make_tree_vector ();
14459 for (i
= 0; i
< nargs
; ++i
)
14461 tree arg
= CALL_EXPR_ARG (t
, i
);
14463 if (!PACK_EXPANSION_P (arg
))
14464 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14467 /* Expand the pack expansion and push each entry onto
14469 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14470 if (TREE_CODE (arg
) == TREE_VEC
)
14472 unsigned int len
, j
;
14474 len
= TREE_VEC_LENGTH (arg
);
14475 for (j
= 0; j
< len
; ++j
)
14477 tree value
= TREE_VEC_ELT (arg
, j
);
14478 if (value
!= NULL_TREE
)
14479 value
= convert_from_reference (value
);
14480 vec_safe_push (call_args
, value
);
14485 /* A partial substitution. Add one entry. */
14486 vec_safe_push (call_args
, arg
);
14491 /* We do not perform argument-dependent lookup if normal
14492 lookup finds a non-function, in accordance with the
14493 expected resolution of DR 218. */
14495 && ((is_overloaded_fn (function
)
14496 /* If lookup found a member function, the Koenig lookup is
14497 not appropriate, even if an unqualified-name was used
14498 to denote the function. */
14499 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14500 || identifier_p (function
))
14501 /* Only do this when substitution turns a dependent call
14502 into a non-dependent call. */
14503 && type_dependent_expression_p_push (t
)
14504 && !any_type_dependent_arguments_p (call_args
))
14505 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
14507 if (identifier_p (function
)
14508 && !any_type_dependent_arguments_p (call_args
))
14510 if (koenig_p
&& (complain
& tf_warning_or_error
))
14512 /* For backwards compatibility and good diagnostics, try
14513 the unqualified lookup again if we aren't in SFINAE
14515 tree unq
= (tsubst_copy_and_build
14516 (function
, args
, complain
, in_decl
, true,
14517 integral_constant_expression_p
));
14518 if (unq
== error_mark_node
)
14519 RETURN (error_mark_node
);
14521 if (unq
!= function
)
14524 if (INDIRECT_REF_P (fn
))
14525 fn
= TREE_OPERAND (fn
, 0);
14526 if (TREE_CODE (fn
) == COMPONENT_REF
)
14527 fn
= TREE_OPERAND (fn
, 1);
14528 if (is_overloaded_fn (fn
))
14529 fn
= get_first_fn (fn
);
14530 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
14531 "%qD was not declared in this scope, "
14532 "and no declarations were found by "
14533 "argument-dependent lookup at the point "
14534 "of instantiation", function
))
14537 /* Can't say anything more. */;
14538 else if (DECL_CLASS_SCOPE_P (fn
))
14540 location_t loc
= EXPR_LOC_OR_LOC (t
,
14543 "declarations in dependent base %qT are "
14544 "not found by unqualified lookup",
14545 DECL_CLASS_CONTEXT (fn
));
14546 if (current_class_ptr
)
14548 "use %<this->%D%> instead", function
);
14551 "use %<%T::%D%> instead",
14552 current_class_name
, function
);
14555 inform (0, "%q+D declared here, later in the "
14556 "translation unit", fn
);
14561 if (identifier_p (function
))
14563 if (complain
& tf_error
)
14564 unqualified_name_lookup_error (function
);
14565 release_tree_vector (call_args
);
14566 RETURN (error_mark_node
);
14570 /* Remember that there was a reference to this entity. */
14571 if (DECL_P (function
))
14572 mark_used (function
);
14574 /* Put back tf_decltype for the actual call. */
14575 complain
|= decltype_flag
;
14577 if (TREE_CODE (function
) == OFFSET_REF
)
14578 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14580 else if (TREE_CODE (function
) == COMPONENT_REF
)
14582 tree instance
= TREE_OPERAND (function
, 0);
14583 tree fn
= TREE_OPERAND (function
, 1);
14585 if (processing_template_decl
14586 && (type_dependent_expression_p (instance
)
14587 || (!BASELINK_P (fn
)
14588 && TREE_CODE (fn
) != FIELD_DECL
)
14589 || type_dependent_expression_p (fn
)
14590 || any_type_dependent_arguments_p (call_args
)))
14591 ret
= build_nt_call_vec (function
, call_args
);
14592 else if (!BASELINK_P (fn
))
14593 ret
= finish_call_expr (function
, &call_args
,
14594 /*disallow_virtual=*/false,
14595 /*koenig_p=*/false,
14598 ret
= (build_new_method_call
14600 &call_args
, NULL_TREE
,
14601 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14606 ret
= finish_call_expr (function
, &call_args
,
14607 /*disallow_virtual=*/qualified_p
,
14611 release_tree_vector (call_args
);
14618 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14621 if (TREE_CODE (cond
) == INTEGER_CST
)
14623 if (integer_zerop (cond
))
14625 ++c_inhibit_evaluation_warnings
;
14626 exp1
= RECUR (TREE_OPERAND (t
, 1));
14627 --c_inhibit_evaluation_warnings
;
14628 exp2
= RECUR (TREE_OPERAND (t
, 2));
14632 exp1
= RECUR (TREE_OPERAND (t
, 1));
14633 ++c_inhibit_evaluation_warnings
;
14634 exp2
= RECUR (TREE_OPERAND (t
, 2));
14635 --c_inhibit_evaluation_warnings
;
14640 exp1
= RECUR (TREE_OPERAND (t
, 1));
14641 exp2
= RECUR (TREE_OPERAND (t
, 2));
14644 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14645 cond
, exp1
, exp2
, complain
));
14648 case PSEUDO_DTOR_EXPR
:
14649 RETURN (finish_pseudo_destructor_expr
14650 (RECUR (TREE_OPERAND (t
, 0)),
14651 RECUR (TREE_OPERAND (t
, 1)),
14652 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14657 tree purpose
, value
, chain
;
14659 if (t
== void_list_node
)
14662 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14663 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14665 /* We have pack expansions, so expand those and
14666 create a new list out of it. */
14667 tree purposevec
= NULL_TREE
;
14668 tree valuevec
= NULL_TREE
;
14672 /* Expand the argument expressions. */
14673 if (TREE_PURPOSE (t
))
14674 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14675 complain
, in_decl
);
14676 if (TREE_VALUE (t
))
14677 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14678 complain
, in_decl
);
14680 /* Build the rest of the list. */
14681 chain
= TREE_CHAIN (t
);
14682 if (chain
&& chain
!= void_type_node
)
14683 chain
= RECUR (chain
);
14685 /* Determine the number of arguments. */
14686 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14688 len
= TREE_VEC_LENGTH (purposevec
);
14689 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14691 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14692 len
= TREE_VEC_LENGTH (valuevec
);
14695 /* Since we only performed a partial substitution into
14696 the argument pack, we only RETURN (a single list
14698 if (purposevec
== TREE_PURPOSE (t
)
14699 && valuevec
== TREE_VALUE (t
)
14700 && chain
== TREE_CHAIN (t
))
14703 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14706 /* Convert the argument vectors into a TREE_LIST */
14710 /* Grab the Ith values. */
14712 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14715 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14718 /* Build the list (backwards). */
14719 chain
= tree_cons (purpose
, value
, chain
);
14725 purpose
= TREE_PURPOSE (t
);
14727 purpose
= RECUR (purpose
);
14728 value
= TREE_VALUE (t
);
14730 value
= RECUR (value
);
14731 chain
= TREE_CHAIN (t
);
14732 if (chain
&& chain
!= void_type_node
)
14733 chain
= RECUR (chain
);
14734 if (purpose
== TREE_PURPOSE (t
)
14735 && value
== TREE_VALUE (t
)
14736 && chain
== TREE_CHAIN (t
))
14738 RETURN (tree_cons (purpose
, value
, chain
));
14741 case COMPONENT_REF
:
14747 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14748 args
, complain
, in_decl
);
14749 /* Remember that there was a reference to this entity. */
14750 if (DECL_P (object
))
14751 mark_used (object
);
14752 object_type
= TREE_TYPE (object
);
14754 member
= TREE_OPERAND (t
, 1);
14755 if (BASELINK_P (member
))
14756 member
= tsubst_baselink (member
,
14757 non_reference (TREE_TYPE (object
)),
14758 args
, complain
, in_decl
);
14760 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14761 if (member
== error_mark_node
)
14762 RETURN (error_mark_node
);
14764 if (type_dependent_expression_p (object
))
14765 /* We can't do much here. */;
14766 else if (!CLASS_TYPE_P (object_type
))
14768 if (scalarish_type_p (object_type
))
14770 tree s
= NULL_TREE
;
14771 tree dtor
= member
;
14773 if (TREE_CODE (dtor
) == SCOPE_REF
)
14775 s
= TREE_OPERAND (dtor
, 0);
14776 dtor
= TREE_OPERAND (dtor
, 1);
14778 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14780 dtor
= TREE_OPERAND (dtor
, 0);
14782 RETURN (finish_pseudo_destructor_expr
14783 (object
, s
, dtor
, input_location
));
14787 else if (TREE_CODE (member
) == SCOPE_REF
14788 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14790 /* Lookup the template functions now that we know what the
14792 tree scope
= TREE_OPERAND (member
, 0);
14793 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14794 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14795 member
= lookup_qualified_name (scope
, tmpl
,
14796 /*is_type_p=*/false,
14797 /*complain=*/false);
14798 if (BASELINK_P (member
))
14800 BASELINK_FUNCTIONS (member
)
14801 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14803 member
= (adjust_result_of_qualified_name_lookup
14804 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14809 qualified_name_lookup_error (scope
, tmpl
, member
,
14811 RETURN (error_mark_node
);
14814 else if (TREE_CODE (member
) == SCOPE_REF
14815 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14816 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14818 if (complain
& tf_error
)
14820 if (TYPE_P (TREE_OPERAND (member
, 0)))
14821 error ("%qT is not a class or namespace",
14822 TREE_OPERAND (member
, 0));
14824 error ("%qD is not a class or namespace",
14825 TREE_OPERAND (member
, 0));
14827 RETURN (error_mark_node
);
14829 else if (TREE_CODE (member
) == FIELD_DECL
)
14830 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14832 RETURN (finish_class_member_access_expr (object
, member
,
14833 /*template_p=*/false,
14838 RETURN (build_throw
14839 (RECUR (TREE_OPERAND (t
, 0))));
14843 vec
<constructor_elt
, va_gc
> *n
;
14844 constructor_elt
*ce
;
14845 unsigned HOST_WIDE_INT idx
;
14846 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14847 bool process_index_p
;
14849 bool need_copy_p
= false;
14852 if (type
== error_mark_node
)
14853 RETURN (error_mark_node
);
14855 /* digest_init will do the wrong thing if we let it. */
14856 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14859 /* We do not want to process the index of aggregate
14860 initializers as they are identifier nodes which will be
14861 looked up by digest_init. */
14862 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14864 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14865 newlen
= vec_safe_length (n
);
14866 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14868 if (ce
->index
&& process_index_p
14869 /* An identifier index is looked up in the type
14870 being initialized, not the current scope. */
14871 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14872 ce
->index
= RECUR (ce
->index
);
14874 if (PACK_EXPANSION_P (ce
->value
))
14876 /* Substitute into the pack expansion. */
14877 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14880 if (ce
->value
== error_mark_node
14881 || PACK_EXPANSION_P (ce
->value
))
14883 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14884 /* Just move the argument into place. */
14885 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14888 /* Update the length of the final CONSTRUCTOR
14889 arguments vector, and note that we will need to
14891 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14892 need_copy_p
= true;
14896 ce
->value
= RECUR (ce
->value
);
14901 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14903 vec_alloc (n
, newlen
);
14904 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14906 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14908 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14909 for (i
= 0; i
< len
; ++i
)
14910 CONSTRUCTOR_APPEND_ELT (n
, 0,
14911 TREE_VEC_ELT (ce
->value
, i
));
14914 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14918 r
= build_constructor (init_list_type_node
, n
);
14919 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14921 if (TREE_HAS_CONSTRUCTOR (t
))
14922 RETURN (finish_compound_literal (type
, r
, complain
));
14924 TREE_TYPE (r
) = type
;
14930 tree operand_0
= TREE_OPERAND (t
, 0);
14931 if (TYPE_P (operand_0
))
14933 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14934 RETURN (get_typeid (operand_0
, complain
));
14938 operand_0
= RECUR (operand_0
);
14939 RETURN (build_typeid (operand_0
, complain
));
14946 else if (DECL_PACK_P (t
))
14948 /* We don't build decls for an instantiation of a
14949 variadic capture proxy, we instantiate the elements
14951 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14952 return RECUR (DECL_VALUE_EXPR (t
));
14958 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14960 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14961 /* If the original type was a reference, we'll be wrapped in
14962 the appropriate INDIRECT_REF. */
14963 r
= convert_from_reference (r
);
14968 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14969 RECUR (TREE_OPERAND (t
, 0)),
14970 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14972 case OFFSETOF_EXPR
:
14973 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14977 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
14978 complain
, in_decl
);
14980 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14982 type2
= tsubst (type2
, args
, complain
, in_decl
);
14984 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14989 tree old_stmt_expr
= cur_stmt_expr
;
14990 tree stmt_expr
= begin_stmt_expr ();
14992 cur_stmt_expr
= stmt_expr
;
14993 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14994 integral_constant_expression_p
);
14995 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14996 cur_stmt_expr
= old_stmt_expr
;
14998 /* If the resulting list of expression statement is empty,
14999 fold it further into void_zero_node. */
15000 if (empty_expr_stmt_p (stmt_expr
))
15001 stmt_expr
= void_zero_node
;
15003 RETURN (stmt_expr
);
15008 tree r
= build_lambda_expr ();
15010 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15011 LAMBDA_EXPR_CLOSURE (r
) = type
;
15012 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15014 LAMBDA_EXPR_LOCATION (r
)
15015 = LAMBDA_EXPR_LOCATION (t
);
15016 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15017 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15018 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15019 LAMBDA_EXPR_DISCRIMINATOR (r
)
15020 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15021 /* For a function scope, we want to use tsubst so that we don't
15022 complain about referring to an auto function before its return
15023 type has been deduced. Otherwise, we want to use tsubst_copy so
15024 that we look up the existing field/parameter/variable rather
15025 than build a new one. */
15026 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15027 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15028 scope
= tsubst (scope
, args
, complain
, in_decl
);
15029 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15031 /* Look up the parameter we want directly, as tsubst_copy
15032 doesn't do what we need. */
15033 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15034 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15035 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15036 parm
= DECL_CHAIN (parm
);
15038 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15039 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15040 DECL_CONTEXT (scope
) = fn
;
15043 scope
= RECUR (scope
);
15044 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15045 LAMBDA_EXPR_RETURN_TYPE (r
)
15046 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15048 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15049 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15051 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15052 determine_visibility (TYPE_NAME (type
));
15053 /* Now that we know visibility, instantiate the type so we have a
15054 declaration of the op() for later calls to lambda_function. */
15055 complete_type (type
);
15057 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15059 RETURN (build_lambda_object (r
));
15063 /* We can get here for a constant initializer of non-dependent type.
15064 FIXME stop folding in cp_parser_initializer_clause. */
15066 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15071 case TRANSACTION_EXPR
:
15072 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15073 integral_constant_expression_p
));
15076 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15078 case VEC_PERM_EXPR
:
15079 RETURN (build_x_vec_perm_expr (input_location
,
15080 RECUR (TREE_OPERAND (t
, 0)),
15081 RECUR (TREE_OPERAND (t
, 1)),
15082 RECUR (TREE_OPERAND (t
, 2)),
15086 /* Handle Objective-C++ constructs, if appropriate. */
15089 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15090 in_decl
, /*function_p=*/false);
15094 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15100 input_location
= loc
;
15104 /* Verify that the instantiated ARGS are valid. For type arguments,
15105 make sure that the type's linkage is ok. For non-type arguments,
15106 make sure they are constants if they are integral or enumerations.
15107 Emit an error under control of COMPLAIN, and return TRUE on error. */
15110 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15112 if (dependent_template_arg_p (t
))
15114 if (ARGUMENT_PACK_P (t
))
15116 tree vec
= ARGUMENT_PACK_ARGS (t
);
15117 int len
= TREE_VEC_LENGTH (vec
);
15118 bool result
= false;
15121 for (i
= 0; i
< len
; ++i
)
15122 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15126 else if (TYPE_P (t
))
15128 /* [basic.link]: A name with no linkage (notably, the name
15129 of a class or enumeration declared in a local scope)
15130 shall not be used to declare an entity with linkage.
15131 This implies that names with no linkage cannot be used as
15134 DR 757 relaxes this restriction for C++0x. */
15135 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15136 : no_linkage_check (t
, /*relaxed_p=*/false));
15140 /* DR 488 makes use of a type with no linkage cause
15141 type deduction to fail. */
15142 if (complain
& tf_error
)
15144 if (TYPE_ANONYMOUS_P (nt
))
15145 error ("%qT is/uses anonymous type", t
);
15147 error ("template argument for %qD uses local type %qT",
15152 /* In order to avoid all sorts of complications, we do not
15153 allow variably-modified types as template arguments. */
15154 else if (variably_modified_type_p (t
, NULL_TREE
))
15156 if (complain
& tf_error
)
15157 error ("%qT is a variably modified type", t
);
15161 /* Class template and alias template arguments should be OK. */
15162 else if (DECL_TYPE_TEMPLATE_P (t
))
15164 /* A non-type argument of integral or enumerated type must be a
15166 else if (TREE_TYPE (t
)
15167 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15168 && !TREE_CONSTANT (t
))
15170 if (complain
& tf_error
)
15171 error ("integral expression %qE is not constant", t
);
15178 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15180 int ix
, len
= DECL_NTPARMS (tmpl
);
15181 bool result
= false;
15183 for (ix
= 0; ix
!= len
; ix
++)
15185 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15188 if (result
&& (complain
& tf_error
))
15189 error (" trying to instantiate %qD", tmpl
);
15193 /* We're out of SFINAE context now, so generate diagnostics for the access
15194 errors we saw earlier when instantiating D from TMPL and ARGS. */
15197 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15199 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15200 tree type
= TREE_TYPE (pattern
);
15201 location_t loc
= input_location
;
15203 push_access_scope (d
);
15204 push_deferring_access_checks (dk_no_deferred
);
15205 input_location
= DECL_SOURCE_LOCATION (pattern
);
15206 tsubst (type
, args
, tf_warning_or_error
, d
);
15207 input_location
= loc
;
15208 pop_deferring_access_checks ();
15209 pop_access_scope (d
);
15212 /* Instantiate the indicated variable, function, or alias template TMPL with
15213 the template arguments in TARG_PTR. */
15216 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15218 tree targ_ptr
= orig_args
;
15222 bool access_ok
= true;
15224 if (tmpl
== error_mark_node
)
15225 return error_mark_node
;
15227 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15229 /* If this function is a clone, handle it specially. */
15230 if (DECL_CLONED_FUNCTION_P (tmpl
))
15235 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15236 DECL_CLONED_FUNCTION. */
15237 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15238 targ_ptr
, complain
);
15239 if (spec
== error_mark_node
)
15240 return error_mark_node
;
15242 /* Look for the clone. */
15243 FOR_EACH_CLONE (clone
, spec
)
15244 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15246 /* We should always have found the clone by now. */
15247 gcc_unreachable ();
15251 /* Check to see if we already have this specialization. */
15252 gen_tmpl
= most_general_template (tmpl
);
15253 if (tmpl
!= gen_tmpl
)
15254 /* The TMPL is a partial instantiation. To get a full set of
15255 arguments we must add the arguments used to perform the
15256 partial instantiation. */
15257 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15260 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15261 but it doesn't seem to be on the hot path. */
15262 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15264 gcc_assert (tmpl
== gen_tmpl
15265 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15267 || fndecl
== NULL_TREE
);
15269 if (spec
!= NULL_TREE
)
15271 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15273 if (complain
& tf_error
)
15274 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15275 return error_mark_node
;
15280 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15282 return error_mark_node
;
15284 /* We are building a FUNCTION_DECL, during which the access of its
15285 parameters and return types have to be checked. However this
15286 FUNCTION_DECL which is the desired context for access checking
15287 is not built yet. We solve this chicken-and-egg problem by
15288 deferring all checks until we have the FUNCTION_DECL. */
15289 push_deferring_access_checks (dk_deferred
);
15291 /* Instantiation of the function happens in the context of the function
15292 template, not the context of the overload resolution we're doing. */
15293 push_to_top_level ();
15294 /* If there are dependent arguments, e.g. because we're doing partial
15295 ordering, make sure processing_template_decl stays set. */
15296 if (uses_template_parms (targ_ptr
))
15297 ++processing_template_decl
;
15298 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15300 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15301 complain
, gen_tmpl
);
15302 push_nested_class (ctx
);
15304 /* Substitute template parameters to obtain the specialization. */
15305 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15306 targ_ptr
, complain
, gen_tmpl
);
15307 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15308 pop_nested_class ();
15309 pop_from_top_level ();
15311 if (fndecl
== error_mark_node
)
15313 pop_deferring_access_checks ();
15314 return error_mark_node
;
15317 /* The DECL_TI_TEMPLATE should always be the immediate parent
15318 template, not the most general template. */
15319 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15321 /* Now we know the specialization, compute access previously
15323 push_access_scope (fndecl
);
15324 if (!perform_deferred_access_checks (complain
))
15326 pop_access_scope (fndecl
);
15327 pop_deferring_access_checks ();
15329 /* If we've just instantiated the main entry point for a function,
15330 instantiate all the alternate entry points as well. We do this
15331 by cloning the instantiation of the main entry point, not by
15332 instantiating the template clones. */
15333 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15334 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15338 if (!(complain
& tf_error
))
15340 /* Remember to reinstantiate when we're out of SFINAE so the user
15341 can see the errors. */
15342 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15344 return error_mark_node
;
15349 /* Wrapper for instantiate_template_1. */
15352 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15355 timevar_push (TV_TEMPLATE_INST
);
15356 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15357 timevar_pop (TV_TEMPLATE_INST
);
15361 /* Instantiate the alias template TMPL with ARGS. Also push a template
15362 instantiation level, which instantiate_template doesn't do because
15363 functions and variables have sufficient context established by the
15367 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15369 struct pending_template
*old_last_pend
= last_pending_template
;
15370 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15371 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15372 return error_mark_node
;
15373 tree tinst
= build_tree_list (tmpl
, args
);
15374 if (!push_tinst_level (tinst
))
15377 return error_mark_node
;
15381 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15382 args
, tmpl
, complain
,
15383 /*require_all_args=*/true,
15384 /*use_default_args=*/true);
15386 tree r
= instantiate_template (tmpl
, args
, complain
);
15387 pop_tinst_level ();
15388 /* We can't free this if a pending_template entry or last_error_tinst_level
15389 is pointing at it. */
15390 if (last_pending_template
== old_last_pend
15391 && last_error_tinst_level
== old_error_tinst
)
15397 /* PARM is a template parameter pack for FN. Returns true iff
15398 PARM is used in a deducible way in the argument list of FN. */
15401 pack_deducible_p (tree parm
, tree fn
)
15403 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15404 for (; t
; t
= TREE_CHAIN (t
))
15406 tree type
= TREE_VALUE (t
);
15408 if (!PACK_EXPANSION_P (type
))
15410 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15411 packs
; packs
= TREE_CHAIN (packs
))
15412 if (TREE_VALUE (packs
) == parm
)
15414 /* The template parameter pack is used in a function parameter
15415 pack. If this is the end of the parameter list, the
15416 template parameter pack is deducible. */
15417 if (TREE_CHAIN (t
) == void_list_node
)
15420 /* Otherwise, not. Well, it could be deduced from
15421 a non-pack parameter, but doing so would end up with
15422 a deduction mismatch, so don't bother. */
15426 /* The template parameter pack isn't used in any function parameter
15427 packs, but it might be used deeper, e.g. tuple<Args...>. */
15431 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15432 NARGS elements of the arguments that are being used when calling
15433 it. TARGS is a vector into which the deduced template arguments
15436 Returns either a FUNCTION_DECL for the matching specialization of FN or
15437 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15438 true, diagnostics will be printed to explain why it failed.
15440 If FN is a conversion operator, or we are trying to produce a specific
15441 specialization, RETURN_TYPE is the return type desired.
15443 The EXPLICIT_TARGS are explicit template arguments provided via a
15446 The parameter STRICT is one of:
15449 We are deducing arguments for a function call, as in
15450 [temp.deduct.call].
15453 We are deducing arguments for a conversion function, as in
15454 [temp.deduct.conv].
15457 We are deducing arguments when doing an explicit instantiation
15458 as in [temp.explicit], when determining an explicit specialization
15459 as in [temp.expl.spec], or when taking the address of a function
15460 template, as in [temp.deduct.funcaddr]. */
15463 fn_type_unification (tree fn
,
15464 tree explicit_targs
,
15467 unsigned int nargs
,
15469 unification_kind_t strict
,
15476 tree decl
= NULL_TREE
;
15477 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15479 static int deduction_depth
;
15480 struct pending_template
*old_last_pend
= last_pending_template
;
15481 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15482 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15484 tree r
= error_mark_node
;
15487 complain
|= tf_decltype
;
15489 /* In C++0x, it's possible to have a function template whose type depends
15490 on itself recursively. This is most obvious with decltype, but can also
15491 occur with enumeration scope (c++/48969). So we need to catch infinite
15492 recursion and reject the substitution at deduction time; this function
15493 will return error_mark_node for any repeated substitution.
15495 This also catches excessive recursion such as when f<N> depends on
15496 f<N-1> across all integers, and returns error_mark_node for all the
15497 substitutions back up to the initial one.
15499 This is, of course, not reentrant. */
15500 if (excessive_deduction_depth
)
15501 return error_mark_node
;
15502 tinst
= build_tree_list (fn
, NULL_TREE
);
15505 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15507 fntype
= TREE_TYPE (fn
);
15508 if (explicit_targs
)
15512 The specified template arguments must match the template
15513 parameters in kind (i.e., type, nontype, template), and there
15514 must not be more arguments than there are parameters;
15515 otherwise type deduction fails.
15517 Nontype arguments must match the types of the corresponding
15518 nontype template parameters, or must be convertible to the
15519 types of the corresponding nontype parameters as specified in
15520 _temp.arg.nontype_, otherwise type deduction fails.
15522 All references in the function type of the function template
15523 to the corresponding template parameters are replaced by the
15524 specified template argument values. If a substitution in a
15525 template parameter or in the function type of the function
15526 template results in an invalid type, type deduction fails. */
15527 int i
, len
= TREE_VEC_LENGTH (tparms
);
15528 location_t loc
= input_location
;
15529 bool incomplete
= false;
15531 /* Adjust any explicit template arguments before entering the
15532 substitution context. */
15534 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15536 /*require_all_args=*/false,
15537 /*use_default_args=*/false));
15538 if (explicit_targs
== error_mark_node
)
15541 /* Substitute the explicit args into the function type. This is
15542 necessary so that, for instance, explicitly declared function
15543 arguments can match null pointed constants. If we were given
15544 an incomplete set of explicit args, we must not do semantic
15545 processing during substitution as we could create partial
15547 for (i
= 0; i
< len
; i
++)
15549 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15550 bool parameter_pack
= false;
15551 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15553 /* Dig out the actual parm. */
15554 if (TREE_CODE (parm
) == TYPE_DECL
15555 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15557 parm
= TREE_TYPE (parm
);
15558 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15560 else if (TREE_CODE (parm
) == PARM_DECL
)
15562 parm
= DECL_INITIAL (parm
);
15563 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15566 if (!parameter_pack
&& targ
== NULL_TREE
)
15567 /* No explicit argument for this template parameter. */
15570 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15572 /* Mark the argument pack as "incomplete". We could
15573 still deduce more arguments during unification.
15574 We remove this mark in type_unification_real. */
15577 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15578 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15579 = ARGUMENT_PACK_ARGS (targ
);
15582 /* We have some incomplete argument packs. */
15587 TREE_VALUE (tinst
) = explicit_targs
;
15588 if (!push_tinst_level (tinst
))
15590 excessive_deduction_depth
= true;
15593 processing_template_decl
+= incomplete
;
15594 input_location
= DECL_SOURCE_LOCATION (fn
);
15595 /* Ignore any access checks; we'll see them again in
15596 instantiate_template and they might have the wrong
15597 access path at this point. */
15598 push_deferring_access_checks (dk_deferred
);
15599 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15600 complain
| tf_partial
, NULL_TREE
);
15601 pop_deferring_access_checks ();
15602 input_location
= loc
;
15603 processing_template_decl
-= incomplete
;
15604 pop_tinst_level ();
15606 if (fntype
== error_mark_node
)
15609 /* Place the explicitly specified arguments in TARGS. */
15610 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15611 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15614 /* Never do unification on the 'this' parameter. */
15615 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15621 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15622 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15623 new_args
[0] = return_type
;
15624 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15629 /* We allow incomplete unification without an error message here
15630 because the standard doesn't seem to explicitly prohibit it. Our
15631 callers must be ready to deal with unification failures in any
15634 TREE_VALUE (tinst
) = targs
;
15635 /* If we aren't explaining yet, push tinst context so we can see where
15636 any errors (e.g. from class instantiations triggered by instantiation
15637 of default template arguments) come from. If we are explaining, this
15638 context is redundant. */
15639 if (!explain_p
&& !push_tinst_level (tinst
))
15641 excessive_deduction_depth
= true;
15645 /* type_unification_real will pass back any access checks from default
15646 template argument substitution. */
15647 vec
<deferred_access_check
, va_gc
> *checks
;
15650 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15651 targs
, parms
, args
, nargs
, /*subr=*/0,
15652 strict
, flags
, &checks
, explain_p
);
15654 pop_tinst_level ();
15658 /* Now that we have bindings for all of the template arguments,
15659 ensure that the arguments deduced for the template template
15660 parameters have compatible template parameter lists. We cannot
15661 check this property before we have deduced all template
15662 arguments, because the template parameter types of a template
15663 template parameter might depend on prior template parameters
15664 deduced after the template template parameter. The following
15665 ill-formed example illustrates this issue:
15667 template<typename T, template<T> class C> void f(C<5>, T);
15669 template<int N> struct X {};
15672 f(X<5>(), 5l); // error: template argument deduction fails
15675 The template parameter list of 'C' depends on the template type
15676 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15677 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15678 time that we deduce 'C'. */
15679 if (!template_template_parm_bindings_ok_p
15680 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15682 unify_inconsistent_template_template_parameters (explain_p
);
15686 /* All is well so far. Now, check:
15690 When all template arguments have been deduced, all uses of
15691 template parameters in nondeduced contexts are replaced with
15692 the corresponding deduced argument values. If the
15693 substitution results in an invalid type, as described above,
15694 type deduction fails. */
15695 TREE_VALUE (tinst
) = targs
;
15696 if (!push_tinst_level (tinst
))
15698 excessive_deduction_depth
= true;
15702 /* Also collect access checks from the instantiation. */
15703 reopen_deferring_access_checks (checks
);
15705 decl
= instantiate_template (fn
, targs
, complain
);
15707 checks
= get_deferred_access_checks ();
15708 pop_deferring_access_checks ();
15710 pop_tinst_level ();
15712 if (decl
== error_mark_node
)
15715 /* Now perform any access checks encountered during substitution. */
15716 push_access_scope (decl
);
15717 ok
= perform_access_checks (checks
, complain
);
15718 pop_access_scope (decl
);
15722 /* If we're looking for an exact match, check that what we got
15723 is indeed an exact match. It might not be if some template
15724 parameters are used in non-deduced contexts. */
15725 if (strict
== DEDUCE_EXACT
)
15727 tree substed
= TREE_TYPE (decl
);
15731 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15733 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15734 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15735 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15737 unify_type_mismatch (explain_p
, args
[i
],
15738 TREE_VALUE (sarg
));
15747 if (excessive_deduction_depth
)
15749 if (deduction_depth
== 0)
15750 /* Reset once we're all the way out. */
15751 excessive_deduction_depth
= false;
15754 /* We can't free this if a pending_template entry or last_error_tinst_level
15755 is pointing at it. */
15756 if (last_pending_template
== old_last_pend
15757 && last_error_tinst_level
== old_error_tinst
)
15763 /* Adjust types before performing type deduction, as described in
15764 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15765 sections are symmetric. PARM is the type of a function parameter
15766 or the return type of the conversion function. ARG is the type of
15767 the argument passed to the call, or the type of the value
15768 initialized with the result of the conversion function.
15769 ARG_EXPR is the original argument expression, which may be null. */
15772 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15786 /* Swap PARM and ARG throughout the remainder of this
15787 function; the handling is precisely symmetric since PARM
15788 will initialize ARG rather than vice versa. */
15796 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15797 too, but here handle it by stripping the reference from PARM
15798 rather than by adding it to ARG. */
15799 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15800 && TYPE_REF_IS_RVALUE (*parm
)
15801 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15802 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15803 && TREE_CODE (*arg
) == REFERENCE_TYPE
15804 && !TYPE_REF_IS_RVALUE (*arg
))
15805 *parm
= TREE_TYPE (*parm
);
15806 /* Nothing else to do in this case. */
15810 gcc_unreachable ();
15813 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15815 /* [temp.deduct.call]
15817 If P is not a reference type:
15819 --If A is an array type, the pointer type produced by the
15820 array-to-pointer standard conversion (_conv.array_) is
15821 used in place of A for type deduction; otherwise,
15823 --If A is a function type, the pointer type produced by
15824 the function-to-pointer standard conversion
15825 (_conv.func_) is used in place of A for type deduction;
15828 --If A is a cv-qualified type, the top level
15829 cv-qualifiers of A's type are ignored for type
15831 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15832 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15833 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15834 *arg
= build_pointer_type (*arg
);
15836 *arg
= TYPE_MAIN_VARIANT (*arg
);
15839 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15840 of the form T&&, where T is a template parameter, and the argument
15841 is an lvalue, T is deduced as A& */
15842 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15843 && TYPE_REF_IS_RVALUE (*parm
)
15844 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15845 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15846 && (arg_expr
? real_lvalue_p (arg_expr
)
15847 /* try_one_overload doesn't provide an arg_expr, but
15848 functions are always lvalues. */
15849 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15850 *arg
= build_reference_type (*arg
);
15852 /* [temp.deduct.call]
15854 If P is a cv-qualified type, the top level cv-qualifiers
15855 of P's type are ignored for type deduction. If P is a
15856 reference type, the type referred to by P is used for
15858 *parm
= TYPE_MAIN_VARIANT (*parm
);
15859 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15861 *parm
= TREE_TYPE (*parm
);
15862 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15865 /* DR 322. For conversion deduction, remove a reference type on parm
15866 too (which has been swapped into ARG). */
15867 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15868 *arg
= TREE_TYPE (*arg
);
15873 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15874 template which does contain any deducible template parameters; check if
15875 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15876 unify_one_argument. */
15879 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15880 int flags
, bool explain_p
)
15885 type
= TREE_TYPE (arg
);
15889 if (same_type_p (parm
, type
))
15890 return unify_success (explain_p
);
15892 if (strict
== DEDUCE_CONV
)
15894 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15895 explain_p
? tf_warning_or_error
: tf_none
))
15896 return unify_success (explain_p
);
15898 else if (strict
!= DEDUCE_EXACT
)
15900 if (can_convert_arg (parm
, type
,
15901 TYPE_P (arg
) ? NULL_TREE
: arg
,
15902 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15903 return unify_success (explain_p
);
15906 if (strict
== DEDUCE_EXACT
)
15907 return unify_type_mismatch (explain_p
, parm
, arg
);
15909 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15912 static bool uses_deducible_template_parms (tree type
);
15914 /* Returns true iff the expression EXPR is one from which a template
15915 argument can be deduced. In other words, if it's an undecorated
15916 use of a template non-type parameter. */
15919 deducible_expression (tree expr
)
15921 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15924 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15925 deducible way; that is, if it has a max value of <PARM> - 1. */
15928 deducible_array_bound (tree domain
)
15930 if (domain
== NULL_TREE
)
15933 tree max
= TYPE_MAX_VALUE (domain
);
15934 if (TREE_CODE (max
) != MINUS_EXPR
)
15937 return deducible_expression (TREE_OPERAND (max
, 0));
15940 /* Returns true iff the template arguments ARGS use a template parameter
15941 in a deducible way. */
15944 deducible_template_args (tree args
)
15946 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15949 tree elt
= TREE_VEC_ELT (args
, i
);
15950 if (ARGUMENT_PACK_P (elt
))
15951 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15954 if (PACK_EXPANSION_P (elt
))
15955 elt
= PACK_EXPANSION_PATTERN (elt
);
15956 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15958 else if (TYPE_P (elt
))
15959 deducible
= uses_deducible_template_parms (elt
);
15961 deducible
= deducible_expression (elt
);
15969 /* Returns true iff TYPE contains any deducible references to template
15970 parameters, as per 14.8.2.5. */
15973 uses_deducible_template_parms (tree type
)
15975 if (PACK_EXPANSION_P (type
))
15976 type
= PACK_EXPANSION_PATTERN (type
);
15983 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15984 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15990 if (POINTER_TYPE_P (type
))
15991 return uses_deducible_template_parms (TREE_TYPE (type
));
15993 /* T[integer-constant ]
15995 if (TREE_CODE (type
) == ARRAY_TYPE
)
15996 return (uses_deducible_template_parms (TREE_TYPE (type
))
15997 || deducible_array_bound (TYPE_DOMAIN (type
)));
16009 if (TYPE_PTRMEM_P (type
))
16010 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16011 || (uses_deducible_template_parms
16012 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16014 /* template-name <T> (where template-name refers to a class template)
16015 template-name <i> (where template-name refers to a class template) */
16016 if (CLASS_TYPE_P (type
)
16017 && CLASSTYPE_TEMPLATE_INFO (type
)
16018 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16019 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16020 (CLASSTYPE_TI_ARGS (type
)));
16025 if (TREE_CODE (type
) == FUNCTION_TYPE
16026 || TREE_CODE (type
) == METHOD_TYPE
)
16028 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16030 tree parm
= TYPE_ARG_TYPES (type
);
16031 if (TREE_CODE (type
) == METHOD_TYPE
)
16032 parm
= TREE_CHAIN (parm
);
16033 for (; parm
; parm
= TREE_CHAIN (parm
))
16034 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16041 /* Subroutine of type_unification_real and unify_pack_expansion to
16042 handle unification of a single P/A pair. Parameters are as
16043 for those functions. */
16046 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16047 int subr
, unification_kind_t strict
, int flags
,
16050 tree arg_expr
= NULL_TREE
;
16053 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16054 return unify_invalid (explain_p
);
16055 if (arg
== unknown_type_node
)
16056 /* We can't deduce anything from this, but we might get all the
16057 template args from other function args. */
16058 return unify_success (explain_p
);
16060 /* Implicit conversions (Clause 4) will be performed on a function
16061 argument to convert it to the type of the corresponding function
16062 parameter if the parameter type contains no template-parameters that
16063 participate in template argument deduction. */
16064 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16065 /* For function parameters that contain no template-parameters at all,
16066 we have historically checked for convertibility in order to shortcut
16067 consideration of this candidate. */
16068 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16070 else if (strict
== DEDUCE_CALL
16071 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16072 /* For function parameters with only non-deducible template parameters,
16074 return unify_success (explain_p
);
16079 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16080 | UNIFY_ALLOW_MORE_CV_QUAL
16081 | UNIFY_ALLOW_DERIVED
);
16085 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16089 arg_strict
= UNIFY_ALLOW_NONE
;
16093 gcc_unreachable ();
16096 /* We only do these transformations if this is the top-level
16097 parameter_type_list in a call or declaration matching; in other
16098 situations (nested function declarators, template argument lists) we
16099 won't be comparing a type to an expression, and we don't do any type
16105 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16106 if (type_unknown_p (arg
))
16108 /* [temp.deduct.type] A template-argument can be
16109 deduced from a pointer to function or pointer
16110 to member function argument if the set of
16111 overloaded functions does not contain function
16112 templates and at most one of a set of
16113 overloaded functions provides a unique
16116 if (resolve_overloaded_unification
16117 (tparms
, targs
, parm
, arg
, strict
,
16118 arg_strict
, explain_p
))
16119 return unify_success (explain_p
);
16120 return unify_overload_resolution_failure (explain_p
, arg
);
16124 arg
= unlowered_expr_type (arg
);
16125 if (arg
== error_mark_node
)
16126 return unify_invalid (explain_p
);
16130 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16133 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16134 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16136 /* For deduction from an init-list we need the actual list. */
16137 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16139 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16142 /* Most parms like fn_type_unification.
16144 If SUBR is 1, we're being called recursively (to unify the
16145 arguments of a function or method parameter of a function
16148 CHECKS is a pointer to a vector of access checks encountered while
16149 substituting default template arguments. */
16152 type_unification_real (tree tparms
,
16156 unsigned int xnargs
,
16158 unification_kind_t strict
,
16160 vec
<deferred_access_check
, va_gc
> **checks
,
16165 int ntparms
= TREE_VEC_LENGTH (tparms
);
16166 int saw_undeduced
= 0;
16169 unsigned int nargs
;
16172 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16173 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16174 gcc_assert (ntparms
> 0);
16176 /* Reset the number of non-defaulted template arguments contained
16178 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16186 while (parms
&& parms
!= void_list_node
16189 parm
= TREE_VALUE (parms
);
16191 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16192 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16193 /* For a function parameter pack that occurs at the end of the
16194 parameter-declaration-list, the type A of each remaining
16195 argument of the call is compared with the type P of the
16196 declarator-id of the function parameter pack. */
16199 parms
= TREE_CHAIN (parms
);
16201 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16202 /* For a function parameter pack that does not occur at the
16203 end of the parameter-declaration-list, the type of the
16204 parameter pack is a non-deduced context. */
16210 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16216 && parms
!= void_list_node
16217 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16219 /* Unify the remaining arguments with the pack expansion type. */
16221 tree parmvec
= make_tree_vec (1);
16223 /* Allocate a TREE_VEC and copy in all of the arguments */
16224 argvec
= make_tree_vec (nargs
- ia
);
16225 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16226 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16228 /* Copy the parameter into parmvec. */
16229 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16230 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16231 /*subr=*/subr
, explain_p
))
16234 /* Advance to the end of the list of parameters. */
16235 parms
= TREE_CHAIN (parms
);
16238 /* Fail if we've reached the end of the parm list, and more args
16239 are present, and the parm list isn't variadic. */
16240 if (ia
< nargs
&& parms
== void_list_node
)
16241 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16242 /* Fail if parms are left and they don't have default values. */
16243 if (parms
&& parms
!= void_list_node
16244 && TREE_PURPOSE (parms
) == NULL_TREE
)
16246 unsigned int count
= nargs
;
16248 while (p
&& p
!= void_list_node
)
16251 p
= TREE_CHAIN (p
);
16253 return unify_too_few_arguments (explain_p
, ia
, count
);
16258 tsubst_flags_t complain
= (explain_p
16259 ? tf_warning_or_error
16262 for (i
= 0; i
< ntparms
; i
++)
16264 tree targ
= TREE_VEC_ELT (targs
, i
);
16265 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16267 /* Clear the "incomplete" flags on all argument packs now so that
16268 substituting them into later default arguments works. */
16269 if (targ
&& ARGUMENT_PACK_P (targ
))
16271 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16272 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16275 if (targ
|| tparm
== error_mark_node
)
16277 tparm
= TREE_VALUE (tparm
);
16279 /* If this is an undeduced nontype parameter that depends on
16280 a type parameter, try another pass; its type may have been
16281 deduced from a later argument than the one from which
16282 this parameter can be deduced. */
16283 if (TREE_CODE (tparm
) == PARM_DECL
16284 && uses_template_parms (TREE_TYPE (tparm
))
16285 && !saw_undeduced
++)
16288 /* Core issue #226 (C++0x) [temp.deduct]:
16290 If a template argument has not been deduced, its
16291 default template argument, if any, is used.
16293 When we are in C++98 mode, TREE_PURPOSE will either
16294 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16295 to explicitly check cxx_dialect here. */
16296 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16298 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16299 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16300 reopen_deferring_access_checks (*checks
);
16301 location_t save_loc
= input_location
;
16303 input_location
= DECL_SOURCE_LOCATION (parm
);
16304 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16305 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16307 input_location
= save_loc
;
16308 *checks
= get_deferred_access_checks ();
16309 pop_deferring_access_checks ();
16310 if (arg
== error_mark_node
)
16314 TREE_VEC_ELT (targs
, i
) = arg
;
16315 /* The position of the first default template argument,
16316 is also the number of non-defaulted arguments in TARGS.
16318 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16324 /* If the type parameter is a parameter pack, then it will
16325 be deduced to an empty parameter pack. */
16326 if (template_parameter_pack_p (tparm
))
16330 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16332 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16333 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16334 TREE_CONSTANT (arg
) = 1;
16337 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16339 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16341 TREE_VEC_ELT (targs
, i
) = arg
;
16345 return unify_parameter_deduction_failure (explain_p
, tparm
);
16348 #ifdef ENABLE_CHECKING
16349 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16350 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16353 return unify_success (explain_p
);
16356 /* Subroutine of type_unification_real. Args are like the variables
16357 at the call site. ARG is an overloaded function (or template-id);
16358 we try deducing template args from each of the overloads, and if
16359 only one succeeds, we go with that. Modifies TARGS and returns
16360 true on success. */
16363 resolve_overloaded_unification (tree tparms
,
16367 unification_kind_t strict
,
16371 tree tempargs
= copy_node (targs
);
16373 tree goodfn
= NULL_TREE
;
16376 if (TREE_CODE (arg
) == ADDR_EXPR
)
16378 arg
= TREE_OPERAND (arg
, 0);
16384 if (TREE_CODE (arg
) == COMPONENT_REF
)
16385 /* Handle `&x' where `x' is some static or non-static member
16387 arg
= TREE_OPERAND (arg
, 1);
16389 if (TREE_CODE (arg
) == OFFSET_REF
)
16390 arg
= TREE_OPERAND (arg
, 1);
16392 /* Strip baselink information. */
16393 if (BASELINK_P (arg
))
16394 arg
= BASELINK_FUNCTIONS (arg
);
16396 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16398 /* If we got some explicit template args, we need to plug them into
16399 the affected templates before we try to unify, in case the
16400 explicit args will completely resolve the templates in question. */
16403 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16404 arg
= TREE_OPERAND (arg
, 0);
16406 for (; arg
; arg
= OVL_NEXT (arg
))
16408 tree fn
= OVL_CURRENT (arg
);
16409 tree subargs
, elem
;
16411 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16414 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16415 expl_subargs
, NULL_TREE
, tf_none
,
16416 /*require_all_args=*/true,
16417 /*use_default_args=*/true);
16418 if (subargs
!= error_mark_node
16419 && !any_dependent_template_arguments_p (subargs
))
16421 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
16422 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16423 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16424 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16433 /* If no templates (or more than one) are fully resolved by the
16434 explicit arguments, this template-id is a non-deduced context; it
16435 could still be OK if we deduce all template arguments for the
16436 enclosing call through other arguments. */
16440 else if (TREE_CODE (arg
) != OVERLOAD
16441 && TREE_CODE (arg
) != FUNCTION_DECL
)
16442 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16443 -- but the deduction does not succeed because the expression is
16444 not just the function on its own. */
16447 for (; arg
; arg
= OVL_NEXT (arg
))
16448 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16449 TREE_TYPE (OVL_CURRENT (arg
)),
16450 strict
, sub_strict
, addr_p
, explain_p
)
16451 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16453 goodfn
= OVL_CURRENT (arg
);
16457 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16458 to function or pointer to member function argument if the set of
16459 overloaded functions does not contain function templates and at most
16460 one of a set of overloaded functions provides a unique match.
16462 So if we found multiple possibilities, we return success but don't
16463 deduce anything. */
16467 int i
= TREE_VEC_LENGTH (targs
);
16469 if (TREE_VEC_ELT (tempargs
, i
))
16470 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16478 /* Core DR 115: In contexts where deduction is done and fails, or in
16479 contexts where deduction is not done, if a template argument list is
16480 specified and it, along with any default template arguments, identifies
16481 a single function template specialization, then the template-id is an
16482 lvalue for the function template specialization. */
16485 resolve_nondeduced_context (tree orig_expr
)
16487 tree expr
, offset
, baselink
;
16490 if (!type_unknown_p (orig_expr
))
16495 offset
= NULL_TREE
;
16496 baselink
= NULL_TREE
;
16498 if (TREE_CODE (expr
) == ADDR_EXPR
)
16500 expr
= TREE_OPERAND (expr
, 0);
16503 if (TREE_CODE (expr
) == OFFSET_REF
)
16506 expr
= TREE_OPERAND (expr
, 1);
16508 if (BASELINK_P (expr
))
16511 expr
= BASELINK_FUNCTIONS (expr
);
16514 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16517 tree goodfn
= NULL_TREE
;
16519 /* If we got some explicit template args, we need to plug them into
16520 the affected templates before we try to unify, in case the
16521 explicit args will completely resolve the templates in question. */
16523 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16524 tree arg
= TREE_OPERAND (expr
, 0);
16525 tree badfn
= NULL_TREE
;
16526 tree badargs
= NULL_TREE
;
16528 for (; arg
; arg
= OVL_NEXT (arg
))
16530 tree fn
= OVL_CURRENT (arg
);
16531 tree subargs
, elem
;
16533 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16536 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16537 expl_subargs
, NULL_TREE
, tf_none
,
16538 /*require_all_args=*/true,
16539 /*use_default_args=*/true);
16540 if (subargs
!= error_mark_node
16541 && !any_dependent_template_arguments_p (subargs
))
16543 elem
= instantiate_template (fn
, subargs
, tf_none
);
16544 if (elem
== error_mark_node
)
16549 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16558 mark_used (goodfn
);
16561 expr
= build_baselink (BASELINK_BINFO (baselink
),
16562 BASELINK_ACCESS_BINFO (baselink
),
16563 expr
, BASELINK_OPTYPE (baselink
));
16567 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16568 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16571 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16574 else if (good
== 0 && badargs
)
16575 /* There were no good options and at least one bad one, so let the
16576 user know what the problem is. */
16577 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16582 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16583 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16584 different overloads deduce different arguments for a given parm.
16585 ADDR_P is true if the expression for which deduction is being
16586 performed was of the form "& fn" rather than simply "fn".
16588 Returns 1 on success. */
16591 try_one_overload (tree tparms
,
16596 unification_kind_t strict
,
16605 if (arg
== error_mark_node
)
16608 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16609 to function or pointer to member function argument if the set of
16610 overloaded functions does not contain function templates and at most
16611 one of a set of overloaded functions provides a unique match.
16613 So if this is a template, just return success. */
16615 if (uses_template_parms (arg
))
16618 if (TREE_CODE (arg
) == METHOD_TYPE
)
16619 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16621 arg
= build_pointer_type (arg
);
16623 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16625 /* We don't copy orig_targs for this because if we have already deduced
16626 some template args from previous args, unify would complain when we
16627 try to deduce a template parameter for the same argument, even though
16628 there isn't really a conflict. */
16629 nargs
= TREE_VEC_LENGTH (targs
);
16630 tempargs
= make_tree_vec (nargs
);
16632 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16635 /* First make sure we didn't deduce anything that conflicts with
16636 explicitly specified args. */
16637 for (i
= nargs
; i
--; )
16639 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16640 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16644 else if (uses_template_parms (elt
))
16645 /* Since we're unifying against ourselves, we will fill in
16646 template args used in the function parm list with our own
16647 template parms. Discard them. */
16648 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16649 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16653 for (i
= nargs
; i
--; )
16655 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16658 TREE_VEC_ELT (targs
, i
) = elt
;
16664 /* PARM is a template class (perhaps with unbound template
16665 parameters). ARG is a fully instantiated type. If ARG can be
16666 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16667 TARGS are as for unify. */
16670 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16673 tree copy_of_targs
;
16675 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16676 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16677 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16680 /* We need to make a new template argument vector for the call to
16681 unify. If we used TARGS, we'd clutter it up with the result of
16682 the attempted unification, even if this class didn't work out.
16683 We also don't want to commit ourselves to all the unifications
16684 we've already done, since unification is supposed to be done on
16685 an argument-by-argument basis. In other words, consider the
16686 following pathological case:
16688 template <int I, int J, int K>
16691 template <int I, int J>
16692 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16694 template <int I, int J, int K>
16695 void f(S<I, J, K>, S<I, I, I>);
16704 Now, by the time we consider the unification involving `s2', we
16705 already know that we must have `f<0, 0, 0>'. But, even though
16706 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16707 because there are two ways to unify base classes of S<0, 1, 2>
16708 with S<I, I, I>. If we kept the already deduced knowledge, we
16709 would reject the possibility I=1. */
16710 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16712 /* If unification failed, we're done. */
16713 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16714 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16720 /* Given a template type PARM and a class type ARG, find the unique
16721 base type in ARG that is an instance of PARM. We do not examine
16722 ARG itself; only its base-classes. If there is not exactly one
16723 appropriate base class, return NULL_TREE. PARM may be the type of
16724 a partial specialization, as well as a plain template type. Used
16727 static enum template_base_result
16728 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16729 bool explain_p
, tree
*result
)
16731 tree rval
= NULL_TREE
;
16734 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16736 binfo
= TYPE_BINFO (complete_type (arg
));
16739 /* The type could not be completed. */
16740 *result
= NULL_TREE
;
16741 return tbr_incomplete_type
;
16744 /* Walk in inheritance graph order. The search order is not
16745 important, and this avoids multiple walks of virtual bases. */
16746 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16748 tree r
= try_class_unification (tparms
, targs
, parm
,
16749 BINFO_TYPE (binfo
), explain_p
);
16753 /* If there is more than one satisfactory baseclass, then:
16757 If they yield more than one possible deduced A, the type
16761 if (rval
&& !same_type_p (r
, rval
))
16763 *result
= NULL_TREE
;
16764 return tbr_ambiguous_baseclass
;
16772 return tbr_success
;
16775 /* Returns the level of DECL, which declares a template parameter. */
16778 template_decl_level (tree decl
)
16780 switch (TREE_CODE (decl
))
16783 case TEMPLATE_DECL
:
16784 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16787 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16790 gcc_unreachable ();
16795 /* Decide whether ARG can be unified with PARM, considering only the
16796 cv-qualifiers of each type, given STRICT as documented for unify.
16797 Returns nonzero iff the unification is OK on that basis. */
16800 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16802 int arg_quals
= cp_type_quals (arg
);
16803 int parm_quals
= cp_type_quals (parm
);
16805 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16806 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16808 /* Although a CVR qualifier is ignored when being applied to a
16809 substituted template parameter ([8.3.2]/1 for example), that
16810 does not allow us to unify "const T" with "int&" because both
16811 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16812 It is ok when we're allowing additional CV qualifiers
16813 at the outer level [14.8.2.1]/3,1st bullet. */
16814 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16815 || TREE_CODE (arg
) == FUNCTION_TYPE
16816 || TREE_CODE (arg
) == METHOD_TYPE
)
16817 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16820 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16821 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16825 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16826 && (arg_quals
& parm_quals
) != parm_quals
)
16829 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16830 && (parm_quals
& arg_quals
) != arg_quals
)
16836 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16838 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16840 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16841 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16842 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16844 *index
= TEMPLATE_TYPE_IDX (parm
);
16845 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16849 *index
= TEMPLATE_PARM_IDX (parm
);
16850 *level
= TEMPLATE_PARM_LEVEL (parm
);
16854 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16856 if (unify (TP, TA, P, A, S, EP)) \
16860 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16861 expansion at the end of PACKED_PARMS. Returns 0 if the type
16862 deduction succeeds, 1 otherwise. STRICT is the same as in
16863 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16864 call argument list. We'll need to adjust the arguments to make them
16865 types. SUBR tells us if this is from a recursive call to
16866 type_unification_real, or for comparing two template argument
16870 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16871 tree packed_args
, unification_kind_t strict
,
16872 bool subr
, bool explain_p
)
16875 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16876 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16877 tree pack
, packs
= NULL_TREE
;
16878 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16879 int len
= TREE_VEC_LENGTH (packed_args
);
16881 /* Determine the parameter packs we will be deducing from the
16882 pattern, and record their current deductions. */
16883 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16884 pack
; pack
= TREE_CHAIN (pack
))
16886 tree parm_pack
= TREE_VALUE (pack
);
16889 /* Determine the index and level of this parameter pack. */
16890 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16892 /* Keep track of the parameter packs and their corresponding
16894 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16895 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16898 /* Loop through all of the arguments that have not yet been
16899 unified and unify each with the pattern. */
16900 for (i
= start
; i
< len
; i
++)
16903 bool any_explicit
= false;
16904 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16906 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16907 or the element of its argument pack at the current index if
16908 this argument was explicitly specified. */
16909 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16913 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16916 if (TREE_VALUE (pack
)
16917 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16918 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16920 any_explicit
= true;
16921 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16923 TMPL_ARG (targs
, level
, idx
) = arg
;
16926 /* If we had explicit template arguments, substitute them into the
16927 pattern before deduction. */
16930 /* Some arguments might still be unspecified or dependent. */
16932 ++processing_template_decl
;
16933 dependent
= any_dependent_template_arguments_p (targs
);
16935 --processing_template_decl
;
16936 parm
= tsubst (pattern
, targs
,
16937 explain_p
? tf_warning_or_error
: tf_none
,
16940 --processing_template_decl
;
16941 if (parm
== error_mark_node
)
16947 /* Unify the pattern with the current argument. */
16948 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16949 LOOKUP_IMPLICIT
, explain_p
))
16952 /* For each parameter pack, collect the deduced value. */
16953 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16956 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16958 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16959 TMPL_ARG (targs
, level
, idx
);
16963 /* Verify that the results of unification with the parameter packs
16964 produce results consistent with what we've seen before, and make
16965 the deduced argument packs available. */
16966 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16968 tree old_pack
= TREE_VALUE (pack
);
16969 tree new_args
= TREE_TYPE (pack
);
16970 int i
, len
= TREE_VEC_LENGTH (new_args
);
16972 bool nondeduced_p
= false;
16974 /* By default keep the original deduced argument pack.
16975 If necessary, more specific code is going to update the
16976 resulting deduced argument later down in this function. */
16977 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16978 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16980 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16981 actually deduce anything. */
16982 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16983 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16984 nondeduced_p
= true;
16988 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16990 /* If we had fewer function args than explicit template args,
16991 just use the explicits. */
16992 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16993 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16994 if (len
< explicit_len
)
16995 new_args
= explicit_args
;
17001 /* Build the deduced *_ARGUMENT_PACK. */
17002 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17004 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17005 TREE_TYPE (result
) =
17006 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17007 TREE_CONSTANT (result
) = 1;
17010 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17012 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17014 /* Note the deduced argument packs for this parameter
17016 TMPL_ARG (targs
, level
, idx
) = result
;
17018 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17019 && (ARGUMENT_PACK_ARGS (old_pack
)
17020 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17022 /* We only had the explicitly-provided arguments before, but
17023 now we have a complete set of arguments. */
17024 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17026 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17027 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17028 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17032 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17033 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17035 if (!comp_template_args_with_info (old_args
, new_args
,
17036 &bad_old_arg
, &bad_new_arg
))
17037 /* Inconsistent unification of this parameter pack. */
17038 return unify_parameter_pack_inconsistent (explain_p
,
17044 return unify_success (explain_p
);
17047 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17048 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17049 parameters and return value are as for unify. */
17052 unify_array_domain (tree tparms
, tree targs
,
17053 tree parm_dom
, tree arg_dom
,
17061 /* Our representation of array types uses "N - 1" as the
17062 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17063 not an integer constant. We cannot unify arbitrarily
17064 complex expressions, so we eliminate the MINUS_EXPRs
17066 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17067 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17070 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17071 parm_max
= TREE_OPERAND (parm_max
, 0);
17073 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17074 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17077 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17078 trying to unify the type of a variable with the type
17079 of a template parameter. For example:
17081 template <unsigned int N>
17082 void f (char (&) [N]);
17089 Here, the type of the ARG will be "int [g(i)]", and
17090 may be a SAVE_EXPR, etc. */
17091 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17092 return unify_vla_arg (explain_p
, arg_dom
);
17093 arg_max
= TREE_OPERAND (arg_max
, 0);
17096 /* If only one of the bounds used a MINUS_EXPR, compensate
17097 by adding one to the other bound. */
17098 if (parm_cst
&& !arg_cst
)
17099 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17103 else if (arg_cst
&& !parm_cst
)
17104 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17109 return unify (tparms
, targs
, parm_max
, arg_max
,
17110 UNIFY_ALLOW_INTEGER
, explain_p
);
17113 /* Deduce the value of template parameters. TPARMS is the (innermost)
17114 set of template parameters to a template. TARGS is the bindings
17115 for those template parameters, as determined thus far; TARGS may
17116 include template arguments for outer levels of template parameters
17117 as well. PARM is a parameter to a template function, or a
17118 subcomponent of that parameter; ARG is the corresponding argument.
17119 This function attempts to match PARM with ARG in a manner
17120 consistent with the existing assignments in TARGS. If more values
17121 are deduced, then TARGS is updated.
17123 Returns 0 if the type deduction succeeds, 1 otherwise. The
17124 parameter STRICT is a bitwise or of the following flags:
17127 Require an exact match between PARM and ARG.
17128 UNIFY_ALLOW_MORE_CV_QUAL:
17129 Allow the deduced ARG to be more cv-qualified (by qualification
17130 conversion) than ARG.
17131 UNIFY_ALLOW_LESS_CV_QUAL:
17132 Allow the deduced ARG to be less cv-qualified than ARG.
17133 UNIFY_ALLOW_DERIVED:
17134 Allow the deduced ARG to be a template base class of ARG,
17135 or a pointer to a template base class of the type pointed to by
17137 UNIFY_ALLOW_INTEGER:
17138 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17139 case for more information.
17140 UNIFY_ALLOW_OUTER_LEVEL:
17141 This is the outermost level of a deduction. Used to determine validity
17142 of qualification conversions. A valid qualification conversion must
17143 have const qualified pointers leading up to the inner type which
17144 requires additional CV quals, except at the outer level, where const
17145 is not required [conv.qual]. It would be normal to set this flag in
17146 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17147 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17148 This is the outermost level of a deduction, and PARM can be more CV
17149 qualified at this point.
17150 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17151 This is the outermost level of a deduction, and PARM can be less CV
17152 qualified at this point. */
17155 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17161 int strict_in
= strict
;
17163 /* I don't think this will do the right thing with respect to types.
17164 But the only case I've seen it in so far has been array bounds, where
17165 signedness is the only information lost, and I think that will be
17167 while (TREE_CODE (parm
) == NOP_EXPR
)
17168 parm
= TREE_OPERAND (parm
, 0);
17170 if (arg
== error_mark_node
)
17171 return unify_invalid (explain_p
);
17172 if (arg
== unknown_type_node
17173 || arg
== init_list_type_node
)
17174 /* We can't deduce anything from this, but we might get all the
17175 template args from other function args. */
17176 return unify_success (explain_p
);
17178 /* If PARM uses template parameters, then we can't bail out here,
17179 even if ARG == PARM, since we won't record unifications for the
17180 template parameters. We might need them if we're trying to
17181 figure out which of two things is more specialized. */
17182 if (arg
== parm
&& !uses_template_parms (parm
))
17183 return unify_success (explain_p
);
17185 /* Handle init lists early, so the rest of the function can assume
17186 we're dealing with a type. */
17187 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17191 tree orig_parm
= parm
;
17193 /* Replace T with std::initializer_list<T> for deduction. */
17194 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17195 && flag_deduce_init_list
)
17196 parm
= listify (parm
);
17198 if (!is_std_init_list (parm
)
17199 && TREE_CODE (parm
) != ARRAY_TYPE
)
17200 /* We can only deduce from an initializer list argument if the
17201 parameter is std::initializer_list or an array; otherwise this
17202 is a non-deduced context. */
17203 return unify_success (explain_p
);
17205 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17206 elttype
= TREE_TYPE (parm
);
17208 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17210 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17212 int elt_strict
= strict
;
17214 if (elt
== error_mark_node
)
17215 return unify_invalid (explain_p
);
17217 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17219 tree type
= TREE_TYPE (elt
);
17220 /* It should only be possible to get here for a call. */
17221 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17222 elt_strict
|= maybe_adjust_types_for_deduction
17223 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17227 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17231 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17233 /* Also deduce from the length of the initializer list. */
17234 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17235 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17236 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17237 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17241 /* If the std::initializer_list<T> deduction worked, replace the
17242 deduced A with std::initializer_list<A>. */
17243 if (orig_parm
!= parm
)
17245 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17246 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17247 targ
= listify (targ
);
17248 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17250 return unify_success (explain_p
);
17253 /* Immediately reject some pairs that won't unify because of
17254 cv-qualification mismatches. */
17255 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17257 /* It is the elements of the array which hold the cv quals of an array
17258 type, and the elements might be template type parms. We'll check
17259 when we recurse. */
17260 && TREE_CODE (arg
) != ARRAY_TYPE
17261 /* We check the cv-qualifiers when unifying with template type
17262 parameters below. We want to allow ARG `const T' to unify with
17263 PARM `T' for example, when computing which of two templates
17264 is more specialized, for example. */
17265 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17266 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17267 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17269 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17270 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17271 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17272 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17273 strict
&= ~UNIFY_ALLOW_DERIVED
;
17274 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17275 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17277 switch (TREE_CODE (parm
))
17279 case TYPENAME_TYPE
:
17281 case UNBOUND_CLASS_TEMPLATE
:
17282 /* In a type which contains a nested-name-specifier, template
17283 argument values cannot be deduced for template parameters used
17284 within the nested-name-specifier. */
17285 return unify_success (explain_p
);
17287 case TEMPLATE_TYPE_PARM
:
17288 case TEMPLATE_TEMPLATE_PARM
:
17289 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17290 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17291 if (tparm
== error_mark_node
)
17292 return unify_invalid (explain_p
);
17294 if (TEMPLATE_TYPE_LEVEL (parm
)
17295 != template_decl_level (tparm
))
17296 /* The PARM is not one we're trying to unify. Just check
17297 to see if it matches ARG. */
17299 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17300 && (is_auto (parm
) ? is_auto (arg
)
17301 : same_type_p (parm
, arg
)))
17302 return unify_success (explain_p
);
17304 return unify_type_mismatch (explain_p
, parm
, arg
);
17306 idx
= TEMPLATE_TYPE_IDX (parm
);
17307 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17308 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17309 if (tparm
== error_mark_node
)
17310 return unify_invalid (explain_p
);
17312 /* Check for mixed types and values. */
17313 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17314 && TREE_CODE (tparm
) != TYPE_DECL
)
17315 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17316 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17317 gcc_unreachable ();
17319 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17321 /* ARG must be constructed from a template class or a template
17322 template parameter. */
17323 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17324 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17325 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17328 tree parmvec
= TYPE_TI_ARGS (parm
);
17329 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17330 tree full_argvec
= add_to_template_args (targs
, argvec
);
17332 = DECL_INNERMOST_TEMPLATE_PARMS
17333 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17335 int parm_variadic_p
= 0;
17337 /* The resolution to DR150 makes clear that default
17338 arguments for an N-argument may not be used to bind T
17339 to a template template parameter with fewer than N
17340 parameters. It is not safe to permit the binding of
17341 default arguments as an extension, as that may change
17342 the meaning of a conforming program. Consider:
17344 struct Dense { static const unsigned int dim = 1; };
17346 template <template <typename> class View,
17348 void operator+(float, View<Block> const&);
17350 template <typename Block,
17351 unsigned int Dim = Block::dim>
17352 struct Lvalue_proxy { operator float() const; };
17356 Lvalue_proxy<Dense> p;
17361 Here, if Lvalue_proxy is permitted to bind to View, then
17362 the global operator+ will be used; if they are not, the
17363 Lvalue_proxy will be converted to float. */
17364 if (coerce_template_parms (parm_parms
,
17366 TYPE_TI_TEMPLATE (parm
),
17368 ? tf_warning_or_error
17370 /*require_all_args=*/true,
17371 /*use_default_args=*/false)
17372 == error_mark_node
)
17375 /* Deduce arguments T, i from TT<T> or TT<i>.
17376 We check each element of PARMVEC and ARGVEC individually
17377 rather than the whole TREE_VEC since they can have
17378 different number of elements. */
17380 parmvec
= expand_template_argument_pack (parmvec
);
17381 argvec
= expand_template_argument_pack (argvec
);
17383 len
= TREE_VEC_LENGTH (parmvec
);
17385 /* Check if the parameters end in a pack, making them
17388 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17389 parm_variadic_p
= 1;
17391 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17392 /* If the template argument list of P contains a pack
17393 expansion that is not the last template argument, the
17394 entire template argument list is a non-deduced
17396 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17397 return unify_success (explain_p
);
17399 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17400 return unify_too_few_arguments (explain_p
,
17401 TREE_VEC_LENGTH (argvec
), len
);
17403 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17405 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17406 TREE_VEC_ELT (parmvec
, i
),
17407 TREE_VEC_ELT (argvec
, i
),
17408 UNIFY_ALLOW_NONE
, explain_p
);
17411 if (parm_variadic_p
17412 && unify_pack_expansion (tparms
, targs
,
17415 /*subr=*/true, explain_p
))
17418 arg
= TYPE_TI_TEMPLATE (arg
);
17420 /* Fall through to deduce template name. */
17423 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17424 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17426 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17428 /* Simple cases: Value already set, does match or doesn't. */
17429 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17430 return unify_success (explain_p
);
17432 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17436 /* If PARM is `const T' and ARG is only `int', we don't have
17437 a match unless we are allowing additional qualification.
17438 If ARG is `const int' and PARM is just `T' that's OK;
17439 that binds `const int' to `T'. */
17440 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17442 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17444 /* Consider the case where ARG is `const volatile int' and
17445 PARM is `const T'. Then, T should be `volatile int'. */
17446 arg
= cp_build_qualified_type_real
17447 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17448 if (arg
== error_mark_node
)
17449 return unify_invalid (explain_p
);
17451 /* Simple cases: Value already set, does match or doesn't. */
17452 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17453 return unify_success (explain_p
);
17455 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17457 /* Make sure that ARG is not a variable-sized array. (Note
17458 that were talking about variable-sized arrays (like
17459 `int[n]'), rather than arrays of unknown size (like
17460 `int[]').) We'll get very confused by such a type since
17461 the bound of the array is not constant, and therefore
17462 not mangleable. Besides, such types are not allowed in
17463 ISO C++, so we can do as we please here. We do allow
17464 them for 'auto' deduction, since that isn't ABI-exposed. */
17465 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17466 return unify_vla_arg (explain_p
, arg
);
17468 /* Strip typedefs as in convert_template_argument. */
17469 arg
= canonicalize_type_argument (arg
, tf_none
);
17472 /* If ARG is a parameter pack or an expansion, we cannot unify
17473 against it unless PARM is also a parameter pack. */
17474 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17475 && !template_parameter_pack_p (parm
))
17476 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17478 /* If the argument deduction results is a METHOD_TYPE,
17479 then there is a problem.
17480 METHOD_TYPE doesn't map to any real C++ type the result of
17481 the deduction can not be of that type. */
17482 if (TREE_CODE (arg
) == METHOD_TYPE
)
17483 return unify_method_type_error (explain_p
, arg
);
17485 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17486 return unify_success (explain_p
);
17488 case TEMPLATE_PARM_INDEX
:
17489 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17490 if (tparm
== error_mark_node
)
17491 return unify_invalid (explain_p
);
17493 if (TEMPLATE_PARM_LEVEL (parm
)
17494 != template_decl_level (tparm
))
17496 /* The PARM is not one we're trying to unify. Just check
17497 to see if it matches ARG. */
17498 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17499 && cp_tree_equal (parm
, arg
));
17501 unify_expression_unequal (explain_p
, parm
, arg
);
17505 idx
= TEMPLATE_PARM_IDX (parm
);
17506 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17510 int x
= !cp_tree_equal (targ
, arg
);
17512 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17516 /* [temp.deduct.type] If, in the declaration of a function template
17517 with a non-type template-parameter, the non-type
17518 template-parameter is used in an expression in the function
17519 parameter-list and, if the corresponding template-argument is
17520 deduced, the template-argument type shall match the type of the
17521 template-parameter exactly, except that a template-argument
17522 deduced from an array bound may be of any integral type.
17523 The non-type parameter might use already deduced type parameters. */
17524 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17525 if (!TREE_TYPE (arg
))
17526 /* Template-parameter dependent expression. Just accept it for now.
17527 It will later be processed in convert_template_argument. */
17529 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17531 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17532 && CP_INTEGRAL_TYPE_P (tparm
))
17533 /* Convert the ARG to the type of PARM; the deduced non-type
17534 template argument must exactly match the types of the
17535 corresponding parameter. */
17536 arg
= fold (build_nop (tparm
, arg
));
17537 else if (uses_template_parms (tparm
))
17538 /* We haven't deduced the type of this parameter yet. Try again
17540 return unify_success (explain_p
);
17542 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17544 /* If ARG is a parameter pack or an expansion, we cannot unify
17545 against it unless PARM is also a parameter pack. */
17546 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17547 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17548 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17550 arg
= strip_typedefs_expr (arg
);
17551 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17552 return unify_success (explain_p
);
17556 /* A pointer-to-member constant can be unified only with
17557 another constant. */
17558 if (TREE_CODE (arg
) != PTRMEM_CST
)
17559 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17561 /* Just unify the class member. It would be useless (and possibly
17562 wrong, depending on the strict flags) to unify also
17563 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17564 arg refer to the same variable, even if through different
17565 classes. For instance:
17567 struct A { int x; };
17570 Unification of &A::x and &B::x must succeed. */
17571 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17572 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17577 if (!TYPE_PTR_P (arg
))
17578 return unify_type_mismatch (explain_p
, parm
, arg
);
17580 /* [temp.deduct.call]
17582 A can be another pointer or pointer to member type that can
17583 be converted to the deduced A via a qualification
17584 conversion (_conv.qual_).
17586 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17587 This will allow for additional cv-qualification of the
17588 pointed-to types if appropriate. */
17590 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17591 /* The derived-to-base conversion only persists through one
17592 level of pointers. */
17593 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17595 return unify (tparms
, targs
, TREE_TYPE (parm
),
17596 TREE_TYPE (arg
), strict
, explain_p
);
17599 case REFERENCE_TYPE
:
17600 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17601 return unify_type_mismatch (explain_p
, parm
, arg
);
17602 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17603 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17606 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17607 return unify_type_mismatch (explain_p
, parm
, arg
);
17608 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17609 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17610 return unify_type_mismatch (explain_p
, parm
, arg
);
17611 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17612 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17613 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17614 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17615 TYPE_DOMAIN (arg
), explain_p
);
17616 return unify_success (explain_p
);
17623 case ENUMERAL_TYPE
:
17626 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17627 return unify_type_mismatch (explain_p
, parm
, arg
);
17629 /* We have already checked cv-qualification at the top of the
17631 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17632 return unify_type_mismatch (explain_p
, parm
, arg
);
17634 /* As far as unification is concerned, this wins. Later checks
17635 will invalidate it if necessary. */
17636 return unify_success (explain_p
);
17638 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17639 /* Type INTEGER_CST can come from ordinary constant template args. */
17641 while (TREE_CODE (arg
) == NOP_EXPR
)
17642 arg
= TREE_OPERAND (arg
, 0);
17644 if (TREE_CODE (arg
) != INTEGER_CST
)
17645 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17646 return (tree_int_cst_equal (parm
, arg
)
17647 ? unify_success (explain_p
)
17648 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17652 int i
, len
, argslen
;
17653 int parm_variadic_p
= 0;
17655 if (TREE_CODE (arg
) != TREE_VEC
)
17656 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17658 len
= TREE_VEC_LENGTH (parm
);
17659 argslen
= TREE_VEC_LENGTH (arg
);
17661 /* Check for pack expansions in the parameters. */
17662 for (i
= 0; i
< len
; ++i
)
17664 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17667 /* We can unify against something with a trailing
17669 parm_variadic_p
= 1;
17671 /* [temp.deduct.type]/9: If the template argument list of
17672 P contains a pack expansion that is not the last
17673 template argument, the entire template argument list
17674 is a non-deduced context. */
17675 return unify_success (explain_p
);
17679 /* If we don't have enough arguments to satisfy the parameters
17680 (not counting the pack expression at the end), or we have
17681 too many arguments for a parameter list that doesn't end in
17682 a pack expression, we can't unify. */
17683 if (parm_variadic_p
17684 ? argslen
< len
- parm_variadic_p
17686 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17688 /* Unify all of the parameters that precede the (optional)
17689 pack expression. */
17690 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17692 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17693 TREE_VEC_ELT (parm
, i
),
17694 TREE_VEC_ELT (arg
, i
),
17695 UNIFY_ALLOW_NONE
, explain_p
);
17697 if (parm_variadic_p
)
17698 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17700 /*subr=*/true, explain_p
);
17701 return unify_success (explain_p
);
17706 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17707 return unify_type_mismatch (explain_p
, parm
, arg
);
17709 if (TYPE_PTRMEMFUNC_P (parm
))
17711 if (!TYPE_PTRMEMFUNC_P (arg
))
17712 return unify_type_mismatch (explain_p
, parm
, arg
);
17714 return unify (tparms
, targs
,
17715 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17716 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17717 strict
, explain_p
);
17720 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17722 tree t
= NULL_TREE
;
17724 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17726 /* First, we try to unify the PARM and ARG directly. */
17727 t
= try_class_unification (tparms
, targs
,
17728 parm
, arg
, explain_p
);
17732 /* Fallback to the special case allowed in
17733 [temp.deduct.call]:
17735 If P is a class, and P has the form
17736 template-id, then A can be a derived class of
17737 the deduced A. Likewise, if P is a pointer to
17738 a class of the form template-id, A can be a
17739 pointer to a derived class pointed to by the
17741 enum template_base_result r
;
17742 r
= get_template_base (tparms
, targs
, parm
, arg
,
17746 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17749 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17750 && (CLASSTYPE_TI_TEMPLATE (parm
)
17751 == CLASSTYPE_TI_TEMPLATE (arg
)))
17752 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17753 Then, we should unify `int' and `U'. */
17756 /* There's no chance of unification succeeding. */
17757 return unify_type_mismatch (explain_p
, parm
, arg
);
17759 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17760 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17762 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17763 return unify_type_mismatch (explain_p
, parm
, arg
);
17764 return unify_success (explain_p
);
17767 case FUNCTION_TYPE
:
17769 unsigned int nargs
;
17774 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17775 return unify_type_mismatch (explain_p
, parm
, arg
);
17777 /* CV qualifications for methods can never be deduced, they must
17778 match exactly. We need to check them explicitly here,
17779 because type_unification_real treats them as any other
17780 cv-qualified parameter. */
17781 if (TREE_CODE (parm
) == METHOD_TYPE
17782 && (!check_cv_quals_for_unify
17784 class_of_this_parm (arg
),
17785 class_of_this_parm (parm
))))
17786 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17788 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17789 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17791 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17792 args
= XALLOCAVEC (tree
, nargs
);
17793 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17794 a
!= NULL_TREE
&& a
!= void_list_node
;
17795 a
= TREE_CHAIN (a
), ++i
)
17796 args
[i
] = TREE_VALUE (a
);
17799 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17800 args
, nargs
, 1, DEDUCE_EXACT
,
17801 LOOKUP_NORMAL
, NULL
, explain_p
);
17805 /* Unify a pointer to member with a pointer to member function, which
17806 deduces the type of the member as a function type. */
17807 if (TYPE_PTRMEMFUNC_P (arg
))
17809 /* Check top-level cv qualifiers */
17810 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17811 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17813 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17814 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17815 UNIFY_ALLOW_NONE
, explain_p
);
17817 /* Determine the type of the function we are unifying against. */
17818 tree fntype
= static_fn_type (arg
);
17820 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17823 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17824 return unify_type_mismatch (explain_p
, parm
, arg
);
17825 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17826 TYPE_OFFSET_BASETYPE (arg
),
17827 UNIFY_ALLOW_NONE
, explain_p
);
17828 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17829 strict
, explain_p
);
17832 if (DECL_TEMPLATE_PARM_P (parm
))
17833 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17834 if (arg
!= integral_constant_value (parm
))
17835 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17836 return unify_success (explain_p
);
17839 case TEMPLATE_DECL
:
17840 /* Matched cases are handled by the ARG == PARM test above. */
17841 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17844 /* A non-type template parameter that is a variable should be a
17845 an integral constant, in which case, it whould have been
17846 folded into its (constant) value. So we should not be getting
17847 a variable here. */
17848 gcc_unreachable ();
17850 case TYPE_ARGUMENT_PACK
:
17851 case NONTYPE_ARGUMENT_PACK
:
17852 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17853 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17856 case DECLTYPE_TYPE
:
17857 case UNDERLYING_TYPE
:
17858 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17859 or UNDERLYING_TYPE nodes. */
17860 return unify_success (explain_p
);
17863 /* Unification fails if we hit an error node. */
17864 return unify_invalid (explain_p
);
17867 /* An unresolved overload is a nondeduced context. */
17868 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17869 return unify_success (explain_p
);
17870 gcc_assert (EXPR_P (parm
));
17872 /* We must be looking at an expression. This can happen with
17876 void foo(S<I>, S<I + 2>);
17878 This is a "nondeduced context":
17882 The nondeduced contexts are:
17884 --A type that is a template-id in which one or more of
17885 the template-arguments is an expression that references
17886 a template-parameter.
17888 In these cases, we assume deduction succeeded, but don't
17889 actually infer any unifications. */
17891 if (!uses_template_parms (parm
)
17892 && !template_args_equal (parm
, arg
))
17893 return unify_expression_unequal (explain_p
, parm
, arg
);
17895 return unify_success (explain_p
);
17898 #undef RECUR_AND_CHECK_FAILURE
17900 /* Note that DECL can be defined in this translation unit, if
17904 mark_definable (tree decl
)
17907 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17908 FOR_EACH_CLONE (clone
, decl
)
17909 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17912 /* Called if RESULT is explicitly instantiated, or is a member of an
17913 explicitly instantiated class. */
17916 mark_decl_instantiated (tree result
, int extern_p
)
17918 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17920 /* If this entity has already been written out, it's too late to
17921 make any modifications. */
17922 if (TREE_ASM_WRITTEN (result
))
17925 /* For anonymous namespace we don't need to do anything. */
17926 if (decl_anon_ns_mem_p (result
))
17928 gcc_assert (!TREE_PUBLIC (result
));
17932 if (TREE_CODE (result
) != FUNCTION_DECL
)
17933 /* The TREE_PUBLIC flag for function declarations will have been
17934 set correctly by tsubst. */
17935 TREE_PUBLIC (result
) = 1;
17937 /* This might have been set by an earlier implicit instantiation. */
17938 DECL_COMDAT (result
) = 0;
17941 DECL_NOT_REALLY_EXTERN (result
) = 0;
17944 mark_definable (result
);
17945 /* Always make artificials weak. */
17946 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17947 comdat_linkage (result
);
17948 /* For WIN32 we also want to put explicit instantiations in
17949 linkonce sections. */
17950 else if (TREE_PUBLIC (result
))
17951 maybe_make_one_only (result
);
17954 /* If EXTERN_P, then this function will not be emitted -- unless
17955 followed by an explicit instantiation, at which point its linkage
17956 will be adjusted. If !EXTERN_P, then this function will be
17957 emitted here. In neither circumstance do we want
17958 import_export_decl to adjust the linkage. */
17959 DECL_INTERFACE_KNOWN (result
) = 1;
17962 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17963 important template arguments. If any are missing, we check whether
17964 they're important by using error_mark_node for substituting into any
17965 args that were used for partial ordering (the ones between ARGS and END)
17966 and seeing if it bubbles up. */
17969 check_undeduced_parms (tree targs
, tree args
, tree end
)
17971 bool found
= false;
17973 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17974 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17977 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17981 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17982 if (substed
== error_mark_node
)
17988 /* Given two function templates PAT1 and PAT2, return:
17990 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17991 -1 if PAT2 is more specialized than PAT1.
17992 0 if neither is more specialized.
17994 LEN indicates the number of parameters we should consider
17995 (defaulted parameters should not be considered).
17997 The 1998 std underspecified function template partial ordering, and
17998 DR214 addresses the issue. We take pairs of arguments, one from
17999 each of the templates, and deduce them against each other. One of
18000 the templates will be more specialized if all the *other*
18001 template's arguments deduce against its arguments and at least one
18002 of its arguments *does* *not* deduce against the other template's
18003 corresponding argument. Deduction is done as for class templates.
18004 The arguments used in deduction have reference and top level cv
18005 qualifiers removed. Iff both arguments were originally reference
18006 types *and* deduction succeeds in both directions, an lvalue reference
18007 wins against an rvalue reference and otherwise the template
18008 with the more cv-qualified argument wins for that pairing (if
18009 neither is more cv-qualified, they both are equal). Unlike regular
18010 deduction, after all the arguments have been deduced in this way,
18011 we do *not* verify the deduced template argument values can be
18012 substituted into non-deduced contexts.
18014 The logic can be a bit confusing here, because we look at deduce1 and
18015 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18016 can find template arguments for pat1 to make arg1 look like arg2, that
18017 means that arg2 is at least as specialized as arg1. */
18020 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18022 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18023 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18024 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18025 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18026 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18027 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18028 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18029 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18030 tree origs1
, origs2
;
18031 bool lose1
= false;
18032 bool lose2
= false;
18034 /* Remove the this parameter from non-static member functions. If
18035 one is a non-static member function and the other is not a static
18036 member function, remove the first parameter from that function
18037 also. This situation occurs for operator functions where we
18038 locate both a member function (with this pointer) and non-member
18039 operator (with explicit first operand). */
18040 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18042 len
--; /* LEN is the number of significant arguments for DECL1 */
18043 args1
= TREE_CHAIN (args1
);
18044 if (!DECL_STATIC_FUNCTION_P (decl2
))
18045 args2
= TREE_CHAIN (args2
);
18047 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18049 args2
= TREE_CHAIN (args2
);
18050 if (!DECL_STATIC_FUNCTION_P (decl1
))
18053 args1
= TREE_CHAIN (args1
);
18057 /* If only one is a conversion operator, they are unordered. */
18058 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18061 /* Consider the return type for a conversion function */
18062 if (DECL_CONV_FN_P (decl1
))
18064 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18065 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18069 processing_template_decl
++;
18075 /* Stop when an ellipsis is seen. */
18076 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18078 tree arg1
= TREE_VALUE (args1
);
18079 tree arg2
= TREE_VALUE (args2
);
18080 int deduce1
, deduce2
;
18086 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18087 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18089 /* When both arguments are pack expansions, we need only
18090 unify the patterns themselves. */
18091 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18092 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18094 /* This is the last comparison we need to do. */
18098 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18100 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18101 arg1
= TREE_TYPE (arg1
);
18102 quals1
= cp_type_quals (arg1
);
18105 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18107 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18108 arg2
= TREE_TYPE (arg2
);
18109 quals2
= cp_type_quals (arg2
);
18112 arg1
= TYPE_MAIN_VARIANT (arg1
);
18113 arg2
= TYPE_MAIN_VARIANT (arg2
);
18115 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18117 int i
, len2
= list_length (args2
);
18118 tree parmvec
= make_tree_vec (1);
18119 tree argvec
= make_tree_vec (len2
);
18122 /* Setup the parameter vector, which contains only ARG1. */
18123 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18125 /* Setup the argument vector, which contains the remaining
18127 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18128 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18130 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18131 argvec
, DEDUCE_EXACT
,
18132 /*subr=*/true, /*explain_p=*/false)
18135 /* We cannot deduce in the other direction, because ARG1 is
18136 a pack expansion but ARG2 is not. */
18139 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18141 int i
, len1
= list_length (args1
);
18142 tree parmvec
= make_tree_vec (1);
18143 tree argvec
= make_tree_vec (len1
);
18146 /* Setup the parameter vector, which contains only ARG1. */
18147 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18149 /* Setup the argument vector, which contains the remaining
18151 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18152 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18154 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18155 argvec
, DEDUCE_EXACT
,
18156 /*subr=*/true, /*explain_p=*/false)
18159 /* We cannot deduce in the other direction, because ARG2 is
18160 a pack expansion but ARG1 is not.*/
18166 /* The normal case, where neither argument is a pack
18168 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18169 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18171 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18172 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18176 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18177 arg2, then arg2 is not as specialized as arg1. */
18183 /* "If, for a given type, deduction succeeds in both directions
18184 (i.e., the types are identical after the transformations above)
18185 and both P and A were reference types (before being replaced with
18186 the type referred to above):
18187 - if the type from the argument template was an lvalue reference and
18188 the type from the parameter template was not, the argument type is
18189 considered to be more specialized than the other; otherwise,
18190 - if the type from the argument template is more cv-qualified
18191 than the type from the parameter template (as described above),
18192 the argument type is considered to be more specialized than the other;
18194 - neither type is more specialized than the other." */
18196 if (deduce1
&& deduce2
)
18198 if (ref1
&& ref2
&& ref1
!= ref2
)
18205 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18207 if ((quals1
& quals2
) == quals2
)
18209 if ((quals1
& quals2
) == quals1
)
18214 if (lose1
&& lose2
)
18215 /* We've failed to deduce something in either direction.
18216 These must be unordered. */
18219 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18220 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18221 /* We have already processed all of the arguments in our
18222 handing of the pack expansion type. */
18225 args1
= TREE_CHAIN (args1
);
18226 args2
= TREE_CHAIN (args2
);
18229 /* "In most cases, all template parameters must have values in order for
18230 deduction to succeed, but for partial ordering purposes a template
18231 parameter may remain without a value provided it is not used in the
18232 types being used for partial ordering."
18234 Thus, if we are missing any of the targs1 we need to substitute into
18235 origs1, then pat2 is not as specialized as pat1. This can happen when
18236 there is a nondeduced context. */
18237 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18239 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18242 processing_template_decl
--;
18244 /* All things being equal, if the next argument is a pack expansion
18245 for one function but not for the other, prefer the
18246 non-variadic function. FIXME this is bogus; see c++/41958. */
18248 && args1
&& TREE_VALUE (args1
)
18249 && args2
&& TREE_VALUE (args2
))
18251 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18252 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18255 if (lose1
== lose2
)
18263 /* Determine which of two partial specializations of TMPL is more
18266 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18267 to the first partial specialization. The TREE_VALUE is the
18268 innermost set of template parameters for the partial
18269 specialization. PAT2 is similar, but for the second template.
18271 Return 1 if the first partial specialization is more specialized;
18272 -1 if the second is more specialized; 0 if neither is more
18275 See [temp.class.order] for information about determining which of
18276 two templates is more specialized. */
18279 more_specialized_class (tree tmpl
, tree pat1
, tree pat2
)
18284 bool any_deductions
= false;
18286 tmpl1
= TREE_TYPE (pat1
);
18287 tmpl2
= TREE_TYPE (pat2
);
18289 /* Just like what happens for functions, if we are ordering between
18290 different class template specializations, we may encounter dependent
18291 types in the arguments, and we need our dependency check functions
18292 to behave correctly. */
18293 ++processing_template_decl
;
18294 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat1
),
18295 CLASSTYPE_TI_ARGS (tmpl1
),
18296 CLASSTYPE_TI_ARGS (tmpl2
));
18300 any_deductions
= true;
18303 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat2
),
18304 CLASSTYPE_TI_ARGS (tmpl2
),
18305 CLASSTYPE_TI_ARGS (tmpl1
));
18309 any_deductions
= true;
18311 --processing_template_decl
;
18313 /* In the case of a tie where at least one of the class templates
18314 has a parameter pack at the end, the template with the most
18315 non-packed parameters wins. */
18318 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18319 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18321 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18322 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18323 int len1
= TREE_VEC_LENGTH (args1
);
18324 int len2
= TREE_VEC_LENGTH (args2
);
18326 /* We don't count the pack expansion at the end. */
18327 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18329 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18334 else if (len1
< len2
)
18341 /* Return the template arguments that will produce the function signature
18342 DECL from the function template FN, with the explicit template
18343 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18344 also match. Return NULL_TREE if no satisfactory arguments could be
18348 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18350 int ntparms
= DECL_NTPARMS (fn
);
18351 tree targs
= make_tree_vec (ntparms
);
18352 tree decl_type
= TREE_TYPE (decl
);
18353 tree decl_arg_types
;
18355 unsigned int nargs
, ix
;
18358 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18360 /* Never do unification on the 'this' parameter. */
18361 decl_arg_types
= skip_artificial_parms_for (decl
,
18362 TYPE_ARG_TYPES (decl_type
));
18364 nargs
= list_length (decl_arg_types
);
18365 args
= XALLOCAVEC (tree
, nargs
);
18366 for (arg
= decl_arg_types
, ix
= 0;
18367 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18368 arg
= TREE_CHAIN (arg
), ++ix
)
18369 args
[ix
] = TREE_VALUE (arg
);
18371 if (fn_type_unification (fn
, explicit_args
, targs
,
18373 (check_rettype
|| DECL_CONV_FN_P (fn
)
18374 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18375 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18377 == error_mark_node
)
18383 /* Return the innermost template arguments that, when applied to a partial
18384 specialization of TMPL whose innermost template parameters are
18385 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18388 For example, suppose we have:
18390 template <class T, class U> struct S {};
18391 template <class T> struct S<T*, int> {};
18393 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18394 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18395 int}. The resulting vector will be {double}, indicating that `T'
18396 is bound to `double'. */
18399 get_class_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
18401 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18403 tree innermost_deduced_args
;
18405 innermost_deduced_args
= make_tree_vec (ntparms
);
18406 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18408 deduced_args
= copy_node (args
);
18409 SET_TMPL_ARGS_LEVEL (deduced_args
,
18410 TMPL_ARGS_DEPTH (deduced_args
),
18411 innermost_deduced_args
);
18414 deduced_args
= innermost_deduced_args
;
18416 if (unify (tparms
, deduced_args
,
18417 INNERMOST_TEMPLATE_ARGS (spec_args
),
18418 INNERMOST_TEMPLATE_ARGS (args
),
18419 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18422 for (i
= 0; i
< ntparms
; ++i
)
18423 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18426 /* Verify that nondeduced template arguments agree with the type
18427 obtained from argument deduction.
18431 struct A { typedef int X; };
18432 template <class T, class U> struct C {};
18433 template <class T> struct C<T, typename T::X> {};
18435 Then with the instantiation `C<A, int>', we can deduce that
18436 `T' is `A' but unify () does not check whether `typename T::X'
18438 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18439 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18441 tf_none
, false, false);
18442 if (spec_args
== error_mark_node
18443 /* We only need to check the innermost arguments; the other
18444 arguments will always agree. */
18445 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18446 INNERMOST_TEMPLATE_ARGS (args
)))
18449 /* Now that we have bindings for all of the template arguments,
18450 ensure that the arguments deduced for the template template
18451 parameters have compatible template parameter lists. See the use
18452 of template_template_parm_bindings_ok_p in fn_type_unification
18453 for more information. */
18454 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18457 return deduced_args
;
18460 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18461 Return the TREE_LIST node with the most specialized template, if
18462 any. If there is no most specialized template, the error_mark_node
18465 Note that this function does not look at, or modify, the
18466 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18467 returned is one of the elements of INSTANTIATIONS, callers may
18468 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18469 and retrieve it from the value returned. */
18472 most_specialized_instantiation (tree templates
)
18476 ++processing_template_decl
;
18479 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18483 if (get_bindings (TREE_VALUE (champ
),
18484 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18485 NULL_TREE
, /*check_ret=*/true))
18488 if (get_bindings (TREE_VALUE (fn
),
18489 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18490 NULL_TREE
, /*check_ret=*/true))
18497 /* Equally specialized, move to next function. If there
18498 is no next function, nothing's most specialized. */
18499 fn
= TREE_CHAIN (fn
);
18507 /* Now verify that champ is better than everything earlier in the
18508 instantiation list. */
18509 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18510 if (get_bindings (TREE_VALUE (champ
),
18511 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18512 NULL_TREE
, /*check_ret=*/true)
18513 || !get_bindings (TREE_VALUE (fn
),
18514 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18515 NULL_TREE
, /*check_ret=*/true))
18521 processing_template_decl
--;
18524 return error_mark_node
;
18529 /* If DECL is a specialization of some template, return the most
18530 general such template. Otherwise, returns NULL_TREE.
18532 For example, given:
18534 template <class T> struct S { template <class U> void f(U); };
18536 if TMPL is `template <class U> void S<int>::f(U)' this will return
18537 the full template. This function will not trace past partial
18538 specializations, however. For example, given in addition:
18540 template <class T> struct S<T*> { template <class U> void f(U); };
18542 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18543 `template <class T> template <class U> S<T*>::f(U)'. */
18546 most_general_template (tree decl
)
18548 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18549 an immediate specialization. */
18550 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18552 if (DECL_TEMPLATE_INFO (decl
)) {
18553 decl
= DECL_TI_TEMPLATE (decl
);
18555 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18556 template friend. */
18557 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18563 /* Look for more and more general templates. */
18564 while (DECL_TEMPLATE_INFO (decl
))
18566 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18567 (See cp-tree.h for details.) */
18568 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18571 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18572 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18575 /* Stop if we run into an explicitly specialized class template. */
18576 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18577 && DECL_CONTEXT (decl
)
18578 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18581 decl
= DECL_TI_TEMPLATE (decl
);
18587 /* Return the most specialized of the class template partial
18588 specializations which can produce TYPE, a specialization of some class
18589 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18590 a _TYPE node corresponding to the partial specialization, while the
18591 TREE_PURPOSE is the set of template arguments that must be
18592 substituted into the TREE_TYPE in order to generate TYPE.
18594 If the choice of partial specialization is ambiguous, a diagnostic
18595 is issued, and the error_mark_node is returned. If there are no
18596 partial specializations matching TYPE, then NULL_TREE is
18597 returned, indicating that the primary template should be used. */
18600 most_specialized_class (tree type
, tsubst_flags_t complain
)
18602 tree list
= NULL_TREE
;
18607 tree outer_args
= NULL_TREE
;
18609 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
18610 tree main_tmpl
= most_general_template (tmpl
);
18611 tree args
= CLASSTYPE_TI_ARGS (type
);
18613 /* For determining which partial specialization to use, only the
18614 innermost args are interesting. */
18615 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18617 outer_args
= strip_innermost_template_args (args
, 1);
18618 args
= INNERMOST_TEMPLATE_ARGS (args
);
18621 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
18623 tree partial_spec_args
;
18625 tree spec_tmpl
= TREE_VALUE (t
);
18626 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18628 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18630 ++processing_template_decl
;
18634 /* Discard the outer levels of args, and then substitute in the
18635 template args from the enclosing class. */
18636 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18637 partial_spec_args
= tsubst_template_args
18638 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18640 /* And the same for the partial specialization TEMPLATE_DECL. */
18641 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18644 partial_spec_args
=
18645 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18648 /*require_all_args=*/true,
18649 /*use_default_args=*/true);
18651 --processing_template_decl
;
18653 if (partial_spec_args
== error_mark_node
)
18654 return error_mark_node
;
18655 if (spec_tmpl
== error_mark_node
)
18656 return error_mark_node
;
18658 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18659 spec_args
= get_class_bindings (tmpl
, parms
,
18665 spec_args
= add_to_template_args (outer_args
, spec_args
);
18666 list
= tree_cons (spec_args
, orig_parms
, list
);
18667 TREE_TYPE (list
) = TREE_TYPE (t
);
18674 ambiguous_p
= false;
18677 t
= TREE_CHAIN (t
);
18678 for (; t
; t
= TREE_CHAIN (t
))
18680 fate
= more_specialized_class (tmpl
, champ
, t
);
18687 t
= TREE_CHAIN (t
);
18690 ambiguous_p
= true;
18699 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18701 fate
= more_specialized_class (tmpl
, champ
, t
);
18704 ambiguous_p
= true;
18712 char *spaces
= NULL
;
18713 if (!(complain
& tf_error
))
18714 return error_mark_node
;
18715 error ("ambiguous class template instantiation for %q#T", type
);
18716 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18717 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18719 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18720 spaces
= spaces
? spaces
: get_spaces (str
);
18723 return error_mark_node
;
18729 /* Explicitly instantiate DECL. */
18732 do_decl_instantiation (tree decl
, tree storage
)
18734 tree result
= NULL_TREE
;
18737 if (!decl
|| decl
== error_mark_node
)
18738 /* An error occurred, for which grokdeclarator has already issued
18739 an appropriate message. */
18741 else if (! DECL_LANG_SPECIFIC (decl
))
18743 error ("explicit instantiation of non-template %q#D", decl
);
18746 else if (VAR_P (decl
))
18748 /* There is an asymmetry here in the way VAR_DECLs and
18749 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18750 the latter, the DECL we get back will be marked as a
18751 template instantiation, and the appropriate
18752 DECL_TEMPLATE_INFO will be set up. This does not happen for
18753 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18754 should handle VAR_DECLs as it currently handles
18756 if (!DECL_CLASS_SCOPE_P (decl
))
18758 error ("%qD is not a static data member of a class template", decl
);
18761 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18762 if (!result
|| !VAR_P (result
))
18764 error ("no matching template for %qD found", decl
);
18767 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18769 error ("type %qT for explicit instantiation %qD does not match "
18770 "declared type %qT", TREE_TYPE (result
), decl
,
18775 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18777 error ("explicit instantiation of %q#D", decl
);
18783 /* Check for various error cases. Note that if the explicit
18784 instantiation is valid the RESULT will currently be marked as an
18785 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18786 until we get here. */
18788 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18790 /* DR 259 [temp.spec].
18792 Both an explicit instantiation and a declaration of an explicit
18793 specialization shall not appear in a program unless the explicit
18794 instantiation follows a declaration of the explicit specialization.
18796 For a given set of template parameters, if an explicit
18797 instantiation of a template appears after a declaration of an
18798 explicit specialization for that template, the explicit
18799 instantiation has no effect. */
18802 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18806 No program shall explicitly instantiate any template more
18809 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18810 the first instantiation was `extern' and the second is not,
18811 and EXTERN_P for the opposite case. */
18812 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18813 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18814 /* If an "extern" explicit instantiation follows an ordinary
18815 explicit instantiation, the template is instantiated. */
18819 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18821 error ("no matching template for %qD found", result
);
18824 else if (!DECL_TEMPLATE_INFO (result
))
18826 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18830 if (storage
== NULL_TREE
)
18832 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18834 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
18835 pedwarn (input_location
, OPT_Wpedantic
,
18836 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18841 error ("storage class %qD applied to template instantiation", storage
);
18843 check_explicit_instantiation_namespace (result
);
18844 mark_decl_instantiated (result
, extern_p
);
18846 instantiate_decl (result
, /*defer_ok=*/1,
18847 /*expl_inst_class_mem_p=*/false);
18851 mark_class_instantiated (tree t
, int extern_p
)
18853 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18854 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18855 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18856 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18859 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18860 rest_of_type_compilation (t
, 1);
18864 /* Called from do_type_instantiation through binding_table_foreach to
18865 do recursive instantiation for the type bound in ENTRY. */
18867 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18869 tree storage
= *(tree
*) data
;
18871 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18872 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18873 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18876 /* Called from do_type_instantiation to instantiate a member
18877 (a member function or a static member variable) of an
18878 explicitly instantiated class template. */
18880 instantiate_class_member (tree decl
, int extern_p
)
18882 mark_decl_instantiated (decl
, extern_p
);
18884 instantiate_decl (decl
, /*defer_ok=*/1,
18885 /*expl_inst_class_mem_p=*/true);
18888 /* Perform an explicit instantiation of template class T. STORAGE, if
18889 non-null, is the RID for extern, inline or static. COMPLAIN is
18890 nonzero if this is called from the parser, zero if called recursively,
18891 since the standard is unclear (as detailed below). */
18894 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18899 int previous_instantiation_extern_p
= 0;
18901 if (TREE_CODE (t
) == TYPE_DECL
)
18904 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18907 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18909 error ("explicit instantiation of non-class template %qD", tmpl
);
18911 error ("explicit instantiation of non-template type %qT", t
);
18917 if (!COMPLETE_TYPE_P (t
))
18919 if (complain
& tf_error
)
18920 error ("explicit instantiation of %q#T before definition of template",
18925 if (storage
!= NULL_TREE
)
18927 if (!in_system_header_at (input_location
))
18929 if (storage
== ridpointers
[(int) RID_EXTERN
])
18931 if (cxx_dialect
== cxx98
)
18932 pedwarn (input_location
, OPT_Wpedantic
,
18933 "ISO C++ 1998 forbids the use of %<extern%> on "
18934 "explicit instantiations");
18937 pedwarn (input_location
, OPT_Wpedantic
,
18938 "ISO C++ forbids the use of %qE"
18939 " on explicit instantiations", storage
);
18942 if (storage
== ridpointers
[(int) RID_INLINE
])
18944 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18946 else if (storage
== ridpointers
[(int) RID_STATIC
])
18950 error ("storage class %qD applied to template instantiation",
18956 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18958 /* DR 259 [temp.spec].
18960 Both an explicit instantiation and a declaration of an explicit
18961 specialization shall not appear in a program unless the explicit
18962 instantiation follows a declaration of the explicit specialization.
18964 For a given set of template parameters, if an explicit
18965 instantiation of a template appears after a declaration of an
18966 explicit specialization for that template, the explicit
18967 instantiation has no effect. */
18970 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18974 No program shall explicitly instantiate any template more
18977 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18978 instantiation was `extern'. If EXTERN_P then the second is.
18979 These cases are OK. */
18980 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18982 if (!previous_instantiation_extern_p
&& !extern_p
18983 && (complain
& tf_error
))
18984 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18986 /* If we've already instantiated the template, just return now. */
18987 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18991 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18992 mark_class_instantiated (t
, extern_p
);
19000 /* In contrast to implicit instantiation, where only the
19001 declarations, and not the definitions, of members are
19002 instantiated, we have here:
19006 The explicit instantiation of a class template specialization
19007 implies the instantiation of all of its members not
19008 previously explicitly specialized in the translation unit
19009 containing the explicit instantiation.
19011 Of course, we can't instantiate member template classes, since
19012 we don't have any arguments for them. Note that the standard
19013 is unclear on whether the instantiation of the members are
19014 *explicit* instantiations or not. However, the most natural
19015 interpretation is that it should be an explicit instantiation. */
19018 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19019 if (TREE_CODE (tmp
) == FUNCTION_DECL
19020 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19021 instantiate_class_member (tmp
, extern_p
);
19023 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19024 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19025 instantiate_class_member (tmp
, extern_p
);
19027 if (CLASSTYPE_NESTED_UTDS (t
))
19028 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19029 bt_instantiate_type_proc
, &storage
);
19033 /* Given a function DECL, which is a specialization of TMPL, modify
19034 DECL to be a re-instantiation of TMPL with the same template
19035 arguments. TMPL should be the template into which tsubst'ing
19036 should occur for DECL, not the most general template.
19038 One reason for doing this is a scenario like this:
19041 void f(const T&, int i);
19043 void g() { f(3, 7); }
19046 void f(const T& t, const int i) { }
19048 Note that when the template is first instantiated, with
19049 instantiate_template, the resulting DECL will have no name for the
19050 first parameter, and the wrong type for the second. So, when we go
19051 to instantiate the DECL, we regenerate it. */
19054 regenerate_decl_from_template (tree decl
, tree tmpl
)
19056 /* The arguments used to instantiate DECL, from the most general
19061 args
= DECL_TI_ARGS (decl
);
19062 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19064 /* Make sure that we can see identifiers, and compute access
19066 push_access_scope (decl
);
19068 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19076 args_depth
= TMPL_ARGS_DEPTH (args
);
19077 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19078 if (args_depth
> parms_depth
)
19079 args
= get_innermost_template_args (args
, parms_depth
);
19081 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19082 args
, tf_error
, NULL_TREE
,
19083 /*defer_ok*/false);
19084 if (specs
&& specs
!= error_mark_node
)
19085 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19088 /* Merge parameter declarations. */
19089 decl_parm
= skip_artificial_parms_for (decl
,
19090 DECL_ARGUMENTS (decl
));
19092 = skip_artificial_parms_for (code_pattern
,
19093 DECL_ARGUMENTS (code_pattern
));
19094 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19099 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19100 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19101 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19103 parm_type
= type_decays_to (parm_type
);
19104 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19105 TREE_TYPE (decl_parm
) = parm_type
;
19106 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19107 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19109 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19110 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19112 decl_parm
= DECL_CHAIN (decl_parm
);
19113 pattern_parm
= DECL_CHAIN (pattern_parm
);
19115 /* Merge any parameters that match with the function parameter
19117 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19120 tree expanded_types
;
19121 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19122 the parameters in this function parameter pack. */
19123 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19124 args
, tf_error
, NULL_TREE
);
19125 len
= TREE_VEC_LENGTH (expanded_types
);
19126 for (i
= 0; i
< len
; i
++)
19131 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19132 /* Rename the parameter to include the index. */
19133 DECL_NAME (decl_parm
) =
19134 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19135 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19136 parm_type
= type_decays_to (parm_type
);
19137 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19138 TREE_TYPE (decl_parm
) = parm_type
;
19139 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19140 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19142 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19143 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19145 decl_parm
= DECL_CHAIN (decl_parm
);
19148 /* Merge additional specifiers from the CODE_PATTERN. */
19149 if (DECL_DECLARED_INLINE_P (code_pattern
)
19150 && !DECL_DECLARED_INLINE_P (decl
))
19151 DECL_DECLARED_INLINE_P (decl
) = 1;
19153 else if (VAR_P (decl
))
19155 DECL_INITIAL (decl
) =
19156 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19157 tf_error
, DECL_TI_TEMPLATE (decl
),
19158 /*integral_constant_expression_p=*/false);
19159 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19160 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19161 tf_error
, DECL_TI_TEMPLATE (decl
));
19164 gcc_unreachable ();
19166 pop_access_scope (decl
);
19169 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19170 substituted to get DECL. */
19173 template_for_substitution (tree decl
)
19175 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19177 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19178 for the instantiation. This is not always the most general
19179 template. Consider, for example:
19182 struct S { template <class U> void f();
19183 template <> void f<int>(); };
19185 and an instantiation of S<double>::f<int>. We want TD to be the
19186 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19187 while (/* An instantiation cannot have a definition, so we need a
19188 more general template. */
19189 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19190 /* We must also deal with friend templates. Given:
19192 template <class T> struct S {
19193 template <class U> friend void f() {};
19196 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19197 so far as the language is concerned, but that's still
19198 where we get the pattern for the instantiation from. On
19199 other hand, if the definition comes outside the class, say:
19201 template <class T> struct S {
19202 template <class U> friend void f();
19204 template <class U> friend void f() {}
19206 we don't need to look any further. That's what the check for
19207 DECL_INITIAL is for. */
19208 || (TREE_CODE (decl
) == FUNCTION_DECL
19209 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19210 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19212 /* The present template, TD, should not be a definition. If it
19213 were a definition, we should be using it! Note that we
19214 cannot restructure the loop to just keep going until we find
19215 a template with a definition, since that might go too far if
19216 a specialization was declared, but not defined. */
19217 gcc_assert (!VAR_P (decl
)
19218 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19220 /* Fetch the more general template. */
19221 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19227 /* Returns true if we need to instantiate this template instance even if we
19228 know we aren't going to emit it.. */
19231 always_instantiate_p (tree decl
)
19233 /* We always instantiate inline functions so that we can inline them. An
19234 explicit instantiation declaration prohibits implicit instantiation of
19235 non-inline functions. With high levels of optimization, we would
19236 normally inline non-inline functions -- but we're not allowed to do
19237 that for "extern template" functions. Therefore, we check
19238 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19239 return ((TREE_CODE (decl
) == FUNCTION_DECL
19240 && (DECL_DECLARED_INLINE_P (decl
)
19241 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19242 /* And we need to instantiate static data members so that
19243 their initializers are available in integral constant
19246 && decl_maybe_constant_var_p (decl
)));
19249 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19250 instantiate it now, modifying TREE_TYPE (fn). */
19253 maybe_instantiate_noexcept (tree fn
)
19255 tree fntype
, spec
, noex
, clone
;
19257 if (DECL_CLONED_FUNCTION_P (fn
))
19258 fn
= DECL_CLONED_FUNCTION (fn
);
19259 fntype
= TREE_TYPE (fn
);
19260 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19262 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19265 noex
= TREE_PURPOSE (spec
);
19267 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19269 if (push_tinst_level (fn
))
19271 push_access_scope (fn
);
19272 push_deferring_access_checks (dk_no_deferred
);
19273 input_location
= DECL_SOURCE_LOCATION (fn
);
19274 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19275 DEFERRED_NOEXCEPT_ARGS (noex
),
19276 tf_warning_or_error
, fn
,
19277 /*function_p=*/false,
19278 /*integral_constant_expression_p=*/true);
19279 pop_deferring_access_checks ();
19280 pop_access_scope (fn
);
19281 pop_tinst_level ();
19282 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19283 if (spec
== error_mark_node
)
19284 spec
= noexcept_false_spec
;
19287 spec
= noexcept_false_spec
;
19291 /* This is an implicitly declared function, so NOEX is a list of
19292 other functions to evaluate and merge. */
19294 spec
= noexcept_true_spec
;
19295 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19297 tree fn
= OVL_CURRENT (elt
);
19299 maybe_instantiate_noexcept (fn
);
19300 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19301 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19305 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19307 FOR_EACH_CLONE (clone
, fn
)
19309 if (TREE_TYPE (clone
) == fntype
)
19310 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19312 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19316 /* Produce the definition of D, a _DECL generated from a template. If
19317 DEFER_OK is nonzero, then we don't have to actually do the
19318 instantiation now; we just have to do it sometime. Normally it is
19319 an error if this is an explicit instantiation but D is undefined.
19320 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19321 explicitly instantiated class template. */
19324 instantiate_decl (tree d
, int defer_ok
,
19325 bool expl_inst_class_mem_p
)
19327 tree tmpl
= DECL_TI_TEMPLATE (d
);
19334 bool pattern_defined
;
19335 location_t saved_loc
= input_location
;
19336 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19337 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19342 /* This function should only be used to instantiate templates for
19343 functions and static member variables. */
19344 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19346 /* Variables are never deferred; if instantiation is required, they
19347 are instantiated right away. That allows for better code in the
19348 case that an expression refers to the value of the variable --
19349 if the variable has a constant value the referring expression can
19350 take advantage of that fact. */
19352 || DECL_DECLARED_CONSTEXPR_P (d
))
19355 /* Don't instantiate cloned functions. Instead, instantiate the
19356 functions they cloned. */
19357 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19358 d
= DECL_CLONED_FUNCTION (d
);
19360 if (DECL_TEMPLATE_INSTANTIATED (d
)
19361 || (TREE_CODE (d
) == FUNCTION_DECL
19362 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19363 || DECL_TEMPLATE_SPECIALIZATION (d
))
19364 /* D has already been instantiated or explicitly specialized, so
19365 there's nothing for us to do here.
19367 It might seem reasonable to check whether or not D is an explicit
19368 instantiation, and, if so, stop here. But when an explicit
19369 instantiation is deferred until the end of the compilation,
19370 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19371 the instantiation. */
19374 /* Check to see whether we know that this template will be
19375 instantiated in some other file, as with "extern template"
19377 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19379 /* In general, we do not instantiate such templates. */
19380 if (external_p
&& !always_instantiate_p (d
))
19383 gen_tmpl
= most_general_template (tmpl
);
19384 gen_args
= DECL_TI_ARGS (d
);
19386 if (tmpl
!= gen_tmpl
)
19387 /* We should already have the extra args. */
19388 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19389 == TMPL_ARGS_DEPTH (gen_args
));
19390 /* And what's in the hash table should match D. */
19391 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19392 || spec
== NULL_TREE
);
19394 /* This needs to happen before any tsubsting. */
19395 if (! push_tinst_level (d
))
19398 timevar_push (TV_TEMPLATE_INST
);
19400 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19401 for the instantiation. */
19402 td
= template_for_substitution (d
);
19403 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19405 /* We should never be trying to instantiate a member of a class
19406 template or partial specialization. */
19407 gcc_assert (d
!= code_pattern
);
19409 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19410 || DECL_TEMPLATE_SPECIALIZATION (td
))
19411 /* In the case of a friend template whose definition is provided
19412 outside the class, we may have too many arguments. Drop the
19413 ones we don't need. The same is true for specializations. */
19414 args
= get_innermost_template_args
19415 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19419 if (TREE_CODE (d
) == FUNCTION_DECL
)
19420 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19421 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19423 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19425 /* We may be in the middle of deferred access check. Disable it now. */
19426 push_deferring_access_checks (dk_no_deferred
);
19428 /* Unless an explicit instantiation directive has already determined
19429 the linkage of D, remember that a definition is available for
19431 if (pattern_defined
19432 && !DECL_INTERFACE_KNOWN (d
)
19433 && !DECL_NOT_REALLY_EXTERN (d
))
19434 mark_definable (d
);
19436 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19437 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19438 input_location
= DECL_SOURCE_LOCATION (d
);
19440 /* If D is a member of an explicitly instantiated class template,
19441 and no definition is available, treat it like an implicit
19443 if (!pattern_defined
&& expl_inst_class_mem_p
19444 && DECL_EXPLICIT_INSTANTIATION (d
))
19446 /* Leave linkage flags alone on instantiations with anonymous
19448 if (TREE_PUBLIC (d
))
19450 DECL_NOT_REALLY_EXTERN (d
) = 0;
19451 DECL_INTERFACE_KNOWN (d
) = 0;
19453 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19456 if (TREE_CODE (d
) == FUNCTION_DECL
)
19457 maybe_instantiate_noexcept (d
);
19459 /* Defer all other templates, unless we have been explicitly
19460 forbidden from doing so. */
19461 if (/* If there is no definition, we cannot instantiate the
19464 /* If it's OK to postpone instantiation, do so. */
19466 /* If this is a static data member that will be defined
19467 elsewhere, we don't want to instantiate the entire data
19468 member, but we do want to instantiate the initializer so that
19469 we can substitute that elsewhere. */
19470 || (external_p
&& VAR_P (d
)))
19472 /* The definition of the static data member is now required so
19473 we must substitute the initializer. */
19475 && !DECL_INITIAL (d
)
19476 && DECL_INITIAL (code_pattern
))
19480 bool const_init
= false;
19482 ns
= decl_namespace_context (d
);
19483 push_nested_namespace (ns
);
19484 push_nested_class (DECL_CONTEXT (d
));
19485 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19487 tf_warning_or_error
, NULL_TREE
,
19488 /*integral_constant_expression_p=*/false);
19489 /* Make sure the initializer is still constant, in case of
19490 circular dependency (template/instantiate6.C). */
19492 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19493 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19494 /*asmspec_tree=*/NULL_TREE
,
19495 LOOKUP_ONLYCONVERTING
);
19496 pop_nested_class ();
19497 pop_nested_namespace (ns
);
19500 /* We restore the source position here because it's used by
19501 add_pending_template. */
19502 input_location
= saved_loc
;
19504 if (at_eof
&& !pattern_defined
19505 && DECL_EXPLICIT_INSTANTIATION (d
)
19506 && DECL_NOT_REALLY_EXTERN (d
))
19509 The definition of a non-exported function template, a
19510 non-exported member function template, or a non-exported
19511 member function or static data member of a class template
19512 shall be present in every translation unit in which it is
19513 explicitly instantiated. */
19514 permerror (input_location
, "explicit instantiation of %qD "
19515 "but no definition available", d
);
19517 /* If we're in unevaluated context, we just wanted to get the
19518 constant value; this isn't an odr use, so don't queue
19519 a full instantiation. */
19520 if (cp_unevaluated_operand
!= 0)
19522 /* ??? Historically, we have instantiated inline functions, even
19523 when marked as "extern template". */
19524 if (!(external_p
&& VAR_P (d
)))
19525 add_pending_template (d
);
19528 /* Tell the repository that D is available in this translation unit
19529 -- and see if it is supposed to be instantiated here. */
19530 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19532 /* In a PCH file, despite the fact that the repository hasn't
19533 requested instantiation in the PCH it is still possible that
19534 an instantiation will be required in a file that includes the
19537 add_pending_template (d
);
19538 /* Instantiate inline functions so that the inliner can do its
19539 job, even though we'll not be emitting a copy of this
19541 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19545 fn_context
= decl_function_context (d
);
19546 nested
= (current_function_decl
!= NULL_TREE
);
19548 push_to_top_level ();
19552 push_function_context ();
19553 cp_unevaluated_operand
= 0;
19554 c_inhibit_evaluation_warnings
= 0;
19557 /* Mark D as instantiated so that recursive calls to
19558 instantiate_decl do not try to instantiate it again. */
19559 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19561 /* Regenerate the declaration in case the template has been modified
19562 by a subsequent redeclaration. */
19563 regenerate_decl_from_template (d
, td
);
19565 /* We already set the file and line above. Reset them now in case
19566 they changed as a result of calling regenerate_decl_from_template. */
19567 input_location
= DECL_SOURCE_LOCATION (d
);
19572 bool const_init
= false;
19574 /* Clear out DECL_RTL; whatever was there before may not be right
19575 since we've reset the type of the declaration. */
19576 SET_DECL_RTL (d
, NULL
);
19577 DECL_IN_AGGR_P (d
) = 0;
19579 /* The initializer is placed in DECL_INITIAL by
19580 regenerate_decl_from_template so we don't need to
19581 push/pop_access_scope again here. Pull it out so that
19582 cp_finish_decl can process it. */
19583 init
= DECL_INITIAL (d
);
19584 DECL_INITIAL (d
) = NULL_TREE
;
19585 DECL_INITIALIZED_P (d
) = 0;
19587 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19588 initializer. That function will defer actual emission until
19589 we have a chance to determine linkage. */
19590 DECL_EXTERNAL (d
) = 0;
19592 /* Enter the scope of D so that access-checking works correctly. */
19593 push_nested_class (DECL_CONTEXT (d
));
19594 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19595 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19596 pop_nested_class ();
19598 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19599 synthesize_method (d
);
19600 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19602 struct pointer_map_t
*saved_local_specializations
;
19606 tree block
= NULL_TREE
;
19608 /* Save away the current list, in case we are instantiating one
19609 template from within the body of another. */
19610 saved_local_specializations
= local_specializations
;
19612 /* Set up the list of local specializations. */
19613 local_specializations
= pointer_map_create ();
19615 /* Set up context. */
19616 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19617 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19618 block
= push_stmt_list ();
19620 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19622 /* Some typedefs referenced from within the template code need to be
19623 access checked at template instantiation time, i.e now. These
19624 types were added to the template at parsing time. Let's get those
19625 and perform the access checks then. */
19626 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19629 /* Create substitution entries for the parameters. */
19630 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19631 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19632 spec_parm
= DECL_ARGUMENTS (d
);
19633 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19635 register_local_specialization (spec_parm
, tmpl_parm
);
19636 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19637 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19639 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19641 if (!DECL_PACK_P (tmpl_parm
))
19643 register_local_specialization (spec_parm
, tmpl_parm
);
19644 spec_parm
= DECL_CHAIN (spec_parm
);
19648 /* Register the (value) argument pack as a specialization of
19649 TMPL_PARM, then move on. */
19650 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19651 register_local_specialization (argpack
, tmpl_parm
);
19654 gcc_assert (!spec_parm
);
19656 /* Substitute into the body of the function. */
19657 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19658 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19659 tf_warning_or_error
, tmpl
);
19662 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19663 tf_warning_or_error
, tmpl
,
19664 /*integral_constant_expression_p=*/false);
19666 /* Set the current input_location to the end of the function
19667 so that finish_function knows where we are. */
19669 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19671 /* Remember if we saw an infinite loop in the template. */
19672 current_function_infinite_loop
19673 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
19676 /* We don't need the local specializations any more. */
19677 pointer_map_destroy (local_specializations
);
19678 local_specializations
= saved_local_specializations
;
19680 /* Finish the function. */
19681 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19682 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19683 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19686 d
= finish_function (0);
19687 expand_or_defer_fn (d
);
19690 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19691 cp_check_omp_declare_reduction (d
);
19694 /* We're not deferring instantiation any more. */
19695 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19698 pop_from_top_level ();
19700 pop_function_context ();
19703 input_location
= saved_loc
;
19704 cp_unevaluated_operand
= saved_unevaluated_operand
;
19705 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19706 pop_deferring_access_checks ();
19707 pop_tinst_level ();
19709 timevar_pop (TV_TEMPLATE_INST
);
19714 /* Run through the list of templates that we wish we could
19715 instantiate, and instantiate any we can. RETRIES is the
19716 number of times we retry pending template instantiation. */
19719 instantiate_pending_templates (int retries
)
19722 location_t saved_loc
= input_location
;
19724 /* Instantiating templates may trigger vtable generation. This in turn
19725 may require further template instantiations. We place a limit here
19726 to avoid infinite loop. */
19727 if (pending_templates
&& retries
>= max_tinst_depth
)
19729 tree decl
= pending_templates
->tinst
->decl
;
19731 error ("template instantiation depth exceeds maximum of %d"
19732 " instantiating %q+D, possibly from virtual table generation"
19733 " (use -ftemplate-depth= to increase the maximum)",
19734 max_tinst_depth
, decl
);
19735 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19736 /* Pretend that we defined it. */
19737 DECL_INITIAL (decl
) = error_mark_node
;
19743 struct pending_template
**t
= &pending_templates
;
19744 struct pending_template
*last
= NULL
;
19748 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19749 bool complete
= false;
19751 if (TYPE_P (instantiation
))
19755 if (!COMPLETE_TYPE_P (instantiation
))
19757 instantiate_class_template (instantiation
);
19758 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19759 for (fn
= TYPE_METHODS (instantiation
);
19761 fn
= TREE_CHAIN (fn
))
19762 if (! DECL_ARTIFICIAL (fn
))
19763 instantiate_decl (fn
,
19765 /*expl_inst_class_mem_p=*/false);
19766 if (COMPLETE_TYPE_P (instantiation
))
19770 complete
= COMPLETE_TYPE_P (instantiation
);
19774 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19775 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19778 = instantiate_decl (instantiation
,
19780 /*expl_inst_class_mem_p=*/false);
19781 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19785 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19786 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19790 /* If INSTANTIATION has been instantiated, then we don't
19791 need to consider it again in the future. */
19799 current_tinst_level
= NULL
;
19801 last_pending_template
= last
;
19803 while (reconsider
);
19805 input_location
= saved_loc
;
19808 /* Substitute ARGVEC into T, which is a list of initializers for
19809 either base class or a non-static data member. The TREE_PURPOSEs
19810 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19811 instantiate_decl. */
19814 tsubst_initializer_list (tree t
, tree argvec
)
19816 tree inits
= NULL_TREE
;
19818 for (; t
; t
= TREE_CHAIN (t
))
19822 tree expanded_bases
= NULL_TREE
;
19823 tree expanded_arguments
= NULL_TREE
;
19826 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19831 /* Expand the base class expansion type into separate base
19833 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19834 tf_warning_or_error
,
19836 if (expanded_bases
== error_mark_node
)
19839 /* We'll be building separate TREE_LISTs of arguments for
19841 len
= TREE_VEC_LENGTH (expanded_bases
);
19842 expanded_arguments
= make_tree_vec (len
);
19843 for (i
= 0; i
< len
; i
++)
19844 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19846 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19847 expand each argument in the TREE_VALUE of t. */
19848 expr
= make_node (EXPR_PACK_EXPANSION
);
19849 PACK_EXPANSION_LOCAL_P (expr
) = true;
19850 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19851 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19853 if (TREE_VALUE (t
) == void_type_node
)
19854 /* VOID_TYPE_NODE is used to indicate
19855 value-initialization. */
19857 for (i
= 0; i
< len
; i
++)
19858 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19862 /* Substitute parameter packs into each argument in the
19864 in_base_initializer
= 1;
19865 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19867 tree expanded_exprs
;
19869 /* Expand the argument. */
19870 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19872 = tsubst_pack_expansion (expr
, argvec
,
19873 tf_warning_or_error
,
19875 if (expanded_exprs
== error_mark_node
)
19878 /* Prepend each of the expanded expressions to the
19879 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19880 for (i
= 0; i
< len
; i
++)
19882 TREE_VEC_ELT (expanded_arguments
, i
) =
19883 tree_cons (NULL_TREE
,
19884 TREE_VEC_ELT (expanded_exprs
, i
),
19885 TREE_VEC_ELT (expanded_arguments
, i
));
19888 in_base_initializer
= 0;
19890 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19891 since we built them backwards. */
19892 for (i
= 0; i
< len
; i
++)
19894 TREE_VEC_ELT (expanded_arguments
, i
) =
19895 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19900 for (i
= 0; i
< len
; ++i
)
19902 if (expanded_bases
)
19904 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19905 decl
= expand_member_init (decl
);
19906 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19911 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19912 tf_warning_or_error
, NULL_TREE
);
19914 decl
= expand_member_init (decl
);
19915 if (decl
&& !DECL_P (decl
))
19916 in_base_initializer
= 1;
19918 init
= TREE_VALUE (t
);
19920 if (init
!= void_type_node
)
19921 init
= tsubst_expr (init
, argvec
,
19922 tf_warning_or_error
, NULL_TREE
,
19923 /*integral_constant_expression_p=*/false);
19924 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19925 /* If we had an initializer but it instantiated to nothing,
19926 value-initialize the object. This will only occur when
19927 the initializer was a pack expansion where the parameter
19928 packs used in that expansion were of length zero. */
19929 init
= void_type_node
;
19930 in_base_initializer
= 0;
19935 init
= build_tree_list (decl
, init
);
19936 TREE_CHAIN (init
) = inits
;
19944 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19947 set_current_access_from_decl (tree decl
)
19949 if (TREE_PRIVATE (decl
))
19950 current_access_specifier
= access_private_node
;
19951 else if (TREE_PROTECTED (decl
))
19952 current_access_specifier
= access_protected_node
;
19954 current_access_specifier
= access_public_node
;
19957 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19958 is the instantiation (which should have been created with
19959 start_enum) and ARGS are the template arguments to use. */
19962 tsubst_enum (tree tag
, tree newtag
, tree args
)
19966 if (SCOPED_ENUM_P (newtag
))
19967 begin_scope (sk_scoped_enum
, newtag
);
19969 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19974 decl
= TREE_VALUE (e
);
19975 /* Note that in a template enum, the TREE_VALUE is the
19976 CONST_DECL, not the corresponding INTEGER_CST. */
19977 value
= tsubst_expr (DECL_INITIAL (decl
),
19978 args
, tf_warning_or_error
, NULL_TREE
,
19979 /*integral_constant_expression_p=*/true);
19981 /* Give this enumeration constant the correct access. */
19982 set_current_access_from_decl (decl
);
19984 /* Actually build the enumerator itself. */
19986 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19989 if (SCOPED_ENUM_P (newtag
))
19992 finish_enum_value_list (newtag
);
19993 finish_enum (newtag
);
19995 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19996 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19999 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20000 its type -- but without substituting the innermost set of template
20001 arguments. So, innermost set of template parameters will appear in
20005 get_mostly_instantiated_function_type (tree decl
)
20013 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
20014 targs
= DECL_TI_ARGS (decl
);
20015 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
20016 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
20018 /* There should be as many levels of arguments as there are levels
20020 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
20022 fn_type
= TREE_TYPE (tmpl
);
20024 if (parm_depth
== 1)
20025 /* No substitution is necessary. */
20032 /* Replace the innermost level of the TARGS with NULL_TREEs to
20033 let tsubst know not to substitute for those parameters. */
20034 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20035 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20036 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20037 TMPL_ARGS_LEVEL (targs
, i
));
20038 SET_TMPL_ARGS_LEVEL (partial_args
,
20039 TMPL_ARGS_DEPTH (targs
),
20040 make_tree_vec (DECL_NTPARMS (tmpl
)));
20042 /* Make sure that we can see identifiers, and compute access
20044 push_access_scope (decl
);
20046 ++processing_template_decl
;
20047 /* Now, do the (partial) substitution to figure out the
20048 appropriate function type. */
20049 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20050 --processing_template_decl
;
20052 /* Substitute into the template parameters to obtain the real
20053 innermost set of parameters. This step is important if the
20054 innermost set of template parameters contains value
20055 parameters whose types depend on outer template parameters. */
20056 TREE_VEC_LENGTH (partial_args
)--;
20057 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20059 pop_access_scope (decl
);
20065 /* Return truthvalue if we're processing a template different from
20066 the last one involved in diagnostics. */
20068 problematic_instantiation_changed (void)
20070 return current_tinst_level
!= last_error_tinst_level
;
20073 /* Remember current template involved in diagnostics. */
20075 record_last_problematic_instantiation (void)
20077 last_error_tinst_level
= current_tinst_level
;
20080 struct tinst_level
*
20081 current_instantiation (void)
20083 return current_tinst_level
;
20086 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20087 type. Return zero for ok, nonzero for disallowed. Issue error and
20088 warning messages under control of COMPLAIN. */
20091 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20093 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20095 else if (POINTER_TYPE_P (type
))
20097 else if (TYPE_PTRMEM_P (type
))
20099 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20101 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20103 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20105 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20108 if (complain
& tf_error
)
20110 if (type
== error_mark_node
)
20111 inform (input_location
, "invalid template non-type parameter");
20113 error ("%q#T is not a valid type for a template non-type parameter",
20119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20123 dependent_type_p_r (tree type
)
20129 A type is dependent if it is:
20131 -- a template parameter. Template template parameters are types
20132 for us (since TYPE_P holds true for them) so we handle
20134 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20135 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20137 /* -- a qualified-id with a nested-name-specifier which contains a
20138 class-name that names a dependent type or whose unqualified-id
20139 names a dependent type. */
20140 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20142 /* -- a cv-qualified type where the cv-unqualified type is
20144 type
= TYPE_MAIN_VARIANT (type
);
20145 /* -- a compound type constructed from any dependent type. */
20146 if (TYPE_PTRMEM_P (type
))
20147 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20148 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20150 else if (TYPE_PTR_P (type
)
20151 || TREE_CODE (type
) == REFERENCE_TYPE
)
20152 return dependent_type_p (TREE_TYPE (type
));
20153 else if (TREE_CODE (type
) == FUNCTION_TYPE
20154 || TREE_CODE (type
) == METHOD_TYPE
)
20158 if (dependent_type_p (TREE_TYPE (type
)))
20160 for (arg_type
= TYPE_ARG_TYPES (type
);
20162 arg_type
= TREE_CHAIN (arg_type
))
20163 if (dependent_type_p (TREE_VALUE (arg_type
)))
20167 /* -- an array type constructed from any dependent type or whose
20168 size is specified by a constant expression that is
20171 We checked for type- and value-dependence of the bounds in
20172 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20173 if (TREE_CODE (type
) == ARRAY_TYPE
)
20175 if (TYPE_DOMAIN (type
)
20176 && dependent_type_p (TYPE_DOMAIN (type
)))
20178 return dependent_type_p (TREE_TYPE (type
));
20181 /* -- a template-id in which either the template name is a template
20183 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20185 /* ... or any of the template arguments is a dependent type or
20186 an expression that is type-dependent or value-dependent. */
20187 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20188 && (any_dependent_template_arguments_p
20189 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20192 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20193 dependent; if the argument of the `typeof' expression is not
20194 type-dependent, then it should already been have resolved. */
20195 if (TREE_CODE (type
) == TYPEOF_TYPE
20196 || TREE_CODE (type
) == DECLTYPE_TYPE
20197 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20200 /* A template argument pack is dependent if any of its packed
20202 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20204 tree args
= ARGUMENT_PACK_ARGS (type
);
20205 int i
, len
= TREE_VEC_LENGTH (args
);
20206 for (i
= 0; i
< len
; ++i
)
20207 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20211 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20212 be template parameters. */
20213 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20216 /* The standard does not specifically mention types that are local
20217 to template functions or local classes, but they should be
20218 considered dependent too. For example:
20220 template <int I> void f() {
20225 The size of `E' cannot be known until the value of `I' has been
20226 determined. Therefore, `E' must be considered dependent. */
20227 scope
= TYPE_CONTEXT (type
);
20228 if (scope
&& TYPE_P (scope
))
20229 return dependent_type_p (scope
);
20230 /* Don't use type_dependent_expression_p here, as it can lead
20231 to infinite recursion trying to determine whether a lambda
20232 nested in a lambda is dependent (c++/47687). */
20233 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20234 && DECL_LANG_SPECIFIC (scope
)
20235 && DECL_TEMPLATE_INFO (scope
)
20236 && (any_dependent_template_arguments_p
20237 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20240 /* Other types are non-dependent. */
20244 /* Returns TRUE if TYPE is dependent, in the sense of
20245 [temp.dep.type]. Note that a NULL type is considered dependent. */
20248 dependent_type_p (tree type
)
20250 /* If there are no template parameters in scope, then there can't be
20251 any dependent types. */
20252 if (!processing_template_decl
)
20254 /* If we are not processing a template, then nobody should be
20255 providing us with a dependent type. */
20257 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20261 /* If the type is NULL, we have not computed a type for the entity
20262 in question; in that case, the type is dependent. */
20266 /* Erroneous types can be considered non-dependent. */
20267 if (type
== error_mark_node
)
20270 /* If we have not already computed the appropriate value for TYPE,
20272 if (!TYPE_DEPENDENT_P_VALID (type
))
20274 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20275 TYPE_DEPENDENT_P_VALID (type
) = 1;
20278 return TYPE_DEPENDENT_P (type
);
20281 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20282 lookup. In other words, a dependent type that is not the current
20286 dependent_scope_p (tree scope
)
20288 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20289 && !currently_open_class (scope
));
20292 /* T is a SCOPE_REF; return whether we need to consider it
20293 instantiation-dependent so that we can check access at instantiation
20294 time even though we know which member it resolves to. */
20297 instantiation_dependent_scope_ref_p (tree t
)
20299 if (DECL_P (TREE_OPERAND (t
, 1))
20300 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20301 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20302 TREE_OPERAND (t
, 1)))
20308 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20309 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20312 /* Note that this predicate is not appropriate for general expressions;
20313 only constant expressions (that satisfy potential_constant_expression)
20314 can be tested for value dependence. */
20317 value_dependent_expression_p (tree expression
)
20319 if (!processing_template_decl
)
20322 /* A name declared with a dependent type. */
20323 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20326 switch (TREE_CODE (expression
))
20328 case IDENTIFIER_NODE
:
20329 /* A name that has not been looked up -- must be dependent. */
20332 case TEMPLATE_PARM_INDEX
:
20333 /* A non-type template parm. */
20337 /* A non-type template parm. */
20338 if (DECL_TEMPLATE_PARM_P (expression
))
20340 return value_dependent_expression_p (DECL_INITIAL (expression
));
20343 /* A constant with literal type and is initialized
20344 with an expression that is value-dependent.
20346 Note that a non-dependent parenthesized initializer will have
20347 already been replaced with its constant value, so if we see
20348 a TREE_LIST it must be dependent. */
20349 if (DECL_INITIAL (expression
)
20350 && decl_constant_var_p (expression
)
20351 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20352 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20356 case DYNAMIC_CAST_EXPR
:
20357 case STATIC_CAST_EXPR
:
20358 case CONST_CAST_EXPR
:
20359 case REINTERPRET_CAST_EXPR
:
20361 /* These expressions are value-dependent if the type to which
20362 the cast occurs is dependent or the expression being casted
20363 is value-dependent. */
20365 tree type
= TREE_TYPE (expression
);
20367 if (dependent_type_p (type
))
20370 /* A functional cast has a list of operands. */
20371 expression
= TREE_OPERAND (expression
, 0);
20374 /* If there are no operands, it must be an expression such
20375 as "int()". This should not happen for aggregate types
20376 because it would form non-constant expressions. */
20377 gcc_assert (cxx_dialect
>= cxx11
20378 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20383 if (TREE_CODE (expression
) == TREE_LIST
)
20384 return any_value_dependent_elements_p (expression
);
20386 return value_dependent_expression_p (expression
);
20390 if (SIZEOF_EXPR_TYPE_P (expression
))
20391 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20395 /* A `sizeof' expression is value-dependent if the operand is
20396 type-dependent or is a pack expansion. */
20397 expression
= TREE_OPERAND (expression
, 0);
20398 if (PACK_EXPANSION_P (expression
))
20400 else if (TYPE_P (expression
))
20401 return dependent_type_p (expression
);
20402 return instantiation_dependent_expression_p (expression
);
20404 case AT_ENCODE_EXPR
:
20405 /* An 'encode' expression is value-dependent if the operand is
20407 expression
= TREE_OPERAND (expression
, 0);
20408 return dependent_type_p (expression
);
20410 case NOEXCEPT_EXPR
:
20411 expression
= TREE_OPERAND (expression
, 0);
20412 return instantiation_dependent_expression_p (expression
);
20415 /* All instantiation-dependent expressions should also be considered
20416 value-dependent. */
20417 return instantiation_dependent_scope_ref_p (expression
);
20419 case COMPONENT_REF
:
20420 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20421 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20423 case NONTYPE_ARGUMENT_PACK
:
20424 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20425 is value-dependent. */
20427 tree values
= ARGUMENT_PACK_ARGS (expression
);
20428 int i
, len
= TREE_VEC_LENGTH (values
);
20430 for (i
= 0; i
< len
; ++i
)
20431 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20439 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20440 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20441 || (type2
? dependent_type_p (type2
) : false));
20445 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20446 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20449 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20450 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20454 tree op
= TREE_OPERAND (expression
, 0);
20455 return (value_dependent_expression_p (op
)
20456 || has_value_dependent_address (op
));
20461 tree fn
= get_callee_fndecl (expression
);
20463 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20465 nargs
= call_expr_nargs (expression
);
20466 for (i
= 0; i
< nargs
; ++i
)
20468 tree op
= CALL_EXPR_ARG (expression
, i
);
20469 /* In a call to a constexpr member function, look through the
20470 implicit ADDR_EXPR on the object argument so that it doesn't
20471 cause the call to be considered value-dependent. We also
20472 look through it in potential_constant_expression. */
20473 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20474 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20475 && TREE_CODE (op
) == ADDR_EXPR
)
20476 op
= TREE_OPERAND (op
, 0);
20477 if (value_dependent_expression_p (op
))
20483 case TEMPLATE_ID_EXPR
:
20484 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20486 return type_dependent_expression_p (expression
);
20492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20493 if (value_dependent_expression_p (val
))
20499 /* Treat a GNU statement expression as dependent to avoid crashing
20500 under fold_non_dependent_expr; it can't be constant. */
20504 /* A constant expression is value-dependent if any subexpression is
20505 value-dependent. */
20506 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20508 case tcc_reference
:
20510 case tcc_comparison
:
20512 case tcc_expression
:
20515 int i
, len
= cp_tree_operand_length (expression
);
20517 for (i
= 0; i
< len
; i
++)
20519 tree t
= TREE_OPERAND (expression
, i
);
20521 /* In some cases, some of the operands may be missing.l
20522 (For example, in the case of PREDECREMENT_EXPR, the
20523 amount to increment by may be missing.) That doesn't
20524 make the expression dependent. */
20525 if (t
&& value_dependent_expression_p (t
))
20536 /* The expression is not value-dependent. */
20540 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20541 [temp.dep.expr]. Note that an expression with no type is
20542 considered dependent. Other parts of the compiler arrange for an
20543 expression with type-dependent subexpressions to have no type, so
20544 this function doesn't have to be fully recursive. */
20547 type_dependent_expression_p (tree expression
)
20549 if (!processing_template_decl
)
20552 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20555 /* An unresolved name is always dependent. */
20556 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20559 /* Some expression forms are never type-dependent. */
20560 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20561 || TREE_CODE (expression
) == SIZEOF_EXPR
20562 || TREE_CODE (expression
) == ALIGNOF_EXPR
20563 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20564 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20565 || TREE_CODE (expression
) == TRAIT_EXPR
20566 || TREE_CODE (expression
) == TYPEID_EXPR
20567 || TREE_CODE (expression
) == DELETE_EXPR
20568 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20569 || TREE_CODE (expression
) == THROW_EXPR
)
20572 /* The types of these expressions depends only on the type to which
20573 the cast occurs. */
20574 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20575 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20576 || TREE_CODE (expression
) == CONST_CAST_EXPR
20577 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20578 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20579 || TREE_CODE (expression
) == CAST_EXPR
)
20580 return dependent_type_p (TREE_TYPE (expression
));
20582 /* The types of these expressions depends only on the type created
20583 by the expression. */
20584 if (TREE_CODE (expression
) == NEW_EXPR
20585 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20587 /* For NEW_EXPR tree nodes created inside a template, either
20588 the object type itself or a TREE_LIST may appear as the
20590 tree type
= TREE_OPERAND (expression
, 1);
20591 if (TREE_CODE (type
) == TREE_LIST
)
20592 /* This is an array type. We need to check array dimensions
20594 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20595 || value_dependent_expression_p
20596 (TREE_OPERAND (TREE_VALUE (type
), 1));
20598 return dependent_type_p (type
);
20601 if (TREE_CODE (expression
) == SCOPE_REF
)
20603 tree scope
= TREE_OPERAND (expression
, 0);
20604 tree name
= TREE_OPERAND (expression
, 1);
20606 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20607 contains an identifier associated by name lookup with one or more
20608 declarations declared with a dependent type, or...a
20609 nested-name-specifier or qualified-id that names a member of an
20610 unknown specialization. */
20611 return (type_dependent_expression_p (name
)
20612 || dependent_scope_p (scope
));
20615 if (TREE_CODE (expression
) == FUNCTION_DECL
20616 && DECL_LANG_SPECIFIC (expression
)
20617 && DECL_TEMPLATE_INFO (expression
)
20618 && (any_dependent_template_arguments_p
20619 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20622 if (TREE_CODE (expression
) == TEMPLATE_DECL
20623 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20626 if (TREE_CODE (expression
) == STMT_EXPR
)
20627 expression
= stmt_expr_value_expr (expression
);
20629 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20634 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20636 if (type_dependent_expression_p (elt
))
20642 /* A static data member of the current instantiation with incomplete
20643 array type is type-dependent, as the definition and specializations
20644 can have different bounds. */
20645 if (VAR_P (expression
)
20646 && DECL_CLASS_SCOPE_P (expression
)
20647 && dependent_type_p (DECL_CONTEXT (expression
))
20648 && VAR_HAD_UNKNOWN_BOUND (expression
))
20651 /* An array of unknown bound depending on a variadic parameter, eg:
20653 template<typename... Args>
20654 void foo (Args... args)
20656 int arr[] = { args... };
20659 template<int... vals>
20662 int arr[] = { vals... };
20665 If the array has no length and has an initializer, it must be that
20666 we couldn't determine its length in cp_complete_array_type because
20667 it is dependent. */
20668 if (VAR_P (expression
)
20669 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20670 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20671 && DECL_INITIAL (expression
))
20674 if (TREE_TYPE (expression
) == unknown_type_node
)
20676 if (TREE_CODE (expression
) == ADDR_EXPR
)
20677 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20678 if (TREE_CODE (expression
) == COMPONENT_REF
20679 || TREE_CODE (expression
) == OFFSET_REF
)
20681 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20683 expression
= TREE_OPERAND (expression
, 1);
20684 if (identifier_p (expression
))
20687 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20688 if (TREE_CODE (expression
) == SCOPE_REF
)
20691 /* Always dependent, on the number of arguments if nothing else. */
20692 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20695 if (BASELINK_P (expression
))
20696 expression
= BASELINK_FUNCTIONS (expression
);
20698 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20700 if (any_dependent_template_arguments_p
20701 (TREE_OPERAND (expression
, 1)))
20703 expression
= TREE_OPERAND (expression
, 0);
20705 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20706 || TREE_CODE (expression
) == FUNCTION_DECL
);
20710 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20712 expression
= OVL_NEXT (expression
);
20717 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20719 return (dependent_type_p (TREE_TYPE (expression
)));
20722 /* walk_tree callback function for instantiation_dependent_expression_p,
20723 below. Returns non-zero if a dependent subexpression is found. */
20726 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20731 /* We don't have to worry about decltype currently because decltype
20732 of an instantiation-dependent expr is a dependent type. This
20733 might change depending on the resolution of DR 1172. */
20734 *walk_subtrees
= false;
20737 enum tree_code code
= TREE_CODE (*tp
);
20740 /* Don't treat an argument list as dependent just because it has no
20748 /* A constant with a dependent initializer is dependent. */
20749 if (value_dependent_expression_p (*tp
))
20753 case TEMPLATE_PARM_INDEX
:
20756 /* Handle expressions with type operands. */
20760 case AT_ENCODE_EXPR
:
20762 tree op
= TREE_OPERAND (*tp
, 0);
20763 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20764 op
= TREE_TYPE (op
);
20767 if (dependent_type_p (op
))
20771 *walk_subtrees
= false;
20779 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20780 || (TRAIT_EXPR_TYPE2 (*tp
)
20781 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20783 *walk_subtrees
= false;
20786 case COMPONENT_REF
:
20787 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20788 /* In a template, finish_class_member_access_expr creates a
20789 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20790 type-dependent, so that we can check access control at
20791 instantiation time (PR 42277). See also Core issue 1273. */
20796 if (instantiation_dependent_scope_ref_p (*tp
))
20801 /* Treat statement-expressions as dependent. */
20809 if (type_dependent_expression_p (*tp
))
20815 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20816 sense defined by the ABI:
20818 "An expression is instantiation-dependent if it is type-dependent
20819 or value-dependent, or it has a subexpression that is type-dependent
20820 or value-dependent." */
20823 instantiation_dependent_expression_p (tree expression
)
20827 if (!processing_template_decl
)
20830 if (expression
== error_mark_node
)
20833 result
= cp_walk_tree_without_duplicates (&expression
,
20834 instantiation_dependent_r
, NULL
);
20835 return result
!= NULL_TREE
;
20838 /* Like type_dependent_expression_p, but it also works while not processing
20839 a template definition, i.e. during substitution or mangling. */
20842 type_dependent_expression_p_push (tree expr
)
20845 ++processing_template_decl
;
20846 b
= type_dependent_expression_p (expr
);
20847 --processing_template_decl
;
20851 /* Returns TRUE if ARGS contains a type-dependent expression. */
20854 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20859 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20861 if (type_dependent_expression_p (arg
))
20867 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20868 expressions) contains any type-dependent expressions. */
20871 any_type_dependent_elements_p (const_tree list
)
20873 for (; list
; list
= TREE_CHAIN (list
))
20874 if (type_dependent_expression_p (TREE_VALUE (list
)))
20880 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20881 expressions) contains any value-dependent expressions. */
20884 any_value_dependent_elements_p (const_tree list
)
20886 for (; list
; list
= TREE_CHAIN (list
))
20887 if (value_dependent_expression_p (TREE_VALUE (list
)))
20893 /* Returns TRUE if the ARG (a template argument) is dependent. */
20896 dependent_template_arg_p (tree arg
)
20898 if (!processing_template_decl
)
20901 /* Assume a template argument that was wrongly written by the user
20902 is dependent. This is consistent with what
20903 any_dependent_template_arguments_p [that calls this function]
20905 if (!arg
|| arg
== error_mark_node
)
20908 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20909 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20911 if (TREE_CODE (arg
) == TEMPLATE_DECL
20912 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20913 return dependent_template_p (arg
);
20914 else if (ARGUMENT_PACK_P (arg
))
20916 tree args
= ARGUMENT_PACK_ARGS (arg
);
20917 int i
, len
= TREE_VEC_LENGTH (args
);
20918 for (i
= 0; i
< len
; ++i
)
20920 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20926 else if (TYPE_P (arg
))
20927 return dependent_type_p (arg
);
20929 return (type_dependent_expression_p (arg
)
20930 || value_dependent_expression_p (arg
));
20933 /* Returns true if ARGS (a collection of template arguments) contains
20934 any types that require structural equality testing. */
20937 any_template_arguments_need_structural_equality_p (tree args
)
20944 if (args
== error_mark_node
)
20947 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20949 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20950 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20952 tree arg
= TREE_VEC_ELT (level
, j
);
20953 tree packed_args
= NULL_TREE
;
20956 if (ARGUMENT_PACK_P (arg
))
20958 /* Look inside the argument pack. */
20959 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20960 len
= TREE_VEC_LENGTH (packed_args
);
20963 for (k
= 0; k
< len
; ++k
)
20966 arg
= TREE_VEC_ELT (packed_args
, k
);
20968 if (error_operand_p (arg
))
20970 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20972 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20974 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20975 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20984 /* Returns true if ARGS (a collection of template arguments) contains
20985 any dependent arguments. */
20988 any_dependent_template_arguments_p (const_tree args
)
20995 if (args
== error_mark_node
)
20998 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21000 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21001 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21002 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21009 /* Returns TRUE if the template TMPL is dependent. */
21012 dependent_template_p (tree tmpl
)
21014 if (TREE_CODE (tmpl
) == OVERLOAD
)
21018 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21020 tmpl
= OVL_NEXT (tmpl
);
21025 /* Template template parameters are dependent. */
21026 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21027 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21029 /* So are names that have not been looked up. */
21030 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21032 /* So are member templates of dependent classes. */
21033 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21034 return dependent_type_p (DECL_CONTEXT (tmpl
));
21038 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21041 dependent_template_id_p (tree tmpl
, tree args
)
21043 return (dependent_template_p (tmpl
)
21044 || any_dependent_template_arguments_p (args
));
21047 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21051 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21055 if (!processing_template_decl
)
21058 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21060 tree decl
= TREE_VEC_ELT (declv
, i
);
21061 tree init
= TREE_VEC_ELT (initv
, i
);
21062 tree cond
= TREE_VEC_ELT (condv
, i
);
21063 tree incr
= TREE_VEC_ELT (incrv
, i
);
21065 if (type_dependent_expression_p (decl
))
21068 if (init
&& type_dependent_expression_p (init
))
21071 if (type_dependent_expression_p (cond
))
21074 if (COMPARISON_CLASS_P (cond
)
21075 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21076 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21079 if (TREE_CODE (incr
) == MODOP_EXPR
)
21081 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21082 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21085 else if (type_dependent_expression_p (incr
))
21087 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21089 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21091 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21093 tree t
= TREE_OPERAND (incr
, 1);
21094 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21095 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21104 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21105 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21106 no such TYPE can be found. Note that this function peers inside
21107 uninstantiated templates and therefore should be used only in
21108 extremely limited situations. ONLY_CURRENT_P restricts this
21109 peering to the currently open classes hierarchy (which is required
21110 when comparing types). */
21113 resolve_typename_type (tree type
, bool only_current_p
)
21122 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21124 scope
= TYPE_CONTEXT (type
);
21125 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21126 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21127 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21128 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21129 identifier of the TYPENAME_TYPE anymore.
21130 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21131 TYPENAME_TYPE instead, we avoid messing up with a possible
21132 typedef variant case. */
21133 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21135 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21136 it first before we can figure out what NAME refers to. */
21137 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21139 if (TYPENAME_IS_RESOLVING_P (scope
))
21140 /* Given a class template A with a dependent base with nested type C,
21141 typedef typename A::C::C C will land us here, as trying to resolve
21142 the initial A::C leads to the local C typedef, which leads back to
21143 A::C::C. So we break the recursion now. */
21146 scope
= resolve_typename_type (scope
, only_current_p
);
21148 /* If we don't know what SCOPE refers to, then we cannot resolve the
21150 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21152 /* If the SCOPE is a template type parameter, we have no way of
21153 resolving the name. */
21154 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21156 /* If the SCOPE is not the current instantiation, there's no reason
21157 to look inside it. */
21158 if (only_current_p
&& !currently_open_class (scope
))
21160 /* If this is a typedef, we don't want to look inside (c++/11987). */
21161 if (typedef_variant_p (type
))
21163 /* If SCOPE isn't the template itself, it will not have a valid
21164 TYPE_FIELDS list. */
21165 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21166 /* scope is either the template itself or a compatible instantiation
21167 like X<T>, so look up the name in the original template. */
21168 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21170 /* scope is a partial instantiation, so we can't do the lookup or we
21171 will lose the template arguments. */
21173 /* Enter the SCOPE so that name lookup will be resolved as if we
21174 were in the class definition. In particular, SCOPE will no
21175 longer be considered a dependent type. */
21176 pushed_scope
= push_scope (scope
);
21177 /* Look up the declaration. */
21178 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21179 tf_warning_or_error
);
21181 result
= NULL_TREE
;
21183 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21184 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21187 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21188 && TREE_CODE (decl
) == TYPE_DECL
)
21190 result
= TREE_TYPE (decl
);
21191 if (result
== error_mark_node
)
21192 result
= NULL_TREE
;
21194 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21195 && DECL_CLASS_TEMPLATE_P (decl
))
21199 /* Obtain the template and the arguments. */
21200 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21201 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21202 /* Instantiate the template. */
21203 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21204 /*entering_scope=*/0,
21205 tf_error
| tf_user
);
21206 if (result
== error_mark_node
)
21207 result
= NULL_TREE
;
21210 /* Leave the SCOPE. */
21212 pop_scope (pushed_scope
);
21214 /* If we failed to resolve it, return the original typename. */
21218 /* If lookup found a typename type, resolve that too. */
21219 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21221 /* Ill-formed programs can cause infinite recursion here, so we
21222 must catch that. */
21223 TYPENAME_IS_RESOLVING_P (type
) = 1;
21224 result
= resolve_typename_type (result
, only_current_p
);
21225 TYPENAME_IS_RESOLVING_P (type
) = 0;
21228 /* Qualify the resulting type. */
21229 quals
= cp_type_quals (type
);
21231 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21236 /* EXPR is an expression which is not type-dependent. Return a proxy
21237 for EXPR that can be used to compute the types of larger
21238 expressions containing EXPR. */
21241 build_non_dependent_expr (tree expr
)
21245 #ifdef ENABLE_CHECKING
21246 /* Try to get a constant value for all non-dependent expressions in
21247 order to expose bugs in *_dependent_expression_p and constexpr. */
21248 if (cxx_dialect
>= cxx11
)
21249 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21252 /* Preserve OVERLOADs; the functions must be available to resolve
21255 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21256 inner_expr
= stmt_expr_value_expr (inner_expr
);
21257 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21258 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21259 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21260 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21261 if (is_overloaded_fn (inner_expr
)
21262 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21264 /* There is no need to return a proxy for a variable. */
21267 /* Preserve string constants; conversions from string constants to
21268 "char *" are allowed, even though normally a "const char *"
21269 cannot be used to initialize a "char *". */
21270 if (TREE_CODE (expr
) == STRING_CST
)
21272 /* Preserve arithmetic constants, as an optimization -- there is no
21273 reason to create a new node. */
21274 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21276 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21277 There is at least one place where we want to know that a
21278 particular expression is a throw-expression: when checking a ?:
21279 expression, there are special rules if the second or third
21280 argument is a throw-expression. */
21281 if (TREE_CODE (expr
) == THROW_EXPR
)
21284 /* Don't wrap an initializer list, we need to be able to look inside. */
21285 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21288 /* Don't wrap a dummy object, we need to be able to test for it. */
21289 if (is_dummy_object (expr
))
21292 if (TREE_CODE (expr
) == COND_EXPR
)
21293 return build3 (COND_EXPR
,
21295 TREE_OPERAND (expr
, 0),
21296 (TREE_OPERAND (expr
, 1)
21297 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21298 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21299 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21300 if (TREE_CODE (expr
) == COMPOUND_EXPR
21301 && !COMPOUND_EXPR_OVERLOADED (expr
))
21302 return build2 (COMPOUND_EXPR
,
21304 TREE_OPERAND (expr
, 0),
21305 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21307 /* If the type is unknown, it can't really be non-dependent */
21308 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21310 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21311 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21314 /* ARGS is a vector of expressions as arguments to a function call.
21315 Replace the arguments with equivalent non-dependent expressions.
21316 This modifies ARGS in place. */
21319 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21324 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21326 tree newarg
= build_non_dependent_expr (arg
);
21328 (*args
)[ix
] = newarg
;
21332 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21333 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21337 make_auto_1 (tree name
)
21339 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21340 TYPE_NAME (au
) = build_decl (input_location
,
21341 TYPE_DECL
, name
, au
);
21342 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21343 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21344 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21345 TYPE_NAME (au
), NULL_TREE
);
21346 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21347 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21348 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21354 make_decltype_auto (void)
21356 return make_auto_1 (get_identifier ("decltype(auto)"));
21362 return make_auto_1 (get_identifier ("auto"));
21365 /* Given type ARG, return std::initializer_list<ARG>. */
21370 tree std_init_list
= namespace_binding
21371 (get_identifier ("initializer_list"), std_node
);
21373 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21375 error ("deducing from brace-enclosed initializer list requires "
21376 "#include <initializer_list>");
21377 return error_mark_node
;
21379 argvec
= make_tree_vec (1);
21380 TREE_VEC_ELT (argvec
, 0) = arg
;
21381 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21382 NULL_TREE
, 0, tf_warning_or_error
);
21385 /* Replace auto in TYPE with std::initializer_list<auto>. */
21388 listify_autos (tree type
, tree auto_node
)
21390 tree init_auto
= listify (auto_node
);
21391 tree argvec
= make_tree_vec (1);
21392 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21393 if (processing_template_decl
)
21394 argvec
= add_to_template_args (current_template_args (), argvec
);
21395 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21398 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21399 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21402 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21406 if (init
== error_mark_node
)
21407 return error_mark_node
;
21409 if (type_dependent_expression_p (init
))
21410 /* Defining a subset of type-dependent expressions that we can deduce
21411 from ahead of time isn't worth the trouble. */
21414 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21415 with either a new invented type template parameter U or, if the
21416 initializer is a braced-init-list (8.5.4), with
21417 std::initializer_list<U>. */
21418 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21419 type
= listify_autos (type
, auto_node
);
21421 init
= resolve_nondeduced_context (init
);
21423 targs
= make_tree_vec (1);
21424 if (AUTO_IS_DECLTYPE (auto_node
))
21426 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21427 TREE_VEC_ELT (targs
, 0)
21428 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21429 if (type
!= auto_node
)
21431 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21432 return error_mark_node
;
21437 tree parms
= build_tree_list (NULL_TREE
, type
);
21438 tree tparms
= make_tree_vec (1);
21441 TREE_VEC_ELT (tparms
, 0)
21442 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21443 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21444 DEDUCE_CALL
, LOOKUP_NORMAL
,
21445 NULL
, /*explain_p=*/false);
21448 if (processing_template_decl
)
21449 /* Try again at instantiation time. */
21451 if (type
&& type
!= error_mark_node
)
21452 /* If type is error_mark_node a diagnostic must have been
21453 emitted by now. Also, having a mention to '<type error>'
21454 in the diagnostic is not really useful to the user. */
21456 if (cfun
&& auto_node
== current_function_auto_return_pattern
21457 && LAMBDA_FUNCTION_P (current_function_decl
))
21458 error ("unable to deduce lambda return type from %qE", init
);
21460 error ("unable to deduce %qT from %qE", type
, init
);
21462 return error_mark_node
;
21466 /* If the list of declarators contains more than one declarator, the type
21467 of each declared variable is determined as described above. If the
21468 type deduced for the template parameter U is not the same in each
21469 deduction, the program is ill-formed. */
21470 if (TREE_TYPE (auto_node
)
21471 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21473 if (cfun
&& auto_node
== current_function_auto_return_pattern
21474 && LAMBDA_FUNCTION_P (current_function_decl
))
21475 error ("inconsistent types %qT and %qT deduced for "
21476 "lambda return type", TREE_TYPE (auto_node
),
21477 TREE_VEC_ELT (targs
, 0));
21479 error ("inconsistent deduction for %qT: %qT and then %qT",
21480 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21481 return error_mark_node
;
21483 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21485 if (processing_template_decl
)
21486 targs
= add_to_template_args (current_template_args (), targs
);
21487 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21490 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21494 splice_late_return_type (tree type
, tree late_return_type
)
21498 if (late_return_type
== NULL_TREE
)
21500 argvec
= make_tree_vec (1);
21501 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21502 if (processing_template_parmlist
)
21503 /* For a late-specified return type in a template type-parameter, we
21504 need to add a dummy argument level for its parmlist. */
21505 argvec
= add_to_template_args
21506 (make_tree_vec (processing_template_parmlist
), argvec
);
21507 if (current_template_parms
)
21508 argvec
= add_to_template_args (current_template_args (), argvec
);
21509 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21512 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21513 'decltype(auto)'. */
21516 is_auto (const_tree type
)
21518 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21519 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21520 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21526 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21527 a use of `auto'. Returns NULL_TREE otherwise. */
21530 type_uses_auto (tree type
)
21532 return find_type_usage (type
, is_auto
);
21535 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21536 'decltype(auto)' or a concept. */
21539 is_auto_or_concept (const_tree type
)
21541 return is_auto (type
); // or concept
21544 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21545 a concept identifier) iff TYPE contains a use of a generic type. Returns
21546 NULL_TREE otherwise. */
21549 type_uses_auto_or_concept (tree type
)
21551 return find_type_usage (type
, is_auto_or_concept
);
21555 /* For a given template T, return the vector of typedefs referenced
21556 in T for which access check is needed at T instantiation time.
21557 T is either a FUNCTION_DECL or a RECORD_TYPE.
21558 Those typedefs were added to T by the function
21559 append_type_to_template_for_access_check. */
21561 vec
<qualified_typedef_usage_t
, va_gc
> *
21562 get_types_needing_access_check (tree t
)
21565 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21567 if (!t
|| t
== error_mark_node
)
21570 if (!(ti
= get_template_info (t
)))
21573 if (CLASS_TYPE_P (t
)
21574 || TREE_CODE (t
) == FUNCTION_DECL
)
21576 if (!TI_TEMPLATE (ti
))
21579 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21585 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21586 tied to T. That list of typedefs will be access checked at
21587 T instantiation time.
21588 T is either a FUNCTION_DECL or a RECORD_TYPE.
21589 TYPE_DECL is a TYPE_DECL node representing a typedef.
21590 SCOPE is the scope through which TYPE_DECL is accessed.
21591 LOCATION is the location of the usage point of TYPE_DECL.
21593 This function is a subroutine of
21594 append_type_to_template_for_access_check. */
21597 append_type_to_template_for_access_check_1 (tree t
,
21600 location_t location
)
21602 qualified_typedef_usage_t typedef_usage
;
21605 if (!t
|| t
== error_mark_node
)
21608 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21609 || CLASS_TYPE_P (t
))
21611 && TREE_CODE (type_decl
) == TYPE_DECL
21614 if (!(ti
= get_template_info (t
)))
21617 gcc_assert (TI_TEMPLATE (ti
));
21619 typedef_usage
.typedef_decl
= type_decl
;
21620 typedef_usage
.context
= scope
;
21621 typedef_usage
.locus
= location
;
21623 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21626 /* Append TYPE_DECL to the template TEMPL.
21627 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21628 At TEMPL instanciation time, TYPE_DECL will be checked to see
21629 if it can be accessed through SCOPE.
21630 LOCATION is the location of the usage point of TYPE_DECL.
21632 e.g. consider the following code snippet:
21639 template<class U> struct S
21641 C::myint mi; // <-- usage point of the typedef C::myint
21646 At S<char> instantiation time, we need to check the access of C::myint
21647 In other words, we need to check the access of the myint typedef through
21648 the C scope. For that purpose, this function will add the myint typedef
21649 and the scope C through which its being accessed to a list of typedefs
21650 tied to the template S. That list will be walked at template instantiation
21651 time and access check performed on each typedefs it contains.
21652 Note that this particular code snippet should yield an error because
21653 myint is private to C. */
21656 append_type_to_template_for_access_check (tree templ
,
21659 location_t location
)
21661 qualified_typedef_usage_t
*iter
;
21664 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21666 /* Make sure we don't append the type to the template twice. */
21667 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21668 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21671 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21675 /* Convert the generic type parameters in PARM that match the types given in the
21676 range [START_IDX, END_IDX) from the current_template_parms into generic type
21680 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21682 tree current
= current_template_parms
;
21683 int depth
= TMPL_PARMS_DEPTH (current
);
21684 current
= INNERMOST_TEMPLATE_PARMS (current
);
21685 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21687 for (int i
= 0; i
< start_idx
; ++i
)
21688 TREE_VEC_ELT (replacement
, i
)
21689 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21691 for (int i
= start_idx
; i
< end_idx
; ++i
)
21693 /* Create a distinct parameter pack type from the current parm and add it
21694 to the replacement args to tsubst below into the generic function
21697 tree o
= TREE_TYPE (TREE_VALUE
21698 (TREE_VEC_ELT (current
, i
)));
21699 tree t
= copy_type (o
);
21700 TEMPLATE_TYPE_PARM_INDEX (t
)
21701 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21703 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21704 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21705 TYPE_MAIN_VARIANT (t
) = t
;
21706 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21707 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21708 TREE_VEC_ELT (replacement
, i
) = t
;
21709 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21712 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21713 TREE_VEC_ELT (replacement
, i
)
21714 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21716 /* If there are more levels then build up the replacement with the outer
21719 replacement
= add_to_template_args (template_parms_to_args
21720 (TREE_CHAIN (current_template_parms
)),
21723 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21727 /* Set up the hash tables for template instantiations. */
21730 init_template_processing (void)
21732 decl_specializations
= htab_create_ggc (37,
21733 hash_specialization
,
21734 eq_specializations
,
21736 type_specializations
= htab_create_ggc (37,
21737 hash_specialization
,
21738 eq_specializations
,
21742 /* Print stats about the template hash tables for -fstats. */
21745 print_template_statistics (void)
21747 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21748 "%f collisions\n", (long) htab_size (decl_specializations
),
21749 (long) htab_elements (decl_specializations
),
21750 htab_collisions (decl_specializations
));
21751 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21752 "%f collisions\n", (long) htab_size (type_specializations
),
21753 (long) htab_elements (type_specializations
),
21754 htab_collisions (type_specializations
));
21757 #include "gt-cp-pt.h"