1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
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"
52 /* The type of functions taking a tree, and some additional data, and
54 typedef int (*tree_fn_t
) (tree
, void*);
56 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
57 instantiations have been deferred, either because their definitions
58 were not yet available, or because we were putting off doing the work. */
59 struct GTY ((chain_next ("%h.next"))) pending_template
{
60 struct pending_template
*next
;
61 struct tinst_level
*tinst
;
64 static GTY(()) struct pending_template
*pending_templates
;
65 static GTY(()) struct pending_template
*last_pending_template
;
67 int processing_template_parmlist
;
68 static int template_header_count
;
70 static GTY(()) tree saved_trees
;
71 static vec
<int> inline_parm_levels
;
73 static GTY(()) struct tinst_level
*current_tinst_level
;
75 static GTY(()) tree saved_access_scope
;
77 /* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80 static tree cur_stmt_expr
;
82 /* A map from local variable declarations in the body of the template
83 presently being instantiated to the corresponding instantiated
85 static struct pointer_map_t
*local_specializations
;
87 /* True if we've recursed into fn_type_unification too many times. */
88 static bool excessive_deduction_depth
;
90 typedef struct GTY(()) spec_entry
97 static GTY ((param_is (spec_entry
)))
98 htab_t decl_specializations
;
100 static GTY ((param_is (spec_entry
)))
101 htab_t type_specializations
;
103 /* Contains canonical template parameter types. The vector is indexed by
104 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
105 TREE_LIST, whose TREE_VALUEs contain the canonical template
106 parameters of various types and levels. */
107 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
109 #define UNIFY_ALLOW_NONE 0
110 #define UNIFY_ALLOW_MORE_CV_QUAL 1
111 #define UNIFY_ALLOW_LESS_CV_QUAL 2
112 #define UNIFY_ALLOW_DERIVED 4
113 #define UNIFY_ALLOW_INTEGER 8
114 #define UNIFY_ALLOW_OUTER_LEVEL 16
115 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
116 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
118 enum template_base_result
{
120 tbr_ambiguous_baseclass
,
124 static void push_access_scope (tree
);
125 static void pop_access_scope (tree
);
126 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
127 unification_kind_t
, int,
129 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
130 unification_kind_t
, int, bool, bool);
131 static int unify (tree
, tree
, tree
, tree
, int, bool);
132 static void add_pending_template (tree
);
133 static tree
reopen_tinst_level (struct tinst_level
*);
134 static tree
tsubst_initializer_list (tree
, tree
);
135 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
136 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
138 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
140 static void tsubst_enum (tree
, tree
, tree
);
141 static tree
add_to_template_args (tree
, tree
);
142 static tree
add_outermost_template_args (tree
, tree
);
143 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
144 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
146 static int type_unification_real (tree
, tree
, tree
, const tree
*,
147 unsigned int, int, unification_kind_t
, int,
148 vec
<deferred_access_check
, va_gc
> **,
150 static void note_template_header (int);
151 static tree
convert_nontype_argument_function (tree
, tree
);
152 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
153 static tree
convert_template_argument (tree
, tree
, tree
,
154 tsubst_flags_t
, int, tree
);
155 static int for_each_template_parm (tree
, tree_fn_t
, void*,
156 struct pointer_set_t
*, bool);
157 static tree
expand_template_argument_pack (tree
);
158 static tree
build_template_parm_index (int, int, int, tree
, tree
);
159 static bool inline_needs_template_parms (tree
, bool);
160 static void push_inline_template_parms_recursive (tree
, int);
161 static tree
retrieve_local_specialization (tree
);
162 static void register_local_specialization (tree
, tree
);
163 static hashval_t
hash_specialization (const void *p
);
164 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
165 static int mark_template_parm (tree
, void *);
166 static int template_parm_this_level_p (tree
, void *);
167 static tree
tsubst_friend_function (tree
, tree
);
168 static tree
tsubst_friend_class (tree
, tree
);
169 static int can_complete_type_without_circularity (tree
);
170 static tree
get_bindings (tree
, tree
, tree
, bool);
171 static int template_decl_level (tree
);
172 static int check_cv_quals_for_unify (int, tree
, tree
);
173 static void template_parm_level_and_index (tree
, int*, int*);
174 static int unify_pack_expansion (tree
, tree
, tree
,
175 tree
, unification_kind_t
, bool, bool);
176 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
178 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
179 static void regenerate_decl_from_template (tree
, tree
);
180 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
181 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
182 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
184 static bool check_specialization_scope (void);
185 static tree
process_partial_specialization (tree
);
186 static void set_current_access_from_decl (tree
);
187 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
189 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
190 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
192 static bool template_template_parm_bindings_ok_p (tree
, tree
);
193 static int template_args_equal (tree
, tree
);
194 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
195 static tree
for_each_template_parm_r (tree
*, int *, void *);
196 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
197 static void copy_default_args_to_explicit_spec (tree
);
198 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
199 static bool dependent_template_arg_p (tree
);
200 static bool any_template_arguments_need_structural_equality_p (tree
);
201 static bool dependent_type_p_r (tree
);
202 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
203 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
204 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
205 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
206 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
207 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
209 static tree
listify (tree
);
210 static tree
listify_autos (tree
, tree
);
211 static tree
template_parm_to_arg (tree t
);
212 static tree
current_template_args (void);
213 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
214 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
216 /* Make the current scope suitable for access checking when we are
217 processing T. T can be FUNCTION_DECL for instantiated function
218 template, VAR_DECL for static member variable, or TYPE_DECL for
219 alias template (needed by instantiate_decl). */
222 push_access_scope (tree t
)
224 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
225 || TREE_CODE (t
) == TYPE_DECL
);
227 if (DECL_FRIEND_CONTEXT (t
))
228 push_nested_class (DECL_FRIEND_CONTEXT (t
));
229 else if (DECL_CLASS_SCOPE_P (t
))
230 push_nested_class (DECL_CONTEXT (t
));
232 push_to_top_level ();
234 if (TREE_CODE (t
) == FUNCTION_DECL
)
236 saved_access_scope
= tree_cons
237 (NULL_TREE
, current_function_decl
, saved_access_scope
);
238 current_function_decl
= t
;
242 /* Restore the scope set up by push_access_scope. T is the node we
246 pop_access_scope (tree t
)
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 current_function_decl
= TREE_VALUE (saved_access_scope
);
251 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
254 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
257 pop_from_top_level ();
260 /* Do any processing required when DECL (a member template
261 declaration) is finished. Returns the TEMPLATE_DECL corresponding
262 to DECL, unless it is a specialization, in which case the DECL
263 itself is returned. */
266 finish_member_template_decl (tree decl
)
268 if (decl
== error_mark_node
)
269 return error_mark_node
;
271 gcc_assert (DECL_P (decl
));
273 if (TREE_CODE (decl
) == TYPE_DECL
)
277 type
= TREE_TYPE (decl
);
278 if (type
== error_mark_node
)
279 return error_mark_node
;
280 if (MAYBE_CLASS_TYPE_P (type
)
281 && CLASSTYPE_TEMPLATE_INFO (type
)
282 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
284 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
285 check_member_template (tmpl
);
290 else if (TREE_CODE (decl
) == FIELD_DECL
)
291 error ("data member %qD cannot be a member template", decl
);
292 else if (DECL_TEMPLATE_INFO (decl
))
294 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
296 check_member_template (DECL_TI_TEMPLATE (decl
));
297 return DECL_TI_TEMPLATE (decl
);
303 error ("invalid member template declaration %qD", decl
);
305 return error_mark_node
;
308 /* Create a template info node. */
311 build_template_info (tree template_decl
, tree template_args
)
313 tree result
= make_node (TEMPLATE_INFO
);
314 TI_TEMPLATE (result
) = template_decl
;
315 TI_ARGS (result
) = template_args
;
319 /* Return the template info node corresponding to T, whatever T is. */
322 get_template_info (const_tree t
)
324 tree tinfo
= NULL_TREE
;
326 if (!t
|| t
== error_mark_node
)
329 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
330 tinfo
= DECL_TEMPLATE_INFO (t
);
332 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
335 if (OVERLOAD_TYPE_P (t
))
336 tinfo
= TYPE_TEMPLATE_INFO (t
);
337 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
338 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
343 /* Returns the template nesting level of the indicated class TYPE.
353 A<T>::B<U> has depth two, while A<T> has depth one.
354 Both A<T>::B<int> and A<int>::B<U> have depth one, if
355 they are instantiations, not specializations.
357 This function is guaranteed to return 0 if passed NULL_TREE so
358 that, for example, `template_class_depth (current_class_type)' is
362 template_class_depth (tree type
)
367 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
368 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
369 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
371 tree tinfo
= get_template_info (type
);
373 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
374 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
381 /* Subroutine of maybe_begin_member_template_processing.
382 Returns true if processing DECL needs us to push template parms. */
385 inline_needs_template_parms (tree decl
, bool nsdmi
)
387 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
390 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
391 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
394 /* Subroutine of maybe_begin_member_template_processing.
395 Push the template parms in PARMS, starting from LEVELS steps into the
396 chain, and ending at the beginning, since template parms are listed
400 push_inline_template_parms_recursive (tree parmlist
, int levels
)
402 tree parms
= TREE_VALUE (parmlist
);
406 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
408 ++processing_template_decl
;
409 current_template_parms
410 = tree_cons (size_int (processing_template_decl
),
411 parms
, current_template_parms
);
412 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
414 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
416 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
418 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
420 if (parm
== error_mark_node
)
423 gcc_assert (DECL_P (parm
));
425 switch (TREE_CODE (parm
))
434 /* Make a CONST_DECL as is done in process_template_parm.
435 It is ugly that we recreate this here; the original
436 version built in process_template_parm is no longer
438 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
439 CONST_DECL
, DECL_NAME (parm
),
441 DECL_ARTIFICIAL (decl
) = 1;
442 TREE_CONSTANT (decl
) = 1;
443 TREE_READONLY (decl
) = 1;
444 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
445 SET_DECL_TEMPLATE_PARM_P (decl
);
456 /* Restore the template parameter context for a member template, a
457 friend template defined in a class definition, or a non-template
458 member of template class. */
461 maybe_begin_member_template_processing (tree decl
)
465 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
468 decl
= (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
469 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl
))
472 if (inline_needs_template_parms (decl
, nsdmi
))
474 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
475 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
477 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
480 parms
= TREE_CHAIN (parms
);
483 push_inline_template_parms_recursive (parms
, levels
);
486 /* Remember how many levels of template parameters we pushed so that
487 we can pop them later. */
488 inline_parm_levels
.safe_push (levels
);
491 /* Undo the effects of maybe_begin_member_template_processing. */
494 maybe_end_member_template_processing (void)
499 if (inline_parm_levels
.length () == 0)
502 last
= inline_parm_levels
.pop ();
503 for (i
= 0; i
< last
; ++i
)
505 --processing_template_decl
;
506 current_template_parms
= TREE_CHAIN (current_template_parms
);
511 /* Return a new template argument vector which contains all of ARGS,
512 but has as its innermost set of arguments the EXTRA_ARGS. */
515 add_to_template_args (tree args
, tree extra_args
)
522 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
525 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
526 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
528 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
529 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
531 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
532 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
537 /* Like add_to_template_args, but only the outermost ARGS are added to
538 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
539 (EXTRA_ARGS) levels are added. This function is used to combine
540 the template arguments from a partial instantiation with the
541 template arguments used to attain the full instantiation from the
542 partial instantiation. */
545 add_outermost_template_args (tree args
, tree extra_args
)
549 /* If there are more levels of EXTRA_ARGS than there are ARGS,
550 something very fishy is going on. */
551 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
553 /* If *all* the new arguments will be the EXTRA_ARGS, just return
555 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
558 /* For the moment, we make ARGS look like it contains fewer levels. */
559 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
561 new_args
= add_to_template_args (args
, extra_args
);
563 /* Now, we restore ARGS to its full dimensions. */
564 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
569 /* Return the N levels of innermost template arguments from the ARGS. */
572 get_innermost_template_args (tree args
, int n
)
580 /* If N is 1, just return the innermost set of template arguments. */
582 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
584 /* If we're not removing anything, just return the arguments we were
586 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
587 gcc_assert (extra_levels
>= 0);
588 if (extra_levels
== 0)
591 /* Make a new set of arguments, not containing the outer arguments. */
592 new_args
= make_tree_vec (n
);
593 for (i
= 1; i
<= n
; ++i
)
594 SET_TMPL_ARGS_LEVEL (new_args
, i
,
595 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
600 /* The inverse of get_innermost_template_args: Return all but the innermost
601 EXTRA_LEVELS levels of template arguments from the ARGS. */
604 strip_innermost_template_args (tree args
, int extra_levels
)
607 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
612 /* If N is 1, just return the outermost set of template arguments. */
614 return TMPL_ARGS_LEVEL (args
, 1);
616 /* If we're not removing anything, just return the arguments we were
618 gcc_assert (extra_levels
>= 0);
619 if (extra_levels
== 0)
622 /* Make a new set of arguments, not containing the inner arguments. */
623 new_args
= make_tree_vec (n
);
624 for (i
= 1; i
<= n
; ++i
)
625 SET_TMPL_ARGS_LEVEL (new_args
, i
,
626 TMPL_ARGS_LEVEL (args
, i
));
631 /* We've got a template header coming up; push to a new level for storing
635 begin_template_parm_list (void)
637 /* We use a non-tag-transparent scope here, which causes pushtag to
638 put tags in this scope, rather than in the enclosing class or
639 namespace scope. This is the right thing, since we want
640 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
641 global template class, push_template_decl handles putting the
642 TEMPLATE_DECL into top-level scope. For a nested template class,
645 template <class T> struct S1 {
646 template <class T> struct S2 {};
649 pushtag contains special code to call pushdecl_with_scope on the
650 TEMPLATE_DECL for S2. */
651 begin_scope (sk_template_parms
, NULL
);
652 ++processing_template_decl
;
653 ++processing_template_parmlist
;
654 note_template_header (0);
657 /* This routine is called when a specialization is declared. If it is
658 invalid to declare a specialization here, an error is reported and
659 false is returned, otherwise this routine will return true. */
662 check_specialization_scope (void)
664 tree scope
= current_scope ();
668 An explicit specialization shall be declared in the namespace of
669 which the template is a member, or, for member templates, in the
670 namespace of which the enclosing class or enclosing class
671 template is a member. An explicit specialization of a member
672 function, member class or static data member of a class template
673 shall be declared in the namespace of which the class template
675 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
677 error ("explicit specialization in non-namespace scope %qD", scope
);
683 In an explicit specialization declaration for a member of a class
684 template or a member template that appears in namespace scope,
685 the member template and some of its enclosing class templates may
686 remain unspecialized, except that the declaration shall not
687 explicitly specialize a class member template if its enclosing
688 class templates are not explicitly specialized as well. */
689 if (current_template_parms
)
691 error ("enclosing class templates are not explicitly specialized");
698 /* We've just seen template <>. */
701 begin_specialization (void)
703 begin_scope (sk_template_spec
, NULL
);
704 note_template_header (1);
705 return check_specialization_scope ();
708 /* Called at then end of processing a declaration preceded by
712 end_specialization (void)
715 reset_specialization ();
718 /* Any template <>'s that we have seen thus far are not referring to a
719 function specialization. */
722 reset_specialization (void)
724 processing_specialization
= 0;
725 template_header_count
= 0;
728 /* We've just seen a template header. If SPECIALIZATION is nonzero,
729 it was of the form template <>. */
732 note_template_header (int specialization
)
734 processing_specialization
= specialization
;
735 template_header_count
++;
738 /* We're beginning an explicit instantiation. */
741 begin_explicit_instantiation (void)
743 gcc_assert (!processing_explicit_instantiation
);
744 processing_explicit_instantiation
= true;
749 end_explicit_instantiation (void)
751 gcc_assert (processing_explicit_instantiation
);
752 processing_explicit_instantiation
= false;
755 /* An explicit specialization or partial specialization of TMPL is being
756 declared. Check that the namespace in which the specialization is
757 occurring is permissible. Returns false iff it is invalid to
758 specialize TMPL in the current namespace. */
761 check_specialization_namespace (tree tmpl
)
763 tree tpl_ns
= decl_namespace_context (tmpl
);
767 An explicit specialization shall be declared in the namespace of
768 which the template is a member, or, for member templates, in the
769 namespace of which the enclosing class or enclosing class
770 template is a member. An explicit specialization of a member
771 function, member class or static data member of a class template
772 shall be declared in the namespace of which the class template is
774 if (current_scope() != DECL_CONTEXT (tmpl
)
775 && !at_namespace_scope_p ())
777 error ("specialization of %qD must appear at namespace scope", tmpl
);
780 if (is_associated_namespace (current_namespace
, tpl_ns
))
781 /* Same or super-using namespace. */
785 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
786 permerror (input_location
, " from definition of %q+#D", tmpl
);
791 /* SPEC is an explicit instantiation. Check that it is valid to
792 perform this explicit instantiation in the current namespace. */
795 check_explicit_instantiation_namespace (tree spec
)
799 /* DR 275: An explicit instantiation shall appear in an enclosing
800 namespace of its template. */
801 ns
= decl_namespace_context (spec
);
802 if (!is_ancestor (current_namespace
, ns
))
803 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
804 "(which does not enclose namespace %qD)",
805 spec
, current_namespace
, ns
);
808 /* The TYPE is being declared. If it is a template type, that means it
809 is a partial specialization. Do appropriate error-checking. */
812 maybe_process_partial_specialization (tree type
)
816 if (type
== error_mark_node
)
817 return error_mark_node
;
819 /* A lambda that appears in specialization context is not itself a
821 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
824 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
826 error ("name of class shadows template template parameter %qD",
828 return error_mark_node
;
831 context
= TYPE_CONTEXT (type
);
833 if (TYPE_ALIAS_P (type
))
835 if (TYPE_TEMPLATE_INFO (type
)
836 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
837 error ("specialization of alias template %qD",
838 TYPE_TI_TEMPLATE (type
));
840 error ("explicit specialization of non-template %qT", type
);
841 return error_mark_node
;
843 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
845 /* This is for ordinary explicit specialization and partial
846 specialization of a template class such as:
848 template <> class C<int>;
852 template <class T> class C<T*>;
854 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
856 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
857 && !COMPLETE_TYPE_P (type
))
859 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
860 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
861 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
862 if (processing_template_decl
)
864 if (push_template_decl (TYPE_MAIN_DECL (type
))
866 return error_mark_node
;
869 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
870 error ("specialization of %qT after instantiation", type
);
871 else if (errorcount
&& !processing_specialization
872 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
873 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
874 /* Trying to define a specialization either without a template<> header
875 or in an inappropriate place. We've already given an error, so just
876 bail now so we don't actually define the specialization. */
877 return error_mark_node
;
879 else if (CLASS_TYPE_P (type
)
880 && !CLASSTYPE_USE_TEMPLATE (type
)
881 && CLASSTYPE_TEMPLATE_INFO (type
)
882 && context
&& CLASS_TYPE_P (context
)
883 && CLASSTYPE_TEMPLATE_INFO (context
))
885 /* This is for an explicit specialization of member class
886 template according to [temp.expl.spec/18]:
888 template <> template <class U> class C<int>::D;
890 The context `C<int>' must be an implicit instantiation.
891 Otherwise this is just a member class template declared
894 template <> class C<int> { template <class U> class D; };
895 template <> template <class U> class C<int>::D;
897 In the first case, `C<int>::D' is a specialization of `C<T>::D'
898 while in the second case, `C<int>::D' is a primary template
899 and `C<T>::D' may not exist. */
901 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
902 && !COMPLETE_TYPE_P (type
))
905 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
907 if (current_namespace
908 != decl_namespace_context (tmpl
))
910 permerror (input_location
, "specializing %q#T in different namespace", type
);
911 permerror (input_location
, " from definition of %q+#D", tmpl
);
914 /* Check for invalid specialization after instantiation:
916 template <> template <> class C<int>::D<int>;
917 template <> template <class U> class C<int>::D; */
919 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
920 t
; t
= TREE_CHAIN (t
))
922 tree inst
= TREE_VALUE (t
);
923 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
925 /* We already have a full specialization of this partial
926 instantiation. Reassign it to the new member
927 specialization template. */
932 elt
.tmpl
= most_general_template (tmpl
);
933 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
936 htab_remove_elt (type_specializations
, &elt
);
939 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
941 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
942 entry
= ggc_alloc_spec_entry ();
946 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
947 /* But if we've had an implicit instantiation, that's a
948 problem ([temp.expl.spec]/6). */
949 error ("specialization %qT after instantiation %qT",
953 /* Mark TYPE as a specialization. And as a result, we only
954 have one level of template argument for the innermost
956 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
957 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
958 CLASSTYPE_TI_ARGS (type
)
959 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
962 else if (processing_specialization
)
964 /* Someday C++0x may allow for enum template specialization. */
965 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
966 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
967 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
968 "of %qD not allowed by ISO C++", type
);
971 error ("explicit specialization of non-template %qT", type
);
972 return error_mark_node
;
979 /* Returns nonzero if we can optimize the retrieval of specializations
980 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
981 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
984 optimize_specialization_lookup_p (tree tmpl
)
986 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
987 && DECL_CLASS_SCOPE_P (tmpl
)
988 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
990 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
991 /* The optimized lookup depends on the fact that the
992 template arguments for the member function template apply
993 purely to the containing class, which is not true if the
994 containing class is an explicit or partial
996 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
997 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
998 && !DECL_CONV_FN_P (tmpl
)
999 /* It is possible to have a template that is not a member
1000 template and is not a member of a template class:
1002 template <typename T>
1003 struct S { friend A::f(); };
1005 Here, the friend function is a template, but the context does
1006 not have template information. The optimized lookup relies
1007 on having ARGS be the template arguments for both the class
1008 and the function template. */
1009 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1012 /* Retrieve the specialization (in the sense of [temp.spec] - a
1013 specialization is either an instantiation or an explicit
1014 specialization) of TMPL for the given template ARGS. If there is
1015 no such specialization, return NULL_TREE. The ARGS are a vector of
1016 arguments, or a vector of vectors of arguments, in the case of
1017 templates with more than one level of parameters.
1019 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1020 then we search for a partial specialization matching ARGS. This
1021 parameter is ignored if TMPL is not a class template.
1023 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1024 result is a NONTYPE_ARGUMENT_PACK. */
1027 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1029 if (tmpl
== NULL_TREE
)
1032 if (args
== error_mark_node
)
1035 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1036 || TREE_CODE (tmpl
) == FIELD_DECL
);
1038 /* There should be as many levels of arguments as there are
1039 levels of parameters. */
1040 gcc_assert (TMPL_ARGS_DEPTH (args
)
1041 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1042 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1043 : template_class_depth (DECL_CONTEXT (tmpl
))));
1045 if (optimize_specialization_lookup_p (tmpl
))
1047 tree class_template
;
1048 tree class_specialization
;
1049 vec
<tree
, va_gc
> *methods
;
1053 /* The template arguments actually apply to the containing
1054 class. Find the class specialization with those
1056 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1057 class_specialization
1058 = retrieve_specialization (class_template
, args
, 0);
1059 if (!class_specialization
)
1061 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1062 for the specialization. */
1063 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1066 /* Iterate through the methods with the indicated name, looking
1067 for the one that has an instance of TMPL. */
1068 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1069 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1071 tree fn
= OVL_CURRENT (fns
);
1072 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1073 /* using-declarations can add base methods to the method vec,
1074 and we don't want those here. */
1075 && DECL_CONTEXT (fn
) == class_specialization
)
1084 htab_t specializations
;
1088 elt
.spec
= NULL_TREE
;
1090 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1091 specializations
= type_specializations
;
1093 specializations
= decl_specializations
;
1096 hash
= hash_specialization (&elt
);
1097 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1105 /* Like retrieve_specialization, but for local declarations. */
1108 retrieve_local_specialization (tree tmpl
)
1112 if (local_specializations
== NULL
)
1115 slot
= pointer_map_contains (local_specializations
, tmpl
);
1116 return slot
? (tree
) *slot
: NULL_TREE
;
1119 /* Returns nonzero iff DECL is a specialization of TMPL. */
1122 is_specialization_of (tree decl
, tree tmpl
)
1126 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1130 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1136 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1138 for (t
= TREE_TYPE (decl
);
1140 t
= CLASSTYPE_USE_TEMPLATE (t
)
1141 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1142 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1149 /* Returns nonzero iff DECL is a specialization of friend declaration
1150 FRIEND_DECL according to [temp.friend]. */
1153 is_specialization_of_friend (tree decl
, tree friend_decl
)
1155 bool need_template
= true;
1158 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1159 || TREE_CODE (decl
) == TYPE_DECL
);
1161 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1162 of a template class, we want to check if DECL is a specialization
1164 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1165 && DECL_TEMPLATE_INFO (friend_decl
)
1166 && !DECL_USE_TEMPLATE (friend_decl
))
1168 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1169 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1170 need_template
= false;
1172 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1173 && !PRIMARY_TEMPLATE_P (friend_decl
))
1174 need_template
= false;
1176 /* There is nothing to do if this is not a template friend. */
1177 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1180 if (is_specialization_of (decl
, friend_decl
))
1184 A member of a class template may be declared to be a friend of a
1185 non-template class. In this case, the corresponding member of
1186 every specialization of the class template is a friend of the
1187 class granting friendship.
1189 For example, given a template friend declaration
1191 template <class T> friend void A<T>::f();
1193 the member function below is considered a friend
1195 template <> struct A<int> {
1199 For this type of template friend, TEMPLATE_DEPTH below will be
1200 nonzero. To determine if DECL is a friend of FRIEND, we first
1201 check if the enclosing class is a specialization of another. */
1203 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1205 && DECL_CLASS_SCOPE_P (decl
)
1206 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1207 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1209 /* Next, we check the members themselves. In order to handle
1210 a few tricky cases, such as when FRIEND_DECL's are
1212 template <class T> friend void A<T>::g(T t);
1213 template <class T> template <T t> friend void A<T>::h();
1217 void A<int>::g(int);
1218 template <int> void A<int>::h();
1220 we need to figure out ARGS, the template arguments from
1221 the context of DECL. This is required for template substitution
1222 of `T' in the function parameter of `g' and template parameter
1223 of `h' in the above examples. Here ARGS corresponds to `int'. */
1225 tree context
= DECL_CONTEXT (decl
);
1226 tree args
= NULL_TREE
;
1227 int current_depth
= 0;
1229 while (current_depth
< template_depth
)
1231 if (CLASSTYPE_TEMPLATE_INFO (context
))
1233 if (current_depth
== 0)
1234 args
= TYPE_TI_ARGS (context
);
1236 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1239 context
= TYPE_CONTEXT (context
);
1242 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1247 tree friend_args_type
;
1248 tree decl_args_type
;
1250 /* Make sure that both DECL and FRIEND_DECL are templates or
1252 is_template
= DECL_TEMPLATE_INFO (decl
)
1253 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1254 if (need_template
^ is_template
)
1256 else if (is_template
)
1258 /* If both are templates, check template parameter list. */
1260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1262 if (!comp_template_parms
1263 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1267 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1270 decl_type
= TREE_TYPE (decl
);
1272 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1273 tf_none
, NULL_TREE
);
1274 if (friend_type
== error_mark_node
)
1277 /* Check if return types match. */
1278 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1281 /* Check if function parameter types match, ignoring the
1282 `this' parameter. */
1283 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1284 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1285 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1286 friend_args_type
= TREE_CHAIN (friend_args_type
);
1287 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1288 decl_args_type
= TREE_CHAIN (decl_args_type
);
1290 return compparms (decl_args_type
, friend_args_type
);
1294 /* DECL is a TYPE_DECL */
1296 tree decl_type
= TREE_TYPE (decl
);
1298 /* Make sure that both DECL and FRIEND_DECL are templates or
1301 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1302 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1304 if (need_template
^ is_template
)
1306 else if (is_template
)
1309 /* If both are templates, check the name of the two
1310 TEMPLATE_DECL's first because is_friend didn't. */
1311 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1312 != DECL_NAME (friend_decl
))
1315 /* Now check template parameter list. */
1317 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1319 return comp_template_parms
1320 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1324 return (DECL_NAME (decl
)
1325 == DECL_NAME (friend_decl
));
1331 /* Register the specialization SPEC as a specialization of TMPL with
1332 the indicated ARGS. IS_FRIEND indicates whether the specialization
1333 is actually just a friend declaration. Returns SPEC, or an
1334 equivalent prior declaration, if available.
1336 We also store instantiations of field packs in the hash table, even
1337 though they are not themselves templates, to make lookup easier. */
1340 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1347 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1348 || (TREE_CODE (tmpl
) == FIELD_DECL
1349 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1351 if (TREE_CODE (spec
) == FUNCTION_DECL
1352 && uses_template_parms (DECL_TI_ARGS (spec
)))
1353 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1354 register it; we want the corresponding TEMPLATE_DECL instead.
1355 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1356 the more obvious `uses_template_parms (spec)' to avoid problems
1357 with default function arguments. In particular, given
1358 something like this:
1360 template <class T> void f(T t1, T t = T())
1362 the default argument expression is not substituted for in an
1363 instantiation unless and until it is actually needed. */
1366 if (optimize_specialization_lookup_p (tmpl
))
1367 /* We don't put these specializations in the hash table, but we might
1368 want to give an error about a mismatch. */
1369 fn
= retrieve_specialization (tmpl
, args
, 0);
1377 hash
= hash_specialization (&elt
);
1380 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1382 fn
= ((spec_entry
*) *slot
)->spec
;
1387 /* We can sometimes try to re-register a specialization that we've
1388 already got. In particular, regenerate_decl_from_template calls
1389 duplicate_decls which will update the specialization list. But,
1390 we'll still get called again here anyhow. It's more convenient
1391 to simply allow this than to try to prevent it. */
1394 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1396 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1398 if (DECL_ODR_USED (fn
)
1399 || DECL_EXPLICIT_INSTANTIATION (fn
))
1401 error ("specialization of %qD after instantiation",
1403 return error_mark_node
;
1408 /* This situation should occur only if the first
1409 specialization is an implicit instantiation, the
1410 second is an explicit specialization, and the
1411 implicit instantiation has not yet been used. That
1412 situation can occur if we have implicitly
1413 instantiated a member function and then specialized
1416 We can also wind up here if a friend declaration that
1417 looked like an instantiation turns out to be a
1420 template <class T> void foo(T);
1421 class S { friend void foo<>(int) };
1422 template <> void foo(int);
1424 We transform the existing DECL in place so that any
1425 pointers to it become pointers to the updated
1428 If there was a definition for the template, but not
1429 for the specialization, we want this to look as if
1430 there were no definition, and vice versa. */
1431 DECL_INITIAL (fn
) = NULL_TREE
;
1432 duplicate_decls (spec
, fn
, is_friend
);
1433 /* The call to duplicate_decls will have applied
1436 An explicit specialization of a function template
1437 is inline only if it is explicitly declared to be,
1438 and independently of whether its function template
1441 to the primary function; now copy the inline bits to
1442 the various clones. */
1443 FOR_EACH_CLONE (clone
, fn
)
1445 DECL_DECLARED_INLINE_P (clone
)
1446 = DECL_DECLARED_INLINE_P (fn
);
1447 DECL_SOURCE_LOCATION (clone
)
1448 = DECL_SOURCE_LOCATION (fn
);
1450 check_specialization_namespace (tmpl
);
1455 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1457 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1458 /* Dup decl failed, but this is a new definition. Set the
1459 line number so any errors match this new
1461 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1467 return duplicate_decls (spec
, fn
, is_friend
);
1469 /* A specialization must be declared in the same namespace as the
1470 template it is specializing. */
1471 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1472 && !check_specialization_namespace (tmpl
))
1473 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1475 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1477 spec_entry
*entry
= ggc_alloc_spec_entry ();
1478 gcc_assert (tmpl
&& args
&& spec
);
1481 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1482 && PRIMARY_TEMPLATE_P (tmpl
)
1483 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1484 /* TMPL is a forward declaration of a template function; keep a list
1485 of all specializations in case we need to reassign them to a friend
1486 template later in tsubst_friend_function. */
1487 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1488 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1494 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1495 TMPL and ARGS members, ignores SPEC. */
1497 int comparing_specializations
;
1500 eq_specializations (const void *p1
, const void *p2
)
1502 const spec_entry
*e1
= (const spec_entry
*)p1
;
1503 const spec_entry
*e2
= (const spec_entry
*)p2
;
1506 ++comparing_specializations
;
1507 equal
= (e1
->tmpl
== e2
->tmpl
1508 && comp_template_args (e1
->args
, e2
->args
));
1509 --comparing_specializations
;
1514 /* Returns a hash for a template TMPL and template arguments ARGS. */
1517 hash_tmpl_and_args (tree tmpl
, tree args
)
1519 hashval_t val
= DECL_UID (tmpl
);
1520 return iterative_hash_template_arg (args
, val
);
1523 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1527 hash_specialization (const void *p
)
1529 const spec_entry
*e
= (const spec_entry
*)p
;
1530 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1533 /* Recursively calculate a hash value for a template argument ARG, for use
1534 in the hash tables of template specializations. */
1537 iterative_hash_template_arg (tree arg
, hashval_t val
)
1539 unsigned HOST_WIDE_INT i
;
1540 enum tree_code code
;
1543 if (arg
== NULL_TREE
)
1544 return iterative_hash_object (arg
, val
);
1549 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1550 /* We can get one of these when re-hashing a previous entry in the middle
1551 of substituting into a pack expansion. Just look through it. */
1552 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1554 code
= TREE_CODE (arg
);
1555 tclass
= TREE_CODE_CLASS (code
);
1557 val
= iterative_hash_object (code
, val
);
1564 case IDENTIFIER_NODE
:
1565 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1569 int i
, len
= TREE_VEC_LENGTH (arg
);
1570 for (i
= 0; i
< len
; ++i
)
1571 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1575 case TYPE_PACK_EXPANSION
:
1576 case EXPR_PACK_EXPANSION
:
1577 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1578 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1580 case TYPE_ARGUMENT_PACK
:
1581 case NONTYPE_ARGUMENT_PACK
:
1582 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1585 for (; arg
; arg
= TREE_CHAIN (arg
))
1586 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1590 for (; arg
; arg
= OVL_NEXT (arg
))
1591 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1597 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1599 val
= iterative_hash_template_arg (field
, val
);
1600 val
= iterative_hash_template_arg (value
, val
);
1606 if (!DECL_ARTIFICIAL (arg
))
1608 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1609 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1611 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1614 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1617 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1618 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1620 case TEMPLATE_PARM_INDEX
:
1621 val
= iterative_hash_template_arg
1622 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1623 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1624 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1627 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1628 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1629 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1632 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1634 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1638 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1639 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1640 val
= iterative_hash_object (code
, val
);
1641 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1644 /* A lambda can't appear in a template arg, but don't crash on
1646 gcc_assert (seen_error ());
1650 case IMPLICIT_CONV_EXPR
:
1651 case STATIC_CAST_EXPR
:
1652 case REINTERPRET_CAST_EXPR
:
1653 case CONST_CAST_EXPR
:
1654 case DYNAMIC_CAST_EXPR
:
1656 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1657 /* Now hash operands as usual. */
1667 if (TYPE_CANONICAL (arg
))
1668 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1670 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1671 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1672 /* Otherwise just compare the types during lookup. */
1675 case tcc_declaration
:
1677 return iterative_hash_expr (arg
, val
);
1680 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1682 unsigned n
= cp_tree_operand_length (arg
);
1683 for (i
= 0; i
< n
; ++i
)
1684 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1692 /* Unregister the specialization SPEC as a specialization of TMPL.
1693 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1694 if the SPEC was listed as a specialization of TMPL.
1696 Note that SPEC has been ggc_freed, so we can't look inside it. */
1699 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1704 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1705 elt
.args
= TI_ARGS (tinfo
);
1706 elt
.spec
= NULL_TREE
;
1708 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1711 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1712 gcc_assert (new_spec
!= NULL_TREE
);
1713 entry
->spec
= new_spec
;
1720 /* Like register_specialization, but for local declarations. We are
1721 registering SPEC, an instantiation of TMPL. */
1724 register_local_specialization (tree spec
, tree tmpl
)
1728 slot
= pointer_map_insert (local_specializations
, tmpl
);
1732 /* TYPE is a class type. Returns true if TYPE is an explicitly
1733 specialized class. */
1736 explicit_class_specialization_p (tree type
)
1738 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1740 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1743 /* Print the list of functions at FNS, going through all the overloads
1744 for each element of the list. Alternatively, FNS can not be a
1745 TREE_LIST, in which case it will be printed together with all the
1748 MORE and *STR should respectively be FALSE and NULL when the function
1749 is called from the outside. They are used internally on recursive
1750 calls. print_candidates manages the two parameters and leaves NULL
1751 in *STR when it ends. */
1754 print_candidates_1 (tree fns
, bool more
, const char **str
)
1757 char *spaces
= NULL
;
1759 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1760 if (TREE_CODE (fn
) == TREE_LIST
)
1762 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1763 print_candidates_1 (TREE_VALUE (fn2
),
1764 TREE_CHAIN (fn2
) || more
, str
);
1768 tree cand
= OVL_CURRENT (fn
);
1771 /* Pick the prefix string. */
1772 if (!more
&& !OVL_NEXT (fns
))
1774 inform (DECL_SOURCE_LOCATION (cand
),
1775 "candidate is: %#D", cand
);
1779 *str
= _("candidates are:");
1780 spaces
= get_spaces (*str
);
1782 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1783 *str
= spaces
? spaces
: *str
;
1793 /* Print the list of candidate FNS in an error message. FNS can also
1794 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1797 print_candidates (tree fns
)
1799 const char *str
= NULL
;
1800 print_candidates_1 (fns
, false, &str
);
1801 gcc_assert (str
== NULL
);
1804 /* Returns the template (one of the functions given by TEMPLATE_ID)
1805 which can be specialized to match the indicated DECL with the
1806 explicit template args given in TEMPLATE_ID. The DECL may be
1807 NULL_TREE if none is available. In that case, the functions in
1808 TEMPLATE_ID are non-members.
1810 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1811 specialization of a member template.
1813 The TEMPLATE_COUNT is the number of references to qualifying
1814 template classes that appeared in the name of the function. See
1815 check_explicit_specialization for a more accurate description.
1817 TSK indicates what kind of template declaration (if any) is being
1818 declared. TSK_TEMPLATE indicates that the declaration given by
1819 DECL, though a FUNCTION_DECL, has template parameters, and is
1820 therefore a template function.
1822 The template args (those explicitly specified and those deduced)
1823 are output in a newly created vector *TARGS_OUT.
1825 If it is impossible to determine the result, an error message is
1826 issued. The error_mark_node is returned to indicate failure. */
1829 determine_specialization (tree template_id
,
1832 int need_member_template
,
1838 tree explicit_targs
;
1839 tree candidates
= NULL_TREE
;
1840 /* A TREE_LIST of templates of which DECL may be a specialization.
1841 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1842 corresponding TREE_PURPOSE is the set of template arguments that,
1843 when used to instantiate the template, would produce a function
1844 with the signature of DECL. */
1845 tree templates
= NULL_TREE
;
1847 cp_binding_level
*b
;
1849 *targs_out
= NULL_TREE
;
1851 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1852 return error_mark_node
;
1854 /* We shouldn't be specializing a member template of an
1855 unspecialized class template; we already gave an error in
1856 check_specialization_scope, now avoid crashing. */
1857 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1858 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1860 gcc_assert (errorcount
);
1861 return error_mark_node
;
1864 fns
= TREE_OPERAND (template_id
, 0);
1865 explicit_targs
= TREE_OPERAND (template_id
, 1);
1867 if (fns
== error_mark_node
)
1868 return error_mark_node
;
1870 /* Check for baselinks. */
1871 if (BASELINK_P (fns
))
1872 fns
= BASELINK_FUNCTIONS (fns
);
1874 if (!is_overloaded_fn (fns
))
1876 error ("%qD is not a function template", fns
);
1877 return error_mark_node
;
1880 /* Count the number of template headers specified for this
1883 for (b
= current_binding_level
;
1884 b
->kind
== sk_template_parms
;
1888 for (; fns
; fns
= OVL_NEXT (fns
))
1890 tree fn
= OVL_CURRENT (fns
);
1892 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1894 tree decl_arg_types
;
1898 /* In case of explicit specialization, we need to check if
1899 the number of template headers appearing in the specialization
1900 is correct. This is usually done in check_explicit_specialization,
1901 but the check done there cannot be exhaustive when specializing
1902 member functions. Consider the following code:
1904 template <> void A<int>::f(int);
1905 template <> template <> void A<int>::f(int);
1907 Assuming that A<int> is not itself an explicit specialization
1908 already, the first line specializes "f" which is a non-template
1909 member function, whilst the second line specializes "f" which
1910 is a template member function. So both lines are syntactically
1911 correct, and check_explicit_specialization does not reject
1914 Here, we can do better, as we are matching the specialization
1915 against the declarations. We count the number of template
1916 headers, and we check if they match TEMPLATE_COUNT + 1
1917 (TEMPLATE_COUNT is the number of qualifying template classes,
1918 plus there must be another header for the member template
1921 Notice that if header_count is zero, this is not a
1922 specialization but rather a template instantiation, so there
1923 is no check we can perform here. */
1924 if (header_count
&& header_count
!= template_count
+ 1)
1927 /* Check that the number of template arguments at the
1928 innermost level for DECL is the same as for FN. */
1929 if (current_binding_level
->kind
== sk_template_parms
1930 && !current_binding_level
->explicit_spec_p
1931 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1932 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1933 (current_template_parms
))))
1936 /* DECL might be a specialization of FN. */
1937 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1938 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1940 /* For a non-static member function, we need to make sure
1941 that the const qualification is the same. Since
1942 get_bindings does not try to merge the "this" parameter,
1943 we must do the comparison explicitly. */
1944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1945 && !same_type_p (TREE_VALUE (fn_arg_types
),
1946 TREE_VALUE (decl_arg_types
)))
1949 /* Skip the "this" parameter and, for constructors of
1950 classes with virtual bases, the VTT parameter. A
1951 full specialization of a constructor will have a VTT
1952 parameter, but a template never will. */
1954 = skip_artificial_parms_for (decl
, decl_arg_types
);
1956 = skip_artificial_parms_for (fn
, fn_arg_types
);
1958 /* Function templates cannot be specializations; there are
1959 no partial specializations of functions. Therefore, if
1960 the type of DECL does not match FN, there is no
1962 if (tsk
== tsk_template
)
1964 if (compparms (fn_arg_types
, decl_arg_types
))
1965 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1969 /* See whether this function might be a specialization of this
1970 template. Suppress access control because we might be trying
1971 to make this specialization a friend, and we have already done
1972 access control for the declaration of the specialization. */
1973 push_deferring_access_checks (dk_no_check
);
1974 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1975 pop_deferring_access_checks ();
1978 /* We cannot deduce template arguments that when used to
1979 specialize TMPL will produce DECL. */
1982 /* Make sure that the deduced arguments actually work. */
1983 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1984 if (insttype
== error_mark_node
)
1987 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1988 if (!compparms (fn_arg_types
, decl_arg_types
))
1991 /* Save this template, and the arguments deduced. */
1992 templates
= tree_cons (targs
, fn
, templates
);
1994 else if (need_member_template
)
1995 /* FN is an ordinary member function, and we need a
1996 specialization of a member template. */
1998 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1999 /* We can get IDENTIFIER_NODEs here in certain erroneous
2002 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2003 /* This is just an ordinary non-member function. Nothing can
2004 be a specialization of that. */
2006 else if (DECL_ARTIFICIAL (fn
))
2007 /* Cannot specialize functions that are created implicitly. */
2011 tree decl_arg_types
;
2013 /* This is an ordinary member function. However, since
2014 we're here, we can assume its enclosing class is a
2015 template class. For example,
2017 template <typename T> struct S { void f(); };
2018 template <> void S<int>::f() {}
2020 Here, S<int>::f is a non-template, but S<int> is a
2021 template class. If FN has the same type as DECL, we
2022 might be in business. */
2024 if (!DECL_TEMPLATE_INFO (fn
))
2025 /* Its enclosing class is an explicit specialization
2026 of a template class. This is not a candidate. */
2029 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2030 TREE_TYPE (TREE_TYPE (fn
))))
2031 /* The return types differ. */
2034 /* Adjust the type of DECL in case FN is a static member. */
2035 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2036 if (DECL_STATIC_FUNCTION_P (fn
)
2037 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2038 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2040 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2043 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2047 if (templates
&& TREE_CHAIN (templates
))
2053 It is possible for a specialization with a given function
2054 signature to be instantiated from more than one function
2055 template. In such cases, explicit specification of the
2056 template arguments must be used to uniquely identify the
2057 function template specialization being specialized.
2059 Note that here, there's no suggestion that we're supposed to
2060 determine which of the candidate templates is most
2061 specialized. However, we, also have:
2065 Partial ordering of overloaded function template
2066 declarations is used in the following contexts to select
2067 the function template to which a function template
2068 specialization refers:
2070 -- when an explicit specialization refers to a function
2073 So, we do use the partial ordering rules, at least for now.
2074 This extension can only serve to make invalid programs valid,
2075 so it's safe. And, there is strong anecdotal evidence that
2076 the committee intended the partial ordering rules to apply;
2077 the EDG front end has that behavior, and John Spicer claims
2078 that the committee simply forgot to delete the wording in
2079 [temp.expl.spec]. */
2080 tree tmpl
= most_specialized_instantiation (templates
);
2081 if (tmpl
!= error_mark_node
)
2084 TREE_CHAIN (templates
) = NULL_TREE
;
2088 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2090 error ("template-id %qD for %q+D does not match any template "
2091 "declaration", template_id
, decl
);
2092 if (header_count
&& header_count
!= template_count
+ 1)
2093 inform (input_location
, "saw %d %<template<>%>, need %d for "
2094 "specializing a member function template",
2095 header_count
, template_count
+ 1);
2096 return error_mark_node
;
2098 else if ((templates
&& TREE_CHAIN (templates
))
2099 || (candidates
&& TREE_CHAIN (candidates
))
2100 || (templates
&& candidates
))
2102 error ("ambiguous template specialization %qD for %q+D",
2104 candidates
= chainon (candidates
, templates
);
2105 print_candidates (candidates
);
2106 return error_mark_node
;
2109 /* We have one, and exactly one, match. */
2112 tree fn
= TREE_VALUE (candidates
);
2113 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2114 /* DECL is a re-declaration or partial instantiation of a template
2116 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2118 /* It was a specialization of an ordinary member function in a
2120 return DECL_TI_TEMPLATE (fn
);
2123 /* It was a specialization of a template. */
2124 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2125 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2127 *targs_out
= copy_node (targs
);
2128 SET_TMPL_ARGS_LEVEL (*targs_out
,
2129 TMPL_ARGS_DEPTH (*targs_out
),
2130 TREE_PURPOSE (templates
));
2133 *targs_out
= TREE_PURPOSE (templates
);
2134 return TREE_VALUE (templates
);
2137 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2138 but with the default argument values filled in from those in the
2142 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2145 tree new_spec_types
;
2150 if (spec_types
== void_list_node
)
2151 return void_list_node
;
2153 /* Substitute into the rest of the list. */
2155 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2156 TREE_CHAIN (tmpl_types
));
2158 /* Add the default argument for this parameter. */
2159 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2160 TREE_VALUE (spec_types
),
2164 /* DECL is an explicit specialization. Replicate default arguments
2165 from the template it specializes. (That way, code like:
2167 template <class T> void f(T = 3);
2168 template <> void f(double);
2171 works, as required.) An alternative approach would be to look up
2172 the correct default arguments at the call-site, but this approach
2173 is consistent with how implicit instantiations are handled. */
2176 copy_default_args_to_explicit_spec (tree decl
)
2181 tree new_spec_types
;
2185 tree object_type
= NULL_TREE
;
2186 tree in_charge
= NULL_TREE
;
2187 tree vtt
= NULL_TREE
;
2189 /* See if there's anything we need to do. */
2190 tmpl
= DECL_TI_TEMPLATE (decl
);
2191 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2192 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2193 if (TREE_PURPOSE (t
))
2198 old_type
= TREE_TYPE (decl
);
2199 spec_types
= TYPE_ARG_TYPES (old_type
);
2201 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2203 /* Remove the this pointer, but remember the object's type for
2205 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2206 spec_types
= TREE_CHAIN (spec_types
);
2207 tmpl_types
= TREE_CHAIN (tmpl_types
);
2209 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2211 /* DECL may contain more parameters than TMPL due to the extra
2212 in-charge parameter in constructors and destructors. */
2213 in_charge
= spec_types
;
2214 spec_types
= TREE_CHAIN (spec_types
);
2216 if (DECL_HAS_VTT_PARM_P (decl
))
2219 spec_types
= TREE_CHAIN (spec_types
);
2223 /* Compute the merged default arguments. */
2225 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2227 /* Compute the new FUNCTION_TYPE. */
2231 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2236 /* Put the in-charge parameter back. */
2237 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2238 TREE_VALUE (in_charge
),
2241 new_type
= build_method_type_directly (object_type
,
2242 TREE_TYPE (old_type
),
2246 new_type
= build_function_type (TREE_TYPE (old_type
),
2248 new_type
= cp_build_type_attribute_variant (new_type
,
2249 TYPE_ATTRIBUTES (old_type
));
2250 new_type
= build_exception_variant (new_type
,
2251 TYPE_RAISES_EXCEPTIONS (old_type
));
2252 TREE_TYPE (decl
) = new_type
;
2255 /* Return the number of template headers we expect to see for a definition
2256 or specialization of CTYPE or one of its non-template members. */
2259 num_template_headers_for_class (tree ctype
)
2261 int num_templates
= 0;
2263 while (ctype
&& CLASS_TYPE_P (ctype
))
2265 /* You're supposed to have one `template <...>' for every
2266 template class, but you don't need one for a full
2267 specialization. For example:
2269 template <class T> struct S{};
2270 template <> struct S<int> { void f(); };
2271 void S<int>::f () {}
2273 is correct; there shouldn't be a `template <>' for the
2274 definition of `S<int>::f'. */
2275 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2276 /* If CTYPE does not have template information of any
2277 kind, then it is not a template, nor is it nested
2278 within a template. */
2280 if (explicit_class_specialization_p (ctype
))
2282 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2285 ctype
= TYPE_CONTEXT (ctype
);
2288 return num_templates
;
2291 /* Do a simple sanity check on the template headers that precede the
2292 variable declaration DECL. */
2295 check_template_variable (tree decl
)
2297 tree ctx
= CP_DECL_CONTEXT (decl
);
2298 int wanted
= num_template_headers_for_class (ctx
);
2299 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2300 permerror (DECL_SOURCE_LOCATION (decl
),
2301 "%qD is not a static data member of a class template", decl
);
2302 else if (template_header_count
> wanted
)
2304 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2305 "too many template headers for %D (should be %d)",
2307 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2308 inform (DECL_SOURCE_LOCATION (decl
),
2309 "members of an explicitly specialized class are defined "
2310 "without a template header");
2314 /* Check to see if the function just declared, as indicated in
2315 DECLARATOR, and in DECL, is a specialization of a function
2316 template. We may also discover that the declaration is an explicit
2317 instantiation at this point.
2319 Returns DECL, or an equivalent declaration that should be used
2320 instead if all goes well. Issues an error message if something is
2321 amiss. Returns error_mark_node if the error is not easily
2324 FLAGS is a bitmask consisting of the following flags:
2326 2: The function has a definition.
2327 4: The function is a friend.
2329 The TEMPLATE_COUNT is the number of references to qualifying
2330 template classes that appeared in the name of the function. For
2333 template <class T> struct S { void f(); };
2336 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2337 classes are not counted in the TEMPLATE_COUNT, so that in
2339 template <class T> struct S {};
2340 template <> struct S<int> { void f(); }
2341 template <> void S<int>::f();
2343 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2344 invalid; there should be no template <>.)
2346 If the function is a specialization, it is marked as such via
2347 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2348 is set up correctly, and it is added to the list of specializations
2349 for that template. */
2352 check_explicit_specialization (tree declarator
,
2357 int have_def
= flags
& 2;
2358 int is_friend
= flags
& 4;
2359 int specialization
= 0;
2360 int explicit_instantiation
= 0;
2361 int member_specialization
= 0;
2362 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2363 tree dname
= DECL_NAME (decl
);
2368 if (!processing_specialization
)
2371 tsk
= tsk_excessive_parms
;
2374 tsk
= current_tmpl_spec_kind (template_count
);
2379 if (processing_specialization
)
2382 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2384 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2387 /* This could be something like:
2389 template <class T> void f(T);
2390 class S { friend void f<>(int); } */
2394 /* This case handles bogus declarations like template <>
2395 template <class T> void f<int>(); */
2397 error ("template-id %qD in declaration of primary template",
2404 case tsk_invalid_member_spec
:
2405 /* The error has already been reported in
2406 check_specialization_scope. */
2407 return error_mark_node
;
2409 case tsk_invalid_expl_inst
:
2410 error ("template parameter list used in explicit instantiation");
2416 error ("definition provided for explicit instantiation");
2418 explicit_instantiation
= 1;
2421 case tsk_excessive_parms
:
2422 case tsk_insufficient_parms
:
2423 if (tsk
== tsk_excessive_parms
)
2424 error ("too many template parameter lists in declaration of %qD",
2426 else if (template_header_count
)
2427 error("too few template parameter lists in declaration of %qD", decl
);
2429 error("explicit specialization of %qD must be introduced by "
2430 "%<template <>%>", decl
);
2434 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2436 member_specialization
= 1;
2442 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2444 /* This case handles bogus declarations like template <>
2445 template <class T> void f<int>(); */
2447 if (uses_template_parms (declarator
))
2448 error ("function template partial specialization %qD "
2449 "is not allowed", declarator
);
2451 error ("template-id %qD in declaration of primary template",
2456 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2457 /* This is a specialization of a member template, without
2458 specialization the containing class. Something like:
2460 template <class T> struct S {
2461 template <class U> void f (U);
2463 template <> template <class U> void S<int>::f(U) {}
2465 That's a specialization -- but of the entire template. */
2473 if (specialization
|| member_specialization
)
2475 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2476 for (; t
; t
= TREE_CHAIN (t
))
2477 if (TREE_PURPOSE (t
))
2479 permerror (input_location
,
2480 "default argument specified in explicit specialization");
2485 if (specialization
|| member_specialization
|| explicit_instantiation
)
2487 tree tmpl
= NULL_TREE
;
2488 tree targs
= NULL_TREE
;
2490 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2491 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2495 gcc_assert (identifier_p (declarator
));
2500 /* If there is no class context, the explicit instantiation
2501 must be at namespace scope. */
2502 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2504 /* Find the namespace binding, using the declaration
2506 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2508 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2510 error ("%qD is not a template function", dname
);
2511 fns
= error_mark_node
;
2515 tree fn
= OVL_CURRENT (fns
);
2516 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2517 CP_DECL_CONTEXT (fn
)))
2518 error ("%qD is not declared in %qD",
2519 decl
, current_namespace
);
2523 declarator
= lookup_template_function (fns
, NULL_TREE
);
2526 if (declarator
== error_mark_node
)
2527 return error_mark_node
;
2529 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2531 if (!explicit_instantiation
)
2532 /* A specialization in class scope. This is invalid,
2533 but the error will already have been flagged by
2534 check_specialization_scope. */
2535 return error_mark_node
;
2538 /* It's not valid to write an explicit instantiation in
2541 class C { template void f(); }
2543 This case is caught by the parser. However, on
2546 template class C { void f(); };
2548 (which is invalid) we can get here. The error will be
2555 else if (ctype
!= NULL_TREE
2556 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2558 /* Find the list of functions in ctype that have the same
2559 name as the declared function. */
2560 tree name
= TREE_OPERAND (declarator
, 0);
2561 tree fns
= NULL_TREE
;
2564 if (constructor_name_p (name
, ctype
))
2566 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2568 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2569 : !CLASSTYPE_DESTRUCTORS (ctype
))
2571 /* From [temp.expl.spec]:
2573 If such an explicit specialization for the member
2574 of a class template names an implicitly-declared
2575 special member function (clause _special_), the
2576 program is ill-formed.
2578 Similar language is found in [temp.explicit]. */
2579 error ("specialization of implicitly-declared special member function");
2580 return error_mark_node
;
2583 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2586 if (!DECL_CONV_FN_P (decl
))
2588 idx
= lookup_fnfields_1 (ctype
, name
);
2590 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2594 vec
<tree
, va_gc
> *methods
;
2597 /* For a type-conversion operator, we cannot do a
2598 name-based lookup. We might be looking for `operator
2599 int' which will be a specialization of `operator T'.
2600 So, we find *all* the conversion operators, and then
2601 select from them. */
2604 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2606 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2607 methods
->iterate (idx
, &ovl
);
2610 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2611 /* There are no more conversion functions. */
2614 /* Glue all these conversion functions together
2615 with those we already have. */
2616 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2617 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2621 if (fns
== NULL_TREE
)
2623 error ("no member function %qD declared in %qT", name
, ctype
);
2624 return error_mark_node
;
2627 TREE_OPERAND (declarator
, 0) = fns
;
2630 /* Figure out what exactly is being specialized at this point.
2631 Note that for an explicit instantiation, even one for a
2632 member function, we cannot tell apriori whether the
2633 instantiation is for a member template, or just a member
2634 function of a template class. Even if a member template is
2635 being instantiated, the member template arguments may be
2636 elided if they can be deduced from the rest of the
2638 tmpl
= determine_specialization (declarator
, decl
,
2640 member_specialization
,
2644 if (!tmpl
|| tmpl
== error_mark_node
)
2645 /* We couldn't figure out what this declaration was
2647 return error_mark_node
;
2650 tree gen_tmpl
= most_general_template (tmpl
);
2652 if (explicit_instantiation
)
2654 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2655 is done by do_decl_instantiation later. */
2657 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2658 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2660 if (arg_depth
> parm_depth
)
2662 /* If TMPL is not the most general template (for
2663 example, if TMPL is a friend template that is
2664 injected into namespace scope), then there will
2665 be too many levels of TARGS. Remove some of them
2670 new_targs
= make_tree_vec (parm_depth
);
2671 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2672 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2673 = TREE_VEC_ELT (targs
, i
);
2677 return instantiate_template (tmpl
, targs
, tf_error
);
2680 /* If we thought that the DECL was a member function, but it
2681 turns out to be specializing a static member function,
2682 make DECL a static member function as well. */
2683 if (DECL_STATIC_FUNCTION_P (tmpl
)
2684 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2685 revert_static_member_fn (decl
);
2687 /* If this is a specialization of a member template of a
2688 template class, we want to return the TEMPLATE_DECL, not
2689 the specialization of it. */
2690 if (tsk
== tsk_template
)
2692 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2693 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2694 DECL_INITIAL (result
) = NULL_TREE
;
2698 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2699 DECL_SOURCE_LOCATION (result
)
2700 = DECL_SOURCE_LOCATION (decl
);
2701 /* We want to use the argument list specified in the
2702 definition, not in the original declaration. */
2703 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2704 for (parm
= DECL_ARGUMENTS (result
); parm
;
2705 parm
= DECL_CHAIN (parm
))
2706 DECL_CONTEXT (parm
) = result
;
2708 return register_specialization (tmpl
, gen_tmpl
, targs
,
2712 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2713 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2715 /* Inherit default function arguments from the template
2716 DECL is specializing. */
2717 copy_default_args_to_explicit_spec (decl
);
2719 /* This specialization has the same protection as the
2720 template it specializes. */
2721 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2722 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2724 /* 7.1.1-1 [dcl.stc]
2726 A storage-class-specifier shall not be specified in an
2727 explicit specialization...
2729 The parser rejects these, so unless action is taken here,
2730 explicit function specializations will always appear with
2733 The action recommended by the C++ CWG in response to C++
2734 defect report 605 is to make the storage class and linkage
2735 of the explicit specialization match the templated function:
2737 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2739 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2741 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2742 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2744 /* This specialization has the same linkage and visibility as
2745 the function template it specializes. */
2746 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2747 if (! TREE_PUBLIC (decl
))
2749 DECL_INTERFACE_KNOWN (decl
) = 1;
2750 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2752 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2753 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2755 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2756 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2760 /* If DECL is a friend declaration, declared using an
2761 unqualified name, the namespace associated with DECL may
2762 have been set incorrectly. For example, in:
2764 template <typename T> void f(T);
2766 struct S { friend void f<int>(int); }
2769 we will have set the DECL_CONTEXT for the friend
2770 declaration to N, rather than to the global namespace. */
2771 if (DECL_NAMESPACE_SCOPE_P (decl
))
2772 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2774 if (is_friend
&& !have_def
)
2775 /* This is not really a declaration of a specialization.
2776 It's just the name of an instantiation. But, it's not
2777 a request for an instantiation, either. */
2778 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2779 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2780 /* This is indeed a specialization. In case of constructors
2781 and destructors, we need in-charge and not-in-charge
2782 versions in V3 ABI. */
2783 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2785 /* Register this specialization so that we can find it
2787 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2794 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2795 parameters. These are represented in the same format used for
2796 DECL_TEMPLATE_PARMS. */
2799 comp_template_parms (const_tree parms1
, const_tree parms2
)
2804 if (parms1
== parms2
)
2807 for (p1
= parms1
, p2
= parms2
;
2808 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2809 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2811 tree t1
= TREE_VALUE (p1
);
2812 tree t2
= TREE_VALUE (p2
);
2815 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2816 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2818 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2821 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2823 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2824 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2826 /* If either of the template parameters are invalid, assume
2827 they match for the sake of error recovery. */
2828 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2831 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2834 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2835 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2836 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2838 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2843 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2844 /* One set of parameters has more parameters lists than the
2851 /* Determine whether PARM is a parameter pack. */
2854 template_parameter_pack_p (const_tree parm
)
2856 /* Determine if we have a non-type template parameter pack. */
2857 if (TREE_CODE (parm
) == PARM_DECL
)
2858 return (DECL_TEMPLATE_PARM_P (parm
)
2859 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2860 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2861 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2863 /* If this is a list of template parameters, we could get a
2864 TYPE_DECL or a TEMPLATE_DECL. */
2865 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2866 parm
= TREE_TYPE (parm
);
2868 /* Otherwise it must be a type template parameter. */
2869 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2870 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2871 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2874 /* Determine if T is a function parameter pack. */
2877 function_parameter_pack_p (const_tree t
)
2879 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2880 return DECL_PACK_P (t
);
2884 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2885 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2888 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2890 if (! primary_func_tmpl_inst
2891 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2892 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2895 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2898 /* Return true iff the function parameter PARAM_DECL was expanded
2899 from the function parameter pack PACK. */
2902 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2904 if (DECL_ARTIFICIAL (param_decl
)
2905 || !function_parameter_pack_p (pack
))
2908 /* The parameter pack and its pack arguments have the same
2910 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2913 /* Determine whether ARGS describes a variadic template args list,
2914 i.e., one that is terminated by a template argument pack. */
2917 template_args_variadic_p (tree args
)
2922 if (args
== NULL_TREE
)
2925 args
= INNERMOST_TEMPLATE_ARGS (args
);
2926 nargs
= TREE_VEC_LENGTH (args
);
2931 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2933 return ARGUMENT_PACK_P (last_parm
);
2936 /* Generate a new name for the parameter pack name NAME (an
2937 IDENTIFIER_NODE) that incorporates its */
2940 make_ith_pack_parameter_name (tree name
, int i
)
2942 /* Munge the name to include the parameter index. */
2943 #define NUMBUF_LEN 128
2944 char numbuf
[NUMBUF_LEN
];
2948 if (name
== NULL_TREE
)
2950 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2951 newname_len
= IDENTIFIER_LENGTH (name
)
2952 + strlen (numbuf
) + 2;
2953 newname
= (char*)alloca (newname_len
);
2954 snprintf (newname
, newname_len
,
2955 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2956 return get_identifier (newname
);
2959 /* Return true if T is a primary function, class or alias template
2963 primary_template_instantiation_p (const_tree t
)
2968 if (TREE_CODE (t
) == FUNCTION_DECL
)
2969 return DECL_LANG_SPECIFIC (t
)
2970 && DECL_TEMPLATE_INSTANTIATION (t
)
2971 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2972 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2973 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2974 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2975 else if (alias_template_specialization_p (t
))
2980 /* Return true if PARM is a template template parameter. */
2983 template_template_parameter_p (const_tree parm
)
2985 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2988 /* Return true iff PARM is a DECL representing a type template
2992 template_type_parameter_p (const_tree parm
)
2995 && (TREE_CODE (parm
) == TYPE_DECL
2996 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2997 && DECL_TEMPLATE_PARM_P (parm
));
3000 /* Return the template parameters of T if T is a
3001 primary template instantiation, NULL otherwise. */
3004 get_primary_template_innermost_parameters (const_tree t
)
3006 tree parms
= NULL
, template_info
= NULL
;
3008 if ((template_info
= get_template_info (t
))
3009 && primary_template_instantiation_p (t
))
3010 parms
= INNERMOST_TEMPLATE_PARMS
3011 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3016 /* Return the template parameters of the LEVELth level from the full list
3017 of template parameters PARMS. */
3020 get_template_parms_at_level (tree parms
, int level
)
3024 || TREE_CODE (parms
) != TREE_LIST
3025 || level
> TMPL_PARMS_DEPTH (parms
))
3028 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3029 if (TMPL_PARMS_DEPTH (p
) == level
)
3035 /* Returns the template arguments of T if T is a template instantiation,
3039 get_template_innermost_arguments (const_tree t
)
3041 tree args
= NULL
, template_info
= NULL
;
3043 if ((template_info
= get_template_info (t
))
3044 && TI_ARGS (template_info
))
3045 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3050 /* Return the argument pack elements of T if T is a template argument pack,
3054 get_template_argument_pack_elems (const_tree t
)
3056 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3057 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3060 return ARGUMENT_PACK_ARGS (t
);
3063 /* Structure used to track the progress of find_parameter_packs_r. */
3064 struct find_parameter_pack_data
3066 /* TREE_LIST that will contain all of the parameter packs found by
3068 tree
* parameter_packs
;
3070 /* Set of AST nodes that have been visited by the traversal. */
3071 struct pointer_set_t
*visited
;
3074 /* Identifies all of the argument packs that occur in a template
3075 argument and appends them to the TREE_LIST inside DATA, which is a
3076 find_parameter_pack_data structure. This is a subroutine of
3077 make_pack_expansion and uses_parameter_packs. */
3079 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3082 struct find_parameter_pack_data
* ppd
=
3083 (struct find_parameter_pack_data
*)data
;
3084 bool parameter_pack_p
= false;
3086 /* Handle type aliases/typedefs. */
3087 if (TYPE_ALIAS_P (t
))
3089 if (TYPE_TEMPLATE_INFO (t
))
3090 cp_walk_tree (&TYPE_TI_ARGS (t
),
3091 &find_parameter_packs_r
,
3097 /* Identify whether this is a parameter pack or not. */
3098 switch (TREE_CODE (t
))
3100 case TEMPLATE_PARM_INDEX
:
3101 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3102 parameter_pack_p
= true;
3105 case TEMPLATE_TYPE_PARM
:
3106 t
= TYPE_MAIN_VARIANT (t
);
3107 case TEMPLATE_TEMPLATE_PARM
:
3108 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3109 parameter_pack_p
= true;
3114 if (DECL_PACK_P (t
))
3116 /* We don't want to walk into the type of a PARM_DECL,
3117 because we don't want to see the type parameter pack. */
3119 parameter_pack_p
= true;
3123 /* Look through a lambda capture proxy to the field pack. */
3125 if (DECL_HAS_VALUE_EXPR_P (t
))
3127 tree v
= DECL_VALUE_EXPR (t
);
3129 &find_parameter_packs_r
,
3136 parameter_pack_p
= true;
3139 /* Not a parameter pack. */
3143 if (parameter_pack_p
)
3145 /* Add this parameter pack to the list. */
3146 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3150 cp_walk_tree (&TYPE_CONTEXT (t
),
3151 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3153 /* This switch statement will return immediately if we don't find a
3155 switch (TREE_CODE (t
))
3157 case TEMPLATE_PARM_INDEX
:
3160 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3161 /* Check the template itself. */
3162 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3163 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3164 /* Check the template arguments. */
3165 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3170 case TEMPLATE_TYPE_PARM
:
3171 case TEMPLATE_TEMPLATE_PARM
:
3178 if (TYPE_PTRMEMFUNC_P (t
))
3184 if (TYPE_TEMPLATE_INFO (t
))
3185 cp_walk_tree (&TYPE_TI_ARGS (t
),
3186 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3193 cp_walk_tree (&TREE_TYPE (t
),
3194 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3198 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3203 case TYPE_PACK_EXPANSION
:
3204 case EXPR_PACK_EXPANSION
:
3209 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3214 case IDENTIFIER_NODE
:
3215 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3227 /* Determines if the expression or type T uses any parameter packs. */
3229 uses_parameter_packs (tree t
)
3231 tree parameter_packs
= NULL_TREE
;
3232 struct find_parameter_pack_data ppd
;
3233 ppd
.parameter_packs
= ¶meter_packs
;
3234 ppd
.visited
= pointer_set_create ();
3235 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3236 pointer_set_destroy (ppd
.visited
);
3237 return parameter_packs
!= NULL_TREE
;
3240 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3241 representation a base-class initializer into a parameter pack
3242 expansion. If all goes well, the resulting node will be an
3243 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3246 make_pack_expansion (tree arg
)
3249 tree parameter_packs
= NULL_TREE
;
3250 bool for_types
= false;
3251 struct find_parameter_pack_data ppd
;
3253 if (!arg
|| arg
== error_mark_node
)
3256 if (TREE_CODE (arg
) == TREE_LIST
)
3258 /* The only time we will see a TREE_LIST here is for a base
3259 class initializer. In this case, the TREE_PURPOSE will be a
3260 _TYPE node (representing the base class expansion we're
3261 initializing) and the TREE_VALUE will be a TREE_LIST
3262 containing the initialization arguments.
3264 The resulting expansion looks somewhat different from most
3265 expansions. Rather than returning just one _EXPANSION, we
3266 return a TREE_LIST whose TREE_PURPOSE is a
3267 TYPE_PACK_EXPANSION containing the bases that will be
3268 initialized. The TREE_VALUE will be identical to the
3269 original TREE_VALUE, which is a list of arguments that will
3270 be passed to each base. We do not introduce any new pack
3271 expansion nodes into the TREE_VALUE (although it is possible
3272 that some already exist), because the TREE_PURPOSE and
3273 TREE_VALUE all need to be expanded together with the same
3274 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3275 resulting TREE_PURPOSE will mention the parameter packs in
3276 both the bases and the arguments to the bases. */
3279 tree parameter_packs
= NULL_TREE
;
3281 /* Determine which parameter packs will be used by the base
3283 ppd
.visited
= pointer_set_create ();
3284 ppd
.parameter_packs
= ¶meter_packs
;
3285 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3288 if (parameter_packs
== NULL_TREE
)
3290 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3291 pointer_set_destroy (ppd
.visited
);
3292 return error_mark_node
;
3295 if (TREE_VALUE (arg
) != void_type_node
)
3297 /* Collect the sets of parameter packs used in each of the
3298 initialization arguments. */
3299 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3301 /* Determine which parameter packs will be expanded in this
3303 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3308 pointer_set_destroy (ppd
.visited
);
3310 /* Create the pack expansion type for the base type. */
3311 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3312 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3313 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3315 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3316 they will rarely be compared to anything. */
3317 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3319 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3322 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3325 /* Build the PACK_EXPANSION_* node. */
3327 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3328 : make_node (EXPR_PACK_EXPANSION
);
3329 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3330 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3332 /* Propagate type and const-expression information. */
3333 TREE_TYPE (result
) = TREE_TYPE (arg
);
3334 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3337 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3338 they will rarely be compared to anything. */
3339 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3341 /* Determine which parameter packs will be expanded. */
3342 ppd
.parameter_packs
= ¶meter_packs
;
3343 ppd
.visited
= pointer_set_create ();
3344 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3345 pointer_set_destroy (ppd
.visited
);
3347 /* Make sure we found some parameter packs. */
3348 if (parameter_packs
== NULL_TREE
)
3351 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3353 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3354 return error_mark_node
;
3356 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3358 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3363 /* Checks T for any "bare" parameter packs, which have not yet been
3364 expanded, and issues an error if any are found. This operation can
3365 only be done on full expressions or types (e.g., an expression
3366 statement, "if" condition, etc.), because we could have expressions like:
3368 foo(f(g(h(args)))...)
3370 where "args" is a parameter pack. check_for_bare_parameter_packs
3371 should not be called for the subexpressions args, h(args),
3372 g(h(args)), or f(g(h(args))), because we would produce erroneous
3375 Returns TRUE and emits an error if there were bare parameter packs,
3376 returns FALSE otherwise. */
3378 check_for_bare_parameter_packs (tree t
)
3380 tree parameter_packs
= NULL_TREE
;
3381 struct find_parameter_pack_data ppd
;
3383 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3386 if (TREE_CODE (t
) == TYPE_DECL
)
3389 ppd
.parameter_packs
= ¶meter_packs
;
3390 ppd
.visited
= pointer_set_create ();
3391 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3392 pointer_set_destroy (ppd
.visited
);
3394 if (parameter_packs
)
3396 error ("parameter packs not expanded with %<...%>:");
3397 while (parameter_packs
)
3399 tree pack
= TREE_VALUE (parameter_packs
);
3400 tree name
= NULL_TREE
;
3402 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3403 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3404 name
= TYPE_NAME (pack
);
3405 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3406 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3408 name
= DECL_NAME (pack
);
3411 inform (input_location
, " %qD", name
);
3413 inform (input_location
, " <anonymous>");
3415 parameter_packs
= TREE_CHAIN (parameter_packs
);
3424 /* Expand any parameter packs that occur in the template arguments in
3427 expand_template_argument_pack (tree args
)
3429 tree result_args
= NULL_TREE
;
3430 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3431 int num_result_args
= -1;
3432 int non_default_args_count
= -1;
3434 /* First, determine if we need to expand anything, and the number of
3435 slots we'll need. */
3436 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3438 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3439 if (arg
== NULL_TREE
)
3441 if (ARGUMENT_PACK_P (arg
))
3443 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3444 if (num_result_args
< 0)
3445 num_result_args
= in_arg
+ num_packed
;
3447 num_result_args
+= num_packed
;
3451 if (num_result_args
>= 0)
3456 /* If no expansion is necessary, we're done. */
3457 if (num_result_args
< 0)
3460 /* Expand arguments. */
3461 result_args
= make_tree_vec (num_result_args
);
3462 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3463 non_default_args_count
=
3464 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3465 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3467 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3468 if (ARGUMENT_PACK_P (arg
))
3470 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3471 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3472 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3473 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3474 if (non_default_args_count
> 0)
3475 non_default_args_count
+= num_packed
;
3479 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3483 if (non_default_args_count
>= 0)
3484 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3488 /* Checks if DECL shadows a template parameter.
3490 [temp.local]: A template-parameter shall not be redeclared within its
3491 scope (including nested scopes).
3493 Emits an error and returns TRUE if the DECL shadows a parameter,
3494 returns FALSE otherwise. */
3497 check_template_shadow (tree decl
)
3501 /* If we're not in a template, we can't possibly shadow a template
3503 if (!current_template_parms
)
3506 /* Figure out what we're shadowing. */
3507 if (TREE_CODE (decl
) == OVERLOAD
)
3508 decl
= OVL_CURRENT (decl
);
3509 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3511 /* If there's no previous binding for this name, we're not shadowing
3512 anything, let alone a template parameter. */
3516 /* If we're not shadowing a template parameter, we're done. Note
3517 that OLDDECL might be an OVERLOAD (or perhaps even an
3518 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3520 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3523 /* We check for decl != olddecl to avoid bogus errors for using a
3524 name inside a class. We check TPFI to avoid duplicate errors for
3525 inline member templates. */
3527 || (DECL_TEMPLATE_PARM_P (decl
)
3528 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3531 error ("declaration of %q+#D", decl
);
3532 error (" shadows template parm %q+#D", olddecl
);
3536 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3537 ORIG_LEVEL, DECL, and TYPE. */
3540 build_template_parm_index (int index
,
3546 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3547 TEMPLATE_PARM_IDX (t
) = index
;
3548 TEMPLATE_PARM_LEVEL (t
) = level
;
3549 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3550 TEMPLATE_PARM_DECL (t
) = decl
;
3551 TREE_TYPE (t
) = type
;
3552 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3553 TREE_READONLY (t
) = TREE_READONLY (decl
);
3558 /* Find the canonical type parameter for the given template type
3559 parameter. Returns the canonical type parameter, which may be TYPE
3560 if no such parameter existed. */
3563 canonical_type_parameter (tree type
)
3566 int idx
= TEMPLATE_TYPE_IDX (type
);
3567 if (!canonical_template_parms
)
3568 vec_alloc (canonical_template_parms
, idx
+1);
3570 while (canonical_template_parms
->length () <= (unsigned)idx
)
3571 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3573 list
= (*canonical_template_parms
)[idx
];
3574 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3575 list
= TREE_CHAIN (list
);
3578 return TREE_VALUE (list
);
3581 (*canonical_template_parms
)[idx
]
3582 = tree_cons (NULL_TREE
, type
,
3583 (*canonical_template_parms
)[idx
]);
3588 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3589 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3590 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3591 new one is created. */
3594 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3595 tsubst_flags_t complain
)
3597 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3598 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3599 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3600 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3602 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3605 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3606 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3607 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3608 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3609 DECL_ARTIFICIAL (decl
) = 1;
3610 SET_DECL_TEMPLATE_PARM_P (decl
);
3612 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3613 TEMPLATE_PARM_LEVEL (index
) - levels
,
3614 TEMPLATE_PARM_ORIG_LEVEL (index
),
3616 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3617 TEMPLATE_PARM_PARAMETER_PACK (t
)
3618 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3620 /* Template template parameters need this. */
3621 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3622 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3623 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3627 return TEMPLATE_PARM_DESCENDANTS (index
);
3630 /* Process information from new template parameter PARM and append it
3631 to the LIST being built. This new parameter is a non-type
3632 parameter iff IS_NON_TYPE is true. This new parameter is a
3633 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3634 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3635 parameter list PARM belongs to. This is used used to create a
3636 proper canonical type for the type of PARM that is to be created,
3637 iff PARM is a type. If the size is not known, this parameter shall
3641 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3642 bool is_non_type
, bool is_parameter_pack
)
3649 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3650 defval
= TREE_PURPOSE (parm
);
3654 tree p
= tree_last (list
);
3656 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3659 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3660 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3662 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3672 parm
= TREE_VALUE (parm
);
3674 SET_DECL_TEMPLATE_PARM_P (parm
);
3676 if (TREE_TYPE (parm
) == error_mark_node
)
3678 err_parm_list
= build_tree_list (defval
, parm
);
3679 TREE_VALUE (err_parm_list
) = error_mark_node
;
3680 return chainon (list
, err_parm_list
);
3686 The top-level cv-qualifiers on the template-parameter are
3687 ignored when determining its type. */
3688 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3689 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3691 err_parm_list
= build_tree_list (defval
, parm
);
3692 TREE_VALUE (err_parm_list
) = error_mark_node
;
3693 return chainon (list
, err_parm_list
);
3696 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3698 /* This template parameter is not a parameter pack, but it
3699 should be. Complain about "bare" parameter packs. */
3700 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3702 /* Recover by calling this a parameter pack. */
3703 is_parameter_pack
= true;
3707 /* A template parameter is not modifiable. */
3708 TREE_CONSTANT (parm
) = 1;
3709 TREE_READONLY (parm
) = 1;
3710 decl
= build_decl (parm_loc
,
3711 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3712 TREE_CONSTANT (decl
) = 1;
3713 TREE_READONLY (decl
) = 1;
3714 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3715 = build_template_parm_index (idx
, processing_template_decl
,
3716 processing_template_decl
,
3717 decl
, TREE_TYPE (parm
));
3719 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3720 = is_parameter_pack
;
3725 parm
= TREE_VALUE (TREE_VALUE (parm
));
3727 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3729 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3730 /* This is for distinguishing between real templates and template
3731 template parameters */
3732 TREE_TYPE (parm
) = t
;
3733 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3738 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3739 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3740 decl
= build_decl (parm_loc
,
3741 TYPE_DECL
, parm
, t
);
3744 TYPE_NAME (t
) = decl
;
3745 TYPE_STUB_DECL (t
) = decl
;
3747 TEMPLATE_TYPE_PARM_INDEX (t
)
3748 = build_template_parm_index (idx
, processing_template_decl
,
3749 processing_template_decl
,
3750 decl
, TREE_TYPE (parm
));
3751 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3752 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3754 DECL_ARTIFICIAL (decl
) = 1;
3755 SET_DECL_TEMPLATE_PARM_P (decl
);
3757 parm
= build_tree_list (defval
, parm
);
3758 return chainon (list
, parm
);
3761 /* The end of a template parameter list has been reached. Process the
3762 tree list into a parameter vector, converting each parameter into a more
3763 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3767 end_template_parm_list (tree parms
)
3771 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3773 current_template_parms
3774 = tree_cons (size_int (processing_template_decl
),
3775 saved_parmlist
, current_template_parms
);
3777 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3779 next
= TREE_CHAIN (parm
);
3780 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3781 TREE_CHAIN (parm
) = NULL_TREE
;
3784 --processing_template_parmlist
;
3786 return saved_parmlist
;
3789 /* end_template_decl is called after a template declaration is seen. */
3792 end_template_decl (void)
3794 reset_specialization ();
3796 if (! processing_template_decl
)
3799 /* This matches the pushlevel in begin_template_parm_list. */
3802 --processing_template_decl
;
3803 current_template_parms
= TREE_CHAIN (current_template_parms
);
3806 /* Takes a TREE_LIST representing a template parameter and convert it
3807 into an argument suitable to be passed to the type substitution
3808 functions. Note that If the TREE_LIST contains an error_mark
3809 node, the returned argument is error_mark_node. */
3812 template_parm_to_arg (tree t
)
3816 || TREE_CODE (t
) != TREE_LIST
)
3819 if (error_operand_p (TREE_VALUE (t
)))
3820 return error_mark_node
;
3824 if (TREE_CODE (t
) == TYPE_DECL
3825 || TREE_CODE (t
) == TEMPLATE_DECL
)
3829 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3831 /* Turn this argument into a TYPE_ARGUMENT_PACK
3832 with a single element, which expands T. */
3833 tree vec
= make_tree_vec (1);
3834 #ifdef ENABLE_CHECKING
3835 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3836 (vec
, TREE_VEC_LENGTH (vec
));
3838 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3840 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3841 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3846 t
= DECL_INITIAL (t
);
3848 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3850 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3851 with a single element, which expands T. */
3852 tree vec
= make_tree_vec (1);
3853 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3854 #ifdef ENABLE_CHECKING
3855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3856 (vec
, TREE_VEC_LENGTH (vec
));
3858 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3860 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3861 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3862 TREE_TYPE (t
) = type
;
3868 /* Given a set of template parameters, return them as a set of template
3869 arguments. The template parameters are represented as a TREE_VEC, in
3870 the form documented in cp-tree.h for template arguments. */
3873 template_parms_to_args (tree parms
)
3876 tree args
= NULL_TREE
;
3877 int length
= TMPL_PARMS_DEPTH (parms
);
3880 /* If there is only one level of template parameters, we do not
3881 create a TREE_VEC of TREE_VECs. Instead, we return a single
3882 TREE_VEC containing the arguments. */
3884 args
= make_tree_vec (length
);
3886 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3888 tree a
= copy_node (TREE_VALUE (header
));
3891 TREE_TYPE (a
) = NULL_TREE
;
3892 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3893 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3895 #ifdef ENABLE_CHECKING
3896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3900 TREE_VEC_ELT (args
, --l
) = a
;
3905 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3906 /* This can happen for template parms of a template template
3909 template<template<class T, class U> class TT> struct S;
3911 Consider the level of the parms of TT; T and U both have
3912 level 2; TT has no template parm of level 1. So in this case
3913 the first element of full_template_args is NULL_TREE. If we
3914 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3915 of 2. This will make tsubst wrongly consider that T and U
3916 have level 1. Instead, let's create a dummy vector as the
3917 first element of full_template_args so that TMPL_ARGS_DEPTH
3918 returns the correct depth for args. */
3919 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3923 /* Within the declaration of a template, return the currently active
3924 template parameters as an argument TREE_VEC. */
3927 current_template_args (void)
3929 return template_parms_to_args (current_template_parms
);
3932 /* Update the declared TYPE by doing any lookups which were thought to be
3933 dependent, but are not now that we know the SCOPE of the declarator. */
3936 maybe_update_decl_type (tree orig_type
, tree scope
)
3938 tree type
= orig_type
;
3940 if (type
== NULL_TREE
)
3943 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3944 type
= TREE_TYPE (type
);
3946 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3947 && dependent_type_p (type
)
3948 /* Don't bother building up the args in this case. */
3949 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3951 /* tsubst in the args corresponding to the template parameters,
3952 including auto if present. Most things will be unchanged, but
3953 make_typename_type and tsubst_qualified_id will resolve
3954 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3955 tree args
= current_template_args ();
3956 tree auto_node
= type_uses_auto (type
);
3960 tree auto_vec
= make_tree_vec (1);
3961 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3962 args
= add_to_template_args (args
, auto_vec
);
3964 pushed
= push_scope (scope
);
3965 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3970 if (type
== error_mark_node
)
3973 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3975 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3978 type
= TYPE_NAME (type
);
3983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3984 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3985 a member template. Used by push_template_decl below. */
3988 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3990 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3991 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3992 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3993 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3994 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3999 struct template_parm_data
4001 /* The level of the template parameters we are currently
4005 /* The index of the specialization argument we are currently
4009 /* An array whose size is the number of template parameters. The
4010 elements are nonzero if the parameter has been used in any one
4011 of the arguments processed so far. */
4014 /* An array whose size is the number of template arguments. The
4015 elements are nonzero if the argument makes use of template
4016 parameters of this level. */
4017 int* arg_uses_template_parms
;
4020 /* Subroutine of push_template_decl used to see if each template
4021 parameter in a partial specialization is used in the explicit
4022 argument list. If T is of the LEVEL given in DATA (which is
4023 treated as a template_parm_data*), then DATA->PARMS is marked
4027 mark_template_parm (tree t
, void* data
)
4031 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4033 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4035 level
= TEMPLATE_PARM_LEVEL (t
);
4036 idx
= TEMPLATE_PARM_IDX (t
);
4040 level
= TEMPLATE_TYPE_LEVEL (t
);
4041 idx
= TEMPLATE_TYPE_IDX (t
);
4044 if (level
== tpd
->level
)
4046 tpd
->parms
[idx
] = 1;
4047 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4050 /* Return zero so that for_each_template_parm will continue the
4051 traversal of the tree; we want to mark *every* template parm. */
4055 /* Process the partial specialization DECL. */
4058 process_partial_specialization (tree decl
)
4060 tree type
= TREE_TYPE (decl
);
4061 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4062 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4063 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4064 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4067 int nargs
= TREE_VEC_LENGTH (inner_args
);
4070 bool did_error_intro
= false;
4071 struct template_parm_data tpd
;
4072 struct template_parm_data tpd2
;
4074 gcc_assert (current_template_parms
);
4076 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4077 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4079 /* We check that each of the template parameters given in the
4080 partial specialization is used in the argument list to the
4081 specialization. For example:
4083 template <class T> struct S;
4084 template <class T> struct S<T*>;
4086 The second declaration is OK because `T*' uses the template
4087 parameter T, whereas
4089 template <class T> struct S<int>;
4091 is no good. Even trickier is:
4102 The S2<T> declaration is actually invalid; it is a
4103 full-specialization. Of course,
4106 struct S2<T (*)(U)>;
4108 or some such would have been OK. */
4109 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4110 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4111 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4113 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4114 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4115 for (i
= 0; i
< nargs
; ++i
)
4117 tpd
.current_arg
= i
;
4118 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4119 &mark_template_parm
,
4122 /*include_nondeduced_p=*/false);
4124 for (i
= 0; i
< ntparms
; ++i
)
4125 if (tpd
.parms
[i
] == 0)
4127 /* One of the template parms was not used in the
4129 if (!did_error_intro
)
4131 error ("template parameters not used in partial specialization:");
4132 did_error_intro
= true;
4135 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4138 if (did_error_intro
)
4139 return error_mark_node
;
4141 /* [temp.class.spec]
4143 The argument list of the specialization shall not be identical to
4144 the implicit argument list of the primary template. */
4145 if (comp_template_args
4147 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4149 error ("partial specialization %qT does not specialize any template arguments", type
);
4151 /* A partial specialization that replaces multiple parameters of the
4152 primary template with a pack expansion is less specialized for those
4154 if (nargs
< DECL_NTPARMS (maintmpl
))
4156 error ("partial specialization is not more specialized than the "
4157 "primary template because it replaces multiple parameters "
4158 "with a pack expansion");
4159 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4163 /* [temp.class.spec]
4165 A partially specialized non-type argument expression shall not
4166 involve template parameters of the partial specialization except
4167 when the argument expression is a simple identifier.
4169 The type of a template parameter corresponding to a specialized
4170 non-type argument shall not be dependent on a parameter of the
4173 Also, we verify that pack expansions only occur at the
4174 end of the argument list. */
4175 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4177 for (i
= 0; i
< nargs
; ++i
)
4179 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4180 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4181 tree packed_args
= NULL_TREE
;
4184 if (ARGUMENT_PACK_P (arg
))
4186 /* Extract the arguments from the argument pack. We'll be
4187 iterating over these in the following loop. */
4188 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4189 len
= TREE_VEC_LENGTH (packed_args
);
4192 for (j
= 0; j
< len
; j
++)
4195 /* Get the Jth argument in the parameter pack. */
4196 arg
= TREE_VEC_ELT (packed_args
, j
);
4198 if (PACK_EXPANSION_P (arg
))
4200 /* Pack expansions must come at the end of the
4202 if ((packed_args
&& j
< len
- 1)
4203 || (!packed_args
&& i
< nargs
- 1))
4205 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4206 error ("parameter pack argument %qE must be at the "
4207 "end of the template argument list", arg
);
4209 error ("parameter pack argument %qT must be at the "
4210 "end of the template argument list", arg
);
4214 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4215 /* We only care about the pattern. */
4216 arg
= PACK_EXPANSION_PATTERN (arg
);
4218 if (/* These first two lines are the `non-type' bit. */
4220 && TREE_CODE (arg
) != TEMPLATE_DECL
4221 /* This next line is the `argument expression is not just a
4222 simple identifier' condition and also the `specialized
4223 non-type argument' bit. */
4224 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4226 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4227 || (packed_args
&& uses_template_parms (arg
)))
4228 error ("template argument %qE involves template parameter(s)",
4232 /* Look at the corresponding template parameter,
4233 marking which template parameters its type depends
4235 tree type
= TREE_TYPE (parm
);
4239 /* We haven't yet initialized TPD2. Do so now. */
4240 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4241 /* The number of parameters here is the number in the
4242 main template, which, as checked in the assertion
4244 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4246 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4249 /* Mark the template parameters. But this time, we're
4250 looking for the template parameters of the main
4251 template, not in the specialization. */
4252 tpd2
.current_arg
= i
;
4253 tpd2
.arg_uses_template_parms
[i
] = 0;
4254 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4255 for_each_template_parm (type
,
4256 &mark_template_parm
,
4259 /*include_nondeduced_p=*/false);
4261 if (tpd2
.arg_uses_template_parms
[i
])
4263 /* The type depended on some template parameters.
4264 If they are fully specialized in the
4265 specialization, that's OK. */
4268 for (j
= 0; j
< nargs
; ++j
)
4269 if (tpd2
.parms
[j
] != 0
4270 && tpd
.arg_uses_template_parms
[j
])
4273 error_n (input_location
, count
,
4274 "type %qT of template argument %qE depends "
4275 "on a template parameter",
4276 "type %qT of template argument %qE depends "
4277 "on template parameters",
4286 /* We should only get here once. */
4287 gcc_assert (!COMPLETE_TYPE_P (type
));
4289 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4290 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4291 TREE_TYPE (tmpl
) = type
;
4292 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4293 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4294 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4295 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4297 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4298 = tree_cons (specargs
, tmpl
,
4299 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4300 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4302 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4303 inst
= TREE_CHAIN (inst
))
4305 tree inst_type
= TREE_VALUE (inst
);
4306 if (COMPLETE_TYPE_P (inst_type
)
4307 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4309 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4310 if (spec
&& TREE_TYPE (spec
) == type
)
4311 permerror (input_location
,
4312 "partial specialization of %qT after instantiation "
4313 "of %qT", type
, inst_type
);
4320 /* Check that a template declaration's use of default arguments and
4321 parameter packs is not invalid. Here, PARMS are the template
4322 parameters. IS_PRIMARY is true if DECL is the thing declared by
4323 a primary template. IS_PARTIAL is true if DECL is a partial
4326 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4327 declaration (but not a definition); 1 indicates a declaration, 2
4328 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4329 emitted for extraneous default arguments.
4331 Returns TRUE if there were no errors found, FALSE otherwise. */
4334 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4335 bool is_partial
, int is_friend_decl
)
4338 int last_level_to_check
;
4340 bool no_errors
= true;
4344 A default template-argument shall not be specified in a
4345 function template declaration or a function template definition, nor
4346 in the template-parameter-list of the definition of a member of a
4349 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4350 /* You can't have a function template declaration in a local
4351 scope, nor you can you define a member of a class template in a
4355 if (TREE_CODE (decl
) == TYPE_DECL
4357 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4358 /* A lambda doesn't have an explicit declaration; don't complain
4359 about the parms of the enclosing class. */
4362 if (current_class_type
4363 && !TYPE_BEING_DEFINED (current_class_type
)
4364 && DECL_LANG_SPECIFIC (decl
)
4365 && DECL_DECLARES_FUNCTION_P (decl
)
4366 /* If this is either a friend defined in the scope of the class
4367 or a member function. */
4368 && (DECL_FUNCTION_MEMBER_P (decl
)
4369 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4370 : DECL_FRIEND_CONTEXT (decl
)
4371 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4373 /* And, if it was a member function, it really was defined in
4374 the scope of the class. */
4375 && (!DECL_FUNCTION_MEMBER_P (decl
)
4376 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4377 /* We already checked these parameters when the template was
4378 declared, so there's no need to do it again now. This function
4379 was defined in class scope, but we're processing its body now
4380 that the class is complete. */
4383 /* Core issue 226 (C++0x only): the following only applies to class
4386 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4390 If a template-parameter has a default template-argument, all
4391 subsequent template-parameters shall have a default
4392 template-argument supplied. */
4393 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4395 tree inner_parms
= TREE_VALUE (parm_level
);
4396 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4397 int seen_def_arg_p
= 0;
4400 for (i
= 0; i
< ntparms
; ++i
)
4402 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4404 if (parm
== error_mark_node
)
4407 if (TREE_PURPOSE (parm
))
4409 else if (seen_def_arg_p
4410 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4412 error ("no default argument for %qD", TREE_VALUE (parm
));
4413 /* For better subsequent error-recovery, we indicate that
4414 there should have been a default argument. */
4415 TREE_PURPOSE (parm
) = error_mark_node
;
4418 else if (!is_partial
4420 /* Don't complain about an enclosing partial
4422 && parm_level
== parms
4423 && TREE_CODE (decl
) == TYPE_DECL
4425 && template_parameter_pack_p (TREE_VALUE (parm
)))
4427 /* A primary class template can only have one
4428 parameter pack, at the end of the template
4431 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4432 error ("parameter pack %qE must be at the end of the"
4433 " template parameter list", TREE_VALUE (parm
));
4435 error ("parameter pack %qT must be at the end of the"
4436 " template parameter list",
4437 TREE_TYPE (TREE_VALUE (parm
)));
4439 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4447 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4451 /* For an ordinary class template, default template arguments are
4452 allowed at the innermost level, e.g.:
4453 template <class T = int>
4455 but, in a partial specialization, they're not allowed even
4456 there, as we have in [temp.class.spec]:
4458 The template parameter list of a specialization shall not
4459 contain default template argument values.
4461 So, for a partial specialization, or for a function template
4462 (in C++98/C++03), we look at all of them. */
4465 /* But, for a primary class template that is not a partial
4466 specialization we look at all template parameters except the
4468 parms
= TREE_CHAIN (parms
);
4470 /* Figure out what error message to issue. */
4471 if (is_friend_decl
== 2)
4472 msg
= G_("default template arguments may not be used in function template "
4473 "friend re-declaration");
4474 else if (is_friend_decl
)
4475 msg
= G_("default template arguments may not be used in function template "
4476 "friend declarations");
4477 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4478 msg
= G_("default template arguments may not be used in function templates "
4479 "without -std=c++11 or -std=gnu++11");
4480 else if (is_partial
)
4481 msg
= G_("default template arguments may not be used in "
4482 "partial specializations");
4484 msg
= G_("default argument for template parameter for class enclosing %qD");
4486 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4487 /* If we're inside a class definition, there's no need to
4488 examine the parameters to the class itself. On the one
4489 hand, they will be checked when the class is defined, and,
4490 on the other, default arguments are valid in things like:
4491 template <class T = double>
4492 struct S { template <class U> void f(U); };
4493 Here the default argument for `S' has no bearing on the
4494 declaration of `f'. */
4495 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4497 /* Check everything. */
4498 last_level_to_check
= 0;
4500 for (parm_level
= parms
;
4501 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4502 parm_level
= TREE_CHAIN (parm_level
))
4504 tree inner_parms
= TREE_VALUE (parm_level
);
4508 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4509 for (i
= 0; i
< ntparms
; ++i
)
4511 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4514 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4519 if (is_friend_decl
== 2)
4526 /* Clear out the default argument so that we are not
4528 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4532 /* At this point, if we're still interested in issuing messages,
4533 they must apply to classes surrounding the object declared. */
4535 msg
= G_("default argument for template parameter for class "
4542 /* Worker for push_template_decl_real, called via
4543 for_each_template_parm. DATA is really an int, indicating the
4544 level of the parameters we are interested in. If T is a template
4545 parameter of that level, return nonzero. */
4548 template_parm_this_level_p (tree t
, void* data
)
4550 int this_level
= *(int *)data
;
4553 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4554 level
= TEMPLATE_PARM_LEVEL (t
);
4556 level
= TEMPLATE_TYPE_LEVEL (t
);
4557 return level
== this_level
;
4560 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4561 parameters given by current_template_args, or reuses a
4562 previously existing one, if appropriate. Returns the DECL, or an
4563 equivalent one, if it is replaced via a call to duplicate_decls.
4565 If IS_FRIEND is true, DECL is a friend declaration. */
4568 push_template_decl_real (tree decl
, bool is_friend
)
4576 int new_template_p
= 0;
4577 /* True if the template is a member template, in the sense of
4579 bool member_template_p
= false;
4581 if (decl
== error_mark_node
|| !current_template_parms
)
4582 return error_mark_node
;
4584 /* See if this is a partial specialization. */
4585 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4586 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4587 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4589 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4593 /* For a friend, we want the context of the friend function, not
4594 the type of which it is a friend. */
4595 ctx
= CP_DECL_CONTEXT (decl
);
4596 else if (CP_DECL_CONTEXT (decl
)
4597 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4598 /* In the case of a virtual function, we want the class in which
4600 ctx
= CP_DECL_CONTEXT (decl
);
4602 /* Otherwise, if we're currently defining some class, the DECL
4603 is assumed to be a member of the class. */
4604 ctx
= current_scope ();
4606 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4609 if (!DECL_CONTEXT (decl
))
4610 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4612 /* See if this is a primary template. */
4613 if (is_friend
&& ctx
)
4614 /* A friend template that specifies a class context, i.e.
4615 template <typename T> friend void A<T>::f();
4619 is_primary
= template_parm_scope_p ();
4623 if (DECL_CLASS_SCOPE_P (decl
))
4624 member_template_p
= true;
4625 if (TREE_CODE (decl
) == TYPE_DECL
4626 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4628 error ("template class without a name");
4629 return error_mark_node
;
4631 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4633 if (DECL_DESTRUCTOR_P (decl
))
4637 A destructor shall not be a member template. */
4638 error ("destructor %qD declared as member template", decl
);
4639 return error_mark_node
;
4641 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4642 && (!prototype_p (TREE_TYPE (decl
))
4643 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4644 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4645 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4646 == void_list_node
)))
4648 /* [basic.stc.dynamic.allocation]
4650 An allocation function can be a function
4651 template. ... Template allocation functions shall
4652 have two or more parameters. */
4653 error ("invalid template declaration of %qD", decl
);
4654 return error_mark_node
;
4657 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4658 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4660 else if (TREE_CODE (decl
) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl
))
4662 /* alias-declaration */
4663 gcc_assert (!DECL_ARTIFICIAL (decl
));
4666 error ("template declaration of %q#D", decl
);
4667 return error_mark_node
;
4671 /* Check to see that the rules regarding the use of default
4672 arguments are not being violated. */
4673 check_default_tmpl_args (decl
, current_template_parms
,
4674 is_primary
, is_partial
, /*is_friend_decl=*/0);
4676 /* Ensure that there are no parameter packs in the type of this
4677 declaration that have not been expanded. */
4678 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4680 /* Check each of the arguments individually to see if there are
4681 any bare parameter packs. */
4682 tree type
= TREE_TYPE (decl
);
4683 tree arg
= DECL_ARGUMENTS (decl
);
4684 tree argtype
= TYPE_ARG_TYPES (type
);
4686 while (arg
&& argtype
)
4688 if (!DECL_PACK_P (arg
)
4689 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4691 /* This is a PARM_DECL that contains unexpanded parameter
4692 packs. We have already complained about this in the
4693 check_for_bare_parameter_packs call, so just replace
4694 these types with ERROR_MARK_NODE. */
4695 TREE_TYPE (arg
) = error_mark_node
;
4696 TREE_VALUE (argtype
) = error_mark_node
;
4699 arg
= DECL_CHAIN (arg
);
4700 argtype
= TREE_CHAIN (argtype
);
4703 /* Check for bare parameter packs in the return type and the
4704 exception specifiers. */
4705 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4706 /* Errors were already issued, set return type to int
4707 as the frontend doesn't expect error_mark_node as
4709 TREE_TYPE (type
) = integer_type_node
;
4710 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4711 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4713 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4714 && TYPE_DECL_ALIAS_P (decl
))
4715 ? DECL_ORIGINAL_TYPE (decl
)
4716 : TREE_TYPE (decl
)))
4718 TREE_TYPE (decl
) = error_mark_node
;
4719 return error_mark_node
;
4723 return process_partial_specialization (decl
);
4725 args
= current_template_args ();
4728 || TREE_CODE (ctx
) == FUNCTION_DECL
4729 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4730 || (TREE_CODE (decl
) == TYPE_DECL
4731 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4732 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4734 if (DECL_LANG_SPECIFIC (decl
)
4735 && DECL_TEMPLATE_INFO (decl
)
4736 && DECL_TI_TEMPLATE (decl
))
4737 tmpl
= DECL_TI_TEMPLATE (decl
);
4738 /* If DECL is a TYPE_DECL for a class-template, then there won't
4739 be DECL_LANG_SPECIFIC. The information equivalent to
4740 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4741 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4742 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4743 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4745 /* Since a template declaration already existed for this
4746 class-type, we must be redeclaring it here. Make sure
4747 that the redeclaration is valid. */
4748 redeclare_class_template (TREE_TYPE (decl
),
4749 current_template_parms
);
4750 /* We don't need to create a new TEMPLATE_DECL; just use the
4751 one we already had. */
4752 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4756 tmpl
= build_template_decl (decl
, current_template_parms
,
4760 if (DECL_LANG_SPECIFIC (decl
)
4761 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4763 /* A specialization of a member template of a template
4765 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4766 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4767 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4773 tree a
, t
, current
, parms
;
4775 tree tinfo
= get_template_info (decl
);
4779 error ("template definition of non-template %q#D", decl
);
4780 return error_mark_node
;
4783 tmpl
= TI_TEMPLATE (tinfo
);
4785 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4786 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4787 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4788 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4792 /* The declaration is a specialization of a member
4793 template, declared outside the class. Therefore, the
4794 innermost template arguments will be NULL, so we
4795 replace them with the arguments determined by the
4796 earlier call to check_explicit_specialization. */
4797 args
= DECL_TI_ARGS (decl
);
4800 = build_template_decl (decl
, current_template_parms
,
4802 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4803 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4804 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4805 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4806 DECL_TEMPLATE_INFO (new_tmpl
)
4807 = build_template_info (tmpl
, args
);
4809 register_specialization (new_tmpl
,
4810 most_general_template (tmpl
),
4816 /* Make sure the template headers we got make sense. */
4818 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4819 i
= TMPL_PARMS_DEPTH (parms
);
4820 if (TMPL_ARGS_DEPTH (args
) != i
)
4822 error ("expected %d levels of template parms for %q#D, got %d",
4823 i
, decl
, TMPL_ARGS_DEPTH (args
));
4826 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4828 a
= TMPL_ARGS_LEVEL (args
, i
);
4829 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4831 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4833 if (current
== decl
)
4834 error ("got %d template parameters for %q#D",
4835 TREE_VEC_LENGTH (a
), decl
);
4837 error ("got %d template parameters for %q#T",
4838 TREE_VEC_LENGTH (a
), current
);
4839 error (" but %d required", TREE_VEC_LENGTH (t
));
4840 /* Avoid crash in import_export_decl. */
4841 DECL_INTERFACE_KNOWN (decl
) = 1;
4842 return error_mark_node
;
4845 if (current
== decl
)
4847 else if (current
== NULL_TREE
)
4848 /* Can happen in erroneous input. */
4851 current
= get_containing_scope (current
);
4854 /* Check that the parms are used in the appropriate qualifying scopes
4855 in the declarator. */
4856 if (!comp_template_args
4858 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4861 template arguments to %qD do not match original template %qD",
4862 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4863 if (!uses_template_parms (TI_ARGS (tinfo
)))
4864 inform (input_location
, "use template<> for an explicit specialization");
4865 /* Avoid crash in import_export_decl. */
4866 DECL_INTERFACE_KNOWN (decl
) = 1;
4867 return error_mark_node
;
4871 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4872 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4874 /* Push template declarations for global functions and types. Note
4875 that we do not try to push a global template friend declared in a
4876 template class; such a thing may well depend on the template
4877 parameters of the class. */
4878 if (new_template_p
&& !ctx
4879 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4881 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4882 if (tmpl
== error_mark_node
)
4883 return error_mark_node
;
4885 /* Hide template friend classes that haven't been declared yet. */
4886 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4888 DECL_ANTICIPATED (tmpl
) = 1;
4889 DECL_FRIEND_P (tmpl
) = 1;
4895 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4898 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4899 if (DECL_CONV_FN_P (tmpl
))
4901 int depth
= TMPL_PARMS_DEPTH (parms
);
4903 /* It is a conversion operator. See if the type converted to
4904 depends on innermost template operands. */
4906 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4908 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4911 /* Give template template parms a DECL_CONTEXT of the template
4912 for which they are a parameter. */
4913 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4914 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4916 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4917 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4918 DECL_CONTEXT (parm
) = tmpl
;
4922 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4923 back to its most general template. If TMPL is a specialization,
4924 ARGS may only have the innermost set of arguments. Add the missing
4925 argument levels if necessary. */
4926 if (DECL_TEMPLATE_INFO (tmpl
))
4927 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4929 info
= build_template_info (tmpl
, args
);
4931 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4932 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4935 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4936 retrofit_lang_decl (decl
);
4937 if (DECL_LANG_SPECIFIC (decl
))
4938 DECL_TEMPLATE_INFO (decl
) = info
;
4941 return DECL_TEMPLATE_RESULT (tmpl
);
4945 push_template_decl (tree decl
)
4947 return push_template_decl_real (decl
, false);
4950 /* FN is an inheriting constructor that inherits from the constructor
4951 template INHERITED; turn FN into a constructor template with a matching
4955 add_inherited_template_parms (tree fn
, tree inherited
)
4958 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4959 inner_parms
= copy_node (inner_parms
);
4961 = tree_cons (size_int (processing_template_decl
+ 1),
4962 inner_parms
, current_template_parms
);
4963 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4964 tree args
= template_parms_to_args (parms
);
4965 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4966 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4967 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4968 DECL_ARTIFICIAL (tmpl
) = true;
4969 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4973 /* Called when a class template TYPE is redeclared with the indicated
4974 template PARMS, e.g.:
4976 template <class T> struct S;
4977 template <class T> struct S {}; */
4980 redeclare_class_template (tree type
, tree parms
)
4986 if (!TYPE_TEMPLATE_INFO (type
))
4988 error ("%qT is not a template type", type
);
4992 tmpl
= TYPE_TI_TEMPLATE (type
);
4993 if (!PRIMARY_TEMPLATE_P (tmpl
))
4994 /* The type is nested in some template class. Nothing to worry
4995 about here; there are no new template parameters for the nested
5001 error ("template specifiers not specified in declaration of %qD",
5006 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5007 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5009 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5011 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5012 "redeclared with %d template parameter",
5013 "redeclared with %d template parameters",
5014 TREE_VEC_LENGTH (parms
));
5015 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5016 "previous declaration %q+D used %d template parameter",
5017 "previous declaration %q+D used %d template parameters",
5018 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5022 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5029 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5030 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5033 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5034 if (tmpl_parm
== error_mark_node
)
5037 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5038 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5039 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5041 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5043 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5044 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5045 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5046 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5047 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5048 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5049 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5050 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5051 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5053 error ("template parameter %q+#D", tmpl_parm
);
5054 error ("redeclared here as %q#D", parm
);
5058 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5060 /* We have in [temp.param]:
5062 A template-parameter may not be given default arguments
5063 by two different declarations in the same scope. */
5064 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5065 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5066 "original definition appeared here");
5070 if (parm_default
!= NULL_TREE
)
5071 /* Update the previous template parameters (which are the ones
5072 that will really count) with the new default value. */
5073 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5074 else if (tmpl_default
!= NULL_TREE
)
5075 /* Update the new parameters, too; they'll be used as the
5076 parameters for any members. */
5077 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5083 /* Simplify EXPR if it is a non-dependent expression. Returns the
5084 (possibly simplified) expression. */
5087 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5089 if (expr
== NULL_TREE
)
5092 /* If we're in a template, but EXPR isn't value dependent, simplify
5093 it. We're supposed to treat:
5095 template <typename T> void f(T[1 + 1]);
5096 template <typename T> void f(T[2]);
5098 as two declarations of the same function, for example. */
5099 if (processing_template_decl
5100 && !instantiation_dependent_expression_p (expr
)
5101 && potential_constant_expression (expr
))
5103 HOST_WIDE_INT saved_processing_template_decl
;
5105 saved_processing_template_decl
= processing_template_decl
;
5106 processing_template_decl
= 0;
5107 expr
= tsubst_copy_and_build (expr
,
5110 /*in_decl=*/NULL_TREE
,
5111 /*function_p=*/false,
5112 /*integral_constant_expression_p=*/true);
5113 processing_template_decl
= saved_processing_template_decl
;
5119 fold_non_dependent_expr (tree expr
)
5121 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5124 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5125 template declaration, or a TYPE_DECL for an alias declaration. */
5128 alias_type_or_template_p (tree t
)
5132 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5135 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5136 || DECL_ALIAS_TEMPLATE_P (t
));
5139 /* Return TRUE iff is a specialization of an alias template. */
5142 alias_template_specialization_p (const_tree t
)
5148 && TYPE_TEMPLATE_INFO (t
)
5149 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5150 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5153 /* Return either TMPL or another template that it is equivalent to under DR
5154 1286: An alias that just changes the name of a template is equivalent to
5155 the other template. */
5158 get_underlying_template (tree tmpl
)
5160 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5161 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5163 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5164 if (TYPE_TEMPLATE_INFO (result
))
5166 tree sub
= TYPE_TI_TEMPLATE (result
);
5167 if (PRIMARY_TEMPLATE_P (sub
)
5168 && same_type_p (result
, TREE_TYPE (sub
)))
5170 /* The alias type is equivalent to the pattern of the
5171 underlying template, so strip the alias. */
5181 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5182 must be a function or a pointer-to-function type, as specified
5183 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5184 and check that the resulting function has external linkage. */
5187 convert_nontype_argument_function (tree type
, tree expr
)
5191 linkage_kind linkage
;
5193 fn
= instantiate_type (type
, fns
, tf_none
);
5194 if (fn
== error_mark_node
)
5195 return error_mark_node
;
5198 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5199 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5200 if (BASELINK_P (fn_no_ptr
))
5201 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5203 /* [temp.arg.nontype]/1
5205 A template-argument for a non-type, non-template template-parameter
5208 -- the address of an object or function with external [C++11: or
5209 internal] linkage. */
5211 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5213 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5214 if (TYPE_PTR_P (type
))
5215 error ("it must be the address of a function with external linkage");
5217 error ("it must be the name of a function with external linkage");
5221 linkage
= decl_linkage (fn_no_ptr
);
5222 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5224 if (cxx_dialect
>= cxx11
)
5225 error ("%qE is not a valid template argument for type %qT "
5226 "because %qD has no linkage",
5227 expr
, type
, fn_no_ptr
);
5229 error ("%qE is not a valid template argument for type %qT "
5230 "because %qD does not have external linkage",
5231 expr
, type
, fn_no_ptr
);
5238 /* Subroutine of convert_nontype_argument.
5239 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5240 Emit an error otherwise. */
5243 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5244 tsubst_flags_t complain
)
5247 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5249 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5251 if (complain
& tf_error
)
5253 error ("%qE is not a valid template argument for type %qT",
5255 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5260 /* Returns TRUE iff the address of OP is value-dependent.
5262 14.6.2.4 [temp.dep.temp]:
5263 A non-integral non-type template-argument is dependent if its type is
5264 dependent or it has either of the following forms
5267 and contains a nested-name-specifier which specifies a class-name that
5268 names a dependent type.
5270 We generalize this to just say that the address of a member of a
5271 dependent class is value-dependent; the above doesn't cover the
5272 address of a static data member named with an unqualified-id. */
5275 has_value_dependent_address (tree op
)
5277 /* We could use get_inner_reference here, but there's no need;
5278 this is only relevant for template non-type arguments, which
5279 can only be expressed as &id-expression. */
5282 tree ctx
= CP_DECL_CONTEXT (op
);
5283 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5290 /* The next set of functions are used for providing helpful explanatory
5291 diagnostics for failed overload resolution. Their messages should be
5292 indented by two spaces for consistency with the messages in
5296 unify_success (bool /*explain_p*/)
5302 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5305 inform (input_location
,
5306 " couldn't deduce template parameter %qD", parm
);
5311 unify_invalid (bool /*explain_p*/)
5317 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5320 inform (input_location
,
5321 " types %qT and %qT have incompatible cv-qualifiers",
5327 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5330 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5335 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5338 inform (input_location
,
5339 " template parameter %qD is not a parameter pack, but "
5346 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5349 inform (input_location
,
5350 " template argument %qE does not match "
5351 "pointer-to-member constant %qE",
5357 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5360 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5365 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5368 inform (input_location
,
5369 " inconsistent parameter pack deduction with %qT and %qT",
5375 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5380 inform (input_location
,
5381 " deduced conflicting types for parameter %qT (%qT and %qT)",
5382 parm
, first
, second
);
5384 inform (input_location
,
5385 " deduced conflicting values for non-type parameter "
5386 "%qE (%qE and %qE)", parm
, first
, second
);
5392 unify_vla_arg (bool explain_p
, tree arg
)
5395 inform (input_location
,
5396 " variable-sized array type %qT is not "
5397 "a valid template argument",
5403 unify_method_type_error (bool explain_p
, tree arg
)
5406 inform (input_location
,
5407 " member function type %qT is not a valid template argument",
5413 unify_arity (bool explain_p
, int have
, int wanted
)
5416 inform_n (input_location
, wanted
,
5417 " candidate expects %d argument, %d provided",
5418 " candidate expects %d arguments, %d provided",
5424 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5426 return unify_arity (explain_p
, have
, wanted
);
5430 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5432 return unify_arity (explain_p
, have
, wanted
);
5436 unify_arg_conversion (bool explain_p
, tree to_type
,
5437 tree from_type
, tree arg
)
5440 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5441 " cannot convert %qE (type %qT) to type %qT",
5442 arg
, from_type
, to_type
);
5447 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5448 tree parm
, tree arg
)
5453 case tbr_ambiguous_baseclass
:
5454 inform (input_location
, " %qT is an ambiguous base class of %qT",
5458 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5465 unify_inconsistent_template_template_parameters (bool explain_p
)
5468 inform (input_location
,
5469 " template parameters of a template template argument are "
5470 "inconsistent with other deduced template arguments");
5475 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5478 inform (input_location
,
5479 " can't deduce a template for %qT from non-template type %qT",
5485 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5488 inform (input_location
,
5489 " template argument %qE does not match %qD", arg
, parm
);
5494 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5497 inform (input_location
,
5498 " could not resolve address from overloaded function %qE",
5503 /* Attempt to convert the non-type template parameter EXPR to the
5504 indicated TYPE. If the conversion is successful, return the
5505 converted value. If the conversion is unsuccessful, return
5506 NULL_TREE if we issued an error message, or error_mark_node if we
5507 did not. We issue error messages for out-and-out bad template
5508 parameters, but not simply because the conversion failed, since we
5509 might be just trying to do argument deduction. Both TYPE and EXPR
5510 must be non-dependent.
5512 The conversion follows the special rules described in
5513 [temp.arg.nontype], and it is much more strict than an implicit
5516 This function is called twice for each template argument (see
5517 lookup_template_class for a more accurate description of this
5518 problem). This means that we need to handle expressions which
5519 are not valid in a C++ source, but can be created from the
5520 first call (for instance, casts to perform conversions). These
5521 hacks can go away after we fix the double coercion problem. */
5524 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5528 /* Detect immediately string literals as invalid non-type argument.
5529 This special-case is not needed for correctness (we would easily
5530 catch this later), but only to provide better diagnostic for this
5531 common user mistake. As suggested by DR 100, we do not mention
5532 linkage issues in the diagnostic as this is not the point. */
5533 /* FIXME we're making this OK. */
5534 if (TREE_CODE (expr
) == STRING_CST
)
5536 if (complain
& tf_error
)
5537 error ("%qE is not a valid template argument for type %qT "
5538 "because string literals can never be used in this context",
5543 /* Add the ADDR_EXPR now for the benefit of
5544 value_dependent_expression_p. */
5545 if (TYPE_PTROBV_P (type
)
5546 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5548 expr
= decay_conversion (expr
, complain
);
5549 if (expr
== error_mark_node
)
5550 return error_mark_node
;
5553 /* If we are in a template, EXPR may be non-dependent, but still
5554 have a syntactic, rather than semantic, form. For example, EXPR
5555 might be a SCOPE_REF, rather than the VAR_DECL to which the
5556 SCOPE_REF refers. Preserving the qualifying scope is necessary
5557 so that access checking can be performed when the template is
5558 instantiated -- but here we need the resolved form so that we can
5559 convert the argument. */
5560 if (TYPE_REF_OBJ_P (type
)
5561 && has_value_dependent_address (expr
))
5562 /* If we want the address and it's value-dependent, don't fold. */;
5563 else if (!type_unknown_p (expr
))
5564 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5565 if (error_operand_p (expr
))
5566 return error_mark_node
;
5567 expr_type
= TREE_TYPE (expr
);
5568 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5569 expr
= mark_lvalue_use (expr
);
5571 expr
= mark_rvalue_use (expr
);
5573 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5574 to a non-type argument of "nullptr". */
5575 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5576 expr
= convert (type
, expr
);
5578 /* In C++11, integral or enumeration non-type template arguments can be
5579 arbitrary constant expressions. Pointer and pointer to
5580 member arguments can be general constant expressions that evaluate
5581 to a null value, but otherwise still need to be of a specific form. */
5582 if (cxx_dialect
>= cxx11
)
5584 if (TREE_CODE (expr
) == PTRMEM_CST
)
5585 /* A PTRMEM_CST is already constant, and a valid template
5586 argument for a parameter of pointer to member type, we just want
5587 to leave it in that form rather than lower it to a
5589 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5590 expr
= maybe_constant_value (expr
);
5591 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5593 tree folded
= maybe_constant_value (expr
);
5594 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5595 : null_member_pointer_value_p (folded
))
5600 /* HACK: Due to double coercion, we can get a
5601 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5602 which is the tree that we built on the first call (see
5603 below when coercing to reference to object or to reference to
5604 function). We just strip everything and get to the arg.
5605 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5607 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5609 tree probe_type
, probe
= expr
;
5610 if (REFERENCE_REF_P (probe
))
5611 probe
= TREE_OPERAND (probe
, 0);
5612 probe_type
= TREE_TYPE (probe
);
5613 if (TREE_CODE (probe
) == NOP_EXPR
)
5615 /* ??? Maybe we could use convert_from_reference here, but we
5616 would need to relax its constraints because the NOP_EXPR
5617 could actually change the type to something more cv-qualified,
5618 and this is not folded by convert_from_reference. */
5619 tree addr
= TREE_OPERAND (probe
, 0);
5620 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5621 && TREE_CODE (addr
) == ADDR_EXPR
5622 && TYPE_PTR_P (TREE_TYPE (addr
))
5623 && (same_type_ignoring_top_level_qualifiers_p
5624 (TREE_TYPE (probe_type
),
5625 TREE_TYPE (TREE_TYPE (addr
)))))
5627 expr
= TREE_OPERAND (addr
, 0);
5628 expr_type
= TREE_TYPE (probe_type
);
5633 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5634 parameter is a pointer to object, through decay and
5635 qualification conversion. Let's strip everything. */
5636 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5640 if (TREE_CODE (probe
) == ADDR_EXPR
5641 && TYPE_PTR_P (TREE_TYPE (probe
)))
5643 /* Skip the ADDR_EXPR only if it is part of the decay for
5644 an array. Otherwise, it is part of the original argument
5645 in the source code. */
5646 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5647 probe
= TREE_OPERAND (probe
, 0);
5649 expr_type
= TREE_TYPE (expr
);
5653 /* [temp.arg.nontype]/5, bullet 1
5655 For a non-type template-parameter of integral or enumeration type,
5656 integral promotions (_conv.prom_) and integral conversions
5657 (_conv.integral_) are applied. */
5658 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5660 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5661 t
= maybe_constant_value (t
);
5662 if (t
!= error_mark_node
)
5665 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5666 return error_mark_node
;
5668 /* Notice that there are constant expressions like '4 % 0' which
5669 do not fold into integer constants. */
5670 if (TREE_CODE (expr
) != INTEGER_CST
)
5672 if (complain
& tf_error
)
5674 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5675 if (processing_template_decl
5676 && !require_potential_constant_expression (expr
))
5678 expr
= cxx_constant_value (expr
);
5679 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5680 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5681 "in template argument for type %qT ", type
);
5682 if (expr
== error_mark_node
)
5684 /* else cxx_constant_value complained but gave us
5685 a real constant, so go ahead. */
5686 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5692 /* Avoid typedef problems. */
5693 if (TREE_TYPE (expr
) != type
)
5694 expr
= fold_convert (type
, expr
);
5696 /* [temp.arg.nontype]/5, bullet 2
5698 For a non-type template-parameter of type pointer to object,
5699 qualification conversions (_conv.qual_) and the array-to-pointer
5700 conversion (_conv.array_) are applied. */
5701 else if (TYPE_PTROBV_P (type
))
5703 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5705 A template-argument for a non-type, non-template template-parameter
5706 shall be one of: [...]
5708 -- the name of a non-type template-parameter;
5709 -- the address of an object or function with external linkage, [...]
5710 expressed as "& id-expression" where the & is optional if the name
5711 refers to a function or array, or if the corresponding
5712 template-parameter is a reference.
5714 Here, we do not care about functions, as they are invalid anyway
5715 for a parameter of type pointer-to-object. */
5717 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5718 /* Non-type template parameters are OK. */
5720 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5721 /* Null pointer values are OK in C++11. */;
5722 else if (TREE_CODE (expr
) != ADDR_EXPR
5723 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5727 error ("%qD is not a valid template argument "
5728 "because %qD is a variable, not the address of "
5733 if (POINTER_TYPE_P (expr_type
))
5735 error ("%qE is not a valid template argument for %qT "
5736 "because it is not the address of a variable",
5740 /* Other values, like integer constants, might be valid
5741 non-type arguments of some other type. */
5742 return error_mark_node
;
5748 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5749 ? TREE_OPERAND (expr
, 0) : expr
);
5752 error ("%qE is not a valid template argument of type %qT "
5753 "because %qE is not a variable",
5757 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5759 error ("%qE is not a valid template argument of type %qT "
5760 "because %qD does not have external linkage",
5764 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5766 error ("%qE is not a valid template argument of type %qT "
5767 "because %qD has no linkage",
5773 expr
= decay_conversion (expr
, complain
);
5774 if (expr
== error_mark_node
)
5775 return error_mark_node
;
5777 expr
= perform_qualification_conversions (type
, expr
);
5778 if (expr
== error_mark_node
)
5779 return error_mark_node
;
5781 /* [temp.arg.nontype]/5, bullet 3
5783 For a non-type template-parameter of type reference to object, no
5784 conversions apply. The type referred to by the reference may be more
5785 cv-qualified than the (otherwise identical) type of the
5786 template-argument. The template-parameter is bound directly to the
5787 template-argument, which must be an lvalue. */
5788 else if (TYPE_REF_OBJ_P (type
))
5790 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5792 return error_mark_node
;
5794 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5796 error ("%qE is not a valid template argument for type %qT "
5797 "because of conflicts in cv-qualification", expr
, type
);
5801 if (!real_lvalue_p (expr
))
5803 error ("%qE is not a valid template argument for type %qT "
5804 "because it is not an lvalue", expr
, type
);
5808 /* [temp.arg.nontype]/1
5810 A template-argument for a non-type, non-template template-parameter
5811 shall be one of: [...]
5813 -- the address of an object or function with external linkage. */
5814 if (INDIRECT_REF_P (expr
)
5815 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5817 expr
= TREE_OPERAND (expr
, 0);
5820 error ("%q#D is not a valid template argument for type %qT "
5821 "because a reference variable does not have a constant "
5822 "address", expr
, type
);
5829 error ("%qE is not a valid template argument for type %qT "
5830 "because it is not an object with external linkage",
5835 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5837 error ("%qE is not a valid template argument for type %qT "
5838 "because object %qD has not external linkage",
5843 expr
= build_nop (type
, build_address (expr
));
5845 /* [temp.arg.nontype]/5, bullet 4
5847 For a non-type template-parameter of type pointer to function, only
5848 the function-to-pointer conversion (_conv.func_) is applied. If the
5849 template-argument represents a set of overloaded functions (or a
5850 pointer to such), the matching function is selected from the set
5852 else if (TYPE_PTRFN_P (type
))
5854 /* If the argument is a template-id, we might not have enough
5855 context information to decay the pointer. */
5856 if (!type_unknown_p (expr_type
))
5858 expr
= decay_conversion (expr
, complain
);
5859 if (expr
== error_mark_node
)
5860 return error_mark_node
;
5863 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5864 /* Null pointer values are OK in C++11. */
5865 return perform_qualification_conversions (type
, expr
);
5867 expr
= convert_nontype_argument_function (type
, expr
);
5868 if (!expr
|| expr
== error_mark_node
)
5871 /* [temp.arg.nontype]/5, bullet 5
5873 For a non-type template-parameter of type reference to function, no
5874 conversions apply. If the template-argument represents a set of
5875 overloaded functions, the matching function is selected from the set
5877 else if (TYPE_REFFN_P (type
))
5879 if (TREE_CODE (expr
) == ADDR_EXPR
)
5881 error ("%qE is not a valid template argument for type %qT "
5882 "because it is a pointer", expr
, type
);
5883 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5887 expr
= convert_nontype_argument_function (type
, expr
);
5888 if (!expr
|| expr
== error_mark_node
)
5891 expr
= build_nop (type
, build_address (expr
));
5893 /* [temp.arg.nontype]/5, bullet 6
5895 For a non-type template-parameter of type pointer to member function,
5896 no conversions apply. If the template-argument represents a set of
5897 overloaded member functions, the matching member function is selected
5898 from the set (_over.over_). */
5899 else if (TYPE_PTRMEMFUNC_P (type
))
5901 expr
= instantiate_type (type
, expr
, tf_none
);
5902 if (expr
== error_mark_node
)
5903 return error_mark_node
;
5905 /* [temp.arg.nontype] bullet 1 says the pointer to member
5906 expression must be a pointer-to-member constant. */
5907 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5908 return error_mark_node
;
5910 /* There is no way to disable standard conversions in
5911 resolve_address_of_overloaded_function (called by
5912 instantiate_type). It is possible that the call succeeded by
5913 converting &B::I to &D::I (where B is a base of D), so we need
5914 to reject this conversion here.
5916 Actually, even if there was a way to disable standard conversions,
5917 it would still be better to reject them here so that we can
5918 provide a superior diagnostic. */
5919 if (!same_type_p (TREE_TYPE (expr
), type
))
5921 error ("%qE is not a valid template argument for type %qT "
5922 "because it is of type %qT", expr
, type
,
5924 /* If we are just one standard conversion off, explain. */
5925 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5926 inform (input_location
,
5927 "standard conversions are not allowed in this context");
5931 /* [temp.arg.nontype]/5, bullet 7
5933 For a non-type template-parameter of type pointer to data member,
5934 qualification conversions (_conv.qual_) are applied. */
5935 else if (TYPE_PTRDATAMEM_P (type
))
5937 /* [temp.arg.nontype] bullet 1 says the pointer to member
5938 expression must be a pointer-to-member constant. */
5939 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5940 return error_mark_node
;
5942 expr
= perform_qualification_conversions (type
, expr
);
5943 if (expr
== error_mark_node
)
5946 else if (NULLPTR_TYPE_P (type
))
5948 if (expr
!= nullptr_node
)
5950 error ("%qE is not a valid template argument for type %qT "
5951 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5956 /* A template non-type parameter must be one of the above. */
5960 /* Sanity check: did we actually convert the argument to the
5962 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5963 (type
, TREE_TYPE (expr
)));
5967 /* Subroutine of coerce_template_template_parms, which returns 1 if
5968 PARM_PARM and ARG_PARM match using the rule for the template
5969 parameters of template template parameters. Both PARM and ARG are
5970 template parameters; the rest of the arguments are the same as for
5971 coerce_template_template_parms.
5974 coerce_template_template_parm (tree parm
,
5976 tsubst_flags_t complain
,
5980 if (arg
== NULL_TREE
|| arg
== error_mark_node
5981 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5984 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5987 switch (TREE_CODE (parm
))
5990 /* We encounter instantiations of templates like
5991 template <template <template <class> class> class TT>
5994 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5995 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5997 if (!coerce_template_template_parms
5998 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6004 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6005 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6006 /* Argument is a parameter pack but parameter is not. */
6011 /* The tsubst call is used to handle cases such as
6013 template <int> class C {};
6014 template <class T, template <T> class TT> class D {};
6017 i.e. the parameter list of TT depends on earlier parameters. */
6018 if (!uses_template_parms (TREE_TYPE (arg
))
6020 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6024 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6025 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6026 /* Argument is a parameter pack but parameter is not. */
6039 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6040 template template parameters. Both PARM_PARMS and ARG_PARMS are
6041 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6044 Consider the example:
6045 template <class T> class A;
6046 template<template <class U> class TT> class B;
6048 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6049 the parameters to A, and OUTER_ARGS contains A. */
6052 coerce_template_template_parms (tree parm_parms
,
6054 tsubst_flags_t complain
,
6058 int nparms
, nargs
, i
;
6062 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6063 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6065 nparms
= TREE_VEC_LENGTH (parm_parms
);
6066 nargs
= TREE_VEC_LENGTH (arg_parms
);
6068 /* Determine whether we have a parameter pack at the end of the
6069 template template parameter's template parameter list. */
6070 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6072 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6074 if (parm
== error_mark_node
)
6077 switch (TREE_CODE (parm
))
6081 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6086 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6096 && !(variadic_p
&& nargs
>= nparms
- 1))
6099 /* Check all of the template parameters except the parameter pack at
6100 the end (if any). */
6101 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6103 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6104 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6107 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6108 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6110 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6118 /* Check each of the template parameters in the template
6119 argument against the template parameter pack at the end of
6120 the template template parameter. */
6121 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6124 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6126 for (; i
< nargs
; ++i
)
6128 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6131 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6133 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6142 /* Verifies that the deduced template arguments (in TARGS) for the
6143 template template parameters (in TPARMS) represent valid bindings,
6144 by comparing the template parameter list of each template argument
6145 to the template parameter list of its corresponding template
6146 template parameter, in accordance with DR150. This
6147 routine can only be called after all template arguments have been
6148 deduced. It will return TRUE if all of the template template
6149 parameter bindings are okay, FALSE otherwise. */
6151 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6153 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6156 /* We're dealing with template parms in this process. */
6157 ++processing_template_decl
;
6159 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6161 for (i
= 0; i
< ntparms
; ++i
)
6163 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6164 tree targ
= TREE_VEC_ELT (targs
, i
);
6166 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6168 tree packed_args
= NULL_TREE
;
6171 if (ARGUMENT_PACK_P (targ
))
6173 /* Look inside the argument pack. */
6174 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6175 len
= TREE_VEC_LENGTH (packed_args
);
6178 for (idx
= 0; idx
< len
; ++idx
)
6180 tree targ_parms
= NULL_TREE
;
6183 /* Extract the next argument from the argument
6185 targ
= TREE_VEC_ELT (packed_args
, idx
);
6187 if (PACK_EXPANSION_P (targ
))
6188 /* Look at the pattern of the pack expansion. */
6189 targ
= PACK_EXPANSION_PATTERN (targ
);
6191 /* Extract the template parameters from the template
6193 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6194 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6195 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6196 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6198 /* Verify that we can coerce the template template
6199 parameters from the template argument to the template
6200 parameter. This requires an exact match. */
6202 && !coerce_template_template_parms
6203 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6218 --processing_template_decl
;
6222 /* Since type attributes aren't mangled, we need to strip them from
6223 template type arguments. */
6226 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6229 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6231 mv
= TYPE_MAIN_VARIANT (arg
);
6232 arg
= strip_typedefs (arg
);
6233 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6234 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6236 if (complain
& tf_warning
)
6237 warning (0, "ignoring attributes on template argument %qT", arg
);
6238 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6239 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6244 /* Convert the indicated template ARG as necessary to match the
6245 indicated template PARM. Returns the converted ARG, or
6246 error_mark_node if the conversion was unsuccessful. Error and
6247 warning messages are issued under control of COMPLAIN. This
6248 conversion is for the Ith parameter in the parameter list. ARGS is
6249 the full set of template arguments deduced so far. */
6252 convert_template_argument (tree parm
,
6255 tsubst_flags_t complain
,
6261 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6263 if (TREE_CODE (arg
) == TREE_LIST
6264 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6266 /* The template argument was the name of some
6267 member function. That's usually
6268 invalid, but static members are OK. In any
6269 case, grab the underlying fields/functions
6270 and issue an error later if required. */
6271 orig_arg
= TREE_VALUE (arg
);
6272 TREE_TYPE (arg
) = unknown_type_node
;
6277 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6278 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6279 || requires_tmpl_type
);
6281 /* When determining whether an argument pack expansion is a template,
6282 look at the pattern. */
6283 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6284 arg
= PACK_EXPANSION_PATTERN (arg
);
6286 /* Deal with an injected-class-name used as a template template arg. */
6287 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6289 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6290 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6292 if (cxx_dialect
>= cxx11
)
6293 /* OK under DR 1004. */;
6294 else if (complain
& tf_warning_or_error
)
6295 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6296 " used as template template argument", TYPE_NAME (arg
));
6297 else if (flag_pedantic_errors
)
6305 ((TREE_CODE (arg
) == TEMPLATE_DECL
6306 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6307 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6308 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6309 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6312 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6313 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6314 arg
= TYPE_STUB_DECL (arg
);
6316 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6318 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6319 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6321 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6323 if (complain
& tf_error
)
6324 error ("invalid use of destructor %qE as a type", orig_arg
);
6325 return error_mark_node
;
6328 permerror (input_location
,
6329 "to refer to a type member of a template parameter, "
6330 "use %<typename %E%>", orig_arg
);
6332 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6333 TREE_OPERAND (arg
, 1),
6339 if (is_type
!= requires_type
)
6343 if (complain
& tf_error
)
6345 error ("type/value mismatch at argument %d in template "
6346 "parameter list for %qD",
6349 error (" expected a constant of type %qT, got %qT",
6351 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6352 else if (requires_tmpl_type
)
6353 error (" expected a class template, got %qE", orig_arg
);
6355 error (" expected a type, got %qE", orig_arg
);
6358 return error_mark_node
;
6360 if (is_tmpl_type
^ requires_tmpl_type
)
6362 if (in_decl
&& (complain
& tf_error
))
6364 error ("type/value mismatch at argument %d in template "
6365 "parameter list for %qD",
6368 error (" expected a type, got %qT", DECL_NAME (arg
));
6370 error (" expected a class template, got %qT", orig_arg
);
6372 return error_mark_node
;
6377 if (requires_tmpl_type
)
6379 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6381 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6382 /* The number of argument required is not known yet.
6383 Just accept it for now. */
6384 val
= TREE_TYPE (arg
);
6387 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6390 /* Strip alias templates that are equivalent to another
6392 arg
= get_underlying_template (arg
);
6393 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6395 if (coerce_template_template_parms (parmparm
, argparm
,
6401 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6403 if (val
!= error_mark_node
)
6405 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6406 val
= TREE_TYPE (val
);
6407 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6408 val
= make_pack_expansion (val
);
6413 if (in_decl
&& (complain
& tf_error
))
6415 error ("type/value mismatch at argument %d in "
6416 "template parameter list for %qD",
6418 error (" expected a template of type %qD, got %qT",
6422 val
= error_mark_node
;
6428 /* We only form one instance of each template specialization.
6429 Therefore, if we use a non-canonical variant (i.e., a
6430 typedef), any future messages referring to the type will use
6431 the typedef, which is confusing if those future uses do not
6432 themselves also use the typedef. */
6434 val
= canonicalize_type_argument (val
, complain
);
6438 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6440 if (invalid_nontype_parm_type_p (t
, complain
))
6441 return error_mark_node
;
6443 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6445 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6449 /* Not sure if this is reachable, but it doesn't hurt
6451 error ("type mismatch in nontype parameter pack");
6452 val
= error_mark_node
;
6455 else if (!dependent_template_arg_p (orig_arg
)
6456 && !uses_template_parms (t
))
6457 /* We used to call digest_init here. However, digest_init
6458 will report errors, which we don't want when complain
6459 is zero. More importantly, digest_init will try too
6460 hard to convert things: for example, `0' should not be
6461 converted to pointer type at this point according to
6462 the standard. Accepting this is not merely an
6463 extension, since deciding whether or not these
6464 conversions can occur is part of determining which
6465 function template to call, or whether a given explicit
6466 argument specification is valid. */
6467 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6469 val
= strip_typedefs_expr (orig_arg
);
6471 if (val
== NULL_TREE
)
6472 val
= error_mark_node
;
6473 else if (val
== error_mark_node
&& (complain
& tf_error
))
6474 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6476 if (TREE_CODE (val
) == SCOPE_REF
)
6478 /* Strip typedefs from the SCOPE_REF. */
6479 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6480 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6482 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6483 QUALIFIED_NAME_IS_TEMPLATE (val
));
6490 /* Coerces the remaining template arguments in INNER_ARGS (from
6491 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6492 Returns the coerced argument pack. PARM_IDX is the position of this
6493 parameter in the template parameter list. ARGS is the original
6494 template argument list. */
6496 coerce_template_parameter_pack (tree parms
,
6504 tsubst_flags_t complain
)
6506 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6507 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6510 tree packed_types
= NULL_TREE
;
6512 if (arg_idx
> nargs
)
6515 packed_args
= make_tree_vec (nargs
- arg_idx
);
6517 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6518 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6520 /* When the template parameter is a non-type template
6521 parameter pack whose type uses parameter packs, we need
6522 to look at each of the template arguments
6523 separately. Build a vector of the types for these
6524 non-type template parameters in PACKED_TYPES. */
6526 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6527 packed_types
= tsubst_pack_expansion (expansion
, args
,
6530 if (packed_types
== error_mark_node
)
6531 return error_mark_node
;
6533 /* Check that we have the right number of arguments. */
6535 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6536 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6539 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6540 error ("wrong number of template arguments (%d, should be %d)",
6541 nargs
, needed_parms
);
6542 return error_mark_node
;
6545 /* If we aren't able to check the actual arguments now
6546 (because they haven't been expanded yet), we can at least
6547 verify that all of the types used for the non-type
6548 template parameter pack are, in fact, valid for non-type
6549 template parameters. */
6551 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6553 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6554 for (j
= 0; j
< len
; ++j
)
6556 tree t
= TREE_VEC_ELT (packed_types
, j
);
6557 if (invalid_nontype_parm_type_p (t
, complain
))
6558 return error_mark_node
;
6563 /* Convert the remaining arguments, which will be a part of the
6564 parameter pack "parm". */
6565 for (; arg_idx
< nargs
; ++arg_idx
)
6567 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6568 tree actual_parm
= TREE_VALUE (parm
);
6570 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6572 /* When we have a vector of types (corresponding to the
6573 non-type template parameter pack that uses parameter
6574 packs in its type, as mention above), and the
6575 argument is not an expansion (which expands to a
6576 currently unknown number of arguments), clone the
6577 parm and give it the next type in PACKED_TYPES. */
6578 actual_parm
= copy_node (actual_parm
);
6579 TREE_TYPE (actual_parm
) =
6580 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6583 if (arg
== error_mark_node
)
6585 if (complain
& tf_error
)
6586 error ("template argument %d is invalid", arg_idx
+ 1);
6589 arg
= convert_template_argument (actual_parm
,
6590 arg
, new_args
, complain
, parm_idx
,
6592 if (arg
== error_mark_node
)
6594 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6597 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6598 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6599 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6602 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6603 TREE_TYPE (argument_pack
)
6604 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6605 TREE_CONSTANT (argument_pack
) = 1;
6608 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6609 #ifdef ENABLE_CHECKING
6610 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6611 TREE_VEC_LENGTH (packed_args
));
6613 return argument_pack
;
6616 /* Returns the number of pack expansions in the template argument vector
6620 pack_expansion_args_count (tree args
)
6625 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6627 tree elt
= TREE_VEC_ELT (args
, i
);
6628 if (elt
&& PACK_EXPANSION_P (elt
))
6634 /* Convert all template arguments to their appropriate types, and
6635 return a vector containing the innermost resulting template
6636 arguments. If any error occurs, return error_mark_node. Error and
6637 warning messages are issued under control of COMPLAIN.
6639 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6640 for arguments not specified in ARGS. Otherwise, if
6641 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6642 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6643 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6647 coerce_template_parms (tree parms
,
6650 tsubst_flags_t complain
,
6651 bool require_all_args
,
6652 bool use_default_args
)
6654 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6657 tree new_inner_args
;
6658 int saved_unevaluated_operand
;
6659 int saved_inhibit_evaluation_warnings
;
6661 /* When used as a boolean value, indicates whether this is a
6662 variadic template parameter list. Since it's an int, we can also
6663 subtract it from nparms to get the number of non-variadic
6666 int variadic_args_p
= 0;
6667 int post_variadic_parms
= 0;
6669 if (args
== error_mark_node
)
6670 return error_mark_node
;
6672 nparms
= TREE_VEC_LENGTH (parms
);
6674 /* Determine if there are any parameter packs. */
6675 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6677 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6679 ++post_variadic_parms
;
6680 if (template_parameter_pack_p (tparm
))
6684 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6685 /* If there are no parameters that follow a parameter pack, we need to
6686 expand any argument packs so that we can deduce a parameter pack from
6687 some non-packed args followed by an argument pack, as in variadic85.C.
6688 If there are such parameters, we need to leave argument packs intact
6689 so the arguments are assigned properly. This can happen when dealing
6690 with a nested class inside a partial specialization of a class
6691 template, as in variadic92.C, or when deducing a template parameter pack
6692 from a sub-declarator, as in variadic114.C. */
6693 if (!post_variadic_parms
)
6694 inner_args
= expand_template_argument_pack (inner_args
);
6696 /* Count any pack expansion args. */
6697 variadic_args_p
= pack_expansion_args_count (inner_args
);
6699 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6700 if ((nargs
> nparms
&& !variadic_p
)
6701 || (nargs
< nparms
- variadic_p
6704 && (!use_default_args
6705 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6706 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6708 if (complain
& tf_error
)
6712 nparms
-= variadic_p
;
6713 error ("wrong number of template arguments "
6714 "(%d, should be %d or more)", nargs
, nparms
);
6717 error ("wrong number of template arguments "
6718 "(%d, should be %d)", nargs
, nparms
);
6721 error ("provided for %q+D", in_decl
);
6724 return error_mark_node
;
6726 /* We can't pass a pack expansion to a non-pack parameter of an alias
6727 template (DR 1430). */
6728 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6730 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6732 if (complain
& tf_error
)
6734 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6736 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6737 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6739 if (PACK_EXPANSION_P (arg
)
6740 && !template_parameter_pack_p (parm
))
6742 error ("pack expansion argument for non-pack parameter "
6743 "%qD of alias template %qD", parm
, in_decl
);
6744 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6751 return error_mark_node
;
6754 /* We need to evaluate the template arguments, even though this
6755 template-id may be nested within a "sizeof". */
6756 saved_unevaluated_operand
= cp_unevaluated_operand
;
6757 cp_unevaluated_operand
= 0;
6758 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6759 c_inhibit_evaluation_warnings
= 0;
6760 new_inner_args
= make_tree_vec (nparms
);
6761 new_args
= add_outermost_template_args (args
, new_inner_args
);
6762 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6767 /* Get the Ith template parameter. */
6768 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6770 if (parm
== error_mark_node
)
6772 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6776 /* Calculate the next argument. */
6777 if (arg_idx
< nargs
)
6778 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6782 if (template_parameter_pack_p (TREE_VALUE (parm
))
6783 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6785 /* All remaining arguments will be placed in the
6786 template parameter pack PARM. */
6787 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6788 inner_args
, arg_idx
,
6792 /* Store this argument. */
6793 if (arg
== error_mark_node
)
6795 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6797 /* We are done with all of the arguments. */
6804 if (PACK_EXPANSION_P (arg
))
6806 /* We don't know how many args we have yet, just
6807 use the unconverted ones for now. */
6808 new_inner_args
= inner_args
;
6812 else if (require_all_args
)
6814 /* There must be a default arg in this case. */
6815 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6817 /* The position of the first default template argument,
6818 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6820 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6826 if (arg
== error_mark_node
)
6828 if (complain
& tf_error
)
6829 error ("template argument %d is invalid", arg_idx
+ 1);
6832 /* This only occurs if there was an error in the template
6833 parameter list itself (which we would already have
6834 reported) that we are trying to recover from, e.g., a class
6835 template with a parameter list such as
6836 template<typename..., typename>. */
6839 arg
= convert_template_argument (TREE_VALUE (parm
),
6840 arg
, new_args
, complain
,
6843 if (arg
== error_mark_node
)
6845 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6847 cp_unevaluated_operand
= saved_unevaluated_operand
;
6848 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6851 return error_mark_node
;
6853 #ifdef ENABLE_CHECKING
6854 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6856 TREE_VEC_LENGTH (new_inner_args
));
6859 return new_inner_args
;
6862 /* Like coerce_template_parms. If PARMS represents all template
6863 parameters levels, this function returns a vector of vectors
6864 representing all the resulting argument levels. Note that in this
6865 case, only the innermost arguments are coerced because the
6866 outermost ones are supposed to have been coerced already.
6868 Otherwise, if PARMS represents only (the innermost) vector of
6869 parameters, this function returns a vector containing just the
6870 innermost resulting arguments. */
6873 coerce_innermost_template_parms (tree parms
,
6876 tsubst_flags_t complain
,
6877 bool require_all_args
,
6878 bool use_default_args
)
6880 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6881 int args_depth
= TMPL_ARGS_DEPTH (args
);
6884 if (parms_depth
> 1)
6886 coerced_args
= make_tree_vec (parms_depth
);
6890 for (level
= parms
, cur_depth
= parms_depth
;
6891 parms_depth
> 0 && level
!= NULL_TREE
;
6892 level
= TREE_CHAIN (level
), --cur_depth
)
6895 if (cur_depth
== args_depth
)
6896 l
= coerce_template_parms (TREE_VALUE (level
),
6897 args
, in_decl
, complain
,
6901 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6903 if (l
== error_mark_node
)
6904 return error_mark_node
;
6906 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6910 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6911 args
, in_decl
, complain
,
6914 return coerced_args
;
6917 /* Returns 1 if template args OT and NT are equivalent. */
6920 template_args_equal (tree ot
, tree nt
)
6924 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6927 if (TREE_CODE (nt
) == TREE_VEC
)
6928 /* For member templates */
6929 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6930 else if (PACK_EXPANSION_P (ot
))
6931 return (PACK_EXPANSION_P (nt
)
6932 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6933 PACK_EXPANSION_PATTERN (nt
))
6934 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6935 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6936 else if (ARGUMENT_PACK_P (ot
))
6941 if (!ARGUMENT_PACK_P (nt
))
6944 opack
= ARGUMENT_PACK_ARGS (ot
);
6945 npack
= ARGUMENT_PACK_ARGS (nt
);
6946 len
= TREE_VEC_LENGTH (opack
);
6947 if (TREE_VEC_LENGTH (npack
) != len
)
6949 for (i
= 0; i
< len
; ++i
)
6950 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6951 TREE_VEC_ELT (npack
, i
)))
6955 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6957 /* We get here probably because we are in the middle of substituting
6958 into the pattern of a pack expansion. In that case the
6959 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6960 interested in. So we want to use the initial pack argument for
6962 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6963 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6964 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6965 return template_args_equal (ot
, nt
);
6967 else if (TYPE_P (nt
))
6968 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6969 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6972 return cp_tree_equal (ot
, nt
);
6975 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6976 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6977 NEWARG_PTR with the offending arguments if they are non-NULL. */
6980 comp_template_args_with_info (tree oldargs
, tree newargs
,
6981 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6985 if (oldargs
== newargs
)
6988 if (!oldargs
|| !newargs
)
6991 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6994 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6996 tree nt
= TREE_VEC_ELT (newargs
, i
);
6997 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6999 if (! template_args_equal (ot
, nt
))
7001 if (oldarg_ptr
!= NULL
)
7003 if (newarg_ptr
!= NULL
)
7011 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7012 of template arguments. Returns 0 otherwise. */
7015 comp_template_args (tree oldargs
, tree newargs
)
7017 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7021 add_pending_template (tree d
)
7023 tree ti
= (TYPE_P (d
)
7024 ? CLASSTYPE_TEMPLATE_INFO (d
)
7025 : DECL_TEMPLATE_INFO (d
));
7026 struct pending_template
*pt
;
7029 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7032 /* We are called both from instantiate_decl, where we've already had a
7033 tinst_level pushed, and instantiate_template, where we haven't.
7035 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7038 push_tinst_level (d
);
7040 pt
= ggc_alloc_pending_template ();
7042 pt
->tinst
= current_tinst_level
;
7043 if (last_pending_template
)
7044 last_pending_template
->next
= pt
;
7046 pending_templates
= pt
;
7048 last_pending_template
= pt
;
7050 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7057 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7058 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7059 documentation for TEMPLATE_ID_EXPR. */
7062 lookup_template_function (tree fns
, tree arglist
)
7066 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7067 return error_mark_node
;
7069 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7071 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7073 error ("%q#D is not a function template", fns
);
7074 return error_mark_node
;
7077 if (BASELINK_P (fns
))
7079 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7081 BASELINK_FUNCTIONS (fns
),
7086 type
= TREE_TYPE (fns
);
7087 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7088 type
= unknown_type_node
;
7090 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7093 /* Within the scope of a template class S<T>, the name S gets bound
7094 (in build_self_reference) to a TYPE_DECL for the class, not a
7095 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7096 or one of its enclosing classes, and that type is a template,
7097 return the associated TEMPLATE_DECL. Otherwise, the original
7100 Also handle the case when DECL is a TREE_LIST of ambiguous
7101 injected-class-names from different bases. */
7104 maybe_get_template_decl_from_type_decl (tree decl
)
7106 if (decl
== NULL_TREE
)
7109 /* DR 176: A lookup that finds an injected-class-name (10.2
7110 [class.member.lookup]) can result in an ambiguity in certain cases
7111 (for example, if it is found in more than one base class). If all of
7112 the injected-class-names that are found refer to specializations of
7113 the same class template, and if the name is followed by a
7114 template-argument-list, the reference refers to the class template
7115 itself and not a specialization thereof, and is not ambiguous. */
7116 if (TREE_CODE (decl
) == TREE_LIST
)
7118 tree t
, tmpl
= NULL_TREE
;
7119 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7121 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7124 else if (tmpl
!= elt
)
7127 if (tmpl
&& t
== NULL_TREE
)
7133 return (decl
!= NULL_TREE
7134 && DECL_SELF_REFERENCE_P (decl
)
7135 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7136 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7139 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7140 parameters, find the desired type.
7142 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7144 IN_DECL, if non-NULL, is the template declaration we are trying to
7147 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7148 the class we are looking up.
7150 Issue error and warning messages under control of COMPLAIN.
7152 If the template class is really a local class in a template
7153 function, then the FUNCTION_CONTEXT is the function in which it is
7156 ??? Note that this function is currently called *twice* for each
7157 template-id: the first time from the parser, while creating the
7158 incomplete type (finish_template_type), and the second type during the
7159 real instantiation (instantiate_template_class). This is surely something
7160 that we want to avoid. It also causes some problems with argument
7161 coercion (see convert_nontype_argument for more information on this). */
7164 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7165 int entering_scope
, tsubst_flags_t complain
)
7167 tree templ
= NULL_TREE
, parmlist
;
7174 if (identifier_p (d1
))
7176 tree value
= innermost_non_namespace_value (d1
);
7177 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7182 push_decl_namespace (context
);
7183 templ
= lookup_name (d1
);
7184 templ
= maybe_get_template_decl_from_type_decl (templ
);
7186 pop_decl_namespace ();
7189 context
= DECL_CONTEXT (templ
);
7191 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7193 tree type
= TREE_TYPE (d1
);
7195 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7196 an implicit typename for the second A. Deal with it. */
7197 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7198 type
= TREE_TYPE (type
);
7200 if (CLASSTYPE_TEMPLATE_INFO (type
))
7202 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7203 d1
= DECL_NAME (templ
);
7206 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7207 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7209 templ
= TYPE_TI_TEMPLATE (d1
);
7210 d1
= DECL_NAME (templ
);
7212 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7213 && DECL_TEMPLATE_RESULT (d1
)
7214 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7217 d1
= DECL_NAME (templ
);
7218 context
= DECL_CONTEXT (templ
);
7220 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7223 d1
= DECL_NAME (templ
);
7226 /* Issue an error message if we didn't find a template. */
7229 if (complain
& tf_error
)
7230 error ("%qT is not a template", d1
);
7231 return error_mark_node
;
7234 if (TREE_CODE (templ
) != TEMPLATE_DECL
7235 /* Make sure it's a user visible template, if it was named by
7237 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7238 && !PRIMARY_TEMPLATE_P (templ
)))
7240 if (complain
& tf_error
)
7242 error ("non-template type %qT used as a template", d1
);
7244 error ("for template declaration %q+D", in_decl
);
7246 return error_mark_node
;
7249 complain
&= ~tf_user
;
7251 /* An alias that just changes the name of a template is equivalent to the
7252 other template, so if any of the arguments are pack expansions, strip
7253 the alias to avoid problems with a pack expansion passed to a non-pack
7254 alias template parameter (DR 1430). */
7255 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7256 templ
= get_underlying_template (templ
);
7258 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7260 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7261 template arguments */
7267 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7269 /* Consider an example where a template template parameter declared as
7271 template <class T, class U = std::allocator<T> > class TT
7273 The template parameter level of T and U are one level larger than
7274 of TT. To proper process the default argument of U, say when an
7275 instantiation `TT<int>' is seen, we need to build the full
7276 arguments containing {int} as the innermost level. Outer levels,
7277 available when not appearing as default template argument, can be
7278 obtained from the arguments of the enclosing template.
7280 Suppose that TT is later substituted with std::vector. The above
7281 instantiation is `TT<int, std::allocator<T> >' with TT at
7282 level 1, and T at level 2, while the template arguments at level 1
7283 becomes {std::vector} and the inner level 2 is {int}. */
7285 outer
= DECL_CONTEXT (templ
);
7287 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7288 else if (current_template_parms
)
7289 /* This is an argument of the current template, so we haven't set
7290 DECL_CONTEXT yet. */
7291 outer
= current_template_args ();
7294 arglist
= add_to_template_args (outer
, arglist
);
7296 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7298 /*require_all_args=*/true,
7299 /*use_default_args=*/true);
7300 if (arglist2
== error_mark_node
7301 || (!uses_template_parms (arglist2
)
7302 && check_instantiated_args (templ
, arglist2
, complain
)))
7303 return error_mark_node
;
7305 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7310 tree template_type
= TREE_TYPE (templ
);
7313 tree found
= NULL_TREE
;
7316 int is_dependent_type
;
7317 int use_partial_inst_tmpl
= false;
7319 if (template_type
== error_mark_node
)
7320 /* An error occurred while building the template TEMPL, and a
7321 diagnostic has most certainly been emitted for that
7322 already. Let's propagate that error. */
7323 return error_mark_node
;
7325 gen_tmpl
= most_general_template (templ
);
7326 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7327 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7328 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7330 if (arg_depth
== 1 && parm_depth
> 1)
7332 /* We've been given an incomplete set of template arguments.
7335 template <class T> struct S1 {
7336 template <class U> struct S2 {};
7337 template <class U> struct S2<U*> {};
7340 we will be called with an ARGLIST of `U*', but the
7341 TEMPLATE will be `template <class T> template
7342 <class U> struct S1<T>::S2'. We must fill in the missing
7345 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7347 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7350 /* Now we should have enough arguments. */
7351 gcc_assert (parm_depth
== arg_depth
);
7353 /* From here on, we're only interested in the most general
7356 /* Calculate the BOUND_ARGS. These will be the args that are
7357 actually tsubst'd into the definition to create the
7361 /* We have multiple levels of arguments to coerce, at once. */
7363 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7365 tree bound_args
= make_tree_vec (parm_depth
);
7367 for (i
= saved_depth
,
7368 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7369 i
> 0 && t
!= NULL_TREE
;
7370 --i
, t
= TREE_CHAIN (t
))
7373 if (i
== saved_depth
)
7374 a
= coerce_template_parms (TREE_VALUE (t
),
7377 /*require_all_args=*/true,
7378 /*use_default_args=*/true);
7380 /* Outer levels should have already been coerced. */
7381 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7383 /* Don't process further if one of the levels fails. */
7384 if (a
== error_mark_node
)
7386 /* Restore the ARGLIST to its full size. */
7387 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7388 return error_mark_node
;
7391 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7393 /* We temporarily reduce the length of the ARGLIST so
7394 that coerce_template_parms will see only the arguments
7395 corresponding to the template parameters it is
7397 TREE_VEC_LENGTH (arglist
)--;
7400 /* Restore the ARGLIST to its full size. */
7401 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7403 arglist
= bound_args
;
7407 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7408 INNERMOST_TEMPLATE_ARGS (arglist
),
7411 /*require_all_args=*/true,
7412 /*use_default_args=*/true);
7414 if (arglist
== error_mark_node
)
7415 /* We were unable to bind the arguments. */
7416 return error_mark_node
;
7418 /* In the scope of a template class, explicit references to the
7419 template class refer to the type of the template, not any
7420 instantiation of it. For example, in:
7422 template <class T> class C { void f(C<T>); }
7424 the `C<T>' is just the same as `C'. Outside of the
7425 class, however, such a reference is an instantiation. */
7427 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7428 || currently_open_class (template_type
))
7429 /* comp_template_args is expensive, check it last. */
7430 && comp_template_args (TYPE_TI_ARGS (template_type
),
7432 return template_type
;
7434 /* If we already have this specialization, return it. */
7435 elt
.tmpl
= gen_tmpl
;
7437 hash
= hash_specialization (&elt
);
7438 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7444 is_dependent_type
= uses_template_parms (arglist
);
7446 /* If the deduced arguments are invalid, then the binding
7448 if (!is_dependent_type
7449 && check_instantiated_args (gen_tmpl
,
7450 INNERMOST_TEMPLATE_ARGS (arglist
),
7452 return error_mark_node
;
7454 if (!is_dependent_type
7455 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7456 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7457 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7459 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7460 DECL_NAME (gen_tmpl
),
7461 /*tag_scope=*/ts_global
);
7465 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7467 if (context
== error_mark_node
)
7468 return error_mark_node
;
7471 context
= global_namespace
;
7473 /* Create the type. */
7474 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7476 /* The user referred to a specialization of an alias
7477 template represented by GEN_TMPL.
7479 [temp.alias]/2 says:
7481 When a template-id refers to the specialization of an
7482 alias template, it is equivalent to the associated
7483 type obtained by substitution of its
7484 template-arguments for the template-parameters in the
7485 type-id of the alias template. */
7487 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7488 /* Note that the call above (by indirectly calling
7489 register_specialization in tsubst_decl) registers the
7490 TYPE_DECL representing the specialization of the alias
7491 template. So next time someone substitutes ARGLIST for
7492 the template parms into the alias template (GEN_TMPL),
7493 she'll get that TYPE_DECL back. */
7495 if (t
== error_mark_node
)
7498 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7500 if (!is_dependent_type
)
7502 set_current_access_from_decl (TYPE_NAME (template_type
));
7503 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7504 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7505 arglist
, complain
, in_decl
),
7506 SCOPED_ENUM_P (template_type
), NULL
);
7510 /* We don't want to call start_enum for this type, since
7511 the values for the enumeration constants may involve
7512 template parameters. And, no one should be interested
7513 in the enumeration constants for such a type. */
7514 t
= cxx_make_type (ENUMERAL_TYPE
);
7515 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7517 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7518 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7519 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7521 else if (CLASS_TYPE_P (template_type
))
7523 t
= make_class_type (TREE_CODE (template_type
));
7524 CLASSTYPE_DECLARED_CLASS (t
)
7525 = CLASSTYPE_DECLARED_CLASS (template_type
);
7526 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7527 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7529 /* A local class. Make sure the decl gets registered properly. */
7530 if (context
== current_function_decl
)
7531 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7533 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7534 /* This instantiation is another name for the primary
7535 template type. Set the TYPE_CANONICAL field
7537 TYPE_CANONICAL (t
) = template_type
;
7538 else if (any_template_arguments_need_structural_equality_p (arglist
))
7539 /* Some of the template arguments require structural
7540 equality testing, so this template class requires
7541 structural equality testing. */
7542 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7547 /* If we called start_enum or pushtag above, this information
7548 will already be set up. */
7551 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7553 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7554 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7555 DECL_SOURCE_LOCATION (type_decl
)
7556 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7559 type_decl
= TYPE_NAME (t
);
7561 if (CLASS_TYPE_P (template_type
))
7563 TREE_PRIVATE (type_decl
)
7564 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7565 TREE_PROTECTED (type_decl
)
7566 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7567 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7569 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7570 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7574 /* Let's consider the explicit specialization of a member
7575 of a class template specialization that is implicitely instantiated,
7580 template<class U> struct M {}; //#0
7585 struct S<int>::M<char> //#1
7589 [temp.expl.spec]/4 says this is valid.
7591 In this case, when we write:
7594 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7597 When we encounter #1, we want to store the partial instantiation
7598 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7600 For all cases other than this "explicit specialization of member of a
7601 class template", we just want to store the most general template into
7602 the CLASSTYPE_TI_TEMPLATE of M.
7604 This case of "explicit specialization of member of a class template"
7606 1/ the enclosing class is an instantiation of, and therefore not
7607 the same as, the context of the most general template, and
7608 2/ we aren't looking at the partial instantiation itself, i.e.
7609 the innermost arguments are not the same as the innermost parms of
7610 the most general template.
7612 So it's only when 1/ and 2/ happens that we want to use the partial
7613 instantiation of the member template in lieu of its most general
7616 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7617 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7618 /* the enclosing class must be an instantiation... */
7619 && CLASS_TYPE_P (context
)
7620 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7622 tree partial_inst_args
;
7623 TREE_VEC_LENGTH (arglist
)--;
7624 ++processing_template_decl
;
7626 tsubst (INNERMOST_TEMPLATE_ARGS
7627 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7628 arglist
, complain
, NULL_TREE
);
7629 --processing_template_decl
;
7630 TREE_VEC_LENGTH (arglist
)++;
7631 use_partial_inst_tmpl
=
7632 /*...and we must not be looking at the partial instantiation
7634 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7638 if (!use_partial_inst_tmpl
)
7639 /* This case is easy; there are no member templates involved. */
7643 /* This is a full instantiation of a member template. Find
7644 the partial instantiation of which this is an instance. */
7646 /* Temporarily reduce by one the number of levels in the ARGLIST
7647 so as to avoid comparing the last set of arguments. */
7648 TREE_VEC_LENGTH (arglist
)--;
7649 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7650 TREE_VEC_LENGTH (arglist
)++;
7651 /* FOUND is either a proper class type, or an alias
7652 template specialization. In the later case, it's a
7653 TYPE_DECL, resulting from the substituting of arguments
7654 for parameters in the TYPE_DECL of the alias template
7655 done earlier. So be careful while getting the template
7657 found
= TREE_CODE (found
) == TYPE_DECL
7658 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7659 : CLASSTYPE_TI_TEMPLATE (found
);
7662 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7665 slot
= htab_find_slot_with_hash (type_specializations
,
7666 &elt
, hash
, INSERT
);
7667 entry
= ggc_alloc_spec_entry ();
7671 /* Note this use of the partial instantiation so we can check it
7672 later in maybe_process_partial_specialization. */
7673 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7674 = tree_cons (arglist
, t
,
7675 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7677 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7678 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7679 /* Now that the type has been registered on the instantiations
7680 list, we set up the enumerators. Because the enumeration
7681 constants may involve the enumeration type itself, we make
7682 sure to register the type first, and then create the
7683 constants. That way, doing tsubst_expr for the enumeration
7684 constants won't result in recursive calls here; we'll find
7685 the instantiation and exit above. */
7686 tsubst_enum (template_type
, t
, arglist
);
7688 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7689 /* If the type makes use of template parameters, the
7690 code that generates debugging information will crash. */
7691 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7693 /* Possibly limit visibility based on template args. */
7694 TREE_PUBLIC (type_decl
) = 1;
7695 determine_visibility (type_decl
);
7701 /* Wrapper for lookup_template_class_1. */
7704 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7705 int entering_scope
, tsubst_flags_t complain
)
7708 timevar_push (TV_TEMPLATE_INST
);
7709 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7710 entering_scope
, complain
);
7711 timevar_pop (TV_TEMPLATE_INST
);
7719 /* True when we should also visit template parameters that occur in
7720 non-deduced contexts. */
7721 bool include_nondeduced_p
;
7722 struct pointer_set_t
*visited
;
7725 /* Called from for_each_template_parm via walk_tree. */
7728 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7731 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7732 tree_fn_t fn
= pfd
->fn
;
7733 void *data
= pfd
->data
;
7736 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7737 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7738 pfd
->include_nondeduced_p
))
7739 return error_mark_node
;
7741 switch (TREE_CODE (t
))
7744 if (TYPE_PTRMEMFUNC_P (t
))
7750 if (!TYPE_TEMPLATE_INFO (t
))
7752 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7753 fn
, data
, pfd
->visited
,
7754 pfd
->include_nondeduced_p
))
7755 return error_mark_node
;
7759 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7760 fn
, data
, pfd
->visited
,
7761 pfd
->include_nondeduced_p
)
7762 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7763 fn
, data
, pfd
->visited
,
7764 pfd
->include_nondeduced_p
))
7765 return error_mark_node
;
7769 /* Since we're not going to walk subtrees, we have to do this
7771 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7772 pfd
->visited
, pfd
->include_nondeduced_p
))
7773 return error_mark_node
;
7777 /* Check the return type. */
7778 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7779 pfd
->include_nondeduced_p
))
7780 return error_mark_node
;
7782 /* Check the parameter types. Since default arguments are not
7783 instantiated until they are needed, the TYPE_ARG_TYPES may
7784 contain expressions that involve template parameters. But,
7785 no-one should be looking at them yet. And, once they're
7786 instantiated, they don't contain template parameters, so
7787 there's no point in looking at them then, either. */
7791 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7792 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7793 pfd
->visited
, pfd
->include_nondeduced_p
))
7794 return error_mark_node
;
7796 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7797 want walk_tree walking into them itself. */
7803 case UNDERLYING_TYPE
:
7804 if (pfd
->include_nondeduced_p
7805 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7807 pfd
->include_nondeduced_p
))
7808 return error_mark_node
;
7813 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7814 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7815 pfd
->visited
, pfd
->include_nondeduced_p
))
7816 return error_mark_node
;
7821 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7822 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7823 pfd
->visited
, pfd
->include_nondeduced_p
))
7824 return error_mark_node
;
7825 if (DECL_CONTEXT (t
)
7826 && pfd
->include_nondeduced_p
7827 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7828 pfd
->visited
, pfd
->include_nondeduced_p
))
7829 return error_mark_node
;
7832 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7833 /* Record template parameters such as `T' inside `TT<T>'. */
7834 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7835 pfd
->include_nondeduced_p
))
7836 return error_mark_node
;
7839 case TEMPLATE_TEMPLATE_PARM
:
7840 case TEMPLATE_TYPE_PARM
:
7841 case TEMPLATE_PARM_INDEX
:
7842 if (fn
&& (*fn
)(t
, data
))
7843 return error_mark_node
;
7845 return error_mark_node
;
7849 /* A template template parameter is encountered. */
7850 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7851 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7852 pfd
->include_nondeduced_p
))
7853 return error_mark_node
;
7855 /* Already substituted template template parameter */
7861 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7863 pfd
->include_nondeduced_p
))
7864 return error_mark_node
;
7868 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7869 && pfd
->include_nondeduced_p
7870 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7871 (TREE_TYPE (t
)), fn
, data
,
7872 pfd
->visited
, pfd
->include_nondeduced_p
))
7873 return error_mark_node
;
7878 /* If there's no type, then this thing must be some expression
7879 involving template parameters. */
7880 if (!fn
&& !TREE_TYPE (t
))
7881 return error_mark_node
;
7886 case IMPLICIT_CONV_EXPR
:
7887 case REINTERPRET_CAST_EXPR
:
7888 case CONST_CAST_EXPR
:
7889 case STATIC_CAST_EXPR
:
7890 case DYNAMIC_CAST_EXPR
:
7894 case PSEUDO_DTOR_EXPR
:
7896 return error_mark_node
;
7903 /* We didn't find any template parameters we liked. */
7907 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7908 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7909 call FN with the parameter and the DATA.
7910 If FN returns nonzero, the iteration is terminated, and
7911 for_each_template_parm returns 1. Otherwise, the iteration
7912 continues. If FN never returns a nonzero value, the value
7913 returned by for_each_template_parm is 0. If FN is NULL, it is
7914 considered to be the function which always returns 1.
7916 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7917 parameters that occur in non-deduced contexts. When false, only
7918 visits those template parameters that can be deduced. */
7921 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7922 struct pointer_set_t
*visited
,
7923 bool include_nondeduced_p
)
7925 struct pair_fn_data pfd
;
7931 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7933 /* Walk the tree. (Conceptually, we would like to walk without
7934 duplicates, but for_each_template_parm_r recursively calls
7935 for_each_template_parm, so we would need to reorganize a fair
7936 bit to use walk_tree_without_duplicates, so we keep our own
7939 pfd
.visited
= visited
;
7941 pfd
.visited
= pointer_set_create ();
7942 result
= cp_walk_tree (&t
,
7943 for_each_template_parm_r
,
7945 pfd
.visited
) != NULL_TREE
;
7950 pointer_set_destroy (pfd
.visited
);
7957 /* Returns true if T depends on any template parameter. */
7960 uses_template_parms (tree t
)
7963 int saved_processing_template_decl
;
7965 saved_processing_template_decl
= processing_template_decl
;
7966 if (!saved_processing_template_decl
)
7967 processing_template_decl
= 1;
7969 dependent_p
= dependent_type_p (t
);
7970 else if (TREE_CODE (t
) == TREE_VEC
)
7971 dependent_p
= any_dependent_template_arguments_p (t
);
7972 else if (TREE_CODE (t
) == TREE_LIST
)
7973 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7974 || uses_template_parms (TREE_CHAIN (t
)));
7975 else if (TREE_CODE (t
) == TYPE_DECL
)
7976 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7979 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7980 || TREE_CODE (t
) == OVERLOAD
7983 || TREE_CODE (t
) == TRAIT_EXPR
7984 || TREE_CODE (t
) == CONSTRUCTOR
7985 || CONSTANT_CLASS_P (t
))
7986 dependent_p
= (type_dependent_expression_p (t
)
7987 || value_dependent_expression_p (t
));
7990 gcc_assert (t
== error_mark_node
);
7991 dependent_p
= false;
7994 processing_template_decl
= saved_processing_template_decl
;
7999 /* Returns true iff current_function_decl is an incompletely instantiated
8000 template. Useful instead of processing_template_decl because the latter
8001 is set to 0 during fold_non_dependent_expr. */
8004 in_template_function (void)
8006 tree fn
= current_function_decl
;
8008 ++processing_template_decl
;
8009 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8010 && DECL_TEMPLATE_INFO (fn
)
8011 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8012 --processing_template_decl
;
8016 /* Returns true if T depends on any template parameter with level LEVEL. */
8019 uses_template_parms_level (tree t
, int level
)
8021 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8022 /*include_nondeduced_p=*/true);
8025 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8026 ill-formed translation unit, i.e. a variable or function that isn't
8027 usable in a constant expression. */
8030 neglectable_inst_p (tree d
)
8033 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8034 : decl_maybe_constant_var_p (d
)));
8037 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8038 neglectable and instantiated from within an erroneous instantiation. */
8041 limit_bad_template_recursion (tree decl
)
8043 struct tinst_level
*lev
= current_tinst_level
;
8044 int errs
= errorcount
+ sorrycount
;
8045 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8048 for (; lev
; lev
= lev
->next
)
8049 if (neglectable_inst_p (lev
->decl
))
8052 return (lev
&& errs
> lev
->errors
);
8055 static int tinst_depth
;
8056 extern int max_tinst_depth
;
8059 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8061 /* We're starting to instantiate D; record the template instantiation context
8062 for diagnostics and to restore it later. */
8065 push_tinst_level (tree d
)
8067 struct tinst_level
*new_level
;
8069 if (tinst_depth
>= max_tinst_depth
)
8071 last_error_tinst_level
= current_tinst_level
;
8072 if (TREE_CODE (d
) == TREE_LIST
)
8073 error ("template instantiation depth exceeds maximum of %d (use "
8074 "-ftemplate-depth= to increase the maximum) substituting %qS",
8075 max_tinst_depth
, d
);
8077 error ("template instantiation depth exceeds maximum of %d (use "
8078 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8079 max_tinst_depth
, d
);
8081 print_instantiation_context ();
8086 /* If the current instantiation caused problems, don't let it instantiate
8087 anything else. Do allow deduction substitution and decls usable in
8088 constant expressions. */
8089 if (limit_bad_template_recursion (d
))
8092 new_level
= ggc_alloc_tinst_level ();
8093 new_level
->decl
= d
;
8094 new_level
->locus
= input_location
;
8095 new_level
->errors
= errorcount
+sorrycount
;
8096 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8097 new_level
->next
= current_tinst_level
;
8098 current_tinst_level
= new_level
;
8101 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8102 depth_reached
= tinst_depth
;
8107 /* We're done instantiating this template; return to the instantiation
8111 pop_tinst_level (void)
8113 /* Restore the filename and line number stashed away when we started
8114 this instantiation. */
8115 input_location
= current_tinst_level
->locus
;
8116 current_tinst_level
= current_tinst_level
->next
;
8120 /* We're instantiating a deferred template; restore the template
8121 instantiation context in which the instantiation was requested, which
8122 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8125 reopen_tinst_level (struct tinst_level
*level
)
8127 struct tinst_level
*t
;
8130 for (t
= level
; t
; t
= t
->next
)
8133 current_tinst_level
= level
;
8135 if (current_tinst_level
)
8136 current_tinst_level
->errors
= errorcount
+sorrycount
;
8140 /* Returns the TINST_LEVEL which gives the original instantiation
8143 struct tinst_level
*
8144 outermost_tinst_level (void)
8146 struct tinst_level
*level
= current_tinst_level
;
8149 level
= level
->next
;
8153 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8154 vector of template arguments, as for tsubst.
8156 Returns an appropriate tsubst'd friend declaration. */
8159 tsubst_friend_function (tree decl
, tree args
)
8163 if (TREE_CODE (decl
) == FUNCTION_DECL
8164 && DECL_TEMPLATE_INSTANTIATION (decl
)
8165 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8166 /* This was a friend declared with an explicit template
8167 argument list, e.g.:
8171 to indicate that f was a template instantiation, not a new
8172 function declaration. Now, we have to figure out what
8173 instantiation of what template. */
8175 tree template_id
, arglist
, fns
;
8178 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8180 /* Friend functions are looked up in the containing namespace scope.
8181 We must enter that scope, to avoid finding member functions of the
8182 current class with same name. */
8183 push_nested_namespace (ns
);
8184 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8185 tf_warning_or_error
, NULL_TREE
,
8186 /*integral_constant_expression_p=*/false);
8187 pop_nested_namespace (ns
);
8188 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8189 tf_warning_or_error
, NULL_TREE
);
8190 template_id
= lookup_template_function (fns
, arglist
);
8192 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8193 tmpl
= determine_specialization (template_id
, new_friend
,
8195 /*need_member_template=*/0,
8196 TREE_VEC_LENGTH (args
),
8198 return instantiate_template (tmpl
, new_args
, tf_error
);
8201 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8203 /* The NEW_FRIEND will look like an instantiation, to the
8204 compiler, but is not an instantiation from the point of view of
8205 the language. For example, we might have had:
8207 template <class T> struct S {
8208 template <class U> friend void f(T, U);
8211 Then, in S<int>, template <class U> void f(int, U) is not an
8212 instantiation of anything. */
8213 if (new_friend
== error_mark_node
)
8214 return error_mark_node
;
8216 DECL_USE_TEMPLATE (new_friend
) = 0;
8217 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8219 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8220 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8221 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8224 /* The mangled name for the NEW_FRIEND is incorrect. The function
8225 is not a template instantiation and should not be mangled like
8226 one. Therefore, we forget the mangling here; we'll recompute it
8227 later if we need it. */
8228 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8230 SET_DECL_RTL (new_friend
, NULL
);
8231 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8234 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8237 tree new_friend_template_info
;
8238 tree new_friend_result_template_info
;
8240 int new_friend_is_defn
;
8242 /* We must save some information from NEW_FRIEND before calling
8243 duplicate decls since that function will free NEW_FRIEND if
8245 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8246 new_friend_is_defn
=
8247 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8248 (template_for_substitution (new_friend
)))
8250 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8252 /* This declaration is a `primary' template. */
8253 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8255 new_friend_result_template_info
8256 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8259 new_friend_result_template_info
= NULL_TREE
;
8261 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8262 if (new_friend_is_defn
)
8263 DECL_INITIAL (new_friend
) = error_mark_node
;
8265 /* Inside pushdecl_namespace_level, we will push into the
8266 current namespace. However, the friend function should go
8267 into the namespace of the template. */
8268 ns
= decl_namespace_context (new_friend
);
8269 push_nested_namespace (ns
);
8270 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8271 pop_nested_namespace (ns
);
8273 if (old_decl
== error_mark_node
)
8274 return error_mark_node
;
8276 if (old_decl
!= new_friend
)
8278 /* This new friend declaration matched an existing
8279 declaration. For example, given:
8281 template <class T> void f(T);
8282 template <class U> class C {
8283 template <class T> friend void f(T) {}
8286 the friend declaration actually provides the definition
8287 of `f', once C has been instantiated for some type. So,
8288 old_decl will be the out-of-class template declaration,
8289 while new_friend is the in-class definition.
8291 But, if `f' was called before this point, the
8292 instantiation of `f' will have DECL_TI_ARGS corresponding
8293 to `T' but not to `U', references to which might appear
8294 in the definition of `f'. Previously, the most general
8295 template for an instantiation of `f' was the out-of-class
8296 version; now it is the in-class version. Therefore, we
8297 run through all specialization of `f', adding to their
8298 DECL_TI_ARGS appropriately. In particular, they need a
8299 new set of outer arguments, corresponding to the
8300 arguments for this class instantiation.
8302 The same situation can arise with something like this:
8305 template <class T> class C {
8309 when `C<int>' is instantiated. Now, `f(int)' is defined
8312 if (!new_friend_is_defn
)
8313 /* On the other hand, if the in-class declaration does
8314 *not* provide a definition, then we don't want to alter
8315 existing definitions. We can just leave everything
8320 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8321 tree new_args
= TI_ARGS (new_friend_template_info
);
8323 /* Overwrite whatever template info was there before, if
8324 any, with the new template information pertaining to
8326 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8328 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8330 /* We should have called reregister_specialization in
8332 gcc_assert (retrieve_specialization (new_template
,
8336 /* Instantiate it if the global has already been used. */
8337 if (DECL_ODR_USED (old_decl
))
8338 instantiate_decl (old_decl
, /*defer_ok=*/true,
8339 /*expl_inst_class_mem_p=*/false);
8345 /* Indicate that the old function template is a partial
8347 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8348 = new_friend_result_template_info
;
8350 gcc_assert (new_template
8351 == most_general_template (new_template
));
8352 gcc_assert (new_template
!= old_decl
);
8354 /* Reassign any specializations already in the hash table
8355 to the new more general template, and add the
8356 additional template args. */
8357 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8361 tree spec
= TREE_VALUE (t
);
8364 elt
.tmpl
= old_decl
;
8365 elt
.args
= DECL_TI_ARGS (spec
);
8366 elt
.spec
= NULL_TREE
;
8368 htab_remove_elt (decl_specializations
, &elt
);
8371 = add_outermost_template_args (new_args
,
8372 DECL_TI_ARGS (spec
));
8374 register_specialization
8375 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8378 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8382 /* The information from NEW_FRIEND has been merged into OLD_DECL
8383 by duplicate_decls. */
8384 new_friend
= old_decl
;
8389 tree context
= DECL_CONTEXT (new_friend
);
8393 template <class T> class C {
8394 template <class U> friend void C1<U>::f (); // case 1
8395 friend void C2<T>::f (); // case 2
8397 we only need to make sure CONTEXT is a complete type for
8398 case 2. To distinguish between the two cases, we note that
8399 CONTEXT of case 1 remains dependent type after tsubst while
8400 this isn't true for case 2. */
8401 ++processing_template_decl
;
8402 dependent_p
= dependent_type_p (context
);
8403 --processing_template_decl
;
8406 && !complete_type_or_else (context
, NULL_TREE
))
8407 return error_mark_node
;
8409 if (COMPLETE_TYPE_P (context
))
8411 /* Check to see that the declaration is really present, and,
8412 possibly obtain an improved declaration. */
8413 tree fn
= check_classfn (context
,
8414 new_friend
, NULL_TREE
);
8424 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8425 template arguments, as for tsubst.
8427 Returns an appropriate tsubst'd friend type or error_mark_node on
8431 tsubst_friend_class (tree friend_tmpl
, tree args
)
8437 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8439 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8440 return TREE_TYPE (t
);
8443 context
= CP_DECL_CONTEXT (friend_tmpl
);
8445 if (context
!= global_namespace
)
8447 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8448 push_nested_namespace (context
);
8450 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8453 /* Look for a class template declaration. We look for hidden names
8454 because two friend declarations of the same template are the
8455 same. For example, in:
8458 template <typename> friend class F;
8460 template <typename> struct B {
8461 template <typename> friend class F;
8464 both F templates are the same. */
8465 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8466 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8468 /* But, if we don't find one, it might be because we're in a
8469 situation like this:
8477 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8478 for `S<int>', not the TEMPLATE_DECL. */
8479 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8481 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8482 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8485 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8487 /* The friend template has already been declared. Just
8488 check to see that the declarations match, and install any new
8489 default parameters. We must tsubst the default parameters,
8490 of course. We only need the innermost template parameters
8491 because that is all that redeclare_class_template will look
8493 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8494 > TMPL_ARGS_DEPTH (args
))
8497 location_t saved_input_location
;
8498 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8499 args
, tf_warning_or_error
);
8501 saved_input_location
= input_location
;
8502 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8503 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8504 input_location
= saved_input_location
;
8508 friend_type
= TREE_TYPE (tmpl
);
8512 /* The friend template has not already been declared. In this
8513 case, the instantiation of the template class will cause the
8514 injection of this template into the global scope. */
8515 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8516 if (tmpl
== error_mark_node
)
8517 return error_mark_node
;
8519 /* The new TMPL is not an instantiation of anything, so we
8520 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8521 the new type because that is supposed to be the corresponding
8522 template decl, i.e., TMPL. */
8523 DECL_USE_TEMPLATE (tmpl
) = 0;
8524 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8525 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8526 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8527 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8529 /* Inject this template into the global scope. */
8530 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8533 if (context
!= global_namespace
)
8535 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8536 pop_nested_namespace (context
);
8538 pop_nested_class ();
8544 /* Returns zero if TYPE cannot be completed later due to circularity.
8545 Otherwise returns one. */
8548 can_complete_type_without_circularity (tree type
)
8550 if (type
== NULL_TREE
|| type
== error_mark_node
)
8552 else if (COMPLETE_TYPE_P (type
))
8554 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8555 return can_complete_type_without_circularity (TREE_TYPE (type
));
8556 else if (CLASS_TYPE_P (type
)
8557 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8563 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8565 /* Apply any attributes which had to be deferred until instantiation
8566 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8567 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8570 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8571 tree args
, tsubst_flags_t complain
, tree in_decl
)
8573 tree last_dep
= NULL_TREE
;
8577 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8578 if (ATTR_IS_DEPENDENT (t
))
8581 attributes
= copy_list (attributes
);
8585 if (DECL_P (*decl_p
))
8587 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8589 p
= &DECL_ATTRIBUTES (*decl_p
);
8592 p
= &TYPE_ATTRIBUTES (*decl_p
);
8596 tree late_attrs
= NULL_TREE
;
8597 tree
*q
= &late_attrs
;
8599 for (*p
= attributes
; *p
; )
8602 if (ATTR_IS_DEPENDENT (t
))
8604 *p
= TREE_CHAIN (t
);
8605 TREE_CHAIN (t
) = NULL_TREE
;
8607 && is_attribute_p ("omp declare simd",
8608 get_attribute_name (t
))
8611 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8612 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8614 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8615 clauses
= finish_omp_clauses (clauses
);
8616 tree parms
= DECL_ARGUMENTS (*decl_p
);
8618 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8620 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8622 TREE_VALUE (t
) = NULL_TREE
;
8624 /* If the first attribute argument is an identifier, don't
8625 pass it through tsubst. Attributes like mode, format,
8626 cleanup and several target specific attributes expect it
8628 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
8631 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8633 /*integral_constant_expression_p=*/false);
8634 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8636 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8641 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8642 /*integral_constant_expression_p=*/false);
8644 q
= &TREE_CHAIN (t
);
8647 p
= &TREE_CHAIN (t
);
8650 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8654 /* Perform (or defer) access check for typedefs that were referenced
8655 from within the template TMPL code.
8656 This is a subroutine of instantiate_decl and instantiate_class_template.
8657 TMPL is the template to consider and TARGS is the list of arguments of
8661 perform_typedefs_access_check (tree tmpl
, tree targs
)
8663 location_t saved_location
;
8665 qualified_typedef_usage_t
*iter
;
8668 || (!CLASS_TYPE_P (tmpl
)
8669 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8672 saved_location
= input_location
;
8673 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8675 tree type_decl
= iter
->typedef_decl
;
8676 tree type_scope
= iter
->context
;
8678 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8681 if (uses_template_parms (type_decl
))
8682 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8683 if (uses_template_parms (type_scope
))
8684 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8686 /* Make access check error messages point to the location
8687 of the use of the typedef. */
8688 input_location
= iter
->locus
;
8689 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8690 type_decl
, type_decl
,
8691 tf_warning_or_error
);
8693 input_location
= saved_location
;
8697 instantiate_class_template_1 (tree type
)
8699 tree templ
, args
, pattern
, t
, member
;
8703 unsigned int saved_maximum_field_alignment
;
8706 if (type
== error_mark_node
)
8707 return error_mark_node
;
8709 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8710 || uses_template_parms (type
))
8713 /* Figure out which template is being instantiated. */
8714 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8715 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8717 /* Determine what specialization of the original template to
8719 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8720 if (t
== error_mark_node
)
8722 TYPE_BEING_DEFINED (type
) = 1;
8723 return error_mark_node
;
8727 /* This TYPE is actually an instantiation of a partial
8728 specialization. We replace the innermost set of ARGS with
8729 the arguments appropriate for substitution. For example,
8732 template <class T> struct S {};
8733 template <class T> struct S<T*> {};
8735 and supposing that we are instantiating S<int*>, ARGS will
8736 presently be {int*} -- but we need {int}. */
8737 pattern
= TREE_TYPE (t
);
8738 args
= TREE_PURPOSE (t
);
8742 pattern
= TREE_TYPE (templ
);
8743 args
= CLASSTYPE_TI_ARGS (type
);
8746 /* If the template we're instantiating is incomplete, then clearly
8747 there's nothing we can do. */
8748 if (!COMPLETE_TYPE_P (pattern
))
8751 /* If we've recursively instantiated too many templates, stop. */
8752 if (! push_tinst_level (type
))
8755 /* Now we're really doing the instantiation. Mark the type as in
8756 the process of being defined. */
8757 TYPE_BEING_DEFINED (type
) = 1;
8759 /* We may be in the middle of deferred access check. Disable
8761 push_deferring_access_checks (dk_no_deferred
);
8763 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8765 push_to_top_level ();
8766 /* Use #pragma pack from the template context. */
8767 saved_maximum_field_alignment
= maximum_field_alignment
;
8768 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8770 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8772 /* Set the input location to the most specialized template definition.
8773 This is needed if tsubsting causes an error. */
8774 typedecl
= TYPE_MAIN_DECL (pattern
);
8775 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8776 DECL_SOURCE_LOCATION (typedecl
);
8778 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8779 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8780 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8781 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8782 if (ANON_AGGR_TYPE_P (pattern
))
8783 SET_ANON_AGGR_TYPE_P (type
);
8784 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8786 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8787 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8788 /* Adjust visibility for template arguments. */
8789 determine_visibility (TYPE_MAIN_DECL (type
));
8791 if (CLASS_TYPE_P (type
))
8792 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8794 pbinfo
= TYPE_BINFO (pattern
);
8796 /* We should never instantiate a nested class before its enclosing
8797 class; we need to look up the nested class by name before we can
8798 instantiate it, and that lookup should instantiate the enclosing
8800 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8801 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8803 base_list
= NULL_TREE
;
8804 if (BINFO_N_BASE_BINFOS (pbinfo
))
8810 /* We must enter the scope containing the type, as that is where
8811 the accessibility of types named in dependent bases are
8813 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8815 /* Substitute into each of the bases to determine the actual
8817 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8820 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8821 tree expanded_bases
= NULL_TREE
;
8824 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8827 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8828 args
, tf_error
, NULL_TREE
);
8829 if (expanded_bases
== error_mark_node
)
8832 len
= TREE_VEC_LENGTH (expanded_bases
);
8835 for (idx
= 0; idx
< len
; idx
++)
8838 /* Extract the already-expanded base class. */
8839 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8841 /* Substitute to figure out the base class. */
8842 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8845 if (base
== error_mark_node
)
8848 base_list
= tree_cons (access
, base
, base_list
);
8849 if (BINFO_VIRTUAL_P (pbase_binfo
))
8850 TREE_TYPE (base_list
) = integer_type_node
;
8854 /* The list is now in reverse order; correct that. */
8855 base_list
= nreverse (base_list
);
8858 pop_scope (pushed_scope
);
8860 /* Now call xref_basetypes to set up all the base-class
8862 xref_basetypes (type
, base_list
);
8864 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8865 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8866 args
, tf_error
, NULL_TREE
);
8867 fixup_attribute_variants (type
);
8869 /* Now that our base classes are set up, enter the scope of the
8870 class, so that name lookups into base classes, etc. will work
8871 correctly. This is precisely analogous to what we do in
8872 begin_class_definition when defining an ordinary non-template
8873 class, except we also need to push the enclosing classes. */
8874 push_nested_class (type
);
8876 /* Now members are processed in the order of declaration. */
8877 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8878 member
; member
= TREE_CHAIN (member
))
8880 tree t
= TREE_VALUE (member
);
8882 if (TREE_PURPOSE (member
))
8886 /* Build new CLASSTYPE_NESTED_UTDS. */
8889 bool class_template_p
;
8891 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8892 && TYPE_LANG_SPECIFIC (t
)
8893 && CLASSTYPE_IS_TEMPLATE (t
));
8894 /* If the member is a class template, then -- even after
8895 substitution -- there may be dependent types in the
8896 template argument list for the class. We increment
8897 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8898 that function will assume that no types are dependent
8899 when outside of a template. */
8900 if (class_template_p
)
8901 ++processing_template_decl
;
8902 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8903 if (class_template_p
)
8904 --processing_template_decl
;
8905 if (newtag
== error_mark_node
)
8908 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8910 tree name
= TYPE_IDENTIFIER (t
);
8912 if (class_template_p
)
8913 /* Unfortunately, lookup_template_class sets
8914 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8915 instantiation (i.e., for the type of a member
8916 template class nested within a template class.)
8917 This behavior is required for
8918 maybe_process_partial_specialization to work
8919 correctly, but is not accurate in this case;
8920 the TAG is not an instantiation of anything.
8921 (The corresponding TEMPLATE_DECL is an
8922 instantiation, but the TYPE is not.) */
8923 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8925 /* Now, we call pushtag to put this NEWTAG into the scope of
8926 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8927 pushtag calling push_template_decl. We don't have to do
8928 this for enums because it will already have been done in
8931 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8932 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8935 else if (DECL_DECLARES_FUNCTION_P (t
))
8937 /* Build new TYPE_METHODS. */
8940 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8941 ++processing_template_decl
;
8942 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8943 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8944 --processing_template_decl
;
8945 set_current_access_from_decl (r
);
8946 finish_member_declaration (r
);
8947 /* Instantiate members marked with attribute used. */
8948 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8950 if (TREE_CODE (r
) == FUNCTION_DECL
8951 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8952 cp_check_omp_declare_reduction (r
);
8956 /* Build new TYPE_FIELDS. */
8957 if (TREE_CODE (t
) == STATIC_ASSERT
)
8961 ++c_inhibit_evaluation_warnings
;
8963 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8964 tf_warning_or_error
, NULL_TREE
,
8965 /*integral_constant_expression_p=*/true);
8966 --c_inhibit_evaluation_warnings
;
8968 finish_static_assert (condition
,
8969 STATIC_ASSERT_MESSAGE (t
),
8970 STATIC_ASSERT_SOURCE_LOCATION (t
),
8973 else if (TREE_CODE (t
) != CONST_DECL
)
8976 tree vec
= NULL_TREE
;
8979 /* The file and line for this declaration, to
8980 assist in error message reporting. Since we
8981 called push_tinst_level above, we don't need to
8983 input_location
= DECL_SOURCE_LOCATION (t
);
8985 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8986 ++processing_template_decl
;
8987 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8988 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8989 --processing_template_decl
;
8991 if (TREE_CODE (r
) == TREE_VEC
)
8993 /* A capture pack became multiple fields. */
8995 len
= TREE_VEC_LENGTH (vec
);
8998 for (int i
= 0; i
< len
; ++i
)
9001 r
= TREE_VEC_ELT (vec
, i
);
9006 [t]he initialization (and any associated
9007 side-effects) of a static data member does
9008 not occur unless the static data member is
9009 itself used in a way that requires the
9010 definition of the static data member to
9013 Therefore, we do not substitute into the
9014 initialized for the static data member here. */
9015 finish_static_data_member_decl
9018 /*init_const_expr_p=*/false,
9019 /*asmspec_tree=*/NULL_TREE
,
9021 /* Instantiate members marked with attribute used. */
9022 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9025 else if (TREE_CODE (r
) == FIELD_DECL
)
9027 /* Determine whether R has a valid type and can be
9028 completed later. If R is invalid, then its type
9029 is replaced by error_mark_node. */
9030 tree rtype
= TREE_TYPE (r
);
9031 if (can_complete_type_without_circularity (rtype
))
9032 complete_type (rtype
);
9034 if (!COMPLETE_TYPE_P (rtype
))
9036 cxx_incomplete_type_error (r
, rtype
);
9037 TREE_TYPE (r
) = error_mark_node
;
9041 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9042 such a thing will already have been added to the field
9043 list by tsubst_enum in finish_member_declaration in the
9044 CLASSTYPE_NESTED_UTDS case above. */
9045 if (!(TREE_CODE (r
) == TYPE_DECL
9046 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9047 && DECL_ARTIFICIAL (r
)))
9049 set_current_access_from_decl (r
);
9050 finish_member_declaration (r
);
9058 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9059 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9061 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9063 tree friend_type
= t
;
9064 bool adjust_processing_template_decl
= false;
9066 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9068 /* template <class T> friend class C; */
9069 friend_type
= tsubst_friend_class (friend_type
, args
);
9070 adjust_processing_template_decl
= true;
9072 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9074 /* template <class T> friend class C::D; */
9075 friend_type
= tsubst (friend_type
, args
,
9076 tf_warning_or_error
, NULL_TREE
);
9077 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9078 friend_type
= TREE_TYPE (friend_type
);
9079 adjust_processing_template_decl
= true;
9081 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9082 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9084 /* This could be either
9088 when dependent_type_p is false or
9090 template <class U> friend class T::C;
9093 friend_type
= tsubst (friend_type
, args
,
9094 tf_warning_or_error
, NULL_TREE
);
9095 /* Bump processing_template_decl for correct
9096 dependent_type_p calculation. */
9097 ++processing_template_decl
;
9098 if (dependent_type_p (friend_type
))
9099 adjust_processing_template_decl
= true;
9100 --processing_template_decl
;
9102 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9103 && hidden_name_p (TYPE_NAME (friend_type
)))
9107 where C hasn't been declared yet. Let's lookup name
9108 from namespace scope directly, bypassing any name that
9109 come from dependent base class. */
9110 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9112 /* The call to xref_tag_from_type does injection for friend
9114 push_nested_namespace (ns
);
9116 xref_tag_from_type (friend_type
, NULL_TREE
,
9117 /*tag_scope=*/ts_current
);
9118 pop_nested_namespace (ns
);
9120 else if (uses_template_parms (friend_type
))
9121 /* friend class C<T>; */
9122 friend_type
= tsubst (friend_type
, args
,
9123 tf_warning_or_error
, NULL_TREE
);
9128 where C is already declared or
9130 friend class C<int>;
9132 We don't have to do anything in these cases. */
9134 if (adjust_processing_template_decl
)
9135 /* Trick make_friend_class into realizing that the friend
9136 we're adding is a template, not an ordinary class. It's
9137 important that we use make_friend_class since it will
9138 perform some error-checking and output cross-reference
9140 ++processing_template_decl
;
9142 if (friend_type
!= error_mark_node
)
9143 make_friend_class (type
, friend_type
, /*complain=*/false);
9145 if (adjust_processing_template_decl
)
9146 --processing_template_decl
;
9150 /* Build new DECL_FRIENDLIST. */
9153 /* The file and line for this declaration, to
9154 assist in error message reporting. Since we
9155 called push_tinst_level above, we don't need to
9157 input_location
= DECL_SOURCE_LOCATION (t
);
9159 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9161 ++processing_template_decl
;
9162 push_deferring_access_checks (dk_no_check
);
9165 r
= tsubst_friend_function (t
, args
);
9166 add_friend (type
, r
, /*complain=*/false);
9167 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9169 pop_deferring_access_checks ();
9170 --processing_template_decl
;
9176 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9178 tree decl
= lambda_function (type
);
9181 if (!DECL_TEMPLATE_INFO (decl
)
9182 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9183 instantiate_decl (decl
, false, false);
9185 /* We need to instantiate the capture list from the template
9186 after we've instantiated the closure members, but before we
9187 consider adding the conversion op. Also keep any captures
9188 that may have been added during instantiation of the op(). */
9189 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9191 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9192 args
, tf_warning_or_error
, NULL_TREE
,
9195 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9196 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9198 maybe_add_lambda_conv_op (type
);
9201 gcc_assert (errorcount
);
9204 /* Set the file and line number information to whatever is given for
9205 the class itself. This puts error messages involving generated
9206 implicit functions at a predictable point, and the same point
9207 that would be used for non-template classes. */
9208 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9210 unreverse_member_declarations (type
);
9211 finish_struct_1 (type
);
9212 TYPE_BEING_DEFINED (type
) = 0;
9214 /* We don't instantiate default arguments for member functions. 14.7.1:
9216 The implicit instantiation of a class template specialization causes
9217 the implicit instantiation of the declarations, but not of the
9218 definitions or default arguments, of the class member functions,
9219 member classes, static data members and member templates.... */
9221 /* Some typedefs referenced from within the template code need to be access
9222 checked at template instantiation time, i.e now. These types were
9223 added to the template at parsing time. Let's get those and perform
9224 the access checks then. */
9225 perform_typedefs_access_check (pattern
, args
);
9226 perform_deferred_access_checks (tf_warning_or_error
);
9227 pop_nested_class ();
9228 maximum_field_alignment
= saved_maximum_field_alignment
;
9230 pop_from_top_level ();
9231 pop_deferring_access_checks ();
9234 /* The vtable for a template class can be emitted in any translation
9235 unit in which the class is instantiated. When there is no key
9236 method, however, finish_struct_1 will already have added TYPE to
9237 the keyed_classes list. */
9238 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9239 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9244 /* Wrapper for instantiate_class_template_1. */
9247 instantiate_class_template (tree type
)
9250 timevar_push (TV_TEMPLATE_INST
);
9251 ret
= instantiate_class_template_1 (type
);
9252 timevar_pop (TV_TEMPLATE_INST
);
9257 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9263 else if (TYPE_P (t
))
9264 r
= tsubst (t
, args
, complain
, in_decl
);
9267 if (!(complain
& tf_warning
))
9268 ++c_inhibit_evaluation_warnings
;
9269 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9270 /*integral_constant_expression_p=*/true);
9271 if (!(complain
& tf_warning
))
9272 --c_inhibit_evaluation_warnings
;
9273 /* Preserve the raw-reference nature of T. */
9274 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9275 && REFERENCE_REF_P (r
))
9276 r
= TREE_OPERAND (r
, 0);
9281 /* Given a function parameter pack TMPL_PARM and some function parameters
9282 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9283 and set *SPEC_P to point at the next point in the list. */
9286 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9288 /* Collect all of the extra "packed" parameters into an
9292 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9293 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9294 tree spec_parm
= *spec_p
;
9297 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9299 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9302 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9303 parmvec
= make_tree_vec (len
);
9304 parmtypevec
= make_tree_vec (len
);
9305 spec_parm
= *spec_p
;
9306 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9308 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9309 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9312 /* Build the argument packs. */
9313 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9314 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9315 TREE_TYPE (argpack
) = argtypepack
;
9316 *spec_p
= spec_parm
;
9321 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9322 NONTYPE_ARGUMENT_PACK. */
9325 make_fnparm_pack (tree spec_parm
)
9327 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9330 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9334 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9336 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9337 if (i
>= TREE_VEC_LENGTH (vec
))
9339 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9343 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9346 make_argument_pack_select (tree arg_pack
, unsigned index
)
9348 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9350 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9351 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9356 /* This is a subroutine of tsubst_pack_expansion.
9358 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9359 mechanism to store the (non complete list of) arguments of the
9360 substitution and return a non substituted pack expansion, in order
9361 to wait for when we have enough arguments to really perform the
9365 use_pack_expansion_extra_args_p (tree parm_packs
,
9369 /* If one pack has an expansion and another pack has a normal
9370 argument or if one pack has an empty argument and an another
9371 one hasn't then tsubst_pack_expansion cannot perform the
9372 substitution and need to fall back on the
9373 PACK_EXPANSION_EXTRA mechanism. */
9374 if (parm_packs
== NULL_TREE
)
9376 else if (has_empty_arg
)
9379 bool has_expansion_arg
= false;
9380 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9382 bool has_non_expansion_arg
= false;
9383 for (tree parm_pack
= parm_packs
;
9385 parm_pack
= TREE_CHAIN (parm_pack
))
9387 tree arg
= TREE_VALUE (parm_pack
);
9389 if (argument_pack_element_is_expansion_p (arg
, i
))
9390 has_expansion_arg
= true;
9392 has_non_expansion_arg
= true;
9395 if (has_expansion_arg
&& has_non_expansion_arg
)
9401 /* [temp.variadic]/6 says that:
9403 The instantiation of a pack expansion [...]
9404 produces a list E1,E2, ..., En, where N is the number of elements
9405 in the pack expansion parameters.
9407 This subroutine of tsubst_pack_expansion produces one of these Ei.
9409 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9410 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9411 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9412 INDEX is the index 'i' of the element Ei to produce. ARGS,
9413 COMPLAIN, and IN_DECL are the same parameters as for the
9414 tsubst_pack_expansion function.
9416 The function returns the resulting Ei upon successful completion,
9419 Note that this function possibly modifies the ARGS parameter, so
9420 it's the responsibility of the caller to restore it. */
9423 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9426 tree args
/* This parm gets
9428 tsubst_flags_t complain
,
9432 bool ith_elem_is_expansion
= false;
9434 /* For each parameter pack, change the substitution of the parameter
9435 pack to the ith argument in its argument pack, then expand the
9437 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9439 tree parm
= TREE_PURPOSE (pack
);
9440 tree arg_pack
= TREE_VALUE (pack
);
9441 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9443 ith_elem_is_expansion
|=
9444 argument_pack_element_is_expansion_p (arg_pack
, index
);
9446 /* Select the Ith argument from the pack. */
9447 if (TREE_CODE (parm
) == PARM_DECL
9448 || TREE_CODE (parm
) == FIELD_DECL
)
9452 aps
= make_argument_pack_select (arg_pack
, index
);
9454 register_local_specialization (aps
, parm
);
9457 aps
= retrieve_local_specialization (parm
);
9462 template_parm_level_and_index (parm
, &level
, &idx
);
9466 aps
= make_argument_pack_select (arg_pack
, index
);
9467 /* Update the corresponding argument. */
9468 TMPL_ARG (args
, level
, idx
) = aps
;
9471 /* Re-use the ARGUMENT_PACK_SELECT. */
9472 aps
= TMPL_ARG (args
, level
, idx
);
9474 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9477 /* Substitute into the PATTERN with the (possibly altered)
9479 if (!TYPE_P (pattern
))
9480 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9481 /*integral_constant_expression_p=*/false);
9483 t
= tsubst (pattern
, args
, complain
, in_decl
);
9485 /* If the Ith argument pack element is a pack expansion, then
9486 the Ith element resulting from the substituting is going to
9487 be a pack expansion as well. */
9488 if (ith_elem_is_expansion
)
9489 t
= make_pack_expansion (t
);
9494 /* Substitute ARGS into T, which is an pack expansion
9495 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9496 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9497 (if only a partial substitution could be performed) or
9498 ERROR_MARK_NODE if there was an error. */
9500 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9504 tree pack
, packs
= NULL_TREE
;
9505 bool unsubstituted_packs
= false;
9508 struct pointer_map_t
*saved_local_specializations
= NULL
;
9509 bool need_local_specializations
= false;
9512 gcc_assert (PACK_EXPANSION_P (t
));
9513 pattern
= PACK_EXPANSION_PATTERN (t
);
9515 /* Add in any args remembered from an earlier partial instantiation. */
9516 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9518 levels
= TMPL_ARGS_DEPTH (args
);
9520 /* Determine the argument packs that will instantiate the parameter
9521 packs used in the expansion expression. While we're at it,
9522 compute the number of arguments to be expanded and make sure it
9524 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9525 pack
= TREE_CHAIN (pack
))
9527 tree parm_pack
= TREE_VALUE (pack
);
9528 tree arg_pack
= NULL_TREE
;
9529 tree orig_arg
= NULL_TREE
;
9532 if (TREE_CODE (parm_pack
) == BASES
)
9534 if (BASES_DIRECT (parm_pack
))
9535 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9536 args
, complain
, in_decl
, false));
9538 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9539 args
, complain
, in_decl
, false));
9541 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9543 if (PACK_EXPANSION_LOCAL_P (t
))
9544 arg_pack
= retrieve_local_specialization (parm_pack
);
9547 /* We can't rely on local_specializations for a parameter
9548 name used later in a function declaration (such as in a
9549 late-specified return type). Even if it exists, it might
9550 have the wrong value for a recursive call. Just make a
9551 dummy decl, since it's only used for its type. */
9552 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9553 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9554 /* Partial instantiation of the parm_pack, we can't build
9555 up an argument pack yet. */
9556 arg_pack
= NULL_TREE
;
9558 arg_pack
= make_fnparm_pack (arg_pack
);
9559 need_local_specializations
= true;
9562 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9563 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9567 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9569 if (level
<= levels
)
9570 arg_pack
= TMPL_ARG (args
, level
, idx
);
9573 orig_arg
= arg_pack
;
9574 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9575 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9577 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9578 /* This can only happen if we forget to expand an argument
9579 pack somewhere else. Just return an error, silently. */
9581 result
= make_tree_vec (1);
9582 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9589 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9591 /* Don't bother trying to do a partial substitution with
9592 incomplete packs; we'll try again after deduction. */
9593 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9598 else if (len
!= my_len
)
9600 if (!(complain
& tf_error
))
9601 /* Fail quietly. */;
9602 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9603 error ("mismatched argument pack lengths while expanding "
9607 error ("mismatched argument pack lengths while expanding "
9610 return error_mark_node
;
9613 /* Keep track of the parameter packs and their corresponding
9615 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9616 TREE_TYPE (packs
) = orig_arg
;
9620 /* We can't substitute for this parameter pack. We use a flag as
9621 well as the missing_level counter because function parameter
9622 packs don't have a level. */
9623 unsubstituted_packs
= true;
9627 /* We cannot expand this expansion expression, because we don't have
9628 all of the argument packs we need. */
9629 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9631 /* We got some full packs, but we can't substitute them in until we
9632 have values for all the packs. So remember these until then. */
9634 t
= make_pack_expansion (pattern
);
9635 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9638 else if (unsubstituted_packs
)
9640 /* There were no real arguments, we're just replacing a parameter
9641 pack with another version of itself. Substitute into the
9642 pattern and return a PACK_EXPANSION_*. The caller will need to
9644 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9645 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9646 /*integral_constant_expression_p=*/false);
9648 t
= tsubst (pattern
, args
, complain
, in_decl
);
9649 t
= make_pack_expansion (t
);
9653 gcc_assert (len
>= 0);
9655 if (need_local_specializations
)
9657 /* We're in a late-specified return type, so create our own local
9658 specializations map; the current map is either NULL or (in the
9659 case of recursive unification) might have bindings that we don't
9660 want to use or alter. */
9661 saved_local_specializations
= local_specializations
;
9662 local_specializations
= pointer_map_create ();
9665 /* For each argument in each argument pack, substitute into the
9667 result
= make_tree_vec (len
);
9668 for (i
= 0; i
< len
; ++i
)
9670 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9674 TREE_VEC_ELT (result
, i
) = t
;
9675 if (t
== error_mark_node
)
9677 result
= error_mark_node
;
9682 /* Update ARGS to restore the substitution from parameter packs to
9683 their argument packs. */
9684 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9686 tree parm
= TREE_PURPOSE (pack
);
9688 if (TREE_CODE (parm
) == PARM_DECL
9689 || TREE_CODE (parm
) == FIELD_DECL
)
9690 register_local_specialization (TREE_TYPE (pack
), parm
);
9695 if (TREE_VALUE (pack
) == NULL_TREE
)
9698 template_parm_level_and_index (parm
, &level
, &idx
);
9700 /* Update the corresponding argument. */
9701 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9702 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9705 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9709 if (need_local_specializations
)
9711 pointer_map_destroy (local_specializations
);
9712 local_specializations
= saved_local_specializations
;
9718 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9719 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9720 parameter packs; all parms generated from a function parameter pack will
9721 have the same DECL_PARM_INDEX. */
9724 get_pattern_parm (tree parm
, tree tmpl
)
9726 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9729 if (DECL_ARTIFICIAL (parm
))
9731 for (patparm
= DECL_ARGUMENTS (pattern
);
9732 patparm
; patparm
= DECL_CHAIN (patparm
))
9733 if (DECL_ARTIFICIAL (patparm
)
9734 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9739 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9740 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9741 gcc_assert (DECL_PARM_INDEX (patparm
)
9742 == DECL_PARM_INDEX (parm
));
9748 /* Substitute ARGS into the vector or list of template arguments T. */
9751 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9754 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9757 if (t
== error_mark_node
)
9758 return error_mark_node
;
9760 len
= TREE_VEC_LENGTH (t
);
9761 elts
= XALLOCAVEC (tree
, len
);
9763 for (i
= 0; i
< len
; i
++)
9765 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9768 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9769 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9770 else if (PACK_EXPANSION_P (orig_arg
))
9772 /* Substitute into an expansion expression. */
9773 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9775 if (TREE_CODE (new_arg
) == TREE_VEC
)
9776 /* Add to the expanded length adjustment the number of
9777 expanded arguments. We subtract one from this
9778 measurement, because the argument pack expression
9779 itself is already counted as 1 in
9780 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9781 the argument pack is empty. */
9782 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9784 else if (ARGUMENT_PACK_P (orig_arg
))
9786 /* Substitute into each of the arguments. */
9787 new_arg
= TYPE_P (orig_arg
)
9788 ? cxx_make_type (TREE_CODE (orig_arg
))
9789 : make_node (TREE_CODE (orig_arg
));
9791 SET_ARGUMENT_PACK_ARGS (
9793 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9794 args
, complain
, in_decl
));
9796 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9797 new_arg
= error_mark_node
;
9799 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9800 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9802 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9804 if (TREE_TYPE (new_arg
) == error_mark_node
)
9805 new_arg
= error_mark_node
;
9809 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9811 if (new_arg
== error_mark_node
)
9812 return error_mark_node
;
9815 if (new_arg
!= orig_arg
)
9822 /* Make space for the expanded arguments coming from template
9824 t
= make_tree_vec (len
+ expanded_len_adjust
);
9825 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9826 arguments for a member template.
9827 In that case each TREE_VEC in ORIG_T represents a level of template
9828 arguments, and ORIG_T won't carry any non defaulted argument count.
9829 It will rather be the nested TREE_VECs that will carry one.
9830 In other words, ORIG_T carries a non defaulted argument count only
9831 if it doesn't contain any nested TREE_VEC. */
9832 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9834 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9835 count
+= expanded_len_adjust
;
9836 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9838 for (i
= 0, out
= 0; i
< len
; i
++)
9840 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9841 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9842 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9846 /* Now expand the template argument pack "in place". */
9847 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9848 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9852 TREE_VEC_ELT (t
, out
) = elts
[i
];
9860 /* Return the result of substituting ARGS into the template parameters
9861 given by PARMS. If there are m levels of ARGS and m + n levels of
9862 PARMS, then the result will contain n levels of PARMS. For
9863 example, if PARMS is `template <class T> template <class U>
9864 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9865 result will be `template <int*, double, class V>'. */
9868 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9873 /* When substituting into a template, we must set
9874 PROCESSING_TEMPLATE_DECL as the template parameters may be
9875 dependent if they are based on one-another, and the dependency
9876 predicates are short-circuit outside of templates. */
9877 ++processing_template_decl
;
9879 for (new_parms
= &r
;
9880 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9881 new_parms
= &(TREE_CHAIN (*new_parms
)),
9882 parms
= TREE_CHAIN (parms
))
9885 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9888 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9892 if (parms
== error_mark_node
)
9895 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9897 if (tuple
== error_mark_node
)
9900 TREE_VEC_ELT (new_vec
, i
) =
9901 tsubst_template_parm (tuple
, args
, complain
);
9905 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9906 - TMPL_ARGS_DEPTH (args
)),
9907 new_vec
, NULL_TREE
);
9910 --processing_template_decl
;
9915 /* Return the result of substituting ARGS into one template parameter
9916 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9917 parameter and which TREE_PURPOSE is the default argument of the
9918 template parameter. */
9921 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9923 tree default_value
, parm_decl
;
9925 if (args
== NULL_TREE
9927 || t
== error_mark_node
)
9930 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9932 default_value
= TREE_PURPOSE (t
);
9933 parm_decl
= TREE_VALUE (t
);
9935 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9936 if (TREE_CODE (parm_decl
) == PARM_DECL
9937 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9938 parm_decl
= error_mark_node
;
9939 default_value
= tsubst_template_arg (default_value
, args
,
9940 complain
, NULL_TREE
);
9942 return build_tree_list (default_value
, parm_decl
);
9945 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9946 type T. If T is not an aggregate or enumeration type, it is
9947 handled as if by tsubst. IN_DECL is as for tsubst. If
9948 ENTERING_SCOPE is nonzero, T is the context for a template which
9949 we are presently tsubst'ing. Return the substituted value. */
9952 tsubst_aggr_type (tree t
,
9954 tsubst_flags_t complain
,
9961 switch (TREE_CODE (t
))
9964 if (TYPE_PTRMEMFUNC_P (t
))
9965 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9967 /* Else fall through. */
9970 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9975 int saved_unevaluated_operand
;
9976 int saved_inhibit_evaluation_warnings
;
9978 /* In "sizeof(X<I>)" we need to evaluate "I". */
9979 saved_unevaluated_operand
= cp_unevaluated_operand
;
9980 cp_unevaluated_operand
= 0;
9981 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9982 c_inhibit_evaluation_warnings
= 0;
9984 /* First, determine the context for the type we are looking
9986 context
= TYPE_CONTEXT (t
);
9987 if (context
&& TYPE_P (context
))
9989 context
= tsubst_aggr_type (context
, args
, complain
,
9990 in_decl
, /*entering_scope=*/1);
9991 /* If context is a nested class inside a class template,
9992 it may still need to be instantiated (c++/33959). */
9993 context
= complete_type (context
);
9996 /* Then, figure out what arguments are appropriate for the
9997 type we are trying to find. For example, given:
9999 template <class T> struct S;
10000 template <class T, class U> void f(T, U) { S<U> su; }
10002 and supposing that we are instantiating f<int, double>,
10003 then our ARGS will be {int, double}, but, when looking up
10004 S we only want {double}. */
10005 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10006 complain
, in_decl
);
10007 if (argvec
== error_mark_node
)
10008 r
= error_mark_node
;
10011 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10012 entering_scope
, complain
);
10013 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10016 cp_unevaluated_operand
= saved_unevaluated_operand
;
10017 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10022 /* This is not a template type, so there's nothing to do. */
10026 return tsubst (t
, args
, complain
, in_decl
);
10030 /* Substitute into the default argument ARG (a default argument for
10031 FN), which has the indicated TYPE. */
10034 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10036 tree saved_class_ptr
= NULL_TREE
;
10037 tree saved_class_ref
= NULL_TREE
;
10038 int errs
= errorcount
+ sorrycount
;
10040 /* This can happen in invalid code. */
10041 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10044 /* This default argument came from a template. Instantiate the
10045 default argument here, not in tsubst. In the case of
10054 we must be careful to do name lookup in the scope of S<T>,
10055 rather than in the current class. */
10056 push_access_scope (fn
);
10057 /* The "this" pointer is not valid in a default argument. */
10060 saved_class_ptr
= current_class_ptr
;
10061 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10062 saved_class_ref
= current_class_ref
;
10063 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10066 push_deferring_access_checks(dk_no_deferred
);
10067 /* The default argument expression may cause implicitly defined
10068 member functions to be synthesized, which will result in garbage
10069 collection. We must treat this situation as if we were within
10070 the body of function so as to avoid collecting live data on the
10073 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10074 complain
, NULL_TREE
,
10075 /*integral_constant_expression_p=*/false);
10077 pop_deferring_access_checks();
10079 /* Restore the "this" pointer. */
10082 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10083 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10086 if (errorcount
+sorrycount
> errs
10087 && (complain
& tf_warning_or_error
))
10088 inform (input_location
,
10089 " when instantiating default argument for call to %D", fn
);
10091 /* Make sure the default argument is reasonable. */
10092 arg
= check_default_argument (type
, arg
, complain
);
10094 pop_access_scope (fn
);
10099 /* Substitute into all the default arguments for FN. */
10102 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10107 tmpl_args
= DECL_TI_ARGS (fn
);
10109 /* If this function is not yet instantiated, we certainly don't need
10110 its default arguments. */
10111 if (uses_template_parms (tmpl_args
))
10113 /* Don't do this again for clones. */
10114 if (DECL_CLONED_FUNCTION_P (fn
))
10117 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10119 arg
= TREE_CHAIN (arg
))
10120 if (TREE_PURPOSE (arg
))
10121 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10123 TREE_PURPOSE (arg
),
10127 /* Substitute the ARGS into the T, which is a _DECL. Return the
10128 result of the substitution. Issue error and warning messages under
10129 control of COMPLAIN. */
10132 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10134 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10135 location_t saved_loc
;
10136 tree r
= NULL_TREE
;
10138 hashval_t hash
= 0;
10140 /* Set the filename and linenumber to improve error-reporting. */
10141 saved_loc
= input_location
;
10142 input_location
= DECL_SOURCE_LOCATION (t
);
10144 switch (TREE_CODE (t
))
10146 case TEMPLATE_DECL
:
10148 /* We can get here when processing a member function template,
10149 member class template, or template template parameter. */
10150 tree decl
= DECL_TEMPLATE_RESULT (t
);
10155 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10157 /* Template template parameter is treated here. */
10158 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10159 if (new_type
== error_mark_node
)
10160 RETURN (error_mark_node
);
10161 /* If we get a real template back, return it. This can happen in
10162 the context of most_specialized_class. */
10163 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10167 DECL_CHAIN (r
) = NULL_TREE
;
10168 TREE_TYPE (r
) = new_type
;
10169 DECL_TEMPLATE_RESULT (r
)
10170 = build_decl (DECL_SOURCE_LOCATION (decl
),
10171 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10172 DECL_TEMPLATE_PARMS (r
)
10173 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10175 TYPE_NAME (new_type
) = r
;
10179 /* We might already have an instance of this template.
10180 The ARGS are for the surrounding class type, so the
10181 full args contain the tsubst'd args for the context,
10182 plus the innermost args from the template decl. */
10183 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10184 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10185 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10186 /* Because this is a template, the arguments will still be
10187 dependent, even after substitution. If
10188 PROCESSING_TEMPLATE_DECL is not set, the dependency
10189 predicates will short-circuit. */
10190 ++processing_template_decl
;
10191 full_args
= tsubst_template_args (tmpl_args
, args
,
10192 complain
, in_decl
);
10193 --processing_template_decl
;
10194 if (full_args
== error_mark_node
)
10195 RETURN (error_mark_node
);
10197 /* If this is a default template template argument,
10198 tsubst might not have changed anything. */
10199 if (full_args
== tmpl_args
)
10202 hash
= hash_tmpl_and_args (t
, full_args
);
10203 spec
= retrieve_specialization (t
, full_args
, hash
);
10204 if (spec
!= NULL_TREE
)
10210 /* Make a new template decl. It will be similar to the
10211 original, but will record the current template arguments.
10212 We also create a new function declaration, which is just
10213 like the old one, but points to this new template, rather
10214 than the old one. */
10216 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10217 DECL_CHAIN (r
) = NULL_TREE
;
10219 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10221 if (TREE_CODE (decl
) == TYPE_DECL
10222 && !TYPE_DECL_ALIAS_P (decl
))
10225 ++processing_template_decl
;
10226 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10227 --processing_template_decl
;
10228 if (new_type
== error_mark_node
)
10229 RETURN (error_mark_node
);
10231 TREE_TYPE (r
) = new_type
;
10232 /* For a partial specialization, we need to keep pointing to
10233 the primary template. */
10234 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10235 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10236 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10237 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10238 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10243 ++processing_template_decl
;
10244 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10245 --processing_template_decl
;
10246 if (new_decl
== error_mark_node
)
10247 RETURN (error_mark_node
);
10249 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10250 DECL_TI_TEMPLATE (new_decl
) = r
;
10251 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10252 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10253 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10256 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10257 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10258 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10260 /* The template parameters for this new template are all the
10261 template parameters for the old template, except the
10262 outermost level of parameters. */
10263 DECL_TEMPLATE_PARMS (r
)
10264 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10267 if (PRIMARY_TEMPLATE_P (t
))
10268 DECL_PRIMARY_TEMPLATE (r
) = r
;
10270 if (TREE_CODE (decl
) != TYPE_DECL
)
10271 /* Record this non-type partial instantiation. */
10272 register_specialization (r
, t
,
10273 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10278 case FUNCTION_DECL
:
10281 tree argvec
= NULL_TREE
;
10289 /* Nobody should be tsubst'ing into non-template functions. */
10290 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10292 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10297 /* If T is not dependent, just return it. We have to
10298 increment PROCESSING_TEMPLATE_DECL because
10299 value_dependent_expression_p assumes that nothing is
10300 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10301 ++processing_template_decl
;
10302 dependent_p
= value_dependent_expression_p (t
);
10303 --processing_template_decl
;
10307 /* Calculate the most general template of which R is a
10308 specialization, and the complete set of arguments used to
10310 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10311 argvec
= tsubst_template_args (DECL_TI_ARGS
10312 (DECL_TEMPLATE_RESULT
10313 (DECL_TI_TEMPLATE (t
))),
10314 args
, complain
, in_decl
);
10315 if (argvec
== error_mark_node
)
10316 RETURN (error_mark_node
);
10318 /* Check to see if we already have this specialization. */
10319 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10320 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10328 /* We can see more levels of arguments than parameters if
10329 there was a specialization of a member template, like
10332 template <class T> struct S { template <class U> void f(); }
10333 template <> template <class U> void S<int>::f(U);
10335 Here, we'll be substituting into the specialization,
10336 because that's where we can find the code we actually
10337 want to generate, but we'll have enough arguments for
10338 the most general template.
10340 We also deal with the peculiar case:
10342 template <class T> struct S {
10343 template <class U> friend void f();
10345 template <class U> void f() {}
10347 template void f<double>();
10349 Here, the ARGS for the instantiation of will be {int,
10350 double}. But, we only need as many ARGS as there are
10351 levels of template parameters in CODE_PATTERN. We are
10352 careful not to get fooled into reducing the ARGS in
10355 template <class T> struct S { template <class U> void f(U); }
10356 template <class T> template <> void S<T>::f(int) {}
10358 which we can spot because the pattern will be a
10359 specialization in this case. */
10360 args_depth
= TMPL_ARGS_DEPTH (args
);
10362 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10363 if (args_depth
> parms_depth
10364 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10365 args
= get_innermost_template_args (args
, parms_depth
);
10369 /* This special case arises when we have something like this:
10371 template <class T> struct S {
10372 friend void f<int>(int, double);
10375 Here, the DECL_TI_TEMPLATE for the friend declaration
10376 will be an IDENTIFIER_NODE. We are being called from
10377 tsubst_friend_function, and we want only to create a
10378 new decl (R) with appropriate types so that we can call
10379 determine_specialization. */
10380 gen_tmpl
= NULL_TREE
;
10383 if (DECL_CLASS_SCOPE_P (t
))
10385 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10389 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10390 complain
, t
, /*entering_scope=*/1);
10395 ctx
= DECL_CONTEXT (t
);
10397 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10398 if (type
== error_mark_node
)
10399 RETURN (error_mark_node
);
10401 /* If we hit excessive deduction depth, the type is bogus even if
10402 it isn't error_mark_node, so don't build a decl. */
10403 if (excessive_deduction_depth
)
10404 RETURN (error_mark_node
);
10406 /* We do NOT check for matching decls pushed separately at this
10407 point, as they may not represent instantiations of this
10408 template, and in any case are considered separate under the
10411 DECL_USE_TEMPLATE (r
) = 0;
10412 TREE_TYPE (r
) = type
;
10413 /* Clear out the mangled name and RTL for the instantiation. */
10414 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10415 SET_DECL_RTL (r
, NULL
);
10416 /* Leave DECL_INITIAL set on deleted instantiations. */
10417 if (!DECL_DELETED_FN (r
))
10418 DECL_INITIAL (r
) = NULL_TREE
;
10419 DECL_CONTEXT (r
) = ctx
;
10421 /* OpenMP UDRs have the only argument a reference to the declared
10422 type. We want to diagnose if the declared type is a reference,
10423 which is invalid, but as references to references are usually
10424 quietly merged, diagnose it here. */
10425 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10428 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10429 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10430 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10431 error_at (DECL_SOURCE_LOCATION (t
),
10432 "reference type %qT in "
10433 "%<#pragma omp declare reduction%>", argtype
);
10434 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10435 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10439 if (member
&& DECL_CONV_FN_P (r
))
10440 /* Type-conversion operator. Reconstruct the name, in
10441 case it's the name of one of the template's parameters. */
10442 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10444 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10446 DECL_RESULT (r
) = NULL_TREE
;
10448 TREE_STATIC (r
) = 0;
10449 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10450 DECL_EXTERNAL (r
) = 1;
10451 /* If this is an instantiation of a function with internal
10452 linkage, we already know what object file linkage will be
10453 assigned to the instantiation. */
10454 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10455 DECL_DEFER_OUTPUT (r
) = 0;
10456 DECL_CHAIN (r
) = NULL_TREE
;
10457 DECL_PENDING_INLINE_INFO (r
) = 0;
10458 DECL_PENDING_INLINE_P (r
) = 0;
10459 DECL_SAVED_TREE (r
) = NULL_TREE
;
10460 DECL_STRUCT_FUNCTION (r
) = NULL
;
10462 /* We'll re-clone as appropriate in instantiate_template. */
10463 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10465 /* If we aren't complaining now, return on error before we register
10466 the specialization so that we'll complain eventually. */
10467 if ((complain
& tf_error
) == 0
10468 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10469 && !grok_op_properties (r
, /*complain=*/false))
10470 RETURN (error_mark_node
);
10472 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10473 this in the special friend case mentioned above where
10474 GEN_TMPL is NULL. */
10477 DECL_TEMPLATE_INFO (r
)
10478 = build_template_info (gen_tmpl
, argvec
);
10479 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10482 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10484 /* We instantiated this while substituting into
10485 the type earlier (template/friend54.C). */
10488 /* We're not supposed to instantiate default arguments
10489 until they are called, for a template. But, for a
10492 template <class T> void f ()
10493 { extern void g(int i = T()); }
10495 we should do the substitution when the template is
10496 instantiated. We handle the member function case in
10497 instantiate_class_template since the default arguments
10498 might refer to other members of the class. */
10500 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10501 && !uses_template_parms (argvec
))
10502 tsubst_default_arguments (r
, complain
);
10505 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10507 /* Copy the list of befriending classes. */
10508 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10510 friends
= &TREE_CHAIN (*friends
))
10512 *friends
= copy_node (*friends
);
10513 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10518 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10520 maybe_retrofit_in_chrg (r
);
10521 if (DECL_CONSTRUCTOR_P (r
))
10522 grok_ctor_properties (ctx
, r
);
10523 if (DECL_INHERITED_CTOR_BASE (r
))
10524 deduce_inheriting_ctor (r
);
10525 /* If this is an instantiation of a member template, clone it.
10526 If it isn't, that'll be handled by
10527 clone_constructors_and_destructors. */
10528 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10529 clone_function_decl (r
, /*update_method_vec_p=*/0);
10531 else if ((complain
& tf_error
) != 0
10532 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10533 && !grok_op_properties (r
, /*complain=*/true))
10534 RETURN (error_mark_node
);
10536 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10537 SET_DECL_FRIEND_CONTEXT (r
,
10538 tsubst (DECL_FRIEND_CONTEXT (t
),
10539 args
, complain
, in_decl
));
10541 /* Possibly limit visibility based on template args. */
10542 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10543 if (DECL_VISIBILITY_SPECIFIED (t
))
10545 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10546 DECL_ATTRIBUTES (r
)
10547 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10549 determine_visibility (r
);
10550 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10551 && !processing_template_decl
)
10552 defaulted_late_check (r
);
10554 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10555 args
, complain
, in_decl
);
10561 tree type
= NULL_TREE
;
10563 tree expanded_types
= NULL_TREE
;
10564 tree prev_r
= NULL_TREE
;
10565 tree first_r
= NULL_TREE
;
10567 if (DECL_PACK_P (t
))
10569 /* If there is a local specialization that isn't a
10570 parameter pack, it means that we're doing a "simple"
10571 substitution from inside tsubst_pack_expansion. Just
10572 return the local specialization (which will be a single
10574 tree spec
= retrieve_local_specialization (t
);
10576 && TREE_CODE (spec
) == PARM_DECL
10577 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10580 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10581 the parameters in this function parameter pack. */
10582 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10583 complain
, in_decl
);
10584 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10586 len
= TREE_VEC_LENGTH (expanded_types
);
10588 /* Zero-length parameter packs are boring. Just substitute
10591 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10596 /* All we did was update the type. Make a note of that. */
10597 type
= expanded_types
;
10598 expanded_types
= NULL_TREE
;
10602 /* Loop through all of the parameters we'll build. When T is
10603 a function parameter pack, LEN is the number of expanded
10604 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10606 for (i
= 0; i
< len
; ++i
)
10610 if (DECL_TEMPLATE_PARM_P (t
))
10611 SET_DECL_TEMPLATE_PARM_P (r
);
10613 if (expanded_types
)
10614 /* We're on the Ith parameter of the function parameter
10617 /* Get the Ith type. */
10618 type
= TREE_VEC_ELT (expanded_types
, i
);
10620 /* Rename the parameter to include the index. */
10622 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10625 /* We're dealing with a normal parameter. */
10626 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10628 type
= type_decays_to (type
);
10629 TREE_TYPE (r
) = type
;
10630 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10632 if (DECL_INITIAL (r
))
10634 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10635 DECL_INITIAL (r
) = TREE_TYPE (r
);
10637 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10638 complain
, in_decl
);
10641 DECL_CONTEXT (r
) = NULL_TREE
;
10643 if (!DECL_TEMPLATE_PARM_P (r
))
10644 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10646 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10647 args
, complain
, in_decl
);
10649 /* Keep track of the first new parameter we
10650 generate. That's what will be returned to the
10655 /* Build a proper chain of parameters when substituting
10656 into a function parameter pack. */
10658 DECL_CHAIN (prev_r
) = r
;
10661 /* If cp_unevaluated_operand is set, we're just looking for a
10662 single dummy parameter, so don't keep going. */
10663 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10664 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10665 complain
, DECL_CHAIN (t
));
10667 /* FIRST_R contains the start of the chain we've built. */
10674 tree type
= NULL_TREE
;
10675 tree vec
= NULL_TREE
;
10676 tree expanded_types
= NULL_TREE
;
10679 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10681 /* This field is a lambda capture pack. Return a TREE_VEC of
10682 the expanded fields to instantiate_class_template_1 and
10683 store them in the specializations hash table as a
10684 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10685 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10686 complain
, in_decl
);
10687 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10689 len
= TREE_VEC_LENGTH (expanded_types
);
10690 vec
= make_tree_vec (len
);
10694 /* All we did was update the type. Make a note of that. */
10695 type
= expanded_types
;
10696 expanded_types
= NULL_TREE
;
10700 for (int i
= 0; i
< len
; ++i
)
10703 if (expanded_types
)
10705 type
= TREE_VEC_ELT (expanded_types
, i
);
10707 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10710 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10712 if (type
== error_mark_node
)
10713 RETURN (error_mark_node
);
10714 TREE_TYPE (r
) = type
;
10715 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10717 if (DECL_C_BIT_FIELD (r
))
10718 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10719 non-bit-fields DECL_INITIAL is a non-static data member
10720 initializer, which gets deferred instantiation. */
10722 = tsubst_expr (DECL_INITIAL (t
), args
,
10724 /*integral_constant_expression_p=*/true);
10725 else if (DECL_INITIAL (t
))
10727 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10728 NSDMI in perform_member_init. Still set DECL_INITIAL
10729 so that we know there is one. */
10730 DECL_INITIAL (r
) = void_zero_node
;
10731 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10732 retrofit_lang_decl (r
);
10733 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10735 /* We don't have to set DECL_CONTEXT here; it is set by
10736 finish_member_declaration. */
10737 DECL_CHAIN (r
) = NULL_TREE
;
10739 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10740 args
, complain
, in_decl
);
10743 TREE_VEC_ELT (vec
, i
) = r
;
10749 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10750 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10751 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10752 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10753 TREE_TYPE (pack
) = tpack
;
10754 register_specialization (pack
, t
, args
, false, 0);
10760 /* We reach here only for member using decls. We also need to check
10761 uses_template_parms because DECL_DEPENDENT_P is not set for a
10762 using-declaration that designates a member of the current
10763 instantiation (c++/53549). */
10764 if (DECL_DEPENDENT_P (t
)
10765 || uses_template_parms (USING_DECL_SCOPE (t
)))
10767 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10768 complain
, in_decl
);
10769 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10770 r
= do_class_using_decl (inst_scope
, name
);
10772 r
= error_mark_node
;
10775 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10776 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10782 DECL_CHAIN (r
) = NULL_TREE
;
10789 tree argvec
= NULL_TREE
;
10790 tree gen_tmpl
= NULL_TREE
;
10792 tree tmpl
= NULL_TREE
;
10794 tree type
= NULL_TREE
;
10797 if (TREE_CODE (t
) == TYPE_DECL
10798 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10800 /* If this is the canonical decl, we don't have to
10801 mess with instantiations, and often we can't (for
10802 typename, template type parms and such). Note that
10803 TYPE_NAME is not correct for the above test if
10804 we've copied the type for a typedef. */
10805 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10806 if (type
== error_mark_node
)
10807 RETURN (error_mark_node
);
10808 r
= TYPE_NAME (type
);
10812 /* Check to see if we already have the specialization we
10815 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10817 /* T is a static data member or namespace-scope entity.
10818 We have to substitute into namespace-scope variables
10819 (even though such entities are never templates) because
10822 template <class T> void f() { extern T t; }
10824 where the entity referenced is not known until
10825 instantiation time. */
10827 ctx
= DECL_CONTEXT (t
);
10828 if (DECL_CLASS_SCOPE_P (t
))
10830 ctx
= tsubst_aggr_type (ctx
, args
,
10832 in_decl
, /*entering_scope=*/1);
10833 /* If CTX is unchanged, then T is in fact the
10834 specialization we want. That situation occurs when
10835 referencing a static data member within in its own
10836 class. We can use pointer equality, rather than
10837 same_type_p, because DECL_CONTEXT is always
10839 if (ctx
== DECL_CONTEXT (t
)
10840 && (TREE_CODE (t
) != TYPE_DECL
10841 /* ... unless T is a member template; in which
10842 case our caller can be willing to create a
10843 specialization of that template represented
10845 || !(DECL_TI_TEMPLATE (t
)
10846 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10852 tmpl
= DECL_TI_TEMPLATE (t
);
10853 gen_tmpl
= most_general_template (tmpl
);
10854 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10855 if (argvec
== error_mark_node
)
10856 RETURN (error_mark_node
);
10857 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10858 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10863 /* A local variable. */
10865 /* Subsequent calls to pushdecl will fill this in. */
10867 spec
= retrieve_local_specialization (t
);
10869 /* If we already have the specialization we need, there is
10870 nothing more to do. */
10877 /* Create a new node for the specialization we need. */
10879 if (type
== NULL_TREE
)
10881 if (is_typedef_decl (t
))
10882 type
= DECL_ORIGINAL_TYPE (t
);
10884 type
= TREE_TYPE (t
);
10886 && VAR_HAD_UNKNOWN_BOUND (t
)
10887 && type
!= error_mark_node
)
10888 type
= strip_array_domain (type
);
10889 type
= tsubst (type
, args
, complain
, in_decl
);
10893 /* Even if the original location is out of scope, the
10894 newly substituted one is not. */
10895 DECL_DEAD_FOR_LOCAL (r
) = 0;
10896 DECL_INITIALIZED_P (r
) = 0;
10897 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10898 if (type
== error_mark_node
)
10899 RETURN (error_mark_node
);
10900 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10902 /* It may seem that this case cannot occur, since:
10907 declares a function, not a variable. However:
10910 template <typename T> void g() { T t; }
10911 template void g<f>();
10913 is an attempt to declare a variable with function
10915 error ("variable %qD has function type",
10916 /* R is not yet sufficiently initialized, so we
10917 just use its name. */
10919 RETURN (error_mark_node
);
10921 type
= complete_type (type
);
10922 /* Wait until cp_finish_decl to set this again, to handle
10923 circular dependency (template/instantiate6.C). */
10924 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10925 type
= check_var_type (DECL_NAME (r
), type
);
10927 if (DECL_HAS_VALUE_EXPR_P (t
))
10929 tree ve
= DECL_VALUE_EXPR (t
);
10930 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10931 /*constant_expression_p=*/false);
10932 if (REFERENCE_REF_P (ve
))
10934 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10935 ve
= TREE_OPERAND (ve
, 0);
10937 SET_DECL_VALUE_EXPR (r
, ve
);
10940 else if (DECL_SELF_REFERENCE_P (t
))
10941 SET_DECL_SELF_REFERENCE_P (r
);
10942 TREE_TYPE (r
) = type
;
10943 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10944 DECL_CONTEXT (r
) = ctx
;
10945 /* Clear out the mangled name and RTL for the instantiation. */
10946 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10947 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10948 SET_DECL_RTL (r
, NULL
);
10949 /* The initializer must not be expanded until it is required;
10950 see [temp.inst]. */
10951 DECL_INITIAL (r
) = NULL_TREE
;
10952 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10953 SET_DECL_RTL (r
, NULL
);
10954 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10957 /* Possibly limit visibility based on template args. */
10958 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10959 if (DECL_VISIBILITY_SPECIFIED (t
))
10961 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10962 DECL_ATTRIBUTES (r
)
10963 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10965 determine_visibility (r
);
10970 /* A static data member declaration is always marked
10971 external when it is declared in-class, even if an
10972 initializer is present. We mimic the non-template
10973 processing here. */
10974 DECL_EXTERNAL (r
) = 1;
10976 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10977 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10978 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10980 else if (cp_unevaluated_operand
)
10981 gcc_unreachable ();
10983 register_local_specialization (r
, t
);
10985 DECL_CHAIN (r
) = NULL_TREE
;
10987 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10989 args
, complain
, in_decl
);
10991 /* Preserve a typedef that names a type. */
10992 if (is_typedef_decl (r
))
10994 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10995 set_underlying_type (r
);
10998 layout_decl (r
, 0);
11003 gcc_unreachable ();
11008 /* Restore the file and line information. */
11009 input_location
= saved_loc
;
11014 /* Substitute into the ARG_TYPES of a function type.
11015 If END is a TREE_CHAIN, leave it and any following types
11019 tsubst_arg_types (tree arg_types
,
11022 tsubst_flags_t complain
,
11025 tree remaining_arg_types
;
11026 tree type
= NULL_TREE
;
11028 tree expanded_args
= NULL_TREE
;
11031 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11034 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11035 args
, end
, complain
, in_decl
);
11036 if (remaining_arg_types
== error_mark_node
)
11037 return error_mark_node
;
11039 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11041 /* For a pack expansion, perform substitution on the
11042 entire expression. Later on, we'll handle the arguments
11044 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11045 args
, complain
, in_decl
);
11047 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11048 /* So that we'll spin through the parameters, one by one. */
11049 i
= TREE_VEC_LENGTH (expanded_args
);
11052 /* We only partially substituted into the parameter
11053 pack. Our type is TYPE_PACK_EXPANSION. */
11054 type
= expanded_args
;
11055 expanded_args
= NULL_TREE
;
11063 type
= TREE_VEC_ELT (expanded_args
, i
);
11065 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11067 if (type
== error_mark_node
)
11068 return error_mark_node
;
11069 if (VOID_TYPE_P (type
))
11071 if (complain
& tf_error
)
11073 error ("invalid parameter type %qT", type
);
11075 error ("in declaration %q+D", in_decl
);
11077 return error_mark_node
;
11080 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11081 return error_mark_node
;
11083 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11084 top-level qualifiers as required. */
11085 type
= cv_unqualified (type_decays_to (type
));
11087 /* We do not substitute into default arguments here. The standard
11088 mandates that they be instantiated only when needed, which is
11089 done in build_over_call. */
11090 default_arg
= TREE_PURPOSE (arg_types
);
11092 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11094 /* We've instantiated a template before its default arguments
11095 have been parsed. This can happen for a nested template
11096 class, and is not an error unless we require the default
11097 argument in a call of this function. */
11098 remaining_arg_types
=
11099 tree_cons (default_arg
, type
, remaining_arg_types
);
11100 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11103 remaining_arg_types
=
11104 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11107 return remaining_arg_types
;
11110 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11111 *not* handle the exception-specification for FNTYPE, because the
11112 initial substitution of explicitly provided template parameters
11113 during argument deduction forbids substitution into the
11114 exception-specification:
11118 All references in the function type of the function template to the
11119 corresponding template parameters are replaced by the specified tem-
11120 plate argument values. If a substitution in a template parameter or
11121 in the function type of the function template results in an invalid
11122 type, type deduction fails. [Note: The equivalent substitution in
11123 exception specifications is done only when the function is instanti-
11124 ated, at which point a program is ill-formed if the substitution
11125 results in an invalid type.] */
11128 tsubst_function_type (tree t
,
11130 tsubst_flags_t complain
,
11137 /* The TYPE_CONTEXT is not used for function/method types. */
11138 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11140 /* Substitute the return type. */
11141 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11142 if (return_type
== error_mark_node
)
11143 return error_mark_node
;
11144 /* DR 486 clarifies that creation of a function type with an
11145 invalid return type is a deduction failure. */
11146 if (TREE_CODE (return_type
) == ARRAY_TYPE
11147 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11149 if (complain
& tf_error
)
11151 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11152 error ("function returning an array");
11154 error ("function returning a function");
11156 return error_mark_node
;
11159 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11160 return error_mark_node
;
11162 /* Substitute the argument types. */
11163 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11164 complain
, in_decl
);
11165 if (arg_types
== error_mark_node
)
11166 return error_mark_node
;
11168 /* Construct a new type node and return it. */
11169 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11171 fntype
= build_function_type (return_type
, arg_types
);
11172 fntype
= apply_memfn_quals (fntype
,
11173 type_memfn_quals (t
),
11174 type_memfn_rqual (t
));
11178 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11179 if (! MAYBE_CLASS_TYPE_P (r
))
11183 Type deduction may fail for any of the following
11186 -- Attempting to create "pointer to member of T" when T
11187 is not a class type. */
11188 if (complain
& tf_error
)
11189 error ("creating pointer to member function of non-class type %qT",
11191 return error_mark_node
;
11194 fntype
= build_method_type_directly (r
, return_type
,
11195 TREE_CHAIN (arg_types
));
11196 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11198 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11203 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11204 ARGS into that specification, and return the substituted
11205 specification. If there is no specification, return NULL_TREE. */
11208 tsubst_exception_specification (tree fntype
,
11210 tsubst_flags_t complain
,
11217 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11218 new_specs
= NULL_TREE
;
11219 if (specs
&& TREE_PURPOSE (specs
))
11221 /* A noexcept-specifier. */
11222 tree expr
= TREE_PURPOSE (specs
);
11223 if (TREE_CODE (expr
) == INTEGER_CST
)
11227 /* Defer instantiation of noexcept-specifiers to avoid
11228 excessive instantiations (c++/49107). */
11229 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11230 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11232 /* We already partially instantiated this member template,
11233 so combine the new args with the old. */
11234 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11235 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11236 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11237 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11241 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11242 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11246 new_specs
= tsubst_copy_and_build
11247 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11248 /*integral_constant_expression_p=*/true);
11249 new_specs
= build_noexcept_spec (new_specs
, complain
);
11253 if (! TREE_VALUE (specs
))
11260 tree expanded_specs
= NULL_TREE
;
11262 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11264 /* Expand the pack expansion type. */
11265 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11269 if (expanded_specs
== error_mark_node
)
11270 return error_mark_node
;
11271 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11272 len
= TREE_VEC_LENGTH (expanded_specs
);
11275 /* We're substituting into a member template, so
11276 we got a TYPE_PACK_EXPANSION back. Add that
11277 expansion and move on. */
11278 gcc_assert (TREE_CODE (expanded_specs
)
11279 == TYPE_PACK_EXPANSION
);
11280 new_specs
= add_exception_specifier (new_specs
,
11283 specs
= TREE_CHAIN (specs
);
11288 for (i
= 0; i
< len
; ++i
)
11290 if (expanded_specs
)
11291 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11293 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11294 if (spec
== error_mark_node
)
11296 new_specs
= add_exception_specifier (new_specs
, spec
,
11300 specs
= TREE_CHAIN (specs
);
11306 /* Take the tree structure T and replace template parameters used
11307 therein with the argument vector ARGS. IN_DECL is an associated
11308 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11309 Issue error and warning messages under control of COMPLAIN. Note
11310 that we must be relatively non-tolerant of extensions here, in
11311 order to preserve conformance; if we allow substitutions that
11312 should not be allowed, we may allow argument deductions that should
11313 not succeed, and therefore report ambiguous overload situations
11314 where there are none. In theory, we could allow the substitution,
11315 but indicate that it should have failed, and allow our caller to
11316 make sure that the right thing happens, but we don't try to do this
11319 This function is used for dealing with types, decls and the like;
11320 for expressions, use tsubst_expr or tsubst_copy. */
11323 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11325 enum tree_code code
;
11326 tree type
, r
= NULL_TREE
;
11328 if (t
== NULL_TREE
|| t
== error_mark_node
11329 || t
== integer_type_node
11330 || t
== void_type_node
11331 || t
== char_type_node
11332 || t
== unknown_type_node
11333 || TREE_CODE (t
) == NAMESPACE_DECL
11334 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11338 return tsubst_decl (t
, args
, complain
);
11340 if (args
== NULL_TREE
)
11343 code
= TREE_CODE (t
);
11345 if (code
== IDENTIFIER_NODE
)
11346 type
= IDENTIFIER_TYPE_VALUE (t
);
11348 type
= TREE_TYPE (t
);
11350 gcc_assert (type
!= unknown_type_node
);
11352 /* Reuse typedefs. We need to do this to handle dependent attributes,
11353 such as attribute aligned. */
11355 && typedef_variant_p (t
))
11357 tree decl
= TYPE_NAME (t
);
11359 if (alias_template_specialization_p (t
))
11361 /* DECL represents an alias template and we want to
11363 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11364 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11365 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11367 else if (DECL_CLASS_SCOPE_P (decl
)
11368 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11369 && uses_template_parms (DECL_CONTEXT (decl
)))
11371 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11372 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11373 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11375 else if (DECL_FUNCTION_SCOPE_P (decl
)
11376 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11377 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11378 r
= retrieve_local_specialization (decl
);
11380 /* The typedef is from a non-template context. */
11386 r
= cp_build_qualified_type_real
11387 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11388 complain
| tf_ignore_bad_quals
);
11393 /* We don't have an instantiation yet, so drop the typedef. */
11394 int quals
= cp_type_quals (t
);
11395 t
= DECL_ORIGINAL_TYPE (decl
);
11396 t
= cp_build_qualified_type_real (t
, quals
,
11397 complain
| tf_ignore_bad_quals
);
11402 && code
!= TYPENAME_TYPE
11403 && code
!= TEMPLATE_TYPE_PARM
11404 && code
!= IDENTIFIER_NODE
11405 && code
!= FUNCTION_TYPE
11406 && code
!= METHOD_TYPE
)
11407 type
= tsubst (type
, args
, complain
, in_decl
);
11408 if (type
== error_mark_node
)
11409 return error_mark_node
;
11415 case ENUMERAL_TYPE
:
11416 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11417 /*entering_scope=*/0);
11420 case IDENTIFIER_NODE
:
11431 if (t
== integer_type_node
)
11434 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11435 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11439 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11441 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11442 /*integral_constant_expression_p=*/false);
11444 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11446 if (TREE_CODE (max
) == NOP_EXPR
11447 && TREE_SIDE_EFFECTS (omax
)
11448 && !TREE_TYPE (max
))
11449 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11451 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11452 with TREE_SIDE_EFFECTS that indicates this is not an integral
11453 constant expression. */
11454 if (processing_template_decl
11455 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11457 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11458 TREE_SIDE_EFFECTS (max
) = 1;
11461 return compute_array_index_type (NULL_TREE
, max
, complain
);
11464 case TEMPLATE_TYPE_PARM
:
11465 case TEMPLATE_TEMPLATE_PARM
:
11466 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11467 case TEMPLATE_PARM_INDEX
:
11472 tree arg
= NULL_TREE
;
11476 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11477 template_parm_level_and_index (t
, &level
, &idx
);
11479 levels
= TMPL_ARGS_DEPTH (args
);
11480 if (level
<= levels
)
11482 arg
= TMPL_ARG (args
, level
, idx
);
11484 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11486 /* See through ARGUMENT_PACK_SELECT arguments. */
11487 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11488 /* If the selected argument is an expansion E, that most
11489 likely means we were called from
11490 gen_elem_of_pack_expansion_instantiation during the
11491 substituting of pack an argument pack (which Ith
11492 element is a pack expansion, where I is
11493 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11494 In this case, the Ith element resulting from this
11495 substituting is going to be a pack expansion, which
11496 pattern is the pattern of E. Let's return the
11498 gen_elem_of_pack_expansion_instantiation will
11499 build the resulting pack expansion from it. */
11500 if (PACK_EXPANSION_P (arg
))
11501 arg
= PACK_EXPANSION_PATTERN (arg
);
11505 if (arg
== error_mark_node
)
11506 return error_mark_node
;
11507 else if (arg
!= NULL_TREE
)
11509 if (ARGUMENT_PACK_P (arg
))
11510 /* If ARG is an argument pack, we don't actually want to
11511 perform a substitution here, because substitutions
11512 for argument packs are only done
11513 element-by-element. We can get to this point when
11514 substituting the type of a non-type template
11515 parameter pack, when that type actually contains
11516 template parameter packs from an outer template, e.g.,
11518 template<typename... Types> struct A {
11519 template<Types... Values> struct B { };
11523 if (code
== TEMPLATE_TYPE_PARM
)
11526 gcc_assert (TYPE_P (arg
));
11528 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11530 return cp_build_qualified_type_real
11531 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11533 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11535 /* We are processing a type constructed from a
11536 template template parameter. */
11537 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11538 args
, complain
, in_decl
);
11539 if (argvec
== error_mark_node
)
11540 return error_mark_node
;
11542 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11543 || TREE_CODE (arg
) == TEMPLATE_DECL
11544 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11546 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11547 /* Consider this code:
11549 template <template <class> class Template>
11551 template <class Arg> using Bind = Template<Arg>;
11554 template <template <class> class Template, class Arg>
11555 using Instantiate = Template<Arg>; //#0
11557 template <template <class> class Template,
11560 Instantiate<Internal<Template>::template Bind,
11563 When #1 is parsed, the
11564 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11565 parameter `Template' in #0 matches the
11566 UNBOUND_CLASS_TEMPLATE representing the argument
11567 `Internal<Template>::template Bind'; We then want
11568 to assemble the type `Bind<Argument>' that can't
11569 be fully created right now, because
11570 `Internal<Template>' not being complete, the Bind
11571 template cannot be looked up in that context. So
11572 we need to "store" `Bind<Argument>' for later
11573 when the context of Bind becomes complete. Let's
11574 store that in a TYPENAME_TYPE. */
11575 return make_typename_type (TYPE_CONTEXT (arg
),
11576 build_nt (TEMPLATE_ID_EXPR
,
11577 TYPE_IDENTIFIER (arg
),
11582 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11583 are resolving nested-types in the signature of a
11584 member function templates. Otherwise ARG is a
11585 TEMPLATE_DECL and is the real template to be
11587 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11588 arg
= TYPE_NAME (arg
);
11590 r
= lookup_template_class (arg
,
11592 DECL_CONTEXT (arg
),
11593 /*entering_scope=*/0,
11595 return cp_build_qualified_type_real
11596 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11599 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11600 return convert_from_reference (unshare_expr (arg
));
11604 /* This can happen during the attempted tsubst'ing in
11605 unify. This means that we don't yet have any information
11606 about the template parameter in question. */
11609 /* Early in template argument deduction substitution, we don't
11610 want to reduce the level of 'auto', or it will be confused
11611 with a normal template parm in subsequent deduction. */
11612 if (is_auto (t
) && (complain
& tf_partial
))
11615 /* If we get here, we must have been looking at a parm for a
11616 more deeply nested template. Make a new version of this
11617 template parameter, but with a lower level. */
11620 case TEMPLATE_TYPE_PARM
:
11621 case TEMPLATE_TEMPLATE_PARM
:
11622 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11623 if (cp_type_quals (t
))
11625 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11626 r
= cp_build_qualified_type_real
11627 (r
, cp_type_quals (t
),
11628 complain
| (code
== TEMPLATE_TYPE_PARM
11629 ? tf_ignore_bad_quals
: 0));
11634 TEMPLATE_TYPE_PARM_INDEX (r
)
11635 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11636 r
, levels
, args
, complain
);
11637 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11638 TYPE_MAIN_VARIANT (r
) = r
;
11639 TYPE_POINTER_TO (r
) = NULL_TREE
;
11640 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11642 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11643 /* We have reduced the level of the template
11644 template parameter, but not the levels of its
11645 template parameters, so canonical_type_parameter
11646 will not be able to find the canonical template
11647 template parameter for this level. Thus, we
11648 require structural equality checking to compare
11649 TEMPLATE_TEMPLATE_PARMs. */
11650 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11651 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11652 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11654 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11656 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11658 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11659 complain
, in_decl
);
11660 if (argvec
== error_mark_node
)
11661 return error_mark_node
;
11663 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11664 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11669 case TEMPLATE_PARM_INDEX
:
11670 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11674 gcc_unreachable ();
11682 tree purpose
, value
, chain
;
11684 if (t
== void_list_node
)
11687 purpose
= TREE_PURPOSE (t
);
11690 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11691 if (purpose
== error_mark_node
)
11692 return error_mark_node
;
11694 value
= TREE_VALUE (t
);
11697 value
= tsubst (value
, args
, complain
, in_decl
);
11698 if (value
== error_mark_node
)
11699 return error_mark_node
;
11701 chain
= TREE_CHAIN (t
);
11702 if (chain
&& chain
!= void_type_node
)
11704 chain
= tsubst (chain
, args
, complain
, in_decl
);
11705 if (chain
== error_mark_node
)
11706 return error_mark_node
;
11708 if (purpose
== TREE_PURPOSE (t
)
11709 && value
== TREE_VALUE (t
)
11710 && chain
== TREE_CHAIN (t
))
11712 return hash_tree_cons (purpose
, value
, chain
);
11716 /* We should never be tsubsting a binfo. */
11717 gcc_unreachable ();
11720 /* A vector of template arguments. */
11721 gcc_assert (!type
);
11722 return tsubst_template_args (t
, args
, complain
, in_decl
);
11725 case REFERENCE_TYPE
:
11727 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11732 Type deduction may fail for any of the following
11735 -- Attempting to create a pointer to reference type.
11736 -- Attempting to create a reference to a reference type or
11737 a reference to void.
11739 Core issue 106 says that creating a reference to a reference
11740 during instantiation is no longer a cause for failure. We
11741 only enforce this check in strict C++98 mode. */
11742 if ((TREE_CODE (type
) == REFERENCE_TYPE
11743 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11744 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11746 static location_t last_loc
;
11748 /* We keep track of the last time we issued this error
11749 message to avoid spewing a ton of messages during a
11750 single bad template instantiation. */
11751 if (complain
& tf_error
11752 && last_loc
!= input_location
)
11754 if (VOID_TYPE_P (type
))
11755 error ("forming reference to void");
11756 else if (code
== POINTER_TYPE
)
11757 error ("forming pointer to reference type %qT", type
);
11759 error ("forming reference to reference type %qT", type
);
11760 last_loc
= input_location
;
11763 return error_mark_node
;
11765 else if (TREE_CODE (type
) == FUNCTION_TYPE
11766 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11767 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11769 if (complain
& tf_error
)
11771 if (code
== POINTER_TYPE
)
11772 error ("forming pointer to qualified function type %qT",
11775 error ("forming reference to qualified function type %qT",
11778 return error_mark_node
;
11780 else if (code
== POINTER_TYPE
)
11782 r
= build_pointer_type (type
);
11783 if (TREE_CODE (type
) == METHOD_TYPE
)
11784 r
= build_ptrmemfunc_type (r
);
11786 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11787 /* In C++0x, during template argument substitution, when there is an
11788 attempt to create a reference to a reference type, reference
11789 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11791 "If a template-argument for a template-parameter T names a type
11792 that is a reference to a type A, an attempt to create the type
11793 'lvalue reference to cv T' creates the type 'lvalue reference to
11794 A,' while an attempt to create the type type rvalue reference to
11795 cv T' creates the type T"
11797 r
= cp_build_reference_type
11799 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11801 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11802 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11804 if (cxx_dialect
>= cxx1y
11805 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11806 && array_of_runtime_bound_p (type
))
11808 if (complain
& tf_warning_or_error
)
11810 (input_location
, OPT_Wvla
,
11811 code
== REFERENCE_TYPE
11812 ? G_("cannot declare reference to array of runtime bound")
11813 : G_("cannot declare pointer to array of runtime bound"));
11815 r
= error_mark_node
;
11818 if (r
!= error_mark_node
)
11819 /* Will this ever be needed for TYPE_..._TO values? */
11826 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11827 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11831 Type deduction may fail for any of the following
11834 -- Attempting to create "pointer to member of T" when T
11835 is not a class type. */
11836 if (complain
& tf_error
)
11837 error ("creating pointer to member of non-class type %qT", r
);
11838 return error_mark_node
;
11840 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11842 if (complain
& tf_error
)
11843 error ("creating pointer to member reference type %qT", type
);
11844 return error_mark_node
;
11846 if (VOID_TYPE_P (type
))
11848 if (complain
& tf_error
)
11849 error ("creating pointer to member of type void");
11850 return error_mark_node
;
11852 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11853 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11855 /* The type of the implicit object parameter gets its
11856 cv-qualifiers from the FUNCTION_TYPE. */
11859 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11860 type_memfn_rqual (type
));
11861 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11862 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11866 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11870 case FUNCTION_TYPE
:
11875 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11876 if (fntype
== error_mark_node
)
11877 return error_mark_node
;
11879 /* Substitute the exception specification. */
11880 specs
= tsubst_exception_specification (t
, args
, complain
,
11881 in_decl
, /*defer_ok*/true);
11882 if (specs
== error_mark_node
)
11883 return error_mark_node
;
11885 fntype
= build_exception_variant (fntype
, specs
);
11890 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11891 if (domain
== error_mark_node
)
11892 return error_mark_node
;
11894 /* As an optimization, we avoid regenerating the array type if
11895 it will obviously be the same as T. */
11896 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11899 /* These checks should match the ones in grokdeclarator.
11903 The deduction may fail for any of the following reasons:
11905 -- Attempting to create an array with an element type that
11906 is void, a function type, or a reference type, or [DR337]
11907 an abstract class type. */
11908 if (VOID_TYPE_P (type
)
11909 || TREE_CODE (type
) == FUNCTION_TYPE
11910 || TREE_CODE (type
) == REFERENCE_TYPE
)
11912 if (complain
& tf_error
)
11913 error ("creating array of %qT", type
);
11914 return error_mark_node
;
11917 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11918 return error_mark_node
;
11920 r
= build_cplus_array_type (type
, domain
);
11922 if (TYPE_USER_ALIGN (t
))
11924 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11925 TYPE_USER_ALIGN (r
) = 1;
11931 case TYPENAME_TYPE
:
11933 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11934 in_decl
, /*entering_scope=*/1);
11935 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11936 complain
, in_decl
);
11938 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11939 return error_mark_node
;
11941 if (!MAYBE_CLASS_TYPE_P (ctx
))
11943 if (complain
& tf_error
)
11944 error ("%qT is not a class, struct, or union type", ctx
);
11945 return error_mark_node
;
11947 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11949 /* Normally, make_typename_type does not require that the CTX
11950 have complete type in order to allow things like:
11952 template <class T> struct S { typename S<T>::X Y; };
11954 But, such constructs have already been resolved by this
11955 point, so here CTX really should have complete type, unless
11956 it's a partial instantiation. */
11957 ctx
= complete_type (ctx
);
11958 if (!COMPLETE_TYPE_P (ctx
))
11960 if (complain
& tf_error
)
11961 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11962 return error_mark_node
;
11966 f
= make_typename_type (ctx
, f
, typename_type
,
11967 complain
| tf_keep_type_decl
);
11968 if (f
== error_mark_node
)
11970 if (TREE_CODE (f
) == TYPE_DECL
)
11972 complain
|= tf_ignore_bad_quals
;
11976 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11978 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11980 if (complain
& tf_error
)
11981 error ("%qT resolves to %qT, which is not an enumeration type",
11984 return error_mark_node
;
11986 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11988 if (complain
& tf_error
)
11989 error ("%qT resolves to %qT, which is is not a class type",
11992 return error_mark_node
;
11996 return cp_build_qualified_type_real
11997 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12000 case UNBOUND_CLASS_TEMPLATE
:
12002 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12003 in_decl
, /*entering_scope=*/1);
12004 tree name
= TYPE_IDENTIFIER (t
);
12005 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12007 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12008 return error_mark_node
;
12011 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12012 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12019 ++cp_unevaluated_operand
;
12020 ++c_inhibit_evaluation_warnings
;
12022 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12024 /*integral_constant_expression_p=*/false);
12026 --cp_unevaluated_operand
;
12027 --c_inhibit_evaluation_warnings
;
12029 type
= finish_typeof (type
);
12030 return cp_build_qualified_type_real (type
,
12032 | cp_type_quals (type
),
12036 case DECLTYPE_TYPE
:
12040 ++cp_unevaluated_operand
;
12041 ++c_inhibit_evaluation_warnings
;
12043 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12044 complain
|tf_decltype
, in_decl
,
12045 /*function_p*/false,
12046 /*integral_constant_expression*/false);
12048 --cp_unevaluated_operand
;
12049 --c_inhibit_evaluation_warnings
;
12051 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12052 type
= lambda_capture_field_type (type
,
12053 DECLTYPE_FOR_INIT_CAPTURE (t
));
12054 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12055 type
= lambda_proxy_type (type
);
12058 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12059 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12061 /* In a template ~id could be either a complement expression
12062 or an unqualified-id naming a destructor; if instantiating
12063 it produces an expression, it's not an id-expression or
12066 type
= finish_decltype_type (type
, id
, complain
);
12068 return cp_build_qualified_type_real (type
,
12070 | cp_type_quals (type
),
12074 case UNDERLYING_TYPE
:
12076 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12077 complain
, in_decl
);
12078 return finish_underlying_type (type
);
12081 case TYPE_ARGUMENT_PACK
:
12082 case NONTYPE_ARGUMENT_PACK
:
12084 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12086 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12090 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12092 /* For template nontype argument packs, also substitute into
12094 if (code
== NONTYPE_ARGUMENT_PACK
)
12095 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12113 /* We should use one of the expression tsubsts for these codes. */
12114 gcc_unreachable ();
12117 sorry ("use of %qs in template", get_tree_code_name (code
));
12118 return error_mark_node
;
12122 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12123 type of the expression on the left-hand side of the "." or "->"
12127 tsubst_baselink (tree baselink
, tree object_type
,
12128 tree args
, tsubst_flags_t complain
, tree in_decl
)
12131 tree qualifying_scope
;
12134 tree template_args
= 0;
12135 bool template_id_p
= false;
12136 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12138 /* A baselink indicates a function from a base class. Both the
12139 BASELINK_ACCESS_BINFO and the base class referenced may
12140 indicate bases of the template class, rather than the
12141 instantiated class. In addition, lookups that were not
12142 ambiguous before may be ambiguous now. Therefore, we perform
12143 the lookup again. */
12144 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12145 qualifying_scope
= tsubst (qualifying_scope
, args
,
12146 complain
, in_decl
);
12147 fns
= BASELINK_FUNCTIONS (baselink
);
12148 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12149 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12151 template_id_p
= true;
12152 template_args
= TREE_OPERAND (fns
, 1);
12153 fns
= TREE_OPERAND (fns
, 0);
12155 template_args
= tsubst_template_args (template_args
, args
,
12156 complain
, in_decl
);
12158 name
= DECL_NAME (get_first_fn (fns
));
12159 if (IDENTIFIER_TYPENAME_P (name
))
12160 name
= mangle_conv_op_name_for_type (optype
);
12161 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12163 return error_mark_node
;
12165 /* If lookup found a single function, mark it as used at this
12166 point. (If it lookup found multiple functions the one selected
12167 later by overload resolution will be marked as used at that
12169 if (BASELINK_P (baselink
))
12170 fns
= BASELINK_FUNCTIONS (baselink
);
12171 if (!template_id_p
&& !really_overloaded_fn (fns
))
12172 mark_used (OVL_CURRENT (fns
));
12174 /* Add back the template arguments, if present. */
12175 if (BASELINK_P (baselink
) && template_id_p
)
12176 BASELINK_FUNCTIONS (baselink
)
12177 = build_nt (TEMPLATE_ID_EXPR
,
12178 BASELINK_FUNCTIONS (baselink
),
12180 /* Update the conversion operator type. */
12181 BASELINK_OPTYPE (baselink
) = optype
;
12184 object_type
= current_class_type
;
12187 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12193 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12194 true if the qualified-id will be a postfix-expression in-and-of
12195 itself; false if more of the postfix-expression follows the
12196 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12200 tsubst_qualified_id (tree qualified_id
, tree args
,
12201 tsubst_flags_t complain
, tree in_decl
,
12202 bool done
, bool address_p
)
12208 tree template_args
;
12209 location_t loc
= UNKNOWN_LOCATION
;
12211 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12213 /* Figure out what name to look up. */
12214 name
= TREE_OPERAND (qualified_id
, 1);
12215 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12217 is_template
= true;
12218 loc
= EXPR_LOCATION (name
);
12219 template_args
= TREE_OPERAND (name
, 1);
12221 template_args
= tsubst_template_args (template_args
, args
,
12222 complain
, in_decl
);
12223 name
= TREE_OPERAND (name
, 0);
12227 is_template
= false;
12228 template_args
= NULL_TREE
;
12231 /* Substitute into the qualifying scope. When there are no ARGS, we
12232 are just trying to simplify a non-dependent expression. In that
12233 case the qualifying scope may be dependent, and, in any case,
12234 substituting will not help. */
12235 scope
= TREE_OPERAND (qualified_id
, 0);
12238 scope
= tsubst (scope
, args
, complain
, in_decl
);
12239 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12244 if (dependent_scope_p (scope
))
12247 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12248 return build_qualified_name (NULL_TREE
, scope
, expr
,
12249 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12252 if (!BASELINK_P (name
) && !DECL_P (expr
))
12254 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12256 /* A BIT_NOT_EXPR is used to represent a destructor. */
12257 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12259 error ("qualifying type %qT does not match destructor name ~%qT",
12260 scope
, TREE_OPERAND (expr
, 0));
12261 expr
= error_mark_node
;
12264 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12265 /*is_type_p=*/0, false);
12268 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12269 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12270 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12272 if (complain
& tf_error
)
12274 error ("dependent-name %qE is parsed as a non-type, but "
12275 "instantiation yields a type", qualified_id
);
12276 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12278 return error_mark_node
;
12284 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12286 /* Remember that there was a reference to this entity. */
12290 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12292 if (complain
& tf_error
)
12293 qualified_name_lookup_error (scope
,
12294 TREE_OPERAND (qualified_id
, 1),
12295 expr
, input_location
);
12296 return error_mark_node
;
12300 expr
= lookup_template_function (expr
, template_args
);
12302 if (expr
== error_mark_node
&& complain
& tf_error
)
12303 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12304 expr
, input_location
);
12305 else if (TYPE_P (scope
))
12307 expr
= (adjust_result_of_qualified_name_lookup
12308 (expr
, scope
, current_nonlambda_class_type ()));
12309 expr
= (finish_qualified_id_expr
12310 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12311 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12312 /*template_arg_p=*/false, complain
));
12315 /* Expressions do not generally have reference type. */
12316 if (TREE_CODE (expr
) != SCOPE_REF
12317 /* However, if we're about to form a pointer-to-member, we just
12318 want the referenced member referenced. */
12319 && TREE_CODE (expr
) != OFFSET_REF
)
12320 expr
= convert_from_reference (expr
);
12325 /* Like tsubst, but deals with expressions. This function just replaces
12326 template parms; to finish processing the resultant expression, use
12327 tsubst_copy_and_build or tsubst_expr. */
12330 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12332 enum tree_code code
;
12335 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12338 code
= TREE_CODE (t
);
12343 r
= retrieve_local_specialization (t
);
12345 if (r
== NULL_TREE
)
12347 /* We get here for a use of 'this' in an NSDMI. */
12348 if (DECL_NAME (t
) == this_identifier
12349 && at_function_scope_p ()
12350 && DECL_CONSTRUCTOR_P (current_function_decl
))
12351 return current_class_ptr
;
12353 /* This can happen for a parameter name used later in a function
12354 declaration (such as in a late-specified return type). Just
12355 make a dummy decl, since it's only used for its type. */
12356 gcc_assert (cp_unevaluated_operand
!= 0);
12357 r
= tsubst_decl (t
, args
, complain
);
12358 /* Give it the template pattern as its context; its true context
12359 hasn't been instantiated yet and this is good enough for
12361 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12364 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12365 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12374 if (DECL_TEMPLATE_PARM_P (t
))
12375 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12376 /* There is no need to substitute into namespace-scope
12378 if (DECL_NAMESPACE_SCOPE_P (t
))
12380 /* If ARGS is NULL, then T is known to be non-dependent. */
12381 if (args
== NULL_TREE
)
12382 return integral_constant_value (t
);
12384 /* Unfortunately, we cannot just call lookup_name here.
12387 template <int I> int f() {
12389 struct S { void g() { E e = a; } };
12392 When we instantiate f<7>::S::g(), say, lookup_name is not
12393 clever enough to find f<7>::a. */
12395 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12396 /*entering_scope=*/0);
12398 for (v
= TYPE_VALUES (enum_type
);
12400 v
= TREE_CHAIN (v
))
12401 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12402 return TREE_VALUE (v
);
12404 /* We didn't find the name. That should never happen; if
12405 name-lookup found it during preliminary parsing, we
12406 should find it again here during instantiation. */
12407 gcc_unreachable ();
12412 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12414 /* Check for a local specialization set up by
12415 tsubst_pack_expansion. */
12416 tree r
= retrieve_local_specialization (t
);
12419 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12420 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12424 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12425 tsubst_decl put in the hash table. */
12426 return retrieve_specialization (t
, args
, 0);
12429 if (DECL_CONTEXT (t
))
12433 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12434 /*entering_scope=*/1);
12435 if (ctx
!= DECL_CONTEXT (t
))
12437 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12440 if (complain
& tf_error
)
12441 error ("using invalid field %qD", t
);
12442 return error_mark_node
;
12451 case FUNCTION_DECL
:
12452 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12453 r
= tsubst (t
, args
, complain
, in_decl
);
12454 else if (local_variable_p (t
))
12456 r
= retrieve_local_specialization (t
);
12457 if (r
== NULL_TREE
)
12459 if (DECL_ANON_UNION_VAR_P (t
))
12461 /* Just use name lookup to find a member alias for an
12462 anonymous union, but then add it to the hash table. */
12463 r
= lookup_name (DECL_NAME (t
));
12464 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12465 register_local_specialization (r
, t
);
12469 gcc_assert (errorcount
|| sorrycount
);
12470 return error_mark_node
;
12479 case NAMESPACE_DECL
:
12483 /* An OVERLOAD will always be a non-dependent overload set; an
12484 overload set from function scope will just be represented with an
12485 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12486 gcc_assert (!uses_template_parms (t
));
12490 return tsubst_baselink (t
, current_nonlambda_class_type (),
12491 args
, complain
, in_decl
);
12493 case TEMPLATE_DECL
:
12494 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12495 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12496 args
, complain
, in_decl
);
12497 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12498 return tsubst (t
, args
, complain
, in_decl
);
12499 else if (DECL_CLASS_SCOPE_P (t
)
12500 && uses_template_parms (DECL_CONTEXT (t
)))
12502 /* Template template argument like the following example need
12505 template <template <class> class TT> struct C {};
12506 template <class T> struct D {
12507 template <class U> struct E {};
12512 We are processing the template argument `E' in #1 for
12513 the template instantiation #2. Originally, `E' is a
12514 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12515 have to substitute this with one having context `D<int>'. */
12517 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12518 return lookup_field (context
, DECL_NAME(t
), 0, false);
12521 /* Ordinary template template argument. */
12525 case REINTERPRET_CAST_EXPR
:
12526 case CONST_CAST_EXPR
:
12527 case STATIC_CAST_EXPR
:
12528 case DYNAMIC_CAST_EXPR
:
12529 case IMPLICIT_CONV_EXPR
:
12533 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12534 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12537 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12540 tree expanded
, op
= TREE_OPERAND (t
, 0);
12543 if (SIZEOF_EXPR_TYPE_P (t
))
12544 op
= TREE_TYPE (op
);
12546 ++cp_unevaluated_operand
;
12547 ++c_inhibit_evaluation_warnings
;
12548 /* We only want to compute the number of arguments. */
12549 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12550 --cp_unevaluated_operand
;
12551 --c_inhibit_evaluation_warnings
;
12553 if (TREE_CODE (expanded
) == TREE_VEC
)
12554 len
= TREE_VEC_LENGTH (expanded
);
12556 if (expanded
== error_mark_node
)
12557 return error_mark_node
;
12558 else if (PACK_EXPANSION_P (expanded
)
12559 || (TREE_CODE (expanded
) == TREE_VEC
12561 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12563 if (TREE_CODE (expanded
) == TREE_VEC
)
12564 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12566 if (TYPE_P (expanded
))
12567 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12568 complain
& tf_error
);
12570 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12571 complain
& tf_error
);
12574 return build_int_cst (size_type_node
, len
);
12576 if (SIZEOF_EXPR_TYPE_P (t
))
12578 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12579 args
, complain
, in_decl
);
12580 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12581 r
= build1 (SIZEOF_EXPR
,
12582 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12583 SIZEOF_EXPR_TYPE_P (r
) = 1;
12590 case TRUTH_NOT_EXPR
:
12593 case UNARY_PLUS_EXPR
: /* Unary + */
12595 case AT_ENCODE_EXPR
:
12599 case REALPART_EXPR
:
12600 case IMAGPART_EXPR
:
12603 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12604 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12606 case COMPONENT_REF
:
12611 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12612 name
= TREE_OPERAND (t
, 1);
12613 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12615 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12616 complain
, in_decl
);
12617 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12619 else if (TREE_CODE (name
) == SCOPE_REF
12620 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12622 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12623 complain
, in_decl
);
12624 name
= TREE_OPERAND (name
, 1);
12625 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12626 complain
, in_decl
);
12627 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12628 name
= build_qualified_name (/*type=*/NULL_TREE
,
12630 /*template_p=*/false);
12632 else if (BASELINK_P (name
))
12633 name
= tsubst_baselink (name
,
12634 non_reference (TREE_TYPE (object
)),
12638 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12639 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12645 case TRUNC_DIV_EXPR
:
12646 case CEIL_DIV_EXPR
:
12647 case FLOOR_DIV_EXPR
:
12648 case ROUND_DIV_EXPR
:
12649 case EXACT_DIV_EXPR
:
12653 case TRUNC_MOD_EXPR
:
12654 case FLOOR_MOD_EXPR
:
12655 case TRUTH_ANDIF_EXPR
:
12656 case TRUTH_ORIF_EXPR
:
12657 case TRUTH_AND_EXPR
:
12658 case TRUTH_OR_EXPR
:
12671 case COMPOUND_EXPR
:
12674 case PREDECREMENT_EXPR
:
12675 case PREINCREMENT_EXPR
:
12676 case POSTDECREMENT_EXPR
:
12677 case POSTINCREMENT_EXPR
:
12679 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12680 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12683 return build_qualified_name (/*type=*/NULL_TREE
,
12684 tsubst_copy (TREE_OPERAND (t
, 0),
12685 args
, complain
, in_decl
),
12686 tsubst_copy (TREE_OPERAND (t
, 1),
12687 args
, complain
, in_decl
),
12688 QUALIFIED_NAME_IS_TEMPLATE (t
));
12693 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12694 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12695 NULL_TREE
, NULL_TREE
);
12699 int n
= VL_EXP_OPERAND_LENGTH (t
);
12700 tree result
= build_vl_exp (CALL_EXPR
, n
);
12702 for (i
= 0; i
< n
; i
++)
12703 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12704 complain
, in_decl
);
12710 case PSEUDO_DTOR_EXPR
:
12711 case VEC_PERM_EXPR
:
12714 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12715 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12716 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12717 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12724 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12725 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12726 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12727 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12734 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12735 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12736 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12737 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12741 case TEMPLATE_ID_EXPR
:
12743 /* Substituted template arguments */
12744 tree fn
= TREE_OPERAND (t
, 0);
12745 tree targs
= TREE_OPERAND (t
, 1);
12747 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12749 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12751 return lookup_template_function (fn
, targs
);
12756 tree purpose
, value
, chain
;
12758 if (t
== void_list_node
)
12761 purpose
= TREE_PURPOSE (t
);
12763 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12764 value
= TREE_VALUE (t
);
12766 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12767 chain
= TREE_CHAIN (t
);
12768 if (chain
&& chain
!= void_type_node
)
12769 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12770 if (purpose
== TREE_PURPOSE (t
)
12771 && value
== TREE_VALUE (t
)
12772 && chain
== TREE_CHAIN (t
))
12774 return tree_cons (purpose
, value
, chain
);
12779 case ENUMERAL_TYPE
:
12781 case TEMPLATE_TYPE_PARM
:
12782 case TEMPLATE_TEMPLATE_PARM
:
12783 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12784 case TEMPLATE_PARM_INDEX
:
12786 case REFERENCE_TYPE
:
12788 case FUNCTION_TYPE
:
12791 case TYPENAME_TYPE
:
12792 case UNBOUND_CLASS_TEMPLATE
:
12794 case DECLTYPE_TYPE
:
12796 return tsubst (t
, args
, complain
, in_decl
);
12800 /* Fall through. */
12801 case IDENTIFIER_NODE
:
12802 if (IDENTIFIER_TYPENAME_P (t
))
12804 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12805 return mangle_conv_op_name_for_type (new_type
);
12811 /* This is handled by tsubst_copy_and_build. */
12812 gcc_unreachable ();
12815 return build_x_va_arg (EXPR_LOCATION (t
),
12816 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12818 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12820 case CLEANUP_POINT_EXPR
:
12821 /* We shouldn't have built any of these during initial template
12822 generation. Instead, they should be built during instantiation
12823 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12824 gcc_unreachable ();
12828 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12829 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12830 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12831 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12832 mark_used (TREE_OPERAND (r
, 1));
12835 case EXPR_PACK_EXPANSION
:
12836 error ("invalid use of pack expansion expression");
12837 return error_mark_node
;
12839 case NONTYPE_ARGUMENT_PACK
:
12840 error ("use %<...%> to expand argument pack");
12841 return error_mark_node
;
12848 /* Instantiate any typedefs in the type. */
12849 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12850 r
= fold_convert (type
, t
);
12851 gcc_assert (TREE_CODE (r
) == code
);
12856 /* These can sometimes show up in a partial instantiation, but never
12857 involve template parms. */
12858 gcc_assert (!uses_template_parms (t
));
12862 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12863 gcc_checking_assert (false);
12868 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12871 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12872 tree args
, tsubst_flags_t complain
, tree in_decl
)
12874 tree new_clauses
= NULL
, nc
, oc
;
12876 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12878 nc
= copy_node (oc
);
12879 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12882 switch (OMP_CLAUSE_CODE (nc
))
12884 case OMP_CLAUSE_LASTPRIVATE
:
12885 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12887 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12888 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12889 in_decl
, /*integral_constant_expression_p=*/false);
12890 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12891 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12894 case OMP_CLAUSE_PRIVATE
:
12895 case OMP_CLAUSE_SHARED
:
12896 case OMP_CLAUSE_FIRSTPRIVATE
:
12897 case OMP_CLAUSE_COPYIN
:
12898 case OMP_CLAUSE_COPYPRIVATE
:
12899 case OMP_CLAUSE_IF
:
12900 case OMP_CLAUSE_NUM_THREADS
:
12901 case OMP_CLAUSE_SCHEDULE
:
12902 case OMP_CLAUSE_COLLAPSE
:
12903 case OMP_CLAUSE_FINAL
:
12904 case OMP_CLAUSE_DEPEND
:
12905 case OMP_CLAUSE_FROM
:
12906 case OMP_CLAUSE_TO
:
12907 case OMP_CLAUSE_UNIFORM
:
12908 case OMP_CLAUSE_MAP
:
12909 case OMP_CLAUSE_DEVICE
:
12910 case OMP_CLAUSE_DIST_SCHEDULE
:
12911 case OMP_CLAUSE_NUM_TEAMS
:
12912 case OMP_CLAUSE_THREAD_LIMIT
:
12913 case OMP_CLAUSE_SAFELEN
:
12914 case OMP_CLAUSE_SIMDLEN
:
12915 OMP_CLAUSE_OPERAND (nc
, 0)
12916 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12917 in_decl
, /*integral_constant_expression_p=*/false);
12919 case OMP_CLAUSE_REDUCTION
:
12920 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12922 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12923 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12925 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12926 complain
, in_decl
);
12927 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12928 = build_qualified_name (NULL_TREE
, scope
,
12929 TREE_OPERAND (placeholder
, 1),
12933 gcc_assert (identifier_p (placeholder
));
12935 OMP_CLAUSE_OPERAND (nc
, 0)
12936 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12937 in_decl
, /*integral_constant_expression_p=*/false);
12939 case OMP_CLAUSE_LINEAR
:
12940 case OMP_CLAUSE_ALIGNED
:
12941 OMP_CLAUSE_OPERAND (nc
, 0)
12942 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12943 in_decl
, /*integral_constant_expression_p=*/false);
12944 OMP_CLAUSE_OPERAND (nc
, 1)
12945 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12946 in_decl
, /*integral_constant_expression_p=*/false);
12949 case OMP_CLAUSE_NOWAIT
:
12950 case OMP_CLAUSE_ORDERED
:
12951 case OMP_CLAUSE_DEFAULT
:
12952 case OMP_CLAUSE_UNTIED
:
12953 case OMP_CLAUSE_MERGEABLE
:
12954 case OMP_CLAUSE_INBRANCH
:
12955 case OMP_CLAUSE_NOTINBRANCH
:
12956 case OMP_CLAUSE_PROC_BIND
:
12957 case OMP_CLAUSE_FOR
:
12958 case OMP_CLAUSE_PARALLEL
:
12959 case OMP_CLAUSE_SECTIONS
:
12960 case OMP_CLAUSE_TASKGROUP
:
12963 gcc_unreachable ();
12967 new_clauses
= nreverse (new_clauses
);
12969 new_clauses
= finish_omp_clauses (new_clauses
);
12970 return new_clauses
;
12973 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12976 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12979 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12981 tree purpose
, value
, chain
;
12986 if (TREE_CODE (t
) != TREE_LIST
)
12987 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12988 /*function_p=*/false,
12989 /*integral_constant_expression_p=*/false);
12991 if (t
== void_list_node
)
12994 purpose
= TREE_PURPOSE (t
);
12996 purpose
= RECUR (purpose
);
12997 value
= TREE_VALUE (t
);
13000 if (TREE_CODE (value
) != LABEL_DECL
)
13001 value
= RECUR (value
);
13004 value
= lookup_label (DECL_NAME (value
));
13005 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13006 TREE_USED (value
) = 1;
13009 chain
= TREE_CHAIN (t
);
13010 if (chain
&& chain
!= void_type_node
)
13011 chain
= RECUR (chain
);
13012 return tree_cons (purpose
, value
, chain
);
13016 /* Substitute one OMP_FOR iterator. */
13019 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13020 tree condv
, tree incrv
, tree
*clauses
,
13021 tree args
, tsubst_flags_t complain
, tree in_decl
,
13022 bool integral_constant_expression_p
)
13024 #define RECUR(NODE) \
13025 tsubst_expr ((NODE), args, complain, in_decl, \
13026 integral_constant_expression_p)
13027 tree decl
, init
, cond
, incr
;
13030 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13031 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13032 decl
= TREE_OPERAND (init
, 0);
13033 init
= TREE_OPERAND (init
, 1);
13034 /* Do this before substituting into decl to handle 'auto'. */
13035 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13036 init
= RECUR (init
);
13037 decl
= RECUR (decl
);
13040 gcc_assert (!processing_template_decl
);
13041 init
= DECL_INITIAL (decl
);
13042 DECL_INITIAL (decl
) = NULL_TREE
;
13045 gcc_assert (!type_dependent_expression_p (decl
));
13047 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13049 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13050 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13051 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13052 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13053 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13054 RECUR (TREE_OPERAND (incr
, 1)),
13057 incr
= RECUR (incr
);
13058 TREE_VEC_ELT (declv
, i
) = decl
;
13059 TREE_VEC_ELT (initv
, i
) = init
;
13060 TREE_VEC_ELT (condv
, i
) = cond
;
13061 TREE_VEC_ELT (incrv
, i
) = incr
;
13065 if (init
&& !init_decl
)
13068 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13070 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13071 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13072 && OMP_CLAUSE_DECL (c
) == decl
)
13074 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13075 && OMP_CLAUSE_DECL (c
) == decl
)
13076 error ("iteration variable %qD should not be firstprivate", decl
);
13077 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13078 && OMP_CLAUSE_DECL (c
) == decl
)
13079 error ("iteration variable %qD should not be reduction", decl
);
13083 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13084 OMP_CLAUSE_DECL (c
) = decl
;
13085 c
= finish_omp_clauses (c
);
13088 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13093 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13094 if (COMPARISON_CLASS_P (cond
))
13095 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13096 RECUR (TREE_OPERAND (cond
, 0)),
13097 RECUR (TREE_OPERAND (cond
, 1)));
13099 cond
= RECUR (cond
);
13100 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13101 switch (TREE_CODE (incr
))
13103 case PREINCREMENT_EXPR
:
13104 case PREDECREMENT_EXPR
:
13105 case POSTINCREMENT_EXPR
:
13106 case POSTDECREMENT_EXPR
:
13107 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13108 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13111 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13112 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13114 tree rhs
= TREE_OPERAND (incr
, 1);
13115 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13116 RECUR (TREE_OPERAND (incr
, 0)),
13117 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13118 RECUR (TREE_OPERAND (rhs
, 0)),
13119 RECUR (TREE_OPERAND (rhs
, 1))));
13122 incr
= RECUR (incr
);
13125 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13126 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13128 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13129 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13130 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13131 TREE_TYPE (decl
), lhs
,
13132 RECUR (TREE_OPERAND (incr
, 2))));
13134 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13135 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13136 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13138 tree rhs
= TREE_OPERAND (incr
, 2);
13139 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13140 RECUR (TREE_OPERAND (incr
, 0)),
13141 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13142 RECUR (TREE_OPERAND (rhs
, 0)),
13143 RECUR (TREE_OPERAND (rhs
, 1))));
13146 incr
= RECUR (incr
);
13149 incr
= RECUR (incr
);
13153 TREE_VEC_ELT (declv
, i
) = decl
;
13154 TREE_VEC_ELT (initv
, i
) = init
;
13155 TREE_VEC_ELT (condv
, i
) = cond
;
13156 TREE_VEC_ELT (incrv
, i
) = incr
;
13160 /* Like tsubst_copy for expressions, etc. but also does semantic
13164 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13165 bool integral_constant_expression_p
)
13167 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13168 #define RECUR(NODE) \
13169 tsubst_expr ((NODE), args, complain, in_decl, \
13170 integral_constant_expression_p)
13176 if (t
== NULL_TREE
|| t
== error_mark_node
)
13179 loc
= input_location
;
13180 if (EXPR_HAS_LOCATION (t
))
13181 input_location
= EXPR_LOCATION (t
);
13182 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13183 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13185 switch (TREE_CODE (t
))
13187 case STATEMENT_LIST
:
13189 tree_stmt_iterator i
;
13190 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13191 RECUR (tsi_stmt (i
));
13195 case CTOR_INITIALIZER
:
13196 finish_mem_initializers (tsubst_initializer_list
13197 (TREE_OPERAND (t
, 0), args
));
13201 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13205 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13206 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13207 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13209 finish_expr_stmt (tmp
);
13213 do_using_directive (USING_STMT_NAMESPACE (t
));
13218 tree decl
, pattern_decl
;
13221 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13222 if (TREE_CODE (decl
) == LABEL_DECL
)
13223 finish_label_decl (DECL_NAME (decl
));
13224 else if (TREE_CODE (decl
) == USING_DECL
)
13226 tree scope
= USING_DECL_SCOPE (decl
);
13227 tree name
= DECL_NAME (decl
);
13230 scope
= tsubst (scope
, args
, complain
, in_decl
);
13231 decl
= lookup_qualified_name (scope
, name
,
13232 /*is_type_p=*/false,
13233 /*complain=*/false);
13234 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13235 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13237 do_local_using_decl (decl
, scope
, name
);
13239 else if (DECL_PACK_P (decl
))
13241 /* Don't build up decls for a variadic capture proxy, we'll
13242 instantiate the elements directly as needed. */
13247 init
= DECL_INITIAL (decl
);
13248 decl
= tsubst (decl
, args
, complain
, in_decl
);
13249 if (decl
!= error_mark_node
)
13251 /* By marking the declaration as instantiated, we avoid
13252 trying to instantiate it. Since instantiate_decl can't
13253 handle local variables, and since we've already done
13254 all that needs to be done, that's the right thing to
13257 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13259 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13260 /* Anonymous aggregates are a special case. */
13261 finish_anon_union (decl
);
13262 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13264 DECL_CONTEXT (decl
) = current_function_decl
;
13265 if (DECL_NAME (decl
) == this_identifier
)
13267 tree lam
= DECL_CONTEXT (current_function_decl
);
13268 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13269 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13271 insert_capture_proxy (decl
);
13273 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13274 /* We already did a pushtag. */;
13275 else if (TREE_CODE (decl
) == FUNCTION_DECL
13276 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13277 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13279 DECL_CONTEXT (decl
) = NULL_TREE
;
13281 DECL_CONTEXT (decl
) = current_function_decl
;
13282 cp_check_omp_declare_reduction (decl
);
13286 int const_init
= false;
13287 maybe_push_decl (decl
);
13289 && DECL_PRETTY_FUNCTION_P (decl
))
13291 /* For __PRETTY_FUNCTION__ we have to adjust the
13293 const char *const name
13294 = cxx_printable_name (current_function_decl
, 2);
13295 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13299 tree t
= RECUR (init
);
13303 /* If we had an initializer but it
13304 instantiated to nothing,
13305 value-initialize the object. This will
13306 only occur when the initializer was a
13307 pack expansion where the parameter packs
13308 used in that expansion were of length
13310 init
= build_value_init (TREE_TYPE (decl
),
13312 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13313 init
= get_target_expr_sfinae (init
, complain
);
13320 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13322 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13331 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13332 RECUR (FOR_INIT_STMT (t
));
13333 finish_for_init_stmt (stmt
);
13334 tmp
= RECUR (FOR_COND (t
));
13335 finish_for_cond (tmp
, stmt
, false);
13336 tmp
= RECUR (FOR_EXPR (t
));
13337 finish_for_expr (tmp
, stmt
);
13338 RECUR (FOR_BODY (t
));
13339 finish_for_stmt (stmt
);
13342 case RANGE_FOR_STMT
:
13345 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13346 decl
= RANGE_FOR_DECL (t
);
13347 decl
= tsubst (decl
, args
, complain
, in_decl
);
13348 maybe_push_decl (decl
);
13349 expr
= RECUR (RANGE_FOR_EXPR (t
));
13350 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13351 RECUR (RANGE_FOR_BODY (t
));
13352 finish_for_stmt (stmt
);
13357 stmt
= begin_while_stmt ();
13358 tmp
= RECUR (WHILE_COND (t
));
13359 finish_while_stmt_cond (tmp
, stmt
, false);
13360 RECUR (WHILE_BODY (t
));
13361 finish_while_stmt (stmt
);
13365 stmt
= begin_do_stmt ();
13366 RECUR (DO_BODY (t
));
13367 finish_do_body (stmt
);
13368 tmp
= RECUR (DO_COND (t
));
13369 finish_do_stmt (tmp
, stmt
, false);
13373 stmt
= begin_if_stmt ();
13374 tmp
= RECUR (IF_COND (t
));
13375 finish_if_stmt_cond (tmp
, stmt
);
13376 RECUR (THEN_CLAUSE (t
));
13377 finish_then_clause (stmt
);
13379 if (ELSE_CLAUSE (t
))
13381 begin_else_clause (stmt
);
13382 RECUR (ELSE_CLAUSE (t
));
13383 finish_else_clause (stmt
);
13386 finish_if_stmt (stmt
);
13390 if (BIND_EXPR_BODY_BLOCK (t
))
13391 stmt
= begin_function_body ();
13393 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13394 ? BCS_TRY_BLOCK
: 0);
13396 RECUR (BIND_EXPR_BODY (t
));
13398 if (BIND_EXPR_BODY_BLOCK (t
))
13399 finish_function_body (stmt
);
13401 finish_compound_stmt (stmt
);
13405 finish_break_stmt ();
13408 case CONTINUE_STMT
:
13409 finish_continue_stmt ();
13413 stmt
= begin_switch_stmt ();
13414 tmp
= RECUR (SWITCH_STMT_COND (t
));
13415 finish_switch_cond (tmp
, stmt
);
13416 RECUR (SWITCH_STMT_BODY (t
));
13417 finish_switch_stmt (stmt
);
13420 case CASE_LABEL_EXPR
:
13421 finish_case_label (EXPR_LOCATION (t
),
13422 RECUR (CASE_LOW (t
)),
13423 RECUR (CASE_HIGH (t
)));
13428 tree decl
= LABEL_EXPR_LABEL (t
);
13431 label
= finish_label_stmt (DECL_NAME (decl
));
13432 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13433 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13438 tmp
= GOTO_DESTINATION (t
);
13439 if (TREE_CODE (tmp
) != LABEL_DECL
)
13440 /* Computed goto's must be tsubst'd into. On the other hand,
13441 non-computed gotos must not be; the identifier in question
13442 will have no binding. */
13445 tmp
= DECL_NAME (tmp
);
13446 finish_goto_stmt (tmp
);
13450 tmp
= finish_asm_stmt
13451 (ASM_VOLATILE_P (t
),
13452 RECUR (ASM_STRING (t
)),
13453 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13454 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13455 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13456 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13458 tree asm_expr
= tmp
;
13459 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13460 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13461 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13468 stmt
= begin_try_block ();
13469 RECUR (TRY_STMTS (t
));
13470 finish_cleanup_try_block (stmt
);
13471 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13475 tree compound_stmt
= NULL_TREE
;
13477 if (FN_TRY_BLOCK_P (t
))
13478 stmt
= begin_function_try_block (&compound_stmt
);
13480 stmt
= begin_try_block ();
13482 RECUR (TRY_STMTS (t
));
13484 if (FN_TRY_BLOCK_P (t
))
13485 finish_function_try_block (stmt
);
13487 finish_try_block (stmt
);
13489 RECUR (TRY_HANDLERS (t
));
13490 if (FN_TRY_BLOCK_P (t
))
13491 finish_function_handler_sequence (stmt
, compound_stmt
);
13493 finish_handler_sequence (stmt
);
13499 tree decl
= HANDLER_PARMS (t
);
13503 decl
= tsubst (decl
, args
, complain
, in_decl
);
13504 /* Prevent instantiate_decl from trying to instantiate
13505 this variable. We've already done all that needs to be
13507 if (decl
!= error_mark_node
)
13508 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13510 stmt
= begin_handler ();
13511 finish_handler_parms (decl
, stmt
);
13512 RECUR (HANDLER_BODY (t
));
13513 finish_handler (stmt
);
13518 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13519 if (CLASS_TYPE_P (tmp
))
13521 /* Local classes are not independent templates; they are
13522 instantiated along with their containing function. And this
13523 way we don't have to deal with pushing out of one local class
13524 to instantiate a member of another local class. */
13526 /* Closures are handled by the LAMBDA_EXPR. */
13527 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13528 complete_type (tmp
);
13529 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13530 if (!DECL_ARTIFICIAL (fn
))
13531 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13535 case STATIC_ASSERT
:
13539 ++c_inhibit_evaluation_warnings
;
13541 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13544 /*integral_constant_expression_p=*/true);
13545 --c_inhibit_evaluation_warnings
;
13547 finish_static_assert (condition
,
13548 STATIC_ASSERT_MESSAGE (t
),
13549 STATIC_ASSERT_SOURCE_LOCATION (t
),
13550 /*member_p=*/false);
13555 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13556 args
, complain
, in_decl
);
13557 stmt
= begin_omp_parallel ();
13558 RECUR (OMP_PARALLEL_BODY (t
));
13559 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13560 = OMP_PARALLEL_COMBINED (t
);
13564 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13565 args
, complain
, in_decl
);
13566 stmt
= begin_omp_task ();
13567 RECUR (OMP_TASK_BODY (t
));
13568 finish_omp_task (tmp
, stmt
);
13574 case OMP_DISTRIBUTE
:
13576 tree clauses
, body
, pre_body
;
13577 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13578 tree incrv
= NULL_TREE
;
13581 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13582 args
, complain
, in_decl
);
13583 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13585 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13586 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13587 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13588 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13591 stmt
= begin_omp_structured_block ();
13593 pre_body
= push_stmt_list ();
13594 RECUR (OMP_FOR_PRE_BODY (t
));
13595 pre_body
= pop_stmt_list (pre_body
);
13597 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13598 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13599 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13600 &clauses
, args
, complain
, in_decl
,
13601 integral_constant_expression_p
);
13603 body
= push_stmt_list ();
13604 RECUR (OMP_FOR_BODY (t
));
13605 body
= pop_stmt_list (body
);
13607 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13608 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13609 condv
, incrv
, body
, pre_body
, clauses
);
13612 t
= make_node (TREE_CODE (t
));
13613 TREE_TYPE (t
) = void_type_node
;
13614 OMP_FOR_BODY (t
) = body
;
13615 OMP_FOR_PRE_BODY (t
) = pre_body
;
13616 OMP_FOR_CLAUSES (t
) = clauses
;
13617 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13621 add_stmt (finish_omp_structured_block (stmt
));
13628 case OMP_TARGET_DATA
:
13630 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13631 args
, complain
, in_decl
);
13632 stmt
= push_stmt_list ();
13633 RECUR (OMP_BODY (t
));
13634 stmt
= pop_stmt_list (stmt
);
13637 OMP_BODY (t
) = stmt
;
13638 OMP_CLAUSES (t
) = tmp
;
13642 case OMP_TARGET_UPDATE
:
13643 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13644 args
, complain
, in_decl
);
13646 OMP_CLAUSES (t
) = tmp
;
13653 case OMP_TASKGROUP
:
13655 stmt
= push_stmt_list ();
13656 RECUR (OMP_BODY (t
));
13657 stmt
= pop_stmt_list (stmt
);
13660 OMP_BODY (t
) = stmt
;
13665 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13666 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13668 tree op1
= TREE_OPERAND (t
, 1);
13669 tree rhs1
= NULL_TREE
;
13671 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13673 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13674 op1
= TREE_OPERAND (op1
, 1);
13676 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13677 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13678 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13679 NULL_TREE
, NULL_TREE
, rhs1
,
13680 OMP_ATOMIC_SEQ_CST (t
));
13684 tree op1
= TREE_OPERAND (t
, 1);
13685 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13686 tree rhs1
= NULL_TREE
;
13687 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13688 enum tree_code opcode
= NOP_EXPR
;
13689 if (code
== OMP_ATOMIC_READ
)
13691 v
= RECUR (TREE_OPERAND (op1
, 0));
13692 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13694 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13695 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13697 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13698 v
= RECUR (TREE_OPERAND (op1
, 0));
13699 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13700 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13702 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13703 op11
= TREE_OPERAND (op11
, 1);
13705 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13706 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13707 opcode
= TREE_CODE (op11
);
13708 if (opcode
== MODIFY_EXPR
)
13714 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13715 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13717 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13718 OMP_ATOMIC_SEQ_CST (t
));
13722 case TRANSACTION_EXPR
:
13725 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13726 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13728 if (TRANSACTION_EXPR_IS_STMT (t
))
13730 tree body
= TRANSACTION_EXPR_BODY (t
);
13731 tree noex
= NULL_TREE
;
13732 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13734 noex
= MUST_NOT_THROW_COND (body
);
13735 if (noex
== NULL_TREE
)
13736 noex
= boolean_true_node
;
13737 body
= TREE_OPERAND (body
, 0);
13739 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13741 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13745 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13746 RECUR (TRANSACTION_EXPR_BODY (t
)),
13753 case MUST_NOT_THROW_EXPR
:
13754 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13755 RECUR (MUST_NOT_THROW_COND (t
))));
13757 case EXPR_PACK_EXPANSION
:
13758 error ("invalid use of pack expansion expression");
13759 RETURN (error_mark_node
);
13761 case NONTYPE_ARGUMENT_PACK
:
13762 error ("use %<...%> to expand argument pack");
13763 RETURN (error_mark_node
);
13765 case COMPOUND_EXPR
:
13766 tmp
= RECUR (TREE_OPERAND (t
, 0));
13767 if (tmp
== NULL_TREE
)
13768 /* If the first operand was a statement, we're done with it. */
13769 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13770 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13771 RECUR (TREE_OPERAND (t
, 1)),
13775 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13777 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13778 /*function_p=*/false,
13779 integral_constant_expression_p
));
13782 RETURN (NULL_TREE
);
13784 input_location
= loc
;
13790 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13791 function. For description of the body see comment above
13792 cp_parser_omp_declare_reduction_exprs. */
13795 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13797 if (t
== NULL_TREE
|| t
== error_mark_node
)
13800 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13802 tree_stmt_iterator tsi
;
13805 memset (stmts
, 0, sizeof stmts
);
13806 for (i
= 0, tsi
= tsi_start (t
);
13807 i
< 7 && !tsi_end_p (tsi
);
13808 i
++, tsi_next (&tsi
))
13809 stmts
[i
] = tsi_stmt (tsi
);
13810 gcc_assert (tsi_end_p (tsi
));
13814 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13815 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13816 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13817 args
, complain
, in_decl
);
13818 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13819 args
, complain
, in_decl
);
13820 DECL_CONTEXT (omp_out
) = current_function_decl
;
13821 DECL_CONTEXT (omp_in
) = current_function_decl
;
13822 keep_next_level (true);
13823 tree block
= begin_omp_structured_block ();
13824 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13825 block
= finish_omp_structured_block (block
);
13826 block
= maybe_cleanup_point_expr_void (block
);
13827 add_decl_expr (omp_out
);
13828 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13829 TREE_NO_WARNING (omp_out
) = 1;
13830 add_decl_expr (omp_in
);
13831 finish_expr_stmt (block
);
13835 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13836 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13837 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13838 args
, complain
, in_decl
);
13839 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13840 args
, complain
, in_decl
);
13841 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13842 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13843 keep_next_level (true);
13844 tree block
= begin_omp_structured_block ();
13845 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13846 block
= finish_omp_structured_block (block
);
13847 block
= maybe_cleanup_point_expr_void (block
);
13848 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13849 add_decl_expr (omp_priv
);
13850 add_decl_expr (omp_orig
);
13851 finish_expr_stmt (block
);
13853 add_decl_expr (omp_orig
);
13857 /* T is a postfix-expression that is not being used in a function
13858 call. Return the substituted version of T. */
13861 tsubst_non_call_postfix_expression (tree t
, tree args
,
13862 tsubst_flags_t complain
,
13865 if (TREE_CODE (t
) == SCOPE_REF
)
13866 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13867 /*done=*/false, /*address_p=*/false);
13869 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13870 /*function_p=*/false,
13871 /*integral_constant_expression_p=*/false);
13876 /* Like tsubst but deals with expressions and performs semantic
13877 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13880 tsubst_copy_and_build (tree t
,
13882 tsubst_flags_t complain
,
13885 bool integral_constant_expression_p
)
13887 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13888 #define RECUR(NODE) \
13889 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13890 /*function_p=*/false, \
13891 integral_constant_expression_p)
13896 if (t
== NULL_TREE
|| t
== error_mark_node
)
13899 loc
= input_location
;
13900 if (EXPR_HAS_LOCATION (t
))
13901 input_location
= EXPR_LOCATION (t
);
13903 /* N3276 decltype magic only applies to calls at the top level or on the
13904 right side of a comma. */
13905 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13906 complain
&= ~tf_decltype
;
13908 switch (TREE_CODE (t
))
13912 /* Fall through. */
13913 case IDENTIFIER_NODE
:
13917 bool non_integral_constant_expression_p
;
13918 const char *error_msg
;
13920 if (IDENTIFIER_TYPENAME_P (t
))
13922 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13923 t
= mangle_conv_op_name_for_type (new_type
);
13926 /* Look up the name. */
13927 decl
= lookup_name (t
);
13929 /* By convention, expressions use ERROR_MARK_NODE to indicate
13930 failure, not NULL_TREE. */
13931 if (decl
== NULL_TREE
)
13932 decl
= error_mark_node
;
13934 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13936 integral_constant_expression_p
,
13937 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13938 &non_integral_constant_expression_p
,
13939 /*template_p=*/false,
13941 /*address_p=*/false,
13942 /*template_arg_p=*/false,
13947 if (!function_p
&& identifier_p (decl
))
13949 if (complain
& tf_error
)
13950 unqualified_name_lookup_error (decl
);
13951 decl
= error_mark_node
;
13956 case TEMPLATE_ID_EXPR
:
13959 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13960 tree targs
= TREE_OPERAND (t
, 1);
13963 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13965 if (TREE_CODE (templ
) == COMPONENT_REF
)
13967 object
= TREE_OPERAND (templ
, 0);
13968 templ
= TREE_OPERAND (templ
, 1);
13971 object
= NULL_TREE
;
13972 templ
= lookup_template_function (templ
, targs
);
13975 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13976 object
, templ
, NULL_TREE
));
13978 RETURN (baselink_for_fns (templ
));
13983 tree r
= RECUR (TREE_OPERAND (t
, 0));
13985 if (REFERENCE_REF_P (t
))
13987 /* A type conversion to reference type will be enclosed in
13988 such an indirect ref, but the substitution of the cast
13989 will have also added such an indirect ref. */
13990 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13991 r
= convert_from_reference (r
);
13994 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
13995 complain
|decltype_flag
);
14001 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14002 RECUR (TREE_OPERAND (t
, 0))));
14004 case IMPLICIT_CONV_EXPR
:
14006 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14007 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14008 int flags
= LOOKUP_IMPLICIT
;
14009 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14010 flags
= LOOKUP_NORMAL
;
14011 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14018 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14019 RECUR (TREE_OPERAND (t
, 0))));
14022 case REINTERPRET_CAST_EXPR
:
14023 case CONST_CAST_EXPR
:
14024 case DYNAMIC_CAST_EXPR
:
14025 case STATIC_CAST_EXPR
:
14028 tree op
, r
= NULL_TREE
;
14030 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14031 if (integral_constant_expression_p
14032 && !cast_valid_in_integral_constant_expression_p (type
))
14034 if (complain
& tf_error
)
14035 error ("a cast to a type other than an integral or "
14036 "enumeration type cannot appear in a constant-expression");
14037 RETURN (error_mark_node
);
14040 op
= RECUR (TREE_OPERAND (t
, 0));
14042 ++c_inhibit_evaluation_warnings
;
14043 switch (TREE_CODE (t
))
14046 r
= build_functional_cast (type
, op
, complain
);
14048 case REINTERPRET_CAST_EXPR
:
14049 r
= build_reinterpret_cast (type
, op
, complain
);
14051 case CONST_CAST_EXPR
:
14052 r
= build_const_cast (type
, op
, complain
);
14054 case DYNAMIC_CAST_EXPR
:
14055 r
= build_dynamic_cast (type
, op
, complain
);
14057 case STATIC_CAST_EXPR
:
14058 r
= build_static_cast (type
, op
, complain
);
14061 gcc_unreachable ();
14063 --c_inhibit_evaluation_warnings
;
14068 case POSTDECREMENT_EXPR
:
14069 case POSTINCREMENT_EXPR
:
14070 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14071 args
, complain
, in_decl
);
14072 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14073 complain
|decltype_flag
));
14075 case PREDECREMENT_EXPR
:
14076 case PREINCREMENT_EXPR
:
14080 case TRUTH_NOT_EXPR
:
14081 case UNARY_PLUS_EXPR
: /* Unary + */
14082 case REALPART_EXPR
:
14083 case IMAGPART_EXPR
:
14084 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14085 RECUR (TREE_OPERAND (t
, 0)),
14086 complain
|decltype_flag
));
14088 case FIX_TRUNC_EXPR
:
14089 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14093 op1
= TREE_OPERAND (t
, 0);
14094 if (TREE_CODE (op1
) == LABEL_DECL
)
14095 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14096 EXPR_LOCATION (op1
)));
14097 if (TREE_CODE (op1
) == SCOPE_REF
)
14098 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14099 /*done=*/true, /*address_p=*/true);
14101 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14103 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14104 complain
|decltype_flag
));
14109 case TRUNC_DIV_EXPR
:
14110 case CEIL_DIV_EXPR
:
14111 case FLOOR_DIV_EXPR
:
14112 case ROUND_DIV_EXPR
:
14113 case EXACT_DIV_EXPR
:
14117 case TRUNC_MOD_EXPR
:
14118 case FLOOR_MOD_EXPR
:
14119 case TRUTH_ANDIF_EXPR
:
14120 case TRUTH_ORIF_EXPR
:
14121 case TRUTH_AND_EXPR
:
14122 case TRUTH_OR_EXPR
:
14140 ++c_inhibit_evaluation_warnings
;
14142 r
= build_x_binary_op
14143 (input_location
, TREE_CODE (t
),
14144 RECUR (TREE_OPERAND (t
, 0)),
14145 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14147 : TREE_CODE (TREE_OPERAND (t
, 0))),
14148 RECUR (TREE_OPERAND (t
, 1)),
14149 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14151 : TREE_CODE (TREE_OPERAND (t
, 1))),
14153 complain
|decltype_flag
);
14154 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14155 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14157 --c_inhibit_evaluation_warnings
;
14163 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14164 /*address_p=*/false));
14166 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14167 args
, complain
, in_decl
);
14168 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14169 RECUR (TREE_OPERAND (t
, 1)),
14170 complain
|decltype_flag
));
14172 case ARRAY_NOTATION_REF
:
14174 tree start_index
, length
, stride
;
14175 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14176 args
, complain
, in_decl
);
14177 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14178 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14179 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14180 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14181 length
, stride
, TREE_TYPE (op1
)));
14184 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14185 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14192 op1
= TREE_OPERAND (t
, 0);
14193 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14194 op1
= TREE_TYPE (op1
);
14197 /* When there are no ARGS, we are trying to evaluate a
14198 non-dependent expression from the parser. Trying to do
14199 the substitutions may not work. */
14201 op1
= TREE_TYPE (op1
);
14205 ++cp_unevaluated_operand
;
14206 ++c_inhibit_evaluation_warnings
;
14208 op1
= tsubst (op1
, args
, complain
, in_decl
);
14210 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14211 /*function_p=*/false,
14212 /*integral_constant_expression_p=*/
14214 --cp_unevaluated_operand
;
14215 --c_inhibit_evaluation_warnings
;
14218 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14219 complain
& tf_error
);
14221 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14222 complain
& tf_error
);
14223 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14225 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14227 if (!processing_template_decl
&& TYPE_P (op1
))
14229 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14230 build1 (NOP_EXPR
, op1
, error_mark_node
));
14231 SIZEOF_EXPR_TYPE_P (r
) = 1;
14234 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14235 TREE_SIDE_EFFECTS (r
) = 0;
14236 TREE_READONLY (r
) = 1;
14238 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14243 case AT_ENCODE_EXPR
:
14245 op1
= TREE_OPERAND (t
, 0);
14246 ++cp_unevaluated_operand
;
14247 ++c_inhibit_evaluation_warnings
;
14248 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14249 /*function_p=*/false,
14250 /*integral_constant_expression_p=*/false);
14251 --cp_unevaluated_operand
;
14252 --c_inhibit_evaluation_warnings
;
14253 RETURN (objc_build_encode_expr (op1
));
14256 case NOEXCEPT_EXPR
:
14257 op1
= TREE_OPERAND (t
, 0);
14258 ++cp_unevaluated_operand
;
14259 ++c_inhibit_evaluation_warnings
;
14260 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14261 /*function_p=*/false,
14262 /*integral_constant_expression_p=*/false);
14263 --cp_unevaluated_operand
;
14264 --c_inhibit_evaluation_warnings
;
14265 RETURN (finish_noexcept_expr (op1
, complain
));
14271 ++c_inhibit_evaluation_warnings
;
14273 r
= build_x_modify_expr
14274 (EXPR_LOCATION (t
),
14275 RECUR (TREE_OPERAND (t
, 0)),
14276 TREE_CODE (TREE_OPERAND (t
, 1)),
14277 RECUR (TREE_OPERAND (t
, 2)),
14278 complain
|decltype_flag
);
14279 /* TREE_NO_WARNING must be set if either the expression was
14280 parenthesized or it uses an operator such as >>= rather
14281 than plain assignment. In the former case, it was already
14282 set and must be copied. In the latter case,
14283 build_x_modify_expr sets it and it must not be reset
14285 if (TREE_NO_WARNING (t
))
14286 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14288 --c_inhibit_evaluation_warnings
;
14294 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14295 args
, complain
, in_decl
);
14296 /* Remember that there was a reference to this entity. */
14299 RETURN (build_x_arrow (input_location
, op1
, complain
));
14303 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14304 tree init
= RECUR (TREE_OPERAND (t
, 3));
14305 vec
<tree
, va_gc
> *placement_vec
;
14306 vec
<tree
, va_gc
> *init_vec
;
14309 if (placement
== NULL_TREE
)
14310 placement_vec
= NULL
;
14313 placement_vec
= make_tree_vector ();
14314 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14315 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14318 /* If there was an initializer in the original tree, but it
14319 instantiated to an empty list, then we should pass a
14320 non-NULL empty vector to tell build_new that it was an
14321 empty initializer() rather than no initializer. This can
14322 only happen when the initializer is a pack expansion whose
14323 parameter packs are of length zero. */
14324 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14328 init_vec
= make_tree_vector ();
14329 if (init
== void_zero_node
)
14330 gcc_assert (init_vec
!= NULL
);
14333 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14334 vec_safe_push (init_vec
, TREE_VALUE (init
));
14338 ret
= build_new (&placement_vec
,
14339 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14340 RECUR (TREE_OPERAND (t
, 2)),
14342 NEW_EXPR_USE_GLOBAL (t
),
14345 if (placement_vec
!= NULL
)
14346 release_tree_vector (placement_vec
);
14347 if (init_vec
!= NULL
)
14348 release_tree_vector (init_vec
);
14354 RETURN (delete_sanity
14355 (RECUR (TREE_OPERAND (t
, 0)),
14356 RECUR (TREE_OPERAND (t
, 1)),
14357 DELETE_EXPR_USE_VEC (t
),
14358 DELETE_EXPR_USE_GLOBAL (t
),
14361 case COMPOUND_EXPR
:
14363 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14364 complain
& ~tf_decltype
, in_decl
,
14365 /*function_p=*/false,
14366 integral_constant_expression_p
);
14367 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14369 RECUR (TREE_OPERAND (t
, 1)),
14370 complain
|decltype_flag
));
14376 vec
<tree
, va_gc
> *call_args
;
14377 unsigned int nargs
, i
;
14382 function
= CALL_EXPR_FN (t
);
14383 /* When we parsed the expression, we determined whether or
14384 not Koenig lookup should be performed. */
14385 koenig_p
= KOENIG_LOOKUP_P (t
);
14386 if (TREE_CODE (function
) == SCOPE_REF
)
14388 qualified_p
= true;
14389 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14391 /*address_p=*/false);
14393 else if (koenig_p
&& identifier_p (function
))
14395 /* Do nothing; calling tsubst_copy_and_build on an identifier
14396 would incorrectly perform unqualified lookup again.
14398 Note that we can also have an IDENTIFIER_NODE if the earlier
14399 unqualified lookup found a member function; in that case
14400 koenig_p will be false and we do want to do the lookup
14401 again to find the instantiated member function.
14403 FIXME but doing that causes c++/15272, so we need to stop
14404 using IDENTIFIER_NODE in that situation. */
14405 qualified_p
= false;
14409 if (TREE_CODE (function
) == COMPONENT_REF
)
14411 tree op
= TREE_OPERAND (function
, 1);
14413 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14414 || (BASELINK_P (op
)
14415 && BASELINK_QUALIFIED_P (op
)));
14418 qualified_p
= false;
14420 if (TREE_CODE (function
) == ADDR_EXPR
14421 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14422 /* Avoid error about taking the address of a constructor. */
14423 function
= TREE_OPERAND (function
, 0);
14425 function
= tsubst_copy_and_build (function
, args
, complain
,
14428 integral_constant_expression_p
);
14430 if (BASELINK_P (function
))
14431 qualified_p
= true;
14434 nargs
= call_expr_nargs (t
);
14435 call_args
= make_tree_vector ();
14436 for (i
= 0; i
< nargs
; ++i
)
14438 tree arg
= CALL_EXPR_ARG (t
, i
);
14440 if (!PACK_EXPANSION_P (arg
))
14441 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14444 /* Expand the pack expansion and push each entry onto
14446 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14447 if (TREE_CODE (arg
) == TREE_VEC
)
14449 unsigned int len
, j
;
14451 len
= TREE_VEC_LENGTH (arg
);
14452 for (j
= 0; j
< len
; ++j
)
14454 tree value
= TREE_VEC_ELT (arg
, j
);
14455 if (value
!= NULL_TREE
)
14456 value
= convert_from_reference (value
);
14457 vec_safe_push (call_args
, value
);
14462 /* A partial substitution. Add one entry. */
14463 vec_safe_push (call_args
, arg
);
14468 /* We do not perform argument-dependent lookup if normal
14469 lookup finds a non-function, in accordance with the
14470 expected resolution of DR 218. */
14472 && ((is_overloaded_fn (function
)
14473 /* If lookup found a member function, the Koenig lookup is
14474 not appropriate, even if an unqualified-name was used
14475 to denote the function. */
14476 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14477 || identifier_p (function
))
14478 /* Only do this when substitution turns a dependent call
14479 into a non-dependent call. */
14480 && type_dependent_expression_p_push (t
)
14481 && !any_type_dependent_arguments_p (call_args
))
14482 function
= perform_koenig_lookup (function
, call_args
, false,
14485 if (identifier_p (function
)
14486 && !any_type_dependent_arguments_p (call_args
))
14488 if (koenig_p
&& (complain
& tf_warning_or_error
))
14490 /* For backwards compatibility and good diagnostics, try
14491 the unqualified lookup again if we aren't in SFINAE
14493 tree unq
= (tsubst_copy_and_build
14494 (function
, args
, complain
, in_decl
, true,
14495 integral_constant_expression_p
));
14496 if (unq
== error_mark_node
)
14497 RETURN (error_mark_node
);
14499 if (unq
!= function
)
14502 if (INDIRECT_REF_P (fn
))
14503 fn
= TREE_OPERAND (fn
, 0);
14504 if (TREE_CODE (fn
) == COMPONENT_REF
)
14505 fn
= TREE_OPERAND (fn
, 1);
14506 if (is_overloaded_fn (fn
))
14507 fn
= get_first_fn (fn
);
14508 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
14509 "%qD was not declared in this scope, "
14510 "and no declarations were found by "
14511 "argument-dependent lookup at the point "
14512 "of instantiation", function
))
14515 /* Can't say anything more. */;
14516 else if (DECL_CLASS_SCOPE_P (fn
))
14518 location_t loc
= EXPR_LOC_OR_LOC (t
,
14521 "declarations in dependent base %qT are "
14522 "not found by unqualified lookup",
14523 DECL_CLASS_CONTEXT (fn
));
14524 if (current_class_ptr
)
14526 "use %<this->%D%> instead", function
);
14529 "use %<%T::%D%> instead",
14530 current_class_name
, function
);
14533 inform (0, "%q+D declared here, later in the "
14534 "translation unit", fn
);
14539 if (identifier_p (function
))
14541 if (complain
& tf_error
)
14542 unqualified_name_lookup_error (function
);
14543 release_tree_vector (call_args
);
14544 RETURN (error_mark_node
);
14548 /* Remember that there was a reference to this entity. */
14549 if (DECL_P (function
))
14550 mark_used (function
);
14552 /* Put back tf_decltype for the actual call. */
14553 complain
|= decltype_flag
;
14555 if (TREE_CODE (function
) == OFFSET_REF
)
14556 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14558 else if (TREE_CODE (function
) == COMPONENT_REF
)
14560 tree instance
= TREE_OPERAND (function
, 0);
14561 tree fn
= TREE_OPERAND (function
, 1);
14563 if (processing_template_decl
14564 && (type_dependent_expression_p (instance
)
14565 || (!BASELINK_P (fn
)
14566 && TREE_CODE (fn
) != FIELD_DECL
)
14567 || type_dependent_expression_p (fn
)
14568 || any_type_dependent_arguments_p (call_args
)))
14569 ret
= build_nt_call_vec (function
, call_args
);
14570 else if (!BASELINK_P (fn
))
14571 ret
= finish_call_expr (function
, &call_args
,
14572 /*disallow_virtual=*/false,
14573 /*koenig_p=*/false,
14576 ret
= (build_new_method_call
14578 &call_args
, NULL_TREE
,
14579 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14584 ret
= finish_call_expr (function
, &call_args
,
14585 /*disallow_virtual=*/qualified_p
,
14589 release_tree_vector (call_args
);
14596 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14599 if (TREE_CODE (cond
) == INTEGER_CST
)
14601 if (integer_zerop (cond
))
14603 ++c_inhibit_evaluation_warnings
;
14604 exp1
= RECUR (TREE_OPERAND (t
, 1));
14605 --c_inhibit_evaluation_warnings
;
14606 exp2
= RECUR (TREE_OPERAND (t
, 2));
14610 exp1
= RECUR (TREE_OPERAND (t
, 1));
14611 ++c_inhibit_evaluation_warnings
;
14612 exp2
= RECUR (TREE_OPERAND (t
, 2));
14613 --c_inhibit_evaluation_warnings
;
14618 exp1
= RECUR (TREE_OPERAND (t
, 1));
14619 exp2
= RECUR (TREE_OPERAND (t
, 2));
14622 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14623 cond
, exp1
, exp2
, complain
));
14626 case PSEUDO_DTOR_EXPR
:
14627 RETURN (finish_pseudo_destructor_expr
14628 (RECUR (TREE_OPERAND (t
, 0)),
14629 RECUR (TREE_OPERAND (t
, 1)),
14630 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14635 tree purpose
, value
, chain
;
14637 if (t
== void_list_node
)
14640 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14641 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14643 /* We have pack expansions, so expand those and
14644 create a new list out of it. */
14645 tree purposevec
= NULL_TREE
;
14646 tree valuevec
= NULL_TREE
;
14650 /* Expand the argument expressions. */
14651 if (TREE_PURPOSE (t
))
14652 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14653 complain
, in_decl
);
14654 if (TREE_VALUE (t
))
14655 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14656 complain
, in_decl
);
14658 /* Build the rest of the list. */
14659 chain
= TREE_CHAIN (t
);
14660 if (chain
&& chain
!= void_type_node
)
14661 chain
= RECUR (chain
);
14663 /* Determine the number of arguments. */
14664 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14666 len
= TREE_VEC_LENGTH (purposevec
);
14667 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14669 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14670 len
= TREE_VEC_LENGTH (valuevec
);
14673 /* Since we only performed a partial substitution into
14674 the argument pack, we only RETURN (a single list
14676 if (purposevec
== TREE_PURPOSE (t
)
14677 && valuevec
== TREE_VALUE (t
)
14678 && chain
== TREE_CHAIN (t
))
14681 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14684 /* Convert the argument vectors into a TREE_LIST */
14688 /* Grab the Ith values. */
14690 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14693 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14696 /* Build the list (backwards). */
14697 chain
= tree_cons (purpose
, value
, chain
);
14703 purpose
= TREE_PURPOSE (t
);
14705 purpose
= RECUR (purpose
);
14706 value
= TREE_VALUE (t
);
14708 value
= RECUR (value
);
14709 chain
= TREE_CHAIN (t
);
14710 if (chain
&& chain
!= void_type_node
)
14711 chain
= RECUR (chain
);
14712 if (purpose
== TREE_PURPOSE (t
)
14713 && value
== TREE_VALUE (t
)
14714 && chain
== TREE_CHAIN (t
))
14716 RETURN (tree_cons (purpose
, value
, chain
));
14719 case COMPONENT_REF
:
14725 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14726 args
, complain
, in_decl
);
14727 /* Remember that there was a reference to this entity. */
14728 if (DECL_P (object
))
14729 mark_used (object
);
14730 object_type
= TREE_TYPE (object
);
14732 member
= TREE_OPERAND (t
, 1);
14733 if (BASELINK_P (member
))
14734 member
= tsubst_baselink (member
,
14735 non_reference (TREE_TYPE (object
)),
14736 args
, complain
, in_decl
);
14738 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14739 if (member
== error_mark_node
)
14740 RETURN (error_mark_node
);
14742 if (type_dependent_expression_p (object
))
14743 /* We can't do much here. */;
14744 else if (!CLASS_TYPE_P (object_type
))
14746 if (scalarish_type_p (object_type
))
14748 tree s
= NULL_TREE
;
14749 tree dtor
= member
;
14751 if (TREE_CODE (dtor
) == SCOPE_REF
)
14753 s
= TREE_OPERAND (dtor
, 0);
14754 dtor
= TREE_OPERAND (dtor
, 1);
14756 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14758 dtor
= TREE_OPERAND (dtor
, 0);
14760 RETURN (finish_pseudo_destructor_expr
14761 (object
, s
, dtor
, input_location
));
14765 else if (TREE_CODE (member
) == SCOPE_REF
14766 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14768 /* Lookup the template functions now that we know what the
14770 tree scope
= TREE_OPERAND (member
, 0);
14771 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14772 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14773 member
= lookup_qualified_name (scope
, tmpl
,
14774 /*is_type_p=*/false,
14775 /*complain=*/false);
14776 if (BASELINK_P (member
))
14778 BASELINK_FUNCTIONS (member
)
14779 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14781 member
= (adjust_result_of_qualified_name_lookup
14782 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14787 qualified_name_lookup_error (scope
, tmpl
, member
,
14789 RETURN (error_mark_node
);
14792 else if (TREE_CODE (member
) == SCOPE_REF
14793 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14794 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14796 if (complain
& tf_error
)
14798 if (TYPE_P (TREE_OPERAND (member
, 0)))
14799 error ("%qT is not a class or namespace",
14800 TREE_OPERAND (member
, 0));
14802 error ("%qD is not a class or namespace",
14803 TREE_OPERAND (member
, 0));
14805 RETURN (error_mark_node
);
14807 else if (TREE_CODE (member
) == FIELD_DECL
)
14808 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14810 RETURN (finish_class_member_access_expr (object
, member
,
14811 /*template_p=*/false,
14816 RETURN (build_throw
14817 (RECUR (TREE_OPERAND (t
, 0))));
14821 vec
<constructor_elt
, va_gc
> *n
;
14822 constructor_elt
*ce
;
14823 unsigned HOST_WIDE_INT idx
;
14824 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14825 bool process_index_p
;
14827 bool need_copy_p
= false;
14830 if (type
== error_mark_node
)
14831 RETURN (error_mark_node
);
14833 /* digest_init will do the wrong thing if we let it. */
14834 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14837 /* We do not want to process the index of aggregate
14838 initializers as they are identifier nodes which will be
14839 looked up by digest_init. */
14840 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14842 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14843 newlen
= vec_safe_length (n
);
14844 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14846 if (ce
->index
&& process_index_p
14847 /* An identifier index is looked up in the type
14848 being initialized, not the current scope. */
14849 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14850 ce
->index
= RECUR (ce
->index
);
14852 if (PACK_EXPANSION_P (ce
->value
))
14854 /* Substitute into the pack expansion. */
14855 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14858 if (ce
->value
== error_mark_node
14859 || PACK_EXPANSION_P (ce
->value
))
14861 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14862 /* Just move the argument into place. */
14863 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14866 /* Update the length of the final CONSTRUCTOR
14867 arguments vector, and note that we will need to
14869 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14870 need_copy_p
= true;
14874 ce
->value
= RECUR (ce
->value
);
14879 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14881 vec_alloc (n
, newlen
);
14882 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14884 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14886 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14887 for (i
= 0; i
< len
; ++i
)
14888 CONSTRUCTOR_APPEND_ELT (n
, 0,
14889 TREE_VEC_ELT (ce
->value
, i
));
14892 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14896 r
= build_constructor (init_list_type_node
, n
);
14897 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14899 if (TREE_HAS_CONSTRUCTOR (t
))
14900 RETURN (finish_compound_literal (type
, r
, complain
));
14902 TREE_TYPE (r
) = type
;
14908 tree operand_0
= TREE_OPERAND (t
, 0);
14909 if (TYPE_P (operand_0
))
14911 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14912 RETURN (get_typeid (operand_0
, complain
));
14916 operand_0
= RECUR (operand_0
);
14917 RETURN (build_typeid (operand_0
, complain
));
14924 else if (DECL_PACK_P (t
))
14926 /* We don't build decls for an instantiation of a
14927 variadic capture proxy, we instantiate the elements
14929 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14930 return RECUR (DECL_VALUE_EXPR (t
));
14936 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14938 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14939 /* If the original type was a reference, we'll be wrapped in
14940 the appropriate INDIRECT_REF. */
14941 r
= convert_from_reference (r
);
14946 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14947 RECUR (TREE_OPERAND (t
, 0)),
14948 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14950 case OFFSETOF_EXPR
:
14951 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14955 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14956 complain
, in_decl
);
14958 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14960 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14962 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14967 tree old_stmt_expr
= cur_stmt_expr
;
14968 tree stmt_expr
= begin_stmt_expr ();
14970 cur_stmt_expr
= stmt_expr
;
14971 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14972 integral_constant_expression_p
);
14973 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14974 cur_stmt_expr
= old_stmt_expr
;
14976 /* If the resulting list of expression statement is empty,
14977 fold it further into void_zero_node. */
14978 if (empty_expr_stmt_p (stmt_expr
))
14979 stmt_expr
= void_zero_node
;
14981 RETURN (stmt_expr
);
14986 tree r
= build_lambda_expr ();
14988 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14989 LAMBDA_EXPR_CLOSURE (r
) = type
;
14990 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14992 LAMBDA_EXPR_LOCATION (r
)
14993 = LAMBDA_EXPR_LOCATION (t
);
14994 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14995 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14996 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14997 LAMBDA_EXPR_DISCRIMINATOR (r
)
14998 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14999 /* For a function scope, we want to use tsubst so that we don't
15000 complain about referring to an auto function before its return
15001 type has been deduced. Otherwise, we want to use tsubst_copy so
15002 that we look up the existing field/parameter/variable rather
15003 than build a new one. */
15004 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15005 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15006 scope
= tsubst (scope
, args
, complain
, in_decl
);
15007 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15009 /* Look up the parameter we want directly, as tsubst_copy
15010 doesn't do what we need. */
15011 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15012 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15013 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15014 parm
= DECL_CHAIN (parm
);
15016 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15017 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15018 DECL_CONTEXT (scope
) = fn
;
15021 scope
= RECUR (scope
);
15022 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15023 LAMBDA_EXPR_RETURN_TYPE (r
)
15024 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15026 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15027 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15029 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15030 determine_visibility (TYPE_NAME (type
));
15031 /* Now that we know visibility, instantiate the type so we have a
15032 declaration of the op() for later calls to lambda_function. */
15033 complete_type (type
);
15035 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15037 RETURN (build_lambda_object (r
));
15041 /* We can get here for a constant initializer of non-dependent type.
15042 FIXME stop folding in cp_parser_initializer_clause. */
15044 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15049 case TRANSACTION_EXPR
:
15050 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15051 integral_constant_expression_p
));
15054 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15056 case VEC_PERM_EXPR
:
15057 RETURN (build_x_vec_perm_expr (input_location
,
15058 RECUR (TREE_OPERAND (t
, 0)),
15059 RECUR (TREE_OPERAND (t
, 1)),
15060 RECUR (TREE_OPERAND (t
, 2)),
15064 /* Handle Objective-C++ constructs, if appropriate. */
15067 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15068 in_decl
, /*function_p=*/false);
15072 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15078 input_location
= loc
;
15082 /* Verify that the instantiated ARGS are valid. For type arguments,
15083 make sure that the type's linkage is ok. For non-type arguments,
15084 make sure they are constants if they are integral or enumerations.
15085 Emit an error under control of COMPLAIN, and return TRUE on error. */
15088 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15090 if (dependent_template_arg_p (t
))
15092 if (ARGUMENT_PACK_P (t
))
15094 tree vec
= ARGUMENT_PACK_ARGS (t
);
15095 int len
= TREE_VEC_LENGTH (vec
);
15096 bool result
= false;
15099 for (i
= 0; i
< len
; ++i
)
15100 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15104 else if (TYPE_P (t
))
15106 /* [basic.link]: A name with no linkage (notably, the name
15107 of a class or enumeration declared in a local scope)
15108 shall not be used to declare an entity with linkage.
15109 This implies that names with no linkage cannot be used as
15112 DR 757 relaxes this restriction for C++0x. */
15113 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15114 : no_linkage_check (t
, /*relaxed_p=*/false));
15118 /* DR 488 makes use of a type with no linkage cause
15119 type deduction to fail. */
15120 if (complain
& tf_error
)
15122 if (TYPE_ANONYMOUS_P (nt
))
15123 error ("%qT is/uses anonymous type", t
);
15125 error ("template argument for %qD uses local type %qT",
15130 /* In order to avoid all sorts of complications, we do not
15131 allow variably-modified types as template arguments. */
15132 else if (variably_modified_type_p (t
, NULL_TREE
))
15134 if (complain
& tf_error
)
15135 error ("%qT is a variably modified type", t
);
15139 /* Class template and alias template arguments should be OK. */
15140 else if (DECL_TYPE_TEMPLATE_P (t
))
15142 /* A non-type argument of integral or enumerated type must be a
15144 else if (TREE_TYPE (t
)
15145 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15146 && !TREE_CONSTANT (t
))
15148 if (complain
& tf_error
)
15149 error ("integral expression %qE is not constant", t
);
15156 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15158 int ix
, len
= DECL_NTPARMS (tmpl
);
15159 bool result
= false;
15161 for (ix
= 0; ix
!= len
; ix
++)
15163 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15166 if (result
&& (complain
& tf_error
))
15167 error (" trying to instantiate %qD", tmpl
);
15171 /* We're out of SFINAE context now, so generate diagnostics for the access
15172 errors we saw earlier when instantiating D from TMPL and ARGS. */
15175 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15177 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15178 tree type
= TREE_TYPE (pattern
);
15179 location_t loc
= input_location
;
15181 push_access_scope (d
);
15182 push_deferring_access_checks (dk_no_deferred
);
15183 input_location
= DECL_SOURCE_LOCATION (pattern
);
15184 tsubst (type
, args
, tf_warning_or_error
, d
);
15185 input_location
= loc
;
15186 pop_deferring_access_checks ();
15187 pop_access_scope (d
);
15190 /* Instantiate the indicated variable, function, or alias template TMPL with
15191 the template arguments in TARG_PTR. */
15194 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15196 tree targ_ptr
= orig_args
;
15200 bool access_ok
= true;
15202 if (tmpl
== error_mark_node
)
15203 return error_mark_node
;
15205 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15207 /* If this function is a clone, handle it specially. */
15208 if (DECL_CLONED_FUNCTION_P (tmpl
))
15213 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15214 DECL_CLONED_FUNCTION. */
15215 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15216 targ_ptr
, complain
);
15217 if (spec
== error_mark_node
)
15218 return error_mark_node
;
15220 /* Look for the clone. */
15221 FOR_EACH_CLONE (clone
, spec
)
15222 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15224 /* We should always have found the clone by now. */
15225 gcc_unreachable ();
15229 /* Check to see if we already have this specialization. */
15230 gen_tmpl
= most_general_template (tmpl
);
15231 if (tmpl
!= gen_tmpl
)
15232 /* The TMPL is a partial instantiation. To get a full set of
15233 arguments we must add the arguments used to perform the
15234 partial instantiation. */
15235 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15238 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15239 but it doesn't seem to be on the hot path. */
15240 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15242 gcc_assert (tmpl
== gen_tmpl
15243 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15245 || fndecl
== NULL_TREE
);
15247 if (spec
!= NULL_TREE
)
15249 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15251 if (complain
& tf_error
)
15252 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15253 return error_mark_node
;
15258 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15260 return error_mark_node
;
15262 /* We are building a FUNCTION_DECL, during which the access of its
15263 parameters and return types have to be checked. However this
15264 FUNCTION_DECL which is the desired context for access checking
15265 is not built yet. We solve this chicken-and-egg problem by
15266 deferring all checks until we have the FUNCTION_DECL. */
15267 push_deferring_access_checks (dk_deferred
);
15269 /* Instantiation of the function happens in the context of the function
15270 template, not the context of the overload resolution we're doing. */
15271 push_to_top_level ();
15272 /* If there are dependent arguments, e.g. because we're doing partial
15273 ordering, make sure processing_template_decl stays set. */
15274 if (uses_template_parms (targ_ptr
))
15275 ++processing_template_decl
;
15276 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15278 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15279 complain
, gen_tmpl
);
15280 push_nested_class (ctx
);
15282 /* Substitute template parameters to obtain the specialization. */
15283 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15284 targ_ptr
, complain
, gen_tmpl
);
15285 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15286 pop_nested_class ();
15287 pop_from_top_level ();
15289 if (fndecl
== error_mark_node
)
15291 pop_deferring_access_checks ();
15292 return error_mark_node
;
15295 /* The DECL_TI_TEMPLATE should always be the immediate parent
15296 template, not the most general template. */
15297 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15299 /* Now we know the specialization, compute access previously
15301 push_access_scope (fndecl
);
15302 if (!perform_deferred_access_checks (complain
))
15304 pop_access_scope (fndecl
);
15305 pop_deferring_access_checks ();
15307 /* If we've just instantiated the main entry point for a function,
15308 instantiate all the alternate entry points as well. We do this
15309 by cloning the instantiation of the main entry point, not by
15310 instantiating the template clones. */
15311 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15312 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15316 if (!(complain
& tf_error
))
15318 /* Remember to reinstantiate when we're out of SFINAE so the user
15319 can see the errors. */
15320 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15322 return error_mark_node
;
15327 /* Wrapper for instantiate_template_1. */
15330 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15333 timevar_push (TV_TEMPLATE_INST
);
15334 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15335 timevar_pop (TV_TEMPLATE_INST
);
15339 /* Instantiate the alias template TMPL with ARGS. Also push a template
15340 instantiation level, which instantiate_template doesn't do because
15341 functions and variables have sufficient context established by the
15345 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15347 struct pending_template
*old_last_pend
= last_pending_template
;
15348 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15349 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15350 return error_mark_node
;
15351 tree tinst
= build_tree_list (tmpl
, args
);
15352 if (!push_tinst_level (tinst
))
15355 return error_mark_node
;
15359 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15360 args
, tmpl
, complain
,
15361 /*require_all_args=*/true,
15362 /*use_default_args=*/true);
15364 tree r
= instantiate_template (tmpl
, args
, complain
);
15365 pop_tinst_level ();
15366 /* We can't free this if a pending_template entry or last_error_tinst_level
15367 is pointing at it. */
15368 if (last_pending_template
== old_last_pend
15369 && last_error_tinst_level
== old_error_tinst
)
15375 /* PARM is a template parameter pack for FN. Returns true iff
15376 PARM is used in a deducible way in the argument list of FN. */
15379 pack_deducible_p (tree parm
, tree fn
)
15381 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15382 for (; t
; t
= TREE_CHAIN (t
))
15384 tree type
= TREE_VALUE (t
);
15386 if (!PACK_EXPANSION_P (type
))
15388 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15389 packs
; packs
= TREE_CHAIN (packs
))
15390 if (TREE_VALUE (packs
) == parm
)
15392 /* The template parameter pack is used in a function parameter
15393 pack. If this is the end of the parameter list, the
15394 template parameter pack is deducible. */
15395 if (TREE_CHAIN (t
) == void_list_node
)
15398 /* Otherwise, not. Well, it could be deduced from
15399 a non-pack parameter, but doing so would end up with
15400 a deduction mismatch, so don't bother. */
15404 /* The template parameter pack isn't used in any function parameter
15405 packs, but it might be used deeper, e.g. tuple<Args...>. */
15409 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15410 NARGS elements of the arguments that are being used when calling
15411 it. TARGS is a vector into which the deduced template arguments
15414 Return zero for success, 2 for an incomplete match that doesn't resolve
15415 all the types, and 1 for complete failure. An error message will be
15416 printed only for an incomplete match.
15418 If FN is a conversion operator, or we are trying to produce a specific
15419 specialization, RETURN_TYPE is the return type desired.
15421 The EXPLICIT_TARGS are explicit template arguments provided via a
15424 The parameter STRICT is one of:
15427 We are deducing arguments for a function call, as in
15428 [temp.deduct.call].
15431 We are deducing arguments for a conversion function, as in
15432 [temp.deduct.conv].
15435 We are deducing arguments when doing an explicit instantiation
15436 as in [temp.explicit], when determining an explicit specialization
15437 as in [temp.expl.spec], or when taking the address of a function
15438 template, as in [temp.deduct.funcaddr]. */
15441 fn_type_unification (tree fn
,
15442 tree explicit_targs
,
15445 unsigned int nargs
,
15447 unification_kind_t strict
,
15454 tree decl
= NULL_TREE
;
15455 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15457 static int deduction_depth
;
15458 struct pending_template
*old_last_pend
= last_pending_template
;
15459 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15460 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15462 tree r
= error_mark_node
;
15465 complain
|= tf_decltype
;
15467 /* In C++0x, it's possible to have a function template whose type depends
15468 on itself recursively. This is most obvious with decltype, but can also
15469 occur with enumeration scope (c++/48969). So we need to catch infinite
15470 recursion and reject the substitution at deduction time; this function
15471 will return error_mark_node for any repeated substitution.
15473 This also catches excessive recursion such as when f<N> depends on
15474 f<N-1> across all integers, and returns error_mark_node for all the
15475 substitutions back up to the initial one.
15477 This is, of course, not reentrant. */
15478 if (excessive_deduction_depth
)
15479 return error_mark_node
;
15480 tinst
= build_tree_list (fn
, NULL_TREE
);
15483 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15485 fntype
= TREE_TYPE (fn
);
15486 if (explicit_targs
)
15490 The specified template arguments must match the template
15491 parameters in kind (i.e., type, nontype, template), and there
15492 must not be more arguments than there are parameters;
15493 otherwise type deduction fails.
15495 Nontype arguments must match the types of the corresponding
15496 nontype template parameters, or must be convertible to the
15497 types of the corresponding nontype parameters as specified in
15498 _temp.arg.nontype_, otherwise type deduction fails.
15500 All references in the function type of the function template
15501 to the corresponding template parameters are replaced by the
15502 specified template argument values. If a substitution in a
15503 template parameter or in the function type of the function
15504 template results in an invalid type, type deduction fails. */
15505 int i
, len
= TREE_VEC_LENGTH (tparms
);
15506 location_t loc
= input_location
;
15507 bool incomplete
= false;
15509 /* Adjust any explicit template arguments before entering the
15510 substitution context. */
15512 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15514 /*require_all_args=*/false,
15515 /*use_default_args=*/false));
15516 if (explicit_targs
== error_mark_node
)
15519 /* Substitute the explicit args into the function type. This is
15520 necessary so that, for instance, explicitly declared function
15521 arguments can match null pointed constants. If we were given
15522 an incomplete set of explicit args, we must not do semantic
15523 processing during substitution as we could create partial
15525 for (i
= 0; i
< len
; i
++)
15527 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15528 bool parameter_pack
= false;
15529 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15531 /* Dig out the actual parm. */
15532 if (TREE_CODE (parm
) == TYPE_DECL
15533 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15535 parm
= TREE_TYPE (parm
);
15536 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15538 else if (TREE_CODE (parm
) == PARM_DECL
)
15540 parm
= DECL_INITIAL (parm
);
15541 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15544 if (!parameter_pack
&& targ
== NULL_TREE
)
15545 /* No explicit argument for this template parameter. */
15548 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15550 /* Mark the argument pack as "incomplete". We could
15551 still deduce more arguments during unification.
15552 We remove this mark in type_unification_real. */
15555 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15556 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15557 = ARGUMENT_PACK_ARGS (targ
);
15560 /* We have some incomplete argument packs. */
15565 TREE_VALUE (tinst
) = explicit_targs
;
15566 if (!push_tinst_level (tinst
))
15568 excessive_deduction_depth
= true;
15571 processing_template_decl
+= incomplete
;
15572 input_location
= DECL_SOURCE_LOCATION (fn
);
15573 /* Ignore any access checks; we'll see them again in
15574 instantiate_template and they might have the wrong
15575 access path at this point. */
15576 push_deferring_access_checks (dk_deferred
);
15577 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15578 complain
| tf_partial
, NULL_TREE
);
15579 pop_deferring_access_checks ();
15580 input_location
= loc
;
15581 processing_template_decl
-= incomplete
;
15582 pop_tinst_level ();
15584 if (fntype
== error_mark_node
)
15587 /* Place the explicitly specified arguments in TARGS. */
15588 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15589 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15592 /* Never do unification on the 'this' parameter. */
15593 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15599 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15600 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15601 new_args
[0] = return_type
;
15602 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15607 /* We allow incomplete unification without an error message here
15608 because the standard doesn't seem to explicitly prohibit it. Our
15609 callers must be ready to deal with unification failures in any
15612 TREE_VALUE (tinst
) = targs
;
15613 /* If we aren't explaining yet, push tinst context so we can see where
15614 any errors (e.g. from class instantiations triggered by instantiation
15615 of default template arguments) come from. If we are explaining, this
15616 context is redundant. */
15617 if (!explain_p
&& !push_tinst_level (tinst
))
15619 excessive_deduction_depth
= true;
15623 /* type_unification_real will pass back any access checks from default
15624 template argument substitution. */
15625 vec
<deferred_access_check
, va_gc
> *checks
;
15628 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15629 targs
, parms
, args
, nargs
, /*subr=*/0,
15630 strict
, flags
, &checks
, explain_p
);
15632 pop_tinst_level ();
15636 /* Now that we have bindings for all of the template arguments,
15637 ensure that the arguments deduced for the template template
15638 parameters have compatible template parameter lists. We cannot
15639 check this property before we have deduced all template
15640 arguments, because the template parameter types of a template
15641 template parameter might depend on prior template parameters
15642 deduced after the template template parameter. The following
15643 ill-formed example illustrates this issue:
15645 template<typename T, template<T> class C> void f(C<5>, T);
15647 template<int N> struct X {};
15650 f(X<5>(), 5l); // error: template argument deduction fails
15653 The template parameter list of 'C' depends on the template type
15654 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15655 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15656 time that we deduce 'C'. */
15657 if (!template_template_parm_bindings_ok_p
15658 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15660 unify_inconsistent_template_template_parameters (explain_p
);
15664 /* All is well so far. Now, check:
15668 When all template arguments have been deduced, all uses of
15669 template parameters in nondeduced contexts are replaced with
15670 the corresponding deduced argument values. If the
15671 substitution results in an invalid type, as described above,
15672 type deduction fails. */
15673 TREE_VALUE (tinst
) = targs
;
15674 if (!push_tinst_level (tinst
))
15676 excessive_deduction_depth
= true;
15680 /* Also collect access checks from the instantiation. */
15681 reopen_deferring_access_checks (checks
);
15683 decl
= instantiate_template (fn
, targs
, complain
);
15685 checks
= get_deferred_access_checks ();
15686 pop_deferring_access_checks ();
15688 pop_tinst_level ();
15690 if (decl
== error_mark_node
)
15693 /* Now perform any access checks encountered during substitution. */
15694 push_access_scope (decl
);
15695 ok
= perform_access_checks (checks
, complain
);
15696 pop_access_scope (decl
);
15700 /* If we're looking for an exact match, check that what we got
15701 is indeed an exact match. It might not be if some template
15702 parameters are used in non-deduced contexts. */
15703 if (strict
== DEDUCE_EXACT
)
15705 tree substed
= TREE_TYPE (decl
);
15709 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15711 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15712 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15713 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15715 unify_type_mismatch (explain_p
, args
[i
],
15716 TREE_VALUE (sarg
));
15725 if (excessive_deduction_depth
)
15727 if (deduction_depth
== 0)
15728 /* Reset once we're all the way out. */
15729 excessive_deduction_depth
= false;
15732 /* We can't free this if a pending_template entry or last_error_tinst_level
15733 is pointing at it. */
15734 if (last_pending_template
== old_last_pend
15735 && last_error_tinst_level
== old_error_tinst
)
15741 /* Adjust types before performing type deduction, as described in
15742 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15743 sections are symmetric. PARM is the type of a function parameter
15744 or the return type of the conversion function. ARG is the type of
15745 the argument passed to the call, or the type of the value
15746 initialized with the result of the conversion function.
15747 ARG_EXPR is the original argument expression, which may be null. */
15750 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15764 /* Swap PARM and ARG throughout the remainder of this
15765 function; the handling is precisely symmetric since PARM
15766 will initialize ARG rather than vice versa. */
15774 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15775 too, but here handle it by stripping the reference from PARM
15776 rather than by adding it to ARG. */
15777 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15778 && TYPE_REF_IS_RVALUE (*parm
)
15779 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15780 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15781 && TREE_CODE (*arg
) == REFERENCE_TYPE
15782 && !TYPE_REF_IS_RVALUE (*arg
))
15783 *parm
= TREE_TYPE (*parm
);
15784 /* Nothing else to do in this case. */
15788 gcc_unreachable ();
15791 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15793 /* [temp.deduct.call]
15795 If P is not a reference type:
15797 --If A is an array type, the pointer type produced by the
15798 array-to-pointer standard conversion (_conv.array_) is
15799 used in place of A for type deduction; otherwise,
15801 --If A is a function type, the pointer type produced by
15802 the function-to-pointer standard conversion
15803 (_conv.func_) is used in place of A for type deduction;
15806 --If A is a cv-qualified type, the top level
15807 cv-qualifiers of A's type are ignored for type
15809 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15810 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15811 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15812 *arg
= build_pointer_type (*arg
);
15814 *arg
= TYPE_MAIN_VARIANT (*arg
);
15817 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15818 of the form T&&, where T is a template parameter, and the argument
15819 is an lvalue, T is deduced as A& */
15820 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15821 && TYPE_REF_IS_RVALUE (*parm
)
15822 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15823 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15824 && (arg_expr
? real_lvalue_p (arg_expr
)
15825 /* try_one_overload doesn't provide an arg_expr, but
15826 functions are always lvalues. */
15827 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15828 *arg
= build_reference_type (*arg
);
15830 /* [temp.deduct.call]
15832 If P is a cv-qualified type, the top level cv-qualifiers
15833 of P's type are ignored for type deduction. If P is a
15834 reference type, the type referred to by P is used for
15836 *parm
= TYPE_MAIN_VARIANT (*parm
);
15837 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15839 *parm
= TREE_TYPE (*parm
);
15840 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15843 /* DR 322. For conversion deduction, remove a reference type on parm
15844 too (which has been swapped into ARG). */
15845 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15846 *arg
= TREE_TYPE (*arg
);
15851 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15852 template which does contain any deducible template parameters; check if
15853 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15854 unify_one_argument. */
15857 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15858 int flags
, bool explain_p
)
15863 type
= TREE_TYPE (arg
);
15867 if (same_type_p (parm
, type
))
15868 return unify_success (explain_p
);
15870 if (strict
== DEDUCE_CONV
)
15872 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15873 explain_p
? tf_warning_or_error
: tf_none
))
15874 return unify_success (explain_p
);
15876 else if (strict
!= DEDUCE_EXACT
)
15878 if (can_convert_arg (parm
, type
,
15879 TYPE_P (arg
) ? NULL_TREE
: arg
,
15880 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15881 return unify_success (explain_p
);
15884 if (strict
== DEDUCE_EXACT
)
15885 return unify_type_mismatch (explain_p
, parm
, arg
);
15887 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15890 static bool uses_deducible_template_parms (tree type
);
15892 /* Returns true iff the expression EXPR is one from which a template
15893 argument can be deduced. In other words, if it's an undecorated
15894 use of a template non-type parameter. */
15897 deducible_expression (tree expr
)
15899 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15902 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15903 deducible way; that is, if it has a max value of <PARM> - 1. */
15906 deducible_array_bound (tree domain
)
15908 if (domain
== NULL_TREE
)
15911 tree max
= TYPE_MAX_VALUE (domain
);
15912 if (TREE_CODE (max
) != MINUS_EXPR
)
15915 return deducible_expression (TREE_OPERAND (max
, 0));
15918 /* Returns true iff the template arguments ARGS use a template parameter
15919 in a deducible way. */
15922 deducible_template_args (tree args
)
15924 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15927 tree elt
= TREE_VEC_ELT (args
, i
);
15928 if (ARGUMENT_PACK_P (elt
))
15929 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15932 if (PACK_EXPANSION_P (elt
))
15933 elt
= PACK_EXPANSION_PATTERN (elt
);
15934 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15936 else if (TYPE_P (elt
))
15937 deducible
= uses_deducible_template_parms (elt
);
15939 deducible
= deducible_expression (elt
);
15947 /* Returns true iff TYPE contains any deducible references to template
15948 parameters, as per 14.8.2.5. */
15951 uses_deducible_template_parms (tree type
)
15953 if (PACK_EXPANSION_P (type
))
15954 type
= PACK_EXPANSION_PATTERN (type
);
15961 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15962 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15968 if (POINTER_TYPE_P (type
))
15969 return uses_deducible_template_parms (TREE_TYPE (type
));
15971 /* T[integer-constant ]
15973 if (TREE_CODE (type
) == ARRAY_TYPE
)
15974 return (uses_deducible_template_parms (TREE_TYPE (type
))
15975 || deducible_array_bound (TYPE_DOMAIN (type
)));
15987 if (TYPE_PTRMEM_P (type
))
15988 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
15989 || (uses_deducible_template_parms
15990 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
15992 /* template-name <T> (where template-name refers to a class template)
15993 template-name <i> (where template-name refers to a class template) */
15994 if (CLASS_TYPE_P (type
)
15995 && CLASSTYPE_TEMPLATE_INFO (type
)
15996 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
15997 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15998 (CLASSTYPE_TI_ARGS (type
)));
16003 if (TREE_CODE (type
) == FUNCTION_TYPE
16004 || TREE_CODE (type
) == METHOD_TYPE
)
16006 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16008 tree parm
= TYPE_ARG_TYPES (type
);
16009 if (TREE_CODE (type
) == METHOD_TYPE
)
16010 parm
= TREE_CHAIN (parm
);
16011 for (; parm
; parm
= TREE_CHAIN (parm
))
16012 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16019 /* Subroutine of type_unification_real and unify_pack_expansion to
16020 handle unification of a single P/A pair. Parameters are as
16021 for those functions. */
16024 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16025 int subr
, unification_kind_t strict
, int flags
,
16028 tree arg_expr
= NULL_TREE
;
16031 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16032 return unify_invalid (explain_p
);
16033 if (arg
== unknown_type_node
)
16034 /* We can't deduce anything from this, but we might get all the
16035 template args from other function args. */
16036 return unify_success (explain_p
);
16038 /* Implicit conversions (Clause 4) will be performed on a function
16039 argument to convert it to the type of the corresponding function
16040 parameter if the parameter type contains no template-parameters that
16041 participate in template argument deduction. */
16042 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16043 /* For function parameters that contain no template-parameters at all,
16044 we have historically checked for convertibility in order to shortcut
16045 consideration of this candidate. */
16046 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16048 else if (strict
== DEDUCE_CALL
16049 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16050 /* For function parameters with only non-deducible template parameters,
16052 return unify_success (explain_p
);
16057 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16058 | UNIFY_ALLOW_MORE_CV_QUAL
16059 | UNIFY_ALLOW_DERIVED
);
16063 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16067 arg_strict
= UNIFY_ALLOW_NONE
;
16071 gcc_unreachable ();
16074 /* We only do these transformations if this is the top-level
16075 parameter_type_list in a call or declaration matching; in other
16076 situations (nested function declarators, template argument lists) we
16077 won't be comparing a type to an expression, and we don't do any type
16083 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16084 if (type_unknown_p (arg
))
16086 /* [temp.deduct.type] A template-argument can be
16087 deduced from a pointer to function or pointer
16088 to member function argument if the set of
16089 overloaded functions does not contain function
16090 templates and at most one of a set of
16091 overloaded functions provides a unique
16094 if (resolve_overloaded_unification
16095 (tparms
, targs
, parm
, arg
, strict
,
16096 arg_strict
, explain_p
))
16097 return unify_success (explain_p
);
16098 return unify_overload_resolution_failure (explain_p
, arg
);
16102 arg
= unlowered_expr_type (arg
);
16103 if (arg
== error_mark_node
)
16104 return unify_invalid (explain_p
);
16108 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16111 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16112 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16114 /* For deduction from an init-list we need the actual list. */
16115 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16117 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16120 /* Most parms like fn_type_unification.
16122 If SUBR is 1, we're being called recursively (to unify the
16123 arguments of a function or method parameter of a function
16126 CHECKS is a pointer to a vector of access checks encountered while
16127 substituting default template arguments. */
16130 type_unification_real (tree tparms
,
16134 unsigned int xnargs
,
16136 unification_kind_t strict
,
16138 vec
<deferred_access_check
, va_gc
> **checks
,
16143 int ntparms
= TREE_VEC_LENGTH (tparms
);
16144 int saw_undeduced
= 0;
16147 unsigned int nargs
;
16150 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16151 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16152 gcc_assert (ntparms
> 0);
16154 /* Reset the number of non-defaulted template arguments contained
16156 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16164 while (parms
&& parms
!= void_list_node
16167 parm
= TREE_VALUE (parms
);
16169 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16170 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16171 /* For a function parameter pack that occurs at the end of the
16172 parameter-declaration-list, the type A of each remaining
16173 argument of the call is compared with the type P of the
16174 declarator-id of the function parameter pack. */
16177 parms
= TREE_CHAIN (parms
);
16179 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16180 /* For a function parameter pack that does not occur at the
16181 end of the parameter-declaration-list, the type of the
16182 parameter pack is a non-deduced context. */
16188 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16194 && parms
!= void_list_node
16195 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16197 /* Unify the remaining arguments with the pack expansion type. */
16199 tree parmvec
= make_tree_vec (1);
16201 /* Allocate a TREE_VEC and copy in all of the arguments */
16202 argvec
= make_tree_vec (nargs
- ia
);
16203 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16204 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16206 /* Copy the parameter into parmvec. */
16207 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16208 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16209 /*subr=*/subr
, explain_p
))
16212 /* Advance to the end of the list of parameters. */
16213 parms
= TREE_CHAIN (parms
);
16216 /* Fail if we've reached the end of the parm list, and more args
16217 are present, and the parm list isn't variadic. */
16218 if (ia
< nargs
&& parms
== void_list_node
)
16219 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16220 /* Fail if parms are left and they don't have default values. */
16221 if (parms
&& parms
!= void_list_node
16222 && TREE_PURPOSE (parms
) == NULL_TREE
)
16224 unsigned int count
= nargs
;
16226 while (p
&& p
!= void_list_node
)
16229 p
= TREE_CHAIN (p
);
16231 return unify_too_few_arguments (explain_p
, ia
, count
);
16236 tsubst_flags_t complain
= (explain_p
16237 ? tf_warning_or_error
16240 for (i
= 0; i
< ntparms
; i
++)
16242 tree targ
= TREE_VEC_ELT (targs
, i
);
16243 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16245 /* Clear the "incomplete" flags on all argument packs now so that
16246 substituting them into later default arguments works. */
16247 if (targ
&& ARGUMENT_PACK_P (targ
))
16249 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16250 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16253 if (targ
|| tparm
== error_mark_node
)
16255 tparm
= TREE_VALUE (tparm
);
16257 /* If this is an undeduced nontype parameter that depends on
16258 a type parameter, try another pass; its type may have been
16259 deduced from a later argument than the one from which
16260 this parameter can be deduced. */
16261 if (TREE_CODE (tparm
) == PARM_DECL
16262 && uses_template_parms (TREE_TYPE (tparm
))
16263 && !saw_undeduced
++)
16266 /* Core issue #226 (C++0x) [temp.deduct]:
16268 If a template argument has not been deduced, its
16269 default template argument, if any, is used.
16271 When we are in C++98 mode, TREE_PURPOSE will either
16272 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16273 to explicitly check cxx_dialect here. */
16274 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16276 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16277 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16278 reopen_deferring_access_checks (*checks
);
16279 location_t save_loc
= input_location
;
16281 input_location
= DECL_SOURCE_LOCATION (parm
);
16282 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16283 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16285 input_location
= save_loc
;
16286 *checks
= get_deferred_access_checks ();
16287 pop_deferring_access_checks ();
16288 if (arg
== error_mark_node
)
16292 TREE_VEC_ELT (targs
, i
) = arg
;
16293 /* The position of the first default template argument,
16294 is also the number of non-defaulted arguments in TARGS.
16296 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16297 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16302 /* If the type parameter is a parameter pack, then it will
16303 be deduced to an empty parameter pack. */
16304 if (template_parameter_pack_p (tparm
))
16308 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16310 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16311 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16312 TREE_CONSTANT (arg
) = 1;
16315 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16317 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16319 TREE_VEC_ELT (targs
, i
) = arg
;
16323 return unify_parameter_deduction_failure (explain_p
, tparm
);
16326 #ifdef ENABLE_CHECKING
16327 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16331 return unify_success (explain_p
);
16334 /* Subroutine of type_unification_real. Args are like the variables
16335 at the call site. ARG is an overloaded function (or template-id);
16336 we try deducing template args from each of the overloads, and if
16337 only one succeeds, we go with that. Modifies TARGS and returns
16338 true on success. */
16341 resolve_overloaded_unification (tree tparms
,
16345 unification_kind_t strict
,
16349 tree tempargs
= copy_node (targs
);
16351 tree goodfn
= NULL_TREE
;
16354 if (TREE_CODE (arg
) == ADDR_EXPR
)
16356 arg
= TREE_OPERAND (arg
, 0);
16362 if (TREE_CODE (arg
) == COMPONENT_REF
)
16363 /* Handle `&x' where `x' is some static or non-static member
16365 arg
= TREE_OPERAND (arg
, 1);
16367 if (TREE_CODE (arg
) == OFFSET_REF
)
16368 arg
= TREE_OPERAND (arg
, 1);
16370 /* Strip baselink information. */
16371 if (BASELINK_P (arg
))
16372 arg
= BASELINK_FUNCTIONS (arg
);
16374 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16376 /* If we got some explicit template args, we need to plug them into
16377 the affected templates before we try to unify, in case the
16378 explicit args will completely resolve the templates in question. */
16381 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16382 arg
= TREE_OPERAND (arg
, 0);
16384 for (; arg
; arg
= OVL_NEXT (arg
))
16386 tree fn
= OVL_CURRENT (arg
);
16387 tree subargs
, elem
;
16389 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16392 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16393 expl_subargs
, NULL_TREE
, tf_none
,
16394 /*require_all_args=*/true,
16395 /*use_default_args=*/true);
16396 if (subargs
!= error_mark_node
16397 && !any_dependent_template_arguments_p (subargs
))
16399 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
16400 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16401 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16402 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16411 /* If no templates (or more than one) are fully resolved by the
16412 explicit arguments, this template-id is a non-deduced context; it
16413 could still be OK if we deduce all template arguments for the
16414 enclosing call through other arguments. */
16418 else if (TREE_CODE (arg
) != OVERLOAD
16419 && TREE_CODE (arg
) != FUNCTION_DECL
)
16420 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16421 -- but the deduction does not succeed because the expression is
16422 not just the function on its own. */
16425 for (; arg
; arg
= OVL_NEXT (arg
))
16426 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16427 TREE_TYPE (OVL_CURRENT (arg
)),
16428 strict
, sub_strict
, addr_p
, explain_p
)
16429 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16431 goodfn
= OVL_CURRENT (arg
);
16435 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16436 to function or pointer to member function argument if the set of
16437 overloaded functions does not contain function templates and at most
16438 one of a set of overloaded functions provides a unique match.
16440 So if we found multiple possibilities, we return success but don't
16441 deduce anything. */
16445 int i
= TREE_VEC_LENGTH (targs
);
16447 if (TREE_VEC_ELT (tempargs
, i
))
16448 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16456 /* Core DR 115: In contexts where deduction is done and fails, or in
16457 contexts where deduction is not done, if a template argument list is
16458 specified and it, along with any default template arguments, identifies
16459 a single function template specialization, then the template-id is an
16460 lvalue for the function template specialization. */
16463 resolve_nondeduced_context (tree orig_expr
)
16465 tree expr
, offset
, baselink
;
16468 if (!type_unknown_p (orig_expr
))
16473 offset
= NULL_TREE
;
16474 baselink
= NULL_TREE
;
16476 if (TREE_CODE (expr
) == ADDR_EXPR
)
16478 expr
= TREE_OPERAND (expr
, 0);
16481 if (TREE_CODE (expr
) == OFFSET_REF
)
16484 expr
= TREE_OPERAND (expr
, 1);
16486 if (BASELINK_P (expr
))
16489 expr
= BASELINK_FUNCTIONS (expr
);
16492 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16495 tree goodfn
= NULL_TREE
;
16497 /* If we got some explicit template args, we need to plug them into
16498 the affected templates before we try to unify, in case the
16499 explicit args will completely resolve the templates in question. */
16501 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16502 tree arg
= TREE_OPERAND (expr
, 0);
16503 tree badfn
= NULL_TREE
;
16504 tree badargs
= NULL_TREE
;
16506 for (; arg
; arg
= OVL_NEXT (arg
))
16508 tree fn
= OVL_CURRENT (arg
);
16509 tree subargs
, elem
;
16511 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16514 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16515 expl_subargs
, NULL_TREE
, tf_none
,
16516 /*require_all_args=*/true,
16517 /*use_default_args=*/true);
16518 if (subargs
!= error_mark_node
16519 && !any_dependent_template_arguments_p (subargs
))
16521 elem
= instantiate_template (fn
, subargs
, tf_none
);
16522 if (elem
== error_mark_node
)
16527 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16536 mark_used (goodfn
);
16539 expr
= build_baselink (BASELINK_BINFO (baselink
),
16540 BASELINK_ACCESS_BINFO (baselink
),
16541 expr
, BASELINK_OPTYPE (baselink
));
16545 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16546 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16549 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16552 else if (good
== 0 && badargs
)
16553 /* There were no good options and at least one bad one, so let the
16554 user know what the problem is. */
16555 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16560 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16561 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16562 different overloads deduce different arguments for a given parm.
16563 ADDR_P is true if the expression for which deduction is being
16564 performed was of the form "& fn" rather than simply "fn".
16566 Returns 1 on success. */
16569 try_one_overload (tree tparms
,
16574 unification_kind_t strict
,
16583 if (arg
== error_mark_node
)
16586 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16587 to function or pointer to member function argument if the set of
16588 overloaded functions does not contain function templates and at most
16589 one of a set of overloaded functions provides a unique match.
16591 So if this is a template, just return success. */
16593 if (uses_template_parms (arg
))
16596 if (TREE_CODE (arg
) == METHOD_TYPE
)
16597 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16599 arg
= build_pointer_type (arg
);
16601 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16603 /* We don't copy orig_targs for this because if we have already deduced
16604 some template args from previous args, unify would complain when we
16605 try to deduce a template parameter for the same argument, even though
16606 there isn't really a conflict. */
16607 nargs
= TREE_VEC_LENGTH (targs
);
16608 tempargs
= make_tree_vec (nargs
);
16610 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16613 /* First make sure we didn't deduce anything that conflicts with
16614 explicitly specified args. */
16615 for (i
= nargs
; i
--; )
16617 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16618 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16622 else if (uses_template_parms (elt
))
16623 /* Since we're unifying against ourselves, we will fill in
16624 template args used in the function parm list with our own
16625 template parms. Discard them. */
16626 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16627 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16631 for (i
= nargs
; i
--; )
16633 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16636 TREE_VEC_ELT (targs
, i
) = elt
;
16642 /* PARM is a template class (perhaps with unbound template
16643 parameters). ARG is a fully instantiated type. If ARG can be
16644 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16645 TARGS are as for unify. */
16648 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16651 tree copy_of_targs
;
16653 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16654 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16655 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16658 /* We need to make a new template argument vector for the call to
16659 unify. If we used TARGS, we'd clutter it up with the result of
16660 the attempted unification, even if this class didn't work out.
16661 We also don't want to commit ourselves to all the unifications
16662 we've already done, since unification is supposed to be done on
16663 an argument-by-argument basis. In other words, consider the
16664 following pathological case:
16666 template <int I, int J, int K>
16669 template <int I, int J>
16670 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16672 template <int I, int J, int K>
16673 void f(S<I, J, K>, S<I, I, I>);
16682 Now, by the time we consider the unification involving `s2', we
16683 already know that we must have `f<0, 0, 0>'. But, even though
16684 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16685 because there are two ways to unify base classes of S<0, 1, 2>
16686 with S<I, I, I>. If we kept the already deduced knowledge, we
16687 would reject the possibility I=1. */
16688 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16690 /* If unification failed, we're done. */
16691 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16692 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16698 /* Given a template type PARM and a class type ARG, find the unique
16699 base type in ARG that is an instance of PARM. We do not examine
16700 ARG itself; only its base-classes. If there is not exactly one
16701 appropriate base class, return NULL_TREE. PARM may be the type of
16702 a partial specialization, as well as a plain template type. Used
16705 static enum template_base_result
16706 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16707 bool explain_p
, tree
*result
)
16709 tree rval
= NULL_TREE
;
16712 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16714 binfo
= TYPE_BINFO (complete_type (arg
));
16717 /* The type could not be completed. */
16718 *result
= NULL_TREE
;
16719 return tbr_incomplete_type
;
16722 /* Walk in inheritance graph order. The search order is not
16723 important, and this avoids multiple walks of virtual bases. */
16724 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16726 tree r
= try_class_unification (tparms
, targs
, parm
,
16727 BINFO_TYPE (binfo
), explain_p
);
16731 /* If there is more than one satisfactory baseclass, then:
16735 If they yield more than one possible deduced A, the type
16739 if (rval
&& !same_type_p (r
, rval
))
16741 *result
= NULL_TREE
;
16742 return tbr_ambiguous_baseclass
;
16750 return tbr_success
;
16753 /* Returns the level of DECL, which declares a template parameter. */
16756 template_decl_level (tree decl
)
16758 switch (TREE_CODE (decl
))
16761 case TEMPLATE_DECL
:
16762 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16765 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16768 gcc_unreachable ();
16773 /* Decide whether ARG can be unified with PARM, considering only the
16774 cv-qualifiers of each type, given STRICT as documented for unify.
16775 Returns nonzero iff the unification is OK on that basis. */
16778 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16780 int arg_quals
= cp_type_quals (arg
);
16781 int parm_quals
= cp_type_quals (parm
);
16783 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16784 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16786 /* Although a CVR qualifier is ignored when being applied to a
16787 substituted template parameter ([8.3.2]/1 for example), that
16788 does not allow us to unify "const T" with "int&" because both
16789 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16790 It is ok when we're allowing additional CV qualifiers
16791 at the outer level [14.8.2.1]/3,1st bullet. */
16792 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16793 || TREE_CODE (arg
) == FUNCTION_TYPE
16794 || TREE_CODE (arg
) == METHOD_TYPE
)
16795 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16798 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16799 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16803 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16804 && (arg_quals
& parm_quals
) != parm_quals
)
16807 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16808 && (parm_quals
& arg_quals
) != arg_quals
)
16814 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16816 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16818 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16819 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16820 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16822 *index
= TEMPLATE_TYPE_IDX (parm
);
16823 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16827 *index
= TEMPLATE_PARM_IDX (parm
);
16828 *level
= TEMPLATE_PARM_LEVEL (parm
);
16832 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16834 if (unify (TP, TA, P, A, S, EP)) \
16838 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16839 expansion at the end of PACKED_PARMS. Returns 0 if the type
16840 deduction succeeds, 1 otherwise. STRICT is the same as in
16841 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16842 call argument list. We'll need to adjust the arguments to make them
16843 types. SUBR tells us if this is from a recursive call to
16844 type_unification_real, or for comparing two template argument
16848 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16849 tree packed_args
, unification_kind_t strict
,
16850 bool subr
, bool explain_p
)
16853 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16854 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16855 tree pack
, packs
= NULL_TREE
;
16856 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16857 int len
= TREE_VEC_LENGTH (packed_args
);
16859 /* Determine the parameter packs we will be deducing from the
16860 pattern, and record their current deductions. */
16861 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16862 pack
; pack
= TREE_CHAIN (pack
))
16864 tree parm_pack
= TREE_VALUE (pack
);
16867 /* Determine the index and level of this parameter pack. */
16868 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16870 /* Keep track of the parameter packs and their corresponding
16872 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16873 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16876 /* Loop through all of the arguments that have not yet been
16877 unified and unify each with the pattern. */
16878 for (i
= start
; i
< len
; i
++)
16881 bool any_explicit
= false;
16882 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16884 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16885 or the element of its argument pack at the current index if
16886 this argument was explicitly specified. */
16887 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16891 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16894 if (TREE_VALUE (pack
)
16895 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16896 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16898 any_explicit
= true;
16899 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16901 TMPL_ARG (targs
, level
, idx
) = arg
;
16904 /* If we had explicit template arguments, substitute them into the
16905 pattern before deduction. */
16908 /* Some arguments might still be unspecified or dependent. */
16910 ++processing_template_decl
;
16911 dependent
= any_dependent_template_arguments_p (targs
);
16913 --processing_template_decl
;
16914 parm
= tsubst (pattern
, targs
,
16915 explain_p
? tf_warning_or_error
: tf_none
,
16918 --processing_template_decl
;
16919 if (parm
== error_mark_node
)
16925 /* Unify the pattern with the current argument. */
16926 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16927 LOOKUP_IMPLICIT
, explain_p
))
16930 /* For each parameter pack, collect the deduced value. */
16931 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16934 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16936 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16937 TMPL_ARG (targs
, level
, idx
);
16941 /* Verify that the results of unification with the parameter packs
16942 produce results consistent with what we've seen before, and make
16943 the deduced argument packs available. */
16944 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16946 tree old_pack
= TREE_VALUE (pack
);
16947 tree new_args
= TREE_TYPE (pack
);
16948 int i
, len
= TREE_VEC_LENGTH (new_args
);
16950 bool nondeduced_p
= false;
16952 /* By default keep the original deduced argument pack.
16953 If necessary, more specific code is going to update the
16954 resulting deduced argument later down in this function. */
16955 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16956 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16958 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16959 actually deduce anything. */
16960 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16961 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16962 nondeduced_p
= true;
16966 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16968 /* If we had fewer function args than explicit template args,
16969 just use the explicits. */
16970 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16971 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16972 if (len
< explicit_len
)
16973 new_args
= explicit_args
;
16979 /* Build the deduced *_ARGUMENT_PACK. */
16980 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16982 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16983 TREE_TYPE (result
) =
16984 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16985 TREE_CONSTANT (result
) = 1;
16988 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16990 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16992 /* Note the deduced argument packs for this parameter
16994 TMPL_ARG (targs
, level
, idx
) = result
;
16996 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16997 && (ARGUMENT_PACK_ARGS (old_pack
)
16998 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17000 /* We only had the explicitly-provided arguments before, but
17001 now we have a complete set of arguments. */
17002 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17004 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17005 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17006 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17010 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17011 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17013 if (!comp_template_args_with_info (old_args
, new_args
,
17014 &bad_old_arg
, &bad_new_arg
))
17015 /* Inconsistent unification of this parameter pack. */
17016 return unify_parameter_pack_inconsistent (explain_p
,
17022 return unify_success (explain_p
);
17025 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17026 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17027 parameters and return value are as for unify. */
17030 unify_array_domain (tree tparms
, tree targs
,
17031 tree parm_dom
, tree arg_dom
,
17039 /* Our representation of array types uses "N - 1" as the
17040 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17041 not an integer constant. We cannot unify arbitrarily
17042 complex expressions, so we eliminate the MINUS_EXPRs
17044 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17045 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17048 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17049 parm_max
= TREE_OPERAND (parm_max
, 0);
17051 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17052 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17055 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17056 trying to unify the type of a variable with the type
17057 of a template parameter. For example:
17059 template <unsigned int N>
17060 void f (char (&) [N]);
17067 Here, the type of the ARG will be "int [g(i)]", and
17068 may be a SAVE_EXPR, etc. */
17069 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17070 return unify_vla_arg (explain_p
, arg_dom
);
17071 arg_max
= TREE_OPERAND (arg_max
, 0);
17074 /* If only one of the bounds used a MINUS_EXPR, compensate
17075 by adding one to the other bound. */
17076 if (parm_cst
&& !arg_cst
)
17077 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17081 else if (arg_cst
&& !parm_cst
)
17082 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17087 return unify (tparms
, targs
, parm_max
, arg_max
,
17088 UNIFY_ALLOW_INTEGER
, explain_p
);
17091 /* Deduce the value of template parameters. TPARMS is the (innermost)
17092 set of template parameters to a template. TARGS is the bindings
17093 for those template parameters, as determined thus far; TARGS may
17094 include template arguments for outer levels of template parameters
17095 as well. PARM is a parameter to a template function, or a
17096 subcomponent of that parameter; ARG is the corresponding argument.
17097 This function attempts to match PARM with ARG in a manner
17098 consistent with the existing assignments in TARGS. If more values
17099 are deduced, then TARGS is updated.
17101 Returns 0 if the type deduction succeeds, 1 otherwise. The
17102 parameter STRICT is a bitwise or of the following flags:
17105 Require an exact match between PARM and ARG.
17106 UNIFY_ALLOW_MORE_CV_QUAL:
17107 Allow the deduced ARG to be more cv-qualified (by qualification
17108 conversion) than ARG.
17109 UNIFY_ALLOW_LESS_CV_QUAL:
17110 Allow the deduced ARG to be less cv-qualified than ARG.
17111 UNIFY_ALLOW_DERIVED:
17112 Allow the deduced ARG to be a template base class of ARG,
17113 or a pointer to a template base class of the type pointed to by
17115 UNIFY_ALLOW_INTEGER:
17116 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17117 case for more information.
17118 UNIFY_ALLOW_OUTER_LEVEL:
17119 This is the outermost level of a deduction. Used to determine validity
17120 of qualification conversions. A valid qualification conversion must
17121 have const qualified pointers leading up to the inner type which
17122 requires additional CV quals, except at the outer level, where const
17123 is not required [conv.qual]. It would be normal to set this flag in
17124 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17125 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17126 This is the outermost level of a deduction, and PARM can be more CV
17127 qualified at this point.
17128 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17129 This is the outermost level of a deduction, and PARM can be less CV
17130 qualified at this point. */
17133 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17139 int strict_in
= strict
;
17141 /* I don't think this will do the right thing with respect to types.
17142 But the only case I've seen it in so far has been array bounds, where
17143 signedness is the only information lost, and I think that will be
17145 while (TREE_CODE (parm
) == NOP_EXPR
)
17146 parm
= TREE_OPERAND (parm
, 0);
17148 if (arg
== error_mark_node
)
17149 return unify_invalid (explain_p
);
17150 if (arg
== unknown_type_node
17151 || arg
== init_list_type_node
)
17152 /* We can't deduce anything from this, but we might get all the
17153 template args from other function args. */
17154 return unify_success (explain_p
);
17156 /* If PARM uses template parameters, then we can't bail out here,
17157 even if ARG == PARM, since we won't record unifications for the
17158 template parameters. We might need them if we're trying to
17159 figure out which of two things is more specialized. */
17160 if (arg
== parm
&& !uses_template_parms (parm
))
17161 return unify_success (explain_p
);
17163 /* Handle init lists early, so the rest of the function can assume
17164 we're dealing with a type. */
17165 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17169 tree orig_parm
= parm
;
17171 /* Replace T with std::initializer_list<T> for deduction. */
17172 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17173 && flag_deduce_init_list
)
17174 parm
= listify (parm
);
17176 if (!is_std_init_list (parm
)
17177 && TREE_CODE (parm
) != ARRAY_TYPE
)
17178 /* We can only deduce from an initializer list argument if the
17179 parameter is std::initializer_list or an array; otherwise this
17180 is a non-deduced context. */
17181 return unify_success (explain_p
);
17183 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17184 elttype
= TREE_TYPE (parm
);
17186 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17188 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17190 int elt_strict
= strict
;
17192 if (elt
== error_mark_node
)
17193 return unify_invalid (explain_p
);
17195 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17197 tree type
= TREE_TYPE (elt
);
17198 /* It should only be possible to get here for a call. */
17199 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17200 elt_strict
|= maybe_adjust_types_for_deduction
17201 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17205 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17209 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17211 /* Also deduce from the length of the initializer list. */
17212 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17213 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17214 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17218 /* If the std::initializer_list<T> deduction worked, replace the
17219 deduced A with std::initializer_list<A>. */
17220 if (orig_parm
!= parm
)
17222 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17223 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17224 targ
= listify (targ
);
17225 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17227 return unify_success (explain_p
);
17230 /* Immediately reject some pairs that won't unify because of
17231 cv-qualification mismatches. */
17232 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17234 /* It is the elements of the array which hold the cv quals of an array
17235 type, and the elements might be template type parms. We'll check
17236 when we recurse. */
17237 && TREE_CODE (arg
) != ARRAY_TYPE
17238 /* We check the cv-qualifiers when unifying with template type
17239 parameters below. We want to allow ARG `const T' to unify with
17240 PARM `T' for example, when computing which of two templates
17241 is more specialized, for example. */
17242 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17243 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17244 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17246 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17247 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17248 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17249 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17250 strict
&= ~UNIFY_ALLOW_DERIVED
;
17251 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17252 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17254 switch (TREE_CODE (parm
))
17256 case TYPENAME_TYPE
:
17258 case UNBOUND_CLASS_TEMPLATE
:
17259 /* In a type which contains a nested-name-specifier, template
17260 argument values cannot be deduced for template parameters used
17261 within the nested-name-specifier. */
17262 return unify_success (explain_p
);
17264 case TEMPLATE_TYPE_PARM
:
17265 case TEMPLATE_TEMPLATE_PARM
:
17266 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17267 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17268 if (tparm
== error_mark_node
)
17269 return unify_invalid (explain_p
);
17271 if (TEMPLATE_TYPE_LEVEL (parm
)
17272 != template_decl_level (tparm
))
17273 /* The PARM is not one we're trying to unify. Just check
17274 to see if it matches ARG. */
17276 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17277 && (is_auto (parm
) ? is_auto (arg
)
17278 : same_type_p (parm
, arg
)))
17279 return unify_success (explain_p
);
17281 return unify_type_mismatch (explain_p
, parm
, arg
);
17283 idx
= TEMPLATE_TYPE_IDX (parm
);
17284 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17285 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17286 if (tparm
== error_mark_node
)
17287 return unify_invalid (explain_p
);
17289 /* Check for mixed types and values. */
17290 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17291 && TREE_CODE (tparm
) != TYPE_DECL
)
17292 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17293 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17294 gcc_unreachable ();
17296 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17298 /* ARG must be constructed from a template class or a template
17299 template parameter. */
17300 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17301 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17302 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17305 tree parmvec
= TYPE_TI_ARGS (parm
);
17306 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17307 tree full_argvec
= add_to_template_args (targs
, argvec
);
17309 = DECL_INNERMOST_TEMPLATE_PARMS
17310 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17312 int parm_variadic_p
= 0;
17314 /* The resolution to DR150 makes clear that default
17315 arguments for an N-argument may not be used to bind T
17316 to a template template parameter with fewer than N
17317 parameters. It is not safe to permit the binding of
17318 default arguments as an extension, as that may change
17319 the meaning of a conforming program. Consider:
17321 struct Dense { static const unsigned int dim = 1; };
17323 template <template <typename> class View,
17325 void operator+(float, View<Block> const&);
17327 template <typename Block,
17328 unsigned int Dim = Block::dim>
17329 struct Lvalue_proxy { operator float() const; };
17333 Lvalue_proxy<Dense> p;
17338 Here, if Lvalue_proxy is permitted to bind to View, then
17339 the global operator+ will be used; if they are not, the
17340 Lvalue_proxy will be converted to float. */
17341 if (coerce_template_parms (parm_parms
,
17343 TYPE_TI_TEMPLATE (parm
),
17345 ? tf_warning_or_error
17347 /*require_all_args=*/true,
17348 /*use_default_args=*/false)
17349 == error_mark_node
)
17352 /* Deduce arguments T, i from TT<T> or TT<i>.
17353 We check each element of PARMVEC and ARGVEC individually
17354 rather than the whole TREE_VEC since they can have
17355 different number of elements. */
17357 parmvec
= expand_template_argument_pack (parmvec
);
17358 argvec
= expand_template_argument_pack (argvec
);
17360 len
= TREE_VEC_LENGTH (parmvec
);
17362 /* Check if the parameters end in a pack, making them
17365 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17366 parm_variadic_p
= 1;
17368 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17369 /* If the template argument list of P contains a pack
17370 expansion that is not the last template argument, the
17371 entire template argument list is a non-deduced
17373 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17374 return unify_success (explain_p
);
17376 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17377 return unify_too_few_arguments (explain_p
,
17378 TREE_VEC_LENGTH (argvec
), len
);
17380 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17382 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17383 TREE_VEC_ELT (parmvec
, i
),
17384 TREE_VEC_ELT (argvec
, i
),
17385 UNIFY_ALLOW_NONE
, explain_p
);
17388 if (parm_variadic_p
17389 && unify_pack_expansion (tparms
, targs
,
17392 /*subr=*/true, explain_p
))
17395 arg
= TYPE_TI_TEMPLATE (arg
);
17397 /* Fall through to deduce template name. */
17400 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17401 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17403 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17405 /* Simple cases: Value already set, does match or doesn't. */
17406 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17407 return unify_success (explain_p
);
17409 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17413 /* If PARM is `const T' and ARG is only `int', we don't have
17414 a match unless we are allowing additional qualification.
17415 If ARG is `const int' and PARM is just `T' that's OK;
17416 that binds `const int' to `T'. */
17417 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17419 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17421 /* Consider the case where ARG is `const volatile int' and
17422 PARM is `const T'. Then, T should be `volatile int'. */
17423 arg
= cp_build_qualified_type_real
17424 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17425 if (arg
== error_mark_node
)
17426 return unify_invalid (explain_p
);
17428 /* Simple cases: Value already set, does match or doesn't. */
17429 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17430 return unify_success (explain_p
);
17432 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17434 /* Make sure that ARG is not a variable-sized array. (Note
17435 that were talking about variable-sized arrays (like
17436 `int[n]'), rather than arrays of unknown size (like
17437 `int[]').) We'll get very confused by such a type since
17438 the bound of the array is not constant, and therefore
17439 not mangleable. Besides, such types are not allowed in
17440 ISO C++, so we can do as we please here. We do allow
17441 them for 'auto' deduction, since that isn't ABI-exposed. */
17442 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17443 return unify_vla_arg (explain_p
, arg
);
17445 /* Strip typedefs as in convert_template_argument. */
17446 arg
= canonicalize_type_argument (arg
, tf_none
);
17449 /* If ARG is a parameter pack or an expansion, we cannot unify
17450 against it unless PARM is also a parameter pack. */
17451 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17452 && !template_parameter_pack_p (parm
))
17453 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17455 /* If the argument deduction results is a METHOD_TYPE,
17456 then there is a problem.
17457 METHOD_TYPE doesn't map to any real C++ type the result of
17458 the deduction can not be of that type. */
17459 if (TREE_CODE (arg
) == METHOD_TYPE
)
17460 return unify_method_type_error (explain_p
, arg
);
17462 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17463 return unify_success (explain_p
);
17465 case TEMPLATE_PARM_INDEX
:
17466 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17467 if (tparm
== error_mark_node
)
17468 return unify_invalid (explain_p
);
17470 if (TEMPLATE_PARM_LEVEL (parm
)
17471 != template_decl_level (tparm
))
17473 /* The PARM is not one we're trying to unify. Just check
17474 to see if it matches ARG. */
17475 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17476 && cp_tree_equal (parm
, arg
));
17478 unify_expression_unequal (explain_p
, parm
, arg
);
17482 idx
= TEMPLATE_PARM_IDX (parm
);
17483 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17487 int x
= !cp_tree_equal (targ
, arg
);
17489 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17493 /* [temp.deduct.type] If, in the declaration of a function template
17494 with a non-type template-parameter, the non-type
17495 template-parameter is used in an expression in the function
17496 parameter-list and, if the corresponding template-argument is
17497 deduced, the template-argument type shall match the type of the
17498 template-parameter exactly, except that a template-argument
17499 deduced from an array bound may be of any integral type.
17500 The non-type parameter might use already deduced type parameters. */
17501 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17502 if (!TREE_TYPE (arg
))
17503 /* Template-parameter dependent expression. Just accept it for now.
17504 It will later be processed in convert_template_argument. */
17506 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17508 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17509 && CP_INTEGRAL_TYPE_P (tparm
))
17510 /* Convert the ARG to the type of PARM; the deduced non-type
17511 template argument must exactly match the types of the
17512 corresponding parameter. */
17513 arg
= fold (build_nop (tparm
, arg
));
17514 else if (uses_template_parms (tparm
))
17515 /* We haven't deduced the type of this parameter yet. Try again
17517 return unify_success (explain_p
);
17519 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17521 /* If ARG is a parameter pack or an expansion, we cannot unify
17522 against it unless PARM is also a parameter pack. */
17523 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17524 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17525 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17527 arg
= strip_typedefs_expr (arg
);
17528 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17529 return unify_success (explain_p
);
17533 /* A pointer-to-member constant can be unified only with
17534 another constant. */
17535 if (TREE_CODE (arg
) != PTRMEM_CST
)
17536 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17538 /* Just unify the class member. It would be useless (and possibly
17539 wrong, depending on the strict flags) to unify also
17540 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17541 arg refer to the same variable, even if through different
17542 classes. For instance:
17544 struct A { int x; };
17547 Unification of &A::x and &B::x must succeed. */
17548 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17549 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17554 if (!TYPE_PTR_P (arg
))
17555 return unify_type_mismatch (explain_p
, parm
, arg
);
17557 /* [temp.deduct.call]
17559 A can be another pointer or pointer to member type that can
17560 be converted to the deduced A via a qualification
17561 conversion (_conv.qual_).
17563 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17564 This will allow for additional cv-qualification of the
17565 pointed-to types if appropriate. */
17567 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17568 /* The derived-to-base conversion only persists through one
17569 level of pointers. */
17570 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17572 return unify (tparms
, targs
, TREE_TYPE (parm
),
17573 TREE_TYPE (arg
), strict
, explain_p
);
17576 case REFERENCE_TYPE
:
17577 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17578 return unify_type_mismatch (explain_p
, parm
, arg
);
17579 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17580 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17583 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17584 return unify_type_mismatch (explain_p
, parm
, arg
);
17585 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17586 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17587 return unify_type_mismatch (explain_p
, parm
, arg
);
17588 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17589 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17590 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17591 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17592 TYPE_DOMAIN (arg
), explain_p
);
17593 return unify_success (explain_p
);
17600 case ENUMERAL_TYPE
:
17603 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17604 return unify_type_mismatch (explain_p
, parm
, arg
);
17606 /* We have already checked cv-qualification at the top of the
17608 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17609 return unify_type_mismatch (explain_p
, parm
, arg
);
17611 /* As far as unification is concerned, this wins. Later checks
17612 will invalidate it if necessary. */
17613 return unify_success (explain_p
);
17615 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17616 /* Type INTEGER_CST can come from ordinary constant template args. */
17618 while (TREE_CODE (arg
) == NOP_EXPR
)
17619 arg
= TREE_OPERAND (arg
, 0);
17621 if (TREE_CODE (arg
) != INTEGER_CST
)
17622 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17623 return (tree_int_cst_equal (parm
, arg
)
17624 ? unify_success (explain_p
)
17625 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17629 int i
, len
, argslen
;
17630 int parm_variadic_p
= 0;
17632 if (TREE_CODE (arg
) != TREE_VEC
)
17633 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17635 len
= TREE_VEC_LENGTH (parm
);
17636 argslen
= TREE_VEC_LENGTH (arg
);
17638 /* Check for pack expansions in the parameters. */
17639 for (i
= 0; i
< len
; ++i
)
17641 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17644 /* We can unify against something with a trailing
17646 parm_variadic_p
= 1;
17648 /* [temp.deduct.type]/9: If the template argument list of
17649 P contains a pack expansion that is not the last
17650 template argument, the entire template argument list
17651 is a non-deduced context. */
17652 return unify_success (explain_p
);
17656 /* If we don't have enough arguments to satisfy the parameters
17657 (not counting the pack expression at the end), or we have
17658 too many arguments for a parameter list that doesn't end in
17659 a pack expression, we can't unify. */
17660 if (parm_variadic_p
17661 ? argslen
< len
- parm_variadic_p
17663 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17665 /* Unify all of the parameters that precede the (optional)
17666 pack expression. */
17667 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17669 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17670 TREE_VEC_ELT (parm
, i
),
17671 TREE_VEC_ELT (arg
, i
),
17672 UNIFY_ALLOW_NONE
, explain_p
);
17674 if (parm_variadic_p
)
17675 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17677 /*subr=*/true, explain_p
);
17678 return unify_success (explain_p
);
17683 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17684 return unify_type_mismatch (explain_p
, parm
, arg
);
17686 if (TYPE_PTRMEMFUNC_P (parm
))
17688 if (!TYPE_PTRMEMFUNC_P (arg
))
17689 return unify_type_mismatch (explain_p
, parm
, arg
);
17691 return unify (tparms
, targs
,
17692 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17693 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17694 strict
, explain_p
);
17697 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17699 tree t
= NULL_TREE
;
17701 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17703 /* First, we try to unify the PARM and ARG directly. */
17704 t
= try_class_unification (tparms
, targs
,
17705 parm
, arg
, explain_p
);
17709 /* Fallback to the special case allowed in
17710 [temp.deduct.call]:
17712 If P is a class, and P has the form
17713 template-id, then A can be a derived class of
17714 the deduced A. Likewise, if P is a pointer to
17715 a class of the form template-id, A can be a
17716 pointer to a derived class pointed to by the
17718 enum template_base_result r
;
17719 r
= get_template_base (tparms
, targs
, parm
, arg
,
17723 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17726 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17727 && (CLASSTYPE_TI_TEMPLATE (parm
)
17728 == CLASSTYPE_TI_TEMPLATE (arg
)))
17729 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17730 Then, we should unify `int' and `U'. */
17733 /* There's no chance of unification succeeding. */
17734 return unify_type_mismatch (explain_p
, parm
, arg
);
17736 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17737 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17739 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17740 return unify_type_mismatch (explain_p
, parm
, arg
);
17741 return unify_success (explain_p
);
17744 case FUNCTION_TYPE
:
17746 unsigned int nargs
;
17751 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17752 return unify_type_mismatch (explain_p
, parm
, arg
);
17754 /* CV qualifications for methods can never be deduced, they must
17755 match exactly. We need to check them explicitly here,
17756 because type_unification_real treats them as any other
17757 cv-qualified parameter. */
17758 if (TREE_CODE (parm
) == METHOD_TYPE
17759 && (!check_cv_quals_for_unify
17761 class_of_this_parm (arg
),
17762 class_of_this_parm (parm
))))
17763 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17765 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17766 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17768 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17769 args
= XALLOCAVEC (tree
, nargs
);
17770 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17771 a
!= NULL_TREE
&& a
!= void_list_node
;
17772 a
= TREE_CHAIN (a
), ++i
)
17773 args
[i
] = TREE_VALUE (a
);
17776 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17777 args
, nargs
, 1, DEDUCE_EXACT
,
17778 LOOKUP_NORMAL
, NULL
, explain_p
);
17782 /* Unify a pointer to member with a pointer to member function, which
17783 deduces the type of the member as a function type. */
17784 if (TYPE_PTRMEMFUNC_P (arg
))
17786 /* Check top-level cv qualifiers */
17787 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17788 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17790 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17791 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17792 UNIFY_ALLOW_NONE
, explain_p
);
17794 /* Determine the type of the function we are unifying against. */
17795 tree fntype
= static_fn_type (arg
);
17797 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17800 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17801 return unify_type_mismatch (explain_p
, parm
, arg
);
17802 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17803 TYPE_OFFSET_BASETYPE (arg
),
17804 UNIFY_ALLOW_NONE
, explain_p
);
17805 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17806 strict
, explain_p
);
17809 if (DECL_TEMPLATE_PARM_P (parm
))
17810 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17811 if (arg
!= integral_constant_value (parm
))
17812 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17813 return unify_success (explain_p
);
17816 case TEMPLATE_DECL
:
17817 /* Matched cases are handled by the ARG == PARM test above. */
17818 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17821 /* A non-type template parameter that is a variable should be a
17822 an integral constant, in which case, it whould have been
17823 folded into its (constant) value. So we should not be getting
17824 a variable here. */
17825 gcc_unreachable ();
17827 case TYPE_ARGUMENT_PACK
:
17828 case NONTYPE_ARGUMENT_PACK
:
17829 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17830 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17833 case DECLTYPE_TYPE
:
17834 case UNDERLYING_TYPE
:
17835 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17836 or UNDERLYING_TYPE nodes. */
17837 return unify_success (explain_p
);
17840 /* Unification fails if we hit an error node. */
17841 return unify_invalid (explain_p
);
17844 /* An unresolved overload is a nondeduced context. */
17845 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17846 return unify_success (explain_p
);
17847 gcc_assert (EXPR_P (parm
));
17849 /* We must be looking at an expression. This can happen with
17853 void foo(S<I>, S<I + 2>);
17855 This is a "nondeduced context":
17859 The nondeduced contexts are:
17861 --A type that is a template-id in which one or more of
17862 the template-arguments is an expression that references
17863 a template-parameter.
17865 In these cases, we assume deduction succeeded, but don't
17866 actually infer any unifications. */
17868 if (!uses_template_parms (parm
)
17869 && !template_args_equal (parm
, arg
))
17870 return unify_expression_unequal (explain_p
, parm
, arg
);
17872 return unify_success (explain_p
);
17875 #undef RECUR_AND_CHECK_FAILURE
17877 /* Note that DECL can be defined in this translation unit, if
17881 mark_definable (tree decl
)
17884 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17885 FOR_EACH_CLONE (clone
, decl
)
17886 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17889 /* Called if RESULT is explicitly instantiated, or is a member of an
17890 explicitly instantiated class. */
17893 mark_decl_instantiated (tree result
, int extern_p
)
17895 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17897 /* If this entity has already been written out, it's too late to
17898 make any modifications. */
17899 if (TREE_ASM_WRITTEN (result
))
17902 /* For anonymous namespace we don't need to do anything. */
17903 if (decl_anon_ns_mem_p (result
))
17905 gcc_assert (!TREE_PUBLIC (result
));
17909 if (TREE_CODE (result
) != FUNCTION_DECL
)
17910 /* The TREE_PUBLIC flag for function declarations will have been
17911 set correctly by tsubst. */
17912 TREE_PUBLIC (result
) = 1;
17914 /* This might have been set by an earlier implicit instantiation. */
17915 DECL_COMDAT (result
) = 0;
17918 DECL_NOT_REALLY_EXTERN (result
) = 0;
17921 mark_definable (result
);
17922 /* Always make artificials weak. */
17923 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17924 comdat_linkage (result
);
17925 /* For WIN32 we also want to put explicit instantiations in
17926 linkonce sections. */
17927 else if (TREE_PUBLIC (result
))
17928 maybe_make_one_only (result
);
17931 /* If EXTERN_P, then this function will not be emitted -- unless
17932 followed by an explicit instantiation, at which point its linkage
17933 will be adjusted. If !EXTERN_P, then this function will be
17934 emitted here. In neither circumstance do we want
17935 import_export_decl to adjust the linkage. */
17936 DECL_INTERFACE_KNOWN (result
) = 1;
17939 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17940 important template arguments. If any are missing, we check whether
17941 they're important by using error_mark_node for substituting into any
17942 args that were used for partial ordering (the ones between ARGS and END)
17943 and seeing if it bubbles up. */
17946 check_undeduced_parms (tree targs
, tree args
, tree end
)
17948 bool found
= false;
17950 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17951 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17954 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17958 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17959 if (substed
== error_mark_node
)
17965 /* Given two function templates PAT1 and PAT2, return:
17967 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17968 -1 if PAT2 is more specialized than PAT1.
17969 0 if neither is more specialized.
17971 LEN indicates the number of parameters we should consider
17972 (defaulted parameters should not be considered).
17974 The 1998 std underspecified function template partial ordering, and
17975 DR214 addresses the issue. We take pairs of arguments, one from
17976 each of the templates, and deduce them against each other. One of
17977 the templates will be more specialized if all the *other*
17978 template's arguments deduce against its arguments and at least one
17979 of its arguments *does* *not* deduce against the other template's
17980 corresponding argument. Deduction is done as for class templates.
17981 The arguments used in deduction have reference and top level cv
17982 qualifiers removed. Iff both arguments were originally reference
17983 types *and* deduction succeeds in both directions, an lvalue reference
17984 wins against an rvalue reference and otherwise the template
17985 with the more cv-qualified argument wins for that pairing (if
17986 neither is more cv-qualified, they both are equal). Unlike regular
17987 deduction, after all the arguments have been deduced in this way,
17988 we do *not* verify the deduced template argument values can be
17989 substituted into non-deduced contexts.
17991 The logic can be a bit confusing here, because we look at deduce1 and
17992 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17993 can find template arguments for pat1 to make arg1 look like arg2, that
17994 means that arg2 is at least as specialized as arg1. */
17997 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17999 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18000 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18001 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18002 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18003 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18004 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18005 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18006 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18007 tree origs1
, origs2
;
18008 bool lose1
= false;
18009 bool lose2
= false;
18011 /* Remove the this parameter from non-static member functions. If
18012 one is a non-static member function and the other is not a static
18013 member function, remove the first parameter from that function
18014 also. This situation occurs for operator functions where we
18015 locate both a member function (with this pointer) and non-member
18016 operator (with explicit first operand). */
18017 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18019 len
--; /* LEN is the number of significant arguments for DECL1 */
18020 args1
= TREE_CHAIN (args1
);
18021 if (!DECL_STATIC_FUNCTION_P (decl2
))
18022 args2
= TREE_CHAIN (args2
);
18024 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18026 args2
= TREE_CHAIN (args2
);
18027 if (!DECL_STATIC_FUNCTION_P (decl1
))
18030 args1
= TREE_CHAIN (args1
);
18034 /* If only one is a conversion operator, they are unordered. */
18035 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18038 /* Consider the return type for a conversion function */
18039 if (DECL_CONV_FN_P (decl1
))
18041 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18042 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18046 processing_template_decl
++;
18052 /* Stop when an ellipsis is seen. */
18053 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18055 tree arg1
= TREE_VALUE (args1
);
18056 tree arg2
= TREE_VALUE (args2
);
18057 int deduce1
, deduce2
;
18063 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18064 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18066 /* When both arguments are pack expansions, we need only
18067 unify the patterns themselves. */
18068 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18069 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18071 /* This is the last comparison we need to do. */
18075 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18077 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18078 arg1
= TREE_TYPE (arg1
);
18079 quals1
= cp_type_quals (arg1
);
18082 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18084 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18085 arg2
= TREE_TYPE (arg2
);
18086 quals2
= cp_type_quals (arg2
);
18089 arg1
= TYPE_MAIN_VARIANT (arg1
);
18090 arg2
= TYPE_MAIN_VARIANT (arg2
);
18092 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18094 int i
, len2
= list_length (args2
);
18095 tree parmvec
= make_tree_vec (1);
18096 tree argvec
= make_tree_vec (len2
);
18099 /* Setup the parameter vector, which contains only ARG1. */
18100 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18102 /* Setup the argument vector, which contains the remaining
18104 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18105 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18107 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18108 argvec
, DEDUCE_EXACT
,
18109 /*subr=*/true, /*explain_p=*/false)
18112 /* We cannot deduce in the other direction, because ARG1 is
18113 a pack expansion but ARG2 is not. */
18116 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18118 int i
, len1
= list_length (args1
);
18119 tree parmvec
= make_tree_vec (1);
18120 tree argvec
= make_tree_vec (len1
);
18123 /* Setup the parameter vector, which contains only ARG1. */
18124 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18126 /* Setup the argument vector, which contains the remaining
18128 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18129 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18131 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18132 argvec
, DEDUCE_EXACT
,
18133 /*subr=*/true, /*explain_p=*/false)
18136 /* We cannot deduce in the other direction, because ARG2 is
18137 a pack expansion but ARG1 is not.*/
18143 /* The normal case, where neither argument is a pack
18145 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18146 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18148 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18149 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18153 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18154 arg2, then arg2 is not as specialized as arg1. */
18160 /* "If, for a given type, deduction succeeds in both directions
18161 (i.e., the types are identical after the transformations above)
18162 and both P and A were reference types (before being replaced with
18163 the type referred to above):
18164 - if the type from the argument template was an lvalue reference and
18165 the type from the parameter template was not, the argument type is
18166 considered to be more specialized than the other; otherwise,
18167 - if the type from the argument template is more cv-qualified
18168 than the type from the parameter template (as described above),
18169 the argument type is considered to be more specialized than the other;
18171 - neither type is more specialized than the other." */
18173 if (deduce1
&& deduce2
)
18175 if (ref1
&& ref2
&& ref1
!= ref2
)
18182 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18184 if ((quals1
& quals2
) == quals2
)
18186 if ((quals1
& quals2
) == quals1
)
18191 if (lose1
&& lose2
)
18192 /* We've failed to deduce something in either direction.
18193 These must be unordered. */
18196 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18197 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18198 /* We have already processed all of the arguments in our
18199 handing of the pack expansion type. */
18202 args1
= TREE_CHAIN (args1
);
18203 args2
= TREE_CHAIN (args2
);
18206 /* "In most cases, all template parameters must have values in order for
18207 deduction to succeed, but for partial ordering purposes a template
18208 parameter may remain without a value provided it is not used in the
18209 types being used for partial ordering."
18211 Thus, if we are missing any of the targs1 we need to substitute into
18212 origs1, then pat2 is not as specialized as pat1. This can happen when
18213 there is a nondeduced context. */
18214 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18216 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18219 processing_template_decl
--;
18221 /* All things being equal, if the next argument is a pack expansion
18222 for one function but not for the other, prefer the
18223 non-variadic function. FIXME this is bogus; see c++/41958. */
18225 && args1
&& TREE_VALUE (args1
)
18226 && args2
&& TREE_VALUE (args2
))
18228 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18229 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18232 if (lose1
== lose2
)
18240 /* Determine which of two partial specializations of MAIN_TMPL is more
18243 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18244 to the first partial specialization. The TREE_VALUE is the
18245 innermost set of template parameters for the partial
18246 specialization. PAT2 is similar, but for the second template.
18248 Return 1 if the first partial specialization is more specialized;
18249 -1 if the second is more specialized; 0 if neither is more
18252 See [temp.class.order] for information about determining which of
18253 two templates is more specialized. */
18256 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
18261 bool any_deductions
= false;
18263 tmpl1
= TREE_TYPE (pat1
);
18264 tmpl2
= TREE_TYPE (pat2
);
18266 /* Just like what happens for functions, if we are ordering between
18267 different class template specializations, we may encounter dependent
18268 types in the arguments, and we need our dependency check functions
18269 to behave correctly. */
18270 ++processing_template_decl
;
18271 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
18272 CLASSTYPE_TI_ARGS (tmpl1
),
18273 CLASSTYPE_TI_ARGS (tmpl2
));
18277 any_deductions
= true;
18280 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
18281 CLASSTYPE_TI_ARGS (tmpl2
),
18282 CLASSTYPE_TI_ARGS (tmpl1
));
18286 any_deductions
= true;
18288 --processing_template_decl
;
18290 /* In the case of a tie where at least one of the class templates
18291 has a parameter pack at the end, the template with the most
18292 non-packed parameters wins. */
18295 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18296 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18298 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18299 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18300 int len1
= TREE_VEC_LENGTH (args1
);
18301 int len2
= TREE_VEC_LENGTH (args2
);
18303 /* We don't count the pack expansion at the end. */
18304 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18306 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18311 else if (len1
< len2
)
18318 /* Return the template arguments that will produce the function signature
18319 DECL from the function template FN, with the explicit template
18320 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18321 also match. Return NULL_TREE if no satisfactory arguments could be
18325 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18327 int ntparms
= DECL_NTPARMS (fn
);
18328 tree targs
= make_tree_vec (ntparms
);
18329 tree decl_type
= TREE_TYPE (decl
);
18330 tree decl_arg_types
;
18332 unsigned int nargs
, ix
;
18335 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18337 /* Never do unification on the 'this' parameter. */
18338 decl_arg_types
= skip_artificial_parms_for (decl
,
18339 TYPE_ARG_TYPES (decl_type
));
18341 nargs
= list_length (decl_arg_types
);
18342 args
= XALLOCAVEC (tree
, nargs
);
18343 for (arg
= decl_arg_types
, ix
= 0;
18344 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18345 arg
= TREE_CHAIN (arg
), ++ix
)
18346 args
[ix
] = TREE_VALUE (arg
);
18348 if (fn_type_unification (fn
, explicit_args
, targs
,
18350 (check_rettype
|| DECL_CONV_FN_P (fn
)
18351 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18352 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18354 == error_mark_node
)
18360 /* Return the innermost template arguments that, when applied to a partial
18361 specialization of MAIN_TMPL whose innermost template parameters are
18362 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18365 For example, suppose we have:
18367 template <class T, class U> struct S {};
18368 template <class T> struct S<T*, int> {};
18370 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18371 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18372 int}. The resulting vector will be {double}, indicating that `T'
18373 is bound to `double'. */
18376 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
18378 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18380 tree innermost_deduced_args
;
18382 innermost_deduced_args
= make_tree_vec (ntparms
);
18383 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18385 deduced_args
= copy_node (args
);
18386 SET_TMPL_ARGS_LEVEL (deduced_args
,
18387 TMPL_ARGS_DEPTH (deduced_args
),
18388 innermost_deduced_args
);
18391 deduced_args
= innermost_deduced_args
;
18393 if (unify (tparms
, deduced_args
,
18394 INNERMOST_TEMPLATE_ARGS (spec_args
),
18395 INNERMOST_TEMPLATE_ARGS (args
),
18396 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18399 for (i
= 0; i
< ntparms
; ++i
)
18400 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18403 /* Verify that nondeduced template arguments agree with the type
18404 obtained from argument deduction.
18408 struct A { typedef int X; };
18409 template <class T, class U> struct C {};
18410 template <class T> struct C<T, typename T::X> {};
18412 Then with the instantiation `C<A, int>', we can deduce that
18413 `T' is `A' but unify () does not check whether `typename T::X'
18415 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18416 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
18417 spec_args
, main_tmpl
,
18418 tf_none
, false, false);
18419 if (spec_args
== error_mark_node
18420 /* We only need to check the innermost arguments; the other
18421 arguments will always agree. */
18422 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18423 INNERMOST_TEMPLATE_ARGS (args
)))
18426 /* Now that we have bindings for all of the template arguments,
18427 ensure that the arguments deduced for the template template
18428 parameters have compatible template parameter lists. See the use
18429 of template_template_parm_bindings_ok_p in fn_type_unification
18430 for more information. */
18431 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18434 return deduced_args
;
18437 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18438 Return the TREE_LIST node with the most specialized template, if
18439 any. If there is no most specialized template, the error_mark_node
18442 Note that this function does not look at, or modify, the
18443 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18444 returned is one of the elements of INSTANTIATIONS, callers may
18445 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18446 and retrieve it from the value returned. */
18449 most_specialized_instantiation (tree templates
)
18453 ++processing_template_decl
;
18456 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18460 if (get_bindings (TREE_VALUE (champ
),
18461 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18462 NULL_TREE
, /*check_ret=*/true))
18465 if (get_bindings (TREE_VALUE (fn
),
18466 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18467 NULL_TREE
, /*check_ret=*/true))
18474 /* Equally specialized, move to next function. If there
18475 is no next function, nothing's most specialized. */
18476 fn
= TREE_CHAIN (fn
);
18484 /* Now verify that champ is better than everything earlier in the
18485 instantiation list. */
18486 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18487 if (get_bindings (TREE_VALUE (champ
),
18488 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18489 NULL_TREE
, /*check_ret=*/true)
18490 || !get_bindings (TREE_VALUE (fn
),
18491 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18492 NULL_TREE
, /*check_ret=*/true))
18498 processing_template_decl
--;
18501 return error_mark_node
;
18506 /* If DECL is a specialization of some template, return the most
18507 general such template. Otherwise, returns NULL_TREE.
18509 For example, given:
18511 template <class T> struct S { template <class U> void f(U); };
18513 if TMPL is `template <class U> void S<int>::f(U)' this will return
18514 the full template. This function will not trace past partial
18515 specializations, however. For example, given in addition:
18517 template <class T> struct S<T*> { template <class U> void f(U); };
18519 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18520 `template <class T> template <class U> S<T*>::f(U)'. */
18523 most_general_template (tree decl
)
18525 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18526 an immediate specialization. */
18527 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18529 if (DECL_TEMPLATE_INFO (decl
)) {
18530 decl
= DECL_TI_TEMPLATE (decl
);
18532 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18533 template friend. */
18534 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18540 /* Look for more and more general templates. */
18541 while (DECL_TEMPLATE_INFO (decl
))
18543 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18544 (See cp-tree.h for details.) */
18545 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18548 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18549 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18552 /* Stop if we run into an explicitly specialized class template. */
18553 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18554 && DECL_CONTEXT (decl
)
18555 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18558 decl
= DECL_TI_TEMPLATE (decl
);
18564 /* Return the most specialized of the class template partial
18565 specializations of TMPL which can produce TYPE, a specialization of
18566 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18567 a _TYPE node corresponding to the partial specialization, while the
18568 TREE_PURPOSE is the set of template arguments that must be
18569 substituted into the TREE_TYPE in order to generate TYPE.
18571 If the choice of partial specialization is ambiguous, a diagnostic
18572 is issued, and the error_mark_node is returned. If there are no
18573 partial specializations of TMPL matching TYPE, then NULL_TREE is
18577 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
18579 tree list
= NULL_TREE
;
18585 tree outer_args
= NULL_TREE
;
18587 tmpl
= most_general_template (tmpl
);
18588 args
= CLASSTYPE_TI_ARGS (type
);
18590 /* For determining which partial specialization to use, only the
18591 innermost args are interesting. */
18592 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18594 outer_args
= strip_innermost_template_args (args
, 1);
18595 args
= INNERMOST_TEMPLATE_ARGS (args
);
18598 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
18600 tree partial_spec_args
;
18602 tree spec_tmpl
= TREE_VALUE (t
);
18603 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18605 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18607 ++processing_template_decl
;
18611 /* Discard the outer levels of args, and then substitute in the
18612 template args from the enclosing class. */
18613 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18614 partial_spec_args
= tsubst_template_args
18615 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18617 /* And the same for the partial specialization TEMPLATE_DECL. */
18618 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18621 partial_spec_args
=
18622 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18623 add_to_template_args (outer_args
,
18624 partial_spec_args
),
18626 /*require_all_args=*/true,
18627 /*use_default_args=*/true);
18629 --processing_template_decl
;
18631 if (partial_spec_args
== error_mark_node
)
18632 return error_mark_node
;
18633 if (spec_tmpl
== error_mark_node
)
18634 return error_mark_node
;
18636 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18637 spec_args
= get_class_bindings (tmpl
, parms
,
18643 spec_args
= add_to_template_args (outer_args
, spec_args
);
18644 list
= tree_cons (spec_args
, orig_parms
, list
);
18645 TREE_TYPE (list
) = TREE_TYPE (t
);
18652 ambiguous_p
= false;
18655 t
= TREE_CHAIN (t
);
18656 for (; t
; t
= TREE_CHAIN (t
))
18658 fate
= more_specialized_class (tmpl
, champ
, t
);
18665 t
= TREE_CHAIN (t
);
18668 ambiguous_p
= true;
18677 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18679 fate
= more_specialized_class (tmpl
, champ
, t
);
18682 ambiguous_p
= true;
18690 char *spaces
= NULL
;
18691 if (!(complain
& tf_error
))
18692 return error_mark_node
;
18693 error ("ambiguous class template instantiation for %q#T", type
);
18694 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18695 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18697 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18698 spaces
= spaces
? spaces
: get_spaces (str
);
18701 return error_mark_node
;
18707 /* Explicitly instantiate DECL. */
18710 do_decl_instantiation (tree decl
, tree storage
)
18712 tree result
= NULL_TREE
;
18715 if (!decl
|| decl
== error_mark_node
)
18716 /* An error occurred, for which grokdeclarator has already issued
18717 an appropriate message. */
18719 else if (! DECL_LANG_SPECIFIC (decl
))
18721 error ("explicit instantiation of non-template %q#D", decl
);
18724 else if (VAR_P (decl
))
18726 /* There is an asymmetry here in the way VAR_DECLs and
18727 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18728 the latter, the DECL we get back will be marked as a
18729 template instantiation, and the appropriate
18730 DECL_TEMPLATE_INFO will be set up. This does not happen for
18731 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18732 should handle VAR_DECLs as it currently handles
18734 if (!DECL_CLASS_SCOPE_P (decl
))
18736 error ("%qD is not a static data member of a class template", decl
);
18739 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18740 if (!result
|| !VAR_P (result
))
18742 error ("no matching template for %qD found", decl
);
18745 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18747 error ("type %qT for explicit instantiation %qD does not match "
18748 "declared type %qT", TREE_TYPE (result
), decl
,
18753 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18755 error ("explicit instantiation of %q#D", decl
);
18761 /* Check for various error cases. Note that if the explicit
18762 instantiation is valid the RESULT will currently be marked as an
18763 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18764 until we get here. */
18766 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18768 /* DR 259 [temp.spec].
18770 Both an explicit instantiation and a declaration of an explicit
18771 specialization shall not appear in a program unless the explicit
18772 instantiation follows a declaration of the explicit specialization.
18774 For a given set of template parameters, if an explicit
18775 instantiation of a template appears after a declaration of an
18776 explicit specialization for that template, the explicit
18777 instantiation has no effect. */
18780 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18784 No program shall explicitly instantiate any template more
18787 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18788 the first instantiation was `extern' and the second is not,
18789 and EXTERN_P for the opposite case. */
18790 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18791 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18792 /* If an "extern" explicit instantiation follows an ordinary
18793 explicit instantiation, the template is instantiated. */
18797 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18799 error ("no matching template for %qD found", result
);
18802 else if (!DECL_TEMPLATE_INFO (result
))
18804 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18808 if (storage
== NULL_TREE
)
18810 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18812 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
18813 pedwarn (input_location
, OPT_Wpedantic
,
18814 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18819 error ("storage class %qD applied to template instantiation", storage
);
18821 check_explicit_instantiation_namespace (result
);
18822 mark_decl_instantiated (result
, extern_p
);
18824 instantiate_decl (result
, /*defer_ok=*/1,
18825 /*expl_inst_class_mem_p=*/false);
18829 mark_class_instantiated (tree t
, int extern_p
)
18831 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18832 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18833 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18834 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18837 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18838 rest_of_type_compilation (t
, 1);
18842 /* Called from do_type_instantiation through binding_table_foreach to
18843 do recursive instantiation for the type bound in ENTRY. */
18845 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18847 tree storage
= *(tree
*) data
;
18849 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18850 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18851 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18854 /* Called from do_type_instantiation to instantiate a member
18855 (a member function or a static member variable) of an
18856 explicitly instantiated class template. */
18858 instantiate_class_member (tree decl
, int extern_p
)
18860 mark_decl_instantiated (decl
, extern_p
);
18862 instantiate_decl (decl
, /*defer_ok=*/1,
18863 /*expl_inst_class_mem_p=*/true);
18866 /* Perform an explicit instantiation of template class T. STORAGE, if
18867 non-null, is the RID for extern, inline or static. COMPLAIN is
18868 nonzero if this is called from the parser, zero if called recursively,
18869 since the standard is unclear (as detailed below). */
18872 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18877 int previous_instantiation_extern_p
= 0;
18879 if (TREE_CODE (t
) == TYPE_DECL
)
18882 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18885 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18887 error ("explicit instantiation of non-class template %qD", tmpl
);
18889 error ("explicit instantiation of non-template type %qT", t
);
18895 if (!COMPLETE_TYPE_P (t
))
18897 if (complain
& tf_error
)
18898 error ("explicit instantiation of %q#T before definition of template",
18903 if (storage
!= NULL_TREE
)
18905 if (!in_system_header_at (input_location
))
18907 if (storage
== ridpointers
[(int) RID_EXTERN
])
18909 if (cxx_dialect
== cxx98
)
18910 pedwarn (input_location
, OPT_Wpedantic
,
18911 "ISO C++ 1998 forbids the use of %<extern%> on "
18912 "explicit instantiations");
18915 pedwarn (input_location
, OPT_Wpedantic
,
18916 "ISO C++ forbids the use of %qE"
18917 " on explicit instantiations", storage
);
18920 if (storage
== ridpointers
[(int) RID_INLINE
])
18922 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18924 else if (storage
== ridpointers
[(int) RID_STATIC
])
18928 error ("storage class %qD applied to template instantiation",
18934 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18936 /* DR 259 [temp.spec].
18938 Both an explicit instantiation and a declaration of an explicit
18939 specialization shall not appear in a program unless the explicit
18940 instantiation follows a declaration of the explicit specialization.
18942 For a given set of template parameters, if an explicit
18943 instantiation of a template appears after a declaration of an
18944 explicit specialization for that template, the explicit
18945 instantiation has no effect. */
18948 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18952 No program shall explicitly instantiate any template more
18955 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18956 instantiation was `extern'. If EXTERN_P then the second is.
18957 These cases are OK. */
18958 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18960 if (!previous_instantiation_extern_p
&& !extern_p
18961 && (complain
& tf_error
))
18962 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18964 /* If we've already instantiated the template, just return now. */
18965 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18969 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18970 mark_class_instantiated (t
, extern_p
);
18978 /* In contrast to implicit instantiation, where only the
18979 declarations, and not the definitions, of members are
18980 instantiated, we have here:
18984 The explicit instantiation of a class template specialization
18985 implies the instantiation of all of its members not
18986 previously explicitly specialized in the translation unit
18987 containing the explicit instantiation.
18989 Of course, we can't instantiate member template classes, since
18990 we don't have any arguments for them. Note that the standard
18991 is unclear on whether the instantiation of the members are
18992 *explicit* instantiations or not. However, the most natural
18993 interpretation is that it should be an explicit instantiation. */
18996 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18997 if (TREE_CODE (tmp
) == FUNCTION_DECL
18998 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18999 instantiate_class_member (tmp
, extern_p
);
19001 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19002 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19003 instantiate_class_member (tmp
, extern_p
);
19005 if (CLASSTYPE_NESTED_UTDS (t
))
19006 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19007 bt_instantiate_type_proc
, &storage
);
19011 /* Given a function DECL, which is a specialization of TMPL, modify
19012 DECL to be a re-instantiation of TMPL with the same template
19013 arguments. TMPL should be the template into which tsubst'ing
19014 should occur for DECL, not the most general template.
19016 One reason for doing this is a scenario like this:
19019 void f(const T&, int i);
19021 void g() { f(3, 7); }
19024 void f(const T& t, const int i) { }
19026 Note that when the template is first instantiated, with
19027 instantiate_template, the resulting DECL will have no name for the
19028 first parameter, and the wrong type for the second. So, when we go
19029 to instantiate the DECL, we regenerate it. */
19032 regenerate_decl_from_template (tree decl
, tree tmpl
)
19034 /* The arguments used to instantiate DECL, from the most general
19039 args
= DECL_TI_ARGS (decl
);
19040 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19042 /* Make sure that we can see identifiers, and compute access
19044 push_access_scope (decl
);
19046 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19054 args_depth
= TMPL_ARGS_DEPTH (args
);
19055 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19056 if (args_depth
> parms_depth
)
19057 args
= get_innermost_template_args (args
, parms_depth
);
19059 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19060 args
, tf_error
, NULL_TREE
,
19061 /*defer_ok*/false);
19062 if (specs
&& specs
!= error_mark_node
)
19063 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19066 /* Merge parameter declarations. */
19067 decl_parm
= skip_artificial_parms_for (decl
,
19068 DECL_ARGUMENTS (decl
));
19070 = skip_artificial_parms_for (code_pattern
,
19071 DECL_ARGUMENTS (code_pattern
));
19072 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19077 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19078 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19079 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19081 parm_type
= type_decays_to (parm_type
);
19082 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19083 TREE_TYPE (decl_parm
) = parm_type
;
19084 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19085 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19087 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19088 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19090 decl_parm
= DECL_CHAIN (decl_parm
);
19091 pattern_parm
= DECL_CHAIN (pattern_parm
);
19093 /* Merge any parameters that match with the function parameter
19095 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19098 tree expanded_types
;
19099 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19100 the parameters in this function parameter pack. */
19101 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19102 args
, tf_error
, NULL_TREE
);
19103 len
= TREE_VEC_LENGTH (expanded_types
);
19104 for (i
= 0; i
< len
; i
++)
19109 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19110 /* Rename the parameter to include the index. */
19111 DECL_NAME (decl_parm
) =
19112 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19113 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19114 parm_type
= type_decays_to (parm_type
);
19115 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19116 TREE_TYPE (decl_parm
) = parm_type
;
19117 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19118 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19120 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19121 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19123 decl_parm
= DECL_CHAIN (decl_parm
);
19126 /* Merge additional specifiers from the CODE_PATTERN. */
19127 if (DECL_DECLARED_INLINE_P (code_pattern
)
19128 && !DECL_DECLARED_INLINE_P (decl
))
19129 DECL_DECLARED_INLINE_P (decl
) = 1;
19131 else if (VAR_P (decl
))
19133 DECL_INITIAL (decl
) =
19134 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19135 tf_error
, DECL_TI_TEMPLATE (decl
),
19136 /*integral_constant_expression_p=*/false);
19137 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19138 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19139 tf_error
, DECL_TI_TEMPLATE (decl
));
19142 gcc_unreachable ();
19144 pop_access_scope (decl
);
19147 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19148 substituted to get DECL. */
19151 template_for_substitution (tree decl
)
19153 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19155 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19156 for the instantiation. This is not always the most general
19157 template. Consider, for example:
19160 struct S { template <class U> void f();
19161 template <> void f<int>(); };
19163 and an instantiation of S<double>::f<int>. We want TD to be the
19164 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19165 while (/* An instantiation cannot have a definition, so we need a
19166 more general template. */
19167 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19168 /* We must also deal with friend templates. Given:
19170 template <class T> struct S {
19171 template <class U> friend void f() {};
19174 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19175 so far as the language is concerned, but that's still
19176 where we get the pattern for the instantiation from. On
19177 other hand, if the definition comes outside the class, say:
19179 template <class T> struct S {
19180 template <class U> friend void f();
19182 template <class U> friend void f() {}
19184 we don't need to look any further. That's what the check for
19185 DECL_INITIAL is for. */
19186 || (TREE_CODE (decl
) == FUNCTION_DECL
19187 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19188 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19190 /* The present template, TD, should not be a definition. If it
19191 were a definition, we should be using it! Note that we
19192 cannot restructure the loop to just keep going until we find
19193 a template with a definition, since that might go too far if
19194 a specialization was declared, but not defined. */
19195 gcc_assert (!VAR_P (decl
)
19196 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19198 /* Fetch the more general template. */
19199 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19205 /* Returns true if we need to instantiate this template instance even if we
19206 know we aren't going to emit it.. */
19209 always_instantiate_p (tree decl
)
19211 /* We always instantiate inline functions so that we can inline them. An
19212 explicit instantiation declaration prohibits implicit instantiation of
19213 non-inline functions. With high levels of optimization, we would
19214 normally inline non-inline functions -- but we're not allowed to do
19215 that for "extern template" functions. Therefore, we check
19216 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19217 return ((TREE_CODE (decl
) == FUNCTION_DECL
19218 && (DECL_DECLARED_INLINE_P (decl
)
19219 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19220 /* And we need to instantiate static data members so that
19221 their initializers are available in integral constant
19224 && decl_maybe_constant_var_p (decl
)));
19227 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19228 instantiate it now, modifying TREE_TYPE (fn). */
19231 maybe_instantiate_noexcept (tree fn
)
19233 tree fntype
, spec
, noex
, clone
;
19235 if (DECL_CLONED_FUNCTION_P (fn
))
19236 fn
= DECL_CLONED_FUNCTION (fn
);
19237 fntype
= TREE_TYPE (fn
);
19238 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19240 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19243 noex
= TREE_PURPOSE (spec
);
19245 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19247 if (push_tinst_level (fn
))
19249 push_access_scope (fn
);
19250 push_deferring_access_checks (dk_no_deferred
);
19251 input_location
= DECL_SOURCE_LOCATION (fn
);
19252 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19253 DEFERRED_NOEXCEPT_ARGS (noex
),
19254 tf_warning_or_error
, fn
,
19255 /*function_p=*/false,
19256 /*integral_constant_expression_p=*/true);
19257 pop_deferring_access_checks ();
19258 pop_access_scope (fn
);
19259 pop_tinst_level ();
19260 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19261 if (spec
== error_mark_node
)
19262 spec
= noexcept_false_spec
;
19265 spec
= noexcept_false_spec
;
19269 /* This is an implicitly declared function, so NOEX is a list of
19270 other functions to evaluate and merge. */
19272 spec
= noexcept_true_spec
;
19273 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19275 tree fn
= OVL_CURRENT (elt
);
19277 maybe_instantiate_noexcept (fn
);
19278 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19279 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19283 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19285 FOR_EACH_CLONE (clone
, fn
)
19287 if (TREE_TYPE (clone
) == fntype
)
19288 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19290 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19294 /* Produce the definition of D, a _DECL generated from a template. If
19295 DEFER_OK is nonzero, then we don't have to actually do the
19296 instantiation now; we just have to do it sometime. Normally it is
19297 an error if this is an explicit instantiation but D is undefined.
19298 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19299 explicitly instantiated class template. */
19302 instantiate_decl (tree d
, int defer_ok
,
19303 bool expl_inst_class_mem_p
)
19305 tree tmpl
= DECL_TI_TEMPLATE (d
);
19312 bool pattern_defined
;
19313 location_t saved_loc
= input_location
;
19314 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19315 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19320 /* This function should only be used to instantiate templates for
19321 functions and static member variables. */
19322 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19324 /* Variables are never deferred; if instantiation is required, they
19325 are instantiated right away. That allows for better code in the
19326 case that an expression refers to the value of the variable --
19327 if the variable has a constant value the referring expression can
19328 take advantage of that fact. */
19330 || DECL_DECLARED_CONSTEXPR_P (d
))
19333 /* Don't instantiate cloned functions. Instead, instantiate the
19334 functions they cloned. */
19335 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19336 d
= DECL_CLONED_FUNCTION (d
);
19338 if (DECL_TEMPLATE_INSTANTIATED (d
)
19339 || (TREE_CODE (d
) == FUNCTION_DECL
19340 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19341 || DECL_TEMPLATE_SPECIALIZATION (d
))
19342 /* D has already been instantiated or explicitly specialized, so
19343 there's nothing for us to do here.
19345 It might seem reasonable to check whether or not D is an explicit
19346 instantiation, and, if so, stop here. But when an explicit
19347 instantiation is deferred until the end of the compilation,
19348 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19349 the instantiation. */
19352 /* Check to see whether we know that this template will be
19353 instantiated in some other file, as with "extern template"
19355 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19357 /* In general, we do not instantiate such templates. */
19358 if (external_p
&& !always_instantiate_p (d
))
19361 gen_tmpl
= most_general_template (tmpl
);
19362 gen_args
= DECL_TI_ARGS (d
);
19364 if (tmpl
!= gen_tmpl
)
19365 /* We should already have the extra args. */
19366 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19367 == TMPL_ARGS_DEPTH (gen_args
));
19368 /* And what's in the hash table should match D. */
19369 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19370 || spec
== NULL_TREE
);
19372 /* This needs to happen before any tsubsting. */
19373 if (! push_tinst_level (d
))
19376 timevar_push (TV_TEMPLATE_INST
);
19378 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19379 for the instantiation. */
19380 td
= template_for_substitution (d
);
19381 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19383 /* We should never be trying to instantiate a member of a class
19384 template or partial specialization. */
19385 gcc_assert (d
!= code_pattern
);
19387 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19388 || DECL_TEMPLATE_SPECIALIZATION (td
))
19389 /* In the case of a friend template whose definition is provided
19390 outside the class, we may have too many arguments. Drop the
19391 ones we don't need. The same is true for specializations. */
19392 args
= get_innermost_template_args
19393 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19397 if (TREE_CODE (d
) == FUNCTION_DECL
)
19398 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19399 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19401 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19403 /* We may be in the middle of deferred access check. Disable it now. */
19404 push_deferring_access_checks (dk_no_deferred
);
19406 /* Unless an explicit instantiation directive has already determined
19407 the linkage of D, remember that a definition is available for
19409 if (pattern_defined
19410 && !DECL_INTERFACE_KNOWN (d
)
19411 && !DECL_NOT_REALLY_EXTERN (d
))
19412 mark_definable (d
);
19414 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19415 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19416 input_location
= DECL_SOURCE_LOCATION (d
);
19418 /* If D is a member of an explicitly instantiated class template,
19419 and no definition is available, treat it like an implicit
19421 if (!pattern_defined
&& expl_inst_class_mem_p
19422 && DECL_EXPLICIT_INSTANTIATION (d
))
19424 /* Leave linkage flags alone on instantiations with anonymous
19426 if (TREE_PUBLIC (d
))
19428 DECL_NOT_REALLY_EXTERN (d
) = 0;
19429 DECL_INTERFACE_KNOWN (d
) = 0;
19431 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19434 if (TREE_CODE (d
) == FUNCTION_DECL
)
19435 maybe_instantiate_noexcept (d
);
19437 /* Defer all other templates, unless we have been explicitly
19438 forbidden from doing so. */
19439 if (/* If there is no definition, we cannot instantiate the
19442 /* If it's OK to postpone instantiation, do so. */
19444 /* If this is a static data member that will be defined
19445 elsewhere, we don't want to instantiate the entire data
19446 member, but we do want to instantiate the initializer so that
19447 we can substitute that elsewhere. */
19448 || (external_p
&& VAR_P (d
)))
19450 /* The definition of the static data member is now required so
19451 we must substitute the initializer. */
19453 && !DECL_INITIAL (d
)
19454 && DECL_INITIAL (code_pattern
))
19458 bool const_init
= false;
19460 ns
= decl_namespace_context (d
);
19461 push_nested_namespace (ns
);
19462 push_nested_class (DECL_CONTEXT (d
));
19463 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19465 tf_warning_or_error
, NULL_TREE
,
19466 /*integral_constant_expression_p=*/false);
19467 /* Make sure the initializer is still constant, in case of
19468 circular dependency (template/instantiate6.C). */
19470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19471 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19472 /*asmspec_tree=*/NULL_TREE
,
19473 LOOKUP_ONLYCONVERTING
);
19474 pop_nested_class ();
19475 pop_nested_namespace (ns
);
19478 /* We restore the source position here because it's used by
19479 add_pending_template. */
19480 input_location
= saved_loc
;
19482 if (at_eof
&& !pattern_defined
19483 && DECL_EXPLICIT_INSTANTIATION (d
)
19484 && DECL_NOT_REALLY_EXTERN (d
))
19487 The definition of a non-exported function template, a
19488 non-exported member function template, or a non-exported
19489 member function or static data member of a class template
19490 shall be present in every translation unit in which it is
19491 explicitly instantiated. */
19492 permerror (input_location
, "explicit instantiation of %qD "
19493 "but no definition available", d
);
19495 /* If we're in unevaluated context, we just wanted to get the
19496 constant value; this isn't an odr use, so don't queue
19497 a full instantiation. */
19498 if (cp_unevaluated_operand
!= 0)
19500 /* ??? Historically, we have instantiated inline functions, even
19501 when marked as "extern template". */
19502 if (!(external_p
&& VAR_P (d
)))
19503 add_pending_template (d
);
19506 /* Tell the repository that D is available in this translation unit
19507 -- and see if it is supposed to be instantiated here. */
19508 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19510 /* In a PCH file, despite the fact that the repository hasn't
19511 requested instantiation in the PCH it is still possible that
19512 an instantiation will be required in a file that includes the
19515 add_pending_template (d
);
19516 /* Instantiate inline functions so that the inliner can do its
19517 job, even though we'll not be emitting a copy of this
19519 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19523 fn_context
= decl_function_context (d
);
19524 nested
= (current_function_decl
!= NULL_TREE
);
19526 push_to_top_level ();
19530 push_function_context ();
19531 cp_unevaluated_operand
= 0;
19532 c_inhibit_evaluation_warnings
= 0;
19535 /* Mark D as instantiated so that recursive calls to
19536 instantiate_decl do not try to instantiate it again. */
19537 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19539 /* Regenerate the declaration in case the template has been modified
19540 by a subsequent redeclaration. */
19541 regenerate_decl_from_template (d
, td
);
19543 /* We already set the file and line above. Reset them now in case
19544 they changed as a result of calling regenerate_decl_from_template. */
19545 input_location
= DECL_SOURCE_LOCATION (d
);
19550 bool const_init
= false;
19552 /* Clear out DECL_RTL; whatever was there before may not be right
19553 since we've reset the type of the declaration. */
19554 SET_DECL_RTL (d
, NULL
);
19555 DECL_IN_AGGR_P (d
) = 0;
19557 /* The initializer is placed in DECL_INITIAL by
19558 regenerate_decl_from_template so we don't need to
19559 push/pop_access_scope again here. Pull it out so that
19560 cp_finish_decl can process it. */
19561 init
= DECL_INITIAL (d
);
19562 DECL_INITIAL (d
) = NULL_TREE
;
19563 DECL_INITIALIZED_P (d
) = 0;
19565 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19566 initializer. That function will defer actual emission until
19567 we have a chance to determine linkage. */
19568 DECL_EXTERNAL (d
) = 0;
19570 /* Enter the scope of D so that access-checking works correctly. */
19571 push_nested_class (DECL_CONTEXT (d
));
19572 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19573 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19574 pop_nested_class ();
19576 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19577 synthesize_method (d
);
19578 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19580 struct pointer_map_t
*saved_local_specializations
;
19584 tree block
= NULL_TREE
;
19586 /* Save away the current list, in case we are instantiating one
19587 template from within the body of another. */
19588 saved_local_specializations
= local_specializations
;
19590 /* Set up the list of local specializations. */
19591 local_specializations
= pointer_map_create ();
19593 /* Set up context. */
19594 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19595 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19596 block
= push_stmt_list ();
19598 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19600 /* Some typedefs referenced from within the template code need to be
19601 access checked at template instantiation time, i.e now. These
19602 types were added to the template at parsing time. Let's get those
19603 and perform the access checks then. */
19604 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19607 /* Create substitution entries for the parameters. */
19608 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19609 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19610 spec_parm
= DECL_ARGUMENTS (d
);
19611 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19613 register_local_specialization (spec_parm
, tmpl_parm
);
19614 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19615 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19617 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19619 if (!DECL_PACK_P (tmpl_parm
))
19621 register_local_specialization (spec_parm
, tmpl_parm
);
19622 spec_parm
= DECL_CHAIN (spec_parm
);
19626 /* Register the (value) argument pack as a specialization of
19627 TMPL_PARM, then move on. */
19628 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19629 register_local_specialization (argpack
, tmpl_parm
);
19632 gcc_assert (!spec_parm
);
19634 /* Substitute into the body of the function. */
19635 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19636 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19637 tf_warning_or_error
, tmpl
);
19640 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19641 tf_warning_or_error
, tmpl
,
19642 /*integral_constant_expression_p=*/false);
19644 /* Set the current input_location to the end of the function
19645 so that finish_function knows where we are. */
19647 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19650 /* We don't need the local specializations any more. */
19651 pointer_map_destroy (local_specializations
);
19652 local_specializations
= saved_local_specializations
;
19654 /* Finish the function. */
19655 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19656 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19657 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19660 d
= finish_function (0);
19661 expand_or_defer_fn (d
);
19664 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19665 cp_check_omp_declare_reduction (d
);
19668 /* We're not deferring instantiation any more. */
19669 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19672 pop_from_top_level ();
19674 pop_function_context ();
19677 input_location
= saved_loc
;
19678 cp_unevaluated_operand
= saved_unevaluated_operand
;
19679 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19680 pop_deferring_access_checks ();
19681 pop_tinst_level ();
19683 timevar_pop (TV_TEMPLATE_INST
);
19688 /* Run through the list of templates that we wish we could
19689 instantiate, and instantiate any we can. RETRIES is the
19690 number of times we retry pending template instantiation. */
19693 instantiate_pending_templates (int retries
)
19696 location_t saved_loc
= input_location
;
19698 /* Instantiating templates may trigger vtable generation. This in turn
19699 may require further template instantiations. We place a limit here
19700 to avoid infinite loop. */
19701 if (pending_templates
&& retries
>= max_tinst_depth
)
19703 tree decl
= pending_templates
->tinst
->decl
;
19705 error ("template instantiation depth exceeds maximum of %d"
19706 " instantiating %q+D, possibly from virtual table generation"
19707 " (use -ftemplate-depth= to increase the maximum)",
19708 max_tinst_depth
, decl
);
19709 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19710 /* Pretend that we defined it. */
19711 DECL_INITIAL (decl
) = error_mark_node
;
19717 struct pending_template
**t
= &pending_templates
;
19718 struct pending_template
*last
= NULL
;
19722 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19723 bool complete
= false;
19725 if (TYPE_P (instantiation
))
19729 if (!COMPLETE_TYPE_P (instantiation
))
19731 instantiate_class_template (instantiation
);
19732 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19733 for (fn
= TYPE_METHODS (instantiation
);
19735 fn
= TREE_CHAIN (fn
))
19736 if (! DECL_ARTIFICIAL (fn
))
19737 instantiate_decl (fn
,
19739 /*expl_inst_class_mem_p=*/false);
19740 if (COMPLETE_TYPE_P (instantiation
))
19744 complete
= COMPLETE_TYPE_P (instantiation
);
19748 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19749 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19752 = instantiate_decl (instantiation
,
19754 /*expl_inst_class_mem_p=*/false);
19755 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19759 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19760 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19764 /* If INSTANTIATION has been instantiated, then we don't
19765 need to consider it again in the future. */
19773 current_tinst_level
= NULL
;
19775 last_pending_template
= last
;
19777 while (reconsider
);
19779 input_location
= saved_loc
;
19782 /* Substitute ARGVEC into T, which is a list of initializers for
19783 either base class or a non-static data member. The TREE_PURPOSEs
19784 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19785 instantiate_decl. */
19788 tsubst_initializer_list (tree t
, tree argvec
)
19790 tree inits
= NULL_TREE
;
19792 for (; t
; t
= TREE_CHAIN (t
))
19796 tree expanded_bases
= NULL_TREE
;
19797 tree expanded_arguments
= NULL_TREE
;
19800 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19805 /* Expand the base class expansion type into separate base
19807 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19808 tf_warning_or_error
,
19810 if (expanded_bases
== error_mark_node
)
19813 /* We'll be building separate TREE_LISTs of arguments for
19815 len
= TREE_VEC_LENGTH (expanded_bases
);
19816 expanded_arguments
= make_tree_vec (len
);
19817 for (i
= 0; i
< len
; i
++)
19818 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19820 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19821 expand each argument in the TREE_VALUE of t. */
19822 expr
= make_node (EXPR_PACK_EXPANSION
);
19823 PACK_EXPANSION_LOCAL_P (expr
) = true;
19824 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19825 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19827 if (TREE_VALUE (t
) == void_type_node
)
19828 /* VOID_TYPE_NODE is used to indicate
19829 value-initialization. */
19831 for (i
= 0; i
< len
; i
++)
19832 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19836 /* Substitute parameter packs into each argument in the
19838 in_base_initializer
= 1;
19839 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19841 tree expanded_exprs
;
19843 /* Expand the argument. */
19844 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19846 = tsubst_pack_expansion (expr
, argvec
,
19847 tf_warning_or_error
,
19849 if (expanded_exprs
== error_mark_node
)
19852 /* Prepend each of the expanded expressions to the
19853 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19854 for (i
= 0; i
< len
; i
++)
19856 TREE_VEC_ELT (expanded_arguments
, i
) =
19857 tree_cons (NULL_TREE
,
19858 TREE_VEC_ELT (expanded_exprs
, i
),
19859 TREE_VEC_ELT (expanded_arguments
, i
));
19862 in_base_initializer
= 0;
19864 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19865 since we built them backwards. */
19866 for (i
= 0; i
< len
; i
++)
19868 TREE_VEC_ELT (expanded_arguments
, i
) =
19869 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19874 for (i
= 0; i
< len
; ++i
)
19876 if (expanded_bases
)
19878 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19879 decl
= expand_member_init (decl
);
19880 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19885 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19886 tf_warning_or_error
, NULL_TREE
);
19888 decl
= expand_member_init (decl
);
19889 if (decl
&& !DECL_P (decl
))
19890 in_base_initializer
= 1;
19892 init
= TREE_VALUE (t
);
19894 if (init
!= void_type_node
)
19895 init
= tsubst_expr (init
, argvec
,
19896 tf_warning_or_error
, NULL_TREE
,
19897 /*integral_constant_expression_p=*/false);
19898 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19899 /* If we had an initializer but it instantiated to nothing,
19900 value-initialize the object. This will only occur when
19901 the initializer was a pack expansion where the parameter
19902 packs used in that expansion were of length zero. */
19903 init
= void_type_node
;
19904 in_base_initializer
= 0;
19909 init
= build_tree_list (decl
, init
);
19910 TREE_CHAIN (init
) = inits
;
19918 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19921 set_current_access_from_decl (tree decl
)
19923 if (TREE_PRIVATE (decl
))
19924 current_access_specifier
= access_private_node
;
19925 else if (TREE_PROTECTED (decl
))
19926 current_access_specifier
= access_protected_node
;
19928 current_access_specifier
= access_public_node
;
19931 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19932 is the instantiation (which should have been created with
19933 start_enum) and ARGS are the template arguments to use. */
19936 tsubst_enum (tree tag
, tree newtag
, tree args
)
19940 if (SCOPED_ENUM_P (newtag
))
19941 begin_scope (sk_scoped_enum
, newtag
);
19943 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19948 decl
= TREE_VALUE (e
);
19949 /* Note that in a template enum, the TREE_VALUE is the
19950 CONST_DECL, not the corresponding INTEGER_CST. */
19951 value
= tsubst_expr (DECL_INITIAL (decl
),
19952 args
, tf_warning_or_error
, NULL_TREE
,
19953 /*integral_constant_expression_p=*/true);
19955 /* Give this enumeration constant the correct access. */
19956 set_current_access_from_decl (decl
);
19958 /* Actually build the enumerator itself. */
19960 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19963 if (SCOPED_ENUM_P (newtag
))
19966 finish_enum_value_list (newtag
);
19967 finish_enum (newtag
);
19969 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19970 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19973 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19974 its type -- but without substituting the innermost set of template
19975 arguments. So, innermost set of template parameters will appear in
19979 get_mostly_instantiated_function_type (tree decl
)
19987 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19988 targs
= DECL_TI_ARGS (decl
);
19989 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19990 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19992 /* There should be as many levels of arguments as there are levels
19994 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19996 fn_type
= TREE_TYPE (tmpl
);
19998 if (parm_depth
== 1)
19999 /* No substitution is necessary. */
20006 /* Replace the innermost level of the TARGS with NULL_TREEs to
20007 let tsubst know not to substitute for those parameters. */
20008 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20009 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20010 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20011 TMPL_ARGS_LEVEL (targs
, i
));
20012 SET_TMPL_ARGS_LEVEL (partial_args
,
20013 TMPL_ARGS_DEPTH (targs
),
20014 make_tree_vec (DECL_NTPARMS (tmpl
)));
20016 /* Make sure that we can see identifiers, and compute access
20018 push_access_scope (decl
);
20020 ++processing_template_decl
;
20021 /* Now, do the (partial) substitution to figure out the
20022 appropriate function type. */
20023 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20024 --processing_template_decl
;
20026 /* Substitute into the template parameters to obtain the real
20027 innermost set of parameters. This step is important if the
20028 innermost set of template parameters contains value
20029 parameters whose types depend on outer template parameters. */
20030 TREE_VEC_LENGTH (partial_args
)--;
20031 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20033 pop_access_scope (decl
);
20039 /* Return truthvalue if we're processing a template different from
20040 the last one involved in diagnostics. */
20042 problematic_instantiation_changed (void)
20044 return current_tinst_level
!= last_error_tinst_level
;
20047 /* Remember current template involved in diagnostics. */
20049 record_last_problematic_instantiation (void)
20051 last_error_tinst_level
= current_tinst_level
;
20054 struct tinst_level
*
20055 current_instantiation (void)
20057 return current_tinst_level
;
20060 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20061 type. Return zero for ok, nonzero for disallowed. Issue error and
20062 warning messages under control of COMPLAIN. */
20065 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20067 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20069 else if (POINTER_TYPE_P (type
))
20071 else if (TYPE_PTRMEM_P (type
))
20073 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20075 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20077 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20079 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20082 if (complain
& tf_error
)
20084 if (type
== error_mark_node
)
20085 inform (input_location
, "invalid template non-type parameter");
20087 error ("%q#T is not a valid type for a template non-type parameter",
20093 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20094 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20097 dependent_type_p_r (tree type
)
20103 A type is dependent if it is:
20105 -- a template parameter. Template template parameters are types
20106 for us (since TYPE_P holds true for them) so we handle
20108 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20109 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20111 /* -- a qualified-id with a nested-name-specifier which contains a
20112 class-name that names a dependent type or whose unqualified-id
20113 names a dependent type. */
20114 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20116 /* -- a cv-qualified type where the cv-unqualified type is
20118 type
= TYPE_MAIN_VARIANT (type
);
20119 /* -- a compound type constructed from any dependent type. */
20120 if (TYPE_PTRMEM_P (type
))
20121 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20122 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20124 else if (TYPE_PTR_P (type
)
20125 || TREE_CODE (type
) == REFERENCE_TYPE
)
20126 return dependent_type_p (TREE_TYPE (type
));
20127 else if (TREE_CODE (type
) == FUNCTION_TYPE
20128 || TREE_CODE (type
) == METHOD_TYPE
)
20132 if (dependent_type_p (TREE_TYPE (type
)))
20134 for (arg_type
= TYPE_ARG_TYPES (type
);
20136 arg_type
= TREE_CHAIN (arg_type
))
20137 if (dependent_type_p (TREE_VALUE (arg_type
)))
20141 /* -- an array type constructed from any dependent type or whose
20142 size is specified by a constant expression that is
20145 We checked for type- and value-dependence of the bounds in
20146 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20147 if (TREE_CODE (type
) == ARRAY_TYPE
)
20149 if (TYPE_DOMAIN (type
)
20150 && dependent_type_p (TYPE_DOMAIN (type
)))
20152 return dependent_type_p (TREE_TYPE (type
));
20155 /* -- a template-id in which either the template name is a template
20157 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20159 /* ... or any of the template arguments is a dependent type or
20160 an expression that is type-dependent or value-dependent. */
20161 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20162 && (any_dependent_template_arguments_p
20163 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20166 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20167 dependent; if the argument of the `typeof' expression is not
20168 type-dependent, then it should already been have resolved. */
20169 if (TREE_CODE (type
) == TYPEOF_TYPE
20170 || TREE_CODE (type
) == DECLTYPE_TYPE
20171 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20174 /* A template argument pack is dependent if any of its packed
20176 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20178 tree args
= ARGUMENT_PACK_ARGS (type
);
20179 int i
, len
= TREE_VEC_LENGTH (args
);
20180 for (i
= 0; i
< len
; ++i
)
20181 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20185 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20186 be template parameters. */
20187 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20190 /* The standard does not specifically mention types that are local
20191 to template functions or local classes, but they should be
20192 considered dependent too. For example:
20194 template <int I> void f() {
20199 The size of `E' cannot be known until the value of `I' has been
20200 determined. Therefore, `E' must be considered dependent. */
20201 scope
= TYPE_CONTEXT (type
);
20202 if (scope
&& TYPE_P (scope
))
20203 return dependent_type_p (scope
);
20204 /* Don't use type_dependent_expression_p here, as it can lead
20205 to infinite recursion trying to determine whether a lambda
20206 nested in a lambda is dependent (c++/47687). */
20207 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20208 && DECL_LANG_SPECIFIC (scope
)
20209 && DECL_TEMPLATE_INFO (scope
)
20210 && (any_dependent_template_arguments_p
20211 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20214 /* Other types are non-dependent. */
20218 /* Returns TRUE if TYPE is dependent, in the sense of
20219 [temp.dep.type]. Note that a NULL type is considered dependent. */
20222 dependent_type_p (tree type
)
20224 /* If there are no template parameters in scope, then there can't be
20225 any dependent types. */
20226 if (!processing_template_decl
)
20228 /* If we are not processing a template, then nobody should be
20229 providing us with a dependent type. */
20231 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20235 /* If the type is NULL, we have not computed a type for the entity
20236 in question; in that case, the type is dependent. */
20240 /* Erroneous types can be considered non-dependent. */
20241 if (type
== error_mark_node
)
20244 /* If we have not already computed the appropriate value for TYPE,
20246 if (!TYPE_DEPENDENT_P_VALID (type
))
20248 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20249 TYPE_DEPENDENT_P_VALID (type
) = 1;
20252 return TYPE_DEPENDENT_P (type
);
20255 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20256 lookup. In other words, a dependent type that is not the current
20260 dependent_scope_p (tree scope
)
20262 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20263 && !currently_open_class (scope
));
20266 /* T is a SCOPE_REF; return whether we need to consider it
20267 instantiation-dependent so that we can check access at instantiation
20268 time even though we know which member it resolves to. */
20271 instantiation_dependent_scope_ref_p (tree t
)
20273 if (DECL_P (TREE_OPERAND (t
, 1))
20274 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20275 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20276 TREE_OPERAND (t
, 1)))
20282 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20283 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20286 /* Note that this predicate is not appropriate for general expressions;
20287 only constant expressions (that satisfy potential_constant_expression)
20288 can be tested for value dependence. */
20291 value_dependent_expression_p (tree expression
)
20293 if (!processing_template_decl
)
20296 /* A name declared with a dependent type. */
20297 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20300 switch (TREE_CODE (expression
))
20302 case IDENTIFIER_NODE
:
20303 /* A name that has not been looked up -- must be dependent. */
20306 case TEMPLATE_PARM_INDEX
:
20307 /* A non-type template parm. */
20311 /* A non-type template parm. */
20312 if (DECL_TEMPLATE_PARM_P (expression
))
20314 return value_dependent_expression_p (DECL_INITIAL (expression
));
20317 /* A constant with literal type and is initialized
20318 with an expression that is value-dependent.
20320 Note that a non-dependent parenthesized initializer will have
20321 already been replaced with its constant value, so if we see
20322 a TREE_LIST it must be dependent. */
20323 if (DECL_INITIAL (expression
)
20324 && decl_constant_var_p (expression
)
20325 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20326 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20330 case DYNAMIC_CAST_EXPR
:
20331 case STATIC_CAST_EXPR
:
20332 case CONST_CAST_EXPR
:
20333 case REINTERPRET_CAST_EXPR
:
20335 /* These expressions are value-dependent if the type to which
20336 the cast occurs is dependent or the expression being casted
20337 is value-dependent. */
20339 tree type
= TREE_TYPE (expression
);
20341 if (dependent_type_p (type
))
20344 /* A functional cast has a list of operands. */
20345 expression
= TREE_OPERAND (expression
, 0);
20348 /* If there are no operands, it must be an expression such
20349 as "int()". This should not happen for aggregate types
20350 because it would form non-constant expressions. */
20351 gcc_assert (cxx_dialect
>= cxx11
20352 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20357 if (TREE_CODE (expression
) == TREE_LIST
)
20358 return any_value_dependent_elements_p (expression
);
20360 return value_dependent_expression_p (expression
);
20364 if (SIZEOF_EXPR_TYPE_P (expression
))
20365 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20369 /* A `sizeof' expression is value-dependent if the operand is
20370 type-dependent or is a pack expansion. */
20371 expression
= TREE_OPERAND (expression
, 0);
20372 if (PACK_EXPANSION_P (expression
))
20374 else if (TYPE_P (expression
))
20375 return dependent_type_p (expression
);
20376 return instantiation_dependent_expression_p (expression
);
20378 case AT_ENCODE_EXPR
:
20379 /* An 'encode' expression is value-dependent if the operand is
20381 expression
= TREE_OPERAND (expression
, 0);
20382 return dependent_type_p (expression
);
20384 case NOEXCEPT_EXPR
:
20385 expression
= TREE_OPERAND (expression
, 0);
20386 return instantiation_dependent_expression_p (expression
);
20389 /* All instantiation-dependent expressions should also be considered
20390 value-dependent. */
20391 return instantiation_dependent_scope_ref_p (expression
);
20393 case COMPONENT_REF
:
20394 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20395 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20397 case NONTYPE_ARGUMENT_PACK
:
20398 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20399 is value-dependent. */
20401 tree values
= ARGUMENT_PACK_ARGS (expression
);
20402 int i
, len
= TREE_VEC_LENGTH (values
);
20404 for (i
= 0; i
< len
; ++i
)
20405 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20413 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20414 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20415 || (type2
? dependent_type_p (type2
) : false));
20419 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20420 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20423 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20424 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20428 tree op
= TREE_OPERAND (expression
, 0);
20429 return (value_dependent_expression_p (op
)
20430 || has_value_dependent_address (op
));
20435 tree fn
= get_callee_fndecl (expression
);
20437 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20439 nargs
= call_expr_nargs (expression
);
20440 for (i
= 0; i
< nargs
; ++i
)
20442 tree op
= CALL_EXPR_ARG (expression
, i
);
20443 /* In a call to a constexpr member function, look through the
20444 implicit ADDR_EXPR on the object argument so that it doesn't
20445 cause the call to be considered value-dependent. We also
20446 look through it in potential_constant_expression. */
20447 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20448 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20449 && TREE_CODE (op
) == ADDR_EXPR
)
20450 op
= TREE_OPERAND (op
, 0);
20451 if (value_dependent_expression_p (op
))
20457 case TEMPLATE_ID_EXPR
:
20458 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20460 return type_dependent_expression_p (expression
);
20466 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20467 if (value_dependent_expression_p (val
))
20473 /* Treat a GNU statement expression as dependent to avoid crashing
20474 under fold_non_dependent_expr; it can't be constant. */
20478 /* A constant expression is value-dependent if any subexpression is
20479 value-dependent. */
20480 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20482 case tcc_reference
:
20484 case tcc_comparison
:
20486 case tcc_expression
:
20489 int i
, len
= cp_tree_operand_length (expression
);
20491 for (i
= 0; i
< len
; i
++)
20493 tree t
= TREE_OPERAND (expression
, i
);
20495 /* In some cases, some of the operands may be missing.l
20496 (For example, in the case of PREDECREMENT_EXPR, the
20497 amount to increment by may be missing.) That doesn't
20498 make the expression dependent. */
20499 if (t
&& value_dependent_expression_p (t
))
20510 /* The expression is not value-dependent. */
20514 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20515 [temp.dep.expr]. Note that an expression with no type is
20516 considered dependent. Other parts of the compiler arrange for an
20517 expression with type-dependent subexpressions to have no type, so
20518 this function doesn't have to be fully recursive. */
20521 type_dependent_expression_p (tree expression
)
20523 if (!processing_template_decl
)
20526 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20529 /* An unresolved name is always dependent. */
20530 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20533 /* Some expression forms are never type-dependent. */
20534 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20535 || TREE_CODE (expression
) == SIZEOF_EXPR
20536 || TREE_CODE (expression
) == ALIGNOF_EXPR
20537 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20538 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20539 || TREE_CODE (expression
) == TRAIT_EXPR
20540 || TREE_CODE (expression
) == TYPEID_EXPR
20541 || TREE_CODE (expression
) == DELETE_EXPR
20542 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20543 || TREE_CODE (expression
) == THROW_EXPR
)
20546 /* The types of these expressions depends only on the type to which
20547 the cast occurs. */
20548 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20549 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20550 || TREE_CODE (expression
) == CONST_CAST_EXPR
20551 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20552 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20553 || TREE_CODE (expression
) == CAST_EXPR
)
20554 return dependent_type_p (TREE_TYPE (expression
));
20556 /* The types of these expressions depends only on the type created
20557 by the expression. */
20558 if (TREE_CODE (expression
) == NEW_EXPR
20559 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20561 /* For NEW_EXPR tree nodes created inside a template, either
20562 the object type itself or a TREE_LIST may appear as the
20564 tree type
= TREE_OPERAND (expression
, 1);
20565 if (TREE_CODE (type
) == TREE_LIST
)
20566 /* This is an array type. We need to check array dimensions
20568 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20569 || value_dependent_expression_p
20570 (TREE_OPERAND (TREE_VALUE (type
), 1));
20572 return dependent_type_p (type
);
20575 if (TREE_CODE (expression
) == SCOPE_REF
)
20577 tree scope
= TREE_OPERAND (expression
, 0);
20578 tree name
= TREE_OPERAND (expression
, 1);
20580 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20581 contains an identifier associated by name lookup with one or more
20582 declarations declared with a dependent type, or...a
20583 nested-name-specifier or qualified-id that names a member of an
20584 unknown specialization. */
20585 return (type_dependent_expression_p (name
)
20586 || dependent_scope_p (scope
));
20589 if (TREE_CODE (expression
) == FUNCTION_DECL
20590 && DECL_LANG_SPECIFIC (expression
)
20591 && DECL_TEMPLATE_INFO (expression
)
20592 && (any_dependent_template_arguments_p
20593 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20596 if (TREE_CODE (expression
) == TEMPLATE_DECL
20597 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20600 if (TREE_CODE (expression
) == STMT_EXPR
)
20601 expression
= stmt_expr_value_expr (expression
);
20603 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20610 if (type_dependent_expression_p (elt
))
20616 /* A static data member of the current instantiation with incomplete
20617 array type is type-dependent, as the definition and specializations
20618 can have different bounds. */
20619 if (VAR_P (expression
)
20620 && DECL_CLASS_SCOPE_P (expression
)
20621 && dependent_type_p (DECL_CONTEXT (expression
))
20622 && VAR_HAD_UNKNOWN_BOUND (expression
))
20625 /* An array of unknown bound depending on a variadic parameter, eg:
20627 template<typename... Args>
20628 void foo (Args... args)
20630 int arr[] = { args... };
20633 template<int... vals>
20636 int arr[] = { vals... };
20639 If the array has no length and has an initializer, it must be that
20640 we couldn't determine its length in cp_complete_array_type because
20641 it is dependent. */
20642 if (VAR_P (expression
)
20643 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20644 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20645 && DECL_INITIAL (expression
))
20648 if (TREE_TYPE (expression
) == unknown_type_node
)
20650 if (TREE_CODE (expression
) == ADDR_EXPR
)
20651 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20652 if (TREE_CODE (expression
) == COMPONENT_REF
20653 || TREE_CODE (expression
) == OFFSET_REF
)
20655 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20657 expression
= TREE_OPERAND (expression
, 1);
20658 if (identifier_p (expression
))
20661 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20662 if (TREE_CODE (expression
) == SCOPE_REF
)
20665 /* Always dependent, on the number of arguments if nothing else. */
20666 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20669 if (BASELINK_P (expression
))
20670 expression
= BASELINK_FUNCTIONS (expression
);
20672 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20674 if (any_dependent_template_arguments_p
20675 (TREE_OPERAND (expression
, 1)))
20677 expression
= TREE_OPERAND (expression
, 0);
20679 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20680 || TREE_CODE (expression
) == FUNCTION_DECL
);
20684 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20686 expression
= OVL_NEXT (expression
);
20691 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20693 return (dependent_type_p (TREE_TYPE (expression
)));
20696 /* walk_tree callback function for instantiation_dependent_expression_p,
20697 below. Returns non-zero if a dependent subexpression is found. */
20700 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20705 /* We don't have to worry about decltype currently because decltype
20706 of an instantiation-dependent expr is a dependent type. This
20707 might change depending on the resolution of DR 1172. */
20708 *walk_subtrees
= false;
20711 enum tree_code code
= TREE_CODE (*tp
);
20714 /* Don't treat an argument list as dependent just because it has no
20722 /* A constant with a dependent initializer is dependent. */
20723 if (value_dependent_expression_p (*tp
))
20727 case TEMPLATE_PARM_INDEX
:
20730 /* Handle expressions with type operands. */
20734 case AT_ENCODE_EXPR
:
20736 tree op
= TREE_OPERAND (*tp
, 0);
20737 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20738 op
= TREE_TYPE (op
);
20741 if (dependent_type_p (op
))
20745 *walk_subtrees
= false;
20753 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20754 || (TRAIT_EXPR_TYPE2 (*tp
)
20755 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20757 *walk_subtrees
= false;
20760 case COMPONENT_REF
:
20761 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20762 /* In a template, finish_class_member_access_expr creates a
20763 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20764 type-dependent, so that we can check access control at
20765 instantiation time (PR 42277). See also Core issue 1273. */
20770 if (instantiation_dependent_scope_ref_p (*tp
))
20775 /* Treat statement-expressions as dependent. */
20783 if (type_dependent_expression_p (*tp
))
20789 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20790 sense defined by the ABI:
20792 "An expression is instantiation-dependent if it is type-dependent
20793 or value-dependent, or it has a subexpression that is type-dependent
20794 or value-dependent." */
20797 instantiation_dependent_expression_p (tree expression
)
20801 if (!processing_template_decl
)
20804 if (expression
== error_mark_node
)
20807 result
= cp_walk_tree_without_duplicates (&expression
,
20808 instantiation_dependent_r
, NULL
);
20809 return result
!= NULL_TREE
;
20812 /* Like type_dependent_expression_p, but it also works while not processing
20813 a template definition, i.e. during substitution or mangling. */
20816 type_dependent_expression_p_push (tree expr
)
20819 ++processing_template_decl
;
20820 b
= type_dependent_expression_p (expr
);
20821 --processing_template_decl
;
20825 /* Returns TRUE if ARGS contains a type-dependent expression. */
20828 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20833 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20835 if (type_dependent_expression_p (arg
))
20841 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20842 expressions) contains any type-dependent expressions. */
20845 any_type_dependent_elements_p (const_tree list
)
20847 for (; list
; list
= TREE_CHAIN (list
))
20848 if (type_dependent_expression_p (TREE_VALUE (list
)))
20854 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20855 expressions) contains any value-dependent expressions. */
20858 any_value_dependent_elements_p (const_tree list
)
20860 for (; list
; list
= TREE_CHAIN (list
))
20861 if (value_dependent_expression_p (TREE_VALUE (list
)))
20867 /* Returns TRUE if the ARG (a template argument) is dependent. */
20870 dependent_template_arg_p (tree arg
)
20872 if (!processing_template_decl
)
20875 /* Assume a template argument that was wrongly written by the user
20876 is dependent. This is consistent with what
20877 any_dependent_template_arguments_p [that calls this function]
20879 if (!arg
|| arg
== error_mark_node
)
20882 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20883 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20885 if (TREE_CODE (arg
) == TEMPLATE_DECL
20886 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20887 return dependent_template_p (arg
);
20888 else if (ARGUMENT_PACK_P (arg
))
20890 tree args
= ARGUMENT_PACK_ARGS (arg
);
20891 int i
, len
= TREE_VEC_LENGTH (args
);
20892 for (i
= 0; i
< len
; ++i
)
20894 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20900 else if (TYPE_P (arg
))
20901 return dependent_type_p (arg
);
20903 return (type_dependent_expression_p (arg
)
20904 || value_dependent_expression_p (arg
));
20907 /* Returns true if ARGS (a collection of template arguments) contains
20908 any types that require structural equality testing. */
20911 any_template_arguments_need_structural_equality_p (tree args
)
20918 if (args
== error_mark_node
)
20921 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20923 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20924 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20926 tree arg
= TREE_VEC_ELT (level
, j
);
20927 tree packed_args
= NULL_TREE
;
20930 if (ARGUMENT_PACK_P (arg
))
20932 /* Look inside the argument pack. */
20933 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20934 len
= TREE_VEC_LENGTH (packed_args
);
20937 for (k
= 0; k
< len
; ++k
)
20940 arg
= TREE_VEC_ELT (packed_args
, k
);
20942 if (error_operand_p (arg
))
20944 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20946 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20948 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20949 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20958 /* Returns true if ARGS (a collection of template arguments) contains
20959 any dependent arguments. */
20962 any_dependent_template_arguments_p (const_tree args
)
20969 if (args
== error_mark_node
)
20972 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20974 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20975 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20976 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20983 /* Returns TRUE if the template TMPL is dependent. */
20986 dependent_template_p (tree tmpl
)
20988 if (TREE_CODE (tmpl
) == OVERLOAD
)
20992 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20994 tmpl
= OVL_NEXT (tmpl
);
20999 /* Template template parameters are dependent. */
21000 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21001 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21003 /* So are names that have not been looked up. */
21004 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21006 /* So are member templates of dependent classes. */
21007 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21008 return dependent_type_p (DECL_CONTEXT (tmpl
));
21012 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21015 dependent_template_id_p (tree tmpl
, tree args
)
21017 return (dependent_template_p (tmpl
)
21018 || any_dependent_template_arguments_p (args
));
21021 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21025 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21029 if (!processing_template_decl
)
21032 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21034 tree decl
= TREE_VEC_ELT (declv
, i
);
21035 tree init
= TREE_VEC_ELT (initv
, i
);
21036 tree cond
= TREE_VEC_ELT (condv
, i
);
21037 tree incr
= TREE_VEC_ELT (incrv
, i
);
21039 if (type_dependent_expression_p (decl
))
21042 if (init
&& type_dependent_expression_p (init
))
21045 if (type_dependent_expression_p (cond
))
21048 if (COMPARISON_CLASS_P (cond
)
21049 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21050 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21053 if (TREE_CODE (incr
) == MODOP_EXPR
)
21055 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21056 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21059 else if (type_dependent_expression_p (incr
))
21061 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21063 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21065 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21067 tree t
= TREE_OPERAND (incr
, 1);
21068 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21069 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21078 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21079 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21080 no such TYPE can be found. Note that this function peers inside
21081 uninstantiated templates and therefore should be used only in
21082 extremely limited situations. ONLY_CURRENT_P restricts this
21083 peering to the currently open classes hierarchy (which is required
21084 when comparing types). */
21087 resolve_typename_type (tree type
, bool only_current_p
)
21096 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21098 scope
= TYPE_CONTEXT (type
);
21099 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21100 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21101 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21102 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21103 identifier of the TYPENAME_TYPE anymore.
21104 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21105 TYPENAME_TYPE instead, we avoid messing up with a possible
21106 typedef variant case. */
21107 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21109 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21110 it first before we can figure out what NAME refers to. */
21111 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21113 if (TYPENAME_IS_RESOLVING_P (scope
))
21114 /* Given a class template A with a dependent base with nested type C,
21115 typedef typename A::C::C C will land us here, as trying to resolve
21116 the initial A::C leads to the local C typedef, which leads back to
21117 A::C::C. So we break the recursion now. */
21120 scope
= resolve_typename_type (scope
, only_current_p
);
21122 /* If we don't know what SCOPE refers to, then we cannot resolve the
21124 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21126 /* If the SCOPE is a template type parameter, we have no way of
21127 resolving the name. */
21128 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21130 /* If the SCOPE is not the current instantiation, there's no reason
21131 to look inside it. */
21132 if (only_current_p
&& !currently_open_class (scope
))
21134 /* If this is a typedef, we don't want to look inside (c++/11987). */
21135 if (typedef_variant_p (type
))
21137 /* If SCOPE isn't the template itself, it will not have a valid
21138 TYPE_FIELDS list. */
21139 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21140 /* scope is either the template itself or a compatible instantiation
21141 like X<T>, so look up the name in the original template. */
21142 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21144 /* scope is a partial instantiation, so we can't do the lookup or we
21145 will lose the template arguments. */
21147 /* Enter the SCOPE so that name lookup will be resolved as if we
21148 were in the class definition. In particular, SCOPE will no
21149 longer be considered a dependent type. */
21150 pushed_scope
= push_scope (scope
);
21151 /* Look up the declaration. */
21152 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21153 tf_warning_or_error
);
21155 result
= NULL_TREE
;
21157 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21158 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21161 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21162 && TREE_CODE (decl
) == TYPE_DECL
)
21164 result
= TREE_TYPE (decl
);
21165 if (result
== error_mark_node
)
21166 result
= NULL_TREE
;
21168 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21169 && DECL_CLASS_TEMPLATE_P (decl
))
21173 /* Obtain the template and the arguments. */
21174 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21175 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21176 /* Instantiate the template. */
21177 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21178 /*entering_scope=*/0,
21179 tf_error
| tf_user
);
21180 if (result
== error_mark_node
)
21181 result
= NULL_TREE
;
21184 /* Leave the SCOPE. */
21186 pop_scope (pushed_scope
);
21188 /* If we failed to resolve it, return the original typename. */
21192 /* If lookup found a typename type, resolve that too. */
21193 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21195 /* Ill-formed programs can cause infinite recursion here, so we
21196 must catch that. */
21197 TYPENAME_IS_RESOLVING_P (type
) = 1;
21198 result
= resolve_typename_type (result
, only_current_p
);
21199 TYPENAME_IS_RESOLVING_P (type
) = 0;
21202 /* Qualify the resulting type. */
21203 quals
= cp_type_quals (type
);
21205 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21210 /* EXPR is an expression which is not type-dependent. Return a proxy
21211 for EXPR that can be used to compute the types of larger
21212 expressions containing EXPR. */
21215 build_non_dependent_expr (tree expr
)
21219 #ifdef ENABLE_CHECKING
21220 /* Try to get a constant value for all non-dependent expressions in
21221 order to expose bugs in *_dependent_expression_p and constexpr. */
21222 if (cxx_dialect
>= cxx11
)
21223 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21226 /* Preserve OVERLOADs; the functions must be available to resolve
21229 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21230 inner_expr
= stmt_expr_value_expr (inner_expr
);
21231 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21232 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21233 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21234 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21235 if (is_overloaded_fn (inner_expr
)
21236 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21238 /* There is no need to return a proxy for a variable. */
21241 /* Preserve string constants; conversions from string constants to
21242 "char *" are allowed, even though normally a "const char *"
21243 cannot be used to initialize a "char *". */
21244 if (TREE_CODE (expr
) == STRING_CST
)
21246 /* Preserve arithmetic constants, as an optimization -- there is no
21247 reason to create a new node. */
21248 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21250 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21251 There is at least one place where we want to know that a
21252 particular expression is a throw-expression: when checking a ?:
21253 expression, there are special rules if the second or third
21254 argument is a throw-expression. */
21255 if (TREE_CODE (expr
) == THROW_EXPR
)
21258 /* Don't wrap an initializer list, we need to be able to look inside. */
21259 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21262 /* Don't wrap a dummy object, we need to be able to test for it. */
21263 if (is_dummy_object (expr
))
21266 if (TREE_CODE (expr
) == COND_EXPR
)
21267 return build3 (COND_EXPR
,
21269 TREE_OPERAND (expr
, 0),
21270 (TREE_OPERAND (expr
, 1)
21271 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21272 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21273 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21274 if (TREE_CODE (expr
) == COMPOUND_EXPR
21275 && !COMPOUND_EXPR_OVERLOADED (expr
))
21276 return build2 (COMPOUND_EXPR
,
21278 TREE_OPERAND (expr
, 0),
21279 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21281 /* If the type is unknown, it can't really be non-dependent */
21282 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21284 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21285 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21288 /* ARGS is a vector of expressions as arguments to a function call.
21289 Replace the arguments with equivalent non-dependent expressions.
21290 This modifies ARGS in place. */
21293 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21298 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21300 tree newarg
= build_non_dependent_expr (arg
);
21302 (*args
)[ix
] = newarg
;
21306 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21307 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21311 make_auto_1 (tree name
)
21313 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21314 TYPE_NAME (au
) = build_decl (input_location
,
21315 TYPE_DECL
, name
, au
);
21316 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21317 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21318 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21319 TYPE_NAME (au
), NULL_TREE
);
21320 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21321 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21322 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21328 make_decltype_auto (void)
21330 return make_auto_1 (get_identifier ("decltype(auto)"));
21336 return make_auto_1 (get_identifier ("auto"));
21339 /* Given type ARG, return std::initializer_list<ARG>. */
21344 tree std_init_list
= namespace_binding
21345 (get_identifier ("initializer_list"), std_node
);
21347 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21349 error ("deducing from brace-enclosed initializer list requires "
21350 "#include <initializer_list>");
21351 return error_mark_node
;
21353 argvec
= make_tree_vec (1);
21354 TREE_VEC_ELT (argvec
, 0) = arg
;
21355 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21356 NULL_TREE
, 0, tf_warning_or_error
);
21359 /* Replace auto in TYPE with std::initializer_list<auto>. */
21362 listify_autos (tree type
, tree auto_node
)
21364 tree init_auto
= listify (auto_node
);
21365 tree argvec
= make_tree_vec (1);
21366 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21367 if (processing_template_decl
)
21368 argvec
= add_to_template_args (current_template_args (), argvec
);
21369 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21372 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21373 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21376 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21380 if (init
== error_mark_node
)
21381 return error_mark_node
;
21383 if (type_dependent_expression_p (init
))
21384 /* Defining a subset of type-dependent expressions that we can deduce
21385 from ahead of time isn't worth the trouble. */
21388 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21389 with either a new invented type template parameter U or, if the
21390 initializer is a braced-init-list (8.5.4), with
21391 std::initializer_list<U>. */
21392 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21393 type
= listify_autos (type
, auto_node
);
21395 init
= resolve_nondeduced_context (init
);
21397 targs
= make_tree_vec (1);
21398 if (AUTO_IS_DECLTYPE (auto_node
))
21400 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21401 TREE_VEC_ELT (targs
, 0)
21402 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21403 if (type
!= auto_node
)
21405 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21406 return error_mark_node
;
21411 tree parms
= build_tree_list (NULL_TREE
, type
);
21412 tree tparms
= make_tree_vec (1);
21415 TREE_VEC_ELT (tparms
, 0)
21416 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21417 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21418 DEDUCE_CALL
, LOOKUP_NORMAL
,
21419 NULL
, /*explain_p=*/false);
21422 if (processing_template_decl
)
21423 /* Try again at instantiation time. */
21425 if (type
&& type
!= error_mark_node
)
21426 /* If type is error_mark_node a diagnostic must have been
21427 emitted by now. Also, having a mention to '<type error>'
21428 in the diagnostic is not really useful to the user. */
21430 if (cfun
&& auto_node
== current_function_auto_return_pattern
21431 && LAMBDA_FUNCTION_P (current_function_decl
))
21432 error ("unable to deduce lambda return type from %qE", init
);
21434 error ("unable to deduce %qT from %qE", type
, init
);
21436 return error_mark_node
;
21440 /* If the list of declarators contains more than one declarator, the type
21441 of each declared variable is determined as described above. If the
21442 type deduced for the template parameter U is not the same in each
21443 deduction, the program is ill-formed. */
21444 if (TREE_TYPE (auto_node
)
21445 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21447 if (cfun
&& auto_node
== current_function_auto_return_pattern
21448 && LAMBDA_FUNCTION_P (current_function_decl
))
21449 error ("inconsistent types %qT and %qT deduced for "
21450 "lambda return type", TREE_TYPE (auto_node
),
21451 TREE_VEC_ELT (targs
, 0));
21453 error ("inconsistent deduction for %qT: %qT and then %qT",
21454 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21455 return error_mark_node
;
21457 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21459 if (processing_template_decl
)
21460 targs
= add_to_template_args (current_template_args (), targs
);
21461 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21464 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21468 splice_late_return_type (tree type
, tree late_return_type
)
21472 if (late_return_type
== NULL_TREE
)
21474 argvec
= make_tree_vec (1);
21475 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21476 if (processing_template_parmlist
)
21477 /* For a late-specified return type in a template type-parameter, we
21478 need to add a dummy argument level for its parmlist. */
21479 argvec
= add_to_template_args
21480 (make_tree_vec (processing_template_parmlist
), argvec
);
21481 if (current_template_parms
)
21482 argvec
= add_to_template_args (current_template_args (), argvec
);
21483 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21486 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21487 'decltype(auto)'. */
21490 is_auto (const_tree type
)
21492 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21493 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21494 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21500 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21501 a use of `auto'. Returns NULL_TREE otherwise. */
21504 type_uses_auto (tree type
)
21506 return find_type_usage (type
, is_auto
);
21509 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21510 'decltype(auto)' or a concept. */
21513 is_auto_or_concept (const_tree type
)
21515 return is_auto (type
); // or concept
21518 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21519 a concept identifier) iff TYPE contains a use of a generic type. Returns
21520 NULL_TREE otherwise. */
21523 type_uses_auto_or_concept (tree type
)
21525 return find_type_usage (type
, is_auto_or_concept
);
21529 /* For a given template T, return the vector of typedefs referenced
21530 in T for which access check is needed at T instantiation time.
21531 T is either a FUNCTION_DECL or a RECORD_TYPE.
21532 Those typedefs were added to T by the function
21533 append_type_to_template_for_access_check. */
21535 vec
<qualified_typedef_usage_t
, va_gc
> *
21536 get_types_needing_access_check (tree t
)
21539 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21541 if (!t
|| t
== error_mark_node
)
21544 if (!(ti
= get_template_info (t
)))
21547 if (CLASS_TYPE_P (t
)
21548 || TREE_CODE (t
) == FUNCTION_DECL
)
21550 if (!TI_TEMPLATE (ti
))
21553 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21559 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21560 tied to T. That list of typedefs will be access checked at
21561 T instantiation time.
21562 T is either a FUNCTION_DECL or a RECORD_TYPE.
21563 TYPE_DECL is a TYPE_DECL node representing a typedef.
21564 SCOPE is the scope through which TYPE_DECL is accessed.
21565 LOCATION is the location of the usage point of TYPE_DECL.
21567 This function is a subroutine of
21568 append_type_to_template_for_access_check. */
21571 append_type_to_template_for_access_check_1 (tree t
,
21574 location_t location
)
21576 qualified_typedef_usage_t typedef_usage
;
21579 if (!t
|| t
== error_mark_node
)
21582 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21583 || CLASS_TYPE_P (t
))
21585 && TREE_CODE (type_decl
) == TYPE_DECL
21588 if (!(ti
= get_template_info (t
)))
21591 gcc_assert (TI_TEMPLATE (ti
));
21593 typedef_usage
.typedef_decl
= type_decl
;
21594 typedef_usage
.context
= scope
;
21595 typedef_usage
.locus
= location
;
21597 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21600 /* Append TYPE_DECL to the template TEMPL.
21601 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21602 At TEMPL instanciation time, TYPE_DECL will be checked to see
21603 if it can be accessed through SCOPE.
21604 LOCATION is the location of the usage point of TYPE_DECL.
21606 e.g. consider the following code snippet:
21613 template<class U> struct S
21615 C::myint mi; // <-- usage point of the typedef C::myint
21620 At S<char> instantiation time, we need to check the access of C::myint
21621 In other words, we need to check the access of the myint typedef through
21622 the C scope. For that purpose, this function will add the myint typedef
21623 and the scope C through which its being accessed to a list of typedefs
21624 tied to the template S. That list will be walked at template instantiation
21625 time and access check performed on each typedefs it contains.
21626 Note that this particular code snippet should yield an error because
21627 myint is private to C. */
21630 append_type_to_template_for_access_check (tree templ
,
21633 location_t location
)
21635 qualified_typedef_usage_t
*iter
;
21638 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21640 /* Make sure we don't append the type to the template twice. */
21641 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21642 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21645 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21649 /* Convert the generic type parameters in PARM that match the types given in the
21650 range [START_IDX, END_IDX) from the current_template_parms into generic type
21654 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21656 tree current
= current_template_parms
;
21657 int depth
= TMPL_PARMS_DEPTH (current
);
21658 current
= INNERMOST_TEMPLATE_PARMS (current
);
21659 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21661 for (int i
= 0; i
< start_idx
; ++i
)
21662 TREE_VEC_ELT (replacement
, i
)
21663 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21665 for (int i
= start_idx
; i
< end_idx
; ++i
)
21667 /* Create a distinct parameter pack type from the current parm and add it
21668 to the replacement args to tsubst below into the generic function
21671 tree o
= TREE_TYPE (TREE_VALUE
21672 (TREE_VEC_ELT (current
, i
)));
21673 tree t
= copy_type (o
);
21674 TEMPLATE_TYPE_PARM_INDEX (t
)
21675 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21677 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21678 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21679 TYPE_MAIN_VARIANT (t
) = t
;
21680 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21681 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21682 TREE_VEC_ELT (replacement
, i
) = t
;
21683 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21686 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21687 TREE_VEC_ELT (replacement
, i
)
21688 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21690 /* If there are more levels then build up the replacement with the outer
21693 replacement
= add_to_template_args (template_parms_to_args
21694 (TREE_CHAIN (current_template_parms
)),
21697 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21701 /* Set up the hash tables for template instantiations. */
21704 init_template_processing (void)
21706 decl_specializations
= htab_create_ggc (37,
21707 hash_specialization
,
21708 eq_specializations
,
21710 type_specializations
= htab_create_ggc (37,
21711 hash_specialization
,
21712 eq_specializations
,
21716 /* Print stats about the template hash tables for -fstats. */
21719 print_template_statistics (void)
21721 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21722 "%f collisions\n", (long) htab_size (decl_specializations
),
21723 (long) htab_elements (decl_specializations
),
21724 htab_collisions (decl_specializations
));
21725 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21726 "%f collisions\n", (long) htab_size (type_specializations
),
21727 (long) htab_elements (type_specializations
),
21728 htab_collisions (type_specializations
));
21731 #include "gt-cp-pt.h"