1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "pointer-set.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
51 /* The type of functions taking a tree, and some additional data, and
53 typedef int (*tree_fn_t
) (tree
, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template
{
59 struct pending_template
*next
;
60 struct tinst_level
*tinst
;
63 static GTY(()) struct pending_template
*pending_templates
;
64 static GTY(()) struct pending_template
*last_pending_template
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static GTY(()) tree saved_trees
;
70 static vec
<int> inline_parm_levels
;
72 static GTY(()) struct tinst_level
*current_tinst_level
;
74 static GTY(()) tree saved_access_scope
;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr
;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
121 unification_kind_t
, int,
123 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
124 unification_kind_t
, int, bool, bool);
125 static int unify (tree
, tree
, tree
, tree
, int, bool);
126 static void add_pending_template (tree
);
127 static tree
reopen_tinst_level (struct tinst_level
*);
128 static tree
tsubst_initializer_list (tree
, tree
);
129 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
130 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
132 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
142 vec
<deferred_access_check
, va_gc
> **,
144 static void note_template_header (int);
145 static tree
convert_nontype_argument_function (tree
, tree
);
146 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
147 static tree
convert_template_argument (tree
, tree
, tree
,
148 tsubst_flags_t
, int, tree
);
149 static int for_each_template_parm (tree
, tree_fn_t
, void*,
150 struct pointer_set_t
*, bool);
151 static tree
expand_template_argument_pack (tree
);
152 static tree
build_template_parm_index (int, int, int, tree
, tree
);
153 static bool inline_needs_template_parms (tree
, bool);
154 static void push_inline_template_parms_recursive (tree
, int);
155 static tree
retrieve_local_specialization (tree
);
156 static void register_local_specialization (tree
, tree
);
157 static hashval_t
hash_specialization (const void *p
);
158 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
159 static int mark_template_parm (tree
, void *);
160 static int template_parm_this_level_p (tree
, void *);
161 static tree
tsubst_friend_function (tree
, tree
);
162 static tree
tsubst_friend_class (tree
, tree
);
163 static int can_complete_type_without_circularity (tree
);
164 static tree
get_bindings (tree
, tree
, tree
, bool);
165 static int template_decl_level (tree
);
166 static int check_cv_quals_for_unify (int, tree
, tree
);
167 static void template_parm_level_and_index (tree
, int*, int*);
168 static int unify_pack_expansion (tree
, tree
, tree
,
169 tree
, unification_kind_t
, bool, bool);
170 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
173 static void regenerate_decl_from_template (tree
, tree
);
174 static tree
most_specialized_class (tree
, tsubst_flags_t
);
175 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
176 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
178 static bool check_specialization_scope (void);
179 static tree
process_partial_specialization (tree
);
180 static void set_current_access_from_decl (tree
);
181 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
183 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
184 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
186 static bool template_template_parm_bindings_ok_p (tree
, tree
);
187 static int template_args_equal (tree
, tree
);
188 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
189 static tree
for_each_template_parm_r (tree
*, int *, void *);
190 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
191 static void copy_default_args_to_explicit_spec (tree
);
192 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
193 static bool dependent_template_arg_p (tree
);
194 static bool any_template_arguments_need_structural_equality_p (tree
);
195 static bool dependent_type_p_r (tree
);
196 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
197 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
200 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
201 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
203 static tree
listify (tree
);
204 static tree
listify_autos (tree
, tree
);
205 static tree
template_parm_to_arg (tree t
);
206 static tree
current_template_args (void);
207 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
208 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
216 push_access_scope (tree t
)
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
219 || TREE_CODE (t
) == TYPE_DECL
);
221 if (DECL_FRIEND_CONTEXT (t
))
222 push_nested_class (DECL_FRIEND_CONTEXT (t
));
223 else if (DECL_CLASS_SCOPE_P (t
))
224 push_nested_class (DECL_CONTEXT (t
));
226 push_to_top_level ();
228 if (TREE_CODE (t
) == FUNCTION_DECL
)
230 saved_access_scope
= tree_cons
231 (NULL_TREE
, current_function_decl
, saved_access_scope
);
232 current_function_decl
= t
;
236 /* Restore the scope set up by push_access_scope. T is the node we
240 pop_access_scope (tree t
)
242 if (TREE_CODE (t
) == FUNCTION_DECL
)
244 current_function_decl
= TREE_VALUE (saved_access_scope
);
245 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
248 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
260 finish_member_template_decl (tree decl
)
262 if (decl
== error_mark_node
)
263 return error_mark_node
;
265 gcc_assert (DECL_P (decl
));
267 if (TREE_CODE (decl
) == TYPE_DECL
)
271 type
= TREE_TYPE (decl
);
272 if (type
== error_mark_node
)
273 return error_mark_node
;
274 if (MAYBE_CLASS_TYPE_P (type
)
275 && CLASSTYPE_TEMPLATE_INFO (type
)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
278 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
279 check_member_template (tmpl
);
284 else if (TREE_CODE (decl
) == FIELD_DECL
)
285 error ("data member %qD cannot be a member template", decl
);
286 else if (DECL_TEMPLATE_INFO (decl
))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
290 check_member_template (DECL_TI_TEMPLATE (decl
));
291 return DECL_TI_TEMPLATE (decl
);
297 error ("invalid member template declaration %qD", decl
);
299 return error_mark_node
;
302 /* Create a template info node. */
305 build_template_info (tree template_decl
, tree template_args
)
307 tree result
= make_node (TEMPLATE_INFO
);
308 TI_TEMPLATE (result
) = template_decl
;
309 TI_ARGS (result
) = template_args
;
313 /* Return the template info node corresponding to T, whatever T is. */
316 get_template_info (const_tree t
)
318 tree tinfo
= NULL_TREE
;
320 if (!t
|| t
== error_mark_node
)
323 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
324 tinfo
= DECL_TEMPLATE_INFO (t
);
326 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
329 if (OVERLOAD_TYPE_P (t
))
330 tinfo
= TYPE_TEMPLATE_INFO (t
);
331 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
332 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
337 /* Returns the template nesting level of the indicated class TYPE.
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
356 template_class_depth (tree type
)
361 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
362 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
363 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
365 tree tinfo
= get_template_info (type
);
367 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
379 inline_needs_template_parms (tree decl
, bool nsdmi
)
381 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
385 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
394 push_inline_template_parms_recursive (tree parmlist
, int levels
)
396 tree parms
= TREE_VALUE (parmlist
);
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
402 ++processing_template_decl
;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl
),
405 parms
, current_template_parms
);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
408 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
410 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
412 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
414 if (parm
== error_mark_node
)
417 gcc_assert (DECL_P (parm
));
419 switch (TREE_CODE (parm
))
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
432 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
433 CONST_DECL
, DECL_NAME (parm
),
435 DECL_ARTIFICIAL (decl
) = 1;
436 TREE_CONSTANT (decl
) = 1;
437 TREE_READONLY (decl
) = 1;
438 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
439 SET_DECL_TEMPLATE_PARM_P (decl
);
450 /* Restore the template parameter context for a member template, a
451 friend template defined in a class definition, or a non-template
452 member of template class. */
455 maybe_begin_member_template_processing (tree decl
)
459 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
462 decl
= (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
463 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl
))
466 if (inline_needs_template_parms (decl
, nsdmi
))
468 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
469 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
471 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
474 parms
= TREE_CHAIN (parms
);
477 push_inline_template_parms_recursive (parms
, levels
);
480 /* Remember how many levels of template parameters we pushed so that
481 we can pop them later. */
482 inline_parm_levels
.safe_push (levels
);
485 /* Undo the effects of maybe_begin_member_template_processing. */
488 maybe_end_member_template_processing (void)
493 if (inline_parm_levels
.length () == 0)
496 last
= inline_parm_levels
.pop ();
497 for (i
= 0; i
< last
; ++i
)
499 --processing_template_decl
;
500 current_template_parms
= TREE_CHAIN (current_template_parms
);
505 /* Return a new template argument vector which contains all of ARGS,
506 but has as its innermost set of arguments the EXTRA_ARGS. */
509 add_to_template_args (tree args
, tree extra_args
)
516 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
519 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
520 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
522 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
523 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
525 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
526 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
531 /* Like add_to_template_args, but only the outermost ARGS are added to
532 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
533 (EXTRA_ARGS) levels are added. This function is used to combine
534 the template arguments from a partial instantiation with the
535 template arguments used to attain the full instantiation from the
536 partial instantiation. */
539 add_outermost_template_args (tree args
, tree extra_args
)
543 /* If there are more levels of EXTRA_ARGS than there are ARGS,
544 something very fishy is going on. */
545 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
547 /* If *all* the new arguments will be the EXTRA_ARGS, just return
549 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
552 /* For the moment, we make ARGS look like it contains fewer levels. */
553 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
555 new_args
= add_to_template_args (args
, extra_args
);
557 /* Now, we restore ARGS to its full dimensions. */
558 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
563 /* Return the N levels of innermost template arguments from the ARGS. */
566 get_innermost_template_args (tree args
, int n
)
574 /* If N is 1, just return the innermost set of template arguments. */
576 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
578 /* If we're not removing anything, just return the arguments we were
580 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
581 gcc_assert (extra_levels
>= 0);
582 if (extra_levels
== 0)
585 /* Make a new set of arguments, not containing the outer arguments. */
586 new_args
= make_tree_vec (n
);
587 for (i
= 1; i
<= n
; ++i
)
588 SET_TMPL_ARGS_LEVEL (new_args
, i
,
589 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
594 /* The inverse of get_innermost_template_args: Return all but the innermost
595 EXTRA_LEVELS levels of template arguments from the ARGS. */
598 strip_innermost_template_args (tree args
, int extra_levels
)
601 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
606 /* If N is 1, just return the outermost set of template arguments. */
608 return TMPL_ARGS_LEVEL (args
, 1);
610 /* If we're not removing anything, just return the arguments we were
612 gcc_assert (extra_levels
>= 0);
613 if (extra_levels
== 0)
616 /* Make a new set of arguments, not containing the inner arguments. */
617 new_args
= make_tree_vec (n
);
618 for (i
= 1; i
<= n
; ++i
)
619 SET_TMPL_ARGS_LEVEL (new_args
, i
,
620 TMPL_ARGS_LEVEL (args
, i
));
625 /* We've got a template header coming up; push to a new level for storing
629 begin_template_parm_list (void)
631 /* We use a non-tag-transparent scope here, which causes pushtag to
632 put tags in this scope, rather than in the enclosing class or
633 namespace scope. This is the right thing, since we want
634 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
635 global template class, push_template_decl handles putting the
636 TEMPLATE_DECL into top-level scope. For a nested template class,
639 template <class T> struct S1 {
640 template <class T> struct S2 {};
643 pushtag contains special code to call pushdecl_with_scope on the
644 TEMPLATE_DECL for S2. */
645 begin_scope (sk_template_parms
, NULL
);
646 ++processing_template_decl
;
647 ++processing_template_parmlist
;
648 note_template_header (0);
651 /* This routine is called when a specialization is declared. If it is
652 invalid to declare a specialization here, an error is reported and
653 false is returned, otherwise this routine will return true. */
656 check_specialization_scope (void)
658 tree scope
= current_scope ();
662 An explicit specialization shall be declared in the namespace of
663 which the template is a member, or, for member templates, in the
664 namespace of which the enclosing class or enclosing class
665 template is a member. An explicit specialization of a member
666 function, member class or static data member of a class template
667 shall be declared in the namespace of which the class template
669 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
671 error ("explicit specialization in non-namespace scope %qD", scope
);
677 In an explicit specialization declaration for a member of a class
678 template or a member template that appears in namespace scope,
679 the member template and some of its enclosing class templates may
680 remain unspecialized, except that the declaration shall not
681 explicitly specialize a class member template if its enclosing
682 class templates are not explicitly specialized as well. */
683 if (current_template_parms
)
685 error ("enclosing class templates are not explicitly specialized");
692 /* We've just seen template <>. */
695 begin_specialization (void)
697 begin_scope (sk_template_spec
, NULL
);
698 note_template_header (1);
699 return check_specialization_scope ();
702 /* Called at then end of processing a declaration preceded by
706 end_specialization (void)
709 reset_specialization ();
712 /* Any template <>'s that we have seen thus far are not referring to a
713 function specialization. */
716 reset_specialization (void)
718 processing_specialization
= 0;
719 template_header_count
= 0;
722 /* We've just seen a template header. If SPECIALIZATION is nonzero,
723 it was of the form template <>. */
726 note_template_header (int specialization
)
728 processing_specialization
= specialization
;
729 template_header_count
++;
732 /* We're beginning an explicit instantiation. */
735 begin_explicit_instantiation (void)
737 gcc_assert (!processing_explicit_instantiation
);
738 processing_explicit_instantiation
= true;
743 end_explicit_instantiation (void)
745 gcc_assert (processing_explicit_instantiation
);
746 processing_explicit_instantiation
= false;
749 /* An explicit specialization or partial specialization of TMPL is being
750 declared. Check that the namespace in which the specialization is
751 occurring is permissible. Returns false iff it is invalid to
752 specialize TMPL in the current namespace. */
755 check_specialization_namespace (tree tmpl
)
757 tree tpl_ns
= decl_namespace_context (tmpl
);
761 An explicit specialization shall be declared in the namespace of
762 which the template is a member, or, for member templates, in the
763 namespace of which the enclosing class or enclosing class
764 template is a member. An explicit specialization of a member
765 function, member class or static data member of a class template
766 shall be declared in the namespace of which the class template is
768 if (current_scope() != DECL_CONTEXT (tmpl
)
769 && !at_namespace_scope_p ())
771 error ("specialization of %qD must appear at namespace scope", tmpl
);
774 if (is_associated_namespace (current_namespace
, tpl_ns
))
775 /* Same or super-using namespace. */
779 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
780 permerror (input_location
, " from definition of %q+#D", tmpl
);
785 /* SPEC is an explicit instantiation. Check that it is valid to
786 perform this explicit instantiation in the current namespace. */
789 check_explicit_instantiation_namespace (tree spec
)
793 /* DR 275: An explicit instantiation shall appear in an enclosing
794 namespace of its template. */
795 ns
= decl_namespace_context (spec
);
796 if (!is_ancestor (current_namespace
, ns
))
797 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
798 "(which does not enclose namespace %qD)",
799 spec
, current_namespace
, ns
);
802 /* The TYPE is being declared. If it is a template type, that means it
803 is a partial specialization. Do appropriate error-checking. */
806 maybe_process_partial_specialization (tree type
)
810 if (type
== error_mark_node
)
811 return error_mark_node
;
813 /* A lambda that appears in specialization context is not itself a
815 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
818 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
820 error ("name of class shadows template template parameter %qD",
822 return error_mark_node
;
825 context
= TYPE_CONTEXT (type
);
827 if (TYPE_ALIAS_P (type
))
829 if (TYPE_TEMPLATE_INFO (type
)
830 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
831 error ("specialization of alias template %qD",
832 TYPE_TI_TEMPLATE (type
));
834 error ("explicit specialization of non-template %qT", type
);
835 return error_mark_node
;
837 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
839 /* This is for ordinary explicit specialization and partial
840 specialization of a template class such as:
842 template <> class C<int>;
846 template <class T> class C<T*>;
848 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
850 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
851 && !COMPLETE_TYPE_P (type
))
853 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
854 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
855 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
856 if (processing_template_decl
)
858 if (push_template_decl (TYPE_MAIN_DECL (type
))
860 return error_mark_node
;
863 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
864 error ("specialization of %qT after instantiation", type
);
865 else if (errorcount
&& !processing_specialization
866 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
867 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
868 /* Trying to define a specialization either without a template<> header
869 or in an inappropriate place. We've already given an error, so just
870 bail now so we don't actually define the specialization. */
871 return error_mark_node
;
873 else if (CLASS_TYPE_P (type
)
874 && !CLASSTYPE_USE_TEMPLATE (type
)
875 && CLASSTYPE_TEMPLATE_INFO (type
)
876 && context
&& CLASS_TYPE_P (context
)
877 && CLASSTYPE_TEMPLATE_INFO (context
))
879 /* This is for an explicit specialization of member class
880 template according to [temp.expl.spec/18]:
882 template <> template <class U> class C<int>::D;
884 The context `C<int>' must be an implicit instantiation.
885 Otherwise this is just a member class template declared
888 template <> class C<int> { template <class U> class D; };
889 template <> template <class U> class C<int>::D;
891 In the first case, `C<int>::D' is a specialization of `C<T>::D'
892 while in the second case, `C<int>::D' is a primary template
893 and `C<T>::D' may not exist. */
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
896 && !COMPLETE_TYPE_P (type
))
899 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
901 if (current_namespace
902 != decl_namespace_context (tmpl
))
904 permerror (input_location
, "specializing %q#T in different namespace", type
);
905 permerror (input_location
, " from definition of %q+#D", tmpl
);
908 /* Check for invalid specialization after instantiation:
910 template <> template <> class C<int>::D<int>;
911 template <> template <class U> class C<int>::D; */
913 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
914 t
; t
= TREE_CHAIN (t
))
916 tree inst
= TREE_VALUE (t
);
917 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
918 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
920 /* We already have a full specialization of this partial
921 instantiation, or a full specialization has been
922 looked up but not instantiated. Reassign it to the
923 new member specialization template. */
928 elt
.tmpl
= most_general_template (tmpl
);
929 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
932 htab_remove_elt (type_specializations
, &elt
);
935 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
937 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
938 entry
= ggc_alloc_spec_entry ();
943 /* But if we've had an implicit instantiation, that's a
944 problem ([temp.expl.spec]/6). */
945 error ("specialization %qT after instantiation %qT",
949 /* Mark TYPE as a specialization. And as a result, we only
950 have one level of template argument for the innermost
952 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
953 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
954 CLASSTYPE_TI_ARGS (type
)
955 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
958 else if (processing_specialization
)
960 /* Someday C++0x may allow for enum template specialization. */
961 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
962 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
963 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
964 "of %qD not allowed by ISO C++", type
);
967 error ("explicit specialization of non-template %qT", type
);
968 return error_mark_node
;
975 /* Returns nonzero if we can optimize the retrieval of specializations
976 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
977 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
980 optimize_specialization_lookup_p (tree tmpl
)
982 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
983 && DECL_CLASS_SCOPE_P (tmpl
)
984 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
986 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
987 /* The optimized lookup depends on the fact that the
988 template arguments for the member function template apply
989 purely to the containing class, which is not true if the
990 containing class is an explicit or partial
992 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
993 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
994 && !DECL_CONV_FN_P (tmpl
)
995 /* It is possible to have a template that is not a member
996 template and is not a member of a template class:
998 template <typename T>
999 struct S { friend A::f(); };
1001 Here, the friend function is a template, but the context does
1002 not have template information. The optimized lookup relies
1003 on having ARGS be the template arguments for both the class
1004 and the function template. */
1005 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1008 /* Retrieve the specialization (in the sense of [temp.spec] - a
1009 specialization is either an instantiation or an explicit
1010 specialization) of TMPL for the given template ARGS. If there is
1011 no such specialization, return NULL_TREE. The ARGS are a vector of
1012 arguments, or a vector of vectors of arguments, in the case of
1013 templates with more than one level of parameters.
1015 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1016 then we search for a partial specialization matching ARGS. This
1017 parameter is ignored if TMPL is not a class template.
1019 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1020 result is a NONTYPE_ARGUMENT_PACK. */
1023 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1025 if (tmpl
== NULL_TREE
)
1028 if (args
== error_mark_node
)
1031 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1032 || TREE_CODE (tmpl
) == FIELD_DECL
);
1034 /* There should be as many levels of arguments as there are
1035 levels of parameters. */
1036 gcc_assert (TMPL_ARGS_DEPTH (args
)
1037 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1038 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1039 : template_class_depth (DECL_CONTEXT (tmpl
))));
1041 if (optimize_specialization_lookup_p (tmpl
))
1043 tree class_template
;
1044 tree class_specialization
;
1045 vec
<tree
, va_gc
> *methods
;
1049 /* The template arguments actually apply to the containing
1050 class. Find the class specialization with those
1052 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1053 class_specialization
1054 = retrieve_specialization (class_template
, args
, 0);
1055 if (!class_specialization
)
1057 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1058 for the specialization. */
1059 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1062 /* Iterate through the methods with the indicated name, looking
1063 for the one that has an instance of TMPL. */
1064 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1065 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1067 tree fn
= OVL_CURRENT (fns
);
1068 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1069 /* using-declarations can add base methods to the method vec,
1070 and we don't want those here. */
1071 && DECL_CONTEXT (fn
) == class_specialization
)
1080 htab_t specializations
;
1084 elt
.spec
= NULL_TREE
;
1086 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1087 specializations
= type_specializations
;
1089 specializations
= decl_specializations
;
1092 hash
= hash_specialization (&elt
);
1093 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1101 /* Like retrieve_specialization, but for local declarations. */
1104 retrieve_local_specialization (tree tmpl
)
1108 if (local_specializations
== NULL
)
1111 slot
= pointer_map_contains (local_specializations
, tmpl
);
1112 return slot
? (tree
) *slot
: NULL_TREE
;
1115 /* Returns nonzero iff DECL is a specialization of TMPL. */
1118 is_specialization_of (tree decl
, tree tmpl
)
1122 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1126 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1132 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1134 for (t
= TREE_TYPE (decl
);
1136 t
= CLASSTYPE_USE_TEMPLATE (t
)
1137 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1138 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1145 /* Returns nonzero iff DECL is a specialization of friend declaration
1146 FRIEND_DECL according to [temp.friend]. */
1149 is_specialization_of_friend (tree decl
, tree friend_decl
)
1151 bool need_template
= true;
1154 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1155 || TREE_CODE (decl
) == TYPE_DECL
);
1157 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1158 of a template class, we want to check if DECL is a specialization
1160 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1161 && DECL_TEMPLATE_INFO (friend_decl
)
1162 && !DECL_USE_TEMPLATE (friend_decl
))
1164 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1165 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1166 need_template
= false;
1168 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1169 && !PRIMARY_TEMPLATE_P (friend_decl
))
1170 need_template
= false;
1172 /* There is nothing to do if this is not a template friend. */
1173 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1176 if (is_specialization_of (decl
, friend_decl
))
1180 A member of a class template may be declared to be a friend of a
1181 non-template class. In this case, the corresponding member of
1182 every specialization of the class template is a friend of the
1183 class granting friendship.
1185 For example, given a template friend declaration
1187 template <class T> friend void A<T>::f();
1189 the member function below is considered a friend
1191 template <> struct A<int> {
1195 For this type of template friend, TEMPLATE_DEPTH below will be
1196 nonzero. To determine if DECL is a friend of FRIEND, we first
1197 check if the enclosing class is a specialization of another. */
1199 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1201 && DECL_CLASS_SCOPE_P (decl
)
1202 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1203 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1205 /* Next, we check the members themselves. In order to handle
1206 a few tricky cases, such as when FRIEND_DECL's are
1208 template <class T> friend void A<T>::g(T t);
1209 template <class T> template <T t> friend void A<T>::h();
1213 void A<int>::g(int);
1214 template <int> void A<int>::h();
1216 we need to figure out ARGS, the template arguments from
1217 the context of DECL. This is required for template substitution
1218 of `T' in the function parameter of `g' and template parameter
1219 of `h' in the above examples. Here ARGS corresponds to `int'. */
1221 tree context
= DECL_CONTEXT (decl
);
1222 tree args
= NULL_TREE
;
1223 int current_depth
= 0;
1225 while (current_depth
< template_depth
)
1227 if (CLASSTYPE_TEMPLATE_INFO (context
))
1229 if (current_depth
== 0)
1230 args
= TYPE_TI_ARGS (context
);
1232 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1235 context
= TYPE_CONTEXT (context
);
1238 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1243 tree friend_args_type
;
1244 tree decl_args_type
;
1246 /* Make sure that both DECL and FRIEND_DECL are templates or
1248 is_template
= DECL_TEMPLATE_INFO (decl
)
1249 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1250 if (need_template
^ is_template
)
1252 else if (is_template
)
1254 /* If both are templates, check template parameter list. */
1256 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1258 if (!comp_template_parms
1259 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1263 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1266 decl_type
= TREE_TYPE (decl
);
1268 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1269 tf_none
, NULL_TREE
);
1270 if (friend_type
== error_mark_node
)
1273 /* Check if return types match. */
1274 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1277 /* Check if function parameter types match, ignoring the
1278 `this' parameter. */
1279 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1280 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1281 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1282 friend_args_type
= TREE_CHAIN (friend_args_type
);
1283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1284 decl_args_type
= TREE_CHAIN (decl_args_type
);
1286 return compparms (decl_args_type
, friend_args_type
);
1290 /* DECL is a TYPE_DECL */
1292 tree decl_type
= TREE_TYPE (decl
);
1294 /* Make sure that both DECL and FRIEND_DECL are templates or
1297 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1300 if (need_template
^ is_template
)
1302 else if (is_template
)
1305 /* If both are templates, check the name of the two
1306 TEMPLATE_DECL's first because is_friend didn't. */
1307 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1308 != DECL_NAME (friend_decl
))
1311 /* Now check template parameter list. */
1313 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1315 return comp_template_parms
1316 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1320 return (DECL_NAME (decl
)
1321 == DECL_NAME (friend_decl
));
1327 /* Register the specialization SPEC as a specialization of TMPL with
1328 the indicated ARGS. IS_FRIEND indicates whether the specialization
1329 is actually just a friend declaration. Returns SPEC, or an
1330 equivalent prior declaration, if available.
1332 We also store instantiations of field packs in the hash table, even
1333 though they are not themselves templates, to make lookup easier. */
1336 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1343 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1344 || (TREE_CODE (tmpl
) == FIELD_DECL
1345 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1347 if (TREE_CODE (spec
) == FUNCTION_DECL
1348 && uses_template_parms (DECL_TI_ARGS (spec
)))
1349 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1350 register it; we want the corresponding TEMPLATE_DECL instead.
1351 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1352 the more obvious `uses_template_parms (spec)' to avoid problems
1353 with default function arguments. In particular, given
1354 something like this:
1356 template <class T> void f(T t1, T t = T())
1358 the default argument expression is not substituted for in an
1359 instantiation unless and until it is actually needed. */
1362 if (optimize_specialization_lookup_p (tmpl
))
1363 /* We don't put these specializations in the hash table, but we might
1364 want to give an error about a mismatch. */
1365 fn
= retrieve_specialization (tmpl
, args
, 0);
1373 hash
= hash_specialization (&elt
);
1376 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1378 fn
= ((spec_entry
*) *slot
)->spec
;
1383 /* We can sometimes try to re-register a specialization that we've
1384 already got. In particular, regenerate_decl_from_template calls
1385 duplicate_decls which will update the specialization list. But,
1386 we'll still get called again here anyhow. It's more convenient
1387 to simply allow this than to try to prevent it. */
1390 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1392 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1394 if (DECL_ODR_USED (fn
)
1395 || DECL_EXPLICIT_INSTANTIATION (fn
))
1397 error ("specialization of %qD after instantiation",
1399 return error_mark_node
;
1404 /* This situation should occur only if the first
1405 specialization is an implicit instantiation, the
1406 second is an explicit specialization, and the
1407 implicit instantiation has not yet been used. That
1408 situation can occur if we have implicitly
1409 instantiated a member function and then specialized
1412 We can also wind up here if a friend declaration that
1413 looked like an instantiation turns out to be a
1416 template <class T> void foo(T);
1417 class S { friend void foo<>(int) };
1418 template <> void foo(int);
1420 We transform the existing DECL in place so that any
1421 pointers to it become pointers to the updated
1424 If there was a definition for the template, but not
1425 for the specialization, we want this to look as if
1426 there were no definition, and vice versa. */
1427 DECL_INITIAL (fn
) = NULL_TREE
;
1428 duplicate_decls (spec
, fn
, is_friend
);
1429 /* The call to duplicate_decls will have applied
1432 An explicit specialization of a function template
1433 is inline only if it is explicitly declared to be,
1434 and independently of whether its function template
1437 to the primary function; now copy the inline bits to
1438 the various clones. */
1439 FOR_EACH_CLONE (clone
, fn
)
1441 DECL_DECLARED_INLINE_P (clone
)
1442 = DECL_DECLARED_INLINE_P (fn
);
1443 DECL_SOURCE_LOCATION (clone
)
1444 = DECL_SOURCE_LOCATION (fn
);
1445 DECL_DELETED_FN (clone
)
1446 = DECL_DELETED_FN (fn
);
1448 check_specialization_namespace (tmpl
);
1453 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1455 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1456 /* Dup decl failed, but this is a new definition. Set the
1457 line number so any errors match this new
1459 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1465 return duplicate_decls (spec
, fn
, is_friend
);
1467 /* A specialization must be declared in the same namespace as the
1468 template it is specializing. */
1469 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1470 && !check_specialization_namespace (tmpl
))
1471 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1473 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1475 spec_entry
*entry
= ggc_alloc_spec_entry ();
1476 gcc_assert (tmpl
&& args
&& spec
);
1479 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1480 && PRIMARY_TEMPLATE_P (tmpl
)
1481 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1482 /* TMPL is a forward declaration of a template function; keep a list
1483 of all specializations in case we need to reassign them to a friend
1484 template later in tsubst_friend_function. */
1485 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1486 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1492 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1493 TMPL and ARGS members, ignores SPEC. */
1495 int comparing_specializations
;
1498 eq_specializations (const void *p1
, const void *p2
)
1500 const spec_entry
*e1
= (const spec_entry
*)p1
;
1501 const spec_entry
*e2
= (const spec_entry
*)p2
;
1504 ++comparing_specializations
;
1505 equal
= (e1
->tmpl
== e2
->tmpl
1506 && comp_template_args (e1
->args
, e2
->args
));
1507 --comparing_specializations
;
1512 /* Returns a hash for a template TMPL and template arguments ARGS. */
1515 hash_tmpl_and_args (tree tmpl
, tree args
)
1517 hashval_t val
= DECL_UID (tmpl
);
1518 return iterative_hash_template_arg (args
, val
);
1521 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1525 hash_specialization (const void *p
)
1527 const spec_entry
*e
= (const spec_entry
*)p
;
1528 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1531 /* Recursively calculate a hash value for a template argument ARG, for use
1532 in the hash tables of template specializations. */
1535 iterative_hash_template_arg (tree arg
, hashval_t val
)
1537 unsigned HOST_WIDE_INT i
;
1538 enum tree_code code
;
1541 if (arg
== NULL_TREE
)
1542 return iterative_hash_object (arg
, val
);
1547 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1548 /* We can get one of these when re-hashing a previous entry in the middle
1549 of substituting into a pack expansion. Just look through it. */
1550 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1552 code
= TREE_CODE (arg
);
1553 tclass
= TREE_CODE_CLASS (code
);
1555 val
= iterative_hash_object (code
, val
);
1562 case IDENTIFIER_NODE
:
1563 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1567 int i
, len
= TREE_VEC_LENGTH (arg
);
1568 for (i
= 0; i
< len
; ++i
)
1569 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1573 case TYPE_PACK_EXPANSION
:
1574 case EXPR_PACK_EXPANSION
:
1575 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1576 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1578 case TYPE_ARGUMENT_PACK
:
1579 case NONTYPE_ARGUMENT_PACK
:
1580 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1583 for (; arg
; arg
= TREE_CHAIN (arg
))
1584 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1588 for (; arg
; arg
= OVL_NEXT (arg
))
1589 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1595 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1597 val
= iterative_hash_template_arg (field
, val
);
1598 val
= iterative_hash_template_arg (value
, val
);
1604 if (!DECL_ARTIFICIAL (arg
))
1606 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1607 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1609 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1612 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1615 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1616 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1618 case TEMPLATE_PARM_INDEX
:
1619 val
= iterative_hash_template_arg
1620 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1621 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1622 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1625 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1626 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1627 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1630 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1632 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1636 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1637 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1638 val
= iterative_hash_object (code
, val
);
1639 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1642 /* A lambda can't appear in a template arg, but don't crash on
1644 gcc_assert (seen_error ());
1648 case IMPLICIT_CONV_EXPR
:
1649 case STATIC_CAST_EXPR
:
1650 case REINTERPRET_CAST_EXPR
:
1651 case CONST_CAST_EXPR
:
1652 case DYNAMIC_CAST_EXPR
:
1654 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1655 /* Now hash operands as usual. */
1665 if (TYPE_CANONICAL (arg
))
1666 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1668 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1669 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1670 /* Otherwise just compare the types during lookup. */
1673 case tcc_declaration
:
1675 return iterative_hash_expr (arg
, val
);
1678 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1680 unsigned n
= cp_tree_operand_length (arg
);
1681 for (i
= 0; i
< n
; ++i
)
1682 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1690 /* Unregister the specialization SPEC as a specialization of TMPL.
1691 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1692 if the SPEC was listed as a specialization of TMPL.
1694 Note that SPEC has been ggc_freed, so we can't look inside it. */
1697 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1702 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1703 elt
.args
= TI_ARGS (tinfo
);
1704 elt
.spec
= NULL_TREE
;
1706 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1709 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1710 gcc_assert (new_spec
!= NULL_TREE
);
1711 entry
->spec
= new_spec
;
1718 /* Like register_specialization, but for local declarations. We are
1719 registering SPEC, an instantiation of TMPL. */
1722 register_local_specialization (tree spec
, tree tmpl
)
1726 slot
= pointer_map_insert (local_specializations
, tmpl
);
1730 /* TYPE is a class type. Returns true if TYPE is an explicitly
1731 specialized class. */
1734 explicit_class_specialization_p (tree type
)
1736 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1738 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1741 /* Print the list of functions at FNS, going through all the overloads
1742 for each element of the list. Alternatively, FNS can not be a
1743 TREE_LIST, in which case it will be printed together with all the
1746 MORE and *STR should respectively be FALSE and NULL when the function
1747 is called from the outside. They are used internally on recursive
1748 calls. print_candidates manages the two parameters and leaves NULL
1749 in *STR when it ends. */
1752 print_candidates_1 (tree fns
, bool more
, const char **str
)
1755 char *spaces
= NULL
;
1757 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1758 if (TREE_CODE (fn
) == TREE_LIST
)
1760 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1761 print_candidates_1 (TREE_VALUE (fn2
),
1762 TREE_CHAIN (fn2
) || more
, str
);
1766 tree cand
= OVL_CURRENT (fn
);
1769 /* Pick the prefix string. */
1770 if (!more
&& !OVL_NEXT (fns
))
1772 inform (DECL_SOURCE_LOCATION (cand
),
1773 "candidate is: %#D", cand
);
1777 *str
= _("candidates are:");
1778 spaces
= get_spaces (*str
);
1780 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1781 *str
= spaces
? spaces
: *str
;
1791 /* Print the list of candidate FNS in an error message. FNS can also
1792 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1795 print_candidates (tree fns
)
1797 const char *str
= NULL
;
1798 print_candidates_1 (fns
, false, &str
);
1799 gcc_assert (str
== NULL
);
1802 /* Returns the template (one of the functions given by TEMPLATE_ID)
1803 which can be specialized to match the indicated DECL with the
1804 explicit template args given in TEMPLATE_ID. The DECL may be
1805 NULL_TREE if none is available. In that case, the functions in
1806 TEMPLATE_ID are non-members.
1808 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1809 specialization of a member template.
1811 The TEMPLATE_COUNT is the number of references to qualifying
1812 template classes that appeared in the name of the function. See
1813 check_explicit_specialization for a more accurate description.
1815 TSK indicates what kind of template declaration (if any) is being
1816 declared. TSK_TEMPLATE indicates that the declaration given by
1817 DECL, though a FUNCTION_DECL, has template parameters, and is
1818 therefore a template function.
1820 The template args (those explicitly specified and those deduced)
1821 are output in a newly created vector *TARGS_OUT.
1823 If it is impossible to determine the result, an error message is
1824 issued. The error_mark_node is returned to indicate failure. */
1827 determine_specialization (tree template_id
,
1830 int need_member_template
,
1836 tree explicit_targs
;
1837 tree candidates
= NULL_TREE
;
1838 /* A TREE_LIST of templates of which DECL may be a specialization.
1839 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1840 corresponding TREE_PURPOSE is the set of template arguments that,
1841 when used to instantiate the template, would produce a function
1842 with the signature of DECL. */
1843 tree templates
= NULL_TREE
;
1845 cp_binding_level
*b
;
1847 *targs_out
= NULL_TREE
;
1849 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1850 return error_mark_node
;
1852 /* We shouldn't be specializing a member template of an
1853 unspecialized class template; we already gave an error in
1854 check_specialization_scope, now avoid crashing. */
1855 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1856 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1858 gcc_assert (errorcount
);
1859 return error_mark_node
;
1862 fns
= TREE_OPERAND (template_id
, 0);
1863 explicit_targs
= TREE_OPERAND (template_id
, 1);
1865 if (fns
== error_mark_node
)
1866 return error_mark_node
;
1868 /* Check for baselinks. */
1869 if (BASELINK_P (fns
))
1870 fns
= BASELINK_FUNCTIONS (fns
);
1872 if (!is_overloaded_fn (fns
))
1874 error ("%qD is not a function template", fns
);
1875 return error_mark_node
;
1878 /* Count the number of template headers specified for this
1881 for (b
= current_binding_level
;
1882 b
->kind
== sk_template_parms
;
1886 for (; fns
; fns
= OVL_NEXT (fns
))
1888 tree fn
= OVL_CURRENT (fns
);
1890 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1892 tree decl_arg_types
;
1896 /* In case of explicit specialization, we need to check if
1897 the number of template headers appearing in the specialization
1898 is correct. This is usually done in check_explicit_specialization,
1899 but the check done there cannot be exhaustive when specializing
1900 member functions. Consider the following code:
1902 template <> void A<int>::f(int);
1903 template <> template <> void A<int>::f(int);
1905 Assuming that A<int> is not itself an explicit specialization
1906 already, the first line specializes "f" which is a non-template
1907 member function, whilst the second line specializes "f" which
1908 is a template member function. So both lines are syntactically
1909 correct, and check_explicit_specialization does not reject
1912 Here, we can do better, as we are matching the specialization
1913 against the declarations. We count the number of template
1914 headers, and we check if they match TEMPLATE_COUNT + 1
1915 (TEMPLATE_COUNT is the number of qualifying template classes,
1916 plus there must be another header for the member template
1919 Notice that if header_count is zero, this is not a
1920 specialization but rather a template instantiation, so there
1921 is no check we can perform here. */
1922 if (header_count
&& header_count
!= template_count
+ 1)
1925 /* Check that the number of template arguments at the
1926 innermost level for DECL is the same as for FN. */
1927 if (current_binding_level
->kind
== sk_template_parms
1928 && !current_binding_level
->explicit_spec_p
1929 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1930 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1931 (current_template_parms
))))
1934 /* DECL might be a specialization of FN. */
1935 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1936 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1938 /* For a non-static member function, we need to make sure
1939 that the const qualification is the same. Since
1940 get_bindings does not try to merge the "this" parameter,
1941 we must do the comparison explicitly. */
1942 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1943 && !same_type_p (TREE_VALUE (fn_arg_types
),
1944 TREE_VALUE (decl_arg_types
)))
1947 /* Skip the "this" parameter and, for constructors of
1948 classes with virtual bases, the VTT parameter. A
1949 full specialization of a constructor will have a VTT
1950 parameter, but a template never will. */
1952 = skip_artificial_parms_for (decl
, decl_arg_types
);
1954 = skip_artificial_parms_for (fn
, fn_arg_types
);
1956 /* Function templates cannot be specializations; there are
1957 no partial specializations of functions. Therefore, if
1958 the type of DECL does not match FN, there is no
1960 if (tsk
== tsk_template
)
1962 if (compparms (fn_arg_types
, decl_arg_types
))
1963 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1967 /* See whether this function might be a specialization of this
1968 template. Suppress access control because we might be trying
1969 to make this specialization a friend, and we have already done
1970 access control for the declaration of the specialization. */
1971 push_deferring_access_checks (dk_no_check
);
1972 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1973 pop_deferring_access_checks ();
1976 /* We cannot deduce template arguments that when used to
1977 specialize TMPL will produce DECL. */
1980 /* Make sure that the deduced arguments actually work. */
1981 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1982 if (insttype
== error_mark_node
)
1985 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1986 if (!compparms (fn_arg_types
, decl_arg_types
))
1989 /* Save this template, and the arguments deduced. */
1990 templates
= tree_cons (targs
, fn
, templates
);
1992 else if (need_member_template
)
1993 /* FN is an ordinary member function, and we need a
1994 specialization of a member template. */
1996 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1997 /* We can get IDENTIFIER_NODEs here in certain erroneous
2000 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2001 /* This is just an ordinary non-member function. Nothing can
2002 be a specialization of that. */
2004 else if (DECL_ARTIFICIAL (fn
))
2005 /* Cannot specialize functions that are created implicitly. */
2009 tree decl_arg_types
;
2011 /* This is an ordinary member function. However, since
2012 we're here, we can assume its enclosing class is a
2013 template class. For example,
2015 template <typename T> struct S { void f(); };
2016 template <> void S<int>::f() {}
2018 Here, S<int>::f is a non-template, but S<int> is a
2019 template class. If FN has the same type as DECL, we
2020 might be in business. */
2022 if (!DECL_TEMPLATE_INFO (fn
))
2023 /* Its enclosing class is an explicit specialization
2024 of a template class. This is not a candidate. */
2027 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2028 TREE_TYPE (TREE_TYPE (fn
))))
2029 /* The return types differ. */
2032 /* Adjust the type of DECL in case FN is a static member. */
2033 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2034 if (DECL_STATIC_FUNCTION_P (fn
)
2035 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2036 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2038 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2041 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2045 if (templates
&& TREE_CHAIN (templates
))
2051 It is possible for a specialization with a given function
2052 signature to be instantiated from more than one function
2053 template. In such cases, explicit specification of the
2054 template arguments must be used to uniquely identify the
2055 function template specialization being specialized.
2057 Note that here, there's no suggestion that we're supposed to
2058 determine which of the candidate templates is most
2059 specialized. However, we, also have:
2063 Partial ordering of overloaded function template
2064 declarations is used in the following contexts to select
2065 the function template to which a function template
2066 specialization refers:
2068 -- when an explicit specialization refers to a function
2071 So, we do use the partial ordering rules, at least for now.
2072 This extension can only serve to make invalid programs valid,
2073 so it's safe. And, there is strong anecdotal evidence that
2074 the committee intended the partial ordering rules to apply;
2075 the EDG front end has that behavior, and John Spicer claims
2076 that the committee simply forgot to delete the wording in
2077 [temp.expl.spec]. */
2078 tree tmpl
= most_specialized_instantiation (templates
);
2079 if (tmpl
!= error_mark_node
)
2082 TREE_CHAIN (templates
) = NULL_TREE
;
2086 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2088 error ("template-id %qD for %q+D does not match any template "
2089 "declaration", template_id
, decl
);
2090 if (header_count
&& header_count
!= template_count
+ 1)
2091 inform (input_location
, "saw %d %<template<>%>, need %d for "
2092 "specializing a member function template",
2093 header_count
, template_count
+ 1);
2094 return error_mark_node
;
2096 else if ((templates
&& TREE_CHAIN (templates
))
2097 || (candidates
&& TREE_CHAIN (candidates
))
2098 || (templates
&& candidates
))
2100 error ("ambiguous template specialization %qD for %q+D",
2102 candidates
= chainon (candidates
, templates
);
2103 print_candidates (candidates
);
2104 return error_mark_node
;
2107 /* We have one, and exactly one, match. */
2110 tree fn
= TREE_VALUE (candidates
);
2111 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2112 /* DECL is a re-declaration or partial instantiation of a template
2114 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2116 /* It was a specialization of an ordinary member function in a
2118 return DECL_TI_TEMPLATE (fn
);
2121 /* It was a specialization of a template. */
2122 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2123 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2125 *targs_out
= copy_node (targs
);
2126 SET_TMPL_ARGS_LEVEL (*targs_out
,
2127 TMPL_ARGS_DEPTH (*targs_out
),
2128 TREE_PURPOSE (templates
));
2131 *targs_out
= TREE_PURPOSE (templates
);
2132 return TREE_VALUE (templates
);
2135 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2136 but with the default argument values filled in from those in the
2140 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2143 tree new_spec_types
;
2148 if (spec_types
== void_list_node
)
2149 return void_list_node
;
2151 /* Substitute into the rest of the list. */
2153 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2154 TREE_CHAIN (tmpl_types
));
2156 /* Add the default argument for this parameter. */
2157 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2158 TREE_VALUE (spec_types
),
2162 /* DECL is an explicit specialization. Replicate default arguments
2163 from the template it specializes. (That way, code like:
2165 template <class T> void f(T = 3);
2166 template <> void f(double);
2169 works, as required.) An alternative approach would be to look up
2170 the correct default arguments at the call-site, but this approach
2171 is consistent with how implicit instantiations are handled. */
2174 copy_default_args_to_explicit_spec (tree decl
)
2179 tree new_spec_types
;
2183 tree object_type
= NULL_TREE
;
2184 tree in_charge
= NULL_TREE
;
2185 tree vtt
= NULL_TREE
;
2187 /* See if there's anything we need to do. */
2188 tmpl
= DECL_TI_TEMPLATE (decl
);
2189 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2190 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2191 if (TREE_PURPOSE (t
))
2196 old_type
= TREE_TYPE (decl
);
2197 spec_types
= TYPE_ARG_TYPES (old_type
);
2199 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2201 /* Remove the this pointer, but remember the object's type for
2203 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2204 spec_types
= TREE_CHAIN (spec_types
);
2205 tmpl_types
= TREE_CHAIN (tmpl_types
);
2207 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2209 /* DECL may contain more parameters than TMPL due to the extra
2210 in-charge parameter in constructors and destructors. */
2211 in_charge
= spec_types
;
2212 spec_types
= TREE_CHAIN (spec_types
);
2214 if (DECL_HAS_VTT_PARM_P (decl
))
2217 spec_types
= TREE_CHAIN (spec_types
);
2221 /* Compute the merged default arguments. */
2223 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2225 /* Compute the new FUNCTION_TYPE. */
2229 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2234 /* Put the in-charge parameter back. */
2235 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2236 TREE_VALUE (in_charge
),
2239 new_type
= build_method_type_directly (object_type
,
2240 TREE_TYPE (old_type
),
2244 new_type
= build_function_type (TREE_TYPE (old_type
),
2246 new_type
= cp_build_type_attribute_variant (new_type
,
2247 TYPE_ATTRIBUTES (old_type
));
2248 new_type
= build_exception_variant (new_type
,
2249 TYPE_RAISES_EXCEPTIONS (old_type
));
2250 TREE_TYPE (decl
) = new_type
;
2253 /* Return the number of template headers we expect to see for a definition
2254 or specialization of CTYPE or one of its non-template members. */
2257 num_template_headers_for_class (tree ctype
)
2259 int num_templates
= 0;
2261 while (ctype
&& CLASS_TYPE_P (ctype
))
2263 /* You're supposed to have one `template <...>' for every
2264 template class, but you don't need one for a full
2265 specialization. For example:
2267 template <class T> struct S{};
2268 template <> struct S<int> { void f(); };
2269 void S<int>::f () {}
2271 is correct; there shouldn't be a `template <>' for the
2272 definition of `S<int>::f'. */
2273 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2274 /* If CTYPE does not have template information of any
2275 kind, then it is not a template, nor is it nested
2276 within a template. */
2278 if (explicit_class_specialization_p (ctype
))
2280 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2283 ctype
= TYPE_CONTEXT (ctype
);
2286 return num_templates
;
2289 /* Do a simple sanity check on the template headers that precede the
2290 variable declaration DECL. */
2293 check_template_variable (tree decl
)
2295 tree ctx
= CP_DECL_CONTEXT (decl
);
2296 int wanted
= num_template_headers_for_class (ctx
);
2297 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2298 permerror (DECL_SOURCE_LOCATION (decl
),
2299 "%qD is not a static data member of a class template", decl
);
2300 else if (template_header_count
> wanted
)
2302 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2303 "too many template headers for %D (should be %d)",
2305 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2306 inform (DECL_SOURCE_LOCATION (decl
),
2307 "members of an explicitly specialized class are defined "
2308 "without a template header");
2312 /* Check to see if the function just declared, as indicated in
2313 DECLARATOR, and in DECL, is a specialization of a function
2314 template. We may also discover that the declaration is an explicit
2315 instantiation at this point.
2317 Returns DECL, or an equivalent declaration that should be used
2318 instead if all goes well. Issues an error message if something is
2319 amiss. Returns error_mark_node if the error is not easily
2322 FLAGS is a bitmask consisting of the following flags:
2324 2: The function has a definition.
2325 4: The function is a friend.
2327 The TEMPLATE_COUNT is the number of references to qualifying
2328 template classes that appeared in the name of the function. For
2331 template <class T> struct S { void f(); };
2334 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2335 classes are not counted in the TEMPLATE_COUNT, so that in
2337 template <class T> struct S {};
2338 template <> struct S<int> { void f(); }
2339 template <> void S<int>::f();
2341 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2342 invalid; there should be no template <>.)
2344 If the function is a specialization, it is marked as such via
2345 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2346 is set up correctly, and it is added to the list of specializations
2347 for that template. */
2350 check_explicit_specialization (tree declarator
,
2355 int have_def
= flags
& 2;
2356 int is_friend
= flags
& 4;
2357 int specialization
= 0;
2358 int explicit_instantiation
= 0;
2359 int member_specialization
= 0;
2360 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2361 tree dname
= DECL_NAME (decl
);
2366 if (!processing_specialization
)
2369 tsk
= tsk_excessive_parms
;
2372 tsk
= current_tmpl_spec_kind (template_count
);
2377 if (processing_specialization
)
2380 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2382 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2385 /* This could be something like:
2387 template <class T> void f(T);
2388 class S { friend void f<>(int); } */
2392 /* This case handles bogus declarations like template <>
2393 template <class T> void f<int>(); */
2395 error ("template-id %qD in declaration of primary template",
2402 case tsk_invalid_member_spec
:
2403 /* The error has already been reported in
2404 check_specialization_scope. */
2405 return error_mark_node
;
2407 case tsk_invalid_expl_inst
:
2408 error ("template parameter list used in explicit instantiation");
2414 error ("definition provided for explicit instantiation");
2416 explicit_instantiation
= 1;
2419 case tsk_excessive_parms
:
2420 case tsk_insufficient_parms
:
2421 if (tsk
== tsk_excessive_parms
)
2422 error ("too many template parameter lists in declaration of %qD",
2424 else if (template_header_count
)
2425 error("too few template parameter lists in declaration of %qD", decl
);
2427 error("explicit specialization of %qD must be introduced by "
2428 "%<template <>%>", decl
);
2432 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2434 member_specialization
= 1;
2440 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2442 /* This case handles bogus declarations like template <>
2443 template <class T> void f<int>(); */
2445 if (uses_template_parms (declarator
))
2446 error ("function template partial specialization %qD "
2447 "is not allowed", declarator
);
2449 error ("template-id %qD in declaration of primary template",
2454 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2455 /* This is a specialization of a member template, without
2456 specialization the containing class. Something like:
2458 template <class T> struct S {
2459 template <class U> void f (U);
2461 template <> template <class U> void S<int>::f(U) {}
2463 That's a specialization -- but of the entire template. */
2471 if (specialization
|| member_specialization
)
2473 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2474 for (; t
; t
= TREE_CHAIN (t
))
2475 if (TREE_PURPOSE (t
))
2477 permerror (input_location
,
2478 "default argument specified in explicit specialization");
2483 if (specialization
|| member_specialization
|| explicit_instantiation
)
2485 tree tmpl
= NULL_TREE
;
2486 tree targs
= NULL_TREE
;
2488 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2489 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2493 gcc_assert (identifier_p (declarator
));
2498 /* If there is no class context, the explicit instantiation
2499 must be at namespace scope. */
2500 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2502 /* Find the namespace binding, using the declaration
2504 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2506 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2508 error ("%qD is not a template function", dname
);
2509 fns
= error_mark_node
;
2513 tree fn
= OVL_CURRENT (fns
);
2514 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2515 CP_DECL_CONTEXT (fn
)))
2516 error ("%qD is not declared in %qD",
2517 decl
, current_namespace
);
2521 declarator
= lookup_template_function (fns
, NULL_TREE
);
2524 if (declarator
== error_mark_node
)
2525 return error_mark_node
;
2527 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2529 if (!explicit_instantiation
)
2530 /* A specialization in class scope. This is invalid,
2531 but the error will already have been flagged by
2532 check_specialization_scope. */
2533 return error_mark_node
;
2536 /* It's not valid to write an explicit instantiation in
2539 class C { template void f(); }
2541 This case is caught by the parser. However, on
2544 template class C { void f(); };
2546 (which is invalid) we can get here. The error will be
2553 else if (ctype
!= NULL_TREE
2554 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2556 /* Find the list of functions in ctype that have the same
2557 name as the declared function. */
2558 tree name
= TREE_OPERAND (declarator
, 0);
2559 tree fns
= NULL_TREE
;
2562 if (constructor_name_p (name
, ctype
))
2564 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2566 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2567 : !CLASSTYPE_DESTRUCTORS (ctype
))
2569 /* From [temp.expl.spec]:
2571 If such an explicit specialization for the member
2572 of a class template names an implicitly-declared
2573 special member function (clause _special_), the
2574 program is ill-formed.
2576 Similar language is found in [temp.explicit]. */
2577 error ("specialization of implicitly-declared special member function");
2578 return error_mark_node
;
2581 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2584 if (!DECL_CONV_FN_P (decl
))
2586 idx
= lookup_fnfields_1 (ctype
, name
);
2588 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2592 vec
<tree
, va_gc
> *methods
;
2595 /* For a type-conversion operator, we cannot do a
2596 name-based lookup. We might be looking for `operator
2597 int' which will be a specialization of `operator T'.
2598 So, we find *all* the conversion operators, and then
2599 select from them. */
2602 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2604 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2605 methods
->iterate (idx
, &ovl
);
2608 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2609 /* There are no more conversion functions. */
2612 /* Glue all these conversion functions together
2613 with those we already have. */
2614 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2615 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2619 if (fns
== NULL_TREE
)
2621 error ("no member function %qD declared in %qT", name
, ctype
);
2622 return error_mark_node
;
2625 TREE_OPERAND (declarator
, 0) = fns
;
2628 /* Figure out what exactly is being specialized at this point.
2629 Note that for an explicit instantiation, even one for a
2630 member function, we cannot tell apriori whether the
2631 instantiation is for a member template, or just a member
2632 function of a template class. Even if a member template is
2633 being instantiated, the member template arguments may be
2634 elided if they can be deduced from the rest of the
2636 tmpl
= determine_specialization (declarator
, decl
,
2638 member_specialization
,
2642 if (!tmpl
|| tmpl
== error_mark_node
)
2643 /* We couldn't figure out what this declaration was
2645 return error_mark_node
;
2648 tree gen_tmpl
= most_general_template (tmpl
);
2650 if (explicit_instantiation
)
2652 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2653 is done by do_decl_instantiation later. */
2655 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2656 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2658 if (arg_depth
> parm_depth
)
2660 /* If TMPL is not the most general template (for
2661 example, if TMPL is a friend template that is
2662 injected into namespace scope), then there will
2663 be too many levels of TARGS. Remove some of them
2668 new_targs
= make_tree_vec (parm_depth
);
2669 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2670 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2671 = TREE_VEC_ELT (targs
, i
);
2675 return instantiate_template (tmpl
, targs
, tf_error
);
2678 /* If we thought that the DECL was a member function, but it
2679 turns out to be specializing a static member function,
2680 make DECL a static member function as well. */
2681 if (DECL_STATIC_FUNCTION_P (tmpl
)
2682 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2683 revert_static_member_fn (decl
);
2685 /* If this is a specialization of a member template of a
2686 template class, we want to return the TEMPLATE_DECL, not
2687 the specialization of it. */
2688 if (tsk
== tsk_template
)
2690 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2691 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2692 DECL_INITIAL (result
) = NULL_TREE
;
2696 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2697 DECL_SOURCE_LOCATION (result
)
2698 = DECL_SOURCE_LOCATION (decl
);
2699 /* We want to use the argument list specified in the
2700 definition, not in the original declaration. */
2701 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2702 for (parm
= DECL_ARGUMENTS (result
); parm
;
2703 parm
= DECL_CHAIN (parm
))
2704 DECL_CONTEXT (parm
) = result
;
2706 return register_specialization (tmpl
, gen_tmpl
, targs
,
2710 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2711 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2713 /* Inherit default function arguments from the template
2714 DECL is specializing. */
2715 copy_default_args_to_explicit_spec (decl
);
2717 /* This specialization has the same protection as the
2718 template it specializes. */
2719 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2720 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2722 /* 7.1.1-1 [dcl.stc]
2724 A storage-class-specifier shall not be specified in an
2725 explicit specialization...
2727 The parser rejects these, so unless action is taken here,
2728 explicit function specializations will always appear with
2731 The action recommended by the C++ CWG in response to C++
2732 defect report 605 is to make the storage class and linkage
2733 of the explicit specialization match the templated function:
2735 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2737 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2739 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2740 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2742 /* This specialization has the same linkage and visibility as
2743 the function template it specializes. */
2744 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2745 if (! TREE_PUBLIC (decl
))
2747 DECL_INTERFACE_KNOWN (decl
) = 1;
2748 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2750 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2751 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2753 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2754 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2758 /* If DECL is a friend declaration, declared using an
2759 unqualified name, the namespace associated with DECL may
2760 have been set incorrectly. For example, in:
2762 template <typename T> void f(T);
2764 struct S { friend void f<int>(int); }
2767 we will have set the DECL_CONTEXT for the friend
2768 declaration to N, rather than to the global namespace. */
2769 if (DECL_NAMESPACE_SCOPE_P (decl
))
2770 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2772 if (is_friend
&& !have_def
)
2773 /* This is not really a declaration of a specialization.
2774 It's just the name of an instantiation. But, it's not
2775 a request for an instantiation, either. */
2776 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2778 /* Register this specialization so that we can find it
2780 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2782 /* A 'structor should already have clones. */
2783 gcc_assert (decl
== error_mark_node
2784 || !(DECL_CONSTRUCTOR_P (decl
)
2785 || DECL_DESTRUCTOR_P (decl
))
2786 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
2793 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2794 parameters. These are represented in the same format used for
2795 DECL_TEMPLATE_PARMS. */
2798 comp_template_parms (const_tree parms1
, const_tree parms2
)
2803 if (parms1
== parms2
)
2806 for (p1
= parms1
, p2
= parms2
;
2807 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2808 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2810 tree t1
= TREE_VALUE (p1
);
2811 tree t2
= TREE_VALUE (p2
);
2814 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2815 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2817 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2820 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2822 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2823 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2825 /* If either of the template parameters are invalid, assume
2826 they match for the sake of error recovery. */
2827 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2830 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2833 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2834 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2835 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2837 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2842 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2843 /* One set of parameters has more parameters lists than the
2850 /* Determine whether PARM is a parameter pack. */
2853 template_parameter_pack_p (const_tree parm
)
2855 /* Determine if we have a non-type template parameter pack. */
2856 if (TREE_CODE (parm
) == PARM_DECL
)
2857 return (DECL_TEMPLATE_PARM_P (parm
)
2858 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2859 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2860 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2862 /* If this is a list of template parameters, we could get a
2863 TYPE_DECL or a TEMPLATE_DECL. */
2864 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2865 parm
= TREE_TYPE (parm
);
2867 /* Otherwise it must be a type template parameter. */
2868 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2869 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2870 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2873 /* Determine if T is a function parameter pack. */
2876 function_parameter_pack_p (const_tree t
)
2878 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2879 return DECL_PACK_P (t
);
2883 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2884 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2887 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2889 if (! primary_func_tmpl_inst
2890 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2891 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2894 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2897 /* Return true iff the function parameter PARAM_DECL was expanded
2898 from the function parameter pack PACK. */
2901 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2903 if (DECL_ARTIFICIAL (param_decl
)
2904 || !function_parameter_pack_p (pack
))
2907 /* The parameter pack and its pack arguments have the same
2909 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2912 /* Determine whether ARGS describes a variadic template args list,
2913 i.e., one that is terminated by a template argument pack. */
2916 template_args_variadic_p (tree args
)
2921 if (args
== NULL_TREE
)
2924 args
= INNERMOST_TEMPLATE_ARGS (args
);
2925 nargs
= TREE_VEC_LENGTH (args
);
2930 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2932 return ARGUMENT_PACK_P (last_parm
);
2935 /* Generate a new name for the parameter pack name NAME (an
2936 IDENTIFIER_NODE) that incorporates its */
2939 make_ith_pack_parameter_name (tree name
, int i
)
2941 /* Munge the name to include the parameter index. */
2942 #define NUMBUF_LEN 128
2943 char numbuf
[NUMBUF_LEN
];
2947 if (name
== NULL_TREE
)
2949 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2950 newname_len
= IDENTIFIER_LENGTH (name
)
2951 + strlen (numbuf
) + 2;
2952 newname
= (char*)alloca (newname_len
);
2953 snprintf (newname
, newname_len
,
2954 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2955 return get_identifier (newname
);
2958 /* Return true if T is a primary function, class or alias template
2962 primary_template_instantiation_p (const_tree t
)
2967 if (TREE_CODE (t
) == FUNCTION_DECL
)
2968 return DECL_LANG_SPECIFIC (t
)
2969 && DECL_TEMPLATE_INSTANTIATION (t
)
2970 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2971 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2972 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2973 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2974 else if (alias_template_specialization_p (t
))
2979 /* Return true if PARM is a template template parameter. */
2982 template_template_parameter_p (const_tree parm
)
2984 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2987 /* Return true iff PARM is a DECL representing a type template
2991 template_type_parameter_p (const_tree parm
)
2994 && (TREE_CODE (parm
) == TYPE_DECL
2995 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2996 && DECL_TEMPLATE_PARM_P (parm
));
2999 /* Return the template parameters of T if T is a
3000 primary template instantiation, NULL otherwise. */
3003 get_primary_template_innermost_parameters (const_tree t
)
3005 tree parms
= NULL
, template_info
= NULL
;
3007 if ((template_info
= get_template_info (t
))
3008 && primary_template_instantiation_p (t
))
3009 parms
= INNERMOST_TEMPLATE_PARMS
3010 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3015 /* Return the template parameters of the LEVELth level from the full list
3016 of template parameters PARMS. */
3019 get_template_parms_at_level (tree parms
, int level
)
3023 || TREE_CODE (parms
) != TREE_LIST
3024 || level
> TMPL_PARMS_DEPTH (parms
))
3027 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3028 if (TMPL_PARMS_DEPTH (p
) == level
)
3034 /* Returns the template arguments of T if T is a template instantiation,
3038 get_template_innermost_arguments (const_tree t
)
3040 tree args
= NULL
, template_info
= NULL
;
3042 if ((template_info
= get_template_info (t
))
3043 && TI_ARGS (template_info
))
3044 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3049 /* Return the argument pack elements of T if T is a template argument pack,
3053 get_template_argument_pack_elems (const_tree t
)
3055 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3056 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3059 return ARGUMENT_PACK_ARGS (t
);
3062 /* Structure used to track the progress of find_parameter_packs_r. */
3063 struct find_parameter_pack_data
3065 /* TREE_LIST that will contain all of the parameter packs found by
3067 tree
* parameter_packs
;
3069 /* Set of AST nodes that have been visited by the traversal. */
3070 struct pointer_set_t
*visited
;
3073 /* Identifies all of the argument packs that occur in a template
3074 argument and appends them to the TREE_LIST inside DATA, which is a
3075 find_parameter_pack_data structure. This is a subroutine of
3076 make_pack_expansion and uses_parameter_packs. */
3078 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3081 struct find_parameter_pack_data
* ppd
=
3082 (struct find_parameter_pack_data
*)data
;
3083 bool parameter_pack_p
= false;
3085 /* Handle type aliases/typedefs. */
3086 if (TYPE_ALIAS_P (t
))
3088 if (TYPE_TEMPLATE_INFO (t
))
3089 cp_walk_tree (&TYPE_TI_ARGS (t
),
3090 &find_parameter_packs_r
,
3096 /* Identify whether this is a parameter pack or not. */
3097 switch (TREE_CODE (t
))
3099 case TEMPLATE_PARM_INDEX
:
3100 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3101 parameter_pack_p
= true;
3104 case TEMPLATE_TYPE_PARM
:
3105 t
= TYPE_MAIN_VARIANT (t
);
3106 case TEMPLATE_TEMPLATE_PARM
:
3107 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3108 parameter_pack_p
= true;
3113 if (DECL_PACK_P (t
))
3115 /* We don't want to walk into the type of a PARM_DECL,
3116 because we don't want to see the type parameter pack. */
3118 parameter_pack_p
= true;
3122 /* Look through a lambda capture proxy to the field pack. */
3124 if (DECL_HAS_VALUE_EXPR_P (t
))
3126 tree v
= DECL_VALUE_EXPR (t
);
3128 &find_parameter_packs_r
,
3135 parameter_pack_p
= true;
3138 /* Not a parameter pack. */
3142 if (parameter_pack_p
)
3144 /* Add this parameter pack to the list. */
3145 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3149 cp_walk_tree (&TYPE_CONTEXT (t
),
3150 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3152 /* This switch statement will return immediately if we don't find a
3154 switch (TREE_CODE (t
))
3156 case TEMPLATE_PARM_INDEX
:
3159 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3160 /* Check the template itself. */
3161 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3162 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3163 /* Check the template arguments. */
3164 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3169 case TEMPLATE_TYPE_PARM
:
3170 case TEMPLATE_TEMPLATE_PARM
:
3177 if (TYPE_PTRMEMFUNC_P (t
))
3183 if (TYPE_TEMPLATE_INFO (t
))
3184 cp_walk_tree (&TYPE_TI_ARGS (t
),
3185 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3192 cp_walk_tree (&TREE_TYPE (t
),
3193 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3197 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3202 case TYPE_PACK_EXPANSION
:
3203 case EXPR_PACK_EXPANSION
:
3208 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3213 case IDENTIFIER_NODE
:
3214 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3226 /* Determines if the expression or type T uses any parameter packs. */
3228 uses_parameter_packs (tree t
)
3230 tree parameter_packs
= NULL_TREE
;
3231 struct find_parameter_pack_data ppd
;
3232 ppd
.parameter_packs
= ¶meter_packs
;
3233 ppd
.visited
= pointer_set_create ();
3234 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3235 pointer_set_destroy (ppd
.visited
);
3236 return parameter_packs
!= NULL_TREE
;
3239 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3240 representation a base-class initializer into a parameter pack
3241 expansion. If all goes well, the resulting node will be an
3242 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3245 make_pack_expansion (tree arg
)
3248 tree parameter_packs
= NULL_TREE
;
3249 bool for_types
= false;
3250 struct find_parameter_pack_data ppd
;
3252 if (!arg
|| arg
== error_mark_node
)
3255 if (TREE_CODE (arg
) == TREE_LIST
)
3257 /* The only time we will see a TREE_LIST here is for a base
3258 class initializer. In this case, the TREE_PURPOSE will be a
3259 _TYPE node (representing the base class expansion we're
3260 initializing) and the TREE_VALUE will be a TREE_LIST
3261 containing the initialization arguments.
3263 The resulting expansion looks somewhat different from most
3264 expansions. Rather than returning just one _EXPANSION, we
3265 return a TREE_LIST whose TREE_PURPOSE is a
3266 TYPE_PACK_EXPANSION containing the bases that will be
3267 initialized. The TREE_VALUE will be identical to the
3268 original TREE_VALUE, which is a list of arguments that will
3269 be passed to each base. We do not introduce any new pack
3270 expansion nodes into the TREE_VALUE (although it is possible
3271 that some already exist), because the TREE_PURPOSE and
3272 TREE_VALUE all need to be expanded together with the same
3273 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3274 resulting TREE_PURPOSE will mention the parameter packs in
3275 both the bases and the arguments to the bases. */
3278 tree parameter_packs
= NULL_TREE
;
3280 /* Determine which parameter packs will be used by the base
3282 ppd
.visited
= pointer_set_create ();
3283 ppd
.parameter_packs
= ¶meter_packs
;
3284 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3287 if (parameter_packs
== NULL_TREE
)
3289 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3290 pointer_set_destroy (ppd
.visited
);
3291 return error_mark_node
;
3294 if (TREE_VALUE (arg
) != void_type_node
)
3296 /* Collect the sets of parameter packs used in each of the
3297 initialization arguments. */
3298 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3300 /* Determine which parameter packs will be expanded in this
3302 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3307 pointer_set_destroy (ppd
.visited
);
3309 /* Create the pack expansion type for the base type. */
3310 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3311 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3312 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3314 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3315 they will rarely be compared to anything. */
3316 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3318 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3321 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3324 /* Build the PACK_EXPANSION_* node. */
3326 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3327 : make_node (EXPR_PACK_EXPANSION
);
3328 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3329 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3331 /* Propagate type and const-expression information. */
3332 TREE_TYPE (result
) = TREE_TYPE (arg
);
3333 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3336 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3337 they will rarely be compared to anything. */
3338 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3340 /* Determine which parameter packs will be expanded. */
3341 ppd
.parameter_packs
= ¶meter_packs
;
3342 ppd
.visited
= pointer_set_create ();
3343 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3344 pointer_set_destroy (ppd
.visited
);
3346 /* Make sure we found some parameter packs. */
3347 if (parameter_packs
== NULL_TREE
)
3350 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3352 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3353 return error_mark_node
;
3355 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3357 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3362 /* Checks T for any "bare" parameter packs, which have not yet been
3363 expanded, and issues an error if any are found. This operation can
3364 only be done on full expressions or types (e.g., an expression
3365 statement, "if" condition, etc.), because we could have expressions like:
3367 foo(f(g(h(args)))...)
3369 where "args" is a parameter pack. check_for_bare_parameter_packs
3370 should not be called for the subexpressions args, h(args),
3371 g(h(args)), or f(g(h(args))), because we would produce erroneous
3374 Returns TRUE and emits an error if there were bare parameter packs,
3375 returns FALSE otherwise. */
3377 check_for_bare_parameter_packs (tree t
)
3379 tree parameter_packs
= NULL_TREE
;
3380 struct find_parameter_pack_data ppd
;
3382 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3385 if (TREE_CODE (t
) == TYPE_DECL
)
3388 ppd
.parameter_packs
= ¶meter_packs
;
3389 ppd
.visited
= pointer_set_create ();
3390 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3391 pointer_set_destroy (ppd
.visited
);
3393 if (parameter_packs
)
3395 error ("parameter packs not expanded with %<...%>:");
3396 while (parameter_packs
)
3398 tree pack
= TREE_VALUE (parameter_packs
);
3399 tree name
= NULL_TREE
;
3401 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3402 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3403 name
= TYPE_NAME (pack
);
3404 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3405 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3407 name
= DECL_NAME (pack
);
3410 inform (input_location
, " %qD", name
);
3412 inform (input_location
, " <anonymous>");
3414 parameter_packs
= TREE_CHAIN (parameter_packs
);
3423 /* Expand any parameter packs that occur in the template arguments in
3426 expand_template_argument_pack (tree args
)
3428 tree result_args
= NULL_TREE
;
3429 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3430 int num_result_args
= -1;
3431 int non_default_args_count
= -1;
3433 /* First, determine if we need to expand anything, and the number of
3434 slots we'll need. */
3435 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3437 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3438 if (arg
== NULL_TREE
)
3440 if (ARGUMENT_PACK_P (arg
))
3442 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3443 if (num_result_args
< 0)
3444 num_result_args
= in_arg
+ num_packed
;
3446 num_result_args
+= num_packed
;
3450 if (num_result_args
>= 0)
3455 /* If no expansion is necessary, we're done. */
3456 if (num_result_args
< 0)
3459 /* Expand arguments. */
3460 result_args
= make_tree_vec (num_result_args
);
3461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3462 non_default_args_count
=
3463 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3464 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3466 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3467 if (ARGUMENT_PACK_P (arg
))
3469 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3470 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3471 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3472 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3473 if (non_default_args_count
> 0)
3474 non_default_args_count
+= num_packed
- 1;
3478 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3482 if (non_default_args_count
>= 0)
3483 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3487 /* Checks if DECL shadows a template parameter.
3489 [temp.local]: A template-parameter shall not be redeclared within its
3490 scope (including nested scopes).
3492 Emits an error and returns TRUE if the DECL shadows a parameter,
3493 returns FALSE otherwise. */
3496 check_template_shadow (tree decl
)
3500 /* If we're not in a template, we can't possibly shadow a template
3502 if (!current_template_parms
)
3505 /* Figure out what we're shadowing. */
3506 if (TREE_CODE (decl
) == OVERLOAD
)
3507 decl
= OVL_CURRENT (decl
);
3508 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3510 /* If there's no previous binding for this name, we're not shadowing
3511 anything, let alone a template parameter. */
3515 /* If we're not shadowing a template parameter, we're done. Note
3516 that OLDDECL might be an OVERLOAD (or perhaps even an
3517 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3519 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3522 /* We check for decl != olddecl to avoid bogus errors for using a
3523 name inside a class. We check TPFI to avoid duplicate errors for
3524 inline member templates. */
3526 || (DECL_TEMPLATE_PARM_P (decl
)
3527 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3530 /* Don't complain about the injected class name, as we've already
3531 complained about the class itself. */
3532 if (DECL_SELF_REFERENCE_P (decl
))
3535 error ("declaration of %q+#D", decl
);
3536 error (" shadows template parm %q+#D", olddecl
);
3540 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3541 ORIG_LEVEL, DECL, and TYPE. */
3544 build_template_parm_index (int index
,
3550 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3551 TEMPLATE_PARM_IDX (t
) = index
;
3552 TEMPLATE_PARM_LEVEL (t
) = level
;
3553 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3554 TEMPLATE_PARM_DECL (t
) = decl
;
3555 TREE_TYPE (t
) = type
;
3556 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3557 TREE_READONLY (t
) = TREE_READONLY (decl
);
3562 /* Find the canonical type parameter for the given template type
3563 parameter. Returns the canonical type parameter, which may be TYPE
3564 if no such parameter existed. */
3567 canonical_type_parameter (tree type
)
3570 int idx
= TEMPLATE_TYPE_IDX (type
);
3571 if (!canonical_template_parms
)
3572 vec_alloc (canonical_template_parms
, idx
+1);
3574 while (canonical_template_parms
->length () <= (unsigned)idx
)
3575 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3577 list
= (*canonical_template_parms
)[idx
];
3578 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3579 list
= TREE_CHAIN (list
);
3582 return TREE_VALUE (list
);
3585 (*canonical_template_parms
)[idx
]
3586 = tree_cons (NULL_TREE
, type
,
3587 (*canonical_template_parms
)[idx
]);
3592 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3593 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3594 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3595 new one is created. */
3598 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3599 tsubst_flags_t complain
)
3601 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3602 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3603 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3604 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3606 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3609 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3610 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3611 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3612 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3613 DECL_ARTIFICIAL (decl
) = 1;
3614 SET_DECL_TEMPLATE_PARM_P (decl
);
3616 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3617 TEMPLATE_PARM_LEVEL (index
) - levels
,
3618 TEMPLATE_PARM_ORIG_LEVEL (index
),
3620 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3621 TEMPLATE_PARM_PARAMETER_PACK (t
)
3622 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3624 /* Template template parameters need this. */
3625 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3626 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3627 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3631 return TEMPLATE_PARM_DESCENDANTS (index
);
3634 /* Process information from new template parameter PARM and append it
3635 to the LIST being built. This new parameter is a non-type
3636 parameter iff IS_NON_TYPE is true. This new parameter is a
3637 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3638 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3639 parameter list PARM belongs to. This is used used to create a
3640 proper canonical type for the type of PARM that is to be created,
3641 iff PARM is a type. If the size is not known, this parameter shall
3645 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3646 bool is_non_type
, bool is_parameter_pack
)
3653 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3654 defval
= TREE_PURPOSE (parm
);
3658 tree p
= tree_last (list
);
3660 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3663 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3664 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3666 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3676 parm
= TREE_VALUE (parm
);
3678 SET_DECL_TEMPLATE_PARM_P (parm
);
3680 if (TREE_TYPE (parm
) == error_mark_node
)
3682 err_parm_list
= build_tree_list (defval
, parm
);
3683 TREE_VALUE (err_parm_list
) = error_mark_node
;
3684 return chainon (list
, err_parm_list
);
3690 The top-level cv-qualifiers on the template-parameter are
3691 ignored when determining its type. */
3692 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3693 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3695 err_parm_list
= build_tree_list (defval
, parm
);
3696 TREE_VALUE (err_parm_list
) = error_mark_node
;
3697 return chainon (list
, err_parm_list
);
3700 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
3701 /* If we're in a nested template parameter list, the template
3702 template parameter could be a parameter pack. */
3703 && processing_template_parmlist
== 1)
3705 /* This template parameter is not a parameter pack, but it
3706 should be. Complain about "bare" parameter packs. */
3707 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3709 /* Recover by calling this a parameter pack. */
3710 is_parameter_pack
= true;
3714 /* A template parameter is not modifiable. */
3715 TREE_CONSTANT (parm
) = 1;
3716 TREE_READONLY (parm
) = 1;
3717 decl
= build_decl (parm_loc
,
3718 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3719 TREE_CONSTANT (decl
) = 1;
3720 TREE_READONLY (decl
) = 1;
3721 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3722 = build_template_parm_index (idx
, processing_template_decl
,
3723 processing_template_decl
,
3724 decl
, TREE_TYPE (parm
));
3726 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3727 = is_parameter_pack
;
3732 parm
= TREE_VALUE (TREE_VALUE (parm
));
3734 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3736 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3737 /* This is for distinguishing between real templates and template
3738 template parameters */
3739 TREE_TYPE (parm
) = t
;
3740 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3745 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3746 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3747 decl
= build_decl (parm_loc
,
3748 TYPE_DECL
, parm
, t
);
3751 TYPE_NAME (t
) = decl
;
3752 TYPE_STUB_DECL (t
) = decl
;
3754 TEMPLATE_TYPE_PARM_INDEX (t
)
3755 = build_template_parm_index (idx
, processing_template_decl
,
3756 processing_template_decl
,
3757 decl
, TREE_TYPE (parm
));
3758 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3759 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3761 DECL_ARTIFICIAL (decl
) = 1;
3762 SET_DECL_TEMPLATE_PARM_P (decl
);
3764 parm
= build_tree_list (defval
, parm
);
3765 return chainon (list
, parm
);
3768 /* The end of a template parameter list has been reached. Process the
3769 tree list into a parameter vector, converting each parameter into a more
3770 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3774 end_template_parm_list (tree parms
)
3778 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3780 current_template_parms
3781 = tree_cons (size_int (processing_template_decl
),
3782 saved_parmlist
, current_template_parms
);
3784 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3786 next
= TREE_CHAIN (parm
);
3787 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3788 TREE_CHAIN (parm
) = NULL_TREE
;
3791 --processing_template_parmlist
;
3793 return saved_parmlist
;
3796 /* end_template_decl is called after a template declaration is seen. */
3799 end_template_decl (void)
3801 reset_specialization ();
3803 if (! processing_template_decl
)
3806 /* This matches the pushlevel in begin_template_parm_list. */
3809 --processing_template_decl
;
3810 current_template_parms
= TREE_CHAIN (current_template_parms
);
3813 /* Takes a TREE_LIST representing a template parameter and convert it
3814 into an argument suitable to be passed to the type substitution
3815 functions. Note that If the TREE_LIST contains an error_mark
3816 node, the returned argument is error_mark_node. */
3819 template_parm_to_arg (tree t
)
3823 || TREE_CODE (t
) != TREE_LIST
)
3826 if (error_operand_p (TREE_VALUE (t
)))
3827 return error_mark_node
;
3831 if (TREE_CODE (t
) == TYPE_DECL
3832 || TREE_CODE (t
) == TEMPLATE_DECL
)
3836 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3838 /* Turn this argument into a TYPE_ARGUMENT_PACK
3839 with a single element, which expands T. */
3840 tree vec
= make_tree_vec (1);
3841 #ifdef ENABLE_CHECKING
3842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3843 (vec
, TREE_VEC_LENGTH (vec
));
3845 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3847 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3848 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3853 t
= DECL_INITIAL (t
);
3855 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3857 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3858 with a single element, which expands T. */
3859 tree vec
= make_tree_vec (1);
3860 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3861 #ifdef ENABLE_CHECKING
3862 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3863 (vec
, TREE_VEC_LENGTH (vec
));
3865 t
= convert_from_reference (t
);
3866 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3868 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3869 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3870 TREE_TYPE (t
) = type
;
3873 t
= convert_from_reference (t
);
3878 /* Given a set of template parameters, return them as a set of template
3879 arguments. The template parameters are represented as a TREE_VEC, in
3880 the form documented in cp-tree.h for template arguments. */
3883 template_parms_to_args (tree parms
)
3886 tree args
= NULL_TREE
;
3887 int length
= TMPL_PARMS_DEPTH (parms
);
3890 /* If there is only one level of template parameters, we do not
3891 create a TREE_VEC of TREE_VECs. Instead, we return a single
3892 TREE_VEC containing the arguments. */
3894 args
= make_tree_vec (length
);
3896 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3898 tree a
= copy_node (TREE_VALUE (header
));
3901 TREE_TYPE (a
) = NULL_TREE
;
3902 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3903 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3905 #ifdef ENABLE_CHECKING
3906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3910 TREE_VEC_ELT (args
, --l
) = a
;
3915 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3916 /* This can happen for template parms of a template template
3919 template<template<class T, class U> class TT> struct S;
3921 Consider the level of the parms of TT; T and U both have
3922 level 2; TT has no template parm of level 1. So in this case
3923 the first element of full_template_args is NULL_TREE. If we
3924 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3925 of 2. This will make tsubst wrongly consider that T and U
3926 have level 1. Instead, let's create a dummy vector as the
3927 first element of full_template_args so that TMPL_ARGS_DEPTH
3928 returns the correct depth for args. */
3929 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3933 /* Within the declaration of a template, return the currently active
3934 template parameters as an argument TREE_VEC. */
3937 current_template_args (void)
3939 return template_parms_to_args (current_template_parms
);
3942 /* Update the declared TYPE by doing any lookups which were thought to be
3943 dependent, but are not now that we know the SCOPE of the declarator. */
3946 maybe_update_decl_type (tree orig_type
, tree scope
)
3948 tree type
= orig_type
;
3950 if (type
== NULL_TREE
)
3953 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3954 type
= TREE_TYPE (type
);
3956 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3957 && dependent_type_p (type
)
3958 /* Don't bother building up the args in this case. */
3959 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3961 /* tsubst in the args corresponding to the template parameters,
3962 including auto if present. Most things will be unchanged, but
3963 make_typename_type and tsubst_qualified_id will resolve
3964 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3965 tree args
= current_template_args ();
3966 tree auto_node
= type_uses_auto (type
);
3970 tree auto_vec
= make_tree_vec (1);
3971 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3972 args
= add_to_template_args (args
, auto_vec
);
3974 pushed
= push_scope (scope
);
3975 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3980 if (type
== error_mark_node
)
3983 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3985 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3988 type
= TYPE_NAME (type
);
3993 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3994 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3995 a member template. Used by push_template_decl below. */
3998 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4000 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4001 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4002 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4003 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4004 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4009 struct template_parm_data
4011 /* The level of the template parameters we are currently
4015 /* The index of the specialization argument we are currently
4019 /* An array whose size is the number of template parameters. The
4020 elements are nonzero if the parameter has been used in any one
4021 of the arguments processed so far. */
4024 /* An array whose size is the number of template arguments. The
4025 elements are nonzero if the argument makes use of template
4026 parameters of this level. */
4027 int* arg_uses_template_parms
;
4030 /* Subroutine of push_template_decl used to see if each template
4031 parameter in a partial specialization is used in the explicit
4032 argument list. If T is of the LEVEL given in DATA (which is
4033 treated as a template_parm_data*), then DATA->PARMS is marked
4037 mark_template_parm (tree t
, void* data
)
4041 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4043 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4045 level
= TEMPLATE_PARM_LEVEL (t
);
4046 idx
= TEMPLATE_PARM_IDX (t
);
4050 level
= TEMPLATE_TYPE_LEVEL (t
);
4051 idx
= TEMPLATE_TYPE_IDX (t
);
4054 if (level
== tpd
->level
)
4056 tpd
->parms
[idx
] = 1;
4057 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4060 /* Return zero so that for_each_template_parm will continue the
4061 traversal of the tree; we want to mark *every* template parm. */
4065 /* Process the partial specialization DECL. */
4068 process_partial_specialization (tree decl
)
4070 tree type
= TREE_TYPE (decl
);
4071 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4072 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4073 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4074 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4077 int nargs
= TREE_VEC_LENGTH (inner_args
);
4080 bool did_error_intro
= false;
4081 struct template_parm_data tpd
;
4082 struct template_parm_data tpd2
;
4084 gcc_assert (current_template_parms
);
4086 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4087 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4089 /* We check that each of the template parameters given in the
4090 partial specialization is used in the argument list to the
4091 specialization. For example:
4093 template <class T> struct S;
4094 template <class T> struct S<T*>;
4096 The second declaration is OK because `T*' uses the template
4097 parameter T, whereas
4099 template <class T> struct S<int>;
4101 is no good. Even trickier is:
4112 The S2<T> declaration is actually invalid; it is a
4113 full-specialization. Of course,
4116 struct S2<T (*)(U)>;
4118 or some such would have been OK. */
4119 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4120 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4121 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4123 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4124 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4125 for (i
= 0; i
< nargs
; ++i
)
4127 tpd
.current_arg
= i
;
4128 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4129 &mark_template_parm
,
4132 /*include_nondeduced_p=*/false);
4134 for (i
= 0; i
< ntparms
; ++i
)
4135 if (tpd
.parms
[i
] == 0)
4137 /* One of the template parms was not used in the
4139 if (!did_error_intro
)
4141 error ("template parameters not used in partial specialization:");
4142 did_error_intro
= true;
4145 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4148 if (did_error_intro
)
4149 return error_mark_node
;
4151 /* [temp.class.spec]
4153 The argument list of the specialization shall not be identical to
4154 the implicit argument list of the primary template. */
4155 if (comp_template_args
4157 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4159 error ("partial specialization %qT does not specialize any template arguments", type
);
4161 /* A partial specialization that replaces multiple parameters of the
4162 primary template with a pack expansion is less specialized for those
4164 if (nargs
< DECL_NTPARMS (maintmpl
))
4166 error ("partial specialization is not more specialized than the "
4167 "primary template because it replaces multiple parameters "
4168 "with a pack expansion");
4169 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4173 /* [temp.class.spec]
4175 A partially specialized non-type argument expression shall not
4176 involve template parameters of the partial specialization except
4177 when the argument expression is a simple identifier.
4179 The type of a template parameter corresponding to a specialized
4180 non-type argument shall not be dependent on a parameter of the
4183 Also, we verify that pack expansions only occur at the
4184 end of the argument list. */
4185 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4187 for (i
= 0; i
< nargs
; ++i
)
4189 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4190 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4191 tree packed_args
= NULL_TREE
;
4194 if (ARGUMENT_PACK_P (arg
))
4196 /* Extract the arguments from the argument pack. We'll be
4197 iterating over these in the following loop. */
4198 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4199 len
= TREE_VEC_LENGTH (packed_args
);
4202 for (j
= 0; j
< len
; j
++)
4205 /* Get the Jth argument in the parameter pack. */
4206 arg
= TREE_VEC_ELT (packed_args
, j
);
4208 if (PACK_EXPANSION_P (arg
))
4210 /* Pack expansions must come at the end of the
4212 if ((packed_args
&& j
< len
- 1)
4213 || (!packed_args
&& i
< nargs
- 1))
4215 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4216 error ("parameter pack argument %qE must be at the "
4217 "end of the template argument list", arg
);
4219 error ("parameter pack argument %qT must be at the "
4220 "end of the template argument list", arg
);
4224 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4225 /* We only care about the pattern. */
4226 arg
= PACK_EXPANSION_PATTERN (arg
);
4228 if (/* These first two lines are the `non-type' bit. */
4230 && TREE_CODE (arg
) != TEMPLATE_DECL
4231 /* This next two lines are the `argument expression is not just a
4232 simple identifier' condition and also the `specialized
4233 non-type argument' bit. */
4234 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4235 && !(REFERENCE_REF_P (arg
)
4236 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4238 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4239 || (packed_args
&& uses_template_parms (arg
)))
4240 error ("template argument %qE involves template parameter(s)",
4244 /* Look at the corresponding template parameter,
4245 marking which template parameters its type depends
4247 tree type
= TREE_TYPE (parm
);
4251 /* We haven't yet initialized TPD2. Do so now. */
4252 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4253 /* The number of parameters here is the number in the
4254 main template, which, as checked in the assertion
4256 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4258 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4261 /* Mark the template parameters. But this time, we're
4262 looking for the template parameters of the main
4263 template, not in the specialization. */
4264 tpd2
.current_arg
= i
;
4265 tpd2
.arg_uses_template_parms
[i
] = 0;
4266 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4267 for_each_template_parm (type
,
4268 &mark_template_parm
,
4271 /*include_nondeduced_p=*/false);
4273 if (tpd2
.arg_uses_template_parms
[i
])
4275 /* The type depended on some template parameters.
4276 If they are fully specialized in the
4277 specialization, that's OK. */
4280 for (j
= 0; j
< nargs
; ++j
)
4281 if (tpd2
.parms
[j
] != 0
4282 && tpd
.arg_uses_template_parms
[j
])
4285 error_n (input_location
, count
,
4286 "type %qT of template argument %qE depends "
4287 "on a template parameter",
4288 "type %qT of template argument %qE depends "
4289 "on template parameters",
4298 /* We should only get here once. */
4299 gcc_assert (!COMPLETE_TYPE_P (type
));
4301 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4302 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4303 TREE_TYPE (tmpl
) = type
;
4304 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4305 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4306 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4307 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4309 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4310 = tree_cons (specargs
, tmpl
,
4311 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4312 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4314 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4315 inst
= TREE_CHAIN (inst
))
4317 tree inst_type
= TREE_VALUE (inst
);
4318 if (COMPLETE_TYPE_P (inst_type
)
4319 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4321 tree spec
= most_specialized_class (inst_type
, tf_none
);
4322 if (spec
&& TREE_TYPE (spec
) == type
)
4323 permerror (input_location
,
4324 "partial specialization of %qT after instantiation "
4325 "of %qT", type
, inst_type
);
4332 /* PARM is a template parameter of some form; return the corresponding
4333 TEMPLATE_PARM_INDEX. */
4336 get_template_parm_index (tree parm
)
4338 if (TREE_CODE (parm
) == PARM_DECL
4339 || TREE_CODE (parm
) == CONST_DECL
)
4340 parm
= DECL_INITIAL (parm
);
4341 else if (TREE_CODE (parm
) == TYPE_DECL
4342 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4343 parm
= TREE_TYPE (parm
);
4344 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4345 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4346 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4347 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4351 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4352 parameter packs used by the template parameter PARM. */
4355 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4357 /* A type parm can't refer to another parm. */
4358 if (TREE_CODE (parm
) == TYPE_DECL
)
4360 else if (TREE_CODE (parm
) == PARM_DECL
)
4362 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4367 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4369 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4370 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4371 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4374 /* PARM is a template parameter pack. Return any parameter packs used in
4375 its type or the type of any of its template parameters. If there are
4376 any such packs, it will be instantiated into a fixed template parameter
4377 list by partial instantiation rather than be fully deduced. */
4380 fixed_parameter_pack_p (tree parm
)
4382 /* This can only be true in a member template. */
4383 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4385 /* This can only be true for a parameter pack. */
4386 if (!template_parameter_pack_p (parm
))
4388 /* A type parm can't refer to another parm. */
4389 if (TREE_CODE (parm
) == TYPE_DECL
)
4392 tree parameter_packs
= NULL_TREE
;
4393 struct find_parameter_pack_data ppd
;
4394 ppd
.parameter_packs
= ¶meter_packs
;
4395 ppd
.visited
= pointer_set_create ();
4397 fixed_parameter_pack_p_1 (parm
, &ppd
);
4399 pointer_set_destroy (ppd
.visited
);
4400 return parameter_packs
;
4403 /* Check that a template declaration's use of default arguments and
4404 parameter packs is not invalid. Here, PARMS are the template
4405 parameters. IS_PRIMARY is true if DECL is the thing declared by
4406 a primary template. IS_PARTIAL is true if DECL is a partial
4409 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4410 declaration (but not a definition); 1 indicates a declaration, 2
4411 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4412 emitted for extraneous default arguments.
4414 Returns TRUE if there were no errors found, FALSE otherwise. */
4417 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4418 bool is_partial
, int is_friend_decl
)
4421 int last_level_to_check
;
4423 bool no_errors
= true;
4427 A default template-argument shall not be specified in a
4428 function template declaration or a function template definition, nor
4429 in the template-parameter-list of the definition of a member of a
4432 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4433 /* You can't have a function template declaration in a local
4434 scope, nor you can you define a member of a class template in a
4438 if (TREE_CODE (decl
) == TYPE_DECL
4440 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4441 /* A lambda doesn't have an explicit declaration; don't complain
4442 about the parms of the enclosing class. */
4445 if (current_class_type
4446 && !TYPE_BEING_DEFINED (current_class_type
)
4447 && DECL_LANG_SPECIFIC (decl
)
4448 && DECL_DECLARES_FUNCTION_P (decl
)
4449 /* If this is either a friend defined in the scope of the class
4450 or a member function. */
4451 && (DECL_FUNCTION_MEMBER_P (decl
)
4452 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4453 : DECL_FRIEND_CONTEXT (decl
)
4454 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4456 /* And, if it was a member function, it really was defined in
4457 the scope of the class. */
4458 && (!DECL_FUNCTION_MEMBER_P (decl
)
4459 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4460 /* We already checked these parameters when the template was
4461 declared, so there's no need to do it again now. This function
4462 was defined in class scope, but we're processing its body now
4463 that the class is complete. */
4466 /* Core issue 226 (C++0x only): the following only applies to class
4469 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4473 If a template-parameter has a default template-argument, all
4474 subsequent template-parameters shall have a default
4475 template-argument supplied. */
4476 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4478 tree inner_parms
= TREE_VALUE (parm_level
);
4479 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4480 int seen_def_arg_p
= 0;
4483 for (i
= 0; i
< ntparms
; ++i
)
4485 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4487 if (parm
== error_mark_node
)
4490 if (TREE_PURPOSE (parm
))
4492 else if (seen_def_arg_p
4493 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4495 error ("no default argument for %qD", TREE_VALUE (parm
));
4496 /* For better subsequent error-recovery, we indicate that
4497 there should have been a default argument. */
4498 TREE_PURPOSE (parm
) = error_mark_node
;
4501 else if (!is_partial
4503 /* Don't complain about an enclosing partial
4505 && parm_level
== parms
4506 && TREE_CODE (decl
) == TYPE_DECL
4508 && template_parameter_pack_p (TREE_VALUE (parm
))
4509 /* A fixed parameter pack will be partially
4510 instantiated into a fixed length list. */
4511 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4513 /* A primary class template can only have one
4514 parameter pack, at the end of the template
4517 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4518 error ("parameter pack %qE must be at the end of the"
4519 " template parameter list", TREE_VALUE (parm
));
4521 error ("parameter pack %qT must be at the end of the"
4522 " template parameter list",
4523 TREE_TYPE (TREE_VALUE (parm
)));
4525 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4533 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4537 /* For an ordinary class template, default template arguments are
4538 allowed at the innermost level, e.g.:
4539 template <class T = int>
4541 but, in a partial specialization, they're not allowed even
4542 there, as we have in [temp.class.spec]:
4544 The template parameter list of a specialization shall not
4545 contain default template argument values.
4547 So, for a partial specialization, or for a function template
4548 (in C++98/C++03), we look at all of them. */
4551 /* But, for a primary class template that is not a partial
4552 specialization we look at all template parameters except the
4554 parms
= TREE_CHAIN (parms
);
4556 /* Figure out what error message to issue. */
4557 if (is_friend_decl
== 2)
4558 msg
= G_("default template arguments may not be used in function template "
4559 "friend re-declaration");
4560 else if (is_friend_decl
)
4561 msg
= G_("default template arguments may not be used in function template "
4562 "friend declarations");
4563 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4564 msg
= G_("default template arguments may not be used in function templates "
4565 "without -std=c++11 or -std=gnu++11");
4566 else if (is_partial
)
4567 msg
= G_("default template arguments may not be used in "
4568 "partial specializations");
4570 msg
= G_("default argument for template parameter for class enclosing %qD");
4572 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4573 /* If we're inside a class definition, there's no need to
4574 examine the parameters to the class itself. On the one
4575 hand, they will be checked when the class is defined, and,
4576 on the other, default arguments are valid in things like:
4577 template <class T = double>
4578 struct S { template <class U> void f(U); };
4579 Here the default argument for `S' has no bearing on the
4580 declaration of `f'. */
4581 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4583 /* Check everything. */
4584 last_level_to_check
= 0;
4586 for (parm_level
= parms
;
4587 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4588 parm_level
= TREE_CHAIN (parm_level
))
4590 tree inner_parms
= TREE_VALUE (parm_level
);
4594 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4595 for (i
= 0; i
< ntparms
; ++i
)
4597 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4605 if (is_friend_decl
== 2)
4612 /* Clear out the default argument so that we are not
4614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4618 /* At this point, if we're still interested in issuing messages,
4619 they must apply to classes surrounding the object declared. */
4621 msg
= G_("default argument for template parameter for class "
4628 /* Worker for push_template_decl_real, called via
4629 for_each_template_parm. DATA is really an int, indicating the
4630 level of the parameters we are interested in. If T is a template
4631 parameter of that level, return nonzero. */
4634 template_parm_this_level_p (tree t
, void* data
)
4636 int this_level
= *(int *)data
;
4639 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4640 level
= TEMPLATE_PARM_LEVEL (t
);
4642 level
= TEMPLATE_TYPE_LEVEL (t
);
4643 return level
== this_level
;
4646 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4647 parameters given by current_template_args, or reuses a
4648 previously existing one, if appropriate. Returns the DECL, or an
4649 equivalent one, if it is replaced via a call to duplicate_decls.
4651 If IS_FRIEND is true, DECL is a friend declaration. */
4654 push_template_decl_real (tree decl
, bool is_friend
)
4662 int new_template_p
= 0;
4663 /* True if the template is a member template, in the sense of
4665 bool member_template_p
= false;
4667 if (decl
== error_mark_node
|| !current_template_parms
)
4668 return error_mark_node
;
4670 /* See if this is a partial specialization. */
4671 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4672 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4673 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4675 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4679 /* For a friend, we want the context of the friend function, not
4680 the type of which it is a friend. */
4681 ctx
= CP_DECL_CONTEXT (decl
);
4682 else if (CP_DECL_CONTEXT (decl
)
4683 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4684 /* In the case of a virtual function, we want the class in which
4686 ctx
= CP_DECL_CONTEXT (decl
);
4688 /* Otherwise, if we're currently defining some class, the DECL
4689 is assumed to be a member of the class. */
4690 ctx
= current_scope ();
4692 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4695 if (!DECL_CONTEXT (decl
))
4696 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4698 /* See if this is a primary template. */
4699 if (is_friend
&& ctx
4700 && uses_template_parms_level (ctx
, processing_template_decl
))
4701 /* A friend template that specifies a class context, i.e.
4702 template <typename T> friend void A<T>::f();
4706 is_primary
= template_parm_scope_p ();
4710 if (DECL_CLASS_SCOPE_P (decl
))
4711 member_template_p
= true;
4712 if (TREE_CODE (decl
) == TYPE_DECL
4713 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4715 error ("template class without a name");
4716 return error_mark_node
;
4718 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4720 if (DECL_DESTRUCTOR_P (decl
))
4724 A destructor shall not be a member template. */
4725 error ("destructor %qD declared as member template", decl
);
4726 return error_mark_node
;
4728 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4729 && (!prototype_p (TREE_TYPE (decl
))
4730 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4731 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4732 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4733 == void_list_node
)))
4735 /* [basic.stc.dynamic.allocation]
4737 An allocation function can be a function
4738 template. ... Template allocation functions shall
4739 have two or more parameters. */
4740 error ("invalid template declaration of %qD", decl
);
4741 return error_mark_node
;
4744 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4745 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4747 else if (TREE_CODE (decl
) == TYPE_DECL
4748 && TYPE_DECL_ALIAS_P (decl
))
4749 /* alias-declaration */
4750 gcc_assert (!DECL_ARTIFICIAL (decl
));
4753 error ("template declaration of %q#D", decl
);
4754 return error_mark_node
;
4758 /* Check to see that the rules regarding the use of default
4759 arguments are not being violated. */
4760 check_default_tmpl_args (decl
, current_template_parms
,
4761 is_primary
, is_partial
, /*is_friend_decl=*/0);
4763 /* Ensure that there are no parameter packs in the type of this
4764 declaration that have not been expanded. */
4765 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4767 /* Check each of the arguments individually to see if there are
4768 any bare parameter packs. */
4769 tree type
= TREE_TYPE (decl
);
4770 tree arg
= DECL_ARGUMENTS (decl
);
4771 tree argtype
= TYPE_ARG_TYPES (type
);
4773 while (arg
&& argtype
)
4775 if (!DECL_PACK_P (arg
)
4776 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4778 /* This is a PARM_DECL that contains unexpanded parameter
4779 packs. We have already complained about this in the
4780 check_for_bare_parameter_packs call, so just replace
4781 these types with ERROR_MARK_NODE. */
4782 TREE_TYPE (arg
) = error_mark_node
;
4783 TREE_VALUE (argtype
) = error_mark_node
;
4786 arg
= DECL_CHAIN (arg
);
4787 argtype
= TREE_CHAIN (argtype
);
4790 /* Check for bare parameter packs in the return type and the
4791 exception specifiers. */
4792 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4793 /* Errors were already issued, set return type to int
4794 as the frontend doesn't expect error_mark_node as
4796 TREE_TYPE (type
) = integer_type_node
;
4797 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4798 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4800 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4801 && TYPE_DECL_ALIAS_P (decl
))
4802 ? DECL_ORIGINAL_TYPE (decl
)
4803 : TREE_TYPE (decl
)))
4805 TREE_TYPE (decl
) = error_mark_node
;
4806 return error_mark_node
;
4810 return process_partial_specialization (decl
);
4812 args
= current_template_args ();
4815 || TREE_CODE (ctx
) == FUNCTION_DECL
4816 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4817 || (TREE_CODE (decl
) == TYPE_DECL
4818 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4819 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4821 if (DECL_LANG_SPECIFIC (decl
)
4822 && DECL_TEMPLATE_INFO (decl
)
4823 && DECL_TI_TEMPLATE (decl
))
4824 tmpl
= DECL_TI_TEMPLATE (decl
);
4825 /* If DECL is a TYPE_DECL for a class-template, then there won't
4826 be DECL_LANG_SPECIFIC. The information equivalent to
4827 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4828 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4829 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4830 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4832 /* Since a template declaration already existed for this
4833 class-type, we must be redeclaring it here. Make sure
4834 that the redeclaration is valid. */
4835 redeclare_class_template (TREE_TYPE (decl
),
4836 current_template_parms
);
4837 /* We don't need to create a new TEMPLATE_DECL; just use the
4838 one we already had. */
4839 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4843 tmpl
= build_template_decl (decl
, current_template_parms
,
4847 if (DECL_LANG_SPECIFIC (decl
)
4848 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4850 /* A specialization of a member template of a template
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4853 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4854 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4860 tree a
, t
, current
, parms
;
4862 tree tinfo
= get_template_info (decl
);
4866 error ("template definition of non-template %q#D", decl
);
4867 return error_mark_node
;
4870 tmpl
= TI_TEMPLATE (tinfo
);
4872 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4873 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4874 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4875 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4879 /* The declaration is a specialization of a member
4880 template, declared outside the class. Therefore, the
4881 innermost template arguments will be NULL, so we
4882 replace them with the arguments determined by the
4883 earlier call to check_explicit_specialization. */
4884 args
= DECL_TI_ARGS (decl
);
4887 = build_template_decl (decl
, current_template_parms
,
4889 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4890 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4891 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4892 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4893 DECL_TEMPLATE_INFO (new_tmpl
)
4894 = build_template_info (tmpl
, args
);
4896 register_specialization (new_tmpl
,
4897 most_general_template (tmpl
),
4903 /* Make sure the template headers we got make sense. */
4905 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4906 i
= TMPL_PARMS_DEPTH (parms
);
4907 if (TMPL_ARGS_DEPTH (args
) != i
)
4909 error ("expected %d levels of template parms for %q#D, got %d",
4910 i
, decl
, TMPL_ARGS_DEPTH (args
));
4913 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4915 a
= TMPL_ARGS_LEVEL (args
, i
);
4916 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4918 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4920 if (current
== decl
)
4921 error ("got %d template parameters for %q#D",
4922 TREE_VEC_LENGTH (a
), decl
);
4924 error ("got %d template parameters for %q#T",
4925 TREE_VEC_LENGTH (a
), current
);
4926 error (" but %d required", TREE_VEC_LENGTH (t
));
4927 /* Avoid crash in import_export_decl. */
4928 DECL_INTERFACE_KNOWN (decl
) = 1;
4929 return error_mark_node
;
4932 if (current
== decl
)
4934 else if (current
== NULL_TREE
)
4935 /* Can happen in erroneous input. */
4938 current
= get_containing_scope (current
);
4941 /* Check that the parms are used in the appropriate qualifying scopes
4942 in the declarator. */
4943 if (!comp_template_args
4945 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4948 template arguments to %qD do not match original template %qD",
4949 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4950 if (!uses_template_parms (TI_ARGS (tinfo
)))
4951 inform (input_location
, "use template<> for an explicit specialization");
4952 /* Avoid crash in import_export_decl. */
4953 DECL_INTERFACE_KNOWN (decl
) = 1;
4954 return error_mark_node
;
4958 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4959 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4961 /* Push template declarations for global functions and types. Note
4962 that we do not try to push a global template friend declared in a
4963 template class; such a thing may well depend on the template
4964 parameters of the class. */
4965 if (new_template_p
&& !ctx
4966 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4968 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4969 if (tmpl
== error_mark_node
)
4970 return error_mark_node
;
4972 /* Hide template friend classes that haven't been declared yet. */
4973 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4975 DECL_ANTICIPATED (tmpl
) = 1;
4976 DECL_FRIEND_P (tmpl
) = 1;
4982 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4985 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4986 if (DECL_CONV_FN_P (tmpl
))
4988 int depth
= TMPL_PARMS_DEPTH (parms
);
4990 /* It is a conversion operator. See if the type converted to
4991 depends on innermost template operands. */
4993 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4995 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4998 /* Give template template parms a DECL_CONTEXT of the template
4999 for which they are a parameter. */
5000 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5001 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5003 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5004 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5005 DECL_CONTEXT (parm
) = tmpl
;
5009 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5010 back to its most general template. If TMPL is a specialization,
5011 ARGS may only have the innermost set of arguments. Add the missing
5012 argument levels if necessary. */
5013 if (DECL_TEMPLATE_INFO (tmpl
))
5014 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5016 info
= build_template_info (tmpl
, args
);
5018 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5019 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5022 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5023 retrofit_lang_decl (decl
);
5024 if (DECL_LANG_SPECIFIC (decl
))
5025 DECL_TEMPLATE_INFO (decl
) = info
;
5028 return DECL_TEMPLATE_RESULT (tmpl
);
5032 push_template_decl (tree decl
)
5034 return push_template_decl_real (decl
, false);
5037 /* FN is an inheriting constructor that inherits from the constructor
5038 template INHERITED; turn FN into a constructor template with a matching
5042 add_inherited_template_parms (tree fn
, tree inherited
)
5045 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5046 inner_parms
= copy_node (inner_parms
);
5048 = tree_cons (size_int (processing_template_decl
+ 1),
5049 inner_parms
, current_template_parms
);
5050 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5051 tree args
= template_parms_to_args (parms
);
5052 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5053 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5054 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5055 DECL_ARTIFICIAL (tmpl
) = true;
5056 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5060 /* Called when a class template TYPE is redeclared with the indicated
5061 template PARMS, e.g.:
5063 template <class T> struct S;
5064 template <class T> struct S {}; */
5067 redeclare_class_template (tree type
, tree parms
)
5073 if (!TYPE_TEMPLATE_INFO (type
))
5075 error ("%qT is not a template type", type
);
5079 tmpl
= TYPE_TI_TEMPLATE (type
);
5080 if (!PRIMARY_TEMPLATE_P (tmpl
))
5081 /* The type is nested in some template class. Nothing to worry
5082 about here; there are no new template parameters for the nested
5088 error ("template specifiers not specified in declaration of %qD",
5093 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5094 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5096 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5098 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5099 "redeclared with %d template parameter",
5100 "redeclared with %d template parameters",
5101 TREE_VEC_LENGTH (parms
));
5102 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5103 "previous declaration %q+D used %d template parameter",
5104 "previous declaration %q+D used %d template parameters",
5105 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5109 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5116 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5117 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5120 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5121 if (tmpl_parm
== error_mark_node
)
5124 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5125 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5126 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5128 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5130 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5131 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5132 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5133 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5134 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5135 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5136 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5137 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5138 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5140 error ("template parameter %q+#D", tmpl_parm
);
5141 error ("redeclared here as %q#D", parm
);
5145 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5147 /* We have in [temp.param]:
5149 A template-parameter may not be given default arguments
5150 by two different declarations in the same scope. */
5151 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5152 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5153 "original definition appeared here");
5157 if (parm_default
!= NULL_TREE
)
5158 /* Update the previous template parameters (which are the ones
5159 that will really count) with the new default value. */
5160 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5161 else if (tmpl_default
!= NULL_TREE
)
5162 /* Update the new parameters, too; they'll be used as the
5163 parameters for any members. */
5164 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5170 /* Simplify EXPR if it is a non-dependent expression. Returns the
5171 (possibly simplified) expression. */
5174 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5176 if (expr
== NULL_TREE
)
5179 /* If we're in a template, but EXPR isn't value dependent, simplify
5180 it. We're supposed to treat:
5182 template <typename T> void f(T[1 + 1]);
5183 template <typename T> void f(T[2]);
5185 as two declarations of the same function, for example. */
5186 if (processing_template_decl
5187 && !instantiation_dependent_expression_p (expr
)
5188 && potential_constant_expression (expr
))
5190 HOST_WIDE_INT saved_processing_template_decl
;
5192 saved_processing_template_decl
= processing_template_decl
;
5193 processing_template_decl
= 0;
5194 expr
= tsubst_copy_and_build (expr
,
5197 /*in_decl=*/NULL_TREE
,
5198 /*function_p=*/false,
5199 /*integral_constant_expression_p=*/true);
5200 processing_template_decl
= saved_processing_template_decl
;
5206 fold_non_dependent_expr (tree expr
)
5208 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5211 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5212 template declaration, or a TYPE_DECL for an alias declaration. */
5215 alias_type_or_template_p (tree t
)
5219 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5222 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5223 || DECL_ALIAS_TEMPLATE_P (t
));
5226 /* Return TRUE iff is a specialization of an alias template. */
5229 alias_template_specialization_p (const_tree t
)
5235 && TYPE_TEMPLATE_INFO (t
)
5236 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5237 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5240 /* Return the number of innermost template parameters in TMPL. */
5243 num_innermost_template_parms (tree tmpl
)
5245 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5246 return TREE_VEC_LENGTH (parms
);
5249 /* Return either TMPL or another template that it is equivalent to under DR
5250 1286: An alias that just changes the name of a template is equivalent to
5251 the other template. */
5254 get_underlying_template (tree tmpl
)
5256 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5257 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5259 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5260 if (TYPE_TEMPLATE_INFO (result
))
5262 tree sub
= TYPE_TI_TEMPLATE (result
);
5263 if (PRIMARY_TEMPLATE_P (sub
)
5264 && (num_innermost_template_parms (tmpl
)
5265 == num_innermost_template_parms (sub
)))
5267 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5268 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5269 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5271 /* The alias type is equivalent to the pattern of the
5272 underlying template, so strip the alias. */
5282 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5283 must be a function or a pointer-to-function type, as specified
5284 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5285 and check that the resulting function has external linkage. */
5288 convert_nontype_argument_function (tree type
, tree expr
)
5292 linkage_kind linkage
;
5294 fn
= instantiate_type (type
, fns
, tf_none
);
5295 if (fn
== error_mark_node
)
5296 return error_mark_node
;
5299 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5300 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5301 if (BASELINK_P (fn_no_ptr
))
5302 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5304 /* [temp.arg.nontype]/1
5306 A template-argument for a non-type, non-template template-parameter
5309 -- the address of an object or function with external [C++11: or
5310 internal] linkage. */
5312 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5314 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5315 if (TYPE_PTR_P (type
))
5316 error ("it must be the address of a function with external linkage");
5318 error ("it must be the name of a function with external linkage");
5322 linkage
= decl_linkage (fn_no_ptr
);
5323 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5325 if (cxx_dialect
>= cxx11
)
5326 error ("%qE is not a valid template argument for type %qT "
5327 "because %qD has no linkage",
5328 expr
, type
, fn_no_ptr
);
5330 error ("%qE is not a valid template argument for type %qT "
5331 "because %qD does not have external linkage",
5332 expr
, type
, fn_no_ptr
);
5339 /* Subroutine of convert_nontype_argument.
5340 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5341 Emit an error otherwise. */
5344 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5345 tsubst_flags_t complain
)
5348 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5350 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5352 if (complain
& tf_error
)
5354 error ("%qE is not a valid template argument for type %qT",
5356 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5361 /* Returns TRUE iff the address of OP is value-dependent.
5363 14.6.2.4 [temp.dep.temp]:
5364 A non-integral non-type template-argument is dependent if its type is
5365 dependent or it has either of the following forms
5368 and contains a nested-name-specifier which specifies a class-name that
5369 names a dependent type.
5371 We generalize this to just say that the address of a member of a
5372 dependent class is value-dependent; the above doesn't cover the
5373 address of a static data member named with an unqualified-id. */
5376 has_value_dependent_address (tree op
)
5378 /* We could use get_inner_reference here, but there's no need;
5379 this is only relevant for template non-type arguments, which
5380 can only be expressed as &id-expression. */
5383 tree ctx
= CP_DECL_CONTEXT (op
);
5384 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5391 /* The next set of functions are used for providing helpful explanatory
5392 diagnostics for failed overload resolution. Their messages should be
5393 indented by two spaces for consistency with the messages in
5397 unify_success (bool /*explain_p*/)
5403 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5406 inform (input_location
,
5407 " couldn't deduce template parameter %qD", parm
);
5412 unify_invalid (bool /*explain_p*/)
5418 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5421 inform (input_location
,
5422 " types %qT and %qT have incompatible cv-qualifiers",
5428 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5431 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5436 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5439 inform (input_location
,
5440 " template parameter %qD is not a parameter pack, but "
5447 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5450 inform (input_location
,
5451 " template argument %qE does not match "
5452 "pointer-to-member constant %qE",
5458 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5461 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5466 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5469 inform (input_location
,
5470 " inconsistent parameter pack deduction with %qT and %qT",
5476 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5481 inform (input_location
,
5482 " deduced conflicting types for parameter %qT (%qT and %qT)",
5483 parm
, first
, second
);
5485 inform (input_location
,
5486 " deduced conflicting values for non-type parameter "
5487 "%qE (%qE and %qE)", parm
, first
, second
);
5493 unify_vla_arg (bool explain_p
, tree arg
)
5496 inform (input_location
,
5497 " variable-sized array type %qT is not "
5498 "a valid template argument",
5504 unify_method_type_error (bool explain_p
, tree arg
)
5507 inform (input_location
,
5508 " member function type %qT is not a valid template argument",
5514 unify_arity (bool explain_p
, int have
, int wanted
)
5517 inform_n (input_location
, wanted
,
5518 " candidate expects %d argument, %d provided",
5519 " candidate expects %d arguments, %d provided",
5525 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5527 return unify_arity (explain_p
, have
, wanted
);
5531 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5533 return unify_arity (explain_p
, have
, wanted
);
5537 unify_arg_conversion (bool explain_p
, tree to_type
,
5538 tree from_type
, tree arg
)
5541 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5542 " cannot convert %qE (type %qT) to type %qT",
5543 arg
, from_type
, to_type
);
5548 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5549 tree parm
, tree arg
)
5554 case tbr_ambiguous_baseclass
:
5555 inform (input_location
, " %qT is an ambiguous base class of %qT",
5559 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5566 unify_inconsistent_template_template_parameters (bool explain_p
)
5569 inform (input_location
,
5570 " template parameters of a template template argument are "
5571 "inconsistent with other deduced template arguments");
5576 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5579 inform (input_location
,
5580 " can't deduce a template for %qT from non-template type %qT",
5586 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5589 inform (input_location
,
5590 " template argument %qE does not match %qD", arg
, parm
);
5595 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5598 inform (input_location
,
5599 " could not resolve address from overloaded function %qE",
5604 /* Attempt to convert the non-type template parameter EXPR to the
5605 indicated TYPE. If the conversion is successful, return the
5606 converted value. If the conversion is unsuccessful, return
5607 NULL_TREE if we issued an error message, or error_mark_node if we
5608 did not. We issue error messages for out-and-out bad template
5609 parameters, but not simply because the conversion failed, since we
5610 might be just trying to do argument deduction. Both TYPE and EXPR
5611 must be non-dependent.
5613 The conversion follows the special rules described in
5614 [temp.arg.nontype], and it is much more strict than an implicit
5617 This function is called twice for each template argument (see
5618 lookup_template_class for a more accurate description of this
5619 problem). This means that we need to handle expressions which
5620 are not valid in a C++ source, but can be created from the
5621 first call (for instance, casts to perform conversions). These
5622 hacks can go away after we fix the double coercion problem. */
5625 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5629 /* Detect immediately string literals as invalid non-type argument.
5630 This special-case is not needed for correctness (we would easily
5631 catch this later), but only to provide better diagnostic for this
5632 common user mistake. As suggested by DR 100, we do not mention
5633 linkage issues in the diagnostic as this is not the point. */
5634 /* FIXME we're making this OK. */
5635 if (TREE_CODE (expr
) == STRING_CST
)
5637 if (complain
& tf_error
)
5638 error ("%qE is not a valid template argument for type %qT "
5639 "because string literals can never be used in this context",
5644 /* Add the ADDR_EXPR now for the benefit of
5645 value_dependent_expression_p. */
5646 if (TYPE_PTROBV_P (type
)
5647 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5649 expr
= decay_conversion (expr
, complain
);
5650 if (expr
== error_mark_node
)
5651 return error_mark_node
;
5654 /* If we are in a template, EXPR may be non-dependent, but still
5655 have a syntactic, rather than semantic, form. For example, EXPR
5656 might be a SCOPE_REF, rather than the VAR_DECL to which the
5657 SCOPE_REF refers. Preserving the qualifying scope is necessary
5658 so that access checking can be performed when the template is
5659 instantiated -- but here we need the resolved form so that we can
5660 convert the argument. */
5661 if (TYPE_REF_OBJ_P (type
)
5662 && has_value_dependent_address (expr
))
5663 /* If we want the address and it's value-dependent, don't fold. */;
5664 else if (!type_unknown_p (expr
))
5665 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5666 if (error_operand_p (expr
))
5667 return error_mark_node
;
5668 expr_type
= TREE_TYPE (expr
);
5669 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5670 expr
= mark_lvalue_use (expr
);
5672 expr
= mark_rvalue_use (expr
);
5674 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5675 to a non-type argument of "nullptr". */
5676 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5677 expr
= convert (type
, expr
);
5679 /* In C++11, integral or enumeration non-type template arguments can be
5680 arbitrary constant expressions. Pointer and pointer to
5681 member arguments can be general constant expressions that evaluate
5682 to a null value, but otherwise still need to be of a specific form. */
5683 if (cxx_dialect
>= cxx11
)
5685 if (TREE_CODE (expr
) == PTRMEM_CST
)
5686 /* A PTRMEM_CST is already constant, and a valid template
5687 argument for a parameter of pointer to member type, we just want
5688 to leave it in that form rather than lower it to a
5690 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5691 expr
= maybe_constant_value (expr
);
5692 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5694 tree folded
= maybe_constant_value (expr
);
5695 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5696 : null_member_pointer_value_p (folded
))
5701 /* HACK: Due to double coercion, we can get a
5702 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5703 which is the tree that we built on the first call (see
5704 below when coercing to reference to object or to reference to
5705 function). We just strip everything and get to the arg.
5706 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5708 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5710 tree probe_type
, probe
= expr
;
5711 if (REFERENCE_REF_P (probe
))
5712 probe
= TREE_OPERAND (probe
, 0);
5713 probe_type
= TREE_TYPE (probe
);
5714 if (TREE_CODE (probe
) == NOP_EXPR
)
5716 /* ??? Maybe we could use convert_from_reference here, but we
5717 would need to relax its constraints because the NOP_EXPR
5718 could actually change the type to something more cv-qualified,
5719 and this is not folded by convert_from_reference. */
5720 tree addr
= TREE_OPERAND (probe
, 0);
5721 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5722 && TREE_CODE (addr
) == ADDR_EXPR
5723 && TYPE_PTR_P (TREE_TYPE (addr
))
5724 && (same_type_ignoring_top_level_qualifiers_p
5725 (TREE_TYPE (probe_type
),
5726 TREE_TYPE (TREE_TYPE (addr
)))))
5728 expr
= TREE_OPERAND (addr
, 0);
5729 expr_type
= TREE_TYPE (probe_type
);
5734 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5735 parameter is a pointer to object, through decay and
5736 qualification conversion. Let's strip everything. */
5737 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5741 if (TREE_CODE (probe
) == ADDR_EXPR
5742 && TYPE_PTR_P (TREE_TYPE (probe
)))
5744 /* Skip the ADDR_EXPR only if it is part of the decay for
5745 an array. Otherwise, it is part of the original argument
5746 in the source code. */
5747 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5748 probe
= TREE_OPERAND (probe
, 0);
5750 expr_type
= TREE_TYPE (expr
);
5754 /* [temp.arg.nontype]/5, bullet 1
5756 For a non-type template-parameter of integral or enumeration type,
5757 integral promotions (_conv.prom_) and integral conversions
5758 (_conv.integral_) are applied. */
5759 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5761 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5762 t
= maybe_constant_value (t
);
5763 if (t
!= error_mark_node
)
5766 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5767 return error_mark_node
;
5769 /* Notice that there are constant expressions like '4 % 0' which
5770 do not fold into integer constants. */
5771 if (TREE_CODE (expr
) != INTEGER_CST
)
5773 if (complain
& tf_error
)
5775 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5776 if (processing_template_decl
5777 && !require_potential_constant_expression (expr
))
5779 expr
= cxx_constant_value (expr
);
5780 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5781 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5782 "in template argument for type %qT ", type
);
5783 if (expr
== error_mark_node
)
5785 /* else cxx_constant_value complained but gave us
5786 a real constant, so go ahead. */
5787 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5793 /* Avoid typedef problems. */
5794 if (TREE_TYPE (expr
) != type
)
5795 expr
= fold_convert (type
, expr
);
5797 /* [temp.arg.nontype]/5, bullet 2
5799 For a non-type template-parameter of type pointer to object,
5800 qualification conversions (_conv.qual_) and the array-to-pointer
5801 conversion (_conv.array_) are applied. */
5802 else if (TYPE_PTROBV_P (type
))
5804 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5806 A template-argument for a non-type, non-template template-parameter
5807 shall be one of: [...]
5809 -- the name of a non-type template-parameter;
5810 -- the address of an object or function with external linkage, [...]
5811 expressed as "& id-expression" where the & is optional if the name
5812 refers to a function or array, or if the corresponding
5813 template-parameter is a reference.
5815 Here, we do not care about functions, as they are invalid anyway
5816 for a parameter of type pointer-to-object. */
5818 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5819 /* Non-type template parameters are OK. */
5821 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5822 /* Null pointer values are OK in C++11. */;
5823 else if (TREE_CODE (expr
) != ADDR_EXPR
5824 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5828 error ("%qD is not a valid template argument "
5829 "because %qD is a variable, not the address of "
5834 if (POINTER_TYPE_P (expr_type
))
5836 error ("%qE is not a valid template argument for %qT "
5837 "because it is not the address of a variable",
5841 /* Other values, like integer constants, might be valid
5842 non-type arguments of some other type. */
5843 return error_mark_node
;
5849 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5850 ? TREE_OPERAND (expr
, 0) : expr
);
5853 error ("%qE is not a valid template argument of type %qT "
5854 "because %qE is not a variable",
5858 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5860 error ("%qE is not a valid template argument of type %qT "
5861 "because %qD does not have external linkage",
5865 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5867 error ("%qE is not a valid template argument of type %qT "
5868 "because %qD has no linkage",
5874 expr
= decay_conversion (expr
, complain
);
5875 if (expr
== error_mark_node
)
5876 return error_mark_node
;
5878 expr
= perform_qualification_conversions (type
, expr
);
5879 if (expr
== error_mark_node
)
5880 return error_mark_node
;
5882 /* [temp.arg.nontype]/5, bullet 3
5884 For a non-type template-parameter of type reference to object, no
5885 conversions apply. The type referred to by the reference may be more
5886 cv-qualified than the (otherwise identical) type of the
5887 template-argument. The template-parameter is bound directly to the
5888 template-argument, which must be an lvalue. */
5889 else if (TYPE_REF_OBJ_P (type
))
5891 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5893 return error_mark_node
;
5895 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5897 error ("%qE is not a valid template argument for type %qT "
5898 "because of conflicts in cv-qualification", expr
, type
);
5902 if (!real_lvalue_p (expr
))
5904 error ("%qE is not a valid template argument for type %qT "
5905 "because it is not an lvalue", expr
, type
);
5909 /* [temp.arg.nontype]/1
5911 A template-argument for a non-type, non-template template-parameter
5912 shall be one of: [...]
5914 -- the address of an object or function with external linkage. */
5915 if (INDIRECT_REF_P (expr
)
5916 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5918 expr
= TREE_OPERAND (expr
, 0);
5921 error ("%q#D is not a valid template argument for type %qT "
5922 "because a reference variable does not have a constant "
5923 "address", expr
, type
);
5930 error ("%qE is not a valid template argument for type %qT "
5931 "because it is not an object with external linkage",
5936 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5938 error ("%qE is not a valid template argument for type %qT "
5939 "because object %qD has not external linkage",
5944 expr
= build_nop (type
, build_address (expr
));
5946 /* [temp.arg.nontype]/5, bullet 4
5948 For a non-type template-parameter of type pointer to function, only
5949 the function-to-pointer conversion (_conv.func_) is applied. If the
5950 template-argument represents a set of overloaded functions (or a
5951 pointer to such), the matching function is selected from the set
5953 else if (TYPE_PTRFN_P (type
))
5955 /* If the argument is a template-id, we might not have enough
5956 context information to decay the pointer. */
5957 if (!type_unknown_p (expr_type
))
5959 expr
= decay_conversion (expr
, complain
);
5960 if (expr
== error_mark_node
)
5961 return error_mark_node
;
5964 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5965 /* Null pointer values are OK in C++11. */
5966 return perform_qualification_conversions (type
, expr
);
5968 expr
= convert_nontype_argument_function (type
, expr
);
5969 if (!expr
|| expr
== error_mark_node
)
5972 /* [temp.arg.nontype]/5, bullet 5
5974 For a non-type template-parameter of type reference to function, no
5975 conversions apply. If the template-argument represents a set of
5976 overloaded functions, the matching function is selected from the set
5978 else if (TYPE_REFFN_P (type
))
5980 if (TREE_CODE (expr
) == ADDR_EXPR
)
5982 error ("%qE is not a valid template argument for type %qT "
5983 "because it is a pointer", expr
, type
);
5984 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5988 expr
= convert_nontype_argument_function (type
, expr
);
5989 if (!expr
|| expr
== error_mark_node
)
5992 expr
= build_nop (type
, build_address (expr
));
5994 /* [temp.arg.nontype]/5, bullet 6
5996 For a non-type template-parameter of type pointer to member function,
5997 no conversions apply. If the template-argument represents a set of
5998 overloaded member functions, the matching member function is selected
5999 from the set (_over.over_). */
6000 else if (TYPE_PTRMEMFUNC_P (type
))
6002 expr
= instantiate_type (type
, expr
, tf_none
);
6003 if (expr
== error_mark_node
)
6004 return error_mark_node
;
6006 /* [temp.arg.nontype] bullet 1 says the pointer to member
6007 expression must be a pointer-to-member constant. */
6008 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6009 return error_mark_node
;
6011 /* There is no way to disable standard conversions in
6012 resolve_address_of_overloaded_function (called by
6013 instantiate_type). It is possible that the call succeeded by
6014 converting &B::I to &D::I (where B is a base of D), so we need
6015 to reject this conversion here.
6017 Actually, even if there was a way to disable standard conversions,
6018 it would still be better to reject them here so that we can
6019 provide a superior diagnostic. */
6020 if (!same_type_p (TREE_TYPE (expr
), type
))
6022 error ("%qE is not a valid template argument for type %qT "
6023 "because it is of type %qT", expr
, type
,
6025 /* If we are just one standard conversion off, explain. */
6026 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6027 inform (input_location
,
6028 "standard conversions are not allowed in this context");
6032 /* [temp.arg.nontype]/5, bullet 7
6034 For a non-type template-parameter of type pointer to data member,
6035 qualification conversions (_conv.qual_) are applied. */
6036 else if (TYPE_PTRDATAMEM_P (type
))
6038 /* [temp.arg.nontype] bullet 1 says the pointer to member
6039 expression must be a pointer-to-member constant. */
6040 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6041 return error_mark_node
;
6043 expr
= perform_qualification_conversions (type
, expr
);
6044 if (expr
== error_mark_node
)
6047 else if (NULLPTR_TYPE_P (type
))
6049 if (expr
!= nullptr_node
)
6051 error ("%qE is not a valid template argument for type %qT "
6052 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6057 /* A template non-type parameter must be one of the above. */
6061 /* Sanity check: did we actually convert the argument to the
6063 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6064 (type
, TREE_TYPE (expr
)));
6068 /* Subroutine of coerce_template_template_parms, which returns 1 if
6069 PARM_PARM and ARG_PARM match using the rule for the template
6070 parameters of template template parameters. Both PARM and ARG are
6071 template parameters; the rest of the arguments are the same as for
6072 coerce_template_template_parms.
6075 coerce_template_template_parm (tree parm
,
6077 tsubst_flags_t complain
,
6081 if (arg
== NULL_TREE
|| arg
== error_mark_node
6082 || parm
== NULL_TREE
|| parm
== error_mark_node
)
6085 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6088 switch (TREE_CODE (parm
))
6091 /* We encounter instantiations of templates like
6092 template <template <template <class> class> class TT>
6095 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6096 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6098 if (!coerce_template_template_parms
6099 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6105 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6106 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6107 /* Argument is a parameter pack but parameter is not. */
6112 /* The tsubst call is used to handle cases such as
6114 template <int> class C {};
6115 template <class T, template <T> class TT> class D {};
6118 i.e. the parameter list of TT depends on earlier parameters. */
6119 if (!uses_template_parms (TREE_TYPE (arg
))
6121 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6125 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6126 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6127 /* Argument is a parameter pack but parameter is not. */
6140 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6141 template template parameters. Both PARM_PARMS and ARG_PARMS are
6142 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6145 Consider the example:
6146 template <class T> class A;
6147 template<template <class U> class TT> class B;
6149 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6150 the parameters to A, and OUTER_ARGS contains A. */
6153 coerce_template_template_parms (tree parm_parms
,
6155 tsubst_flags_t complain
,
6159 int nparms
, nargs
, i
;
6163 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6164 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6166 nparms
= TREE_VEC_LENGTH (parm_parms
);
6167 nargs
= TREE_VEC_LENGTH (arg_parms
);
6169 /* Determine whether we have a parameter pack at the end of the
6170 template template parameter's template parameter list. */
6171 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6173 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6175 if (parm
== error_mark_node
)
6178 switch (TREE_CODE (parm
))
6182 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6187 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6197 && !(variadic_p
&& nargs
>= nparms
- 1))
6200 /* Check all of the template parameters except the parameter pack at
6201 the end (if any). */
6202 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6204 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6205 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6208 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6209 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6211 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6219 /* Check each of the template parameters in the template
6220 argument against the template parameter pack at the end of
6221 the template template parameter. */
6222 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6225 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6227 for (; i
< nargs
; ++i
)
6229 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6232 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6234 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6243 /* Verifies that the deduced template arguments (in TARGS) for the
6244 template template parameters (in TPARMS) represent valid bindings,
6245 by comparing the template parameter list of each template argument
6246 to the template parameter list of its corresponding template
6247 template parameter, in accordance with DR150. This
6248 routine can only be called after all template arguments have been
6249 deduced. It will return TRUE if all of the template template
6250 parameter bindings are okay, FALSE otherwise. */
6252 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6254 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6257 /* We're dealing with template parms in this process. */
6258 ++processing_template_decl
;
6260 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6262 for (i
= 0; i
< ntparms
; ++i
)
6264 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6265 tree targ
= TREE_VEC_ELT (targs
, i
);
6267 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6269 tree packed_args
= NULL_TREE
;
6272 if (ARGUMENT_PACK_P (targ
))
6274 /* Look inside the argument pack. */
6275 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6276 len
= TREE_VEC_LENGTH (packed_args
);
6279 for (idx
= 0; idx
< len
; ++idx
)
6281 tree targ_parms
= NULL_TREE
;
6284 /* Extract the next argument from the argument
6286 targ
= TREE_VEC_ELT (packed_args
, idx
);
6288 if (PACK_EXPANSION_P (targ
))
6289 /* Look at the pattern of the pack expansion. */
6290 targ
= PACK_EXPANSION_PATTERN (targ
);
6292 /* Extract the template parameters from the template
6294 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6295 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6296 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6297 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6299 /* Verify that we can coerce the template template
6300 parameters from the template argument to the template
6301 parameter. This requires an exact match. */
6303 && !coerce_template_template_parms
6304 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6319 --processing_template_decl
;
6323 /* Since type attributes aren't mangled, we need to strip them from
6324 template type arguments. */
6327 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6330 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6332 mv
= TYPE_MAIN_VARIANT (arg
);
6333 arg
= strip_typedefs (arg
);
6334 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6335 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6337 if (complain
& tf_warning
)
6338 warning (0, "ignoring attributes on template argument %qT", arg
);
6339 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6340 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6345 /* Convert the indicated template ARG as necessary to match the
6346 indicated template PARM. Returns the converted ARG, or
6347 error_mark_node if the conversion was unsuccessful. Error and
6348 warning messages are issued under control of COMPLAIN. This
6349 conversion is for the Ith parameter in the parameter list. ARGS is
6350 the full set of template arguments deduced so far. */
6353 convert_template_argument (tree parm
,
6356 tsubst_flags_t complain
,
6362 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6364 if (TREE_CODE (arg
) == TREE_LIST
6365 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6367 /* The template argument was the name of some
6368 member function. That's usually
6369 invalid, but static members are OK. In any
6370 case, grab the underlying fields/functions
6371 and issue an error later if required. */
6372 orig_arg
= TREE_VALUE (arg
);
6373 TREE_TYPE (arg
) = unknown_type_node
;
6378 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6379 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6380 || requires_tmpl_type
);
6382 /* When determining whether an argument pack expansion is a template,
6383 look at the pattern. */
6384 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6385 arg
= PACK_EXPANSION_PATTERN (arg
);
6387 /* Deal with an injected-class-name used as a template template arg. */
6388 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6390 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6391 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6393 if (cxx_dialect
>= cxx11
)
6394 /* OK under DR 1004. */;
6395 else if (complain
& tf_warning_or_error
)
6396 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6397 " used as template template argument", TYPE_NAME (arg
));
6398 else if (flag_pedantic_errors
)
6406 ((TREE_CODE (arg
) == TEMPLATE_DECL
6407 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6408 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6409 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6410 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6413 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6414 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6415 arg
= TYPE_STUB_DECL (arg
);
6417 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6419 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6420 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6422 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6424 if (complain
& tf_error
)
6425 error ("invalid use of destructor %qE as a type", orig_arg
);
6426 return error_mark_node
;
6429 permerror (input_location
,
6430 "to refer to a type member of a template parameter, "
6431 "use %<typename %E%>", orig_arg
);
6433 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6434 TREE_OPERAND (arg
, 1),
6440 if (is_type
!= requires_type
)
6444 if (complain
& tf_error
)
6446 error ("type/value mismatch at argument %d in template "
6447 "parameter list for %qD",
6450 error (" expected a constant of type %qT, got %qT",
6452 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6453 else if (requires_tmpl_type
)
6454 error (" expected a class template, got %qE", orig_arg
);
6456 error (" expected a type, got %qE", orig_arg
);
6459 return error_mark_node
;
6461 if (is_tmpl_type
^ requires_tmpl_type
)
6463 if (in_decl
&& (complain
& tf_error
))
6465 error ("type/value mismatch at argument %d in template "
6466 "parameter list for %qD",
6469 error (" expected a type, got %qT", DECL_NAME (arg
));
6471 error (" expected a class template, got %qT", orig_arg
);
6473 return error_mark_node
;
6478 if (requires_tmpl_type
)
6480 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6482 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6483 /* The number of argument required is not known yet.
6484 Just accept it for now. */
6485 val
= TREE_TYPE (arg
);
6488 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6491 /* Strip alias templates that are equivalent to another
6493 arg
= get_underlying_template (arg
);
6494 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6496 if (coerce_template_template_parms (parmparm
, argparm
,
6502 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6504 if (val
!= error_mark_node
)
6506 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6507 val
= TREE_TYPE (val
);
6508 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6509 val
= make_pack_expansion (val
);
6514 if (in_decl
&& (complain
& tf_error
))
6516 error ("type/value mismatch at argument %d in "
6517 "template parameter list for %qD",
6519 error (" expected a template of type %qD, got %qT",
6523 val
= error_mark_node
;
6529 /* We only form one instance of each template specialization.
6530 Therefore, if we use a non-canonical variant (i.e., a
6531 typedef), any future messages referring to the type will use
6532 the typedef, which is confusing if those future uses do not
6533 themselves also use the typedef. */
6535 val
= canonicalize_type_argument (val
, complain
);
6539 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6541 if (invalid_nontype_parm_type_p (t
, complain
))
6542 return error_mark_node
;
6544 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6546 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6550 /* Not sure if this is reachable, but it doesn't hurt
6552 error ("type mismatch in nontype parameter pack");
6553 val
= error_mark_node
;
6556 else if (!dependent_template_arg_p (orig_arg
)
6557 && !uses_template_parms (t
))
6558 /* We used to call digest_init here. However, digest_init
6559 will report errors, which we don't want when complain
6560 is zero. More importantly, digest_init will try too
6561 hard to convert things: for example, `0' should not be
6562 converted to pointer type at this point according to
6563 the standard. Accepting this is not merely an
6564 extension, since deciding whether or not these
6565 conversions can occur is part of determining which
6566 function template to call, or whether a given explicit
6567 argument specification is valid. */
6568 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6570 val
= strip_typedefs_expr (orig_arg
);
6572 if (val
== NULL_TREE
)
6573 val
= error_mark_node
;
6574 else if (val
== error_mark_node
&& (complain
& tf_error
))
6575 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6577 if (TREE_CODE (val
) == SCOPE_REF
)
6579 /* Strip typedefs from the SCOPE_REF. */
6580 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6581 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6583 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6584 QUALIFIED_NAME_IS_TEMPLATE (val
));
6591 /* Coerces the remaining template arguments in INNER_ARGS (from
6592 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6593 Returns the coerced argument pack. PARM_IDX is the position of this
6594 parameter in the template parameter list. ARGS is the original
6595 template argument list. */
6597 coerce_template_parameter_pack (tree parms
,
6605 tsubst_flags_t complain
)
6607 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6608 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6611 tree packed_parms
= NULL_TREE
;
6613 if (arg_idx
> nargs
)
6616 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
6618 /* When the template parameter is a non-type template parameter pack
6619 or template template parameter pack whose type or template
6620 parameters use parameter packs, we know exactly how many arguments
6621 we are looking for. Build a vector of the instantiated decls for
6622 these template parameters in PACKED_PARMS. */
6623 /* We can't use make_pack_expansion here because it would interpret a
6624 _DECL as a use rather than a declaration. */
6625 tree decl
= TREE_VALUE (parm
);
6626 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
6627 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
6628 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
6629 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
6631 TREE_VEC_LENGTH (args
)--;
6632 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
6633 TREE_VEC_LENGTH (args
)++;
6635 if (packed_parms
== error_mark_node
)
6636 return error_mark_node
;
6638 /* If we're doing a partial instantiation of a member template,
6639 verify that all of the types used for the non-type
6640 template parameter pack are, in fact, valid for non-type
6641 template parameters. */
6643 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6645 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
6646 for (j
= 0; j
< len
; ++j
)
6648 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
6649 if (invalid_nontype_parm_type_p (t
, complain
))
6650 return error_mark_node
;
6654 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
6657 packed_args
= make_tree_vec (nargs
- arg_idx
);
6659 /* Convert the remaining arguments, which will be a part of the
6660 parameter pack "parm". */
6661 for (; arg_idx
< nargs
; ++arg_idx
)
6663 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6664 tree actual_parm
= TREE_VALUE (parm
);
6665 int pack_idx
= arg_idx
- parm_idx
;
6669 /* Once we've packed as many args as we have types, stop. */
6670 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
6672 else if (PACK_EXPANSION_P (arg
))
6673 /* We don't know how many args we have yet, just
6674 use the unconverted ones for now. */
6677 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
6680 if (arg
== error_mark_node
)
6682 if (complain
& tf_error
)
6683 error ("template argument %d is invalid", arg_idx
+ 1);
6686 arg
= convert_template_argument (actual_parm
,
6687 arg
, new_args
, complain
, parm_idx
,
6689 if (arg
== error_mark_node
)
6691 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
6694 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
6695 && TREE_VEC_LENGTH (packed_args
) > 0)
6697 error ("wrong number of template arguments (%d, should be %d)",
6698 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
6699 return error_mark_node
;
6702 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6703 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6704 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6707 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6708 TREE_TYPE (argument_pack
)
6709 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6710 TREE_CONSTANT (argument_pack
) = 1;
6713 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6714 #ifdef ENABLE_CHECKING
6715 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6716 TREE_VEC_LENGTH (packed_args
));
6718 return argument_pack
;
6721 /* Returns the number of pack expansions in the template argument vector
6725 pack_expansion_args_count (tree args
)
6730 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6732 tree elt
= TREE_VEC_ELT (args
, i
);
6733 if (elt
&& PACK_EXPANSION_P (elt
))
6739 /* Convert all template arguments to their appropriate types, and
6740 return a vector containing the innermost resulting template
6741 arguments. If any error occurs, return error_mark_node. Error and
6742 warning messages are issued under control of COMPLAIN.
6744 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6745 for arguments not specified in ARGS. Otherwise, if
6746 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6747 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6748 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6752 coerce_template_parms (tree parms
,
6755 tsubst_flags_t complain
,
6756 bool require_all_args
,
6757 bool use_default_args
)
6759 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6760 tree orig_inner_args
;
6763 tree new_inner_args
;
6764 int saved_unevaluated_operand
;
6765 int saved_inhibit_evaluation_warnings
;
6767 /* When used as a boolean value, indicates whether this is a
6768 variadic template parameter list. Since it's an int, we can also
6769 subtract it from nparms to get the number of non-variadic
6772 int variadic_args_p
= 0;
6773 int post_variadic_parms
= 0;
6775 if (args
== error_mark_node
)
6776 return error_mark_node
;
6778 nparms
= TREE_VEC_LENGTH (parms
);
6780 /* Determine if there are any parameter packs. */
6781 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6783 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6785 ++post_variadic_parms
;
6786 if (template_parameter_pack_p (tparm
))
6790 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6791 /* If there are no parameters that follow a parameter pack, we need to
6792 expand any argument packs so that we can deduce a parameter pack from
6793 some non-packed args followed by an argument pack, as in variadic85.C.
6794 If there are such parameters, we need to leave argument packs intact
6795 so the arguments are assigned properly. This can happen when dealing
6796 with a nested class inside a partial specialization of a class
6797 template, as in variadic92.C, or when deducing a template parameter pack
6798 from a sub-declarator, as in variadic114.C. */
6799 if (!post_variadic_parms
)
6800 inner_args
= expand_template_argument_pack (inner_args
);
6802 /* Count any pack expansion args. */
6803 variadic_args_p
= pack_expansion_args_count (inner_args
);
6805 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6806 if ((nargs
> nparms
&& !variadic_p
)
6807 || (nargs
< nparms
- variadic_p
6810 && (!use_default_args
6811 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6812 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6814 if (complain
& tf_error
)
6818 nparms
-= variadic_p
;
6819 error ("wrong number of template arguments "
6820 "(%d, should be %d or more)", nargs
, nparms
);
6823 error ("wrong number of template arguments "
6824 "(%d, should be %d)", nargs
, nparms
);
6827 error ("provided for %q+D", in_decl
);
6830 return error_mark_node
;
6832 /* We can't pass a pack expansion to a non-pack parameter of an alias
6833 template (DR 1430). */
6834 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6836 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6838 if (complain
& tf_error
)
6840 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6842 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6843 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6845 if (PACK_EXPANSION_P (arg
)
6846 && !template_parameter_pack_p (parm
))
6848 error ("pack expansion argument for non-pack parameter "
6849 "%qD of alias template %qD", parm
, in_decl
);
6850 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6857 return error_mark_node
;
6860 /* We need to evaluate the template arguments, even though this
6861 template-id may be nested within a "sizeof". */
6862 saved_unevaluated_operand
= cp_unevaluated_operand
;
6863 cp_unevaluated_operand
= 0;
6864 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6865 c_inhibit_evaluation_warnings
= 0;
6866 new_inner_args
= make_tree_vec (nparms
);
6867 new_args
= add_outermost_template_args (args
, new_inner_args
);
6868 int pack_adjust
= 0;
6869 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6874 /* Get the Ith template parameter. */
6875 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6877 if (parm
== error_mark_node
)
6879 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6883 /* Calculate the next argument. */
6884 if (arg_idx
< nargs
)
6885 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6889 if (template_parameter_pack_p (TREE_VALUE (parm
))
6890 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6892 /* Some arguments will be placed in the
6893 template parameter pack PARM. */
6894 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6895 inner_args
, arg_idx
,
6899 if (arg
== NULL_TREE
)
6901 /* We don't know how many args we have yet, just use the
6902 unconverted (and still packed) ones for now. */
6903 new_inner_args
= orig_inner_args
;
6908 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6910 /* Store this argument. */
6911 if (arg
== error_mark_node
)
6914 /* We are done with all of the arguments. */
6919 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
6920 arg_idx
+= pack_adjust
;
6927 if (PACK_EXPANSION_P (arg
))
6929 /* We don't know how many args we have yet, just
6930 use the unconverted ones for now. */
6931 new_inner_args
= inner_args
;
6936 else if (require_all_args
)
6938 /* There must be a default arg in this case. */
6939 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6941 /* The position of the first default template argument,
6942 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6944 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6946 arg_idx
- pack_adjust
);
6951 if (arg
== error_mark_node
)
6953 if (complain
& tf_error
)
6954 error ("template argument %d is invalid", arg_idx
+ 1);
6957 /* This only occurs if there was an error in the template
6958 parameter list itself (which we would already have
6959 reported) that we are trying to recover from, e.g., a class
6960 template with a parameter list such as
6961 template<typename..., typename>. */
6964 arg
= convert_template_argument (TREE_VALUE (parm
),
6965 arg
, new_args
, complain
,
6968 if (arg
== error_mark_node
)
6970 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
6972 cp_unevaluated_operand
= saved_unevaluated_operand
;
6973 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6975 if (variadic_p
&& arg_idx
< nargs
)
6977 if (complain
& tf_error
)
6979 error ("wrong number of template arguments "
6980 "(%d, should be %d)", nargs
, arg_idx
);
6982 error ("provided for %q+D", in_decl
);
6984 return error_mark_node
;
6988 return error_mark_node
;
6990 #ifdef ENABLE_CHECKING
6991 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6992 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6993 TREE_VEC_LENGTH (new_inner_args
));
6996 return new_inner_args
;
6999 /* Like coerce_template_parms. If PARMS represents all template
7000 parameters levels, this function returns a vector of vectors
7001 representing all the resulting argument levels. Note that in this
7002 case, only the innermost arguments are coerced because the
7003 outermost ones are supposed to have been coerced already.
7005 Otherwise, if PARMS represents only (the innermost) vector of
7006 parameters, this function returns a vector containing just the
7007 innermost resulting arguments. */
7010 coerce_innermost_template_parms (tree parms
,
7013 tsubst_flags_t complain
,
7014 bool require_all_args
,
7015 bool use_default_args
)
7017 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7018 int args_depth
= TMPL_ARGS_DEPTH (args
);
7021 if (parms_depth
> 1)
7023 coerced_args
= make_tree_vec (parms_depth
);
7027 for (level
= parms
, cur_depth
= parms_depth
;
7028 parms_depth
> 0 && level
!= NULL_TREE
;
7029 level
= TREE_CHAIN (level
), --cur_depth
)
7032 if (cur_depth
== args_depth
)
7033 l
= coerce_template_parms (TREE_VALUE (level
),
7034 args
, in_decl
, complain
,
7038 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7040 if (l
== error_mark_node
)
7041 return error_mark_node
;
7043 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7047 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7048 args
, in_decl
, complain
,
7051 return coerced_args
;
7054 /* Returns 1 if template args OT and NT are equivalent. */
7057 template_args_equal (tree ot
, tree nt
)
7061 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7064 if (TREE_CODE (nt
) == TREE_VEC
)
7065 /* For member templates */
7066 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7067 else if (PACK_EXPANSION_P (ot
))
7068 return (PACK_EXPANSION_P (nt
)
7069 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7070 PACK_EXPANSION_PATTERN (nt
))
7071 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7072 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7073 else if (ARGUMENT_PACK_P (ot
))
7078 if (!ARGUMENT_PACK_P (nt
))
7081 opack
= ARGUMENT_PACK_ARGS (ot
);
7082 npack
= ARGUMENT_PACK_ARGS (nt
);
7083 len
= TREE_VEC_LENGTH (opack
);
7084 if (TREE_VEC_LENGTH (npack
) != len
)
7086 for (i
= 0; i
< len
; ++i
)
7087 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7088 TREE_VEC_ELT (npack
, i
)))
7092 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7094 /* We get here probably because we are in the middle of substituting
7095 into the pattern of a pack expansion. In that case the
7096 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7097 interested in. So we want to use the initial pack argument for
7099 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7100 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7101 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7102 return template_args_equal (ot
, nt
);
7104 else if (TYPE_P (nt
))
7105 return TYPE_P (ot
) && same_type_p (ot
, nt
);
7106 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7109 return cp_tree_equal (ot
, nt
);
7112 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7113 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7114 NEWARG_PTR with the offending arguments if they are non-NULL. */
7117 comp_template_args_with_info (tree oldargs
, tree newargs
,
7118 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7122 if (oldargs
== newargs
)
7125 if (!oldargs
|| !newargs
)
7128 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7131 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7133 tree nt
= TREE_VEC_ELT (newargs
, i
);
7134 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7136 if (! template_args_equal (ot
, nt
))
7138 if (oldarg_ptr
!= NULL
)
7140 if (newarg_ptr
!= NULL
)
7148 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7149 of template arguments. Returns 0 otherwise. */
7152 comp_template_args (tree oldargs
, tree newargs
)
7154 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7158 add_pending_template (tree d
)
7160 tree ti
= (TYPE_P (d
)
7161 ? CLASSTYPE_TEMPLATE_INFO (d
)
7162 : DECL_TEMPLATE_INFO (d
));
7163 struct pending_template
*pt
;
7166 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7169 /* We are called both from instantiate_decl, where we've already had a
7170 tinst_level pushed, and instantiate_template, where we haven't.
7172 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7175 push_tinst_level (d
);
7177 pt
= ggc_alloc_pending_template ();
7179 pt
->tinst
= current_tinst_level
;
7180 if (last_pending_template
)
7181 last_pending_template
->next
= pt
;
7183 pending_templates
= pt
;
7185 last_pending_template
= pt
;
7187 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7194 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7195 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7196 documentation for TEMPLATE_ID_EXPR. */
7199 lookup_template_function (tree fns
, tree arglist
)
7203 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7204 return error_mark_node
;
7206 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7208 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7210 error ("%q#D is not a function template", fns
);
7211 return error_mark_node
;
7214 if (BASELINK_P (fns
))
7216 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7218 BASELINK_FUNCTIONS (fns
),
7223 type
= TREE_TYPE (fns
);
7224 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7225 type
= unknown_type_node
;
7227 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7230 /* Within the scope of a template class S<T>, the name S gets bound
7231 (in build_self_reference) to a TYPE_DECL for the class, not a
7232 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7233 or one of its enclosing classes, and that type is a template,
7234 return the associated TEMPLATE_DECL. Otherwise, the original
7237 Also handle the case when DECL is a TREE_LIST of ambiguous
7238 injected-class-names from different bases. */
7241 maybe_get_template_decl_from_type_decl (tree decl
)
7243 if (decl
== NULL_TREE
)
7246 /* DR 176: A lookup that finds an injected-class-name (10.2
7247 [class.member.lookup]) can result in an ambiguity in certain cases
7248 (for example, if it is found in more than one base class). If all of
7249 the injected-class-names that are found refer to specializations of
7250 the same class template, and if the name is followed by a
7251 template-argument-list, the reference refers to the class template
7252 itself and not a specialization thereof, and is not ambiguous. */
7253 if (TREE_CODE (decl
) == TREE_LIST
)
7255 tree t
, tmpl
= NULL_TREE
;
7256 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7258 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7261 else if (tmpl
!= elt
)
7264 if (tmpl
&& t
== NULL_TREE
)
7270 return (decl
!= NULL_TREE
7271 && DECL_SELF_REFERENCE_P (decl
)
7272 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7273 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7276 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7277 parameters, find the desired type.
7279 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7281 IN_DECL, if non-NULL, is the template declaration we are trying to
7284 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7285 the class we are looking up.
7287 Issue error and warning messages under control of COMPLAIN.
7289 If the template class is really a local class in a template
7290 function, then the FUNCTION_CONTEXT is the function in which it is
7293 ??? Note that this function is currently called *twice* for each
7294 template-id: the first time from the parser, while creating the
7295 incomplete type (finish_template_type), and the second type during the
7296 real instantiation (instantiate_template_class). This is surely something
7297 that we want to avoid. It also causes some problems with argument
7298 coercion (see convert_nontype_argument for more information on this). */
7301 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7302 int entering_scope
, tsubst_flags_t complain
)
7304 tree templ
= NULL_TREE
, parmlist
;
7311 if (identifier_p (d1
))
7313 tree value
= innermost_non_namespace_value (d1
);
7314 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7319 push_decl_namespace (context
);
7320 templ
= lookup_name (d1
);
7321 templ
= maybe_get_template_decl_from_type_decl (templ
);
7323 pop_decl_namespace ();
7326 context
= DECL_CONTEXT (templ
);
7328 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7330 tree type
= TREE_TYPE (d1
);
7332 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7333 an implicit typename for the second A. Deal with it. */
7334 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7335 type
= TREE_TYPE (type
);
7337 if (CLASSTYPE_TEMPLATE_INFO (type
))
7339 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7340 d1
= DECL_NAME (templ
);
7343 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7344 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7346 templ
= TYPE_TI_TEMPLATE (d1
);
7347 d1
= DECL_NAME (templ
);
7349 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7350 && DECL_TEMPLATE_RESULT (d1
)
7351 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7354 d1
= DECL_NAME (templ
);
7355 context
= DECL_CONTEXT (templ
);
7357 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7360 d1
= DECL_NAME (templ
);
7363 /* Issue an error message if we didn't find a template. */
7366 if (complain
& tf_error
)
7367 error ("%qT is not a template", d1
);
7368 return error_mark_node
;
7371 if (TREE_CODE (templ
) != TEMPLATE_DECL
7372 /* Make sure it's a user visible template, if it was named by
7374 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7375 && !PRIMARY_TEMPLATE_P (templ
)))
7377 if (complain
& tf_error
)
7379 error ("non-template type %qT used as a template", d1
);
7381 error ("for template declaration %q+D", in_decl
);
7383 return error_mark_node
;
7386 complain
&= ~tf_user
;
7388 /* An alias that just changes the name of a template is equivalent to the
7389 other template, so if any of the arguments are pack expansions, strip
7390 the alias to avoid problems with a pack expansion passed to a non-pack
7391 alias template parameter (DR 1430). */
7392 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7393 templ
= get_underlying_template (templ
);
7395 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7397 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7398 template arguments */
7404 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7406 /* Consider an example where a template template parameter declared as
7408 template <class T, class U = std::allocator<T> > class TT
7410 The template parameter level of T and U are one level larger than
7411 of TT. To proper process the default argument of U, say when an
7412 instantiation `TT<int>' is seen, we need to build the full
7413 arguments containing {int} as the innermost level. Outer levels,
7414 available when not appearing as default template argument, can be
7415 obtained from the arguments of the enclosing template.
7417 Suppose that TT is later substituted with std::vector. The above
7418 instantiation is `TT<int, std::allocator<T> >' with TT at
7419 level 1, and T at level 2, while the template arguments at level 1
7420 becomes {std::vector} and the inner level 2 is {int}. */
7422 outer
= DECL_CONTEXT (templ
);
7424 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7425 else if (current_template_parms
)
7426 /* This is an argument of the current template, so we haven't set
7427 DECL_CONTEXT yet. */
7428 outer
= current_template_args ();
7431 arglist
= add_to_template_args (outer
, arglist
);
7433 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7435 /*require_all_args=*/true,
7436 /*use_default_args=*/true);
7437 if (arglist2
== error_mark_node
7438 || (!uses_template_parms (arglist2
)
7439 && check_instantiated_args (templ
, arglist2
, complain
)))
7440 return error_mark_node
;
7442 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7447 tree template_type
= TREE_TYPE (templ
);
7450 tree found
= NULL_TREE
;
7453 int is_dependent_type
;
7454 int use_partial_inst_tmpl
= false;
7456 if (template_type
== error_mark_node
)
7457 /* An error occurred while building the template TEMPL, and a
7458 diagnostic has most certainly been emitted for that
7459 already. Let's propagate that error. */
7460 return error_mark_node
;
7462 gen_tmpl
= most_general_template (templ
);
7463 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7464 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7465 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7467 if (arg_depth
== 1 && parm_depth
> 1)
7469 /* We've been given an incomplete set of template arguments.
7472 template <class T> struct S1 {
7473 template <class U> struct S2 {};
7474 template <class U> struct S2<U*> {};
7477 we will be called with an ARGLIST of `U*', but the
7478 TEMPLATE will be `template <class T> template
7479 <class U> struct S1<T>::S2'. We must fill in the missing
7482 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7484 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7487 /* Now we should have enough arguments. */
7488 gcc_assert (parm_depth
== arg_depth
);
7490 /* From here on, we're only interested in the most general
7493 /* Calculate the BOUND_ARGS. These will be the args that are
7494 actually tsubst'd into the definition to create the
7498 /* We have multiple levels of arguments to coerce, at once. */
7500 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7502 tree bound_args
= make_tree_vec (parm_depth
);
7504 for (i
= saved_depth
,
7505 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7506 i
> 0 && t
!= NULL_TREE
;
7507 --i
, t
= TREE_CHAIN (t
))
7510 if (i
== saved_depth
)
7511 a
= coerce_template_parms (TREE_VALUE (t
),
7514 /*require_all_args=*/true,
7515 /*use_default_args=*/true);
7517 /* Outer levels should have already been coerced. */
7518 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7520 /* Don't process further if one of the levels fails. */
7521 if (a
== error_mark_node
)
7523 /* Restore the ARGLIST to its full size. */
7524 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7525 return error_mark_node
;
7528 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7530 /* We temporarily reduce the length of the ARGLIST so
7531 that coerce_template_parms will see only the arguments
7532 corresponding to the template parameters it is
7534 TREE_VEC_LENGTH (arglist
)--;
7537 /* Restore the ARGLIST to its full size. */
7538 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7540 arglist
= bound_args
;
7544 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7545 INNERMOST_TEMPLATE_ARGS (arglist
),
7548 /*require_all_args=*/true,
7549 /*use_default_args=*/true);
7551 if (arglist
== error_mark_node
)
7552 /* We were unable to bind the arguments. */
7553 return error_mark_node
;
7555 /* In the scope of a template class, explicit references to the
7556 template class refer to the type of the template, not any
7557 instantiation of it. For example, in:
7559 template <class T> class C { void f(C<T>); }
7561 the `C<T>' is just the same as `C'. Outside of the
7562 class, however, such a reference is an instantiation. */
7564 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7565 || currently_open_class (template_type
))
7566 /* comp_template_args is expensive, check it last. */
7567 && comp_template_args (TYPE_TI_ARGS (template_type
),
7569 return template_type
;
7571 /* If we already have this specialization, return it. */
7572 elt
.tmpl
= gen_tmpl
;
7574 hash
= hash_specialization (&elt
);
7575 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7581 is_dependent_type
= uses_template_parms (arglist
);
7583 /* If the deduced arguments are invalid, then the binding
7585 if (!is_dependent_type
7586 && check_instantiated_args (gen_tmpl
,
7587 INNERMOST_TEMPLATE_ARGS (arglist
),
7589 return error_mark_node
;
7591 if (!is_dependent_type
7592 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7593 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7594 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7596 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7597 DECL_NAME (gen_tmpl
),
7598 /*tag_scope=*/ts_global
);
7602 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7604 if (context
== error_mark_node
)
7605 return error_mark_node
;
7608 context
= global_namespace
;
7610 /* Create the type. */
7611 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7613 /* The user referred to a specialization of an alias
7614 template represented by GEN_TMPL.
7616 [temp.alias]/2 says:
7618 When a template-id refers to the specialization of an
7619 alias template, it is equivalent to the associated
7620 type obtained by substitution of its
7621 template-arguments for the template-parameters in the
7622 type-id of the alias template. */
7624 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7625 /* Note that the call above (by indirectly calling
7626 register_specialization in tsubst_decl) registers the
7627 TYPE_DECL representing the specialization of the alias
7628 template. So next time someone substitutes ARGLIST for
7629 the template parms into the alias template (GEN_TMPL),
7630 she'll get that TYPE_DECL back. */
7632 if (t
== error_mark_node
)
7635 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7637 if (!is_dependent_type
)
7639 set_current_access_from_decl (TYPE_NAME (template_type
));
7640 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7641 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7642 arglist
, complain
, in_decl
),
7643 SCOPED_ENUM_P (template_type
), NULL
);
7645 if (t
== error_mark_node
)
7650 /* We don't want to call start_enum for this type, since
7651 the values for the enumeration constants may involve
7652 template parameters. And, no one should be interested
7653 in the enumeration constants for such a type. */
7654 t
= cxx_make_type (ENUMERAL_TYPE
);
7655 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7657 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7658 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7659 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7661 else if (CLASS_TYPE_P (template_type
))
7663 t
= make_class_type (TREE_CODE (template_type
));
7664 CLASSTYPE_DECLARED_CLASS (t
)
7665 = CLASSTYPE_DECLARED_CLASS (template_type
);
7666 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7667 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7669 /* A local class. Make sure the decl gets registered properly. */
7670 if (context
== current_function_decl
)
7671 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7673 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7674 /* This instantiation is another name for the primary
7675 template type. Set the TYPE_CANONICAL field
7677 TYPE_CANONICAL (t
) = template_type
;
7678 else if (any_template_arguments_need_structural_equality_p (arglist
))
7679 /* Some of the template arguments require structural
7680 equality testing, so this template class requires
7681 structural equality testing. */
7682 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7687 /* If we called start_enum or pushtag above, this information
7688 will already be set up. */
7691 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7693 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7694 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7695 DECL_SOURCE_LOCATION (type_decl
)
7696 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7699 type_decl
= TYPE_NAME (t
);
7701 if (CLASS_TYPE_P (template_type
))
7703 TREE_PRIVATE (type_decl
)
7704 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7705 TREE_PROTECTED (type_decl
)
7706 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7707 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7709 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7710 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7714 /* Let's consider the explicit specialization of a member
7715 of a class template specialization that is implicitly instantiated,
7720 template<class U> struct M {}; //#0
7725 struct S<int>::M<char> //#1
7729 [temp.expl.spec]/4 says this is valid.
7731 In this case, when we write:
7734 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7737 When we encounter #1, we want to store the partial instantiation
7738 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7740 For all cases other than this "explicit specialization of member of a
7741 class template", we just want to store the most general template into
7742 the CLASSTYPE_TI_TEMPLATE of M.
7744 This case of "explicit specialization of member of a class template"
7746 1/ the enclosing class is an instantiation of, and therefore not
7747 the same as, the context of the most general template, and
7748 2/ we aren't looking at the partial instantiation itself, i.e.
7749 the innermost arguments are not the same as the innermost parms of
7750 the most general template.
7752 So it's only when 1/ and 2/ happens that we want to use the partial
7753 instantiation of the member template in lieu of its most general
7756 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7757 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7758 /* the enclosing class must be an instantiation... */
7759 && CLASS_TYPE_P (context
)
7760 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7762 tree partial_inst_args
;
7763 TREE_VEC_LENGTH (arglist
)--;
7764 ++processing_template_decl
;
7766 tsubst (INNERMOST_TEMPLATE_ARGS
7767 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7768 arglist
, complain
, NULL_TREE
);
7769 --processing_template_decl
;
7770 TREE_VEC_LENGTH (arglist
)++;
7771 use_partial_inst_tmpl
=
7772 /*...and we must not be looking at the partial instantiation
7774 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7778 if (!use_partial_inst_tmpl
)
7779 /* This case is easy; there are no member templates involved. */
7783 /* This is a full instantiation of a member template. Find
7784 the partial instantiation of which this is an instance. */
7786 /* Temporarily reduce by one the number of levels in the ARGLIST
7787 so as to avoid comparing the last set of arguments. */
7788 TREE_VEC_LENGTH (arglist
)--;
7789 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7790 TREE_VEC_LENGTH (arglist
)++;
7791 /* FOUND is either a proper class type, or an alias
7792 template specialization. In the later case, it's a
7793 TYPE_DECL, resulting from the substituting of arguments
7794 for parameters in the TYPE_DECL of the alias template
7795 done earlier. So be careful while getting the template
7797 found
= TREE_CODE (found
) == TYPE_DECL
7798 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7799 : CLASSTYPE_TI_TEMPLATE (found
);
7802 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7805 slot
= htab_find_slot_with_hash (type_specializations
,
7806 &elt
, hash
, INSERT
);
7807 entry
= ggc_alloc_spec_entry ();
7811 /* Note this use of the partial instantiation so we can check it
7812 later in maybe_process_partial_specialization. */
7813 DECL_TEMPLATE_INSTANTIATIONS (found
)
7814 = tree_cons (arglist
, t
,
7815 DECL_TEMPLATE_INSTANTIATIONS (found
));
7817 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7818 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7819 /* Now that the type has been registered on the instantiations
7820 list, we set up the enumerators. Because the enumeration
7821 constants may involve the enumeration type itself, we make
7822 sure to register the type first, and then create the
7823 constants. That way, doing tsubst_expr for the enumeration
7824 constants won't result in recursive calls here; we'll find
7825 the instantiation and exit above. */
7826 tsubst_enum (template_type
, t
, arglist
);
7828 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7829 /* If the type makes use of template parameters, the
7830 code that generates debugging information will crash. */
7831 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7833 /* Possibly limit visibility based on template args. */
7834 TREE_PUBLIC (type_decl
) = 1;
7835 determine_visibility (type_decl
);
7841 /* Wrapper for lookup_template_class_1. */
7844 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7845 int entering_scope
, tsubst_flags_t complain
)
7848 timevar_push (TV_TEMPLATE_INST
);
7849 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7850 entering_scope
, complain
);
7851 timevar_pop (TV_TEMPLATE_INST
);
7859 /* True when we should also visit template parameters that occur in
7860 non-deduced contexts. */
7861 bool include_nondeduced_p
;
7862 struct pointer_set_t
*visited
;
7865 /* Called from for_each_template_parm via walk_tree. */
7868 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7871 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7872 tree_fn_t fn
= pfd
->fn
;
7873 void *data
= pfd
->data
;
7876 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7877 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7878 pfd
->include_nondeduced_p
))
7879 return error_mark_node
;
7881 switch (TREE_CODE (t
))
7884 if (TYPE_PTRMEMFUNC_P (t
))
7890 if (!TYPE_TEMPLATE_INFO (t
))
7892 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7893 fn
, data
, pfd
->visited
,
7894 pfd
->include_nondeduced_p
))
7895 return error_mark_node
;
7899 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7900 fn
, data
, pfd
->visited
,
7901 pfd
->include_nondeduced_p
)
7902 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7903 fn
, data
, pfd
->visited
,
7904 pfd
->include_nondeduced_p
))
7905 return error_mark_node
;
7909 /* Since we're not going to walk subtrees, we have to do this
7911 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7912 pfd
->visited
, pfd
->include_nondeduced_p
))
7913 return error_mark_node
;
7917 /* Check the return type. */
7918 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7919 pfd
->include_nondeduced_p
))
7920 return error_mark_node
;
7922 /* Check the parameter types. Since default arguments are not
7923 instantiated until they are needed, the TYPE_ARG_TYPES may
7924 contain expressions that involve template parameters. But,
7925 no-one should be looking at them yet. And, once they're
7926 instantiated, they don't contain template parameters, so
7927 there's no point in looking at them then, either. */
7931 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7932 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7933 pfd
->visited
, pfd
->include_nondeduced_p
))
7934 return error_mark_node
;
7936 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7937 want walk_tree walking into them itself. */
7943 case UNDERLYING_TYPE
:
7944 if (pfd
->include_nondeduced_p
7945 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7947 pfd
->include_nondeduced_p
))
7948 return error_mark_node
;
7953 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7954 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7955 pfd
->visited
, pfd
->include_nondeduced_p
))
7956 return error_mark_node
;
7961 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7962 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7963 pfd
->visited
, pfd
->include_nondeduced_p
))
7964 return error_mark_node
;
7965 if (DECL_CONTEXT (t
)
7966 && pfd
->include_nondeduced_p
7967 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7968 pfd
->visited
, pfd
->include_nondeduced_p
))
7969 return error_mark_node
;
7972 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7973 /* Record template parameters such as `T' inside `TT<T>'. */
7974 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7975 pfd
->include_nondeduced_p
))
7976 return error_mark_node
;
7979 case TEMPLATE_TEMPLATE_PARM
:
7980 case TEMPLATE_TYPE_PARM
:
7981 case TEMPLATE_PARM_INDEX
:
7982 if (fn
&& (*fn
)(t
, data
))
7983 return error_mark_node
;
7985 return error_mark_node
;
7989 /* A template template parameter is encountered. */
7990 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7991 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7992 pfd
->include_nondeduced_p
))
7993 return error_mark_node
;
7995 /* Already substituted template template parameter */
8001 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8003 pfd
->include_nondeduced_p
))
8004 return error_mark_node
;
8008 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8009 && pfd
->include_nondeduced_p
8010 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8011 (TREE_TYPE (t
)), fn
, data
,
8012 pfd
->visited
, pfd
->include_nondeduced_p
))
8013 return error_mark_node
;
8018 /* If there's no type, then this thing must be some expression
8019 involving template parameters. */
8020 if (!fn
&& !TREE_TYPE (t
))
8021 return error_mark_node
;
8026 case IMPLICIT_CONV_EXPR
:
8027 case REINTERPRET_CAST_EXPR
:
8028 case CONST_CAST_EXPR
:
8029 case STATIC_CAST_EXPR
:
8030 case DYNAMIC_CAST_EXPR
:
8034 case PSEUDO_DTOR_EXPR
:
8036 return error_mark_node
;
8043 /* We didn't find any template parameters we liked. */
8047 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8048 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8049 call FN with the parameter and the DATA.
8050 If FN returns nonzero, the iteration is terminated, and
8051 for_each_template_parm returns 1. Otherwise, the iteration
8052 continues. If FN never returns a nonzero value, the value
8053 returned by for_each_template_parm is 0. If FN is NULL, it is
8054 considered to be the function which always returns 1.
8056 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8057 parameters that occur in non-deduced contexts. When false, only
8058 visits those template parameters that can be deduced. */
8061 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8062 struct pointer_set_t
*visited
,
8063 bool include_nondeduced_p
)
8065 struct pair_fn_data pfd
;
8071 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8073 /* Walk the tree. (Conceptually, we would like to walk without
8074 duplicates, but for_each_template_parm_r recursively calls
8075 for_each_template_parm, so we would need to reorganize a fair
8076 bit to use walk_tree_without_duplicates, so we keep our own
8079 pfd
.visited
= visited
;
8081 pfd
.visited
= pointer_set_create ();
8082 result
= cp_walk_tree (&t
,
8083 for_each_template_parm_r
,
8085 pfd
.visited
) != NULL_TREE
;
8090 pointer_set_destroy (pfd
.visited
);
8097 /* Returns true if T depends on any template parameter. */
8100 uses_template_parms (tree t
)
8103 int saved_processing_template_decl
;
8105 saved_processing_template_decl
= processing_template_decl
;
8106 if (!saved_processing_template_decl
)
8107 processing_template_decl
= 1;
8109 dependent_p
= dependent_type_p (t
);
8110 else if (TREE_CODE (t
) == TREE_VEC
)
8111 dependent_p
= any_dependent_template_arguments_p (t
);
8112 else if (TREE_CODE (t
) == TREE_LIST
)
8113 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8114 || uses_template_parms (TREE_CHAIN (t
)));
8115 else if (TREE_CODE (t
) == TYPE_DECL
)
8116 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8119 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8120 || TREE_CODE (t
) == OVERLOAD
8123 || TREE_CODE (t
) == TRAIT_EXPR
8124 || TREE_CODE (t
) == CONSTRUCTOR
8125 || CONSTANT_CLASS_P (t
))
8126 dependent_p
= (type_dependent_expression_p (t
)
8127 || value_dependent_expression_p (t
));
8130 gcc_assert (t
== error_mark_node
);
8131 dependent_p
= false;
8134 processing_template_decl
= saved_processing_template_decl
;
8139 /* Returns true iff current_function_decl is an incompletely instantiated
8140 template. Useful instead of processing_template_decl because the latter
8141 is set to 0 during fold_non_dependent_expr. */
8144 in_template_function (void)
8146 tree fn
= current_function_decl
;
8148 ++processing_template_decl
;
8149 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8150 && DECL_TEMPLATE_INFO (fn
)
8151 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8152 --processing_template_decl
;
8156 /* Returns true if T depends on any template parameter with level LEVEL. */
8159 uses_template_parms_level (tree t
, int level
)
8161 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8162 /*include_nondeduced_p=*/true);
8165 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8166 ill-formed translation unit, i.e. a variable or function that isn't
8167 usable in a constant expression. */
8170 neglectable_inst_p (tree d
)
8173 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8174 : decl_maybe_constant_var_p (d
)));
8177 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8178 neglectable and instantiated from within an erroneous instantiation. */
8181 limit_bad_template_recursion (tree decl
)
8183 struct tinst_level
*lev
= current_tinst_level
;
8184 int errs
= errorcount
+ sorrycount
;
8185 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8188 for (; lev
; lev
= lev
->next
)
8189 if (neglectable_inst_p (lev
->decl
))
8192 return (lev
&& errs
> lev
->errors
);
8195 static int tinst_depth
;
8196 extern int max_tinst_depth
;
8199 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8201 /* We're starting to instantiate D; record the template instantiation context
8202 for diagnostics and to restore it later. */
8205 push_tinst_level (tree d
)
8207 struct tinst_level
*new_level
;
8209 if (tinst_depth
>= max_tinst_depth
)
8211 last_error_tinst_level
= current_tinst_level
;
8212 if (TREE_CODE (d
) == TREE_LIST
)
8213 error ("template instantiation depth exceeds maximum of %d (use "
8214 "-ftemplate-depth= to increase the maximum) substituting %qS",
8215 max_tinst_depth
, d
);
8217 error ("template instantiation depth exceeds maximum of %d (use "
8218 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8219 max_tinst_depth
, d
);
8221 print_instantiation_context ();
8226 /* If the current instantiation caused problems, don't let it instantiate
8227 anything else. Do allow deduction substitution and decls usable in
8228 constant expressions. */
8229 if (limit_bad_template_recursion (d
))
8232 new_level
= ggc_alloc_tinst_level ();
8233 new_level
->decl
= d
;
8234 new_level
->locus
= input_location
;
8235 new_level
->errors
= errorcount
+sorrycount
;
8236 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8237 new_level
->next
= current_tinst_level
;
8238 current_tinst_level
= new_level
;
8241 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8242 depth_reached
= tinst_depth
;
8247 /* We're done instantiating this template; return to the instantiation
8251 pop_tinst_level (void)
8253 /* Restore the filename and line number stashed away when we started
8254 this instantiation. */
8255 input_location
= current_tinst_level
->locus
;
8256 current_tinst_level
= current_tinst_level
->next
;
8260 /* We're instantiating a deferred template; restore the template
8261 instantiation context in which the instantiation was requested, which
8262 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8265 reopen_tinst_level (struct tinst_level
*level
)
8267 struct tinst_level
*t
;
8270 for (t
= level
; t
; t
= t
->next
)
8273 current_tinst_level
= level
;
8275 if (current_tinst_level
)
8276 current_tinst_level
->errors
= errorcount
+sorrycount
;
8280 /* Returns the TINST_LEVEL which gives the original instantiation
8283 struct tinst_level
*
8284 outermost_tinst_level (void)
8286 struct tinst_level
*level
= current_tinst_level
;
8289 level
= level
->next
;
8293 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8294 vector of template arguments, as for tsubst.
8296 Returns an appropriate tsubst'd friend declaration. */
8299 tsubst_friend_function (tree decl
, tree args
)
8303 if (TREE_CODE (decl
) == FUNCTION_DECL
8304 && DECL_TEMPLATE_INSTANTIATION (decl
)
8305 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8306 /* This was a friend declared with an explicit template
8307 argument list, e.g.:
8311 to indicate that f was a template instantiation, not a new
8312 function declaration. Now, we have to figure out what
8313 instantiation of what template. */
8315 tree template_id
, arglist
, fns
;
8318 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8320 /* Friend functions are looked up in the containing namespace scope.
8321 We must enter that scope, to avoid finding member functions of the
8322 current class with same name. */
8323 push_nested_namespace (ns
);
8324 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8325 tf_warning_or_error
, NULL_TREE
,
8326 /*integral_constant_expression_p=*/false);
8327 pop_nested_namespace (ns
);
8328 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8329 tf_warning_or_error
, NULL_TREE
);
8330 template_id
= lookup_template_function (fns
, arglist
);
8332 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8333 tmpl
= determine_specialization (template_id
, new_friend
,
8335 /*need_member_template=*/0,
8336 TREE_VEC_LENGTH (args
),
8338 return instantiate_template (tmpl
, new_args
, tf_error
);
8341 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8343 /* The NEW_FRIEND will look like an instantiation, to the
8344 compiler, but is not an instantiation from the point of view of
8345 the language. For example, we might have had:
8347 template <class T> struct S {
8348 template <class U> friend void f(T, U);
8351 Then, in S<int>, template <class U> void f(int, U) is not an
8352 instantiation of anything. */
8353 if (new_friend
== error_mark_node
)
8354 return error_mark_node
;
8356 DECL_USE_TEMPLATE (new_friend
) = 0;
8357 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8359 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8360 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8361 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8364 /* The mangled name for the NEW_FRIEND is incorrect. The function
8365 is not a template instantiation and should not be mangled like
8366 one. Therefore, we forget the mangling here; we'll recompute it
8367 later if we need it. */
8368 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8370 SET_DECL_RTL (new_friend
, NULL
);
8371 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8374 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8377 tree new_friend_template_info
;
8378 tree new_friend_result_template_info
;
8380 int new_friend_is_defn
;
8382 /* We must save some information from NEW_FRIEND before calling
8383 duplicate decls since that function will free NEW_FRIEND if
8385 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8386 new_friend_is_defn
=
8387 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8388 (template_for_substitution (new_friend
)))
8390 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8392 /* This declaration is a `primary' template. */
8393 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8395 new_friend_result_template_info
8396 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8399 new_friend_result_template_info
= NULL_TREE
;
8401 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8402 if (new_friend_is_defn
)
8403 DECL_INITIAL (new_friend
) = error_mark_node
;
8405 /* Inside pushdecl_namespace_level, we will push into the
8406 current namespace. However, the friend function should go
8407 into the namespace of the template. */
8408 ns
= decl_namespace_context (new_friend
);
8409 push_nested_namespace (ns
);
8410 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8411 pop_nested_namespace (ns
);
8413 if (old_decl
== error_mark_node
)
8414 return error_mark_node
;
8416 if (old_decl
!= new_friend
)
8418 /* This new friend declaration matched an existing
8419 declaration. For example, given:
8421 template <class T> void f(T);
8422 template <class U> class C {
8423 template <class T> friend void f(T) {}
8426 the friend declaration actually provides the definition
8427 of `f', once C has been instantiated for some type. So,
8428 old_decl will be the out-of-class template declaration,
8429 while new_friend is the in-class definition.
8431 But, if `f' was called before this point, the
8432 instantiation of `f' will have DECL_TI_ARGS corresponding
8433 to `T' but not to `U', references to which might appear
8434 in the definition of `f'. Previously, the most general
8435 template for an instantiation of `f' was the out-of-class
8436 version; now it is the in-class version. Therefore, we
8437 run through all specialization of `f', adding to their
8438 DECL_TI_ARGS appropriately. In particular, they need a
8439 new set of outer arguments, corresponding to the
8440 arguments for this class instantiation.
8442 The same situation can arise with something like this:
8445 template <class T> class C {
8449 when `C<int>' is instantiated. Now, `f(int)' is defined
8452 if (!new_friend_is_defn
)
8453 /* On the other hand, if the in-class declaration does
8454 *not* provide a definition, then we don't want to alter
8455 existing definitions. We can just leave everything
8460 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8461 tree new_args
= TI_ARGS (new_friend_template_info
);
8463 /* Overwrite whatever template info was there before, if
8464 any, with the new template information pertaining to
8466 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8468 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8470 /* We should have called reregister_specialization in
8472 gcc_assert (retrieve_specialization (new_template
,
8476 /* Instantiate it if the global has already been used. */
8477 if (DECL_ODR_USED (old_decl
))
8478 instantiate_decl (old_decl
, /*defer_ok=*/true,
8479 /*expl_inst_class_mem_p=*/false);
8485 /* Indicate that the old function template is a partial
8487 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8488 = new_friend_result_template_info
;
8490 gcc_assert (new_template
8491 == most_general_template (new_template
));
8492 gcc_assert (new_template
!= old_decl
);
8494 /* Reassign any specializations already in the hash table
8495 to the new more general template, and add the
8496 additional template args. */
8497 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8501 tree spec
= TREE_VALUE (t
);
8504 elt
.tmpl
= old_decl
;
8505 elt
.args
= DECL_TI_ARGS (spec
);
8506 elt
.spec
= NULL_TREE
;
8508 htab_remove_elt (decl_specializations
, &elt
);
8511 = add_outermost_template_args (new_args
,
8512 DECL_TI_ARGS (spec
));
8514 register_specialization
8515 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8518 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8522 /* The information from NEW_FRIEND has been merged into OLD_DECL
8523 by duplicate_decls. */
8524 new_friend
= old_decl
;
8529 tree context
= DECL_CONTEXT (new_friend
);
8533 template <class T> class C {
8534 template <class U> friend void C1<U>::f (); // case 1
8535 friend void C2<T>::f (); // case 2
8537 we only need to make sure CONTEXT is a complete type for
8538 case 2. To distinguish between the two cases, we note that
8539 CONTEXT of case 1 remains dependent type after tsubst while
8540 this isn't true for case 2. */
8541 ++processing_template_decl
;
8542 dependent_p
= dependent_type_p (context
);
8543 --processing_template_decl
;
8546 && !complete_type_or_else (context
, NULL_TREE
))
8547 return error_mark_node
;
8549 if (COMPLETE_TYPE_P (context
))
8551 tree fn
= new_friend
;
8552 /* do_friend adds the TEMPLATE_DECL for any member friend
8553 template even if it isn't a member template, i.e.
8554 template <class T> friend A<T>::f();
8555 Look through it in that case. */
8556 if (TREE_CODE (fn
) == TEMPLATE_DECL
8557 && !PRIMARY_TEMPLATE_P (fn
))
8558 fn
= DECL_TEMPLATE_RESULT (fn
);
8559 /* Check to see that the declaration is really present, and,
8560 possibly obtain an improved declaration. */
8561 fn
= check_classfn (context
, fn
, NULL_TREE
);
8571 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8572 template arguments, as for tsubst.
8574 Returns an appropriate tsubst'd friend type or error_mark_node on
8578 tsubst_friend_class (tree friend_tmpl
, tree args
)
8584 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8586 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8587 return TREE_TYPE (t
);
8590 context
= CP_DECL_CONTEXT (friend_tmpl
);
8592 if (context
!= global_namespace
)
8594 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8595 push_nested_namespace (context
);
8597 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8600 /* Look for a class template declaration. We look for hidden names
8601 because two friend declarations of the same template are the
8602 same. For example, in:
8605 template <typename> friend class F;
8607 template <typename> struct B {
8608 template <typename> friend class F;
8611 both F templates are the same. */
8612 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8613 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8615 /* But, if we don't find one, it might be because we're in a
8616 situation like this:
8624 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8625 for `S<int>', not the TEMPLATE_DECL. */
8626 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8628 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8629 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8632 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8634 /* The friend template has already been declared. Just
8635 check to see that the declarations match, and install any new
8636 default parameters. We must tsubst the default parameters,
8637 of course. We only need the innermost template parameters
8638 because that is all that redeclare_class_template will look
8640 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8641 > TMPL_ARGS_DEPTH (args
))
8644 location_t saved_input_location
;
8645 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8646 args
, tf_warning_or_error
);
8648 saved_input_location
= input_location
;
8649 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8650 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8651 input_location
= saved_input_location
;
8655 friend_type
= TREE_TYPE (tmpl
);
8659 /* The friend template has not already been declared. In this
8660 case, the instantiation of the template class will cause the
8661 injection of this template into the global scope. */
8662 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8663 if (tmpl
== error_mark_node
)
8664 return error_mark_node
;
8666 /* The new TMPL is not an instantiation of anything, so we
8667 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8668 the new type because that is supposed to be the corresponding
8669 template decl, i.e., TMPL. */
8670 DECL_USE_TEMPLATE (tmpl
) = 0;
8671 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8672 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8673 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8674 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8676 /* Inject this template into the global scope. */
8677 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8680 if (context
!= global_namespace
)
8682 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8683 pop_nested_namespace (context
);
8685 pop_nested_class ();
8691 /* Returns zero if TYPE cannot be completed later due to circularity.
8692 Otherwise returns one. */
8695 can_complete_type_without_circularity (tree type
)
8697 if (type
== NULL_TREE
|| type
== error_mark_node
)
8699 else if (COMPLETE_TYPE_P (type
))
8701 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8702 return can_complete_type_without_circularity (TREE_TYPE (type
));
8703 else if (CLASS_TYPE_P (type
)
8704 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8710 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8712 /* Apply any attributes which had to be deferred until instantiation
8713 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8714 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8717 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8718 tree args
, tsubst_flags_t complain
, tree in_decl
)
8720 tree last_dep
= NULL_TREE
;
8724 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8725 if (ATTR_IS_DEPENDENT (t
))
8728 attributes
= copy_list (attributes
);
8732 if (DECL_P (*decl_p
))
8734 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8736 p
= &DECL_ATTRIBUTES (*decl_p
);
8739 p
= &TYPE_ATTRIBUTES (*decl_p
);
8743 tree late_attrs
= NULL_TREE
;
8744 tree
*q
= &late_attrs
;
8746 for (*p
= attributes
; *p
; )
8749 if (ATTR_IS_DEPENDENT (t
))
8751 *p
= TREE_CHAIN (t
);
8752 TREE_CHAIN (t
) = NULL_TREE
;
8753 if ((flag_openmp
|| flag_cilkplus
)
8754 && is_attribute_p ("omp declare simd",
8755 get_attribute_name (t
))
8758 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8759 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8761 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8762 clauses
= finish_omp_clauses (clauses
);
8763 tree parms
= DECL_ARGUMENTS (*decl_p
);
8765 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8767 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8769 TREE_VALUE (t
) = NULL_TREE
;
8771 /* If the first attribute argument is an identifier, don't
8772 pass it through tsubst. Attributes like mode, format,
8773 cleanup and several target specific attributes expect it
8775 else if (attribute_takes_identifier_p (get_attribute_name (t
))
8779 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8781 /*integral_constant_expression_p=*/false);
8782 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8784 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8789 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8790 /*integral_constant_expression_p=*/false);
8792 q
= &TREE_CHAIN (t
);
8795 p
= &TREE_CHAIN (t
);
8798 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8802 /* Perform (or defer) access check for typedefs that were referenced
8803 from within the template TMPL code.
8804 This is a subroutine of instantiate_decl and instantiate_class_template.
8805 TMPL is the template to consider and TARGS is the list of arguments of
8809 perform_typedefs_access_check (tree tmpl
, tree targs
)
8811 location_t saved_location
;
8813 qualified_typedef_usage_t
*iter
;
8816 || (!CLASS_TYPE_P (tmpl
)
8817 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8820 saved_location
= input_location
;
8821 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8823 tree type_decl
= iter
->typedef_decl
;
8824 tree type_scope
= iter
->context
;
8826 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8829 if (uses_template_parms (type_decl
))
8830 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8831 if (uses_template_parms (type_scope
))
8832 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8834 /* Make access check error messages point to the location
8835 of the use of the typedef. */
8836 input_location
= iter
->locus
;
8837 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8838 type_decl
, type_decl
,
8839 tf_warning_or_error
);
8841 input_location
= saved_location
;
8845 instantiate_class_template_1 (tree type
)
8847 tree templ
, args
, pattern
, t
, member
;
8851 unsigned int saved_maximum_field_alignment
;
8854 if (type
== error_mark_node
)
8855 return error_mark_node
;
8857 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8858 || uses_template_parms (type
))
8861 /* Figure out which template is being instantiated. */
8862 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8863 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8865 /* Determine what specialization of the original template to
8867 t
= most_specialized_class (type
, tf_warning_or_error
);
8868 if (t
== error_mark_node
)
8870 TYPE_BEING_DEFINED (type
) = 1;
8871 return error_mark_node
;
8875 /* This TYPE is actually an instantiation of a partial
8876 specialization. We replace the innermost set of ARGS with
8877 the arguments appropriate for substitution. For example,
8880 template <class T> struct S {};
8881 template <class T> struct S<T*> {};
8883 and supposing that we are instantiating S<int*>, ARGS will
8884 presently be {int*} -- but we need {int}. */
8885 pattern
= TREE_TYPE (t
);
8886 args
= TREE_PURPOSE (t
);
8890 pattern
= TREE_TYPE (templ
);
8891 args
= CLASSTYPE_TI_ARGS (type
);
8894 /* If the template we're instantiating is incomplete, then clearly
8895 there's nothing we can do. */
8896 if (!COMPLETE_TYPE_P (pattern
))
8899 /* If we've recursively instantiated too many templates, stop. */
8900 if (! push_tinst_level (type
))
8903 /* Now we're really doing the instantiation. Mark the type as in
8904 the process of being defined. */
8905 TYPE_BEING_DEFINED (type
) = 1;
8907 /* We may be in the middle of deferred access check. Disable
8909 push_deferring_access_checks (dk_no_deferred
);
8911 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8913 push_to_top_level ();
8914 /* Use #pragma pack from the template context. */
8915 saved_maximum_field_alignment
= maximum_field_alignment
;
8916 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8918 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8920 /* Set the input location to the most specialized template definition.
8921 This is needed if tsubsting causes an error. */
8922 typedecl
= TYPE_MAIN_DECL (pattern
);
8923 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8924 DECL_SOURCE_LOCATION (typedecl
);
8926 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8927 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8928 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8929 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8930 if (ANON_AGGR_TYPE_P (pattern
))
8931 SET_ANON_AGGR_TYPE_P (type
);
8932 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8934 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8935 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8936 /* Adjust visibility for template arguments. */
8937 determine_visibility (TYPE_MAIN_DECL (type
));
8939 if (CLASS_TYPE_P (type
))
8940 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8942 pbinfo
= TYPE_BINFO (pattern
);
8944 /* We should never instantiate a nested class before its enclosing
8945 class; we need to look up the nested class by name before we can
8946 instantiate it, and that lookup should instantiate the enclosing
8948 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8949 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8951 base_list
= NULL_TREE
;
8952 if (BINFO_N_BASE_BINFOS (pbinfo
))
8958 /* We must enter the scope containing the type, as that is where
8959 the accessibility of types named in dependent bases are
8961 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8963 /* Substitute into each of the bases to determine the actual
8965 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8968 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8969 tree expanded_bases
= NULL_TREE
;
8972 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8975 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8976 args
, tf_error
, NULL_TREE
);
8977 if (expanded_bases
== error_mark_node
)
8980 len
= TREE_VEC_LENGTH (expanded_bases
);
8983 for (idx
= 0; idx
< len
; idx
++)
8986 /* Extract the already-expanded base class. */
8987 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8989 /* Substitute to figure out the base class. */
8990 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8993 if (base
== error_mark_node
)
8996 base_list
= tree_cons (access
, base
, base_list
);
8997 if (BINFO_VIRTUAL_P (pbase_binfo
))
8998 TREE_TYPE (base_list
) = integer_type_node
;
9002 /* The list is now in reverse order; correct that. */
9003 base_list
= nreverse (base_list
);
9006 pop_scope (pushed_scope
);
9008 /* Now call xref_basetypes to set up all the base-class
9010 xref_basetypes (type
, base_list
);
9012 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9013 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9014 args
, tf_error
, NULL_TREE
);
9015 fixup_attribute_variants (type
);
9017 /* Now that our base classes are set up, enter the scope of the
9018 class, so that name lookups into base classes, etc. will work
9019 correctly. This is precisely analogous to what we do in
9020 begin_class_definition when defining an ordinary non-template
9021 class, except we also need to push the enclosing classes. */
9022 push_nested_class (type
);
9024 /* Now members are processed in the order of declaration. */
9025 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9026 member
; member
= TREE_CHAIN (member
))
9028 tree t
= TREE_VALUE (member
);
9030 if (TREE_PURPOSE (member
))
9034 /* Build new CLASSTYPE_NESTED_UTDS. */
9037 bool class_template_p
;
9039 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9040 && TYPE_LANG_SPECIFIC (t
)
9041 && CLASSTYPE_IS_TEMPLATE (t
));
9042 /* If the member is a class template, then -- even after
9043 substitution -- there may be dependent types in the
9044 template argument list for the class. We increment
9045 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9046 that function will assume that no types are dependent
9047 when outside of a template. */
9048 if (class_template_p
)
9049 ++processing_template_decl
;
9050 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9051 if (class_template_p
)
9052 --processing_template_decl
;
9053 if (newtag
== error_mark_node
)
9056 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9058 tree name
= TYPE_IDENTIFIER (t
);
9060 if (class_template_p
)
9061 /* Unfortunately, lookup_template_class sets
9062 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9063 instantiation (i.e., for the type of a member
9064 template class nested within a template class.)
9065 This behavior is required for
9066 maybe_process_partial_specialization to work
9067 correctly, but is not accurate in this case;
9068 the TAG is not an instantiation of anything.
9069 (The corresponding TEMPLATE_DECL is an
9070 instantiation, but the TYPE is not.) */
9071 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9073 /* Now, we call pushtag to put this NEWTAG into the scope of
9074 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9075 pushtag calling push_template_decl. We don't have to do
9076 this for enums because it will already have been done in
9079 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9080 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9083 else if (DECL_DECLARES_FUNCTION_P (t
))
9085 /* Build new TYPE_METHODS. */
9088 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9089 ++processing_template_decl
;
9090 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9091 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9092 --processing_template_decl
;
9093 set_current_access_from_decl (r
);
9094 finish_member_declaration (r
);
9095 /* Instantiate members marked with attribute used. */
9096 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9098 if (TREE_CODE (r
) == FUNCTION_DECL
9099 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9100 cp_check_omp_declare_reduction (r
);
9104 /* Build new TYPE_FIELDS. */
9105 if (TREE_CODE (t
) == STATIC_ASSERT
)
9109 ++c_inhibit_evaluation_warnings
;
9111 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9112 tf_warning_or_error
, NULL_TREE
,
9113 /*integral_constant_expression_p=*/true);
9114 --c_inhibit_evaluation_warnings
;
9116 finish_static_assert (condition
,
9117 STATIC_ASSERT_MESSAGE (t
),
9118 STATIC_ASSERT_SOURCE_LOCATION (t
),
9121 else if (TREE_CODE (t
) != CONST_DECL
)
9124 tree vec
= NULL_TREE
;
9127 /* The file and line for this declaration, to
9128 assist in error message reporting. Since we
9129 called push_tinst_level above, we don't need to
9131 input_location
= DECL_SOURCE_LOCATION (t
);
9133 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9134 ++processing_template_decl
;
9135 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9136 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9137 --processing_template_decl
;
9139 if (TREE_CODE (r
) == TREE_VEC
)
9141 /* A capture pack became multiple fields. */
9143 len
= TREE_VEC_LENGTH (vec
);
9146 for (int i
= 0; i
< len
; ++i
)
9149 r
= TREE_VEC_ELT (vec
, i
);
9154 [t]he initialization (and any associated
9155 side-effects) of a static data member does
9156 not occur unless the static data member is
9157 itself used in a way that requires the
9158 definition of the static data member to
9161 Therefore, we do not substitute into the
9162 initialized for the static data member here. */
9163 finish_static_data_member_decl
9166 /*init_const_expr_p=*/false,
9167 /*asmspec_tree=*/NULL_TREE
,
9169 /* Instantiate members marked with attribute used. */
9170 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9173 else if (TREE_CODE (r
) == FIELD_DECL
)
9175 /* Determine whether R has a valid type and can be
9176 completed later. If R is invalid, then its type
9177 is replaced by error_mark_node. */
9178 tree rtype
= TREE_TYPE (r
);
9179 if (can_complete_type_without_circularity (rtype
))
9180 complete_type (rtype
);
9182 if (!COMPLETE_TYPE_P (rtype
))
9184 cxx_incomplete_type_error (r
, rtype
);
9185 TREE_TYPE (r
) = error_mark_node
;
9189 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9190 such a thing will already have been added to the field
9191 list by tsubst_enum in finish_member_declaration in the
9192 CLASSTYPE_NESTED_UTDS case above. */
9193 if (!(TREE_CODE (r
) == TYPE_DECL
9194 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9195 && DECL_ARTIFICIAL (r
)))
9197 set_current_access_from_decl (r
);
9198 finish_member_declaration (r
);
9206 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9207 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9209 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9211 tree friend_type
= t
;
9212 bool adjust_processing_template_decl
= false;
9214 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9216 /* template <class T> friend class C; */
9217 friend_type
= tsubst_friend_class (friend_type
, args
);
9218 adjust_processing_template_decl
= true;
9220 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9222 /* template <class T> friend class C::D; */
9223 friend_type
= tsubst (friend_type
, args
,
9224 tf_warning_or_error
, NULL_TREE
);
9225 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9226 friend_type
= TREE_TYPE (friend_type
);
9227 adjust_processing_template_decl
= true;
9229 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9230 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9232 /* This could be either
9236 when dependent_type_p is false or
9238 template <class U> friend class T::C;
9241 friend_type
= tsubst (friend_type
, args
,
9242 tf_warning_or_error
, NULL_TREE
);
9243 /* Bump processing_template_decl for correct
9244 dependent_type_p calculation. */
9245 ++processing_template_decl
;
9246 if (dependent_type_p (friend_type
))
9247 adjust_processing_template_decl
= true;
9248 --processing_template_decl
;
9250 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9251 && hidden_name_p (TYPE_NAME (friend_type
)))
9255 where C hasn't been declared yet. Let's lookup name
9256 from namespace scope directly, bypassing any name that
9257 come from dependent base class. */
9258 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9260 /* The call to xref_tag_from_type does injection for friend
9262 push_nested_namespace (ns
);
9264 xref_tag_from_type (friend_type
, NULL_TREE
,
9265 /*tag_scope=*/ts_current
);
9266 pop_nested_namespace (ns
);
9268 else if (uses_template_parms (friend_type
))
9269 /* friend class C<T>; */
9270 friend_type
= tsubst (friend_type
, args
,
9271 tf_warning_or_error
, NULL_TREE
);
9276 where C is already declared or
9278 friend class C<int>;
9280 We don't have to do anything in these cases. */
9282 if (adjust_processing_template_decl
)
9283 /* Trick make_friend_class into realizing that the friend
9284 we're adding is a template, not an ordinary class. It's
9285 important that we use make_friend_class since it will
9286 perform some error-checking and output cross-reference
9288 ++processing_template_decl
;
9290 if (friend_type
!= error_mark_node
)
9291 make_friend_class (type
, friend_type
, /*complain=*/false);
9293 if (adjust_processing_template_decl
)
9294 --processing_template_decl
;
9298 /* Build new DECL_FRIENDLIST. */
9301 /* The file and line for this declaration, to
9302 assist in error message reporting. Since we
9303 called push_tinst_level above, we don't need to
9305 input_location
= DECL_SOURCE_LOCATION (t
);
9307 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9309 ++processing_template_decl
;
9310 push_deferring_access_checks (dk_no_check
);
9313 r
= tsubst_friend_function (t
, args
);
9314 add_friend (type
, r
, /*complain=*/false);
9315 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9317 pop_deferring_access_checks ();
9318 --processing_template_decl
;
9324 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9326 tree decl
= lambda_function (type
);
9329 if (!DECL_TEMPLATE_INFO (decl
)
9330 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9331 instantiate_decl (decl
, false, false);
9333 /* We need to instantiate the capture list from the template
9334 after we've instantiated the closure members, but before we
9335 consider adding the conversion op. Also keep any captures
9336 that may have been added during instantiation of the op(). */
9337 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9339 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9340 args
, tf_warning_or_error
, NULL_TREE
,
9343 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9344 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9346 maybe_add_lambda_conv_op (type
);
9349 gcc_assert (errorcount
);
9352 /* Set the file and line number information to whatever is given for
9353 the class itself. This puts error messages involving generated
9354 implicit functions at a predictable point, and the same point
9355 that would be used for non-template classes. */
9356 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9358 unreverse_member_declarations (type
);
9359 finish_struct_1 (type
);
9360 TYPE_BEING_DEFINED (type
) = 0;
9362 /* We don't instantiate default arguments for member functions. 14.7.1:
9364 The implicit instantiation of a class template specialization causes
9365 the implicit instantiation of the declarations, but not of the
9366 definitions or default arguments, of the class member functions,
9367 member classes, static data members and member templates.... */
9369 /* Some typedefs referenced from within the template code need to be access
9370 checked at template instantiation time, i.e now. These types were
9371 added to the template at parsing time. Let's get those and perform
9372 the access checks then. */
9373 perform_typedefs_access_check (pattern
, args
);
9374 perform_deferred_access_checks (tf_warning_or_error
);
9375 pop_nested_class ();
9376 maximum_field_alignment
= saved_maximum_field_alignment
;
9378 pop_from_top_level ();
9379 pop_deferring_access_checks ();
9382 /* The vtable for a template class can be emitted in any translation
9383 unit in which the class is instantiated. When there is no key
9384 method, however, finish_struct_1 will already have added TYPE to
9385 the keyed_classes list. */
9386 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9387 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9392 /* Wrapper for instantiate_class_template_1. */
9395 instantiate_class_template (tree type
)
9398 timevar_push (TV_TEMPLATE_INST
);
9399 ret
= instantiate_class_template_1 (type
);
9400 timevar_pop (TV_TEMPLATE_INST
);
9405 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9411 else if (TYPE_P (t
))
9412 r
= tsubst (t
, args
, complain
, in_decl
);
9415 if (!(complain
& tf_warning
))
9416 ++c_inhibit_evaluation_warnings
;
9417 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9418 /*integral_constant_expression_p=*/true);
9419 if (!(complain
& tf_warning
))
9420 --c_inhibit_evaluation_warnings
;
9425 /* Given a function parameter pack TMPL_PARM and some function parameters
9426 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9427 and set *SPEC_P to point at the next point in the list. */
9430 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9432 /* Collect all of the extra "packed" parameters into an
9436 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9437 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9438 tree spec_parm
= *spec_p
;
9441 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9443 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9446 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9447 parmvec
= make_tree_vec (len
);
9448 parmtypevec
= make_tree_vec (len
);
9449 spec_parm
= *spec_p
;
9450 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9452 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9453 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9456 /* Build the argument packs. */
9457 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9458 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9459 TREE_TYPE (argpack
) = argtypepack
;
9460 *spec_p
= spec_parm
;
9465 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9466 NONTYPE_ARGUMENT_PACK. */
9469 make_fnparm_pack (tree spec_parm
)
9471 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9474 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9478 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9480 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9481 if (i
>= TREE_VEC_LENGTH (vec
))
9483 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9487 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9490 make_argument_pack_select (tree arg_pack
, unsigned index
)
9492 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9494 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9495 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9500 /* This is a subroutine of tsubst_pack_expansion.
9502 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9503 mechanism to store the (non complete list of) arguments of the
9504 substitution and return a non substituted pack expansion, in order
9505 to wait for when we have enough arguments to really perform the
9509 use_pack_expansion_extra_args_p (tree parm_packs
,
9513 /* If one pack has an expansion and another pack has a normal
9514 argument or if one pack has an empty argument and an another
9515 one hasn't then tsubst_pack_expansion cannot perform the
9516 substitution and need to fall back on the
9517 PACK_EXPANSION_EXTRA mechanism. */
9518 if (parm_packs
== NULL_TREE
)
9520 else if (has_empty_arg
)
9523 bool has_expansion_arg
= false;
9524 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9526 bool has_non_expansion_arg
= false;
9527 for (tree parm_pack
= parm_packs
;
9529 parm_pack
= TREE_CHAIN (parm_pack
))
9531 tree arg
= TREE_VALUE (parm_pack
);
9533 if (argument_pack_element_is_expansion_p (arg
, i
))
9534 has_expansion_arg
= true;
9536 has_non_expansion_arg
= true;
9539 if (has_expansion_arg
&& has_non_expansion_arg
)
9545 /* [temp.variadic]/6 says that:
9547 The instantiation of a pack expansion [...]
9548 produces a list E1,E2, ..., En, where N is the number of elements
9549 in the pack expansion parameters.
9551 This subroutine of tsubst_pack_expansion produces one of these Ei.
9553 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9554 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9555 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9556 INDEX is the index 'i' of the element Ei to produce. ARGS,
9557 COMPLAIN, and IN_DECL are the same parameters as for the
9558 tsubst_pack_expansion function.
9560 The function returns the resulting Ei upon successful completion,
9563 Note that this function possibly modifies the ARGS parameter, so
9564 it's the responsibility of the caller to restore it. */
9567 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9570 tree args
/* This parm gets
9572 tsubst_flags_t complain
,
9576 bool ith_elem_is_expansion
= false;
9578 /* For each parameter pack, change the substitution of the parameter
9579 pack to the ith argument in its argument pack, then expand the
9581 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9583 tree parm
= TREE_PURPOSE (pack
);
9584 tree arg_pack
= TREE_VALUE (pack
);
9585 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9587 ith_elem_is_expansion
|=
9588 argument_pack_element_is_expansion_p (arg_pack
, index
);
9590 /* Select the Ith argument from the pack. */
9591 if (TREE_CODE (parm
) == PARM_DECL
9592 || TREE_CODE (parm
) == FIELD_DECL
)
9596 aps
= make_argument_pack_select (arg_pack
, index
);
9598 register_local_specialization (aps
, parm
);
9601 aps
= retrieve_local_specialization (parm
);
9606 template_parm_level_and_index (parm
, &level
, &idx
);
9610 aps
= make_argument_pack_select (arg_pack
, index
);
9611 /* Update the corresponding argument. */
9612 TMPL_ARG (args
, level
, idx
) = aps
;
9615 /* Re-use the ARGUMENT_PACK_SELECT. */
9616 aps
= TMPL_ARG (args
, level
, idx
);
9618 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9621 /* Substitute into the PATTERN with the (possibly altered)
9623 if (pattern
== in_decl
)
9624 /* Expanding a fixed parameter pack from
9625 coerce_template_parameter_pack. */
9626 t
= tsubst_decl (pattern
, args
, complain
);
9627 else if (!TYPE_P (pattern
))
9628 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9629 /*integral_constant_expression_p=*/false);
9631 t
= tsubst (pattern
, args
, complain
, in_decl
);
9633 /* If the Ith argument pack element is a pack expansion, then
9634 the Ith element resulting from the substituting is going to
9635 be a pack expansion as well. */
9636 if (ith_elem_is_expansion
)
9637 t
= make_pack_expansion (t
);
9642 /* Substitute ARGS into T, which is an pack expansion
9643 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9644 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9645 (if only a partial substitution could be performed) or
9646 ERROR_MARK_NODE if there was an error. */
9648 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9652 tree pack
, packs
= NULL_TREE
;
9653 bool unsubstituted_packs
= false;
9656 struct pointer_map_t
*saved_local_specializations
= NULL
;
9657 bool need_local_specializations
= false;
9660 gcc_assert (PACK_EXPANSION_P (t
));
9661 pattern
= PACK_EXPANSION_PATTERN (t
);
9663 /* Add in any args remembered from an earlier partial instantiation. */
9664 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9666 levels
= TMPL_ARGS_DEPTH (args
);
9668 /* Determine the argument packs that will instantiate the parameter
9669 packs used in the expansion expression. While we're at it,
9670 compute the number of arguments to be expanded and make sure it
9672 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9673 pack
= TREE_CHAIN (pack
))
9675 tree parm_pack
= TREE_VALUE (pack
);
9676 tree arg_pack
= NULL_TREE
;
9677 tree orig_arg
= NULL_TREE
;
9680 if (TREE_CODE (parm_pack
) == BASES
)
9682 if (BASES_DIRECT (parm_pack
))
9683 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9684 args
, complain
, in_decl
, false));
9686 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9687 args
, complain
, in_decl
, false));
9689 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9691 if (PACK_EXPANSION_LOCAL_P (t
))
9692 arg_pack
= retrieve_local_specialization (parm_pack
);
9695 /* We can't rely on local_specializations for a parameter
9696 name used later in a function declaration (such as in a
9697 late-specified return type). Even if it exists, it might
9698 have the wrong value for a recursive call. Just make a
9699 dummy decl, since it's only used for its type. */
9700 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9701 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9702 /* Partial instantiation of the parm_pack, we can't build
9703 up an argument pack yet. */
9704 arg_pack
= NULL_TREE
;
9706 arg_pack
= make_fnparm_pack (arg_pack
);
9707 need_local_specializations
= true;
9710 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9711 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9715 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9717 if (level
<= levels
)
9718 arg_pack
= TMPL_ARG (args
, level
, idx
);
9721 orig_arg
= arg_pack
;
9722 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9723 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9725 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9726 /* This can only happen if we forget to expand an argument
9727 pack somewhere else. Just return an error, silently. */
9729 result
= make_tree_vec (1);
9730 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9737 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9739 /* Don't bother trying to do a partial substitution with
9740 incomplete packs; we'll try again after deduction. */
9741 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9746 else if (len
!= my_len
)
9748 if (!(complain
& tf_error
))
9749 /* Fail quietly. */;
9750 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9751 error ("mismatched argument pack lengths while expanding "
9755 error ("mismatched argument pack lengths while expanding "
9758 return error_mark_node
;
9761 /* Keep track of the parameter packs and their corresponding
9763 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9764 TREE_TYPE (packs
) = orig_arg
;
9768 /* We can't substitute for this parameter pack. We use a flag as
9769 well as the missing_level counter because function parameter
9770 packs don't have a level. */
9771 unsubstituted_packs
= true;
9775 /* We cannot expand this expansion expression, because we don't have
9776 all of the argument packs we need. */
9777 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9779 /* We got some full packs, but we can't substitute them in until we
9780 have values for all the packs. So remember these until then. */
9782 t
= make_pack_expansion (pattern
);
9783 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9786 else if (unsubstituted_packs
)
9788 /* There were no real arguments, we're just replacing a parameter
9789 pack with another version of itself. Substitute into the
9790 pattern and return a PACK_EXPANSION_*. The caller will need to
9792 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9793 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9794 /*integral_constant_expression_p=*/false);
9796 t
= tsubst (pattern
, args
, complain
, in_decl
);
9797 t
= make_pack_expansion (t
);
9801 gcc_assert (len
>= 0);
9803 if (need_local_specializations
)
9805 /* We're in a late-specified return type, so create our own local
9806 specializations map; the current map is either NULL or (in the
9807 case of recursive unification) might have bindings that we don't
9808 want to use or alter. */
9809 saved_local_specializations
= local_specializations
;
9810 local_specializations
= pointer_map_create ();
9813 /* For each argument in each argument pack, substitute into the
9815 result
= make_tree_vec (len
);
9816 for (i
= 0; i
< len
; ++i
)
9818 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9822 TREE_VEC_ELT (result
, i
) = t
;
9823 if (t
== error_mark_node
)
9825 result
= error_mark_node
;
9830 /* Update ARGS to restore the substitution from parameter packs to
9831 their argument packs. */
9832 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9834 tree parm
= TREE_PURPOSE (pack
);
9836 if (TREE_CODE (parm
) == PARM_DECL
9837 || TREE_CODE (parm
) == FIELD_DECL
)
9838 register_local_specialization (TREE_TYPE (pack
), parm
);
9843 if (TREE_VALUE (pack
) == NULL_TREE
)
9846 template_parm_level_and_index (parm
, &level
, &idx
);
9848 /* Update the corresponding argument. */
9849 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9850 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9853 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9857 if (need_local_specializations
)
9859 pointer_map_destroy (local_specializations
);
9860 local_specializations
= saved_local_specializations
;
9866 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9867 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9868 parameter packs; all parms generated from a function parameter pack will
9869 have the same DECL_PARM_INDEX. */
9872 get_pattern_parm (tree parm
, tree tmpl
)
9874 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9877 if (DECL_ARTIFICIAL (parm
))
9879 for (patparm
= DECL_ARGUMENTS (pattern
);
9880 patparm
; patparm
= DECL_CHAIN (patparm
))
9881 if (DECL_ARTIFICIAL (patparm
)
9882 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9887 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9888 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9889 gcc_assert (DECL_PARM_INDEX (patparm
)
9890 == DECL_PARM_INDEX (parm
));
9896 /* Substitute ARGS into the vector or list of template arguments T. */
9899 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9902 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9905 if (t
== error_mark_node
)
9906 return error_mark_node
;
9908 len
= TREE_VEC_LENGTH (t
);
9909 elts
= XALLOCAVEC (tree
, len
);
9911 for (i
= 0; i
< len
; i
++)
9913 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9916 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9917 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9918 else if (PACK_EXPANSION_P (orig_arg
))
9920 /* Substitute into an expansion expression. */
9921 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9923 if (TREE_CODE (new_arg
) == TREE_VEC
)
9924 /* Add to the expanded length adjustment the number of
9925 expanded arguments. We subtract one from this
9926 measurement, because the argument pack expression
9927 itself is already counted as 1 in
9928 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9929 the argument pack is empty. */
9930 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9932 else if (ARGUMENT_PACK_P (orig_arg
))
9934 /* Substitute into each of the arguments. */
9935 new_arg
= TYPE_P (orig_arg
)
9936 ? cxx_make_type (TREE_CODE (orig_arg
))
9937 : make_node (TREE_CODE (orig_arg
));
9939 SET_ARGUMENT_PACK_ARGS (
9941 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9942 args
, complain
, in_decl
));
9944 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9945 new_arg
= error_mark_node
;
9947 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9948 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9950 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9952 if (TREE_TYPE (new_arg
) == error_mark_node
)
9953 new_arg
= error_mark_node
;
9957 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9959 if (new_arg
== error_mark_node
)
9960 return error_mark_node
;
9963 if (new_arg
!= orig_arg
)
9970 /* Make space for the expanded arguments coming from template
9972 t
= make_tree_vec (len
+ expanded_len_adjust
);
9973 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9974 arguments for a member template.
9975 In that case each TREE_VEC in ORIG_T represents a level of template
9976 arguments, and ORIG_T won't carry any non defaulted argument count.
9977 It will rather be the nested TREE_VECs that will carry one.
9978 In other words, ORIG_T carries a non defaulted argument count only
9979 if it doesn't contain any nested TREE_VEC. */
9980 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9982 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9983 count
+= expanded_len_adjust
;
9984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9986 for (i
= 0, out
= 0; i
< len
; i
++)
9988 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9989 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9990 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9994 /* Now expand the template argument pack "in place". */
9995 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9996 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10000 TREE_VEC_ELT (t
, out
) = elts
[i
];
10008 /* Return the result of substituting ARGS into the template parameters
10009 given by PARMS. If there are m levels of ARGS and m + n levels of
10010 PARMS, then the result will contain n levels of PARMS. For
10011 example, if PARMS is `template <class T> template <class U>
10012 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10013 result will be `template <int*, double, class V>'. */
10016 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10018 tree r
= NULL_TREE
;
10021 /* When substituting into a template, we must set
10022 PROCESSING_TEMPLATE_DECL as the template parameters may be
10023 dependent if they are based on one-another, and the dependency
10024 predicates are short-circuit outside of templates. */
10025 ++processing_template_decl
;
10027 for (new_parms
= &r
;
10028 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10029 new_parms
= &(TREE_CHAIN (*new_parms
)),
10030 parms
= TREE_CHAIN (parms
))
10033 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10036 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10040 if (parms
== error_mark_node
)
10043 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10045 if (tuple
== error_mark_node
)
10048 TREE_VEC_ELT (new_vec
, i
) =
10049 tsubst_template_parm (tuple
, args
, complain
);
10053 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10054 - TMPL_ARGS_DEPTH (args
)),
10055 new_vec
, NULL_TREE
);
10058 --processing_template_decl
;
10063 /* Return the result of substituting ARGS into one template parameter
10064 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10065 parameter and which TREE_PURPOSE is the default argument of the
10066 template parameter. */
10069 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10071 tree default_value
, parm_decl
;
10073 if (args
== NULL_TREE
10075 || t
== error_mark_node
)
10078 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10080 default_value
= TREE_PURPOSE (t
);
10081 parm_decl
= TREE_VALUE (t
);
10083 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10084 if (TREE_CODE (parm_decl
) == PARM_DECL
10085 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10086 parm_decl
= error_mark_node
;
10087 default_value
= tsubst_template_arg (default_value
, args
,
10088 complain
, NULL_TREE
);
10090 return build_tree_list (default_value
, parm_decl
);
10093 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10094 type T. If T is not an aggregate or enumeration type, it is
10095 handled as if by tsubst. IN_DECL is as for tsubst. If
10096 ENTERING_SCOPE is nonzero, T is the context for a template which
10097 we are presently tsubst'ing. Return the substituted value. */
10100 tsubst_aggr_type (tree t
,
10102 tsubst_flags_t complain
,
10104 int entering_scope
)
10106 if (t
== NULL_TREE
)
10109 switch (TREE_CODE (t
))
10112 if (TYPE_PTRMEMFUNC_P (t
))
10113 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10115 /* Else fall through. */
10116 case ENUMERAL_TYPE
:
10118 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10123 int saved_unevaluated_operand
;
10124 int saved_inhibit_evaluation_warnings
;
10126 /* In "sizeof(X<I>)" we need to evaluate "I". */
10127 saved_unevaluated_operand
= cp_unevaluated_operand
;
10128 cp_unevaluated_operand
= 0;
10129 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10130 c_inhibit_evaluation_warnings
= 0;
10132 /* First, determine the context for the type we are looking
10134 context
= TYPE_CONTEXT (t
);
10135 if (context
&& TYPE_P (context
))
10137 context
= tsubst_aggr_type (context
, args
, complain
,
10138 in_decl
, /*entering_scope=*/1);
10139 /* If context is a nested class inside a class template,
10140 it may still need to be instantiated (c++/33959). */
10141 context
= complete_type (context
);
10144 /* Then, figure out what arguments are appropriate for the
10145 type we are trying to find. For example, given:
10147 template <class T> struct S;
10148 template <class T, class U> void f(T, U) { S<U> su; }
10150 and supposing that we are instantiating f<int, double>,
10151 then our ARGS will be {int, double}, but, when looking up
10152 S we only want {double}. */
10153 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10154 complain
, in_decl
);
10155 if (argvec
== error_mark_node
)
10156 r
= error_mark_node
;
10159 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10160 entering_scope
, complain
);
10161 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10164 cp_unevaluated_operand
= saved_unevaluated_operand
;
10165 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10170 /* This is not a template type, so there's nothing to do. */
10174 return tsubst (t
, args
, complain
, in_decl
);
10178 /* Substitute into the default argument ARG (a default argument for
10179 FN), which has the indicated TYPE. */
10182 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10184 tree saved_class_ptr
= NULL_TREE
;
10185 tree saved_class_ref
= NULL_TREE
;
10186 int errs
= errorcount
+ sorrycount
;
10188 /* This can happen in invalid code. */
10189 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10192 /* This default argument came from a template. Instantiate the
10193 default argument here, not in tsubst. In the case of
10202 we must be careful to do name lookup in the scope of S<T>,
10203 rather than in the current class. */
10204 push_access_scope (fn
);
10205 /* The "this" pointer is not valid in a default argument. */
10208 saved_class_ptr
= current_class_ptr
;
10209 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10210 saved_class_ref
= current_class_ref
;
10211 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10214 push_deferring_access_checks(dk_no_deferred
);
10215 /* The default argument expression may cause implicitly defined
10216 member functions to be synthesized, which will result in garbage
10217 collection. We must treat this situation as if we were within
10218 the body of function so as to avoid collecting live data on the
10221 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10222 complain
, NULL_TREE
,
10223 /*integral_constant_expression_p=*/false);
10225 pop_deferring_access_checks();
10227 /* Restore the "this" pointer. */
10230 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10231 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10234 if (errorcount
+sorrycount
> errs
10235 && (complain
& tf_warning_or_error
))
10236 inform (input_location
,
10237 " when instantiating default argument for call to %D", fn
);
10239 /* Make sure the default argument is reasonable. */
10240 arg
= check_default_argument (type
, arg
, complain
);
10242 pop_access_scope (fn
);
10247 /* Substitute into all the default arguments for FN. */
10250 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10255 tmpl_args
= DECL_TI_ARGS (fn
);
10257 /* If this function is not yet instantiated, we certainly don't need
10258 its default arguments. */
10259 if (uses_template_parms (tmpl_args
))
10261 /* Don't do this again for clones. */
10262 if (DECL_CLONED_FUNCTION_P (fn
))
10265 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10267 arg
= TREE_CHAIN (arg
))
10268 if (TREE_PURPOSE (arg
))
10269 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10271 TREE_PURPOSE (arg
),
10275 /* Substitute the ARGS into the T, which is a _DECL. Return the
10276 result of the substitution. Issue error and warning messages under
10277 control of COMPLAIN. */
10280 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10282 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10283 location_t saved_loc
;
10284 tree r
= NULL_TREE
;
10286 hashval_t hash
= 0;
10288 /* Set the filename and linenumber to improve error-reporting. */
10289 saved_loc
= input_location
;
10290 input_location
= DECL_SOURCE_LOCATION (t
);
10292 switch (TREE_CODE (t
))
10294 case TEMPLATE_DECL
:
10296 /* We can get here when processing a member function template,
10297 member class template, or template template parameter. */
10298 tree decl
= DECL_TEMPLATE_RESULT (t
);
10303 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10305 /* Template template parameter is treated here. */
10306 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10307 if (new_type
== error_mark_node
)
10308 RETURN (error_mark_node
);
10309 /* If we get a real template back, return it. This can happen in
10310 the context of most_specialized_class. */
10311 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10315 DECL_CHAIN (r
) = NULL_TREE
;
10316 TREE_TYPE (r
) = new_type
;
10317 DECL_TEMPLATE_RESULT (r
)
10318 = build_decl (DECL_SOURCE_LOCATION (decl
),
10319 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10320 DECL_TEMPLATE_PARMS (r
)
10321 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10323 TYPE_NAME (new_type
) = r
;
10327 /* We might already have an instance of this template.
10328 The ARGS are for the surrounding class type, so the
10329 full args contain the tsubst'd args for the context,
10330 plus the innermost args from the template decl. */
10331 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10332 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10333 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10334 /* Because this is a template, the arguments will still be
10335 dependent, even after substitution. If
10336 PROCESSING_TEMPLATE_DECL is not set, the dependency
10337 predicates will short-circuit. */
10338 ++processing_template_decl
;
10339 full_args
= tsubst_template_args (tmpl_args
, args
,
10340 complain
, in_decl
);
10341 --processing_template_decl
;
10342 if (full_args
== error_mark_node
)
10343 RETURN (error_mark_node
);
10345 /* If this is a default template template argument,
10346 tsubst might not have changed anything. */
10347 if (full_args
== tmpl_args
)
10350 hash
= hash_tmpl_and_args (t
, full_args
);
10351 spec
= retrieve_specialization (t
, full_args
, hash
);
10352 if (spec
!= NULL_TREE
)
10358 /* Make a new template decl. It will be similar to the
10359 original, but will record the current template arguments.
10360 We also create a new function declaration, which is just
10361 like the old one, but points to this new template, rather
10362 than the old one. */
10364 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10365 DECL_CHAIN (r
) = NULL_TREE
;
10367 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10369 if (TREE_CODE (decl
) == TYPE_DECL
10370 && !TYPE_DECL_ALIAS_P (decl
))
10373 ++processing_template_decl
;
10374 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10375 --processing_template_decl
;
10376 if (new_type
== error_mark_node
)
10377 RETURN (error_mark_node
);
10379 TREE_TYPE (r
) = new_type
;
10380 /* For a partial specialization, we need to keep pointing to
10381 the primary template. */
10382 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10383 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10384 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10385 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10386 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10391 ++processing_template_decl
;
10392 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10393 --processing_template_decl
;
10394 if (new_decl
== error_mark_node
)
10395 RETURN (error_mark_node
);
10397 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10398 DECL_TI_TEMPLATE (new_decl
) = r
;
10399 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10400 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10401 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10404 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10405 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10406 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10408 /* The template parameters for this new template are all the
10409 template parameters for the old template, except the
10410 outermost level of parameters. */
10411 DECL_TEMPLATE_PARMS (r
)
10412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10415 if (PRIMARY_TEMPLATE_P (t
))
10416 DECL_PRIMARY_TEMPLATE (r
) = r
;
10418 if (TREE_CODE (decl
) != TYPE_DECL
)
10419 /* Record this non-type partial instantiation. */
10420 register_specialization (r
, t
,
10421 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10426 case FUNCTION_DECL
:
10429 tree argvec
= NULL_TREE
;
10437 /* Nobody should be tsubst'ing into non-template functions. */
10438 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10440 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10445 /* If T is not dependent, just return it. We have to
10446 increment PROCESSING_TEMPLATE_DECL because
10447 value_dependent_expression_p assumes that nothing is
10448 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10449 ++processing_template_decl
;
10450 dependent_p
= value_dependent_expression_p (t
);
10451 --processing_template_decl
;
10455 /* Calculate the most general template of which R is a
10456 specialization, and the complete set of arguments used to
10458 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10459 argvec
= tsubst_template_args (DECL_TI_ARGS
10460 (DECL_TEMPLATE_RESULT
10461 (DECL_TI_TEMPLATE (t
))),
10462 args
, complain
, in_decl
);
10463 if (argvec
== error_mark_node
)
10464 RETURN (error_mark_node
);
10466 /* Check to see if we already have this specialization. */
10467 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10468 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10476 /* We can see more levels of arguments than parameters if
10477 there was a specialization of a member template, like
10480 template <class T> struct S { template <class U> void f(); }
10481 template <> template <class U> void S<int>::f(U);
10483 Here, we'll be substituting into the specialization,
10484 because that's where we can find the code we actually
10485 want to generate, but we'll have enough arguments for
10486 the most general template.
10488 We also deal with the peculiar case:
10490 template <class T> struct S {
10491 template <class U> friend void f();
10493 template <class U> void f() {}
10495 template void f<double>();
10497 Here, the ARGS for the instantiation of will be {int,
10498 double}. But, we only need as many ARGS as there are
10499 levels of template parameters in CODE_PATTERN. We are
10500 careful not to get fooled into reducing the ARGS in
10503 template <class T> struct S { template <class U> void f(U); }
10504 template <class T> template <> void S<T>::f(int) {}
10506 which we can spot because the pattern will be a
10507 specialization in this case. */
10508 args_depth
= TMPL_ARGS_DEPTH (args
);
10510 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10511 if (args_depth
> parms_depth
10512 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10513 args
= get_innermost_template_args (args
, parms_depth
);
10517 /* This special case arises when we have something like this:
10519 template <class T> struct S {
10520 friend void f<int>(int, double);
10523 Here, the DECL_TI_TEMPLATE for the friend declaration
10524 will be an IDENTIFIER_NODE. We are being called from
10525 tsubst_friend_function, and we want only to create a
10526 new decl (R) with appropriate types so that we can call
10527 determine_specialization. */
10528 gen_tmpl
= NULL_TREE
;
10531 if (DECL_CLASS_SCOPE_P (t
))
10533 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10537 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10538 complain
, t
, /*entering_scope=*/1);
10543 ctx
= DECL_CONTEXT (t
);
10545 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10546 if (type
== error_mark_node
)
10547 RETURN (error_mark_node
);
10549 /* If we hit excessive deduction depth, the type is bogus even if
10550 it isn't error_mark_node, so don't build a decl. */
10551 if (excessive_deduction_depth
)
10552 RETURN (error_mark_node
);
10554 /* We do NOT check for matching decls pushed separately at this
10555 point, as they may not represent instantiations of this
10556 template, and in any case are considered separate under the
10559 DECL_USE_TEMPLATE (r
) = 0;
10560 TREE_TYPE (r
) = type
;
10561 /* Clear out the mangled name and RTL for the instantiation. */
10562 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10563 SET_DECL_RTL (r
, NULL
);
10564 /* Leave DECL_INITIAL set on deleted instantiations. */
10565 if (!DECL_DELETED_FN (r
))
10566 DECL_INITIAL (r
) = NULL_TREE
;
10567 DECL_CONTEXT (r
) = ctx
;
10569 /* OpenMP UDRs have the only argument a reference to the declared
10570 type. We want to diagnose if the declared type is a reference,
10571 which is invalid, but as references to references are usually
10572 quietly merged, diagnose it here. */
10573 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10576 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10577 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10578 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10579 error_at (DECL_SOURCE_LOCATION (t
),
10580 "reference type %qT in "
10581 "%<#pragma omp declare reduction%>", argtype
);
10582 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10583 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10587 if (member
&& DECL_CONV_FN_P (r
))
10588 /* Type-conversion operator. Reconstruct the name, in
10589 case it's the name of one of the template's parameters. */
10590 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10592 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10594 DECL_RESULT (r
) = NULL_TREE
;
10596 TREE_STATIC (r
) = 0;
10597 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10598 DECL_EXTERNAL (r
) = 1;
10599 /* If this is an instantiation of a function with internal
10600 linkage, we already know what object file linkage will be
10601 assigned to the instantiation. */
10602 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10603 DECL_DEFER_OUTPUT (r
) = 0;
10604 DECL_CHAIN (r
) = NULL_TREE
;
10605 DECL_PENDING_INLINE_INFO (r
) = 0;
10606 DECL_PENDING_INLINE_P (r
) = 0;
10607 DECL_SAVED_TREE (r
) = NULL_TREE
;
10608 DECL_STRUCT_FUNCTION (r
) = NULL
;
10610 /* We'll re-clone as appropriate in instantiate_template. */
10611 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10613 /* If we aren't complaining now, return on error before we register
10614 the specialization so that we'll complain eventually. */
10615 if ((complain
& tf_error
) == 0
10616 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10617 && !grok_op_properties (r
, /*complain=*/false))
10618 RETURN (error_mark_node
);
10620 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10621 this in the special friend case mentioned above where
10622 GEN_TMPL is NULL. */
10625 DECL_TEMPLATE_INFO (r
)
10626 = build_template_info (gen_tmpl
, argvec
);
10627 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10630 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10632 /* We instantiated this while substituting into
10633 the type earlier (template/friend54.C). */
10636 /* We're not supposed to instantiate default arguments
10637 until they are called, for a template. But, for a
10640 template <class T> void f ()
10641 { extern void g(int i = T()); }
10643 we should do the substitution when the template is
10644 instantiated. We handle the member function case in
10645 instantiate_class_template since the default arguments
10646 might refer to other members of the class. */
10648 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10649 && !uses_template_parms (argvec
))
10650 tsubst_default_arguments (r
, complain
);
10653 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10655 /* Copy the list of befriending classes. */
10656 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10658 friends
= &TREE_CHAIN (*friends
))
10660 *friends
= copy_node (*friends
);
10661 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10666 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10668 maybe_retrofit_in_chrg (r
);
10669 if (DECL_CONSTRUCTOR_P (r
))
10670 grok_ctor_properties (ctx
, r
);
10671 if (DECL_INHERITED_CTOR_BASE (r
))
10672 deduce_inheriting_ctor (r
);
10673 /* If this is an instantiation of a member template, clone it.
10674 If it isn't, that'll be handled by
10675 clone_constructors_and_destructors. */
10676 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10677 clone_function_decl (r
, /*update_method_vec_p=*/0);
10679 else if ((complain
& tf_error
) != 0
10680 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10681 && !grok_op_properties (r
, /*complain=*/true))
10682 RETURN (error_mark_node
);
10684 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10685 SET_DECL_FRIEND_CONTEXT (r
,
10686 tsubst (DECL_FRIEND_CONTEXT (t
),
10687 args
, complain
, in_decl
));
10689 /* Possibly limit visibility based on template args. */
10690 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10691 if (DECL_VISIBILITY_SPECIFIED (t
))
10693 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10694 DECL_ATTRIBUTES (r
)
10695 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10697 determine_visibility (r
);
10698 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10699 && !processing_template_decl
)
10700 defaulted_late_check (r
);
10702 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10703 args
, complain
, in_decl
);
10709 tree type
= NULL_TREE
;
10711 tree expanded_types
= NULL_TREE
;
10712 tree prev_r
= NULL_TREE
;
10713 tree first_r
= NULL_TREE
;
10715 if (DECL_PACK_P (t
))
10717 /* If there is a local specialization that isn't a
10718 parameter pack, it means that we're doing a "simple"
10719 substitution from inside tsubst_pack_expansion. Just
10720 return the local specialization (which will be a single
10722 tree spec
= retrieve_local_specialization (t
);
10724 && TREE_CODE (spec
) == PARM_DECL
10725 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10728 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10729 the parameters in this function parameter pack. */
10730 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10731 complain
, in_decl
);
10732 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10734 len
= TREE_VEC_LENGTH (expanded_types
);
10736 /* Zero-length parameter packs are boring. Just substitute
10739 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10744 /* All we did was update the type. Make a note of that. */
10745 type
= expanded_types
;
10746 expanded_types
= NULL_TREE
;
10750 /* Loop through all of the parameters we'll build. When T is
10751 a function parameter pack, LEN is the number of expanded
10752 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10754 for (i
= 0; i
< len
; ++i
)
10758 if (DECL_TEMPLATE_PARM_P (t
))
10759 SET_DECL_TEMPLATE_PARM_P (r
);
10761 if (expanded_types
)
10762 /* We're on the Ith parameter of the function parameter
10765 /* Get the Ith type. */
10766 type
= TREE_VEC_ELT (expanded_types
, i
);
10768 /* Rename the parameter to include the index. */
10770 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10773 /* We're dealing with a normal parameter. */
10774 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10776 type
= type_decays_to (type
);
10777 TREE_TYPE (r
) = type
;
10778 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10780 if (DECL_INITIAL (r
))
10782 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10783 DECL_INITIAL (r
) = TREE_TYPE (r
);
10785 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10786 complain
, in_decl
);
10789 DECL_CONTEXT (r
) = NULL_TREE
;
10791 if (!DECL_TEMPLATE_PARM_P (r
))
10792 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10794 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10795 args
, complain
, in_decl
);
10797 /* Keep track of the first new parameter we
10798 generate. That's what will be returned to the
10803 /* Build a proper chain of parameters when substituting
10804 into a function parameter pack. */
10806 DECL_CHAIN (prev_r
) = r
;
10809 /* If cp_unevaluated_operand is set, we're just looking for a
10810 single dummy parameter, so don't keep going. */
10811 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10812 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10813 complain
, DECL_CHAIN (t
));
10815 /* FIRST_R contains the start of the chain we've built. */
10822 tree type
= NULL_TREE
;
10823 tree vec
= NULL_TREE
;
10824 tree expanded_types
= NULL_TREE
;
10827 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10829 /* This field is a lambda capture pack. Return a TREE_VEC of
10830 the expanded fields to instantiate_class_template_1 and
10831 store them in the specializations hash table as a
10832 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10833 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10834 complain
, in_decl
);
10835 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10837 len
= TREE_VEC_LENGTH (expanded_types
);
10838 vec
= make_tree_vec (len
);
10842 /* All we did was update the type. Make a note of that. */
10843 type
= expanded_types
;
10844 expanded_types
= NULL_TREE
;
10848 for (int i
= 0; i
< len
; ++i
)
10851 if (expanded_types
)
10853 type
= TREE_VEC_ELT (expanded_types
, i
);
10855 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10858 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10860 if (type
== error_mark_node
)
10861 RETURN (error_mark_node
);
10862 TREE_TYPE (r
) = type
;
10863 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10865 if (DECL_C_BIT_FIELD (r
))
10866 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10867 non-bit-fields DECL_INITIAL is a non-static data member
10868 initializer, which gets deferred instantiation. */
10870 = tsubst_expr (DECL_INITIAL (t
), args
,
10872 /*integral_constant_expression_p=*/true);
10873 else if (DECL_INITIAL (t
))
10875 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10876 NSDMI in perform_member_init. Still set DECL_INITIAL
10877 so that we know there is one. */
10878 DECL_INITIAL (r
) = void_zero_node
;
10879 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10880 retrofit_lang_decl (r
);
10881 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10883 /* We don't have to set DECL_CONTEXT here; it is set by
10884 finish_member_declaration. */
10885 DECL_CHAIN (r
) = NULL_TREE
;
10887 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10888 args
, complain
, in_decl
);
10891 TREE_VEC_ELT (vec
, i
) = r
;
10897 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10898 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10899 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10900 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10901 TREE_TYPE (pack
) = tpack
;
10902 register_specialization (pack
, t
, args
, false, 0);
10908 /* We reach here only for member using decls. We also need to check
10909 uses_template_parms because DECL_DEPENDENT_P is not set for a
10910 using-declaration that designates a member of the current
10911 instantiation (c++/53549). */
10912 if (DECL_DEPENDENT_P (t
)
10913 || uses_template_parms (USING_DECL_SCOPE (t
)))
10915 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10916 complain
, in_decl
);
10917 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10918 r
= do_class_using_decl (inst_scope
, name
);
10920 r
= error_mark_node
;
10923 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10924 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10930 DECL_CHAIN (r
) = NULL_TREE
;
10937 tree argvec
= NULL_TREE
;
10938 tree gen_tmpl
= NULL_TREE
;
10940 tree tmpl
= NULL_TREE
;
10942 tree type
= NULL_TREE
;
10945 if (TREE_TYPE (t
) == error_mark_node
)
10946 RETURN (error_mark_node
);
10948 if (TREE_CODE (t
) == TYPE_DECL
10949 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10951 /* If this is the canonical decl, we don't have to
10952 mess with instantiations, and often we can't (for
10953 typename, template type parms and such). Note that
10954 TYPE_NAME is not correct for the above test if
10955 we've copied the type for a typedef. */
10956 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10957 if (type
== error_mark_node
)
10958 RETURN (error_mark_node
);
10959 r
= TYPE_NAME (type
);
10963 /* Check to see if we already have the specialization we
10966 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10968 /* T is a static data member or namespace-scope entity.
10969 We have to substitute into namespace-scope variables
10970 (even though such entities are never templates) because
10973 template <class T> void f() { extern T t; }
10975 where the entity referenced is not known until
10976 instantiation time. */
10978 ctx
= DECL_CONTEXT (t
);
10979 if (DECL_CLASS_SCOPE_P (t
))
10981 ctx
= tsubst_aggr_type (ctx
, args
,
10983 in_decl
, /*entering_scope=*/1);
10984 /* If CTX is unchanged, then T is in fact the
10985 specialization we want. That situation occurs when
10986 referencing a static data member within in its own
10987 class. We can use pointer equality, rather than
10988 same_type_p, because DECL_CONTEXT is always
10990 if (ctx
== DECL_CONTEXT (t
)
10991 && (TREE_CODE (t
) != TYPE_DECL
10992 /* ... unless T is a member template; in which
10993 case our caller can be willing to create a
10994 specialization of that template represented
10996 || !(DECL_TI_TEMPLATE (t
)
10997 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
11003 tmpl
= DECL_TI_TEMPLATE (t
);
11004 gen_tmpl
= most_general_template (tmpl
);
11005 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11006 if (argvec
== error_mark_node
)
11007 RETURN (error_mark_node
);
11008 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11009 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11014 /* A local variable. */
11016 /* Subsequent calls to pushdecl will fill this in. */
11018 spec
= retrieve_local_specialization (t
);
11020 /* If we already have the specialization we need, there is
11021 nothing more to do. */
11028 /* Create a new node for the specialization we need. */
11030 if (type
== NULL_TREE
)
11032 if (is_typedef_decl (t
))
11033 type
= DECL_ORIGINAL_TYPE (t
);
11035 type
= TREE_TYPE (t
);
11037 && VAR_HAD_UNKNOWN_BOUND (t
)
11038 && type
!= error_mark_node
)
11039 type
= strip_array_domain (type
);
11040 type
= tsubst (type
, args
, complain
, in_decl
);
11044 /* Even if the original location is out of scope, the
11045 newly substituted one is not. */
11046 DECL_DEAD_FOR_LOCAL (r
) = 0;
11047 DECL_INITIALIZED_P (r
) = 0;
11048 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11049 if (type
== error_mark_node
)
11050 RETURN (error_mark_node
);
11051 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11053 /* It may seem that this case cannot occur, since:
11058 declares a function, not a variable. However:
11061 template <typename T> void g() { T t; }
11062 template void g<f>();
11064 is an attempt to declare a variable with function
11066 error ("variable %qD has function type",
11067 /* R is not yet sufficiently initialized, so we
11068 just use its name. */
11070 RETURN (error_mark_node
);
11072 type
= complete_type (type
);
11073 /* Wait until cp_finish_decl to set this again, to handle
11074 circular dependency (template/instantiate6.C). */
11075 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11076 type
= check_var_type (DECL_NAME (r
), type
);
11078 if (DECL_HAS_VALUE_EXPR_P (t
))
11080 tree ve
= DECL_VALUE_EXPR (t
);
11081 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11082 /*constant_expression_p=*/false);
11083 if (REFERENCE_REF_P (ve
))
11085 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11086 ve
= TREE_OPERAND (ve
, 0);
11088 SET_DECL_VALUE_EXPR (r
, ve
);
11091 else if (DECL_SELF_REFERENCE_P (t
))
11092 SET_DECL_SELF_REFERENCE_P (r
);
11093 TREE_TYPE (r
) = type
;
11094 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11095 DECL_CONTEXT (r
) = ctx
;
11096 /* Clear out the mangled name and RTL for the instantiation. */
11097 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11098 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11099 SET_DECL_RTL (r
, NULL
);
11100 /* The initializer must not be expanded until it is required;
11101 see [temp.inst]. */
11102 DECL_INITIAL (r
) = NULL_TREE
;
11103 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11104 SET_DECL_RTL (r
, NULL
);
11105 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11108 /* Possibly limit visibility based on template args. */
11109 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11110 if (DECL_VISIBILITY_SPECIFIED (t
))
11112 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11113 DECL_ATTRIBUTES (r
)
11114 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11116 determine_visibility (r
);
11121 /* A static data member declaration is always marked
11122 external when it is declared in-class, even if an
11123 initializer is present. We mimic the non-template
11124 processing here. */
11125 DECL_EXTERNAL (r
) = 1;
11127 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11128 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
11129 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11131 else if (!cp_unevaluated_operand
)
11132 register_local_specialization (r
, t
);
11134 DECL_CHAIN (r
) = NULL_TREE
;
11136 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11138 args
, complain
, in_decl
);
11140 /* Preserve a typedef that names a type. */
11141 if (is_typedef_decl (r
))
11143 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11144 set_underlying_type (r
);
11147 layout_decl (r
, 0);
11152 gcc_unreachable ();
11157 /* Restore the file and line information. */
11158 input_location
= saved_loc
;
11163 /* Substitute into the ARG_TYPES of a function type.
11164 If END is a TREE_CHAIN, leave it and any following types
11168 tsubst_arg_types (tree arg_types
,
11171 tsubst_flags_t complain
,
11174 tree remaining_arg_types
;
11175 tree type
= NULL_TREE
;
11177 tree expanded_args
= NULL_TREE
;
11180 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11183 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11184 args
, end
, complain
, in_decl
);
11185 if (remaining_arg_types
== error_mark_node
)
11186 return error_mark_node
;
11188 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11190 /* For a pack expansion, perform substitution on the
11191 entire expression. Later on, we'll handle the arguments
11193 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11194 args
, complain
, in_decl
);
11196 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11197 /* So that we'll spin through the parameters, one by one. */
11198 i
= TREE_VEC_LENGTH (expanded_args
);
11201 /* We only partially substituted into the parameter
11202 pack. Our type is TYPE_PACK_EXPANSION. */
11203 type
= expanded_args
;
11204 expanded_args
= NULL_TREE
;
11212 type
= TREE_VEC_ELT (expanded_args
, i
);
11214 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11216 if (type
== error_mark_node
)
11217 return error_mark_node
;
11218 if (VOID_TYPE_P (type
))
11220 if (complain
& tf_error
)
11222 error ("invalid parameter type %qT", type
);
11224 error ("in declaration %q+D", in_decl
);
11226 return error_mark_node
;
11229 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11230 return error_mark_node
;
11232 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11233 top-level qualifiers as required. */
11234 type
= cv_unqualified (type_decays_to (type
));
11236 /* We do not substitute into default arguments here. The standard
11237 mandates that they be instantiated only when needed, which is
11238 done in build_over_call. */
11239 default_arg
= TREE_PURPOSE (arg_types
);
11241 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11243 /* We've instantiated a template before its default arguments
11244 have been parsed. This can happen for a nested template
11245 class, and is not an error unless we require the default
11246 argument in a call of this function. */
11247 remaining_arg_types
=
11248 tree_cons (default_arg
, type
, remaining_arg_types
);
11249 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11252 remaining_arg_types
=
11253 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11256 return remaining_arg_types
;
11259 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11260 *not* handle the exception-specification for FNTYPE, because the
11261 initial substitution of explicitly provided template parameters
11262 during argument deduction forbids substitution into the
11263 exception-specification:
11267 All references in the function type of the function template to the
11268 corresponding template parameters are replaced by the specified tem-
11269 plate argument values. If a substitution in a template parameter or
11270 in the function type of the function template results in an invalid
11271 type, type deduction fails. [Note: The equivalent substitution in
11272 exception specifications is done only when the function is instanti-
11273 ated, at which point a program is ill-formed if the substitution
11274 results in an invalid type.] */
11277 tsubst_function_type (tree t
,
11279 tsubst_flags_t complain
,
11286 /* The TYPE_CONTEXT is not used for function/method types. */
11287 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11289 /* Substitute the return type. */
11290 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11291 if (return_type
== error_mark_node
)
11292 return error_mark_node
;
11293 /* DR 486 clarifies that creation of a function type with an
11294 invalid return type is a deduction failure. */
11295 if (TREE_CODE (return_type
) == ARRAY_TYPE
11296 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11298 if (complain
& tf_error
)
11300 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11301 error ("function returning an array");
11303 error ("function returning a function");
11305 return error_mark_node
;
11308 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11309 return error_mark_node
;
11311 /* Substitute the argument types. */
11312 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11313 complain
, in_decl
);
11314 if (arg_types
== error_mark_node
)
11315 return error_mark_node
;
11317 /* Construct a new type node and return it. */
11318 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11320 fntype
= build_function_type (return_type
, arg_types
);
11321 fntype
= apply_memfn_quals (fntype
,
11322 type_memfn_quals (t
),
11323 type_memfn_rqual (t
));
11327 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11328 /* Don't pick up extra function qualifiers from the basetype. */
11329 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
11330 if (! MAYBE_CLASS_TYPE_P (r
))
11334 Type deduction may fail for any of the following
11337 -- Attempting to create "pointer to member of T" when T
11338 is not a class type. */
11339 if (complain
& tf_error
)
11340 error ("creating pointer to member function of non-class type %qT",
11342 return error_mark_node
;
11345 fntype
= build_method_type_directly (r
, return_type
,
11346 TREE_CHAIN (arg_types
));
11347 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11349 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11354 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11355 ARGS into that specification, and return the substituted
11356 specification. If there is no specification, return NULL_TREE. */
11359 tsubst_exception_specification (tree fntype
,
11361 tsubst_flags_t complain
,
11368 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11369 new_specs
= NULL_TREE
;
11370 if (specs
&& TREE_PURPOSE (specs
))
11372 /* A noexcept-specifier. */
11373 tree expr
= TREE_PURPOSE (specs
);
11374 if (TREE_CODE (expr
) == INTEGER_CST
)
11378 /* Defer instantiation of noexcept-specifiers to avoid
11379 excessive instantiations (c++/49107). */
11380 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11381 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11383 /* We already partially instantiated this member template,
11384 so combine the new args with the old. */
11385 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11386 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11387 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11388 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11392 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11393 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11397 new_specs
= tsubst_copy_and_build
11398 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11399 /*integral_constant_expression_p=*/true);
11400 new_specs
= build_noexcept_spec (new_specs
, complain
);
11404 if (! TREE_VALUE (specs
))
11411 tree expanded_specs
= NULL_TREE
;
11413 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11415 /* Expand the pack expansion type. */
11416 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11420 if (expanded_specs
== error_mark_node
)
11421 return error_mark_node
;
11422 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11423 len
= TREE_VEC_LENGTH (expanded_specs
);
11426 /* We're substituting into a member template, so
11427 we got a TYPE_PACK_EXPANSION back. Add that
11428 expansion and move on. */
11429 gcc_assert (TREE_CODE (expanded_specs
)
11430 == TYPE_PACK_EXPANSION
);
11431 new_specs
= add_exception_specifier (new_specs
,
11434 specs
= TREE_CHAIN (specs
);
11439 for (i
= 0; i
< len
; ++i
)
11441 if (expanded_specs
)
11442 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11444 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11445 if (spec
== error_mark_node
)
11447 new_specs
= add_exception_specifier (new_specs
, spec
,
11451 specs
= TREE_CHAIN (specs
);
11457 /* Take the tree structure T and replace template parameters used
11458 therein with the argument vector ARGS. IN_DECL is an associated
11459 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11460 Issue error and warning messages under control of COMPLAIN. Note
11461 that we must be relatively non-tolerant of extensions here, in
11462 order to preserve conformance; if we allow substitutions that
11463 should not be allowed, we may allow argument deductions that should
11464 not succeed, and therefore report ambiguous overload situations
11465 where there are none. In theory, we could allow the substitution,
11466 but indicate that it should have failed, and allow our caller to
11467 make sure that the right thing happens, but we don't try to do this
11470 This function is used for dealing with types, decls and the like;
11471 for expressions, use tsubst_expr or tsubst_copy. */
11474 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11476 enum tree_code code
;
11477 tree type
, r
= NULL_TREE
;
11479 if (t
== NULL_TREE
|| t
== error_mark_node
11480 || t
== integer_type_node
11481 || t
== void_type_node
11482 || t
== char_type_node
11483 || t
== unknown_type_node
11484 || TREE_CODE (t
) == NAMESPACE_DECL
11485 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11489 return tsubst_decl (t
, args
, complain
);
11491 if (args
== NULL_TREE
)
11494 code
= TREE_CODE (t
);
11496 if (code
== IDENTIFIER_NODE
)
11497 type
= IDENTIFIER_TYPE_VALUE (t
);
11499 type
= TREE_TYPE (t
);
11501 gcc_assert (type
!= unknown_type_node
);
11503 /* Reuse typedefs. We need to do this to handle dependent attributes,
11504 such as attribute aligned. */
11506 && typedef_variant_p (t
))
11508 tree decl
= TYPE_NAME (t
);
11510 if (alias_template_specialization_p (t
))
11512 /* DECL represents an alias template and we want to
11514 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11515 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11516 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11518 else if (DECL_CLASS_SCOPE_P (decl
)
11519 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11520 && uses_template_parms (DECL_CONTEXT (decl
)))
11522 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11523 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11524 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11526 else if (DECL_FUNCTION_SCOPE_P (decl
)
11527 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11528 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11529 r
= retrieve_local_specialization (decl
);
11531 /* The typedef is from a non-template context. */
11537 r
= cp_build_qualified_type_real
11538 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11539 complain
| tf_ignore_bad_quals
);
11544 /* We don't have an instantiation yet, so drop the typedef. */
11545 int quals
= cp_type_quals (t
);
11546 t
= DECL_ORIGINAL_TYPE (decl
);
11547 t
= cp_build_qualified_type_real (t
, quals
,
11548 complain
| tf_ignore_bad_quals
);
11553 && code
!= TYPENAME_TYPE
11554 && code
!= TEMPLATE_TYPE_PARM
11555 && code
!= IDENTIFIER_NODE
11556 && code
!= FUNCTION_TYPE
11557 && code
!= METHOD_TYPE
)
11558 type
= tsubst (type
, args
, complain
, in_decl
);
11559 if (type
== error_mark_node
)
11560 return error_mark_node
;
11566 case ENUMERAL_TYPE
:
11567 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11568 /*entering_scope=*/0);
11571 case IDENTIFIER_NODE
:
11582 if (t
== integer_type_node
)
11585 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11586 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11590 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11592 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11593 /*integral_constant_expression_p=*/false);
11595 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11597 if (TREE_CODE (max
) == NOP_EXPR
11598 && TREE_SIDE_EFFECTS (omax
)
11599 && !TREE_TYPE (max
))
11600 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11602 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11603 with TREE_SIDE_EFFECTS that indicates this is not an integral
11604 constant expression. */
11605 if (processing_template_decl
11606 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11608 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11609 TREE_SIDE_EFFECTS (max
) = 1;
11612 return compute_array_index_type (NULL_TREE
, max
, complain
);
11615 case TEMPLATE_TYPE_PARM
:
11616 case TEMPLATE_TEMPLATE_PARM
:
11617 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11618 case TEMPLATE_PARM_INDEX
:
11623 tree arg
= NULL_TREE
;
11627 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11628 template_parm_level_and_index (t
, &level
, &idx
);
11630 levels
= TMPL_ARGS_DEPTH (args
);
11631 if (level
<= levels
)
11633 arg
= TMPL_ARG (args
, level
, idx
);
11635 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11637 /* See through ARGUMENT_PACK_SELECT arguments. */
11638 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11639 /* If the selected argument is an expansion E, that most
11640 likely means we were called from
11641 gen_elem_of_pack_expansion_instantiation during the
11642 substituting of pack an argument pack (which Ith
11643 element is a pack expansion, where I is
11644 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11645 In this case, the Ith element resulting from this
11646 substituting is going to be a pack expansion, which
11647 pattern is the pattern of E. Let's return the
11649 gen_elem_of_pack_expansion_instantiation will
11650 build the resulting pack expansion from it. */
11651 if (PACK_EXPANSION_P (arg
))
11652 arg
= PACK_EXPANSION_PATTERN (arg
);
11656 if (arg
== error_mark_node
)
11657 return error_mark_node
;
11658 else if (arg
!= NULL_TREE
)
11660 if (ARGUMENT_PACK_P (arg
))
11661 /* If ARG is an argument pack, we don't actually want to
11662 perform a substitution here, because substitutions
11663 for argument packs are only done
11664 element-by-element. We can get to this point when
11665 substituting the type of a non-type template
11666 parameter pack, when that type actually contains
11667 template parameter packs from an outer template, e.g.,
11669 template<typename... Types> struct A {
11670 template<Types... Values> struct B { };
11674 if (code
== TEMPLATE_TYPE_PARM
)
11677 gcc_assert (TYPE_P (arg
));
11679 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11681 return cp_build_qualified_type_real
11682 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11684 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11686 /* We are processing a type constructed from a
11687 template template parameter. */
11688 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11689 args
, complain
, in_decl
);
11690 if (argvec
== error_mark_node
)
11691 return error_mark_node
;
11693 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11694 || TREE_CODE (arg
) == TEMPLATE_DECL
11695 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11697 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11698 /* Consider this code:
11700 template <template <class> class Template>
11702 template <class Arg> using Bind = Template<Arg>;
11705 template <template <class> class Template, class Arg>
11706 using Instantiate = Template<Arg>; //#0
11708 template <template <class> class Template,
11711 Instantiate<Internal<Template>::template Bind,
11714 When #1 is parsed, the
11715 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11716 parameter `Template' in #0 matches the
11717 UNBOUND_CLASS_TEMPLATE representing the argument
11718 `Internal<Template>::template Bind'; We then want
11719 to assemble the type `Bind<Argument>' that can't
11720 be fully created right now, because
11721 `Internal<Template>' not being complete, the Bind
11722 template cannot be looked up in that context. So
11723 we need to "store" `Bind<Argument>' for later
11724 when the context of Bind becomes complete. Let's
11725 store that in a TYPENAME_TYPE. */
11726 return make_typename_type (TYPE_CONTEXT (arg
),
11727 build_nt (TEMPLATE_ID_EXPR
,
11728 TYPE_IDENTIFIER (arg
),
11733 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11734 are resolving nested-types in the signature of a
11735 member function templates. Otherwise ARG is a
11736 TEMPLATE_DECL and is the real template to be
11738 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11739 arg
= TYPE_NAME (arg
);
11741 r
= lookup_template_class (arg
,
11743 DECL_CONTEXT (arg
),
11744 /*entering_scope=*/0,
11746 return cp_build_qualified_type_real
11747 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11750 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11751 return convert_from_reference (unshare_expr (arg
));
11755 /* This can happen during the attempted tsubst'ing in
11756 unify. This means that we don't yet have any information
11757 about the template parameter in question. */
11760 /* Early in template argument deduction substitution, we don't
11761 want to reduce the level of 'auto', or it will be confused
11762 with a normal template parm in subsequent deduction. */
11763 if (is_auto (t
) && (complain
& tf_partial
))
11766 /* If we get here, we must have been looking at a parm for a
11767 more deeply nested template. Make a new version of this
11768 template parameter, but with a lower level. */
11771 case TEMPLATE_TYPE_PARM
:
11772 case TEMPLATE_TEMPLATE_PARM
:
11773 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11774 if (cp_type_quals (t
))
11776 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11777 r
= cp_build_qualified_type_real
11778 (r
, cp_type_quals (t
),
11779 complain
| (code
== TEMPLATE_TYPE_PARM
11780 ? tf_ignore_bad_quals
: 0));
11785 TEMPLATE_TYPE_PARM_INDEX (r
)
11786 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11787 r
, levels
, args
, complain
);
11788 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11789 TYPE_MAIN_VARIANT (r
) = r
;
11790 TYPE_POINTER_TO (r
) = NULL_TREE
;
11791 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11793 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11794 /* We have reduced the level of the template
11795 template parameter, but not the levels of its
11796 template parameters, so canonical_type_parameter
11797 will not be able to find the canonical template
11798 template parameter for this level. Thus, we
11799 require structural equality checking to compare
11800 TEMPLATE_TEMPLATE_PARMs. */
11801 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11802 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11803 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11805 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11807 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11809 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11810 complain
, in_decl
);
11811 if (argvec
== error_mark_node
)
11812 return error_mark_node
;
11814 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11815 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11820 case TEMPLATE_PARM_INDEX
:
11821 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11825 gcc_unreachable ();
11833 tree purpose
, value
, chain
;
11835 if (t
== void_list_node
)
11838 purpose
= TREE_PURPOSE (t
);
11841 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11842 if (purpose
== error_mark_node
)
11843 return error_mark_node
;
11845 value
= TREE_VALUE (t
);
11848 value
= tsubst (value
, args
, complain
, in_decl
);
11849 if (value
== error_mark_node
)
11850 return error_mark_node
;
11852 chain
= TREE_CHAIN (t
);
11853 if (chain
&& chain
!= void_type_node
)
11855 chain
= tsubst (chain
, args
, complain
, in_decl
);
11856 if (chain
== error_mark_node
)
11857 return error_mark_node
;
11859 if (purpose
== TREE_PURPOSE (t
)
11860 && value
== TREE_VALUE (t
)
11861 && chain
== TREE_CHAIN (t
))
11863 return hash_tree_cons (purpose
, value
, chain
);
11867 /* We should never be tsubsting a binfo. */
11868 gcc_unreachable ();
11871 /* A vector of template arguments. */
11872 gcc_assert (!type
);
11873 return tsubst_template_args (t
, args
, complain
, in_decl
);
11876 case REFERENCE_TYPE
:
11878 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11883 Type deduction may fail for any of the following
11886 -- Attempting to create a pointer to reference type.
11887 -- Attempting to create a reference to a reference type or
11888 a reference to void.
11890 Core issue 106 says that creating a reference to a reference
11891 during instantiation is no longer a cause for failure. We
11892 only enforce this check in strict C++98 mode. */
11893 if ((TREE_CODE (type
) == REFERENCE_TYPE
11894 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11895 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11897 static location_t last_loc
;
11899 /* We keep track of the last time we issued this error
11900 message to avoid spewing a ton of messages during a
11901 single bad template instantiation. */
11902 if (complain
& tf_error
11903 && last_loc
!= input_location
)
11905 if (VOID_TYPE_P (type
))
11906 error ("forming reference to void");
11907 else if (code
== POINTER_TYPE
)
11908 error ("forming pointer to reference type %qT", type
);
11910 error ("forming reference to reference type %qT", type
);
11911 last_loc
= input_location
;
11914 return error_mark_node
;
11916 else if (TREE_CODE (type
) == FUNCTION_TYPE
11917 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11918 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11920 if (complain
& tf_error
)
11922 if (code
== POINTER_TYPE
)
11923 error ("forming pointer to qualified function type %qT",
11926 error ("forming reference to qualified function type %qT",
11929 return error_mark_node
;
11931 else if (code
== POINTER_TYPE
)
11933 r
= build_pointer_type (type
);
11934 if (TREE_CODE (type
) == METHOD_TYPE
)
11935 r
= build_ptrmemfunc_type (r
);
11937 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11938 /* In C++0x, during template argument substitution, when there is an
11939 attempt to create a reference to a reference type, reference
11940 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11942 "If a template-argument for a template-parameter T names a type
11943 that is a reference to a type A, an attempt to create the type
11944 'lvalue reference to cv T' creates the type 'lvalue reference to
11945 A,' while an attempt to create the type type rvalue reference to
11946 cv T' creates the type T"
11948 r
= cp_build_reference_type
11950 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11952 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11953 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11955 if (cxx_dialect
>= cxx1y
11956 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11957 && array_of_runtime_bound_p (type
))
11959 if (complain
& tf_warning_or_error
)
11961 (input_location
, OPT_Wvla
,
11962 code
== REFERENCE_TYPE
11963 ? G_("cannot declare reference to array of runtime bound")
11964 : G_("cannot declare pointer to array of runtime bound"));
11966 r
= error_mark_node
;
11969 if (r
!= error_mark_node
)
11970 /* Will this ever be needed for TYPE_..._TO values? */
11977 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11978 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11982 Type deduction may fail for any of the following
11985 -- Attempting to create "pointer to member of T" when T
11986 is not a class type. */
11987 if (complain
& tf_error
)
11988 error ("creating pointer to member of non-class type %qT", r
);
11989 return error_mark_node
;
11991 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11993 if (complain
& tf_error
)
11994 error ("creating pointer to member reference type %qT", type
);
11995 return error_mark_node
;
11997 if (VOID_TYPE_P (type
))
11999 if (complain
& tf_error
)
12000 error ("creating pointer to member of type void");
12001 return error_mark_node
;
12003 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12004 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12006 /* The type of the implicit object parameter gets its
12007 cv-qualifiers from the FUNCTION_TYPE. */
12010 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12011 type_memfn_rqual (type
));
12012 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12013 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12017 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12021 case FUNCTION_TYPE
:
12026 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12027 if (fntype
== error_mark_node
)
12028 return error_mark_node
;
12030 /* Substitute the exception specification. */
12031 specs
= tsubst_exception_specification (t
, args
, complain
,
12032 in_decl
, /*defer_ok*/true);
12033 if (specs
== error_mark_node
)
12034 return error_mark_node
;
12036 fntype
= build_exception_variant (fntype
, specs
);
12041 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12042 if (domain
== error_mark_node
)
12043 return error_mark_node
;
12045 /* As an optimization, we avoid regenerating the array type if
12046 it will obviously be the same as T. */
12047 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12050 /* These checks should match the ones in grokdeclarator.
12054 The deduction may fail for any of the following reasons:
12056 -- Attempting to create an array with an element type that
12057 is void, a function type, or a reference type, or [DR337]
12058 an abstract class type. */
12059 if (VOID_TYPE_P (type
)
12060 || TREE_CODE (type
) == FUNCTION_TYPE
12061 || TREE_CODE (type
) == REFERENCE_TYPE
)
12063 if (complain
& tf_error
)
12064 error ("creating array of %qT", type
);
12065 return error_mark_node
;
12068 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12069 return error_mark_node
;
12071 r
= build_cplus_array_type (type
, domain
);
12073 if (TYPE_USER_ALIGN (t
))
12075 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12076 TYPE_USER_ALIGN (r
) = 1;
12082 case TYPENAME_TYPE
:
12084 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12085 in_decl
, /*entering_scope=*/1);
12086 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12087 complain
, in_decl
);
12089 if (ctx
== error_mark_node
|| f
== error_mark_node
)
12090 return error_mark_node
;
12092 if (!MAYBE_CLASS_TYPE_P (ctx
))
12094 if (complain
& tf_error
)
12095 error ("%qT is not a class, struct, or union type", ctx
);
12096 return error_mark_node
;
12098 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
12100 /* Normally, make_typename_type does not require that the CTX
12101 have complete type in order to allow things like:
12103 template <class T> struct S { typename S<T>::X Y; };
12105 But, such constructs have already been resolved by this
12106 point, so here CTX really should have complete type, unless
12107 it's a partial instantiation. */
12108 ctx
= complete_type (ctx
);
12109 if (!COMPLETE_TYPE_P (ctx
))
12111 if (complain
& tf_error
)
12112 cxx_incomplete_type_error (NULL_TREE
, ctx
);
12113 return error_mark_node
;
12117 f
= make_typename_type (ctx
, f
, typename_type
,
12118 complain
| tf_keep_type_decl
);
12119 if (f
== error_mark_node
)
12121 if (TREE_CODE (f
) == TYPE_DECL
)
12123 complain
|= tf_ignore_bad_quals
;
12127 if (TREE_CODE (f
) != TYPENAME_TYPE
)
12129 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
12131 if (complain
& tf_error
)
12132 error ("%qT resolves to %qT, which is not an enumeration type",
12135 return error_mark_node
;
12137 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
12139 if (complain
& tf_error
)
12140 error ("%qT resolves to %qT, which is is not a class type",
12143 return error_mark_node
;
12147 return cp_build_qualified_type_real
12148 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12151 case UNBOUND_CLASS_TEMPLATE
:
12153 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12154 in_decl
, /*entering_scope=*/1);
12155 tree name
= TYPE_IDENTIFIER (t
);
12156 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12158 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12159 return error_mark_node
;
12162 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12163 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12170 ++cp_unevaluated_operand
;
12171 ++c_inhibit_evaluation_warnings
;
12173 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12175 /*integral_constant_expression_p=*/false);
12177 --cp_unevaluated_operand
;
12178 --c_inhibit_evaluation_warnings
;
12180 type
= finish_typeof (type
);
12181 return cp_build_qualified_type_real (type
,
12183 | cp_type_quals (type
),
12187 case DECLTYPE_TYPE
:
12191 ++cp_unevaluated_operand
;
12192 ++c_inhibit_evaluation_warnings
;
12194 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12195 complain
|tf_decltype
, in_decl
,
12196 /*function_p*/false,
12197 /*integral_constant_expression*/false);
12199 --cp_unevaluated_operand
;
12200 --c_inhibit_evaluation_warnings
;
12202 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12203 type
= lambda_capture_field_type (type
,
12204 DECLTYPE_FOR_INIT_CAPTURE (t
));
12205 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12206 type
= lambda_proxy_type (type
);
12209 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12210 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12212 /* In a template ~id could be either a complement expression
12213 or an unqualified-id naming a destructor; if instantiating
12214 it produces an expression, it's not an id-expression or
12217 type
= finish_decltype_type (type
, id
, complain
);
12219 return cp_build_qualified_type_real (type
,
12221 | cp_type_quals (type
),
12225 case UNDERLYING_TYPE
:
12227 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12228 complain
, in_decl
);
12229 return finish_underlying_type (type
);
12232 case TYPE_ARGUMENT_PACK
:
12233 case NONTYPE_ARGUMENT_PACK
:
12235 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12237 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12241 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12243 /* For template nontype argument packs, also substitute into
12245 if (code
== NONTYPE_ARGUMENT_PACK
)
12246 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12264 /* We should use one of the expression tsubsts for these codes. */
12265 gcc_unreachable ();
12268 sorry ("use of %qs in template", get_tree_code_name (code
));
12269 return error_mark_node
;
12273 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12274 type of the expression on the left-hand side of the "." or "->"
12278 tsubst_baselink (tree baselink
, tree object_type
,
12279 tree args
, tsubst_flags_t complain
, tree in_decl
)
12282 tree qualifying_scope
;
12285 tree template_args
= 0;
12286 bool template_id_p
= false;
12287 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12289 /* A baselink indicates a function from a base class. Both the
12290 BASELINK_ACCESS_BINFO and the base class referenced may
12291 indicate bases of the template class, rather than the
12292 instantiated class. In addition, lookups that were not
12293 ambiguous before may be ambiguous now. Therefore, we perform
12294 the lookup again. */
12295 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12296 qualifying_scope
= tsubst (qualifying_scope
, args
,
12297 complain
, in_decl
);
12298 fns
= BASELINK_FUNCTIONS (baselink
);
12299 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12300 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12302 template_id_p
= true;
12303 template_args
= TREE_OPERAND (fns
, 1);
12304 fns
= TREE_OPERAND (fns
, 0);
12306 template_args
= tsubst_template_args (template_args
, args
,
12307 complain
, in_decl
);
12309 name
= DECL_NAME (get_first_fn (fns
));
12310 if (IDENTIFIER_TYPENAME_P (name
))
12311 name
= mangle_conv_op_name_for_type (optype
);
12312 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12314 return error_mark_node
;
12316 /* If lookup found a single function, mark it as used at this
12317 point. (If it lookup found multiple functions the one selected
12318 later by overload resolution will be marked as used at that
12320 if (BASELINK_P (baselink
))
12321 fns
= BASELINK_FUNCTIONS (baselink
);
12322 if (!template_id_p
&& !really_overloaded_fn (fns
))
12323 mark_used (OVL_CURRENT (fns
));
12325 /* Add back the template arguments, if present. */
12326 if (BASELINK_P (baselink
) && template_id_p
)
12327 BASELINK_FUNCTIONS (baselink
)
12328 = build_nt (TEMPLATE_ID_EXPR
,
12329 BASELINK_FUNCTIONS (baselink
),
12331 /* Update the conversion operator type. */
12332 BASELINK_OPTYPE (baselink
) = optype
;
12335 object_type
= current_class_type
;
12338 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12344 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12345 true if the qualified-id will be a postfix-expression in-and-of
12346 itself; false if more of the postfix-expression follows the
12347 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12351 tsubst_qualified_id (tree qualified_id
, tree args
,
12352 tsubst_flags_t complain
, tree in_decl
,
12353 bool done
, bool address_p
)
12359 tree template_args
;
12360 location_t loc
= UNKNOWN_LOCATION
;
12362 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12364 /* Figure out what name to look up. */
12365 name
= TREE_OPERAND (qualified_id
, 1);
12366 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12368 is_template
= true;
12369 loc
= EXPR_LOCATION (name
);
12370 template_args
= TREE_OPERAND (name
, 1);
12372 template_args
= tsubst_template_args (template_args
, args
,
12373 complain
, in_decl
);
12374 name
= TREE_OPERAND (name
, 0);
12378 is_template
= false;
12379 template_args
= NULL_TREE
;
12382 /* Substitute into the qualifying scope. When there are no ARGS, we
12383 are just trying to simplify a non-dependent expression. In that
12384 case the qualifying scope may be dependent, and, in any case,
12385 substituting will not help. */
12386 scope
= TREE_OPERAND (qualified_id
, 0);
12389 scope
= tsubst (scope
, args
, complain
, in_decl
);
12390 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12395 if (dependent_scope_p (scope
))
12398 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12399 return build_qualified_name (NULL_TREE
, scope
, expr
,
12400 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12403 if (!BASELINK_P (name
) && !DECL_P (expr
))
12405 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12407 /* A BIT_NOT_EXPR is used to represent a destructor. */
12408 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12410 error ("qualifying type %qT does not match destructor name ~%qT",
12411 scope
, TREE_OPERAND (expr
, 0));
12412 expr
= error_mark_node
;
12415 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12416 /*is_type_p=*/0, false);
12419 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12420 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12421 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12423 if (complain
& tf_error
)
12425 error ("dependent-name %qE is parsed as a non-type, but "
12426 "instantiation yields a type", qualified_id
);
12427 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12429 return error_mark_node
;
12435 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12437 /* Remember that there was a reference to this entity. */
12441 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12443 if (complain
& tf_error
)
12444 qualified_name_lookup_error (scope
,
12445 TREE_OPERAND (qualified_id
, 1),
12446 expr
, input_location
);
12447 return error_mark_node
;
12451 expr
= lookup_template_function (expr
, template_args
);
12453 if (expr
== error_mark_node
&& complain
& tf_error
)
12454 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12455 expr
, input_location
);
12456 else if (TYPE_P (scope
))
12458 expr
= (adjust_result_of_qualified_name_lookup
12459 (expr
, scope
, current_nonlambda_class_type ()));
12460 expr
= (finish_qualified_id_expr
12461 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12462 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12463 /*template_arg_p=*/false, complain
));
12466 /* Expressions do not generally have reference type. */
12467 if (TREE_CODE (expr
) != SCOPE_REF
12468 /* However, if we're about to form a pointer-to-member, we just
12469 want the referenced member referenced. */
12470 && TREE_CODE (expr
) != OFFSET_REF
)
12471 expr
= convert_from_reference (expr
);
12476 /* Like tsubst, but deals with expressions. This function just replaces
12477 template parms; to finish processing the resultant expression, use
12478 tsubst_copy_and_build or tsubst_expr. */
12481 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12483 enum tree_code code
;
12486 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12489 code
= TREE_CODE (t
);
12494 r
= retrieve_local_specialization (t
);
12496 if (r
== NULL_TREE
)
12498 /* We get here for a use of 'this' in an NSDMI. */
12499 if (DECL_NAME (t
) == this_identifier
12500 && at_function_scope_p ()
12501 && DECL_CONSTRUCTOR_P (current_function_decl
))
12502 return current_class_ptr
;
12504 /* This can happen for a parameter name used later in a function
12505 declaration (such as in a late-specified return type). Just
12506 make a dummy decl, since it's only used for its type. */
12507 gcc_assert (cp_unevaluated_operand
!= 0);
12508 r
= tsubst_decl (t
, args
, complain
);
12509 /* Give it the template pattern as its context; its true context
12510 hasn't been instantiated yet and this is good enough for
12512 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12515 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12516 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12525 if (DECL_TEMPLATE_PARM_P (t
))
12526 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12527 /* There is no need to substitute into namespace-scope
12529 if (DECL_NAMESPACE_SCOPE_P (t
))
12531 /* If ARGS is NULL, then T is known to be non-dependent. */
12532 if (args
== NULL_TREE
)
12533 return integral_constant_value (t
);
12535 /* Unfortunately, we cannot just call lookup_name here.
12538 template <int I> int f() {
12540 struct S { void g() { E e = a; } };
12543 When we instantiate f<7>::S::g(), say, lookup_name is not
12544 clever enough to find f<7>::a. */
12546 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12547 /*entering_scope=*/0);
12549 for (v
= TYPE_VALUES (enum_type
);
12551 v
= TREE_CHAIN (v
))
12552 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12553 return TREE_VALUE (v
);
12555 /* We didn't find the name. That should never happen; if
12556 name-lookup found it during preliminary parsing, we
12557 should find it again here during instantiation. */
12558 gcc_unreachable ();
12563 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12565 /* Check for a local specialization set up by
12566 tsubst_pack_expansion. */
12567 tree r
= retrieve_local_specialization (t
);
12570 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12571 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12575 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12576 tsubst_decl put in the hash table. */
12577 return retrieve_specialization (t
, args
, 0);
12580 if (DECL_CONTEXT (t
))
12584 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12585 /*entering_scope=*/1);
12586 if (ctx
!= DECL_CONTEXT (t
))
12588 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12591 if (complain
& tf_error
)
12592 error ("using invalid field %qD", t
);
12593 return error_mark_node
;
12602 case FUNCTION_DECL
:
12603 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12604 r
= tsubst (t
, args
, complain
, in_decl
);
12605 else if (local_variable_p (t
))
12607 r
= retrieve_local_specialization (t
);
12608 if (r
== NULL_TREE
)
12610 if (DECL_ANON_UNION_VAR_P (t
))
12612 /* Just use name lookup to find a member alias for an
12613 anonymous union, but then add it to the hash table. */
12614 r
= lookup_name (DECL_NAME (t
));
12615 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12616 register_local_specialization (r
, t
);
12620 /* This can happen for a variable used in a late-specified
12621 return type of a local lambda. Just make a dummy decl
12622 since it's only used for its type. */
12623 if (cp_unevaluated_operand
)
12624 return tsubst_decl (t
, args
, complain
);
12625 gcc_assert (errorcount
|| sorrycount
);
12626 return error_mark_node
;
12635 case NAMESPACE_DECL
:
12639 /* An OVERLOAD will always be a non-dependent overload set; an
12640 overload set from function scope will just be represented with an
12641 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12642 gcc_assert (!uses_template_parms (t
));
12646 return tsubst_baselink (t
, current_nonlambda_class_type (),
12647 args
, complain
, in_decl
);
12649 case TEMPLATE_DECL
:
12650 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12651 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12652 args
, complain
, in_decl
);
12653 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12654 return tsubst (t
, args
, complain
, in_decl
);
12655 else if (DECL_CLASS_SCOPE_P (t
)
12656 && uses_template_parms (DECL_CONTEXT (t
)))
12658 /* Template template argument like the following example need
12661 template <template <class> class TT> struct C {};
12662 template <class T> struct D {
12663 template <class U> struct E {};
12668 We are processing the template argument `E' in #1 for
12669 the template instantiation #2. Originally, `E' is a
12670 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12671 have to substitute this with one having context `D<int>'. */
12673 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12674 return lookup_field (context
, DECL_NAME(t
), 0, false);
12677 /* Ordinary template template argument. */
12681 case REINTERPRET_CAST_EXPR
:
12682 case CONST_CAST_EXPR
:
12683 case STATIC_CAST_EXPR
:
12684 case DYNAMIC_CAST_EXPR
:
12685 case IMPLICIT_CONV_EXPR
:
12689 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12690 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12693 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12696 tree expanded
, op
= TREE_OPERAND (t
, 0);
12699 if (SIZEOF_EXPR_TYPE_P (t
))
12700 op
= TREE_TYPE (op
);
12702 ++cp_unevaluated_operand
;
12703 ++c_inhibit_evaluation_warnings
;
12704 /* We only want to compute the number of arguments. */
12705 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12706 --cp_unevaluated_operand
;
12707 --c_inhibit_evaluation_warnings
;
12709 if (TREE_CODE (expanded
) == TREE_VEC
)
12710 len
= TREE_VEC_LENGTH (expanded
);
12712 if (expanded
== error_mark_node
)
12713 return error_mark_node
;
12714 else if (PACK_EXPANSION_P (expanded
)
12715 || (TREE_CODE (expanded
) == TREE_VEC
12717 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12719 if (TREE_CODE (expanded
) == TREE_VEC
)
12720 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12722 if (TYPE_P (expanded
))
12723 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12724 complain
& tf_error
);
12726 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12727 complain
& tf_error
);
12730 return build_int_cst (size_type_node
, len
);
12732 if (SIZEOF_EXPR_TYPE_P (t
))
12734 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12735 args
, complain
, in_decl
);
12736 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12737 r
= build1 (SIZEOF_EXPR
,
12738 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12739 SIZEOF_EXPR_TYPE_P (r
) = 1;
12746 case TRUTH_NOT_EXPR
:
12749 case UNARY_PLUS_EXPR
: /* Unary + */
12751 case AT_ENCODE_EXPR
:
12755 case REALPART_EXPR
:
12756 case IMAGPART_EXPR
:
12759 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12760 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12762 case COMPONENT_REF
:
12767 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12768 name
= TREE_OPERAND (t
, 1);
12769 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12771 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12772 complain
, in_decl
);
12773 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12775 else if (TREE_CODE (name
) == SCOPE_REF
12776 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12778 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12779 complain
, in_decl
);
12780 name
= TREE_OPERAND (name
, 1);
12781 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12782 complain
, in_decl
);
12783 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12784 name
= build_qualified_name (/*type=*/NULL_TREE
,
12786 /*template_p=*/false);
12788 else if (BASELINK_P (name
))
12789 name
= tsubst_baselink (name
,
12790 non_reference (TREE_TYPE (object
)),
12794 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12795 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12801 case TRUNC_DIV_EXPR
:
12802 case CEIL_DIV_EXPR
:
12803 case FLOOR_DIV_EXPR
:
12804 case ROUND_DIV_EXPR
:
12805 case EXACT_DIV_EXPR
:
12809 case TRUNC_MOD_EXPR
:
12810 case FLOOR_MOD_EXPR
:
12811 case TRUTH_ANDIF_EXPR
:
12812 case TRUTH_ORIF_EXPR
:
12813 case TRUTH_AND_EXPR
:
12814 case TRUTH_OR_EXPR
:
12827 case COMPOUND_EXPR
:
12830 case PREDECREMENT_EXPR
:
12831 case PREINCREMENT_EXPR
:
12832 case POSTDECREMENT_EXPR
:
12833 case POSTINCREMENT_EXPR
:
12835 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12836 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12839 return build_qualified_name (/*type=*/NULL_TREE
,
12840 tsubst_copy (TREE_OPERAND (t
, 0),
12841 args
, complain
, in_decl
),
12842 tsubst_copy (TREE_OPERAND (t
, 1),
12843 args
, complain
, in_decl
),
12844 QUALIFIED_NAME_IS_TEMPLATE (t
));
12849 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12850 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12851 NULL_TREE
, NULL_TREE
);
12855 int n
= VL_EXP_OPERAND_LENGTH (t
);
12856 tree result
= build_vl_exp (CALL_EXPR
, n
);
12858 for (i
= 0; i
< n
; i
++)
12859 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12860 complain
, in_decl
);
12866 case PSEUDO_DTOR_EXPR
:
12867 case VEC_PERM_EXPR
:
12870 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12871 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12872 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12873 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12880 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12881 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12882 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12883 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12890 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12891 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12892 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12893 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12897 case TEMPLATE_ID_EXPR
:
12899 /* Substituted template arguments */
12900 tree fn
= TREE_OPERAND (t
, 0);
12901 tree targs
= TREE_OPERAND (t
, 1);
12903 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12905 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12907 return lookup_template_function (fn
, targs
);
12912 tree purpose
, value
, chain
;
12914 if (t
== void_list_node
)
12917 purpose
= TREE_PURPOSE (t
);
12919 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12920 value
= TREE_VALUE (t
);
12922 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12923 chain
= TREE_CHAIN (t
);
12924 if (chain
&& chain
!= void_type_node
)
12925 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12926 if (purpose
== TREE_PURPOSE (t
)
12927 && value
== TREE_VALUE (t
)
12928 && chain
== TREE_CHAIN (t
))
12930 return tree_cons (purpose
, value
, chain
);
12935 case ENUMERAL_TYPE
:
12937 case TEMPLATE_TYPE_PARM
:
12938 case TEMPLATE_TEMPLATE_PARM
:
12939 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12940 case TEMPLATE_PARM_INDEX
:
12942 case REFERENCE_TYPE
:
12944 case FUNCTION_TYPE
:
12947 case TYPENAME_TYPE
:
12948 case UNBOUND_CLASS_TEMPLATE
:
12950 case DECLTYPE_TYPE
:
12952 return tsubst (t
, args
, complain
, in_decl
);
12956 /* Fall through. */
12957 case IDENTIFIER_NODE
:
12958 if (IDENTIFIER_TYPENAME_P (t
))
12960 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12961 return mangle_conv_op_name_for_type (new_type
);
12967 /* This is handled by tsubst_copy_and_build. */
12968 gcc_unreachable ();
12971 return build_x_va_arg (EXPR_LOCATION (t
),
12972 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12974 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12976 case CLEANUP_POINT_EXPR
:
12977 /* We shouldn't have built any of these during initial template
12978 generation. Instead, they should be built during instantiation
12979 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12980 gcc_unreachable ();
12984 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12985 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12986 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12987 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12988 mark_used (TREE_OPERAND (r
, 1));
12991 case EXPR_PACK_EXPANSION
:
12992 error ("invalid use of pack expansion expression");
12993 return error_mark_node
;
12995 case NONTYPE_ARGUMENT_PACK
:
12996 error ("use %<...%> to expand argument pack");
12997 return error_mark_node
;
13004 /* Instantiate any typedefs in the type. */
13005 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13006 r
= fold_convert (type
, t
);
13007 gcc_assert (TREE_CODE (r
) == code
);
13012 /* These can sometimes show up in a partial instantiation, but never
13013 involve template parms. */
13014 gcc_assert (!uses_template_parms (t
));
13018 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13019 gcc_checking_assert (false);
13024 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13027 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
13028 tree args
, tsubst_flags_t complain
, tree in_decl
)
13030 tree new_clauses
= NULL
, nc
, oc
;
13032 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
13034 nc
= copy_node (oc
);
13035 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
13038 switch (OMP_CLAUSE_CODE (nc
))
13040 case OMP_CLAUSE_LASTPRIVATE
:
13041 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
13043 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
13044 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
13045 in_decl
, /*integral_constant_expression_p=*/false);
13046 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
13047 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
13050 case OMP_CLAUSE_PRIVATE
:
13051 case OMP_CLAUSE_SHARED
:
13052 case OMP_CLAUSE_FIRSTPRIVATE
:
13053 case OMP_CLAUSE_COPYIN
:
13054 case OMP_CLAUSE_COPYPRIVATE
:
13055 case OMP_CLAUSE_IF
:
13056 case OMP_CLAUSE_NUM_THREADS
:
13057 case OMP_CLAUSE_SCHEDULE
:
13058 case OMP_CLAUSE_COLLAPSE
:
13059 case OMP_CLAUSE_FINAL
:
13060 case OMP_CLAUSE_DEPEND
:
13061 case OMP_CLAUSE_FROM
:
13062 case OMP_CLAUSE_TO
:
13063 case OMP_CLAUSE_UNIFORM
:
13064 case OMP_CLAUSE_MAP
:
13065 case OMP_CLAUSE_DEVICE
:
13066 case OMP_CLAUSE_DIST_SCHEDULE
:
13067 case OMP_CLAUSE_NUM_TEAMS
:
13068 case OMP_CLAUSE_THREAD_LIMIT
:
13069 case OMP_CLAUSE_SAFELEN
:
13070 case OMP_CLAUSE_SIMDLEN
:
13071 OMP_CLAUSE_OPERAND (nc
, 0)
13072 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13073 in_decl
, /*integral_constant_expression_p=*/false);
13075 case OMP_CLAUSE_REDUCTION
:
13076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
13078 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
13079 if (TREE_CODE (placeholder
) == SCOPE_REF
)
13081 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
13082 complain
, in_decl
);
13083 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
13084 = build_qualified_name (NULL_TREE
, scope
,
13085 TREE_OPERAND (placeholder
, 1),
13089 gcc_assert (identifier_p (placeholder
));
13091 OMP_CLAUSE_OPERAND (nc
, 0)
13092 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13093 in_decl
, /*integral_constant_expression_p=*/false);
13095 case OMP_CLAUSE_LINEAR
:
13096 case OMP_CLAUSE_ALIGNED
:
13097 OMP_CLAUSE_OPERAND (nc
, 0)
13098 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13099 in_decl
, /*integral_constant_expression_p=*/false);
13100 OMP_CLAUSE_OPERAND (nc
, 1)
13101 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
13102 in_decl
, /*integral_constant_expression_p=*/false);
13105 case OMP_CLAUSE_NOWAIT
:
13106 case OMP_CLAUSE_ORDERED
:
13107 case OMP_CLAUSE_DEFAULT
:
13108 case OMP_CLAUSE_UNTIED
:
13109 case OMP_CLAUSE_MERGEABLE
:
13110 case OMP_CLAUSE_INBRANCH
:
13111 case OMP_CLAUSE_NOTINBRANCH
:
13112 case OMP_CLAUSE_PROC_BIND
:
13113 case OMP_CLAUSE_FOR
:
13114 case OMP_CLAUSE_PARALLEL
:
13115 case OMP_CLAUSE_SECTIONS
:
13116 case OMP_CLAUSE_TASKGROUP
:
13119 gcc_unreachable ();
13123 new_clauses
= nreverse (new_clauses
);
13125 new_clauses
= finish_omp_clauses (new_clauses
);
13126 return new_clauses
;
13129 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13132 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
13135 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13137 tree purpose
, value
, chain
;
13142 if (TREE_CODE (t
) != TREE_LIST
)
13143 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13144 /*function_p=*/false,
13145 /*integral_constant_expression_p=*/false);
13147 if (t
== void_list_node
)
13150 purpose
= TREE_PURPOSE (t
);
13152 purpose
= RECUR (purpose
);
13153 value
= TREE_VALUE (t
);
13156 if (TREE_CODE (value
) != LABEL_DECL
)
13157 value
= RECUR (value
);
13160 value
= lookup_label (DECL_NAME (value
));
13161 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13162 TREE_USED (value
) = 1;
13165 chain
= TREE_CHAIN (t
);
13166 if (chain
&& chain
!= void_type_node
)
13167 chain
= RECUR (chain
);
13168 return tree_cons (purpose
, value
, chain
);
13172 /* Substitute one OMP_FOR iterator. */
13175 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13176 tree condv
, tree incrv
, tree
*clauses
,
13177 tree args
, tsubst_flags_t complain
, tree in_decl
,
13178 bool integral_constant_expression_p
)
13180 #define RECUR(NODE) \
13181 tsubst_expr ((NODE), args, complain, in_decl, \
13182 integral_constant_expression_p)
13183 tree decl
, init
, cond
, incr
;
13185 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13186 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13187 decl
= TREE_OPERAND (init
, 0);
13188 init
= TREE_OPERAND (init
, 1);
13189 tree decl_expr
= NULL_TREE
;
13190 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
13192 /* We need to jump through some hoops to handle declarations in the
13193 for-init-statement, since we might need to handle auto deduction,
13194 but we need to keep control of initialization. */
13196 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
13197 decl
= tsubst_decl (decl
, args
, complain
);
13200 decl
= RECUR (decl
);
13201 init
= RECUR (init
);
13203 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13204 if (auto_node
&& init
)
13206 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
13208 gcc_assert (!type_dependent_expression_p (decl
));
13210 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13214 /* Declare the variable, but don't let that initialize it. */
13215 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13216 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
13218 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
13221 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13222 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13223 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13224 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13225 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13226 RECUR (TREE_OPERAND (incr
, 1)),
13229 incr
= RECUR (incr
);
13230 TREE_VEC_ELT (declv
, i
) = decl
;
13231 TREE_VEC_ELT (initv
, i
) = init
;
13232 TREE_VEC_ELT (condv
, i
) = cond
;
13233 TREE_VEC_ELT (incrv
, i
) = incr
;
13239 /* Declare and initialize the variable. */
13246 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13248 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13249 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13250 && OMP_CLAUSE_DECL (c
) == decl
)
13252 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13253 && OMP_CLAUSE_DECL (c
) == decl
)
13254 error ("iteration variable %qD should not be firstprivate", decl
);
13255 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13256 && OMP_CLAUSE_DECL (c
) == decl
)
13257 error ("iteration variable %qD should not be reduction", decl
);
13261 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13262 OMP_CLAUSE_DECL (c
) = decl
;
13263 c
= finish_omp_clauses (c
);
13266 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13271 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13272 if (COMPARISON_CLASS_P (cond
))
13273 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13274 RECUR (TREE_OPERAND (cond
, 0)),
13275 RECUR (TREE_OPERAND (cond
, 1)));
13277 cond
= RECUR (cond
);
13278 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13279 switch (TREE_CODE (incr
))
13281 case PREINCREMENT_EXPR
:
13282 case PREDECREMENT_EXPR
:
13283 case POSTINCREMENT_EXPR
:
13284 case POSTDECREMENT_EXPR
:
13285 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13286 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13289 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13290 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13292 tree rhs
= TREE_OPERAND (incr
, 1);
13293 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13294 RECUR (TREE_OPERAND (incr
, 0)),
13295 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13296 RECUR (TREE_OPERAND (rhs
, 0)),
13297 RECUR (TREE_OPERAND (rhs
, 1))));
13300 incr
= RECUR (incr
);
13303 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13304 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13306 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13307 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13308 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13309 TREE_TYPE (decl
), lhs
,
13310 RECUR (TREE_OPERAND (incr
, 2))));
13312 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13313 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13314 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13316 tree rhs
= TREE_OPERAND (incr
, 2);
13317 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13318 RECUR (TREE_OPERAND (incr
, 0)),
13319 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13320 RECUR (TREE_OPERAND (rhs
, 0)),
13321 RECUR (TREE_OPERAND (rhs
, 1))));
13324 incr
= RECUR (incr
);
13327 incr
= RECUR (incr
);
13331 TREE_VEC_ELT (declv
, i
) = decl
;
13332 TREE_VEC_ELT (initv
, i
) = init
;
13333 TREE_VEC_ELT (condv
, i
) = cond
;
13334 TREE_VEC_ELT (incrv
, i
) = incr
;
13338 /* Like tsubst_copy for expressions, etc. but also does semantic
13342 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13343 bool integral_constant_expression_p
)
13345 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13346 #define RECUR(NODE) \
13347 tsubst_expr ((NODE), args, complain, in_decl, \
13348 integral_constant_expression_p)
13354 if (t
== NULL_TREE
|| t
== error_mark_node
)
13357 loc
= input_location
;
13358 if (EXPR_HAS_LOCATION (t
))
13359 input_location
= EXPR_LOCATION (t
);
13360 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13361 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13363 switch (TREE_CODE (t
))
13365 case STATEMENT_LIST
:
13367 tree_stmt_iterator i
;
13368 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13369 RECUR (tsi_stmt (i
));
13373 case CTOR_INITIALIZER
:
13374 finish_mem_initializers (tsubst_initializer_list
13375 (TREE_OPERAND (t
, 0), args
));
13379 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13383 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13384 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13385 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13387 finish_expr_stmt (tmp
);
13391 do_using_directive (USING_STMT_NAMESPACE (t
));
13396 tree decl
, pattern_decl
;
13399 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13400 if (TREE_CODE (decl
) == LABEL_DECL
)
13401 finish_label_decl (DECL_NAME (decl
));
13402 else if (TREE_CODE (decl
) == USING_DECL
)
13404 tree scope
= USING_DECL_SCOPE (decl
);
13405 tree name
= DECL_NAME (decl
);
13408 scope
= tsubst (scope
, args
, complain
, in_decl
);
13409 decl
= lookup_qualified_name (scope
, name
,
13410 /*is_type_p=*/false,
13411 /*complain=*/false);
13412 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13413 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13415 do_local_using_decl (decl
, scope
, name
);
13417 else if (DECL_PACK_P (decl
))
13419 /* Don't build up decls for a variadic capture proxy, we'll
13420 instantiate the elements directly as needed. */
13425 init
= DECL_INITIAL (decl
);
13426 decl
= tsubst (decl
, args
, complain
, in_decl
);
13427 if (decl
!= error_mark_node
)
13429 /* By marking the declaration as instantiated, we avoid
13430 trying to instantiate it. Since instantiate_decl can't
13431 handle local variables, and since we've already done
13432 all that needs to be done, that's the right thing to
13435 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13437 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13438 /* Anonymous aggregates are a special case. */
13439 finish_anon_union (decl
);
13440 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13442 DECL_CONTEXT (decl
) = current_function_decl
;
13443 if (DECL_NAME (decl
) == this_identifier
)
13445 tree lam
= DECL_CONTEXT (current_function_decl
);
13446 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13447 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13449 insert_capture_proxy (decl
);
13451 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13452 /* We already did a pushtag. */;
13453 else if (TREE_CODE (decl
) == FUNCTION_DECL
13454 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13455 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13457 DECL_CONTEXT (decl
) = NULL_TREE
;
13459 DECL_CONTEXT (decl
) = current_function_decl
;
13460 cp_check_omp_declare_reduction (decl
);
13464 int const_init
= false;
13465 maybe_push_decl (decl
);
13467 && DECL_PRETTY_FUNCTION_P (decl
))
13469 /* For __PRETTY_FUNCTION__ we have to adjust the
13471 const char *const name
13472 = cxx_printable_name (current_function_decl
, 2);
13473 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13477 tree t
= RECUR (init
);
13481 /* If we had an initializer but it
13482 instantiated to nothing,
13483 value-initialize the object. This will
13484 only occur when the initializer was a
13485 pack expansion where the parameter packs
13486 used in that expansion were of length
13488 init
= build_value_init (TREE_TYPE (decl
),
13490 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13491 init
= get_target_expr_sfinae (init
, complain
);
13498 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13500 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13509 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13510 RECUR (FOR_INIT_STMT (t
));
13511 finish_for_init_stmt (stmt
);
13512 tmp
= RECUR (FOR_COND (t
));
13513 finish_for_cond (tmp
, stmt
, false);
13514 tmp
= RECUR (FOR_EXPR (t
));
13515 finish_for_expr (tmp
, stmt
);
13516 RECUR (FOR_BODY (t
));
13517 finish_for_stmt (stmt
);
13520 case RANGE_FOR_STMT
:
13523 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13524 decl
= RANGE_FOR_DECL (t
);
13525 decl
= tsubst (decl
, args
, complain
, in_decl
);
13526 maybe_push_decl (decl
);
13527 expr
= RECUR (RANGE_FOR_EXPR (t
));
13528 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13529 RECUR (RANGE_FOR_BODY (t
));
13530 finish_for_stmt (stmt
);
13535 stmt
= begin_while_stmt ();
13536 tmp
= RECUR (WHILE_COND (t
));
13537 finish_while_stmt_cond (tmp
, stmt
, false);
13538 RECUR (WHILE_BODY (t
));
13539 finish_while_stmt (stmt
);
13543 stmt
= begin_do_stmt ();
13544 RECUR (DO_BODY (t
));
13545 finish_do_body (stmt
);
13546 tmp
= RECUR (DO_COND (t
));
13547 finish_do_stmt (tmp
, stmt
, false);
13551 stmt
= begin_if_stmt ();
13552 tmp
= RECUR (IF_COND (t
));
13553 finish_if_stmt_cond (tmp
, stmt
);
13554 RECUR (THEN_CLAUSE (t
));
13555 finish_then_clause (stmt
);
13557 if (ELSE_CLAUSE (t
))
13559 begin_else_clause (stmt
);
13560 RECUR (ELSE_CLAUSE (t
));
13561 finish_else_clause (stmt
);
13564 finish_if_stmt (stmt
);
13568 if (BIND_EXPR_BODY_BLOCK (t
))
13569 stmt
= begin_function_body ();
13571 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13572 ? BCS_TRY_BLOCK
: 0);
13574 RECUR (BIND_EXPR_BODY (t
));
13576 if (BIND_EXPR_BODY_BLOCK (t
))
13577 finish_function_body (stmt
);
13579 finish_compound_stmt (stmt
);
13583 finish_break_stmt ();
13586 case CONTINUE_STMT
:
13587 finish_continue_stmt ();
13591 stmt
= begin_switch_stmt ();
13592 tmp
= RECUR (SWITCH_STMT_COND (t
));
13593 finish_switch_cond (tmp
, stmt
);
13594 RECUR (SWITCH_STMT_BODY (t
));
13595 finish_switch_stmt (stmt
);
13598 case CASE_LABEL_EXPR
:
13599 finish_case_label (EXPR_LOCATION (t
),
13600 RECUR (CASE_LOW (t
)),
13601 RECUR (CASE_HIGH (t
)));
13606 tree decl
= LABEL_EXPR_LABEL (t
);
13609 label
= finish_label_stmt (DECL_NAME (decl
));
13610 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13611 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13616 tmp
= GOTO_DESTINATION (t
);
13617 if (TREE_CODE (tmp
) != LABEL_DECL
)
13618 /* Computed goto's must be tsubst'd into. On the other hand,
13619 non-computed gotos must not be; the identifier in question
13620 will have no binding. */
13623 tmp
= DECL_NAME (tmp
);
13624 finish_goto_stmt (tmp
);
13628 tmp
= finish_asm_stmt
13629 (ASM_VOLATILE_P (t
),
13630 RECUR (ASM_STRING (t
)),
13631 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13632 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13633 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13634 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13636 tree asm_expr
= tmp
;
13637 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13638 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13639 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13646 stmt
= begin_try_block ();
13647 RECUR (TRY_STMTS (t
));
13648 finish_cleanup_try_block (stmt
);
13649 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13653 tree compound_stmt
= NULL_TREE
;
13655 if (FN_TRY_BLOCK_P (t
))
13656 stmt
= begin_function_try_block (&compound_stmt
);
13658 stmt
= begin_try_block ();
13660 RECUR (TRY_STMTS (t
));
13662 if (FN_TRY_BLOCK_P (t
))
13663 finish_function_try_block (stmt
);
13665 finish_try_block (stmt
);
13667 RECUR (TRY_HANDLERS (t
));
13668 if (FN_TRY_BLOCK_P (t
))
13669 finish_function_handler_sequence (stmt
, compound_stmt
);
13671 finish_handler_sequence (stmt
);
13677 tree decl
= HANDLER_PARMS (t
);
13681 decl
= tsubst (decl
, args
, complain
, in_decl
);
13682 /* Prevent instantiate_decl from trying to instantiate
13683 this variable. We've already done all that needs to be
13685 if (decl
!= error_mark_node
)
13686 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13688 stmt
= begin_handler ();
13689 finish_handler_parms (decl
, stmt
);
13690 RECUR (HANDLER_BODY (t
));
13691 finish_handler (stmt
);
13696 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13697 if (CLASS_TYPE_P (tmp
))
13699 /* Local classes are not independent templates; they are
13700 instantiated along with their containing function. And this
13701 way we don't have to deal with pushing out of one local class
13702 to instantiate a member of another local class. */
13704 /* Closures are handled by the LAMBDA_EXPR. */
13705 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13706 complete_type (tmp
);
13707 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13708 if (!DECL_ARTIFICIAL (fn
))
13709 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13713 case STATIC_ASSERT
:
13717 ++c_inhibit_evaluation_warnings
;
13719 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13722 /*integral_constant_expression_p=*/true);
13723 --c_inhibit_evaluation_warnings
;
13725 finish_static_assert (condition
,
13726 STATIC_ASSERT_MESSAGE (t
),
13727 STATIC_ASSERT_SOURCE_LOCATION (t
),
13728 /*member_p=*/false);
13733 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13734 args
, complain
, in_decl
);
13735 stmt
= begin_omp_parallel ();
13736 RECUR (OMP_PARALLEL_BODY (t
));
13737 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13738 = OMP_PARALLEL_COMBINED (t
);
13742 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13743 args
, complain
, in_decl
);
13744 stmt
= begin_omp_task ();
13745 RECUR (OMP_TASK_BODY (t
));
13746 finish_omp_task (tmp
, stmt
);
13752 case OMP_DISTRIBUTE
:
13754 tree clauses
, body
, pre_body
;
13755 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13756 tree incrv
= NULL_TREE
;
13759 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13760 args
, complain
, in_decl
);
13761 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13763 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13764 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13765 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13766 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13769 stmt
= begin_omp_structured_block ();
13771 pre_body
= push_stmt_list ();
13772 RECUR (OMP_FOR_PRE_BODY (t
));
13773 pre_body
= pop_stmt_list (pre_body
);
13775 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13776 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13777 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13778 &clauses
, args
, complain
, in_decl
,
13779 integral_constant_expression_p
);
13781 body
= push_stmt_list ();
13782 RECUR (OMP_FOR_BODY (t
));
13783 body
= pop_stmt_list (body
);
13785 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13786 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13787 condv
, incrv
, body
, pre_body
, clauses
);
13790 t
= make_node (TREE_CODE (t
));
13791 TREE_TYPE (t
) = void_type_node
;
13792 OMP_FOR_BODY (t
) = body
;
13793 OMP_FOR_PRE_BODY (t
) = pre_body
;
13794 OMP_FOR_CLAUSES (t
) = clauses
;
13795 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13799 add_stmt (finish_omp_structured_block (stmt
));
13806 case OMP_TARGET_DATA
:
13808 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13809 args
, complain
, in_decl
);
13810 stmt
= push_stmt_list ();
13811 RECUR (OMP_BODY (t
));
13812 stmt
= pop_stmt_list (stmt
);
13815 OMP_BODY (t
) = stmt
;
13816 OMP_CLAUSES (t
) = tmp
;
13820 case OMP_TARGET_UPDATE
:
13821 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13822 args
, complain
, in_decl
);
13824 OMP_CLAUSES (t
) = tmp
;
13831 case OMP_TASKGROUP
:
13833 stmt
= push_stmt_list ();
13834 RECUR (OMP_BODY (t
));
13835 stmt
= pop_stmt_list (stmt
);
13838 OMP_BODY (t
) = stmt
;
13843 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13844 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13846 tree op1
= TREE_OPERAND (t
, 1);
13847 tree rhs1
= NULL_TREE
;
13849 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13851 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13852 op1
= TREE_OPERAND (op1
, 1);
13854 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13855 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13856 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13857 NULL_TREE
, NULL_TREE
, rhs1
,
13858 OMP_ATOMIC_SEQ_CST (t
));
13862 tree op1
= TREE_OPERAND (t
, 1);
13863 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13864 tree rhs1
= NULL_TREE
;
13865 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13866 enum tree_code opcode
= NOP_EXPR
;
13867 if (code
== OMP_ATOMIC_READ
)
13869 v
= RECUR (TREE_OPERAND (op1
, 0));
13870 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13872 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13873 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13875 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13876 v
= RECUR (TREE_OPERAND (op1
, 0));
13877 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13878 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13880 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13881 op11
= TREE_OPERAND (op11
, 1);
13883 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13884 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13885 opcode
= TREE_CODE (op11
);
13886 if (opcode
== MODIFY_EXPR
)
13892 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13893 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13895 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13896 OMP_ATOMIC_SEQ_CST (t
));
13900 case TRANSACTION_EXPR
:
13903 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13904 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13906 if (TRANSACTION_EXPR_IS_STMT (t
))
13908 tree body
= TRANSACTION_EXPR_BODY (t
);
13909 tree noex
= NULL_TREE
;
13910 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13912 noex
= MUST_NOT_THROW_COND (body
);
13913 if (noex
== NULL_TREE
)
13914 noex
= boolean_true_node
;
13915 body
= TREE_OPERAND (body
, 0);
13917 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13919 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13923 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13924 RECUR (TRANSACTION_EXPR_BODY (t
)),
13931 case MUST_NOT_THROW_EXPR
:
13932 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13933 RECUR (MUST_NOT_THROW_COND (t
))));
13935 case EXPR_PACK_EXPANSION
:
13936 error ("invalid use of pack expansion expression");
13937 RETURN (error_mark_node
);
13939 case NONTYPE_ARGUMENT_PACK
:
13940 error ("use %<...%> to expand argument pack");
13941 RETURN (error_mark_node
);
13943 case CILK_SPAWN_STMT
:
13944 cfun
->calls_cilk_spawn
= 1;
13945 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
13947 case CILK_SYNC_STMT
:
13948 RETURN (build_cilk_sync ());
13950 case COMPOUND_EXPR
:
13951 tmp
= RECUR (TREE_OPERAND (t
, 0));
13952 if (tmp
== NULL_TREE
)
13953 /* If the first operand was a statement, we're done with it. */
13954 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13955 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13956 RECUR (TREE_OPERAND (t
, 1)),
13959 case ANNOTATE_EXPR
:
13960 tmp
= RECUR (TREE_OPERAND (t
, 0));
13961 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
13962 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
13965 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13967 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13968 /*function_p=*/false,
13969 integral_constant_expression_p
));
13972 RETURN (NULL_TREE
);
13974 input_location
= loc
;
13980 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13981 function. For description of the body see comment above
13982 cp_parser_omp_declare_reduction_exprs. */
13985 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13987 if (t
== NULL_TREE
|| t
== error_mark_node
)
13990 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13992 tree_stmt_iterator tsi
;
13995 memset (stmts
, 0, sizeof stmts
);
13996 for (i
= 0, tsi
= tsi_start (t
);
13997 i
< 7 && !tsi_end_p (tsi
);
13998 i
++, tsi_next (&tsi
))
13999 stmts
[i
] = tsi_stmt (tsi
);
14000 gcc_assert (tsi_end_p (tsi
));
14004 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
14005 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
14006 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
14007 args
, complain
, in_decl
);
14008 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
14009 args
, complain
, in_decl
);
14010 DECL_CONTEXT (omp_out
) = current_function_decl
;
14011 DECL_CONTEXT (omp_in
) = current_function_decl
;
14012 keep_next_level (true);
14013 tree block
= begin_omp_structured_block ();
14014 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
14015 block
= finish_omp_structured_block (block
);
14016 block
= maybe_cleanup_point_expr_void (block
);
14017 add_decl_expr (omp_out
);
14018 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
14019 TREE_NO_WARNING (omp_out
) = 1;
14020 add_decl_expr (omp_in
);
14021 finish_expr_stmt (block
);
14025 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
14026 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
14027 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
14028 args
, complain
, in_decl
);
14029 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
14030 args
, complain
, in_decl
);
14031 DECL_CONTEXT (omp_priv
) = current_function_decl
;
14032 DECL_CONTEXT (omp_orig
) = current_function_decl
;
14033 keep_next_level (true);
14034 tree block
= begin_omp_structured_block ();
14035 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
14036 block
= finish_omp_structured_block (block
);
14037 block
= maybe_cleanup_point_expr_void (block
);
14038 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
14039 add_decl_expr (omp_priv
);
14040 add_decl_expr (omp_orig
);
14041 finish_expr_stmt (block
);
14043 add_decl_expr (omp_orig
);
14047 /* T is a postfix-expression that is not being used in a function
14048 call. Return the substituted version of T. */
14051 tsubst_non_call_postfix_expression (tree t
, tree args
,
14052 tsubst_flags_t complain
,
14055 if (TREE_CODE (t
) == SCOPE_REF
)
14056 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
14057 /*done=*/false, /*address_p=*/false);
14059 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14060 /*function_p=*/false,
14061 /*integral_constant_expression_p=*/false);
14066 /* Like tsubst but deals with expressions and performs semantic
14067 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14070 tsubst_copy_and_build (tree t
,
14072 tsubst_flags_t complain
,
14075 bool integral_constant_expression_p
)
14077 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14078 #define RECUR(NODE) \
14079 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14080 /*function_p=*/false, \
14081 integral_constant_expression_p)
14086 if (t
== NULL_TREE
|| t
== error_mark_node
)
14089 loc
= input_location
;
14090 if (EXPR_HAS_LOCATION (t
))
14091 input_location
= EXPR_LOCATION (t
);
14093 /* N3276 decltype magic only applies to calls at the top level or on the
14094 right side of a comma. */
14095 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
14096 complain
&= ~tf_decltype
;
14098 switch (TREE_CODE (t
))
14102 /* Fall through. */
14103 case IDENTIFIER_NODE
:
14107 bool non_integral_constant_expression_p
;
14108 const char *error_msg
;
14110 if (IDENTIFIER_TYPENAME_P (t
))
14112 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14113 t
= mangle_conv_op_name_for_type (new_type
);
14116 /* Look up the name. */
14117 decl
= lookup_name (t
);
14119 /* By convention, expressions use ERROR_MARK_NODE to indicate
14120 failure, not NULL_TREE. */
14121 if (decl
== NULL_TREE
)
14122 decl
= error_mark_node
;
14124 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
14126 integral_constant_expression_p
,
14127 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
14128 &non_integral_constant_expression_p
,
14129 /*template_p=*/false,
14131 /*address_p=*/false,
14132 /*template_arg_p=*/false,
14137 if (!function_p
&& identifier_p (decl
))
14139 if (complain
& tf_error
)
14140 unqualified_name_lookup_error (decl
);
14141 decl
= error_mark_node
;
14146 case TEMPLATE_ID_EXPR
:
14149 tree templ
= RECUR (TREE_OPERAND (t
, 0));
14150 tree targs
= TREE_OPERAND (t
, 1);
14153 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14155 if (TREE_CODE (templ
) == COMPONENT_REF
)
14157 object
= TREE_OPERAND (templ
, 0);
14158 templ
= TREE_OPERAND (templ
, 1);
14161 object
= NULL_TREE
;
14162 templ
= lookup_template_function (templ
, targs
);
14165 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
14166 object
, templ
, NULL_TREE
));
14168 RETURN (baselink_for_fns (templ
));
14173 tree r
= RECUR (TREE_OPERAND (t
, 0));
14175 if (REFERENCE_REF_P (t
))
14177 /* A type conversion to reference type will be enclosed in
14178 such an indirect ref, but the substitution of the cast
14179 will have also added such an indirect ref. */
14180 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14181 r
= convert_from_reference (r
);
14184 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14185 complain
|decltype_flag
);
14191 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14192 RECUR (TREE_OPERAND (t
, 0))));
14194 case IMPLICIT_CONV_EXPR
:
14196 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14197 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14198 int flags
= LOOKUP_IMPLICIT
;
14199 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14200 flags
= LOOKUP_NORMAL
;
14201 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14208 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14209 RECUR (TREE_OPERAND (t
, 0))));
14212 case REINTERPRET_CAST_EXPR
:
14213 case CONST_CAST_EXPR
:
14214 case DYNAMIC_CAST_EXPR
:
14215 case STATIC_CAST_EXPR
:
14218 tree op
, r
= NULL_TREE
;
14220 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14221 if (integral_constant_expression_p
14222 && !cast_valid_in_integral_constant_expression_p (type
))
14224 if (complain
& tf_error
)
14225 error ("a cast to a type other than an integral or "
14226 "enumeration type cannot appear in a constant-expression");
14227 RETURN (error_mark_node
);
14230 op
= RECUR (TREE_OPERAND (t
, 0));
14232 ++c_inhibit_evaluation_warnings
;
14233 switch (TREE_CODE (t
))
14236 r
= build_functional_cast (type
, op
, complain
);
14238 case REINTERPRET_CAST_EXPR
:
14239 r
= build_reinterpret_cast (type
, op
, complain
);
14241 case CONST_CAST_EXPR
:
14242 r
= build_const_cast (type
, op
, complain
);
14244 case DYNAMIC_CAST_EXPR
:
14245 r
= build_dynamic_cast (type
, op
, complain
);
14247 case STATIC_CAST_EXPR
:
14248 r
= build_static_cast (type
, op
, complain
);
14251 gcc_unreachable ();
14253 --c_inhibit_evaluation_warnings
;
14258 case POSTDECREMENT_EXPR
:
14259 case POSTINCREMENT_EXPR
:
14260 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14261 args
, complain
, in_decl
);
14262 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14263 complain
|decltype_flag
));
14265 case PREDECREMENT_EXPR
:
14266 case PREINCREMENT_EXPR
:
14270 case TRUTH_NOT_EXPR
:
14271 case UNARY_PLUS_EXPR
: /* Unary + */
14272 case REALPART_EXPR
:
14273 case IMAGPART_EXPR
:
14274 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14275 RECUR (TREE_OPERAND (t
, 0)),
14276 complain
|decltype_flag
));
14278 case FIX_TRUNC_EXPR
:
14279 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14283 op1
= TREE_OPERAND (t
, 0);
14284 if (TREE_CODE (op1
) == LABEL_DECL
)
14285 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14286 EXPR_LOCATION (op1
)));
14287 if (TREE_CODE (op1
) == SCOPE_REF
)
14288 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14289 /*done=*/true, /*address_p=*/true);
14291 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14293 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14294 complain
|decltype_flag
));
14299 case TRUNC_DIV_EXPR
:
14300 case CEIL_DIV_EXPR
:
14301 case FLOOR_DIV_EXPR
:
14302 case ROUND_DIV_EXPR
:
14303 case EXACT_DIV_EXPR
:
14307 case TRUNC_MOD_EXPR
:
14308 case FLOOR_MOD_EXPR
:
14309 case TRUTH_ANDIF_EXPR
:
14310 case TRUTH_ORIF_EXPR
:
14311 case TRUTH_AND_EXPR
:
14312 case TRUTH_OR_EXPR
:
14330 ++c_inhibit_evaluation_warnings
;
14332 r
= build_x_binary_op
14333 (input_location
, TREE_CODE (t
),
14334 RECUR (TREE_OPERAND (t
, 0)),
14335 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14337 : TREE_CODE (TREE_OPERAND (t
, 0))),
14338 RECUR (TREE_OPERAND (t
, 1)),
14339 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14341 : TREE_CODE (TREE_OPERAND (t
, 1))),
14343 complain
|decltype_flag
);
14344 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14345 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14347 --c_inhibit_evaluation_warnings
;
14352 case POINTER_PLUS_EXPR
:
14353 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t
, 0)),
14354 RECUR (TREE_OPERAND (t
, 1)));
14357 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14358 /*address_p=*/false));
14360 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14361 args
, complain
, in_decl
);
14362 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14363 RECUR (TREE_OPERAND (t
, 1)),
14364 complain
|decltype_flag
));
14366 case ARRAY_NOTATION_REF
:
14368 tree start_index
, length
, stride
;
14369 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14370 args
, complain
, in_decl
);
14371 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14372 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14373 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14374 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14375 length
, stride
, TREE_TYPE (op1
)));
14378 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14379 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14386 op1
= TREE_OPERAND (t
, 0);
14387 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14388 op1
= TREE_TYPE (op1
);
14391 /* When there are no ARGS, we are trying to evaluate a
14392 non-dependent expression from the parser. Trying to do
14393 the substitutions may not work. */
14395 op1
= TREE_TYPE (op1
);
14399 ++cp_unevaluated_operand
;
14400 ++c_inhibit_evaluation_warnings
;
14402 op1
= tsubst (op1
, args
, complain
, in_decl
);
14404 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14405 /*function_p=*/false,
14406 /*integral_constant_expression_p=*/
14408 --cp_unevaluated_operand
;
14409 --c_inhibit_evaluation_warnings
;
14412 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14413 complain
& tf_error
);
14415 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14416 complain
& tf_error
);
14417 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14419 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14421 if (!processing_template_decl
&& TYPE_P (op1
))
14423 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14424 build1 (NOP_EXPR
, op1
, error_mark_node
));
14425 SIZEOF_EXPR_TYPE_P (r
) = 1;
14428 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14429 TREE_SIDE_EFFECTS (r
) = 0;
14430 TREE_READONLY (r
) = 1;
14432 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14437 case AT_ENCODE_EXPR
:
14439 op1
= TREE_OPERAND (t
, 0);
14440 ++cp_unevaluated_operand
;
14441 ++c_inhibit_evaluation_warnings
;
14442 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14443 /*function_p=*/false,
14444 /*integral_constant_expression_p=*/false);
14445 --cp_unevaluated_operand
;
14446 --c_inhibit_evaluation_warnings
;
14447 RETURN (objc_build_encode_expr (op1
));
14450 case NOEXCEPT_EXPR
:
14451 op1
= TREE_OPERAND (t
, 0);
14452 ++cp_unevaluated_operand
;
14453 ++c_inhibit_evaluation_warnings
;
14454 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14455 /*function_p=*/false,
14456 /*integral_constant_expression_p=*/false);
14457 --cp_unevaluated_operand
;
14458 --c_inhibit_evaluation_warnings
;
14459 RETURN (finish_noexcept_expr (op1
, complain
));
14465 ++c_inhibit_evaluation_warnings
;
14467 r
= build_x_modify_expr
14468 (EXPR_LOCATION (t
),
14469 RECUR (TREE_OPERAND (t
, 0)),
14470 TREE_CODE (TREE_OPERAND (t
, 1)),
14471 RECUR (TREE_OPERAND (t
, 2)),
14472 complain
|decltype_flag
);
14473 /* TREE_NO_WARNING must be set if either the expression was
14474 parenthesized or it uses an operator such as >>= rather
14475 than plain assignment. In the former case, it was already
14476 set and must be copied. In the latter case,
14477 build_x_modify_expr sets it and it must not be reset
14479 if (TREE_NO_WARNING (t
))
14480 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14482 --c_inhibit_evaluation_warnings
;
14488 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14489 args
, complain
, in_decl
);
14490 /* Remember that there was a reference to this entity. */
14493 RETURN (build_x_arrow (input_location
, op1
, complain
));
14497 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14498 tree init
= RECUR (TREE_OPERAND (t
, 3));
14499 vec
<tree
, va_gc
> *placement_vec
;
14500 vec
<tree
, va_gc
> *init_vec
;
14503 if (placement
== NULL_TREE
)
14504 placement_vec
= NULL
;
14507 placement_vec
= make_tree_vector ();
14508 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14509 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14512 /* If there was an initializer in the original tree, but it
14513 instantiated to an empty list, then we should pass a
14514 non-NULL empty vector to tell build_new that it was an
14515 empty initializer() rather than no initializer. This can
14516 only happen when the initializer is a pack expansion whose
14517 parameter packs are of length zero. */
14518 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14522 init_vec
= make_tree_vector ();
14523 if (init
== void_zero_node
)
14524 gcc_assert (init_vec
!= NULL
);
14527 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14528 vec_safe_push (init_vec
, TREE_VALUE (init
));
14532 ret
= build_new (&placement_vec
,
14533 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14534 RECUR (TREE_OPERAND (t
, 2)),
14536 NEW_EXPR_USE_GLOBAL (t
),
14539 if (placement_vec
!= NULL
)
14540 release_tree_vector (placement_vec
);
14541 if (init_vec
!= NULL
)
14542 release_tree_vector (init_vec
);
14548 RETURN (delete_sanity
14549 (RECUR (TREE_OPERAND (t
, 0)),
14550 RECUR (TREE_OPERAND (t
, 1)),
14551 DELETE_EXPR_USE_VEC (t
),
14552 DELETE_EXPR_USE_GLOBAL (t
),
14555 case COMPOUND_EXPR
:
14557 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14558 complain
& ~tf_decltype
, in_decl
,
14559 /*function_p=*/false,
14560 integral_constant_expression_p
);
14561 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14563 RECUR (TREE_OPERAND (t
, 1)),
14564 complain
|decltype_flag
));
14570 vec
<tree
, va_gc
> *call_args
;
14571 unsigned int nargs
, i
;
14576 function
= CALL_EXPR_FN (t
);
14577 /* When we parsed the expression, we determined whether or
14578 not Koenig lookup should be performed. */
14579 koenig_p
= KOENIG_LOOKUP_P (t
);
14580 if (TREE_CODE (function
) == SCOPE_REF
)
14582 qualified_p
= true;
14583 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14585 /*address_p=*/false);
14587 else if (koenig_p
&& identifier_p (function
))
14589 /* Do nothing; calling tsubst_copy_and_build on an identifier
14590 would incorrectly perform unqualified lookup again.
14592 Note that we can also have an IDENTIFIER_NODE if the earlier
14593 unqualified lookup found a member function; in that case
14594 koenig_p will be false and we do want to do the lookup
14595 again to find the instantiated member function.
14597 FIXME but doing that causes c++/15272, so we need to stop
14598 using IDENTIFIER_NODE in that situation. */
14599 qualified_p
= false;
14603 if (TREE_CODE (function
) == COMPONENT_REF
)
14605 tree op
= TREE_OPERAND (function
, 1);
14607 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14608 || (BASELINK_P (op
)
14609 && BASELINK_QUALIFIED_P (op
)));
14612 qualified_p
= false;
14614 if (TREE_CODE (function
) == ADDR_EXPR
14615 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14616 /* Avoid error about taking the address of a constructor. */
14617 function
= TREE_OPERAND (function
, 0);
14619 function
= tsubst_copy_and_build (function
, args
, complain
,
14622 integral_constant_expression_p
);
14624 if (BASELINK_P (function
))
14625 qualified_p
= true;
14628 nargs
= call_expr_nargs (t
);
14629 call_args
= make_tree_vector ();
14630 for (i
= 0; i
< nargs
; ++i
)
14632 tree arg
= CALL_EXPR_ARG (t
, i
);
14634 if (!PACK_EXPANSION_P (arg
))
14635 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14638 /* Expand the pack expansion and push each entry onto
14640 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14641 if (TREE_CODE (arg
) == TREE_VEC
)
14643 unsigned int len
, j
;
14645 len
= TREE_VEC_LENGTH (arg
);
14646 for (j
= 0; j
< len
; ++j
)
14648 tree value
= TREE_VEC_ELT (arg
, j
);
14649 if (value
!= NULL_TREE
)
14650 value
= convert_from_reference (value
);
14651 vec_safe_push (call_args
, value
);
14656 /* A partial substitution. Add one entry. */
14657 vec_safe_push (call_args
, arg
);
14662 /* We do not perform argument-dependent lookup if normal
14663 lookup finds a non-function, in accordance with the
14664 expected resolution of DR 218. */
14666 && ((is_overloaded_fn (function
)
14667 /* If lookup found a member function, the Koenig lookup is
14668 not appropriate, even if an unqualified-name was used
14669 to denote the function. */
14670 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14671 || identifier_p (function
))
14672 /* Only do this when substitution turns a dependent call
14673 into a non-dependent call. */
14674 && type_dependent_expression_p_push (t
)
14675 && !any_type_dependent_arguments_p (call_args
))
14676 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
14678 if (identifier_p (function
)
14679 && !any_type_dependent_arguments_p (call_args
))
14681 if (koenig_p
&& (complain
& tf_warning_or_error
))
14683 /* For backwards compatibility and good diagnostics, try
14684 the unqualified lookup again if we aren't in SFINAE
14686 tree unq
= (tsubst_copy_and_build
14687 (function
, args
, complain
, in_decl
, true,
14688 integral_constant_expression_p
));
14689 if (unq
== error_mark_node
)
14690 RETURN (error_mark_node
);
14692 if (unq
!= function
)
14695 if (INDIRECT_REF_P (fn
))
14696 fn
= TREE_OPERAND (fn
, 0);
14697 if (TREE_CODE (fn
) == COMPONENT_REF
)
14698 fn
= TREE_OPERAND (fn
, 1);
14699 if (is_overloaded_fn (fn
))
14700 fn
= get_first_fn (fn
);
14701 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
14702 "%qD was not declared in this scope, "
14703 "and no declarations were found by "
14704 "argument-dependent lookup at the point "
14705 "of instantiation", function
))
14708 /* Can't say anything more. */;
14709 else if (DECL_CLASS_SCOPE_P (fn
))
14711 location_t loc
= EXPR_LOC_OR_LOC (t
,
14714 "declarations in dependent base %qT are "
14715 "not found by unqualified lookup",
14716 DECL_CLASS_CONTEXT (fn
));
14717 if (current_class_ptr
)
14719 "use %<this->%D%> instead", function
);
14722 "use %<%T::%D%> instead",
14723 current_class_name
, function
);
14726 inform (0, "%q+D declared here, later in the "
14727 "translation unit", fn
);
14732 if (identifier_p (function
))
14734 if (complain
& tf_error
)
14735 unqualified_name_lookup_error (function
);
14736 release_tree_vector (call_args
);
14737 RETURN (error_mark_node
);
14741 /* Remember that there was a reference to this entity. */
14742 if (DECL_P (function
))
14743 mark_used (function
);
14745 /* Put back tf_decltype for the actual call. */
14746 complain
|= decltype_flag
;
14748 if (TREE_CODE (function
) == OFFSET_REF
)
14749 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14751 else if (TREE_CODE (function
) == COMPONENT_REF
)
14753 tree instance
= TREE_OPERAND (function
, 0);
14754 tree fn
= TREE_OPERAND (function
, 1);
14756 if (processing_template_decl
14757 && (type_dependent_expression_p (instance
)
14758 || (!BASELINK_P (fn
)
14759 && TREE_CODE (fn
) != FIELD_DECL
)
14760 || type_dependent_expression_p (fn
)
14761 || any_type_dependent_arguments_p (call_args
)))
14762 ret
= build_nt_call_vec (function
, call_args
);
14763 else if (!BASELINK_P (fn
))
14764 ret
= finish_call_expr (function
, &call_args
,
14765 /*disallow_virtual=*/false,
14766 /*koenig_p=*/false,
14769 ret
= (build_new_method_call
14771 &call_args
, NULL_TREE
,
14772 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14777 ret
= finish_call_expr (function
, &call_args
,
14778 /*disallow_virtual=*/qualified_p
,
14782 release_tree_vector (call_args
);
14789 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14792 if (TREE_CODE (cond
) == INTEGER_CST
)
14794 if (integer_zerop (cond
))
14796 ++c_inhibit_evaluation_warnings
;
14797 exp1
= RECUR (TREE_OPERAND (t
, 1));
14798 --c_inhibit_evaluation_warnings
;
14799 exp2
= RECUR (TREE_OPERAND (t
, 2));
14803 exp1
= RECUR (TREE_OPERAND (t
, 1));
14804 ++c_inhibit_evaluation_warnings
;
14805 exp2
= RECUR (TREE_OPERAND (t
, 2));
14806 --c_inhibit_evaluation_warnings
;
14811 exp1
= RECUR (TREE_OPERAND (t
, 1));
14812 exp2
= RECUR (TREE_OPERAND (t
, 2));
14815 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14816 cond
, exp1
, exp2
, complain
));
14819 case PSEUDO_DTOR_EXPR
:
14820 RETURN (finish_pseudo_destructor_expr
14821 (RECUR (TREE_OPERAND (t
, 0)),
14822 RECUR (TREE_OPERAND (t
, 1)),
14823 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14828 tree purpose
, value
, chain
;
14830 if (t
== void_list_node
)
14833 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14834 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14836 /* We have pack expansions, so expand those and
14837 create a new list out of it. */
14838 tree purposevec
= NULL_TREE
;
14839 tree valuevec
= NULL_TREE
;
14843 /* Expand the argument expressions. */
14844 if (TREE_PURPOSE (t
))
14845 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14846 complain
, in_decl
);
14847 if (TREE_VALUE (t
))
14848 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14849 complain
, in_decl
);
14851 /* Build the rest of the list. */
14852 chain
= TREE_CHAIN (t
);
14853 if (chain
&& chain
!= void_type_node
)
14854 chain
= RECUR (chain
);
14856 /* Determine the number of arguments. */
14857 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14859 len
= TREE_VEC_LENGTH (purposevec
);
14860 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14862 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14863 len
= TREE_VEC_LENGTH (valuevec
);
14866 /* Since we only performed a partial substitution into
14867 the argument pack, we only RETURN (a single list
14869 if (purposevec
== TREE_PURPOSE (t
)
14870 && valuevec
== TREE_VALUE (t
)
14871 && chain
== TREE_CHAIN (t
))
14874 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14877 /* Convert the argument vectors into a TREE_LIST */
14881 /* Grab the Ith values. */
14883 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14886 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14889 /* Build the list (backwards). */
14890 chain
= tree_cons (purpose
, value
, chain
);
14896 purpose
= TREE_PURPOSE (t
);
14898 purpose
= RECUR (purpose
);
14899 value
= TREE_VALUE (t
);
14901 value
= RECUR (value
);
14902 chain
= TREE_CHAIN (t
);
14903 if (chain
&& chain
!= void_type_node
)
14904 chain
= RECUR (chain
);
14905 if (purpose
== TREE_PURPOSE (t
)
14906 && value
== TREE_VALUE (t
)
14907 && chain
== TREE_CHAIN (t
))
14909 RETURN (tree_cons (purpose
, value
, chain
));
14912 case COMPONENT_REF
:
14918 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14919 args
, complain
, in_decl
);
14920 /* Remember that there was a reference to this entity. */
14921 if (DECL_P (object
))
14922 mark_used (object
);
14923 object_type
= TREE_TYPE (object
);
14925 member
= TREE_OPERAND (t
, 1);
14926 if (BASELINK_P (member
))
14927 member
= tsubst_baselink (member
,
14928 non_reference (TREE_TYPE (object
)),
14929 args
, complain
, in_decl
);
14931 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14932 if (member
== error_mark_node
)
14933 RETURN (error_mark_node
);
14935 if (type_dependent_expression_p (object
))
14936 /* We can't do much here. */;
14937 else if (!CLASS_TYPE_P (object_type
))
14939 if (scalarish_type_p (object_type
))
14941 tree s
= NULL_TREE
;
14942 tree dtor
= member
;
14944 if (TREE_CODE (dtor
) == SCOPE_REF
)
14946 s
= TREE_OPERAND (dtor
, 0);
14947 dtor
= TREE_OPERAND (dtor
, 1);
14949 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14951 dtor
= TREE_OPERAND (dtor
, 0);
14953 RETURN (finish_pseudo_destructor_expr
14954 (object
, s
, dtor
, input_location
));
14958 else if (TREE_CODE (member
) == SCOPE_REF
14959 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14961 /* Lookup the template functions now that we know what the
14963 tree scope
= TREE_OPERAND (member
, 0);
14964 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14965 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14966 member
= lookup_qualified_name (scope
, tmpl
,
14967 /*is_type_p=*/false,
14968 /*complain=*/false);
14969 if (BASELINK_P (member
))
14971 BASELINK_FUNCTIONS (member
)
14972 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14974 member
= (adjust_result_of_qualified_name_lookup
14975 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14980 qualified_name_lookup_error (scope
, tmpl
, member
,
14982 RETURN (error_mark_node
);
14985 else if (TREE_CODE (member
) == SCOPE_REF
14986 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14987 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14989 if (complain
& tf_error
)
14991 if (TYPE_P (TREE_OPERAND (member
, 0)))
14992 error ("%qT is not a class or namespace",
14993 TREE_OPERAND (member
, 0));
14995 error ("%qD is not a class or namespace",
14996 TREE_OPERAND (member
, 0));
14998 RETURN (error_mark_node
);
15000 else if (TREE_CODE (member
) == FIELD_DECL
)
15001 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
15003 RETURN (finish_class_member_access_expr (object
, member
,
15004 /*template_p=*/false,
15009 RETURN (build_throw
15010 (RECUR (TREE_OPERAND (t
, 0))));
15014 vec
<constructor_elt
, va_gc
> *n
;
15015 constructor_elt
*ce
;
15016 unsigned HOST_WIDE_INT idx
;
15017 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15018 bool process_index_p
;
15020 bool need_copy_p
= false;
15023 if (type
== error_mark_node
)
15024 RETURN (error_mark_node
);
15026 /* digest_init will do the wrong thing if we let it. */
15027 if (type
&& TYPE_PTRMEMFUNC_P (type
))
15030 /* We do not want to process the index of aggregate
15031 initializers as they are identifier nodes which will be
15032 looked up by digest_init. */
15033 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
15035 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
15036 newlen
= vec_safe_length (n
);
15037 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
15039 if (ce
->index
&& process_index_p
15040 /* An identifier index is looked up in the type
15041 being initialized, not the current scope. */
15042 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
15043 ce
->index
= RECUR (ce
->index
);
15045 if (PACK_EXPANSION_P (ce
->value
))
15047 /* Substitute into the pack expansion. */
15048 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
15051 if (ce
->value
== error_mark_node
15052 || PACK_EXPANSION_P (ce
->value
))
15054 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
15055 /* Just move the argument into place. */
15056 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
15059 /* Update the length of the final CONSTRUCTOR
15060 arguments vector, and note that we will need to
15062 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
15063 need_copy_p
= true;
15067 ce
->value
= RECUR (ce
->value
);
15072 vec
<constructor_elt
, va_gc
> *old_n
= n
;
15074 vec_alloc (n
, newlen
);
15075 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
15077 if (TREE_CODE (ce
->value
) == TREE_VEC
)
15079 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
15080 for (i
= 0; i
< len
; ++i
)
15081 CONSTRUCTOR_APPEND_ELT (n
, 0,
15082 TREE_VEC_ELT (ce
->value
, i
));
15085 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
15089 r
= build_constructor (init_list_type_node
, n
);
15090 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
15092 if (TREE_HAS_CONSTRUCTOR (t
))
15093 RETURN (finish_compound_literal (type
, r
, complain
));
15095 TREE_TYPE (r
) = type
;
15101 tree operand_0
= TREE_OPERAND (t
, 0);
15102 if (TYPE_P (operand_0
))
15104 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
15105 RETURN (get_typeid (operand_0
, complain
));
15109 operand_0
= RECUR (operand_0
);
15110 RETURN (build_typeid (operand_0
, complain
));
15117 else if (DECL_PACK_P (t
))
15119 /* We don't build decls for an instantiation of a
15120 variadic capture proxy, we instantiate the elements
15122 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
15123 return RECUR (DECL_VALUE_EXPR (t
));
15129 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
15131 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
15132 /* If the original type was a reference, we'll be wrapped in
15133 the appropriate INDIRECT_REF. */
15134 r
= convert_from_reference (r
);
15139 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
15140 RECUR (TREE_OPERAND (t
, 0)),
15141 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
15143 case OFFSETOF_EXPR
:
15144 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
15148 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
15149 complain
, in_decl
);
15151 tree type2
= TRAIT_EXPR_TYPE2 (t
);
15153 type2
= tsubst (type2
, args
, complain
, in_decl
);
15155 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
15160 tree old_stmt_expr
= cur_stmt_expr
;
15161 tree stmt_expr
= begin_stmt_expr ();
15163 cur_stmt_expr
= stmt_expr
;
15164 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
15165 integral_constant_expression_p
);
15166 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
15167 cur_stmt_expr
= old_stmt_expr
;
15169 /* If the resulting list of expression statement is empty,
15170 fold it further into void_zero_node. */
15171 if (empty_expr_stmt_p (stmt_expr
))
15172 stmt_expr
= void_zero_node
;
15174 RETURN (stmt_expr
);
15179 tree r
= build_lambda_expr ();
15181 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15182 LAMBDA_EXPR_CLOSURE (r
) = type
;
15183 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15185 LAMBDA_EXPR_LOCATION (r
)
15186 = LAMBDA_EXPR_LOCATION (t
);
15187 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15188 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15189 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15190 LAMBDA_EXPR_DISCRIMINATOR (r
)
15191 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15192 /* For a function scope, we want to use tsubst so that we don't
15193 complain about referring to an auto function before its return
15194 type has been deduced. Otherwise, we want to use tsubst_copy so
15195 that we look up the existing field/parameter/variable rather
15196 than build a new one. */
15197 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15198 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15199 scope
= tsubst (scope
, args
, complain
, in_decl
);
15200 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15202 /* Look up the parameter we want directly, as tsubst_copy
15203 doesn't do what we need. */
15204 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15205 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15206 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15207 parm
= DECL_CHAIN (parm
);
15209 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15210 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15211 DECL_CONTEXT (scope
) = fn
;
15214 scope
= RECUR (scope
);
15215 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15216 LAMBDA_EXPR_RETURN_TYPE (r
)
15217 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15219 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15220 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15222 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15223 determine_visibility (TYPE_NAME (type
));
15224 /* Now that we know visibility, instantiate the type so we have a
15225 declaration of the op() for later calls to lambda_function. */
15226 complete_type (type
);
15228 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15230 RETURN (build_lambda_object (r
));
15234 /* We can get here for a constant initializer of non-dependent type.
15235 FIXME stop folding in cp_parser_initializer_clause. */
15237 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15242 case TRANSACTION_EXPR
:
15243 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15244 integral_constant_expression_p
));
15247 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15249 case VEC_PERM_EXPR
:
15250 RETURN (build_x_vec_perm_expr (input_location
,
15251 RECUR (TREE_OPERAND (t
, 0)),
15252 RECUR (TREE_OPERAND (t
, 1)),
15253 RECUR (TREE_OPERAND (t
, 2)),
15257 /* Handle Objective-C++ constructs, if appropriate. */
15260 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15261 in_decl
, /*function_p=*/false);
15265 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15271 input_location
= loc
;
15275 /* Verify that the instantiated ARGS are valid. For type arguments,
15276 make sure that the type's linkage is ok. For non-type arguments,
15277 make sure they are constants if they are integral or enumerations.
15278 Emit an error under control of COMPLAIN, and return TRUE on error. */
15281 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15283 if (dependent_template_arg_p (t
))
15285 if (ARGUMENT_PACK_P (t
))
15287 tree vec
= ARGUMENT_PACK_ARGS (t
);
15288 int len
= TREE_VEC_LENGTH (vec
);
15289 bool result
= false;
15292 for (i
= 0; i
< len
; ++i
)
15293 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15297 else if (TYPE_P (t
))
15299 /* [basic.link]: A name with no linkage (notably, the name
15300 of a class or enumeration declared in a local scope)
15301 shall not be used to declare an entity with linkage.
15302 This implies that names with no linkage cannot be used as
15305 DR 757 relaxes this restriction for C++0x. */
15306 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15307 : no_linkage_check (t
, /*relaxed_p=*/false));
15311 /* DR 488 makes use of a type with no linkage cause
15312 type deduction to fail. */
15313 if (complain
& tf_error
)
15315 if (TYPE_ANONYMOUS_P (nt
))
15316 error ("%qT is/uses anonymous type", t
);
15318 error ("template argument for %qD uses local type %qT",
15323 /* In order to avoid all sorts of complications, we do not
15324 allow variably-modified types as template arguments. */
15325 else if (variably_modified_type_p (t
, NULL_TREE
))
15327 if (complain
& tf_error
)
15328 error ("%qT is a variably modified type", t
);
15332 /* Class template and alias template arguments should be OK. */
15333 else if (DECL_TYPE_TEMPLATE_P (t
))
15335 /* A non-type argument of integral or enumerated type must be a
15337 else if (TREE_TYPE (t
)
15338 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15339 && !TREE_CONSTANT (t
))
15341 if (complain
& tf_error
)
15342 error ("integral expression %qE is not constant", t
);
15349 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15351 int ix
, len
= DECL_NTPARMS (tmpl
);
15352 bool result
= false;
15354 for (ix
= 0; ix
!= len
; ix
++)
15356 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15359 if (result
&& (complain
& tf_error
))
15360 error (" trying to instantiate %qD", tmpl
);
15364 /* We're out of SFINAE context now, so generate diagnostics for the access
15365 errors we saw earlier when instantiating D from TMPL and ARGS. */
15368 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15370 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15371 tree type
= TREE_TYPE (pattern
);
15372 location_t loc
= input_location
;
15374 push_access_scope (d
);
15375 push_deferring_access_checks (dk_no_deferred
);
15376 input_location
= DECL_SOURCE_LOCATION (pattern
);
15377 tsubst (type
, args
, tf_warning_or_error
, d
);
15378 input_location
= loc
;
15379 pop_deferring_access_checks ();
15380 pop_access_scope (d
);
15383 /* Instantiate the indicated variable, function, or alias template TMPL with
15384 the template arguments in TARG_PTR. */
15387 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15389 tree targ_ptr
= orig_args
;
15393 bool access_ok
= true;
15395 if (tmpl
== error_mark_node
)
15396 return error_mark_node
;
15398 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15400 /* If this function is a clone, handle it specially. */
15401 if (DECL_CLONED_FUNCTION_P (tmpl
))
15406 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15407 DECL_CLONED_FUNCTION. */
15408 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15409 targ_ptr
, complain
);
15410 if (spec
== error_mark_node
)
15411 return error_mark_node
;
15413 /* Look for the clone. */
15414 FOR_EACH_CLONE (clone
, spec
)
15415 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15417 /* We should always have found the clone by now. */
15418 gcc_unreachable ();
15422 if (targ_ptr
== error_mark_node
)
15423 return error_mark_node
;
15425 /* Check to see if we already have this specialization. */
15426 gen_tmpl
= most_general_template (tmpl
);
15427 if (tmpl
!= gen_tmpl
)
15428 /* The TMPL is a partial instantiation. To get a full set of
15429 arguments we must add the arguments used to perform the
15430 partial instantiation. */
15431 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15434 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15435 but it doesn't seem to be on the hot path. */
15436 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15438 gcc_assert (tmpl
== gen_tmpl
15439 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15441 || fndecl
== NULL_TREE
);
15443 if (spec
!= NULL_TREE
)
15445 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15447 if (complain
& tf_error
)
15448 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15449 return error_mark_node
;
15454 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15456 return error_mark_node
;
15458 /* We are building a FUNCTION_DECL, during which the access of its
15459 parameters and return types have to be checked. However this
15460 FUNCTION_DECL which is the desired context for access checking
15461 is not built yet. We solve this chicken-and-egg problem by
15462 deferring all checks until we have the FUNCTION_DECL. */
15463 push_deferring_access_checks (dk_deferred
);
15465 /* Instantiation of the function happens in the context of the function
15466 template, not the context of the overload resolution we're doing. */
15467 push_to_top_level ();
15468 /* If there are dependent arguments, e.g. because we're doing partial
15469 ordering, make sure processing_template_decl stays set. */
15470 if (uses_template_parms (targ_ptr
))
15471 ++processing_template_decl
;
15472 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15474 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15475 complain
, gen_tmpl
);
15476 push_nested_class (ctx
);
15478 /* Substitute template parameters to obtain the specialization. */
15479 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15480 targ_ptr
, complain
, gen_tmpl
);
15481 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15482 pop_nested_class ();
15483 pop_from_top_level ();
15485 if (fndecl
== error_mark_node
)
15487 pop_deferring_access_checks ();
15488 return error_mark_node
;
15491 /* The DECL_TI_TEMPLATE should always be the immediate parent
15492 template, not the most general template. */
15493 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15495 /* Now we know the specialization, compute access previously
15497 push_access_scope (fndecl
);
15498 if (!perform_deferred_access_checks (complain
))
15500 pop_access_scope (fndecl
);
15501 pop_deferring_access_checks ();
15503 /* If we've just instantiated the main entry point for a function,
15504 instantiate all the alternate entry points as well. We do this
15505 by cloning the instantiation of the main entry point, not by
15506 instantiating the template clones. */
15507 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15508 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15512 if (!(complain
& tf_error
))
15514 /* Remember to reinstantiate when we're out of SFINAE so the user
15515 can see the errors. */
15516 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15518 return error_mark_node
;
15523 /* Wrapper for instantiate_template_1. */
15526 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15529 timevar_push (TV_TEMPLATE_INST
);
15530 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15531 timevar_pop (TV_TEMPLATE_INST
);
15535 /* Instantiate the alias template TMPL with ARGS. Also push a template
15536 instantiation level, which instantiate_template doesn't do because
15537 functions and variables have sufficient context established by the
15541 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15543 struct pending_template
*old_last_pend
= last_pending_template
;
15544 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15545 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15546 return error_mark_node
;
15547 tree tinst
= build_tree_list (tmpl
, args
);
15548 if (!push_tinst_level (tinst
))
15551 return error_mark_node
;
15555 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15556 args
, tmpl
, complain
,
15557 /*require_all_args=*/true,
15558 /*use_default_args=*/true);
15560 tree r
= instantiate_template (tmpl
, args
, complain
);
15561 pop_tinst_level ();
15562 /* We can't free this if a pending_template entry or last_error_tinst_level
15563 is pointing at it. */
15564 if (last_pending_template
== old_last_pend
15565 && last_error_tinst_level
== old_error_tinst
)
15571 /* PARM is a template parameter pack for FN. Returns true iff
15572 PARM is used in a deducible way in the argument list of FN. */
15575 pack_deducible_p (tree parm
, tree fn
)
15577 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15578 for (; t
; t
= TREE_CHAIN (t
))
15580 tree type
= TREE_VALUE (t
);
15582 if (!PACK_EXPANSION_P (type
))
15584 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15585 packs
; packs
= TREE_CHAIN (packs
))
15586 if (TREE_VALUE (packs
) == parm
)
15588 /* The template parameter pack is used in a function parameter
15589 pack. If this is the end of the parameter list, the
15590 template parameter pack is deducible. */
15591 if (TREE_CHAIN (t
) == void_list_node
)
15594 /* Otherwise, not. Well, it could be deduced from
15595 a non-pack parameter, but doing so would end up with
15596 a deduction mismatch, so don't bother. */
15600 /* The template parameter pack isn't used in any function parameter
15601 packs, but it might be used deeper, e.g. tuple<Args...>. */
15605 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15606 NARGS elements of the arguments that are being used when calling
15607 it. TARGS is a vector into which the deduced template arguments
15610 Returns either a FUNCTION_DECL for the matching specialization of FN or
15611 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15612 true, diagnostics will be printed to explain why it failed.
15614 If FN is a conversion operator, or we are trying to produce a specific
15615 specialization, RETURN_TYPE is the return type desired.
15617 The EXPLICIT_TARGS are explicit template arguments provided via a
15620 The parameter STRICT is one of:
15623 We are deducing arguments for a function call, as in
15624 [temp.deduct.call].
15627 We are deducing arguments for a conversion function, as in
15628 [temp.deduct.conv].
15631 We are deducing arguments when doing an explicit instantiation
15632 as in [temp.explicit], when determining an explicit specialization
15633 as in [temp.expl.spec], or when taking the address of a function
15634 template, as in [temp.deduct.funcaddr]. */
15637 fn_type_unification (tree fn
,
15638 tree explicit_targs
,
15641 unsigned int nargs
,
15643 unification_kind_t strict
,
15650 tree decl
= NULL_TREE
;
15651 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15653 static int deduction_depth
;
15654 struct pending_template
*old_last_pend
= last_pending_template
;
15655 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15656 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15658 tree r
= error_mark_node
;
15661 complain
|= tf_decltype
;
15663 /* In C++0x, it's possible to have a function template whose type depends
15664 on itself recursively. This is most obvious with decltype, but can also
15665 occur with enumeration scope (c++/48969). So we need to catch infinite
15666 recursion and reject the substitution at deduction time; this function
15667 will return error_mark_node for any repeated substitution.
15669 This also catches excessive recursion such as when f<N> depends on
15670 f<N-1> across all integers, and returns error_mark_node for all the
15671 substitutions back up to the initial one.
15673 This is, of course, not reentrant. */
15674 if (excessive_deduction_depth
)
15675 return error_mark_node
;
15676 tinst
= build_tree_list (fn
, NULL_TREE
);
15679 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15681 fntype
= TREE_TYPE (fn
);
15682 if (explicit_targs
)
15686 The specified template arguments must match the template
15687 parameters in kind (i.e., type, nontype, template), and there
15688 must not be more arguments than there are parameters;
15689 otherwise type deduction fails.
15691 Nontype arguments must match the types of the corresponding
15692 nontype template parameters, or must be convertible to the
15693 types of the corresponding nontype parameters as specified in
15694 _temp.arg.nontype_, otherwise type deduction fails.
15696 All references in the function type of the function template
15697 to the corresponding template parameters are replaced by the
15698 specified template argument values. If a substitution in a
15699 template parameter or in the function type of the function
15700 template results in an invalid type, type deduction fails. */
15701 int i
, len
= TREE_VEC_LENGTH (tparms
);
15702 location_t loc
= input_location
;
15703 bool incomplete
= false;
15705 /* Adjust any explicit template arguments before entering the
15706 substitution context. */
15708 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15710 /*require_all_args=*/false,
15711 /*use_default_args=*/false));
15712 if (explicit_targs
== error_mark_node
)
15715 /* Substitute the explicit args into the function type. This is
15716 necessary so that, for instance, explicitly declared function
15717 arguments can match null pointed constants. If we were given
15718 an incomplete set of explicit args, we must not do semantic
15719 processing during substitution as we could create partial
15721 for (i
= 0; i
< len
; i
++)
15723 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15724 bool parameter_pack
= false;
15725 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15727 /* Dig out the actual parm. */
15728 if (TREE_CODE (parm
) == TYPE_DECL
15729 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15731 parm
= TREE_TYPE (parm
);
15732 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15734 else if (TREE_CODE (parm
) == PARM_DECL
)
15736 parm
= DECL_INITIAL (parm
);
15737 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15740 if (!parameter_pack
&& targ
== NULL_TREE
)
15741 /* No explicit argument for this template parameter. */
15744 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15746 /* Mark the argument pack as "incomplete". We could
15747 still deduce more arguments during unification.
15748 We remove this mark in type_unification_real. */
15751 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15752 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15753 = ARGUMENT_PACK_ARGS (targ
);
15756 /* We have some incomplete argument packs. */
15761 TREE_VALUE (tinst
) = explicit_targs
;
15762 if (!push_tinst_level (tinst
))
15764 excessive_deduction_depth
= true;
15767 processing_template_decl
+= incomplete
;
15768 input_location
= DECL_SOURCE_LOCATION (fn
);
15769 /* Ignore any access checks; we'll see them again in
15770 instantiate_template and they might have the wrong
15771 access path at this point. */
15772 push_deferring_access_checks (dk_deferred
);
15773 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15774 complain
| tf_partial
, NULL_TREE
);
15775 pop_deferring_access_checks ();
15776 input_location
= loc
;
15777 processing_template_decl
-= incomplete
;
15778 pop_tinst_level ();
15780 if (fntype
== error_mark_node
)
15783 /* Place the explicitly specified arguments in TARGS. */
15784 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15785 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15788 /* Never do unification on the 'this' parameter. */
15789 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15795 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15796 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15797 new_args
[0] = return_type
;
15798 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15803 /* We allow incomplete unification without an error message here
15804 because the standard doesn't seem to explicitly prohibit it. Our
15805 callers must be ready to deal with unification failures in any
15808 TREE_VALUE (tinst
) = targs
;
15809 /* If we aren't explaining yet, push tinst context so we can see where
15810 any errors (e.g. from class instantiations triggered by instantiation
15811 of default template arguments) come from. If we are explaining, this
15812 context is redundant. */
15813 if (!explain_p
&& !push_tinst_level (tinst
))
15815 excessive_deduction_depth
= true;
15819 /* type_unification_real will pass back any access checks from default
15820 template argument substitution. */
15821 vec
<deferred_access_check
, va_gc
> *checks
;
15824 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15825 targs
, parms
, args
, nargs
, /*subr=*/0,
15826 strict
, flags
, &checks
, explain_p
);
15828 pop_tinst_level ();
15832 /* Now that we have bindings for all of the template arguments,
15833 ensure that the arguments deduced for the template template
15834 parameters have compatible template parameter lists. We cannot
15835 check this property before we have deduced all template
15836 arguments, because the template parameter types of a template
15837 template parameter might depend on prior template parameters
15838 deduced after the template template parameter. The following
15839 ill-formed example illustrates this issue:
15841 template<typename T, template<T> class C> void f(C<5>, T);
15843 template<int N> struct X {};
15846 f(X<5>(), 5l); // error: template argument deduction fails
15849 The template parameter list of 'C' depends on the template type
15850 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15851 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15852 time that we deduce 'C'. */
15853 if (!template_template_parm_bindings_ok_p
15854 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15856 unify_inconsistent_template_template_parameters (explain_p
);
15860 /* All is well so far. Now, check:
15864 When all template arguments have been deduced, all uses of
15865 template parameters in nondeduced contexts are replaced with
15866 the corresponding deduced argument values. If the
15867 substitution results in an invalid type, as described above,
15868 type deduction fails. */
15869 TREE_VALUE (tinst
) = targs
;
15870 if (!push_tinst_level (tinst
))
15872 excessive_deduction_depth
= true;
15876 /* Also collect access checks from the instantiation. */
15877 reopen_deferring_access_checks (checks
);
15879 decl
= instantiate_template (fn
, targs
, complain
);
15881 checks
= get_deferred_access_checks ();
15882 pop_deferring_access_checks ();
15884 pop_tinst_level ();
15886 if (decl
== error_mark_node
)
15889 /* Now perform any access checks encountered during substitution. */
15890 push_access_scope (decl
);
15891 ok
= perform_access_checks (checks
, complain
);
15892 pop_access_scope (decl
);
15896 /* If we're looking for an exact match, check that what we got
15897 is indeed an exact match. It might not be if some template
15898 parameters are used in non-deduced contexts. But don't check
15899 for an exact match if we have dependent template arguments;
15900 in that case we're doing partial ordering, and we already know
15901 that we have two candidates that will provide the actual type. */
15902 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
15904 tree substed
= TREE_TYPE (decl
);
15908 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15910 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15911 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15912 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15914 unify_type_mismatch (explain_p
, args
[i
],
15915 TREE_VALUE (sarg
));
15924 if (excessive_deduction_depth
)
15926 if (deduction_depth
== 0)
15927 /* Reset once we're all the way out. */
15928 excessive_deduction_depth
= false;
15931 /* We can't free this if a pending_template entry or last_error_tinst_level
15932 is pointing at it. */
15933 if (last_pending_template
== old_last_pend
15934 && last_error_tinst_level
== old_error_tinst
)
15940 /* Adjust types before performing type deduction, as described in
15941 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15942 sections are symmetric. PARM is the type of a function parameter
15943 or the return type of the conversion function. ARG is the type of
15944 the argument passed to the call, or the type of the value
15945 initialized with the result of the conversion function.
15946 ARG_EXPR is the original argument expression, which may be null. */
15949 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15963 /* Swap PARM and ARG throughout the remainder of this
15964 function; the handling is precisely symmetric since PARM
15965 will initialize ARG rather than vice versa. */
15973 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15974 too, but here handle it by stripping the reference from PARM
15975 rather than by adding it to ARG. */
15976 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15977 && TYPE_REF_IS_RVALUE (*parm
)
15978 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15979 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15980 && TREE_CODE (*arg
) == REFERENCE_TYPE
15981 && !TYPE_REF_IS_RVALUE (*arg
))
15982 *parm
= TREE_TYPE (*parm
);
15983 /* Nothing else to do in this case. */
15987 gcc_unreachable ();
15990 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15992 /* [temp.deduct.call]
15994 If P is not a reference type:
15996 --If A is an array type, the pointer type produced by the
15997 array-to-pointer standard conversion (_conv.array_) is
15998 used in place of A for type deduction; otherwise,
16000 --If A is a function type, the pointer type produced by
16001 the function-to-pointer standard conversion
16002 (_conv.func_) is used in place of A for type deduction;
16005 --If A is a cv-qualified type, the top level
16006 cv-qualifiers of A's type are ignored for type
16008 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
16009 *arg
= build_pointer_type (TREE_TYPE (*arg
));
16010 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
16011 *arg
= build_pointer_type (*arg
);
16013 *arg
= TYPE_MAIN_VARIANT (*arg
);
16016 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16017 of the form T&&, where T is a template parameter, and the argument
16018 is an lvalue, T is deduced as A& */
16019 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16020 && TYPE_REF_IS_RVALUE (*parm
)
16021 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16022 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16023 && (arg_expr
? real_lvalue_p (arg_expr
)
16024 /* try_one_overload doesn't provide an arg_expr, but
16025 functions are always lvalues. */
16026 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
16027 *arg
= build_reference_type (*arg
);
16029 /* [temp.deduct.call]
16031 If P is a cv-qualified type, the top level cv-qualifiers
16032 of P's type are ignored for type deduction. If P is a
16033 reference type, the type referred to by P is used for
16035 *parm
= TYPE_MAIN_VARIANT (*parm
);
16036 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
16038 *parm
= TREE_TYPE (*parm
);
16039 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16042 /* DR 322. For conversion deduction, remove a reference type on parm
16043 too (which has been swapped into ARG). */
16044 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
16045 *arg
= TREE_TYPE (*arg
);
16050 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16051 template which does contain any deducible template parameters; check if
16052 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16053 unify_one_argument. */
16056 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
16057 int flags
, bool explain_p
)
16062 type
= TREE_TYPE (arg
);
16066 if (same_type_p (parm
, type
))
16067 return unify_success (explain_p
);
16069 if (strict
== DEDUCE_CONV
)
16071 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
16072 explain_p
? tf_warning_or_error
: tf_none
))
16073 return unify_success (explain_p
);
16075 else if (strict
!= DEDUCE_EXACT
)
16077 if (can_convert_arg (parm
, type
,
16078 TYPE_P (arg
) ? NULL_TREE
: arg
,
16079 flags
, explain_p
? tf_warning_or_error
: tf_none
))
16080 return unify_success (explain_p
);
16083 if (strict
== DEDUCE_EXACT
)
16084 return unify_type_mismatch (explain_p
, parm
, arg
);
16086 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
16089 static bool uses_deducible_template_parms (tree type
);
16091 /* Returns true iff the expression EXPR is one from which a template
16092 argument can be deduced. In other words, if it's an undecorated
16093 use of a template non-type parameter. */
16096 deducible_expression (tree expr
)
16098 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
16101 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16102 deducible way; that is, if it has a max value of <PARM> - 1. */
16105 deducible_array_bound (tree domain
)
16107 if (domain
== NULL_TREE
)
16110 tree max
= TYPE_MAX_VALUE (domain
);
16111 if (TREE_CODE (max
) != MINUS_EXPR
)
16114 return deducible_expression (TREE_OPERAND (max
, 0));
16117 /* Returns true iff the template arguments ARGS use a template parameter
16118 in a deducible way. */
16121 deducible_template_args (tree args
)
16123 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
16126 tree elt
= TREE_VEC_ELT (args
, i
);
16127 if (ARGUMENT_PACK_P (elt
))
16128 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
16131 if (PACK_EXPANSION_P (elt
))
16132 elt
= PACK_EXPANSION_PATTERN (elt
);
16133 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
16135 else if (TYPE_P (elt
))
16136 deducible
= uses_deducible_template_parms (elt
);
16138 deducible
= deducible_expression (elt
);
16146 /* Returns true iff TYPE contains any deducible references to template
16147 parameters, as per 14.8.2.5. */
16150 uses_deducible_template_parms (tree type
)
16152 if (PACK_EXPANSION_P (type
))
16153 type
= PACK_EXPANSION_PATTERN (type
);
16160 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16161 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16167 if (POINTER_TYPE_P (type
))
16168 return uses_deducible_template_parms (TREE_TYPE (type
));
16170 /* T[integer-constant ]
16172 if (TREE_CODE (type
) == ARRAY_TYPE
)
16173 return (uses_deducible_template_parms (TREE_TYPE (type
))
16174 || deducible_array_bound (TYPE_DOMAIN (type
)));
16186 if (TYPE_PTRMEM_P (type
))
16187 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16188 || (uses_deducible_template_parms
16189 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16191 /* template-name <T> (where template-name refers to a class template)
16192 template-name <i> (where template-name refers to a class template) */
16193 if (CLASS_TYPE_P (type
)
16194 && CLASSTYPE_TEMPLATE_INFO (type
)
16195 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16196 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16197 (CLASSTYPE_TI_ARGS (type
)));
16202 if (TREE_CODE (type
) == FUNCTION_TYPE
16203 || TREE_CODE (type
) == METHOD_TYPE
)
16205 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16207 tree parm
= TYPE_ARG_TYPES (type
);
16208 if (TREE_CODE (type
) == METHOD_TYPE
)
16209 parm
= TREE_CHAIN (parm
);
16210 for (; parm
; parm
= TREE_CHAIN (parm
))
16211 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16218 /* Subroutine of type_unification_real and unify_pack_expansion to
16219 handle unification of a single P/A pair. Parameters are as
16220 for those functions. */
16223 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16224 int subr
, unification_kind_t strict
, int flags
,
16227 tree arg_expr
= NULL_TREE
;
16230 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16231 return unify_invalid (explain_p
);
16232 if (arg
== unknown_type_node
)
16233 /* We can't deduce anything from this, but we might get all the
16234 template args from other function args. */
16235 return unify_success (explain_p
);
16237 /* Implicit conversions (Clause 4) will be performed on a function
16238 argument to convert it to the type of the corresponding function
16239 parameter if the parameter type contains no template-parameters that
16240 participate in template argument deduction. */
16241 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16242 /* For function parameters that contain no template-parameters at all,
16243 we have historically checked for convertibility in order to shortcut
16244 consideration of this candidate. */
16245 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16247 else if (strict
== DEDUCE_CALL
16248 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16249 /* For function parameters with only non-deducible template parameters,
16251 return unify_success (explain_p
);
16256 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16257 | UNIFY_ALLOW_MORE_CV_QUAL
16258 | UNIFY_ALLOW_DERIVED
);
16262 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16266 arg_strict
= UNIFY_ALLOW_NONE
;
16270 gcc_unreachable ();
16273 /* We only do these transformations if this is the top-level
16274 parameter_type_list in a call or declaration matching; in other
16275 situations (nested function declarators, template argument lists) we
16276 won't be comparing a type to an expression, and we don't do any type
16282 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16283 if (type_unknown_p (arg
))
16285 /* [temp.deduct.type] A template-argument can be
16286 deduced from a pointer to function or pointer
16287 to member function argument if the set of
16288 overloaded functions does not contain function
16289 templates and at most one of a set of
16290 overloaded functions provides a unique
16293 if (resolve_overloaded_unification
16294 (tparms
, targs
, parm
, arg
, strict
,
16295 arg_strict
, explain_p
))
16296 return unify_success (explain_p
);
16297 return unify_overload_resolution_failure (explain_p
, arg
);
16301 arg
= unlowered_expr_type (arg
);
16302 if (arg
== error_mark_node
)
16303 return unify_invalid (explain_p
);
16307 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16310 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16311 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16313 /* For deduction from an init-list we need the actual list. */
16314 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16316 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16319 /* Most parms like fn_type_unification.
16321 If SUBR is 1, we're being called recursively (to unify the
16322 arguments of a function or method parameter of a function
16325 CHECKS is a pointer to a vector of access checks encountered while
16326 substituting default template arguments. */
16329 type_unification_real (tree tparms
,
16333 unsigned int xnargs
,
16335 unification_kind_t strict
,
16337 vec
<deferred_access_check
, va_gc
> **checks
,
16342 int ntparms
= TREE_VEC_LENGTH (tparms
);
16343 int saw_undeduced
= 0;
16346 unsigned int nargs
;
16349 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16350 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16351 gcc_assert (ntparms
> 0);
16353 /* Reset the number of non-defaulted template arguments contained
16355 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16363 while (parms
&& parms
!= void_list_node
16366 parm
= TREE_VALUE (parms
);
16368 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16369 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16370 /* For a function parameter pack that occurs at the end of the
16371 parameter-declaration-list, the type A of each remaining
16372 argument of the call is compared with the type P of the
16373 declarator-id of the function parameter pack. */
16376 parms
= TREE_CHAIN (parms
);
16378 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16379 /* For a function parameter pack that does not occur at the
16380 end of the parameter-declaration-list, the type of the
16381 parameter pack is a non-deduced context. */
16387 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16393 && parms
!= void_list_node
16394 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16396 /* Unify the remaining arguments with the pack expansion type. */
16398 tree parmvec
= make_tree_vec (1);
16400 /* Allocate a TREE_VEC and copy in all of the arguments */
16401 argvec
= make_tree_vec (nargs
- ia
);
16402 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16403 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16405 /* Copy the parameter into parmvec. */
16406 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16407 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16408 /*subr=*/subr
, explain_p
))
16411 /* Advance to the end of the list of parameters. */
16412 parms
= TREE_CHAIN (parms
);
16415 /* Fail if we've reached the end of the parm list, and more args
16416 are present, and the parm list isn't variadic. */
16417 if (ia
< nargs
&& parms
== void_list_node
)
16418 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16419 /* Fail if parms are left and they don't have default values. */
16420 if (parms
&& parms
!= void_list_node
16421 && TREE_PURPOSE (parms
) == NULL_TREE
)
16423 unsigned int count
= nargs
;
16425 while (p
&& p
!= void_list_node
)
16428 p
= TREE_CHAIN (p
);
16430 return unify_too_few_arguments (explain_p
, ia
, count
);
16435 tsubst_flags_t complain
= (explain_p
16436 ? tf_warning_or_error
16439 for (i
= 0; i
< ntparms
; i
++)
16441 tree targ
= TREE_VEC_ELT (targs
, i
);
16442 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16444 /* Clear the "incomplete" flags on all argument packs now so that
16445 substituting them into later default arguments works. */
16446 if (targ
&& ARGUMENT_PACK_P (targ
))
16448 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16449 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16452 if (targ
|| tparm
== error_mark_node
)
16454 tparm
= TREE_VALUE (tparm
);
16456 /* If this is an undeduced nontype parameter that depends on
16457 a type parameter, try another pass; its type may have been
16458 deduced from a later argument than the one from which
16459 this parameter can be deduced. */
16460 if (TREE_CODE (tparm
) == PARM_DECL
16461 && uses_template_parms (TREE_TYPE (tparm
))
16462 && !saw_undeduced
++)
16465 /* Core issue #226 (C++0x) [temp.deduct]:
16467 If a template argument has not been deduced, its
16468 default template argument, if any, is used.
16470 When we are in C++98 mode, TREE_PURPOSE will either
16471 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16472 to explicitly check cxx_dialect here. */
16473 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16475 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16476 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16477 reopen_deferring_access_checks (*checks
);
16478 location_t save_loc
= input_location
;
16480 input_location
= DECL_SOURCE_LOCATION (parm
);
16481 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16482 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16484 input_location
= save_loc
;
16485 *checks
= get_deferred_access_checks ();
16486 pop_deferring_access_checks ();
16487 if (arg
== error_mark_node
)
16491 TREE_VEC_ELT (targs
, i
) = arg
;
16492 /* The position of the first default template argument,
16493 is also the number of non-defaulted arguments in TARGS.
16495 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16496 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16501 /* If the type parameter is a parameter pack, then it will
16502 be deduced to an empty parameter pack. */
16503 if (template_parameter_pack_p (tparm
))
16507 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16509 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16510 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16511 TREE_CONSTANT (arg
) = 1;
16514 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16516 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16518 TREE_VEC_ELT (targs
, i
) = arg
;
16522 return unify_parameter_deduction_failure (explain_p
, tparm
);
16525 #ifdef ENABLE_CHECKING
16526 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16527 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16530 return unify_success (explain_p
);
16533 /* Subroutine of type_unification_real. Args are like the variables
16534 at the call site. ARG is an overloaded function (or template-id);
16535 we try deducing template args from each of the overloads, and if
16536 only one succeeds, we go with that. Modifies TARGS and returns
16537 true on success. */
16540 resolve_overloaded_unification (tree tparms
,
16544 unification_kind_t strict
,
16548 tree tempargs
= copy_node (targs
);
16550 tree goodfn
= NULL_TREE
;
16553 if (TREE_CODE (arg
) == ADDR_EXPR
)
16555 arg
= TREE_OPERAND (arg
, 0);
16561 if (TREE_CODE (arg
) == COMPONENT_REF
)
16562 /* Handle `&x' where `x' is some static or non-static member
16564 arg
= TREE_OPERAND (arg
, 1);
16566 if (TREE_CODE (arg
) == OFFSET_REF
)
16567 arg
= TREE_OPERAND (arg
, 1);
16569 /* Strip baselink information. */
16570 if (BASELINK_P (arg
))
16571 arg
= BASELINK_FUNCTIONS (arg
);
16573 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16575 /* If we got some explicit template args, we need to plug them into
16576 the affected templates before we try to unify, in case the
16577 explicit args will completely resolve the templates in question. */
16580 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16581 arg
= TREE_OPERAND (arg
, 0);
16583 for (; arg
; arg
= OVL_NEXT (arg
))
16585 tree fn
= OVL_CURRENT (arg
);
16586 tree subargs
, elem
;
16588 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16591 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16592 expl_subargs
, NULL_TREE
, tf_none
,
16593 /*require_all_args=*/true,
16594 /*use_default_args=*/true);
16595 if (subargs
!= error_mark_node
16596 && !any_dependent_template_arguments_p (subargs
))
16598 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
16599 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16600 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16601 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16610 /* If no templates (or more than one) are fully resolved by the
16611 explicit arguments, this template-id is a non-deduced context; it
16612 could still be OK if we deduce all template arguments for the
16613 enclosing call through other arguments. */
16617 else if (TREE_CODE (arg
) != OVERLOAD
16618 && TREE_CODE (arg
) != FUNCTION_DECL
)
16619 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16620 -- but the deduction does not succeed because the expression is
16621 not just the function on its own. */
16624 for (; arg
; arg
= OVL_NEXT (arg
))
16625 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16626 TREE_TYPE (OVL_CURRENT (arg
)),
16627 strict
, sub_strict
, addr_p
, explain_p
)
16628 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16630 goodfn
= OVL_CURRENT (arg
);
16634 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16635 to function or pointer to member function argument if the set of
16636 overloaded functions does not contain function templates and at most
16637 one of a set of overloaded functions provides a unique match.
16639 So if we found multiple possibilities, we return success but don't
16640 deduce anything. */
16644 int i
= TREE_VEC_LENGTH (targs
);
16646 if (TREE_VEC_ELT (tempargs
, i
))
16647 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16655 /* Core DR 115: In contexts where deduction is done and fails, or in
16656 contexts where deduction is not done, if a template argument list is
16657 specified and it, along with any default template arguments, identifies
16658 a single function template specialization, then the template-id is an
16659 lvalue for the function template specialization. */
16662 resolve_nondeduced_context (tree orig_expr
)
16664 tree expr
, offset
, baselink
;
16667 if (!type_unknown_p (orig_expr
))
16672 offset
= NULL_TREE
;
16673 baselink
= NULL_TREE
;
16675 if (TREE_CODE (expr
) == ADDR_EXPR
)
16677 expr
= TREE_OPERAND (expr
, 0);
16680 if (TREE_CODE (expr
) == OFFSET_REF
)
16683 expr
= TREE_OPERAND (expr
, 1);
16685 if (BASELINK_P (expr
))
16688 expr
= BASELINK_FUNCTIONS (expr
);
16691 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16694 tree goodfn
= NULL_TREE
;
16696 /* If we got some explicit template args, we need to plug them into
16697 the affected templates before we try to unify, in case the
16698 explicit args will completely resolve the templates in question. */
16700 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16701 tree arg
= TREE_OPERAND (expr
, 0);
16702 tree badfn
= NULL_TREE
;
16703 tree badargs
= NULL_TREE
;
16705 for (; arg
; arg
= OVL_NEXT (arg
))
16707 tree fn
= OVL_CURRENT (arg
);
16708 tree subargs
, elem
;
16710 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16713 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16714 expl_subargs
, NULL_TREE
, tf_none
,
16715 /*require_all_args=*/true,
16716 /*use_default_args=*/true);
16717 if (subargs
!= error_mark_node
16718 && !any_dependent_template_arguments_p (subargs
))
16720 elem
= instantiate_template (fn
, subargs
, tf_none
);
16721 if (elem
== error_mark_node
)
16726 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16735 mark_used (goodfn
);
16738 expr
= build_baselink (BASELINK_BINFO (baselink
),
16739 BASELINK_ACCESS_BINFO (baselink
),
16740 expr
, BASELINK_OPTYPE (baselink
));
16744 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16745 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16748 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16751 else if (good
== 0 && badargs
)
16752 /* There were no good options and at least one bad one, so let the
16753 user know what the problem is. */
16754 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16759 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16760 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16761 different overloads deduce different arguments for a given parm.
16762 ADDR_P is true if the expression for which deduction is being
16763 performed was of the form "& fn" rather than simply "fn".
16765 Returns 1 on success. */
16768 try_one_overload (tree tparms
,
16773 unification_kind_t strict
,
16782 if (arg
== error_mark_node
)
16785 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16786 to function or pointer to member function argument if the set of
16787 overloaded functions does not contain function templates and at most
16788 one of a set of overloaded functions provides a unique match.
16790 So if this is a template, just return success. */
16792 if (uses_template_parms (arg
))
16795 if (TREE_CODE (arg
) == METHOD_TYPE
)
16796 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16798 arg
= build_pointer_type (arg
);
16800 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16802 /* We don't copy orig_targs for this because if we have already deduced
16803 some template args from previous args, unify would complain when we
16804 try to deduce a template parameter for the same argument, even though
16805 there isn't really a conflict. */
16806 nargs
= TREE_VEC_LENGTH (targs
);
16807 tempargs
= make_tree_vec (nargs
);
16809 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16812 /* First make sure we didn't deduce anything that conflicts with
16813 explicitly specified args. */
16814 for (i
= nargs
; i
--; )
16816 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16817 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16821 else if (uses_template_parms (elt
))
16822 /* Since we're unifying against ourselves, we will fill in
16823 template args used in the function parm list with our own
16824 template parms. Discard them. */
16825 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16826 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16830 for (i
= nargs
; i
--; )
16832 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16835 TREE_VEC_ELT (targs
, i
) = elt
;
16841 /* PARM is a template class (perhaps with unbound template
16842 parameters). ARG is a fully instantiated type. If ARG can be
16843 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16844 TARGS are as for unify. */
16847 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16850 tree copy_of_targs
;
16852 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16853 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16854 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16857 /* We need to make a new template argument vector for the call to
16858 unify. If we used TARGS, we'd clutter it up with the result of
16859 the attempted unification, even if this class didn't work out.
16860 We also don't want to commit ourselves to all the unifications
16861 we've already done, since unification is supposed to be done on
16862 an argument-by-argument basis. In other words, consider the
16863 following pathological case:
16865 template <int I, int J, int K>
16868 template <int I, int J>
16869 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16871 template <int I, int J, int K>
16872 void f(S<I, J, K>, S<I, I, I>);
16881 Now, by the time we consider the unification involving `s2', we
16882 already know that we must have `f<0, 0, 0>'. But, even though
16883 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16884 because there are two ways to unify base classes of S<0, 1, 2>
16885 with S<I, I, I>. If we kept the already deduced knowledge, we
16886 would reject the possibility I=1. */
16887 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16889 /* If unification failed, we're done. */
16890 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16891 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16897 /* Given a template type PARM and a class type ARG, find the unique
16898 base type in ARG that is an instance of PARM. We do not examine
16899 ARG itself; only its base-classes. If there is not exactly one
16900 appropriate base class, return NULL_TREE. PARM may be the type of
16901 a partial specialization, as well as a plain template type. Used
16904 static enum template_base_result
16905 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16906 bool explain_p
, tree
*result
)
16908 tree rval
= NULL_TREE
;
16911 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16913 binfo
= TYPE_BINFO (complete_type (arg
));
16916 /* The type could not be completed. */
16917 *result
= NULL_TREE
;
16918 return tbr_incomplete_type
;
16921 /* Walk in inheritance graph order. The search order is not
16922 important, and this avoids multiple walks of virtual bases. */
16923 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16925 tree r
= try_class_unification (tparms
, targs
, parm
,
16926 BINFO_TYPE (binfo
), explain_p
);
16930 /* If there is more than one satisfactory baseclass, then:
16934 If they yield more than one possible deduced A, the type
16938 if (rval
&& !same_type_p (r
, rval
))
16940 *result
= NULL_TREE
;
16941 return tbr_ambiguous_baseclass
;
16949 return tbr_success
;
16952 /* Returns the level of DECL, which declares a template parameter. */
16955 template_decl_level (tree decl
)
16957 switch (TREE_CODE (decl
))
16960 case TEMPLATE_DECL
:
16961 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16964 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16967 gcc_unreachable ();
16972 /* Decide whether ARG can be unified with PARM, considering only the
16973 cv-qualifiers of each type, given STRICT as documented for unify.
16974 Returns nonzero iff the unification is OK on that basis. */
16977 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16979 int arg_quals
= cp_type_quals (arg
);
16980 int parm_quals
= cp_type_quals (parm
);
16982 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16983 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16985 /* Although a CVR qualifier is ignored when being applied to a
16986 substituted template parameter ([8.3.2]/1 for example), that
16987 does not allow us to unify "const T" with "int&" because both
16988 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16989 It is ok when we're allowing additional CV qualifiers
16990 at the outer level [14.8.2.1]/3,1st bullet. */
16991 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16992 || TREE_CODE (arg
) == FUNCTION_TYPE
16993 || TREE_CODE (arg
) == METHOD_TYPE
)
16994 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16997 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16998 && (parm_quals
& TYPE_QUAL_RESTRICT
))
17002 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17003 && (arg_quals
& parm_quals
) != parm_quals
)
17006 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
17007 && (parm_quals
& arg_quals
) != arg_quals
)
17013 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17015 template_parm_level_and_index (tree parm
, int* level
, int* index
)
17017 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17018 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17019 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17021 *index
= TEMPLATE_TYPE_IDX (parm
);
17022 *level
= TEMPLATE_TYPE_LEVEL (parm
);
17026 *index
= TEMPLATE_PARM_IDX (parm
);
17027 *level
= TEMPLATE_PARM_LEVEL (parm
);
17031 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17033 if (unify (TP, TA, P, A, S, EP)) \
17037 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17038 expansion at the end of PACKED_PARMS. Returns 0 if the type
17039 deduction succeeds, 1 otherwise. STRICT is the same as in
17040 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17041 call argument list. We'll need to adjust the arguments to make them
17042 types. SUBR tells us if this is from a recursive call to
17043 type_unification_real, or for comparing two template argument
17047 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
17048 tree packed_args
, unification_kind_t strict
,
17049 bool subr
, bool explain_p
)
17052 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
17053 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
17054 tree pack
, packs
= NULL_TREE
;
17055 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
17057 packed_args
= expand_template_argument_pack (packed_args
);
17059 int len
= TREE_VEC_LENGTH (packed_args
);
17061 /* Determine the parameter packs we will be deducing from the
17062 pattern, and record their current deductions. */
17063 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
17064 pack
; pack
= TREE_CHAIN (pack
))
17066 tree parm_pack
= TREE_VALUE (pack
);
17069 /* Determine the index and level of this parameter pack. */
17070 template_parm_level_and_index (parm_pack
, &level
, &idx
);
17072 /* Keep track of the parameter packs and their corresponding
17074 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
17075 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
17078 /* Loop through all of the arguments that have not yet been
17079 unified and unify each with the pattern. */
17080 for (i
= start
; i
< len
; i
++)
17083 bool any_explicit
= false;
17084 tree arg
= TREE_VEC_ELT (packed_args
, i
);
17086 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17087 or the element of its argument pack at the current index if
17088 this argument was explicitly specified. */
17089 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17093 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17096 if (TREE_VALUE (pack
)
17097 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
17098 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
17100 any_explicit
= true;
17101 arg
= TREE_VEC_ELT (pargs
, i
- start
);
17103 TMPL_ARG (targs
, level
, idx
) = arg
;
17106 /* If we had explicit template arguments, substitute them into the
17107 pattern before deduction. */
17110 /* Some arguments might still be unspecified or dependent. */
17112 ++processing_template_decl
;
17113 dependent
= any_dependent_template_arguments_p (targs
);
17115 --processing_template_decl
;
17116 parm
= tsubst (pattern
, targs
,
17117 explain_p
? tf_warning_or_error
: tf_none
,
17120 --processing_template_decl
;
17121 if (parm
== error_mark_node
)
17127 /* Unify the pattern with the current argument. */
17128 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17129 LOOKUP_IMPLICIT
, explain_p
))
17132 /* For each parameter pack, collect the deduced value. */
17133 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17136 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17138 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
17139 TMPL_ARG (targs
, level
, idx
);
17143 /* Verify that the results of unification with the parameter packs
17144 produce results consistent with what we've seen before, and make
17145 the deduced argument packs available. */
17146 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17148 tree old_pack
= TREE_VALUE (pack
);
17149 tree new_args
= TREE_TYPE (pack
);
17150 int i
, len
= TREE_VEC_LENGTH (new_args
);
17152 bool nondeduced_p
= false;
17154 /* By default keep the original deduced argument pack.
17155 If necessary, more specific code is going to update the
17156 resulting deduced argument later down in this function. */
17157 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17158 TMPL_ARG (targs
, level
, idx
) = old_pack
;
17160 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17161 actually deduce anything. */
17162 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
17163 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
17164 nondeduced_p
= true;
17168 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
17170 /* If we had fewer function args than explicit template args,
17171 just use the explicits. */
17172 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17173 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
17174 if (len
< explicit_len
)
17175 new_args
= explicit_args
;
17181 /* Build the deduced *_ARGUMENT_PACK. */
17182 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17184 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17185 TREE_TYPE (result
) =
17186 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17187 TREE_CONSTANT (result
) = 1;
17190 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17192 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17194 /* Note the deduced argument packs for this parameter
17196 TMPL_ARG (targs
, level
, idx
) = result
;
17198 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17199 && (ARGUMENT_PACK_ARGS (old_pack
)
17200 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17202 /* We only had the explicitly-provided arguments before, but
17203 now we have a complete set of arguments. */
17204 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17206 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17207 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17208 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17212 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17213 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17215 if (!comp_template_args_with_info (old_args
, new_args
,
17216 &bad_old_arg
, &bad_new_arg
))
17217 /* Inconsistent unification of this parameter pack. */
17218 return unify_parameter_pack_inconsistent (explain_p
,
17224 return unify_success (explain_p
);
17227 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17228 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17229 parameters and return value are as for unify. */
17232 unify_array_domain (tree tparms
, tree targs
,
17233 tree parm_dom
, tree arg_dom
,
17241 /* Our representation of array types uses "N - 1" as the
17242 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17243 not an integer constant. We cannot unify arbitrarily
17244 complex expressions, so we eliminate the MINUS_EXPRs
17246 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17247 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17250 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17251 parm_max
= TREE_OPERAND (parm_max
, 0);
17253 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17254 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17257 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17258 trying to unify the type of a variable with the type
17259 of a template parameter. For example:
17261 template <unsigned int N>
17262 void f (char (&) [N]);
17269 Here, the type of the ARG will be "int [g(i)]", and
17270 may be a SAVE_EXPR, etc. */
17271 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17272 return unify_vla_arg (explain_p
, arg_dom
);
17273 arg_max
= TREE_OPERAND (arg_max
, 0);
17276 /* If only one of the bounds used a MINUS_EXPR, compensate
17277 by adding one to the other bound. */
17278 if (parm_cst
&& !arg_cst
)
17279 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17283 else if (arg_cst
&& !parm_cst
)
17284 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17289 return unify (tparms
, targs
, parm_max
, arg_max
,
17290 UNIFY_ALLOW_INTEGER
, explain_p
);
17293 /* Deduce the value of template parameters. TPARMS is the (innermost)
17294 set of template parameters to a template. TARGS is the bindings
17295 for those template parameters, as determined thus far; TARGS may
17296 include template arguments for outer levels of template parameters
17297 as well. PARM is a parameter to a template function, or a
17298 subcomponent of that parameter; ARG is the corresponding argument.
17299 This function attempts to match PARM with ARG in a manner
17300 consistent with the existing assignments in TARGS. If more values
17301 are deduced, then TARGS is updated.
17303 Returns 0 if the type deduction succeeds, 1 otherwise. The
17304 parameter STRICT is a bitwise or of the following flags:
17307 Require an exact match between PARM and ARG.
17308 UNIFY_ALLOW_MORE_CV_QUAL:
17309 Allow the deduced ARG to be more cv-qualified (by qualification
17310 conversion) than ARG.
17311 UNIFY_ALLOW_LESS_CV_QUAL:
17312 Allow the deduced ARG to be less cv-qualified than ARG.
17313 UNIFY_ALLOW_DERIVED:
17314 Allow the deduced ARG to be a template base class of ARG,
17315 or a pointer to a template base class of the type pointed to by
17317 UNIFY_ALLOW_INTEGER:
17318 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17319 case for more information.
17320 UNIFY_ALLOW_OUTER_LEVEL:
17321 This is the outermost level of a deduction. Used to determine validity
17322 of qualification conversions. A valid qualification conversion must
17323 have const qualified pointers leading up to the inner type which
17324 requires additional CV quals, except at the outer level, where const
17325 is not required [conv.qual]. It would be normal to set this flag in
17326 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17327 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17328 This is the outermost level of a deduction, and PARM can be more CV
17329 qualified at this point.
17330 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17331 This is the outermost level of a deduction, and PARM can be less CV
17332 qualified at this point. */
17335 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17341 int strict_in
= strict
;
17343 /* I don't think this will do the right thing with respect to types.
17344 But the only case I've seen it in so far has been array bounds, where
17345 signedness is the only information lost, and I think that will be
17347 while (TREE_CODE (parm
) == NOP_EXPR
)
17348 parm
= TREE_OPERAND (parm
, 0);
17350 if (arg
== error_mark_node
)
17351 return unify_invalid (explain_p
);
17352 if (arg
== unknown_type_node
17353 || arg
== init_list_type_node
)
17354 /* We can't deduce anything from this, but we might get all the
17355 template args from other function args. */
17356 return unify_success (explain_p
);
17358 /* If PARM uses template parameters, then we can't bail out here,
17359 even if ARG == PARM, since we won't record unifications for the
17360 template parameters. We might need them if we're trying to
17361 figure out which of two things is more specialized. */
17362 if (arg
== parm
&& !uses_template_parms (parm
))
17363 return unify_success (explain_p
);
17365 /* Handle init lists early, so the rest of the function can assume
17366 we're dealing with a type. */
17367 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17371 tree orig_parm
= parm
;
17373 /* Replace T with std::initializer_list<T> for deduction. */
17374 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17375 && flag_deduce_init_list
)
17376 parm
= listify (parm
);
17378 if (!is_std_init_list (parm
)
17379 && TREE_CODE (parm
) != ARRAY_TYPE
)
17380 /* We can only deduce from an initializer list argument if the
17381 parameter is std::initializer_list or an array; otherwise this
17382 is a non-deduced context. */
17383 return unify_success (explain_p
);
17385 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17386 elttype
= TREE_TYPE (parm
);
17388 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17390 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17392 int elt_strict
= strict
;
17394 if (elt
== error_mark_node
)
17395 return unify_invalid (explain_p
);
17397 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17399 tree type
= TREE_TYPE (elt
);
17400 /* It should only be possible to get here for a call. */
17401 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17402 elt_strict
|= maybe_adjust_types_for_deduction
17403 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17407 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17411 if (TREE_CODE (parm
) == ARRAY_TYPE
17412 && deducible_array_bound (TYPE_DOMAIN (parm
)))
17414 /* Also deduce from the length of the initializer list. */
17415 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17416 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17417 if (idx
== error_mark_node
)
17418 return unify_invalid (explain_p
);
17419 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17423 /* If the std::initializer_list<T> deduction worked, replace the
17424 deduced A with std::initializer_list<A>. */
17425 if (orig_parm
!= parm
)
17427 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17428 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17429 targ
= listify (targ
);
17430 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17432 return unify_success (explain_p
);
17435 /* Immediately reject some pairs that won't unify because of
17436 cv-qualification mismatches. */
17437 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17439 /* It is the elements of the array which hold the cv quals of an array
17440 type, and the elements might be template type parms. We'll check
17441 when we recurse. */
17442 && TREE_CODE (arg
) != ARRAY_TYPE
17443 /* We check the cv-qualifiers when unifying with template type
17444 parameters below. We want to allow ARG `const T' to unify with
17445 PARM `T' for example, when computing which of two templates
17446 is more specialized, for example. */
17447 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17448 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17449 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17451 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17452 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17453 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17454 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17455 strict
&= ~UNIFY_ALLOW_DERIVED
;
17456 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17457 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17459 switch (TREE_CODE (parm
))
17461 case TYPENAME_TYPE
:
17463 case UNBOUND_CLASS_TEMPLATE
:
17464 /* In a type which contains a nested-name-specifier, template
17465 argument values cannot be deduced for template parameters used
17466 within the nested-name-specifier. */
17467 return unify_success (explain_p
);
17469 case TEMPLATE_TYPE_PARM
:
17470 case TEMPLATE_TEMPLATE_PARM
:
17471 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17472 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17473 if (tparm
== error_mark_node
)
17474 return unify_invalid (explain_p
);
17476 if (TEMPLATE_TYPE_LEVEL (parm
)
17477 != template_decl_level (tparm
))
17478 /* The PARM is not one we're trying to unify. Just check
17479 to see if it matches ARG. */
17481 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17482 && (is_auto (parm
) ? is_auto (arg
)
17483 : same_type_p (parm
, arg
)))
17484 return unify_success (explain_p
);
17486 return unify_type_mismatch (explain_p
, parm
, arg
);
17488 idx
= TEMPLATE_TYPE_IDX (parm
);
17489 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17490 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17491 if (tparm
== error_mark_node
)
17492 return unify_invalid (explain_p
);
17494 /* Check for mixed types and values. */
17495 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17496 && TREE_CODE (tparm
) != TYPE_DECL
)
17497 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17498 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17499 gcc_unreachable ();
17501 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17503 /* ARG must be constructed from a template class or a template
17504 template parameter. */
17505 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17506 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17507 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17509 tree parmvec
= TYPE_TI_ARGS (parm
);
17510 /* An alias template name is never deduced. */
17511 if (TYPE_ALIAS_P (arg
))
17512 arg
= strip_typedefs (arg
);
17513 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17514 tree full_argvec
= add_to_template_args (targs
, argvec
);
17516 = DECL_INNERMOST_TEMPLATE_PARMS
17517 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17519 int parm_variadic_p
= 0;
17521 /* The resolution to DR150 makes clear that default
17522 arguments for an N-argument may not be used to bind T
17523 to a template template parameter with fewer than N
17524 parameters. It is not safe to permit the binding of
17525 default arguments as an extension, as that may change
17526 the meaning of a conforming program. Consider:
17528 struct Dense { static const unsigned int dim = 1; };
17530 template <template <typename> class View,
17532 void operator+(float, View<Block> const&);
17534 template <typename Block,
17535 unsigned int Dim = Block::dim>
17536 struct Lvalue_proxy { operator float() const; };
17540 Lvalue_proxy<Dense> p;
17545 Here, if Lvalue_proxy is permitted to bind to View, then
17546 the global operator+ will be used; if they are not, the
17547 Lvalue_proxy will be converted to float. */
17548 if (coerce_template_parms (parm_parms
,
17550 TYPE_TI_TEMPLATE (parm
),
17552 ? tf_warning_or_error
17554 /*require_all_args=*/true,
17555 /*use_default_args=*/false)
17556 == error_mark_node
)
17559 /* Deduce arguments T, i from TT<T> or TT<i>.
17560 We check each element of PARMVEC and ARGVEC individually
17561 rather than the whole TREE_VEC since they can have
17562 different number of elements. */
17564 parmvec
= expand_template_argument_pack (parmvec
);
17565 argvec
= expand_template_argument_pack (argvec
);
17567 len
= TREE_VEC_LENGTH (parmvec
);
17569 /* Check if the parameters end in a pack, making them
17572 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17573 parm_variadic_p
= 1;
17575 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17576 /* If the template argument list of P contains a pack
17577 expansion that is not the last template argument, the
17578 entire template argument list is a non-deduced
17580 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17581 return unify_success (explain_p
);
17583 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17584 return unify_too_few_arguments (explain_p
,
17585 TREE_VEC_LENGTH (argvec
), len
);
17587 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17589 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17590 TREE_VEC_ELT (parmvec
, i
),
17591 TREE_VEC_ELT (argvec
, i
),
17592 UNIFY_ALLOW_NONE
, explain_p
);
17595 if (parm_variadic_p
17596 && unify_pack_expansion (tparms
, targs
,
17599 /*subr=*/true, explain_p
))
17602 arg
= TYPE_TI_TEMPLATE (arg
);
17604 /* Fall through to deduce template name. */
17607 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17608 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17610 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17612 /* Simple cases: Value already set, does match or doesn't. */
17613 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17614 return unify_success (explain_p
);
17616 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17620 /* If PARM is `const T' and ARG is only `int', we don't have
17621 a match unless we are allowing additional qualification.
17622 If ARG is `const int' and PARM is just `T' that's OK;
17623 that binds `const int' to `T'. */
17624 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17626 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17628 /* Consider the case where ARG is `const volatile int' and
17629 PARM is `const T'. Then, T should be `volatile int'. */
17630 arg
= cp_build_qualified_type_real
17631 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17632 if (arg
== error_mark_node
)
17633 return unify_invalid (explain_p
);
17635 /* Simple cases: Value already set, does match or doesn't. */
17636 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17637 return unify_success (explain_p
);
17639 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17641 /* Make sure that ARG is not a variable-sized array. (Note
17642 that were talking about variable-sized arrays (like
17643 `int[n]'), rather than arrays of unknown size (like
17644 `int[]').) We'll get very confused by such a type since
17645 the bound of the array is not constant, and therefore
17646 not mangleable. Besides, such types are not allowed in
17647 ISO C++, so we can do as we please here. We do allow
17648 them for 'auto' deduction, since that isn't ABI-exposed. */
17649 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17650 return unify_vla_arg (explain_p
, arg
);
17652 /* Strip typedefs as in convert_template_argument. */
17653 arg
= canonicalize_type_argument (arg
, tf_none
);
17656 /* If ARG is a parameter pack or an expansion, we cannot unify
17657 against it unless PARM is also a parameter pack. */
17658 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17659 && !template_parameter_pack_p (parm
))
17660 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17662 /* If the argument deduction results is a METHOD_TYPE,
17663 then there is a problem.
17664 METHOD_TYPE doesn't map to any real C++ type the result of
17665 the deduction can not be of that type. */
17666 if (TREE_CODE (arg
) == METHOD_TYPE
)
17667 return unify_method_type_error (explain_p
, arg
);
17669 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17670 return unify_success (explain_p
);
17672 case TEMPLATE_PARM_INDEX
:
17673 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17674 if (tparm
== error_mark_node
)
17675 return unify_invalid (explain_p
);
17677 if (TEMPLATE_PARM_LEVEL (parm
)
17678 != template_decl_level (tparm
))
17680 /* The PARM is not one we're trying to unify. Just check
17681 to see if it matches ARG. */
17682 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17683 && cp_tree_equal (parm
, arg
));
17685 unify_expression_unequal (explain_p
, parm
, arg
);
17689 idx
= TEMPLATE_PARM_IDX (parm
);
17690 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17694 int x
= !cp_tree_equal (targ
, arg
);
17696 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17700 /* [temp.deduct.type] If, in the declaration of a function template
17701 with a non-type template-parameter, the non-type
17702 template-parameter is used in an expression in the function
17703 parameter-list and, if the corresponding template-argument is
17704 deduced, the template-argument type shall match the type of the
17705 template-parameter exactly, except that a template-argument
17706 deduced from an array bound may be of any integral type.
17707 The non-type parameter might use already deduced type parameters. */
17708 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17709 if (!TREE_TYPE (arg
))
17710 /* Template-parameter dependent expression. Just accept it for now.
17711 It will later be processed in convert_template_argument. */
17713 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17715 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17716 && CP_INTEGRAL_TYPE_P (tparm
))
17717 /* Convert the ARG to the type of PARM; the deduced non-type
17718 template argument must exactly match the types of the
17719 corresponding parameter. */
17720 arg
= fold (build_nop (tparm
, arg
));
17721 else if (uses_template_parms (tparm
))
17722 /* We haven't deduced the type of this parameter yet. Try again
17724 return unify_success (explain_p
);
17726 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17728 /* If ARG is a parameter pack or an expansion, we cannot unify
17729 against it unless PARM is also a parameter pack. */
17730 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17731 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17732 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17734 arg
= strip_typedefs_expr (arg
);
17735 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17736 return unify_success (explain_p
);
17740 /* A pointer-to-member constant can be unified only with
17741 another constant. */
17742 if (TREE_CODE (arg
) != PTRMEM_CST
)
17743 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17745 /* Just unify the class member. It would be useless (and possibly
17746 wrong, depending on the strict flags) to unify also
17747 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17748 arg refer to the same variable, even if through different
17749 classes. For instance:
17751 struct A { int x; };
17754 Unification of &A::x and &B::x must succeed. */
17755 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17756 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17761 if (!TYPE_PTR_P (arg
))
17762 return unify_type_mismatch (explain_p
, parm
, arg
);
17764 /* [temp.deduct.call]
17766 A can be another pointer or pointer to member type that can
17767 be converted to the deduced A via a qualification
17768 conversion (_conv.qual_).
17770 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17771 This will allow for additional cv-qualification of the
17772 pointed-to types if appropriate. */
17774 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17775 /* The derived-to-base conversion only persists through one
17776 level of pointers. */
17777 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17779 return unify (tparms
, targs
, TREE_TYPE (parm
),
17780 TREE_TYPE (arg
), strict
, explain_p
);
17783 case REFERENCE_TYPE
:
17784 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17785 return unify_type_mismatch (explain_p
, parm
, arg
);
17786 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17787 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17790 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17791 return unify_type_mismatch (explain_p
, parm
, arg
);
17792 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17793 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17794 return unify_type_mismatch (explain_p
, parm
, arg
);
17795 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17796 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17797 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17798 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17799 TYPE_DOMAIN (arg
), explain_p
);
17800 return unify_success (explain_p
);
17807 case ENUMERAL_TYPE
:
17810 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17811 return unify_type_mismatch (explain_p
, parm
, arg
);
17813 /* We have already checked cv-qualification at the top of the
17815 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17816 return unify_type_mismatch (explain_p
, parm
, arg
);
17818 /* As far as unification is concerned, this wins. Later checks
17819 will invalidate it if necessary. */
17820 return unify_success (explain_p
);
17822 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17823 /* Type INTEGER_CST can come from ordinary constant template args. */
17825 while (TREE_CODE (arg
) == NOP_EXPR
)
17826 arg
= TREE_OPERAND (arg
, 0);
17828 if (TREE_CODE (arg
) != INTEGER_CST
)
17829 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17830 return (tree_int_cst_equal (parm
, arg
)
17831 ? unify_success (explain_p
)
17832 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17836 int i
, len
, argslen
;
17837 int parm_variadic_p
= 0;
17839 if (TREE_CODE (arg
) != TREE_VEC
)
17840 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17842 len
= TREE_VEC_LENGTH (parm
);
17843 argslen
= TREE_VEC_LENGTH (arg
);
17845 /* Check for pack expansions in the parameters. */
17846 for (i
= 0; i
< len
; ++i
)
17848 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17851 /* We can unify against something with a trailing
17853 parm_variadic_p
= 1;
17855 /* [temp.deduct.type]/9: If the template argument list of
17856 P contains a pack expansion that is not the last
17857 template argument, the entire template argument list
17858 is a non-deduced context. */
17859 return unify_success (explain_p
);
17863 /* If we don't have enough arguments to satisfy the parameters
17864 (not counting the pack expression at the end), or we have
17865 too many arguments for a parameter list that doesn't end in
17866 a pack expression, we can't unify. */
17867 if (parm_variadic_p
17868 ? argslen
< len
- parm_variadic_p
17870 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17872 /* Unify all of the parameters that precede the (optional)
17873 pack expression. */
17874 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17876 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17877 TREE_VEC_ELT (parm
, i
),
17878 TREE_VEC_ELT (arg
, i
),
17879 UNIFY_ALLOW_NONE
, explain_p
);
17881 if (parm_variadic_p
)
17882 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17884 /*subr=*/true, explain_p
);
17885 return unify_success (explain_p
);
17890 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17891 return unify_type_mismatch (explain_p
, parm
, arg
);
17893 if (TYPE_PTRMEMFUNC_P (parm
))
17895 if (!TYPE_PTRMEMFUNC_P (arg
))
17896 return unify_type_mismatch (explain_p
, parm
, arg
);
17898 return unify (tparms
, targs
,
17899 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17900 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17901 strict
, explain_p
);
17904 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17906 tree t
= NULL_TREE
;
17908 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17910 /* First, we try to unify the PARM and ARG directly. */
17911 t
= try_class_unification (tparms
, targs
,
17912 parm
, arg
, explain_p
);
17916 /* Fallback to the special case allowed in
17917 [temp.deduct.call]:
17919 If P is a class, and P has the form
17920 template-id, then A can be a derived class of
17921 the deduced A. Likewise, if P is a pointer to
17922 a class of the form template-id, A can be a
17923 pointer to a derived class pointed to by the
17925 enum template_base_result r
;
17926 r
= get_template_base (tparms
, targs
, parm
, arg
,
17930 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17933 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17934 && (CLASSTYPE_TI_TEMPLATE (parm
)
17935 == CLASSTYPE_TI_TEMPLATE (arg
)))
17936 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17937 Then, we should unify `int' and `U'. */
17940 /* There's no chance of unification succeeding. */
17941 return unify_type_mismatch (explain_p
, parm
, arg
);
17943 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17944 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17946 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17947 return unify_type_mismatch (explain_p
, parm
, arg
);
17948 return unify_success (explain_p
);
17951 case FUNCTION_TYPE
:
17953 unsigned int nargs
;
17958 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17959 return unify_type_mismatch (explain_p
, parm
, arg
);
17961 /* CV qualifications for methods can never be deduced, they must
17962 match exactly. We need to check them explicitly here,
17963 because type_unification_real treats them as any other
17964 cv-qualified parameter. */
17965 if (TREE_CODE (parm
) == METHOD_TYPE
17966 && (!check_cv_quals_for_unify
17968 class_of_this_parm (arg
),
17969 class_of_this_parm (parm
))))
17970 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17972 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17973 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17975 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17976 args
= XALLOCAVEC (tree
, nargs
);
17977 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17978 a
!= NULL_TREE
&& a
!= void_list_node
;
17979 a
= TREE_CHAIN (a
), ++i
)
17980 args
[i
] = TREE_VALUE (a
);
17983 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17984 args
, nargs
, 1, DEDUCE_EXACT
,
17985 LOOKUP_NORMAL
, NULL
, explain_p
);
17989 /* Unify a pointer to member with a pointer to member function, which
17990 deduces the type of the member as a function type. */
17991 if (TYPE_PTRMEMFUNC_P (arg
))
17993 /* Check top-level cv qualifiers */
17994 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17995 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17997 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17998 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17999 UNIFY_ALLOW_NONE
, explain_p
);
18001 /* Determine the type of the function we are unifying against. */
18002 tree fntype
= static_fn_type (arg
);
18004 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
18007 if (TREE_CODE (arg
) != OFFSET_TYPE
)
18008 return unify_type_mismatch (explain_p
, parm
, arg
);
18009 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18010 TYPE_OFFSET_BASETYPE (arg
),
18011 UNIFY_ALLOW_NONE
, explain_p
);
18012 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18013 strict
, explain_p
);
18016 if (DECL_TEMPLATE_PARM_P (parm
))
18017 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
18018 if (arg
!= integral_constant_value (parm
))
18019 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18020 return unify_success (explain_p
);
18023 case TEMPLATE_DECL
:
18024 /* Matched cases are handled by the ARG == PARM test above. */
18025 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18028 /* A non-type template parameter that is a variable should be a
18029 an integral constant, in which case, it whould have been
18030 folded into its (constant) value. So we should not be getting
18031 a variable here. */
18032 gcc_unreachable ();
18034 case TYPE_ARGUMENT_PACK
:
18035 case NONTYPE_ARGUMENT_PACK
:
18036 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
18037 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
18040 case DECLTYPE_TYPE
:
18041 case UNDERLYING_TYPE
:
18042 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18043 or UNDERLYING_TYPE nodes. */
18044 return unify_success (explain_p
);
18047 /* Unification fails if we hit an error node. */
18048 return unify_invalid (explain_p
);
18051 if (REFERENCE_REF_P (parm
))
18052 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
18053 strict
, explain_p
);
18057 /* An unresolved overload is a nondeduced context. */
18058 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
18059 return unify_success (explain_p
);
18060 gcc_assert (EXPR_P (parm
));
18062 /* We must be looking at an expression. This can happen with
18066 void foo(S<I>, S<I + 2>);
18068 This is a "nondeduced context":
18072 The nondeduced contexts are:
18074 --A type that is a template-id in which one or more of
18075 the template-arguments is an expression that references
18076 a template-parameter.
18078 In these cases, we assume deduction succeeded, but don't
18079 actually infer any unifications. */
18081 if (!uses_template_parms (parm
)
18082 && !template_args_equal (parm
, arg
))
18083 return unify_expression_unequal (explain_p
, parm
, arg
);
18085 return unify_success (explain_p
);
18088 #undef RECUR_AND_CHECK_FAILURE
18090 /* Note that DECL can be defined in this translation unit, if
18094 mark_definable (tree decl
)
18097 DECL_NOT_REALLY_EXTERN (decl
) = 1;
18098 FOR_EACH_CLONE (clone
, decl
)
18099 DECL_NOT_REALLY_EXTERN (clone
) = 1;
18102 /* Called if RESULT is explicitly instantiated, or is a member of an
18103 explicitly instantiated class. */
18106 mark_decl_instantiated (tree result
, int extern_p
)
18108 SET_DECL_EXPLICIT_INSTANTIATION (result
);
18110 /* If this entity has already been written out, it's too late to
18111 make any modifications. */
18112 if (TREE_ASM_WRITTEN (result
))
18115 /* For anonymous namespace we don't need to do anything. */
18116 if (decl_anon_ns_mem_p (result
))
18118 gcc_assert (!TREE_PUBLIC (result
));
18122 if (TREE_CODE (result
) != FUNCTION_DECL
)
18123 /* The TREE_PUBLIC flag for function declarations will have been
18124 set correctly by tsubst. */
18125 TREE_PUBLIC (result
) = 1;
18127 /* This might have been set by an earlier implicit instantiation. */
18128 DECL_COMDAT (result
) = 0;
18131 DECL_NOT_REALLY_EXTERN (result
) = 0;
18134 mark_definable (result
);
18135 mark_needed (result
);
18136 /* Always make artificials weak. */
18137 if (DECL_ARTIFICIAL (result
) && flag_weak
)
18138 comdat_linkage (result
);
18139 /* For WIN32 we also want to put explicit instantiations in
18140 linkonce sections. */
18141 else if (TREE_PUBLIC (result
))
18142 maybe_make_one_only (result
);
18145 /* If EXTERN_P, then this function will not be emitted -- unless
18146 followed by an explicit instantiation, at which point its linkage
18147 will be adjusted. If !EXTERN_P, then this function will be
18148 emitted here. In neither circumstance do we want
18149 import_export_decl to adjust the linkage. */
18150 DECL_INTERFACE_KNOWN (result
) = 1;
18153 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18154 important template arguments. If any are missing, we check whether
18155 they're important by using error_mark_node for substituting into any
18156 args that were used for partial ordering (the ones between ARGS and END)
18157 and seeing if it bubbles up. */
18160 check_undeduced_parms (tree targs
, tree args
, tree end
)
18162 bool found
= false;
18164 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
18165 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
18168 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
18172 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
18173 if (substed
== error_mark_node
)
18179 /* Given two function templates PAT1 and PAT2, return:
18181 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18182 -1 if PAT2 is more specialized than PAT1.
18183 0 if neither is more specialized.
18185 LEN indicates the number of parameters we should consider
18186 (defaulted parameters should not be considered).
18188 The 1998 std underspecified function template partial ordering, and
18189 DR214 addresses the issue. We take pairs of arguments, one from
18190 each of the templates, and deduce them against each other. One of
18191 the templates will be more specialized if all the *other*
18192 template's arguments deduce against its arguments and at least one
18193 of its arguments *does* *not* deduce against the other template's
18194 corresponding argument. Deduction is done as for class templates.
18195 The arguments used in deduction have reference and top level cv
18196 qualifiers removed. Iff both arguments were originally reference
18197 types *and* deduction succeeds in both directions, an lvalue reference
18198 wins against an rvalue reference and otherwise the template
18199 with the more cv-qualified argument wins for that pairing (if
18200 neither is more cv-qualified, they both are equal). Unlike regular
18201 deduction, after all the arguments have been deduced in this way,
18202 we do *not* verify the deduced template argument values can be
18203 substituted into non-deduced contexts.
18205 The logic can be a bit confusing here, because we look at deduce1 and
18206 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18207 can find template arguments for pat1 to make arg1 look like arg2, that
18208 means that arg2 is at least as specialized as arg1. */
18211 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18213 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18214 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18215 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18216 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18217 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18218 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18219 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18220 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18221 tree origs1
, origs2
;
18222 bool lose1
= false;
18223 bool lose2
= false;
18225 /* Remove the this parameter from non-static member functions. If
18226 one is a non-static member function and the other is not a static
18227 member function, remove the first parameter from that function
18228 also. This situation occurs for operator functions where we
18229 locate both a member function (with this pointer) and non-member
18230 operator (with explicit first operand). */
18231 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18233 len
--; /* LEN is the number of significant arguments for DECL1 */
18234 args1
= TREE_CHAIN (args1
);
18235 if (!DECL_STATIC_FUNCTION_P (decl2
))
18236 args2
= TREE_CHAIN (args2
);
18238 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18240 args2
= TREE_CHAIN (args2
);
18241 if (!DECL_STATIC_FUNCTION_P (decl1
))
18244 args1
= TREE_CHAIN (args1
);
18248 /* If only one is a conversion operator, they are unordered. */
18249 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18252 /* Consider the return type for a conversion function */
18253 if (DECL_CONV_FN_P (decl1
))
18255 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18256 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18260 processing_template_decl
++;
18266 /* Stop when an ellipsis is seen. */
18267 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18269 tree arg1
= TREE_VALUE (args1
);
18270 tree arg2
= TREE_VALUE (args2
);
18271 int deduce1
, deduce2
;
18277 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18278 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18280 /* When both arguments are pack expansions, we need only
18281 unify the patterns themselves. */
18282 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18283 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18285 /* This is the last comparison we need to do. */
18289 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18291 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18292 arg1
= TREE_TYPE (arg1
);
18293 quals1
= cp_type_quals (arg1
);
18296 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18298 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18299 arg2
= TREE_TYPE (arg2
);
18300 quals2
= cp_type_quals (arg2
);
18303 arg1
= TYPE_MAIN_VARIANT (arg1
);
18304 arg2
= TYPE_MAIN_VARIANT (arg2
);
18306 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18308 int i
, len2
= list_length (args2
);
18309 tree parmvec
= make_tree_vec (1);
18310 tree argvec
= make_tree_vec (len2
);
18313 /* Setup the parameter vector, which contains only ARG1. */
18314 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18316 /* Setup the argument vector, which contains the remaining
18318 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18319 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18321 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18322 argvec
, DEDUCE_EXACT
,
18323 /*subr=*/true, /*explain_p=*/false)
18326 /* We cannot deduce in the other direction, because ARG1 is
18327 a pack expansion but ARG2 is not. */
18330 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18332 int i
, len1
= list_length (args1
);
18333 tree parmvec
= make_tree_vec (1);
18334 tree argvec
= make_tree_vec (len1
);
18337 /* Setup the parameter vector, which contains only ARG1. */
18338 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18340 /* Setup the argument vector, which contains the remaining
18342 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18343 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18345 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18346 argvec
, DEDUCE_EXACT
,
18347 /*subr=*/true, /*explain_p=*/false)
18350 /* We cannot deduce in the other direction, because ARG2 is
18351 a pack expansion but ARG1 is not.*/
18357 /* The normal case, where neither argument is a pack
18359 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18360 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18362 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18363 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18367 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18368 arg2, then arg2 is not as specialized as arg1. */
18374 /* "If, for a given type, deduction succeeds in both directions
18375 (i.e., the types are identical after the transformations above)
18376 and both P and A were reference types (before being replaced with
18377 the type referred to above):
18378 - if the type from the argument template was an lvalue reference and
18379 the type from the parameter template was not, the argument type is
18380 considered to be more specialized than the other; otherwise,
18381 - if the type from the argument template is more cv-qualified
18382 than the type from the parameter template (as described above),
18383 the argument type is considered to be more specialized than the other;
18385 - neither type is more specialized than the other." */
18387 if (deduce1
&& deduce2
)
18389 if (ref1
&& ref2
&& ref1
!= ref2
)
18396 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18398 if ((quals1
& quals2
) == quals2
)
18400 if ((quals1
& quals2
) == quals1
)
18405 if (lose1
&& lose2
)
18406 /* We've failed to deduce something in either direction.
18407 These must be unordered. */
18410 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18411 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18412 /* We have already processed all of the arguments in our
18413 handing of the pack expansion type. */
18416 args1
= TREE_CHAIN (args1
);
18417 args2
= TREE_CHAIN (args2
);
18420 /* "In most cases, all template parameters must have values in order for
18421 deduction to succeed, but for partial ordering purposes a template
18422 parameter may remain without a value provided it is not used in the
18423 types being used for partial ordering."
18425 Thus, if we are missing any of the targs1 we need to substitute into
18426 origs1, then pat2 is not as specialized as pat1. This can happen when
18427 there is a nondeduced context. */
18428 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18430 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18433 processing_template_decl
--;
18435 /* All things being equal, if the next argument is a pack expansion
18436 for one function but not for the other, prefer the
18437 non-variadic function. FIXME this is bogus; see c++/41958. */
18439 && args1
&& TREE_VALUE (args1
)
18440 && args2
&& TREE_VALUE (args2
))
18442 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18443 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18446 if (lose1
== lose2
)
18454 /* Determine which of two partial specializations of TMPL is more
18457 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18458 to the first partial specialization. The TREE_VALUE is the
18459 innermost set of template parameters for the partial
18460 specialization. PAT2 is similar, but for the second template.
18462 Return 1 if the first partial specialization is more specialized;
18463 -1 if the second is more specialized; 0 if neither is more
18466 See [temp.class.order] for information about determining which of
18467 two templates is more specialized. */
18470 more_specialized_class (tree tmpl
, tree pat1
, tree pat2
)
18475 bool any_deductions
= false;
18477 tmpl1
= TREE_TYPE (pat1
);
18478 tmpl2
= TREE_TYPE (pat2
);
18480 /* Just like what happens for functions, if we are ordering between
18481 different class template specializations, we may encounter dependent
18482 types in the arguments, and we need our dependency check functions
18483 to behave correctly. */
18484 ++processing_template_decl
;
18485 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat1
),
18486 CLASSTYPE_TI_ARGS (tmpl1
),
18487 CLASSTYPE_TI_ARGS (tmpl2
));
18491 any_deductions
= true;
18494 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat2
),
18495 CLASSTYPE_TI_ARGS (tmpl2
),
18496 CLASSTYPE_TI_ARGS (tmpl1
));
18500 any_deductions
= true;
18502 --processing_template_decl
;
18504 /* In the case of a tie where at least one of the class templates
18505 has a parameter pack at the end, the template with the most
18506 non-packed parameters wins. */
18509 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18510 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18512 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18513 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18514 int len1
= TREE_VEC_LENGTH (args1
);
18515 int len2
= TREE_VEC_LENGTH (args2
);
18517 /* We don't count the pack expansion at the end. */
18518 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18520 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18525 else if (len1
< len2
)
18532 /* Return the template arguments that will produce the function signature
18533 DECL from the function template FN, with the explicit template
18534 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18535 also match. Return NULL_TREE if no satisfactory arguments could be
18539 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18541 int ntparms
= DECL_NTPARMS (fn
);
18542 tree targs
= make_tree_vec (ntparms
);
18543 tree decl_type
= TREE_TYPE (decl
);
18544 tree decl_arg_types
;
18546 unsigned int nargs
, ix
;
18549 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18551 /* Never do unification on the 'this' parameter. */
18552 decl_arg_types
= skip_artificial_parms_for (decl
,
18553 TYPE_ARG_TYPES (decl_type
));
18555 nargs
= list_length (decl_arg_types
);
18556 args
= XALLOCAVEC (tree
, nargs
);
18557 for (arg
= decl_arg_types
, ix
= 0;
18558 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18559 arg
= TREE_CHAIN (arg
), ++ix
)
18560 args
[ix
] = TREE_VALUE (arg
);
18562 if (fn_type_unification (fn
, explicit_args
, targs
,
18564 (check_rettype
|| DECL_CONV_FN_P (fn
)
18565 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18566 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18568 == error_mark_node
)
18574 /* Return the innermost template arguments that, when applied to a partial
18575 specialization of TMPL whose innermost template parameters are
18576 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18579 For example, suppose we have:
18581 template <class T, class U> struct S {};
18582 template <class T> struct S<T*, int> {};
18584 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18585 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18586 int}. The resulting vector will be {double}, indicating that `T'
18587 is bound to `double'. */
18590 get_class_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
18592 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18594 tree innermost_deduced_args
;
18596 innermost_deduced_args
= make_tree_vec (ntparms
);
18597 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18599 deduced_args
= copy_node (args
);
18600 SET_TMPL_ARGS_LEVEL (deduced_args
,
18601 TMPL_ARGS_DEPTH (deduced_args
),
18602 innermost_deduced_args
);
18605 deduced_args
= innermost_deduced_args
;
18607 if (unify (tparms
, deduced_args
,
18608 INNERMOST_TEMPLATE_ARGS (spec_args
),
18609 INNERMOST_TEMPLATE_ARGS (args
),
18610 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18613 for (i
= 0; i
< ntparms
; ++i
)
18614 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18617 /* Verify that nondeduced template arguments agree with the type
18618 obtained from argument deduction.
18622 struct A { typedef int X; };
18623 template <class T, class U> struct C {};
18624 template <class T> struct C<T, typename T::X> {};
18626 Then with the instantiation `C<A, int>', we can deduce that
18627 `T' is `A' but unify () does not check whether `typename T::X'
18629 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18630 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18632 tf_none
, false, false);
18633 if (spec_args
== error_mark_node
18634 /* We only need to check the innermost arguments; the other
18635 arguments will always agree. */
18636 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18637 INNERMOST_TEMPLATE_ARGS (args
)))
18640 /* Now that we have bindings for all of the template arguments,
18641 ensure that the arguments deduced for the template template
18642 parameters have compatible template parameter lists. See the use
18643 of template_template_parm_bindings_ok_p in fn_type_unification
18644 for more information. */
18645 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18648 return deduced_args
;
18651 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18652 Return the TREE_LIST node with the most specialized template, if
18653 any. If there is no most specialized template, the error_mark_node
18656 Note that this function does not look at, or modify, the
18657 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18658 returned is one of the elements of INSTANTIATIONS, callers may
18659 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18660 and retrieve it from the value returned. */
18663 most_specialized_instantiation (tree templates
)
18667 ++processing_template_decl
;
18670 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18674 if (get_bindings (TREE_VALUE (champ
),
18675 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18676 NULL_TREE
, /*check_ret=*/true))
18679 if (get_bindings (TREE_VALUE (fn
),
18680 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18681 NULL_TREE
, /*check_ret=*/true))
18688 /* Equally specialized, move to next function. If there
18689 is no next function, nothing's most specialized. */
18690 fn
= TREE_CHAIN (fn
);
18698 /* Now verify that champ is better than everything earlier in the
18699 instantiation list. */
18700 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18701 if (get_bindings (TREE_VALUE (champ
),
18702 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18703 NULL_TREE
, /*check_ret=*/true)
18704 || !get_bindings (TREE_VALUE (fn
),
18705 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18706 NULL_TREE
, /*check_ret=*/true))
18712 processing_template_decl
--;
18715 return error_mark_node
;
18720 /* If DECL is a specialization of some template, return the most
18721 general such template. Otherwise, returns NULL_TREE.
18723 For example, given:
18725 template <class T> struct S { template <class U> void f(U); };
18727 if TMPL is `template <class U> void S<int>::f(U)' this will return
18728 the full template. This function will not trace past partial
18729 specializations, however. For example, given in addition:
18731 template <class T> struct S<T*> { template <class U> void f(U); };
18733 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18734 `template <class T> template <class U> S<T*>::f(U)'. */
18737 most_general_template (tree decl
)
18739 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18740 an immediate specialization. */
18741 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18743 if (DECL_TEMPLATE_INFO (decl
)) {
18744 decl
= DECL_TI_TEMPLATE (decl
);
18746 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18747 template friend. */
18748 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18754 /* Look for more and more general templates. */
18755 while (DECL_TEMPLATE_INFO (decl
))
18757 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18758 (See cp-tree.h for details.) */
18759 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18762 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18763 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18766 /* Stop if we run into an explicitly specialized class template. */
18767 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18768 && DECL_CONTEXT (decl
)
18769 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18772 decl
= DECL_TI_TEMPLATE (decl
);
18778 /* Return the most specialized of the class template partial
18779 specializations which can produce TYPE, a specialization of some class
18780 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18781 a _TYPE node corresponding to the partial specialization, while the
18782 TREE_PURPOSE is the set of template arguments that must be
18783 substituted into the TREE_TYPE in order to generate TYPE.
18785 If the choice of partial specialization is ambiguous, a diagnostic
18786 is issued, and the error_mark_node is returned. If there are no
18787 partial specializations matching TYPE, then NULL_TREE is
18788 returned, indicating that the primary template should be used. */
18791 most_specialized_class (tree type
, tsubst_flags_t complain
)
18793 tree list
= NULL_TREE
;
18798 tree outer_args
= NULL_TREE
;
18800 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
18801 tree main_tmpl
= most_general_template (tmpl
);
18802 tree args
= CLASSTYPE_TI_ARGS (type
);
18804 /* For determining which partial specialization to use, only the
18805 innermost args are interesting. */
18806 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18808 outer_args
= strip_innermost_template_args (args
, 1);
18809 args
= INNERMOST_TEMPLATE_ARGS (args
);
18812 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
18814 tree partial_spec_args
;
18816 tree spec_tmpl
= TREE_VALUE (t
);
18817 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18819 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18821 ++processing_template_decl
;
18825 /* Discard the outer levels of args, and then substitute in the
18826 template args from the enclosing class. */
18827 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18828 partial_spec_args
= tsubst_template_args
18829 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18831 /* And the same for the partial specialization TEMPLATE_DECL. */
18832 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18835 partial_spec_args
=
18836 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18839 /*require_all_args=*/true,
18840 /*use_default_args=*/true);
18842 --processing_template_decl
;
18844 if (partial_spec_args
== error_mark_node
)
18845 return error_mark_node
;
18846 if (spec_tmpl
== error_mark_node
)
18847 return error_mark_node
;
18849 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18850 spec_args
= get_class_bindings (tmpl
, parms
,
18856 spec_args
= add_to_template_args (outer_args
, spec_args
);
18857 list
= tree_cons (spec_args
, orig_parms
, list
);
18858 TREE_TYPE (list
) = TREE_TYPE (t
);
18865 ambiguous_p
= false;
18868 t
= TREE_CHAIN (t
);
18869 for (; t
; t
= TREE_CHAIN (t
))
18871 fate
= more_specialized_class (tmpl
, champ
, t
);
18878 t
= TREE_CHAIN (t
);
18881 ambiguous_p
= true;
18890 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18892 fate
= more_specialized_class (tmpl
, champ
, t
);
18895 ambiguous_p
= true;
18903 char *spaces
= NULL
;
18904 if (!(complain
& tf_error
))
18905 return error_mark_node
;
18906 error ("ambiguous class template instantiation for %q#T", type
);
18907 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18908 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18910 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18911 spaces
= spaces
? spaces
: get_spaces (str
);
18914 return error_mark_node
;
18920 /* Explicitly instantiate DECL. */
18923 do_decl_instantiation (tree decl
, tree storage
)
18925 tree result
= NULL_TREE
;
18928 if (!decl
|| decl
== error_mark_node
)
18929 /* An error occurred, for which grokdeclarator has already issued
18930 an appropriate message. */
18932 else if (! DECL_LANG_SPECIFIC (decl
))
18934 error ("explicit instantiation of non-template %q#D", decl
);
18937 else if (VAR_P (decl
))
18939 /* There is an asymmetry here in the way VAR_DECLs and
18940 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18941 the latter, the DECL we get back will be marked as a
18942 template instantiation, and the appropriate
18943 DECL_TEMPLATE_INFO will be set up. This does not happen for
18944 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18945 should handle VAR_DECLs as it currently handles
18947 if (!DECL_CLASS_SCOPE_P (decl
))
18949 error ("%qD is not a static data member of a class template", decl
);
18952 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18953 if (!result
|| !VAR_P (result
))
18955 error ("no matching template for %qD found", decl
);
18958 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18960 error ("type %qT for explicit instantiation %qD does not match "
18961 "declared type %qT", TREE_TYPE (result
), decl
,
18966 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18968 error ("explicit instantiation of %q#D", decl
);
18974 /* Check for various error cases. Note that if the explicit
18975 instantiation is valid the RESULT will currently be marked as an
18976 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18977 until we get here. */
18979 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18981 /* DR 259 [temp.spec].
18983 Both an explicit instantiation and a declaration of an explicit
18984 specialization shall not appear in a program unless the explicit
18985 instantiation follows a declaration of the explicit specialization.
18987 For a given set of template parameters, if an explicit
18988 instantiation of a template appears after a declaration of an
18989 explicit specialization for that template, the explicit
18990 instantiation has no effect. */
18993 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18997 No program shall explicitly instantiate any template more
19000 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19001 the first instantiation was `extern' and the second is not,
19002 and EXTERN_P for the opposite case. */
19003 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
19004 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
19005 /* If an "extern" explicit instantiation follows an ordinary
19006 explicit instantiation, the template is instantiated. */
19010 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
19012 error ("no matching template for %qD found", result
);
19015 else if (!DECL_TEMPLATE_INFO (result
))
19017 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
19021 if (storage
== NULL_TREE
)
19023 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19025 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
19026 pedwarn (input_location
, OPT_Wpedantic
,
19027 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19032 error ("storage class %qD applied to template instantiation", storage
);
19034 check_explicit_instantiation_namespace (result
);
19035 mark_decl_instantiated (result
, extern_p
);
19037 instantiate_decl (result
, /*defer_ok=*/1,
19038 /*expl_inst_class_mem_p=*/false);
19042 mark_class_instantiated (tree t
, int extern_p
)
19044 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
19045 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
19046 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
19047 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
19050 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
19051 rest_of_type_compilation (t
, 1);
19055 /* Called from do_type_instantiation through binding_table_foreach to
19056 do recursive instantiation for the type bound in ENTRY. */
19058 bt_instantiate_type_proc (binding_entry entry
, void *data
)
19060 tree storage
= *(tree
*) data
;
19062 if (MAYBE_CLASS_TYPE_P (entry
->type
)
19063 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
19064 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
19067 /* Called from do_type_instantiation to instantiate a member
19068 (a member function or a static member variable) of an
19069 explicitly instantiated class template. */
19071 instantiate_class_member (tree decl
, int extern_p
)
19073 mark_decl_instantiated (decl
, extern_p
);
19075 instantiate_decl (decl
, /*defer_ok=*/1,
19076 /*expl_inst_class_mem_p=*/true);
19079 /* Perform an explicit instantiation of template class T. STORAGE, if
19080 non-null, is the RID for extern, inline or static. COMPLAIN is
19081 nonzero if this is called from the parser, zero if called recursively,
19082 since the standard is unclear (as detailed below). */
19085 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
19090 int previous_instantiation_extern_p
= 0;
19092 if (TREE_CODE (t
) == TYPE_DECL
)
19095 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
19098 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
19100 error ("explicit instantiation of non-class template %qD", tmpl
);
19102 error ("explicit instantiation of non-template type %qT", t
);
19108 if (!COMPLETE_TYPE_P (t
))
19110 if (complain
& tf_error
)
19111 error ("explicit instantiation of %q#T before definition of template",
19116 if (storage
!= NULL_TREE
)
19118 if (!in_system_header_at (input_location
))
19120 if (storage
== ridpointers
[(int) RID_EXTERN
])
19122 if (cxx_dialect
== cxx98
)
19123 pedwarn (input_location
, OPT_Wpedantic
,
19124 "ISO C++ 1998 forbids the use of %<extern%> on "
19125 "explicit instantiations");
19128 pedwarn (input_location
, OPT_Wpedantic
,
19129 "ISO C++ forbids the use of %qE"
19130 " on explicit instantiations", storage
);
19133 if (storage
== ridpointers
[(int) RID_INLINE
])
19135 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19137 else if (storage
== ridpointers
[(int) RID_STATIC
])
19141 error ("storage class %qD applied to template instantiation",
19147 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
19149 /* DR 259 [temp.spec].
19151 Both an explicit instantiation and a declaration of an explicit
19152 specialization shall not appear in a program unless the explicit
19153 instantiation follows a declaration of the explicit specialization.
19155 For a given set of template parameters, if an explicit
19156 instantiation of a template appears after a declaration of an
19157 explicit specialization for that template, the explicit
19158 instantiation has no effect. */
19161 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
19165 No program shall explicitly instantiate any template more
19168 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19169 instantiation was `extern'. If EXTERN_P then the second is.
19170 These cases are OK. */
19171 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
19173 if (!previous_instantiation_extern_p
&& !extern_p
19174 && (complain
& tf_error
))
19175 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
19177 /* If we've already instantiated the template, just return now. */
19178 if (!CLASSTYPE_INTERFACE_ONLY (t
))
19182 check_explicit_instantiation_namespace (TYPE_NAME (t
));
19183 mark_class_instantiated (t
, extern_p
);
19191 /* In contrast to implicit instantiation, where only the
19192 declarations, and not the definitions, of members are
19193 instantiated, we have here:
19197 The explicit instantiation of a class template specialization
19198 implies the instantiation of all of its members not
19199 previously explicitly specialized in the translation unit
19200 containing the explicit instantiation.
19202 Of course, we can't instantiate member template classes, since
19203 we don't have any arguments for them. Note that the standard
19204 is unclear on whether the instantiation of the members are
19205 *explicit* instantiations or not. However, the most natural
19206 interpretation is that it should be an explicit instantiation. */
19209 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19210 if (TREE_CODE (tmp
) == FUNCTION_DECL
19211 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19212 instantiate_class_member (tmp
, extern_p
);
19214 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19215 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19216 instantiate_class_member (tmp
, extern_p
);
19218 if (CLASSTYPE_NESTED_UTDS (t
))
19219 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19220 bt_instantiate_type_proc
, &storage
);
19224 /* Given a function DECL, which is a specialization of TMPL, modify
19225 DECL to be a re-instantiation of TMPL with the same template
19226 arguments. TMPL should be the template into which tsubst'ing
19227 should occur for DECL, not the most general template.
19229 One reason for doing this is a scenario like this:
19232 void f(const T&, int i);
19234 void g() { f(3, 7); }
19237 void f(const T& t, const int i) { }
19239 Note that when the template is first instantiated, with
19240 instantiate_template, the resulting DECL will have no name for the
19241 first parameter, and the wrong type for the second. So, when we go
19242 to instantiate the DECL, we regenerate it. */
19245 regenerate_decl_from_template (tree decl
, tree tmpl
)
19247 /* The arguments used to instantiate DECL, from the most general
19252 args
= DECL_TI_ARGS (decl
);
19253 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19255 /* Make sure that we can see identifiers, and compute access
19257 push_access_scope (decl
);
19259 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19267 args_depth
= TMPL_ARGS_DEPTH (args
);
19268 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19269 if (args_depth
> parms_depth
)
19270 args
= get_innermost_template_args (args
, parms_depth
);
19272 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19273 args
, tf_error
, NULL_TREE
,
19274 /*defer_ok*/false);
19275 if (specs
&& specs
!= error_mark_node
)
19276 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19279 /* Merge parameter declarations. */
19280 decl_parm
= skip_artificial_parms_for (decl
,
19281 DECL_ARGUMENTS (decl
));
19283 = skip_artificial_parms_for (code_pattern
,
19284 DECL_ARGUMENTS (code_pattern
));
19285 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19290 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19291 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19292 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19294 parm_type
= type_decays_to (parm_type
);
19295 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19296 TREE_TYPE (decl_parm
) = parm_type
;
19297 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19298 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19300 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19301 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19303 decl_parm
= DECL_CHAIN (decl_parm
);
19304 pattern_parm
= DECL_CHAIN (pattern_parm
);
19306 /* Merge any parameters that match with the function parameter
19308 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19311 tree expanded_types
;
19312 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19313 the parameters in this function parameter pack. */
19314 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19315 args
, tf_error
, NULL_TREE
);
19316 len
= TREE_VEC_LENGTH (expanded_types
);
19317 for (i
= 0; i
< len
; i
++)
19322 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19323 /* Rename the parameter to include the index. */
19324 DECL_NAME (decl_parm
) =
19325 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19326 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19327 parm_type
= type_decays_to (parm_type
);
19328 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19329 TREE_TYPE (decl_parm
) = parm_type
;
19330 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19331 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19333 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19334 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19336 decl_parm
= DECL_CHAIN (decl_parm
);
19339 /* Merge additional specifiers from the CODE_PATTERN. */
19340 if (DECL_DECLARED_INLINE_P (code_pattern
)
19341 && !DECL_DECLARED_INLINE_P (decl
))
19342 DECL_DECLARED_INLINE_P (decl
) = 1;
19344 else if (VAR_P (decl
))
19346 DECL_INITIAL (decl
) =
19347 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19348 tf_error
, DECL_TI_TEMPLATE (decl
),
19349 /*integral_constant_expression_p=*/false);
19350 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19351 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19352 tf_error
, DECL_TI_TEMPLATE (decl
));
19355 gcc_unreachable ();
19357 pop_access_scope (decl
);
19360 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19361 substituted to get DECL. */
19364 template_for_substitution (tree decl
)
19366 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19368 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19369 for the instantiation. This is not always the most general
19370 template. Consider, for example:
19373 struct S { template <class U> void f();
19374 template <> void f<int>(); };
19376 and an instantiation of S<double>::f<int>. We want TD to be the
19377 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19378 while (/* An instantiation cannot have a definition, so we need a
19379 more general template. */
19380 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19381 /* We must also deal with friend templates. Given:
19383 template <class T> struct S {
19384 template <class U> friend void f() {};
19387 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19388 so far as the language is concerned, but that's still
19389 where we get the pattern for the instantiation from. On
19390 other hand, if the definition comes outside the class, say:
19392 template <class T> struct S {
19393 template <class U> friend void f();
19395 template <class U> friend void f() {}
19397 we don't need to look any further. That's what the check for
19398 DECL_INITIAL is for. */
19399 || (TREE_CODE (decl
) == FUNCTION_DECL
19400 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19401 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19403 /* The present template, TD, should not be a definition. If it
19404 were a definition, we should be using it! Note that we
19405 cannot restructure the loop to just keep going until we find
19406 a template with a definition, since that might go too far if
19407 a specialization was declared, but not defined. */
19408 gcc_assert (!VAR_P (decl
)
19409 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19411 /* Fetch the more general template. */
19412 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19418 /* Returns true if we need to instantiate this template instance even if we
19419 know we aren't going to emit it.. */
19422 always_instantiate_p (tree decl
)
19424 /* We always instantiate inline functions so that we can inline them. An
19425 explicit instantiation declaration prohibits implicit instantiation of
19426 non-inline functions. With high levels of optimization, we would
19427 normally inline non-inline functions -- but we're not allowed to do
19428 that for "extern template" functions. Therefore, we check
19429 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19430 return ((TREE_CODE (decl
) == FUNCTION_DECL
19431 && (DECL_DECLARED_INLINE_P (decl
)
19432 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19433 /* And we need to instantiate static data members so that
19434 their initializers are available in integral constant
19437 && decl_maybe_constant_var_p (decl
)));
19440 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19441 instantiate it now, modifying TREE_TYPE (fn). */
19444 maybe_instantiate_noexcept (tree fn
)
19446 tree fntype
, spec
, noex
, clone
;
19448 /* Don't instantiate a noexcept-specification from template context. */
19449 if (processing_template_decl
)
19452 if (DECL_CLONED_FUNCTION_P (fn
))
19453 fn
= DECL_CLONED_FUNCTION (fn
);
19454 fntype
= TREE_TYPE (fn
);
19455 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19457 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19460 noex
= TREE_PURPOSE (spec
);
19462 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19464 if (push_tinst_level (fn
))
19466 push_access_scope (fn
);
19467 push_deferring_access_checks (dk_no_deferred
);
19468 input_location
= DECL_SOURCE_LOCATION (fn
);
19469 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19470 DEFERRED_NOEXCEPT_ARGS (noex
),
19471 tf_warning_or_error
, fn
,
19472 /*function_p=*/false,
19473 /*integral_constant_expression_p=*/true);
19474 pop_deferring_access_checks ();
19475 pop_access_scope (fn
);
19476 pop_tinst_level ();
19477 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19478 if (spec
== error_mark_node
)
19479 spec
= noexcept_false_spec
;
19482 spec
= noexcept_false_spec
;
19486 /* This is an implicitly declared function, so NOEX is a list of
19487 other functions to evaluate and merge. */
19489 spec
= noexcept_true_spec
;
19490 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19492 tree fn
= OVL_CURRENT (elt
);
19494 maybe_instantiate_noexcept (fn
);
19495 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19496 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19500 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19502 FOR_EACH_CLONE (clone
, fn
)
19504 if (TREE_TYPE (clone
) == fntype
)
19505 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19507 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19511 /* Produce the definition of D, a _DECL generated from a template. If
19512 DEFER_OK is nonzero, then we don't have to actually do the
19513 instantiation now; we just have to do it sometime. Normally it is
19514 an error if this is an explicit instantiation but D is undefined.
19515 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19516 explicitly instantiated class template. */
19519 instantiate_decl (tree d
, int defer_ok
,
19520 bool expl_inst_class_mem_p
)
19522 tree tmpl
= DECL_TI_TEMPLATE (d
);
19529 bool pattern_defined
;
19530 location_t saved_loc
= input_location
;
19531 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19532 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19537 /* This function should only be used to instantiate templates for
19538 functions and static member variables. */
19539 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19541 /* Variables are never deferred; if instantiation is required, they
19542 are instantiated right away. That allows for better code in the
19543 case that an expression refers to the value of the variable --
19544 if the variable has a constant value the referring expression can
19545 take advantage of that fact. */
19547 || DECL_DECLARED_CONSTEXPR_P (d
))
19550 /* Don't instantiate cloned functions. Instead, instantiate the
19551 functions they cloned. */
19552 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19553 d
= DECL_CLONED_FUNCTION (d
);
19555 if (DECL_TEMPLATE_INSTANTIATED (d
)
19556 || (TREE_CODE (d
) == FUNCTION_DECL
19557 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19558 || DECL_TEMPLATE_SPECIALIZATION (d
))
19559 /* D has already been instantiated or explicitly specialized, so
19560 there's nothing for us to do here.
19562 It might seem reasonable to check whether or not D is an explicit
19563 instantiation, and, if so, stop here. But when an explicit
19564 instantiation is deferred until the end of the compilation,
19565 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19566 the instantiation. */
19569 /* Check to see whether we know that this template will be
19570 instantiated in some other file, as with "extern template"
19572 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19574 /* In general, we do not instantiate such templates. */
19575 if (external_p
&& !always_instantiate_p (d
))
19578 gen_tmpl
= most_general_template (tmpl
);
19579 gen_args
= DECL_TI_ARGS (d
);
19581 if (tmpl
!= gen_tmpl
)
19582 /* We should already have the extra args. */
19583 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19584 == TMPL_ARGS_DEPTH (gen_args
));
19585 /* And what's in the hash table should match D. */
19586 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19587 || spec
== NULL_TREE
);
19589 /* This needs to happen before any tsubsting. */
19590 if (! push_tinst_level (d
))
19593 timevar_push (TV_TEMPLATE_INST
);
19595 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19596 for the instantiation. */
19597 td
= template_for_substitution (d
);
19598 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19600 /* We should never be trying to instantiate a member of a class
19601 template or partial specialization. */
19602 gcc_assert (d
!= code_pattern
);
19604 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19605 || DECL_TEMPLATE_SPECIALIZATION (td
))
19606 /* In the case of a friend template whose definition is provided
19607 outside the class, we may have too many arguments. Drop the
19608 ones we don't need. The same is true for specializations. */
19609 args
= get_innermost_template_args
19610 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19614 if (TREE_CODE (d
) == FUNCTION_DECL
)
19615 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19616 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19618 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19620 /* We may be in the middle of deferred access check. Disable it now. */
19621 push_deferring_access_checks (dk_no_deferred
);
19623 /* Unless an explicit instantiation directive has already determined
19624 the linkage of D, remember that a definition is available for
19626 if (pattern_defined
19627 && !DECL_INTERFACE_KNOWN (d
)
19628 && !DECL_NOT_REALLY_EXTERN (d
))
19629 mark_definable (d
);
19631 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19632 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19633 input_location
= DECL_SOURCE_LOCATION (d
);
19635 /* If D is a member of an explicitly instantiated class template,
19636 and no definition is available, treat it like an implicit
19638 if (!pattern_defined
&& expl_inst_class_mem_p
19639 && DECL_EXPLICIT_INSTANTIATION (d
))
19641 /* Leave linkage flags alone on instantiations with anonymous
19643 if (TREE_PUBLIC (d
))
19645 DECL_NOT_REALLY_EXTERN (d
) = 0;
19646 DECL_INTERFACE_KNOWN (d
) = 0;
19648 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19651 if (TREE_CODE (d
) == FUNCTION_DECL
)
19652 maybe_instantiate_noexcept (d
);
19654 /* Defer all other templates, unless we have been explicitly
19655 forbidden from doing so. */
19656 if (/* If there is no definition, we cannot instantiate the
19659 /* If it's OK to postpone instantiation, do so. */
19661 /* If this is a static data member that will be defined
19662 elsewhere, we don't want to instantiate the entire data
19663 member, but we do want to instantiate the initializer so that
19664 we can substitute that elsewhere. */
19665 || (external_p
&& VAR_P (d
)))
19667 /* The definition of the static data member is now required so
19668 we must substitute the initializer. */
19670 && !DECL_INITIAL (d
)
19671 && DECL_INITIAL (code_pattern
))
19675 bool const_init
= false;
19677 ns
= decl_namespace_context (d
);
19678 push_nested_namespace (ns
);
19679 push_nested_class (DECL_CONTEXT (d
));
19680 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19682 tf_warning_or_error
, NULL_TREE
,
19683 /*integral_constant_expression_p=*/false);
19684 /* Make sure the initializer is still constant, in case of
19685 circular dependency (template/instantiate6.C). */
19687 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19688 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19689 /*asmspec_tree=*/NULL_TREE
,
19690 LOOKUP_ONLYCONVERTING
);
19691 pop_nested_class ();
19692 pop_nested_namespace (ns
);
19695 /* We restore the source position here because it's used by
19696 add_pending_template. */
19697 input_location
= saved_loc
;
19699 if (at_eof
&& !pattern_defined
19700 && DECL_EXPLICIT_INSTANTIATION (d
)
19701 && DECL_NOT_REALLY_EXTERN (d
))
19704 The definition of a non-exported function template, a
19705 non-exported member function template, or a non-exported
19706 member function or static data member of a class template
19707 shall be present in every translation unit in which it is
19708 explicitly instantiated. */
19709 permerror (input_location
, "explicit instantiation of %qD "
19710 "but no definition available", d
);
19712 /* If we're in unevaluated context, we just wanted to get the
19713 constant value; this isn't an odr use, so don't queue
19714 a full instantiation. */
19715 if (cp_unevaluated_operand
!= 0)
19717 /* ??? Historically, we have instantiated inline functions, even
19718 when marked as "extern template". */
19719 if (!(external_p
&& VAR_P (d
)))
19720 add_pending_template (d
);
19723 /* Tell the repository that D is available in this translation unit
19724 -- and see if it is supposed to be instantiated here. */
19725 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19727 /* In a PCH file, despite the fact that the repository hasn't
19728 requested instantiation in the PCH it is still possible that
19729 an instantiation will be required in a file that includes the
19732 add_pending_template (d
);
19733 /* Instantiate inline functions so that the inliner can do its
19734 job, even though we'll not be emitting a copy of this
19736 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19740 fn_context
= decl_function_context (d
);
19741 nested
= (current_function_decl
!= NULL_TREE
);
19743 push_to_top_level ();
19747 push_function_context ();
19748 cp_unevaluated_operand
= 0;
19749 c_inhibit_evaluation_warnings
= 0;
19752 /* Mark D as instantiated so that recursive calls to
19753 instantiate_decl do not try to instantiate it again. */
19754 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19756 /* Regenerate the declaration in case the template has been modified
19757 by a subsequent redeclaration. */
19758 regenerate_decl_from_template (d
, td
);
19760 /* We already set the file and line above. Reset them now in case
19761 they changed as a result of calling regenerate_decl_from_template. */
19762 input_location
= DECL_SOURCE_LOCATION (d
);
19767 bool const_init
= false;
19769 /* Clear out DECL_RTL; whatever was there before may not be right
19770 since we've reset the type of the declaration. */
19771 SET_DECL_RTL (d
, NULL
);
19772 DECL_IN_AGGR_P (d
) = 0;
19774 /* The initializer is placed in DECL_INITIAL by
19775 regenerate_decl_from_template so we don't need to
19776 push/pop_access_scope again here. Pull it out so that
19777 cp_finish_decl can process it. */
19778 init
= DECL_INITIAL (d
);
19779 DECL_INITIAL (d
) = NULL_TREE
;
19780 DECL_INITIALIZED_P (d
) = 0;
19782 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19783 initializer. That function will defer actual emission until
19784 we have a chance to determine linkage. */
19785 DECL_EXTERNAL (d
) = 0;
19787 /* Enter the scope of D so that access-checking works correctly. */
19788 push_nested_class (DECL_CONTEXT (d
));
19789 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19790 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19791 pop_nested_class ();
19793 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19794 synthesize_method (d
);
19795 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19797 struct pointer_map_t
*saved_local_specializations
;
19801 tree block
= NULL_TREE
;
19803 /* Save away the current list, in case we are instantiating one
19804 template from within the body of another. */
19805 saved_local_specializations
= local_specializations
;
19807 /* Set up the list of local specializations. */
19808 local_specializations
= pointer_map_create ();
19810 /* Set up context. */
19811 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19812 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19813 block
= push_stmt_list ();
19815 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19817 /* Some typedefs referenced from within the template code need to be
19818 access checked at template instantiation time, i.e now. These
19819 types were added to the template at parsing time. Let's get those
19820 and perform the access checks then. */
19821 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19824 /* Create substitution entries for the parameters. */
19825 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19826 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19827 spec_parm
= DECL_ARGUMENTS (d
);
19828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19830 register_local_specialization (spec_parm
, tmpl_parm
);
19831 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19832 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19834 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19836 if (!DECL_PACK_P (tmpl_parm
))
19838 register_local_specialization (spec_parm
, tmpl_parm
);
19839 spec_parm
= DECL_CHAIN (spec_parm
);
19843 /* Register the (value) argument pack as a specialization of
19844 TMPL_PARM, then move on. */
19845 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19846 register_local_specialization (argpack
, tmpl_parm
);
19849 gcc_assert (!spec_parm
);
19851 /* Substitute into the body of the function. */
19852 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19853 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19854 tf_warning_or_error
, tmpl
);
19857 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19858 tf_warning_or_error
, tmpl
,
19859 /*integral_constant_expression_p=*/false);
19861 /* Set the current input_location to the end of the function
19862 so that finish_function knows where we are. */
19864 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19866 /* Remember if we saw an infinite loop in the template. */
19867 current_function_infinite_loop
19868 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
19871 /* We don't need the local specializations any more. */
19872 pointer_map_destroy (local_specializations
);
19873 local_specializations
= saved_local_specializations
;
19875 /* Finish the function. */
19876 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19877 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19878 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19881 d
= finish_function (0);
19882 expand_or_defer_fn (d
);
19885 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19886 cp_check_omp_declare_reduction (d
);
19889 /* We're not deferring instantiation any more. */
19890 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19893 pop_from_top_level ();
19895 pop_function_context ();
19898 input_location
= saved_loc
;
19899 cp_unevaluated_operand
= saved_unevaluated_operand
;
19900 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19901 pop_deferring_access_checks ();
19902 pop_tinst_level ();
19904 timevar_pop (TV_TEMPLATE_INST
);
19909 /* Run through the list of templates that we wish we could
19910 instantiate, and instantiate any we can. RETRIES is the
19911 number of times we retry pending template instantiation. */
19914 instantiate_pending_templates (int retries
)
19917 location_t saved_loc
= input_location
;
19919 /* Instantiating templates may trigger vtable generation. This in turn
19920 may require further template instantiations. We place a limit here
19921 to avoid infinite loop. */
19922 if (pending_templates
&& retries
>= max_tinst_depth
)
19924 tree decl
= pending_templates
->tinst
->decl
;
19926 error ("template instantiation depth exceeds maximum of %d"
19927 " instantiating %q+D, possibly from virtual table generation"
19928 " (use -ftemplate-depth= to increase the maximum)",
19929 max_tinst_depth
, decl
);
19930 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19931 /* Pretend that we defined it. */
19932 DECL_INITIAL (decl
) = error_mark_node
;
19938 struct pending_template
**t
= &pending_templates
;
19939 struct pending_template
*last
= NULL
;
19943 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19944 bool complete
= false;
19946 if (TYPE_P (instantiation
))
19950 if (!COMPLETE_TYPE_P (instantiation
))
19952 instantiate_class_template (instantiation
);
19953 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19954 for (fn
= TYPE_METHODS (instantiation
);
19956 fn
= TREE_CHAIN (fn
))
19957 if (! DECL_ARTIFICIAL (fn
))
19958 instantiate_decl (fn
,
19960 /*expl_inst_class_mem_p=*/false);
19961 if (COMPLETE_TYPE_P (instantiation
))
19965 complete
= COMPLETE_TYPE_P (instantiation
);
19969 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19970 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19973 = instantiate_decl (instantiation
,
19975 /*expl_inst_class_mem_p=*/false);
19976 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19980 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19981 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19985 /* If INSTANTIATION has been instantiated, then we don't
19986 need to consider it again in the future. */
19994 current_tinst_level
= NULL
;
19996 last_pending_template
= last
;
19998 while (reconsider
);
20000 input_location
= saved_loc
;
20003 /* Substitute ARGVEC into T, which is a list of initializers for
20004 either base class or a non-static data member. The TREE_PURPOSEs
20005 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20006 instantiate_decl. */
20009 tsubst_initializer_list (tree t
, tree argvec
)
20011 tree inits
= NULL_TREE
;
20013 for (; t
; t
= TREE_CHAIN (t
))
20017 tree expanded_bases
= NULL_TREE
;
20018 tree expanded_arguments
= NULL_TREE
;
20021 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
20026 /* Expand the base class expansion type into separate base
20028 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
20029 tf_warning_or_error
,
20031 if (expanded_bases
== error_mark_node
)
20034 /* We'll be building separate TREE_LISTs of arguments for
20036 len
= TREE_VEC_LENGTH (expanded_bases
);
20037 expanded_arguments
= make_tree_vec (len
);
20038 for (i
= 0; i
< len
; i
++)
20039 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
20041 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20042 expand each argument in the TREE_VALUE of t. */
20043 expr
= make_node (EXPR_PACK_EXPANSION
);
20044 PACK_EXPANSION_LOCAL_P (expr
) = true;
20045 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
20046 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
20048 if (TREE_VALUE (t
) == void_type_node
)
20049 /* VOID_TYPE_NODE is used to indicate
20050 value-initialization. */
20052 for (i
= 0; i
< len
; i
++)
20053 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
20057 /* Substitute parameter packs into each argument in the
20059 in_base_initializer
= 1;
20060 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
20062 tree expanded_exprs
;
20064 /* Expand the argument. */
20065 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
20067 = tsubst_pack_expansion (expr
, argvec
,
20068 tf_warning_or_error
,
20070 if (expanded_exprs
== error_mark_node
)
20073 /* Prepend each of the expanded expressions to the
20074 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20075 for (i
= 0; i
< len
; i
++)
20077 TREE_VEC_ELT (expanded_arguments
, i
) =
20078 tree_cons (NULL_TREE
,
20079 TREE_VEC_ELT (expanded_exprs
, i
),
20080 TREE_VEC_ELT (expanded_arguments
, i
));
20083 in_base_initializer
= 0;
20085 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20086 since we built them backwards. */
20087 for (i
= 0; i
< len
; i
++)
20089 TREE_VEC_ELT (expanded_arguments
, i
) =
20090 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
20095 for (i
= 0; i
< len
; ++i
)
20097 if (expanded_bases
)
20099 decl
= TREE_VEC_ELT (expanded_bases
, i
);
20100 decl
= expand_member_init (decl
);
20101 init
= TREE_VEC_ELT (expanded_arguments
, i
);
20106 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
20107 tf_warning_or_error
, NULL_TREE
);
20109 decl
= expand_member_init (decl
);
20110 if (decl
&& !DECL_P (decl
))
20111 in_base_initializer
= 1;
20113 init
= TREE_VALUE (t
);
20115 if (init
!= void_type_node
)
20116 init
= tsubst_expr (init
, argvec
,
20117 tf_warning_or_error
, NULL_TREE
,
20118 /*integral_constant_expression_p=*/false);
20119 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
20120 /* If we had an initializer but it instantiated to nothing,
20121 value-initialize the object. This will only occur when
20122 the initializer was a pack expansion where the parameter
20123 packs used in that expansion were of length zero. */
20124 init
= void_type_node
;
20125 in_base_initializer
= 0;
20130 init
= build_tree_list (decl
, init
);
20131 TREE_CHAIN (init
) = inits
;
20139 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20142 set_current_access_from_decl (tree decl
)
20144 if (TREE_PRIVATE (decl
))
20145 current_access_specifier
= access_private_node
;
20146 else if (TREE_PROTECTED (decl
))
20147 current_access_specifier
= access_protected_node
;
20149 current_access_specifier
= access_public_node
;
20152 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20153 is the instantiation (which should have been created with
20154 start_enum) and ARGS are the template arguments to use. */
20157 tsubst_enum (tree tag
, tree newtag
, tree args
)
20161 if (SCOPED_ENUM_P (newtag
))
20162 begin_scope (sk_scoped_enum
, newtag
);
20164 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
20169 decl
= TREE_VALUE (e
);
20170 /* Note that in a template enum, the TREE_VALUE is the
20171 CONST_DECL, not the corresponding INTEGER_CST. */
20172 value
= tsubst_expr (DECL_INITIAL (decl
),
20173 args
, tf_warning_or_error
, NULL_TREE
,
20174 /*integral_constant_expression_p=*/true);
20176 /* Give this enumeration constant the correct access. */
20177 set_current_access_from_decl (decl
);
20179 /* Actually build the enumerator itself. */
20181 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
20184 if (SCOPED_ENUM_P (newtag
))
20187 finish_enum_value_list (newtag
);
20188 finish_enum (newtag
);
20190 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
20191 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
20194 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20195 its type -- but without substituting the innermost set of template
20196 arguments. So, innermost set of template parameters will appear in
20200 get_mostly_instantiated_function_type (tree decl
)
20208 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
20209 targs
= DECL_TI_ARGS (decl
);
20210 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
20211 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
20213 /* There should be as many levels of arguments as there are levels
20215 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
20217 fn_type
= TREE_TYPE (tmpl
);
20219 if (parm_depth
== 1)
20220 /* No substitution is necessary. */
20227 /* Replace the innermost level of the TARGS with NULL_TREEs to
20228 let tsubst know not to substitute for those parameters. */
20229 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20230 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20231 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20232 TMPL_ARGS_LEVEL (targs
, i
));
20233 SET_TMPL_ARGS_LEVEL (partial_args
,
20234 TMPL_ARGS_DEPTH (targs
),
20235 make_tree_vec (DECL_NTPARMS (tmpl
)));
20237 /* Make sure that we can see identifiers, and compute access
20239 push_access_scope (decl
);
20241 ++processing_template_decl
;
20242 /* Now, do the (partial) substitution to figure out the
20243 appropriate function type. */
20244 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20245 --processing_template_decl
;
20247 /* Substitute into the template parameters to obtain the real
20248 innermost set of parameters. This step is important if the
20249 innermost set of template parameters contains value
20250 parameters whose types depend on outer template parameters. */
20251 TREE_VEC_LENGTH (partial_args
)--;
20252 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20254 pop_access_scope (decl
);
20260 /* Return truthvalue if we're processing a template different from
20261 the last one involved in diagnostics. */
20263 problematic_instantiation_changed (void)
20265 return current_tinst_level
!= last_error_tinst_level
;
20268 /* Remember current template involved in diagnostics. */
20270 record_last_problematic_instantiation (void)
20272 last_error_tinst_level
= current_tinst_level
;
20275 struct tinst_level
*
20276 current_instantiation (void)
20278 return current_tinst_level
;
20281 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20282 type. Return zero for ok, nonzero for disallowed. Issue error and
20283 warning messages under control of COMPLAIN. */
20286 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20288 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20290 else if (POINTER_TYPE_P (type
))
20292 else if (TYPE_PTRMEM_P (type
))
20294 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20296 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20298 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20300 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20303 if (complain
& tf_error
)
20305 if (type
== error_mark_node
)
20306 inform (input_location
, "invalid template non-type parameter");
20308 error ("%q#T is not a valid type for a template non-type parameter",
20314 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20315 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20318 dependent_type_p_r (tree type
)
20324 A type is dependent if it is:
20326 -- a template parameter. Template template parameters are types
20327 for us (since TYPE_P holds true for them) so we handle
20329 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20330 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20332 /* -- a qualified-id with a nested-name-specifier which contains a
20333 class-name that names a dependent type or whose unqualified-id
20334 names a dependent type. */
20335 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20337 /* -- a cv-qualified type where the cv-unqualified type is
20339 type
= TYPE_MAIN_VARIANT (type
);
20340 /* -- a compound type constructed from any dependent type. */
20341 if (TYPE_PTRMEM_P (type
))
20342 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20343 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20345 else if (TYPE_PTR_P (type
)
20346 || TREE_CODE (type
) == REFERENCE_TYPE
)
20347 return dependent_type_p (TREE_TYPE (type
));
20348 else if (TREE_CODE (type
) == FUNCTION_TYPE
20349 || TREE_CODE (type
) == METHOD_TYPE
)
20353 if (dependent_type_p (TREE_TYPE (type
)))
20355 for (arg_type
= TYPE_ARG_TYPES (type
);
20357 arg_type
= TREE_CHAIN (arg_type
))
20358 if (dependent_type_p (TREE_VALUE (arg_type
)))
20362 /* -- an array type constructed from any dependent type or whose
20363 size is specified by a constant expression that is
20366 We checked for type- and value-dependence of the bounds in
20367 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20368 if (TREE_CODE (type
) == ARRAY_TYPE
)
20370 if (TYPE_DOMAIN (type
)
20371 && dependent_type_p (TYPE_DOMAIN (type
)))
20373 return dependent_type_p (TREE_TYPE (type
));
20376 /* -- a template-id in which either the template name is a template
20378 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20380 /* ... or any of the template arguments is a dependent type or
20381 an expression that is type-dependent or value-dependent. */
20382 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20383 && (any_dependent_template_arguments_p
20384 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20387 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20388 dependent; if the argument of the `typeof' expression is not
20389 type-dependent, then it should already been have resolved. */
20390 if (TREE_CODE (type
) == TYPEOF_TYPE
20391 || TREE_CODE (type
) == DECLTYPE_TYPE
20392 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20395 /* A template argument pack is dependent if any of its packed
20397 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20399 tree args
= ARGUMENT_PACK_ARGS (type
);
20400 int i
, len
= TREE_VEC_LENGTH (args
);
20401 for (i
= 0; i
< len
; ++i
)
20402 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20406 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20407 be template parameters. */
20408 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20411 /* The standard does not specifically mention types that are local
20412 to template functions or local classes, but they should be
20413 considered dependent too. For example:
20415 template <int I> void f() {
20420 The size of `E' cannot be known until the value of `I' has been
20421 determined. Therefore, `E' must be considered dependent. */
20422 scope
= TYPE_CONTEXT (type
);
20423 if (scope
&& TYPE_P (scope
))
20424 return dependent_type_p (scope
);
20425 /* Don't use type_dependent_expression_p here, as it can lead
20426 to infinite recursion trying to determine whether a lambda
20427 nested in a lambda is dependent (c++/47687). */
20428 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20429 && DECL_LANG_SPECIFIC (scope
)
20430 && DECL_TEMPLATE_INFO (scope
)
20431 && (any_dependent_template_arguments_p
20432 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20435 /* Other types are non-dependent. */
20439 /* Returns TRUE if TYPE is dependent, in the sense of
20440 [temp.dep.type]. Note that a NULL type is considered dependent. */
20443 dependent_type_p (tree type
)
20445 /* If there are no template parameters in scope, then there can't be
20446 any dependent types. */
20447 if (!processing_template_decl
)
20449 /* If we are not processing a template, then nobody should be
20450 providing us with a dependent type. */
20452 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20456 /* If the type is NULL, we have not computed a type for the entity
20457 in question; in that case, the type is dependent. */
20461 /* Erroneous types can be considered non-dependent. */
20462 if (type
== error_mark_node
)
20465 /* If we have not already computed the appropriate value for TYPE,
20467 if (!TYPE_DEPENDENT_P_VALID (type
))
20469 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20470 TYPE_DEPENDENT_P_VALID (type
) = 1;
20473 return TYPE_DEPENDENT_P (type
);
20476 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20477 lookup. In other words, a dependent type that is not the current
20481 dependent_scope_p (tree scope
)
20483 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20484 && !currently_open_class (scope
));
20487 /* T is a SCOPE_REF; return whether we need to consider it
20488 instantiation-dependent so that we can check access at instantiation
20489 time even though we know which member it resolves to. */
20492 instantiation_dependent_scope_ref_p (tree t
)
20494 if (DECL_P (TREE_OPERAND (t
, 1))
20495 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20496 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20497 TREE_OPERAND (t
, 1)))
20503 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20504 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20507 /* Note that this predicate is not appropriate for general expressions;
20508 only constant expressions (that satisfy potential_constant_expression)
20509 can be tested for value dependence. */
20512 value_dependent_expression_p (tree expression
)
20514 if (!processing_template_decl
)
20517 /* A name declared with a dependent type. */
20518 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20521 switch (TREE_CODE (expression
))
20523 case IDENTIFIER_NODE
:
20524 /* A name that has not been looked up -- must be dependent. */
20527 case TEMPLATE_PARM_INDEX
:
20528 /* A non-type template parm. */
20532 /* A non-type template parm. */
20533 if (DECL_TEMPLATE_PARM_P (expression
))
20535 return value_dependent_expression_p (DECL_INITIAL (expression
));
20538 /* A constant with literal type and is initialized
20539 with an expression that is value-dependent.
20541 Note that a non-dependent parenthesized initializer will have
20542 already been replaced with its constant value, so if we see
20543 a TREE_LIST it must be dependent. */
20544 if (DECL_INITIAL (expression
)
20545 && decl_constant_var_p (expression
)
20546 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20547 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20551 case DYNAMIC_CAST_EXPR
:
20552 case STATIC_CAST_EXPR
:
20553 case CONST_CAST_EXPR
:
20554 case REINTERPRET_CAST_EXPR
:
20556 /* These expressions are value-dependent if the type to which
20557 the cast occurs is dependent or the expression being casted
20558 is value-dependent. */
20560 tree type
= TREE_TYPE (expression
);
20562 if (dependent_type_p (type
))
20565 /* A functional cast has a list of operands. */
20566 expression
= TREE_OPERAND (expression
, 0);
20569 /* If there are no operands, it must be an expression such
20570 as "int()". This should not happen for aggregate types
20571 because it would form non-constant expressions. */
20572 gcc_assert (cxx_dialect
>= cxx11
20573 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20578 if (TREE_CODE (expression
) == TREE_LIST
)
20579 return any_value_dependent_elements_p (expression
);
20581 return value_dependent_expression_p (expression
);
20585 if (SIZEOF_EXPR_TYPE_P (expression
))
20586 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20590 /* A `sizeof' expression is value-dependent if the operand is
20591 type-dependent or is a pack expansion. */
20592 expression
= TREE_OPERAND (expression
, 0);
20593 if (PACK_EXPANSION_P (expression
))
20595 else if (TYPE_P (expression
))
20596 return dependent_type_p (expression
);
20597 return instantiation_dependent_expression_p (expression
);
20599 case AT_ENCODE_EXPR
:
20600 /* An 'encode' expression is value-dependent if the operand is
20602 expression
= TREE_OPERAND (expression
, 0);
20603 return dependent_type_p (expression
);
20605 case NOEXCEPT_EXPR
:
20606 expression
= TREE_OPERAND (expression
, 0);
20607 return instantiation_dependent_expression_p (expression
);
20610 /* All instantiation-dependent expressions should also be considered
20611 value-dependent. */
20612 return instantiation_dependent_scope_ref_p (expression
);
20614 case COMPONENT_REF
:
20615 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20616 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20618 case NONTYPE_ARGUMENT_PACK
:
20619 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20620 is value-dependent. */
20622 tree values
= ARGUMENT_PACK_ARGS (expression
);
20623 int i
, len
= TREE_VEC_LENGTH (values
);
20625 for (i
= 0; i
< len
; ++i
)
20626 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20634 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20635 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20636 || (type2
? dependent_type_p (type2
) : false));
20640 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20641 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20644 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20645 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20649 tree op
= TREE_OPERAND (expression
, 0);
20650 return (value_dependent_expression_p (op
)
20651 || has_value_dependent_address (op
));
20656 tree fn
= get_callee_fndecl (expression
);
20658 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20660 nargs
= call_expr_nargs (expression
);
20661 for (i
= 0; i
< nargs
; ++i
)
20663 tree op
= CALL_EXPR_ARG (expression
, i
);
20664 /* In a call to a constexpr member function, look through the
20665 implicit ADDR_EXPR on the object argument so that it doesn't
20666 cause the call to be considered value-dependent. We also
20667 look through it in potential_constant_expression. */
20668 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20670 && TREE_CODE (op
) == ADDR_EXPR
)
20671 op
= TREE_OPERAND (op
, 0);
20672 if (value_dependent_expression_p (op
))
20678 case TEMPLATE_ID_EXPR
:
20679 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20681 return type_dependent_expression_p (expression
);
20687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20688 if (value_dependent_expression_p (val
))
20694 /* Treat a GNU statement expression as dependent to avoid crashing
20695 under fold_non_dependent_expr; it can't be constant. */
20699 /* A constant expression is value-dependent if any subexpression is
20700 value-dependent. */
20701 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20703 case tcc_reference
:
20705 case tcc_comparison
:
20707 case tcc_expression
:
20710 int i
, len
= cp_tree_operand_length (expression
);
20712 for (i
= 0; i
< len
; i
++)
20714 tree t
= TREE_OPERAND (expression
, i
);
20716 /* In some cases, some of the operands may be missing.l
20717 (For example, in the case of PREDECREMENT_EXPR, the
20718 amount to increment by may be missing.) That doesn't
20719 make the expression dependent. */
20720 if (t
&& value_dependent_expression_p (t
))
20731 /* The expression is not value-dependent. */
20735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20736 [temp.dep.expr]. Note that an expression with no type is
20737 considered dependent. Other parts of the compiler arrange for an
20738 expression with type-dependent subexpressions to have no type, so
20739 this function doesn't have to be fully recursive. */
20742 type_dependent_expression_p (tree expression
)
20744 if (!processing_template_decl
)
20747 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20750 /* An unresolved name is always dependent. */
20751 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20754 /* Some expression forms are never type-dependent. */
20755 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20756 || TREE_CODE (expression
) == SIZEOF_EXPR
20757 || TREE_CODE (expression
) == ALIGNOF_EXPR
20758 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20759 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20760 || TREE_CODE (expression
) == TRAIT_EXPR
20761 || TREE_CODE (expression
) == TYPEID_EXPR
20762 || TREE_CODE (expression
) == DELETE_EXPR
20763 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20764 || TREE_CODE (expression
) == THROW_EXPR
)
20767 /* The types of these expressions depends only on the type to which
20768 the cast occurs. */
20769 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20770 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20771 || TREE_CODE (expression
) == CONST_CAST_EXPR
20772 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20773 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20774 || TREE_CODE (expression
) == CAST_EXPR
)
20775 return dependent_type_p (TREE_TYPE (expression
));
20777 /* The types of these expressions depends only on the type created
20778 by the expression. */
20779 if (TREE_CODE (expression
) == NEW_EXPR
20780 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20782 /* For NEW_EXPR tree nodes created inside a template, either
20783 the object type itself or a TREE_LIST may appear as the
20785 tree type
= TREE_OPERAND (expression
, 1);
20786 if (TREE_CODE (type
) == TREE_LIST
)
20787 /* This is an array type. We need to check array dimensions
20789 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20790 || value_dependent_expression_p
20791 (TREE_OPERAND (TREE_VALUE (type
), 1));
20793 return dependent_type_p (type
);
20796 if (TREE_CODE (expression
) == SCOPE_REF
)
20798 tree scope
= TREE_OPERAND (expression
, 0);
20799 tree name
= TREE_OPERAND (expression
, 1);
20801 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20802 contains an identifier associated by name lookup with one or more
20803 declarations declared with a dependent type, or...a
20804 nested-name-specifier or qualified-id that names a member of an
20805 unknown specialization. */
20806 return (type_dependent_expression_p (name
)
20807 || dependent_scope_p (scope
));
20810 if (TREE_CODE (expression
) == FUNCTION_DECL
20811 && DECL_LANG_SPECIFIC (expression
)
20812 && DECL_TEMPLATE_INFO (expression
)
20813 && (any_dependent_template_arguments_p
20814 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20817 if (TREE_CODE (expression
) == TEMPLATE_DECL
20818 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20821 if (TREE_CODE (expression
) == STMT_EXPR
)
20822 expression
= stmt_expr_value_expr (expression
);
20824 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20829 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20831 if (type_dependent_expression_p (elt
))
20837 /* A static data member of the current instantiation with incomplete
20838 array type is type-dependent, as the definition and specializations
20839 can have different bounds. */
20840 if (VAR_P (expression
)
20841 && DECL_CLASS_SCOPE_P (expression
)
20842 && dependent_type_p (DECL_CONTEXT (expression
))
20843 && VAR_HAD_UNKNOWN_BOUND (expression
))
20846 /* An array of unknown bound depending on a variadic parameter, eg:
20848 template<typename... Args>
20849 void foo (Args... args)
20851 int arr[] = { args... };
20854 template<int... vals>
20857 int arr[] = { vals... };
20860 If the array has no length and has an initializer, it must be that
20861 we couldn't determine its length in cp_complete_array_type because
20862 it is dependent. */
20863 if (VAR_P (expression
)
20864 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20865 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20866 && DECL_INITIAL (expression
))
20869 if (TREE_TYPE (expression
) == unknown_type_node
)
20871 if (TREE_CODE (expression
) == ADDR_EXPR
)
20872 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20873 if (TREE_CODE (expression
) == COMPONENT_REF
20874 || TREE_CODE (expression
) == OFFSET_REF
)
20876 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20878 expression
= TREE_OPERAND (expression
, 1);
20879 if (identifier_p (expression
))
20882 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20883 if (TREE_CODE (expression
) == SCOPE_REF
)
20886 /* Always dependent, on the number of arguments if nothing else. */
20887 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20890 if (BASELINK_P (expression
))
20891 expression
= BASELINK_FUNCTIONS (expression
);
20893 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20895 if (any_dependent_template_arguments_p
20896 (TREE_OPERAND (expression
, 1)))
20898 expression
= TREE_OPERAND (expression
, 0);
20900 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20901 || TREE_CODE (expression
) == FUNCTION_DECL
);
20905 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20907 expression
= OVL_NEXT (expression
);
20912 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20914 return (dependent_type_p (TREE_TYPE (expression
)));
20917 /* walk_tree callback function for instantiation_dependent_expression_p,
20918 below. Returns non-zero if a dependent subexpression is found. */
20921 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20926 /* We don't have to worry about decltype currently because decltype
20927 of an instantiation-dependent expr is a dependent type. This
20928 might change depending on the resolution of DR 1172. */
20929 *walk_subtrees
= false;
20932 enum tree_code code
= TREE_CODE (*tp
);
20935 /* Don't treat an argument list as dependent just because it has no
20943 /* A constant with a dependent initializer is dependent. */
20944 if (value_dependent_expression_p (*tp
))
20948 case TEMPLATE_PARM_INDEX
:
20951 /* Handle expressions with type operands. */
20955 case AT_ENCODE_EXPR
:
20957 tree op
= TREE_OPERAND (*tp
, 0);
20958 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20959 op
= TREE_TYPE (op
);
20962 if (dependent_type_p (op
))
20966 *walk_subtrees
= false;
20974 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20975 || (TRAIT_EXPR_TYPE2 (*tp
)
20976 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20978 *walk_subtrees
= false;
20981 case COMPONENT_REF
:
20982 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20983 /* In a template, finish_class_member_access_expr creates a
20984 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20985 type-dependent, so that we can check access control at
20986 instantiation time (PR 42277). See also Core issue 1273. */
20991 if (instantiation_dependent_scope_ref_p (*tp
))
20996 /* Treat statement-expressions as dependent. */
21004 if (type_dependent_expression_p (*tp
))
21010 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21011 sense defined by the ABI:
21013 "An expression is instantiation-dependent if it is type-dependent
21014 or value-dependent, or it has a subexpression that is type-dependent
21015 or value-dependent." */
21018 instantiation_dependent_expression_p (tree expression
)
21022 if (!processing_template_decl
)
21025 if (expression
== error_mark_node
)
21028 result
= cp_walk_tree_without_duplicates (&expression
,
21029 instantiation_dependent_r
, NULL
);
21030 return result
!= NULL_TREE
;
21033 /* Like type_dependent_expression_p, but it also works while not processing
21034 a template definition, i.e. during substitution or mangling. */
21037 type_dependent_expression_p_push (tree expr
)
21040 ++processing_template_decl
;
21041 b
= type_dependent_expression_p (expr
);
21042 --processing_template_decl
;
21046 /* Returns TRUE if ARGS contains a type-dependent expression. */
21049 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
21054 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
21056 if (type_dependent_expression_p (arg
))
21062 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21063 expressions) contains any type-dependent expressions. */
21066 any_type_dependent_elements_p (const_tree list
)
21068 for (; list
; list
= TREE_CHAIN (list
))
21069 if (type_dependent_expression_p (TREE_VALUE (list
)))
21075 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21076 expressions) contains any value-dependent expressions. */
21079 any_value_dependent_elements_p (const_tree list
)
21081 for (; list
; list
= TREE_CHAIN (list
))
21082 if (value_dependent_expression_p (TREE_VALUE (list
)))
21088 /* Returns TRUE if the ARG (a template argument) is dependent. */
21091 dependent_template_arg_p (tree arg
)
21093 if (!processing_template_decl
)
21096 /* Assume a template argument that was wrongly written by the user
21097 is dependent. This is consistent with what
21098 any_dependent_template_arguments_p [that calls this function]
21100 if (!arg
|| arg
== error_mark_node
)
21103 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
21104 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
21106 if (TREE_CODE (arg
) == TEMPLATE_DECL
21107 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
21108 return dependent_template_p (arg
);
21109 else if (ARGUMENT_PACK_P (arg
))
21111 tree args
= ARGUMENT_PACK_ARGS (arg
);
21112 int i
, len
= TREE_VEC_LENGTH (args
);
21113 for (i
= 0; i
< len
; ++i
)
21115 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21121 else if (TYPE_P (arg
))
21122 return dependent_type_p (arg
);
21124 return (type_dependent_expression_p (arg
)
21125 || value_dependent_expression_p (arg
));
21128 /* Returns true if ARGS (a collection of template arguments) contains
21129 any types that require structural equality testing. */
21132 any_template_arguments_need_structural_equality_p (tree args
)
21139 if (args
== error_mark_node
)
21142 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21144 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21145 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21147 tree arg
= TREE_VEC_ELT (level
, j
);
21148 tree packed_args
= NULL_TREE
;
21151 if (ARGUMENT_PACK_P (arg
))
21153 /* Look inside the argument pack. */
21154 packed_args
= ARGUMENT_PACK_ARGS (arg
);
21155 len
= TREE_VEC_LENGTH (packed_args
);
21158 for (k
= 0; k
< len
; ++k
)
21161 arg
= TREE_VEC_ELT (packed_args
, k
);
21163 if (error_operand_p (arg
))
21165 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
21167 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
21169 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
21170 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
21179 /* Returns true if ARGS (a collection of template arguments) contains
21180 any dependent arguments. */
21183 any_dependent_template_arguments_p (const_tree args
)
21190 if (args
== error_mark_node
)
21193 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21195 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21196 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21197 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21204 /* Returns TRUE if the template TMPL is dependent. */
21207 dependent_template_p (tree tmpl
)
21209 if (TREE_CODE (tmpl
) == OVERLOAD
)
21213 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21215 tmpl
= OVL_NEXT (tmpl
);
21220 /* Template template parameters are dependent. */
21221 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21222 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21224 /* So are names that have not been looked up. */
21225 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21227 /* So are member templates of dependent classes. */
21228 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21229 return dependent_type_p (DECL_CONTEXT (tmpl
));
21233 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21236 dependent_template_id_p (tree tmpl
, tree args
)
21238 return (dependent_template_p (tmpl
)
21239 || any_dependent_template_arguments_p (args
));
21242 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21246 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21250 if (!processing_template_decl
)
21253 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21255 tree decl
= TREE_VEC_ELT (declv
, i
);
21256 tree init
= TREE_VEC_ELT (initv
, i
);
21257 tree cond
= TREE_VEC_ELT (condv
, i
);
21258 tree incr
= TREE_VEC_ELT (incrv
, i
);
21260 if (type_dependent_expression_p (decl
))
21263 if (init
&& type_dependent_expression_p (init
))
21266 if (type_dependent_expression_p (cond
))
21269 if (COMPARISON_CLASS_P (cond
)
21270 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21271 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21274 if (TREE_CODE (incr
) == MODOP_EXPR
)
21276 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21277 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21280 else if (type_dependent_expression_p (incr
))
21282 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21284 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21286 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21288 tree t
= TREE_OPERAND (incr
, 1);
21289 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21290 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21299 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21300 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21301 no such TYPE can be found. Note that this function peers inside
21302 uninstantiated templates and therefore should be used only in
21303 extremely limited situations. ONLY_CURRENT_P restricts this
21304 peering to the currently open classes hierarchy (which is required
21305 when comparing types). */
21308 resolve_typename_type (tree type
, bool only_current_p
)
21317 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21319 scope
= TYPE_CONTEXT (type
);
21320 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21321 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21322 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21323 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21324 identifier of the TYPENAME_TYPE anymore.
21325 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21326 TYPENAME_TYPE instead, we avoid messing up with a possible
21327 typedef variant case. */
21328 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21330 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21331 it first before we can figure out what NAME refers to. */
21332 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21334 if (TYPENAME_IS_RESOLVING_P (scope
))
21335 /* Given a class template A with a dependent base with nested type C,
21336 typedef typename A::C::C C will land us here, as trying to resolve
21337 the initial A::C leads to the local C typedef, which leads back to
21338 A::C::C. So we break the recursion now. */
21341 scope
= resolve_typename_type (scope
, only_current_p
);
21343 /* If we don't know what SCOPE refers to, then we cannot resolve the
21345 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21347 /* If the SCOPE is a template type parameter, we have no way of
21348 resolving the name. */
21349 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21351 /* If the SCOPE is not the current instantiation, there's no reason
21352 to look inside it. */
21353 if (only_current_p
&& !currently_open_class (scope
))
21355 /* If this is a typedef, we don't want to look inside (c++/11987). */
21356 if (typedef_variant_p (type
))
21358 /* If SCOPE isn't the template itself, it will not have a valid
21359 TYPE_FIELDS list. */
21360 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21361 /* scope is either the template itself or a compatible instantiation
21362 like X<T>, so look up the name in the original template. */
21363 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21365 /* scope is a partial instantiation, so we can't do the lookup or we
21366 will lose the template arguments. */
21368 /* Enter the SCOPE so that name lookup will be resolved as if we
21369 were in the class definition. In particular, SCOPE will no
21370 longer be considered a dependent type. */
21371 pushed_scope
= push_scope (scope
);
21372 /* Look up the declaration. */
21373 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21374 tf_warning_or_error
);
21376 result
= NULL_TREE
;
21378 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21379 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21382 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21383 && TREE_CODE (decl
) == TYPE_DECL
)
21385 result
= TREE_TYPE (decl
);
21386 if (result
== error_mark_node
)
21387 result
= NULL_TREE
;
21389 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21390 && DECL_CLASS_TEMPLATE_P (decl
))
21394 /* Obtain the template and the arguments. */
21395 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21396 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21397 /* Instantiate the template. */
21398 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21399 /*entering_scope=*/0,
21400 tf_error
| tf_user
);
21401 if (result
== error_mark_node
)
21402 result
= NULL_TREE
;
21405 /* Leave the SCOPE. */
21407 pop_scope (pushed_scope
);
21409 /* If we failed to resolve it, return the original typename. */
21413 /* If lookup found a typename type, resolve that too. */
21414 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21416 /* Ill-formed programs can cause infinite recursion here, so we
21417 must catch that. */
21418 TYPENAME_IS_RESOLVING_P (type
) = 1;
21419 result
= resolve_typename_type (result
, only_current_p
);
21420 TYPENAME_IS_RESOLVING_P (type
) = 0;
21423 /* Qualify the resulting type. */
21424 quals
= cp_type_quals (type
);
21426 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21431 /* EXPR is an expression which is not type-dependent. Return a proxy
21432 for EXPR that can be used to compute the types of larger
21433 expressions containing EXPR. */
21436 build_non_dependent_expr (tree expr
)
21440 #ifdef ENABLE_CHECKING
21441 /* Try to get a constant value for all non-dependent expressions in
21442 order to expose bugs in *_dependent_expression_p and constexpr. */
21443 if (cxx_dialect
>= cxx11
)
21444 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21447 /* Preserve OVERLOADs; the functions must be available to resolve
21450 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21451 inner_expr
= stmt_expr_value_expr (inner_expr
);
21452 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21453 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21454 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21455 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21456 if (is_overloaded_fn (inner_expr
)
21457 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21459 /* There is no need to return a proxy for a variable. */
21462 /* Preserve string constants; conversions from string constants to
21463 "char *" are allowed, even though normally a "const char *"
21464 cannot be used to initialize a "char *". */
21465 if (TREE_CODE (expr
) == STRING_CST
)
21467 /* Preserve arithmetic constants, as an optimization -- there is no
21468 reason to create a new node. */
21469 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21471 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21472 There is at least one place where we want to know that a
21473 particular expression is a throw-expression: when checking a ?:
21474 expression, there are special rules if the second or third
21475 argument is a throw-expression. */
21476 if (TREE_CODE (expr
) == THROW_EXPR
)
21479 /* Don't wrap an initializer list, we need to be able to look inside. */
21480 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21483 /* Don't wrap a dummy object, we need to be able to test for it. */
21484 if (is_dummy_object (expr
))
21487 if (TREE_CODE (expr
) == COND_EXPR
)
21488 return build3 (COND_EXPR
,
21490 TREE_OPERAND (expr
, 0),
21491 (TREE_OPERAND (expr
, 1)
21492 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21493 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21494 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21495 if (TREE_CODE (expr
) == COMPOUND_EXPR
21496 && !COMPOUND_EXPR_OVERLOADED (expr
))
21497 return build2 (COMPOUND_EXPR
,
21499 TREE_OPERAND (expr
, 0),
21500 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21502 /* If the type is unknown, it can't really be non-dependent */
21503 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21505 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21506 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21509 /* ARGS is a vector of expressions as arguments to a function call.
21510 Replace the arguments with equivalent non-dependent expressions.
21511 This modifies ARGS in place. */
21514 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21519 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21521 tree newarg
= build_non_dependent_expr (arg
);
21523 (*args
)[ix
] = newarg
;
21527 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21528 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21532 make_auto_1 (tree name
)
21534 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21535 TYPE_NAME (au
) = build_decl (input_location
,
21536 TYPE_DECL
, name
, au
);
21537 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21538 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21539 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21540 TYPE_NAME (au
), NULL_TREE
);
21541 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21542 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21543 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21549 make_decltype_auto (void)
21551 return make_auto_1 (get_identifier ("decltype(auto)"));
21557 return make_auto_1 (get_identifier ("auto"));
21560 /* Given type ARG, return std::initializer_list<ARG>. */
21565 tree std_init_list
= namespace_binding
21566 (get_identifier ("initializer_list"), std_node
);
21568 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21570 error ("deducing from brace-enclosed initializer list requires "
21571 "#include <initializer_list>");
21572 return error_mark_node
;
21574 argvec
= make_tree_vec (1);
21575 TREE_VEC_ELT (argvec
, 0) = arg
;
21576 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21577 NULL_TREE
, 0, tf_warning_or_error
);
21580 /* Replace auto in TYPE with std::initializer_list<auto>. */
21583 listify_autos (tree type
, tree auto_node
)
21585 tree init_auto
= listify (auto_node
);
21586 tree argvec
= make_tree_vec (1);
21587 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21588 if (processing_template_decl
)
21589 argvec
= add_to_template_args (current_template_args (), argvec
);
21590 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21593 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21594 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21597 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21601 if (init
== error_mark_node
)
21602 return error_mark_node
;
21604 if (type_dependent_expression_p (init
))
21605 /* Defining a subset of type-dependent expressions that we can deduce
21606 from ahead of time isn't worth the trouble. */
21609 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21610 with either a new invented type template parameter U or, if the
21611 initializer is a braced-init-list (8.5.4), with
21612 std::initializer_list<U>. */
21613 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21614 type
= listify_autos (type
, auto_node
);
21616 init
= resolve_nondeduced_context (init
);
21618 targs
= make_tree_vec (1);
21619 if (AUTO_IS_DECLTYPE (auto_node
))
21621 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21622 TREE_VEC_ELT (targs
, 0)
21623 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21624 if (type
!= auto_node
)
21626 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21627 return error_mark_node
;
21632 tree parms
= build_tree_list (NULL_TREE
, type
);
21633 tree tparms
= make_tree_vec (1);
21636 TREE_VEC_ELT (tparms
, 0)
21637 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21638 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21639 DEDUCE_CALL
, LOOKUP_NORMAL
,
21640 NULL
, /*explain_p=*/false);
21643 if (processing_template_decl
)
21644 /* Try again at instantiation time. */
21646 if (type
&& type
!= error_mark_node
)
21647 /* If type is error_mark_node a diagnostic must have been
21648 emitted by now. Also, having a mention to '<type error>'
21649 in the diagnostic is not really useful to the user. */
21651 if (cfun
&& auto_node
== current_function_auto_return_pattern
21652 && LAMBDA_FUNCTION_P (current_function_decl
))
21653 error ("unable to deduce lambda return type from %qE", init
);
21655 error ("unable to deduce %qT from %qE", type
, init
);
21657 return error_mark_node
;
21661 /* If the list of declarators contains more than one declarator, the type
21662 of each declared variable is determined as described above. If the
21663 type deduced for the template parameter U is not the same in each
21664 deduction, the program is ill-formed. */
21665 if (TREE_TYPE (auto_node
)
21666 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21668 if (cfun
&& auto_node
== current_function_auto_return_pattern
21669 && LAMBDA_FUNCTION_P (current_function_decl
))
21670 error ("inconsistent types %qT and %qT deduced for "
21671 "lambda return type", TREE_TYPE (auto_node
),
21672 TREE_VEC_ELT (targs
, 0));
21674 error ("inconsistent deduction for %qT: %qT and then %qT",
21675 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21676 return error_mark_node
;
21678 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21680 if (processing_template_decl
)
21681 targs
= add_to_template_args (current_template_args (), targs
);
21682 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21685 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21689 splice_late_return_type (tree type
, tree late_return_type
)
21693 if (late_return_type
== NULL_TREE
)
21695 argvec
= make_tree_vec (1);
21696 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21697 if (processing_template_parmlist
)
21698 /* For a late-specified return type in a template type-parameter, we
21699 need to add a dummy argument level for its parmlist. */
21700 argvec
= add_to_template_args
21701 (make_tree_vec (processing_template_parmlist
), argvec
);
21702 if (current_template_parms
)
21703 argvec
= add_to_template_args (current_template_args (), argvec
);
21704 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21707 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21708 'decltype(auto)'. */
21711 is_auto (const_tree type
)
21713 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21714 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21715 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21721 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21722 a use of `auto'. Returns NULL_TREE otherwise. */
21725 type_uses_auto (tree type
)
21727 return find_type_usage (type
, is_auto
);
21730 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21731 'decltype(auto)' or a concept. */
21734 is_auto_or_concept (const_tree type
)
21736 return is_auto (type
); // or concept
21739 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21740 a concept identifier) iff TYPE contains a use of a generic type. Returns
21741 NULL_TREE otherwise. */
21744 type_uses_auto_or_concept (tree type
)
21746 return find_type_usage (type
, is_auto_or_concept
);
21750 /* For a given template T, return the vector of typedefs referenced
21751 in T for which access check is needed at T instantiation time.
21752 T is either a FUNCTION_DECL or a RECORD_TYPE.
21753 Those typedefs were added to T by the function
21754 append_type_to_template_for_access_check. */
21756 vec
<qualified_typedef_usage_t
, va_gc
> *
21757 get_types_needing_access_check (tree t
)
21760 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21762 if (!t
|| t
== error_mark_node
)
21765 if (!(ti
= get_template_info (t
)))
21768 if (CLASS_TYPE_P (t
)
21769 || TREE_CODE (t
) == FUNCTION_DECL
)
21771 if (!TI_TEMPLATE (ti
))
21774 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21780 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21781 tied to T. That list of typedefs will be access checked at
21782 T instantiation time.
21783 T is either a FUNCTION_DECL or a RECORD_TYPE.
21784 TYPE_DECL is a TYPE_DECL node representing a typedef.
21785 SCOPE is the scope through which TYPE_DECL is accessed.
21786 LOCATION is the location of the usage point of TYPE_DECL.
21788 This function is a subroutine of
21789 append_type_to_template_for_access_check. */
21792 append_type_to_template_for_access_check_1 (tree t
,
21795 location_t location
)
21797 qualified_typedef_usage_t typedef_usage
;
21800 if (!t
|| t
== error_mark_node
)
21803 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21804 || CLASS_TYPE_P (t
))
21806 && TREE_CODE (type_decl
) == TYPE_DECL
21809 if (!(ti
= get_template_info (t
)))
21812 gcc_assert (TI_TEMPLATE (ti
));
21814 typedef_usage
.typedef_decl
= type_decl
;
21815 typedef_usage
.context
= scope
;
21816 typedef_usage
.locus
= location
;
21818 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21821 /* Append TYPE_DECL to the template TEMPL.
21822 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21823 At TEMPL instanciation time, TYPE_DECL will be checked to see
21824 if it can be accessed through SCOPE.
21825 LOCATION is the location of the usage point of TYPE_DECL.
21827 e.g. consider the following code snippet:
21834 template<class U> struct S
21836 C::myint mi; // <-- usage point of the typedef C::myint
21841 At S<char> instantiation time, we need to check the access of C::myint
21842 In other words, we need to check the access of the myint typedef through
21843 the C scope. For that purpose, this function will add the myint typedef
21844 and the scope C through which its being accessed to a list of typedefs
21845 tied to the template S. That list will be walked at template instantiation
21846 time and access check performed on each typedefs it contains.
21847 Note that this particular code snippet should yield an error because
21848 myint is private to C. */
21851 append_type_to_template_for_access_check (tree templ
,
21854 location_t location
)
21856 qualified_typedef_usage_t
*iter
;
21859 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21861 /* Make sure we don't append the type to the template twice. */
21862 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21863 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21866 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21870 /* Convert the generic type parameters in PARM that match the types given in the
21871 range [START_IDX, END_IDX) from the current_template_parms into generic type
21875 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21877 tree current
= current_template_parms
;
21878 int depth
= TMPL_PARMS_DEPTH (current
);
21879 current
= INNERMOST_TEMPLATE_PARMS (current
);
21880 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21882 for (int i
= 0; i
< start_idx
; ++i
)
21883 TREE_VEC_ELT (replacement
, i
)
21884 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21886 for (int i
= start_idx
; i
< end_idx
; ++i
)
21888 /* Create a distinct parameter pack type from the current parm and add it
21889 to the replacement args to tsubst below into the generic function
21892 tree o
= TREE_TYPE (TREE_VALUE
21893 (TREE_VEC_ELT (current
, i
)));
21894 tree t
= copy_type (o
);
21895 TEMPLATE_TYPE_PARM_INDEX (t
)
21896 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21898 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21899 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21900 TYPE_MAIN_VARIANT (t
) = t
;
21901 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21902 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21903 TREE_VEC_ELT (replacement
, i
) = t
;
21904 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21907 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21908 TREE_VEC_ELT (replacement
, i
)
21909 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21911 /* If there are more levels then build up the replacement with the outer
21914 replacement
= add_to_template_args (template_parms_to_args
21915 (TREE_CHAIN (current_template_parms
)),
21918 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21922 /* Set up the hash tables for template instantiations. */
21925 init_template_processing (void)
21927 decl_specializations
= htab_create_ggc (37,
21928 hash_specialization
,
21929 eq_specializations
,
21931 type_specializations
= htab_create_ggc (37,
21932 hash_specialization
,
21933 eq_specializations
,
21937 /* Print stats about the template hash tables for -fstats. */
21940 print_template_statistics (void)
21942 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21943 "%f collisions\n", (long) htab_size (decl_specializations
),
21944 (long) htab_elements (decl_specializations
),
21945 htab_collisions (decl_specializations
));
21946 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21947 "%f collisions\n", (long) htab_size (type_specializations
),
21948 (long) htab_elements (type_specializations
),
21949 htab_collisions (type_specializations
));
21952 #include "gt-cp-pt.h"