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 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3859 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3860 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3861 TREE_TYPE (t
) = type
;
3867 /* Given a set of template parameters, return them as a set of template
3868 arguments. The template parameters are represented as a TREE_VEC, in
3869 the form documented in cp-tree.h for template arguments. */
3872 template_parms_to_args (tree parms
)
3875 tree args
= NULL_TREE
;
3876 int length
= TMPL_PARMS_DEPTH (parms
);
3879 /* If there is only one level of template parameters, we do not
3880 create a TREE_VEC of TREE_VECs. Instead, we return a single
3881 TREE_VEC containing the arguments. */
3883 args
= make_tree_vec (length
);
3885 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3887 tree a
= copy_node (TREE_VALUE (header
));
3890 TREE_TYPE (a
) = NULL_TREE
;
3891 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3892 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3894 #ifdef ENABLE_CHECKING
3895 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3899 TREE_VEC_ELT (args
, --l
) = a
;
3904 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3905 /* This can happen for template parms of a template template
3908 template<template<class T, class U> class TT> struct S;
3910 Consider the level of the parms of TT; T and U both have
3911 level 2; TT has no template parm of level 1. So in this case
3912 the first element of full_template_args is NULL_TREE. If we
3913 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3914 of 2. This will make tsubst wrongly consider that T and U
3915 have level 1. Instead, let's create a dummy vector as the
3916 first element of full_template_args so that TMPL_ARGS_DEPTH
3917 returns the correct depth for args. */
3918 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3922 /* Within the declaration of a template, return the currently active
3923 template parameters as an argument TREE_VEC. */
3926 current_template_args (void)
3928 return template_parms_to_args (current_template_parms
);
3931 /* Update the declared TYPE by doing any lookups which were thought to be
3932 dependent, but are not now that we know the SCOPE of the declarator. */
3935 maybe_update_decl_type (tree orig_type
, tree scope
)
3937 tree type
= orig_type
;
3939 if (type
== NULL_TREE
)
3942 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3943 type
= TREE_TYPE (type
);
3945 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3946 && dependent_type_p (type
)
3947 /* Don't bother building up the args in this case. */
3948 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3950 /* tsubst in the args corresponding to the template parameters,
3951 including auto if present. Most things will be unchanged, but
3952 make_typename_type and tsubst_qualified_id will resolve
3953 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3954 tree args
= current_template_args ();
3955 tree auto_node
= type_uses_auto (type
);
3959 tree auto_vec
= make_tree_vec (1);
3960 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3961 args
= add_to_template_args (args
, auto_vec
);
3963 pushed
= push_scope (scope
);
3964 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3969 if (type
== error_mark_node
)
3972 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3974 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3977 type
= TYPE_NAME (type
);
3982 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3983 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3984 a member template. Used by push_template_decl below. */
3987 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3989 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3990 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3991 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3992 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3993 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3998 struct template_parm_data
4000 /* The level of the template parameters we are currently
4004 /* The index of the specialization argument we are currently
4008 /* An array whose size is the number of template parameters. The
4009 elements are nonzero if the parameter has been used in any one
4010 of the arguments processed so far. */
4013 /* An array whose size is the number of template arguments. The
4014 elements are nonzero if the argument makes use of template
4015 parameters of this level. */
4016 int* arg_uses_template_parms
;
4019 /* Subroutine of push_template_decl used to see if each template
4020 parameter in a partial specialization is used in the explicit
4021 argument list. If T is of the LEVEL given in DATA (which is
4022 treated as a template_parm_data*), then DATA->PARMS is marked
4026 mark_template_parm (tree t
, void* data
)
4030 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4032 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4034 level
= TEMPLATE_PARM_LEVEL (t
);
4035 idx
= TEMPLATE_PARM_IDX (t
);
4039 level
= TEMPLATE_TYPE_LEVEL (t
);
4040 idx
= TEMPLATE_TYPE_IDX (t
);
4043 if (level
== tpd
->level
)
4045 tpd
->parms
[idx
] = 1;
4046 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4049 /* Return zero so that for_each_template_parm will continue the
4050 traversal of the tree; we want to mark *every* template parm. */
4054 /* Process the partial specialization DECL. */
4057 process_partial_specialization (tree decl
)
4059 tree type
= TREE_TYPE (decl
);
4060 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4061 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4062 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4063 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4066 int nargs
= TREE_VEC_LENGTH (inner_args
);
4069 bool did_error_intro
= false;
4070 struct template_parm_data tpd
;
4071 struct template_parm_data tpd2
;
4073 gcc_assert (current_template_parms
);
4075 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4076 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4078 /* We check that each of the template parameters given in the
4079 partial specialization is used in the argument list to the
4080 specialization. For example:
4082 template <class T> struct S;
4083 template <class T> struct S<T*>;
4085 The second declaration is OK because `T*' uses the template
4086 parameter T, whereas
4088 template <class T> struct S<int>;
4090 is no good. Even trickier is:
4101 The S2<T> declaration is actually invalid; it is a
4102 full-specialization. Of course,
4105 struct S2<T (*)(U)>;
4107 or some such would have been OK. */
4108 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4109 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4110 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4112 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4113 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4114 for (i
= 0; i
< nargs
; ++i
)
4116 tpd
.current_arg
= i
;
4117 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4118 &mark_template_parm
,
4121 /*include_nondeduced_p=*/false);
4123 for (i
= 0; i
< ntparms
; ++i
)
4124 if (tpd
.parms
[i
] == 0)
4126 /* One of the template parms was not used in the
4128 if (!did_error_intro
)
4130 error ("template parameters not used in partial specialization:");
4131 did_error_intro
= true;
4134 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4137 if (did_error_intro
)
4138 return error_mark_node
;
4140 /* [temp.class.spec]
4142 The argument list of the specialization shall not be identical to
4143 the implicit argument list of the primary template. */
4144 if (comp_template_args
4146 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4148 error ("partial specialization %qT does not specialize any template arguments", type
);
4150 /* A partial specialization that replaces multiple parameters of the
4151 primary template with a pack expansion is less specialized for those
4153 if (nargs
< DECL_NTPARMS (maintmpl
))
4155 error ("partial specialization is not more specialized than the "
4156 "primary template because it replaces multiple parameters "
4157 "with a pack expansion");
4158 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4162 /* [temp.class.spec]
4164 A partially specialized non-type argument expression shall not
4165 involve template parameters of the partial specialization except
4166 when the argument expression is a simple identifier.
4168 The type of a template parameter corresponding to a specialized
4169 non-type argument shall not be dependent on a parameter of the
4172 Also, we verify that pack expansions only occur at the
4173 end of the argument list. */
4174 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4176 for (i
= 0; i
< nargs
; ++i
)
4178 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4179 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4180 tree packed_args
= NULL_TREE
;
4183 if (ARGUMENT_PACK_P (arg
))
4185 /* Extract the arguments from the argument pack. We'll be
4186 iterating over these in the following loop. */
4187 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4188 len
= TREE_VEC_LENGTH (packed_args
);
4191 for (j
= 0; j
< len
; j
++)
4194 /* Get the Jth argument in the parameter pack. */
4195 arg
= TREE_VEC_ELT (packed_args
, j
);
4197 if (PACK_EXPANSION_P (arg
))
4199 /* Pack expansions must come at the end of the
4201 if ((packed_args
&& j
< len
- 1)
4202 || (!packed_args
&& i
< nargs
- 1))
4204 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4205 error ("parameter pack argument %qE must be at the "
4206 "end of the template argument list", arg
);
4208 error ("parameter pack argument %qT must be at the "
4209 "end of the template argument list", arg
);
4213 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4214 /* We only care about the pattern. */
4215 arg
= PACK_EXPANSION_PATTERN (arg
);
4217 if (/* These first two lines are the `non-type' bit. */
4219 && TREE_CODE (arg
) != TEMPLATE_DECL
4220 /* This next line is the `argument expression is not just a
4221 simple identifier' condition and also the `specialized
4222 non-type argument' bit. */
4223 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4225 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4226 || (packed_args
&& uses_template_parms (arg
)))
4227 error ("template argument %qE involves template parameter(s)",
4231 /* Look at the corresponding template parameter,
4232 marking which template parameters its type depends
4234 tree type
= TREE_TYPE (parm
);
4238 /* We haven't yet initialized TPD2. Do so now. */
4239 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4240 /* The number of parameters here is the number in the
4241 main template, which, as checked in the assertion
4243 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4245 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4248 /* Mark the template parameters. But this time, we're
4249 looking for the template parameters of the main
4250 template, not in the specialization. */
4251 tpd2
.current_arg
= i
;
4252 tpd2
.arg_uses_template_parms
[i
] = 0;
4253 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4254 for_each_template_parm (type
,
4255 &mark_template_parm
,
4258 /*include_nondeduced_p=*/false);
4260 if (tpd2
.arg_uses_template_parms
[i
])
4262 /* The type depended on some template parameters.
4263 If they are fully specialized in the
4264 specialization, that's OK. */
4267 for (j
= 0; j
< nargs
; ++j
)
4268 if (tpd2
.parms
[j
] != 0
4269 && tpd
.arg_uses_template_parms
[j
])
4272 error_n (input_location
, count
,
4273 "type %qT of template argument %qE depends "
4274 "on a template parameter",
4275 "type %qT of template argument %qE depends "
4276 "on template parameters",
4285 /* We should only get here once. */
4286 gcc_assert (!COMPLETE_TYPE_P (type
));
4288 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4289 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4290 TREE_TYPE (tmpl
) = type
;
4291 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4292 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4293 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4294 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4296 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4297 = tree_cons (specargs
, tmpl
,
4298 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4299 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4301 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4302 inst
= TREE_CHAIN (inst
))
4304 tree inst_type
= TREE_VALUE (inst
);
4305 if (COMPLETE_TYPE_P (inst_type
)
4306 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4308 tree spec
= most_specialized_class (inst_type
, tf_none
);
4309 if (spec
&& TREE_TYPE (spec
) == type
)
4310 permerror (input_location
,
4311 "partial specialization of %qT after instantiation "
4312 "of %qT", type
, inst_type
);
4319 /* Check that a template declaration's use of default arguments and
4320 parameter packs is not invalid. Here, PARMS are the template
4321 parameters. IS_PRIMARY is true if DECL is the thing declared by
4322 a primary template. IS_PARTIAL is true if DECL is a partial
4325 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4326 declaration (but not a definition); 1 indicates a declaration, 2
4327 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4328 emitted for extraneous default arguments.
4330 Returns TRUE if there were no errors found, FALSE otherwise. */
4333 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4334 bool is_partial
, int is_friend_decl
)
4337 int last_level_to_check
;
4339 bool no_errors
= true;
4343 A default template-argument shall not be specified in a
4344 function template declaration or a function template definition, nor
4345 in the template-parameter-list of the definition of a member of a
4348 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4349 /* You can't have a function template declaration in a local
4350 scope, nor you can you define a member of a class template in a
4354 if (TREE_CODE (decl
) == TYPE_DECL
4356 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4357 /* A lambda doesn't have an explicit declaration; don't complain
4358 about the parms of the enclosing class. */
4361 if (current_class_type
4362 && !TYPE_BEING_DEFINED (current_class_type
)
4363 && DECL_LANG_SPECIFIC (decl
)
4364 && DECL_DECLARES_FUNCTION_P (decl
)
4365 /* If this is either a friend defined in the scope of the class
4366 or a member function. */
4367 && (DECL_FUNCTION_MEMBER_P (decl
)
4368 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4369 : DECL_FRIEND_CONTEXT (decl
)
4370 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4372 /* And, if it was a member function, it really was defined in
4373 the scope of the class. */
4374 && (!DECL_FUNCTION_MEMBER_P (decl
)
4375 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4376 /* We already checked these parameters when the template was
4377 declared, so there's no need to do it again now. This function
4378 was defined in class scope, but we're processing its body now
4379 that the class is complete. */
4382 /* Core issue 226 (C++0x only): the following only applies to class
4385 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4389 If a template-parameter has a default template-argument, all
4390 subsequent template-parameters shall have a default
4391 template-argument supplied. */
4392 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4394 tree inner_parms
= TREE_VALUE (parm_level
);
4395 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4396 int seen_def_arg_p
= 0;
4399 for (i
= 0; i
< ntparms
; ++i
)
4401 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4403 if (parm
== error_mark_node
)
4406 if (TREE_PURPOSE (parm
))
4408 else if (seen_def_arg_p
4409 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4411 error ("no default argument for %qD", TREE_VALUE (parm
));
4412 /* For better subsequent error-recovery, we indicate that
4413 there should have been a default argument. */
4414 TREE_PURPOSE (parm
) = error_mark_node
;
4417 else if (!is_partial
4419 /* Don't complain about an enclosing partial
4421 && parm_level
== parms
4422 && TREE_CODE (decl
) == TYPE_DECL
4424 && template_parameter_pack_p (TREE_VALUE (parm
)))
4426 /* A primary class template can only have one
4427 parameter pack, at the end of the template
4430 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4431 error ("parameter pack %qE must be at the end of the"
4432 " template parameter list", TREE_VALUE (parm
));
4434 error ("parameter pack %qT must be at the end of the"
4435 " template parameter list",
4436 TREE_TYPE (TREE_VALUE (parm
)));
4438 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4446 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4450 /* For an ordinary class template, default template arguments are
4451 allowed at the innermost level, e.g.:
4452 template <class T = int>
4454 but, in a partial specialization, they're not allowed even
4455 there, as we have in [temp.class.spec]:
4457 The template parameter list of a specialization shall not
4458 contain default template argument values.
4460 So, for a partial specialization, or for a function template
4461 (in C++98/C++03), we look at all of them. */
4464 /* But, for a primary class template that is not a partial
4465 specialization we look at all template parameters except the
4467 parms
= TREE_CHAIN (parms
);
4469 /* Figure out what error message to issue. */
4470 if (is_friend_decl
== 2)
4471 msg
= G_("default template arguments may not be used in function template "
4472 "friend re-declaration");
4473 else if (is_friend_decl
)
4474 msg
= G_("default template arguments may not be used in function template "
4475 "friend declarations");
4476 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4477 msg
= G_("default template arguments may not be used in function templates "
4478 "without -std=c++11 or -std=gnu++11");
4479 else if (is_partial
)
4480 msg
= G_("default template arguments may not be used in "
4481 "partial specializations");
4483 msg
= G_("default argument for template parameter for class enclosing %qD");
4485 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4486 /* If we're inside a class definition, there's no need to
4487 examine the parameters to the class itself. On the one
4488 hand, they will be checked when the class is defined, and,
4489 on the other, default arguments are valid in things like:
4490 template <class T = double>
4491 struct S { template <class U> void f(U); };
4492 Here the default argument for `S' has no bearing on the
4493 declaration of `f'. */
4494 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4496 /* Check everything. */
4497 last_level_to_check
= 0;
4499 for (parm_level
= parms
;
4500 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4501 parm_level
= TREE_CHAIN (parm_level
))
4503 tree inner_parms
= TREE_VALUE (parm_level
);
4507 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4508 for (i
= 0; i
< ntparms
; ++i
)
4510 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4513 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4518 if (is_friend_decl
== 2)
4525 /* Clear out the default argument so that we are not
4527 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4531 /* At this point, if we're still interested in issuing messages,
4532 they must apply to classes surrounding the object declared. */
4534 msg
= G_("default argument for template parameter for class "
4541 /* Worker for push_template_decl_real, called via
4542 for_each_template_parm. DATA is really an int, indicating the
4543 level of the parameters we are interested in. If T is a template
4544 parameter of that level, return nonzero. */
4547 template_parm_this_level_p (tree t
, void* data
)
4549 int this_level
= *(int *)data
;
4552 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4553 level
= TEMPLATE_PARM_LEVEL (t
);
4555 level
= TEMPLATE_TYPE_LEVEL (t
);
4556 return level
== this_level
;
4559 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4560 parameters given by current_template_args, or reuses a
4561 previously existing one, if appropriate. Returns the DECL, or an
4562 equivalent one, if it is replaced via a call to duplicate_decls.
4564 If IS_FRIEND is true, DECL is a friend declaration. */
4567 push_template_decl_real (tree decl
, bool is_friend
)
4575 int new_template_p
= 0;
4576 /* True if the template is a member template, in the sense of
4578 bool member_template_p
= false;
4580 if (decl
== error_mark_node
|| !current_template_parms
)
4581 return error_mark_node
;
4583 /* See if this is a partial specialization. */
4584 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4585 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4586 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4588 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4592 /* For a friend, we want the context of the friend function, not
4593 the type of which it is a friend. */
4594 ctx
= CP_DECL_CONTEXT (decl
);
4595 else if (CP_DECL_CONTEXT (decl
)
4596 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4597 /* In the case of a virtual function, we want the class in which
4599 ctx
= CP_DECL_CONTEXT (decl
);
4601 /* Otherwise, if we're currently defining some class, the DECL
4602 is assumed to be a member of the class. */
4603 ctx
= current_scope ();
4605 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4608 if (!DECL_CONTEXT (decl
))
4609 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4611 /* See if this is a primary template. */
4612 if (is_friend
&& ctx
)
4613 /* A friend template that specifies a class context, i.e.
4614 template <typename T> friend void A<T>::f();
4618 is_primary
= template_parm_scope_p ();
4622 if (DECL_CLASS_SCOPE_P (decl
))
4623 member_template_p
= true;
4624 if (TREE_CODE (decl
) == TYPE_DECL
4625 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4627 error ("template class without a name");
4628 return error_mark_node
;
4630 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4632 if (DECL_DESTRUCTOR_P (decl
))
4636 A destructor shall not be a member template. */
4637 error ("destructor %qD declared as member template", decl
);
4638 return error_mark_node
;
4640 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4641 && (!prototype_p (TREE_TYPE (decl
))
4642 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4643 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4644 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4645 == void_list_node
)))
4647 /* [basic.stc.dynamic.allocation]
4649 An allocation function can be a function
4650 template. ... Template allocation functions shall
4651 have two or more parameters. */
4652 error ("invalid template declaration of %qD", decl
);
4653 return error_mark_node
;
4656 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4657 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4659 else if (TREE_CODE (decl
) == TYPE_DECL
4660 && TYPE_DECL_ALIAS_P (decl
))
4661 /* alias-declaration */
4662 gcc_assert (!DECL_ARTIFICIAL (decl
));
4665 error ("template declaration of %q#D", decl
);
4666 return error_mark_node
;
4670 /* Check to see that the rules regarding the use of default
4671 arguments are not being violated. */
4672 check_default_tmpl_args (decl
, current_template_parms
,
4673 is_primary
, is_partial
, /*is_friend_decl=*/0);
4675 /* Ensure that there are no parameter packs in the type of this
4676 declaration that have not been expanded. */
4677 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4679 /* Check each of the arguments individually to see if there are
4680 any bare parameter packs. */
4681 tree type
= TREE_TYPE (decl
);
4682 tree arg
= DECL_ARGUMENTS (decl
);
4683 tree argtype
= TYPE_ARG_TYPES (type
);
4685 while (arg
&& argtype
)
4687 if (!DECL_PACK_P (arg
)
4688 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4690 /* This is a PARM_DECL that contains unexpanded parameter
4691 packs. We have already complained about this in the
4692 check_for_bare_parameter_packs call, so just replace
4693 these types with ERROR_MARK_NODE. */
4694 TREE_TYPE (arg
) = error_mark_node
;
4695 TREE_VALUE (argtype
) = error_mark_node
;
4698 arg
= DECL_CHAIN (arg
);
4699 argtype
= TREE_CHAIN (argtype
);
4702 /* Check for bare parameter packs in the return type and the
4703 exception specifiers. */
4704 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4705 /* Errors were already issued, set return type to int
4706 as the frontend doesn't expect error_mark_node as
4708 TREE_TYPE (type
) = integer_type_node
;
4709 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4710 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4712 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4713 && TYPE_DECL_ALIAS_P (decl
))
4714 ? DECL_ORIGINAL_TYPE (decl
)
4715 : TREE_TYPE (decl
)))
4717 TREE_TYPE (decl
) = error_mark_node
;
4718 return error_mark_node
;
4722 return process_partial_specialization (decl
);
4724 args
= current_template_args ();
4727 || TREE_CODE (ctx
) == FUNCTION_DECL
4728 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4729 || (TREE_CODE (decl
) == TYPE_DECL
4730 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4731 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4733 if (DECL_LANG_SPECIFIC (decl
)
4734 && DECL_TEMPLATE_INFO (decl
)
4735 && DECL_TI_TEMPLATE (decl
))
4736 tmpl
= DECL_TI_TEMPLATE (decl
);
4737 /* If DECL is a TYPE_DECL for a class-template, then there won't
4738 be DECL_LANG_SPECIFIC. The information equivalent to
4739 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4740 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4741 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4742 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4744 /* Since a template declaration already existed for this
4745 class-type, we must be redeclaring it here. Make sure
4746 that the redeclaration is valid. */
4747 redeclare_class_template (TREE_TYPE (decl
),
4748 current_template_parms
);
4749 /* We don't need to create a new TEMPLATE_DECL; just use the
4750 one we already had. */
4751 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4755 tmpl
= build_template_decl (decl
, current_template_parms
,
4759 if (DECL_LANG_SPECIFIC (decl
)
4760 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4762 /* A specialization of a member template of a template
4764 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4765 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4766 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4772 tree a
, t
, current
, parms
;
4774 tree tinfo
= get_template_info (decl
);
4778 error ("template definition of non-template %q#D", decl
);
4779 return error_mark_node
;
4782 tmpl
= TI_TEMPLATE (tinfo
);
4784 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4785 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4786 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4787 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4791 /* The declaration is a specialization of a member
4792 template, declared outside the class. Therefore, the
4793 innermost template arguments will be NULL, so we
4794 replace them with the arguments determined by the
4795 earlier call to check_explicit_specialization. */
4796 args
= DECL_TI_ARGS (decl
);
4799 = build_template_decl (decl
, current_template_parms
,
4801 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4802 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4803 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4804 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4805 DECL_TEMPLATE_INFO (new_tmpl
)
4806 = build_template_info (tmpl
, args
);
4808 register_specialization (new_tmpl
,
4809 most_general_template (tmpl
),
4815 /* Make sure the template headers we got make sense. */
4817 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4818 i
= TMPL_PARMS_DEPTH (parms
);
4819 if (TMPL_ARGS_DEPTH (args
) != i
)
4821 error ("expected %d levels of template parms for %q#D, got %d",
4822 i
, decl
, TMPL_ARGS_DEPTH (args
));
4825 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4827 a
= TMPL_ARGS_LEVEL (args
, i
);
4828 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4830 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4832 if (current
== decl
)
4833 error ("got %d template parameters for %q#D",
4834 TREE_VEC_LENGTH (a
), decl
);
4836 error ("got %d template parameters for %q#T",
4837 TREE_VEC_LENGTH (a
), current
);
4838 error (" but %d required", TREE_VEC_LENGTH (t
));
4839 /* Avoid crash in import_export_decl. */
4840 DECL_INTERFACE_KNOWN (decl
) = 1;
4841 return error_mark_node
;
4844 if (current
== decl
)
4846 else if (current
== NULL_TREE
)
4847 /* Can happen in erroneous input. */
4850 current
= get_containing_scope (current
);
4853 /* Check that the parms are used in the appropriate qualifying scopes
4854 in the declarator. */
4855 if (!comp_template_args
4857 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4860 template arguments to %qD do not match original template %qD",
4861 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4862 if (!uses_template_parms (TI_ARGS (tinfo
)))
4863 inform (input_location
, "use template<> for an explicit specialization");
4864 /* Avoid crash in import_export_decl. */
4865 DECL_INTERFACE_KNOWN (decl
) = 1;
4866 return error_mark_node
;
4870 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4871 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4873 /* Push template declarations for global functions and types. Note
4874 that we do not try to push a global template friend declared in a
4875 template class; such a thing may well depend on the template
4876 parameters of the class. */
4877 if (new_template_p
&& !ctx
4878 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4880 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4881 if (tmpl
== error_mark_node
)
4882 return error_mark_node
;
4884 /* Hide template friend classes that haven't been declared yet. */
4885 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4887 DECL_ANTICIPATED (tmpl
) = 1;
4888 DECL_FRIEND_P (tmpl
) = 1;
4894 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4897 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4898 if (DECL_CONV_FN_P (tmpl
))
4900 int depth
= TMPL_PARMS_DEPTH (parms
);
4902 /* It is a conversion operator. See if the type converted to
4903 depends on innermost template operands. */
4905 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4907 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4910 /* Give template template parms a DECL_CONTEXT of the template
4911 for which they are a parameter. */
4912 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4913 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4915 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4916 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4917 DECL_CONTEXT (parm
) = tmpl
;
4921 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4922 back to its most general template. If TMPL is a specialization,
4923 ARGS may only have the innermost set of arguments. Add the missing
4924 argument levels if necessary. */
4925 if (DECL_TEMPLATE_INFO (tmpl
))
4926 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4928 info
= build_template_info (tmpl
, args
);
4930 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4931 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4934 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4935 retrofit_lang_decl (decl
);
4936 if (DECL_LANG_SPECIFIC (decl
))
4937 DECL_TEMPLATE_INFO (decl
) = info
;
4940 return DECL_TEMPLATE_RESULT (tmpl
);
4944 push_template_decl (tree decl
)
4946 return push_template_decl_real (decl
, false);
4949 /* FN is an inheriting constructor that inherits from the constructor
4950 template INHERITED; turn FN into a constructor template with a matching
4954 add_inherited_template_parms (tree fn
, tree inherited
)
4957 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4958 inner_parms
= copy_node (inner_parms
);
4960 = tree_cons (size_int (processing_template_decl
+ 1),
4961 inner_parms
, current_template_parms
);
4962 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4963 tree args
= template_parms_to_args (parms
);
4964 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4965 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4966 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4967 DECL_ARTIFICIAL (tmpl
) = true;
4968 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4972 /* Called when a class template TYPE is redeclared with the indicated
4973 template PARMS, e.g.:
4975 template <class T> struct S;
4976 template <class T> struct S {}; */
4979 redeclare_class_template (tree type
, tree parms
)
4985 if (!TYPE_TEMPLATE_INFO (type
))
4987 error ("%qT is not a template type", type
);
4991 tmpl
= TYPE_TI_TEMPLATE (type
);
4992 if (!PRIMARY_TEMPLATE_P (tmpl
))
4993 /* The type is nested in some template class. Nothing to worry
4994 about here; there are no new template parameters for the nested
5000 error ("template specifiers not specified in declaration of %qD",
5005 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5006 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5008 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5010 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5011 "redeclared with %d template parameter",
5012 "redeclared with %d template parameters",
5013 TREE_VEC_LENGTH (parms
));
5014 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5015 "previous declaration %q+D used %d template parameter",
5016 "previous declaration %q+D used %d template parameters",
5017 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5021 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5028 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5029 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5032 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5033 if (tmpl_parm
== error_mark_node
)
5036 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5037 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5038 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5040 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5042 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5043 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5044 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5045 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5046 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5047 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5048 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5049 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5050 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5052 error ("template parameter %q+#D", tmpl_parm
);
5053 error ("redeclared here as %q#D", parm
);
5057 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5059 /* We have in [temp.param]:
5061 A template-parameter may not be given default arguments
5062 by two different declarations in the same scope. */
5063 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5064 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5065 "original definition appeared here");
5069 if (parm_default
!= NULL_TREE
)
5070 /* Update the previous template parameters (which are the ones
5071 that will really count) with the new default value. */
5072 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5073 else if (tmpl_default
!= NULL_TREE
)
5074 /* Update the new parameters, too; they'll be used as the
5075 parameters for any members. */
5076 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5082 /* Simplify EXPR if it is a non-dependent expression. Returns the
5083 (possibly simplified) expression. */
5086 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5088 if (expr
== NULL_TREE
)
5091 /* If we're in a template, but EXPR isn't value dependent, simplify
5092 it. We're supposed to treat:
5094 template <typename T> void f(T[1 + 1]);
5095 template <typename T> void f(T[2]);
5097 as two declarations of the same function, for example. */
5098 if (processing_template_decl
5099 && !instantiation_dependent_expression_p (expr
)
5100 && potential_constant_expression (expr
))
5102 HOST_WIDE_INT saved_processing_template_decl
;
5104 saved_processing_template_decl
= processing_template_decl
;
5105 processing_template_decl
= 0;
5106 expr
= tsubst_copy_and_build (expr
,
5109 /*in_decl=*/NULL_TREE
,
5110 /*function_p=*/false,
5111 /*integral_constant_expression_p=*/true);
5112 processing_template_decl
= saved_processing_template_decl
;
5118 fold_non_dependent_expr (tree expr
)
5120 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5123 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5124 template declaration, or a TYPE_DECL for an alias declaration. */
5127 alias_type_or_template_p (tree t
)
5131 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5134 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5135 || DECL_ALIAS_TEMPLATE_P (t
));
5138 /* Return TRUE iff is a specialization of an alias template. */
5141 alias_template_specialization_p (const_tree t
)
5147 && TYPE_TEMPLATE_INFO (t
)
5148 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5149 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5152 /* Return the number of innermost template parameters in TMPL. */
5155 num_innermost_template_parms (tree tmpl
)
5157 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5158 return TREE_VEC_LENGTH (parms
);
5161 /* Return either TMPL or another template that it is equivalent to under DR
5162 1286: An alias that just changes the name of a template is equivalent to
5163 the other template. */
5166 get_underlying_template (tree tmpl
)
5168 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5169 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5171 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5172 if (TYPE_TEMPLATE_INFO (result
))
5174 tree sub
= TYPE_TI_TEMPLATE (result
);
5175 if (PRIMARY_TEMPLATE_P (sub
)
5176 && (num_innermost_template_parms (tmpl
)
5177 == num_innermost_template_parms (sub
))
5178 && same_type_p (result
, TREE_TYPE (sub
)))
5180 /* The alias type is equivalent to the pattern of the
5181 underlying template, so strip the alias. */
5191 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5192 must be a function or a pointer-to-function type, as specified
5193 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5194 and check that the resulting function has external linkage. */
5197 convert_nontype_argument_function (tree type
, tree expr
)
5201 linkage_kind linkage
;
5203 fn
= instantiate_type (type
, fns
, tf_none
);
5204 if (fn
== error_mark_node
)
5205 return error_mark_node
;
5208 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5209 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5210 if (BASELINK_P (fn_no_ptr
))
5211 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5213 /* [temp.arg.nontype]/1
5215 A template-argument for a non-type, non-template template-parameter
5218 -- the address of an object or function with external [C++11: or
5219 internal] linkage. */
5221 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5223 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5224 if (TYPE_PTR_P (type
))
5225 error ("it must be the address of a function with external linkage");
5227 error ("it must be the name of a function with external linkage");
5231 linkage
= decl_linkage (fn_no_ptr
);
5232 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5234 if (cxx_dialect
>= cxx11
)
5235 error ("%qE is not a valid template argument for type %qT "
5236 "because %qD has no linkage",
5237 expr
, type
, fn_no_ptr
);
5239 error ("%qE is not a valid template argument for type %qT "
5240 "because %qD does not have external linkage",
5241 expr
, type
, fn_no_ptr
);
5248 /* Subroutine of convert_nontype_argument.
5249 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5250 Emit an error otherwise. */
5253 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5254 tsubst_flags_t complain
)
5257 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5259 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5261 if (complain
& tf_error
)
5263 error ("%qE is not a valid template argument for type %qT",
5265 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5270 /* Returns TRUE iff the address of OP is value-dependent.
5272 14.6.2.4 [temp.dep.temp]:
5273 A non-integral non-type template-argument is dependent if its type is
5274 dependent or it has either of the following forms
5277 and contains a nested-name-specifier which specifies a class-name that
5278 names a dependent type.
5280 We generalize this to just say that the address of a member of a
5281 dependent class is value-dependent; the above doesn't cover the
5282 address of a static data member named with an unqualified-id. */
5285 has_value_dependent_address (tree op
)
5287 /* We could use get_inner_reference here, but there's no need;
5288 this is only relevant for template non-type arguments, which
5289 can only be expressed as &id-expression. */
5292 tree ctx
= CP_DECL_CONTEXT (op
);
5293 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5300 /* The next set of functions are used for providing helpful explanatory
5301 diagnostics for failed overload resolution. Their messages should be
5302 indented by two spaces for consistency with the messages in
5306 unify_success (bool /*explain_p*/)
5312 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5315 inform (input_location
,
5316 " couldn't deduce template parameter %qD", parm
);
5321 unify_invalid (bool /*explain_p*/)
5327 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5330 inform (input_location
,
5331 " types %qT and %qT have incompatible cv-qualifiers",
5337 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5340 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5345 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5348 inform (input_location
,
5349 " template parameter %qD is not a parameter pack, but "
5356 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5359 inform (input_location
,
5360 " template argument %qE does not match "
5361 "pointer-to-member constant %qE",
5367 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5370 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5375 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5378 inform (input_location
,
5379 " inconsistent parameter pack deduction with %qT and %qT",
5385 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5390 inform (input_location
,
5391 " deduced conflicting types for parameter %qT (%qT and %qT)",
5392 parm
, first
, second
);
5394 inform (input_location
,
5395 " deduced conflicting values for non-type parameter "
5396 "%qE (%qE and %qE)", parm
, first
, second
);
5402 unify_vla_arg (bool explain_p
, tree arg
)
5405 inform (input_location
,
5406 " variable-sized array type %qT is not "
5407 "a valid template argument",
5413 unify_method_type_error (bool explain_p
, tree arg
)
5416 inform (input_location
,
5417 " member function type %qT is not a valid template argument",
5423 unify_arity (bool explain_p
, int have
, int wanted
)
5426 inform_n (input_location
, wanted
,
5427 " candidate expects %d argument, %d provided",
5428 " candidate expects %d arguments, %d provided",
5434 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5436 return unify_arity (explain_p
, have
, wanted
);
5440 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5442 return unify_arity (explain_p
, have
, wanted
);
5446 unify_arg_conversion (bool explain_p
, tree to_type
,
5447 tree from_type
, tree arg
)
5450 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5451 " cannot convert %qE (type %qT) to type %qT",
5452 arg
, from_type
, to_type
);
5457 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5458 tree parm
, tree arg
)
5463 case tbr_ambiguous_baseclass
:
5464 inform (input_location
, " %qT is an ambiguous base class of %qT",
5468 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5475 unify_inconsistent_template_template_parameters (bool explain_p
)
5478 inform (input_location
,
5479 " template parameters of a template template argument are "
5480 "inconsistent with other deduced template arguments");
5485 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5488 inform (input_location
,
5489 " can't deduce a template for %qT from non-template type %qT",
5495 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5498 inform (input_location
,
5499 " template argument %qE does not match %qD", arg
, parm
);
5504 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5507 inform (input_location
,
5508 " could not resolve address from overloaded function %qE",
5513 /* Attempt to convert the non-type template parameter EXPR to the
5514 indicated TYPE. If the conversion is successful, return the
5515 converted value. If the conversion is unsuccessful, return
5516 NULL_TREE if we issued an error message, or error_mark_node if we
5517 did not. We issue error messages for out-and-out bad template
5518 parameters, but not simply because the conversion failed, since we
5519 might be just trying to do argument deduction. Both TYPE and EXPR
5520 must be non-dependent.
5522 The conversion follows the special rules described in
5523 [temp.arg.nontype], and it is much more strict than an implicit
5526 This function is called twice for each template argument (see
5527 lookup_template_class for a more accurate description of this
5528 problem). This means that we need to handle expressions which
5529 are not valid in a C++ source, but can be created from the
5530 first call (for instance, casts to perform conversions). These
5531 hacks can go away after we fix the double coercion problem. */
5534 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5538 /* Detect immediately string literals as invalid non-type argument.
5539 This special-case is not needed for correctness (we would easily
5540 catch this later), but only to provide better diagnostic for this
5541 common user mistake. As suggested by DR 100, we do not mention
5542 linkage issues in the diagnostic as this is not the point. */
5543 /* FIXME we're making this OK. */
5544 if (TREE_CODE (expr
) == STRING_CST
)
5546 if (complain
& tf_error
)
5547 error ("%qE is not a valid template argument for type %qT "
5548 "because string literals can never be used in this context",
5553 /* Add the ADDR_EXPR now for the benefit of
5554 value_dependent_expression_p. */
5555 if (TYPE_PTROBV_P (type
)
5556 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5558 expr
= decay_conversion (expr
, complain
);
5559 if (expr
== error_mark_node
)
5560 return error_mark_node
;
5563 /* If we are in a template, EXPR may be non-dependent, but still
5564 have a syntactic, rather than semantic, form. For example, EXPR
5565 might be a SCOPE_REF, rather than the VAR_DECL to which the
5566 SCOPE_REF refers. Preserving the qualifying scope is necessary
5567 so that access checking can be performed when the template is
5568 instantiated -- but here we need the resolved form so that we can
5569 convert the argument. */
5570 if (TYPE_REF_OBJ_P (type
)
5571 && has_value_dependent_address (expr
))
5572 /* If we want the address and it's value-dependent, don't fold. */;
5573 else if (!type_unknown_p (expr
))
5574 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5575 if (error_operand_p (expr
))
5576 return error_mark_node
;
5577 expr_type
= TREE_TYPE (expr
);
5578 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5579 expr
= mark_lvalue_use (expr
);
5581 expr
= mark_rvalue_use (expr
);
5583 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5584 to a non-type argument of "nullptr". */
5585 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5586 expr
= convert (type
, expr
);
5588 /* In C++11, integral or enumeration non-type template arguments can be
5589 arbitrary constant expressions. Pointer and pointer to
5590 member arguments can be general constant expressions that evaluate
5591 to a null value, but otherwise still need to be of a specific form. */
5592 if (cxx_dialect
>= cxx11
)
5594 if (TREE_CODE (expr
) == PTRMEM_CST
)
5595 /* A PTRMEM_CST is already constant, and a valid template
5596 argument for a parameter of pointer to member type, we just want
5597 to leave it in that form rather than lower it to a
5599 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5600 expr
= maybe_constant_value (expr
);
5601 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5603 tree folded
= maybe_constant_value (expr
);
5604 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5605 : null_member_pointer_value_p (folded
))
5610 /* HACK: Due to double coercion, we can get a
5611 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5612 which is the tree that we built on the first call (see
5613 below when coercing to reference to object or to reference to
5614 function). We just strip everything and get to the arg.
5615 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5617 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5619 tree probe_type
, probe
= expr
;
5620 if (REFERENCE_REF_P (probe
))
5621 probe
= TREE_OPERAND (probe
, 0);
5622 probe_type
= TREE_TYPE (probe
);
5623 if (TREE_CODE (probe
) == NOP_EXPR
)
5625 /* ??? Maybe we could use convert_from_reference here, but we
5626 would need to relax its constraints because the NOP_EXPR
5627 could actually change the type to something more cv-qualified,
5628 and this is not folded by convert_from_reference. */
5629 tree addr
= TREE_OPERAND (probe
, 0);
5630 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5631 && TREE_CODE (addr
) == ADDR_EXPR
5632 && TYPE_PTR_P (TREE_TYPE (addr
))
5633 && (same_type_ignoring_top_level_qualifiers_p
5634 (TREE_TYPE (probe_type
),
5635 TREE_TYPE (TREE_TYPE (addr
)))))
5637 expr
= TREE_OPERAND (addr
, 0);
5638 expr_type
= TREE_TYPE (probe_type
);
5643 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5644 parameter is a pointer to object, through decay and
5645 qualification conversion. Let's strip everything. */
5646 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5650 if (TREE_CODE (probe
) == ADDR_EXPR
5651 && TYPE_PTR_P (TREE_TYPE (probe
)))
5653 /* Skip the ADDR_EXPR only if it is part of the decay for
5654 an array. Otherwise, it is part of the original argument
5655 in the source code. */
5656 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5657 probe
= TREE_OPERAND (probe
, 0);
5659 expr_type
= TREE_TYPE (expr
);
5663 /* [temp.arg.nontype]/5, bullet 1
5665 For a non-type template-parameter of integral or enumeration type,
5666 integral promotions (_conv.prom_) and integral conversions
5667 (_conv.integral_) are applied. */
5668 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5670 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5671 t
= maybe_constant_value (t
);
5672 if (t
!= error_mark_node
)
5675 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5676 return error_mark_node
;
5678 /* Notice that there are constant expressions like '4 % 0' which
5679 do not fold into integer constants. */
5680 if (TREE_CODE (expr
) != INTEGER_CST
)
5682 if (complain
& tf_error
)
5684 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5685 if (processing_template_decl
5686 && !require_potential_constant_expression (expr
))
5688 expr
= cxx_constant_value (expr
);
5689 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5690 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5691 "in template argument for type %qT ", type
);
5692 if (expr
== error_mark_node
)
5694 /* else cxx_constant_value complained but gave us
5695 a real constant, so go ahead. */
5696 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5702 /* Avoid typedef problems. */
5703 if (TREE_TYPE (expr
) != type
)
5704 expr
= fold_convert (type
, expr
);
5706 /* [temp.arg.nontype]/5, bullet 2
5708 For a non-type template-parameter of type pointer to object,
5709 qualification conversions (_conv.qual_) and the array-to-pointer
5710 conversion (_conv.array_) are applied. */
5711 else if (TYPE_PTROBV_P (type
))
5713 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5715 A template-argument for a non-type, non-template template-parameter
5716 shall be one of: [...]
5718 -- the name of a non-type template-parameter;
5719 -- the address of an object or function with external linkage, [...]
5720 expressed as "& id-expression" where the & is optional if the name
5721 refers to a function or array, or if the corresponding
5722 template-parameter is a reference.
5724 Here, we do not care about functions, as they are invalid anyway
5725 for a parameter of type pointer-to-object. */
5727 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5728 /* Non-type template parameters are OK. */
5730 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5731 /* Null pointer values are OK in C++11. */;
5732 else if (TREE_CODE (expr
) != ADDR_EXPR
5733 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5737 error ("%qD is not a valid template argument "
5738 "because %qD is a variable, not the address of "
5743 if (POINTER_TYPE_P (expr_type
))
5745 error ("%qE is not a valid template argument for %qT "
5746 "because it is not the address of a variable",
5750 /* Other values, like integer constants, might be valid
5751 non-type arguments of some other type. */
5752 return error_mark_node
;
5758 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5759 ? TREE_OPERAND (expr
, 0) : expr
);
5762 error ("%qE is not a valid template argument of type %qT "
5763 "because %qE is not a variable",
5767 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5769 error ("%qE is not a valid template argument of type %qT "
5770 "because %qD does not have external linkage",
5774 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5776 error ("%qE is not a valid template argument of type %qT "
5777 "because %qD has no linkage",
5783 expr
= decay_conversion (expr
, complain
);
5784 if (expr
== error_mark_node
)
5785 return error_mark_node
;
5787 expr
= perform_qualification_conversions (type
, expr
);
5788 if (expr
== error_mark_node
)
5789 return error_mark_node
;
5791 /* [temp.arg.nontype]/5, bullet 3
5793 For a non-type template-parameter of type reference to object, no
5794 conversions apply. The type referred to by the reference may be more
5795 cv-qualified than the (otherwise identical) type of the
5796 template-argument. The template-parameter is bound directly to the
5797 template-argument, which must be an lvalue. */
5798 else if (TYPE_REF_OBJ_P (type
))
5800 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5802 return error_mark_node
;
5804 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5806 error ("%qE is not a valid template argument for type %qT "
5807 "because of conflicts in cv-qualification", expr
, type
);
5811 if (!real_lvalue_p (expr
))
5813 error ("%qE is not a valid template argument for type %qT "
5814 "because it is not an lvalue", expr
, type
);
5818 /* [temp.arg.nontype]/1
5820 A template-argument for a non-type, non-template template-parameter
5821 shall be one of: [...]
5823 -- the address of an object or function with external linkage. */
5824 if (INDIRECT_REF_P (expr
)
5825 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5827 expr
= TREE_OPERAND (expr
, 0);
5830 error ("%q#D is not a valid template argument for type %qT "
5831 "because a reference variable does not have a constant "
5832 "address", expr
, type
);
5839 error ("%qE is not a valid template argument for type %qT "
5840 "because it is not an object with external linkage",
5845 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5847 error ("%qE is not a valid template argument for type %qT "
5848 "because object %qD has not external linkage",
5853 expr
= build_nop (type
, build_address (expr
));
5855 /* [temp.arg.nontype]/5, bullet 4
5857 For a non-type template-parameter of type pointer to function, only
5858 the function-to-pointer conversion (_conv.func_) is applied. If the
5859 template-argument represents a set of overloaded functions (or a
5860 pointer to such), the matching function is selected from the set
5862 else if (TYPE_PTRFN_P (type
))
5864 /* If the argument is a template-id, we might not have enough
5865 context information to decay the pointer. */
5866 if (!type_unknown_p (expr_type
))
5868 expr
= decay_conversion (expr
, complain
);
5869 if (expr
== error_mark_node
)
5870 return error_mark_node
;
5873 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5874 /* Null pointer values are OK in C++11. */
5875 return perform_qualification_conversions (type
, expr
);
5877 expr
= convert_nontype_argument_function (type
, expr
);
5878 if (!expr
|| expr
== error_mark_node
)
5881 /* [temp.arg.nontype]/5, bullet 5
5883 For a non-type template-parameter of type reference to function, no
5884 conversions apply. If the template-argument represents a set of
5885 overloaded functions, the matching function is selected from the set
5887 else if (TYPE_REFFN_P (type
))
5889 if (TREE_CODE (expr
) == ADDR_EXPR
)
5891 error ("%qE is not a valid template argument for type %qT "
5892 "because it is a pointer", expr
, type
);
5893 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5897 expr
= convert_nontype_argument_function (type
, expr
);
5898 if (!expr
|| expr
== error_mark_node
)
5901 expr
= build_nop (type
, build_address (expr
));
5903 /* [temp.arg.nontype]/5, bullet 6
5905 For a non-type template-parameter of type pointer to member function,
5906 no conversions apply. If the template-argument represents a set of
5907 overloaded member functions, the matching member function is selected
5908 from the set (_over.over_). */
5909 else if (TYPE_PTRMEMFUNC_P (type
))
5911 expr
= instantiate_type (type
, expr
, tf_none
);
5912 if (expr
== error_mark_node
)
5913 return error_mark_node
;
5915 /* [temp.arg.nontype] bullet 1 says the pointer to member
5916 expression must be a pointer-to-member constant. */
5917 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5918 return error_mark_node
;
5920 /* There is no way to disable standard conversions in
5921 resolve_address_of_overloaded_function (called by
5922 instantiate_type). It is possible that the call succeeded by
5923 converting &B::I to &D::I (where B is a base of D), so we need
5924 to reject this conversion here.
5926 Actually, even if there was a way to disable standard conversions,
5927 it would still be better to reject them here so that we can
5928 provide a superior diagnostic. */
5929 if (!same_type_p (TREE_TYPE (expr
), type
))
5931 error ("%qE is not a valid template argument for type %qT "
5932 "because it is of type %qT", expr
, type
,
5934 /* If we are just one standard conversion off, explain. */
5935 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5936 inform (input_location
,
5937 "standard conversions are not allowed in this context");
5941 /* [temp.arg.nontype]/5, bullet 7
5943 For a non-type template-parameter of type pointer to data member,
5944 qualification conversions (_conv.qual_) are applied. */
5945 else if (TYPE_PTRDATAMEM_P (type
))
5947 /* [temp.arg.nontype] bullet 1 says the pointer to member
5948 expression must be a pointer-to-member constant. */
5949 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5950 return error_mark_node
;
5952 expr
= perform_qualification_conversions (type
, expr
);
5953 if (expr
== error_mark_node
)
5956 else if (NULLPTR_TYPE_P (type
))
5958 if (expr
!= nullptr_node
)
5960 error ("%qE is not a valid template argument for type %qT "
5961 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5966 /* A template non-type parameter must be one of the above. */
5970 /* Sanity check: did we actually convert the argument to the
5972 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5973 (type
, TREE_TYPE (expr
)));
5977 /* Subroutine of coerce_template_template_parms, which returns 1 if
5978 PARM_PARM and ARG_PARM match using the rule for the template
5979 parameters of template template parameters. Both PARM and ARG are
5980 template parameters; the rest of the arguments are the same as for
5981 coerce_template_template_parms.
5984 coerce_template_template_parm (tree parm
,
5986 tsubst_flags_t complain
,
5990 if (arg
== NULL_TREE
|| arg
== error_mark_node
5991 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5994 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5997 switch (TREE_CODE (parm
))
6000 /* We encounter instantiations of templates like
6001 template <template <template <class> class> class TT>
6004 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6005 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6007 if (!coerce_template_template_parms
6008 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6014 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6015 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6016 /* Argument is a parameter pack but parameter is not. */
6021 /* The tsubst call is used to handle cases such as
6023 template <int> class C {};
6024 template <class T, template <T> class TT> class D {};
6027 i.e. the parameter list of TT depends on earlier parameters. */
6028 if (!uses_template_parms (TREE_TYPE (arg
))
6030 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6034 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6035 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6036 /* Argument is a parameter pack but parameter is not. */
6049 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6050 template template parameters. Both PARM_PARMS and ARG_PARMS are
6051 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6054 Consider the example:
6055 template <class T> class A;
6056 template<template <class U> class TT> class B;
6058 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6059 the parameters to A, and OUTER_ARGS contains A. */
6062 coerce_template_template_parms (tree parm_parms
,
6064 tsubst_flags_t complain
,
6068 int nparms
, nargs
, i
;
6072 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6073 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6075 nparms
= TREE_VEC_LENGTH (parm_parms
);
6076 nargs
= TREE_VEC_LENGTH (arg_parms
);
6078 /* Determine whether we have a parameter pack at the end of the
6079 template template parameter's template parameter list. */
6080 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6082 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6084 if (parm
== error_mark_node
)
6087 switch (TREE_CODE (parm
))
6091 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6096 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6106 && !(variadic_p
&& nargs
>= nparms
- 1))
6109 /* Check all of the template parameters except the parameter pack at
6110 the end (if any). */
6111 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6113 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6114 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6117 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6118 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6120 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6128 /* Check each of the template parameters in the template
6129 argument against the template parameter pack at the end of
6130 the template template parameter. */
6131 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6134 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6136 for (; i
< nargs
; ++i
)
6138 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6141 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6143 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6152 /* Verifies that the deduced template arguments (in TARGS) for the
6153 template template parameters (in TPARMS) represent valid bindings,
6154 by comparing the template parameter list of each template argument
6155 to the template parameter list of its corresponding template
6156 template parameter, in accordance with DR150. This
6157 routine can only be called after all template arguments have been
6158 deduced. It will return TRUE if all of the template template
6159 parameter bindings are okay, FALSE otherwise. */
6161 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6163 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6166 /* We're dealing with template parms in this process. */
6167 ++processing_template_decl
;
6169 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6171 for (i
= 0; i
< ntparms
; ++i
)
6173 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6174 tree targ
= TREE_VEC_ELT (targs
, i
);
6176 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6178 tree packed_args
= NULL_TREE
;
6181 if (ARGUMENT_PACK_P (targ
))
6183 /* Look inside the argument pack. */
6184 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6185 len
= TREE_VEC_LENGTH (packed_args
);
6188 for (idx
= 0; idx
< len
; ++idx
)
6190 tree targ_parms
= NULL_TREE
;
6193 /* Extract the next argument from the argument
6195 targ
= TREE_VEC_ELT (packed_args
, idx
);
6197 if (PACK_EXPANSION_P (targ
))
6198 /* Look at the pattern of the pack expansion. */
6199 targ
= PACK_EXPANSION_PATTERN (targ
);
6201 /* Extract the template parameters from the template
6203 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6204 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6205 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6206 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6208 /* Verify that we can coerce the template template
6209 parameters from the template argument to the template
6210 parameter. This requires an exact match. */
6212 && !coerce_template_template_parms
6213 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6228 --processing_template_decl
;
6232 /* Since type attributes aren't mangled, we need to strip them from
6233 template type arguments. */
6236 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6239 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6241 mv
= TYPE_MAIN_VARIANT (arg
);
6242 arg
= strip_typedefs (arg
);
6243 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6244 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6246 if (complain
& tf_warning
)
6247 warning (0, "ignoring attributes on template argument %qT", arg
);
6248 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6249 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6254 /* Convert the indicated template ARG as necessary to match the
6255 indicated template PARM. Returns the converted ARG, or
6256 error_mark_node if the conversion was unsuccessful. Error and
6257 warning messages are issued under control of COMPLAIN. This
6258 conversion is for the Ith parameter in the parameter list. ARGS is
6259 the full set of template arguments deduced so far. */
6262 convert_template_argument (tree parm
,
6265 tsubst_flags_t complain
,
6271 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6273 if (TREE_CODE (arg
) == TREE_LIST
6274 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6276 /* The template argument was the name of some
6277 member function. That's usually
6278 invalid, but static members are OK. In any
6279 case, grab the underlying fields/functions
6280 and issue an error later if required. */
6281 orig_arg
= TREE_VALUE (arg
);
6282 TREE_TYPE (arg
) = unknown_type_node
;
6287 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6288 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6289 || requires_tmpl_type
);
6291 /* When determining whether an argument pack expansion is a template,
6292 look at the pattern. */
6293 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6294 arg
= PACK_EXPANSION_PATTERN (arg
);
6296 /* Deal with an injected-class-name used as a template template arg. */
6297 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6299 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6300 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6302 if (cxx_dialect
>= cxx11
)
6303 /* OK under DR 1004. */;
6304 else if (complain
& tf_warning_or_error
)
6305 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6306 " used as template template argument", TYPE_NAME (arg
));
6307 else if (flag_pedantic_errors
)
6315 ((TREE_CODE (arg
) == TEMPLATE_DECL
6316 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6317 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6318 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6319 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6322 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6323 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6324 arg
= TYPE_STUB_DECL (arg
);
6326 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6328 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6329 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6331 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6333 if (complain
& tf_error
)
6334 error ("invalid use of destructor %qE as a type", orig_arg
);
6335 return error_mark_node
;
6338 permerror (input_location
,
6339 "to refer to a type member of a template parameter, "
6340 "use %<typename %E%>", orig_arg
);
6342 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6343 TREE_OPERAND (arg
, 1),
6349 if (is_type
!= requires_type
)
6353 if (complain
& tf_error
)
6355 error ("type/value mismatch at argument %d in template "
6356 "parameter list for %qD",
6359 error (" expected a constant of type %qT, got %qT",
6361 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6362 else if (requires_tmpl_type
)
6363 error (" expected a class template, got %qE", orig_arg
);
6365 error (" expected a type, got %qE", orig_arg
);
6368 return error_mark_node
;
6370 if (is_tmpl_type
^ requires_tmpl_type
)
6372 if (in_decl
&& (complain
& tf_error
))
6374 error ("type/value mismatch at argument %d in template "
6375 "parameter list for %qD",
6378 error (" expected a type, got %qT", DECL_NAME (arg
));
6380 error (" expected a class template, got %qT", orig_arg
);
6382 return error_mark_node
;
6387 if (requires_tmpl_type
)
6389 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6391 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6392 /* The number of argument required is not known yet.
6393 Just accept it for now. */
6394 val
= TREE_TYPE (arg
);
6397 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6400 /* Strip alias templates that are equivalent to another
6402 arg
= get_underlying_template (arg
);
6403 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6405 if (coerce_template_template_parms (parmparm
, argparm
,
6411 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6413 if (val
!= error_mark_node
)
6415 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6416 val
= TREE_TYPE (val
);
6417 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6418 val
= make_pack_expansion (val
);
6423 if (in_decl
&& (complain
& tf_error
))
6425 error ("type/value mismatch at argument %d in "
6426 "template parameter list for %qD",
6428 error (" expected a template of type %qD, got %qT",
6432 val
= error_mark_node
;
6438 /* We only form one instance of each template specialization.
6439 Therefore, if we use a non-canonical variant (i.e., a
6440 typedef), any future messages referring to the type will use
6441 the typedef, which is confusing if those future uses do not
6442 themselves also use the typedef. */
6444 val
= canonicalize_type_argument (val
, complain
);
6448 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6450 if (invalid_nontype_parm_type_p (t
, complain
))
6451 return error_mark_node
;
6453 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6455 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6459 /* Not sure if this is reachable, but it doesn't hurt
6461 error ("type mismatch in nontype parameter pack");
6462 val
= error_mark_node
;
6465 else if (!dependent_template_arg_p (orig_arg
)
6466 && !uses_template_parms (t
))
6467 /* We used to call digest_init here. However, digest_init
6468 will report errors, which we don't want when complain
6469 is zero. More importantly, digest_init will try too
6470 hard to convert things: for example, `0' should not be
6471 converted to pointer type at this point according to
6472 the standard. Accepting this is not merely an
6473 extension, since deciding whether or not these
6474 conversions can occur is part of determining which
6475 function template to call, or whether a given explicit
6476 argument specification is valid. */
6477 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6479 val
= strip_typedefs_expr (orig_arg
);
6481 if (val
== NULL_TREE
)
6482 val
= error_mark_node
;
6483 else if (val
== error_mark_node
&& (complain
& tf_error
))
6484 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6486 if (TREE_CODE (val
) == SCOPE_REF
)
6488 /* Strip typedefs from the SCOPE_REF. */
6489 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6490 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6492 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6493 QUALIFIED_NAME_IS_TEMPLATE (val
));
6500 /* Coerces the remaining template arguments in INNER_ARGS (from
6501 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6502 Returns the coerced argument pack. PARM_IDX is the position of this
6503 parameter in the template parameter list. ARGS is the original
6504 template argument list. */
6506 coerce_template_parameter_pack (tree parms
,
6514 tsubst_flags_t complain
)
6516 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6517 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6520 tree packed_types
= NULL_TREE
;
6522 if (arg_idx
> nargs
)
6525 packed_args
= make_tree_vec (nargs
- arg_idx
);
6527 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6528 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6530 /* When the template parameter is a non-type template
6531 parameter pack whose type uses parameter packs, we need
6532 to look at each of the template arguments
6533 separately. Build a vector of the types for these
6534 non-type template parameters in PACKED_TYPES. */
6536 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6537 packed_types
= tsubst_pack_expansion (expansion
, args
,
6540 if (packed_types
== error_mark_node
)
6541 return error_mark_node
;
6543 /* Check that we have the right number of arguments. */
6545 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6546 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6549 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6550 error ("wrong number of template arguments (%d, should be %d)",
6551 nargs
, needed_parms
);
6552 return error_mark_node
;
6555 /* If we aren't able to check the actual arguments now
6556 (because they haven't been expanded yet), we can at least
6557 verify that all of the types used for the non-type
6558 template parameter pack are, in fact, valid for non-type
6559 template parameters. */
6561 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6563 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6564 for (j
= 0; j
< len
; ++j
)
6566 tree t
= TREE_VEC_ELT (packed_types
, j
);
6567 if (invalid_nontype_parm_type_p (t
, complain
))
6568 return error_mark_node
;
6573 /* Convert the remaining arguments, which will be a part of the
6574 parameter pack "parm". */
6575 for (; arg_idx
< nargs
; ++arg_idx
)
6577 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6578 tree actual_parm
= TREE_VALUE (parm
);
6580 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6582 /* When we have a vector of types (corresponding to the
6583 non-type template parameter pack that uses parameter
6584 packs in its type, as mention above), and the
6585 argument is not an expansion (which expands to a
6586 currently unknown number of arguments), clone the
6587 parm and give it the next type in PACKED_TYPES. */
6588 actual_parm
= copy_node (actual_parm
);
6589 TREE_TYPE (actual_parm
) =
6590 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6593 if (arg
== error_mark_node
)
6595 if (complain
& tf_error
)
6596 error ("template argument %d is invalid", arg_idx
+ 1);
6599 arg
= convert_template_argument (actual_parm
,
6600 arg
, new_args
, complain
, parm_idx
,
6602 if (arg
== error_mark_node
)
6604 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6607 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6608 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6609 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6612 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6613 TREE_TYPE (argument_pack
)
6614 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6615 TREE_CONSTANT (argument_pack
) = 1;
6618 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6619 #ifdef ENABLE_CHECKING
6620 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6621 TREE_VEC_LENGTH (packed_args
));
6623 return argument_pack
;
6626 /* Returns the number of pack expansions in the template argument vector
6630 pack_expansion_args_count (tree args
)
6635 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6637 tree elt
= TREE_VEC_ELT (args
, i
);
6638 if (elt
&& PACK_EXPANSION_P (elt
))
6644 /* Convert all template arguments to their appropriate types, and
6645 return a vector containing the innermost resulting template
6646 arguments. If any error occurs, return error_mark_node. Error and
6647 warning messages are issued under control of COMPLAIN.
6649 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6650 for arguments not specified in ARGS. Otherwise, if
6651 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6652 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6653 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6657 coerce_template_parms (tree parms
,
6660 tsubst_flags_t complain
,
6661 bool require_all_args
,
6662 bool use_default_args
)
6664 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6667 tree new_inner_args
;
6668 int saved_unevaluated_operand
;
6669 int saved_inhibit_evaluation_warnings
;
6671 /* When used as a boolean value, indicates whether this is a
6672 variadic template parameter list. Since it's an int, we can also
6673 subtract it from nparms to get the number of non-variadic
6676 int variadic_args_p
= 0;
6677 int post_variadic_parms
= 0;
6679 if (args
== error_mark_node
)
6680 return error_mark_node
;
6682 nparms
= TREE_VEC_LENGTH (parms
);
6684 /* Determine if there are any parameter packs. */
6685 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6687 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6689 ++post_variadic_parms
;
6690 if (template_parameter_pack_p (tparm
))
6694 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6695 /* If there are no parameters that follow a parameter pack, we need to
6696 expand any argument packs so that we can deduce a parameter pack from
6697 some non-packed args followed by an argument pack, as in variadic85.C.
6698 If there are such parameters, we need to leave argument packs intact
6699 so the arguments are assigned properly. This can happen when dealing
6700 with a nested class inside a partial specialization of a class
6701 template, as in variadic92.C, or when deducing a template parameter pack
6702 from a sub-declarator, as in variadic114.C. */
6703 if (!post_variadic_parms
)
6704 inner_args
= expand_template_argument_pack (inner_args
);
6706 /* Count any pack expansion args. */
6707 variadic_args_p
= pack_expansion_args_count (inner_args
);
6709 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6710 if ((nargs
> nparms
&& !variadic_p
)
6711 || (nargs
< nparms
- variadic_p
6714 && (!use_default_args
6715 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6716 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6718 if (complain
& tf_error
)
6722 nparms
-= variadic_p
;
6723 error ("wrong number of template arguments "
6724 "(%d, should be %d or more)", nargs
, nparms
);
6727 error ("wrong number of template arguments "
6728 "(%d, should be %d)", nargs
, nparms
);
6731 error ("provided for %q+D", in_decl
);
6734 return error_mark_node
;
6736 /* We can't pass a pack expansion to a non-pack parameter of an alias
6737 template (DR 1430). */
6738 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6740 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6742 if (complain
& tf_error
)
6744 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6746 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6747 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6749 if (PACK_EXPANSION_P (arg
)
6750 && !template_parameter_pack_p (parm
))
6752 error ("pack expansion argument for non-pack parameter "
6753 "%qD of alias template %qD", parm
, in_decl
);
6754 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6761 return error_mark_node
;
6764 /* We need to evaluate the template arguments, even though this
6765 template-id may be nested within a "sizeof". */
6766 saved_unevaluated_operand
= cp_unevaluated_operand
;
6767 cp_unevaluated_operand
= 0;
6768 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6769 c_inhibit_evaluation_warnings
= 0;
6770 new_inner_args
= make_tree_vec (nparms
);
6771 new_args
= add_outermost_template_args (args
, new_inner_args
);
6772 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6777 /* Get the Ith template parameter. */
6778 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6780 if (parm
== error_mark_node
)
6782 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6786 /* Calculate the next argument. */
6787 if (arg_idx
< nargs
)
6788 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6792 if (template_parameter_pack_p (TREE_VALUE (parm
))
6793 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6795 /* All remaining arguments will be placed in the
6796 template parameter pack PARM. */
6797 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6798 inner_args
, arg_idx
,
6802 /* Store this argument. */
6803 if (arg
== error_mark_node
)
6805 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6807 /* We are done with all of the arguments. */
6814 if (PACK_EXPANSION_P (arg
))
6816 /* We don't know how many args we have yet, just
6817 use the unconverted ones for now. */
6818 new_inner_args
= inner_args
;
6822 else if (require_all_args
)
6824 /* There must be a default arg in this case. */
6825 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6827 /* The position of the first default template argument,
6828 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6830 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6831 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6836 if (arg
== error_mark_node
)
6838 if (complain
& tf_error
)
6839 error ("template argument %d is invalid", arg_idx
+ 1);
6842 /* This only occurs if there was an error in the template
6843 parameter list itself (which we would already have
6844 reported) that we are trying to recover from, e.g., a class
6845 template with a parameter list such as
6846 template<typename..., typename>. */
6849 arg
= convert_template_argument (TREE_VALUE (parm
),
6850 arg
, new_args
, complain
,
6853 if (arg
== error_mark_node
)
6855 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6857 cp_unevaluated_operand
= saved_unevaluated_operand
;
6858 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6861 return error_mark_node
;
6863 #ifdef ENABLE_CHECKING
6864 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6866 TREE_VEC_LENGTH (new_inner_args
));
6869 return new_inner_args
;
6872 /* Like coerce_template_parms. If PARMS represents all template
6873 parameters levels, this function returns a vector of vectors
6874 representing all the resulting argument levels. Note that in this
6875 case, only the innermost arguments are coerced because the
6876 outermost ones are supposed to have been coerced already.
6878 Otherwise, if PARMS represents only (the innermost) vector of
6879 parameters, this function returns a vector containing just the
6880 innermost resulting arguments. */
6883 coerce_innermost_template_parms (tree parms
,
6886 tsubst_flags_t complain
,
6887 bool require_all_args
,
6888 bool use_default_args
)
6890 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6891 int args_depth
= TMPL_ARGS_DEPTH (args
);
6894 if (parms_depth
> 1)
6896 coerced_args
= make_tree_vec (parms_depth
);
6900 for (level
= parms
, cur_depth
= parms_depth
;
6901 parms_depth
> 0 && level
!= NULL_TREE
;
6902 level
= TREE_CHAIN (level
), --cur_depth
)
6905 if (cur_depth
== args_depth
)
6906 l
= coerce_template_parms (TREE_VALUE (level
),
6907 args
, in_decl
, complain
,
6911 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6913 if (l
== error_mark_node
)
6914 return error_mark_node
;
6916 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6920 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6921 args
, in_decl
, complain
,
6924 return coerced_args
;
6927 /* Returns 1 if template args OT and NT are equivalent. */
6930 template_args_equal (tree ot
, tree nt
)
6934 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6937 if (TREE_CODE (nt
) == TREE_VEC
)
6938 /* For member templates */
6939 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6940 else if (PACK_EXPANSION_P (ot
))
6941 return (PACK_EXPANSION_P (nt
)
6942 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6943 PACK_EXPANSION_PATTERN (nt
))
6944 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6945 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6946 else if (ARGUMENT_PACK_P (ot
))
6951 if (!ARGUMENT_PACK_P (nt
))
6954 opack
= ARGUMENT_PACK_ARGS (ot
);
6955 npack
= ARGUMENT_PACK_ARGS (nt
);
6956 len
= TREE_VEC_LENGTH (opack
);
6957 if (TREE_VEC_LENGTH (npack
) != len
)
6959 for (i
= 0; i
< len
; ++i
)
6960 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6961 TREE_VEC_ELT (npack
, i
)))
6965 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6967 /* We get here probably because we are in the middle of substituting
6968 into the pattern of a pack expansion. In that case the
6969 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6970 interested in. So we want to use the initial pack argument for
6972 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6973 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6974 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6975 return template_args_equal (ot
, nt
);
6977 else if (TYPE_P (nt
))
6978 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6979 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6982 return cp_tree_equal (ot
, nt
);
6985 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6986 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6987 NEWARG_PTR with the offending arguments if they are non-NULL. */
6990 comp_template_args_with_info (tree oldargs
, tree newargs
,
6991 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6995 if (oldargs
== newargs
)
6998 if (!oldargs
|| !newargs
)
7001 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7004 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7006 tree nt
= TREE_VEC_ELT (newargs
, i
);
7007 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7009 if (! template_args_equal (ot
, nt
))
7011 if (oldarg_ptr
!= NULL
)
7013 if (newarg_ptr
!= NULL
)
7021 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7022 of template arguments. Returns 0 otherwise. */
7025 comp_template_args (tree oldargs
, tree newargs
)
7027 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7031 add_pending_template (tree d
)
7033 tree ti
= (TYPE_P (d
)
7034 ? CLASSTYPE_TEMPLATE_INFO (d
)
7035 : DECL_TEMPLATE_INFO (d
));
7036 struct pending_template
*pt
;
7039 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7042 /* We are called both from instantiate_decl, where we've already had a
7043 tinst_level pushed, and instantiate_template, where we haven't.
7045 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7048 push_tinst_level (d
);
7050 pt
= ggc_alloc_pending_template ();
7052 pt
->tinst
= current_tinst_level
;
7053 if (last_pending_template
)
7054 last_pending_template
->next
= pt
;
7056 pending_templates
= pt
;
7058 last_pending_template
= pt
;
7060 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7067 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7068 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7069 documentation for TEMPLATE_ID_EXPR. */
7072 lookup_template_function (tree fns
, tree arglist
)
7076 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7077 return error_mark_node
;
7079 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7081 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7083 error ("%q#D is not a function template", fns
);
7084 return error_mark_node
;
7087 if (BASELINK_P (fns
))
7089 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7091 BASELINK_FUNCTIONS (fns
),
7096 type
= TREE_TYPE (fns
);
7097 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7098 type
= unknown_type_node
;
7100 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7103 /* Within the scope of a template class S<T>, the name S gets bound
7104 (in build_self_reference) to a TYPE_DECL for the class, not a
7105 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7106 or one of its enclosing classes, and that type is a template,
7107 return the associated TEMPLATE_DECL. Otherwise, the original
7110 Also handle the case when DECL is a TREE_LIST of ambiguous
7111 injected-class-names from different bases. */
7114 maybe_get_template_decl_from_type_decl (tree decl
)
7116 if (decl
== NULL_TREE
)
7119 /* DR 176: A lookup that finds an injected-class-name (10.2
7120 [class.member.lookup]) can result in an ambiguity in certain cases
7121 (for example, if it is found in more than one base class). If all of
7122 the injected-class-names that are found refer to specializations of
7123 the same class template, and if the name is followed by a
7124 template-argument-list, the reference refers to the class template
7125 itself and not a specialization thereof, and is not ambiguous. */
7126 if (TREE_CODE (decl
) == TREE_LIST
)
7128 tree t
, tmpl
= NULL_TREE
;
7129 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7131 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7134 else if (tmpl
!= elt
)
7137 if (tmpl
&& t
== NULL_TREE
)
7143 return (decl
!= NULL_TREE
7144 && DECL_SELF_REFERENCE_P (decl
)
7145 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7146 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7149 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7150 parameters, find the desired type.
7152 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7154 IN_DECL, if non-NULL, is the template declaration we are trying to
7157 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7158 the class we are looking up.
7160 Issue error and warning messages under control of COMPLAIN.
7162 If the template class is really a local class in a template
7163 function, then the FUNCTION_CONTEXT is the function in which it is
7166 ??? Note that this function is currently called *twice* for each
7167 template-id: the first time from the parser, while creating the
7168 incomplete type (finish_template_type), and the second type during the
7169 real instantiation (instantiate_template_class). This is surely something
7170 that we want to avoid. It also causes some problems with argument
7171 coercion (see convert_nontype_argument for more information on this). */
7174 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7175 int entering_scope
, tsubst_flags_t complain
)
7177 tree templ
= NULL_TREE
, parmlist
;
7184 if (identifier_p (d1
))
7186 tree value
= innermost_non_namespace_value (d1
);
7187 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7192 push_decl_namespace (context
);
7193 templ
= lookup_name (d1
);
7194 templ
= maybe_get_template_decl_from_type_decl (templ
);
7196 pop_decl_namespace ();
7199 context
= DECL_CONTEXT (templ
);
7201 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7203 tree type
= TREE_TYPE (d1
);
7205 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7206 an implicit typename for the second A. Deal with it. */
7207 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7208 type
= TREE_TYPE (type
);
7210 if (CLASSTYPE_TEMPLATE_INFO (type
))
7212 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7213 d1
= DECL_NAME (templ
);
7216 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7217 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7219 templ
= TYPE_TI_TEMPLATE (d1
);
7220 d1
= DECL_NAME (templ
);
7222 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7223 && DECL_TEMPLATE_RESULT (d1
)
7224 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7227 d1
= DECL_NAME (templ
);
7228 context
= DECL_CONTEXT (templ
);
7230 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7233 d1
= DECL_NAME (templ
);
7236 /* Issue an error message if we didn't find a template. */
7239 if (complain
& tf_error
)
7240 error ("%qT is not a template", d1
);
7241 return error_mark_node
;
7244 if (TREE_CODE (templ
) != TEMPLATE_DECL
7245 /* Make sure it's a user visible template, if it was named by
7247 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7248 && !PRIMARY_TEMPLATE_P (templ
)))
7250 if (complain
& tf_error
)
7252 error ("non-template type %qT used as a template", d1
);
7254 error ("for template declaration %q+D", in_decl
);
7256 return error_mark_node
;
7259 complain
&= ~tf_user
;
7261 /* An alias that just changes the name of a template is equivalent to the
7262 other template, so if any of the arguments are pack expansions, strip
7263 the alias to avoid problems with a pack expansion passed to a non-pack
7264 alias template parameter (DR 1430). */
7265 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7266 templ
= get_underlying_template (templ
);
7268 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7270 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7271 template arguments */
7277 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7279 /* Consider an example where a template template parameter declared as
7281 template <class T, class U = std::allocator<T> > class TT
7283 The template parameter level of T and U are one level larger than
7284 of TT. To proper process the default argument of U, say when an
7285 instantiation `TT<int>' is seen, we need to build the full
7286 arguments containing {int} as the innermost level. Outer levels,
7287 available when not appearing as default template argument, can be
7288 obtained from the arguments of the enclosing template.
7290 Suppose that TT is later substituted with std::vector. The above
7291 instantiation is `TT<int, std::allocator<T> >' with TT at
7292 level 1, and T at level 2, while the template arguments at level 1
7293 becomes {std::vector} and the inner level 2 is {int}. */
7295 outer
= DECL_CONTEXT (templ
);
7297 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7298 else if (current_template_parms
)
7299 /* This is an argument of the current template, so we haven't set
7300 DECL_CONTEXT yet. */
7301 outer
= current_template_args ();
7304 arglist
= add_to_template_args (outer
, arglist
);
7306 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7308 /*require_all_args=*/true,
7309 /*use_default_args=*/true);
7310 if (arglist2
== error_mark_node
7311 || (!uses_template_parms (arglist2
)
7312 && check_instantiated_args (templ
, arglist2
, complain
)))
7313 return error_mark_node
;
7315 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7320 tree template_type
= TREE_TYPE (templ
);
7323 tree found
= NULL_TREE
;
7326 int is_dependent_type
;
7327 int use_partial_inst_tmpl
= false;
7329 if (template_type
== error_mark_node
)
7330 /* An error occurred while building the template TEMPL, and a
7331 diagnostic has most certainly been emitted for that
7332 already. Let's propagate that error. */
7333 return error_mark_node
;
7335 gen_tmpl
= most_general_template (templ
);
7336 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7337 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7338 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7340 if (arg_depth
== 1 && parm_depth
> 1)
7342 /* We've been given an incomplete set of template arguments.
7345 template <class T> struct S1 {
7346 template <class U> struct S2 {};
7347 template <class U> struct S2<U*> {};
7350 we will be called with an ARGLIST of `U*', but the
7351 TEMPLATE will be `template <class T> template
7352 <class U> struct S1<T>::S2'. We must fill in the missing
7355 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7357 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7360 /* Now we should have enough arguments. */
7361 gcc_assert (parm_depth
== arg_depth
);
7363 /* From here on, we're only interested in the most general
7366 /* Calculate the BOUND_ARGS. These will be the args that are
7367 actually tsubst'd into the definition to create the
7371 /* We have multiple levels of arguments to coerce, at once. */
7373 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7375 tree bound_args
= make_tree_vec (parm_depth
);
7377 for (i
= saved_depth
,
7378 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7379 i
> 0 && t
!= NULL_TREE
;
7380 --i
, t
= TREE_CHAIN (t
))
7383 if (i
== saved_depth
)
7384 a
= coerce_template_parms (TREE_VALUE (t
),
7387 /*require_all_args=*/true,
7388 /*use_default_args=*/true);
7390 /* Outer levels should have already been coerced. */
7391 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7393 /* Don't process further if one of the levels fails. */
7394 if (a
== error_mark_node
)
7396 /* Restore the ARGLIST to its full size. */
7397 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7398 return error_mark_node
;
7401 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7403 /* We temporarily reduce the length of the ARGLIST so
7404 that coerce_template_parms will see only the arguments
7405 corresponding to the template parameters it is
7407 TREE_VEC_LENGTH (arglist
)--;
7410 /* Restore the ARGLIST to its full size. */
7411 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7413 arglist
= bound_args
;
7417 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7418 INNERMOST_TEMPLATE_ARGS (arglist
),
7421 /*require_all_args=*/true,
7422 /*use_default_args=*/true);
7424 if (arglist
== error_mark_node
)
7425 /* We were unable to bind the arguments. */
7426 return error_mark_node
;
7428 /* In the scope of a template class, explicit references to the
7429 template class refer to the type of the template, not any
7430 instantiation of it. For example, in:
7432 template <class T> class C { void f(C<T>); }
7434 the `C<T>' is just the same as `C'. Outside of the
7435 class, however, such a reference is an instantiation. */
7437 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7438 || currently_open_class (template_type
))
7439 /* comp_template_args is expensive, check it last. */
7440 && comp_template_args (TYPE_TI_ARGS (template_type
),
7442 return template_type
;
7444 /* If we already have this specialization, return it. */
7445 elt
.tmpl
= gen_tmpl
;
7447 hash
= hash_specialization (&elt
);
7448 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7454 is_dependent_type
= uses_template_parms (arglist
);
7456 /* If the deduced arguments are invalid, then the binding
7458 if (!is_dependent_type
7459 && check_instantiated_args (gen_tmpl
,
7460 INNERMOST_TEMPLATE_ARGS (arglist
),
7462 return error_mark_node
;
7464 if (!is_dependent_type
7465 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7466 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7467 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7469 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7470 DECL_NAME (gen_tmpl
),
7471 /*tag_scope=*/ts_global
);
7475 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7477 if (context
== error_mark_node
)
7478 return error_mark_node
;
7481 context
= global_namespace
;
7483 /* Create the type. */
7484 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7486 /* The user referred to a specialization of an alias
7487 template represented by GEN_TMPL.
7489 [temp.alias]/2 says:
7491 When a template-id refers to the specialization of an
7492 alias template, it is equivalent to the associated
7493 type obtained by substitution of its
7494 template-arguments for the template-parameters in the
7495 type-id of the alias template. */
7497 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7498 /* Note that the call above (by indirectly calling
7499 register_specialization in tsubst_decl) registers the
7500 TYPE_DECL representing the specialization of the alias
7501 template. So next time someone substitutes ARGLIST for
7502 the template parms into the alias template (GEN_TMPL),
7503 she'll get that TYPE_DECL back. */
7505 if (t
== error_mark_node
)
7508 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7510 if (!is_dependent_type
)
7512 set_current_access_from_decl (TYPE_NAME (template_type
));
7513 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7514 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7515 arglist
, complain
, in_decl
),
7516 SCOPED_ENUM_P (template_type
), NULL
);
7520 /* We don't want to call start_enum for this type, since
7521 the values for the enumeration constants may involve
7522 template parameters. And, no one should be interested
7523 in the enumeration constants for such a type. */
7524 t
= cxx_make_type (ENUMERAL_TYPE
);
7525 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7527 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7528 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7529 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7531 else if (CLASS_TYPE_P (template_type
))
7533 t
= make_class_type (TREE_CODE (template_type
));
7534 CLASSTYPE_DECLARED_CLASS (t
)
7535 = CLASSTYPE_DECLARED_CLASS (template_type
);
7536 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7537 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7539 /* A local class. Make sure the decl gets registered properly. */
7540 if (context
== current_function_decl
)
7541 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7543 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7544 /* This instantiation is another name for the primary
7545 template type. Set the TYPE_CANONICAL field
7547 TYPE_CANONICAL (t
) = template_type
;
7548 else if (any_template_arguments_need_structural_equality_p (arglist
))
7549 /* Some of the template arguments require structural
7550 equality testing, so this template class requires
7551 structural equality testing. */
7552 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7557 /* If we called start_enum or pushtag above, this information
7558 will already be set up. */
7561 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7563 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7564 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7565 DECL_SOURCE_LOCATION (type_decl
)
7566 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7569 type_decl
= TYPE_NAME (t
);
7571 if (CLASS_TYPE_P (template_type
))
7573 TREE_PRIVATE (type_decl
)
7574 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7575 TREE_PROTECTED (type_decl
)
7576 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7577 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7579 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7580 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7584 /* Let's consider the explicit specialization of a member
7585 of a class template specialization that is implicitely instantiated,
7590 template<class U> struct M {}; //#0
7595 struct S<int>::M<char> //#1
7599 [temp.expl.spec]/4 says this is valid.
7601 In this case, when we write:
7604 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7607 When we encounter #1, we want to store the partial instantiation
7608 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7610 For all cases other than this "explicit specialization of member of a
7611 class template", we just want to store the most general template into
7612 the CLASSTYPE_TI_TEMPLATE of M.
7614 This case of "explicit specialization of member of a class template"
7616 1/ the enclosing class is an instantiation of, and therefore not
7617 the same as, the context of the most general template, and
7618 2/ we aren't looking at the partial instantiation itself, i.e.
7619 the innermost arguments are not the same as the innermost parms of
7620 the most general template.
7622 So it's only when 1/ and 2/ happens that we want to use the partial
7623 instantiation of the member template in lieu of its most general
7626 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7627 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7628 /* the enclosing class must be an instantiation... */
7629 && CLASS_TYPE_P (context
)
7630 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7632 tree partial_inst_args
;
7633 TREE_VEC_LENGTH (arglist
)--;
7634 ++processing_template_decl
;
7636 tsubst (INNERMOST_TEMPLATE_ARGS
7637 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7638 arglist
, complain
, NULL_TREE
);
7639 --processing_template_decl
;
7640 TREE_VEC_LENGTH (arglist
)++;
7641 use_partial_inst_tmpl
=
7642 /*...and we must not be looking at the partial instantiation
7644 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7648 if (!use_partial_inst_tmpl
)
7649 /* This case is easy; there are no member templates involved. */
7653 /* This is a full instantiation of a member template. Find
7654 the partial instantiation of which this is an instance. */
7656 /* Temporarily reduce by one the number of levels in the ARGLIST
7657 so as to avoid comparing the last set of arguments. */
7658 TREE_VEC_LENGTH (arglist
)--;
7659 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7660 TREE_VEC_LENGTH (arglist
)++;
7661 /* FOUND is either a proper class type, or an alias
7662 template specialization. In the later case, it's a
7663 TYPE_DECL, resulting from the substituting of arguments
7664 for parameters in the TYPE_DECL of the alias template
7665 done earlier. So be careful while getting the template
7667 found
= TREE_CODE (found
) == TYPE_DECL
7668 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7669 : CLASSTYPE_TI_TEMPLATE (found
);
7672 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7675 slot
= htab_find_slot_with_hash (type_specializations
,
7676 &elt
, hash
, INSERT
);
7677 entry
= ggc_alloc_spec_entry ();
7681 /* Note this use of the partial instantiation so we can check it
7682 later in maybe_process_partial_specialization. */
7683 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7684 = tree_cons (arglist
, t
,
7685 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7687 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7688 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7689 /* Now that the type has been registered on the instantiations
7690 list, we set up the enumerators. Because the enumeration
7691 constants may involve the enumeration type itself, we make
7692 sure to register the type first, and then create the
7693 constants. That way, doing tsubst_expr for the enumeration
7694 constants won't result in recursive calls here; we'll find
7695 the instantiation and exit above. */
7696 tsubst_enum (template_type
, t
, arglist
);
7698 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7699 /* If the type makes use of template parameters, the
7700 code that generates debugging information will crash. */
7701 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7703 /* Possibly limit visibility based on template args. */
7704 TREE_PUBLIC (type_decl
) = 1;
7705 determine_visibility (type_decl
);
7711 /* Wrapper for lookup_template_class_1. */
7714 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7715 int entering_scope
, tsubst_flags_t complain
)
7718 timevar_push (TV_TEMPLATE_INST
);
7719 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7720 entering_scope
, complain
);
7721 timevar_pop (TV_TEMPLATE_INST
);
7729 /* True when we should also visit template parameters that occur in
7730 non-deduced contexts. */
7731 bool include_nondeduced_p
;
7732 struct pointer_set_t
*visited
;
7735 /* Called from for_each_template_parm via walk_tree. */
7738 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7741 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7742 tree_fn_t fn
= pfd
->fn
;
7743 void *data
= pfd
->data
;
7746 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7747 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7748 pfd
->include_nondeduced_p
))
7749 return error_mark_node
;
7751 switch (TREE_CODE (t
))
7754 if (TYPE_PTRMEMFUNC_P (t
))
7760 if (!TYPE_TEMPLATE_INFO (t
))
7762 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7763 fn
, data
, pfd
->visited
,
7764 pfd
->include_nondeduced_p
))
7765 return error_mark_node
;
7769 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7770 fn
, data
, pfd
->visited
,
7771 pfd
->include_nondeduced_p
)
7772 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7773 fn
, data
, pfd
->visited
,
7774 pfd
->include_nondeduced_p
))
7775 return error_mark_node
;
7779 /* Since we're not going to walk subtrees, we have to do this
7781 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7782 pfd
->visited
, pfd
->include_nondeduced_p
))
7783 return error_mark_node
;
7787 /* Check the return type. */
7788 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7789 pfd
->include_nondeduced_p
))
7790 return error_mark_node
;
7792 /* Check the parameter types. Since default arguments are not
7793 instantiated until they are needed, the TYPE_ARG_TYPES may
7794 contain expressions that involve template parameters. But,
7795 no-one should be looking at them yet. And, once they're
7796 instantiated, they don't contain template parameters, so
7797 there's no point in looking at them then, either. */
7801 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7802 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7803 pfd
->visited
, pfd
->include_nondeduced_p
))
7804 return error_mark_node
;
7806 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7807 want walk_tree walking into them itself. */
7813 case UNDERLYING_TYPE
:
7814 if (pfd
->include_nondeduced_p
7815 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7817 pfd
->include_nondeduced_p
))
7818 return error_mark_node
;
7823 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7824 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7825 pfd
->visited
, pfd
->include_nondeduced_p
))
7826 return error_mark_node
;
7831 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7832 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7833 pfd
->visited
, pfd
->include_nondeduced_p
))
7834 return error_mark_node
;
7835 if (DECL_CONTEXT (t
)
7836 && pfd
->include_nondeduced_p
7837 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7838 pfd
->visited
, pfd
->include_nondeduced_p
))
7839 return error_mark_node
;
7842 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7843 /* Record template parameters such as `T' inside `TT<T>'. */
7844 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7845 pfd
->include_nondeduced_p
))
7846 return error_mark_node
;
7849 case TEMPLATE_TEMPLATE_PARM
:
7850 case TEMPLATE_TYPE_PARM
:
7851 case TEMPLATE_PARM_INDEX
:
7852 if (fn
&& (*fn
)(t
, data
))
7853 return error_mark_node
;
7855 return error_mark_node
;
7859 /* A template template parameter is encountered. */
7860 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7861 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7862 pfd
->include_nondeduced_p
))
7863 return error_mark_node
;
7865 /* Already substituted template template parameter */
7871 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7873 pfd
->include_nondeduced_p
))
7874 return error_mark_node
;
7878 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7879 && pfd
->include_nondeduced_p
7880 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7881 (TREE_TYPE (t
)), fn
, data
,
7882 pfd
->visited
, pfd
->include_nondeduced_p
))
7883 return error_mark_node
;
7888 /* If there's no type, then this thing must be some expression
7889 involving template parameters. */
7890 if (!fn
&& !TREE_TYPE (t
))
7891 return error_mark_node
;
7896 case IMPLICIT_CONV_EXPR
:
7897 case REINTERPRET_CAST_EXPR
:
7898 case CONST_CAST_EXPR
:
7899 case STATIC_CAST_EXPR
:
7900 case DYNAMIC_CAST_EXPR
:
7904 case PSEUDO_DTOR_EXPR
:
7906 return error_mark_node
;
7913 /* We didn't find any template parameters we liked. */
7917 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7918 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7919 call FN with the parameter and the DATA.
7920 If FN returns nonzero, the iteration is terminated, and
7921 for_each_template_parm returns 1. Otherwise, the iteration
7922 continues. If FN never returns a nonzero value, the value
7923 returned by for_each_template_parm is 0. If FN is NULL, it is
7924 considered to be the function which always returns 1.
7926 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7927 parameters that occur in non-deduced contexts. When false, only
7928 visits those template parameters that can be deduced. */
7931 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7932 struct pointer_set_t
*visited
,
7933 bool include_nondeduced_p
)
7935 struct pair_fn_data pfd
;
7941 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7943 /* Walk the tree. (Conceptually, we would like to walk without
7944 duplicates, but for_each_template_parm_r recursively calls
7945 for_each_template_parm, so we would need to reorganize a fair
7946 bit to use walk_tree_without_duplicates, so we keep our own
7949 pfd
.visited
= visited
;
7951 pfd
.visited
= pointer_set_create ();
7952 result
= cp_walk_tree (&t
,
7953 for_each_template_parm_r
,
7955 pfd
.visited
) != NULL_TREE
;
7960 pointer_set_destroy (pfd
.visited
);
7967 /* Returns true if T depends on any template parameter. */
7970 uses_template_parms (tree t
)
7973 int saved_processing_template_decl
;
7975 saved_processing_template_decl
= processing_template_decl
;
7976 if (!saved_processing_template_decl
)
7977 processing_template_decl
= 1;
7979 dependent_p
= dependent_type_p (t
);
7980 else if (TREE_CODE (t
) == TREE_VEC
)
7981 dependent_p
= any_dependent_template_arguments_p (t
);
7982 else if (TREE_CODE (t
) == TREE_LIST
)
7983 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7984 || uses_template_parms (TREE_CHAIN (t
)));
7985 else if (TREE_CODE (t
) == TYPE_DECL
)
7986 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7989 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7990 || TREE_CODE (t
) == OVERLOAD
7993 || TREE_CODE (t
) == TRAIT_EXPR
7994 || TREE_CODE (t
) == CONSTRUCTOR
7995 || CONSTANT_CLASS_P (t
))
7996 dependent_p
= (type_dependent_expression_p (t
)
7997 || value_dependent_expression_p (t
));
8000 gcc_assert (t
== error_mark_node
);
8001 dependent_p
= false;
8004 processing_template_decl
= saved_processing_template_decl
;
8009 /* Returns true iff current_function_decl is an incompletely instantiated
8010 template. Useful instead of processing_template_decl because the latter
8011 is set to 0 during fold_non_dependent_expr. */
8014 in_template_function (void)
8016 tree fn
= current_function_decl
;
8018 ++processing_template_decl
;
8019 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8020 && DECL_TEMPLATE_INFO (fn
)
8021 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8022 --processing_template_decl
;
8026 /* Returns true if T depends on any template parameter with level LEVEL. */
8029 uses_template_parms_level (tree t
, int level
)
8031 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8032 /*include_nondeduced_p=*/true);
8035 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8036 ill-formed translation unit, i.e. a variable or function that isn't
8037 usable in a constant expression. */
8040 neglectable_inst_p (tree d
)
8043 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8044 : decl_maybe_constant_var_p (d
)));
8047 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8048 neglectable and instantiated from within an erroneous instantiation. */
8051 limit_bad_template_recursion (tree decl
)
8053 struct tinst_level
*lev
= current_tinst_level
;
8054 int errs
= errorcount
+ sorrycount
;
8055 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8058 for (; lev
; lev
= lev
->next
)
8059 if (neglectable_inst_p (lev
->decl
))
8062 return (lev
&& errs
> lev
->errors
);
8065 static int tinst_depth
;
8066 extern int max_tinst_depth
;
8069 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8071 /* We're starting to instantiate D; record the template instantiation context
8072 for diagnostics and to restore it later. */
8075 push_tinst_level (tree d
)
8077 struct tinst_level
*new_level
;
8079 if (tinst_depth
>= max_tinst_depth
)
8081 last_error_tinst_level
= current_tinst_level
;
8082 if (TREE_CODE (d
) == TREE_LIST
)
8083 error ("template instantiation depth exceeds maximum of %d (use "
8084 "-ftemplate-depth= to increase the maximum) substituting %qS",
8085 max_tinst_depth
, d
);
8087 error ("template instantiation depth exceeds maximum of %d (use "
8088 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8089 max_tinst_depth
, d
);
8091 print_instantiation_context ();
8096 /* If the current instantiation caused problems, don't let it instantiate
8097 anything else. Do allow deduction substitution and decls usable in
8098 constant expressions. */
8099 if (limit_bad_template_recursion (d
))
8102 new_level
= ggc_alloc_tinst_level ();
8103 new_level
->decl
= d
;
8104 new_level
->locus
= input_location
;
8105 new_level
->errors
= errorcount
+sorrycount
;
8106 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8107 new_level
->next
= current_tinst_level
;
8108 current_tinst_level
= new_level
;
8111 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8112 depth_reached
= tinst_depth
;
8117 /* We're done instantiating this template; return to the instantiation
8121 pop_tinst_level (void)
8123 /* Restore the filename and line number stashed away when we started
8124 this instantiation. */
8125 input_location
= current_tinst_level
->locus
;
8126 current_tinst_level
= current_tinst_level
->next
;
8130 /* We're instantiating a deferred template; restore the template
8131 instantiation context in which the instantiation was requested, which
8132 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8135 reopen_tinst_level (struct tinst_level
*level
)
8137 struct tinst_level
*t
;
8140 for (t
= level
; t
; t
= t
->next
)
8143 current_tinst_level
= level
;
8145 if (current_tinst_level
)
8146 current_tinst_level
->errors
= errorcount
+sorrycount
;
8150 /* Returns the TINST_LEVEL which gives the original instantiation
8153 struct tinst_level
*
8154 outermost_tinst_level (void)
8156 struct tinst_level
*level
= current_tinst_level
;
8159 level
= level
->next
;
8163 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8164 vector of template arguments, as for tsubst.
8166 Returns an appropriate tsubst'd friend declaration. */
8169 tsubst_friend_function (tree decl
, tree args
)
8173 if (TREE_CODE (decl
) == FUNCTION_DECL
8174 && DECL_TEMPLATE_INSTANTIATION (decl
)
8175 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8176 /* This was a friend declared with an explicit template
8177 argument list, e.g.:
8181 to indicate that f was a template instantiation, not a new
8182 function declaration. Now, we have to figure out what
8183 instantiation of what template. */
8185 tree template_id
, arglist
, fns
;
8188 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8190 /* Friend functions are looked up in the containing namespace scope.
8191 We must enter that scope, to avoid finding member functions of the
8192 current class with same name. */
8193 push_nested_namespace (ns
);
8194 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8195 tf_warning_or_error
, NULL_TREE
,
8196 /*integral_constant_expression_p=*/false);
8197 pop_nested_namespace (ns
);
8198 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8199 tf_warning_or_error
, NULL_TREE
);
8200 template_id
= lookup_template_function (fns
, arglist
);
8202 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8203 tmpl
= determine_specialization (template_id
, new_friend
,
8205 /*need_member_template=*/0,
8206 TREE_VEC_LENGTH (args
),
8208 return instantiate_template (tmpl
, new_args
, tf_error
);
8211 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8213 /* The NEW_FRIEND will look like an instantiation, to the
8214 compiler, but is not an instantiation from the point of view of
8215 the language. For example, we might have had:
8217 template <class T> struct S {
8218 template <class U> friend void f(T, U);
8221 Then, in S<int>, template <class U> void f(int, U) is not an
8222 instantiation of anything. */
8223 if (new_friend
== error_mark_node
)
8224 return error_mark_node
;
8226 DECL_USE_TEMPLATE (new_friend
) = 0;
8227 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8229 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8230 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8231 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8234 /* The mangled name for the NEW_FRIEND is incorrect. The function
8235 is not a template instantiation and should not be mangled like
8236 one. Therefore, we forget the mangling here; we'll recompute it
8237 later if we need it. */
8238 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8240 SET_DECL_RTL (new_friend
, NULL
);
8241 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8244 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8247 tree new_friend_template_info
;
8248 tree new_friend_result_template_info
;
8250 int new_friend_is_defn
;
8252 /* We must save some information from NEW_FRIEND before calling
8253 duplicate decls since that function will free NEW_FRIEND if
8255 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8256 new_friend_is_defn
=
8257 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8258 (template_for_substitution (new_friend
)))
8260 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8262 /* This declaration is a `primary' template. */
8263 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8265 new_friend_result_template_info
8266 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8269 new_friend_result_template_info
= NULL_TREE
;
8271 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8272 if (new_friend_is_defn
)
8273 DECL_INITIAL (new_friend
) = error_mark_node
;
8275 /* Inside pushdecl_namespace_level, we will push into the
8276 current namespace. However, the friend function should go
8277 into the namespace of the template. */
8278 ns
= decl_namespace_context (new_friend
);
8279 push_nested_namespace (ns
);
8280 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8281 pop_nested_namespace (ns
);
8283 if (old_decl
== error_mark_node
)
8284 return error_mark_node
;
8286 if (old_decl
!= new_friend
)
8288 /* This new friend declaration matched an existing
8289 declaration. For example, given:
8291 template <class T> void f(T);
8292 template <class U> class C {
8293 template <class T> friend void f(T) {}
8296 the friend declaration actually provides the definition
8297 of `f', once C has been instantiated for some type. So,
8298 old_decl will be the out-of-class template declaration,
8299 while new_friend is the in-class definition.
8301 But, if `f' was called before this point, the
8302 instantiation of `f' will have DECL_TI_ARGS corresponding
8303 to `T' but not to `U', references to which might appear
8304 in the definition of `f'. Previously, the most general
8305 template for an instantiation of `f' was the out-of-class
8306 version; now it is the in-class version. Therefore, we
8307 run through all specialization of `f', adding to their
8308 DECL_TI_ARGS appropriately. In particular, they need a
8309 new set of outer arguments, corresponding to the
8310 arguments for this class instantiation.
8312 The same situation can arise with something like this:
8315 template <class T> class C {
8319 when `C<int>' is instantiated. Now, `f(int)' is defined
8322 if (!new_friend_is_defn
)
8323 /* On the other hand, if the in-class declaration does
8324 *not* provide a definition, then we don't want to alter
8325 existing definitions. We can just leave everything
8330 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8331 tree new_args
= TI_ARGS (new_friend_template_info
);
8333 /* Overwrite whatever template info was there before, if
8334 any, with the new template information pertaining to
8336 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8338 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8340 /* We should have called reregister_specialization in
8342 gcc_assert (retrieve_specialization (new_template
,
8346 /* Instantiate it if the global has already been used. */
8347 if (DECL_ODR_USED (old_decl
))
8348 instantiate_decl (old_decl
, /*defer_ok=*/true,
8349 /*expl_inst_class_mem_p=*/false);
8355 /* Indicate that the old function template is a partial
8357 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8358 = new_friend_result_template_info
;
8360 gcc_assert (new_template
8361 == most_general_template (new_template
));
8362 gcc_assert (new_template
!= old_decl
);
8364 /* Reassign any specializations already in the hash table
8365 to the new more general template, and add the
8366 additional template args. */
8367 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8371 tree spec
= TREE_VALUE (t
);
8374 elt
.tmpl
= old_decl
;
8375 elt
.args
= DECL_TI_ARGS (spec
);
8376 elt
.spec
= NULL_TREE
;
8378 htab_remove_elt (decl_specializations
, &elt
);
8381 = add_outermost_template_args (new_args
,
8382 DECL_TI_ARGS (spec
));
8384 register_specialization
8385 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8388 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8392 /* The information from NEW_FRIEND has been merged into OLD_DECL
8393 by duplicate_decls. */
8394 new_friend
= old_decl
;
8399 tree context
= DECL_CONTEXT (new_friend
);
8403 template <class T> class C {
8404 template <class U> friend void C1<U>::f (); // case 1
8405 friend void C2<T>::f (); // case 2
8407 we only need to make sure CONTEXT is a complete type for
8408 case 2. To distinguish between the two cases, we note that
8409 CONTEXT of case 1 remains dependent type after tsubst while
8410 this isn't true for case 2. */
8411 ++processing_template_decl
;
8412 dependent_p
= dependent_type_p (context
);
8413 --processing_template_decl
;
8416 && !complete_type_or_else (context
, NULL_TREE
))
8417 return error_mark_node
;
8419 if (COMPLETE_TYPE_P (context
))
8421 /* Check to see that the declaration is really present, and,
8422 possibly obtain an improved declaration. */
8423 tree fn
= check_classfn (context
,
8424 new_friend
, NULL_TREE
);
8434 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8435 template arguments, as for tsubst.
8437 Returns an appropriate tsubst'd friend type or error_mark_node on
8441 tsubst_friend_class (tree friend_tmpl
, tree args
)
8447 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8449 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8450 return TREE_TYPE (t
);
8453 context
= CP_DECL_CONTEXT (friend_tmpl
);
8455 if (context
!= global_namespace
)
8457 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8458 push_nested_namespace (context
);
8460 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8463 /* Look for a class template declaration. We look for hidden names
8464 because two friend declarations of the same template are the
8465 same. For example, in:
8468 template <typename> friend class F;
8470 template <typename> struct B {
8471 template <typename> friend class F;
8474 both F templates are the same. */
8475 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8476 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8478 /* But, if we don't find one, it might be because we're in a
8479 situation like this:
8487 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8488 for `S<int>', not the TEMPLATE_DECL. */
8489 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8491 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8492 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8495 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8497 /* The friend template has already been declared. Just
8498 check to see that the declarations match, and install any new
8499 default parameters. We must tsubst the default parameters,
8500 of course. We only need the innermost template parameters
8501 because that is all that redeclare_class_template will look
8503 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8504 > TMPL_ARGS_DEPTH (args
))
8507 location_t saved_input_location
;
8508 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8509 args
, tf_warning_or_error
);
8511 saved_input_location
= input_location
;
8512 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8513 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8514 input_location
= saved_input_location
;
8518 friend_type
= TREE_TYPE (tmpl
);
8522 /* The friend template has not already been declared. In this
8523 case, the instantiation of the template class will cause the
8524 injection of this template into the global scope. */
8525 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8526 if (tmpl
== error_mark_node
)
8527 return error_mark_node
;
8529 /* The new TMPL is not an instantiation of anything, so we
8530 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8531 the new type because that is supposed to be the corresponding
8532 template decl, i.e., TMPL. */
8533 DECL_USE_TEMPLATE (tmpl
) = 0;
8534 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8535 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8536 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8537 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8539 /* Inject this template into the global scope. */
8540 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8543 if (context
!= global_namespace
)
8545 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8546 pop_nested_namespace (context
);
8548 pop_nested_class ();
8554 /* Returns zero if TYPE cannot be completed later due to circularity.
8555 Otherwise returns one. */
8558 can_complete_type_without_circularity (tree type
)
8560 if (type
== NULL_TREE
|| type
== error_mark_node
)
8562 else if (COMPLETE_TYPE_P (type
))
8564 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8565 return can_complete_type_without_circularity (TREE_TYPE (type
));
8566 else if (CLASS_TYPE_P (type
)
8567 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8573 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8575 /* Apply any attributes which had to be deferred until instantiation
8576 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8577 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8580 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8581 tree args
, tsubst_flags_t complain
, tree in_decl
)
8583 tree last_dep
= NULL_TREE
;
8587 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8588 if (ATTR_IS_DEPENDENT (t
))
8591 attributes
= copy_list (attributes
);
8595 if (DECL_P (*decl_p
))
8597 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8599 p
= &DECL_ATTRIBUTES (*decl_p
);
8602 p
= &TYPE_ATTRIBUTES (*decl_p
);
8606 tree late_attrs
= NULL_TREE
;
8607 tree
*q
= &late_attrs
;
8609 for (*p
= attributes
; *p
; )
8612 if (ATTR_IS_DEPENDENT (t
))
8614 *p
= TREE_CHAIN (t
);
8615 TREE_CHAIN (t
) = NULL_TREE
;
8616 if ((flag_openmp
|| flag_enable_cilkplus
)
8617 && is_attribute_p ("omp declare simd",
8618 get_attribute_name (t
))
8621 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8622 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8624 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8625 clauses
= finish_omp_clauses (clauses
);
8626 tree parms
= DECL_ARGUMENTS (*decl_p
);
8628 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8630 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8632 TREE_VALUE (t
) = NULL_TREE
;
8634 /* If the first attribute argument is an identifier, don't
8635 pass it through tsubst. Attributes like mode, format,
8636 cleanup and several target specific attributes expect it
8638 else if (attribute_takes_identifier_p (get_attribute_name (t
))
8642 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8644 /*integral_constant_expression_p=*/false);
8645 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8647 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8652 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8653 /*integral_constant_expression_p=*/false);
8655 q
= &TREE_CHAIN (t
);
8658 p
= &TREE_CHAIN (t
);
8661 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8665 /* Perform (or defer) access check for typedefs that were referenced
8666 from within the template TMPL code.
8667 This is a subroutine of instantiate_decl and instantiate_class_template.
8668 TMPL is the template to consider and TARGS is the list of arguments of
8672 perform_typedefs_access_check (tree tmpl
, tree targs
)
8674 location_t saved_location
;
8676 qualified_typedef_usage_t
*iter
;
8679 || (!CLASS_TYPE_P (tmpl
)
8680 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8683 saved_location
= input_location
;
8684 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8686 tree type_decl
= iter
->typedef_decl
;
8687 tree type_scope
= iter
->context
;
8689 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8692 if (uses_template_parms (type_decl
))
8693 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8694 if (uses_template_parms (type_scope
))
8695 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8697 /* Make access check error messages point to the location
8698 of the use of the typedef. */
8699 input_location
= iter
->locus
;
8700 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8701 type_decl
, type_decl
,
8702 tf_warning_or_error
);
8704 input_location
= saved_location
;
8708 instantiate_class_template_1 (tree type
)
8710 tree templ
, args
, pattern
, t
, member
;
8714 unsigned int saved_maximum_field_alignment
;
8717 if (type
== error_mark_node
)
8718 return error_mark_node
;
8720 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8721 || uses_template_parms (type
))
8724 /* Figure out which template is being instantiated. */
8725 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8726 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8728 /* Determine what specialization of the original template to
8730 t
= most_specialized_class (type
, tf_warning_or_error
);
8731 if (t
== error_mark_node
)
8733 TYPE_BEING_DEFINED (type
) = 1;
8734 return error_mark_node
;
8738 /* This TYPE is actually an instantiation of a partial
8739 specialization. We replace the innermost set of ARGS with
8740 the arguments appropriate for substitution. For example,
8743 template <class T> struct S {};
8744 template <class T> struct S<T*> {};
8746 and supposing that we are instantiating S<int*>, ARGS will
8747 presently be {int*} -- but we need {int}. */
8748 pattern
= TREE_TYPE (t
);
8749 args
= TREE_PURPOSE (t
);
8753 pattern
= TREE_TYPE (templ
);
8754 args
= CLASSTYPE_TI_ARGS (type
);
8757 /* If the template we're instantiating is incomplete, then clearly
8758 there's nothing we can do. */
8759 if (!COMPLETE_TYPE_P (pattern
))
8762 /* If we've recursively instantiated too many templates, stop. */
8763 if (! push_tinst_level (type
))
8766 /* Now we're really doing the instantiation. Mark the type as in
8767 the process of being defined. */
8768 TYPE_BEING_DEFINED (type
) = 1;
8770 /* We may be in the middle of deferred access check. Disable
8772 push_deferring_access_checks (dk_no_deferred
);
8774 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8776 push_to_top_level ();
8777 /* Use #pragma pack from the template context. */
8778 saved_maximum_field_alignment
= maximum_field_alignment
;
8779 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8781 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8783 /* Set the input location to the most specialized template definition.
8784 This is needed if tsubsting causes an error. */
8785 typedecl
= TYPE_MAIN_DECL (pattern
);
8786 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8787 DECL_SOURCE_LOCATION (typedecl
);
8789 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8790 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8791 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8792 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8793 if (ANON_AGGR_TYPE_P (pattern
))
8794 SET_ANON_AGGR_TYPE_P (type
);
8795 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8797 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8798 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8799 /* Adjust visibility for template arguments. */
8800 determine_visibility (TYPE_MAIN_DECL (type
));
8802 if (CLASS_TYPE_P (type
))
8803 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8805 pbinfo
= TYPE_BINFO (pattern
);
8807 /* We should never instantiate a nested class before its enclosing
8808 class; we need to look up the nested class by name before we can
8809 instantiate it, and that lookup should instantiate the enclosing
8811 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8812 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8814 base_list
= NULL_TREE
;
8815 if (BINFO_N_BASE_BINFOS (pbinfo
))
8821 /* We must enter the scope containing the type, as that is where
8822 the accessibility of types named in dependent bases are
8824 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8826 /* Substitute into each of the bases to determine the actual
8828 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8831 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8832 tree expanded_bases
= NULL_TREE
;
8835 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8838 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8839 args
, tf_error
, NULL_TREE
);
8840 if (expanded_bases
== error_mark_node
)
8843 len
= TREE_VEC_LENGTH (expanded_bases
);
8846 for (idx
= 0; idx
< len
; idx
++)
8849 /* Extract the already-expanded base class. */
8850 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8852 /* Substitute to figure out the base class. */
8853 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8856 if (base
== error_mark_node
)
8859 base_list
= tree_cons (access
, base
, base_list
);
8860 if (BINFO_VIRTUAL_P (pbase_binfo
))
8861 TREE_TYPE (base_list
) = integer_type_node
;
8865 /* The list is now in reverse order; correct that. */
8866 base_list
= nreverse (base_list
);
8869 pop_scope (pushed_scope
);
8871 /* Now call xref_basetypes to set up all the base-class
8873 xref_basetypes (type
, base_list
);
8875 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8876 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8877 args
, tf_error
, NULL_TREE
);
8878 fixup_attribute_variants (type
);
8880 /* Now that our base classes are set up, enter the scope of the
8881 class, so that name lookups into base classes, etc. will work
8882 correctly. This is precisely analogous to what we do in
8883 begin_class_definition when defining an ordinary non-template
8884 class, except we also need to push the enclosing classes. */
8885 push_nested_class (type
);
8887 /* Now members are processed in the order of declaration. */
8888 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8889 member
; member
= TREE_CHAIN (member
))
8891 tree t
= TREE_VALUE (member
);
8893 if (TREE_PURPOSE (member
))
8897 /* Build new CLASSTYPE_NESTED_UTDS. */
8900 bool class_template_p
;
8902 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8903 && TYPE_LANG_SPECIFIC (t
)
8904 && CLASSTYPE_IS_TEMPLATE (t
));
8905 /* If the member is a class template, then -- even after
8906 substitution -- there may be dependent types in the
8907 template argument list for the class. We increment
8908 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8909 that function will assume that no types are dependent
8910 when outside of a template. */
8911 if (class_template_p
)
8912 ++processing_template_decl
;
8913 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8914 if (class_template_p
)
8915 --processing_template_decl
;
8916 if (newtag
== error_mark_node
)
8919 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8921 tree name
= TYPE_IDENTIFIER (t
);
8923 if (class_template_p
)
8924 /* Unfortunately, lookup_template_class sets
8925 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8926 instantiation (i.e., for the type of a member
8927 template class nested within a template class.)
8928 This behavior is required for
8929 maybe_process_partial_specialization to work
8930 correctly, but is not accurate in this case;
8931 the TAG is not an instantiation of anything.
8932 (The corresponding TEMPLATE_DECL is an
8933 instantiation, but the TYPE is not.) */
8934 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8936 /* Now, we call pushtag to put this NEWTAG into the scope of
8937 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8938 pushtag calling push_template_decl. We don't have to do
8939 this for enums because it will already have been done in
8942 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8943 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8946 else if (DECL_DECLARES_FUNCTION_P (t
))
8948 /* Build new TYPE_METHODS. */
8951 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8952 ++processing_template_decl
;
8953 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8954 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8955 --processing_template_decl
;
8956 set_current_access_from_decl (r
);
8957 finish_member_declaration (r
);
8958 /* Instantiate members marked with attribute used. */
8959 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8961 if (TREE_CODE (r
) == FUNCTION_DECL
8962 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8963 cp_check_omp_declare_reduction (r
);
8967 /* Build new TYPE_FIELDS. */
8968 if (TREE_CODE (t
) == STATIC_ASSERT
)
8972 ++c_inhibit_evaluation_warnings
;
8974 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8975 tf_warning_or_error
, NULL_TREE
,
8976 /*integral_constant_expression_p=*/true);
8977 --c_inhibit_evaluation_warnings
;
8979 finish_static_assert (condition
,
8980 STATIC_ASSERT_MESSAGE (t
),
8981 STATIC_ASSERT_SOURCE_LOCATION (t
),
8984 else if (TREE_CODE (t
) != CONST_DECL
)
8987 tree vec
= NULL_TREE
;
8990 /* The file and line for this declaration, to
8991 assist in error message reporting. Since we
8992 called push_tinst_level above, we don't need to
8994 input_location
= DECL_SOURCE_LOCATION (t
);
8996 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8997 ++processing_template_decl
;
8998 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8999 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9000 --processing_template_decl
;
9002 if (TREE_CODE (r
) == TREE_VEC
)
9004 /* A capture pack became multiple fields. */
9006 len
= TREE_VEC_LENGTH (vec
);
9009 for (int i
= 0; i
< len
; ++i
)
9012 r
= TREE_VEC_ELT (vec
, i
);
9017 [t]he initialization (and any associated
9018 side-effects) of a static data member does
9019 not occur unless the static data member is
9020 itself used in a way that requires the
9021 definition of the static data member to
9024 Therefore, we do not substitute into the
9025 initialized for the static data member here. */
9026 finish_static_data_member_decl
9029 /*init_const_expr_p=*/false,
9030 /*asmspec_tree=*/NULL_TREE
,
9032 /* Instantiate members marked with attribute used. */
9033 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9036 else if (TREE_CODE (r
) == FIELD_DECL
)
9038 /* Determine whether R has a valid type and can be
9039 completed later. If R is invalid, then its type
9040 is replaced by error_mark_node. */
9041 tree rtype
= TREE_TYPE (r
);
9042 if (can_complete_type_without_circularity (rtype
))
9043 complete_type (rtype
);
9045 if (!COMPLETE_TYPE_P (rtype
))
9047 cxx_incomplete_type_error (r
, rtype
);
9048 TREE_TYPE (r
) = error_mark_node
;
9052 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9053 such a thing will already have been added to the field
9054 list by tsubst_enum in finish_member_declaration in the
9055 CLASSTYPE_NESTED_UTDS case above. */
9056 if (!(TREE_CODE (r
) == TYPE_DECL
9057 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9058 && DECL_ARTIFICIAL (r
)))
9060 set_current_access_from_decl (r
);
9061 finish_member_declaration (r
);
9069 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9070 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9072 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9074 tree friend_type
= t
;
9075 bool adjust_processing_template_decl
= false;
9077 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9079 /* template <class T> friend class C; */
9080 friend_type
= tsubst_friend_class (friend_type
, args
);
9081 adjust_processing_template_decl
= true;
9083 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9085 /* template <class T> friend class C::D; */
9086 friend_type
= tsubst (friend_type
, args
,
9087 tf_warning_or_error
, NULL_TREE
);
9088 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9089 friend_type
= TREE_TYPE (friend_type
);
9090 adjust_processing_template_decl
= true;
9092 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9093 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9095 /* This could be either
9099 when dependent_type_p is false or
9101 template <class U> friend class T::C;
9104 friend_type
= tsubst (friend_type
, args
,
9105 tf_warning_or_error
, NULL_TREE
);
9106 /* Bump processing_template_decl for correct
9107 dependent_type_p calculation. */
9108 ++processing_template_decl
;
9109 if (dependent_type_p (friend_type
))
9110 adjust_processing_template_decl
= true;
9111 --processing_template_decl
;
9113 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9114 && hidden_name_p (TYPE_NAME (friend_type
)))
9118 where C hasn't been declared yet. Let's lookup name
9119 from namespace scope directly, bypassing any name that
9120 come from dependent base class. */
9121 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9123 /* The call to xref_tag_from_type does injection for friend
9125 push_nested_namespace (ns
);
9127 xref_tag_from_type (friend_type
, NULL_TREE
,
9128 /*tag_scope=*/ts_current
);
9129 pop_nested_namespace (ns
);
9131 else if (uses_template_parms (friend_type
))
9132 /* friend class C<T>; */
9133 friend_type
= tsubst (friend_type
, args
,
9134 tf_warning_or_error
, NULL_TREE
);
9139 where C is already declared or
9141 friend class C<int>;
9143 We don't have to do anything in these cases. */
9145 if (adjust_processing_template_decl
)
9146 /* Trick make_friend_class into realizing that the friend
9147 we're adding is a template, not an ordinary class. It's
9148 important that we use make_friend_class since it will
9149 perform some error-checking and output cross-reference
9151 ++processing_template_decl
;
9153 if (friend_type
!= error_mark_node
)
9154 make_friend_class (type
, friend_type
, /*complain=*/false);
9156 if (adjust_processing_template_decl
)
9157 --processing_template_decl
;
9161 /* Build new DECL_FRIENDLIST. */
9164 /* The file and line for this declaration, to
9165 assist in error message reporting. Since we
9166 called push_tinst_level above, we don't need to
9168 input_location
= DECL_SOURCE_LOCATION (t
);
9170 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9172 ++processing_template_decl
;
9173 push_deferring_access_checks (dk_no_check
);
9176 r
= tsubst_friend_function (t
, args
);
9177 add_friend (type
, r
, /*complain=*/false);
9178 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9180 pop_deferring_access_checks ();
9181 --processing_template_decl
;
9187 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9189 tree decl
= lambda_function (type
);
9192 if (!DECL_TEMPLATE_INFO (decl
)
9193 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9194 instantiate_decl (decl
, false, false);
9196 /* We need to instantiate the capture list from the template
9197 after we've instantiated the closure members, but before we
9198 consider adding the conversion op. Also keep any captures
9199 that may have been added during instantiation of the op(). */
9200 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9202 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9203 args
, tf_warning_or_error
, NULL_TREE
,
9206 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9207 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9209 maybe_add_lambda_conv_op (type
);
9212 gcc_assert (errorcount
);
9215 /* Set the file and line number information to whatever is given for
9216 the class itself. This puts error messages involving generated
9217 implicit functions at a predictable point, and the same point
9218 that would be used for non-template classes. */
9219 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9221 unreverse_member_declarations (type
);
9222 finish_struct_1 (type
);
9223 TYPE_BEING_DEFINED (type
) = 0;
9225 /* We don't instantiate default arguments for member functions. 14.7.1:
9227 The implicit instantiation of a class template specialization causes
9228 the implicit instantiation of the declarations, but not of the
9229 definitions or default arguments, of the class member functions,
9230 member classes, static data members and member templates.... */
9232 /* Some typedefs referenced from within the template code need to be access
9233 checked at template instantiation time, i.e now. These types were
9234 added to the template at parsing time. Let's get those and perform
9235 the access checks then. */
9236 perform_typedefs_access_check (pattern
, args
);
9237 perform_deferred_access_checks (tf_warning_or_error
);
9238 pop_nested_class ();
9239 maximum_field_alignment
= saved_maximum_field_alignment
;
9241 pop_from_top_level ();
9242 pop_deferring_access_checks ();
9245 /* The vtable for a template class can be emitted in any translation
9246 unit in which the class is instantiated. When there is no key
9247 method, however, finish_struct_1 will already have added TYPE to
9248 the keyed_classes list. */
9249 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9250 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9255 /* Wrapper for instantiate_class_template_1. */
9258 instantiate_class_template (tree type
)
9261 timevar_push (TV_TEMPLATE_INST
);
9262 ret
= instantiate_class_template_1 (type
);
9263 timevar_pop (TV_TEMPLATE_INST
);
9268 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9274 else if (TYPE_P (t
))
9275 r
= tsubst (t
, args
, complain
, in_decl
);
9278 if (!(complain
& tf_warning
))
9279 ++c_inhibit_evaluation_warnings
;
9280 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9281 /*integral_constant_expression_p=*/true);
9282 if (!(complain
& tf_warning
))
9283 --c_inhibit_evaluation_warnings
;
9284 /* Preserve the raw-reference nature of T. */
9285 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9286 && REFERENCE_REF_P (r
))
9287 r
= TREE_OPERAND (r
, 0);
9292 /* Given a function parameter pack TMPL_PARM and some function parameters
9293 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9294 and set *SPEC_P to point at the next point in the list. */
9297 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9299 /* Collect all of the extra "packed" parameters into an
9303 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9304 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9305 tree spec_parm
= *spec_p
;
9308 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9310 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9313 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9314 parmvec
= make_tree_vec (len
);
9315 parmtypevec
= make_tree_vec (len
);
9316 spec_parm
= *spec_p
;
9317 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9319 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9320 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9323 /* Build the argument packs. */
9324 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9325 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9326 TREE_TYPE (argpack
) = argtypepack
;
9327 *spec_p
= spec_parm
;
9332 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9333 NONTYPE_ARGUMENT_PACK. */
9336 make_fnparm_pack (tree spec_parm
)
9338 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9341 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9345 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9347 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9348 if (i
>= TREE_VEC_LENGTH (vec
))
9350 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9354 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9357 make_argument_pack_select (tree arg_pack
, unsigned index
)
9359 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9361 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9362 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9367 /* This is a subroutine of tsubst_pack_expansion.
9369 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9370 mechanism to store the (non complete list of) arguments of the
9371 substitution and return a non substituted pack expansion, in order
9372 to wait for when we have enough arguments to really perform the
9376 use_pack_expansion_extra_args_p (tree parm_packs
,
9380 /* If one pack has an expansion and another pack has a normal
9381 argument or if one pack has an empty argument and an another
9382 one hasn't then tsubst_pack_expansion cannot perform the
9383 substitution and need to fall back on the
9384 PACK_EXPANSION_EXTRA mechanism. */
9385 if (parm_packs
== NULL_TREE
)
9387 else if (has_empty_arg
)
9390 bool has_expansion_arg
= false;
9391 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9393 bool has_non_expansion_arg
= false;
9394 for (tree parm_pack
= parm_packs
;
9396 parm_pack
= TREE_CHAIN (parm_pack
))
9398 tree arg
= TREE_VALUE (parm_pack
);
9400 if (argument_pack_element_is_expansion_p (arg
, i
))
9401 has_expansion_arg
= true;
9403 has_non_expansion_arg
= true;
9406 if (has_expansion_arg
&& has_non_expansion_arg
)
9412 /* [temp.variadic]/6 says that:
9414 The instantiation of a pack expansion [...]
9415 produces a list E1,E2, ..., En, where N is the number of elements
9416 in the pack expansion parameters.
9418 This subroutine of tsubst_pack_expansion produces one of these Ei.
9420 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9421 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9422 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9423 INDEX is the index 'i' of the element Ei to produce. ARGS,
9424 COMPLAIN, and IN_DECL are the same parameters as for the
9425 tsubst_pack_expansion function.
9427 The function returns the resulting Ei upon successful completion,
9430 Note that this function possibly modifies the ARGS parameter, so
9431 it's the responsibility of the caller to restore it. */
9434 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9437 tree args
/* This parm gets
9439 tsubst_flags_t complain
,
9443 bool ith_elem_is_expansion
= false;
9445 /* For each parameter pack, change the substitution of the parameter
9446 pack to the ith argument in its argument pack, then expand the
9448 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9450 tree parm
= TREE_PURPOSE (pack
);
9451 tree arg_pack
= TREE_VALUE (pack
);
9452 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9454 ith_elem_is_expansion
|=
9455 argument_pack_element_is_expansion_p (arg_pack
, index
);
9457 /* Select the Ith argument from the pack. */
9458 if (TREE_CODE (parm
) == PARM_DECL
9459 || TREE_CODE (parm
) == FIELD_DECL
)
9463 aps
= make_argument_pack_select (arg_pack
, index
);
9465 register_local_specialization (aps
, parm
);
9468 aps
= retrieve_local_specialization (parm
);
9473 template_parm_level_and_index (parm
, &level
, &idx
);
9477 aps
= make_argument_pack_select (arg_pack
, index
);
9478 /* Update the corresponding argument. */
9479 TMPL_ARG (args
, level
, idx
) = aps
;
9482 /* Re-use the ARGUMENT_PACK_SELECT. */
9483 aps
= TMPL_ARG (args
, level
, idx
);
9485 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9488 /* Substitute into the PATTERN with the (possibly altered)
9490 if (!TYPE_P (pattern
))
9491 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9492 /*integral_constant_expression_p=*/false);
9494 t
= tsubst (pattern
, args
, complain
, in_decl
);
9496 /* If the Ith argument pack element is a pack expansion, then
9497 the Ith element resulting from the substituting is going to
9498 be a pack expansion as well. */
9499 if (ith_elem_is_expansion
)
9500 t
= make_pack_expansion (t
);
9505 /* Substitute ARGS into T, which is an pack expansion
9506 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9507 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9508 (if only a partial substitution could be performed) or
9509 ERROR_MARK_NODE if there was an error. */
9511 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9515 tree pack
, packs
= NULL_TREE
;
9516 bool unsubstituted_packs
= false;
9519 struct pointer_map_t
*saved_local_specializations
= NULL
;
9520 bool need_local_specializations
= false;
9523 gcc_assert (PACK_EXPANSION_P (t
));
9524 pattern
= PACK_EXPANSION_PATTERN (t
);
9526 /* Add in any args remembered from an earlier partial instantiation. */
9527 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9529 levels
= TMPL_ARGS_DEPTH (args
);
9531 /* Determine the argument packs that will instantiate the parameter
9532 packs used in the expansion expression. While we're at it,
9533 compute the number of arguments to be expanded and make sure it
9535 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9536 pack
= TREE_CHAIN (pack
))
9538 tree parm_pack
= TREE_VALUE (pack
);
9539 tree arg_pack
= NULL_TREE
;
9540 tree orig_arg
= NULL_TREE
;
9543 if (TREE_CODE (parm_pack
) == BASES
)
9545 if (BASES_DIRECT (parm_pack
))
9546 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9547 args
, complain
, in_decl
, false));
9549 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9550 args
, complain
, in_decl
, false));
9552 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9554 if (PACK_EXPANSION_LOCAL_P (t
))
9555 arg_pack
= retrieve_local_specialization (parm_pack
);
9558 /* We can't rely on local_specializations for a parameter
9559 name used later in a function declaration (such as in a
9560 late-specified return type). Even if it exists, it might
9561 have the wrong value for a recursive call. Just make a
9562 dummy decl, since it's only used for its type. */
9563 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9564 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9565 /* Partial instantiation of the parm_pack, we can't build
9566 up an argument pack yet. */
9567 arg_pack
= NULL_TREE
;
9569 arg_pack
= make_fnparm_pack (arg_pack
);
9570 need_local_specializations
= true;
9573 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9574 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9578 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9580 if (level
<= levels
)
9581 arg_pack
= TMPL_ARG (args
, level
, idx
);
9584 orig_arg
= arg_pack
;
9585 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9586 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9588 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9589 /* This can only happen if we forget to expand an argument
9590 pack somewhere else. Just return an error, silently. */
9592 result
= make_tree_vec (1);
9593 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9600 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9602 /* Don't bother trying to do a partial substitution with
9603 incomplete packs; we'll try again after deduction. */
9604 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9609 else if (len
!= my_len
)
9611 if (!(complain
& tf_error
))
9612 /* Fail quietly. */;
9613 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9614 error ("mismatched argument pack lengths while expanding "
9618 error ("mismatched argument pack lengths while expanding "
9621 return error_mark_node
;
9624 /* Keep track of the parameter packs and their corresponding
9626 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9627 TREE_TYPE (packs
) = orig_arg
;
9631 /* We can't substitute for this parameter pack. We use a flag as
9632 well as the missing_level counter because function parameter
9633 packs don't have a level. */
9634 unsubstituted_packs
= true;
9638 /* We cannot expand this expansion expression, because we don't have
9639 all of the argument packs we need. */
9640 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9642 /* We got some full packs, but we can't substitute them in until we
9643 have values for all the packs. So remember these until then. */
9645 t
= make_pack_expansion (pattern
);
9646 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9649 else if (unsubstituted_packs
)
9651 /* There were no real arguments, we're just replacing a parameter
9652 pack with another version of itself. Substitute into the
9653 pattern and return a PACK_EXPANSION_*. The caller will need to
9655 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9656 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9657 /*integral_constant_expression_p=*/false);
9659 t
= tsubst (pattern
, args
, complain
, in_decl
);
9660 t
= make_pack_expansion (t
);
9664 gcc_assert (len
>= 0);
9666 if (need_local_specializations
)
9668 /* We're in a late-specified return type, so create our own local
9669 specializations map; the current map is either NULL or (in the
9670 case of recursive unification) might have bindings that we don't
9671 want to use or alter. */
9672 saved_local_specializations
= local_specializations
;
9673 local_specializations
= pointer_map_create ();
9676 /* For each argument in each argument pack, substitute into the
9678 result
= make_tree_vec (len
);
9679 for (i
= 0; i
< len
; ++i
)
9681 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9685 TREE_VEC_ELT (result
, i
) = t
;
9686 if (t
== error_mark_node
)
9688 result
= error_mark_node
;
9693 /* Update ARGS to restore the substitution from parameter packs to
9694 their argument packs. */
9695 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9697 tree parm
= TREE_PURPOSE (pack
);
9699 if (TREE_CODE (parm
) == PARM_DECL
9700 || TREE_CODE (parm
) == FIELD_DECL
)
9701 register_local_specialization (TREE_TYPE (pack
), parm
);
9706 if (TREE_VALUE (pack
) == NULL_TREE
)
9709 template_parm_level_and_index (parm
, &level
, &idx
);
9711 /* Update the corresponding argument. */
9712 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9713 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9716 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9720 if (need_local_specializations
)
9722 pointer_map_destroy (local_specializations
);
9723 local_specializations
= saved_local_specializations
;
9729 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9730 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9731 parameter packs; all parms generated from a function parameter pack will
9732 have the same DECL_PARM_INDEX. */
9735 get_pattern_parm (tree parm
, tree tmpl
)
9737 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9740 if (DECL_ARTIFICIAL (parm
))
9742 for (patparm
= DECL_ARGUMENTS (pattern
);
9743 patparm
; patparm
= DECL_CHAIN (patparm
))
9744 if (DECL_ARTIFICIAL (patparm
)
9745 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9750 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9751 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9752 gcc_assert (DECL_PARM_INDEX (patparm
)
9753 == DECL_PARM_INDEX (parm
));
9759 /* Substitute ARGS into the vector or list of template arguments T. */
9762 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9765 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9768 if (t
== error_mark_node
)
9769 return error_mark_node
;
9771 len
= TREE_VEC_LENGTH (t
);
9772 elts
= XALLOCAVEC (tree
, len
);
9774 for (i
= 0; i
< len
; i
++)
9776 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9779 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9780 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9781 else if (PACK_EXPANSION_P (orig_arg
))
9783 /* Substitute into an expansion expression. */
9784 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9786 if (TREE_CODE (new_arg
) == TREE_VEC
)
9787 /* Add to the expanded length adjustment the number of
9788 expanded arguments. We subtract one from this
9789 measurement, because the argument pack expression
9790 itself is already counted as 1 in
9791 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9792 the argument pack is empty. */
9793 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9795 else if (ARGUMENT_PACK_P (orig_arg
))
9797 /* Substitute into each of the arguments. */
9798 new_arg
= TYPE_P (orig_arg
)
9799 ? cxx_make_type (TREE_CODE (orig_arg
))
9800 : make_node (TREE_CODE (orig_arg
));
9802 SET_ARGUMENT_PACK_ARGS (
9804 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9805 args
, complain
, in_decl
));
9807 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9808 new_arg
= error_mark_node
;
9810 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9811 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9813 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9815 if (TREE_TYPE (new_arg
) == error_mark_node
)
9816 new_arg
= error_mark_node
;
9820 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9822 if (new_arg
== error_mark_node
)
9823 return error_mark_node
;
9826 if (new_arg
!= orig_arg
)
9833 /* Make space for the expanded arguments coming from template
9835 t
= make_tree_vec (len
+ expanded_len_adjust
);
9836 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9837 arguments for a member template.
9838 In that case each TREE_VEC in ORIG_T represents a level of template
9839 arguments, and ORIG_T won't carry any non defaulted argument count.
9840 It will rather be the nested TREE_VECs that will carry one.
9841 In other words, ORIG_T carries a non defaulted argument count only
9842 if it doesn't contain any nested TREE_VEC. */
9843 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9845 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9846 count
+= expanded_len_adjust
;
9847 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9849 for (i
= 0, out
= 0; i
< len
; i
++)
9851 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9852 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9853 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9857 /* Now expand the template argument pack "in place". */
9858 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9859 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9863 TREE_VEC_ELT (t
, out
) = elts
[i
];
9871 /* Return the result of substituting ARGS into the template parameters
9872 given by PARMS. If there are m levels of ARGS and m + n levels of
9873 PARMS, then the result will contain n levels of PARMS. For
9874 example, if PARMS is `template <class T> template <class U>
9875 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9876 result will be `template <int*, double, class V>'. */
9879 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9884 /* When substituting into a template, we must set
9885 PROCESSING_TEMPLATE_DECL as the template parameters may be
9886 dependent if they are based on one-another, and the dependency
9887 predicates are short-circuit outside of templates. */
9888 ++processing_template_decl
;
9890 for (new_parms
= &r
;
9891 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9892 new_parms
= &(TREE_CHAIN (*new_parms
)),
9893 parms
= TREE_CHAIN (parms
))
9896 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9899 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9903 if (parms
== error_mark_node
)
9906 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9908 if (tuple
== error_mark_node
)
9911 TREE_VEC_ELT (new_vec
, i
) =
9912 tsubst_template_parm (tuple
, args
, complain
);
9916 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9917 - TMPL_ARGS_DEPTH (args
)),
9918 new_vec
, NULL_TREE
);
9921 --processing_template_decl
;
9926 /* Return the result of substituting ARGS into one template parameter
9927 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9928 parameter and which TREE_PURPOSE is the default argument of the
9929 template parameter. */
9932 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9934 tree default_value
, parm_decl
;
9936 if (args
== NULL_TREE
9938 || t
== error_mark_node
)
9941 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9943 default_value
= TREE_PURPOSE (t
);
9944 parm_decl
= TREE_VALUE (t
);
9946 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9947 if (TREE_CODE (parm_decl
) == PARM_DECL
9948 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9949 parm_decl
= error_mark_node
;
9950 default_value
= tsubst_template_arg (default_value
, args
,
9951 complain
, NULL_TREE
);
9953 return build_tree_list (default_value
, parm_decl
);
9956 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9957 type T. If T is not an aggregate or enumeration type, it is
9958 handled as if by tsubst. IN_DECL is as for tsubst. If
9959 ENTERING_SCOPE is nonzero, T is the context for a template which
9960 we are presently tsubst'ing. Return the substituted value. */
9963 tsubst_aggr_type (tree t
,
9965 tsubst_flags_t complain
,
9972 switch (TREE_CODE (t
))
9975 if (TYPE_PTRMEMFUNC_P (t
))
9976 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9978 /* Else fall through. */
9981 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9986 int saved_unevaluated_operand
;
9987 int saved_inhibit_evaluation_warnings
;
9989 /* In "sizeof(X<I>)" we need to evaluate "I". */
9990 saved_unevaluated_operand
= cp_unevaluated_operand
;
9991 cp_unevaluated_operand
= 0;
9992 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9993 c_inhibit_evaluation_warnings
= 0;
9995 /* First, determine the context for the type we are looking
9997 context
= TYPE_CONTEXT (t
);
9998 if (context
&& TYPE_P (context
))
10000 context
= tsubst_aggr_type (context
, args
, complain
,
10001 in_decl
, /*entering_scope=*/1);
10002 /* If context is a nested class inside a class template,
10003 it may still need to be instantiated (c++/33959). */
10004 context
= complete_type (context
);
10007 /* Then, figure out what arguments are appropriate for the
10008 type we are trying to find. For example, given:
10010 template <class T> struct S;
10011 template <class T, class U> void f(T, U) { S<U> su; }
10013 and supposing that we are instantiating f<int, double>,
10014 then our ARGS will be {int, double}, but, when looking up
10015 S we only want {double}. */
10016 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10017 complain
, in_decl
);
10018 if (argvec
== error_mark_node
)
10019 r
= error_mark_node
;
10022 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10023 entering_scope
, complain
);
10024 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10027 cp_unevaluated_operand
= saved_unevaluated_operand
;
10028 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10033 /* This is not a template type, so there's nothing to do. */
10037 return tsubst (t
, args
, complain
, in_decl
);
10041 /* Substitute into the default argument ARG (a default argument for
10042 FN), which has the indicated TYPE. */
10045 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10047 tree saved_class_ptr
= NULL_TREE
;
10048 tree saved_class_ref
= NULL_TREE
;
10049 int errs
= errorcount
+ sorrycount
;
10051 /* This can happen in invalid code. */
10052 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10055 /* This default argument came from a template. Instantiate the
10056 default argument here, not in tsubst. In the case of
10065 we must be careful to do name lookup in the scope of S<T>,
10066 rather than in the current class. */
10067 push_access_scope (fn
);
10068 /* The "this" pointer is not valid in a default argument. */
10071 saved_class_ptr
= current_class_ptr
;
10072 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10073 saved_class_ref
= current_class_ref
;
10074 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10077 push_deferring_access_checks(dk_no_deferred
);
10078 /* The default argument expression may cause implicitly defined
10079 member functions to be synthesized, which will result in garbage
10080 collection. We must treat this situation as if we were within
10081 the body of function so as to avoid collecting live data on the
10084 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10085 complain
, NULL_TREE
,
10086 /*integral_constant_expression_p=*/false);
10088 pop_deferring_access_checks();
10090 /* Restore the "this" pointer. */
10093 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10094 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10097 if (errorcount
+sorrycount
> errs
10098 && (complain
& tf_warning_or_error
))
10099 inform (input_location
,
10100 " when instantiating default argument for call to %D", fn
);
10102 /* Make sure the default argument is reasonable. */
10103 arg
= check_default_argument (type
, arg
, complain
);
10105 pop_access_scope (fn
);
10110 /* Substitute into all the default arguments for FN. */
10113 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10118 tmpl_args
= DECL_TI_ARGS (fn
);
10120 /* If this function is not yet instantiated, we certainly don't need
10121 its default arguments. */
10122 if (uses_template_parms (tmpl_args
))
10124 /* Don't do this again for clones. */
10125 if (DECL_CLONED_FUNCTION_P (fn
))
10128 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10130 arg
= TREE_CHAIN (arg
))
10131 if (TREE_PURPOSE (arg
))
10132 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10134 TREE_PURPOSE (arg
),
10138 /* Substitute the ARGS into the T, which is a _DECL. Return the
10139 result of the substitution. Issue error and warning messages under
10140 control of COMPLAIN. */
10143 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10145 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10146 location_t saved_loc
;
10147 tree r
= NULL_TREE
;
10149 hashval_t hash
= 0;
10151 /* Set the filename and linenumber to improve error-reporting. */
10152 saved_loc
= input_location
;
10153 input_location
= DECL_SOURCE_LOCATION (t
);
10155 switch (TREE_CODE (t
))
10157 case TEMPLATE_DECL
:
10159 /* We can get here when processing a member function template,
10160 member class template, or template template parameter. */
10161 tree decl
= DECL_TEMPLATE_RESULT (t
);
10166 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10168 /* Template template parameter is treated here. */
10169 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10170 if (new_type
== error_mark_node
)
10171 RETURN (error_mark_node
);
10172 /* If we get a real template back, return it. This can happen in
10173 the context of most_specialized_class. */
10174 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10178 DECL_CHAIN (r
) = NULL_TREE
;
10179 TREE_TYPE (r
) = new_type
;
10180 DECL_TEMPLATE_RESULT (r
)
10181 = build_decl (DECL_SOURCE_LOCATION (decl
),
10182 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10183 DECL_TEMPLATE_PARMS (r
)
10184 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10186 TYPE_NAME (new_type
) = r
;
10190 /* We might already have an instance of this template.
10191 The ARGS are for the surrounding class type, so the
10192 full args contain the tsubst'd args for the context,
10193 plus the innermost args from the template decl. */
10194 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10195 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10196 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10197 /* Because this is a template, the arguments will still be
10198 dependent, even after substitution. If
10199 PROCESSING_TEMPLATE_DECL is not set, the dependency
10200 predicates will short-circuit. */
10201 ++processing_template_decl
;
10202 full_args
= tsubst_template_args (tmpl_args
, args
,
10203 complain
, in_decl
);
10204 --processing_template_decl
;
10205 if (full_args
== error_mark_node
)
10206 RETURN (error_mark_node
);
10208 /* If this is a default template template argument,
10209 tsubst might not have changed anything. */
10210 if (full_args
== tmpl_args
)
10213 hash
= hash_tmpl_and_args (t
, full_args
);
10214 spec
= retrieve_specialization (t
, full_args
, hash
);
10215 if (spec
!= NULL_TREE
)
10221 /* Make a new template decl. It will be similar to the
10222 original, but will record the current template arguments.
10223 We also create a new function declaration, which is just
10224 like the old one, but points to this new template, rather
10225 than the old one. */
10227 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10228 DECL_CHAIN (r
) = NULL_TREE
;
10230 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10232 if (TREE_CODE (decl
) == TYPE_DECL
10233 && !TYPE_DECL_ALIAS_P (decl
))
10236 ++processing_template_decl
;
10237 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10238 --processing_template_decl
;
10239 if (new_type
== error_mark_node
)
10240 RETURN (error_mark_node
);
10242 TREE_TYPE (r
) = new_type
;
10243 /* For a partial specialization, we need to keep pointing to
10244 the primary template. */
10245 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10246 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10247 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10248 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10249 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10254 ++processing_template_decl
;
10255 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10256 --processing_template_decl
;
10257 if (new_decl
== error_mark_node
)
10258 RETURN (error_mark_node
);
10260 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10261 DECL_TI_TEMPLATE (new_decl
) = r
;
10262 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10263 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10264 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10267 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10268 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10269 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10271 /* The template parameters for this new template are all the
10272 template parameters for the old template, except the
10273 outermost level of parameters. */
10274 DECL_TEMPLATE_PARMS (r
)
10275 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10278 if (PRIMARY_TEMPLATE_P (t
))
10279 DECL_PRIMARY_TEMPLATE (r
) = r
;
10281 if (TREE_CODE (decl
) != TYPE_DECL
)
10282 /* Record this non-type partial instantiation. */
10283 register_specialization (r
, t
,
10284 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10289 case FUNCTION_DECL
:
10292 tree argvec
= NULL_TREE
;
10300 /* Nobody should be tsubst'ing into non-template functions. */
10301 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10303 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10308 /* If T is not dependent, just return it. We have to
10309 increment PROCESSING_TEMPLATE_DECL because
10310 value_dependent_expression_p assumes that nothing is
10311 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10312 ++processing_template_decl
;
10313 dependent_p
= value_dependent_expression_p (t
);
10314 --processing_template_decl
;
10318 /* Calculate the most general template of which R is a
10319 specialization, and the complete set of arguments used to
10321 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10322 argvec
= tsubst_template_args (DECL_TI_ARGS
10323 (DECL_TEMPLATE_RESULT
10324 (DECL_TI_TEMPLATE (t
))),
10325 args
, complain
, in_decl
);
10326 if (argvec
== error_mark_node
)
10327 RETURN (error_mark_node
);
10329 /* Check to see if we already have this specialization. */
10330 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10331 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10339 /* We can see more levels of arguments than parameters if
10340 there was a specialization of a member template, like
10343 template <class T> struct S { template <class U> void f(); }
10344 template <> template <class U> void S<int>::f(U);
10346 Here, we'll be substituting into the specialization,
10347 because that's where we can find the code we actually
10348 want to generate, but we'll have enough arguments for
10349 the most general template.
10351 We also deal with the peculiar case:
10353 template <class T> struct S {
10354 template <class U> friend void f();
10356 template <class U> void f() {}
10358 template void f<double>();
10360 Here, the ARGS for the instantiation of will be {int,
10361 double}. But, we only need as many ARGS as there are
10362 levels of template parameters in CODE_PATTERN. We are
10363 careful not to get fooled into reducing the ARGS in
10366 template <class T> struct S { template <class U> void f(U); }
10367 template <class T> template <> void S<T>::f(int) {}
10369 which we can spot because the pattern will be a
10370 specialization in this case. */
10371 args_depth
= TMPL_ARGS_DEPTH (args
);
10373 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10374 if (args_depth
> parms_depth
10375 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10376 args
= get_innermost_template_args (args
, parms_depth
);
10380 /* This special case arises when we have something like this:
10382 template <class T> struct S {
10383 friend void f<int>(int, double);
10386 Here, the DECL_TI_TEMPLATE for the friend declaration
10387 will be an IDENTIFIER_NODE. We are being called from
10388 tsubst_friend_function, and we want only to create a
10389 new decl (R) with appropriate types so that we can call
10390 determine_specialization. */
10391 gen_tmpl
= NULL_TREE
;
10394 if (DECL_CLASS_SCOPE_P (t
))
10396 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10400 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10401 complain
, t
, /*entering_scope=*/1);
10406 ctx
= DECL_CONTEXT (t
);
10408 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10409 if (type
== error_mark_node
)
10410 RETURN (error_mark_node
);
10412 /* If we hit excessive deduction depth, the type is bogus even if
10413 it isn't error_mark_node, so don't build a decl. */
10414 if (excessive_deduction_depth
)
10415 RETURN (error_mark_node
);
10417 /* We do NOT check for matching decls pushed separately at this
10418 point, as they may not represent instantiations of this
10419 template, and in any case are considered separate under the
10422 DECL_USE_TEMPLATE (r
) = 0;
10423 TREE_TYPE (r
) = type
;
10424 /* Clear out the mangled name and RTL for the instantiation. */
10425 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10426 SET_DECL_RTL (r
, NULL
);
10427 /* Leave DECL_INITIAL set on deleted instantiations. */
10428 if (!DECL_DELETED_FN (r
))
10429 DECL_INITIAL (r
) = NULL_TREE
;
10430 DECL_CONTEXT (r
) = ctx
;
10432 /* OpenMP UDRs have the only argument a reference to the declared
10433 type. We want to diagnose if the declared type is a reference,
10434 which is invalid, but as references to references are usually
10435 quietly merged, diagnose it here. */
10436 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10439 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10440 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10441 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10442 error_at (DECL_SOURCE_LOCATION (t
),
10443 "reference type %qT in "
10444 "%<#pragma omp declare reduction%>", argtype
);
10445 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10446 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10450 if (member
&& DECL_CONV_FN_P (r
))
10451 /* Type-conversion operator. Reconstruct the name, in
10452 case it's the name of one of the template's parameters. */
10453 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10455 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10457 DECL_RESULT (r
) = NULL_TREE
;
10459 TREE_STATIC (r
) = 0;
10460 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10461 DECL_EXTERNAL (r
) = 1;
10462 /* If this is an instantiation of a function with internal
10463 linkage, we already know what object file linkage will be
10464 assigned to the instantiation. */
10465 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10466 DECL_DEFER_OUTPUT (r
) = 0;
10467 DECL_CHAIN (r
) = NULL_TREE
;
10468 DECL_PENDING_INLINE_INFO (r
) = 0;
10469 DECL_PENDING_INLINE_P (r
) = 0;
10470 DECL_SAVED_TREE (r
) = NULL_TREE
;
10471 DECL_STRUCT_FUNCTION (r
) = NULL
;
10473 /* We'll re-clone as appropriate in instantiate_template. */
10474 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10476 /* If we aren't complaining now, return on error before we register
10477 the specialization so that we'll complain eventually. */
10478 if ((complain
& tf_error
) == 0
10479 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10480 && !grok_op_properties (r
, /*complain=*/false))
10481 RETURN (error_mark_node
);
10483 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10484 this in the special friend case mentioned above where
10485 GEN_TMPL is NULL. */
10488 DECL_TEMPLATE_INFO (r
)
10489 = build_template_info (gen_tmpl
, argvec
);
10490 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10493 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10495 /* We instantiated this while substituting into
10496 the type earlier (template/friend54.C). */
10499 /* We're not supposed to instantiate default arguments
10500 until they are called, for a template. But, for a
10503 template <class T> void f ()
10504 { extern void g(int i = T()); }
10506 we should do the substitution when the template is
10507 instantiated. We handle the member function case in
10508 instantiate_class_template since the default arguments
10509 might refer to other members of the class. */
10511 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10512 && !uses_template_parms (argvec
))
10513 tsubst_default_arguments (r
, complain
);
10516 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10518 /* Copy the list of befriending classes. */
10519 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10521 friends
= &TREE_CHAIN (*friends
))
10523 *friends
= copy_node (*friends
);
10524 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10529 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10531 maybe_retrofit_in_chrg (r
);
10532 if (DECL_CONSTRUCTOR_P (r
))
10533 grok_ctor_properties (ctx
, r
);
10534 if (DECL_INHERITED_CTOR_BASE (r
))
10535 deduce_inheriting_ctor (r
);
10536 /* If this is an instantiation of a member template, clone it.
10537 If it isn't, that'll be handled by
10538 clone_constructors_and_destructors. */
10539 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10540 clone_function_decl (r
, /*update_method_vec_p=*/0);
10542 else if ((complain
& tf_error
) != 0
10543 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10544 && !grok_op_properties (r
, /*complain=*/true))
10545 RETURN (error_mark_node
);
10547 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10548 SET_DECL_FRIEND_CONTEXT (r
,
10549 tsubst (DECL_FRIEND_CONTEXT (t
),
10550 args
, complain
, in_decl
));
10552 /* Possibly limit visibility based on template args. */
10553 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10554 if (DECL_VISIBILITY_SPECIFIED (t
))
10556 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10557 DECL_ATTRIBUTES (r
)
10558 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10560 determine_visibility (r
);
10561 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10562 && !processing_template_decl
)
10563 defaulted_late_check (r
);
10565 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10566 args
, complain
, in_decl
);
10572 tree type
= NULL_TREE
;
10574 tree expanded_types
= NULL_TREE
;
10575 tree prev_r
= NULL_TREE
;
10576 tree first_r
= NULL_TREE
;
10578 if (DECL_PACK_P (t
))
10580 /* If there is a local specialization that isn't a
10581 parameter pack, it means that we're doing a "simple"
10582 substitution from inside tsubst_pack_expansion. Just
10583 return the local specialization (which will be a single
10585 tree spec
= retrieve_local_specialization (t
);
10587 && TREE_CODE (spec
) == PARM_DECL
10588 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10591 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10592 the parameters in this function parameter pack. */
10593 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10594 complain
, in_decl
);
10595 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10597 len
= TREE_VEC_LENGTH (expanded_types
);
10599 /* Zero-length parameter packs are boring. Just substitute
10602 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10607 /* All we did was update the type. Make a note of that. */
10608 type
= expanded_types
;
10609 expanded_types
= NULL_TREE
;
10613 /* Loop through all of the parameters we'll build. When T is
10614 a function parameter pack, LEN is the number of expanded
10615 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10617 for (i
= 0; i
< len
; ++i
)
10621 if (DECL_TEMPLATE_PARM_P (t
))
10622 SET_DECL_TEMPLATE_PARM_P (r
);
10624 if (expanded_types
)
10625 /* We're on the Ith parameter of the function parameter
10628 /* Get the Ith type. */
10629 type
= TREE_VEC_ELT (expanded_types
, i
);
10631 /* Rename the parameter to include the index. */
10633 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10636 /* We're dealing with a normal parameter. */
10637 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10639 type
= type_decays_to (type
);
10640 TREE_TYPE (r
) = type
;
10641 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10643 if (DECL_INITIAL (r
))
10645 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10646 DECL_INITIAL (r
) = TREE_TYPE (r
);
10648 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10649 complain
, in_decl
);
10652 DECL_CONTEXT (r
) = NULL_TREE
;
10654 if (!DECL_TEMPLATE_PARM_P (r
))
10655 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10657 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10658 args
, complain
, in_decl
);
10660 /* Keep track of the first new parameter we
10661 generate. That's what will be returned to the
10666 /* Build a proper chain of parameters when substituting
10667 into a function parameter pack. */
10669 DECL_CHAIN (prev_r
) = r
;
10672 /* If cp_unevaluated_operand is set, we're just looking for a
10673 single dummy parameter, so don't keep going. */
10674 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10675 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10676 complain
, DECL_CHAIN (t
));
10678 /* FIRST_R contains the start of the chain we've built. */
10685 tree type
= NULL_TREE
;
10686 tree vec
= NULL_TREE
;
10687 tree expanded_types
= NULL_TREE
;
10690 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10692 /* This field is a lambda capture pack. Return a TREE_VEC of
10693 the expanded fields to instantiate_class_template_1 and
10694 store them in the specializations hash table as a
10695 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10696 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10697 complain
, in_decl
);
10698 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10700 len
= TREE_VEC_LENGTH (expanded_types
);
10701 vec
= make_tree_vec (len
);
10705 /* All we did was update the type. Make a note of that. */
10706 type
= expanded_types
;
10707 expanded_types
= NULL_TREE
;
10711 for (int i
= 0; i
< len
; ++i
)
10714 if (expanded_types
)
10716 type
= TREE_VEC_ELT (expanded_types
, i
);
10718 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10721 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10723 if (type
== error_mark_node
)
10724 RETURN (error_mark_node
);
10725 TREE_TYPE (r
) = type
;
10726 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10728 if (DECL_C_BIT_FIELD (r
))
10729 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10730 non-bit-fields DECL_INITIAL is a non-static data member
10731 initializer, which gets deferred instantiation. */
10733 = tsubst_expr (DECL_INITIAL (t
), args
,
10735 /*integral_constant_expression_p=*/true);
10736 else if (DECL_INITIAL (t
))
10738 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10739 NSDMI in perform_member_init. Still set DECL_INITIAL
10740 so that we know there is one. */
10741 DECL_INITIAL (r
) = void_zero_node
;
10742 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10743 retrofit_lang_decl (r
);
10744 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10746 /* We don't have to set DECL_CONTEXT here; it is set by
10747 finish_member_declaration. */
10748 DECL_CHAIN (r
) = NULL_TREE
;
10750 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10751 args
, complain
, in_decl
);
10754 TREE_VEC_ELT (vec
, i
) = r
;
10760 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10761 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10762 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10763 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10764 TREE_TYPE (pack
) = tpack
;
10765 register_specialization (pack
, t
, args
, false, 0);
10771 /* We reach here only for member using decls. We also need to check
10772 uses_template_parms because DECL_DEPENDENT_P is not set for a
10773 using-declaration that designates a member of the current
10774 instantiation (c++/53549). */
10775 if (DECL_DEPENDENT_P (t
)
10776 || uses_template_parms (USING_DECL_SCOPE (t
)))
10778 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10779 complain
, in_decl
);
10780 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10781 r
= do_class_using_decl (inst_scope
, name
);
10783 r
= error_mark_node
;
10786 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10787 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10793 DECL_CHAIN (r
) = NULL_TREE
;
10800 tree argvec
= NULL_TREE
;
10801 tree gen_tmpl
= NULL_TREE
;
10803 tree tmpl
= NULL_TREE
;
10805 tree type
= NULL_TREE
;
10808 if (TREE_CODE (t
) == TYPE_DECL
10809 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10811 /* If this is the canonical decl, we don't have to
10812 mess with instantiations, and often we can't (for
10813 typename, template type parms and such). Note that
10814 TYPE_NAME is not correct for the above test if
10815 we've copied the type for a typedef. */
10816 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10817 if (type
== error_mark_node
)
10818 RETURN (error_mark_node
);
10819 r
= TYPE_NAME (type
);
10823 /* Check to see if we already have the specialization we
10826 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10828 /* T is a static data member or namespace-scope entity.
10829 We have to substitute into namespace-scope variables
10830 (even though such entities are never templates) because
10833 template <class T> void f() { extern T t; }
10835 where the entity referenced is not known until
10836 instantiation time. */
10838 ctx
= DECL_CONTEXT (t
);
10839 if (DECL_CLASS_SCOPE_P (t
))
10841 ctx
= tsubst_aggr_type (ctx
, args
,
10843 in_decl
, /*entering_scope=*/1);
10844 /* If CTX is unchanged, then T is in fact the
10845 specialization we want. That situation occurs when
10846 referencing a static data member within in its own
10847 class. We can use pointer equality, rather than
10848 same_type_p, because DECL_CONTEXT is always
10850 if (ctx
== DECL_CONTEXT (t
)
10851 && (TREE_CODE (t
) != TYPE_DECL
10852 /* ... unless T is a member template; in which
10853 case our caller can be willing to create a
10854 specialization of that template represented
10856 || !(DECL_TI_TEMPLATE (t
)
10857 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10863 tmpl
= DECL_TI_TEMPLATE (t
);
10864 gen_tmpl
= most_general_template (tmpl
);
10865 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10866 if (argvec
== error_mark_node
)
10867 RETURN (error_mark_node
);
10868 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10869 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10874 /* A local variable. */
10876 /* Subsequent calls to pushdecl will fill this in. */
10878 spec
= retrieve_local_specialization (t
);
10880 /* If we already have the specialization we need, there is
10881 nothing more to do. */
10888 /* Create a new node for the specialization we need. */
10890 if (type
== NULL_TREE
)
10892 if (is_typedef_decl (t
))
10893 type
= DECL_ORIGINAL_TYPE (t
);
10895 type
= TREE_TYPE (t
);
10897 && VAR_HAD_UNKNOWN_BOUND (t
)
10898 && type
!= error_mark_node
)
10899 type
= strip_array_domain (type
);
10900 type
= tsubst (type
, args
, complain
, in_decl
);
10904 /* Even if the original location is out of scope, the
10905 newly substituted one is not. */
10906 DECL_DEAD_FOR_LOCAL (r
) = 0;
10907 DECL_INITIALIZED_P (r
) = 0;
10908 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10909 if (type
== error_mark_node
)
10910 RETURN (error_mark_node
);
10911 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10913 /* It may seem that this case cannot occur, since:
10918 declares a function, not a variable. However:
10921 template <typename T> void g() { T t; }
10922 template void g<f>();
10924 is an attempt to declare a variable with function
10926 error ("variable %qD has function type",
10927 /* R is not yet sufficiently initialized, so we
10928 just use its name. */
10930 RETURN (error_mark_node
);
10932 type
= complete_type (type
);
10933 /* Wait until cp_finish_decl to set this again, to handle
10934 circular dependency (template/instantiate6.C). */
10935 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10936 type
= check_var_type (DECL_NAME (r
), type
);
10938 if (DECL_HAS_VALUE_EXPR_P (t
))
10940 tree ve
= DECL_VALUE_EXPR (t
);
10941 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10942 /*constant_expression_p=*/false);
10943 if (REFERENCE_REF_P (ve
))
10945 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10946 ve
= TREE_OPERAND (ve
, 0);
10948 SET_DECL_VALUE_EXPR (r
, ve
);
10951 else if (DECL_SELF_REFERENCE_P (t
))
10952 SET_DECL_SELF_REFERENCE_P (r
);
10953 TREE_TYPE (r
) = type
;
10954 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10955 DECL_CONTEXT (r
) = ctx
;
10956 /* Clear out the mangled name and RTL for the instantiation. */
10957 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10958 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10959 SET_DECL_RTL (r
, NULL
);
10960 /* The initializer must not be expanded until it is required;
10961 see [temp.inst]. */
10962 DECL_INITIAL (r
) = NULL_TREE
;
10963 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10964 SET_DECL_RTL (r
, NULL
);
10965 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10968 /* Possibly limit visibility based on template args. */
10969 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10970 if (DECL_VISIBILITY_SPECIFIED (t
))
10972 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10973 DECL_ATTRIBUTES (r
)
10974 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10976 determine_visibility (r
);
10981 /* A static data member declaration is always marked
10982 external when it is declared in-class, even if an
10983 initializer is present. We mimic the non-template
10984 processing here. */
10985 DECL_EXTERNAL (r
) = 1;
10987 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10988 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10989 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10991 else if (cp_unevaluated_operand
)
10992 gcc_unreachable ();
10994 register_local_specialization (r
, t
);
10996 DECL_CHAIN (r
) = NULL_TREE
;
10998 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11000 args
, complain
, in_decl
);
11002 /* Preserve a typedef that names a type. */
11003 if (is_typedef_decl (r
))
11005 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11006 set_underlying_type (r
);
11009 layout_decl (r
, 0);
11014 gcc_unreachable ();
11019 /* Restore the file and line information. */
11020 input_location
= saved_loc
;
11025 /* Substitute into the ARG_TYPES of a function type.
11026 If END is a TREE_CHAIN, leave it and any following types
11030 tsubst_arg_types (tree arg_types
,
11033 tsubst_flags_t complain
,
11036 tree remaining_arg_types
;
11037 tree type
= NULL_TREE
;
11039 tree expanded_args
= NULL_TREE
;
11042 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11045 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11046 args
, end
, complain
, in_decl
);
11047 if (remaining_arg_types
== error_mark_node
)
11048 return error_mark_node
;
11050 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11052 /* For a pack expansion, perform substitution on the
11053 entire expression. Later on, we'll handle the arguments
11055 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11056 args
, complain
, in_decl
);
11058 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11059 /* So that we'll spin through the parameters, one by one. */
11060 i
= TREE_VEC_LENGTH (expanded_args
);
11063 /* We only partially substituted into the parameter
11064 pack. Our type is TYPE_PACK_EXPANSION. */
11065 type
= expanded_args
;
11066 expanded_args
= NULL_TREE
;
11074 type
= TREE_VEC_ELT (expanded_args
, i
);
11076 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11078 if (type
== error_mark_node
)
11079 return error_mark_node
;
11080 if (VOID_TYPE_P (type
))
11082 if (complain
& tf_error
)
11084 error ("invalid parameter type %qT", type
);
11086 error ("in declaration %q+D", in_decl
);
11088 return error_mark_node
;
11091 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11092 return error_mark_node
;
11094 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11095 top-level qualifiers as required. */
11096 type
= cv_unqualified (type_decays_to (type
));
11098 /* We do not substitute into default arguments here. The standard
11099 mandates that they be instantiated only when needed, which is
11100 done in build_over_call. */
11101 default_arg
= TREE_PURPOSE (arg_types
);
11103 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11105 /* We've instantiated a template before its default arguments
11106 have been parsed. This can happen for a nested template
11107 class, and is not an error unless we require the default
11108 argument in a call of this function. */
11109 remaining_arg_types
=
11110 tree_cons (default_arg
, type
, remaining_arg_types
);
11111 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11114 remaining_arg_types
=
11115 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11118 return remaining_arg_types
;
11121 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11122 *not* handle the exception-specification for FNTYPE, because the
11123 initial substitution of explicitly provided template parameters
11124 during argument deduction forbids substitution into the
11125 exception-specification:
11129 All references in the function type of the function template to the
11130 corresponding template parameters are replaced by the specified tem-
11131 plate argument values. If a substitution in a template parameter or
11132 in the function type of the function template results in an invalid
11133 type, type deduction fails. [Note: The equivalent substitution in
11134 exception specifications is done only when the function is instanti-
11135 ated, at which point a program is ill-formed if the substitution
11136 results in an invalid type.] */
11139 tsubst_function_type (tree t
,
11141 tsubst_flags_t complain
,
11148 /* The TYPE_CONTEXT is not used for function/method types. */
11149 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11151 /* Substitute the return type. */
11152 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11153 if (return_type
== error_mark_node
)
11154 return error_mark_node
;
11155 /* DR 486 clarifies that creation of a function type with an
11156 invalid return type is a deduction failure. */
11157 if (TREE_CODE (return_type
) == ARRAY_TYPE
11158 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11160 if (complain
& tf_error
)
11162 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11163 error ("function returning an array");
11165 error ("function returning a function");
11167 return error_mark_node
;
11170 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11171 return error_mark_node
;
11173 /* Substitute the argument types. */
11174 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11175 complain
, in_decl
);
11176 if (arg_types
== error_mark_node
)
11177 return error_mark_node
;
11179 /* Construct a new type node and return it. */
11180 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11182 fntype
= build_function_type (return_type
, arg_types
);
11183 fntype
= apply_memfn_quals (fntype
,
11184 type_memfn_quals (t
),
11185 type_memfn_rqual (t
));
11189 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11190 if (! MAYBE_CLASS_TYPE_P (r
))
11194 Type deduction may fail for any of the following
11197 -- Attempting to create "pointer to member of T" when T
11198 is not a class type. */
11199 if (complain
& tf_error
)
11200 error ("creating pointer to member function of non-class type %qT",
11202 return error_mark_node
;
11205 fntype
= build_method_type_directly (r
, return_type
,
11206 TREE_CHAIN (arg_types
));
11207 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11209 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11214 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11215 ARGS into that specification, and return the substituted
11216 specification. If there is no specification, return NULL_TREE. */
11219 tsubst_exception_specification (tree fntype
,
11221 tsubst_flags_t complain
,
11228 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11229 new_specs
= NULL_TREE
;
11230 if (specs
&& TREE_PURPOSE (specs
))
11232 /* A noexcept-specifier. */
11233 tree expr
= TREE_PURPOSE (specs
);
11234 if (TREE_CODE (expr
) == INTEGER_CST
)
11238 /* Defer instantiation of noexcept-specifiers to avoid
11239 excessive instantiations (c++/49107). */
11240 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11241 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11243 /* We already partially instantiated this member template,
11244 so combine the new args with the old. */
11245 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11246 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11247 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11248 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11252 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11253 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11257 new_specs
= tsubst_copy_and_build
11258 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11259 /*integral_constant_expression_p=*/true);
11260 new_specs
= build_noexcept_spec (new_specs
, complain
);
11264 if (! TREE_VALUE (specs
))
11271 tree expanded_specs
= NULL_TREE
;
11273 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11275 /* Expand the pack expansion type. */
11276 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11280 if (expanded_specs
== error_mark_node
)
11281 return error_mark_node
;
11282 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11283 len
= TREE_VEC_LENGTH (expanded_specs
);
11286 /* We're substituting into a member template, so
11287 we got a TYPE_PACK_EXPANSION back. Add that
11288 expansion and move on. */
11289 gcc_assert (TREE_CODE (expanded_specs
)
11290 == TYPE_PACK_EXPANSION
);
11291 new_specs
= add_exception_specifier (new_specs
,
11294 specs
= TREE_CHAIN (specs
);
11299 for (i
= 0; i
< len
; ++i
)
11301 if (expanded_specs
)
11302 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11304 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11305 if (spec
== error_mark_node
)
11307 new_specs
= add_exception_specifier (new_specs
, spec
,
11311 specs
= TREE_CHAIN (specs
);
11317 /* Take the tree structure T and replace template parameters used
11318 therein with the argument vector ARGS. IN_DECL is an associated
11319 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11320 Issue error and warning messages under control of COMPLAIN. Note
11321 that we must be relatively non-tolerant of extensions here, in
11322 order to preserve conformance; if we allow substitutions that
11323 should not be allowed, we may allow argument deductions that should
11324 not succeed, and therefore report ambiguous overload situations
11325 where there are none. In theory, we could allow the substitution,
11326 but indicate that it should have failed, and allow our caller to
11327 make sure that the right thing happens, but we don't try to do this
11330 This function is used for dealing with types, decls and the like;
11331 for expressions, use tsubst_expr or tsubst_copy. */
11334 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11336 enum tree_code code
;
11337 tree type
, r
= NULL_TREE
;
11339 if (t
== NULL_TREE
|| t
== error_mark_node
11340 || t
== integer_type_node
11341 || t
== void_type_node
11342 || t
== char_type_node
11343 || t
== unknown_type_node
11344 || TREE_CODE (t
) == NAMESPACE_DECL
11345 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11349 return tsubst_decl (t
, args
, complain
);
11351 if (args
== NULL_TREE
)
11354 code
= TREE_CODE (t
);
11356 if (code
== IDENTIFIER_NODE
)
11357 type
= IDENTIFIER_TYPE_VALUE (t
);
11359 type
= TREE_TYPE (t
);
11361 gcc_assert (type
!= unknown_type_node
);
11363 /* Reuse typedefs. We need to do this to handle dependent attributes,
11364 such as attribute aligned. */
11366 && typedef_variant_p (t
))
11368 tree decl
= TYPE_NAME (t
);
11370 if (alias_template_specialization_p (t
))
11372 /* DECL represents an alias template and we want to
11374 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11375 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11376 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11378 else if (DECL_CLASS_SCOPE_P (decl
)
11379 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11380 && uses_template_parms (DECL_CONTEXT (decl
)))
11382 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11383 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11384 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11386 else if (DECL_FUNCTION_SCOPE_P (decl
)
11387 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11388 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11389 r
= retrieve_local_specialization (decl
);
11391 /* The typedef is from a non-template context. */
11397 r
= cp_build_qualified_type_real
11398 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11399 complain
| tf_ignore_bad_quals
);
11404 /* We don't have an instantiation yet, so drop the typedef. */
11405 int quals
= cp_type_quals (t
);
11406 t
= DECL_ORIGINAL_TYPE (decl
);
11407 t
= cp_build_qualified_type_real (t
, quals
,
11408 complain
| tf_ignore_bad_quals
);
11413 && code
!= TYPENAME_TYPE
11414 && code
!= TEMPLATE_TYPE_PARM
11415 && code
!= IDENTIFIER_NODE
11416 && code
!= FUNCTION_TYPE
11417 && code
!= METHOD_TYPE
)
11418 type
= tsubst (type
, args
, complain
, in_decl
);
11419 if (type
== error_mark_node
)
11420 return error_mark_node
;
11426 case ENUMERAL_TYPE
:
11427 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11428 /*entering_scope=*/0);
11431 case IDENTIFIER_NODE
:
11442 if (t
== integer_type_node
)
11445 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11446 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11450 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11452 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11453 /*integral_constant_expression_p=*/false);
11455 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11457 if (TREE_CODE (max
) == NOP_EXPR
11458 && TREE_SIDE_EFFECTS (omax
)
11459 && !TREE_TYPE (max
))
11460 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11462 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11463 with TREE_SIDE_EFFECTS that indicates this is not an integral
11464 constant expression. */
11465 if (processing_template_decl
11466 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11468 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11469 TREE_SIDE_EFFECTS (max
) = 1;
11472 return compute_array_index_type (NULL_TREE
, max
, complain
);
11475 case TEMPLATE_TYPE_PARM
:
11476 case TEMPLATE_TEMPLATE_PARM
:
11477 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11478 case TEMPLATE_PARM_INDEX
:
11483 tree arg
= NULL_TREE
;
11487 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11488 template_parm_level_and_index (t
, &level
, &idx
);
11490 levels
= TMPL_ARGS_DEPTH (args
);
11491 if (level
<= levels
)
11493 arg
= TMPL_ARG (args
, level
, idx
);
11495 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11497 /* See through ARGUMENT_PACK_SELECT arguments. */
11498 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11499 /* If the selected argument is an expansion E, that most
11500 likely means we were called from
11501 gen_elem_of_pack_expansion_instantiation during the
11502 substituting of pack an argument pack (which Ith
11503 element is a pack expansion, where I is
11504 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11505 In this case, the Ith element resulting from this
11506 substituting is going to be a pack expansion, which
11507 pattern is the pattern of E. Let's return the
11509 gen_elem_of_pack_expansion_instantiation will
11510 build the resulting pack expansion from it. */
11511 if (PACK_EXPANSION_P (arg
))
11512 arg
= PACK_EXPANSION_PATTERN (arg
);
11516 if (arg
== error_mark_node
)
11517 return error_mark_node
;
11518 else if (arg
!= NULL_TREE
)
11520 if (ARGUMENT_PACK_P (arg
))
11521 /* If ARG is an argument pack, we don't actually want to
11522 perform a substitution here, because substitutions
11523 for argument packs are only done
11524 element-by-element. We can get to this point when
11525 substituting the type of a non-type template
11526 parameter pack, when that type actually contains
11527 template parameter packs from an outer template, e.g.,
11529 template<typename... Types> struct A {
11530 template<Types... Values> struct B { };
11534 if (code
== TEMPLATE_TYPE_PARM
)
11537 gcc_assert (TYPE_P (arg
));
11539 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11541 return cp_build_qualified_type_real
11542 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11544 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11546 /* We are processing a type constructed from a
11547 template template parameter. */
11548 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11549 args
, complain
, in_decl
);
11550 if (argvec
== error_mark_node
)
11551 return error_mark_node
;
11553 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11554 || TREE_CODE (arg
) == TEMPLATE_DECL
11555 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11557 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11558 /* Consider this code:
11560 template <template <class> class Template>
11562 template <class Arg> using Bind = Template<Arg>;
11565 template <template <class> class Template, class Arg>
11566 using Instantiate = Template<Arg>; //#0
11568 template <template <class> class Template,
11571 Instantiate<Internal<Template>::template Bind,
11574 When #1 is parsed, the
11575 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11576 parameter `Template' in #0 matches the
11577 UNBOUND_CLASS_TEMPLATE representing the argument
11578 `Internal<Template>::template Bind'; We then want
11579 to assemble the type `Bind<Argument>' that can't
11580 be fully created right now, because
11581 `Internal<Template>' not being complete, the Bind
11582 template cannot be looked up in that context. So
11583 we need to "store" `Bind<Argument>' for later
11584 when the context of Bind becomes complete. Let's
11585 store that in a TYPENAME_TYPE. */
11586 return make_typename_type (TYPE_CONTEXT (arg
),
11587 build_nt (TEMPLATE_ID_EXPR
,
11588 TYPE_IDENTIFIER (arg
),
11593 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11594 are resolving nested-types in the signature of a
11595 member function templates. Otherwise ARG is a
11596 TEMPLATE_DECL and is the real template to be
11598 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11599 arg
= TYPE_NAME (arg
);
11601 r
= lookup_template_class (arg
,
11603 DECL_CONTEXT (arg
),
11604 /*entering_scope=*/0,
11606 return cp_build_qualified_type_real
11607 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11610 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11611 return convert_from_reference (unshare_expr (arg
));
11615 /* This can happen during the attempted tsubst'ing in
11616 unify. This means that we don't yet have any information
11617 about the template parameter in question. */
11620 /* Early in template argument deduction substitution, we don't
11621 want to reduce the level of 'auto', or it will be confused
11622 with a normal template parm in subsequent deduction. */
11623 if (is_auto (t
) && (complain
& tf_partial
))
11626 /* If we get here, we must have been looking at a parm for a
11627 more deeply nested template. Make a new version of this
11628 template parameter, but with a lower level. */
11631 case TEMPLATE_TYPE_PARM
:
11632 case TEMPLATE_TEMPLATE_PARM
:
11633 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11634 if (cp_type_quals (t
))
11636 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11637 r
= cp_build_qualified_type_real
11638 (r
, cp_type_quals (t
),
11639 complain
| (code
== TEMPLATE_TYPE_PARM
11640 ? tf_ignore_bad_quals
: 0));
11645 TEMPLATE_TYPE_PARM_INDEX (r
)
11646 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11647 r
, levels
, args
, complain
);
11648 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11649 TYPE_MAIN_VARIANT (r
) = r
;
11650 TYPE_POINTER_TO (r
) = NULL_TREE
;
11651 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11653 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11654 /* We have reduced the level of the template
11655 template parameter, but not the levels of its
11656 template parameters, so canonical_type_parameter
11657 will not be able to find the canonical template
11658 template parameter for this level. Thus, we
11659 require structural equality checking to compare
11660 TEMPLATE_TEMPLATE_PARMs. */
11661 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11662 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11663 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11665 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11667 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11669 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11670 complain
, in_decl
);
11671 if (argvec
== error_mark_node
)
11672 return error_mark_node
;
11674 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11675 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11680 case TEMPLATE_PARM_INDEX
:
11681 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11685 gcc_unreachable ();
11693 tree purpose
, value
, chain
;
11695 if (t
== void_list_node
)
11698 purpose
= TREE_PURPOSE (t
);
11701 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11702 if (purpose
== error_mark_node
)
11703 return error_mark_node
;
11705 value
= TREE_VALUE (t
);
11708 value
= tsubst (value
, args
, complain
, in_decl
);
11709 if (value
== error_mark_node
)
11710 return error_mark_node
;
11712 chain
= TREE_CHAIN (t
);
11713 if (chain
&& chain
!= void_type_node
)
11715 chain
= tsubst (chain
, args
, complain
, in_decl
);
11716 if (chain
== error_mark_node
)
11717 return error_mark_node
;
11719 if (purpose
== TREE_PURPOSE (t
)
11720 && value
== TREE_VALUE (t
)
11721 && chain
== TREE_CHAIN (t
))
11723 return hash_tree_cons (purpose
, value
, chain
);
11727 /* We should never be tsubsting a binfo. */
11728 gcc_unreachable ();
11731 /* A vector of template arguments. */
11732 gcc_assert (!type
);
11733 return tsubst_template_args (t
, args
, complain
, in_decl
);
11736 case REFERENCE_TYPE
:
11738 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11743 Type deduction may fail for any of the following
11746 -- Attempting to create a pointer to reference type.
11747 -- Attempting to create a reference to a reference type or
11748 a reference to void.
11750 Core issue 106 says that creating a reference to a reference
11751 during instantiation is no longer a cause for failure. We
11752 only enforce this check in strict C++98 mode. */
11753 if ((TREE_CODE (type
) == REFERENCE_TYPE
11754 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11755 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11757 static location_t last_loc
;
11759 /* We keep track of the last time we issued this error
11760 message to avoid spewing a ton of messages during a
11761 single bad template instantiation. */
11762 if (complain
& tf_error
11763 && last_loc
!= input_location
)
11765 if (VOID_TYPE_P (type
))
11766 error ("forming reference to void");
11767 else if (code
== POINTER_TYPE
)
11768 error ("forming pointer to reference type %qT", type
);
11770 error ("forming reference to reference type %qT", type
);
11771 last_loc
= input_location
;
11774 return error_mark_node
;
11776 else if (TREE_CODE (type
) == FUNCTION_TYPE
11777 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11778 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11780 if (complain
& tf_error
)
11782 if (code
== POINTER_TYPE
)
11783 error ("forming pointer to qualified function type %qT",
11786 error ("forming reference to qualified function type %qT",
11789 return error_mark_node
;
11791 else if (code
== POINTER_TYPE
)
11793 r
= build_pointer_type (type
);
11794 if (TREE_CODE (type
) == METHOD_TYPE
)
11795 r
= build_ptrmemfunc_type (r
);
11797 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11798 /* In C++0x, during template argument substitution, when there is an
11799 attempt to create a reference to a reference type, reference
11800 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11802 "If a template-argument for a template-parameter T names a type
11803 that is a reference to a type A, an attempt to create the type
11804 'lvalue reference to cv T' creates the type 'lvalue reference to
11805 A,' while an attempt to create the type type rvalue reference to
11806 cv T' creates the type T"
11808 r
= cp_build_reference_type
11810 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11812 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11813 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11815 if (cxx_dialect
>= cxx1y
11816 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11817 && array_of_runtime_bound_p (type
))
11819 if (complain
& tf_warning_or_error
)
11821 (input_location
, OPT_Wvla
,
11822 code
== REFERENCE_TYPE
11823 ? G_("cannot declare reference to array of runtime bound")
11824 : G_("cannot declare pointer to array of runtime bound"));
11826 r
= error_mark_node
;
11829 if (r
!= error_mark_node
)
11830 /* Will this ever be needed for TYPE_..._TO values? */
11837 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11838 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11842 Type deduction may fail for any of the following
11845 -- Attempting to create "pointer to member of T" when T
11846 is not a class type. */
11847 if (complain
& tf_error
)
11848 error ("creating pointer to member of non-class type %qT", r
);
11849 return error_mark_node
;
11851 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11853 if (complain
& tf_error
)
11854 error ("creating pointer to member reference type %qT", type
);
11855 return error_mark_node
;
11857 if (VOID_TYPE_P (type
))
11859 if (complain
& tf_error
)
11860 error ("creating pointer to member of type void");
11861 return error_mark_node
;
11863 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11864 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11866 /* The type of the implicit object parameter gets its
11867 cv-qualifiers from the FUNCTION_TYPE. */
11870 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11871 type_memfn_rqual (type
));
11872 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11873 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11877 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11881 case FUNCTION_TYPE
:
11886 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11887 if (fntype
== error_mark_node
)
11888 return error_mark_node
;
11890 /* Substitute the exception specification. */
11891 specs
= tsubst_exception_specification (t
, args
, complain
,
11892 in_decl
, /*defer_ok*/true);
11893 if (specs
== error_mark_node
)
11894 return error_mark_node
;
11896 fntype
= build_exception_variant (fntype
, specs
);
11901 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11902 if (domain
== error_mark_node
)
11903 return error_mark_node
;
11905 /* As an optimization, we avoid regenerating the array type if
11906 it will obviously be the same as T. */
11907 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11910 /* These checks should match the ones in grokdeclarator.
11914 The deduction may fail for any of the following reasons:
11916 -- Attempting to create an array with an element type that
11917 is void, a function type, or a reference type, or [DR337]
11918 an abstract class type. */
11919 if (VOID_TYPE_P (type
)
11920 || TREE_CODE (type
) == FUNCTION_TYPE
11921 || TREE_CODE (type
) == REFERENCE_TYPE
)
11923 if (complain
& tf_error
)
11924 error ("creating array of %qT", type
);
11925 return error_mark_node
;
11928 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11929 return error_mark_node
;
11931 r
= build_cplus_array_type (type
, domain
);
11933 if (TYPE_USER_ALIGN (t
))
11935 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11936 TYPE_USER_ALIGN (r
) = 1;
11942 case TYPENAME_TYPE
:
11944 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11945 in_decl
, /*entering_scope=*/1);
11946 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11947 complain
, in_decl
);
11949 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11950 return error_mark_node
;
11952 if (!MAYBE_CLASS_TYPE_P (ctx
))
11954 if (complain
& tf_error
)
11955 error ("%qT is not a class, struct, or union type", ctx
);
11956 return error_mark_node
;
11958 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11960 /* Normally, make_typename_type does not require that the CTX
11961 have complete type in order to allow things like:
11963 template <class T> struct S { typename S<T>::X Y; };
11965 But, such constructs have already been resolved by this
11966 point, so here CTX really should have complete type, unless
11967 it's a partial instantiation. */
11968 ctx
= complete_type (ctx
);
11969 if (!COMPLETE_TYPE_P (ctx
))
11971 if (complain
& tf_error
)
11972 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11973 return error_mark_node
;
11977 f
= make_typename_type (ctx
, f
, typename_type
,
11978 complain
| tf_keep_type_decl
);
11979 if (f
== error_mark_node
)
11981 if (TREE_CODE (f
) == TYPE_DECL
)
11983 complain
|= tf_ignore_bad_quals
;
11987 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11989 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11991 if (complain
& tf_error
)
11992 error ("%qT resolves to %qT, which is not an enumeration type",
11995 return error_mark_node
;
11997 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11999 if (complain
& tf_error
)
12000 error ("%qT resolves to %qT, which is is not a class type",
12003 return error_mark_node
;
12007 return cp_build_qualified_type_real
12008 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12011 case UNBOUND_CLASS_TEMPLATE
:
12013 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12014 in_decl
, /*entering_scope=*/1);
12015 tree name
= TYPE_IDENTIFIER (t
);
12016 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12018 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12019 return error_mark_node
;
12022 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12023 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12030 ++cp_unevaluated_operand
;
12031 ++c_inhibit_evaluation_warnings
;
12033 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12035 /*integral_constant_expression_p=*/false);
12037 --cp_unevaluated_operand
;
12038 --c_inhibit_evaluation_warnings
;
12040 type
= finish_typeof (type
);
12041 return cp_build_qualified_type_real (type
,
12043 | cp_type_quals (type
),
12047 case DECLTYPE_TYPE
:
12051 ++cp_unevaluated_operand
;
12052 ++c_inhibit_evaluation_warnings
;
12054 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12055 complain
|tf_decltype
, in_decl
,
12056 /*function_p*/false,
12057 /*integral_constant_expression*/false);
12059 --cp_unevaluated_operand
;
12060 --c_inhibit_evaluation_warnings
;
12062 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12063 type
= lambda_capture_field_type (type
,
12064 DECLTYPE_FOR_INIT_CAPTURE (t
));
12065 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12066 type
= lambda_proxy_type (type
);
12069 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12070 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12072 /* In a template ~id could be either a complement expression
12073 or an unqualified-id naming a destructor; if instantiating
12074 it produces an expression, it's not an id-expression or
12077 type
= finish_decltype_type (type
, id
, complain
);
12079 return cp_build_qualified_type_real (type
,
12081 | cp_type_quals (type
),
12085 case UNDERLYING_TYPE
:
12087 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12088 complain
, in_decl
);
12089 return finish_underlying_type (type
);
12092 case TYPE_ARGUMENT_PACK
:
12093 case NONTYPE_ARGUMENT_PACK
:
12095 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12097 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12101 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12103 /* For template nontype argument packs, also substitute into
12105 if (code
== NONTYPE_ARGUMENT_PACK
)
12106 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12124 /* We should use one of the expression tsubsts for these codes. */
12125 gcc_unreachable ();
12128 sorry ("use of %qs in template", get_tree_code_name (code
));
12129 return error_mark_node
;
12133 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12134 type of the expression on the left-hand side of the "." or "->"
12138 tsubst_baselink (tree baselink
, tree object_type
,
12139 tree args
, tsubst_flags_t complain
, tree in_decl
)
12142 tree qualifying_scope
;
12145 tree template_args
= 0;
12146 bool template_id_p
= false;
12147 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12149 /* A baselink indicates a function from a base class. Both the
12150 BASELINK_ACCESS_BINFO and the base class referenced may
12151 indicate bases of the template class, rather than the
12152 instantiated class. In addition, lookups that were not
12153 ambiguous before may be ambiguous now. Therefore, we perform
12154 the lookup again. */
12155 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12156 qualifying_scope
= tsubst (qualifying_scope
, args
,
12157 complain
, in_decl
);
12158 fns
= BASELINK_FUNCTIONS (baselink
);
12159 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12160 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12162 template_id_p
= true;
12163 template_args
= TREE_OPERAND (fns
, 1);
12164 fns
= TREE_OPERAND (fns
, 0);
12166 template_args
= tsubst_template_args (template_args
, args
,
12167 complain
, in_decl
);
12169 name
= DECL_NAME (get_first_fn (fns
));
12170 if (IDENTIFIER_TYPENAME_P (name
))
12171 name
= mangle_conv_op_name_for_type (optype
);
12172 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12174 return error_mark_node
;
12176 /* If lookup found a single function, mark it as used at this
12177 point. (If it lookup found multiple functions the one selected
12178 later by overload resolution will be marked as used at that
12180 if (BASELINK_P (baselink
))
12181 fns
= BASELINK_FUNCTIONS (baselink
);
12182 if (!template_id_p
&& !really_overloaded_fn (fns
))
12183 mark_used (OVL_CURRENT (fns
));
12185 /* Add back the template arguments, if present. */
12186 if (BASELINK_P (baselink
) && template_id_p
)
12187 BASELINK_FUNCTIONS (baselink
)
12188 = build_nt (TEMPLATE_ID_EXPR
,
12189 BASELINK_FUNCTIONS (baselink
),
12191 /* Update the conversion operator type. */
12192 BASELINK_OPTYPE (baselink
) = optype
;
12195 object_type
= current_class_type
;
12198 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12204 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12205 true if the qualified-id will be a postfix-expression in-and-of
12206 itself; false if more of the postfix-expression follows the
12207 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12211 tsubst_qualified_id (tree qualified_id
, tree args
,
12212 tsubst_flags_t complain
, tree in_decl
,
12213 bool done
, bool address_p
)
12219 tree template_args
;
12220 location_t loc
= UNKNOWN_LOCATION
;
12222 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12224 /* Figure out what name to look up. */
12225 name
= TREE_OPERAND (qualified_id
, 1);
12226 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12228 is_template
= true;
12229 loc
= EXPR_LOCATION (name
);
12230 template_args
= TREE_OPERAND (name
, 1);
12232 template_args
= tsubst_template_args (template_args
, args
,
12233 complain
, in_decl
);
12234 name
= TREE_OPERAND (name
, 0);
12238 is_template
= false;
12239 template_args
= NULL_TREE
;
12242 /* Substitute into the qualifying scope. When there are no ARGS, we
12243 are just trying to simplify a non-dependent expression. In that
12244 case the qualifying scope may be dependent, and, in any case,
12245 substituting will not help. */
12246 scope
= TREE_OPERAND (qualified_id
, 0);
12249 scope
= tsubst (scope
, args
, complain
, in_decl
);
12250 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12255 if (dependent_scope_p (scope
))
12258 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12259 return build_qualified_name (NULL_TREE
, scope
, expr
,
12260 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12263 if (!BASELINK_P (name
) && !DECL_P (expr
))
12265 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12267 /* A BIT_NOT_EXPR is used to represent a destructor. */
12268 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12270 error ("qualifying type %qT does not match destructor name ~%qT",
12271 scope
, TREE_OPERAND (expr
, 0));
12272 expr
= error_mark_node
;
12275 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12276 /*is_type_p=*/0, false);
12279 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12280 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12281 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12283 if (complain
& tf_error
)
12285 error ("dependent-name %qE is parsed as a non-type, but "
12286 "instantiation yields a type", qualified_id
);
12287 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12289 return error_mark_node
;
12295 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12297 /* Remember that there was a reference to this entity. */
12301 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12303 if (complain
& tf_error
)
12304 qualified_name_lookup_error (scope
,
12305 TREE_OPERAND (qualified_id
, 1),
12306 expr
, input_location
);
12307 return error_mark_node
;
12311 expr
= lookup_template_function (expr
, template_args
);
12313 if (expr
== error_mark_node
&& complain
& tf_error
)
12314 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12315 expr
, input_location
);
12316 else if (TYPE_P (scope
))
12318 expr
= (adjust_result_of_qualified_name_lookup
12319 (expr
, scope
, current_nonlambda_class_type ()));
12320 expr
= (finish_qualified_id_expr
12321 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12322 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12323 /*template_arg_p=*/false, complain
));
12326 /* Expressions do not generally have reference type. */
12327 if (TREE_CODE (expr
) != SCOPE_REF
12328 /* However, if we're about to form a pointer-to-member, we just
12329 want the referenced member referenced. */
12330 && TREE_CODE (expr
) != OFFSET_REF
)
12331 expr
= convert_from_reference (expr
);
12336 /* Like tsubst, but deals with expressions. This function just replaces
12337 template parms; to finish processing the resultant expression, use
12338 tsubst_copy_and_build or tsubst_expr. */
12341 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12343 enum tree_code code
;
12346 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12349 code
= TREE_CODE (t
);
12354 r
= retrieve_local_specialization (t
);
12356 if (r
== NULL_TREE
)
12358 /* We get here for a use of 'this' in an NSDMI. */
12359 if (DECL_NAME (t
) == this_identifier
12360 && at_function_scope_p ()
12361 && DECL_CONSTRUCTOR_P (current_function_decl
))
12362 return current_class_ptr
;
12364 /* This can happen for a parameter name used later in a function
12365 declaration (such as in a late-specified return type). Just
12366 make a dummy decl, since it's only used for its type. */
12367 gcc_assert (cp_unevaluated_operand
!= 0);
12368 r
= tsubst_decl (t
, args
, complain
);
12369 /* Give it the template pattern as its context; its true context
12370 hasn't been instantiated yet and this is good enough for
12372 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12375 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12376 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12385 if (DECL_TEMPLATE_PARM_P (t
))
12386 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12387 /* There is no need to substitute into namespace-scope
12389 if (DECL_NAMESPACE_SCOPE_P (t
))
12391 /* If ARGS is NULL, then T is known to be non-dependent. */
12392 if (args
== NULL_TREE
)
12393 return integral_constant_value (t
);
12395 /* Unfortunately, we cannot just call lookup_name here.
12398 template <int I> int f() {
12400 struct S { void g() { E e = a; } };
12403 When we instantiate f<7>::S::g(), say, lookup_name is not
12404 clever enough to find f<7>::a. */
12406 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12407 /*entering_scope=*/0);
12409 for (v
= TYPE_VALUES (enum_type
);
12411 v
= TREE_CHAIN (v
))
12412 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12413 return TREE_VALUE (v
);
12415 /* We didn't find the name. That should never happen; if
12416 name-lookup found it during preliminary parsing, we
12417 should find it again here during instantiation. */
12418 gcc_unreachable ();
12423 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12425 /* Check for a local specialization set up by
12426 tsubst_pack_expansion. */
12427 tree r
= retrieve_local_specialization (t
);
12430 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12431 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12435 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12436 tsubst_decl put in the hash table. */
12437 return retrieve_specialization (t
, args
, 0);
12440 if (DECL_CONTEXT (t
))
12444 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12445 /*entering_scope=*/1);
12446 if (ctx
!= DECL_CONTEXT (t
))
12448 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12451 if (complain
& tf_error
)
12452 error ("using invalid field %qD", t
);
12453 return error_mark_node
;
12462 case FUNCTION_DECL
:
12463 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12464 r
= tsubst (t
, args
, complain
, in_decl
);
12465 else if (local_variable_p (t
))
12467 r
= retrieve_local_specialization (t
);
12468 if (r
== NULL_TREE
)
12470 if (DECL_ANON_UNION_VAR_P (t
))
12472 /* Just use name lookup to find a member alias for an
12473 anonymous union, but then add it to the hash table. */
12474 r
= lookup_name (DECL_NAME (t
));
12475 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12476 register_local_specialization (r
, t
);
12480 gcc_assert (errorcount
|| sorrycount
);
12481 return error_mark_node
;
12490 case NAMESPACE_DECL
:
12494 /* An OVERLOAD will always be a non-dependent overload set; an
12495 overload set from function scope will just be represented with an
12496 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12497 gcc_assert (!uses_template_parms (t
));
12501 return tsubst_baselink (t
, current_nonlambda_class_type (),
12502 args
, complain
, in_decl
);
12504 case TEMPLATE_DECL
:
12505 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12506 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12507 args
, complain
, in_decl
);
12508 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12509 return tsubst (t
, args
, complain
, in_decl
);
12510 else if (DECL_CLASS_SCOPE_P (t
)
12511 && uses_template_parms (DECL_CONTEXT (t
)))
12513 /* Template template argument like the following example need
12516 template <template <class> class TT> struct C {};
12517 template <class T> struct D {
12518 template <class U> struct E {};
12523 We are processing the template argument `E' in #1 for
12524 the template instantiation #2. Originally, `E' is a
12525 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12526 have to substitute this with one having context `D<int>'. */
12528 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12529 return lookup_field (context
, DECL_NAME(t
), 0, false);
12532 /* Ordinary template template argument. */
12536 case REINTERPRET_CAST_EXPR
:
12537 case CONST_CAST_EXPR
:
12538 case STATIC_CAST_EXPR
:
12539 case DYNAMIC_CAST_EXPR
:
12540 case IMPLICIT_CONV_EXPR
:
12544 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12545 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12548 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12551 tree expanded
, op
= TREE_OPERAND (t
, 0);
12554 if (SIZEOF_EXPR_TYPE_P (t
))
12555 op
= TREE_TYPE (op
);
12557 ++cp_unevaluated_operand
;
12558 ++c_inhibit_evaluation_warnings
;
12559 /* We only want to compute the number of arguments. */
12560 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12561 --cp_unevaluated_operand
;
12562 --c_inhibit_evaluation_warnings
;
12564 if (TREE_CODE (expanded
) == TREE_VEC
)
12565 len
= TREE_VEC_LENGTH (expanded
);
12567 if (expanded
== error_mark_node
)
12568 return error_mark_node
;
12569 else if (PACK_EXPANSION_P (expanded
)
12570 || (TREE_CODE (expanded
) == TREE_VEC
12572 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12574 if (TREE_CODE (expanded
) == TREE_VEC
)
12575 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12577 if (TYPE_P (expanded
))
12578 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12579 complain
& tf_error
);
12581 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12582 complain
& tf_error
);
12585 return build_int_cst (size_type_node
, len
);
12587 if (SIZEOF_EXPR_TYPE_P (t
))
12589 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12590 args
, complain
, in_decl
);
12591 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12592 r
= build1 (SIZEOF_EXPR
,
12593 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12594 SIZEOF_EXPR_TYPE_P (r
) = 1;
12601 case TRUTH_NOT_EXPR
:
12604 case UNARY_PLUS_EXPR
: /* Unary + */
12606 case AT_ENCODE_EXPR
:
12610 case REALPART_EXPR
:
12611 case IMAGPART_EXPR
:
12614 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12615 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12617 case COMPONENT_REF
:
12622 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12623 name
= TREE_OPERAND (t
, 1);
12624 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12626 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12627 complain
, in_decl
);
12628 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12630 else if (TREE_CODE (name
) == SCOPE_REF
12631 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12633 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12634 complain
, in_decl
);
12635 name
= TREE_OPERAND (name
, 1);
12636 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12637 complain
, in_decl
);
12638 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12639 name
= build_qualified_name (/*type=*/NULL_TREE
,
12641 /*template_p=*/false);
12643 else if (BASELINK_P (name
))
12644 name
= tsubst_baselink (name
,
12645 non_reference (TREE_TYPE (object
)),
12649 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12650 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12656 case TRUNC_DIV_EXPR
:
12657 case CEIL_DIV_EXPR
:
12658 case FLOOR_DIV_EXPR
:
12659 case ROUND_DIV_EXPR
:
12660 case EXACT_DIV_EXPR
:
12664 case TRUNC_MOD_EXPR
:
12665 case FLOOR_MOD_EXPR
:
12666 case TRUTH_ANDIF_EXPR
:
12667 case TRUTH_ORIF_EXPR
:
12668 case TRUTH_AND_EXPR
:
12669 case TRUTH_OR_EXPR
:
12682 case COMPOUND_EXPR
:
12685 case PREDECREMENT_EXPR
:
12686 case PREINCREMENT_EXPR
:
12687 case POSTDECREMENT_EXPR
:
12688 case POSTINCREMENT_EXPR
:
12690 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12691 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12694 return build_qualified_name (/*type=*/NULL_TREE
,
12695 tsubst_copy (TREE_OPERAND (t
, 0),
12696 args
, complain
, in_decl
),
12697 tsubst_copy (TREE_OPERAND (t
, 1),
12698 args
, complain
, in_decl
),
12699 QUALIFIED_NAME_IS_TEMPLATE (t
));
12704 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12705 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12706 NULL_TREE
, NULL_TREE
);
12710 int n
= VL_EXP_OPERAND_LENGTH (t
);
12711 tree result
= build_vl_exp (CALL_EXPR
, n
);
12713 for (i
= 0; i
< n
; i
++)
12714 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12715 complain
, in_decl
);
12721 case PSEUDO_DTOR_EXPR
:
12722 case VEC_PERM_EXPR
:
12725 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12726 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12727 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12728 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12735 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12736 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12737 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12738 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12745 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12746 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12747 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12748 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12752 case TEMPLATE_ID_EXPR
:
12754 /* Substituted template arguments */
12755 tree fn
= TREE_OPERAND (t
, 0);
12756 tree targs
= TREE_OPERAND (t
, 1);
12758 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12760 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12762 return lookup_template_function (fn
, targs
);
12767 tree purpose
, value
, chain
;
12769 if (t
== void_list_node
)
12772 purpose
= TREE_PURPOSE (t
);
12774 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12775 value
= TREE_VALUE (t
);
12777 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12778 chain
= TREE_CHAIN (t
);
12779 if (chain
&& chain
!= void_type_node
)
12780 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12781 if (purpose
== TREE_PURPOSE (t
)
12782 && value
== TREE_VALUE (t
)
12783 && chain
== TREE_CHAIN (t
))
12785 return tree_cons (purpose
, value
, chain
);
12790 case ENUMERAL_TYPE
:
12792 case TEMPLATE_TYPE_PARM
:
12793 case TEMPLATE_TEMPLATE_PARM
:
12794 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12795 case TEMPLATE_PARM_INDEX
:
12797 case REFERENCE_TYPE
:
12799 case FUNCTION_TYPE
:
12802 case TYPENAME_TYPE
:
12803 case UNBOUND_CLASS_TEMPLATE
:
12805 case DECLTYPE_TYPE
:
12807 return tsubst (t
, args
, complain
, in_decl
);
12811 /* Fall through. */
12812 case IDENTIFIER_NODE
:
12813 if (IDENTIFIER_TYPENAME_P (t
))
12815 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12816 return mangle_conv_op_name_for_type (new_type
);
12822 /* This is handled by tsubst_copy_and_build. */
12823 gcc_unreachable ();
12826 return build_x_va_arg (EXPR_LOCATION (t
),
12827 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12829 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12831 case CLEANUP_POINT_EXPR
:
12832 /* We shouldn't have built any of these during initial template
12833 generation. Instead, they should be built during instantiation
12834 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12835 gcc_unreachable ();
12839 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12840 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12841 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12842 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12843 mark_used (TREE_OPERAND (r
, 1));
12846 case EXPR_PACK_EXPANSION
:
12847 error ("invalid use of pack expansion expression");
12848 return error_mark_node
;
12850 case NONTYPE_ARGUMENT_PACK
:
12851 error ("use %<...%> to expand argument pack");
12852 return error_mark_node
;
12859 /* Instantiate any typedefs in the type. */
12860 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12861 r
= fold_convert (type
, t
);
12862 gcc_assert (TREE_CODE (r
) == code
);
12867 /* These can sometimes show up in a partial instantiation, but never
12868 involve template parms. */
12869 gcc_assert (!uses_template_parms (t
));
12873 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12874 gcc_checking_assert (false);
12879 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12882 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12883 tree args
, tsubst_flags_t complain
, tree in_decl
)
12885 tree new_clauses
= NULL
, nc
, oc
;
12887 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12889 nc
= copy_node (oc
);
12890 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12893 switch (OMP_CLAUSE_CODE (nc
))
12895 case OMP_CLAUSE_LASTPRIVATE
:
12896 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12898 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12899 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12900 in_decl
, /*integral_constant_expression_p=*/false);
12901 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12902 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12905 case OMP_CLAUSE_PRIVATE
:
12906 case OMP_CLAUSE_SHARED
:
12907 case OMP_CLAUSE_FIRSTPRIVATE
:
12908 case OMP_CLAUSE_COPYIN
:
12909 case OMP_CLAUSE_COPYPRIVATE
:
12910 case OMP_CLAUSE_IF
:
12911 case OMP_CLAUSE_NUM_THREADS
:
12912 case OMP_CLAUSE_SCHEDULE
:
12913 case OMP_CLAUSE_COLLAPSE
:
12914 case OMP_CLAUSE_FINAL
:
12915 case OMP_CLAUSE_DEPEND
:
12916 case OMP_CLAUSE_FROM
:
12917 case OMP_CLAUSE_TO
:
12918 case OMP_CLAUSE_UNIFORM
:
12919 case OMP_CLAUSE_MAP
:
12920 case OMP_CLAUSE_DEVICE
:
12921 case OMP_CLAUSE_DIST_SCHEDULE
:
12922 case OMP_CLAUSE_NUM_TEAMS
:
12923 case OMP_CLAUSE_THREAD_LIMIT
:
12924 case OMP_CLAUSE_SAFELEN
:
12925 case OMP_CLAUSE_SIMDLEN
:
12926 OMP_CLAUSE_OPERAND (nc
, 0)
12927 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12928 in_decl
, /*integral_constant_expression_p=*/false);
12930 case OMP_CLAUSE_REDUCTION
:
12931 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12933 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12934 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12936 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12937 complain
, in_decl
);
12938 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12939 = build_qualified_name (NULL_TREE
, scope
,
12940 TREE_OPERAND (placeholder
, 1),
12944 gcc_assert (identifier_p (placeholder
));
12946 OMP_CLAUSE_OPERAND (nc
, 0)
12947 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12948 in_decl
, /*integral_constant_expression_p=*/false);
12950 case OMP_CLAUSE_LINEAR
:
12951 case OMP_CLAUSE_ALIGNED
:
12952 OMP_CLAUSE_OPERAND (nc
, 0)
12953 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12954 in_decl
, /*integral_constant_expression_p=*/false);
12955 OMP_CLAUSE_OPERAND (nc
, 1)
12956 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12957 in_decl
, /*integral_constant_expression_p=*/false);
12960 case OMP_CLAUSE_NOWAIT
:
12961 case OMP_CLAUSE_ORDERED
:
12962 case OMP_CLAUSE_DEFAULT
:
12963 case OMP_CLAUSE_UNTIED
:
12964 case OMP_CLAUSE_MERGEABLE
:
12965 case OMP_CLAUSE_INBRANCH
:
12966 case OMP_CLAUSE_NOTINBRANCH
:
12967 case OMP_CLAUSE_PROC_BIND
:
12968 case OMP_CLAUSE_FOR
:
12969 case OMP_CLAUSE_PARALLEL
:
12970 case OMP_CLAUSE_SECTIONS
:
12971 case OMP_CLAUSE_TASKGROUP
:
12974 gcc_unreachable ();
12978 new_clauses
= nreverse (new_clauses
);
12980 new_clauses
= finish_omp_clauses (new_clauses
);
12981 return new_clauses
;
12984 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12987 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12990 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12992 tree purpose
, value
, chain
;
12997 if (TREE_CODE (t
) != TREE_LIST
)
12998 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12999 /*function_p=*/false,
13000 /*integral_constant_expression_p=*/false);
13002 if (t
== void_list_node
)
13005 purpose
= TREE_PURPOSE (t
);
13007 purpose
= RECUR (purpose
);
13008 value
= TREE_VALUE (t
);
13011 if (TREE_CODE (value
) != LABEL_DECL
)
13012 value
= RECUR (value
);
13015 value
= lookup_label (DECL_NAME (value
));
13016 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13017 TREE_USED (value
) = 1;
13020 chain
= TREE_CHAIN (t
);
13021 if (chain
&& chain
!= void_type_node
)
13022 chain
= RECUR (chain
);
13023 return tree_cons (purpose
, value
, chain
);
13027 /* Substitute one OMP_FOR iterator. */
13030 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13031 tree condv
, tree incrv
, tree
*clauses
,
13032 tree args
, tsubst_flags_t complain
, tree in_decl
,
13033 bool integral_constant_expression_p
)
13035 #define RECUR(NODE) \
13036 tsubst_expr ((NODE), args, complain, in_decl, \
13037 integral_constant_expression_p)
13038 tree decl
, init
, cond
, incr
;
13041 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13042 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13043 decl
= TREE_OPERAND (init
, 0);
13044 init
= TREE_OPERAND (init
, 1);
13045 /* Do this before substituting into decl to handle 'auto'. */
13046 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13047 init
= RECUR (init
);
13048 decl
= RECUR (decl
);
13050 if (decl
== error_mark_node
|| init
== error_mark_node
)
13055 gcc_assert (!processing_template_decl
);
13056 init
= DECL_INITIAL (decl
);
13057 DECL_INITIAL (decl
) = NULL_TREE
;
13060 gcc_assert (!type_dependent_expression_p (decl
));
13062 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13064 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13065 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13066 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13067 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13068 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13069 RECUR (TREE_OPERAND (incr
, 1)),
13072 incr
= RECUR (incr
);
13073 TREE_VEC_ELT (declv
, i
) = decl
;
13074 TREE_VEC_ELT (initv
, i
) = init
;
13075 TREE_VEC_ELT (condv
, i
) = cond
;
13076 TREE_VEC_ELT (incrv
, i
) = incr
;
13080 if (init
&& !init_decl
)
13083 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13085 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13086 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13087 && OMP_CLAUSE_DECL (c
) == decl
)
13089 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13090 && OMP_CLAUSE_DECL (c
) == decl
)
13091 error ("iteration variable %qD should not be firstprivate", decl
);
13092 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13093 && OMP_CLAUSE_DECL (c
) == decl
)
13094 error ("iteration variable %qD should not be reduction", decl
);
13098 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13099 OMP_CLAUSE_DECL (c
) = decl
;
13100 c
= finish_omp_clauses (c
);
13103 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13108 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13109 if (COMPARISON_CLASS_P (cond
))
13110 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13111 RECUR (TREE_OPERAND (cond
, 0)),
13112 RECUR (TREE_OPERAND (cond
, 1)));
13114 cond
= RECUR (cond
);
13115 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13116 switch (TREE_CODE (incr
))
13118 case PREINCREMENT_EXPR
:
13119 case PREDECREMENT_EXPR
:
13120 case POSTINCREMENT_EXPR
:
13121 case POSTDECREMENT_EXPR
:
13122 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13123 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13126 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13127 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13129 tree rhs
= TREE_OPERAND (incr
, 1);
13130 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13131 RECUR (TREE_OPERAND (incr
, 0)),
13132 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13133 RECUR (TREE_OPERAND (rhs
, 0)),
13134 RECUR (TREE_OPERAND (rhs
, 1))));
13137 incr
= RECUR (incr
);
13140 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13141 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13143 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13144 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13145 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13146 TREE_TYPE (decl
), lhs
,
13147 RECUR (TREE_OPERAND (incr
, 2))));
13149 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13150 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13151 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13153 tree rhs
= TREE_OPERAND (incr
, 2);
13154 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13155 RECUR (TREE_OPERAND (incr
, 0)),
13156 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13157 RECUR (TREE_OPERAND (rhs
, 0)),
13158 RECUR (TREE_OPERAND (rhs
, 1))));
13161 incr
= RECUR (incr
);
13164 incr
= RECUR (incr
);
13168 TREE_VEC_ELT (declv
, i
) = decl
;
13169 TREE_VEC_ELT (initv
, i
) = init
;
13170 TREE_VEC_ELT (condv
, i
) = cond
;
13171 TREE_VEC_ELT (incrv
, i
) = incr
;
13175 /* Like tsubst_copy for expressions, etc. but also does semantic
13179 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13180 bool integral_constant_expression_p
)
13182 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13183 #define RECUR(NODE) \
13184 tsubst_expr ((NODE), args, complain, in_decl, \
13185 integral_constant_expression_p)
13191 if (t
== NULL_TREE
|| t
== error_mark_node
)
13194 loc
= input_location
;
13195 if (EXPR_HAS_LOCATION (t
))
13196 input_location
= EXPR_LOCATION (t
);
13197 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13198 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13200 switch (TREE_CODE (t
))
13202 case STATEMENT_LIST
:
13204 tree_stmt_iterator i
;
13205 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13206 RECUR (tsi_stmt (i
));
13210 case CTOR_INITIALIZER
:
13211 finish_mem_initializers (tsubst_initializer_list
13212 (TREE_OPERAND (t
, 0), args
));
13216 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13220 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13221 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13222 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13224 finish_expr_stmt (tmp
);
13228 do_using_directive (USING_STMT_NAMESPACE (t
));
13233 tree decl
, pattern_decl
;
13236 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13237 if (TREE_CODE (decl
) == LABEL_DECL
)
13238 finish_label_decl (DECL_NAME (decl
));
13239 else if (TREE_CODE (decl
) == USING_DECL
)
13241 tree scope
= USING_DECL_SCOPE (decl
);
13242 tree name
= DECL_NAME (decl
);
13245 scope
= tsubst (scope
, args
, complain
, in_decl
);
13246 decl
= lookup_qualified_name (scope
, name
,
13247 /*is_type_p=*/false,
13248 /*complain=*/false);
13249 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13250 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13252 do_local_using_decl (decl
, scope
, name
);
13254 else if (DECL_PACK_P (decl
))
13256 /* Don't build up decls for a variadic capture proxy, we'll
13257 instantiate the elements directly as needed. */
13262 init
= DECL_INITIAL (decl
);
13263 decl
= tsubst (decl
, args
, complain
, in_decl
);
13264 if (decl
!= error_mark_node
)
13266 /* By marking the declaration as instantiated, we avoid
13267 trying to instantiate it. Since instantiate_decl can't
13268 handle local variables, and since we've already done
13269 all that needs to be done, that's the right thing to
13272 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13274 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13275 /* Anonymous aggregates are a special case. */
13276 finish_anon_union (decl
);
13277 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13279 DECL_CONTEXT (decl
) = current_function_decl
;
13280 if (DECL_NAME (decl
) == this_identifier
)
13282 tree lam
= DECL_CONTEXT (current_function_decl
);
13283 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13284 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13286 insert_capture_proxy (decl
);
13288 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13289 /* We already did a pushtag. */;
13290 else if (TREE_CODE (decl
) == FUNCTION_DECL
13291 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13292 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13294 DECL_CONTEXT (decl
) = NULL_TREE
;
13296 DECL_CONTEXT (decl
) = current_function_decl
;
13297 cp_check_omp_declare_reduction (decl
);
13301 int const_init
= false;
13302 maybe_push_decl (decl
);
13304 && DECL_PRETTY_FUNCTION_P (decl
))
13306 /* For __PRETTY_FUNCTION__ we have to adjust the
13308 const char *const name
13309 = cxx_printable_name (current_function_decl
, 2);
13310 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13314 tree t
= RECUR (init
);
13318 /* If we had an initializer but it
13319 instantiated to nothing,
13320 value-initialize the object. This will
13321 only occur when the initializer was a
13322 pack expansion where the parameter packs
13323 used in that expansion were of length
13325 init
= build_value_init (TREE_TYPE (decl
),
13327 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13328 init
= get_target_expr_sfinae (init
, complain
);
13335 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13337 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13346 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13347 RECUR (FOR_INIT_STMT (t
));
13348 finish_for_init_stmt (stmt
);
13349 tmp
= RECUR (FOR_COND (t
));
13350 finish_for_cond (tmp
, stmt
, false);
13351 tmp
= RECUR (FOR_EXPR (t
));
13352 finish_for_expr (tmp
, stmt
);
13353 RECUR (FOR_BODY (t
));
13354 finish_for_stmt (stmt
);
13357 case RANGE_FOR_STMT
:
13360 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13361 decl
= RANGE_FOR_DECL (t
);
13362 decl
= tsubst (decl
, args
, complain
, in_decl
);
13363 maybe_push_decl (decl
);
13364 expr
= RECUR (RANGE_FOR_EXPR (t
));
13365 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13366 RECUR (RANGE_FOR_BODY (t
));
13367 finish_for_stmt (stmt
);
13372 stmt
= begin_while_stmt ();
13373 tmp
= RECUR (WHILE_COND (t
));
13374 finish_while_stmt_cond (tmp
, stmt
, false);
13375 RECUR (WHILE_BODY (t
));
13376 finish_while_stmt (stmt
);
13380 stmt
= begin_do_stmt ();
13381 RECUR (DO_BODY (t
));
13382 finish_do_body (stmt
);
13383 tmp
= RECUR (DO_COND (t
));
13384 finish_do_stmt (tmp
, stmt
, false);
13388 stmt
= begin_if_stmt ();
13389 tmp
= RECUR (IF_COND (t
));
13390 finish_if_stmt_cond (tmp
, stmt
);
13391 RECUR (THEN_CLAUSE (t
));
13392 finish_then_clause (stmt
);
13394 if (ELSE_CLAUSE (t
))
13396 begin_else_clause (stmt
);
13397 RECUR (ELSE_CLAUSE (t
));
13398 finish_else_clause (stmt
);
13401 finish_if_stmt (stmt
);
13405 if (BIND_EXPR_BODY_BLOCK (t
))
13406 stmt
= begin_function_body ();
13408 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13409 ? BCS_TRY_BLOCK
: 0);
13411 RECUR (BIND_EXPR_BODY (t
));
13413 if (BIND_EXPR_BODY_BLOCK (t
))
13414 finish_function_body (stmt
);
13416 finish_compound_stmt (stmt
);
13420 finish_break_stmt ();
13423 case CONTINUE_STMT
:
13424 finish_continue_stmt ();
13428 stmt
= begin_switch_stmt ();
13429 tmp
= RECUR (SWITCH_STMT_COND (t
));
13430 finish_switch_cond (tmp
, stmt
);
13431 RECUR (SWITCH_STMT_BODY (t
));
13432 finish_switch_stmt (stmt
);
13435 case CASE_LABEL_EXPR
:
13436 finish_case_label (EXPR_LOCATION (t
),
13437 RECUR (CASE_LOW (t
)),
13438 RECUR (CASE_HIGH (t
)));
13443 tree decl
= LABEL_EXPR_LABEL (t
);
13446 label
= finish_label_stmt (DECL_NAME (decl
));
13447 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13448 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13453 tmp
= GOTO_DESTINATION (t
);
13454 if (TREE_CODE (tmp
) != LABEL_DECL
)
13455 /* Computed goto's must be tsubst'd into. On the other hand,
13456 non-computed gotos must not be; the identifier in question
13457 will have no binding. */
13460 tmp
= DECL_NAME (tmp
);
13461 finish_goto_stmt (tmp
);
13465 tmp
= finish_asm_stmt
13466 (ASM_VOLATILE_P (t
),
13467 RECUR (ASM_STRING (t
)),
13468 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13469 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13470 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13471 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13473 tree asm_expr
= tmp
;
13474 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13475 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13476 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13483 stmt
= begin_try_block ();
13484 RECUR (TRY_STMTS (t
));
13485 finish_cleanup_try_block (stmt
);
13486 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13490 tree compound_stmt
= NULL_TREE
;
13492 if (FN_TRY_BLOCK_P (t
))
13493 stmt
= begin_function_try_block (&compound_stmt
);
13495 stmt
= begin_try_block ();
13497 RECUR (TRY_STMTS (t
));
13499 if (FN_TRY_BLOCK_P (t
))
13500 finish_function_try_block (stmt
);
13502 finish_try_block (stmt
);
13504 RECUR (TRY_HANDLERS (t
));
13505 if (FN_TRY_BLOCK_P (t
))
13506 finish_function_handler_sequence (stmt
, compound_stmt
);
13508 finish_handler_sequence (stmt
);
13514 tree decl
= HANDLER_PARMS (t
);
13518 decl
= tsubst (decl
, args
, complain
, in_decl
);
13519 /* Prevent instantiate_decl from trying to instantiate
13520 this variable. We've already done all that needs to be
13522 if (decl
!= error_mark_node
)
13523 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13525 stmt
= begin_handler ();
13526 finish_handler_parms (decl
, stmt
);
13527 RECUR (HANDLER_BODY (t
));
13528 finish_handler (stmt
);
13533 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13534 if (CLASS_TYPE_P (tmp
))
13536 /* Local classes are not independent templates; they are
13537 instantiated along with their containing function. And this
13538 way we don't have to deal with pushing out of one local class
13539 to instantiate a member of another local class. */
13541 /* Closures are handled by the LAMBDA_EXPR. */
13542 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13543 complete_type (tmp
);
13544 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13545 if (!DECL_ARTIFICIAL (fn
))
13546 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13550 case STATIC_ASSERT
:
13554 ++c_inhibit_evaluation_warnings
;
13556 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13559 /*integral_constant_expression_p=*/true);
13560 --c_inhibit_evaluation_warnings
;
13562 finish_static_assert (condition
,
13563 STATIC_ASSERT_MESSAGE (t
),
13564 STATIC_ASSERT_SOURCE_LOCATION (t
),
13565 /*member_p=*/false);
13570 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13571 args
, complain
, in_decl
);
13572 stmt
= begin_omp_parallel ();
13573 RECUR (OMP_PARALLEL_BODY (t
));
13574 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13575 = OMP_PARALLEL_COMBINED (t
);
13579 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13580 args
, complain
, in_decl
);
13581 stmt
= begin_omp_task ();
13582 RECUR (OMP_TASK_BODY (t
));
13583 finish_omp_task (tmp
, stmt
);
13589 case OMP_DISTRIBUTE
:
13591 tree clauses
, body
, pre_body
;
13592 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13593 tree incrv
= NULL_TREE
;
13596 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13597 args
, complain
, in_decl
);
13598 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13600 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13601 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13602 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13603 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13606 stmt
= begin_omp_structured_block ();
13608 pre_body
= push_stmt_list ();
13609 RECUR (OMP_FOR_PRE_BODY (t
));
13610 pre_body
= pop_stmt_list (pre_body
);
13612 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13613 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13614 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13615 &clauses
, args
, complain
, in_decl
,
13616 integral_constant_expression_p
);
13618 body
= push_stmt_list ();
13619 RECUR (OMP_FOR_BODY (t
));
13620 body
= pop_stmt_list (body
);
13622 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13623 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13624 condv
, incrv
, body
, pre_body
, clauses
);
13627 t
= make_node (TREE_CODE (t
));
13628 TREE_TYPE (t
) = void_type_node
;
13629 OMP_FOR_BODY (t
) = body
;
13630 OMP_FOR_PRE_BODY (t
) = pre_body
;
13631 OMP_FOR_CLAUSES (t
) = clauses
;
13632 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13636 add_stmt (finish_omp_structured_block (stmt
));
13643 case OMP_TARGET_DATA
:
13645 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13646 args
, complain
, in_decl
);
13647 stmt
= push_stmt_list ();
13648 RECUR (OMP_BODY (t
));
13649 stmt
= pop_stmt_list (stmt
);
13652 OMP_BODY (t
) = stmt
;
13653 OMP_CLAUSES (t
) = tmp
;
13657 case OMP_TARGET_UPDATE
:
13658 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13659 args
, complain
, in_decl
);
13661 OMP_CLAUSES (t
) = tmp
;
13668 case OMP_TASKGROUP
:
13670 stmt
= push_stmt_list ();
13671 RECUR (OMP_BODY (t
));
13672 stmt
= pop_stmt_list (stmt
);
13675 OMP_BODY (t
) = stmt
;
13680 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13681 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13683 tree op1
= TREE_OPERAND (t
, 1);
13684 tree rhs1
= NULL_TREE
;
13686 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13688 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13689 op1
= TREE_OPERAND (op1
, 1);
13691 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13692 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13693 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13694 NULL_TREE
, NULL_TREE
, rhs1
,
13695 OMP_ATOMIC_SEQ_CST (t
));
13699 tree op1
= TREE_OPERAND (t
, 1);
13700 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13701 tree rhs1
= NULL_TREE
;
13702 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13703 enum tree_code opcode
= NOP_EXPR
;
13704 if (code
== OMP_ATOMIC_READ
)
13706 v
= RECUR (TREE_OPERAND (op1
, 0));
13707 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13709 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13710 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13712 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13713 v
= RECUR (TREE_OPERAND (op1
, 0));
13714 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13715 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13717 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13718 op11
= TREE_OPERAND (op11
, 1);
13720 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13721 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13722 opcode
= TREE_CODE (op11
);
13723 if (opcode
== MODIFY_EXPR
)
13729 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13730 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13732 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13733 OMP_ATOMIC_SEQ_CST (t
));
13737 case TRANSACTION_EXPR
:
13740 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13741 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13743 if (TRANSACTION_EXPR_IS_STMT (t
))
13745 tree body
= TRANSACTION_EXPR_BODY (t
);
13746 tree noex
= NULL_TREE
;
13747 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13749 noex
= MUST_NOT_THROW_COND (body
);
13750 if (noex
== NULL_TREE
)
13751 noex
= boolean_true_node
;
13752 body
= TREE_OPERAND (body
, 0);
13754 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13756 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13760 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13761 RECUR (TRANSACTION_EXPR_BODY (t
)),
13768 case MUST_NOT_THROW_EXPR
:
13769 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13770 RECUR (MUST_NOT_THROW_COND (t
))));
13772 case EXPR_PACK_EXPANSION
:
13773 error ("invalid use of pack expansion expression");
13774 RETURN (error_mark_node
);
13776 case NONTYPE_ARGUMENT_PACK
:
13777 error ("use %<...%> to expand argument pack");
13778 RETURN (error_mark_node
);
13780 case CILK_SPAWN_STMT
:
13781 cfun
->calls_cilk_spawn
= 1;
13782 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
13784 case CILK_SYNC_STMT
:
13785 RETURN (build_cilk_sync ());
13787 case COMPOUND_EXPR
:
13788 tmp
= RECUR (TREE_OPERAND (t
, 0));
13789 if (tmp
== NULL_TREE
)
13790 /* If the first operand was a statement, we're done with it. */
13791 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13792 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13793 RECUR (TREE_OPERAND (t
, 1)),
13797 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13799 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13800 /*function_p=*/false,
13801 integral_constant_expression_p
));
13804 RETURN (NULL_TREE
);
13806 input_location
= loc
;
13812 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13813 function. For description of the body see comment above
13814 cp_parser_omp_declare_reduction_exprs. */
13817 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13819 if (t
== NULL_TREE
|| t
== error_mark_node
)
13822 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13824 tree_stmt_iterator tsi
;
13827 memset (stmts
, 0, sizeof stmts
);
13828 for (i
= 0, tsi
= tsi_start (t
);
13829 i
< 7 && !tsi_end_p (tsi
);
13830 i
++, tsi_next (&tsi
))
13831 stmts
[i
] = tsi_stmt (tsi
);
13832 gcc_assert (tsi_end_p (tsi
));
13836 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13837 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13838 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13839 args
, complain
, in_decl
);
13840 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13841 args
, complain
, in_decl
);
13842 DECL_CONTEXT (omp_out
) = current_function_decl
;
13843 DECL_CONTEXT (omp_in
) = current_function_decl
;
13844 keep_next_level (true);
13845 tree block
= begin_omp_structured_block ();
13846 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13847 block
= finish_omp_structured_block (block
);
13848 block
= maybe_cleanup_point_expr_void (block
);
13849 add_decl_expr (omp_out
);
13850 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13851 TREE_NO_WARNING (omp_out
) = 1;
13852 add_decl_expr (omp_in
);
13853 finish_expr_stmt (block
);
13857 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13858 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13859 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13860 args
, complain
, in_decl
);
13861 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13862 args
, complain
, in_decl
);
13863 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13864 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13865 keep_next_level (true);
13866 tree block
= begin_omp_structured_block ();
13867 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13868 block
= finish_omp_structured_block (block
);
13869 block
= maybe_cleanup_point_expr_void (block
);
13870 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13871 add_decl_expr (omp_priv
);
13872 add_decl_expr (omp_orig
);
13873 finish_expr_stmt (block
);
13875 add_decl_expr (omp_orig
);
13879 /* T is a postfix-expression that is not being used in a function
13880 call. Return the substituted version of T. */
13883 tsubst_non_call_postfix_expression (tree t
, tree args
,
13884 tsubst_flags_t complain
,
13887 if (TREE_CODE (t
) == SCOPE_REF
)
13888 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13889 /*done=*/false, /*address_p=*/false);
13891 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13892 /*function_p=*/false,
13893 /*integral_constant_expression_p=*/false);
13898 /* Like tsubst but deals with expressions and performs semantic
13899 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13902 tsubst_copy_and_build (tree t
,
13904 tsubst_flags_t complain
,
13907 bool integral_constant_expression_p
)
13909 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13910 #define RECUR(NODE) \
13911 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13912 /*function_p=*/false, \
13913 integral_constant_expression_p)
13918 if (t
== NULL_TREE
|| t
== error_mark_node
)
13921 loc
= input_location
;
13922 if (EXPR_HAS_LOCATION (t
))
13923 input_location
= EXPR_LOCATION (t
);
13925 /* N3276 decltype magic only applies to calls at the top level or on the
13926 right side of a comma. */
13927 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13928 complain
&= ~tf_decltype
;
13930 switch (TREE_CODE (t
))
13934 /* Fall through. */
13935 case IDENTIFIER_NODE
:
13939 bool non_integral_constant_expression_p
;
13940 const char *error_msg
;
13942 if (IDENTIFIER_TYPENAME_P (t
))
13944 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13945 t
= mangle_conv_op_name_for_type (new_type
);
13948 /* Look up the name. */
13949 decl
= lookup_name (t
);
13951 /* By convention, expressions use ERROR_MARK_NODE to indicate
13952 failure, not NULL_TREE. */
13953 if (decl
== NULL_TREE
)
13954 decl
= error_mark_node
;
13956 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13958 integral_constant_expression_p
,
13959 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13960 &non_integral_constant_expression_p
,
13961 /*template_p=*/false,
13963 /*address_p=*/false,
13964 /*template_arg_p=*/false,
13969 if (!function_p
&& identifier_p (decl
))
13971 if (complain
& tf_error
)
13972 unqualified_name_lookup_error (decl
);
13973 decl
= error_mark_node
;
13978 case TEMPLATE_ID_EXPR
:
13981 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13982 tree targs
= TREE_OPERAND (t
, 1);
13985 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13987 if (TREE_CODE (templ
) == COMPONENT_REF
)
13989 object
= TREE_OPERAND (templ
, 0);
13990 templ
= TREE_OPERAND (templ
, 1);
13993 object
= NULL_TREE
;
13994 templ
= lookup_template_function (templ
, targs
);
13997 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13998 object
, templ
, NULL_TREE
));
14000 RETURN (baselink_for_fns (templ
));
14005 tree r
= RECUR (TREE_OPERAND (t
, 0));
14007 if (REFERENCE_REF_P (t
))
14009 /* A type conversion to reference type will be enclosed in
14010 such an indirect ref, but the substitution of the cast
14011 will have also added such an indirect ref. */
14012 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14013 r
= convert_from_reference (r
);
14016 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14017 complain
|decltype_flag
);
14023 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14024 RECUR (TREE_OPERAND (t
, 0))));
14026 case IMPLICIT_CONV_EXPR
:
14028 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14029 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14030 int flags
= LOOKUP_IMPLICIT
;
14031 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14032 flags
= LOOKUP_NORMAL
;
14033 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14040 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14041 RECUR (TREE_OPERAND (t
, 0))));
14044 case REINTERPRET_CAST_EXPR
:
14045 case CONST_CAST_EXPR
:
14046 case DYNAMIC_CAST_EXPR
:
14047 case STATIC_CAST_EXPR
:
14050 tree op
, r
= NULL_TREE
;
14052 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14053 if (integral_constant_expression_p
14054 && !cast_valid_in_integral_constant_expression_p (type
))
14056 if (complain
& tf_error
)
14057 error ("a cast to a type other than an integral or "
14058 "enumeration type cannot appear in a constant-expression");
14059 RETURN (error_mark_node
);
14062 op
= RECUR (TREE_OPERAND (t
, 0));
14064 ++c_inhibit_evaluation_warnings
;
14065 switch (TREE_CODE (t
))
14068 r
= build_functional_cast (type
, op
, complain
);
14070 case REINTERPRET_CAST_EXPR
:
14071 r
= build_reinterpret_cast (type
, op
, complain
);
14073 case CONST_CAST_EXPR
:
14074 r
= build_const_cast (type
, op
, complain
);
14076 case DYNAMIC_CAST_EXPR
:
14077 r
= build_dynamic_cast (type
, op
, complain
);
14079 case STATIC_CAST_EXPR
:
14080 r
= build_static_cast (type
, op
, complain
);
14083 gcc_unreachable ();
14085 --c_inhibit_evaluation_warnings
;
14090 case POSTDECREMENT_EXPR
:
14091 case POSTINCREMENT_EXPR
:
14092 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14093 args
, complain
, in_decl
);
14094 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14095 complain
|decltype_flag
));
14097 case PREDECREMENT_EXPR
:
14098 case PREINCREMENT_EXPR
:
14102 case TRUTH_NOT_EXPR
:
14103 case UNARY_PLUS_EXPR
: /* Unary + */
14104 case REALPART_EXPR
:
14105 case IMAGPART_EXPR
:
14106 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14107 RECUR (TREE_OPERAND (t
, 0)),
14108 complain
|decltype_flag
));
14110 case FIX_TRUNC_EXPR
:
14111 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14115 op1
= TREE_OPERAND (t
, 0);
14116 if (TREE_CODE (op1
) == LABEL_DECL
)
14117 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14118 EXPR_LOCATION (op1
)));
14119 if (TREE_CODE (op1
) == SCOPE_REF
)
14120 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14121 /*done=*/true, /*address_p=*/true);
14123 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14125 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14126 complain
|decltype_flag
));
14131 case TRUNC_DIV_EXPR
:
14132 case CEIL_DIV_EXPR
:
14133 case FLOOR_DIV_EXPR
:
14134 case ROUND_DIV_EXPR
:
14135 case EXACT_DIV_EXPR
:
14139 case TRUNC_MOD_EXPR
:
14140 case FLOOR_MOD_EXPR
:
14141 case TRUTH_ANDIF_EXPR
:
14142 case TRUTH_ORIF_EXPR
:
14143 case TRUTH_AND_EXPR
:
14144 case TRUTH_OR_EXPR
:
14162 ++c_inhibit_evaluation_warnings
;
14164 r
= build_x_binary_op
14165 (input_location
, TREE_CODE (t
),
14166 RECUR (TREE_OPERAND (t
, 0)),
14167 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14169 : TREE_CODE (TREE_OPERAND (t
, 0))),
14170 RECUR (TREE_OPERAND (t
, 1)),
14171 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14173 : TREE_CODE (TREE_OPERAND (t
, 1))),
14175 complain
|decltype_flag
);
14176 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14177 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14179 --c_inhibit_evaluation_warnings
;
14184 case POINTER_PLUS_EXPR
:
14185 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t
, 0)),
14186 RECUR (TREE_OPERAND (t
, 1)));
14189 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14190 /*address_p=*/false));
14192 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14193 args
, complain
, in_decl
);
14194 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14195 RECUR (TREE_OPERAND (t
, 1)),
14196 complain
|decltype_flag
));
14198 case ARRAY_NOTATION_REF
:
14200 tree start_index
, length
, stride
;
14201 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14202 args
, complain
, in_decl
);
14203 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14204 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14205 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14206 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14207 length
, stride
, TREE_TYPE (op1
)));
14210 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14211 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14218 op1
= TREE_OPERAND (t
, 0);
14219 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14220 op1
= TREE_TYPE (op1
);
14223 /* When there are no ARGS, we are trying to evaluate a
14224 non-dependent expression from the parser. Trying to do
14225 the substitutions may not work. */
14227 op1
= TREE_TYPE (op1
);
14231 ++cp_unevaluated_operand
;
14232 ++c_inhibit_evaluation_warnings
;
14234 op1
= tsubst (op1
, args
, complain
, in_decl
);
14236 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14237 /*function_p=*/false,
14238 /*integral_constant_expression_p=*/
14240 --cp_unevaluated_operand
;
14241 --c_inhibit_evaluation_warnings
;
14244 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14245 complain
& tf_error
);
14247 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14248 complain
& tf_error
);
14249 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14251 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14253 if (!processing_template_decl
&& TYPE_P (op1
))
14255 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14256 build1 (NOP_EXPR
, op1
, error_mark_node
));
14257 SIZEOF_EXPR_TYPE_P (r
) = 1;
14260 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14261 TREE_SIDE_EFFECTS (r
) = 0;
14262 TREE_READONLY (r
) = 1;
14264 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14269 case AT_ENCODE_EXPR
:
14271 op1
= TREE_OPERAND (t
, 0);
14272 ++cp_unevaluated_operand
;
14273 ++c_inhibit_evaluation_warnings
;
14274 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14275 /*function_p=*/false,
14276 /*integral_constant_expression_p=*/false);
14277 --cp_unevaluated_operand
;
14278 --c_inhibit_evaluation_warnings
;
14279 RETURN (objc_build_encode_expr (op1
));
14282 case NOEXCEPT_EXPR
:
14283 op1
= TREE_OPERAND (t
, 0);
14284 ++cp_unevaluated_operand
;
14285 ++c_inhibit_evaluation_warnings
;
14286 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14287 /*function_p=*/false,
14288 /*integral_constant_expression_p=*/false);
14289 --cp_unevaluated_operand
;
14290 --c_inhibit_evaluation_warnings
;
14291 RETURN (finish_noexcept_expr (op1
, complain
));
14297 ++c_inhibit_evaluation_warnings
;
14299 r
= build_x_modify_expr
14300 (EXPR_LOCATION (t
),
14301 RECUR (TREE_OPERAND (t
, 0)),
14302 TREE_CODE (TREE_OPERAND (t
, 1)),
14303 RECUR (TREE_OPERAND (t
, 2)),
14304 complain
|decltype_flag
);
14305 /* TREE_NO_WARNING must be set if either the expression was
14306 parenthesized or it uses an operator such as >>= rather
14307 than plain assignment. In the former case, it was already
14308 set and must be copied. In the latter case,
14309 build_x_modify_expr sets it and it must not be reset
14311 if (TREE_NO_WARNING (t
))
14312 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14314 --c_inhibit_evaluation_warnings
;
14320 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14321 args
, complain
, in_decl
);
14322 /* Remember that there was a reference to this entity. */
14325 RETURN (build_x_arrow (input_location
, op1
, complain
));
14329 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14330 tree init
= RECUR (TREE_OPERAND (t
, 3));
14331 vec
<tree
, va_gc
> *placement_vec
;
14332 vec
<tree
, va_gc
> *init_vec
;
14335 if (placement
== NULL_TREE
)
14336 placement_vec
= NULL
;
14339 placement_vec
= make_tree_vector ();
14340 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14341 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14344 /* If there was an initializer in the original tree, but it
14345 instantiated to an empty list, then we should pass a
14346 non-NULL empty vector to tell build_new that it was an
14347 empty initializer() rather than no initializer. This can
14348 only happen when the initializer is a pack expansion whose
14349 parameter packs are of length zero. */
14350 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14354 init_vec
= make_tree_vector ();
14355 if (init
== void_zero_node
)
14356 gcc_assert (init_vec
!= NULL
);
14359 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14360 vec_safe_push (init_vec
, TREE_VALUE (init
));
14364 ret
= build_new (&placement_vec
,
14365 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14366 RECUR (TREE_OPERAND (t
, 2)),
14368 NEW_EXPR_USE_GLOBAL (t
),
14371 if (placement_vec
!= NULL
)
14372 release_tree_vector (placement_vec
);
14373 if (init_vec
!= NULL
)
14374 release_tree_vector (init_vec
);
14380 RETURN (delete_sanity
14381 (RECUR (TREE_OPERAND (t
, 0)),
14382 RECUR (TREE_OPERAND (t
, 1)),
14383 DELETE_EXPR_USE_VEC (t
),
14384 DELETE_EXPR_USE_GLOBAL (t
),
14387 case COMPOUND_EXPR
:
14389 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14390 complain
& ~tf_decltype
, in_decl
,
14391 /*function_p=*/false,
14392 integral_constant_expression_p
);
14393 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14395 RECUR (TREE_OPERAND (t
, 1)),
14396 complain
|decltype_flag
));
14402 vec
<tree
, va_gc
> *call_args
;
14403 unsigned int nargs
, i
;
14408 function
= CALL_EXPR_FN (t
);
14409 /* When we parsed the expression, we determined whether or
14410 not Koenig lookup should be performed. */
14411 koenig_p
= KOENIG_LOOKUP_P (t
);
14412 if (TREE_CODE (function
) == SCOPE_REF
)
14414 qualified_p
= true;
14415 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14417 /*address_p=*/false);
14419 else if (koenig_p
&& identifier_p (function
))
14421 /* Do nothing; calling tsubst_copy_and_build on an identifier
14422 would incorrectly perform unqualified lookup again.
14424 Note that we can also have an IDENTIFIER_NODE if the earlier
14425 unqualified lookup found a member function; in that case
14426 koenig_p will be false and we do want to do the lookup
14427 again to find the instantiated member function.
14429 FIXME but doing that causes c++/15272, so we need to stop
14430 using IDENTIFIER_NODE in that situation. */
14431 qualified_p
= false;
14435 if (TREE_CODE (function
) == COMPONENT_REF
)
14437 tree op
= TREE_OPERAND (function
, 1);
14439 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14440 || (BASELINK_P (op
)
14441 && BASELINK_QUALIFIED_P (op
)));
14444 qualified_p
= false;
14446 if (TREE_CODE (function
) == ADDR_EXPR
14447 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14448 /* Avoid error about taking the address of a constructor. */
14449 function
= TREE_OPERAND (function
, 0);
14451 function
= tsubst_copy_and_build (function
, args
, complain
,
14454 integral_constant_expression_p
);
14456 if (BASELINK_P (function
))
14457 qualified_p
= true;
14460 nargs
= call_expr_nargs (t
);
14461 call_args
= make_tree_vector ();
14462 for (i
= 0; i
< nargs
; ++i
)
14464 tree arg
= CALL_EXPR_ARG (t
, i
);
14466 if (!PACK_EXPANSION_P (arg
))
14467 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14470 /* Expand the pack expansion and push each entry onto
14472 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14473 if (TREE_CODE (arg
) == TREE_VEC
)
14475 unsigned int len
, j
;
14477 len
= TREE_VEC_LENGTH (arg
);
14478 for (j
= 0; j
< len
; ++j
)
14480 tree value
= TREE_VEC_ELT (arg
, j
);
14481 if (value
!= NULL_TREE
)
14482 value
= convert_from_reference (value
);
14483 vec_safe_push (call_args
, value
);
14488 /* A partial substitution. Add one entry. */
14489 vec_safe_push (call_args
, arg
);
14494 /* We do not perform argument-dependent lookup if normal
14495 lookup finds a non-function, in accordance with the
14496 expected resolution of DR 218. */
14498 && ((is_overloaded_fn (function
)
14499 /* If lookup found a member function, the Koenig lookup is
14500 not appropriate, even if an unqualified-name was used
14501 to denote the function. */
14502 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14503 || identifier_p (function
))
14504 /* Only do this when substitution turns a dependent call
14505 into a non-dependent call. */
14506 && type_dependent_expression_p_push (t
)
14507 && !any_type_dependent_arguments_p (call_args
))
14508 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
14510 if (identifier_p (function
)
14511 && !any_type_dependent_arguments_p (call_args
))
14513 if (koenig_p
&& (complain
& tf_warning_or_error
))
14515 /* For backwards compatibility and good diagnostics, try
14516 the unqualified lookup again if we aren't in SFINAE
14518 tree unq
= (tsubst_copy_and_build
14519 (function
, args
, complain
, in_decl
, true,
14520 integral_constant_expression_p
));
14521 if (unq
== error_mark_node
)
14522 RETURN (error_mark_node
);
14524 if (unq
!= function
)
14527 if (INDIRECT_REF_P (fn
))
14528 fn
= TREE_OPERAND (fn
, 0);
14529 if (TREE_CODE (fn
) == COMPONENT_REF
)
14530 fn
= TREE_OPERAND (fn
, 1);
14531 if (is_overloaded_fn (fn
))
14532 fn
= get_first_fn (fn
);
14533 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
14534 "%qD was not declared in this scope, "
14535 "and no declarations were found by "
14536 "argument-dependent lookup at the point "
14537 "of instantiation", function
))
14540 /* Can't say anything more. */;
14541 else if (DECL_CLASS_SCOPE_P (fn
))
14543 location_t loc
= EXPR_LOC_OR_LOC (t
,
14546 "declarations in dependent base %qT are "
14547 "not found by unqualified lookup",
14548 DECL_CLASS_CONTEXT (fn
));
14549 if (current_class_ptr
)
14551 "use %<this->%D%> instead", function
);
14554 "use %<%T::%D%> instead",
14555 current_class_name
, function
);
14558 inform (0, "%q+D declared here, later in the "
14559 "translation unit", fn
);
14564 if (identifier_p (function
))
14566 if (complain
& tf_error
)
14567 unqualified_name_lookup_error (function
);
14568 release_tree_vector (call_args
);
14569 RETURN (error_mark_node
);
14573 /* Remember that there was a reference to this entity. */
14574 if (DECL_P (function
))
14575 mark_used (function
);
14577 /* Put back tf_decltype for the actual call. */
14578 complain
|= decltype_flag
;
14580 if (TREE_CODE (function
) == OFFSET_REF
)
14581 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14583 else if (TREE_CODE (function
) == COMPONENT_REF
)
14585 tree instance
= TREE_OPERAND (function
, 0);
14586 tree fn
= TREE_OPERAND (function
, 1);
14588 if (processing_template_decl
14589 && (type_dependent_expression_p (instance
)
14590 || (!BASELINK_P (fn
)
14591 && TREE_CODE (fn
) != FIELD_DECL
)
14592 || type_dependent_expression_p (fn
)
14593 || any_type_dependent_arguments_p (call_args
)))
14594 ret
= build_nt_call_vec (function
, call_args
);
14595 else if (!BASELINK_P (fn
))
14596 ret
= finish_call_expr (function
, &call_args
,
14597 /*disallow_virtual=*/false,
14598 /*koenig_p=*/false,
14601 ret
= (build_new_method_call
14603 &call_args
, NULL_TREE
,
14604 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14609 ret
= finish_call_expr (function
, &call_args
,
14610 /*disallow_virtual=*/qualified_p
,
14614 release_tree_vector (call_args
);
14621 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14624 if (TREE_CODE (cond
) == INTEGER_CST
)
14626 if (integer_zerop (cond
))
14628 ++c_inhibit_evaluation_warnings
;
14629 exp1
= RECUR (TREE_OPERAND (t
, 1));
14630 --c_inhibit_evaluation_warnings
;
14631 exp2
= RECUR (TREE_OPERAND (t
, 2));
14635 exp1
= RECUR (TREE_OPERAND (t
, 1));
14636 ++c_inhibit_evaluation_warnings
;
14637 exp2
= RECUR (TREE_OPERAND (t
, 2));
14638 --c_inhibit_evaluation_warnings
;
14643 exp1
= RECUR (TREE_OPERAND (t
, 1));
14644 exp2
= RECUR (TREE_OPERAND (t
, 2));
14647 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14648 cond
, exp1
, exp2
, complain
));
14651 case PSEUDO_DTOR_EXPR
:
14652 RETURN (finish_pseudo_destructor_expr
14653 (RECUR (TREE_OPERAND (t
, 0)),
14654 RECUR (TREE_OPERAND (t
, 1)),
14655 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14660 tree purpose
, value
, chain
;
14662 if (t
== void_list_node
)
14665 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14666 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14668 /* We have pack expansions, so expand those and
14669 create a new list out of it. */
14670 tree purposevec
= NULL_TREE
;
14671 tree valuevec
= NULL_TREE
;
14675 /* Expand the argument expressions. */
14676 if (TREE_PURPOSE (t
))
14677 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14678 complain
, in_decl
);
14679 if (TREE_VALUE (t
))
14680 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14681 complain
, in_decl
);
14683 /* Build the rest of the list. */
14684 chain
= TREE_CHAIN (t
);
14685 if (chain
&& chain
!= void_type_node
)
14686 chain
= RECUR (chain
);
14688 /* Determine the number of arguments. */
14689 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14691 len
= TREE_VEC_LENGTH (purposevec
);
14692 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14694 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14695 len
= TREE_VEC_LENGTH (valuevec
);
14698 /* Since we only performed a partial substitution into
14699 the argument pack, we only RETURN (a single list
14701 if (purposevec
== TREE_PURPOSE (t
)
14702 && valuevec
== TREE_VALUE (t
)
14703 && chain
== TREE_CHAIN (t
))
14706 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14709 /* Convert the argument vectors into a TREE_LIST */
14713 /* Grab the Ith values. */
14715 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14718 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14721 /* Build the list (backwards). */
14722 chain
= tree_cons (purpose
, value
, chain
);
14728 purpose
= TREE_PURPOSE (t
);
14730 purpose
= RECUR (purpose
);
14731 value
= TREE_VALUE (t
);
14733 value
= RECUR (value
);
14734 chain
= TREE_CHAIN (t
);
14735 if (chain
&& chain
!= void_type_node
)
14736 chain
= RECUR (chain
);
14737 if (purpose
== TREE_PURPOSE (t
)
14738 && value
== TREE_VALUE (t
)
14739 && chain
== TREE_CHAIN (t
))
14741 RETURN (tree_cons (purpose
, value
, chain
));
14744 case COMPONENT_REF
:
14750 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14751 args
, complain
, in_decl
);
14752 /* Remember that there was a reference to this entity. */
14753 if (DECL_P (object
))
14754 mark_used (object
);
14755 object_type
= TREE_TYPE (object
);
14757 member
= TREE_OPERAND (t
, 1);
14758 if (BASELINK_P (member
))
14759 member
= tsubst_baselink (member
,
14760 non_reference (TREE_TYPE (object
)),
14761 args
, complain
, in_decl
);
14763 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14764 if (member
== error_mark_node
)
14765 RETURN (error_mark_node
);
14767 if (type_dependent_expression_p (object
))
14768 /* We can't do much here. */;
14769 else if (!CLASS_TYPE_P (object_type
))
14771 if (scalarish_type_p (object_type
))
14773 tree s
= NULL_TREE
;
14774 tree dtor
= member
;
14776 if (TREE_CODE (dtor
) == SCOPE_REF
)
14778 s
= TREE_OPERAND (dtor
, 0);
14779 dtor
= TREE_OPERAND (dtor
, 1);
14781 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14783 dtor
= TREE_OPERAND (dtor
, 0);
14785 RETURN (finish_pseudo_destructor_expr
14786 (object
, s
, dtor
, input_location
));
14790 else if (TREE_CODE (member
) == SCOPE_REF
14791 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14793 /* Lookup the template functions now that we know what the
14795 tree scope
= TREE_OPERAND (member
, 0);
14796 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14797 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14798 member
= lookup_qualified_name (scope
, tmpl
,
14799 /*is_type_p=*/false,
14800 /*complain=*/false);
14801 if (BASELINK_P (member
))
14803 BASELINK_FUNCTIONS (member
)
14804 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14806 member
= (adjust_result_of_qualified_name_lookup
14807 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14812 qualified_name_lookup_error (scope
, tmpl
, member
,
14814 RETURN (error_mark_node
);
14817 else if (TREE_CODE (member
) == SCOPE_REF
14818 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14819 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14821 if (complain
& tf_error
)
14823 if (TYPE_P (TREE_OPERAND (member
, 0)))
14824 error ("%qT is not a class or namespace",
14825 TREE_OPERAND (member
, 0));
14827 error ("%qD is not a class or namespace",
14828 TREE_OPERAND (member
, 0));
14830 RETURN (error_mark_node
);
14832 else if (TREE_CODE (member
) == FIELD_DECL
)
14833 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14835 RETURN (finish_class_member_access_expr (object
, member
,
14836 /*template_p=*/false,
14841 RETURN (build_throw
14842 (RECUR (TREE_OPERAND (t
, 0))));
14846 vec
<constructor_elt
, va_gc
> *n
;
14847 constructor_elt
*ce
;
14848 unsigned HOST_WIDE_INT idx
;
14849 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14850 bool process_index_p
;
14852 bool need_copy_p
= false;
14855 if (type
== error_mark_node
)
14856 RETURN (error_mark_node
);
14858 /* digest_init will do the wrong thing if we let it. */
14859 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14862 /* We do not want to process the index of aggregate
14863 initializers as they are identifier nodes which will be
14864 looked up by digest_init. */
14865 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14867 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14868 newlen
= vec_safe_length (n
);
14869 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14871 if (ce
->index
&& process_index_p
14872 /* An identifier index is looked up in the type
14873 being initialized, not the current scope. */
14874 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14875 ce
->index
= RECUR (ce
->index
);
14877 if (PACK_EXPANSION_P (ce
->value
))
14879 /* Substitute into the pack expansion. */
14880 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14883 if (ce
->value
== error_mark_node
14884 || PACK_EXPANSION_P (ce
->value
))
14886 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14887 /* Just move the argument into place. */
14888 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14891 /* Update the length of the final CONSTRUCTOR
14892 arguments vector, and note that we will need to
14894 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14895 need_copy_p
= true;
14899 ce
->value
= RECUR (ce
->value
);
14904 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14906 vec_alloc (n
, newlen
);
14907 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14909 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14911 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14912 for (i
= 0; i
< len
; ++i
)
14913 CONSTRUCTOR_APPEND_ELT (n
, 0,
14914 TREE_VEC_ELT (ce
->value
, i
));
14917 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14921 r
= build_constructor (init_list_type_node
, n
);
14922 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14924 if (TREE_HAS_CONSTRUCTOR (t
))
14925 RETURN (finish_compound_literal (type
, r
, complain
));
14927 TREE_TYPE (r
) = type
;
14933 tree operand_0
= TREE_OPERAND (t
, 0);
14934 if (TYPE_P (operand_0
))
14936 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14937 RETURN (get_typeid (operand_0
, complain
));
14941 operand_0
= RECUR (operand_0
);
14942 RETURN (build_typeid (operand_0
, complain
));
14949 else if (DECL_PACK_P (t
))
14951 /* We don't build decls for an instantiation of a
14952 variadic capture proxy, we instantiate the elements
14954 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14955 return RECUR (DECL_VALUE_EXPR (t
));
14961 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14963 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14964 /* If the original type was a reference, we'll be wrapped in
14965 the appropriate INDIRECT_REF. */
14966 r
= convert_from_reference (r
);
14971 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14972 RECUR (TREE_OPERAND (t
, 0)),
14973 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14975 case OFFSETOF_EXPR
:
14976 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14980 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14981 complain
, in_decl
);
14983 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14985 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14987 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14992 tree old_stmt_expr
= cur_stmt_expr
;
14993 tree stmt_expr
= begin_stmt_expr ();
14995 cur_stmt_expr
= stmt_expr
;
14996 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14997 integral_constant_expression_p
);
14998 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14999 cur_stmt_expr
= old_stmt_expr
;
15001 /* If the resulting list of expression statement is empty,
15002 fold it further into void_zero_node. */
15003 if (empty_expr_stmt_p (stmt_expr
))
15004 stmt_expr
= void_zero_node
;
15006 RETURN (stmt_expr
);
15011 tree r
= build_lambda_expr ();
15013 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15014 LAMBDA_EXPR_CLOSURE (r
) = type
;
15015 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15017 LAMBDA_EXPR_LOCATION (r
)
15018 = LAMBDA_EXPR_LOCATION (t
);
15019 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15020 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15021 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15022 LAMBDA_EXPR_DISCRIMINATOR (r
)
15023 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15024 /* For a function scope, we want to use tsubst so that we don't
15025 complain about referring to an auto function before its return
15026 type has been deduced. Otherwise, we want to use tsubst_copy so
15027 that we look up the existing field/parameter/variable rather
15028 than build a new one. */
15029 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15030 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15031 scope
= tsubst (scope
, args
, complain
, in_decl
);
15032 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15034 /* Look up the parameter we want directly, as tsubst_copy
15035 doesn't do what we need. */
15036 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15037 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15038 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15039 parm
= DECL_CHAIN (parm
);
15041 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15042 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15043 DECL_CONTEXT (scope
) = fn
;
15046 scope
= RECUR (scope
);
15047 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15048 LAMBDA_EXPR_RETURN_TYPE (r
)
15049 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15051 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15052 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15054 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15055 determine_visibility (TYPE_NAME (type
));
15056 /* Now that we know visibility, instantiate the type so we have a
15057 declaration of the op() for later calls to lambda_function. */
15058 complete_type (type
);
15060 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15062 RETURN (build_lambda_object (r
));
15066 /* We can get here for a constant initializer of non-dependent type.
15067 FIXME stop folding in cp_parser_initializer_clause. */
15069 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15074 case TRANSACTION_EXPR
:
15075 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15076 integral_constant_expression_p
));
15079 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15081 case VEC_PERM_EXPR
:
15082 RETURN (build_x_vec_perm_expr (input_location
,
15083 RECUR (TREE_OPERAND (t
, 0)),
15084 RECUR (TREE_OPERAND (t
, 1)),
15085 RECUR (TREE_OPERAND (t
, 2)),
15089 /* Handle Objective-C++ constructs, if appropriate. */
15092 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15093 in_decl
, /*function_p=*/false);
15097 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15103 input_location
= loc
;
15107 /* Verify that the instantiated ARGS are valid. For type arguments,
15108 make sure that the type's linkage is ok. For non-type arguments,
15109 make sure they are constants if they are integral or enumerations.
15110 Emit an error under control of COMPLAIN, and return TRUE on error. */
15113 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15115 if (dependent_template_arg_p (t
))
15117 if (ARGUMENT_PACK_P (t
))
15119 tree vec
= ARGUMENT_PACK_ARGS (t
);
15120 int len
= TREE_VEC_LENGTH (vec
);
15121 bool result
= false;
15124 for (i
= 0; i
< len
; ++i
)
15125 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15129 else if (TYPE_P (t
))
15131 /* [basic.link]: A name with no linkage (notably, the name
15132 of a class or enumeration declared in a local scope)
15133 shall not be used to declare an entity with linkage.
15134 This implies that names with no linkage cannot be used as
15137 DR 757 relaxes this restriction for C++0x. */
15138 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15139 : no_linkage_check (t
, /*relaxed_p=*/false));
15143 /* DR 488 makes use of a type with no linkage cause
15144 type deduction to fail. */
15145 if (complain
& tf_error
)
15147 if (TYPE_ANONYMOUS_P (nt
))
15148 error ("%qT is/uses anonymous type", t
);
15150 error ("template argument for %qD uses local type %qT",
15155 /* In order to avoid all sorts of complications, we do not
15156 allow variably-modified types as template arguments. */
15157 else if (variably_modified_type_p (t
, NULL_TREE
))
15159 if (complain
& tf_error
)
15160 error ("%qT is a variably modified type", t
);
15164 /* Class template and alias template arguments should be OK. */
15165 else if (DECL_TYPE_TEMPLATE_P (t
))
15167 /* A non-type argument of integral or enumerated type must be a
15169 else if (TREE_TYPE (t
)
15170 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15171 && !TREE_CONSTANT (t
))
15173 if (complain
& tf_error
)
15174 error ("integral expression %qE is not constant", t
);
15181 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15183 int ix
, len
= DECL_NTPARMS (tmpl
);
15184 bool result
= false;
15186 for (ix
= 0; ix
!= len
; ix
++)
15188 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15191 if (result
&& (complain
& tf_error
))
15192 error (" trying to instantiate %qD", tmpl
);
15196 /* We're out of SFINAE context now, so generate diagnostics for the access
15197 errors we saw earlier when instantiating D from TMPL and ARGS. */
15200 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15202 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15203 tree type
= TREE_TYPE (pattern
);
15204 location_t loc
= input_location
;
15206 push_access_scope (d
);
15207 push_deferring_access_checks (dk_no_deferred
);
15208 input_location
= DECL_SOURCE_LOCATION (pattern
);
15209 tsubst (type
, args
, tf_warning_or_error
, d
);
15210 input_location
= loc
;
15211 pop_deferring_access_checks ();
15212 pop_access_scope (d
);
15215 /* Instantiate the indicated variable, function, or alias template TMPL with
15216 the template arguments in TARG_PTR. */
15219 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15221 tree targ_ptr
= orig_args
;
15225 bool access_ok
= true;
15227 if (tmpl
== error_mark_node
)
15228 return error_mark_node
;
15230 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15232 /* If this function is a clone, handle it specially. */
15233 if (DECL_CLONED_FUNCTION_P (tmpl
))
15238 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15239 DECL_CLONED_FUNCTION. */
15240 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15241 targ_ptr
, complain
);
15242 if (spec
== error_mark_node
)
15243 return error_mark_node
;
15245 /* Look for the clone. */
15246 FOR_EACH_CLONE (clone
, spec
)
15247 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15249 /* We should always have found the clone by now. */
15250 gcc_unreachable ();
15254 /* Check to see if we already have this specialization. */
15255 gen_tmpl
= most_general_template (tmpl
);
15256 if (tmpl
!= gen_tmpl
)
15257 /* The TMPL is a partial instantiation. To get a full set of
15258 arguments we must add the arguments used to perform the
15259 partial instantiation. */
15260 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15263 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15264 but it doesn't seem to be on the hot path. */
15265 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15267 gcc_assert (tmpl
== gen_tmpl
15268 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15270 || fndecl
== NULL_TREE
);
15272 if (spec
!= NULL_TREE
)
15274 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15276 if (complain
& tf_error
)
15277 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15278 return error_mark_node
;
15283 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15285 return error_mark_node
;
15287 /* We are building a FUNCTION_DECL, during which the access of its
15288 parameters and return types have to be checked. However this
15289 FUNCTION_DECL which is the desired context for access checking
15290 is not built yet. We solve this chicken-and-egg problem by
15291 deferring all checks until we have the FUNCTION_DECL. */
15292 push_deferring_access_checks (dk_deferred
);
15294 /* Instantiation of the function happens in the context of the function
15295 template, not the context of the overload resolution we're doing. */
15296 push_to_top_level ();
15297 /* If there are dependent arguments, e.g. because we're doing partial
15298 ordering, make sure processing_template_decl stays set. */
15299 if (uses_template_parms (targ_ptr
))
15300 ++processing_template_decl
;
15301 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15303 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15304 complain
, gen_tmpl
);
15305 push_nested_class (ctx
);
15307 /* Substitute template parameters to obtain the specialization. */
15308 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15309 targ_ptr
, complain
, gen_tmpl
);
15310 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15311 pop_nested_class ();
15312 pop_from_top_level ();
15314 if (fndecl
== error_mark_node
)
15316 pop_deferring_access_checks ();
15317 return error_mark_node
;
15320 /* The DECL_TI_TEMPLATE should always be the immediate parent
15321 template, not the most general template. */
15322 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15324 /* Now we know the specialization, compute access previously
15326 push_access_scope (fndecl
);
15327 if (!perform_deferred_access_checks (complain
))
15329 pop_access_scope (fndecl
);
15330 pop_deferring_access_checks ();
15332 /* If we've just instantiated the main entry point for a function,
15333 instantiate all the alternate entry points as well. We do this
15334 by cloning the instantiation of the main entry point, not by
15335 instantiating the template clones. */
15336 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15337 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15341 if (!(complain
& tf_error
))
15343 /* Remember to reinstantiate when we're out of SFINAE so the user
15344 can see the errors. */
15345 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15347 return error_mark_node
;
15352 /* Wrapper for instantiate_template_1. */
15355 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15358 timevar_push (TV_TEMPLATE_INST
);
15359 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15360 timevar_pop (TV_TEMPLATE_INST
);
15364 /* Instantiate the alias template TMPL with ARGS. Also push a template
15365 instantiation level, which instantiate_template doesn't do because
15366 functions and variables have sufficient context established by the
15370 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15372 struct pending_template
*old_last_pend
= last_pending_template
;
15373 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15374 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15375 return error_mark_node
;
15376 tree tinst
= build_tree_list (tmpl
, args
);
15377 if (!push_tinst_level (tinst
))
15380 return error_mark_node
;
15384 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15385 args
, tmpl
, complain
,
15386 /*require_all_args=*/true,
15387 /*use_default_args=*/true);
15389 tree r
= instantiate_template (tmpl
, args
, complain
);
15390 pop_tinst_level ();
15391 /* We can't free this if a pending_template entry or last_error_tinst_level
15392 is pointing at it. */
15393 if (last_pending_template
== old_last_pend
15394 && last_error_tinst_level
== old_error_tinst
)
15400 /* PARM is a template parameter pack for FN. Returns true iff
15401 PARM is used in a deducible way in the argument list of FN. */
15404 pack_deducible_p (tree parm
, tree fn
)
15406 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15407 for (; t
; t
= TREE_CHAIN (t
))
15409 tree type
= TREE_VALUE (t
);
15411 if (!PACK_EXPANSION_P (type
))
15413 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15414 packs
; packs
= TREE_CHAIN (packs
))
15415 if (TREE_VALUE (packs
) == parm
)
15417 /* The template parameter pack is used in a function parameter
15418 pack. If this is the end of the parameter list, the
15419 template parameter pack is deducible. */
15420 if (TREE_CHAIN (t
) == void_list_node
)
15423 /* Otherwise, not. Well, it could be deduced from
15424 a non-pack parameter, but doing so would end up with
15425 a deduction mismatch, so don't bother. */
15429 /* The template parameter pack isn't used in any function parameter
15430 packs, but it might be used deeper, e.g. tuple<Args...>. */
15434 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15435 NARGS elements of the arguments that are being used when calling
15436 it. TARGS is a vector into which the deduced template arguments
15439 Returns either a FUNCTION_DECL for the matching specialization of FN or
15440 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15441 true, diagnostics will be printed to explain why it failed.
15443 If FN is a conversion operator, or we are trying to produce a specific
15444 specialization, RETURN_TYPE is the return type desired.
15446 The EXPLICIT_TARGS are explicit template arguments provided via a
15449 The parameter STRICT is one of:
15452 We are deducing arguments for a function call, as in
15453 [temp.deduct.call].
15456 We are deducing arguments for a conversion function, as in
15457 [temp.deduct.conv].
15460 We are deducing arguments when doing an explicit instantiation
15461 as in [temp.explicit], when determining an explicit specialization
15462 as in [temp.expl.spec], or when taking the address of a function
15463 template, as in [temp.deduct.funcaddr]. */
15466 fn_type_unification (tree fn
,
15467 tree explicit_targs
,
15470 unsigned int nargs
,
15472 unification_kind_t strict
,
15479 tree decl
= NULL_TREE
;
15480 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15482 static int deduction_depth
;
15483 struct pending_template
*old_last_pend
= last_pending_template
;
15484 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15485 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15487 tree r
= error_mark_node
;
15490 complain
|= tf_decltype
;
15492 /* In C++0x, it's possible to have a function template whose type depends
15493 on itself recursively. This is most obvious with decltype, but can also
15494 occur with enumeration scope (c++/48969). So we need to catch infinite
15495 recursion and reject the substitution at deduction time; this function
15496 will return error_mark_node for any repeated substitution.
15498 This also catches excessive recursion such as when f<N> depends on
15499 f<N-1> across all integers, and returns error_mark_node for all the
15500 substitutions back up to the initial one.
15502 This is, of course, not reentrant. */
15503 if (excessive_deduction_depth
)
15504 return error_mark_node
;
15505 tinst
= build_tree_list (fn
, NULL_TREE
);
15508 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15510 fntype
= TREE_TYPE (fn
);
15511 if (explicit_targs
)
15515 The specified template arguments must match the template
15516 parameters in kind (i.e., type, nontype, template), and there
15517 must not be more arguments than there are parameters;
15518 otherwise type deduction fails.
15520 Nontype arguments must match the types of the corresponding
15521 nontype template parameters, or must be convertible to the
15522 types of the corresponding nontype parameters as specified in
15523 _temp.arg.nontype_, otherwise type deduction fails.
15525 All references in the function type of the function template
15526 to the corresponding template parameters are replaced by the
15527 specified template argument values. If a substitution in a
15528 template parameter or in the function type of the function
15529 template results in an invalid type, type deduction fails. */
15530 int i
, len
= TREE_VEC_LENGTH (tparms
);
15531 location_t loc
= input_location
;
15532 bool incomplete
= false;
15534 /* Adjust any explicit template arguments before entering the
15535 substitution context. */
15537 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15539 /*require_all_args=*/false,
15540 /*use_default_args=*/false));
15541 if (explicit_targs
== error_mark_node
)
15544 /* Substitute the explicit args into the function type. This is
15545 necessary so that, for instance, explicitly declared function
15546 arguments can match null pointed constants. If we were given
15547 an incomplete set of explicit args, we must not do semantic
15548 processing during substitution as we could create partial
15550 for (i
= 0; i
< len
; i
++)
15552 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15553 bool parameter_pack
= false;
15554 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15556 /* Dig out the actual parm. */
15557 if (TREE_CODE (parm
) == TYPE_DECL
15558 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15560 parm
= TREE_TYPE (parm
);
15561 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15563 else if (TREE_CODE (parm
) == PARM_DECL
)
15565 parm
= DECL_INITIAL (parm
);
15566 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15569 if (!parameter_pack
&& targ
== NULL_TREE
)
15570 /* No explicit argument for this template parameter. */
15573 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15575 /* Mark the argument pack as "incomplete". We could
15576 still deduce more arguments during unification.
15577 We remove this mark in type_unification_real. */
15580 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15581 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15582 = ARGUMENT_PACK_ARGS (targ
);
15585 /* We have some incomplete argument packs. */
15590 TREE_VALUE (tinst
) = explicit_targs
;
15591 if (!push_tinst_level (tinst
))
15593 excessive_deduction_depth
= true;
15596 processing_template_decl
+= incomplete
;
15597 input_location
= DECL_SOURCE_LOCATION (fn
);
15598 /* Ignore any access checks; we'll see them again in
15599 instantiate_template and they might have the wrong
15600 access path at this point. */
15601 push_deferring_access_checks (dk_deferred
);
15602 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15603 complain
| tf_partial
, NULL_TREE
);
15604 pop_deferring_access_checks ();
15605 input_location
= loc
;
15606 processing_template_decl
-= incomplete
;
15607 pop_tinst_level ();
15609 if (fntype
== error_mark_node
)
15612 /* Place the explicitly specified arguments in TARGS. */
15613 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15614 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15617 /* Never do unification on the 'this' parameter. */
15618 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15624 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15625 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15626 new_args
[0] = return_type
;
15627 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15632 /* We allow incomplete unification without an error message here
15633 because the standard doesn't seem to explicitly prohibit it. Our
15634 callers must be ready to deal with unification failures in any
15637 TREE_VALUE (tinst
) = targs
;
15638 /* If we aren't explaining yet, push tinst context so we can see where
15639 any errors (e.g. from class instantiations triggered by instantiation
15640 of default template arguments) come from. If we are explaining, this
15641 context is redundant. */
15642 if (!explain_p
&& !push_tinst_level (tinst
))
15644 excessive_deduction_depth
= true;
15648 /* type_unification_real will pass back any access checks from default
15649 template argument substitution. */
15650 vec
<deferred_access_check
, va_gc
> *checks
;
15653 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15654 targs
, parms
, args
, nargs
, /*subr=*/0,
15655 strict
, flags
, &checks
, explain_p
);
15657 pop_tinst_level ();
15661 /* Now that we have bindings for all of the template arguments,
15662 ensure that the arguments deduced for the template template
15663 parameters have compatible template parameter lists. We cannot
15664 check this property before we have deduced all template
15665 arguments, because the template parameter types of a template
15666 template parameter might depend on prior template parameters
15667 deduced after the template template parameter. The following
15668 ill-formed example illustrates this issue:
15670 template<typename T, template<T> class C> void f(C<5>, T);
15672 template<int N> struct X {};
15675 f(X<5>(), 5l); // error: template argument deduction fails
15678 The template parameter list of 'C' depends on the template type
15679 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15680 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15681 time that we deduce 'C'. */
15682 if (!template_template_parm_bindings_ok_p
15683 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15685 unify_inconsistent_template_template_parameters (explain_p
);
15689 /* All is well so far. Now, check:
15693 When all template arguments have been deduced, all uses of
15694 template parameters in nondeduced contexts are replaced with
15695 the corresponding deduced argument values. If the
15696 substitution results in an invalid type, as described above,
15697 type deduction fails. */
15698 TREE_VALUE (tinst
) = targs
;
15699 if (!push_tinst_level (tinst
))
15701 excessive_deduction_depth
= true;
15705 /* Also collect access checks from the instantiation. */
15706 reopen_deferring_access_checks (checks
);
15708 decl
= instantiate_template (fn
, targs
, complain
);
15710 checks
= get_deferred_access_checks ();
15711 pop_deferring_access_checks ();
15713 pop_tinst_level ();
15715 if (decl
== error_mark_node
)
15718 /* Now perform any access checks encountered during substitution. */
15719 push_access_scope (decl
);
15720 ok
= perform_access_checks (checks
, complain
);
15721 pop_access_scope (decl
);
15725 /* If we're looking for an exact match, check that what we got
15726 is indeed an exact match. It might not be if some template
15727 parameters are used in non-deduced contexts. */
15728 if (strict
== DEDUCE_EXACT
)
15730 tree substed
= TREE_TYPE (decl
);
15734 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15736 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15737 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15738 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15740 unify_type_mismatch (explain_p
, args
[i
],
15741 TREE_VALUE (sarg
));
15750 if (excessive_deduction_depth
)
15752 if (deduction_depth
== 0)
15753 /* Reset once we're all the way out. */
15754 excessive_deduction_depth
= false;
15757 /* We can't free this if a pending_template entry or last_error_tinst_level
15758 is pointing at it. */
15759 if (last_pending_template
== old_last_pend
15760 && last_error_tinst_level
== old_error_tinst
)
15766 /* Adjust types before performing type deduction, as described in
15767 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15768 sections are symmetric. PARM is the type of a function parameter
15769 or the return type of the conversion function. ARG is the type of
15770 the argument passed to the call, or the type of the value
15771 initialized with the result of the conversion function.
15772 ARG_EXPR is the original argument expression, which may be null. */
15775 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15789 /* Swap PARM and ARG throughout the remainder of this
15790 function; the handling is precisely symmetric since PARM
15791 will initialize ARG rather than vice versa. */
15799 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15800 too, but here handle it by stripping the reference from PARM
15801 rather than by adding it to ARG. */
15802 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15803 && TYPE_REF_IS_RVALUE (*parm
)
15804 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15805 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15806 && TREE_CODE (*arg
) == REFERENCE_TYPE
15807 && !TYPE_REF_IS_RVALUE (*arg
))
15808 *parm
= TREE_TYPE (*parm
);
15809 /* Nothing else to do in this case. */
15813 gcc_unreachable ();
15816 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15818 /* [temp.deduct.call]
15820 If P is not a reference type:
15822 --If A is an array type, the pointer type produced by the
15823 array-to-pointer standard conversion (_conv.array_) is
15824 used in place of A for type deduction; otherwise,
15826 --If A is a function type, the pointer type produced by
15827 the function-to-pointer standard conversion
15828 (_conv.func_) is used in place of A for type deduction;
15831 --If A is a cv-qualified type, the top level
15832 cv-qualifiers of A's type are ignored for type
15834 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15835 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15836 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15837 *arg
= build_pointer_type (*arg
);
15839 *arg
= TYPE_MAIN_VARIANT (*arg
);
15842 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15843 of the form T&&, where T is a template parameter, and the argument
15844 is an lvalue, T is deduced as A& */
15845 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15846 && TYPE_REF_IS_RVALUE (*parm
)
15847 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15848 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15849 && (arg_expr
? real_lvalue_p (arg_expr
)
15850 /* try_one_overload doesn't provide an arg_expr, but
15851 functions are always lvalues. */
15852 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15853 *arg
= build_reference_type (*arg
);
15855 /* [temp.deduct.call]
15857 If P is a cv-qualified type, the top level cv-qualifiers
15858 of P's type are ignored for type deduction. If P is a
15859 reference type, the type referred to by P is used for
15861 *parm
= TYPE_MAIN_VARIANT (*parm
);
15862 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15864 *parm
= TREE_TYPE (*parm
);
15865 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15868 /* DR 322. For conversion deduction, remove a reference type on parm
15869 too (which has been swapped into ARG). */
15870 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15871 *arg
= TREE_TYPE (*arg
);
15876 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15877 template which does contain any deducible template parameters; check if
15878 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15879 unify_one_argument. */
15882 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15883 int flags
, bool explain_p
)
15888 type
= TREE_TYPE (arg
);
15892 if (same_type_p (parm
, type
))
15893 return unify_success (explain_p
);
15895 if (strict
== DEDUCE_CONV
)
15897 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15898 explain_p
? tf_warning_or_error
: tf_none
))
15899 return unify_success (explain_p
);
15901 else if (strict
!= DEDUCE_EXACT
)
15903 if (can_convert_arg (parm
, type
,
15904 TYPE_P (arg
) ? NULL_TREE
: arg
,
15905 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15906 return unify_success (explain_p
);
15909 if (strict
== DEDUCE_EXACT
)
15910 return unify_type_mismatch (explain_p
, parm
, arg
);
15912 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15915 static bool uses_deducible_template_parms (tree type
);
15917 /* Returns true iff the expression EXPR is one from which a template
15918 argument can be deduced. In other words, if it's an undecorated
15919 use of a template non-type parameter. */
15922 deducible_expression (tree expr
)
15924 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15927 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15928 deducible way; that is, if it has a max value of <PARM> - 1. */
15931 deducible_array_bound (tree domain
)
15933 if (domain
== NULL_TREE
)
15936 tree max
= TYPE_MAX_VALUE (domain
);
15937 if (TREE_CODE (max
) != MINUS_EXPR
)
15940 return deducible_expression (TREE_OPERAND (max
, 0));
15943 /* Returns true iff the template arguments ARGS use a template parameter
15944 in a deducible way. */
15947 deducible_template_args (tree args
)
15949 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15952 tree elt
= TREE_VEC_ELT (args
, i
);
15953 if (ARGUMENT_PACK_P (elt
))
15954 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15957 if (PACK_EXPANSION_P (elt
))
15958 elt
= PACK_EXPANSION_PATTERN (elt
);
15959 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15961 else if (TYPE_P (elt
))
15962 deducible
= uses_deducible_template_parms (elt
);
15964 deducible
= deducible_expression (elt
);
15972 /* Returns true iff TYPE contains any deducible references to template
15973 parameters, as per 14.8.2.5. */
15976 uses_deducible_template_parms (tree type
)
15978 if (PACK_EXPANSION_P (type
))
15979 type
= PACK_EXPANSION_PATTERN (type
);
15986 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15987 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15993 if (POINTER_TYPE_P (type
))
15994 return uses_deducible_template_parms (TREE_TYPE (type
));
15996 /* T[integer-constant ]
15998 if (TREE_CODE (type
) == ARRAY_TYPE
)
15999 return (uses_deducible_template_parms (TREE_TYPE (type
))
16000 || deducible_array_bound (TYPE_DOMAIN (type
)));
16012 if (TYPE_PTRMEM_P (type
))
16013 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16014 || (uses_deducible_template_parms
16015 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16017 /* template-name <T> (where template-name refers to a class template)
16018 template-name <i> (where template-name refers to a class template) */
16019 if (CLASS_TYPE_P (type
)
16020 && CLASSTYPE_TEMPLATE_INFO (type
)
16021 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16022 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16023 (CLASSTYPE_TI_ARGS (type
)));
16028 if (TREE_CODE (type
) == FUNCTION_TYPE
16029 || TREE_CODE (type
) == METHOD_TYPE
)
16031 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16033 tree parm
= TYPE_ARG_TYPES (type
);
16034 if (TREE_CODE (type
) == METHOD_TYPE
)
16035 parm
= TREE_CHAIN (parm
);
16036 for (; parm
; parm
= TREE_CHAIN (parm
))
16037 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16044 /* Subroutine of type_unification_real and unify_pack_expansion to
16045 handle unification of a single P/A pair. Parameters are as
16046 for those functions. */
16049 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16050 int subr
, unification_kind_t strict
, int flags
,
16053 tree arg_expr
= NULL_TREE
;
16056 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16057 return unify_invalid (explain_p
);
16058 if (arg
== unknown_type_node
)
16059 /* We can't deduce anything from this, but we might get all the
16060 template args from other function args. */
16061 return unify_success (explain_p
);
16063 /* Implicit conversions (Clause 4) will be performed on a function
16064 argument to convert it to the type of the corresponding function
16065 parameter if the parameter type contains no template-parameters that
16066 participate in template argument deduction. */
16067 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16068 /* For function parameters that contain no template-parameters at all,
16069 we have historically checked for convertibility in order to shortcut
16070 consideration of this candidate. */
16071 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16073 else if (strict
== DEDUCE_CALL
16074 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16075 /* For function parameters with only non-deducible template parameters,
16077 return unify_success (explain_p
);
16082 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16083 | UNIFY_ALLOW_MORE_CV_QUAL
16084 | UNIFY_ALLOW_DERIVED
);
16088 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16092 arg_strict
= UNIFY_ALLOW_NONE
;
16096 gcc_unreachable ();
16099 /* We only do these transformations if this is the top-level
16100 parameter_type_list in a call or declaration matching; in other
16101 situations (nested function declarators, template argument lists) we
16102 won't be comparing a type to an expression, and we don't do any type
16108 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16109 if (type_unknown_p (arg
))
16111 /* [temp.deduct.type] A template-argument can be
16112 deduced from a pointer to function or pointer
16113 to member function argument if the set of
16114 overloaded functions does not contain function
16115 templates and at most one of a set of
16116 overloaded functions provides a unique
16119 if (resolve_overloaded_unification
16120 (tparms
, targs
, parm
, arg
, strict
,
16121 arg_strict
, explain_p
))
16122 return unify_success (explain_p
);
16123 return unify_overload_resolution_failure (explain_p
, arg
);
16127 arg
= unlowered_expr_type (arg
);
16128 if (arg
== error_mark_node
)
16129 return unify_invalid (explain_p
);
16133 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16136 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16137 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16139 /* For deduction from an init-list we need the actual list. */
16140 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16142 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16145 /* Most parms like fn_type_unification.
16147 If SUBR is 1, we're being called recursively (to unify the
16148 arguments of a function or method parameter of a function
16151 CHECKS is a pointer to a vector of access checks encountered while
16152 substituting default template arguments. */
16155 type_unification_real (tree tparms
,
16159 unsigned int xnargs
,
16161 unification_kind_t strict
,
16163 vec
<deferred_access_check
, va_gc
> **checks
,
16168 int ntparms
= TREE_VEC_LENGTH (tparms
);
16169 int saw_undeduced
= 0;
16172 unsigned int nargs
;
16175 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16176 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16177 gcc_assert (ntparms
> 0);
16179 /* Reset the number of non-defaulted template arguments contained
16181 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16189 while (parms
&& parms
!= void_list_node
16192 parm
= TREE_VALUE (parms
);
16194 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16195 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16196 /* For a function parameter pack that occurs at the end of the
16197 parameter-declaration-list, the type A of each remaining
16198 argument of the call is compared with the type P of the
16199 declarator-id of the function parameter pack. */
16202 parms
= TREE_CHAIN (parms
);
16204 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16205 /* For a function parameter pack that does not occur at the
16206 end of the parameter-declaration-list, the type of the
16207 parameter pack is a non-deduced context. */
16213 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16219 && parms
!= void_list_node
16220 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16222 /* Unify the remaining arguments with the pack expansion type. */
16224 tree parmvec
= make_tree_vec (1);
16226 /* Allocate a TREE_VEC and copy in all of the arguments */
16227 argvec
= make_tree_vec (nargs
- ia
);
16228 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16229 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16231 /* Copy the parameter into parmvec. */
16232 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16233 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16234 /*subr=*/subr
, explain_p
))
16237 /* Advance to the end of the list of parameters. */
16238 parms
= TREE_CHAIN (parms
);
16241 /* Fail if we've reached the end of the parm list, and more args
16242 are present, and the parm list isn't variadic. */
16243 if (ia
< nargs
&& parms
== void_list_node
)
16244 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16245 /* Fail if parms are left and they don't have default values. */
16246 if (parms
&& parms
!= void_list_node
16247 && TREE_PURPOSE (parms
) == NULL_TREE
)
16249 unsigned int count
= nargs
;
16251 while (p
&& p
!= void_list_node
)
16254 p
= TREE_CHAIN (p
);
16256 return unify_too_few_arguments (explain_p
, ia
, count
);
16261 tsubst_flags_t complain
= (explain_p
16262 ? tf_warning_or_error
16265 for (i
= 0; i
< ntparms
; i
++)
16267 tree targ
= TREE_VEC_ELT (targs
, i
);
16268 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16270 /* Clear the "incomplete" flags on all argument packs now so that
16271 substituting them into later default arguments works. */
16272 if (targ
&& ARGUMENT_PACK_P (targ
))
16274 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16275 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16278 if (targ
|| tparm
== error_mark_node
)
16280 tparm
= TREE_VALUE (tparm
);
16282 /* If this is an undeduced nontype parameter that depends on
16283 a type parameter, try another pass; its type may have been
16284 deduced from a later argument than the one from which
16285 this parameter can be deduced. */
16286 if (TREE_CODE (tparm
) == PARM_DECL
16287 && uses_template_parms (TREE_TYPE (tparm
))
16288 && !saw_undeduced
++)
16291 /* Core issue #226 (C++0x) [temp.deduct]:
16293 If a template argument has not been deduced, its
16294 default template argument, if any, is used.
16296 When we are in C++98 mode, TREE_PURPOSE will either
16297 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16298 to explicitly check cxx_dialect here. */
16299 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16301 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16302 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16303 reopen_deferring_access_checks (*checks
);
16304 location_t save_loc
= input_location
;
16306 input_location
= DECL_SOURCE_LOCATION (parm
);
16307 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16308 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16310 input_location
= save_loc
;
16311 *checks
= get_deferred_access_checks ();
16312 pop_deferring_access_checks ();
16313 if (arg
== error_mark_node
)
16317 TREE_VEC_ELT (targs
, i
) = arg
;
16318 /* The position of the first default template argument,
16319 is also the number of non-defaulted arguments in TARGS.
16321 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16322 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16327 /* If the type parameter is a parameter pack, then it will
16328 be deduced to an empty parameter pack. */
16329 if (template_parameter_pack_p (tparm
))
16333 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16335 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16336 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16337 TREE_CONSTANT (arg
) = 1;
16340 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16342 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16344 TREE_VEC_ELT (targs
, i
) = arg
;
16348 return unify_parameter_deduction_failure (explain_p
, tparm
);
16351 #ifdef ENABLE_CHECKING
16352 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16353 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16356 return unify_success (explain_p
);
16359 /* Subroutine of type_unification_real. Args are like the variables
16360 at the call site. ARG is an overloaded function (or template-id);
16361 we try deducing template args from each of the overloads, and if
16362 only one succeeds, we go with that. Modifies TARGS and returns
16363 true on success. */
16366 resolve_overloaded_unification (tree tparms
,
16370 unification_kind_t strict
,
16374 tree tempargs
= copy_node (targs
);
16376 tree goodfn
= NULL_TREE
;
16379 if (TREE_CODE (arg
) == ADDR_EXPR
)
16381 arg
= TREE_OPERAND (arg
, 0);
16387 if (TREE_CODE (arg
) == COMPONENT_REF
)
16388 /* Handle `&x' where `x' is some static or non-static member
16390 arg
= TREE_OPERAND (arg
, 1);
16392 if (TREE_CODE (arg
) == OFFSET_REF
)
16393 arg
= TREE_OPERAND (arg
, 1);
16395 /* Strip baselink information. */
16396 if (BASELINK_P (arg
))
16397 arg
= BASELINK_FUNCTIONS (arg
);
16399 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16401 /* If we got some explicit template args, we need to plug them into
16402 the affected templates before we try to unify, in case the
16403 explicit args will completely resolve the templates in question. */
16406 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16407 arg
= TREE_OPERAND (arg
, 0);
16409 for (; arg
; arg
= OVL_NEXT (arg
))
16411 tree fn
= OVL_CURRENT (arg
);
16412 tree subargs
, elem
;
16414 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16417 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16418 expl_subargs
, NULL_TREE
, tf_none
,
16419 /*require_all_args=*/true,
16420 /*use_default_args=*/true);
16421 if (subargs
!= error_mark_node
16422 && !any_dependent_template_arguments_p (subargs
))
16424 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
16425 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16426 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16427 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16436 /* If no templates (or more than one) are fully resolved by the
16437 explicit arguments, this template-id is a non-deduced context; it
16438 could still be OK if we deduce all template arguments for the
16439 enclosing call through other arguments. */
16443 else if (TREE_CODE (arg
) != OVERLOAD
16444 && TREE_CODE (arg
) != FUNCTION_DECL
)
16445 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16446 -- but the deduction does not succeed because the expression is
16447 not just the function on its own. */
16450 for (; arg
; arg
= OVL_NEXT (arg
))
16451 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16452 TREE_TYPE (OVL_CURRENT (arg
)),
16453 strict
, sub_strict
, addr_p
, explain_p
)
16454 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16456 goodfn
= OVL_CURRENT (arg
);
16460 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16461 to function or pointer to member function argument if the set of
16462 overloaded functions does not contain function templates and at most
16463 one of a set of overloaded functions provides a unique match.
16465 So if we found multiple possibilities, we return success but don't
16466 deduce anything. */
16470 int i
= TREE_VEC_LENGTH (targs
);
16472 if (TREE_VEC_ELT (tempargs
, i
))
16473 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16481 /* Core DR 115: In contexts where deduction is done and fails, or in
16482 contexts where deduction is not done, if a template argument list is
16483 specified and it, along with any default template arguments, identifies
16484 a single function template specialization, then the template-id is an
16485 lvalue for the function template specialization. */
16488 resolve_nondeduced_context (tree orig_expr
)
16490 tree expr
, offset
, baselink
;
16493 if (!type_unknown_p (orig_expr
))
16498 offset
= NULL_TREE
;
16499 baselink
= NULL_TREE
;
16501 if (TREE_CODE (expr
) == ADDR_EXPR
)
16503 expr
= TREE_OPERAND (expr
, 0);
16506 if (TREE_CODE (expr
) == OFFSET_REF
)
16509 expr
= TREE_OPERAND (expr
, 1);
16511 if (BASELINK_P (expr
))
16514 expr
= BASELINK_FUNCTIONS (expr
);
16517 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16520 tree goodfn
= NULL_TREE
;
16522 /* If we got some explicit template args, we need to plug them into
16523 the affected templates before we try to unify, in case the
16524 explicit args will completely resolve the templates in question. */
16526 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16527 tree arg
= TREE_OPERAND (expr
, 0);
16528 tree badfn
= NULL_TREE
;
16529 tree badargs
= NULL_TREE
;
16531 for (; arg
; arg
= OVL_NEXT (arg
))
16533 tree fn
= OVL_CURRENT (arg
);
16534 tree subargs
, elem
;
16536 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16539 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16540 expl_subargs
, NULL_TREE
, tf_none
,
16541 /*require_all_args=*/true,
16542 /*use_default_args=*/true);
16543 if (subargs
!= error_mark_node
16544 && !any_dependent_template_arguments_p (subargs
))
16546 elem
= instantiate_template (fn
, subargs
, tf_none
);
16547 if (elem
== error_mark_node
)
16552 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16561 mark_used (goodfn
);
16564 expr
= build_baselink (BASELINK_BINFO (baselink
),
16565 BASELINK_ACCESS_BINFO (baselink
),
16566 expr
, BASELINK_OPTYPE (baselink
));
16570 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16571 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16574 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16577 else if (good
== 0 && badargs
)
16578 /* There were no good options and at least one bad one, so let the
16579 user know what the problem is. */
16580 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16585 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16586 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16587 different overloads deduce different arguments for a given parm.
16588 ADDR_P is true if the expression for which deduction is being
16589 performed was of the form "& fn" rather than simply "fn".
16591 Returns 1 on success. */
16594 try_one_overload (tree tparms
,
16599 unification_kind_t strict
,
16608 if (arg
== error_mark_node
)
16611 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16612 to function or pointer to member function argument if the set of
16613 overloaded functions does not contain function templates and at most
16614 one of a set of overloaded functions provides a unique match.
16616 So if this is a template, just return success. */
16618 if (uses_template_parms (arg
))
16621 if (TREE_CODE (arg
) == METHOD_TYPE
)
16622 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16624 arg
= build_pointer_type (arg
);
16626 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16628 /* We don't copy orig_targs for this because if we have already deduced
16629 some template args from previous args, unify would complain when we
16630 try to deduce a template parameter for the same argument, even though
16631 there isn't really a conflict. */
16632 nargs
= TREE_VEC_LENGTH (targs
);
16633 tempargs
= make_tree_vec (nargs
);
16635 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16638 /* First make sure we didn't deduce anything that conflicts with
16639 explicitly specified args. */
16640 for (i
= nargs
; i
--; )
16642 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16643 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16647 else if (uses_template_parms (elt
))
16648 /* Since we're unifying against ourselves, we will fill in
16649 template args used in the function parm list with our own
16650 template parms. Discard them. */
16651 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16652 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16656 for (i
= nargs
; i
--; )
16658 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16661 TREE_VEC_ELT (targs
, i
) = elt
;
16667 /* PARM is a template class (perhaps with unbound template
16668 parameters). ARG is a fully instantiated type. If ARG can be
16669 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16670 TARGS are as for unify. */
16673 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16676 tree copy_of_targs
;
16678 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16679 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16680 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16683 /* We need to make a new template argument vector for the call to
16684 unify. If we used TARGS, we'd clutter it up with the result of
16685 the attempted unification, even if this class didn't work out.
16686 We also don't want to commit ourselves to all the unifications
16687 we've already done, since unification is supposed to be done on
16688 an argument-by-argument basis. In other words, consider the
16689 following pathological case:
16691 template <int I, int J, int K>
16694 template <int I, int J>
16695 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16697 template <int I, int J, int K>
16698 void f(S<I, J, K>, S<I, I, I>);
16707 Now, by the time we consider the unification involving `s2', we
16708 already know that we must have `f<0, 0, 0>'. But, even though
16709 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16710 because there are two ways to unify base classes of S<0, 1, 2>
16711 with S<I, I, I>. If we kept the already deduced knowledge, we
16712 would reject the possibility I=1. */
16713 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16715 /* If unification failed, we're done. */
16716 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16717 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16723 /* Given a template type PARM and a class type ARG, find the unique
16724 base type in ARG that is an instance of PARM. We do not examine
16725 ARG itself; only its base-classes. If there is not exactly one
16726 appropriate base class, return NULL_TREE. PARM may be the type of
16727 a partial specialization, as well as a plain template type. Used
16730 static enum template_base_result
16731 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16732 bool explain_p
, tree
*result
)
16734 tree rval
= NULL_TREE
;
16737 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16739 binfo
= TYPE_BINFO (complete_type (arg
));
16742 /* The type could not be completed. */
16743 *result
= NULL_TREE
;
16744 return tbr_incomplete_type
;
16747 /* Walk in inheritance graph order. The search order is not
16748 important, and this avoids multiple walks of virtual bases. */
16749 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16751 tree r
= try_class_unification (tparms
, targs
, parm
,
16752 BINFO_TYPE (binfo
), explain_p
);
16756 /* If there is more than one satisfactory baseclass, then:
16760 If they yield more than one possible deduced A, the type
16764 if (rval
&& !same_type_p (r
, rval
))
16766 *result
= NULL_TREE
;
16767 return tbr_ambiguous_baseclass
;
16775 return tbr_success
;
16778 /* Returns the level of DECL, which declares a template parameter. */
16781 template_decl_level (tree decl
)
16783 switch (TREE_CODE (decl
))
16786 case TEMPLATE_DECL
:
16787 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16790 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16793 gcc_unreachable ();
16798 /* Decide whether ARG can be unified with PARM, considering only the
16799 cv-qualifiers of each type, given STRICT as documented for unify.
16800 Returns nonzero iff the unification is OK on that basis. */
16803 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16805 int arg_quals
= cp_type_quals (arg
);
16806 int parm_quals
= cp_type_quals (parm
);
16808 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16809 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16811 /* Although a CVR qualifier is ignored when being applied to a
16812 substituted template parameter ([8.3.2]/1 for example), that
16813 does not allow us to unify "const T" with "int&" because both
16814 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16815 It is ok when we're allowing additional CV qualifiers
16816 at the outer level [14.8.2.1]/3,1st bullet. */
16817 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16818 || TREE_CODE (arg
) == FUNCTION_TYPE
16819 || TREE_CODE (arg
) == METHOD_TYPE
)
16820 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16823 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16824 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16828 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16829 && (arg_quals
& parm_quals
) != parm_quals
)
16832 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16833 && (parm_quals
& arg_quals
) != arg_quals
)
16839 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16841 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16843 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16844 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16845 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16847 *index
= TEMPLATE_TYPE_IDX (parm
);
16848 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16852 *index
= TEMPLATE_PARM_IDX (parm
);
16853 *level
= TEMPLATE_PARM_LEVEL (parm
);
16857 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16859 if (unify (TP, TA, P, A, S, EP)) \
16863 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16864 expansion at the end of PACKED_PARMS. Returns 0 if the type
16865 deduction succeeds, 1 otherwise. STRICT is the same as in
16866 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16867 call argument list. We'll need to adjust the arguments to make them
16868 types. SUBR tells us if this is from a recursive call to
16869 type_unification_real, or for comparing two template argument
16873 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16874 tree packed_args
, unification_kind_t strict
,
16875 bool subr
, bool explain_p
)
16878 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16879 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16880 tree pack
, packs
= NULL_TREE
;
16881 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16882 int len
= TREE_VEC_LENGTH (packed_args
);
16884 /* Determine the parameter packs we will be deducing from the
16885 pattern, and record their current deductions. */
16886 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16887 pack
; pack
= TREE_CHAIN (pack
))
16889 tree parm_pack
= TREE_VALUE (pack
);
16892 /* Determine the index and level of this parameter pack. */
16893 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16895 /* Keep track of the parameter packs and their corresponding
16897 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16898 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16901 /* Loop through all of the arguments that have not yet been
16902 unified and unify each with the pattern. */
16903 for (i
= start
; i
< len
; i
++)
16906 bool any_explicit
= false;
16907 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16909 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16910 or the element of its argument pack at the current index if
16911 this argument was explicitly specified. */
16912 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16916 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16919 if (TREE_VALUE (pack
)
16920 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16921 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16923 any_explicit
= true;
16924 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16926 TMPL_ARG (targs
, level
, idx
) = arg
;
16929 /* If we had explicit template arguments, substitute them into the
16930 pattern before deduction. */
16933 /* Some arguments might still be unspecified or dependent. */
16935 ++processing_template_decl
;
16936 dependent
= any_dependent_template_arguments_p (targs
);
16938 --processing_template_decl
;
16939 parm
= tsubst (pattern
, targs
,
16940 explain_p
? tf_warning_or_error
: tf_none
,
16943 --processing_template_decl
;
16944 if (parm
== error_mark_node
)
16950 /* Unify the pattern with the current argument. */
16951 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16952 LOOKUP_IMPLICIT
, explain_p
))
16955 /* For each parameter pack, collect the deduced value. */
16956 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16959 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16961 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16962 TMPL_ARG (targs
, level
, idx
);
16966 /* Verify that the results of unification with the parameter packs
16967 produce results consistent with what we've seen before, and make
16968 the deduced argument packs available. */
16969 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16971 tree old_pack
= TREE_VALUE (pack
);
16972 tree new_args
= TREE_TYPE (pack
);
16973 int i
, len
= TREE_VEC_LENGTH (new_args
);
16975 bool nondeduced_p
= false;
16977 /* By default keep the original deduced argument pack.
16978 If necessary, more specific code is going to update the
16979 resulting deduced argument later down in this function. */
16980 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16981 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16983 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16984 actually deduce anything. */
16985 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16986 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16987 nondeduced_p
= true;
16991 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16993 /* If we had fewer function args than explicit template args,
16994 just use the explicits. */
16995 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16996 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16997 if (len
< explicit_len
)
16998 new_args
= explicit_args
;
17004 /* Build the deduced *_ARGUMENT_PACK. */
17005 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17007 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17008 TREE_TYPE (result
) =
17009 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17010 TREE_CONSTANT (result
) = 1;
17013 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17015 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17017 /* Note the deduced argument packs for this parameter
17019 TMPL_ARG (targs
, level
, idx
) = result
;
17021 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17022 && (ARGUMENT_PACK_ARGS (old_pack
)
17023 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17025 /* We only had the explicitly-provided arguments before, but
17026 now we have a complete set of arguments. */
17027 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17029 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17030 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17031 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17035 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17036 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17038 if (!comp_template_args_with_info (old_args
, new_args
,
17039 &bad_old_arg
, &bad_new_arg
))
17040 /* Inconsistent unification of this parameter pack. */
17041 return unify_parameter_pack_inconsistent (explain_p
,
17047 return unify_success (explain_p
);
17050 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17051 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17052 parameters and return value are as for unify. */
17055 unify_array_domain (tree tparms
, tree targs
,
17056 tree parm_dom
, tree arg_dom
,
17064 /* Our representation of array types uses "N - 1" as the
17065 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17066 not an integer constant. We cannot unify arbitrarily
17067 complex expressions, so we eliminate the MINUS_EXPRs
17069 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17070 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17073 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17074 parm_max
= TREE_OPERAND (parm_max
, 0);
17076 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17077 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17080 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17081 trying to unify the type of a variable with the type
17082 of a template parameter. For example:
17084 template <unsigned int N>
17085 void f (char (&) [N]);
17092 Here, the type of the ARG will be "int [g(i)]", and
17093 may be a SAVE_EXPR, etc. */
17094 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17095 return unify_vla_arg (explain_p
, arg_dom
);
17096 arg_max
= TREE_OPERAND (arg_max
, 0);
17099 /* If only one of the bounds used a MINUS_EXPR, compensate
17100 by adding one to the other bound. */
17101 if (parm_cst
&& !arg_cst
)
17102 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17106 else if (arg_cst
&& !parm_cst
)
17107 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17112 return unify (tparms
, targs
, parm_max
, arg_max
,
17113 UNIFY_ALLOW_INTEGER
, explain_p
);
17116 /* Deduce the value of template parameters. TPARMS is the (innermost)
17117 set of template parameters to a template. TARGS is the bindings
17118 for those template parameters, as determined thus far; TARGS may
17119 include template arguments for outer levels of template parameters
17120 as well. PARM is a parameter to a template function, or a
17121 subcomponent of that parameter; ARG is the corresponding argument.
17122 This function attempts to match PARM with ARG in a manner
17123 consistent with the existing assignments in TARGS. If more values
17124 are deduced, then TARGS is updated.
17126 Returns 0 if the type deduction succeeds, 1 otherwise. The
17127 parameter STRICT is a bitwise or of the following flags:
17130 Require an exact match between PARM and ARG.
17131 UNIFY_ALLOW_MORE_CV_QUAL:
17132 Allow the deduced ARG to be more cv-qualified (by qualification
17133 conversion) than ARG.
17134 UNIFY_ALLOW_LESS_CV_QUAL:
17135 Allow the deduced ARG to be less cv-qualified than ARG.
17136 UNIFY_ALLOW_DERIVED:
17137 Allow the deduced ARG to be a template base class of ARG,
17138 or a pointer to a template base class of the type pointed to by
17140 UNIFY_ALLOW_INTEGER:
17141 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17142 case for more information.
17143 UNIFY_ALLOW_OUTER_LEVEL:
17144 This is the outermost level of a deduction. Used to determine validity
17145 of qualification conversions. A valid qualification conversion must
17146 have const qualified pointers leading up to the inner type which
17147 requires additional CV quals, except at the outer level, where const
17148 is not required [conv.qual]. It would be normal to set this flag in
17149 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17150 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17151 This is the outermost level of a deduction, and PARM can be more CV
17152 qualified at this point.
17153 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17154 This is the outermost level of a deduction, and PARM can be less CV
17155 qualified at this point. */
17158 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17164 int strict_in
= strict
;
17166 /* I don't think this will do the right thing with respect to types.
17167 But the only case I've seen it in so far has been array bounds, where
17168 signedness is the only information lost, and I think that will be
17170 while (TREE_CODE (parm
) == NOP_EXPR
)
17171 parm
= TREE_OPERAND (parm
, 0);
17173 if (arg
== error_mark_node
)
17174 return unify_invalid (explain_p
);
17175 if (arg
== unknown_type_node
17176 || arg
== init_list_type_node
)
17177 /* We can't deduce anything from this, but we might get all the
17178 template args from other function args. */
17179 return unify_success (explain_p
);
17181 /* If PARM uses template parameters, then we can't bail out here,
17182 even if ARG == PARM, since we won't record unifications for the
17183 template parameters. We might need them if we're trying to
17184 figure out which of two things is more specialized. */
17185 if (arg
== parm
&& !uses_template_parms (parm
))
17186 return unify_success (explain_p
);
17188 /* Handle init lists early, so the rest of the function can assume
17189 we're dealing with a type. */
17190 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17194 tree orig_parm
= parm
;
17196 /* Replace T with std::initializer_list<T> for deduction. */
17197 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17198 && flag_deduce_init_list
)
17199 parm
= listify (parm
);
17201 if (!is_std_init_list (parm
)
17202 && TREE_CODE (parm
) != ARRAY_TYPE
)
17203 /* We can only deduce from an initializer list argument if the
17204 parameter is std::initializer_list or an array; otherwise this
17205 is a non-deduced context. */
17206 return unify_success (explain_p
);
17208 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17209 elttype
= TREE_TYPE (parm
);
17211 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17213 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17215 int elt_strict
= strict
;
17217 if (elt
== error_mark_node
)
17218 return unify_invalid (explain_p
);
17220 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17222 tree type
= TREE_TYPE (elt
);
17223 /* It should only be possible to get here for a call. */
17224 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17225 elt_strict
|= maybe_adjust_types_for_deduction
17226 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17230 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17234 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17236 /* Also deduce from the length of the initializer list. */
17237 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17238 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17239 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17240 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17244 /* If the std::initializer_list<T> deduction worked, replace the
17245 deduced A with std::initializer_list<A>. */
17246 if (orig_parm
!= parm
)
17248 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17249 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17250 targ
= listify (targ
);
17251 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17253 return unify_success (explain_p
);
17256 /* Immediately reject some pairs that won't unify because of
17257 cv-qualification mismatches. */
17258 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17260 /* It is the elements of the array which hold the cv quals of an array
17261 type, and the elements might be template type parms. We'll check
17262 when we recurse. */
17263 && TREE_CODE (arg
) != ARRAY_TYPE
17264 /* We check the cv-qualifiers when unifying with template type
17265 parameters below. We want to allow ARG `const T' to unify with
17266 PARM `T' for example, when computing which of two templates
17267 is more specialized, for example. */
17268 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17269 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17270 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17272 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17273 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17274 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17275 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17276 strict
&= ~UNIFY_ALLOW_DERIVED
;
17277 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17278 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17280 switch (TREE_CODE (parm
))
17282 case TYPENAME_TYPE
:
17284 case UNBOUND_CLASS_TEMPLATE
:
17285 /* In a type which contains a nested-name-specifier, template
17286 argument values cannot be deduced for template parameters used
17287 within the nested-name-specifier. */
17288 return unify_success (explain_p
);
17290 case TEMPLATE_TYPE_PARM
:
17291 case TEMPLATE_TEMPLATE_PARM
:
17292 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17293 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17294 if (tparm
== error_mark_node
)
17295 return unify_invalid (explain_p
);
17297 if (TEMPLATE_TYPE_LEVEL (parm
)
17298 != template_decl_level (tparm
))
17299 /* The PARM is not one we're trying to unify. Just check
17300 to see if it matches ARG. */
17302 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17303 && (is_auto (parm
) ? is_auto (arg
)
17304 : same_type_p (parm
, arg
)))
17305 return unify_success (explain_p
);
17307 return unify_type_mismatch (explain_p
, parm
, arg
);
17309 idx
= TEMPLATE_TYPE_IDX (parm
);
17310 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17311 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17312 if (tparm
== error_mark_node
)
17313 return unify_invalid (explain_p
);
17315 /* Check for mixed types and values. */
17316 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17317 && TREE_CODE (tparm
) != TYPE_DECL
)
17318 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17319 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17320 gcc_unreachable ();
17322 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17324 /* ARG must be constructed from a template class or a template
17325 template parameter. */
17326 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17327 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17328 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17331 tree parmvec
= TYPE_TI_ARGS (parm
);
17332 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17333 tree full_argvec
= add_to_template_args (targs
, argvec
);
17335 = DECL_INNERMOST_TEMPLATE_PARMS
17336 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17338 int parm_variadic_p
= 0;
17340 /* The resolution to DR150 makes clear that default
17341 arguments for an N-argument may not be used to bind T
17342 to a template template parameter with fewer than N
17343 parameters. It is not safe to permit the binding of
17344 default arguments as an extension, as that may change
17345 the meaning of a conforming program. Consider:
17347 struct Dense { static const unsigned int dim = 1; };
17349 template <template <typename> class View,
17351 void operator+(float, View<Block> const&);
17353 template <typename Block,
17354 unsigned int Dim = Block::dim>
17355 struct Lvalue_proxy { operator float() const; };
17359 Lvalue_proxy<Dense> p;
17364 Here, if Lvalue_proxy is permitted to bind to View, then
17365 the global operator+ will be used; if they are not, the
17366 Lvalue_proxy will be converted to float. */
17367 if (coerce_template_parms (parm_parms
,
17369 TYPE_TI_TEMPLATE (parm
),
17371 ? tf_warning_or_error
17373 /*require_all_args=*/true,
17374 /*use_default_args=*/false)
17375 == error_mark_node
)
17378 /* Deduce arguments T, i from TT<T> or TT<i>.
17379 We check each element of PARMVEC and ARGVEC individually
17380 rather than the whole TREE_VEC since they can have
17381 different number of elements. */
17383 parmvec
= expand_template_argument_pack (parmvec
);
17384 argvec
= expand_template_argument_pack (argvec
);
17386 len
= TREE_VEC_LENGTH (parmvec
);
17388 /* Check if the parameters end in a pack, making them
17391 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17392 parm_variadic_p
= 1;
17394 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17395 /* If the template argument list of P contains a pack
17396 expansion that is not the last template argument, the
17397 entire template argument list is a non-deduced
17399 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17400 return unify_success (explain_p
);
17402 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17403 return unify_too_few_arguments (explain_p
,
17404 TREE_VEC_LENGTH (argvec
), len
);
17406 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17408 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17409 TREE_VEC_ELT (parmvec
, i
),
17410 TREE_VEC_ELT (argvec
, i
),
17411 UNIFY_ALLOW_NONE
, explain_p
);
17414 if (parm_variadic_p
17415 && unify_pack_expansion (tparms
, targs
,
17418 /*subr=*/true, explain_p
))
17421 arg
= TYPE_TI_TEMPLATE (arg
);
17423 /* Fall through to deduce template name. */
17426 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17427 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17429 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17431 /* Simple cases: Value already set, does match or doesn't. */
17432 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17433 return unify_success (explain_p
);
17435 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17439 /* If PARM is `const T' and ARG is only `int', we don't have
17440 a match unless we are allowing additional qualification.
17441 If ARG is `const int' and PARM is just `T' that's OK;
17442 that binds `const int' to `T'. */
17443 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17445 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17447 /* Consider the case where ARG is `const volatile int' and
17448 PARM is `const T'. Then, T should be `volatile int'. */
17449 arg
= cp_build_qualified_type_real
17450 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17451 if (arg
== error_mark_node
)
17452 return unify_invalid (explain_p
);
17454 /* Simple cases: Value already set, does match or doesn't. */
17455 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17456 return unify_success (explain_p
);
17458 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17460 /* Make sure that ARG is not a variable-sized array. (Note
17461 that were talking about variable-sized arrays (like
17462 `int[n]'), rather than arrays of unknown size (like
17463 `int[]').) We'll get very confused by such a type since
17464 the bound of the array is not constant, and therefore
17465 not mangleable. Besides, such types are not allowed in
17466 ISO C++, so we can do as we please here. We do allow
17467 them for 'auto' deduction, since that isn't ABI-exposed. */
17468 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17469 return unify_vla_arg (explain_p
, arg
);
17471 /* Strip typedefs as in convert_template_argument. */
17472 arg
= canonicalize_type_argument (arg
, tf_none
);
17475 /* If ARG is a parameter pack or an expansion, we cannot unify
17476 against it unless PARM is also a parameter pack. */
17477 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17478 && !template_parameter_pack_p (parm
))
17479 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17481 /* If the argument deduction results is a METHOD_TYPE,
17482 then there is a problem.
17483 METHOD_TYPE doesn't map to any real C++ type the result of
17484 the deduction can not be of that type. */
17485 if (TREE_CODE (arg
) == METHOD_TYPE
)
17486 return unify_method_type_error (explain_p
, arg
);
17488 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17489 return unify_success (explain_p
);
17491 case TEMPLATE_PARM_INDEX
:
17492 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17493 if (tparm
== error_mark_node
)
17494 return unify_invalid (explain_p
);
17496 if (TEMPLATE_PARM_LEVEL (parm
)
17497 != template_decl_level (tparm
))
17499 /* The PARM is not one we're trying to unify. Just check
17500 to see if it matches ARG. */
17501 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17502 && cp_tree_equal (parm
, arg
));
17504 unify_expression_unequal (explain_p
, parm
, arg
);
17508 idx
= TEMPLATE_PARM_IDX (parm
);
17509 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17513 int x
= !cp_tree_equal (targ
, arg
);
17515 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17519 /* [temp.deduct.type] If, in the declaration of a function template
17520 with a non-type template-parameter, the non-type
17521 template-parameter is used in an expression in the function
17522 parameter-list and, if the corresponding template-argument is
17523 deduced, the template-argument type shall match the type of the
17524 template-parameter exactly, except that a template-argument
17525 deduced from an array bound may be of any integral type.
17526 The non-type parameter might use already deduced type parameters. */
17527 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17528 if (!TREE_TYPE (arg
))
17529 /* Template-parameter dependent expression. Just accept it for now.
17530 It will later be processed in convert_template_argument. */
17532 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17534 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17535 && CP_INTEGRAL_TYPE_P (tparm
))
17536 /* Convert the ARG to the type of PARM; the deduced non-type
17537 template argument must exactly match the types of the
17538 corresponding parameter. */
17539 arg
= fold (build_nop (tparm
, arg
));
17540 else if (uses_template_parms (tparm
))
17541 /* We haven't deduced the type of this parameter yet. Try again
17543 return unify_success (explain_p
);
17545 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17547 /* If ARG is a parameter pack or an expansion, we cannot unify
17548 against it unless PARM is also a parameter pack. */
17549 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17550 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17551 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17553 arg
= strip_typedefs_expr (arg
);
17554 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17555 return unify_success (explain_p
);
17559 /* A pointer-to-member constant can be unified only with
17560 another constant. */
17561 if (TREE_CODE (arg
) != PTRMEM_CST
)
17562 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17564 /* Just unify the class member. It would be useless (and possibly
17565 wrong, depending on the strict flags) to unify also
17566 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17567 arg refer to the same variable, even if through different
17568 classes. For instance:
17570 struct A { int x; };
17573 Unification of &A::x and &B::x must succeed. */
17574 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17575 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17580 if (!TYPE_PTR_P (arg
))
17581 return unify_type_mismatch (explain_p
, parm
, arg
);
17583 /* [temp.deduct.call]
17585 A can be another pointer or pointer to member type that can
17586 be converted to the deduced A via a qualification
17587 conversion (_conv.qual_).
17589 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17590 This will allow for additional cv-qualification of the
17591 pointed-to types if appropriate. */
17593 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17594 /* The derived-to-base conversion only persists through one
17595 level of pointers. */
17596 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17598 return unify (tparms
, targs
, TREE_TYPE (parm
),
17599 TREE_TYPE (arg
), strict
, explain_p
);
17602 case REFERENCE_TYPE
:
17603 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17604 return unify_type_mismatch (explain_p
, parm
, arg
);
17605 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17606 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17609 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17610 return unify_type_mismatch (explain_p
, parm
, arg
);
17611 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17612 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17613 return unify_type_mismatch (explain_p
, parm
, arg
);
17614 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17615 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17616 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17617 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17618 TYPE_DOMAIN (arg
), explain_p
);
17619 return unify_success (explain_p
);
17626 case ENUMERAL_TYPE
:
17629 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17630 return unify_type_mismatch (explain_p
, parm
, arg
);
17632 /* We have already checked cv-qualification at the top of the
17634 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17635 return unify_type_mismatch (explain_p
, parm
, arg
);
17637 /* As far as unification is concerned, this wins. Later checks
17638 will invalidate it if necessary. */
17639 return unify_success (explain_p
);
17641 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17642 /* Type INTEGER_CST can come from ordinary constant template args. */
17644 while (TREE_CODE (arg
) == NOP_EXPR
)
17645 arg
= TREE_OPERAND (arg
, 0);
17647 if (TREE_CODE (arg
) != INTEGER_CST
)
17648 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17649 return (tree_int_cst_equal (parm
, arg
)
17650 ? unify_success (explain_p
)
17651 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17655 int i
, len
, argslen
;
17656 int parm_variadic_p
= 0;
17658 if (TREE_CODE (arg
) != TREE_VEC
)
17659 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17661 len
= TREE_VEC_LENGTH (parm
);
17662 argslen
= TREE_VEC_LENGTH (arg
);
17664 /* Check for pack expansions in the parameters. */
17665 for (i
= 0; i
< len
; ++i
)
17667 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17670 /* We can unify against something with a trailing
17672 parm_variadic_p
= 1;
17674 /* [temp.deduct.type]/9: If the template argument list of
17675 P contains a pack expansion that is not the last
17676 template argument, the entire template argument list
17677 is a non-deduced context. */
17678 return unify_success (explain_p
);
17682 /* If we don't have enough arguments to satisfy the parameters
17683 (not counting the pack expression at the end), or we have
17684 too many arguments for a parameter list that doesn't end in
17685 a pack expression, we can't unify. */
17686 if (parm_variadic_p
17687 ? argslen
< len
- parm_variadic_p
17689 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17691 /* Unify all of the parameters that precede the (optional)
17692 pack expression. */
17693 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17695 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17696 TREE_VEC_ELT (parm
, i
),
17697 TREE_VEC_ELT (arg
, i
),
17698 UNIFY_ALLOW_NONE
, explain_p
);
17700 if (parm_variadic_p
)
17701 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17703 /*subr=*/true, explain_p
);
17704 return unify_success (explain_p
);
17709 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17710 return unify_type_mismatch (explain_p
, parm
, arg
);
17712 if (TYPE_PTRMEMFUNC_P (parm
))
17714 if (!TYPE_PTRMEMFUNC_P (arg
))
17715 return unify_type_mismatch (explain_p
, parm
, arg
);
17717 return unify (tparms
, targs
,
17718 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17719 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17720 strict
, explain_p
);
17723 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17725 tree t
= NULL_TREE
;
17727 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17729 /* First, we try to unify the PARM and ARG directly. */
17730 t
= try_class_unification (tparms
, targs
,
17731 parm
, arg
, explain_p
);
17735 /* Fallback to the special case allowed in
17736 [temp.deduct.call]:
17738 If P is a class, and P has the form
17739 template-id, then A can be a derived class of
17740 the deduced A. Likewise, if P is a pointer to
17741 a class of the form template-id, A can be a
17742 pointer to a derived class pointed to by the
17744 enum template_base_result r
;
17745 r
= get_template_base (tparms
, targs
, parm
, arg
,
17749 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17752 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17753 && (CLASSTYPE_TI_TEMPLATE (parm
)
17754 == CLASSTYPE_TI_TEMPLATE (arg
)))
17755 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17756 Then, we should unify `int' and `U'. */
17759 /* There's no chance of unification succeeding. */
17760 return unify_type_mismatch (explain_p
, parm
, arg
);
17762 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17763 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17765 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17766 return unify_type_mismatch (explain_p
, parm
, arg
);
17767 return unify_success (explain_p
);
17770 case FUNCTION_TYPE
:
17772 unsigned int nargs
;
17777 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17778 return unify_type_mismatch (explain_p
, parm
, arg
);
17780 /* CV qualifications for methods can never be deduced, they must
17781 match exactly. We need to check them explicitly here,
17782 because type_unification_real treats them as any other
17783 cv-qualified parameter. */
17784 if (TREE_CODE (parm
) == METHOD_TYPE
17785 && (!check_cv_quals_for_unify
17787 class_of_this_parm (arg
),
17788 class_of_this_parm (parm
))))
17789 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17791 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17792 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17794 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17795 args
= XALLOCAVEC (tree
, nargs
);
17796 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17797 a
!= NULL_TREE
&& a
!= void_list_node
;
17798 a
= TREE_CHAIN (a
), ++i
)
17799 args
[i
] = TREE_VALUE (a
);
17802 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17803 args
, nargs
, 1, DEDUCE_EXACT
,
17804 LOOKUP_NORMAL
, NULL
, explain_p
);
17808 /* Unify a pointer to member with a pointer to member function, which
17809 deduces the type of the member as a function type. */
17810 if (TYPE_PTRMEMFUNC_P (arg
))
17812 /* Check top-level cv qualifiers */
17813 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17814 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17816 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17817 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17818 UNIFY_ALLOW_NONE
, explain_p
);
17820 /* Determine the type of the function we are unifying against. */
17821 tree fntype
= static_fn_type (arg
);
17823 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17826 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17827 return unify_type_mismatch (explain_p
, parm
, arg
);
17828 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17829 TYPE_OFFSET_BASETYPE (arg
),
17830 UNIFY_ALLOW_NONE
, explain_p
);
17831 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17832 strict
, explain_p
);
17835 if (DECL_TEMPLATE_PARM_P (parm
))
17836 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17837 if (arg
!= integral_constant_value (parm
))
17838 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17839 return unify_success (explain_p
);
17842 case TEMPLATE_DECL
:
17843 /* Matched cases are handled by the ARG == PARM test above. */
17844 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17847 /* A non-type template parameter that is a variable should be a
17848 an integral constant, in which case, it whould have been
17849 folded into its (constant) value. So we should not be getting
17850 a variable here. */
17851 gcc_unreachable ();
17853 case TYPE_ARGUMENT_PACK
:
17854 case NONTYPE_ARGUMENT_PACK
:
17855 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17856 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17859 case DECLTYPE_TYPE
:
17860 case UNDERLYING_TYPE
:
17861 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17862 or UNDERLYING_TYPE nodes. */
17863 return unify_success (explain_p
);
17866 /* Unification fails if we hit an error node. */
17867 return unify_invalid (explain_p
);
17870 /* An unresolved overload is a nondeduced context. */
17871 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17872 return unify_success (explain_p
);
17873 gcc_assert (EXPR_P (parm
));
17875 /* We must be looking at an expression. This can happen with
17879 void foo(S<I>, S<I + 2>);
17881 This is a "nondeduced context":
17885 The nondeduced contexts are:
17887 --A type that is a template-id in which one or more of
17888 the template-arguments is an expression that references
17889 a template-parameter.
17891 In these cases, we assume deduction succeeded, but don't
17892 actually infer any unifications. */
17894 if (!uses_template_parms (parm
)
17895 && !template_args_equal (parm
, arg
))
17896 return unify_expression_unequal (explain_p
, parm
, arg
);
17898 return unify_success (explain_p
);
17901 #undef RECUR_AND_CHECK_FAILURE
17903 /* Note that DECL can be defined in this translation unit, if
17907 mark_definable (tree decl
)
17910 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17911 FOR_EACH_CLONE (clone
, decl
)
17912 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17915 /* Called if RESULT is explicitly instantiated, or is a member of an
17916 explicitly instantiated class. */
17919 mark_decl_instantiated (tree result
, int extern_p
)
17921 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17923 /* If this entity has already been written out, it's too late to
17924 make any modifications. */
17925 if (TREE_ASM_WRITTEN (result
))
17928 /* For anonymous namespace we don't need to do anything. */
17929 if (decl_anon_ns_mem_p (result
))
17931 gcc_assert (!TREE_PUBLIC (result
));
17935 if (TREE_CODE (result
) != FUNCTION_DECL
)
17936 /* The TREE_PUBLIC flag for function declarations will have been
17937 set correctly by tsubst. */
17938 TREE_PUBLIC (result
) = 1;
17940 /* This might have been set by an earlier implicit instantiation. */
17941 DECL_COMDAT (result
) = 0;
17944 DECL_NOT_REALLY_EXTERN (result
) = 0;
17947 mark_definable (result
);
17948 /* Always make artificials weak. */
17949 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17950 comdat_linkage (result
);
17951 /* For WIN32 we also want to put explicit instantiations in
17952 linkonce sections. */
17953 else if (TREE_PUBLIC (result
))
17954 maybe_make_one_only (result
);
17957 /* If EXTERN_P, then this function will not be emitted -- unless
17958 followed by an explicit instantiation, at which point its linkage
17959 will be adjusted. If !EXTERN_P, then this function will be
17960 emitted here. In neither circumstance do we want
17961 import_export_decl to adjust the linkage. */
17962 DECL_INTERFACE_KNOWN (result
) = 1;
17965 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17966 important template arguments. If any are missing, we check whether
17967 they're important by using error_mark_node for substituting into any
17968 args that were used for partial ordering (the ones between ARGS and END)
17969 and seeing if it bubbles up. */
17972 check_undeduced_parms (tree targs
, tree args
, tree end
)
17974 bool found
= false;
17976 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17977 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17980 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17984 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17985 if (substed
== error_mark_node
)
17991 /* Given two function templates PAT1 and PAT2, return:
17993 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17994 -1 if PAT2 is more specialized than PAT1.
17995 0 if neither is more specialized.
17997 LEN indicates the number of parameters we should consider
17998 (defaulted parameters should not be considered).
18000 The 1998 std underspecified function template partial ordering, and
18001 DR214 addresses the issue. We take pairs of arguments, one from
18002 each of the templates, and deduce them against each other. One of
18003 the templates will be more specialized if all the *other*
18004 template's arguments deduce against its arguments and at least one
18005 of its arguments *does* *not* deduce against the other template's
18006 corresponding argument. Deduction is done as for class templates.
18007 The arguments used in deduction have reference and top level cv
18008 qualifiers removed. Iff both arguments were originally reference
18009 types *and* deduction succeeds in both directions, an lvalue reference
18010 wins against an rvalue reference and otherwise the template
18011 with the more cv-qualified argument wins for that pairing (if
18012 neither is more cv-qualified, they both are equal). Unlike regular
18013 deduction, after all the arguments have been deduced in this way,
18014 we do *not* verify the deduced template argument values can be
18015 substituted into non-deduced contexts.
18017 The logic can be a bit confusing here, because we look at deduce1 and
18018 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18019 can find template arguments for pat1 to make arg1 look like arg2, that
18020 means that arg2 is at least as specialized as arg1. */
18023 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18025 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18026 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18027 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18028 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18029 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18030 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18031 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18032 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18033 tree origs1
, origs2
;
18034 bool lose1
= false;
18035 bool lose2
= false;
18037 /* Remove the this parameter from non-static member functions. If
18038 one is a non-static member function and the other is not a static
18039 member function, remove the first parameter from that function
18040 also. This situation occurs for operator functions where we
18041 locate both a member function (with this pointer) and non-member
18042 operator (with explicit first operand). */
18043 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18045 len
--; /* LEN is the number of significant arguments for DECL1 */
18046 args1
= TREE_CHAIN (args1
);
18047 if (!DECL_STATIC_FUNCTION_P (decl2
))
18048 args2
= TREE_CHAIN (args2
);
18050 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18052 args2
= TREE_CHAIN (args2
);
18053 if (!DECL_STATIC_FUNCTION_P (decl1
))
18056 args1
= TREE_CHAIN (args1
);
18060 /* If only one is a conversion operator, they are unordered. */
18061 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18064 /* Consider the return type for a conversion function */
18065 if (DECL_CONV_FN_P (decl1
))
18067 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18068 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18072 processing_template_decl
++;
18078 /* Stop when an ellipsis is seen. */
18079 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18081 tree arg1
= TREE_VALUE (args1
);
18082 tree arg2
= TREE_VALUE (args2
);
18083 int deduce1
, deduce2
;
18089 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18090 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18092 /* When both arguments are pack expansions, we need only
18093 unify the patterns themselves. */
18094 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18095 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18097 /* This is the last comparison we need to do. */
18101 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18103 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18104 arg1
= TREE_TYPE (arg1
);
18105 quals1
= cp_type_quals (arg1
);
18108 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18110 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18111 arg2
= TREE_TYPE (arg2
);
18112 quals2
= cp_type_quals (arg2
);
18115 arg1
= TYPE_MAIN_VARIANT (arg1
);
18116 arg2
= TYPE_MAIN_VARIANT (arg2
);
18118 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18120 int i
, len2
= list_length (args2
);
18121 tree parmvec
= make_tree_vec (1);
18122 tree argvec
= make_tree_vec (len2
);
18125 /* Setup the parameter vector, which contains only ARG1. */
18126 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18128 /* Setup the argument vector, which contains the remaining
18130 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18131 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18133 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18134 argvec
, DEDUCE_EXACT
,
18135 /*subr=*/true, /*explain_p=*/false)
18138 /* We cannot deduce in the other direction, because ARG1 is
18139 a pack expansion but ARG2 is not. */
18142 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18144 int i
, len1
= list_length (args1
);
18145 tree parmvec
= make_tree_vec (1);
18146 tree argvec
= make_tree_vec (len1
);
18149 /* Setup the parameter vector, which contains only ARG1. */
18150 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18152 /* Setup the argument vector, which contains the remaining
18154 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18155 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18157 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18158 argvec
, DEDUCE_EXACT
,
18159 /*subr=*/true, /*explain_p=*/false)
18162 /* We cannot deduce in the other direction, because ARG2 is
18163 a pack expansion but ARG1 is not.*/
18169 /* The normal case, where neither argument is a pack
18171 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18172 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18174 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18175 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18179 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18180 arg2, then arg2 is not as specialized as arg1. */
18186 /* "If, for a given type, deduction succeeds in both directions
18187 (i.e., the types are identical after the transformations above)
18188 and both P and A were reference types (before being replaced with
18189 the type referred to above):
18190 - if the type from the argument template was an lvalue reference and
18191 the type from the parameter template was not, the argument type is
18192 considered to be more specialized than the other; otherwise,
18193 - if the type from the argument template is more cv-qualified
18194 than the type from the parameter template (as described above),
18195 the argument type is considered to be more specialized than the other;
18197 - neither type is more specialized than the other." */
18199 if (deduce1
&& deduce2
)
18201 if (ref1
&& ref2
&& ref1
!= ref2
)
18208 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18210 if ((quals1
& quals2
) == quals2
)
18212 if ((quals1
& quals2
) == quals1
)
18217 if (lose1
&& lose2
)
18218 /* We've failed to deduce something in either direction.
18219 These must be unordered. */
18222 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18223 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18224 /* We have already processed all of the arguments in our
18225 handing of the pack expansion type. */
18228 args1
= TREE_CHAIN (args1
);
18229 args2
= TREE_CHAIN (args2
);
18232 /* "In most cases, all template parameters must have values in order for
18233 deduction to succeed, but for partial ordering purposes a template
18234 parameter may remain without a value provided it is not used in the
18235 types being used for partial ordering."
18237 Thus, if we are missing any of the targs1 we need to substitute into
18238 origs1, then pat2 is not as specialized as pat1. This can happen when
18239 there is a nondeduced context. */
18240 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18242 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18245 processing_template_decl
--;
18247 /* All things being equal, if the next argument is a pack expansion
18248 for one function but not for the other, prefer the
18249 non-variadic function. FIXME this is bogus; see c++/41958. */
18251 && args1
&& TREE_VALUE (args1
)
18252 && args2
&& TREE_VALUE (args2
))
18254 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18255 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18258 if (lose1
== lose2
)
18266 /* Determine which of two partial specializations of TMPL is more
18269 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18270 to the first partial specialization. The TREE_VALUE is the
18271 innermost set of template parameters for the partial
18272 specialization. PAT2 is similar, but for the second template.
18274 Return 1 if the first partial specialization is more specialized;
18275 -1 if the second is more specialized; 0 if neither is more
18278 See [temp.class.order] for information about determining which of
18279 two templates is more specialized. */
18282 more_specialized_class (tree tmpl
, tree pat1
, tree pat2
)
18287 bool any_deductions
= false;
18289 tmpl1
= TREE_TYPE (pat1
);
18290 tmpl2
= TREE_TYPE (pat2
);
18292 /* Just like what happens for functions, if we are ordering between
18293 different class template specializations, we may encounter dependent
18294 types in the arguments, and we need our dependency check functions
18295 to behave correctly. */
18296 ++processing_template_decl
;
18297 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat1
),
18298 CLASSTYPE_TI_ARGS (tmpl1
),
18299 CLASSTYPE_TI_ARGS (tmpl2
));
18303 any_deductions
= true;
18306 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat2
),
18307 CLASSTYPE_TI_ARGS (tmpl2
),
18308 CLASSTYPE_TI_ARGS (tmpl1
));
18312 any_deductions
= true;
18314 --processing_template_decl
;
18316 /* In the case of a tie where at least one of the class templates
18317 has a parameter pack at the end, the template with the most
18318 non-packed parameters wins. */
18321 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18322 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18324 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18325 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18326 int len1
= TREE_VEC_LENGTH (args1
);
18327 int len2
= TREE_VEC_LENGTH (args2
);
18329 /* We don't count the pack expansion at the end. */
18330 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18332 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18337 else if (len1
< len2
)
18344 /* Return the template arguments that will produce the function signature
18345 DECL from the function template FN, with the explicit template
18346 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18347 also match. Return NULL_TREE if no satisfactory arguments could be
18351 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18353 int ntparms
= DECL_NTPARMS (fn
);
18354 tree targs
= make_tree_vec (ntparms
);
18355 tree decl_type
= TREE_TYPE (decl
);
18356 tree decl_arg_types
;
18358 unsigned int nargs
, ix
;
18361 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18363 /* Never do unification on the 'this' parameter. */
18364 decl_arg_types
= skip_artificial_parms_for (decl
,
18365 TYPE_ARG_TYPES (decl_type
));
18367 nargs
= list_length (decl_arg_types
);
18368 args
= XALLOCAVEC (tree
, nargs
);
18369 for (arg
= decl_arg_types
, ix
= 0;
18370 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18371 arg
= TREE_CHAIN (arg
), ++ix
)
18372 args
[ix
] = TREE_VALUE (arg
);
18374 if (fn_type_unification (fn
, explicit_args
, targs
,
18376 (check_rettype
|| DECL_CONV_FN_P (fn
)
18377 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18378 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18380 == error_mark_node
)
18386 /* Return the innermost template arguments that, when applied to a partial
18387 specialization of TMPL whose innermost template parameters are
18388 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18391 For example, suppose we have:
18393 template <class T, class U> struct S {};
18394 template <class T> struct S<T*, int> {};
18396 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18397 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18398 int}. The resulting vector will be {double}, indicating that `T'
18399 is bound to `double'. */
18402 get_class_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
18404 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18406 tree innermost_deduced_args
;
18408 innermost_deduced_args
= make_tree_vec (ntparms
);
18409 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18411 deduced_args
= copy_node (args
);
18412 SET_TMPL_ARGS_LEVEL (deduced_args
,
18413 TMPL_ARGS_DEPTH (deduced_args
),
18414 innermost_deduced_args
);
18417 deduced_args
= innermost_deduced_args
;
18419 if (unify (tparms
, deduced_args
,
18420 INNERMOST_TEMPLATE_ARGS (spec_args
),
18421 INNERMOST_TEMPLATE_ARGS (args
),
18422 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18425 for (i
= 0; i
< ntparms
; ++i
)
18426 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18429 /* Verify that nondeduced template arguments agree with the type
18430 obtained from argument deduction.
18434 struct A { typedef int X; };
18435 template <class T, class U> struct C {};
18436 template <class T> struct C<T, typename T::X> {};
18438 Then with the instantiation `C<A, int>', we can deduce that
18439 `T' is `A' but unify () does not check whether `typename T::X'
18441 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18442 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18444 tf_none
, false, false);
18445 if (spec_args
== error_mark_node
18446 /* We only need to check the innermost arguments; the other
18447 arguments will always agree. */
18448 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18449 INNERMOST_TEMPLATE_ARGS (args
)))
18452 /* Now that we have bindings for all of the template arguments,
18453 ensure that the arguments deduced for the template template
18454 parameters have compatible template parameter lists. See the use
18455 of template_template_parm_bindings_ok_p in fn_type_unification
18456 for more information. */
18457 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18460 return deduced_args
;
18463 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18464 Return the TREE_LIST node with the most specialized template, if
18465 any. If there is no most specialized template, the error_mark_node
18468 Note that this function does not look at, or modify, the
18469 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18470 returned is one of the elements of INSTANTIATIONS, callers may
18471 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18472 and retrieve it from the value returned. */
18475 most_specialized_instantiation (tree templates
)
18479 ++processing_template_decl
;
18482 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18486 if (get_bindings (TREE_VALUE (champ
),
18487 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18488 NULL_TREE
, /*check_ret=*/true))
18491 if (get_bindings (TREE_VALUE (fn
),
18492 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18493 NULL_TREE
, /*check_ret=*/true))
18500 /* Equally specialized, move to next function. If there
18501 is no next function, nothing's most specialized. */
18502 fn
= TREE_CHAIN (fn
);
18510 /* Now verify that champ is better than everything earlier in the
18511 instantiation list. */
18512 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18513 if (get_bindings (TREE_VALUE (champ
),
18514 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18515 NULL_TREE
, /*check_ret=*/true)
18516 || !get_bindings (TREE_VALUE (fn
),
18517 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18518 NULL_TREE
, /*check_ret=*/true))
18524 processing_template_decl
--;
18527 return error_mark_node
;
18532 /* If DECL is a specialization of some template, return the most
18533 general such template. Otherwise, returns NULL_TREE.
18535 For example, given:
18537 template <class T> struct S { template <class U> void f(U); };
18539 if TMPL is `template <class U> void S<int>::f(U)' this will return
18540 the full template. This function will not trace past partial
18541 specializations, however. For example, given in addition:
18543 template <class T> struct S<T*> { template <class U> void f(U); };
18545 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18546 `template <class T> template <class U> S<T*>::f(U)'. */
18549 most_general_template (tree decl
)
18551 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18552 an immediate specialization. */
18553 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18555 if (DECL_TEMPLATE_INFO (decl
)) {
18556 decl
= DECL_TI_TEMPLATE (decl
);
18558 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18559 template friend. */
18560 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18566 /* Look for more and more general templates. */
18567 while (DECL_TEMPLATE_INFO (decl
))
18569 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18570 (See cp-tree.h for details.) */
18571 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18574 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18578 /* Stop if we run into an explicitly specialized class template. */
18579 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18580 && DECL_CONTEXT (decl
)
18581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18584 decl
= DECL_TI_TEMPLATE (decl
);
18590 /* Return the most specialized of the class template partial
18591 specializations which can produce TYPE, a specialization of some class
18592 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18593 a _TYPE node corresponding to the partial specialization, while the
18594 TREE_PURPOSE is the set of template arguments that must be
18595 substituted into the TREE_TYPE in order to generate TYPE.
18597 If the choice of partial specialization is ambiguous, a diagnostic
18598 is issued, and the error_mark_node is returned. If there are no
18599 partial specializations matching TYPE, then NULL_TREE is
18600 returned, indicating that the primary template should be used. */
18603 most_specialized_class (tree type
, tsubst_flags_t complain
)
18605 tree list
= NULL_TREE
;
18610 tree outer_args
= NULL_TREE
;
18612 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
18613 tree main_tmpl
= most_general_template (tmpl
);
18614 tree args
= CLASSTYPE_TI_ARGS (type
);
18616 /* For determining which partial specialization to use, only the
18617 innermost args are interesting. */
18618 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18620 outer_args
= strip_innermost_template_args (args
, 1);
18621 args
= INNERMOST_TEMPLATE_ARGS (args
);
18624 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
18626 tree partial_spec_args
;
18628 tree spec_tmpl
= TREE_VALUE (t
);
18629 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18631 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18633 ++processing_template_decl
;
18637 /* Discard the outer levels of args, and then substitute in the
18638 template args from the enclosing class. */
18639 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18640 partial_spec_args
= tsubst_template_args
18641 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18643 /* And the same for the partial specialization TEMPLATE_DECL. */
18644 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18647 partial_spec_args
=
18648 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18651 /*require_all_args=*/true,
18652 /*use_default_args=*/true);
18654 --processing_template_decl
;
18656 if (partial_spec_args
== error_mark_node
)
18657 return error_mark_node
;
18658 if (spec_tmpl
== error_mark_node
)
18659 return error_mark_node
;
18661 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18662 spec_args
= get_class_bindings (tmpl
, parms
,
18668 spec_args
= add_to_template_args (outer_args
, spec_args
);
18669 list
= tree_cons (spec_args
, orig_parms
, list
);
18670 TREE_TYPE (list
) = TREE_TYPE (t
);
18677 ambiguous_p
= false;
18680 t
= TREE_CHAIN (t
);
18681 for (; t
; t
= TREE_CHAIN (t
))
18683 fate
= more_specialized_class (tmpl
, champ
, t
);
18690 t
= TREE_CHAIN (t
);
18693 ambiguous_p
= true;
18702 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18704 fate
= more_specialized_class (tmpl
, champ
, t
);
18707 ambiguous_p
= true;
18715 char *spaces
= NULL
;
18716 if (!(complain
& tf_error
))
18717 return error_mark_node
;
18718 error ("ambiguous class template instantiation for %q#T", type
);
18719 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18720 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18722 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18723 spaces
= spaces
? spaces
: get_spaces (str
);
18726 return error_mark_node
;
18732 /* Explicitly instantiate DECL. */
18735 do_decl_instantiation (tree decl
, tree storage
)
18737 tree result
= NULL_TREE
;
18740 if (!decl
|| decl
== error_mark_node
)
18741 /* An error occurred, for which grokdeclarator has already issued
18742 an appropriate message. */
18744 else if (! DECL_LANG_SPECIFIC (decl
))
18746 error ("explicit instantiation of non-template %q#D", decl
);
18749 else if (VAR_P (decl
))
18751 /* There is an asymmetry here in the way VAR_DECLs and
18752 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18753 the latter, the DECL we get back will be marked as a
18754 template instantiation, and the appropriate
18755 DECL_TEMPLATE_INFO will be set up. This does not happen for
18756 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18757 should handle VAR_DECLs as it currently handles
18759 if (!DECL_CLASS_SCOPE_P (decl
))
18761 error ("%qD is not a static data member of a class template", decl
);
18764 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18765 if (!result
|| !VAR_P (result
))
18767 error ("no matching template for %qD found", decl
);
18770 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18772 error ("type %qT for explicit instantiation %qD does not match "
18773 "declared type %qT", TREE_TYPE (result
), decl
,
18778 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18780 error ("explicit instantiation of %q#D", decl
);
18786 /* Check for various error cases. Note that if the explicit
18787 instantiation is valid the RESULT will currently be marked as an
18788 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18789 until we get here. */
18791 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18793 /* DR 259 [temp.spec].
18795 Both an explicit instantiation and a declaration of an explicit
18796 specialization shall not appear in a program unless the explicit
18797 instantiation follows a declaration of the explicit specialization.
18799 For a given set of template parameters, if an explicit
18800 instantiation of a template appears after a declaration of an
18801 explicit specialization for that template, the explicit
18802 instantiation has no effect. */
18805 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18809 No program shall explicitly instantiate any template more
18812 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18813 the first instantiation was `extern' and the second is not,
18814 and EXTERN_P for the opposite case. */
18815 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18816 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18817 /* If an "extern" explicit instantiation follows an ordinary
18818 explicit instantiation, the template is instantiated. */
18822 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18824 error ("no matching template for %qD found", result
);
18827 else if (!DECL_TEMPLATE_INFO (result
))
18829 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18833 if (storage
== NULL_TREE
)
18835 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18837 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
18838 pedwarn (input_location
, OPT_Wpedantic
,
18839 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18844 error ("storage class %qD applied to template instantiation", storage
);
18846 check_explicit_instantiation_namespace (result
);
18847 mark_decl_instantiated (result
, extern_p
);
18849 instantiate_decl (result
, /*defer_ok=*/1,
18850 /*expl_inst_class_mem_p=*/false);
18854 mark_class_instantiated (tree t
, int extern_p
)
18856 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18857 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18858 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18859 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18862 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18863 rest_of_type_compilation (t
, 1);
18867 /* Called from do_type_instantiation through binding_table_foreach to
18868 do recursive instantiation for the type bound in ENTRY. */
18870 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18872 tree storage
= *(tree
*) data
;
18874 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18875 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18876 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18879 /* Called from do_type_instantiation to instantiate a member
18880 (a member function or a static member variable) of an
18881 explicitly instantiated class template. */
18883 instantiate_class_member (tree decl
, int extern_p
)
18885 mark_decl_instantiated (decl
, extern_p
);
18887 instantiate_decl (decl
, /*defer_ok=*/1,
18888 /*expl_inst_class_mem_p=*/true);
18891 /* Perform an explicit instantiation of template class T. STORAGE, if
18892 non-null, is the RID for extern, inline or static. COMPLAIN is
18893 nonzero if this is called from the parser, zero if called recursively,
18894 since the standard is unclear (as detailed below). */
18897 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18902 int previous_instantiation_extern_p
= 0;
18904 if (TREE_CODE (t
) == TYPE_DECL
)
18907 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18910 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18912 error ("explicit instantiation of non-class template %qD", tmpl
);
18914 error ("explicit instantiation of non-template type %qT", t
);
18920 if (!COMPLETE_TYPE_P (t
))
18922 if (complain
& tf_error
)
18923 error ("explicit instantiation of %q#T before definition of template",
18928 if (storage
!= NULL_TREE
)
18930 if (!in_system_header_at (input_location
))
18932 if (storage
== ridpointers
[(int) RID_EXTERN
])
18934 if (cxx_dialect
== cxx98
)
18935 pedwarn (input_location
, OPT_Wpedantic
,
18936 "ISO C++ 1998 forbids the use of %<extern%> on "
18937 "explicit instantiations");
18940 pedwarn (input_location
, OPT_Wpedantic
,
18941 "ISO C++ forbids the use of %qE"
18942 " on explicit instantiations", storage
);
18945 if (storage
== ridpointers
[(int) RID_INLINE
])
18947 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18949 else if (storage
== ridpointers
[(int) RID_STATIC
])
18953 error ("storage class %qD applied to template instantiation",
18959 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18961 /* DR 259 [temp.spec].
18963 Both an explicit instantiation and a declaration of an explicit
18964 specialization shall not appear in a program unless the explicit
18965 instantiation follows a declaration of the explicit specialization.
18967 For a given set of template parameters, if an explicit
18968 instantiation of a template appears after a declaration of an
18969 explicit specialization for that template, the explicit
18970 instantiation has no effect. */
18973 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18977 No program shall explicitly instantiate any template more
18980 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18981 instantiation was `extern'. If EXTERN_P then the second is.
18982 These cases are OK. */
18983 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18985 if (!previous_instantiation_extern_p
&& !extern_p
18986 && (complain
& tf_error
))
18987 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18989 /* If we've already instantiated the template, just return now. */
18990 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18994 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18995 mark_class_instantiated (t
, extern_p
);
19003 /* In contrast to implicit instantiation, where only the
19004 declarations, and not the definitions, of members are
19005 instantiated, we have here:
19009 The explicit instantiation of a class template specialization
19010 implies the instantiation of all of its members not
19011 previously explicitly specialized in the translation unit
19012 containing the explicit instantiation.
19014 Of course, we can't instantiate member template classes, since
19015 we don't have any arguments for them. Note that the standard
19016 is unclear on whether the instantiation of the members are
19017 *explicit* instantiations or not. However, the most natural
19018 interpretation is that it should be an explicit instantiation. */
19021 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19022 if (TREE_CODE (tmp
) == FUNCTION_DECL
19023 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19024 instantiate_class_member (tmp
, extern_p
);
19026 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19027 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19028 instantiate_class_member (tmp
, extern_p
);
19030 if (CLASSTYPE_NESTED_UTDS (t
))
19031 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19032 bt_instantiate_type_proc
, &storage
);
19036 /* Given a function DECL, which is a specialization of TMPL, modify
19037 DECL to be a re-instantiation of TMPL with the same template
19038 arguments. TMPL should be the template into which tsubst'ing
19039 should occur for DECL, not the most general template.
19041 One reason for doing this is a scenario like this:
19044 void f(const T&, int i);
19046 void g() { f(3, 7); }
19049 void f(const T& t, const int i) { }
19051 Note that when the template is first instantiated, with
19052 instantiate_template, the resulting DECL will have no name for the
19053 first parameter, and the wrong type for the second. So, when we go
19054 to instantiate the DECL, we regenerate it. */
19057 regenerate_decl_from_template (tree decl
, tree tmpl
)
19059 /* The arguments used to instantiate DECL, from the most general
19064 args
= DECL_TI_ARGS (decl
);
19065 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19067 /* Make sure that we can see identifiers, and compute access
19069 push_access_scope (decl
);
19071 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19079 args_depth
= TMPL_ARGS_DEPTH (args
);
19080 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19081 if (args_depth
> parms_depth
)
19082 args
= get_innermost_template_args (args
, parms_depth
);
19084 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19085 args
, tf_error
, NULL_TREE
,
19086 /*defer_ok*/false);
19087 if (specs
&& specs
!= error_mark_node
)
19088 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19091 /* Merge parameter declarations. */
19092 decl_parm
= skip_artificial_parms_for (decl
,
19093 DECL_ARGUMENTS (decl
));
19095 = skip_artificial_parms_for (code_pattern
,
19096 DECL_ARGUMENTS (code_pattern
));
19097 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19102 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19103 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19104 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19106 parm_type
= type_decays_to (parm_type
);
19107 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19108 TREE_TYPE (decl_parm
) = parm_type
;
19109 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19110 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19112 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19113 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19115 decl_parm
= DECL_CHAIN (decl_parm
);
19116 pattern_parm
= DECL_CHAIN (pattern_parm
);
19118 /* Merge any parameters that match with the function parameter
19120 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19123 tree expanded_types
;
19124 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19125 the parameters in this function parameter pack. */
19126 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19127 args
, tf_error
, NULL_TREE
);
19128 len
= TREE_VEC_LENGTH (expanded_types
);
19129 for (i
= 0; i
< len
; i
++)
19134 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19135 /* Rename the parameter to include the index. */
19136 DECL_NAME (decl_parm
) =
19137 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19138 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19139 parm_type
= type_decays_to (parm_type
);
19140 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19141 TREE_TYPE (decl_parm
) = parm_type
;
19142 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19143 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19145 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19146 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19148 decl_parm
= DECL_CHAIN (decl_parm
);
19151 /* Merge additional specifiers from the CODE_PATTERN. */
19152 if (DECL_DECLARED_INLINE_P (code_pattern
)
19153 && !DECL_DECLARED_INLINE_P (decl
))
19154 DECL_DECLARED_INLINE_P (decl
) = 1;
19156 else if (VAR_P (decl
))
19158 DECL_INITIAL (decl
) =
19159 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19160 tf_error
, DECL_TI_TEMPLATE (decl
),
19161 /*integral_constant_expression_p=*/false);
19162 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19163 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19164 tf_error
, DECL_TI_TEMPLATE (decl
));
19167 gcc_unreachable ();
19169 pop_access_scope (decl
);
19172 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19173 substituted to get DECL. */
19176 template_for_substitution (tree decl
)
19178 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19180 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19181 for the instantiation. This is not always the most general
19182 template. Consider, for example:
19185 struct S { template <class U> void f();
19186 template <> void f<int>(); };
19188 and an instantiation of S<double>::f<int>. We want TD to be the
19189 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19190 while (/* An instantiation cannot have a definition, so we need a
19191 more general template. */
19192 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19193 /* We must also deal with friend templates. Given:
19195 template <class T> struct S {
19196 template <class U> friend void f() {};
19199 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19200 so far as the language is concerned, but that's still
19201 where we get the pattern for the instantiation from. On
19202 other hand, if the definition comes outside the class, say:
19204 template <class T> struct S {
19205 template <class U> friend void f();
19207 template <class U> friend void f() {}
19209 we don't need to look any further. That's what the check for
19210 DECL_INITIAL is for. */
19211 || (TREE_CODE (decl
) == FUNCTION_DECL
19212 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19213 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19215 /* The present template, TD, should not be a definition. If it
19216 were a definition, we should be using it! Note that we
19217 cannot restructure the loop to just keep going until we find
19218 a template with a definition, since that might go too far if
19219 a specialization was declared, but not defined. */
19220 gcc_assert (!VAR_P (decl
)
19221 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19223 /* Fetch the more general template. */
19224 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19230 /* Returns true if we need to instantiate this template instance even if we
19231 know we aren't going to emit it.. */
19234 always_instantiate_p (tree decl
)
19236 /* We always instantiate inline functions so that we can inline them. An
19237 explicit instantiation declaration prohibits implicit instantiation of
19238 non-inline functions. With high levels of optimization, we would
19239 normally inline non-inline functions -- but we're not allowed to do
19240 that for "extern template" functions. Therefore, we check
19241 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19242 return ((TREE_CODE (decl
) == FUNCTION_DECL
19243 && (DECL_DECLARED_INLINE_P (decl
)
19244 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19245 /* And we need to instantiate static data members so that
19246 their initializers are available in integral constant
19249 && decl_maybe_constant_var_p (decl
)));
19252 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19253 instantiate it now, modifying TREE_TYPE (fn). */
19256 maybe_instantiate_noexcept (tree fn
)
19258 tree fntype
, spec
, noex
, clone
;
19260 if (DECL_CLONED_FUNCTION_P (fn
))
19261 fn
= DECL_CLONED_FUNCTION (fn
);
19262 fntype
= TREE_TYPE (fn
);
19263 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19265 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19268 noex
= TREE_PURPOSE (spec
);
19270 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19272 if (push_tinst_level (fn
))
19274 push_access_scope (fn
);
19275 push_deferring_access_checks (dk_no_deferred
);
19276 input_location
= DECL_SOURCE_LOCATION (fn
);
19277 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19278 DEFERRED_NOEXCEPT_ARGS (noex
),
19279 tf_warning_or_error
, fn
,
19280 /*function_p=*/false,
19281 /*integral_constant_expression_p=*/true);
19282 pop_deferring_access_checks ();
19283 pop_access_scope (fn
);
19284 pop_tinst_level ();
19285 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19286 if (spec
== error_mark_node
)
19287 spec
= noexcept_false_spec
;
19290 spec
= noexcept_false_spec
;
19294 /* This is an implicitly declared function, so NOEX is a list of
19295 other functions to evaluate and merge. */
19297 spec
= noexcept_true_spec
;
19298 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19300 tree fn
= OVL_CURRENT (elt
);
19302 maybe_instantiate_noexcept (fn
);
19303 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19304 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19308 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19310 FOR_EACH_CLONE (clone
, fn
)
19312 if (TREE_TYPE (clone
) == fntype
)
19313 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19315 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19319 /* Produce the definition of D, a _DECL generated from a template. If
19320 DEFER_OK is nonzero, then we don't have to actually do the
19321 instantiation now; we just have to do it sometime. Normally it is
19322 an error if this is an explicit instantiation but D is undefined.
19323 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19324 explicitly instantiated class template. */
19327 instantiate_decl (tree d
, int defer_ok
,
19328 bool expl_inst_class_mem_p
)
19330 tree tmpl
= DECL_TI_TEMPLATE (d
);
19337 bool pattern_defined
;
19338 location_t saved_loc
= input_location
;
19339 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19340 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19345 /* This function should only be used to instantiate templates for
19346 functions and static member variables. */
19347 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19349 /* Variables are never deferred; if instantiation is required, they
19350 are instantiated right away. That allows for better code in the
19351 case that an expression refers to the value of the variable --
19352 if the variable has a constant value the referring expression can
19353 take advantage of that fact. */
19355 || DECL_DECLARED_CONSTEXPR_P (d
))
19358 /* Don't instantiate cloned functions. Instead, instantiate the
19359 functions they cloned. */
19360 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19361 d
= DECL_CLONED_FUNCTION (d
);
19363 if (DECL_TEMPLATE_INSTANTIATED (d
)
19364 || (TREE_CODE (d
) == FUNCTION_DECL
19365 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19366 || DECL_TEMPLATE_SPECIALIZATION (d
))
19367 /* D has already been instantiated or explicitly specialized, so
19368 there's nothing for us to do here.
19370 It might seem reasonable to check whether or not D is an explicit
19371 instantiation, and, if so, stop here. But when an explicit
19372 instantiation is deferred until the end of the compilation,
19373 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19374 the instantiation. */
19377 /* Check to see whether we know that this template will be
19378 instantiated in some other file, as with "extern template"
19380 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19382 /* In general, we do not instantiate such templates. */
19383 if (external_p
&& !always_instantiate_p (d
))
19386 gen_tmpl
= most_general_template (tmpl
);
19387 gen_args
= DECL_TI_ARGS (d
);
19389 if (tmpl
!= gen_tmpl
)
19390 /* We should already have the extra args. */
19391 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19392 == TMPL_ARGS_DEPTH (gen_args
));
19393 /* And what's in the hash table should match D. */
19394 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19395 || spec
== NULL_TREE
);
19397 /* This needs to happen before any tsubsting. */
19398 if (! push_tinst_level (d
))
19401 timevar_push (TV_TEMPLATE_INST
);
19403 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19404 for the instantiation. */
19405 td
= template_for_substitution (d
);
19406 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19408 /* We should never be trying to instantiate a member of a class
19409 template or partial specialization. */
19410 gcc_assert (d
!= code_pattern
);
19412 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19413 || DECL_TEMPLATE_SPECIALIZATION (td
))
19414 /* In the case of a friend template whose definition is provided
19415 outside the class, we may have too many arguments. Drop the
19416 ones we don't need. The same is true for specializations. */
19417 args
= get_innermost_template_args
19418 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19422 if (TREE_CODE (d
) == FUNCTION_DECL
)
19423 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19424 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19426 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19428 /* We may be in the middle of deferred access check. Disable it now. */
19429 push_deferring_access_checks (dk_no_deferred
);
19431 /* Unless an explicit instantiation directive has already determined
19432 the linkage of D, remember that a definition is available for
19434 if (pattern_defined
19435 && !DECL_INTERFACE_KNOWN (d
)
19436 && !DECL_NOT_REALLY_EXTERN (d
))
19437 mark_definable (d
);
19439 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19440 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19441 input_location
= DECL_SOURCE_LOCATION (d
);
19443 /* If D is a member of an explicitly instantiated class template,
19444 and no definition is available, treat it like an implicit
19446 if (!pattern_defined
&& expl_inst_class_mem_p
19447 && DECL_EXPLICIT_INSTANTIATION (d
))
19449 /* Leave linkage flags alone on instantiations with anonymous
19451 if (TREE_PUBLIC (d
))
19453 DECL_NOT_REALLY_EXTERN (d
) = 0;
19454 DECL_INTERFACE_KNOWN (d
) = 0;
19456 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19459 if (TREE_CODE (d
) == FUNCTION_DECL
)
19460 maybe_instantiate_noexcept (d
);
19462 /* Defer all other templates, unless we have been explicitly
19463 forbidden from doing so. */
19464 if (/* If there is no definition, we cannot instantiate the
19467 /* If it's OK to postpone instantiation, do so. */
19469 /* If this is a static data member that will be defined
19470 elsewhere, we don't want to instantiate the entire data
19471 member, but we do want to instantiate the initializer so that
19472 we can substitute that elsewhere. */
19473 || (external_p
&& VAR_P (d
)))
19475 /* The definition of the static data member is now required so
19476 we must substitute the initializer. */
19478 && !DECL_INITIAL (d
)
19479 && DECL_INITIAL (code_pattern
))
19483 bool const_init
= false;
19485 ns
= decl_namespace_context (d
);
19486 push_nested_namespace (ns
);
19487 push_nested_class (DECL_CONTEXT (d
));
19488 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19490 tf_warning_or_error
, NULL_TREE
,
19491 /*integral_constant_expression_p=*/false);
19492 /* Make sure the initializer is still constant, in case of
19493 circular dependency (template/instantiate6.C). */
19495 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19496 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19497 /*asmspec_tree=*/NULL_TREE
,
19498 LOOKUP_ONLYCONVERTING
);
19499 pop_nested_class ();
19500 pop_nested_namespace (ns
);
19503 /* We restore the source position here because it's used by
19504 add_pending_template. */
19505 input_location
= saved_loc
;
19507 if (at_eof
&& !pattern_defined
19508 && DECL_EXPLICIT_INSTANTIATION (d
)
19509 && DECL_NOT_REALLY_EXTERN (d
))
19512 The definition of a non-exported function template, a
19513 non-exported member function template, or a non-exported
19514 member function or static data member of a class template
19515 shall be present in every translation unit in which it is
19516 explicitly instantiated. */
19517 permerror (input_location
, "explicit instantiation of %qD "
19518 "but no definition available", d
);
19520 /* If we're in unevaluated context, we just wanted to get the
19521 constant value; this isn't an odr use, so don't queue
19522 a full instantiation. */
19523 if (cp_unevaluated_operand
!= 0)
19525 /* ??? Historically, we have instantiated inline functions, even
19526 when marked as "extern template". */
19527 if (!(external_p
&& VAR_P (d
)))
19528 add_pending_template (d
);
19531 /* Tell the repository that D is available in this translation unit
19532 -- and see if it is supposed to be instantiated here. */
19533 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19535 /* In a PCH file, despite the fact that the repository hasn't
19536 requested instantiation in the PCH it is still possible that
19537 an instantiation will be required in a file that includes the
19540 add_pending_template (d
);
19541 /* Instantiate inline functions so that the inliner can do its
19542 job, even though we'll not be emitting a copy of this
19544 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19548 fn_context
= decl_function_context (d
);
19549 nested
= (current_function_decl
!= NULL_TREE
);
19551 push_to_top_level ();
19555 push_function_context ();
19556 cp_unevaluated_operand
= 0;
19557 c_inhibit_evaluation_warnings
= 0;
19560 /* Mark D as instantiated so that recursive calls to
19561 instantiate_decl do not try to instantiate it again. */
19562 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19564 /* Regenerate the declaration in case the template has been modified
19565 by a subsequent redeclaration. */
19566 regenerate_decl_from_template (d
, td
);
19568 /* We already set the file and line above. Reset them now in case
19569 they changed as a result of calling regenerate_decl_from_template. */
19570 input_location
= DECL_SOURCE_LOCATION (d
);
19575 bool const_init
= false;
19577 /* Clear out DECL_RTL; whatever was there before may not be right
19578 since we've reset the type of the declaration. */
19579 SET_DECL_RTL (d
, NULL
);
19580 DECL_IN_AGGR_P (d
) = 0;
19582 /* The initializer is placed in DECL_INITIAL by
19583 regenerate_decl_from_template so we don't need to
19584 push/pop_access_scope again here. Pull it out so that
19585 cp_finish_decl can process it. */
19586 init
= DECL_INITIAL (d
);
19587 DECL_INITIAL (d
) = NULL_TREE
;
19588 DECL_INITIALIZED_P (d
) = 0;
19590 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19591 initializer. That function will defer actual emission until
19592 we have a chance to determine linkage. */
19593 DECL_EXTERNAL (d
) = 0;
19595 /* Enter the scope of D so that access-checking works correctly. */
19596 push_nested_class (DECL_CONTEXT (d
));
19597 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19598 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19599 pop_nested_class ();
19601 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19602 synthesize_method (d
);
19603 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19605 struct pointer_map_t
*saved_local_specializations
;
19609 tree block
= NULL_TREE
;
19611 /* Save away the current list, in case we are instantiating one
19612 template from within the body of another. */
19613 saved_local_specializations
= local_specializations
;
19615 /* Set up the list of local specializations. */
19616 local_specializations
= pointer_map_create ();
19618 /* Set up context. */
19619 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19620 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19621 block
= push_stmt_list ();
19623 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19625 /* Some typedefs referenced from within the template code need to be
19626 access checked at template instantiation time, i.e now. These
19627 types were added to the template at parsing time. Let's get those
19628 and perform the access checks then. */
19629 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19632 /* Create substitution entries for the parameters. */
19633 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19634 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19635 spec_parm
= DECL_ARGUMENTS (d
);
19636 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19638 register_local_specialization (spec_parm
, tmpl_parm
);
19639 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19640 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19642 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19644 if (!DECL_PACK_P (tmpl_parm
))
19646 register_local_specialization (spec_parm
, tmpl_parm
);
19647 spec_parm
= DECL_CHAIN (spec_parm
);
19651 /* Register the (value) argument pack as a specialization of
19652 TMPL_PARM, then move on. */
19653 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19654 register_local_specialization (argpack
, tmpl_parm
);
19657 gcc_assert (!spec_parm
);
19659 /* Substitute into the body of the function. */
19660 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19661 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19662 tf_warning_or_error
, tmpl
);
19665 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19666 tf_warning_or_error
, tmpl
,
19667 /*integral_constant_expression_p=*/false);
19669 /* Set the current input_location to the end of the function
19670 so that finish_function knows where we are. */
19672 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19675 /* We don't need the local specializations any more. */
19676 pointer_map_destroy (local_specializations
);
19677 local_specializations
= saved_local_specializations
;
19679 /* Finish the function. */
19680 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19681 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19682 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19685 d
= finish_function (0);
19686 expand_or_defer_fn (d
);
19689 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19690 cp_check_omp_declare_reduction (d
);
19693 /* We're not deferring instantiation any more. */
19694 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19697 pop_from_top_level ();
19699 pop_function_context ();
19702 input_location
= saved_loc
;
19703 cp_unevaluated_operand
= saved_unevaluated_operand
;
19704 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19705 pop_deferring_access_checks ();
19706 pop_tinst_level ();
19708 timevar_pop (TV_TEMPLATE_INST
);
19713 /* Run through the list of templates that we wish we could
19714 instantiate, and instantiate any we can. RETRIES is the
19715 number of times we retry pending template instantiation. */
19718 instantiate_pending_templates (int retries
)
19721 location_t saved_loc
= input_location
;
19723 /* Instantiating templates may trigger vtable generation. This in turn
19724 may require further template instantiations. We place a limit here
19725 to avoid infinite loop. */
19726 if (pending_templates
&& retries
>= max_tinst_depth
)
19728 tree decl
= pending_templates
->tinst
->decl
;
19730 error ("template instantiation depth exceeds maximum of %d"
19731 " instantiating %q+D, possibly from virtual table generation"
19732 " (use -ftemplate-depth= to increase the maximum)",
19733 max_tinst_depth
, decl
);
19734 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19735 /* Pretend that we defined it. */
19736 DECL_INITIAL (decl
) = error_mark_node
;
19742 struct pending_template
**t
= &pending_templates
;
19743 struct pending_template
*last
= NULL
;
19747 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19748 bool complete
= false;
19750 if (TYPE_P (instantiation
))
19754 if (!COMPLETE_TYPE_P (instantiation
))
19756 instantiate_class_template (instantiation
);
19757 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19758 for (fn
= TYPE_METHODS (instantiation
);
19760 fn
= TREE_CHAIN (fn
))
19761 if (! DECL_ARTIFICIAL (fn
))
19762 instantiate_decl (fn
,
19764 /*expl_inst_class_mem_p=*/false);
19765 if (COMPLETE_TYPE_P (instantiation
))
19769 complete
= COMPLETE_TYPE_P (instantiation
);
19773 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19774 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19777 = instantiate_decl (instantiation
,
19779 /*expl_inst_class_mem_p=*/false);
19780 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19784 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19785 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19789 /* If INSTANTIATION has been instantiated, then we don't
19790 need to consider it again in the future. */
19798 current_tinst_level
= NULL
;
19800 last_pending_template
= last
;
19802 while (reconsider
);
19804 input_location
= saved_loc
;
19807 /* Substitute ARGVEC into T, which is a list of initializers for
19808 either base class or a non-static data member. The TREE_PURPOSEs
19809 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19810 instantiate_decl. */
19813 tsubst_initializer_list (tree t
, tree argvec
)
19815 tree inits
= NULL_TREE
;
19817 for (; t
; t
= TREE_CHAIN (t
))
19821 tree expanded_bases
= NULL_TREE
;
19822 tree expanded_arguments
= NULL_TREE
;
19825 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19830 /* Expand the base class expansion type into separate base
19832 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19833 tf_warning_or_error
,
19835 if (expanded_bases
== error_mark_node
)
19838 /* We'll be building separate TREE_LISTs of arguments for
19840 len
= TREE_VEC_LENGTH (expanded_bases
);
19841 expanded_arguments
= make_tree_vec (len
);
19842 for (i
= 0; i
< len
; i
++)
19843 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19845 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19846 expand each argument in the TREE_VALUE of t. */
19847 expr
= make_node (EXPR_PACK_EXPANSION
);
19848 PACK_EXPANSION_LOCAL_P (expr
) = true;
19849 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19850 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19852 if (TREE_VALUE (t
) == void_type_node
)
19853 /* VOID_TYPE_NODE is used to indicate
19854 value-initialization. */
19856 for (i
= 0; i
< len
; i
++)
19857 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19861 /* Substitute parameter packs into each argument in the
19863 in_base_initializer
= 1;
19864 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19866 tree expanded_exprs
;
19868 /* Expand the argument. */
19869 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19871 = tsubst_pack_expansion (expr
, argvec
,
19872 tf_warning_or_error
,
19874 if (expanded_exprs
== error_mark_node
)
19877 /* Prepend each of the expanded expressions to the
19878 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19879 for (i
= 0; i
< len
; i
++)
19881 TREE_VEC_ELT (expanded_arguments
, i
) =
19882 tree_cons (NULL_TREE
,
19883 TREE_VEC_ELT (expanded_exprs
, i
),
19884 TREE_VEC_ELT (expanded_arguments
, i
));
19887 in_base_initializer
= 0;
19889 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19890 since we built them backwards. */
19891 for (i
= 0; i
< len
; i
++)
19893 TREE_VEC_ELT (expanded_arguments
, i
) =
19894 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19899 for (i
= 0; i
< len
; ++i
)
19901 if (expanded_bases
)
19903 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19904 decl
= expand_member_init (decl
);
19905 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19910 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19911 tf_warning_or_error
, NULL_TREE
);
19913 decl
= expand_member_init (decl
);
19914 if (decl
&& !DECL_P (decl
))
19915 in_base_initializer
= 1;
19917 init
= TREE_VALUE (t
);
19919 if (init
!= void_type_node
)
19920 init
= tsubst_expr (init
, argvec
,
19921 tf_warning_or_error
, NULL_TREE
,
19922 /*integral_constant_expression_p=*/false);
19923 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19924 /* If we had an initializer but it instantiated to nothing,
19925 value-initialize the object. This will only occur when
19926 the initializer was a pack expansion where the parameter
19927 packs used in that expansion were of length zero. */
19928 init
= void_type_node
;
19929 in_base_initializer
= 0;
19934 init
= build_tree_list (decl
, init
);
19935 TREE_CHAIN (init
) = inits
;
19943 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19946 set_current_access_from_decl (tree decl
)
19948 if (TREE_PRIVATE (decl
))
19949 current_access_specifier
= access_private_node
;
19950 else if (TREE_PROTECTED (decl
))
19951 current_access_specifier
= access_protected_node
;
19953 current_access_specifier
= access_public_node
;
19956 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19957 is the instantiation (which should have been created with
19958 start_enum) and ARGS are the template arguments to use. */
19961 tsubst_enum (tree tag
, tree newtag
, tree args
)
19965 if (SCOPED_ENUM_P (newtag
))
19966 begin_scope (sk_scoped_enum
, newtag
);
19968 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19973 decl
= TREE_VALUE (e
);
19974 /* Note that in a template enum, the TREE_VALUE is the
19975 CONST_DECL, not the corresponding INTEGER_CST. */
19976 value
= tsubst_expr (DECL_INITIAL (decl
),
19977 args
, tf_warning_or_error
, NULL_TREE
,
19978 /*integral_constant_expression_p=*/true);
19980 /* Give this enumeration constant the correct access. */
19981 set_current_access_from_decl (decl
);
19983 /* Actually build the enumerator itself. */
19985 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19988 if (SCOPED_ENUM_P (newtag
))
19991 finish_enum_value_list (newtag
);
19992 finish_enum (newtag
);
19994 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19995 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19998 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19999 its type -- but without substituting the innermost set of template
20000 arguments. So, innermost set of template parameters will appear in
20004 get_mostly_instantiated_function_type (tree decl
)
20012 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
20013 targs
= DECL_TI_ARGS (decl
);
20014 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
20015 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
20017 /* There should be as many levels of arguments as there are levels
20019 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
20021 fn_type
= TREE_TYPE (tmpl
);
20023 if (parm_depth
== 1)
20024 /* No substitution is necessary. */
20031 /* Replace the innermost level of the TARGS with NULL_TREEs to
20032 let tsubst know not to substitute for those parameters. */
20033 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20034 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20035 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20036 TMPL_ARGS_LEVEL (targs
, i
));
20037 SET_TMPL_ARGS_LEVEL (partial_args
,
20038 TMPL_ARGS_DEPTH (targs
),
20039 make_tree_vec (DECL_NTPARMS (tmpl
)));
20041 /* Make sure that we can see identifiers, and compute access
20043 push_access_scope (decl
);
20045 ++processing_template_decl
;
20046 /* Now, do the (partial) substitution to figure out the
20047 appropriate function type. */
20048 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20049 --processing_template_decl
;
20051 /* Substitute into the template parameters to obtain the real
20052 innermost set of parameters. This step is important if the
20053 innermost set of template parameters contains value
20054 parameters whose types depend on outer template parameters. */
20055 TREE_VEC_LENGTH (partial_args
)--;
20056 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20058 pop_access_scope (decl
);
20064 /* Return truthvalue if we're processing a template different from
20065 the last one involved in diagnostics. */
20067 problematic_instantiation_changed (void)
20069 return current_tinst_level
!= last_error_tinst_level
;
20072 /* Remember current template involved in diagnostics. */
20074 record_last_problematic_instantiation (void)
20076 last_error_tinst_level
= current_tinst_level
;
20079 struct tinst_level
*
20080 current_instantiation (void)
20082 return current_tinst_level
;
20085 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20086 type. Return zero for ok, nonzero for disallowed. Issue error and
20087 warning messages under control of COMPLAIN. */
20090 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20092 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20094 else if (POINTER_TYPE_P (type
))
20096 else if (TYPE_PTRMEM_P (type
))
20098 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20100 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20102 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20104 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20107 if (complain
& tf_error
)
20109 if (type
== error_mark_node
)
20110 inform (input_location
, "invalid template non-type parameter");
20112 error ("%q#T is not a valid type for a template non-type parameter",
20118 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20119 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20122 dependent_type_p_r (tree type
)
20128 A type is dependent if it is:
20130 -- a template parameter. Template template parameters are types
20131 for us (since TYPE_P holds true for them) so we handle
20133 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20134 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20136 /* -- a qualified-id with a nested-name-specifier which contains a
20137 class-name that names a dependent type or whose unqualified-id
20138 names a dependent type. */
20139 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20141 /* -- a cv-qualified type where the cv-unqualified type is
20143 type
= TYPE_MAIN_VARIANT (type
);
20144 /* -- a compound type constructed from any dependent type. */
20145 if (TYPE_PTRMEM_P (type
))
20146 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20147 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20149 else if (TYPE_PTR_P (type
)
20150 || TREE_CODE (type
) == REFERENCE_TYPE
)
20151 return dependent_type_p (TREE_TYPE (type
));
20152 else if (TREE_CODE (type
) == FUNCTION_TYPE
20153 || TREE_CODE (type
) == METHOD_TYPE
)
20157 if (dependent_type_p (TREE_TYPE (type
)))
20159 for (arg_type
= TYPE_ARG_TYPES (type
);
20161 arg_type
= TREE_CHAIN (arg_type
))
20162 if (dependent_type_p (TREE_VALUE (arg_type
)))
20166 /* -- an array type constructed from any dependent type or whose
20167 size is specified by a constant expression that is
20170 We checked for type- and value-dependence of the bounds in
20171 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20172 if (TREE_CODE (type
) == ARRAY_TYPE
)
20174 if (TYPE_DOMAIN (type
)
20175 && dependent_type_p (TYPE_DOMAIN (type
)))
20177 return dependent_type_p (TREE_TYPE (type
));
20180 /* -- a template-id in which either the template name is a template
20182 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20184 /* ... or any of the template arguments is a dependent type or
20185 an expression that is type-dependent or value-dependent. */
20186 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20187 && (any_dependent_template_arguments_p
20188 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20191 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20192 dependent; if the argument of the `typeof' expression is not
20193 type-dependent, then it should already been have resolved. */
20194 if (TREE_CODE (type
) == TYPEOF_TYPE
20195 || TREE_CODE (type
) == DECLTYPE_TYPE
20196 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20199 /* A template argument pack is dependent if any of its packed
20201 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20203 tree args
= ARGUMENT_PACK_ARGS (type
);
20204 int i
, len
= TREE_VEC_LENGTH (args
);
20205 for (i
= 0; i
< len
; ++i
)
20206 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20210 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20211 be template parameters. */
20212 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20215 /* The standard does not specifically mention types that are local
20216 to template functions or local classes, but they should be
20217 considered dependent too. For example:
20219 template <int I> void f() {
20224 The size of `E' cannot be known until the value of `I' has been
20225 determined. Therefore, `E' must be considered dependent. */
20226 scope
= TYPE_CONTEXT (type
);
20227 if (scope
&& TYPE_P (scope
))
20228 return dependent_type_p (scope
);
20229 /* Don't use type_dependent_expression_p here, as it can lead
20230 to infinite recursion trying to determine whether a lambda
20231 nested in a lambda is dependent (c++/47687). */
20232 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20233 && DECL_LANG_SPECIFIC (scope
)
20234 && DECL_TEMPLATE_INFO (scope
)
20235 && (any_dependent_template_arguments_p
20236 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20239 /* Other types are non-dependent. */
20243 /* Returns TRUE if TYPE is dependent, in the sense of
20244 [temp.dep.type]. Note that a NULL type is considered dependent. */
20247 dependent_type_p (tree type
)
20249 /* If there are no template parameters in scope, then there can't be
20250 any dependent types. */
20251 if (!processing_template_decl
)
20253 /* If we are not processing a template, then nobody should be
20254 providing us with a dependent type. */
20256 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20260 /* If the type is NULL, we have not computed a type for the entity
20261 in question; in that case, the type is dependent. */
20265 /* Erroneous types can be considered non-dependent. */
20266 if (type
== error_mark_node
)
20269 /* If we have not already computed the appropriate value for TYPE,
20271 if (!TYPE_DEPENDENT_P_VALID (type
))
20273 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20274 TYPE_DEPENDENT_P_VALID (type
) = 1;
20277 return TYPE_DEPENDENT_P (type
);
20280 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20281 lookup. In other words, a dependent type that is not the current
20285 dependent_scope_p (tree scope
)
20287 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20288 && !currently_open_class (scope
));
20291 /* T is a SCOPE_REF; return whether we need to consider it
20292 instantiation-dependent so that we can check access at instantiation
20293 time even though we know which member it resolves to. */
20296 instantiation_dependent_scope_ref_p (tree t
)
20298 if (DECL_P (TREE_OPERAND (t
, 1))
20299 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20300 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20301 TREE_OPERAND (t
, 1)))
20307 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20308 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20311 /* Note that this predicate is not appropriate for general expressions;
20312 only constant expressions (that satisfy potential_constant_expression)
20313 can be tested for value dependence. */
20316 value_dependent_expression_p (tree expression
)
20318 if (!processing_template_decl
)
20321 /* A name declared with a dependent type. */
20322 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20325 switch (TREE_CODE (expression
))
20327 case IDENTIFIER_NODE
:
20328 /* A name that has not been looked up -- must be dependent. */
20331 case TEMPLATE_PARM_INDEX
:
20332 /* A non-type template parm. */
20336 /* A non-type template parm. */
20337 if (DECL_TEMPLATE_PARM_P (expression
))
20339 return value_dependent_expression_p (DECL_INITIAL (expression
));
20342 /* A constant with literal type and is initialized
20343 with an expression that is value-dependent.
20345 Note that a non-dependent parenthesized initializer will have
20346 already been replaced with its constant value, so if we see
20347 a TREE_LIST it must be dependent. */
20348 if (DECL_INITIAL (expression
)
20349 && decl_constant_var_p (expression
)
20350 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20351 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20355 case DYNAMIC_CAST_EXPR
:
20356 case STATIC_CAST_EXPR
:
20357 case CONST_CAST_EXPR
:
20358 case REINTERPRET_CAST_EXPR
:
20360 /* These expressions are value-dependent if the type to which
20361 the cast occurs is dependent or the expression being casted
20362 is value-dependent. */
20364 tree type
= TREE_TYPE (expression
);
20366 if (dependent_type_p (type
))
20369 /* A functional cast has a list of operands. */
20370 expression
= TREE_OPERAND (expression
, 0);
20373 /* If there are no operands, it must be an expression such
20374 as "int()". This should not happen for aggregate types
20375 because it would form non-constant expressions. */
20376 gcc_assert (cxx_dialect
>= cxx11
20377 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20382 if (TREE_CODE (expression
) == TREE_LIST
)
20383 return any_value_dependent_elements_p (expression
);
20385 return value_dependent_expression_p (expression
);
20389 if (SIZEOF_EXPR_TYPE_P (expression
))
20390 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20394 /* A `sizeof' expression is value-dependent if the operand is
20395 type-dependent or is a pack expansion. */
20396 expression
= TREE_OPERAND (expression
, 0);
20397 if (PACK_EXPANSION_P (expression
))
20399 else if (TYPE_P (expression
))
20400 return dependent_type_p (expression
);
20401 return instantiation_dependent_expression_p (expression
);
20403 case AT_ENCODE_EXPR
:
20404 /* An 'encode' expression is value-dependent if the operand is
20406 expression
= TREE_OPERAND (expression
, 0);
20407 return dependent_type_p (expression
);
20409 case NOEXCEPT_EXPR
:
20410 expression
= TREE_OPERAND (expression
, 0);
20411 return instantiation_dependent_expression_p (expression
);
20414 /* All instantiation-dependent expressions should also be considered
20415 value-dependent. */
20416 return instantiation_dependent_scope_ref_p (expression
);
20418 case COMPONENT_REF
:
20419 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20420 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20422 case NONTYPE_ARGUMENT_PACK
:
20423 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20424 is value-dependent. */
20426 tree values
= ARGUMENT_PACK_ARGS (expression
);
20427 int i
, len
= TREE_VEC_LENGTH (values
);
20429 for (i
= 0; i
< len
; ++i
)
20430 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20438 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20439 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20440 || (type2
? dependent_type_p (type2
) : false));
20444 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20445 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20448 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20449 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20453 tree op
= TREE_OPERAND (expression
, 0);
20454 return (value_dependent_expression_p (op
)
20455 || has_value_dependent_address (op
));
20460 tree fn
= get_callee_fndecl (expression
);
20462 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20464 nargs
= call_expr_nargs (expression
);
20465 for (i
= 0; i
< nargs
; ++i
)
20467 tree op
= CALL_EXPR_ARG (expression
, i
);
20468 /* In a call to a constexpr member function, look through the
20469 implicit ADDR_EXPR on the object argument so that it doesn't
20470 cause the call to be considered value-dependent. We also
20471 look through it in potential_constant_expression. */
20472 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20473 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20474 && TREE_CODE (op
) == ADDR_EXPR
)
20475 op
= TREE_OPERAND (op
, 0);
20476 if (value_dependent_expression_p (op
))
20482 case TEMPLATE_ID_EXPR
:
20483 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20485 return type_dependent_expression_p (expression
);
20491 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20492 if (value_dependent_expression_p (val
))
20498 /* Treat a GNU statement expression as dependent to avoid crashing
20499 under fold_non_dependent_expr; it can't be constant. */
20503 /* A constant expression is value-dependent if any subexpression is
20504 value-dependent. */
20505 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20507 case tcc_reference
:
20509 case tcc_comparison
:
20511 case tcc_expression
:
20514 int i
, len
= cp_tree_operand_length (expression
);
20516 for (i
= 0; i
< len
; i
++)
20518 tree t
= TREE_OPERAND (expression
, i
);
20520 /* In some cases, some of the operands may be missing.l
20521 (For example, in the case of PREDECREMENT_EXPR, the
20522 amount to increment by may be missing.) That doesn't
20523 make the expression dependent. */
20524 if (t
&& value_dependent_expression_p (t
))
20535 /* The expression is not value-dependent. */
20539 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20540 [temp.dep.expr]. Note that an expression with no type is
20541 considered dependent. Other parts of the compiler arrange for an
20542 expression with type-dependent subexpressions to have no type, so
20543 this function doesn't have to be fully recursive. */
20546 type_dependent_expression_p (tree expression
)
20548 if (!processing_template_decl
)
20551 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20554 /* An unresolved name is always dependent. */
20555 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20558 /* Some expression forms are never type-dependent. */
20559 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20560 || TREE_CODE (expression
) == SIZEOF_EXPR
20561 || TREE_CODE (expression
) == ALIGNOF_EXPR
20562 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20563 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20564 || TREE_CODE (expression
) == TRAIT_EXPR
20565 || TREE_CODE (expression
) == TYPEID_EXPR
20566 || TREE_CODE (expression
) == DELETE_EXPR
20567 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20568 || TREE_CODE (expression
) == THROW_EXPR
)
20571 /* The types of these expressions depends only on the type to which
20572 the cast occurs. */
20573 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20574 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20575 || TREE_CODE (expression
) == CONST_CAST_EXPR
20576 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20577 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20578 || TREE_CODE (expression
) == CAST_EXPR
)
20579 return dependent_type_p (TREE_TYPE (expression
));
20581 /* The types of these expressions depends only on the type created
20582 by the expression. */
20583 if (TREE_CODE (expression
) == NEW_EXPR
20584 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20586 /* For NEW_EXPR tree nodes created inside a template, either
20587 the object type itself or a TREE_LIST may appear as the
20589 tree type
= TREE_OPERAND (expression
, 1);
20590 if (TREE_CODE (type
) == TREE_LIST
)
20591 /* This is an array type. We need to check array dimensions
20593 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20594 || value_dependent_expression_p
20595 (TREE_OPERAND (TREE_VALUE (type
), 1));
20597 return dependent_type_p (type
);
20600 if (TREE_CODE (expression
) == SCOPE_REF
)
20602 tree scope
= TREE_OPERAND (expression
, 0);
20603 tree name
= TREE_OPERAND (expression
, 1);
20605 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20606 contains an identifier associated by name lookup with one or more
20607 declarations declared with a dependent type, or...a
20608 nested-name-specifier or qualified-id that names a member of an
20609 unknown specialization. */
20610 return (type_dependent_expression_p (name
)
20611 || dependent_scope_p (scope
));
20614 if (TREE_CODE (expression
) == FUNCTION_DECL
20615 && DECL_LANG_SPECIFIC (expression
)
20616 && DECL_TEMPLATE_INFO (expression
)
20617 && (any_dependent_template_arguments_p
20618 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20621 if (TREE_CODE (expression
) == TEMPLATE_DECL
20622 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20625 if (TREE_CODE (expression
) == STMT_EXPR
)
20626 expression
= stmt_expr_value_expr (expression
);
20628 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20633 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20635 if (type_dependent_expression_p (elt
))
20641 /* A static data member of the current instantiation with incomplete
20642 array type is type-dependent, as the definition and specializations
20643 can have different bounds. */
20644 if (VAR_P (expression
)
20645 && DECL_CLASS_SCOPE_P (expression
)
20646 && dependent_type_p (DECL_CONTEXT (expression
))
20647 && VAR_HAD_UNKNOWN_BOUND (expression
))
20650 /* An array of unknown bound depending on a variadic parameter, eg:
20652 template<typename... Args>
20653 void foo (Args... args)
20655 int arr[] = { args... };
20658 template<int... vals>
20661 int arr[] = { vals... };
20664 If the array has no length and has an initializer, it must be that
20665 we couldn't determine its length in cp_complete_array_type because
20666 it is dependent. */
20667 if (VAR_P (expression
)
20668 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20669 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20670 && DECL_INITIAL (expression
))
20673 if (TREE_TYPE (expression
) == unknown_type_node
)
20675 if (TREE_CODE (expression
) == ADDR_EXPR
)
20676 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20677 if (TREE_CODE (expression
) == COMPONENT_REF
20678 || TREE_CODE (expression
) == OFFSET_REF
)
20680 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20682 expression
= TREE_OPERAND (expression
, 1);
20683 if (identifier_p (expression
))
20686 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20687 if (TREE_CODE (expression
) == SCOPE_REF
)
20690 /* Always dependent, on the number of arguments if nothing else. */
20691 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20694 if (BASELINK_P (expression
))
20695 expression
= BASELINK_FUNCTIONS (expression
);
20697 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20699 if (any_dependent_template_arguments_p
20700 (TREE_OPERAND (expression
, 1)))
20702 expression
= TREE_OPERAND (expression
, 0);
20704 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20705 || TREE_CODE (expression
) == FUNCTION_DECL
);
20709 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20711 expression
= OVL_NEXT (expression
);
20716 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20718 return (dependent_type_p (TREE_TYPE (expression
)));
20721 /* walk_tree callback function for instantiation_dependent_expression_p,
20722 below. Returns non-zero if a dependent subexpression is found. */
20725 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20730 /* We don't have to worry about decltype currently because decltype
20731 of an instantiation-dependent expr is a dependent type. This
20732 might change depending on the resolution of DR 1172. */
20733 *walk_subtrees
= false;
20736 enum tree_code code
= TREE_CODE (*tp
);
20739 /* Don't treat an argument list as dependent just because it has no
20747 /* A constant with a dependent initializer is dependent. */
20748 if (value_dependent_expression_p (*tp
))
20752 case TEMPLATE_PARM_INDEX
:
20755 /* Handle expressions with type operands. */
20759 case AT_ENCODE_EXPR
:
20761 tree op
= TREE_OPERAND (*tp
, 0);
20762 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20763 op
= TREE_TYPE (op
);
20766 if (dependent_type_p (op
))
20770 *walk_subtrees
= false;
20778 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20779 || (TRAIT_EXPR_TYPE2 (*tp
)
20780 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20782 *walk_subtrees
= false;
20785 case COMPONENT_REF
:
20786 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20787 /* In a template, finish_class_member_access_expr creates a
20788 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20789 type-dependent, so that we can check access control at
20790 instantiation time (PR 42277). See also Core issue 1273. */
20795 if (instantiation_dependent_scope_ref_p (*tp
))
20800 /* Treat statement-expressions as dependent. */
20808 if (type_dependent_expression_p (*tp
))
20814 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20815 sense defined by the ABI:
20817 "An expression is instantiation-dependent if it is type-dependent
20818 or value-dependent, or it has a subexpression that is type-dependent
20819 or value-dependent." */
20822 instantiation_dependent_expression_p (tree expression
)
20826 if (!processing_template_decl
)
20829 if (expression
== error_mark_node
)
20832 result
= cp_walk_tree_without_duplicates (&expression
,
20833 instantiation_dependent_r
, NULL
);
20834 return result
!= NULL_TREE
;
20837 /* Like type_dependent_expression_p, but it also works while not processing
20838 a template definition, i.e. during substitution or mangling. */
20841 type_dependent_expression_p_push (tree expr
)
20844 ++processing_template_decl
;
20845 b
= type_dependent_expression_p (expr
);
20846 --processing_template_decl
;
20850 /* Returns TRUE if ARGS contains a type-dependent expression. */
20853 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20858 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20860 if (type_dependent_expression_p (arg
))
20866 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20867 expressions) contains any type-dependent expressions. */
20870 any_type_dependent_elements_p (const_tree list
)
20872 for (; list
; list
= TREE_CHAIN (list
))
20873 if (type_dependent_expression_p (TREE_VALUE (list
)))
20879 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20880 expressions) contains any value-dependent expressions. */
20883 any_value_dependent_elements_p (const_tree list
)
20885 for (; list
; list
= TREE_CHAIN (list
))
20886 if (value_dependent_expression_p (TREE_VALUE (list
)))
20892 /* Returns TRUE if the ARG (a template argument) is dependent. */
20895 dependent_template_arg_p (tree arg
)
20897 if (!processing_template_decl
)
20900 /* Assume a template argument that was wrongly written by the user
20901 is dependent. This is consistent with what
20902 any_dependent_template_arguments_p [that calls this function]
20904 if (!arg
|| arg
== error_mark_node
)
20907 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20908 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20910 if (TREE_CODE (arg
) == TEMPLATE_DECL
20911 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20912 return dependent_template_p (arg
);
20913 else if (ARGUMENT_PACK_P (arg
))
20915 tree args
= ARGUMENT_PACK_ARGS (arg
);
20916 int i
, len
= TREE_VEC_LENGTH (args
);
20917 for (i
= 0; i
< len
; ++i
)
20919 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20925 else if (TYPE_P (arg
))
20926 return dependent_type_p (arg
);
20928 return (type_dependent_expression_p (arg
)
20929 || value_dependent_expression_p (arg
));
20932 /* Returns true if ARGS (a collection of template arguments) contains
20933 any types that require structural equality testing. */
20936 any_template_arguments_need_structural_equality_p (tree args
)
20943 if (args
== error_mark_node
)
20946 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20948 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20949 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20951 tree arg
= TREE_VEC_ELT (level
, j
);
20952 tree packed_args
= NULL_TREE
;
20955 if (ARGUMENT_PACK_P (arg
))
20957 /* Look inside the argument pack. */
20958 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20959 len
= TREE_VEC_LENGTH (packed_args
);
20962 for (k
= 0; k
< len
; ++k
)
20965 arg
= TREE_VEC_ELT (packed_args
, k
);
20967 if (error_operand_p (arg
))
20969 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20971 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20973 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20974 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20983 /* Returns true if ARGS (a collection of template arguments) contains
20984 any dependent arguments. */
20987 any_dependent_template_arguments_p (const_tree args
)
20994 if (args
== error_mark_node
)
20997 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20999 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21000 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21001 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21008 /* Returns TRUE if the template TMPL is dependent. */
21011 dependent_template_p (tree tmpl
)
21013 if (TREE_CODE (tmpl
) == OVERLOAD
)
21017 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21019 tmpl
= OVL_NEXT (tmpl
);
21024 /* Template template parameters are dependent. */
21025 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21026 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21028 /* So are names that have not been looked up. */
21029 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21031 /* So are member templates of dependent classes. */
21032 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21033 return dependent_type_p (DECL_CONTEXT (tmpl
));
21037 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21040 dependent_template_id_p (tree tmpl
, tree args
)
21042 return (dependent_template_p (tmpl
)
21043 || any_dependent_template_arguments_p (args
));
21046 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21050 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21054 if (!processing_template_decl
)
21057 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21059 tree decl
= TREE_VEC_ELT (declv
, i
);
21060 tree init
= TREE_VEC_ELT (initv
, i
);
21061 tree cond
= TREE_VEC_ELT (condv
, i
);
21062 tree incr
= TREE_VEC_ELT (incrv
, i
);
21064 if (type_dependent_expression_p (decl
))
21067 if (init
&& type_dependent_expression_p (init
))
21070 if (type_dependent_expression_p (cond
))
21073 if (COMPARISON_CLASS_P (cond
)
21074 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21075 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21078 if (TREE_CODE (incr
) == MODOP_EXPR
)
21080 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21081 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21084 else if (type_dependent_expression_p (incr
))
21086 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21088 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21090 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21092 tree t
= TREE_OPERAND (incr
, 1);
21093 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21094 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21103 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21104 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21105 no such TYPE can be found. Note that this function peers inside
21106 uninstantiated templates and therefore should be used only in
21107 extremely limited situations. ONLY_CURRENT_P restricts this
21108 peering to the currently open classes hierarchy (which is required
21109 when comparing types). */
21112 resolve_typename_type (tree type
, bool only_current_p
)
21121 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21123 scope
= TYPE_CONTEXT (type
);
21124 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21125 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21126 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21127 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21128 identifier of the TYPENAME_TYPE anymore.
21129 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21130 TYPENAME_TYPE instead, we avoid messing up with a possible
21131 typedef variant case. */
21132 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21134 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21135 it first before we can figure out what NAME refers to. */
21136 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21138 if (TYPENAME_IS_RESOLVING_P (scope
))
21139 /* Given a class template A with a dependent base with nested type C,
21140 typedef typename A::C::C C will land us here, as trying to resolve
21141 the initial A::C leads to the local C typedef, which leads back to
21142 A::C::C. So we break the recursion now. */
21145 scope
= resolve_typename_type (scope
, only_current_p
);
21147 /* If we don't know what SCOPE refers to, then we cannot resolve the
21149 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21151 /* If the SCOPE is a template type parameter, we have no way of
21152 resolving the name. */
21153 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21155 /* If the SCOPE is not the current instantiation, there's no reason
21156 to look inside it. */
21157 if (only_current_p
&& !currently_open_class (scope
))
21159 /* If this is a typedef, we don't want to look inside (c++/11987). */
21160 if (typedef_variant_p (type
))
21162 /* If SCOPE isn't the template itself, it will not have a valid
21163 TYPE_FIELDS list. */
21164 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21165 /* scope is either the template itself or a compatible instantiation
21166 like X<T>, so look up the name in the original template. */
21167 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21169 /* scope is a partial instantiation, so we can't do the lookup or we
21170 will lose the template arguments. */
21172 /* Enter the SCOPE so that name lookup will be resolved as if we
21173 were in the class definition. In particular, SCOPE will no
21174 longer be considered a dependent type. */
21175 pushed_scope
= push_scope (scope
);
21176 /* Look up the declaration. */
21177 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21178 tf_warning_or_error
);
21180 result
= NULL_TREE
;
21182 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21183 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21186 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21187 && TREE_CODE (decl
) == TYPE_DECL
)
21189 result
= TREE_TYPE (decl
);
21190 if (result
== error_mark_node
)
21191 result
= NULL_TREE
;
21193 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21194 && DECL_CLASS_TEMPLATE_P (decl
))
21198 /* Obtain the template and the arguments. */
21199 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21200 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21201 /* Instantiate the template. */
21202 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21203 /*entering_scope=*/0,
21204 tf_error
| tf_user
);
21205 if (result
== error_mark_node
)
21206 result
= NULL_TREE
;
21209 /* Leave the SCOPE. */
21211 pop_scope (pushed_scope
);
21213 /* If we failed to resolve it, return the original typename. */
21217 /* If lookup found a typename type, resolve that too. */
21218 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21220 /* Ill-formed programs can cause infinite recursion here, so we
21221 must catch that. */
21222 TYPENAME_IS_RESOLVING_P (type
) = 1;
21223 result
= resolve_typename_type (result
, only_current_p
);
21224 TYPENAME_IS_RESOLVING_P (type
) = 0;
21227 /* Qualify the resulting type. */
21228 quals
= cp_type_quals (type
);
21230 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21235 /* EXPR is an expression which is not type-dependent. Return a proxy
21236 for EXPR that can be used to compute the types of larger
21237 expressions containing EXPR. */
21240 build_non_dependent_expr (tree expr
)
21244 #ifdef ENABLE_CHECKING
21245 /* Try to get a constant value for all non-dependent expressions in
21246 order to expose bugs in *_dependent_expression_p and constexpr. */
21247 if (cxx_dialect
>= cxx11
)
21248 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21251 /* Preserve OVERLOADs; the functions must be available to resolve
21254 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21255 inner_expr
= stmt_expr_value_expr (inner_expr
);
21256 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21257 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21258 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21259 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21260 if (is_overloaded_fn (inner_expr
)
21261 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21263 /* There is no need to return a proxy for a variable. */
21266 /* Preserve string constants; conversions from string constants to
21267 "char *" are allowed, even though normally a "const char *"
21268 cannot be used to initialize a "char *". */
21269 if (TREE_CODE (expr
) == STRING_CST
)
21271 /* Preserve arithmetic constants, as an optimization -- there is no
21272 reason to create a new node. */
21273 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21275 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21276 There is at least one place where we want to know that a
21277 particular expression is a throw-expression: when checking a ?:
21278 expression, there are special rules if the second or third
21279 argument is a throw-expression. */
21280 if (TREE_CODE (expr
) == THROW_EXPR
)
21283 /* Don't wrap an initializer list, we need to be able to look inside. */
21284 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21287 /* Don't wrap a dummy object, we need to be able to test for it. */
21288 if (is_dummy_object (expr
))
21291 if (TREE_CODE (expr
) == COND_EXPR
)
21292 return build3 (COND_EXPR
,
21294 TREE_OPERAND (expr
, 0),
21295 (TREE_OPERAND (expr
, 1)
21296 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21297 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21298 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21299 if (TREE_CODE (expr
) == COMPOUND_EXPR
21300 && !COMPOUND_EXPR_OVERLOADED (expr
))
21301 return build2 (COMPOUND_EXPR
,
21303 TREE_OPERAND (expr
, 0),
21304 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21306 /* If the type is unknown, it can't really be non-dependent */
21307 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21309 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21310 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21313 /* ARGS is a vector of expressions as arguments to a function call.
21314 Replace the arguments with equivalent non-dependent expressions.
21315 This modifies ARGS in place. */
21318 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21323 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21325 tree newarg
= build_non_dependent_expr (arg
);
21327 (*args
)[ix
] = newarg
;
21331 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21332 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21336 make_auto_1 (tree name
)
21338 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21339 TYPE_NAME (au
) = build_decl (input_location
,
21340 TYPE_DECL
, name
, au
);
21341 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21342 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21343 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21344 TYPE_NAME (au
), NULL_TREE
);
21345 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21346 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21347 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21353 make_decltype_auto (void)
21355 return make_auto_1 (get_identifier ("decltype(auto)"));
21361 return make_auto_1 (get_identifier ("auto"));
21364 /* Given type ARG, return std::initializer_list<ARG>. */
21369 tree std_init_list
= namespace_binding
21370 (get_identifier ("initializer_list"), std_node
);
21372 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21374 error ("deducing from brace-enclosed initializer list requires "
21375 "#include <initializer_list>");
21376 return error_mark_node
;
21378 argvec
= make_tree_vec (1);
21379 TREE_VEC_ELT (argvec
, 0) = arg
;
21380 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21381 NULL_TREE
, 0, tf_warning_or_error
);
21384 /* Replace auto in TYPE with std::initializer_list<auto>. */
21387 listify_autos (tree type
, tree auto_node
)
21389 tree init_auto
= listify (auto_node
);
21390 tree argvec
= make_tree_vec (1);
21391 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21392 if (processing_template_decl
)
21393 argvec
= add_to_template_args (current_template_args (), argvec
);
21394 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21397 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21398 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21401 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21405 if (init
== error_mark_node
)
21406 return error_mark_node
;
21408 if (type_dependent_expression_p (init
))
21409 /* Defining a subset of type-dependent expressions that we can deduce
21410 from ahead of time isn't worth the trouble. */
21413 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21414 with either a new invented type template parameter U or, if the
21415 initializer is a braced-init-list (8.5.4), with
21416 std::initializer_list<U>. */
21417 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21418 type
= listify_autos (type
, auto_node
);
21420 init
= resolve_nondeduced_context (init
);
21422 targs
= make_tree_vec (1);
21423 if (AUTO_IS_DECLTYPE (auto_node
))
21425 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21426 TREE_VEC_ELT (targs
, 0)
21427 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21428 if (type
!= auto_node
)
21430 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21431 return error_mark_node
;
21436 tree parms
= build_tree_list (NULL_TREE
, type
);
21437 tree tparms
= make_tree_vec (1);
21440 TREE_VEC_ELT (tparms
, 0)
21441 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21442 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21443 DEDUCE_CALL
, LOOKUP_NORMAL
,
21444 NULL
, /*explain_p=*/false);
21447 if (processing_template_decl
)
21448 /* Try again at instantiation time. */
21450 if (type
&& type
!= error_mark_node
)
21451 /* If type is error_mark_node a diagnostic must have been
21452 emitted by now. Also, having a mention to '<type error>'
21453 in the diagnostic is not really useful to the user. */
21455 if (cfun
&& auto_node
== current_function_auto_return_pattern
21456 && LAMBDA_FUNCTION_P (current_function_decl
))
21457 error ("unable to deduce lambda return type from %qE", init
);
21459 error ("unable to deduce %qT from %qE", type
, init
);
21461 return error_mark_node
;
21465 /* If the list of declarators contains more than one declarator, the type
21466 of each declared variable is determined as described above. If the
21467 type deduced for the template parameter U is not the same in each
21468 deduction, the program is ill-formed. */
21469 if (TREE_TYPE (auto_node
)
21470 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21472 if (cfun
&& auto_node
== current_function_auto_return_pattern
21473 && LAMBDA_FUNCTION_P (current_function_decl
))
21474 error ("inconsistent types %qT and %qT deduced for "
21475 "lambda return type", TREE_TYPE (auto_node
),
21476 TREE_VEC_ELT (targs
, 0));
21478 error ("inconsistent deduction for %qT: %qT and then %qT",
21479 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21480 return error_mark_node
;
21482 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21484 if (processing_template_decl
)
21485 targs
= add_to_template_args (current_template_args (), targs
);
21486 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21489 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21493 splice_late_return_type (tree type
, tree late_return_type
)
21497 if (late_return_type
== NULL_TREE
)
21499 argvec
= make_tree_vec (1);
21500 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21501 if (processing_template_parmlist
)
21502 /* For a late-specified return type in a template type-parameter, we
21503 need to add a dummy argument level for its parmlist. */
21504 argvec
= add_to_template_args
21505 (make_tree_vec (processing_template_parmlist
), argvec
);
21506 if (current_template_parms
)
21507 argvec
= add_to_template_args (current_template_args (), argvec
);
21508 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21511 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21512 'decltype(auto)'. */
21515 is_auto (const_tree type
)
21517 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21518 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21519 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21525 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21526 a use of `auto'. Returns NULL_TREE otherwise. */
21529 type_uses_auto (tree type
)
21531 return find_type_usage (type
, is_auto
);
21534 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21535 'decltype(auto)' or a concept. */
21538 is_auto_or_concept (const_tree type
)
21540 return is_auto (type
); // or concept
21543 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21544 a concept identifier) iff TYPE contains a use of a generic type. Returns
21545 NULL_TREE otherwise. */
21548 type_uses_auto_or_concept (tree type
)
21550 return find_type_usage (type
, is_auto_or_concept
);
21554 /* For a given template T, return the vector of typedefs referenced
21555 in T for which access check is needed at T instantiation time.
21556 T is either a FUNCTION_DECL or a RECORD_TYPE.
21557 Those typedefs were added to T by the function
21558 append_type_to_template_for_access_check. */
21560 vec
<qualified_typedef_usage_t
, va_gc
> *
21561 get_types_needing_access_check (tree t
)
21564 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21566 if (!t
|| t
== error_mark_node
)
21569 if (!(ti
= get_template_info (t
)))
21572 if (CLASS_TYPE_P (t
)
21573 || TREE_CODE (t
) == FUNCTION_DECL
)
21575 if (!TI_TEMPLATE (ti
))
21578 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21584 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21585 tied to T. That list of typedefs will be access checked at
21586 T instantiation time.
21587 T is either a FUNCTION_DECL or a RECORD_TYPE.
21588 TYPE_DECL is a TYPE_DECL node representing a typedef.
21589 SCOPE is the scope through which TYPE_DECL is accessed.
21590 LOCATION is the location of the usage point of TYPE_DECL.
21592 This function is a subroutine of
21593 append_type_to_template_for_access_check. */
21596 append_type_to_template_for_access_check_1 (tree t
,
21599 location_t location
)
21601 qualified_typedef_usage_t typedef_usage
;
21604 if (!t
|| t
== error_mark_node
)
21607 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21608 || CLASS_TYPE_P (t
))
21610 && TREE_CODE (type_decl
) == TYPE_DECL
21613 if (!(ti
= get_template_info (t
)))
21616 gcc_assert (TI_TEMPLATE (ti
));
21618 typedef_usage
.typedef_decl
= type_decl
;
21619 typedef_usage
.context
= scope
;
21620 typedef_usage
.locus
= location
;
21622 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21625 /* Append TYPE_DECL to the template TEMPL.
21626 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21627 At TEMPL instanciation time, TYPE_DECL will be checked to see
21628 if it can be accessed through SCOPE.
21629 LOCATION is the location of the usage point of TYPE_DECL.
21631 e.g. consider the following code snippet:
21638 template<class U> struct S
21640 C::myint mi; // <-- usage point of the typedef C::myint
21645 At S<char> instantiation time, we need to check the access of C::myint
21646 In other words, we need to check the access of the myint typedef through
21647 the C scope. For that purpose, this function will add the myint typedef
21648 and the scope C through which its being accessed to a list of typedefs
21649 tied to the template S. That list will be walked at template instantiation
21650 time and access check performed on each typedefs it contains.
21651 Note that this particular code snippet should yield an error because
21652 myint is private to C. */
21655 append_type_to_template_for_access_check (tree templ
,
21658 location_t location
)
21660 qualified_typedef_usage_t
*iter
;
21663 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21665 /* Make sure we don't append the type to the template twice. */
21666 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21667 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21670 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21674 /* Convert the generic type parameters in PARM that match the types given in the
21675 range [START_IDX, END_IDX) from the current_template_parms into generic type
21679 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21681 tree current
= current_template_parms
;
21682 int depth
= TMPL_PARMS_DEPTH (current
);
21683 current
= INNERMOST_TEMPLATE_PARMS (current
);
21684 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21686 for (int i
= 0; i
< start_idx
; ++i
)
21687 TREE_VEC_ELT (replacement
, i
)
21688 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21690 for (int i
= start_idx
; i
< end_idx
; ++i
)
21692 /* Create a distinct parameter pack type from the current parm and add it
21693 to the replacement args to tsubst below into the generic function
21696 tree o
= TREE_TYPE (TREE_VALUE
21697 (TREE_VEC_ELT (current
, i
)));
21698 tree t
= copy_type (o
);
21699 TEMPLATE_TYPE_PARM_INDEX (t
)
21700 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21702 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21703 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21704 TYPE_MAIN_VARIANT (t
) = t
;
21705 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21706 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21707 TREE_VEC_ELT (replacement
, i
) = t
;
21708 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21711 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21712 TREE_VEC_ELT (replacement
, i
)
21713 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21715 /* If there are more levels then build up the replacement with the outer
21718 replacement
= add_to_template_args (template_parms_to_args
21719 (TREE_CHAIN (current_template_parms
)),
21722 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21726 /* Set up the hash tables for template instantiations. */
21729 init_template_processing (void)
21731 decl_specializations
= htab_create_ggc (37,
21732 hash_specialization
,
21733 eq_specializations
,
21735 type_specializations
= htab_create_ggc (37,
21736 hash_specialization
,
21737 eq_specializations
,
21741 /* Print stats about the template hash tables for -fstats. */
21744 print_template_statistics (void)
21746 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21747 "%f collisions\n", (long) htab_size (decl_specializations
),
21748 (long) htab_elements (decl_specializations
),
21749 htab_collisions (decl_specializations
));
21750 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21751 "%f collisions\n", (long) htab_size (type_specializations
),
21752 (long) htab_elements (type_specializations
),
21753 htab_collisions (type_specializations
));
21756 #include "gt-cp-pt.h"