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 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
854 && !at_namespace_scope_p ())
855 return error_mark_node
;
856 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
857 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
858 if (processing_template_decl
)
860 if (push_template_decl (TYPE_MAIN_DECL (type
))
862 return error_mark_node
;
865 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
866 error ("specialization of %qT after instantiation", type
);
867 else if (errorcount
&& !processing_specialization
868 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
869 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
870 /* Trying to define a specialization either without a template<> header
871 or in an inappropriate place. We've already given an error, so just
872 bail now so we don't actually define the specialization. */
873 return error_mark_node
;
875 else if (CLASS_TYPE_P (type
)
876 && !CLASSTYPE_USE_TEMPLATE (type
)
877 && CLASSTYPE_TEMPLATE_INFO (type
)
878 && context
&& CLASS_TYPE_P (context
)
879 && CLASSTYPE_TEMPLATE_INFO (context
))
881 /* This is for an explicit specialization of member class
882 template according to [temp.expl.spec/18]:
884 template <> template <class U> class C<int>::D;
886 The context `C<int>' must be an implicit instantiation.
887 Otherwise this is just a member class template declared
890 template <> class C<int> { template <class U> class D; };
891 template <> template <class U> class C<int>::D;
893 In the first case, `C<int>::D' is a specialization of `C<T>::D'
894 while in the second case, `C<int>::D' is a primary template
895 and `C<T>::D' may not exist. */
897 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
898 && !COMPLETE_TYPE_P (type
))
901 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
903 if (current_namespace
904 != decl_namespace_context (tmpl
))
906 permerror (input_location
, "specializing %q#T in different namespace", type
);
907 permerror (input_location
, " from definition of %q+#D", tmpl
);
910 /* Check for invalid specialization after instantiation:
912 template <> template <> class C<int>::D<int>;
913 template <> template <class U> class C<int>::D; */
915 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
916 t
; t
= TREE_CHAIN (t
))
918 tree inst
= TREE_VALUE (t
);
919 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
920 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
922 /* We already have a full specialization of this partial
923 instantiation, or a full specialization has been
924 looked up but not instantiated. Reassign it to the
925 new member specialization template. */
930 elt
.tmpl
= most_general_template (tmpl
);
931 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
934 htab_remove_elt (type_specializations
, &elt
);
937 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
939 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
940 entry
= ggc_alloc_spec_entry ();
945 /* But if we've had an implicit instantiation, that's a
946 problem ([temp.expl.spec]/6). */
947 error ("specialization %qT after instantiation %qT",
951 /* Mark TYPE as a specialization. And as a result, we only
952 have one level of template argument for the innermost
954 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
955 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
956 CLASSTYPE_TI_ARGS (type
)
957 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
960 else if (processing_specialization
)
962 /* Someday C++0x may allow for enum template specialization. */
963 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
964 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
965 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
966 "of %qD not allowed by ISO C++", type
);
969 error ("explicit specialization of non-template %qT", type
);
970 return error_mark_node
;
977 /* Returns nonzero if we can optimize the retrieval of specializations
978 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
979 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
982 optimize_specialization_lookup_p (tree tmpl
)
984 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
985 && DECL_CLASS_SCOPE_P (tmpl
)
986 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
988 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
989 /* The optimized lookup depends on the fact that the
990 template arguments for the member function template apply
991 purely to the containing class, which is not true if the
992 containing class is an explicit or partial
994 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
995 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
996 && !DECL_CONV_FN_P (tmpl
)
997 /* It is possible to have a template that is not a member
998 template and is not a member of a template class:
1000 template <typename T>
1001 struct S { friend A::f(); };
1003 Here, the friend function is a template, but the context does
1004 not have template information. The optimized lookup relies
1005 on having ARGS be the template arguments for both the class
1006 and the function template. */
1007 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1010 /* Retrieve the specialization (in the sense of [temp.spec] - a
1011 specialization is either an instantiation or an explicit
1012 specialization) of TMPL for the given template ARGS. If there is
1013 no such specialization, return NULL_TREE. The ARGS are a vector of
1014 arguments, or a vector of vectors of arguments, in the case of
1015 templates with more than one level of parameters.
1017 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1018 then we search for a partial specialization matching ARGS. This
1019 parameter is ignored if TMPL is not a class template.
1021 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1022 result is a NONTYPE_ARGUMENT_PACK. */
1025 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1027 if (tmpl
== NULL_TREE
)
1030 if (args
== error_mark_node
)
1033 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1034 || TREE_CODE (tmpl
) == FIELD_DECL
);
1036 /* There should be as many levels of arguments as there are
1037 levels of parameters. */
1038 gcc_assert (TMPL_ARGS_DEPTH (args
)
1039 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1040 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1041 : template_class_depth (DECL_CONTEXT (tmpl
))));
1043 if (optimize_specialization_lookup_p (tmpl
))
1045 tree class_template
;
1046 tree class_specialization
;
1047 vec
<tree
, va_gc
> *methods
;
1051 /* The template arguments actually apply to the containing
1052 class. Find the class specialization with those
1054 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1055 class_specialization
1056 = retrieve_specialization (class_template
, args
, 0);
1057 if (!class_specialization
)
1059 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1060 for the specialization. */
1061 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1064 /* Iterate through the methods with the indicated name, looking
1065 for the one that has an instance of TMPL. */
1066 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1067 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1069 tree fn
= OVL_CURRENT (fns
);
1070 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1071 /* using-declarations can add base methods to the method vec,
1072 and we don't want those here. */
1073 && DECL_CONTEXT (fn
) == class_specialization
)
1082 htab_t specializations
;
1086 elt
.spec
= NULL_TREE
;
1088 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1089 specializations
= type_specializations
;
1091 specializations
= decl_specializations
;
1094 hash
= hash_specialization (&elt
);
1095 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1103 /* Like retrieve_specialization, but for local declarations. */
1106 retrieve_local_specialization (tree tmpl
)
1110 if (local_specializations
== NULL
)
1113 slot
= pointer_map_contains (local_specializations
, tmpl
);
1114 return slot
? (tree
) *slot
: NULL_TREE
;
1117 /* Returns nonzero iff DECL is a specialization of TMPL. */
1120 is_specialization_of (tree decl
, tree tmpl
)
1124 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1128 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1134 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1136 for (t
= TREE_TYPE (decl
);
1138 t
= CLASSTYPE_USE_TEMPLATE (t
)
1139 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1140 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1147 /* Returns nonzero iff DECL is a specialization of friend declaration
1148 FRIEND_DECL according to [temp.friend]. */
1151 is_specialization_of_friend (tree decl
, tree friend_decl
)
1153 bool need_template
= true;
1156 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1157 || TREE_CODE (decl
) == TYPE_DECL
);
1159 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1160 of a template class, we want to check if DECL is a specialization
1162 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1163 && DECL_TEMPLATE_INFO (friend_decl
)
1164 && !DECL_USE_TEMPLATE (friend_decl
))
1166 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1167 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1168 need_template
= false;
1170 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1171 && !PRIMARY_TEMPLATE_P (friend_decl
))
1172 need_template
= false;
1174 /* There is nothing to do if this is not a template friend. */
1175 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1178 if (is_specialization_of (decl
, friend_decl
))
1182 A member of a class template may be declared to be a friend of a
1183 non-template class. In this case, the corresponding member of
1184 every specialization of the class template is a friend of the
1185 class granting friendship.
1187 For example, given a template friend declaration
1189 template <class T> friend void A<T>::f();
1191 the member function below is considered a friend
1193 template <> struct A<int> {
1197 For this type of template friend, TEMPLATE_DEPTH below will be
1198 nonzero. To determine if DECL is a friend of FRIEND, we first
1199 check if the enclosing class is a specialization of another. */
1201 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1203 && DECL_CLASS_SCOPE_P (decl
)
1204 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1205 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1207 /* Next, we check the members themselves. In order to handle
1208 a few tricky cases, such as when FRIEND_DECL's are
1210 template <class T> friend void A<T>::g(T t);
1211 template <class T> template <T t> friend void A<T>::h();
1215 void A<int>::g(int);
1216 template <int> void A<int>::h();
1218 we need to figure out ARGS, the template arguments from
1219 the context of DECL. This is required for template substitution
1220 of `T' in the function parameter of `g' and template parameter
1221 of `h' in the above examples. Here ARGS corresponds to `int'. */
1223 tree context
= DECL_CONTEXT (decl
);
1224 tree args
= NULL_TREE
;
1225 int current_depth
= 0;
1227 while (current_depth
< template_depth
)
1229 if (CLASSTYPE_TEMPLATE_INFO (context
))
1231 if (current_depth
== 0)
1232 args
= TYPE_TI_ARGS (context
);
1234 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1237 context
= TYPE_CONTEXT (context
);
1240 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1245 tree friend_args_type
;
1246 tree decl_args_type
;
1248 /* Make sure that both DECL and FRIEND_DECL are templates or
1250 is_template
= DECL_TEMPLATE_INFO (decl
)
1251 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1252 if (need_template
^ is_template
)
1254 else if (is_template
)
1256 /* If both are templates, check template parameter list. */
1258 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1260 if (!comp_template_parms
1261 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1265 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1268 decl_type
= TREE_TYPE (decl
);
1270 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1271 tf_none
, NULL_TREE
);
1272 if (friend_type
== error_mark_node
)
1275 /* Check if return types match. */
1276 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1279 /* Check if function parameter types match, ignoring the
1280 `this' parameter. */
1281 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1282 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1284 friend_args_type
= TREE_CHAIN (friend_args_type
);
1285 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1286 decl_args_type
= TREE_CHAIN (decl_args_type
);
1288 return compparms (decl_args_type
, friend_args_type
);
1292 /* DECL is a TYPE_DECL */
1294 tree decl_type
= TREE_TYPE (decl
);
1296 /* Make sure that both DECL and FRIEND_DECL are templates or
1299 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1300 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1302 if (need_template
^ is_template
)
1304 else if (is_template
)
1307 /* If both are templates, check the name of the two
1308 TEMPLATE_DECL's first because is_friend didn't. */
1309 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1310 != DECL_NAME (friend_decl
))
1313 /* Now check template parameter list. */
1315 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1317 return comp_template_parms
1318 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1322 return (DECL_NAME (decl
)
1323 == DECL_NAME (friend_decl
));
1329 /* Register the specialization SPEC as a specialization of TMPL with
1330 the indicated ARGS. IS_FRIEND indicates whether the specialization
1331 is actually just a friend declaration. Returns SPEC, or an
1332 equivalent prior declaration, if available.
1334 We also store instantiations of field packs in the hash table, even
1335 though they are not themselves templates, to make lookup easier. */
1338 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1345 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1346 || (TREE_CODE (tmpl
) == FIELD_DECL
1347 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1349 if (TREE_CODE (spec
) == FUNCTION_DECL
1350 && uses_template_parms (DECL_TI_ARGS (spec
)))
1351 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1352 register it; we want the corresponding TEMPLATE_DECL instead.
1353 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1354 the more obvious `uses_template_parms (spec)' to avoid problems
1355 with default function arguments. In particular, given
1356 something like this:
1358 template <class T> void f(T t1, T t = T())
1360 the default argument expression is not substituted for in an
1361 instantiation unless and until it is actually needed. */
1364 if (optimize_specialization_lookup_p (tmpl
))
1365 /* We don't put these specializations in the hash table, but we might
1366 want to give an error about a mismatch. */
1367 fn
= retrieve_specialization (tmpl
, args
, 0);
1375 hash
= hash_specialization (&elt
);
1378 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1380 fn
= ((spec_entry
*) *slot
)->spec
;
1385 /* We can sometimes try to re-register a specialization that we've
1386 already got. In particular, regenerate_decl_from_template calls
1387 duplicate_decls which will update the specialization list. But,
1388 we'll still get called again here anyhow. It's more convenient
1389 to simply allow this than to try to prevent it. */
1392 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1394 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1396 if (DECL_ODR_USED (fn
)
1397 || DECL_EXPLICIT_INSTANTIATION (fn
))
1399 error ("specialization of %qD after instantiation",
1401 return error_mark_node
;
1406 /* This situation should occur only if the first
1407 specialization is an implicit instantiation, the
1408 second is an explicit specialization, and the
1409 implicit instantiation has not yet been used. That
1410 situation can occur if we have implicitly
1411 instantiated a member function and then specialized
1414 We can also wind up here if a friend declaration that
1415 looked like an instantiation turns out to be a
1418 template <class T> void foo(T);
1419 class S { friend void foo<>(int) };
1420 template <> void foo(int);
1422 We transform the existing DECL in place so that any
1423 pointers to it become pointers to the updated
1426 If there was a definition for the template, but not
1427 for the specialization, we want this to look as if
1428 there were no definition, and vice versa. */
1429 DECL_INITIAL (fn
) = NULL_TREE
;
1430 duplicate_decls (spec
, fn
, is_friend
);
1431 /* The call to duplicate_decls will have applied
1434 An explicit specialization of a function template
1435 is inline only if it is explicitly declared to be,
1436 and independently of whether its function template
1439 to the primary function; now copy the inline bits to
1440 the various clones. */
1441 FOR_EACH_CLONE (clone
, fn
)
1443 DECL_DECLARED_INLINE_P (clone
)
1444 = DECL_DECLARED_INLINE_P (fn
);
1445 DECL_SOURCE_LOCATION (clone
)
1446 = DECL_SOURCE_LOCATION (fn
);
1447 DECL_DELETED_FN (clone
)
1448 = DECL_DELETED_FN (fn
);
1450 check_specialization_namespace (tmpl
);
1455 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1457 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1458 /* Dup decl failed, but this is a new definition. Set the
1459 line number so any errors match this new
1461 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1467 return duplicate_decls (spec
, fn
, is_friend
);
1469 /* A specialization must be declared in the same namespace as the
1470 template it is specializing. */
1471 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1472 && !check_specialization_namespace (tmpl
))
1473 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1475 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1477 spec_entry
*entry
= ggc_alloc_spec_entry ();
1478 gcc_assert (tmpl
&& args
&& spec
);
1481 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1482 && PRIMARY_TEMPLATE_P (tmpl
)
1483 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1484 /* TMPL is a forward declaration of a template function; keep a list
1485 of all specializations in case we need to reassign them to a friend
1486 template later in tsubst_friend_function. */
1487 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1488 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1494 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1495 TMPL and ARGS members, ignores SPEC. */
1497 int comparing_specializations
;
1500 eq_specializations (const void *p1
, const void *p2
)
1502 const spec_entry
*e1
= (const spec_entry
*)p1
;
1503 const spec_entry
*e2
= (const spec_entry
*)p2
;
1506 ++comparing_specializations
;
1507 equal
= (e1
->tmpl
== e2
->tmpl
1508 && comp_template_args (e1
->args
, e2
->args
));
1509 --comparing_specializations
;
1514 /* Returns a hash for a template TMPL and template arguments ARGS. */
1517 hash_tmpl_and_args (tree tmpl
, tree args
)
1519 hashval_t val
= DECL_UID (tmpl
);
1520 return iterative_hash_template_arg (args
, val
);
1523 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1527 hash_specialization (const void *p
)
1529 const spec_entry
*e
= (const spec_entry
*)p
;
1530 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1533 /* Recursively calculate a hash value for a template argument ARG, for use
1534 in the hash tables of template specializations. */
1537 iterative_hash_template_arg (tree arg
, hashval_t val
)
1539 unsigned HOST_WIDE_INT i
;
1540 enum tree_code code
;
1543 if (arg
== NULL_TREE
)
1544 return iterative_hash_object (arg
, val
);
1549 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1550 /* We can get one of these when re-hashing a previous entry in the middle
1551 of substituting into a pack expansion. Just look through it. */
1552 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1554 code
= TREE_CODE (arg
);
1555 tclass
= TREE_CODE_CLASS (code
);
1557 val
= iterative_hash_object (code
, val
);
1564 case IDENTIFIER_NODE
:
1565 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1569 int i
, len
= TREE_VEC_LENGTH (arg
);
1570 for (i
= 0; i
< len
; ++i
)
1571 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1575 case TYPE_PACK_EXPANSION
:
1576 case EXPR_PACK_EXPANSION
:
1577 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1578 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1580 case TYPE_ARGUMENT_PACK
:
1581 case NONTYPE_ARGUMENT_PACK
:
1582 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1585 for (; arg
; arg
= TREE_CHAIN (arg
))
1586 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1590 for (; arg
; arg
= OVL_NEXT (arg
))
1591 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1597 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1599 val
= iterative_hash_template_arg (field
, val
);
1600 val
= iterative_hash_template_arg (value
, val
);
1606 if (!DECL_ARTIFICIAL (arg
))
1608 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1609 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1611 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1614 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1617 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1618 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1620 case TEMPLATE_PARM_INDEX
:
1621 val
= iterative_hash_template_arg
1622 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1623 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1624 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1627 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1628 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1629 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1632 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1634 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1638 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1639 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1640 val
= iterative_hash_object (code
, val
);
1641 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1644 /* A lambda can't appear in a template arg, but don't crash on
1646 gcc_assert (seen_error ());
1650 case IMPLICIT_CONV_EXPR
:
1651 case STATIC_CAST_EXPR
:
1652 case REINTERPRET_CAST_EXPR
:
1653 case CONST_CAST_EXPR
:
1654 case DYNAMIC_CAST_EXPR
:
1656 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1657 /* Now hash operands as usual. */
1667 if (TYPE_CANONICAL (arg
))
1668 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1670 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1671 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1672 /* Otherwise just compare the types during lookup. */
1675 case tcc_declaration
:
1677 return iterative_hash_expr (arg
, val
);
1680 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1682 unsigned n
= cp_tree_operand_length (arg
);
1683 for (i
= 0; i
< n
; ++i
)
1684 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1692 /* Unregister the specialization SPEC as a specialization of TMPL.
1693 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1694 if the SPEC was listed as a specialization of TMPL.
1696 Note that SPEC has been ggc_freed, so we can't look inside it. */
1699 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1704 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1705 elt
.args
= TI_ARGS (tinfo
);
1706 elt
.spec
= NULL_TREE
;
1708 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1711 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1712 gcc_assert (new_spec
!= NULL_TREE
);
1713 entry
->spec
= new_spec
;
1720 /* Like register_specialization, but for local declarations. We are
1721 registering SPEC, an instantiation of TMPL. */
1724 register_local_specialization (tree spec
, tree tmpl
)
1728 slot
= pointer_map_insert (local_specializations
, tmpl
);
1732 /* TYPE is a class type. Returns true if TYPE is an explicitly
1733 specialized class. */
1736 explicit_class_specialization_p (tree type
)
1738 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1740 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1743 /* Print the list of functions at FNS, going through all the overloads
1744 for each element of the list. Alternatively, FNS can not be a
1745 TREE_LIST, in which case it will be printed together with all the
1748 MORE and *STR should respectively be FALSE and NULL when the function
1749 is called from the outside. They are used internally on recursive
1750 calls. print_candidates manages the two parameters and leaves NULL
1751 in *STR when it ends. */
1754 print_candidates_1 (tree fns
, bool more
, const char **str
)
1757 char *spaces
= NULL
;
1759 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1760 if (TREE_CODE (fn
) == TREE_LIST
)
1762 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1763 print_candidates_1 (TREE_VALUE (fn2
),
1764 TREE_CHAIN (fn2
) || more
, str
);
1768 tree cand
= OVL_CURRENT (fn
);
1771 /* Pick the prefix string. */
1772 if (!more
&& !OVL_NEXT (fns
))
1774 inform (DECL_SOURCE_LOCATION (cand
),
1775 "candidate is: %#D", cand
);
1779 *str
= _("candidates are:");
1780 spaces
= get_spaces (*str
);
1782 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1783 *str
= spaces
? spaces
: *str
;
1793 /* Print the list of candidate FNS in an error message. FNS can also
1794 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1797 print_candidates (tree fns
)
1799 const char *str
= NULL
;
1800 print_candidates_1 (fns
, false, &str
);
1801 gcc_assert (str
== NULL
);
1804 /* Returns the template (one of the functions given by TEMPLATE_ID)
1805 which can be specialized to match the indicated DECL with the
1806 explicit template args given in TEMPLATE_ID. The DECL may be
1807 NULL_TREE if none is available. In that case, the functions in
1808 TEMPLATE_ID are non-members.
1810 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1811 specialization of a member template.
1813 The TEMPLATE_COUNT is the number of references to qualifying
1814 template classes that appeared in the name of the function. See
1815 check_explicit_specialization for a more accurate description.
1817 TSK indicates what kind of template declaration (if any) is being
1818 declared. TSK_TEMPLATE indicates that the declaration given by
1819 DECL, though a FUNCTION_DECL, has template parameters, and is
1820 therefore a template function.
1822 The template args (those explicitly specified and those deduced)
1823 are output in a newly created vector *TARGS_OUT.
1825 If it is impossible to determine the result, an error message is
1826 issued. The error_mark_node is returned to indicate failure. */
1829 determine_specialization (tree template_id
,
1832 int need_member_template
,
1838 tree explicit_targs
;
1839 tree candidates
= NULL_TREE
;
1840 /* A TREE_LIST of templates of which DECL may be a specialization.
1841 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1842 corresponding TREE_PURPOSE is the set of template arguments that,
1843 when used to instantiate the template, would produce a function
1844 with the signature of DECL. */
1845 tree templates
= NULL_TREE
;
1847 cp_binding_level
*b
;
1849 *targs_out
= NULL_TREE
;
1851 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1852 return error_mark_node
;
1854 /* We shouldn't be specializing a member template of an
1855 unspecialized class template; we already gave an error in
1856 check_specialization_scope, now avoid crashing. */
1857 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1858 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1860 gcc_assert (errorcount
);
1861 return error_mark_node
;
1864 fns
= TREE_OPERAND (template_id
, 0);
1865 explicit_targs
= TREE_OPERAND (template_id
, 1);
1867 if (fns
== error_mark_node
)
1868 return error_mark_node
;
1870 /* Check for baselinks. */
1871 if (BASELINK_P (fns
))
1872 fns
= BASELINK_FUNCTIONS (fns
);
1874 if (!is_overloaded_fn (fns
))
1876 error ("%qD is not a function template", fns
);
1877 return error_mark_node
;
1880 /* Count the number of template headers specified for this
1883 for (b
= current_binding_level
;
1884 b
->kind
== sk_template_parms
;
1888 for (; fns
; fns
= OVL_NEXT (fns
))
1890 tree fn
= OVL_CURRENT (fns
);
1892 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1894 tree decl_arg_types
;
1898 /* In case of explicit specialization, we need to check if
1899 the number of template headers appearing in the specialization
1900 is correct. This is usually done in check_explicit_specialization,
1901 but the check done there cannot be exhaustive when specializing
1902 member functions. Consider the following code:
1904 template <> void A<int>::f(int);
1905 template <> template <> void A<int>::f(int);
1907 Assuming that A<int> is not itself an explicit specialization
1908 already, the first line specializes "f" which is a non-template
1909 member function, whilst the second line specializes "f" which
1910 is a template member function. So both lines are syntactically
1911 correct, and check_explicit_specialization does not reject
1914 Here, we can do better, as we are matching the specialization
1915 against the declarations. We count the number of template
1916 headers, and we check if they match TEMPLATE_COUNT + 1
1917 (TEMPLATE_COUNT is the number of qualifying template classes,
1918 plus there must be another header for the member template
1921 Notice that if header_count is zero, this is not a
1922 specialization but rather a template instantiation, so there
1923 is no check we can perform here. */
1924 if (header_count
&& header_count
!= template_count
+ 1)
1927 /* Check that the number of template arguments at the
1928 innermost level for DECL is the same as for FN. */
1929 if (current_binding_level
->kind
== sk_template_parms
1930 && !current_binding_level
->explicit_spec_p
1931 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1932 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1933 (current_template_parms
))))
1936 /* DECL might be a specialization of FN. */
1937 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1938 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1940 /* For a non-static member function, we need to make sure
1941 that the const qualification is the same. Since
1942 get_bindings does not try to merge the "this" parameter,
1943 we must do the comparison explicitly. */
1944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1945 && !same_type_p (TREE_VALUE (fn_arg_types
),
1946 TREE_VALUE (decl_arg_types
)))
1949 /* Skip the "this" parameter and, for constructors of
1950 classes with virtual bases, the VTT parameter. A
1951 full specialization of a constructor will have a VTT
1952 parameter, but a template never will. */
1954 = skip_artificial_parms_for (decl
, decl_arg_types
);
1956 = skip_artificial_parms_for (fn
, fn_arg_types
);
1958 /* Function templates cannot be specializations; there are
1959 no partial specializations of functions. Therefore, if
1960 the type of DECL does not match FN, there is no
1962 if (tsk
== tsk_template
)
1964 if (compparms (fn_arg_types
, decl_arg_types
))
1965 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1969 /* See whether this function might be a specialization of this
1970 template. Suppress access control because we might be trying
1971 to make this specialization a friend, and we have already done
1972 access control for the declaration of the specialization. */
1973 push_deferring_access_checks (dk_no_check
);
1974 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1975 pop_deferring_access_checks ();
1978 /* We cannot deduce template arguments that when used to
1979 specialize TMPL will produce DECL. */
1982 /* Make sure that the deduced arguments actually work. */
1983 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1984 if (insttype
== error_mark_node
)
1987 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1988 if (!compparms (fn_arg_types
, decl_arg_types
))
1991 /* Save this template, and the arguments deduced. */
1992 templates
= tree_cons (targs
, fn
, templates
);
1994 else if (need_member_template
)
1995 /* FN is an ordinary member function, and we need a
1996 specialization of a member template. */
1998 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1999 /* We can get IDENTIFIER_NODEs here in certain erroneous
2002 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2003 /* This is just an ordinary non-member function. Nothing can
2004 be a specialization of that. */
2006 else if (DECL_ARTIFICIAL (fn
))
2007 /* Cannot specialize functions that are created implicitly. */
2011 tree decl_arg_types
;
2013 /* This is an ordinary member function. However, since
2014 we're here, we can assume its enclosing class is a
2015 template class. For example,
2017 template <typename T> struct S { void f(); };
2018 template <> void S<int>::f() {}
2020 Here, S<int>::f is a non-template, but S<int> is a
2021 template class. If FN has the same type as DECL, we
2022 might be in business. */
2024 if (!DECL_TEMPLATE_INFO (fn
))
2025 /* Its enclosing class is an explicit specialization
2026 of a template class. This is not a candidate. */
2029 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2030 TREE_TYPE (TREE_TYPE (fn
))))
2031 /* The return types differ. */
2034 /* Adjust the type of DECL in case FN is a static member. */
2035 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2036 if (DECL_STATIC_FUNCTION_P (fn
)
2037 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2038 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2040 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2043 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2047 if (templates
&& TREE_CHAIN (templates
))
2053 It is possible for a specialization with a given function
2054 signature to be instantiated from more than one function
2055 template. In such cases, explicit specification of the
2056 template arguments must be used to uniquely identify the
2057 function template specialization being specialized.
2059 Note that here, there's no suggestion that we're supposed to
2060 determine which of the candidate templates is most
2061 specialized. However, we, also have:
2065 Partial ordering of overloaded function template
2066 declarations is used in the following contexts to select
2067 the function template to which a function template
2068 specialization refers:
2070 -- when an explicit specialization refers to a function
2073 So, we do use the partial ordering rules, at least for now.
2074 This extension can only serve to make invalid programs valid,
2075 so it's safe. And, there is strong anecdotal evidence that
2076 the committee intended the partial ordering rules to apply;
2077 the EDG front end has that behavior, and John Spicer claims
2078 that the committee simply forgot to delete the wording in
2079 [temp.expl.spec]. */
2080 tree tmpl
= most_specialized_instantiation (templates
);
2081 if (tmpl
!= error_mark_node
)
2084 TREE_CHAIN (templates
) = NULL_TREE
;
2088 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2090 error ("template-id %qD for %q+D does not match any template "
2091 "declaration", template_id
, decl
);
2092 if (header_count
&& header_count
!= template_count
+ 1)
2093 inform (input_location
, "saw %d %<template<>%>, need %d for "
2094 "specializing a member function template",
2095 header_count
, template_count
+ 1);
2096 return error_mark_node
;
2098 else if ((templates
&& TREE_CHAIN (templates
))
2099 || (candidates
&& TREE_CHAIN (candidates
))
2100 || (templates
&& candidates
))
2102 error ("ambiguous template specialization %qD for %q+D",
2104 candidates
= chainon (candidates
, templates
);
2105 print_candidates (candidates
);
2106 return error_mark_node
;
2109 /* We have one, and exactly one, match. */
2112 tree fn
= TREE_VALUE (candidates
);
2113 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2114 /* DECL is a re-declaration or partial instantiation of a template
2116 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2118 /* It was a specialization of an ordinary member function in a
2120 return DECL_TI_TEMPLATE (fn
);
2123 /* It was a specialization of a template. */
2124 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2125 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2127 *targs_out
= copy_node (targs
);
2128 SET_TMPL_ARGS_LEVEL (*targs_out
,
2129 TMPL_ARGS_DEPTH (*targs_out
),
2130 TREE_PURPOSE (templates
));
2133 *targs_out
= TREE_PURPOSE (templates
);
2134 return TREE_VALUE (templates
);
2137 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2138 but with the default argument values filled in from those in the
2142 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2145 tree new_spec_types
;
2150 if (spec_types
== void_list_node
)
2151 return void_list_node
;
2153 /* Substitute into the rest of the list. */
2155 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2156 TREE_CHAIN (tmpl_types
));
2158 /* Add the default argument for this parameter. */
2159 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2160 TREE_VALUE (spec_types
),
2164 /* DECL is an explicit specialization. Replicate default arguments
2165 from the template it specializes. (That way, code like:
2167 template <class T> void f(T = 3);
2168 template <> void f(double);
2171 works, as required.) An alternative approach would be to look up
2172 the correct default arguments at the call-site, but this approach
2173 is consistent with how implicit instantiations are handled. */
2176 copy_default_args_to_explicit_spec (tree decl
)
2181 tree new_spec_types
;
2185 tree object_type
= NULL_TREE
;
2186 tree in_charge
= NULL_TREE
;
2187 tree vtt
= NULL_TREE
;
2189 /* See if there's anything we need to do. */
2190 tmpl
= DECL_TI_TEMPLATE (decl
);
2191 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2192 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2193 if (TREE_PURPOSE (t
))
2198 old_type
= TREE_TYPE (decl
);
2199 spec_types
= TYPE_ARG_TYPES (old_type
);
2201 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2203 /* Remove the this pointer, but remember the object's type for
2205 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2206 spec_types
= TREE_CHAIN (spec_types
);
2207 tmpl_types
= TREE_CHAIN (tmpl_types
);
2209 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2211 /* DECL may contain more parameters than TMPL due to the extra
2212 in-charge parameter in constructors and destructors. */
2213 in_charge
= spec_types
;
2214 spec_types
= TREE_CHAIN (spec_types
);
2216 if (DECL_HAS_VTT_PARM_P (decl
))
2219 spec_types
= TREE_CHAIN (spec_types
);
2223 /* Compute the merged default arguments. */
2225 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2227 /* Compute the new FUNCTION_TYPE. */
2231 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2236 /* Put the in-charge parameter back. */
2237 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2238 TREE_VALUE (in_charge
),
2241 new_type
= build_method_type_directly (object_type
,
2242 TREE_TYPE (old_type
),
2246 new_type
= build_function_type (TREE_TYPE (old_type
),
2248 new_type
= cp_build_type_attribute_variant (new_type
,
2249 TYPE_ATTRIBUTES (old_type
));
2250 new_type
= build_exception_variant (new_type
,
2251 TYPE_RAISES_EXCEPTIONS (old_type
));
2252 TREE_TYPE (decl
) = new_type
;
2255 /* Return the number of template headers we expect to see for a definition
2256 or specialization of CTYPE or one of its non-template members. */
2259 num_template_headers_for_class (tree ctype
)
2261 int num_templates
= 0;
2263 while (ctype
&& CLASS_TYPE_P (ctype
))
2265 /* You're supposed to have one `template <...>' for every
2266 template class, but you don't need one for a full
2267 specialization. For example:
2269 template <class T> struct S{};
2270 template <> struct S<int> { void f(); };
2271 void S<int>::f () {}
2273 is correct; there shouldn't be a `template <>' for the
2274 definition of `S<int>::f'. */
2275 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2276 /* If CTYPE does not have template information of any
2277 kind, then it is not a template, nor is it nested
2278 within a template. */
2280 if (explicit_class_specialization_p (ctype
))
2282 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2285 ctype
= TYPE_CONTEXT (ctype
);
2288 return num_templates
;
2291 /* Do a simple sanity check on the template headers that precede the
2292 variable declaration DECL. */
2295 check_template_variable (tree decl
)
2297 tree ctx
= CP_DECL_CONTEXT (decl
);
2298 int wanted
= num_template_headers_for_class (ctx
);
2299 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2300 permerror (DECL_SOURCE_LOCATION (decl
),
2301 "%qD is not a static data member of a class template", decl
);
2302 else if (template_header_count
> wanted
)
2304 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2305 "too many template headers for %D (should be %d)",
2307 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2308 inform (DECL_SOURCE_LOCATION (decl
),
2309 "members of an explicitly specialized class are defined "
2310 "without a template header");
2314 /* Check to see if the function just declared, as indicated in
2315 DECLARATOR, and in DECL, is a specialization of a function
2316 template. We may also discover that the declaration is an explicit
2317 instantiation at this point.
2319 Returns DECL, or an equivalent declaration that should be used
2320 instead if all goes well. Issues an error message if something is
2321 amiss. Returns error_mark_node if the error is not easily
2324 FLAGS is a bitmask consisting of the following flags:
2326 2: The function has a definition.
2327 4: The function is a friend.
2329 The TEMPLATE_COUNT is the number of references to qualifying
2330 template classes that appeared in the name of the function. For
2333 template <class T> struct S { void f(); };
2336 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2337 classes are not counted in the TEMPLATE_COUNT, so that in
2339 template <class T> struct S {};
2340 template <> struct S<int> { void f(); }
2341 template <> void S<int>::f();
2343 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2344 invalid; there should be no template <>.)
2346 If the function is a specialization, it is marked as such via
2347 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2348 is set up correctly, and it is added to the list of specializations
2349 for that template. */
2352 check_explicit_specialization (tree declarator
,
2357 int have_def
= flags
& 2;
2358 int is_friend
= flags
& 4;
2359 int specialization
= 0;
2360 int explicit_instantiation
= 0;
2361 int member_specialization
= 0;
2362 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2363 tree dname
= DECL_NAME (decl
);
2368 if (!processing_specialization
)
2371 tsk
= tsk_excessive_parms
;
2374 tsk
= current_tmpl_spec_kind (template_count
);
2379 if (processing_specialization
)
2382 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2384 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2387 /* This could be something like:
2389 template <class T> void f(T);
2390 class S { friend void f<>(int); } */
2394 /* This case handles bogus declarations like template <>
2395 template <class T> void f<int>(); */
2397 error ("template-id %qD in declaration of primary template",
2404 case tsk_invalid_member_spec
:
2405 /* The error has already been reported in
2406 check_specialization_scope. */
2407 return error_mark_node
;
2409 case tsk_invalid_expl_inst
:
2410 error ("template parameter list used in explicit instantiation");
2416 error ("definition provided for explicit instantiation");
2418 explicit_instantiation
= 1;
2421 case tsk_excessive_parms
:
2422 case tsk_insufficient_parms
:
2423 if (tsk
== tsk_excessive_parms
)
2424 error ("too many template parameter lists in declaration of %qD",
2426 else if (template_header_count
)
2427 error("too few template parameter lists in declaration of %qD", decl
);
2429 error("explicit specialization of %qD must be introduced by "
2430 "%<template <>%>", decl
);
2434 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2436 member_specialization
= 1;
2442 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2444 /* This case handles bogus declarations like template <>
2445 template <class T> void f<int>(); */
2447 if (uses_template_parms (declarator
))
2448 error ("function template partial specialization %qD "
2449 "is not allowed", declarator
);
2451 error ("template-id %qD in declaration of primary template",
2456 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2457 /* This is a specialization of a member template, without
2458 specialization the containing class. Something like:
2460 template <class T> struct S {
2461 template <class U> void f (U);
2463 template <> template <class U> void S<int>::f(U) {}
2465 That's a specialization -- but of the entire template. */
2473 if (specialization
|| member_specialization
)
2475 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2476 for (; t
; t
= TREE_CHAIN (t
))
2477 if (TREE_PURPOSE (t
))
2479 permerror (input_location
,
2480 "default argument specified in explicit specialization");
2485 if (specialization
|| member_specialization
|| explicit_instantiation
)
2487 tree tmpl
= NULL_TREE
;
2488 tree targs
= NULL_TREE
;
2490 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2491 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2495 gcc_assert (identifier_p (declarator
));
2500 /* If there is no class context, the explicit instantiation
2501 must be at namespace scope. */
2502 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2504 /* Find the namespace binding, using the declaration
2506 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2508 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2510 error ("%qD is not a template function", dname
);
2511 fns
= error_mark_node
;
2515 tree fn
= OVL_CURRENT (fns
);
2516 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2517 CP_DECL_CONTEXT (fn
)))
2518 error ("%qD is not declared in %qD",
2519 decl
, current_namespace
);
2523 declarator
= lookup_template_function (fns
, NULL_TREE
);
2526 if (declarator
== error_mark_node
)
2527 return error_mark_node
;
2529 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2531 if (!explicit_instantiation
)
2532 /* A specialization in class scope. This is invalid,
2533 but the error will already have been flagged by
2534 check_specialization_scope. */
2535 return error_mark_node
;
2538 /* It's not valid to write an explicit instantiation in
2541 class C { template void f(); }
2543 This case is caught by the parser. However, on
2546 template class C { void f(); };
2548 (which is invalid) we can get here. The error will be
2555 else if (ctype
!= NULL_TREE
2556 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2558 /* Find the list of functions in ctype that have the same
2559 name as the declared function. */
2560 tree name
= TREE_OPERAND (declarator
, 0);
2561 tree fns
= NULL_TREE
;
2564 if (constructor_name_p (name
, ctype
))
2566 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2568 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2569 : !CLASSTYPE_DESTRUCTORS (ctype
))
2571 /* From [temp.expl.spec]:
2573 If such an explicit specialization for the member
2574 of a class template names an implicitly-declared
2575 special member function (clause _special_), the
2576 program is ill-formed.
2578 Similar language is found in [temp.explicit]. */
2579 error ("specialization of implicitly-declared special member function");
2580 return error_mark_node
;
2583 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2586 if (!DECL_CONV_FN_P (decl
))
2588 idx
= lookup_fnfields_1 (ctype
, name
);
2590 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2594 vec
<tree
, va_gc
> *methods
;
2597 /* For a type-conversion operator, we cannot do a
2598 name-based lookup. We might be looking for `operator
2599 int' which will be a specialization of `operator T'.
2600 So, we find *all* the conversion operators, and then
2601 select from them. */
2604 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2606 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2607 methods
->iterate (idx
, &ovl
);
2610 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2611 /* There are no more conversion functions. */
2614 /* Glue all these conversion functions together
2615 with those we already have. */
2616 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2617 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2621 if (fns
== NULL_TREE
)
2623 error ("no member function %qD declared in %qT", name
, ctype
);
2624 return error_mark_node
;
2627 TREE_OPERAND (declarator
, 0) = fns
;
2630 /* Figure out what exactly is being specialized at this point.
2631 Note that for an explicit instantiation, even one for a
2632 member function, we cannot tell apriori whether the
2633 instantiation is for a member template, or just a member
2634 function of a template class. Even if a member template is
2635 being instantiated, the member template arguments may be
2636 elided if they can be deduced from the rest of the
2638 tmpl
= determine_specialization (declarator
, decl
,
2640 member_specialization
,
2644 if (!tmpl
|| tmpl
== error_mark_node
)
2645 /* We couldn't figure out what this declaration was
2647 return error_mark_node
;
2650 tree gen_tmpl
= most_general_template (tmpl
);
2652 if (explicit_instantiation
)
2654 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2655 is done by do_decl_instantiation later. */
2657 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2658 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2660 if (arg_depth
> parm_depth
)
2662 /* If TMPL is not the most general template (for
2663 example, if TMPL is a friend template that is
2664 injected into namespace scope), then there will
2665 be too many levels of TARGS. Remove some of them
2670 new_targs
= make_tree_vec (parm_depth
);
2671 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2672 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2673 = TREE_VEC_ELT (targs
, i
);
2677 return instantiate_template (tmpl
, targs
, tf_error
);
2680 /* If we thought that the DECL was a member function, but it
2681 turns out to be specializing a static member function,
2682 make DECL a static member function as well. */
2683 if (DECL_STATIC_FUNCTION_P (tmpl
)
2684 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2685 revert_static_member_fn (decl
);
2687 /* If this is a specialization of a member template of a
2688 template class, we want to return the TEMPLATE_DECL, not
2689 the specialization of it. */
2690 if (tsk
== tsk_template
)
2692 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2693 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2694 DECL_INITIAL (result
) = NULL_TREE
;
2698 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2699 DECL_SOURCE_LOCATION (result
)
2700 = DECL_SOURCE_LOCATION (decl
);
2701 /* We want to use the argument list specified in the
2702 definition, not in the original declaration. */
2703 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2704 for (parm
= DECL_ARGUMENTS (result
); parm
;
2705 parm
= DECL_CHAIN (parm
))
2706 DECL_CONTEXT (parm
) = result
;
2708 return register_specialization (tmpl
, gen_tmpl
, targs
,
2712 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2713 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2715 /* Inherit default function arguments from the template
2716 DECL is specializing. */
2717 copy_default_args_to_explicit_spec (decl
);
2719 /* This specialization has the same protection as the
2720 template it specializes. */
2721 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2722 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2724 /* 7.1.1-1 [dcl.stc]
2726 A storage-class-specifier shall not be specified in an
2727 explicit specialization...
2729 The parser rejects these, so unless action is taken here,
2730 explicit function specializations will always appear with
2733 The action recommended by the C++ CWG in response to C++
2734 defect report 605 is to make the storage class and linkage
2735 of the explicit specialization match the templated function:
2737 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2739 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2741 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2742 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2744 /* This specialization has the same linkage and visibility as
2745 the function template it specializes. */
2746 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2747 if (! TREE_PUBLIC (decl
))
2749 DECL_INTERFACE_KNOWN (decl
) = 1;
2750 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2752 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2753 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2755 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2756 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2760 /* If DECL is a friend declaration, declared using an
2761 unqualified name, the namespace associated with DECL may
2762 have been set incorrectly. For example, in:
2764 template <typename T> void f(T);
2766 struct S { friend void f<int>(int); }
2769 we will have set the DECL_CONTEXT for the friend
2770 declaration to N, rather than to the global namespace. */
2771 if (DECL_NAMESPACE_SCOPE_P (decl
))
2772 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2774 if (is_friend
&& !have_def
)
2775 /* This is not really a declaration of a specialization.
2776 It's just the name of an instantiation. But, it's not
2777 a request for an instantiation, either. */
2778 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2780 /* Register this specialization so that we can find it
2782 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2784 /* A 'structor should already have clones. */
2785 gcc_assert (decl
== error_mark_node
2786 || !(DECL_CONSTRUCTOR_P (decl
)
2787 || DECL_DESTRUCTOR_P (decl
))
2788 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
2795 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2796 parameters. These are represented in the same format used for
2797 DECL_TEMPLATE_PARMS. */
2800 comp_template_parms (const_tree parms1
, const_tree parms2
)
2805 if (parms1
== parms2
)
2808 for (p1
= parms1
, p2
= parms2
;
2809 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2810 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2812 tree t1
= TREE_VALUE (p1
);
2813 tree t2
= TREE_VALUE (p2
);
2816 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2817 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2819 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2822 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2824 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2825 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2827 /* If either of the template parameters are invalid, assume
2828 they match for the sake of error recovery. */
2829 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2832 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2835 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2836 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2837 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2839 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2844 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2845 /* One set of parameters has more parameters lists than the
2852 /* Determine whether PARM is a parameter pack. */
2855 template_parameter_pack_p (const_tree parm
)
2857 /* Determine if we have a non-type template parameter pack. */
2858 if (TREE_CODE (parm
) == PARM_DECL
)
2859 return (DECL_TEMPLATE_PARM_P (parm
)
2860 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2861 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2862 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2864 /* If this is a list of template parameters, we could get a
2865 TYPE_DECL or a TEMPLATE_DECL. */
2866 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2867 parm
= TREE_TYPE (parm
);
2869 /* Otherwise it must be a type template parameter. */
2870 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2871 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2872 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2875 /* Determine if T is a function parameter pack. */
2878 function_parameter_pack_p (const_tree t
)
2880 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2881 return DECL_PACK_P (t
);
2885 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2886 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2889 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2891 if (! primary_func_tmpl_inst
2892 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2893 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2896 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2899 /* Return true iff the function parameter PARAM_DECL was expanded
2900 from the function parameter pack PACK. */
2903 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2905 if (DECL_ARTIFICIAL (param_decl
)
2906 || !function_parameter_pack_p (pack
))
2909 /* The parameter pack and its pack arguments have the same
2911 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2914 /* Determine whether ARGS describes a variadic template args list,
2915 i.e., one that is terminated by a template argument pack. */
2918 template_args_variadic_p (tree args
)
2923 if (args
== NULL_TREE
)
2926 args
= INNERMOST_TEMPLATE_ARGS (args
);
2927 nargs
= TREE_VEC_LENGTH (args
);
2932 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2934 return ARGUMENT_PACK_P (last_parm
);
2937 /* Generate a new name for the parameter pack name NAME (an
2938 IDENTIFIER_NODE) that incorporates its */
2941 make_ith_pack_parameter_name (tree name
, int i
)
2943 /* Munge the name to include the parameter index. */
2944 #define NUMBUF_LEN 128
2945 char numbuf
[NUMBUF_LEN
];
2949 if (name
== NULL_TREE
)
2951 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2952 newname_len
= IDENTIFIER_LENGTH (name
)
2953 + strlen (numbuf
) + 2;
2954 newname
= (char*)alloca (newname_len
);
2955 snprintf (newname
, newname_len
,
2956 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2957 return get_identifier (newname
);
2960 /* Return true if T is a primary function, class or alias template
2964 primary_template_instantiation_p (const_tree t
)
2969 if (TREE_CODE (t
) == FUNCTION_DECL
)
2970 return DECL_LANG_SPECIFIC (t
)
2971 && DECL_TEMPLATE_INSTANTIATION (t
)
2972 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2973 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2974 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2975 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2976 else if (alias_template_specialization_p (t
))
2981 /* Return true if PARM is a template template parameter. */
2984 template_template_parameter_p (const_tree parm
)
2986 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2989 /* Return true iff PARM is a DECL representing a type template
2993 template_type_parameter_p (const_tree parm
)
2996 && (TREE_CODE (parm
) == TYPE_DECL
2997 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2998 && DECL_TEMPLATE_PARM_P (parm
));
3001 /* Return the template parameters of T if T is a
3002 primary template instantiation, NULL otherwise. */
3005 get_primary_template_innermost_parameters (const_tree t
)
3007 tree parms
= NULL
, template_info
= NULL
;
3009 if ((template_info
= get_template_info (t
))
3010 && primary_template_instantiation_p (t
))
3011 parms
= INNERMOST_TEMPLATE_PARMS
3012 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3017 /* Return the template parameters of the LEVELth level from the full list
3018 of template parameters PARMS. */
3021 get_template_parms_at_level (tree parms
, int level
)
3025 || TREE_CODE (parms
) != TREE_LIST
3026 || level
> TMPL_PARMS_DEPTH (parms
))
3029 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3030 if (TMPL_PARMS_DEPTH (p
) == level
)
3036 /* Returns the template arguments of T if T is a template instantiation,
3040 get_template_innermost_arguments (const_tree t
)
3042 tree args
= NULL
, template_info
= NULL
;
3044 if ((template_info
= get_template_info (t
))
3045 && TI_ARGS (template_info
))
3046 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3051 /* Return the argument pack elements of T if T is a template argument pack,
3055 get_template_argument_pack_elems (const_tree t
)
3057 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3058 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3061 return ARGUMENT_PACK_ARGS (t
);
3064 /* Structure used to track the progress of find_parameter_packs_r. */
3065 struct find_parameter_pack_data
3067 /* TREE_LIST that will contain all of the parameter packs found by
3069 tree
* parameter_packs
;
3071 /* Set of AST nodes that have been visited by the traversal. */
3072 struct pointer_set_t
*visited
;
3075 /* Identifies all of the argument packs that occur in a template
3076 argument and appends them to the TREE_LIST inside DATA, which is a
3077 find_parameter_pack_data structure. This is a subroutine of
3078 make_pack_expansion and uses_parameter_packs. */
3080 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3083 struct find_parameter_pack_data
* ppd
=
3084 (struct find_parameter_pack_data
*)data
;
3085 bool parameter_pack_p
= false;
3087 /* Handle type aliases/typedefs. */
3088 if (TYPE_ALIAS_P (t
))
3090 if (TYPE_TEMPLATE_INFO (t
))
3091 cp_walk_tree (&TYPE_TI_ARGS (t
),
3092 &find_parameter_packs_r
,
3098 /* Identify whether this is a parameter pack or not. */
3099 switch (TREE_CODE (t
))
3101 case TEMPLATE_PARM_INDEX
:
3102 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3103 parameter_pack_p
= true;
3106 case TEMPLATE_TYPE_PARM
:
3107 t
= TYPE_MAIN_VARIANT (t
);
3108 case TEMPLATE_TEMPLATE_PARM
:
3109 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3110 parameter_pack_p
= true;
3115 if (DECL_PACK_P (t
))
3117 /* We don't want to walk into the type of a PARM_DECL,
3118 because we don't want to see the type parameter pack. */
3120 parameter_pack_p
= true;
3124 /* Look through a lambda capture proxy to the field pack. */
3126 if (DECL_HAS_VALUE_EXPR_P (t
))
3128 tree v
= DECL_VALUE_EXPR (t
);
3130 &find_parameter_packs_r
,
3137 parameter_pack_p
= true;
3140 /* Not a parameter pack. */
3144 if (parameter_pack_p
)
3146 /* Add this parameter pack to the list. */
3147 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3151 cp_walk_tree (&TYPE_CONTEXT (t
),
3152 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3154 /* This switch statement will return immediately if we don't find a
3156 switch (TREE_CODE (t
))
3158 case TEMPLATE_PARM_INDEX
:
3161 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3162 /* Check the template itself. */
3163 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3164 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3165 /* Check the template arguments. */
3166 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3171 case TEMPLATE_TYPE_PARM
:
3172 case TEMPLATE_TEMPLATE_PARM
:
3179 if (TYPE_PTRMEMFUNC_P (t
))
3185 if (TYPE_TEMPLATE_INFO (t
))
3186 cp_walk_tree (&TYPE_TI_ARGS (t
),
3187 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3194 cp_walk_tree (&TREE_TYPE (t
),
3195 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3199 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3204 case TYPE_PACK_EXPANSION
:
3205 case EXPR_PACK_EXPANSION
:
3210 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3215 case IDENTIFIER_NODE
:
3216 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3228 /* Determines if the expression or type T uses any parameter packs. */
3230 uses_parameter_packs (tree t
)
3232 tree parameter_packs
= NULL_TREE
;
3233 struct find_parameter_pack_data ppd
;
3234 ppd
.parameter_packs
= ¶meter_packs
;
3235 ppd
.visited
= pointer_set_create ();
3236 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3237 pointer_set_destroy (ppd
.visited
);
3238 return parameter_packs
!= NULL_TREE
;
3241 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3242 representation a base-class initializer into a parameter pack
3243 expansion. If all goes well, the resulting node will be an
3244 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3247 make_pack_expansion (tree arg
)
3250 tree parameter_packs
= NULL_TREE
;
3251 bool for_types
= false;
3252 struct find_parameter_pack_data ppd
;
3254 if (!arg
|| arg
== error_mark_node
)
3257 if (TREE_CODE (arg
) == TREE_LIST
)
3259 /* The only time we will see a TREE_LIST here is for a base
3260 class initializer. In this case, the TREE_PURPOSE will be a
3261 _TYPE node (representing the base class expansion we're
3262 initializing) and the TREE_VALUE will be a TREE_LIST
3263 containing the initialization arguments.
3265 The resulting expansion looks somewhat different from most
3266 expansions. Rather than returning just one _EXPANSION, we
3267 return a TREE_LIST whose TREE_PURPOSE is a
3268 TYPE_PACK_EXPANSION containing the bases that will be
3269 initialized. The TREE_VALUE will be identical to the
3270 original TREE_VALUE, which is a list of arguments that will
3271 be passed to each base. We do not introduce any new pack
3272 expansion nodes into the TREE_VALUE (although it is possible
3273 that some already exist), because the TREE_PURPOSE and
3274 TREE_VALUE all need to be expanded together with the same
3275 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3276 resulting TREE_PURPOSE will mention the parameter packs in
3277 both the bases and the arguments to the bases. */
3280 tree parameter_packs
= NULL_TREE
;
3282 /* Determine which parameter packs will be used by the base
3284 ppd
.visited
= pointer_set_create ();
3285 ppd
.parameter_packs
= ¶meter_packs
;
3286 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3289 if (parameter_packs
== NULL_TREE
)
3291 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3292 pointer_set_destroy (ppd
.visited
);
3293 return error_mark_node
;
3296 if (TREE_VALUE (arg
) != void_type_node
)
3298 /* Collect the sets of parameter packs used in each of the
3299 initialization arguments. */
3300 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3302 /* Determine which parameter packs will be expanded in this
3304 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3309 pointer_set_destroy (ppd
.visited
);
3311 /* Create the pack expansion type for the base type. */
3312 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3313 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3314 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3316 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3317 they will rarely be compared to anything. */
3318 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3320 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3323 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3326 /* Build the PACK_EXPANSION_* node. */
3328 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3329 : make_node (EXPR_PACK_EXPANSION
);
3330 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3331 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3333 /* Propagate type and const-expression information. */
3334 TREE_TYPE (result
) = TREE_TYPE (arg
);
3335 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3338 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3339 they will rarely be compared to anything. */
3340 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3342 /* Determine which parameter packs will be expanded. */
3343 ppd
.parameter_packs
= ¶meter_packs
;
3344 ppd
.visited
= pointer_set_create ();
3345 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3346 pointer_set_destroy (ppd
.visited
);
3348 /* Make sure we found some parameter packs. */
3349 if (parameter_packs
== NULL_TREE
)
3352 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3354 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3355 return error_mark_node
;
3357 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3359 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3364 /* Checks T for any "bare" parameter packs, which have not yet been
3365 expanded, and issues an error if any are found. This operation can
3366 only be done on full expressions or types (e.g., an expression
3367 statement, "if" condition, etc.), because we could have expressions like:
3369 foo(f(g(h(args)))...)
3371 where "args" is a parameter pack. check_for_bare_parameter_packs
3372 should not be called for the subexpressions args, h(args),
3373 g(h(args)), or f(g(h(args))), because we would produce erroneous
3376 Returns TRUE and emits an error if there were bare parameter packs,
3377 returns FALSE otherwise. */
3379 check_for_bare_parameter_packs (tree t
)
3381 tree parameter_packs
= NULL_TREE
;
3382 struct find_parameter_pack_data ppd
;
3384 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3387 if (TREE_CODE (t
) == TYPE_DECL
)
3390 ppd
.parameter_packs
= ¶meter_packs
;
3391 ppd
.visited
= pointer_set_create ();
3392 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3393 pointer_set_destroy (ppd
.visited
);
3395 if (parameter_packs
)
3397 error ("parameter packs not expanded with %<...%>:");
3398 while (parameter_packs
)
3400 tree pack
= TREE_VALUE (parameter_packs
);
3401 tree name
= NULL_TREE
;
3403 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3404 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3405 name
= TYPE_NAME (pack
);
3406 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3407 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3409 name
= DECL_NAME (pack
);
3412 inform (input_location
, " %qD", name
);
3414 inform (input_location
, " <anonymous>");
3416 parameter_packs
= TREE_CHAIN (parameter_packs
);
3425 /* Expand any parameter packs that occur in the template arguments in
3428 expand_template_argument_pack (tree args
)
3430 tree result_args
= NULL_TREE
;
3431 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3432 int num_result_args
= -1;
3433 int non_default_args_count
= -1;
3435 /* First, determine if we need to expand anything, and the number of
3436 slots we'll need. */
3437 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3439 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3440 if (arg
== NULL_TREE
)
3442 if (ARGUMENT_PACK_P (arg
))
3444 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3445 if (num_result_args
< 0)
3446 num_result_args
= in_arg
+ num_packed
;
3448 num_result_args
+= num_packed
;
3452 if (num_result_args
>= 0)
3457 /* If no expansion is necessary, we're done. */
3458 if (num_result_args
< 0)
3461 /* Expand arguments. */
3462 result_args
= make_tree_vec (num_result_args
);
3463 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3464 non_default_args_count
=
3465 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3466 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3468 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3469 if (ARGUMENT_PACK_P (arg
))
3471 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3472 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3473 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3474 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3475 if (non_default_args_count
> 0)
3476 non_default_args_count
+= num_packed
- 1;
3480 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3484 if (non_default_args_count
>= 0)
3485 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3489 /* Checks if DECL shadows a template parameter.
3491 [temp.local]: A template-parameter shall not be redeclared within its
3492 scope (including nested scopes).
3494 Emits an error and returns TRUE if the DECL shadows a parameter,
3495 returns FALSE otherwise. */
3498 check_template_shadow (tree decl
)
3502 /* If we're not in a template, we can't possibly shadow a template
3504 if (!current_template_parms
)
3507 /* Figure out what we're shadowing. */
3508 if (TREE_CODE (decl
) == OVERLOAD
)
3509 decl
= OVL_CURRENT (decl
);
3510 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3512 /* If there's no previous binding for this name, we're not shadowing
3513 anything, let alone a template parameter. */
3517 /* If we're not shadowing a template parameter, we're done. Note
3518 that OLDDECL might be an OVERLOAD (or perhaps even an
3519 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3521 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3524 /* We check for decl != olddecl to avoid bogus errors for using a
3525 name inside a class. We check TPFI to avoid duplicate errors for
3526 inline member templates. */
3528 || (DECL_TEMPLATE_PARM_P (decl
)
3529 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3532 /* Don't complain about the injected class name, as we've already
3533 complained about the class itself. */
3534 if (DECL_SELF_REFERENCE_P (decl
))
3537 error ("declaration of %q+#D", decl
);
3538 error (" shadows template parm %q+#D", olddecl
);
3542 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3543 ORIG_LEVEL, DECL, and TYPE. */
3546 build_template_parm_index (int index
,
3552 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3553 TEMPLATE_PARM_IDX (t
) = index
;
3554 TEMPLATE_PARM_LEVEL (t
) = level
;
3555 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3556 TEMPLATE_PARM_DECL (t
) = decl
;
3557 TREE_TYPE (t
) = type
;
3558 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3559 TREE_READONLY (t
) = TREE_READONLY (decl
);
3564 /* Find the canonical type parameter for the given template type
3565 parameter. Returns the canonical type parameter, which may be TYPE
3566 if no such parameter existed. */
3569 canonical_type_parameter (tree type
)
3572 int idx
= TEMPLATE_TYPE_IDX (type
);
3573 if (!canonical_template_parms
)
3574 vec_alloc (canonical_template_parms
, idx
+1);
3576 while (canonical_template_parms
->length () <= (unsigned)idx
)
3577 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3579 list
= (*canonical_template_parms
)[idx
];
3580 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3581 list
= TREE_CHAIN (list
);
3584 return TREE_VALUE (list
);
3587 (*canonical_template_parms
)[idx
]
3588 = tree_cons (NULL_TREE
, type
,
3589 (*canonical_template_parms
)[idx
]);
3594 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3595 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3596 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3597 new one is created. */
3600 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3601 tsubst_flags_t complain
)
3603 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3604 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3605 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3606 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3608 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3611 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3612 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3613 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3614 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3615 DECL_ARTIFICIAL (decl
) = 1;
3616 SET_DECL_TEMPLATE_PARM_P (decl
);
3618 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3619 TEMPLATE_PARM_LEVEL (index
) - levels
,
3620 TEMPLATE_PARM_ORIG_LEVEL (index
),
3622 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3623 TEMPLATE_PARM_PARAMETER_PACK (t
)
3624 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3626 /* Template template parameters need this. */
3627 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3628 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3629 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3633 return TEMPLATE_PARM_DESCENDANTS (index
);
3636 /* Process information from new template parameter PARM and append it
3637 to the LIST being built. This new parameter is a non-type
3638 parameter iff IS_NON_TYPE is true. This new parameter is a
3639 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3640 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3641 parameter list PARM belongs to. This is used used to create a
3642 proper canonical type for the type of PARM that is to be created,
3643 iff PARM is a type. If the size is not known, this parameter shall
3647 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3648 bool is_non_type
, bool is_parameter_pack
)
3655 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3656 defval
= TREE_PURPOSE (parm
);
3660 tree p
= tree_last (list
);
3662 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3665 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3666 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3668 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3678 parm
= TREE_VALUE (parm
);
3680 SET_DECL_TEMPLATE_PARM_P (parm
);
3682 if (TREE_TYPE (parm
) == error_mark_node
)
3684 err_parm_list
= build_tree_list (defval
, parm
);
3685 TREE_VALUE (err_parm_list
) = error_mark_node
;
3686 return chainon (list
, err_parm_list
);
3692 The top-level cv-qualifiers on the template-parameter are
3693 ignored when determining its type. */
3694 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3695 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3697 err_parm_list
= build_tree_list (defval
, parm
);
3698 TREE_VALUE (err_parm_list
) = error_mark_node
;
3699 return chainon (list
, err_parm_list
);
3702 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
3703 /* If we're in a nested template parameter list, the template
3704 template parameter could be a parameter pack. */
3705 && processing_template_parmlist
== 1)
3707 /* This template parameter is not a parameter pack, but it
3708 should be. Complain about "bare" parameter packs. */
3709 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3711 /* Recover by calling this a parameter pack. */
3712 is_parameter_pack
= true;
3716 /* A template parameter is not modifiable. */
3717 TREE_CONSTANT (parm
) = 1;
3718 TREE_READONLY (parm
) = 1;
3719 decl
= build_decl (parm_loc
,
3720 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3721 TREE_CONSTANT (decl
) = 1;
3722 TREE_READONLY (decl
) = 1;
3723 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3724 = build_template_parm_index (idx
, processing_template_decl
,
3725 processing_template_decl
,
3726 decl
, TREE_TYPE (parm
));
3728 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3729 = is_parameter_pack
;
3734 parm
= TREE_VALUE (TREE_VALUE (parm
));
3736 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3738 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3739 /* This is for distinguishing between real templates and template
3740 template parameters */
3741 TREE_TYPE (parm
) = t
;
3742 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3747 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3748 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3749 decl
= build_decl (parm_loc
,
3750 TYPE_DECL
, parm
, t
);
3753 TYPE_NAME (t
) = decl
;
3754 TYPE_STUB_DECL (t
) = decl
;
3756 TEMPLATE_TYPE_PARM_INDEX (t
)
3757 = build_template_parm_index (idx
, processing_template_decl
,
3758 processing_template_decl
,
3759 decl
, TREE_TYPE (parm
));
3760 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3761 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3763 DECL_ARTIFICIAL (decl
) = 1;
3764 SET_DECL_TEMPLATE_PARM_P (decl
);
3766 parm
= build_tree_list (defval
, parm
);
3767 return chainon (list
, parm
);
3770 /* The end of a template parameter list has been reached. Process the
3771 tree list into a parameter vector, converting each parameter into a more
3772 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3776 end_template_parm_list (tree parms
)
3780 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3782 current_template_parms
3783 = tree_cons (size_int (processing_template_decl
),
3784 saved_parmlist
, current_template_parms
);
3786 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3788 next
= TREE_CHAIN (parm
);
3789 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3790 TREE_CHAIN (parm
) = NULL_TREE
;
3793 --processing_template_parmlist
;
3795 return saved_parmlist
;
3798 /* end_template_decl is called after a template declaration is seen. */
3801 end_template_decl (void)
3803 reset_specialization ();
3805 if (! processing_template_decl
)
3808 /* This matches the pushlevel in begin_template_parm_list. */
3811 --processing_template_decl
;
3812 current_template_parms
= TREE_CHAIN (current_template_parms
);
3815 /* Takes a TREE_LIST representing a template parameter and convert it
3816 into an argument suitable to be passed to the type substitution
3817 functions. Note that If the TREE_LIST contains an error_mark
3818 node, the returned argument is error_mark_node. */
3821 template_parm_to_arg (tree t
)
3825 || TREE_CODE (t
) != TREE_LIST
)
3828 if (error_operand_p (TREE_VALUE (t
)))
3829 return error_mark_node
;
3833 if (TREE_CODE (t
) == TYPE_DECL
3834 || TREE_CODE (t
) == TEMPLATE_DECL
)
3838 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3840 /* Turn this argument into a TYPE_ARGUMENT_PACK
3841 with a single element, which expands T. */
3842 tree vec
= make_tree_vec (1);
3843 #ifdef ENABLE_CHECKING
3844 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3845 (vec
, TREE_VEC_LENGTH (vec
));
3847 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3849 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3850 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3855 t
= DECL_INITIAL (t
);
3857 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3859 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3860 with a single element, which expands T. */
3861 tree vec
= make_tree_vec (1);
3862 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3863 #ifdef ENABLE_CHECKING
3864 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3865 (vec
, TREE_VEC_LENGTH (vec
));
3867 t
= convert_from_reference (t
);
3868 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3870 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3871 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3872 TREE_TYPE (t
) = type
;
3875 t
= convert_from_reference (t
);
3880 /* Given a set of template parameters, return them as a set of template
3881 arguments. The template parameters are represented as a TREE_VEC, in
3882 the form documented in cp-tree.h for template arguments. */
3885 template_parms_to_args (tree parms
)
3888 tree args
= NULL_TREE
;
3889 int length
= TMPL_PARMS_DEPTH (parms
);
3892 /* If there is only one level of template parameters, we do not
3893 create a TREE_VEC of TREE_VECs. Instead, we return a single
3894 TREE_VEC containing the arguments. */
3896 args
= make_tree_vec (length
);
3898 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3900 tree a
= copy_node (TREE_VALUE (header
));
3903 TREE_TYPE (a
) = NULL_TREE
;
3904 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3905 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3907 #ifdef ENABLE_CHECKING
3908 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3912 TREE_VEC_ELT (args
, --l
) = a
;
3917 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3918 /* This can happen for template parms of a template template
3921 template<template<class T, class U> class TT> struct S;
3923 Consider the level of the parms of TT; T and U both have
3924 level 2; TT has no template parm of level 1. So in this case
3925 the first element of full_template_args is NULL_TREE. If we
3926 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3927 of 2. This will make tsubst wrongly consider that T and U
3928 have level 1. Instead, let's create a dummy vector as the
3929 first element of full_template_args so that TMPL_ARGS_DEPTH
3930 returns the correct depth for args. */
3931 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3935 /* Within the declaration of a template, return the currently active
3936 template parameters as an argument TREE_VEC. */
3939 current_template_args (void)
3941 return template_parms_to_args (current_template_parms
);
3944 /* Update the declared TYPE by doing any lookups which were thought to be
3945 dependent, but are not now that we know the SCOPE of the declarator. */
3948 maybe_update_decl_type (tree orig_type
, tree scope
)
3950 tree type
= orig_type
;
3952 if (type
== NULL_TREE
)
3955 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3956 type
= TREE_TYPE (type
);
3958 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3959 && dependent_type_p (type
)
3960 /* Don't bother building up the args in this case. */
3961 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3963 /* tsubst in the args corresponding to the template parameters,
3964 including auto if present. Most things will be unchanged, but
3965 make_typename_type and tsubst_qualified_id will resolve
3966 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3967 tree args
= current_template_args ();
3968 tree auto_node
= type_uses_auto (type
);
3972 tree auto_vec
= make_tree_vec (1);
3973 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3974 args
= add_to_template_args (args
, auto_vec
);
3976 pushed
= push_scope (scope
);
3977 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3982 if (type
== error_mark_node
)
3985 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3987 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3990 type
= TYPE_NAME (type
);
3995 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3996 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3997 a member template. Used by push_template_decl below. */
4000 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4002 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4003 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4004 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4005 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4006 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4011 struct template_parm_data
4013 /* The level of the template parameters we are currently
4017 /* The index of the specialization argument we are currently
4021 /* An array whose size is the number of template parameters. The
4022 elements are nonzero if the parameter has been used in any one
4023 of the arguments processed so far. */
4026 /* An array whose size is the number of template arguments. The
4027 elements are nonzero if the argument makes use of template
4028 parameters of this level. */
4029 int* arg_uses_template_parms
;
4032 /* Subroutine of push_template_decl used to see if each template
4033 parameter in a partial specialization is used in the explicit
4034 argument list. If T is of the LEVEL given in DATA (which is
4035 treated as a template_parm_data*), then DATA->PARMS is marked
4039 mark_template_parm (tree t
, void* data
)
4043 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4045 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4047 level
= TEMPLATE_PARM_LEVEL (t
);
4048 idx
= TEMPLATE_PARM_IDX (t
);
4052 level
= TEMPLATE_TYPE_LEVEL (t
);
4053 idx
= TEMPLATE_TYPE_IDX (t
);
4056 if (level
== tpd
->level
)
4058 tpd
->parms
[idx
] = 1;
4059 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4062 /* Return zero so that for_each_template_parm will continue the
4063 traversal of the tree; we want to mark *every* template parm. */
4067 /* Process the partial specialization DECL. */
4070 process_partial_specialization (tree decl
)
4072 tree type
= TREE_TYPE (decl
);
4073 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4074 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4075 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4076 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4079 int nargs
= TREE_VEC_LENGTH (inner_args
);
4082 bool did_error_intro
= false;
4083 struct template_parm_data tpd
;
4084 struct template_parm_data tpd2
;
4086 gcc_assert (current_template_parms
);
4088 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4089 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4091 /* We check that each of the template parameters given in the
4092 partial specialization is used in the argument list to the
4093 specialization. For example:
4095 template <class T> struct S;
4096 template <class T> struct S<T*>;
4098 The second declaration is OK because `T*' uses the template
4099 parameter T, whereas
4101 template <class T> struct S<int>;
4103 is no good. Even trickier is:
4114 The S2<T> declaration is actually invalid; it is a
4115 full-specialization. Of course,
4118 struct S2<T (*)(U)>;
4120 or some such would have been OK. */
4121 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4122 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4123 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4125 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4126 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4127 for (i
= 0; i
< nargs
; ++i
)
4129 tpd
.current_arg
= i
;
4130 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4131 &mark_template_parm
,
4134 /*include_nondeduced_p=*/false);
4136 for (i
= 0; i
< ntparms
; ++i
)
4137 if (tpd
.parms
[i
] == 0)
4139 /* One of the template parms was not used in the
4141 if (!did_error_intro
)
4143 error ("template parameters not used in partial specialization:");
4144 did_error_intro
= true;
4147 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4150 if (did_error_intro
)
4151 return error_mark_node
;
4153 /* [temp.class.spec]
4155 The argument list of the specialization shall not be identical to
4156 the implicit argument list of the primary template. */
4157 if (comp_template_args
4159 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4161 error ("partial specialization %qT does not specialize any template arguments", type
);
4163 /* A partial specialization that replaces multiple parameters of the
4164 primary template with a pack expansion is less specialized for those
4166 if (nargs
< DECL_NTPARMS (maintmpl
))
4168 error ("partial specialization is not more specialized than the "
4169 "primary template because it replaces multiple parameters "
4170 "with a pack expansion");
4171 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4175 /* [temp.class.spec]
4177 A partially specialized non-type argument expression shall not
4178 involve template parameters of the partial specialization except
4179 when the argument expression is a simple identifier.
4181 The type of a template parameter corresponding to a specialized
4182 non-type argument shall not be dependent on a parameter of the
4185 Also, we verify that pack expansions only occur at the
4186 end of the argument list. */
4187 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4189 for (i
= 0; i
< nargs
; ++i
)
4191 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4192 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4193 tree packed_args
= NULL_TREE
;
4196 if (ARGUMENT_PACK_P (arg
))
4198 /* Extract the arguments from the argument pack. We'll be
4199 iterating over these in the following loop. */
4200 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4201 len
= TREE_VEC_LENGTH (packed_args
);
4204 for (j
= 0; j
< len
; j
++)
4207 /* Get the Jth argument in the parameter pack. */
4208 arg
= TREE_VEC_ELT (packed_args
, j
);
4210 if (PACK_EXPANSION_P (arg
))
4212 /* Pack expansions must come at the end of the
4214 if ((packed_args
&& j
< len
- 1)
4215 || (!packed_args
&& i
< nargs
- 1))
4217 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4218 error ("parameter pack argument %qE must be at the "
4219 "end of the template argument list", arg
);
4221 error ("parameter pack argument %qT must be at the "
4222 "end of the template argument list", arg
);
4226 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4227 /* We only care about the pattern. */
4228 arg
= PACK_EXPANSION_PATTERN (arg
);
4230 if (/* These first two lines are the `non-type' bit. */
4232 && TREE_CODE (arg
) != TEMPLATE_DECL
4233 /* This next two lines are the `argument expression is not just a
4234 simple identifier' condition and also the `specialized
4235 non-type argument' bit. */
4236 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4237 && !(REFERENCE_REF_P (arg
)
4238 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4240 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4241 || (packed_args
&& uses_template_parms (arg
)))
4242 error ("template argument %qE involves template parameter(s)",
4246 /* Look at the corresponding template parameter,
4247 marking which template parameters its type depends
4249 tree type
= TREE_TYPE (parm
);
4253 /* We haven't yet initialized TPD2. Do so now. */
4254 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4255 /* The number of parameters here is the number in the
4256 main template, which, as checked in the assertion
4258 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4260 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4263 /* Mark the template parameters. But this time, we're
4264 looking for the template parameters of the main
4265 template, not in the specialization. */
4266 tpd2
.current_arg
= i
;
4267 tpd2
.arg_uses_template_parms
[i
] = 0;
4268 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4269 for_each_template_parm (type
,
4270 &mark_template_parm
,
4273 /*include_nondeduced_p=*/false);
4275 if (tpd2
.arg_uses_template_parms
[i
])
4277 /* The type depended on some template parameters.
4278 If they are fully specialized in the
4279 specialization, that's OK. */
4282 for (j
= 0; j
< nargs
; ++j
)
4283 if (tpd2
.parms
[j
] != 0
4284 && tpd
.arg_uses_template_parms
[j
])
4287 error_n (input_location
, count
,
4288 "type %qT of template argument %qE depends "
4289 "on a template parameter",
4290 "type %qT of template argument %qE depends "
4291 "on template parameters",
4300 /* We should only get here once. */
4301 gcc_assert (!COMPLETE_TYPE_P (type
));
4303 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4304 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4305 TREE_TYPE (tmpl
) = type
;
4306 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4307 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4308 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4309 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4311 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4312 = tree_cons (specargs
, tmpl
,
4313 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4314 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4316 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4317 inst
= TREE_CHAIN (inst
))
4319 tree inst_type
= TREE_VALUE (inst
);
4320 if (COMPLETE_TYPE_P (inst_type
)
4321 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4323 tree spec
= most_specialized_class (inst_type
, tf_none
);
4324 if (spec
&& TREE_TYPE (spec
) == type
)
4325 permerror (input_location
,
4326 "partial specialization of %qT after instantiation "
4327 "of %qT", type
, inst_type
);
4334 /* PARM is a template parameter of some form; return the corresponding
4335 TEMPLATE_PARM_INDEX. */
4338 get_template_parm_index (tree parm
)
4340 if (TREE_CODE (parm
) == PARM_DECL
4341 || TREE_CODE (parm
) == CONST_DECL
)
4342 parm
= DECL_INITIAL (parm
);
4343 else if (TREE_CODE (parm
) == TYPE_DECL
4344 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4345 parm
= TREE_TYPE (parm
);
4346 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4347 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4348 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4349 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4353 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4354 parameter packs used by the template parameter PARM. */
4357 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4359 /* A type parm can't refer to another parm. */
4360 if (TREE_CODE (parm
) == TYPE_DECL
)
4362 else if (TREE_CODE (parm
) == PARM_DECL
)
4364 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4369 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4371 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4372 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4373 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4376 /* PARM is a template parameter pack. Return any parameter packs used in
4377 its type or the type of any of its template parameters. If there are
4378 any such packs, it will be instantiated into a fixed template parameter
4379 list by partial instantiation rather than be fully deduced. */
4382 fixed_parameter_pack_p (tree parm
)
4384 /* This can only be true in a member template. */
4385 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4387 /* This can only be true for a parameter pack. */
4388 if (!template_parameter_pack_p (parm
))
4390 /* A type parm can't refer to another parm. */
4391 if (TREE_CODE (parm
) == TYPE_DECL
)
4394 tree parameter_packs
= NULL_TREE
;
4395 struct find_parameter_pack_data ppd
;
4396 ppd
.parameter_packs
= ¶meter_packs
;
4397 ppd
.visited
= pointer_set_create ();
4399 fixed_parameter_pack_p_1 (parm
, &ppd
);
4401 pointer_set_destroy (ppd
.visited
);
4402 return parameter_packs
;
4405 /* Check that a template declaration's use of default arguments and
4406 parameter packs is not invalid. Here, PARMS are the template
4407 parameters. IS_PRIMARY is true if DECL is the thing declared by
4408 a primary template. IS_PARTIAL is true if DECL is a partial
4411 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4412 declaration (but not a definition); 1 indicates a declaration, 2
4413 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4414 emitted for extraneous default arguments.
4416 Returns TRUE if there were no errors found, FALSE otherwise. */
4419 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4420 bool is_partial
, int is_friend_decl
)
4423 int last_level_to_check
;
4425 bool no_errors
= true;
4429 A default template-argument shall not be specified in a
4430 function template declaration or a function template definition, nor
4431 in the template-parameter-list of the definition of a member of a
4434 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4435 /* You can't have a function template declaration in a local
4436 scope, nor you can you define a member of a class template in a
4440 if (TREE_CODE (decl
) == TYPE_DECL
4442 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4443 /* A lambda doesn't have an explicit declaration; don't complain
4444 about the parms of the enclosing class. */
4447 if (current_class_type
4448 && !TYPE_BEING_DEFINED (current_class_type
)
4449 && DECL_LANG_SPECIFIC (decl
)
4450 && DECL_DECLARES_FUNCTION_P (decl
)
4451 /* If this is either a friend defined in the scope of the class
4452 or a member function. */
4453 && (DECL_FUNCTION_MEMBER_P (decl
)
4454 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4455 : DECL_FRIEND_CONTEXT (decl
)
4456 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4458 /* And, if it was a member function, it really was defined in
4459 the scope of the class. */
4460 && (!DECL_FUNCTION_MEMBER_P (decl
)
4461 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4462 /* We already checked these parameters when the template was
4463 declared, so there's no need to do it again now. This function
4464 was defined in class scope, but we're processing its body now
4465 that the class is complete. */
4468 /* Core issue 226 (C++0x only): the following only applies to class
4471 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4475 If a template-parameter has a default template-argument, all
4476 subsequent template-parameters shall have a default
4477 template-argument supplied. */
4478 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4480 tree inner_parms
= TREE_VALUE (parm_level
);
4481 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4482 int seen_def_arg_p
= 0;
4485 for (i
= 0; i
< ntparms
; ++i
)
4487 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4489 if (parm
== error_mark_node
)
4492 if (TREE_PURPOSE (parm
))
4494 else if (seen_def_arg_p
4495 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4497 error ("no default argument for %qD", TREE_VALUE (parm
));
4498 /* For better subsequent error-recovery, we indicate that
4499 there should have been a default argument. */
4500 TREE_PURPOSE (parm
) = error_mark_node
;
4503 else if (!is_partial
4505 /* Don't complain about an enclosing partial
4507 && parm_level
== parms
4508 && TREE_CODE (decl
) == TYPE_DECL
4510 && template_parameter_pack_p (TREE_VALUE (parm
))
4511 /* A fixed parameter pack will be partially
4512 instantiated into a fixed length list. */
4513 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4515 /* A primary class template can only have one
4516 parameter pack, at the end of the template
4519 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4520 error ("parameter pack %qE must be at the end of the"
4521 " template parameter list", TREE_VALUE (parm
));
4523 error ("parameter pack %qT must be at the end of the"
4524 " template parameter list",
4525 TREE_TYPE (TREE_VALUE (parm
)));
4527 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4535 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4539 /* For an ordinary class template, default template arguments are
4540 allowed at the innermost level, e.g.:
4541 template <class T = int>
4543 but, in a partial specialization, they're not allowed even
4544 there, as we have in [temp.class.spec]:
4546 The template parameter list of a specialization shall not
4547 contain default template argument values.
4549 So, for a partial specialization, or for a function template
4550 (in C++98/C++03), we look at all of them. */
4553 /* But, for a primary class template that is not a partial
4554 specialization we look at all template parameters except the
4556 parms
= TREE_CHAIN (parms
);
4558 /* Figure out what error message to issue. */
4559 if (is_friend_decl
== 2)
4560 msg
= G_("default template arguments may not be used in function template "
4561 "friend re-declaration");
4562 else if (is_friend_decl
)
4563 msg
= G_("default template arguments may not be used in function template "
4564 "friend declarations");
4565 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4566 msg
= G_("default template arguments may not be used in function templates "
4567 "without -std=c++11 or -std=gnu++11");
4568 else if (is_partial
)
4569 msg
= G_("default template arguments may not be used in "
4570 "partial specializations");
4572 msg
= G_("default argument for template parameter for class enclosing %qD");
4574 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4575 /* If we're inside a class definition, there's no need to
4576 examine the parameters to the class itself. On the one
4577 hand, they will be checked when the class is defined, and,
4578 on the other, default arguments are valid in things like:
4579 template <class T = double>
4580 struct S { template <class U> void f(U); };
4581 Here the default argument for `S' has no bearing on the
4582 declaration of `f'. */
4583 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4585 /* Check everything. */
4586 last_level_to_check
= 0;
4588 for (parm_level
= parms
;
4589 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4590 parm_level
= TREE_CHAIN (parm_level
))
4592 tree inner_parms
= TREE_VALUE (parm_level
);
4596 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4597 for (i
= 0; i
< ntparms
; ++i
)
4599 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4602 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4607 if (is_friend_decl
== 2)
4614 /* Clear out the default argument so that we are not
4616 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4620 /* At this point, if we're still interested in issuing messages,
4621 they must apply to classes surrounding the object declared. */
4623 msg
= G_("default argument for template parameter for class "
4630 /* Worker for push_template_decl_real, called via
4631 for_each_template_parm. DATA is really an int, indicating the
4632 level of the parameters we are interested in. If T is a template
4633 parameter of that level, return nonzero. */
4636 template_parm_this_level_p (tree t
, void* data
)
4638 int this_level
= *(int *)data
;
4641 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4642 level
= TEMPLATE_PARM_LEVEL (t
);
4644 level
= TEMPLATE_TYPE_LEVEL (t
);
4645 return level
== this_level
;
4648 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4649 parameters given by current_template_args, or reuses a
4650 previously existing one, if appropriate. Returns the DECL, or an
4651 equivalent one, if it is replaced via a call to duplicate_decls.
4653 If IS_FRIEND is true, DECL is a friend declaration. */
4656 push_template_decl_real (tree decl
, bool is_friend
)
4664 int new_template_p
= 0;
4665 /* True if the template is a member template, in the sense of
4667 bool member_template_p
= false;
4669 if (decl
== error_mark_node
|| !current_template_parms
)
4670 return error_mark_node
;
4672 /* See if this is a partial specialization. */
4673 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4674 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4675 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4677 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4681 /* For a friend, we want the context of the friend function, not
4682 the type of which it is a friend. */
4683 ctx
= CP_DECL_CONTEXT (decl
);
4684 else if (CP_DECL_CONTEXT (decl
)
4685 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4686 /* In the case of a virtual function, we want the class in which
4688 ctx
= CP_DECL_CONTEXT (decl
);
4690 /* Otherwise, if we're currently defining some class, the DECL
4691 is assumed to be a member of the class. */
4692 ctx
= current_scope ();
4694 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4697 if (!DECL_CONTEXT (decl
))
4698 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4700 /* See if this is a primary template. */
4701 if (is_friend
&& ctx
4702 && uses_template_parms_level (ctx
, processing_template_decl
))
4703 /* A friend template that specifies a class context, i.e.
4704 template <typename T> friend void A<T>::f();
4708 is_primary
= template_parm_scope_p ();
4712 if (DECL_CLASS_SCOPE_P (decl
))
4713 member_template_p
= true;
4714 if (TREE_CODE (decl
) == TYPE_DECL
4715 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4717 error ("template class without a name");
4718 return error_mark_node
;
4720 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4722 if (DECL_DESTRUCTOR_P (decl
))
4726 A destructor shall not be a member template. */
4727 error ("destructor %qD declared as member template", decl
);
4728 return error_mark_node
;
4730 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4731 && (!prototype_p (TREE_TYPE (decl
))
4732 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4733 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4734 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4735 == void_list_node
)))
4737 /* [basic.stc.dynamic.allocation]
4739 An allocation function can be a function
4740 template. ... Template allocation functions shall
4741 have two or more parameters. */
4742 error ("invalid template declaration of %qD", decl
);
4743 return error_mark_node
;
4746 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4747 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4749 else if (TREE_CODE (decl
) == TYPE_DECL
4750 && TYPE_DECL_ALIAS_P (decl
))
4751 /* alias-declaration */
4752 gcc_assert (!DECL_ARTIFICIAL (decl
));
4755 error ("template declaration of %q#D", decl
);
4756 return error_mark_node
;
4760 /* Check to see that the rules regarding the use of default
4761 arguments are not being violated. */
4762 check_default_tmpl_args (decl
, current_template_parms
,
4763 is_primary
, is_partial
, /*is_friend_decl=*/0);
4765 /* Ensure that there are no parameter packs in the type of this
4766 declaration that have not been expanded. */
4767 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4769 /* Check each of the arguments individually to see if there are
4770 any bare parameter packs. */
4771 tree type
= TREE_TYPE (decl
);
4772 tree arg
= DECL_ARGUMENTS (decl
);
4773 tree argtype
= TYPE_ARG_TYPES (type
);
4775 while (arg
&& argtype
)
4777 if (!DECL_PACK_P (arg
)
4778 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4780 /* This is a PARM_DECL that contains unexpanded parameter
4781 packs. We have already complained about this in the
4782 check_for_bare_parameter_packs call, so just replace
4783 these types with ERROR_MARK_NODE. */
4784 TREE_TYPE (arg
) = error_mark_node
;
4785 TREE_VALUE (argtype
) = error_mark_node
;
4788 arg
= DECL_CHAIN (arg
);
4789 argtype
= TREE_CHAIN (argtype
);
4792 /* Check for bare parameter packs in the return type and the
4793 exception specifiers. */
4794 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4795 /* Errors were already issued, set return type to int
4796 as the frontend doesn't expect error_mark_node as
4798 TREE_TYPE (type
) = integer_type_node
;
4799 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4800 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4802 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4803 && TYPE_DECL_ALIAS_P (decl
))
4804 ? DECL_ORIGINAL_TYPE (decl
)
4805 : TREE_TYPE (decl
)))
4807 TREE_TYPE (decl
) = error_mark_node
;
4808 return error_mark_node
;
4812 return process_partial_specialization (decl
);
4814 args
= current_template_args ();
4817 || TREE_CODE (ctx
) == FUNCTION_DECL
4818 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4819 || (TREE_CODE (decl
) == TYPE_DECL
4820 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4821 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4823 if (DECL_LANG_SPECIFIC (decl
)
4824 && DECL_TEMPLATE_INFO (decl
)
4825 && DECL_TI_TEMPLATE (decl
))
4826 tmpl
= DECL_TI_TEMPLATE (decl
);
4827 /* If DECL is a TYPE_DECL for a class-template, then there won't
4828 be DECL_LANG_SPECIFIC. The information equivalent to
4829 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4830 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4831 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4832 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4834 /* Since a template declaration already existed for this
4835 class-type, we must be redeclaring it here. Make sure
4836 that the redeclaration is valid. */
4837 redeclare_class_template (TREE_TYPE (decl
),
4838 current_template_parms
);
4839 /* We don't need to create a new TEMPLATE_DECL; just use the
4840 one we already had. */
4841 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4845 tmpl
= build_template_decl (decl
, current_template_parms
,
4849 if (DECL_LANG_SPECIFIC (decl
)
4850 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4852 /* A specialization of a member template of a template
4854 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4855 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4856 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4862 tree a
, t
, current
, parms
;
4864 tree tinfo
= get_template_info (decl
);
4868 error ("template definition of non-template %q#D", decl
);
4869 return error_mark_node
;
4872 tmpl
= TI_TEMPLATE (tinfo
);
4874 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4875 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4876 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4877 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4881 /* The declaration is a specialization of a member
4882 template, declared outside the class. Therefore, the
4883 innermost template arguments will be NULL, so we
4884 replace them with the arguments determined by the
4885 earlier call to check_explicit_specialization. */
4886 args
= DECL_TI_ARGS (decl
);
4889 = build_template_decl (decl
, current_template_parms
,
4891 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4892 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4893 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4894 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4895 DECL_TEMPLATE_INFO (new_tmpl
)
4896 = build_template_info (tmpl
, args
);
4898 register_specialization (new_tmpl
,
4899 most_general_template (tmpl
),
4905 /* Make sure the template headers we got make sense. */
4907 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4908 i
= TMPL_PARMS_DEPTH (parms
);
4909 if (TMPL_ARGS_DEPTH (args
) != i
)
4911 error ("expected %d levels of template parms for %q#D, got %d",
4912 i
, decl
, TMPL_ARGS_DEPTH (args
));
4913 DECL_INTERFACE_KNOWN (decl
) = 1;
4914 return error_mark_node
;
4917 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4919 a
= TMPL_ARGS_LEVEL (args
, i
);
4920 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4922 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4924 if (current
== decl
)
4925 error ("got %d template parameters for %q#D",
4926 TREE_VEC_LENGTH (a
), decl
);
4928 error ("got %d template parameters for %q#T",
4929 TREE_VEC_LENGTH (a
), current
);
4930 error (" but %d required", TREE_VEC_LENGTH (t
));
4931 /* Avoid crash in import_export_decl. */
4932 DECL_INTERFACE_KNOWN (decl
) = 1;
4933 return error_mark_node
;
4936 if (current
== decl
)
4938 else if (current
== NULL_TREE
)
4939 /* Can happen in erroneous input. */
4942 current
= get_containing_scope (current
);
4945 /* Check that the parms are used in the appropriate qualifying scopes
4946 in the declarator. */
4947 if (!comp_template_args
4949 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4952 template arguments to %qD do not match original template %qD",
4953 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4954 if (!uses_template_parms (TI_ARGS (tinfo
)))
4955 inform (input_location
, "use template<> for an explicit specialization");
4956 /* Avoid crash in import_export_decl. */
4957 DECL_INTERFACE_KNOWN (decl
) = 1;
4958 return error_mark_node
;
4962 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4963 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4965 /* Push template declarations for global functions and types. Note
4966 that we do not try to push a global template friend declared in a
4967 template class; such a thing may well depend on the template
4968 parameters of the class. */
4969 if (new_template_p
&& !ctx
4970 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4972 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4973 if (tmpl
== error_mark_node
)
4974 return error_mark_node
;
4976 /* Hide template friend classes that haven't been declared yet. */
4977 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4979 DECL_ANTICIPATED (tmpl
) = 1;
4980 DECL_FRIEND_P (tmpl
) = 1;
4986 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4989 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4990 if (DECL_CONV_FN_P (tmpl
))
4992 int depth
= TMPL_PARMS_DEPTH (parms
);
4994 /* It is a conversion operator. See if the type converted to
4995 depends on innermost template operands. */
4997 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4999 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5002 /* Give template template parms a DECL_CONTEXT of the template
5003 for which they are a parameter. */
5004 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5005 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5007 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5008 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5009 DECL_CONTEXT (parm
) = tmpl
;
5013 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5014 back to its most general template. If TMPL is a specialization,
5015 ARGS may only have the innermost set of arguments. Add the missing
5016 argument levels if necessary. */
5017 if (DECL_TEMPLATE_INFO (tmpl
))
5018 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5020 info
= build_template_info (tmpl
, args
);
5022 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5023 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5026 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5027 retrofit_lang_decl (decl
);
5028 if (DECL_LANG_SPECIFIC (decl
))
5029 DECL_TEMPLATE_INFO (decl
) = info
;
5032 return DECL_TEMPLATE_RESULT (tmpl
);
5036 push_template_decl (tree decl
)
5038 return push_template_decl_real (decl
, false);
5041 /* FN is an inheriting constructor that inherits from the constructor
5042 template INHERITED; turn FN into a constructor template with a matching
5046 add_inherited_template_parms (tree fn
, tree inherited
)
5049 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5050 inner_parms
= copy_node (inner_parms
);
5052 = tree_cons (size_int (processing_template_decl
+ 1),
5053 inner_parms
, current_template_parms
);
5054 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5055 tree args
= template_parms_to_args (parms
);
5056 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5057 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5058 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5059 DECL_ARTIFICIAL (tmpl
) = true;
5060 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5064 /* Called when a class template TYPE is redeclared with the indicated
5065 template PARMS, e.g.:
5067 template <class T> struct S;
5068 template <class T> struct S {}; */
5071 redeclare_class_template (tree type
, tree parms
)
5077 if (!TYPE_TEMPLATE_INFO (type
))
5079 error ("%qT is not a template type", type
);
5083 tmpl
= TYPE_TI_TEMPLATE (type
);
5084 if (!PRIMARY_TEMPLATE_P (tmpl
))
5085 /* The type is nested in some template class. Nothing to worry
5086 about here; there are no new template parameters for the nested
5092 error ("template specifiers not specified in declaration of %qD",
5097 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5098 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5100 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5102 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5103 "redeclared with %d template parameter",
5104 "redeclared with %d template parameters",
5105 TREE_VEC_LENGTH (parms
));
5106 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5107 "previous declaration %q+D used %d template parameter",
5108 "previous declaration %q+D used %d template parameters",
5109 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5113 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5120 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5121 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5124 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5125 if (tmpl_parm
== error_mark_node
)
5128 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5129 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5130 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5132 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5134 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5135 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5136 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5137 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5138 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5139 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5140 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5141 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5142 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5144 error ("template parameter %q+#D", tmpl_parm
);
5145 error ("redeclared here as %q#D", parm
);
5149 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5151 /* We have in [temp.param]:
5153 A template-parameter may not be given default arguments
5154 by two different declarations in the same scope. */
5155 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5156 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5157 "original definition appeared here");
5161 if (parm_default
!= NULL_TREE
)
5162 /* Update the previous template parameters (which are the ones
5163 that will really count) with the new default value. */
5164 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5165 else if (tmpl_default
!= NULL_TREE
)
5166 /* Update the new parameters, too; they'll be used as the
5167 parameters for any members. */
5168 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5174 /* Simplify EXPR if it is a non-dependent expression. Returns the
5175 (possibly simplified) expression. */
5178 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5180 if (expr
== NULL_TREE
)
5183 /* If we're in a template, but EXPR isn't value dependent, simplify
5184 it. We're supposed to treat:
5186 template <typename T> void f(T[1 + 1]);
5187 template <typename T> void f(T[2]);
5189 as two declarations of the same function, for example. */
5190 if (processing_template_decl
5191 && !instantiation_dependent_expression_p (expr
)
5192 && potential_constant_expression (expr
))
5194 HOST_WIDE_INT saved_processing_template_decl
;
5196 saved_processing_template_decl
= processing_template_decl
;
5197 processing_template_decl
= 0;
5198 expr
= tsubst_copy_and_build (expr
,
5201 /*in_decl=*/NULL_TREE
,
5202 /*function_p=*/false,
5203 /*integral_constant_expression_p=*/true);
5204 processing_template_decl
= saved_processing_template_decl
;
5210 fold_non_dependent_expr (tree expr
)
5212 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5215 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5216 template declaration, or a TYPE_DECL for an alias declaration. */
5219 alias_type_or_template_p (tree t
)
5223 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5226 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5227 || DECL_ALIAS_TEMPLATE_P (t
));
5230 /* Return TRUE iff is a specialization of an alias template. */
5233 alias_template_specialization_p (const_tree t
)
5239 && TYPE_TEMPLATE_INFO (t
)
5240 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5241 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5244 /* Return the number of innermost template parameters in TMPL. */
5247 num_innermost_template_parms (tree tmpl
)
5249 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5250 return TREE_VEC_LENGTH (parms
);
5253 /* Return either TMPL or another template that it is equivalent to under DR
5254 1286: An alias that just changes the name of a template is equivalent to
5255 the other template. */
5258 get_underlying_template (tree tmpl
)
5260 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5261 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5263 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5264 if (TYPE_TEMPLATE_INFO (result
))
5266 tree sub
= TYPE_TI_TEMPLATE (result
);
5267 if (PRIMARY_TEMPLATE_P (sub
)
5268 && (num_innermost_template_parms (tmpl
)
5269 == num_innermost_template_parms (sub
)))
5271 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5272 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5273 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5275 /* The alias type is equivalent to the pattern of the
5276 underlying template, so strip the alias. */
5286 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5287 must be a function or a pointer-to-function type, as specified
5288 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5289 and check that the resulting function has external linkage. */
5292 convert_nontype_argument_function (tree type
, tree expr
)
5296 linkage_kind linkage
;
5298 fn
= instantiate_type (type
, fns
, tf_none
);
5299 if (fn
== error_mark_node
)
5300 return error_mark_node
;
5303 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5304 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5305 if (BASELINK_P (fn_no_ptr
))
5306 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5308 /* [temp.arg.nontype]/1
5310 A template-argument for a non-type, non-template template-parameter
5313 -- the address of an object or function with external [C++11: or
5314 internal] linkage. */
5316 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5318 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5319 if (TYPE_PTR_P (type
))
5320 error ("it must be the address of a function with external linkage");
5322 error ("it must be the name of a function with external linkage");
5326 linkage
= decl_linkage (fn_no_ptr
);
5327 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5329 if (cxx_dialect
>= cxx11
)
5330 error ("%qE is not a valid template argument for type %qT "
5331 "because %qD has no linkage",
5332 expr
, type
, fn_no_ptr
);
5334 error ("%qE is not a valid template argument for type %qT "
5335 "because %qD does not have external linkage",
5336 expr
, type
, fn_no_ptr
);
5343 /* Subroutine of convert_nontype_argument.
5344 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5345 Emit an error otherwise. */
5348 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5349 tsubst_flags_t complain
)
5352 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5354 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5356 if (complain
& tf_error
)
5358 error ("%qE is not a valid template argument for type %qT",
5360 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5365 /* Returns TRUE iff the address of OP is value-dependent.
5367 14.6.2.4 [temp.dep.temp]:
5368 A non-integral non-type template-argument is dependent if its type is
5369 dependent or it has either of the following forms
5372 and contains a nested-name-specifier which specifies a class-name that
5373 names a dependent type.
5375 We generalize this to just say that the address of a member of a
5376 dependent class is value-dependent; the above doesn't cover the
5377 address of a static data member named with an unqualified-id. */
5380 has_value_dependent_address (tree op
)
5382 /* We could use get_inner_reference here, but there's no need;
5383 this is only relevant for template non-type arguments, which
5384 can only be expressed as &id-expression. */
5387 tree ctx
= CP_DECL_CONTEXT (op
);
5388 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5395 /* The next set of functions are used for providing helpful explanatory
5396 diagnostics for failed overload resolution. Their messages should be
5397 indented by two spaces for consistency with the messages in
5401 unify_success (bool /*explain_p*/)
5407 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5410 inform (input_location
,
5411 " couldn't deduce template parameter %qD", parm
);
5416 unify_invalid (bool /*explain_p*/)
5422 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5425 inform (input_location
,
5426 " types %qT and %qT have incompatible cv-qualifiers",
5432 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5435 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5440 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5443 inform (input_location
,
5444 " template parameter %qD is not a parameter pack, but "
5451 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5454 inform (input_location
,
5455 " template argument %qE does not match "
5456 "pointer-to-member constant %qE",
5462 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5465 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5470 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5473 inform (input_location
,
5474 " inconsistent parameter pack deduction with %qT and %qT",
5480 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5485 inform (input_location
,
5486 " deduced conflicting types for parameter %qT (%qT and %qT)",
5487 parm
, first
, second
);
5489 inform (input_location
,
5490 " deduced conflicting values for non-type parameter "
5491 "%qE (%qE and %qE)", parm
, first
, second
);
5497 unify_vla_arg (bool explain_p
, tree arg
)
5500 inform (input_location
,
5501 " variable-sized array type %qT is not "
5502 "a valid template argument",
5508 unify_method_type_error (bool explain_p
, tree arg
)
5511 inform (input_location
,
5512 " member function type %qT is not a valid template argument",
5518 unify_arity (bool explain_p
, int have
, int wanted
)
5521 inform_n (input_location
, wanted
,
5522 " candidate expects %d argument, %d provided",
5523 " candidate expects %d arguments, %d provided",
5529 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5531 return unify_arity (explain_p
, have
, wanted
);
5535 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5537 return unify_arity (explain_p
, have
, wanted
);
5541 unify_arg_conversion (bool explain_p
, tree to_type
,
5542 tree from_type
, tree arg
)
5545 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5546 " cannot convert %qE (type %qT) to type %qT",
5547 arg
, from_type
, to_type
);
5552 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5553 tree parm
, tree arg
)
5558 case tbr_ambiguous_baseclass
:
5559 inform (input_location
, " %qT is an ambiguous base class of %qT",
5563 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5570 unify_inconsistent_template_template_parameters (bool explain_p
)
5573 inform (input_location
,
5574 " template parameters of a template template argument are "
5575 "inconsistent with other deduced template arguments");
5580 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5583 inform (input_location
,
5584 " can't deduce a template for %qT from non-template type %qT",
5590 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5593 inform (input_location
,
5594 " template argument %qE does not match %qD", arg
, parm
);
5599 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5602 inform (input_location
,
5603 " could not resolve address from overloaded function %qE",
5608 /* Attempt to convert the non-type template parameter EXPR to the
5609 indicated TYPE. If the conversion is successful, return the
5610 converted value. If the conversion is unsuccessful, return
5611 NULL_TREE if we issued an error message, or error_mark_node if we
5612 did not. We issue error messages for out-and-out bad template
5613 parameters, but not simply because the conversion failed, since we
5614 might be just trying to do argument deduction. Both TYPE and EXPR
5615 must be non-dependent.
5617 The conversion follows the special rules described in
5618 [temp.arg.nontype], and it is much more strict than an implicit
5621 This function is called twice for each template argument (see
5622 lookup_template_class for a more accurate description of this
5623 problem). This means that we need to handle expressions which
5624 are not valid in a C++ source, but can be created from the
5625 first call (for instance, casts to perform conversions). These
5626 hacks can go away after we fix the double coercion problem. */
5629 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5633 /* Detect immediately string literals as invalid non-type argument.
5634 This special-case is not needed for correctness (we would easily
5635 catch this later), but only to provide better diagnostic for this
5636 common user mistake. As suggested by DR 100, we do not mention
5637 linkage issues in the diagnostic as this is not the point. */
5638 /* FIXME we're making this OK. */
5639 if (TREE_CODE (expr
) == STRING_CST
)
5641 if (complain
& tf_error
)
5642 error ("%qE is not a valid template argument for type %qT "
5643 "because string literals can never be used in this context",
5648 /* Add the ADDR_EXPR now for the benefit of
5649 value_dependent_expression_p. */
5650 if (TYPE_PTROBV_P (type
)
5651 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5653 expr
= decay_conversion (expr
, complain
);
5654 if (expr
== error_mark_node
)
5655 return error_mark_node
;
5658 /* If we are in a template, EXPR may be non-dependent, but still
5659 have a syntactic, rather than semantic, form. For example, EXPR
5660 might be a SCOPE_REF, rather than the VAR_DECL to which the
5661 SCOPE_REF refers. Preserving the qualifying scope is necessary
5662 so that access checking can be performed when the template is
5663 instantiated -- but here we need the resolved form so that we can
5664 convert the argument. */
5665 if (TYPE_REF_OBJ_P (type
)
5666 && has_value_dependent_address (expr
))
5667 /* If we want the address and it's value-dependent, don't fold. */;
5668 else if (!type_unknown_p (expr
))
5669 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5670 if (error_operand_p (expr
))
5671 return error_mark_node
;
5672 expr_type
= TREE_TYPE (expr
);
5673 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5674 expr
= mark_lvalue_use (expr
);
5676 expr
= mark_rvalue_use (expr
);
5678 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5679 to a non-type argument of "nullptr". */
5680 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5681 expr
= convert (type
, expr
);
5683 /* In C++11, integral or enumeration non-type template arguments can be
5684 arbitrary constant expressions. Pointer and pointer to
5685 member arguments can be general constant expressions that evaluate
5686 to a null value, but otherwise still need to be of a specific form. */
5687 if (cxx_dialect
>= cxx11
)
5689 if (TREE_CODE (expr
) == PTRMEM_CST
)
5690 /* A PTRMEM_CST is already constant, and a valid template
5691 argument for a parameter of pointer to member type, we just want
5692 to leave it in that form rather than lower it to a
5694 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5695 expr
= maybe_constant_value (expr
);
5696 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5698 tree folded
= maybe_constant_value (expr
);
5699 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5700 : null_member_pointer_value_p (folded
))
5705 /* HACK: Due to double coercion, we can get a
5706 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5707 which is the tree that we built on the first call (see
5708 below when coercing to reference to object or to reference to
5709 function). We just strip everything and get to the arg.
5710 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5712 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5714 tree probe_type
, probe
= expr
;
5715 if (REFERENCE_REF_P (probe
))
5716 probe
= TREE_OPERAND (probe
, 0);
5717 probe_type
= TREE_TYPE (probe
);
5718 if (TREE_CODE (probe
) == NOP_EXPR
)
5720 /* ??? Maybe we could use convert_from_reference here, but we
5721 would need to relax its constraints because the NOP_EXPR
5722 could actually change the type to something more cv-qualified,
5723 and this is not folded by convert_from_reference. */
5724 tree addr
= TREE_OPERAND (probe
, 0);
5725 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5726 && TREE_CODE (addr
) == ADDR_EXPR
5727 && TYPE_PTR_P (TREE_TYPE (addr
))
5728 && (same_type_ignoring_top_level_qualifiers_p
5729 (TREE_TYPE (probe_type
),
5730 TREE_TYPE (TREE_TYPE (addr
)))))
5732 expr
= TREE_OPERAND (addr
, 0);
5733 expr_type
= TREE_TYPE (probe_type
);
5738 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5739 parameter is a pointer to object, through decay and
5740 qualification conversion. Let's strip everything. */
5741 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5745 if (TREE_CODE (probe
) == ADDR_EXPR
5746 && TYPE_PTR_P (TREE_TYPE (probe
)))
5748 /* Skip the ADDR_EXPR only if it is part of the decay for
5749 an array. Otherwise, it is part of the original argument
5750 in the source code. */
5751 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5752 probe
= TREE_OPERAND (probe
, 0);
5754 expr_type
= TREE_TYPE (expr
);
5758 /* [temp.arg.nontype]/5, bullet 1
5760 For a non-type template-parameter of integral or enumeration type,
5761 integral promotions (_conv.prom_) and integral conversions
5762 (_conv.integral_) are applied. */
5763 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5765 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5766 t
= maybe_constant_value (t
);
5767 if (t
!= error_mark_node
)
5770 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5771 return error_mark_node
;
5773 /* Notice that there are constant expressions like '4 % 0' which
5774 do not fold into integer constants. */
5775 if (TREE_CODE (expr
) != INTEGER_CST
)
5777 if (complain
& tf_error
)
5779 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5780 if (processing_template_decl
5781 && !require_potential_constant_expression (expr
))
5783 expr
= cxx_constant_value (expr
);
5784 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5785 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5786 "in template argument for type %qT ", type
);
5787 if (expr
== error_mark_node
)
5789 /* else cxx_constant_value complained but gave us
5790 a real constant, so go ahead. */
5791 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5797 /* Avoid typedef problems. */
5798 if (TREE_TYPE (expr
) != type
)
5799 expr
= fold_convert (type
, expr
);
5801 /* [temp.arg.nontype]/5, bullet 2
5803 For a non-type template-parameter of type pointer to object,
5804 qualification conversions (_conv.qual_) and the array-to-pointer
5805 conversion (_conv.array_) are applied. */
5806 else if (TYPE_PTROBV_P (type
))
5808 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5810 A template-argument for a non-type, non-template template-parameter
5811 shall be one of: [...]
5813 -- the name of a non-type template-parameter;
5814 -- the address of an object or function with external linkage, [...]
5815 expressed as "& id-expression" where the & is optional if the name
5816 refers to a function or array, or if the corresponding
5817 template-parameter is a reference.
5819 Here, we do not care about functions, as they are invalid anyway
5820 for a parameter of type pointer-to-object. */
5822 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5823 /* Non-type template parameters are OK. */
5825 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5826 /* Null pointer values are OK in C++11. */;
5827 else if (TREE_CODE (expr
) != ADDR_EXPR
5828 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5832 error ("%qD is not a valid template argument "
5833 "because %qD is a variable, not the address of "
5838 if (POINTER_TYPE_P (expr_type
))
5840 error ("%qE is not a valid template argument for %qT "
5841 "because it is not the address of a variable",
5845 /* Other values, like integer constants, might be valid
5846 non-type arguments of some other type. */
5847 return error_mark_node
;
5853 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5854 ? TREE_OPERAND (expr
, 0) : expr
);
5857 error ("%qE is not a valid template argument of type %qT "
5858 "because %qE is not a variable",
5862 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5864 error ("%qE is not a valid template argument of type %qT "
5865 "because %qD does not have external linkage",
5869 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5871 error ("%qE is not a valid template argument of type %qT "
5872 "because %qD has no linkage",
5878 expr
= decay_conversion (expr
, complain
);
5879 if (expr
== error_mark_node
)
5880 return error_mark_node
;
5882 expr
= perform_qualification_conversions (type
, expr
);
5883 if (expr
== error_mark_node
)
5884 return error_mark_node
;
5886 /* [temp.arg.nontype]/5, bullet 3
5888 For a non-type template-parameter of type reference to object, no
5889 conversions apply. The type referred to by the reference may be more
5890 cv-qualified than the (otherwise identical) type of the
5891 template-argument. The template-parameter is bound directly to the
5892 template-argument, which must be an lvalue. */
5893 else if (TYPE_REF_OBJ_P (type
))
5895 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5897 return error_mark_node
;
5899 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5901 error ("%qE is not a valid template argument for type %qT "
5902 "because of conflicts in cv-qualification", expr
, type
);
5906 if (!real_lvalue_p (expr
))
5908 error ("%qE is not a valid template argument for type %qT "
5909 "because it is not an lvalue", expr
, type
);
5913 /* [temp.arg.nontype]/1
5915 A template-argument for a non-type, non-template template-parameter
5916 shall be one of: [...]
5918 -- the address of an object or function with external linkage. */
5919 if (INDIRECT_REF_P (expr
)
5920 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5922 expr
= TREE_OPERAND (expr
, 0);
5925 error ("%q#D is not a valid template argument for type %qT "
5926 "because a reference variable does not have a constant "
5927 "address", expr
, type
);
5934 error ("%qE is not a valid template argument for type %qT "
5935 "because it is not an object with external linkage",
5940 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5942 error ("%qE is not a valid template argument for type %qT "
5943 "because object %qD has not external linkage",
5948 expr
= build_nop (type
, build_address (expr
));
5950 /* [temp.arg.nontype]/5, bullet 4
5952 For a non-type template-parameter of type pointer to function, only
5953 the function-to-pointer conversion (_conv.func_) is applied. If the
5954 template-argument represents a set of overloaded functions (or a
5955 pointer to such), the matching function is selected from the set
5957 else if (TYPE_PTRFN_P (type
))
5959 /* If the argument is a template-id, we might not have enough
5960 context information to decay the pointer. */
5961 if (!type_unknown_p (expr_type
))
5963 expr
= decay_conversion (expr
, complain
);
5964 if (expr
== error_mark_node
)
5965 return error_mark_node
;
5968 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5969 /* Null pointer values are OK in C++11. */
5970 return perform_qualification_conversions (type
, expr
);
5972 expr
= convert_nontype_argument_function (type
, expr
);
5973 if (!expr
|| expr
== error_mark_node
)
5976 /* [temp.arg.nontype]/5, bullet 5
5978 For a non-type template-parameter of type reference to function, no
5979 conversions apply. If the template-argument represents a set of
5980 overloaded functions, the matching function is selected from the set
5982 else if (TYPE_REFFN_P (type
))
5984 if (TREE_CODE (expr
) == ADDR_EXPR
)
5986 error ("%qE is not a valid template argument for type %qT "
5987 "because it is a pointer", expr
, type
);
5988 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5992 expr
= convert_nontype_argument_function (type
, expr
);
5993 if (!expr
|| expr
== error_mark_node
)
5996 expr
= build_nop (type
, build_address (expr
));
5998 /* [temp.arg.nontype]/5, bullet 6
6000 For a non-type template-parameter of type pointer to member function,
6001 no conversions apply. If the template-argument represents a set of
6002 overloaded member functions, the matching member function is selected
6003 from the set (_over.over_). */
6004 else if (TYPE_PTRMEMFUNC_P (type
))
6006 expr
= instantiate_type (type
, expr
, tf_none
);
6007 if (expr
== error_mark_node
)
6008 return error_mark_node
;
6010 /* [temp.arg.nontype] bullet 1 says the pointer to member
6011 expression must be a pointer-to-member constant. */
6012 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6013 return error_mark_node
;
6015 /* There is no way to disable standard conversions in
6016 resolve_address_of_overloaded_function (called by
6017 instantiate_type). It is possible that the call succeeded by
6018 converting &B::I to &D::I (where B is a base of D), so we need
6019 to reject this conversion here.
6021 Actually, even if there was a way to disable standard conversions,
6022 it would still be better to reject them here so that we can
6023 provide a superior diagnostic. */
6024 if (!same_type_p (TREE_TYPE (expr
), type
))
6026 error ("%qE is not a valid template argument for type %qT "
6027 "because it is of type %qT", expr
, type
,
6029 /* If we are just one standard conversion off, explain. */
6030 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6031 inform (input_location
,
6032 "standard conversions are not allowed in this context");
6036 /* [temp.arg.nontype]/5, bullet 7
6038 For a non-type template-parameter of type pointer to data member,
6039 qualification conversions (_conv.qual_) are applied. */
6040 else if (TYPE_PTRDATAMEM_P (type
))
6042 /* [temp.arg.nontype] bullet 1 says the pointer to member
6043 expression must be a pointer-to-member constant. */
6044 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6045 return error_mark_node
;
6047 expr
= perform_qualification_conversions (type
, expr
);
6048 if (expr
== error_mark_node
)
6051 else if (NULLPTR_TYPE_P (type
))
6053 if (expr
!= nullptr_node
)
6055 error ("%qE is not a valid template argument for type %qT "
6056 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6061 /* A template non-type parameter must be one of the above. */
6065 /* Sanity check: did we actually convert the argument to the
6067 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6068 (type
, TREE_TYPE (expr
)));
6072 /* Subroutine of coerce_template_template_parms, which returns 1 if
6073 PARM_PARM and ARG_PARM match using the rule for the template
6074 parameters of template template parameters. Both PARM and ARG are
6075 template parameters; the rest of the arguments are the same as for
6076 coerce_template_template_parms.
6079 coerce_template_template_parm (tree parm
,
6081 tsubst_flags_t complain
,
6085 if (arg
== NULL_TREE
|| arg
== error_mark_node
6086 || parm
== NULL_TREE
|| parm
== error_mark_node
)
6089 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6092 switch (TREE_CODE (parm
))
6095 /* We encounter instantiations of templates like
6096 template <template <template <class> class> class TT>
6099 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6100 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6102 if (!coerce_template_template_parms
6103 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6109 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6110 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6111 /* Argument is a parameter pack but parameter is not. */
6116 /* The tsubst call is used to handle cases such as
6118 template <int> class C {};
6119 template <class T, template <T> class TT> class D {};
6122 i.e. the parameter list of TT depends on earlier parameters. */
6123 if (!uses_template_parms (TREE_TYPE (arg
))
6125 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6129 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6130 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6131 /* Argument is a parameter pack but parameter is not. */
6144 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6145 template template parameters. Both PARM_PARMS and ARG_PARMS are
6146 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6149 Consider the example:
6150 template <class T> class A;
6151 template<template <class U> class TT> class B;
6153 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6154 the parameters to A, and OUTER_ARGS contains A. */
6157 coerce_template_template_parms (tree parm_parms
,
6159 tsubst_flags_t complain
,
6163 int nparms
, nargs
, i
;
6167 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6168 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6170 nparms
= TREE_VEC_LENGTH (parm_parms
);
6171 nargs
= TREE_VEC_LENGTH (arg_parms
);
6173 /* Determine whether we have a parameter pack at the end of the
6174 template template parameter's template parameter list. */
6175 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6177 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6179 if (parm
== error_mark_node
)
6182 switch (TREE_CODE (parm
))
6186 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6191 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6201 && !(variadic_p
&& nargs
>= nparms
- 1))
6204 /* Check all of the template parameters except the parameter pack at
6205 the end (if any). */
6206 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6208 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6209 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6212 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6213 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6215 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6223 /* Check each of the template parameters in the template
6224 argument against the template parameter pack at the end of
6225 the template template parameter. */
6226 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6229 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6231 for (; i
< nargs
; ++i
)
6233 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6236 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6238 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6247 /* Verifies that the deduced template arguments (in TARGS) for the
6248 template template parameters (in TPARMS) represent valid bindings,
6249 by comparing the template parameter list of each template argument
6250 to the template parameter list of its corresponding template
6251 template parameter, in accordance with DR150. This
6252 routine can only be called after all template arguments have been
6253 deduced. It will return TRUE if all of the template template
6254 parameter bindings are okay, FALSE otherwise. */
6256 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6258 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6261 /* We're dealing with template parms in this process. */
6262 ++processing_template_decl
;
6264 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6266 for (i
= 0; i
< ntparms
; ++i
)
6268 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6269 tree targ
= TREE_VEC_ELT (targs
, i
);
6271 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6273 tree packed_args
= NULL_TREE
;
6276 if (ARGUMENT_PACK_P (targ
))
6278 /* Look inside the argument pack. */
6279 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6280 len
= TREE_VEC_LENGTH (packed_args
);
6283 for (idx
= 0; idx
< len
; ++idx
)
6285 tree targ_parms
= NULL_TREE
;
6288 /* Extract the next argument from the argument
6290 targ
= TREE_VEC_ELT (packed_args
, idx
);
6292 if (PACK_EXPANSION_P (targ
))
6293 /* Look at the pattern of the pack expansion. */
6294 targ
= PACK_EXPANSION_PATTERN (targ
);
6296 /* Extract the template parameters from the template
6298 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6299 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6300 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6301 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6303 /* Verify that we can coerce the template template
6304 parameters from the template argument to the template
6305 parameter. This requires an exact match. */
6307 && !coerce_template_template_parms
6308 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6323 --processing_template_decl
;
6327 /* Since type attributes aren't mangled, we need to strip them from
6328 template type arguments. */
6331 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6334 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6336 mv
= TYPE_MAIN_VARIANT (arg
);
6337 arg
= strip_typedefs (arg
);
6338 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6339 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6341 if (complain
& tf_warning
)
6342 warning (0, "ignoring attributes on template argument %qT", arg
);
6343 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6344 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6349 /* Convert the indicated template ARG as necessary to match the
6350 indicated template PARM. Returns the converted ARG, or
6351 error_mark_node if the conversion was unsuccessful. Error and
6352 warning messages are issued under control of COMPLAIN. This
6353 conversion is for the Ith parameter in the parameter list. ARGS is
6354 the full set of template arguments deduced so far. */
6357 convert_template_argument (tree parm
,
6360 tsubst_flags_t complain
,
6366 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6368 if (TREE_CODE (arg
) == TREE_LIST
6369 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6371 /* The template argument was the name of some
6372 member function. That's usually
6373 invalid, but static members are OK. In any
6374 case, grab the underlying fields/functions
6375 and issue an error later if required. */
6376 orig_arg
= TREE_VALUE (arg
);
6377 TREE_TYPE (arg
) = unknown_type_node
;
6382 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6383 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6384 || requires_tmpl_type
);
6386 /* When determining whether an argument pack expansion is a template,
6387 look at the pattern. */
6388 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6389 arg
= PACK_EXPANSION_PATTERN (arg
);
6391 /* Deal with an injected-class-name used as a template template arg. */
6392 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6394 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6395 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6397 if (cxx_dialect
>= cxx11
)
6398 /* OK under DR 1004. */;
6399 else if (complain
& tf_warning_or_error
)
6400 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6401 " used as template template argument", TYPE_NAME (arg
));
6402 else if (flag_pedantic_errors
)
6410 ((TREE_CODE (arg
) == TEMPLATE_DECL
6411 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6412 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6413 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6414 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6417 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6418 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6419 arg
= TYPE_STUB_DECL (arg
);
6421 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6423 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6424 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6426 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6428 if (complain
& tf_error
)
6429 error ("invalid use of destructor %qE as a type", orig_arg
);
6430 return error_mark_node
;
6433 permerror (input_location
,
6434 "to refer to a type member of a template parameter, "
6435 "use %<typename %E%>", orig_arg
);
6437 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6438 TREE_OPERAND (arg
, 1),
6444 if (is_type
!= requires_type
)
6448 if (complain
& tf_error
)
6450 error ("type/value mismatch at argument %d in template "
6451 "parameter list for %qD",
6454 error (" expected a constant of type %qT, got %qT",
6456 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6457 else if (requires_tmpl_type
)
6458 error (" expected a class template, got %qE", orig_arg
);
6460 error (" expected a type, got %qE", orig_arg
);
6463 return error_mark_node
;
6465 if (is_tmpl_type
^ requires_tmpl_type
)
6467 if (in_decl
&& (complain
& tf_error
))
6469 error ("type/value mismatch at argument %d in template "
6470 "parameter list for %qD",
6473 error (" expected a type, got %qT", DECL_NAME (arg
));
6475 error (" expected a class template, got %qT", orig_arg
);
6477 return error_mark_node
;
6482 if (requires_tmpl_type
)
6484 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6486 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6487 /* The number of argument required is not known yet.
6488 Just accept it for now. */
6489 val
= TREE_TYPE (arg
);
6492 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6495 /* Strip alias templates that are equivalent to another
6497 arg
= get_underlying_template (arg
);
6498 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6500 if (coerce_template_template_parms (parmparm
, argparm
,
6506 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6508 if (val
!= error_mark_node
)
6510 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6511 val
= TREE_TYPE (val
);
6512 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6513 val
= make_pack_expansion (val
);
6518 if (in_decl
&& (complain
& tf_error
))
6520 error ("type/value mismatch at argument %d in "
6521 "template parameter list for %qD",
6523 error (" expected a template of type %qD, got %qT",
6527 val
= error_mark_node
;
6533 /* We only form one instance of each template specialization.
6534 Therefore, if we use a non-canonical variant (i.e., a
6535 typedef), any future messages referring to the type will use
6536 the typedef, which is confusing if those future uses do not
6537 themselves also use the typedef. */
6539 val
= canonicalize_type_argument (val
, complain
);
6543 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6545 if (invalid_nontype_parm_type_p (t
, complain
))
6546 return error_mark_node
;
6548 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6550 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6554 /* Not sure if this is reachable, but it doesn't hurt
6556 error ("type mismatch in nontype parameter pack");
6557 val
= error_mark_node
;
6560 else if (!dependent_template_arg_p (orig_arg
)
6561 && !uses_template_parms (t
))
6562 /* We used to call digest_init here. However, digest_init
6563 will report errors, which we don't want when complain
6564 is zero. More importantly, digest_init will try too
6565 hard to convert things: for example, `0' should not be
6566 converted to pointer type at this point according to
6567 the standard. Accepting this is not merely an
6568 extension, since deciding whether or not these
6569 conversions can occur is part of determining which
6570 function template to call, or whether a given explicit
6571 argument specification is valid. */
6572 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6574 val
= strip_typedefs_expr (orig_arg
);
6576 if (val
== NULL_TREE
)
6577 val
= error_mark_node
;
6578 else if (val
== error_mark_node
&& (complain
& tf_error
))
6579 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6581 if (TREE_CODE (val
) == SCOPE_REF
)
6583 /* Strip typedefs from the SCOPE_REF. */
6584 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6585 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6587 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6588 QUALIFIED_NAME_IS_TEMPLATE (val
));
6595 /* Coerces the remaining template arguments in INNER_ARGS (from
6596 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6597 Returns the coerced argument pack. PARM_IDX is the position of this
6598 parameter in the template parameter list. ARGS is the original
6599 template argument list. */
6601 coerce_template_parameter_pack (tree parms
,
6609 tsubst_flags_t complain
)
6611 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6612 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6615 tree packed_parms
= NULL_TREE
;
6617 if (arg_idx
> nargs
)
6620 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
6622 /* When the template parameter is a non-type template parameter pack
6623 or template template parameter pack whose type or template
6624 parameters use parameter packs, we know exactly how many arguments
6625 we are looking for. Build a vector of the instantiated decls for
6626 these template parameters in PACKED_PARMS. */
6627 /* We can't use make_pack_expansion here because it would interpret a
6628 _DECL as a use rather than a declaration. */
6629 tree decl
= TREE_VALUE (parm
);
6630 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
6631 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
6632 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
6633 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
6635 TREE_VEC_LENGTH (args
)--;
6636 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
6637 TREE_VEC_LENGTH (args
)++;
6639 if (packed_parms
== error_mark_node
)
6640 return error_mark_node
;
6642 /* If we're doing a partial instantiation of a member template,
6643 verify that all of the types used for the non-type
6644 template parameter pack are, in fact, valid for non-type
6645 template parameters. */
6647 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6649 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
6650 for (j
= 0; j
< len
; ++j
)
6652 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
6653 if (invalid_nontype_parm_type_p (t
, complain
))
6654 return error_mark_node
;
6658 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
6661 packed_args
= make_tree_vec (nargs
- arg_idx
);
6663 /* Convert the remaining arguments, which will be a part of the
6664 parameter pack "parm". */
6665 for (; arg_idx
< nargs
; ++arg_idx
)
6667 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6668 tree actual_parm
= TREE_VALUE (parm
);
6669 int pack_idx
= arg_idx
- parm_idx
;
6673 /* Once we've packed as many args as we have types, stop. */
6674 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
6676 else if (PACK_EXPANSION_P (arg
))
6677 /* We don't know how many args we have yet, just
6678 use the unconverted ones for now. */
6681 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
6684 if (arg
== error_mark_node
)
6686 if (complain
& tf_error
)
6687 error ("template argument %d is invalid", arg_idx
+ 1);
6690 arg
= convert_template_argument (actual_parm
,
6691 arg
, new_args
, complain
, parm_idx
,
6693 if (arg
== error_mark_node
)
6695 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
6698 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
6699 && TREE_VEC_LENGTH (packed_args
) > 0)
6701 error ("wrong number of template arguments (%d, should be %d)",
6702 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
6703 return error_mark_node
;
6706 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6707 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6708 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6711 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6712 TREE_TYPE (argument_pack
)
6713 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6714 TREE_CONSTANT (argument_pack
) = 1;
6717 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6718 #ifdef ENABLE_CHECKING
6719 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6720 TREE_VEC_LENGTH (packed_args
));
6722 return argument_pack
;
6725 /* Returns the number of pack expansions in the template argument vector
6729 pack_expansion_args_count (tree args
)
6734 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6736 tree elt
= TREE_VEC_ELT (args
, i
);
6737 if (elt
&& PACK_EXPANSION_P (elt
))
6743 /* Convert all template arguments to their appropriate types, and
6744 return a vector containing the innermost resulting template
6745 arguments. If any error occurs, return error_mark_node. Error and
6746 warning messages are issued under control of COMPLAIN.
6748 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6749 for arguments not specified in ARGS. Otherwise, if
6750 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6751 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6752 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6756 coerce_template_parms (tree parms
,
6759 tsubst_flags_t complain
,
6760 bool require_all_args
,
6761 bool use_default_args
)
6763 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6764 tree orig_inner_args
;
6767 tree new_inner_args
;
6768 int saved_unevaluated_operand
;
6769 int saved_inhibit_evaluation_warnings
;
6771 /* When used as a boolean value, indicates whether this is a
6772 variadic template parameter list. Since it's an int, we can also
6773 subtract it from nparms to get the number of non-variadic
6776 int variadic_args_p
= 0;
6777 int post_variadic_parms
= 0;
6779 if (args
== error_mark_node
)
6780 return error_mark_node
;
6782 nparms
= TREE_VEC_LENGTH (parms
);
6784 /* Determine if there are any parameter packs. */
6785 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6787 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6789 ++post_variadic_parms
;
6790 if (template_parameter_pack_p (tparm
))
6794 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6795 /* If there are no parameters that follow a parameter pack, we need to
6796 expand any argument packs so that we can deduce a parameter pack from
6797 some non-packed args followed by an argument pack, as in variadic85.C.
6798 If there are such parameters, we need to leave argument packs intact
6799 so the arguments are assigned properly. This can happen when dealing
6800 with a nested class inside a partial specialization of a class
6801 template, as in variadic92.C, or when deducing a template parameter pack
6802 from a sub-declarator, as in variadic114.C. */
6803 if (!post_variadic_parms
)
6804 inner_args
= expand_template_argument_pack (inner_args
);
6806 /* Count any pack expansion args. */
6807 variadic_args_p
= pack_expansion_args_count (inner_args
);
6809 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6810 if ((nargs
> nparms
&& !variadic_p
)
6811 || (nargs
< nparms
- variadic_p
6814 && (!use_default_args
6815 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6816 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6818 if (complain
& tf_error
)
6822 nparms
-= variadic_p
;
6823 error ("wrong number of template arguments "
6824 "(%d, should be %d or more)", nargs
, nparms
);
6827 error ("wrong number of template arguments "
6828 "(%d, should be %d)", nargs
, nparms
);
6831 error ("provided for %q+D", in_decl
);
6834 return error_mark_node
;
6836 /* We can't pass a pack expansion to a non-pack parameter of an alias
6837 template (DR 1430). */
6838 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6840 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6842 if (complain
& tf_error
)
6844 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6846 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6847 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6849 if (PACK_EXPANSION_P (arg
)
6850 && !template_parameter_pack_p (parm
))
6852 error ("pack expansion argument for non-pack parameter "
6853 "%qD of alias template %qD", parm
, in_decl
);
6854 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6861 return error_mark_node
;
6864 /* We need to evaluate the template arguments, even though this
6865 template-id may be nested within a "sizeof". */
6866 saved_unevaluated_operand
= cp_unevaluated_operand
;
6867 cp_unevaluated_operand
= 0;
6868 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6869 c_inhibit_evaluation_warnings
= 0;
6870 new_inner_args
= make_tree_vec (nparms
);
6871 new_args
= add_outermost_template_args (args
, new_inner_args
);
6872 int pack_adjust
= 0;
6873 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6878 /* Get the Ith template parameter. */
6879 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6881 if (parm
== error_mark_node
)
6883 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6887 /* Calculate the next argument. */
6888 if (arg_idx
< nargs
)
6889 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6893 if (template_parameter_pack_p (TREE_VALUE (parm
))
6894 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6896 /* Some arguments will be placed in the
6897 template parameter pack PARM. */
6898 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6899 inner_args
, arg_idx
,
6903 if (arg
== NULL_TREE
)
6905 /* We don't know how many args we have yet, just use the
6906 unconverted (and still packed) ones for now. */
6907 new_inner_args
= orig_inner_args
;
6912 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6914 /* Store this argument. */
6915 if (arg
== error_mark_node
)
6918 /* We are done with all of the arguments. */
6923 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
6924 arg_idx
+= pack_adjust
;
6931 if (PACK_EXPANSION_P (arg
))
6933 /* We don't know how many args we have yet, just
6934 use the unconverted ones for now. */
6935 new_inner_args
= inner_args
;
6940 else if (require_all_args
)
6942 /* There must be a default arg in this case. */
6943 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6945 /* The position of the first default template argument,
6946 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6948 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6949 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6950 arg_idx
- pack_adjust
);
6955 if (arg
== error_mark_node
)
6957 if (complain
& tf_error
)
6958 error ("template argument %d is invalid", arg_idx
+ 1);
6961 /* This only occurs if there was an error in the template
6962 parameter list itself (which we would already have
6963 reported) that we are trying to recover from, e.g., a class
6964 template with a parameter list such as
6965 template<typename..., typename>. */
6968 arg
= convert_template_argument (TREE_VALUE (parm
),
6969 arg
, new_args
, complain
,
6972 if (arg
== error_mark_node
)
6974 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
6976 cp_unevaluated_operand
= saved_unevaluated_operand
;
6977 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6979 if (variadic_p
&& arg_idx
< nargs
)
6981 if (complain
& tf_error
)
6983 error ("wrong number of template arguments "
6984 "(%d, should be %d)", nargs
, arg_idx
);
6986 error ("provided for %q+D", in_decl
);
6988 return error_mark_node
;
6992 return error_mark_node
;
6994 #ifdef ENABLE_CHECKING
6995 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6996 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6997 TREE_VEC_LENGTH (new_inner_args
));
7000 return new_inner_args
;
7003 /* Like coerce_template_parms. If PARMS represents all template
7004 parameters levels, this function returns a vector of vectors
7005 representing all the resulting argument levels. Note that in this
7006 case, only the innermost arguments are coerced because the
7007 outermost ones are supposed to have been coerced already.
7009 Otherwise, if PARMS represents only (the innermost) vector of
7010 parameters, this function returns a vector containing just the
7011 innermost resulting arguments. */
7014 coerce_innermost_template_parms (tree parms
,
7017 tsubst_flags_t complain
,
7018 bool require_all_args
,
7019 bool use_default_args
)
7021 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7022 int args_depth
= TMPL_ARGS_DEPTH (args
);
7025 if (parms_depth
> 1)
7027 coerced_args
= make_tree_vec (parms_depth
);
7031 for (level
= parms
, cur_depth
= parms_depth
;
7032 parms_depth
> 0 && level
!= NULL_TREE
;
7033 level
= TREE_CHAIN (level
), --cur_depth
)
7036 if (cur_depth
== args_depth
)
7037 l
= coerce_template_parms (TREE_VALUE (level
),
7038 args
, in_decl
, complain
,
7042 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7044 if (l
== error_mark_node
)
7045 return error_mark_node
;
7047 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7051 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7052 args
, in_decl
, complain
,
7055 return coerced_args
;
7058 /* Returns 1 if template args OT and NT are equivalent. */
7061 template_args_equal (tree ot
, tree nt
)
7065 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7068 if (TREE_CODE (nt
) == TREE_VEC
)
7069 /* For member templates */
7070 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7071 else if (PACK_EXPANSION_P (ot
))
7072 return (PACK_EXPANSION_P (nt
)
7073 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7074 PACK_EXPANSION_PATTERN (nt
))
7075 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7076 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7077 else if (ARGUMENT_PACK_P (ot
))
7082 if (!ARGUMENT_PACK_P (nt
))
7085 opack
= ARGUMENT_PACK_ARGS (ot
);
7086 npack
= ARGUMENT_PACK_ARGS (nt
);
7087 len
= TREE_VEC_LENGTH (opack
);
7088 if (TREE_VEC_LENGTH (npack
) != len
)
7090 for (i
= 0; i
< len
; ++i
)
7091 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7092 TREE_VEC_ELT (npack
, i
)))
7096 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7098 /* We get here probably because we are in the middle of substituting
7099 into the pattern of a pack expansion. In that case the
7100 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7101 interested in. So we want to use the initial pack argument for
7103 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7104 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7105 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7106 return template_args_equal (ot
, nt
);
7108 else if (TYPE_P (nt
))
7109 return TYPE_P (ot
) && same_type_p (ot
, nt
);
7110 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7113 return cp_tree_equal (ot
, nt
);
7116 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7117 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7118 NEWARG_PTR with the offending arguments if they are non-NULL. */
7121 comp_template_args_with_info (tree oldargs
, tree newargs
,
7122 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7126 if (oldargs
== newargs
)
7129 if (!oldargs
|| !newargs
)
7132 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7135 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7137 tree nt
= TREE_VEC_ELT (newargs
, i
);
7138 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7140 if (! template_args_equal (ot
, nt
))
7142 if (oldarg_ptr
!= NULL
)
7144 if (newarg_ptr
!= NULL
)
7152 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7153 of template arguments. Returns 0 otherwise. */
7156 comp_template_args (tree oldargs
, tree newargs
)
7158 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7162 add_pending_template (tree d
)
7164 tree ti
= (TYPE_P (d
)
7165 ? CLASSTYPE_TEMPLATE_INFO (d
)
7166 : DECL_TEMPLATE_INFO (d
));
7167 struct pending_template
*pt
;
7170 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7173 /* We are called both from instantiate_decl, where we've already had a
7174 tinst_level pushed, and instantiate_template, where we haven't.
7176 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7179 push_tinst_level (d
);
7181 pt
= ggc_alloc_pending_template ();
7183 pt
->tinst
= current_tinst_level
;
7184 if (last_pending_template
)
7185 last_pending_template
->next
= pt
;
7187 pending_templates
= pt
;
7189 last_pending_template
= pt
;
7191 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7198 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7199 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7200 documentation for TEMPLATE_ID_EXPR. */
7203 lookup_template_function (tree fns
, tree arglist
)
7207 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7208 return error_mark_node
;
7210 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7212 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7214 error ("%q#D is not a function template", fns
);
7215 return error_mark_node
;
7218 if (BASELINK_P (fns
))
7220 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7222 BASELINK_FUNCTIONS (fns
),
7227 type
= TREE_TYPE (fns
);
7228 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7229 type
= unknown_type_node
;
7231 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7234 /* Within the scope of a template class S<T>, the name S gets bound
7235 (in build_self_reference) to a TYPE_DECL for the class, not a
7236 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7237 or one of its enclosing classes, and that type is a template,
7238 return the associated TEMPLATE_DECL. Otherwise, the original
7241 Also handle the case when DECL is a TREE_LIST of ambiguous
7242 injected-class-names from different bases. */
7245 maybe_get_template_decl_from_type_decl (tree decl
)
7247 if (decl
== NULL_TREE
)
7250 /* DR 176: A lookup that finds an injected-class-name (10.2
7251 [class.member.lookup]) can result in an ambiguity in certain cases
7252 (for example, if it is found in more than one base class). If all of
7253 the injected-class-names that are found refer to specializations of
7254 the same class template, and if the name is followed by a
7255 template-argument-list, the reference refers to the class template
7256 itself and not a specialization thereof, and is not ambiguous. */
7257 if (TREE_CODE (decl
) == TREE_LIST
)
7259 tree t
, tmpl
= NULL_TREE
;
7260 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7262 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7265 else if (tmpl
!= elt
)
7268 if (tmpl
&& t
== NULL_TREE
)
7274 return (decl
!= NULL_TREE
7275 && DECL_SELF_REFERENCE_P (decl
)
7276 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7277 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7280 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7281 parameters, find the desired type.
7283 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7285 IN_DECL, if non-NULL, is the template declaration we are trying to
7288 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7289 the class we are looking up.
7291 Issue error and warning messages under control of COMPLAIN.
7293 If the template class is really a local class in a template
7294 function, then the FUNCTION_CONTEXT is the function in which it is
7297 ??? Note that this function is currently called *twice* for each
7298 template-id: the first time from the parser, while creating the
7299 incomplete type (finish_template_type), and the second type during the
7300 real instantiation (instantiate_template_class). This is surely something
7301 that we want to avoid. It also causes some problems with argument
7302 coercion (see convert_nontype_argument for more information on this). */
7305 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7306 int entering_scope
, tsubst_flags_t complain
)
7308 tree templ
= NULL_TREE
, parmlist
;
7315 if (identifier_p (d1
))
7317 tree value
= innermost_non_namespace_value (d1
);
7318 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7323 push_decl_namespace (context
);
7324 templ
= lookup_name (d1
);
7325 templ
= maybe_get_template_decl_from_type_decl (templ
);
7327 pop_decl_namespace ();
7330 context
= DECL_CONTEXT (templ
);
7332 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7334 tree type
= TREE_TYPE (d1
);
7336 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7337 an implicit typename for the second A. Deal with it. */
7338 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7339 type
= TREE_TYPE (type
);
7341 if (CLASSTYPE_TEMPLATE_INFO (type
))
7343 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7344 d1
= DECL_NAME (templ
);
7347 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7348 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7350 templ
= TYPE_TI_TEMPLATE (d1
);
7351 d1
= DECL_NAME (templ
);
7353 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7354 && DECL_TEMPLATE_RESULT (d1
)
7355 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7358 d1
= DECL_NAME (templ
);
7359 context
= DECL_CONTEXT (templ
);
7361 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7364 d1
= DECL_NAME (templ
);
7367 /* Issue an error message if we didn't find a template. */
7370 if (complain
& tf_error
)
7371 error ("%qT is not a template", d1
);
7372 return error_mark_node
;
7375 if (TREE_CODE (templ
) != TEMPLATE_DECL
7376 /* Make sure it's a user visible template, if it was named by
7378 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7379 && !PRIMARY_TEMPLATE_P (templ
)))
7381 if (complain
& tf_error
)
7383 error ("non-template type %qT used as a template", d1
);
7385 error ("for template declaration %q+D", in_decl
);
7387 return error_mark_node
;
7390 complain
&= ~tf_user
;
7392 /* An alias that just changes the name of a template is equivalent to the
7393 other template, so if any of the arguments are pack expansions, strip
7394 the alias to avoid problems with a pack expansion passed to a non-pack
7395 alias template parameter (DR 1430). */
7396 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7397 templ
= get_underlying_template (templ
);
7399 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7401 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7402 template arguments */
7408 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7410 /* Consider an example where a template template parameter declared as
7412 template <class T, class U = std::allocator<T> > class TT
7414 The template parameter level of T and U are one level larger than
7415 of TT. To proper process the default argument of U, say when an
7416 instantiation `TT<int>' is seen, we need to build the full
7417 arguments containing {int} as the innermost level. Outer levels,
7418 available when not appearing as default template argument, can be
7419 obtained from the arguments of the enclosing template.
7421 Suppose that TT is later substituted with std::vector. The above
7422 instantiation is `TT<int, std::allocator<T> >' with TT at
7423 level 1, and T at level 2, while the template arguments at level 1
7424 becomes {std::vector} and the inner level 2 is {int}. */
7426 outer
= DECL_CONTEXT (templ
);
7428 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7429 else if (current_template_parms
)
7430 /* This is an argument of the current template, so we haven't set
7431 DECL_CONTEXT yet. */
7432 outer
= current_template_args ();
7435 arglist
= add_to_template_args (outer
, arglist
);
7437 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7439 /*require_all_args=*/true,
7440 /*use_default_args=*/true);
7441 if (arglist2
== error_mark_node
7442 || (!uses_template_parms (arglist2
)
7443 && check_instantiated_args (templ
, arglist2
, complain
)))
7444 return error_mark_node
;
7446 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7451 tree template_type
= TREE_TYPE (templ
);
7454 tree found
= NULL_TREE
;
7457 int is_dependent_type
;
7458 int use_partial_inst_tmpl
= false;
7460 if (template_type
== error_mark_node
)
7461 /* An error occurred while building the template TEMPL, and a
7462 diagnostic has most certainly been emitted for that
7463 already. Let's propagate that error. */
7464 return error_mark_node
;
7466 gen_tmpl
= most_general_template (templ
);
7467 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7468 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7469 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7471 if (arg_depth
== 1 && parm_depth
> 1)
7473 /* We've been given an incomplete set of template arguments.
7476 template <class T> struct S1 {
7477 template <class U> struct S2 {};
7478 template <class U> struct S2<U*> {};
7481 we will be called with an ARGLIST of `U*', but the
7482 TEMPLATE will be `template <class T> template
7483 <class U> struct S1<T>::S2'. We must fill in the missing
7486 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7488 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7491 /* Now we should have enough arguments. */
7492 gcc_assert (parm_depth
== arg_depth
);
7494 /* From here on, we're only interested in the most general
7497 /* Calculate the BOUND_ARGS. These will be the args that are
7498 actually tsubst'd into the definition to create the
7502 /* We have multiple levels of arguments to coerce, at once. */
7504 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7506 tree bound_args
= make_tree_vec (parm_depth
);
7508 for (i
= saved_depth
,
7509 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7510 i
> 0 && t
!= NULL_TREE
;
7511 --i
, t
= TREE_CHAIN (t
))
7514 if (i
== saved_depth
)
7515 a
= coerce_template_parms (TREE_VALUE (t
),
7518 /*require_all_args=*/true,
7519 /*use_default_args=*/true);
7521 /* Outer levels should have already been coerced. */
7522 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7524 /* Don't process further if one of the levels fails. */
7525 if (a
== error_mark_node
)
7527 /* Restore the ARGLIST to its full size. */
7528 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7529 return error_mark_node
;
7532 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7534 /* We temporarily reduce the length of the ARGLIST so
7535 that coerce_template_parms will see only the arguments
7536 corresponding to the template parameters it is
7538 TREE_VEC_LENGTH (arglist
)--;
7541 /* Restore the ARGLIST to its full size. */
7542 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7544 arglist
= bound_args
;
7548 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7549 INNERMOST_TEMPLATE_ARGS (arglist
),
7552 /*require_all_args=*/true,
7553 /*use_default_args=*/true);
7555 if (arglist
== error_mark_node
)
7556 /* We were unable to bind the arguments. */
7557 return error_mark_node
;
7559 /* In the scope of a template class, explicit references to the
7560 template class refer to the type of the template, not any
7561 instantiation of it. For example, in:
7563 template <class T> class C { void f(C<T>); }
7565 the `C<T>' is just the same as `C'. Outside of the
7566 class, however, such a reference is an instantiation. */
7568 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7569 || currently_open_class (template_type
))
7570 /* comp_template_args is expensive, check it last. */
7571 && comp_template_args (TYPE_TI_ARGS (template_type
),
7573 return template_type
;
7575 /* If we already have this specialization, return it. */
7576 elt
.tmpl
= gen_tmpl
;
7578 hash
= hash_specialization (&elt
);
7579 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7585 is_dependent_type
= uses_template_parms (arglist
);
7587 /* If the deduced arguments are invalid, then the binding
7589 if (!is_dependent_type
7590 && check_instantiated_args (gen_tmpl
,
7591 INNERMOST_TEMPLATE_ARGS (arglist
),
7593 return error_mark_node
;
7595 if (!is_dependent_type
7596 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7597 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7598 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7600 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7601 DECL_NAME (gen_tmpl
),
7602 /*tag_scope=*/ts_global
);
7606 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7608 if (context
== error_mark_node
)
7609 return error_mark_node
;
7612 context
= global_namespace
;
7614 /* Create the type. */
7615 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7617 /* The user referred to a specialization of an alias
7618 template represented by GEN_TMPL.
7620 [temp.alias]/2 says:
7622 When a template-id refers to the specialization of an
7623 alias template, it is equivalent to the associated
7624 type obtained by substitution of its
7625 template-arguments for the template-parameters in the
7626 type-id of the alias template. */
7628 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7629 /* Note that the call above (by indirectly calling
7630 register_specialization in tsubst_decl) registers the
7631 TYPE_DECL representing the specialization of the alias
7632 template. So next time someone substitutes ARGLIST for
7633 the template parms into the alias template (GEN_TMPL),
7634 she'll get that TYPE_DECL back. */
7636 if (t
== error_mark_node
)
7639 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7641 if (!is_dependent_type
)
7643 set_current_access_from_decl (TYPE_NAME (template_type
));
7644 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7645 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7646 arglist
, complain
, in_decl
),
7647 SCOPED_ENUM_P (template_type
), NULL
);
7649 if (t
== error_mark_node
)
7654 /* We don't want to call start_enum for this type, since
7655 the values for the enumeration constants may involve
7656 template parameters. And, no one should be interested
7657 in the enumeration constants for such a type. */
7658 t
= cxx_make_type (ENUMERAL_TYPE
);
7659 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7661 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7662 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7663 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7665 else if (CLASS_TYPE_P (template_type
))
7667 t
= make_class_type (TREE_CODE (template_type
));
7668 CLASSTYPE_DECLARED_CLASS (t
)
7669 = CLASSTYPE_DECLARED_CLASS (template_type
);
7670 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7671 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7673 /* A local class. Make sure the decl gets registered properly. */
7674 if (context
== current_function_decl
)
7675 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7677 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7678 /* This instantiation is another name for the primary
7679 template type. Set the TYPE_CANONICAL field
7681 TYPE_CANONICAL (t
) = template_type
;
7682 else if (any_template_arguments_need_structural_equality_p (arglist
))
7683 /* Some of the template arguments require structural
7684 equality testing, so this template class requires
7685 structural equality testing. */
7686 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7691 /* If we called start_enum or pushtag above, this information
7692 will already be set up. */
7695 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7697 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7698 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7699 DECL_SOURCE_LOCATION (type_decl
)
7700 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7703 type_decl
= TYPE_NAME (t
);
7705 if (CLASS_TYPE_P (template_type
))
7707 TREE_PRIVATE (type_decl
)
7708 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7709 TREE_PROTECTED (type_decl
)
7710 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7711 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7713 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7714 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7718 /* Let's consider the explicit specialization of a member
7719 of a class template specialization that is implicitly instantiated,
7724 template<class U> struct M {}; //#0
7729 struct S<int>::M<char> //#1
7733 [temp.expl.spec]/4 says this is valid.
7735 In this case, when we write:
7738 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7741 When we encounter #1, we want to store the partial instantiation
7742 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7744 For all cases other than this "explicit specialization of member of a
7745 class template", we just want to store the most general template into
7746 the CLASSTYPE_TI_TEMPLATE of M.
7748 This case of "explicit specialization of member of a class template"
7750 1/ the enclosing class is an instantiation of, and therefore not
7751 the same as, the context of the most general template, and
7752 2/ we aren't looking at the partial instantiation itself, i.e.
7753 the innermost arguments are not the same as the innermost parms of
7754 the most general template.
7756 So it's only when 1/ and 2/ happens that we want to use the partial
7757 instantiation of the member template in lieu of its most general
7760 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7761 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7762 /* the enclosing class must be an instantiation... */
7763 && CLASS_TYPE_P (context
)
7764 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7766 tree partial_inst_args
;
7767 TREE_VEC_LENGTH (arglist
)--;
7768 ++processing_template_decl
;
7770 tsubst (INNERMOST_TEMPLATE_ARGS
7771 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7772 arglist
, complain
, NULL_TREE
);
7773 --processing_template_decl
;
7774 TREE_VEC_LENGTH (arglist
)++;
7775 use_partial_inst_tmpl
=
7776 /*...and we must not be looking at the partial instantiation
7778 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7782 if (!use_partial_inst_tmpl
)
7783 /* This case is easy; there are no member templates involved. */
7787 /* This is a full instantiation of a member template. Find
7788 the partial instantiation of which this is an instance. */
7790 /* Temporarily reduce by one the number of levels in the ARGLIST
7791 so as to avoid comparing the last set of arguments. */
7792 TREE_VEC_LENGTH (arglist
)--;
7793 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7794 TREE_VEC_LENGTH (arglist
)++;
7795 /* FOUND is either a proper class type, or an alias
7796 template specialization. In the later case, it's a
7797 TYPE_DECL, resulting from the substituting of arguments
7798 for parameters in the TYPE_DECL of the alias template
7799 done earlier. So be careful while getting the template
7801 found
= TREE_CODE (found
) == TYPE_DECL
7802 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7803 : CLASSTYPE_TI_TEMPLATE (found
);
7806 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7809 slot
= htab_find_slot_with_hash (type_specializations
,
7810 &elt
, hash
, INSERT
);
7811 entry
= ggc_alloc_spec_entry ();
7815 /* Note this use of the partial instantiation so we can check it
7816 later in maybe_process_partial_specialization. */
7817 DECL_TEMPLATE_INSTANTIATIONS (found
)
7818 = tree_cons (arglist
, t
,
7819 DECL_TEMPLATE_INSTANTIATIONS (found
));
7821 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7822 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7823 /* Now that the type has been registered on the instantiations
7824 list, we set up the enumerators. Because the enumeration
7825 constants may involve the enumeration type itself, we make
7826 sure to register the type first, and then create the
7827 constants. That way, doing tsubst_expr for the enumeration
7828 constants won't result in recursive calls here; we'll find
7829 the instantiation and exit above. */
7830 tsubst_enum (template_type
, t
, arglist
);
7832 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7833 /* If the type makes use of template parameters, the
7834 code that generates debugging information will crash. */
7835 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7837 /* Possibly limit visibility based on template args. */
7838 TREE_PUBLIC (type_decl
) = 1;
7839 determine_visibility (type_decl
);
7845 /* Wrapper for lookup_template_class_1. */
7848 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7849 int entering_scope
, tsubst_flags_t complain
)
7852 timevar_push (TV_TEMPLATE_INST
);
7853 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7854 entering_scope
, complain
);
7855 timevar_pop (TV_TEMPLATE_INST
);
7863 /* True when we should also visit template parameters that occur in
7864 non-deduced contexts. */
7865 bool include_nondeduced_p
;
7866 struct pointer_set_t
*visited
;
7869 /* Called from for_each_template_parm via walk_tree. */
7872 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7875 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7876 tree_fn_t fn
= pfd
->fn
;
7877 void *data
= pfd
->data
;
7880 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7881 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7882 pfd
->include_nondeduced_p
))
7883 return error_mark_node
;
7885 switch (TREE_CODE (t
))
7888 if (TYPE_PTRMEMFUNC_P (t
))
7894 if (!TYPE_TEMPLATE_INFO (t
))
7896 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7897 fn
, data
, pfd
->visited
,
7898 pfd
->include_nondeduced_p
))
7899 return error_mark_node
;
7903 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7904 fn
, data
, pfd
->visited
,
7905 pfd
->include_nondeduced_p
)
7906 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7907 fn
, data
, pfd
->visited
,
7908 pfd
->include_nondeduced_p
))
7909 return error_mark_node
;
7913 /* Since we're not going to walk subtrees, we have to do this
7915 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7916 pfd
->visited
, pfd
->include_nondeduced_p
))
7917 return error_mark_node
;
7921 /* Check the return type. */
7922 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7923 pfd
->include_nondeduced_p
))
7924 return error_mark_node
;
7926 /* Check the parameter types. Since default arguments are not
7927 instantiated until they are needed, the TYPE_ARG_TYPES may
7928 contain expressions that involve template parameters. But,
7929 no-one should be looking at them yet. And, once they're
7930 instantiated, they don't contain template parameters, so
7931 there's no point in looking at them then, either. */
7935 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7936 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7937 pfd
->visited
, pfd
->include_nondeduced_p
))
7938 return error_mark_node
;
7940 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7941 want walk_tree walking into them itself. */
7947 case UNDERLYING_TYPE
:
7948 if (pfd
->include_nondeduced_p
7949 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7951 pfd
->include_nondeduced_p
))
7952 return error_mark_node
;
7957 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7958 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7959 pfd
->visited
, pfd
->include_nondeduced_p
))
7960 return error_mark_node
;
7965 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7966 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7967 pfd
->visited
, pfd
->include_nondeduced_p
))
7968 return error_mark_node
;
7969 if (DECL_CONTEXT (t
)
7970 && pfd
->include_nondeduced_p
7971 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7972 pfd
->visited
, pfd
->include_nondeduced_p
))
7973 return error_mark_node
;
7976 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7977 /* Record template parameters such as `T' inside `TT<T>'. */
7978 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7979 pfd
->include_nondeduced_p
))
7980 return error_mark_node
;
7983 case TEMPLATE_TEMPLATE_PARM
:
7984 case TEMPLATE_TYPE_PARM
:
7985 case TEMPLATE_PARM_INDEX
:
7986 if (fn
&& (*fn
)(t
, data
))
7987 return error_mark_node
;
7989 return error_mark_node
;
7993 /* A template template parameter is encountered. */
7994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7995 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7996 pfd
->include_nondeduced_p
))
7997 return error_mark_node
;
7999 /* Already substituted template template parameter */
8005 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8007 pfd
->include_nondeduced_p
))
8008 return error_mark_node
;
8012 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8013 && pfd
->include_nondeduced_p
8014 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8015 (TREE_TYPE (t
)), fn
, data
,
8016 pfd
->visited
, pfd
->include_nondeduced_p
))
8017 return error_mark_node
;
8022 /* If there's no type, then this thing must be some expression
8023 involving template parameters. */
8024 if (!fn
&& !TREE_TYPE (t
))
8025 return error_mark_node
;
8030 case IMPLICIT_CONV_EXPR
:
8031 case REINTERPRET_CAST_EXPR
:
8032 case CONST_CAST_EXPR
:
8033 case STATIC_CAST_EXPR
:
8034 case DYNAMIC_CAST_EXPR
:
8038 case PSEUDO_DTOR_EXPR
:
8040 return error_mark_node
;
8047 /* We didn't find any template parameters we liked. */
8051 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8052 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8053 call FN with the parameter and the DATA.
8054 If FN returns nonzero, the iteration is terminated, and
8055 for_each_template_parm returns 1. Otherwise, the iteration
8056 continues. If FN never returns a nonzero value, the value
8057 returned by for_each_template_parm is 0. If FN is NULL, it is
8058 considered to be the function which always returns 1.
8060 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8061 parameters that occur in non-deduced contexts. When false, only
8062 visits those template parameters that can be deduced. */
8065 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8066 struct pointer_set_t
*visited
,
8067 bool include_nondeduced_p
)
8069 struct pair_fn_data pfd
;
8075 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8077 /* Walk the tree. (Conceptually, we would like to walk without
8078 duplicates, but for_each_template_parm_r recursively calls
8079 for_each_template_parm, so we would need to reorganize a fair
8080 bit to use walk_tree_without_duplicates, so we keep our own
8083 pfd
.visited
= visited
;
8085 pfd
.visited
= pointer_set_create ();
8086 result
= cp_walk_tree (&t
,
8087 for_each_template_parm_r
,
8089 pfd
.visited
) != NULL_TREE
;
8094 pointer_set_destroy (pfd
.visited
);
8101 /* Returns true if T depends on any template parameter. */
8104 uses_template_parms (tree t
)
8107 int saved_processing_template_decl
;
8109 saved_processing_template_decl
= processing_template_decl
;
8110 if (!saved_processing_template_decl
)
8111 processing_template_decl
= 1;
8113 dependent_p
= dependent_type_p (t
);
8114 else if (TREE_CODE (t
) == TREE_VEC
)
8115 dependent_p
= any_dependent_template_arguments_p (t
);
8116 else if (TREE_CODE (t
) == TREE_LIST
)
8117 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8118 || uses_template_parms (TREE_CHAIN (t
)));
8119 else if (TREE_CODE (t
) == TYPE_DECL
)
8120 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8123 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8124 || TREE_CODE (t
) == OVERLOAD
8127 || TREE_CODE (t
) == TRAIT_EXPR
8128 || TREE_CODE (t
) == CONSTRUCTOR
8129 || CONSTANT_CLASS_P (t
))
8130 dependent_p
= (type_dependent_expression_p (t
)
8131 || value_dependent_expression_p (t
));
8134 gcc_assert (t
== error_mark_node
);
8135 dependent_p
= false;
8138 processing_template_decl
= saved_processing_template_decl
;
8143 /* Returns true iff current_function_decl is an incompletely instantiated
8144 template. Useful instead of processing_template_decl because the latter
8145 is set to 0 during fold_non_dependent_expr. */
8148 in_template_function (void)
8150 tree fn
= current_function_decl
;
8152 ++processing_template_decl
;
8153 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8154 && DECL_TEMPLATE_INFO (fn
)
8155 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8156 --processing_template_decl
;
8160 /* Returns true if T depends on any template parameter with level LEVEL. */
8163 uses_template_parms_level (tree t
, int level
)
8165 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8166 /*include_nondeduced_p=*/true);
8169 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8170 ill-formed translation unit, i.e. a variable or function that isn't
8171 usable in a constant expression. */
8174 neglectable_inst_p (tree d
)
8177 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8178 : decl_maybe_constant_var_p (d
)));
8181 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8182 neglectable and instantiated from within an erroneous instantiation. */
8185 limit_bad_template_recursion (tree decl
)
8187 struct tinst_level
*lev
= current_tinst_level
;
8188 int errs
= errorcount
+ sorrycount
;
8189 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8192 for (; lev
; lev
= lev
->next
)
8193 if (neglectable_inst_p (lev
->decl
))
8196 return (lev
&& errs
> lev
->errors
);
8199 static int tinst_depth
;
8200 extern int max_tinst_depth
;
8203 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8205 /* We're starting to instantiate D; record the template instantiation context
8206 for diagnostics and to restore it later. */
8209 push_tinst_level (tree d
)
8211 struct tinst_level
*new_level
;
8213 if (tinst_depth
>= max_tinst_depth
)
8215 last_error_tinst_level
= current_tinst_level
;
8216 if (TREE_CODE (d
) == TREE_LIST
)
8217 error ("template instantiation depth exceeds maximum of %d (use "
8218 "-ftemplate-depth= to increase the maximum) substituting %qS",
8219 max_tinst_depth
, d
);
8221 error ("template instantiation depth exceeds maximum of %d (use "
8222 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8223 max_tinst_depth
, d
);
8225 print_instantiation_context ();
8230 /* If the current instantiation caused problems, don't let it instantiate
8231 anything else. Do allow deduction substitution and decls usable in
8232 constant expressions. */
8233 if (limit_bad_template_recursion (d
))
8236 new_level
= ggc_alloc_tinst_level ();
8237 new_level
->decl
= d
;
8238 new_level
->locus
= input_location
;
8239 new_level
->errors
= errorcount
+sorrycount
;
8240 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8241 new_level
->next
= current_tinst_level
;
8242 current_tinst_level
= new_level
;
8245 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8246 depth_reached
= tinst_depth
;
8251 /* We're done instantiating this template; return to the instantiation
8255 pop_tinst_level (void)
8257 /* Restore the filename and line number stashed away when we started
8258 this instantiation. */
8259 input_location
= current_tinst_level
->locus
;
8260 current_tinst_level
= current_tinst_level
->next
;
8264 /* We're instantiating a deferred template; restore the template
8265 instantiation context in which the instantiation was requested, which
8266 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8269 reopen_tinst_level (struct tinst_level
*level
)
8271 struct tinst_level
*t
;
8274 for (t
= level
; t
; t
= t
->next
)
8277 current_tinst_level
= level
;
8279 if (current_tinst_level
)
8280 current_tinst_level
->errors
= errorcount
+sorrycount
;
8284 /* Returns the TINST_LEVEL which gives the original instantiation
8287 struct tinst_level
*
8288 outermost_tinst_level (void)
8290 struct tinst_level
*level
= current_tinst_level
;
8293 level
= level
->next
;
8297 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8298 vector of template arguments, as for tsubst.
8300 Returns an appropriate tsubst'd friend declaration. */
8303 tsubst_friend_function (tree decl
, tree args
)
8307 if (TREE_CODE (decl
) == FUNCTION_DECL
8308 && DECL_TEMPLATE_INSTANTIATION (decl
)
8309 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8310 /* This was a friend declared with an explicit template
8311 argument list, e.g.:
8315 to indicate that f was a template instantiation, not a new
8316 function declaration. Now, we have to figure out what
8317 instantiation of what template. */
8319 tree template_id
, arglist
, fns
;
8322 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8324 /* Friend functions are looked up in the containing namespace scope.
8325 We must enter that scope, to avoid finding member functions of the
8326 current class with same name. */
8327 push_nested_namespace (ns
);
8328 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8329 tf_warning_or_error
, NULL_TREE
,
8330 /*integral_constant_expression_p=*/false);
8331 pop_nested_namespace (ns
);
8332 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8333 tf_warning_or_error
, NULL_TREE
);
8334 template_id
= lookup_template_function (fns
, arglist
);
8336 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8337 tmpl
= determine_specialization (template_id
, new_friend
,
8339 /*need_member_template=*/0,
8340 TREE_VEC_LENGTH (args
),
8342 return instantiate_template (tmpl
, new_args
, tf_error
);
8345 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8347 /* The NEW_FRIEND will look like an instantiation, to the
8348 compiler, but is not an instantiation from the point of view of
8349 the language. For example, we might have had:
8351 template <class T> struct S {
8352 template <class U> friend void f(T, U);
8355 Then, in S<int>, template <class U> void f(int, U) is not an
8356 instantiation of anything. */
8357 if (new_friend
== error_mark_node
)
8358 return error_mark_node
;
8360 DECL_USE_TEMPLATE (new_friend
) = 0;
8361 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8363 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8364 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8365 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8368 /* The mangled name for the NEW_FRIEND is incorrect. The function
8369 is not a template instantiation and should not be mangled like
8370 one. Therefore, we forget the mangling here; we'll recompute it
8371 later if we need it. */
8372 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8374 SET_DECL_RTL (new_friend
, NULL
);
8375 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8378 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8381 tree new_friend_template_info
;
8382 tree new_friend_result_template_info
;
8384 int new_friend_is_defn
;
8386 /* We must save some information from NEW_FRIEND before calling
8387 duplicate decls since that function will free NEW_FRIEND if
8389 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8390 new_friend_is_defn
=
8391 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8392 (template_for_substitution (new_friend
)))
8394 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8396 /* This declaration is a `primary' template. */
8397 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8399 new_friend_result_template_info
8400 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8403 new_friend_result_template_info
= NULL_TREE
;
8405 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8406 if (new_friend_is_defn
)
8407 DECL_INITIAL (new_friend
) = error_mark_node
;
8409 /* Inside pushdecl_namespace_level, we will push into the
8410 current namespace. However, the friend function should go
8411 into the namespace of the template. */
8412 ns
= decl_namespace_context (new_friend
);
8413 push_nested_namespace (ns
);
8414 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8415 pop_nested_namespace (ns
);
8417 if (old_decl
== error_mark_node
)
8418 return error_mark_node
;
8420 if (old_decl
!= new_friend
)
8422 /* This new friend declaration matched an existing
8423 declaration. For example, given:
8425 template <class T> void f(T);
8426 template <class U> class C {
8427 template <class T> friend void f(T) {}
8430 the friend declaration actually provides the definition
8431 of `f', once C has been instantiated for some type. So,
8432 old_decl will be the out-of-class template declaration,
8433 while new_friend is the in-class definition.
8435 But, if `f' was called before this point, the
8436 instantiation of `f' will have DECL_TI_ARGS corresponding
8437 to `T' but not to `U', references to which might appear
8438 in the definition of `f'. Previously, the most general
8439 template for an instantiation of `f' was the out-of-class
8440 version; now it is the in-class version. Therefore, we
8441 run through all specialization of `f', adding to their
8442 DECL_TI_ARGS appropriately. In particular, they need a
8443 new set of outer arguments, corresponding to the
8444 arguments for this class instantiation.
8446 The same situation can arise with something like this:
8449 template <class T> class C {
8453 when `C<int>' is instantiated. Now, `f(int)' is defined
8456 if (!new_friend_is_defn
)
8457 /* On the other hand, if the in-class declaration does
8458 *not* provide a definition, then we don't want to alter
8459 existing definitions. We can just leave everything
8464 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8465 tree new_args
= TI_ARGS (new_friend_template_info
);
8467 /* Overwrite whatever template info was there before, if
8468 any, with the new template information pertaining to
8470 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8472 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8474 /* We should have called reregister_specialization in
8476 gcc_assert (retrieve_specialization (new_template
,
8480 /* Instantiate it if the global has already been used. */
8481 if (DECL_ODR_USED (old_decl
))
8482 instantiate_decl (old_decl
, /*defer_ok=*/true,
8483 /*expl_inst_class_mem_p=*/false);
8489 /* Indicate that the old function template is a partial
8491 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8492 = new_friend_result_template_info
;
8494 gcc_assert (new_template
8495 == most_general_template (new_template
));
8496 gcc_assert (new_template
!= old_decl
);
8498 /* Reassign any specializations already in the hash table
8499 to the new more general template, and add the
8500 additional template args. */
8501 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8505 tree spec
= TREE_VALUE (t
);
8508 elt
.tmpl
= old_decl
;
8509 elt
.args
= DECL_TI_ARGS (spec
);
8510 elt
.spec
= NULL_TREE
;
8512 htab_remove_elt (decl_specializations
, &elt
);
8515 = add_outermost_template_args (new_args
,
8516 DECL_TI_ARGS (spec
));
8518 register_specialization
8519 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8522 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8526 /* The information from NEW_FRIEND has been merged into OLD_DECL
8527 by duplicate_decls. */
8528 new_friend
= old_decl
;
8533 tree context
= DECL_CONTEXT (new_friend
);
8537 template <class T> class C {
8538 template <class U> friend void C1<U>::f (); // case 1
8539 friend void C2<T>::f (); // case 2
8541 we only need to make sure CONTEXT is a complete type for
8542 case 2. To distinguish between the two cases, we note that
8543 CONTEXT of case 1 remains dependent type after tsubst while
8544 this isn't true for case 2. */
8545 ++processing_template_decl
;
8546 dependent_p
= dependent_type_p (context
);
8547 --processing_template_decl
;
8550 && !complete_type_or_else (context
, NULL_TREE
))
8551 return error_mark_node
;
8553 if (COMPLETE_TYPE_P (context
))
8555 tree fn
= new_friend
;
8556 /* do_friend adds the TEMPLATE_DECL for any member friend
8557 template even if it isn't a member template, i.e.
8558 template <class T> friend A<T>::f();
8559 Look through it in that case. */
8560 if (TREE_CODE (fn
) == TEMPLATE_DECL
8561 && !PRIMARY_TEMPLATE_P (fn
))
8562 fn
= DECL_TEMPLATE_RESULT (fn
);
8563 /* Check to see that the declaration is really present, and,
8564 possibly obtain an improved declaration. */
8565 fn
= check_classfn (context
, fn
, NULL_TREE
);
8575 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8576 template arguments, as for tsubst.
8578 Returns an appropriate tsubst'd friend type or error_mark_node on
8582 tsubst_friend_class (tree friend_tmpl
, tree args
)
8588 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8590 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8591 return TREE_TYPE (t
);
8594 context
= CP_DECL_CONTEXT (friend_tmpl
);
8596 if (context
!= global_namespace
)
8598 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8599 push_nested_namespace (context
);
8601 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8604 /* Look for a class template declaration. We look for hidden names
8605 because two friend declarations of the same template are the
8606 same. For example, in:
8609 template <typename> friend class F;
8611 template <typename> struct B {
8612 template <typename> friend class F;
8615 both F templates are the same. */
8616 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8617 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8619 /* But, if we don't find one, it might be because we're in a
8620 situation like this:
8628 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8629 for `S<int>', not the TEMPLATE_DECL. */
8630 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8632 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8633 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8636 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8638 /* The friend template has already been declared. Just
8639 check to see that the declarations match, and install any new
8640 default parameters. We must tsubst the default parameters,
8641 of course. We only need the innermost template parameters
8642 because that is all that redeclare_class_template will look
8644 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8645 > TMPL_ARGS_DEPTH (args
))
8648 location_t saved_input_location
;
8649 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8650 args
, tf_warning_or_error
);
8652 saved_input_location
= input_location
;
8653 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8654 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8655 input_location
= saved_input_location
;
8659 friend_type
= TREE_TYPE (tmpl
);
8663 /* The friend template has not already been declared. In this
8664 case, the instantiation of the template class will cause the
8665 injection of this template into the global scope. */
8666 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8667 if (tmpl
== error_mark_node
)
8668 return error_mark_node
;
8670 /* The new TMPL is not an instantiation of anything, so we
8671 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8672 the new type because that is supposed to be the corresponding
8673 template decl, i.e., TMPL. */
8674 DECL_USE_TEMPLATE (tmpl
) = 0;
8675 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8676 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8677 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8678 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8680 /* Inject this template into the global scope. */
8681 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8684 if (context
!= global_namespace
)
8686 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8687 pop_nested_namespace (context
);
8689 pop_nested_class ();
8695 /* Returns zero if TYPE cannot be completed later due to circularity.
8696 Otherwise returns one. */
8699 can_complete_type_without_circularity (tree type
)
8701 if (type
== NULL_TREE
|| type
== error_mark_node
)
8703 else if (COMPLETE_TYPE_P (type
))
8705 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8706 return can_complete_type_without_circularity (TREE_TYPE (type
));
8707 else if (CLASS_TYPE_P (type
)
8708 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8714 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8716 /* Apply any attributes which had to be deferred until instantiation
8717 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8718 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8721 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8722 tree args
, tsubst_flags_t complain
, tree in_decl
)
8724 tree last_dep
= NULL_TREE
;
8728 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8729 if (ATTR_IS_DEPENDENT (t
))
8732 attributes
= copy_list (attributes
);
8736 if (DECL_P (*decl_p
))
8738 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8740 p
= &DECL_ATTRIBUTES (*decl_p
);
8743 p
= &TYPE_ATTRIBUTES (*decl_p
);
8747 tree late_attrs
= NULL_TREE
;
8748 tree
*q
= &late_attrs
;
8750 for (*p
= attributes
; *p
; )
8753 if (ATTR_IS_DEPENDENT (t
))
8755 *p
= TREE_CHAIN (t
);
8756 TREE_CHAIN (t
) = NULL_TREE
;
8757 if ((flag_openmp
|| flag_cilkplus
)
8758 && is_attribute_p ("omp declare simd",
8759 get_attribute_name (t
))
8762 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8763 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8765 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8766 clauses
= finish_omp_clauses (clauses
);
8767 tree parms
= DECL_ARGUMENTS (*decl_p
);
8769 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8771 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8773 TREE_VALUE (t
) = NULL_TREE
;
8775 /* If the first attribute argument is an identifier, don't
8776 pass it through tsubst. Attributes like mode, format,
8777 cleanup and several target specific attributes expect it
8779 else if (attribute_takes_identifier_p (get_attribute_name (t
))
8783 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8785 /*integral_constant_expression_p=*/false);
8786 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8788 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8793 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8794 /*integral_constant_expression_p=*/false);
8796 q
= &TREE_CHAIN (t
);
8799 p
= &TREE_CHAIN (t
);
8802 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8806 /* Perform (or defer) access check for typedefs that were referenced
8807 from within the template TMPL code.
8808 This is a subroutine of instantiate_decl and instantiate_class_template.
8809 TMPL is the template to consider and TARGS is the list of arguments of
8813 perform_typedefs_access_check (tree tmpl
, tree targs
)
8815 location_t saved_location
;
8817 qualified_typedef_usage_t
*iter
;
8820 || (!CLASS_TYPE_P (tmpl
)
8821 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8824 saved_location
= input_location
;
8825 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8827 tree type_decl
= iter
->typedef_decl
;
8828 tree type_scope
= iter
->context
;
8830 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8833 if (uses_template_parms (type_decl
))
8834 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8835 if (uses_template_parms (type_scope
))
8836 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8838 /* Make access check error messages point to the location
8839 of the use of the typedef. */
8840 input_location
= iter
->locus
;
8841 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8842 type_decl
, type_decl
,
8843 tf_warning_or_error
);
8845 input_location
= saved_location
;
8849 instantiate_class_template_1 (tree type
)
8851 tree templ
, args
, pattern
, t
, member
;
8855 unsigned int saved_maximum_field_alignment
;
8858 if (type
== error_mark_node
)
8859 return error_mark_node
;
8861 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8862 || uses_template_parms (type
))
8865 /* Figure out which template is being instantiated. */
8866 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8867 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8869 /* Determine what specialization of the original template to
8871 t
= most_specialized_class (type
, tf_warning_or_error
);
8872 if (t
== error_mark_node
)
8874 TYPE_BEING_DEFINED (type
) = 1;
8875 return error_mark_node
;
8879 /* This TYPE is actually an instantiation of a partial
8880 specialization. We replace the innermost set of ARGS with
8881 the arguments appropriate for substitution. For example,
8884 template <class T> struct S {};
8885 template <class T> struct S<T*> {};
8887 and supposing that we are instantiating S<int*>, ARGS will
8888 presently be {int*} -- but we need {int}. */
8889 pattern
= TREE_TYPE (t
);
8890 args
= TREE_PURPOSE (t
);
8894 pattern
= TREE_TYPE (templ
);
8895 args
= CLASSTYPE_TI_ARGS (type
);
8898 /* If the template we're instantiating is incomplete, then clearly
8899 there's nothing we can do. */
8900 if (!COMPLETE_TYPE_P (pattern
))
8903 /* If we've recursively instantiated too many templates, stop. */
8904 if (! push_tinst_level (type
))
8907 /* Now we're really doing the instantiation. Mark the type as in
8908 the process of being defined. */
8909 TYPE_BEING_DEFINED (type
) = 1;
8911 /* We may be in the middle of deferred access check. Disable
8913 push_deferring_access_checks (dk_no_deferred
);
8915 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8917 push_to_top_level ();
8918 /* Use #pragma pack from the template context. */
8919 saved_maximum_field_alignment
= maximum_field_alignment
;
8920 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8922 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8924 /* Set the input location to the most specialized template definition.
8925 This is needed if tsubsting causes an error. */
8926 typedecl
= TYPE_MAIN_DECL (pattern
);
8927 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8928 DECL_SOURCE_LOCATION (typedecl
);
8930 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8931 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8932 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8933 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8934 if (ANON_AGGR_TYPE_P (pattern
))
8935 SET_ANON_AGGR_TYPE_P (type
);
8936 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8938 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8939 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8940 /* Adjust visibility for template arguments. */
8941 determine_visibility (TYPE_MAIN_DECL (type
));
8943 if (CLASS_TYPE_P (type
))
8944 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8946 pbinfo
= TYPE_BINFO (pattern
);
8948 /* We should never instantiate a nested class before its enclosing
8949 class; we need to look up the nested class by name before we can
8950 instantiate it, and that lookup should instantiate the enclosing
8952 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8953 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8955 base_list
= NULL_TREE
;
8956 if (BINFO_N_BASE_BINFOS (pbinfo
))
8962 /* We must enter the scope containing the type, as that is where
8963 the accessibility of types named in dependent bases are
8965 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8967 /* Substitute into each of the bases to determine the actual
8969 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8972 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8973 tree expanded_bases
= NULL_TREE
;
8976 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8979 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8980 args
, tf_error
, NULL_TREE
);
8981 if (expanded_bases
== error_mark_node
)
8984 len
= TREE_VEC_LENGTH (expanded_bases
);
8987 for (idx
= 0; idx
< len
; idx
++)
8990 /* Extract the already-expanded base class. */
8991 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8993 /* Substitute to figure out the base class. */
8994 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8997 if (base
== error_mark_node
)
9000 base_list
= tree_cons (access
, base
, base_list
);
9001 if (BINFO_VIRTUAL_P (pbase_binfo
))
9002 TREE_TYPE (base_list
) = integer_type_node
;
9006 /* The list is now in reverse order; correct that. */
9007 base_list
= nreverse (base_list
);
9010 pop_scope (pushed_scope
);
9012 /* Now call xref_basetypes to set up all the base-class
9014 xref_basetypes (type
, base_list
);
9016 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9017 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9018 args
, tf_error
, NULL_TREE
);
9019 fixup_attribute_variants (type
);
9021 /* Now that our base classes are set up, enter the scope of the
9022 class, so that name lookups into base classes, etc. will work
9023 correctly. This is precisely analogous to what we do in
9024 begin_class_definition when defining an ordinary non-template
9025 class, except we also need to push the enclosing classes. */
9026 push_nested_class (type
);
9028 /* Now members are processed in the order of declaration. */
9029 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9030 member
; member
= TREE_CHAIN (member
))
9032 tree t
= TREE_VALUE (member
);
9034 if (TREE_PURPOSE (member
))
9038 /* Build new CLASSTYPE_NESTED_UTDS. */
9041 bool class_template_p
;
9043 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9044 && TYPE_LANG_SPECIFIC (t
)
9045 && CLASSTYPE_IS_TEMPLATE (t
));
9046 /* If the member is a class template, then -- even after
9047 substitution -- there may be dependent types in the
9048 template argument list for the class. We increment
9049 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9050 that function will assume that no types are dependent
9051 when outside of a template. */
9052 if (class_template_p
)
9053 ++processing_template_decl
;
9054 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9055 if (class_template_p
)
9056 --processing_template_decl
;
9057 if (newtag
== error_mark_node
)
9060 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9062 tree name
= TYPE_IDENTIFIER (t
);
9064 if (class_template_p
)
9065 /* Unfortunately, lookup_template_class sets
9066 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9067 instantiation (i.e., for the type of a member
9068 template class nested within a template class.)
9069 This behavior is required for
9070 maybe_process_partial_specialization to work
9071 correctly, but is not accurate in this case;
9072 the TAG is not an instantiation of anything.
9073 (The corresponding TEMPLATE_DECL is an
9074 instantiation, but the TYPE is not.) */
9075 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9077 /* Now, we call pushtag to put this NEWTAG into the scope of
9078 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9079 pushtag calling push_template_decl. We don't have to do
9080 this for enums because it will already have been done in
9083 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9084 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9087 else if (DECL_DECLARES_FUNCTION_P (t
))
9089 /* Build new TYPE_METHODS. */
9092 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9093 ++processing_template_decl
;
9094 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9095 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9096 --processing_template_decl
;
9097 set_current_access_from_decl (r
);
9098 finish_member_declaration (r
);
9099 /* Instantiate members marked with attribute used. */
9100 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9102 if (TREE_CODE (r
) == FUNCTION_DECL
9103 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9104 cp_check_omp_declare_reduction (r
);
9108 /* Build new TYPE_FIELDS. */
9109 if (TREE_CODE (t
) == STATIC_ASSERT
)
9113 ++c_inhibit_evaluation_warnings
;
9115 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9116 tf_warning_or_error
, NULL_TREE
,
9117 /*integral_constant_expression_p=*/true);
9118 --c_inhibit_evaluation_warnings
;
9120 finish_static_assert (condition
,
9121 STATIC_ASSERT_MESSAGE (t
),
9122 STATIC_ASSERT_SOURCE_LOCATION (t
),
9125 else if (TREE_CODE (t
) != CONST_DECL
)
9128 tree vec
= NULL_TREE
;
9131 /* The file and line for this declaration, to
9132 assist in error message reporting. Since we
9133 called push_tinst_level above, we don't need to
9135 input_location
= DECL_SOURCE_LOCATION (t
);
9137 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9138 ++processing_template_decl
;
9139 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9140 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9141 --processing_template_decl
;
9143 if (TREE_CODE (r
) == TREE_VEC
)
9145 /* A capture pack became multiple fields. */
9147 len
= TREE_VEC_LENGTH (vec
);
9150 for (int i
= 0; i
< len
; ++i
)
9153 r
= TREE_VEC_ELT (vec
, i
);
9158 [t]he initialization (and any associated
9159 side-effects) of a static data member does
9160 not occur unless the static data member is
9161 itself used in a way that requires the
9162 definition of the static data member to
9165 Therefore, we do not substitute into the
9166 initialized for the static data member here. */
9167 finish_static_data_member_decl
9170 /*init_const_expr_p=*/false,
9171 /*asmspec_tree=*/NULL_TREE
,
9173 /* Instantiate members marked with attribute used. */
9174 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9177 else if (TREE_CODE (r
) == FIELD_DECL
)
9179 /* Determine whether R has a valid type and can be
9180 completed later. If R is invalid, then its type
9181 is replaced by error_mark_node. */
9182 tree rtype
= TREE_TYPE (r
);
9183 if (can_complete_type_without_circularity (rtype
))
9184 complete_type (rtype
);
9186 if (!COMPLETE_TYPE_P (rtype
))
9188 cxx_incomplete_type_error (r
, rtype
);
9189 TREE_TYPE (r
) = error_mark_node
;
9193 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9194 such a thing will already have been added to the field
9195 list by tsubst_enum in finish_member_declaration in the
9196 CLASSTYPE_NESTED_UTDS case above. */
9197 if (!(TREE_CODE (r
) == TYPE_DECL
9198 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9199 && DECL_ARTIFICIAL (r
)))
9201 set_current_access_from_decl (r
);
9202 finish_member_declaration (r
);
9210 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9211 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9213 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9215 tree friend_type
= t
;
9216 bool adjust_processing_template_decl
= false;
9218 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9220 /* template <class T> friend class C; */
9221 friend_type
= tsubst_friend_class (friend_type
, args
);
9222 adjust_processing_template_decl
= true;
9224 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9226 /* template <class T> friend class C::D; */
9227 friend_type
= tsubst (friend_type
, args
,
9228 tf_warning_or_error
, NULL_TREE
);
9229 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9230 friend_type
= TREE_TYPE (friend_type
);
9231 adjust_processing_template_decl
= true;
9233 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9234 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9236 /* This could be either
9240 when dependent_type_p is false or
9242 template <class U> friend class T::C;
9245 friend_type
= tsubst (friend_type
, args
,
9246 tf_warning_or_error
, NULL_TREE
);
9247 /* Bump processing_template_decl for correct
9248 dependent_type_p calculation. */
9249 ++processing_template_decl
;
9250 if (dependent_type_p (friend_type
))
9251 adjust_processing_template_decl
= true;
9252 --processing_template_decl
;
9254 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9255 && hidden_name_p (TYPE_NAME (friend_type
)))
9259 where C hasn't been declared yet. Let's lookup name
9260 from namespace scope directly, bypassing any name that
9261 come from dependent base class. */
9262 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9264 /* The call to xref_tag_from_type does injection for friend
9266 push_nested_namespace (ns
);
9268 xref_tag_from_type (friend_type
, NULL_TREE
,
9269 /*tag_scope=*/ts_current
);
9270 pop_nested_namespace (ns
);
9272 else if (uses_template_parms (friend_type
))
9273 /* friend class C<T>; */
9274 friend_type
= tsubst (friend_type
, args
,
9275 tf_warning_or_error
, NULL_TREE
);
9280 where C is already declared or
9282 friend class C<int>;
9284 We don't have to do anything in these cases. */
9286 if (adjust_processing_template_decl
)
9287 /* Trick make_friend_class into realizing that the friend
9288 we're adding is a template, not an ordinary class. It's
9289 important that we use make_friend_class since it will
9290 perform some error-checking and output cross-reference
9292 ++processing_template_decl
;
9294 if (friend_type
!= error_mark_node
)
9295 make_friend_class (type
, friend_type
, /*complain=*/false);
9297 if (adjust_processing_template_decl
)
9298 --processing_template_decl
;
9302 /* Build new DECL_FRIENDLIST. */
9305 /* The file and line for this declaration, to
9306 assist in error message reporting. Since we
9307 called push_tinst_level above, we don't need to
9309 input_location
= DECL_SOURCE_LOCATION (t
);
9311 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9313 ++processing_template_decl
;
9314 push_deferring_access_checks (dk_no_check
);
9317 r
= tsubst_friend_function (t
, args
);
9318 add_friend (type
, r
, /*complain=*/false);
9319 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9321 pop_deferring_access_checks ();
9322 --processing_template_decl
;
9328 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9330 tree decl
= lambda_function (type
);
9333 if (!DECL_TEMPLATE_INFO (decl
)
9334 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9335 instantiate_decl (decl
, false, false);
9337 /* We need to instantiate the capture list from the template
9338 after we've instantiated the closure members, but before we
9339 consider adding the conversion op. Also keep any captures
9340 that may have been added during instantiation of the op(). */
9341 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9343 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9344 args
, tf_warning_or_error
, NULL_TREE
,
9347 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9348 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9350 maybe_add_lambda_conv_op (type
);
9353 gcc_assert (errorcount
);
9356 /* Set the file and line number information to whatever is given for
9357 the class itself. This puts error messages involving generated
9358 implicit functions at a predictable point, and the same point
9359 that would be used for non-template classes. */
9360 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9362 unreverse_member_declarations (type
);
9363 finish_struct_1 (type
);
9364 TYPE_BEING_DEFINED (type
) = 0;
9366 /* We don't instantiate default arguments for member functions. 14.7.1:
9368 The implicit instantiation of a class template specialization causes
9369 the implicit instantiation of the declarations, but not of the
9370 definitions or default arguments, of the class member functions,
9371 member classes, static data members and member templates.... */
9373 /* Some typedefs referenced from within the template code need to be access
9374 checked at template instantiation time, i.e now. These types were
9375 added to the template at parsing time. Let's get those and perform
9376 the access checks then. */
9377 perform_typedefs_access_check (pattern
, args
);
9378 perform_deferred_access_checks (tf_warning_or_error
);
9379 pop_nested_class ();
9380 maximum_field_alignment
= saved_maximum_field_alignment
;
9382 pop_from_top_level ();
9383 pop_deferring_access_checks ();
9386 /* The vtable for a template class can be emitted in any translation
9387 unit in which the class is instantiated. When there is no key
9388 method, however, finish_struct_1 will already have added TYPE to
9389 the keyed_classes list. */
9390 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9391 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9396 /* Wrapper for instantiate_class_template_1. */
9399 instantiate_class_template (tree type
)
9402 timevar_push (TV_TEMPLATE_INST
);
9403 ret
= instantiate_class_template_1 (type
);
9404 timevar_pop (TV_TEMPLATE_INST
);
9409 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9415 else if (TYPE_P (t
))
9416 r
= tsubst (t
, args
, complain
, in_decl
);
9419 if (!(complain
& tf_warning
))
9420 ++c_inhibit_evaluation_warnings
;
9421 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9422 /*integral_constant_expression_p=*/true);
9423 if (!(complain
& tf_warning
))
9424 --c_inhibit_evaluation_warnings
;
9429 /* Given a function parameter pack TMPL_PARM and some function parameters
9430 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9431 and set *SPEC_P to point at the next point in the list. */
9434 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9436 /* Collect all of the extra "packed" parameters into an
9440 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9441 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9442 tree spec_parm
= *spec_p
;
9445 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9447 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9450 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9451 parmvec
= make_tree_vec (len
);
9452 parmtypevec
= make_tree_vec (len
);
9453 spec_parm
= *spec_p
;
9454 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9456 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9457 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9460 /* Build the argument packs. */
9461 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9462 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9463 TREE_TYPE (argpack
) = argtypepack
;
9464 *spec_p
= spec_parm
;
9469 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9470 NONTYPE_ARGUMENT_PACK. */
9473 make_fnparm_pack (tree spec_parm
)
9475 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9478 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9482 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9484 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9485 if (i
>= TREE_VEC_LENGTH (vec
))
9487 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9491 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9494 make_argument_pack_select (tree arg_pack
, unsigned index
)
9496 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9498 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9499 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9504 /* This is a subroutine of tsubst_pack_expansion.
9506 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9507 mechanism to store the (non complete list of) arguments of the
9508 substitution and return a non substituted pack expansion, in order
9509 to wait for when we have enough arguments to really perform the
9513 use_pack_expansion_extra_args_p (tree parm_packs
,
9517 /* If one pack has an expansion and another pack has a normal
9518 argument or if one pack has an empty argument and an another
9519 one hasn't then tsubst_pack_expansion cannot perform the
9520 substitution and need to fall back on the
9521 PACK_EXPANSION_EXTRA mechanism. */
9522 if (parm_packs
== NULL_TREE
)
9524 else if (has_empty_arg
)
9527 bool has_expansion_arg
= false;
9528 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9530 bool has_non_expansion_arg
= false;
9531 for (tree parm_pack
= parm_packs
;
9533 parm_pack
= TREE_CHAIN (parm_pack
))
9535 tree arg
= TREE_VALUE (parm_pack
);
9537 if (argument_pack_element_is_expansion_p (arg
, i
))
9538 has_expansion_arg
= true;
9540 has_non_expansion_arg
= true;
9543 if (has_expansion_arg
&& has_non_expansion_arg
)
9549 /* [temp.variadic]/6 says that:
9551 The instantiation of a pack expansion [...]
9552 produces a list E1,E2, ..., En, where N is the number of elements
9553 in the pack expansion parameters.
9555 This subroutine of tsubst_pack_expansion produces one of these Ei.
9557 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9558 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9559 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9560 INDEX is the index 'i' of the element Ei to produce. ARGS,
9561 COMPLAIN, and IN_DECL are the same parameters as for the
9562 tsubst_pack_expansion function.
9564 The function returns the resulting Ei upon successful completion,
9567 Note that this function possibly modifies the ARGS parameter, so
9568 it's the responsibility of the caller to restore it. */
9571 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9574 tree args
/* This parm gets
9576 tsubst_flags_t complain
,
9580 bool ith_elem_is_expansion
= false;
9582 /* For each parameter pack, change the substitution of the parameter
9583 pack to the ith argument in its argument pack, then expand the
9585 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9587 tree parm
= TREE_PURPOSE (pack
);
9588 tree arg_pack
= TREE_VALUE (pack
);
9589 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9591 ith_elem_is_expansion
|=
9592 argument_pack_element_is_expansion_p (arg_pack
, index
);
9594 /* Select the Ith argument from the pack. */
9595 if (TREE_CODE (parm
) == PARM_DECL
9596 || TREE_CODE (parm
) == FIELD_DECL
)
9600 aps
= make_argument_pack_select (arg_pack
, index
);
9602 register_local_specialization (aps
, parm
);
9605 aps
= retrieve_local_specialization (parm
);
9610 template_parm_level_and_index (parm
, &level
, &idx
);
9614 aps
= make_argument_pack_select (arg_pack
, index
);
9615 /* Update the corresponding argument. */
9616 TMPL_ARG (args
, level
, idx
) = aps
;
9619 /* Re-use the ARGUMENT_PACK_SELECT. */
9620 aps
= TMPL_ARG (args
, level
, idx
);
9622 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9625 /* Substitute into the PATTERN with the (possibly altered)
9627 if (pattern
== in_decl
)
9628 /* Expanding a fixed parameter pack from
9629 coerce_template_parameter_pack. */
9630 t
= tsubst_decl (pattern
, args
, complain
);
9631 else if (!TYPE_P (pattern
))
9632 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9633 /*integral_constant_expression_p=*/false);
9635 t
= tsubst (pattern
, args
, complain
, in_decl
);
9637 /* If the Ith argument pack element is a pack expansion, then
9638 the Ith element resulting from the substituting is going to
9639 be a pack expansion as well. */
9640 if (ith_elem_is_expansion
)
9641 t
= make_pack_expansion (t
);
9646 /* Substitute ARGS into T, which is an pack expansion
9647 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9648 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9649 (if only a partial substitution could be performed) or
9650 ERROR_MARK_NODE if there was an error. */
9652 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9656 tree pack
, packs
= NULL_TREE
;
9657 bool unsubstituted_packs
= false;
9660 struct pointer_map_t
*saved_local_specializations
= NULL
;
9661 bool need_local_specializations
= false;
9664 gcc_assert (PACK_EXPANSION_P (t
));
9665 pattern
= PACK_EXPANSION_PATTERN (t
);
9667 /* Add in any args remembered from an earlier partial instantiation. */
9668 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9670 levels
= TMPL_ARGS_DEPTH (args
);
9672 /* Determine the argument packs that will instantiate the parameter
9673 packs used in the expansion expression. While we're at it,
9674 compute the number of arguments to be expanded and make sure it
9676 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9677 pack
= TREE_CHAIN (pack
))
9679 tree parm_pack
= TREE_VALUE (pack
);
9680 tree arg_pack
= NULL_TREE
;
9681 tree orig_arg
= NULL_TREE
;
9684 if (TREE_CODE (parm_pack
) == BASES
)
9686 if (BASES_DIRECT (parm_pack
))
9687 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9688 args
, complain
, in_decl
, false));
9690 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9691 args
, complain
, in_decl
, false));
9693 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9695 if (PACK_EXPANSION_LOCAL_P (t
))
9696 arg_pack
= retrieve_local_specialization (parm_pack
);
9699 /* We can't rely on local_specializations for a parameter
9700 name used later in a function declaration (such as in a
9701 late-specified return type). Even if it exists, it might
9702 have the wrong value for a recursive call. Just make a
9703 dummy decl, since it's only used for its type. */
9704 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9705 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9706 /* Partial instantiation of the parm_pack, we can't build
9707 up an argument pack yet. */
9708 arg_pack
= NULL_TREE
;
9710 arg_pack
= make_fnparm_pack (arg_pack
);
9711 need_local_specializations
= true;
9714 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9715 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9719 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9721 if (level
<= levels
)
9722 arg_pack
= TMPL_ARG (args
, level
, idx
);
9725 orig_arg
= arg_pack
;
9726 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9727 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9729 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9730 /* This can only happen if we forget to expand an argument
9731 pack somewhere else. Just return an error, silently. */
9733 result
= make_tree_vec (1);
9734 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9741 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9743 /* Don't bother trying to do a partial substitution with
9744 incomplete packs; we'll try again after deduction. */
9745 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9750 else if (len
!= my_len
)
9752 if (!(complain
& tf_error
))
9753 /* Fail quietly. */;
9754 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9755 error ("mismatched argument pack lengths while expanding "
9759 error ("mismatched argument pack lengths while expanding "
9762 return error_mark_node
;
9765 /* Keep track of the parameter packs and their corresponding
9767 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9768 TREE_TYPE (packs
) = orig_arg
;
9772 /* We can't substitute for this parameter pack. We use a flag as
9773 well as the missing_level counter because function parameter
9774 packs don't have a level. */
9775 unsubstituted_packs
= true;
9779 /* We cannot expand this expansion expression, because we don't have
9780 all of the argument packs we need. */
9781 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9783 /* We got some full packs, but we can't substitute them in until we
9784 have values for all the packs. So remember these until then. */
9786 t
= make_pack_expansion (pattern
);
9787 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9790 else if (unsubstituted_packs
)
9792 /* There were no real arguments, we're just replacing a parameter
9793 pack with another version of itself. Substitute into the
9794 pattern and return a PACK_EXPANSION_*. The caller will need to
9796 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9797 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9798 /*integral_constant_expression_p=*/false);
9800 t
= tsubst (pattern
, args
, complain
, in_decl
);
9801 t
= make_pack_expansion (t
);
9805 gcc_assert (len
>= 0);
9807 if (need_local_specializations
)
9809 /* We're in a late-specified return type, so create our own local
9810 specializations map; the current map is either NULL or (in the
9811 case of recursive unification) might have bindings that we don't
9812 want to use or alter. */
9813 saved_local_specializations
= local_specializations
;
9814 local_specializations
= pointer_map_create ();
9817 /* For each argument in each argument pack, substitute into the
9819 result
= make_tree_vec (len
);
9820 for (i
= 0; i
< len
; ++i
)
9822 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9826 TREE_VEC_ELT (result
, i
) = t
;
9827 if (t
== error_mark_node
)
9829 result
= error_mark_node
;
9834 /* Update ARGS to restore the substitution from parameter packs to
9835 their argument packs. */
9836 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9838 tree parm
= TREE_PURPOSE (pack
);
9840 if (TREE_CODE (parm
) == PARM_DECL
9841 || TREE_CODE (parm
) == FIELD_DECL
)
9842 register_local_specialization (TREE_TYPE (pack
), parm
);
9847 if (TREE_VALUE (pack
) == NULL_TREE
)
9850 template_parm_level_and_index (parm
, &level
, &idx
);
9852 /* Update the corresponding argument. */
9853 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9854 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9857 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9861 if (need_local_specializations
)
9863 pointer_map_destroy (local_specializations
);
9864 local_specializations
= saved_local_specializations
;
9870 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9871 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9872 parameter packs; all parms generated from a function parameter pack will
9873 have the same DECL_PARM_INDEX. */
9876 get_pattern_parm (tree parm
, tree tmpl
)
9878 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9881 if (DECL_ARTIFICIAL (parm
))
9883 for (patparm
= DECL_ARGUMENTS (pattern
);
9884 patparm
; patparm
= DECL_CHAIN (patparm
))
9885 if (DECL_ARTIFICIAL (patparm
)
9886 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9891 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9892 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9893 gcc_assert (DECL_PARM_INDEX (patparm
)
9894 == DECL_PARM_INDEX (parm
));
9900 /* Substitute ARGS into the vector or list of template arguments T. */
9903 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9906 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9909 if (t
== error_mark_node
)
9910 return error_mark_node
;
9912 len
= TREE_VEC_LENGTH (t
);
9913 elts
= XALLOCAVEC (tree
, len
);
9915 for (i
= 0; i
< len
; i
++)
9917 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9920 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9921 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9922 else if (PACK_EXPANSION_P (orig_arg
))
9924 /* Substitute into an expansion expression. */
9925 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9927 if (TREE_CODE (new_arg
) == TREE_VEC
)
9928 /* Add to the expanded length adjustment the number of
9929 expanded arguments. We subtract one from this
9930 measurement, because the argument pack expression
9931 itself is already counted as 1 in
9932 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9933 the argument pack is empty. */
9934 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9936 else if (ARGUMENT_PACK_P (orig_arg
))
9938 /* Substitute into each of the arguments. */
9939 new_arg
= TYPE_P (orig_arg
)
9940 ? cxx_make_type (TREE_CODE (orig_arg
))
9941 : make_node (TREE_CODE (orig_arg
));
9943 SET_ARGUMENT_PACK_ARGS (
9945 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9946 args
, complain
, in_decl
));
9948 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9949 new_arg
= error_mark_node
;
9951 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9952 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9954 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9956 if (TREE_TYPE (new_arg
) == error_mark_node
)
9957 new_arg
= error_mark_node
;
9961 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9963 if (new_arg
== error_mark_node
)
9964 return error_mark_node
;
9967 if (new_arg
!= orig_arg
)
9974 /* Make space for the expanded arguments coming from template
9976 t
= make_tree_vec (len
+ expanded_len_adjust
);
9977 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9978 arguments for a member template.
9979 In that case each TREE_VEC in ORIG_T represents a level of template
9980 arguments, and ORIG_T won't carry any non defaulted argument count.
9981 It will rather be the nested TREE_VECs that will carry one.
9982 In other words, ORIG_T carries a non defaulted argument count only
9983 if it doesn't contain any nested TREE_VEC. */
9984 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9986 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9987 count
+= expanded_len_adjust
;
9988 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9990 for (i
= 0, out
= 0; i
< len
; i
++)
9992 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9993 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9994 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9998 /* Now expand the template argument pack "in place". */
9999 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
10000 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10004 TREE_VEC_ELT (t
, out
) = elts
[i
];
10012 /* Return the result of substituting ARGS into the template parameters
10013 given by PARMS. If there are m levels of ARGS and m + n levels of
10014 PARMS, then the result will contain n levels of PARMS. For
10015 example, if PARMS is `template <class T> template <class U>
10016 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10017 result will be `template <int*, double, class V>'. */
10020 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10022 tree r
= NULL_TREE
;
10025 /* When substituting into a template, we must set
10026 PROCESSING_TEMPLATE_DECL as the template parameters may be
10027 dependent if they are based on one-another, and the dependency
10028 predicates are short-circuit outside of templates. */
10029 ++processing_template_decl
;
10031 for (new_parms
= &r
;
10032 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10033 new_parms
= &(TREE_CHAIN (*new_parms
)),
10034 parms
= TREE_CHAIN (parms
))
10037 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10040 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10044 if (parms
== error_mark_node
)
10047 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10049 if (tuple
== error_mark_node
)
10052 TREE_VEC_ELT (new_vec
, i
) =
10053 tsubst_template_parm (tuple
, args
, complain
);
10057 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10058 - TMPL_ARGS_DEPTH (args
)),
10059 new_vec
, NULL_TREE
);
10062 --processing_template_decl
;
10067 /* Return the result of substituting ARGS into one template parameter
10068 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10069 parameter and which TREE_PURPOSE is the default argument of the
10070 template parameter. */
10073 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10075 tree default_value
, parm_decl
;
10077 if (args
== NULL_TREE
10079 || t
== error_mark_node
)
10082 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10084 default_value
= TREE_PURPOSE (t
);
10085 parm_decl
= TREE_VALUE (t
);
10087 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10088 if (TREE_CODE (parm_decl
) == PARM_DECL
10089 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10090 parm_decl
= error_mark_node
;
10091 default_value
= tsubst_template_arg (default_value
, args
,
10092 complain
, NULL_TREE
);
10094 return build_tree_list (default_value
, parm_decl
);
10097 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10098 type T. If T is not an aggregate or enumeration type, it is
10099 handled as if by tsubst. IN_DECL is as for tsubst. If
10100 ENTERING_SCOPE is nonzero, T is the context for a template which
10101 we are presently tsubst'ing. Return the substituted value. */
10104 tsubst_aggr_type (tree t
,
10106 tsubst_flags_t complain
,
10108 int entering_scope
)
10110 if (t
== NULL_TREE
)
10113 switch (TREE_CODE (t
))
10116 if (TYPE_PTRMEMFUNC_P (t
))
10117 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10119 /* Else fall through. */
10120 case ENUMERAL_TYPE
:
10122 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10127 int saved_unevaluated_operand
;
10128 int saved_inhibit_evaluation_warnings
;
10130 /* In "sizeof(X<I>)" we need to evaluate "I". */
10131 saved_unevaluated_operand
= cp_unevaluated_operand
;
10132 cp_unevaluated_operand
= 0;
10133 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10134 c_inhibit_evaluation_warnings
= 0;
10136 /* First, determine the context for the type we are looking
10138 context
= TYPE_CONTEXT (t
);
10139 if (context
&& TYPE_P (context
))
10141 context
= tsubst_aggr_type (context
, args
, complain
,
10142 in_decl
, /*entering_scope=*/1);
10143 /* If context is a nested class inside a class template,
10144 it may still need to be instantiated (c++/33959). */
10145 context
= complete_type (context
);
10148 /* Then, figure out what arguments are appropriate for the
10149 type we are trying to find. For example, given:
10151 template <class T> struct S;
10152 template <class T, class U> void f(T, U) { S<U> su; }
10154 and supposing that we are instantiating f<int, double>,
10155 then our ARGS will be {int, double}, but, when looking up
10156 S we only want {double}. */
10157 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10158 complain
, in_decl
);
10159 if (argvec
== error_mark_node
)
10160 r
= error_mark_node
;
10163 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10164 entering_scope
, complain
);
10165 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10168 cp_unevaluated_operand
= saved_unevaluated_operand
;
10169 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10174 /* This is not a template type, so there's nothing to do. */
10178 return tsubst (t
, args
, complain
, in_decl
);
10182 /* Substitute into the default argument ARG (a default argument for
10183 FN), which has the indicated TYPE. */
10186 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10188 tree saved_class_ptr
= NULL_TREE
;
10189 tree saved_class_ref
= NULL_TREE
;
10190 int errs
= errorcount
+ sorrycount
;
10192 /* This can happen in invalid code. */
10193 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10196 /* This default argument came from a template. Instantiate the
10197 default argument here, not in tsubst. In the case of
10206 we must be careful to do name lookup in the scope of S<T>,
10207 rather than in the current class. */
10208 push_access_scope (fn
);
10209 /* The "this" pointer is not valid in a default argument. */
10212 saved_class_ptr
= current_class_ptr
;
10213 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10214 saved_class_ref
= current_class_ref
;
10215 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10218 push_deferring_access_checks(dk_no_deferred
);
10219 /* The default argument expression may cause implicitly defined
10220 member functions to be synthesized, which will result in garbage
10221 collection. We must treat this situation as if we were within
10222 the body of function so as to avoid collecting live data on the
10225 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10226 complain
, NULL_TREE
,
10227 /*integral_constant_expression_p=*/false);
10229 pop_deferring_access_checks();
10231 /* Restore the "this" pointer. */
10234 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10235 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10238 if (errorcount
+sorrycount
> errs
10239 && (complain
& tf_warning_or_error
))
10240 inform (input_location
,
10241 " when instantiating default argument for call to %D", fn
);
10243 /* Make sure the default argument is reasonable. */
10244 arg
= check_default_argument (type
, arg
, complain
);
10246 pop_access_scope (fn
);
10251 /* Substitute into all the default arguments for FN. */
10254 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10259 tmpl_args
= DECL_TI_ARGS (fn
);
10261 /* If this function is not yet instantiated, we certainly don't need
10262 its default arguments. */
10263 if (uses_template_parms (tmpl_args
))
10265 /* Don't do this again for clones. */
10266 if (DECL_CLONED_FUNCTION_P (fn
))
10269 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10271 arg
= TREE_CHAIN (arg
))
10272 if (TREE_PURPOSE (arg
))
10273 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10275 TREE_PURPOSE (arg
),
10279 /* Substitute the ARGS into the T, which is a _DECL. Return the
10280 result of the substitution. Issue error and warning messages under
10281 control of COMPLAIN. */
10284 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10286 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10287 location_t saved_loc
;
10288 tree r
= NULL_TREE
;
10290 hashval_t hash
= 0;
10292 /* Set the filename and linenumber to improve error-reporting. */
10293 saved_loc
= input_location
;
10294 input_location
= DECL_SOURCE_LOCATION (t
);
10296 switch (TREE_CODE (t
))
10298 case TEMPLATE_DECL
:
10300 /* We can get here when processing a member function template,
10301 member class template, or template template parameter. */
10302 tree decl
= DECL_TEMPLATE_RESULT (t
);
10307 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10309 /* Template template parameter is treated here. */
10310 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10311 if (new_type
== error_mark_node
)
10312 RETURN (error_mark_node
);
10313 /* If we get a real template back, return it. This can happen in
10314 the context of most_specialized_class. */
10315 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10319 DECL_CHAIN (r
) = NULL_TREE
;
10320 TREE_TYPE (r
) = new_type
;
10321 DECL_TEMPLATE_RESULT (r
)
10322 = build_decl (DECL_SOURCE_LOCATION (decl
),
10323 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10324 DECL_TEMPLATE_PARMS (r
)
10325 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10327 TYPE_NAME (new_type
) = r
;
10331 /* We might already have an instance of this template.
10332 The ARGS are for the surrounding class type, so the
10333 full args contain the tsubst'd args for the context,
10334 plus the innermost args from the template decl. */
10335 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10336 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10337 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10338 /* Because this is a template, the arguments will still be
10339 dependent, even after substitution. If
10340 PROCESSING_TEMPLATE_DECL is not set, the dependency
10341 predicates will short-circuit. */
10342 ++processing_template_decl
;
10343 full_args
= tsubst_template_args (tmpl_args
, args
,
10344 complain
, in_decl
);
10345 --processing_template_decl
;
10346 if (full_args
== error_mark_node
)
10347 RETURN (error_mark_node
);
10349 /* If this is a default template template argument,
10350 tsubst might not have changed anything. */
10351 if (full_args
== tmpl_args
)
10354 hash
= hash_tmpl_and_args (t
, full_args
);
10355 spec
= retrieve_specialization (t
, full_args
, hash
);
10356 if (spec
!= NULL_TREE
)
10362 /* Make a new template decl. It will be similar to the
10363 original, but will record the current template arguments.
10364 We also create a new function declaration, which is just
10365 like the old one, but points to this new template, rather
10366 than the old one. */
10368 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10369 DECL_CHAIN (r
) = NULL_TREE
;
10371 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10373 if (TREE_CODE (decl
) == TYPE_DECL
10374 && !TYPE_DECL_ALIAS_P (decl
))
10377 ++processing_template_decl
;
10378 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10379 --processing_template_decl
;
10380 if (new_type
== error_mark_node
)
10381 RETURN (error_mark_node
);
10383 TREE_TYPE (r
) = new_type
;
10384 /* For a partial specialization, we need to keep pointing to
10385 the primary template. */
10386 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10387 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10388 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10389 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10390 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10395 ++processing_template_decl
;
10396 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10397 --processing_template_decl
;
10398 if (new_decl
== error_mark_node
)
10399 RETURN (error_mark_node
);
10401 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10402 DECL_TI_TEMPLATE (new_decl
) = r
;
10403 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10404 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10405 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10408 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10409 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10410 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10412 /* The template parameters for this new template are all the
10413 template parameters for the old template, except the
10414 outermost level of parameters. */
10415 DECL_TEMPLATE_PARMS (r
)
10416 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10419 if (PRIMARY_TEMPLATE_P (t
))
10420 DECL_PRIMARY_TEMPLATE (r
) = r
;
10422 if (TREE_CODE (decl
) != TYPE_DECL
)
10423 /* Record this non-type partial instantiation. */
10424 register_specialization (r
, t
,
10425 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10430 case FUNCTION_DECL
:
10433 tree argvec
= NULL_TREE
;
10441 /* Nobody should be tsubst'ing into non-template functions. */
10442 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10444 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10449 /* If T is not dependent, just return it. We have to
10450 increment PROCESSING_TEMPLATE_DECL because
10451 value_dependent_expression_p assumes that nothing is
10452 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10453 ++processing_template_decl
;
10454 dependent_p
= value_dependent_expression_p (t
);
10455 --processing_template_decl
;
10459 /* Calculate the most general template of which R is a
10460 specialization, and the complete set of arguments used to
10462 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10463 argvec
= tsubst_template_args (DECL_TI_ARGS
10464 (DECL_TEMPLATE_RESULT
10465 (DECL_TI_TEMPLATE (t
))),
10466 args
, complain
, in_decl
);
10467 if (argvec
== error_mark_node
)
10468 RETURN (error_mark_node
);
10470 /* Check to see if we already have this specialization. */
10471 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10472 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10480 /* We can see more levels of arguments than parameters if
10481 there was a specialization of a member template, like
10484 template <class T> struct S { template <class U> void f(); }
10485 template <> template <class U> void S<int>::f(U);
10487 Here, we'll be substituting into the specialization,
10488 because that's where we can find the code we actually
10489 want to generate, but we'll have enough arguments for
10490 the most general template.
10492 We also deal with the peculiar case:
10494 template <class T> struct S {
10495 template <class U> friend void f();
10497 template <class U> void f() {}
10499 template void f<double>();
10501 Here, the ARGS for the instantiation of will be {int,
10502 double}. But, we only need as many ARGS as there are
10503 levels of template parameters in CODE_PATTERN. We are
10504 careful not to get fooled into reducing the ARGS in
10507 template <class T> struct S { template <class U> void f(U); }
10508 template <class T> template <> void S<T>::f(int) {}
10510 which we can spot because the pattern will be a
10511 specialization in this case. */
10512 args_depth
= TMPL_ARGS_DEPTH (args
);
10514 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10515 if (args_depth
> parms_depth
10516 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10517 args
= get_innermost_template_args (args
, parms_depth
);
10521 /* This special case arises when we have something like this:
10523 template <class T> struct S {
10524 friend void f<int>(int, double);
10527 Here, the DECL_TI_TEMPLATE for the friend declaration
10528 will be an IDENTIFIER_NODE. We are being called from
10529 tsubst_friend_function, and we want only to create a
10530 new decl (R) with appropriate types so that we can call
10531 determine_specialization. */
10532 gen_tmpl
= NULL_TREE
;
10535 if (DECL_CLASS_SCOPE_P (t
))
10537 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10541 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10542 complain
, t
, /*entering_scope=*/1);
10547 ctx
= DECL_CONTEXT (t
);
10549 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10550 if (type
== error_mark_node
)
10551 RETURN (error_mark_node
);
10553 /* If we hit excessive deduction depth, the type is bogus even if
10554 it isn't error_mark_node, so don't build a decl. */
10555 if (excessive_deduction_depth
)
10556 RETURN (error_mark_node
);
10558 /* We do NOT check for matching decls pushed separately at this
10559 point, as they may not represent instantiations of this
10560 template, and in any case are considered separate under the
10563 DECL_USE_TEMPLATE (r
) = 0;
10564 TREE_TYPE (r
) = type
;
10565 /* Clear out the mangled name and RTL for the instantiation. */
10566 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10567 SET_DECL_RTL (r
, NULL
);
10568 /* Leave DECL_INITIAL set on deleted instantiations. */
10569 if (!DECL_DELETED_FN (r
))
10570 DECL_INITIAL (r
) = NULL_TREE
;
10571 DECL_CONTEXT (r
) = ctx
;
10573 /* OpenMP UDRs have the only argument a reference to the declared
10574 type. We want to diagnose if the declared type is a reference,
10575 which is invalid, but as references to references are usually
10576 quietly merged, diagnose it here. */
10577 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10580 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10581 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10582 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10583 error_at (DECL_SOURCE_LOCATION (t
),
10584 "reference type %qT in "
10585 "%<#pragma omp declare reduction%>", argtype
);
10586 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10587 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10591 if (member
&& DECL_CONV_FN_P (r
))
10592 /* Type-conversion operator. Reconstruct the name, in
10593 case it's the name of one of the template's parameters. */
10594 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10596 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10598 DECL_RESULT (r
) = NULL_TREE
;
10600 TREE_STATIC (r
) = 0;
10601 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10602 DECL_EXTERNAL (r
) = 1;
10603 /* If this is an instantiation of a function with internal
10604 linkage, we already know what object file linkage will be
10605 assigned to the instantiation. */
10606 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10607 DECL_DEFER_OUTPUT (r
) = 0;
10608 DECL_CHAIN (r
) = NULL_TREE
;
10609 DECL_PENDING_INLINE_INFO (r
) = 0;
10610 DECL_PENDING_INLINE_P (r
) = 0;
10611 DECL_SAVED_TREE (r
) = NULL_TREE
;
10612 DECL_STRUCT_FUNCTION (r
) = NULL
;
10614 /* We'll re-clone as appropriate in instantiate_template. */
10615 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10617 /* If we aren't complaining now, return on error before we register
10618 the specialization so that we'll complain eventually. */
10619 if ((complain
& tf_error
) == 0
10620 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10621 && !grok_op_properties (r
, /*complain=*/false))
10622 RETURN (error_mark_node
);
10624 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10625 this in the special friend case mentioned above where
10626 GEN_TMPL is NULL. */
10629 DECL_TEMPLATE_INFO (r
)
10630 = build_template_info (gen_tmpl
, argvec
);
10631 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10634 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10636 /* We instantiated this while substituting into
10637 the type earlier (template/friend54.C). */
10640 /* We're not supposed to instantiate default arguments
10641 until they are called, for a template. But, for a
10644 template <class T> void f ()
10645 { extern void g(int i = T()); }
10647 we should do the substitution when the template is
10648 instantiated. We handle the member function case in
10649 instantiate_class_template since the default arguments
10650 might refer to other members of the class. */
10652 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10653 && !uses_template_parms (argvec
))
10654 tsubst_default_arguments (r
, complain
);
10657 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10659 /* Copy the list of befriending classes. */
10660 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10662 friends
= &TREE_CHAIN (*friends
))
10664 *friends
= copy_node (*friends
);
10665 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10670 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10672 maybe_retrofit_in_chrg (r
);
10673 if (DECL_CONSTRUCTOR_P (r
))
10674 grok_ctor_properties (ctx
, r
);
10675 if (DECL_INHERITED_CTOR_BASE (r
))
10676 deduce_inheriting_ctor (r
);
10677 /* If this is an instantiation of a member template, clone it.
10678 If it isn't, that'll be handled by
10679 clone_constructors_and_destructors. */
10680 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10681 clone_function_decl (r
, /*update_method_vec_p=*/0);
10683 else if ((complain
& tf_error
) != 0
10684 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10685 && !grok_op_properties (r
, /*complain=*/true))
10686 RETURN (error_mark_node
);
10688 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10689 SET_DECL_FRIEND_CONTEXT (r
,
10690 tsubst (DECL_FRIEND_CONTEXT (t
),
10691 args
, complain
, in_decl
));
10693 /* Possibly limit visibility based on template args. */
10694 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10695 if (DECL_VISIBILITY_SPECIFIED (t
))
10697 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10698 DECL_ATTRIBUTES (r
)
10699 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10701 determine_visibility (r
);
10702 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10703 && !processing_template_decl
)
10704 defaulted_late_check (r
);
10706 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10707 args
, complain
, in_decl
);
10713 tree type
= NULL_TREE
;
10715 tree expanded_types
= NULL_TREE
;
10716 tree prev_r
= NULL_TREE
;
10717 tree first_r
= NULL_TREE
;
10719 if (DECL_PACK_P (t
))
10721 /* If there is a local specialization that isn't a
10722 parameter pack, it means that we're doing a "simple"
10723 substitution from inside tsubst_pack_expansion. Just
10724 return the local specialization (which will be a single
10726 tree spec
= retrieve_local_specialization (t
);
10728 && TREE_CODE (spec
) == PARM_DECL
10729 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10732 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10733 the parameters in this function parameter pack. */
10734 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10735 complain
, in_decl
);
10736 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10738 len
= TREE_VEC_LENGTH (expanded_types
);
10740 /* Zero-length parameter packs are boring. Just substitute
10743 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10748 /* All we did was update the type. Make a note of that. */
10749 type
= expanded_types
;
10750 expanded_types
= NULL_TREE
;
10754 /* Loop through all of the parameters we'll build. When T is
10755 a function parameter pack, LEN is the number of expanded
10756 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10758 for (i
= 0; i
< len
; ++i
)
10762 if (DECL_TEMPLATE_PARM_P (t
))
10763 SET_DECL_TEMPLATE_PARM_P (r
);
10765 if (expanded_types
)
10766 /* We're on the Ith parameter of the function parameter
10769 /* Get the Ith type. */
10770 type
= TREE_VEC_ELT (expanded_types
, i
);
10772 /* Rename the parameter to include the index. */
10774 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10777 /* We're dealing with a normal parameter. */
10778 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10780 type
= type_decays_to (type
);
10781 TREE_TYPE (r
) = type
;
10782 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10784 if (DECL_INITIAL (r
))
10786 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10787 DECL_INITIAL (r
) = TREE_TYPE (r
);
10789 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10790 complain
, in_decl
);
10793 DECL_CONTEXT (r
) = NULL_TREE
;
10795 if (!DECL_TEMPLATE_PARM_P (r
))
10796 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10798 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10799 args
, complain
, in_decl
);
10801 /* Keep track of the first new parameter we
10802 generate. That's what will be returned to the
10807 /* Build a proper chain of parameters when substituting
10808 into a function parameter pack. */
10810 DECL_CHAIN (prev_r
) = r
;
10813 /* If cp_unevaluated_operand is set, we're just looking for a
10814 single dummy parameter, so don't keep going. */
10815 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10816 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10817 complain
, DECL_CHAIN (t
));
10819 /* FIRST_R contains the start of the chain we've built. */
10826 tree type
= NULL_TREE
;
10827 tree vec
= NULL_TREE
;
10828 tree expanded_types
= NULL_TREE
;
10831 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10833 /* This field is a lambda capture pack. Return a TREE_VEC of
10834 the expanded fields to instantiate_class_template_1 and
10835 store them in the specializations hash table as a
10836 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10837 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10838 complain
, in_decl
);
10839 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10841 len
= TREE_VEC_LENGTH (expanded_types
);
10842 vec
= make_tree_vec (len
);
10846 /* All we did was update the type. Make a note of that. */
10847 type
= expanded_types
;
10848 expanded_types
= NULL_TREE
;
10852 for (int i
= 0; i
< len
; ++i
)
10855 if (expanded_types
)
10857 type
= TREE_VEC_ELT (expanded_types
, i
);
10859 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10862 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10864 if (type
== error_mark_node
)
10865 RETURN (error_mark_node
);
10866 TREE_TYPE (r
) = type
;
10867 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10869 if (DECL_C_BIT_FIELD (r
))
10870 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10871 non-bit-fields DECL_INITIAL is a non-static data member
10872 initializer, which gets deferred instantiation. */
10874 = tsubst_expr (DECL_INITIAL (t
), args
,
10876 /*integral_constant_expression_p=*/true);
10877 else if (DECL_INITIAL (t
))
10879 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10880 NSDMI in perform_member_init. Still set DECL_INITIAL
10881 so that we know there is one. */
10882 DECL_INITIAL (r
) = void_zero_node
;
10883 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10884 retrofit_lang_decl (r
);
10885 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10887 /* We don't have to set DECL_CONTEXT here; it is set by
10888 finish_member_declaration. */
10889 DECL_CHAIN (r
) = NULL_TREE
;
10891 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10892 args
, complain
, in_decl
);
10895 TREE_VEC_ELT (vec
, i
) = r
;
10901 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10902 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10903 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10904 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10905 TREE_TYPE (pack
) = tpack
;
10906 register_specialization (pack
, t
, args
, false, 0);
10912 /* We reach here only for member using decls. We also need to check
10913 uses_template_parms because DECL_DEPENDENT_P is not set for a
10914 using-declaration that designates a member of the current
10915 instantiation (c++/53549). */
10916 if (DECL_DEPENDENT_P (t
)
10917 || uses_template_parms (USING_DECL_SCOPE (t
)))
10919 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10920 complain
, in_decl
);
10921 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10922 r
= do_class_using_decl (inst_scope
, name
);
10924 r
= error_mark_node
;
10927 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10928 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10934 DECL_CHAIN (r
) = NULL_TREE
;
10941 tree argvec
= NULL_TREE
;
10942 tree gen_tmpl
= NULL_TREE
;
10944 tree tmpl
= NULL_TREE
;
10946 tree type
= NULL_TREE
;
10949 if (TREE_TYPE (t
) == error_mark_node
)
10950 RETURN (error_mark_node
);
10952 if (TREE_CODE (t
) == TYPE_DECL
10953 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10955 /* If this is the canonical decl, we don't have to
10956 mess with instantiations, and often we can't (for
10957 typename, template type parms and such). Note that
10958 TYPE_NAME is not correct for the above test if
10959 we've copied the type for a typedef. */
10960 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10961 if (type
== error_mark_node
)
10962 RETURN (error_mark_node
);
10963 r
= TYPE_NAME (type
);
10967 /* Check to see if we already have the specialization we
10970 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10972 /* T is a static data member or namespace-scope entity.
10973 We have to substitute into namespace-scope variables
10974 (even though such entities are never templates) because
10977 template <class T> void f() { extern T t; }
10979 where the entity referenced is not known until
10980 instantiation time. */
10982 ctx
= DECL_CONTEXT (t
);
10983 if (DECL_CLASS_SCOPE_P (t
))
10985 ctx
= tsubst_aggr_type (ctx
, args
,
10987 in_decl
, /*entering_scope=*/1);
10988 /* If CTX is unchanged, then T is in fact the
10989 specialization we want. That situation occurs when
10990 referencing a static data member within in its own
10991 class. We can use pointer equality, rather than
10992 same_type_p, because DECL_CONTEXT is always
10994 if (ctx
== DECL_CONTEXT (t
)
10995 && (TREE_CODE (t
) != TYPE_DECL
10996 /* ... unless T is a member template; in which
10997 case our caller can be willing to create a
10998 specialization of that template represented
11000 || !(DECL_TI_TEMPLATE (t
)
11001 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
11007 tmpl
= DECL_TI_TEMPLATE (t
);
11008 gen_tmpl
= most_general_template (tmpl
);
11009 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11010 if (argvec
== error_mark_node
)
11011 RETURN (error_mark_node
);
11012 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11013 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11018 /* A local variable. */
11020 /* Subsequent calls to pushdecl will fill this in. */
11022 spec
= retrieve_local_specialization (t
);
11024 /* If we already have the specialization we need, there is
11025 nothing more to do. */
11032 /* Create a new node for the specialization we need. */
11034 if (type
== NULL_TREE
)
11036 if (is_typedef_decl (t
))
11037 type
= DECL_ORIGINAL_TYPE (t
);
11039 type
= TREE_TYPE (t
);
11041 && VAR_HAD_UNKNOWN_BOUND (t
)
11042 && type
!= error_mark_node
)
11043 type
= strip_array_domain (type
);
11044 type
= tsubst (type
, args
, complain
, in_decl
);
11048 /* Even if the original location is out of scope, the
11049 newly substituted one is not. */
11050 DECL_DEAD_FOR_LOCAL (r
) = 0;
11051 DECL_INITIALIZED_P (r
) = 0;
11052 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11053 if (type
== error_mark_node
)
11054 RETURN (error_mark_node
);
11055 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11057 /* It may seem that this case cannot occur, since:
11062 declares a function, not a variable. However:
11065 template <typename T> void g() { T t; }
11066 template void g<f>();
11068 is an attempt to declare a variable with function
11070 error ("variable %qD has function type",
11071 /* R is not yet sufficiently initialized, so we
11072 just use its name. */
11074 RETURN (error_mark_node
);
11076 type
= complete_type (type
);
11077 /* Wait until cp_finish_decl to set this again, to handle
11078 circular dependency (template/instantiate6.C). */
11079 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11080 type
= check_var_type (DECL_NAME (r
), type
);
11082 if (DECL_HAS_VALUE_EXPR_P (t
))
11084 tree ve
= DECL_VALUE_EXPR (t
);
11085 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11086 /*constant_expression_p=*/false);
11087 if (REFERENCE_REF_P (ve
))
11089 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11090 ve
= TREE_OPERAND (ve
, 0);
11092 SET_DECL_VALUE_EXPR (r
, ve
);
11095 else if (DECL_SELF_REFERENCE_P (t
))
11096 SET_DECL_SELF_REFERENCE_P (r
);
11097 TREE_TYPE (r
) = type
;
11098 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11099 DECL_CONTEXT (r
) = ctx
;
11100 /* Clear out the mangled name and RTL for the instantiation. */
11101 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11102 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11103 SET_DECL_RTL (r
, NULL
);
11104 /* The initializer must not be expanded until it is required;
11105 see [temp.inst]. */
11106 DECL_INITIAL (r
) = NULL_TREE
;
11107 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11108 SET_DECL_RTL (r
, NULL
);
11109 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11112 /* Possibly limit visibility based on template args. */
11113 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11114 if (DECL_VISIBILITY_SPECIFIED (t
))
11116 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11117 DECL_ATTRIBUTES (r
)
11118 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11120 determine_visibility (r
);
11125 /* A static data member declaration is always marked
11126 external when it is declared in-class, even if an
11127 initializer is present. We mimic the non-template
11128 processing here. */
11129 DECL_EXTERNAL (r
) = 1;
11131 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11132 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
11133 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11135 else if (!cp_unevaluated_operand
)
11136 register_local_specialization (r
, t
);
11138 DECL_CHAIN (r
) = NULL_TREE
;
11140 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11142 args
, complain
, in_decl
);
11144 /* Preserve a typedef that names a type. */
11145 if (is_typedef_decl (r
))
11147 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11148 set_underlying_type (r
);
11151 layout_decl (r
, 0);
11156 gcc_unreachable ();
11161 /* Restore the file and line information. */
11162 input_location
= saved_loc
;
11167 /* Substitute into the ARG_TYPES of a function type.
11168 If END is a TREE_CHAIN, leave it and any following types
11172 tsubst_arg_types (tree arg_types
,
11175 tsubst_flags_t complain
,
11178 tree remaining_arg_types
;
11179 tree type
= NULL_TREE
;
11181 tree expanded_args
= NULL_TREE
;
11184 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11187 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11188 args
, end
, complain
, in_decl
);
11189 if (remaining_arg_types
== error_mark_node
)
11190 return error_mark_node
;
11192 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11194 /* For a pack expansion, perform substitution on the
11195 entire expression. Later on, we'll handle the arguments
11197 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11198 args
, complain
, in_decl
);
11200 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11201 /* So that we'll spin through the parameters, one by one. */
11202 i
= TREE_VEC_LENGTH (expanded_args
);
11205 /* We only partially substituted into the parameter
11206 pack. Our type is TYPE_PACK_EXPANSION. */
11207 type
= expanded_args
;
11208 expanded_args
= NULL_TREE
;
11216 type
= TREE_VEC_ELT (expanded_args
, i
);
11218 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11220 if (type
== error_mark_node
)
11221 return error_mark_node
;
11222 if (VOID_TYPE_P (type
))
11224 if (complain
& tf_error
)
11226 error ("invalid parameter type %qT", type
);
11228 error ("in declaration %q+D", in_decl
);
11230 return error_mark_node
;
11233 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11234 return error_mark_node
;
11236 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11237 top-level qualifiers as required. */
11238 type
= cv_unqualified (type_decays_to (type
));
11240 /* We do not substitute into default arguments here. The standard
11241 mandates that they be instantiated only when needed, which is
11242 done in build_over_call. */
11243 default_arg
= TREE_PURPOSE (arg_types
);
11245 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11247 /* We've instantiated a template before its default arguments
11248 have been parsed. This can happen for a nested template
11249 class, and is not an error unless we require the default
11250 argument in a call of this function. */
11251 remaining_arg_types
=
11252 tree_cons (default_arg
, type
, remaining_arg_types
);
11253 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11256 remaining_arg_types
=
11257 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11260 return remaining_arg_types
;
11263 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11264 *not* handle the exception-specification for FNTYPE, because the
11265 initial substitution of explicitly provided template parameters
11266 during argument deduction forbids substitution into the
11267 exception-specification:
11271 All references in the function type of the function template to the
11272 corresponding template parameters are replaced by the specified tem-
11273 plate argument values. If a substitution in a template parameter or
11274 in the function type of the function template results in an invalid
11275 type, type deduction fails. [Note: The equivalent substitution in
11276 exception specifications is done only when the function is instanti-
11277 ated, at which point a program is ill-formed if the substitution
11278 results in an invalid type.] */
11281 tsubst_function_type (tree t
,
11283 tsubst_flags_t complain
,
11290 /* The TYPE_CONTEXT is not used for function/method types. */
11291 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11293 /* Substitute the return type. */
11294 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11295 if (return_type
== error_mark_node
)
11296 return error_mark_node
;
11297 /* DR 486 clarifies that creation of a function type with an
11298 invalid return type is a deduction failure. */
11299 if (TREE_CODE (return_type
) == ARRAY_TYPE
11300 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11302 if (complain
& tf_error
)
11304 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11305 error ("function returning an array");
11307 error ("function returning a function");
11309 return error_mark_node
;
11312 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11313 return error_mark_node
;
11315 /* Substitute the argument types. */
11316 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11317 complain
, in_decl
);
11318 if (arg_types
== error_mark_node
)
11319 return error_mark_node
;
11321 /* Construct a new type node and return it. */
11322 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11324 fntype
= build_function_type (return_type
, arg_types
);
11325 fntype
= apply_memfn_quals (fntype
,
11326 type_memfn_quals (t
),
11327 type_memfn_rqual (t
));
11331 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11332 /* Don't pick up extra function qualifiers from the basetype. */
11333 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
11334 if (! MAYBE_CLASS_TYPE_P (r
))
11338 Type deduction may fail for any of the following
11341 -- Attempting to create "pointer to member of T" when T
11342 is not a class type. */
11343 if (complain
& tf_error
)
11344 error ("creating pointer to member function of non-class type %qT",
11346 return error_mark_node
;
11349 fntype
= build_method_type_directly (r
, return_type
,
11350 TREE_CHAIN (arg_types
));
11351 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11353 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11358 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11359 ARGS into that specification, and return the substituted
11360 specification. If there is no specification, return NULL_TREE. */
11363 tsubst_exception_specification (tree fntype
,
11365 tsubst_flags_t complain
,
11372 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11373 new_specs
= NULL_TREE
;
11374 if (specs
&& TREE_PURPOSE (specs
))
11376 /* A noexcept-specifier. */
11377 tree expr
= TREE_PURPOSE (specs
);
11378 if (TREE_CODE (expr
) == INTEGER_CST
)
11382 /* Defer instantiation of noexcept-specifiers to avoid
11383 excessive instantiations (c++/49107). */
11384 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11385 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11387 /* We already partially instantiated this member template,
11388 so combine the new args with the old. */
11389 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11390 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11391 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11392 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11396 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11397 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11401 new_specs
= tsubst_copy_and_build
11402 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11403 /*integral_constant_expression_p=*/true);
11404 new_specs
= build_noexcept_spec (new_specs
, complain
);
11408 if (! TREE_VALUE (specs
))
11415 tree expanded_specs
= NULL_TREE
;
11417 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11419 /* Expand the pack expansion type. */
11420 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11424 if (expanded_specs
== error_mark_node
)
11425 return error_mark_node
;
11426 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11427 len
= TREE_VEC_LENGTH (expanded_specs
);
11430 /* We're substituting into a member template, so
11431 we got a TYPE_PACK_EXPANSION back. Add that
11432 expansion and move on. */
11433 gcc_assert (TREE_CODE (expanded_specs
)
11434 == TYPE_PACK_EXPANSION
);
11435 new_specs
= add_exception_specifier (new_specs
,
11438 specs
= TREE_CHAIN (specs
);
11443 for (i
= 0; i
< len
; ++i
)
11445 if (expanded_specs
)
11446 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11448 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11449 if (spec
== error_mark_node
)
11451 new_specs
= add_exception_specifier (new_specs
, spec
,
11455 specs
= TREE_CHAIN (specs
);
11461 /* Take the tree structure T and replace template parameters used
11462 therein with the argument vector ARGS. IN_DECL is an associated
11463 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11464 Issue error and warning messages under control of COMPLAIN. Note
11465 that we must be relatively non-tolerant of extensions here, in
11466 order to preserve conformance; if we allow substitutions that
11467 should not be allowed, we may allow argument deductions that should
11468 not succeed, and therefore report ambiguous overload situations
11469 where there are none. In theory, we could allow the substitution,
11470 but indicate that it should have failed, and allow our caller to
11471 make sure that the right thing happens, but we don't try to do this
11474 This function is used for dealing with types, decls and the like;
11475 for expressions, use tsubst_expr or tsubst_copy. */
11478 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11480 enum tree_code code
;
11481 tree type
, r
= NULL_TREE
;
11483 if (t
== NULL_TREE
|| t
== error_mark_node
11484 || t
== integer_type_node
11485 || t
== void_type_node
11486 || t
== char_type_node
11487 || t
== unknown_type_node
11488 || TREE_CODE (t
) == NAMESPACE_DECL
11489 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11493 return tsubst_decl (t
, args
, complain
);
11495 if (args
== NULL_TREE
)
11498 code
= TREE_CODE (t
);
11500 if (code
== IDENTIFIER_NODE
)
11501 type
= IDENTIFIER_TYPE_VALUE (t
);
11503 type
= TREE_TYPE (t
);
11505 gcc_assert (type
!= unknown_type_node
);
11507 /* Reuse typedefs. We need to do this to handle dependent attributes,
11508 such as attribute aligned. */
11510 && typedef_variant_p (t
))
11512 tree decl
= TYPE_NAME (t
);
11514 if (alias_template_specialization_p (t
))
11516 /* DECL represents an alias template and we want to
11518 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11519 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11520 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11522 else if (DECL_CLASS_SCOPE_P (decl
)
11523 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11524 && uses_template_parms (DECL_CONTEXT (decl
)))
11526 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11527 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11528 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11530 else if (DECL_FUNCTION_SCOPE_P (decl
)
11531 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11532 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11533 r
= retrieve_local_specialization (decl
);
11535 /* The typedef is from a non-template context. */
11541 r
= cp_build_qualified_type_real
11542 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11543 complain
| tf_ignore_bad_quals
);
11548 /* We don't have an instantiation yet, so drop the typedef. */
11549 int quals
= cp_type_quals (t
);
11550 t
= DECL_ORIGINAL_TYPE (decl
);
11551 t
= cp_build_qualified_type_real (t
, quals
,
11552 complain
| tf_ignore_bad_quals
);
11557 && code
!= TYPENAME_TYPE
11558 && code
!= TEMPLATE_TYPE_PARM
11559 && code
!= IDENTIFIER_NODE
11560 && code
!= FUNCTION_TYPE
11561 && code
!= METHOD_TYPE
)
11562 type
= tsubst (type
, args
, complain
, in_decl
);
11563 if (type
== error_mark_node
)
11564 return error_mark_node
;
11570 case ENUMERAL_TYPE
:
11571 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11572 /*entering_scope=*/0);
11575 case IDENTIFIER_NODE
:
11586 if (t
== integer_type_node
)
11589 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11590 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11594 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11596 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11597 /*integral_constant_expression_p=*/false);
11599 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11601 if (TREE_CODE (max
) == NOP_EXPR
11602 && TREE_SIDE_EFFECTS (omax
)
11603 && !TREE_TYPE (max
))
11604 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11606 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11607 with TREE_SIDE_EFFECTS that indicates this is not an integral
11608 constant expression. */
11609 if (processing_template_decl
11610 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11612 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11613 TREE_SIDE_EFFECTS (max
) = 1;
11616 return compute_array_index_type (NULL_TREE
, max
, complain
);
11619 case TEMPLATE_TYPE_PARM
:
11620 case TEMPLATE_TEMPLATE_PARM
:
11621 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11622 case TEMPLATE_PARM_INDEX
:
11627 tree arg
= NULL_TREE
;
11631 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11632 template_parm_level_and_index (t
, &level
, &idx
);
11634 levels
= TMPL_ARGS_DEPTH (args
);
11635 if (level
<= levels
)
11637 arg
= TMPL_ARG (args
, level
, idx
);
11639 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11641 /* See through ARGUMENT_PACK_SELECT arguments. */
11642 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11643 /* If the selected argument is an expansion E, that most
11644 likely means we were called from
11645 gen_elem_of_pack_expansion_instantiation during the
11646 substituting of pack an argument pack (which Ith
11647 element is a pack expansion, where I is
11648 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11649 In this case, the Ith element resulting from this
11650 substituting is going to be a pack expansion, which
11651 pattern is the pattern of E. Let's return the
11653 gen_elem_of_pack_expansion_instantiation will
11654 build the resulting pack expansion from it. */
11655 if (PACK_EXPANSION_P (arg
))
11656 arg
= PACK_EXPANSION_PATTERN (arg
);
11660 if (arg
== error_mark_node
)
11661 return error_mark_node
;
11662 else if (arg
!= NULL_TREE
)
11664 if (ARGUMENT_PACK_P (arg
))
11665 /* If ARG is an argument pack, we don't actually want to
11666 perform a substitution here, because substitutions
11667 for argument packs are only done
11668 element-by-element. We can get to this point when
11669 substituting the type of a non-type template
11670 parameter pack, when that type actually contains
11671 template parameter packs from an outer template, e.g.,
11673 template<typename... Types> struct A {
11674 template<Types... Values> struct B { };
11678 if (code
== TEMPLATE_TYPE_PARM
)
11681 gcc_assert (TYPE_P (arg
));
11683 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11685 return cp_build_qualified_type_real
11686 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11688 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11690 /* We are processing a type constructed from a
11691 template template parameter. */
11692 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11693 args
, complain
, in_decl
);
11694 if (argvec
== error_mark_node
)
11695 return error_mark_node
;
11697 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11698 || TREE_CODE (arg
) == TEMPLATE_DECL
11699 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11701 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11702 /* Consider this code:
11704 template <template <class> class Template>
11706 template <class Arg> using Bind = Template<Arg>;
11709 template <template <class> class Template, class Arg>
11710 using Instantiate = Template<Arg>; //#0
11712 template <template <class> class Template,
11715 Instantiate<Internal<Template>::template Bind,
11718 When #1 is parsed, the
11719 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11720 parameter `Template' in #0 matches the
11721 UNBOUND_CLASS_TEMPLATE representing the argument
11722 `Internal<Template>::template Bind'; We then want
11723 to assemble the type `Bind<Argument>' that can't
11724 be fully created right now, because
11725 `Internal<Template>' not being complete, the Bind
11726 template cannot be looked up in that context. So
11727 we need to "store" `Bind<Argument>' for later
11728 when the context of Bind becomes complete. Let's
11729 store that in a TYPENAME_TYPE. */
11730 return make_typename_type (TYPE_CONTEXT (arg
),
11731 build_nt (TEMPLATE_ID_EXPR
,
11732 TYPE_IDENTIFIER (arg
),
11737 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11738 are resolving nested-types in the signature of a
11739 member function templates. Otherwise ARG is a
11740 TEMPLATE_DECL and is the real template to be
11742 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11743 arg
= TYPE_NAME (arg
);
11745 r
= lookup_template_class (arg
,
11747 DECL_CONTEXT (arg
),
11748 /*entering_scope=*/0,
11750 return cp_build_qualified_type_real
11751 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11754 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11755 return convert_from_reference (unshare_expr (arg
));
11759 /* This can happen during the attempted tsubst'ing in
11760 unify. This means that we don't yet have any information
11761 about the template parameter in question. */
11764 /* Early in template argument deduction substitution, we don't
11765 want to reduce the level of 'auto', or it will be confused
11766 with a normal template parm in subsequent deduction. */
11767 if (is_auto (t
) && (complain
& tf_partial
))
11770 /* If we get here, we must have been looking at a parm for a
11771 more deeply nested template. Make a new version of this
11772 template parameter, but with a lower level. */
11775 case TEMPLATE_TYPE_PARM
:
11776 case TEMPLATE_TEMPLATE_PARM
:
11777 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11778 if (cp_type_quals (t
))
11780 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11781 r
= cp_build_qualified_type_real
11782 (r
, cp_type_quals (t
),
11783 complain
| (code
== TEMPLATE_TYPE_PARM
11784 ? tf_ignore_bad_quals
: 0));
11789 TEMPLATE_TYPE_PARM_INDEX (r
)
11790 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11791 r
, levels
, args
, complain
);
11792 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11793 TYPE_MAIN_VARIANT (r
) = r
;
11794 TYPE_POINTER_TO (r
) = NULL_TREE
;
11795 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11797 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11798 /* We have reduced the level of the template
11799 template parameter, but not the levels of its
11800 template parameters, so canonical_type_parameter
11801 will not be able to find the canonical template
11802 template parameter for this level. Thus, we
11803 require structural equality checking to compare
11804 TEMPLATE_TEMPLATE_PARMs. */
11805 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11806 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11807 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11809 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11811 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11813 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11814 complain
, in_decl
);
11815 if (argvec
== error_mark_node
)
11816 return error_mark_node
;
11818 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11819 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11824 case TEMPLATE_PARM_INDEX
:
11825 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11829 gcc_unreachable ();
11837 tree purpose
, value
, chain
;
11839 if (t
== void_list_node
)
11842 purpose
= TREE_PURPOSE (t
);
11845 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11846 if (purpose
== error_mark_node
)
11847 return error_mark_node
;
11849 value
= TREE_VALUE (t
);
11852 value
= tsubst (value
, args
, complain
, in_decl
);
11853 if (value
== error_mark_node
)
11854 return error_mark_node
;
11856 chain
= TREE_CHAIN (t
);
11857 if (chain
&& chain
!= void_type_node
)
11859 chain
= tsubst (chain
, args
, complain
, in_decl
);
11860 if (chain
== error_mark_node
)
11861 return error_mark_node
;
11863 if (purpose
== TREE_PURPOSE (t
)
11864 && value
== TREE_VALUE (t
)
11865 && chain
== TREE_CHAIN (t
))
11867 return hash_tree_cons (purpose
, value
, chain
);
11871 /* We should never be tsubsting a binfo. */
11872 gcc_unreachable ();
11875 /* A vector of template arguments. */
11876 gcc_assert (!type
);
11877 return tsubst_template_args (t
, args
, complain
, in_decl
);
11880 case REFERENCE_TYPE
:
11882 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11887 Type deduction may fail for any of the following
11890 -- Attempting to create a pointer to reference type.
11891 -- Attempting to create a reference to a reference type or
11892 a reference to void.
11894 Core issue 106 says that creating a reference to a reference
11895 during instantiation is no longer a cause for failure. We
11896 only enforce this check in strict C++98 mode. */
11897 if ((TREE_CODE (type
) == REFERENCE_TYPE
11898 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11899 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11901 static location_t last_loc
;
11903 /* We keep track of the last time we issued this error
11904 message to avoid spewing a ton of messages during a
11905 single bad template instantiation. */
11906 if (complain
& tf_error
11907 && last_loc
!= input_location
)
11909 if (VOID_TYPE_P (type
))
11910 error ("forming reference to void");
11911 else if (code
== POINTER_TYPE
)
11912 error ("forming pointer to reference type %qT", type
);
11914 error ("forming reference to reference type %qT", type
);
11915 last_loc
= input_location
;
11918 return error_mark_node
;
11920 else if (TREE_CODE (type
) == FUNCTION_TYPE
11921 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11922 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11924 if (complain
& tf_error
)
11926 if (code
== POINTER_TYPE
)
11927 error ("forming pointer to qualified function type %qT",
11930 error ("forming reference to qualified function type %qT",
11933 return error_mark_node
;
11935 else if (code
== POINTER_TYPE
)
11937 r
= build_pointer_type (type
);
11938 if (TREE_CODE (type
) == METHOD_TYPE
)
11939 r
= build_ptrmemfunc_type (r
);
11941 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11942 /* In C++0x, during template argument substitution, when there is an
11943 attempt to create a reference to a reference type, reference
11944 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11946 "If a template-argument for a template-parameter T names a type
11947 that is a reference to a type A, an attempt to create the type
11948 'lvalue reference to cv T' creates the type 'lvalue reference to
11949 A,' while an attempt to create the type type rvalue reference to
11950 cv T' creates the type T"
11952 r
= cp_build_reference_type
11954 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11956 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11957 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11959 if (cxx_dialect
>= cxx1y
11960 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11961 && array_of_runtime_bound_p (type
)
11962 && (flag_iso
|| warn_vla
> 0))
11964 if (complain
& tf_warning_or_error
)
11966 (input_location
, OPT_Wvla
,
11967 code
== REFERENCE_TYPE
11968 ? G_("cannot declare reference to array of runtime bound")
11969 : G_("cannot declare pointer to array of runtime bound"));
11971 r
= error_mark_node
;
11974 if (r
!= error_mark_node
)
11975 /* Will this ever be needed for TYPE_..._TO values? */
11982 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11983 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11987 Type deduction may fail for any of the following
11990 -- Attempting to create "pointer to member of T" when T
11991 is not a class type. */
11992 if (complain
& tf_error
)
11993 error ("creating pointer to member of non-class type %qT", r
);
11994 return error_mark_node
;
11996 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11998 if (complain
& tf_error
)
11999 error ("creating pointer to member reference type %qT", type
);
12000 return error_mark_node
;
12002 if (VOID_TYPE_P (type
))
12004 if (complain
& tf_error
)
12005 error ("creating pointer to member of type void");
12006 return error_mark_node
;
12008 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12009 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12011 /* The type of the implicit object parameter gets its
12012 cv-qualifiers from the FUNCTION_TYPE. */
12015 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12016 type_memfn_rqual (type
));
12017 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12018 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12022 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12026 case FUNCTION_TYPE
:
12031 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12032 if (fntype
== error_mark_node
)
12033 return error_mark_node
;
12035 /* Substitute the exception specification. */
12036 specs
= tsubst_exception_specification (t
, args
, complain
,
12037 in_decl
, /*defer_ok*/true);
12038 if (specs
== error_mark_node
)
12039 return error_mark_node
;
12041 fntype
= build_exception_variant (fntype
, specs
);
12046 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12047 if (domain
== error_mark_node
)
12048 return error_mark_node
;
12050 /* As an optimization, we avoid regenerating the array type if
12051 it will obviously be the same as T. */
12052 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12055 /* These checks should match the ones in grokdeclarator.
12059 The deduction may fail for any of the following reasons:
12061 -- Attempting to create an array with an element type that
12062 is void, a function type, or a reference type, or [DR337]
12063 an abstract class type. */
12064 if (VOID_TYPE_P (type
)
12065 || TREE_CODE (type
) == FUNCTION_TYPE
12066 || TREE_CODE (type
) == REFERENCE_TYPE
)
12068 if (complain
& tf_error
)
12069 error ("creating array of %qT", type
);
12070 return error_mark_node
;
12073 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12074 return error_mark_node
;
12076 r
= build_cplus_array_type (type
, domain
);
12078 if (TYPE_USER_ALIGN (t
))
12080 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12081 TYPE_USER_ALIGN (r
) = 1;
12087 case TYPENAME_TYPE
:
12089 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12090 in_decl
, /*entering_scope=*/1);
12091 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12092 complain
, in_decl
);
12094 if (ctx
== error_mark_node
|| f
== error_mark_node
)
12095 return error_mark_node
;
12097 if (!MAYBE_CLASS_TYPE_P (ctx
))
12099 if (complain
& tf_error
)
12100 error ("%qT is not a class, struct, or union type", ctx
);
12101 return error_mark_node
;
12103 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
12105 /* Normally, make_typename_type does not require that the CTX
12106 have complete type in order to allow things like:
12108 template <class T> struct S { typename S<T>::X Y; };
12110 But, such constructs have already been resolved by this
12111 point, so here CTX really should have complete type, unless
12112 it's a partial instantiation. */
12113 ctx
= complete_type (ctx
);
12114 if (!COMPLETE_TYPE_P (ctx
))
12116 if (complain
& tf_error
)
12117 cxx_incomplete_type_error (NULL_TREE
, ctx
);
12118 return error_mark_node
;
12122 f
= make_typename_type (ctx
, f
, typename_type
,
12123 complain
| tf_keep_type_decl
);
12124 if (f
== error_mark_node
)
12126 if (TREE_CODE (f
) == TYPE_DECL
)
12128 complain
|= tf_ignore_bad_quals
;
12132 if (TREE_CODE (f
) != TYPENAME_TYPE
)
12134 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
12136 if (complain
& tf_error
)
12137 error ("%qT resolves to %qT, which is not an enumeration type",
12140 return error_mark_node
;
12142 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
12144 if (complain
& tf_error
)
12145 error ("%qT resolves to %qT, which is is not a class type",
12148 return error_mark_node
;
12152 return cp_build_qualified_type_real
12153 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12156 case UNBOUND_CLASS_TEMPLATE
:
12158 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12159 in_decl
, /*entering_scope=*/1);
12160 tree name
= TYPE_IDENTIFIER (t
);
12161 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12163 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12164 return error_mark_node
;
12167 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12168 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12175 ++cp_unevaluated_operand
;
12176 ++c_inhibit_evaluation_warnings
;
12178 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12180 /*integral_constant_expression_p=*/false);
12182 --cp_unevaluated_operand
;
12183 --c_inhibit_evaluation_warnings
;
12185 type
= finish_typeof (type
);
12186 return cp_build_qualified_type_real (type
,
12188 | cp_type_quals (type
),
12192 case DECLTYPE_TYPE
:
12196 ++cp_unevaluated_operand
;
12197 ++c_inhibit_evaluation_warnings
;
12199 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12200 complain
|tf_decltype
, in_decl
,
12201 /*function_p*/false,
12202 /*integral_constant_expression*/false);
12204 --cp_unevaluated_operand
;
12205 --c_inhibit_evaluation_warnings
;
12207 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12208 type
= lambda_capture_field_type (type
,
12209 DECLTYPE_FOR_INIT_CAPTURE (t
));
12210 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12211 type
= lambda_proxy_type (type
);
12214 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12215 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12217 /* In a template ~id could be either a complement expression
12218 or an unqualified-id naming a destructor; if instantiating
12219 it produces an expression, it's not an id-expression or
12222 type
= finish_decltype_type (type
, id
, complain
);
12224 return cp_build_qualified_type_real (type
,
12226 | cp_type_quals (type
),
12230 case UNDERLYING_TYPE
:
12232 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12233 complain
, in_decl
);
12234 return finish_underlying_type (type
);
12237 case TYPE_ARGUMENT_PACK
:
12238 case NONTYPE_ARGUMENT_PACK
:
12240 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12242 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12246 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12248 /* For template nontype argument packs, also substitute into
12250 if (code
== NONTYPE_ARGUMENT_PACK
)
12251 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12269 /* We should use one of the expression tsubsts for these codes. */
12270 gcc_unreachable ();
12273 sorry ("use of %qs in template", get_tree_code_name (code
));
12274 return error_mark_node
;
12278 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12279 type of the expression on the left-hand side of the "." or "->"
12283 tsubst_baselink (tree baselink
, tree object_type
,
12284 tree args
, tsubst_flags_t complain
, tree in_decl
)
12287 tree qualifying_scope
;
12290 tree template_args
= 0;
12291 bool template_id_p
= false;
12292 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12294 /* A baselink indicates a function from a base class. Both the
12295 BASELINK_ACCESS_BINFO and the base class referenced may
12296 indicate bases of the template class, rather than the
12297 instantiated class. In addition, lookups that were not
12298 ambiguous before may be ambiguous now. Therefore, we perform
12299 the lookup again. */
12300 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12301 qualifying_scope
= tsubst (qualifying_scope
, args
,
12302 complain
, in_decl
);
12303 fns
= BASELINK_FUNCTIONS (baselink
);
12304 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12305 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12307 template_id_p
= true;
12308 template_args
= TREE_OPERAND (fns
, 1);
12309 fns
= TREE_OPERAND (fns
, 0);
12311 template_args
= tsubst_template_args (template_args
, args
,
12312 complain
, in_decl
);
12314 name
= DECL_NAME (get_first_fn (fns
));
12315 if (IDENTIFIER_TYPENAME_P (name
))
12316 name
= mangle_conv_op_name_for_type (optype
);
12317 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12319 return error_mark_node
;
12321 /* If lookup found a single function, mark it as used at this
12322 point. (If it lookup found multiple functions the one selected
12323 later by overload resolution will be marked as used at that
12325 if (BASELINK_P (baselink
))
12326 fns
= BASELINK_FUNCTIONS (baselink
);
12327 if (!template_id_p
&& !really_overloaded_fn (fns
))
12328 mark_used (OVL_CURRENT (fns
));
12330 /* Add back the template arguments, if present. */
12331 if (BASELINK_P (baselink
) && template_id_p
)
12332 BASELINK_FUNCTIONS (baselink
)
12333 = build_nt (TEMPLATE_ID_EXPR
,
12334 BASELINK_FUNCTIONS (baselink
),
12336 /* Update the conversion operator type. */
12337 BASELINK_OPTYPE (baselink
) = optype
;
12340 object_type
= current_class_type
;
12343 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12349 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12350 true if the qualified-id will be a postfix-expression in-and-of
12351 itself; false if more of the postfix-expression follows the
12352 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12356 tsubst_qualified_id (tree qualified_id
, tree args
,
12357 tsubst_flags_t complain
, tree in_decl
,
12358 bool done
, bool address_p
)
12364 tree template_args
;
12365 location_t loc
= UNKNOWN_LOCATION
;
12367 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12369 /* Figure out what name to look up. */
12370 name
= TREE_OPERAND (qualified_id
, 1);
12371 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12373 is_template
= true;
12374 loc
= EXPR_LOCATION (name
);
12375 template_args
= TREE_OPERAND (name
, 1);
12377 template_args
= tsubst_template_args (template_args
, args
,
12378 complain
, in_decl
);
12379 name
= TREE_OPERAND (name
, 0);
12383 is_template
= false;
12384 template_args
= NULL_TREE
;
12387 /* Substitute into the qualifying scope. When there are no ARGS, we
12388 are just trying to simplify a non-dependent expression. In that
12389 case the qualifying scope may be dependent, and, in any case,
12390 substituting will not help. */
12391 scope
= TREE_OPERAND (qualified_id
, 0);
12394 scope
= tsubst (scope
, args
, complain
, in_decl
);
12395 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12400 if (dependent_scope_p (scope
))
12403 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12404 return build_qualified_name (NULL_TREE
, scope
, expr
,
12405 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12408 if (!BASELINK_P (name
) && !DECL_P (expr
))
12410 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12412 /* A BIT_NOT_EXPR is used to represent a destructor. */
12413 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12415 error ("qualifying type %qT does not match destructor name ~%qT",
12416 scope
, TREE_OPERAND (expr
, 0));
12417 expr
= error_mark_node
;
12420 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12421 /*is_type_p=*/0, false);
12424 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12425 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12426 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12428 if (complain
& tf_error
)
12430 error ("dependent-name %qE is parsed as a non-type, but "
12431 "instantiation yields a type", qualified_id
);
12432 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12434 return error_mark_node
;
12440 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12442 /* Remember that there was a reference to this entity. */
12446 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12448 if (complain
& tf_error
)
12449 qualified_name_lookup_error (scope
,
12450 TREE_OPERAND (qualified_id
, 1),
12451 expr
, input_location
);
12452 return error_mark_node
;
12456 expr
= lookup_template_function (expr
, template_args
);
12458 if (expr
== error_mark_node
&& complain
& tf_error
)
12459 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12460 expr
, input_location
);
12461 else if (TYPE_P (scope
))
12463 expr
= (adjust_result_of_qualified_name_lookup
12464 (expr
, scope
, current_nonlambda_class_type ()));
12465 expr
= (finish_qualified_id_expr
12466 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12467 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12468 /*template_arg_p=*/false, complain
));
12471 /* Expressions do not generally have reference type. */
12472 if (TREE_CODE (expr
) != SCOPE_REF
12473 /* However, if we're about to form a pointer-to-member, we just
12474 want the referenced member referenced. */
12475 && TREE_CODE (expr
) != OFFSET_REF
)
12476 expr
= convert_from_reference (expr
);
12481 /* Like tsubst, but deals with expressions. This function just replaces
12482 template parms; to finish processing the resultant expression, use
12483 tsubst_copy_and_build or tsubst_expr. */
12486 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12488 enum tree_code code
;
12491 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12494 code
= TREE_CODE (t
);
12499 r
= retrieve_local_specialization (t
);
12501 if (r
== NULL_TREE
)
12503 /* We get here for a use of 'this' in an NSDMI. */
12504 if (DECL_NAME (t
) == this_identifier
12505 && at_function_scope_p ()
12506 && DECL_CONSTRUCTOR_P (current_function_decl
))
12507 return current_class_ptr
;
12509 /* This can happen for a parameter name used later in a function
12510 declaration (such as in a late-specified return type). Just
12511 make a dummy decl, since it's only used for its type. */
12512 gcc_assert (cp_unevaluated_operand
!= 0);
12513 r
= tsubst_decl (t
, args
, complain
);
12514 /* Give it the template pattern as its context; its true context
12515 hasn't been instantiated yet and this is good enough for
12517 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12520 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12521 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12530 if (DECL_TEMPLATE_PARM_P (t
))
12531 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12532 /* There is no need to substitute into namespace-scope
12534 if (DECL_NAMESPACE_SCOPE_P (t
))
12536 /* If ARGS is NULL, then T is known to be non-dependent. */
12537 if (args
== NULL_TREE
)
12538 return integral_constant_value (t
);
12540 /* Unfortunately, we cannot just call lookup_name here.
12543 template <int I> int f() {
12545 struct S { void g() { E e = a; } };
12548 When we instantiate f<7>::S::g(), say, lookup_name is not
12549 clever enough to find f<7>::a. */
12551 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12552 /*entering_scope=*/0);
12554 for (v
= TYPE_VALUES (enum_type
);
12556 v
= TREE_CHAIN (v
))
12557 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12558 return TREE_VALUE (v
);
12560 /* We didn't find the name. That should never happen; if
12561 name-lookup found it during preliminary parsing, we
12562 should find it again here during instantiation. */
12563 gcc_unreachable ();
12568 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12570 /* Check for a local specialization set up by
12571 tsubst_pack_expansion. */
12572 if (tree r
= retrieve_local_specialization (t
))
12574 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12575 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12579 /* When retrieving a capture pack from a generic lambda, remove the
12580 lambda call op's own template argument list from ARGS. Only the
12581 template arguments active for the closure type should be used to
12582 retrieve the pack specialization. */
12583 if (LAMBDA_FUNCTION_P (current_function_decl
)
12584 && (template_class_depth (DECL_CONTEXT (t
))
12585 != TMPL_ARGS_DEPTH (args
)))
12586 args
= strip_innermost_template_args (args
, 1);
12588 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12589 tsubst_decl put in the hash table. */
12590 return retrieve_specialization (t
, args
, 0);
12593 if (DECL_CONTEXT (t
))
12597 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12598 /*entering_scope=*/1);
12599 if (ctx
!= DECL_CONTEXT (t
))
12601 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12604 if (complain
& tf_error
)
12605 error ("using invalid field %qD", t
);
12606 return error_mark_node
;
12615 case FUNCTION_DECL
:
12616 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12617 r
= tsubst (t
, args
, complain
, in_decl
);
12618 else if (local_variable_p (t
))
12620 r
= retrieve_local_specialization (t
);
12621 if (r
== NULL_TREE
)
12623 if (DECL_ANON_UNION_VAR_P (t
))
12625 /* Just use name lookup to find a member alias for an
12626 anonymous union, but then add it to the hash table. */
12627 r
= lookup_name (DECL_NAME (t
));
12628 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12629 register_local_specialization (r
, t
);
12633 /* This can happen for a variable used in a late-specified
12634 return type of a local lambda. Just make a dummy decl
12635 since it's only used for its type. */
12636 if (cp_unevaluated_operand
)
12637 return tsubst_decl (t
, args
, complain
);
12638 gcc_assert (errorcount
|| sorrycount
);
12639 return error_mark_node
;
12648 case NAMESPACE_DECL
:
12652 /* An OVERLOAD will always be a non-dependent overload set; an
12653 overload set from function scope will just be represented with an
12654 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12655 gcc_assert (!uses_template_parms (t
));
12659 return tsubst_baselink (t
, current_nonlambda_class_type (),
12660 args
, complain
, in_decl
);
12662 case TEMPLATE_DECL
:
12663 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12664 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12665 args
, complain
, in_decl
);
12666 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12667 return tsubst (t
, args
, complain
, in_decl
);
12668 else if (DECL_CLASS_SCOPE_P (t
)
12669 && uses_template_parms (DECL_CONTEXT (t
)))
12671 /* Template template argument like the following example need
12674 template <template <class> class TT> struct C {};
12675 template <class T> struct D {
12676 template <class U> struct E {};
12681 We are processing the template argument `E' in #1 for
12682 the template instantiation #2. Originally, `E' is a
12683 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12684 have to substitute this with one having context `D<int>'. */
12686 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12687 return lookup_field (context
, DECL_NAME(t
), 0, false);
12690 /* Ordinary template template argument. */
12694 case REINTERPRET_CAST_EXPR
:
12695 case CONST_CAST_EXPR
:
12696 case STATIC_CAST_EXPR
:
12697 case DYNAMIC_CAST_EXPR
:
12698 case IMPLICIT_CONV_EXPR
:
12702 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12703 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12706 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12709 tree expanded
, op
= TREE_OPERAND (t
, 0);
12712 if (SIZEOF_EXPR_TYPE_P (t
))
12713 op
= TREE_TYPE (op
);
12715 ++cp_unevaluated_operand
;
12716 ++c_inhibit_evaluation_warnings
;
12717 /* We only want to compute the number of arguments. */
12718 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12719 --cp_unevaluated_operand
;
12720 --c_inhibit_evaluation_warnings
;
12722 if (TREE_CODE (expanded
) == TREE_VEC
)
12723 len
= TREE_VEC_LENGTH (expanded
);
12725 if (expanded
== error_mark_node
)
12726 return error_mark_node
;
12727 else if (PACK_EXPANSION_P (expanded
)
12728 || (TREE_CODE (expanded
) == TREE_VEC
12730 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12732 if (TREE_CODE (expanded
) == TREE_VEC
)
12733 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12735 if (TYPE_P (expanded
))
12736 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12737 complain
& tf_error
);
12739 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12740 complain
& tf_error
);
12743 return build_int_cst (size_type_node
, len
);
12745 if (SIZEOF_EXPR_TYPE_P (t
))
12747 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12748 args
, complain
, in_decl
);
12749 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12750 r
= build1 (SIZEOF_EXPR
,
12751 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12752 SIZEOF_EXPR_TYPE_P (r
) = 1;
12759 case TRUTH_NOT_EXPR
:
12762 case UNARY_PLUS_EXPR
: /* Unary + */
12764 case AT_ENCODE_EXPR
:
12768 case REALPART_EXPR
:
12769 case IMAGPART_EXPR
:
12772 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12773 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12775 case COMPONENT_REF
:
12780 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12781 name
= TREE_OPERAND (t
, 1);
12782 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12784 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12785 complain
, in_decl
);
12786 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12788 else if (TREE_CODE (name
) == SCOPE_REF
12789 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12791 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12792 complain
, in_decl
);
12793 name
= TREE_OPERAND (name
, 1);
12794 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12795 complain
, in_decl
);
12796 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12797 name
= build_qualified_name (/*type=*/NULL_TREE
,
12799 /*template_p=*/false);
12801 else if (BASELINK_P (name
))
12802 name
= tsubst_baselink (name
,
12803 non_reference (TREE_TYPE (object
)),
12807 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12808 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12814 case TRUNC_DIV_EXPR
:
12815 case CEIL_DIV_EXPR
:
12816 case FLOOR_DIV_EXPR
:
12817 case ROUND_DIV_EXPR
:
12818 case EXACT_DIV_EXPR
:
12822 case TRUNC_MOD_EXPR
:
12823 case FLOOR_MOD_EXPR
:
12824 case TRUTH_ANDIF_EXPR
:
12825 case TRUTH_ORIF_EXPR
:
12826 case TRUTH_AND_EXPR
:
12827 case TRUTH_OR_EXPR
:
12840 case COMPOUND_EXPR
:
12843 case PREDECREMENT_EXPR
:
12844 case PREINCREMENT_EXPR
:
12845 case POSTDECREMENT_EXPR
:
12846 case POSTINCREMENT_EXPR
:
12848 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12849 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12852 return build_qualified_name (/*type=*/NULL_TREE
,
12853 tsubst_copy (TREE_OPERAND (t
, 0),
12854 args
, complain
, in_decl
),
12855 tsubst_copy (TREE_OPERAND (t
, 1),
12856 args
, complain
, in_decl
),
12857 QUALIFIED_NAME_IS_TEMPLATE (t
));
12862 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12863 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12864 NULL_TREE
, NULL_TREE
);
12868 int n
= VL_EXP_OPERAND_LENGTH (t
);
12869 tree result
= build_vl_exp (CALL_EXPR
, n
);
12871 for (i
= 0; i
< n
; i
++)
12872 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12873 complain
, in_decl
);
12879 case PSEUDO_DTOR_EXPR
:
12880 case VEC_PERM_EXPR
:
12883 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12884 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12885 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12886 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12893 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12894 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12895 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12896 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12903 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12904 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12905 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12906 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12910 case TEMPLATE_ID_EXPR
:
12912 /* Substituted template arguments */
12913 tree fn
= TREE_OPERAND (t
, 0);
12914 tree targs
= TREE_OPERAND (t
, 1);
12916 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12918 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12920 return lookup_template_function (fn
, targs
);
12925 tree purpose
, value
, chain
;
12927 if (t
== void_list_node
)
12930 purpose
= TREE_PURPOSE (t
);
12932 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12933 value
= TREE_VALUE (t
);
12935 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12936 chain
= TREE_CHAIN (t
);
12937 if (chain
&& chain
!= void_type_node
)
12938 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12939 if (purpose
== TREE_PURPOSE (t
)
12940 && value
== TREE_VALUE (t
)
12941 && chain
== TREE_CHAIN (t
))
12943 return tree_cons (purpose
, value
, chain
);
12948 case ENUMERAL_TYPE
:
12950 case TEMPLATE_TYPE_PARM
:
12951 case TEMPLATE_TEMPLATE_PARM
:
12952 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12953 case TEMPLATE_PARM_INDEX
:
12955 case REFERENCE_TYPE
:
12957 case FUNCTION_TYPE
:
12960 case TYPENAME_TYPE
:
12961 case UNBOUND_CLASS_TEMPLATE
:
12963 case DECLTYPE_TYPE
:
12965 return tsubst (t
, args
, complain
, in_decl
);
12969 /* Fall through. */
12970 case IDENTIFIER_NODE
:
12971 if (IDENTIFIER_TYPENAME_P (t
))
12973 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12974 return mangle_conv_op_name_for_type (new_type
);
12980 /* This is handled by tsubst_copy_and_build. */
12981 gcc_unreachable ();
12984 return build_x_va_arg (EXPR_LOCATION (t
),
12985 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12987 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12989 case CLEANUP_POINT_EXPR
:
12990 /* We shouldn't have built any of these during initial template
12991 generation. Instead, they should be built during instantiation
12992 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12993 gcc_unreachable ();
12997 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12998 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12999 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
13000 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
13001 mark_used (TREE_OPERAND (r
, 1));
13004 case EXPR_PACK_EXPANSION
:
13005 error ("invalid use of pack expansion expression");
13006 return error_mark_node
;
13008 case NONTYPE_ARGUMENT_PACK
:
13009 error ("use %<...%> to expand argument pack");
13010 return error_mark_node
;
13017 /* Instantiate any typedefs in the type. */
13018 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13019 r
= fold_convert (type
, t
);
13020 gcc_assert (TREE_CODE (r
) == code
);
13025 /* These can sometimes show up in a partial instantiation, but never
13026 involve template parms. */
13027 gcc_assert (!uses_template_parms (t
));
13031 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13032 gcc_checking_assert (false);
13037 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13040 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
13041 tree args
, tsubst_flags_t complain
, tree in_decl
)
13043 tree new_clauses
= NULL
, nc
, oc
;
13045 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
13047 nc
= copy_node (oc
);
13048 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
13051 switch (OMP_CLAUSE_CODE (nc
))
13053 case OMP_CLAUSE_LASTPRIVATE
:
13054 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
13056 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
13057 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
13058 in_decl
, /*integral_constant_expression_p=*/false);
13059 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
13060 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
13063 case OMP_CLAUSE_PRIVATE
:
13064 case OMP_CLAUSE_SHARED
:
13065 case OMP_CLAUSE_FIRSTPRIVATE
:
13066 case OMP_CLAUSE_COPYIN
:
13067 case OMP_CLAUSE_COPYPRIVATE
:
13068 case OMP_CLAUSE_IF
:
13069 case OMP_CLAUSE_NUM_THREADS
:
13070 case OMP_CLAUSE_SCHEDULE
:
13071 case OMP_CLAUSE_COLLAPSE
:
13072 case OMP_CLAUSE_FINAL
:
13073 case OMP_CLAUSE_DEPEND
:
13074 case OMP_CLAUSE_FROM
:
13075 case OMP_CLAUSE_TO
:
13076 case OMP_CLAUSE_UNIFORM
:
13077 case OMP_CLAUSE_MAP
:
13078 case OMP_CLAUSE_DEVICE
:
13079 case OMP_CLAUSE_DIST_SCHEDULE
:
13080 case OMP_CLAUSE_NUM_TEAMS
:
13081 case OMP_CLAUSE_THREAD_LIMIT
:
13082 case OMP_CLAUSE_SAFELEN
:
13083 case OMP_CLAUSE_SIMDLEN
:
13084 OMP_CLAUSE_OPERAND (nc
, 0)
13085 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13086 in_decl
, /*integral_constant_expression_p=*/false);
13088 case OMP_CLAUSE_REDUCTION
:
13089 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
13091 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
13092 if (TREE_CODE (placeholder
) == SCOPE_REF
)
13094 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
13095 complain
, in_decl
);
13096 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
13097 = build_qualified_name (NULL_TREE
, scope
,
13098 TREE_OPERAND (placeholder
, 1),
13102 gcc_assert (identifier_p (placeholder
));
13104 OMP_CLAUSE_OPERAND (nc
, 0)
13105 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13106 in_decl
, /*integral_constant_expression_p=*/false);
13108 case OMP_CLAUSE_LINEAR
:
13109 case OMP_CLAUSE_ALIGNED
:
13110 OMP_CLAUSE_OPERAND (nc
, 0)
13111 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13112 in_decl
, /*integral_constant_expression_p=*/false);
13113 OMP_CLAUSE_OPERAND (nc
, 1)
13114 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
13115 in_decl
, /*integral_constant_expression_p=*/false);
13118 case OMP_CLAUSE_NOWAIT
:
13119 case OMP_CLAUSE_ORDERED
:
13120 case OMP_CLAUSE_DEFAULT
:
13121 case OMP_CLAUSE_UNTIED
:
13122 case OMP_CLAUSE_MERGEABLE
:
13123 case OMP_CLAUSE_INBRANCH
:
13124 case OMP_CLAUSE_NOTINBRANCH
:
13125 case OMP_CLAUSE_PROC_BIND
:
13126 case OMP_CLAUSE_FOR
:
13127 case OMP_CLAUSE_PARALLEL
:
13128 case OMP_CLAUSE_SECTIONS
:
13129 case OMP_CLAUSE_TASKGROUP
:
13132 gcc_unreachable ();
13136 new_clauses
= nreverse (new_clauses
);
13138 new_clauses
= finish_omp_clauses (new_clauses
);
13139 return new_clauses
;
13142 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13145 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
13148 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13150 tree purpose
, value
, chain
;
13155 if (TREE_CODE (t
) != TREE_LIST
)
13156 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13157 /*function_p=*/false,
13158 /*integral_constant_expression_p=*/false);
13160 if (t
== void_list_node
)
13163 purpose
= TREE_PURPOSE (t
);
13165 purpose
= RECUR (purpose
);
13166 value
= TREE_VALUE (t
);
13169 if (TREE_CODE (value
) != LABEL_DECL
)
13170 value
= RECUR (value
);
13173 value
= lookup_label (DECL_NAME (value
));
13174 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13175 TREE_USED (value
) = 1;
13178 chain
= TREE_CHAIN (t
);
13179 if (chain
&& chain
!= void_type_node
)
13180 chain
= RECUR (chain
);
13181 return tree_cons (purpose
, value
, chain
);
13185 /* Substitute one OMP_FOR iterator. */
13188 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13189 tree condv
, tree incrv
, tree
*clauses
,
13190 tree args
, tsubst_flags_t complain
, tree in_decl
,
13191 bool integral_constant_expression_p
)
13193 #define RECUR(NODE) \
13194 tsubst_expr ((NODE), args, complain, in_decl, \
13195 integral_constant_expression_p)
13196 tree decl
, init
, cond
, incr
;
13198 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13199 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13200 decl
= TREE_OPERAND (init
, 0);
13201 init
= TREE_OPERAND (init
, 1);
13202 tree decl_expr
= NULL_TREE
;
13203 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
13205 /* We need to jump through some hoops to handle declarations in the
13206 for-init-statement, since we might need to handle auto deduction,
13207 but we need to keep control of initialization. */
13209 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
13210 decl
= tsubst_decl (decl
, args
, complain
);
13213 decl
= RECUR (decl
);
13214 init
= RECUR (init
);
13216 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13217 if (auto_node
&& init
)
13219 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
13221 gcc_assert (!type_dependent_expression_p (decl
));
13223 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13227 /* Declare the variable, but don't let that initialize it. */
13228 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13229 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
13231 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
13234 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13235 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13236 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13237 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13238 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13239 RECUR (TREE_OPERAND (incr
, 1)),
13242 incr
= RECUR (incr
);
13243 TREE_VEC_ELT (declv
, i
) = decl
;
13244 TREE_VEC_ELT (initv
, i
) = init
;
13245 TREE_VEC_ELT (condv
, i
) = cond
;
13246 TREE_VEC_ELT (incrv
, i
) = incr
;
13252 /* Declare and initialize the variable. */
13259 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13261 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13262 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13263 && OMP_CLAUSE_DECL (c
) == decl
)
13265 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13266 && OMP_CLAUSE_DECL (c
) == decl
)
13267 error ("iteration variable %qD should not be firstprivate", decl
);
13268 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13269 && OMP_CLAUSE_DECL (c
) == decl
)
13270 error ("iteration variable %qD should not be reduction", decl
);
13274 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13275 OMP_CLAUSE_DECL (c
) = decl
;
13276 c
= finish_omp_clauses (c
);
13279 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13284 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13285 if (COMPARISON_CLASS_P (cond
))
13286 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13287 RECUR (TREE_OPERAND (cond
, 0)),
13288 RECUR (TREE_OPERAND (cond
, 1)));
13290 cond
= RECUR (cond
);
13291 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13292 switch (TREE_CODE (incr
))
13294 case PREINCREMENT_EXPR
:
13295 case PREDECREMENT_EXPR
:
13296 case POSTINCREMENT_EXPR
:
13297 case POSTDECREMENT_EXPR
:
13298 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13299 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13302 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13303 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13305 tree rhs
= TREE_OPERAND (incr
, 1);
13306 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13307 RECUR (TREE_OPERAND (incr
, 0)),
13308 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13309 RECUR (TREE_OPERAND (rhs
, 0)),
13310 RECUR (TREE_OPERAND (rhs
, 1))));
13313 incr
= RECUR (incr
);
13316 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13317 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13319 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13320 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13321 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13322 TREE_TYPE (decl
), lhs
,
13323 RECUR (TREE_OPERAND (incr
, 2))));
13325 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13326 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13327 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13329 tree rhs
= TREE_OPERAND (incr
, 2);
13330 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13331 RECUR (TREE_OPERAND (incr
, 0)),
13332 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13333 RECUR (TREE_OPERAND (rhs
, 0)),
13334 RECUR (TREE_OPERAND (rhs
, 1))));
13337 incr
= RECUR (incr
);
13340 incr
= RECUR (incr
);
13344 TREE_VEC_ELT (declv
, i
) = decl
;
13345 TREE_VEC_ELT (initv
, i
) = init
;
13346 TREE_VEC_ELT (condv
, i
) = cond
;
13347 TREE_VEC_ELT (incrv
, i
) = incr
;
13351 /* Like tsubst_copy for expressions, etc. but also does semantic
13355 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13356 bool integral_constant_expression_p
)
13358 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13359 #define RECUR(NODE) \
13360 tsubst_expr ((NODE), args, complain, in_decl, \
13361 integral_constant_expression_p)
13367 if (t
== NULL_TREE
|| t
== error_mark_node
)
13370 loc
= input_location
;
13371 if (EXPR_HAS_LOCATION (t
))
13372 input_location
= EXPR_LOCATION (t
);
13373 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13374 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13376 switch (TREE_CODE (t
))
13378 case STATEMENT_LIST
:
13380 tree_stmt_iterator i
;
13381 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13382 RECUR (tsi_stmt (i
));
13386 case CTOR_INITIALIZER
:
13387 finish_mem_initializers (tsubst_initializer_list
13388 (TREE_OPERAND (t
, 0), args
));
13392 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13396 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13397 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13398 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13400 finish_expr_stmt (tmp
);
13404 do_using_directive (USING_STMT_NAMESPACE (t
));
13409 tree decl
, pattern_decl
;
13412 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13413 if (TREE_CODE (decl
) == LABEL_DECL
)
13414 finish_label_decl (DECL_NAME (decl
));
13415 else if (TREE_CODE (decl
) == USING_DECL
)
13417 tree scope
= USING_DECL_SCOPE (decl
);
13418 tree name
= DECL_NAME (decl
);
13421 scope
= tsubst (scope
, args
, complain
, in_decl
);
13422 decl
= lookup_qualified_name (scope
, name
,
13423 /*is_type_p=*/false,
13424 /*complain=*/false);
13425 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13426 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13428 do_local_using_decl (decl
, scope
, name
);
13430 else if (DECL_PACK_P (decl
))
13432 /* Don't build up decls for a variadic capture proxy, we'll
13433 instantiate the elements directly as needed. */
13438 init
= DECL_INITIAL (decl
);
13439 decl
= tsubst (decl
, args
, complain
, in_decl
);
13440 if (decl
!= error_mark_node
)
13442 /* By marking the declaration as instantiated, we avoid
13443 trying to instantiate it. Since instantiate_decl can't
13444 handle local variables, and since we've already done
13445 all that needs to be done, that's the right thing to
13448 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13450 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13451 /* Anonymous aggregates are a special case. */
13452 finish_anon_union (decl
);
13453 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13455 DECL_CONTEXT (decl
) = current_function_decl
;
13456 if (DECL_NAME (decl
) == this_identifier
)
13458 tree lam
= DECL_CONTEXT (current_function_decl
);
13459 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13460 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13462 insert_capture_proxy (decl
);
13464 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13465 /* We already did a pushtag. */;
13466 else if (TREE_CODE (decl
) == FUNCTION_DECL
13467 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13468 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13470 DECL_CONTEXT (decl
) = NULL_TREE
;
13472 DECL_CONTEXT (decl
) = current_function_decl
;
13473 cp_check_omp_declare_reduction (decl
);
13477 int const_init
= false;
13478 maybe_push_decl (decl
);
13480 && DECL_PRETTY_FUNCTION_P (decl
))
13482 /* For __PRETTY_FUNCTION__ we have to adjust the
13484 const char *const name
13485 = cxx_printable_name (current_function_decl
, 2);
13486 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13490 tree t
= RECUR (init
);
13494 /* If we had an initializer but it
13495 instantiated to nothing,
13496 value-initialize the object. This will
13497 only occur when the initializer was a
13498 pack expansion where the parameter packs
13499 used in that expansion were of length
13501 init
= build_value_init (TREE_TYPE (decl
),
13503 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13504 init
= get_target_expr_sfinae (init
, complain
);
13511 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13513 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13522 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13523 RECUR (FOR_INIT_STMT (t
));
13524 finish_for_init_stmt (stmt
);
13525 tmp
= RECUR (FOR_COND (t
));
13526 finish_for_cond (tmp
, stmt
, false);
13527 tmp
= RECUR (FOR_EXPR (t
));
13528 finish_for_expr (tmp
, stmt
);
13529 RECUR (FOR_BODY (t
));
13530 finish_for_stmt (stmt
);
13533 case RANGE_FOR_STMT
:
13536 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13537 decl
= RANGE_FOR_DECL (t
);
13538 decl
= tsubst (decl
, args
, complain
, in_decl
);
13539 maybe_push_decl (decl
);
13540 expr
= RECUR (RANGE_FOR_EXPR (t
));
13541 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13542 RECUR (RANGE_FOR_BODY (t
));
13543 finish_for_stmt (stmt
);
13548 stmt
= begin_while_stmt ();
13549 tmp
= RECUR (WHILE_COND (t
));
13550 finish_while_stmt_cond (tmp
, stmt
, false);
13551 RECUR (WHILE_BODY (t
));
13552 finish_while_stmt (stmt
);
13556 stmt
= begin_do_stmt ();
13557 RECUR (DO_BODY (t
));
13558 finish_do_body (stmt
);
13559 tmp
= RECUR (DO_COND (t
));
13560 finish_do_stmt (tmp
, stmt
, false);
13564 stmt
= begin_if_stmt ();
13565 tmp
= RECUR (IF_COND (t
));
13566 finish_if_stmt_cond (tmp
, stmt
);
13567 RECUR (THEN_CLAUSE (t
));
13568 finish_then_clause (stmt
);
13570 if (ELSE_CLAUSE (t
))
13572 begin_else_clause (stmt
);
13573 RECUR (ELSE_CLAUSE (t
));
13574 finish_else_clause (stmt
);
13577 finish_if_stmt (stmt
);
13581 if (BIND_EXPR_BODY_BLOCK (t
))
13582 stmt
= begin_function_body ();
13584 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13585 ? BCS_TRY_BLOCK
: 0);
13587 RECUR (BIND_EXPR_BODY (t
));
13589 if (BIND_EXPR_BODY_BLOCK (t
))
13590 finish_function_body (stmt
);
13592 finish_compound_stmt (stmt
);
13596 finish_break_stmt ();
13599 case CONTINUE_STMT
:
13600 finish_continue_stmt ();
13604 stmt
= begin_switch_stmt ();
13605 tmp
= RECUR (SWITCH_STMT_COND (t
));
13606 finish_switch_cond (tmp
, stmt
);
13607 RECUR (SWITCH_STMT_BODY (t
));
13608 finish_switch_stmt (stmt
);
13611 case CASE_LABEL_EXPR
:
13612 finish_case_label (EXPR_LOCATION (t
),
13613 RECUR (CASE_LOW (t
)),
13614 RECUR (CASE_HIGH (t
)));
13619 tree decl
= LABEL_EXPR_LABEL (t
);
13622 label
= finish_label_stmt (DECL_NAME (decl
));
13623 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13624 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13629 tmp
= GOTO_DESTINATION (t
);
13630 if (TREE_CODE (tmp
) != LABEL_DECL
)
13631 /* Computed goto's must be tsubst'd into. On the other hand,
13632 non-computed gotos must not be; the identifier in question
13633 will have no binding. */
13636 tmp
= DECL_NAME (tmp
);
13637 finish_goto_stmt (tmp
);
13641 tmp
= finish_asm_stmt
13642 (ASM_VOLATILE_P (t
),
13643 RECUR (ASM_STRING (t
)),
13644 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13645 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13646 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13647 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13649 tree asm_expr
= tmp
;
13650 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13651 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13652 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13659 stmt
= begin_try_block ();
13660 RECUR (TRY_STMTS (t
));
13661 finish_cleanup_try_block (stmt
);
13662 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13666 tree compound_stmt
= NULL_TREE
;
13668 if (FN_TRY_BLOCK_P (t
))
13669 stmt
= begin_function_try_block (&compound_stmt
);
13671 stmt
= begin_try_block ();
13673 RECUR (TRY_STMTS (t
));
13675 if (FN_TRY_BLOCK_P (t
))
13676 finish_function_try_block (stmt
);
13678 finish_try_block (stmt
);
13680 RECUR (TRY_HANDLERS (t
));
13681 if (FN_TRY_BLOCK_P (t
))
13682 finish_function_handler_sequence (stmt
, compound_stmt
);
13684 finish_handler_sequence (stmt
);
13690 tree decl
= HANDLER_PARMS (t
);
13694 decl
= tsubst (decl
, args
, complain
, in_decl
);
13695 /* Prevent instantiate_decl from trying to instantiate
13696 this variable. We've already done all that needs to be
13698 if (decl
!= error_mark_node
)
13699 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13701 stmt
= begin_handler ();
13702 finish_handler_parms (decl
, stmt
);
13703 RECUR (HANDLER_BODY (t
));
13704 finish_handler (stmt
);
13709 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13710 if (CLASS_TYPE_P (tmp
))
13712 /* Local classes are not independent templates; they are
13713 instantiated along with their containing function. And this
13714 way we don't have to deal with pushing out of one local class
13715 to instantiate a member of another local class. */
13717 /* Closures are handled by the LAMBDA_EXPR. */
13718 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13719 complete_type (tmp
);
13720 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13721 if (!DECL_ARTIFICIAL (fn
))
13722 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13726 case STATIC_ASSERT
:
13730 ++c_inhibit_evaluation_warnings
;
13732 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13735 /*integral_constant_expression_p=*/true);
13736 --c_inhibit_evaluation_warnings
;
13738 finish_static_assert (condition
,
13739 STATIC_ASSERT_MESSAGE (t
),
13740 STATIC_ASSERT_SOURCE_LOCATION (t
),
13741 /*member_p=*/false);
13746 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13747 args
, complain
, in_decl
);
13748 stmt
= begin_omp_parallel ();
13749 RECUR (OMP_PARALLEL_BODY (t
));
13750 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13751 = OMP_PARALLEL_COMBINED (t
);
13755 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13756 args
, complain
, in_decl
);
13757 stmt
= begin_omp_task ();
13758 RECUR (OMP_TASK_BODY (t
));
13759 finish_omp_task (tmp
, stmt
);
13765 case OMP_DISTRIBUTE
:
13767 tree clauses
, body
, pre_body
;
13768 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13769 tree incrv
= NULL_TREE
;
13772 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13773 args
, complain
, in_decl
);
13774 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13776 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13777 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13778 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13779 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13782 stmt
= begin_omp_structured_block ();
13784 pre_body
= push_stmt_list ();
13785 RECUR (OMP_FOR_PRE_BODY (t
));
13786 pre_body
= pop_stmt_list (pre_body
);
13788 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13789 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13790 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13791 &clauses
, args
, complain
, in_decl
,
13792 integral_constant_expression_p
);
13794 body
= push_stmt_list ();
13795 RECUR (OMP_FOR_BODY (t
));
13796 body
= pop_stmt_list (body
);
13798 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13799 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13800 condv
, incrv
, body
, pre_body
, clauses
);
13803 t
= make_node (TREE_CODE (t
));
13804 TREE_TYPE (t
) = void_type_node
;
13805 OMP_FOR_BODY (t
) = body
;
13806 OMP_FOR_PRE_BODY (t
) = pre_body
;
13807 OMP_FOR_CLAUSES (t
) = clauses
;
13808 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13812 add_stmt (finish_omp_structured_block (stmt
));
13819 case OMP_TARGET_DATA
:
13821 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13822 args
, complain
, in_decl
);
13823 stmt
= push_stmt_list ();
13824 RECUR (OMP_BODY (t
));
13825 stmt
= pop_stmt_list (stmt
);
13828 OMP_BODY (t
) = stmt
;
13829 OMP_CLAUSES (t
) = tmp
;
13833 case OMP_TARGET_UPDATE
:
13834 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13835 args
, complain
, in_decl
);
13837 OMP_CLAUSES (t
) = tmp
;
13844 case OMP_TASKGROUP
:
13846 stmt
= push_stmt_list ();
13847 RECUR (OMP_BODY (t
));
13848 stmt
= pop_stmt_list (stmt
);
13851 OMP_BODY (t
) = stmt
;
13856 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13857 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13859 tree op1
= TREE_OPERAND (t
, 1);
13860 tree rhs1
= NULL_TREE
;
13862 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13864 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13865 op1
= TREE_OPERAND (op1
, 1);
13867 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13868 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13869 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13870 NULL_TREE
, NULL_TREE
, rhs1
,
13871 OMP_ATOMIC_SEQ_CST (t
));
13875 tree op1
= TREE_OPERAND (t
, 1);
13876 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13877 tree rhs1
= NULL_TREE
;
13878 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13879 enum tree_code opcode
= NOP_EXPR
;
13880 if (code
== OMP_ATOMIC_READ
)
13882 v
= RECUR (TREE_OPERAND (op1
, 0));
13883 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13885 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13886 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13888 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13889 v
= RECUR (TREE_OPERAND (op1
, 0));
13890 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13891 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13893 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13894 op11
= TREE_OPERAND (op11
, 1);
13896 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13897 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13898 opcode
= TREE_CODE (op11
);
13899 if (opcode
== MODIFY_EXPR
)
13905 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13906 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13908 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13909 OMP_ATOMIC_SEQ_CST (t
));
13913 case TRANSACTION_EXPR
:
13916 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13917 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13919 if (TRANSACTION_EXPR_IS_STMT (t
))
13921 tree body
= TRANSACTION_EXPR_BODY (t
);
13922 tree noex
= NULL_TREE
;
13923 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13925 noex
= MUST_NOT_THROW_COND (body
);
13926 if (noex
== NULL_TREE
)
13927 noex
= boolean_true_node
;
13928 body
= TREE_OPERAND (body
, 0);
13930 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13932 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13936 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13937 RECUR (TRANSACTION_EXPR_BODY (t
)),
13944 case MUST_NOT_THROW_EXPR
:
13945 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13946 RECUR (MUST_NOT_THROW_COND (t
))));
13948 case EXPR_PACK_EXPANSION
:
13949 error ("invalid use of pack expansion expression");
13950 RETURN (error_mark_node
);
13952 case NONTYPE_ARGUMENT_PACK
:
13953 error ("use %<...%> to expand argument pack");
13954 RETURN (error_mark_node
);
13956 case CILK_SPAWN_STMT
:
13957 cfun
->calls_cilk_spawn
= 1;
13958 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
13960 case CILK_SYNC_STMT
:
13961 RETURN (build_cilk_sync ());
13963 case COMPOUND_EXPR
:
13964 tmp
= RECUR (TREE_OPERAND (t
, 0));
13965 if (tmp
== NULL_TREE
)
13966 /* If the first operand was a statement, we're done with it. */
13967 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13968 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13969 RECUR (TREE_OPERAND (t
, 1)),
13972 case ANNOTATE_EXPR
:
13973 tmp
= RECUR (TREE_OPERAND (t
, 0));
13974 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
13975 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
13978 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13980 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13981 /*function_p=*/false,
13982 integral_constant_expression_p
));
13985 RETURN (NULL_TREE
);
13987 input_location
= loc
;
13993 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13994 function. For description of the body see comment above
13995 cp_parser_omp_declare_reduction_exprs. */
13998 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14000 if (t
== NULL_TREE
|| t
== error_mark_node
)
14003 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
14005 tree_stmt_iterator tsi
;
14008 memset (stmts
, 0, sizeof stmts
);
14009 for (i
= 0, tsi
= tsi_start (t
);
14010 i
< 7 && !tsi_end_p (tsi
);
14011 i
++, tsi_next (&tsi
))
14012 stmts
[i
] = tsi_stmt (tsi
);
14013 gcc_assert (tsi_end_p (tsi
));
14017 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
14018 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
14019 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
14020 args
, complain
, in_decl
);
14021 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
14022 args
, complain
, in_decl
);
14023 DECL_CONTEXT (omp_out
) = current_function_decl
;
14024 DECL_CONTEXT (omp_in
) = current_function_decl
;
14025 keep_next_level (true);
14026 tree block
= begin_omp_structured_block ();
14027 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
14028 block
= finish_omp_structured_block (block
);
14029 block
= maybe_cleanup_point_expr_void (block
);
14030 add_decl_expr (omp_out
);
14031 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
14032 TREE_NO_WARNING (omp_out
) = 1;
14033 add_decl_expr (omp_in
);
14034 finish_expr_stmt (block
);
14038 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
14039 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
14040 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
14041 args
, complain
, in_decl
);
14042 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
14043 args
, complain
, in_decl
);
14044 DECL_CONTEXT (omp_priv
) = current_function_decl
;
14045 DECL_CONTEXT (omp_orig
) = current_function_decl
;
14046 keep_next_level (true);
14047 tree block
= begin_omp_structured_block ();
14048 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
14049 block
= finish_omp_structured_block (block
);
14050 block
= maybe_cleanup_point_expr_void (block
);
14051 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
14052 add_decl_expr (omp_priv
);
14053 add_decl_expr (omp_orig
);
14054 finish_expr_stmt (block
);
14056 add_decl_expr (omp_orig
);
14060 /* T is a postfix-expression that is not being used in a function
14061 call. Return the substituted version of T. */
14064 tsubst_non_call_postfix_expression (tree t
, tree args
,
14065 tsubst_flags_t complain
,
14068 if (TREE_CODE (t
) == SCOPE_REF
)
14069 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
14070 /*done=*/false, /*address_p=*/false);
14072 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14073 /*function_p=*/false,
14074 /*integral_constant_expression_p=*/false);
14079 /* Sentinel to disable certain warnings during template substitution. */
14081 struct warning_sentinel
{
14084 warning_sentinel(int& flag
, bool suppress
=true)
14085 : flag(flag
), val(flag
) { if (suppress
) flag
= 0; }
14086 ~warning_sentinel() { flag
= val
; }
14089 /* Like tsubst but deals with expressions and performs semantic
14090 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14093 tsubst_copy_and_build (tree t
,
14095 tsubst_flags_t complain
,
14098 bool integral_constant_expression_p
)
14100 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14101 #define RECUR(NODE) \
14102 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14103 /*function_p=*/false, \
14104 integral_constant_expression_p)
14109 if (t
== NULL_TREE
|| t
== error_mark_node
)
14112 loc
= input_location
;
14113 if (EXPR_HAS_LOCATION (t
))
14114 input_location
= EXPR_LOCATION (t
);
14116 /* N3276 decltype magic only applies to calls at the top level or on the
14117 right side of a comma. */
14118 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
14119 complain
&= ~tf_decltype
;
14121 switch (TREE_CODE (t
))
14125 /* Fall through. */
14126 case IDENTIFIER_NODE
:
14130 bool non_integral_constant_expression_p
;
14131 const char *error_msg
;
14133 if (IDENTIFIER_TYPENAME_P (t
))
14135 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14136 t
= mangle_conv_op_name_for_type (new_type
);
14139 /* Look up the name. */
14140 decl
= lookup_name (t
);
14142 /* By convention, expressions use ERROR_MARK_NODE to indicate
14143 failure, not NULL_TREE. */
14144 if (decl
== NULL_TREE
)
14145 decl
= error_mark_node
;
14147 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
14149 integral_constant_expression_p
,
14150 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
14151 &non_integral_constant_expression_p
,
14152 /*template_p=*/false,
14154 /*address_p=*/false,
14155 /*template_arg_p=*/false,
14160 if (!function_p
&& identifier_p (decl
))
14162 if (complain
& tf_error
)
14163 unqualified_name_lookup_error (decl
);
14164 decl
= error_mark_node
;
14169 case TEMPLATE_ID_EXPR
:
14172 tree templ
= RECUR (TREE_OPERAND (t
, 0));
14173 tree targs
= TREE_OPERAND (t
, 1);
14176 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14178 if (TREE_CODE (templ
) == COMPONENT_REF
)
14180 object
= TREE_OPERAND (templ
, 0);
14181 templ
= TREE_OPERAND (templ
, 1);
14184 object
= NULL_TREE
;
14185 templ
= lookup_template_function (templ
, targs
);
14188 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
14189 object
, templ
, NULL_TREE
));
14191 RETURN (baselink_for_fns (templ
));
14196 tree r
= RECUR (TREE_OPERAND (t
, 0));
14198 if (REFERENCE_REF_P (t
))
14200 /* A type conversion to reference type will be enclosed in
14201 such an indirect ref, but the substitution of the cast
14202 will have also added such an indirect ref. */
14203 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14204 r
= convert_from_reference (r
);
14207 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14208 complain
|decltype_flag
);
14214 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14215 RECUR (TREE_OPERAND (t
, 0))));
14217 case IMPLICIT_CONV_EXPR
:
14219 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14220 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14221 int flags
= LOOKUP_IMPLICIT
;
14222 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14223 flags
= LOOKUP_NORMAL
;
14224 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14231 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14232 RECUR (TREE_OPERAND (t
, 0))));
14235 case REINTERPRET_CAST_EXPR
:
14236 case CONST_CAST_EXPR
:
14237 case DYNAMIC_CAST_EXPR
:
14238 case STATIC_CAST_EXPR
:
14241 tree op
, r
= NULL_TREE
;
14243 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14244 if (integral_constant_expression_p
14245 && !cast_valid_in_integral_constant_expression_p (type
))
14247 if (complain
& tf_error
)
14248 error ("a cast to a type other than an integral or "
14249 "enumeration type cannot appear in a constant-expression");
14250 RETURN (error_mark_node
);
14253 op
= RECUR (TREE_OPERAND (t
, 0));
14255 warning_sentinel
s(warn_useless_cast
);
14256 switch (TREE_CODE (t
))
14259 r
= build_functional_cast (type
, op
, complain
);
14261 case REINTERPRET_CAST_EXPR
:
14262 r
= build_reinterpret_cast (type
, op
, complain
);
14264 case CONST_CAST_EXPR
:
14265 r
= build_const_cast (type
, op
, complain
);
14267 case DYNAMIC_CAST_EXPR
:
14268 r
= build_dynamic_cast (type
, op
, complain
);
14270 case STATIC_CAST_EXPR
:
14271 r
= build_static_cast (type
, op
, complain
);
14274 gcc_unreachable ();
14280 case POSTDECREMENT_EXPR
:
14281 case POSTINCREMENT_EXPR
:
14282 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14283 args
, complain
, in_decl
);
14284 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14285 complain
|decltype_flag
));
14287 case PREDECREMENT_EXPR
:
14288 case PREINCREMENT_EXPR
:
14292 case TRUTH_NOT_EXPR
:
14293 case UNARY_PLUS_EXPR
: /* Unary + */
14294 case REALPART_EXPR
:
14295 case IMAGPART_EXPR
:
14296 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14297 RECUR (TREE_OPERAND (t
, 0)),
14298 complain
|decltype_flag
));
14300 case FIX_TRUNC_EXPR
:
14301 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14305 op1
= TREE_OPERAND (t
, 0);
14306 if (TREE_CODE (op1
) == LABEL_DECL
)
14307 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14308 EXPR_LOCATION (op1
)));
14309 if (TREE_CODE (op1
) == SCOPE_REF
)
14310 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14311 /*done=*/true, /*address_p=*/true);
14313 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14315 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14316 complain
|decltype_flag
));
14321 case TRUNC_DIV_EXPR
:
14322 case CEIL_DIV_EXPR
:
14323 case FLOOR_DIV_EXPR
:
14324 case ROUND_DIV_EXPR
:
14325 case EXACT_DIV_EXPR
:
14329 case TRUNC_MOD_EXPR
:
14330 case FLOOR_MOD_EXPR
:
14331 case TRUTH_ANDIF_EXPR
:
14332 case TRUTH_ORIF_EXPR
:
14333 case TRUTH_AND_EXPR
:
14334 case TRUTH_OR_EXPR
:
14350 warning_sentinel
s1(warn_type_limits
);
14351 warning_sentinel
s2(warn_div_by_zero
);
14352 tree r
= build_x_binary_op
14353 (input_location
, TREE_CODE (t
),
14354 RECUR (TREE_OPERAND (t
, 0)),
14355 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14357 : TREE_CODE (TREE_OPERAND (t
, 0))),
14358 RECUR (TREE_OPERAND (t
, 1)),
14359 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14361 : TREE_CODE (TREE_OPERAND (t
, 1))),
14363 complain
|decltype_flag
);
14364 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14365 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14370 case POINTER_PLUS_EXPR
:
14371 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t
, 0)),
14372 RECUR (TREE_OPERAND (t
, 1)));
14375 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14376 /*address_p=*/false));
14378 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14379 args
, complain
, in_decl
);
14380 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14381 RECUR (TREE_OPERAND (t
, 1)),
14382 complain
|decltype_flag
));
14384 case ARRAY_NOTATION_REF
:
14386 tree start_index
, length
, stride
;
14387 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14388 args
, complain
, in_decl
);
14389 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14390 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14391 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14392 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14393 length
, stride
, TREE_TYPE (op1
)));
14396 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14397 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14404 op1
= TREE_OPERAND (t
, 0);
14405 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14406 op1
= TREE_TYPE (op1
);
14409 /* When there are no ARGS, we are trying to evaluate a
14410 non-dependent expression from the parser. Trying to do
14411 the substitutions may not work. */
14413 op1
= TREE_TYPE (op1
);
14417 ++cp_unevaluated_operand
;
14418 ++c_inhibit_evaluation_warnings
;
14420 op1
= tsubst (op1
, args
, complain
, in_decl
);
14422 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14423 /*function_p=*/false,
14424 /*integral_constant_expression_p=*/
14426 --cp_unevaluated_operand
;
14427 --c_inhibit_evaluation_warnings
;
14430 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14431 complain
& tf_error
);
14433 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14434 complain
& tf_error
);
14435 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14437 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14439 if (!processing_template_decl
&& TYPE_P (op1
))
14441 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14442 build1 (NOP_EXPR
, op1
, error_mark_node
));
14443 SIZEOF_EXPR_TYPE_P (r
) = 1;
14446 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14447 TREE_SIDE_EFFECTS (r
) = 0;
14448 TREE_READONLY (r
) = 1;
14450 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14455 case AT_ENCODE_EXPR
:
14457 op1
= TREE_OPERAND (t
, 0);
14458 ++cp_unevaluated_operand
;
14459 ++c_inhibit_evaluation_warnings
;
14460 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14461 /*function_p=*/false,
14462 /*integral_constant_expression_p=*/false);
14463 --cp_unevaluated_operand
;
14464 --c_inhibit_evaluation_warnings
;
14465 RETURN (objc_build_encode_expr (op1
));
14468 case NOEXCEPT_EXPR
:
14469 op1
= TREE_OPERAND (t
, 0);
14470 ++cp_unevaluated_operand
;
14471 ++c_inhibit_evaluation_warnings
;
14472 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14473 /*function_p=*/false,
14474 /*integral_constant_expression_p=*/false);
14475 --cp_unevaluated_operand
;
14476 --c_inhibit_evaluation_warnings
;
14477 RETURN (finish_noexcept_expr (op1
, complain
));
14481 warning_sentinel
s(warn_div_by_zero
);
14482 tree r
= build_x_modify_expr
14483 (EXPR_LOCATION (t
),
14484 RECUR (TREE_OPERAND (t
, 0)),
14485 TREE_CODE (TREE_OPERAND (t
, 1)),
14486 RECUR (TREE_OPERAND (t
, 2)),
14487 complain
|decltype_flag
);
14488 /* TREE_NO_WARNING must be set if either the expression was
14489 parenthesized or it uses an operator such as >>= rather
14490 than plain assignment. In the former case, it was already
14491 set and must be copied. In the latter case,
14492 build_x_modify_expr sets it and it must not be reset
14494 if (TREE_NO_WARNING (t
))
14495 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14501 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14502 args
, complain
, in_decl
);
14503 /* Remember that there was a reference to this entity. */
14506 RETURN (build_x_arrow (input_location
, op1
, complain
));
14510 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14511 tree init
= RECUR (TREE_OPERAND (t
, 3));
14512 vec
<tree
, va_gc
> *placement_vec
;
14513 vec
<tree
, va_gc
> *init_vec
;
14516 if (placement
== NULL_TREE
)
14517 placement_vec
= NULL
;
14520 placement_vec
= make_tree_vector ();
14521 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14522 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14525 /* If there was an initializer in the original tree, but it
14526 instantiated to an empty list, then we should pass a
14527 non-NULL empty vector to tell build_new that it was an
14528 empty initializer() rather than no initializer. This can
14529 only happen when the initializer is a pack expansion whose
14530 parameter packs are of length zero. */
14531 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14535 init_vec
= make_tree_vector ();
14536 if (init
== void_zero_node
)
14537 gcc_assert (init_vec
!= NULL
);
14540 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14541 vec_safe_push (init_vec
, TREE_VALUE (init
));
14545 ret
= build_new (&placement_vec
,
14546 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14547 RECUR (TREE_OPERAND (t
, 2)),
14549 NEW_EXPR_USE_GLOBAL (t
),
14552 if (placement_vec
!= NULL
)
14553 release_tree_vector (placement_vec
);
14554 if (init_vec
!= NULL
)
14555 release_tree_vector (init_vec
);
14561 RETURN (delete_sanity
14562 (RECUR (TREE_OPERAND (t
, 0)),
14563 RECUR (TREE_OPERAND (t
, 1)),
14564 DELETE_EXPR_USE_VEC (t
),
14565 DELETE_EXPR_USE_GLOBAL (t
),
14568 case COMPOUND_EXPR
:
14570 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14571 complain
& ~tf_decltype
, in_decl
,
14572 /*function_p=*/false,
14573 integral_constant_expression_p
);
14574 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14576 RECUR (TREE_OPERAND (t
, 1)),
14577 complain
|decltype_flag
));
14583 vec
<tree
, va_gc
> *call_args
;
14584 unsigned int nargs
, i
;
14589 function
= CALL_EXPR_FN (t
);
14590 /* When we parsed the expression, we determined whether or
14591 not Koenig lookup should be performed. */
14592 koenig_p
= KOENIG_LOOKUP_P (t
);
14593 if (TREE_CODE (function
) == SCOPE_REF
)
14595 qualified_p
= true;
14596 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14598 /*address_p=*/false);
14600 else if (koenig_p
&& identifier_p (function
))
14602 /* Do nothing; calling tsubst_copy_and_build on an identifier
14603 would incorrectly perform unqualified lookup again.
14605 Note that we can also have an IDENTIFIER_NODE if the earlier
14606 unqualified lookup found a member function; in that case
14607 koenig_p will be false and we do want to do the lookup
14608 again to find the instantiated member function.
14610 FIXME but doing that causes c++/15272, so we need to stop
14611 using IDENTIFIER_NODE in that situation. */
14612 qualified_p
= false;
14616 if (TREE_CODE (function
) == COMPONENT_REF
)
14618 tree op
= TREE_OPERAND (function
, 1);
14620 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14621 || (BASELINK_P (op
)
14622 && BASELINK_QUALIFIED_P (op
)));
14625 qualified_p
= false;
14627 if (TREE_CODE (function
) == ADDR_EXPR
14628 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14629 /* Avoid error about taking the address of a constructor. */
14630 function
= TREE_OPERAND (function
, 0);
14632 function
= tsubst_copy_and_build (function
, args
, complain
,
14635 integral_constant_expression_p
);
14637 if (BASELINK_P (function
))
14638 qualified_p
= true;
14641 nargs
= call_expr_nargs (t
);
14642 call_args
= make_tree_vector ();
14643 for (i
= 0; i
< nargs
; ++i
)
14645 tree arg
= CALL_EXPR_ARG (t
, i
);
14647 if (!PACK_EXPANSION_P (arg
))
14648 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14651 /* Expand the pack expansion and push each entry onto
14653 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14654 if (TREE_CODE (arg
) == TREE_VEC
)
14656 unsigned int len
, j
;
14658 len
= TREE_VEC_LENGTH (arg
);
14659 for (j
= 0; j
< len
; ++j
)
14661 tree value
= TREE_VEC_ELT (arg
, j
);
14662 if (value
!= NULL_TREE
)
14663 value
= convert_from_reference (value
);
14664 vec_safe_push (call_args
, value
);
14669 /* A partial substitution. Add one entry. */
14670 vec_safe_push (call_args
, arg
);
14675 /* We do not perform argument-dependent lookup if normal
14676 lookup finds a non-function, in accordance with the
14677 expected resolution of DR 218. */
14679 && ((is_overloaded_fn (function
)
14680 /* If lookup found a member function, the Koenig lookup is
14681 not appropriate, even if an unqualified-name was used
14682 to denote the function. */
14683 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14684 || identifier_p (function
))
14685 /* Only do this when substitution turns a dependent call
14686 into a non-dependent call. */
14687 && type_dependent_expression_p_push (t
)
14688 && !any_type_dependent_arguments_p (call_args
))
14689 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
14691 if (identifier_p (function
)
14692 && !any_type_dependent_arguments_p (call_args
))
14694 if (koenig_p
&& (complain
& tf_warning_or_error
))
14696 /* For backwards compatibility and good diagnostics, try
14697 the unqualified lookup again if we aren't in SFINAE
14699 tree unq
= (tsubst_copy_and_build
14700 (function
, args
, complain
, in_decl
, true,
14701 integral_constant_expression_p
));
14702 if (unq
== error_mark_node
)
14703 RETURN (error_mark_node
);
14705 if (unq
!= function
)
14708 if (INDIRECT_REF_P (fn
))
14709 fn
= TREE_OPERAND (fn
, 0);
14710 if (TREE_CODE (fn
) == COMPONENT_REF
)
14711 fn
= TREE_OPERAND (fn
, 1);
14712 if (is_overloaded_fn (fn
))
14713 fn
= get_first_fn (fn
);
14714 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
14715 "%qD was not declared in this scope, "
14716 "and no declarations were found by "
14717 "argument-dependent lookup at the point "
14718 "of instantiation", function
))
14721 /* Can't say anything more. */;
14722 else if (DECL_CLASS_SCOPE_P (fn
))
14724 location_t loc
= EXPR_LOC_OR_LOC (t
,
14727 "declarations in dependent base %qT are "
14728 "not found by unqualified lookup",
14729 DECL_CLASS_CONTEXT (fn
));
14730 if (current_class_ptr
)
14732 "use %<this->%D%> instead", function
);
14735 "use %<%T::%D%> instead",
14736 current_class_name
, function
);
14739 inform (0, "%q+D declared here, later in the "
14740 "translation unit", fn
);
14745 if (identifier_p (function
))
14747 if (complain
& tf_error
)
14748 unqualified_name_lookup_error (function
);
14749 release_tree_vector (call_args
);
14750 RETURN (error_mark_node
);
14754 /* Remember that there was a reference to this entity. */
14755 if (DECL_P (function
))
14756 mark_used (function
);
14758 /* Put back tf_decltype for the actual call. */
14759 complain
|= decltype_flag
;
14761 if (TREE_CODE (function
) == OFFSET_REF
)
14762 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14764 else if (TREE_CODE (function
) == COMPONENT_REF
)
14766 tree instance
= TREE_OPERAND (function
, 0);
14767 tree fn
= TREE_OPERAND (function
, 1);
14769 if (processing_template_decl
14770 && (type_dependent_expression_p (instance
)
14771 || (!BASELINK_P (fn
)
14772 && TREE_CODE (fn
) != FIELD_DECL
)
14773 || type_dependent_expression_p (fn
)
14774 || any_type_dependent_arguments_p (call_args
)))
14775 ret
= build_nt_call_vec (function
, call_args
);
14776 else if (!BASELINK_P (fn
))
14777 ret
= finish_call_expr (function
, &call_args
,
14778 /*disallow_virtual=*/false,
14779 /*koenig_p=*/false,
14782 ret
= (build_new_method_call
14784 &call_args
, NULL_TREE
,
14785 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14790 ret
= finish_call_expr (function
, &call_args
,
14791 /*disallow_virtual=*/qualified_p
,
14795 release_tree_vector (call_args
);
14802 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14805 if (TREE_CODE (cond
) == INTEGER_CST
)
14807 if (integer_zerop (cond
))
14809 ++c_inhibit_evaluation_warnings
;
14810 exp1
= RECUR (TREE_OPERAND (t
, 1));
14811 --c_inhibit_evaluation_warnings
;
14812 exp2
= RECUR (TREE_OPERAND (t
, 2));
14816 exp1
= RECUR (TREE_OPERAND (t
, 1));
14817 ++c_inhibit_evaluation_warnings
;
14818 exp2
= RECUR (TREE_OPERAND (t
, 2));
14819 --c_inhibit_evaluation_warnings
;
14824 exp1
= RECUR (TREE_OPERAND (t
, 1));
14825 exp2
= RECUR (TREE_OPERAND (t
, 2));
14828 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14829 cond
, exp1
, exp2
, complain
));
14832 case PSEUDO_DTOR_EXPR
:
14833 RETURN (finish_pseudo_destructor_expr
14834 (RECUR (TREE_OPERAND (t
, 0)),
14835 RECUR (TREE_OPERAND (t
, 1)),
14836 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14841 tree purpose
, value
, chain
;
14843 if (t
== void_list_node
)
14846 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14847 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14849 /* We have pack expansions, so expand those and
14850 create a new list out of it. */
14851 tree purposevec
= NULL_TREE
;
14852 tree valuevec
= NULL_TREE
;
14856 /* Expand the argument expressions. */
14857 if (TREE_PURPOSE (t
))
14858 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14859 complain
, in_decl
);
14860 if (TREE_VALUE (t
))
14861 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14862 complain
, in_decl
);
14864 /* Build the rest of the list. */
14865 chain
= TREE_CHAIN (t
);
14866 if (chain
&& chain
!= void_type_node
)
14867 chain
= RECUR (chain
);
14869 /* Determine the number of arguments. */
14870 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14872 len
= TREE_VEC_LENGTH (purposevec
);
14873 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14875 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14876 len
= TREE_VEC_LENGTH (valuevec
);
14879 /* Since we only performed a partial substitution into
14880 the argument pack, we only RETURN (a single list
14882 if (purposevec
== TREE_PURPOSE (t
)
14883 && valuevec
== TREE_VALUE (t
)
14884 && chain
== TREE_CHAIN (t
))
14887 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14890 /* Convert the argument vectors into a TREE_LIST */
14894 /* Grab the Ith values. */
14896 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14899 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14902 /* Build the list (backwards). */
14903 chain
= tree_cons (purpose
, value
, chain
);
14909 purpose
= TREE_PURPOSE (t
);
14911 purpose
= RECUR (purpose
);
14912 value
= TREE_VALUE (t
);
14914 value
= RECUR (value
);
14915 chain
= TREE_CHAIN (t
);
14916 if (chain
&& chain
!= void_type_node
)
14917 chain
= RECUR (chain
);
14918 if (purpose
== TREE_PURPOSE (t
)
14919 && value
== TREE_VALUE (t
)
14920 && chain
== TREE_CHAIN (t
))
14922 RETURN (tree_cons (purpose
, value
, chain
));
14925 case COMPONENT_REF
:
14932 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14933 args
, complain
, in_decl
);
14934 /* Remember that there was a reference to this entity. */
14935 if (DECL_P (object
))
14936 mark_used (object
);
14937 object_type
= TREE_TYPE (object
);
14939 member
= TREE_OPERAND (t
, 1);
14940 if (BASELINK_P (member
))
14941 member
= tsubst_baselink (member
,
14942 non_reference (TREE_TYPE (object
)),
14943 args
, complain
, in_decl
);
14945 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14946 if (member
== error_mark_node
)
14947 RETURN (error_mark_node
);
14949 if (type_dependent_expression_p (object
))
14950 /* We can't do much here. */;
14951 else if (!CLASS_TYPE_P (object_type
))
14953 if (scalarish_type_p (object_type
))
14955 tree s
= NULL_TREE
;
14956 tree dtor
= member
;
14958 if (TREE_CODE (dtor
) == SCOPE_REF
)
14960 s
= TREE_OPERAND (dtor
, 0);
14961 dtor
= TREE_OPERAND (dtor
, 1);
14963 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14965 dtor
= TREE_OPERAND (dtor
, 0);
14967 RETURN (finish_pseudo_destructor_expr
14968 (object
, s
, dtor
, input_location
));
14972 else if (TREE_CODE (member
) == SCOPE_REF
14973 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14975 /* Lookup the template functions now that we know what the
14977 tree scope
= TREE_OPERAND (member
, 0);
14978 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14979 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14980 member
= lookup_qualified_name (scope
, tmpl
,
14981 /*is_type_p=*/false,
14982 /*complain=*/false);
14983 if (BASELINK_P (member
))
14985 BASELINK_FUNCTIONS (member
)
14986 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14988 member
= (adjust_result_of_qualified_name_lookup
14989 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14994 qualified_name_lookup_error (scope
, tmpl
, member
,
14996 RETURN (error_mark_node
);
14999 else if (TREE_CODE (member
) == SCOPE_REF
15000 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
15001 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
15003 if (complain
& tf_error
)
15005 if (TYPE_P (TREE_OPERAND (member
, 0)))
15006 error ("%qT is not a class or namespace",
15007 TREE_OPERAND (member
, 0));
15009 error ("%qD is not a class or namespace",
15010 TREE_OPERAND (member
, 0));
15012 RETURN (error_mark_node
);
15014 else if (TREE_CODE (member
) == FIELD_DECL
)
15016 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
15017 if (TREE_CODE (r
) == COMPONENT_REF
)
15018 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15022 r
= finish_class_member_access_expr (object
, member
,
15023 /*template_p=*/false,
15025 if (TREE_CODE (r
) == COMPONENT_REF
)
15026 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15031 RETURN (build_throw
15032 (RECUR (TREE_OPERAND (t
, 0))));
15036 vec
<constructor_elt
, va_gc
> *n
;
15037 constructor_elt
*ce
;
15038 unsigned HOST_WIDE_INT idx
;
15039 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15040 bool process_index_p
;
15042 bool need_copy_p
= false;
15045 if (type
== error_mark_node
)
15046 RETURN (error_mark_node
);
15048 /* digest_init will do the wrong thing if we let it. */
15049 if (type
&& TYPE_PTRMEMFUNC_P (type
))
15052 /* We do not want to process the index of aggregate
15053 initializers as they are identifier nodes which will be
15054 looked up by digest_init. */
15055 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
15057 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
15058 newlen
= vec_safe_length (n
);
15059 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
15061 if (ce
->index
&& process_index_p
15062 /* An identifier index is looked up in the type
15063 being initialized, not the current scope. */
15064 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
15065 ce
->index
= RECUR (ce
->index
);
15067 if (PACK_EXPANSION_P (ce
->value
))
15069 /* Substitute into the pack expansion. */
15070 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
15073 if (ce
->value
== error_mark_node
15074 || PACK_EXPANSION_P (ce
->value
))
15076 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
15077 /* Just move the argument into place. */
15078 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
15081 /* Update the length of the final CONSTRUCTOR
15082 arguments vector, and note that we will need to
15084 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
15085 need_copy_p
= true;
15089 ce
->value
= RECUR (ce
->value
);
15094 vec
<constructor_elt
, va_gc
> *old_n
= n
;
15096 vec_alloc (n
, newlen
);
15097 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
15099 if (TREE_CODE (ce
->value
) == TREE_VEC
)
15101 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
15102 for (i
= 0; i
< len
; ++i
)
15103 CONSTRUCTOR_APPEND_ELT (n
, 0,
15104 TREE_VEC_ELT (ce
->value
, i
));
15107 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
15111 r
= build_constructor (init_list_type_node
, n
);
15112 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
15114 if (TREE_HAS_CONSTRUCTOR (t
))
15115 RETURN (finish_compound_literal (type
, r
, complain
));
15117 TREE_TYPE (r
) = type
;
15123 tree operand_0
= TREE_OPERAND (t
, 0);
15124 if (TYPE_P (operand_0
))
15126 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
15127 RETURN (get_typeid (operand_0
, complain
));
15131 operand_0
= RECUR (operand_0
);
15132 RETURN (build_typeid (operand_0
, complain
));
15139 else if (DECL_PACK_P (t
))
15141 /* We don't build decls for an instantiation of a
15142 variadic capture proxy, we instantiate the elements
15144 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
15145 return RECUR (DECL_VALUE_EXPR (t
));
15151 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
15153 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
15154 /* If the original type was a reference, we'll be wrapped in
15155 the appropriate INDIRECT_REF. */
15156 r
= convert_from_reference (r
);
15161 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
15162 RECUR (TREE_OPERAND (t
, 0)),
15163 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
15165 case OFFSETOF_EXPR
:
15166 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
15170 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
15171 complain
, in_decl
);
15173 tree type2
= TRAIT_EXPR_TYPE2 (t
);
15175 type2
= tsubst (type2
, args
, complain
, in_decl
);
15177 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
15182 tree old_stmt_expr
= cur_stmt_expr
;
15183 tree stmt_expr
= begin_stmt_expr ();
15185 cur_stmt_expr
= stmt_expr
;
15186 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
15187 integral_constant_expression_p
);
15188 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
15189 cur_stmt_expr
= old_stmt_expr
;
15191 /* If the resulting list of expression statement is empty,
15192 fold it further into void_zero_node. */
15193 if (empty_expr_stmt_p (stmt_expr
))
15194 stmt_expr
= void_zero_node
;
15196 RETURN (stmt_expr
);
15201 tree r
= build_lambda_expr ();
15203 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15204 LAMBDA_EXPR_CLOSURE (r
) = type
;
15205 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15207 LAMBDA_EXPR_LOCATION (r
)
15208 = LAMBDA_EXPR_LOCATION (t
);
15209 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15210 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15211 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15212 LAMBDA_EXPR_DISCRIMINATOR (r
)
15213 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15214 /* For a function scope, we want to use tsubst so that we don't
15215 complain about referring to an auto function before its return
15216 type has been deduced. Otherwise, we want to use tsubst_copy so
15217 that we look up the existing field/parameter/variable rather
15218 than build a new one. */
15219 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15220 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15221 scope
= tsubst (scope
, args
, complain
, in_decl
);
15222 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15224 /* Look up the parameter we want directly, as tsubst_copy
15225 doesn't do what we need. */
15226 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15227 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15228 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15229 parm
= DECL_CHAIN (parm
);
15231 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15232 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15233 DECL_CONTEXT (scope
) = fn
;
15236 scope
= RECUR (scope
);
15237 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15238 LAMBDA_EXPR_RETURN_TYPE (r
)
15239 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15241 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15242 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15244 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15245 determine_visibility (TYPE_NAME (type
));
15246 /* Now that we know visibility, instantiate the type so we have a
15247 declaration of the op() for later calls to lambda_function. */
15248 complete_type (type
);
15250 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15252 RETURN (build_lambda_object (r
));
15256 /* We can get here for a constant initializer of non-dependent type.
15257 FIXME stop folding in cp_parser_initializer_clause. */
15259 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15264 case TRANSACTION_EXPR
:
15265 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15266 integral_constant_expression_p
));
15269 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15271 case VEC_PERM_EXPR
:
15272 RETURN (build_x_vec_perm_expr (input_location
,
15273 RECUR (TREE_OPERAND (t
, 0)),
15274 RECUR (TREE_OPERAND (t
, 1)),
15275 RECUR (TREE_OPERAND (t
, 2)),
15279 /* Handle Objective-C++ constructs, if appropriate. */
15282 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15283 in_decl
, /*function_p=*/false);
15287 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15293 input_location
= loc
;
15297 /* Verify that the instantiated ARGS are valid. For type arguments,
15298 make sure that the type's linkage is ok. For non-type arguments,
15299 make sure they are constants if they are integral or enumerations.
15300 Emit an error under control of COMPLAIN, and return TRUE on error. */
15303 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15305 if (dependent_template_arg_p (t
))
15307 if (ARGUMENT_PACK_P (t
))
15309 tree vec
= ARGUMENT_PACK_ARGS (t
);
15310 int len
= TREE_VEC_LENGTH (vec
);
15311 bool result
= false;
15314 for (i
= 0; i
< len
; ++i
)
15315 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15319 else if (TYPE_P (t
))
15321 /* [basic.link]: A name with no linkage (notably, the name
15322 of a class or enumeration declared in a local scope)
15323 shall not be used to declare an entity with linkage.
15324 This implies that names with no linkage cannot be used as
15327 DR 757 relaxes this restriction for C++0x. */
15328 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15329 : no_linkage_check (t
, /*relaxed_p=*/false));
15333 /* DR 488 makes use of a type with no linkage cause
15334 type deduction to fail. */
15335 if (complain
& tf_error
)
15337 if (TYPE_ANONYMOUS_P (nt
))
15338 error ("%qT is/uses anonymous type", t
);
15340 error ("template argument for %qD uses local type %qT",
15345 /* In order to avoid all sorts of complications, we do not
15346 allow variably-modified types as template arguments. */
15347 else if (variably_modified_type_p (t
, NULL_TREE
))
15349 if (complain
& tf_error
)
15350 error ("%qT is a variably modified type", t
);
15354 /* Class template and alias template arguments should be OK. */
15355 else if (DECL_TYPE_TEMPLATE_P (t
))
15357 /* A non-type argument of integral or enumerated type must be a
15359 else if (TREE_TYPE (t
)
15360 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15361 && !TREE_CONSTANT (t
))
15363 if (complain
& tf_error
)
15364 error ("integral expression %qE is not constant", t
);
15371 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15373 int ix
, len
= DECL_NTPARMS (tmpl
);
15374 bool result
= false;
15376 for (ix
= 0; ix
!= len
; ix
++)
15378 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15381 if (result
&& (complain
& tf_error
))
15382 error (" trying to instantiate %qD", tmpl
);
15386 /* We're out of SFINAE context now, so generate diagnostics for the access
15387 errors we saw earlier when instantiating D from TMPL and ARGS. */
15390 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15392 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15393 tree type
= TREE_TYPE (pattern
);
15394 location_t loc
= input_location
;
15396 push_access_scope (d
);
15397 push_deferring_access_checks (dk_no_deferred
);
15398 input_location
= DECL_SOURCE_LOCATION (pattern
);
15399 tsubst (type
, args
, tf_warning_or_error
, d
);
15400 input_location
= loc
;
15401 pop_deferring_access_checks ();
15402 pop_access_scope (d
);
15405 /* Instantiate the indicated variable, function, or alias template TMPL with
15406 the template arguments in TARG_PTR. */
15409 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15411 tree targ_ptr
= orig_args
;
15415 bool access_ok
= true;
15417 if (tmpl
== error_mark_node
)
15418 return error_mark_node
;
15420 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15422 /* If this function is a clone, handle it specially. */
15423 if (DECL_CLONED_FUNCTION_P (tmpl
))
15428 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15429 DECL_CLONED_FUNCTION. */
15430 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15431 targ_ptr
, complain
);
15432 if (spec
== error_mark_node
)
15433 return error_mark_node
;
15435 /* Look for the clone. */
15436 FOR_EACH_CLONE (clone
, spec
)
15437 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15439 /* We should always have found the clone by now. */
15440 gcc_unreachable ();
15444 if (targ_ptr
== error_mark_node
)
15445 return error_mark_node
;
15447 /* Check to see if we already have this specialization. */
15448 gen_tmpl
= most_general_template (tmpl
);
15449 if (tmpl
!= gen_tmpl
)
15450 /* The TMPL is a partial instantiation. To get a full set of
15451 arguments we must add the arguments used to perform the
15452 partial instantiation. */
15453 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15456 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15457 but it doesn't seem to be on the hot path. */
15458 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15460 gcc_assert (tmpl
== gen_tmpl
15461 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15463 || fndecl
== NULL_TREE
);
15465 if (spec
!= NULL_TREE
)
15467 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15469 if (complain
& tf_error
)
15470 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15471 return error_mark_node
;
15476 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15478 return error_mark_node
;
15480 /* We are building a FUNCTION_DECL, during which the access of its
15481 parameters and return types have to be checked. However this
15482 FUNCTION_DECL which is the desired context for access checking
15483 is not built yet. We solve this chicken-and-egg problem by
15484 deferring all checks until we have the FUNCTION_DECL. */
15485 push_deferring_access_checks (dk_deferred
);
15487 /* Instantiation of the function happens in the context of the function
15488 template, not the context of the overload resolution we're doing. */
15489 push_to_top_level ();
15490 /* If there are dependent arguments, e.g. because we're doing partial
15491 ordering, make sure processing_template_decl stays set. */
15492 if (uses_template_parms (targ_ptr
))
15493 ++processing_template_decl
;
15494 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15496 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15497 complain
, gen_tmpl
);
15498 push_nested_class (ctx
);
15500 /* Substitute template parameters to obtain the specialization. */
15501 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15502 targ_ptr
, complain
, gen_tmpl
);
15503 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15504 pop_nested_class ();
15505 pop_from_top_level ();
15507 if (fndecl
== error_mark_node
)
15509 pop_deferring_access_checks ();
15510 return error_mark_node
;
15513 /* The DECL_TI_TEMPLATE should always be the immediate parent
15514 template, not the most general template. */
15515 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15517 /* Now we know the specialization, compute access previously
15519 push_access_scope (fndecl
);
15520 if (!perform_deferred_access_checks (complain
))
15522 pop_access_scope (fndecl
);
15523 pop_deferring_access_checks ();
15525 /* If we've just instantiated the main entry point for a function,
15526 instantiate all the alternate entry points as well. We do this
15527 by cloning the instantiation of the main entry point, not by
15528 instantiating the template clones. */
15529 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15530 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15534 if (!(complain
& tf_error
))
15536 /* Remember to reinstantiate when we're out of SFINAE so the user
15537 can see the errors. */
15538 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15540 return error_mark_node
;
15545 /* Wrapper for instantiate_template_1. */
15548 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15551 timevar_push (TV_TEMPLATE_INST
);
15552 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15553 timevar_pop (TV_TEMPLATE_INST
);
15557 /* Instantiate the alias template TMPL with ARGS. Also push a template
15558 instantiation level, which instantiate_template doesn't do because
15559 functions and variables have sufficient context established by the
15563 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15565 struct pending_template
*old_last_pend
= last_pending_template
;
15566 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15567 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15568 return error_mark_node
;
15569 tree tinst
= build_tree_list (tmpl
, args
);
15570 if (!push_tinst_level (tinst
))
15573 return error_mark_node
;
15577 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15578 args
, tmpl
, complain
,
15579 /*require_all_args=*/true,
15580 /*use_default_args=*/true);
15582 tree r
= instantiate_template (tmpl
, args
, complain
);
15583 pop_tinst_level ();
15584 /* We can't free this if a pending_template entry or last_error_tinst_level
15585 is pointing at it. */
15586 if (last_pending_template
== old_last_pend
15587 && last_error_tinst_level
== old_error_tinst
)
15593 /* PARM is a template parameter pack for FN. Returns true iff
15594 PARM is used in a deducible way in the argument list of FN. */
15597 pack_deducible_p (tree parm
, tree fn
)
15599 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15600 for (; t
; t
= TREE_CHAIN (t
))
15602 tree type
= TREE_VALUE (t
);
15604 if (!PACK_EXPANSION_P (type
))
15606 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15607 packs
; packs
= TREE_CHAIN (packs
))
15608 if (TREE_VALUE (packs
) == parm
)
15610 /* The template parameter pack is used in a function parameter
15611 pack. If this is the end of the parameter list, the
15612 template parameter pack is deducible. */
15613 if (TREE_CHAIN (t
) == void_list_node
)
15616 /* Otherwise, not. Well, it could be deduced from
15617 a non-pack parameter, but doing so would end up with
15618 a deduction mismatch, so don't bother. */
15622 /* The template parameter pack isn't used in any function parameter
15623 packs, but it might be used deeper, e.g. tuple<Args...>. */
15627 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15628 NARGS elements of the arguments that are being used when calling
15629 it. TARGS is a vector into which the deduced template arguments
15632 Returns either a FUNCTION_DECL for the matching specialization of FN or
15633 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15634 true, diagnostics will be printed to explain why it failed.
15636 If FN is a conversion operator, or we are trying to produce a specific
15637 specialization, RETURN_TYPE is the return type desired.
15639 The EXPLICIT_TARGS are explicit template arguments provided via a
15642 The parameter STRICT is one of:
15645 We are deducing arguments for a function call, as in
15646 [temp.deduct.call].
15649 We are deducing arguments for a conversion function, as in
15650 [temp.deduct.conv].
15653 We are deducing arguments when doing an explicit instantiation
15654 as in [temp.explicit], when determining an explicit specialization
15655 as in [temp.expl.spec], or when taking the address of a function
15656 template, as in [temp.deduct.funcaddr]. */
15659 fn_type_unification (tree fn
,
15660 tree explicit_targs
,
15663 unsigned int nargs
,
15665 unification_kind_t strict
,
15672 tree decl
= NULL_TREE
;
15673 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15675 static int deduction_depth
;
15676 struct pending_template
*old_last_pend
= last_pending_template
;
15677 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15678 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15680 tree r
= error_mark_node
;
15683 complain
|= tf_decltype
;
15685 /* In C++0x, it's possible to have a function template whose type depends
15686 on itself recursively. This is most obvious with decltype, but can also
15687 occur with enumeration scope (c++/48969). So we need to catch infinite
15688 recursion and reject the substitution at deduction time; this function
15689 will return error_mark_node for any repeated substitution.
15691 This also catches excessive recursion such as when f<N> depends on
15692 f<N-1> across all integers, and returns error_mark_node for all the
15693 substitutions back up to the initial one.
15695 This is, of course, not reentrant. */
15696 if (excessive_deduction_depth
)
15697 return error_mark_node
;
15698 tinst
= build_tree_list (fn
, NULL_TREE
);
15701 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15703 fntype
= TREE_TYPE (fn
);
15704 if (explicit_targs
)
15708 The specified template arguments must match the template
15709 parameters in kind (i.e., type, nontype, template), and there
15710 must not be more arguments than there are parameters;
15711 otherwise type deduction fails.
15713 Nontype arguments must match the types of the corresponding
15714 nontype template parameters, or must be convertible to the
15715 types of the corresponding nontype parameters as specified in
15716 _temp.arg.nontype_, otherwise type deduction fails.
15718 All references in the function type of the function template
15719 to the corresponding template parameters are replaced by the
15720 specified template argument values. If a substitution in a
15721 template parameter or in the function type of the function
15722 template results in an invalid type, type deduction fails. */
15723 int i
, len
= TREE_VEC_LENGTH (tparms
);
15724 location_t loc
= input_location
;
15725 bool incomplete
= false;
15727 /* Adjust any explicit template arguments before entering the
15728 substitution context. */
15730 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15732 /*require_all_args=*/false,
15733 /*use_default_args=*/false));
15734 if (explicit_targs
== error_mark_node
)
15737 /* Substitute the explicit args into the function type. This is
15738 necessary so that, for instance, explicitly declared function
15739 arguments can match null pointed constants. If we were given
15740 an incomplete set of explicit args, we must not do semantic
15741 processing during substitution as we could create partial
15743 for (i
= 0; i
< len
; i
++)
15745 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15746 bool parameter_pack
= false;
15747 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15749 /* Dig out the actual parm. */
15750 if (TREE_CODE (parm
) == TYPE_DECL
15751 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15753 parm
= TREE_TYPE (parm
);
15754 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15756 else if (TREE_CODE (parm
) == PARM_DECL
)
15758 parm
= DECL_INITIAL (parm
);
15759 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15762 if (!parameter_pack
&& targ
== NULL_TREE
)
15763 /* No explicit argument for this template parameter. */
15766 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15768 /* Mark the argument pack as "incomplete". We could
15769 still deduce more arguments during unification.
15770 We remove this mark in type_unification_real. */
15773 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15774 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15775 = ARGUMENT_PACK_ARGS (targ
);
15778 /* We have some incomplete argument packs. */
15783 TREE_VALUE (tinst
) = explicit_targs
;
15784 if (!push_tinst_level (tinst
))
15786 excessive_deduction_depth
= true;
15789 processing_template_decl
+= incomplete
;
15790 input_location
= DECL_SOURCE_LOCATION (fn
);
15791 /* Ignore any access checks; we'll see them again in
15792 instantiate_template and they might have the wrong
15793 access path at this point. */
15794 push_deferring_access_checks (dk_deferred
);
15795 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15796 complain
| tf_partial
, NULL_TREE
);
15797 pop_deferring_access_checks ();
15798 input_location
= loc
;
15799 processing_template_decl
-= incomplete
;
15800 pop_tinst_level ();
15802 if (fntype
== error_mark_node
)
15805 /* Place the explicitly specified arguments in TARGS. */
15806 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15807 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15810 /* Never do unification on the 'this' parameter. */
15811 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15817 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15818 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15819 new_args
[0] = return_type
;
15820 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15825 /* We allow incomplete unification without an error message here
15826 because the standard doesn't seem to explicitly prohibit it. Our
15827 callers must be ready to deal with unification failures in any
15830 TREE_VALUE (tinst
) = targs
;
15831 /* If we aren't explaining yet, push tinst context so we can see where
15832 any errors (e.g. from class instantiations triggered by instantiation
15833 of default template arguments) come from. If we are explaining, this
15834 context is redundant. */
15835 if (!explain_p
&& !push_tinst_level (tinst
))
15837 excessive_deduction_depth
= true;
15841 /* type_unification_real will pass back any access checks from default
15842 template argument substitution. */
15843 vec
<deferred_access_check
, va_gc
> *checks
;
15846 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15847 targs
, parms
, args
, nargs
, /*subr=*/0,
15848 strict
, flags
, &checks
, explain_p
);
15850 pop_tinst_level ();
15854 /* Now that we have bindings for all of the template arguments,
15855 ensure that the arguments deduced for the template template
15856 parameters have compatible template parameter lists. We cannot
15857 check this property before we have deduced all template
15858 arguments, because the template parameter types of a template
15859 template parameter might depend on prior template parameters
15860 deduced after the template template parameter. The following
15861 ill-formed example illustrates this issue:
15863 template<typename T, template<T> class C> void f(C<5>, T);
15865 template<int N> struct X {};
15868 f(X<5>(), 5l); // error: template argument deduction fails
15871 The template parameter list of 'C' depends on the template type
15872 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15873 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15874 time that we deduce 'C'. */
15875 if (!template_template_parm_bindings_ok_p
15876 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15878 unify_inconsistent_template_template_parameters (explain_p
);
15882 /* All is well so far. Now, check:
15886 When all template arguments have been deduced, all uses of
15887 template parameters in nondeduced contexts are replaced with
15888 the corresponding deduced argument values. If the
15889 substitution results in an invalid type, as described above,
15890 type deduction fails. */
15891 TREE_VALUE (tinst
) = targs
;
15892 if (!push_tinst_level (tinst
))
15894 excessive_deduction_depth
= true;
15898 /* Also collect access checks from the instantiation. */
15899 reopen_deferring_access_checks (checks
);
15901 decl
= instantiate_template (fn
, targs
, complain
);
15903 checks
= get_deferred_access_checks ();
15904 pop_deferring_access_checks ();
15906 pop_tinst_level ();
15908 if (decl
== error_mark_node
)
15911 /* Now perform any access checks encountered during substitution. */
15912 push_access_scope (decl
);
15913 ok
= perform_access_checks (checks
, complain
);
15914 pop_access_scope (decl
);
15918 /* If we're looking for an exact match, check that what we got
15919 is indeed an exact match. It might not be if some template
15920 parameters are used in non-deduced contexts. But don't check
15921 for an exact match if we have dependent template arguments;
15922 in that case we're doing partial ordering, and we already know
15923 that we have two candidates that will provide the actual type. */
15924 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
15926 tree substed
= TREE_TYPE (decl
);
15930 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15932 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15933 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15934 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15936 unify_type_mismatch (explain_p
, args
[i
],
15937 TREE_VALUE (sarg
));
15946 if (excessive_deduction_depth
)
15948 if (deduction_depth
== 0)
15949 /* Reset once we're all the way out. */
15950 excessive_deduction_depth
= false;
15953 /* We can't free this if a pending_template entry or last_error_tinst_level
15954 is pointing at it. */
15955 if (last_pending_template
== old_last_pend
15956 && last_error_tinst_level
== old_error_tinst
)
15962 /* Adjust types before performing type deduction, as described in
15963 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15964 sections are symmetric. PARM is the type of a function parameter
15965 or the return type of the conversion function. ARG is the type of
15966 the argument passed to the call, or the type of the value
15967 initialized with the result of the conversion function.
15968 ARG_EXPR is the original argument expression, which may be null. */
15971 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15985 /* Swap PARM and ARG throughout the remainder of this
15986 function; the handling is precisely symmetric since PARM
15987 will initialize ARG rather than vice versa. */
15995 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15996 too, but here handle it by stripping the reference from PARM
15997 rather than by adding it to ARG. */
15998 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15999 && TYPE_REF_IS_RVALUE (*parm
)
16000 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16001 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16002 && TREE_CODE (*arg
) == REFERENCE_TYPE
16003 && !TYPE_REF_IS_RVALUE (*arg
))
16004 *parm
= TREE_TYPE (*parm
);
16005 /* Nothing else to do in this case. */
16009 gcc_unreachable ();
16012 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
16014 /* [temp.deduct.call]
16016 If P is not a reference type:
16018 --If A is an array type, the pointer type produced by the
16019 array-to-pointer standard conversion (_conv.array_) is
16020 used in place of A for type deduction; otherwise,
16022 --If A is a function type, the pointer type produced by
16023 the function-to-pointer standard conversion
16024 (_conv.func_) is used in place of A for type deduction;
16027 --If A is a cv-qualified type, the top level
16028 cv-qualifiers of A's type are ignored for type
16030 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
16031 *arg
= build_pointer_type (TREE_TYPE (*arg
));
16032 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
16033 *arg
= build_pointer_type (*arg
);
16035 *arg
= TYPE_MAIN_VARIANT (*arg
);
16038 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16039 of the form T&&, where T is a template parameter, and the argument
16040 is an lvalue, T is deduced as A& */
16041 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16042 && TYPE_REF_IS_RVALUE (*parm
)
16043 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16044 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16045 && (arg_expr
? real_lvalue_p (arg_expr
)
16046 /* try_one_overload doesn't provide an arg_expr, but
16047 functions are always lvalues. */
16048 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
16049 *arg
= build_reference_type (*arg
);
16051 /* [temp.deduct.call]
16053 If P is a cv-qualified type, the top level cv-qualifiers
16054 of P's type are ignored for type deduction. If P is a
16055 reference type, the type referred to by P is used for
16057 *parm
= TYPE_MAIN_VARIANT (*parm
);
16058 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
16060 *parm
= TREE_TYPE (*parm
);
16061 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16064 /* DR 322. For conversion deduction, remove a reference type on parm
16065 too (which has been swapped into ARG). */
16066 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
16067 *arg
= TREE_TYPE (*arg
);
16072 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16073 template which does contain any deducible template parameters; check if
16074 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16075 unify_one_argument. */
16078 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
16079 int flags
, bool explain_p
)
16084 type
= TREE_TYPE (arg
);
16088 if (same_type_p (parm
, type
))
16089 return unify_success (explain_p
);
16091 if (strict
== DEDUCE_CONV
)
16093 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
16094 explain_p
? tf_warning_or_error
: tf_none
))
16095 return unify_success (explain_p
);
16097 else if (strict
!= DEDUCE_EXACT
)
16099 if (can_convert_arg (parm
, type
,
16100 TYPE_P (arg
) ? NULL_TREE
: arg
,
16101 flags
, explain_p
? tf_warning_or_error
: tf_none
))
16102 return unify_success (explain_p
);
16105 if (strict
== DEDUCE_EXACT
)
16106 return unify_type_mismatch (explain_p
, parm
, arg
);
16108 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
16111 static bool uses_deducible_template_parms (tree type
);
16113 /* Returns true iff the expression EXPR is one from which a template
16114 argument can be deduced. In other words, if it's an undecorated
16115 use of a template non-type parameter. */
16118 deducible_expression (tree expr
)
16120 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
16123 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16124 deducible way; that is, if it has a max value of <PARM> - 1. */
16127 deducible_array_bound (tree domain
)
16129 if (domain
== NULL_TREE
)
16132 tree max
= TYPE_MAX_VALUE (domain
);
16133 if (TREE_CODE (max
) != MINUS_EXPR
)
16136 return deducible_expression (TREE_OPERAND (max
, 0));
16139 /* Returns true iff the template arguments ARGS use a template parameter
16140 in a deducible way. */
16143 deducible_template_args (tree args
)
16145 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
16148 tree elt
= TREE_VEC_ELT (args
, i
);
16149 if (ARGUMENT_PACK_P (elt
))
16150 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
16153 if (PACK_EXPANSION_P (elt
))
16154 elt
= PACK_EXPANSION_PATTERN (elt
);
16155 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
16157 else if (TYPE_P (elt
))
16158 deducible
= uses_deducible_template_parms (elt
);
16160 deducible
= deducible_expression (elt
);
16168 /* Returns true iff TYPE contains any deducible references to template
16169 parameters, as per 14.8.2.5. */
16172 uses_deducible_template_parms (tree type
)
16174 if (PACK_EXPANSION_P (type
))
16175 type
= PACK_EXPANSION_PATTERN (type
);
16182 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16183 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16189 if (POINTER_TYPE_P (type
))
16190 return uses_deducible_template_parms (TREE_TYPE (type
));
16192 /* T[integer-constant ]
16194 if (TREE_CODE (type
) == ARRAY_TYPE
)
16195 return (uses_deducible_template_parms (TREE_TYPE (type
))
16196 || deducible_array_bound (TYPE_DOMAIN (type
)));
16208 if (TYPE_PTRMEM_P (type
))
16209 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16210 || (uses_deducible_template_parms
16211 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16213 /* template-name <T> (where template-name refers to a class template)
16214 template-name <i> (where template-name refers to a class template) */
16215 if (CLASS_TYPE_P (type
)
16216 && CLASSTYPE_TEMPLATE_INFO (type
)
16217 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16218 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16219 (CLASSTYPE_TI_ARGS (type
)));
16224 if (TREE_CODE (type
) == FUNCTION_TYPE
16225 || TREE_CODE (type
) == METHOD_TYPE
)
16227 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16229 tree parm
= TYPE_ARG_TYPES (type
);
16230 if (TREE_CODE (type
) == METHOD_TYPE
)
16231 parm
= TREE_CHAIN (parm
);
16232 for (; parm
; parm
= TREE_CHAIN (parm
))
16233 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16240 /* Subroutine of type_unification_real and unify_pack_expansion to
16241 handle unification of a single P/A pair. Parameters are as
16242 for those functions. */
16245 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16246 int subr
, unification_kind_t strict
, int flags
,
16249 tree arg_expr
= NULL_TREE
;
16252 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16253 return unify_invalid (explain_p
);
16254 if (arg
== unknown_type_node
)
16255 /* We can't deduce anything from this, but we might get all the
16256 template args from other function args. */
16257 return unify_success (explain_p
);
16259 /* Implicit conversions (Clause 4) will be performed on a function
16260 argument to convert it to the type of the corresponding function
16261 parameter if the parameter type contains no template-parameters that
16262 participate in template argument deduction. */
16263 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16264 /* For function parameters that contain no template-parameters at all,
16265 we have historically checked for convertibility in order to shortcut
16266 consideration of this candidate. */
16267 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16269 else if (strict
== DEDUCE_CALL
16270 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16271 /* For function parameters with only non-deducible template parameters,
16273 return unify_success (explain_p
);
16278 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16279 | UNIFY_ALLOW_MORE_CV_QUAL
16280 | UNIFY_ALLOW_DERIVED
);
16284 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16288 arg_strict
= UNIFY_ALLOW_NONE
;
16292 gcc_unreachable ();
16295 /* We only do these transformations if this is the top-level
16296 parameter_type_list in a call or declaration matching; in other
16297 situations (nested function declarators, template argument lists) we
16298 won't be comparing a type to an expression, and we don't do any type
16304 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16305 if (type_unknown_p (arg
))
16307 /* [temp.deduct.type] A template-argument can be
16308 deduced from a pointer to function or pointer
16309 to member function argument if the set of
16310 overloaded functions does not contain function
16311 templates and at most one of a set of
16312 overloaded functions provides a unique
16315 if (resolve_overloaded_unification
16316 (tparms
, targs
, parm
, arg
, strict
,
16317 arg_strict
, explain_p
))
16318 return unify_success (explain_p
);
16319 return unify_overload_resolution_failure (explain_p
, arg
);
16323 arg
= unlowered_expr_type (arg
);
16324 if (arg
== error_mark_node
)
16325 return unify_invalid (explain_p
);
16329 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16332 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16333 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16335 /* For deduction from an init-list we need the actual list. */
16336 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16338 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16341 /* Most parms like fn_type_unification.
16343 If SUBR is 1, we're being called recursively (to unify the
16344 arguments of a function or method parameter of a function
16347 CHECKS is a pointer to a vector of access checks encountered while
16348 substituting default template arguments. */
16351 type_unification_real (tree tparms
,
16355 unsigned int xnargs
,
16357 unification_kind_t strict
,
16359 vec
<deferred_access_check
, va_gc
> **checks
,
16364 int ntparms
= TREE_VEC_LENGTH (tparms
);
16365 int saw_undeduced
= 0;
16368 unsigned int nargs
;
16371 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16372 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16373 gcc_assert (ntparms
> 0);
16375 /* Reset the number of non-defaulted template arguments contained
16377 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16385 while (parms
&& parms
!= void_list_node
16388 parm
= TREE_VALUE (parms
);
16390 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16391 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16392 /* For a function parameter pack that occurs at the end of the
16393 parameter-declaration-list, the type A of each remaining
16394 argument of the call is compared with the type P of the
16395 declarator-id of the function parameter pack. */
16398 parms
= TREE_CHAIN (parms
);
16400 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16401 /* For a function parameter pack that does not occur at the
16402 end of the parameter-declaration-list, the type of the
16403 parameter pack is a non-deduced context. */
16409 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16415 && parms
!= void_list_node
16416 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16418 /* Unify the remaining arguments with the pack expansion type. */
16420 tree parmvec
= make_tree_vec (1);
16422 /* Allocate a TREE_VEC and copy in all of the arguments */
16423 argvec
= make_tree_vec (nargs
- ia
);
16424 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16425 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16427 /* Copy the parameter into parmvec. */
16428 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16429 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16430 /*subr=*/subr
, explain_p
))
16433 /* Advance to the end of the list of parameters. */
16434 parms
= TREE_CHAIN (parms
);
16437 /* Fail if we've reached the end of the parm list, and more args
16438 are present, and the parm list isn't variadic. */
16439 if (ia
< nargs
&& parms
== void_list_node
)
16440 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16441 /* Fail if parms are left and they don't have default values. */
16442 if (parms
&& parms
!= void_list_node
16443 && TREE_PURPOSE (parms
) == NULL_TREE
)
16445 unsigned int count
= nargs
;
16447 while (p
&& p
!= void_list_node
)
16450 p
= TREE_CHAIN (p
);
16452 return unify_too_few_arguments (explain_p
, ia
, count
);
16457 tsubst_flags_t complain
= (explain_p
16458 ? tf_warning_or_error
16461 for (i
= 0; i
< ntparms
; i
++)
16463 tree targ
= TREE_VEC_ELT (targs
, i
);
16464 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16466 /* Clear the "incomplete" flags on all argument packs now so that
16467 substituting them into later default arguments works. */
16468 if (targ
&& ARGUMENT_PACK_P (targ
))
16470 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16471 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16474 if (targ
|| tparm
== error_mark_node
)
16476 tparm
= TREE_VALUE (tparm
);
16478 /* If this is an undeduced nontype parameter that depends on
16479 a type parameter, try another pass; its type may have been
16480 deduced from a later argument than the one from which
16481 this parameter can be deduced. */
16482 if (TREE_CODE (tparm
) == PARM_DECL
16483 && uses_template_parms (TREE_TYPE (tparm
))
16484 && !saw_undeduced
++)
16487 /* Core issue #226 (C++0x) [temp.deduct]:
16489 If a template argument has not been deduced, its
16490 default template argument, if any, is used.
16492 When we are in C++98 mode, TREE_PURPOSE will either
16493 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16494 to explicitly check cxx_dialect here. */
16495 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16497 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16498 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16499 reopen_deferring_access_checks (*checks
);
16500 location_t save_loc
= input_location
;
16502 input_location
= DECL_SOURCE_LOCATION (parm
);
16503 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16504 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16506 input_location
= save_loc
;
16507 *checks
= get_deferred_access_checks ();
16508 pop_deferring_access_checks ();
16509 if (arg
== error_mark_node
)
16513 TREE_VEC_ELT (targs
, i
) = arg
;
16514 /* The position of the first default template argument,
16515 is also the number of non-defaulted arguments in TARGS.
16517 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16518 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16523 /* If the type parameter is a parameter pack, then it will
16524 be deduced to an empty parameter pack. */
16525 if (template_parameter_pack_p (tparm
))
16529 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16531 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16532 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16533 TREE_CONSTANT (arg
) = 1;
16536 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16538 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16540 TREE_VEC_ELT (targs
, i
) = arg
;
16544 return unify_parameter_deduction_failure (explain_p
, tparm
);
16547 #ifdef ENABLE_CHECKING
16548 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16549 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16552 return unify_success (explain_p
);
16555 /* Subroutine of type_unification_real. Args are like the variables
16556 at the call site. ARG is an overloaded function (or template-id);
16557 we try deducing template args from each of the overloads, and if
16558 only one succeeds, we go with that. Modifies TARGS and returns
16559 true on success. */
16562 resolve_overloaded_unification (tree tparms
,
16566 unification_kind_t strict
,
16570 tree tempargs
= copy_node (targs
);
16572 tree goodfn
= NULL_TREE
;
16575 if (TREE_CODE (arg
) == ADDR_EXPR
)
16577 arg
= TREE_OPERAND (arg
, 0);
16583 if (TREE_CODE (arg
) == COMPONENT_REF
)
16584 /* Handle `&x' where `x' is some static or non-static member
16586 arg
= TREE_OPERAND (arg
, 1);
16588 if (TREE_CODE (arg
) == OFFSET_REF
)
16589 arg
= TREE_OPERAND (arg
, 1);
16591 /* Strip baselink information. */
16592 if (BASELINK_P (arg
))
16593 arg
= BASELINK_FUNCTIONS (arg
);
16595 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16597 /* If we got some explicit template args, we need to plug them into
16598 the affected templates before we try to unify, in case the
16599 explicit args will completely resolve the templates in question. */
16602 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16603 arg
= TREE_OPERAND (arg
, 0);
16605 for (; arg
; arg
= OVL_NEXT (arg
))
16607 tree fn
= OVL_CURRENT (arg
);
16608 tree subargs
, elem
;
16610 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16613 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16614 expl_subargs
, NULL_TREE
, tf_none
,
16615 /*require_all_args=*/true,
16616 /*use_default_args=*/true);
16617 if (subargs
!= error_mark_node
16618 && !any_dependent_template_arguments_p (subargs
))
16620 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
16621 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16622 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16623 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16632 /* If no templates (or more than one) are fully resolved by the
16633 explicit arguments, this template-id is a non-deduced context; it
16634 could still be OK if we deduce all template arguments for the
16635 enclosing call through other arguments. */
16639 else if (TREE_CODE (arg
) != OVERLOAD
16640 && TREE_CODE (arg
) != FUNCTION_DECL
)
16641 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16642 -- but the deduction does not succeed because the expression is
16643 not just the function on its own. */
16646 for (; arg
; arg
= OVL_NEXT (arg
))
16647 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16648 TREE_TYPE (OVL_CURRENT (arg
)),
16649 strict
, sub_strict
, addr_p
, explain_p
)
16650 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16652 goodfn
= OVL_CURRENT (arg
);
16656 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16657 to function or pointer to member function argument if the set of
16658 overloaded functions does not contain function templates and at most
16659 one of a set of overloaded functions provides a unique match.
16661 So if we found multiple possibilities, we return success but don't
16662 deduce anything. */
16666 int i
= TREE_VEC_LENGTH (targs
);
16668 if (TREE_VEC_ELT (tempargs
, i
))
16669 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16677 /* Core DR 115: In contexts where deduction is done and fails, or in
16678 contexts where deduction is not done, if a template argument list is
16679 specified and it, along with any default template arguments, identifies
16680 a single function template specialization, then the template-id is an
16681 lvalue for the function template specialization. */
16684 resolve_nondeduced_context (tree orig_expr
)
16686 tree expr
, offset
, baselink
;
16689 if (!type_unknown_p (orig_expr
))
16694 offset
= NULL_TREE
;
16695 baselink
= NULL_TREE
;
16697 if (TREE_CODE (expr
) == ADDR_EXPR
)
16699 expr
= TREE_OPERAND (expr
, 0);
16702 if (TREE_CODE (expr
) == OFFSET_REF
)
16705 expr
= TREE_OPERAND (expr
, 1);
16707 if (BASELINK_P (expr
))
16710 expr
= BASELINK_FUNCTIONS (expr
);
16713 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16716 tree goodfn
= NULL_TREE
;
16718 /* If we got some explicit template args, we need to plug them into
16719 the affected templates before we try to unify, in case the
16720 explicit args will completely resolve the templates in question. */
16722 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16723 tree arg
= TREE_OPERAND (expr
, 0);
16724 tree badfn
= NULL_TREE
;
16725 tree badargs
= NULL_TREE
;
16727 for (; arg
; arg
= OVL_NEXT (arg
))
16729 tree fn
= OVL_CURRENT (arg
);
16730 tree subargs
, elem
;
16732 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16735 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16736 expl_subargs
, NULL_TREE
, tf_none
,
16737 /*require_all_args=*/true,
16738 /*use_default_args=*/true);
16739 if (subargs
!= error_mark_node
16740 && !any_dependent_template_arguments_p (subargs
))
16742 elem
= instantiate_template (fn
, subargs
, tf_none
);
16743 if (elem
== error_mark_node
)
16748 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16757 mark_used (goodfn
);
16760 expr
= build_baselink (BASELINK_BINFO (baselink
),
16761 BASELINK_ACCESS_BINFO (baselink
),
16762 expr
, BASELINK_OPTYPE (baselink
));
16766 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16767 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16770 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16773 else if (good
== 0 && badargs
)
16774 /* There were no good options and at least one bad one, so let the
16775 user know what the problem is. */
16776 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16781 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16782 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16783 different overloads deduce different arguments for a given parm.
16784 ADDR_P is true if the expression for which deduction is being
16785 performed was of the form "& fn" rather than simply "fn".
16787 Returns 1 on success. */
16790 try_one_overload (tree tparms
,
16795 unification_kind_t strict
,
16804 if (arg
== error_mark_node
)
16807 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16808 to function or pointer to member function argument if the set of
16809 overloaded functions does not contain function templates and at most
16810 one of a set of overloaded functions provides a unique match.
16812 So if this is a template, just return success. */
16814 if (uses_template_parms (arg
))
16817 if (TREE_CODE (arg
) == METHOD_TYPE
)
16818 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16820 arg
= build_pointer_type (arg
);
16822 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16824 /* We don't copy orig_targs for this because if we have already deduced
16825 some template args from previous args, unify would complain when we
16826 try to deduce a template parameter for the same argument, even though
16827 there isn't really a conflict. */
16828 nargs
= TREE_VEC_LENGTH (targs
);
16829 tempargs
= make_tree_vec (nargs
);
16831 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16834 /* First make sure we didn't deduce anything that conflicts with
16835 explicitly specified args. */
16836 for (i
= nargs
; i
--; )
16838 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16839 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16843 else if (uses_template_parms (elt
))
16844 /* Since we're unifying against ourselves, we will fill in
16845 template args used in the function parm list with our own
16846 template parms. Discard them. */
16847 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16848 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16852 for (i
= nargs
; i
--; )
16854 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16857 TREE_VEC_ELT (targs
, i
) = elt
;
16863 /* PARM is a template class (perhaps with unbound template
16864 parameters). ARG is a fully instantiated type. If ARG can be
16865 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16866 TARGS are as for unify. */
16869 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16872 tree copy_of_targs
;
16874 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16875 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16876 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16879 /* We need to make a new template argument vector for the call to
16880 unify. If we used TARGS, we'd clutter it up with the result of
16881 the attempted unification, even if this class didn't work out.
16882 We also don't want to commit ourselves to all the unifications
16883 we've already done, since unification is supposed to be done on
16884 an argument-by-argument basis. In other words, consider the
16885 following pathological case:
16887 template <int I, int J, int K>
16890 template <int I, int J>
16891 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16893 template <int I, int J, int K>
16894 void f(S<I, J, K>, S<I, I, I>);
16903 Now, by the time we consider the unification involving `s2', we
16904 already know that we must have `f<0, 0, 0>'. But, even though
16905 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16906 because there are two ways to unify base classes of S<0, 1, 2>
16907 with S<I, I, I>. If we kept the already deduced knowledge, we
16908 would reject the possibility I=1. */
16909 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16911 /* If unification failed, we're done. */
16912 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16913 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16919 /* Given a template type PARM and a class type ARG, find the unique
16920 base type in ARG that is an instance of PARM. We do not examine
16921 ARG itself; only its base-classes. If there is not exactly one
16922 appropriate base class, return NULL_TREE. PARM may be the type of
16923 a partial specialization, as well as a plain template type. Used
16926 static enum template_base_result
16927 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16928 bool explain_p
, tree
*result
)
16930 tree rval
= NULL_TREE
;
16933 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16935 binfo
= TYPE_BINFO (complete_type (arg
));
16938 /* The type could not be completed. */
16939 *result
= NULL_TREE
;
16940 return tbr_incomplete_type
;
16943 /* Walk in inheritance graph order. The search order is not
16944 important, and this avoids multiple walks of virtual bases. */
16945 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16947 tree r
= try_class_unification (tparms
, targs
, parm
,
16948 BINFO_TYPE (binfo
), explain_p
);
16952 /* If there is more than one satisfactory baseclass, then:
16956 If they yield more than one possible deduced A, the type
16960 if (rval
&& !same_type_p (r
, rval
))
16962 *result
= NULL_TREE
;
16963 return tbr_ambiguous_baseclass
;
16971 return tbr_success
;
16974 /* Returns the level of DECL, which declares a template parameter. */
16977 template_decl_level (tree decl
)
16979 switch (TREE_CODE (decl
))
16982 case TEMPLATE_DECL
:
16983 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16986 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16989 gcc_unreachable ();
16994 /* Decide whether ARG can be unified with PARM, considering only the
16995 cv-qualifiers of each type, given STRICT as documented for unify.
16996 Returns nonzero iff the unification is OK on that basis. */
16999 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
17001 int arg_quals
= cp_type_quals (arg
);
17002 int parm_quals
= cp_type_quals (parm
);
17004 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17005 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17007 /* Although a CVR qualifier is ignored when being applied to a
17008 substituted template parameter ([8.3.2]/1 for example), that
17009 does not allow us to unify "const T" with "int&" because both
17010 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17011 It is ok when we're allowing additional CV qualifiers
17012 at the outer level [14.8.2.1]/3,1st bullet. */
17013 if ((TREE_CODE (arg
) == REFERENCE_TYPE
17014 || TREE_CODE (arg
) == FUNCTION_TYPE
17015 || TREE_CODE (arg
) == METHOD_TYPE
)
17016 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
17019 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
17020 && (parm_quals
& TYPE_QUAL_RESTRICT
))
17024 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17025 && (arg_quals
& parm_quals
) != parm_quals
)
17028 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
17029 && (parm_quals
& arg_quals
) != arg_quals
)
17035 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17037 template_parm_level_and_index (tree parm
, int* level
, int* index
)
17039 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17040 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17041 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17043 *index
= TEMPLATE_TYPE_IDX (parm
);
17044 *level
= TEMPLATE_TYPE_LEVEL (parm
);
17048 *index
= TEMPLATE_PARM_IDX (parm
);
17049 *level
= TEMPLATE_PARM_LEVEL (parm
);
17053 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17055 if (unify (TP, TA, P, A, S, EP)) \
17059 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17060 expansion at the end of PACKED_PARMS. Returns 0 if the type
17061 deduction succeeds, 1 otherwise. STRICT is the same as in
17062 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17063 call argument list. We'll need to adjust the arguments to make them
17064 types. SUBR tells us if this is from a recursive call to
17065 type_unification_real, or for comparing two template argument
17069 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
17070 tree packed_args
, unification_kind_t strict
,
17071 bool subr
, bool explain_p
)
17074 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
17075 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
17076 tree pack
, packs
= NULL_TREE
;
17077 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
17079 packed_args
= expand_template_argument_pack (packed_args
);
17081 int len
= TREE_VEC_LENGTH (packed_args
);
17083 /* Determine the parameter packs we will be deducing from the
17084 pattern, and record their current deductions. */
17085 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
17086 pack
; pack
= TREE_CHAIN (pack
))
17088 tree parm_pack
= TREE_VALUE (pack
);
17091 /* Determine the index and level of this parameter pack. */
17092 template_parm_level_and_index (parm_pack
, &level
, &idx
);
17094 /* Keep track of the parameter packs and their corresponding
17096 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
17097 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
17100 /* Loop through all of the arguments that have not yet been
17101 unified and unify each with the pattern. */
17102 for (i
= start
; i
< len
; i
++)
17105 bool any_explicit
= false;
17106 tree arg
= TREE_VEC_ELT (packed_args
, i
);
17108 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17109 or the element of its argument pack at the current index if
17110 this argument was explicitly specified. */
17111 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17115 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17118 if (TREE_VALUE (pack
)
17119 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
17120 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
17122 any_explicit
= true;
17123 arg
= TREE_VEC_ELT (pargs
, i
- start
);
17125 TMPL_ARG (targs
, level
, idx
) = arg
;
17128 /* If we had explicit template arguments, substitute them into the
17129 pattern before deduction. */
17132 /* Some arguments might still be unspecified or dependent. */
17134 ++processing_template_decl
;
17135 dependent
= any_dependent_template_arguments_p (targs
);
17137 --processing_template_decl
;
17138 parm
= tsubst (pattern
, targs
,
17139 explain_p
? tf_warning_or_error
: tf_none
,
17142 --processing_template_decl
;
17143 if (parm
== error_mark_node
)
17149 /* Unify the pattern with the current argument. */
17150 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17151 LOOKUP_IMPLICIT
, explain_p
))
17154 /* For each parameter pack, collect the deduced value. */
17155 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17158 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17160 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
17161 TMPL_ARG (targs
, level
, idx
);
17165 /* Verify that the results of unification with the parameter packs
17166 produce results consistent with what we've seen before, and make
17167 the deduced argument packs available. */
17168 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17170 tree old_pack
= TREE_VALUE (pack
);
17171 tree new_args
= TREE_TYPE (pack
);
17172 int i
, len
= TREE_VEC_LENGTH (new_args
);
17174 bool nondeduced_p
= false;
17176 /* By default keep the original deduced argument pack.
17177 If necessary, more specific code is going to update the
17178 resulting deduced argument later down in this function. */
17179 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17180 TMPL_ARG (targs
, level
, idx
) = old_pack
;
17182 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17183 actually deduce anything. */
17184 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
17185 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
17186 nondeduced_p
= true;
17190 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
17192 /* If we had fewer function args than explicit template args,
17193 just use the explicits. */
17194 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17195 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
17196 if (len
< explicit_len
)
17197 new_args
= explicit_args
;
17203 /* Build the deduced *_ARGUMENT_PACK. */
17204 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17206 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17207 TREE_TYPE (result
) =
17208 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17209 TREE_CONSTANT (result
) = 1;
17212 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17214 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17216 /* Note the deduced argument packs for this parameter
17218 TMPL_ARG (targs
, level
, idx
) = result
;
17220 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17221 && (ARGUMENT_PACK_ARGS (old_pack
)
17222 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17224 /* We only had the explicitly-provided arguments before, but
17225 now we have a complete set of arguments. */
17226 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17228 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17229 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17230 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17234 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17235 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17237 if (!comp_template_args_with_info (old_args
, new_args
,
17238 &bad_old_arg
, &bad_new_arg
))
17239 /* Inconsistent unification of this parameter pack. */
17240 return unify_parameter_pack_inconsistent (explain_p
,
17246 return unify_success (explain_p
);
17249 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17250 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17251 parameters and return value are as for unify. */
17254 unify_array_domain (tree tparms
, tree targs
,
17255 tree parm_dom
, tree arg_dom
,
17263 /* Our representation of array types uses "N - 1" as the
17264 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17265 not an integer constant. We cannot unify arbitrarily
17266 complex expressions, so we eliminate the MINUS_EXPRs
17268 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17269 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17272 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17273 parm_max
= TREE_OPERAND (parm_max
, 0);
17275 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17276 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17279 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17280 trying to unify the type of a variable with the type
17281 of a template parameter. For example:
17283 template <unsigned int N>
17284 void f (char (&) [N]);
17291 Here, the type of the ARG will be "int [g(i)]", and
17292 may be a SAVE_EXPR, etc. */
17293 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17294 return unify_vla_arg (explain_p
, arg_dom
);
17295 arg_max
= TREE_OPERAND (arg_max
, 0);
17298 /* If only one of the bounds used a MINUS_EXPR, compensate
17299 by adding one to the other bound. */
17300 if (parm_cst
&& !arg_cst
)
17301 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17305 else if (arg_cst
&& !parm_cst
)
17306 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17311 return unify (tparms
, targs
, parm_max
, arg_max
,
17312 UNIFY_ALLOW_INTEGER
, explain_p
);
17315 /* Deduce the value of template parameters. TPARMS is the (innermost)
17316 set of template parameters to a template. TARGS is the bindings
17317 for those template parameters, as determined thus far; TARGS may
17318 include template arguments for outer levels of template parameters
17319 as well. PARM is a parameter to a template function, or a
17320 subcomponent of that parameter; ARG is the corresponding argument.
17321 This function attempts to match PARM with ARG in a manner
17322 consistent with the existing assignments in TARGS. If more values
17323 are deduced, then TARGS is updated.
17325 Returns 0 if the type deduction succeeds, 1 otherwise. The
17326 parameter STRICT is a bitwise or of the following flags:
17329 Require an exact match between PARM and ARG.
17330 UNIFY_ALLOW_MORE_CV_QUAL:
17331 Allow the deduced ARG to be more cv-qualified (by qualification
17332 conversion) than ARG.
17333 UNIFY_ALLOW_LESS_CV_QUAL:
17334 Allow the deduced ARG to be less cv-qualified than ARG.
17335 UNIFY_ALLOW_DERIVED:
17336 Allow the deduced ARG to be a template base class of ARG,
17337 or a pointer to a template base class of the type pointed to by
17339 UNIFY_ALLOW_INTEGER:
17340 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17341 case for more information.
17342 UNIFY_ALLOW_OUTER_LEVEL:
17343 This is the outermost level of a deduction. Used to determine validity
17344 of qualification conversions. A valid qualification conversion must
17345 have const qualified pointers leading up to the inner type which
17346 requires additional CV quals, except at the outer level, where const
17347 is not required [conv.qual]. It would be normal to set this flag in
17348 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17349 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17350 This is the outermost level of a deduction, and PARM can be more CV
17351 qualified at this point.
17352 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17353 This is the outermost level of a deduction, and PARM can be less CV
17354 qualified at this point. */
17357 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17363 int strict_in
= strict
;
17365 /* I don't think this will do the right thing with respect to types.
17366 But the only case I've seen it in so far has been array bounds, where
17367 signedness is the only information lost, and I think that will be
17369 while (TREE_CODE (parm
) == NOP_EXPR
)
17370 parm
= TREE_OPERAND (parm
, 0);
17372 if (arg
== error_mark_node
)
17373 return unify_invalid (explain_p
);
17374 if (arg
== unknown_type_node
17375 || arg
== init_list_type_node
)
17376 /* We can't deduce anything from this, but we might get all the
17377 template args from other function args. */
17378 return unify_success (explain_p
);
17380 /* If PARM uses template parameters, then we can't bail out here,
17381 even if ARG == PARM, since we won't record unifications for the
17382 template parameters. We might need them if we're trying to
17383 figure out which of two things is more specialized. */
17384 if (arg
== parm
&& !uses_template_parms (parm
))
17385 return unify_success (explain_p
);
17387 /* Handle init lists early, so the rest of the function can assume
17388 we're dealing with a type. */
17389 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17393 tree orig_parm
= parm
;
17395 /* Replace T with std::initializer_list<T> for deduction. */
17396 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17397 && flag_deduce_init_list
)
17398 parm
= listify (parm
);
17400 if (!is_std_init_list (parm
)
17401 && TREE_CODE (parm
) != ARRAY_TYPE
)
17402 /* We can only deduce from an initializer list argument if the
17403 parameter is std::initializer_list or an array; otherwise this
17404 is a non-deduced context. */
17405 return unify_success (explain_p
);
17407 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17408 elttype
= TREE_TYPE (parm
);
17410 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17412 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17414 int elt_strict
= strict
;
17416 if (elt
== error_mark_node
)
17417 return unify_invalid (explain_p
);
17419 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17421 tree type
= TREE_TYPE (elt
);
17422 /* It should only be possible to get here for a call. */
17423 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17424 elt_strict
|= maybe_adjust_types_for_deduction
17425 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17429 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17433 if (TREE_CODE (parm
) == ARRAY_TYPE
17434 && deducible_array_bound (TYPE_DOMAIN (parm
)))
17436 /* Also deduce from the length of the initializer list. */
17437 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17438 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17439 if (idx
== error_mark_node
)
17440 return unify_invalid (explain_p
);
17441 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17445 /* If the std::initializer_list<T> deduction worked, replace the
17446 deduced A with std::initializer_list<A>. */
17447 if (orig_parm
!= parm
)
17449 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17450 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17451 targ
= listify (targ
);
17452 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17454 return unify_success (explain_p
);
17457 /* Immediately reject some pairs that won't unify because of
17458 cv-qualification mismatches. */
17459 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17461 /* It is the elements of the array which hold the cv quals of an array
17462 type, and the elements might be template type parms. We'll check
17463 when we recurse. */
17464 && TREE_CODE (arg
) != ARRAY_TYPE
17465 /* We check the cv-qualifiers when unifying with template type
17466 parameters below. We want to allow ARG `const T' to unify with
17467 PARM `T' for example, when computing which of two templates
17468 is more specialized, for example. */
17469 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17470 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17471 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17473 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17474 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17475 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17476 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17477 strict
&= ~UNIFY_ALLOW_DERIVED
;
17478 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17479 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17481 switch (TREE_CODE (parm
))
17483 case TYPENAME_TYPE
:
17485 case UNBOUND_CLASS_TEMPLATE
:
17486 /* In a type which contains a nested-name-specifier, template
17487 argument values cannot be deduced for template parameters used
17488 within the nested-name-specifier. */
17489 return unify_success (explain_p
);
17491 case TEMPLATE_TYPE_PARM
:
17492 case TEMPLATE_TEMPLATE_PARM
:
17493 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17494 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17495 if (tparm
== error_mark_node
)
17496 return unify_invalid (explain_p
);
17498 if (TEMPLATE_TYPE_LEVEL (parm
)
17499 != template_decl_level (tparm
))
17500 /* The PARM is not one we're trying to unify. Just check
17501 to see if it matches ARG. */
17503 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17504 && (is_auto (parm
) ? is_auto (arg
)
17505 : same_type_p (parm
, arg
)))
17506 return unify_success (explain_p
);
17508 return unify_type_mismatch (explain_p
, parm
, arg
);
17510 idx
= TEMPLATE_TYPE_IDX (parm
);
17511 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17512 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17513 if (tparm
== error_mark_node
)
17514 return unify_invalid (explain_p
);
17516 /* Check for mixed types and values. */
17517 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17518 && TREE_CODE (tparm
) != TYPE_DECL
)
17519 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17520 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17521 gcc_unreachable ();
17523 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17525 /* ARG must be constructed from a template class or a template
17526 template parameter. */
17527 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17528 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17529 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17531 tree parmvec
= TYPE_TI_ARGS (parm
);
17532 /* An alias template name is never deduced. */
17533 if (TYPE_ALIAS_P (arg
))
17534 arg
= strip_typedefs (arg
);
17535 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17536 tree full_argvec
= add_to_template_args (targs
, argvec
);
17538 = DECL_INNERMOST_TEMPLATE_PARMS
17539 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17541 int parm_variadic_p
= 0;
17543 /* The resolution to DR150 makes clear that default
17544 arguments for an N-argument may not be used to bind T
17545 to a template template parameter with fewer than N
17546 parameters. It is not safe to permit the binding of
17547 default arguments as an extension, as that may change
17548 the meaning of a conforming program. Consider:
17550 struct Dense { static const unsigned int dim = 1; };
17552 template <template <typename> class View,
17554 void operator+(float, View<Block> const&);
17556 template <typename Block,
17557 unsigned int Dim = Block::dim>
17558 struct Lvalue_proxy { operator float() const; };
17562 Lvalue_proxy<Dense> p;
17567 Here, if Lvalue_proxy is permitted to bind to View, then
17568 the global operator+ will be used; if they are not, the
17569 Lvalue_proxy will be converted to float. */
17570 if (coerce_template_parms (parm_parms
,
17572 TYPE_TI_TEMPLATE (parm
),
17574 ? tf_warning_or_error
17576 /*require_all_args=*/true,
17577 /*use_default_args=*/false)
17578 == error_mark_node
)
17581 /* Deduce arguments T, i from TT<T> or TT<i>.
17582 We check each element of PARMVEC and ARGVEC individually
17583 rather than the whole TREE_VEC since they can have
17584 different number of elements. */
17586 parmvec
= expand_template_argument_pack (parmvec
);
17587 argvec
= expand_template_argument_pack (argvec
);
17589 len
= TREE_VEC_LENGTH (parmvec
);
17591 /* Check if the parameters end in a pack, making them
17594 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17595 parm_variadic_p
= 1;
17597 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17598 /* If the template argument list of P contains a pack
17599 expansion that is not the last template argument, the
17600 entire template argument list is a non-deduced
17602 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17603 return unify_success (explain_p
);
17605 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17606 return unify_too_few_arguments (explain_p
,
17607 TREE_VEC_LENGTH (argvec
), len
);
17609 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17611 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17612 TREE_VEC_ELT (parmvec
, i
),
17613 TREE_VEC_ELT (argvec
, i
),
17614 UNIFY_ALLOW_NONE
, explain_p
);
17617 if (parm_variadic_p
17618 && unify_pack_expansion (tparms
, targs
,
17621 /*subr=*/true, explain_p
))
17624 arg
= TYPE_TI_TEMPLATE (arg
);
17626 /* Fall through to deduce template name. */
17629 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17630 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17632 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17634 /* Simple cases: Value already set, does match or doesn't. */
17635 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17636 return unify_success (explain_p
);
17638 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17642 /* If PARM is `const T' and ARG is only `int', we don't have
17643 a match unless we are allowing additional qualification.
17644 If ARG is `const int' and PARM is just `T' that's OK;
17645 that binds `const int' to `T'. */
17646 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17648 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17650 /* Consider the case where ARG is `const volatile int' and
17651 PARM is `const T'. Then, T should be `volatile int'. */
17652 arg
= cp_build_qualified_type_real
17653 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17654 if (arg
== error_mark_node
)
17655 return unify_invalid (explain_p
);
17657 /* Simple cases: Value already set, does match or doesn't. */
17658 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17659 return unify_success (explain_p
);
17661 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17663 /* Make sure that ARG is not a variable-sized array. (Note
17664 that were talking about variable-sized arrays (like
17665 `int[n]'), rather than arrays of unknown size (like
17666 `int[]').) We'll get very confused by such a type since
17667 the bound of the array is not constant, and therefore
17668 not mangleable. Besides, such types are not allowed in
17669 ISO C++, so we can do as we please here. We do allow
17670 them for 'auto' deduction, since that isn't ABI-exposed. */
17671 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17672 return unify_vla_arg (explain_p
, arg
);
17674 /* Strip typedefs as in convert_template_argument. */
17675 arg
= canonicalize_type_argument (arg
, tf_none
);
17678 /* If ARG is a parameter pack or an expansion, we cannot unify
17679 against it unless PARM is also a parameter pack. */
17680 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17681 && !template_parameter_pack_p (parm
))
17682 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17684 /* If the argument deduction results is a METHOD_TYPE,
17685 then there is a problem.
17686 METHOD_TYPE doesn't map to any real C++ type the result of
17687 the deduction can not be of that type. */
17688 if (TREE_CODE (arg
) == METHOD_TYPE
)
17689 return unify_method_type_error (explain_p
, arg
);
17691 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17692 return unify_success (explain_p
);
17694 case TEMPLATE_PARM_INDEX
:
17695 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17696 if (tparm
== error_mark_node
)
17697 return unify_invalid (explain_p
);
17699 if (TEMPLATE_PARM_LEVEL (parm
)
17700 != template_decl_level (tparm
))
17702 /* The PARM is not one we're trying to unify. Just check
17703 to see if it matches ARG. */
17704 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17705 && cp_tree_equal (parm
, arg
));
17707 unify_expression_unequal (explain_p
, parm
, arg
);
17711 idx
= TEMPLATE_PARM_IDX (parm
);
17712 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17716 int x
= !cp_tree_equal (targ
, arg
);
17718 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17722 /* [temp.deduct.type] If, in the declaration of a function template
17723 with a non-type template-parameter, the non-type
17724 template-parameter is used in an expression in the function
17725 parameter-list and, if the corresponding template-argument is
17726 deduced, the template-argument type shall match the type of the
17727 template-parameter exactly, except that a template-argument
17728 deduced from an array bound may be of any integral type.
17729 The non-type parameter might use already deduced type parameters. */
17730 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17731 if (!TREE_TYPE (arg
))
17732 /* Template-parameter dependent expression. Just accept it for now.
17733 It will later be processed in convert_template_argument. */
17735 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17737 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17738 && CP_INTEGRAL_TYPE_P (tparm
))
17739 /* Convert the ARG to the type of PARM; the deduced non-type
17740 template argument must exactly match the types of the
17741 corresponding parameter. */
17742 arg
= fold (build_nop (tparm
, arg
));
17743 else if (uses_template_parms (tparm
))
17744 /* We haven't deduced the type of this parameter yet. Try again
17746 return unify_success (explain_p
);
17748 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17750 /* If ARG is a parameter pack or an expansion, we cannot unify
17751 against it unless PARM is also a parameter pack. */
17752 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17753 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17754 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17756 arg
= strip_typedefs_expr (arg
);
17757 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17758 return unify_success (explain_p
);
17762 /* A pointer-to-member constant can be unified only with
17763 another constant. */
17764 if (TREE_CODE (arg
) != PTRMEM_CST
)
17765 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17767 /* Just unify the class member. It would be useless (and possibly
17768 wrong, depending on the strict flags) to unify also
17769 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17770 arg refer to the same variable, even if through different
17771 classes. For instance:
17773 struct A { int x; };
17776 Unification of &A::x and &B::x must succeed. */
17777 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17778 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17783 if (!TYPE_PTR_P (arg
))
17784 return unify_type_mismatch (explain_p
, parm
, arg
);
17786 /* [temp.deduct.call]
17788 A can be another pointer or pointer to member type that can
17789 be converted to the deduced A via a qualification
17790 conversion (_conv.qual_).
17792 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17793 This will allow for additional cv-qualification of the
17794 pointed-to types if appropriate. */
17796 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17797 /* The derived-to-base conversion only persists through one
17798 level of pointers. */
17799 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17801 return unify (tparms
, targs
, TREE_TYPE (parm
),
17802 TREE_TYPE (arg
), strict
, explain_p
);
17805 case REFERENCE_TYPE
:
17806 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17807 return unify_type_mismatch (explain_p
, parm
, arg
);
17808 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17809 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17812 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17813 return unify_type_mismatch (explain_p
, parm
, arg
);
17814 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17815 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17816 return unify_type_mismatch (explain_p
, parm
, arg
);
17817 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17818 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17819 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17820 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17821 TYPE_DOMAIN (arg
), explain_p
);
17822 return unify_success (explain_p
);
17829 case ENUMERAL_TYPE
:
17832 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17833 return unify_type_mismatch (explain_p
, parm
, arg
);
17835 /* We have already checked cv-qualification at the top of the
17837 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17838 return unify_type_mismatch (explain_p
, parm
, arg
);
17840 /* As far as unification is concerned, this wins. Later checks
17841 will invalidate it if necessary. */
17842 return unify_success (explain_p
);
17844 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17845 /* Type INTEGER_CST can come from ordinary constant template args. */
17847 while (TREE_CODE (arg
) == NOP_EXPR
)
17848 arg
= TREE_OPERAND (arg
, 0);
17850 if (TREE_CODE (arg
) != INTEGER_CST
)
17851 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17852 return (tree_int_cst_equal (parm
, arg
)
17853 ? unify_success (explain_p
)
17854 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17858 int i
, len
, argslen
;
17859 int parm_variadic_p
= 0;
17861 if (TREE_CODE (arg
) != TREE_VEC
)
17862 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17864 len
= TREE_VEC_LENGTH (parm
);
17865 argslen
= TREE_VEC_LENGTH (arg
);
17867 /* Check for pack expansions in the parameters. */
17868 for (i
= 0; i
< len
; ++i
)
17870 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17873 /* We can unify against something with a trailing
17875 parm_variadic_p
= 1;
17877 /* [temp.deduct.type]/9: If the template argument list of
17878 P contains a pack expansion that is not the last
17879 template argument, the entire template argument list
17880 is a non-deduced context. */
17881 return unify_success (explain_p
);
17885 /* If we don't have enough arguments to satisfy the parameters
17886 (not counting the pack expression at the end), or we have
17887 too many arguments for a parameter list that doesn't end in
17888 a pack expression, we can't unify. */
17889 if (parm_variadic_p
17890 ? argslen
< len
- parm_variadic_p
17892 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17894 /* Unify all of the parameters that precede the (optional)
17895 pack expression. */
17896 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17898 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17899 TREE_VEC_ELT (parm
, i
),
17900 TREE_VEC_ELT (arg
, i
),
17901 UNIFY_ALLOW_NONE
, explain_p
);
17903 if (parm_variadic_p
)
17904 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17906 /*subr=*/true, explain_p
);
17907 return unify_success (explain_p
);
17912 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17913 return unify_type_mismatch (explain_p
, parm
, arg
);
17915 if (TYPE_PTRMEMFUNC_P (parm
))
17917 if (!TYPE_PTRMEMFUNC_P (arg
))
17918 return unify_type_mismatch (explain_p
, parm
, arg
);
17920 return unify (tparms
, targs
,
17921 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17922 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17923 strict
, explain_p
);
17926 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17928 tree t
= NULL_TREE
;
17930 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17932 /* First, we try to unify the PARM and ARG directly. */
17933 t
= try_class_unification (tparms
, targs
,
17934 parm
, arg
, explain_p
);
17938 /* Fallback to the special case allowed in
17939 [temp.deduct.call]:
17941 If P is a class, and P has the form
17942 template-id, then A can be a derived class of
17943 the deduced A. Likewise, if P is a pointer to
17944 a class of the form template-id, A can be a
17945 pointer to a derived class pointed to by the
17947 enum template_base_result r
;
17948 r
= get_template_base (tparms
, targs
, parm
, arg
,
17952 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17955 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17956 && (CLASSTYPE_TI_TEMPLATE (parm
)
17957 == CLASSTYPE_TI_TEMPLATE (arg
)))
17958 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17959 Then, we should unify `int' and `U'. */
17962 /* There's no chance of unification succeeding. */
17963 return unify_type_mismatch (explain_p
, parm
, arg
);
17965 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17966 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17968 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17969 return unify_type_mismatch (explain_p
, parm
, arg
);
17970 return unify_success (explain_p
);
17973 case FUNCTION_TYPE
:
17975 unsigned int nargs
;
17980 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17981 return unify_type_mismatch (explain_p
, parm
, arg
);
17983 /* CV qualifications for methods can never be deduced, they must
17984 match exactly. We need to check them explicitly here,
17985 because type_unification_real treats them as any other
17986 cv-qualified parameter. */
17987 if (TREE_CODE (parm
) == METHOD_TYPE
17988 && (!check_cv_quals_for_unify
17990 class_of_this_parm (arg
),
17991 class_of_this_parm (parm
))))
17992 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17994 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17995 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17997 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17998 args
= XALLOCAVEC (tree
, nargs
);
17999 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
18000 a
!= NULL_TREE
&& a
!= void_list_node
;
18001 a
= TREE_CHAIN (a
), ++i
)
18002 args
[i
] = TREE_VALUE (a
);
18005 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
18006 args
, nargs
, 1, DEDUCE_EXACT
,
18007 LOOKUP_NORMAL
, NULL
, explain_p
);
18011 /* Unify a pointer to member with a pointer to member function, which
18012 deduces the type of the member as a function type. */
18013 if (TYPE_PTRMEMFUNC_P (arg
))
18015 /* Check top-level cv qualifiers */
18016 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
18017 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18019 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18020 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
18021 UNIFY_ALLOW_NONE
, explain_p
);
18023 /* Determine the type of the function we are unifying against. */
18024 tree fntype
= static_fn_type (arg
);
18026 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
18029 if (TREE_CODE (arg
) != OFFSET_TYPE
)
18030 return unify_type_mismatch (explain_p
, parm
, arg
);
18031 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18032 TYPE_OFFSET_BASETYPE (arg
),
18033 UNIFY_ALLOW_NONE
, explain_p
);
18034 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18035 strict
, explain_p
);
18038 if (DECL_TEMPLATE_PARM_P (parm
))
18039 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
18040 if (arg
!= integral_constant_value (parm
))
18041 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18042 return unify_success (explain_p
);
18045 case TEMPLATE_DECL
:
18046 /* Matched cases are handled by the ARG == PARM test above. */
18047 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18050 /* A non-type template parameter that is a variable should be a
18051 an integral constant, in which case, it whould have been
18052 folded into its (constant) value. So we should not be getting
18053 a variable here. */
18054 gcc_unreachable ();
18056 case TYPE_ARGUMENT_PACK
:
18057 case NONTYPE_ARGUMENT_PACK
:
18058 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
18059 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
18062 case DECLTYPE_TYPE
:
18063 case UNDERLYING_TYPE
:
18064 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18065 or UNDERLYING_TYPE nodes. */
18066 return unify_success (explain_p
);
18069 /* Unification fails if we hit an error node. */
18070 return unify_invalid (explain_p
);
18073 if (REFERENCE_REF_P (parm
))
18074 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
18075 strict
, explain_p
);
18079 /* An unresolved overload is a nondeduced context. */
18080 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
18081 return unify_success (explain_p
);
18082 gcc_assert (EXPR_P (parm
));
18084 /* We must be looking at an expression. This can happen with
18088 void foo(S<I>, S<I + 2>);
18090 This is a "nondeduced context":
18094 The nondeduced contexts are:
18096 --A type that is a template-id in which one or more of
18097 the template-arguments is an expression that references
18098 a template-parameter.
18100 In these cases, we assume deduction succeeded, but don't
18101 actually infer any unifications. */
18103 if (!uses_template_parms (parm
)
18104 && !template_args_equal (parm
, arg
))
18105 return unify_expression_unequal (explain_p
, parm
, arg
);
18107 return unify_success (explain_p
);
18110 #undef RECUR_AND_CHECK_FAILURE
18112 /* Note that DECL can be defined in this translation unit, if
18116 mark_definable (tree decl
)
18119 DECL_NOT_REALLY_EXTERN (decl
) = 1;
18120 FOR_EACH_CLONE (clone
, decl
)
18121 DECL_NOT_REALLY_EXTERN (clone
) = 1;
18124 /* Called if RESULT is explicitly instantiated, or is a member of an
18125 explicitly instantiated class. */
18128 mark_decl_instantiated (tree result
, int extern_p
)
18130 SET_DECL_EXPLICIT_INSTANTIATION (result
);
18132 /* If this entity has already been written out, it's too late to
18133 make any modifications. */
18134 if (TREE_ASM_WRITTEN (result
))
18137 /* For anonymous namespace we don't need to do anything. */
18138 if (decl_anon_ns_mem_p (result
))
18140 gcc_assert (!TREE_PUBLIC (result
));
18144 if (TREE_CODE (result
) != FUNCTION_DECL
)
18145 /* The TREE_PUBLIC flag for function declarations will have been
18146 set correctly by tsubst. */
18147 TREE_PUBLIC (result
) = 1;
18149 /* This might have been set by an earlier implicit instantiation. */
18150 DECL_COMDAT (result
) = 0;
18153 DECL_NOT_REALLY_EXTERN (result
) = 0;
18156 mark_definable (result
);
18157 mark_needed (result
);
18158 /* Always make artificials weak. */
18159 if (DECL_ARTIFICIAL (result
) && flag_weak
)
18160 comdat_linkage (result
);
18161 /* For WIN32 we also want to put explicit instantiations in
18162 linkonce sections. */
18163 else if (TREE_PUBLIC (result
))
18164 maybe_make_one_only (result
);
18167 /* If EXTERN_P, then this function will not be emitted -- unless
18168 followed by an explicit instantiation, at which point its linkage
18169 will be adjusted. If !EXTERN_P, then this function will be
18170 emitted here. In neither circumstance do we want
18171 import_export_decl to adjust the linkage. */
18172 DECL_INTERFACE_KNOWN (result
) = 1;
18175 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18176 important template arguments. If any are missing, we check whether
18177 they're important by using error_mark_node for substituting into any
18178 args that were used for partial ordering (the ones between ARGS and END)
18179 and seeing if it bubbles up. */
18182 check_undeduced_parms (tree targs
, tree args
, tree end
)
18184 bool found
= false;
18186 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
18187 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
18190 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
18194 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
18195 if (substed
== error_mark_node
)
18201 /* Given two function templates PAT1 and PAT2, return:
18203 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18204 -1 if PAT2 is more specialized than PAT1.
18205 0 if neither is more specialized.
18207 LEN indicates the number of parameters we should consider
18208 (defaulted parameters should not be considered).
18210 The 1998 std underspecified function template partial ordering, and
18211 DR214 addresses the issue. We take pairs of arguments, one from
18212 each of the templates, and deduce them against each other. One of
18213 the templates will be more specialized if all the *other*
18214 template's arguments deduce against its arguments and at least one
18215 of its arguments *does* *not* deduce against the other template's
18216 corresponding argument. Deduction is done as for class templates.
18217 The arguments used in deduction have reference and top level cv
18218 qualifiers removed. Iff both arguments were originally reference
18219 types *and* deduction succeeds in both directions, an lvalue reference
18220 wins against an rvalue reference and otherwise the template
18221 with the more cv-qualified argument wins for that pairing (if
18222 neither is more cv-qualified, they both are equal). Unlike regular
18223 deduction, after all the arguments have been deduced in this way,
18224 we do *not* verify the deduced template argument values can be
18225 substituted into non-deduced contexts.
18227 The logic can be a bit confusing here, because we look at deduce1 and
18228 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18229 can find template arguments for pat1 to make arg1 look like arg2, that
18230 means that arg2 is at least as specialized as arg1. */
18233 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18235 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18236 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18237 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18238 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18239 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18240 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18241 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18242 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18243 tree origs1
, origs2
;
18244 bool lose1
= false;
18245 bool lose2
= false;
18247 /* Remove the this parameter from non-static member functions. If
18248 one is a non-static member function and the other is not a static
18249 member function, remove the first parameter from that function
18250 also. This situation occurs for operator functions where we
18251 locate both a member function (with this pointer) and non-member
18252 operator (with explicit first operand). */
18253 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18255 len
--; /* LEN is the number of significant arguments for DECL1 */
18256 args1
= TREE_CHAIN (args1
);
18257 if (!DECL_STATIC_FUNCTION_P (decl2
))
18258 args2
= TREE_CHAIN (args2
);
18260 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18262 args2
= TREE_CHAIN (args2
);
18263 if (!DECL_STATIC_FUNCTION_P (decl1
))
18266 args1
= TREE_CHAIN (args1
);
18270 /* If only one is a conversion operator, they are unordered. */
18271 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18274 /* Consider the return type for a conversion function */
18275 if (DECL_CONV_FN_P (decl1
))
18277 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18278 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18282 processing_template_decl
++;
18288 /* Stop when an ellipsis is seen. */
18289 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18291 tree arg1
= TREE_VALUE (args1
);
18292 tree arg2
= TREE_VALUE (args2
);
18293 int deduce1
, deduce2
;
18299 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18300 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18302 /* When both arguments are pack expansions, we need only
18303 unify the patterns themselves. */
18304 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18305 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18307 /* This is the last comparison we need to do. */
18311 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18313 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18314 arg1
= TREE_TYPE (arg1
);
18315 quals1
= cp_type_quals (arg1
);
18318 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18320 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18321 arg2
= TREE_TYPE (arg2
);
18322 quals2
= cp_type_quals (arg2
);
18325 arg1
= TYPE_MAIN_VARIANT (arg1
);
18326 arg2
= TYPE_MAIN_VARIANT (arg2
);
18328 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18330 int i
, len2
= list_length (args2
);
18331 tree parmvec
= make_tree_vec (1);
18332 tree argvec
= make_tree_vec (len2
);
18335 /* Setup the parameter vector, which contains only ARG1. */
18336 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18338 /* Setup the argument vector, which contains the remaining
18340 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18341 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18343 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18344 argvec
, DEDUCE_EXACT
,
18345 /*subr=*/true, /*explain_p=*/false)
18348 /* We cannot deduce in the other direction, because ARG1 is
18349 a pack expansion but ARG2 is not. */
18352 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18354 int i
, len1
= list_length (args1
);
18355 tree parmvec
= make_tree_vec (1);
18356 tree argvec
= make_tree_vec (len1
);
18359 /* Setup the parameter vector, which contains only ARG1. */
18360 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18362 /* Setup the argument vector, which contains the remaining
18364 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18365 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18367 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18368 argvec
, DEDUCE_EXACT
,
18369 /*subr=*/true, /*explain_p=*/false)
18372 /* We cannot deduce in the other direction, because ARG2 is
18373 a pack expansion but ARG1 is not.*/
18379 /* The normal case, where neither argument is a pack
18381 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18382 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18384 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18385 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18389 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18390 arg2, then arg2 is not as specialized as arg1. */
18396 /* "If, for a given type, deduction succeeds in both directions
18397 (i.e., the types are identical after the transformations above)
18398 and both P and A were reference types (before being replaced with
18399 the type referred to above):
18400 - if the type from the argument template was an lvalue reference and
18401 the type from the parameter template was not, the argument type is
18402 considered to be more specialized than the other; otherwise,
18403 - if the type from the argument template is more cv-qualified
18404 than the type from the parameter template (as described above),
18405 the argument type is considered to be more specialized than the other;
18407 - neither type is more specialized than the other." */
18409 if (deduce1
&& deduce2
)
18411 if (ref1
&& ref2
&& ref1
!= ref2
)
18418 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18420 if ((quals1
& quals2
) == quals2
)
18422 if ((quals1
& quals2
) == quals1
)
18427 if (lose1
&& lose2
)
18428 /* We've failed to deduce something in either direction.
18429 These must be unordered. */
18432 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18433 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18434 /* We have already processed all of the arguments in our
18435 handing of the pack expansion type. */
18438 args1
= TREE_CHAIN (args1
);
18439 args2
= TREE_CHAIN (args2
);
18442 /* "In most cases, all template parameters must have values in order for
18443 deduction to succeed, but for partial ordering purposes a template
18444 parameter may remain without a value provided it is not used in the
18445 types being used for partial ordering."
18447 Thus, if we are missing any of the targs1 we need to substitute into
18448 origs1, then pat2 is not as specialized as pat1. This can happen when
18449 there is a nondeduced context. */
18450 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18452 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18455 processing_template_decl
--;
18457 /* All things being equal, if the next argument is a pack expansion
18458 for one function but not for the other, prefer the
18459 non-variadic function. FIXME this is bogus; see c++/41958. */
18461 && args1
&& TREE_VALUE (args1
)
18462 && args2
&& TREE_VALUE (args2
))
18464 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18465 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18468 if (lose1
== lose2
)
18476 /* Determine which of two partial specializations of TMPL is more
18479 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18480 to the first partial specialization. The TREE_VALUE is the
18481 innermost set of template parameters for the partial
18482 specialization. PAT2 is similar, but for the second template.
18484 Return 1 if the first partial specialization is more specialized;
18485 -1 if the second is more specialized; 0 if neither is more
18488 See [temp.class.order] for information about determining which of
18489 two templates is more specialized. */
18492 more_specialized_class (tree tmpl
, tree pat1
, tree pat2
)
18497 bool any_deductions
= false;
18499 tmpl1
= TREE_TYPE (pat1
);
18500 tmpl2
= TREE_TYPE (pat2
);
18502 /* Just like what happens for functions, if we are ordering between
18503 different class template specializations, we may encounter dependent
18504 types in the arguments, and we need our dependency check functions
18505 to behave correctly. */
18506 ++processing_template_decl
;
18507 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat1
),
18508 CLASSTYPE_TI_ARGS (tmpl1
),
18509 CLASSTYPE_TI_ARGS (tmpl2
));
18513 any_deductions
= true;
18516 targs
= get_class_bindings (tmpl
, TREE_VALUE (pat2
),
18517 CLASSTYPE_TI_ARGS (tmpl2
),
18518 CLASSTYPE_TI_ARGS (tmpl1
));
18522 any_deductions
= true;
18524 --processing_template_decl
;
18526 /* In the case of a tie where at least one of the class templates
18527 has a parameter pack at the end, the template with the most
18528 non-packed parameters wins. */
18531 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18532 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18534 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18535 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18536 int len1
= TREE_VEC_LENGTH (args1
);
18537 int len2
= TREE_VEC_LENGTH (args2
);
18539 /* We don't count the pack expansion at the end. */
18540 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18542 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18547 else if (len1
< len2
)
18554 /* Return the template arguments that will produce the function signature
18555 DECL from the function template FN, with the explicit template
18556 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18557 also match. Return NULL_TREE if no satisfactory arguments could be
18561 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18563 int ntparms
= DECL_NTPARMS (fn
);
18564 tree targs
= make_tree_vec (ntparms
);
18565 tree decl_type
= TREE_TYPE (decl
);
18566 tree decl_arg_types
;
18568 unsigned int nargs
, ix
;
18571 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18573 /* Never do unification on the 'this' parameter. */
18574 decl_arg_types
= skip_artificial_parms_for (decl
,
18575 TYPE_ARG_TYPES (decl_type
));
18577 nargs
= list_length (decl_arg_types
);
18578 args
= XALLOCAVEC (tree
, nargs
);
18579 for (arg
= decl_arg_types
, ix
= 0;
18580 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18581 arg
= TREE_CHAIN (arg
), ++ix
)
18582 args
[ix
] = TREE_VALUE (arg
);
18584 if (fn_type_unification (fn
, explicit_args
, targs
,
18586 (check_rettype
|| DECL_CONV_FN_P (fn
)
18587 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18588 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18590 == error_mark_node
)
18596 /* Return the innermost template arguments that, when applied to a partial
18597 specialization of TMPL whose innermost template parameters are
18598 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18601 For example, suppose we have:
18603 template <class T, class U> struct S {};
18604 template <class T> struct S<T*, int> {};
18606 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18607 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18608 int}. The resulting vector will be {double}, indicating that `T'
18609 is bound to `double'. */
18612 get_class_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
18614 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18616 tree innermost_deduced_args
;
18618 innermost_deduced_args
= make_tree_vec (ntparms
);
18619 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18621 deduced_args
= copy_node (args
);
18622 SET_TMPL_ARGS_LEVEL (deduced_args
,
18623 TMPL_ARGS_DEPTH (deduced_args
),
18624 innermost_deduced_args
);
18627 deduced_args
= innermost_deduced_args
;
18629 if (unify (tparms
, deduced_args
,
18630 INNERMOST_TEMPLATE_ARGS (spec_args
),
18631 INNERMOST_TEMPLATE_ARGS (args
),
18632 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18635 for (i
= 0; i
< ntparms
; ++i
)
18636 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18639 /* Verify that nondeduced template arguments agree with the type
18640 obtained from argument deduction.
18644 struct A { typedef int X; };
18645 template <class T, class U> struct C {};
18646 template <class T> struct C<T, typename T::X> {};
18648 Then with the instantiation `C<A, int>', we can deduce that
18649 `T' is `A' but unify () does not check whether `typename T::X'
18651 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18652 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18654 tf_none
, false, false);
18655 if (spec_args
== error_mark_node
18656 /* We only need to check the innermost arguments; the other
18657 arguments will always agree. */
18658 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18659 INNERMOST_TEMPLATE_ARGS (args
)))
18662 /* Now that we have bindings for all of the template arguments,
18663 ensure that the arguments deduced for the template template
18664 parameters have compatible template parameter lists. See the use
18665 of template_template_parm_bindings_ok_p in fn_type_unification
18666 for more information. */
18667 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18670 return deduced_args
;
18673 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18674 Return the TREE_LIST node with the most specialized template, if
18675 any. If there is no most specialized template, the error_mark_node
18678 Note that this function does not look at, or modify, the
18679 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18680 returned is one of the elements of INSTANTIATIONS, callers may
18681 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18682 and retrieve it from the value returned. */
18685 most_specialized_instantiation (tree templates
)
18689 ++processing_template_decl
;
18692 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18696 if (get_bindings (TREE_VALUE (champ
),
18697 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18698 NULL_TREE
, /*check_ret=*/true))
18701 if (get_bindings (TREE_VALUE (fn
),
18702 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18703 NULL_TREE
, /*check_ret=*/true))
18710 /* Equally specialized, move to next function. If there
18711 is no next function, nothing's most specialized. */
18712 fn
= TREE_CHAIN (fn
);
18720 /* Now verify that champ is better than everything earlier in the
18721 instantiation list. */
18722 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18723 if (get_bindings (TREE_VALUE (champ
),
18724 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18725 NULL_TREE
, /*check_ret=*/true)
18726 || !get_bindings (TREE_VALUE (fn
),
18727 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18728 NULL_TREE
, /*check_ret=*/true))
18734 processing_template_decl
--;
18737 return error_mark_node
;
18742 /* If DECL is a specialization of some template, return the most
18743 general such template. Otherwise, returns NULL_TREE.
18745 For example, given:
18747 template <class T> struct S { template <class U> void f(U); };
18749 if TMPL is `template <class U> void S<int>::f(U)' this will return
18750 the full template. This function will not trace past partial
18751 specializations, however. For example, given in addition:
18753 template <class T> struct S<T*> { template <class U> void f(U); };
18755 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18756 `template <class T> template <class U> S<T*>::f(U)'. */
18759 most_general_template (tree decl
)
18761 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18762 an immediate specialization. */
18763 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18765 if (DECL_TEMPLATE_INFO (decl
)) {
18766 decl
= DECL_TI_TEMPLATE (decl
);
18768 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18769 template friend. */
18770 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18776 /* Look for more and more general templates. */
18777 while (DECL_TEMPLATE_INFO (decl
))
18779 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18780 (See cp-tree.h for details.) */
18781 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18784 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18785 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18788 /* Stop if we run into an explicitly specialized class template. */
18789 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18790 && DECL_CONTEXT (decl
)
18791 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18794 decl
= DECL_TI_TEMPLATE (decl
);
18800 /* Return the most specialized of the class template partial
18801 specializations which can produce TYPE, a specialization of some class
18802 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18803 a _TYPE node corresponding to the partial specialization, while the
18804 TREE_PURPOSE is the set of template arguments that must be
18805 substituted into the TREE_TYPE in order to generate TYPE.
18807 If the choice of partial specialization is ambiguous, a diagnostic
18808 is issued, and the error_mark_node is returned. If there are no
18809 partial specializations matching TYPE, then NULL_TREE is
18810 returned, indicating that the primary template should be used. */
18813 most_specialized_class (tree type
, tsubst_flags_t complain
)
18815 tree list
= NULL_TREE
;
18820 tree outer_args
= NULL_TREE
;
18822 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
18823 tree main_tmpl
= most_general_template (tmpl
);
18824 tree args
= CLASSTYPE_TI_ARGS (type
);
18826 /* For determining which partial specialization to use, only the
18827 innermost args are interesting. */
18828 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18830 outer_args
= strip_innermost_template_args (args
, 1);
18831 args
= INNERMOST_TEMPLATE_ARGS (args
);
18834 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
18836 tree partial_spec_args
;
18838 tree spec_tmpl
= TREE_VALUE (t
);
18839 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18841 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18843 ++processing_template_decl
;
18847 /* Discard the outer levels of args, and then substitute in the
18848 template args from the enclosing class. */
18849 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18850 partial_spec_args
= tsubst_template_args
18851 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18853 /* And the same for the partial specialization TEMPLATE_DECL. */
18854 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18857 partial_spec_args
=
18858 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18861 /*require_all_args=*/true,
18862 /*use_default_args=*/true);
18864 --processing_template_decl
;
18866 if (partial_spec_args
== error_mark_node
)
18867 return error_mark_node
;
18868 if (spec_tmpl
== error_mark_node
)
18869 return error_mark_node
;
18871 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18872 spec_args
= get_class_bindings (tmpl
, parms
,
18878 spec_args
= add_to_template_args (outer_args
, spec_args
);
18879 list
= tree_cons (spec_args
, orig_parms
, list
);
18880 TREE_TYPE (list
) = TREE_TYPE (t
);
18887 ambiguous_p
= false;
18890 t
= TREE_CHAIN (t
);
18891 for (; t
; t
= TREE_CHAIN (t
))
18893 fate
= more_specialized_class (tmpl
, champ
, t
);
18900 t
= TREE_CHAIN (t
);
18903 ambiguous_p
= true;
18912 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18914 fate
= more_specialized_class (tmpl
, champ
, t
);
18917 ambiguous_p
= true;
18925 char *spaces
= NULL
;
18926 if (!(complain
& tf_error
))
18927 return error_mark_node
;
18928 error ("ambiguous class template instantiation for %q#T", type
);
18929 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18930 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18932 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18933 spaces
= spaces
? spaces
: get_spaces (str
);
18936 return error_mark_node
;
18942 /* Explicitly instantiate DECL. */
18945 do_decl_instantiation (tree decl
, tree storage
)
18947 tree result
= NULL_TREE
;
18950 if (!decl
|| decl
== error_mark_node
)
18951 /* An error occurred, for which grokdeclarator has already issued
18952 an appropriate message. */
18954 else if (! DECL_LANG_SPECIFIC (decl
))
18956 error ("explicit instantiation of non-template %q#D", decl
);
18959 else if (VAR_P (decl
))
18961 /* There is an asymmetry here in the way VAR_DECLs and
18962 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18963 the latter, the DECL we get back will be marked as a
18964 template instantiation, and the appropriate
18965 DECL_TEMPLATE_INFO will be set up. This does not happen for
18966 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18967 should handle VAR_DECLs as it currently handles
18969 if (!DECL_CLASS_SCOPE_P (decl
))
18971 error ("%qD is not a static data member of a class template", decl
);
18974 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18975 if (!result
|| !VAR_P (result
))
18977 error ("no matching template for %qD found", decl
);
18980 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18982 error ("type %qT for explicit instantiation %qD does not match "
18983 "declared type %qT", TREE_TYPE (result
), decl
,
18988 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18990 error ("explicit instantiation of %q#D", decl
);
18996 /* Check for various error cases. Note that if the explicit
18997 instantiation is valid the RESULT will currently be marked as an
18998 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18999 until we get here. */
19001 if (DECL_TEMPLATE_SPECIALIZATION (result
))
19003 /* DR 259 [temp.spec].
19005 Both an explicit instantiation and a declaration of an explicit
19006 specialization shall not appear in a program unless the explicit
19007 instantiation follows a declaration of the explicit specialization.
19009 For a given set of template parameters, if an explicit
19010 instantiation of a template appears after a declaration of an
19011 explicit specialization for that template, the explicit
19012 instantiation has no effect. */
19015 else if (DECL_EXPLICIT_INSTANTIATION (result
))
19019 No program shall explicitly instantiate any template more
19022 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19023 the first instantiation was `extern' and the second is not,
19024 and EXTERN_P for the opposite case. */
19025 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
19026 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
19027 /* If an "extern" explicit instantiation follows an ordinary
19028 explicit instantiation, the template is instantiated. */
19032 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
19034 error ("no matching template for %qD found", result
);
19037 else if (!DECL_TEMPLATE_INFO (result
))
19039 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
19043 if (storage
== NULL_TREE
)
19045 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19047 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
19048 pedwarn (input_location
, OPT_Wpedantic
,
19049 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19054 error ("storage class %qD applied to template instantiation", storage
);
19056 check_explicit_instantiation_namespace (result
);
19057 mark_decl_instantiated (result
, extern_p
);
19059 instantiate_decl (result
, /*defer_ok=*/1,
19060 /*expl_inst_class_mem_p=*/false);
19064 mark_class_instantiated (tree t
, int extern_p
)
19066 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
19067 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
19068 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
19069 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
19072 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
19073 rest_of_type_compilation (t
, 1);
19077 /* Called from do_type_instantiation through binding_table_foreach to
19078 do recursive instantiation for the type bound in ENTRY. */
19080 bt_instantiate_type_proc (binding_entry entry
, void *data
)
19082 tree storage
= *(tree
*) data
;
19084 if (MAYBE_CLASS_TYPE_P (entry
->type
)
19085 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
19086 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
19089 /* Called from do_type_instantiation to instantiate a member
19090 (a member function or a static member variable) of an
19091 explicitly instantiated class template. */
19093 instantiate_class_member (tree decl
, int extern_p
)
19095 mark_decl_instantiated (decl
, extern_p
);
19097 instantiate_decl (decl
, /*defer_ok=*/1,
19098 /*expl_inst_class_mem_p=*/true);
19101 /* Perform an explicit instantiation of template class T. STORAGE, if
19102 non-null, is the RID for extern, inline or static. COMPLAIN is
19103 nonzero if this is called from the parser, zero if called recursively,
19104 since the standard is unclear (as detailed below). */
19107 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
19112 int previous_instantiation_extern_p
= 0;
19114 if (TREE_CODE (t
) == TYPE_DECL
)
19117 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
19120 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
19122 error ("explicit instantiation of non-class template %qD", tmpl
);
19124 error ("explicit instantiation of non-template type %qT", t
);
19130 if (!COMPLETE_TYPE_P (t
))
19132 if (complain
& tf_error
)
19133 error ("explicit instantiation of %q#T before definition of template",
19138 if (storage
!= NULL_TREE
)
19140 if (!in_system_header_at (input_location
))
19142 if (storage
== ridpointers
[(int) RID_EXTERN
])
19144 if (cxx_dialect
== cxx98
)
19145 pedwarn (input_location
, OPT_Wpedantic
,
19146 "ISO C++ 1998 forbids the use of %<extern%> on "
19147 "explicit instantiations");
19150 pedwarn (input_location
, OPT_Wpedantic
,
19151 "ISO C++ forbids the use of %qE"
19152 " on explicit instantiations", storage
);
19155 if (storage
== ridpointers
[(int) RID_INLINE
])
19157 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19159 else if (storage
== ridpointers
[(int) RID_STATIC
])
19163 error ("storage class %qD applied to template instantiation",
19169 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
19171 /* DR 259 [temp.spec].
19173 Both an explicit instantiation and a declaration of an explicit
19174 specialization shall not appear in a program unless the explicit
19175 instantiation follows a declaration of the explicit specialization.
19177 For a given set of template parameters, if an explicit
19178 instantiation of a template appears after a declaration of an
19179 explicit specialization for that template, the explicit
19180 instantiation has no effect. */
19183 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
19187 No program shall explicitly instantiate any template more
19190 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19191 instantiation was `extern'. If EXTERN_P then the second is.
19192 These cases are OK. */
19193 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
19195 if (!previous_instantiation_extern_p
&& !extern_p
19196 && (complain
& tf_error
))
19197 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
19199 /* If we've already instantiated the template, just return now. */
19200 if (!CLASSTYPE_INTERFACE_ONLY (t
))
19204 check_explicit_instantiation_namespace (TYPE_NAME (t
));
19205 mark_class_instantiated (t
, extern_p
);
19213 /* In contrast to implicit instantiation, where only the
19214 declarations, and not the definitions, of members are
19215 instantiated, we have here:
19219 The explicit instantiation of a class template specialization
19220 implies the instantiation of all of its members not
19221 previously explicitly specialized in the translation unit
19222 containing the explicit instantiation.
19224 Of course, we can't instantiate member template classes, since
19225 we don't have any arguments for them. Note that the standard
19226 is unclear on whether the instantiation of the members are
19227 *explicit* instantiations or not. However, the most natural
19228 interpretation is that it should be an explicit instantiation. */
19231 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19232 if (TREE_CODE (tmp
) == FUNCTION_DECL
19233 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19234 instantiate_class_member (tmp
, extern_p
);
19236 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19237 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19238 instantiate_class_member (tmp
, extern_p
);
19240 if (CLASSTYPE_NESTED_UTDS (t
))
19241 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19242 bt_instantiate_type_proc
, &storage
);
19246 /* Given a function DECL, which is a specialization of TMPL, modify
19247 DECL to be a re-instantiation of TMPL with the same template
19248 arguments. TMPL should be the template into which tsubst'ing
19249 should occur for DECL, not the most general template.
19251 One reason for doing this is a scenario like this:
19254 void f(const T&, int i);
19256 void g() { f(3, 7); }
19259 void f(const T& t, const int i) { }
19261 Note that when the template is first instantiated, with
19262 instantiate_template, the resulting DECL will have no name for the
19263 first parameter, and the wrong type for the second. So, when we go
19264 to instantiate the DECL, we regenerate it. */
19267 regenerate_decl_from_template (tree decl
, tree tmpl
)
19269 /* The arguments used to instantiate DECL, from the most general
19274 args
= DECL_TI_ARGS (decl
);
19275 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19277 /* Make sure that we can see identifiers, and compute access
19279 push_access_scope (decl
);
19281 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19289 args_depth
= TMPL_ARGS_DEPTH (args
);
19290 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19291 if (args_depth
> parms_depth
)
19292 args
= get_innermost_template_args (args
, parms_depth
);
19294 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19295 args
, tf_error
, NULL_TREE
,
19296 /*defer_ok*/false);
19297 if (specs
&& specs
!= error_mark_node
)
19298 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19301 /* Merge parameter declarations. */
19302 decl_parm
= skip_artificial_parms_for (decl
,
19303 DECL_ARGUMENTS (decl
));
19305 = skip_artificial_parms_for (code_pattern
,
19306 DECL_ARGUMENTS (code_pattern
));
19307 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19312 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19313 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19314 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19316 parm_type
= type_decays_to (parm_type
);
19317 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19318 TREE_TYPE (decl_parm
) = parm_type
;
19319 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19320 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19322 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19323 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19325 decl_parm
= DECL_CHAIN (decl_parm
);
19326 pattern_parm
= DECL_CHAIN (pattern_parm
);
19328 /* Merge any parameters that match with the function parameter
19330 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19333 tree expanded_types
;
19334 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19335 the parameters in this function parameter pack. */
19336 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19337 args
, tf_error
, NULL_TREE
);
19338 len
= TREE_VEC_LENGTH (expanded_types
);
19339 for (i
= 0; i
< len
; i
++)
19344 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19345 /* Rename the parameter to include the index. */
19346 DECL_NAME (decl_parm
) =
19347 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19348 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19349 parm_type
= type_decays_to (parm_type
);
19350 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19351 TREE_TYPE (decl_parm
) = parm_type
;
19352 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19353 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19355 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19356 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19358 decl_parm
= DECL_CHAIN (decl_parm
);
19361 /* Merge additional specifiers from the CODE_PATTERN. */
19362 if (DECL_DECLARED_INLINE_P (code_pattern
)
19363 && !DECL_DECLARED_INLINE_P (decl
))
19364 DECL_DECLARED_INLINE_P (decl
) = 1;
19366 else if (VAR_P (decl
))
19368 DECL_INITIAL (decl
) =
19369 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19370 tf_error
, DECL_TI_TEMPLATE (decl
),
19371 /*integral_constant_expression_p=*/false);
19372 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19373 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19374 tf_error
, DECL_TI_TEMPLATE (decl
));
19377 gcc_unreachable ();
19379 pop_access_scope (decl
);
19382 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19383 substituted to get DECL. */
19386 template_for_substitution (tree decl
)
19388 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19390 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19391 for the instantiation. This is not always the most general
19392 template. Consider, for example:
19395 struct S { template <class U> void f();
19396 template <> void f<int>(); };
19398 and an instantiation of S<double>::f<int>. We want TD to be the
19399 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19400 while (/* An instantiation cannot have a definition, so we need a
19401 more general template. */
19402 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19403 /* We must also deal with friend templates. Given:
19405 template <class T> struct S {
19406 template <class U> friend void f() {};
19409 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19410 so far as the language is concerned, but that's still
19411 where we get the pattern for the instantiation from. On
19412 other hand, if the definition comes outside the class, say:
19414 template <class T> struct S {
19415 template <class U> friend void f();
19417 template <class U> friend void f() {}
19419 we don't need to look any further. That's what the check for
19420 DECL_INITIAL is for. */
19421 || (TREE_CODE (decl
) == FUNCTION_DECL
19422 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19423 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19425 /* The present template, TD, should not be a definition. If it
19426 were a definition, we should be using it! Note that we
19427 cannot restructure the loop to just keep going until we find
19428 a template with a definition, since that might go too far if
19429 a specialization was declared, but not defined. */
19430 gcc_assert (!VAR_P (decl
)
19431 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19433 /* Fetch the more general template. */
19434 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19440 /* Returns true if we need to instantiate this template instance even if we
19441 know we aren't going to emit it.. */
19444 always_instantiate_p (tree decl
)
19446 /* We always instantiate inline functions so that we can inline them. An
19447 explicit instantiation declaration prohibits implicit instantiation of
19448 non-inline functions. With high levels of optimization, we would
19449 normally inline non-inline functions -- but we're not allowed to do
19450 that for "extern template" functions. Therefore, we check
19451 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19452 return ((TREE_CODE (decl
) == FUNCTION_DECL
19453 && (DECL_DECLARED_INLINE_P (decl
)
19454 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19455 /* And we need to instantiate static data members so that
19456 their initializers are available in integral constant
19459 && decl_maybe_constant_var_p (decl
)));
19462 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19463 instantiate it now, modifying TREE_TYPE (fn). */
19466 maybe_instantiate_noexcept (tree fn
)
19468 tree fntype
, spec
, noex
, clone
;
19470 /* Don't instantiate a noexcept-specification from template context. */
19471 if (processing_template_decl
)
19474 if (DECL_CLONED_FUNCTION_P (fn
))
19475 fn
= DECL_CLONED_FUNCTION (fn
);
19476 fntype
= TREE_TYPE (fn
);
19477 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19479 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19482 noex
= TREE_PURPOSE (spec
);
19484 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19486 if (push_tinst_level (fn
))
19488 push_access_scope (fn
);
19489 push_deferring_access_checks (dk_no_deferred
);
19490 input_location
= DECL_SOURCE_LOCATION (fn
);
19491 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19492 DEFERRED_NOEXCEPT_ARGS (noex
),
19493 tf_warning_or_error
, fn
,
19494 /*function_p=*/false,
19495 /*integral_constant_expression_p=*/true);
19496 pop_deferring_access_checks ();
19497 pop_access_scope (fn
);
19498 pop_tinst_level ();
19499 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19500 if (spec
== error_mark_node
)
19501 spec
= noexcept_false_spec
;
19504 spec
= noexcept_false_spec
;
19508 /* This is an implicitly declared function, so NOEX is a list of
19509 other functions to evaluate and merge. */
19511 spec
= noexcept_true_spec
;
19512 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19514 tree fn
= OVL_CURRENT (elt
);
19516 maybe_instantiate_noexcept (fn
);
19517 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19518 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19522 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19524 FOR_EACH_CLONE (clone
, fn
)
19526 if (TREE_TYPE (clone
) == fntype
)
19527 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19529 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19533 /* Produce the definition of D, a _DECL generated from a template. If
19534 DEFER_OK is nonzero, then we don't have to actually do the
19535 instantiation now; we just have to do it sometime. Normally it is
19536 an error if this is an explicit instantiation but D is undefined.
19537 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19538 explicitly instantiated class template. */
19541 instantiate_decl (tree d
, int defer_ok
,
19542 bool expl_inst_class_mem_p
)
19544 tree tmpl
= DECL_TI_TEMPLATE (d
);
19551 bool pattern_defined
;
19552 location_t saved_loc
= input_location
;
19553 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19554 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19559 /* This function should only be used to instantiate templates for
19560 functions and static member variables. */
19561 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19563 /* Variables are never deferred; if instantiation is required, they
19564 are instantiated right away. That allows for better code in the
19565 case that an expression refers to the value of the variable --
19566 if the variable has a constant value the referring expression can
19567 take advantage of that fact. */
19569 || DECL_DECLARED_CONSTEXPR_P (d
))
19572 /* Don't instantiate cloned functions. Instead, instantiate the
19573 functions they cloned. */
19574 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19575 d
= DECL_CLONED_FUNCTION (d
);
19577 if (DECL_TEMPLATE_INSTANTIATED (d
)
19578 || (TREE_CODE (d
) == FUNCTION_DECL
19579 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19580 || DECL_TEMPLATE_SPECIALIZATION (d
))
19581 /* D has already been instantiated or explicitly specialized, so
19582 there's nothing for us to do here.
19584 It might seem reasonable to check whether or not D is an explicit
19585 instantiation, and, if so, stop here. But when an explicit
19586 instantiation is deferred until the end of the compilation,
19587 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19588 the instantiation. */
19591 /* Check to see whether we know that this template will be
19592 instantiated in some other file, as with "extern template"
19594 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19596 /* In general, we do not instantiate such templates. */
19597 if (external_p
&& !always_instantiate_p (d
))
19600 gen_tmpl
= most_general_template (tmpl
);
19601 gen_args
= DECL_TI_ARGS (d
);
19603 if (tmpl
!= gen_tmpl
)
19604 /* We should already have the extra args. */
19605 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19606 == TMPL_ARGS_DEPTH (gen_args
));
19607 /* And what's in the hash table should match D. */
19608 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19609 || spec
== NULL_TREE
);
19611 /* This needs to happen before any tsubsting. */
19612 if (! push_tinst_level (d
))
19615 timevar_push (TV_TEMPLATE_INST
);
19617 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19618 for the instantiation. */
19619 td
= template_for_substitution (d
);
19620 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19622 /* We should never be trying to instantiate a member of a class
19623 template or partial specialization. */
19624 gcc_assert (d
!= code_pattern
);
19626 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19627 || DECL_TEMPLATE_SPECIALIZATION (td
))
19628 /* In the case of a friend template whose definition is provided
19629 outside the class, we may have too many arguments. Drop the
19630 ones we don't need. The same is true for specializations. */
19631 args
= get_innermost_template_args
19632 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19636 if (TREE_CODE (d
) == FUNCTION_DECL
)
19637 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19638 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19640 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19642 /* We may be in the middle of deferred access check. Disable it now. */
19643 push_deferring_access_checks (dk_no_deferred
);
19645 /* Unless an explicit instantiation directive has already determined
19646 the linkage of D, remember that a definition is available for
19648 if (pattern_defined
19649 && !DECL_INTERFACE_KNOWN (d
)
19650 && !DECL_NOT_REALLY_EXTERN (d
))
19651 mark_definable (d
);
19653 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19654 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19655 input_location
= DECL_SOURCE_LOCATION (d
);
19657 /* If D is a member of an explicitly instantiated class template,
19658 and no definition is available, treat it like an implicit
19660 if (!pattern_defined
&& expl_inst_class_mem_p
19661 && DECL_EXPLICIT_INSTANTIATION (d
))
19663 /* Leave linkage flags alone on instantiations with anonymous
19665 if (TREE_PUBLIC (d
))
19667 DECL_NOT_REALLY_EXTERN (d
) = 0;
19668 DECL_INTERFACE_KNOWN (d
) = 0;
19670 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19673 if (TREE_CODE (d
) == FUNCTION_DECL
)
19674 maybe_instantiate_noexcept (d
);
19676 /* Defer all other templates, unless we have been explicitly
19677 forbidden from doing so. */
19678 if (/* If there is no definition, we cannot instantiate the
19681 /* If it's OK to postpone instantiation, do so. */
19683 /* If this is a static data member that will be defined
19684 elsewhere, we don't want to instantiate the entire data
19685 member, but we do want to instantiate the initializer so that
19686 we can substitute that elsewhere. */
19687 || (external_p
&& VAR_P (d
)))
19689 /* The definition of the static data member is now required so
19690 we must substitute the initializer. */
19692 && !DECL_INITIAL (d
)
19693 && DECL_INITIAL (code_pattern
))
19697 bool const_init
= false;
19699 ns
= decl_namespace_context (d
);
19700 push_nested_namespace (ns
);
19701 push_nested_class (DECL_CONTEXT (d
));
19702 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19704 tf_warning_or_error
, NULL_TREE
,
19705 /*integral_constant_expression_p=*/false);
19706 /* Make sure the initializer is still constant, in case of
19707 circular dependency (template/instantiate6.C). */
19709 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19710 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19711 /*asmspec_tree=*/NULL_TREE
,
19712 LOOKUP_ONLYCONVERTING
);
19713 pop_nested_class ();
19714 pop_nested_namespace (ns
);
19717 /* We restore the source position here because it's used by
19718 add_pending_template. */
19719 input_location
= saved_loc
;
19721 if (at_eof
&& !pattern_defined
19722 && DECL_EXPLICIT_INSTANTIATION (d
)
19723 && DECL_NOT_REALLY_EXTERN (d
))
19726 The definition of a non-exported function template, a
19727 non-exported member function template, or a non-exported
19728 member function or static data member of a class template
19729 shall be present in every translation unit in which it is
19730 explicitly instantiated. */
19731 permerror (input_location
, "explicit instantiation of %qD "
19732 "but no definition available", d
);
19734 /* If we're in unevaluated context, we just wanted to get the
19735 constant value; this isn't an odr use, so don't queue
19736 a full instantiation. */
19737 if (cp_unevaluated_operand
!= 0)
19739 /* ??? Historically, we have instantiated inline functions, even
19740 when marked as "extern template". */
19741 if (!(external_p
&& VAR_P (d
)))
19742 add_pending_template (d
);
19745 /* Tell the repository that D is available in this translation unit
19746 -- and see if it is supposed to be instantiated here. */
19747 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19749 /* In a PCH file, despite the fact that the repository hasn't
19750 requested instantiation in the PCH it is still possible that
19751 an instantiation will be required in a file that includes the
19754 add_pending_template (d
);
19755 /* Instantiate inline functions so that the inliner can do its
19756 job, even though we'll not be emitting a copy of this
19758 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19762 fn_context
= decl_function_context (d
);
19763 nested
= (current_function_decl
!= NULL_TREE
);
19765 push_to_top_level ();
19769 push_function_context ();
19770 cp_unevaluated_operand
= 0;
19771 c_inhibit_evaluation_warnings
= 0;
19774 /* Mark D as instantiated so that recursive calls to
19775 instantiate_decl do not try to instantiate it again. */
19776 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19778 /* Regenerate the declaration in case the template has been modified
19779 by a subsequent redeclaration. */
19780 regenerate_decl_from_template (d
, td
);
19782 /* We already set the file and line above. Reset them now in case
19783 they changed as a result of calling regenerate_decl_from_template. */
19784 input_location
= DECL_SOURCE_LOCATION (d
);
19789 bool const_init
= false;
19791 /* Clear out DECL_RTL; whatever was there before may not be right
19792 since we've reset the type of the declaration. */
19793 SET_DECL_RTL (d
, NULL
);
19794 DECL_IN_AGGR_P (d
) = 0;
19796 /* The initializer is placed in DECL_INITIAL by
19797 regenerate_decl_from_template so we don't need to
19798 push/pop_access_scope again here. Pull it out so that
19799 cp_finish_decl can process it. */
19800 init
= DECL_INITIAL (d
);
19801 DECL_INITIAL (d
) = NULL_TREE
;
19802 DECL_INITIALIZED_P (d
) = 0;
19804 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19805 initializer. That function will defer actual emission until
19806 we have a chance to determine linkage. */
19807 DECL_EXTERNAL (d
) = 0;
19809 /* Enter the scope of D so that access-checking works correctly. */
19810 push_nested_class (DECL_CONTEXT (d
));
19811 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19812 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19813 pop_nested_class ();
19815 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19816 synthesize_method (d
);
19817 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19819 struct pointer_map_t
*saved_local_specializations
;
19823 tree block
= NULL_TREE
;
19825 /* Save away the current list, in case we are instantiating one
19826 template from within the body of another. */
19827 saved_local_specializations
= local_specializations
;
19829 /* Set up the list of local specializations. */
19830 local_specializations
= pointer_map_create ();
19832 /* Set up context. */
19833 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19834 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19835 block
= push_stmt_list ();
19837 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19839 /* Some typedefs referenced from within the template code need to be
19840 access checked at template instantiation time, i.e now. These
19841 types were added to the template at parsing time. Let's get those
19842 and perform the access checks then. */
19843 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19846 /* Create substitution entries for the parameters. */
19847 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19848 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19849 spec_parm
= DECL_ARGUMENTS (d
);
19850 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19852 register_local_specialization (spec_parm
, tmpl_parm
);
19853 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19854 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19856 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19858 if (!DECL_PACK_P (tmpl_parm
))
19860 register_local_specialization (spec_parm
, tmpl_parm
);
19861 spec_parm
= DECL_CHAIN (spec_parm
);
19865 /* Register the (value) argument pack as a specialization of
19866 TMPL_PARM, then move on. */
19867 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19868 register_local_specialization (argpack
, tmpl_parm
);
19871 gcc_assert (!spec_parm
);
19873 /* Substitute into the body of the function. */
19874 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19875 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19876 tf_warning_or_error
, tmpl
);
19879 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19880 tf_warning_or_error
, tmpl
,
19881 /*integral_constant_expression_p=*/false);
19883 /* Set the current input_location to the end of the function
19884 so that finish_function knows where we are. */
19886 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19888 /* Remember if we saw an infinite loop in the template. */
19889 current_function_infinite_loop
19890 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
19893 /* We don't need the local specializations any more. */
19894 pointer_map_destroy (local_specializations
);
19895 local_specializations
= saved_local_specializations
;
19897 /* Finish the function. */
19898 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19899 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19900 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19903 d
= finish_function (0);
19904 expand_or_defer_fn (d
);
19907 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19908 cp_check_omp_declare_reduction (d
);
19911 /* We're not deferring instantiation any more. */
19912 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19915 pop_from_top_level ();
19917 pop_function_context ();
19920 input_location
= saved_loc
;
19921 cp_unevaluated_operand
= saved_unevaluated_operand
;
19922 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19923 pop_deferring_access_checks ();
19924 pop_tinst_level ();
19926 timevar_pop (TV_TEMPLATE_INST
);
19931 /* Run through the list of templates that we wish we could
19932 instantiate, and instantiate any we can. RETRIES is the
19933 number of times we retry pending template instantiation. */
19936 instantiate_pending_templates (int retries
)
19939 location_t saved_loc
= input_location
;
19941 /* Instantiating templates may trigger vtable generation. This in turn
19942 may require further template instantiations. We place a limit here
19943 to avoid infinite loop. */
19944 if (pending_templates
&& retries
>= max_tinst_depth
)
19946 tree decl
= pending_templates
->tinst
->decl
;
19948 error ("template instantiation depth exceeds maximum of %d"
19949 " instantiating %q+D, possibly from virtual table generation"
19950 " (use -ftemplate-depth= to increase the maximum)",
19951 max_tinst_depth
, decl
);
19952 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19953 /* Pretend that we defined it. */
19954 DECL_INITIAL (decl
) = error_mark_node
;
19960 struct pending_template
**t
= &pending_templates
;
19961 struct pending_template
*last
= NULL
;
19965 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19966 bool complete
= false;
19968 if (TYPE_P (instantiation
))
19972 if (!COMPLETE_TYPE_P (instantiation
))
19974 instantiate_class_template (instantiation
);
19975 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19976 for (fn
= TYPE_METHODS (instantiation
);
19978 fn
= TREE_CHAIN (fn
))
19979 if (! DECL_ARTIFICIAL (fn
))
19980 instantiate_decl (fn
,
19982 /*expl_inst_class_mem_p=*/false);
19983 if (COMPLETE_TYPE_P (instantiation
))
19987 complete
= COMPLETE_TYPE_P (instantiation
);
19991 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19992 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19995 = instantiate_decl (instantiation
,
19997 /*expl_inst_class_mem_p=*/false);
19998 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
20002 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20003 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
20007 /* If INSTANTIATION has been instantiated, then we don't
20008 need to consider it again in the future. */
20016 current_tinst_level
= NULL
;
20018 last_pending_template
= last
;
20020 while (reconsider
);
20022 input_location
= saved_loc
;
20025 /* Substitute ARGVEC into T, which is a list of initializers for
20026 either base class or a non-static data member. The TREE_PURPOSEs
20027 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20028 instantiate_decl. */
20031 tsubst_initializer_list (tree t
, tree argvec
)
20033 tree inits
= NULL_TREE
;
20035 for (; t
; t
= TREE_CHAIN (t
))
20039 tree expanded_bases
= NULL_TREE
;
20040 tree expanded_arguments
= NULL_TREE
;
20043 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
20048 /* Expand the base class expansion type into separate base
20050 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
20051 tf_warning_or_error
,
20053 if (expanded_bases
== error_mark_node
)
20056 /* We'll be building separate TREE_LISTs of arguments for
20058 len
= TREE_VEC_LENGTH (expanded_bases
);
20059 expanded_arguments
= make_tree_vec (len
);
20060 for (i
= 0; i
< len
; i
++)
20061 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
20063 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20064 expand each argument in the TREE_VALUE of t. */
20065 expr
= make_node (EXPR_PACK_EXPANSION
);
20066 PACK_EXPANSION_LOCAL_P (expr
) = true;
20067 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
20068 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
20070 if (TREE_VALUE (t
) == void_type_node
)
20071 /* VOID_TYPE_NODE is used to indicate
20072 value-initialization. */
20074 for (i
= 0; i
< len
; i
++)
20075 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
20079 /* Substitute parameter packs into each argument in the
20081 in_base_initializer
= 1;
20082 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
20084 tree expanded_exprs
;
20086 /* Expand the argument. */
20087 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
20089 = tsubst_pack_expansion (expr
, argvec
,
20090 tf_warning_or_error
,
20092 if (expanded_exprs
== error_mark_node
)
20095 /* Prepend each of the expanded expressions to the
20096 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20097 for (i
= 0; i
< len
; i
++)
20099 TREE_VEC_ELT (expanded_arguments
, i
) =
20100 tree_cons (NULL_TREE
,
20101 TREE_VEC_ELT (expanded_exprs
, i
),
20102 TREE_VEC_ELT (expanded_arguments
, i
));
20105 in_base_initializer
= 0;
20107 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20108 since we built them backwards. */
20109 for (i
= 0; i
< len
; i
++)
20111 TREE_VEC_ELT (expanded_arguments
, i
) =
20112 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
20117 for (i
= 0; i
< len
; ++i
)
20119 if (expanded_bases
)
20121 decl
= TREE_VEC_ELT (expanded_bases
, i
);
20122 decl
= expand_member_init (decl
);
20123 init
= TREE_VEC_ELT (expanded_arguments
, i
);
20128 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
20129 tf_warning_or_error
, NULL_TREE
);
20131 decl
= expand_member_init (decl
);
20132 if (decl
&& !DECL_P (decl
))
20133 in_base_initializer
= 1;
20135 init
= TREE_VALUE (t
);
20137 if (init
!= void_type_node
)
20138 init
= tsubst_expr (init
, argvec
,
20139 tf_warning_or_error
, NULL_TREE
,
20140 /*integral_constant_expression_p=*/false);
20141 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
20142 /* If we had an initializer but it instantiated to nothing,
20143 value-initialize the object. This will only occur when
20144 the initializer was a pack expansion where the parameter
20145 packs used in that expansion were of length zero. */
20146 init
= void_type_node
;
20147 in_base_initializer
= 0;
20152 init
= build_tree_list (decl
, init
);
20153 TREE_CHAIN (init
) = inits
;
20161 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20164 set_current_access_from_decl (tree decl
)
20166 if (TREE_PRIVATE (decl
))
20167 current_access_specifier
= access_private_node
;
20168 else if (TREE_PROTECTED (decl
))
20169 current_access_specifier
= access_protected_node
;
20171 current_access_specifier
= access_public_node
;
20174 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20175 is the instantiation (which should have been created with
20176 start_enum) and ARGS are the template arguments to use. */
20179 tsubst_enum (tree tag
, tree newtag
, tree args
)
20183 if (SCOPED_ENUM_P (newtag
))
20184 begin_scope (sk_scoped_enum
, newtag
);
20186 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
20191 decl
= TREE_VALUE (e
);
20192 /* Note that in a template enum, the TREE_VALUE is the
20193 CONST_DECL, not the corresponding INTEGER_CST. */
20194 value
= tsubst_expr (DECL_INITIAL (decl
),
20195 args
, tf_warning_or_error
, NULL_TREE
,
20196 /*integral_constant_expression_p=*/true);
20198 /* Give this enumeration constant the correct access. */
20199 set_current_access_from_decl (decl
);
20201 /* Actually build the enumerator itself. */
20203 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
20206 if (SCOPED_ENUM_P (newtag
))
20209 finish_enum_value_list (newtag
);
20210 finish_enum (newtag
);
20212 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
20213 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
20216 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20217 its type -- but without substituting the innermost set of template
20218 arguments. So, innermost set of template parameters will appear in
20222 get_mostly_instantiated_function_type (tree decl
)
20230 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
20231 targs
= DECL_TI_ARGS (decl
);
20232 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
20233 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
20235 /* There should be as many levels of arguments as there are levels
20237 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
20239 fn_type
= TREE_TYPE (tmpl
);
20241 if (parm_depth
== 1)
20242 /* No substitution is necessary. */
20249 /* Replace the innermost level of the TARGS with NULL_TREEs to
20250 let tsubst know not to substitute for those parameters. */
20251 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20252 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20253 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20254 TMPL_ARGS_LEVEL (targs
, i
));
20255 SET_TMPL_ARGS_LEVEL (partial_args
,
20256 TMPL_ARGS_DEPTH (targs
),
20257 make_tree_vec (DECL_NTPARMS (tmpl
)));
20259 /* Make sure that we can see identifiers, and compute access
20261 push_access_scope (decl
);
20263 ++processing_template_decl
;
20264 /* Now, do the (partial) substitution to figure out the
20265 appropriate function type. */
20266 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20267 --processing_template_decl
;
20269 /* Substitute into the template parameters to obtain the real
20270 innermost set of parameters. This step is important if the
20271 innermost set of template parameters contains value
20272 parameters whose types depend on outer template parameters. */
20273 TREE_VEC_LENGTH (partial_args
)--;
20274 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20276 pop_access_scope (decl
);
20282 /* Return truthvalue if we're processing a template different from
20283 the last one involved in diagnostics. */
20285 problematic_instantiation_changed (void)
20287 return current_tinst_level
!= last_error_tinst_level
;
20290 /* Remember current template involved in diagnostics. */
20292 record_last_problematic_instantiation (void)
20294 last_error_tinst_level
= current_tinst_level
;
20297 struct tinst_level
*
20298 current_instantiation (void)
20300 return current_tinst_level
;
20303 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20304 type. Return zero for ok, nonzero for disallowed. Issue error and
20305 warning messages under control of COMPLAIN. */
20308 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20310 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20312 else if (POINTER_TYPE_P (type
))
20314 else if (TYPE_PTRMEM_P (type
))
20316 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20318 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20320 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20322 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20325 if (complain
& tf_error
)
20327 if (type
== error_mark_node
)
20328 inform (input_location
, "invalid template non-type parameter");
20330 error ("%q#T is not a valid type for a template non-type parameter",
20336 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20337 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20340 dependent_type_p_r (tree type
)
20346 A type is dependent if it is:
20348 -- a template parameter. Template template parameters are types
20349 for us (since TYPE_P holds true for them) so we handle
20351 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20352 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20354 /* -- a qualified-id with a nested-name-specifier which contains a
20355 class-name that names a dependent type or whose unqualified-id
20356 names a dependent type. */
20357 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20359 /* -- a cv-qualified type where the cv-unqualified type is
20361 type
= TYPE_MAIN_VARIANT (type
);
20362 /* -- a compound type constructed from any dependent type. */
20363 if (TYPE_PTRMEM_P (type
))
20364 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20365 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20367 else if (TYPE_PTR_P (type
)
20368 || TREE_CODE (type
) == REFERENCE_TYPE
)
20369 return dependent_type_p (TREE_TYPE (type
));
20370 else if (TREE_CODE (type
) == FUNCTION_TYPE
20371 || TREE_CODE (type
) == METHOD_TYPE
)
20375 if (dependent_type_p (TREE_TYPE (type
)))
20377 for (arg_type
= TYPE_ARG_TYPES (type
);
20379 arg_type
= TREE_CHAIN (arg_type
))
20380 if (dependent_type_p (TREE_VALUE (arg_type
)))
20384 /* -- an array type constructed from any dependent type or whose
20385 size is specified by a constant expression that is
20388 We checked for type- and value-dependence of the bounds in
20389 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20390 if (TREE_CODE (type
) == ARRAY_TYPE
)
20392 if (TYPE_DOMAIN (type
)
20393 && dependent_type_p (TYPE_DOMAIN (type
)))
20395 return dependent_type_p (TREE_TYPE (type
));
20398 /* -- a template-id in which either the template name is a template
20400 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20402 /* ... or any of the template arguments is a dependent type or
20403 an expression that is type-dependent or value-dependent. */
20404 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20405 && (any_dependent_template_arguments_p
20406 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20409 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20410 dependent; if the argument of the `typeof' expression is not
20411 type-dependent, then it should already been have resolved. */
20412 if (TREE_CODE (type
) == TYPEOF_TYPE
20413 || TREE_CODE (type
) == DECLTYPE_TYPE
20414 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20417 /* A template argument pack is dependent if any of its packed
20419 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20421 tree args
= ARGUMENT_PACK_ARGS (type
);
20422 int i
, len
= TREE_VEC_LENGTH (args
);
20423 for (i
= 0; i
< len
; ++i
)
20424 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20428 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20429 be template parameters. */
20430 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20433 /* The standard does not specifically mention types that are local
20434 to template functions or local classes, but they should be
20435 considered dependent too. For example:
20437 template <int I> void f() {
20442 The size of `E' cannot be known until the value of `I' has been
20443 determined. Therefore, `E' must be considered dependent. */
20444 scope
= TYPE_CONTEXT (type
);
20445 if (scope
&& TYPE_P (scope
))
20446 return dependent_type_p (scope
);
20447 /* Don't use type_dependent_expression_p here, as it can lead
20448 to infinite recursion trying to determine whether a lambda
20449 nested in a lambda is dependent (c++/47687). */
20450 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20451 && DECL_LANG_SPECIFIC (scope
)
20452 && DECL_TEMPLATE_INFO (scope
)
20453 && (any_dependent_template_arguments_p
20454 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20457 /* Other types are non-dependent. */
20461 /* Returns TRUE if TYPE is dependent, in the sense of
20462 [temp.dep.type]. Note that a NULL type is considered dependent. */
20465 dependent_type_p (tree type
)
20467 /* If there are no template parameters in scope, then there can't be
20468 any dependent types. */
20469 if (!processing_template_decl
)
20471 /* If we are not processing a template, then nobody should be
20472 providing us with a dependent type. */
20474 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20478 /* If the type is NULL, we have not computed a type for the entity
20479 in question; in that case, the type is dependent. */
20483 /* Erroneous types can be considered non-dependent. */
20484 if (type
== error_mark_node
)
20487 /* If we have not already computed the appropriate value for TYPE,
20489 if (!TYPE_DEPENDENT_P_VALID (type
))
20491 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20492 TYPE_DEPENDENT_P_VALID (type
) = 1;
20495 return TYPE_DEPENDENT_P (type
);
20498 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20499 lookup. In other words, a dependent type that is not the current
20503 dependent_scope_p (tree scope
)
20505 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20506 && !currently_open_class (scope
));
20509 /* T is a SCOPE_REF; return whether we need to consider it
20510 instantiation-dependent so that we can check access at instantiation
20511 time even though we know which member it resolves to. */
20514 instantiation_dependent_scope_ref_p (tree t
)
20516 if (DECL_P (TREE_OPERAND (t
, 1))
20517 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20518 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20519 TREE_OPERAND (t
, 1)))
20525 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20526 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20529 /* Note that this predicate is not appropriate for general expressions;
20530 only constant expressions (that satisfy potential_constant_expression)
20531 can be tested for value dependence. */
20534 value_dependent_expression_p (tree expression
)
20536 if (!processing_template_decl
)
20539 /* A name declared with a dependent type. */
20540 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20543 switch (TREE_CODE (expression
))
20545 case IDENTIFIER_NODE
:
20546 /* A name that has not been looked up -- must be dependent. */
20549 case TEMPLATE_PARM_INDEX
:
20550 /* A non-type template parm. */
20554 /* A non-type template parm. */
20555 if (DECL_TEMPLATE_PARM_P (expression
))
20557 return value_dependent_expression_p (DECL_INITIAL (expression
));
20560 /* A constant with literal type and is initialized
20561 with an expression that is value-dependent.
20563 Note that a non-dependent parenthesized initializer will have
20564 already been replaced with its constant value, so if we see
20565 a TREE_LIST it must be dependent. */
20566 if (DECL_INITIAL (expression
)
20567 && decl_constant_var_p (expression
)
20568 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20569 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20573 case DYNAMIC_CAST_EXPR
:
20574 case STATIC_CAST_EXPR
:
20575 case CONST_CAST_EXPR
:
20576 case REINTERPRET_CAST_EXPR
:
20578 /* These expressions are value-dependent if the type to which
20579 the cast occurs is dependent or the expression being casted
20580 is value-dependent. */
20582 tree type
= TREE_TYPE (expression
);
20584 if (dependent_type_p (type
))
20587 /* A functional cast has a list of operands. */
20588 expression
= TREE_OPERAND (expression
, 0);
20591 /* If there are no operands, it must be an expression such
20592 as "int()". This should not happen for aggregate types
20593 because it would form non-constant expressions. */
20594 gcc_assert (cxx_dialect
>= cxx11
20595 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20600 if (TREE_CODE (expression
) == TREE_LIST
)
20601 return any_value_dependent_elements_p (expression
);
20603 return value_dependent_expression_p (expression
);
20607 if (SIZEOF_EXPR_TYPE_P (expression
))
20608 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20612 /* A `sizeof' expression is value-dependent if the operand is
20613 type-dependent or is a pack expansion. */
20614 expression
= TREE_OPERAND (expression
, 0);
20615 if (PACK_EXPANSION_P (expression
))
20617 else if (TYPE_P (expression
))
20618 return dependent_type_p (expression
);
20619 return instantiation_dependent_expression_p (expression
);
20621 case AT_ENCODE_EXPR
:
20622 /* An 'encode' expression is value-dependent if the operand is
20624 expression
= TREE_OPERAND (expression
, 0);
20625 return dependent_type_p (expression
);
20627 case NOEXCEPT_EXPR
:
20628 expression
= TREE_OPERAND (expression
, 0);
20629 return instantiation_dependent_expression_p (expression
);
20632 /* All instantiation-dependent expressions should also be considered
20633 value-dependent. */
20634 return instantiation_dependent_scope_ref_p (expression
);
20636 case COMPONENT_REF
:
20637 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20638 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20640 case NONTYPE_ARGUMENT_PACK
:
20641 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20642 is value-dependent. */
20644 tree values
= ARGUMENT_PACK_ARGS (expression
);
20645 int i
, len
= TREE_VEC_LENGTH (values
);
20647 for (i
= 0; i
< len
; ++i
)
20648 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20656 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20657 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20658 || (type2
? dependent_type_p (type2
) : false));
20662 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20663 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20666 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20667 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20671 tree op
= TREE_OPERAND (expression
, 0);
20672 return (value_dependent_expression_p (op
)
20673 || has_value_dependent_address (op
));
20678 tree fn
= get_callee_fndecl (expression
);
20680 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20682 nargs
= call_expr_nargs (expression
);
20683 for (i
= 0; i
< nargs
; ++i
)
20685 tree op
= CALL_EXPR_ARG (expression
, i
);
20686 /* In a call to a constexpr member function, look through the
20687 implicit ADDR_EXPR on the object argument so that it doesn't
20688 cause the call to be considered value-dependent. We also
20689 look through it in potential_constant_expression. */
20690 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20691 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20692 && TREE_CODE (op
) == ADDR_EXPR
)
20693 op
= TREE_OPERAND (op
, 0);
20694 if (value_dependent_expression_p (op
))
20700 case TEMPLATE_ID_EXPR
:
20701 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20703 return type_dependent_expression_p (expression
);
20709 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20710 if (value_dependent_expression_p (val
))
20716 /* Treat a GNU statement expression as dependent to avoid crashing
20717 under fold_non_dependent_expr; it can't be constant. */
20721 /* A constant expression is value-dependent if any subexpression is
20722 value-dependent. */
20723 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20725 case tcc_reference
:
20727 case tcc_comparison
:
20729 case tcc_expression
:
20732 int i
, len
= cp_tree_operand_length (expression
);
20734 for (i
= 0; i
< len
; i
++)
20736 tree t
= TREE_OPERAND (expression
, i
);
20738 /* In some cases, some of the operands may be missing.l
20739 (For example, in the case of PREDECREMENT_EXPR, the
20740 amount to increment by may be missing.) That doesn't
20741 make the expression dependent. */
20742 if (t
&& value_dependent_expression_p (t
))
20753 /* The expression is not value-dependent. */
20757 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20758 [temp.dep.expr]. Note that an expression with no type is
20759 considered dependent. Other parts of the compiler arrange for an
20760 expression with type-dependent subexpressions to have no type, so
20761 this function doesn't have to be fully recursive. */
20764 type_dependent_expression_p (tree expression
)
20766 if (!processing_template_decl
)
20769 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20772 /* An unresolved name is always dependent. */
20773 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20776 /* Some expression forms are never type-dependent. */
20777 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20778 || TREE_CODE (expression
) == SIZEOF_EXPR
20779 || TREE_CODE (expression
) == ALIGNOF_EXPR
20780 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20781 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20782 || TREE_CODE (expression
) == TRAIT_EXPR
20783 || TREE_CODE (expression
) == TYPEID_EXPR
20784 || TREE_CODE (expression
) == DELETE_EXPR
20785 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20786 || TREE_CODE (expression
) == THROW_EXPR
)
20789 /* The types of these expressions depends only on the type to which
20790 the cast occurs. */
20791 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20792 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20793 || TREE_CODE (expression
) == CONST_CAST_EXPR
20794 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20795 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20796 || TREE_CODE (expression
) == CAST_EXPR
)
20797 return dependent_type_p (TREE_TYPE (expression
));
20799 /* The types of these expressions depends only on the type created
20800 by the expression. */
20801 if (TREE_CODE (expression
) == NEW_EXPR
20802 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20804 /* For NEW_EXPR tree nodes created inside a template, either
20805 the object type itself or a TREE_LIST may appear as the
20807 tree type
= TREE_OPERAND (expression
, 1);
20808 if (TREE_CODE (type
) == TREE_LIST
)
20809 /* This is an array type. We need to check array dimensions
20811 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20812 || value_dependent_expression_p
20813 (TREE_OPERAND (TREE_VALUE (type
), 1));
20815 return dependent_type_p (type
);
20818 if (TREE_CODE (expression
) == SCOPE_REF
)
20820 tree scope
= TREE_OPERAND (expression
, 0);
20821 tree name
= TREE_OPERAND (expression
, 1);
20823 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20824 contains an identifier associated by name lookup with one or more
20825 declarations declared with a dependent type, or...a
20826 nested-name-specifier or qualified-id that names a member of an
20827 unknown specialization. */
20828 return (type_dependent_expression_p (name
)
20829 || dependent_scope_p (scope
));
20832 if (TREE_CODE (expression
) == FUNCTION_DECL
20833 && DECL_LANG_SPECIFIC (expression
)
20834 && DECL_TEMPLATE_INFO (expression
)
20835 && (any_dependent_template_arguments_p
20836 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20839 if (TREE_CODE (expression
) == TEMPLATE_DECL
20840 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20843 if (TREE_CODE (expression
) == STMT_EXPR
)
20844 expression
= stmt_expr_value_expr (expression
);
20846 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20851 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20853 if (type_dependent_expression_p (elt
))
20859 /* A static data member of the current instantiation with incomplete
20860 array type is type-dependent, as the definition and specializations
20861 can have different bounds. */
20862 if (VAR_P (expression
)
20863 && DECL_CLASS_SCOPE_P (expression
)
20864 && dependent_type_p (DECL_CONTEXT (expression
))
20865 && VAR_HAD_UNKNOWN_BOUND (expression
))
20868 /* An array of unknown bound depending on a variadic parameter, eg:
20870 template<typename... Args>
20871 void foo (Args... args)
20873 int arr[] = { args... };
20876 template<int... vals>
20879 int arr[] = { vals... };
20882 If the array has no length and has an initializer, it must be that
20883 we couldn't determine its length in cp_complete_array_type because
20884 it is dependent. */
20885 if (VAR_P (expression
)
20886 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20887 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20888 && DECL_INITIAL (expression
))
20891 if (TREE_TYPE (expression
) == unknown_type_node
)
20893 if (TREE_CODE (expression
) == ADDR_EXPR
)
20894 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20895 if (TREE_CODE (expression
) == COMPONENT_REF
20896 || TREE_CODE (expression
) == OFFSET_REF
)
20898 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20900 expression
= TREE_OPERAND (expression
, 1);
20901 if (identifier_p (expression
))
20904 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20905 if (TREE_CODE (expression
) == SCOPE_REF
)
20908 /* Always dependent, on the number of arguments if nothing else. */
20909 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20912 if (BASELINK_P (expression
))
20913 expression
= BASELINK_FUNCTIONS (expression
);
20915 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20917 if (any_dependent_template_arguments_p
20918 (TREE_OPERAND (expression
, 1)))
20920 expression
= TREE_OPERAND (expression
, 0);
20922 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20923 || TREE_CODE (expression
) == FUNCTION_DECL
);
20927 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20929 expression
= OVL_NEXT (expression
);
20934 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20936 return (dependent_type_p (TREE_TYPE (expression
)));
20939 /* walk_tree callback function for instantiation_dependent_expression_p,
20940 below. Returns non-zero if a dependent subexpression is found. */
20943 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20948 /* We don't have to worry about decltype currently because decltype
20949 of an instantiation-dependent expr is a dependent type. This
20950 might change depending on the resolution of DR 1172. */
20951 *walk_subtrees
= false;
20954 enum tree_code code
= TREE_CODE (*tp
);
20957 /* Don't treat an argument list as dependent just because it has no
20965 /* A constant with a dependent initializer is dependent. */
20966 if (value_dependent_expression_p (*tp
))
20970 case TEMPLATE_PARM_INDEX
:
20973 /* Handle expressions with type operands. */
20977 case AT_ENCODE_EXPR
:
20979 tree op
= TREE_OPERAND (*tp
, 0);
20980 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20981 op
= TREE_TYPE (op
);
20984 if (dependent_type_p (op
))
20988 *walk_subtrees
= false;
20996 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20997 || (TRAIT_EXPR_TYPE2 (*tp
)
20998 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
21000 *walk_subtrees
= false;
21003 case COMPONENT_REF
:
21004 if (identifier_p (TREE_OPERAND (*tp
, 1)))
21005 /* In a template, finish_class_member_access_expr creates a
21006 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21007 type-dependent, so that we can check access control at
21008 instantiation time (PR 42277). See also Core issue 1273. */
21013 if (instantiation_dependent_scope_ref_p (*tp
))
21018 /* Treat statement-expressions as dependent. */
21026 if (type_dependent_expression_p (*tp
))
21032 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21033 sense defined by the ABI:
21035 "An expression is instantiation-dependent if it is type-dependent
21036 or value-dependent, or it has a subexpression that is type-dependent
21037 or value-dependent." */
21040 instantiation_dependent_expression_p (tree expression
)
21044 if (!processing_template_decl
)
21047 if (expression
== error_mark_node
)
21050 result
= cp_walk_tree_without_duplicates (&expression
,
21051 instantiation_dependent_r
, NULL
);
21052 return result
!= NULL_TREE
;
21055 /* Like type_dependent_expression_p, but it also works while not processing
21056 a template definition, i.e. during substitution or mangling. */
21059 type_dependent_expression_p_push (tree expr
)
21062 ++processing_template_decl
;
21063 b
= type_dependent_expression_p (expr
);
21064 --processing_template_decl
;
21068 /* Returns TRUE if ARGS contains a type-dependent expression. */
21071 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
21076 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
21078 if (type_dependent_expression_p (arg
))
21084 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21085 expressions) contains any type-dependent expressions. */
21088 any_type_dependent_elements_p (const_tree list
)
21090 for (; list
; list
= TREE_CHAIN (list
))
21091 if (type_dependent_expression_p (TREE_VALUE (list
)))
21097 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21098 expressions) contains any value-dependent expressions. */
21101 any_value_dependent_elements_p (const_tree list
)
21103 for (; list
; list
= TREE_CHAIN (list
))
21104 if (value_dependent_expression_p (TREE_VALUE (list
)))
21110 /* Returns TRUE if the ARG (a template argument) is dependent. */
21113 dependent_template_arg_p (tree arg
)
21115 if (!processing_template_decl
)
21118 /* Assume a template argument that was wrongly written by the user
21119 is dependent. This is consistent with what
21120 any_dependent_template_arguments_p [that calls this function]
21122 if (!arg
|| arg
== error_mark_node
)
21125 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
21126 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
21128 if (TREE_CODE (arg
) == TEMPLATE_DECL
21129 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
21130 return dependent_template_p (arg
);
21131 else if (ARGUMENT_PACK_P (arg
))
21133 tree args
= ARGUMENT_PACK_ARGS (arg
);
21134 int i
, len
= TREE_VEC_LENGTH (args
);
21135 for (i
= 0; i
< len
; ++i
)
21137 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21143 else if (TYPE_P (arg
))
21144 return dependent_type_p (arg
);
21146 return (type_dependent_expression_p (arg
)
21147 || value_dependent_expression_p (arg
));
21150 /* Returns true if ARGS (a collection of template arguments) contains
21151 any types that require structural equality testing. */
21154 any_template_arguments_need_structural_equality_p (tree args
)
21161 if (args
== error_mark_node
)
21164 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21166 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21167 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21169 tree arg
= TREE_VEC_ELT (level
, j
);
21170 tree packed_args
= NULL_TREE
;
21173 if (ARGUMENT_PACK_P (arg
))
21175 /* Look inside the argument pack. */
21176 packed_args
= ARGUMENT_PACK_ARGS (arg
);
21177 len
= TREE_VEC_LENGTH (packed_args
);
21180 for (k
= 0; k
< len
; ++k
)
21183 arg
= TREE_VEC_ELT (packed_args
, k
);
21185 if (error_operand_p (arg
))
21187 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
21189 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
21191 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
21192 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
21201 /* Returns true if ARGS (a collection of template arguments) contains
21202 any dependent arguments. */
21205 any_dependent_template_arguments_p (const_tree args
)
21212 if (args
== error_mark_node
)
21215 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21217 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21218 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21219 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21226 /* Returns TRUE if the template TMPL is dependent. */
21229 dependent_template_p (tree tmpl
)
21231 if (TREE_CODE (tmpl
) == OVERLOAD
)
21235 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21237 tmpl
= OVL_NEXT (tmpl
);
21242 /* Template template parameters are dependent. */
21243 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21244 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21246 /* So are names that have not been looked up. */
21247 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21249 /* So are member templates of dependent classes. */
21250 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21251 return dependent_type_p (DECL_CONTEXT (tmpl
));
21255 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21258 dependent_template_id_p (tree tmpl
, tree args
)
21260 return (dependent_template_p (tmpl
)
21261 || any_dependent_template_arguments_p (args
));
21264 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21268 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21272 if (!processing_template_decl
)
21275 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21277 tree decl
= TREE_VEC_ELT (declv
, i
);
21278 tree init
= TREE_VEC_ELT (initv
, i
);
21279 tree cond
= TREE_VEC_ELT (condv
, i
);
21280 tree incr
= TREE_VEC_ELT (incrv
, i
);
21282 if (type_dependent_expression_p (decl
))
21285 if (init
&& type_dependent_expression_p (init
))
21288 if (type_dependent_expression_p (cond
))
21291 if (COMPARISON_CLASS_P (cond
)
21292 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21293 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21296 if (TREE_CODE (incr
) == MODOP_EXPR
)
21298 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21299 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21302 else if (type_dependent_expression_p (incr
))
21304 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21306 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21308 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21310 tree t
= TREE_OPERAND (incr
, 1);
21311 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21312 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21321 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21322 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21323 no such TYPE can be found. Note that this function peers inside
21324 uninstantiated templates and therefore should be used only in
21325 extremely limited situations. ONLY_CURRENT_P restricts this
21326 peering to the currently open classes hierarchy (which is required
21327 when comparing types). */
21330 resolve_typename_type (tree type
, bool only_current_p
)
21339 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21341 scope
= TYPE_CONTEXT (type
);
21342 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21343 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21344 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21345 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21346 identifier of the TYPENAME_TYPE anymore.
21347 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21348 TYPENAME_TYPE instead, we avoid messing up with a possible
21349 typedef variant case. */
21350 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21352 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21353 it first before we can figure out what NAME refers to. */
21354 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21356 if (TYPENAME_IS_RESOLVING_P (scope
))
21357 /* Given a class template A with a dependent base with nested type C,
21358 typedef typename A::C::C C will land us here, as trying to resolve
21359 the initial A::C leads to the local C typedef, which leads back to
21360 A::C::C. So we break the recursion now. */
21363 scope
= resolve_typename_type (scope
, only_current_p
);
21365 /* If we don't know what SCOPE refers to, then we cannot resolve the
21367 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21369 /* If the SCOPE is a template type parameter, we have no way of
21370 resolving the name. */
21371 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21373 /* If the SCOPE is not the current instantiation, there's no reason
21374 to look inside it. */
21375 if (only_current_p
&& !currently_open_class (scope
))
21377 /* If this is a typedef, we don't want to look inside (c++/11987). */
21378 if (typedef_variant_p (type
))
21380 /* If SCOPE isn't the template itself, it will not have a valid
21381 TYPE_FIELDS list. */
21382 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21383 /* scope is either the template itself or a compatible instantiation
21384 like X<T>, so look up the name in the original template. */
21385 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21387 /* scope is a partial instantiation, so we can't do the lookup or we
21388 will lose the template arguments. */
21390 /* Enter the SCOPE so that name lookup will be resolved as if we
21391 were in the class definition. In particular, SCOPE will no
21392 longer be considered a dependent type. */
21393 pushed_scope
= push_scope (scope
);
21394 /* Look up the declaration. */
21395 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21396 tf_warning_or_error
);
21398 result
= NULL_TREE
;
21400 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21401 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21404 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21405 && TREE_CODE (decl
) == TYPE_DECL
)
21407 result
= TREE_TYPE (decl
);
21408 if (result
== error_mark_node
)
21409 result
= NULL_TREE
;
21411 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21412 && DECL_CLASS_TEMPLATE_P (decl
))
21416 /* Obtain the template and the arguments. */
21417 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21418 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21419 /* Instantiate the template. */
21420 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21421 /*entering_scope=*/0,
21422 tf_error
| tf_user
);
21423 if (result
== error_mark_node
)
21424 result
= NULL_TREE
;
21427 /* Leave the SCOPE. */
21429 pop_scope (pushed_scope
);
21431 /* If we failed to resolve it, return the original typename. */
21435 /* If lookup found a typename type, resolve that too. */
21436 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21438 /* Ill-formed programs can cause infinite recursion here, so we
21439 must catch that. */
21440 TYPENAME_IS_RESOLVING_P (type
) = 1;
21441 result
= resolve_typename_type (result
, only_current_p
);
21442 TYPENAME_IS_RESOLVING_P (type
) = 0;
21445 /* Qualify the resulting type. */
21446 quals
= cp_type_quals (type
);
21448 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21453 /* EXPR is an expression which is not type-dependent. Return a proxy
21454 for EXPR that can be used to compute the types of larger
21455 expressions containing EXPR. */
21458 build_non_dependent_expr (tree expr
)
21462 #ifdef ENABLE_CHECKING
21463 /* Try to get a constant value for all non-dependent expressions in
21464 order to expose bugs in *_dependent_expression_p and constexpr. */
21465 if (cxx_dialect
>= cxx11
)
21466 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21469 /* Preserve OVERLOADs; the functions must be available to resolve
21472 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21473 inner_expr
= stmt_expr_value_expr (inner_expr
);
21474 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21475 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21476 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21477 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21478 if (is_overloaded_fn (inner_expr
)
21479 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21481 /* There is no need to return a proxy for a variable. */
21484 /* Preserve string constants; conversions from string constants to
21485 "char *" are allowed, even though normally a "const char *"
21486 cannot be used to initialize a "char *". */
21487 if (TREE_CODE (expr
) == STRING_CST
)
21489 /* Preserve arithmetic constants, as an optimization -- there is no
21490 reason to create a new node. */
21491 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21493 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21494 There is at least one place where we want to know that a
21495 particular expression is a throw-expression: when checking a ?:
21496 expression, there are special rules if the second or third
21497 argument is a throw-expression. */
21498 if (TREE_CODE (expr
) == THROW_EXPR
)
21501 /* Don't wrap an initializer list, we need to be able to look inside. */
21502 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21505 /* Don't wrap a dummy object, we need to be able to test for it. */
21506 if (is_dummy_object (expr
))
21509 if (TREE_CODE (expr
) == COND_EXPR
)
21510 return build3 (COND_EXPR
,
21512 TREE_OPERAND (expr
, 0),
21513 (TREE_OPERAND (expr
, 1)
21514 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21515 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21516 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21517 if (TREE_CODE (expr
) == COMPOUND_EXPR
21518 && !COMPOUND_EXPR_OVERLOADED (expr
))
21519 return build2 (COMPOUND_EXPR
,
21521 TREE_OPERAND (expr
, 0),
21522 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21524 /* If the type is unknown, it can't really be non-dependent */
21525 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21527 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21528 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21531 /* ARGS is a vector of expressions as arguments to a function call.
21532 Replace the arguments with equivalent non-dependent expressions.
21533 This modifies ARGS in place. */
21536 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21541 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21543 tree newarg
= build_non_dependent_expr (arg
);
21545 (*args
)[ix
] = newarg
;
21549 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21550 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21554 make_auto_1 (tree name
)
21556 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21557 TYPE_NAME (au
) = build_decl (input_location
,
21558 TYPE_DECL
, name
, au
);
21559 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21560 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21561 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21562 TYPE_NAME (au
), NULL_TREE
);
21563 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21564 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21565 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21571 make_decltype_auto (void)
21573 return make_auto_1 (get_identifier ("decltype(auto)"));
21579 return make_auto_1 (get_identifier ("auto"));
21582 /* Given type ARG, return std::initializer_list<ARG>. */
21587 tree std_init_list
= namespace_binding
21588 (get_identifier ("initializer_list"), std_node
);
21590 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21592 error ("deducing from brace-enclosed initializer list requires "
21593 "#include <initializer_list>");
21594 return error_mark_node
;
21596 argvec
= make_tree_vec (1);
21597 TREE_VEC_ELT (argvec
, 0) = arg
;
21598 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21599 NULL_TREE
, 0, tf_warning_or_error
);
21602 /* Replace auto in TYPE with std::initializer_list<auto>. */
21605 listify_autos (tree type
, tree auto_node
)
21607 tree init_auto
= listify (auto_node
);
21608 tree argvec
= make_tree_vec (1);
21609 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21610 if (processing_template_decl
)
21611 argvec
= add_to_template_args (current_template_args (), argvec
);
21612 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21615 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21616 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21619 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21623 if (init
== error_mark_node
)
21624 return error_mark_node
;
21626 if (type_dependent_expression_p (init
))
21627 /* Defining a subset of type-dependent expressions that we can deduce
21628 from ahead of time isn't worth the trouble. */
21631 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21632 with either a new invented type template parameter U or, if the
21633 initializer is a braced-init-list (8.5.4), with
21634 std::initializer_list<U>. */
21635 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21636 type
= listify_autos (type
, auto_node
);
21638 init
= resolve_nondeduced_context (init
);
21640 targs
= make_tree_vec (1);
21641 if (AUTO_IS_DECLTYPE (auto_node
))
21643 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
21644 && !REF_PARENTHESIZED_P (init
)));
21645 TREE_VEC_ELT (targs
, 0)
21646 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21647 if (type
!= auto_node
)
21649 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21650 return error_mark_node
;
21655 tree parms
= build_tree_list (NULL_TREE
, type
);
21656 tree tparms
= make_tree_vec (1);
21659 TREE_VEC_ELT (tparms
, 0)
21660 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21661 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21662 DEDUCE_CALL
, LOOKUP_NORMAL
,
21663 NULL
, /*explain_p=*/false);
21666 if (processing_template_decl
)
21667 /* Try again at instantiation time. */
21669 if (type
&& type
!= error_mark_node
)
21670 /* If type is error_mark_node a diagnostic must have been
21671 emitted by now. Also, having a mention to '<type error>'
21672 in the diagnostic is not really useful to the user. */
21674 if (cfun
&& auto_node
== current_function_auto_return_pattern
21675 && LAMBDA_FUNCTION_P (current_function_decl
))
21676 error ("unable to deduce lambda return type from %qE", init
);
21678 error ("unable to deduce %qT from %qE", type
, init
);
21680 return error_mark_node
;
21684 /* If the list of declarators contains more than one declarator, the type
21685 of each declared variable is determined as described above. If the
21686 type deduced for the template parameter U is not the same in each
21687 deduction, the program is ill-formed. */
21688 if (TREE_TYPE (auto_node
)
21689 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21691 if (cfun
&& auto_node
== current_function_auto_return_pattern
21692 && LAMBDA_FUNCTION_P (current_function_decl
))
21693 error ("inconsistent types %qT and %qT deduced for "
21694 "lambda return type", TREE_TYPE (auto_node
),
21695 TREE_VEC_ELT (targs
, 0));
21697 error ("inconsistent deduction for %qT: %qT and then %qT",
21698 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21699 return error_mark_node
;
21701 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21703 if (processing_template_decl
)
21704 targs
= add_to_template_args (current_template_args (), targs
);
21705 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21708 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21712 splice_late_return_type (tree type
, tree late_return_type
)
21716 if (late_return_type
== NULL_TREE
)
21718 argvec
= make_tree_vec (1);
21719 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21720 if (processing_template_parmlist
)
21721 /* For a late-specified return type in a template type-parameter, we
21722 need to add a dummy argument level for its parmlist. */
21723 argvec
= add_to_template_args
21724 (make_tree_vec (processing_template_parmlist
), argvec
);
21725 if (current_template_parms
)
21726 argvec
= add_to_template_args (current_template_args (), argvec
);
21727 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21730 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21731 'decltype(auto)'. */
21734 is_auto (const_tree type
)
21736 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21737 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21738 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21744 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21745 a use of `auto'. Returns NULL_TREE otherwise. */
21748 type_uses_auto (tree type
)
21750 return find_type_usage (type
, is_auto
);
21753 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21754 'decltype(auto)' or a concept. */
21757 is_auto_or_concept (const_tree type
)
21759 return is_auto (type
); // or concept
21762 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21763 a concept identifier) iff TYPE contains a use of a generic type. Returns
21764 NULL_TREE otherwise. */
21767 type_uses_auto_or_concept (tree type
)
21769 return find_type_usage (type
, is_auto_or_concept
);
21773 /* For a given template T, return the vector of typedefs referenced
21774 in T for which access check is needed at T instantiation time.
21775 T is either a FUNCTION_DECL or a RECORD_TYPE.
21776 Those typedefs were added to T by the function
21777 append_type_to_template_for_access_check. */
21779 vec
<qualified_typedef_usage_t
, va_gc
> *
21780 get_types_needing_access_check (tree t
)
21783 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21785 if (!t
|| t
== error_mark_node
)
21788 if (!(ti
= get_template_info (t
)))
21791 if (CLASS_TYPE_P (t
)
21792 || TREE_CODE (t
) == FUNCTION_DECL
)
21794 if (!TI_TEMPLATE (ti
))
21797 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21803 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21804 tied to T. That list of typedefs will be access checked at
21805 T instantiation time.
21806 T is either a FUNCTION_DECL or a RECORD_TYPE.
21807 TYPE_DECL is a TYPE_DECL node representing a typedef.
21808 SCOPE is the scope through which TYPE_DECL is accessed.
21809 LOCATION is the location of the usage point of TYPE_DECL.
21811 This function is a subroutine of
21812 append_type_to_template_for_access_check. */
21815 append_type_to_template_for_access_check_1 (tree t
,
21818 location_t location
)
21820 qualified_typedef_usage_t typedef_usage
;
21823 if (!t
|| t
== error_mark_node
)
21826 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21827 || CLASS_TYPE_P (t
))
21829 && TREE_CODE (type_decl
) == TYPE_DECL
21832 if (!(ti
= get_template_info (t
)))
21835 gcc_assert (TI_TEMPLATE (ti
));
21837 typedef_usage
.typedef_decl
= type_decl
;
21838 typedef_usage
.context
= scope
;
21839 typedef_usage
.locus
= location
;
21841 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21844 /* Append TYPE_DECL to the template TEMPL.
21845 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21846 At TEMPL instanciation time, TYPE_DECL will be checked to see
21847 if it can be accessed through SCOPE.
21848 LOCATION is the location of the usage point of TYPE_DECL.
21850 e.g. consider the following code snippet:
21857 template<class U> struct S
21859 C::myint mi; // <-- usage point of the typedef C::myint
21864 At S<char> instantiation time, we need to check the access of C::myint
21865 In other words, we need to check the access of the myint typedef through
21866 the C scope. For that purpose, this function will add the myint typedef
21867 and the scope C through which its being accessed to a list of typedefs
21868 tied to the template S. That list will be walked at template instantiation
21869 time and access check performed on each typedefs it contains.
21870 Note that this particular code snippet should yield an error because
21871 myint is private to C. */
21874 append_type_to_template_for_access_check (tree templ
,
21877 location_t location
)
21879 qualified_typedef_usage_t
*iter
;
21882 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21884 /* Make sure we don't append the type to the template twice. */
21885 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21886 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21889 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21893 /* Convert the generic type parameters in PARM that match the types given in the
21894 range [START_IDX, END_IDX) from the current_template_parms into generic type
21898 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21900 tree current
= current_template_parms
;
21901 int depth
= TMPL_PARMS_DEPTH (current
);
21902 current
= INNERMOST_TEMPLATE_PARMS (current
);
21903 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21905 for (int i
= 0; i
< start_idx
; ++i
)
21906 TREE_VEC_ELT (replacement
, i
)
21907 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21909 for (int i
= start_idx
; i
< end_idx
; ++i
)
21911 /* Create a distinct parameter pack type from the current parm and add it
21912 to the replacement args to tsubst below into the generic function
21915 tree o
= TREE_TYPE (TREE_VALUE
21916 (TREE_VEC_ELT (current
, i
)));
21917 tree t
= copy_type (o
);
21918 TEMPLATE_TYPE_PARM_INDEX (t
)
21919 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21921 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21922 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21923 TYPE_MAIN_VARIANT (t
) = t
;
21924 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21925 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21926 TREE_VEC_ELT (replacement
, i
) = t
;
21927 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21930 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21931 TREE_VEC_ELT (replacement
, i
)
21932 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21934 /* If there are more levels then build up the replacement with the outer
21937 replacement
= add_to_template_args (template_parms_to_args
21938 (TREE_CHAIN (current_template_parms
)),
21941 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21945 /* Set up the hash tables for template instantiations. */
21948 init_template_processing (void)
21950 decl_specializations
= htab_create_ggc (37,
21951 hash_specialization
,
21952 eq_specializations
,
21954 type_specializations
= htab_create_ggc (37,
21955 hash_specialization
,
21956 eq_specializations
,
21960 /* Print stats about the template hash tables for -fstats. */
21963 print_template_statistics (void)
21965 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21966 "%f collisions\n", (long) htab_size (decl_specializations
),
21967 (long) htab_elements (decl_specializations
),
21968 htab_collisions (decl_specializations
));
21969 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21970 "%f collisions\n", (long) htab_size (type_specializations
),
21971 (long) htab_elements (type_specializations
),
21972 htab_collisions (type_specializations
));
21975 #include "gt-cp-pt.h"