Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / pt.c
blob020a4bf2f6d620ca91e4400de9f291e781a750e7
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 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)
11 any later version.
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".
27 Fixed by: C++20 modules. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "cp-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "intl.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "toplev.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
44 #include "gimplify.h"
45 #include "gcc-rich-location.h"
46 #include "selftest.h"
47 #include "target.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static vec<int> inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) vec<tree, va_gc> *saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 // -------------------------------------------------------------------------- //
80 // Local Specialization Stack
82 // Implementation of the RAII helper for creating new local
83 // specializations.
84 local_specialization_stack::local_specialization_stack (lss_policy policy)
85 : saved (local_specializations)
87 if (policy == lss_nop)
89 else if (policy == lss_blank || !saved)
90 local_specializations = new hash_map<tree, tree>;
91 else
92 local_specializations = new hash_map<tree, tree>(*saved);
95 local_specialization_stack::~local_specialization_stack ()
97 if (local_specializations != saved)
99 delete local_specializations;
100 local_specializations = saved;
104 /* True if we've recursed into fn_type_unification too many times. */
105 static bool excessive_deduction_depth;
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
113 /* The general template is not in these tables. */
114 typedef hash_table<spec_hasher> spec_hash_table;
115 static GTY (()) spec_hash_table *decl_specializations;
116 static GTY (()) spec_hash_table *type_specializations;
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int check_non_deducible_conversion (tree, tree, int, int,
156 struct conversion **, bool);
157 static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
158 tree*, tree*, tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187 tree most_specialized_partial_spec (tree, tsubst_flags_t);
188 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
189 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
191 static bool check_specialization_scope (void);
192 static tree process_partial_specialization (tree);
193 static enum template_base_result get_template_base (tree, tree, tree, tree,
194 bool , tree *);
195 static tree try_class_unification (tree, tree, tree, tree, bool);
196 static bool class_nttp_const_wrapper_p (tree t);
197 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
198 tree, tree);
199 static bool template_template_parm_bindings_ok_p (tree, tree);
200 static void tsubst_default_arguments (tree, tsubst_flags_t);
201 static tree for_each_template_parm_r (tree *, int *, void *);
202 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
203 static void copy_default_args_to_explicit_spec (tree);
204 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
205 static bool dependent_template_arg_p (tree);
206 static bool any_template_arguments_need_structural_equality_p (tree);
207 static bool dependent_type_p_r (tree);
208 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_decl (tree, tree, tsubst_flags_t);
210 static void perform_instantiation_time_access_checks (tree, tree);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree get_underlying_template (tree);
217 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
218 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 static tree make_argument_pack (tree);
220 static void register_parameter_specializations (tree, tree);
221 static tree enclosing_instantiation_of (tree tctx);
222 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 for a class or alias template (needed by instantiate_decl). */
229 void
230 push_access_scope (tree t)
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
233 || TREE_CODE (t) == TYPE_DECL);
235 if (DECL_FRIEND_CONTEXT (t))
236 push_nested_class (DECL_FRIEND_CONTEXT (t));
237 else if (DECL_IMPLICIT_TYPEDEF_P (t)
238 && CLASS_TYPE_P (TREE_TYPE (t)))
239 push_nested_class (TREE_TYPE (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
243 /* An artificial deduction guide should have the same access as
244 the constructor. */
245 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
246 else
247 push_to_top_level ();
249 if (TREE_CODE (t) == FUNCTION_DECL)
251 vec_safe_push (saved_access_scope, current_function_decl);
252 current_function_decl = t;
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
259 void
260 pop_access_scope (tree t)
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 current_function_decl = saved_access_scope->pop();
265 if (DECL_FRIEND_CONTEXT (t)
266 || (DECL_IMPLICIT_TYPEDEF_P (t)
267 && CLASS_TYPE_P (TREE_TYPE (t)))
268 || DECL_CLASS_SCOPE_P (t)
269 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
270 pop_nested_class ();
271 else
272 pop_from_top_level ();
275 /* Do any processing required when DECL (a member template
276 declaration) is finished. Returns the TEMPLATE_DECL corresponding
277 to DECL, unless it is a specialization, in which case the DECL
278 itself is returned. */
280 tree
281 finish_member_template_decl (tree decl)
283 if (decl == error_mark_node)
284 return error_mark_node;
286 gcc_assert (DECL_P (decl));
288 if (TREE_CODE (decl) == TYPE_DECL)
290 tree type;
292 type = TREE_TYPE (decl);
293 if (type == error_mark_node)
294 return error_mark_node;
295 if (MAYBE_CLASS_TYPE_P (type)
296 && CLASSTYPE_TEMPLATE_INFO (type)
297 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
299 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
300 check_member_template (tmpl);
301 return tmpl;
303 return NULL_TREE;
305 else if (TREE_CODE (decl) == FIELD_DECL)
306 error_at (DECL_SOURCE_LOCATION (decl),
307 "data member %qD cannot be a member template", decl);
308 else if (DECL_TEMPLATE_INFO (decl))
310 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
312 check_member_template (DECL_TI_TEMPLATE (decl));
313 return DECL_TI_TEMPLATE (decl);
315 else
316 return decl;
318 else
319 error_at (DECL_SOURCE_LOCATION (decl),
320 "invalid member template declaration %qD", decl);
322 return error_mark_node;
325 /* Create a template info node. */
327 tree
328 build_template_info (tree template_decl, tree template_args)
330 tree result = make_node (TEMPLATE_INFO);
331 TI_TEMPLATE (result) = template_decl;
332 TI_ARGS (result) = template_args;
333 return result;
336 /* Return the template info node corresponding to T, whatever T is. */
338 tree
339 get_template_info (const_tree t)
341 tree tinfo = NULL_TREE;
343 if (!t || t == error_mark_node)
344 return NULL;
346 if (TREE_CODE (t) == NAMESPACE_DECL
347 || TREE_CODE (t) == PARM_DECL)
348 return NULL;
350 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
351 tinfo = DECL_TEMPLATE_INFO (t);
353 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
354 t = TREE_TYPE (t);
356 if (OVERLOAD_TYPE_P (t))
357 tinfo = TYPE_TEMPLATE_INFO (t);
358 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
359 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
361 return tinfo;
364 /* Returns the template nesting level of the indicated class TYPE.
366 For example, in:
367 template <class T>
368 struct A
370 template <class U>
371 struct B {};
374 A<T>::B<U> has depth two, while A<T> has depth one.
375 Both A<T>::B<int> and A<int>::B<U> have depth one, if
376 they are instantiations, not specializations.
378 This function is guaranteed to return 0 if passed NULL_TREE so
379 that, for example, `template_class_depth (current_class_type)' is
380 always safe. */
383 template_class_depth (tree type)
385 int depth;
387 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
389 tree tinfo = get_template_info (type);
391 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
392 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
393 ++depth;
395 if (DECL_P (type))
397 if (tree fctx = DECL_FRIEND_CONTEXT (type))
398 type = fctx;
399 else
400 type = CP_DECL_CONTEXT (type);
402 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
403 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
404 else
405 type = CP_TYPE_CONTEXT (type);
408 return depth;
411 /* Return TRUE if NODE instantiates a template that has arguments of
412 its own, be it directly a primary template or indirectly through a
413 partial specializations. */
414 static bool
415 instantiates_primary_template_p (tree node)
417 tree tinfo = get_template_info (node);
418 if (!tinfo)
419 return false;
421 tree tmpl = TI_TEMPLATE (tinfo);
422 if (PRIMARY_TEMPLATE_P (tmpl))
423 return true;
425 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
426 return false;
428 /* So now we know we have a specialization, but it could be a full
429 or a partial specialization. To tell which, compare the depth of
430 its template arguments with those of its context. */
432 tree ctxt = DECL_CONTEXT (tmpl);
433 tree ctinfo = get_template_info (ctxt);
434 if (!ctinfo)
435 return true;
437 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
438 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
441 /* Subroutine of maybe_begin_member_template_processing.
442 Returns true if processing DECL needs us to push template parms. */
444 static bool
445 inline_needs_template_parms (tree decl, bool nsdmi)
447 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
448 return false;
450 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
451 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
454 /* Subroutine of maybe_begin_member_template_processing.
455 Push the template parms in PARMS, starting from LEVELS steps into the
456 chain, and ending at the beginning, since template parms are listed
457 innermost first. */
459 static void
460 push_inline_template_parms_recursive (tree parmlist, int levels)
462 tree parms = TREE_VALUE (parmlist);
463 int i;
465 if (levels > 1)
466 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
468 ++processing_template_decl;
469 current_template_parms
470 = tree_cons (size_int (processing_template_decl),
471 parms, current_template_parms);
472 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
474 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
475 NULL);
476 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
478 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
480 if (error_operand_p (parm))
481 continue;
483 gcc_assert (DECL_P (parm));
485 switch (TREE_CODE (parm))
487 case TYPE_DECL:
488 case TEMPLATE_DECL:
489 pushdecl (parm);
490 break;
492 case PARM_DECL:
493 /* Push the CONST_DECL. */
494 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
495 break;
497 default:
498 gcc_unreachable ();
503 /* Restore the template parameter context for a member template, a
504 friend template defined in a class definition, or a non-template
505 member of template class. */
507 void
508 maybe_begin_member_template_processing (tree decl)
510 tree parms;
511 int levels = 0;
512 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
514 if (nsdmi)
516 tree ctx = DECL_CONTEXT (decl);
517 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
518 /* Disregard full specializations (c++/60999). */
519 && uses_template_parms (ctx)
520 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
523 if (inline_needs_template_parms (decl, nsdmi))
525 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
526 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
528 if (DECL_TEMPLATE_SPECIALIZATION (decl))
530 --levels;
531 parms = TREE_CHAIN (parms);
534 push_inline_template_parms_recursive (parms, levels);
537 /* Remember how many levels of template parameters we pushed so that
538 we can pop them later. */
539 inline_parm_levels.safe_push (levels);
542 /* Undo the effects of maybe_begin_member_template_processing. */
544 void
545 maybe_end_member_template_processing (void)
547 int i;
548 int last;
550 if (inline_parm_levels.length () == 0)
551 return;
553 last = inline_parm_levels.pop ();
554 for (i = 0; i < last; ++i)
556 --processing_template_decl;
557 current_template_parms = TREE_CHAIN (current_template_parms);
558 poplevel (0, 0, 0);
562 /* Return a new template argument vector which contains all of ARGS,
563 but has as its innermost set of arguments the EXTRA_ARGS. */
565 tree
566 add_to_template_args (tree args, tree extra_args)
568 tree new_args;
569 int extra_depth;
570 int i;
571 int j;
573 if (args == NULL_TREE || extra_args == error_mark_node)
574 return extra_args;
576 extra_depth = TMPL_ARGS_DEPTH (extra_args);
577 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
579 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
582 for (j = 1; j <= extra_depth; ++j, ++i)
583 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
585 return new_args;
588 /* Like add_to_template_args, but only the outermost ARGS are added to
589 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
590 (EXTRA_ARGS) levels are added. This function is used to combine
591 the template arguments from a partial instantiation with the
592 template arguments used to attain the full instantiation from the
593 partial instantiation.
595 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
597 tree
598 add_outermost_template_args (tree args, tree extra_args)
600 tree new_args;
602 if (!args)
603 return extra_args;
604 if (TREE_CODE (args) == TEMPLATE_DECL)
606 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
607 args = TI_ARGS (ti);
610 /* If there are more levels of EXTRA_ARGS than there are ARGS,
611 something very fishy is going on. */
612 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
614 /* If *all* the new arguments will be the EXTRA_ARGS, just return
615 them. */
616 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
617 return extra_args;
619 /* For the moment, we make ARGS look like it contains fewer levels. */
620 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
622 new_args = add_to_template_args (args, extra_args);
624 /* Now, we restore ARGS to its full dimensions. */
625 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
627 return new_args;
630 /* Return the N levels of innermost template arguments from the ARGS. */
632 tree
633 get_innermost_template_args (tree args, int n)
635 tree new_args;
636 int extra_levels;
637 int i;
639 gcc_assert (n >= 0);
641 /* If N is 1, just return the innermost set of template arguments. */
642 if (n == 1)
643 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
645 /* If we're not removing anything, just return the arguments we were
646 given. */
647 extra_levels = TMPL_ARGS_DEPTH (args) - n;
648 gcc_assert (extra_levels >= 0);
649 if (extra_levels == 0)
650 return args;
652 /* Make a new set of arguments, not containing the outer arguments. */
653 new_args = make_tree_vec (n);
654 for (i = 1; i <= n; ++i)
655 SET_TMPL_ARGS_LEVEL (new_args, i,
656 TMPL_ARGS_LEVEL (args, i + extra_levels));
658 return new_args;
661 /* The inverse of get_innermost_template_args: Return all but the innermost
662 EXTRA_LEVELS levels of template arguments from the ARGS. */
664 static tree
665 strip_innermost_template_args (tree args, int extra_levels)
667 tree new_args;
668 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
669 int i;
671 gcc_assert (n >= 0);
673 /* If N is 1, just return the outermost set of template arguments. */
674 if (n == 1)
675 return TMPL_ARGS_LEVEL (args, 1);
677 /* If we're not removing anything, just return the arguments we were
678 given. */
679 gcc_assert (extra_levels >= 0);
680 if (extra_levels == 0)
681 return args;
683 /* Make a new set of arguments, not containing the inner arguments. */
684 new_args = make_tree_vec (n);
685 for (i = 1; i <= n; ++i)
686 SET_TMPL_ARGS_LEVEL (new_args, i,
687 TMPL_ARGS_LEVEL (args, i));
689 return new_args;
692 /* We've got a template header coming up; push to a new level for storing
693 the parms. */
695 void
696 begin_template_parm_list (void)
698 /* We use a non-tag-transparent scope here, which causes pushtag to
699 put tags in this scope, rather than in the enclosing class or
700 namespace scope. This is the right thing, since we want
701 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
702 global template class, push_template_decl handles putting the
703 TEMPLATE_DECL into top-level scope. For a nested template class,
704 e.g.:
706 template <class T> struct S1 {
707 template <class T> struct S2 {};
710 pushtag contains special code to insert the TEMPLATE_DECL for S2
711 at the right scope. */
712 begin_scope (sk_template_parms, NULL);
713 ++processing_template_decl;
714 ++processing_template_parmlist;
715 note_template_header (0);
717 /* Add a dummy parameter level while we process the parameter list. */
718 current_template_parms
719 = tree_cons (size_int (processing_template_decl),
720 make_tree_vec (0),
721 current_template_parms);
724 /* This routine is called when a specialization is declared. If it is
725 invalid to declare a specialization here, an error is reported and
726 false is returned, otherwise this routine will return true. */
728 static bool
729 check_specialization_scope (void)
731 tree scope = current_scope ();
733 /* [temp.expl.spec]
735 An explicit specialization shall be declared in the namespace of
736 which the template is a member, or, for member templates, in the
737 namespace of which the enclosing class or enclosing class
738 template is a member. An explicit specialization of a member
739 function, member class or static data member of a class template
740 shall be declared in the namespace of which the class template
741 is a member. */
742 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
744 error ("explicit specialization in non-namespace scope %qD", scope);
745 return false;
748 /* [temp.expl.spec]
750 In an explicit specialization declaration for a member of a class
751 template or a member template that appears in namespace scope,
752 the member template and some of its enclosing class templates may
753 remain unspecialized, except that the declaration shall not
754 explicitly specialize a class member template if its enclosing
755 class templates are not explicitly specialized as well. */
756 if (current_template_parms)
758 error ("enclosing class templates are not explicitly specialized");
759 return false;
762 return true;
765 /* We've just seen template <>. */
767 bool
768 begin_specialization (void)
770 begin_scope (sk_template_spec, NULL);
771 note_template_header (1);
772 return check_specialization_scope ();
775 /* Called at then end of processing a declaration preceded by
776 template<>. */
778 void
779 end_specialization (void)
781 finish_scope ();
782 reset_specialization ();
785 /* Any template <>'s that we have seen thus far are not referring to a
786 function specialization. */
788 void
789 reset_specialization (void)
791 processing_specialization = 0;
792 template_header_count = 0;
795 /* We've just seen a template header. If SPECIALIZATION is nonzero,
796 it was of the form template <>. */
798 static void
799 note_template_header (int specialization)
801 processing_specialization = specialization;
802 template_header_count++;
805 /* We're beginning an explicit instantiation. */
807 void
808 begin_explicit_instantiation (void)
810 gcc_assert (!processing_explicit_instantiation);
811 processing_explicit_instantiation = true;
815 void
816 end_explicit_instantiation (void)
818 gcc_assert (processing_explicit_instantiation);
819 processing_explicit_instantiation = false;
822 /* An explicit specialization or partial specialization of TMPL is being
823 declared. Check that the namespace in which the specialization is
824 occurring is permissible. Returns false iff it is invalid to
825 specialize TMPL in the current namespace. */
827 static bool
828 check_specialization_namespace (tree tmpl)
830 tree tpl_ns = decl_namespace_context (tmpl);
832 /* [tmpl.expl.spec]
834 An explicit specialization shall be declared in a namespace enclosing the
835 specialized template. An explicit specialization whose declarator-id is
836 not qualified shall be declared in the nearest enclosing namespace of the
837 template, or, if the namespace is inline (7.3.1), any namespace from its
838 enclosing namespace set. */
839 if (current_scope() != DECL_CONTEXT (tmpl)
840 && !at_namespace_scope_p ())
842 error ("specialization of %qD must appear at namespace scope", tmpl);
843 return false;
846 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
847 /* Same or enclosing namespace. */
848 return true;
849 else
851 auto_diagnostic_group d;
852 if (permerror (input_location,
853 "specialization of %qD in different namespace", tmpl))
854 inform (DECL_SOURCE_LOCATION (tmpl),
855 " from definition of %q#D", tmpl);
856 return false;
860 /* SPEC is an explicit instantiation. Check that it is valid to
861 perform this explicit instantiation in the current namespace. */
863 static void
864 check_explicit_instantiation_namespace (tree spec)
866 tree ns;
868 /* DR 275: An explicit instantiation shall appear in an enclosing
869 namespace of its template. */
870 ns = decl_namespace_context (spec);
871 if (!is_nested_namespace (current_namespace, ns))
872 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
873 "(which does not enclose namespace %qD)",
874 spec, current_namespace, ns);
877 /* Returns the type of a template specialization only if that
878 specialization needs to be defined. Otherwise (e.g., if the type has
879 already been defined), the function returns NULL_TREE. */
881 static tree
882 maybe_new_partial_specialization (tree type)
884 /* An implicit instantiation of an incomplete type implies
885 the definition of a new class template.
887 template<typename T>
888 struct S;
890 template<typename T>
891 struct S<T*>;
893 Here, S<T*> is an implicit instantiation of S whose type
894 is incomplete. */
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
896 return type;
898 /* It can also be the case that TYPE is a completed specialization.
899 Continuing the previous example, suppose we also declare:
901 template<typename T>
902 requires Integral<T>
903 struct S<T*>;
905 Here, S<T*> refers to the specialization S<T*> defined
906 above. However, we need to differentiate definitions because
907 we intend to define a new partial specialization. In this case,
908 we rely on the fact that the constraints are different for
909 this declaration than that above.
911 Note that we also get here for injected class names and
912 late-parsed template definitions. We must ensure that we
913 do not create new type declarations for those cases. */
914 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
916 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
917 tree args = CLASSTYPE_TI_ARGS (type);
919 /* If there are no template parameters, this cannot be a new
920 partial template specialization? */
921 if (!current_template_parms)
922 return NULL_TREE;
924 /* The injected-class-name is not a new partial specialization. */
925 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
926 return NULL_TREE;
928 /* If the constraints are not the same as those of the primary
929 then, we can probably create a new specialization. */
930 tree type_constr = current_template_constraints ();
932 if (type == TREE_TYPE (tmpl))
934 tree main_constr = get_constraints (tmpl);
935 if (equivalent_constraints (type_constr, main_constr))
936 return NULL_TREE;
939 /* Also, if there's a pre-existing specialization with matching
940 constraints, then this also isn't new. */
941 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
942 while (specs)
944 tree spec_tmpl = TREE_VALUE (specs);
945 tree spec_args = TREE_PURPOSE (specs);
946 tree spec_constr = get_constraints (spec_tmpl);
947 if (comp_template_args (args, spec_args)
948 && equivalent_constraints (type_constr, spec_constr))
949 return NULL_TREE;
950 specs = TREE_CHAIN (specs);
953 /* Create a new type node (and corresponding type decl)
954 for the newly declared specialization. */
955 tree t = make_class_type (TREE_CODE (type));
956 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
957 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
959 /* We only need a separate type node for storing the definition of this
960 partial specialization; uses of S<T*> are unconstrained, so all are
961 equivalent. So keep TYPE_CANONICAL the same. */
962 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
964 /* Build the corresponding type decl. */
965 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
966 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
967 DECL_SOURCE_LOCATION (d) = input_location;
968 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
969 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
971 set_instantiating_module (d);
972 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
974 return t;
977 return NULL_TREE;
980 /* The TYPE is being declared. If it is a template type, that means it
981 is a partial specialization. Do appropriate error-checking. */
983 tree
984 maybe_process_partial_specialization (tree type)
986 tree context;
988 if (type == error_mark_node)
989 return error_mark_node;
991 /* A lambda that appears in specialization context is not itself a
992 specialization. */
993 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
994 return type;
996 /* An injected-class-name is not a specialization. */
997 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
998 return type;
1000 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1002 error ("name of class shadows template template parameter %qD",
1003 TYPE_NAME (type));
1004 return error_mark_node;
1007 context = TYPE_CONTEXT (type);
1009 if (TYPE_ALIAS_P (type))
1011 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1013 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1014 error ("specialization of alias template %qD",
1015 TI_TEMPLATE (tinfo));
1016 else
1017 error ("explicit specialization of non-template %qT", type);
1018 return error_mark_node;
1020 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1022 /* This is for ordinary explicit specialization and partial
1023 specialization of a template class such as:
1025 template <> class C<int>;
1029 template <class T> class C<T*>;
1031 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1033 if (tree t = maybe_new_partial_specialization (type))
1035 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1036 && !at_namespace_scope_p ())
1037 return error_mark_node;
1038 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1039 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1040 if (processing_template_decl)
1042 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1043 if (decl == error_mark_node)
1044 return error_mark_node;
1045 return TREE_TYPE (decl);
1048 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1049 error ("specialization of %qT after instantiation", type);
1050 else if (errorcount && !processing_specialization
1051 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1052 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1053 /* Trying to define a specialization either without a template<> header
1054 or in an inappropriate place. We've already given an error, so just
1055 bail now so we don't actually define the specialization. */
1056 return error_mark_node;
1058 else if (CLASS_TYPE_P (type)
1059 && !CLASSTYPE_USE_TEMPLATE (type)
1060 && CLASSTYPE_TEMPLATE_INFO (type)
1061 && context && CLASS_TYPE_P (context)
1062 && CLASSTYPE_TEMPLATE_INFO (context))
1064 /* This is for an explicit specialization of member class
1065 template according to [temp.expl.spec/18]:
1067 template <> template <class U> class C<int>::D;
1069 The context `C<int>' must be an implicit instantiation.
1070 Otherwise this is just a member class template declared
1071 earlier like:
1073 template <> class C<int> { template <class U> class D; };
1074 template <> template <class U> class C<int>::D;
1076 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1077 while in the second case, `C<int>::D' is a primary template
1078 and `C<T>::D' may not exist. */
1080 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1081 && !COMPLETE_TYPE_P (type))
1083 tree t;
1084 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1086 if (current_namespace
1087 != decl_namespace_context (tmpl))
1089 if (permerror (input_location,
1090 "specialization of %qD in different namespace",
1091 type))
1092 inform (DECL_SOURCE_LOCATION (tmpl),
1093 "from definition of %q#D", tmpl);
1096 /* Check for invalid specialization after instantiation:
1098 template <> template <> class C<int>::D<int>;
1099 template <> template <class U> class C<int>::D; */
1101 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1102 t; t = TREE_CHAIN (t))
1104 tree inst = TREE_VALUE (t);
1105 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1106 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1108 /* We already have a full specialization of this partial
1109 instantiation, or a full specialization has been
1110 looked up but not instantiated. Reassign it to the
1111 new member specialization template. */
1112 spec_entry elt;
1113 spec_entry *entry;
1115 elt.tmpl = most_general_template (tmpl);
1116 elt.args = CLASSTYPE_TI_ARGS (inst);
1117 elt.spec = inst;
1119 type_specializations->remove_elt (&elt);
1121 elt.tmpl = tmpl;
1122 CLASSTYPE_TI_ARGS (inst)
1123 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1125 spec_entry **slot
1126 = type_specializations->find_slot (&elt, INSERT);
1127 entry = ggc_alloc<spec_entry> ();
1128 *entry = elt;
1129 *slot = entry;
1131 else
1132 /* But if we've had an implicit instantiation, that's a
1133 problem ([temp.expl.spec]/6). */
1134 error ("specialization %qT after instantiation %qT",
1135 type, inst);
1138 /* Mark TYPE as a specialization. And as a result, we only
1139 have one level of template argument for the innermost
1140 class template. */
1141 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1142 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1143 CLASSTYPE_TI_ARGS (type)
1144 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1147 else if (processing_specialization)
1149 /* Someday C++0x may allow for enum template specialization. */
1150 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1151 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1152 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1153 "of %qD not allowed by ISO C++", type);
1154 else
1156 error ("explicit specialization of non-template %qT", type);
1157 return error_mark_node;
1161 return type;
1164 /* Returns nonzero if we can optimize the retrieval of specializations
1165 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1166 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1168 static inline bool
1169 optimize_specialization_lookup_p (tree tmpl)
1171 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1172 && DECL_CLASS_SCOPE_P (tmpl)
1173 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1174 parameter. */
1175 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1176 /* The optimized lookup depends on the fact that the
1177 template arguments for the member function template apply
1178 purely to the containing class, which is not true if the
1179 containing class is an explicit or partial
1180 specialization. */
1181 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1182 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1183 && !DECL_CONV_FN_P (tmpl)
1184 /* It is possible to have a template that is not a member
1185 template and is not a member of a template class:
1187 template <typename T>
1188 struct S { friend A::f(); };
1190 Here, the friend function is a template, but the context does
1191 not have template information. The optimized lookup relies
1192 on having ARGS be the template arguments for both the class
1193 and the function template. */
1194 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1197 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1198 gone through coerce_template_parms by now. */
1200 static void
1201 verify_unstripped_args_1 (tree inner)
1203 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1205 tree arg = TREE_VEC_ELT (inner, i);
1206 if (TREE_CODE (arg) == TEMPLATE_DECL)
1207 /* OK */;
1208 else if (TYPE_P (arg))
1209 gcc_assert (strip_typedefs (arg, NULL) == arg);
1210 else if (ARGUMENT_PACK_P (arg))
1211 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1212 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1213 /* Allow typedefs on the type of a non-type argument, since a
1214 parameter can have them. */;
1215 else
1216 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1220 static void
1221 verify_unstripped_args (tree args)
1223 ++processing_template_decl;
1224 if (!any_dependent_template_arguments_p (args))
1225 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1226 --processing_template_decl;
1229 /* Retrieve the specialization (in the sense of [temp.spec] - a
1230 specialization is either an instantiation or an explicit
1231 specialization) of TMPL for the given template ARGS. If there is
1232 no such specialization, return NULL_TREE. The ARGS are a vector of
1233 arguments, or a vector of vectors of arguments, in the case of
1234 templates with more than one level of parameters.
1236 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1237 then we search for a partial specialization matching ARGS. This
1238 parameter is ignored if TMPL is not a class template.
1240 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1241 result is a NONTYPE_ARGUMENT_PACK. */
1243 static tree
1244 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1246 if (tmpl == NULL_TREE)
1247 return NULL_TREE;
1249 if (args == error_mark_node)
1250 return NULL_TREE;
1252 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1253 || TREE_CODE (tmpl) == FIELD_DECL);
1255 /* There should be as many levels of arguments as there are
1256 levels of parameters. */
1257 gcc_assert (TMPL_ARGS_DEPTH (args)
1258 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1259 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1260 : template_class_depth (DECL_CONTEXT (tmpl))));
1262 if (flag_checking)
1263 verify_unstripped_args (args);
1265 /* Lambda functions in templates aren't instantiated normally, but through
1266 tsubst_lambda_expr. */
1267 if (lambda_fn_in_template_p (tmpl))
1268 return NULL_TREE;
1270 if (optimize_specialization_lookup_p (tmpl))
1272 /* The template arguments actually apply to the containing
1273 class. Find the class specialization with those
1274 arguments. */
1275 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1276 tree class_specialization
1277 = retrieve_specialization (class_template, args, 0);
1278 if (!class_specialization)
1279 return NULL_TREE;
1281 /* Find the instance of TMPL. */
1282 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1283 for (ovl_iterator iter (fns); iter; ++iter)
1285 tree fn = *iter;
1286 if (tree ti = get_template_info (fn))
1287 if (TI_TEMPLATE (ti) == tmpl
1288 /* using-declarations can bring in a different
1289 instantiation of tmpl as a member of a different
1290 instantiation of tmpl's class. We don't want those
1291 here. */
1292 && DECL_CONTEXT (fn) == class_specialization)
1293 return fn;
1295 return NULL_TREE;
1297 else
1299 spec_entry *found;
1300 spec_entry elt;
1301 spec_hash_table *specializations;
1303 elt.tmpl = tmpl;
1304 elt.args = args;
1305 elt.spec = NULL_TREE;
1307 if (DECL_CLASS_TEMPLATE_P (tmpl))
1308 specializations = type_specializations;
1309 else
1310 specializations = decl_specializations;
1312 if (hash == 0)
1313 hash = spec_hasher::hash (&elt);
1314 found = specializations->find_with_hash (&elt, hash);
1315 if (found)
1316 return found->spec;
1319 return NULL_TREE;
1322 /* Like retrieve_specialization, but for local declarations. */
1324 tree
1325 retrieve_local_specialization (tree tmpl)
1327 if (local_specializations == NULL)
1328 return NULL_TREE;
1330 tree *slot = local_specializations->get (tmpl);
1331 return slot ? *slot : NULL_TREE;
1334 /* Returns nonzero iff DECL is a specialization of TMPL. */
1337 is_specialization_of (tree decl, tree tmpl)
1339 tree t;
1341 if (TREE_CODE (decl) == FUNCTION_DECL)
1343 for (t = decl;
1344 t != NULL_TREE;
1345 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1346 if (t == tmpl)
1347 return 1;
1349 else
1351 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1353 for (t = TREE_TYPE (decl);
1354 t != NULL_TREE;
1355 t = CLASSTYPE_USE_TEMPLATE (t)
1356 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1357 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1358 return 1;
1361 return 0;
1364 /* Returns nonzero iff DECL is a specialization of friend declaration
1365 FRIEND_DECL according to [temp.friend]. */
1367 bool
1368 is_specialization_of_friend (tree decl, tree friend_decl)
1370 bool need_template = true;
1371 int template_depth;
1373 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1374 || TREE_CODE (decl) == TYPE_DECL);
1376 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1377 of a template class, we want to check if DECL is a specialization
1378 if this. */
1379 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1380 && DECL_TEMPLATE_INFO (friend_decl)
1381 && !DECL_USE_TEMPLATE (friend_decl))
1383 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1384 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1385 need_template = false;
1387 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1388 && !PRIMARY_TEMPLATE_P (friend_decl))
1389 need_template = false;
1391 /* There is nothing to do if this is not a template friend. */
1392 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1393 return false;
1395 if (is_specialization_of (decl, friend_decl))
1396 return true;
1398 /* [temp.friend/6]
1399 A member of a class template may be declared to be a friend of a
1400 non-template class. In this case, the corresponding member of
1401 every specialization of the class template is a friend of the
1402 class granting friendship.
1404 For example, given a template friend declaration
1406 template <class T> friend void A<T>::f();
1408 the member function below is considered a friend
1410 template <> struct A<int> {
1411 void f();
1414 For this type of template friend, TEMPLATE_DEPTH below will be
1415 nonzero. To determine if DECL is a friend of FRIEND, we first
1416 check if the enclosing class is a specialization of another. */
1418 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1419 if (template_depth
1420 && DECL_CLASS_SCOPE_P (decl)
1421 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1422 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1424 /* Next, we check the members themselves. In order to handle
1425 a few tricky cases, such as when FRIEND_DECL's are
1427 template <class T> friend void A<T>::g(T t);
1428 template <class T> template <T t> friend void A<T>::h();
1430 and DECL's are
1432 void A<int>::g(int);
1433 template <int> void A<int>::h();
1435 we need to figure out ARGS, the template arguments from
1436 the context of DECL. This is required for template substitution
1437 of `T' in the function parameter of `g' and template parameter
1438 of `h' in the above examples. Here ARGS corresponds to `int'. */
1440 tree context = DECL_CONTEXT (decl);
1441 tree args = NULL_TREE;
1442 int current_depth = 0;
1444 while (current_depth < template_depth)
1446 if (CLASSTYPE_TEMPLATE_INFO (context))
1448 if (current_depth == 0)
1449 args = TYPE_TI_ARGS (context);
1450 else
1451 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1452 current_depth++;
1454 context = TYPE_CONTEXT (context);
1457 if (TREE_CODE (decl) == FUNCTION_DECL)
1459 bool is_template;
1460 tree friend_type;
1461 tree decl_type;
1462 tree friend_args_type;
1463 tree decl_args_type;
1465 /* Make sure that both DECL and FRIEND_DECL are templates or
1466 non-templates. */
1467 is_template = DECL_TEMPLATE_INFO (decl)
1468 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1469 if (need_template ^ is_template)
1470 return false;
1471 else if (is_template)
1473 /* If both are templates, check template parameter list. */
1474 tree friend_parms
1475 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1476 args, tf_none);
1477 if (!comp_template_parms
1478 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1479 friend_parms))
1480 return false;
1482 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1484 else
1485 decl_type = TREE_TYPE (decl);
1487 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1488 tf_none, NULL_TREE);
1489 if (friend_type == error_mark_node)
1490 return false;
1492 /* Check if return types match. */
1493 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1494 return false;
1496 /* Check if function parameter types match, ignoring the
1497 `this' parameter. */
1498 friend_args_type = TYPE_ARG_TYPES (friend_type);
1499 decl_args_type = TYPE_ARG_TYPES (decl_type);
1500 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1501 friend_args_type = TREE_CHAIN (friend_args_type);
1502 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1503 decl_args_type = TREE_CHAIN (decl_args_type);
1505 return compparms (decl_args_type, friend_args_type);
1507 else
1509 /* DECL is a TYPE_DECL */
1510 bool is_template;
1511 tree decl_type = TREE_TYPE (decl);
1513 /* Make sure that both DECL and FRIEND_DECL are templates or
1514 non-templates. */
1515 is_template
1516 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1517 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1519 if (need_template ^ is_template)
1520 return false;
1521 else if (is_template)
1523 tree friend_parms;
1524 /* If both are templates, check the name of the two
1525 TEMPLATE_DECL's first because is_friend didn't. */
1526 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1527 != DECL_NAME (friend_decl))
1528 return false;
1530 /* Now check template parameter list. */
1531 friend_parms
1532 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1533 args, tf_none);
1534 return comp_template_parms
1535 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1536 friend_parms);
1538 else
1539 return (DECL_NAME (decl)
1540 == DECL_NAME (friend_decl));
1543 return false;
1546 /* Register the specialization SPEC as a specialization of TMPL with
1547 the indicated ARGS. IS_FRIEND indicates whether the specialization
1548 is actually just a friend declaration. ATTRLIST is the list of
1549 attributes that the specialization is declared with or NULL when
1550 it isn't. Returns SPEC, or an equivalent prior declaration, if
1551 available.
1553 We also store instantiations of field packs in the hash table, even
1554 though they are not themselves templates, to make lookup easier. */
1556 static tree
1557 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1558 hashval_t hash)
1560 tree fn;
1561 spec_entry **slot = NULL;
1562 spec_entry elt;
1564 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1565 || (TREE_CODE (tmpl) == FIELD_DECL
1566 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1568 if (TREE_CODE (spec) == FUNCTION_DECL
1569 && uses_template_parms (DECL_TI_ARGS (spec)))
1570 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1571 register it; we want the corresponding TEMPLATE_DECL instead.
1572 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1573 the more obvious `uses_template_parms (spec)' to avoid problems
1574 with default function arguments. In particular, given
1575 something like this:
1577 template <class T> void f(T t1, T t = T())
1579 the default argument expression is not substituted for in an
1580 instantiation unless and until it is actually needed. */
1581 return spec;
1583 if (optimize_specialization_lookup_p (tmpl))
1584 /* We don't put these specializations in the hash table, but we might
1585 want to give an error about a mismatch. */
1586 fn = retrieve_specialization (tmpl, args, 0);
1587 else
1589 elt.tmpl = tmpl;
1590 elt.args = args;
1591 elt.spec = spec;
1593 if (hash == 0)
1594 hash = spec_hasher::hash (&elt);
1596 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1597 if (*slot)
1598 fn = (*slot)->spec;
1599 else
1600 fn = NULL_TREE;
1603 /* We can sometimes try to re-register a specialization that we've
1604 already got. In particular, regenerate_decl_from_template calls
1605 duplicate_decls which will update the specialization list. But,
1606 we'll still get called again here anyhow. It's more convenient
1607 to simply allow this than to try to prevent it. */
1608 if (fn == spec)
1609 return spec;
1610 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1612 if (DECL_TEMPLATE_INSTANTIATION (fn))
1614 if (DECL_ODR_USED (fn)
1615 || DECL_EXPLICIT_INSTANTIATION (fn))
1617 error ("specialization of %qD after instantiation",
1618 fn);
1619 return error_mark_node;
1621 else
1623 tree clone;
1624 /* This situation should occur only if the first
1625 specialization is an implicit instantiation, the
1626 second is an explicit specialization, and the
1627 implicit instantiation has not yet been used. That
1628 situation can occur if we have implicitly
1629 instantiated a member function and then specialized
1630 it later.
1632 We can also wind up here if a friend declaration that
1633 looked like an instantiation turns out to be a
1634 specialization:
1636 template <class T> void foo(T);
1637 class S { friend void foo<>(int) };
1638 template <> void foo(int);
1640 We transform the existing DECL in place so that any
1641 pointers to it become pointers to the updated
1642 declaration.
1644 If there was a definition for the template, but not
1645 for the specialization, we want this to look as if
1646 there were no definition, and vice versa. */
1647 DECL_INITIAL (fn) = NULL_TREE;
1648 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1649 /* The call to duplicate_decls will have applied
1650 [temp.expl.spec]:
1652 An explicit specialization of a function template
1653 is inline only if it is explicitly declared to be,
1654 and independently of whether its function template
1657 to the primary function; now copy the inline bits to
1658 the various clones. */
1659 FOR_EACH_CLONE (clone, fn)
1661 DECL_DECLARED_INLINE_P (clone)
1662 = DECL_DECLARED_INLINE_P (fn);
1663 DECL_SOURCE_LOCATION (clone)
1664 = DECL_SOURCE_LOCATION (fn);
1665 DECL_DELETED_FN (clone)
1666 = DECL_DELETED_FN (fn);
1668 check_specialization_namespace (tmpl);
1670 return fn;
1673 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1675 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1676 if (dd == error_mark_node)
1677 /* We've already complained in duplicate_decls. */
1678 return error_mark_node;
1680 if (dd == NULL_TREE && DECL_INITIAL (spec))
1681 /* Dup decl failed, but this is a new definition. Set the
1682 line number so any errors match this new
1683 definition. */
1684 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1686 return fn;
1689 else if (fn)
1690 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1692 /* A specialization must be declared in the same namespace as the
1693 template it is specializing. */
1694 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1695 && !check_specialization_namespace (tmpl))
1696 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1698 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1700 spec_entry *entry = ggc_alloc<spec_entry> ();
1701 gcc_assert (tmpl && args && spec);
1702 *entry = elt;
1703 *slot = entry;
1704 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1705 && PRIMARY_TEMPLATE_P (tmpl)
1706 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1707 || variable_template_p (tmpl))
1708 /* If TMPL is a forward declaration of a template function, keep a list
1709 of all specializations in case we need to reassign them to a friend
1710 template later in tsubst_friend_function.
1712 Also keep a list of all variable template instantiations so that
1713 process_partial_specialization can check whether a later partial
1714 specialization would have used it. */
1715 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1716 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1719 return spec;
1722 /* Restricts tree and type comparisons. */
1723 int comparing_specializations;
1724 int comparing_dependent_aliases;
1726 /* Returns true iff two spec_entry nodes are equivalent. */
1728 bool
1729 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1731 int equal;
1733 ++comparing_specializations;
1734 ++comparing_dependent_aliases;
1735 ++processing_template_decl;
1736 equal = (e1->tmpl == e2->tmpl
1737 && comp_template_args (e1->args, e2->args));
1738 if (equal && flag_concepts
1739 /* tmpl could be a FIELD_DECL for a capture pack. */
1740 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1741 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1742 && uses_template_parms (e1->args))
1744 /* Partial specializations of a variable template can be distinguished by
1745 constraints. */
1746 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1747 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1748 equal = equivalent_constraints (c1, c2);
1750 --processing_template_decl;
1751 --comparing_dependent_aliases;
1752 --comparing_specializations;
1754 return equal;
1757 /* Returns a hash for a template TMPL and template arguments ARGS. */
1759 static hashval_t
1760 hash_tmpl_and_args (tree tmpl, tree args)
1762 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1763 return iterative_hash_template_arg (args, val);
1766 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1767 ignoring SPEC. */
1769 hashval_t
1770 spec_hasher::hash (spec_entry *e)
1772 return hash_tmpl_and_args (e->tmpl, e->args);
1775 /* Recursively calculate a hash value for a template argument ARG, for use
1776 in the hash tables of template specializations. We must be
1777 careful to (at least) skip the same entities template_args_equal
1778 does. */
1780 hashval_t
1781 iterative_hash_template_arg (tree arg, hashval_t val)
1783 if (arg == NULL_TREE)
1784 return iterative_hash_object (arg, val);
1786 if (!TYPE_P (arg))
1787 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1788 while (CONVERT_EXPR_P (arg)
1789 || TREE_CODE (arg) == NON_LVALUE_EXPR
1790 || class_nttp_const_wrapper_p (arg))
1791 arg = TREE_OPERAND (arg, 0);
1793 enum tree_code code = TREE_CODE (arg);
1795 val = iterative_hash_object (code, val);
1797 switch (code)
1799 case ARGUMENT_PACK_SELECT:
1800 gcc_unreachable ();
1802 case ERROR_MARK:
1803 return val;
1805 case IDENTIFIER_NODE:
1806 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1808 case TREE_VEC:
1809 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1810 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1811 return val;
1813 case TYPE_PACK_EXPANSION:
1814 case EXPR_PACK_EXPANSION:
1815 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1816 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1818 case TYPE_ARGUMENT_PACK:
1819 case NONTYPE_ARGUMENT_PACK:
1820 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1822 case TREE_LIST:
1823 for (; arg; arg = TREE_CHAIN (arg))
1824 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1825 return val;
1827 case OVERLOAD:
1828 for (lkp_iterator iter (arg); iter; ++iter)
1829 val = iterative_hash_template_arg (*iter, val);
1830 return val;
1832 case CONSTRUCTOR:
1834 tree field, value;
1835 unsigned i;
1836 iterative_hash_template_arg (TREE_TYPE (arg), val);
1837 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1839 val = iterative_hash_template_arg (field, val);
1840 val = iterative_hash_template_arg (value, val);
1842 return val;
1845 case PARM_DECL:
1846 if (!DECL_ARTIFICIAL (arg))
1848 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1849 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1851 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1853 case TARGET_EXPR:
1854 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1856 case PTRMEM_CST:
1857 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1858 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1860 case TEMPLATE_PARM_INDEX:
1861 val = iterative_hash_template_arg
1862 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1863 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1864 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1866 case TRAIT_EXPR:
1867 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1868 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1869 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1871 case BASELINK:
1872 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1873 val);
1874 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1875 val);
1877 case MODOP_EXPR:
1878 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1879 code = TREE_CODE (TREE_OPERAND (arg, 1));
1880 val = iterative_hash_object (code, val);
1881 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1883 case LAMBDA_EXPR:
1884 /* [temp.over.link] Two lambda-expressions are never considered
1885 equivalent.
1887 So just hash the closure type. */
1888 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1890 case CAST_EXPR:
1891 case IMPLICIT_CONV_EXPR:
1892 case STATIC_CAST_EXPR:
1893 case REINTERPRET_CAST_EXPR:
1894 case CONST_CAST_EXPR:
1895 case DYNAMIC_CAST_EXPR:
1896 case NEW_EXPR:
1897 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1898 /* Now hash operands as usual. */
1899 break;
1901 case CALL_EXPR:
1903 tree fn = CALL_EXPR_FN (arg);
1904 if (tree name = dependent_name (fn))
1906 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1907 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1908 fn = name;
1910 val = iterative_hash_template_arg (fn, val);
1911 call_expr_arg_iterator ai;
1912 for (tree x = first_call_expr_arg (arg, &ai); x;
1913 x = next_call_expr_arg (&ai))
1914 val = iterative_hash_template_arg (x, val);
1915 return val;
1918 default:
1919 break;
1922 char tclass = TREE_CODE_CLASS (code);
1923 switch (tclass)
1925 case tcc_type:
1926 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1928 // We want an alias specialization that survived strip_typedefs
1929 // to hash differently from its TYPE_CANONICAL, to avoid hash
1930 // collisions that compare as different in template_args_equal.
1931 // These could be dependent specializations that strip_typedefs
1932 // left alone, or untouched specializations because
1933 // coerce_template_parms returns the unconverted template
1934 // arguments if it sees incomplete argument packs.
1935 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1936 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1939 switch (TREE_CODE (arg))
1941 case TEMPLATE_TEMPLATE_PARM:
1943 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1945 /* Do not recurse with TPI directly, as that is unbounded
1946 recursion. */
1947 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1948 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1950 break;
1952 case DECLTYPE_TYPE:
1953 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1954 break;
1956 default:
1957 if (tree canonical = TYPE_CANONICAL (arg))
1958 val = iterative_hash_object (TYPE_HASH (canonical), val);
1959 break;
1962 return val;
1964 case tcc_declaration:
1965 case tcc_constant:
1966 return iterative_hash_expr (arg, val);
1968 default:
1969 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1970 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1971 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1972 return val;
1975 gcc_unreachable ();
1976 return 0;
1979 /* Unregister the specialization SPEC as a specialization of TMPL.
1980 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1981 if the SPEC was listed as a specialization of TMPL.
1983 Note that SPEC has been ggc_freed, so we can't look inside it. */
1985 bool
1986 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1988 spec_entry *entry;
1989 spec_entry elt;
1991 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1992 elt.args = TI_ARGS (tinfo);
1993 elt.spec = NULL_TREE;
1995 entry = decl_specializations->find (&elt);
1996 if (entry != NULL)
1998 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1999 gcc_assert (new_spec != NULL_TREE);
2000 entry->spec = new_spec;
2001 return 1;
2004 return 0;
2007 /* Like register_specialization, but for local declarations. We are
2008 registering SPEC, an instantiation of TMPL. */
2010 void
2011 register_local_specialization (tree spec, tree tmpl)
2013 gcc_assert (tmpl != spec);
2014 local_specializations->put (tmpl, spec);
2017 /* TYPE is a class type. Returns true if TYPE is an explicitly
2018 specialized class. */
2020 bool
2021 explicit_class_specialization_p (tree type)
2023 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2024 return false;
2025 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2028 /* Print the list of functions at FNS, going through all the overloads
2029 for each element of the list. Alternatively, FNS cannot be a
2030 TREE_LIST, in which case it will be printed together with all the
2031 overloads.
2033 MORE and *STR should respectively be FALSE and NULL when the function
2034 is called from the outside. They are used internally on recursive
2035 calls. print_candidates manages the two parameters and leaves NULL
2036 in *STR when it ends. */
2038 static void
2039 print_candidates_1 (tree fns, char **str, bool more = false)
2041 if (TREE_CODE (fns) == TREE_LIST)
2042 for (; fns; fns = TREE_CHAIN (fns))
2043 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2044 else
2045 for (lkp_iterator iter (fns); iter;)
2047 tree cand = *iter;
2048 ++iter;
2050 const char *pfx = *str;
2051 if (!pfx)
2053 if (more || iter)
2054 pfx = _("candidates are:");
2055 else
2056 pfx = _("candidate is:");
2057 *str = get_spaces (pfx);
2059 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2063 /* Print the list of candidate FNS in an error message. FNS can also
2064 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2066 void
2067 print_candidates (tree fns)
2069 char *str = NULL;
2070 print_candidates_1 (fns, &str);
2071 free (str);
2074 /* Get a (possibly) constrained template declaration for the
2075 purpose of ordering candidates. */
2076 static tree
2077 get_template_for_ordering (tree list)
2079 gcc_assert (TREE_CODE (list) == TREE_LIST);
2080 tree f = TREE_VALUE (list);
2081 if (tree ti = DECL_TEMPLATE_INFO (f))
2082 return TI_TEMPLATE (ti);
2083 return f;
2086 /* Among candidates having the same signature, return the
2087 most constrained or NULL_TREE if there is no best candidate.
2088 If the signatures of candidates vary (e.g., template
2089 specialization vs. member function), then there can be no
2090 most constrained.
2092 Note that we don't compare constraints on the functions
2093 themselves, but rather those of their templates. */
2094 static tree
2095 most_constrained_function (tree candidates)
2097 // Try to find the best candidate in a first pass.
2098 tree champ = candidates;
2099 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2101 int winner = more_constrained (get_template_for_ordering (champ),
2102 get_template_for_ordering (c));
2103 if (winner == -1)
2104 champ = c; // The candidate is more constrained
2105 else if (winner == 0)
2106 return NULL_TREE; // Neither is more constrained
2109 // Verify that the champ is better than previous candidates.
2110 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2111 if (!more_constrained (get_template_for_ordering (champ),
2112 get_template_for_ordering (c)))
2113 return NULL_TREE;
2116 return champ;
2120 /* Returns the template (one of the functions given by TEMPLATE_ID)
2121 which can be specialized to match the indicated DECL with the
2122 explicit template args given in TEMPLATE_ID. The DECL may be
2123 NULL_TREE if none is available. In that case, the functions in
2124 TEMPLATE_ID are non-members.
2126 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2127 specialization of a member template.
2129 The TEMPLATE_COUNT is the number of references to qualifying
2130 template classes that appeared in the name of the function. See
2131 check_explicit_specialization for a more accurate description.
2133 TSK indicates what kind of template declaration (if any) is being
2134 declared. TSK_TEMPLATE indicates that the declaration given by
2135 DECL, though a FUNCTION_DECL, has template parameters, and is
2136 therefore a template function.
2138 The template args (those explicitly specified and those deduced)
2139 are output in a newly created vector *TARGS_OUT.
2141 If it is impossible to determine the result, an error message is
2142 issued. The error_mark_node is returned to indicate failure. */
2144 static tree
2145 determine_specialization (tree template_id,
2146 tree decl,
2147 tree* targs_out,
2148 int need_member_template,
2149 int template_count,
2150 tmpl_spec_kind tsk)
2152 tree fns;
2153 tree targs;
2154 tree explicit_targs;
2155 tree candidates = NULL_TREE;
2157 /* A TREE_LIST of templates of which DECL may be a specialization.
2158 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2159 corresponding TREE_PURPOSE is the set of template arguments that,
2160 when used to instantiate the template, would produce a function
2161 with the signature of DECL. */
2162 tree templates = NULL_TREE;
2163 int header_count;
2164 cp_binding_level *b;
2166 *targs_out = NULL_TREE;
2168 if (template_id == error_mark_node || decl == error_mark_node)
2169 return error_mark_node;
2171 /* We shouldn't be specializing a member template of an
2172 unspecialized class template; we already gave an error in
2173 check_specialization_scope, now avoid crashing. */
2174 if (!VAR_P (decl)
2175 && template_count && DECL_CLASS_SCOPE_P (decl)
2176 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2178 gcc_assert (errorcount);
2179 return error_mark_node;
2182 fns = TREE_OPERAND (template_id, 0);
2183 explicit_targs = TREE_OPERAND (template_id, 1);
2185 if (fns == error_mark_node)
2186 return error_mark_node;
2188 /* Check for baselinks. */
2189 if (BASELINK_P (fns))
2190 fns = BASELINK_FUNCTIONS (fns);
2192 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2194 error_at (DECL_SOURCE_LOCATION (decl),
2195 "%qD is not a function template", fns);
2196 return error_mark_node;
2198 else if (VAR_P (decl) && !variable_template_p (fns))
2200 error ("%qD is not a variable template", fns);
2201 return error_mark_node;
2204 /* Count the number of template headers specified for this
2205 specialization. */
2206 header_count = 0;
2207 for (b = current_binding_level;
2208 b->kind == sk_template_parms;
2209 b = b->level_chain)
2210 ++header_count;
2212 tree orig_fns = fns;
2213 bool header_mismatch = false;
2215 if (variable_template_p (fns))
2217 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2218 targs = coerce_template_parms (parms, explicit_targs, fns,
2219 tf_warning_or_error,
2220 /*req_all*/true, /*use_defarg*/true);
2221 if (targs != error_mark_node)
2222 templates = tree_cons (targs, fns, templates);
2224 else for (lkp_iterator iter (fns); iter; ++iter)
2226 tree fn = *iter;
2228 if (TREE_CODE (fn) == TEMPLATE_DECL)
2230 tree decl_arg_types;
2231 tree fn_arg_types;
2232 tree insttype;
2234 /* In case of explicit specialization, we need to check if
2235 the number of template headers appearing in the specialization
2236 is correct. This is usually done in check_explicit_specialization,
2237 but the check done there cannot be exhaustive when specializing
2238 member functions. Consider the following code:
2240 template <> void A<int>::f(int);
2241 template <> template <> void A<int>::f(int);
2243 Assuming that A<int> is not itself an explicit specialization
2244 already, the first line specializes "f" which is a non-template
2245 member function, whilst the second line specializes "f" which
2246 is a template member function. So both lines are syntactically
2247 correct, and check_explicit_specialization does not reject
2248 them.
2250 Here, we can do better, as we are matching the specialization
2251 against the declarations. We count the number of template
2252 headers, and we check if they match TEMPLATE_COUNT + 1
2253 (TEMPLATE_COUNT is the number of qualifying template classes,
2254 plus there must be another header for the member template
2255 itself).
2257 Notice that if header_count is zero, this is not a
2258 specialization but rather a template instantiation, so there
2259 is no check we can perform here. */
2260 if (header_count && header_count != template_count + 1)
2262 header_mismatch = true;
2263 continue;
2266 /* Check that the number of template arguments at the
2267 innermost level for DECL is the same as for FN. */
2268 if (current_binding_level->kind == sk_template_parms
2269 && !current_binding_level->explicit_spec_p
2270 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2271 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2272 (current_template_parms))))
2273 continue;
2275 /* DECL might be a specialization of FN. */
2276 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2277 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2279 /* For a non-static member function, we need to make sure
2280 that the const qualification is the same. Since
2281 get_bindings does not try to merge the "this" parameter,
2282 we must do the comparison explicitly. */
2283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2285 if (!same_type_p (TREE_VALUE (fn_arg_types),
2286 TREE_VALUE (decl_arg_types)))
2287 continue;
2289 /* And the ref-qualification. */
2290 if (type_memfn_rqual (TREE_TYPE (decl))
2291 != type_memfn_rqual (TREE_TYPE (fn)))
2292 continue;
2295 /* Skip the "this" parameter and, for constructors of
2296 classes with virtual bases, the VTT parameter. A
2297 full specialization of a constructor will have a VTT
2298 parameter, but a template never will. */
2299 decl_arg_types
2300 = skip_artificial_parms_for (decl, decl_arg_types);
2301 fn_arg_types
2302 = skip_artificial_parms_for (fn, fn_arg_types);
2304 /* Function templates cannot be specializations; there are
2305 no partial specializations of functions. Therefore, if
2306 the type of DECL does not match FN, there is no
2307 match.
2309 Note that it should never be the case that we have both
2310 candidates added here, and for regular member functions
2311 below. */
2312 if (tsk == tsk_template)
2314 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2315 current_template_parms))
2316 continue;
2317 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2318 TREE_TYPE (TREE_TYPE (fn))))
2319 continue;
2320 if (!compparms (fn_arg_types, decl_arg_types))
2321 continue;
2323 tree freq = get_trailing_function_requirements (fn);
2324 tree dreq = get_trailing_function_requirements (decl);
2325 if (!freq != !dreq)
2326 continue;
2327 if (freq)
2329 tree fargs = DECL_TI_ARGS (fn);
2330 tsubst_flags_t complain = tf_none;
2331 freq = tsubst_constraint (freq, fargs, complain, fn);
2332 if (!cp_tree_equal (freq, dreq))
2333 continue;
2336 candidates = tree_cons (NULL_TREE, fn, candidates);
2337 continue;
2340 /* See whether this function might be a specialization of this
2341 template. Suppress access control because we might be trying
2342 to make this specialization a friend, and we have already done
2343 access control for the declaration of the specialization. */
2344 push_deferring_access_checks (dk_no_check);
2345 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2346 pop_deferring_access_checks ();
2348 if (!targs)
2349 /* We cannot deduce template arguments that when used to
2350 specialize TMPL will produce DECL. */
2351 continue;
2353 if (uses_template_parms (targs))
2354 /* We deduced something involving 'auto', which isn't a valid
2355 template argument. */
2356 continue;
2358 /* Remove, from the set of candidates, all those functions
2359 whose constraints are not satisfied. */
2360 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2361 continue;
2363 // Then, try to form the new function type.
2364 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2365 if (insttype == error_mark_node)
2366 continue;
2367 fn_arg_types
2368 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2369 if (!compparms (fn_arg_types, decl_arg_types))
2370 continue;
2372 /* Save this template, and the arguments deduced. */
2373 templates = tree_cons (targs, fn, templates);
2375 else if (need_member_template)
2376 /* FN is an ordinary member function, and we need a
2377 specialization of a member template. */
2379 else if (TREE_CODE (fn) != FUNCTION_DECL)
2380 /* We can get IDENTIFIER_NODEs here in certain erroneous
2381 cases. */
2383 else if (!DECL_FUNCTION_MEMBER_P (fn))
2384 /* This is just an ordinary non-member function. Nothing can
2385 be a specialization of that. */
2387 else if (DECL_ARTIFICIAL (fn))
2388 /* Cannot specialize functions that are created implicitly. */
2390 else
2392 tree decl_arg_types;
2394 /* This is an ordinary member function. However, since
2395 we're here, we can assume its enclosing class is a
2396 template class. For example,
2398 template <typename T> struct S { void f(); };
2399 template <> void S<int>::f() {}
2401 Here, S<int>::f is a non-template, but S<int> is a
2402 template class. If FN has the same type as DECL, we
2403 might be in business. */
2405 if (!DECL_TEMPLATE_INFO (fn))
2406 /* Its enclosing class is an explicit specialization
2407 of a template class. This is not a candidate. */
2408 continue;
2410 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2411 TREE_TYPE (TREE_TYPE (fn))))
2412 /* The return types differ. */
2413 continue;
2415 /* Adjust the type of DECL in case FN is a static member. */
2416 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2417 if (DECL_STATIC_FUNCTION_P (fn)
2418 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2419 decl_arg_types = TREE_CHAIN (decl_arg_types);
2421 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2422 decl_arg_types))
2423 continue;
2425 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2426 && (type_memfn_rqual (TREE_TYPE (decl))
2427 != type_memfn_rqual (TREE_TYPE (fn))))
2428 continue;
2430 // If the deduced arguments do not satisfy the constraints,
2431 // this is not a candidate.
2432 if (flag_concepts && !constraints_satisfied_p (fn))
2433 continue;
2435 // Add the candidate.
2436 candidates = tree_cons (NULL_TREE, fn, candidates);
2440 if (templates && TREE_CHAIN (templates))
2442 /* We have:
2444 [temp.expl.spec]
2446 It is possible for a specialization with a given function
2447 signature to be instantiated from more than one function
2448 template. In such cases, explicit specification of the
2449 template arguments must be used to uniquely identify the
2450 function template specialization being specialized.
2452 Note that here, there's no suggestion that we're supposed to
2453 determine which of the candidate templates is most
2454 specialized. However, we, also have:
2456 [temp.func.order]
2458 Partial ordering of overloaded function template
2459 declarations is used in the following contexts to select
2460 the function template to which a function template
2461 specialization refers:
2463 -- when an explicit specialization refers to a function
2464 template.
2466 So, we do use the partial ordering rules, at least for now.
2467 This extension can only serve to make invalid programs valid,
2468 so it's safe. And, there is strong anecdotal evidence that
2469 the committee intended the partial ordering rules to apply;
2470 the EDG front end has that behavior, and John Spicer claims
2471 that the committee simply forgot to delete the wording in
2472 [temp.expl.spec]. */
2473 tree tmpl = most_specialized_instantiation (templates);
2474 if (tmpl != error_mark_node)
2476 templates = tmpl;
2477 TREE_CHAIN (templates) = NULL_TREE;
2481 // Concepts allows multiple declarations of member functions
2482 // with the same signature. Like above, we need to rely on
2483 // on the partial ordering of those candidates to determine which
2484 // is the best.
2485 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2487 if (tree cand = most_constrained_function (candidates))
2489 candidates = cand;
2490 TREE_CHAIN (cand) = NULL_TREE;
2494 if (templates == NULL_TREE && candidates == NULL_TREE)
2496 error ("template-id %qD for %q+D does not match any template "
2497 "declaration", template_id, decl);
2498 if (header_mismatch)
2499 inform (DECL_SOURCE_LOCATION (decl),
2500 "saw %d %<template<>%>, need %d for "
2501 "specializing a member function template",
2502 header_count, template_count + 1);
2503 print_candidates (orig_fns);
2504 return error_mark_node;
2506 else if ((templates && TREE_CHAIN (templates))
2507 || (candidates && TREE_CHAIN (candidates))
2508 || (templates && candidates))
2510 error ("ambiguous template specialization %qD for %q+D",
2511 template_id, decl);
2512 candidates = chainon (candidates, templates);
2513 print_candidates (candidates);
2514 return error_mark_node;
2517 /* We have one, and exactly one, match. */
2518 if (candidates)
2520 tree fn = TREE_VALUE (candidates);
2521 *targs_out = copy_node (DECL_TI_ARGS (fn));
2523 /* Propagate the candidate's constraints to the declaration. */
2524 if (tsk != tsk_template)
2525 set_constraints (decl, get_constraints (fn));
2527 /* DECL is a re-declaration or partial instantiation of a template
2528 function. */
2529 if (TREE_CODE (fn) == TEMPLATE_DECL)
2530 return fn;
2531 /* It was a specialization of an ordinary member function in a
2532 template class. */
2533 return DECL_TI_TEMPLATE (fn);
2536 /* It was a specialization of a template. */
2537 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2538 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2540 *targs_out = copy_node (targs);
2541 SET_TMPL_ARGS_LEVEL (*targs_out,
2542 TMPL_ARGS_DEPTH (*targs_out),
2543 TREE_PURPOSE (templates));
2545 else
2546 *targs_out = TREE_PURPOSE (templates);
2547 return TREE_VALUE (templates);
2550 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2551 but with the default argument values filled in from those in the
2552 TMPL_TYPES. */
2554 static tree
2555 copy_default_args_to_explicit_spec_1 (tree spec_types,
2556 tree tmpl_types)
2558 tree new_spec_types;
2560 if (!spec_types)
2561 return NULL_TREE;
2563 if (spec_types == void_list_node)
2564 return void_list_node;
2566 /* Substitute into the rest of the list. */
2567 new_spec_types =
2568 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2569 TREE_CHAIN (tmpl_types));
2571 /* Add the default argument for this parameter. */
2572 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2573 TREE_VALUE (spec_types),
2574 new_spec_types);
2577 /* DECL is an explicit specialization. Replicate default arguments
2578 from the template it specializes. (That way, code like:
2580 template <class T> void f(T = 3);
2581 template <> void f(double);
2582 void g () { f (); }
2584 works, as required.) An alternative approach would be to look up
2585 the correct default arguments at the call-site, but this approach
2586 is consistent with how implicit instantiations are handled. */
2588 static void
2589 copy_default_args_to_explicit_spec (tree decl)
2591 tree tmpl;
2592 tree spec_types;
2593 tree tmpl_types;
2594 tree new_spec_types;
2595 tree old_type;
2596 tree new_type;
2597 tree t;
2598 tree object_type = NULL_TREE;
2599 tree in_charge = NULL_TREE;
2600 tree vtt = NULL_TREE;
2602 /* See if there's anything we need to do. */
2603 tmpl = DECL_TI_TEMPLATE (decl);
2604 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2605 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2606 if (TREE_PURPOSE (t))
2607 break;
2608 if (!t)
2609 return;
2611 old_type = TREE_TYPE (decl);
2612 spec_types = TYPE_ARG_TYPES (old_type);
2614 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2616 /* Remove the this pointer, but remember the object's type for
2617 CV quals. */
2618 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2619 spec_types = TREE_CHAIN (spec_types);
2620 tmpl_types = TREE_CHAIN (tmpl_types);
2622 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2624 /* DECL may contain more parameters than TMPL due to the extra
2625 in-charge parameter in constructors and destructors. */
2626 in_charge = spec_types;
2627 spec_types = TREE_CHAIN (spec_types);
2629 if (DECL_HAS_VTT_PARM_P (decl))
2631 vtt = spec_types;
2632 spec_types = TREE_CHAIN (spec_types);
2636 /* Compute the merged default arguments. */
2637 new_spec_types =
2638 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2640 /* Compute the new FUNCTION_TYPE. */
2641 if (object_type)
2643 if (vtt)
2644 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2645 TREE_VALUE (vtt),
2646 new_spec_types);
2648 if (in_charge)
2649 /* Put the in-charge parameter back. */
2650 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2651 TREE_VALUE (in_charge),
2652 new_spec_types);
2654 new_type = build_method_type_directly (object_type,
2655 TREE_TYPE (old_type),
2656 new_spec_types);
2658 else
2659 new_type = build_function_type (TREE_TYPE (old_type),
2660 new_spec_types);
2661 new_type = cp_build_type_attribute_variant (new_type,
2662 TYPE_ATTRIBUTES (old_type));
2663 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2665 TREE_TYPE (decl) = new_type;
2668 /* Return the number of template headers we expect to see for a definition
2669 or specialization of CTYPE or one of its non-template members. */
2672 num_template_headers_for_class (tree ctype)
2674 int num_templates = 0;
2676 while (ctype && CLASS_TYPE_P (ctype))
2678 /* You're supposed to have one `template <...>' for every
2679 template class, but you don't need one for a full
2680 specialization. For example:
2682 template <class T> struct S{};
2683 template <> struct S<int> { void f(); };
2684 void S<int>::f () {}
2686 is correct; there shouldn't be a `template <>' for the
2687 definition of `S<int>::f'. */
2688 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2689 /* If CTYPE does not have template information of any
2690 kind, then it is not a template, nor is it nested
2691 within a template. */
2692 break;
2693 if (explicit_class_specialization_p (ctype))
2694 break;
2695 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2696 ++num_templates;
2698 ctype = TYPE_CONTEXT (ctype);
2701 return num_templates;
2704 /* Do a simple sanity check on the template headers that precede the
2705 variable declaration DECL. */
2707 void
2708 check_template_variable (tree decl)
2710 tree ctx = CP_DECL_CONTEXT (decl);
2711 int wanted = num_template_headers_for_class (ctx);
2712 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2713 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2715 if (cxx_dialect < cxx14)
2716 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2717 "variable templates only available with "
2718 "%<-std=c++14%> or %<-std=gnu++14%>");
2720 // Namespace-scope variable templates should have a template header.
2721 ++wanted;
2723 if (template_header_count > wanted)
2725 auto_diagnostic_group d;
2726 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2727 "too many template headers for %qD "
2728 "(should be %d)",
2729 decl, wanted);
2730 if (warned && CLASS_TYPE_P (ctx)
2731 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2732 inform (DECL_SOURCE_LOCATION (decl),
2733 "members of an explicitly specialized class are defined "
2734 "without a template header");
2738 /* An explicit specialization whose declarator-id or class-head-name is not
2739 qualified shall be declared in the nearest enclosing namespace of the
2740 template, or, if the namespace is inline (7.3.1), any namespace from its
2741 enclosing namespace set.
2743 If the name declared in the explicit instantiation is an unqualified name,
2744 the explicit instantiation shall appear in the namespace where its template
2745 is declared or, if that namespace is inline (7.3.1), any namespace from its
2746 enclosing namespace set. */
2748 void
2749 check_unqualified_spec_or_inst (tree t, location_t loc)
2751 tree tmpl = most_general_template (t);
2752 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2753 && !is_nested_namespace (current_namespace,
2754 CP_DECL_CONTEXT (tmpl), true))
2756 if (processing_specialization)
2757 permerror (loc, "explicit specialization of %qD outside its "
2758 "namespace must use a nested-name-specifier", tmpl);
2759 else if (processing_explicit_instantiation
2760 && cxx_dialect >= cxx11)
2761 /* This was allowed in C++98, so only pedwarn. */
2762 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2763 "outside its namespace must use a nested-name-"
2764 "specifier", tmpl);
2768 /* Warn for a template specialization SPEC that is missing some of a set
2769 of function or type attributes that the template TEMPL is declared with.
2770 ATTRLIST is a list of additional attributes that SPEC should be taken
2771 to ultimately be declared with. */
2773 static void
2774 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2776 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2777 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2779 /* Avoid warning if the difference between the primary and
2780 the specialization is not in one of the attributes below. */
2781 const char* const blacklist[] = {
2782 "alloc_align", "alloc_size", "assume_aligned", "format",
2783 "format_arg", "malloc", "nonnull", NULL
2786 /* Put together a list of the black listed attributes that the primary
2787 template is declared with that the specialization is not, in case
2788 it's not apparent from the most recent declaration of the primary. */
2789 pretty_printer str;
2790 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2791 blacklist, &str);
2793 if (!nattrs)
2794 return;
2796 auto_diagnostic_group d;
2797 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2798 "explicit specialization %q#D may be missing attributes",
2799 spec))
2800 inform (DECL_SOURCE_LOCATION (tmpl),
2801 nattrs > 1
2802 ? G_("missing primary template attributes %s")
2803 : G_("missing primary template attribute %s"),
2804 pp_formatted_text (&str));
2807 /* Check to see if the function just declared, as indicated in
2808 DECLARATOR, and in DECL, is a specialization of a function
2809 template. We may also discover that the declaration is an explicit
2810 instantiation at this point.
2812 Returns DECL, or an equivalent declaration that should be used
2813 instead if all goes well. Issues an error message if something is
2814 amiss. Returns error_mark_node if the error is not easily
2815 recoverable.
2817 FLAGS is a bitmask consisting of the following flags:
2819 2: The function has a definition.
2820 4: The function is a friend.
2822 The TEMPLATE_COUNT is the number of references to qualifying
2823 template classes that appeared in the name of the function. For
2824 example, in
2826 template <class T> struct S { void f(); };
2827 void S<int>::f();
2829 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2830 classes are not counted in the TEMPLATE_COUNT, so that in
2832 template <class T> struct S {};
2833 template <> struct S<int> { void f(); }
2834 template <> void S<int>::f();
2836 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2837 invalid; there should be no template <>.)
2839 If the function is a specialization, it is marked as such via
2840 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2841 is set up correctly, and it is added to the list of specializations
2842 for that template. */
2844 tree
2845 check_explicit_specialization (tree declarator,
2846 tree decl,
2847 int template_count,
2848 int flags,
2849 tree attrlist)
2851 int have_def = flags & 2;
2852 int is_friend = flags & 4;
2853 bool is_concept = flags & 8;
2854 int specialization = 0;
2855 int explicit_instantiation = 0;
2856 int member_specialization = 0;
2857 tree ctype = DECL_CLASS_CONTEXT (decl);
2858 tree dname = DECL_NAME (decl);
2859 tmpl_spec_kind tsk;
2861 if (is_friend)
2863 if (!processing_specialization)
2864 tsk = tsk_none;
2865 else
2866 tsk = tsk_excessive_parms;
2868 else
2869 tsk = current_tmpl_spec_kind (template_count);
2871 switch (tsk)
2873 case tsk_none:
2874 if (processing_specialization && !VAR_P (decl))
2876 specialization = 1;
2877 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2879 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2881 if (is_friend)
2882 /* This could be something like:
2884 template <class T> void f(T);
2885 class S { friend void f<>(int); } */
2886 specialization = 1;
2887 else
2889 /* This case handles bogus declarations like template <>
2890 template <class T> void f<int>(); */
2892 error_at (cp_expr_loc_or_input_loc (declarator),
2893 "template-id %qE in declaration of primary template",
2894 declarator);
2895 return decl;
2898 break;
2900 case tsk_invalid_member_spec:
2901 /* The error has already been reported in
2902 check_specialization_scope. */
2903 return error_mark_node;
2905 case tsk_invalid_expl_inst:
2906 error ("template parameter list used in explicit instantiation");
2908 /* Fall through. */
2910 case tsk_expl_inst:
2911 if (have_def)
2912 error ("definition provided for explicit instantiation");
2914 explicit_instantiation = 1;
2915 break;
2917 case tsk_excessive_parms:
2918 case tsk_insufficient_parms:
2919 if (tsk == tsk_excessive_parms)
2920 error ("too many template parameter lists in declaration of %qD",
2921 decl);
2922 else if (template_header_count)
2923 error("too few template parameter lists in declaration of %qD", decl);
2924 else
2925 error("explicit specialization of %qD must be introduced by "
2926 "%<template <>%>", decl);
2928 /* Fall through. */
2929 case tsk_expl_spec:
2930 if (is_concept)
2931 error ("explicit specialization declared %<concept%>");
2933 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2934 /* In cases like template<> constexpr bool v = true;
2935 We'll give an error in check_template_variable. */
2936 break;
2938 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2939 if (ctype)
2940 member_specialization = 1;
2941 else
2942 specialization = 1;
2943 break;
2945 case tsk_template:
2946 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2948 /* This case handles bogus declarations like template <>
2949 template <class T> void f<int>(); */
2951 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2952 error_at (cp_expr_loc_or_input_loc (declarator),
2953 "template-id %qE in declaration of primary template",
2954 declarator);
2955 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2957 /* Partial specialization of variable template. */
2958 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2959 specialization = 1;
2960 goto ok;
2962 else if (cxx_dialect < cxx14)
2963 error_at (cp_expr_loc_or_input_loc (declarator),
2964 "non-type partial specialization %qE "
2965 "is not allowed", declarator);
2966 else
2967 error_at (cp_expr_loc_or_input_loc (declarator),
2968 "non-class, non-variable partial specialization %qE "
2969 "is not allowed", declarator);
2970 return decl;
2971 ok:;
2974 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2975 /* This is a specialization of a member template, without
2976 specialization the containing class. Something like:
2978 template <class T> struct S {
2979 template <class U> void f (U);
2981 template <> template <class U> void S<int>::f(U) {}
2983 That's a specialization -- but of the entire template. */
2984 specialization = 1;
2985 break;
2987 default:
2988 gcc_unreachable ();
2991 if ((specialization || member_specialization)
2992 /* This doesn't apply to variable templates. */
2993 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2995 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2996 for (; t; t = TREE_CHAIN (t))
2997 if (TREE_PURPOSE (t))
2999 permerror (input_location,
3000 "default argument specified in explicit specialization");
3001 break;
3005 if (specialization || member_specialization || explicit_instantiation)
3007 tree tmpl = NULL_TREE;
3008 tree targs = NULL_TREE;
3009 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
3010 bool found_hidden = false;
3012 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3013 if (!was_template_id)
3015 tree fns;
3017 gcc_assert (identifier_p (declarator));
3018 if (ctype)
3019 fns = dname;
3020 else
3022 /* If there is no class context, the explicit instantiation
3023 must be at namespace scope. */
3024 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3026 /* Find the namespace binding, using the declaration
3027 context. */
3028 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3029 LOOK_want::NORMAL, true);
3030 if (fns == error_mark_node)
3032 /* If lookup fails, look for a friend declaration so we can
3033 give a better diagnostic. */
3034 fns = (lookup_qualified_name
3035 (CP_DECL_CONTEXT (decl), dname,
3036 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3037 /*complain*/true));
3038 found_hidden = true;
3041 if (fns == error_mark_node || !is_overloaded_fn (fns))
3043 error ("%qD is not a template function", dname);
3044 fns = error_mark_node;
3048 declarator = lookup_template_function (fns, NULL_TREE);
3051 if (declarator == error_mark_node)
3052 return error_mark_node;
3054 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3056 if (!explicit_instantiation)
3057 /* A specialization in class scope. This is invalid,
3058 but the error will already have been flagged by
3059 check_specialization_scope. */
3060 return error_mark_node;
3061 else
3063 /* It's not valid to write an explicit instantiation in
3064 class scope, e.g.:
3066 class C { template void f(); }
3068 This case is caught by the parser. However, on
3069 something like:
3071 template class C { void f(); };
3073 (which is invalid) we can get here. The error will be
3074 issued later. */
3078 return decl;
3080 else if (ctype != NULL_TREE
3081 && (identifier_p (TREE_OPERAND (declarator, 0))))
3083 // We'll match variable templates in start_decl.
3084 if (VAR_P (decl))
3085 return decl;
3087 /* Find the list of functions in ctype that have the same
3088 name as the declared function. */
3089 tree name = TREE_OPERAND (declarator, 0);
3091 if (constructor_name_p (name, ctype))
3093 if (DECL_CONSTRUCTOR_P (decl)
3094 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3095 : !CLASSTYPE_DESTRUCTOR (ctype))
3097 /* From [temp.expl.spec]:
3099 If such an explicit specialization for the member
3100 of a class template names an implicitly-declared
3101 special member function (clause _special_), the
3102 program is ill-formed.
3104 Similar language is found in [temp.explicit]. */
3105 error ("specialization of implicitly-declared special member function");
3106 return error_mark_node;
3109 name = DECL_NAME (decl);
3112 /* For a type-conversion operator, We might be looking for
3113 `operator int' which will be a specialization of
3114 `operator T'. Grab all the conversion operators, and
3115 then select from them. */
3116 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3117 ? conv_op_identifier : name);
3119 if (fns == NULL_TREE)
3121 error ("no member function %qD declared in %qT", name, ctype);
3122 return error_mark_node;
3124 else
3125 TREE_OPERAND (declarator, 0) = fns;
3128 /* Figure out what exactly is being specialized at this point.
3129 Note that for an explicit instantiation, even one for a
3130 member function, we cannot tell a priori whether the
3131 instantiation is for a member template, or just a member
3132 function of a template class. Even if a member template is
3133 being instantiated, the member template arguments may be
3134 elided if they can be deduced from the rest of the
3135 declaration. */
3136 tmpl = determine_specialization (declarator, decl,
3137 &targs,
3138 member_specialization,
3139 template_count,
3140 tsk);
3142 if (!tmpl || tmpl == error_mark_node)
3143 /* We couldn't figure out what this declaration was
3144 specializing. */
3145 return error_mark_node;
3146 else
3148 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3150 auto_diagnostic_group d;
3151 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3152 "friend declaration %qD is not visible to "
3153 "explicit specialization", tmpl))
3154 inform (DECL_SOURCE_LOCATION (tmpl),
3155 "friend declaration here");
3158 if (!ctype && !is_friend
3159 && CP_DECL_CONTEXT (decl) == current_namespace)
3160 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3162 tree gen_tmpl = most_general_template (tmpl);
3164 if (explicit_instantiation)
3166 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3167 is done by do_decl_instantiation later. */
3169 int arg_depth = TMPL_ARGS_DEPTH (targs);
3170 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3172 if (arg_depth > parm_depth)
3174 /* If TMPL is not the most general template (for
3175 example, if TMPL is a friend template that is
3176 injected into namespace scope), then there will
3177 be too many levels of TARGS. Remove some of them
3178 here. */
3179 int i;
3180 tree new_targs;
3182 new_targs = make_tree_vec (parm_depth);
3183 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3184 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3185 = TREE_VEC_ELT (targs, i);
3186 targs = new_targs;
3189 return instantiate_template (tmpl, targs, tf_error);
3192 /* If we thought that the DECL was a member function, but it
3193 turns out to be specializing a static member function,
3194 make DECL a static member function as well. */
3195 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3196 && DECL_STATIC_FUNCTION_P (tmpl)
3197 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3198 revert_static_member_fn (decl);
3200 /* If this is a specialization of a member template of a
3201 template class, we want to return the TEMPLATE_DECL, not
3202 the specialization of it. */
3203 if (tsk == tsk_template && !was_template_id)
3205 tree result = DECL_TEMPLATE_RESULT (tmpl);
3206 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3207 DECL_INITIAL (result) = NULL_TREE;
3208 if (have_def)
3210 tree parm;
3211 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3212 DECL_SOURCE_LOCATION (result)
3213 = DECL_SOURCE_LOCATION (decl);
3214 /* We want to use the argument list specified in the
3215 definition, not in the original declaration. */
3216 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3217 for (parm = DECL_ARGUMENTS (result); parm;
3218 parm = DECL_CHAIN (parm))
3219 DECL_CONTEXT (parm) = result;
3221 return register_specialization (tmpl, gen_tmpl, targs,
3222 is_friend, 0);
3225 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3226 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3228 if (was_template_id)
3229 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3231 /* Inherit default function arguments from the template
3232 DECL is specializing. */
3233 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3234 copy_default_args_to_explicit_spec (decl);
3236 /* This specialization has the same protection as the
3237 template it specializes. */
3238 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3239 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3241 /* 7.1.1-1 [dcl.stc]
3243 A storage-class-specifier shall not be specified in an
3244 explicit specialization...
3246 The parser rejects these, so unless action is taken here,
3247 explicit function specializations will always appear with
3248 global linkage.
3250 The action recommended by the C++ CWG in response to C++
3251 defect report 605 is to make the storage class and linkage
3252 of the explicit specialization match the templated function:
3254 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3256 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3258 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3259 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3261 /* A concept cannot be specialized. */
3262 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3264 error ("explicit specialization of function concept %qD",
3265 gen_tmpl);
3266 return error_mark_node;
3269 /* This specialization has the same linkage and visibility as
3270 the function template it specializes. */
3271 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3272 if (! TREE_PUBLIC (decl))
3274 DECL_INTERFACE_KNOWN (decl) = 1;
3275 DECL_NOT_REALLY_EXTERN (decl) = 1;
3277 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3278 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3280 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3281 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3285 /* If DECL is a friend declaration, declared using an
3286 unqualified name, the namespace associated with DECL may
3287 have been set incorrectly. For example, in:
3289 template <typename T> void f(T);
3290 namespace N {
3291 struct S { friend void f<int>(int); }
3294 we will have set the DECL_CONTEXT for the friend
3295 declaration to N, rather than to the global namespace. */
3296 if (DECL_NAMESPACE_SCOPE_P (decl))
3297 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3299 if (is_friend && !have_def)
3300 /* This is not really a declaration of a specialization.
3301 It's just the name of an instantiation. But, it's not
3302 a request for an instantiation, either. */
3303 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3304 else if (TREE_CODE (decl) == FUNCTION_DECL)
3305 /* A specialization is not necessarily COMDAT. */
3306 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3307 && DECL_DECLARED_INLINE_P (decl));
3308 else if (VAR_P (decl))
3309 DECL_COMDAT (decl) = false;
3311 /* If this is a full specialization, register it so that we can find
3312 it again. Partial specializations will be registered in
3313 process_partial_specialization. */
3314 if (!processing_template_decl)
3316 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3318 decl = register_specialization (decl, gen_tmpl, targs,
3319 is_friend, 0);
3323 /* A 'structor should already have clones. */
3324 gcc_assert (decl == error_mark_node
3325 || variable_template_p (tmpl)
3326 || !(DECL_CONSTRUCTOR_P (decl)
3327 || DECL_DESTRUCTOR_P (decl))
3328 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3332 return decl;
3335 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3336 parameters. These are represented in the same format used for
3337 DECL_TEMPLATE_PARMS. */
3340 comp_template_parms (const_tree parms1, const_tree parms2)
3342 const_tree p1;
3343 const_tree p2;
3345 if (parms1 == parms2)
3346 return 1;
3348 for (p1 = parms1, p2 = parms2;
3349 p1 != NULL_TREE && p2 != NULL_TREE;
3350 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3352 tree t1 = TREE_VALUE (p1);
3353 tree t2 = TREE_VALUE (p2);
3354 int i;
3356 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3357 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3359 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3360 return 0;
3362 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3364 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3365 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3367 /* If either of the template parameters are invalid, assume
3368 they match for the sake of error recovery. */
3369 if (error_operand_p (parm1) || error_operand_p (parm2))
3370 return 1;
3372 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3373 return 0;
3375 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3376 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3377 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3378 continue;
3379 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3380 return 0;
3384 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3385 /* One set of parameters has more parameters lists than the
3386 other. */
3387 return 0;
3389 return 1;
3392 /* Returns true if two template parameters are declared with
3393 equivalent constraints. */
3395 static bool
3396 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3398 tree req1 = TREE_TYPE (parm1);
3399 tree req2 = TREE_TYPE (parm2);
3400 if (!req1 != !req2)
3401 return false;
3402 if (req1)
3403 return cp_tree_equal (req1, req2);
3404 return true;
3407 /* Returns true when two template parameters are equivalent. */
3409 static bool
3410 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3412 tree decl1 = TREE_VALUE (parm1);
3413 tree decl2 = TREE_VALUE (parm2);
3415 /* If either of the template parameters are invalid, assume
3416 they match for the sake of error recovery. */
3417 if (error_operand_p (decl1) || error_operand_p (decl2))
3418 return true;
3420 /* ... they declare parameters of the same kind. */
3421 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3422 return false;
3424 /* ... one parameter was introduced by a parameter declaration, then
3425 both are. This case arises as a result of eagerly rewriting declarations
3426 during parsing. */
3427 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3428 return false;
3430 /* ... if either declares a pack, they both do. */
3431 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3432 return false;
3434 if (TREE_CODE (decl1) == PARM_DECL)
3436 /* ... if they declare non-type parameters, the types are equivalent. */
3437 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3438 return false;
3440 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3442 /* ... if they declare template template parameters, their template
3443 parameter lists are equivalent. */
3444 if (!template_heads_equivalent_p (decl1, decl2))
3445 return false;
3448 /* ... if they are declared with a qualified-concept name, they both
3449 are, and those names are equivalent. */
3450 return template_parameter_constraints_equivalent_p (parm1, parm2);
3453 /* Returns true if two template parameters lists are equivalent.
3454 Two template parameter lists are equivalent if they have the
3455 same length and their corresponding parameters are equivalent.
3457 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3458 data structure returned by DECL_TEMPLATE_PARMS.
3460 This is generally the same implementation as comp_template_parms
3461 except that it also the concept names and arguments used to
3462 introduce parameters. */
3464 static bool
3465 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3467 if (parms1 == parms2)
3468 return true;
3470 const_tree p1 = parms1;
3471 const_tree p2 = parms2;
3472 while (p1 != NULL_TREE && p2 != NULL_TREE)
3474 tree list1 = TREE_VALUE (p1);
3475 tree list2 = TREE_VALUE (p2);
3477 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3478 return 0;
3480 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3482 tree parm1 = TREE_VEC_ELT (list1, i);
3483 tree parm2 = TREE_VEC_ELT (list2, i);
3484 if (!template_parameters_equivalent_p (parm1, parm2))
3485 return false;
3488 p1 = TREE_CHAIN (p1);
3489 p2 = TREE_CHAIN (p2);
3492 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3493 return false;
3495 return true;
3498 /* Return true if the requires-clause of the template parameter lists are
3499 equivalent and false otherwise. */
3500 static bool
3501 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3503 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3504 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3505 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3506 return false;
3507 if (!cp_tree_equal (req1, req2))
3508 return false;
3509 return true;
3512 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3513 Two template heads are equivalent if their template parameter
3514 lists are equivalent and their requires clauses are equivalent.
3516 In pre-C++20, this is equivalent to calling comp_template_parms
3517 for the template parameters of TMPL1 and TMPL2. */
3519 bool
3520 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3522 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3523 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3525 /* Don't change the matching rules for pre-C++20. */
3526 if (cxx_dialect < cxx20)
3527 return comp_template_parms (parms1, parms2);
3529 /* ... have the same number of template parameters, and their
3530 corresponding parameters are equivalent. */
3531 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3532 return false;
3534 /* ... if either has a requires-clause, they both do and their
3535 corresponding constraint-expressions are equivalent. */
3536 return template_requirements_equivalent_p (parms1, parms2);
3539 /* Determine whether PARM is a parameter pack. */
3541 bool
3542 template_parameter_pack_p (const_tree parm)
3544 /* Determine if we have a non-type template parameter pack. */
3545 if (TREE_CODE (parm) == PARM_DECL)
3546 return (DECL_TEMPLATE_PARM_P (parm)
3547 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3548 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3549 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3551 /* If this is a list of template parameters, we could get a
3552 TYPE_DECL or a TEMPLATE_DECL. */
3553 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3554 parm = TREE_TYPE (parm);
3556 /* Otherwise it must be a type template parameter. */
3557 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3558 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3559 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3562 /* Determine if T is a function parameter pack. */
3564 bool
3565 function_parameter_pack_p (const_tree t)
3567 if (t && TREE_CODE (t) == PARM_DECL)
3568 return DECL_PACK_P (t);
3569 return false;
3572 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3573 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3575 tree
3576 get_function_template_decl (const_tree primary_func_tmpl_inst)
3578 if (! primary_func_tmpl_inst
3579 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3580 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3581 return NULL;
3583 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3586 /* Return true iff the function parameter PARAM_DECL was expanded
3587 from the function parameter pack PACK. */
3589 bool
3590 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3592 if (DECL_ARTIFICIAL (param_decl)
3593 || !function_parameter_pack_p (pack))
3594 return false;
3596 /* The parameter pack and its pack arguments have the same
3597 DECL_PARM_INDEX. */
3598 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3601 /* Determine whether ARGS describes a variadic template args list,
3602 i.e., one that is terminated by a template argument pack. */
3604 static bool
3605 template_args_variadic_p (tree args)
3607 int nargs;
3608 tree last_parm;
3610 if (args == NULL_TREE)
3611 return false;
3613 args = INNERMOST_TEMPLATE_ARGS (args);
3614 nargs = TREE_VEC_LENGTH (args);
3616 if (nargs == 0)
3617 return false;
3619 last_parm = TREE_VEC_ELT (args, nargs - 1);
3621 return ARGUMENT_PACK_P (last_parm);
3624 /* Generate a new name for the parameter pack name NAME (an
3625 IDENTIFIER_NODE) that incorporates its */
3627 static tree
3628 make_ith_pack_parameter_name (tree name, int i)
3630 /* Munge the name to include the parameter index. */
3631 #define NUMBUF_LEN 128
3632 char numbuf[NUMBUF_LEN];
3633 char* newname;
3634 int newname_len;
3636 if (name == NULL_TREE)
3637 return name;
3638 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3639 newname_len = IDENTIFIER_LENGTH (name)
3640 + strlen (numbuf) + 2;
3641 newname = (char*)alloca (newname_len);
3642 snprintf (newname, newname_len,
3643 "%s#%i", IDENTIFIER_POINTER (name), i);
3644 return get_identifier (newname);
3647 /* Return true if T is a primary function, class or alias template
3648 specialization, not including the template pattern. */
3650 bool
3651 primary_template_specialization_p (const_tree t)
3653 if (!t)
3654 return false;
3656 if (VAR_OR_FUNCTION_DECL_P (t))
3657 return (DECL_LANG_SPECIFIC (t)
3658 && DECL_USE_TEMPLATE (t)
3659 && DECL_TEMPLATE_INFO (t)
3660 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3661 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3662 return (CLASSTYPE_TEMPLATE_INFO (t)
3663 && CLASSTYPE_USE_TEMPLATE (t)
3664 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3665 else if (alias_template_specialization_p (t, nt_transparent))
3666 return true;
3667 return false;
3670 /* Return true if PARM is a template template parameter. */
3672 bool
3673 template_template_parameter_p (const_tree parm)
3675 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3678 /* Return true iff PARM is a DECL representing a type template
3679 parameter. */
3681 bool
3682 template_type_parameter_p (const_tree parm)
3684 return (parm
3685 && (TREE_CODE (parm) == TYPE_DECL
3686 || TREE_CODE (parm) == TEMPLATE_DECL)
3687 && DECL_TEMPLATE_PARM_P (parm));
3690 /* Return the template parameters of T if T is a
3691 primary template instantiation, NULL otherwise. */
3693 tree
3694 get_primary_template_innermost_parameters (const_tree t)
3696 tree parms = NULL, template_info = NULL;
3698 if ((template_info = get_template_info (t))
3699 && primary_template_specialization_p (t))
3700 parms = INNERMOST_TEMPLATE_PARMS
3701 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3703 return parms;
3706 /* Returns the template arguments of T if T is a template instantiation,
3707 NULL otherwise. */
3709 tree
3710 get_template_innermost_arguments (const_tree t)
3712 tree args = NULL, template_info = NULL;
3714 if ((template_info = get_template_info (t))
3715 && TI_ARGS (template_info))
3716 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3718 return args;
3721 /* Return the argument pack elements of T if T is a template argument pack,
3722 NULL otherwise. */
3724 tree
3725 get_template_argument_pack_elems (const_tree t)
3727 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3728 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3729 return NULL;
3731 return ARGUMENT_PACK_ARGS (t);
3734 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3735 ARGUMENT_PACK_SELECT represents. */
3737 static tree
3738 argument_pack_select_arg (tree t)
3740 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3741 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3743 /* If the selected argument is an expansion E, that most likely means we were
3744 called from gen_elem_of_pack_expansion_instantiation during the
3745 substituting of an argument pack (of which the Ith element is a pack
3746 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3747 In this case, the Ith element resulting from this substituting is going to
3748 be a pack expansion, which pattern is the pattern of E. Let's return the
3749 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3750 resulting pack expansion from it. */
3751 if (PACK_EXPANSION_P (arg))
3753 /* Make sure we aren't throwing away arg info. */
3754 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3755 arg = PACK_EXPANSION_PATTERN (arg);
3758 return arg;
3762 /* True iff FN is a function representing a built-in variadic parameter
3763 pack. */
3765 bool
3766 builtin_pack_fn_p (tree fn)
3768 if (!fn
3769 || TREE_CODE (fn) != FUNCTION_DECL
3770 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3771 return false;
3773 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3774 return true;
3776 return false;
3779 /* True iff CALL is a call to a function representing a built-in variadic
3780 parameter pack. */
3782 static bool
3783 builtin_pack_call_p (tree call)
3785 if (TREE_CODE (call) != CALL_EXPR)
3786 return false;
3787 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3790 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3792 static tree
3793 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3794 tree in_decl)
3796 tree ohi = CALL_EXPR_ARG (call, 0);
3797 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3798 false/*fn*/, true/*int_cst*/);
3800 if (value_dependent_expression_p (hi))
3802 if (hi != ohi)
3804 call = copy_node (call);
3805 CALL_EXPR_ARG (call, 0) = hi;
3807 tree ex = make_pack_expansion (call, complain);
3808 tree vec = make_tree_vec (1);
3809 TREE_VEC_ELT (vec, 0) = ex;
3810 return vec;
3812 else
3814 hi = cxx_constant_value (hi);
3815 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3817 /* Calculate the largest value of len that won't make the size of the vec
3818 overflow an int. The compiler will exceed resource limits long before
3819 this, but it seems a decent place to diagnose. */
3820 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3822 if (len < 0 || len > max)
3824 if ((complain & tf_error)
3825 && hi != error_mark_node)
3826 error ("argument to %<__integer_pack%> must be between 0 and %d",
3827 max);
3828 return error_mark_node;
3831 tree vec = make_tree_vec (len);
3833 for (int i = 0; i < len; ++i)
3834 TREE_VEC_ELT (vec, i) = size_int (i);
3836 return vec;
3840 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3841 CALL. */
3843 static tree
3844 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3845 tree in_decl)
3847 if (!builtin_pack_call_p (call))
3848 return NULL_TREE;
3850 tree fn = CALL_EXPR_FN (call);
3852 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3853 return expand_integer_pack (call, args, complain, in_decl);
3855 return NULL_TREE;
3858 /* Structure used to track the progress of find_parameter_packs_r. */
3859 struct find_parameter_pack_data
3861 /* TREE_LIST that will contain all of the parameter packs found by
3862 the traversal. */
3863 tree* parameter_packs;
3865 /* Set of AST nodes that have been visited by the traversal. */
3866 hash_set<tree> *visited;
3868 /* True iff we're making a type pack expansion. */
3869 bool type_pack_expansion_p;
3872 /* Identifies all of the argument packs that occur in a template
3873 argument and appends them to the TREE_LIST inside DATA, which is a
3874 find_parameter_pack_data structure. This is a subroutine of
3875 make_pack_expansion and uses_parameter_packs. */
3876 static tree
3877 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3879 tree t = *tp;
3880 struct find_parameter_pack_data* ppd =
3881 (struct find_parameter_pack_data*)data;
3882 bool parameter_pack_p = false;
3884 #define WALK_SUBTREE(NODE) \
3885 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3886 ppd, ppd->visited) \
3888 /* Don't look through typedefs; we are interested in whether a
3889 parameter pack is actually written in the expression/type we're
3890 looking at, not the target type. */
3891 if (TYPE_P (t) && typedef_variant_p (t))
3893 /* But do look at arguments for an alias template. */
3894 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3895 cp_walk_tree (&TI_ARGS (tinfo),
3896 &find_parameter_packs_r,
3897 ppd, ppd->visited);
3898 *walk_subtrees = 0;
3899 return NULL_TREE;
3902 /* Identify whether this is a parameter pack or not. */
3903 switch (TREE_CODE (t))
3905 case TEMPLATE_PARM_INDEX:
3906 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3907 parameter_pack_p = true;
3908 break;
3910 case TEMPLATE_TYPE_PARM:
3911 t = TYPE_MAIN_VARIANT (t);
3912 /* FALLTHRU */
3913 case TEMPLATE_TEMPLATE_PARM:
3914 /* If the placeholder appears in the decl-specifier-seq of a function
3915 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3916 is a pack expansion, the invented template parameter is a template
3917 parameter pack. */
3918 if (ppd->type_pack_expansion_p && is_auto (t))
3919 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3920 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3921 parameter_pack_p = true;
3922 break;
3924 case FIELD_DECL:
3925 case PARM_DECL:
3926 if (DECL_PACK_P (t))
3928 /* We don't want to walk into the type of a PARM_DECL,
3929 because we don't want to see the type parameter pack. */
3930 *walk_subtrees = 0;
3931 parameter_pack_p = true;
3933 break;
3935 case VAR_DECL:
3936 if (DECL_PACK_P (t))
3938 /* We don't want to walk into the type of a variadic capture proxy,
3939 because we don't want to see the type parameter pack. */
3940 *walk_subtrees = 0;
3941 parameter_pack_p = true;
3943 else if (variable_template_specialization_p (t))
3945 cp_walk_tree (&DECL_TI_ARGS (t),
3946 find_parameter_packs_r,
3947 ppd, ppd->visited);
3948 *walk_subtrees = 0;
3950 break;
3952 case CALL_EXPR:
3953 if (builtin_pack_call_p (t))
3954 parameter_pack_p = true;
3955 break;
3957 case BASES:
3958 parameter_pack_p = true;
3959 break;
3960 default:
3961 /* Not a parameter pack. */
3962 break;
3965 if (parameter_pack_p)
3967 /* Add this parameter pack to the list. */
3968 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3971 if (TYPE_P (t))
3972 cp_walk_tree (&TYPE_CONTEXT (t),
3973 &find_parameter_packs_r, ppd, ppd->visited);
3975 /* This switch statement will return immediately if we don't find a
3976 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3977 switch (TREE_CODE (t))
3979 case BOUND_TEMPLATE_TEMPLATE_PARM:
3980 /* Check the template itself. */
3981 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3982 &find_parameter_packs_r, ppd, ppd->visited);
3983 return NULL_TREE;
3985 case DECL_EXPR:
3987 tree decl = DECL_EXPR_DECL (t);
3988 /* Ignore the declaration of a capture proxy for a parameter pack. */
3989 if (is_capture_proxy (decl))
3990 *walk_subtrees = 0;
3991 if (is_typedef_decl (decl))
3992 /* Since we stop at typedefs above, we need to look through them at
3993 the point of the DECL_EXPR. */
3994 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3995 &find_parameter_packs_r, ppd, ppd->visited);
3996 return NULL_TREE;
3999 case TEMPLATE_DECL:
4000 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4001 return NULL_TREE;
4002 cp_walk_tree (&TREE_TYPE (t),
4003 &find_parameter_packs_r, ppd, ppd->visited);
4004 return NULL_TREE;
4006 case TYPE_PACK_EXPANSION:
4007 case EXPR_PACK_EXPANSION:
4008 *walk_subtrees = 0;
4009 return NULL_TREE;
4011 case INTEGER_TYPE:
4012 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4013 ppd, ppd->visited);
4014 *walk_subtrees = 0;
4015 return NULL_TREE;
4017 case IDENTIFIER_NODE:
4018 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4019 ppd->visited);
4020 *walk_subtrees = 0;
4021 return NULL_TREE;
4023 case LAMBDA_EXPR:
4025 /* Since we defer implicit capture, look in the parms and body. */
4026 tree fn = lambda_function (t);
4027 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4028 ppd->visited);
4029 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4030 ppd->visited);
4031 return NULL_TREE;
4034 case DECLTYPE_TYPE:
4036 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4037 type_pack_expansion_p to false so that any placeholders
4038 within the expression don't get marked as parameter packs. */
4039 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4040 ppd->type_pack_expansion_p = false;
4041 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4042 ppd, ppd->visited);
4043 ppd->type_pack_expansion_p = type_pack_expansion_p;
4044 *walk_subtrees = 0;
4045 return NULL_TREE;
4048 case IF_STMT:
4049 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4050 ppd, ppd->visited);
4051 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4052 ppd, ppd->visited);
4053 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4054 ppd, ppd->visited);
4055 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4056 *walk_subtrees = 0;
4057 return NULL_TREE;
4059 case TAG_DEFN:
4060 t = TREE_TYPE (t);
4061 if (CLASS_TYPE_P (t))
4062 /* Local class, need to look through the whole definition. */
4063 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4064 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4065 ppd, ppd->visited);
4066 else
4067 /* Enum, look at the values. */
4068 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4069 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4070 &find_parameter_packs_r,
4071 ppd, ppd->visited);
4072 return NULL_TREE;
4074 case FUNCTION_TYPE:
4075 case METHOD_TYPE:
4076 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4077 break;
4079 default:
4080 return NULL_TREE;
4083 #undef WALK_SUBTREE
4085 return NULL_TREE;
4088 /* Determines if the expression or type T uses any parameter packs. */
4089 tree
4090 uses_parameter_packs (tree t)
4092 tree parameter_packs = NULL_TREE;
4093 struct find_parameter_pack_data ppd;
4094 ppd.parameter_packs = &parameter_packs;
4095 ppd.visited = new hash_set<tree>;
4096 ppd.type_pack_expansion_p = false;
4097 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4098 delete ppd.visited;
4099 return parameter_packs;
4102 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4103 representation a base-class initializer into a parameter pack
4104 expansion. If all goes well, the resulting node will be an
4105 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4106 respectively. */
4107 tree
4108 make_pack_expansion (tree arg, tsubst_flags_t complain)
4110 tree result;
4111 tree parameter_packs = NULL_TREE;
4112 bool for_types = false;
4113 struct find_parameter_pack_data ppd;
4115 if (!arg || arg == error_mark_node)
4116 return arg;
4118 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4120 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4121 class initializer. In this case, the TREE_PURPOSE will be a
4122 _TYPE node (representing the base class expansion we're
4123 initializing) and the TREE_VALUE will be a TREE_LIST
4124 containing the initialization arguments.
4126 The resulting expansion looks somewhat different from most
4127 expansions. Rather than returning just one _EXPANSION, we
4128 return a TREE_LIST whose TREE_PURPOSE is a
4129 TYPE_PACK_EXPANSION containing the bases that will be
4130 initialized. The TREE_VALUE will be identical to the
4131 original TREE_VALUE, which is a list of arguments that will
4132 be passed to each base. We do not introduce any new pack
4133 expansion nodes into the TREE_VALUE (although it is possible
4134 that some already exist), because the TREE_PURPOSE and
4135 TREE_VALUE all need to be expanded together with the same
4136 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4137 resulting TREE_PURPOSE will mention the parameter packs in
4138 both the bases and the arguments to the bases. */
4139 tree purpose;
4140 tree value;
4141 tree parameter_packs = NULL_TREE;
4143 /* Determine which parameter packs will be used by the base
4144 class expansion. */
4145 ppd.visited = new hash_set<tree>;
4146 ppd.parameter_packs = &parameter_packs;
4147 ppd.type_pack_expansion_p = false;
4148 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4149 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4150 &ppd, ppd.visited);
4152 if (parameter_packs == NULL_TREE)
4154 if (complain & tf_error)
4155 error ("base initializer expansion %qT contains no parameter packs",
4156 arg);
4157 delete ppd.visited;
4158 return error_mark_node;
4161 if (TREE_VALUE (arg) != void_type_node)
4163 /* Collect the sets of parameter packs used in each of the
4164 initialization arguments. */
4165 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4167 /* Determine which parameter packs will be expanded in this
4168 argument. */
4169 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4170 &ppd, ppd.visited);
4174 delete ppd.visited;
4176 /* Create the pack expansion type for the base type. */
4177 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4178 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4179 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4180 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4182 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4183 they will rarely be compared to anything. */
4184 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4186 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4189 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4190 for_types = true;
4192 /* Build the PACK_EXPANSION_* node. */
4193 result = for_types
4194 ? cxx_make_type (TYPE_PACK_EXPANSION)
4195 : make_node (EXPR_PACK_EXPANSION);
4196 SET_PACK_EXPANSION_PATTERN (result, arg);
4197 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4199 /* Propagate type and const-expression information. */
4200 TREE_TYPE (result) = TREE_TYPE (arg);
4201 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4202 /* Mark this read now, since the expansion might be length 0. */
4203 mark_exp_read (arg);
4205 else
4206 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4207 they will rarely be compared to anything. */
4208 SET_TYPE_STRUCTURAL_EQUALITY (result);
4210 /* Determine which parameter packs will be expanded. */
4211 ppd.parameter_packs = &parameter_packs;
4212 ppd.visited = new hash_set<tree>;
4213 ppd.type_pack_expansion_p = TYPE_P (arg);
4214 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4215 delete ppd.visited;
4217 /* Make sure we found some parameter packs. */
4218 if (parameter_packs == NULL_TREE)
4220 if (complain & tf_error)
4222 if (TYPE_P (arg))
4223 error ("expansion pattern %qT contains no parameter packs", arg);
4224 else
4225 error ("expansion pattern %qE contains no parameter packs", arg);
4227 return error_mark_node;
4229 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4231 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4233 return result;
4236 /* Checks T for any "bare" parameter packs, which have not yet been
4237 expanded, and issues an error if any are found. This operation can
4238 only be done on full expressions or types (e.g., an expression
4239 statement, "if" condition, etc.), because we could have expressions like:
4241 foo(f(g(h(args)))...)
4243 where "args" is a parameter pack. check_for_bare_parameter_packs
4244 should not be called for the subexpressions args, h(args),
4245 g(h(args)), or f(g(h(args))), because we would produce erroneous
4246 error messages.
4248 Returns TRUE and emits an error if there were bare parameter packs,
4249 returns FALSE otherwise. */
4250 bool
4251 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4253 tree parameter_packs = NULL_TREE;
4254 struct find_parameter_pack_data ppd;
4256 if (!processing_template_decl || !t || t == error_mark_node)
4257 return false;
4259 if (TREE_CODE (t) == TYPE_DECL)
4260 t = TREE_TYPE (t);
4262 ppd.parameter_packs = &parameter_packs;
4263 ppd.visited = new hash_set<tree>;
4264 ppd.type_pack_expansion_p = false;
4265 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4266 delete ppd.visited;
4268 /* It's OK for a lambda to have an unexpanded parameter pack from the
4269 containing context, but do complain about unexpanded capture packs. */
4270 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4271 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4272 for (; parameter_packs;
4273 parameter_packs = TREE_CHAIN (parameter_packs))
4275 tree pack = TREE_VALUE (parameter_packs);
4276 if (is_capture_proxy (pack))
4277 break;
4280 if (parameter_packs)
4282 if (loc == UNKNOWN_LOCATION)
4283 loc = cp_expr_loc_or_input_loc (t);
4284 error_at (loc, "parameter packs not expanded with %<...%>:");
4285 while (parameter_packs)
4287 tree pack = TREE_VALUE (parameter_packs);
4288 tree name = NULL_TREE;
4290 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4291 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4292 name = TYPE_NAME (pack);
4293 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4294 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4295 else if (TREE_CODE (pack) == CALL_EXPR)
4296 name = DECL_NAME (CALL_EXPR_FN (pack));
4297 else
4298 name = DECL_NAME (pack);
4300 if (name)
4301 inform (loc, " %qD", name);
4302 else
4303 inform (loc, " %s", "<anonymous>");
4305 parameter_packs = TREE_CHAIN (parameter_packs);
4308 return true;
4311 return false;
4314 /* Expand any parameter packs that occur in the template arguments in
4315 ARGS. */
4316 tree
4317 expand_template_argument_pack (tree args)
4319 if (args == error_mark_node)
4320 return error_mark_node;
4322 tree result_args = NULL_TREE;
4323 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4324 int num_result_args = -1;
4325 int non_default_args_count = -1;
4327 /* First, determine if we need to expand anything, and the number of
4328 slots we'll need. */
4329 for (in_arg = 0; in_arg < nargs; ++in_arg)
4331 tree arg = TREE_VEC_ELT (args, in_arg);
4332 if (arg == NULL_TREE)
4333 return args;
4334 if (ARGUMENT_PACK_P (arg))
4336 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4337 if (num_result_args < 0)
4338 num_result_args = in_arg + num_packed;
4339 else
4340 num_result_args += num_packed;
4342 else
4344 if (num_result_args >= 0)
4345 num_result_args++;
4349 /* If no expansion is necessary, we're done. */
4350 if (num_result_args < 0)
4351 return args;
4353 /* Expand arguments. */
4354 result_args = make_tree_vec (num_result_args);
4355 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4356 non_default_args_count =
4357 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4358 for (in_arg = 0; in_arg < nargs; ++in_arg)
4360 tree arg = TREE_VEC_ELT (args, in_arg);
4361 if (ARGUMENT_PACK_P (arg))
4363 tree packed = ARGUMENT_PACK_ARGS (arg);
4364 int i, num_packed = TREE_VEC_LENGTH (packed);
4365 for (i = 0; i < num_packed; ++i, ++out_arg)
4366 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4367 if (non_default_args_count > 0)
4368 non_default_args_count += num_packed - 1;
4370 else
4372 TREE_VEC_ELT (result_args, out_arg) = arg;
4373 ++out_arg;
4376 if (non_default_args_count >= 0)
4377 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4378 return result_args;
4381 /* Checks if DECL shadows a template parameter.
4383 [temp.local]: A template-parameter shall not be redeclared within its
4384 scope (including nested scopes).
4386 Emits an error and returns TRUE if the DECL shadows a parameter,
4387 returns FALSE otherwise. */
4389 bool
4390 check_template_shadow (tree decl)
4392 tree olddecl;
4394 /* If we're not in a template, we can't possibly shadow a template
4395 parameter. */
4396 if (!current_template_parms)
4397 return true;
4399 /* Figure out what we're shadowing. */
4400 decl = OVL_FIRST (decl);
4401 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4403 /* If there's no previous binding for this name, we're not shadowing
4404 anything, let alone a template parameter. */
4405 if (!olddecl)
4406 return true;
4408 /* If we're not shadowing a template parameter, we're done. Note
4409 that OLDDECL might be an OVERLOAD (or perhaps even an
4410 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4411 node. */
4412 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4413 return true;
4415 /* We check for decl != olddecl to avoid bogus errors for using a
4416 name inside a class. We check TPFI to avoid duplicate errors for
4417 inline member templates. */
4418 if (decl == olddecl
4419 || (DECL_TEMPLATE_PARM_P (decl)
4420 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4421 return true;
4423 /* Don't complain about the injected class name, as we've already
4424 complained about the class itself. */
4425 if (DECL_SELF_REFERENCE_P (decl))
4426 return false;
4428 if (DECL_TEMPLATE_PARM_P (decl))
4429 error ("declaration of template parameter %q+D shadows "
4430 "template parameter", decl);
4431 else
4432 error ("declaration of %q+#D shadows template parameter", decl);
4433 inform (DECL_SOURCE_LOCATION (olddecl),
4434 "template parameter %qD declared here", olddecl);
4435 return false;
4438 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4439 ORIG_LEVEL, DECL, and TYPE. */
4441 static tree
4442 build_template_parm_index (int index,
4443 int level,
4444 int orig_level,
4445 tree decl,
4446 tree type)
4448 tree t = make_node (TEMPLATE_PARM_INDEX);
4449 TEMPLATE_PARM_IDX (t) = index;
4450 TEMPLATE_PARM_LEVEL (t) = level;
4451 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4452 TEMPLATE_PARM_DECL (t) = decl;
4453 TREE_TYPE (t) = type;
4454 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4455 TREE_READONLY (t) = TREE_READONLY (decl);
4457 return t;
4460 /* Find the canonical type parameter for the given template type
4461 parameter. Returns the canonical type parameter, which may be TYPE
4462 if no such parameter existed. */
4464 tree
4465 canonical_type_parameter (tree type)
4467 int idx = TEMPLATE_TYPE_IDX (type);
4469 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4471 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4472 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4474 for (tree list = (*canonical_template_parms)[idx];
4475 list; list = TREE_CHAIN (list))
4476 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4477 return TREE_VALUE (list);
4479 (*canonical_template_parms)[idx]
4480 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4481 return type;
4484 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4485 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4486 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4487 new one is created. */
4489 static tree
4490 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4491 tsubst_flags_t complain)
4493 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4494 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4495 != TEMPLATE_PARM_LEVEL (index) - levels)
4496 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4498 tree orig_decl = TEMPLATE_PARM_DECL (index);
4500 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4501 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4502 type);
4503 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4504 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4505 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4506 DECL_ARTIFICIAL (decl) = 1;
4507 SET_DECL_TEMPLATE_PARM_P (decl);
4509 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4510 TEMPLATE_PARM_LEVEL (index) - levels,
4511 TEMPLATE_PARM_ORIG_LEVEL (index),
4512 decl, type);
4513 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4514 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4515 = TEMPLATE_PARM_PARAMETER_PACK (index);
4517 /* Template template parameters need this. */
4518 tree inner = decl;
4519 if (TREE_CODE (decl) == TEMPLATE_DECL)
4521 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4522 TYPE_DECL, DECL_NAME (decl), type);
4523 DECL_TEMPLATE_RESULT (decl) = inner;
4524 DECL_ARTIFICIAL (inner) = true;
4525 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4526 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4529 /* Attach the TPI to the decl. */
4530 if (TREE_CODE (inner) == TYPE_DECL)
4531 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4532 else
4533 DECL_INITIAL (decl) = tpi;
4536 return TEMPLATE_PARM_DESCENDANTS (index);
4539 /* Process information from new template parameter PARM and append it
4540 to the LIST being built. This new parameter is a non-type
4541 parameter iff IS_NON_TYPE is true. This new parameter is a
4542 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4543 is in PARM_LOC. */
4545 tree
4546 process_template_parm (tree list, location_t parm_loc, tree parm,
4547 bool is_non_type, bool is_parameter_pack)
4549 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4550 tree prev = NULL_TREE;
4551 int idx = 0;
4553 if (list)
4555 prev = tree_last (list);
4557 tree p = TREE_VALUE (prev);
4558 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4559 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4560 else if (TREE_CODE (p) == PARM_DECL)
4561 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4563 ++idx;
4566 tree decl = NULL_TREE;
4567 tree defval = TREE_PURPOSE (parm);
4568 tree constr = TREE_TYPE (parm);
4570 if (is_non_type)
4572 parm = TREE_VALUE (parm);
4574 SET_DECL_TEMPLATE_PARM_P (parm);
4576 if (TREE_TYPE (parm) != error_mark_node)
4578 /* [temp.param]
4580 The top-level cv-qualifiers on the template-parameter are
4581 ignored when determining its type. */
4582 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4583 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4584 TREE_TYPE (parm) = error_mark_node;
4585 else if (uses_parameter_packs (TREE_TYPE (parm))
4586 && !is_parameter_pack
4587 /* If we're in a nested template parameter list, the template
4588 template parameter could be a parameter pack. */
4589 && processing_template_parmlist == 1)
4591 /* This template parameter is not a parameter pack, but it
4592 should be. Complain about "bare" parameter packs. */
4593 check_for_bare_parameter_packs (TREE_TYPE (parm));
4595 /* Recover by calling this a parameter pack. */
4596 is_parameter_pack = true;
4600 /* A template parameter is not modifiable. */
4601 TREE_CONSTANT (parm) = 1;
4602 TREE_READONLY (parm) = 1;
4603 decl = build_decl (parm_loc,
4604 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4605 TREE_CONSTANT (decl) = 1;
4606 TREE_READONLY (decl) = 1;
4607 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4608 = build_template_parm_index (idx, processing_template_decl,
4609 processing_template_decl,
4610 decl, TREE_TYPE (parm));
4612 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4613 = is_parameter_pack;
4615 else
4617 tree t;
4618 parm = TREE_VALUE (TREE_VALUE (parm));
4620 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4622 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4623 /* This is for distinguishing between real templates and template
4624 template parameters */
4625 TREE_TYPE (parm) = t;
4627 /* any_template_parm_r expects to be able to get the targs of a
4628 DECL_TEMPLATE_RESULT. */
4629 tree result = DECL_TEMPLATE_RESULT (parm);
4630 TREE_TYPE (result) = t;
4631 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4632 tree tinfo = build_template_info (parm, args);
4633 retrofit_lang_decl (result);
4634 DECL_TEMPLATE_INFO (result) = tinfo;
4636 decl = parm;
4638 else
4640 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4641 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4642 decl = build_decl (parm_loc,
4643 TYPE_DECL, parm, t);
4646 TYPE_NAME (t) = decl;
4647 TYPE_STUB_DECL (t) = decl;
4648 parm = decl;
4649 TEMPLATE_TYPE_PARM_INDEX (t)
4650 = build_template_parm_index (idx, processing_template_decl,
4651 processing_template_decl,
4652 decl, TREE_TYPE (parm));
4653 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4654 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4655 SET_TYPE_STRUCTURAL_EQUALITY (t);
4656 else
4657 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4659 DECL_ARTIFICIAL (decl) = 1;
4660 SET_DECL_TEMPLATE_PARM_P (decl);
4662 /* Build requirements for the type/template parameter.
4663 This must be done after SET_DECL_TEMPLATE_PARM_P or
4664 process_template_parm could fail. */
4665 tree reqs = finish_shorthand_constraint (parm, constr);
4667 decl = pushdecl (decl);
4668 if (!is_non_type)
4669 parm = decl;
4671 /* Build the parameter node linking the parameter declaration,
4672 its default argument (if any), and its constraints (if any). */
4673 parm = build_tree_list (defval, parm);
4674 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4676 if (prev)
4677 TREE_CHAIN (prev) = parm;
4678 else
4679 list = parm;
4681 return list;
4684 /* The end of a template parameter list has been reached. Process the
4685 tree list into a parameter vector, converting each parameter into a more
4686 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4687 as PARM_DECLs. */
4689 tree
4690 end_template_parm_list (tree parms)
4692 tree saved_parmlist = make_tree_vec (list_length (parms));
4694 /* Pop the dummy parameter level and add the real one. We do not
4695 morph the dummy parameter in place, as it might have been
4696 captured by a (nested) template-template-parm. */
4697 current_template_parms = TREE_CHAIN (current_template_parms);
4699 current_template_parms
4700 = tree_cons (size_int (processing_template_decl),
4701 saved_parmlist, current_template_parms);
4703 for (unsigned ix = 0; parms; ix++)
4705 tree parm = parms;
4706 parms = TREE_CHAIN (parms);
4707 TREE_CHAIN (parm) = NULL_TREE;
4709 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4712 --processing_template_parmlist;
4714 return saved_parmlist;
4717 // Explicitly indicate the end of the template parameter list. We assume
4718 // that the current template parameters have been constructed and/or
4719 // managed explicitly, as when creating new template template parameters
4720 // from a shorthand constraint.
4721 void
4722 end_template_parm_list ()
4724 --processing_template_parmlist;
4727 /* end_template_decl is called after a template declaration is seen. */
4729 void
4730 end_template_decl (void)
4732 reset_specialization ();
4734 if (! processing_template_decl)
4735 return;
4737 /* This matches the pushlevel in begin_template_parm_list. */
4738 finish_scope ();
4740 --processing_template_decl;
4741 current_template_parms = TREE_CHAIN (current_template_parms);
4744 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4745 thereof, and converts it into an argument suitable to be passed to
4746 the type substitution functions. Note that if the TREE_LIST contains
4747 an error_mark node, the returned argument is error_mark_node. */
4749 tree
4750 template_parm_to_arg (tree t)
4752 if (!t)
4753 return NULL_TREE;
4755 if (TREE_CODE (t) == TREE_LIST)
4756 t = TREE_VALUE (t);
4758 if (error_operand_p (t))
4759 return error_mark_node;
4761 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4763 if (TREE_CODE (t) == TYPE_DECL
4764 || TREE_CODE (t) == TEMPLATE_DECL)
4765 t = TREE_TYPE (t);
4766 else
4767 t = DECL_INITIAL (t);
4770 gcc_assert (TEMPLATE_PARM_P (t));
4772 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4773 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4775 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4777 /* Turn this argument into a TYPE_ARGUMENT_PACK
4778 with a single element, which expands T. */
4779 tree vec = make_tree_vec (1);
4780 if (CHECKING_P)
4781 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4783 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4785 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4786 SET_ARGUMENT_PACK_ARGS (t, vec);
4789 else
4791 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4793 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4794 with a single element, which expands T. */
4795 tree vec = make_tree_vec (1);
4796 if (CHECKING_P)
4797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4799 t = convert_from_reference (t);
4800 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4802 t = make_node (NONTYPE_ARGUMENT_PACK);
4803 SET_ARGUMENT_PACK_ARGS (t, vec);
4805 else
4806 t = convert_from_reference (t);
4808 return t;
4811 /* Given a single level of template parameters (a TREE_VEC), return it
4812 as a set of template arguments. */
4814 tree
4815 template_parms_level_to_args (tree parms)
4817 tree a = copy_node (parms);
4818 TREE_TYPE (a) = NULL_TREE;
4819 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4820 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4822 if (CHECKING_P)
4823 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4825 return a;
4828 /* Given a set of template parameters, return them as a set of template
4829 arguments. The template parameters are represented as a TREE_VEC, in
4830 the form documented in cp-tree.h for template arguments. */
4832 tree
4833 template_parms_to_args (tree parms)
4835 tree header;
4836 tree args = NULL_TREE;
4837 int length = TMPL_PARMS_DEPTH (parms);
4838 int l = length;
4840 /* If there is only one level of template parameters, we do not
4841 create a TREE_VEC of TREE_VECs. Instead, we return a single
4842 TREE_VEC containing the arguments. */
4843 if (length > 1)
4844 args = make_tree_vec (length);
4846 for (header = parms; header; header = TREE_CHAIN (header))
4848 tree a = template_parms_level_to_args (TREE_VALUE (header));
4850 if (length > 1)
4851 TREE_VEC_ELT (args, --l) = a;
4852 else
4853 args = a;
4856 return args;
4859 /* Within the declaration of a template, return the currently active
4860 template parameters as an argument TREE_VEC. */
4862 static tree
4863 current_template_args (void)
4865 return template_parms_to_args (current_template_parms);
4868 /* Return the fully generic arguments for of TMPL, i.e. what
4869 current_template_args would be while parsing it. */
4871 tree
4872 generic_targs_for (tree tmpl)
4874 if (tmpl == NULL_TREE)
4875 return NULL_TREE;
4876 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4877 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4878 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4879 template parameter, it has no TEMPLATE_INFO; for a partial
4880 specialization, it has the arguments for the primary template, and we
4881 want the arguments for the partial specialization. */;
4882 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4883 if (tree ti = get_template_info (result))
4884 return TI_ARGS (ti);
4885 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4888 /* Update the declared TYPE by doing any lookups which were thought to be
4889 dependent, but are not now that we know the SCOPE of the declarator. */
4891 tree
4892 maybe_update_decl_type (tree orig_type, tree scope)
4894 tree type = orig_type;
4896 if (type == NULL_TREE)
4897 return type;
4899 if (TREE_CODE (orig_type) == TYPE_DECL)
4900 type = TREE_TYPE (type);
4902 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4903 && dependent_type_p (type)
4904 /* Don't bother building up the args in this case. */
4905 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4907 /* tsubst in the args corresponding to the template parameters,
4908 including auto if present. Most things will be unchanged, but
4909 make_typename_type and tsubst_qualified_id will resolve
4910 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4911 tree args = current_template_args ();
4912 tree auto_node = type_uses_auto (type);
4913 tree pushed;
4914 if (auto_node)
4916 tree auto_vec = make_tree_vec (1);
4917 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4918 args = add_to_template_args (args, auto_vec);
4920 pushed = push_scope (scope);
4921 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4922 if (pushed)
4923 pop_scope (scope);
4926 if (type == error_mark_node)
4927 return orig_type;
4929 if (TREE_CODE (orig_type) == TYPE_DECL)
4931 if (same_type_p (type, TREE_TYPE (orig_type)))
4932 type = orig_type;
4933 else
4934 type = TYPE_NAME (type);
4936 return type;
4939 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4940 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4941 the new template is a member template. */
4943 static tree
4944 build_template_decl (tree decl, tree parms, bool member_template_p)
4946 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4947 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4948 DECL_TEMPLATE_PARMS (tmpl) = parms;
4949 DECL_TEMPLATE_RESULT (tmpl) = decl;
4950 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4951 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4952 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4953 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4955 /* Propagate module information from the decl. */
4956 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
4958 return tmpl;
4961 struct template_parm_data
4963 /* The level of the template parameters we are currently
4964 processing. */
4965 int level;
4967 /* The index of the specialization argument we are currently
4968 processing. */
4969 int current_arg;
4971 /* An array whose size is the number of template parameters. The
4972 elements are nonzero if the parameter has been used in any one
4973 of the arguments processed so far. */
4974 int* parms;
4976 /* An array whose size is the number of template arguments. The
4977 elements are nonzero if the argument makes use of template
4978 parameters of this level. */
4979 int* arg_uses_template_parms;
4982 /* Subroutine of push_template_decl used to see if each template
4983 parameter in a partial specialization is used in the explicit
4984 argument list. If T is of the LEVEL given in DATA (which is
4985 treated as a template_parm_data*), then DATA->PARMS is marked
4986 appropriately. */
4988 static int
4989 mark_template_parm (tree t, void* data)
4991 int level;
4992 int idx;
4993 struct template_parm_data* tpd = (struct template_parm_data*) data;
4995 template_parm_level_and_index (t, &level, &idx);
4997 if (level == tpd->level)
4999 tpd->parms[idx] = 1;
5000 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5003 /* In C++17 the type of a non-type argument is a deduced context. */
5004 if (cxx_dialect >= cxx17
5005 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5006 for_each_template_parm (TREE_TYPE (t),
5007 &mark_template_parm,
5008 data,
5009 NULL,
5010 /*include_nondeduced_p=*/false);
5012 /* Return zero so that for_each_template_parm will continue the
5013 traversal of the tree; we want to mark *every* template parm. */
5014 return 0;
5017 /* Process the partial specialization DECL. */
5019 static tree
5020 process_partial_specialization (tree decl)
5022 tree type = TREE_TYPE (decl);
5023 tree tinfo = get_template_info (decl);
5024 tree maintmpl = TI_TEMPLATE (tinfo);
5025 tree specargs = TI_ARGS (tinfo);
5026 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5027 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5028 tree inner_parms;
5029 tree inst;
5030 int nargs = TREE_VEC_LENGTH (inner_args);
5031 int ntparms;
5032 int i;
5033 bool did_error_intro = false;
5034 struct template_parm_data tpd;
5035 struct template_parm_data tpd2;
5037 gcc_assert (current_template_parms);
5039 /* A concept cannot be specialized. */
5040 if (flag_concepts && variable_concept_p (maintmpl))
5042 error ("specialization of variable concept %q#D", maintmpl);
5043 return error_mark_node;
5046 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5047 ntparms = TREE_VEC_LENGTH (inner_parms);
5049 /* We check that each of the template parameters given in the
5050 partial specialization is used in the argument list to the
5051 specialization. For example:
5053 template <class T> struct S;
5054 template <class T> struct S<T*>;
5056 The second declaration is OK because `T*' uses the template
5057 parameter T, whereas
5059 template <class T> struct S<int>;
5061 is no good. Even trickier is:
5063 template <class T>
5064 struct S1
5066 template <class U>
5067 struct S2;
5068 template <class U>
5069 struct S2<T>;
5072 The S2<T> declaration is actually invalid; it is a
5073 full-specialization. Of course,
5075 template <class U>
5076 struct S2<T (*)(U)>;
5078 or some such would have been OK. */
5079 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5080 tpd.parms = XALLOCAVEC (int, ntparms);
5081 memset (tpd.parms, 0, sizeof (int) * ntparms);
5083 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5084 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5085 for (i = 0; i < nargs; ++i)
5087 tpd.current_arg = i;
5088 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5089 &mark_template_parm,
5090 &tpd,
5091 NULL,
5092 /*include_nondeduced_p=*/false);
5094 for (i = 0; i < ntparms; ++i)
5095 if (tpd.parms[i] == 0)
5097 /* One of the template parms was not used in a deduced context in the
5098 specialization. */
5099 if (!did_error_intro)
5101 error ("template parameters not deducible in "
5102 "partial specialization:");
5103 did_error_intro = true;
5106 inform (input_location, " %qD",
5107 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5110 if (did_error_intro)
5111 return error_mark_node;
5113 /* [temp.class.spec]
5115 The argument list of the specialization shall not be identical to
5116 the implicit argument list of the primary template. */
5117 tree main_args
5118 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5119 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5120 && (!flag_concepts
5121 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5123 if (!flag_concepts)
5124 error ("partial specialization %q+D does not specialize "
5125 "any template arguments; to define the primary template, "
5126 "remove the template argument list", decl);
5127 else
5128 error ("partial specialization %q+D does not specialize any "
5129 "template arguments and is not more constrained than "
5130 "the primary template; to define the primary template, "
5131 "remove the template argument list", decl);
5132 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5135 /* A partial specialization that replaces multiple parameters of the
5136 primary template with a pack expansion is less specialized for those
5137 parameters. */
5138 if (nargs < DECL_NTPARMS (maintmpl))
5140 error ("partial specialization is not more specialized than the "
5141 "primary template because it replaces multiple parameters "
5142 "with a pack expansion");
5143 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5144 /* Avoid crash in process_partial_specialization. */
5145 return decl;
5148 else if (nargs > DECL_NTPARMS (maintmpl))
5150 error ("too many arguments for partial specialization %qT", type);
5151 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5152 /* Avoid crash below. */
5153 return decl;
5156 /* If we aren't in a dependent class, we can actually try deduction. */
5157 else if (tpd.level == 1
5158 /* FIXME we should be able to handle a partial specialization of a
5159 partial instantiation, but currently we can't (c++/41727). */
5160 && TMPL_ARGS_DEPTH (specargs) == 1
5161 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5163 auto_diagnostic_group d;
5164 if (permerror (input_location, "partial specialization %qD is not "
5165 "more specialized than", decl))
5166 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5167 maintmpl);
5170 /* [temp.spec.partial]
5172 The type of a template parameter corresponding to a specialized
5173 non-type argument shall not be dependent on a parameter of the
5174 specialization.
5176 Also, we verify that pack expansions only occur at the
5177 end of the argument list. */
5178 tpd2.parms = 0;
5179 for (i = 0; i < nargs; ++i)
5181 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5182 tree arg = TREE_VEC_ELT (inner_args, i);
5183 tree packed_args = NULL_TREE;
5184 int j, len = 1;
5186 if (ARGUMENT_PACK_P (arg))
5188 /* Extract the arguments from the argument pack. We'll be
5189 iterating over these in the following loop. */
5190 packed_args = ARGUMENT_PACK_ARGS (arg);
5191 len = TREE_VEC_LENGTH (packed_args);
5194 for (j = 0; j < len; j++)
5196 if (packed_args)
5197 /* Get the Jth argument in the parameter pack. */
5198 arg = TREE_VEC_ELT (packed_args, j);
5200 if (PACK_EXPANSION_P (arg))
5202 /* Pack expansions must come at the end of the
5203 argument list. */
5204 if ((packed_args && j < len - 1)
5205 || (!packed_args && i < nargs - 1))
5207 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5208 error ("parameter pack argument %qE must be at the "
5209 "end of the template argument list", arg);
5210 else
5211 error ("parameter pack argument %qT must be at the "
5212 "end of the template argument list", arg);
5216 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5217 /* We only care about the pattern. */
5218 arg = PACK_EXPANSION_PATTERN (arg);
5220 if (/* These first two lines are the `non-type' bit. */
5221 !TYPE_P (arg)
5222 && TREE_CODE (arg) != TEMPLATE_DECL
5223 /* This next two lines are the `argument expression is not just a
5224 simple identifier' condition and also the `specialized
5225 non-type argument' bit. */
5226 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5227 && !((REFERENCE_REF_P (arg)
5228 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5229 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5231 /* Look at the corresponding template parameter,
5232 marking which template parameters its type depends
5233 upon. */
5234 tree type = TREE_TYPE (parm);
5236 if (!tpd2.parms)
5238 /* We haven't yet initialized TPD2. Do so now. */
5239 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5240 /* The number of parameters here is the number in the
5241 main template, which, as checked in the assertion
5242 above, is NARGS. */
5243 tpd2.parms = XALLOCAVEC (int, nargs);
5244 tpd2.level =
5245 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5248 /* Mark the template parameters. But this time, we're
5249 looking for the template parameters of the main
5250 template, not in the specialization. */
5251 tpd2.current_arg = i;
5252 tpd2.arg_uses_template_parms[i] = 0;
5253 memset (tpd2.parms, 0, sizeof (int) * nargs);
5254 for_each_template_parm (type,
5255 &mark_template_parm,
5256 &tpd2,
5257 NULL,
5258 /*include_nondeduced_p=*/false);
5260 if (tpd2.arg_uses_template_parms [i])
5262 /* The type depended on some template parameters.
5263 If they are fully specialized in the
5264 specialization, that's OK. */
5265 int j;
5266 int count = 0;
5267 for (j = 0; j < nargs; ++j)
5268 if (tpd2.parms[j] != 0
5269 && tpd.arg_uses_template_parms [j])
5270 ++count;
5271 if (count != 0)
5272 error_n (input_location, count,
5273 "type %qT of template argument %qE depends "
5274 "on a template parameter",
5275 "type %qT of template argument %qE depends "
5276 "on template parameters",
5277 type,
5278 arg);
5284 /* We should only get here once. */
5285 if (TREE_CODE (decl) == TYPE_DECL)
5286 gcc_assert (!COMPLETE_TYPE_P (type));
5288 // Build the template decl.
5289 tree tmpl = build_template_decl (decl, current_template_parms,
5290 DECL_MEMBER_TEMPLATE_P (maintmpl));
5291 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5292 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5293 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5295 /* Give template template parms a DECL_CONTEXT of the template
5296 for which they are a parameter. */
5297 for (i = 0; i < ntparms; ++i)
5299 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5300 if (TREE_CODE (parm) == TEMPLATE_DECL)
5301 DECL_CONTEXT (parm) = tmpl;
5304 if (VAR_P (decl))
5305 /* We didn't register this in check_explicit_specialization so we could
5306 wait until the constraints were set. */
5307 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5308 else
5309 associate_classtype_constraints (type);
5311 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5312 = tree_cons (specargs, tmpl,
5313 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5314 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5316 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5317 inst = TREE_CHAIN (inst))
5319 tree instance = TREE_VALUE (inst);
5320 if (TYPE_P (instance)
5321 ? (COMPLETE_TYPE_P (instance)
5322 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5323 : DECL_TEMPLATE_INSTANTIATION (instance))
5325 tree spec = most_specialized_partial_spec (instance, tf_none);
5326 tree inst_decl = (DECL_P (instance)
5327 ? instance : TYPE_NAME (instance));
5328 if (!spec)
5329 /* OK */;
5330 else if (spec == error_mark_node)
5331 permerror (input_location,
5332 "declaration of %qD ambiguates earlier template "
5333 "instantiation for %qD", decl, inst_decl);
5334 else if (TREE_VALUE (spec) == tmpl)
5335 permerror (input_location,
5336 "partial specialization of %qD after instantiation "
5337 "of %qD", decl, inst_decl);
5341 return decl;
5344 /* PARM is a template parameter of some form; return the corresponding
5345 TEMPLATE_PARM_INDEX. */
5347 static tree
5348 get_template_parm_index (tree parm)
5350 if (TREE_CODE (parm) == PARM_DECL
5351 || TREE_CODE (parm) == CONST_DECL)
5352 parm = DECL_INITIAL (parm);
5353 else if (TREE_CODE (parm) == TYPE_DECL
5354 || TREE_CODE (parm) == TEMPLATE_DECL)
5355 parm = TREE_TYPE (parm);
5356 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5357 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5358 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5359 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5360 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5361 return parm;
5364 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5365 parameter packs used by the template parameter PARM. */
5367 static void
5368 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5370 /* A type parm can't refer to another parm. */
5371 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5372 return;
5373 else if (TREE_CODE (parm) == PARM_DECL)
5375 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5376 ppd, ppd->visited);
5377 return;
5380 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5382 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5383 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5385 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5386 if (template_parameter_pack_p (p))
5387 /* Any packs in the type are expanded by this parameter. */;
5388 else
5389 fixed_parameter_pack_p_1 (p, ppd);
5393 /* PARM is a template parameter pack. Return any parameter packs used in
5394 its type or the type of any of its template parameters. If there are
5395 any such packs, it will be instantiated into a fixed template parameter
5396 list by partial instantiation rather than be fully deduced. */
5398 tree
5399 fixed_parameter_pack_p (tree parm)
5401 /* This can only be true in a member template. */
5402 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5403 return NULL_TREE;
5404 /* This can only be true for a parameter pack. */
5405 if (!template_parameter_pack_p (parm))
5406 return NULL_TREE;
5407 /* A type parm can't refer to another parm. */
5408 if (TREE_CODE (parm) == TYPE_DECL)
5409 return NULL_TREE;
5411 tree parameter_packs = NULL_TREE;
5412 struct find_parameter_pack_data ppd;
5413 ppd.parameter_packs = &parameter_packs;
5414 ppd.visited = new hash_set<tree>;
5415 ppd.type_pack_expansion_p = false;
5417 fixed_parameter_pack_p_1 (parm, &ppd);
5419 delete ppd.visited;
5420 return parameter_packs;
5423 /* Check that a template declaration's use of default arguments and
5424 parameter packs is not invalid. Here, PARMS are the template
5425 parameters. IS_PRIMARY is true if DECL is the thing declared by
5426 a primary template. IS_PARTIAL is true if DECL is a partial
5427 specialization.
5429 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5430 function template declaration or a friend class template
5431 declaration. In the function case, 1 indicates a declaration, 2
5432 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5433 emitted for extraneous default arguments.
5435 Returns TRUE if there were no errors found, FALSE otherwise. */
5437 bool
5438 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5439 bool is_partial, int is_friend_decl)
5441 const char *msg;
5442 int last_level_to_check;
5443 tree parm_level;
5444 bool no_errors = true;
5446 /* [temp.param]
5448 A default template-argument shall not be specified in a
5449 function template declaration or a function template definition, nor
5450 in the template-parameter-list of the definition of a member of a
5451 class template. */
5453 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5454 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5455 /* You can't have a function template declaration in a local
5456 scope, nor you can you define a member of a class template in a
5457 local scope. */
5458 return true;
5460 if ((TREE_CODE (decl) == TYPE_DECL
5461 && TREE_TYPE (decl)
5462 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5463 || (TREE_CODE (decl) == FUNCTION_DECL
5464 && LAMBDA_FUNCTION_P (decl)))
5465 /* A lambda doesn't have an explicit declaration; don't complain
5466 about the parms of the enclosing class. */
5467 return true;
5469 if (current_class_type
5470 && !TYPE_BEING_DEFINED (current_class_type)
5471 && DECL_LANG_SPECIFIC (decl)
5472 && DECL_DECLARES_FUNCTION_P (decl)
5473 /* If this is either a friend defined in the scope of the class
5474 or a member function. */
5475 && (DECL_FUNCTION_MEMBER_P (decl)
5476 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5477 : DECL_FRIEND_CONTEXT (decl)
5478 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5479 : false)
5480 /* And, if it was a member function, it really was defined in
5481 the scope of the class. */
5482 && (!DECL_FUNCTION_MEMBER_P (decl)
5483 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5484 /* We already checked these parameters when the template was
5485 declared, so there's no need to do it again now. This function
5486 was defined in class scope, but we're processing its body now
5487 that the class is complete. */
5488 return true;
5490 /* Core issue 226 (C++0x only): the following only applies to class
5491 templates. */
5492 if (is_primary
5493 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5495 /* [temp.param]
5497 If a template-parameter has a default template-argument, all
5498 subsequent template-parameters shall have a default
5499 template-argument supplied. */
5500 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5502 tree inner_parms = TREE_VALUE (parm_level);
5503 int ntparms = TREE_VEC_LENGTH (inner_parms);
5504 int seen_def_arg_p = 0;
5505 int i;
5507 for (i = 0; i < ntparms; ++i)
5509 tree parm = TREE_VEC_ELT (inner_parms, i);
5511 if (parm == error_mark_node)
5512 continue;
5514 if (TREE_PURPOSE (parm))
5515 seen_def_arg_p = 1;
5516 else if (seen_def_arg_p
5517 && !template_parameter_pack_p (TREE_VALUE (parm)))
5519 error ("no default argument for %qD", TREE_VALUE (parm));
5520 /* For better subsequent error-recovery, we indicate that
5521 there should have been a default argument. */
5522 TREE_PURPOSE (parm) = error_mark_node;
5523 no_errors = false;
5525 else if (!is_partial
5526 && !is_friend_decl
5527 /* Don't complain about an enclosing partial
5528 specialization. */
5529 && parm_level == parms
5530 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5531 && i < ntparms - 1
5532 && template_parameter_pack_p (TREE_VALUE (parm))
5533 /* A fixed parameter pack will be partially
5534 instantiated into a fixed length list. */
5535 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5537 /* A primary class template, primary variable template
5538 (DR 2032), or alias template can only have one
5539 parameter pack, at the end of the template
5540 parameter list. */
5542 error ("parameter pack %q+D must be at the end of the"
5543 " template parameter list", TREE_VALUE (parm));
5545 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5546 = error_mark_node;
5547 no_errors = false;
5553 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5554 || is_partial
5555 || !is_primary
5556 || is_friend_decl)
5557 /* For an ordinary class template, default template arguments are
5558 allowed at the innermost level, e.g.:
5559 template <class T = int>
5560 struct S {};
5561 but, in a partial specialization, they're not allowed even
5562 there, as we have in [temp.class.spec]:
5564 The template parameter list of a specialization shall not
5565 contain default template argument values.
5567 So, for a partial specialization, or for a function template
5568 (in C++98/C++03), we look at all of them. */
5570 else
5571 /* But, for a primary class template that is not a partial
5572 specialization we look at all template parameters except the
5573 innermost ones. */
5574 parms = TREE_CHAIN (parms);
5576 /* Figure out what error message to issue. */
5577 if (is_friend_decl == 2)
5578 msg = G_("default template arguments may not be used in function template "
5579 "friend re-declaration");
5580 else if (is_friend_decl)
5581 msg = G_("default template arguments may not be used in template "
5582 "friend declarations");
5583 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5584 msg = G_("default template arguments may not be used in function templates "
5585 "without %<-std=c++11%> or %<-std=gnu++11%>");
5586 else if (is_partial)
5587 msg = G_("default template arguments may not be used in "
5588 "partial specializations");
5589 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5590 msg = G_("default argument for template parameter for class enclosing %qD");
5591 else
5592 /* Per [temp.param]/9, "A default template-argument shall not be
5593 specified in the template-parameter-lists of the definition of
5594 a member of a class template that appears outside of the member's
5595 class.", thus if we aren't handling a member of a class template
5596 there is no need to examine the parameters. */
5597 return true;
5599 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5600 /* If we're inside a class definition, there's no need to
5601 examine the parameters to the class itself. On the one
5602 hand, they will be checked when the class is defined, and,
5603 on the other, default arguments are valid in things like:
5604 template <class T = double>
5605 struct S { template <class U> void f(U); };
5606 Here the default argument for `S' has no bearing on the
5607 declaration of `f'. */
5608 last_level_to_check = template_class_depth (current_class_type) + 1;
5609 else
5610 /* Check everything. */
5611 last_level_to_check = 0;
5613 for (parm_level = parms;
5614 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5615 parm_level = TREE_CHAIN (parm_level))
5617 tree inner_parms = TREE_VALUE (parm_level);
5618 int i;
5619 int ntparms;
5621 ntparms = TREE_VEC_LENGTH (inner_parms);
5622 for (i = 0; i < ntparms; ++i)
5624 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5625 continue;
5627 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5629 if (msg)
5631 no_errors = false;
5632 if (is_friend_decl == 2)
5633 return no_errors;
5635 error (msg, decl);
5636 msg = 0;
5639 /* Clear out the default argument so that we are not
5640 confused later. */
5641 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5645 /* At this point, if we're still interested in issuing messages,
5646 they must apply to classes surrounding the object declared. */
5647 if (msg)
5648 msg = G_("default argument for template parameter for class "
5649 "enclosing %qD");
5652 return no_errors;
5655 /* Worker for push_template_decl_real, called via
5656 for_each_template_parm. DATA is really an int, indicating the
5657 level of the parameters we are interested in. If T is a template
5658 parameter of that level, return nonzero. */
5660 static int
5661 template_parm_this_level_p (tree t, void* data)
5663 int this_level = *(int *)data;
5664 int level;
5666 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5667 level = TEMPLATE_PARM_LEVEL (t);
5668 else
5669 level = TEMPLATE_TYPE_LEVEL (t);
5670 return level == this_level;
5673 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5674 DATA is really an int, indicating the innermost outer level of parameters.
5675 If T is a template parameter of that level or further out, return
5676 nonzero. */
5678 static int
5679 template_parm_outer_level (tree t, void *data)
5681 int this_level = *(int *)data;
5682 int level;
5684 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5685 level = TEMPLATE_PARM_LEVEL (t);
5686 else
5687 level = TEMPLATE_TYPE_LEVEL (t);
5688 return level <= this_level;
5691 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5692 parameters given by current_template_args, or reuses a
5693 previously existing one, if appropriate. Returns the DECL, or an
5694 equivalent one, if it is replaced via a call to duplicate_decls.
5696 If IS_FRIEND is true, DECL is a friend declaration. */
5698 tree
5699 push_template_decl (tree decl, bool is_friend)
5701 if (decl == error_mark_node || !current_template_parms)
5702 return error_mark_node;
5704 /* See if this is a partial specialization. */
5705 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5706 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5707 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5708 || (VAR_P (decl)
5709 && DECL_LANG_SPECIFIC (decl)
5710 && DECL_TEMPLATE_SPECIALIZATION (decl)
5711 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5713 /* No surprising friend functions. */
5714 gcc_checking_assert (is_friend
5715 || !(TREE_CODE (decl) == FUNCTION_DECL
5716 && DECL_UNIQUE_FRIEND_P (decl)));
5718 tree ctx;
5719 if (is_friend)
5720 /* For a friend, we want the context of the friend, not
5721 the type of which it is a friend. */
5722 ctx = CP_DECL_CONTEXT (decl);
5723 else if (CP_DECL_CONTEXT (decl)
5724 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5725 /* In the case of a virtual function, we want the class in which
5726 it is defined. */
5727 ctx = CP_DECL_CONTEXT (decl);
5728 else
5729 /* Otherwise, if we're currently defining some class, the DECL
5730 is assumed to be a member of the class. */
5731 ctx = current_scope ();
5733 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5734 ctx = NULL_TREE;
5736 if (!DECL_CONTEXT (decl))
5737 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5739 /* See if this is a primary template. */
5740 bool is_primary = false;
5741 if (is_friend && ctx
5742 && uses_template_parms_level (ctx, processing_template_decl))
5743 /* A friend template that specifies a class context, i.e.
5744 template <typename T> friend void A<T>::f();
5745 is not primary. */
5747 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5748 /* Lambdas are not primary. */
5750 else
5751 is_primary = template_parm_scope_p ();
5753 /* True if the template is a member template, in the sense of
5754 [temp.mem]. */
5755 bool member_template_p = false;
5757 if (is_primary)
5759 warning (OPT_Wtemplates, "template %qD declared", decl);
5761 if (DECL_CLASS_SCOPE_P (decl))
5762 member_template_p = true;
5764 if (TREE_CODE (decl) == TYPE_DECL
5765 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5767 error ("template class without a name");
5768 return error_mark_node;
5770 else if (TREE_CODE (decl) == FUNCTION_DECL)
5772 if (member_template_p)
5774 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5775 error ("member template %qD may not have virt-specifiers", decl);
5777 if (DECL_DESTRUCTOR_P (decl))
5779 /* [temp.mem]
5781 A destructor shall not be a member template. */
5782 error_at (DECL_SOURCE_LOCATION (decl),
5783 "destructor %qD declared as member template", decl);
5784 return error_mark_node;
5786 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5787 && (!prototype_p (TREE_TYPE (decl))
5788 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5789 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5790 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5791 == void_list_node)))
5793 /* [basic.stc.dynamic.allocation]
5795 An allocation function can be a function
5796 template. ... Template allocation functions shall
5797 have two or more parameters. */
5798 error ("invalid template declaration of %qD", decl);
5799 return error_mark_node;
5802 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5803 && CLASS_TYPE_P (TREE_TYPE (decl)))
5804 /* Class template. */;
5805 else if (TREE_CODE (decl) == TYPE_DECL
5806 && TYPE_DECL_ALIAS_P (decl))
5807 /* alias-declaration */
5808 gcc_assert (!DECL_ARTIFICIAL (decl));
5809 else if (VAR_P (decl))
5810 /* C++14 variable template. */;
5811 else if (TREE_CODE (decl) == CONCEPT_DECL)
5812 /* C++20 concept definitions. */;
5813 else
5815 error ("template declaration of %q#D", decl);
5816 return error_mark_node;
5820 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5821 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5822 || (VAR_OR_FUNCTION_DECL_P (decl)
5823 && DECL_LOCAL_DECL_P (decl))));
5825 /* Check to see that the rules regarding the use of default
5826 arguments are not being violated. We check args for a friend
5827 functions when we know whether it's a definition, introducing
5828 declaration or re-declaration. */
5829 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5830 check_default_tmpl_args (decl, current_template_parms,
5831 is_primary, is_partial, is_friend);
5833 /* Ensure that there are no parameter packs in the type of this
5834 declaration that have not been expanded. */
5835 if (TREE_CODE (decl) == FUNCTION_DECL)
5837 /* Check each of the arguments individually to see if there are
5838 any bare parameter packs. */
5839 tree type = TREE_TYPE (decl);
5840 tree arg = DECL_ARGUMENTS (decl);
5841 tree argtype = TYPE_ARG_TYPES (type);
5843 while (arg && argtype)
5845 if (!DECL_PACK_P (arg)
5846 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5848 /* This is a PARM_DECL that contains unexpanded parameter
5849 packs. We have already complained about this in the
5850 check_for_bare_parameter_packs call, so just replace
5851 these types with ERROR_MARK_NODE. */
5852 TREE_TYPE (arg) = error_mark_node;
5853 TREE_VALUE (argtype) = error_mark_node;
5856 arg = DECL_CHAIN (arg);
5857 argtype = TREE_CHAIN (argtype);
5860 /* Check for bare parameter packs in the return type and the
5861 exception specifiers. */
5862 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5863 /* Errors were already issued, set return type to int
5864 as the frontend doesn't expect error_mark_node as
5865 the return type. */
5866 TREE_TYPE (type) = integer_type_node;
5867 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5868 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5870 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5871 ? DECL_ORIGINAL_TYPE (decl)
5872 : TREE_TYPE (decl)))
5874 TREE_TYPE (decl) = error_mark_node;
5875 return error_mark_node;
5878 if (is_partial)
5879 return process_partial_specialization (decl);
5881 tree args = current_template_args ();
5882 tree tmpl = NULL_TREE;
5883 bool new_template_p = false;
5884 if (local_p)
5886 /* Does not get a template head. */
5887 tmpl = NULL_TREE;
5888 gcc_checking_assert (!is_primary);
5890 else if (!ctx
5891 || TREE_CODE (ctx) == FUNCTION_DECL
5892 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5893 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5894 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5895 && DECL_TEMPLATE_INFO (decl))))
5897 if (DECL_LANG_SPECIFIC (decl)
5898 && DECL_TEMPLATE_INFO (decl)
5899 && DECL_TI_TEMPLATE (decl))
5900 tmpl = DECL_TI_TEMPLATE (decl);
5901 /* If DECL is a TYPE_DECL for a class-template, then there won't
5902 be DECL_LANG_SPECIFIC. The information equivalent to
5903 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5904 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5905 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5906 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5908 /* Since a template declaration already existed for this
5909 class-type, we must be redeclaring it here. Make sure
5910 that the redeclaration is valid. */
5911 redeclare_class_template (TREE_TYPE (decl),
5912 current_template_parms,
5913 current_template_constraints ());
5914 /* We don't need to create a new TEMPLATE_DECL; just use the
5915 one we already had. */
5916 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5918 else
5920 tmpl = build_template_decl (decl, current_template_parms,
5921 member_template_p);
5922 new_template_p = true;
5924 if (DECL_LANG_SPECIFIC (decl)
5925 && DECL_TEMPLATE_SPECIALIZATION (decl))
5927 /* A specialization of a member template of a template
5928 class. */
5929 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5930 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5931 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5935 else
5937 tree a, t, current, parms;
5938 int i;
5939 tree tinfo = get_template_info (decl);
5941 if (!tinfo)
5943 error ("template definition of non-template %q#D", decl);
5944 return error_mark_node;
5947 tmpl = TI_TEMPLATE (tinfo);
5949 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5950 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5951 && DECL_TEMPLATE_SPECIALIZATION (decl)
5952 && DECL_MEMBER_TEMPLATE_P (tmpl))
5954 /* The declaration is a specialization of a member
5955 template, declared outside the class. Therefore, the
5956 innermost template arguments will be NULL, so we
5957 replace them with the arguments determined by the
5958 earlier call to check_explicit_specialization. */
5959 args = DECL_TI_ARGS (decl);
5961 tree new_tmpl
5962 = build_template_decl (decl, current_template_parms,
5963 member_template_p);
5964 DECL_TI_TEMPLATE (decl) = new_tmpl;
5965 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5966 DECL_TEMPLATE_INFO (new_tmpl)
5967 = build_template_info (tmpl, args);
5969 register_specialization (new_tmpl,
5970 most_general_template (tmpl),
5971 args,
5972 is_friend, 0);
5973 return decl;
5976 /* Make sure the template headers we got make sense. */
5978 parms = DECL_TEMPLATE_PARMS (tmpl);
5979 i = TMPL_PARMS_DEPTH (parms);
5980 if (TMPL_ARGS_DEPTH (args) != i)
5982 error ("expected %d levels of template parms for %q#D, got %d",
5983 i, decl, TMPL_ARGS_DEPTH (args));
5984 DECL_INTERFACE_KNOWN (decl) = 1;
5985 return error_mark_node;
5987 else
5988 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5990 a = TMPL_ARGS_LEVEL (args, i);
5991 t = INNERMOST_TEMPLATE_PARMS (parms);
5993 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5995 if (current == decl)
5996 error ("got %d template parameters for %q#D",
5997 TREE_VEC_LENGTH (a), decl);
5998 else
5999 error ("got %d template parameters for %q#T",
6000 TREE_VEC_LENGTH (a), current);
6001 error (" but %d required", TREE_VEC_LENGTH (t));
6002 /* Avoid crash in import_export_decl. */
6003 DECL_INTERFACE_KNOWN (decl) = 1;
6004 return error_mark_node;
6007 if (current == decl)
6008 current = ctx;
6009 else if (current == NULL_TREE)
6010 /* Can happen in erroneous input. */
6011 break;
6012 else
6013 current = get_containing_scope (current);
6016 /* Check that the parms are used in the appropriate qualifying scopes
6017 in the declarator. */
6018 if (!comp_template_args
6019 (TI_ARGS (tinfo),
6020 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6022 error ("template arguments to %qD do not match original "
6023 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6024 if (!uses_template_parms (TI_ARGS (tinfo)))
6025 inform (input_location, "use %<template<>%> for"
6026 " an explicit specialization");
6027 /* Avoid crash in import_export_decl. */
6028 DECL_INTERFACE_KNOWN (decl) = 1;
6029 return error_mark_node;
6033 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6035 if (new_template_p)
6037 /* Push template declarations for global functions and types.
6038 Note that we do not try to push a global template friend
6039 declared in a template class; such a thing may well depend on
6040 the template parameters of the class and we'll push it when
6041 instantiating the befriending class. */
6042 if (!ctx
6043 && !(is_friend && template_class_depth (current_class_type) > 0))
6045 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6046 if (pushed == error_mark_node)
6047 return error_mark_node;
6049 /* pushdecl may have found an existing template. */
6050 if (pushed != tmpl)
6052 decl = DECL_TEMPLATE_RESULT (pushed);
6053 tmpl = NULL_TREE;
6056 else if (is_friend)
6058 /* Record this decl as belonging to the current class. It's
6059 not chained onto anything else. */
6060 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6061 gcc_checking_assert (!DECL_CHAIN (tmpl));
6062 DECL_CHAIN (tmpl) = current_scope ();
6065 else if (tmpl)
6066 /* The type may have been completed, or (erroneously) changed. */
6067 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6069 if (tmpl)
6071 if (is_primary)
6073 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6075 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6077 /* Give template template parms a DECL_CONTEXT of the template
6078 for which they are a parameter. */
6079 parms = INNERMOST_TEMPLATE_PARMS (parms);
6080 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6082 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6083 if (TREE_CODE (parm) == TEMPLATE_DECL)
6084 DECL_CONTEXT (parm) = tmpl;
6087 if (TREE_CODE (decl) == TYPE_DECL
6088 && TYPE_DECL_ALIAS_P (decl))
6090 if (tree constr
6091 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6093 /* ??? Why don't we do this here for all templates? */
6094 constr = build_constraints (constr, NULL_TREE);
6095 set_constraints (decl, constr);
6097 if (complex_alias_template_p (tmpl))
6098 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6102 /* The DECL_TI_ARGS of DECL contains full set of arguments
6103 referring wback to its most general template. If TMPL is a
6104 specialization, ARGS may only have the innermost set of
6105 arguments. Add the missing argument levels if necessary. */
6106 if (DECL_TEMPLATE_INFO (tmpl))
6107 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6109 tree info = build_template_info (tmpl, args);
6111 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6112 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6113 else
6115 retrofit_lang_decl (decl);
6116 DECL_TEMPLATE_INFO (decl) = info;
6120 if (flag_implicit_templates
6121 && !is_friend
6122 && TREE_PUBLIC (decl)
6123 && VAR_OR_FUNCTION_DECL_P (decl))
6124 /* Set DECL_COMDAT on template instantiations; if we force
6125 them to be emitted by explicit instantiation,
6126 mark_needed will tell cgraph to do the right thing. */
6127 DECL_COMDAT (decl) = true;
6129 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6131 return decl;
6134 /* FN is an inheriting constructor that inherits from the constructor
6135 template INHERITED; turn FN into a constructor template with a matching
6136 template header. */
6138 tree
6139 add_inherited_template_parms (tree fn, tree inherited)
6141 tree inner_parms
6142 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6143 inner_parms = copy_node (inner_parms);
6144 tree parms
6145 = tree_cons (size_int (processing_template_decl + 1),
6146 inner_parms, current_template_parms);
6147 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6148 tree args = template_parms_to_args (parms);
6149 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6150 DECL_ARTIFICIAL (tmpl) = true;
6151 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6152 return tmpl;
6155 /* Called when a class template TYPE is redeclared with the indicated
6156 template PARMS, e.g.:
6158 template <class T> struct S;
6159 template <class T> struct S {}; */
6161 bool
6162 redeclare_class_template (tree type, tree parms, tree cons)
6164 tree tmpl;
6165 tree tmpl_parms;
6166 int i;
6168 if (!TYPE_TEMPLATE_INFO (type))
6170 error ("%qT is not a template type", type);
6171 return false;
6174 tmpl = TYPE_TI_TEMPLATE (type);
6175 if (!PRIMARY_TEMPLATE_P (tmpl))
6176 /* The type is nested in some template class. Nothing to worry
6177 about here; there are no new template parameters for the nested
6178 type. */
6179 return true;
6181 if (!parms)
6183 error ("template specifiers not specified in declaration of %qD",
6184 tmpl);
6185 return false;
6188 parms = INNERMOST_TEMPLATE_PARMS (parms);
6189 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6191 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6193 error_n (input_location, TREE_VEC_LENGTH (parms),
6194 "redeclared with %d template parameter",
6195 "redeclared with %d template parameters",
6196 TREE_VEC_LENGTH (parms));
6197 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6198 "previous declaration %qD used %d template parameter",
6199 "previous declaration %qD used %d template parameters",
6200 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6201 return false;
6204 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6206 tree tmpl_parm;
6207 tree parm;
6208 tree tmpl_default;
6209 tree parm_default;
6211 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6212 || TREE_VEC_ELT (parms, i) == error_mark_node)
6213 continue;
6215 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6216 if (error_operand_p (tmpl_parm))
6217 return false;
6219 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6220 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6221 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6223 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6224 TEMPLATE_DECL. */
6225 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6226 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6227 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6228 || (TREE_CODE (tmpl_parm) != PARM_DECL
6229 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6230 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6231 || (TREE_CODE (tmpl_parm) == PARM_DECL
6232 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6233 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6235 auto_diagnostic_group d;
6236 error ("template parameter %q+#D", tmpl_parm);
6237 inform (input_location, "redeclared here as %q#D", parm);
6238 return false;
6241 /* The parameters can be declared to introduce different
6242 constraints. */
6243 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6244 tree p2 = TREE_VEC_ELT (parms, i);
6245 if (!template_parameter_constraints_equivalent_p (p1, p2))
6247 auto_diagnostic_group d;
6248 error ("declaration of template parameter %q+#D with different "
6249 "constraints", parm);
6250 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6251 "original declaration appeared here");
6252 return false;
6255 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6257 /* We have in [temp.param]:
6259 A template-parameter may not be given default arguments
6260 by two different declarations in the same scope. */
6261 auto_diagnostic_group d;
6262 error_at (input_location, "redefinition of default argument for %q#D", parm);
6263 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6264 "original definition appeared here");
6265 return false;
6268 if (parm_default != NULL_TREE)
6269 /* Update the previous template parameters (which are the ones
6270 that will really count) with the new default value. */
6271 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6272 else if (tmpl_default != NULL_TREE)
6273 /* Update the new parameters, too; they'll be used as the
6274 parameters for any members. */
6275 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6277 /* Give each template template parm in this redeclaration a
6278 DECL_CONTEXT of the template for which they are a parameter. */
6279 if (TREE_CODE (parm) == TEMPLATE_DECL)
6281 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6282 DECL_CONTEXT (parm) = tmpl;
6286 tree ci = get_constraints (tmpl);
6287 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6288 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6290 /* Two classes with different constraints declare different entities. */
6291 if (!cp_tree_equal (req1, req2))
6293 auto_diagnostic_group d;
6294 error_at (input_location, "redeclaration %q#D with different "
6295 "constraints", tmpl);
6296 inform (DECL_SOURCE_LOCATION (tmpl),
6297 "original declaration appeared here");
6298 return false;
6301 return true;
6304 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6305 to be used when the caller has already checked
6306 (processing_template_decl
6307 && !instantiation_dependent_expression_p (expr)
6308 && potential_constant_expression (expr))
6309 and cleared processing_template_decl. */
6311 tree
6312 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6314 return tsubst_copy_and_build (expr,
6315 /*args=*/NULL_TREE,
6316 complain,
6317 /*in_decl=*/NULL_TREE,
6318 /*function_p=*/false,
6319 /*integral_constant_expression_p=*/true);
6322 /* Simplify EXPR if it is a non-dependent expression. Returns the
6323 (possibly simplified) expression. */
6325 tree
6326 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6328 if (expr == NULL_TREE)
6329 return NULL_TREE;
6331 /* If we're in a template, but EXPR isn't value dependent, simplify
6332 it. We're supposed to treat:
6334 template <typename T> void f(T[1 + 1]);
6335 template <typename T> void f(T[2]);
6337 as two declarations of the same function, for example. */
6338 if (processing_template_decl
6339 && is_nondependent_constant_expression (expr))
6341 processing_template_decl_sentinel s;
6342 expr = instantiate_non_dependent_expr_internal (expr, complain);
6344 return expr;
6347 tree
6348 instantiate_non_dependent_expr (tree expr)
6350 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6353 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6354 an uninstantiated expression. */
6356 tree
6357 instantiate_non_dependent_or_null (tree expr)
6359 if (expr == NULL_TREE)
6360 return NULL_TREE;
6361 if (processing_template_decl)
6363 if (!is_nondependent_constant_expression (expr))
6364 expr = NULL_TREE;
6365 else
6367 processing_template_decl_sentinel s;
6368 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6371 return expr;
6374 /* True iff T is a specialization of a variable template. */
6376 bool
6377 variable_template_specialization_p (tree t)
6379 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6380 return false;
6381 tree tmpl = DECL_TI_TEMPLATE (t);
6382 return variable_template_p (tmpl);
6385 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6386 template declaration, or a TYPE_DECL for an alias declaration. */
6388 bool
6389 alias_type_or_template_p (tree t)
6391 if (t == NULL_TREE)
6392 return false;
6393 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6394 || (TYPE_P (t)
6395 && TYPE_NAME (t)
6396 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6397 || DECL_ALIAS_TEMPLATE_P (t));
6400 /* If T is a specialization of an alias template, return it; otherwise return
6401 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6403 tree
6404 alias_template_specialization_p (const_tree t,
6405 bool transparent_typedefs)
6407 if (!TYPE_P (t))
6408 return NULL_TREE;
6410 /* It's an alias template specialization if it's an alias and its
6411 TYPE_NAME is a specialization of a primary template. */
6412 if (typedef_variant_p (t))
6414 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6415 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6416 return CONST_CAST_TREE (t);
6417 if (transparent_typedefs)
6418 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6419 (TYPE_NAME (t)),
6420 transparent_typedefs);
6423 return NULL_TREE;
6426 /* An alias template is complex from a SFINAE perspective if a template-id
6427 using that alias can be ill-formed when the expansion is not, as with
6428 the void_t template. We determine this by checking whether the
6429 expansion for the alias template uses all its template parameters. */
6431 struct uses_all_template_parms_data
6433 int level;
6434 bool *seen;
6437 static int
6438 uses_all_template_parms_r (tree t, void *data_)
6440 struct uses_all_template_parms_data &data
6441 = *(struct uses_all_template_parms_data*)data_;
6442 tree idx = get_template_parm_index (t);
6444 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6445 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6446 return 0;
6449 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6451 static int
6452 complex_pack_expansion_r (tree t, void *data_)
6454 /* An alias template with a pack expansion that expands a pack from the
6455 enclosing class needs to be considered complex, to avoid confusion with
6456 the same pack being used as an argument to the alias's own template
6457 parameter (91966). */
6458 if (!PACK_EXPANSION_P (t))
6459 return 0;
6460 struct uses_all_template_parms_data &data
6461 = *(struct uses_all_template_parms_data*)data_;
6462 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6463 pack = TREE_CHAIN (pack))
6465 tree parm_pack = TREE_VALUE (pack);
6466 if (!TEMPLATE_PARM_P (parm_pack))
6467 continue;
6468 int idx, level;
6469 template_parm_level_and_index (parm_pack, &level, &idx);
6470 if (level < data.level)
6471 return 1;
6473 return 0;
6476 static bool
6477 complex_alias_template_p (const_tree tmpl)
6479 /* A renaming alias isn't complex. */
6480 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6481 return false;
6483 /* Any other constrained alias is complex. */
6484 if (get_constraints (tmpl))
6485 return true;
6487 struct uses_all_template_parms_data data;
6488 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6489 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6490 data.level = TMPL_PARMS_DEPTH (parms);
6491 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6492 data.seen = XALLOCAVEC (bool, len);
6493 for (int i = 0; i < len; ++i)
6494 data.seen[i] = false;
6496 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6497 NULL, true, complex_pack_expansion_r))
6498 return true;
6499 for (int i = 0; i < len; ++i)
6500 if (!data.seen[i])
6501 return true;
6502 return false;
6505 /* If T is a specialization of a complex alias template with dependent
6506 template-arguments, return it; otherwise return NULL_TREE. If T is a
6507 typedef to such a specialization, return the specialization. */
6509 tree
6510 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6512 if (t == error_mark_node)
6513 return NULL_TREE;
6514 gcc_assert (TYPE_P (t));
6516 if (!typedef_variant_p (t))
6517 return NULL_TREE;
6519 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6520 if (tinfo
6521 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6522 && (any_dependent_template_arguments_p
6523 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6524 return CONST_CAST_TREE (t);
6526 if (transparent_typedefs)
6528 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6529 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6532 return NULL_TREE;
6535 /* Return the number of innermost template parameters in TMPL. */
6537 static int
6538 num_innermost_template_parms (const_tree tmpl)
6540 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6541 return TREE_VEC_LENGTH (parms);
6544 /* Return either TMPL or another template that it is equivalent to under DR
6545 1286: An alias that just changes the name of a template is equivalent to
6546 the other template. */
6548 static tree
6549 get_underlying_template (tree tmpl)
6551 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6552 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6554 /* Determine if the alias is equivalent to an underlying template. */
6555 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6556 /* The underlying type may have been ill-formed. Don't proceed. */
6557 if (!orig_type)
6558 break;
6559 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6560 if (!tinfo)
6561 break;
6563 tree underlying = TI_TEMPLATE (tinfo);
6564 if (!PRIMARY_TEMPLATE_P (underlying)
6565 || (num_innermost_template_parms (tmpl)
6566 != num_innermost_template_parms (underlying)))
6567 break;
6569 /* Does the alias add cv-quals? */
6570 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6571 break;
6573 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6574 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6575 break;
6577 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6578 it's appropriate to treat a less-constrained alias as equivalent. */
6579 if (!at_least_as_constrained (underlying, tmpl))
6580 break;
6582 /* Alias is equivalent. Strip it and repeat. */
6583 tmpl = underlying;
6586 return tmpl;
6589 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6590 must be a reference-to-function or a pointer-to-function type, as specified
6591 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6592 and check that the resulting function has external linkage. */
6594 static tree
6595 convert_nontype_argument_function (tree type, tree expr,
6596 tsubst_flags_t complain)
6598 tree fns = expr;
6599 tree fn, fn_no_ptr;
6600 linkage_kind linkage;
6602 fn = instantiate_type (type, fns, tf_none);
6603 if (fn == error_mark_node)
6604 return error_mark_node;
6606 if (value_dependent_expression_p (fn))
6607 goto accept;
6609 fn_no_ptr = fn;
6610 if (REFERENCE_REF_P (fn_no_ptr))
6611 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6612 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6613 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6614 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6615 if (BASELINK_P (fn_no_ptr))
6616 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6618 /* [temp.arg.nontype]/1
6620 A template-argument for a non-type, non-template template-parameter
6621 shall be one of:
6622 [...]
6623 -- the address of an object or function with external [C++11: or
6624 internal] linkage. */
6626 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6627 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6629 if (complain & tf_error)
6631 location_t loc = cp_expr_loc_or_input_loc (expr);
6632 error_at (loc, "%qE is not a valid template argument for type %qT",
6633 expr, type);
6634 if (TYPE_PTR_P (type))
6635 inform (loc, "it must be the address of a function "
6636 "with external linkage");
6637 else
6638 inform (loc, "it must be the name of a function with "
6639 "external linkage");
6641 return NULL_TREE;
6644 linkage = decl_linkage (fn_no_ptr);
6645 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6647 if (complain & tf_error)
6649 location_t loc = cp_expr_loc_or_input_loc (expr);
6650 if (cxx_dialect >= cxx11)
6651 error_at (loc, "%qE is not a valid template argument for type "
6652 "%qT because %qD has no linkage",
6653 expr, type, fn_no_ptr);
6654 else
6655 error_at (loc, "%qE is not a valid template argument for type "
6656 "%qT because %qD does not have external linkage",
6657 expr, type, fn_no_ptr);
6659 return NULL_TREE;
6662 accept:
6663 if (TYPE_REF_P (type))
6665 if (REFERENCE_REF_P (fn))
6666 fn = TREE_OPERAND (fn, 0);
6667 else
6668 fn = build_address (fn);
6670 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6671 fn = build_nop (type, fn);
6673 return fn;
6676 /* Subroutine of convert_nontype_argument.
6677 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6678 Emit an error otherwise. */
6680 static bool
6681 check_valid_ptrmem_cst_expr (tree type, tree expr,
6682 tsubst_flags_t complain)
6684 tree orig_expr = expr;
6685 STRIP_NOPS (expr);
6686 if (null_ptr_cst_p (expr))
6687 return true;
6688 if (TREE_CODE (expr) == PTRMEM_CST
6689 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6690 PTRMEM_CST_CLASS (expr)))
6691 return true;
6692 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6693 return true;
6694 if (processing_template_decl
6695 && TREE_CODE (expr) == ADDR_EXPR
6696 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6697 return true;
6698 if (complain & tf_error)
6700 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6701 error_at (loc, "%qE is not a valid template argument for type %qT",
6702 orig_expr, type);
6703 if (TREE_CODE (expr) != PTRMEM_CST)
6704 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6705 else
6706 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6708 return false;
6711 /* Returns TRUE iff the address of OP is value-dependent.
6713 14.6.2.4 [temp.dep.temp]:
6714 A non-integral non-type template-argument is dependent if its type is
6715 dependent or it has either of the following forms
6716 qualified-id
6717 & qualified-id
6718 and contains a nested-name-specifier which specifies a class-name that
6719 names a dependent type.
6721 We generalize this to just say that the address of a member of a
6722 dependent class is value-dependent; the above doesn't cover the
6723 address of a static data member named with an unqualified-id. */
6725 static bool
6726 has_value_dependent_address (tree op)
6728 STRIP_ANY_LOCATION_WRAPPER (op);
6730 /* We could use get_inner_reference here, but there's no need;
6731 this is only relevant for template non-type arguments, which
6732 can only be expressed as &id-expression. */
6733 if (DECL_P (op))
6735 tree ctx = CP_DECL_CONTEXT (op);
6736 if (TYPE_P (ctx) && dependent_type_p (ctx))
6737 return true;
6740 return false;
6743 /* The next set of functions are used for providing helpful explanatory
6744 diagnostics for failed overload resolution. Their messages should be
6745 indented by two spaces for consistency with the messages in
6746 call.c */
6748 static int
6749 unify_success (bool /*explain_p*/)
6751 return 0;
6754 /* Other failure functions should call this one, to provide a single function
6755 for setting a breakpoint on. */
6757 static int
6758 unify_invalid (bool /*explain_p*/)
6760 return 1;
6763 static int
6764 unify_parameter_deduction_failure (bool explain_p, tree parm)
6766 if (explain_p)
6767 inform (input_location,
6768 " couldn%'t deduce template parameter %qD", parm);
6769 return unify_invalid (explain_p);
6772 static int
6773 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6775 if (explain_p)
6776 inform (input_location,
6777 " types %qT and %qT have incompatible cv-qualifiers",
6778 parm, arg);
6779 return unify_invalid (explain_p);
6782 static int
6783 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6785 if (explain_p)
6786 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6787 return unify_invalid (explain_p);
6790 static int
6791 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6793 if (explain_p)
6794 inform (input_location,
6795 " template parameter %qD is not a parameter pack, but "
6796 "argument %qD is",
6797 parm, arg);
6798 return unify_invalid (explain_p);
6801 static int
6802 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6804 if (explain_p)
6805 inform (input_location,
6806 " template argument %qE does not match "
6807 "pointer-to-member constant %qE",
6808 arg, parm);
6809 return unify_invalid (explain_p);
6812 static int
6813 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6815 if (explain_p)
6816 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6817 return unify_invalid (explain_p);
6820 static int
6821 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6823 if (explain_p)
6824 inform (input_location,
6825 " inconsistent parameter pack deduction with %qT and %qT",
6826 old_arg, new_arg);
6827 return unify_invalid (explain_p);
6830 static int
6831 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6833 if (explain_p)
6835 if (TYPE_P (parm))
6836 inform (input_location,
6837 " deduced conflicting types for parameter %qT (%qT and %qT)",
6838 parm, first, second);
6839 else
6840 inform (input_location,
6841 " deduced conflicting values for non-type parameter "
6842 "%qE (%qE and %qE)", parm, first, second);
6844 return unify_invalid (explain_p);
6847 static int
6848 unify_vla_arg (bool explain_p, tree arg)
6850 if (explain_p)
6851 inform (input_location,
6852 " variable-sized array type %qT is not "
6853 "a valid template argument",
6854 arg);
6855 return unify_invalid (explain_p);
6858 static int
6859 unify_method_type_error (bool explain_p, tree arg)
6861 if (explain_p)
6862 inform (input_location,
6863 " member function type %qT is not a valid template argument",
6864 arg);
6865 return unify_invalid (explain_p);
6868 static int
6869 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6871 if (explain_p)
6873 if (least_p)
6874 inform_n (input_location, wanted,
6875 " candidate expects at least %d argument, %d provided",
6876 " candidate expects at least %d arguments, %d provided",
6877 wanted, have);
6878 else
6879 inform_n (input_location, wanted,
6880 " candidate expects %d argument, %d provided",
6881 " candidate expects %d arguments, %d provided",
6882 wanted, have);
6884 return unify_invalid (explain_p);
6887 static int
6888 unify_too_many_arguments (bool explain_p, int have, int wanted)
6890 return unify_arity (explain_p, have, wanted);
6893 static int
6894 unify_too_few_arguments (bool explain_p, int have, int wanted,
6895 bool least_p = false)
6897 return unify_arity (explain_p, have, wanted, least_p);
6900 static int
6901 unify_arg_conversion (bool explain_p, tree to_type,
6902 tree from_type, tree arg)
6904 if (explain_p)
6905 inform (cp_expr_loc_or_input_loc (arg),
6906 " cannot convert %qE (type %qT) to type %qT",
6907 arg, from_type, to_type);
6908 return unify_invalid (explain_p);
6911 static int
6912 unify_no_common_base (bool explain_p, enum template_base_result r,
6913 tree parm, tree arg)
6915 if (explain_p)
6916 switch (r)
6918 case tbr_ambiguous_baseclass:
6919 inform (input_location, " %qT is an ambiguous base class of %qT",
6920 parm, arg);
6921 break;
6922 default:
6923 inform (input_location, " %qT is not derived from %qT", arg, parm);
6924 break;
6926 return unify_invalid (explain_p);
6929 static int
6930 unify_inconsistent_template_template_parameters (bool explain_p)
6932 if (explain_p)
6933 inform (input_location,
6934 " template parameters of a template template argument are "
6935 "inconsistent with other deduced template arguments");
6936 return unify_invalid (explain_p);
6939 static int
6940 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6942 if (explain_p)
6943 inform (input_location,
6944 " cannot deduce a template for %qT from non-template type %qT",
6945 parm, arg);
6946 return unify_invalid (explain_p);
6949 static int
6950 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6952 if (explain_p)
6953 inform (input_location,
6954 " template argument %qE does not match %qE", arg, parm);
6955 return unify_invalid (explain_p);
6958 /* True if T is a C++20 template parameter object to store the argument for a
6959 template parameter of class type. */
6961 bool
6962 template_parm_object_p (const_tree t)
6964 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6965 && startswith (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA"));
6968 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6969 argument for TYPE, points to an unsuitable object.
6971 Also adjust the type of the index in C++20 array subobject references. */
6973 static bool
6974 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6976 switch (TREE_CODE (expr))
6978 CASE_CONVERT:
6979 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6980 complain);
6982 case TARGET_EXPR:
6983 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6984 complain);
6986 case CONSTRUCTOR:
6988 unsigned i; tree elt;
6989 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6990 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6991 return true;
6993 break;
6995 case ADDR_EXPR:
6997 tree decl = TREE_OPERAND (expr, 0);
6999 if (cxx_dialect >= cxx20)
7000 while (TREE_CODE (decl) == COMPONENT_REF
7001 || TREE_CODE (decl) == ARRAY_REF)
7003 tree &op = TREE_OPERAND (decl, 1);
7004 if (TREE_CODE (decl) == ARRAY_REF
7005 && TREE_CODE (op) == INTEGER_CST)
7006 /* Canonicalize array offsets to ptrdiff_t; how they were
7007 written doesn't matter for subobject identity. */
7008 op = fold_convert (ptrdiff_type_node, op);
7009 decl = TREE_OPERAND (decl, 0);
7012 if (!VAR_P (decl))
7014 if (complain & tf_error)
7015 error_at (cp_expr_loc_or_input_loc (expr),
7016 "%qE is not a valid template argument of type %qT "
7017 "because %qE is not a variable", expr, type, decl);
7018 return true;
7020 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7022 if (complain & tf_error)
7023 error_at (cp_expr_loc_or_input_loc (expr),
7024 "%qE is not a valid template argument of type %qT "
7025 "in C++98 because %qD does not have external linkage",
7026 expr, type, decl);
7027 return true;
7029 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7030 && decl_linkage (decl) == lk_none)
7032 if (complain & tf_error)
7033 error_at (cp_expr_loc_or_input_loc (expr),
7034 "%qE is not a valid template argument of type %qT "
7035 "because %qD has no linkage", expr, type, decl);
7036 return true;
7038 /* C++17: For a non-type template-parameter of reference or pointer
7039 type, the value of the constant expression shall not refer to (or
7040 for a pointer type, shall not be the address of):
7041 * a subobject (4.5),
7042 * a temporary object (15.2),
7043 * a string literal (5.13.5),
7044 * the result of a typeid expression (8.2.8), or
7045 * a predefined __func__ variable (11.4.1). */
7046 else if (DECL_ARTIFICIAL (decl))
7048 if (complain & tf_error)
7049 error ("the address of %qD is not a valid template argument",
7050 decl);
7051 return true;
7053 else if (cxx_dialect < cxx20
7054 && !(same_type_ignoring_top_level_qualifiers_p
7055 (strip_array_types (TREE_TYPE (type)),
7056 strip_array_types (TREE_TYPE (decl)))))
7058 if (complain & tf_error)
7059 error ("the address of the %qT subobject of %qD is not a "
7060 "valid template argument", TREE_TYPE (type), decl);
7061 return true;
7063 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7065 if (complain & tf_error)
7066 error ("the address of %qD is not a valid template argument "
7067 "because it does not have static storage duration",
7068 decl);
7069 return true;
7072 break;
7074 default:
7075 if (!INDIRECT_TYPE_P (type))
7076 /* We're only concerned about pointers and references here. */;
7077 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7078 /* Null pointer values are OK in C++11. */;
7079 else
7081 if (VAR_P (expr))
7083 if (complain & tf_error)
7084 error ("%qD is not a valid template argument "
7085 "because %qD is a variable, not the address of "
7086 "a variable", expr, expr);
7087 return true;
7089 else
7091 if (complain & tf_error)
7092 error ("%qE is not a valid template argument for %qT "
7093 "because it is not the address of a variable",
7094 expr, type);
7095 return true;
7099 return false;
7103 /* The template arguments corresponding to template parameter objects of types
7104 that contain pointers to members. */
7106 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7108 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7109 template argument EXPR. */
7111 static tree
7112 get_template_parm_object (tree expr, tsubst_flags_t complain)
7114 if (TREE_CODE (expr) == TARGET_EXPR)
7115 expr = TARGET_EXPR_INITIAL (expr);
7117 if (!TREE_CONSTANT (expr))
7119 if ((complain & tf_error)
7120 && require_rvalue_constant_expression (expr))
7121 cxx_constant_value (expr);
7122 return error_mark_node;
7124 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7125 return error_mark_node;
7127 /* This is no longer a compound literal. */
7128 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7130 tree name = mangle_template_parm_object (expr);
7131 tree decl = get_global_binding (name);
7132 if (decl)
7133 return decl;
7135 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7136 decl = create_temporary_var (type);
7137 DECL_CONTEXT (decl) = NULL_TREE;
7138 TREE_STATIC (decl) = true;
7139 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7140 TREE_READONLY (decl) = true;
7141 DECL_NAME (decl) = name;
7142 SET_DECL_ASSEMBLER_NAME (decl, name);
7143 comdat_linkage (decl);
7145 if (!zero_init_p (type))
7147 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7148 lower_var_init before we're done mangling. So store the original
7149 value elsewhere. */
7150 tree copy = unshare_constructor (expr);
7151 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7154 pushdecl_top_level_and_finish (decl, expr);
7156 return decl;
7159 /* Return the actual template argument corresponding to template parameter
7160 object VAR. */
7162 tree
7163 tparm_object_argument (tree var)
7165 if (zero_init_p (TREE_TYPE (var)))
7166 return DECL_INITIAL (var);
7167 return *(tparm_obj_values->get (var));
7170 /* Attempt to convert the non-type template parameter EXPR to the
7171 indicated TYPE. If the conversion is successful, return the
7172 converted value. If the conversion is unsuccessful, return
7173 NULL_TREE if we issued an error message, or error_mark_node if we
7174 did not. We issue error messages for out-and-out bad template
7175 parameters, but not simply because the conversion failed, since we
7176 might be just trying to do argument deduction. Both TYPE and EXPR
7177 must be non-dependent.
7179 The conversion follows the special rules described in
7180 [temp.arg.nontype], and it is much more strict than an implicit
7181 conversion.
7183 This function is called twice for each template argument (see
7184 lookup_template_class for a more accurate description of this
7185 problem). This means that we need to handle expressions which
7186 are not valid in a C++ source, but can be created from the
7187 first call (for instance, casts to perform conversions). These
7188 hacks can go away after we fix the double coercion problem. */
7190 static tree
7191 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7193 tree expr_type;
7194 location_t loc = cp_expr_loc_or_input_loc (expr);
7196 /* Detect immediately string literals as invalid non-type argument.
7197 This special-case is not needed for correctness (we would easily
7198 catch this later), but only to provide better diagnostic for this
7199 common user mistake. As suggested by DR 100, we do not mention
7200 linkage issues in the diagnostic as this is not the point. */
7201 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7203 if (complain & tf_error)
7204 error ("%qE is not a valid template argument for type %qT "
7205 "because string literals can never be used in this context",
7206 expr, type);
7207 return NULL_TREE;
7210 /* Add the ADDR_EXPR now for the benefit of
7211 value_dependent_expression_p. */
7212 if (TYPE_PTROBV_P (type)
7213 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7215 expr = decay_conversion (expr, complain);
7216 if (expr == error_mark_node)
7217 return error_mark_node;
7220 /* If we are in a template, EXPR may be non-dependent, but still
7221 have a syntactic, rather than semantic, form. For example, EXPR
7222 might be a SCOPE_REF, rather than the VAR_DECL to which the
7223 SCOPE_REF refers. Preserving the qualifying scope is necessary
7224 so that access checking can be performed when the template is
7225 instantiated -- but here we need the resolved form so that we can
7226 convert the argument. */
7227 bool non_dep = false;
7228 if (TYPE_REF_OBJ_P (type)
7229 && has_value_dependent_address (expr))
7230 /* If we want the address and it's value-dependent, don't fold. */;
7231 else if (processing_template_decl
7232 && is_nondependent_constant_expression (expr))
7233 non_dep = true;
7234 if (error_operand_p (expr))
7235 return error_mark_node;
7236 expr_type = TREE_TYPE (expr);
7238 /* If the argument is non-dependent, perform any conversions in
7239 non-dependent context as well. */
7240 processing_template_decl_sentinel s (non_dep);
7241 if (non_dep)
7242 expr = instantiate_non_dependent_expr_internal (expr, complain);
7244 const bool val_dep_p = value_dependent_expression_p (expr);
7245 if (val_dep_p)
7246 expr = canonicalize_expr_argument (expr, complain);
7248 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7249 to a non-type argument of "nullptr". */
7250 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7251 expr = fold_simple (convert (type, expr));
7253 /* In C++11, integral or enumeration non-type template arguments can be
7254 arbitrary constant expressions. Pointer and pointer to
7255 member arguments can be general constant expressions that evaluate
7256 to a null value, but otherwise still need to be of a specific form. */
7257 if (cxx_dialect >= cxx11)
7259 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7260 /* A PTRMEM_CST is already constant, and a valid template
7261 argument for a parameter of pointer to member type, we just want
7262 to leave it in that form rather than lower it to a
7263 CONSTRUCTOR. */;
7264 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7265 || cxx_dialect >= cxx17)
7267 /* C++17: A template-argument for a non-type template-parameter shall
7268 be a converted constant expression (8.20) of the type of the
7269 template-parameter. */
7270 expr = build_converted_constant_expr (type, expr, complain);
7271 if (expr == error_mark_node)
7272 /* Make sure we return NULL_TREE only if we have really issued
7273 an error, as described above. */
7274 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7275 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7277 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7278 return expr;
7280 expr = maybe_constant_value (expr, NULL_TREE,
7281 /*manifestly_const_eval=*/true);
7282 expr = convert_from_reference (expr);
7284 else if (TYPE_PTR_OR_PTRMEM_P (type))
7286 tree folded = maybe_constant_value (expr, NULL_TREE,
7287 /*manifestly_const_eval=*/true);
7288 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7289 : null_member_pointer_value_p (folded))
7290 expr = folded;
7294 if (TYPE_REF_P (type))
7295 expr = mark_lvalue_use (expr);
7296 else
7297 expr = mark_rvalue_use (expr);
7299 /* HACK: Due to double coercion, we can get a
7300 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7301 which is the tree that we built on the first call (see
7302 below when coercing to reference to object or to reference to
7303 function). We just strip everything and get to the arg.
7304 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7305 for examples. */
7306 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7308 tree probe_type, probe = expr;
7309 if (REFERENCE_REF_P (probe))
7310 probe = TREE_OPERAND (probe, 0);
7311 probe_type = TREE_TYPE (probe);
7312 if (TREE_CODE (probe) == NOP_EXPR)
7314 /* ??? Maybe we could use convert_from_reference here, but we
7315 would need to relax its constraints because the NOP_EXPR
7316 could actually change the type to something more cv-qualified,
7317 and this is not folded by convert_from_reference. */
7318 tree addr = TREE_OPERAND (probe, 0);
7319 if (TYPE_REF_P (probe_type)
7320 && TREE_CODE (addr) == ADDR_EXPR
7321 && TYPE_PTR_P (TREE_TYPE (addr))
7322 && (same_type_ignoring_top_level_qualifiers_p
7323 (TREE_TYPE (probe_type),
7324 TREE_TYPE (TREE_TYPE (addr)))))
7326 expr = TREE_OPERAND (addr, 0);
7327 expr_type = TREE_TYPE (probe_type);
7332 /* [temp.arg.nontype]/5, bullet 1
7334 For a non-type template-parameter of integral or enumeration type,
7335 integral promotions (_conv.prom_) and integral conversions
7336 (_conv.integral_) are applied. */
7337 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7338 || TREE_CODE (type) == REAL_TYPE)
7340 if (cxx_dialect < cxx11)
7342 tree t = build_converted_constant_expr (type, expr, complain);
7343 t = maybe_constant_value (t);
7344 if (t != error_mark_node)
7345 expr = t;
7348 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7349 return error_mark_node;
7351 /* Notice that there are constant expressions like '4 % 0' which
7352 do not fold into integer constants. */
7353 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7355 if (complain & tf_error)
7357 int errs = errorcount, warns = warningcount + werrorcount;
7358 if (!require_potential_constant_expression (expr))
7359 expr = error_mark_node;
7360 else
7361 expr = cxx_constant_value (expr);
7362 if (errorcount > errs || warningcount + werrorcount > warns)
7363 inform (loc, "in template argument for type %qT", type);
7364 if (expr == error_mark_node)
7365 return NULL_TREE;
7366 /* else cxx_constant_value complained but gave us
7367 a real constant, so go ahead. */
7368 if (!CONSTANT_CLASS_P (expr))
7370 /* Some assemble time constant expressions like
7371 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7372 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7373 as we can emit them into .rodata initializers of
7374 variables, yet they can't fold into an INTEGER_CST at
7375 compile time. Refuse them here. */
7376 gcc_checking_assert (reduced_constant_expression_p (expr));
7377 error_at (loc, "template argument %qE for type %qT not "
7378 "a compile-time constant", expr, type);
7379 return NULL_TREE;
7382 else
7383 return NULL_TREE;
7386 /* Avoid typedef problems. */
7387 if (TREE_TYPE (expr) != type)
7388 expr = fold_convert (type, expr);
7390 /* [temp.arg.nontype]/5, bullet 2
7392 For a non-type template-parameter of type pointer to object,
7393 qualification conversions (_conv.qual_) and the array-to-pointer
7394 conversion (_conv.array_) are applied. */
7395 else if (TYPE_PTROBV_P (type))
7397 tree decayed = expr;
7399 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7400 decay_conversion or an explicit cast. If it's a problematic cast,
7401 we'll complain about it below. */
7402 if (TREE_CODE (expr) == NOP_EXPR)
7404 tree probe = expr;
7405 STRIP_NOPS (probe);
7406 if (TREE_CODE (probe) == ADDR_EXPR
7407 && TYPE_PTR_P (TREE_TYPE (probe)))
7409 expr = probe;
7410 expr_type = TREE_TYPE (expr);
7414 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7416 A template-argument for a non-type, non-template template-parameter
7417 shall be one of: [...]
7419 -- the name of a non-type template-parameter;
7420 -- the address of an object or function with external linkage, [...]
7421 expressed as "& id-expression" where the & is optional if the name
7422 refers to a function or array, or if the corresponding
7423 template-parameter is a reference.
7425 Here, we do not care about functions, as they are invalid anyway
7426 for a parameter of type pointer-to-object. */
7428 if (val_dep_p)
7429 /* Non-type template parameters are OK. */
7431 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7432 /* Null pointer values are OK in C++11. */;
7433 else if (TREE_CODE (expr) != ADDR_EXPR
7434 && !INDIRECT_TYPE_P (expr_type))
7435 /* Other values, like integer constants, might be valid
7436 non-type arguments of some other type. */
7437 return error_mark_node;
7438 else if (invalid_tparm_referent_p (type, expr, complain))
7439 return NULL_TREE;
7441 expr = decayed;
7443 expr = perform_qualification_conversions (type, expr);
7444 if (expr == error_mark_node)
7445 return error_mark_node;
7447 /* [temp.arg.nontype]/5, bullet 3
7449 For a non-type template-parameter of type reference to object, no
7450 conversions apply. The type referred to by the reference may be more
7451 cv-qualified than the (otherwise identical) type of the
7452 template-argument. The template-parameter is bound directly to the
7453 template-argument, which must be an lvalue. */
7454 else if (TYPE_REF_OBJ_P (type))
7456 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7457 expr_type))
7458 return error_mark_node;
7460 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7462 if (complain & tf_error)
7463 error ("%qE is not a valid template argument for type %qT "
7464 "because of conflicts in cv-qualification", expr, type);
7465 return NULL_TREE;
7468 if (!lvalue_p (expr))
7470 if (complain & tf_error)
7471 error ("%qE is not a valid template argument for type %qT "
7472 "because it is not an lvalue", expr, type);
7473 return NULL_TREE;
7476 /* [temp.arg.nontype]/1
7478 A template-argument for a non-type, non-template template-parameter
7479 shall be one of: [...]
7481 -- the address of an object or function with external linkage. */
7482 if (INDIRECT_REF_P (expr)
7483 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7485 expr = TREE_OPERAND (expr, 0);
7486 if (DECL_P (expr))
7488 if (complain & tf_error)
7489 error ("%q#D is not a valid template argument for type %qT "
7490 "because a reference variable does not have a constant "
7491 "address", expr, type);
7492 return NULL_TREE;
7496 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7497 /* OK, dependent reference. We don't want to ask whether a DECL is
7498 itself value-dependent, since what we want here is its address. */;
7499 else
7501 expr = build_address (expr);
7503 if (invalid_tparm_referent_p (type, expr, complain))
7504 return NULL_TREE;
7507 if (!same_type_p (type, TREE_TYPE (expr)))
7508 expr = build_nop (type, expr);
7510 /* [temp.arg.nontype]/5, bullet 4
7512 For a non-type template-parameter of type pointer to function, only
7513 the function-to-pointer conversion (_conv.func_) is applied. If the
7514 template-argument represents a set of overloaded functions (or a
7515 pointer to such), the matching function is selected from the set
7516 (_over.over_). */
7517 else if (TYPE_PTRFN_P (type))
7519 /* If the argument is a template-id, we might not have enough
7520 context information to decay the pointer. */
7521 if (!type_unknown_p (expr_type))
7523 expr = decay_conversion (expr, complain);
7524 if (expr == error_mark_node)
7525 return error_mark_node;
7528 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7529 /* Null pointer values are OK in C++11. */
7530 return perform_qualification_conversions (type, expr);
7532 expr = convert_nontype_argument_function (type, expr, complain);
7533 if (!expr || expr == error_mark_node)
7534 return expr;
7536 /* [temp.arg.nontype]/5, bullet 5
7538 For a non-type template-parameter of type reference to function, no
7539 conversions apply. If the template-argument represents a set of
7540 overloaded functions, the matching function is selected from the set
7541 (_over.over_). */
7542 else if (TYPE_REFFN_P (type))
7544 if (TREE_CODE (expr) == ADDR_EXPR)
7546 if (complain & tf_error)
7548 error ("%qE is not a valid template argument for type %qT "
7549 "because it is a pointer", expr, type);
7550 inform (input_location, "try using %qE instead",
7551 TREE_OPERAND (expr, 0));
7553 return NULL_TREE;
7556 expr = convert_nontype_argument_function (type, expr, complain);
7557 if (!expr || expr == error_mark_node)
7558 return expr;
7560 /* [temp.arg.nontype]/5, bullet 6
7562 For a non-type template-parameter of type pointer to member function,
7563 no conversions apply. If the template-argument represents a set of
7564 overloaded member functions, the matching member function is selected
7565 from the set (_over.over_). */
7566 else if (TYPE_PTRMEMFUNC_P (type))
7568 expr = instantiate_type (type, expr, tf_none);
7569 if (expr == error_mark_node)
7570 return error_mark_node;
7572 /* [temp.arg.nontype] bullet 1 says the pointer to member
7573 expression must be a pointer-to-member constant. */
7574 if (!val_dep_p
7575 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7576 return NULL_TREE;
7578 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7579 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7580 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7581 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7583 /* [temp.arg.nontype]/5, bullet 7
7585 For a non-type template-parameter of type pointer to data member,
7586 qualification conversions (_conv.qual_) are applied. */
7587 else if (TYPE_PTRDATAMEM_P (type))
7589 /* [temp.arg.nontype] bullet 1 says the pointer to member
7590 expression must be a pointer-to-member constant. */
7591 if (!val_dep_p
7592 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7593 return NULL_TREE;
7595 expr = perform_qualification_conversions (type, expr);
7596 if (expr == error_mark_node)
7597 return expr;
7599 else if (NULLPTR_TYPE_P (type))
7601 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7603 if (complain & tf_error)
7604 error ("%qE is not a valid template argument for type %qT "
7605 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7606 return NULL_TREE;
7608 return expr;
7610 else if (CLASS_TYPE_P (type))
7612 /* Replace the argument with a reference to the corresponding template
7613 parameter object. */
7614 if (!val_dep_p)
7615 expr = get_template_parm_object (expr, complain);
7616 if (expr == error_mark_node)
7617 return NULL_TREE;
7619 /* A template non-type parameter must be one of the above. */
7620 else
7621 gcc_unreachable ();
7623 /* Sanity check: did we actually convert the argument to the
7624 right type? */
7625 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7626 (type, TREE_TYPE (expr)));
7627 return convert_from_reference (expr);
7630 /* Subroutine of coerce_template_template_parms, which returns 1 if
7631 PARM_PARM and ARG_PARM match using the rule for the template
7632 parameters of template template parameters. Both PARM and ARG are
7633 template parameters; the rest of the arguments are the same as for
7634 coerce_template_template_parms.
7636 static int
7637 coerce_template_template_parm (tree parm,
7638 tree arg,
7639 tsubst_flags_t complain,
7640 tree in_decl,
7641 tree outer_args)
7643 if (arg == NULL_TREE || error_operand_p (arg)
7644 || parm == NULL_TREE || error_operand_p (parm))
7645 return 0;
7647 if (TREE_CODE (arg) != TREE_CODE (parm))
7648 return 0;
7650 switch (TREE_CODE (parm))
7652 case TEMPLATE_DECL:
7653 /* We encounter instantiations of templates like
7654 template <template <template <class> class> class TT>
7655 class C; */
7657 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7658 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7660 if (!coerce_template_template_parms
7661 (parmparm, argparm, complain, in_decl, outer_args))
7662 return 0;
7664 /* Fall through. */
7666 case TYPE_DECL:
7667 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7668 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7669 /* Argument is a parameter pack but parameter is not. */
7670 return 0;
7671 break;
7673 case PARM_DECL:
7674 /* The tsubst call is used to handle cases such as
7676 template <int> class C {};
7677 template <class T, template <T> class TT> class D {};
7678 D<int, C> d;
7680 i.e. the parameter list of TT depends on earlier parameters. */
7681 if (!uses_template_parms (TREE_TYPE (arg)))
7683 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7684 if (!uses_template_parms (t)
7685 && !same_type_p (t, TREE_TYPE (arg)))
7686 return 0;
7689 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7690 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7691 /* Argument is a parameter pack but parameter is not. */
7692 return 0;
7694 break;
7696 default:
7697 gcc_unreachable ();
7700 return 1;
7703 /* Coerce template argument list ARGLIST for use with template
7704 template-parameter TEMPL. */
7706 static tree
7707 coerce_template_args_for_ttp (tree templ, tree arglist,
7708 tsubst_flags_t complain)
7710 /* Consider an example where a template template parameter declared as
7712 template <class T, class U = std::allocator<T> > class TT
7714 The template parameter level of T and U are one level larger than
7715 of TT. To proper process the default argument of U, say when an
7716 instantiation `TT<int>' is seen, we need to build the full
7717 arguments containing {int} as the innermost level. Outer levels,
7718 available when not appearing as default template argument, can be
7719 obtained from the arguments of the enclosing template.
7721 Suppose that TT is later substituted with std::vector. The above
7722 instantiation is `TT<int, std::allocator<T> >' with TT at
7723 level 1, and T at level 2, while the template arguments at level 1
7724 becomes {std::vector} and the inner level 2 is {int}. */
7726 tree outer = DECL_CONTEXT (templ);
7727 if (outer)
7728 outer = generic_targs_for (outer);
7729 else if (current_template_parms)
7731 /* This is an argument of the current template, so we haven't set
7732 DECL_CONTEXT yet. */
7733 tree relevant_template_parms;
7735 /* Parameter levels that are greater than the level of the given
7736 template template parm are irrelevant. */
7737 relevant_template_parms = current_template_parms;
7738 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7739 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7740 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7742 outer = template_parms_to_args (relevant_template_parms);
7745 if (outer)
7746 arglist = add_to_template_args (outer, arglist);
7748 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7749 return coerce_template_parms (parmlist, arglist, templ,
7750 complain,
7751 /*require_all_args=*/true,
7752 /*use_default_args=*/true);
7755 /* A cache of template template parameters with match-all default
7756 arguments. */
7757 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7759 /* T is a bound template template-parameter. Copy its arguments into default
7760 arguments of the template template-parameter's template parameters. */
7762 static tree
7763 add_defaults_to_ttp (tree otmpl)
7765 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7766 return *c;
7768 tree ntmpl = copy_node (otmpl);
7770 tree ntype = copy_node (TREE_TYPE (otmpl));
7771 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7772 TYPE_MAIN_VARIANT (ntype) = ntype;
7773 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7774 TYPE_NAME (ntype) = ntmpl;
7775 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7777 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7778 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7779 TEMPLATE_PARM_DECL (idx) = ntmpl;
7780 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7782 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7783 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7784 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7785 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7786 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7788 tree o = TREE_VEC_ELT (vec, i);
7789 if (!template_parameter_pack_p (TREE_VALUE (o)))
7791 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7792 TREE_PURPOSE (n) = any_targ_node;
7796 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7797 return ntmpl;
7800 /* ARG is a bound potential template template-argument, and PARGS is a list
7801 of arguments for the corresponding template template-parameter. Adjust
7802 PARGS as appropriate for application to ARG's template, and if ARG is a
7803 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7804 arguments to the template template parameter. */
7806 static tree
7807 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7809 ++processing_template_decl;
7810 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7811 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7813 /* When comparing two template template-parameters in partial ordering,
7814 rewrite the one currently being used as an argument to have default
7815 arguments for all parameters. */
7816 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7817 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7818 if (pargs != error_mark_node)
7819 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7820 TYPE_TI_ARGS (arg));
7822 else
7824 tree aparms
7825 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7826 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7827 /*require_all*/true,
7828 /*use_default*/true);
7830 --processing_template_decl;
7831 return pargs;
7834 /* Subroutine of unify for the case when PARM is a
7835 BOUND_TEMPLATE_TEMPLATE_PARM. */
7837 static int
7838 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7839 bool explain_p)
7841 tree parmvec = TYPE_TI_ARGS (parm);
7842 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7844 /* The template template parm might be variadic and the argument
7845 not, so flatten both argument lists. */
7846 parmvec = expand_template_argument_pack (parmvec);
7847 argvec = expand_template_argument_pack (argvec);
7849 if (flag_new_ttp)
7851 /* In keeping with P0522R0, adjust P's template arguments
7852 to apply to A's template; then flatten it again. */
7853 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7854 nparmvec = expand_template_argument_pack (nparmvec);
7856 if (unify (tparms, targs, nparmvec, argvec,
7857 UNIFY_ALLOW_NONE, explain_p))
7858 return 1;
7860 /* If the P0522 adjustment eliminated a pack expansion, deduce
7861 empty packs. */
7862 if (flag_new_ttp
7863 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7864 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7865 DEDUCE_EXACT, /*sub*/true, explain_p))
7866 return 1;
7868 else
7870 /* Deduce arguments T, i from TT<T> or TT<i>.
7871 We check each element of PARMVEC and ARGVEC individually
7872 rather than the whole TREE_VEC since they can have
7873 different number of elements, which is allowed under N2555. */
7875 int len = TREE_VEC_LENGTH (parmvec);
7877 /* Check if the parameters end in a pack, making them
7878 variadic. */
7879 int parm_variadic_p = 0;
7880 if (len > 0
7881 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7882 parm_variadic_p = 1;
7884 for (int i = 0; i < len - parm_variadic_p; ++i)
7885 /* If the template argument list of P contains a pack
7886 expansion that is not the last template argument, the
7887 entire template argument list is a non-deduced
7888 context. */
7889 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7890 return unify_success (explain_p);
7892 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7893 return unify_too_few_arguments (explain_p,
7894 TREE_VEC_LENGTH (argvec), len);
7896 for (int i = 0; i < len - parm_variadic_p; ++i)
7897 if (unify (tparms, targs,
7898 TREE_VEC_ELT (parmvec, i),
7899 TREE_VEC_ELT (argvec, i),
7900 UNIFY_ALLOW_NONE, explain_p))
7901 return 1;
7903 if (parm_variadic_p
7904 && unify_pack_expansion (tparms, targs,
7905 parmvec, argvec,
7906 DEDUCE_EXACT,
7907 /*subr=*/true, explain_p))
7908 return 1;
7911 return 0;
7914 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7915 template template parameters. Both PARM_PARMS and ARG_PARMS are
7916 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7917 or PARM_DECL.
7919 Consider the example:
7920 template <class T> class A;
7921 template<template <class U> class TT> class B;
7923 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7924 the parameters to A, and OUTER_ARGS contains A. */
7926 static int
7927 coerce_template_template_parms (tree parm_parms,
7928 tree arg_parms,
7929 tsubst_flags_t complain,
7930 tree in_decl,
7931 tree outer_args)
7933 int nparms, nargs, i;
7934 tree parm, arg;
7935 int variadic_p = 0;
7937 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7938 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7940 nparms = TREE_VEC_LENGTH (parm_parms);
7941 nargs = TREE_VEC_LENGTH (arg_parms);
7943 if (flag_new_ttp)
7945 /* P0522R0: A template template-parameter P is at least as specialized as
7946 a template template-argument A if, given the following rewrite to two
7947 function templates, the function template corresponding to P is at
7948 least as specialized as the function template corresponding to A
7949 according to the partial ordering rules for function templates
7950 ([temp.func.order]). Given an invented class template X with the
7951 template parameter list of A (including default arguments):
7953 * Each of the two function templates has the same template parameters,
7954 respectively, as P or A.
7956 * Each function template has a single function parameter whose type is
7957 a specialization of X with template arguments corresponding to the
7958 template parameters from the respective function template where, for
7959 each template parameter PP in the template parameter list of the
7960 function template, a corresponding template argument AA is formed. If
7961 PP declares a parameter pack, then AA is the pack expansion
7962 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7964 If the rewrite produces an invalid type, then P is not at least as
7965 specialized as A. */
7967 /* So coerce P's args to apply to A's parms, and then deduce between A's
7968 args and the converted args. If that succeeds, A is at least as
7969 specialized as P, so they match.*/
7970 tree pargs = template_parms_level_to_args (parm_parms);
7971 pargs = add_outermost_template_args (outer_args, pargs);
7972 ++processing_template_decl;
7973 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7974 /*require_all*/true, /*use_default*/true);
7975 --processing_template_decl;
7976 if (pargs != error_mark_node)
7978 tree targs = make_tree_vec (nargs);
7979 tree aargs = template_parms_level_to_args (arg_parms);
7980 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7981 /*explain*/false))
7982 return 1;
7986 /* Determine whether we have a parameter pack at the end of the
7987 template template parameter's template parameter list. */
7988 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7990 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7992 if (error_operand_p (parm))
7993 return 0;
7995 switch (TREE_CODE (parm))
7997 case TEMPLATE_DECL:
7998 case TYPE_DECL:
7999 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8000 variadic_p = 1;
8001 break;
8003 case PARM_DECL:
8004 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8005 variadic_p = 1;
8006 break;
8008 default:
8009 gcc_unreachable ();
8013 if (nargs != nparms
8014 && !(variadic_p && nargs >= nparms - 1))
8015 return 0;
8017 /* Check all of the template parameters except the parameter pack at
8018 the end (if any). */
8019 for (i = 0; i < nparms - variadic_p; ++i)
8021 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8022 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8023 continue;
8025 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8026 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8028 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8029 outer_args))
8030 return 0;
8034 if (variadic_p)
8036 /* Check each of the template parameters in the template
8037 argument against the template parameter pack at the end of
8038 the template template parameter. */
8039 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8040 return 0;
8042 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8044 for (; i < nargs; ++i)
8046 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8047 continue;
8049 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8051 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8052 outer_args))
8053 return 0;
8057 return 1;
8060 /* Verifies that the deduced template arguments (in TARGS) for the
8061 template template parameters (in TPARMS) represent valid bindings,
8062 by comparing the template parameter list of each template argument
8063 to the template parameter list of its corresponding template
8064 template parameter, in accordance with DR150. This
8065 routine can only be called after all template arguments have been
8066 deduced. It will return TRUE if all of the template template
8067 parameter bindings are okay, FALSE otherwise. */
8068 bool
8069 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8071 int i, ntparms = TREE_VEC_LENGTH (tparms);
8072 bool ret = true;
8074 /* We're dealing with template parms in this process. */
8075 ++processing_template_decl;
8077 targs = INNERMOST_TEMPLATE_ARGS (targs);
8079 for (i = 0; i < ntparms; ++i)
8081 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8082 tree targ = TREE_VEC_ELT (targs, i);
8084 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8086 tree packed_args = NULL_TREE;
8087 int idx, len = 1;
8089 if (ARGUMENT_PACK_P (targ))
8091 /* Look inside the argument pack. */
8092 packed_args = ARGUMENT_PACK_ARGS (targ);
8093 len = TREE_VEC_LENGTH (packed_args);
8096 for (idx = 0; idx < len; ++idx)
8098 tree targ_parms = NULL_TREE;
8100 if (packed_args)
8101 /* Extract the next argument from the argument
8102 pack. */
8103 targ = TREE_VEC_ELT (packed_args, idx);
8105 if (PACK_EXPANSION_P (targ))
8106 /* Look at the pattern of the pack expansion. */
8107 targ = PACK_EXPANSION_PATTERN (targ);
8109 /* Extract the template parameters from the template
8110 argument. */
8111 if (TREE_CODE (targ) == TEMPLATE_DECL)
8112 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8113 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8114 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8116 /* Verify that we can coerce the template template
8117 parameters from the template argument to the template
8118 parameter. This requires an exact match. */
8119 if (targ_parms
8120 && !coerce_template_template_parms
8121 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8122 targ_parms,
8123 tf_none,
8124 tparm,
8125 targs))
8127 ret = false;
8128 goto out;
8134 out:
8136 --processing_template_decl;
8137 return ret;
8140 /* Since type attributes aren't mangled, we need to strip them from
8141 template type arguments. */
8143 tree
8144 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8146 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8147 return arg;
8148 bool removed_attributes = false;
8149 tree canon = strip_typedefs (arg, &removed_attributes);
8150 if (removed_attributes
8151 && (complain & tf_warning))
8152 warning (OPT_Wignored_attributes,
8153 "ignoring attributes on template argument %qT", arg);
8154 return canon;
8157 /* And from inside dependent non-type arguments like sizeof(Type). */
8159 static tree
8160 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8162 if (!arg || arg == error_mark_node)
8163 return arg;
8164 bool removed_attributes = false;
8165 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8166 if (removed_attributes
8167 && (complain & tf_warning))
8168 warning (OPT_Wignored_attributes,
8169 "ignoring attributes in template argument %qE", arg);
8170 return canon;
8173 /* A template declaration can be substituted for a constrained
8174 template template parameter only when the argument is no more
8175 constrained than the parameter. */
8177 static bool
8178 is_compatible_template_arg (tree parm, tree arg)
8180 tree parm_cons = get_constraints (parm);
8182 /* For now, allow constrained template template arguments
8183 and unconstrained template template parameters. */
8184 if (parm_cons == NULL_TREE)
8185 return true;
8187 /* If the template parameter is constrained, we need to rewrite its
8188 constraints in terms of the ARG's template parameters. This ensures
8189 that all of the template parameter types will have the same depth.
8191 Note that this is only valid when coerce_template_template_parm is
8192 true for the innermost template parameters of PARM and ARG. In other
8193 words, because coercion is successful, this conversion will be valid. */
8194 tree new_args = NULL_TREE;
8195 if (parm_cons)
8197 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8198 new_args = template_parms_level_to_args (aparms);
8199 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8200 tf_none, NULL_TREE);
8201 if (parm_cons == error_mark_node)
8202 return false;
8205 return weakly_subsumes (parm_cons, arg);
8208 // Convert a placeholder argument into a binding to the original
8209 // parameter. The original parameter is saved as the TREE_TYPE of
8210 // ARG.
8211 static inline tree
8212 convert_wildcard_argument (tree parm, tree arg)
8214 TREE_TYPE (arg) = parm;
8215 return arg;
8218 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8219 because one of them is dependent. But we need to represent the
8220 conversion for the benefit of cp_tree_equal. */
8222 static tree
8223 maybe_convert_nontype_argument (tree type, tree arg)
8225 /* Auto parms get no conversion. */
8226 if (type_uses_auto (type))
8227 return arg;
8228 /* We don't need or want to add this conversion now if we're going to use the
8229 argument for deduction. */
8230 if (value_dependent_expression_p (arg))
8231 return arg;
8233 type = cv_unqualified (type);
8234 tree argtype = TREE_TYPE (arg);
8235 if (same_type_p (type, argtype))
8236 return arg;
8238 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8239 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8240 return arg;
8243 /* Convert the indicated template ARG as necessary to match the
8244 indicated template PARM. Returns the converted ARG, or
8245 error_mark_node if the conversion was unsuccessful. Error and
8246 warning messages are issued under control of COMPLAIN. This
8247 conversion is for the Ith parameter in the parameter list. ARGS is
8248 the full set of template arguments deduced so far. */
8250 static tree
8251 convert_template_argument (tree parm,
8252 tree arg,
8253 tree args,
8254 tsubst_flags_t complain,
8255 int i,
8256 tree in_decl)
8258 tree orig_arg;
8259 tree val;
8260 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8262 if (parm == error_mark_node || error_operand_p (arg))
8263 return error_mark_node;
8265 /* Trivially convert placeholders. */
8266 if (TREE_CODE (arg) == WILDCARD_DECL)
8267 return convert_wildcard_argument (parm, arg);
8269 if (arg == any_targ_node)
8270 return arg;
8272 if (TREE_CODE (arg) == TREE_LIST
8273 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8275 /* The template argument was the name of some
8276 member function. That's usually
8277 invalid, but static members are OK. In any
8278 case, grab the underlying fields/functions
8279 and issue an error later if required. */
8280 TREE_TYPE (arg) = unknown_type_node;
8283 orig_arg = arg;
8285 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8286 requires_type = (TREE_CODE (parm) == TYPE_DECL
8287 || requires_tmpl_type);
8289 /* When determining whether an argument pack expansion is a template,
8290 look at the pattern. */
8291 if (PACK_EXPANSION_P (arg))
8292 arg = PACK_EXPANSION_PATTERN (arg);
8294 /* Deal with an injected-class-name used as a template template arg. */
8295 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8297 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8298 if (TREE_CODE (t) == TEMPLATE_DECL)
8300 if (cxx_dialect >= cxx11)
8301 /* OK under DR 1004. */;
8302 else if (complain & tf_warning_or_error)
8303 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8304 " used as template template argument", TYPE_NAME (arg));
8305 else if (flag_pedantic_errors)
8306 t = arg;
8308 arg = t;
8312 is_tmpl_type =
8313 ((TREE_CODE (arg) == TEMPLATE_DECL
8314 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8315 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8316 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8317 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8319 if (is_tmpl_type
8320 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8321 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8322 arg = TYPE_STUB_DECL (arg);
8324 is_type = TYPE_P (arg) || is_tmpl_type;
8326 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8327 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8329 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8331 if (complain & tf_error)
8332 error ("invalid use of destructor %qE as a type", orig_arg);
8333 return error_mark_node;
8336 permerror (input_location,
8337 "to refer to a type member of a template parameter, "
8338 "use %<typename %E%>", orig_arg);
8340 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8341 TREE_OPERAND (arg, 1),
8342 typename_type,
8343 complain);
8344 arg = orig_arg;
8345 is_type = 1;
8347 if (is_type != requires_type)
8349 if (in_decl)
8351 if (complain & tf_error)
8353 error ("type/value mismatch at argument %d in template "
8354 "parameter list for %qD",
8355 i + 1, in_decl);
8356 if (is_type)
8358 /* The template argument is a type, but we're expecting
8359 an expression. */
8360 inform (input_location,
8361 " expected a constant of type %qT, got %qT",
8362 TREE_TYPE (parm),
8363 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8364 /* [temp.arg]/2: "In a template-argument, an ambiguity
8365 between a type-id and an expression is resolved to a
8366 type-id, regardless of the form of the corresponding
8367 template-parameter." So give the user a clue. */
8368 if (TREE_CODE (arg) == FUNCTION_TYPE)
8369 inform (input_location, " ambiguous template argument "
8370 "for non-type template parameter is treated as "
8371 "function type");
8373 else if (requires_tmpl_type)
8374 inform (input_location,
8375 " expected a class template, got %qE", orig_arg);
8376 else
8377 inform (input_location,
8378 " expected a type, got %qE", orig_arg);
8381 return error_mark_node;
8383 if (is_tmpl_type ^ requires_tmpl_type)
8385 if (in_decl && (complain & tf_error))
8387 error ("type/value mismatch at argument %d in template "
8388 "parameter list for %qD",
8389 i + 1, in_decl);
8390 if (is_tmpl_type)
8391 inform (input_location,
8392 " expected a type, got %qT", DECL_NAME (arg));
8393 else
8394 inform (input_location,
8395 " expected a class template, got %qT", orig_arg);
8397 return error_mark_node;
8400 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8401 /* We already did the appropriate conversion when packing args. */
8402 val = orig_arg;
8403 else if (is_type)
8405 if (requires_tmpl_type)
8407 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8408 /* The number of argument required is not known yet.
8409 Just accept it for now. */
8410 val = orig_arg;
8411 else
8413 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8414 tree argparm;
8416 /* Strip alias templates that are equivalent to another
8417 template. */
8418 arg = get_underlying_template (arg);
8419 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8421 if (coerce_template_template_parms (parmparm, argparm,
8422 complain, in_decl,
8423 args))
8425 val = arg;
8427 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8428 TEMPLATE_DECL. */
8429 if (val != error_mark_node)
8431 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8432 val = TREE_TYPE (val);
8433 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8434 val = make_pack_expansion (val, complain);
8437 else
8439 if (in_decl && (complain & tf_error))
8441 error ("type/value mismatch at argument %d in "
8442 "template parameter list for %qD",
8443 i + 1, in_decl);
8444 inform (input_location,
8445 " expected a template of type %qD, got %qT",
8446 parm, orig_arg);
8449 val = error_mark_node;
8452 // Check that the constraints are compatible before allowing the
8453 // substitution.
8454 if (val != error_mark_node)
8455 if (!is_compatible_template_arg (parm, arg))
8457 if (in_decl && (complain & tf_error))
8459 error ("constraint mismatch at argument %d in "
8460 "template parameter list for %qD",
8461 i + 1, in_decl);
8462 inform (input_location, " expected %qD but got %qD",
8463 parm, arg);
8465 val = error_mark_node;
8469 else
8470 val = orig_arg;
8471 /* We only form one instance of each template specialization.
8472 Therefore, if we use a non-canonical variant (i.e., a
8473 typedef), any future messages referring to the type will use
8474 the typedef, which is confusing if those future uses do not
8475 themselves also use the typedef. */
8476 if (TYPE_P (val))
8477 val = canonicalize_type_argument (val, complain);
8479 else
8481 tree t = TREE_TYPE (parm);
8483 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8484 > TMPL_ARGS_DEPTH (args))
8485 /* We don't have enough levels of args to do any substitution. This
8486 can happen in the context of -fnew-ttp-matching. */;
8487 else if (tree a = type_uses_auto (t))
8489 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8490 LOOKUP_IMPLICIT);
8491 if (t == error_mark_node)
8492 return error_mark_node;
8494 else
8495 t = tsubst (t, args, complain, in_decl);
8497 if (invalid_nontype_parm_type_p (t, complain))
8498 return error_mark_node;
8500 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8501 this non-type template parameter, as per [temp.param]/6. */
8502 t = cv_unqualified (t);
8504 if (t != TREE_TYPE (parm))
8505 t = canonicalize_type_argument (t, complain);
8507 if (!type_dependent_expression_p (orig_arg)
8508 && !uses_template_parms (t))
8509 /* We used to call digest_init here. However, digest_init
8510 will report errors, which we don't want when complain
8511 is zero. More importantly, digest_init will try too
8512 hard to convert things: for example, `0' should not be
8513 converted to pointer type at this point according to
8514 the standard. Accepting this is not merely an
8515 extension, since deciding whether or not these
8516 conversions can occur is part of determining which
8517 function template to call, or whether a given explicit
8518 argument specification is valid. */
8519 val = convert_nontype_argument (t, orig_arg, complain);
8520 else
8522 val = canonicalize_expr_argument (orig_arg, complain);
8523 val = maybe_convert_nontype_argument (t, val);
8527 if (val == NULL_TREE)
8528 val = error_mark_node;
8529 else if (val == error_mark_node && (complain & tf_error))
8530 error_at (cp_expr_loc_or_input_loc (orig_arg),
8531 "could not convert template argument %qE from %qT to %qT",
8532 orig_arg, TREE_TYPE (orig_arg), t);
8534 if (INDIRECT_REF_P (val))
8536 /* Reject template arguments that are references to built-in
8537 functions with no library fallbacks. */
8538 const_tree inner = TREE_OPERAND (val, 0);
8539 const_tree innertype = TREE_TYPE (inner);
8540 if (innertype
8541 && TYPE_REF_P (innertype)
8542 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8543 && TREE_OPERAND_LENGTH (inner) > 0
8544 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8545 return error_mark_node;
8548 if (TREE_CODE (val) == SCOPE_REF)
8550 /* Strip typedefs from the SCOPE_REF. */
8551 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8552 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8553 complain);
8554 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8555 QUALIFIED_NAME_IS_TEMPLATE (val));
8559 return val;
8562 /* Coerces the remaining template arguments in INNER_ARGS (from
8563 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8564 Returns the coerced argument pack. PARM_IDX is the position of this
8565 parameter in the template parameter list. ARGS is the original
8566 template argument list. */
8567 static tree
8568 coerce_template_parameter_pack (tree parms,
8569 int parm_idx,
8570 tree args,
8571 tree inner_args,
8572 int arg_idx,
8573 tree new_args,
8574 int* lost,
8575 tree in_decl,
8576 tsubst_flags_t complain)
8578 tree parm = TREE_VEC_ELT (parms, parm_idx);
8579 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8580 tree packed_args;
8581 tree argument_pack;
8582 tree packed_parms = NULL_TREE;
8584 if (arg_idx > nargs)
8585 arg_idx = nargs;
8587 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8589 /* When the template parameter is a non-type template parameter pack
8590 or template template parameter pack whose type or template
8591 parameters use parameter packs, we know exactly how many arguments
8592 we are looking for. Build a vector of the instantiated decls for
8593 these template parameters in PACKED_PARMS. */
8594 /* We can't use make_pack_expansion here because it would interpret a
8595 _DECL as a use rather than a declaration. */
8596 tree decl = TREE_VALUE (parm);
8597 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8598 SET_PACK_EXPANSION_PATTERN (exp, decl);
8599 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8600 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8602 TREE_VEC_LENGTH (args)--;
8603 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8604 TREE_VEC_LENGTH (args)++;
8606 if (packed_parms == error_mark_node)
8607 return error_mark_node;
8609 /* If we're doing a partial instantiation of a member template,
8610 verify that all of the types used for the non-type
8611 template parameter pack are, in fact, valid for non-type
8612 template parameters. */
8613 if (arg_idx < nargs
8614 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8616 int j, len = TREE_VEC_LENGTH (packed_parms);
8617 for (j = 0; j < len; ++j)
8619 tree t = TREE_VEC_ELT (packed_parms, j);
8620 if (TREE_CODE (t) == PARM_DECL
8621 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8622 return error_mark_node;
8624 /* We don't know how many args we have yet, just
8625 use the unconverted ones for now. */
8626 return NULL_TREE;
8629 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8631 /* Check if we have a placeholder pack, which indicates we're
8632 in the context of a introduction list. In that case we want
8633 to match this pack to the single placeholder. */
8634 else if (arg_idx < nargs
8635 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8636 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8638 nargs = arg_idx + 1;
8639 packed_args = make_tree_vec (1);
8641 else
8642 packed_args = make_tree_vec (nargs - arg_idx);
8644 /* Convert the remaining arguments, which will be a part of the
8645 parameter pack "parm". */
8646 int first_pack_arg = arg_idx;
8647 for (; arg_idx < nargs; ++arg_idx)
8649 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8650 tree actual_parm = TREE_VALUE (parm);
8651 int pack_idx = arg_idx - first_pack_arg;
8653 if (packed_parms)
8655 /* Once we've packed as many args as we have types, stop. */
8656 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8657 break;
8658 else if (PACK_EXPANSION_P (arg))
8659 /* We don't know how many args we have yet, just
8660 use the unconverted ones for now. */
8661 return NULL_TREE;
8662 else
8663 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8666 if (arg == error_mark_node)
8668 if (complain & tf_error)
8669 error ("template argument %d is invalid", arg_idx + 1);
8671 else
8672 arg = convert_template_argument (actual_parm,
8673 arg, new_args, complain, parm_idx,
8674 in_decl);
8675 if (arg == error_mark_node)
8676 (*lost)++;
8677 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8680 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8681 && TREE_VEC_LENGTH (packed_args) > 0)
8683 if (complain & tf_error)
8684 error ("wrong number of template arguments (%d, should be %d)",
8685 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8686 return error_mark_node;
8689 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8690 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8691 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8692 else
8694 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8695 TREE_CONSTANT (argument_pack) = 1;
8698 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8699 if (CHECKING_P)
8700 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8701 TREE_VEC_LENGTH (packed_args));
8702 return argument_pack;
8705 /* Returns the number of pack expansions in the template argument vector
8706 ARGS. */
8708 static int
8709 pack_expansion_args_count (tree args)
8711 int i;
8712 int count = 0;
8713 if (args)
8714 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8716 tree elt = TREE_VEC_ELT (args, i);
8717 if (elt && PACK_EXPANSION_P (elt))
8718 ++count;
8720 return count;
8723 /* Convert all template arguments to their appropriate types, and
8724 return a vector containing the innermost resulting template
8725 arguments. If any error occurs, return error_mark_node. Error and
8726 warning messages are issued under control of COMPLAIN.
8728 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8729 for arguments not specified in ARGS. Otherwise, if
8730 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8731 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8732 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8733 ARGS. */
8735 static tree
8736 coerce_template_parms (tree parms,
8737 tree args,
8738 tree in_decl,
8739 tsubst_flags_t complain,
8740 bool require_all_args,
8741 bool use_default_args)
8743 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8744 tree orig_inner_args;
8745 tree inner_args;
8746 tree new_args;
8747 tree new_inner_args;
8749 /* When used as a boolean value, indicates whether this is a
8750 variadic template parameter list. Since it's an int, we can also
8751 subtract it from nparms to get the number of non-variadic
8752 parameters. */
8753 int variadic_p = 0;
8754 int variadic_args_p = 0;
8755 int post_variadic_parms = 0;
8757 /* Adjustment to nparms for fixed parameter packs. */
8758 int fixed_pack_adjust = 0;
8759 int fixed_packs = 0;
8760 int missing = 0;
8762 /* Likewise for parameters with default arguments. */
8763 int default_p = 0;
8765 if (args == error_mark_node)
8766 return error_mark_node;
8768 nparms = TREE_VEC_LENGTH (parms);
8770 /* Determine if there are any parameter packs or default arguments. */
8771 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8773 tree parm = TREE_VEC_ELT (parms, parm_idx);
8774 if (variadic_p)
8775 ++post_variadic_parms;
8776 if (template_parameter_pack_p (TREE_VALUE (parm)))
8777 ++variadic_p;
8778 if (TREE_PURPOSE (parm))
8779 ++default_p;
8782 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8783 /* If there are no parameters that follow a parameter pack, we need to
8784 expand any argument packs so that we can deduce a parameter pack from
8785 some non-packed args followed by an argument pack, as in variadic85.C.
8786 If there are such parameters, we need to leave argument packs intact
8787 so the arguments are assigned properly. This can happen when dealing
8788 with a nested class inside a partial specialization of a class
8789 template, as in variadic92.C, or when deducing a template parameter pack
8790 from a sub-declarator, as in variadic114.C. */
8791 if (!post_variadic_parms)
8792 inner_args = expand_template_argument_pack (inner_args);
8794 /* Count any pack expansion args. */
8795 variadic_args_p = pack_expansion_args_count (inner_args);
8797 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8798 if ((nargs - variadic_args_p > nparms && !variadic_p)
8799 || (nargs < nparms - variadic_p
8800 && require_all_args
8801 && !variadic_args_p
8802 && (!use_default_args
8803 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8804 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8806 bad_nargs:
8807 if (complain & tf_error)
8809 if (variadic_p || default_p)
8811 nparms -= variadic_p + default_p;
8812 error ("wrong number of template arguments "
8813 "(%d, should be at least %d)", nargs, nparms);
8815 else
8816 error ("wrong number of template arguments "
8817 "(%d, should be %d)", nargs, nparms);
8819 if (in_decl)
8820 inform (DECL_SOURCE_LOCATION (in_decl),
8821 "provided for %qD", in_decl);
8824 return error_mark_node;
8826 /* We can't pass a pack expansion to a non-pack parameter of an alias
8827 template (DR 1430). */
8828 else if (in_decl
8829 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8830 || concept_definition_p (in_decl))
8831 && variadic_args_p
8832 && nargs - variadic_args_p < nparms - variadic_p)
8834 if (complain & tf_error)
8836 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8838 tree arg = TREE_VEC_ELT (inner_args, i);
8839 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8841 if (PACK_EXPANSION_P (arg)
8842 && !template_parameter_pack_p (parm))
8844 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8845 error_at (location_of (arg),
8846 "pack expansion argument for non-pack parameter "
8847 "%qD of alias template %qD", parm, in_decl);
8848 else
8849 error_at (location_of (arg),
8850 "pack expansion argument for non-pack parameter "
8851 "%qD of concept %qD", parm, in_decl);
8852 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8853 goto found;
8856 gcc_unreachable ();
8857 found:;
8859 return error_mark_node;
8862 /* We need to evaluate the template arguments, even though this
8863 template-id may be nested within a "sizeof". */
8864 cp_evaluated ev;
8866 new_inner_args = make_tree_vec (nparms);
8867 new_args = add_outermost_template_args (args, new_inner_args);
8868 int pack_adjust = 0;
8869 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8871 tree arg;
8872 tree parm;
8874 /* Get the Ith template parameter. */
8875 parm = TREE_VEC_ELT (parms, parm_idx);
8877 if (parm == error_mark_node)
8879 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8880 continue;
8883 /* Calculate the next argument. */
8884 if (arg_idx < nargs)
8885 arg = TREE_VEC_ELT (inner_args, arg_idx);
8886 else
8887 arg = NULL_TREE;
8889 if (template_parameter_pack_p (TREE_VALUE (parm))
8890 && (arg || require_all_args || !(complain & tf_partial))
8891 && !(arg && ARGUMENT_PACK_P (arg)))
8893 /* Some arguments will be placed in the
8894 template parameter pack PARM. */
8895 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8896 inner_args, arg_idx,
8897 new_args, &lost,
8898 in_decl, complain);
8900 if (arg == NULL_TREE)
8902 /* We don't know how many args we have yet, just use the
8903 unconverted (and still packed) ones for now. */
8904 new_inner_args = orig_inner_args;
8905 arg_idx = nargs;
8906 break;
8909 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8911 /* Store this argument. */
8912 if (arg == error_mark_node)
8914 lost++;
8915 /* We are done with all of the arguments. */
8916 arg_idx = nargs;
8917 break;
8919 else
8921 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8922 arg_idx += pack_adjust;
8923 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8925 ++fixed_packs;
8926 fixed_pack_adjust += pack_adjust;
8930 continue;
8932 else if (arg)
8934 if (PACK_EXPANSION_P (arg))
8936 /* "If every valid specialization of a variadic template
8937 requires an empty template parameter pack, the template is
8938 ill-formed, no diagnostic required." So check that the
8939 pattern works with this parameter. */
8940 tree pattern = PACK_EXPANSION_PATTERN (arg);
8941 tree conv = convert_template_argument (TREE_VALUE (parm),
8942 pattern, new_args,
8943 complain, parm_idx,
8944 in_decl);
8945 if (conv == error_mark_node)
8947 if (complain & tf_error)
8948 inform (input_location, "so any instantiation with a "
8949 "non-empty parameter pack would be ill-formed");
8950 ++lost;
8952 else if (TYPE_P (conv) && !TYPE_P (pattern))
8953 /* Recover from missing typename. */
8954 TREE_VEC_ELT (inner_args, arg_idx)
8955 = make_pack_expansion (conv, complain);
8957 /* We don't know how many args we have yet, just
8958 use the unconverted ones for now. */
8959 new_inner_args = inner_args;
8960 arg_idx = nargs;
8961 break;
8964 else if (require_all_args)
8966 /* There must be a default arg in this case. */
8967 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8968 complain, in_decl);
8969 /* The position of the first default template argument,
8970 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8971 Record that. */
8972 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8973 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8974 arg_idx - pack_adjust);
8976 else
8977 break;
8979 if (arg == error_mark_node)
8981 if (complain & tf_error)
8982 error ("template argument %d is invalid", arg_idx + 1);
8984 else if (!arg)
8986 /* This can occur if there was an error in the template
8987 parameter list itself (which we would already have
8988 reported) that we are trying to recover from, e.g., a class
8989 template with a parameter list such as
8990 template<typename..., typename> (cpp0x/variadic150.C). */
8991 ++lost;
8993 /* This can also happen with a fixed parameter pack (71834). */
8994 if (arg_idx >= nargs)
8995 ++missing;
8997 else
8998 arg = convert_template_argument (TREE_VALUE (parm),
8999 arg, new_args, complain,
9000 parm_idx, in_decl);
9002 if (arg == error_mark_node)
9003 lost++;
9005 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9008 if (missing || arg_idx < nargs - variadic_args_p)
9010 /* If we had fixed parameter packs, we didn't know how many arguments we
9011 actually needed earlier; now we do. */
9012 nparms += fixed_pack_adjust;
9013 variadic_p -= fixed_packs;
9014 goto bad_nargs;
9017 if (arg_idx < nargs)
9019 /* We had some pack expansion arguments that will only work if the packs
9020 are empty, but wait until instantiation time to complain.
9021 See variadic-ttp3.C. */
9023 /* Except that we can't provide empty packs to alias templates or
9024 concepts when there are no corresponding parameters. Basically,
9025 we can get here with this:
9027 template<typename T> concept C = true;
9029 template<typename... Args>
9030 requires C<Args...>
9031 void f();
9033 When parsing C<Args...>, we try to form a concept check of
9034 C<?, Args...>. Without the extra check for substituting an empty
9035 pack past the last parameter, we can accept the check as valid.
9037 FIXME: This may be valid for alias templates (but I doubt it).
9039 FIXME: The error could be better also. */
9040 if (in_decl && concept_definition_p (in_decl))
9042 if (complain & tf_error)
9043 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9044 "too many arguments");
9045 return error_mark_node;
9048 int len = nparms + (nargs - arg_idx);
9049 tree args = make_tree_vec (len);
9050 int i = 0;
9051 for (; i < nparms; ++i)
9052 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9053 for (; i < len; ++i, ++arg_idx)
9054 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9055 arg_idx - pack_adjust);
9056 new_inner_args = args;
9059 if (lost)
9061 gcc_assert (!(complain & tf_error) || seen_error ());
9062 return error_mark_node;
9065 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9066 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9067 TREE_VEC_LENGTH (new_inner_args));
9069 return new_inner_args;
9072 /* Convert all template arguments to their appropriate types, and
9073 return a vector containing the innermost resulting template
9074 arguments. If any error occurs, return error_mark_node. Error and
9075 warning messages are not issued.
9077 Note that no function argument deduction is performed, and default
9078 arguments are used to fill in unspecified arguments. */
9079 tree
9080 coerce_template_parms (tree parms, tree args, tree in_decl)
9082 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9085 /* Convert all template arguments to their appropriate type, and
9086 instantiate default arguments as needed. This returns a vector
9087 containing the innermost resulting template arguments, or
9088 error_mark_node if unsuccessful. */
9089 tree
9090 coerce_template_parms (tree parms, tree args, tree in_decl,
9091 tsubst_flags_t complain)
9093 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9096 /* Like coerce_template_parms. If PARMS represents all template
9097 parameters levels, this function returns a vector of vectors
9098 representing all the resulting argument levels. Note that in this
9099 case, only the innermost arguments are coerced because the
9100 outermost ones are supposed to have been coerced already.
9102 Otherwise, if PARMS represents only (the innermost) vector of
9103 parameters, this function returns a vector containing just the
9104 innermost resulting arguments. */
9106 static tree
9107 coerce_innermost_template_parms (tree parms,
9108 tree args,
9109 tree in_decl,
9110 tsubst_flags_t complain,
9111 bool require_all_args,
9112 bool use_default_args)
9114 int parms_depth = TMPL_PARMS_DEPTH (parms);
9115 int args_depth = TMPL_ARGS_DEPTH (args);
9116 tree coerced_args;
9118 if (parms_depth > 1)
9120 coerced_args = make_tree_vec (parms_depth);
9121 tree level;
9122 int cur_depth;
9124 for (level = parms, cur_depth = parms_depth;
9125 parms_depth > 0 && level != NULL_TREE;
9126 level = TREE_CHAIN (level), --cur_depth)
9128 tree l;
9129 if (cur_depth == args_depth)
9130 l = coerce_template_parms (TREE_VALUE (level),
9131 args, in_decl, complain,
9132 require_all_args,
9133 use_default_args);
9134 else
9135 l = TMPL_ARGS_LEVEL (args, cur_depth);
9137 if (l == error_mark_node)
9138 return error_mark_node;
9140 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9143 else
9144 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9145 args, in_decl, complain,
9146 require_all_args,
9147 use_default_args);
9148 return coerced_args;
9151 /* Returns true if T is a wrapper to make a C++20 template parameter
9152 object const. */
9154 static bool
9155 class_nttp_const_wrapper_p (tree t)
9157 if (cxx_dialect < cxx20)
9158 return false;
9159 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9160 && CP_TYPE_CONST_P (TREE_TYPE (t))
9161 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9164 /* Returns 1 if template args OT and NT are equivalent. */
9167 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9169 if (nt == ot)
9170 return 1;
9171 if (nt == NULL_TREE || ot == NULL_TREE)
9172 return false;
9173 if (nt == any_targ_node || ot == any_targ_node)
9174 return true;
9176 if (class_nttp_const_wrapper_p (nt))
9177 nt = TREE_OPERAND (nt, 0);
9178 if (class_nttp_const_wrapper_p (ot))
9179 ot = TREE_OPERAND (ot, 0);
9181 /* DR 1558: Don't treat an alias template specialization with dependent
9182 arguments as equivalent to its underlying type when used as a template
9183 argument; we need them to be distinct so that we substitute into the
9184 specialization arguments at instantiation time. And aliases can't be
9185 equivalent without being ==, so we don't need to look any deeper.
9187 During partial ordering, however, we need to treat them normally so we can
9188 order uses of the same alias with different cv-qualification (79960). */
9189 auto cso = make_temp_override (comparing_dependent_aliases);
9190 if (!partial_order)
9191 ++comparing_dependent_aliases;
9193 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9194 /* For member templates */
9195 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9196 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9197 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9198 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9199 PACK_EXPANSION_PATTERN (nt))
9200 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9201 PACK_EXPANSION_EXTRA_ARGS (nt)));
9202 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9203 return cp_tree_equal (ot, nt);
9204 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9205 gcc_unreachable ();
9206 else if (TYPE_P (nt) || TYPE_P (ot))
9208 if (!(TYPE_P (nt) && TYPE_P (ot)))
9209 return false;
9210 return same_type_p (ot, nt);
9212 else
9214 /* Try to treat a template non-type argument that has been converted
9215 to the parameter type as equivalent to one that hasn't yet. */
9216 for (enum tree_code code1 = TREE_CODE (ot);
9217 CONVERT_EXPR_CODE_P (code1)
9218 || code1 == NON_LVALUE_EXPR;
9219 code1 = TREE_CODE (ot))
9220 ot = TREE_OPERAND (ot, 0);
9222 for (enum tree_code code2 = TREE_CODE (nt);
9223 CONVERT_EXPR_CODE_P (code2)
9224 || code2 == NON_LVALUE_EXPR;
9225 code2 = TREE_CODE (nt))
9226 nt = TREE_OPERAND (nt, 0);
9228 return cp_tree_equal (ot, nt);
9232 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9233 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9234 NEWARG_PTR with the offending arguments if they are non-NULL. */
9237 comp_template_args (tree oldargs, tree newargs,
9238 tree *oldarg_ptr, tree *newarg_ptr,
9239 bool partial_order)
9241 int i;
9243 if (oldargs == newargs)
9244 return 1;
9246 if (!oldargs || !newargs)
9247 return 0;
9249 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9250 return 0;
9252 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9254 tree nt = TREE_VEC_ELT (newargs, i);
9255 tree ot = TREE_VEC_ELT (oldargs, i);
9257 if (! template_args_equal (ot, nt, partial_order))
9259 if (oldarg_ptr != NULL)
9260 *oldarg_ptr = ot;
9261 if (newarg_ptr != NULL)
9262 *newarg_ptr = nt;
9263 return 0;
9266 return 1;
9269 inline bool
9270 comp_template_args_porder (tree oargs, tree nargs)
9272 return comp_template_args (oargs, nargs, NULL, NULL, true);
9275 /* Implement a freelist interface for objects of type T.
9277 Head is a separate object, rather than a regular member, so that we
9278 can define it as a GTY deletable pointer, which is highly
9279 desirable. A data member could be declared that way, but then the
9280 containing object would implicitly get GTY((user)), which would
9281 prevent us from instantiating freelists as global objects.
9282 Although this way we can create freelist global objects, they're
9283 such thin wrappers that instantiating temporaries at every use
9284 loses nothing and saves permanent storage for the freelist object.
9286 Member functions next, anew, poison and reinit have default
9287 implementations that work for most of the types we're interested
9288 in, but if they don't work for some type, they should be explicitly
9289 specialized. See the comments before them for requirements, and
9290 the example specializations for the tree_list_freelist. */
9291 template <typename T>
9292 class freelist
9294 /* Return the next object in a chain. We could just do type
9295 punning, but if we access the object with its underlying type, we
9296 avoid strict-aliasing trouble. This needs only work between
9297 poison and reinit. */
9298 static T *&next (T *obj) { return obj->next; }
9300 /* Return a newly allocated, uninitialized or minimally-initialized
9301 object of type T. Any initialization performed by anew should
9302 either remain across the life of the object and the execution of
9303 poison, or be redone by reinit. */
9304 static T *anew () { return ggc_alloc<T> (); }
9306 /* Optionally scribble all over the bits holding the object, so that
9307 they become (mostly?) uninitialized memory. This is called while
9308 preparing to make the object part of the free list. */
9309 static void poison (T *obj) {
9310 T *p ATTRIBUTE_UNUSED = obj;
9311 T **q ATTRIBUTE_UNUSED = &next (obj);
9313 #ifdef ENABLE_GC_CHECKING
9314 /* Poison the data, to indicate the data is garbage. */
9315 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9316 memset (p, 0xa5, sizeof (*p));
9317 #endif
9318 /* Let valgrind know the object is free. */
9319 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9321 /* Let valgrind know the next portion of the object is available,
9322 but uninitialized. */
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9326 /* Bring an object that underwent at least one lifecycle after anew
9327 and before the most recent free and poison, back to a usable
9328 state, reinitializing whatever is needed for it to be
9329 functionally equivalent to an object just allocated and returned
9330 by anew. This may poison or clear the next field, used by
9331 freelist housekeeping after poison was called. */
9332 static void reinit (T *obj) {
9333 T **q ATTRIBUTE_UNUSED = &next (obj);
9335 #ifdef ENABLE_GC_CHECKING
9336 memset (q, 0xa5, sizeof (*q));
9337 #endif
9338 /* Let valgrind know the entire object is available, but
9339 uninitialized. */
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9343 /* Reference a GTY-deletable pointer that points to the first object
9344 in the free list proper. */
9345 T *&head;
9346 public:
9347 /* Construct a freelist object chaining objects off of HEAD. */
9348 freelist (T *&head) : head(head) {}
9350 /* Add OBJ to the free object list. The former head becomes OBJ's
9351 successor. */
9352 void free (T *obj)
9354 poison (obj);
9355 next (obj) = head;
9356 head = obj;
9359 /* Take an object from the free list, if one is available, or
9360 allocate a new one. Objects taken from the free list should be
9361 regarded as filled with garbage, except for bits that are
9362 configured to be preserved across free and alloc. */
9363 T *alloc ()
9365 if (head)
9367 T *obj = head;
9368 head = next (head);
9369 reinit (obj);
9370 return obj;
9372 else
9373 return anew ();
9377 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9378 want to allocate a TREE_LIST using the usual interface, and ensure
9379 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9380 build_tree_list logic in reinit, so this could go out of sync. */
9381 template <>
9382 inline tree &
9383 freelist<tree_node>::next (tree obj)
9385 return TREE_CHAIN (obj);
9387 template <>
9388 inline tree
9389 freelist<tree_node>::anew ()
9391 return build_tree_list (NULL, NULL);
9393 template <>
9394 inline void
9395 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9397 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9398 tree p ATTRIBUTE_UNUSED = obj;
9399 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9400 tree *q ATTRIBUTE_UNUSED = &next (obj);
9402 #ifdef ENABLE_GC_CHECKING
9403 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9405 /* Poison the data, to indicate the data is garbage. */
9406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9407 memset (p, 0xa5, size);
9408 #endif
9409 /* Let valgrind know the object is free. */
9410 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9411 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9412 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9413 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9415 #ifdef ENABLE_GC_CHECKING
9416 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9417 /* Keep TREE_CHAIN functional. */
9418 TREE_SET_CODE (obj, TREE_LIST);
9419 #else
9420 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9421 #endif
9423 template <>
9424 inline void
9425 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9427 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9429 #ifdef ENABLE_GC_CHECKING
9430 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9431 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9432 memset (obj, 0, sizeof (tree_list));
9433 #endif
9435 /* Let valgrind know the entire object is available, but
9436 uninitialized. */
9437 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9439 #ifdef ENABLE_GC_CHECKING
9440 TREE_SET_CODE (obj, TREE_LIST);
9441 #else
9442 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9443 #endif
9446 /* Point to the first object in the TREE_LIST freelist. */
9447 static GTY((deletable)) tree tree_list_freelist_head;
9448 /* Return the/an actual TREE_LIST freelist. */
9449 static inline freelist<tree_node>
9450 tree_list_freelist ()
9452 return tree_list_freelist_head;
9455 /* Point to the first object in the tinst_level freelist. */
9456 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9457 /* Return the/an actual tinst_level freelist. */
9458 static inline freelist<tinst_level>
9459 tinst_level_freelist ()
9461 return tinst_level_freelist_head;
9464 /* Point to the first object in the pending_template freelist. */
9465 static GTY((deletable)) pending_template *pending_template_freelist_head;
9466 /* Return the/an actual pending_template freelist. */
9467 static inline freelist<pending_template>
9468 pending_template_freelist ()
9470 return pending_template_freelist_head;
9473 /* Build the TREE_LIST object out of a split list, store it
9474 permanently, and return it. */
9475 tree
9476 tinst_level::to_list ()
9478 gcc_assert (split_list_p ());
9479 tree ret = tree_list_freelist ().alloc ();
9480 TREE_PURPOSE (ret) = tldcl;
9481 TREE_VALUE (ret) = targs;
9482 tldcl = ret;
9483 targs = NULL;
9484 gcc_assert (tree_list_p ());
9485 return ret;
9488 const unsigned short tinst_level::refcount_infinity;
9490 /* Increment OBJ's refcount unless it is already infinite. */
9491 static tinst_level *
9492 inc_refcount_use (tinst_level *obj)
9494 if (obj && obj->refcount != tinst_level::refcount_infinity)
9495 ++obj->refcount;
9496 return obj;
9499 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9500 void
9501 tinst_level::free (tinst_level *obj)
9503 if (obj->tree_list_p ())
9504 tree_list_freelist ().free (obj->get_node ());
9505 tinst_level_freelist ().free (obj);
9508 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9509 OBJ's DECL and OBJ, and start over with the tinst_level object that
9510 used to be referenced by OBJ's NEXT. */
9511 static void
9512 dec_refcount_use (tinst_level *obj)
9514 while (obj
9515 && obj->refcount != tinst_level::refcount_infinity
9516 && !--obj->refcount)
9518 tinst_level *next = obj->next;
9519 tinst_level::free (obj);
9520 obj = next;
9524 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9525 and of the former PTR. Omitting the second argument is equivalent
9526 to passing (T*)NULL; this is allowed because passing the
9527 zero-valued integral constant NULL confuses type deduction and/or
9528 overload resolution. */
9529 template <typename T>
9530 static void
9531 set_refcount_ptr (T *& ptr, T *obj = NULL)
9533 T *save = ptr;
9534 ptr = inc_refcount_use (obj);
9535 dec_refcount_use (save);
9538 static void
9539 add_pending_template (tree d)
9541 tree ti = (TYPE_P (d)
9542 ? CLASSTYPE_TEMPLATE_INFO (d)
9543 : DECL_TEMPLATE_INFO (d));
9544 struct pending_template *pt;
9545 int level;
9547 if (TI_PENDING_TEMPLATE_FLAG (ti))
9548 return;
9550 /* We are called both from instantiate_decl, where we've already had a
9551 tinst_level pushed, and instantiate_template, where we haven't.
9552 Compensate. */
9553 gcc_assert (TREE_CODE (d) != TREE_LIST);
9554 level = !current_tinst_level
9555 || current_tinst_level->maybe_get_node () != d;
9557 if (level)
9558 push_tinst_level (d);
9560 pt = pending_template_freelist ().alloc ();
9561 pt->next = NULL;
9562 pt->tinst = NULL;
9563 set_refcount_ptr (pt->tinst, current_tinst_level);
9564 if (last_pending_template)
9565 last_pending_template->next = pt;
9566 else
9567 pending_templates = pt;
9569 last_pending_template = pt;
9571 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9573 if (level)
9574 pop_tinst_level ();
9578 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9579 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9580 documentation for TEMPLATE_ID_EXPR. */
9582 tree
9583 lookup_template_function (tree fns, tree arglist)
9585 if (fns == error_mark_node || arglist == error_mark_node)
9586 return error_mark_node;
9588 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9590 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9592 error ("%q#D is not a function template", fns);
9593 return error_mark_node;
9596 if (BASELINK_P (fns))
9598 fns = copy_node (fns);
9599 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9600 unknown_type_node,
9601 BASELINK_FUNCTIONS (fns),
9602 arglist);
9603 return fns;
9606 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9609 /* Within the scope of a template class S<T>, the name S gets bound
9610 (in build_self_reference) to a TYPE_DECL for the class, not a
9611 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9612 or one of its enclosing classes, and that type is a template,
9613 return the associated TEMPLATE_DECL. Otherwise, the original
9614 DECL is returned.
9616 Also handle the case when DECL is a TREE_LIST of ambiguous
9617 injected-class-names from different bases. */
9619 tree
9620 maybe_get_template_decl_from_type_decl (tree decl)
9622 if (decl == NULL_TREE)
9623 return decl;
9625 /* DR 176: A lookup that finds an injected-class-name (10.2
9626 [class.member.lookup]) can result in an ambiguity in certain cases
9627 (for example, if it is found in more than one base class). If all of
9628 the injected-class-names that are found refer to specializations of
9629 the same class template, and if the name is followed by a
9630 template-argument-list, the reference refers to the class template
9631 itself and not a specialization thereof, and is not ambiguous. */
9632 if (TREE_CODE (decl) == TREE_LIST)
9634 tree t, tmpl = NULL_TREE;
9635 for (t = decl; t; t = TREE_CHAIN (t))
9637 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9638 if (!tmpl)
9639 tmpl = elt;
9640 else if (tmpl != elt)
9641 break;
9643 if (tmpl && t == NULL_TREE)
9644 return tmpl;
9645 else
9646 return decl;
9649 return (decl != NULL_TREE
9650 && DECL_SELF_REFERENCE_P (decl)
9651 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9652 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9655 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9656 parameters, find the desired type.
9658 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9660 IN_DECL, if non-NULL, is the template declaration we are trying to
9661 instantiate.
9663 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9664 the class we are looking up.
9666 Issue error and warning messages under control of COMPLAIN.
9668 If the template class is really a local class in a template
9669 function, then the FUNCTION_CONTEXT is the function in which it is
9670 being instantiated.
9672 ??? Note that this function is currently called *twice* for each
9673 template-id: the first time from the parser, while creating the
9674 incomplete type (finish_template_type), and the second type during the
9675 real instantiation (instantiate_template_class). This is surely something
9676 that we want to avoid. It also causes some problems with argument
9677 coercion (see convert_nontype_argument for more information on this). */
9679 static tree
9680 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9681 int entering_scope, tsubst_flags_t complain)
9683 tree templ = NULL_TREE, parmlist;
9684 tree t;
9685 spec_entry **slot;
9686 spec_entry *entry;
9687 spec_entry elt;
9688 hashval_t hash;
9690 if (identifier_p (d1))
9692 tree value = innermost_non_namespace_value (d1);
9693 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9694 templ = value;
9695 else
9697 if (context)
9698 push_decl_namespace (context);
9699 templ = lookup_name (d1);
9700 templ = maybe_get_template_decl_from_type_decl (templ);
9701 if (context)
9702 pop_decl_namespace ();
9704 if (templ)
9705 context = DECL_CONTEXT (templ);
9707 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9709 tree type = TREE_TYPE (d1);
9711 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9712 an implicit typename for the second A. Deal with it. */
9713 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9714 type = TREE_TYPE (type);
9716 if (CLASSTYPE_TEMPLATE_INFO (type))
9718 templ = CLASSTYPE_TI_TEMPLATE (type);
9719 d1 = DECL_NAME (templ);
9722 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9723 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9725 templ = TYPE_TI_TEMPLATE (d1);
9726 d1 = DECL_NAME (templ);
9728 else if (DECL_TYPE_TEMPLATE_P (d1))
9730 templ = d1;
9731 d1 = DECL_NAME (templ);
9732 context = DECL_CONTEXT (templ);
9734 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9736 templ = d1;
9737 d1 = DECL_NAME (templ);
9740 /* Issue an error message if we didn't find a template. */
9741 if (! templ)
9743 if (complain & tf_error)
9744 error ("%qT is not a template", d1);
9745 return error_mark_node;
9748 if (TREE_CODE (templ) != TEMPLATE_DECL
9749 /* Make sure it's a user visible template, if it was named by
9750 the user. */
9751 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9752 && !PRIMARY_TEMPLATE_P (templ)))
9754 if (complain & tf_error)
9756 error ("non-template type %qT used as a template", d1);
9757 if (in_decl)
9758 error ("for template declaration %q+D", in_decl);
9760 return error_mark_node;
9763 complain &= ~tf_user;
9765 /* An alias that just changes the name of a template is equivalent to the
9766 other template, so if any of the arguments are pack expansions, strip
9767 the alias to avoid problems with a pack expansion passed to a non-pack
9768 alias template parameter (DR 1430). */
9769 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9770 templ = get_underlying_template (templ);
9772 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9774 tree parm;
9775 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9776 if (arglist2 == error_mark_node
9777 || (!uses_template_parms (arglist2)
9778 && check_instantiated_args (templ, arglist2, complain)))
9779 return error_mark_node;
9781 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9782 return parm;
9784 else
9786 tree template_type = TREE_TYPE (templ);
9787 tree gen_tmpl;
9788 tree type_decl;
9789 tree found = NULL_TREE;
9790 int arg_depth;
9791 int parm_depth;
9792 int is_dependent_type;
9793 int use_partial_inst_tmpl = false;
9795 if (template_type == error_mark_node)
9796 /* An error occurred while building the template TEMPL, and a
9797 diagnostic has most certainly been emitted for that
9798 already. Let's propagate that error. */
9799 return error_mark_node;
9801 gen_tmpl = most_general_template (templ);
9802 if (modules_p ())
9803 lazy_load_pendings (gen_tmpl);
9805 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9806 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9807 arg_depth = TMPL_ARGS_DEPTH (arglist);
9809 if (arg_depth == 1 && parm_depth > 1)
9811 /* We've been given an incomplete set of template arguments.
9812 For example, given:
9814 template <class T> struct S1 {
9815 template <class U> struct S2 {};
9816 template <class U> struct S2<U*> {};
9819 we will be called with an ARGLIST of `U*', but the
9820 TEMPLATE will be `template <class T> template
9821 <class U> struct S1<T>::S2'. We must fill in the missing
9822 arguments. */
9823 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9824 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9825 arg_depth = TMPL_ARGS_DEPTH (arglist);
9828 /* Now we should have enough arguments. */
9829 gcc_assert (parm_depth == arg_depth);
9831 /* From here on, we're only interested in the most general
9832 template. */
9834 /* Shortcut looking up the current class scope again. */
9835 if (current_class_type)
9836 if (tree ti = CLASSTYPE_TEMPLATE_INFO (current_class_type))
9837 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
9838 && comp_template_args (arglist, TI_ARGS (ti)))
9839 return current_class_type;
9841 /* Calculate the BOUND_ARGS. These will be the args that are
9842 actually tsubst'd into the definition to create the
9843 instantiation. */
9844 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9845 complain,
9846 /*require_all_args=*/true,
9847 /*use_default_args=*/true);
9849 if (arglist == error_mark_node)
9850 /* We were unable to bind the arguments. */
9851 return error_mark_node;
9853 /* In the scope of a template class, explicit references to the
9854 template class refer to the type of the template, not any
9855 instantiation of it. For example, in:
9857 template <class T> class C { void f(C<T>); }
9859 the `C<T>' is just the same as `C'. Outside of the
9860 class, however, such a reference is an instantiation. */
9861 if (entering_scope
9862 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9863 || currently_open_class (template_type))
9865 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9867 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9868 return template_type;
9871 /* If we already have this specialization, return it. */
9872 elt.tmpl = gen_tmpl;
9873 elt.args = arglist;
9874 elt.spec = NULL_TREE;
9875 hash = spec_hasher::hash (&elt);
9876 entry = type_specializations->find_with_hash (&elt, hash);
9878 if (entry)
9879 return entry->spec;
9881 /* If the template's constraints are not satisfied,
9882 then we cannot form a valid type.
9884 Note that the check is deferred until after the hash
9885 lookup. This prevents redundant checks on previously
9886 instantiated specializations. */
9887 if (flag_concepts
9888 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9889 && !constraints_satisfied_p (gen_tmpl, arglist))
9891 if (complain & tf_error)
9893 auto_diagnostic_group d;
9894 error ("template constraint failure for %qD", gen_tmpl);
9895 diagnose_constraints (input_location, gen_tmpl, arglist);
9897 return error_mark_node;
9900 is_dependent_type = uses_template_parms (arglist);
9902 /* If the deduced arguments are invalid, then the binding
9903 failed. */
9904 if (!is_dependent_type
9905 && check_instantiated_args (gen_tmpl,
9906 INNERMOST_TEMPLATE_ARGS (arglist),
9907 complain))
9908 return error_mark_node;
9910 if (!is_dependent_type
9911 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9912 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9913 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9914 /* This occurs when the user has tried to define a tagged type
9915 in a scope that forbids it. We emitted an error during the
9916 parse. We didn't complete the bail out then, so here we
9917 are. */
9918 return error_mark_node;
9920 context = DECL_CONTEXT (gen_tmpl);
9921 if (context && TYPE_P (context))
9923 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9924 context = complete_type (context);
9926 else
9927 context = tsubst (context, arglist, complain, in_decl);
9929 if (context == error_mark_node)
9930 return error_mark_node;
9932 if (!context)
9933 context = global_namespace;
9935 /* Create the type. */
9936 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9938 /* The user referred to a specialization of an alias
9939 template represented by GEN_TMPL.
9941 [temp.alias]/2 says:
9943 When a template-id refers to the specialization of an
9944 alias template, it is equivalent to the associated
9945 type obtained by substitution of its
9946 template-arguments for the template-parameters in the
9947 type-id of the alias template. */
9949 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9950 /* Note that the call above (by indirectly calling
9951 register_specialization in tsubst_decl) registers the
9952 TYPE_DECL representing the specialization of the alias
9953 template. So next time someone substitutes ARGLIST for
9954 the template parms into the alias template (GEN_TMPL),
9955 she'll get that TYPE_DECL back. */
9957 if (t == error_mark_node)
9958 return t;
9960 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9962 if (!is_dependent_type)
9964 set_current_access_from_decl (TYPE_NAME (template_type));
9965 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9966 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9967 arglist, complain, in_decl),
9968 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9969 arglist, complain, in_decl),
9970 SCOPED_ENUM_P (template_type), NULL);
9972 if (t == error_mark_node)
9973 return t;
9975 else
9977 /* We don't want to call start_enum for this type, since
9978 the values for the enumeration constants may involve
9979 template parameters. And, no one should be interested
9980 in the enumeration constants for such a type. */
9981 t = cxx_make_type (ENUMERAL_TYPE);
9982 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9984 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9985 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9986 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9988 else if (CLASS_TYPE_P (template_type))
9990 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9991 instantiated here. */
9992 gcc_assert (!LAMBDA_TYPE_P (template_type));
9994 t = make_class_type (TREE_CODE (template_type));
9995 CLASSTYPE_DECLARED_CLASS (t)
9996 = CLASSTYPE_DECLARED_CLASS (template_type);
9997 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9999 /* A local class. Make sure the decl gets registered properly. */
10000 if (context == current_function_decl)
10001 if (pushtag (DECL_NAME (gen_tmpl), t)
10002 == error_mark_node)
10003 return error_mark_node;
10005 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10006 /* This instantiation is another name for the primary
10007 template type. Set the TYPE_CANONICAL field
10008 appropriately. */
10009 TYPE_CANONICAL (t) = template_type;
10010 else if (any_template_arguments_need_structural_equality_p (arglist))
10011 /* Some of the template arguments require structural
10012 equality testing, so this template class requires
10013 structural equality testing. */
10014 SET_TYPE_STRUCTURAL_EQUALITY (t);
10016 else
10017 gcc_unreachable ();
10019 /* If we called start_enum or pushtag above, this information
10020 will already be set up. */
10021 type_decl = TYPE_NAME (t);
10022 if (!type_decl)
10024 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10026 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10027 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10028 DECL_SOURCE_LOCATION (type_decl)
10029 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10032 set_instantiating_module (type_decl);
10033 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10034 of export flag. We want to propagate this because it might
10035 be a friend declaration that pushes a new hidden binding. */
10036 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10038 if (CLASS_TYPE_P (template_type))
10040 TREE_PRIVATE (type_decl)
10041 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10042 TREE_PROTECTED (type_decl)
10043 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10044 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10046 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10047 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10051 if (OVERLOAD_TYPE_P (t)
10052 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10054 static const char *tags[] = {"abi_tag", "may_alias"};
10056 for (unsigned ix = 0; ix != 2; ix++)
10058 tree attributes
10059 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10061 if (attributes)
10062 TYPE_ATTRIBUTES (t)
10063 = tree_cons (TREE_PURPOSE (attributes),
10064 TREE_VALUE (attributes),
10065 TYPE_ATTRIBUTES (t));
10069 /* Let's consider the explicit specialization of a member
10070 of a class template specialization that is implicitly instantiated,
10071 e.g.:
10072 template<class T>
10073 struct S
10075 template<class U> struct M {}; //#0
10078 template<>
10079 template<>
10080 struct S<int>::M<char> //#1
10082 int i;
10084 [temp.expl.spec]/4 says this is valid.
10086 In this case, when we write:
10087 S<int>::M<char> m;
10089 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10090 the one of #0.
10092 When we encounter #1, we want to store the partial instantiation
10093 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10095 For all cases other than this "explicit specialization of member of a
10096 class template", we just want to store the most general template into
10097 the CLASSTYPE_TI_TEMPLATE of M.
10099 This case of "explicit specialization of member of a class template"
10100 only happens when:
10101 1/ the enclosing class is an instantiation of, and therefore not
10102 the same as, the context of the most general template, and
10103 2/ we aren't looking at the partial instantiation itself, i.e.
10104 the innermost arguments are not the same as the innermost parms of
10105 the most general template.
10107 So it's only when 1/ and 2/ happens that we want to use the partial
10108 instantiation of the member template in lieu of its most general
10109 template. */
10111 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10112 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10113 /* the enclosing class must be an instantiation... */
10114 && CLASS_TYPE_P (context)
10115 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10117 TREE_VEC_LENGTH (arglist)--;
10118 ++processing_template_decl;
10119 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10120 tree partial_inst_args =
10121 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10122 arglist, complain, NULL_TREE);
10123 --processing_template_decl;
10124 TREE_VEC_LENGTH (arglist)++;
10125 if (partial_inst_args == error_mark_node)
10126 return error_mark_node;
10127 use_partial_inst_tmpl =
10128 /*...and we must not be looking at the partial instantiation
10129 itself. */
10130 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10131 partial_inst_args);
10134 if (!use_partial_inst_tmpl)
10135 /* This case is easy; there are no member templates involved. */
10136 found = gen_tmpl;
10137 else
10139 /* This is a full instantiation of a member template. Find
10140 the partial instantiation of which this is an instance. */
10142 /* Temporarily reduce by one the number of levels in the ARGLIST
10143 so as to avoid comparing the last set of arguments. */
10144 TREE_VEC_LENGTH (arglist)--;
10145 /* We don't use COMPLAIN in the following call because this isn't
10146 the immediate context of deduction. For instance, tf_partial
10147 could be set here as we might be at the beginning of template
10148 argument deduction when any explicitly specified template
10149 arguments are substituted into the function type. tf_partial
10150 could lead into trouble because we wouldn't find the partial
10151 instantiation that might have been created outside tf_partial
10152 context, because the levels of template parameters wouldn't
10153 match, because in a tf_partial context, tsubst doesn't reduce
10154 TEMPLATE_PARM_LEVEL. */
10155 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10156 TREE_VEC_LENGTH (arglist)++;
10157 /* FOUND is either a proper class type, or an alias
10158 template specialization. In the later case, it's a
10159 TYPE_DECL, resulting from the substituting of arguments
10160 for parameters in the TYPE_DECL of the alias template
10161 done earlier. So be careful while getting the template
10162 of FOUND. */
10163 found = (TREE_CODE (found) == TEMPLATE_DECL
10164 ? found
10165 : (TREE_CODE (found) == TYPE_DECL
10166 ? DECL_TI_TEMPLATE (found)
10167 : CLASSTYPE_TI_TEMPLATE (found)));
10169 if (DECL_CLASS_TEMPLATE_P (found)
10170 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10172 /* If this partial instantiation is specialized, we want to
10173 use it for hash table lookup. */
10174 elt.tmpl = found;
10175 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10176 hash = spec_hasher::hash (&elt);
10180 /* Build template info for the new specialization. */
10181 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10183 elt.spec = t;
10184 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10185 gcc_checking_assert (*slot == NULL);
10186 entry = ggc_alloc<spec_entry> ();
10187 *entry = elt;
10188 *slot = entry;
10190 /* Note this use of the partial instantiation so we can check it
10191 later in maybe_process_partial_specialization. */
10192 DECL_TEMPLATE_INSTANTIATIONS (found)
10193 = tree_cons (arglist, t,
10194 DECL_TEMPLATE_INSTANTIATIONS (found));
10196 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10197 && !uses_template_parms (current_nonlambda_scope ())
10198 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10199 /* Now that the type has been registered on the instantiations
10200 list, we set up the enumerators. Because the enumeration
10201 constants may involve the enumeration type itself, we make
10202 sure to register the type first, and then create the
10203 constants. That way, doing tsubst_expr for the enumeration
10204 constants won't result in recursive calls here; we'll find
10205 the instantiation and exit above. */
10206 tsubst_enum (template_type, t, arglist);
10208 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10209 /* If the type makes use of template parameters, the
10210 code that generates debugging information will crash. */
10211 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10213 /* Possibly limit visibility based on template args. */
10214 TREE_PUBLIC (type_decl) = 1;
10215 determine_visibility (type_decl);
10217 inherit_targ_abi_tags (t);
10219 return t;
10223 /* Wrapper for lookup_template_class_1. */
10225 tree
10226 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10227 int entering_scope, tsubst_flags_t complain)
10229 tree ret;
10230 timevar_push (TV_TEMPLATE_INST);
10231 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10232 entering_scope, complain);
10233 timevar_pop (TV_TEMPLATE_INST);
10234 return ret;
10237 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10239 tree
10240 lookup_template_variable (tree templ, tree arglist)
10242 if (flag_concepts && variable_concept_p (templ))
10243 return build_concept_check (templ, arglist, tf_none);
10245 /* The type of the expression is NULL_TREE since the template-id could refer
10246 to an explicit or partial specialization. */
10247 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10250 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10252 tree
10253 finish_template_variable (tree var, tsubst_flags_t complain)
10255 tree templ = TREE_OPERAND (var, 0);
10256 tree arglist = TREE_OPERAND (var, 1);
10258 tree parms = DECL_TEMPLATE_PARMS (templ);
10259 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10260 /*req_all*/true,
10261 /*use_default*/true);
10262 if (arglist == error_mark_node)
10263 return error_mark_node;
10265 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10267 if (complain & tf_error)
10269 auto_diagnostic_group d;
10270 error ("use of invalid variable template %qE", var);
10271 diagnose_constraints (location_of (var), templ, arglist);
10273 return error_mark_node;
10276 return instantiate_template (templ, arglist, complain);
10279 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10280 TARGS template args, and instantiate it if it's not dependent. */
10282 tree
10283 lookup_and_finish_template_variable (tree templ, tree targs,
10284 tsubst_flags_t complain)
10286 templ = lookup_template_variable (templ, targs);
10287 if (!any_dependent_template_arguments_p (targs))
10289 templ = finish_template_variable (templ, complain);
10290 mark_used (templ);
10293 return convert_from_reference (templ);
10296 /* If the set of template parameters PARMS contains a template parameter
10297 at the given LEVEL and INDEX, then return this parameter. Otherwise
10298 return NULL_TREE. */
10300 static tree
10301 corresponding_template_parameter (tree parms, int level, int index)
10303 while (TMPL_PARMS_DEPTH (parms) > level)
10304 parms = TREE_CHAIN (parms);
10306 if (TMPL_PARMS_DEPTH (parms) != level
10307 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10308 return NULL_TREE;
10310 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10311 /* As in template_parm_to_arg. */
10312 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10313 t = TREE_TYPE (t);
10314 else
10315 t = DECL_INITIAL (t);
10317 gcc_assert (TEMPLATE_PARM_P (t));
10318 return t;
10321 /* Return the template parameter from PARMS that positionally corresponds
10322 to the template parameter PARM, or else return NULL_TREE. */
10324 static tree
10325 corresponding_template_parameter (tree parms, tree parm)
10327 int level, index;
10328 template_parm_level_and_index (parm, &level, &index);
10329 return corresponding_template_parameter (parms, level, index);
10333 struct pair_fn_data
10335 tree_fn_t fn;
10336 tree_fn_t any_fn;
10337 void *data;
10338 /* True when we should also visit template parameters that occur in
10339 non-deduced contexts. */
10340 bool include_nondeduced_p;
10341 hash_set<tree> *visited;
10344 /* Called from for_each_template_parm via walk_tree. */
10346 static tree
10347 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10349 tree t = *tp;
10350 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10351 tree_fn_t fn = pfd->fn;
10352 void *data = pfd->data;
10353 tree result = NULL_TREE;
10355 #define WALK_SUBTREE(NODE) \
10356 do \
10358 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10359 pfd->include_nondeduced_p, \
10360 pfd->any_fn); \
10361 if (result) goto out; \
10363 while (0)
10365 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10366 return t;
10368 if (TYPE_P (t)
10369 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10370 WALK_SUBTREE (TYPE_CONTEXT (t));
10372 switch (TREE_CODE (t))
10374 case RECORD_TYPE:
10375 if (TYPE_PTRMEMFUNC_P (t))
10376 break;
10377 /* Fall through. */
10379 case UNION_TYPE:
10380 case ENUMERAL_TYPE:
10381 if (!TYPE_TEMPLATE_INFO (t))
10382 *walk_subtrees = 0;
10383 else
10384 WALK_SUBTREE (TYPE_TI_ARGS (t));
10385 break;
10387 case INTEGER_TYPE:
10388 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10389 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10390 break;
10392 case METHOD_TYPE:
10393 /* Since we're not going to walk subtrees, we have to do this
10394 explicitly here. */
10395 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10396 /* Fall through. */
10398 case FUNCTION_TYPE:
10399 /* Check the return type. */
10400 WALK_SUBTREE (TREE_TYPE (t));
10402 /* Check the parameter types. Since default arguments are not
10403 instantiated until they are needed, the TYPE_ARG_TYPES may
10404 contain expressions that involve template parameters. But,
10405 no-one should be looking at them yet. And, once they're
10406 instantiated, they don't contain template parameters, so
10407 there's no point in looking at them then, either. */
10409 tree parm;
10411 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10412 WALK_SUBTREE (TREE_VALUE (parm));
10414 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10415 want walk_tree walking into them itself. */
10416 *walk_subtrees = 0;
10419 if (flag_noexcept_type)
10421 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10422 if (spec)
10423 WALK_SUBTREE (TREE_PURPOSE (spec));
10425 break;
10427 case TYPEOF_TYPE:
10428 case DECLTYPE_TYPE:
10429 case UNDERLYING_TYPE:
10430 if (pfd->include_nondeduced_p
10431 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10432 pfd->visited,
10433 pfd->include_nondeduced_p,
10434 pfd->any_fn))
10435 return error_mark_node;
10436 *walk_subtrees = false;
10437 break;
10439 case FUNCTION_DECL:
10440 case VAR_DECL:
10441 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10442 WALK_SUBTREE (DECL_TI_ARGS (t));
10443 /* Fall through. */
10445 case PARM_DECL:
10446 case CONST_DECL:
10447 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10448 WALK_SUBTREE (DECL_INITIAL (t));
10449 if (DECL_CONTEXT (t)
10450 && pfd->include_nondeduced_p)
10451 WALK_SUBTREE (DECL_CONTEXT (t));
10452 break;
10454 case BOUND_TEMPLATE_TEMPLATE_PARM:
10455 /* Record template parameters such as `T' inside `TT<T>'. */
10456 WALK_SUBTREE (TYPE_TI_ARGS (t));
10457 /* Fall through. */
10459 case TEMPLATE_TEMPLATE_PARM:
10460 case TEMPLATE_TYPE_PARM:
10461 case TEMPLATE_PARM_INDEX:
10462 if (fn && (*fn)(t, data))
10463 return t;
10464 else if (!fn)
10465 return t;
10466 break;
10468 case TEMPLATE_DECL:
10469 /* A template template parameter is encountered. */
10470 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10471 WALK_SUBTREE (TREE_TYPE (t));
10473 /* Already substituted template template parameter */
10474 *walk_subtrees = 0;
10475 break;
10477 case TYPENAME_TYPE:
10478 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10479 partial instantiation. */
10480 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10481 *walk_subtrees = 0;
10482 break;
10484 case CONSTRUCTOR:
10485 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10486 && pfd->include_nondeduced_p)
10487 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10488 break;
10490 case INDIRECT_REF:
10491 case COMPONENT_REF:
10492 /* If there's no type, then this thing must be some expression
10493 involving template parameters. */
10494 if (!fn && !TREE_TYPE (t))
10495 return error_mark_node;
10496 break;
10498 case TRAIT_EXPR:
10499 case PLUS_EXPR:
10500 case MULT_EXPR:
10501 case SCOPE_REF:
10502 /* These are non-deduced contexts. */
10503 if (!pfd->include_nondeduced_p)
10504 *walk_subtrees = 0;
10505 break;
10507 case MODOP_EXPR:
10508 case CAST_EXPR:
10509 case IMPLICIT_CONV_EXPR:
10510 case REINTERPRET_CAST_EXPR:
10511 case CONST_CAST_EXPR:
10512 case STATIC_CAST_EXPR:
10513 case DYNAMIC_CAST_EXPR:
10514 case ARROW_EXPR:
10515 case DOTSTAR_EXPR:
10516 case TYPEID_EXPR:
10517 case PSEUDO_DTOR_EXPR:
10518 if (!fn)
10519 return error_mark_node;
10520 break;
10522 case REQUIRES_EXPR:
10524 if (!fn)
10525 return error_mark_node;
10527 /* Recursively walk the type of each constraint variable. */
10528 tree p = TREE_OPERAND (t, 0);
10529 while (p)
10531 WALK_SUBTREE (TREE_TYPE (p));
10532 p = TREE_CHAIN (p);
10535 break;
10537 default:
10538 break;
10541 #undef WALK_SUBTREE
10543 /* We didn't find any template parameters we liked. */
10544 out:
10545 return result;
10548 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10549 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10550 call FN with the parameter and the DATA.
10551 If FN returns nonzero, the iteration is terminated, and
10552 for_each_template_parm returns 1. Otherwise, the iteration
10553 continues. If FN never returns a nonzero value, the value
10554 returned by for_each_template_parm is 0. If FN is NULL, it is
10555 considered to be the function which always returns 1.
10557 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10558 parameters that occur in non-deduced contexts. When false, only
10559 visits those template parameters that can be deduced. */
10561 static tree
10562 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10563 hash_set<tree> *visited,
10564 bool include_nondeduced_p,
10565 tree_fn_t any_fn)
10567 struct pair_fn_data pfd;
10568 tree result;
10570 /* Set up. */
10571 pfd.fn = fn;
10572 pfd.any_fn = any_fn;
10573 pfd.data = data;
10574 pfd.include_nondeduced_p = include_nondeduced_p;
10576 /* Walk the tree. (Conceptually, we would like to walk without
10577 duplicates, but for_each_template_parm_r recursively calls
10578 for_each_template_parm, so we would need to reorganize a fair
10579 bit to use walk_tree_without_duplicates, so we keep our own
10580 visited list.) */
10581 if (visited)
10582 pfd.visited = visited;
10583 else
10584 pfd.visited = new hash_set<tree>;
10585 result = cp_walk_tree (&t,
10586 for_each_template_parm_r,
10587 &pfd,
10588 pfd.visited);
10590 /* Clean up. */
10591 if (!visited)
10593 delete pfd.visited;
10594 pfd.visited = 0;
10597 return result;
10600 struct find_template_parameter_info
10602 explicit find_template_parameter_info (tree ctx_parms)
10603 : parm_list (NULL_TREE),
10604 ctx_parms (ctx_parms),
10605 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10608 hash_set<tree> visited;
10609 hash_set<tree> parms;
10610 tree parm_list;
10611 tree ctx_parms;
10612 int max_depth;
10615 /* Appends the declaration of T to the list in DATA. */
10617 static int
10618 keep_template_parm (tree t, void* data)
10620 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10622 /* Template parameters declared within the expression are not part of
10623 the parameter mapping. For example, in this concept:
10625 template<typename T>
10626 concept C = requires { <expr> } -> same_as<int>;
10628 the return specifier same_as<int> declares a new decltype parameter
10629 that must not be part of the parameter mapping. The same is true
10630 for generic lambda parameters, lambda template parameters, etc. */
10631 int level;
10632 int index;
10633 template_parm_level_and_index (t, &level, &index);
10634 if (level > ftpi->max_depth)
10635 return 0;
10637 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10638 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10639 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10640 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10642 /* This template parameter might be an argument to a cached dependent
10643 specalization that was formed earlier inside some other template, in
10644 which case the parameter is not among the ones that are in-scope.
10645 Look in CTX_PARMS to find the corresponding in-scope template
10646 parameter, and use it instead. */
10647 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10648 t = in_scope;
10650 /* Arguments like const T yield parameters like const T. This means that
10651 a template-id like X<T, const T> would yield two distinct parameters:
10652 T and const T. Adjust types to their unqualified versions. */
10653 if (TYPE_P (t))
10654 t = TYPE_MAIN_VARIANT (t);
10655 if (!ftpi->parms.add (t))
10656 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10658 /* Verify the parameter we found has a valid index. */
10659 if (flag_checking)
10661 tree parms = ftpi->ctx_parms;
10662 while (TMPL_PARMS_DEPTH (parms) > level)
10663 parms = TREE_CHAIN (parms);
10664 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10665 gcc_assert (index < len);
10668 return 0;
10671 /* Ensure that we recursively examine certain terms that are not normally
10672 visited in for_each_template_parm_r. */
10674 static int
10675 any_template_parm_r (tree t, void *data)
10677 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10679 #define WALK_SUBTREE(NODE) \
10680 do \
10682 for_each_template_parm (NODE, keep_template_parm, data, \
10683 &ftpi->visited, true, \
10684 any_template_parm_r); \
10686 while (0)
10688 /* A mention of a member alias/typedef is a use of all of its template
10689 arguments, including those from the enclosing class, so we don't use
10690 alias_template_specialization_p here. */
10691 if (TYPE_P (t) && typedef_variant_p (t))
10692 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10693 WALK_SUBTREE (TI_ARGS (tinfo));
10695 switch (TREE_CODE (t))
10697 case TEMPLATE_TYPE_PARM:
10698 /* Type constraints of a placeholder type may contain parameters. */
10699 if (is_auto (t))
10700 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10701 WALK_SUBTREE (constr);
10702 break;
10704 case TEMPLATE_ID_EXPR:
10705 /* Search through references to variable templates. */
10706 WALK_SUBTREE (TREE_OPERAND (t, 0));
10707 WALK_SUBTREE (TREE_OPERAND (t, 1));
10708 break;
10710 case TEMPLATE_PARM_INDEX:
10711 case PARM_DECL:
10712 /* A parameter or constraint variable may also depend on a template
10713 parameter without explicitly naming it. */
10714 WALK_SUBTREE (TREE_TYPE (t));
10715 break;
10717 case TEMPLATE_DECL:
10718 /* If T is a member template that shares template parameters with
10719 ctx_parms, we need to mark all those parameters for mapping.
10720 To that end, it should suffice to just walk the DECL_CONTEXT of
10721 the template (assuming the template is not overly general). */
10722 WALK_SUBTREE (DECL_CONTEXT (t));
10723 break;
10725 case LAMBDA_EXPR:
10727 /* Look in the parms and body. */
10728 tree fn = lambda_function (t);
10729 WALK_SUBTREE (TREE_TYPE (fn));
10730 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10732 break;
10734 case IDENTIFIER_NODE:
10735 if (IDENTIFIER_CONV_OP_P (t))
10736 /* The conversion-type-id of a conversion operator may be dependent. */
10737 WALK_SUBTREE (TREE_TYPE (t));
10738 break;
10740 default:
10741 break;
10744 /* Keep walking. */
10745 return 0;
10748 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10749 are the template parameters in scope. */
10751 tree
10752 find_template_parameters (tree t, tree ctx_parms)
10754 if (!ctx_parms)
10755 return NULL_TREE;
10757 find_template_parameter_info ftpi (ctx_parms);
10758 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10759 /*include_nondeduced*/true, any_template_parm_r);
10760 return ftpi.parm_list;
10763 /* Returns true if T depends on any template parameter. */
10766 uses_template_parms (tree t)
10768 if (t == NULL_TREE)
10769 return false;
10771 bool dependent_p;
10772 int saved_processing_template_decl;
10774 saved_processing_template_decl = processing_template_decl;
10775 if (!saved_processing_template_decl)
10776 processing_template_decl = 1;
10777 if (TYPE_P (t))
10778 dependent_p = dependent_type_p (t);
10779 else if (TREE_CODE (t) == TREE_VEC)
10780 dependent_p = any_dependent_template_arguments_p (t);
10781 else if (TREE_CODE (t) == TREE_LIST)
10782 dependent_p = (uses_template_parms (TREE_VALUE (t))
10783 || uses_template_parms (TREE_CHAIN (t)));
10784 else if (TREE_CODE (t) == TYPE_DECL)
10785 dependent_p = dependent_type_p (TREE_TYPE (t));
10786 else if (t == error_mark_node)
10787 dependent_p = false;
10788 else
10789 dependent_p = instantiation_dependent_expression_p (t);
10791 processing_template_decl = saved_processing_template_decl;
10793 return dependent_p;
10796 /* Returns true iff we're processing an incompletely instantiated function
10797 template. Useful instead of processing_template_decl because the latter
10798 is set to 0 during instantiate_non_dependent_expr. */
10800 bool
10801 in_template_function (void)
10803 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10804 the latter might get set for e.g. access checking during satisfaction. */
10805 tree fn = cfun ? cfun->decl : NULL_TREE;
10806 bool ret;
10807 ++processing_template_decl;
10808 ret = (fn && DECL_LANG_SPECIFIC (fn)
10809 && DECL_TEMPLATE_INFO (fn)
10810 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10811 --processing_template_decl;
10812 return ret;
10815 /* Returns true if T depends on any template parameter with level LEVEL. */
10817 bool
10818 uses_template_parms_level (tree t, int level)
10820 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10821 /*include_nondeduced_p=*/true);
10824 /* Returns true if the signature of DECL depends on any template parameter from
10825 its enclosing class. */
10827 bool
10828 uses_outer_template_parms (tree decl)
10830 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10831 if (depth == 0)
10832 return false;
10833 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10834 &depth, NULL, /*include_nondeduced_p=*/true))
10835 return true;
10836 if (PRIMARY_TEMPLATE_P (decl)
10837 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
10839 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
10840 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
10842 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
10843 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
10844 if (TREE_CODE (parm) == PARM_DECL
10845 && for_each_template_parm (TREE_TYPE (parm),
10846 template_parm_outer_level,
10847 &depth, NULL, /*nondeduced*/true))
10848 return true;
10849 if (TREE_CODE (parm) == TEMPLATE_DECL
10850 && uses_outer_template_parms (parm))
10851 return true;
10852 if (defarg
10853 && for_each_template_parm (defarg, template_parm_outer_level,
10854 &depth, NULL, /*nondeduced*/true))
10855 return true;
10858 tree ci = get_constraints (decl);
10859 if (ci)
10860 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10861 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10862 &depth, NULL, /*nondeduced*/true))
10863 return true;
10864 return false;
10867 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10868 ill-formed translation unit, i.e. a variable or function that isn't
10869 usable in a constant expression. */
10871 static inline bool
10872 neglectable_inst_p (tree d)
10874 return (d && DECL_P (d)
10875 && !undeduced_auto_decl (d)
10876 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10877 : decl_maybe_constant_var_p (d)));
10880 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10881 neglectable and instantiated from within an erroneous instantiation. */
10883 static bool
10884 limit_bad_template_recursion (tree decl)
10886 struct tinst_level *lev = current_tinst_level;
10887 int errs = errorcount + sorrycount;
10888 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10889 return false;
10891 for (; lev; lev = lev->next)
10892 if (neglectable_inst_p (lev->maybe_get_node ()))
10893 break;
10895 return (lev && errs > lev->errors);
10898 static int tinst_depth;
10899 extern int max_tinst_depth;
10900 int depth_reached;
10902 static GTY(()) struct tinst_level *last_error_tinst_level;
10904 /* We're starting to instantiate D; record the template instantiation context
10905 at LOC for diagnostics and to restore it later. */
10907 bool
10908 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10910 struct tinst_level *new_level;
10912 if (tinst_depth >= max_tinst_depth)
10914 /* Tell error.c not to try to instantiate any templates. */
10915 at_eof = 2;
10916 fatal_error (input_location,
10917 "template instantiation depth exceeds maximum of %d"
10918 " (use %<-ftemplate-depth=%> to increase the maximum)",
10919 max_tinst_depth);
10920 return false;
10923 /* If the current instantiation caused problems, don't let it instantiate
10924 anything else. Do allow deduction substitution and decls usable in
10925 constant expressions. */
10926 if (!targs && limit_bad_template_recursion (tldcl))
10928 /* Avoid no_linkage_errors and unused function (and all other)
10929 warnings for this decl. */
10930 suppress_warning (tldcl);
10931 return false;
10934 /* When not -quiet, dump template instantiations other than functions, since
10935 announce_function will take care of those. */
10936 if (!quiet_flag && !targs
10937 && TREE_CODE (tldcl) != TREE_LIST
10938 && TREE_CODE (tldcl) != FUNCTION_DECL)
10939 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10941 new_level = tinst_level_freelist ().alloc ();
10942 new_level->tldcl = tldcl;
10943 new_level->targs = targs;
10944 new_level->locus = loc;
10945 new_level->errors = errorcount + sorrycount;
10946 new_level->next = NULL;
10947 new_level->refcount = 0;
10948 new_level->path = new_level->visible = nullptr;
10949 set_refcount_ptr (new_level->next, current_tinst_level);
10950 set_refcount_ptr (current_tinst_level, new_level);
10952 ++tinst_depth;
10953 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10954 depth_reached = tinst_depth;
10956 return true;
10959 /* We're starting substitution of TMPL<ARGS>; record the template
10960 substitution context for diagnostics and to restore it later. */
10962 bool
10963 push_tinst_level (tree tmpl, tree args)
10965 return push_tinst_level_loc (tmpl, args, input_location);
10968 /* We're starting to instantiate D; record INPUT_LOCATION and the
10969 template instantiation context for diagnostics and to restore it
10970 later. */
10972 bool
10973 push_tinst_level (tree d)
10975 return push_tinst_level_loc (d, input_location);
10978 /* Likewise, but record LOC as the program location. */
10980 bool
10981 push_tinst_level_loc (tree d, location_t loc)
10983 gcc_assert (TREE_CODE (d) != TREE_LIST);
10984 return push_tinst_level_loc (d, NULL, loc);
10987 /* We're done instantiating this template; return to the instantiation
10988 context. */
10990 void
10991 pop_tinst_level (void)
10993 /* Restore the filename and line number stashed away when we started
10994 this instantiation. */
10995 input_location = current_tinst_level->locus;
10996 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10997 --tinst_depth;
11000 /* We're instantiating a deferred template; restore the template
11001 instantiation context in which the instantiation was requested, which
11002 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11004 static tree
11005 reopen_tinst_level (struct tinst_level *level)
11007 struct tinst_level *t;
11009 tinst_depth = 0;
11010 for (t = level; t; t = t->next)
11011 ++tinst_depth;
11013 set_refcount_ptr (current_tinst_level, level);
11014 pop_tinst_level ();
11015 if (current_tinst_level)
11016 current_tinst_level->errors = errorcount+sorrycount;
11017 return level->maybe_get_node ();
11020 /* Returns the TINST_LEVEL which gives the original instantiation
11021 context. */
11023 struct tinst_level *
11024 outermost_tinst_level (void)
11026 struct tinst_level *level = current_tinst_level;
11027 if (level)
11028 while (level->next)
11029 level = level->next;
11030 return level;
11033 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11034 vector of template arguments, as for tsubst.
11036 Returns an appropriate tsubst'd friend declaration. */
11038 static tree
11039 tsubst_friend_function (tree decl, tree args)
11041 tree new_friend;
11043 if (TREE_CODE (decl) == FUNCTION_DECL
11044 && DECL_TEMPLATE_INSTANTIATION (decl)
11045 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11046 /* This was a friend declared with an explicit template
11047 argument list, e.g.:
11049 friend void f<>(T);
11051 to indicate that f was a template instantiation, not a new
11052 function declaration. Now, we have to figure out what
11053 instantiation of what template. */
11055 tree template_id, arglist, fns;
11056 tree new_args;
11057 tree tmpl;
11058 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11060 /* Friend functions are looked up in the containing namespace scope.
11061 We must enter that scope, to avoid finding member functions of the
11062 current class with same name. */
11063 push_nested_namespace (ns);
11064 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11065 tf_warning_or_error, NULL_TREE,
11066 /*integral_constant_expression_p=*/false);
11067 pop_nested_namespace (ns);
11068 arglist = tsubst (DECL_TI_ARGS (decl), args,
11069 tf_warning_or_error, NULL_TREE);
11070 template_id = lookup_template_function (fns, arglist);
11072 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11073 tmpl = determine_specialization (template_id, new_friend,
11074 &new_args,
11075 /*need_member_template=*/0,
11076 TREE_VEC_LENGTH (args),
11077 tsk_none);
11078 return instantiate_template (tmpl, new_args, tf_error);
11081 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11082 if (new_friend == error_mark_node)
11083 return error_mark_node;
11085 /* The NEW_FRIEND will look like an instantiation, to the
11086 compiler, but is not an instantiation from the point of view of
11087 the language. For example, we might have had:
11089 template <class T> struct S {
11090 template <class U> friend void f(T, U);
11093 Then, in S<int>, template <class U> void f(int, U) is not an
11094 instantiation of anything. */
11096 DECL_USE_TEMPLATE (new_friend) = 0;
11097 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11099 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11100 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11101 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11102 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11104 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11105 match in decls_match. */
11106 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11107 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11108 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11109 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11112 /* The mangled name for the NEW_FRIEND is incorrect. The function
11113 is not a template instantiation and should not be mangled like
11114 one. Therefore, we forget the mangling here; we'll recompute it
11115 later if we need it. */
11116 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11118 SET_DECL_RTL (new_friend, NULL);
11119 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11122 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11124 tree old_decl;
11125 tree ns;
11127 /* We must save some information from NEW_FRIEND before calling
11128 duplicate decls since that function will free NEW_FRIEND if
11129 possible. */
11130 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11131 tree new_friend_result_template_info = NULL_TREE;
11132 bool new_friend_is_defn =
11133 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11134 (template_for_substitution (new_friend)))
11135 != NULL_TREE);
11136 tree not_tmpl = new_friend;
11138 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11140 /* This declaration is a `primary' template. */
11141 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11143 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11144 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11147 /* Inside pushdecl_namespace_level, we will push into the
11148 current namespace. However, the friend function should go
11149 into the namespace of the template. */
11150 ns = decl_namespace_context (new_friend);
11151 push_nested_namespace (ns);
11152 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11153 pop_nested_namespace (ns);
11155 if (old_decl == error_mark_node)
11156 return error_mark_node;
11158 if (old_decl != new_friend)
11160 /* This new friend declaration matched an existing
11161 declaration. For example, given:
11163 template <class T> void f(T);
11164 template <class U> class C {
11165 template <class T> friend void f(T) {}
11168 the friend declaration actually provides the definition
11169 of `f', once C has been instantiated for some type. So,
11170 old_decl will be the out-of-class template declaration,
11171 while new_friend is the in-class definition.
11173 But, if `f' was called before this point, the
11174 instantiation of `f' will have DECL_TI_ARGS corresponding
11175 to `T' but not to `U', references to which might appear
11176 in the definition of `f'. Previously, the most general
11177 template for an instantiation of `f' was the out-of-class
11178 version; now it is the in-class version. Therefore, we
11179 run through all specialization of `f', adding to their
11180 DECL_TI_ARGS appropriately. In particular, they need a
11181 new set of outer arguments, corresponding to the
11182 arguments for this class instantiation.
11184 The same situation can arise with something like this:
11186 friend void f(int);
11187 template <class T> class C {
11188 friend void f(T) {}
11191 when `C<int>' is instantiated. Now, `f(int)' is defined
11192 in the class. */
11194 if (!new_friend_is_defn)
11195 /* On the other hand, if the in-class declaration does
11196 *not* provide a definition, then we don't want to alter
11197 existing definitions. We can just leave everything
11198 alone. */
11200 else
11202 tree new_template = TI_TEMPLATE (new_friend_template_info);
11203 tree new_args = TI_ARGS (new_friend_template_info);
11205 /* Overwrite whatever template info was there before, if
11206 any, with the new template information pertaining to
11207 the declaration. */
11208 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11210 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11212 /* We should have called reregister_specialization in
11213 duplicate_decls. */
11214 gcc_assert (retrieve_specialization (new_template,
11215 new_args, 0)
11216 == old_decl);
11218 /* Instantiate it if the global has already been used. */
11219 if (DECL_ODR_USED (old_decl))
11220 instantiate_decl (old_decl, /*defer_ok=*/true,
11221 /*expl_inst_class_mem_p=*/false);
11223 else
11225 tree t;
11227 /* Indicate that the old function template is a partial
11228 instantiation. */
11229 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11230 = new_friend_result_template_info;
11232 gcc_assert (new_template
11233 == most_general_template (new_template));
11234 gcc_assert (new_template != old_decl);
11236 /* Reassign any specializations already in the hash table
11237 to the new more general template, and add the
11238 additional template args. */
11239 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11240 t != NULL_TREE;
11241 t = TREE_CHAIN (t))
11243 tree spec = TREE_VALUE (t);
11244 spec_entry elt;
11246 elt.tmpl = old_decl;
11247 elt.args = DECL_TI_ARGS (spec);
11248 elt.spec = NULL_TREE;
11250 decl_specializations->remove_elt (&elt);
11252 DECL_TI_ARGS (spec)
11253 = add_outermost_template_args (new_args,
11254 DECL_TI_ARGS (spec));
11256 register_specialization
11257 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11260 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11264 /* The information from NEW_FRIEND has been merged into OLD_DECL
11265 by duplicate_decls. */
11266 new_friend = old_decl;
11269 else
11271 tree context = DECL_CONTEXT (new_friend);
11272 bool dependent_p;
11274 /* In the code
11275 template <class T> class C {
11276 template <class U> friend void C1<U>::f (); // case 1
11277 friend void C2<T>::f (); // case 2
11279 we only need to make sure CONTEXT is a complete type for
11280 case 2. To distinguish between the two cases, we note that
11281 CONTEXT of case 1 remains dependent type after tsubst while
11282 this isn't true for case 2. */
11283 ++processing_template_decl;
11284 dependent_p = dependent_type_p (context);
11285 --processing_template_decl;
11287 if (!dependent_p
11288 && !complete_type_or_else (context, NULL_TREE))
11289 return error_mark_node;
11291 if (COMPLETE_TYPE_P (context))
11293 tree fn = new_friend;
11294 /* do_friend adds the TEMPLATE_DECL for any member friend
11295 template even if it isn't a member template, i.e.
11296 template <class T> friend A<T>::f();
11297 Look through it in that case. */
11298 if (TREE_CODE (fn) == TEMPLATE_DECL
11299 && !PRIMARY_TEMPLATE_P (fn))
11300 fn = DECL_TEMPLATE_RESULT (fn);
11301 /* Check to see that the declaration is really present, and,
11302 possibly obtain an improved declaration. */
11303 fn = check_classfn (context, fn, NULL_TREE);
11305 if (fn)
11306 new_friend = fn;
11310 return new_friend;
11313 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11314 template arguments, as for tsubst.
11316 Returns an appropriate tsubst'd friend type or error_mark_node on
11317 failure. */
11319 static tree
11320 tsubst_friend_class (tree friend_tmpl, tree args)
11322 tree tmpl;
11324 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11326 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11327 return TREE_TYPE (tmpl);
11330 tree context = CP_DECL_CONTEXT (friend_tmpl);
11331 if (TREE_CODE (context) == NAMESPACE_DECL)
11332 push_nested_namespace (context);
11333 else
11335 context = tsubst (context, args, tf_error, NULL_TREE);
11336 push_nested_class (context);
11339 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11340 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11342 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11344 /* The friend template has already been declared. Just
11345 check to see that the declarations match, and install any new
11346 default parameters. We must tsubst the default parameters,
11347 of course. We only need the innermost template parameters
11348 because that is all that redeclare_class_template will look
11349 at. */
11350 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11351 > TMPL_ARGS_DEPTH (args))
11353 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11354 args, tf_warning_or_error);
11355 location_t saved_input_location = input_location;
11356 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11357 tree cons = get_constraints (tmpl);
11358 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11359 input_location = saved_input_location;
11362 else
11364 /* The friend template has not already been declared. In this
11365 case, the instantiation of the template class will cause the
11366 injection of this template into the namespace scope. */
11367 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11369 if (tmpl != error_mark_node)
11371 /* The new TMPL is not an instantiation of anything, so we
11372 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11373 for the new type because that is supposed to be the
11374 corresponding template decl, i.e., TMPL. */
11375 DECL_USE_TEMPLATE (tmpl) = 0;
11376 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11377 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11378 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11379 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11381 /* Substitute into and set the constraints on the new declaration. */
11382 if (tree ci = get_constraints (friend_tmpl))
11384 ++processing_template_decl;
11385 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11386 DECL_FRIEND_CONTEXT (friend_tmpl));
11387 --processing_template_decl;
11388 set_constraints (tmpl, ci);
11391 /* Inject this template into the enclosing namspace scope. */
11392 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11396 if (TREE_CODE (context) == NAMESPACE_DECL)
11397 pop_nested_namespace (context);
11398 else
11399 pop_nested_class ();
11401 return TREE_TYPE (tmpl);
11404 /* Returns zero if TYPE cannot be completed later due to circularity.
11405 Otherwise returns one. */
11407 static int
11408 can_complete_type_without_circularity (tree type)
11410 if (type == NULL_TREE || type == error_mark_node)
11411 return 0;
11412 else if (COMPLETE_TYPE_P (type))
11413 return 1;
11414 else if (TREE_CODE (type) == ARRAY_TYPE)
11415 return can_complete_type_without_circularity (TREE_TYPE (type));
11416 else if (CLASS_TYPE_P (type)
11417 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11418 return 0;
11419 else
11420 return 1;
11423 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11424 tsubst_flags_t, tree);
11426 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11427 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11429 static tree
11430 tsubst_attribute (tree t, tree *decl_p, tree args,
11431 tsubst_flags_t complain, tree in_decl)
11433 gcc_assert (ATTR_IS_DEPENDENT (t));
11435 tree val = TREE_VALUE (t);
11436 if (val == NULL_TREE)
11437 /* Nothing to do. */;
11438 else if ((flag_openmp || flag_openmp_simd)
11439 && is_attribute_p ("omp declare simd",
11440 get_attribute_name (t)))
11442 tree clauses = TREE_VALUE (val);
11443 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11444 complain, in_decl);
11445 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11446 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11447 tree parms = DECL_ARGUMENTS (*decl_p);
11448 clauses
11449 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11450 if (clauses)
11451 val = build_tree_list (NULL_TREE, clauses);
11452 else
11453 val = NULL_TREE;
11455 else if (flag_openmp
11456 && is_attribute_p ("omp declare variant base",
11457 get_attribute_name (t)))
11459 ++cp_unevaluated_operand;
11460 tree varid
11461 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11462 in_decl, /*integral_constant_expression_p=*/false);
11463 --cp_unevaluated_operand;
11464 tree chain = TREE_CHAIN (val);
11465 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11466 tree ctx = copy_list (TREE_VALUE (val));
11467 tree simd = get_identifier ("simd");
11468 tree score = get_identifier (" score");
11469 tree condition = get_identifier ("condition");
11470 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11472 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11473 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11474 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11476 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11478 tree clauses = TREE_VALUE (t2);
11479 clauses = tsubst_omp_clauses (clauses,
11480 C_ORT_OMP_DECLARE_SIMD, args,
11481 complain, in_decl);
11482 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11483 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11484 TREE_VALUE (t2) = clauses;
11486 else
11488 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11489 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11490 if (TREE_VALUE (t3))
11492 bool allow_string
11493 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11494 && TREE_PURPOSE (t3) != score);
11495 tree v = TREE_VALUE (t3);
11496 if (TREE_CODE (v) == STRING_CST && allow_string)
11497 continue;
11498 v = tsubst_expr (v, args, complain, in_decl, true);
11499 v = fold_non_dependent_expr (v);
11500 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11501 || (TREE_PURPOSE (t3) == score
11502 ? TREE_CODE (v) != INTEGER_CST
11503 : !tree_fits_shwi_p (v)))
11505 location_t loc
11506 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11507 match_loc);
11508 if (TREE_PURPOSE (t3) == score)
11509 error_at (loc, "score argument must be "
11510 "constant integer expression");
11511 else if (allow_string)
11512 error_at (loc, "property must be constant "
11513 "integer expression or string "
11514 "literal");
11515 else
11516 error_at (loc, "property must be constant "
11517 "integer expression");
11518 return NULL_TREE;
11520 else if (TREE_PURPOSE (t3) == score
11521 && tree_int_cst_sgn (v) < 0)
11523 location_t loc
11524 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11525 match_loc);
11526 error_at (loc, "score argument must be "
11527 "non-negative");
11528 return NULL_TREE;
11530 TREE_VALUE (t3) = v;
11535 val = tree_cons (varid, ctx, chain);
11537 /* If the first attribute argument is an identifier, don't
11538 pass it through tsubst. Attributes like mode, format,
11539 cleanup and several target specific attributes expect it
11540 unmodified. */
11541 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11543 tree chain
11544 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11545 /*integral_constant_expression_p=*/false);
11546 if (chain != TREE_CHAIN (val))
11547 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11549 else if (PACK_EXPANSION_P (val))
11551 /* An attribute pack expansion. */
11552 tree purp = TREE_PURPOSE (t);
11553 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11554 if (pack == error_mark_node)
11555 return error_mark_node;
11556 int len = TREE_VEC_LENGTH (pack);
11557 tree list = NULL_TREE;
11558 tree *q = &list;
11559 for (int i = 0; i < len; ++i)
11561 tree elt = TREE_VEC_ELT (pack, i);
11562 *q = build_tree_list (purp, elt);
11563 q = &TREE_CHAIN (*q);
11565 return list;
11567 else
11568 val = tsubst_expr (val, args, complain, in_decl,
11569 /*integral_constant_expression_p=*/false);
11571 if (val == error_mark_node)
11572 return error_mark_node;
11573 if (val != TREE_VALUE (t))
11574 return build_tree_list (TREE_PURPOSE (t), val);
11575 return t;
11578 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11579 unchanged or a new TREE_LIST chain. */
11581 static tree
11582 tsubst_attributes (tree attributes, tree args,
11583 tsubst_flags_t complain, tree in_decl)
11585 tree last_dep = NULL_TREE;
11587 for (tree t = attributes; t; t = TREE_CHAIN (t))
11588 if (ATTR_IS_DEPENDENT (t))
11590 last_dep = t;
11591 attributes = copy_list (attributes);
11592 break;
11595 if (last_dep)
11596 for (tree *p = &attributes; *p; )
11598 tree t = *p;
11599 if (ATTR_IS_DEPENDENT (t))
11601 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11602 if (subst != t)
11604 *p = subst;
11605 while (*p)
11606 p = &TREE_CHAIN (*p);
11607 *p = TREE_CHAIN (t);
11608 continue;
11611 p = &TREE_CHAIN (*p);
11614 return attributes;
11617 /* Apply any attributes which had to be deferred until instantiation
11618 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11619 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11620 false on error. */
11622 static bool
11623 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11624 tree args, tsubst_flags_t complain, tree in_decl)
11626 tree t;
11627 tree *p;
11629 if (attributes == NULL_TREE)
11630 return true;
11632 if (DECL_P (*decl_p))
11634 if (TREE_TYPE (*decl_p) == error_mark_node)
11635 return false;
11636 p = &DECL_ATTRIBUTES (*decl_p);
11637 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11638 to our attributes parameter. */
11639 gcc_assert (*p == attributes);
11641 else
11643 p = &TYPE_ATTRIBUTES (*decl_p);
11644 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11645 lookup_template_class_1, and should be preserved. */
11646 gcc_assert (*p != attributes);
11647 while (*p)
11648 p = &TREE_CHAIN (*p);
11651 /* save_template_attributes puts the dependent attributes at the beginning of
11652 the list; find the non-dependent ones. */
11653 for (t = attributes; t; t = TREE_CHAIN (t))
11654 if (!ATTR_IS_DEPENDENT (t))
11655 break;
11656 tree nondep = t;
11658 /* Apply any non-dependent attributes. */
11659 *p = nondep;
11661 /* And then any dependent ones. */
11662 tree late_attrs = NULL_TREE;
11663 tree *q = &late_attrs;
11664 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
11666 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11667 if (*q == error_mark_node)
11668 return false;
11669 if (*q == t)
11671 *q = copy_node (t);
11672 TREE_CHAIN (*q) = NULL_TREE;
11674 while (*q)
11675 q = &TREE_CHAIN (*q);
11678 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11680 return true;
11683 /* The template TMPL is being instantiated with the template arguments TARGS.
11684 Perform the access checks that we deferred when parsing the template. */
11686 static void
11687 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11689 unsigned i;
11690 deferred_access_check *chk;
11692 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11693 return;
11695 if (vec<deferred_access_check, va_gc> *access_checks
11696 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11697 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11699 tree decl = chk->decl;
11700 tree diag_decl = chk->diag_decl;
11701 tree type_scope = TREE_TYPE (chk->binfo);
11703 if (uses_template_parms (type_scope))
11704 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11706 /* Make access check error messages point to the location
11707 of the use of the typedef. */
11708 iloc_sentinel ils (chk->loc);
11709 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11710 decl, diag_decl, tf_warning_or_error);
11714 static tree
11715 instantiate_class_template_1 (tree type)
11717 tree templ, args, pattern, t, member;
11718 tree typedecl;
11719 tree pbinfo;
11720 tree base_list;
11721 unsigned int saved_maximum_field_alignment;
11722 tree fn_context;
11724 if (type == error_mark_node)
11725 return error_mark_node;
11727 if (COMPLETE_OR_OPEN_TYPE_P (type)
11728 || uses_template_parms (type))
11729 return type;
11731 /* Figure out which template is being instantiated. */
11732 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11733 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11735 /* Mark the type as in the process of being defined. */
11736 TYPE_BEING_DEFINED (type) = 1;
11738 /* We may be in the middle of deferred access check. Disable
11739 it now. */
11740 deferring_access_check_sentinel acs (dk_no_deferred);
11742 /* Determine what specialization of the original template to
11743 instantiate. */
11744 t = most_specialized_partial_spec (type, tf_warning_or_error);
11745 if (t == error_mark_node)
11746 return error_mark_node;
11747 else if (t)
11749 /* This TYPE is actually an instantiation of a partial
11750 specialization. We replace the innermost set of ARGS with
11751 the arguments appropriate for substitution. For example,
11752 given:
11754 template <class T> struct S {};
11755 template <class T> struct S<T*> {};
11757 and supposing that we are instantiating S<int*>, ARGS will
11758 presently be {int*} -- but we need {int}. */
11759 pattern = TREE_TYPE (t);
11760 args = TREE_PURPOSE (t);
11762 else
11764 pattern = TREE_TYPE (templ);
11765 args = CLASSTYPE_TI_ARGS (type);
11768 /* If the template we're instantiating is incomplete, then clearly
11769 there's nothing we can do. */
11770 if (!COMPLETE_TYPE_P (pattern))
11772 /* We can try again later. */
11773 TYPE_BEING_DEFINED (type) = 0;
11774 return type;
11777 /* If we've recursively instantiated too many templates, stop. */
11778 if (! push_tinst_level (type))
11779 return type;
11781 int saved_unevaluated_operand = cp_unevaluated_operand;
11782 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11784 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11785 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11786 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11787 fn_context = error_mark_node;
11788 if (!fn_context)
11789 push_to_top_level ();
11790 else
11792 cp_unevaluated_operand = 0;
11793 c_inhibit_evaluation_warnings = 0;
11795 /* Use #pragma pack from the template context. */
11796 saved_maximum_field_alignment = maximum_field_alignment;
11797 maximum_field_alignment = TYPE_PRECISION (pattern);
11799 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11801 /* Set the input location to the most specialized template definition.
11802 This is needed if tsubsting causes an error. */
11803 typedecl = TYPE_MAIN_DECL (pattern);
11804 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11805 DECL_SOURCE_LOCATION (typedecl);
11807 set_instantiating_module (TYPE_NAME (type));
11809 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11810 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11811 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11812 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11813 if (ANON_AGGR_TYPE_P (pattern))
11814 SET_ANON_AGGR_TYPE_P (type);
11815 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11817 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11818 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11819 /* Adjust visibility for template arguments. */
11820 determine_visibility (TYPE_MAIN_DECL (type));
11822 if (CLASS_TYPE_P (type))
11823 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11825 pbinfo = TYPE_BINFO (pattern);
11827 /* We should never instantiate a nested class before its enclosing
11828 class; we need to look up the nested class by name before we can
11829 instantiate it, and that lookup should instantiate the enclosing
11830 class. */
11831 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11832 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11834 base_list = NULL_TREE;
11835 /* Defer access checking while we substitute into the types named in
11836 the base-clause. */
11837 push_deferring_access_checks (dk_deferred);
11838 if (BINFO_N_BASE_BINFOS (pbinfo))
11840 tree pbase_binfo;
11841 int i;
11843 /* Substitute into each of the bases to determine the actual
11844 basetypes. */
11845 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11847 tree base;
11848 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11849 tree expanded_bases = NULL_TREE;
11850 int idx, len = 1;
11852 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11854 expanded_bases =
11855 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11856 args, tf_error, NULL_TREE);
11857 if (expanded_bases == error_mark_node)
11858 continue;
11860 len = TREE_VEC_LENGTH (expanded_bases);
11863 for (idx = 0; idx < len; idx++)
11865 if (expanded_bases)
11866 /* Extract the already-expanded base class. */
11867 base = TREE_VEC_ELT (expanded_bases, idx);
11868 else
11869 /* Substitute to figure out the base class. */
11870 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11871 NULL_TREE);
11873 if (base == error_mark_node)
11874 continue;
11876 base_list = tree_cons (access, base, base_list);
11877 if (BINFO_VIRTUAL_P (pbase_binfo))
11878 TREE_TYPE (base_list) = integer_type_node;
11882 /* The list is now in reverse order; correct that. */
11883 base_list = nreverse (base_list);
11885 /* Now call xref_basetypes to set up all the base-class
11886 information. */
11887 xref_basetypes (type, base_list);
11889 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11890 (int) ATTR_FLAG_TYPE_IN_PLACE,
11891 args, tf_error, NULL_TREE);
11892 fixup_attribute_variants (type);
11894 /* Now that our base classes are set up, enter the scope of the
11895 class, so that name lookups into base classes, etc. will work
11896 correctly. This is precisely analogous to what we do in
11897 begin_class_definition when defining an ordinary non-template
11898 class, except we also need to push the enclosing classes. */
11899 push_nested_class (type);
11901 /* Now check accessibility of the types named in its base-clause,
11902 relative to the scope of the class. */
11903 pop_to_parent_deferring_access_checks ();
11905 /* A vector to hold members marked with attribute used. */
11906 auto_vec<tree> used;
11908 /* Now members are processed in the order of declaration. */
11909 for (member = CLASSTYPE_DECL_LIST (pattern);
11910 member; member = TREE_CHAIN (member))
11912 tree t = TREE_VALUE (member);
11914 if (TREE_PURPOSE (member))
11916 if (TYPE_P (t))
11918 if (LAMBDA_TYPE_P (t))
11919 /* A closure type for a lambda in an NSDMI or default argument.
11920 Ignore it; it will be regenerated when needed. */
11921 continue;
11923 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11924 && TYPE_LANG_SPECIFIC (t)
11925 && CLASSTYPE_IS_TEMPLATE (t));
11927 /* If the member is a class template, then -- even after
11928 substitution -- there may be dependent types in the
11929 template argument list for the class. We increment
11930 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11931 that function will assume that no types are dependent
11932 when outside of a template. */
11933 if (class_template_p)
11934 ++processing_template_decl;
11935 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11936 if (class_template_p)
11937 --processing_template_decl;
11938 if (newtag == error_mark_node)
11939 continue;
11941 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11943 tree name = TYPE_IDENTIFIER (t);
11945 if (class_template_p)
11946 /* Unfortunately, lookup_template_class sets
11947 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11948 instantiation (i.e., for the type of a member
11949 template class nested within a template class.)
11950 This behavior is required for
11951 maybe_process_partial_specialization to work
11952 correctly, but is not accurate in this case;
11953 the TAG is not an instantiation of anything.
11954 (The corresponding TEMPLATE_DECL is an
11955 instantiation, but the TYPE is not.) */
11956 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11958 /* Now, install the tag. We don't use pushtag
11959 because that does too much work -- creating an
11960 implicit typedef, which we've already done. */
11961 set_identifier_type_value (name, TYPE_NAME (newtag));
11962 maybe_add_class_template_decl_list (type, newtag, false);
11963 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
11964 determine_visibility (TYPE_NAME (newtag));
11967 else if (DECL_DECLARES_FUNCTION_P (t))
11969 tree r;
11971 if (TREE_CODE (t) == TEMPLATE_DECL)
11972 ++processing_template_decl;
11973 r = tsubst (t, args, tf_error, NULL_TREE);
11974 if (TREE_CODE (t) == TEMPLATE_DECL)
11975 --processing_template_decl;
11976 set_current_access_from_decl (r);
11977 finish_member_declaration (r);
11978 /* Instantiate members marked with attribute used. */
11979 if (r != error_mark_node && DECL_PRESERVE_P (r))
11980 used.safe_push (r);
11981 if (TREE_CODE (r) == FUNCTION_DECL
11982 && DECL_OMP_DECLARE_REDUCTION_P (r))
11983 cp_check_omp_declare_reduction (r);
11985 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11986 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11987 /* A closure type for a lambda in an NSDMI or default argument.
11988 Ignore it; it will be regenerated when needed. */;
11989 else
11991 /* Build new TYPE_FIELDS. */
11992 if (TREE_CODE (t) == STATIC_ASSERT)
11993 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11994 /*integral_constant_expression_p=*/true);
11995 else if (TREE_CODE (t) != CONST_DECL)
11997 tree r;
11998 tree vec = NULL_TREE;
11999 int len = 1;
12001 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12002 /* The file and line for this declaration, to
12003 assist in error message reporting. Since we
12004 called push_tinst_level above, we don't need to
12005 restore these. */
12006 input_location = DECL_SOURCE_LOCATION (t);
12008 if (TREE_CODE (t) == TEMPLATE_DECL)
12009 ++processing_template_decl;
12010 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12011 if (TREE_CODE (t) == TEMPLATE_DECL)
12012 --processing_template_decl;
12014 if (TREE_CODE (r) == TREE_VEC)
12016 /* A capture pack became multiple fields. */
12017 vec = r;
12018 len = TREE_VEC_LENGTH (vec);
12021 for (int i = 0; i < len; ++i)
12023 if (vec)
12024 r = TREE_VEC_ELT (vec, i);
12025 if (VAR_P (r))
12027 /* In [temp.inst]:
12029 [t]he initialization (and any associated
12030 side-effects) of a static data member does
12031 not occur unless the static data member is
12032 itself used in a way that requires the
12033 definition of the static data member to
12034 exist.
12036 Therefore, we do not substitute into the
12037 initialized for the static data member here. */
12038 finish_static_data_member_decl
12040 /*init=*/NULL_TREE,
12041 /*init_const_expr_p=*/false,
12042 /*asmspec_tree=*/NULL_TREE,
12043 /*flags=*/0);
12044 /* Instantiate members marked with attribute used. */
12045 if (r != error_mark_node && DECL_PRESERVE_P (r))
12046 used.safe_push (r);
12048 else if (TREE_CODE (r) == FIELD_DECL)
12050 /* Determine whether R has a valid type and can be
12051 completed later. If R is invalid, then its type
12052 is replaced by error_mark_node. */
12053 tree rtype = TREE_TYPE (r);
12054 if (can_complete_type_without_circularity (rtype))
12055 complete_type (rtype);
12057 if (!complete_or_array_type_p (rtype))
12059 /* If R's type couldn't be completed and
12060 it isn't a flexible array member (whose
12061 type is incomplete by definition) give
12062 an error. */
12063 cxx_incomplete_type_error (r, rtype);
12064 TREE_TYPE (r) = error_mark_node;
12066 else if (TREE_CODE (rtype) == ARRAY_TYPE
12067 && TYPE_DOMAIN (rtype) == NULL_TREE
12068 && (TREE_CODE (type) == UNION_TYPE
12069 || TREE_CODE (type) == QUAL_UNION_TYPE))
12071 error ("flexible array member %qD in union", r);
12072 TREE_TYPE (r) = error_mark_node;
12074 else if (!verify_type_context (input_location,
12075 TCTX_FIELD, rtype))
12076 TREE_TYPE (r) = error_mark_node;
12079 /* If it is a TYPE_DECL for a class-scoped
12080 ENUMERAL_TYPE, such a thing will already have
12081 been added to the field list by tsubst_enum
12082 in finish_member_declaration case above. */
12083 if (!(TREE_CODE (r) == TYPE_DECL
12084 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12085 && DECL_ARTIFICIAL (r)))
12087 set_current_access_from_decl (r);
12088 finish_member_declaration (r);
12094 else
12096 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12097 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12099 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12101 tree friend_type = t;
12102 bool adjust_processing_template_decl = false;
12104 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12106 /* template <class T> friend class C; */
12107 friend_type = tsubst_friend_class (friend_type, args);
12108 adjust_processing_template_decl = true;
12110 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12112 /* template <class T> friend class C::D; */
12113 friend_type = tsubst (friend_type, args,
12114 tf_warning_or_error, NULL_TREE);
12115 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12116 friend_type = TREE_TYPE (friend_type);
12117 adjust_processing_template_decl = true;
12119 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12120 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12122 /* This could be either
12124 friend class T::C;
12126 when dependent_type_p is false or
12128 template <class U> friend class T::C;
12130 otherwise. */
12131 /* Bump processing_template_decl in case this is something like
12132 template <class T> friend struct A<T>::B. */
12133 ++processing_template_decl;
12134 friend_type = tsubst (friend_type, args,
12135 tf_warning_or_error, NULL_TREE);
12136 if (dependent_type_p (friend_type))
12137 adjust_processing_template_decl = true;
12138 --processing_template_decl;
12140 else if (uses_template_parms (friend_type))
12141 /* friend class C<T>; */
12142 friend_type = tsubst (friend_type, args,
12143 tf_warning_or_error, NULL_TREE);
12145 /* Otherwise it's
12147 friend class C;
12149 where C is already declared or
12151 friend class C<int>;
12153 We don't have to do anything in these cases. */
12155 if (adjust_processing_template_decl)
12156 /* Trick make_friend_class into realizing that the friend
12157 we're adding is a template, not an ordinary class. It's
12158 important that we use make_friend_class since it will
12159 perform some error-checking and output cross-reference
12160 information. */
12161 ++processing_template_decl;
12163 if (friend_type != error_mark_node)
12164 make_friend_class (type, friend_type, /*complain=*/false);
12166 if (adjust_processing_template_decl)
12167 --processing_template_decl;
12169 else
12171 /* Build new DECL_FRIENDLIST. */
12172 tree r;
12174 /* The file and line for this declaration, to
12175 assist in error message reporting. Since we
12176 called push_tinst_level above, we don't need to
12177 restore these. */
12178 input_location = DECL_SOURCE_LOCATION (t);
12180 if (TREE_CODE (t) == TEMPLATE_DECL)
12182 ++processing_template_decl;
12183 push_deferring_access_checks (dk_no_check);
12186 r = tsubst_friend_function (t, args);
12187 add_friend (type, r, /*complain=*/false);
12188 if (TREE_CODE (t) == TEMPLATE_DECL)
12190 pop_deferring_access_checks ();
12191 --processing_template_decl;
12197 if (fn_context)
12199 /* Restore these before substituting into the lambda capture
12200 initializers. */
12201 cp_unevaluated_operand = saved_unevaluated_operand;
12202 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12205 /* Set the file and line number information to whatever is given for
12206 the class itself. This puts error messages involving generated
12207 implicit functions at a predictable point, and the same point
12208 that would be used for non-template classes. */
12209 input_location = DECL_SOURCE_LOCATION (typedecl);
12211 unreverse_member_declarations (type);
12212 finish_struct_1 (type);
12213 TYPE_BEING_DEFINED (type) = 0;
12215 /* We don't instantiate default arguments for member functions. 14.7.1:
12217 The implicit instantiation of a class template specialization causes
12218 the implicit instantiation of the declarations, but not of the
12219 definitions or default arguments, of the class member functions,
12220 member classes, static data members and member templates.... */
12222 perform_instantiation_time_access_checks (pattern, args);
12223 perform_deferred_access_checks (tf_warning_or_error);
12224 pop_nested_class ();
12225 maximum_field_alignment = saved_maximum_field_alignment;
12226 if (!fn_context)
12227 pop_from_top_level ();
12228 pop_tinst_level ();
12230 /* The vtable for a template class can be emitted in any translation
12231 unit in which the class is instantiated. When there is no key
12232 method, however, finish_struct_1 will already have added TYPE to
12233 the keyed_classes. */
12234 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12235 vec_safe_push (keyed_classes, type);
12237 /* Now that we've gone through all the members, instantiate those
12238 marked with attribute used. */
12239 for (tree x : used)
12240 mark_used (x);
12242 return type;
12245 /* Wrapper for instantiate_class_template_1. */
12247 tree
12248 instantiate_class_template (tree type)
12250 tree ret;
12251 timevar_push (TV_TEMPLATE_INST);
12252 ret = instantiate_class_template_1 (type);
12253 timevar_pop (TV_TEMPLATE_INST);
12254 return ret;
12257 tree
12258 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12260 tree r;
12262 if (!t)
12263 r = t;
12264 else if (TYPE_P (t))
12265 r = tsubst (t, args, complain, in_decl);
12266 else
12268 if (!(complain & tf_warning))
12269 ++c_inhibit_evaluation_warnings;
12270 r = tsubst_expr (t, args, complain, in_decl,
12271 /*integral_constant_expression_p=*/true);
12272 if (!(complain & tf_warning))
12273 --c_inhibit_evaluation_warnings;
12276 return r;
12279 /* Given a function parameter pack TMPL_PARM and some function parameters
12280 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12281 and set *SPEC_P to point at the next point in the list. */
12283 tree
12284 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12286 /* Collect all of the extra "packed" parameters into an
12287 argument pack. */
12288 tree argpack;
12289 tree spec_parm = *spec_p;
12290 int len;
12292 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12293 if (tmpl_parm
12294 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12295 break;
12297 spec_parm = *spec_p;
12298 if (len == 1 && DECL_PACK_P (spec_parm))
12300 /* The instantiation is still a parameter pack; don't wrap it in a
12301 NONTYPE_ARGUMENT_PACK. */
12302 argpack = spec_parm;
12303 spec_parm = DECL_CHAIN (spec_parm);
12305 else
12307 /* Fill in PARMVEC with all of the parameters. */
12308 tree parmvec = make_tree_vec (len);
12309 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12310 for (int i = 0; i < len; i++)
12312 tree elt = spec_parm;
12313 if (DECL_PACK_P (elt))
12314 elt = make_pack_expansion (elt);
12315 TREE_VEC_ELT (parmvec, i) = elt;
12316 spec_parm = DECL_CHAIN (spec_parm);
12319 /* Build the argument packs. */
12320 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12322 *spec_p = spec_parm;
12324 return argpack;
12327 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12328 NONTYPE_ARGUMENT_PACK. */
12330 static tree
12331 make_fnparm_pack (tree spec_parm)
12333 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12336 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12337 pack expansion with no extra args, 2 if it has extra args, or 0
12338 if it is not a pack expansion. */
12340 static int
12341 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12343 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12344 /* We're being called before this happens in tsubst_pack_expansion. */
12345 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12346 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12347 if (i >= TREE_VEC_LENGTH (vec))
12348 return 0;
12349 tree elt = TREE_VEC_ELT (vec, i);
12350 if (DECL_P (elt))
12351 /* A decl pack is itself an expansion. */
12352 elt = TREE_TYPE (elt);
12353 if (!PACK_EXPANSION_P (elt))
12354 return 0;
12355 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12356 return 2;
12357 return 1;
12361 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12363 static tree
12364 make_argument_pack_select (tree arg_pack, unsigned index)
12366 tree aps = make_node (ARGUMENT_PACK_SELECT);
12368 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12369 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12371 return aps;
12374 /* This is a subroutine of tsubst_pack_expansion.
12376 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12377 mechanism to store the (non complete list of) arguments of the
12378 substitution and return a non substituted pack expansion, in order
12379 to wait for when we have enough arguments to really perform the
12380 substitution. */
12382 static bool
12383 use_pack_expansion_extra_args_p (tree parm_packs,
12384 int arg_pack_len,
12385 bool has_empty_arg)
12387 /* If one pack has an expansion and another pack has a normal
12388 argument or if one pack has an empty argument and an another
12389 one hasn't then tsubst_pack_expansion cannot perform the
12390 substitution and need to fall back on the
12391 PACK_EXPANSION_EXTRA mechanism. */
12392 if (parm_packs == NULL_TREE)
12393 return false;
12394 else if (has_empty_arg)
12396 /* If all the actual packs are pack expansions, we can still
12397 subsitute directly. */
12398 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12400 tree a = TREE_VALUE (p);
12401 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12402 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12403 a = ARGUMENT_PACK_ARGS (a);
12404 if (TREE_VEC_LENGTH (a) == 1)
12405 a = TREE_VEC_ELT (a, 0);
12406 if (PACK_EXPANSION_P (a))
12407 continue;
12408 return true;
12410 return false;
12413 for (int i = 0 ; i < arg_pack_len; ++i)
12415 bool has_expansion_arg = false;
12416 bool has_non_expansion_arg = false;
12417 for (tree parm_pack = parm_packs;
12418 parm_pack;
12419 parm_pack = TREE_CHAIN (parm_pack))
12421 tree arg = TREE_VALUE (parm_pack);
12423 int exp = argument_pack_element_is_expansion_p (arg, i);
12424 if (exp == 2)
12425 /* We can't substitute a pack expansion with extra args into
12426 our pattern. */
12427 return true;
12428 else if (exp)
12429 has_expansion_arg = true;
12430 else
12431 has_non_expansion_arg = true;
12434 if (has_expansion_arg && has_non_expansion_arg)
12436 gcc_checking_assert (false);
12437 return true;
12440 return false;
12443 /* [temp.variadic]/6 says that:
12445 The instantiation of a pack expansion [...]
12446 produces a list E1,E2, ..., En, where N is the number of elements
12447 in the pack expansion parameters.
12449 This subroutine of tsubst_pack_expansion produces one of these Ei.
12451 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12452 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12453 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12454 INDEX is the index 'i' of the element Ei to produce. ARGS,
12455 COMPLAIN, and IN_DECL are the same parameters as for the
12456 tsubst_pack_expansion function.
12458 The function returns the resulting Ei upon successful completion,
12459 or error_mark_node.
12461 Note that this function possibly modifies the ARGS parameter, so
12462 it's the responsibility of the caller to restore it. */
12464 static tree
12465 gen_elem_of_pack_expansion_instantiation (tree pattern,
12466 tree parm_packs,
12467 unsigned index,
12468 tree args /* This parm gets
12469 modified. */,
12470 tsubst_flags_t complain,
12471 tree in_decl)
12473 tree t;
12474 bool ith_elem_is_expansion = false;
12476 /* For each parameter pack, change the substitution of the parameter
12477 pack to the ith argument in its argument pack, then expand the
12478 pattern. */
12479 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12481 tree parm = TREE_PURPOSE (pack);
12482 tree arg_pack = TREE_VALUE (pack);
12483 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12485 ith_elem_is_expansion |=
12486 argument_pack_element_is_expansion_p (arg_pack, index);
12488 /* Select the Ith argument from the pack. */
12489 if (TREE_CODE (parm) == PARM_DECL
12490 || VAR_P (parm)
12491 || TREE_CODE (parm) == FIELD_DECL)
12493 if (index == 0)
12495 aps = make_argument_pack_select (arg_pack, index);
12496 if (!mark_used (parm, complain) && !(complain & tf_error))
12497 return error_mark_node;
12498 register_local_specialization (aps, parm);
12500 else
12501 aps = retrieve_local_specialization (parm);
12503 else
12505 int idx, level;
12506 template_parm_level_and_index (parm, &level, &idx);
12508 if (index == 0)
12510 aps = make_argument_pack_select (arg_pack, index);
12511 /* Update the corresponding argument. */
12512 TMPL_ARG (args, level, idx) = aps;
12514 else
12515 /* Re-use the ARGUMENT_PACK_SELECT. */
12516 aps = TMPL_ARG (args, level, idx);
12518 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12521 /* Substitute into the PATTERN with the (possibly altered)
12522 arguments. */
12523 if (pattern == in_decl)
12524 /* Expanding a fixed parameter pack from
12525 coerce_template_parameter_pack. */
12526 t = tsubst_decl (pattern, args, complain);
12527 else if (pattern == error_mark_node)
12528 t = error_mark_node;
12529 else if (!TYPE_P (pattern))
12530 t = tsubst_expr (pattern, args, complain, in_decl,
12531 /*integral_constant_expression_p=*/false);
12532 else
12533 t = tsubst (pattern, args, complain, in_decl);
12535 /* If the Ith argument pack element is a pack expansion, then
12536 the Ith element resulting from the substituting is going to
12537 be a pack expansion as well. */
12538 if (ith_elem_is_expansion)
12539 t = make_pack_expansion (t, complain);
12541 return t;
12544 /* When the unexpanded parameter pack in a fold expression expands to an empty
12545 sequence, the value of the expression is as follows; the program is
12546 ill-formed if the operator is not listed in this table.
12548 && true
12549 || false
12550 , void() */
12552 tree
12553 expand_empty_fold (tree t, tsubst_flags_t complain)
12555 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12556 if (!FOLD_EXPR_MODIFY_P (t))
12557 switch (code)
12559 case TRUTH_ANDIF_EXPR:
12560 return boolean_true_node;
12561 case TRUTH_ORIF_EXPR:
12562 return boolean_false_node;
12563 case COMPOUND_EXPR:
12564 return void_node;
12565 default:
12566 break;
12569 if (complain & tf_error)
12570 error_at (location_of (t),
12571 "fold of empty expansion over %O", code);
12572 return error_mark_node;
12575 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12576 form an expression that combines the two terms using the
12577 operator of T. */
12579 static tree
12580 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12582 tree op = FOLD_EXPR_OP (t);
12583 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12585 // Handle compound assignment operators.
12586 if (FOLD_EXPR_MODIFY_P (t))
12587 return build_x_modify_expr (input_location, left, code, right, complain);
12589 warning_sentinel s(warn_parentheses);
12590 switch (code)
12592 case COMPOUND_EXPR:
12593 return build_x_compound_expr (input_location, left, right, complain);
12594 default:
12595 return build_x_binary_op (input_location, code,
12596 left, TREE_CODE (left),
12597 right, TREE_CODE (right),
12598 /*overload=*/NULL,
12599 complain);
12603 /* Substitute ARGS into the pack of a fold expression T. */
12605 static inline tree
12606 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12608 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12611 /* Substitute ARGS into the pack of a fold expression T. */
12613 static inline tree
12614 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12616 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12619 /* Expand a PACK of arguments into a grouped as left fold.
12620 Given a pack containing elements A0, A1, ..., An and an
12621 operator @, this builds the expression:
12623 ((A0 @ A1) @ A2) ... @ An
12625 Note that PACK must not be empty.
12627 The operator is defined by the original fold expression T. */
12629 static tree
12630 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12632 tree left = TREE_VEC_ELT (pack, 0);
12633 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12635 tree right = TREE_VEC_ELT (pack, i);
12636 left = fold_expression (t, left, right, complain);
12638 return left;
12641 /* Substitute into a unary left fold expression. */
12643 static tree
12644 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12645 tree in_decl)
12647 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12648 if (pack == error_mark_node)
12649 return error_mark_node;
12650 if (PACK_EXPANSION_P (pack))
12652 tree r = copy_node (t);
12653 FOLD_EXPR_PACK (r) = pack;
12654 return r;
12656 if (TREE_VEC_LENGTH (pack) == 0)
12657 return expand_empty_fold (t, complain);
12658 else
12659 return expand_left_fold (t, pack, complain);
12662 /* Substitute into a binary left fold expression.
12664 Do ths by building a single (non-empty) vector of argumnts and
12665 building the expression from those elements. */
12667 static tree
12668 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12669 tree in_decl)
12671 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12672 if (pack == error_mark_node)
12673 return error_mark_node;
12674 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12675 if (init == error_mark_node)
12676 return error_mark_node;
12678 if (PACK_EXPANSION_P (pack))
12680 tree r = copy_node (t);
12681 FOLD_EXPR_PACK (r) = pack;
12682 FOLD_EXPR_INIT (r) = init;
12683 return r;
12686 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12687 TREE_VEC_ELT (vec, 0) = init;
12688 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12689 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12691 return expand_left_fold (t, vec, complain);
12694 /* Expand a PACK of arguments into a grouped as right fold.
12695 Given a pack containing elementns A0, A1, ..., and an
12696 operator @, this builds the expression:
12698 A0@ ... (An-2 @ (An-1 @ An))
12700 Note that PACK must not be empty.
12702 The operator is defined by the original fold expression T. */
12704 tree
12705 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12707 // Build the expression.
12708 int n = TREE_VEC_LENGTH (pack);
12709 tree right = TREE_VEC_ELT (pack, n - 1);
12710 for (--n; n != 0; --n)
12712 tree left = TREE_VEC_ELT (pack, n - 1);
12713 right = fold_expression (t, left, right, complain);
12715 return right;
12718 /* Substitute into a unary right fold expression. */
12720 static tree
12721 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12722 tree in_decl)
12724 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12725 if (pack == error_mark_node)
12726 return error_mark_node;
12727 if (PACK_EXPANSION_P (pack))
12729 tree r = copy_node (t);
12730 FOLD_EXPR_PACK (r) = pack;
12731 return r;
12733 if (TREE_VEC_LENGTH (pack) == 0)
12734 return expand_empty_fold (t, complain);
12735 else
12736 return expand_right_fold (t, pack, complain);
12739 /* Substitute into a binary right fold expression.
12741 Do ths by building a single (non-empty) vector of arguments and
12742 building the expression from those elements. */
12744 static tree
12745 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12746 tree in_decl)
12748 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12749 if (pack == error_mark_node)
12750 return error_mark_node;
12751 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12752 if (init == error_mark_node)
12753 return error_mark_node;
12755 if (PACK_EXPANSION_P (pack))
12757 tree r = copy_node (t);
12758 FOLD_EXPR_PACK (r) = pack;
12759 FOLD_EXPR_INIT (r) = init;
12760 return r;
12763 int n = TREE_VEC_LENGTH (pack);
12764 tree vec = make_tree_vec (n + 1);
12765 for (int i = 0; i < n; ++i)
12766 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12767 TREE_VEC_ELT (vec, n) = init;
12769 return expand_right_fold (t, vec, complain);
12772 /* Walk through the pattern of a pack expansion, adding everything in
12773 local_specializations to a list. */
12775 class el_data
12777 public:
12778 /* Set of variables declared within the pattern. */
12779 hash_set<tree> internal;
12780 /* Set of AST nodes that have been visited by the traversal. */
12781 hash_set<tree> visited;
12782 /* List of local_specializations used within the pattern. */
12783 tree extra;
12784 tsubst_flags_t complain;
12786 el_data (tsubst_flags_t c)
12787 : extra (NULL_TREE), complain (c) {}
12789 static tree
12790 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12792 el_data &data = *reinterpret_cast<el_data*>(data_);
12793 tree *extra = &data.extra;
12794 tsubst_flags_t complain = data.complain;
12796 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12797 /* Remember local typedefs (85214). */
12798 tp = &TYPE_NAME (*tp);
12800 if (TREE_CODE (*tp) == DECL_EXPR)
12802 tree decl = DECL_EXPR_DECL (*tp);
12803 data.internal.add (decl);
12804 if (VAR_P (decl)
12805 && DECL_DECOMPOSITION_P (decl)
12806 && TREE_TYPE (decl) != error_mark_node)
12808 gcc_assert (DECL_NAME (decl) == NULL_TREE);
12809 for (tree decl2 = DECL_CHAIN (decl);
12810 decl2
12811 && VAR_P (decl2)
12812 && DECL_DECOMPOSITION_P (decl2)
12813 && DECL_NAME (decl2)
12814 && TREE_TYPE (decl2) != error_mark_node;
12815 decl2 = DECL_CHAIN (decl2))
12817 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
12818 data.internal.add (decl2);
12822 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
12824 /* Since we defer implicit capture, look in the parms and body. */
12825 tree fn = lambda_function (*tp);
12826 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
12827 &data.visited);
12828 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
12829 &data.visited);
12831 else if (tree spec = retrieve_local_specialization (*tp))
12833 if (data.internal.contains (*tp))
12834 /* Don't mess with variables declared within the pattern. */
12835 return NULL_TREE;
12836 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12838 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12839 tree args = ARGUMENT_PACK_ARGS (spec);
12840 if (TREE_VEC_LENGTH (args) == 1)
12842 tree elt = TREE_VEC_ELT (args, 0);
12843 if (PACK_EXPANSION_P (elt))
12844 elt = PACK_EXPANSION_PATTERN (elt);
12845 if (DECL_PACK_P (elt))
12846 spec = elt;
12848 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12850 /* Handle lambda capture here, since we aren't doing any
12851 substitution now, and so tsubst_copy won't call
12852 process_outer_var_ref. */
12853 tree args = ARGUMENT_PACK_ARGS (spec);
12854 int len = TREE_VEC_LENGTH (args);
12855 for (int i = 0; i < len; ++i)
12857 tree arg = TREE_VEC_ELT (args, i);
12858 tree carg = arg;
12859 if (outer_automatic_var_p (arg))
12860 carg = process_outer_var_ref (arg, complain);
12861 if (carg != arg)
12863 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12864 proxies. */
12865 if (i == 0)
12867 spec = copy_node (spec);
12868 args = copy_node (args);
12869 SET_ARGUMENT_PACK_ARGS (spec, args);
12870 register_local_specialization (spec, *tp);
12872 TREE_VEC_ELT (args, i) = carg;
12877 if (outer_automatic_var_p (spec))
12878 spec = process_outer_var_ref (spec, complain);
12879 *extra = tree_cons (*tp, spec, *extra);
12881 return NULL_TREE;
12883 static tree
12884 extract_local_specs (tree pattern, tsubst_flags_t complain)
12886 el_data data (complain);
12887 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
12888 return data.extra;
12891 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12892 for use in PACK_EXPANSION_EXTRA_ARGS. */
12894 tree
12895 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12897 /* Make a copy of the extra arguments so that they won't get changed
12898 out from under us. */
12899 tree extra = copy_template_args (args);
12900 if (local_specializations)
12901 if (tree locals = extract_local_specs (pattern, complain))
12902 extra = tree_cons (NULL_TREE, extra, locals);
12903 return extra;
12906 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12907 normal template args to ARGS. */
12909 tree
12910 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
12912 if (extra && TREE_CODE (extra) == TREE_LIST)
12914 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12916 /* The partial instantiation involved local declarations collected in
12917 extract_local_specs; map from the general template to our local
12918 context. */
12919 tree gen = TREE_PURPOSE (elt);
12920 tree inst = TREE_VALUE (elt);
12921 if (DECL_P (inst))
12922 if (tree local = retrieve_local_specialization (inst))
12923 inst = local;
12924 /* else inst is already a full instantiation of the pack. */
12925 register_local_specialization (inst, gen);
12927 gcc_assert (!TREE_PURPOSE (extra));
12928 extra = TREE_VALUE (extra);
12930 if (uses_template_parms (extra))
12932 /* This can happen after dependent substitution into a
12933 requires-expr or a lambda that uses constexpr if. */
12934 extra = tsubst_template_args (extra, args, complain, in_decl);
12935 args = add_outermost_template_args (args, extra);
12937 else
12938 args = add_to_template_args (extra, args);
12939 return args;
12942 /* Substitute ARGS into T, which is an pack expansion
12943 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12944 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12945 (if only a partial substitution could be performed) or
12946 ERROR_MARK_NODE if there was an error. */
12947 tree
12948 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12949 tree in_decl)
12951 tree pattern;
12952 tree pack, packs = NULL_TREE;
12953 bool unsubstituted_packs = false;
12954 int i, len = -1;
12955 tree result;
12956 bool need_local_specializations = false;
12957 int levels;
12959 gcc_assert (PACK_EXPANSION_P (t));
12960 pattern = PACK_EXPANSION_PATTERN (t);
12962 /* Add in any args remembered from an earlier partial instantiation. */
12963 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
12965 levels = TMPL_ARGS_DEPTH (args);
12967 /* Determine the argument packs that will instantiate the parameter
12968 packs used in the expansion expression. While we're at it,
12969 compute the number of arguments to be expanded and make sure it
12970 is consistent. */
12971 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12972 pack = TREE_CHAIN (pack))
12974 tree parm_pack = TREE_VALUE (pack);
12975 tree arg_pack = NULL_TREE;
12976 tree orig_arg = NULL_TREE;
12977 int level = 0;
12979 if (TREE_CODE (parm_pack) == BASES)
12981 gcc_assert (parm_pack == pattern);
12982 if (BASES_DIRECT (parm_pack))
12983 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12984 args, complain,
12985 in_decl, false),
12986 complain);
12987 else
12988 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12989 args, complain, in_decl,
12990 false), complain);
12992 else if (builtin_pack_call_p (parm_pack))
12994 if (parm_pack != pattern)
12996 if (complain & tf_error)
12997 sorry ("%qE is not the entire pattern of the pack expansion",
12998 parm_pack);
12999 return error_mark_node;
13001 return expand_builtin_pack_call (parm_pack, args,
13002 complain, in_decl);
13004 else if (TREE_CODE (parm_pack) == PARM_DECL)
13006 /* We know we have correct local_specializations if this
13007 expansion is at function scope, or if we're dealing with a
13008 local parameter in a requires expression; for the latter,
13009 tsubst_requires_expr set it up appropriately. */
13010 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13011 arg_pack = retrieve_local_specialization (parm_pack);
13012 else
13013 /* We can't rely on local_specializations for a parameter
13014 name used later in a function declaration (such as in a
13015 late-specified return type). Even if it exists, it might
13016 have the wrong value for a recursive call. */
13017 need_local_specializations = true;
13019 if (!arg_pack)
13021 /* This parameter pack was used in an unevaluated context. Just
13022 make a dummy decl, since it's only used for its type. */
13023 ++cp_unevaluated_operand;
13024 arg_pack = tsubst_decl (parm_pack, args, complain);
13025 --cp_unevaluated_operand;
13026 if (arg_pack && DECL_PACK_P (arg_pack))
13027 /* Partial instantiation of the parm_pack, we can't build
13028 up an argument pack yet. */
13029 arg_pack = NULL_TREE;
13030 else
13031 arg_pack = make_fnparm_pack (arg_pack);
13033 else if (DECL_PACK_P (arg_pack))
13034 /* This argument pack isn't fully instantiated yet. */
13035 arg_pack = NULL_TREE;
13037 else if (is_capture_proxy (parm_pack))
13039 arg_pack = retrieve_local_specialization (parm_pack);
13040 if (DECL_PACK_P (arg_pack))
13041 arg_pack = NULL_TREE;
13043 else
13045 int idx;
13046 template_parm_level_and_index (parm_pack, &level, &idx);
13047 if (level <= levels)
13048 arg_pack = TMPL_ARG (args, level, idx);
13050 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13051 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13052 arg_pack = NULL_TREE;
13055 orig_arg = arg_pack;
13056 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13057 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13059 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13060 /* This can only happen if we forget to expand an argument
13061 pack somewhere else. Just return an error, silently. */
13063 result = make_tree_vec (1);
13064 TREE_VEC_ELT (result, 0) = error_mark_node;
13065 return result;
13068 if (arg_pack)
13070 int my_len =
13071 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13073 /* Don't bother trying to do a partial substitution with
13074 incomplete packs; we'll try again after deduction. */
13075 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13076 return t;
13078 if (len < 0)
13079 len = my_len;
13080 else if (len != my_len)
13082 if (!(complain & tf_error))
13083 /* Fail quietly. */;
13084 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13085 error ("mismatched argument pack lengths while expanding %qT",
13086 pattern);
13087 else
13088 error ("mismatched argument pack lengths while expanding %qE",
13089 pattern);
13090 return error_mark_node;
13093 /* Keep track of the parameter packs and their corresponding
13094 argument packs. */
13095 packs = tree_cons (parm_pack, arg_pack, packs);
13096 TREE_TYPE (packs) = orig_arg;
13098 else
13100 /* We can't substitute for this parameter pack. We use a flag as
13101 well as the missing_level counter because function parameter
13102 packs don't have a level. */
13103 gcc_assert (processing_template_decl || is_auto (parm_pack));
13104 unsubstituted_packs = true;
13108 /* If the expansion is just T..., return the matching argument pack, unless
13109 we need to call convert_from_reference on all the elements. This is an
13110 important optimization; see c++/68422. */
13111 if (!unsubstituted_packs
13112 && TREE_PURPOSE (packs) == pattern)
13114 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13116 /* If the argument pack is a single pack expansion, pull it out. */
13117 if (TREE_VEC_LENGTH (args) == 1
13118 && pack_expansion_args_count (args))
13119 return TREE_VEC_ELT (args, 0);
13121 /* Types need no adjustment, nor does sizeof..., and if we still have
13122 some pack expansion args we won't do anything yet. */
13123 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13124 || PACK_EXPANSION_SIZEOF_P (t)
13125 || pack_expansion_args_count (args))
13126 return args;
13127 /* Also optimize expression pack expansions if we can tell that the
13128 elements won't have reference type. */
13129 tree type = TREE_TYPE (pattern);
13130 if (type && !TYPE_REF_P (type)
13131 && !PACK_EXPANSION_P (type)
13132 && !WILDCARD_TYPE_P (type))
13133 return args;
13134 /* Otherwise use the normal path so we get convert_from_reference. */
13137 /* We cannot expand this expansion expression, because we don't have
13138 all of the argument packs we need. */
13139 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13141 /* We got some full packs, but we can't substitute them in until we
13142 have values for all the packs. So remember these until then. */
13144 t = make_pack_expansion (pattern, complain);
13145 PACK_EXPANSION_EXTRA_ARGS (t)
13146 = build_extra_args (pattern, args, complain);
13147 return t;
13150 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13151 type, so create our own local specializations map; the current map is
13152 either NULL or (in the case of recursive unification) might have
13153 bindings that we don't want to use or alter. */
13154 local_specialization_stack lss (need_local_specializations
13155 ? lss_blank : lss_nop);
13157 if (unsubstituted_packs)
13159 /* There were no real arguments, we're just replacing a parameter
13160 pack with another version of itself. Substitute into the
13161 pattern and return a PACK_EXPANSION_*. The caller will need to
13162 deal with that. */
13163 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13164 result = tsubst_expr (pattern, args, complain, in_decl,
13165 /*integral_constant_expression_p=*/false);
13166 else
13167 result = tsubst (pattern, args, complain, in_decl);
13168 result = make_pack_expansion (result, complain);
13169 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13170 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13171 if (PACK_EXPANSION_AUTO_P (t))
13173 /* This is a fake auto... pack expansion created in add_capture with
13174 _PACKS that don't appear in the pattern. Copy one over. */
13175 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13176 pack = retrieve_local_specialization (TREE_VALUE (packs));
13177 gcc_checking_assert (DECL_PACK_P (pack));
13178 PACK_EXPANSION_PARAMETER_PACKS (result)
13179 = build_tree_list (NULL_TREE, pack);
13180 PACK_EXPANSION_AUTO_P (result) = true;
13182 return result;
13185 gcc_assert (len >= 0);
13187 /* For each argument in each argument pack, substitute into the
13188 pattern. */
13189 result = make_tree_vec (len);
13190 tree elem_args = copy_template_args (args);
13191 for (i = 0; i < len; ++i)
13193 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13195 elem_args, complain,
13196 in_decl);
13197 TREE_VEC_ELT (result, i) = t;
13198 if (t == error_mark_node)
13200 result = error_mark_node;
13201 break;
13205 /* Update ARGS to restore the substitution from parameter packs to
13206 their argument packs. */
13207 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13209 tree parm = TREE_PURPOSE (pack);
13211 if (TREE_CODE (parm) == PARM_DECL
13212 || VAR_P (parm)
13213 || TREE_CODE (parm) == FIELD_DECL)
13214 register_local_specialization (TREE_TYPE (pack), parm);
13215 else
13217 int idx, level;
13219 if (TREE_VALUE (pack) == NULL_TREE)
13220 continue;
13222 template_parm_level_and_index (parm, &level, &idx);
13224 /* Update the corresponding argument. */
13225 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13226 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13227 TREE_TYPE (pack);
13228 else
13229 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13233 /* If the dependent pack arguments were such that we end up with only a
13234 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13235 if (len == 1 && TREE_CODE (result) == TREE_VEC
13236 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13237 return TREE_VEC_ELT (result, 0);
13239 return result;
13242 /* Make an argument pack out of the TREE_VEC VEC. */
13244 static tree
13245 make_argument_pack (tree vec)
13247 tree pack;
13249 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13250 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13251 else
13253 pack = make_node (NONTYPE_ARGUMENT_PACK);
13254 TREE_CONSTANT (pack) = 1;
13256 SET_ARGUMENT_PACK_ARGS (pack, vec);
13257 return pack;
13260 /* Return an exact copy of template args T that can be modified
13261 independently. */
13263 static tree
13264 copy_template_args (tree t)
13266 if (t == error_mark_node)
13267 return t;
13269 int len = TREE_VEC_LENGTH (t);
13270 tree new_vec = make_tree_vec (len);
13272 for (int i = 0; i < len; ++i)
13274 tree elt = TREE_VEC_ELT (t, i);
13275 if (elt && TREE_CODE (elt) == TREE_VEC)
13276 elt = copy_template_args (elt);
13277 TREE_VEC_ELT (new_vec, i) = elt;
13280 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13281 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13283 return new_vec;
13286 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13288 tree
13289 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13290 tree in_decl)
13292 /* Substitute into each of the arguments. */
13293 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13294 args, complain, in_decl);
13295 tree new_arg = error_mark_node;
13296 if (pack_args != error_mark_node)
13298 if (TYPE_P (orig_arg))
13300 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13301 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13303 else
13305 new_arg = make_node (TREE_CODE (orig_arg));
13306 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13309 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13312 return new_arg;
13315 /* Substitute ARGS into the vector or list of template arguments T. */
13317 tree
13318 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13320 tree orig_t = t;
13321 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13322 tree *elts;
13324 if (t == error_mark_node)
13325 return error_mark_node;
13327 len = TREE_VEC_LENGTH (t);
13328 elts = XALLOCAVEC (tree, len);
13330 for (i = 0; i < len; i++)
13332 tree orig_arg = TREE_VEC_ELT (t, i);
13333 tree new_arg;
13335 if (!orig_arg)
13336 new_arg = NULL_TREE;
13337 else if (TREE_CODE (orig_arg) == TREE_VEC)
13338 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13339 else if (PACK_EXPANSION_P (orig_arg))
13341 /* Substitute into an expansion expression. */
13342 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13344 if (TREE_CODE (new_arg) == TREE_VEC)
13345 /* Add to the expanded length adjustment the number of
13346 expanded arguments. We subtract one from this
13347 measurement, because the argument pack expression
13348 itself is already counted as 1 in
13349 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13350 the argument pack is empty. */
13351 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13353 else if (ARGUMENT_PACK_P (orig_arg))
13354 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13355 else
13356 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13358 if (new_arg == error_mark_node)
13359 return error_mark_node;
13361 elts[i] = new_arg;
13362 if (new_arg != orig_arg)
13363 need_new = 1;
13366 if (!need_new)
13367 return t;
13369 /* Make space for the expanded arguments coming from template
13370 argument packs. */
13371 t = make_tree_vec (len + expanded_len_adjust);
13372 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13373 arguments for a member template.
13374 In that case each TREE_VEC in ORIG_T represents a level of template
13375 arguments, and ORIG_T won't carry any non defaulted argument count.
13376 It will rather be the nested TREE_VECs that will carry one.
13377 In other words, ORIG_T carries a non defaulted argument count only
13378 if it doesn't contain any nested TREE_VEC. */
13379 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13381 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13382 count += expanded_len_adjust;
13383 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13385 for (i = 0, out = 0; i < len; i++)
13387 tree orig_arg = TREE_VEC_ELT (orig_t, i);
13388 if (orig_arg
13389 && (PACK_EXPANSION_P (orig_arg) || ARGUMENT_PACK_P (orig_arg))
13390 && TREE_CODE (elts[i]) == TREE_VEC)
13392 int idx;
13394 /* Now expand the template argument pack "in place". */
13395 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13396 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13398 else
13400 TREE_VEC_ELT (t, out) = elts[i];
13401 out++;
13405 return t;
13408 /* Substitute ARGS into one level PARMS of template parameters. */
13410 static tree
13411 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13413 if (parms == error_mark_node)
13414 return error_mark_node;
13416 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13418 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13420 tree tuple = TREE_VEC_ELT (parms, i);
13422 if (tuple == error_mark_node)
13423 continue;
13425 TREE_VEC_ELT (new_vec, i) =
13426 tsubst_template_parm (tuple, args, complain);
13429 return new_vec;
13432 /* Return the result of substituting ARGS into the template parameters
13433 given by PARMS. If there are m levels of ARGS and m + n levels of
13434 PARMS, then the result will contain n levels of PARMS. For
13435 example, if PARMS is `template <class T> template <class U>
13436 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13437 result will be `template <int*, double, class V>'. */
13439 static tree
13440 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13442 tree r = NULL_TREE;
13443 tree* new_parms;
13445 /* When substituting into a template, we must set
13446 PROCESSING_TEMPLATE_DECL as the template parameters may be
13447 dependent if they are based on one-another, and the dependency
13448 predicates are short-circuit outside of templates. */
13449 ++processing_template_decl;
13451 for (new_parms = &r;
13452 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13453 new_parms = &(TREE_CHAIN (*new_parms)),
13454 parms = TREE_CHAIN (parms))
13456 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13457 args, complain);
13458 *new_parms =
13459 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13460 - TMPL_ARGS_DEPTH (args)),
13461 new_vec, NULL_TREE);
13462 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13463 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13466 --processing_template_decl;
13468 return r;
13471 /* Return the result of substituting ARGS into one template parameter
13472 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13473 parameter and which TREE_PURPOSE is the default argument of the
13474 template parameter. */
13476 static tree
13477 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13479 tree default_value, parm_decl;
13481 if (args == NULL_TREE
13482 || t == NULL_TREE
13483 || t == error_mark_node)
13484 return t;
13486 gcc_assert (TREE_CODE (t) == TREE_LIST);
13488 default_value = TREE_PURPOSE (t);
13489 parm_decl = TREE_VALUE (t);
13490 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13492 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13493 if (TREE_CODE (parm_decl) == PARM_DECL
13494 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13495 parm_decl = error_mark_node;
13496 default_value = tsubst_template_arg (default_value, args,
13497 complain, NULL_TREE);
13498 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13500 tree r = build_tree_list (default_value, parm_decl);
13501 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13502 return r;
13505 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13506 type T. If T is not an aggregate or enumeration type, it is
13507 handled as if by tsubst. IN_DECL is as for tsubst. If
13508 ENTERING_SCOPE is nonzero, T is the context for a template which
13509 we are presently tsubst'ing. Return the substituted value. */
13511 static tree
13512 tsubst_aggr_type (tree t,
13513 tree args,
13514 tsubst_flags_t complain,
13515 tree in_decl,
13516 int entering_scope)
13518 if (t == NULL_TREE)
13519 return NULL_TREE;
13521 switch (TREE_CODE (t))
13523 case RECORD_TYPE:
13524 if (TYPE_PTRMEMFUNC_P (t))
13525 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13527 /* Fall through. */
13528 case ENUMERAL_TYPE:
13529 case UNION_TYPE:
13530 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13532 tree argvec;
13533 tree context;
13534 tree r;
13536 /* In "sizeof(X<I>)" we need to evaluate "I". */
13537 cp_evaluated ev;
13539 /* First, determine the context for the type we are looking
13540 up. */
13541 context = TYPE_CONTEXT (t);
13542 if (context && TYPE_P (context))
13544 context = tsubst_aggr_type (context, args, complain,
13545 in_decl, /*entering_scope=*/1);
13546 /* If context is a nested class inside a class template,
13547 it may still need to be instantiated (c++/33959). */
13548 context = complete_type (context);
13551 /* Then, figure out what arguments are appropriate for the
13552 type we are trying to find. For example, given:
13554 template <class T> struct S;
13555 template <class T, class U> void f(T, U) { S<U> su; }
13557 and supposing that we are instantiating f<int, double>,
13558 then our ARGS will be {int, double}, but, when looking up
13559 S we only want {double}. */
13560 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13561 complain, in_decl);
13562 if (argvec == error_mark_node)
13563 r = error_mark_node;
13564 else if (!entering_scope
13565 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13567 /* See maybe_dependent_member_ref. */
13568 tree name = TYPE_IDENTIFIER (t);
13569 tree fullname = name;
13570 if (instantiates_primary_template_p (t))
13571 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13572 INNERMOST_TEMPLATE_ARGS (argvec));
13573 return build_typename_type (context, name, fullname,
13574 typename_type);
13576 else
13578 r = lookup_template_class (t, argvec, in_decl, context,
13579 entering_scope, complain);
13580 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13583 return r;
13585 else
13586 /* This is not a template type, so there's nothing to do. */
13587 return t;
13589 default:
13590 return tsubst (t, args, complain, in_decl);
13594 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13596 /* Substitute into the default argument ARG (a default argument for
13597 FN), which has the indicated TYPE. */
13599 tree
13600 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13601 tsubst_flags_t complain)
13603 int errs = errorcount + sorrycount;
13605 /* This can happen in invalid code. */
13606 if (TREE_CODE (arg) == DEFERRED_PARSE)
13607 return arg;
13609 /* Shortcut {}. */
13610 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13611 && CONSTRUCTOR_NELTS (arg) == 0)
13612 return arg;
13614 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13615 parm = chain_index (parmnum, parm);
13616 tree parmtype = TREE_TYPE (parm);
13617 if (DECL_BY_REFERENCE (parm))
13618 parmtype = TREE_TYPE (parmtype);
13619 if (parmtype == error_mark_node)
13620 return error_mark_node;
13622 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13624 tree *slot;
13625 if (defarg_inst && (slot = defarg_inst->get (parm)))
13626 return *slot;
13628 /* This default argument came from a template. Instantiate the
13629 default argument here, not in tsubst. In the case of
13630 something like:
13632 template <class T>
13633 struct S {
13634 static T t();
13635 void f(T = t());
13638 we must be careful to do name lookup in the scope of S<T>,
13639 rather than in the current class. */
13640 push_to_top_level ();
13641 push_access_scope (fn);
13642 push_deferring_access_checks (dk_no_deferred);
13643 start_lambda_scope (parm);
13645 /* The default argument expression may cause implicitly defined
13646 member functions to be synthesized, which will result in garbage
13647 collection. We must treat this situation as if we were within
13648 the body of function so as to avoid collecting live data on the
13649 stack. */
13650 ++function_depth;
13651 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13652 complain, NULL_TREE,
13653 /*integral_constant_expression_p=*/false);
13654 --function_depth;
13656 finish_lambda_scope ();
13658 /* Make sure the default argument is reasonable. */
13659 arg = check_default_argument (type, arg, complain);
13661 if (errorcount+sorrycount > errs
13662 && (complain & tf_warning_or_error))
13663 inform (input_location,
13664 " when instantiating default argument for call to %qD", fn);
13666 pop_deferring_access_checks ();
13667 pop_access_scope (fn);
13668 pop_from_top_level ();
13670 if (arg != error_mark_node && !cp_unevaluated_operand)
13672 if (!defarg_inst)
13673 defarg_inst = decl_tree_cache_map::create_ggc (37);
13674 defarg_inst->put (parm, arg);
13677 return arg;
13680 /* Substitute into all the default arguments for FN. */
13682 static void
13683 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13685 tree arg;
13686 tree tmpl_args;
13688 tmpl_args = DECL_TI_ARGS (fn);
13690 /* If this function is not yet instantiated, we certainly don't need
13691 its default arguments. */
13692 if (uses_template_parms (tmpl_args))
13693 return;
13694 /* Don't do this again for clones. */
13695 if (DECL_CLONED_FUNCTION_P (fn))
13696 return;
13698 int i = 0;
13699 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13700 arg;
13701 arg = TREE_CHAIN (arg), ++i)
13702 if (TREE_PURPOSE (arg))
13703 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13704 TREE_VALUE (arg),
13705 TREE_PURPOSE (arg),
13706 complain);
13709 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13710 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13712 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13714 void
13715 store_explicit_specifier (tree v, tree t)
13717 if (!explicit_specifier_map)
13718 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13719 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13720 explicit_specifier_map->put (v, t);
13723 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13725 static tree
13726 lookup_explicit_specifier (tree v)
13728 return *explicit_specifier_map->get (v);
13731 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13732 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13733 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13734 identical to T. */
13736 static tree
13737 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13738 tree raises, tsubst_flags_t complain)
13740 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13742 tree new_type;
13743 if (TREE_CODE (t) == FUNCTION_TYPE)
13745 new_type = build_function_type (return_type, arg_types);
13746 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13748 else
13750 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13751 /* Don't pick up extra function qualifiers from the basetype. */
13752 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13753 if (! MAYBE_CLASS_TYPE_P (r))
13755 /* [temp.deduct]
13757 Type deduction may fail for any of the following
13758 reasons:
13760 -- Attempting to create "pointer to member of T" when T
13761 is not a class type. */
13762 if (complain & tf_error)
13763 error ("creating pointer to member function of non-class type %qT",
13765 return error_mark_node;
13768 new_type = build_method_type_directly (r, return_type,
13769 TREE_CHAIN (arg_types));
13771 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13773 cp_ref_qualifier rqual = type_memfn_rqual (t);
13774 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13775 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13778 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13779 each of its formal parameters. If there is a disagreement then rebuild
13780 DECL's function type according to its formal parameter types, as part of a
13781 resolution for Core issues 1001/1322. */
13783 static void
13784 maybe_rebuild_function_decl_type (tree decl)
13786 bool function_type_needs_rebuilding = false;
13787 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13789 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13790 while (parm_type_list && parm_type_list != void_list_node)
13792 tree parm_type = TREE_VALUE (parm_type_list);
13793 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13794 if (!same_type_p (parm_type, formal_parm_type_unqual))
13796 function_type_needs_rebuilding = true;
13797 break;
13800 parm_list = DECL_CHAIN (parm_list);
13801 parm_type_list = TREE_CHAIN (parm_type_list);
13805 if (!function_type_needs_rebuilding)
13806 return;
13808 const tree fntype = TREE_TYPE (decl);
13809 tree parm_list = DECL_ARGUMENTS (decl);
13810 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13811 tree new_parm_type_list = NULL_TREE;
13812 tree *q = &new_parm_type_list;
13813 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13815 *q = copy_node (old_parm_type_list);
13816 parm_list = DECL_CHAIN (parm_list);
13817 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13818 q = &TREE_CHAIN (*q);
13820 while (old_parm_type_list && old_parm_type_list != void_list_node)
13822 *q = copy_node (old_parm_type_list);
13823 tree *new_parm_type = &TREE_VALUE (*q);
13824 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13825 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13826 *new_parm_type = formal_parm_type_unqual;
13828 parm_list = DECL_CHAIN (parm_list);
13829 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13830 q = &TREE_CHAIN (*q);
13832 if (old_parm_type_list == void_list_node)
13833 *q = void_list_node;
13835 TREE_TYPE (decl)
13836 = rebuild_function_or_method_type (fntype,
13837 TREE_TYPE (fntype), new_parm_type_list,
13838 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13841 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13843 static tree
13844 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13845 tree lambda_fntype)
13847 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13848 hashval_t hash = 0;
13849 tree in_decl = t;
13851 /* Nobody should be tsubst'ing into non-template functions. */
13852 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13853 || DECL_LOCAL_DECL_P (t));
13855 if (DECL_LOCAL_DECL_P (t))
13857 if (tree spec = retrieve_local_specialization (t))
13858 return spec;
13860 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13862 /* If T is not dependent, just return it. */
13863 if (!uses_template_parms (DECL_TI_ARGS (t))
13864 && !LAMBDA_FUNCTION_P (t))
13865 return t;
13867 /* Calculate the most general template of which R is a
13868 specialization. */
13869 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13871 /* We're substituting a lambda function under tsubst_lambda_expr but not
13872 directly from it; find the matching function we're already inside.
13873 But don't do this if T is a generic lambda with a single level of
13874 template parms, as in that case we're doing a normal instantiation. */
13875 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13876 && (!generic_lambda_fn_p (t)
13877 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13878 return enclosing_instantiation_of (t);
13880 /* Calculate the complete set of arguments used to
13881 specialize R. */
13882 argvec = tsubst_template_args (DECL_TI_ARGS
13883 (DECL_TEMPLATE_RESULT
13884 (DECL_TI_TEMPLATE (t))),
13885 args, complain, in_decl);
13886 if (argvec == error_mark_node)
13887 return error_mark_node;
13889 /* Check to see if we already have this specialization. */
13890 if (!lambda_fntype)
13892 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13893 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13894 return spec;
13897 else
13899 /* This special case arises when we have something like this:
13901 template <class T> struct S {
13902 friend void f<int>(int, double);
13905 Here, the DECL_TI_TEMPLATE for the friend declaration
13906 will be an IDENTIFIER_NODE. We are being called from
13907 tsubst_friend_function, and we want only to create a
13908 new decl (R) with appropriate types so that we can call
13909 determine_specialization. */
13910 gen_tmpl = NULL_TREE;
13911 argvec = NULL_TREE;
13914 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13915 : NULL_TREE);
13916 tree ctx = closure ? closure : DECL_CONTEXT (t);
13917 bool member = ctx && TYPE_P (ctx);
13919 if (member && !closure)
13920 ctx = tsubst_aggr_type (ctx, args,
13921 complain, t, /*entering_scope=*/1);
13923 tree type = (lambda_fntype ? lambda_fntype
13924 : tsubst (TREE_TYPE (t), args,
13925 complain | tf_fndecl_type, in_decl));
13926 if (type == error_mark_node)
13927 return error_mark_node;
13929 /* If we hit excessive deduction depth, the type is bogus even if
13930 it isn't error_mark_node, so don't build a decl. */
13931 if (excessive_deduction_depth)
13932 return error_mark_node;
13934 /* We do NOT check for matching decls pushed separately at this
13935 point, as they may not represent instantiations of this
13936 template, and in any case are considered separate under the
13937 discrete model. */
13938 tree r = copy_decl (t);
13939 DECL_USE_TEMPLATE (r) = 0;
13940 TREE_TYPE (r) = type;
13941 /* Clear out the mangled name and RTL for the instantiation. */
13942 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13943 SET_DECL_RTL (r, NULL);
13944 /* Leave DECL_INITIAL set on deleted instantiations. */
13945 if (!DECL_DELETED_FN (r))
13946 DECL_INITIAL (r) = NULL_TREE;
13947 DECL_CONTEXT (r) = ctx;
13948 set_instantiating_module (r);
13950 /* Handle explicit(dependent-expr). */
13951 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13953 tree spec = lookup_explicit_specifier (t);
13954 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13955 /*function_p=*/false,
13956 /*i_c_e_p=*/true);
13957 spec = build_explicit_specifier (spec, complain);
13958 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13961 /* OpenMP UDRs have the only argument a reference to the declared
13962 type. We want to diagnose if the declared type is a reference,
13963 which is invalid, but as references to references are usually
13964 quietly merged, diagnose it here. */
13965 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13967 tree argtype
13968 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13969 argtype = tsubst (argtype, args, complain, in_decl);
13970 if (TYPE_REF_P (argtype))
13971 error_at (DECL_SOURCE_LOCATION (t),
13972 "reference type %qT in "
13973 "%<#pragma omp declare reduction%>", argtype);
13974 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13975 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13976 argtype);
13979 if (member && DECL_CONV_FN_P (r))
13980 /* Type-conversion operator. Reconstruct the name, in
13981 case it's the name of one of the template's parameters. */
13982 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13984 tree parms = DECL_ARGUMENTS (t);
13985 if (closure)
13986 parms = DECL_CHAIN (parms);
13987 parms = tsubst (parms, args, complain, t);
13988 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13989 DECL_CONTEXT (parm) = r;
13990 if (closure)
13992 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13993 DECL_NAME (tparm) = closure_identifier;
13994 DECL_CHAIN (tparm) = parms;
13995 parms = tparm;
13997 DECL_ARGUMENTS (r) = parms;
13998 DECL_RESULT (r) = NULL_TREE;
14000 maybe_rebuild_function_decl_type (r);
14002 TREE_STATIC (r) = 0;
14003 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14004 DECL_EXTERNAL (r) = 1;
14005 /* If this is an instantiation of a function with internal
14006 linkage, we already know what object file linkage will be
14007 assigned to the instantiation. */
14008 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14009 DECL_DEFER_OUTPUT (r) = 0;
14010 DECL_CHAIN (r) = NULL_TREE;
14011 DECL_PENDING_INLINE_INFO (r) = 0;
14012 DECL_PENDING_INLINE_P (r) = 0;
14013 DECL_SAVED_TREE (r) = NULL_TREE;
14014 DECL_STRUCT_FUNCTION (r) = NULL;
14015 TREE_USED (r) = 0;
14016 /* We'll re-clone as appropriate in instantiate_template. */
14017 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14019 /* If we aren't complaining now, return on error before we register
14020 the specialization so that we'll complain eventually. */
14021 if ((complain & tf_error) == 0
14022 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14023 && !grok_op_properties (r, /*complain=*/false))
14024 return error_mark_node;
14026 /* Associate the constraints directly with the instantiation. We
14027 don't substitute through the constraints; that's only done when
14028 they are checked. */
14029 if (tree ci = get_constraints (t))
14030 set_constraints (r, ci);
14032 if (DECL_FRIEND_CONTEXT (t))
14033 SET_DECL_FRIEND_CONTEXT (r,
14034 tsubst (DECL_FRIEND_CONTEXT (t),
14035 args, complain, in_decl));
14037 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14038 args, complain, in_decl))
14039 return error_mark_node;
14041 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14042 this in the special friend case mentioned above where
14043 GEN_TMPL is NULL. */
14044 if (gen_tmpl && !closure)
14046 DECL_TEMPLATE_INFO (r)
14047 = build_template_info (gen_tmpl, argvec);
14048 SET_DECL_IMPLICIT_INSTANTIATION (r);
14050 tree new_r
14051 = register_specialization (r, gen_tmpl, argvec, false, hash);
14052 if (new_r != r)
14053 /* We instantiated this while substituting into
14054 the type earlier (template/friend54.C). */
14055 return new_r;
14057 /* We're not supposed to instantiate default arguments
14058 until they are called, for a template. But, for a
14059 declaration like:
14061 template <class T> void f ()
14062 { extern void g(int i = T()); }
14064 we should do the substitution when the template is
14065 instantiated. We handle the member function case in
14066 instantiate_class_template since the default arguments
14067 might refer to other members of the class. */
14068 if (!member
14069 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14070 && !uses_template_parms (argvec))
14071 tsubst_default_arguments (r, complain);
14073 else if (DECL_LOCAL_DECL_P (r))
14075 if (!cp_unevaluated_operand)
14076 register_local_specialization (r, t);
14078 else
14079 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14081 /* Copy the list of befriending classes. */
14082 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14083 *friends;
14084 friends = &TREE_CHAIN (*friends))
14086 *friends = copy_node (*friends);
14087 TREE_VALUE (*friends)
14088 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14091 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14093 maybe_retrofit_in_chrg (r);
14094 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14095 return error_mark_node;
14096 /* If this is an instantiation of a member template, clone it.
14097 If it isn't, that'll be handled by
14098 clone_constructors_and_destructors. */
14099 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14100 clone_cdtor (r, /*update_methods=*/false);
14102 else if ((complain & tf_error) != 0
14103 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14104 && !grok_op_properties (r, /*complain=*/true))
14105 return error_mark_node;
14107 /* Possibly limit visibility based on template args. */
14108 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14109 if (DECL_VISIBILITY_SPECIFIED (t))
14111 DECL_VISIBILITY_SPECIFIED (r) = 0;
14112 DECL_ATTRIBUTES (r)
14113 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14115 determine_visibility (r);
14116 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14117 && !processing_template_decl)
14118 defaulted_late_check (r);
14120 if (flag_openmp)
14121 if (tree attr = lookup_attribute ("omp declare variant base",
14122 DECL_ATTRIBUTES (r)))
14123 omp_declare_variant_finalize (r, attr);
14125 return r;
14128 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14130 static tree
14131 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14132 tree lambda_fntype)
14134 /* We can get here when processing a member function template,
14135 member class template, or template template parameter. */
14136 tree decl = DECL_TEMPLATE_RESULT (t);
14137 tree in_decl = t;
14138 tree spec;
14139 tree tmpl_args;
14140 tree full_args;
14141 tree r;
14142 hashval_t hash = 0;
14144 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14146 /* Template template parameter is treated here. */
14147 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14148 if (new_type == error_mark_node)
14149 r = error_mark_node;
14150 /* If we get a real template back, return it. This can happen in
14151 the context of most_specialized_partial_spec. */
14152 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14153 r = new_type;
14154 else
14155 /* The new TEMPLATE_DECL was built in
14156 reduce_template_parm_level. */
14157 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14158 return r;
14161 if (!lambda_fntype)
14163 /* We might already have an instance of this template.
14164 The ARGS are for the surrounding class type, so the
14165 full args contain the tsubst'd args for the context,
14166 plus the innermost args from the template decl. */
14167 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14168 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14169 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14170 /* Because this is a template, the arguments will still be
14171 dependent, even after substitution. If
14172 PROCESSING_TEMPLATE_DECL is not set, the dependency
14173 predicates will short-circuit. */
14174 ++processing_template_decl;
14175 full_args = tsubst_template_args (tmpl_args, args,
14176 complain, in_decl);
14177 --processing_template_decl;
14178 if (full_args == error_mark_node)
14179 return error_mark_node;
14181 /* If this is a default template template argument,
14182 tsubst might not have changed anything. */
14183 if (full_args == tmpl_args)
14184 return t;
14186 hash = hash_tmpl_and_args (t, full_args);
14187 spec = retrieve_specialization (t, full_args, hash);
14188 if (spec != NULL_TREE)
14190 if (TYPE_P (spec))
14191 /* Type partial instantiations are stored as the type by
14192 lookup_template_class_1, not here as the template. */
14193 spec = CLASSTYPE_TI_TEMPLATE (spec);
14194 return spec;
14198 /* Make a new template decl. It will be similar to the
14199 original, but will record the current template arguments.
14200 We also create a new function declaration, which is just
14201 like the old one, but points to this new template, rather
14202 than the old one. */
14203 r = copy_decl (t);
14204 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14205 DECL_CHAIN (r) = NULL_TREE;
14207 // Build new template info linking to the original template decl.
14208 if (!lambda_fntype)
14210 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14211 SET_DECL_IMPLICIT_INSTANTIATION (r);
14213 else
14214 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14216 /* The template parameters for this new template are all the
14217 template parameters for the old template, except the
14218 outermost level of parameters. */
14219 auto tparm_guard = make_temp_override (current_template_parms);
14220 DECL_TEMPLATE_PARMS (r)
14221 = current_template_parms
14222 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14223 complain);
14225 bool class_p = false;
14226 tree inner = decl;
14227 ++processing_template_decl;
14228 if (TREE_CODE (inner) == FUNCTION_DECL)
14229 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14230 else
14232 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14234 class_p = true;
14235 inner = TREE_TYPE (inner);
14237 if (class_p)
14238 inner = tsubst_aggr_type (inner, args, complain,
14239 in_decl, /*entering*/1);
14240 else
14241 inner = tsubst (inner, args, complain, in_decl);
14243 --processing_template_decl;
14244 if (inner == error_mark_node)
14245 return error_mark_node;
14247 if (class_p)
14249 /* For a partial specialization, we need to keep pointing to
14250 the primary template. */
14251 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14252 CLASSTYPE_TI_TEMPLATE (inner) = r;
14254 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14255 inner = TYPE_MAIN_DECL (inner);
14257 else if (lambda_fntype)
14259 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14260 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14262 else
14264 DECL_TI_TEMPLATE (inner) = r;
14265 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14268 DECL_TEMPLATE_RESULT (r) = inner;
14269 TREE_TYPE (r) = TREE_TYPE (inner);
14270 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14272 if (modules_p ())
14274 /* Propagate module information from the decl. */
14275 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14276 if (DECL_LANG_SPECIFIC (inner))
14277 /* If this is a constrained template, the above tsubst of
14278 inner can find the unconstrained template, which may have
14279 come from an import. This is ok, because we don't
14280 register this instantiation (see below). */
14281 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14282 || (TEMPLATE_PARMS_CONSTRAINTS
14283 (DECL_TEMPLATE_PARMS (t))));
14286 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14287 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14289 if (PRIMARY_TEMPLATE_P (t))
14290 DECL_PRIMARY_TEMPLATE (r) = r;
14292 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14293 /* Record this non-type partial instantiation. */
14294 register_specialization (r, t,
14295 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14296 false, hash);
14298 return r;
14301 /* True if FN is the op() for a lambda in an uninstantiated template. */
14303 bool
14304 lambda_fn_in_template_p (tree fn)
14306 if (!fn || !LAMBDA_FUNCTION_P (fn))
14307 return false;
14308 tree closure = DECL_CONTEXT (fn);
14309 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14312 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14313 which the above is true. */
14315 bool
14316 regenerated_lambda_fn_p (tree fn)
14318 if (!fn || !LAMBDA_FUNCTION_P (fn))
14319 return false;
14320 tree closure = DECL_CONTEXT (fn);
14321 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14322 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
14325 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14326 If T is not a regenerated LAMBDA_EXPR, return T. */
14328 tree
14329 most_general_lambda (tree t)
14331 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14332 t = TI_TEMPLATE (ti);
14333 return t;
14336 /* We're instantiating a variable from template function TCTX. Return the
14337 corresponding current enclosing scope. We can match them up using
14338 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14339 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14340 the template definition in regenerate_decl_from_template. */
14342 static tree
14343 enclosing_instantiation_of (tree tctx)
14345 tree fn = current_function_decl;
14347 /* We shouldn't ever need to do this for other artificial functions. */
14348 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
14350 for (; fn; fn = decl_function_context (fn))
14351 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
14352 return fn;
14353 gcc_unreachable ();
14356 /* Substitute the ARGS into the T, which is a _DECL. Return the
14357 result of the substitution. Issue error and warning messages under
14358 control of COMPLAIN. */
14360 static tree
14361 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14363 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14364 location_t saved_loc;
14365 tree r = NULL_TREE;
14366 tree in_decl = t;
14367 hashval_t hash = 0;
14369 /* Set the filename and linenumber to improve error-reporting. */
14370 saved_loc = input_location;
14371 input_location = DECL_SOURCE_LOCATION (t);
14373 switch (TREE_CODE (t))
14375 case TEMPLATE_DECL:
14376 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14377 break;
14379 case FUNCTION_DECL:
14380 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14381 break;
14383 case PARM_DECL:
14385 tree type = NULL_TREE;
14386 int i, len = 1;
14387 tree expanded_types = NULL_TREE;
14388 tree prev_r = NULL_TREE;
14389 tree first_r = NULL_TREE;
14391 if (DECL_PACK_P (t))
14393 /* If there is a local specialization that isn't a
14394 parameter pack, it means that we're doing a "simple"
14395 substitution from inside tsubst_pack_expansion. Just
14396 return the local specialization (which will be a single
14397 parm). */
14398 tree spec = retrieve_local_specialization (t);
14399 if (spec
14400 && TREE_CODE (spec) == PARM_DECL
14401 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14402 RETURN (spec);
14404 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14405 the parameters in this function parameter pack. */
14406 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14407 complain, in_decl);
14408 if (TREE_CODE (expanded_types) == TREE_VEC)
14410 len = TREE_VEC_LENGTH (expanded_types);
14412 /* Zero-length parameter packs are boring. Just substitute
14413 into the chain. */
14414 if (len == 0 && !cp_unevaluated_operand)
14415 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14416 TREE_CHAIN (t)));
14418 else
14420 /* All we did was update the type. Make a note of that. */
14421 type = expanded_types;
14422 expanded_types = NULL_TREE;
14426 /* Loop through all of the parameters we'll build. When T is
14427 a function parameter pack, LEN is the number of expanded
14428 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14429 r = NULL_TREE;
14430 for (i = 0; i < len; ++i)
14432 prev_r = r;
14433 r = copy_node (t);
14434 if (DECL_TEMPLATE_PARM_P (t))
14435 SET_DECL_TEMPLATE_PARM_P (r);
14437 if (expanded_types)
14438 /* We're on the Ith parameter of the function parameter
14439 pack. */
14441 /* Get the Ith type. */
14442 type = TREE_VEC_ELT (expanded_types, i);
14444 /* Rename the parameter to include the index. */
14445 DECL_NAME (r)
14446 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14448 else if (!type)
14449 /* We're dealing with a normal parameter. */
14450 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14452 type = type_decays_to (type);
14453 TREE_TYPE (r) = type;
14454 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14456 if (DECL_INITIAL (r))
14458 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14459 DECL_INITIAL (r) = TREE_TYPE (r);
14460 else
14461 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14462 complain, in_decl);
14465 DECL_CONTEXT (r) = NULL_TREE;
14467 if (!DECL_TEMPLATE_PARM_P (r))
14468 DECL_ARG_TYPE (r) = type_passed_as (type);
14470 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14471 args, complain, in_decl))
14472 return error_mark_node;
14474 /* Keep track of the first new parameter we
14475 generate. That's what will be returned to the
14476 caller. */
14477 if (!first_r)
14478 first_r = r;
14480 /* Build a proper chain of parameters when substituting
14481 into a function parameter pack. */
14482 if (prev_r)
14483 DECL_CHAIN (prev_r) = r;
14486 /* If cp_unevaluated_operand is set, we're just looking for a
14487 single dummy parameter, so don't keep going. */
14488 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14489 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14490 complain, DECL_CHAIN (t));
14492 /* FIRST_R contains the start of the chain we've built. */
14493 r = first_r;
14495 break;
14497 case FIELD_DECL:
14499 tree type = NULL_TREE;
14500 tree vec = NULL_TREE;
14501 tree expanded_types = NULL_TREE;
14502 int len = 1;
14504 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14506 /* This field is a lambda capture pack. Return a TREE_VEC of
14507 the expanded fields to instantiate_class_template_1. */
14508 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14509 complain, in_decl);
14510 if (TREE_CODE (expanded_types) == TREE_VEC)
14512 len = TREE_VEC_LENGTH (expanded_types);
14513 vec = make_tree_vec (len);
14515 else
14517 /* All we did was update the type. Make a note of that. */
14518 type = expanded_types;
14519 expanded_types = NULL_TREE;
14523 for (int i = 0; i < len; ++i)
14525 r = copy_decl (t);
14526 if (expanded_types)
14528 type = TREE_VEC_ELT (expanded_types, i);
14529 DECL_NAME (r)
14530 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14532 else if (!type)
14533 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14535 if (type == error_mark_node)
14536 RETURN (error_mark_node);
14537 TREE_TYPE (r) = type;
14538 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14540 if (DECL_C_BIT_FIELD (r))
14541 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14542 number of bits. */
14543 DECL_BIT_FIELD_REPRESENTATIVE (r)
14544 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14545 complain, in_decl,
14546 /*integral_constant_expression_p=*/true);
14547 if (DECL_INITIAL (t))
14549 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14550 NSDMI in perform_member_init. Still set DECL_INITIAL
14551 so that we know there is one. */
14552 DECL_INITIAL (r) = void_node;
14553 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14554 retrofit_lang_decl (r);
14555 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14557 /* We don't have to set DECL_CONTEXT here; it is set by
14558 finish_member_declaration. */
14559 DECL_CHAIN (r) = NULL_TREE;
14561 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14562 args, complain, in_decl))
14563 return error_mark_node;
14565 if (vec)
14566 TREE_VEC_ELT (vec, i) = r;
14569 if (vec)
14570 r = vec;
14572 break;
14574 case USING_DECL:
14575 /* We reach here only for member using decls. We also need to check
14576 uses_template_parms because DECL_DEPENDENT_P is not set for a
14577 using-declaration that designates a member of the current
14578 instantiation (c++/53549). */
14579 if (DECL_DEPENDENT_P (t)
14580 || uses_template_parms (USING_DECL_SCOPE (t)))
14582 tree scope = USING_DECL_SCOPE (t);
14583 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14584 if (PACK_EXPANSION_P (scope))
14586 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14587 int len = TREE_VEC_LENGTH (vec);
14588 r = make_tree_vec (len);
14589 for (int i = 0; i < len; ++i)
14591 tree escope = TREE_VEC_ELT (vec, i);
14592 tree elt = do_class_using_decl (escope, name);
14593 if (!elt)
14595 r = error_mark_node;
14596 break;
14598 else
14600 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14601 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14603 TREE_VEC_ELT (r, i) = elt;
14606 else
14608 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14609 complain, in_decl);
14610 r = do_class_using_decl (inst_scope, name);
14611 if (!r)
14612 r = error_mark_node;
14613 else
14615 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14616 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14620 else
14622 r = copy_node (t);
14623 DECL_CHAIN (r) = NULL_TREE;
14625 break;
14627 case TYPE_DECL:
14628 case VAR_DECL:
14630 tree argvec = NULL_TREE;
14631 tree gen_tmpl = NULL_TREE;
14632 tree tmpl = NULL_TREE;
14633 tree type = NULL_TREE;
14635 if (TREE_TYPE (t) == error_mark_node)
14636 RETURN (error_mark_node);
14638 if (TREE_CODE (t) == TYPE_DECL
14639 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14641 /* If this is the canonical decl, we don't have to
14642 mess with instantiations, and often we can't (for
14643 typename, template type parms and such). Note that
14644 TYPE_NAME is not correct for the above test if
14645 we've copied the type for a typedef. */
14646 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14647 if (type == error_mark_node)
14648 RETURN (error_mark_node);
14649 r = TYPE_NAME (type);
14650 break;
14653 /* Check to see if we already have the specialization we
14654 need. */
14655 tree spec = NULL_TREE;
14656 bool local_p = false;
14657 tree ctx = DECL_CONTEXT (t);
14658 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14659 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14661 local_p = false;
14662 if (DECL_CLASS_SCOPE_P (t))
14664 ctx = tsubst_aggr_type (ctx, args,
14665 complain,
14666 in_decl, /*entering_scope=*/1);
14667 /* If CTX is unchanged, then T is in fact the
14668 specialization we want. That situation occurs when
14669 referencing a static data member within in its own
14670 class. We can use pointer equality, rather than
14671 same_type_p, because DECL_CONTEXT is always
14672 canonical... */
14673 if (ctx == DECL_CONTEXT (t)
14674 /* ... unless T is a member template; in which
14675 case our caller can be willing to create a
14676 specialization of that template represented
14677 by T. */
14678 && !(DECL_TI_TEMPLATE (t)
14679 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14680 spec = t;
14683 if (!spec)
14685 tmpl = DECL_TI_TEMPLATE (t);
14686 gen_tmpl = most_general_template (tmpl);
14687 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14688 if (argvec != error_mark_node)
14689 argvec = (coerce_innermost_template_parms
14690 (DECL_TEMPLATE_PARMS (gen_tmpl),
14691 argvec, t, complain,
14692 /*all*/true, /*defarg*/true));
14693 if (argvec == error_mark_node)
14694 RETURN (error_mark_node);
14695 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14696 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14699 else
14701 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14702 /* Subsequent calls to pushdecl will fill this in. */
14703 ctx = NULL_TREE;
14704 /* A local variable. */
14705 local_p = true;
14706 /* Unless this is a reference to a static variable from an
14707 enclosing function, in which case we need to fill it in now. */
14708 if (TREE_STATIC (t))
14710 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14711 if (fn != current_function_decl)
14712 ctx = fn;
14714 spec = retrieve_local_specialization (t);
14716 /* If we already have the specialization we need, there is
14717 nothing more to do. */
14718 if (spec)
14720 r = spec;
14721 break;
14724 /* Create a new node for the specialization we need. */
14725 if (type == NULL_TREE)
14727 if (is_typedef_decl (t))
14728 type = DECL_ORIGINAL_TYPE (t);
14729 else
14730 type = TREE_TYPE (t);
14731 if (VAR_P (t)
14732 && VAR_HAD_UNKNOWN_BOUND (t)
14733 && type != error_mark_node)
14734 type = strip_array_domain (type);
14735 tree sub_args = args;
14736 if (tree auto_node = type_uses_auto (type))
14738 /* Mask off any template args past the variable's context so we
14739 don't replace the auto with an unrelated argument. */
14740 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14741 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14742 if (extra > 0)
14743 /* This should never happen with the new lambda instantiation
14744 model, but keep the handling just in case. */
14745 gcc_assert (!CHECKING_P),
14746 sub_args = strip_innermost_template_args (args, extra);
14748 type = tsubst (type, sub_args, complain, in_decl);
14749 /* Substituting the type might have recursively instantiated this
14750 same alias (c++/86171). */
14751 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14752 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14754 r = spec;
14755 break;
14758 r = copy_decl (t);
14759 if (VAR_P (r))
14761 DECL_INITIALIZED_P (r) = 0;
14762 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14763 if (type == error_mark_node)
14764 RETURN (error_mark_node);
14765 if (TREE_CODE (type) == FUNCTION_TYPE)
14767 /* It may seem that this case cannot occur, since:
14769 typedef void f();
14770 void g() { f x; }
14772 declares a function, not a variable. However:
14774 typedef void f();
14775 template <typename T> void g() { T t; }
14776 template void g<f>();
14778 is an attempt to declare a variable with function
14779 type. */
14780 error ("variable %qD has function type",
14781 /* R is not yet sufficiently initialized, so we
14782 just use its name. */
14783 DECL_NAME (r));
14784 RETURN (error_mark_node);
14786 type = complete_type (type);
14787 /* Wait until cp_finish_decl to set this again, to handle
14788 circular dependency (template/instantiate6.C). */
14789 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14790 type = check_var_type (DECL_NAME (r), type,
14791 DECL_SOURCE_LOCATION (r));
14792 if (DECL_HAS_VALUE_EXPR_P (t))
14794 tree ve = DECL_VALUE_EXPR (t);
14795 /* If the DECL_VALUE_EXPR is converted to the declared type,
14796 preserve the identity so that gimplify_type_sizes works. */
14797 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14798 if (nop)
14799 ve = TREE_OPERAND (ve, 0);
14800 ve = tsubst_expr (ve, args, complain, in_decl,
14801 /*constant_expression_p=*/false);
14802 if (REFERENCE_REF_P (ve))
14804 gcc_assert (TYPE_REF_P (type));
14805 ve = TREE_OPERAND (ve, 0);
14807 if (nop)
14808 ve = build_nop (type, ve);
14809 else if (DECL_LANG_SPECIFIC (t)
14810 && DECL_OMP_PRIVATIZED_MEMBER (t)
14811 && TREE_CODE (ve) == COMPONENT_REF
14812 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14813 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14814 type = TREE_TYPE (ve);
14815 else
14816 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14817 == TYPE_MAIN_VARIANT (type));
14818 SET_DECL_VALUE_EXPR (r, ve);
14820 if (CP_DECL_THREAD_LOCAL_P (r)
14821 && !processing_template_decl)
14822 set_decl_tls_model (r, decl_default_tls_model (r));
14824 else if (DECL_SELF_REFERENCE_P (t))
14825 SET_DECL_SELF_REFERENCE_P (r);
14826 TREE_TYPE (r) = type;
14827 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14828 DECL_CONTEXT (r) = ctx;
14829 /* Clear out the mangled name and RTL for the instantiation. */
14830 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14831 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14832 SET_DECL_RTL (r, NULL);
14833 set_instantiating_module (r);
14835 /* The initializer must not be expanded until it is required;
14836 see [temp.inst]. */
14837 DECL_INITIAL (r) = NULL_TREE;
14838 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14839 if (VAR_P (r))
14841 if (DECL_LANG_SPECIFIC (r))
14842 SET_DECL_DEPENDENT_INIT_P (r, false);
14844 SET_DECL_MODE (r, VOIDmode);
14846 /* Possibly limit visibility based on template args. */
14847 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14848 if (DECL_VISIBILITY_SPECIFIED (t))
14850 DECL_VISIBILITY_SPECIFIED (r) = 0;
14851 DECL_ATTRIBUTES (r)
14852 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14854 determine_visibility (r);
14857 if (!local_p)
14859 /* A static data member declaration is always marked
14860 external when it is declared in-class, even if an
14861 initializer is present. We mimic the non-template
14862 processing here. */
14863 DECL_EXTERNAL (r) = 1;
14864 if (DECL_NAMESPACE_SCOPE_P (t))
14865 DECL_NOT_REALLY_EXTERN (r) = 1;
14867 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14868 SET_DECL_IMPLICIT_INSTANTIATION (r);
14869 if (!error_operand_p (r) || (complain & tf_error))
14870 register_specialization (r, gen_tmpl, argvec, false, hash);
14872 else
14874 if (DECL_LANG_SPECIFIC (r))
14875 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14876 if (!cp_unevaluated_operand)
14877 register_local_specialization (r, t);
14880 DECL_CHAIN (r) = NULL_TREE;
14882 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14883 /*flags=*/0,
14884 args, complain, in_decl))
14885 return error_mark_node;
14887 /* Preserve a typedef that names a type. */
14888 if (is_typedef_decl (r) && type != error_mark_node)
14890 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14891 set_underlying_type (r);
14894 layout_decl (r, 0);
14896 break;
14898 default:
14899 gcc_unreachable ();
14901 #undef RETURN
14903 out:
14904 /* Restore the file and line information. */
14905 input_location = saved_loc;
14907 return r;
14910 /* Substitute into the complete parameter type list PARMS. */
14912 tree
14913 tsubst_function_parms (tree parms,
14914 tree args,
14915 tsubst_flags_t complain,
14916 tree in_decl)
14918 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14921 /* Substitute into the ARG_TYPES of a function type.
14922 If END is a TREE_CHAIN, leave it and any following types
14923 un-substituted. */
14925 static tree
14926 tsubst_arg_types (tree arg_types,
14927 tree args,
14928 tree end,
14929 tsubst_flags_t complain,
14930 tree in_decl)
14932 tree type = NULL_TREE;
14933 int len = 1;
14934 tree expanded_args = NULL_TREE;
14936 if (!arg_types || arg_types == void_list_node || arg_types == end)
14937 return arg_types;
14939 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14941 /* For a pack expansion, perform substitution on the
14942 entire expression. Later on, we'll handle the arguments
14943 one-by-one. */
14944 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14945 args, complain, in_decl);
14947 if (TREE_CODE (expanded_args) == TREE_VEC)
14948 /* So that we'll spin through the parameters, one by one. */
14949 len = TREE_VEC_LENGTH (expanded_args);
14950 else
14952 /* We only partially substituted into the parameter
14953 pack. Our type is TYPE_PACK_EXPANSION. */
14954 type = expanded_args;
14955 expanded_args = NULL_TREE;
14958 else
14959 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14961 /* Check if a substituted type is erroneous before substituting into
14962 the rest of the chain. */
14963 for (int i = 0; i < len; i++)
14965 if (expanded_args)
14966 type = TREE_VEC_ELT (expanded_args, i);
14968 if (type == error_mark_node)
14969 return error_mark_node;
14970 if (VOID_TYPE_P (type))
14972 if (complain & tf_error)
14974 error ("invalid parameter type %qT", type);
14975 if (in_decl)
14976 error ("in declaration %q+D", in_decl);
14978 return error_mark_node;
14982 /* We do not substitute into default arguments here. The standard
14983 mandates that they be instantiated only when needed, which is
14984 done in build_over_call. */
14985 tree default_arg = TREE_PURPOSE (arg_types);
14987 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14988 since the new op() won't have any associated template arguments for us
14989 to refer to later. */
14990 if (lambda_fn_in_template_p (in_decl))
14991 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14992 false/*fn*/, false/*constexpr*/);
14994 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14995 args, end, complain, in_decl);
14996 if (remaining_arg_types == error_mark_node)
14997 return error_mark_node;
14999 for (int i = len-1; i >= 0; i--)
15001 if (expanded_args)
15002 type = TREE_VEC_ELT (expanded_args, i);
15004 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15005 top-level qualifiers as required. */
15006 type = cv_unqualified (type_decays_to (type));
15008 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15010 /* We've instantiated a template before its default arguments
15011 have been parsed. This can happen for a nested template
15012 class, and is not an error unless we require the default
15013 argument in a call of this function. */
15014 remaining_arg_types
15015 = tree_cons (default_arg, type, remaining_arg_types);
15016 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15017 remaining_arg_types);
15019 else
15020 remaining_arg_types
15021 = hash_tree_cons (default_arg, type, remaining_arg_types);
15024 return remaining_arg_types;
15027 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15028 *not* handle the exception-specification for FNTYPE, because the
15029 initial substitution of explicitly provided template parameters
15030 during argument deduction forbids substitution into the
15031 exception-specification:
15033 [temp.deduct]
15035 All references in the function type of the function template to the
15036 corresponding template parameters are replaced by the specified tem-
15037 plate argument values. If a substitution in a template parameter or
15038 in the function type of the function template results in an invalid
15039 type, type deduction fails. [Note: The equivalent substitution in
15040 exception specifications is done only when the function is instanti-
15041 ated, at which point a program is ill-formed if the substitution
15042 results in an invalid type.] */
15044 static tree
15045 tsubst_function_type (tree t,
15046 tree args,
15047 tsubst_flags_t complain,
15048 tree in_decl)
15050 tree return_type;
15051 tree arg_types = NULL_TREE;
15053 /* The TYPE_CONTEXT is not used for function/method types. */
15054 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15056 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15057 failure. */
15058 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15060 if (late_return_type_p)
15062 /* Substitute the argument types. */
15063 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15064 complain, in_decl);
15065 if (arg_types == error_mark_node)
15066 return error_mark_node;
15068 tree save_ccp = current_class_ptr;
15069 tree save_ccr = current_class_ref;
15070 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15071 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15072 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15073 if (do_inject)
15075 /* DR 1207: 'this' is in scope in the trailing return type. */
15076 inject_this_parameter (this_type, cp_type_quals (this_type));
15079 /* Substitute the return type. */
15080 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15082 if (do_inject)
15084 current_class_ptr = save_ccp;
15085 current_class_ref = save_ccr;
15088 else
15089 /* Substitute the return type. */
15090 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15092 if (return_type == error_mark_node)
15093 return error_mark_node;
15094 /* DR 486 clarifies that creation of a function type with an
15095 invalid return type is a deduction failure. */
15096 if (TREE_CODE (return_type) == ARRAY_TYPE
15097 || TREE_CODE (return_type) == FUNCTION_TYPE)
15099 if (complain & tf_error)
15101 if (TREE_CODE (return_type) == ARRAY_TYPE)
15102 error ("function returning an array");
15103 else
15104 error ("function returning a function");
15106 return error_mark_node;
15109 if (!late_return_type_p)
15111 /* Substitute the argument types. */
15112 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15113 complain, in_decl);
15114 if (arg_types == error_mark_node)
15115 return error_mark_node;
15118 /* Construct a new type node and return it. */
15119 return rebuild_function_or_method_type (t, return_type, arg_types,
15120 /*raises=*/NULL_TREE, complain);
15123 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15124 ARGS into that specification, and return the substituted
15125 specification. If there is no specification, return NULL_TREE. */
15127 static tree
15128 tsubst_exception_specification (tree fntype,
15129 tree args,
15130 tsubst_flags_t complain,
15131 tree in_decl,
15132 bool defer_ok)
15134 tree specs;
15135 tree new_specs;
15137 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15138 new_specs = NULL_TREE;
15139 if (specs && TREE_PURPOSE (specs))
15141 /* A noexcept-specifier. */
15142 tree expr = TREE_PURPOSE (specs);
15143 if (TREE_CODE (expr) == INTEGER_CST)
15144 new_specs = expr;
15145 else if (defer_ok)
15147 /* Defer instantiation of noexcept-specifiers to avoid
15148 excessive instantiations (c++/49107). */
15149 new_specs = make_node (DEFERRED_NOEXCEPT);
15150 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15152 /* We already partially instantiated this member template,
15153 so combine the new args with the old. */
15154 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15155 = DEFERRED_NOEXCEPT_PATTERN (expr);
15156 DEFERRED_NOEXCEPT_ARGS (new_specs)
15157 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15159 else
15161 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15162 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15165 else
15167 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15169 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15170 args);
15171 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15173 new_specs = tsubst_copy_and_build
15174 (expr, args, complain, in_decl, /*function_p=*/false,
15175 /*integral_constant_expression_p=*/true);
15177 new_specs = build_noexcept_spec (new_specs, complain);
15178 /* We've instantiated a template before a noexcept-specifier
15179 contained therein has been parsed. This can happen for
15180 a nested template class:
15182 struct S {
15183 template<typename> struct B { B() noexcept(...); };
15184 struct A : B<int> { ... use B() ... };
15187 where completing B<int> will trigger instantiating the
15188 noexcept, even though we only parse it at the end of S. */
15189 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
15191 gcc_checking_assert (defer_ok);
15192 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
15195 else if (specs)
15197 if (! TREE_VALUE (specs))
15198 new_specs = specs;
15199 else
15200 while (specs)
15202 tree spec;
15203 int i, len = 1;
15204 tree expanded_specs = NULL_TREE;
15206 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15208 /* Expand the pack expansion type. */
15209 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15210 args, complain,
15211 in_decl);
15213 if (expanded_specs == error_mark_node)
15214 return error_mark_node;
15215 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15216 len = TREE_VEC_LENGTH (expanded_specs);
15217 else
15219 /* We're substituting into a member template, so
15220 we got a TYPE_PACK_EXPANSION back. Add that
15221 expansion and move on. */
15222 gcc_assert (TREE_CODE (expanded_specs)
15223 == TYPE_PACK_EXPANSION);
15224 new_specs = add_exception_specifier (new_specs,
15225 expanded_specs,
15226 complain);
15227 specs = TREE_CHAIN (specs);
15228 continue;
15232 for (i = 0; i < len; ++i)
15234 if (expanded_specs)
15235 spec = TREE_VEC_ELT (expanded_specs, i);
15236 else
15237 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15238 if (spec == error_mark_node)
15239 return spec;
15240 new_specs = add_exception_specifier (new_specs, spec,
15241 complain);
15244 specs = TREE_CHAIN (specs);
15247 return new_specs;
15250 /* Substitute through a TREE_LIST of types or expressions, handling pack
15251 expansions. */
15253 tree
15254 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15256 if (t == void_list_node)
15257 return t;
15259 tree purpose = TREE_PURPOSE (t);
15260 tree purposevec = NULL_TREE;
15261 if (!purpose)
15263 else if (PACK_EXPANSION_P (purpose))
15265 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15266 if (TREE_CODE (purpose) == TREE_VEC)
15267 purposevec = purpose;
15269 else if (TYPE_P (purpose))
15270 purpose = tsubst (purpose, args, complain, in_decl);
15271 else
15272 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15273 if (purpose == error_mark_node || purposevec == error_mark_node)
15274 return error_mark_node;
15276 tree value = TREE_VALUE (t);
15277 tree valuevec = NULL_TREE;
15278 if (!value)
15280 else if (PACK_EXPANSION_P (value))
15282 value = tsubst_pack_expansion (value, args, complain, in_decl);
15283 if (TREE_CODE (value) == TREE_VEC)
15284 valuevec = value;
15286 else if (TYPE_P (value))
15287 value = tsubst (value, args, complain, in_decl);
15288 else
15289 value = tsubst_copy_and_build (value, args, complain, in_decl);
15290 if (value == error_mark_node || valuevec == error_mark_node)
15291 return error_mark_node;
15293 tree chain = TREE_CHAIN (t);
15294 if (!chain)
15296 else if (TREE_CODE (chain) == TREE_LIST)
15297 chain = tsubst_tree_list (chain, args, complain, in_decl);
15298 else if (TYPE_P (chain))
15299 chain = tsubst (chain, args, complain, in_decl);
15300 else
15301 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15302 if (chain == error_mark_node)
15303 return error_mark_node;
15305 if (purpose == TREE_PURPOSE (t)
15306 && value == TREE_VALUE (t)
15307 && chain == TREE_CHAIN (t))
15308 return t;
15310 int len;
15311 /* Determine the number of arguments. */
15312 if (purposevec)
15314 len = TREE_VEC_LENGTH (purposevec);
15315 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15317 else if (valuevec)
15318 len = TREE_VEC_LENGTH (valuevec);
15319 else
15320 len = 1;
15322 for (int i = len; i-- > 0; )
15324 if (purposevec)
15325 purpose = TREE_VEC_ELT (purposevec, i);
15326 if (valuevec)
15327 value = TREE_VEC_ELT (valuevec, i);
15329 if (value && TYPE_P (value))
15330 chain = hash_tree_cons (purpose, value, chain);
15331 else
15332 chain = tree_cons (purpose, value, chain);
15335 return chain;
15338 /* Take the tree structure T and replace template parameters used
15339 therein with the argument vector ARGS. IN_DECL is an associated
15340 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15341 Issue error and warning messages under control of COMPLAIN. Note
15342 that we must be relatively non-tolerant of extensions here, in
15343 order to preserve conformance; if we allow substitutions that
15344 should not be allowed, we may allow argument deductions that should
15345 not succeed, and therefore report ambiguous overload situations
15346 where there are none. In theory, we could allow the substitution,
15347 but indicate that it should have failed, and allow our caller to
15348 make sure that the right thing happens, but we don't try to do this
15349 yet.
15351 This function is used for dealing with types, decls and the like;
15352 for expressions, use tsubst_expr or tsubst_copy. */
15354 tree
15355 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15357 enum tree_code code;
15358 tree type, r = NULL_TREE;
15360 if (t == NULL_TREE || t == error_mark_node
15361 || t == integer_type_node
15362 || t == void_type_node
15363 || t == char_type_node
15364 || t == unknown_type_node
15365 || TREE_CODE (t) == NAMESPACE_DECL
15366 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15367 return t;
15369 if (DECL_P (t))
15370 return tsubst_decl (t, args, complain);
15372 if (args == NULL_TREE)
15373 return t;
15375 code = TREE_CODE (t);
15377 gcc_assert (code != IDENTIFIER_NODE);
15378 type = TREE_TYPE (t);
15380 gcc_assert (type != unknown_type_node);
15382 /* Reuse typedefs. We need to do this to handle dependent attributes,
15383 such as attribute aligned. */
15384 if (TYPE_P (t)
15385 && typedef_variant_p (t))
15387 tree decl = TYPE_NAME (t);
15389 if (alias_template_specialization_p (t, nt_opaque))
15391 /* DECL represents an alias template and we want to
15392 instantiate it. */
15393 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15394 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15395 r = instantiate_alias_template (tmpl, gen_args, complain);
15397 else if (DECL_CLASS_SCOPE_P (decl)
15398 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15399 && uses_template_parms (DECL_CONTEXT (decl)))
15401 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15402 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15403 r = retrieve_specialization (tmpl, gen_args, 0);
15405 else if (DECL_FUNCTION_SCOPE_P (decl)
15406 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15407 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15408 r = retrieve_local_specialization (decl);
15409 else
15410 /* The typedef is from a non-template context. */
15411 return t;
15413 if (r)
15415 r = TREE_TYPE (r);
15416 r = cp_build_qualified_type_real
15417 (r, cp_type_quals (t) | cp_type_quals (r),
15418 complain | tf_ignore_bad_quals);
15419 return r;
15421 else
15423 /* We don't have an instantiation yet, so drop the typedef. */
15424 int quals = cp_type_quals (t);
15425 t = DECL_ORIGINAL_TYPE (decl);
15426 t = cp_build_qualified_type_real (t, quals,
15427 complain | tf_ignore_bad_quals);
15431 bool fndecl_type = (complain & tf_fndecl_type);
15432 complain &= ~tf_fndecl_type;
15434 if (type
15435 && code != TYPENAME_TYPE
15436 && code != TEMPLATE_TYPE_PARM
15437 && code != TEMPLATE_PARM_INDEX
15438 && code != IDENTIFIER_NODE
15439 && code != FUNCTION_TYPE
15440 && code != METHOD_TYPE)
15441 type = tsubst (type, args, complain, in_decl);
15442 if (type == error_mark_node)
15443 return error_mark_node;
15445 switch (code)
15447 case RECORD_TYPE:
15448 case UNION_TYPE:
15449 case ENUMERAL_TYPE:
15450 return tsubst_aggr_type (t, args, complain, in_decl,
15451 /*entering_scope=*/0);
15453 case ERROR_MARK:
15454 case IDENTIFIER_NODE:
15455 case VOID_TYPE:
15456 case OPAQUE_TYPE:
15457 case REAL_TYPE:
15458 case COMPLEX_TYPE:
15459 case VECTOR_TYPE:
15460 case BOOLEAN_TYPE:
15461 case NULLPTR_TYPE:
15462 case LANG_TYPE:
15463 return t;
15465 case INTEGER_TYPE:
15466 if (t == integer_type_node)
15467 return t;
15469 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15470 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15471 return t;
15474 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15476 max = tsubst_expr (omax, args, complain, in_decl,
15477 /*integral_constant_expression_p=*/false);
15479 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15480 needed. */
15481 if (TREE_CODE (max) == NOP_EXPR
15482 && TREE_SIDE_EFFECTS (omax)
15483 && !TREE_TYPE (max))
15484 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15486 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15487 with TREE_SIDE_EFFECTS that indicates this is not an integral
15488 constant expression. */
15489 if (processing_template_decl
15490 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15492 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15493 TREE_SIDE_EFFECTS (max) = 1;
15496 return compute_array_index_type (NULL_TREE, max, complain);
15499 case TEMPLATE_TYPE_PARM:
15500 case TEMPLATE_TEMPLATE_PARM:
15501 case BOUND_TEMPLATE_TEMPLATE_PARM:
15502 case TEMPLATE_PARM_INDEX:
15504 int idx;
15505 int level;
15506 int levels;
15507 tree arg = NULL_TREE;
15509 r = NULL_TREE;
15511 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15512 template_parm_level_and_index (t, &level, &idx);
15514 levels = TMPL_ARGS_DEPTH (args);
15515 if (level <= levels
15516 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15518 arg = TMPL_ARG (args, level, idx);
15520 /* See through ARGUMENT_PACK_SELECT arguments. */
15521 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15522 arg = argument_pack_select_arg (arg);
15525 if (arg == error_mark_node)
15526 return error_mark_node;
15527 else if (arg != NULL_TREE)
15529 if (ARGUMENT_PACK_P (arg))
15530 /* If ARG is an argument pack, we don't actually want to
15531 perform a substitution here, because substitutions
15532 for argument packs are only done
15533 element-by-element. We can get to this point when
15534 substituting the type of a non-type template
15535 parameter pack, when that type actually contains
15536 template parameter packs from an outer template, e.g.,
15538 template<typename... Types> struct A {
15539 template<Types... Values> struct B { };
15540 }; */
15541 return t;
15543 if (code == TEMPLATE_TYPE_PARM)
15545 int quals;
15547 /* When building concept checks for the purpose of
15548 deducing placeholders, we can end up with wildcards
15549 where types are expected. Adjust this to the deduced
15550 value. */
15551 if (TREE_CODE (arg) == WILDCARD_DECL)
15552 arg = TREE_TYPE (TREE_TYPE (arg));
15554 gcc_assert (TYPE_P (arg));
15556 quals = cp_type_quals (arg) | cp_type_quals (t);
15558 return cp_build_qualified_type_real
15559 (arg, quals, complain | tf_ignore_bad_quals);
15561 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15563 /* We are processing a type constructed from a
15564 template template parameter. */
15565 tree argvec = tsubst (TYPE_TI_ARGS (t),
15566 args, complain, in_decl);
15567 if (argvec == error_mark_node)
15568 return error_mark_node;
15570 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15571 || TREE_CODE (arg) == TEMPLATE_DECL
15572 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15574 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15575 /* Consider this code:
15577 template <template <class> class Template>
15578 struct Internal {
15579 template <class Arg> using Bind = Template<Arg>;
15582 template <template <class> class Template, class Arg>
15583 using Instantiate = Template<Arg>; //#0
15585 template <template <class> class Template,
15586 class Argument>
15587 using Bind =
15588 Instantiate<Internal<Template>::template Bind,
15589 Argument>; //#1
15591 When #1 is parsed, the
15592 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15593 parameter `Template' in #0 matches the
15594 UNBOUND_CLASS_TEMPLATE representing the argument
15595 `Internal<Template>::template Bind'; We then want
15596 to assemble the type `Bind<Argument>' that can't
15597 be fully created right now, because
15598 `Internal<Template>' not being complete, the Bind
15599 template cannot be looked up in that context. So
15600 we need to "store" `Bind<Argument>' for later
15601 when the context of Bind becomes complete. Let's
15602 store that in a TYPENAME_TYPE. */
15603 return make_typename_type (TYPE_CONTEXT (arg),
15604 build_nt (TEMPLATE_ID_EXPR,
15605 TYPE_IDENTIFIER (arg),
15606 argvec),
15607 typename_type,
15608 complain);
15610 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15611 are resolving nested-types in the signature of a
15612 member function templates. Otherwise ARG is a
15613 TEMPLATE_DECL and is the real template to be
15614 instantiated. */
15615 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15616 arg = TYPE_NAME (arg);
15618 r = lookup_template_class (arg,
15619 argvec, in_decl,
15620 DECL_CONTEXT (arg),
15621 /*entering_scope=*/0,
15622 complain);
15623 return cp_build_qualified_type_real
15624 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15626 else if (code == TEMPLATE_TEMPLATE_PARM)
15627 return arg;
15628 else
15629 /* TEMPLATE_PARM_INDEX. */
15630 return convert_from_reference (unshare_expr (arg));
15633 if (level == 1)
15634 /* This can happen during the attempted tsubst'ing in
15635 unify. This means that we don't yet have any information
15636 about the template parameter in question. */
15637 return t;
15639 /* Early in template argument deduction substitution, we don't
15640 want to reduce the level of 'auto', or it will be confused
15641 with a normal template parm in subsequent deduction.
15642 Similarly, don't reduce the level of template parameters to
15643 avoid mismatches when deducing their types. */
15644 if (complain & tf_partial)
15645 return t;
15647 /* If we get here, we must have been looking at a parm for a
15648 more deeply nested template. Make a new version of this
15649 template parameter, but with a lower level. */
15650 switch (code)
15652 case TEMPLATE_TYPE_PARM:
15653 case TEMPLATE_TEMPLATE_PARM:
15654 case BOUND_TEMPLATE_TEMPLATE_PARM:
15655 if (cp_type_quals (t))
15657 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15658 r = cp_build_qualified_type_real
15659 (r, cp_type_quals (t),
15660 complain | (code == TEMPLATE_TYPE_PARM
15661 ? tf_ignore_bad_quals : 0));
15663 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15664 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15665 && (r = (TEMPLATE_PARM_DESCENDANTS
15666 (TEMPLATE_TYPE_PARM_INDEX (t))))
15667 && (r = TREE_TYPE (r))
15668 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r))
15669 /* Break infinite recursion when substituting the constraints
15670 of a constrained placeholder. */;
15671 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15672 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15673 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15674 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15675 r = TEMPLATE_PARM_DESCENDANTS (arg))
15676 && (TEMPLATE_PARM_LEVEL (r)
15677 == TEMPLATE_PARM_LEVEL (arg) - levels))
15678 /* Cache the simple case of lowering a type parameter. */
15679 r = TREE_TYPE (r);
15680 else
15682 r = copy_type (t);
15683 TEMPLATE_TYPE_PARM_INDEX (r)
15684 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15685 r, levels, args, complain);
15686 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15687 TYPE_MAIN_VARIANT (r) = r;
15688 TYPE_POINTER_TO (r) = NULL_TREE;
15689 TYPE_REFERENCE_TO (r) = NULL_TREE;
15691 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15693 /* Propagate constraints on placeholders since they are
15694 only instantiated during satisfaction. */
15695 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
15696 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
15697 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15699 pl = tsubst_copy (pl, args, complain, in_decl);
15700 if (TREE_CODE (pl) == TEMPLATE_TEMPLATE_PARM)
15701 pl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl);
15702 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15706 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15707 /* We have reduced the level of the template
15708 template parameter, but not the levels of its
15709 template parameters, so canonical_type_parameter
15710 will not be able to find the canonical template
15711 template parameter for this level. Thus, we
15712 require structural equality checking to compare
15713 TEMPLATE_TEMPLATE_PARMs. */
15714 SET_TYPE_STRUCTURAL_EQUALITY (r);
15715 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15716 SET_TYPE_STRUCTURAL_EQUALITY (r);
15717 else
15718 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15720 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15722 tree tinfo = TYPE_TEMPLATE_INFO (t);
15723 /* We might need to substitute into the types of non-type
15724 template parameters. */
15725 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15726 complain, in_decl);
15727 if (tmpl == error_mark_node)
15728 return error_mark_node;
15729 tree argvec = tsubst (TI_ARGS (tinfo), args,
15730 complain, in_decl);
15731 if (argvec == error_mark_node)
15732 return error_mark_node;
15734 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15735 = build_template_info (tmpl, argvec);
15738 break;
15740 case TEMPLATE_PARM_INDEX:
15741 /* OK, now substitute the type of the non-type parameter. We
15742 couldn't do it earlier because it might be an auto parameter,
15743 and we wouldn't need to if we had an argument. */
15744 type = tsubst (type, args, complain, in_decl);
15745 if (type == error_mark_node)
15746 return error_mark_node;
15747 r = reduce_template_parm_level (t, type, levels, args, complain);
15748 break;
15750 default:
15751 gcc_unreachable ();
15754 return r;
15757 case TREE_LIST:
15758 return tsubst_tree_list (t, args, complain, in_decl);
15760 case TREE_BINFO:
15761 /* We should never be tsubsting a binfo. */
15762 gcc_unreachable ();
15764 case TREE_VEC:
15765 /* A vector of template arguments. */
15766 gcc_assert (!type);
15767 return tsubst_template_args (t, args, complain, in_decl);
15769 case POINTER_TYPE:
15770 case REFERENCE_TYPE:
15772 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15773 return t;
15775 /* [temp.deduct]
15777 Type deduction may fail for any of the following
15778 reasons:
15780 -- Attempting to create a pointer to reference type.
15781 -- Attempting to create a reference to a reference type or
15782 a reference to void.
15784 Core issue 106 says that creating a reference to a reference
15785 during instantiation is no longer a cause for failure. We
15786 only enforce this check in strict C++98 mode. */
15787 if ((TYPE_REF_P (type)
15788 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15789 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15791 static location_t last_loc;
15793 /* We keep track of the last time we issued this error
15794 message to avoid spewing a ton of messages during a
15795 single bad template instantiation. */
15796 if (complain & tf_error
15797 && last_loc != input_location)
15799 if (VOID_TYPE_P (type))
15800 error ("forming reference to void");
15801 else if (code == POINTER_TYPE)
15802 error ("forming pointer to reference type %qT", type);
15803 else
15804 error ("forming reference to reference type %qT", type);
15805 last_loc = input_location;
15808 return error_mark_node;
15810 else if (TREE_CODE (type) == FUNCTION_TYPE
15811 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15812 || type_memfn_rqual (type) != REF_QUAL_NONE))
15814 if (complain & tf_error)
15816 if (code == POINTER_TYPE)
15817 error ("forming pointer to qualified function type %qT",
15818 type);
15819 else
15820 error ("forming reference to qualified function type %qT",
15821 type);
15823 return error_mark_node;
15825 else if (code == POINTER_TYPE)
15827 r = build_pointer_type (type);
15828 if (TREE_CODE (type) == METHOD_TYPE)
15829 r = build_ptrmemfunc_type (r);
15831 else if (TYPE_REF_P (type))
15832 /* In C++0x, during template argument substitution, when there is an
15833 attempt to create a reference to a reference type, reference
15834 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15836 "If a template-argument for a template-parameter T names a type
15837 that is a reference to a type A, an attempt to create the type
15838 'lvalue reference to cv T' creates the type 'lvalue reference to
15839 A,' while an attempt to create the type type rvalue reference to
15840 cv T' creates the type T"
15842 r = cp_build_reference_type
15843 (TREE_TYPE (type),
15844 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15845 else
15846 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15847 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15849 if (r != error_mark_node)
15850 /* Will this ever be needed for TYPE_..._TO values? */
15851 layout_type (r);
15853 return r;
15855 case OFFSET_TYPE:
15857 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15858 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15860 /* [temp.deduct]
15862 Type deduction may fail for any of the following
15863 reasons:
15865 -- Attempting to create "pointer to member of T" when T
15866 is not a class type. */
15867 if (complain & tf_error)
15868 error ("creating pointer to member of non-class type %qT", r);
15869 return error_mark_node;
15871 if (TYPE_REF_P (type))
15873 if (complain & tf_error)
15874 error ("creating pointer to member reference type %qT", type);
15875 return error_mark_node;
15877 if (VOID_TYPE_P (type))
15879 if (complain & tf_error)
15880 error ("creating pointer to member of type void");
15881 return error_mark_node;
15883 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15884 if (TREE_CODE (type) == FUNCTION_TYPE)
15886 /* The type of the implicit object parameter gets its
15887 cv-qualifiers from the FUNCTION_TYPE. */
15888 tree memptr;
15889 tree method_type
15890 = build_memfn_type (type, r, type_memfn_quals (type),
15891 type_memfn_rqual (type));
15892 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15893 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15894 complain);
15896 else
15897 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15898 cp_type_quals (t),
15899 complain);
15901 case FUNCTION_TYPE:
15902 case METHOD_TYPE:
15904 tree fntype;
15905 tree specs;
15906 fntype = tsubst_function_type (t, args, complain, in_decl);
15907 if (fntype == error_mark_node)
15908 return error_mark_node;
15910 /* Substitute the exception specification. */
15911 specs = tsubst_exception_specification (t, args, complain, in_decl,
15912 /*defer_ok*/fndecl_type);
15913 if (specs == error_mark_node)
15914 return error_mark_node;
15915 if (specs)
15916 fntype = build_exception_variant (fntype, specs);
15917 return fntype;
15919 case ARRAY_TYPE:
15921 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15922 if (domain == error_mark_node)
15923 return error_mark_node;
15925 /* As an optimization, we avoid regenerating the array type if
15926 it will obviously be the same as T. */
15927 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15928 return t;
15930 /* These checks should match the ones in create_array_type_for_decl.
15932 [temp.deduct]
15934 The deduction may fail for any of the following reasons:
15936 -- Attempting to create an array with an element type that
15937 is void, a function type, or a reference type, or [DR337]
15938 an abstract class type. */
15939 if (VOID_TYPE_P (type)
15940 || TREE_CODE (type) == FUNCTION_TYPE
15941 || (TREE_CODE (type) == ARRAY_TYPE
15942 && TYPE_DOMAIN (type) == NULL_TREE)
15943 || TYPE_REF_P (type))
15945 if (complain & tf_error)
15946 error ("creating array of %qT", type);
15947 return error_mark_node;
15950 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15951 !(complain & tf_error)))
15952 return error_mark_node;
15954 r = build_cplus_array_type (type, domain);
15956 if (!valid_array_size_p (input_location, r, in_decl,
15957 (complain & tf_error)))
15958 return error_mark_node;
15960 if (TYPE_USER_ALIGN (t))
15962 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15963 TYPE_USER_ALIGN (r) = 1;
15966 return r;
15969 case TYPENAME_TYPE:
15971 tree ctx = TYPE_CONTEXT (t);
15972 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15974 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15975 if (ctx == error_mark_node
15976 || TREE_VEC_LENGTH (ctx) > 1)
15977 return error_mark_node;
15978 if (TREE_VEC_LENGTH (ctx) == 0)
15980 if (complain & tf_error)
15981 error ("%qD is instantiated for an empty pack",
15982 TYPENAME_TYPE_FULLNAME (t));
15983 return error_mark_node;
15985 ctx = TREE_VEC_ELT (ctx, 0);
15987 else
15988 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15989 /*entering_scope=*/1);
15990 if (ctx == error_mark_node)
15991 return error_mark_node;
15993 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15994 complain, in_decl);
15995 if (f == error_mark_node)
15996 return error_mark_node;
15998 if (!MAYBE_CLASS_TYPE_P (ctx))
16000 if (complain & tf_error)
16001 error ("%qT is not a class, struct, or union type", ctx);
16002 return error_mark_node;
16004 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16006 /* Normally, make_typename_type does not require that the CTX
16007 have complete type in order to allow things like:
16009 template <class T> struct S { typename S<T>::X Y; };
16011 But, such constructs have already been resolved by this
16012 point, so here CTX really should have complete type, unless
16013 it's a partial instantiation. */
16014 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16015 return error_mark_node;
16018 f = make_typename_type (ctx, f, typename_type,
16019 complain | tf_keep_type_decl);
16020 if (f == error_mark_node)
16021 return f;
16022 if (TREE_CODE (f) == TYPE_DECL)
16024 complain |= tf_ignore_bad_quals;
16025 f = TREE_TYPE (f);
16028 if (TREE_CODE (f) != TYPENAME_TYPE)
16030 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16032 if (complain & tf_error)
16033 error ("%qT resolves to %qT, which is not an enumeration type",
16034 t, f);
16035 else
16036 return error_mark_node;
16038 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16040 if (complain & tf_error)
16041 error ("%qT resolves to %qT, which is not a class type",
16042 t, f);
16043 else
16044 return error_mark_node;
16048 return cp_build_qualified_type_real
16049 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16052 case UNBOUND_CLASS_TEMPLATE:
16054 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16055 in_decl, /*entering_scope=*/1);
16056 tree name = TYPE_IDENTIFIER (t);
16057 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16059 if (ctx == error_mark_node || name == error_mark_node)
16060 return error_mark_node;
16062 if (parm_list)
16063 parm_list = tsubst_template_parms (parm_list, args, complain);
16064 return make_unbound_class_template (ctx, name, parm_list, complain);
16067 case TYPEOF_TYPE:
16069 tree type;
16071 ++cp_unevaluated_operand;
16072 ++c_inhibit_evaluation_warnings;
16074 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16075 complain, in_decl,
16076 /*integral_constant_expression_p=*/false);
16078 --cp_unevaluated_operand;
16079 --c_inhibit_evaluation_warnings;
16081 type = finish_typeof (type);
16082 return cp_build_qualified_type_real (type,
16083 cp_type_quals (t)
16084 | cp_type_quals (type),
16085 complain);
16088 case DECLTYPE_TYPE:
16090 tree type;
16092 ++cp_unevaluated_operand;
16093 ++c_inhibit_evaluation_warnings;
16095 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16096 complain|tf_decltype, in_decl,
16097 /*function_p*/false,
16098 /*integral_constant_expression*/false);
16100 --cp_unevaluated_operand;
16101 --c_inhibit_evaluation_warnings;
16103 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16104 type = lambda_capture_field_type (type,
16105 false /*explicit_init*/,
16106 DECLTYPE_FOR_REF_CAPTURE (t));
16107 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16108 type = lambda_proxy_type (type);
16109 else
16111 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16112 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16113 && EXPR_P (type))
16114 /* In a template ~id could be either a complement expression
16115 or an unqualified-id naming a destructor; if instantiating
16116 it produces an expression, it's not an id-expression or
16117 member access. */
16118 id = false;
16119 type = finish_decltype_type (type, id, complain);
16121 return cp_build_qualified_type_real (type,
16122 cp_type_quals (t)
16123 | cp_type_quals (type),
16124 complain | tf_ignore_bad_quals);
16127 case UNDERLYING_TYPE:
16129 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16130 complain, in_decl);
16131 return finish_underlying_type (type);
16134 case TYPE_ARGUMENT_PACK:
16135 case NONTYPE_ARGUMENT_PACK:
16136 return tsubst_argument_pack (t, args, complain, in_decl);
16138 case VOID_CST:
16139 case INTEGER_CST:
16140 case REAL_CST:
16141 case STRING_CST:
16142 case PLUS_EXPR:
16143 case MINUS_EXPR:
16144 case NEGATE_EXPR:
16145 case NOP_EXPR:
16146 case INDIRECT_REF:
16147 case ADDR_EXPR:
16148 case CALL_EXPR:
16149 case ARRAY_REF:
16150 case SCOPE_REF:
16151 /* We should use one of the expression tsubsts for these codes. */
16152 gcc_unreachable ();
16154 default:
16155 sorry ("use of %qs in template", get_tree_code_name (code));
16156 return error_mark_node;
16160 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16161 expression on the left-hand side of the "." or "->" operator. We
16162 only do the lookup if we had a dependent BASELINK. Otherwise we
16163 adjust it onto the instantiated heirarchy. */
16165 static tree
16166 tsubst_baselink (tree baselink, tree object_type,
16167 tree args, tsubst_flags_t complain, tree in_decl)
16169 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16170 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16171 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16173 tree optype = BASELINK_OPTYPE (baselink);
16174 optype = tsubst (optype, args, complain, in_decl);
16176 tree template_args = NULL_TREE;
16177 bool template_id_p = false;
16178 tree fns = BASELINK_FUNCTIONS (baselink);
16179 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16181 template_id_p = true;
16182 template_args = TREE_OPERAND (fns, 1);
16183 fns = TREE_OPERAND (fns, 0);
16184 if (template_args)
16185 template_args = tsubst_template_args (template_args, args,
16186 complain, in_decl);
16189 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16190 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16191 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16193 if (dependent_p)
16195 tree name = OVL_NAME (fns);
16196 if (IDENTIFIER_CONV_OP_P (name))
16197 name = make_conv_op_name (optype);
16199 /* See maybe_dependent_member_ref. */
16200 if (dependent_scope_p (qualifying_scope))
16202 if (template_id_p)
16203 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
16204 template_args);
16205 return build_qualified_name (NULL_TREE, qualifying_scope, name,
16206 /* ::template */false);
16209 if (name == complete_dtor_identifier)
16210 /* Treat as-if non-dependent below. */
16211 dependent_p = false;
16213 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16214 complain);
16215 if (!baselink)
16217 if ((complain & tf_error)
16218 && constructor_name_p (name, qualifying_scope))
16219 error ("cannot call constructor %<%T::%D%> directly",
16220 qualifying_scope, name);
16221 return error_mark_node;
16224 if (BASELINK_P (baselink))
16225 fns = BASELINK_FUNCTIONS (baselink);
16227 else
16229 /* We're going to overwrite pieces below, make a duplicate. */
16230 baselink = copy_node (baselink);
16232 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16234 /* The decl we found was from non-dependent scope, but we still need
16235 to update the binfos for the instantiated qualifying_scope. */
16236 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16237 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16238 ba_unique, nullptr, complain);
16242 /* If lookup found a single function, mark it as used at this point.
16243 (If lookup found multiple functions the one selected later by
16244 overload resolution will be marked as used at that point.) */
16245 if (!template_id_p && !really_overloaded_fn (fns))
16247 tree fn = OVL_FIRST (fns);
16248 bool ok = mark_used (fn, complain);
16249 if (!ok && !(complain & tf_error))
16250 return error_mark_node;
16251 if (ok && BASELINK_P (baselink))
16252 /* We might have instantiated an auto function. */
16253 TREE_TYPE (baselink) = TREE_TYPE (fn);
16256 if (BASELINK_P (baselink))
16258 /* Add back the template arguments, if present. */
16259 if (template_id_p)
16260 BASELINK_FUNCTIONS (baselink)
16261 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16263 /* Update the conversion operator type. */
16264 BASELINK_OPTYPE (baselink) = optype;
16267 if (!object_type)
16268 object_type = current_class_type;
16270 if (qualified_p || !dependent_p)
16272 baselink = adjust_result_of_qualified_name_lookup (baselink,
16273 qualifying_scope,
16274 object_type);
16275 if (!qualified_p)
16276 /* We need to call adjust_result_of_qualified_name_lookup in case the
16277 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16278 so that we still get virtual function binding. */
16279 BASELINK_QUALIFIED_P (baselink) = false;
16282 return baselink;
16285 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16286 true if the qualified-id will be a postfix-expression in-and-of
16287 itself; false if more of the postfix-expression follows the
16288 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16289 of "&". */
16291 static tree
16292 tsubst_qualified_id (tree qualified_id, tree args,
16293 tsubst_flags_t complain, tree in_decl,
16294 bool done, bool address_p)
16296 tree expr;
16297 tree scope;
16298 tree name;
16299 bool is_template;
16300 tree template_args;
16301 location_t loc = EXPR_LOCATION (qualified_id);
16303 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16305 /* Figure out what name to look up. */
16306 name = TREE_OPERAND (qualified_id, 1);
16307 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16309 is_template = true;
16310 template_args = TREE_OPERAND (name, 1);
16311 if (template_args)
16312 template_args = tsubst_template_args (template_args, args,
16313 complain, in_decl);
16314 if (template_args == error_mark_node)
16315 return error_mark_node;
16316 name = TREE_OPERAND (name, 0);
16318 else
16320 is_template = false;
16321 template_args = NULL_TREE;
16324 /* Substitute into the qualifying scope. When there are no ARGS, we
16325 are just trying to simplify a non-dependent expression. In that
16326 case the qualifying scope may be dependent, and, in any case,
16327 substituting will not help. */
16328 scope = TREE_OPERAND (qualified_id, 0);
16329 if (args)
16331 scope = tsubst (scope, args, complain, in_decl);
16332 expr = tsubst_copy (name, args, complain, in_decl);
16334 else
16335 expr = name;
16337 if (dependent_scope_p (scope))
16339 if (is_template)
16340 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16341 tree r = build_qualified_name (NULL_TREE, scope, expr,
16342 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16343 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16344 return r;
16347 if (!BASELINK_P (name) && !DECL_P (expr))
16349 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16351 /* A BIT_NOT_EXPR is used to represent a destructor. */
16352 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16354 error ("qualifying type %qT does not match destructor name ~%qT",
16355 scope, TREE_OPERAND (expr, 0));
16356 expr = error_mark_node;
16358 else
16359 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16360 LOOK_want::NORMAL, false);
16362 else
16363 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16364 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16365 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16367 if (complain & tf_error)
16369 error ("dependent-name %qE is parsed as a non-type, but "
16370 "instantiation yields a type", qualified_id);
16371 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16373 return error_mark_node;
16377 if (DECL_P (expr))
16379 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16380 scope, complain))
16381 return error_mark_node;
16382 /* Remember that there was a reference to this entity. */
16383 if (!mark_used (expr, complain) && !(complain & tf_error))
16384 return error_mark_node;
16387 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16389 if (complain & tf_error)
16390 qualified_name_lookup_error (scope,
16391 TREE_OPERAND (qualified_id, 1),
16392 expr, input_location);
16393 return error_mark_node;
16396 if (is_template)
16398 /* We may be repeating a check already done during parsing, but
16399 if it was well-formed and passed then, it will pass again
16400 now, and if it didn't, we wouldn't have got here. The case
16401 we want to catch is when we couldn't tell then, and can now,
16402 namely when templ prior to substitution was an
16403 identifier. */
16404 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16405 return error_mark_node;
16407 if (variable_template_p (expr))
16408 expr = lookup_and_finish_template_variable (expr, template_args,
16409 complain);
16410 else
16411 expr = lookup_template_function (expr, template_args);
16414 if (expr == error_mark_node && complain & tf_error)
16415 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16416 expr, input_location);
16417 else if (TYPE_P (scope))
16419 expr = (adjust_result_of_qualified_name_lookup
16420 (expr, scope, current_nonlambda_class_type ()));
16421 expr = (finish_qualified_id_expr
16422 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16423 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16424 /*template_arg_p=*/false, complain));
16427 /* Expressions do not generally have reference type. */
16428 if (TREE_CODE (expr) != SCOPE_REF
16429 /* However, if we're about to form a pointer-to-member, we just
16430 want the referenced member referenced. */
16431 && TREE_CODE (expr) != OFFSET_REF)
16432 expr = convert_from_reference (expr);
16434 if (REF_PARENTHESIZED_P (qualified_id))
16435 expr = force_paren_expr (expr);
16437 expr = maybe_wrap_with_location (expr, loc);
16439 return expr;
16442 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16443 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16444 for tsubst. */
16446 static tree
16447 tsubst_init (tree init, tree decl, tree args,
16448 tsubst_flags_t complain, tree in_decl)
16450 if (!init)
16451 return NULL_TREE;
16453 init = tsubst_expr (init, args, complain, in_decl, false);
16455 tree type = TREE_TYPE (decl);
16457 if (!init && type != error_mark_node)
16459 if (tree auto_node = type_uses_auto (type))
16461 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16463 if (complain & tf_error)
16464 error ("initializer for %q#D expands to an empty list "
16465 "of expressions", decl);
16466 return error_mark_node;
16469 else if (!dependent_type_p (type))
16471 /* If we had an initializer but it
16472 instantiated to nothing,
16473 value-initialize the object. This will
16474 only occur when the initializer was a
16475 pack expansion where the parameter packs
16476 used in that expansion were of length
16477 zero. */
16478 init = build_value_init (type, complain);
16479 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16480 init = get_target_expr_sfinae (init, complain);
16481 if (TREE_CODE (init) == TARGET_EXPR)
16482 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16486 return init;
16489 /* If T is a reference to a dependent member of the current instantiation C and
16490 we are trying to refer to that member in a partial instantiation of C,
16491 return a SCOPE_REF; otherwise, return NULL_TREE.
16493 This can happen when forming a C++17 deduction guide, as in PR96199. */
16495 static tree
16496 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16497 tree in_decl)
16499 if (cxx_dialect < cxx17)
16500 return NULL_TREE;
16502 tree ctx = context_for_name_lookup (t);
16503 if (!CLASS_TYPE_P (ctx))
16504 return NULL_TREE;
16506 ctx = tsubst (ctx, args, complain, in_decl);
16507 if (dependent_scope_p (ctx))
16508 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16509 /*template_p=*/false);
16511 return NULL_TREE;
16514 /* Like tsubst, but deals with expressions. This function just replaces
16515 template parms; to finish processing the resultant expression, use
16516 tsubst_copy_and_build or tsubst_expr. */
16518 static tree
16519 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16521 enum tree_code code;
16522 tree r;
16524 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16525 return t;
16527 code = TREE_CODE (t);
16529 switch (code)
16531 case PARM_DECL:
16532 r = retrieve_local_specialization (t);
16534 if (r == NULL_TREE)
16536 /* We get here for a use of 'this' in an NSDMI. */
16537 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16538 return current_class_ptr;
16540 /* This can happen for a parameter name used later in a function
16541 declaration (such as in a late-specified return type). Just
16542 make a dummy decl, since it's only used for its type. */
16543 gcc_assert (cp_unevaluated_operand != 0);
16544 r = tsubst_decl (t, args, complain);
16545 /* Give it the template pattern as its context; its true context
16546 hasn't been instantiated yet and this is good enough for
16547 mangling. */
16548 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16551 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16552 r = argument_pack_select_arg (r);
16553 if (!mark_used (r, complain) && !(complain & tf_error))
16554 return error_mark_node;
16555 return r;
16557 case CONST_DECL:
16559 tree enum_type;
16560 tree v;
16562 if (DECL_TEMPLATE_PARM_P (t))
16563 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16564 /* There is no need to substitute into namespace-scope
16565 enumerators. */
16566 if (DECL_NAMESPACE_SCOPE_P (t))
16567 return t;
16568 /* If ARGS is NULL, then T is known to be non-dependent. */
16569 if (args == NULL_TREE)
16570 return scalar_constant_value (t);
16572 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16573 return ref;
16575 /* Unfortunately, we cannot just call lookup_name here.
16576 Consider:
16578 template <int I> int f() {
16579 enum E { a = I };
16580 struct S { void g() { E e = a; } };
16583 When we instantiate f<7>::S::g(), say, lookup_name is not
16584 clever enough to find f<7>::a. */
16585 enum_type
16586 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16587 /*entering_scope=*/0);
16589 for (v = TYPE_VALUES (enum_type);
16590 v != NULL_TREE;
16591 v = TREE_CHAIN (v))
16592 if (TREE_PURPOSE (v) == DECL_NAME (t))
16593 return TREE_VALUE (v);
16595 /* We didn't find the name. That should never happen; if
16596 name-lookup found it during preliminary parsing, we
16597 should find it again here during instantiation. */
16598 gcc_unreachable ();
16600 return t;
16602 case FIELD_DECL:
16603 if (DECL_CONTEXT (t))
16605 tree ctx;
16607 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16608 /*entering_scope=*/1);
16609 if (ctx != DECL_CONTEXT (t))
16611 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16612 if (!r)
16614 if (complain & tf_error)
16615 error ("using invalid field %qD", t);
16616 return error_mark_node;
16618 return r;
16622 return t;
16624 case VAR_DECL:
16625 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16626 return ref;
16627 gcc_fallthrough();
16628 case FUNCTION_DECL:
16629 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16630 r = tsubst (t, args, complain, in_decl);
16631 else if (DECL_LOCAL_DECL_P (t))
16633 /* Local specialization will usually have been created when
16634 we instantiated the DECL_EXPR_DECL. */
16635 r = retrieve_local_specialization (t);
16636 if (!r)
16638 /* We're in a generic lambda referencing a local extern
16639 from an outer block-scope of a non-template. */
16640 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
16641 r = t;
16644 else if (local_variable_p (t)
16645 && uses_template_parms (DECL_CONTEXT (t)))
16647 r = retrieve_local_specialization (t);
16648 if (r == NULL_TREE)
16650 /* First try name lookup to find the instantiation. */
16651 r = lookup_name (DECL_NAME (t));
16652 if (r)
16654 if (!VAR_P (r))
16656 /* During error-recovery we may find a non-variable,
16657 even an OVERLOAD: just bail out and avoid ICEs and
16658 duplicate diagnostics (c++/62207). */
16659 gcc_assert (seen_error ());
16660 return error_mark_node;
16662 if (!is_capture_proxy (r))
16664 /* Make sure the one we found is the one we want. */
16665 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16666 if (ctx != DECL_CONTEXT (r))
16667 r = NULL_TREE;
16671 if (r)
16672 /* OK */;
16673 else
16675 /* This can happen for a variable used in a
16676 late-specified return type of a local lambda, or for a
16677 local static or constant. Building a new VAR_DECL
16678 should be OK in all those cases. */
16679 r = tsubst_decl (t, args, complain);
16680 if (local_specializations)
16681 /* Avoid infinite recursion (79640). */
16682 register_local_specialization (r, t);
16683 if (decl_maybe_constant_var_p (r))
16685 /* We can't call cp_finish_decl, so handle the
16686 initializer by hand. */
16687 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16688 complain, in_decl);
16689 if (!processing_template_decl)
16690 init = maybe_constant_init (init);
16691 if (processing_template_decl
16692 ? potential_constant_expression (init)
16693 : reduced_constant_expression_p (init))
16694 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16695 = TREE_CONSTANT (r) = true;
16696 DECL_INITIAL (r) = init;
16697 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16698 TREE_TYPE (r)
16699 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16700 complain, adc_variable_type);
16702 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16703 || decl_constant_var_p (r)
16704 || seen_error ());
16705 if (!processing_template_decl
16706 && !TREE_STATIC (r))
16707 r = process_outer_var_ref (r, complain);
16709 /* Remember this for subsequent uses. */
16710 if (local_specializations)
16711 register_local_specialization (r, t);
16713 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16714 r = argument_pack_select_arg (r);
16716 else
16717 r = t;
16718 if (!mark_used (r, complain))
16719 return error_mark_node;
16720 return r;
16722 case NAMESPACE_DECL:
16723 return t;
16725 case OVERLOAD:
16726 return t;
16728 case BASELINK:
16729 return tsubst_baselink (t, current_nonlambda_class_type (),
16730 args, complain, in_decl);
16732 case TEMPLATE_DECL:
16733 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16734 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16735 args, complain, in_decl);
16736 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16737 return tsubst (t, args, complain, in_decl);
16738 else if (DECL_CLASS_SCOPE_P (t)
16739 && uses_template_parms (DECL_CONTEXT (t)))
16741 /* Template template argument like the following example need
16742 special treatment:
16744 template <template <class> class TT> struct C {};
16745 template <class T> struct D {
16746 template <class U> struct E {};
16747 C<E> c; // #1
16749 D<int> d; // #2
16751 We are processing the template argument `E' in #1 for
16752 the template instantiation #2. Originally, `E' is a
16753 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16754 have to substitute this with one having context `D<int>'. */
16756 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16757 if (dependent_scope_p (context))
16759 /* When rewriting a constructor into a deduction guide, a
16760 non-dependent name can become dependent, so memtmpl<args>
16761 becomes context::template memtmpl<args>. */
16762 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16763 return build_qualified_name (type, context, DECL_NAME (t),
16764 /*template*/true);
16766 return lookup_field (context, DECL_NAME(t), 0, false);
16768 else
16769 /* Ordinary template template argument. */
16770 return t;
16772 case NON_LVALUE_EXPR:
16773 case VIEW_CONVERT_EXPR:
16775 /* Handle location wrappers by substituting the wrapped node
16776 first, *then* reusing the resulting type. Doing the type
16777 first ensures that we handle template parameters and
16778 parameter pack expansions. */
16779 if (location_wrapper_p (t))
16781 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16782 complain, in_decl);
16783 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16785 tree op = TREE_OPERAND (t, 0);
16786 if (code == VIEW_CONVERT_EXPR
16787 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16789 /* Wrapper to make a C++20 template parameter object const. */
16790 op = tsubst_copy (op, args, complain, in_decl);
16791 if (!CP_TYPE_CONST_P (TREE_TYPE (op)))
16793 /* The template argument is not const, presumably because
16794 it is still dependent, and so not the const template parm
16795 object. */
16796 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16797 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
16798 (type, TREE_TYPE (op)));
16799 if (TREE_CODE (op) == CONSTRUCTOR
16800 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
16802 /* Don't add a wrapper to these. */
16803 op = copy_node (op);
16804 TREE_TYPE (op) = type;
16806 else
16807 /* Do add a wrapper otherwise (in particular, if op is
16808 another TEMPLATE_PARM_INDEX). */
16809 op = build1 (code, type, op);
16811 return op;
16813 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16814 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16816 op = tsubst_copy (op, args, complain, in_decl);
16817 op = build1 (code, TREE_TYPE (op), op);
16818 REF_PARENTHESIZED_P (op) = true;
16819 return op;
16821 /* We shouldn't see any other uses of these in templates. */
16822 gcc_unreachable ();
16825 case CAST_EXPR:
16826 case REINTERPRET_CAST_EXPR:
16827 case CONST_CAST_EXPR:
16828 case STATIC_CAST_EXPR:
16829 case DYNAMIC_CAST_EXPR:
16830 case IMPLICIT_CONV_EXPR:
16831 case CONVERT_EXPR:
16832 case NOP_EXPR:
16834 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16835 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16836 return build1 (code, type, op0);
16839 case BIT_CAST_EXPR:
16841 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16842 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16843 r = build_min (BIT_CAST_EXPR, type, op0);
16844 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16845 return r;
16848 case SIZEOF_EXPR:
16849 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16850 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16852 tree expanded, op = TREE_OPERAND (t, 0);
16853 int len = 0;
16855 if (SIZEOF_EXPR_TYPE_P (t))
16856 op = TREE_TYPE (op);
16858 ++cp_unevaluated_operand;
16859 ++c_inhibit_evaluation_warnings;
16860 /* We only want to compute the number of arguments. */
16861 if (PACK_EXPANSION_P (op))
16862 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16863 else
16864 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16865 args, complain, in_decl);
16866 --cp_unevaluated_operand;
16867 --c_inhibit_evaluation_warnings;
16869 if (TREE_CODE (expanded) == TREE_VEC)
16871 len = TREE_VEC_LENGTH (expanded);
16872 /* Set TREE_USED for the benefit of -Wunused. */
16873 for (int i = 0; i < len; i++)
16874 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16875 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16878 if (expanded == error_mark_node)
16879 return error_mark_node;
16880 else if (PACK_EXPANSION_P (expanded)
16881 || (TREE_CODE (expanded) == TREE_VEC
16882 && pack_expansion_args_count (expanded)))
16885 if (PACK_EXPANSION_P (expanded))
16886 /* OK. */;
16887 else if (TREE_VEC_LENGTH (expanded) == 1)
16888 expanded = TREE_VEC_ELT (expanded, 0);
16889 else
16890 expanded = make_argument_pack (expanded);
16892 if (TYPE_P (expanded))
16893 return cxx_sizeof_or_alignof_type (input_location,
16894 expanded, SIZEOF_EXPR,
16895 false,
16896 complain & tf_error);
16897 else
16898 return cxx_sizeof_or_alignof_expr (input_location,
16899 expanded, SIZEOF_EXPR,
16900 false,
16901 complain & tf_error);
16903 else
16904 return build_int_cst (size_type_node, len);
16906 if (SIZEOF_EXPR_TYPE_P (t))
16908 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16909 args, complain, in_decl);
16910 r = build1 (NOP_EXPR, r, error_mark_node);
16911 r = build1 (SIZEOF_EXPR,
16912 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16913 SIZEOF_EXPR_TYPE_P (r) = 1;
16914 return r;
16916 /* Fall through */
16918 case INDIRECT_REF:
16919 case NEGATE_EXPR:
16920 case TRUTH_NOT_EXPR:
16921 case BIT_NOT_EXPR:
16922 case ADDR_EXPR:
16923 case UNARY_PLUS_EXPR: /* Unary + */
16924 case ALIGNOF_EXPR:
16925 case AT_ENCODE_EXPR:
16926 case ARROW_EXPR:
16927 case THROW_EXPR:
16928 case TYPEID_EXPR:
16929 case REALPART_EXPR:
16930 case IMAGPART_EXPR:
16931 case PAREN_EXPR:
16933 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16934 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16935 r = build1 (code, type, op0);
16936 if (code == ALIGNOF_EXPR)
16937 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16938 return r;
16941 case COMPONENT_REF:
16943 tree object;
16944 tree name;
16946 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16947 name = TREE_OPERAND (t, 1);
16948 if (TREE_CODE (name) == BIT_NOT_EXPR)
16950 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16951 complain, in_decl);
16952 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16954 else if (TREE_CODE (name) == SCOPE_REF
16955 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16957 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16958 complain, in_decl);
16959 name = TREE_OPERAND (name, 1);
16960 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16961 complain, in_decl);
16962 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16963 name = build_qualified_name (/*type=*/NULL_TREE,
16964 base, name,
16965 /*template_p=*/false);
16967 else if (BASELINK_P (name))
16968 name = tsubst_baselink (name,
16969 non_reference (TREE_TYPE (object)),
16970 args, complain,
16971 in_decl);
16972 else
16973 name = tsubst_copy (name, args, complain, in_decl);
16974 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16977 case PLUS_EXPR:
16978 case MINUS_EXPR:
16979 case MULT_EXPR:
16980 case TRUNC_DIV_EXPR:
16981 case CEIL_DIV_EXPR:
16982 case FLOOR_DIV_EXPR:
16983 case ROUND_DIV_EXPR:
16984 case EXACT_DIV_EXPR:
16985 case BIT_AND_EXPR:
16986 case BIT_IOR_EXPR:
16987 case BIT_XOR_EXPR:
16988 case TRUNC_MOD_EXPR:
16989 case FLOOR_MOD_EXPR:
16990 case TRUTH_ANDIF_EXPR:
16991 case TRUTH_ORIF_EXPR:
16992 case TRUTH_AND_EXPR:
16993 case TRUTH_OR_EXPR:
16994 case RSHIFT_EXPR:
16995 case LSHIFT_EXPR:
16996 case EQ_EXPR:
16997 case NE_EXPR:
16998 case MAX_EXPR:
16999 case MIN_EXPR:
17000 case LE_EXPR:
17001 case GE_EXPR:
17002 case LT_EXPR:
17003 case GT_EXPR:
17004 case COMPOUND_EXPR:
17005 case DOTSTAR_EXPR:
17006 case MEMBER_REF:
17007 case PREDECREMENT_EXPR:
17008 case PREINCREMENT_EXPR:
17009 case POSTDECREMENT_EXPR:
17010 case POSTINCREMENT_EXPR:
17012 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17013 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17014 return build_nt (code, op0, op1);
17017 case SCOPE_REF:
17019 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17020 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17021 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
17022 QUALIFIED_NAME_IS_TEMPLATE (t));
17025 case ARRAY_REF:
17027 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17028 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17029 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
17032 case CALL_EXPR:
17034 int n = VL_EXP_OPERAND_LENGTH (t);
17035 tree result = build_vl_exp (CALL_EXPR, n);
17036 int i;
17037 for (i = 0; i < n; i++)
17038 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17039 complain, in_decl);
17040 return result;
17043 case COND_EXPR:
17044 case MODOP_EXPR:
17045 case PSEUDO_DTOR_EXPR:
17046 case VEC_PERM_EXPR:
17048 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17049 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17050 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17051 r = build_nt (code, op0, op1, op2);
17052 copy_warning (r, t);
17053 return r;
17056 case NEW_EXPR:
17058 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17059 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17060 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17061 r = build_nt (code, op0, op1, op2);
17062 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17063 return r;
17066 case DELETE_EXPR:
17068 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17069 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17070 r = build_nt (code, op0, op1);
17071 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17072 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17073 return r;
17076 case TEMPLATE_ID_EXPR:
17078 /* Substituted template arguments */
17079 tree tmpl = TREE_OPERAND (t, 0);
17080 tree targs = TREE_OPERAND (t, 1);
17082 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
17083 if (targs)
17084 targs = tsubst_template_args (targs, args, complain, in_decl);
17086 if (variable_template_p (tmpl))
17087 return lookup_template_variable (tmpl, targs);
17088 else
17089 return lookup_template_function (tmpl, targs);
17092 case TREE_LIST:
17094 tree purpose, value, chain;
17096 if (t == void_list_node)
17097 return t;
17099 purpose = TREE_PURPOSE (t);
17100 if (purpose)
17101 purpose = tsubst_copy (purpose, args, complain, in_decl);
17102 value = TREE_VALUE (t);
17103 if (value)
17104 value = tsubst_copy (value, args, complain, in_decl);
17105 chain = TREE_CHAIN (t);
17106 if (chain && chain != void_type_node)
17107 chain = tsubst_copy (chain, args, complain, in_decl);
17108 if (purpose == TREE_PURPOSE (t)
17109 && value == TREE_VALUE (t)
17110 && chain == TREE_CHAIN (t))
17111 return t;
17112 return tree_cons (purpose, value, chain);
17115 case RECORD_TYPE:
17116 case UNION_TYPE:
17117 case ENUMERAL_TYPE:
17118 case INTEGER_TYPE:
17119 case TEMPLATE_TYPE_PARM:
17120 case TEMPLATE_TEMPLATE_PARM:
17121 case BOUND_TEMPLATE_TEMPLATE_PARM:
17122 case TEMPLATE_PARM_INDEX:
17123 case POINTER_TYPE:
17124 case REFERENCE_TYPE:
17125 case OFFSET_TYPE:
17126 case FUNCTION_TYPE:
17127 case METHOD_TYPE:
17128 case ARRAY_TYPE:
17129 case TYPENAME_TYPE:
17130 case UNBOUND_CLASS_TEMPLATE:
17131 case TYPEOF_TYPE:
17132 case DECLTYPE_TYPE:
17133 case TYPE_DECL:
17134 return tsubst (t, args, complain, in_decl);
17136 case USING_DECL:
17137 t = DECL_NAME (t);
17138 /* Fall through. */
17139 case IDENTIFIER_NODE:
17140 if (IDENTIFIER_CONV_OP_P (t))
17142 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17143 return make_conv_op_name (new_type);
17145 else
17146 return t;
17148 case CONSTRUCTOR:
17149 /* This is handled by tsubst_copy_and_build. */
17150 gcc_unreachable ();
17152 case VA_ARG_EXPR:
17154 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17155 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17156 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17159 case CLEANUP_POINT_EXPR:
17160 /* We shouldn't have built any of these during initial template
17161 generation. Instead, they should be built during instantiation
17162 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17163 gcc_unreachable ();
17165 case OFFSET_REF:
17167 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17168 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17169 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17170 r = build2 (code, type, op0, op1);
17171 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17172 if (!mark_used (TREE_OPERAND (r, 1), complain)
17173 && !(complain & tf_error))
17174 return error_mark_node;
17175 return r;
17178 case EXPR_PACK_EXPANSION:
17179 error ("invalid use of pack expansion expression");
17180 return error_mark_node;
17182 case NONTYPE_ARGUMENT_PACK:
17183 error ("use %<...%> to expand argument pack");
17184 return error_mark_node;
17186 case VOID_CST:
17187 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17188 return t;
17190 case INTEGER_CST:
17191 case REAL_CST:
17192 case COMPLEX_CST:
17194 /* Instantiate any typedefs in the type. */
17195 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17196 r = fold_convert (type, t);
17197 gcc_assert (TREE_CODE (r) == code);
17198 return r;
17201 case STRING_CST:
17203 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17204 r = t;
17205 if (type != TREE_TYPE (t))
17207 r = copy_node (t);
17208 TREE_TYPE (r) = type;
17210 return r;
17213 case PTRMEM_CST:
17214 /* These can sometimes show up in a partial instantiation, but never
17215 involve template parms. */
17216 gcc_assert (!uses_template_parms (t));
17217 return t;
17219 case UNARY_LEFT_FOLD_EXPR:
17220 return tsubst_unary_left_fold (t, args, complain, in_decl);
17221 case UNARY_RIGHT_FOLD_EXPR:
17222 return tsubst_unary_right_fold (t, args, complain, in_decl);
17223 case BINARY_LEFT_FOLD_EXPR:
17224 return tsubst_binary_left_fold (t, args, complain, in_decl);
17225 case BINARY_RIGHT_FOLD_EXPR:
17226 return tsubst_binary_right_fold (t, args, complain, in_decl);
17227 case PREDICT_EXPR:
17228 return t;
17230 case DEBUG_BEGIN_STMT:
17231 /* ??? There's no point in copying it for now, but maybe some
17232 day it will contain more information, such as a pointer back
17233 to the containing function, inlined copy or so. */
17234 return t;
17236 case CO_AWAIT_EXPR:
17237 return tsubst_expr (t, args, complain, in_decl,
17238 /*integral_constant_expression_p=*/false);
17239 break;
17241 default:
17242 /* We shouldn't get here, but keep going if !flag_checking. */
17243 if (flag_checking)
17244 gcc_unreachable ();
17245 return t;
17249 /* Helper function for tsubst_omp_clauses, used for instantiation of
17250 OMP_CLAUSE_DECL of clauses. */
17252 static tree
17253 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17254 tree in_decl, tree *iterator_cache)
17256 if (decl == NULL_TREE)
17257 return NULL_TREE;
17259 /* Handle OpenMP iterators. */
17260 if (TREE_CODE (decl) == TREE_LIST
17261 && TREE_PURPOSE (decl)
17262 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17264 tree ret;
17265 if (iterator_cache[0] == TREE_PURPOSE (decl))
17266 ret = iterator_cache[1];
17267 else
17269 tree *tp = &ret;
17270 begin_scope (sk_omp, NULL);
17271 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17273 *tp = copy_node (it);
17274 TREE_VEC_ELT (*tp, 0)
17275 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17276 TREE_VEC_ELT (*tp, 1)
17277 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17278 /*integral_constant_expression_p=*/false);
17279 TREE_VEC_ELT (*tp, 2)
17280 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17281 /*integral_constant_expression_p=*/false);
17282 TREE_VEC_ELT (*tp, 3)
17283 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17284 /*integral_constant_expression_p=*/false);
17285 TREE_CHAIN (*tp) = NULL_TREE;
17286 tp = &TREE_CHAIN (*tp);
17288 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17289 iterator_cache[0] = TREE_PURPOSE (decl);
17290 iterator_cache[1] = ret;
17292 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17293 args, complain,
17294 in_decl, NULL));
17297 /* Handle an OpenMP array section represented as a TREE_LIST (or
17298 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17299 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17300 TREE_LIST. We can handle it exactly the same as an array section
17301 (purpose, value, and a chain), even though the nomenclature
17302 (low_bound, length, etc) is different. */
17303 if (TREE_CODE (decl) == TREE_LIST)
17305 tree low_bound
17306 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17307 /*integral_constant_expression_p=*/false);
17308 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17309 /*integral_constant_expression_p=*/false);
17310 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17311 in_decl, NULL);
17312 if (TREE_PURPOSE (decl) == low_bound
17313 && TREE_VALUE (decl) == length
17314 && TREE_CHAIN (decl) == chain)
17315 return decl;
17316 tree ret = tree_cons (low_bound, length, chain);
17317 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17318 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17319 return ret;
17321 tree ret = tsubst_expr (decl, args, complain, in_decl,
17322 /*integral_constant_expression_p=*/false);
17323 /* Undo convert_from_reference tsubst_expr could have called. */
17324 if (decl
17325 && REFERENCE_REF_P (ret)
17326 && !REFERENCE_REF_P (decl))
17327 ret = TREE_OPERAND (ret, 0);
17328 return ret;
17331 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17333 static tree
17334 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17335 tree args, tsubst_flags_t complain, tree in_decl)
17337 tree new_clauses = NULL_TREE, nc, oc;
17338 tree linear_no_step = NULL_TREE;
17339 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17341 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17343 nc = copy_node (oc);
17344 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17345 new_clauses = nc;
17347 switch (OMP_CLAUSE_CODE (nc))
17349 case OMP_CLAUSE_LASTPRIVATE:
17350 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17352 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17353 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17354 in_decl, /*integral_constant_expression_p=*/false);
17355 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17356 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17358 /* FALLTHRU */
17359 case OMP_CLAUSE_PRIVATE:
17360 case OMP_CLAUSE_SHARED:
17361 case OMP_CLAUSE_FIRSTPRIVATE:
17362 case OMP_CLAUSE_COPYIN:
17363 case OMP_CLAUSE_COPYPRIVATE:
17364 case OMP_CLAUSE_UNIFORM:
17365 case OMP_CLAUSE_DEPEND:
17366 case OMP_CLAUSE_AFFINITY:
17367 case OMP_CLAUSE_FROM:
17368 case OMP_CLAUSE_TO:
17369 case OMP_CLAUSE_MAP:
17370 case OMP_CLAUSE__CACHE_:
17371 case OMP_CLAUSE_NONTEMPORAL:
17372 case OMP_CLAUSE_USE_DEVICE_PTR:
17373 case OMP_CLAUSE_USE_DEVICE_ADDR:
17374 case OMP_CLAUSE_IS_DEVICE_PTR:
17375 case OMP_CLAUSE_INCLUSIVE:
17376 case OMP_CLAUSE_EXCLUSIVE:
17377 OMP_CLAUSE_DECL (nc)
17378 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17379 in_decl, iterator_cache);
17380 break;
17381 case OMP_CLAUSE_TILE:
17382 case OMP_CLAUSE_IF:
17383 case OMP_CLAUSE_NUM_THREADS:
17384 case OMP_CLAUSE_SCHEDULE:
17385 case OMP_CLAUSE_COLLAPSE:
17386 case OMP_CLAUSE_FINAL:
17387 case OMP_CLAUSE_DEVICE:
17388 case OMP_CLAUSE_DIST_SCHEDULE:
17389 case OMP_CLAUSE_NUM_TEAMS:
17390 case OMP_CLAUSE_THREAD_LIMIT:
17391 case OMP_CLAUSE_SAFELEN:
17392 case OMP_CLAUSE_SIMDLEN:
17393 case OMP_CLAUSE_NUM_TASKS:
17394 case OMP_CLAUSE_GRAINSIZE:
17395 case OMP_CLAUSE_PRIORITY:
17396 case OMP_CLAUSE_ORDERED:
17397 case OMP_CLAUSE_HINT:
17398 case OMP_CLAUSE_FILTER:
17399 case OMP_CLAUSE_NUM_GANGS:
17400 case OMP_CLAUSE_NUM_WORKERS:
17401 case OMP_CLAUSE_VECTOR_LENGTH:
17402 case OMP_CLAUSE_WORKER:
17403 case OMP_CLAUSE_VECTOR:
17404 case OMP_CLAUSE_ASYNC:
17405 case OMP_CLAUSE_WAIT:
17406 case OMP_CLAUSE_DETACH:
17407 OMP_CLAUSE_OPERAND (nc, 0)
17408 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17409 in_decl, /*integral_constant_expression_p=*/false);
17410 break;
17411 case OMP_CLAUSE_REDUCTION:
17412 case OMP_CLAUSE_IN_REDUCTION:
17413 case OMP_CLAUSE_TASK_REDUCTION:
17414 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17416 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17417 if (TREE_CODE (placeholder) == SCOPE_REF)
17419 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17420 complain, in_decl);
17421 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17422 = build_qualified_name (NULL_TREE, scope,
17423 TREE_OPERAND (placeholder, 1),
17424 false);
17426 else
17427 gcc_assert (identifier_p (placeholder));
17429 OMP_CLAUSE_DECL (nc)
17430 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17431 in_decl, NULL);
17432 break;
17433 case OMP_CLAUSE_GANG:
17434 case OMP_CLAUSE_ALIGNED:
17435 case OMP_CLAUSE_ALLOCATE:
17436 OMP_CLAUSE_DECL (nc)
17437 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17438 in_decl, NULL);
17439 OMP_CLAUSE_OPERAND (nc, 1)
17440 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17441 in_decl, /*integral_constant_expression_p=*/false);
17442 break;
17443 case OMP_CLAUSE_LINEAR:
17444 OMP_CLAUSE_DECL (nc)
17445 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17446 in_decl, NULL);
17447 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17449 gcc_assert (!linear_no_step);
17450 linear_no_step = nc;
17452 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17453 OMP_CLAUSE_LINEAR_STEP (nc)
17454 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17455 complain, in_decl, NULL);
17456 else
17457 OMP_CLAUSE_LINEAR_STEP (nc)
17458 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17459 in_decl,
17460 /*integral_constant_expression_p=*/false);
17461 break;
17462 case OMP_CLAUSE_NOWAIT:
17463 case OMP_CLAUSE_DEFAULT:
17464 case OMP_CLAUSE_UNTIED:
17465 case OMP_CLAUSE_MERGEABLE:
17466 case OMP_CLAUSE_INBRANCH:
17467 case OMP_CLAUSE_NOTINBRANCH:
17468 case OMP_CLAUSE_PROC_BIND:
17469 case OMP_CLAUSE_FOR:
17470 case OMP_CLAUSE_PARALLEL:
17471 case OMP_CLAUSE_SECTIONS:
17472 case OMP_CLAUSE_TASKGROUP:
17473 case OMP_CLAUSE_NOGROUP:
17474 case OMP_CLAUSE_THREADS:
17475 case OMP_CLAUSE_SIMD:
17476 case OMP_CLAUSE_DEFAULTMAP:
17477 case OMP_CLAUSE_ORDER:
17478 case OMP_CLAUSE_BIND:
17479 case OMP_CLAUSE_INDEPENDENT:
17480 case OMP_CLAUSE_AUTO:
17481 case OMP_CLAUSE_SEQ:
17482 case OMP_CLAUSE_IF_PRESENT:
17483 case OMP_CLAUSE_FINALIZE:
17484 case OMP_CLAUSE_NOHOST:
17485 break;
17486 default:
17487 gcc_unreachable ();
17489 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17490 switch (OMP_CLAUSE_CODE (nc))
17492 case OMP_CLAUSE_SHARED:
17493 case OMP_CLAUSE_PRIVATE:
17494 case OMP_CLAUSE_FIRSTPRIVATE:
17495 case OMP_CLAUSE_LASTPRIVATE:
17496 case OMP_CLAUSE_COPYPRIVATE:
17497 case OMP_CLAUSE_LINEAR:
17498 case OMP_CLAUSE_REDUCTION:
17499 case OMP_CLAUSE_IN_REDUCTION:
17500 case OMP_CLAUSE_TASK_REDUCTION:
17501 case OMP_CLAUSE_USE_DEVICE_PTR:
17502 case OMP_CLAUSE_USE_DEVICE_ADDR:
17503 case OMP_CLAUSE_IS_DEVICE_PTR:
17504 case OMP_CLAUSE_INCLUSIVE:
17505 case OMP_CLAUSE_EXCLUSIVE:
17506 case OMP_CLAUSE_ALLOCATE:
17507 /* tsubst_expr on SCOPE_REF results in returning
17508 finish_non_static_data_member result. Undo that here. */
17509 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17510 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17511 == IDENTIFIER_NODE))
17513 tree t = OMP_CLAUSE_DECL (nc);
17514 tree v = t;
17515 while (v)
17516 switch (TREE_CODE (v))
17518 case COMPONENT_REF:
17519 case MEM_REF:
17520 case INDIRECT_REF:
17521 CASE_CONVERT:
17522 case POINTER_PLUS_EXPR:
17523 v = TREE_OPERAND (v, 0);
17524 continue;
17525 case PARM_DECL:
17526 if (DECL_CONTEXT (v) == current_function_decl
17527 && DECL_ARTIFICIAL (v)
17528 && DECL_NAME (v) == this_identifier)
17529 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17530 /* FALLTHRU */
17531 default:
17532 v = NULL_TREE;
17533 break;
17536 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17537 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17538 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17539 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17540 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17542 tree decl = OMP_CLAUSE_DECL (nc);
17543 if (VAR_P (decl))
17545 retrofit_lang_decl (decl);
17546 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17549 break;
17550 default:
17551 break;
17555 new_clauses = nreverse (new_clauses);
17556 if (ort != C_ORT_OMP_DECLARE_SIMD)
17558 new_clauses = finish_omp_clauses (new_clauses, ort);
17559 if (linear_no_step)
17560 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17561 if (nc == linear_no_step)
17563 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17564 break;
17567 return new_clauses;
17570 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17572 static tree
17573 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17574 tree in_decl)
17576 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17578 tree purpose, value, chain;
17580 if (t == NULL)
17581 return t;
17583 if (TREE_CODE (t) != TREE_LIST)
17584 return tsubst_copy_and_build (t, args, complain, in_decl,
17585 /*function_p=*/false,
17586 /*integral_constant_expression_p=*/false);
17588 if (t == void_list_node)
17589 return t;
17591 purpose = TREE_PURPOSE (t);
17592 if (purpose)
17593 purpose = RECUR (purpose);
17594 value = TREE_VALUE (t);
17595 if (value)
17597 if (TREE_CODE (value) != LABEL_DECL)
17598 value = RECUR (value);
17599 else
17601 value = lookup_label (DECL_NAME (value));
17602 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17603 TREE_USED (value) = 1;
17606 chain = TREE_CHAIN (t);
17607 if (chain && chain != void_type_node)
17608 chain = RECUR (chain);
17609 return tree_cons (purpose, value, chain);
17610 #undef RECUR
17613 /* Used to temporarily communicate the list of #pragma omp parallel
17614 clauses to #pragma omp for instantiation if they are combined
17615 together. */
17617 static tree *omp_parallel_combined_clauses;
17619 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17620 tree *, unsigned int *);
17622 /* Substitute one OMP_FOR iterator. */
17624 static bool
17625 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17626 tree initv, tree condv, tree incrv, tree *clauses,
17627 tree args, tsubst_flags_t complain, tree in_decl,
17628 bool integral_constant_expression_p)
17630 #define RECUR(NODE) \
17631 tsubst_expr ((NODE), args, complain, in_decl, \
17632 integral_constant_expression_p)
17633 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17634 bool ret = false;
17636 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17637 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17639 decl = TREE_OPERAND (init, 0);
17640 init = TREE_OPERAND (init, 1);
17641 tree decl_expr = NULL_TREE;
17642 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17643 if (range_for)
17645 bool decomp = false;
17646 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17648 tree v = DECL_VALUE_EXPR (decl);
17649 if (TREE_CODE (v) == ARRAY_REF
17650 && VAR_P (TREE_OPERAND (v, 0))
17651 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17653 tree decomp_first = NULL_TREE;
17654 unsigned decomp_cnt = 0;
17655 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17656 maybe_push_decl (d);
17657 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17658 in_decl, &decomp_first, &decomp_cnt);
17659 decomp = true;
17660 if (d == error_mark_node)
17661 decl = error_mark_node;
17662 else
17663 for (unsigned int i = 0; i < decomp_cnt; i++)
17665 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17667 tree v = build_nt (ARRAY_REF, d,
17668 size_int (decomp_cnt - i - 1),
17669 NULL_TREE, NULL_TREE);
17670 SET_DECL_VALUE_EXPR (decomp_first, v);
17671 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17673 fit_decomposition_lang_decl (decomp_first, d);
17674 decomp_first = DECL_CHAIN (decomp_first);
17678 decl = tsubst_decl (decl, args, complain);
17679 if (!decomp)
17680 maybe_push_decl (decl);
17682 else if (init && TREE_CODE (init) == DECL_EXPR)
17684 /* We need to jump through some hoops to handle declarations in the
17685 init-statement, since we might need to handle auto deduction,
17686 but we need to keep control of initialization. */
17687 decl_expr = init;
17688 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17689 decl = tsubst_decl (decl, args, complain);
17691 else
17693 if (TREE_CODE (decl) == SCOPE_REF)
17695 decl = RECUR (decl);
17696 if (TREE_CODE (decl) == COMPONENT_REF)
17698 tree v = decl;
17699 while (v)
17700 switch (TREE_CODE (v))
17702 case COMPONENT_REF:
17703 case MEM_REF:
17704 case INDIRECT_REF:
17705 CASE_CONVERT:
17706 case POINTER_PLUS_EXPR:
17707 v = TREE_OPERAND (v, 0);
17708 continue;
17709 case PARM_DECL:
17710 if (DECL_CONTEXT (v) == current_function_decl
17711 && DECL_ARTIFICIAL (v)
17712 && DECL_NAME (v) == this_identifier)
17714 decl = TREE_OPERAND (decl, 1);
17715 decl = omp_privatize_field (decl, false);
17717 /* FALLTHRU */
17718 default:
17719 v = NULL_TREE;
17720 break;
17724 else
17725 decl = RECUR (decl);
17727 if (init && TREE_CODE (init) == TREE_VEC)
17729 init = copy_node (init);
17730 TREE_VEC_ELT (init, 0)
17731 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17732 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17733 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17735 else
17736 init = RECUR (init);
17738 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17740 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17741 if (TREE_CODE (o) == TREE_LIST)
17742 TREE_VEC_ELT (orig_declv, i)
17743 = tree_cons (RECUR (TREE_PURPOSE (o)),
17744 RECUR (TREE_VALUE (o)),
17745 NULL_TREE);
17746 else
17747 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17750 if (range_for)
17752 tree this_pre_body = NULL_TREE;
17753 tree orig_init = NULL_TREE;
17754 tree orig_decl = NULL_TREE;
17755 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17756 orig_init, cond, incr);
17757 if (orig_decl)
17759 if (orig_declv == NULL_TREE)
17760 orig_declv = copy_node (declv);
17761 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17762 ret = true;
17764 else if (orig_declv)
17765 TREE_VEC_ELT (orig_declv, i) = decl;
17768 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17769 if (!range_for && auto_node && init)
17770 TREE_TYPE (decl)
17771 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17773 gcc_assert (!type_dependent_expression_p (decl));
17775 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17777 if (decl_expr)
17779 /* Declare the variable, but don't let that initialize it. */
17780 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17781 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17782 RECUR (decl_expr);
17783 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17786 if (!range_for)
17788 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17789 if (COMPARISON_CLASS_P (cond)
17790 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17792 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17793 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17794 TREE_VEC_ELT (rhs, 0)
17795 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17796 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17797 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17798 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17799 lhs, rhs);
17801 else
17802 cond = RECUR (cond);
17803 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17804 if (TREE_CODE (incr) == MODIFY_EXPR)
17806 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17807 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17808 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17809 NOP_EXPR, rhs, complain);
17811 else
17812 incr = RECUR (incr);
17813 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17814 TREE_VEC_ELT (orig_declv, i) = decl;
17816 TREE_VEC_ELT (declv, i) = decl;
17817 TREE_VEC_ELT (initv, i) = init;
17818 TREE_VEC_ELT (condv, i) = cond;
17819 TREE_VEC_ELT (incrv, i) = incr;
17820 return ret;
17823 if (decl_expr)
17825 /* Declare and initialize the variable. */
17826 RECUR (decl_expr);
17827 init = NULL_TREE;
17829 else if (init)
17831 tree *pc;
17832 int j;
17833 for (j = ((omp_parallel_combined_clauses == NULL
17834 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17836 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17838 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17839 && OMP_CLAUSE_DECL (*pc) == decl)
17840 break;
17841 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17842 && OMP_CLAUSE_DECL (*pc) == decl)
17844 if (j)
17845 break;
17846 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17847 tree c = *pc;
17848 *pc = OMP_CLAUSE_CHAIN (c);
17849 OMP_CLAUSE_CHAIN (c) = *clauses;
17850 *clauses = c;
17852 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17853 && OMP_CLAUSE_DECL (*pc) == decl)
17855 error ("iteration variable %qD should not be firstprivate",
17856 decl);
17857 *pc = OMP_CLAUSE_CHAIN (*pc);
17859 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17860 && OMP_CLAUSE_DECL (*pc) == decl)
17862 error ("iteration variable %qD should not be reduction",
17863 decl);
17864 *pc = OMP_CLAUSE_CHAIN (*pc);
17866 else
17867 pc = &OMP_CLAUSE_CHAIN (*pc);
17869 if (*pc)
17870 break;
17872 if (*pc == NULL_TREE)
17874 tree c = build_omp_clause (input_location,
17875 TREE_CODE (t) == OMP_LOOP
17876 ? OMP_CLAUSE_LASTPRIVATE
17877 : OMP_CLAUSE_PRIVATE);
17878 OMP_CLAUSE_DECL (c) = decl;
17879 c = finish_omp_clauses (c, C_ORT_OMP);
17880 if (c)
17882 OMP_CLAUSE_CHAIN (c) = *clauses;
17883 *clauses = c;
17887 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17888 if (COMPARISON_CLASS_P (cond))
17890 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17891 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17892 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17894 else
17895 cond = RECUR (cond);
17896 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17897 switch (TREE_CODE (incr))
17899 case PREINCREMENT_EXPR:
17900 case PREDECREMENT_EXPR:
17901 case POSTINCREMENT_EXPR:
17902 case POSTDECREMENT_EXPR:
17903 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17904 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17905 break;
17906 case MODIFY_EXPR:
17907 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17908 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17910 tree rhs = TREE_OPERAND (incr, 1);
17911 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17912 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17913 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17914 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17915 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17916 rhs0, rhs1));
17918 else
17919 incr = RECUR (incr);
17920 break;
17921 case MODOP_EXPR:
17922 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17923 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17925 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17926 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17927 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17928 TREE_TYPE (decl), lhs,
17929 RECUR (TREE_OPERAND (incr, 2))));
17931 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17932 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17933 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17935 tree rhs = TREE_OPERAND (incr, 2);
17936 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17937 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17938 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17939 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17940 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17941 rhs0, rhs1));
17943 else
17944 incr = RECUR (incr);
17945 break;
17946 default:
17947 incr = RECUR (incr);
17948 break;
17951 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17952 TREE_VEC_ELT (orig_declv, i) = decl;
17953 TREE_VEC_ELT (declv, i) = decl;
17954 TREE_VEC_ELT (initv, i) = init;
17955 TREE_VEC_ELT (condv, i) = cond;
17956 TREE_VEC_ELT (incrv, i) = incr;
17957 return false;
17958 #undef RECUR
17961 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17962 of OMP_TARGET's body. */
17964 static tree
17965 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17967 *walk_subtrees = 0;
17968 switch (TREE_CODE (*tp))
17970 case OMP_TEAMS:
17971 return *tp;
17972 case BIND_EXPR:
17973 case STATEMENT_LIST:
17974 *walk_subtrees = 1;
17975 break;
17976 default:
17977 break;
17979 return NULL_TREE;
17982 /* Helper function for tsubst_expr. For decomposition declaration
17983 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17984 also the corresponding decls representing the identifiers
17985 of the decomposition declaration. Return DECL if successful
17986 or error_mark_node otherwise, set *FIRST to the first decl
17987 in the list chained through DECL_CHAIN and *CNT to the number
17988 of such decls. */
17990 static tree
17991 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17992 tsubst_flags_t complain, tree in_decl, tree *first,
17993 unsigned int *cnt)
17995 tree decl2, decl3, prev = decl;
17996 *cnt = 0;
17997 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17998 for (decl2 = DECL_CHAIN (pattern_decl);
17999 decl2
18000 && VAR_P (decl2)
18001 && DECL_DECOMPOSITION_P (decl2)
18002 && DECL_NAME (decl2);
18003 decl2 = DECL_CHAIN (decl2))
18005 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
18007 gcc_assert (errorcount);
18008 return error_mark_node;
18010 (*cnt)++;
18011 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18012 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18013 tree v = DECL_VALUE_EXPR (decl2);
18014 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18015 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18016 decl3 = tsubst (decl2, args, complain, in_decl);
18017 SET_DECL_VALUE_EXPR (decl2, v);
18018 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18019 if (VAR_P (decl3))
18020 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18021 else
18023 gcc_assert (errorcount);
18024 decl = error_mark_node;
18025 continue;
18027 maybe_push_decl (decl3);
18028 if (error_operand_p (decl3))
18029 decl = error_mark_node;
18030 else if (decl != error_mark_node
18031 && DECL_CHAIN (decl3) != prev
18032 && decl != prev)
18034 gcc_assert (errorcount);
18035 decl = error_mark_node;
18037 else
18038 prev = decl3;
18040 *first = prev;
18041 return decl;
18044 /* Return the proper local_specialization for init-capture pack DECL. */
18046 static tree
18047 lookup_init_capture_pack (tree decl)
18049 /* We handle normal pack captures by forwarding to the specialization of the
18050 captured parameter. We can't do that for pack init-captures; we need them
18051 to have their own local_specialization. We created the individual
18052 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18053 when we process the DECL_EXPR for the pack init-capture in the template.
18054 So, how do we find them? We don't know the capture proxy pack when
18055 building the individual resulting proxies, and we don't know the
18056 individual proxies when instantiating the pack. What we have in common is
18057 the FIELD_DECL.
18059 So...when we instantiate the FIELD_DECL, we stick the result in
18060 local_specializations. Then at the DECL_EXPR we look up that result, see
18061 how many elements it has, synthesize the names, and look them up. */
18063 tree cname = DECL_NAME (decl);
18064 tree val = DECL_VALUE_EXPR (decl);
18065 tree field = TREE_OPERAND (val, 1);
18066 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18067 tree fpack = retrieve_local_specialization (field);
18068 if (fpack == error_mark_node)
18069 return error_mark_node;
18071 int len = 1;
18072 tree vec = NULL_TREE;
18073 tree r = NULL_TREE;
18074 if (TREE_CODE (fpack) == TREE_VEC)
18076 len = TREE_VEC_LENGTH (fpack);
18077 vec = make_tree_vec (len);
18078 r = make_node (NONTYPE_ARGUMENT_PACK);
18079 SET_ARGUMENT_PACK_ARGS (r, vec);
18081 for (int i = 0; i < len; ++i)
18083 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18084 tree elt = lookup_name (ename);
18085 if (vec)
18086 TREE_VEC_ELT (vec, i) = elt;
18087 else
18088 r = elt;
18090 return r;
18093 /* Like tsubst_copy for expressions, etc. but also does semantic
18094 processing. */
18096 tree
18097 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18098 bool integral_constant_expression_p)
18100 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18101 #define RECUR(NODE) \
18102 tsubst_expr ((NODE), args, complain, in_decl, \
18103 integral_constant_expression_p)
18105 tree stmt, tmp;
18106 tree r;
18107 location_t loc;
18109 if (t == NULL_TREE || t == error_mark_node)
18110 return t;
18112 loc = input_location;
18113 if (location_t eloc = cp_expr_location (t))
18114 input_location = eloc;
18115 if (STATEMENT_CODE_P (TREE_CODE (t)))
18116 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18118 switch (TREE_CODE (t))
18120 case STATEMENT_LIST:
18122 for (tree stmt : tsi_range (t))
18123 RECUR (stmt);
18124 break;
18127 case CTOR_INITIALIZER:
18128 finish_mem_initializers (tsubst_initializer_list
18129 (TREE_OPERAND (t, 0), args));
18130 break;
18132 case RETURN_EXPR:
18133 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18134 break;
18136 case CO_RETURN_EXPR:
18137 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18138 break;
18140 case CO_YIELD_EXPR:
18141 stmt = finish_co_yield_expr (input_location,
18142 RECUR (TREE_OPERAND (t, 0)));
18143 RETURN (stmt);
18144 break;
18146 case CO_AWAIT_EXPR:
18147 stmt = finish_co_await_expr (input_location,
18148 RECUR (TREE_OPERAND (t, 0)));
18149 RETURN (stmt);
18150 break;
18152 case EXPR_STMT:
18153 tmp = RECUR (EXPR_STMT_EXPR (t));
18154 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18155 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18156 else
18157 finish_expr_stmt (tmp);
18158 break;
18160 case USING_STMT:
18161 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18162 break;
18164 case DECL_EXPR:
18166 tree decl, pattern_decl;
18167 tree init;
18169 pattern_decl = decl = DECL_EXPR_DECL (t);
18170 if (TREE_CODE (decl) == LABEL_DECL)
18171 finish_label_decl (DECL_NAME (decl));
18172 else if (TREE_CODE (decl) == USING_DECL)
18174 tree scope = USING_DECL_SCOPE (decl);
18175 if (DECL_DEPENDENT_P (decl))
18177 scope = tsubst (scope, args, complain, in_decl);
18178 if (!MAYBE_CLASS_TYPE_P (scope)
18179 && TREE_CODE (scope) != ENUMERAL_TYPE)
18181 if (complain & tf_error)
18182 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18183 "class, namespace, or enumeration", scope);
18184 return error_mark_node;
18186 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18188 else
18190 /* This is a non-dependent using-decl, and we'll have
18191 used the names it found during template parsing. We do
18192 not want to do the lookup again, because we might not
18193 find the things we found then. */
18194 gcc_checking_assert (scope == tsubst (scope, args,
18195 complain, in_decl));
18196 /* We still need to push the bindings so that we can look up
18197 this name later. */
18198 push_using_decl_bindings (DECL_NAME (decl),
18199 USING_DECL_DECLS (decl));
18202 else if (is_capture_proxy (decl)
18203 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18205 /* We're in tsubst_lambda_expr, we've already inserted a new
18206 capture proxy, so look it up and register it. */
18207 tree inst;
18208 if (!DECL_PACK_P (decl))
18210 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18211 LOOK_want::HIDDEN_LAMBDA);
18212 gcc_assert (inst != decl && is_capture_proxy (inst));
18214 else if (is_normal_capture_proxy (decl))
18216 inst = (retrieve_local_specialization
18217 (DECL_CAPTURED_VARIABLE (decl)));
18218 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18219 || DECL_PACK_P (inst));
18221 else
18222 inst = lookup_init_capture_pack (decl);
18224 register_local_specialization (inst, decl);
18225 break;
18227 else if (DECL_PRETTY_FUNCTION_P (decl))
18228 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18229 DECL_NAME (decl),
18230 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18231 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18232 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18233 /* Don't copy the old closure; we'll create a new one in
18234 tsubst_lambda_expr. */
18235 break;
18236 else
18238 init = DECL_INITIAL (decl);
18239 decl = tsubst (decl, args, complain, in_decl);
18240 if (decl != error_mark_node)
18242 /* By marking the declaration as instantiated, we avoid
18243 trying to instantiate it. Since instantiate_decl can't
18244 handle local variables, and since we've already done
18245 all that needs to be done, that's the right thing to
18246 do. */
18247 if (VAR_P (decl))
18248 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18249 if (VAR_P (decl) && !DECL_NAME (decl)
18250 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18251 /* Anonymous aggregates are a special case. */
18252 finish_anon_union (decl);
18253 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18255 DECL_CONTEXT (decl) = current_function_decl;
18256 if (DECL_NAME (decl) == this_identifier)
18258 tree lam = DECL_CONTEXT (current_function_decl);
18259 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18260 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18262 insert_capture_proxy (decl);
18264 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18265 /* We already did a pushtag. */;
18266 else if (VAR_OR_FUNCTION_DECL_P (decl)
18267 && DECL_LOCAL_DECL_P (decl))
18269 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18270 DECL_CONTEXT (decl) = NULL_TREE;
18271 decl = pushdecl (decl);
18272 if (TREE_CODE (decl) == FUNCTION_DECL
18273 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18274 && cp_check_omp_declare_reduction (decl))
18275 instantiate_body (pattern_decl, args, decl, true);
18277 else
18279 bool const_init = false;
18280 unsigned int cnt = 0;
18281 tree first = NULL_TREE, ndecl = error_mark_node;
18282 tree asmspec_tree = NULL_TREE;
18283 maybe_push_decl (decl);
18285 if (VAR_P (decl)
18286 && DECL_DECOMPOSITION_P (decl)
18287 && TREE_TYPE (pattern_decl) != error_mark_node)
18288 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18289 complain, in_decl, &first,
18290 &cnt);
18292 init = tsubst_init (init, decl, args, complain, in_decl);
18294 if (VAR_P (decl))
18295 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18296 (pattern_decl));
18298 if (ndecl != error_mark_node)
18299 cp_maybe_mangle_decomp (ndecl, first, cnt);
18301 /* In a non-template function, VLA type declarations are
18302 handled in grokdeclarator; for templates, handle them
18303 now. */
18304 predeclare_vla (decl);
18306 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18308 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18309 const char *asmspec = IDENTIFIER_POINTER (id);
18310 gcc_assert (asmspec[0] == '*');
18311 asmspec_tree
18312 = build_string (IDENTIFIER_LENGTH (id) - 1,
18313 asmspec + 1);
18314 TREE_TYPE (asmspec_tree) = char_array_type_node;
18317 cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
18319 if (ndecl != error_mark_node)
18320 cp_finish_decomp (ndecl, first, cnt);
18325 break;
18328 case FOR_STMT:
18329 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18330 RECUR (FOR_INIT_STMT (t));
18331 finish_init_stmt (stmt);
18332 tmp = RECUR (FOR_COND (t));
18333 finish_for_cond (tmp, stmt, false, 0);
18334 tmp = RECUR (FOR_EXPR (t));
18335 finish_for_expr (tmp, stmt);
18337 bool prev = note_iteration_stmt_body_start ();
18338 RECUR (FOR_BODY (t));
18339 note_iteration_stmt_body_end (prev);
18341 finish_for_stmt (stmt);
18342 break;
18344 case RANGE_FOR_STMT:
18346 /* Construct another range_for, if this is not a final
18347 substitution (for inside a generic lambda of a
18348 template). Otherwise convert to a regular for. */
18349 tree decl, expr;
18350 stmt = (processing_template_decl
18351 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18352 : begin_for_stmt (NULL_TREE, NULL_TREE));
18353 RECUR (RANGE_FOR_INIT_STMT (t));
18354 decl = RANGE_FOR_DECL (t);
18355 decl = tsubst (decl, args, complain, in_decl);
18356 maybe_push_decl (decl);
18357 expr = RECUR (RANGE_FOR_EXPR (t));
18359 tree decomp_first = NULL_TREE;
18360 unsigned decomp_cnt = 0;
18361 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18362 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18363 complain, in_decl,
18364 &decomp_first, &decomp_cnt);
18366 if (processing_template_decl)
18368 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18369 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18370 finish_range_for_decl (stmt, decl, expr);
18371 if (decomp_first && decl != error_mark_node)
18372 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18374 else
18376 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18377 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18378 stmt = cp_convert_range_for (stmt, decl, expr,
18379 decomp_first, decomp_cnt,
18380 RANGE_FOR_IVDEP (t), unroll);
18383 bool prev = note_iteration_stmt_body_start ();
18384 RECUR (RANGE_FOR_BODY (t));
18385 note_iteration_stmt_body_end (prev);
18386 finish_for_stmt (stmt);
18388 break;
18390 case WHILE_STMT:
18391 stmt = begin_while_stmt ();
18392 tmp = RECUR (WHILE_COND (t));
18393 finish_while_stmt_cond (tmp, stmt, false, 0);
18395 bool prev = note_iteration_stmt_body_start ();
18396 RECUR (WHILE_BODY (t));
18397 note_iteration_stmt_body_end (prev);
18399 finish_while_stmt (stmt);
18400 break;
18402 case DO_STMT:
18403 stmt = begin_do_stmt ();
18405 bool prev = note_iteration_stmt_body_start ();
18406 RECUR (DO_BODY (t));
18407 note_iteration_stmt_body_end (prev);
18409 finish_do_body (stmt);
18410 tmp = RECUR (DO_COND (t));
18411 finish_do_stmt (tmp, stmt, false, 0);
18412 break;
18414 case IF_STMT:
18415 stmt = begin_if_stmt ();
18416 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18417 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
18418 if (IF_STMT_CONSTEXPR_P (t))
18419 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
18420 tmp = RECUR (IF_COND (t));
18421 tmp = finish_if_stmt_cond (tmp, stmt);
18422 if (IF_STMT_CONSTEXPR_P (t)
18423 && instantiation_dependent_expression_p (tmp))
18425 /* We're partially instantiating a generic lambda, but the condition
18426 of the constexpr if is still dependent. Don't substitute into the
18427 branches now, just remember the template arguments. */
18428 do_poplevel (IF_SCOPE (stmt));
18429 IF_COND (stmt) = IF_COND (t);
18430 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18431 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18432 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18433 add_stmt (stmt);
18434 break;
18436 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18437 /* Don't instantiate the THEN_CLAUSE. */;
18438 else if (IF_STMT_CONSTEVAL_P (t))
18440 bool save_in_consteval_if_p = in_consteval_if_p;
18441 in_consteval_if_p = true;
18442 RECUR (THEN_CLAUSE (t));
18443 in_consteval_if_p = save_in_consteval_if_p;
18445 else
18447 tree folded = fold_non_dependent_expr (tmp, complain);
18448 bool inhibit = integer_zerop (folded);
18449 if (inhibit)
18450 ++c_inhibit_evaluation_warnings;
18451 RECUR (THEN_CLAUSE (t));
18452 if (inhibit)
18453 --c_inhibit_evaluation_warnings;
18455 finish_then_clause (stmt);
18457 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18458 /* Don't instantiate the ELSE_CLAUSE. */;
18459 else if (ELSE_CLAUSE (t))
18461 tree folded = fold_non_dependent_expr (tmp, complain);
18462 bool inhibit = integer_nonzerop (folded);
18463 begin_else_clause (stmt);
18464 if (inhibit)
18465 ++c_inhibit_evaluation_warnings;
18466 RECUR (ELSE_CLAUSE (t));
18467 if (inhibit)
18468 --c_inhibit_evaluation_warnings;
18469 finish_else_clause (stmt);
18472 finish_if_stmt (stmt);
18473 break;
18475 case BIND_EXPR:
18476 if (BIND_EXPR_BODY_BLOCK (t))
18477 stmt = begin_function_body ();
18478 else
18479 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18480 ? BCS_TRY_BLOCK : 0);
18482 RECUR (BIND_EXPR_BODY (t));
18484 if (BIND_EXPR_BODY_BLOCK (t))
18485 finish_function_body (stmt);
18486 else
18487 finish_compound_stmt (stmt);
18488 break;
18490 case BREAK_STMT:
18491 finish_break_stmt ();
18492 break;
18494 case CONTINUE_STMT:
18495 finish_continue_stmt ();
18496 break;
18498 case SWITCH_STMT:
18499 stmt = begin_switch_stmt ();
18500 tmp = RECUR (SWITCH_STMT_COND (t));
18501 finish_switch_cond (tmp, stmt);
18502 RECUR (SWITCH_STMT_BODY (t));
18503 finish_switch_stmt (stmt);
18504 break;
18506 case CASE_LABEL_EXPR:
18508 tree decl = CASE_LABEL (t);
18509 tree low = RECUR (CASE_LOW (t));
18510 tree high = RECUR (CASE_HIGH (t));
18511 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18512 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18514 tree label = CASE_LABEL (l);
18515 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18516 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18517 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18520 break;
18522 case LABEL_EXPR:
18524 tree decl = LABEL_EXPR_LABEL (t);
18525 tree label;
18527 label = finish_label_stmt (DECL_NAME (decl));
18528 if (TREE_CODE (label) == LABEL_DECL)
18529 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18530 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18531 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18533 break;
18535 case GOTO_EXPR:
18536 tmp = GOTO_DESTINATION (t);
18537 if (TREE_CODE (tmp) != LABEL_DECL)
18538 /* Computed goto's must be tsubst'd into. On the other hand,
18539 non-computed gotos must not be; the identifier in question
18540 will have no binding. */
18541 tmp = RECUR (tmp);
18542 else
18543 tmp = DECL_NAME (tmp);
18544 finish_goto_stmt (tmp);
18545 break;
18547 case ASM_EXPR:
18549 tree string = RECUR (ASM_STRING (t));
18550 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18551 complain, in_decl);
18552 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18553 complain, in_decl);
18554 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18555 complain, in_decl);
18556 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18557 complain, in_decl);
18558 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18559 outputs, inputs, clobbers, labels,
18560 ASM_INLINE_P (t));
18561 tree asm_expr = tmp;
18562 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18563 asm_expr = TREE_OPERAND (asm_expr, 0);
18564 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18566 break;
18568 case TRY_BLOCK:
18569 if (CLEANUP_P (t))
18571 stmt = begin_try_block ();
18572 RECUR (TRY_STMTS (t));
18573 finish_cleanup_try_block (stmt);
18574 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18576 else
18578 tree compound_stmt = NULL_TREE;
18580 if (FN_TRY_BLOCK_P (t))
18581 stmt = begin_function_try_block (&compound_stmt);
18582 else
18583 stmt = begin_try_block ();
18585 RECUR (TRY_STMTS (t));
18587 if (FN_TRY_BLOCK_P (t))
18588 finish_function_try_block (stmt);
18589 else
18590 finish_try_block (stmt);
18592 RECUR (TRY_HANDLERS (t));
18593 if (FN_TRY_BLOCK_P (t))
18594 finish_function_handler_sequence (stmt, compound_stmt);
18595 else
18596 finish_handler_sequence (stmt);
18598 break;
18600 case HANDLER:
18602 tree decl = HANDLER_PARMS (t);
18604 if (decl)
18606 decl = tsubst (decl, args, complain, in_decl);
18607 /* Prevent instantiate_decl from trying to instantiate
18608 this variable. We've already done all that needs to be
18609 done. */
18610 if (decl != error_mark_node)
18611 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18613 stmt = begin_handler ();
18614 finish_handler_parms (decl, stmt);
18615 RECUR (HANDLER_BODY (t));
18616 finish_handler (stmt);
18618 break;
18620 case TAG_DEFN:
18621 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18622 if (CLASS_TYPE_P (tmp))
18624 /* Local classes are not independent templates; they are
18625 instantiated along with their containing function. And this
18626 way we don't have to deal with pushing out of one local class
18627 to instantiate a member of another local class. */
18628 /* Closures are handled by the LAMBDA_EXPR. */
18629 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18630 complete_type (tmp);
18631 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18632 if ((VAR_P (fld)
18633 || (TREE_CODE (fld) == FUNCTION_DECL
18634 && !DECL_ARTIFICIAL (fld)))
18635 && DECL_TEMPLATE_INSTANTIATION (fld))
18636 instantiate_decl (fld, /*defer_ok=*/false,
18637 /*expl_inst_class=*/false);
18639 break;
18641 case STATIC_ASSERT:
18643 tree condition;
18645 ++c_inhibit_evaluation_warnings;
18646 condition =
18647 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18648 args,
18649 complain, in_decl,
18650 /*integral_constant_expression_p=*/true);
18651 --c_inhibit_evaluation_warnings;
18653 finish_static_assert (condition,
18654 STATIC_ASSERT_MESSAGE (t),
18655 STATIC_ASSERT_SOURCE_LOCATION (t),
18656 /*member_p=*/false, /*show_expr_p=*/true);
18658 break;
18660 case OACC_KERNELS:
18661 case OACC_PARALLEL:
18662 case OACC_SERIAL:
18663 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18664 in_decl);
18665 stmt = begin_omp_parallel ();
18666 RECUR (OMP_BODY (t));
18667 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18668 break;
18670 case OMP_PARALLEL:
18671 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18672 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18673 complain, in_decl);
18674 if (OMP_PARALLEL_COMBINED (t))
18675 omp_parallel_combined_clauses = &tmp;
18676 stmt = begin_omp_parallel ();
18677 RECUR (OMP_PARALLEL_BODY (t));
18678 gcc_assert (omp_parallel_combined_clauses == NULL);
18679 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18680 = OMP_PARALLEL_COMBINED (t);
18681 pop_omp_privatization_clauses (r);
18682 break;
18684 case OMP_TASK:
18685 if (OMP_TASK_BODY (t) == NULL_TREE)
18687 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18688 complain, in_decl);
18689 t = copy_node (t);
18690 OMP_TASK_CLAUSES (t) = tmp;
18691 add_stmt (t);
18692 break;
18694 r = push_omp_privatization_clauses (false);
18695 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18696 complain, in_decl);
18697 stmt = begin_omp_task ();
18698 RECUR (OMP_TASK_BODY (t));
18699 finish_omp_task (tmp, stmt);
18700 pop_omp_privatization_clauses (r);
18701 break;
18703 case OMP_FOR:
18704 case OMP_LOOP:
18705 case OMP_SIMD:
18706 case OMP_DISTRIBUTE:
18707 case OMP_TASKLOOP:
18708 case OACC_LOOP:
18710 tree clauses, body, pre_body;
18711 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18712 tree orig_declv = NULL_TREE;
18713 tree incrv = NULL_TREE;
18714 enum c_omp_region_type ort = C_ORT_OMP;
18715 bool any_range_for = false;
18716 int i;
18718 if (TREE_CODE (t) == OACC_LOOP)
18719 ort = C_ORT_ACC;
18721 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18722 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18723 in_decl);
18724 if (OMP_FOR_INIT (t) != NULL_TREE)
18726 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18727 if (OMP_FOR_ORIG_DECLS (t))
18728 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18729 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18730 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18731 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18734 keep_next_level (true);
18735 stmt = begin_omp_structured_block ();
18737 pre_body = push_stmt_list ();
18738 RECUR (OMP_FOR_PRE_BODY (t));
18739 pre_body = pop_stmt_list (pre_body);
18741 if (OMP_FOR_INIT (t) != NULL_TREE)
18742 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18743 any_range_for
18744 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18745 condv, incrv, &clauses, args,
18746 complain, in_decl,
18747 integral_constant_expression_p);
18748 omp_parallel_combined_clauses = NULL;
18750 if (any_range_for)
18752 gcc_assert (orig_declv);
18753 body = begin_omp_structured_block ();
18754 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18755 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18756 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18757 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18758 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18759 TREE_VEC_ELT (declv, i));
18761 else
18762 body = push_stmt_list ();
18763 RECUR (OMP_FOR_BODY (t));
18764 if (any_range_for)
18765 body = finish_omp_structured_block (body);
18766 else
18767 body = pop_stmt_list (body);
18769 if (OMP_FOR_INIT (t) != NULL_TREE)
18770 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18771 orig_declv, initv, condv, incrv, body, pre_body,
18772 NULL, clauses);
18773 else
18775 t = make_node (TREE_CODE (t));
18776 TREE_TYPE (t) = void_type_node;
18777 OMP_FOR_BODY (t) = body;
18778 OMP_FOR_PRE_BODY (t) = pre_body;
18779 OMP_FOR_CLAUSES (t) = clauses;
18780 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18781 add_stmt (t);
18784 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18785 t));
18786 pop_omp_privatization_clauses (r);
18788 break;
18790 case OMP_SECTIONS:
18791 case OMP_MASKED:
18792 omp_parallel_combined_clauses = NULL;
18793 /* FALLTHRU */
18794 case OMP_SINGLE:
18795 case OMP_SCOPE:
18796 case OMP_TEAMS:
18797 case OMP_CRITICAL:
18798 case OMP_TASKGROUP:
18799 case OMP_SCAN:
18800 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18801 && OMP_TEAMS_COMBINED (t));
18802 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18803 in_decl);
18804 if (TREE_CODE (t) == OMP_TEAMS)
18806 keep_next_level (true);
18807 stmt = begin_omp_structured_block ();
18808 RECUR (OMP_BODY (t));
18809 stmt = finish_omp_structured_block (stmt);
18811 else
18813 stmt = push_stmt_list ();
18814 RECUR (OMP_BODY (t));
18815 stmt = pop_stmt_list (stmt);
18818 if (TREE_CODE (t) == OMP_CRITICAL
18819 && tmp != NULL_TREE
18820 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18822 error_at (OMP_CLAUSE_LOCATION (tmp),
18823 "%<#pragma omp critical%> with %<hint%> clause requires "
18824 "a name, except when %<omp_sync_hint_none%> is used");
18825 RETURN (error_mark_node);
18827 t = copy_node (t);
18828 OMP_BODY (t) = stmt;
18829 OMP_CLAUSES (t) = tmp;
18830 add_stmt (t);
18831 pop_omp_privatization_clauses (r);
18832 break;
18834 case OMP_DEPOBJ:
18835 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18836 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18838 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18839 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18841 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18842 args, complain, in_decl);
18843 if (tmp == NULL_TREE)
18844 tmp = error_mark_node;
18846 else
18848 kind = (enum omp_clause_depend_kind)
18849 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18850 tmp = NULL_TREE;
18852 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18854 else
18855 finish_omp_depobj (EXPR_LOCATION (t), r,
18856 OMP_CLAUSE_DEPEND_SOURCE,
18857 OMP_DEPOBJ_CLAUSES (t));
18858 break;
18860 case OACC_DATA:
18861 case OMP_TARGET_DATA:
18862 case OMP_TARGET:
18863 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
18864 TREE_CODE (t) == OACC_DATA
18865 ? C_ORT_ACC
18866 : TREE_CODE (t) == OMP_TARGET
18867 ? C_ORT_OMP_TARGET : C_ORT_OMP,
18868 args, complain, in_decl);
18869 keep_next_level (true);
18870 stmt = begin_omp_structured_block ();
18872 RECUR (OMP_BODY (t));
18873 stmt = finish_omp_structured_block (stmt);
18875 t = copy_node (t);
18876 OMP_BODY (t) = stmt;
18877 OMP_CLAUSES (t) = tmp;
18878 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18880 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18881 if (teams)
18883 /* For combined target teams, ensure the num_teams and
18884 thread_limit clause expressions are evaluated on the host,
18885 before entering the target construct. */
18886 tree c;
18887 for (c = OMP_TEAMS_CLAUSES (teams);
18888 c; c = OMP_CLAUSE_CHAIN (c))
18889 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18890 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18891 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18893 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18894 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18895 if (expr == error_mark_node)
18896 continue;
18897 tmp = TARGET_EXPR_SLOT (expr);
18898 add_stmt (expr);
18899 OMP_CLAUSE_OPERAND (c, 0) = expr;
18900 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18901 OMP_CLAUSE_FIRSTPRIVATE);
18902 OMP_CLAUSE_DECL (tc) = tmp;
18903 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18904 OMP_TARGET_CLAUSES (t) = tc;
18908 add_stmt (t);
18909 break;
18911 case OACC_DECLARE:
18912 t = copy_node (t);
18913 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18914 complain, in_decl);
18915 OACC_DECLARE_CLAUSES (t) = tmp;
18916 add_stmt (t);
18917 break;
18919 case OMP_TARGET_UPDATE:
18920 case OMP_TARGET_ENTER_DATA:
18921 case OMP_TARGET_EXIT_DATA:
18922 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18923 complain, in_decl);
18924 t = copy_node (t);
18925 OMP_STANDALONE_CLAUSES (t) = tmp;
18926 add_stmt (t);
18927 break;
18929 case OACC_CACHE:
18930 case OACC_ENTER_DATA:
18931 case OACC_EXIT_DATA:
18932 case OACC_UPDATE:
18933 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18934 complain, in_decl);
18935 t = copy_node (t);
18936 OMP_STANDALONE_CLAUSES (t) = tmp;
18937 add_stmt (t);
18938 break;
18940 case OMP_ORDERED:
18941 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18942 complain, in_decl);
18943 stmt = push_stmt_list ();
18944 RECUR (OMP_BODY (t));
18945 stmt = pop_stmt_list (stmt);
18947 t = copy_node (t);
18948 OMP_BODY (t) = stmt;
18949 OMP_ORDERED_CLAUSES (t) = tmp;
18950 add_stmt (t);
18951 break;
18953 case OMP_MASTER:
18954 omp_parallel_combined_clauses = NULL;
18955 /* FALLTHRU */
18956 case OMP_SECTION:
18957 stmt = push_stmt_list ();
18958 RECUR (OMP_BODY (t));
18959 stmt = pop_stmt_list (stmt);
18961 t = copy_node (t);
18962 OMP_BODY (t) = stmt;
18963 add_stmt (t);
18964 break;
18966 case OMP_ATOMIC:
18967 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18968 tmp = NULL_TREE;
18969 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18970 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18971 complain, in_decl);
18972 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18974 tree op1 = TREE_OPERAND (t, 1);
18975 tree rhs1 = NULL_TREE;
18976 tree lhs, rhs;
18977 if (TREE_CODE (op1) == COMPOUND_EXPR)
18979 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18980 op1 = TREE_OPERAND (op1, 1);
18982 lhs = RECUR (TREE_OPERAND (op1, 0));
18983 rhs = RECUR (TREE_OPERAND (op1, 1));
18984 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18985 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18986 OMP_ATOMIC_MEMORY_ORDER (t));
18988 else
18990 tree op1 = TREE_OPERAND (t, 1);
18991 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18992 tree rhs1 = NULL_TREE;
18993 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18994 enum tree_code opcode = NOP_EXPR;
18995 if (code == OMP_ATOMIC_READ)
18997 v = RECUR (TREE_OPERAND (op1, 0));
18998 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19000 else if (code == OMP_ATOMIC_CAPTURE_OLD
19001 || code == OMP_ATOMIC_CAPTURE_NEW)
19003 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19004 v = RECUR (TREE_OPERAND (op1, 0));
19005 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19006 if (TREE_CODE (op11) == COMPOUND_EXPR)
19008 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19009 op11 = TREE_OPERAND (op11, 1);
19011 lhs = RECUR (TREE_OPERAND (op11, 0));
19012 rhs = RECUR (TREE_OPERAND (op11, 1));
19013 opcode = TREE_CODE (op11);
19014 if (opcode == MODIFY_EXPR)
19015 opcode = NOP_EXPR;
19017 else
19019 code = OMP_ATOMIC;
19020 lhs = RECUR (TREE_OPERAND (op1, 0));
19021 rhs = RECUR (TREE_OPERAND (op1, 1));
19023 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19024 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
19026 break;
19028 case TRANSACTION_EXPR:
19030 int flags = 0;
19031 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19032 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19034 if (TRANSACTION_EXPR_IS_STMT (t))
19036 tree body = TRANSACTION_EXPR_BODY (t);
19037 tree noex = NULL_TREE;
19038 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19040 noex = MUST_NOT_THROW_COND (body);
19041 if (noex == NULL_TREE)
19042 noex = boolean_true_node;
19043 body = TREE_OPERAND (body, 0);
19045 stmt = begin_transaction_stmt (input_location, NULL, flags);
19046 RECUR (body);
19047 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19049 else
19051 stmt = build_transaction_expr (EXPR_LOCATION (t),
19052 RECUR (TRANSACTION_EXPR_BODY (t)),
19053 flags, NULL_TREE);
19054 RETURN (stmt);
19057 break;
19059 case MUST_NOT_THROW_EXPR:
19061 tree op0 = RECUR (TREE_OPERAND (t, 0));
19062 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19063 RETURN (build_must_not_throw_expr (op0, cond));
19066 case EXPR_PACK_EXPANSION:
19067 error ("invalid use of pack expansion expression");
19068 RETURN (error_mark_node);
19070 case NONTYPE_ARGUMENT_PACK:
19071 error ("use %<...%> to expand argument pack");
19072 RETURN (error_mark_node);
19074 case COMPOUND_EXPR:
19075 tmp = RECUR (TREE_OPERAND (t, 0));
19076 if (tmp == NULL_TREE)
19077 /* If the first operand was a statement, we're done with it. */
19078 RETURN (RECUR (TREE_OPERAND (t, 1)));
19079 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19080 RECUR (TREE_OPERAND (t, 1)),
19081 complain));
19083 case ANNOTATE_EXPR:
19084 tmp = RECUR (TREE_OPERAND (t, 0));
19085 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19086 TREE_TYPE (tmp), tmp,
19087 RECUR (TREE_OPERAND (t, 1)),
19088 RECUR (TREE_OPERAND (t, 2))));
19090 case PREDICT_EXPR:
19091 RETURN (add_stmt (copy_node (t)));
19093 default:
19094 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19096 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19097 /*function_p=*/false,
19098 integral_constant_expression_p));
19101 RETURN (NULL_TREE);
19102 out:
19103 input_location = loc;
19104 return r;
19105 #undef RECUR
19106 #undef RETURN
19109 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19110 function. For description of the body see comment above
19111 cp_parser_omp_declare_reduction_exprs. */
19113 static void
19114 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19116 if (t == NULL_TREE || t == error_mark_node)
19117 return;
19119 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19121 tree_stmt_iterator tsi;
19122 int i;
19123 tree stmts[7];
19124 memset (stmts, 0, sizeof stmts);
19125 for (i = 0, tsi = tsi_start (t);
19126 i < 7 && !tsi_end_p (tsi);
19127 i++, tsi_next (&tsi))
19128 stmts[i] = tsi_stmt (tsi);
19129 gcc_assert (tsi_end_p (tsi));
19131 if (i >= 3)
19133 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19134 && TREE_CODE (stmts[1]) == DECL_EXPR);
19135 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19136 args, complain, in_decl);
19137 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19138 args, complain, in_decl);
19139 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19140 expect to be pushing it. */
19141 DECL_CONTEXT (omp_out) = current_function_decl;
19142 DECL_CONTEXT (omp_in) = current_function_decl;
19143 keep_next_level (true);
19144 tree block = begin_omp_structured_block ();
19145 tsubst_expr (stmts[2], args, complain, in_decl, false);
19146 block = finish_omp_structured_block (block);
19147 block = maybe_cleanup_point_expr_void (block);
19148 add_decl_expr (omp_out);
19149 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
19150 add_decl_expr (omp_in);
19151 finish_expr_stmt (block);
19153 if (i >= 6)
19155 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19156 && TREE_CODE (stmts[4]) == DECL_EXPR);
19157 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19158 args, complain, in_decl);
19159 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19160 args, complain, in_decl);
19161 DECL_CONTEXT (omp_priv) = current_function_decl;
19162 DECL_CONTEXT (omp_orig) = current_function_decl;
19163 keep_next_level (true);
19164 tree block = begin_omp_structured_block ();
19165 tsubst_expr (stmts[5], args, complain, in_decl, false);
19166 block = finish_omp_structured_block (block);
19167 block = maybe_cleanup_point_expr_void (block);
19168 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19169 add_decl_expr (omp_priv);
19170 add_decl_expr (omp_orig);
19171 finish_expr_stmt (block);
19172 if (i == 7)
19173 add_decl_expr (omp_orig);
19177 /* T is a postfix-expression that is not being used in a function
19178 call. Return the substituted version of T. */
19180 static tree
19181 tsubst_non_call_postfix_expression (tree t, tree args,
19182 tsubst_flags_t complain,
19183 tree in_decl)
19185 if (TREE_CODE (t) == SCOPE_REF)
19186 t = tsubst_qualified_id (t, args, complain, in_decl,
19187 /*done=*/false, /*address_p=*/false);
19188 else
19189 t = tsubst_copy_and_build (t, args, complain, in_decl,
19190 /*function_p=*/false,
19191 /*integral_constant_expression_p=*/false);
19193 return t;
19196 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19197 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19198 dependent init-capture. */
19200 static void
19201 prepend_one_capture (tree field, tree init, tree &list,
19202 tsubst_flags_t complain)
19204 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19206 tree type = NULL_TREE;
19207 if (!init)
19209 if (complain & tf_error)
19210 error ("empty initializer in lambda init-capture");
19211 init = error_mark_node;
19213 else if (TREE_CODE (init) == TREE_LIST)
19214 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19215 if (!type)
19216 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19217 TREE_TYPE (field) = type;
19218 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19220 list = tree_cons (field, init, list);
19223 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19224 instantiation context. Instantiating a pack expansion containing a lambda
19225 might result in multiple lambdas all based on the same lambda in the
19226 template. */
19228 tree
19229 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19231 tree oldfn = lambda_function (t);
19232 in_decl = oldfn;
19234 tree r = build_lambda_expr ();
19236 LAMBDA_EXPR_LOCATION (r)
19237 = LAMBDA_EXPR_LOCATION (t);
19238 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19239 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19240 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19241 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19242 LAMBDA_EXPR_REGEN_INFO (r)
19243 = build_template_info (t, add_to_template_args (TI_ARGS (ti), args));
19244 else
19245 LAMBDA_EXPR_REGEN_INFO (r)
19246 = build_template_info (t, args);
19248 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19249 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19251 vec<tree,va_gc>* field_packs = NULL;
19253 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19254 cap = TREE_CHAIN (cap))
19256 tree ofield = TREE_PURPOSE (cap);
19257 tree init = TREE_VALUE (cap);
19258 if (PACK_EXPANSION_P (init))
19259 init = tsubst_pack_expansion (init, args, complain, in_decl);
19260 else
19261 init = tsubst_copy_and_build (init, args, complain, in_decl,
19262 /*fn*/false, /*constexpr*/false);
19264 if (init == error_mark_node)
19265 return error_mark_node;
19267 if (init && TREE_CODE (init) == TREE_LIST)
19268 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19270 if (!processing_template_decl
19271 && init && TREE_CODE (init) != TREE_VEC
19272 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19274 /* For a VLA, simply tsubsting the field type won't work, we need to
19275 go through add_capture again. XXX do we want to do this for all
19276 captures? */
19277 tree name = (get_identifier
19278 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19279 tree ftype = TREE_TYPE (ofield);
19280 bool by_ref = (TYPE_REF_P (ftype)
19281 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19282 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19283 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19284 continue;
19287 if (PACK_EXPANSION_P (ofield))
19288 ofield = PACK_EXPANSION_PATTERN (ofield);
19289 tree field = tsubst_decl (ofield, args, complain);
19291 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19293 /* Remember these for when we've pushed local_specializations. */
19294 vec_safe_push (field_packs, ofield);
19295 vec_safe_push (field_packs, field);
19298 if (field == error_mark_node)
19299 return error_mark_node;
19301 if (TREE_CODE (field) == TREE_VEC)
19303 int len = TREE_VEC_LENGTH (field);
19304 gcc_assert (TREE_CODE (init) == TREE_VEC
19305 && TREE_VEC_LENGTH (init) == len);
19306 for (int i = 0; i < len; ++i)
19307 prepend_one_capture (TREE_VEC_ELT (field, i),
19308 TREE_VEC_ELT (init, i),
19309 LAMBDA_EXPR_CAPTURE_LIST (r),
19310 complain);
19312 else
19314 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19315 complain);
19317 if (id_equal (DECL_NAME (field), "__this"))
19318 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19322 tree type = begin_lambda_type (r);
19323 if (type == error_mark_node)
19324 return error_mark_node;
19326 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19327 /* A lambda in a default argument outside a class gets no
19328 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19329 tsubst_default_argument calls start_lambda_scope, so we need to
19330 specifically ignore it here, and use the global scope. */
19331 record_null_lambda_scope (r);
19332 else
19333 record_lambda_scope (r);
19335 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19336 determine_visibility (TYPE_NAME (type));
19338 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19340 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19341 ? DECL_TI_TEMPLATE (oldfn)
19342 : NULL_TREE);
19344 tree fntype = static_fn_type (oldfn);
19345 if (oldtmpl)
19346 ++processing_template_decl;
19347 fntype = tsubst (fntype, args, complain, in_decl);
19348 if (oldtmpl)
19349 --processing_template_decl;
19351 if (fntype == error_mark_node)
19352 r = error_mark_node;
19353 else
19355 /* The body of a lambda-expression is not a subexpression of the
19356 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19357 which would be skipped if cp_unevaluated_operand. */
19358 cp_evaluated ev;
19360 /* Fix the type of 'this'. */
19361 fntype = build_memfn_type (fntype, type,
19362 type_memfn_quals (fntype),
19363 type_memfn_rqual (fntype));
19364 tree fn, tmpl;
19365 if (oldtmpl)
19367 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19368 if (tmpl == error_mark_node)
19370 r = error_mark_node;
19371 goto out;
19373 fn = DECL_TEMPLATE_RESULT (tmpl);
19374 finish_member_declaration (tmpl);
19376 else
19378 tmpl = NULL_TREE;
19379 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19380 if (fn == error_mark_node)
19382 r = error_mark_node;
19383 goto out;
19385 finish_member_declaration (fn);
19388 /* Let finish_function set this. */
19389 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19391 bool nested = cfun;
19392 if (nested)
19393 push_function_context ();
19394 else
19395 /* Still increment function_depth so that we don't GC in the
19396 middle of an expression. */
19397 ++function_depth;
19399 local_specialization_stack s (lss_copy);
19401 bool save_in_consteval_if_p = in_consteval_if_p;
19402 in_consteval_if_p = false;
19404 tree body = start_lambda_function (fn, r);
19406 /* Now record them for lookup_init_capture_pack. */
19407 int fplen = vec_safe_length (field_packs);
19408 for (int i = 0; i < fplen; )
19410 tree pack = (*field_packs)[i++];
19411 tree inst = (*field_packs)[i++];
19412 register_local_specialization (inst, pack);
19414 release_tree_vector (field_packs);
19416 register_parameter_specializations (oldfn, fn);
19418 if (oldtmpl)
19420 /* We might not partially instantiate some parts of the function, so
19421 copy these flags from the original template. */
19422 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19423 current_function_returns_value = ol->returns_value;
19424 current_function_returns_null = ol->returns_null;
19425 current_function_returns_abnormally = ol->returns_abnormally;
19426 current_function_infinite_loop = ol->infinite_loop;
19429 /* [temp.deduct] A lambda-expression appearing in a function type or a
19430 template parameter is not considered part of the immediate context for
19431 the purposes of template argument deduction. */
19432 complain = tf_warning_or_error;
19434 tree saved = DECL_SAVED_TREE (oldfn);
19435 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19436 /* We already have a body block from start_lambda_function, we don't
19437 need another to confuse NRV (91217). */
19438 saved = BIND_EXPR_BODY (saved);
19440 tsubst_expr (saved, args, complain, r, /*constexpr*/false);
19442 finish_lambda_function (body);
19444 in_consteval_if_p = save_in_consteval_if_p;
19446 if (nested)
19447 pop_function_context ();
19448 else
19449 --function_depth;
19451 /* The capture list was built up in reverse order; fix that now. */
19452 LAMBDA_EXPR_CAPTURE_LIST (r)
19453 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19455 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19457 maybe_add_lambda_conv_op (type);
19460 out:
19461 finish_struct (type, /*attr*/NULL_TREE);
19463 insert_pending_capture_proxies ();
19465 return r;
19468 /* Like tsubst but deals with expressions and performs semantic
19469 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19470 "F<TARGS> (ARGS)". */
19472 tree
19473 tsubst_copy_and_build (tree t,
19474 tree args,
19475 tsubst_flags_t complain,
19476 tree in_decl,
19477 bool function_p,
19478 bool integral_constant_expression_p)
19480 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19481 #define RECUR(NODE) \
19482 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19483 /*function_p=*/false, \
19484 integral_constant_expression_p)
19486 tree retval, op1;
19487 location_t save_loc;
19489 if (t == NULL_TREE || t == error_mark_node)
19490 return t;
19492 save_loc = input_location;
19493 if (location_t eloc = cp_expr_location (t))
19494 input_location = eloc;
19496 /* N3276 decltype magic only applies to calls at the top level or on the
19497 right side of a comma. */
19498 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19499 complain &= ~tf_decltype;
19501 switch (TREE_CODE (t))
19503 case USING_DECL:
19504 t = DECL_NAME (t);
19505 /* Fall through. */
19506 case IDENTIFIER_NODE:
19508 tree decl;
19509 cp_id_kind idk;
19510 bool non_integral_constant_expression_p;
19511 const char *error_msg;
19513 if (IDENTIFIER_CONV_OP_P (t))
19515 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19516 t = make_conv_op_name (new_type);
19519 /* Look up the name. */
19520 decl = lookup_name (t);
19522 /* By convention, expressions use ERROR_MARK_NODE to indicate
19523 failure, not NULL_TREE. */
19524 if (decl == NULL_TREE)
19525 decl = error_mark_node;
19527 decl = finish_id_expression (t, decl, NULL_TREE,
19528 &idk,
19529 integral_constant_expression_p,
19530 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19531 &non_integral_constant_expression_p,
19532 /*template_p=*/false,
19533 /*done=*/true,
19534 /*address_p=*/false,
19535 /*template_arg_p=*/false,
19536 &error_msg,
19537 input_location);
19538 if (error_msg)
19539 error (error_msg);
19540 if (!function_p && identifier_p (decl))
19542 if (complain & tf_error)
19543 unqualified_name_lookup_error (decl);
19544 decl = error_mark_node;
19546 RETURN (decl);
19549 case TEMPLATE_ID_EXPR:
19551 tree object;
19552 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19553 complain, in_decl,
19554 function_p,
19555 integral_constant_expression_p);
19556 tree targs = TREE_OPERAND (t, 1);
19558 if (targs)
19559 targs = tsubst_template_args (targs, args, complain, in_decl);
19560 if (targs == error_mark_node)
19561 RETURN (error_mark_node);
19563 if (TREE_CODE (templ) == SCOPE_REF)
19565 tree name = TREE_OPERAND (templ, 1);
19566 tree tid = lookup_template_function (name, targs);
19567 TREE_OPERAND (templ, 1) = tid;
19568 RETURN (templ);
19571 if (concept_definition_p (templ))
19573 tree check = build_concept_check (templ, targs, complain);
19574 if (check == error_mark_node)
19575 RETURN (error_mark_node);
19577 tree id = unpack_concept_check (check);
19579 /* If we built a function concept check, return the underlying
19580 template-id. So we can evaluate it as a function call. */
19581 if (function_concept_p (TREE_OPERAND (id, 0)))
19582 RETURN (id);
19584 RETURN (check);
19587 if (variable_template_p (templ))
19589 tree r = lookup_and_finish_template_variable (templ, targs,
19590 complain);
19591 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19592 RETURN (r);
19595 if (TREE_CODE (templ) == COMPONENT_REF)
19597 object = TREE_OPERAND (templ, 0);
19598 templ = TREE_OPERAND (templ, 1);
19600 else
19601 object = NULL_TREE;
19603 tree tid = lookup_template_function (templ, targs);
19605 if (object)
19606 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19607 object, tid, NULL_TREE));
19608 else if (identifier_p (templ))
19610 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19611 name lookup found nothing when parsing the template name. */
19612 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19613 RETURN (tid);
19615 else
19616 RETURN (baselink_for_fns (tid));
19619 case INDIRECT_REF:
19621 tree r = RECUR (TREE_OPERAND (t, 0));
19623 if (REFERENCE_REF_P (t))
19625 /* A type conversion to reference type will be enclosed in
19626 such an indirect ref, but the substitution of the cast
19627 will have also added such an indirect ref. */
19628 r = convert_from_reference (r);
19630 else
19631 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19632 complain|decltype_flag);
19634 if (REF_PARENTHESIZED_P (t))
19635 r = force_paren_expr (r);
19637 RETURN (r);
19640 case NOP_EXPR:
19642 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19643 tree op0 = RECUR (TREE_OPERAND (t, 0));
19644 RETURN (build_nop (type, op0));
19647 case IMPLICIT_CONV_EXPR:
19649 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19650 tree expr = RECUR (TREE_OPERAND (t, 0));
19651 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19653 retval = copy_node (t);
19654 TREE_TYPE (retval) = type;
19655 TREE_OPERAND (retval, 0) = expr;
19656 RETURN (retval);
19658 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19659 /* We'll pass this to convert_nontype_argument again, we don't need
19660 to actually perform any conversion here. */
19661 RETURN (expr);
19662 int flags = LOOKUP_IMPLICIT;
19663 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19664 flags = LOOKUP_NORMAL;
19665 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19666 flags |= LOOKUP_NO_NARROWING;
19667 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19668 flags));
19671 case CONVERT_EXPR:
19673 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19674 tree op0 = RECUR (TREE_OPERAND (t, 0));
19675 if (op0 == error_mark_node)
19676 RETURN (error_mark_node);
19677 RETURN (build1 (CONVERT_EXPR, type, op0));
19680 case CAST_EXPR:
19681 case REINTERPRET_CAST_EXPR:
19682 case CONST_CAST_EXPR:
19683 case DYNAMIC_CAST_EXPR:
19684 case STATIC_CAST_EXPR:
19686 tree type;
19687 tree op, r = NULL_TREE;
19689 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19690 if (integral_constant_expression_p
19691 && !cast_valid_in_integral_constant_expression_p (type))
19693 if (complain & tf_error)
19694 error ("a cast to a type other than an integral or "
19695 "enumeration type cannot appear in a constant-expression");
19696 RETURN (error_mark_node);
19699 op = RECUR (TREE_OPERAND (t, 0));
19701 warning_sentinel s(warn_useless_cast);
19702 warning_sentinel s2(warn_ignored_qualifiers);
19703 warning_sentinel s3(warn_int_in_bool_context);
19704 switch (TREE_CODE (t))
19706 case CAST_EXPR:
19707 r = build_functional_cast (input_location, type, op, complain);
19708 break;
19709 case REINTERPRET_CAST_EXPR:
19710 r = build_reinterpret_cast (input_location, type, op, complain);
19711 break;
19712 case CONST_CAST_EXPR:
19713 r = build_const_cast (input_location, type, op, complain);
19714 break;
19715 case DYNAMIC_CAST_EXPR:
19716 r = build_dynamic_cast (input_location, type, op, complain);
19717 break;
19718 case STATIC_CAST_EXPR:
19719 r = build_static_cast (input_location, type, op, complain);
19720 if (IMPLICIT_RVALUE_P (t))
19721 set_implicit_rvalue_p (r);
19722 break;
19723 default:
19724 gcc_unreachable ();
19727 RETURN (r);
19730 case BIT_CAST_EXPR:
19732 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19733 tree op0 = RECUR (TREE_OPERAND (t, 0));
19734 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19737 case POSTDECREMENT_EXPR:
19738 case POSTINCREMENT_EXPR:
19739 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19740 args, complain, in_decl);
19741 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19742 complain|decltype_flag));
19744 case PREDECREMENT_EXPR:
19745 case PREINCREMENT_EXPR:
19746 case NEGATE_EXPR:
19747 case BIT_NOT_EXPR:
19748 case ABS_EXPR:
19749 case TRUTH_NOT_EXPR:
19750 case UNARY_PLUS_EXPR: /* Unary + */
19751 case REALPART_EXPR:
19752 case IMAGPART_EXPR:
19753 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19754 RECUR (TREE_OPERAND (t, 0)),
19755 complain|decltype_flag));
19757 case FIX_TRUNC_EXPR:
19758 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
19759 gcc_unreachable ();
19761 case ADDR_EXPR:
19762 op1 = TREE_OPERAND (t, 0);
19763 if (TREE_CODE (op1) == LABEL_DECL)
19764 RETURN (finish_label_address_expr (DECL_NAME (op1),
19765 EXPR_LOCATION (op1)));
19766 if (TREE_CODE (op1) == SCOPE_REF)
19767 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19768 /*done=*/true, /*address_p=*/true);
19769 else
19770 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19771 in_decl);
19772 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19773 complain|decltype_flag));
19775 case PLUS_EXPR:
19776 case MINUS_EXPR:
19777 case MULT_EXPR:
19778 case TRUNC_DIV_EXPR:
19779 case CEIL_DIV_EXPR:
19780 case FLOOR_DIV_EXPR:
19781 case ROUND_DIV_EXPR:
19782 case EXACT_DIV_EXPR:
19783 case BIT_AND_EXPR:
19784 case BIT_IOR_EXPR:
19785 case BIT_XOR_EXPR:
19786 case TRUNC_MOD_EXPR:
19787 case FLOOR_MOD_EXPR:
19788 case TRUTH_ANDIF_EXPR:
19789 case TRUTH_ORIF_EXPR:
19790 case TRUTH_AND_EXPR:
19791 case TRUTH_OR_EXPR:
19792 case RSHIFT_EXPR:
19793 case LSHIFT_EXPR:
19794 case EQ_EXPR:
19795 case NE_EXPR:
19796 case MAX_EXPR:
19797 case MIN_EXPR:
19798 case LE_EXPR:
19799 case GE_EXPR:
19800 case LT_EXPR:
19801 case GT_EXPR:
19802 case SPACESHIP_EXPR:
19803 case MEMBER_REF:
19804 case DOTSTAR_EXPR:
19806 /* If either OP0 or OP1 was value- or type-dependent, suppress
19807 warnings that depend on the range of the types involved. */
19808 tree op0 = TREE_OPERAND (t, 0);
19809 tree op1 = TREE_OPERAND (t, 1);
19810 auto dep_p = [](tree t) {
19811 ++processing_template_decl;
19812 bool r = (potential_constant_expression (t)
19813 ? value_dependent_expression_p (t)
19814 : type_dependent_expression_p (t));
19815 --processing_template_decl;
19816 return r;
19818 const bool was_dep = dep_p (op0) || dep_p (op1);
19819 op0 = RECUR (op0);
19820 op1 = RECUR (op1);
19822 warning_sentinel s1(warn_type_limits, was_dep);
19823 warning_sentinel s2(warn_div_by_zero, was_dep);
19824 warning_sentinel s3(warn_logical_op, was_dep);
19825 warning_sentinel s4(warn_tautological_compare, was_dep);
19827 tree r = build_x_binary_op
19828 (input_location, TREE_CODE (t),
19829 op0,
19830 (warning_suppressed_p (TREE_OPERAND (t, 0))
19831 ? ERROR_MARK
19832 : TREE_CODE (TREE_OPERAND (t, 0))),
19833 op1,
19834 (warning_suppressed_p (TREE_OPERAND (t, 1))
19835 ? ERROR_MARK
19836 : TREE_CODE (TREE_OPERAND (t, 1))),
19837 /*overload=*/NULL,
19838 complain|decltype_flag);
19839 if (EXPR_P (r))
19840 copy_warning (r, t);
19842 RETURN (r);
19845 case POINTER_PLUS_EXPR:
19847 tree op0 = RECUR (TREE_OPERAND (t, 0));
19848 if (op0 == error_mark_node)
19849 RETURN (error_mark_node);
19850 tree op1 = RECUR (TREE_OPERAND (t, 1));
19851 if (op1 == error_mark_node)
19852 RETURN (error_mark_node);
19853 RETURN (fold_build_pointer_plus (op0, op1));
19856 case SCOPE_REF:
19857 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19858 /*address_p=*/false));
19860 case BASELINK:
19861 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
19862 args, complain, in_decl));
19864 case ARRAY_REF:
19865 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19866 args, complain, in_decl);
19867 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19868 RECUR (TREE_OPERAND (t, 1)),
19869 complain|decltype_flag));
19871 case SIZEOF_EXPR:
19872 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19873 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19874 RETURN (tsubst_copy (t, args, complain, in_decl));
19875 /* Fall through */
19877 case ALIGNOF_EXPR:
19879 tree r;
19881 op1 = TREE_OPERAND (t, 0);
19882 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19883 op1 = TREE_TYPE (op1);
19884 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19885 && ALIGNOF_EXPR_STD_P (t));
19886 if (!args)
19888 /* When there are no ARGS, we are trying to evaluate a
19889 non-dependent expression from the parser. Trying to do
19890 the substitutions may not work. */
19891 if (!TYPE_P (op1))
19892 op1 = TREE_TYPE (op1);
19894 else
19896 ++cp_unevaluated_operand;
19897 ++c_inhibit_evaluation_warnings;
19898 if (TYPE_P (op1))
19899 op1 = tsubst (op1, args, complain, in_decl);
19900 else
19901 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19902 /*function_p=*/false,
19903 /*integral_constant_expression_p=*/
19904 false);
19905 --cp_unevaluated_operand;
19906 --c_inhibit_evaluation_warnings;
19908 if (TYPE_P (op1))
19909 r = cxx_sizeof_or_alignof_type (input_location,
19910 op1, TREE_CODE (t), std_alignof,
19911 complain & tf_error);
19912 else
19913 r = cxx_sizeof_or_alignof_expr (input_location,
19914 op1, TREE_CODE (t), std_alignof,
19915 complain & tf_error);
19916 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19918 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19920 if (!processing_template_decl && TYPE_P (op1))
19922 r = build_min (SIZEOF_EXPR, size_type_node,
19923 build1 (NOP_EXPR, op1, error_mark_node));
19924 SIZEOF_EXPR_TYPE_P (r) = 1;
19926 else
19927 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19928 TREE_SIDE_EFFECTS (r) = 0;
19929 TREE_READONLY (r) = 1;
19931 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19933 RETURN (r);
19936 case AT_ENCODE_EXPR:
19938 op1 = TREE_OPERAND (t, 0);
19939 ++cp_unevaluated_operand;
19940 ++c_inhibit_evaluation_warnings;
19941 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19942 /*function_p=*/false,
19943 /*integral_constant_expression_p=*/false);
19944 --cp_unevaluated_operand;
19945 --c_inhibit_evaluation_warnings;
19946 RETURN (objc_build_encode_expr (op1));
19949 case NOEXCEPT_EXPR:
19950 op1 = TREE_OPERAND (t, 0);
19951 ++cp_unevaluated_operand;
19952 ++c_inhibit_evaluation_warnings;
19953 ++cp_noexcept_operand;
19954 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19955 /*function_p=*/false,
19956 /*integral_constant_expression_p=*/false);
19957 --cp_unevaluated_operand;
19958 --c_inhibit_evaluation_warnings;
19959 --cp_noexcept_operand;
19960 RETURN (finish_noexcept_expr (op1, complain));
19962 case MODOP_EXPR:
19964 warning_sentinel s(warn_div_by_zero);
19965 tree lhs = RECUR (TREE_OPERAND (t, 0));
19966 tree rhs = RECUR (TREE_OPERAND (t, 2));
19967 tree r = build_x_modify_expr
19968 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19969 complain|decltype_flag);
19970 /* TREE_NO_WARNING must be set if either the expression was
19971 parenthesized or it uses an operator such as >>= rather
19972 than plain assignment. In the former case, it was already
19973 set and must be copied. In the latter case,
19974 build_x_modify_expr sets it and it must not be reset
19975 here. */
19976 if (warning_suppressed_p (t, OPT_Wparentheses))
19977 suppress_warning (r, OPT_Wparentheses);
19979 RETURN (r);
19982 case ARROW_EXPR:
19983 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19984 args, complain, in_decl);
19985 /* Remember that there was a reference to this entity. */
19986 if (DECL_P (op1)
19987 && !mark_used (op1, complain) && !(complain & tf_error))
19988 RETURN (error_mark_node);
19989 RETURN (build_x_arrow (input_location, op1, complain));
19991 case NEW_EXPR:
19993 tree placement = RECUR (TREE_OPERAND (t, 0));
19994 tree init = RECUR (TREE_OPERAND (t, 3));
19995 vec<tree, va_gc> *placement_vec;
19996 vec<tree, va_gc> *init_vec;
19997 tree ret;
19998 location_t loc = EXPR_LOCATION (t);
20000 if (placement == NULL_TREE)
20001 placement_vec = NULL;
20002 else if (placement == error_mark_node)
20003 RETURN (error_mark_node);
20004 else
20006 placement_vec = make_tree_vector ();
20007 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20008 vec_safe_push (placement_vec, TREE_VALUE (placement));
20011 /* If there was an initializer in the original tree, but it
20012 instantiated to an empty list, then we should pass a
20013 non-NULL empty vector to tell build_new that it was an
20014 empty initializer() rather than no initializer. This can
20015 only happen when the initializer is a pack expansion whose
20016 parameter packs are of length zero. */
20017 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20018 init_vec = NULL;
20019 else if (init == error_mark_node)
20020 RETURN (error_mark_node);
20021 else
20023 init_vec = make_tree_vector ();
20024 if (init == void_node)
20025 gcc_assert (init_vec != NULL);
20026 else
20028 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20029 vec_safe_push (init_vec, TREE_VALUE (init));
20033 /* Avoid passing an enclosing decl to valid_array_size_p. */
20034 in_decl = NULL_TREE;
20036 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20037 tree op2 = RECUR (TREE_OPERAND (t, 2));
20038 ret = build_new (loc, &placement_vec, op1, op2,
20039 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20040 complain);
20042 if (placement_vec != NULL)
20043 release_tree_vector (placement_vec);
20044 if (init_vec != NULL)
20045 release_tree_vector (init_vec);
20047 RETURN (ret);
20050 case DELETE_EXPR:
20052 tree op0 = RECUR (TREE_OPERAND (t, 0));
20053 tree op1 = RECUR (TREE_OPERAND (t, 1));
20054 RETURN (delete_sanity (input_location, op0, op1,
20055 DELETE_EXPR_USE_VEC (t),
20056 DELETE_EXPR_USE_GLOBAL (t),
20057 complain));
20060 case COMPOUND_EXPR:
20062 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20063 complain & ~tf_decltype, in_decl,
20064 /*function_p=*/false,
20065 integral_constant_expression_p);
20066 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20067 op0,
20068 RECUR (TREE_OPERAND (t, 1)),
20069 complain|decltype_flag));
20072 case CALL_EXPR:
20074 tree function;
20075 unsigned int nargs, i;
20076 bool qualified_p;
20077 bool koenig_p;
20078 tree ret;
20080 function = CALL_EXPR_FN (t);
20081 /* Internal function with no arguments. */
20082 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20083 RETURN (t);
20085 /* When we parsed the expression, we determined whether or
20086 not Koenig lookup should be performed. */
20087 koenig_p = KOENIG_LOOKUP_P (t);
20088 if (function == NULL_TREE)
20090 koenig_p = false;
20091 qualified_p = false;
20093 else if (TREE_CODE (function) == SCOPE_REF)
20095 qualified_p = true;
20096 function = tsubst_qualified_id (function, args, complain, in_decl,
20097 /*done=*/false,
20098 /*address_p=*/false);
20100 else if (koenig_p && identifier_p (function))
20102 /* Do nothing; calling tsubst_copy_and_build on an identifier
20103 would incorrectly perform unqualified lookup again.
20105 Note that we can also have an IDENTIFIER_NODE if the earlier
20106 unqualified lookup found a member function; in that case
20107 koenig_p will be false and we do want to do the lookup
20108 again to find the instantiated member function.
20110 FIXME but doing that causes c++/15272, so we need to stop
20111 using IDENTIFIER_NODE in that situation. */
20112 qualified_p = false;
20114 else
20116 if (TREE_CODE (function) == COMPONENT_REF)
20118 tree op = TREE_OPERAND (function, 1);
20120 qualified_p = (TREE_CODE (op) == SCOPE_REF
20121 || (BASELINK_P (op)
20122 && BASELINK_QUALIFIED_P (op)));
20124 else
20125 qualified_p = false;
20127 if (TREE_CODE (function) == ADDR_EXPR
20128 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20129 /* Avoid error about taking the address of a constructor. */
20130 function = TREE_OPERAND (function, 0);
20132 tsubst_flags_t subcomplain = complain;
20133 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20134 /* When KOENIG_P, we don't want to mark_used the callee before
20135 augmenting the overload set via ADL, so during this initial
20136 substitution we disable mark_used by setting tf_conv (68942). */
20137 subcomplain |= tf_conv;
20138 function = tsubst_copy_and_build (function, args, subcomplain,
20139 in_decl,
20140 !qualified_p,
20141 integral_constant_expression_p);
20143 if (BASELINK_P (function))
20144 qualified_p = true;
20147 nargs = call_expr_nargs (t);
20148 releasing_vec call_args;
20149 for (i = 0; i < nargs; ++i)
20151 tree arg = CALL_EXPR_ARG (t, i);
20153 if (!PACK_EXPANSION_P (arg))
20154 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20155 else
20157 /* Expand the pack expansion and push each entry onto
20158 CALL_ARGS. */
20159 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20160 if (TREE_CODE (arg) == TREE_VEC)
20162 unsigned int len, j;
20164 len = TREE_VEC_LENGTH (arg);
20165 for (j = 0; j < len; ++j)
20167 tree value = TREE_VEC_ELT (arg, j);
20168 if (value != NULL_TREE)
20169 value = convert_from_reference (value);
20170 vec_safe_push (call_args, value);
20173 else
20175 /* A partial substitution. Add one entry. */
20176 vec_safe_push (call_args, arg);
20181 /* Stripped-down processing for a call in a thunk. Specifically, in
20182 the thunk template for a generic lambda. */
20183 if (call_from_lambda_thunk_p (t))
20185 /* Now that we've expanded any packs, the number of call args
20186 might be different. */
20187 unsigned int cargs = call_args->length ();
20188 tree thisarg = NULL_TREE;
20189 if (TREE_CODE (function) == COMPONENT_REF)
20191 thisarg = TREE_OPERAND (function, 0);
20192 if (TREE_CODE (thisarg) == INDIRECT_REF)
20193 thisarg = TREE_OPERAND (thisarg, 0);
20194 function = TREE_OPERAND (function, 1);
20195 if (TREE_CODE (function) == BASELINK)
20196 function = BASELINK_FUNCTIONS (function);
20198 /* We aren't going to do normal overload resolution, so force the
20199 template-id to resolve. */
20200 function = resolve_nondeduced_context (function, complain);
20201 for (unsigned i = 0; i < cargs; ++i)
20203 /* In a thunk, pass through args directly, without any
20204 conversions. */
20205 tree arg = (*call_args)[i];
20206 while (TREE_CODE (arg) != PARM_DECL)
20207 arg = TREE_OPERAND (arg, 0);
20208 (*call_args)[i] = arg;
20210 if (thisarg)
20212 /* If there are no other args, just push 'this'. */
20213 if (cargs == 0)
20214 vec_safe_push (call_args, thisarg);
20215 else
20217 /* Otherwise, shift the other args over to make room. */
20218 tree last = (*call_args)[cargs - 1];
20219 vec_safe_push (call_args, last);
20220 for (int i = cargs - 1; i > 0; --i)
20221 (*call_args)[i] = (*call_args)[i - 1];
20222 (*call_args)[0] = thisarg;
20225 ret = build_call_a (function, call_args->length (),
20226 call_args->address ());
20227 /* The thunk location is not interesting. */
20228 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20229 CALL_FROM_THUNK_P (ret) = true;
20230 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20231 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20233 RETURN (ret);
20236 /* We do not perform argument-dependent lookup if normal
20237 lookup finds a non-function, in accordance with the
20238 resolution of DR 218. */
20239 if (koenig_p
20240 && ((is_overloaded_fn (function)
20241 /* If lookup found a member function, the Koenig lookup is
20242 not appropriate, even if an unqualified-name was used
20243 to denote the function. */
20244 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20245 || identifier_p (function)
20246 /* C++20 P0846: Lookup found nothing. */
20247 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20248 && identifier_p (TREE_OPERAND (function, 0))))
20249 /* Only do this when substitution turns a dependent call
20250 into a non-dependent call. */
20251 && type_dependent_expression_p_push (t)
20252 && !any_type_dependent_arguments_p (call_args))
20253 function = perform_koenig_lookup (function, call_args, tf_none);
20255 if (function != NULL_TREE
20256 && (identifier_p (function)
20257 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20258 && identifier_p (TREE_OPERAND (function, 0))))
20259 && !any_type_dependent_arguments_p (call_args))
20261 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20262 function = TREE_OPERAND (function, 0);
20263 if (koenig_p && (complain & tf_warning_or_error))
20265 /* For backwards compatibility and good diagnostics, try
20266 the unqualified lookup again if we aren't in SFINAE
20267 context. */
20268 tree unq = (tsubst_copy_and_build
20269 (function, args, complain, in_decl, true,
20270 integral_constant_expression_p));
20271 if (unq == error_mark_node)
20272 RETURN (error_mark_node);
20274 if (unq != function)
20276 /* In a lambda fn, we have to be careful to not
20277 introduce new this captures. Legacy code can't
20278 be using lambdas anyway, so it's ok to be
20279 stricter. */
20280 bool in_lambda = (current_class_type
20281 && LAMBDA_TYPE_P (current_class_type));
20282 char const *const msg
20283 = G_("%qD was not declared in this scope, "
20284 "and no declarations were found by "
20285 "argument-dependent lookup at the point "
20286 "of instantiation");
20288 bool diag = true;
20289 if (in_lambda)
20290 error_at (cp_expr_loc_or_input_loc (t),
20291 msg, function);
20292 else
20293 diag = permerror (cp_expr_loc_or_input_loc (t),
20294 msg, function);
20295 if (diag)
20297 tree fn = unq;
20299 if (INDIRECT_REF_P (fn))
20300 fn = TREE_OPERAND (fn, 0);
20301 if (is_overloaded_fn (fn))
20302 fn = get_first_fn (fn);
20304 if (!DECL_P (fn))
20305 /* Can't say anything more. */;
20306 else if (DECL_CLASS_SCOPE_P (fn))
20308 location_t loc = cp_expr_loc_or_input_loc (t);
20309 inform (loc,
20310 "declarations in dependent base %qT are "
20311 "not found by unqualified lookup",
20312 DECL_CLASS_CONTEXT (fn));
20313 if (current_class_ptr)
20314 inform (loc,
20315 "use %<this->%D%> instead", function);
20316 else
20317 inform (loc,
20318 "use %<%T::%D%> instead",
20319 current_class_name, function);
20321 else
20322 inform (DECL_SOURCE_LOCATION (fn),
20323 "%qD declared here, later in the "
20324 "translation unit", fn);
20325 if (in_lambda)
20326 RETURN (error_mark_node);
20329 function = unq;
20332 if (identifier_p (function))
20334 if (complain & tf_error)
20335 unqualified_name_lookup_error (function);
20336 RETURN (error_mark_node);
20340 /* Remember that there was a reference to this entity. */
20341 if (function != NULL_TREE
20342 && DECL_P (function)
20343 && !mark_used (function, complain) && !(complain & tf_error))
20344 RETURN (error_mark_node);
20346 /* Put back tf_decltype for the actual call. */
20347 complain |= decltype_flag;
20349 if (function == NULL_TREE)
20350 switch (CALL_EXPR_IFN (t))
20352 case IFN_LAUNDER:
20353 gcc_assert (nargs == 1);
20354 if (vec_safe_length (call_args) != 1)
20356 error_at (cp_expr_loc_or_input_loc (t),
20357 "wrong number of arguments to "
20358 "%<__builtin_launder%>");
20359 ret = error_mark_node;
20361 else
20362 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20363 (*call_args)[0], complain);
20364 break;
20366 case IFN_VEC_CONVERT:
20367 gcc_assert (nargs == 1);
20368 if (vec_safe_length (call_args) != 1)
20370 error_at (cp_expr_loc_or_input_loc (t),
20371 "wrong number of arguments to "
20372 "%<__builtin_convertvector%>");
20373 ret = error_mark_node;
20374 break;
20376 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20377 tsubst (TREE_TYPE (t), args,
20378 complain, in_decl),
20379 complain);
20380 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20381 RETURN (ret);
20382 break;
20384 case IFN_SHUFFLEVECTOR:
20386 ret = build_x_shufflevector (input_location, call_args,
20387 complain);
20388 if (ret != error_mark_node)
20389 RETURN (ret);
20390 break;
20393 default:
20394 /* Unsupported internal function with arguments. */
20395 gcc_unreachable ();
20397 else if (TREE_CODE (function) == OFFSET_REF
20398 || TREE_CODE (function) == DOTSTAR_EXPR
20399 || TREE_CODE (function) == MEMBER_REF)
20400 ret = build_offset_ref_call_from_tree (function, &call_args,
20401 complain);
20402 else if (TREE_CODE (function) == COMPONENT_REF)
20404 tree instance = TREE_OPERAND (function, 0);
20405 tree fn = TREE_OPERAND (function, 1);
20407 if (processing_template_decl
20408 && (type_dependent_expression_p (instance)
20409 || (!BASELINK_P (fn)
20410 && TREE_CODE (fn) != FIELD_DECL)
20411 || type_dependent_expression_p (fn)
20412 || any_type_dependent_arguments_p (call_args)))
20413 ret = build_min_nt_call_vec (function, call_args);
20414 else if (!BASELINK_P (fn))
20415 ret = finish_call_expr (function, &call_args,
20416 /*disallow_virtual=*/false,
20417 /*koenig_p=*/false,
20418 complain);
20419 else
20420 ret = (build_new_method_call
20421 (instance, fn,
20422 &call_args, NULL_TREE,
20423 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20424 /*fn_p=*/NULL,
20425 complain));
20427 else if (concept_check_p (function))
20429 /* FUNCTION is a template-id referring to a concept definition. */
20430 tree id = unpack_concept_check (function);
20431 tree tmpl = TREE_OPERAND (id, 0);
20432 tree args = TREE_OPERAND (id, 1);
20434 /* Calls to standard and variable concepts should have been
20435 previously diagnosed. */
20436 gcc_assert (function_concept_p (tmpl));
20438 /* Ensure the result is wrapped as a call expression. */
20439 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20441 else
20442 ret = finish_call_expr (function, &call_args,
20443 /*disallow_virtual=*/qualified_p,
20444 koenig_p,
20445 complain);
20447 if (ret != error_mark_node)
20449 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20450 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20451 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20452 if (op || ord || rev)
20454 function = extract_call_expr (ret);
20455 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20456 CALL_EXPR_ORDERED_ARGS (function) = ord;
20457 CALL_EXPR_REVERSE_ARGS (function) = rev;
20461 RETURN (ret);
20464 case COND_EXPR:
20466 tree cond = RECUR (TREE_OPERAND (t, 0));
20467 cond = mark_rvalue_use (cond);
20468 tree folded_cond = fold_non_dependent_expr (cond, complain);
20469 tree exp1, exp2;
20471 if (TREE_CODE (folded_cond) == INTEGER_CST)
20473 if (integer_zerop (folded_cond))
20475 ++c_inhibit_evaluation_warnings;
20476 exp1 = RECUR (TREE_OPERAND (t, 1));
20477 --c_inhibit_evaluation_warnings;
20478 exp2 = RECUR (TREE_OPERAND (t, 2));
20480 else
20482 exp1 = RECUR (TREE_OPERAND (t, 1));
20483 ++c_inhibit_evaluation_warnings;
20484 exp2 = RECUR (TREE_OPERAND (t, 2));
20485 --c_inhibit_evaluation_warnings;
20487 cond = folded_cond;
20489 else
20491 exp1 = RECUR (TREE_OPERAND (t, 1));
20492 exp2 = RECUR (TREE_OPERAND (t, 2));
20495 warning_sentinel s(warn_duplicated_branches);
20496 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20497 cond, exp1, exp2, complain));
20500 case PSEUDO_DTOR_EXPR:
20502 tree op0 = RECUR (TREE_OPERAND (t, 0));
20503 tree op1 = RECUR (TREE_OPERAND (t, 1));
20504 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20505 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20506 input_location));
20509 case TREE_LIST:
20510 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20512 case COMPONENT_REF:
20514 tree object;
20515 tree object_type;
20516 tree member;
20517 tree r;
20519 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20520 args, complain, in_decl);
20521 /* Remember that there was a reference to this entity. */
20522 if (DECL_P (object)
20523 && !mark_used (object, complain) && !(complain & tf_error))
20524 RETURN (error_mark_node);
20525 object_type = TREE_TYPE (object);
20527 member = TREE_OPERAND (t, 1);
20528 if (BASELINK_P (member))
20529 member = tsubst_baselink (member,
20530 non_reference (TREE_TYPE (object)),
20531 args, complain, in_decl);
20532 else
20533 member = tsubst_copy (member, args, complain, in_decl);
20534 if (member == error_mark_node)
20535 RETURN (error_mark_node);
20537 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
20538 && TREE_CODE (member) == FIELD_DECL)
20540 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
20541 RETURN (r);
20543 else if (TREE_CODE (member) == FIELD_DECL)
20545 r = finish_non_static_data_member (member, object, NULL_TREE);
20546 if (TREE_CODE (r) == COMPONENT_REF)
20547 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20548 RETURN (r);
20550 else if (type_dependent_expression_p (object))
20551 /* We can't do much here. */;
20552 else if (!CLASS_TYPE_P (object_type))
20554 if (scalarish_type_p (object_type))
20556 tree s = NULL_TREE;
20557 tree dtor = member;
20559 if (TREE_CODE (dtor) == SCOPE_REF)
20561 s = TREE_OPERAND (dtor, 0);
20562 dtor = TREE_OPERAND (dtor, 1);
20564 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20566 dtor = TREE_OPERAND (dtor, 0);
20567 if (TYPE_P (dtor))
20568 RETURN (finish_pseudo_destructor_expr
20569 (object, s, dtor, input_location));
20573 else if (TREE_CODE (member) == SCOPE_REF
20574 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20576 /* Lookup the template functions now that we know what the
20577 scope is. */
20578 tree scope = TREE_OPERAND (member, 0);
20579 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20580 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20581 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20582 /*complain=*/false);
20583 if (BASELINK_P (member))
20585 BASELINK_FUNCTIONS (member)
20586 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20587 args);
20588 member = (adjust_result_of_qualified_name_lookup
20589 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20590 object_type));
20592 else
20594 qualified_name_lookup_error (scope, tmpl, member,
20595 input_location);
20596 RETURN (error_mark_node);
20599 else if (TREE_CODE (member) == SCOPE_REF
20600 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20601 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20603 if (complain & tf_error)
20605 if (TYPE_P (TREE_OPERAND (member, 0)))
20606 error ("%qT is not a class or namespace",
20607 TREE_OPERAND (member, 0));
20608 else
20609 error ("%qD is not a class or namespace",
20610 TREE_OPERAND (member, 0));
20612 RETURN (error_mark_node);
20615 r = finish_class_member_access_expr (object, member,
20616 /*template_p=*/false,
20617 complain);
20618 if (TREE_CODE (r) == COMPONENT_REF)
20619 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20620 RETURN (r);
20623 case THROW_EXPR:
20624 RETURN (build_throw
20625 (input_location, RECUR (TREE_OPERAND (t, 0))));
20627 case CONSTRUCTOR:
20629 vec<constructor_elt, va_gc> *n;
20630 constructor_elt *ce;
20631 unsigned HOST_WIDE_INT idx;
20632 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20633 bool process_index_p;
20634 int newlen;
20635 bool need_copy_p = false;
20636 tree r;
20638 if (type == error_mark_node)
20639 RETURN (error_mark_node);
20641 /* We do not want to process the index of aggregate
20642 initializers as they are identifier nodes which will be
20643 looked up by digest_init. */
20644 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20646 if (null_member_pointer_value_p (t))
20648 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20649 RETURN (t);
20652 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20653 newlen = vec_safe_length (n);
20654 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20656 if (ce->index && process_index_p
20657 /* An identifier index is looked up in the type
20658 being initialized, not the current scope. */
20659 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20660 ce->index = RECUR (ce->index);
20662 if (PACK_EXPANSION_P (ce->value))
20664 /* Substitute into the pack expansion. */
20665 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20666 in_decl);
20668 if (ce->value == error_mark_node
20669 || PACK_EXPANSION_P (ce->value))
20671 else if (TREE_VEC_LENGTH (ce->value) == 1)
20672 /* Just move the argument into place. */
20673 ce->value = TREE_VEC_ELT (ce->value, 0);
20674 else
20676 /* Update the length of the final CONSTRUCTOR
20677 arguments vector, and note that we will need to
20678 copy.*/
20679 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20680 need_copy_p = true;
20683 else
20684 ce->value = RECUR (ce->value);
20687 if (need_copy_p)
20689 vec<constructor_elt, va_gc> *old_n = n;
20691 vec_alloc (n, newlen);
20692 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20694 if (TREE_CODE (ce->value) == TREE_VEC)
20696 int i, len = TREE_VEC_LENGTH (ce->value);
20697 for (i = 0; i < len; ++i)
20698 CONSTRUCTOR_APPEND_ELT (n, 0,
20699 TREE_VEC_ELT (ce->value, i));
20701 else
20702 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20706 r = build_constructor (init_list_type_node, n);
20707 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20708 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20709 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20711 if (TREE_HAS_CONSTRUCTOR (t))
20713 fcl_t cl = fcl_functional;
20714 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20715 cl = fcl_c99;
20716 RETURN (finish_compound_literal (type, r, complain, cl));
20719 TREE_TYPE (r) = type;
20720 RETURN (r);
20723 case TYPEID_EXPR:
20725 tree operand_0 = TREE_OPERAND (t, 0);
20726 if (TYPE_P (operand_0))
20728 operand_0 = tsubst (operand_0, args, complain, in_decl);
20729 RETURN (get_typeid (operand_0, complain));
20731 else
20733 operand_0 = RECUR (operand_0);
20734 RETURN (build_typeid (operand_0, complain));
20738 case VAR_DECL:
20739 if (!args)
20740 RETURN (t);
20741 /* Fall through */
20743 case PARM_DECL:
20745 tree r = tsubst_copy (t, args, complain, in_decl);
20746 /* ??? We're doing a subset of finish_id_expression here. */
20747 if (tree wrap = maybe_get_tls_wrapper_call (r))
20748 /* Replace an evaluated use of the thread_local variable with
20749 a call to its wrapper. */
20750 r = wrap;
20751 else if (outer_automatic_var_p (r))
20752 r = process_outer_var_ref (r, complain);
20754 if (!TYPE_REF_P (TREE_TYPE (t)))
20755 /* If the original type was a reference, we'll be wrapped in
20756 the appropriate INDIRECT_REF. */
20757 r = convert_from_reference (r);
20758 RETURN (r);
20761 case VA_ARG_EXPR:
20763 tree op0 = RECUR (TREE_OPERAND (t, 0));
20764 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20765 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20768 case OFFSETOF_EXPR:
20770 tree object_ptr
20771 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20772 in_decl, /*function_p=*/false,
20773 /*integral_constant_expression_p=*/false);
20774 RETURN (finish_offsetof (object_ptr,
20775 RECUR (TREE_OPERAND (t, 0)),
20776 EXPR_LOCATION (t)));
20779 case ADDRESSOF_EXPR:
20780 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20781 RECUR (TREE_OPERAND (t, 0)), complain));
20783 case TRAIT_EXPR:
20785 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20786 complain, in_decl);
20787 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20788 complain, in_decl);
20789 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20790 TRAIT_EXPR_KIND (t), type1, type2));
20793 case STMT_EXPR:
20795 tree old_stmt_expr = cur_stmt_expr;
20796 tree stmt_expr = begin_stmt_expr ();
20798 cur_stmt_expr = stmt_expr;
20799 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20800 integral_constant_expression_p);
20801 stmt_expr = finish_stmt_expr (stmt_expr, false);
20802 cur_stmt_expr = old_stmt_expr;
20804 /* If the resulting list of expression statement is empty,
20805 fold it further into void_node. */
20806 if (empty_expr_stmt_p (stmt_expr))
20807 stmt_expr = void_node;
20809 RETURN (stmt_expr);
20812 case LAMBDA_EXPR:
20814 if (complain & tf_partial)
20816 /* We don't have a full set of template arguments yet; don't touch
20817 the lambda at all. */
20818 gcc_assert (processing_template_decl);
20819 return t;
20821 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20823 RETURN (build_lambda_object (r));
20826 case TARGET_EXPR:
20827 /* We can get here for a constant initializer of non-dependent type.
20828 FIXME stop folding in cp_parser_initializer_clause. */
20830 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20831 complain);
20832 RETURN (r);
20835 case TRANSACTION_EXPR:
20836 RETURN (tsubst_expr(t, args, complain, in_decl,
20837 integral_constant_expression_p));
20839 case PAREN_EXPR:
20840 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20842 case VEC_PERM_EXPR:
20844 tree op0 = RECUR (TREE_OPERAND (t, 0));
20845 tree op1 = RECUR (TREE_OPERAND (t, 1));
20846 tree op2 = RECUR (TREE_OPERAND (t, 2));
20847 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20848 complain));
20851 case REQUIRES_EXPR:
20853 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20854 RETURN (r);
20857 case RANGE_EXPR:
20858 /* No need to substitute further, a RANGE_EXPR will always be built
20859 with constant operands. */
20860 RETURN (t);
20862 case NON_LVALUE_EXPR:
20863 case VIEW_CONVERT_EXPR:
20864 if (location_wrapper_p (t))
20865 /* We need to do this here as well as in tsubst_copy so we get the
20866 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20867 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20868 EXPR_LOCATION (t)));
20869 /* fallthrough. */
20871 default:
20872 /* Handle Objective-C++ constructs, if appropriate. */
20874 tree subst
20875 = objcp_tsubst_copy_and_build (t, args, complain,
20876 in_decl, /*function_p=*/false);
20877 if (subst)
20878 RETURN (subst);
20880 RETURN (tsubst_copy (t, args, complain, in_decl));
20883 #undef RECUR
20884 #undef RETURN
20885 out:
20886 input_location = save_loc;
20887 return retval;
20890 /* Verify that the instantiated ARGS are valid. For type arguments,
20891 make sure that the type's linkage is ok. For non-type arguments,
20892 make sure they are constants if they are integral or enumerations.
20893 Emit an error under control of COMPLAIN, and return TRUE on error. */
20895 static bool
20896 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20898 if (dependent_template_arg_p (t))
20899 return false;
20900 if (ARGUMENT_PACK_P (t))
20902 tree vec = ARGUMENT_PACK_ARGS (t);
20903 int len = TREE_VEC_LENGTH (vec);
20904 bool result = false;
20905 int i;
20907 for (i = 0; i < len; ++i)
20908 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20909 result = true;
20910 return result;
20912 else if (TYPE_P (t))
20914 /* [basic.link]: A name with no linkage (notably, the name
20915 of a class or enumeration declared in a local scope)
20916 shall not be used to declare an entity with linkage.
20917 This implies that names with no linkage cannot be used as
20918 template arguments
20920 DR 757 relaxes this restriction for C++0x. */
20921 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20922 : no_linkage_check (t, /*relaxed_p=*/false));
20924 if (nt)
20926 /* DR 488 makes use of a type with no linkage cause
20927 type deduction to fail. */
20928 if (complain & tf_error)
20930 if (TYPE_UNNAMED_P (nt))
20931 error ("%qT is/uses unnamed type", t);
20932 else
20933 error ("template argument for %qD uses local type %qT",
20934 tmpl, t);
20936 return true;
20938 /* In order to avoid all sorts of complications, we do not
20939 allow variably-modified types as template arguments. */
20940 else if (variably_modified_type_p (t, NULL_TREE))
20942 if (complain & tf_error)
20943 error ("%qT is a variably modified type", t);
20944 return true;
20947 /* Class template and alias template arguments should be OK. */
20948 else if (DECL_TYPE_TEMPLATE_P (t))
20950 /* A non-type argument of integral or enumerated type must be a
20951 constant. */
20952 else if (TREE_TYPE (t)
20953 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20954 && !REFERENCE_REF_P (t)
20955 && !TREE_CONSTANT (t))
20957 if (complain & tf_error)
20958 error ("integral expression %qE is not constant", t);
20959 return true;
20961 return false;
20964 static bool
20965 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20967 int ix, len = DECL_NTPARMS (tmpl);
20968 bool result = false;
20970 for (ix = 0; ix != len; ix++)
20972 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20973 result = true;
20975 if (result && (complain & tf_error))
20976 error (" trying to instantiate %qD", tmpl);
20977 return result;
20980 /* We're out of SFINAE context now, so generate diagnostics for the access
20981 errors we saw earlier when instantiating D from TMPL and ARGS. */
20983 static void
20984 recheck_decl_substitution (tree d, tree tmpl, tree args)
20986 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20987 tree type = TREE_TYPE (pattern);
20988 location_t loc = input_location;
20990 push_access_scope (d);
20991 push_deferring_access_checks (dk_no_deferred);
20992 input_location = DECL_SOURCE_LOCATION (pattern);
20993 tsubst (type, args, tf_warning_or_error, d);
20994 input_location = loc;
20995 pop_deferring_access_checks ();
20996 pop_access_scope (d);
20999 /* Instantiate the indicated variable, function, or alias template TMPL with
21000 the template arguments in TARG_PTR. */
21002 static tree
21003 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
21005 tree targ_ptr = orig_args;
21006 tree fndecl;
21007 tree gen_tmpl;
21008 tree spec;
21009 bool access_ok = true;
21011 if (tmpl == error_mark_node)
21012 return error_mark_node;
21014 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
21016 if (modules_p ())
21017 lazy_load_pendings (tmpl);
21019 /* If this function is a clone, handle it specially. */
21020 if (DECL_CLONED_FUNCTION_P (tmpl))
21022 tree spec;
21023 tree clone;
21025 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21026 DECL_CLONED_FUNCTION. */
21027 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
21028 targ_ptr, complain);
21029 if (spec == error_mark_node)
21030 return error_mark_node;
21032 /* Look for the clone. */
21033 FOR_EACH_CLONE (clone, spec)
21034 if (DECL_NAME (clone) == DECL_NAME (tmpl))
21035 return clone;
21036 /* We should always have found the clone by now. */
21037 gcc_unreachable ();
21038 return NULL_TREE;
21041 if (targ_ptr == error_mark_node)
21042 return error_mark_node;
21044 /* Check to see if we already have this specialization. */
21045 gen_tmpl = most_general_template (tmpl);
21046 if (TMPL_ARGS_DEPTH (targ_ptr)
21047 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
21048 /* targ_ptr only has the innermost template args, so add the outer ones
21049 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21050 the case of a non-dependent call within a template definition). */
21051 targ_ptr = (add_outermost_template_args
21052 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
21053 targ_ptr));
21055 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21056 but it doesn't seem to be on the hot path. */
21057 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
21059 gcc_checking_assert (tmpl == gen_tmpl
21060 || ((fndecl
21061 = retrieve_specialization (tmpl, orig_args, 0))
21062 == spec)
21063 || fndecl == NULL_TREE);
21065 if (spec != NULL_TREE)
21067 if (FNDECL_HAS_ACCESS_ERRORS (spec))
21069 if (complain & tf_error)
21070 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
21071 return error_mark_node;
21073 return spec;
21076 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
21077 complain))
21078 return error_mark_node;
21080 /* We are building a FUNCTION_DECL, during which the access of its
21081 parameters and return types have to be checked. However this
21082 FUNCTION_DECL which is the desired context for access checking
21083 is not built yet. We solve this chicken-and-egg problem by
21084 deferring all checks until we have the FUNCTION_DECL. */
21085 push_deferring_access_checks (dk_deferred);
21087 /* Instantiation of the function happens in the context of the function
21088 template, not the context of the overload resolution we're doing. */
21089 push_to_top_level ();
21090 /* If there are dependent arguments, e.g. because we're doing partial
21091 ordering, make sure processing_template_decl stays set. */
21092 if (uses_template_parms (targ_ptr))
21093 ++processing_template_decl;
21094 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21096 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
21097 complain, gen_tmpl, true);
21098 push_nested_class (ctx);
21101 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
21103 fndecl = NULL_TREE;
21104 if (VAR_P (pattern))
21106 /* We need to determine if we're using a partial or explicit
21107 specialization now, because the type of the variable could be
21108 different. */
21109 tree tid = lookup_template_variable (tmpl, targ_ptr);
21110 tree elt = most_specialized_partial_spec (tid, complain);
21111 if (elt == error_mark_node)
21112 pattern = error_mark_node;
21113 else if (elt)
21115 tree partial_tmpl = TREE_VALUE (elt);
21116 tree partial_args = TREE_PURPOSE (elt);
21117 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21118 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21122 /* Substitute template parameters to obtain the specialization. */
21123 if (fndecl == NULL_TREE)
21124 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21125 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21126 pop_nested_class ();
21127 pop_from_top_level ();
21129 if (fndecl == error_mark_node)
21131 pop_deferring_access_checks ();
21132 return error_mark_node;
21135 /* The DECL_TI_TEMPLATE should always be the immediate parent
21136 template, not the most general template. */
21137 DECL_TI_TEMPLATE (fndecl) = tmpl;
21138 DECL_TI_ARGS (fndecl) = targ_ptr;
21140 set_instantiating_module (fndecl);
21142 /* Now we know the specialization, compute access previously
21143 deferred. Do no access control for inheriting constructors,
21144 as we already checked access for the inherited constructor. */
21145 if (!(flag_new_inheriting_ctors
21146 && DECL_INHERITED_CTOR (fndecl)))
21148 push_access_scope (fndecl);
21149 if (!perform_deferred_access_checks (complain))
21150 access_ok = false;
21151 pop_access_scope (fndecl);
21153 pop_deferring_access_checks ();
21155 /* If we've just instantiated the main entry point for a function,
21156 instantiate all the alternate entry points as well. We do this
21157 by cloning the instantiation of the main entry point, not by
21158 instantiating the template clones. */
21159 if (tree chain = DECL_CHAIN (gen_tmpl))
21160 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21161 clone_cdtor (fndecl, /*update_methods=*/false);
21163 if (!access_ok)
21165 if (!(complain & tf_error))
21167 /* Remember to reinstantiate when we're out of SFINAE so the user
21168 can see the errors. */
21169 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21171 return error_mark_node;
21173 return fndecl;
21176 /* Wrapper for instantiate_template_1. */
21178 tree
21179 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21181 tree ret;
21182 timevar_push (TV_TEMPLATE_INST);
21183 ret = instantiate_template_1 (tmpl, orig_args, complain);
21184 timevar_pop (TV_TEMPLATE_INST);
21185 return ret;
21188 /* Instantiate the alias template TMPL with ARGS. Also push a template
21189 instantiation level, which instantiate_template doesn't do because
21190 functions and variables have sufficient context established by the
21191 callers. */
21193 static tree
21194 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21196 if (tmpl == error_mark_node || args == error_mark_node)
21197 return error_mark_node;
21199 args =
21200 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21201 args, tmpl, complain,
21202 /*require_all_args=*/true,
21203 /*use_default_args=*/true);
21205 /* FIXME check for satisfaction in check_instantiated_args. */
21206 if (flag_concepts
21207 && !any_dependent_template_arguments_p (args)
21208 && !constraints_satisfied_p (tmpl, args))
21210 if (complain & tf_error)
21212 auto_diagnostic_group d;
21213 error ("template constraint failure for %qD", tmpl);
21214 diagnose_constraints (input_location, tmpl, args);
21216 return error_mark_node;
21219 if (!push_tinst_level (tmpl, args))
21220 return error_mark_node;
21221 tree r = instantiate_template (tmpl, args, complain);
21222 pop_tinst_level ();
21224 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
21226 /* An alias template specialization can be dependent
21227 even if its underlying type is not. */
21228 TYPE_DEPENDENT_P (d) = true;
21229 TYPE_DEPENDENT_P_VALID (d) = true;
21230 /* Sometimes a dependent alias spec is equivalent to its expansion,
21231 sometimes not. So always use structural_comptypes. */
21232 SET_TYPE_STRUCTURAL_EQUALITY (d);
21235 return r;
21238 /* PARM is a template parameter pack for FN. Returns true iff
21239 PARM is used in a deducible way in the argument list of FN. */
21241 static bool
21242 pack_deducible_p (tree parm, tree fn)
21244 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21245 for (; t; t = TREE_CHAIN (t))
21247 tree type = TREE_VALUE (t);
21248 tree packs;
21249 if (!PACK_EXPANSION_P (type))
21250 continue;
21251 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21252 packs; packs = TREE_CHAIN (packs))
21253 if (template_args_equal (TREE_VALUE (packs), parm))
21255 /* The template parameter pack is used in a function parameter
21256 pack. If this is the end of the parameter list, the
21257 template parameter pack is deducible. */
21258 if (TREE_CHAIN (t) == void_list_node)
21259 return true;
21260 else
21261 /* Otherwise, not. Well, it could be deduced from
21262 a non-pack parameter, but doing so would end up with
21263 a deduction mismatch, so don't bother. */
21264 return false;
21267 /* The template parameter pack isn't used in any function parameter
21268 packs, but it might be used deeper, e.g. tuple<Args...>. */
21269 return true;
21272 /* Subroutine of fn_type_unification: check non-dependent parms for
21273 convertibility. */
21275 static int
21276 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21277 tree fn, unification_kind_t strict, int flags,
21278 struct conversion **convs, bool explain_p)
21280 /* Non-constructor methods need to leave a conversion for 'this', which
21281 isn't included in nargs here. */
21282 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21283 && !DECL_CONSTRUCTOR_P (fn));
21285 for (unsigned ia = 0;
21286 parms && parms != void_list_node && ia < nargs; )
21288 tree parm = TREE_VALUE (parms);
21290 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21291 && (!TREE_CHAIN (parms)
21292 || TREE_CHAIN (parms) == void_list_node))
21293 /* For a function parameter pack that occurs at the end of the
21294 parameter-declaration-list, the type A of each remaining
21295 argument of the call is compared with the type P of the
21296 declarator-id of the function parameter pack. */
21297 break;
21299 parms = TREE_CHAIN (parms);
21301 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21302 /* For a function parameter pack that does not occur at the
21303 end of the parameter-declaration-list, the type of the
21304 parameter pack is a non-deduced context. */
21305 continue;
21307 if (!uses_template_parms (parm))
21309 tree arg = args[ia];
21310 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21311 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21313 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21314 conv_p, explain_p))
21315 return 1;
21318 ++ia;
21321 return 0;
21324 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21325 NARGS elements of the arguments that are being used when calling
21326 it. TARGS is a vector into which the deduced template arguments
21327 are placed.
21329 Returns either a FUNCTION_DECL for the matching specialization of FN or
21330 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21331 true, diagnostics will be printed to explain why it failed.
21333 If FN is a conversion operator, or we are trying to produce a specific
21334 specialization, RETURN_TYPE is the return type desired.
21336 The EXPLICIT_TARGS are explicit template arguments provided via a
21337 template-id.
21339 The parameter STRICT is one of:
21341 DEDUCE_CALL:
21342 We are deducing arguments for a function call, as in
21343 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21344 deducing arguments for a call to the result of a conversion
21345 function template, as in [over.call.object].
21347 DEDUCE_CONV:
21348 We are deducing arguments for a conversion function, as in
21349 [temp.deduct.conv].
21351 DEDUCE_EXACT:
21352 We are deducing arguments when doing an explicit instantiation
21353 as in [temp.explicit], when determining an explicit specialization
21354 as in [temp.expl.spec], or when taking the address of a function
21355 template, as in [temp.deduct.funcaddr]. */
21357 tree
21358 fn_type_unification (tree fn,
21359 tree explicit_targs,
21360 tree targs,
21361 const tree *args,
21362 unsigned int nargs,
21363 tree return_type,
21364 unification_kind_t strict,
21365 int flags,
21366 struct conversion **convs,
21367 bool explain_p,
21368 bool decltype_p)
21370 tree parms;
21371 tree fntype;
21372 tree decl = NULL_TREE;
21373 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21374 bool ok;
21375 static int deduction_depth;
21376 /* type_unification_real will pass back any access checks from default
21377 template argument substitution. */
21378 vec<deferred_access_check, va_gc> *checks = NULL;
21379 /* We don't have all the template args yet. */
21380 bool incomplete = true;
21382 tree orig_fn = fn;
21383 if (flag_new_inheriting_ctors)
21384 fn = strip_inheriting_ctors (fn);
21386 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21387 tree r = error_mark_node;
21389 tree full_targs = targs;
21390 if (TMPL_ARGS_DEPTH (targs)
21391 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21392 full_targs = (add_outermost_template_args
21393 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21394 targs));
21396 if (decltype_p)
21397 complain |= tf_decltype;
21399 /* In C++0x, it's possible to have a function template whose type depends
21400 on itself recursively. This is most obvious with decltype, but can also
21401 occur with enumeration scope (c++/48969). So we need to catch infinite
21402 recursion and reject the substitution at deduction time; this function
21403 will return error_mark_node for any repeated substitution.
21405 This also catches excessive recursion such as when f<N> depends on
21406 f<N-1> across all integers, and returns error_mark_node for all the
21407 substitutions back up to the initial one.
21409 This is, of course, not reentrant. */
21410 if (excessive_deduction_depth)
21411 return error_mark_node;
21412 ++deduction_depth;
21414 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21416 fntype = TREE_TYPE (fn);
21417 if (explicit_targs)
21419 /* [temp.deduct]
21421 The specified template arguments must match the template
21422 parameters in kind (i.e., type, nontype, template), and there
21423 must not be more arguments than there are parameters;
21424 otherwise type deduction fails.
21426 Nontype arguments must match the types of the corresponding
21427 nontype template parameters, or must be convertible to the
21428 types of the corresponding nontype parameters as specified in
21429 _temp.arg.nontype_, otherwise type deduction fails.
21431 All references in the function type of the function template
21432 to the corresponding template parameters are replaced by the
21433 specified template argument values. If a substitution in a
21434 template parameter or in the function type of the function
21435 template results in an invalid type, type deduction fails. */
21436 int i, len = TREE_VEC_LENGTH (tparms);
21437 location_t loc = input_location;
21438 incomplete = false;
21440 if (explicit_targs == error_mark_node)
21441 goto fail;
21443 if (TMPL_ARGS_DEPTH (explicit_targs)
21444 < TMPL_ARGS_DEPTH (full_targs))
21445 explicit_targs = add_outermost_template_args (full_targs,
21446 explicit_targs);
21448 /* Adjust any explicit template arguments before entering the
21449 substitution context. */
21450 explicit_targs
21451 = (coerce_template_parms (tparms, explicit_targs, fn,
21452 complain|tf_partial,
21453 /*require_all_args=*/false,
21454 /*use_default_args=*/false));
21455 if (explicit_targs == error_mark_node)
21456 goto fail;
21458 /* Substitute the explicit args into the function type. This is
21459 necessary so that, for instance, explicitly declared function
21460 arguments can match null pointed constants. If we were given
21461 an incomplete set of explicit args, we must not do semantic
21462 processing during substitution as we could create partial
21463 instantiations. */
21464 for (i = 0; i < len; i++)
21466 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21467 bool parameter_pack = false;
21468 tree targ = TREE_VEC_ELT (explicit_targs, i);
21470 /* Dig out the actual parm. */
21471 if (TREE_CODE (parm) == TYPE_DECL
21472 || TREE_CODE (parm) == TEMPLATE_DECL)
21474 parm = TREE_TYPE (parm);
21475 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21477 else if (TREE_CODE (parm) == PARM_DECL)
21479 parm = DECL_INITIAL (parm);
21480 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21483 if (targ == NULL_TREE)
21484 /* No explicit argument for this template parameter. */
21485 incomplete = true;
21486 else if (parameter_pack && pack_deducible_p (parm, fn))
21488 /* Mark the argument pack as "incomplete". We could
21489 still deduce more arguments during unification.
21490 We remove this mark in type_unification_real. */
21491 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21492 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21493 = ARGUMENT_PACK_ARGS (targ);
21495 /* We have some incomplete argument packs. */
21496 incomplete = true;
21500 if (incomplete)
21502 if (!push_tinst_level (fn, explicit_targs))
21504 excessive_deduction_depth = true;
21505 goto fail;
21507 ++processing_template_decl;
21508 input_location = DECL_SOURCE_LOCATION (fn);
21509 /* Ignore any access checks; we'll see them again in
21510 instantiate_template and they might have the wrong
21511 access path at this point. */
21512 push_deferring_access_checks (dk_deferred);
21513 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21514 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21515 pop_deferring_access_checks ();
21516 input_location = loc;
21517 --processing_template_decl;
21518 pop_tinst_level ();
21520 if (fntype == error_mark_node)
21521 goto fail;
21524 /* Place the explicitly specified arguments in TARGS. */
21525 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21526 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21527 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21528 if (!incomplete && CHECKING_P
21529 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21530 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21531 (targs, NUM_TMPL_ARGS (explicit_targs));
21534 if (return_type && strict != DEDUCE_CALL)
21536 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21537 new_args[0] = return_type;
21538 memcpy (new_args + 1, args, nargs * sizeof (tree));
21539 args = new_args;
21540 ++nargs;
21543 if (!incomplete)
21544 goto deduced;
21546 /* Never do unification on the 'this' parameter. */
21547 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21549 if (return_type && strict == DEDUCE_CALL)
21551 /* We're deducing for a call to the result of a template conversion
21552 function. The parms we really want are in return_type. */
21553 if (INDIRECT_TYPE_P (return_type))
21554 return_type = TREE_TYPE (return_type);
21555 parms = TYPE_ARG_TYPES (return_type);
21557 else if (return_type)
21559 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21562 /* We allow incomplete unification without an error message here
21563 because the standard doesn't seem to explicitly prohibit it. Our
21564 callers must be ready to deal with unification failures in any
21565 event. */
21567 /* If we aren't explaining yet, push tinst context so we can see where
21568 any errors (e.g. from class instantiations triggered by instantiation
21569 of default template arguments) come from. If we are explaining, this
21570 context is redundant. */
21571 if (!explain_p && !push_tinst_level (fn, targs))
21573 excessive_deduction_depth = true;
21574 goto fail;
21577 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21578 full_targs, parms, args, nargs, /*subr=*/0,
21579 strict, &checks, explain_p);
21580 if (!explain_p)
21581 pop_tinst_level ();
21582 if (!ok)
21583 goto fail;
21585 /* Now that we have bindings for all of the template arguments,
21586 ensure that the arguments deduced for the template template
21587 parameters have compatible template parameter lists. We cannot
21588 check this property before we have deduced all template
21589 arguments, because the template parameter types of a template
21590 template parameter might depend on prior template parameters
21591 deduced after the template template parameter. The following
21592 ill-formed example illustrates this issue:
21594 template<typename T, template<T> class C> void f(C<5>, T);
21596 template<int N> struct X {};
21598 void g() {
21599 f(X<5>(), 5l); // error: template argument deduction fails
21602 The template parameter list of 'C' depends on the template type
21603 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21604 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21605 time that we deduce 'C'. */
21606 if (!template_template_parm_bindings_ok_p
21607 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21609 unify_inconsistent_template_template_parameters (explain_p);
21610 goto fail;
21613 deduced:
21615 /* CWG2369: Check satisfaction before non-deducible conversions. */
21616 if (!constraints_satisfied_p (fn, targs))
21618 if (explain_p)
21619 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21620 goto fail;
21623 /* DR 1391: All parameters have args, now check non-dependent parms for
21624 convertibility. We don't do this if all args were explicitly specified,
21625 as the standard says that we substitute explicit args immediately. */
21626 if (incomplete
21627 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21628 convs, explain_p))
21629 goto fail;
21631 /* All is well so far. Now, check:
21633 [temp.deduct]
21635 When all template arguments have been deduced, all uses of
21636 template parameters in nondeduced contexts are replaced with
21637 the corresponding deduced argument values. If the
21638 substitution results in an invalid type, as described above,
21639 type deduction fails. */
21640 if (!push_tinst_level (fn, targs))
21642 excessive_deduction_depth = true;
21643 goto fail;
21646 /* Also collect access checks from the instantiation. */
21647 reopen_deferring_access_checks (checks);
21649 decl = instantiate_template (fn, targs, complain);
21651 checks = get_deferred_access_checks ();
21652 pop_deferring_access_checks ();
21654 pop_tinst_level ();
21656 if (decl == error_mark_node)
21657 goto fail;
21659 /* Now perform any access checks encountered during substitution. */
21660 push_access_scope (decl);
21661 ok = perform_access_checks (checks, complain);
21662 pop_access_scope (decl);
21663 if (!ok)
21664 goto fail;
21666 /* If we're looking for an exact match, check that what we got
21667 is indeed an exact match. It might not be if some template
21668 parameters are used in non-deduced contexts. But don't check
21669 for an exact match if we have dependent template arguments;
21670 in that case we're doing partial ordering, and we already know
21671 that we have two candidates that will provide the actual type. */
21672 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21674 tree substed = TREE_TYPE (decl);
21675 unsigned int i;
21677 tree sarg
21678 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21679 if (return_type)
21680 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21681 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21682 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21684 unify_type_mismatch (explain_p, args[i],
21685 TREE_VALUE (sarg));
21686 goto fail;
21690 /* After doing deduction with the inherited constructor, actually return an
21691 instantiation of the inheriting constructor. */
21692 if (orig_fn != fn)
21693 decl = instantiate_template (orig_fn, targs, complain);
21695 r = decl;
21697 fail:
21698 --deduction_depth;
21699 if (excessive_deduction_depth)
21701 if (deduction_depth == 0)
21702 /* Reset once we're all the way out. */
21703 excessive_deduction_depth = false;
21706 return r;
21709 /* Returns true iff PARM is a forwarding reference in the context of
21710 template argument deduction for TMPL. */
21712 static bool
21713 forwarding_reference_p (tree parm, tree tmpl)
21715 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
21716 cv-unqualified template parameter ..." */
21717 if (TYPE_REF_P (parm)
21718 && TYPE_REF_IS_RVALUE (parm)
21719 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
21720 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
21722 parm = TREE_TYPE (parm);
21723 /* [temp.deduct.call], "... that does not represent a template parameter
21724 of a class template (during class template argument deduction)." */
21725 if (tmpl
21726 && deduction_guide_p (tmpl)
21727 && DECL_ARTIFICIAL (tmpl))
21729 /* Since the template parameters of a synthesized guide consist of
21730 the template parameters of the class template followed by those of
21731 the constructor (if any), we can tell if PARM represents a template
21732 parameter of the class template by comparing its index with the
21733 arity of the class template. */
21734 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
21735 if (TEMPLATE_TYPE_IDX (parm)
21736 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
21737 return false;
21739 return true;
21741 return false;
21744 /* Adjust types before performing type deduction, as described in
21745 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21746 sections are symmetric. PARM is the type of a function parameter
21747 or the return type of the conversion function. ARG is the type of
21748 the argument passed to the call, or the type of the value
21749 initialized with the result of the conversion function.
21750 ARG_EXPR is the original argument expression, which may be null. */
21752 static int
21753 maybe_adjust_types_for_deduction (tree tparms,
21754 unification_kind_t strict,
21755 tree* parm,
21756 tree* arg,
21757 tree arg_expr)
21759 int result = 0;
21761 switch (strict)
21763 case DEDUCE_CALL:
21764 break;
21766 case DEDUCE_CONV:
21767 /* Swap PARM and ARG throughout the remainder of this
21768 function; the handling is precisely symmetric since PARM
21769 will initialize ARG rather than vice versa. */
21770 std::swap (parm, arg);
21771 break;
21773 case DEDUCE_EXACT:
21774 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21775 too, but here handle it by stripping the reference from PARM
21776 rather than by adding it to ARG. */
21777 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
21778 && TYPE_REF_P (*arg)
21779 && !TYPE_REF_IS_RVALUE (*arg))
21780 *parm = TREE_TYPE (*parm);
21781 /* Nothing else to do in this case. */
21782 return 0;
21784 default:
21785 gcc_unreachable ();
21788 if (!TYPE_REF_P (*parm))
21790 /* [temp.deduct.call]
21792 If P is not a reference type:
21794 --If A is an array type, the pointer type produced by the
21795 array-to-pointer standard conversion (_conv.array_) is
21796 used in place of A for type deduction; otherwise,
21798 --If A is a function type, the pointer type produced by
21799 the function-to-pointer standard conversion
21800 (_conv.func_) is used in place of A for type deduction;
21801 otherwise,
21803 --If A is a cv-qualified type, the top level
21804 cv-qualifiers of A's type are ignored for type
21805 deduction. */
21806 if (TREE_CODE (*arg) == ARRAY_TYPE)
21807 *arg = build_pointer_type (TREE_TYPE (*arg));
21808 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21809 *arg = build_pointer_type (*arg);
21810 else
21811 *arg = TYPE_MAIN_VARIANT (*arg);
21814 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
21815 an lvalue, the type 'lvalue reference to A' is used in place of A for
21816 type deduction." */
21817 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
21818 && (arg_expr ? lvalue_p (arg_expr)
21819 /* try_one_overload doesn't provide an arg_expr, but
21820 functions are always lvalues. */
21821 : TREE_CODE (*arg) == FUNCTION_TYPE))
21822 *arg = build_reference_type (*arg);
21824 /* [temp.deduct.call]
21826 If P is a cv-qualified type, the top level cv-qualifiers
21827 of P's type are ignored for type deduction. If P is a
21828 reference type, the type referred to by P is used for
21829 type deduction. */
21830 *parm = TYPE_MAIN_VARIANT (*parm);
21831 if (TYPE_REF_P (*parm))
21833 *parm = TREE_TYPE (*parm);
21834 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21837 /* DR 322. For conversion deduction, remove a reference type on parm
21838 too (which has been swapped into ARG). */
21839 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21840 *arg = TREE_TYPE (*arg);
21842 return result;
21845 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21846 template which doesn't contain any deducible template parameters; check if
21847 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21848 unify_one_argument. */
21850 static int
21851 check_non_deducible_conversion (tree parm, tree arg, int strict,
21852 int flags, struct conversion **conv_p,
21853 bool explain_p)
21855 tree type;
21857 if (!TYPE_P (arg))
21858 type = TREE_TYPE (arg);
21859 else
21860 type = arg;
21862 if (same_type_p (parm, type))
21863 return unify_success (explain_p);
21865 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21866 if (strict == DEDUCE_CONV)
21868 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21869 return unify_success (explain_p);
21871 else if (strict != DEDUCE_EXACT)
21873 bool ok = false;
21874 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21875 if (conv_p)
21876 /* Avoid recalculating this in add_function_candidate. */
21877 ok = (*conv_p
21878 = good_conversion (parm, type, conv_arg, flags, complain));
21879 else
21880 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21881 if (ok)
21882 return unify_success (explain_p);
21885 if (strict == DEDUCE_EXACT)
21886 return unify_type_mismatch (explain_p, parm, arg);
21887 else
21888 return unify_arg_conversion (explain_p, parm, type, arg);
21891 static bool uses_deducible_template_parms (tree type);
21893 /* Returns true iff the expression EXPR is one from which a template
21894 argument can be deduced. In other words, if it's an undecorated
21895 use of a template non-type parameter. */
21897 static bool
21898 deducible_expression (tree expr)
21900 /* Strip implicit conversions and implicit INDIRECT_REFs. */
21901 while (CONVERT_EXPR_P (expr)
21902 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
21903 || REFERENCE_REF_P (expr))
21904 expr = TREE_OPERAND (expr, 0);
21905 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21908 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21909 deducible way; that is, if it has a max value of <PARM> - 1. */
21911 static bool
21912 deducible_array_bound (tree domain)
21914 if (domain == NULL_TREE)
21915 return false;
21917 tree max = TYPE_MAX_VALUE (domain);
21918 if (TREE_CODE (max) != MINUS_EXPR)
21919 return false;
21921 return deducible_expression (TREE_OPERAND (max, 0));
21924 /* Returns true iff the template arguments ARGS use a template parameter
21925 in a deducible way. */
21927 static bool
21928 deducible_template_args (tree args)
21930 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21932 bool deducible;
21933 tree elt = TREE_VEC_ELT (args, i);
21934 if (ARGUMENT_PACK_P (elt))
21935 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21936 else
21938 if (PACK_EXPANSION_P (elt))
21939 elt = PACK_EXPANSION_PATTERN (elt);
21940 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21941 deducible = true;
21942 else if (TYPE_P (elt))
21943 deducible = uses_deducible_template_parms (elt);
21944 else
21945 deducible = deducible_expression (elt);
21947 if (deducible)
21948 return true;
21950 return false;
21953 /* Returns true iff TYPE contains any deducible references to template
21954 parameters, as per 14.8.2.5. */
21956 static bool
21957 uses_deducible_template_parms (tree type)
21959 if (PACK_EXPANSION_P (type))
21960 type = PACK_EXPANSION_PATTERN (type);
21962 /* T
21963 cv-list T
21964 TT<T>
21965 TT<i>
21966 TT<> */
21967 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21968 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21969 return true;
21971 /* T*
21973 T&& */
21974 if (INDIRECT_TYPE_P (type))
21975 return uses_deducible_template_parms (TREE_TYPE (type));
21977 /* T[integer-constant ]
21978 type [i] */
21979 if (TREE_CODE (type) == ARRAY_TYPE)
21980 return (uses_deducible_template_parms (TREE_TYPE (type))
21981 || deducible_array_bound (TYPE_DOMAIN (type)));
21983 /* T type ::*
21984 type T::*
21985 T T::*
21986 T (type ::*)()
21987 type (T::*)()
21988 type (type ::*)(T)
21989 type (T::*)(T)
21990 T (type ::*)(T)
21991 T (T::*)()
21992 T (T::*)(T) */
21993 if (TYPE_PTRMEM_P (type))
21994 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21995 || (uses_deducible_template_parms
21996 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21998 /* template-name <T> (where template-name refers to a class template)
21999 template-name <i> (where template-name refers to a class template) */
22000 if (CLASS_TYPE_P (type)
22001 && CLASSTYPE_TEMPLATE_INFO (type)
22002 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
22003 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22004 (CLASSTYPE_TI_ARGS (type)));
22006 /* type (T)
22008 T(T) */
22009 if (FUNC_OR_METHOD_TYPE_P (type))
22011 if (uses_deducible_template_parms (TREE_TYPE (type)))
22012 return true;
22013 tree parm = TYPE_ARG_TYPES (type);
22014 if (TREE_CODE (type) == METHOD_TYPE)
22015 parm = TREE_CHAIN (parm);
22016 for (; parm; parm = TREE_CHAIN (parm))
22017 if (uses_deducible_template_parms (TREE_VALUE (parm)))
22018 return true;
22021 return false;
22024 /* Subroutine of type_unification_real and unify_pack_expansion to
22025 handle unification of a single P/A pair. Parameters are as
22026 for those functions. */
22028 static int
22029 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
22030 int subr, unification_kind_t strict,
22031 bool explain_p)
22033 tree arg_expr = NULL_TREE;
22034 int arg_strict;
22036 if (arg == error_mark_node || parm == error_mark_node)
22037 return unify_invalid (explain_p);
22038 if (arg == unknown_type_node)
22039 /* We can't deduce anything from this, but we might get all the
22040 template args from other function args. */
22041 return unify_success (explain_p);
22043 /* Implicit conversions (Clause 4) will be performed on a function
22044 argument to convert it to the type of the corresponding function
22045 parameter if the parameter type contains no template-parameters that
22046 participate in template argument deduction. */
22047 if (strict != DEDUCE_EXACT
22048 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
22049 /* For function parameters with no deducible template parameters,
22050 just return. We'll check non-dependent conversions later. */
22051 return unify_success (explain_p);
22053 switch (strict)
22055 case DEDUCE_CALL:
22056 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
22057 | UNIFY_ALLOW_MORE_CV_QUAL
22058 | UNIFY_ALLOW_DERIVED);
22059 break;
22061 case DEDUCE_CONV:
22062 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
22063 break;
22065 case DEDUCE_EXACT:
22066 arg_strict = UNIFY_ALLOW_NONE;
22067 break;
22069 default:
22070 gcc_unreachable ();
22073 /* We only do these transformations if this is the top-level
22074 parameter_type_list in a call or declaration matching; in other
22075 situations (nested function declarators, template argument lists) we
22076 won't be comparing a type to an expression, and we don't do any type
22077 adjustments. */
22078 if (!subr)
22080 if (!TYPE_P (arg))
22082 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
22083 if (type_unknown_p (arg))
22085 /* [temp.deduct.type] A template-argument can be
22086 deduced from a pointer to function or pointer
22087 to member function argument if the set of
22088 overloaded functions does not contain function
22089 templates and at most one of a set of
22090 overloaded functions provides a unique
22091 match. */
22092 resolve_overloaded_unification (tparms, targs, parm,
22093 arg, strict,
22094 arg_strict, explain_p);
22095 /* If a unique match was not found, this is a
22096 non-deduced context, so we still succeed. */
22097 return unify_success (explain_p);
22100 arg_expr = arg;
22101 arg = unlowered_expr_type (arg);
22102 if (arg == error_mark_node)
22103 return unify_invalid (explain_p);
22106 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
22107 &parm, &arg, arg_expr);
22109 else
22110 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
22111 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
22112 return unify_template_argument_mismatch (explain_p, parm, arg);
22114 /* For deduction from an init-list we need the actual list. */
22115 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
22116 arg = arg_expr;
22117 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
22120 /* for_each_template_parm callback that always returns 0. */
22122 static int
22123 zero_r (tree, void *)
22125 return 0;
22128 /* for_each_template_parm any_fn callback to handle deduction of a template
22129 type argument from the type of an array bound. */
22131 static int
22132 array_deduction_r (tree t, void *data)
22134 tree_pair_p d = (tree_pair_p)data;
22135 tree &tparms = d->purpose;
22136 tree &targs = d->value;
22138 if (TREE_CODE (t) == ARRAY_TYPE)
22139 if (tree dom = TYPE_DOMAIN (t))
22140 if (tree max = TYPE_MAX_VALUE (dom))
22142 if (TREE_CODE (max) == MINUS_EXPR)
22143 max = TREE_OPERAND (max, 0);
22144 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
22145 unify (tparms, targs, TREE_TYPE (max), size_type_node,
22146 UNIFY_ALLOW_NONE, /*explain*/false);
22149 /* Keep walking. */
22150 return 0;
22153 /* Try to deduce any not-yet-deduced template type arguments from the type of
22154 an array bound. This is handled separately from unify because 14.8.2.5 says
22155 "The type of a type parameter is only deduced from an array bound if it is
22156 not otherwise deduced." */
22158 static void
22159 try_array_deduction (tree tparms, tree targs, tree parm)
22161 tree_pair_s data = { tparms, targs };
22162 hash_set<tree> visited;
22163 for_each_template_parm (parm, zero_r, &data, &visited,
22164 /*nondeduced*/false, array_deduction_r);
22167 /* Most parms like fn_type_unification.
22169 If SUBR is 1, we're being called recursively (to unify the
22170 arguments of a function or method parameter of a function
22171 template).
22173 CHECKS is a pointer to a vector of access checks encountered while
22174 substituting default template arguments. */
22176 static int
22177 type_unification_real (tree tparms,
22178 tree full_targs,
22179 tree xparms,
22180 const tree *xargs,
22181 unsigned int xnargs,
22182 int subr,
22183 unification_kind_t strict,
22184 vec<deferred_access_check, va_gc> **checks,
22185 bool explain_p)
22187 tree parm, arg;
22188 int i;
22189 int ntparms = TREE_VEC_LENGTH (tparms);
22190 int saw_undeduced = 0;
22191 tree parms;
22192 const tree *args;
22193 unsigned int nargs;
22194 unsigned int ia;
22196 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22197 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22198 gcc_assert (ntparms > 0);
22200 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22202 /* Reset the number of non-defaulted template arguments contained
22203 in TARGS. */
22204 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22206 again:
22207 parms = xparms;
22208 args = xargs;
22209 nargs = xnargs;
22211 ia = 0;
22212 while (parms && parms != void_list_node
22213 && ia < nargs)
22215 parm = TREE_VALUE (parms);
22217 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22218 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22219 /* For a function parameter pack that occurs at the end of the
22220 parameter-declaration-list, the type A of each remaining
22221 argument of the call is compared with the type P of the
22222 declarator-id of the function parameter pack. */
22223 break;
22225 parms = TREE_CHAIN (parms);
22227 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22228 /* For a function parameter pack that does not occur at the
22229 end of the parameter-declaration-list, the type of the
22230 parameter pack is a non-deduced context. */
22231 continue;
22233 arg = args[ia];
22234 ++ia;
22236 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22237 explain_p))
22238 return 1;
22241 if (parms
22242 && parms != void_list_node
22243 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22245 /* Unify the remaining arguments with the pack expansion type. */
22246 tree argvec;
22247 tree parmvec = make_tree_vec (1);
22249 /* Allocate a TREE_VEC and copy in all of the arguments */
22250 argvec = make_tree_vec (nargs - ia);
22251 for (i = 0; ia < nargs; ++ia, ++i)
22252 TREE_VEC_ELT (argvec, i) = args[ia];
22254 /* Copy the parameter into parmvec. */
22255 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22256 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22257 /*subr=*/subr, explain_p))
22258 return 1;
22260 /* Advance to the end of the list of parameters. */
22261 parms = TREE_CHAIN (parms);
22264 /* Fail if we've reached the end of the parm list, and more args
22265 are present, and the parm list isn't variadic. */
22266 if (ia < nargs && parms == void_list_node)
22267 return unify_too_many_arguments (explain_p, nargs, ia);
22268 /* Fail if parms are left and they don't have default values and
22269 they aren't all deduced as empty packs (c++/57397). This is
22270 consistent with sufficient_parms_p. */
22271 if (parms && parms != void_list_node
22272 && TREE_PURPOSE (parms) == NULL_TREE)
22274 unsigned int count = nargs;
22275 tree p = parms;
22276 bool type_pack_p;
22279 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22280 if (!type_pack_p)
22281 count++;
22282 p = TREE_CHAIN (p);
22284 while (p && p != void_list_node);
22285 if (count != nargs)
22286 return unify_too_few_arguments (explain_p, ia, count,
22287 type_pack_p);
22290 if (!subr)
22292 tsubst_flags_t complain = (explain_p
22293 ? tf_warning_or_error
22294 : tf_none);
22295 bool tried_array_deduction = (cxx_dialect < cxx17);
22297 for (i = 0; i < ntparms; i++)
22299 tree targ = TREE_VEC_ELT (targs, i);
22300 tree tparm = TREE_VEC_ELT (tparms, i);
22302 /* Clear the "incomplete" flags on all argument packs now so that
22303 substituting them into later default arguments works. */
22304 if (targ && ARGUMENT_PACK_P (targ))
22306 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22307 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22310 if (targ || tparm == error_mark_node)
22311 continue;
22312 tparm = TREE_VALUE (tparm);
22314 if (TREE_CODE (tparm) == TYPE_DECL
22315 && !tried_array_deduction)
22317 try_array_deduction (tparms, targs, xparms);
22318 tried_array_deduction = true;
22319 if (TREE_VEC_ELT (targs, i))
22320 continue;
22323 /* If this is an undeduced nontype parameter that depends on
22324 a type parameter, try another pass; its type may have been
22325 deduced from a later argument than the one from which
22326 this parameter can be deduced. */
22327 if (TREE_CODE (tparm) == PARM_DECL
22328 && uses_template_parms (TREE_TYPE (tparm))
22329 && saw_undeduced < 2)
22331 saw_undeduced = 1;
22332 continue;
22335 /* Core issue #226 (C++0x) [temp.deduct]:
22337 If a template argument has not been deduced, its
22338 default template argument, if any, is used.
22340 When we are in C++98 mode, TREE_PURPOSE will either
22341 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22342 to explicitly check cxx_dialect here. */
22343 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22344 /* OK, there is a default argument. Wait until after the
22345 conversion check to do substitution. */
22346 continue;
22348 /* If the type parameter is a parameter pack, then it will
22349 be deduced to an empty parameter pack. */
22350 if (template_parameter_pack_p (tparm))
22352 tree arg;
22354 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22356 arg = make_node (NONTYPE_ARGUMENT_PACK);
22357 TREE_CONSTANT (arg) = 1;
22359 else
22360 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22362 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22364 TREE_VEC_ELT (targs, i) = arg;
22365 continue;
22368 return unify_parameter_deduction_failure (explain_p, tparm);
22371 /* Now substitute into the default template arguments. */
22372 for (i = 0; i < ntparms; i++)
22374 tree targ = TREE_VEC_ELT (targs, i);
22375 tree tparm = TREE_VEC_ELT (tparms, i);
22377 if (targ || tparm == error_mark_node)
22378 continue;
22379 tree parm = TREE_VALUE (tparm);
22380 tree arg = TREE_PURPOSE (tparm);
22381 reopen_deferring_access_checks (*checks);
22382 location_t save_loc = input_location;
22383 if (DECL_P (parm))
22384 input_location = DECL_SOURCE_LOCATION (parm);
22386 if (saw_undeduced == 1
22387 && TREE_CODE (parm) == PARM_DECL
22388 && uses_template_parms (TREE_TYPE (parm)))
22390 /* The type of this non-type parameter depends on undeduced
22391 parameters. Don't try to use its default argument yet,
22392 since we might deduce an argument for it on the next pass,
22393 but do check whether the arguments we already have cause
22394 substitution failure, so that that happens before we try
22395 later default arguments (78489). */
22396 ++processing_template_decl;
22397 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22398 NULL_TREE);
22399 --processing_template_decl;
22400 if (type == error_mark_node)
22401 arg = error_mark_node;
22402 else
22403 arg = NULL_TREE;
22405 else
22407 /* Even if the call is happening in template context, getting
22408 here means it's non-dependent, and a default argument is
22409 considered a separate definition under [temp.decls], so we can
22410 do this substitution without processing_template_decl. This
22411 is important if the default argument contains something that
22412 might be instantiation-dependent like access (87480). */
22413 processing_template_decl_sentinel s;
22414 tree substed = NULL_TREE;
22415 if (saw_undeduced == 1)
22417 /* First instatiate in template context, in case we still
22418 depend on undeduced template parameters. */
22419 ++processing_template_decl;
22420 substed = tsubst_template_arg (arg, full_targs, complain,
22421 NULL_TREE);
22422 --processing_template_decl;
22423 if (substed != error_mark_node
22424 && !uses_template_parms (substed))
22425 /* We replaced all the tparms, substitute again out of
22426 template context. */
22427 substed = NULL_TREE;
22429 if (!substed)
22430 substed = tsubst_template_arg (arg, full_targs, complain,
22431 NULL_TREE);
22433 if (!uses_template_parms (substed))
22434 arg = convert_template_argument (parm, substed, full_targs,
22435 complain, i, NULL_TREE);
22436 else if (saw_undeduced == 1)
22437 arg = NULL_TREE;
22438 else
22439 arg = error_mark_node;
22442 input_location = save_loc;
22443 *checks = get_deferred_access_checks ();
22444 pop_deferring_access_checks ();
22446 if (arg == error_mark_node)
22447 return 1;
22448 else if (arg)
22450 TREE_VEC_ELT (targs, i) = arg;
22451 /* The position of the first default template argument,
22452 is also the number of non-defaulted arguments in TARGS.
22453 Record that. */
22454 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22455 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22459 if (saw_undeduced++ == 1)
22460 goto again;
22463 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22464 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22466 return unify_success (explain_p);
22469 /* Subroutine of type_unification_real. Args are like the variables
22470 at the call site. ARG is an overloaded function (or template-id);
22471 we try deducing template args from each of the overloads, and if
22472 only one succeeds, we go with that. Modifies TARGS and returns
22473 true on success. */
22475 static bool
22476 resolve_overloaded_unification (tree tparms,
22477 tree targs,
22478 tree parm,
22479 tree arg,
22480 unification_kind_t strict,
22481 int sub_strict,
22482 bool explain_p)
22484 tree tempargs = copy_node (targs);
22485 int good = 0;
22486 tree goodfn = NULL_TREE;
22487 bool addr_p;
22489 if (TREE_CODE (arg) == ADDR_EXPR)
22491 arg = TREE_OPERAND (arg, 0);
22492 addr_p = true;
22494 else
22495 addr_p = false;
22497 if (TREE_CODE (arg) == COMPONENT_REF)
22498 /* Handle `&x' where `x' is some static or non-static member
22499 function name. */
22500 arg = TREE_OPERAND (arg, 1);
22502 if (TREE_CODE (arg) == OFFSET_REF)
22503 arg = TREE_OPERAND (arg, 1);
22505 /* Strip baselink information. */
22506 if (BASELINK_P (arg))
22507 arg = BASELINK_FUNCTIONS (arg);
22509 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22511 /* If we got some explicit template args, we need to plug them into
22512 the affected templates before we try to unify, in case the
22513 explicit args will completely resolve the templates in question. */
22515 int ok = 0;
22516 tree expl_subargs = TREE_OPERAND (arg, 1);
22517 arg = TREE_OPERAND (arg, 0);
22519 for (lkp_iterator iter (arg); iter; ++iter)
22521 tree fn = *iter;
22522 tree subargs, elem;
22524 if (TREE_CODE (fn) != TEMPLATE_DECL)
22525 continue;
22527 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22528 expl_subargs, NULL_TREE, tf_none,
22529 /*require_all_args=*/true,
22530 /*use_default_args=*/true);
22531 if (subargs != error_mark_node
22532 && !any_dependent_template_arguments_p (subargs))
22534 fn = instantiate_template (fn, subargs, tf_none);
22535 if (!constraints_satisfied_p (fn))
22536 continue;
22537 if (undeduced_auto_decl (fn))
22539 /* Instantiate the function to deduce its return type. */
22540 ++function_depth;
22541 instantiate_decl (fn, /*defer*/false, /*class*/false);
22542 --function_depth;
22545 if (flag_noexcept_type)
22546 maybe_instantiate_noexcept (fn, tf_none);
22548 elem = TREE_TYPE (fn);
22549 if (try_one_overload (tparms, targs, tempargs, parm,
22550 elem, strict, sub_strict, addr_p, explain_p)
22551 && (!goodfn || !same_type_p (goodfn, elem)))
22553 goodfn = elem;
22554 ++good;
22557 else if (subargs)
22558 ++ok;
22560 /* If no templates (or more than one) are fully resolved by the
22561 explicit arguments, this template-id is a non-deduced context; it
22562 could still be OK if we deduce all template arguments for the
22563 enclosing call through other arguments. */
22564 if (good != 1)
22565 good = ok;
22567 else if (!OVL_P (arg))
22568 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22569 -- but the deduction does not succeed because the expression is
22570 not just the function on its own. */
22571 return false;
22572 else
22573 for (lkp_iterator iter (arg); iter; ++iter)
22575 tree fn = *iter;
22576 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22577 strict, sub_strict, addr_p, explain_p)
22578 && (!goodfn || !decls_match (goodfn, fn)))
22580 goodfn = fn;
22581 ++good;
22585 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22586 to function or pointer to member function argument if the set of
22587 overloaded functions does not contain function templates and at most
22588 one of a set of overloaded functions provides a unique match.
22590 So if we found multiple possibilities, we return success but don't
22591 deduce anything. */
22593 if (good == 1)
22595 int i = TREE_VEC_LENGTH (targs);
22596 for (; i--; )
22597 if (TREE_VEC_ELT (tempargs, i))
22599 tree old = TREE_VEC_ELT (targs, i);
22600 tree new_ = TREE_VEC_ELT (tempargs, i);
22601 if (new_ && old && ARGUMENT_PACK_P (old)
22602 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22603 /* Don't forget explicit template arguments in a pack. */
22604 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22605 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22606 TREE_VEC_ELT (targs, i) = new_;
22609 if (good)
22610 return true;
22612 return false;
22615 /* Core DR 115: In contexts where deduction is done and fails, or in
22616 contexts where deduction is not done, if a template argument list is
22617 specified and it, along with any default template arguments, identifies
22618 a single function template specialization, then the template-id is an
22619 lvalue for the function template specialization. */
22621 tree
22622 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22624 tree expr, offset, baselink;
22625 bool addr;
22627 if (!type_unknown_p (orig_expr))
22628 return orig_expr;
22630 expr = orig_expr;
22631 addr = false;
22632 offset = NULL_TREE;
22633 baselink = NULL_TREE;
22635 if (TREE_CODE (expr) == ADDR_EXPR)
22637 expr = TREE_OPERAND (expr, 0);
22638 addr = true;
22640 if (TREE_CODE (expr) == OFFSET_REF)
22642 offset = expr;
22643 expr = TREE_OPERAND (expr, 1);
22645 if (BASELINK_P (expr))
22647 baselink = expr;
22648 expr = BASELINK_FUNCTIONS (expr);
22651 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22653 int good = 0;
22654 tree goodfn = NULL_TREE;
22656 /* If we got some explicit template args, we need to plug them into
22657 the affected templates before we try to unify, in case the
22658 explicit args will completely resolve the templates in question. */
22660 tree expl_subargs = TREE_OPERAND (expr, 1);
22661 tree arg = TREE_OPERAND (expr, 0);
22662 tree badfn = NULL_TREE;
22663 tree badargs = NULL_TREE;
22665 for (lkp_iterator iter (arg); iter; ++iter)
22667 tree fn = *iter;
22668 tree subargs, elem;
22670 if (TREE_CODE (fn) != TEMPLATE_DECL)
22671 continue;
22673 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22674 expl_subargs, NULL_TREE, tf_none,
22675 /*require_all_args=*/true,
22676 /*use_default_args=*/true);
22677 if (subargs != error_mark_node
22678 && !any_dependent_template_arguments_p (subargs))
22680 elem = instantiate_template (fn, subargs, tf_none);
22681 if (elem == error_mark_node)
22683 badfn = fn;
22684 badargs = subargs;
22686 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22687 && constraints_satisfied_p (elem))
22689 goodfn = elem;
22690 ++good;
22694 if (good == 1)
22696 mark_used (goodfn);
22697 expr = goodfn;
22698 if (baselink)
22699 expr = build_baselink (BASELINK_BINFO (baselink),
22700 BASELINK_ACCESS_BINFO (baselink),
22701 expr, BASELINK_OPTYPE (baselink));
22702 if (offset)
22704 tree base
22705 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22706 expr = build_offset_ref (base, expr, addr, complain);
22708 if (addr)
22709 expr = cp_build_addr_expr (expr, complain);
22710 return expr;
22712 else if (good == 0 && badargs && (complain & tf_error))
22713 /* There were no good options and at least one bad one, so let the
22714 user know what the problem is. */
22715 instantiate_template (badfn, badargs, complain);
22717 return orig_expr;
22720 /* As above, but error out if the expression remains overloaded. */
22722 tree
22723 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22725 exp = resolve_nondeduced_context (exp, complain);
22726 if (type_unknown_p (exp))
22728 if (complain & tf_error)
22729 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22730 return error_mark_node;
22732 return exp;
22735 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22736 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22737 different overloads deduce different arguments for a given parm.
22738 ADDR_P is true if the expression for which deduction is being
22739 performed was of the form "& fn" rather than simply "fn".
22741 Returns 1 on success. */
22743 static int
22744 try_one_overload (tree tparms,
22745 tree orig_targs,
22746 tree targs,
22747 tree parm,
22748 tree arg,
22749 unification_kind_t strict,
22750 int sub_strict,
22751 bool addr_p,
22752 bool explain_p)
22754 int nargs;
22755 tree tempargs;
22756 int i;
22758 if (arg == error_mark_node)
22759 return 0;
22761 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22762 to function or pointer to member function argument if the set of
22763 overloaded functions does not contain function templates and at most
22764 one of a set of overloaded functions provides a unique match.
22766 So if this is a template, just return success. */
22768 if (uses_template_parms (arg))
22769 return 1;
22771 if (TREE_CODE (arg) == METHOD_TYPE)
22772 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22773 else if (addr_p)
22774 arg = build_pointer_type (arg);
22776 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
22777 &parm, &arg, NULL_TREE);
22779 /* We don't copy orig_targs for this because if we have already deduced
22780 some template args from previous args, unify would complain when we
22781 try to deduce a template parameter for the same argument, even though
22782 there isn't really a conflict. */
22783 nargs = TREE_VEC_LENGTH (targs);
22784 tempargs = make_tree_vec (nargs);
22786 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22787 return 0;
22789 /* First make sure we didn't deduce anything that conflicts with
22790 explicitly specified args. */
22791 for (i = nargs; i--; )
22793 tree elt = TREE_VEC_ELT (tempargs, i);
22794 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22796 if (!elt)
22797 /*NOP*/;
22798 else if (uses_template_parms (elt))
22799 /* Since we're unifying against ourselves, we will fill in
22800 template args used in the function parm list with our own
22801 template parms. Discard them. */
22802 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22803 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22805 /* Check that the argument at each index of the deduced argument pack
22806 is equivalent to the corresponding explicitly specified argument.
22807 We may have deduced more arguments than were explicitly specified,
22808 and that's OK. */
22810 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22811 that's wrong if we deduce the same argument pack from multiple
22812 function arguments: it's only incomplete the first time. */
22814 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22815 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22817 if (TREE_VEC_LENGTH (deduced_pack)
22818 < TREE_VEC_LENGTH (explicit_pack))
22819 return 0;
22821 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22822 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22823 TREE_VEC_ELT (deduced_pack, j)))
22824 return 0;
22826 else if (oldelt && !template_args_equal (oldelt, elt))
22827 return 0;
22830 for (i = nargs; i--; )
22832 tree elt = TREE_VEC_ELT (tempargs, i);
22834 if (elt)
22835 TREE_VEC_ELT (targs, i) = elt;
22838 return 1;
22841 /* PARM is a template class (perhaps with unbound template
22842 parameters). ARG is a fully instantiated type. If ARG can be
22843 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22844 TARGS are as for unify. */
22846 static tree
22847 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22848 bool explain_p)
22850 tree copy_of_targs;
22852 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22853 return NULL_TREE;
22854 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22855 /* Matches anything. */;
22856 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22857 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22858 return NULL_TREE;
22860 /* We need to make a new template argument vector for the call to
22861 unify. If we used TARGS, we'd clutter it up with the result of
22862 the attempted unification, even if this class didn't work out.
22863 We also don't want to commit ourselves to all the unifications
22864 we've already done, since unification is supposed to be done on
22865 an argument-by-argument basis. In other words, consider the
22866 following pathological case:
22868 template <int I, int J, int K>
22869 struct S {};
22871 template <int I, int J>
22872 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22874 template <int I, int J, int K>
22875 void f(S<I, J, K>, S<I, I, I>);
22877 void g() {
22878 S<0, 0, 0> s0;
22879 S<0, 1, 2> s2;
22881 f(s0, s2);
22884 Now, by the time we consider the unification involving `s2', we
22885 already know that we must have `f<0, 0, 0>'. But, even though
22886 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22887 because there are two ways to unify base classes of S<0, 1, 2>
22888 with S<I, I, I>. If we kept the already deduced knowledge, we
22889 would reject the possibility I=1. */
22890 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22892 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22894 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22895 return NULL_TREE;
22896 return arg;
22899 /* If unification failed, we're done. */
22900 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22901 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22902 return NULL_TREE;
22904 return arg;
22907 /* Given a template type PARM and a class type ARG, find the unique
22908 base type in ARG that is an instance of PARM. We do not examine
22909 ARG itself; only its base-classes. If there is not exactly one
22910 appropriate base class, return NULL_TREE. PARM may be the type of
22911 a partial specialization, as well as a plain template type. Used
22912 by unify. */
22914 static enum template_base_result
22915 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22916 bool explain_p, tree *result)
22918 tree rval = NULL_TREE;
22919 tree binfo;
22921 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22923 binfo = TYPE_BINFO (complete_type (arg));
22924 if (!binfo)
22926 /* The type could not be completed. */
22927 *result = NULL_TREE;
22928 return tbr_incomplete_type;
22931 /* Walk in inheritance graph order. The search order is not
22932 important, and this avoids multiple walks of virtual bases. */
22933 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22935 tree r = try_class_unification (tparms, targs, parm,
22936 BINFO_TYPE (binfo), explain_p);
22938 if (r)
22940 /* If there is more than one satisfactory baseclass, then:
22942 [temp.deduct.call]
22944 If they yield more than one possible deduced A, the type
22945 deduction fails.
22947 applies. */
22948 if (rval && !same_type_p (r, rval))
22950 /* [temp.deduct.call]/4.3: If there is a class C that is a
22951 (direct or indirect) base class of D and derived (directly or
22952 indirectly) from a class B and that would be a valid deduced
22953 A, the deduced A cannot be B or pointer to B, respectively. */
22954 if (DERIVED_FROM_P (r, rval))
22955 /* Ignore r. */
22956 continue;
22957 else if (DERIVED_FROM_P (rval, r))
22958 /* Ignore rval. */;
22959 else
22961 *result = NULL_TREE;
22962 return tbr_ambiguous_baseclass;
22966 rval = r;
22970 *result = rval;
22971 return tbr_success;
22974 /* Returns the level of DECL, which declares a template parameter. */
22976 static int
22977 template_decl_level (tree decl)
22979 switch (TREE_CODE (decl))
22981 case TYPE_DECL:
22982 case TEMPLATE_DECL:
22983 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22985 case PARM_DECL:
22986 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22988 default:
22989 gcc_unreachable ();
22991 return 0;
22994 /* Decide whether ARG can be unified with PARM, considering only the
22995 cv-qualifiers of each type, given STRICT as documented for unify.
22996 Returns nonzero iff the unification is OK on that basis. */
22998 static int
22999 check_cv_quals_for_unify (int strict, tree arg, tree parm)
23001 int arg_quals = cp_type_quals (arg);
23002 int parm_quals = cp_type_quals (parm);
23004 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23005 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23007 /* Although a CVR qualifier is ignored when being applied to a
23008 substituted template parameter ([8.3.2]/1 for example), that
23009 does not allow us to unify "const T" with "int&" because both
23010 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23011 It is ok when we're allowing additional CV qualifiers
23012 at the outer level [14.8.2.1]/3,1st bullet. */
23013 if ((TYPE_REF_P (arg)
23014 || FUNC_OR_METHOD_TYPE_P (arg))
23015 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
23016 return 0;
23018 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
23019 && (parm_quals & TYPE_QUAL_RESTRICT))
23020 return 0;
23023 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23024 && (arg_quals & parm_quals) != parm_quals)
23025 return 0;
23027 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
23028 && (parm_quals & arg_quals) != arg_quals)
23029 return 0;
23031 return 1;
23034 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23035 void
23036 template_parm_level_and_index (tree parm, int* level, int* index)
23038 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23039 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23040 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23042 *index = TEMPLATE_TYPE_IDX (parm);
23043 *level = TEMPLATE_TYPE_LEVEL (parm);
23045 else
23047 *index = TEMPLATE_PARM_IDX (parm);
23048 *level = TEMPLATE_PARM_LEVEL (parm);
23052 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23053 do { \
23054 if (unify (TP, TA, P, A, S, EP)) \
23055 return 1; \
23056 } while (0)
23058 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23059 expansion at the end of PACKED_PARMS. Returns 0 if the type
23060 deduction succeeds, 1 otherwise. STRICT is the same as in
23061 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23062 function call argument list. We'll need to adjust the arguments to make them
23063 types. SUBR tells us if this is from a recursive call to
23064 type_unification_real, or for comparing two template argument
23065 lists. */
23067 static int
23068 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
23069 tree packed_args, unification_kind_t strict,
23070 bool subr, bool explain_p)
23072 tree parm
23073 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
23074 tree pattern = PACK_EXPANSION_PATTERN (parm);
23075 tree pack, packs = NULL_TREE;
23076 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
23078 /* Add in any args remembered from an earlier partial instantiation. */
23079 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
23080 int levels = TMPL_ARGS_DEPTH (targs);
23082 packed_args = expand_template_argument_pack (packed_args);
23084 int len = TREE_VEC_LENGTH (packed_args);
23086 /* Determine the parameter packs we will be deducing from the
23087 pattern, and record their current deductions. */
23088 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
23089 pack; pack = TREE_CHAIN (pack))
23091 tree parm_pack = TREE_VALUE (pack);
23092 int idx, level;
23094 /* Only template parameter packs can be deduced, not e.g. function
23095 parameter packs or __bases or __integer_pack. */
23096 if (!TEMPLATE_PARM_P (parm_pack))
23097 continue;
23099 /* Determine the index and level of this parameter pack. */
23100 template_parm_level_and_index (parm_pack, &level, &idx);
23101 if (level < levels)
23102 continue;
23104 /* Keep track of the parameter packs and their corresponding
23105 argument packs. */
23106 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
23107 TREE_TYPE (packs) = make_tree_vec (len - start);
23110 /* Loop through all of the arguments that have not yet been
23111 unified and unify each with the pattern. */
23112 for (i = start; i < len; i++)
23114 tree parm;
23115 bool any_explicit = false;
23116 tree arg = TREE_VEC_ELT (packed_args, i);
23118 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23119 or the element of its argument pack at the current index if
23120 this argument was explicitly specified. */
23121 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23123 int idx, level;
23124 tree arg, pargs;
23125 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23127 arg = NULL_TREE;
23128 if (TREE_VALUE (pack)
23129 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
23130 && (i - start < TREE_VEC_LENGTH (pargs)))
23132 any_explicit = true;
23133 arg = TREE_VEC_ELT (pargs, i - start);
23135 TMPL_ARG (targs, level, idx) = arg;
23138 /* If we had explicit template arguments, substitute them into the
23139 pattern before deduction. */
23140 if (any_explicit)
23142 /* Some arguments might still be unspecified or dependent. */
23143 bool dependent;
23144 ++processing_template_decl;
23145 dependent = any_dependent_template_arguments_p (targs);
23146 if (!dependent)
23147 --processing_template_decl;
23148 parm = tsubst (pattern, targs,
23149 explain_p ? tf_warning_or_error : tf_none,
23150 NULL_TREE);
23151 if (dependent)
23152 --processing_template_decl;
23153 if (parm == error_mark_node)
23154 return 1;
23156 else
23157 parm = pattern;
23159 /* Unify the pattern with the current argument. */
23160 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23161 explain_p))
23162 return 1;
23164 /* For each parameter pack, collect the deduced value. */
23165 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23167 int idx, level;
23168 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23170 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23171 TMPL_ARG (targs, level, idx);
23175 /* Verify that the results of unification with the parameter packs
23176 produce results consistent with what we've seen before, and make
23177 the deduced argument packs available. */
23178 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23180 tree old_pack = TREE_VALUE (pack);
23181 tree new_args = TREE_TYPE (pack);
23182 int i, len = TREE_VEC_LENGTH (new_args);
23183 int idx, level;
23184 bool nondeduced_p = false;
23186 /* By default keep the original deduced argument pack.
23187 If necessary, more specific code is going to update the
23188 resulting deduced argument later down in this function. */
23189 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23190 TMPL_ARG (targs, level, idx) = old_pack;
23192 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23193 actually deduce anything. */
23194 for (i = 0; i < len && !nondeduced_p; ++i)
23195 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23196 nondeduced_p = true;
23197 if (nondeduced_p)
23198 continue;
23200 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23202 /* If we had fewer function args than explicit template args,
23203 just use the explicits. */
23204 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23205 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23206 if (len < explicit_len)
23207 new_args = explicit_args;
23210 if (!old_pack)
23212 tree result;
23213 /* Build the deduced *_ARGUMENT_PACK. */
23214 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23216 result = make_node (NONTYPE_ARGUMENT_PACK);
23217 TREE_CONSTANT (result) = 1;
23219 else
23220 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23222 SET_ARGUMENT_PACK_ARGS (result, new_args);
23224 /* Note the deduced argument packs for this parameter
23225 pack. */
23226 TMPL_ARG (targs, level, idx) = result;
23228 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23229 && (ARGUMENT_PACK_ARGS (old_pack)
23230 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23232 /* We only had the explicitly-provided arguments before, but
23233 now we have a complete set of arguments. */
23234 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23236 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23237 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23238 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23240 else
23242 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23243 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23244 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23245 /* During template argument deduction for the aggregate deduction
23246 candidate, the number of elements in a trailing parameter pack
23247 is only deduced from the number of remaining function
23248 arguments if it is not otherwise deduced. */
23249 if (cxx_dialect >= cxx20
23250 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23251 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23252 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23253 if (!comp_template_args (old_args, new_args,
23254 &bad_old_arg, &bad_new_arg))
23255 /* Inconsistent unification of this parameter pack. */
23256 return unify_parameter_pack_inconsistent (explain_p,
23257 bad_old_arg,
23258 bad_new_arg);
23262 return unify_success (explain_p);
23265 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23266 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23267 parameters and return value are as for unify. */
23269 static int
23270 unify_array_domain (tree tparms, tree targs,
23271 tree parm_dom, tree arg_dom,
23272 bool explain_p)
23274 tree parm_max;
23275 tree arg_max;
23276 bool parm_cst;
23277 bool arg_cst;
23279 /* Our representation of array types uses "N - 1" as the
23280 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23281 not an integer constant. We cannot unify arbitrarily
23282 complex expressions, so we eliminate the MINUS_EXPRs
23283 here. */
23284 parm_max = TYPE_MAX_VALUE (parm_dom);
23285 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23286 if (!parm_cst)
23288 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23289 parm_max = TREE_OPERAND (parm_max, 0);
23291 arg_max = TYPE_MAX_VALUE (arg_dom);
23292 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23293 if (!arg_cst)
23295 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23296 trying to unify the type of a variable with the type
23297 of a template parameter. For example:
23299 template <unsigned int N>
23300 void f (char (&) [N]);
23301 int g();
23302 void h(int i) {
23303 char a[g(i)];
23304 f(a);
23307 Here, the type of the ARG will be "int [g(i)]", and
23308 may be a SAVE_EXPR, etc. */
23309 if (TREE_CODE (arg_max) != MINUS_EXPR)
23310 return unify_vla_arg (explain_p, arg_dom);
23311 arg_max = TREE_OPERAND (arg_max, 0);
23314 /* If only one of the bounds used a MINUS_EXPR, compensate
23315 by adding one to the other bound. */
23316 if (parm_cst && !arg_cst)
23317 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23318 integer_type_node,
23319 parm_max,
23320 integer_one_node);
23321 else if (arg_cst && !parm_cst)
23322 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23323 integer_type_node,
23324 arg_max,
23325 integer_one_node);
23327 return unify (tparms, targs, parm_max, arg_max,
23328 UNIFY_ALLOW_INTEGER, explain_p);
23331 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23333 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23335 static pa_kind_t
23336 pa_kind (tree t)
23338 if (PACK_EXPANSION_P (t))
23339 t = PACK_EXPANSION_PATTERN (t);
23340 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23341 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23342 || DECL_TYPE_TEMPLATE_P (t))
23343 return pa_tmpl;
23344 else if (TYPE_P (t))
23345 return pa_type;
23346 else
23347 return pa_expr;
23350 /* Deduce the value of template parameters. TPARMS is the (innermost)
23351 set of template parameters to a template. TARGS is the bindings
23352 for those template parameters, as determined thus far; TARGS may
23353 include template arguments for outer levels of template parameters
23354 as well. PARM is a parameter to a template function, or a
23355 subcomponent of that parameter; ARG is the corresponding argument.
23356 This function attempts to match PARM with ARG in a manner
23357 consistent with the existing assignments in TARGS. If more values
23358 are deduced, then TARGS is updated.
23360 Returns 0 if the type deduction succeeds, 1 otherwise. The
23361 parameter STRICT is a bitwise or of the following flags:
23363 UNIFY_ALLOW_NONE:
23364 Require an exact match between PARM and ARG.
23365 UNIFY_ALLOW_MORE_CV_QUAL:
23366 Allow the deduced ARG to be more cv-qualified (by qualification
23367 conversion) than ARG.
23368 UNIFY_ALLOW_LESS_CV_QUAL:
23369 Allow the deduced ARG to be less cv-qualified than ARG.
23370 UNIFY_ALLOW_DERIVED:
23371 Allow the deduced ARG to be a template base class of ARG,
23372 or a pointer to a template base class of the type pointed to by
23373 ARG.
23374 UNIFY_ALLOW_INTEGER:
23375 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23376 case for more information.
23377 UNIFY_ALLOW_OUTER_LEVEL:
23378 This is the outermost level of a deduction. Used to determine validity
23379 of qualification conversions. A valid qualification conversion must
23380 have const qualified pointers leading up to the inner type which
23381 requires additional CV quals, except at the outer level, where const
23382 is not required [conv.qual]. It would be normal to set this flag in
23383 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23384 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23385 This is the outermost level of a deduction, and PARM can be more CV
23386 qualified at this point.
23387 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23388 This is the outermost level of a deduction, and PARM can be less CV
23389 qualified at this point. */
23391 static int
23392 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23393 bool explain_p)
23395 int idx;
23396 tree targ;
23397 tree tparm;
23398 int strict_in = strict;
23399 tsubst_flags_t complain = (explain_p
23400 ? tf_warning_or_error
23401 : tf_none);
23403 /* I don't think this will do the right thing with respect to types.
23404 But the only case I've seen it in so far has been array bounds, where
23405 signedness is the only information lost, and I think that will be
23406 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23407 finish_id_expression_1, and are also OK. */
23408 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23409 parm = TREE_OPERAND (parm, 0);
23411 if (arg == error_mark_node)
23412 return unify_invalid (explain_p);
23413 if (arg == unknown_type_node
23414 || arg == init_list_type_node)
23415 /* We can't deduce anything from this, but we might get all the
23416 template args from other function args. */
23417 return unify_success (explain_p);
23419 if (parm == any_targ_node || arg == any_targ_node)
23420 return unify_success (explain_p);
23422 /* If PARM uses template parameters, then we can't bail out here,
23423 even if ARG == PARM, since we won't record unifications for the
23424 template parameters. We might need them if we're trying to
23425 figure out which of two things is more specialized. */
23426 if (arg == parm && !uses_template_parms (parm))
23427 return unify_success (explain_p);
23429 /* Handle init lists early, so the rest of the function can assume
23430 we're dealing with a type. */
23431 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23433 tree elt, elttype;
23434 unsigned i;
23435 tree orig_parm = parm;
23437 if (!is_std_init_list (parm)
23438 && TREE_CODE (parm) != ARRAY_TYPE)
23439 /* We can only deduce from an initializer list argument if the
23440 parameter is std::initializer_list or an array; otherwise this
23441 is a non-deduced context. */
23442 return unify_success (explain_p);
23444 if (TREE_CODE (parm) == ARRAY_TYPE)
23445 elttype = TREE_TYPE (parm);
23446 else
23448 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23449 /* Deduction is defined in terms of a single type, so just punt
23450 on the (bizarre) std::initializer_list<T...>. */
23451 if (PACK_EXPANSION_P (elttype))
23452 return unify_success (explain_p);
23455 if (strict != DEDUCE_EXACT
23456 && TYPE_P (elttype)
23457 && !uses_deducible_template_parms (elttype))
23458 /* If ELTTYPE has no deducible template parms, skip deduction from
23459 the list elements. */;
23460 else
23461 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23463 int elt_strict = strict;
23465 if (elt == error_mark_node)
23466 return unify_invalid (explain_p);
23468 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23470 tree type = TREE_TYPE (elt);
23471 if (type == error_mark_node)
23472 return unify_invalid (explain_p);
23473 /* It should only be possible to get here for a call. */
23474 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23475 elt_strict |= maybe_adjust_types_for_deduction
23476 (tparms, DEDUCE_CALL, &elttype, &type, elt);
23477 elt = type;
23480 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23481 explain_p);
23484 if (TREE_CODE (parm) == ARRAY_TYPE
23485 && deducible_array_bound (TYPE_DOMAIN (parm)))
23487 /* Also deduce from the length of the initializer list. */
23488 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23489 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23490 if (idx == error_mark_node)
23491 return unify_invalid (explain_p);
23492 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23493 idx, explain_p);
23496 /* If the std::initializer_list<T> deduction worked, replace the
23497 deduced A with std::initializer_list<A>. */
23498 if (orig_parm != parm)
23500 idx = TEMPLATE_TYPE_IDX (orig_parm);
23501 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23502 targ = listify (targ);
23503 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23505 return unify_success (explain_p);
23508 /* If parm and arg aren't the same kind of thing (template, type, or
23509 expression), fail early. */
23510 if (pa_kind (parm) != pa_kind (arg))
23511 return unify_invalid (explain_p);
23513 /* Immediately reject some pairs that won't unify because of
23514 cv-qualification mismatches. */
23515 if (TREE_CODE (arg) == TREE_CODE (parm)
23516 && TYPE_P (arg)
23517 /* It is the elements of the array which hold the cv quals of an array
23518 type, and the elements might be template type parms. We'll check
23519 when we recurse. */
23520 && TREE_CODE (arg) != ARRAY_TYPE
23521 /* We check the cv-qualifiers when unifying with template type
23522 parameters below. We want to allow ARG `const T' to unify with
23523 PARM `T' for example, when computing which of two templates
23524 is more specialized, for example. */
23525 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23526 && !check_cv_quals_for_unify (strict_in, arg, parm))
23527 return unify_cv_qual_mismatch (explain_p, parm, arg);
23529 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23530 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23531 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23532 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23533 strict &= ~UNIFY_ALLOW_DERIVED;
23534 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23535 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23537 switch (TREE_CODE (parm))
23539 case TYPENAME_TYPE:
23540 case SCOPE_REF:
23541 case UNBOUND_CLASS_TEMPLATE:
23542 /* In a type which contains a nested-name-specifier, template
23543 argument values cannot be deduced for template parameters used
23544 within the nested-name-specifier. */
23545 return unify_success (explain_p);
23547 case TEMPLATE_TYPE_PARM:
23548 case TEMPLATE_TEMPLATE_PARM:
23549 case BOUND_TEMPLATE_TEMPLATE_PARM:
23550 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23551 if (error_operand_p (tparm))
23552 return unify_invalid (explain_p);
23554 if (TEMPLATE_TYPE_LEVEL (parm)
23555 != template_decl_level (tparm))
23556 /* The PARM is not one we're trying to unify. Just check
23557 to see if it matches ARG. */
23559 if (TREE_CODE (arg) == TREE_CODE (parm)
23560 && (is_auto (parm) ? is_auto (arg)
23561 : same_type_p (parm, arg)))
23562 return unify_success (explain_p);
23563 else
23564 return unify_type_mismatch (explain_p, parm, arg);
23566 idx = TEMPLATE_TYPE_IDX (parm);
23567 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23568 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23569 if (error_operand_p (tparm))
23570 return unify_invalid (explain_p);
23572 /* Check for mixed types and values. */
23573 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23574 && TREE_CODE (tparm) != TYPE_DECL)
23575 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23576 && TREE_CODE (tparm) != TEMPLATE_DECL))
23577 gcc_unreachable ();
23579 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23581 if ((strict_in & UNIFY_ALLOW_DERIVED)
23582 && CLASS_TYPE_P (arg))
23584 /* First try to match ARG directly. */
23585 tree t = try_class_unification (tparms, targs, parm, arg,
23586 explain_p);
23587 if (!t)
23589 /* Otherwise, look for a suitable base of ARG, as below. */
23590 enum template_base_result r;
23591 r = get_template_base (tparms, targs, parm, arg,
23592 explain_p, &t);
23593 if (!t)
23594 return unify_no_common_base (explain_p, r, parm, arg);
23595 arg = t;
23598 /* ARG must be constructed from a template class or a template
23599 template parameter. */
23600 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23601 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23602 return unify_template_deduction_failure (explain_p, parm, arg);
23604 /* Deduce arguments T, i from TT<T> or TT<i>. */
23605 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23606 return 1;
23608 arg = TYPE_TI_TEMPLATE (arg);
23609 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
23610 /* If the template is a template template parameter, use the
23611 TEMPLATE_TEMPLATE_PARM for matching. */
23612 arg = TREE_TYPE (arg);
23614 /* Fall through to deduce template name. */
23617 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23618 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23620 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23622 /* Simple cases: Value already set, does match or doesn't. */
23623 if (targ != NULL_TREE && template_args_equal (targ, arg))
23624 return unify_success (explain_p);
23625 else if (targ)
23626 return unify_inconsistency (explain_p, parm, targ, arg);
23628 else
23630 /* If PARM is `const T' and ARG is only `int', we don't have
23631 a match unless we are allowing additional qualification.
23632 If ARG is `const int' and PARM is just `T' that's OK;
23633 that binds `const int' to `T'. */
23634 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23635 arg, parm))
23636 return unify_cv_qual_mismatch (explain_p, parm, arg);
23638 /* Consider the case where ARG is `const volatile int' and
23639 PARM is `const T'. Then, T should be `volatile int'. */
23640 arg = cp_build_qualified_type_real
23641 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23642 if (arg == error_mark_node)
23643 return unify_invalid (explain_p);
23645 /* Simple cases: Value already set, does match or doesn't. */
23646 if (targ != NULL_TREE && same_type_p (targ, arg))
23647 return unify_success (explain_p);
23648 else if (targ)
23649 return unify_inconsistency (explain_p, parm, targ, arg);
23651 /* Make sure that ARG is not a variable-sized array. (Note
23652 that were talking about variable-sized arrays (like
23653 `int[n]'), rather than arrays of unknown size (like
23654 `int[]').) We'll get very confused by such a type since
23655 the bound of the array is not constant, and therefore
23656 not mangleable. Besides, such types are not allowed in
23657 ISO C++, so we can do as we please here. We do allow
23658 them for 'auto' deduction, since that isn't ABI-exposed. */
23659 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23660 return unify_vla_arg (explain_p, arg);
23662 /* Strip typedefs as in convert_template_argument. */
23663 arg = canonicalize_type_argument (arg, tf_none);
23666 /* If ARG is a parameter pack or an expansion, we cannot unify
23667 against it unless PARM is also a parameter pack. */
23668 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23669 && !template_parameter_pack_p (parm))
23670 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23672 /* If the argument deduction results is a METHOD_TYPE,
23673 then there is a problem.
23674 METHOD_TYPE doesn't map to any real C++ type the result of
23675 the deduction cannot be of that type. */
23676 if (TREE_CODE (arg) == METHOD_TYPE)
23677 return unify_method_type_error (explain_p, arg);
23679 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23680 return unify_success (explain_p);
23682 case TEMPLATE_PARM_INDEX:
23683 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23684 if (error_operand_p (tparm))
23685 return unify_invalid (explain_p);
23687 if (TEMPLATE_PARM_LEVEL (parm)
23688 != template_decl_level (tparm))
23690 /* The PARM is not one we're trying to unify. Just check
23691 to see if it matches ARG. */
23692 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23693 && cp_tree_equal (parm, arg));
23694 if (result)
23695 unify_expression_unequal (explain_p, parm, arg);
23696 return result;
23699 idx = TEMPLATE_PARM_IDX (parm);
23700 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23702 if (targ)
23704 if ((strict & UNIFY_ALLOW_INTEGER)
23705 && TREE_TYPE (targ) && TREE_TYPE (arg)
23706 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23707 /* We're deducing from an array bound, the type doesn't matter. */
23708 arg = fold_convert (TREE_TYPE (targ), arg);
23709 int x = !cp_tree_equal (targ, arg);
23710 if (x)
23711 unify_inconsistency (explain_p, parm, targ, arg);
23712 return x;
23715 /* [temp.deduct.type] If, in the declaration of a function template
23716 with a non-type template-parameter, the non-type
23717 template-parameter is used in an expression in the function
23718 parameter-list and, if the corresponding template-argument is
23719 deduced, the template-argument type shall match the type of the
23720 template-parameter exactly, except that a template-argument
23721 deduced from an array bound may be of any integral type.
23722 The non-type parameter might use already deduced type parameters. */
23723 tparm = TREE_TYPE (parm);
23724 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23725 /* We don't have enough levels of args to do any substitution. This
23726 can happen in the context of -fnew-ttp-matching. */;
23727 else
23729 ++processing_template_decl;
23730 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23731 --processing_template_decl;
23733 if (tree a = type_uses_auto (tparm))
23735 tparm = do_auto_deduction (tparm, arg, a,
23736 complain, adc_unify, targs);
23737 if (tparm == error_mark_node)
23738 return 1;
23742 if (!TREE_TYPE (arg))
23743 /* Template-parameter dependent expression. Just accept it for now.
23744 It will later be processed in convert_template_argument. */
23746 else if (same_type_ignoring_top_level_qualifiers_p
23747 (non_reference (TREE_TYPE (arg)),
23748 non_reference (tparm)))
23749 /* OK. Ignore top-level quals here because a class-type template
23750 parameter object is const. */;
23751 else if ((strict & UNIFY_ALLOW_INTEGER)
23752 && CP_INTEGRAL_TYPE_P (tparm))
23753 /* Convert the ARG to the type of PARM; the deduced non-type
23754 template argument must exactly match the types of the
23755 corresponding parameter. */
23756 arg = fold (build_nop (tparm, arg));
23757 else if (uses_template_parms (tparm))
23759 /* We haven't deduced the type of this parameter yet. */
23760 if (cxx_dialect >= cxx17
23761 /* We deduce from array bounds in try_array_deduction. */
23762 && !(strict & UNIFY_ALLOW_INTEGER)
23763 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23765 /* Deduce it from the non-type argument. */
23766 tree atype = TREE_TYPE (arg);
23767 RECUR_AND_CHECK_FAILURE (tparms, targs,
23768 tparm, atype,
23769 UNIFY_ALLOW_NONE, explain_p);
23770 /* Now check whether the type of this parameter is still
23771 dependent, and give up if so. */
23772 ++processing_template_decl;
23773 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23774 --processing_template_decl;
23775 if (uses_template_parms (tparm))
23776 return unify_success (explain_p);
23778 else
23779 /* Try again later. */
23780 return unify_success (explain_p);
23782 else
23783 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23785 /* If ARG is a parameter pack or an expansion, we cannot unify
23786 against it unless PARM is also a parameter pack. */
23787 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23788 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23789 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23792 bool removed_attr = false;
23793 arg = strip_typedefs_expr (arg, &removed_attr);
23795 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23796 return unify_success (explain_p);
23798 case PTRMEM_CST:
23800 /* A pointer-to-member constant can be unified only with
23801 another constant. */
23802 if (TREE_CODE (arg) != PTRMEM_CST)
23803 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23805 /* Just unify the class member. It would be useless (and possibly
23806 wrong, depending on the strict flags) to unify also
23807 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23808 arg refer to the same variable, even if through different
23809 classes. For instance:
23811 struct A { int x; };
23812 struct B : A { };
23814 Unification of &A::x and &B::x must succeed. */
23815 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23816 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23819 case POINTER_TYPE:
23821 if (!TYPE_PTR_P (arg))
23822 return unify_type_mismatch (explain_p, parm, arg);
23824 /* [temp.deduct.call]
23826 A can be another pointer or pointer to member type that can
23827 be converted to the deduced A via a qualification
23828 conversion (_conv.qual_).
23830 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23831 This will allow for additional cv-qualification of the
23832 pointed-to types if appropriate. */
23834 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23835 /* The derived-to-base conversion only persists through one
23836 level of pointers. */
23837 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23839 return unify (tparms, targs, TREE_TYPE (parm),
23840 TREE_TYPE (arg), strict, explain_p);
23843 case REFERENCE_TYPE:
23844 if (!TYPE_REF_P (arg))
23845 return unify_type_mismatch (explain_p, parm, arg);
23846 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23847 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23849 case ARRAY_TYPE:
23850 if (TREE_CODE (arg) != ARRAY_TYPE)
23851 return unify_type_mismatch (explain_p, parm, arg);
23852 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23853 != (TYPE_DOMAIN (arg) == NULL_TREE))
23854 return unify_type_mismatch (explain_p, parm, arg);
23855 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23856 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23857 if (TYPE_DOMAIN (parm) != NULL_TREE)
23858 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23859 TYPE_DOMAIN (arg), explain_p);
23860 return unify_success (explain_p);
23862 case REAL_TYPE:
23863 case COMPLEX_TYPE:
23864 case VECTOR_TYPE:
23865 case INTEGER_TYPE:
23866 case BOOLEAN_TYPE:
23867 case ENUMERAL_TYPE:
23868 case VOID_TYPE:
23869 case OPAQUE_TYPE:
23870 case NULLPTR_TYPE:
23871 if (TREE_CODE (arg) != TREE_CODE (parm))
23872 return unify_type_mismatch (explain_p, parm, arg);
23874 /* We have already checked cv-qualification at the top of the
23875 function. */
23876 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23877 return unify_type_mismatch (explain_p, parm, arg);
23879 /* As far as unification is concerned, this wins. Later checks
23880 will invalidate it if necessary. */
23881 return unify_success (explain_p);
23883 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23884 /* Type INTEGER_CST can come from ordinary constant template args. */
23885 case INTEGER_CST:
23886 while (CONVERT_EXPR_P (arg))
23887 arg = TREE_OPERAND (arg, 0);
23889 if (TREE_CODE (arg) != INTEGER_CST)
23890 return unify_template_argument_mismatch (explain_p, parm, arg);
23891 return (tree_int_cst_equal (parm, arg)
23892 ? unify_success (explain_p)
23893 : unify_template_argument_mismatch (explain_p, parm, arg));
23895 case TREE_VEC:
23897 int i, len, argslen;
23898 int parm_variadic_p = 0;
23900 if (TREE_CODE (arg) != TREE_VEC)
23901 return unify_template_argument_mismatch (explain_p, parm, arg);
23903 len = TREE_VEC_LENGTH (parm);
23904 argslen = TREE_VEC_LENGTH (arg);
23906 /* Check for pack expansions in the parameters. */
23907 for (i = 0; i < len; ++i)
23909 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23911 if (i == len - 1)
23912 /* We can unify against something with a trailing
23913 parameter pack. */
23914 parm_variadic_p = 1;
23915 else
23916 /* [temp.deduct.type]/9: If the template argument list of
23917 P contains a pack expansion that is not the last
23918 template argument, the entire template argument list
23919 is a non-deduced context. */
23920 return unify_success (explain_p);
23924 /* If we don't have enough arguments to satisfy the parameters
23925 (not counting the pack expression at the end), or we have
23926 too many arguments for a parameter list that doesn't end in
23927 a pack expression, we can't unify. */
23928 if (parm_variadic_p
23929 ? argslen < len - parm_variadic_p
23930 : argslen != len)
23931 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23933 /* Unify all of the parameters that precede the (optional)
23934 pack expression. */
23935 for (i = 0; i < len - parm_variadic_p; ++i)
23937 RECUR_AND_CHECK_FAILURE (tparms, targs,
23938 TREE_VEC_ELT (parm, i),
23939 TREE_VEC_ELT (arg, i),
23940 UNIFY_ALLOW_NONE, explain_p);
23942 if (parm_variadic_p)
23943 return unify_pack_expansion (tparms, targs, parm, arg,
23944 DEDUCE_EXACT,
23945 /*subr=*/true, explain_p);
23946 return unify_success (explain_p);
23949 case RECORD_TYPE:
23950 case UNION_TYPE:
23951 if (TREE_CODE (arg) != TREE_CODE (parm))
23952 return unify_type_mismatch (explain_p, parm, arg);
23954 if (TYPE_PTRMEMFUNC_P (parm))
23956 if (!TYPE_PTRMEMFUNC_P (arg))
23957 return unify_type_mismatch (explain_p, parm, arg);
23959 return unify (tparms, targs,
23960 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23961 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23962 strict, explain_p);
23964 else if (TYPE_PTRMEMFUNC_P (arg))
23965 return unify_type_mismatch (explain_p, parm, arg);
23967 if (CLASSTYPE_TEMPLATE_INFO (parm))
23969 tree t = NULL_TREE;
23971 if (strict_in & UNIFY_ALLOW_DERIVED)
23973 /* First, we try to unify the PARM and ARG directly. */
23974 t = try_class_unification (tparms, targs,
23975 parm, arg, explain_p);
23977 if (!t)
23979 /* Fallback to the special case allowed in
23980 [temp.deduct.call]:
23982 If P is a class, and P has the form
23983 template-id, then A can be a derived class of
23984 the deduced A. Likewise, if P is a pointer to
23985 a class of the form template-id, A can be a
23986 pointer to a derived class pointed to by the
23987 deduced A. */
23988 enum template_base_result r;
23989 r = get_template_base (tparms, targs, parm, arg,
23990 explain_p, &t);
23992 if (!t)
23994 /* Don't give the derived diagnostic if we're
23995 already dealing with the same template. */
23996 bool same_template
23997 = (CLASSTYPE_TEMPLATE_INFO (arg)
23998 && (CLASSTYPE_TI_TEMPLATE (parm)
23999 == CLASSTYPE_TI_TEMPLATE (arg)));
24000 return unify_no_common_base (explain_p && !same_template,
24001 r, parm, arg);
24005 else if (CLASSTYPE_TEMPLATE_INFO (arg)
24006 && (CLASSTYPE_TI_TEMPLATE (parm)
24007 == CLASSTYPE_TI_TEMPLATE (arg)))
24008 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24009 Then, we should unify `int' and `U'. */
24010 t = arg;
24011 else
24012 /* There's no chance of unification succeeding. */
24013 return unify_type_mismatch (explain_p, parm, arg);
24015 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
24016 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
24018 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
24019 return unify_type_mismatch (explain_p, parm, arg);
24020 return unify_success (explain_p);
24022 case METHOD_TYPE:
24023 case FUNCTION_TYPE:
24025 unsigned int nargs;
24026 tree *args;
24027 tree a;
24028 unsigned int i;
24030 if (TREE_CODE (arg) != TREE_CODE (parm))
24031 return unify_type_mismatch (explain_p, parm, arg);
24033 /* CV qualifications for methods can never be deduced, they must
24034 match exactly. We need to check them explicitly here,
24035 because type_unification_real treats them as any other
24036 cv-qualified parameter. */
24037 if (TREE_CODE (parm) == METHOD_TYPE
24038 && (!check_cv_quals_for_unify
24039 (UNIFY_ALLOW_NONE,
24040 class_of_this_parm (arg),
24041 class_of_this_parm (parm))))
24042 return unify_cv_qual_mismatch (explain_p, parm, arg);
24043 if (TREE_CODE (arg) == FUNCTION_TYPE
24044 && type_memfn_quals (parm) != type_memfn_quals (arg))
24045 return unify_cv_qual_mismatch (explain_p, parm, arg);
24046 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
24047 return unify_type_mismatch (explain_p, parm, arg);
24049 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
24050 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
24052 nargs = list_length (TYPE_ARG_TYPES (arg));
24053 args = XALLOCAVEC (tree, nargs);
24054 for (a = TYPE_ARG_TYPES (arg), i = 0;
24055 a != NULL_TREE && a != void_list_node;
24056 a = TREE_CHAIN (a), ++i)
24057 args[i] = TREE_VALUE (a);
24058 nargs = i;
24060 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
24061 args, nargs, 1, DEDUCE_EXACT,
24062 NULL, explain_p))
24063 return 1;
24065 if (flag_noexcept_type)
24067 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
24068 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
24069 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
24070 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
24071 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
24072 && uses_template_parms (TREE_PURPOSE (pspec)))
24073 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
24074 TREE_PURPOSE (aspec),
24075 UNIFY_ALLOW_NONE, explain_p);
24076 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
24077 return unify_type_mismatch (explain_p, parm, arg);
24080 return 0;
24083 case OFFSET_TYPE:
24084 /* Unify a pointer to member with a pointer to member function, which
24085 deduces the type of the member as a function type. */
24086 if (TYPE_PTRMEMFUNC_P (arg))
24088 /* Check top-level cv qualifiers */
24089 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
24090 return unify_cv_qual_mismatch (explain_p, parm, arg);
24092 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24093 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
24094 UNIFY_ALLOW_NONE, explain_p);
24096 /* Determine the type of the function we are unifying against. */
24097 tree fntype = static_fn_type (arg);
24099 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
24102 if (TREE_CODE (arg) != OFFSET_TYPE)
24103 return unify_type_mismatch (explain_p, parm, arg);
24104 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24105 TYPE_OFFSET_BASETYPE (arg),
24106 UNIFY_ALLOW_NONE, explain_p);
24107 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24108 strict, explain_p);
24110 case CONST_DECL:
24111 if (DECL_TEMPLATE_PARM_P (parm))
24112 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
24113 if (arg != scalar_constant_value (parm))
24114 return unify_template_argument_mismatch (explain_p, parm, arg);
24115 return unify_success (explain_p);
24117 case FIELD_DECL:
24118 case TEMPLATE_DECL:
24119 /* Matched cases are handled by the ARG == PARM test above. */
24120 return unify_template_argument_mismatch (explain_p, parm, arg);
24122 case VAR_DECL:
24123 /* We might get a variable as a non-type template argument in parm if the
24124 corresponding parameter is type-dependent. Make any necessary
24125 adjustments based on whether arg is a reference. */
24126 if (CONSTANT_CLASS_P (arg))
24127 parm = fold_non_dependent_expr (parm, complain);
24128 else if (REFERENCE_REF_P (arg))
24130 tree sub = TREE_OPERAND (arg, 0);
24131 STRIP_NOPS (sub);
24132 if (TREE_CODE (sub) == ADDR_EXPR)
24133 arg = TREE_OPERAND (sub, 0);
24135 /* Now use the normal expression code to check whether they match. */
24136 goto expr;
24138 case TYPE_ARGUMENT_PACK:
24139 case NONTYPE_ARGUMENT_PACK:
24140 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
24141 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
24143 case TYPEOF_TYPE:
24144 case DECLTYPE_TYPE:
24145 case UNDERLYING_TYPE:
24146 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24147 or UNDERLYING_TYPE nodes. */
24148 return unify_success (explain_p);
24150 case ERROR_MARK:
24151 /* Unification fails if we hit an error node. */
24152 return unify_invalid (explain_p);
24154 case INDIRECT_REF:
24155 if (REFERENCE_REF_P (parm))
24157 bool pexp = PACK_EXPANSION_P (arg);
24158 if (pexp)
24159 arg = PACK_EXPANSION_PATTERN (arg);
24160 if (REFERENCE_REF_P (arg))
24161 arg = TREE_OPERAND (arg, 0);
24162 if (pexp)
24163 arg = make_pack_expansion (arg, complain);
24164 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24165 strict, explain_p);
24167 /* FALLTHRU */
24169 default:
24170 /* An unresolved overload is a nondeduced context. */
24171 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24172 return unify_success (explain_p);
24173 gcc_assert (EXPR_P (parm)
24174 || COMPOUND_LITERAL_P (parm)
24175 || TREE_CODE (parm) == TRAIT_EXPR);
24176 expr:
24177 /* We must be looking at an expression. This can happen with
24178 something like:
24180 template <int I>
24181 void foo(S<I>, S<I + 2>);
24185 template<typename T>
24186 void foo(A<T, T{}>);
24188 This is a "non-deduced context":
24190 [deduct.type]
24192 The non-deduced contexts are:
24194 --A non-type template argument or an array bound in which
24195 a subexpression references a template parameter.
24197 In these cases, we assume deduction succeeded, but don't
24198 actually infer any unifications. */
24200 if (!uses_template_parms (parm)
24201 && !template_args_equal (parm, arg))
24202 return unify_expression_unequal (explain_p, parm, arg);
24203 else
24204 return unify_success (explain_p);
24207 #undef RECUR_AND_CHECK_FAILURE
24209 /* Note that DECL can be defined in this translation unit, if
24210 required. */
24212 static void
24213 mark_definable (tree decl)
24215 tree clone;
24216 DECL_NOT_REALLY_EXTERN (decl) = 1;
24217 FOR_EACH_CLONE (clone, decl)
24218 DECL_NOT_REALLY_EXTERN (clone) = 1;
24221 /* Called if RESULT is explicitly instantiated, or is a member of an
24222 explicitly instantiated class. */
24224 void
24225 mark_decl_instantiated (tree result, int extern_p)
24227 SET_DECL_EXPLICIT_INSTANTIATION (result);
24229 /* If this entity has already been written out, it's too late to
24230 make any modifications. */
24231 if (TREE_ASM_WRITTEN (result))
24232 return;
24234 /* consteval functions are never emitted. */
24235 if (TREE_CODE (result) == FUNCTION_DECL
24236 && DECL_IMMEDIATE_FUNCTION_P (result))
24237 return;
24239 /* For anonymous namespace we don't need to do anything. */
24240 if (decl_anon_ns_mem_p (result))
24242 gcc_assert (!TREE_PUBLIC (result));
24243 return;
24246 if (TREE_CODE (result) != FUNCTION_DECL)
24247 /* The TREE_PUBLIC flag for function declarations will have been
24248 set correctly by tsubst. */
24249 TREE_PUBLIC (result) = 1;
24251 /* This might have been set by an earlier implicit instantiation. */
24252 DECL_COMDAT (result) = 0;
24254 if (extern_p)
24256 DECL_EXTERNAL (result) = 1;
24257 DECL_NOT_REALLY_EXTERN (result) = 0;
24259 else
24261 mark_definable (result);
24262 mark_needed (result);
24263 /* Always make artificials weak. */
24264 if (DECL_ARTIFICIAL (result) && flag_weak)
24265 comdat_linkage (result);
24266 /* For WIN32 we also want to put explicit instantiations in
24267 linkonce sections. */
24268 else if (TREE_PUBLIC (result))
24269 maybe_make_one_only (result);
24270 if (TREE_CODE (result) == FUNCTION_DECL
24271 && DECL_TEMPLATE_INSTANTIATED (result))
24272 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24273 since start_preparsed_function wouldn't have if we had an earlier
24274 extern explicit instantiation. */
24275 DECL_EXTERNAL (result) = 0;
24278 /* If EXTERN_P, then this function will not be emitted -- unless
24279 followed by an explicit instantiation, at which point its linkage
24280 will be adjusted. If !EXTERN_P, then this function will be
24281 emitted here. In neither circumstance do we want
24282 import_export_decl to adjust the linkage. */
24283 DECL_INTERFACE_KNOWN (result) = 1;
24286 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24287 important template arguments. If any are missing, we check whether
24288 they're important by using error_mark_node for substituting into any
24289 args that were used for partial ordering (the ones between ARGS and END)
24290 and seeing if it bubbles up. */
24292 static bool
24293 check_undeduced_parms (tree targs, tree args, tree end)
24295 bool found = false;
24296 int i;
24297 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24298 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24300 found = true;
24301 TREE_VEC_ELT (targs, i) = error_mark_node;
24303 if (found)
24305 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24306 if (substed == error_mark_node)
24307 return true;
24309 return false;
24312 /* Given two function templates PAT1 and PAT2, return:
24314 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24315 -1 if PAT2 is more specialized than PAT1.
24316 0 if neither is more specialized.
24318 LEN indicates the number of parameters we should consider
24319 (defaulted parameters should not be considered).
24321 The 1998 std underspecified function template partial ordering, and
24322 DR214 addresses the issue. We take pairs of arguments, one from
24323 each of the templates, and deduce them against each other. One of
24324 the templates will be more specialized if all the *other*
24325 template's arguments deduce against its arguments and at least one
24326 of its arguments *does* *not* deduce against the other template's
24327 corresponding argument. Deduction is done as for class templates.
24328 The arguments used in deduction have reference and top level cv
24329 qualifiers removed. Iff both arguments were originally reference
24330 types *and* deduction succeeds in both directions, an lvalue reference
24331 wins against an rvalue reference and otherwise the template
24332 with the more cv-qualified argument wins for that pairing (if
24333 neither is more cv-qualified, they both are equal). Unlike regular
24334 deduction, after all the arguments have been deduced in this way,
24335 we do *not* verify the deduced template argument values can be
24336 substituted into non-deduced contexts.
24338 The logic can be a bit confusing here, because we look at deduce1 and
24339 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24340 can find template arguments for pat1 to make arg1 look like arg2, that
24341 means that arg2 is at least as specialized as arg1. */
24344 more_specialized_fn (tree pat1, tree pat2, int len)
24346 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24347 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24348 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24349 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24350 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24351 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24352 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24353 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24354 tree origs1, origs2;
24355 bool lose1 = false;
24356 bool lose2 = false;
24358 /* Remove the this parameter from non-static member functions. If
24359 one is a non-static member function and the other is not a static
24360 member function, remove the first parameter from that function
24361 also. This situation occurs for operator functions where we
24362 locate both a member function (with this pointer) and non-member
24363 operator (with explicit first operand). */
24364 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24366 len--; /* LEN is the number of significant arguments for DECL1 */
24367 args1 = TREE_CHAIN (args1);
24368 if (!DECL_STATIC_FUNCTION_P (decl2))
24369 args2 = TREE_CHAIN (args2);
24371 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24373 args2 = TREE_CHAIN (args2);
24374 if (!DECL_STATIC_FUNCTION_P (decl1))
24376 len--;
24377 args1 = TREE_CHAIN (args1);
24381 /* If only one is a conversion operator, they are unordered. */
24382 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24383 return 0;
24385 /* Consider the return type for a conversion function */
24386 if (DECL_CONV_FN_P (decl1))
24388 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24389 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24390 len++;
24393 processing_template_decl++;
24395 origs1 = args1;
24396 origs2 = args2;
24398 while (len--
24399 /* Stop when an ellipsis is seen. */
24400 && args1 != NULL_TREE && args2 != NULL_TREE)
24402 tree arg1 = TREE_VALUE (args1);
24403 tree arg2 = TREE_VALUE (args2);
24404 int deduce1, deduce2;
24405 int quals1 = -1;
24406 int quals2 = -1;
24407 int ref1 = 0;
24408 int ref2 = 0;
24410 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24411 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24413 /* When both arguments are pack expansions, we need only
24414 unify the patterns themselves. */
24415 arg1 = PACK_EXPANSION_PATTERN (arg1);
24416 arg2 = PACK_EXPANSION_PATTERN (arg2);
24418 /* This is the last comparison we need to do. */
24419 len = 0;
24422 if (TYPE_REF_P (arg1))
24424 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24425 arg1 = TREE_TYPE (arg1);
24426 quals1 = cp_type_quals (arg1);
24429 if (TYPE_REF_P (arg2))
24431 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24432 arg2 = TREE_TYPE (arg2);
24433 quals2 = cp_type_quals (arg2);
24436 arg1 = TYPE_MAIN_VARIANT (arg1);
24437 arg2 = TYPE_MAIN_VARIANT (arg2);
24439 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24441 int i, len2 = remaining_arguments (args2);
24442 tree parmvec = make_tree_vec (1);
24443 tree argvec = make_tree_vec (len2);
24444 tree ta = args2;
24446 /* Setup the parameter vector, which contains only ARG1. */
24447 TREE_VEC_ELT (parmvec, 0) = arg1;
24449 /* Setup the argument vector, which contains the remaining
24450 arguments. */
24451 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24452 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24454 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24455 argvec, DEDUCE_EXACT,
24456 /*subr=*/true, /*explain_p=*/false)
24457 == 0);
24459 /* We cannot deduce in the other direction, because ARG1 is
24460 a pack expansion but ARG2 is not. */
24461 deduce2 = 0;
24463 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24465 int i, len1 = remaining_arguments (args1);
24466 tree parmvec = make_tree_vec (1);
24467 tree argvec = make_tree_vec (len1);
24468 tree ta = args1;
24470 /* Setup the parameter vector, which contains only ARG1. */
24471 TREE_VEC_ELT (parmvec, 0) = arg2;
24473 /* Setup the argument vector, which contains the remaining
24474 arguments. */
24475 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24476 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24478 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24479 argvec, DEDUCE_EXACT,
24480 /*subr=*/true, /*explain_p=*/false)
24481 == 0);
24483 /* We cannot deduce in the other direction, because ARG2 is
24484 a pack expansion but ARG1 is not.*/
24485 deduce1 = 0;
24488 else
24490 /* The normal case, where neither argument is a pack
24491 expansion. */
24492 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24493 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24494 == 0);
24495 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24496 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24497 == 0);
24500 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24501 arg2, then arg2 is not as specialized as arg1. */
24502 if (!deduce1)
24503 lose2 = true;
24504 if (!deduce2)
24505 lose1 = true;
24507 /* "If, for a given type, deduction succeeds in both directions
24508 (i.e., the types are identical after the transformations above)
24509 and both P and A were reference types (before being replaced with
24510 the type referred to above):
24511 - if the type from the argument template was an lvalue reference and
24512 the type from the parameter template was not, the argument type is
24513 considered to be more specialized than the other; otherwise,
24514 - if the type from the argument template is more cv-qualified
24515 than the type from the parameter template (as described above),
24516 the argument type is considered to be more specialized than the other;
24517 otherwise,
24518 - neither type is more specialized than the other." */
24520 if (deduce1 && deduce2)
24522 if (ref1 && ref2 && ref1 != ref2)
24524 if (ref1 > ref2)
24525 lose1 = true;
24526 else
24527 lose2 = true;
24529 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24531 if ((quals1 & quals2) == quals2)
24532 lose2 = true;
24533 if ((quals1 & quals2) == quals1)
24534 lose1 = true;
24538 if (lose1 && lose2)
24539 /* We've failed to deduce something in either direction.
24540 These must be unordered. */
24541 break;
24543 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24544 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24545 /* We have already processed all of the arguments in our
24546 handing of the pack expansion type. */
24547 len = 0;
24549 args1 = TREE_CHAIN (args1);
24550 args2 = TREE_CHAIN (args2);
24553 /* "In most cases, all template parameters must have values in order for
24554 deduction to succeed, but for partial ordering purposes a template
24555 parameter may remain without a value provided it is not used in the
24556 types being used for partial ordering."
24558 Thus, if we are missing any of the targs1 we need to substitute into
24559 origs1, then pat2 is not as specialized as pat1. This can happen when
24560 there is a nondeduced context. */
24561 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24562 lose2 = true;
24563 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24564 lose1 = true;
24566 processing_template_decl--;
24568 /* If both deductions succeed, the partial ordering selects the more
24569 constrained template. */
24570 /* P2113: If the corresponding template-parameters of the
24571 template-parameter-lists are not equivalent ([temp.over.link]) or if
24572 the function parameters that positionally correspond between the two
24573 templates are not of the same type, neither template is more
24574 specialized than the other. */
24575 if (!lose1 && !lose2
24576 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24577 DECL_TEMPLATE_PARMS (pat2))
24578 && compparms (origs1, origs2))
24580 int winner = more_constrained (decl1, decl2);
24581 if (winner > 0)
24582 lose2 = true;
24583 else if (winner < 0)
24584 lose1 = true;
24587 /* All things being equal, if the next argument is a pack expansion
24588 for one function but not for the other, prefer the
24589 non-variadic function. FIXME this is bogus; see c++/41958. */
24590 if (lose1 == lose2
24591 && args1 && TREE_VALUE (args1)
24592 && args2 && TREE_VALUE (args2))
24594 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24595 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24598 if (lose1 == lose2)
24599 return 0;
24600 else if (!lose1)
24601 return 1;
24602 else
24603 return -1;
24606 /* Determine which of two partial specializations of TMPL is more
24607 specialized.
24609 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24610 to the first partial specialization. The TREE_PURPOSE is the
24611 innermost set of template parameters for the partial
24612 specialization. PAT2 is similar, but for the second template.
24614 Return 1 if the first partial specialization is more specialized;
24615 -1 if the second is more specialized; 0 if neither is more
24616 specialized.
24618 See [temp.class.order] for information about determining which of
24619 two templates is more specialized. */
24621 static int
24622 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24624 tree targs;
24625 int winner = 0;
24626 bool any_deductions = false;
24628 tree tmpl1 = TREE_VALUE (pat1);
24629 tree tmpl2 = TREE_VALUE (pat2);
24630 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24631 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24633 /* Just like what happens for functions, if we are ordering between
24634 different template specializations, we may encounter dependent
24635 types in the arguments, and we need our dependency check functions
24636 to behave correctly. */
24637 ++processing_template_decl;
24638 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24639 if (targs)
24641 --winner;
24642 any_deductions = true;
24645 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24646 if (targs)
24648 ++winner;
24649 any_deductions = true;
24651 --processing_template_decl;
24653 /* If both deductions succeed, the partial ordering selects the more
24654 constrained template. */
24655 if (!winner && any_deductions)
24656 winner = more_constrained (tmpl1, tmpl2);
24658 /* In the case of a tie where at least one of the templates
24659 has a parameter pack at the end, the template with the most
24660 non-packed parameters wins. */
24661 if (winner == 0
24662 && any_deductions
24663 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24664 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24666 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24667 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24668 int len1 = TREE_VEC_LENGTH (args1);
24669 int len2 = TREE_VEC_LENGTH (args2);
24671 /* We don't count the pack expansion at the end. */
24672 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24673 --len1;
24674 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24675 --len2;
24677 if (len1 > len2)
24678 return 1;
24679 else if (len1 < len2)
24680 return -1;
24683 return winner;
24686 /* Return the template arguments that will produce the function signature
24687 DECL from the function template FN, with the explicit template
24688 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24689 also match. Return NULL_TREE if no satisfactory arguments could be
24690 found. */
24692 static tree
24693 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24695 int ntparms = DECL_NTPARMS (fn);
24696 tree targs = make_tree_vec (ntparms);
24697 tree decl_type = TREE_TYPE (decl);
24698 tree decl_arg_types;
24699 tree *args;
24700 unsigned int nargs, ix;
24701 tree arg;
24703 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24705 /* Never do unification on the 'this' parameter. */
24706 decl_arg_types = skip_artificial_parms_for (decl,
24707 TYPE_ARG_TYPES (decl_type));
24709 nargs = list_length (decl_arg_types);
24710 args = XALLOCAVEC (tree, nargs);
24711 for (arg = decl_arg_types, ix = 0;
24712 arg != NULL_TREE && arg != void_list_node;
24713 arg = TREE_CHAIN (arg), ++ix)
24714 args[ix] = TREE_VALUE (arg);
24716 if (fn_type_unification (fn, explicit_args, targs,
24717 args, ix,
24718 (check_rettype || DECL_CONV_FN_P (fn)
24719 ? TREE_TYPE (decl_type) : NULL_TREE),
24720 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24721 /*explain_p=*/false,
24722 /*decltype*/false)
24723 == error_mark_node)
24724 return NULL_TREE;
24726 return targs;
24729 /* Return the innermost template arguments that, when applied to a partial
24730 specialization SPEC_TMPL of TMPL, yield the ARGS.
24732 For example, suppose we have:
24734 template <class T, class U> struct S {};
24735 template <class T> struct S<T*, int> {};
24737 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24738 partial specialization and the ARGS will be {double*, int}. The resulting
24739 vector will be {double}, indicating that `T' is bound to `double'. */
24741 static tree
24742 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24744 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24745 tree spec_args
24746 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24747 int i, ntparms = TREE_VEC_LENGTH (tparms);
24748 tree deduced_args;
24749 tree innermost_deduced_args;
24751 innermost_deduced_args = make_tree_vec (ntparms);
24752 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24754 deduced_args = copy_node (args);
24755 SET_TMPL_ARGS_LEVEL (deduced_args,
24756 TMPL_ARGS_DEPTH (deduced_args),
24757 innermost_deduced_args);
24759 else
24760 deduced_args = innermost_deduced_args;
24762 bool tried_array_deduction = (cxx_dialect < cxx17);
24763 again:
24764 if (unify (tparms, deduced_args,
24765 INNERMOST_TEMPLATE_ARGS (spec_args),
24766 INNERMOST_TEMPLATE_ARGS (args),
24767 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24768 return NULL_TREE;
24770 for (i = 0; i < ntparms; ++i)
24771 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24773 if (!tried_array_deduction)
24775 try_array_deduction (tparms, innermost_deduced_args,
24776 INNERMOST_TEMPLATE_ARGS (spec_args));
24777 tried_array_deduction = true;
24778 if (TREE_VEC_ELT (innermost_deduced_args, i))
24779 goto again;
24781 return NULL_TREE;
24784 if (!push_tinst_level (spec_tmpl, deduced_args))
24786 excessive_deduction_depth = true;
24787 return NULL_TREE;
24790 /* Verify that nondeduced template arguments agree with the type
24791 obtained from argument deduction.
24793 For example:
24795 struct A { typedef int X; };
24796 template <class T, class U> struct C {};
24797 template <class T> struct C<T, typename T::X> {};
24799 Then with the instantiation `C<A, int>', we can deduce that
24800 `T' is `A' but unify () does not check whether `typename T::X'
24801 is `int'. */
24802 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24804 if (spec_args != error_mark_node)
24805 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24806 INNERMOST_TEMPLATE_ARGS (spec_args),
24807 tmpl, tf_none, false, false);
24809 pop_tinst_level ();
24811 if (spec_args == error_mark_node
24812 /* We only need to check the innermost arguments; the other
24813 arguments will always agree. */
24814 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24815 INNERMOST_TEMPLATE_ARGS (args)))
24816 return NULL_TREE;
24818 /* Now that we have bindings for all of the template arguments,
24819 ensure that the arguments deduced for the template template
24820 parameters have compatible template parameter lists. See the use
24821 of template_template_parm_bindings_ok_p in fn_type_unification
24822 for more information. */
24823 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24824 return NULL_TREE;
24826 return deduced_args;
24829 // Compare two function templates T1 and T2 by deducing bindings
24830 // from one against the other. If both deductions succeed, compare
24831 // constraints to see which is more constrained.
24832 static int
24833 more_specialized_inst (tree t1, tree t2)
24835 int fate = 0;
24836 int count = 0;
24838 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24840 --fate;
24841 ++count;
24844 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24846 ++fate;
24847 ++count;
24850 // If both deductions succeed, then one may be more constrained.
24851 if (count == 2 && fate == 0)
24852 fate = more_constrained (t1, t2);
24854 return fate;
24857 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24858 Return the TREE_LIST node with the most specialized template, if
24859 any. If there is no most specialized template, the error_mark_node
24860 is returned.
24862 Note that this function does not look at, or modify, the
24863 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24864 returned is one of the elements of INSTANTIATIONS, callers may
24865 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24866 and retrieve it from the value returned. */
24868 tree
24869 most_specialized_instantiation (tree templates)
24871 tree fn, champ;
24873 ++processing_template_decl;
24875 champ = templates;
24876 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24878 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24879 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24880 if (fate == -1)
24881 champ = fn;
24882 else if (!fate)
24884 /* Equally specialized, move to next function. If there
24885 is no next function, nothing's most specialized. */
24886 fn = TREE_CHAIN (fn);
24887 champ = fn;
24888 if (!fn)
24889 break;
24893 if (champ)
24894 /* Now verify that champ is better than everything earlier in the
24895 instantiation list. */
24896 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24897 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24899 champ = NULL_TREE;
24900 break;
24904 processing_template_decl--;
24906 if (!champ)
24907 return error_mark_node;
24909 return champ;
24912 /* If DECL is a specialization of some template, return the most
24913 general such template. Otherwise, returns NULL_TREE.
24915 For example, given:
24917 template <class T> struct S { template <class U> void f(U); };
24919 if TMPL is `template <class U> void S<int>::f(U)' this will return
24920 the full template. This function will not trace past partial
24921 specializations, however. For example, given in addition:
24923 template <class T> struct S<T*> { template <class U> void f(U); };
24925 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24926 `template <class T> template <class U> S<T*>::f(U)'. */
24928 tree
24929 most_general_template (tree decl)
24931 if (TREE_CODE (decl) != TEMPLATE_DECL)
24933 if (tree tinfo = get_template_info (decl))
24934 decl = TI_TEMPLATE (tinfo);
24935 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24936 template friend, or a FIELD_DECL for a capture pack. */
24937 if (TREE_CODE (decl) != TEMPLATE_DECL)
24938 return NULL_TREE;
24941 /* Look for more and more general templates. */
24942 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24944 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24945 (See cp-tree.h for details.) */
24946 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24947 break;
24949 if (CLASS_TYPE_P (TREE_TYPE (decl))
24950 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24951 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24952 break;
24954 /* Stop if we run into an explicitly specialized class template. */
24955 if (!DECL_NAMESPACE_SCOPE_P (decl)
24956 && DECL_CONTEXT (decl)
24957 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24958 break;
24960 decl = DECL_TI_TEMPLATE (decl);
24963 return decl;
24966 /* Return the most specialized of the template partial specializations
24967 which can produce TARGET, a specialization of some class or variable
24968 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24969 a TEMPLATE_DECL node corresponding to the partial specialization, while
24970 the TREE_PURPOSE is the set of template arguments that must be
24971 substituted into the template pattern in order to generate TARGET.
24973 If the choice of partial specialization is ambiguous, a diagnostic
24974 is issued, and the error_mark_node is returned. If there are no
24975 partial specializations matching TARGET, then NULL_TREE is
24976 returned, indicating that the primary template should be used. */
24978 tree
24979 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24981 tree list = NULL_TREE;
24982 tree t;
24983 tree champ;
24984 int fate;
24985 bool ambiguous_p;
24986 tree outer_args = NULL_TREE;
24987 tree tmpl, args;
24989 tree decl;
24990 if (TYPE_P (target))
24992 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24993 tmpl = TI_TEMPLATE (tinfo);
24994 args = TI_ARGS (tinfo);
24995 decl = TYPE_NAME (target);
24997 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24999 tmpl = TREE_OPERAND (target, 0);
25000 args = TREE_OPERAND (target, 1);
25001 decl = DECL_TEMPLATE_RESULT (tmpl);
25003 else if (VAR_P (target))
25005 tree tinfo = DECL_TEMPLATE_INFO (target);
25006 tmpl = TI_TEMPLATE (tinfo);
25007 args = TI_ARGS (tinfo);
25008 decl = target;
25010 else
25011 gcc_unreachable ();
25013 push_access_scope_guard pas (decl);
25014 deferring_access_check_sentinel acs (dk_no_deferred);
25016 tree main_tmpl = most_general_template (tmpl);
25018 /* For determining which partial specialization to use, only the
25019 innermost args are interesting. */
25020 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25022 outer_args = strip_innermost_template_args (args, 1);
25023 args = INNERMOST_TEMPLATE_ARGS (args);
25026 /* The caller hasn't called push_to_top_level yet, but we need
25027 get_partial_spec_bindings to be done in non-template context so that we'll
25028 fully resolve everything. */
25029 processing_template_decl_sentinel ptds;
25031 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
25033 const tree ospec_tmpl = TREE_VALUE (t);
25035 tree spec_tmpl;
25036 if (outer_args)
25038 /* Substitute in the template args from the enclosing class. */
25039 ++processing_template_decl;
25040 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
25041 --processing_template_decl;
25042 if (spec_tmpl == error_mark_node)
25043 return error_mark_node;
25045 else
25046 spec_tmpl = ospec_tmpl;
25048 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
25049 if (spec_args)
25051 if (outer_args)
25052 spec_args = add_to_template_args (outer_args, spec_args);
25054 /* Keep the candidate only if the constraints are satisfied,
25055 or if we're not compiling with concepts. */
25056 if (!flag_concepts
25057 || constraints_satisfied_p (ospec_tmpl, spec_args))
25059 list = tree_cons (spec_args, ospec_tmpl, list);
25060 TREE_TYPE (list) = TREE_TYPE (t);
25065 if (! list)
25066 return NULL_TREE;
25068 ambiguous_p = false;
25069 t = list;
25070 champ = t;
25071 t = TREE_CHAIN (t);
25072 for (; t; t = TREE_CHAIN (t))
25074 fate = more_specialized_partial_spec (tmpl, champ, t);
25075 if (fate == 1)
25077 else
25079 if (fate == 0)
25081 t = TREE_CHAIN (t);
25082 if (! t)
25084 ambiguous_p = true;
25085 break;
25088 champ = t;
25092 if (!ambiguous_p)
25093 for (t = list; t && t != champ; t = TREE_CHAIN (t))
25095 fate = more_specialized_partial_spec (tmpl, champ, t);
25096 if (fate != 1)
25098 ambiguous_p = true;
25099 break;
25103 if (ambiguous_p)
25105 const char *str;
25106 char *spaces = NULL;
25107 if (!(complain & tf_error))
25108 return error_mark_node;
25109 if (TYPE_P (target))
25110 error ("ambiguous template instantiation for %q#T", target);
25111 else
25112 error ("ambiguous template instantiation for %q#D", target);
25113 str = ngettext ("candidate is:", "candidates are:", list_length (list));
25114 for (t = list; t; t = TREE_CHAIN (t))
25116 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
25117 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
25118 "%s %#qS", spaces ? spaces : str, subst);
25119 spaces = spaces ? spaces : get_spaces (str);
25121 free (spaces);
25122 return error_mark_node;
25125 return champ;
25128 /* Explicitly instantiate DECL. */
25130 void
25131 do_decl_instantiation (tree decl, tree storage)
25133 tree result = NULL_TREE;
25134 int extern_p = 0;
25136 if (!decl || decl == error_mark_node)
25137 /* An error occurred, for which grokdeclarator has already issued
25138 an appropriate message. */
25139 return;
25140 else if (! DECL_LANG_SPECIFIC (decl))
25142 error ("explicit instantiation of non-template %q#D", decl);
25143 return;
25145 else if (DECL_DECLARED_CONCEPT_P (decl))
25147 if (VAR_P (decl))
25148 error ("explicit instantiation of variable concept %q#D", decl);
25149 else
25150 error ("explicit instantiation of function concept %q#D", decl);
25151 return;
25154 bool var_templ = (DECL_TEMPLATE_INFO (decl)
25155 && variable_template_p (DECL_TI_TEMPLATE (decl)));
25157 if (VAR_P (decl) && !var_templ)
25159 /* There is an asymmetry here in the way VAR_DECLs and
25160 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25161 the latter, the DECL we get back will be marked as a
25162 template instantiation, and the appropriate
25163 DECL_TEMPLATE_INFO will be set up. This does not happen for
25164 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25165 should handle VAR_DECLs as it currently handles
25166 FUNCTION_DECLs. */
25167 if (!DECL_CLASS_SCOPE_P (decl))
25169 error ("%qD is not a static data member of a class template", decl);
25170 return;
25172 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25173 if (!result || !VAR_P (result))
25175 error ("no matching template for %qD found", decl);
25176 return;
25178 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25180 error ("type %qT for explicit instantiation %qD does not match "
25181 "declared type %qT", TREE_TYPE (result), decl,
25182 TREE_TYPE (decl));
25183 return;
25186 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25188 error ("explicit instantiation of %q#D", decl);
25189 return;
25191 else
25192 result = decl;
25194 /* Check for various error cases. Note that if the explicit
25195 instantiation is valid the RESULT will currently be marked as an
25196 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25197 until we get here. */
25199 if (DECL_TEMPLATE_SPECIALIZATION (result))
25201 /* DR 259 [temp.spec].
25203 Both an explicit instantiation and a declaration of an explicit
25204 specialization shall not appear in a program unless the explicit
25205 instantiation follows a declaration of the explicit specialization.
25207 For a given set of template parameters, if an explicit
25208 instantiation of a template appears after a declaration of an
25209 explicit specialization for that template, the explicit
25210 instantiation has no effect. */
25211 return;
25213 else if (DECL_EXPLICIT_INSTANTIATION (result))
25215 /* [temp.spec]
25217 No program shall explicitly instantiate any template more
25218 than once.
25220 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25221 the first instantiation was `extern' and the second is not,
25222 and EXTERN_P for the opposite case. */
25223 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25224 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25225 /* If an "extern" explicit instantiation follows an ordinary
25226 explicit instantiation, the template is instantiated. */
25227 if (extern_p)
25228 return;
25230 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25232 error ("no matching template for %qD found", result);
25233 return;
25235 else if (!DECL_TEMPLATE_INFO (result))
25237 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25238 return;
25241 if (storage == NULL_TREE)
25243 else if (storage == ridpointers[(int) RID_EXTERN])
25245 if (cxx_dialect == cxx98)
25246 pedwarn (input_location, OPT_Wpedantic,
25247 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25248 "instantiations");
25249 extern_p = 1;
25251 else
25252 error ("storage class %qD applied to template instantiation", storage);
25254 check_explicit_instantiation_namespace (result);
25255 mark_decl_instantiated (result, extern_p);
25256 if (! extern_p)
25257 instantiate_decl (result, /*defer_ok=*/true,
25258 /*expl_inst_class_mem_p=*/false);
25261 static void
25262 mark_class_instantiated (tree t, int extern_p)
25264 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25265 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25266 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25267 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25268 if (! extern_p)
25270 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25271 rest_of_type_compilation (t, 1);
25275 /* Perform an explicit instantiation of template class T. STORAGE, if
25276 non-null, is the RID for extern, inline or static. COMPLAIN is
25277 nonzero if this is called from the parser, zero if called recursively,
25278 since the standard is unclear (as detailed below). */
25280 void
25281 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25283 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25285 if (tree ti = TYPE_TEMPLATE_INFO (t))
25286 error ("explicit instantiation of non-class template %qD",
25287 TI_TEMPLATE (ti));
25288 else
25289 error ("explicit instantiation of non-template type %qT", t);
25290 return;
25293 complete_type (t);
25295 if (!COMPLETE_TYPE_P (t))
25297 if (complain & tf_error)
25298 error ("explicit instantiation of %q#T before definition of template",
25300 return;
25303 /* At most one of these will be true. */
25304 bool extern_p = false;
25305 bool nomem_p = false;
25306 bool static_p = false;
25308 if (storage != NULL_TREE)
25310 if (storage == ridpointers[(int) RID_EXTERN])
25312 if (cxx_dialect == cxx98)
25313 pedwarn (input_location, OPT_Wpedantic,
25314 "ISO C++ 1998 forbids the use of %<extern%> on "
25315 "explicit instantiations");
25317 else
25318 pedwarn (input_location, OPT_Wpedantic,
25319 "ISO C++ forbids the use of %qE"
25320 " on explicit instantiations", storage);
25322 if (storage == ridpointers[(int) RID_INLINE])
25323 nomem_p = true;
25324 else if (storage == ridpointers[(int) RID_EXTERN])
25325 extern_p = true;
25326 else if (storage == ridpointers[(int) RID_STATIC])
25327 static_p = true;
25328 else
25329 error ("storage class %qD applied to template instantiation",
25330 storage);
25333 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25334 /* DR 259 [temp.spec].
25336 Both an explicit instantiation and a declaration of an explicit
25337 specialization shall not appear in a program unless the
25338 explicit instantiation follows a declaration of the explicit
25339 specialization.
25341 For a given set of template parameters, if an explicit
25342 instantiation of a template appears after a declaration of an
25343 explicit specialization for that template, the explicit
25344 instantiation has no effect. */
25345 return;
25347 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25349 /* We've already instantiated the template. */
25351 /* [temp.spec]
25353 No program shall explicitly instantiate any template more
25354 than once.
25356 If EXTERN_P then this is ok. */
25357 if (!extern_p && (complain & tf_error))
25358 permerror (input_location,
25359 "duplicate explicit instantiation of %q#T", t);
25361 return;
25364 check_explicit_instantiation_namespace (TYPE_NAME (t));
25365 mark_class_instantiated (t, extern_p);
25367 if (nomem_p)
25368 return;
25370 /* In contrast to implicit instantiation, where only the
25371 declarations, and not the definitions, of members are
25372 instantiated, we have here:
25374 [temp.explicit]
25376 An explicit instantiation that names a class template
25377 specialization is also an explicit instantiation of the same
25378 kind (declaration or definition) of each of its members (not
25379 including members inherited from base classes and members
25380 that are templates) that has not been previously explicitly
25381 specialized in the translation unit containing the explicit
25382 instantiation, provided that the associated constraints, if
25383 any, of that member are satisfied by the template arguments
25384 of the explicit instantiation. */
25385 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25386 if ((VAR_P (fld)
25387 || (TREE_CODE (fld) == FUNCTION_DECL
25388 && !static_p
25389 && user_provided_p (fld)))
25390 && DECL_TEMPLATE_INSTANTIATION (fld)
25391 && constraints_satisfied_p (fld))
25393 mark_decl_instantiated (fld, extern_p);
25394 if (! extern_p)
25395 instantiate_decl (fld, /*defer_ok=*/true,
25396 /*expl_inst_class_mem_p=*/true);
25398 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25400 tree type = TREE_TYPE (fld);
25402 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25403 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25404 do_type_instantiation (type, storage, 0);
25408 /* Given a function DECL, which is a specialization of TMPL, modify
25409 DECL to be a re-instantiation of TMPL with the same template
25410 arguments. TMPL should be the template into which tsubst'ing
25411 should occur for DECL, not the most general template.
25413 One reason for doing this is a scenario like this:
25415 template <class T>
25416 void f(const T&, int i);
25418 void g() { f(3, 7); }
25420 template <class T>
25421 void f(const T& t, const int i) { }
25423 Note that when the template is first instantiated, with
25424 instantiate_template, the resulting DECL will have no name for the
25425 first parameter, and the wrong type for the second. So, when we go
25426 to instantiate the DECL, we regenerate it. */
25428 static void
25429 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25431 /* The arguments used to instantiate DECL, from the most general
25432 template. */
25433 tree code_pattern;
25435 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25437 /* Make sure that we can see identifiers, and compute access
25438 correctly. */
25439 push_access_scope (decl);
25441 if (TREE_CODE (decl) == FUNCTION_DECL)
25443 tree specs;
25444 int args_depth;
25445 int parms_depth;
25447 /* Use the source location of the definition. */
25448 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
25450 args_depth = TMPL_ARGS_DEPTH (args);
25451 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25452 if (args_depth > parms_depth)
25453 args = get_innermost_template_args (args, parms_depth);
25455 /* Instantiate a dynamic exception-specification. noexcept will be
25456 handled below. */
25457 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25458 if (TREE_VALUE (raises))
25460 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25461 args, tf_error, NULL_TREE,
25462 /*defer_ok*/false);
25463 if (specs && specs != error_mark_node)
25464 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25465 specs);
25468 /* Merge parameter declarations. */
25469 if (tree pattern_parm
25470 = skip_artificial_parms_for (code_pattern,
25471 DECL_ARGUMENTS (code_pattern)))
25473 tree *p = &DECL_ARGUMENTS (decl);
25474 for (int skip = num_artificial_parms_for (decl); skip; --skip)
25475 p = &DECL_CHAIN (*p);
25476 *p = tsubst_decl (pattern_parm, args, tf_error);
25477 for (tree t = *p; t; t = DECL_CHAIN (t))
25478 DECL_CONTEXT (t) = decl;
25481 /* Merge additional specifiers from the CODE_PATTERN. */
25482 if (DECL_DECLARED_INLINE_P (code_pattern)
25483 && !DECL_DECLARED_INLINE_P (decl))
25484 DECL_DECLARED_INLINE_P (decl) = 1;
25486 maybe_instantiate_noexcept (decl, tf_error);
25488 else if (VAR_P (decl))
25490 start_lambda_scope (decl);
25491 DECL_INITIAL (decl) =
25492 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25493 tf_error, DECL_TI_TEMPLATE (decl));
25494 finish_lambda_scope ();
25495 if (VAR_HAD_UNKNOWN_BOUND (decl))
25496 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25497 tf_error, DECL_TI_TEMPLATE (decl));
25499 else
25500 gcc_unreachable ();
25502 pop_access_scope (decl);
25505 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25506 substituted to get DECL. */
25508 tree
25509 template_for_substitution (tree decl)
25511 tree tmpl = DECL_TI_TEMPLATE (decl);
25513 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25514 for the instantiation. This is not always the most general
25515 template. Consider, for example:
25517 template <class T>
25518 struct S { template <class U> void f();
25519 template <> void f<int>(); };
25521 and an instantiation of S<double>::f<int>. We want TD to be the
25522 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25523 while (/* An instantiation cannot have a definition, so we need a
25524 more general template. */
25525 DECL_TEMPLATE_INSTANTIATION (tmpl)
25526 /* We must also deal with friend templates. Given:
25528 template <class T> struct S {
25529 template <class U> friend void f() {};
25532 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25533 so far as the language is concerned, but that's still
25534 where we get the pattern for the instantiation from. On
25535 other hand, if the definition comes outside the class, say:
25537 template <class T> struct S {
25538 template <class U> friend void f();
25540 template <class U> friend void f() {}
25542 we don't need to look any further. That's what the check for
25543 DECL_INITIAL is for. */
25544 || (TREE_CODE (decl) == FUNCTION_DECL
25545 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25546 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25548 /* The present template, TD, should not be a definition. If it
25549 were a definition, we should be using it! Note that we
25550 cannot restructure the loop to just keep going until we find
25551 a template with a definition, since that might go too far if
25552 a specialization was declared, but not defined. */
25554 /* Fetch the more general template. */
25555 tmpl = DECL_TI_TEMPLATE (tmpl);
25558 return tmpl;
25561 /* Returns true if we need to instantiate this template instance even if we
25562 know we aren't going to emit it. */
25564 bool
25565 always_instantiate_p (tree decl)
25567 /* We always instantiate inline functions so that we can inline them. An
25568 explicit instantiation declaration prohibits implicit instantiation of
25569 non-inline functions. With high levels of optimization, we would
25570 normally inline non-inline functions -- but we're not allowed to do
25571 that for "extern template" functions. Therefore, we check
25572 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25573 return ((TREE_CODE (decl) == FUNCTION_DECL
25574 && (DECL_DECLARED_INLINE_P (decl)
25575 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25576 /* And we need to instantiate static data members so that
25577 their initializers are available in integral constant
25578 expressions. */
25579 || (VAR_P (decl)
25580 && decl_maybe_constant_var_p (decl)));
25583 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25584 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25585 error, true otherwise. */
25587 bool
25588 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25590 if (fn == error_mark_node)
25591 return false;
25593 /* Don't instantiate a noexcept-specification from template context. */
25594 if (processing_template_decl
25595 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25596 return true;
25598 if (DECL_MAYBE_DELETED (fn))
25600 if (fn == current_function_decl)
25601 /* We're in start_preparsed_function, keep going. */
25602 return true;
25604 ++function_depth;
25605 synthesize_method (fn);
25606 --function_depth;
25607 return !DECL_MAYBE_DELETED (fn);
25610 tree fntype = TREE_TYPE (fn);
25611 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
25613 if (!spec || !TREE_PURPOSE (spec))
25614 return true;
25616 tree noex = TREE_PURPOSE (spec);
25617 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25618 && TREE_CODE (noex) != DEFERRED_PARSE)
25619 return true;
25621 tree orig_fn = NULL_TREE;
25622 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25623 its FUNCTION_DECL for the rest of this function -- push_access_scope
25624 doesn't accept TEMPLATE_DECLs. */
25625 if (DECL_FUNCTION_TEMPLATE_P (fn))
25627 orig_fn = fn;
25628 fn = DECL_TEMPLATE_RESULT (fn);
25631 if (DECL_CLONED_FUNCTION_P (fn))
25633 tree prime = DECL_CLONED_FUNCTION (fn);
25634 if (!maybe_instantiate_noexcept (prime, complain))
25635 return false;
25636 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25638 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25640 static hash_set<tree>* fns = new hash_set<tree>;
25641 bool added = false;
25642 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25644 spec = get_defaulted_eh_spec (fn, complain);
25645 if (spec == error_mark_node)
25646 /* This might have failed because of an unparsed DMI, so
25647 let's try again later. */
25648 return false;
25650 else if (!(added = !fns->add (fn)))
25652 /* If hash_set::add returns true, the element was already there. */
25653 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25654 DECL_SOURCE_LOCATION (fn));
25655 error_at (loc,
25656 "exception specification of %qD depends on itself",
25657 fn);
25658 spec = noexcept_false_spec;
25660 else if (push_tinst_level (fn))
25662 push_to_top_level ();
25663 push_access_scope (fn);
25664 push_deferring_access_checks (dk_no_deferred);
25665 input_location = DECL_SOURCE_LOCATION (fn);
25667 if (!DECL_LOCAL_DECL_P (fn))
25669 /* If needed, set current_class_ptr for the benefit of
25670 tsubst_copy/PARM_DECL. The exception pattern will
25671 refer to the parm of the template, not the
25672 instantiation. */
25673 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25674 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25676 tree this_parm = DECL_ARGUMENTS (tdecl);
25677 current_class_ptr = NULL_TREE;
25678 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25679 current_class_ptr = this_parm;
25683 /* If this function is represented by a TEMPLATE_DECL, then
25684 the deferred noexcept-specification might still contain
25685 dependent types, even after substitution. And we need the
25686 dependency check functions to work in build_noexcept_spec. */
25687 if (orig_fn)
25688 ++processing_template_decl;
25690 /* Do deferred instantiation of the noexcept-specifier. */
25691 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25692 DEFERRED_NOEXCEPT_ARGS (noex),
25693 tf_warning_or_error, fn,
25694 /*function_p=*/false,
25695 /*i_c_e_p=*/true);
25697 /* Build up the noexcept-specification. */
25698 spec = build_noexcept_spec (noex, tf_warning_or_error);
25700 if (orig_fn)
25701 --processing_template_decl;
25703 pop_deferring_access_checks ();
25704 pop_access_scope (fn);
25705 pop_tinst_level ();
25706 pop_from_top_level ();
25708 else
25709 spec = noexcept_false_spec;
25711 if (added)
25712 fns->remove (fn);
25715 if (spec == error_mark_node)
25717 /* This failed with a hard error, so let's go with false. */
25718 gcc_assert (seen_error ());
25719 spec = noexcept_false_spec;
25722 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25723 if (orig_fn)
25724 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25726 return true;
25729 /* We're starting to process the function INST, an instantiation of PATTERN;
25730 add their parameters to local_specializations. */
25732 static void
25733 register_parameter_specializations (tree pattern, tree inst)
25735 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25736 tree spec_parm = DECL_ARGUMENTS (inst);
25737 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25739 register_local_specialization (spec_parm, tmpl_parm);
25740 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25741 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25743 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25745 if (!DECL_PACK_P (tmpl_parm))
25747 register_local_specialization (spec_parm, tmpl_parm);
25748 spec_parm = DECL_CHAIN (spec_parm);
25750 else
25752 /* Register the (value) argument pack as a specialization of
25753 TMPL_PARM, then move on. */
25754 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25755 register_local_specialization (argpack, tmpl_parm);
25758 gcc_assert (!spec_parm);
25761 /* Instantiate the body of D using PATTERN with ARGS. We have
25762 already determined PATTERN is the correct template to use.
25763 NESTED_P is true if this is a nested function, in which case
25764 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25766 static void
25767 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25769 tree td = NULL_TREE;
25770 tree code_pattern = pattern;
25772 if (!nested_p)
25774 td = pattern;
25775 code_pattern = DECL_TEMPLATE_RESULT (td);
25777 else
25778 /* Only OMP reductions are nested. */
25779 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25781 vec<tree> omp_privatization_save;
25782 if (current_function_decl)
25783 save_omp_privatization_clauses (omp_privatization_save);
25785 bool push_to_top
25786 = !(current_function_decl
25787 && !LAMBDA_FUNCTION_P (d)
25788 && decl_function_context (d) == current_function_decl);
25790 if (push_to_top)
25791 push_to_top_level ();
25792 else
25794 gcc_assert (!processing_template_decl);
25795 push_function_context ();
25796 cp_unevaluated_operand = 0;
25797 c_inhibit_evaluation_warnings = 0;
25800 if (VAR_P (d))
25802 /* The variable might be a lambda's extra scope, and that
25803 lambda's visibility depends on D's. */
25804 maybe_commonize_var (d);
25805 determine_visibility (d);
25808 /* Mark D as instantiated so that recursive calls to
25809 instantiate_decl do not try to instantiate it again. */
25810 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25812 if (td)
25813 /* Regenerate the declaration in case the template has been modified
25814 by a subsequent redeclaration. */
25815 regenerate_decl_from_template (d, td, args);
25817 /* We already set the file and line above. Reset them now in case
25818 they changed as a result of calling regenerate_decl_from_template. */
25819 input_location = DECL_SOURCE_LOCATION (d);
25821 if (VAR_P (d))
25823 /* Clear out DECL_RTL; whatever was there before may not be right
25824 since we've reset the type of the declaration. */
25825 SET_DECL_RTL (d, NULL);
25826 DECL_IN_AGGR_P (d) = 0;
25828 /* The initializer is placed in DECL_INITIAL by
25829 regenerate_decl_from_template so we don't need to
25830 push/pop_access_scope again here. Pull it out so that
25831 cp_finish_decl can process it. */
25832 bool const_init = false;
25833 tree init = DECL_INITIAL (d);
25834 DECL_INITIAL (d) = NULL_TREE;
25835 DECL_INITIALIZED_P (d) = 0;
25837 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25838 initializer. That function will defer actual emission until
25839 we have a chance to determine linkage. */
25840 DECL_EXTERNAL (d) = 0;
25842 /* Enter the scope of D so that access-checking works correctly. */
25843 bool enter_context = DECL_CLASS_SCOPE_P (d);
25844 if (enter_context)
25845 push_nested_class (DECL_CONTEXT (d));
25847 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25848 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25850 if (enter_context)
25851 pop_nested_class ();
25853 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25854 synthesize_method (d);
25855 else if (TREE_CODE (d) == FUNCTION_DECL)
25857 /* Set up the list of local specializations. */
25858 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25859 tree block = NULL_TREE;
25861 /* Set up context. */
25862 if (nested_p)
25863 block = push_stmt_list ();
25864 else
25866 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25868 perform_instantiation_time_access_checks (code_pattern, args);
25871 /* Create substitution entries for the parameters. */
25872 register_parameter_specializations (code_pattern, d);
25874 /* Substitute into the body of the function. */
25875 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25876 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25877 tf_warning_or_error, d);
25878 else
25880 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25881 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25882 /*integral_constant_expression_p=*/false);
25884 /* Set the current input_location to the end of the function
25885 so that finish_function knows where we are. */
25886 input_location
25887 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25889 /* Remember if we saw an infinite loop in the template. */
25890 current_function_infinite_loop
25891 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25894 /* Finish the function. */
25895 if (nested_p)
25896 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25897 else
25899 d = finish_function (/*inline_p=*/false);
25900 expand_or_defer_fn (d);
25903 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25904 cp_check_omp_declare_reduction (d);
25907 /* We're not deferring instantiation any more. */
25908 if (!nested_p)
25909 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25911 if (push_to_top)
25912 pop_from_top_level ();
25913 else
25914 pop_function_context ();
25916 if (current_function_decl)
25917 restore_omp_privatization_clauses (omp_privatization_save);
25920 /* Produce the definition of D, a _DECL generated from a template. If
25921 DEFER_OK is true, then we don't have to actually do the
25922 instantiation now; we just have to do it sometime. Normally it is
25923 an error if this is an explicit instantiation but D is undefined.
25924 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25925 instantiated class template. */
25927 tree
25928 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25930 tree tmpl = DECL_TI_TEMPLATE (d);
25931 tree gen_args;
25932 tree args;
25933 tree td;
25934 tree code_pattern;
25935 tree spec;
25936 tree gen_tmpl;
25937 bool pattern_defined;
25938 location_t saved_loc = input_location;
25939 int saved_unevaluated_operand = cp_unevaluated_operand;
25940 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25941 bool external_p;
25942 bool deleted_p;
25944 /* This function should only be used to instantiate templates for
25945 functions and static member variables. */
25946 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25948 /* A concept is never instantiated. */
25949 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25951 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25953 if (modules_p ())
25954 /* We may have a pending instantiation of D itself. */
25955 lazy_load_pendings (d);
25957 /* Variables are never deferred; if instantiation is required, they
25958 are instantiated right away. That allows for better code in the
25959 case that an expression refers to the value of the variable --
25960 if the variable has a constant value the referring expression can
25961 take advantage of that fact. */
25962 if (VAR_P (d))
25963 defer_ok = false;
25965 /* Don't instantiate cloned functions. Instead, instantiate the
25966 functions they cloned. */
25967 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25968 d = DECL_CLONED_FUNCTION (d);
25970 if (DECL_TEMPLATE_INSTANTIATED (d)
25971 || TREE_TYPE (d) == error_mark_node
25972 || (TREE_CODE (d) == FUNCTION_DECL
25973 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25974 || DECL_TEMPLATE_SPECIALIZATION (d))
25975 /* D has already been instantiated or explicitly specialized, so
25976 there's nothing for us to do here.
25978 It might seem reasonable to check whether or not D is an explicit
25979 instantiation, and, if so, stop here. But when an explicit
25980 instantiation is deferred until the end of the compilation,
25981 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25982 the instantiation. */
25983 return d;
25985 /* Check to see whether we know that this template will be
25986 instantiated in some other file, as with "extern template"
25987 extension. */
25988 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25990 /* In general, we do not instantiate such templates. */
25991 if (external_p && !always_instantiate_p (d))
25992 return d;
25994 gen_tmpl = most_general_template (tmpl);
25995 gen_args = DECL_TI_ARGS (d);
25997 /* We should already have the extra args. */
25998 gcc_checking_assert (tmpl == gen_tmpl
25999 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
26000 == TMPL_ARGS_DEPTH (gen_args)));
26001 /* And what's in the hash table should match D. */
26002 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
26003 == d
26004 || spec == NULL_TREE);
26006 /* This needs to happen before any tsubsting. */
26007 if (! push_tinst_level (d))
26008 return d;
26010 timevar_push (TV_TEMPLATE_INST);
26012 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26013 for the instantiation. */
26014 td = template_for_substitution (d);
26015 args = gen_args;
26017 if (variable_template_specialization_p (d))
26019 /* Look up an explicit specialization, if any. */
26020 tree elt = most_specialized_partial_spec (d, tf_warning_or_error);
26021 if (elt && elt != error_mark_node)
26023 td = TREE_VALUE (elt);
26024 args = TREE_PURPOSE (elt);
26028 code_pattern = DECL_TEMPLATE_RESULT (td);
26030 /* We should never be trying to instantiate a member of a class
26031 template or partial specialization. */
26032 gcc_assert (d != code_pattern);
26034 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
26035 || DECL_TEMPLATE_SPECIALIZATION (td))
26036 /* In the case of a friend template whose definition is provided
26037 outside the class, we may have too many arguments. Drop the
26038 ones we don't need. The same is true for specializations. */
26039 args = get_innermost_template_args
26040 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
26042 if (TREE_CODE (d) == FUNCTION_DECL)
26044 deleted_p = DECL_DELETED_FN (code_pattern);
26045 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
26046 && DECL_INITIAL (code_pattern) != error_mark_node)
26047 || DECL_DEFAULTED_FN (code_pattern)
26048 || deleted_p);
26050 else
26052 deleted_p = false;
26053 if (DECL_CLASS_SCOPE_P (code_pattern))
26054 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
26055 else
26056 pattern_defined = ! DECL_EXTERNAL (code_pattern);
26059 /* We may be in the middle of deferred access check. Disable it now. */
26060 push_deferring_access_checks (dk_no_deferred);
26062 /* Unless an explicit instantiation directive has already determined
26063 the linkage of D, remember that a definition is available for
26064 this entity. */
26065 if (pattern_defined
26066 && !DECL_INTERFACE_KNOWN (d)
26067 && !DECL_NOT_REALLY_EXTERN (d))
26068 mark_definable (d);
26070 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
26071 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
26072 input_location = DECL_SOURCE_LOCATION (d);
26074 /* If D is a member of an explicitly instantiated class template,
26075 and no definition is available, treat it like an implicit
26076 instantiation. */
26077 if (!pattern_defined && expl_inst_class_mem_p
26078 && DECL_EXPLICIT_INSTANTIATION (d))
26080 /* Leave linkage flags alone on instantiations with anonymous
26081 visibility. */
26082 if (TREE_PUBLIC (d))
26084 DECL_NOT_REALLY_EXTERN (d) = 0;
26085 DECL_INTERFACE_KNOWN (d) = 0;
26087 SET_DECL_IMPLICIT_INSTANTIATION (d);
26090 /* Defer all other templates, unless we have been explicitly
26091 forbidden from doing so. */
26092 if (/* If there is no definition, we cannot instantiate the
26093 template. */
26094 ! pattern_defined
26095 /* If it's OK to postpone instantiation, do so. */
26096 || defer_ok
26097 /* If this is a static data member that will be defined
26098 elsewhere, we don't want to instantiate the entire data
26099 member, but we do want to instantiate the initializer so that
26100 we can substitute that elsewhere. */
26101 || (external_p && VAR_P (d))
26102 /* Handle here a deleted function too, avoid generating
26103 its body (c++/61080). */
26104 || deleted_p)
26106 /* The definition of the static data member is now required so
26107 we must substitute the initializer. */
26108 if (VAR_P (d)
26109 && !DECL_INITIAL (d)
26110 && DECL_INITIAL (code_pattern))
26112 tree ns;
26113 tree init;
26114 bool const_init = false;
26115 bool enter_context = DECL_CLASS_SCOPE_P (d);
26117 ns = decl_namespace_context (d);
26118 push_nested_namespace (ns);
26119 if (enter_context)
26120 push_nested_class (DECL_CONTEXT (d));
26121 init = tsubst_expr (DECL_INITIAL (code_pattern),
26122 args,
26123 tf_warning_or_error, NULL_TREE,
26124 /*integral_constant_expression_p=*/false);
26125 /* If instantiating the initializer involved instantiating this
26126 again, don't call cp_finish_decl twice. */
26127 if (!DECL_INITIAL (d))
26129 /* Make sure the initializer is still constant, in case of
26130 circular dependency (template/instantiate6.C). */
26131 const_init
26132 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26133 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26134 /*asmspec_tree=*/NULL_TREE,
26135 LOOKUP_ONLYCONVERTING);
26137 if (enter_context)
26138 pop_nested_class ();
26139 pop_nested_namespace (ns);
26142 /* We restore the source position here because it's used by
26143 add_pending_template. */
26144 input_location = saved_loc;
26146 if (at_eof && !pattern_defined
26147 && DECL_EXPLICIT_INSTANTIATION (d)
26148 && DECL_NOT_REALLY_EXTERN (d))
26149 /* [temp.explicit]
26151 The definition of a non-exported function template, a
26152 non-exported member function template, or a non-exported
26153 member function or static data member of a class template
26154 shall be present in every translation unit in which it is
26155 explicitly instantiated. */
26156 permerror (input_location, "explicit instantiation of %qD "
26157 "but no definition available", d);
26159 /* If we're in unevaluated context, we just wanted to get the
26160 constant value; this isn't an odr use, so don't queue
26161 a full instantiation. */
26162 if (!cp_unevaluated_operand
26163 /* ??? Historically, we have instantiated inline functions, even
26164 when marked as "extern template". */
26165 && !(external_p && VAR_P (d)))
26166 add_pending_template (d);
26168 else
26170 set_instantiating_module (d);
26171 if (variable_template_p (gen_tmpl))
26172 note_variable_template_instantiation (d);
26173 instantiate_body (td, args, d, false);
26176 pop_deferring_access_checks ();
26177 timevar_pop (TV_TEMPLATE_INST);
26178 pop_tinst_level ();
26179 input_location = saved_loc;
26180 cp_unevaluated_operand = saved_unevaluated_operand;
26181 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26183 return d;
26186 /* Run through the list of templates that we wish we could
26187 instantiate, and instantiate any we can. RETRIES is the
26188 number of times we retry pending template instantiation. */
26190 void
26191 instantiate_pending_templates (int retries)
26193 int reconsider;
26194 location_t saved_loc = input_location;
26196 /* Instantiating templates may trigger vtable generation. This in turn
26197 may require further template instantiations. We place a limit here
26198 to avoid infinite loop. */
26199 if (pending_templates && retries >= max_tinst_depth)
26201 tree decl = pending_templates->tinst->maybe_get_node ();
26203 fatal_error (input_location,
26204 "template instantiation depth exceeds maximum of %d"
26205 " instantiating %q+D, possibly from virtual table generation"
26206 " (use %<-ftemplate-depth=%> to increase the maximum)",
26207 max_tinst_depth, decl);
26208 if (TREE_CODE (decl) == FUNCTION_DECL)
26209 /* Pretend that we defined it. */
26210 DECL_INITIAL (decl) = error_mark_node;
26211 return;
26216 struct pending_template **t = &pending_templates;
26217 struct pending_template *last = NULL;
26218 reconsider = 0;
26219 while (*t)
26221 tree instantiation = reopen_tinst_level ((*t)->tinst);
26222 bool complete = false;
26224 if (TYPE_P (instantiation))
26226 if (!COMPLETE_TYPE_P (instantiation))
26228 instantiate_class_template (instantiation);
26229 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26230 for (tree fld = TYPE_FIELDS (instantiation);
26231 fld; fld = TREE_CHAIN (fld))
26232 if ((VAR_P (fld)
26233 || (TREE_CODE (fld) == FUNCTION_DECL
26234 && !DECL_ARTIFICIAL (fld)))
26235 && DECL_TEMPLATE_INSTANTIATION (fld))
26236 instantiate_decl (fld,
26237 /*defer_ok=*/false,
26238 /*expl_inst_class_mem_p=*/false);
26240 if (COMPLETE_TYPE_P (instantiation))
26241 reconsider = 1;
26244 complete = COMPLETE_TYPE_P (instantiation);
26246 else
26248 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26249 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26251 instantiation
26252 = instantiate_decl (instantiation,
26253 /*defer_ok=*/false,
26254 /*expl_inst_class_mem_p=*/false);
26255 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26256 reconsider = 1;
26259 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26260 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26263 if (complete)
26265 /* If INSTANTIATION has been instantiated, then we don't
26266 need to consider it again in the future. */
26267 struct pending_template *drop = *t;
26268 *t = (*t)->next;
26269 set_refcount_ptr (drop->tinst);
26270 pending_template_freelist ().free (drop);
26272 else
26274 last = *t;
26275 t = &(*t)->next;
26277 tinst_depth = 0;
26278 set_refcount_ptr (current_tinst_level);
26280 last_pending_template = last;
26282 while (reconsider);
26284 input_location = saved_loc;
26287 /* Substitute ARGVEC into T, which is a list of initializers for
26288 either base class or a non-static data member. The TREE_PURPOSEs
26289 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26290 instantiate_decl. */
26292 static tree
26293 tsubst_initializer_list (tree t, tree argvec)
26295 tree inits = NULL_TREE;
26296 tree target_ctor = error_mark_node;
26298 for (; t; t = TREE_CHAIN (t))
26300 tree decl;
26301 tree init;
26302 tree expanded_bases = NULL_TREE;
26303 tree expanded_arguments = NULL_TREE;
26304 int i, len = 1;
26306 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26308 tree expr;
26309 tree arg;
26311 /* Expand the base class expansion type into separate base
26312 classes. */
26313 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26314 tf_warning_or_error,
26315 NULL_TREE);
26316 if (expanded_bases == error_mark_node)
26317 continue;
26319 /* We'll be building separate TREE_LISTs of arguments for
26320 each base. */
26321 len = TREE_VEC_LENGTH (expanded_bases);
26322 expanded_arguments = make_tree_vec (len);
26323 for (i = 0; i < len; i++)
26324 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26326 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26327 expand each argument in the TREE_VALUE of t. */
26328 expr = make_node (EXPR_PACK_EXPANSION);
26329 PACK_EXPANSION_LOCAL_P (expr) = true;
26330 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26331 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26333 if (TREE_VALUE (t) == void_type_node)
26334 /* VOID_TYPE_NODE is used to indicate
26335 value-initialization. */
26337 for (i = 0; i < len; i++)
26338 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26340 else
26342 /* Substitute parameter packs into each argument in the
26343 TREE_LIST. */
26344 in_base_initializer = 1;
26345 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26347 tree expanded_exprs;
26349 /* Expand the argument. */
26350 tree value;
26351 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26352 value = TREE_VALUE (arg);
26353 else
26355 value = expr;
26356 SET_PACK_EXPANSION_PATTERN (value, TREE_VALUE (arg));
26358 expanded_exprs
26359 = tsubst_pack_expansion (value, argvec,
26360 tf_warning_or_error,
26361 NULL_TREE);
26362 if (expanded_exprs == error_mark_node)
26363 continue;
26365 /* Prepend each of the expanded expressions to the
26366 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26367 for (i = 0; i < len; i++)
26368 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26369 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
26370 TREE_VEC_ELT (expanded_arguments, i)
26371 = tree_cons (NULL_TREE,
26372 TREE_VEC_ELT (expanded_exprs, j),
26373 TREE_VEC_ELT (expanded_arguments, i));
26374 else
26375 TREE_VEC_ELT (expanded_arguments, i)
26376 = tree_cons (NULL_TREE,
26377 TREE_VEC_ELT (expanded_exprs, i),
26378 TREE_VEC_ELT (expanded_arguments, i));
26380 in_base_initializer = 0;
26382 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26383 since we built them backwards. */
26384 for (i = 0; i < len; i++)
26386 TREE_VEC_ELT (expanded_arguments, i) =
26387 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26392 for (i = 0; i < len; ++i)
26394 if (expanded_bases)
26396 decl = TREE_VEC_ELT (expanded_bases, i);
26397 decl = expand_member_init (decl);
26398 init = TREE_VEC_ELT (expanded_arguments, i);
26400 else
26402 tree tmp;
26403 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26404 tf_warning_or_error, NULL_TREE);
26406 decl = expand_member_init (decl);
26407 if (decl && !DECL_P (decl))
26408 in_base_initializer = 1;
26410 init = TREE_VALUE (t);
26411 tmp = init;
26412 if (init != void_type_node)
26413 init = tsubst_expr (init, argvec,
26414 tf_warning_or_error, NULL_TREE,
26415 /*integral_constant_expression_p=*/false);
26416 if (init == NULL_TREE && tmp != NULL_TREE)
26417 /* If we had an initializer but it instantiated to nothing,
26418 value-initialize the object. This will only occur when
26419 the initializer was a pack expansion where the parameter
26420 packs used in that expansion were of length zero. */
26421 init = void_type_node;
26422 in_base_initializer = 0;
26425 if (target_ctor != error_mark_node
26426 && init != error_mark_node)
26428 error ("mem-initializer for %qD follows constructor delegation",
26429 decl);
26430 return inits;
26432 /* Look for a target constructor. */
26433 if (init != error_mark_node
26434 && decl && CLASS_TYPE_P (decl)
26435 && same_type_p (decl, current_class_type))
26437 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26438 if (inits)
26440 error ("constructor delegation follows mem-initializer for %qD",
26441 TREE_PURPOSE (inits));
26442 continue;
26444 target_ctor = init;
26447 if (decl)
26449 init = build_tree_list (decl, init);
26450 /* Carry over the dummy TREE_TYPE node containing the source
26451 location. */
26452 TREE_TYPE (init) = TREE_TYPE (t);
26453 TREE_CHAIN (init) = inits;
26454 inits = init;
26458 return inits;
26461 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26462 is the instantiation (which should have been created with
26463 start_enum) and ARGS are the template arguments to use. */
26465 static void
26466 tsubst_enum (tree tag, tree newtag, tree args)
26468 tree e;
26470 if (SCOPED_ENUM_P (newtag))
26471 begin_scope (sk_scoped_enum, newtag);
26473 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26475 tree value;
26476 tree decl;
26478 decl = TREE_VALUE (e);
26479 /* Note that in a template enum, the TREE_VALUE is the
26480 CONST_DECL, not the corresponding INTEGER_CST. */
26481 value = tsubst_expr (DECL_INITIAL (decl),
26482 args, tf_warning_or_error, NULL_TREE,
26483 /*integral_constant_expression_p=*/true);
26485 /* Give this enumeration constant the correct access. */
26486 set_current_access_from_decl (decl);
26488 /* Actually build the enumerator itself. Here we're assuming that
26489 enumerators can't have dependent attributes. */
26490 build_enumerator (DECL_NAME (decl), value, newtag,
26491 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26494 if (SCOPED_ENUM_P (newtag))
26495 finish_scope ();
26497 finish_enum_value_list (newtag);
26498 finish_enum (newtag);
26500 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26501 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26504 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26505 its type -- but without substituting the innermost set of template
26506 arguments. So, innermost set of template parameters will appear in
26507 the type. */
26509 tree
26510 get_mostly_instantiated_function_type (tree decl)
26512 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26513 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26516 /* Return truthvalue if we're processing a template different from
26517 the last one involved in diagnostics. */
26518 bool
26519 problematic_instantiation_changed (void)
26521 return current_tinst_level != last_error_tinst_level;
26524 /* Remember current template involved in diagnostics. */
26525 void
26526 record_last_problematic_instantiation (void)
26528 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26531 struct tinst_level *
26532 current_instantiation (void)
26534 return current_tinst_level;
26537 /* Return TRUE if current_function_decl is being instantiated, false
26538 otherwise. */
26540 bool
26541 instantiating_current_function_p (void)
26543 return (current_instantiation ()
26544 && (current_instantiation ()->maybe_get_node ()
26545 == current_function_decl));
26548 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26549 type. Return false for ok, true for disallowed. Issue error and
26550 inform messages under control of COMPLAIN. */
26552 static bool
26553 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26555 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26556 return false;
26557 else if (TYPE_PTR_P (type))
26558 return false;
26559 else if (TYPE_REF_P (type)
26560 && !TYPE_REF_IS_RVALUE (type))
26561 return false;
26562 else if (TYPE_PTRMEM_P (type))
26563 return false;
26564 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26566 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26568 if (complain & tf_error)
26569 error ("non-type template parameters of deduced class type only "
26570 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26571 return true;
26573 return false;
26575 else if (TREE_CODE (type) == TYPENAME_TYPE)
26576 return false;
26577 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26578 return false;
26579 else if (TREE_CODE (type) == NULLPTR_TYPE)
26580 return false;
26581 /* A bound template template parm could later be instantiated to have a valid
26582 nontype parm type via an alias template. */
26583 else if (cxx_dialect >= cxx11
26584 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26585 return false;
26586 else if (TREE_CODE (type) == COMPLEX_TYPE)
26587 /* Fall through. */;
26588 else if (VOID_TYPE_P (type))
26589 /* Fall through. */;
26590 else if (cxx_dialect >= cxx20)
26592 if (dependent_type_p (type))
26593 return false;
26594 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26595 return true;
26596 if (structural_type_p (type))
26597 return false;
26598 if (complain & tf_error)
26600 auto_diagnostic_group d;
26601 error ("%qT is not a valid type for a template non-type "
26602 "parameter because it is not structural", type);
26603 structural_type_p (type, true);
26605 return true;
26607 else if (CLASS_TYPE_P (type))
26609 if (complain & tf_error)
26610 error ("non-type template parameters of class type only available "
26611 "with %<-std=c++20%> or %<-std=gnu++20%>");
26612 return true;
26615 if (complain & tf_error)
26617 if (type == error_mark_node)
26618 inform (input_location, "invalid template non-type parameter");
26619 else
26620 error ("%q#T is not a valid type for a template non-type parameter",
26621 type);
26623 return true;
26626 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26627 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26629 static bool
26630 dependent_type_p_r (tree type)
26632 tree scope;
26634 /* [temp.dep.type]
26636 A type is dependent if it is:
26638 -- a template parameter. Template template parameters are types
26639 for us (since TYPE_P holds true for them) so we handle
26640 them here. */
26641 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26642 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26643 return true;
26644 /* -- a qualified-id with a nested-name-specifier which contains a
26645 class-name that names a dependent type or whose unqualified-id
26646 names a dependent type. */
26647 if (TREE_CODE (type) == TYPENAME_TYPE)
26648 return true;
26650 /* An alias template specialization can be dependent even if the
26651 resulting type is not. */
26652 if (dependent_alias_template_spec_p (type, nt_transparent))
26653 return true;
26655 /* -- a cv-qualified type where the cv-unqualified type is
26656 dependent.
26657 No code is necessary for this bullet; the code below handles
26658 cv-qualified types, and we don't want to strip aliases with
26659 TYPE_MAIN_VARIANT because of DR 1558. */
26660 /* -- a compound type constructed from any dependent type. */
26661 if (TYPE_PTRMEM_P (type))
26662 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26663 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26664 (type)));
26665 else if (INDIRECT_TYPE_P (type))
26666 return dependent_type_p (TREE_TYPE (type));
26667 else if (FUNC_OR_METHOD_TYPE_P (type))
26669 tree arg_type;
26671 if (dependent_type_p (TREE_TYPE (type)))
26672 return true;
26673 for (arg_type = TYPE_ARG_TYPES (type);
26674 arg_type;
26675 arg_type = TREE_CHAIN (arg_type))
26676 if (dependent_type_p (TREE_VALUE (arg_type)))
26677 return true;
26678 if (cxx_dialect >= cxx17)
26679 /* A value-dependent noexcept-specifier makes the type dependent. */
26680 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26681 if (tree noex = TREE_PURPOSE (spec))
26682 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26683 affect overload resolution and treating it as dependent breaks
26684 things. Same for an unparsed noexcept expression. */
26685 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26686 && TREE_CODE (noex) != DEFERRED_PARSE
26687 && value_dependent_expression_p (noex))
26688 return true;
26689 return false;
26691 /* -- an array type constructed from any dependent type or whose
26692 size is specified by a constant expression that is
26693 value-dependent.
26695 We checked for type- and value-dependence of the bounds in
26696 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26697 if (TREE_CODE (type) == ARRAY_TYPE)
26699 if (TYPE_DOMAIN (type)
26700 && dependent_type_p (TYPE_DOMAIN (type)))
26701 return true;
26702 return dependent_type_p (TREE_TYPE (type));
26705 /* -- a template-id in which either the template name is a template
26706 parameter ... */
26707 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26708 return true;
26709 /* ... or any of the template arguments is a dependent type or
26710 an expression that is type-dependent or value-dependent. */
26711 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26712 && (any_dependent_template_arguments_p
26713 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26714 return true;
26716 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26717 dependent; if the argument of the `typeof' expression is not
26718 type-dependent, then it should already been have resolved. */
26719 if (TREE_CODE (type) == TYPEOF_TYPE
26720 || TREE_CODE (type) == DECLTYPE_TYPE
26721 || TREE_CODE (type) == UNDERLYING_TYPE)
26722 return true;
26724 /* A template argument pack is dependent if any of its packed
26725 arguments are. */
26726 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26728 tree args = ARGUMENT_PACK_ARGS (type);
26729 int i, len = TREE_VEC_LENGTH (args);
26730 for (i = 0; i < len; ++i)
26731 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26732 return true;
26735 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26736 be template parameters. */
26737 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26738 return true;
26740 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26741 return true;
26743 /* The standard does not specifically mention types that are local
26744 to template functions or local classes, but they should be
26745 considered dependent too. For example:
26747 template <int I> void f() {
26748 enum E { a = I };
26749 S<sizeof (E)> s;
26752 The size of `E' cannot be known until the value of `I' has been
26753 determined. Therefore, `E' must be considered dependent. */
26754 scope = TYPE_CONTEXT (type);
26755 if (scope && TYPE_P (scope))
26756 return dependent_type_p (scope);
26757 /* Don't use type_dependent_expression_p here, as it can lead
26758 to infinite recursion trying to determine whether a lambda
26759 nested in a lambda is dependent (c++/47687). */
26760 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26761 && DECL_LANG_SPECIFIC (scope)
26762 && DECL_TEMPLATE_INFO (scope)
26763 && (any_dependent_template_arguments_p
26764 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26765 return true;
26767 /* Other types are non-dependent. */
26768 return false;
26771 /* Returns TRUE if TYPE is dependent, in the sense of
26772 [temp.dep.type]. Note that a NULL type is considered dependent. */
26774 bool
26775 dependent_type_p (tree type)
26777 /* If there are no template parameters in scope, then there can't be
26778 any dependent types. */
26779 if (!processing_template_decl)
26781 /* If we are not processing a template, then nobody should be
26782 providing us with a dependent type. */
26783 gcc_assert (type);
26784 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26785 return false;
26788 /* If the type is NULL, we have not computed a type for the entity
26789 in question; in that case, the type is dependent. */
26790 if (!type)
26791 return true;
26793 /* Erroneous types can be considered non-dependent. */
26794 if (type == error_mark_node)
26795 return false;
26797 /* If we have not already computed the appropriate value for TYPE,
26798 do so now. */
26799 if (!TYPE_DEPENDENT_P_VALID (type))
26801 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26802 TYPE_DEPENDENT_P_VALID (type) = 1;
26805 return TYPE_DEPENDENT_P (type);
26808 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26809 lookup. In other words, a dependent type that is not the current
26810 instantiation. */
26812 bool
26813 dependent_scope_p (tree scope)
26815 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26816 && !currently_open_class (scope));
26819 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26820 an unknown base of 'this' (and is therefore instantiation-dependent). */
26822 static bool
26823 unknown_base_ref_p (tree t)
26825 if (!current_class_ptr)
26826 return false;
26828 tree mem = TREE_OPERAND (t, 1);
26829 if (shared_member_p (mem))
26830 return false;
26832 tree cur = current_nonlambda_class_type ();
26833 if (!any_dependent_bases_p (cur))
26834 return false;
26836 tree ctx = TREE_OPERAND (t, 0);
26837 if (DERIVED_FROM_P (ctx, cur))
26838 return false;
26840 return true;
26843 /* T is a SCOPE_REF; return whether we need to consider it
26844 instantiation-dependent so that we can check access at instantiation
26845 time even though we know which member it resolves to. */
26847 static bool
26848 instantiation_dependent_scope_ref_p (tree t)
26850 if (DECL_P (TREE_OPERAND (t, 1))
26851 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26852 && !unknown_base_ref_p (t)
26853 && accessible_in_template_p (TREE_OPERAND (t, 0),
26854 TREE_OPERAND (t, 1)))
26855 return false;
26856 else
26857 return true;
26860 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26861 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26862 expression. */
26864 /* Note that this predicate is not appropriate for general expressions;
26865 only constant expressions (that satisfy potential_constant_expression)
26866 can be tested for value dependence. */
26868 bool
26869 value_dependent_expression_p (tree expression)
26871 if (!processing_template_decl || expression == NULL_TREE)
26872 return false;
26874 /* A type-dependent expression is also value-dependent. */
26875 if (type_dependent_expression_p (expression))
26876 return true;
26878 switch (TREE_CODE (expression))
26880 case BASELINK:
26881 /* A dependent member function of the current instantiation. */
26882 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26884 case FUNCTION_DECL:
26885 /* A dependent member function of the current instantiation. */
26886 if (DECL_CLASS_SCOPE_P (expression)
26887 && dependent_type_p (DECL_CONTEXT (expression)))
26888 return true;
26889 break;
26891 case IDENTIFIER_NODE:
26892 /* A name that has not been looked up -- must be dependent. */
26893 return true;
26895 case TEMPLATE_PARM_INDEX:
26896 /* A non-type template parm. */
26897 return true;
26899 case CONST_DECL:
26900 /* A non-type template parm. */
26901 if (DECL_TEMPLATE_PARM_P (expression))
26902 return true;
26903 return value_dependent_expression_p (DECL_INITIAL (expression));
26905 case VAR_DECL:
26906 /* A constant with literal type and is initialized
26907 with an expression that is value-dependent. */
26908 if (DECL_DEPENDENT_INIT_P (expression)
26909 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26910 || TYPE_REF_P (TREE_TYPE (expression)))
26911 return true;
26912 if (DECL_HAS_VALUE_EXPR_P (expression))
26914 tree value_expr = DECL_VALUE_EXPR (expression);
26915 if (value_dependent_expression_p (value_expr)
26916 /* __PRETTY_FUNCTION__ inside a template function is dependent
26917 on the name of the function. */
26918 || (DECL_PRETTY_FUNCTION_P (expression)
26919 /* It might be used in a template, but not a template
26920 function, in which case its DECL_VALUE_EXPR will be
26921 "top level". */
26922 && value_expr == error_mark_node))
26923 return true;
26925 return false;
26927 case DYNAMIC_CAST_EXPR:
26928 case STATIC_CAST_EXPR:
26929 case CONST_CAST_EXPR:
26930 case REINTERPRET_CAST_EXPR:
26931 case CAST_EXPR:
26932 case IMPLICIT_CONV_EXPR:
26933 /* These expressions are value-dependent if the type to which
26934 the cast occurs is dependent or the expression being casted
26935 is value-dependent. */
26937 tree type = TREE_TYPE (expression);
26939 if (dependent_type_p (type))
26940 return true;
26942 /* A functional cast has a list of operands. */
26943 expression = TREE_OPERAND (expression, 0);
26944 if (!expression)
26946 /* If there are no operands, it must be an expression such
26947 as "int()". This should not happen for aggregate types
26948 because it would form non-constant expressions. */
26949 gcc_assert (cxx_dialect >= cxx11
26950 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26952 return false;
26955 if (TREE_CODE (expression) == TREE_LIST)
26956 return any_value_dependent_elements_p (expression);
26958 return value_dependent_expression_p (expression);
26961 case SIZEOF_EXPR:
26962 if (SIZEOF_EXPR_TYPE_P (expression))
26963 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26964 /* FALLTHRU */
26965 case ALIGNOF_EXPR:
26966 case TYPEID_EXPR:
26967 /* A `sizeof' expression is value-dependent if the operand is
26968 type-dependent or is a pack expansion. */
26969 expression = TREE_OPERAND (expression, 0);
26970 if (PACK_EXPANSION_P (expression))
26971 return true;
26972 else if (TYPE_P (expression))
26973 return dependent_type_p (expression);
26974 return instantiation_dependent_uneval_expression_p (expression);
26976 case AT_ENCODE_EXPR:
26977 /* An 'encode' expression is value-dependent if the operand is
26978 type-dependent. */
26979 expression = TREE_OPERAND (expression, 0);
26980 return dependent_type_p (expression);
26982 case NOEXCEPT_EXPR:
26983 expression = TREE_OPERAND (expression, 0);
26984 return instantiation_dependent_uneval_expression_p (expression);
26986 case SCOPE_REF:
26987 /* All instantiation-dependent expressions should also be considered
26988 value-dependent. */
26989 return instantiation_dependent_scope_ref_p (expression);
26991 case COMPONENT_REF:
26992 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26993 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26995 case NONTYPE_ARGUMENT_PACK:
26996 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26997 is value-dependent. */
26999 tree values = ARGUMENT_PACK_ARGS (expression);
27000 int i, len = TREE_VEC_LENGTH (values);
27002 for (i = 0; i < len; ++i)
27003 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
27004 return true;
27006 return false;
27009 case TRAIT_EXPR:
27011 tree type2 = TRAIT_EXPR_TYPE2 (expression);
27013 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
27014 return true;
27016 if (!type2)
27017 return false;
27019 if (TREE_CODE (type2) != TREE_LIST)
27020 return dependent_type_p (type2);
27022 for (; type2; type2 = TREE_CHAIN (type2))
27023 if (dependent_type_p (TREE_VALUE (type2)))
27024 return true;
27026 return false;
27029 case MODOP_EXPR:
27030 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27031 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
27033 case ARRAY_REF:
27034 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27035 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
27037 case ADDR_EXPR:
27039 tree op = TREE_OPERAND (expression, 0);
27040 return (value_dependent_expression_p (op)
27041 || has_value_dependent_address (op));
27044 case REQUIRES_EXPR:
27045 /* Treat all requires-expressions as value-dependent so
27046 we don't try to fold them. */
27047 return true;
27049 case TYPE_REQ:
27050 return dependent_type_p (TREE_OPERAND (expression, 0));
27052 case CALL_EXPR:
27054 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
27055 return true;
27056 tree fn = get_callee_fndecl (expression);
27057 int i, nargs;
27058 nargs = call_expr_nargs (expression);
27059 for (i = 0; i < nargs; ++i)
27061 tree op = CALL_EXPR_ARG (expression, i);
27062 /* In a call to a constexpr member function, look through the
27063 implicit ADDR_EXPR on the object argument so that it doesn't
27064 cause the call to be considered value-dependent. We also
27065 look through it in potential_constant_expression. */
27066 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
27067 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
27068 && TREE_CODE (op) == ADDR_EXPR)
27069 op = TREE_OPERAND (op, 0);
27070 if (value_dependent_expression_p (op))
27071 return true;
27073 return false;
27076 case TEMPLATE_ID_EXPR:
27077 return concept_definition_p (TREE_OPERAND (expression, 0));
27079 case CONSTRUCTOR:
27081 unsigned ix;
27082 tree val;
27083 if (dependent_type_p (TREE_TYPE (expression)))
27084 return true;
27085 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
27086 if (value_dependent_expression_p (val))
27087 return true;
27088 return false;
27091 case STMT_EXPR:
27092 /* Treat a GNU statement expression as dependent to avoid crashing
27093 under instantiate_non_dependent_expr; it can't be constant. */
27094 return true;
27096 default:
27097 /* A constant expression is value-dependent if any subexpression is
27098 value-dependent. */
27099 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
27101 case tcc_reference:
27102 case tcc_unary:
27103 case tcc_comparison:
27104 case tcc_binary:
27105 case tcc_expression:
27106 case tcc_vl_exp:
27108 int i, len = cp_tree_operand_length (expression);
27110 for (i = 0; i < len; i++)
27112 tree t = TREE_OPERAND (expression, i);
27114 /* In some cases, some of the operands may be missing.
27115 (For example, in the case of PREDECREMENT_EXPR, the
27116 amount to increment by may be missing.) That doesn't
27117 make the expression dependent. */
27118 if (t && value_dependent_expression_p (t))
27119 return true;
27122 break;
27123 default:
27124 break;
27126 break;
27129 /* The expression is not value-dependent. */
27130 return false;
27133 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27134 [temp.dep.expr]. Note that an expression with no type is
27135 considered dependent. Other parts of the compiler arrange for an
27136 expression with type-dependent subexpressions to have no type, so
27137 this function doesn't have to be fully recursive. */
27139 bool
27140 type_dependent_expression_p (tree expression)
27142 if (!processing_template_decl)
27143 return false;
27145 if (expression == NULL_TREE || expression == error_mark_node)
27146 return false;
27148 STRIP_ANY_LOCATION_WRAPPER (expression);
27150 /* An unresolved name is always dependent. */
27151 if (identifier_p (expression)
27152 || TREE_CODE (expression) == USING_DECL
27153 || TREE_CODE (expression) == WILDCARD_DECL)
27154 return true;
27156 /* A lambda-expression in template context is dependent. dependent_type_p is
27157 true for a lambda in the scope of a class or function template, but that
27158 doesn't cover all template contexts, like a default template argument. */
27159 if (TREE_CODE (expression) == LAMBDA_EXPR)
27160 return true;
27162 /* A fold expression is type-dependent. */
27163 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27164 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27165 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27166 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27167 return true;
27169 /* Some expression forms are never type-dependent. */
27170 if (TREE_CODE (expression) == SIZEOF_EXPR
27171 || TREE_CODE (expression) == ALIGNOF_EXPR
27172 || TREE_CODE (expression) == AT_ENCODE_EXPR
27173 || TREE_CODE (expression) == NOEXCEPT_EXPR
27174 || TREE_CODE (expression) == TRAIT_EXPR
27175 || TREE_CODE (expression) == TYPEID_EXPR
27176 || TREE_CODE (expression) == DELETE_EXPR
27177 || TREE_CODE (expression) == VEC_DELETE_EXPR
27178 || TREE_CODE (expression) == THROW_EXPR
27179 || TREE_CODE (expression) == REQUIRES_EXPR)
27180 return false;
27182 /* The types of these expressions depends only on the type to which
27183 the cast occurs. */
27184 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27185 || TREE_CODE (expression) == STATIC_CAST_EXPR
27186 || TREE_CODE (expression) == CONST_CAST_EXPR
27187 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27188 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27189 || TREE_CODE (expression) == CAST_EXPR)
27190 return dependent_type_p (TREE_TYPE (expression));
27192 /* The types of these expressions depends only on the type created
27193 by the expression. */
27194 if (TREE_CODE (expression) == NEW_EXPR
27195 || TREE_CODE (expression) == VEC_NEW_EXPR)
27197 /* For NEW_EXPR tree nodes created inside a template, either
27198 the object type itself or a TREE_LIST may appear as the
27199 operand 1. */
27200 tree type = TREE_OPERAND (expression, 1);
27201 if (TREE_CODE (type) == TREE_LIST)
27202 /* This is an array type. We need to check array dimensions
27203 as well. */
27204 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27205 || value_dependent_expression_p
27206 (TREE_OPERAND (TREE_VALUE (type), 1));
27207 /* Array type whose dimension has to be deduced. */
27208 else if (TREE_CODE (type) == ARRAY_TYPE
27209 && TREE_OPERAND (expression, 2) == NULL_TREE)
27210 return true;
27211 else
27212 return dependent_type_p (type);
27215 if (TREE_CODE (expression) == SCOPE_REF)
27217 tree scope = TREE_OPERAND (expression, 0);
27218 tree name = TREE_OPERAND (expression, 1);
27220 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27221 contains an identifier associated by name lookup with one or more
27222 declarations declared with a dependent type, or...a
27223 nested-name-specifier or qualified-id that names a member of an
27224 unknown specialization. */
27225 return (type_dependent_expression_p (name)
27226 || dependent_scope_p (scope));
27229 if (TREE_CODE (expression) == TEMPLATE_DECL
27230 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27231 return uses_outer_template_parms (expression);
27233 if (TREE_CODE (expression) == STMT_EXPR)
27234 expression = stmt_expr_value_expr (expression);
27236 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27238 tree elt;
27239 unsigned i;
27241 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27243 if (type_dependent_expression_p (elt))
27244 return true;
27246 return false;
27249 /* A static data member of the current instantiation with incomplete
27250 array type is type-dependent, as the definition and specializations
27251 can have different bounds. */
27252 if (VAR_P (expression)
27253 && DECL_CLASS_SCOPE_P (expression)
27254 && dependent_type_p (DECL_CONTEXT (expression))
27255 && VAR_HAD_UNKNOWN_BOUND (expression))
27256 return true;
27258 /* An array of unknown bound depending on a variadic parameter, eg:
27260 template<typename... Args>
27261 void foo (Args... args)
27263 int arr[] = { args... };
27266 template<int... vals>
27267 void bar ()
27269 int arr[] = { vals... };
27272 If the array has no length and has an initializer, it must be that
27273 we couldn't determine its length in cp_complete_array_type because
27274 it is dependent. */
27275 if (VAR_P (expression)
27276 && TREE_TYPE (expression) != NULL_TREE
27277 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27278 && !TYPE_DOMAIN (TREE_TYPE (expression))
27279 && DECL_INITIAL (expression))
27280 return true;
27282 /* A function or variable template-id is type-dependent if it has any
27283 dependent template arguments. */
27284 if (VAR_OR_FUNCTION_DECL_P (expression)
27285 && DECL_LANG_SPECIFIC (expression)
27286 && DECL_TEMPLATE_INFO (expression))
27288 /* Consider the innermost template arguments, since those are the ones
27289 that come from the template-id; the template arguments for the
27290 enclosing class do not make it type-dependent unless they are used in
27291 the type of the decl. */
27292 if (instantiates_primary_template_p (expression)
27293 && (any_dependent_template_arguments_p
27294 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27295 return true;
27298 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27299 type-dependent. Checking this is important for functions with auto return
27300 type, which looks like a dependent type. */
27301 if (TREE_CODE (expression) == FUNCTION_DECL
27302 && !(DECL_CLASS_SCOPE_P (expression)
27303 && dependent_type_p (DECL_CONTEXT (expression)))
27304 && !(DECL_LANG_SPECIFIC (expression)
27305 && DECL_UNIQUE_FRIEND_P (expression)
27306 && (!DECL_FRIEND_CONTEXT (expression)
27307 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27308 && !DECL_LOCAL_DECL_P (expression))
27310 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27311 || undeduced_auto_decl (expression));
27312 return false;
27315 /* Always dependent, on the number of arguments if nothing else. */
27316 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27317 return true;
27319 if (TREE_TYPE (expression) == unknown_type_node)
27321 if (TREE_CODE (expression) == ADDR_EXPR)
27322 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27323 if (TREE_CODE (expression) == COMPONENT_REF
27324 || TREE_CODE (expression) == OFFSET_REF)
27326 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27327 return true;
27328 expression = TREE_OPERAND (expression, 1);
27329 if (identifier_p (expression))
27330 return false;
27332 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27333 if (TREE_CODE (expression) == SCOPE_REF)
27334 return false;
27336 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27337 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27338 || TREE_CODE (expression) == CO_YIELD_EXPR)
27339 return true;
27341 if (BASELINK_P (expression))
27343 if (BASELINK_OPTYPE (expression)
27344 && dependent_type_p (BASELINK_OPTYPE (expression)))
27345 return true;
27346 expression = BASELINK_FUNCTIONS (expression);
27349 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27351 if (any_dependent_template_arguments_p
27352 (TREE_OPERAND (expression, 1)))
27353 return true;
27354 expression = TREE_OPERAND (expression, 0);
27355 if (identifier_p (expression))
27356 return true;
27359 gcc_assert (OVL_P (expression));
27361 for (lkp_iterator iter (expression); iter; ++iter)
27362 if (type_dependent_expression_p (*iter))
27363 return true;
27365 return false;
27368 /* The type of a non-type template parm declared with a placeholder type
27369 depends on the corresponding template argument, even though
27370 placeholders are not normally considered dependent. */
27371 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27372 && is_auto (TREE_TYPE (expression)))
27373 return true;
27375 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27377 /* Dependent type attributes might not have made it from the decl to
27378 the type yet. */
27379 if (DECL_P (expression)
27380 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27381 return true;
27383 return (dependent_type_p (TREE_TYPE (expression)));
27386 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27387 type-dependent if the expression refers to a member of the current
27388 instantiation and the type of the referenced member is dependent, or the
27389 class member access expression refers to a member of an unknown
27390 specialization.
27392 This function returns true if the OBJECT in such a class member access
27393 expression is of an unknown specialization. */
27395 bool
27396 type_dependent_object_expression_p (tree object)
27398 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27399 dependent. */
27400 if (TREE_CODE (object) == IDENTIFIER_NODE)
27401 return true;
27402 tree scope = TREE_TYPE (object);
27403 return (!scope || dependent_scope_p (scope));
27406 /* walk_tree callback function for instantiation_dependent_expression_p,
27407 below. Returns non-zero if a dependent subexpression is found. */
27409 static tree
27410 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27411 void * /*data*/)
27413 if (TYPE_P (*tp))
27415 /* We don't have to worry about decltype currently because decltype
27416 of an instantiation-dependent expr is a dependent type. This
27417 might change depending on the resolution of DR 1172. */
27418 *walk_subtrees = false;
27419 return NULL_TREE;
27421 enum tree_code code = TREE_CODE (*tp);
27422 switch (code)
27424 /* Don't treat an argument list as dependent just because it has no
27425 TREE_TYPE. */
27426 case TREE_LIST:
27427 case TREE_VEC:
27428 case NONTYPE_ARGUMENT_PACK:
27429 return NULL_TREE;
27431 case TEMPLATE_PARM_INDEX:
27432 if (dependent_type_p (TREE_TYPE (*tp)))
27433 return *tp;
27434 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27435 return *tp;
27436 /* We'll check value-dependence separately. */
27437 return NULL_TREE;
27439 /* Handle expressions with type operands. */
27440 case SIZEOF_EXPR:
27441 case ALIGNOF_EXPR:
27442 case TYPEID_EXPR:
27443 case AT_ENCODE_EXPR:
27445 tree op = TREE_OPERAND (*tp, 0);
27446 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27447 op = TREE_TYPE (op);
27448 if (TYPE_P (op))
27450 if (dependent_type_p (op))
27451 return *tp;
27452 else
27454 *walk_subtrees = false;
27455 return NULL_TREE;
27458 break;
27461 case COMPONENT_REF:
27462 if (identifier_p (TREE_OPERAND (*tp, 1)))
27463 /* In a template, finish_class_member_access_expr creates a
27464 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27465 type-dependent, so that we can check access control at
27466 instantiation time (PR 42277). See also Core issue 1273. */
27467 return *tp;
27468 break;
27470 case SCOPE_REF:
27471 if (instantiation_dependent_scope_ref_p (*tp))
27472 return *tp;
27473 else
27474 break;
27476 /* Treat statement-expressions as dependent. */
27477 case BIND_EXPR:
27478 return *tp;
27480 /* Treat requires-expressions as dependent. */
27481 case REQUIRES_EXPR:
27482 return *tp;
27484 case CALL_EXPR:
27485 /* Treat concept checks as dependent. */
27486 if (concept_check_p (*tp))
27487 return *tp;
27488 break;
27490 case TEMPLATE_ID_EXPR:
27491 /* Treat concept checks as dependent. */
27492 if (concept_check_p (*tp))
27493 return *tp;
27494 break;
27496 case CONSTRUCTOR:
27497 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27498 return *tp;
27499 break;
27501 default:
27502 break;
27505 if (type_dependent_expression_p (*tp))
27506 return *tp;
27507 else
27508 return NULL_TREE;
27511 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27512 sense defined by the ABI:
27514 "An expression is instantiation-dependent if it is type-dependent
27515 or value-dependent, or it has a subexpression that is type-dependent
27516 or value-dependent."
27518 Except don't actually check value-dependence for unevaluated expressions,
27519 because in sizeof(i) we don't care about the value of i. Checking
27520 type-dependence will in turn check value-dependence of array bounds/template
27521 arguments as needed. */
27523 bool
27524 instantiation_dependent_uneval_expression_p (tree expression)
27526 tree result;
27528 if (!processing_template_decl)
27529 return false;
27531 if (expression == error_mark_node)
27532 return false;
27534 result = cp_walk_tree_without_duplicates (&expression,
27535 instantiation_dependent_r, NULL);
27536 return result != NULL_TREE;
27539 /* As above, but also check value-dependence of the expression as a whole. */
27541 bool
27542 instantiation_dependent_expression_p (tree expression)
27544 return (instantiation_dependent_uneval_expression_p (expression)
27545 || (processing_template_decl
27546 && potential_constant_expression (expression)
27547 && value_dependent_expression_p (expression)));
27550 /* Like type_dependent_expression_p, but it also works while not processing
27551 a template definition, i.e. during substitution or mangling. */
27553 bool
27554 type_dependent_expression_p_push (tree expr)
27556 bool b;
27557 ++processing_template_decl;
27558 b = type_dependent_expression_p (expr);
27559 --processing_template_decl;
27560 return b;
27563 /* Returns TRUE if ARGS contains a type-dependent expression. */
27565 bool
27566 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27568 unsigned int i;
27569 tree arg;
27571 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27573 if (type_dependent_expression_p (arg))
27574 return true;
27576 return false;
27579 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27580 expressions) contains any type-dependent expressions. */
27582 bool
27583 any_type_dependent_elements_p (const_tree list)
27585 for (; list; list = TREE_CHAIN (list))
27586 if (type_dependent_expression_p (TREE_VALUE (list)))
27587 return true;
27589 return false;
27592 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27593 expressions) contains any value-dependent expressions. */
27595 bool
27596 any_value_dependent_elements_p (const_tree list)
27598 for (; list; list = TREE_CHAIN (list))
27599 if (value_dependent_expression_p (TREE_VALUE (list)))
27600 return true;
27602 return false;
27605 /* Returns TRUE if the ARG (a template argument) is dependent. */
27607 bool
27608 dependent_template_arg_p (tree arg)
27610 if (!processing_template_decl)
27611 return false;
27613 /* Assume a template argument that was wrongly written by the user
27614 is dependent. This is consistent with what
27615 any_dependent_template_arguments_p [that calls this function]
27616 does. */
27617 if (!arg || arg == error_mark_node)
27618 return true;
27620 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27621 arg = argument_pack_select_arg (arg);
27623 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27624 return true;
27625 if (TREE_CODE (arg) == TEMPLATE_DECL)
27627 if (DECL_TEMPLATE_PARM_P (arg))
27628 return true;
27629 /* A member template of a dependent class is not necessarily
27630 type-dependent, but it is a dependent template argument because it
27631 will be a member of an unknown specialization to that template. */
27632 tree scope = CP_DECL_CONTEXT (arg);
27633 return TYPE_P (scope) && dependent_type_p (scope);
27635 else if (ARGUMENT_PACK_P (arg))
27637 tree args = ARGUMENT_PACK_ARGS (arg);
27638 int i, len = TREE_VEC_LENGTH (args);
27639 for (i = 0; i < len; ++i)
27641 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27642 return true;
27645 return false;
27647 else if (TYPE_P (arg))
27648 return dependent_type_p (arg);
27649 else
27650 return value_dependent_expression_p (arg);
27653 /* Returns true if ARGS (a collection of template arguments) contains
27654 any types that require structural equality testing. */
27656 bool
27657 any_template_arguments_need_structural_equality_p (tree args)
27659 int i;
27660 int j;
27662 if (!args)
27663 return false;
27664 if (args == error_mark_node)
27665 return true;
27667 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27669 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27670 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27672 tree arg = TREE_VEC_ELT (level, j);
27673 tree packed_args = NULL_TREE;
27674 int k, len = 1;
27676 if (ARGUMENT_PACK_P (arg))
27678 /* Look inside the argument pack. */
27679 packed_args = ARGUMENT_PACK_ARGS (arg);
27680 len = TREE_VEC_LENGTH (packed_args);
27683 for (k = 0; k < len; ++k)
27685 if (packed_args)
27686 arg = TREE_VEC_ELT (packed_args, k);
27688 if (error_operand_p (arg))
27689 return true;
27690 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27691 continue;
27692 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27693 return true;
27694 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27695 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27696 return true;
27701 return false;
27704 /* Returns true if ARGS (a collection of template arguments) contains
27705 any dependent arguments. */
27707 bool
27708 any_dependent_template_arguments_p (const_tree args)
27710 int i;
27711 int j;
27713 if (!args)
27714 return false;
27715 if (args == error_mark_node)
27716 return true;
27718 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27720 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27721 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27722 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27723 return true;
27726 return false;
27729 /* Returns true if ARGS contains any errors. */
27731 bool
27732 any_erroneous_template_args_p (const_tree args)
27734 int i;
27735 int j;
27737 if (args == error_mark_node)
27738 return true;
27740 if (args && TREE_CODE (args) != TREE_VEC)
27742 if (tree ti = get_template_info (args))
27743 args = TI_ARGS (ti);
27744 else
27745 args = NULL_TREE;
27748 if (!args)
27749 return false;
27751 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27753 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27754 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27755 if (error_operand_p (TREE_VEC_ELT (level, j)))
27756 return true;
27759 return false;
27762 /* Returns TRUE if the template TMPL is type-dependent. */
27764 bool
27765 dependent_template_p (tree tmpl)
27767 if (TREE_CODE (tmpl) == OVERLOAD)
27769 for (lkp_iterator iter (tmpl); iter; ++iter)
27770 if (dependent_template_p (*iter))
27771 return true;
27772 return false;
27775 /* Template template parameters are dependent. */
27776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27777 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27778 return true;
27779 /* So are names that have not been looked up. */
27780 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27781 return true;
27782 return false;
27785 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27787 bool
27788 dependent_template_id_p (tree tmpl, tree args)
27790 return (dependent_template_p (tmpl)
27791 || any_dependent_template_arguments_p (args));
27794 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27795 are dependent. */
27797 bool
27798 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27800 int i;
27802 if (!processing_template_decl)
27803 return false;
27805 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27807 tree decl = TREE_VEC_ELT (declv, i);
27808 tree init = TREE_VEC_ELT (initv, i);
27809 tree cond = TREE_VEC_ELT (condv, i);
27810 tree incr = TREE_VEC_ELT (incrv, i);
27812 if (type_dependent_expression_p (decl)
27813 || TREE_CODE (decl) == SCOPE_REF)
27814 return true;
27816 if (init && type_dependent_expression_p (init))
27817 return true;
27819 if (cond == global_namespace)
27820 return true;
27822 if (type_dependent_expression_p (cond))
27823 return true;
27825 if (COMPARISON_CLASS_P (cond)
27826 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27827 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27828 return true;
27830 if (TREE_CODE (incr) == MODOP_EXPR)
27832 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27833 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27834 return true;
27836 else if (type_dependent_expression_p (incr))
27837 return true;
27838 else if (TREE_CODE (incr) == MODIFY_EXPR)
27840 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27841 return true;
27842 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27844 tree t = TREE_OPERAND (incr, 1);
27845 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27846 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27847 return true;
27849 /* If this loop has a class iterator with != comparison
27850 with increment other than i++/++i/i--/--i, make sure the
27851 increment is constant. */
27852 if (CLASS_TYPE_P (TREE_TYPE (decl))
27853 && TREE_CODE (cond) == NE_EXPR)
27855 if (TREE_OPERAND (t, 0) == decl)
27856 t = TREE_OPERAND (t, 1);
27857 else
27858 t = TREE_OPERAND (t, 0);
27859 if (TREE_CODE (t) != INTEGER_CST)
27860 return true;
27866 return false;
27869 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27870 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27871 no such TYPE can be found. Note that this function peers inside
27872 uninstantiated templates and therefore should be used only in
27873 extremely limited situations. ONLY_CURRENT_P restricts this
27874 peering to the currently open classes hierarchy (which is required
27875 when comparing types). */
27877 tree
27878 resolve_typename_type (tree type, bool only_current_p)
27880 tree scope;
27881 tree name;
27882 tree decl;
27883 int quals;
27884 tree pushed_scope;
27885 tree result;
27887 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27889 scope = TYPE_CONTEXT (type);
27890 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27891 gcc_checking_assert (uses_template_parms (scope));
27893 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27894 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27895 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27896 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27897 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27898 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27899 the TYPENAME_TYPE instead, we avoid messing up with a possible
27900 typedef variant case. */
27901 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27903 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27904 it first before we can figure out what NAME refers to. */
27905 if (TREE_CODE (scope) == TYPENAME_TYPE)
27907 if (TYPENAME_IS_RESOLVING_P (scope))
27908 /* Given a class template A with a dependent base with nested type C,
27909 typedef typename A::C::C C will land us here, as trying to resolve
27910 the initial A::C leads to the local C typedef, which leads back to
27911 A::C::C. So we break the recursion now. */
27912 return type;
27913 else
27914 scope = resolve_typename_type (scope, only_current_p);
27916 /* If we don't know what SCOPE refers to, then we cannot resolve the
27917 TYPENAME_TYPE. */
27918 if (!CLASS_TYPE_P (scope))
27919 return type;
27920 /* If this is a typedef, we don't want to look inside (c++/11987). */
27921 if (typedef_variant_p (type))
27922 return type;
27923 /* If SCOPE isn't the template itself, it will not have a valid
27924 TYPE_FIELDS list. */
27925 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27926 /* scope is either the template itself or a compatible instantiation
27927 like X<T>, so look up the name in the original template. */
27928 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27929 /* If scope has no fields, it can't be a current instantiation. Check this
27930 before currently_open_class to avoid infinite recursion (71515). */
27931 if (!TYPE_FIELDS (scope))
27932 return type;
27933 /* If the SCOPE is not the current instantiation, there's no reason
27934 to look inside it. */
27935 if (only_current_p && !currently_open_class (scope))
27936 return type;
27937 /* Enter the SCOPE so that name lookup will be resolved as if we
27938 were in the class definition. In particular, SCOPE will no
27939 longer be considered a dependent type. */
27940 pushed_scope = push_scope (scope);
27941 /* Look up the declaration. */
27942 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27943 tf_warning_or_error);
27945 result = NULL_TREE;
27947 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27948 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27949 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27950 if (!decl)
27951 /*nop*/;
27952 else if (identifier_p (fullname)
27953 && TREE_CODE (decl) == TYPE_DECL)
27955 result = TREE_TYPE (decl);
27956 if (result == error_mark_node)
27957 result = NULL_TREE;
27959 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27960 && DECL_CLASS_TEMPLATE_P (decl))
27962 /* Obtain the template and the arguments. */
27963 tree tmpl = TREE_OPERAND (fullname, 0);
27964 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27966 /* We get here with a plain identifier because a previous tentative
27967 parse of the nested-name-specifier as part of a ptr-operator saw
27968 ::template X<A>. The use of ::template is necessary in a
27969 ptr-operator, but wrong in a declarator-id.
27971 [temp.names]: In a qualified-id of a declarator-id, the keyword
27972 template shall not appear at the top level. */
27973 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27974 "keyword %<template%> not allowed in declarator-id");
27975 tmpl = decl;
27977 tree args = TREE_OPERAND (fullname, 1);
27978 /* Instantiate the template. */
27979 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27980 /*entering_scope=*/true,
27981 tf_error | tf_user);
27982 if (result == error_mark_node)
27983 result = NULL_TREE;
27986 /* Leave the SCOPE. */
27987 if (pushed_scope)
27988 pop_scope (pushed_scope);
27990 /* If we failed to resolve it, return the original typename. */
27991 if (!result)
27992 return type;
27994 /* If lookup found a typename type, resolve that too. */
27995 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27997 /* Ill-formed programs can cause infinite recursion here, so we
27998 must catch that. */
27999 TYPENAME_IS_RESOLVING_P (result) = 1;
28000 result = resolve_typename_type (result, only_current_p);
28001 TYPENAME_IS_RESOLVING_P (result) = 0;
28004 /* Qualify the resulting type. */
28005 quals = cp_type_quals (type);
28006 if (quals)
28007 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
28009 return result;
28012 /* EXPR is an expression which is not type-dependent. Return a proxy
28013 for EXPR that can be used to compute the types of larger
28014 expressions containing EXPR. */
28016 tree
28017 build_non_dependent_expr (tree expr)
28019 tree orig_expr = expr;
28020 tree inner_expr;
28022 /* When checking, try to get a constant value for all non-dependent
28023 expressions in order to expose bugs in *_dependent_expression_p
28024 and constexpr. This can affect code generation, see PR70704, so
28025 only do this for -fchecking=2. */
28026 if (flag_checking > 1
28027 && cxx_dialect >= cxx11
28028 /* Don't do this during nsdmi parsing as it can lead to
28029 unexpected recursive instantiations. */
28030 && !parsing_nsdmi ()
28031 /* Don't do this during concept processing either and for
28032 the same reason. */
28033 && !processing_constraint_expression_p ())
28034 fold_non_dependent_expr (expr, tf_none);
28036 STRIP_ANY_LOCATION_WRAPPER (expr);
28038 /* Preserve OVERLOADs; the functions must be available to resolve
28039 types. */
28040 inner_expr = expr;
28041 if (TREE_CODE (inner_expr) == STMT_EXPR)
28042 inner_expr = stmt_expr_value_expr (inner_expr);
28043 if (TREE_CODE (inner_expr) == ADDR_EXPR)
28044 inner_expr = TREE_OPERAND (inner_expr, 0);
28045 if (TREE_CODE (inner_expr) == COMPONENT_REF)
28046 inner_expr = TREE_OPERAND (inner_expr, 1);
28047 if (is_overloaded_fn (inner_expr)
28048 || TREE_CODE (inner_expr) == OFFSET_REF)
28049 return orig_expr;
28050 /* There is no need to return a proxy for a variable or enumerator. */
28051 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
28052 return orig_expr;
28053 /* Preserve string constants; conversions from string constants to
28054 "char *" are allowed, even though normally a "const char *"
28055 cannot be used to initialize a "char *". */
28056 if (TREE_CODE (expr) == STRING_CST)
28057 return orig_expr;
28058 /* Preserve void and arithmetic constants, as an optimization -- there is no
28059 reason to create a new node. */
28060 if (TREE_CODE (expr) == VOID_CST
28061 || TREE_CODE (expr) == INTEGER_CST
28062 || TREE_CODE (expr) == REAL_CST)
28063 return orig_expr;
28064 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28065 There is at least one place where we want to know that a
28066 particular expression is a throw-expression: when checking a ?:
28067 expression, there are special rules if the second or third
28068 argument is a throw-expression. */
28069 if (TREE_CODE (expr) == THROW_EXPR)
28070 return orig_expr;
28072 /* Don't wrap an initializer list, we need to be able to look inside. */
28073 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
28074 return orig_expr;
28076 /* Don't wrap a dummy object, we need to be able to test for it. */
28077 if (is_dummy_object (expr))
28078 return orig_expr;
28080 if (TREE_CODE (expr) == COND_EXPR)
28081 return build3 (COND_EXPR,
28082 TREE_TYPE (expr),
28083 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
28084 (TREE_OPERAND (expr, 1)
28085 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
28086 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
28087 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
28088 if (TREE_CODE (expr) == COMPOUND_EXPR
28089 && !COMPOUND_EXPR_OVERLOADED (expr))
28090 return build2 (COMPOUND_EXPR,
28091 TREE_TYPE (expr),
28092 TREE_OPERAND (expr, 0),
28093 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
28095 /* If the type is unknown, it can't really be non-dependent */
28096 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
28098 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28099 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
28100 TREE_TYPE (expr), expr);
28103 /* ARGS is a vector of expressions as arguments to a function call.
28104 Replace the arguments with equivalent non-dependent expressions.
28105 This modifies ARGS in place. */
28107 void
28108 make_args_non_dependent (vec<tree, va_gc> *args)
28110 unsigned int ix;
28111 tree arg;
28113 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
28115 tree newarg = build_non_dependent_expr (arg);
28116 if (newarg != arg)
28117 (*args)[ix] = newarg;
28121 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28122 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
28123 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
28125 static tree
28126 make_auto_1 (tree name, bool set_canonical)
28128 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
28129 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
28130 TYPE_STUB_DECL (au) = TYPE_NAME (au);
28131 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28132 (0, processing_template_decl + 1, processing_template_decl + 1,
28133 TYPE_NAME (au), NULL_TREE);
28134 if (set_canonical)
28135 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28136 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28137 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28138 if (name == decltype_auto_identifier)
28139 AUTO_IS_DECLTYPE (au) = true;
28141 return au;
28144 tree
28145 make_decltype_auto (void)
28147 return make_auto_1 (decltype_auto_identifier, true);
28150 tree
28151 make_auto (void)
28153 return make_auto_1 (auto_identifier, true);
28156 /* Return a C++17 deduction placeholder for class template TMPL. */
28158 tree
28159 make_template_placeholder (tree tmpl)
28161 tree t = make_auto_1 (auto_identifier, false);
28162 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28163 /* Our canonical type depends on the placeholder. */
28164 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28165 return t;
28168 /* True iff T is a C++17 class template deduction placeholder. */
28170 bool
28171 template_placeholder_p (tree t)
28173 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28176 /* Make a "constrained auto" type-specifier. This is an auto or
28177 decltype(auto) type with constraints that must be associated after
28178 deduction. The constraint is formed from the given concept CON
28179 and its optional sequence of template arguments ARGS.
28181 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28183 static tree
28184 make_constrained_placeholder_type (tree type, tree con, tree args)
28186 /* Build the constraint. */
28187 tree tmpl = DECL_TI_TEMPLATE (con);
28188 tree expr = tmpl;
28189 if (TREE_CODE (con) == FUNCTION_DECL)
28190 expr = ovl_make (tmpl);
28191 ++processing_template_decl;
28192 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28193 --processing_template_decl;
28195 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
28196 = build_tree_list (current_template_parms, expr);
28198 /* Our canonical type depends on the constraint. */
28199 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28201 /* Attach the constraint to the type declaration. */
28202 return TYPE_NAME (type);
28205 /* Make a "constrained auto" type-specifier. */
28207 tree
28208 make_constrained_auto (tree con, tree args)
28210 tree type = make_auto_1 (auto_identifier, false);
28211 return make_constrained_placeholder_type (type, con, args);
28214 /* Make a "constrained decltype(auto)" type-specifier. */
28216 tree
28217 make_constrained_decltype_auto (tree con, tree args)
28219 tree type = make_auto_1 (decltype_auto_identifier, false);
28220 return make_constrained_placeholder_type (type, con, args);
28223 /* Returns true if the placeholder type constraint T has any dependent
28224 (explicit) template arguments. */
28226 static bool
28227 placeholder_type_constraint_dependent_p (tree t)
28229 tree id = unpack_concept_check (t);
28230 tree args = TREE_OPERAND (id, 1);
28231 tree first = TREE_VEC_ELT (args, 0);
28232 if (ARGUMENT_PACK_P (first))
28234 args = expand_template_argument_pack (args);
28235 first = TREE_VEC_ELT (args, 0);
28237 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
28238 || is_auto (first));
28239 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
28240 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
28241 return true;
28242 return false;
28245 /* Build and return a concept definition. Like other templates, the
28246 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28247 the TEMPLATE_DECL. */
28249 tree
28250 finish_concept_definition (cp_expr id, tree init)
28252 gcc_assert (identifier_p (id));
28253 gcc_assert (processing_template_decl);
28255 location_t loc = id.get_location();
28257 /* A concept-definition shall not have associated constraints. */
28258 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28260 error_at (loc, "a concept cannot be constrained");
28261 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28264 /* A concept-definition shall appear in namespace scope. Templates
28265 aren't allowed in block scope, so we only need to check for class
28266 scope. */
28267 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28269 error_at (loc, "concept %qE not in namespace scope", *id);
28270 return error_mark_node;
28273 /* Initially build the concept declaration; its type is bool. */
28274 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28275 DECL_CONTEXT (decl) = current_scope ();
28276 DECL_INITIAL (decl) = init;
28278 set_originating_module (decl, false);
28280 /* Push the enclosing template. */
28281 return push_template_decl (decl);
28284 /* Given type ARG, return std::initializer_list<ARG>. */
28286 static tree
28287 listify (tree arg)
28289 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28291 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28293 gcc_rich_location richloc (input_location);
28294 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28295 error_at (&richloc,
28296 "deducing from brace-enclosed initializer list"
28297 " requires %<#include <initializer_list>%>");
28299 return error_mark_node;
28301 tree argvec = make_tree_vec (1);
28302 TREE_VEC_ELT (argvec, 0) = arg;
28304 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28305 NULL_TREE, 0, tf_warning_or_error);
28308 /* Replace auto in TYPE with std::initializer_list<auto>. */
28310 static tree
28311 listify_autos (tree type, tree auto_node)
28313 tree init_auto = listify (strip_top_quals (auto_node));
28314 tree argvec = make_tree_vec (1);
28315 TREE_VEC_ELT (argvec, 0) = init_auto;
28316 if (processing_template_decl)
28317 argvec = add_to_template_args (current_template_args (), argvec);
28318 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28321 /* Hash traits for hashing possibly constrained 'auto'
28322 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28324 struct auto_hash : default_hash_traits<tree>
28326 static inline hashval_t hash (tree);
28327 static inline bool equal (tree, tree);
28330 /* Hash the 'auto' T. */
28332 inline hashval_t
28333 auto_hash::hash (tree t)
28335 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28336 /* Matching constrained-type-specifiers denote the same template
28337 parameter, so hash the constraint. */
28338 return hash_placeholder_constraint (c);
28339 else
28340 /* But unconstrained autos are all separate, so just hash the pointer. */
28341 return iterative_hash_object (t, 0);
28344 /* Compare two 'auto's. */
28346 inline bool
28347 auto_hash::equal (tree t1, tree t2)
28349 if (t1 == t2)
28350 return true;
28352 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28353 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28355 /* Two unconstrained autos are distinct. */
28356 if (!c1 || !c2)
28357 return false;
28359 return equivalent_placeholder_constraints (c1, c2);
28362 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28363 constrained) auto, add it to the vector. */
28365 static int
28366 extract_autos_r (tree t, void *data)
28368 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28369 if (is_auto (t))
28371 /* All the autos were built with index 0; fix that up now. */
28372 tree *p = hash.find_slot (t, INSERT);
28373 unsigned idx;
28374 if (*p)
28375 /* If this is a repeated constrained-type-specifier, use the index we
28376 chose before. */
28377 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28378 else
28380 /* Otherwise this is new, so use the current count. */
28381 *p = t;
28382 idx = hash.elements () - 1;
28384 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28387 /* Always keep walking. */
28388 return 0;
28391 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28392 says they can appear anywhere in the type. */
28394 static tree
28395 extract_autos (tree type)
28397 hash_set<tree> visited;
28398 hash_table<auto_hash> hash (2);
28400 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28402 tree tree_vec = make_tree_vec (hash.elements());
28403 for (hash_table<auto_hash>::iterator iter = hash.begin();
28404 iter != hash.end(); ++iter)
28406 tree elt = *iter;
28407 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28408 TREE_VEC_ELT (tree_vec, i)
28409 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28412 return tree_vec;
28415 /* The stem for deduction guide names. */
28416 const char *const dguide_base = "__dguide_";
28418 /* Return the name for a deduction guide for class template TMPL. */
28420 tree
28421 dguide_name (tree tmpl)
28423 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28424 tree tname = TYPE_IDENTIFIER (type);
28425 char *buf = (char *) alloca (1 + strlen (dguide_base)
28426 + IDENTIFIER_LENGTH (tname));
28427 memcpy (buf, dguide_base, strlen (dguide_base));
28428 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28429 IDENTIFIER_LENGTH (tname) + 1);
28430 tree dname = get_identifier (buf);
28431 TREE_TYPE (dname) = type;
28432 return dname;
28435 /* True if NAME is the name of a deduction guide. */
28437 bool
28438 dguide_name_p (tree name)
28440 return (TREE_CODE (name) == IDENTIFIER_NODE
28441 && TREE_TYPE (name)
28442 && startswith (IDENTIFIER_POINTER (name), dguide_base));
28445 /* True if FN is a deduction guide. */
28447 bool
28448 deduction_guide_p (const_tree fn)
28450 if (DECL_P (fn))
28451 if (tree name = DECL_NAME (fn))
28452 return dguide_name_p (name);
28453 return false;
28456 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28458 bool
28459 copy_guide_p (const_tree fn)
28461 gcc_assert (deduction_guide_p (fn));
28462 if (!DECL_ARTIFICIAL (fn))
28463 return false;
28464 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28465 return (TREE_CHAIN (parms) == void_list_node
28466 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28469 /* True if FN is a guide generated from a constructor template. */
28471 bool
28472 template_guide_p (const_tree fn)
28474 gcc_assert (deduction_guide_p (fn));
28475 if (!DECL_ARTIFICIAL (fn))
28476 return false;
28477 tree tmpl = DECL_TI_TEMPLATE (fn);
28478 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28479 return PRIMARY_TEMPLATE_P (org);
28480 return false;
28483 /* True if FN is an aggregate initialization guide or the copy deduction
28484 guide. */
28486 bool
28487 builtin_guide_p (const_tree fn)
28489 if (!deduction_guide_p (fn))
28490 return false;
28491 if (!DECL_ARTIFICIAL (fn))
28492 /* Explicitly declared. */
28493 return false;
28494 if (DECL_ABSTRACT_ORIGIN (fn))
28495 /* Derived from a constructor. */
28496 return false;
28497 return true;
28500 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28501 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28502 template parameter types. Note that the handling of template template
28503 parameters relies on current_template_parms being set appropriately for the
28504 new template. */
28506 static tree
28507 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28508 tree tsubst_args, tsubst_flags_t complain)
28510 if (olddecl == error_mark_node)
28511 return error_mark_node;
28513 tree oldidx = get_template_parm_index (olddecl);
28515 tree newtype;
28516 if (TREE_CODE (olddecl) == TYPE_DECL
28517 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28519 tree oldtype = TREE_TYPE (olddecl);
28520 newtype = cxx_make_type (TREE_CODE (oldtype));
28521 TYPE_MAIN_VARIANT (newtype) = newtype;
28523 else
28525 newtype = TREE_TYPE (olddecl);
28526 if (type_uses_auto (newtype))
28528 // Substitute once to fix references to other template parameters.
28529 newtype = tsubst (newtype, tsubst_args,
28530 complain|tf_partial, NULL_TREE);
28531 // Now substitute again to reduce the level of the auto.
28532 newtype = tsubst (newtype, current_template_args (),
28533 complain, NULL_TREE);
28535 else
28536 newtype = tsubst (newtype, tsubst_args,
28537 complain, NULL_TREE);
28540 tree newdecl
28541 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28542 DECL_NAME (olddecl), newtype);
28543 SET_DECL_TEMPLATE_PARM_P (newdecl);
28545 tree newidx;
28546 if (TREE_CODE (olddecl) == TYPE_DECL
28547 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28549 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28550 = build_template_parm_index (index, level, level,
28551 newdecl, newtype);
28552 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28553 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28554 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28555 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28556 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28557 else
28558 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28560 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28562 DECL_TEMPLATE_RESULT (newdecl)
28563 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28564 DECL_NAME (olddecl), newtype);
28565 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28566 // First create a copy (ttargs) of tsubst_args with an
28567 // additional level for the template template parameter's own
28568 // template parameters (ttparms).
28569 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28570 (DECL_TEMPLATE_PARMS (olddecl)));
28571 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28572 tree ttargs = make_tree_vec (depth + 1);
28573 for (int i = 0; i < depth; ++i)
28574 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28575 TREE_VEC_ELT (ttargs, depth)
28576 = template_parms_level_to_args (ttparms);
28577 // Substitute ttargs into ttparms to fix references to
28578 // other template parameters.
28579 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28580 complain|tf_partial);
28581 // Now substitute again with args based on tparms, to reduce
28582 // the level of the ttparms.
28583 ttargs = current_template_args ();
28584 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28585 complain);
28586 // Finally, tack the adjusted parms onto tparms.
28587 ttparms = tree_cons (size_int (depth), ttparms,
28588 current_template_parms);
28589 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28592 else
28594 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28595 tree newconst
28596 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28597 TREE_CODE (oldconst),
28598 DECL_NAME (oldconst), newtype);
28599 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28600 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28601 SET_DECL_TEMPLATE_PARM_P (newconst);
28602 newidx = build_template_parm_index (index, level, level,
28603 newconst, newtype);
28604 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28605 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28606 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28609 return newdecl;
28612 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28613 template parameter. */
28615 static tree
28616 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28617 tree targs, unsigned targs_index, tsubst_flags_t complain)
28619 tree olddecl = TREE_VALUE (oldelt);
28620 tree newdecl = rewrite_template_parm (olddecl, index, level,
28621 targs, complain);
28622 if (newdecl == error_mark_node)
28623 return error_mark_node;
28624 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28625 targs, complain, NULL_TREE);
28626 tree list = build_tree_list (newdef, newdecl);
28627 TEMPLATE_PARM_CONSTRAINTS (list)
28628 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28629 targs, complain, NULL_TREE);
28630 int depth = TMPL_ARGS_DEPTH (targs);
28631 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28632 return list;
28635 /* Returns a C++17 class deduction guide template based on the constructor
28636 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28637 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28638 aggregate initialization guide. OUTER_ARGS are the template arguments
28639 for the enclosing scope of the class. */
28641 static tree
28642 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28644 tree tparms, targs, fparms, fargs, ci;
28645 bool memtmpl = false;
28646 bool explicit_p;
28647 location_t loc;
28648 tree fn_tmpl = NULL_TREE;
28650 if (outer_args)
28652 ++processing_template_decl;
28653 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28654 --processing_template_decl;
28657 if (!DECL_DECLARES_FUNCTION_P (ctor))
28659 if (TYPE_P (ctor))
28661 bool copy_p = TYPE_REF_P (ctor);
28662 if (copy_p)
28663 fparms = tree_cons (NULL_TREE, type, void_list_node);
28664 else
28665 fparms = void_list_node;
28667 else if (TREE_CODE (ctor) == TREE_LIST)
28668 fparms = ctor;
28669 else
28670 gcc_unreachable ();
28672 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28673 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28674 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
28675 ci = NULL_TREE;
28676 fargs = NULL_TREE;
28677 loc = DECL_SOURCE_LOCATION (ctmpl);
28678 explicit_p = false;
28680 else
28682 ++processing_template_decl;
28683 bool ok = true;
28685 fn_tmpl
28686 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28687 : DECL_TI_TEMPLATE (ctor));
28688 if (outer_args)
28689 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28690 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28692 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28693 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28694 fully specialized args for the enclosing class. Strip those off, as
28695 the deduction guide won't have those template parameters. */
28696 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28697 TMPL_PARMS_DEPTH (tparms));
28698 /* Discard the 'this' parameter. */
28699 fparms = FUNCTION_ARG_CHAIN (ctor);
28700 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28701 ci = get_constraints (ctor);
28702 loc = DECL_SOURCE_LOCATION (ctor);
28703 explicit_p = DECL_NONCONVERTING_P (ctor);
28705 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28707 memtmpl = true;
28709 /* For a member template constructor, we need to flatten the two
28710 template parameter lists into one, and then adjust the function
28711 signature accordingly. This gets...complicated. */
28712 tree save_parms = current_template_parms;
28714 /* For a member template we should have two levels of parms/args, one
28715 for the class and one for the constructor. We stripped
28716 specialized args for further enclosing classes above. */
28717 const int depth = 2;
28718 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28720 /* Template args for translating references to the two-level template
28721 parameters into references to the one-level template parameters we
28722 are creating. */
28723 tree tsubst_args = copy_node (targs);
28724 TMPL_ARGS_LEVEL (tsubst_args, depth)
28725 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28727 /* Template parms for the constructor template. */
28728 tree ftparms = TREE_VALUE (tparms);
28729 unsigned flen = TREE_VEC_LENGTH (ftparms);
28730 /* Template parms for the class template. */
28731 tparms = TREE_CHAIN (tparms);
28732 tree ctparms = TREE_VALUE (tparms);
28733 unsigned clen = TREE_VEC_LENGTH (ctparms);
28734 /* Template parms for the deduction guide start as a copy of the
28735 template parms for the class. We set current_template_parms for
28736 lookup_template_class_1. */
28737 current_template_parms = tparms = copy_node (tparms);
28738 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28739 for (unsigned i = 0; i < clen; ++i)
28740 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28742 /* Now we need to rewrite the constructor parms to append them to the
28743 class parms. */
28744 for (unsigned i = 0; i < flen; ++i)
28746 unsigned index = i + clen;
28747 unsigned level = 1;
28748 tree oldelt = TREE_VEC_ELT (ftparms, i);
28749 tree newelt
28750 = rewrite_tparm_list (oldelt, index, level,
28751 tsubst_args, i, complain);
28752 if (newelt == error_mark_node)
28753 ok = false;
28754 TREE_VEC_ELT (new_vec, index) = newelt;
28757 /* Now we have a final set of template parms to substitute into the
28758 function signature. */
28759 targs = template_parms_to_args (tparms);
28760 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28761 complain, ctor);
28762 if (fparms == error_mark_node)
28763 ok = false;
28764 if (ci)
28766 if (outer_args)
28767 /* FIXME: We'd like to avoid substituting outer template
28768 arguments into the constraint ahead of time, but the
28769 construction of tsubst_args assumes that outer arguments
28770 are already substituted in. */
28771 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28772 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28775 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28776 cp_unevaluated_operand. */
28777 cp_evaluated ev;
28778 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28779 current_template_parms = save_parms;
28781 else
28783 /* Substitute in the same arguments to rewrite class members into
28784 references to members of an unknown specialization. */
28785 cp_evaluated ev;
28786 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28787 fargs = tsubst (fargs, targs, complain, ctor);
28788 if (ci)
28790 if (outer_args)
28791 /* FIXME: As above. */
28792 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28793 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28797 --processing_template_decl;
28798 if (!ok)
28799 return error_mark_node;
28802 if (!memtmpl)
28804 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28805 tparms = copy_node (tparms);
28806 INNERMOST_TEMPLATE_PARMS (tparms)
28807 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28810 tree fntype = build_function_type (type, fparms);
28811 tree ded_fn = build_lang_decl_loc (loc,
28812 FUNCTION_DECL,
28813 dguide_name (type), fntype);
28814 DECL_ARGUMENTS (ded_fn) = fargs;
28815 DECL_ARTIFICIAL (ded_fn) = true;
28816 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28817 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28818 DECL_ARTIFICIAL (ded_tmpl) = true;
28819 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28820 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28821 if (DECL_P (ctor))
28822 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28823 if (ci)
28824 set_constraints (ded_tmpl, ci);
28826 return ded_tmpl;
28829 /* Add to LIST the member types for the reshaped initializer CTOR. */
28831 static tree
28832 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28834 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28835 tree idx, val; unsigned i;
28836 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28838 tree ftype = elt ? elt : TREE_TYPE (idx);
28839 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28840 && CONSTRUCTOR_BRACES_ELIDED_P (val))
28842 tree subelt = NULL_TREE;
28843 if (TREE_CODE (ftype) == ARRAY_TYPE)
28844 subelt = TREE_TYPE (ftype);
28845 list = collect_ctor_idx_types (val, list, subelt);
28846 continue;
28848 tree arg = NULL_TREE;
28849 if (i == v->length() - 1
28850 && PACK_EXPANSION_P (ftype))
28851 /* Give the trailing pack expansion parameter a default argument to
28852 match aggregate initialization behavior, even if we deduce the
28853 length of the pack separately to more than we have initializers. */
28854 arg = build_constructor (init_list_type_node, NULL);
28855 /* if ei is of array type and xi is a braced-init-list or string literal,
28856 Ti is an rvalue reference to the declared type of ei */
28857 STRIP_ANY_LOCATION_WRAPPER (val);
28858 if (TREE_CODE (ftype) == ARRAY_TYPE
28859 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28860 || TREE_CODE (val) == STRING_CST))
28862 if (TREE_CODE (val) == STRING_CST)
28863 ftype = cp_build_qualified_type
28864 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28865 ftype = (cp_build_reference_type
28866 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28868 list = tree_cons (arg, ftype, list);
28871 return list;
28874 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28876 static bool
28877 is_spec_or_derived (tree etype, tree tmpl)
28879 if (!etype || !CLASS_TYPE_P (etype))
28880 return false;
28882 etype = cv_unqualified (etype);
28883 tree type = TREE_TYPE (tmpl);
28884 tree tparms = (INNERMOST_TEMPLATE_PARMS
28885 (DECL_TEMPLATE_PARMS (tmpl)));
28886 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28887 int err = unify (tparms, targs, type, etype,
28888 UNIFY_ALLOW_DERIVED, /*explain*/false);
28889 ggc_free (targs);
28890 return !err;
28893 static tree alias_ctad_tweaks (tree, tree);
28895 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28896 INIT. */
28898 static tree
28899 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28901 if (cxx_dialect < cxx20)
28902 return NULL_TREE;
28904 if (init == NULL_TREE)
28905 return NULL_TREE;
28907 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28909 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28910 tree tinfo = get_template_info (under);
28911 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
28912 return alias_ctad_tweaks (tmpl, guide);
28913 return NULL_TREE;
28916 /* We might be creating a guide for a class member template, e.g.,
28918 template<typename U> struct A {
28919 template<typename T> struct B { T t; };
28922 At this point, A will have been instantiated. Below, we need to
28923 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
28924 const bool member_template_p
28925 = (DECL_TEMPLATE_INFO (tmpl)
28926 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
28927 tree type = TREE_TYPE (tmpl);
28928 tree template_type = (member_template_p
28929 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
28930 : type);
28931 if (!CP_AGGREGATE_TYPE_P (template_type))
28932 return NULL_TREE;
28934 /* No aggregate candidate for copy-initialization. */
28935 if (args->length() == 1)
28937 tree val = (*args)[0];
28938 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
28939 return NULL_TREE;
28942 /* If we encounter a problem, we just won't add the candidate. */
28943 tsubst_flags_t complain = tf_none;
28945 tree parms = NULL_TREE;
28946 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28948 init = reshape_init (template_type, init, complain);
28949 if (init == error_mark_node)
28950 return NULL_TREE;
28951 parms = collect_ctor_idx_types (init, parms);
28952 /* If we're creating a deduction guide for a member class template,
28953 we've used the original template pattern type for the reshape_init
28954 above; this is done because we want PARMS to be a template parameter
28955 type, something that can be deduced when used as a function template
28956 parameter. At this point the outer class template has already been
28957 partially instantiated (we deferred the deduction until the enclosing
28958 scope is non-dependent). Therefore we have to partially instantiate
28959 PARMS, so that its template level is properly reduced and we don't get
28960 mismatches when deducing types using the guide with PARMS. */
28961 if (member_template_p)
28962 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
28964 else if (TREE_CODE (init) == TREE_LIST)
28966 int len = list_length (init);
28967 for (tree field = TYPE_FIELDS (type);
28968 len;
28969 --len, field = DECL_CHAIN (field))
28971 field = next_initializable_field (field);
28972 if (!field)
28973 return NULL_TREE;
28974 tree ftype = finish_decltype_type (field, true, complain);
28975 parms = tree_cons (NULL_TREE, ftype, parms);
28978 else
28979 /* Aggregate initialization doesn't apply to an initializer expression. */
28980 return NULL_TREE;
28982 if (parms)
28984 tree last = parms;
28985 parms = nreverse (parms);
28986 TREE_CHAIN (last) = void_list_node;
28987 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28988 return guide;
28991 return NULL_TREE;
28994 /* UGUIDES are the deduction guides for the underlying template of alias
28995 template TMPL; adjust them to be deduction guides for TMPL. */
28997 static tree
28998 alias_ctad_tweaks (tree tmpl, tree uguides)
29000 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29001 class type (9.2.8.2) where the template-name names an alias template A,
29002 the defining-type-id of A must be of the form
29004 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29006 as specified in 9.2.8.2. The guides of A are the set of functions or
29007 function templates formed as follows. For each function or function
29008 template f in the guides of the template named by the simple-template-id
29009 of the defining-type-id, the template arguments of the return type of f
29010 are deduced from the defining-type-id of A according to the process in
29011 13.10.2.5 with the exception that deduction does not fail if not all
29012 template arguments are deduced. Let g denote the result of substituting
29013 these deductions into f. If substitution succeeds, form a function or
29014 function template f' with the following properties and add it to the set
29015 of guides of A:
29017 * The function type of f' is the function type of g.
29019 * If f is a function template, f' is a function template whose template
29020 parameter list consists of all the template parameters of A (including
29021 their default template arguments) that appear in the above deductions or
29022 (recursively) in their default template arguments, followed by the
29023 template parameters of f that were not deduced (including their default
29024 template arguments), otherwise f' is not a function template.
29026 * The associated constraints (13.5.2) are the conjunction of the
29027 associated constraints of g and a constraint that is satisfied if and only
29028 if the arguments of A are deducible (see below) from the return type.
29030 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29031 be so as well.
29033 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29034 considered to be so as well.
29036 * The explicit-specifier of f' is the explicit-specifier of g (if
29037 any). */
29039 /* This implementation differs from the above in two significant ways:
29041 1) We include all template parameters of A, not just some.
29042 2) The added constraint is same_type instead of deducible.
29044 I believe that while it's probably possible to construct a testcase that
29045 behaves differently with this simplification, it should have the same
29046 effect for real uses. Including all template parameters means that we
29047 deduce all parameters of A when resolving the call, so when we're in the
29048 constraint we don't need to deduce them again, we can just check whether
29049 the deduction produced the desired result. */
29051 tsubst_flags_t complain = tf_warning_or_error;
29052 tree atype = TREE_TYPE (tmpl);
29053 tree aguides = NULL_TREE;
29054 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
29055 unsigned natparms = TREE_VEC_LENGTH (atparms);
29056 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29057 for (ovl_iterator iter (uguides); iter; ++iter)
29059 tree f = *iter;
29060 tree in_decl = f;
29061 location_t loc = DECL_SOURCE_LOCATION (f);
29062 tree ret = TREE_TYPE (TREE_TYPE (f));
29063 tree fprime = f;
29064 if (TREE_CODE (f) == TEMPLATE_DECL)
29066 processing_template_decl_sentinel ptds (/*reset*/false);
29067 ++processing_template_decl;
29069 /* Deduce template arguments for f from the type-id of A. */
29070 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
29071 unsigned len = TREE_VEC_LENGTH (ftparms);
29072 tree targs = make_tree_vec (len);
29073 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
29074 if (err)
29075 continue;
29077 /* The number of parms for f' is the number of parms for A plus
29078 non-deduced parms of f. */
29079 unsigned ndlen = 0;
29080 unsigned j;
29081 for (unsigned i = 0; i < len; ++i)
29082 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29083 ++ndlen;
29084 tree gtparms = make_tree_vec (natparms + ndlen);
29086 /* First copy over the parms of A. */
29087 for (j = 0; j < natparms; ++j)
29088 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
29089 /* Now rewrite the non-deduced parms of f. */
29090 for (unsigned i = 0; ndlen && i < len; ++i)
29091 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29093 --ndlen;
29094 unsigned index = j++;
29095 unsigned level = 1;
29096 tree oldlist = TREE_VEC_ELT (ftparms, i);
29097 tree list = rewrite_tparm_list (oldlist, index, level,
29098 targs, i, complain);
29099 TREE_VEC_ELT (gtparms, index) = list;
29101 gtparms = build_tree_list (size_one_node, gtparms);
29103 /* Substitute the deduced arguments plus the rewritten template
29104 parameters into f to get g. This covers the type, copyness,
29105 guideness, and explicit-specifier. */
29106 tree g;
29108 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
29109 if cp_unevaluated_operand. */
29110 cp_evaluated ev;
29111 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
29113 if (g == error_mark_node)
29114 continue;
29115 DECL_USE_TEMPLATE (g) = 0;
29116 fprime = build_template_decl (g, gtparms, false);
29117 DECL_TEMPLATE_RESULT (fprime) = g;
29118 TREE_TYPE (fprime) = TREE_TYPE (g);
29119 tree gtargs = template_parms_to_args (gtparms);
29120 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
29121 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
29123 /* Substitute the associated constraints. */
29124 tree ci = get_constraints (f);
29125 if (ci)
29126 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
29127 if (ci == error_mark_node)
29128 continue;
29130 /* Add a constraint that the return type matches the instantiation of
29131 A with the same template arguments. */
29132 ret = TREE_TYPE (TREE_TYPE (fprime));
29133 if (!same_type_p (atype, ret)
29134 /* FIXME this should mean they don't compare as equivalent. */
29135 || dependent_alias_template_spec_p (atype, nt_opaque))
29137 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
29138 ci = append_constraint (ci, same);
29141 if (ci)
29143 remove_constraints (fprime);
29144 set_constraints (fprime, ci);
29147 else
29149 /* For a non-template deduction guide, if the arguments of A aren't
29150 deducible from the return type, don't add the candidate. */
29151 tree targs = make_tree_vec (natparms);
29152 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
29153 for (unsigned i = 0; !err && i < natparms; ++i)
29154 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29155 err = true;
29156 if (err)
29157 continue;
29160 aguides = lookup_add (fprime, aguides);
29163 return aguides;
29166 /* Return artificial deduction guides built from the constructors of class
29167 template TMPL. */
29169 static tree
29170 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
29172 tree type = TREE_TYPE (tmpl);
29173 tree outer_args = NULL_TREE;
29174 if (DECL_CLASS_SCOPE_P (tmpl)
29175 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
29177 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
29178 type = TREE_TYPE (most_general_template (tmpl));
29181 tree cands = NULL_TREE;
29183 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
29185 /* Skip inherited constructors. */
29186 if (iter.using_p ())
29187 continue;
29189 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
29190 cands = lookup_add (guide, cands);
29193 /* Add implicit default constructor deduction guide. */
29194 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
29196 tree guide = build_deduction_guide (type, type, outer_args,
29197 complain);
29198 cands = lookup_add (guide, cands);
29201 /* Add copy guide. */
29203 tree gtype = build_reference_type (type);
29204 tree guide = build_deduction_guide (type, gtype, outer_args,
29205 complain);
29206 cands = lookup_add (guide, cands);
29209 return cands;
29212 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29214 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29215 aggregate candidate is added separately because it depends on the
29216 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29217 guide. */
29219 static tree
29220 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29222 tree guides = NULL_TREE;
29223 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29225 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29226 tree tinfo = get_template_info (under);
29227 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29228 complain);
29230 else
29232 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29233 dguide_name (tmpl),
29234 LOOK_want::NORMAL, /*complain*/false);
29235 if (guides == error_mark_node)
29236 guides = NULL_TREE;
29237 else
29238 any_dguides_p = true;
29241 /* Cache the deduction guides for a template. We also remember the result of
29242 lookup, and rebuild everything if it changes; should be very rare. */
29243 tree_pair_p cache = NULL;
29244 if (tree_pair_p &r
29245 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29247 cache = r;
29248 if (cache->purpose == guides)
29249 return cache->value;
29251 else
29253 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29254 cache->purpose = guides;
29257 tree cands = NULL_TREE;
29258 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29259 cands = alias_ctad_tweaks (tmpl, guides);
29260 else
29262 cands = ctor_deduction_guides_for (tmpl, complain);
29263 for (ovl_iterator it (guides); it; ++it)
29264 cands = lookup_add (*it, cands);
29267 cache->value = cands;
29268 return cands;
29271 /* Return whether TMPL is a (class template argument-) deducible template. */
29273 bool
29274 ctad_template_p (tree tmpl)
29276 /* A deducible template is either a class template or is an alias template
29277 whose defining-type-id is of the form
29279 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29281 where the nested-name-specifier (if any) is non-dependent and the
29282 template-name of the simple-template-id names a deducible template. */
29284 if (DECL_CLASS_TEMPLATE_P (tmpl)
29285 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29286 return true;
29287 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29288 return false;
29289 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29290 if (tree tinfo = get_template_info (orig))
29291 return ctad_template_p (TI_TEMPLATE (tinfo));
29292 return false;
29295 /* Deduce template arguments for the class template placeholder PTYPE for
29296 template TMPL based on the initializer INIT, and return the resulting
29297 type. */
29299 static tree
29300 do_class_deduction (tree ptype, tree tmpl, tree init,
29301 int flags, tsubst_flags_t complain)
29303 /* We should have handled this in the caller. */
29304 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29305 return ptype;
29307 /* Wait until the enclosing scope is non-dependent. */
29308 if (DECL_CLASS_SCOPE_P (tmpl)
29309 && dependent_type_p (DECL_CONTEXT (tmpl)))
29310 return ptype;
29312 /* Initializing one placeholder from another. */
29313 if (init
29314 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
29315 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
29316 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
29317 == TEMPLATE_PARM_INDEX)))
29318 && is_auto (TREE_TYPE (init))
29319 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29320 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29322 /* Look through alias templates that just rename another template. */
29323 tmpl = get_underlying_template (tmpl);
29324 if (!ctad_template_p (tmpl))
29326 if (complain & tf_error)
29327 error ("non-deducible template %qT used without template arguments", tmpl);
29328 return error_mark_node;
29330 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29332 if (complain & tf_error)
29333 error ("alias template deduction only available "
29334 "with %<-std=c++20%> or %<-std=gnu++20%>");
29335 return error_mark_node;
29338 /* Wait until the initializer is non-dependent. */
29339 if (type_dependent_expression_p (init))
29340 return ptype;
29342 tree type = TREE_TYPE (tmpl);
29344 bool try_list_ctor = false;
29345 bool list_init_p = false;
29347 releasing_vec rv_args = NULL;
29348 vec<tree,va_gc> *&args = *&rv_args;
29349 if (init == NULL_TREE)
29350 args = make_tree_vector ();
29351 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29353 list_init_p = true;
29354 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29355 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
29356 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
29358 /* As an exception, the first phase in 16.3.1.7 (considering the
29359 initializer list as a single argument) is omitted if the
29360 initializer list consists of a single expression of type cv U,
29361 where U is a specialization of C or a class derived from a
29362 specialization of C. */
29363 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29364 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29365 try_list_ctor = false;
29367 if (try_list_ctor || is_std_init_list (type))
29368 args = make_tree_vector_single (init);
29369 else
29370 args = make_tree_vector_from_ctor (init);
29372 else if (TREE_CODE (init) == TREE_LIST)
29373 args = make_tree_vector_from_list (init);
29374 else
29375 args = make_tree_vector_single (init);
29377 /* Do this now to avoid problems with erroneous args later on. */
29378 args = resolve_args (args, complain);
29379 if (args == NULL)
29380 return error_mark_node;
29382 bool any_dguides_p = false;
29383 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29384 if (cands == error_mark_node)
29385 return error_mark_node;
29387 /* Prune explicit deduction guides in copy-initialization context (but
29388 not copy-list-initialization). */
29389 bool elided = false;
29390 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29392 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29393 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29394 elided = true;
29396 if (elided)
29398 /* Found a nonconverting guide, prune the candidates. */
29399 tree pruned = NULL_TREE;
29400 for (lkp_iterator iter (cands); iter; ++iter)
29401 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29402 pruned = lookup_add (*iter, pruned);
29404 cands = pruned;
29408 if (!any_dguides_p)
29409 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29410 cands = lookup_add (guide, cands);
29412 tree fndecl = error_mark_node;
29414 /* If this is list-initialization and the class has a list constructor, first
29415 try deducing from the list as a single argument, as [over.match.list]. */
29416 tree list_cands = NULL_TREE;
29417 if (try_list_ctor && cands)
29418 for (lkp_iterator iter (cands); iter; ++iter)
29420 tree dg = *iter;
29421 if (is_list_ctor (dg))
29422 list_cands = lookup_add (dg, list_cands);
29424 if (list_cands)
29426 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
29428 if (fndecl == error_mark_node)
29430 /* That didn't work, now try treating the list as a sequence of
29431 arguments. */
29432 release_tree_vector (args);
29433 args = make_tree_vector_from_ctor (init);
29437 if (elided && !cands)
29439 error ("cannot deduce template arguments for copy-initialization"
29440 " of %qT, as it has no non-explicit deduction guides or "
29441 "user-declared constructors", type);
29442 return error_mark_node;
29444 else if (!cands && fndecl == error_mark_node)
29446 error ("cannot deduce template arguments of %qT, as it has no viable "
29447 "deduction guides", type);
29448 return error_mark_node;
29451 if (fndecl == error_mark_node)
29452 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
29454 if (fndecl == error_mark_node)
29456 if (complain & tf_warning_or_error)
29458 error ("class template argument deduction failed:");
29459 perform_dguide_overload_resolution (cands, args, complain);
29460 if (elided)
29461 inform (input_location, "explicit deduction guides not considered "
29462 "for copy-initialization");
29464 return error_mark_node;
29466 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29467 constructor is chosen, the initialization is ill-formed. */
29468 else if (flags & LOOKUP_ONLYCONVERTING)
29470 if (DECL_NONCONVERTING_P (fndecl))
29472 if (complain & tf_warning_or_error)
29474 // TODO: Pass down location from cp_finish_decl.
29475 error ("class template argument deduction for %qT failed: "
29476 "explicit deduction guide selected in "
29477 "copy-list-initialization", type);
29478 inform (DECL_SOURCE_LOCATION (fndecl),
29479 "explicit deduction guide declared here");
29482 return error_mark_node;
29486 /* If CTAD succeeded but the type doesn't have any explicit deduction
29487 guides, this deduction might not be what the user intended. */
29488 if (fndecl != error_mark_node && !any_dguides_p)
29490 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
29491 || global_dc->dc_warn_system_headers)
29492 && warning (OPT_Wctad_maybe_unsupported,
29493 "%qT may not intend to support class template argument "
29494 "deduction", type))
29495 inform (input_location, "add a deduction guide to suppress this "
29496 "warning");
29499 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
29500 cp_type_quals (ptype));
29503 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29504 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29505 The CONTEXT determines the context in which auto deduction is performed
29506 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29508 OUTER_TARGS is used during template argument deduction (context == adc_unify)
29509 to properly substitute the result. It's also used in the adc_unify and
29510 adc_requirement contexts to communicate the the necessary template arguments
29511 to satisfaction. OUTER_TARGS is ignored in other contexts.
29513 For partial-concept-ids, extra args may be appended to the list of deduced
29514 template arguments prior to determining constraint satisfaction. */
29516 tree
29517 do_auto_deduction (tree type, tree init, tree auto_node,
29518 tsubst_flags_t complain, auto_deduction_context context,
29519 tree outer_targs, int flags)
29521 if (init == error_mark_node)
29522 return error_mark_node;
29524 if (init && type_dependent_expression_p (init)
29525 && context != adc_unify)
29526 /* Defining a subset of type-dependent expressions that we can deduce
29527 from ahead of time isn't worth the trouble. */
29528 return type;
29530 /* Similarly, we can't deduce from another undeduced decl. */
29531 if (init && undeduced_auto_decl (init))
29532 return type;
29534 /* We may be doing a partial substitution, but we still want to replace
29535 auto_node. */
29536 complain &= ~tf_partial;
29538 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29539 /* C++17 class template argument deduction. */
29540 return do_class_deduction (type, tmpl, init, flags, complain);
29542 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29543 /* Nothing we can do with this, even in deduction context. */
29544 return type;
29546 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29547 with either a new invented type template parameter U or, if the
29548 initializer is a braced-init-list (8.5.4), with
29549 std::initializer_list<U>. */
29550 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29552 if (!DIRECT_LIST_INIT_P (init))
29553 type = listify_autos (type, auto_node);
29554 else if (CONSTRUCTOR_NELTS (init) == 1)
29555 init = CONSTRUCTOR_ELT (init, 0)->value;
29556 else
29558 if (complain & tf_warning_or_error)
29560 if (permerror (input_location, "direct-list-initialization of "
29561 "%<auto%> requires exactly one element"))
29562 inform (input_location,
29563 "for deduction to %<std::initializer_list%>, use copy-"
29564 "list-initialization (i.e. add %<=%> before the %<{%>)");
29566 type = listify_autos (type, auto_node);
29570 if (type == error_mark_node)
29571 return error_mark_node;
29573 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29575 /* We don't recurse here because we can't deduce from a nested
29576 initializer_list. */
29577 if (CONSTRUCTOR_ELTS (init))
29578 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29579 elt.value = resolve_nondeduced_context (elt.value, complain);
29581 else
29582 init = resolve_nondeduced_context (init, complain);
29584 tree targs;
29585 if (context == adc_decomp_type
29586 && auto_node == type
29587 && init != error_mark_node
29588 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29590 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
29591 and initializer has array type, deduce cv-qualified array type. */
29592 targs = make_tree_vec (1);
29593 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
29595 else if (AUTO_IS_DECLTYPE (auto_node))
29597 tree stripped_init = tree_strip_any_location_wrapper (init);
29598 if (REFERENCE_REF_P (stripped_init))
29599 stripped_init = TREE_OPERAND (stripped_init, 0);
29600 bool id = (DECL_P (stripped_init)
29601 || ((TREE_CODE (init) == COMPONENT_REF
29602 || TREE_CODE (init) == SCOPE_REF)
29603 && !REF_PARENTHESIZED_P (init)));
29604 tree deduced = finish_decltype_type (init, id, complain);
29605 deduced = canonicalize_type_argument (deduced, complain);
29606 if (deduced == error_mark_node)
29607 return error_mark_node;
29608 targs = make_tree_vec (1);
29609 TREE_VEC_ELT (targs, 0) = deduced;
29610 /* FIXME: These errors ought to be diagnosed at parse time. */
29611 if (type != auto_node)
29613 if (complain & tf_error)
29614 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29615 return error_mark_node;
29617 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29619 if (complain & tf_error)
29620 error ("%<decltype(auto)%> cannot be cv-qualified");
29621 return error_mark_node;
29624 else
29626 if (error_operand_p (init))
29627 return error_mark_node;
29629 tree parms = build_tree_list (NULL_TREE, type);
29630 tree tparms;
29632 if (flag_concepts)
29633 tparms = extract_autos (type);
29634 else
29636 tparms = make_tree_vec (1);
29637 TREE_VEC_ELT (tparms, 0)
29638 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29641 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29642 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29643 DEDUCE_CALL,
29644 NULL, /*explain_p=*/false);
29645 if (val > 0)
29647 if (processing_template_decl)
29648 /* Try again at instantiation time. */
29649 return type;
29650 if (type && type != error_mark_node
29651 && (complain & tf_error))
29652 /* If type is error_mark_node a diagnostic must have been
29653 emitted by now. Also, having a mention to '<type error>'
29654 in the diagnostic is not really useful to the user. */
29656 if (cfun
29657 && FNDECL_USED_AUTO (current_function_decl)
29658 && (auto_node
29659 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29660 && LAMBDA_FUNCTION_P (current_function_decl))
29661 error ("unable to deduce lambda return type from %qE", init);
29662 else
29663 error ("unable to deduce %qT from %qE", type, init);
29664 type_unification_real (tparms, targs, parms, &init, 1, 0,
29665 DEDUCE_CALL,
29666 NULL, /*explain_p=*/true);
29668 return error_mark_node;
29672 /* Check any placeholder constraints against the deduced type. */
29673 if (processing_template_decl && context == adc_unify)
29674 /* Constraints will be checked after deduction. */;
29675 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29677 if (processing_template_decl)
29679 gcc_checking_assert (context == adc_variable_type
29680 || context == adc_return_type
29681 || context == adc_decomp_type);
29682 gcc_checking_assert (!type_dependent_expression_p (init));
29683 /* If the constraint is dependent, we need to wait until
29684 instantiation time to resolve the placeholder. */
29685 if (placeholder_type_constraint_dependent_p (constr))
29686 return type;
29689 if ((context == adc_return_type
29690 || context == adc_variable_type
29691 || context == adc_decomp_type)
29692 && current_function_decl
29693 && DECL_TEMPLATE_INFO (current_function_decl))
29694 outer_targs = DECL_TI_ARGS (current_function_decl);
29696 tree full_targs = add_to_template_args (outer_targs, targs);
29698 /* HACK: Compensate for callers not always communicating all levels of
29699 outer template arguments by filling in the outermost missing levels
29700 with dummy levels before checking satisfaction. We'll still crash
29701 if the constraint depends on a template argument belonging to one of
29702 these missing levels, but this hack otherwise allows us to handle a
29703 large subset of possible constraints (including all non-dependent
29704 constraints). */
29705 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
29706 - TMPL_ARGS_DEPTH (full_targs)))
29708 tree dummy_levels = make_tree_vec (missing_levels);
29709 for (int i = 0; i < missing_levels; ++i)
29710 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
29711 full_targs = add_to_template_args (dummy_levels, full_targs);
29714 if (!constraints_satisfied_p (auto_node, full_targs))
29716 if (complain & tf_warning_or_error)
29718 auto_diagnostic_group d;
29719 switch (context)
29721 case adc_unspecified:
29722 case adc_unify:
29723 error("placeholder constraints not satisfied");
29724 break;
29725 case adc_variable_type:
29726 case adc_decomp_type:
29727 error ("deduced initializer does not satisfy "
29728 "placeholder constraints");
29729 break;
29730 case adc_return_type:
29731 error ("deduced return type does not satisfy "
29732 "placeholder constraints");
29733 break;
29734 case adc_requirement:
29735 error ("deduced expression type does not satisfy "
29736 "placeholder constraints");
29737 break;
29739 diagnose_constraints (input_location, auto_node, full_targs);
29741 return error_mark_node;
29745 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
29746 /* The outer template arguments are already substituted into type
29747 (but we still may have used them for constraint checking above). */;
29748 else if (context == adc_unify)
29749 targs = add_to_template_args (outer_targs, targs);
29750 else if (processing_template_decl)
29751 targs = add_to_template_args (current_template_args (), targs);
29752 return tsubst (type, targs, complain, NULL_TREE);
29755 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29756 result. */
29758 tree
29759 splice_late_return_type (tree type, tree late_return_type)
29761 if (late_return_type)
29763 gcc_assert (is_auto (type) || seen_error ());
29764 return late_return_type;
29767 if (tree auto_node = find_type_usage (type, is_auto))
29768 if (TEMPLATE_TYPE_LEVEL (auto_node) <= processing_template_decl)
29770 /* In an abbreviated function template we didn't know we were dealing
29771 with a function template when we saw the auto return type, so rebuild
29772 the return type using an auto with the correct level. */
29773 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
29774 tree auto_vec = make_tree_vec (1);
29775 TREE_VEC_ELT (auto_vec, 0) = new_auto;
29776 tree targs = add_outermost_template_args (current_template_args (),
29777 auto_vec);
29778 /* Also rebuild the constraint info in terms of the new auto. */
29779 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
29780 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
29781 = build_tree_list (current_template_parms,
29782 tsubst_constraint (TREE_VALUE (ci), targs,
29783 tf_none, NULL_TREE));
29784 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29785 return tsubst (type, targs, tf_none, NULL_TREE);
29787 return type;
29790 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29791 'decltype(auto)' or a deduced class template. */
29793 bool
29794 is_auto (const_tree type)
29796 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29797 && (TYPE_IDENTIFIER (type) == auto_identifier
29798 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29799 return true;
29800 else
29801 return false;
29804 /* for_each_template_parm callback for type_uses_auto. */
29807 is_auto_r (tree tp, void */*data*/)
29809 return is_auto (tp);
29812 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29813 a use of `auto'. Returns NULL_TREE otherwise. */
29815 tree
29816 type_uses_auto (tree type)
29818 if (type == NULL_TREE)
29819 return NULL_TREE;
29820 else if (flag_concepts)
29822 /* The Concepts TS allows multiple autos in one type-specifier; just
29823 return the first one we find, do_auto_deduction will collect all of
29824 them. */
29825 if (uses_template_parms (type))
29826 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29827 /*visited*/NULL, /*nondeduced*/false);
29828 else
29829 return NULL_TREE;
29831 else
29832 return find_type_usage (type, is_auto);
29835 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29836 concepts are enabled, auto is acceptable in template arguments, but
29837 only when TEMPL identifies a template class. Return TRUE if any
29838 such errors were reported. */
29840 bool
29841 check_auto_in_tmpl_args (tree tmpl, tree args)
29843 /* If there were previous errors, nevermind. */
29844 if (!args || TREE_CODE (args) != TREE_VEC)
29845 return false;
29847 /* If TMPL is an identifier, we're parsing and we can't tell yet
29848 whether TMPL is supposed to be a type, a function or a variable.
29849 We'll only be able to tell during template substitution, so we
29850 expect to be called again then. If concepts are enabled and we
29851 know we have a type, we're ok. */
29852 if (flag_concepts
29853 && (identifier_p (tmpl)
29854 || (DECL_P (tmpl)
29855 && (DECL_TYPE_TEMPLATE_P (tmpl)
29856 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29857 return false;
29859 /* Quickly search for any occurrences of auto; usually there won't
29860 be any, and then we'll avoid allocating the vector. */
29861 if (!type_uses_auto (args))
29862 return false;
29864 bool errors = false;
29866 tree vec = extract_autos (args);
29867 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29869 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29870 error_at (DECL_SOURCE_LOCATION (xauto),
29871 "invalid use of %qT in template argument", xauto);
29872 errors = true;
29875 return errors;
29878 /* Recursively walk over && expressions searching for EXPR. Return a reference
29879 to that expression. */
29881 static tree *find_template_requirement (tree *t, tree key)
29883 if (*t == key)
29884 return t;
29885 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29887 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29888 return p;
29889 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29890 return p;
29892 return 0;
29895 /* Convert the generic type parameters in PARM that match the types given in the
29896 range [START_IDX, END_IDX) from the current_template_parms into generic type
29897 packs. */
29899 tree
29900 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29902 tree current = current_template_parms;
29903 int depth = TMPL_PARMS_DEPTH (current);
29904 current = INNERMOST_TEMPLATE_PARMS (current);
29905 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29907 for (int i = 0; i < start_idx; ++i)
29908 TREE_VEC_ELT (replacement, i)
29909 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29911 for (int i = start_idx; i < end_idx; ++i)
29913 /* Create a distinct parameter pack type from the current parm and add it
29914 to the replacement args to tsubst below into the generic function
29915 parameter. */
29916 tree node = TREE_VEC_ELT (current, i);
29917 tree o = TREE_TYPE (TREE_VALUE (node));
29918 tree t = copy_type (o);
29919 TEMPLATE_TYPE_PARM_INDEX (t)
29920 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29921 t, 0, 0, tf_none);
29922 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29923 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29924 TYPE_MAIN_VARIANT (t) = t;
29925 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29926 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29927 TREE_VEC_ELT (replacement, i) = t;
29929 /* Replace the current template parameter with new pack. */
29930 TREE_VALUE (node) = TREE_CHAIN (t);
29932 /* Surgically adjust the associated constraint of adjusted parameter
29933 and it's corresponding contribution to the current template
29934 requirements. */
29935 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29937 tree id = unpack_concept_check (constr);
29938 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29939 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29940 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29942 /* If there was a constraint, we also need to replace that in
29943 the template requirements, which we've already built. */
29944 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29945 reqs = find_template_requirement (reqs, constr);
29946 *reqs = fold;
29950 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29951 TREE_VEC_ELT (replacement, i)
29952 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29954 /* If there are more levels then build up the replacement with the outer
29955 template parms. */
29956 if (depth > 1)
29957 replacement = add_to_template_args (template_parms_to_args
29958 (TREE_CHAIN (current_template_parms)),
29959 replacement);
29961 return tsubst (parm, replacement, tf_none, NULL_TREE);
29964 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29965 0..N-1. */
29967 void
29968 declare_integer_pack (void)
29970 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29971 build_function_type_list (integer_type_node,
29972 integer_type_node,
29973 NULL_TREE),
29974 NULL_TREE, ECF_CONST);
29975 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29976 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29977 CP_BUILT_IN_INTEGER_PACK);
29980 /* Walk the decl or type specialization table calling FN on each
29981 entry. */
29983 void
29984 walk_specializations (bool decls_p,
29985 void (*fn) (bool decls_p, spec_entry *entry, void *data),
29986 void *data)
29988 spec_hash_table *table = decls_p ? decl_specializations
29989 : type_specializations;
29990 spec_hash_table::iterator end (table->end ());
29991 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
29992 fn (decls_p, *iter, data);
29995 /* Lookup the specialization of *ELT, in the decl or type
29996 specialization table. Return the SPEC that's already there, or
29997 NULL if nothing. */
29999 tree
30000 match_mergeable_specialization (bool decl_p, spec_entry *elt)
30002 hash_table<spec_hasher> *specializations
30003 = decl_p ? decl_specializations : type_specializations;
30004 hashval_t hash = spec_hasher::hash (elt);
30005 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
30007 if (slot)
30008 return (*slot)->spec;
30010 return NULL_TREE;
30013 /* Return flags encoding whether SPEC is on the instantiation and/or
30014 specialization lists of TMPL. */
30016 unsigned
30017 get_mergeable_specialization_flags (tree tmpl, tree decl)
30019 unsigned flags = 0;
30021 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
30022 inst; inst = TREE_CHAIN (inst))
30023 if (TREE_VALUE (inst) == decl)
30025 flags |= 1;
30026 break;
30029 if (CLASS_TYPE_P (TREE_TYPE (decl))
30030 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
30031 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
30032 /* Only need to search if DECL is a partial specialization. */
30033 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
30034 part; part = TREE_CHAIN (part))
30035 if (TREE_VALUE (part) == decl)
30037 flags |= 2;
30038 break;
30041 return flags;
30044 /* Add a new specialization described by SPEC. DECL is the
30045 maybe-template decl and FLAGS is as returned from
30046 get_mergeable_specialization_flags. */
30048 void
30049 add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
30050 tree decl, unsigned flags)
30052 hashval_t hash = spec_hasher::hash (elt);
30053 if (decl_p)
30055 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
30057 gcc_checking_assert (!*slot);
30058 auto entry = ggc_alloc<spec_entry> ();
30059 *entry = *elt;
30060 *slot = entry;
30062 if (alias_p)
30064 elt->spec = TREE_TYPE (elt->spec);
30065 gcc_checking_assert (elt->spec);
30069 if (!decl_p || alias_p)
30071 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
30073 /* We don't distinguish different constrained partial type
30074 specializations, so there could be duplicates. Everything else
30075 must be new. */
30076 if (!(flags & 2 && *slot))
30078 gcc_checking_assert (!*slot);
30080 auto entry = ggc_alloc<spec_entry> ();
30081 *entry = *elt;
30082 *slot = entry;
30086 if (flags & 1)
30087 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
30088 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
30090 if (flags & 2)
30092 /* A partial specialization. */
30093 tree cons = tree_cons (elt->args, decl,
30094 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
30095 TREE_TYPE (cons) = elt->spec;
30096 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
30100 /* Set up the hash tables for template instantiations. */
30102 void
30103 init_template_processing (void)
30105 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
30106 type_specializations = hash_table<spec_hasher>::create_ggc (37);
30108 if (cxx_dialect >= cxx11)
30109 declare_integer_pack ();
30112 /* Print stats about the template hash tables for -fstats. */
30114 void
30115 print_template_statistics (void)
30117 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
30118 "%f collisions\n", (long) decl_specializations->size (),
30119 (long) decl_specializations->elements (),
30120 decl_specializations->collisions ());
30121 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
30122 "%f collisions\n", (long) type_specializations->size (),
30123 (long) type_specializations->elements (),
30124 type_specializations->collisions ());
30127 #if CHECKING_P
30129 namespace selftest {
30131 /* Verify that build_non_dependent_expr () works, for various expressions,
30132 and that location wrappers don't affect the results. */
30134 static void
30135 test_build_non_dependent_expr ()
30137 location_t loc = BUILTINS_LOCATION;
30139 /* Verify constants, without and with location wrappers. */
30140 tree int_cst = build_int_cst (integer_type_node, 42);
30141 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
30143 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
30144 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
30145 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
30147 tree string_lit = build_string (4, "foo");
30148 TREE_TYPE (string_lit) = char_array_type_node;
30149 string_lit = fix_string_type (string_lit);
30150 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
30152 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
30153 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
30154 ASSERT_EQ (wrapped_string_lit,
30155 build_non_dependent_expr (wrapped_string_lit));
30158 /* Verify that type_dependent_expression_p () works correctly, even
30159 in the presence of location wrapper nodes. */
30161 static void
30162 test_type_dependent_expression_p ()
30164 location_t loc = BUILTINS_LOCATION;
30166 tree name = get_identifier ("foo");
30168 /* If no templates are involved, nothing is type-dependent. */
30169 gcc_assert (!processing_template_decl);
30170 ASSERT_FALSE (type_dependent_expression_p (name));
30172 ++processing_template_decl;
30174 /* Within a template, an unresolved name is always type-dependent. */
30175 ASSERT_TRUE (type_dependent_expression_p (name));
30177 /* Ensure it copes with NULL_TREE and errors. */
30178 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
30179 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
30181 /* A USING_DECL in a template should be type-dependent, even if wrapped
30182 with a location wrapper (PR c++/83799). */
30183 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
30184 TREE_TYPE (using_decl) = integer_type_node;
30185 ASSERT_TRUE (type_dependent_expression_p (using_decl));
30186 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
30187 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
30188 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
30190 --processing_template_decl;
30193 /* Run all of the selftests within this file. */
30195 void
30196 cp_pt_c_tests ()
30198 test_build_non_dependent_expr ();
30199 test_type_dependent_expression_p ();
30202 } // namespace selftest
30204 #endif /* #if CHECKING_P */
30206 #include "gt-cp-pt.h"