1/* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* Known bugs or deficiencies include:
23
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".
26
27 Fixed by: C++20 modules. */
28
29#include "config.h"
30#define INCLUDE_ALGORITHM // for std::equal
31#include "system.h"
32#include "coretypes.h"
33#include "cp-tree.h"
34#include "timevar.h"
35#include "stringpool.h"
36#include "varasm.h"
37#include "attribs.h"
38#include "stor-layout.h"
39#include "intl.h"
40#include "c-family/c-objc.h"
41#include "cp-objcp-common.h"
42#include "toplev.h"
43#include "tree-iterator.h"
44#include "type-utils.h"
45#include "gimplify.h"
46#include "gcc-rich-location.h"
47#include "selftest.h"
48#include "target.h"
49#include "builtins.h"
50#include "omp-general.h"
51
52/* The type of functions taking a tree, and some additional data, and
53 returning an int. */
54typedef int (*tree_fn_t) (tree, void*);
55
56/* The PENDING_TEMPLATES is a list of templates whose instantiations
57 have been deferred, either because their definitions were not yet
58 available, or because we were putting off doing the work. */
59struct GTY ((chain_next ("%h.next"))) pending_template
60{
61 struct pending_template *next;
62 struct tinst_level *tinst;
63};
64
65static GTY(()) struct pending_template *pending_templates;
66static GTY(()) struct pending_template *last_pending_template;
67
68int processing_template_parmlist;
69static int template_header_count;
70
71static vec<int> inline_parm_levels;
72
73static GTY(()) struct tinst_level *current_tinst_level;
74
75static GTY(()) vec<tree, va_gc> *saved_access_scope;
76
77/* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80static tree cur_stmt_expr;
81
82// -------------------------------------------------------------------------- //
83// Local Specialization Stack
84//
85// Implementation of the RAII helper for creating new local
86// specializations.
87local_specialization_stack::local_specialization_stack (lss_policy policy)
88 : saved (local_specializations)
89{
90 if (policy == lss_nop)
91 ;
92 else if (policy == lss_blank || !saved)
93 local_specializations = new hash_map<tree, tree>;
94 else
95 local_specializations = new hash_map<tree, tree>(*saved);
96}
97
98local_specialization_stack::~local_specialization_stack ()
99{
100 if (local_specializations != saved)
101 {
102 delete local_specializations;
103 local_specializations = saved;
104 }
105}
106
107/* True if we've recursed into fn_type_unification too many times. */
108static bool excessive_deduction_depth;
109
110struct spec_hasher : ggc_ptr_hash<spec_entry>
111{
112 static hashval_t hash (tree, tree);
113 static hashval_t hash (spec_entry *);
114 static bool equal (spec_entry *, spec_entry *);
115};
116
117/* The general template is not in these tables. */
118typedef hash_table<spec_hasher> spec_hash_table;
119static GTY (()) spec_hash_table *decl_specializations;
120static GTY (()) spec_hash_table *type_specializations;
121
122/* Contains canonical template parameter types. The vector is indexed by
123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
124 TREE_LIST, whose TREE_VALUEs contain the canonical template
125 parameters of various types and levels. */
126static GTY(()) vec<tree, va_gc> *canonical_template_parms;
127
128#define UNIFY_ALLOW_NONE 0
129#define UNIFY_ALLOW_MORE_CV_QUAL 1
130#define UNIFY_ALLOW_LESS_CV_QUAL 2
131#define UNIFY_ALLOW_DERIVED 4
132#define UNIFY_ALLOW_INTEGER 8
133#define UNIFY_ALLOW_OUTER_LEVEL 16
134#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
135#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
136
137enum template_base_result {
138 tbr_incomplete_type,
139 tbr_ambiguous_baseclass,
140 tbr_success
141};
142
143static bool resolve_overloaded_unification (tree, tree, tree, tree,
144 unification_kind_t, int,
145 bool);
146static int try_one_overload (tree, tree, tree, tree, tree,
147 unification_kind_t, int, bool, bool);
148static int unify (tree, tree, tree, tree, int, bool);
149static void add_pending_template (tree);
150static tree reopen_tinst_level (struct tinst_level *);
151static tree tsubst_initializer_list (tree, tree);
152static tree get_partial_spec_bindings (tree, tree, tree);
153static void tsubst_enum (tree, tree, tree);
154static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
156 struct conversion **, bool, bool);
157static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
158 tree*, tree*, tree);
159static 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);
163static void note_template_header (int);
164static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170static tree expand_template_argument_pack (tree);
171static tree build_template_parm_index (int, int, int, tree, tree);
172static bool inline_needs_template_parms (tree, bool);
173static void push_inline_template_parms_recursive (tree, int);
174static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175static int mark_template_parm (tree, void *);
176static int template_parm_this_level_p (tree, void *);
177static tree tsubst_friend_function (tree, tree);
178static tree tsubst_friend_class (tree, tree);
179static int can_complete_type_without_circularity (tree);
180static tree get_bindings (tree, tree, tree, bool);
181static int template_decl_level (tree);
182static int check_cv_quals_for_unify (int, tree, tree);
183static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185static tree copy_template_args (tree);
186static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
188static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
189static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int);
190static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
191static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
192static bool check_specialization_scope (void);
193static tree process_partial_specialization (tree);
194static enum template_base_result get_template_base (tree, tree, tree, tree,
195 bool , tree *);
196static tree try_class_unification (tree, tree, tree, tree, bool);
197static bool class_nttp_const_wrapper_p (tree t);
198static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
199 tree, tree);
200static bool template_template_parm_bindings_ok_p (tree, tree);
201static void tsubst_default_arguments (tree, tsubst_flags_t);
202static tree for_each_template_parm_r (tree *, int *, void *);
203static tree copy_default_args_to_explicit_spec_1 (tree, tree);
204static void copy_default_args_to_explicit_spec (tree);
205static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
206static bool dependent_template_arg_p (tree);
207static bool dependent_type_p_r (tree);
208static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
209static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
210static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
211static tree tsubst_name (tree, tree, tsubst_flags_t, tree);
212static void perform_instantiation_time_access_checks (tree, tree);
213static tree listify (tree);
214static tree listify_autos (tree, tree);
215static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
216static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
217static tree get_underlying_template (tree);
218static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
219static tree canonicalize_expr_argument (tree, tsubst_flags_t);
220static tree make_argument_pack (tree);
221static tree enclosing_instantiation_of (tree tctx);
222static void instantiate_body (tree pattern, tree args, tree d, bool nested);
223static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
224static void mark_template_arguments_used (tree, tree);
225static bool uses_outer_template_parms (tree);
226static tree alias_ctad_tweaks (tree, tree);
227static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
228static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
229
230/* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 for a class or alias template (needed by instantiate_decl). */
234
235void
236push_access_scope (tree t)
237{
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_IMPLICIT_TYPEDEF_P (t)
244 && CLASS_TYPE_P (TREE_TYPE (t)))
245 push_nested_class (TREE_TYPE (t));
246 else if (DECL_CLASS_SCOPE_P (t))
247 push_nested_class (DECL_CONTEXT (t));
248 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
249 /* An artificial deduction guide should have the same access as
250 the constructor. */
251 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
252 else
253 push_to_top_level ();
254
255 if (TREE_CODE (t) == FUNCTION_DECL)
256 {
257 vec_safe_push (v&: saved_access_scope, obj: current_function_decl);
258 current_function_decl = t;
259 }
260}
261
262/* Restore the scope set up by push_access_scope. T is the node we
263 are processing. */
264
265void
266pop_access_scope (tree t)
267{
268 if (TREE_CODE (t) == FUNCTION_DECL)
269 current_function_decl = saved_access_scope->pop();
270
271 if (DECL_FRIEND_CONTEXT (t)
272 || (DECL_IMPLICIT_TYPEDEF_P (t)
273 && CLASS_TYPE_P (TREE_TYPE (t)))
274 || DECL_CLASS_SCOPE_P (t)
275 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
276 pop_nested_class ();
277 else
278 pop_from_top_level ();
279}
280
281/* Do any processing required when DECL (a member template
282 declaration) is finished. Returns the TEMPLATE_DECL corresponding
283 to DECL, unless it is a specialization, in which case the DECL
284 itself is returned. */
285
286tree
287finish_member_template_decl (tree decl)
288{
289 if (decl == error_mark_node)
290 return error_mark_node;
291
292 gcc_assert (DECL_P (decl));
293
294 if (TREE_CODE (decl) == TYPE_DECL)
295 {
296 tree type;
297
298 type = TREE_TYPE (decl);
299 if (type == error_mark_node)
300 return error_mark_node;
301 if (MAYBE_CLASS_TYPE_P (type)
302 && CLASSTYPE_TEMPLATE_INFO (type)
303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
304 {
305 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
306 check_member_template (tmpl);
307 return tmpl;
308 }
309 return NULL_TREE;
310 }
311 else if (TREE_CODE (decl) == FIELD_DECL)
312 error_at (DECL_SOURCE_LOCATION (decl),
313 "data member %qD cannot be a member template", decl);
314 else if (DECL_TEMPLATE_INFO (decl))
315 {
316 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
317 {
318 check_member_template (DECL_TI_TEMPLATE (decl));
319 return DECL_TI_TEMPLATE (decl);
320 }
321 else
322 return NULL_TREE;
323 }
324 else
325 error_at (DECL_SOURCE_LOCATION (decl),
326 "invalid member template declaration %qD", decl);
327
328 return error_mark_node;
329}
330
331/* Create a template info node. */
332
333tree
334build_template_info (tree template_decl, tree template_args)
335{
336 tree result = make_node (TEMPLATE_INFO);
337 TI_TEMPLATE (result) = template_decl;
338 TI_ARGS (result) = template_args;
339 return result;
340}
341
342/* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */
343
344static tree
345decl_template_info (const_tree decl)
346{
347 /* This needs to match template_info_decl_check. */
348 if (DECL_LANG_SPECIFIC (decl))
349 switch (TREE_CODE (decl))
350 {
351 case FUNCTION_DECL:
352 if (DECL_THUNK_P (decl))
353 break;
354 gcc_fallthrough ();
355 case VAR_DECL:
356 case FIELD_DECL:
357 case TYPE_DECL:
358 case CONCEPT_DECL:
359 case TEMPLATE_DECL:
360 return DECL_TEMPLATE_INFO (decl);
361
362 default:
363 break;
364 }
365 return NULL_TREE;
366}
367
368/* Return the template info node corresponding to T, whatever T is. */
369
370tree
371get_template_info (const_tree t)
372{
373 tree tinfo = NULL_TREE;
374
375 if (!t || t == error_mark_node)
376 return NULL;
377
378 if (TREE_CODE (t) == NAMESPACE_DECL
379 || TREE_CODE (t) == PARM_DECL)
380 return NULL;
381
382 if (DECL_P (t))
383 tinfo = decl_template_info (decl: t);
384
385 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
386 t = TREE_TYPE (t);
387
388 if (OVERLOAD_TYPE_P (t))
389 tinfo = TYPE_TEMPLATE_INFO (t);
390 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
391 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
392
393 return tinfo;
394}
395
396/* Returns the template nesting level of the indicated class TYPE.
397
398 For example, in:
399 template <class T>
400 struct A
401 {
402 template <class U>
403 struct B {};
404 };
405
406 A<T>::B<U> has depth two, while A<T> has depth one.
407 Both A<T>::B<int> and A<int>::B<U> have depth one, if
408 they are instantiations, not specializations.
409
410 This function is guaranteed to return 0 if passed NULL_TREE so
411 that, for example, `template_class_depth (current_class_type)' is
412 always safe. */
413
414int
415template_class_depth (tree type)
416{
417 int depth;
418
419 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
420 {
421 tree tinfo = get_template_info (t: type);
422
423 if (tinfo
424 && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
425 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
426 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
427 ++depth;
428
429 if (DECL_P (type))
430 {
431 if (tree fctx = DECL_FRIEND_CONTEXT (type))
432 type = fctx;
433 else
434 type = CP_DECL_CONTEXT (type);
435 }
436 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
437 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
438 else
439 type = CP_TYPE_CONTEXT (type);
440 }
441
442 return depth;
443}
444
445/* Return TRUE if NODE instantiates a template that has arguments of
446 its own, be it directly a primary template or indirectly through a
447 partial specializations. */
448static bool
449instantiates_primary_template_p (tree node)
450{
451 tree tinfo = get_template_info (t: node);
452 if (!tinfo)
453 return false;
454
455 tree tmpl = TI_TEMPLATE (tinfo);
456 if (PRIMARY_TEMPLATE_P (tmpl))
457 return true;
458
459 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
460 return false;
461
462 /* So now we know we have a specialization, but it could be a full
463 or a partial specialization. To tell which, compare the depth of
464 its template arguments with those of its context. */
465
466 tree ctxt = DECL_CONTEXT (tmpl);
467 tree ctinfo = get_template_info (t: ctxt);
468 if (!ctinfo)
469 return true;
470
471 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
472 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
473}
474
475/* Subroutine of maybe_begin_member_template_processing.
476 Returns true if processing DECL needs us to push template parms. */
477
478static bool
479inline_needs_template_parms (tree decl, bool nsdmi)
480{
481 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
482 return false;
483
484 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
485 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
486}
487
488/* Subroutine of maybe_begin_member_template_processing.
489 Push the template parms in PARMS, starting from LEVELS steps into the
490 chain, and ending at the beginning, since template parms are listed
491 innermost first. */
492
493static void
494push_inline_template_parms_recursive (tree parmlist, int levels)
495{
496 tree parms = TREE_VALUE (parmlist);
497 int i;
498
499 if (levels > 1)
500 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels: levels - 1);
501
502 ++processing_template_decl;
503 current_template_parms
504 = tree_cons (size_int (current_template_depth + 1),
505 parms, current_template_parms);
506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
507 = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
508 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
509
510 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
511 NULL);
512 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
513 {
514 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
515
516 if (error_operand_p (t: parm))
517 continue;
518
519 gcc_assert (DECL_P (parm));
520
521 switch (TREE_CODE (parm))
522 {
523 case TYPE_DECL:
524 case TEMPLATE_DECL:
525 pushdecl (parm);
526 break;
527
528 case PARM_DECL:
529 /* Push the CONST_DECL. */
530 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
531 break;
532
533 default:
534 gcc_unreachable ();
535 }
536 }
537}
538
539/* Restore the template parameter context for a member template, a
540 friend template defined in a class definition, or a non-template
541 member of template class. */
542
543void
544maybe_begin_member_template_processing (tree decl)
545{
546 tree parms;
547 int levels = 0;
548 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
549
550 if (nsdmi)
551 {
552 tree ctx = DECL_CONTEXT (decl);
553 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
554 /* Disregard full specializations (c++/60999). */
555 && uses_template_parms (ctx)
556 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
557 }
558
559 if (inline_needs_template_parms (decl, nsdmi))
560 {
561 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
562 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
563
564 if (DECL_TEMPLATE_SPECIALIZATION (decl))
565 {
566 --levels;
567 parms = TREE_CHAIN (parms);
568 }
569
570 push_inline_template_parms_recursive (parmlist: parms, levels);
571 }
572
573 /* Remember how many levels of template parameters we pushed so that
574 we can pop them later. */
575 inline_parm_levels.safe_push (obj: levels);
576}
577
578/* Undo the effects of maybe_begin_member_template_processing. */
579
580void
581maybe_end_member_template_processing (void)
582{
583 int i;
584 int last;
585
586 if (inline_parm_levels.length () == 0)
587 return;
588
589 last = inline_parm_levels.pop ();
590 for (i = 0; i < last; ++i)
591 {
592 --processing_template_decl;
593 current_template_parms = TREE_CHAIN (current_template_parms);
594 poplevel (0, 0, 0);
595 }
596}
597
598/* Return a new template argument vector which contains all of ARGS,
599 but has as its innermost set of arguments the EXTRA_ARGS. */
600
601tree
602add_to_template_args (tree args, tree extra_args)
603{
604 tree new_args;
605 int extra_depth;
606 int i;
607 int j;
608
609 if (args == NULL_TREE || extra_args == error_mark_node)
610 return extra_args;
611
612 extra_depth = TMPL_ARGS_DEPTH (extra_args);
613 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
614
615 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
616 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
617
618 for (j = 1; j <= extra_depth; ++j, ++i)
619 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
620
621 return new_args;
622}
623
624/* Like add_to_template_args, but only the outermost ARGS are added to
625 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
626 (EXTRA_ARGS) levels are added. This function is used to combine
627 the template arguments from a partial instantiation with the
628 template arguments used to attain the full instantiation from the
629 partial instantiation.
630
631 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
632
633tree
634add_outermost_template_args (tree args, tree extra_args)
635{
636 tree new_args;
637
638 if (!args)
639 return extra_args;
640 if (TREE_CODE (args) == TEMPLATE_DECL)
641 {
642 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
643 args = TI_ARGS (ti);
644 }
645
646 /* If there are more levels of EXTRA_ARGS than there are ARGS,
647 something very fishy is going on. */
648 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
649
650 /* If *all* the new arguments will be the EXTRA_ARGS, just return
651 them. */
652 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
653 return extra_args;
654
655 /* For the moment, we make ARGS look like it contains fewer levels. */
656 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
657
658 new_args = add_to_template_args (args, extra_args);
659
660 /* Now, we restore ARGS to its full dimensions. */
661 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
662
663 return new_args;
664}
665
666/* Return the N levels of innermost template arguments from the ARGS. */
667
668tree
669get_innermost_template_args (tree args, int n)
670{
671 tree new_args;
672 int extra_levels;
673 int i;
674
675 gcc_assert (n >= 0);
676
677 /* If N is 1, just return the innermost set of template arguments. */
678 if (n == 1)
679 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
680
681 /* If we're not removing anything, just return the arguments we were
682 given. */
683 extra_levels = TMPL_ARGS_DEPTH (args) - n;
684 gcc_assert (extra_levels >= 0);
685 if (extra_levels == 0)
686 return args;
687
688 /* Make a new set of arguments, not containing the outer arguments. */
689 new_args = make_tree_vec (n);
690 for (i = 1; i <= n; ++i)
691 SET_TMPL_ARGS_LEVEL (new_args, i,
692 TMPL_ARGS_LEVEL (args, i + extra_levels));
693
694 return new_args;
695}
696
697/* The inverse of get_innermost_template_args: Return all but the innermost
698 EXTRA_LEVELS levels of template arguments from the ARGS. */
699
700static tree
701strip_innermost_template_args (tree args, int extra_levels)
702{
703 tree new_args;
704 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
705 int i;
706
707 gcc_assert (n >= 0);
708
709 /* If N is 1, just return the outermost set of template arguments. */
710 if (n == 1)
711 return TMPL_ARGS_LEVEL (args, 1);
712
713 /* If we're not removing anything, just return the arguments we were
714 given. */
715 gcc_assert (extra_levels >= 0);
716 if (extra_levels == 0)
717 return args;
718
719 /* Make a new set of arguments, not containing the inner arguments. */
720 new_args = make_tree_vec (n);
721 for (i = 1; i <= n; ++i)
722 SET_TMPL_ARGS_LEVEL (new_args, i,
723 TMPL_ARGS_LEVEL (args, i));
724
725 return new_args;
726}
727
728/* We've got a template header coming up; push to a new level for storing
729 the parms. */
730
731void
732begin_template_parm_list (void)
733{
734 /* We use a non-tag-transparent scope here, which causes pushtag to
735 put tags in this scope, rather than in the enclosing class or
736 namespace scope. This is the right thing, since we want
737 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
738 global template class, push_template_decl handles putting the
739 TEMPLATE_DECL into top-level scope. For a nested template class,
740 e.g.:
741
742 template <class T> struct S1 {
743 template <class T> struct S2 {};
744 };
745
746 pushtag contains special code to insert the TEMPLATE_DECL for S2
747 at the right scope. */
748 begin_scope (sk_template_parms, NULL);
749 ++processing_template_decl;
750 ++processing_template_parmlist;
751 note_template_header (0);
752
753 /* Add a dummy parameter level while we process the parameter list. */
754 current_template_parms
755 = tree_cons (size_int (current_template_depth + 1),
756 make_tree_vec (0),
757 current_template_parms);
758}
759
760/* This routine is called when a specialization is declared. If it is
761 invalid to declare a specialization here, an error is reported and
762 false is returned, otherwise this routine will return true. */
763
764static bool
765check_specialization_scope (void)
766{
767 tree scope = current_scope ();
768
769 /* [temp.expl.spec]
770
771 An explicit specialization shall be declared in the namespace of
772 which the template is a member, or, for member templates, in the
773 namespace of which the enclosing class or enclosing class
774 template is a member. An explicit specialization of a member
775 function, member class or static data member of a class template
776 shall be declared in the namespace of which the class template
777 is a member. */
778 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
779 {
780 error ("explicit specialization in non-namespace scope %qD", scope);
781 return false;
782 }
783
784 /* [temp.expl.spec]
785
786 In an explicit specialization declaration for a member of a class
787 template or a member template that appears in namespace scope,
788 the member template and some of its enclosing class templates may
789 remain unspecialized, except that the declaration shall not
790 explicitly specialize a class member template if its enclosing
791 class templates are not explicitly specialized as well. */
792 if (current_template_parms)
793 {
794 error ("enclosing class templates are not explicitly specialized");
795 return false;
796 }
797
798 return true;
799}
800
801/* We've just seen template <>. */
802
803bool
804begin_specialization (void)
805{
806 begin_scope (sk_template_spec, NULL);
807 note_template_header (1);
808 return check_specialization_scope ();
809}
810
811/* Called at then end of processing a declaration preceded by
812 template<>. */
813
814void
815end_specialization (void)
816{
817 finish_scope ();
818 reset_specialization ();
819}
820
821/* Any template <>'s that we have seen thus far are not referring to a
822 function specialization. */
823
824void
825reset_specialization (void)
826{
827 processing_specialization = 0;
828 template_header_count = 0;
829}
830
831/* We've just seen a template header. If SPECIALIZATION is nonzero,
832 it was of the form template <>. */
833
834static void
835note_template_header (int specialization)
836{
837 processing_specialization = specialization;
838 template_header_count++;
839}
840
841/* We're beginning an explicit instantiation. */
842
843void
844begin_explicit_instantiation (void)
845{
846 gcc_assert (!processing_explicit_instantiation);
847 processing_explicit_instantiation = true;
848}
849
850
851void
852end_explicit_instantiation (void)
853{
854 gcc_assert (processing_explicit_instantiation);
855 processing_explicit_instantiation = false;
856}
857
858/* An explicit specialization or partial specialization of TMPL is being
859 declared. Check that the namespace in which the specialization is
860 occurring is permissible. Returns false iff it is invalid to
861 specialize TMPL in the current namespace. */
862
863static bool
864check_specialization_namespace (tree tmpl)
865{
866 tree tpl_ns = decl_namespace_context (tmpl);
867
868 /* [tmpl.expl.spec]
869
870 An explicit specialization shall be declared in a namespace enclosing the
871 specialized template. An explicit specialization whose declarator-id is
872 not qualified shall be declared in the nearest enclosing namespace of the
873 template, or, if the namespace is inline (7.3.1), any namespace from its
874 enclosing namespace set. */
875 if (current_scope() != DECL_CONTEXT (tmpl)
876 && !at_namespace_scope_p ())
877 {
878 error ("specialization of %qD must appear at namespace scope", tmpl);
879 return false;
880 }
881
882 if (is_nested_namespace (current_namespace, descendant: tpl_ns, inline_only: cxx_dialect < cxx11))
883 /* Same or enclosing namespace. */
884 return true;
885 else
886 {
887 auto_diagnostic_group d;
888 if (permerror (input_location,
889 "specialization of %qD in different namespace", tmpl))
890 inform (DECL_SOURCE_LOCATION (tmpl),
891 " from definition of %q#D", tmpl);
892 return false;
893 }
894}
895
896/* SPEC is an explicit instantiation. Check that it is valid to
897 perform this explicit instantiation in the current namespace. */
898
899static void
900check_explicit_instantiation_namespace (tree spec)
901{
902 tree ns;
903
904 /* DR 275: An explicit instantiation shall appear in an enclosing
905 namespace of its template. */
906 ns = decl_namespace_context (spec);
907 if (!is_nested_namespace (current_namespace, descendant: ns))
908 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
909 "(which does not enclose namespace %qD)",
910 spec, current_namespace, ns);
911}
912
913/* Returns true if TYPE is a new partial specialization that needs to be
914 set up. This may also modify TYPE to point to the correct (new or
915 existing) constrained partial specialization. */
916
917static bool
918maybe_new_partial_specialization (tree& type)
919{
920 /* An implicit instantiation of an incomplete type implies
921 the definition of a new class template.
922
923 template<typename T>
924 struct S;
925
926 template<typename T>
927 struct S<T*>;
928
929 Here, S<T*> is an implicit instantiation of S whose type
930 is incomplete. */
931 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
932 return true;
933
934 /* It can also be the case that TYPE is a completed specialization.
935 Continuing the previous example, suppose we also declare:
936
937 template<typename T>
938 requires Integral<T>
939 struct S<T*>;
940
941 Here, S<T*> refers to the specialization S<T*> defined
942 above. However, we need to differentiate definitions because
943 we intend to define a new partial specialization. In this case,
944 we rely on the fact that the constraints are different for
945 this declaration than that above.
946
947 Note that we also get here for injected class names and
948 late-parsed template definitions. We must ensure that we
949 do not create new type declarations for those cases. */
950 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
951 {
952 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
953 tree args = CLASSTYPE_TI_ARGS (type);
954
955 /* If there are no template parameters, this cannot be a new
956 partial template specialization? */
957 if (!current_template_parms)
958 return false;
959
960 /* The injected-class-name is not a new partial specialization. */
961 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
962 return false;
963
964 /* If the constraints are not the same as those of the primary
965 then, we can probably create a new specialization. */
966 tree type_constr = current_template_constraints ();
967
968 if (type == TREE_TYPE (tmpl))
969 {
970 tree main_constr = get_constraints (tmpl);
971 if (equivalent_constraints (type_constr, main_constr))
972 return false;
973 }
974
975 /* Also, if there's a pre-existing specialization with matching
976 constraints, then this also isn't new. */
977 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
978 while (specs)
979 {
980 tree spec_tmpl = TREE_VALUE (specs);
981 tree spec_args = TREE_PURPOSE (specs);
982 tree spec_constr = get_constraints (spec_tmpl);
983 if (comp_template_args (args, spec_args)
984 && equivalent_constraints (type_constr, spec_constr))
985 {
986 type = TREE_TYPE (spec_tmpl);
987 return false;
988 }
989 specs = TREE_CHAIN (specs);
990 }
991
992 /* Create a new type node (and corresponding type decl)
993 for the newly declared specialization. */
994 tree t = make_class_type (TREE_CODE (type));
995 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
996 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
997
998 /* We only need a separate type node for storing the definition of this
999 partial specialization; uses of S<T*> are unconstrained, so all are
1000 equivalent. So keep TYPE_CANONICAL the same. */
1001 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
1002
1003 /* Build the corresponding type decl. */
1004 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
1005 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
1006 DECL_SOURCE_LOCATION (d) = input_location;
1007 TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
1008
1009 set_instantiating_module (d);
1010 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
1011
1012 type = t;
1013 return true;
1014 }
1015
1016 return false;
1017}
1018
1019/* The TYPE is being declared. If it is a template type, that means it
1020 is a partial specialization. Do appropriate error-checking. */
1021
1022tree
1023maybe_process_partial_specialization (tree type)
1024{
1025 tree context;
1026
1027 if (type == error_mark_node)
1028 return error_mark_node;
1029
1030 /* A lambda that appears in specialization context is not itself a
1031 specialization. */
1032 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
1033 return type;
1034
1035 /* An injected-class-name is not a specialization. */
1036 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
1037 return type;
1038
1039 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1040 {
1041 error ("name of class shadows template template parameter %qD",
1042 TYPE_NAME (type));
1043 return error_mark_node;
1044 }
1045
1046 context = TYPE_CONTEXT (type);
1047
1048 if (TYPE_ALIAS_P (type))
1049 {
1050 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1051
1052 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1053 error ("specialization of alias template %qD",
1054 TI_TEMPLATE (tinfo));
1055 else
1056 error ("explicit specialization of non-template %qT", type);
1057 return error_mark_node;
1058 }
1059 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1060 {
1061 /* This is for ordinary explicit specialization and partial
1062 specialization of a template class such as:
1063
1064 template <> class C<int>;
1065
1066 or:
1067
1068 template <class T> class C<T*>;
1069
1070 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1071
1072 if (maybe_new_partial_specialization (type))
1073 {
1074 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
1075 && !at_namespace_scope_p ())
1076 return error_mark_node;
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 if (processing_template_decl)
1080 {
1081 tree decl = push_template_decl (TYPE_MAIN_DECL (type));
1082 if (decl == error_mark_node)
1083 return error_mark_node;
1084 return TREE_TYPE (decl);
1085 }
1086 }
1087 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1088 error ("specialization of %qT after instantiation", type);
1089 else if (errorcount && !processing_specialization
1090 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1091 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1092 /* Trying to define a specialization either without a template<> header
1093 or in an inappropriate place. We've already given an error, so just
1094 bail now so we don't actually define the specialization. */
1095 return error_mark_node;
1096 }
1097 else if (CLASS_TYPE_P (type)
1098 && !CLASSTYPE_USE_TEMPLATE (type)
1099 && CLASSTYPE_TEMPLATE_INFO (type)
1100 && context && CLASS_TYPE_P (context)
1101 && CLASSTYPE_TEMPLATE_INFO (context))
1102 {
1103 /* This is for an explicit specialization of member class
1104 template according to [temp.expl.spec/18]:
1105
1106 template <> template <class U> class C<int>::D;
1107
1108 The context `C<int>' must be an implicit instantiation.
1109 Otherwise this is just a member class template declared
1110 earlier like:
1111
1112 template <> class C<int> { template <class U> class D; };
1113 template <> template <class U> class C<int>::D;
1114
1115 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1116 while in the second case, `C<int>::D' is a primary template
1117 and `C<T>::D' may not exist. */
1118
1119 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1120 && !COMPLETE_TYPE_P (type))
1121 {
1122 tree t;
1123 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1124
1125 if (current_namespace
1126 != decl_namespace_context (tmpl))
1127 {
1128 if (permerror (input_location,
1129 "specialization of %qD in different namespace",
1130 type))
1131 inform (DECL_SOURCE_LOCATION (tmpl),
1132 "from definition of %q#D", tmpl);
1133 }
1134
1135 /* Check for invalid specialization after instantiation:
1136
1137 template <> template <> class C<int>::D<int>;
1138 template <> template <class U> class C<int>::D; */
1139
1140 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1141 t; t = TREE_CHAIN (t))
1142 {
1143 tree inst = TREE_VALUE (t);
1144 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1145 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1146 {
1147 /* We already have a full specialization of this partial
1148 instantiation, or a full specialization has been
1149 looked up but not instantiated. Reassign it to the
1150 new member specialization template. */
1151 spec_entry elt;
1152 spec_entry *entry;
1153
1154 elt.tmpl = most_general_template (tmpl);
1155 elt.args = CLASSTYPE_TI_ARGS (inst);
1156 elt.spec = inst;
1157
1158 type_specializations->remove_elt (value: &elt);
1159
1160 elt.tmpl = tmpl;
1161 CLASSTYPE_TI_ARGS (inst)
1162 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1163
1164 spec_entry **slot
1165 = type_specializations->find_slot (value: &elt, insert: INSERT);
1166 entry = ggc_alloc<spec_entry> ();
1167 *entry = elt;
1168 *slot = entry;
1169 }
1170 else
1171 /* But if we've had an implicit instantiation, that's a
1172 problem ([temp.expl.spec]/6). */
1173 error ("specialization %qT after instantiation %qT",
1174 type, inst);
1175 }
1176
1177 /* Mark TYPE as a specialization. And as a result, we only
1178 have one level of template argument for the innermost
1179 class template. */
1180 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1181 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1182 CLASSTYPE_TI_ARGS (type)
1183 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1184 }
1185 }
1186 else if (processing_specialization)
1187 {
1188 /* Someday C++0x may allow for enum template specialization. */
1189 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1190 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1191 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1192 "of %qD not allowed by ISO C++", type);
1193 else
1194 {
1195 error ("explicit specialization of non-template %qT", type);
1196 return error_mark_node;
1197 }
1198 }
1199
1200 return type;
1201}
1202
1203/* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1204 gone through coerce_template_parms by now. */
1205
1206static void
1207verify_unstripped_args_1 (tree inner)
1208{
1209 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1210 {
1211 tree arg = TREE_VEC_ELT (inner, i);
1212 if (TREE_CODE (arg) == TEMPLATE_DECL)
1213 /* OK */;
1214 else if (TYPE_P (arg))
1215 gcc_assert (strip_typedefs (arg, NULL) == arg);
1216 else if (ARGUMENT_PACK_P (arg))
1217 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1218 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1219 /* Allow typedefs on the type of a non-type argument, since a
1220 parameter can have them. */;
1221 else
1222 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1223 }
1224}
1225
1226static void
1227verify_unstripped_args (tree args)
1228{
1229 ++processing_template_decl;
1230 if (!any_dependent_template_arguments_p (args))
1231 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1232 --processing_template_decl;
1233}
1234
1235/* Retrieve the specialization (in the sense of [temp.spec] - a
1236 specialization is either an instantiation or an explicit
1237 specialization) of TMPL for the given template ARGS. If there is
1238 no such specialization, return NULL_TREE. The ARGS are a vector of
1239 arguments, or a vector of vectors of arguments, in the case of
1240 templates with more than one level of parameters.
1241
1242 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1243 then we search for a partial specialization matching ARGS. This
1244 parameter is ignored if TMPL is not a class template.
1245
1246 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1247 result is a NONTYPE_ARGUMENT_PACK. */
1248
1249static tree
1250retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1251{
1252 if (tmpl == NULL_TREE)
1253 return NULL_TREE;
1254
1255 if (args == error_mark_node)
1256 return NULL_TREE;
1257
1258 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1259 || TREE_CODE (tmpl) == FIELD_DECL);
1260
1261 /* There should be as many levels of arguments as there are
1262 levels of parameters. */
1263 gcc_assert (TMPL_ARGS_DEPTH (args)
1264 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1265 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1266 : template_class_depth (DECL_CONTEXT (tmpl))));
1267
1268 if (flag_checking)
1269 verify_unstripped_args (args);
1270
1271 /* Lambda functions in templates aren't instantiated normally, but through
1272 tsubst_lambda_expr. */
1273 if (lambda_fn_in_template_p (tmpl))
1274 return NULL_TREE;
1275
1276 spec_entry elt;
1277 elt.tmpl = tmpl;
1278 elt.args = args;
1279 elt.spec = NULL_TREE;
1280
1281 spec_hash_table *specializations;
1282 if (DECL_CLASS_TEMPLATE_P (tmpl))
1283 specializations = type_specializations;
1284 else
1285 specializations = decl_specializations;
1286
1287 if (hash == 0)
1288 hash = spec_hasher::hash (&elt);
1289 if (spec_entry *found = specializations->find_with_hash (comparable: &elt, hash))
1290 return found->spec;
1291
1292 return NULL_TREE;
1293}
1294
1295/* Like retrieve_specialization, but for local declarations. */
1296
1297tree
1298retrieve_local_specialization (tree tmpl)
1299{
1300 if (local_specializations == NULL)
1301 return NULL_TREE;
1302
1303 tree *slot = local_specializations->get (k: tmpl);
1304 return slot ? *slot : NULL_TREE;
1305}
1306
1307/* Returns nonzero iff DECL is a specialization of TMPL. */
1308
1309int
1310is_specialization_of (tree decl, tree tmpl)
1311{
1312 tree t;
1313
1314 if (TREE_CODE (decl) == FUNCTION_DECL)
1315 {
1316 for (t = decl;
1317 t != NULL_TREE;
1318 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1319 if (t == tmpl)
1320 return 1;
1321 }
1322 else
1323 {
1324 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1325
1326 for (t = TREE_TYPE (decl);
1327 t != NULL_TREE;
1328 t = CLASSTYPE_USE_TEMPLATE (t)
1329 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1330 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1331 return 1;
1332 }
1333
1334 return 0;
1335}
1336
1337/* Returns nonzero iff DECL is a specialization of friend declaration
1338 FRIEND_DECL according to [temp.friend]. */
1339
1340bool
1341is_specialization_of_friend (tree decl, tree friend_decl)
1342{
1343 bool need_template = true;
1344 int template_depth;
1345
1346 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1347 || TREE_CODE (decl) == TYPE_DECL);
1348
1349 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1350 of a template class, we want to check if DECL is a specialization
1351 if this. */
1352 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1353 && DECL_CLASS_SCOPE_P (friend_decl)
1354 && DECL_TEMPLATE_INFO (friend_decl)
1355 && !DECL_USE_TEMPLATE (friend_decl))
1356 {
1357 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1358 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1359 need_template = false;
1360 }
1361 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1362 && !PRIMARY_TEMPLATE_P (friend_decl))
1363 need_template = false;
1364
1365 /* There is nothing to do if this is not a template friend. */
1366 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1367 return false;
1368
1369 if (is_specialization_of (decl, tmpl: friend_decl))
1370 return true;
1371
1372 /* [temp.friend/6]
1373 A member of a class template may be declared to be a friend of a
1374 non-template class. In this case, the corresponding member of
1375 every specialization of the class template is a friend of the
1376 class granting friendship.
1377
1378 For example, given a template friend declaration
1379
1380 template <class T> friend void A<T>::f();
1381
1382 the member function below is considered a friend
1383
1384 template <> struct A<int> {
1385 void f();
1386 };
1387
1388 For this type of template friend, TEMPLATE_DEPTH below will be
1389 nonzero. To determine if DECL is a friend of FRIEND, we first
1390 check if the enclosing class is a specialization of another. */
1391
1392 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1393 if (template_depth
1394 && DECL_CLASS_SCOPE_P (decl)
1395 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1396 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1397 {
1398 /* Next, we check the members themselves. In order to handle
1399 a few tricky cases, such as when FRIEND_DECL's are
1400
1401 template <class T> friend void A<T>::g(T t);
1402 template <class T> template <T t> friend void A<T>::h();
1403
1404 and DECL's are
1405
1406 void A<int>::g(int);
1407 template <int> void A<int>::h();
1408
1409 we need to figure out ARGS, the template arguments from
1410 the context of DECL. This is required for template substitution
1411 of `T' in the function parameter of `g' and template parameter
1412 of `h' in the above examples. Here ARGS corresponds to `int'. */
1413
1414 tree context = DECL_CONTEXT (decl);
1415 tree args = NULL_TREE;
1416 int current_depth = 0;
1417
1418 while (current_depth < template_depth)
1419 {
1420 if (CLASSTYPE_TEMPLATE_INFO (context))
1421 {
1422 if (current_depth == 0)
1423 args = TYPE_TI_ARGS (context);
1424 else
1425 args = add_to_template_args (TYPE_TI_ARGS (context), extra_args: args);
1426 current_depth++;
1427 }
1428 context = TYPE_CONTEXT (context);
1429 }
1430
1431 if (TREE_CODE (decl) == FUNCTION_DECL)
1432 {
1433 bool is_template;
1434 tree friend_type;
1435 tree decl_type;
1436 tree friend_args_type;
1437 tree decl_args_type;
1438
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template = DECL_TEMPLATE_INFO (decl)
1442 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1443 if (need_template ^ is_template)
1444 return false;
1445 else if (is_template)
1446 {
1447 /* If both are templates, check template parameter list. */
1448 tree friend_parms
1449 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1450 args, tf_none);
1451 if (!comp_template_parms
1452 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1453 friend_parms))
1454 return false;
1455
1456 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1457 }
1458 else
1459 decl_type = TREE_TYPE (decl);
1460
1461 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1462 tf_none, NULL_TREE);
1463 if (friend_type == error_mark_node)
1464 return false;
1465
1466 /* Check if return types match. */
1467 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1468 return false;
1469
1470 /* Check if function parameter types match, ignoring the
1471 `this' parameter. */
1472 friend_args_type = TYPE_ARG_TYPES (friend_type);
1473 decl_args_type = TYPE_ARG_TYPES (decl_type);
1474 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
1475 friend_args_type = TREE_CHAIN (friend_args_type);
1476 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
1477 decl_args_type = TREE_CHAIN (decl_args_type);
1478
1479 return compparms (decl_args_type, friend_args_type);
1480 }
1481 else
1482 {
1483 /* DECL is a TYPE_DECL */
1484 bool is_template;
1485 tree decl_type = TREE_TYPE (decl);
1486
1487 /* Make sure that both DECL and FRIEND_DECL are templates or
1488 non-templates. */
1489 is_template
1490 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1491 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1492
1493 if (need_template ^ is_template)
1494 return false;
1495 else if (is_template)
1496 {
1497 tree friend_parms;
1498 /* If both are templates, check the name of the two
1499 TEMPLATE_DECL's first because is_friend didn't. */
1500 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1501 != DECL_NAME (friend_decl))
1502 return false;
1503
1504 /* Now check template parameter list. */
1505 friend_parms
1506 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1507 args, tf_none);
1508 return comp_template_parms
1509 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1510 friend_parms);
1511 }
1512 else
1513 return (DECL_NAME (decl)
1514 == DECL_NAME (friend_decl));
1515 }
1516 }
1517 return false;
1518}
1519
1520/* Register the specialization SPEC as a specialization of TMPL with
1521 the indicated ARGS. IS_FRIEND indicates whether the specialization
1522 is actually just a friend declaration. ATTRLIST is the list of
1523 attributes that the specialization is declared with or NULL when
1524 it isn't. Returns SPEC, or an equivalent prior declaration, if
1525 available.
1526
1527 We also store instantiations of field packs in the hash table, even
1528 though they are not themselves templates, to make lookup easier. */
1529
1530static tree
1531register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1532 hashval_t hash)
1533{
1534 tree fn;
1535
1536 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1537 || (TREE_CODE (tmpl) == FIELD_DECL
1538 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1539
1540 spec_entry elt;
1541 elt.tmpl = tmpl;
1542 elt.args = args;
1543 elt.spec = spec;
1544
1545 if (hash == 0)
1546 hash = spec_hasher::hash (&elt);
1547
1548 spec_entry **slot = decl_specializations->find_slot_with_hash (comparable: &elt, hash, insert: INSERT);
1549 if (*slot)
1550 fn = (*slot)->spec;
1551 else
1552 fn = NULL_TREE;
1553
1554 /* We can sometimes try to re-register a specialization that we've
1555 already got. In particular, regenerate_decl_from_template calls
1556 duplicate_decls which will update the specialization list. But,
1557 we'll still get called again here anyhow. It's more convenient
1558 to simply allow this than to try to prevent it. */
1559 if (fn == spec)
1560 return spec;
1561 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1562 {
1563 if (DECL_TEMPLATE_INSTANTIATION (fn))
1564 {
1565 if (DECL_ODR_USED (fn)
1566 || DECL_EXPLICIT_INSTANTIATION (fn))
1567 {
1568 error ("specialization of %qD after instantiation",
1569 fn);
1570 return error_mark_node;
1571 }
1572 else
1573 {
1574 tree clone;
1575 /* This situation should occur only if the first
1576 specialization is an implicit instantiation, the
1577 second is an explicit specialization, and the
1578 implicit instantiation has not yet been used. That
1579 situation can occur if we have implicitly
1580 instantiated a member function and then specialized
1581 it later.
1582
1583 We can also wind up here if a friend declaration that
1584 looked like an instantiation turns out to be a
1585 specialization:
1586
1587 template <class T> void foo(T);
1588 class S { friend void foo<>(int) };
1589 template <> void foo(int);
1590
1591 We transform the existing DECL in place so that any
1592 pointers to it become pointers to the updated
1593 declaration.
1594
1595 If there was a definition for the template, but not
1596 for the specialization, we want this to look as if
1597 there were no definition, and vice versa. */
1598 DECL_INITIAL (fn) = NULL_TREE;
1599 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1600
1601 /* The call to duplicate_decls will have applied
1602 [temp.expl.spec]:
1603
1604 An explicit specialization of a function template
1605 is inline only if it is explicitly declared to be,
1606 and independently of whether its function template
1607 is.
1608
1609 to the primary function; now copy the inline bits to
1610 the various clones. */
1611 FOR_EACH_CLONE (clone, fn)
1612 {
1613 DECL_DECLARED_INLINE_P (clone)
1614 = DECL_DECLARED_INLINE_P (fn);
1615 DECL_SOURCE_LOCATION (clone)
1616 = DECL_SOURCE_LOCATION (fn);
1617 DECL_DELETED_FN (clone)
1618 = DECL_DELETED_FN (fn);
1619 }
1620 check_specialization_namespace (tmpl);
1621
1622 return fn;
1623 }
1624 }
1625 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1626 {
1627 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1628 if (dd == error_mark_node)
1629 /* We've already complained in duplicate_decls. */
1630 return error_mark_node;
1631
1632 if (dd == NULL_TREE && DECL_INITIAL (spec))
1633 /* Dup decl failed, but this is a new definition. Set the
1634 line number so any errors match this new
1635 definition. */
1636 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1637
1638 return fn;
1639 }
1640 }
1641 else if (fn)
1642 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1643
1644 /* A specialization must be declared in the same namespace as the
1645 template it is specializing. */
1646 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1647 && !check_specialization_namespace (tmpl))
1648 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1649
1650 spec_entry *entry = ggc_alloc<spec_entry> ();
1651 gcc_assert (tmpl && args && spec);
1652 *entry = elt;
1653 *slot = entry;
1654 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1655 && PRIMARY_TEMPLATE_P (tmpl)
1656 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1657 || variable_template_p (t: tmpl))
1658 /* If TMPL is a forward declaration of a template function, keep a list
1659 of all specializations in case we need to reassign them to a friend
1660 template later in tsubst_friend_function.
1661
1662 Also keep a list of all variable template instantiations so that
1663 process_partial_specialization can check whether a later partial
1664 specialization would have used it. */
1665 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1666 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1667
1668 return spec;
1669}
1670
1671/* Restricts tree and type comparisons. */
1672int comparing_specializations;
1673int comparing_dependent_aliases;
1674
1675/* Whether we are comparing template arguments during partial ordering
1676 (and therefore want the comparison to look through dependent alias
1677 template specializations). */
1678
1679static int comparing_for_partial_ordering;
1680
1681/* Returns true iff two spec_entry nodes are equivalent. */
1682
1683bool
1684spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1685{
1686 int equal;
1687
1688 ++comparing_specializations;
1689 ++comparing_dependent_aliases;
1690 ++processing_template_decl;
1691 equal = (e1->tmpl == e2->tmpl
1692 && comp_template_args (e1->args, e2->args));
1693 if (equal && flag_concepts
1694 /* tmpl could be a FIELD_DECL for a capture pack. */
1695 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1696 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1697 && uses_template_parms (e1->args))
1698 {
1699 /* Partial specializations of a variable template can be distinguished by
1700 constraints. */
1701 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1702 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1703 equal = equivalent_constraints (c1, c2);
1704 }
1705 --processing_template_decl;
1706 --comparing_dependent_aliases;
1707 --comparing_specializations;
1708
1709 return equal;
1710}
1711
1712/* Returns a hash for a template TMPL and template arguments ARGS. */
1713
1714static hashval_t
1715hash_tmpl_and_args (tree tmpl, tree args)
1716{
1717 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1718 return iterative_hash_template_arg (arg: args, val);
1719}
1720
1721hashval_t
1722spec_hasher::hash (tree tmpl, tree args)
1723{
1724 ++comparing_specializations;
1725 hashval_t val = hash_tmpl_and_args (tmpl, args);
1726 --comparing_specializations;
1727 return val;
1728}
1729
1730/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1731 ignoring SPEC. */
1732
1733hashval_t
1734spec_hasher::hash (spec_entry *e)
1735{
1736 return spec_hasher::hash (tmpl: e->tmpl, args: e->args);
1737}
1738
1739/* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. We must be
1741 careful to (at least) skip the same entities template_args_equal
1742 does. */
1743
1744hashval_t
1745iterative_hash_template_arg (tree arg, hashval_t val)
1746{
1747 if (arg == NULL_TREE)
1748 return iterative_hash_object (arg, val);
1749
1750 if (!TYPE_P (arg))
1751 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1752 while (CONVERT_EXPR_P (arg)
1753 || TREE_CODE (arg) == NON_LVALUE_EXPR
1754 || class_nttp_const_wrapper_p (t: arg))
1755 arg = TREE_OPERAND (arg, 0);
1756
1757 enum tree_code code = TREE_CODE (arg);
1758
1759 val = iterative_hash_object (code, val);
1760
1761 switch (code)
1762 {
1763 case ARGUMENT_PACK_SELECT:
1764 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1765 preserving it in a hash table, which is bad because it will change
1766 meaning when gen_elem_of_pack_expansion_instantiation changes the
1767 ARGUMENT_PACK_SELECT_INDEX. */
1768 gcc_unreachable ();
1769
1770 case ERROR_MARK:
1771 return val;
1772
1773 case IDENTIFIER_NODE:
1774 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1775
1776 case TREE_VEC:
1777 for (tree elt : tree_vec_range (arg))
1778 val = iterative_hash_template_arg (arg: elt, val);
1779 return val;
1780
1781 case TYPE_PACK_EXPANSION:
1782 case EXPR_PACK_EXPANSION:
1783 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1784 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1785
1786 case TYPE_ARGUMENT_PACK:
1787 case NONTYPE_ARGUMENT_PACK:
1788 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1789
1790 case TREE_LIST:
1791 for (; arg; arg = TREE_CHAIN (arg))
1792 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1793 return val;
1794
1795 case OVERLOAD:
1796 for (lkp_iterator iter (arg); iter; ++iter)
1797 val = iterative_hash_template_arg (arg: *iter, val);
1798 return val;
1799
1800 case CONSTRUCTOR:
1801 {
1802 iterative_hash_template_arg (TREE_TYPE (arg), val);
1803 for (auto &e: CONSTRUCTOR_ELTS (arg))
1804 {
1805 val = iterative_hash_template_arg (arg: e.index, val);
1806 val = iterative_hash_template_arg (arg: e.value, val);
1807 }
1808 return val;
1809 }
1810
1811 case PARM_DECL:
1812 if (!DECL_ARTIFICIAL (arg))
1813 {
1814 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1815 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1816 }
1817 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1818
1819 case TEMPLATE_DECL:
1820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
1821 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1822 break;
1823
1824 case TARGET_EXPR:
1825 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1826
1827 case PTRMEM_CST:
1828 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1829 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1830
1831 case TEMPLATE_PARM_INDEX:
1832 val = iterative_hash_template_arg
1833 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1834 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1835 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1836
1837 case TRAIT_EXPR:
1838 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1839 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1840 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1841
1842 case BASELINK:
1843 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1844 val);
1845 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1846 val);
1847
1848 case MODOP_EXPR:
1849 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1850 code = TREE_CODE (TREE_OPERAND (arg, 1));
1851 val = iterative_hash_object (code, val);
1852 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1853
1854 case LAMBDA_EXPR:
1855 /* [temp.over.link] Two lambda-expressions are never considered
1856 equivalent.
1857
1858 So just hash the closure type. */
1859 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1860
1861 case CAST_EXPR:
1862 case IMPLICIT_CONV_EXPR:
1863 case STATIC_CAST_EXPR:
1864 case REINTERPRET_CAST_EXPR:
1865 case CONST_CAST_EXPR:
1866 case DYNAMIC_CAST_EXPR:
1867 case NEW_EXPR:
1868 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1869 /* Now hash operands as usual. */
1870 break;
1871
1872 case CALL_EXPR:
1873 {
1874 tree fn = CALL_EXPR_FN (arg);
1875 if (tree name = call_expr_dependent_name (arg))
1876 {
1877 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1878 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1879 fn = name;
1880 }
1881 val = iterative_hash_template_arg (arg: fn, val);
1882 call_expr_arg_iterator ai;
1883 for (tree x = first_call_expr_arg (exp: arg, iter: &ai); x;
1884 x = next_call_expr_arg (iter: &ai))
1885 val = iterative_hash_template_arg (arg: x, val);
1886 return val;
1887 }
1888
1889 default:
1890 break;
1891 }
1892
1893 char tclass = TREE_CODE_CLASS (code);
1894 switch (tclass)
1895 {
1896 case tcc_type:
1897 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1898 {
1899 // We want an alias specialization that survived strip_typedefs
1900 // to hash differently from its TYPE_CANONICAL, to avoid hash
1901 // collisions that compare as different in template_args_equal.
1902 // These could be dependent specializations that strip_typedefs
1903 // left alone, or untouched specializations because
1904 // coerce_template_parms returns the unconverted template
1905 // arguments if it sees incomplete argument packs.
1906 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1907 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1908 }
1909
1910 switch (code)
1911 {
1912 case DECLTYPE_TYPE:
1913 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1914 break;
1915
1916 case TYPENAME_TYPE:
1917 if (comparing_specializations)
1918 {
1919 /* Hash the components that are relevant to TYPENAME_TYPE
1920 equivalence as determined by structural_comptypes. We
1921 can only coherently do this when comparing_specializations
1922 is set, because otherwise structural_comptypes tries
1923 resolving TYPENAME_TYPE via the current instantiation. */
1924 tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
1925 tree fullname = TYPENAME_TYPE_FULLNAME (arg);
1926 val = iterative_hash_template_arg (arg: context, val);
1927 val = iterative_hash_template_arg (arg: fullname, val);
1928 }
1929 break;
1930
1931 default:
1932 if (tree canonical = TYPE_CANONICAL (arg))
1933 val = iterative_hash_object (TYPE_HASH (canonical), val);
1934 else if (tree ti = TYPE_TEMPLATE_INFO (arg))
1935 {
1936 val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
1937 val = iterative_hash_template_arg (TI_ARGS (ti), val);
1938 }
1939 break;
1940 }
1941
1942 return val;
1943
1944 case tcc_declaration:
1945 case tcc_constant:
1946 return iterative_hash_expr (tree: arg, seed: val);
1947
1948 default:
1949 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1950 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1951 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1952 return val;
1953 }
1954}
1955
1956/* Unregister the specialization SPEC as a specialization of TMPL.
1957 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1958 if the SPEC was listed as a specialization of TMPL.
1959
1960 Note that SPEC has been ggc_freed, so we can't look inside it. */
1961
1962bool
1963reregister_specialization (tree spec, tree tinfo, tree new_spec)
1964{
1965 spec_entry *entry;
1966 spec_entry elt;
1967
1968 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1969 elt.args = TI_ARGS (tinfo);
1970 elt.spec = NULL_TREE;
1971
1972 entry = decl_specializations->find (value: &elt);
1973 if (entry != NULL)
1974 {
1975 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1976 gcc_assert (new_spec != NULL_TREE);
1977 entry->spec = new_spec;
1978 return 1;
1979 }
1980
1981 return 0;
1982}
1983
1984/* Like register_specialization, but for local declarations. We are
1985 registering SPEC, an instantiation of TMPL. */
1986
1987void
1988register_local_specialization (tree spec, tree tmpl)
1989{
1990 gcc_assert (tmpl != spec);
1991 local_specializations->put (k: tmpl, v: spec);
1992}
1993
1994/* Registers T as a specialization of itself. This is used to preserve
1995 the references to already-parsed parameters when instantiating
1996 postconditions. */
1997
1998void
1999register_local_identity (tree t)
2000{
2001 local_specializations->put (k: t, v: t);
2002}
2003
2004/* TYPE is a class type. Returns true if TYPE is an explicitly
2005 specialized class. */
2006
2007bool
2008explicit_class_specialization_p (tree type)
2009{
2010 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2011 return false;
2012 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2013}
2014
2015/* Print the list of functions at FNS, going through all the overloads
2016 for each element of the list. Alternatively, FNS cannot be a
2017 TREE_LIST, in which case it will be printed together with all the
2018 overloads.
2019
2020 MORE and *STR should respectively be FALSE and NULL when the function
2021 is called from the outside. They are used internally on recursive
2022 calls. print_candidates manages the two parameters and leaves NULL
2023 in *STR when it ends. */
2024
2025static void
2026print_candidates_1 (tree fns, char **str, bool more = false)
2027{
2028 if (TREE_CODE (fns) == TREE_LIST)
2029 for (; fns; fns = TREE_CHAIN (fns))
2030 print_candidates_1 (TREE_VALUE (fns), str, more: more || TREE_CHAIN (fns));
2031 else
2032 for (lkp_iterator iter (fns); iter;)
2033 {
2034 tree cand = *iter;
2035 ++iter;
2036
2037 const char *pfx = *str;
2038 if (!pfx)
2039 {
2040 if (more || iter)
2041 pfx = _("candidates are:");
2042 else
2043 pfx = _("candidate is:");
2044 *str = get_spaces (pfx);
2045 }
2046 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2047 }
2048}
2049
2050/* Print the list of candidate FNS in an error message. FNS can also
2051 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2052
2053void
2054print_candidates (tree fns)
2055{
2056 char *str = NULL;
2057 print_candidates_1 (fns, str: &str);
2058 free (ptr: str);
2059}
2060
2061/* Get a (possibly) constrained template declaration for the
2062 purpose of ordering candidates. */
2063static tree
2064get_template_for_ordering (tree list)
2065{
2066 gcc_assert (TREE_CODE (list) == TREE_LIST);
2067 tree f = TREE_VALUE (list);
2068 if (tree ti = DECL_TEMPLATE_INFO (f))
2069 return TI_TEMPLATE (ti);
2070 return f;
2071}
2072
2073/* Among candidates having the same signature, return the
2074 most constrained or NULL_TREE if there is no best candidate.
2075 If the signatures of candidates vary (e.g., template
2076 specialization vs. member function), then there can be no
2077 most constrained.
2078
2079 Note that we don't compare constraints on the functions
2080 themselves, but rather those of their templates. */
2081static tree
2082most_constrained_function (tree candidates)
2083{
2084 // Try to find the best candidate in a first pass.
2085 tree champ = candidates;
2086 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2087 {
2088 int winner = more_constrained (get_template_for_ordering (list: champ),
2089 get_template_for_ordering (list: c));
2090 if (winner == -1)
2091 champ = c; // The candidate is more constrained
2092 else if (winner == 0)
2093 return NULL_TREE; // Neither is more constrained
2094 }
2095
2096 // Verify that the champ is better than previous candidates.
2097 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2098 if (!more_constrained (get_template_for_ordering (list: champ),
2099 get_template_for_ordering (list: c)))
2100 return NULL_TREE;
2101 }
2102
2103 return champ;
2104}
2105
2106
2107/* Returns the template (one of the functions given by TEMPLATE_ID)
2108 which can be specialized to match the indicated DECL with the
2109 explicit template args given in TEMPLATE_ID. The DECL may be
2110 NULL_TREE if none is available. In that case, the functions in
2111 TEMPLATE_ID are non-members.
2112
2113 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2114 specialization of a member template.
2115
2116 The TEMPLATE_COUNT is the number of references to qualifying
2117 template classes that appeared in the name of the function. See
2118 check_explicit_specialization for a more accurate description.
2119
2120 TSK indicates what kind of template declaration (if any) is being
2121 declared. TSK_TEMPLATE indicates that the declaration given by
2122 DECL, though a FUNCTION_DECL, has template parameters, and is
2123 therefore a template function.
2124
2125 The template args (those explicitly specified and those deduced)
2126 are output in a newly created vector *TARGS_OUT.
2127
2128 If it is impossible to determine the result, an error message is
2129 issued. The error_mark_node is returned to indicate failure. */
2130
2131static tree
2132determine_specialization (tree template_id,
2133 tree decl,
2134 tree* targs_out,
2135 int need_member_template,
2136 int template_count,
2137 tmpl_spec_kind tsk)
2138{
2139 tree fns;
2140 tree targs;
2141 tree explicit_targs;
2142 tree candidates = NULL_TREE;
2143
2144 /* A TREE_LIST of templates of which DECL may be a specialization.
2145 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2146 corresponding TREE_PURPOSE is the set of template arguments that,
2147 when used to instantiate the template, would produce a function
2148 with the signature of DECL. */
2149 tree templates = NULL_TREE;
2150 int header_count;
2151 cp_binding_level *b;
2152
2153 *targs_out = NULL_TREE;
2154
2155 if (template_id == error_mark_node || decl == error_mark_node)
2156 return error_mark_node;
2157
2158 /* We shouldn't be specializing a member template of an
2159 unspecialized class template; we already gave an error in
2160 check_specialization_scope, now avoid crashing. */
2161 if (!VAR_P (decl)
2162 && template_count && DECL_CLASS_SCOPE_P (decl)
2163 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2164 {
2165 gcc_assert (errorcount);
2166 return error_mark_node;
2167 }
2168
2169 fns = TREE_OPERAND (template_id, 0);
2170 explicit_targs = TREE_OPERAND (template_id, 1);
2171
2172 if (fns == error_mark_node)
2173 return error_mark_node;
2174
2175 /* Check for baselinks. */
2176 if (BASELINK_P (fns))
2177 fns = BASELINK_FUNCTIONS (fns);
2178
2179 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2180 {
2181 error_at (DECL_SOURCE_LOCATION (decl),
2182 "%qD is not a function template", fns);
2183 return error_mark_node;
2184 }
2185 else if (VAR_P (decl) && !variable_template_p (t: fns))
2186 {
2187 error ("%qD is not a variable template", fns);
2188 return error_mark_node;
2189 }
2190
2191 /* Count the number of template headers specified for this
2192 specialization. */
2193 header_count = 0;
2194 for (b = current_binding_level;
2195 b->kind == sk_template_parms;
2196 b = b->level_chain)
2197 ++header_count;
2198
2199 tree orig_fns = fns;
2200 bool header_mismatch = false;
2201
2202 if (variable_template_p (t: fns))
2203 {
2204 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2205 targs = coerce_template_parms (parms, explicit_targs, fns,
2206 tf_warning_or_error);
2207 if (targs != error_mark_node
2208 && constraints_satisfied_p (fns, targs))
2209 templates = tree_cons (targs, fns, templates);
2210 }
2211 else for (lkp_iterator iter (fns); iter; ++iter)
2212 {
2213 tree fn = *iter;
2214
2215 if (TREE_CODE (fn) == TEMPLATE_DECL)
2216 {
2217 tree decl_arg_types;
2218 tree fn_arg_types;
2219
2220 /* In case of explicit specialization, we need to check if
2221 the number of template headers appearing in the specialization
2222 is correct. This is usually done in check_explicit_specialization,
2223 but the check done there cannot be exhaustive when specializing
2224 member functions. Consider the following code:
2225
2226 template <> void A<int>::f(int);
2227 template <> template <> void A<int>::f(int);
2228
2229 Assuming that A<int> is not itself an explicit specialization
2230 already, the first line specializes "f" which is a non-template
2231 member function, whilst the second line specializes "f" which
2232 is a template member function. So both lines are syntactically
2233 correct, and check_explicit_specialization does not reject
2234 them.
2235
2236 Here, we can do better, as we are matching the specialization
2237 against the declarations. We count the number of template
2238 headers, and we check if they match TEMPLATE_COUNT + 1
2239 (TEMPLATE_COUNT is the number of qualifying template classes,
2240 plus there must be another header for the member template
2241 itself).
2242
2243 Notice that if header_count is zero, this is not a
2244 specialization but rather a template instantiation, so there
2245 is no check we can perform here. */
2246 if (header_count && header_count != template_count + 1)
2247 {
2248 header_mismatch = true;
2249 continue;
2250 }
2251
2252 /* Check that the number of template arguments at the
2253 innermost level for DECL is the same as for FN. */
2254 if (current_binding_level->kind == sk_template_parms
2255 && !current_binding_level->explicit_spec_p
2256 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2257 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2258 (current_template_parms))))
2259 continue;
2260
2261 /* DECL might be a specialization of FN. */
2262 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2263 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2264
2265 /* For a non-static member function, we need to make sure
2266 that the const qualification is the same. Since
2267 get_bindings does not try to merge the "this" parameter,
2268 we must do the comparison explicitly. */
2269 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
2270 {
2271 if (!same_type_p (TREE_VALUE (fn_arg_types),
2272 TREE_VALUE (decl_arg_types)))
2273 continue;
2274
2275 /* And the ref-qualification. */
2276 if (type_memfn_rqual (TREE_TYPE (decl))
2277 != type_memfn_rqual (TREE_TYPE (fn)))
2278 continue;
2279 }
2280
2281 /* Skip the "this" parameter and, for constructors of
2282 classes with virtual bases, the VTT parameter. A
2283 full specialization of a constructor will have a VTT
2284 parameter, but a template never will. */
2285 decl_arg_types
2286 = skip_artificial_parms_for (decl, decl_arg_types);
2287 fn_arg_types
2288 = skip_artificial_parms_for (fn, fn_arg_types);
2289
2290 /* Function templates cannot be specializations; there are
2291 no partial specializations of functions. Therefore, if
2292 the type of DECL does not match FN, there is no
2293 match.
2294
2295 Note that it should never be the case that we have both
2296 candidates added here, and for regular member functions
2297 below. */
2298 if (tsk == tsk_template)
2299 {
2300 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2301 current_template_parms))
2302 continue;
2303 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2304 TREE_TYPE (TREE_TYPE (fn))))
2305 continue;
2306 if (!compparms (fn_arg_types, decl_arg_types))
2307 continue;
2308
2309 tree freq = get_constraints (fn);
2310 tree dreq = get_constraints (decl);
2311 if (!freq != !dreq)
2312 continue;
2313 if (freq)
2314 {
2315 /* C++20 CA104: Substitute directly into the
2316 constraint-expression. */
2317 tree fargs = DECL_TI_ARGS (fn);
2318 tsubst_flags_t complain = tf_none;
2319 freq = tsubst_constraint_info (freq, fargs, complain, fn);
2320 if (!cp_tree_equal (freq, dreq))
2321 continue;
2322 }
2323
2324 candidates = tree_cons (NULL_TREE, fn, candidates);
2325 continue;
2326 }
2327
2328 /* See whether this function might be a specialization of this
2329 template. Suppress access control because we might be trying
2330 to make this specialization a friend, and we have already done
2331 access control for the declaration of the specialization. */
2332 push_deferring_access_checks (dk_no_check);
2333 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2334 pop_deferring_access_checks ();
2335
2336 if (!targs)
2337 /* We cannot deduce template arguments that when used to
2338 specialize TMPL will produce DECL. */
2339 continue;
2340
2341 if (uses_template_parms (targs))
2342 /* We deduced something involving 'auto', which isn't a valid
2343 template argument. */
2344 continue;
2345
2346 /* Save this template, and the arguments deduced. */
2347 templates = tree_cons (targs, fn, templates);
2348 }
2349 else if (need_member_template)
2350 /* FN is an ordinary member function, and we need a
2351 specialization of a member template. */
2352 ;
2353 else if (TREE_CODE (fn) != FUNCTION_DECL)
2354 /* We can get IDENTIFIER_NODEs here in certain erroneous
2355 cases. */
2356 ;
2357 else if (!DECL_FUNCTION_MEMBER_P (fn))
2358 /* This is just an ordinary non-member function. Nothing can
2359 be a specialization of that. */
2360 ;
2361 else if (DECL_ARTIFICIAL (fn))
2362 /* Cannot specialize functions that are created implicitly. */
2363 ;
2364 else
2365 {
2366 tree decl_arg_types;
2367
2368 /* This is an ordinary member function. However, since
2369 we're here, we can assume its enclosing class is a
2370 template class. For example,
2371
2372 template <typename T> struct S { void f(); };
2373 template <> void S<int>::f() {}
2374
2375 Here, S<int>::f is a non-template, but S<int> is a
2376 template class. If FN has the same type as DECL, we
2377 might be in business. */
2378
2379 if (!DECL_TEMPLATE_INFO (fn))
2380 /* Its enclosing class is an explicit specialization
2381 of a template class. This is not a candidate. */
2382 continue;
2383
2384 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2385 TREE_TYPE (TREE_TYPE (fn))))
2386 /* The return types differ. */
2387 continue;
2388
2389 /* Adjust the type of DECL in case FN is a static member. */
2390 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2391 if (DECL_STATIC_FUNCTION_P (fn)
2392 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2393 decl_arg_types = TREE_CHAIN (decl_arg_types);
2394
2395 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2396 decl_arg_types))
2397 continue;
2398
2399 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2400 && (type_memfn_rqual (TREE_TYPE (decl))
2401 != type_memfn_rqual (TREE_TYPE (fn))))
2402 continue;
2403
2404 // If the deduced arguments do not satisfy the constraints,
2405 // this is not a candidate.
2406 if (flag_concepts && !constraints_satisfied_p (fn))
2407 continue;
2408
2409 // Add the candidate.
2410 candidates = tree_cons (NULL_TREE, fn, candidates);
2411 }
2412 }
2413
2414 if (templates && TREE_CHAIN (templates))
2415 {
2416 /* We have:
2417
2418 [temp.expl.spec]
2419
2420 It is possible for a specialization with a given function
2421 signature to be instantiated from more than one function
2422 template. In such cases, explicit specification of the
2423 template arguments must be used to uniquely identify the
2424 function template specialization being specialized.
2425
2426 Note that here, there's no suggestion that we're supposed to
2427 determine which of the candidate templates is most
2428 specialized. However, we, also have:
2429
2430 [temp.func.order]
2431
2432 Partial ordering of overloaded function template
2433 declarations is used in the following contexts to select
2434 the function template to which a function template
2435 specialization refers:
2436
2437 -- when an explicit specialization refers to a function
2438 template.
2439
2440 So, we do use the partial ordering rules, at least for now.
2441 This extension can only serve to make invalid programs valid,
2442 so it's safe. And, there is strong anecdotal evidence that
2443 the committee intended the partial ordering rules to apply;
2444 the EDG front end has that behavior, and John Spicer claims
2445 that the committee simply forgot to delete the wording in
2446 [temp.expl.spec]. */
2447 tree tmpl = most_specialized_instantiation (templates);
2448 if (tmpl != error_mark_node)
2449 {
2450 templates = tmpl;
2451 TREE_CHAIN (templates) = NULL_TREE;
2452 }
2453 }
2454
2455 // Concepts allows multiple declarations of member functions
2456 // with the same signature. Like above, we need to rely on
2457 // on the partial ordering of those candidates to determine which
2458 // is the best.
2459 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2460 {
2461 if (tree cand = most_constrained_function (candidates))
2462 {
2463 candidates = cand;
2464 TREE_CHAIN (cand) = NULL_TREE;
2465 }
2466 }
2467
2468 if (templates == NULL_TREE && candidates == NULL_TREE)
2469 {
2470 error ("template-id %qD for %q+D does not match any template "
2471 "declaration", template_id, decl);
2472 if (header_mismatch)
2473 inform (DECL_SOURCE_LOCATION (decl),
2474 "saw %d %<template<>%>, need %d for "
2475 "specializing a member function template",
2476 header_count, template_count + 1);
2477 print_candidates (fns: orig_fns);
2478 return error_mark_node;
2479 }
2480 else if ((templates && TREE_CHAIN (templates))
2481 || (candidates && TREE_CHAIN (candidates))
2482 || (templates && candidates))
2483 {
2484 error ("ambiguous template specialization %qD for %q+D",
2485 template_id, decl);
2486 candidates = chainon (candidates, templates);
2487 print_candidates (fns: candidates);
2488 return error_mark_node;
2489 }
2490
2491 /* We have one, and exactly one, match. */
2492 if (candidates)
2493 {
2494 tree fn = TREE_VALUE (candidates);
2495 *targs_out = copy_node (DECL_TI_ARGS (fn));
2496
2497 /* Propagate the candidate's constraints to the declaration. */
2498 if (tsk != tsk_template)
2499 set_constraints (decl, get_constraints (fn));
2500
2501 /* DECL is a re-declaration or partial instantiation of a template
2502 function. */
2503 if (TREE_CODE (fn) == TEMPLATE_DECL)
2504 return fn;
2505 /* It was a specialization of an ordinary member function in a
2506 template class. */
2507 return DECL_TI_TEMPLATE (fn);
2508 }
2509
2510 /* It was a specialization of a template. */
2511 tree tmpl = TREE_VALUE (templates);
2512 *targs_out = add_outermost_template_args (args: tmpl, TREE_PURPOSE (templates));
2513
2514 /* Propagate the template's constraints to the declaration. */
2515 if (tsk != tsk_template)
2516 set_constraints (decl, get_constraints (tmpl));
2517
2518 return tmpl;
2519}
2520
2521/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2522 but with the default argument values filled in from those in the
2523 TMPL_TYPES. */
2524
2525static tree
2526copy_default_args_to_explicit_spec_1 (tree spec_types,
2527 tree tmpl_types)
2528{
2529 tree new_spec_types;
2530
2531 if (!spec_types)
2532 return NULL_TREE;
2533
2534 if (spec_types == void_list_node)
2535 return void_list_node;
2536
2537 /* Substitute into the rest of the list. */
2538 new_spec_types =
2539 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2540 TREE_CHAIN (tmpl_types));
2541
2542 /* Add the default argument for this parameter. */
2543 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2544 TREE_VALUE (spec_types),
2545 new_spec_types);
2546}
2547
2548/* DECL is an explicit specialization. Replicate default arguments
2549 from the template it specializes. (That way, code like:
2550
2551 template <class T> void f(T = 3);
2552 template <> void f(double);
2553 void g () { f (); }
2554
2555 works, as required.) An alternative approach would be to look up
2556 the correct default arguments at the call-site, but this approach
2557 is consistent with how implicit instantiations are handled. */
2558
2559static void
2560copy_default_args_to_explicit_spec (tree decl)
2561{
2562 tree tmpl;
2563 tree spec_types;
2564 tree tmpl_types;
2565 tree new_spec_types;
2566 tree old_type;
2567 tree new_type;
2568 tree t;
2569 tree object_type = NULL_TREE;
2570 tree in_charge = NULL_TREE;
2571 tree vtt = NULL_TREE;
2572
2573 /* See if there's anything we need to do. */
2574 tmpl = DECL_TI_TEMPLATE (decl);
2575 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2576 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2577 if (TREE_PURPOSE (t))
2578 break;
2579 if (!t)
2580 return;
2581
2582 old_type = TREE_TYPE (decl);
2583 spec_types = TYPE_ARG_TYPES (old_type);
2584
2585 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2586 {
2587 /* Remove the this pointer, but remember the object's type for
2588 CV quals. */
2589 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2590 spec_types = TREE_CHAIN (spec_types);
2591 tmpl_types = TREE_CHAIN (tmpl_types);
2592
2593 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2594 {
2595 /* DECL may contain more parameters than TMPL due to the extra
2596 in-charge parameter in constructors and destructors. */
2597 in_charge = spec_types;
2598 spec_types = TREE_CHAIN (spec_types);
2599 }
2600 if (DECL_HAS_VTT_PARM_P (decl))
2601 {
2602 vtt = spec_types;
2603 spec_types = TREE_CHAIN (spec_types);
2604 }
2605 }
2606
2607 /* Compute the merged default arguments. */
2608 new_spec_types =
2609 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2610
2611 /* Compute the new FUNCTION_TYPE. */
2612 if (object_type)
2613 {
2614 if (vtt)
2615 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2616 TREE_VALUE (vtt),
2617 new_spec_types);
2618
2619 if (in_charge)
2620 /* Put the in-charge parameter back. */
2621 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2622 TREE_VALUE (in_charge),
2623 new_spec_types);
2624
2625 new_type = build_method_type_directly (object_type,
2626 TREE_TYPE (old_type),
2627 new_spec_types);
2628 }
2629 else
2630 new_type = build_function_type (TREE_TYPE (old_type),
2631 new_spec_types);
2632 new_type = cp_build_type_attribute_variant (new_type,
2633 TYPE_ATTRIBUTES (old_type));
2634 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2635
2636 TREE_TYPE (decl) = new_type;
2637}
2638
2639/* Return the number of template headers we expect to see for a definition
2640 or specialization of CTYPE or one of its non-template members. */
2641
2642int
2643num_template_headers_for_class (tree ctype)
2644{
2645 int num_templates = 0;
2646
2647 while (ctype && CLASS_TYPE_P (ctype))
2648 {
2649 /* You're supposed to have one `template <...>' for every
2650 template class, but you don't need one for a full
2651 specialization. For example:
2652
2653 template <class T> struct S{};
2654 template <> struct S<int> { void f(); };
2655 void S<int>::f () {}
2656
2657 is correct; there shouldn't be a `template <>' for the
2658 definition of `S<int>::f'. */
2659 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2660 /* If CTYPE does not have template information of any
2661 kind, then it is not a template, nor is it nested
2662 within a template. */
2663 break;
2664 if (explicit_class_specialization_p (type: ctype))
2665 break;
2666 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2667 ++num_templates;
2668
2669 ctype = TYPE_CONTEXT (ctype);
2670 }
2671
2672 return num_templates;
2673}
2674
2675/* Do a simple sanity check on the template headers that precede the
2676 variable declaration DECL. */
2677
2678void
2679check_template_variable (tree decl)
2680{
2681 tree ctx = CP_DECL_CONTEXT (decl);
2682 int wanted = num_template_headers_for_class (ctype: ctx);
2683 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2684 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2685 {
2686 if (cxx_dialect < cxx14)
2687 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2688 "variable templates only available with "
2689 "%<-std=c++14%> or %<-std=gnu++14%>");
2690
2691 // Namespace-scope variable templates should have a template header.
2692 ++wanted;
2693 }
2694 if (template_header_count > wanted)
2695 {
2696 auto_diagnostic_group d;
2697 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2698 "too many template headers for %qD "
2699 "(should be %d)",
2700 decl, wanted);
2701 if (warned && CLASS_TYPE_P (ctx)
2702 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2703 inform (DECL_SOURCE_LOCATION (decl),
2704 "members of an explicitly specialized class are defined "
2705 "without a template header");
2706 }
2707}
2708
2709/* An explicit specialization whose declarator-id or class-head-name is not
2710 qualified shall be declared in the nearest enclosing namespace of the
2711 template, or, if the namespace is inline (7.3.1), any namespace from its
2712 enclosing namespace set.
2713
2714 If the name declared in the explicit instantiation is an unqualified name,
2715 the explicit instantiation shall appear in the namespace where its template
2716 is declared or, if that namespace is inline (7.3.1), any namespace from its
2717 enclosing namespace set. */
2718
2719void
2720check_unqualified_spec_or_inst (tree t, location_t loc)
2721{
2722 tree tmpl = most_general_template (t);
2723 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2724 && !is_nested_namespace (current_namespace,
2725 CP_DECL_CONTEXT (tmpl), inline_only: true))
2726 {
2727 if (processing_specialization)
2728 permerror (loc, "explicit specialization of %qD outside its "
2729 "namespace must use a nested-name-specifier", tmpl);
2730 else if (processing_explicit_instantiation
2731 && cxx_dialect >= cxx11)
2732 /* This was allowed in C++98, so only pedwarn. */
2733 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2734 "outside its namespace must use a nested-name-"
2735 "specifier", tmpl);
2736 }
2737}
2738
2739/* Warn for a template specialization SPEC that is missing some of a set
2740 of function or type attributes that the template TEMPL is declared with.
2741 ATTRLIST is a list of additional attributes that SPEC should be taken
2742 to ultimately be declared with. */
2743
2744static void
2745warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2746{
2747 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2748 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2749
2750 /* Avoid warning if the difference between the primary and
2751 the specialization is not in one of the attributes below. */
2752 const char* const blacklist[] = {
2753 "alloc_align", "alloc_size", "assume_aligned", "format",
2754 "format_arg", "malloc", "nonnull", NULL
2755 };
2756
2757 /* Put together a list of the black listed attributes that the primary
2758 template is declared with that the specialization is not, in case
2759 it's not apparent from the most recent declaration of the primary. */
2760 pretty_printer str;
2761 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2762 blacklist, &str);
2763
2764 if (!nattrs)
2765 return;
2766
2767 auto_diagnostic_group d;
2768 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2769 "explicit specialization %q#D may be missing attributes",
2770 spec))
2771 inform (DECL_SOURCE_LOCATION (tmpl),
2772 nattrs > 1
2773 ? G_("missing primary template attributes %s")
2774 : G_("missing primary template attribute %s"),
2775 pp_formatted_text (&str));
2776}
2777
2778/* Check to see if the function just declared, as indicated in
2779 DECLARATOR, and in DECL, is a specialization of a function
2780 template. We may also discover that the declaration is an explicit
2781 instantiation at this point.
2782
2783 Returns DECL, or an equivalent declaration that should be used
2784 instead if all goes well. Issues an error message if something is
2785 amiss. Returns error_mark_node if the error is not easily
2786 recoverable.
2787
2788 FLAGS is a bitmask consisting of the following flags:
2789
2790 2: The function has a definition.
2791 4: The function is a friend.
2792
2793 The TEMPLATE_COUNT is the number of references to qualifying
2794 template classes that appeared in the name of the function. For
2795 example, in
2796
2797 template <class T> struct S { void f(); };
2798 void S<int>::f();
2799
2800 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2801 classes are not counted in the TEMPLATE_COUNT, so that in
2802
2803 template <class T> struct S {};
2804 template <> struct S<int> { void f(); }
2805 template <> void S<int>::f();
2806
2807 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2808 invalid; there should be no template <>.)
2809
2810 If the function is a specialization, it is marked as such via
2811 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2812 is set up correctly, and it is added to the list of specializations
2813 for that template. */
2814
2815tree
2816check_explicit_specialization (tree declarator,
2817 tree decl,
2818 int template_count,
2819 int flags,
2820 tree attrlist)
2821{
2822 int have_def = flags & 2;
2823 int is_friend = flags & 4;
2824 bool is_concept = flags & 8;
2825 int specialization = 0;
2826 int explicit_instantiation = 0;
2827 int member_specialization = 0;
2828 tree ctype = DECL_CLASS_CONTEXT (decl);
2829 tree dname = DECL_NAME (decl);
2830 tmpl_spec_kind tsk;
2831
2832 if (is_friend)
2833 {
2834 if (!processing_specialization)
2835 tsk = tsk_none;
2836 else
2837 tsk = tsk_excessive_parms;
2838 }
2839 else
2840 tsk = current_tmpl_spec_kind (template_count);
2841
2842 switch (tsk)
2843 {
2844 case tsk_none:
2845 if (processing_specialization && !VAR_P (decl))
2846 {
2847 specialization = 1;
2848 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2849 }
2850 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
2851 || (DECL_LANG_SPECIFIC (decl)
2852 && DECL_IMPLICIT_INSTANTIATION (decl)))
2853 {
2854 if (is_friend)
2855 /* This could be something like:
2856
2857 template <class T> void f(T);
2858 class S { friend void f<>(int); } */
2859 specialization = 1;
2860 else
2861 {
2862 /* This case handles bogus declarations like template <>
2863 template <class T> void f<int>(); */
2864
2865 error_at (cp_expr_loc_or_input_loc (t: declarator),
2866 "template-id %qE in declaration of primary template",
2867 declarator);
2868 return decl;
2869 }
2870 }
2871 break;
2872
2873 case tsk_invalid_member_spec:
2874 /* The error has already been reported in
2875 check_specialization_scope. */
2876 return error_mark_node;
2877
2878 case tsk_invalid_expl_inst:
2879 error ("template parameter list used in explicit instantiation");
2880
2881 /* Fall through. */
2882
2883 case tsk_expl_inst:
2884 if (have_def)
2885 error ("definition provided for explicit instantiation");
2886
2887 explicit_instantiation = 1;
2888 break;
2889
2890 case tsk_excessive_parms:
2891 case tsk_insufficient_parms:
2892 if (tsk == tsk_excessive_parms)
2893 error ("too many template parameter lists in declaration of %qD",
2894 decl);
2895 else if (template_header_count)
2896 error("too few template parameter lists in declaration of %qD", decl);
2897 else
2898 error("explicit specialization of %qD must be introduced by "
2899 "%<template <>%>", decl);
2900
2901 /* Fall through. */
2902 case tsk_expl_spec:
2903 if (is_concept)
2904 error ("explicit specialization declared %<concept%>");
2905
2906 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2907 /* In cases like template<> constexpr bool v = true;
2908 We'll give an error in check_template_variable. */
2909 break;
2910
2911 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2912 if (ctype)
2913 member_specialization = 1;
2914 else
2915 specialization = 1;
2916 break;
2917
2918 case tsk_template:
2919 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2920 {
2921 /* This case handles bogus declarations like template <>
2922 template <class T> void f<int>(); */
2923
2924 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2925 error_at (cp_expr_loc_or_input_loc (t: declarator),
2926 "template-id %qE in declaration of primary template",
2927 declarator);
2928 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2929 {
2930 /* Partial specialization of variable template. */
2931 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2932 specialization = 1;
2933 goto ok;
2934 }
2935 else if (cxx_dialect < cxx14)
2936 error_at (cp_expr_loc_or_input_loc (t: declarator),
2937 "non-type partial specialization %qE "
2938 "is not allowed", declarator);
2939 else
2940 error_at (cp_expr_loc_or_input_loc (t: declarator),
2941 "non-class, non-variable partial specialization %qE "
2942 "is not allowed", declarator);
2943 return decl;
2944 ok:;
2945 }
2946
2947 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2948 /* This is a specialization of a member template, without
2949 specialization the containing class. Something like:
2950
2951 template <class T> struct S {
2952 template <class U> void f (U);
2953 };
2954 template <> template <class U> void S<int>::f(U) {}
2955
2956 That's a specialization -- but of the entire template. */
2957 specialization = 1;
2958 break;
2959
2960 default:
2961 gcc_unreachable ();
2962 }
2963
2964 if ((specialization || member_specialization)
2965 /* This doesn't apply to variable templates. */
2966 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2967 {
2968 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2969 for (; t; t = TREE_CHAIN (t))
2970 if (TREE_PURPOSE (t))
2971 {
2972 permerror (input_location,
2973 "default argument specified in explicit specialization");
2974 break;
2975 }
2976 }
2977
2978 if (specialization || member_specialization || explicit_instantiation)
2979 {
2980 tree tmpl = NULL_TREE;
2981 tree targs = NULL_TREE;
2982 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2983 bool found_hidden = false;
2984
2985 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2986 if (!was_template_id)
2987 {
2988 tree fns;
2989
2990 gcc_assert (identifier_p (declarator));
2991 if (ctype)
2992 fns = dname;
2993 else
2994 {
2995 /* If there is no class context, the explicit instantiation
2996 must be at namespace scope. */
2997 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2998
2999 /* Find the namespace binding, using the declaration
3000 context. */
3001 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), name: dname,
3002 LOOK_want::NORMAL, true);
3003 if (fns == error_mark_node)
3004 {
3005 /* If lookup fails, look for a friend declaration so we can
3006 give a better diagnostic. */
3007 fns = (lookup_qualified_name
3008 (CP_DECL_CONTEXT (decl), name: dname,
3009 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3010 /*complain*/true));
3011 found_hidden = true;
3012 }
3013
3014 if (fns == error_mark_node || !is_overloaded_fn (fns))
3015 {
3016 error ("%qD is not a template function", dname);
3017 fns = error_mark_node;
3018 }
3019 }
3020
3021 declarator = lookup_template_function (fns, NULL_TREE);
3022 }
3023
3024 if (declarator == error_mark_node)
3025 return error_mark_node;
3026
3027 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3028 {
3029 if (!explicit_instantiation)
3030 /* A specialization in class scope. This is invalid,
3031 but the error will already have been flagged by
3032 check_specialization_scope. */
3033 return error_mark_node;
3034 else
3035 {
3036 /* It's not valid to write an explicit instantiation in
3037 class scope, e.g.:
3038
3039 class C { template void f(); }
3040
3041 This case is caught by the parser. However, on
3042 something like:
3043
3044 template class C { void f(); };
3045
3046 (which is invalid) we can get here. The error will be
3047 issued later. */
3048 ;
3049 }
3050
3051 return decl;
3052 }
3053 else if (ctype != NULL_TREE
3054 && (identifier_p (TREE_OPERAND (declarator, 0))))
3055 {
3056 // We'll match variable templates in start_decl.
3057 if (VAR_P (decl))
3058 return decl;
3059
3060 /* Find the list of functions in ctype that have the same
3061 name as the declared function. */
3062 tree name = TREE_OPERAND (declarator, 0);
3063
3064 if (constructor_name_p (name, ctype))
3065 {
3066 if (DECL_CONSTRUCTOR_P (decl)
3067 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3068 : !CLASSTYPE_DESTRUCTOR (ctype))
3069 {
3070 /* From [temp.expl.spec]:
3071
3072 If such an explicit specialization for the member
3073 of a class template names an implicitly-declared
3074 special member function (clause _special_), the
3075 program is ill-formed.
3076
3077 Similar language is found in [temp.explicit]. */
3078 error ("specialization of implicitly-declared special member function");
3079 return error_mark_node;
3080 }
3081
3082 name = DECL_NAME (decl);
3083 }
3084
3085 /* For a type-conversion operator, We might be looking for
3086 `operator int' which will be a specialization of
3087 `operator T'. Grab all the conversion operators, and
3088 then select from them. */
3089 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3090 ? conv_op_identifier : name);
3091
3092 if (fns == NULL_TREE)
3093 {
3094 error ("no member function %qD declared in %qT", name, ctype);
3095 return error_mark_node;
3096 }
3097 else
3098 TREE_OPERAND (declarator, 0) = fns;
3099 }
3100
3101 /* Figure out what exactly is being specialized at this point.
3102 Note that for an explicit instantiation, even one for a
3103 member function, we cannot tell a priori whether the
3104 instantiation is for a member template, or just a member
3105 function of a template class. Even if a member template is
3106 being instantiated, the member template arguments may be
3107 elided if they can be deduced from the rest of the
3108 declaration. */
3109 tmpl = determine_specialization (template_id: declarator, decl,
3110 targs_out: &targs,
3111 need_member_template: member_specialization,
3112 template_count,
3113 tsk);
3114
3115 if (!tmpl || tmpl == error_mark_node)
3116 /* We couldn't figure out what this declaration was
3117 specializing. */
3118 return error_mark_node;
3119 else
3120 {
3121 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3122 {
3123 auto_diagnostic_group d;
3124 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3125 "friend declaration %qD is not visible to "
3126 "explicit specialization", tmpl))
3127 inform (DECL_SOURCE_LOCATION (tmpl),
3128 "friend declaration here");
3129 }
3130
3131 if (!ctype && !is_friend
3132 && CP_DECL_CONTEXT (decl) == current_namespace)
3133 check_unqualified_spec_or_inst (t: tmpl, DECL_SOURCE_LOCATION (decl));
3134
3135 tree gen_tmpl = most_general_template (tmpl);
3136
3137 if (explicit_instantiation)
3138 {
3139 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3140 is done by do_decl_instantiation later. */
3141
3142 int arg_depth = TMPL_ARGS_DEPTH (targs);
3143 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3144
3145 if (arg_depth > parm_depth)
3146 {
3147 /* If TMPL is not the most general template (for
3148 example, if TMPL is a friend template that is
3149 injected into namespace scope), then there will
3150 be too many levels of TARGS. Remove some of them
3151 here. */
3152 int i;
3153 tree new_targs;
3154
3155 new_targs = make_tree_vec (parm_depth);
3156 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3157 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3158 = TREE_VEC_ELT (targs, i);
3159 targs = new_targs;
3160 }
3161
3162 return instantiate_template (tmpl, targs, tf_error);
3163 }
3164
3165 /* If we thought that the DECL was a member function, but it
3166 turns out to be specializing a static member function,
3167 make DECL a static member function as well. */
3168 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3169 && DECL_STATIC_FUNCTION_P (tmpl)
3170 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
3171 revert_static_member_fn (decl);
3172
3173 /* If this is a specialization of a member template of a
3174 template class, we want to return the TEMPLATE_DECL, not
3175 the specialization of it. */
3176 if (tsk == tsk_template && !was_template_id)
3177 {
3178 tree result = DECL_TEMPLATE_RESULT (tmpl);
3179 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3180 DECL_INITIAL (result) = NULL_TREE;
3181 if (have_def)
3182 {
3183 tree parm;
3184 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3185 DECL_SOURCE_LOCATION (result)
3186 = DECL_SOURCE_LOCATION (decl);
3187 /* We want to use the argument list specified in the
3188 definition, not in the original declaration. */
3189 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3190 for (parm = DECL_ARGUMENTS (result); parm;
3191 parm = DECL_CHAIN (parm))
3192 DECL_CONTEXT (parm) = result;
3193 }
3194 decl = register_specialization (spec: tmpl, tmpl: gen_tmpl, args: targs,
3195 is_friend, hash: 0);
3196 remove_contract_attributes (result);
3197 return decl;
3198 }
3199
3200 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3201 DECL_TEMPLATE_INFO (decl) = build_template_info (template_decl: tmpl, template_args: targs);
3202
3203 if (was_template_id)
3204 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3205
3206 /* Inherit default function arguments from the template
3207 DECL is specializing. */
3208 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3209 copy_default_args_to_explicit_spec (decl);
3210
3211 /* This specialization has the same protection as the
3212 template it specializes. */
3213 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3214 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3215
3216 /* 7.1.1-1 [dcl.stc]
3217
3218 A storage-class-specifier shall not be specified in an
3219 explicit specialization...
3220
3221 The parser rejects these, so unless action is taken here,
3222 explicit function specializations will always appear with
3223 global linkage.
3224
3225 The action recommended by the C++ CWG in response to C++
3226 defect report 605 is to make the storage class and linkage
3227 of the explicit specialization match the templated function:
3228
3229 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3230 */
3231 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3232 {
3233 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3234 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3235
3236 /* A concept cannot be specialized. */
3237 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3238 {
3239 error ("explicit specialization of function concept %qD",
3240 gen_tmpl);
3241 return error_mark_node;
3242 }
3243
3244 /* This specialization has the same linkage and visibility as
3245 the function template it specializes. */
3246 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3247 if (! TREE_PUBLIC (decl))
3248 {
3249 DECL_INTERFACE_KNOWN (decl) = 1;
3250 DECL_NOT_REALLY_EXTERN (decl) = 1;
3251 }
3252 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3253 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3254 {
3255 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3256 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3257 }
3258 }
3259
3260 /* If DECL is a friend declaration, declared using an
3261 unqualified name, the namespace associated with DECL may
3262 have been set incorrectly. For example, in:
3263
3264 template <typename T> void f(T);
3265 namespace N {
3266 struct S { friend void f<int>(int); }
3267 }
3268
3269 we will have set the DECL_CONTEXT for the friend
3270 declaration to N, rather than to the global namespace. */
3271 if (DECL_NAMESPACE_SCOPE_P (decl))
3272 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3273
3274 if (is_friend && !have_def)
3275 /* This is not really a declaration of a specialization.
3276 It's just the name of an instantiation. But, it's not
3277 a request for an instantiation, either. */
3278 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3279 else if (TREE_CODE (decl) == FUNCTION_DECL)
3280 /* A specialization is not necessarily COMDAT. */
3281 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3282 && DECL_DECLARED_INLINE_P (decl));
3283 else if (VAR_P (decl))
3284 DECL_COMDAT (decl) = false;
3285
3286 /* If this is a full specialization, register it so that we can find
3287 it again. Partial specializations will be registered in
3288 process_partial_specialization. */
3289 if (!processing_template_decl)
3290 {
3291 warn_spec_missing_attributes (tmpl: gen_tmpl, spec: decl, attrlist);
3292
3293 decl = register_specialization (spec: decl, tmpl: gen_tmpl, args: targs,
3294 is_friend, hash: 0);
3295 }
3296
3297 /* If this is a specialization, splice any contracts that may have
3298 been inherited from the template, removing them. */
3299 if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
3300 remove_contract_attributes (decl);
3301
3302 /* A 'structor should already have clones. */
3303 gcc_assert (decl == error_mark_node
3304 || variable_template_p (tmpl)
3305 || !(DECL_CONSTRUCTOR_P (decl)
3306 || DECL_DESTRUCTOR_P (decl))
3307 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3308 }
3309 }
3310
3311 return decl;
3312}
3313
3314/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3315 parameters. These are represented in the same format used for
3316 DECL_TEMPLATE_PARMS. */
3317
3318int
3319comp_template_parms (const_tree parms1, const_tree parms2)
3320{
3321 if (parms1 == parms2)
3322 return 1;
3323
3324 tree t1 = TREE_VALUE (parms1);
3325 tree t2 = TREE_VALUE (parms2);
3326 int i;
3327
3328 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3329 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3330
3331 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3332 return 0;
3333
3334 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3335 {
3336 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3337 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3338
3339 /* If either of the template parameters are invalid, assume
3340 they match for the sake of error recovery. */
3341 if (error_operand_p (t: parm1) || error_operand_p (t: parm2))
3342 return 1;
3343
3344 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3345 return 0;
3346
3347 if (TREE_CODE (parm1) == TYPE_DECL
3348 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
3349 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
3350 continue;
3351 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3352 return 0;
3353 }
3354
3355 return 1;
3356}
3357
3358/* Returns true if two template parameters are declared with
3359 equivalent constraints. */
3360
3361static bool
3362template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3363{
3364 tree req1 = TREE_TYPE (parm1);
3365 tree req2 = TREE_TYPE (parm2);
3366 if (!req1 != !req2)
3367 return false;
3368 if (req1)
3369 return cp_tree_equal (req1, req2);
3370 return true;
3371}
3372
3373/* Returns true when two template parameters are equivalent. */
3374
3375static bool
3376template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3377{
3378 tree decl1 = TREE_VALUE (parm1);
3379 tree decl2 = TREE_VALUE (parm2);
3380
3381 /* If either of the template parameters are invalid, assume
3382 they match for the sake of error recovery. */
3383 if (error_operand_p (t: decl1) || error_operand_p (t: decl2))
3384 return true;
3385
3386 /* ... they declare parameters of the same kind. */
3387 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3388 return false;
3389
3390 /* ... one parameter was introduced by a parameter declaration, then
3391 both are. This case arises as a result of eagerly rewriting declarations
3392 during parsing. */
3393 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
3394 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
3395 return false;
3396
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3399 return false;
3400
3401 if (TREE_CODE (decl1) == PARM_DECL)
3402 {
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3405 return false;
3406 }
3407 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3408 {
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1, decl2))
3412 return false;
3413 }
3414
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1, parm2);
3418}
3419
3420/* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3423
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3426
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3430
3431static bool
3432template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3433{
3434 if (parms1 == parms2)
3435 return true;
3436
3437 tree list1 = TREE_VALUE (parms1);
3438 tree list2 = TREE_VALUE (parms2);
3439
3440 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3441 return 0;
3442
3443 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3444 {
3445 tree parm1 = TREE_VEC_ELT (list1, i);
3446 tree parm2 = TREE_VEC_ELT (list2, i);
3447 if (!template_parameters_equivalent_p (parm1, parm2))
3448 return false;
3449 }
3450
3451 return true;
3452}
3453
3454/* Return true if the requires-clause of the template parameter lists are
3455 equivalent and false otherwise. */
3456static bool
3457template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3458{
3459 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3460 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3461 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3462 return false;
3463 if (!cp_tree_equal (req1, req2))
3464 return false;
3465 return true;
3466}
3467
3468/* Returns true if two template heads are equivalent. 17.6.6.1p6:
3469 Two template heads are equivalent if their template parameter
3470 lists are equivalent and their requires clauses are equivalent.
3471
3472 In pre-C++20, this is equivalent to calling comp_template_parms
3473 for the template parameters of TMPL1 and TMPL2. */
3474
3475bool
3476template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3477{
3478 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3479 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3480
3481 /* Don't change the matching rules for pre-C++20. */
3482 if (cxx_dialect < cxx20)
3483 return comp_template_parms (parms1, parms2);
3484
3485 /* ... have the same number of template parameters, and their
3486 corresponding parameters are equivalent. */
3487 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3488 return false;
3489
3490 /* ... if either has a requires-clause, they both do and their
3491 corresponding constraint-expressions are equivalent. */
3492 return template_requirements_equivalent_p (parms1, parms2);
3493}
3494
3495/* Determine whether PARM is a parameter pack. */
3496
3497bool
3498template_parameter_pack_p (const_tree parm)
3499{
3500 /* Determine if we have a non-type template parameter pack. */
3501 if (TREE_CODE (parm) == PARM_DECL)
3502 return (DECL_TEMPLATE_PARM_P (parm)
3503 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3504 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3505 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3506
3507 /* If this is a list of template parameters, we could get a
3508 TYPE_DECL or a TEMPLATE_DECL. */
3509 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3510 parm = TREE_TYPE (parm);
3511
3512 /* Otherwise it must be a type template parameter. */
3513 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3514 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3515 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3516}
3517
3518/* Determine if T is a function parameter pack. */
3519
3520bool
3521function_parameter_pack_p (const_tree t)
3522{
3523 if (t && TREE_CODE (t) == PARM_DECL)
3524 return DECL_PACK_P (t);
3525 return false;
3526}
3527
3528/* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3529 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3530
3531tree
3532get_function_template_decl (const_tree primary_func_tmpl_inst)
3533{
3534 if (! primary_func_tmpl_inst
3535 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3536 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3537 return NULL;
3538
3539 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3540}
3541
3542/* Return true iff the function parameter PARAM_DECL was expanded
3543 from the function parameter pack PACK. */
3544
3545bool
3546function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3547{
3548 if (DECL_ARTIFICIAL (param_decl)
3549 || !function_parameter_pack_p (t: pack))
3550 return false;
3551
3552 /* The parameter pack and its pack arguments have the same
3553 DECL_PARM_INDEX. */
3554 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3555}
3556
3557/* Determine whether ARGS describes a variadic template args list,
3558 i.e., one that is terminated by a template argument pack. */
3559
3560static bool
3561template_args_variadic_p (tree args)
3562{
3563 int nargs;
3564 tree last_parm;
3565
3566 if (args == NULL_TREE)
3567 return false;
3568
3569 args = INNERMOST_TEMPLATE_ARGS (args);
3570 nargs = TREE_VEC_LENGTH (args);
3571
3572 if (nargs == 0)
3573 return false;
3574
3575 last_parm = TREE_VEC_ELT (args, nargs - 1);
3576
3577 return ARGUMENT_PACK_P (last_parm);
3578}
3579
3580/* Generate a new name for the parameter pack name NAME (an
3581 IDENTIFIER_NODE) that incorporates its */
3582
3583static tree
3584make_ith_pack_parameter_name (tree name, int i)
3585{
3586 /* Munge the name to include the parameter index. */
3587#define NUMBUF_LEN 128
3588 char numbuf[NUMBUF_LEN];
3589 char* newname;
3590 int newname_len;
3591
3592 if (name == NULL_TREE)
3593 return name;
3594 snprintf (s: numbuf, NUMBUF_LEN, format: "%i", i);
3595 newname_len = IDENTIFIER_LENGTH (name)
3596 + strlen (s: numbuf) + 2;
3597 newname = (char*)alloca (newname_len);
3598 snprintf (s: newname, maxlen: newname_len,
3599 format: "%s#%i", IDENTIFIER_POINTER (name), i);
3600 return get_identifier (newname);
3601}
3602
3603/* Return true if T is a primary function, class or alias template
3604 specialization, not including the template pattern. */
3605
3606bool
3607primary_template_specialization_p (const_tree t)
3608{
3609 if (!t)
3610 return false;
3611
3612 if (VAR_OR_FUNCTION_DECL_P (t))
3613 return (DECL_LANG_SPECIFIC (t)
3614 && DECL_USE_TEMPLATE (t)
3615 && DECL_TEMPLATE_INFO (t)
3616 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3617 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3618 return (CLASSTYPE_TEMPLATE_INFO (t)
3619 && CLASSTYPE_USE_TEMPLATE (t)
3620 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3621 else if (alias_template_specialization_p (t, nt_transparent))
3622 return true;
3623 return false;
3624}
3625
3626/* Return true if PARM is a template template parameter. */
3627
3628bool
3629template_template_parameter_p (const_tree parm)
3630{
3631 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3632}
3633
3634/* Return true iff PARM is a DECL representing a type template
3635 parameter. */
3636
3637bool
3638template_type_parameter_p (const_tree parm)
3639{
3640 return (parm
3641 && (TREE_CODE (parm) == TYPE_DECL
3642 || TREE_CODE (parm) == TEMPLATE_DECL)
3643 && DECL_TEMPLATE_PARM_P (parm));
3644}
3645
3646/* Return the template parameters of T if T is a
3647 primary template instantiation, NULL otherwise. */
3648
3649tree
3650get_primary_template_innermost_parameters (const_tree t)
3651{
3652 tree parms = NULL, template_info = NULL;
3653
3654 if ((template_info = get_template_info (t))
3655 && primary_template_specialization_p (t))
3656 parms = INNERMOST_TEMPLATE_PARMS
3657 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3658
3659 return parms;
3660}
3661
3662/* Returns the template arguments of T if T is a template instantiation,
3663 NULL otherwise. */
3664
3665tree
3666get_template_innermost_arguments (const_tree t)
3667{
3668 tree args = NULL, template_info = NULL;
3669
3670 if ((template_info = get_template_info (t))
3671 && TI_ARGS (template_info))
3672 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3673
3674 return args;
3675}
3676
3677/* Return the argument pack elements of T if T is a template argument pack,
3678 NULL otherwise. */
3679
3680tree
3681get_template_argument_pack_elems (const_tree t)
3682{
3683 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3684 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3685 return NULL;
3686
3687 return ARGUMENT_PACK_ARGS (t);
3688}
3689
3690/* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3691 ARGUMENT_PACK_SELECT represents. */
3692
3693static tree
3694argument_pack_select_arg (tree t)
3695{
3696 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3697 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3698
3699 /* If the selected argument is an expansion E, that most likely means we were
3700 called from gen_elem_of_pack_expansion_instantiation during the
3701 substituting of an argument pack (of which the Ith element is a pack
3702 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3703 In this case, the Ith element resulting from this substituting is going to
3704 be a pack expansion, which pattern is the pattern of E. Let's return the
3705 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3706 resulting pack expansion from it. */
3707 if (PACK_EXPANSION_P (arg))
3708 {
3709 /* Make sure we aren't throwing away arg info. */
3710 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3711 arg = PACK_EXPANSION_PATTERN (arg);
3712 }
3713
3714 return arg;
3715}
3716
3717/* Return a modification of ARGS that's suitable for preserving inside a hash
3718 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3719 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3720
3721static tree
3722preserve_args (tree args, bool cow_p = true)
3723{
3724 if (!args)
3725 return NULL_TREE;
3726
3727 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
3728 {
3729 tree t = TREE_VEC_ELT (args, i);
3730 tree r;
3731 if (!t)
3732 r = NULL_TREE;
3733 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
3734 r = argument_pack_select_arg (t);
3735 else if (TREE_CODE (t) == TREE_VEC)
3736 r = preserve_args (args: t, cow_p);
3737 else
3738 r = t;
3739 if (r != t)
3740 {
3741 if (cow_p)
3742 {
3743 args = copy_template_args (args);
3744 cow_p = false;
3745 }
3746 TREE_VEC_ELT (args, i) = r;
3747 }
3748 }
3749
3750 return args;
3751}
3752
3753/* True iff FN is a function representing a built-in variadic parameter
3754 pack. */
3755
3756bool
3757builtin_pack_fn_p (tree fn)
3758{
3759 if (!fn
3760 || TREE_CODE (fn) != FUNCTION_DECL
3761 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3762 return false;
3763
3764 if (id_equal (DECL_NAME (fn), str: "__integer_pack"))
3765 return true;
3766
3767 return false;
3768}
3769
3770/* True iff CALL is a call to a function representing a built-in variadic
3771 parameter pack. */
3772
3773static bool
3774builtin_pack_call_p (tree call)
3775{
3776 if (TREE_CODE (call) != CALL_EXPR)
3777 return false;
3778 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3779}
3780
3781/* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3782
3783static tree
3784expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3785 tree in_decl)
3786{
3787 tree ohi = CALL_EXPR_ARG (call, 0);
3788 tree hi = tsubst_expr (ohi, args, complain, in_decl);
3789
3790 if (instantiation_dependent_expression_p (hi))
3791 {
3792 if (hi != ohi)
3793 {
3794 call = copy_node (call);
3795 CALL_EXPR_ARG (call, 0) = hi;
3796 }
3797 tree ex = make_pack_expansion (call, complain);
3798 tree vec = make_tree_vec (1);
3799 TREE_VEC_ELT (vec, 0) = ex;
3800 return vec;
3801 }
3802 else
3803 {
3804 hi = instantiate_non_dependent_expr (hi, complain);
3805 hi = cxx_constant_value (t: hi, complain);
3806 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3807
3808 /* Calculate the largest value of len that won't make the size of the vec
3809 overflow an int. The compiler will exceed resource limits long before
3810 this, but it seems a decent place to diagnose. */
3811 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3812
3813 if (len < 0 || len > max)
3814 {
3815 if ((complain & tf_error)
3816 && hi != error_mark_node)
3817 error ("argument to %<__integer_pack%> must be between 0 and %d",
3818 max);
3819 return error_mark_node;
3820 }
3821
3822 tree vec = make_tree_vec (len);
3823
3824 for (int i = 0; i < len; ++i)
3825 TREE_VEC_ELT (vec, i) = size_int (i);
3826
3827 return vec;
3828 }
3829}
3830
3831/* Return a TREE_VEC for the expansion of built-in template parameter pack
3832 CALL. */
3833
3834static tree
3835expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3836 tree in_decl)
3837{
3838 if (!builtin_pack_call_p (call))
3839 return NULL_TREE;
3840
3841 tree fn = CALL_EXPR_FN (call);
3842
3843 if (id_equal (DECL_NAME (fn), str: "__integer_pack"))
3844 return expand_integer_pack (call, args, complain, in_decl);
3845
3846 return NULL_TREE;
3847}
3848
3849/* Return true if the tree T has the extra args mechanism for
3850 avoiding partial instantiation. */
3851
3852static bool
3853has_extra_args_mechanism_p (const_tree t)
3854{
3855 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */
3856 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */
3857 || (TREE_CODE (t) == IF_STMT
3858 && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS */
3859 || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS */
3860}
3861
3862/* Return *_EXTRA_ARGS of the given supported tree T. */
3863
3864static tree&
3865tree_extra_args (tree t)
3866{
3867 gcc_checking_assert (has_extra_args_mechanism_p (t));
3868
3869 if (PACK_EXPANSION_P (t))
3870 return PACK_EXPANSION_EXTRA_ARGS (t);
3871 else if (TREE_CODE (t) == REQUIRES_EXPR)
3872 return REQUIRES_EXPR_EXTRA_ARGS (t);
3873 else if (TREE_CODE (t) == IF_STMT
3874 && IF_STMT_CONSTEXPR_P (t))
3875 return IF_STMT_EXTRA_ARGS (t);
3876 else if (TREE_CODE (t) == LAMBDA_EXPR)
3877 return LAMBDA_EXPR_EXTRA_ARGS (t);
3878
3879 gcc_unreachable ();
3880}
3881
3882/* Structure used to track the progress of find_parameter_packs_r. */
3883struct find_parameter_pack_data
3884{
3885 /* TREE_LIST that will contain all of the parameter packs found by
3886 the traversal. */
3887 tree* parameter_packs;
3888
3889 /* Set of AST nodes that have been visited by the traversal. */
3890 hash_set<tree> *visited;
3891
3892 /* True iff we're making a type pack expansion. */
3893 bool type_pack_expansion_p;
3894
3895 /* True iff we found a subtree that has the extra args mechanism. */
3896 bool found_extra_args_tree_p = false;
3897};
3898
3899/* Identifies all of the argument packs that occur in a template
3900 argument and appends them to the TREE_LIST inside DATA, which is a
3901 find_parameter_pack_data structure. This is a subroutine of
3902 make_pack_expansion and uses_parameter_packs. */
3903static tree
3904find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3905{
3906 tree t = *tp;
3907 struct find_parameter_pack_data* ppd =
3908 (struct find_parameter_pack_data*)data;
3909 bool parameter_pack_p = false;
3910
3911#define WALK_SUBTREE(NODE) \
3912 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3913 ppd, ppd->visited) \
3914
3915 /* Don't look through typedefs; we are interested in whether a
3916 parameter pack is actually written in the expression/type we're
3917 looking at, not the target type. */
3918 if (TYPE_P (t) && typedef_variant_p (type: t))
3919 {
3920 /* But do look at arguments for an alias template. */
3921 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3922 cp_walk_tree (&TI_ARGS (tinfo),
3923 &find_parameter_packs_r,
3924 ppd, ppd->visited);
3925 *walk_subtrees = 0;
3926 return NULL_TREE;
3927 }
3928
3929 /* Identify whether this is a parameter pack or not. */
3930 switch (TREE_CODE (t))
3931 {
3932 case TEMPLATE_PARM_INDEX:
3933 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3934 parameter_pack_p = true;
3935 break;
3936
3937 case TEMPLATE_TYPE_PARM:
3938 t = TYPE_MAIN_VARIANT (t);
3939 /* FALLTHRU */
3940 case TEMPLATE_TEMPLATE_PARM:
3941 /* If the placeholder appears in the decl-specifier-seq of a function
3942 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3943 is a pack expansion, the invented template parameter is a template
3944 parameter pack. */
3945 if (ppd->type_pack_expansion_p && is_auto (t)
3946 && TEMPLATE_TYPE_LEVEL (t) != 0)
3947 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3948 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3949 parameter_pack_p = true;
3950 break;
3951
3952 case FIELD_DECL:
3953 case PARM_DECL:
3954 if (DECL_PACK_P (t))
3955 {
3956 /* We don't want to walk into the type of a PARM_DECL,
3957 because we don't want to see the type parameter pack. */
3958 *walk_subtrees = 0;
3959 parameter_pack_p = true;
3960 }
3961 break;
3962
3963 case VAR_DECL:
3964 if (DECL_PACK_P (t))
3965 {
3966 /* We don't want to walk into the type of a variadic capture proxy,
3967 because we don't want to see the type parameter pack. */
3968 *walk_subtrees = 0;
3969 parameter_pack_p = true;
3970 }
3971 else if (variable_template_specialization_p (t))
3972 {
3973 cp_walk_tree (&DECL_TI_ARGS (t),
3974 find_parameter_packs_r,
3975 ppd, ppd->visited);
3976 *walk_subtrees = 0;
3977 }
3978 break;
3979
3980 case CALL_EXPR:
3981 if (builtin_pack_call_p (call: t))
3982 parameter_pack_p = true;
3983 break;
3984
3985 case BASES:
3986 parameter_pack_p = true;
3987 break;
3988 default:
3989 /* Not a parameter pack. */
3990 break;
3991 }
3992
3993 if (parameter_pack_p)
3994 {
3995 /* Add this parameter pack to the list. */
3996 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3997 }
3998
3999 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
4000 ppd->found_extra_args_tree_p = true;
4001
4002 if (TYPE_P (t))
4003 cp_walk_tree (&TYPE_CONTEXT (t),
4004 &find_parameter_packs_r, ppd, ppd->visited);
4005
4006 /* This switch statement will return immediately if we don't find a
4007 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4008 switch (TREE_CODE (t))
4009 {
4010 case BOUND_TEMPLATE_TEMPLATE_PARM:
4011 /* Check the template itself. */
4012 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
4013 &find_parameter_packs_r, ppd, ppd->visited);
4014 return NULL_TREE;
4015
4016 case DECL_EXPR:
4017 {
4018 tree decl = DECL_EXPR_DECL (t);
4019 /* Ignore the declaration of a capture proxy for a parameter pack. */
4020 if (is_capture_proxy (decl))
4021 *walk_subtrees = 0;
4022 if (is_typedef_decl (x: decl))
4023 /* Since we stop at typedefs above, we need to look through them at
4024 the point of the DECL_EXPR. */
4025 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4026 &find_parameter_packs_r, ppd, ppd->visited);
4027 return NULL_TREE;
4028 }
4029
4030 case TEMPLATE_DECL:
4031 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4032 return NULL_TREE;
4033 cp_walk_tree (&TREE_TYPE (t),
4034 &find_parameter_packs_r, ppd, ppd->visited);
4035 return NULL_TREE;
4036
4037 case TYPE_PACK_EXPANSION:
4038 case EXPR_PACK_EXPANSION:
4039 *walk_subtrees = 0;
4040 return NULL_TREE;
4041
4042 case INTEGER_TYPE:
4043 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4044 ppd, ppd->visited);
4045 *walk_subtrees = 0;
4046 return NULL_TREE;
4047
4048 case IDENTIFIER_NODE:
4049 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4050 ppd->visited);
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4053
4054 case LAMBDA_EXPR:
4055 {
4056 /* Since we defer implicit capture, look in the parms and body. */
4057 tree fn = lambda_function (t);
4058 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4059 ppd->visited);
4060 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4061 ppd->visited);
4062 return NULL_TREE;
4063 }
4064
4065 case DECLTYPE_TYPE:
4066 {
4067 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4068 type_pack_expansion_p to false so that any placeholders
4069 within the expression don't get marked as parameter packs. */
4070 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4071 ppd->type_pack_expansion_p = false;
4072 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4073 ppd, ppd->visited);
4074 ppd->type_pack_expansion_p = type_pack_expansion_p;
4075 *walk_subtrees = 0;
4076 return NULL_TREE;
4077 }
4078
4079 case IF_STMT:
4080 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4081 ppd, ppd->visited);
4082 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4083 ppd, ppd->visited);
4084 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4085 ppd, ppd->visited);
4086 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4087 *walk_subtrees = 0;
4088 return NULL_TREE;
4089
4090 case TAG_DEFN:
4091 t = TREE_TYPE (t);
4092 if (CLASS_TYPE_P (t))
4093 {
4094 /* Local class, need to look through the whole definition.
4095 TYPE_BINFO might be unset for a partial instantiation. */
4096 if (TYPE_BINFO (t))
4097 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4098 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4099 ppd, ppd->visited);
4100 }
4101 else
4102 /* Enum, look at the values. */
4103 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4104 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4105 &find_parameter_packs_r,
4106 ppd, ppd->visited);
4107 return NULL_TREE;
4108
4109 case FUNCTION_TYPE:
4110 case METHOD_TYPE:
4111 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4112 break;
4113
4114 default:
4115 return NULL_TREE;
4116 }
4117
4118#undef WALK_SUBTREE
4119
4120 return NULL_TREE;
4121}
4122
4123/* Determines if the expression or type T uses any parameter packs. */
4124tree
4125uses_parameter_packs (tree t)
4126{
4127 tree parameter_packs = NULL_TREE;
4128 struct find_parameter_pack_data ppd;
4129 ppd.parameter_packs = &parameter_packs;
4130 ppd.visited = new hash_set<tree>;
4131 ppd.type_pack_expansion_p = false;
4132 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4133 delete ppd.visited;
4134 return parameter_packs;
4135}
4136
4137/* Turn ARG, which may be an expression, type, or a TREE_LIST
4138 representation a base-class initializer into a parameter pack
4139 expansion. If all goes well, the resulting node will be an
4140 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4141 respectively. */
4142tree
4143make_pack_expansion (tree arg, tsubst_flags_t complain)
4144{
4145 tree result;
4146 tree parameter_packs = NULL_TREE;
4147 bool for_types = false;
4148 struct find_parameter_pack_data ppd;
4149
4150 if (!arg || arg == error_mark_node)
4151 return arg;
4152
4153 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4154 {
4155 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4156 class initializer. In this case, the TREE_PURPOSE will be a
4157 _TYPE node (representing the base class expansion we're
4158 initializing) and the TREE_VALUE will be a TREE_LIST
4159 containing the initialization arguments.
4160
4161 The resulting expansion looks somewhat different from most
4162 expansions. Rather than returning just one _EXPANSION, we
4163 return a TREE_LIST whose TREE_PURPOSE is a
4164 TYPE_PACK_EXPANSION containing the bases that will be
4165 initialized. The TREE_VALUE will be identical to the
4166 original TREE_VALUE, which is a list of arguments that will
4167 be passed to each base. We do not introduce any new pack
4168 expansion nodes into the TREE_VALUE (although it is possible
4169 that some already exist), because the TREE_PURPOSE and
4170 TREE_VALUE all need to be expanded together with the same
4171 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4172 resulting TREE_PURPOSE will mention the parameter packs in
4173 both the bases and the arguments to the bases. */
4174 tree purpose;
4175 tree value;
4176 tree parameter_packs = NULL_TREE;
4177
4178 /* Determine which parameter packs will be used by the base
4179 class expansion. */
4180 ppd.visited = new hash_set<tree>;
4181 ppd.parameter_packs = &parameter_packs;
4182 ppd.type_pack_expansion_p = false;
4183 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4184 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4185 &ppd, ppd.visited);
4186
4187 if (parameter_packs == NULL_TREE)
4188 {
4189 if (complain & tf_error)
4190 error ("base initializer expansion %qT contains no parameter packs",
4191 arg);
4192 delete ppd.visited;
4193 return error_mark_node;
4194 }
4195
4196 if (TREE_VALUE (arg) != void_type_node)
4197 {
4198 /* Collect the sets of parameter packs used in each of the
4199 initialization arguments. */
4200 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4201 {
4202 /* Determine which parameter packs will be expanded in this
4203 argument. */
4204 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4205 &ppd, ppd.visited);
4206 }
4207 }
4208
4209 delete ppd.visited;
4210
4211 /* Create the pack expansion type for the base type. */
4212 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4213 PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
4214 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4215 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4216
4217 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4218 they will rarely be compared to anything. */
4219 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4220
4221 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4222 }
4223
4224 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4225 for_types = true;
4226
4227 /* Build the PACK_EXPANSION_* node. */
4228 result = for_types
4229 ? cxx_make_type (TYPE_PACK_EXPANSION)
4230 : make_node (EXPR_PACK_EXPANSION);
4231 PACK_EXPANSION_PATTERN (result) = arg;
4232 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4233 {
4234 /* Propagate type and const-expression information. */
4235 TREE_TYPE (result) = TREE_TYPE (arg);
4236 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4237 /* Mark this read now, since the expansion might be length 0. */
4238 mark_exp_read (arg);
4239 }
4240 else
4241 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4242 they will rarely be compared to anything. */
4243 SET_TYPE_STRUCTURAL_EQUALITY (result);
4244
4245 /* Determine which parameter packs will be expanded. */
4246 ppd.parameter_packs = &parameter_packs;
4247 ppd.visited = new hash_set<tree>;
4248 ppd.type_pack_expansion_p = TYPE_P (arg);
4249 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4250 delete ppd.visited;
4251
4252 /* Make sure we found some parameter packs. */
4253 if (parameter_packs == NULL_TREE)
4254 {
4255 if (complain & tf_error)
4256 {
4257 if (TYPE_P (arg))
4258 error ("expansion pattern %qT contains no parameter packs", arg);
4259 else
4260 error ("expansion pattern %qE contains no parameter packs", arg);
4261 }
4262 return error_mark_node;
4263 }
4264 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4265
4266 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4267 if (ppd.found_extra_args_tree_p)
4268 /* If the pattern of this pack expansion contains a subtree that has
4269 the extra args mechanism for avoiding partial instantiation, then
4270 force this pack expansion to also use extra args. Otherwise
4271 partial instantiation of this pack expansion may not lower the
4272 level of some parameter packs within the pattern, which would
4273 confuse tsubst_pack_expansion later (PR101764). */
4274 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
4275
4276 return result;
4277}
4278
4279/* Checks T for any "bare" parameter packs, which have not yet been
4280 expanded, and issues an error if any are found. This operation can
4281 only be done on full expressions or types (e.g., an expression
4282 statement, "if" condition, etc.), because we could have expressions like:
4283
4284 foo(f(g(h(args)))...)
4285
4286 where "args" is a parameter pack. check_for_bare_parameter_packs
4287 should not be called for the subexpressions args, h(args),
4288 g(h(args)), or f(g(h(args))), because we would produce erroneous
4289 error messages.
4290
4291 Returns TRUE and emits an error if there were bare parameter packs,
4292 returns FALSE otherwise. */
4293bool
4294check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4295{
4296 tree parameter_packs = NULL_TREE;
4297 struct find_parameter_pack_data ppd;
4298
4299 if (!processing_template_decl || !t || t == error_mark_node)
4300 return false;
4301
4302 if (TREE_CODE (t) == TYPE_DECL)
4303 t = TREE_TYPE (t);
4304
4305 ppd.parameter_packs = &parameter_packs;
4306 ppd.visited = new hash_set<tree>;
4307 ppd.type_pack_expansion_p = false;
4308 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4309 delete ppd.visited;
4310
4311 if (!parameter_packs)
4312 return false;
4313
4314 if (loc == UNKNOWN_LOCATION)
4315 loc = cp_expr_loc_or_input_loc (t);
4316
4317 /* It's OK for a lambda to have an unexpanded parameter pack from the
4318 containing context, but do complain about unexpanded capture packs. */
4319 tree lam = current_lambda_expr ();
4320 if (lam)
4321 lam = TREE_TYPE (lam);
4322
4323 if (lam && lam != current_class_type)
4324 {
4325 /* We're in a lambda, but it isn't the innermost class.
4326 This should work, but currently doesn't. */
4327 sorry_at (loc, "unexpanded parameter pack in local class in lambda");
4328 return true;
4329 }
4330
4331 if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
4332 for (; parameter_packs;
4333 parameter_packs = TREE_CHAIN (parameter_packs))
4334 {
4335 tree pack = TREE_VALUE (parameter_packs);
4336 if (is_capture_proxy (pack)
4337 || (TREE_CODE (pack) == PARM_DECL
4338 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
4339 break;
4340 }
4341
4342 if (parameter_packs)
4343 {
4344 error_at (loc, "parameter packs not expanded with %<...%>:");
4345 while (parameter_packs)
4346 {
4347 tree pack = TREE_VALUE (parameter_packs);
4348 tree name = NULL_TREE;
4349
4350 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4351 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4352 name = TYPE_NAME (pack);
4353 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4354 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4355 else if (TREE_CODE (pack) == CALL_EXPR)
4356 name = DECL_NAME (CALL_EXPR_FN (pack));
4357 else
4358 name = DECL_NAME (pack);
4359
4360 if (name)
4361 inform (loc, " %qD", name);
4362 else
4363 inform (loc, " %s", "<anonymous>");
4364
4365 parameter_packs = TREE_CHAIN (parameter_packs);
4366 }
4367
4368 return true;
4369 }
4370
4371 return false;
4372}
4373
4374/* Expand any parameter packs that occur in the template arguments in
4375 ARGS. */
4376tree
4377expand_template_argument_pack (tree args)
4378{
4379 if (args == error_mark_node)
4380 return error_mark_node;
4381
4382 tree result_args = NULL_TREE;
4383 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4384 int num_result_args = -1;
4385 int non_default_args_count = -1;
4386
4387 /* First, determine if we need to expand anything, and the number of
4388 slots we'll need. */
4389 for (in_arg = 0; in_arg < nargs; ++in_arg)
4390 {
4391 tree arg = TREE_VEC_ELT (args, in_arg);
4392 if (arg == NULL_TREE)
4393 return args;
4394 if (ARGUMENT_PACK_P (arg))
4395 {
4396 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4397 if (num_result_args < 0)
4398 num_result_args = in_arg + num_packed;
4399 else
4400 num_result_args += num_packed;
4401 }
4402 else
4403 {
4404 if (num_result_args >= 0)
4405 num_result_args++;
4406 }
4407 }
4408
4409 /* If no expansion is necessary, we're done. */
4410 if (num_result_args < 0)
4411 return args;
4412
4413 /* Expand arguments. */
4414 result_args = make_tree_vec (num_result_args);
4415 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4416 non_default_args_count =
4417 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4418 for (in_arg = 0; in_arg < nargs; ++in_arg)
4419 {
4420 tree arg = TREE_VEC_ELT (args, in_arg);
4421 if (ARGUMENT_PACK_P (arg))
4422 {
4423 tree packed = ARGUMENT_PACK_ARGS (arg);
4424 int i, num_packed = TREE_VEC_LENGTH (packed);
4425 for (i = 0; i < num_packed; ++i, ++out_arg)
4426 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4427 if (non_default_args_count > 0)
4428 non_default_args_count += num_packed - 1;
4429 }
4430 else
4431 {
4432 TREE_VEC_ELT (result_args, out_arg) = arg;
4433 ++out_arg;
4434 }
4435 }
4436 if (non_default_args_count >= 0)
4437 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4438 return result_args;
4439}
4440
4441/* Checks if DECL shadows a template parameter.
4442
4443 [temp.local]: A template-parameter shall not be redeclared within its
4444 scope (including nested scopes).
4445
4446 Emits an error and returns TRUE if the DECL shadows a parameter,
4447 returns FALSE otherwise. */
4448
4449bool
4450check_template_shadow (tree decl)
4451{
4452 tree olddecl;
4453
4454 /* If we're not in a template, we can't possibly shadow a template
4455 parameter. */
4456 if (!current_template_parms)
4457 return true;
4458
4459 /* Figure out what we're shadowing. */
4460 decl = OVL_FIRST (decl);
4461 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4462
4463 /* If there's no previous binding for this name, we're not shadowing
4464 anything, let alone a template parameter. */
4465 if (!olddecl)
4466 return true;
4467
4468 /* If we're not shadowing a template parameter, we're done. Note
4469 that OLDDECL might be an OVERLOAD (or perhaps even an
4470 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4471 node. */
4472 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4473 return true;
4474
4475 /* We check for decl != olddecl to avoid bogus errors for using a
4476 name inside a class. We check TPFI to avoid duplicate errors for
4477 inline member templates. */
4478 if (decl == olddecl
4479 || (DECL_TEMPLATE_PARM_P (decl)
4480 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4481 return true;
4482
4483 /* Don't complain about the injected class name, as we've already
4484 complained about the class itself. */
4485 if (DECL_SELF_REFERENCE_P (decl))
4486 return false;
4487
4488 if (DECL_TEMPLATE_PARM_P (decl))
4489 error ("declaration of template parameter %q+D shadows "
4490 "template parameter", decl);
4491 else
4492 error ("declaration of %q+#D shadows template parameter", decl);
4493 inform (DECL_SOURCE_LOCATION (olddecl),
4494 "template parameter %qD declared here", olddecl);
4495 return false;
4496}
4497
4498/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4499 ORIG_LEVEL, DECL, and TYPE. */
4500
4501static tree
4502build_template_parm_index (int index,
4503 int level,
4504 int orig_level,
4505 tree decl,
4506 tree type)
4507{
4508 tree t = make_node (TEMPLATE_PARM_INDEX);
4509 TEMPLATE_PARM_IDX (t) = index;
4510 TEMPLATE_PARM_LEVEL (t) = level;
4511 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4512 TEMPLATE_PARM_DECL (t) = decl;
4513 TREE_TYPE (t) = type;
4514 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4515 TREE_READONLY (t) = TREE_READONLY (decl);
4516
4517 return t;
4518}
4519
4520struct ctp_hasher : ggc_ptr_hash<tree_node>
4521{
4522 static hashval_t hash (tree t)
4523 {
4524 ++comparing_specializations;
4525 tree_code code = TREE_CODE (t);
4526 hashval_t val = iterative_hash_object (code, 0);
4527 val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
4528 val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
4529 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
4530 val = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
4531 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
4532 val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
4533 --comparing_specializations;
4534 return val;
4535 }
4536
4537 static bool equal (tree t, tree u)
4538 {
4539 ++comparing_specializations;
4540 bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
4541 --comparing_specializations;
4542 return eq;
4543 }
4544};
4545
4546static GTY (()) hash_table<ctp_hasher> *ctp_table;
4547
4548/* Find the canonical type parameter for the given template type
4549 parameter. Returns the canonical type parameter, which may be TYPE
4550 if no such parameter existed. */
4551
4552tree
4553canonical_type_parameter (tree type)
4554{
4555 if (ctp_table == NULL)
4556 ctp_table = hash_table<ctp_hasher>::create_ggc (n: 61);
4557
4558 tree& slot = *ctp_table->find_slot (value: type, insert: INSERT);
4559 if (slot == NULL_TREE)
4560 slot = type;
4561 return slot;
4562}
4563
4564/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4565 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4566 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4567 new one is created. */
4568
4569static tree
4570reduce_template_parm_level (tree index, tree type, int levels, tree args,
4571 tsubst_flags_t complain)
4572{
4573 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4574 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4575 != TEMPLATE_PARM_LEVEL (index) - levels)
4576 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4577 {
4578 tree orig_decl = TEMPLATE_PARM_DECL (index);
4579
4580 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4581 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4582 type);
4583 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4584 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4585 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4586 DECL_ARTIFICIAL (decl) = 1;
4587 SET_DECL_TEMPLATE_PARM_P (decl);
4588
4589 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4590 TEMPLATE_PARM_LEVEL (index) - levels,
4591 TEMPLATE_PARM_ORIG_LEVEL (index),
4592 decl, type);
4593 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4594 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4595 = TEMPLATE_PARM_PARAMETER_PACK (index);
4596
4597 /* Template template parameters need this. */
4598 tree inner = decl;
4599 if (TREE_CODE (decl) == TEMPLATE_DECL)
4600 {
4601 inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
4602 TYPE_DECL, DECL_NAME (decl), type);
4603 DECL_TEMPLATE_RESULT (decl) = inner;
4604 DECL_ARTIFICIAL (inner) = true;
4605 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
4606 args, complain);
4607 DECL_TEMPLATE_PARMS (decl) = parms;
4608 tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
4609 DECL_TEMPLATE_INFO (inner)
4610 = build_template_info (DECL_TI_TEMPLATE (orig_inner),
4611 template_args: template_parms_to_args (parms));
4612 }
4613
4614 /* Attach the TPI to the decl. */
4615 if (TREE_CODE (inner) == TYPE_DECL)
4616 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4617 else
4618 DECL_INITIAL (decl) = tpi;
4619 }
4620
4621 return TEMPLATE_PARM_DESCENDANTS (index);
4622}
4623
4624/* Process information from new template parameter PARM and append it
4625 to the LIST being built. This new parameter is a non-type
4626 parameter iff IS_NON_TYPE is true. This new parameter is a
4627 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4628 is in PARM_LOC. */
4629
4630tree
4631process_template_parm (tree list, location_t parm_loc, tree parm,
4632 bool is_non_type, bool is_parameter_pack)
4633{
4634 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4635 tree prev = NULL_TREE;
4636 int idx = 0;
4637
4638 if (list)
4639 {
4640 prev = tree_last (list);
4641
4642 tree p = TREE_VALUE (prev);
4643 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4644 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4645 else if (TREE_CODE (p) == PARM_DECL)
4646 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4647
4648 ++idx;
4649 }
4650
4651 tree decl = NULL_TREE;
4652 tree defval = TREE_PURPOSE (parm);
4653 tree constr = TREE_TYPE (parm);
4654
4655 if (is_non_type)
4656 {
4657 parm = TREE_VALUE (parm);
4658
4659 SET_DECL_TEMPLATE_PARM_P (parm);
4660
4661 if (TREE_TYPE (parm) != error_mark_node)
4662 {
4663 /* [temp.param]
4664
4665 The top-level cv-qualifiers on the template-parameter are
4666 ignored when determining its type. */
4667 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4668 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4669 TREE_TYPE (parm) = error_mark_node;
4670 else if (uses_parameter_packs (TREE_TYPE (parm))
4671 && !is_parameter_pack
4672 /* If we're in a nested template parameter list, the template
4673 template parameter could be a parameter pack. */
4674 && processing_template_parmlist == 1)
4675 {
4676 /* This template parameter is not a parameter pack, but it
4677 should be. Complain about "bare" parameter packs. */
4678 check_for_bare_parameter_packs (TREE_TYPE (parm));
4679
4680 /* Recover by calling this a parameter pack. */
4681 is_parameter_pack = true;
4682 }
4683 }
4684
4685 /* A template parameter is not modifiable. */
4686 TREE_CONSTANT (parm) = 1;
4687 TREE_READONLY (parm) = 1;
4688 decl = build_decl (parm_loc,
4689 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4690 TREE_CONSTANT (decl) = 1;
4691 TREE_READONLY (decl) = 1;
4692 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4693 = build_template_parm_index (index: idx, current_template_depth,
4694 current_template_depth,
4695 decl, TREE_TYPE (parm));
4696
4697 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4698 = is_parameter_pack;
4699 }
4700 else
4701 {
4702 tree t;
4703 parm = TREE_VALUE (TREE_VALUE (parm));
4704
4705 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4706 {
4707 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4708 /* This is for distinguishing between real templates and template
4709 template parameters */
4710 TREE_TYPE (parm) = t;
4711
4712 /* any_template_parm_r expects to be able to get the targs of a
4713 DECL_TEMPLATE_RESULT. */
4714 tree result = DECL_TEMPLATE_RESULT (parm);
4715 TREE_TYPE (result) = t;
4716 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4717 tree tinfo = build_template_info (template_decl: parm, template_args: args);
4718 retrofit_lang_decl (result);
4719 DECL_TEMPLATE_INFO (result) = tinfo;
4720
4721 decl = parm;
4722 }
4723 else
4724 {
4725 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4726 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4727 decl = build_decl (parm_loc,
4728 TYPE_DECL, parm, t);
4729 }
4730
4731 TYPE_NAME (t) = decl;
4732 TYPE_STUB_DECL (t) = decl;
4733 parm = decl;
4734 TEMPLATE_TYPE_PARM_INDEX (t)
4735 = build_template_parm_index (index: idx, current_template_depth,
4736 current_template_depth,
4737 decl, TREE_TYPE (parm));
4738 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4739 TYPE_CANONICAL (t) = canonical_type_parameter (type: t);
4740 }
4741 DECL_ARTIFICIAL (decl) = 1;
4742 SET_DECL_TEMPLATE_PARM_P (decl);
4743
4744 if (TREE_CODE (parm) == TEMPLATE_DECL
4745 && !uses_outer_template_parms (parm))
4746 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
4747
4748 /* Build requirements for the type/template parameter.
4749 This must be done after SET_DECL_TEMPLATE_PARM_P or
4750 process_template_parm could fail. */
4751 tree reqs = finish_shorthand_constraint (parm, constr);
4752
4753 decl = pushdecl (decl);
4754 if (!is_non_type)
4755 parm = decl;
4756
4757 /* Build the parameter node linking the parameter declaration,
4758 its default argument (if any), and its constraints (if any). */
4759 parm = build_tree_list (defval, parm);
4760 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4761
4762 if (prev)
4763 TREE_CHAIN (prev) = parm;
4764 else
4765 list = parm;
4766
4767 return list;
4768}
4769
4770/* The end of a template parameter list has been reached. Process the
4771 tree list into a parameter vector, converting each parameter into a more
4772 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4773 as PARM_DECLs. */
4774
4775tree
4776end_template_parm_list (tree parms)
4777{
4778 tree saved_parmlist = make_tree_vec (list_length (parms));
4779
4780 /* Pop the dummy parameter level and add the real one. We do not
4781 morph the dummy parameter in place, as it might have been
4782 captured by a (nested) template-template-parm. */
4783 current_template_parms = TREE_CHAIN (current_template_parms);
4784
4785 current_template_parms
4786 = tree_cons (size_int (current_template_depth + 1),
4787 saved_parmlist, current_template_parms);
4788
4789 for (unsigned ix = 0; parms; ix++)
4790 {
4791 tree parm = parms;
4792 parms = TREE_CHAIN (parms);
4793 TREE_CHAIN (parm) = NULL_TREE;
4794
4795 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4796 }
4797
4798 --processing_template_parmlist;
4799
4800 return saved_parmlist;
4801}
4802
4803// Explicitly indicate the end of the template parameter list. We assume
4804// that the current template parameters have been constructed and/or
4805// managed explicitly, as when creating new template template parameters
4806// from a shorthand constraint.
4807void
4808end_template_parm_list ()
4809{
4810 --processing_template_parmlist;
4811}
4812
4813/* end_template_decl is called after a template declaration is seen. */
4814
4815void
4816end_template_decl (void)
4817{
4818 reset_specialization ();
4819
4820 if (! processing_template_decl)
4821 return;
4822
4823 /* This matches the pushlevel in begin_template_parm_list. */
4824 finish_scope ();
4825
4826 --processing_template_decl;
4827 current_template_parms = TREE_CHAIN (current_template_parms);
4828}
4829
4830/* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4831 thereof, and converts it into an argument suitable to be passed to
4832 the type substitution functions. Note that if the TREE_LIST contains
4833 an error_mark node, the returned argument is error_mark_node. */
4834
4835tree
4836template_parm_to_arg (tree t)
4837{
4838 if (!t)
4839 return NULL_TREE;
4840
4841 if (TREE_CODE (t) == TREE_LIST)
4842 t = TREE_VALUE (t);
4843
4844 if (error_operand_p (t))
4845 return error_mark_node;
4846
4847 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4848 {
4849 if (TREE_CODE (t) == TYPE_DECL
4850 || TREE_CODE (t) == TEMPLATE_DECL)
4851 t = TREE_TYPE (t);
4852 else
4853 t = DECL_INITIAL (t);
4854 }
4855
4856 gcc_assert (TEMPLATE_PARM_P (t));
4857
4858 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4859 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4860 {
4861 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4862 {
4863 /* Turn this argument into a TYPE_ARGUMENT_PACK
4864 with a single element, which expands T. */
4865 tree vec = make_tree_vec (1);
4866 if (CHECKING_P)
4867 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4868
4869 TREE_VEC_ELT (vec, 0) = make_pack_expansion (arg: t);
4870
4871 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4872 ARGUMENT_PACK_ARGS (t) = vec;
4873 }
4874 }
4875 else
4876 {
4877 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4878 {
4879 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4880 with a single element, which expands T. */
4881 tree vec = make_tree_vec (1);
4882 if (CHECKING_P)
4883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4884
4885 t = convert_from_reference (t);
4886 TREE_VEC_ELT (vec, 0) = make_pack_expansion (arg: t);
4887
4888 t = make_node (NONTYPE_ARGUMENT_PACK);
4889 ARGUMENT_PACK_ARGS (t) = vec;
4890 }
4891 else
4892 t = convert_from_reference (t);
4893 }
4894 return t;
4895}
4896
4897/* If T looks like a generic template argument produced by template_parm_to_arg,
4898 return the corresponding template parameter, otherwise return NULL_TREE. */
4899
4900static tree
4901template_arg_to_parm (tree t)
4902{
4903 if (t == NULL_TREE)
4904 return NULL_TREE;
4905
4906 if (ARGUMENT_PACK_P (t))
4907 {
4908 tree args = ARGUMENT_PACK_ARGS (t);
4909 if (TREE_VEC_LENGTH (args) == 1
4910 && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
4911 t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
4912 }
4913
4914 if (REFERENCE_REF_P (t))
4915 t = TREE_OPERAND (t, 0);
4916
4917 if (TEMPLATE_PARM_P (t))
4918 return t;
4919 else
4920 return NULL_TREE;
4921}
4922
4923/* Given a single level of template parameters (a TREE_VEC), return it
4924 as a set of template arguments. */
4925
4926tree
4927template_parms_level_to_args (tree parms)
4928{
4929 parms = copy_node (parms);
4930 TREE_TYPE (parms) = NULL_TREE;
4931 for (tree& parm : tree_vec_range (parms))
4932 parm = template_parm_to_arg (t: parm);
4933
4934 if (CHECKING_P)
4935 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
4936
4937 return parms;
4938}
4939
4940/* Given a set of template parameters, return them as a set of template
4941 arguments. The template parameters are represented as a TREE_VEC, in
4942 the form documented in cp-tree.h for template arguments. */
4943
4944tree
4945template_parms_to_args (tree parms)
4946{
4947 tree header;
4948 tree args = NULL_TREE;
4949 int length = TMPL_PARMS_DEPTH (parms);
4950 int l = length;
4951
4952 /* If there is only one level of template parameters, we do not
4953 create a TREE_VEC of TREE_VECs. Instead, we return a single
4954 TREE_VEC containing the arguments. */
4955 if (length > 1)
4956 args = make_tree_vec (length);
4957
4958 for (header = parms; header; header = TREE_CHAIN (header))
4959 {
4960 tree a = template_parms_level_to_args (TREE_VALUE (header));
4961
4962 if (length > 1)
4963 TREE_VEC_ELT (args, --l) = a;
4964 else
4965 args = a;
4966 }
4967
4968 return args;
4969}
4970
4971/* Within the declaration of a template, return the currently active
4972 template parameters as an argument TREE_VEC. */
4973
4974static tree
4975current_template_args (void)
4976{
4977 return template_parms_to_args (current_template_parms);
4978}
4979
4980/* Return the fully generic arguments for of TMPL, i.e. what
4981 current_template_args would be while parsing it. */
4982
4983tree
4984generic_targs_for (tree tmpl)
4985{
4986 if (tmpl == NULL_TREE)
4987 return NULL_TREE;
4988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4989 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4990 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4991 template parameter, it has no TEMPLATE_INFO; for a partial
4992 specialization, it has the arguments for the primary template, and we
4993 want the arguments for the partial specialization. */;
4994 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4995 if (tree ti = get_template_info (t: result))
4996 return TI_ARGS (ti);
4997 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4998}
4999
5000/* Return the template arguments corresponding to the template parameters of
5001 DECL's enclosing scope. When DECL is a member of a partial specialization,
5002 this returns the arguments for the partial specialization as opposed to those
5003 for the primary template, which is the main difference between this function
5004 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */
5005
5006tree
5007outer_template_args (const_tree decl)
5008{
5009 if (TREE_CODE (decl) == TEMPLATE_DECL)
5010 decl = DECL_TEMPLATE_RESULT (decl);
5011 tree ti = get_template_info (t: decl);
5012 if (!ti)
5013 return NULL_TREE;
5014 tree args = TI_ARGS (ti);
5015 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
5016 return args;
5017 if (TMPL_ARGS_DEPTH (args) == 1)
5018 return NULL_TREE;
5019 return strip_innermost_template_args (args, extra_levels: 1);
5020}
5021
5022/* Update the declared TYPE by doing any lookups which were thought to be
5023 dependent, but are not now that we know the SCOPE of the declarator. */
5024
5025tree
5026maybe_update_decl_type (tree orig_type, tree scope)
5027{
5028 tree type = orig_type;
5029
5030 if (type == NULL_TREE)
5031 return type;
5032
5033 if (TREE_CODE (orig_type) == TYPE_DECL)
5034 type = TREE_TYPE (type);
5035
5036 if (scope && TYPE_P (scope) && dependent_type_p (scope)
5037 && dependent_type_p (type)
5038 /* Don't bother building up the args in this case. */
5039 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
5040 {
5041 /* tsubst in the args corresponding to the template parameters,
5042 including auto if present. Most things will be unchanged, but
5043 make_typename_type and tsubst_qualified_id will resolve
5044 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5045 tree args = current_template_args ();
5046 tree auto_node = type_uses_auto (type);
5047 tree pushed;
5048 if (auto_node)
5049 {
5050 tree auto_vec = make_tree_vec (1);
5051 TREE_VEC_ELT (auto_vec, 0) = auto_node;
5052 args = add_to_template_args (args, extra_args: auto_vec);
5053 }
5054 pushed = push_scope (scope);
5055 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
5056 if (pushed)
5057 pop_scope (scope);
5058 }
5059
5060 if (type == error_mark_node)
5061 return orig_type;
5062
5063 if (TREE_CODE (orig_type) == TYPE_DECL)
5064 {
5065 if (same_type_p (type, TREE_TYPE (orig_type)))
5066 type = orig_type;
5067 else
5068 type = TYPE_NAME (type);
5069 }
5070 return type;
5071}
5072
5073/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5074 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5075 the new template is a member template. */
5076
5077static tree
5078build_template_decl (tree decl, tree parms, bool member_template_p)
5079{
5080 gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
5081
5082 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
5083 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
5084 DECL_TEMPLATE_PARMS (tmpl) = parms;
5085 DECL_TEMPLATE_RESULT (tmpl) = decl;
5086 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
5087 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5088 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
5089 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
5090
5091 /* Propagate module information from the decl. */
5092 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
5093
5094 return tmpl;
5095}
5096
5097struct template_parm_data
5098{
5099 /* The level of the template parameters we are currently
5100 processing. */
5101 int level;
5102
5103 /* The index of the specialization argument we are currently
5104 processing. */
5105 int current_arg;
5106
5107 /* An array whose size is the number of template parameters. The
5108 elements are nonzero if the parameter has been used in any one
5109 of the arguments processed so far. */
5110 int* parms;
5111
5112 /* An array whose size is the number of template arguments. The
5113 elements are nonzero if the argument makes use of template
5114 parameters of this level. */
5115 int* arg_uses_template_parms;
5116};
5117
5118/* Subroutine of push_template_decl used to see if each template
5119 parameter in a partial specialization is used in the explicit
5120 argument list. If T is of the LEVEL given in DATA (which is
5121 treated as a template_parm_data*), then DATA->PARMS is marked
5122 appropriately. */
5123
5124static int
5125mark_template_parm (tree t, void* data)
5126{
5127 int level;
5128 int idx;
5129 struct template_parm_data* tpd = (struct template_parm_data*) data;
5130
5131 template_parm_level_and_index (t, &level, &idx);
5132
5133 if (level == tpd->level)
5134 {
5135 tpd->parms[idx] = 1;
5136 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5137 }
5138
5139 /* In C++17 the type of a non-type argument is a deduced context. */
5140 if (cxx_dialect >= cxx17
5141 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5142 for_each_template_parm (TREE_TYPE (t),
5143 &mark_template_parm,
5144 data,
5145 NULL,
5146 /*include_nondeduced_p=*/false);
5147
5148 /* Return zero so that for_each_template_parm will continue the
5149 traversal of the tree; we want to mark *every* template parm. */
5150 return 0;
5151}
5152
5153/* Process the partial specialization DECL. */
5154
5155static tree
5156process_partial_specialization (tree decl)
5157{
5158 tree type = TREE_TYPE (decl);
5159 tree tinfo = get_template_info (t: decl);
5160 tree maintmpl = TI_TEMPLATE (tinfo);
5161 tree specargs = TI_ARGS (tinfo);
5162 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5163 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5164 tree inner_parms;
5165 tree inst;
5166 int nargs = TREE_VEC_LENGTH (inner_args);
5167 int ntparms;
5168 int i;
5169 bool did_error_intro = false;
5170 struct template_parm_data tpd;
5171 struct template_parm_data tpd2;
5172
5173 gcc_assert (current_template_parms);
5174
5175 /* A concept cannot be specialized. */
5176 if (flag_concepts && variable_concept_p (t: maintmpl))
5177 {
5178 error ("specialization of variable concept %q#D", maintmpl);
5179 return error_mark_node;
5180 }
5181
5182 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5183 ntparms = TREE_VEC_LENGTH (inner_parms);
5184
5185 /* We check that each of the template parameters given in the
5186 partial specialization is used in the argument list to the
5187 specialization. For example:
5188
5189 template <class T> struct S;
5190 template <class T> struct S<T*>;
5191
5192 The second declaration is OK because `T*' uses the template
5193 parameter T, whereas
5194
5195 template <class T> struct S<int>;
5196
5197 is no good. Even trickier is:
5198
5199 template <class T>
5200 struct S1
5201 {
5202 template <class U>
5203 struct S2;
5204 template <class U>
5205 struct S2<T>;
5206 };
5207
5208 The S2<T> declaration is actually invalid; it is a
5209 full-specialization. Of course,
5210
5211 template <class U>
5212 struct S2<T (*)(U)>;
5213
5214 or some such would have been OK. */
5215 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5216 tpd.parms = XALLOCAVEC (int, ntparms);
5217 memset (s: tpd.parms, c: 0, n: sizeof (int) * ntparms);
5218
5219 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5220 memset (s: tpd.arg_uses_template_parms, c: 0, n: sizeof (int) * nargs);
5221 for (i = 0; i < nargs; ++i)
5222 {
5223 tpd.current_arg = i;
5224 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5225 &mark_template_parm,
5226 &tpd,
5227 NULL,
5228 /*include_nondeduced_p=*/false);
5229 }
5230 for (i = 0; i < ntparms; ++i)
5231 if (tpd.parms[i] == 0)
5232 {
5233 /* One of the template parms was not used in a deduced context in the
5234 specialization. */
5235 if (!did_error_intro)
5236 {
5237 error ("template parameters not deducible in "
5238 "partial specialization:");
5239 did_error_intro = true;
5240 }
5241
5242 inform (input_location, " %qD",
5243 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5244 }
5245
5246 if (did_error_intro)
5247 return error_mark_node;
5248
5249 /* [temp.class.spec]
5250
5251 The argument list of the specialization shall not be identical to
5252 the implicit argument list of the primary template. */
5253 tree main_args
5254 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5255 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5256 && (!flag_concepts
5257 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5258 {
5259 if (!flag_concepts)
5260 error ("partial specialization %q+D does not specialize "
5261 "any template arguments; to define the primary template, "
5262 "remove the template argument list", decl);
5263 else
5264 error ("partial specialization %q+D does not specialize any "
5265 "template arguments and is not more constrained than "
5266 "the primary template; to define the primary template, "
5267 "remove the template argument list", decl);
5268 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5269 }
5270
5271 /* A partial specialization that replaces multiple parameters of the
5272 primary template with a pack expansion is less specialized for those
5273 parameters. */
5274 if (nargs < DECL_NTPARMS (maintmpl))
5275 {
5276 error ("partial specialization is not more specialized than the "
5277 "primary template because it replaces multiple parameters "
5278 "with a pack expansion");
5279 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5280 /* Avoid crash in process_partial_specialization. */
5281 return decl;
5282 }
5283
5284 else if (nargs > DECL_NTPARMS (maintmpl))
5285 {
5286 error ("too many arguments for partial specialization %qT", type);
5287 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5288 /* Avoid crash below. */
5289 return decl;
5290 }
5291
5292 /* If we aren't in a dependent class, we can actually try deduction. */
5293 else if (tpd.level == 1
5294 /* FIXME we should be able to handle a partial specialization of a
5295 partial instantiation, but currently we can't (c++/41727). */
5296 && TMPL_ARGS_DEPTH (specargs) == 1
5297 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5298 {
5299 auto_diagnostic_group d;
5300 if (pedwarn (input_location, 0,
5301 "partial specialization %qD is not more specialized than",
5302 decl))
5303 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5304 maintmpl);
5305 }
5306
5307 /* [temp.spec.partial]
5308
5309 The type of a template parameter corresponding to a specialized
5310 non-type argument shall not be dependent on a parameter of the
5311 specialization.
5312
5313 Also, we verify that pack expansions only occur at the
5314 end of the argument list. */
5315 tpd2.parms = 0;
5316 for (i = 0; i < nargs; ++i)
5317 {
5318 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5319 tree arg = TREE_VEC_ELT (inner_args, i);
5320 tree packed_args = NULL_TREE;
5321 int j, len = 1;
5322
5323 if (ARGUMENT_PACK_P (arg))
5324 {
5325 /* Extract the arguments from the argument pack. We'll be
5326 iterating over these in the following loop. */
5327 packed_args = ARGUMENT_PACK_ARGS (arg);
5328 len = TREE_VEC_LENGTH (packed_args);
5329 }
5330
5331 for (j = 0; j < len; j++)
5332 {
5333 if (packed_args)
5334 /* Get the Jth argument in the parameter pack. */
5335 arg = TREE_VEC_ELT (packed_args, j);
5336
5337 if (PACK_EXPANSION_P (arg))
5338 {
5339 /* Pack expansions must come at the end of the
5340 argument list. */
5341 if ((packed_args && j < len - 1)
5342 || (!packed_args && i < nargs - 1))
5343 {
5344 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5345 error ("parameter pack argument %qE must be at the "
5346 "end of the template argument list", arg);
5347 else
5348 error ("parameter pack argument %qT must be at the "
5349 "end of the template argument list", arg);
5350 }
5351 }
5352
5353 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5354 /* We only care about the pattern. */
5355 arg = PACK_EXPANSION_PATTERN (arg);
5356
5357 if (/* These first two lines are the `non-type' bit. */
5358 !TYPE_P (arg)
5359 && TREE_CODE (arg) != TEMPLATE_DECL
5360 /* This next two lines are the `argument expression is not just a
5361 simple identifier' condition and also the `specialized
5362 non-type argument' bit. */
5363 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5364 && !((REFERENCE_REF_P (arg)
5365 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5366 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5367 {
5368 /* Look at the corresponding template parameter,
5369 marking which template parameters its type depends
5370 upon. */
5371 tree type = TREE_TYPE (parm);
5372
5373 if (!tpd2.parms)
5374 {
5375 /* We haven't yet initialized TPD2. Do so now. */
5376 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5377 /* The number of parameters here is the number in the
5378 main template, which, as checked in the assertion
5379 above, is NARGS. */
5380 tpd2.parms = XALLOCAVEC (int, nargs);
5381 tpd2.level =
5382 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5383 }
5384
5385 /* Mark the template parameters. But this time, we're
5386 looking for the template parameters of the main
5387 template, not in the specialization. */
5388 tpd2.current_arg = i;
5389 tpd2.arg_uses_template_parms[i] = 0;
5390 memset (s: tpd2.parms, c: 0, n: sizeof (int) * nargs);
5391 for_each_template_parm (type,
5392 &mark_template_parm,
5393 &tpd2,
5394 NULL,
5395 /*include_nondeduced_p=*/false);
5396
5397 if (tpd2.arg_uses_template_parms [i])
5398 {
5399 /* The type depended on some template parameters.
5400 If they are fully specialized in the
5401 specialization, that's OK. */
5402 int j;
5403 int count = 0;
5404 for (j = 0; j < nargs; ++j)
5405 if (tpd2.parms[j] != 0
5406 && tpd.arg_uses_template_parms [j])
5407 ++count;
5408 if (count != 0)
5409 error_n (input_location, count,
5410 "type %qT of template argument %qE depends "
5411 "on a template parameter",
5412 "type %qT of template argument %qE depends "
5413 "on template parameters",
5414 type,
5415 arg);
5416 }
5417 }
5418 }
5419 }
5420
5421 /* We should only get here once. */
5422 if (TREE_CODE (decl) == TYPE_DECL)
5423 gcc_assert (!COMPLETE_TYPE_P (type));
5424
5425 // Build the template decl.
5426 tree tmpl = build_template_decl (decl, current_template_parms,
5427 DECL_MEMBER_TEMPLATE_P (maintmpl));
5428 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5429 DECL_TEMPLATE_INFO (tmpl) = build_template_info (template_decl: maintmpl, template_args: specargs);
5430 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5431
5432 /* Give template template parms a DECL_CONTEXT of the template
5433 for which they are a parameter. */
5434 for (i = 0; i < ntparms; ++i)
5435 {
5436 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5437 if (TREE_CODE (parm) == TEMPLATE_DECL)
5438 DECL_CONTEXT (parm) = tmpl;
5439 }
5440
5441 if (VAR_P (decl))
5442 {
5443 /* We didn't register this in check_explicit_specialization so we could
5444 wait until the constraints were set. */
5445 tree reg = register_specialization (spec: decl, tmpl: maintmpl, args: specargs, is_friend: false, hash: 0);
5446 if (reg != decl)
5447 /* Redeclaration. */
5448 return reg;
5449 }
5450 else
5451 associate_classtype_constraints (type);
5452
5453 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5454 = tree_cons (specargs, tmpl,
5455 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5456 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5457 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */
5458 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
5459 TI_PARTIAL_INFO (tinfo) = build_template_info (template_decl: tmpl, NULL_TREE);
5460
5461 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5462 inst = TREE_CHAIN (inst))
5463 {
5464 tree instance = TREE_VALUE (inst);
5465 if (TYPE_P (instance)
5466 ? (COMPLETE_TYPE_P (instance)
5467 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5468 : DECL_TEMPLATE_INSTANTIATION (instance))
5469 {
5470 tree partial_ti = most_specialized_partial_spec (instance, tf_none,
5471 /*rechecking=*/true);
5472 tree inst_decl = (DECL_P (instance)
5473 ? instance : TYPE_NAME (instance));
5474 if (!partial_ti)
5475 /* OK */;
5476 else if (partial_ti == error_mark_node)
5477 permerror (input_location,
5478 "declaration of %qD ambiguates earlier template "
5479 "instantiation for %qD", decl, inst_decl);
5480 else if (TI_TEMPLATE (partial_ti) == tmpl)
5481 permerror (input_location,
5482 "partial specialization of %qD after instantiation "
5483 "of %qD", decl, inst_decl);
5484 }
5485 }
5486
5487 return decl;
5488}
5489
5490/* PARM is a template parameter of some form; return the corresponding
5491 TEMPLATE_PARM_INDEX. */
5492
5493static tree
5494get_template_parm_index (tree parm)
5495{
5496 if (TREE_CODE (parm) == PARM_DECL
5497 || TREE_CODE (parm) == CONST_DECL)
5498 parm = DECL_INITIAL (parm);
5499 else if (TREE_CODE (parm) == TYPE_DECL
5500 || TREE_CODE (parm) == TEMPLATE_DECL)
5501 parm = TREE_TYPE (parm);
5502 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5503 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5504 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5505 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5506 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5507 return parm;
5508}
5509
5510/* Subroutine of fixed_parameter_pack_p below. Look for any template
5511 parameter packs used by the template parameter PARM. */
5512
5513static void
5514fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5515{
5516 /* A type parm can't refer to another parm. */
5517 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5518 return;
5519 else if (TREE_CODE (parm) == PARM_DECL)
5520 {
5521 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5522 ppd, ppd->visited);
5523 return;
5524 }
5525
5526 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5527
5528 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5529 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5530 {
5531 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5532 if (template_parameter_pack_p (parm: p))
5533 /* Any packs in the type are expanded by this parameter. */;
5534 else
5535 fixed_parameter_pack_p_1 (parm: p, ppd);
5536 }
5537}
5538
5539/* PARM is a template parameter pack. Return any parameter packs used in
5540 its type or the type of any of its template parameters. If there are
5541 any such packs, it will be instantiated into a fixed template parameter
5542 list by partial instantiation rather than be fully deduced. */
5543
5544tree
5545fixed_parameter_pack_p (tree parm)
5546{
5547 /* This can only be true in a member template. */
5548 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5549 return NULL_TREE;
5550 /* This can only be true for a parameter pack. */
5551 if (!template_parameter_pack_p (parm))
5552 return NULL_TREE;
5553 /* A type parm can't refer to another parm. */
5554 if (TREE_CODE (parm) == TYPE_DECL)
5555 return NULL_TREE;
5556
5557 tree parameter_packs = NULL_TREE;
5558 struct find_parameter_pack_data ppd;
5559 ppd.parameter_packs = &parameter_packs;
5560 ppd.visited = new hash_set<tree>;
5561 ppd.type_pack_expansion_p = false;
5562
5563 fixed_parameter_pack_p_1 (parm, ppd: &ppd);
5564
5565 delete ppd.visited;
5566 return parameter_packs;
5567}
5568
5569/* Check that a template declaration's use of default arguments and
5570 parameter packs is not invalid. Here, PARMS are the template
5571 parameters. IS_PRIMARY is true if DECL is the thing declared by
5572 a primary template. IS_PARTIAL is true if DECL is a partial
5573 specialization.
5574
5575 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5576 function template declaration or a friend class template
5577 declaration. In the function case, 1 indicates a declaration, 2
5578 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5579 emitted for extraneous default arguments.
5580
5581 Returns TRUE if there were no errors found, FALSE otherwise. */
5582
5583bool
5584check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5585 bool is_partial, int is_friend_decl)
5586{
5587 const char *msg;
5588 int last_level_to_check;
5589 tree parm_level;
5590 bool no_errors = true;
5591
5592 /* [temp.param]
5593
5594 A default template-argument shall not be specified in a
5595 function template declaration or a function template definition, nor
5596 in the template-parameter-list of the definition of a member of a
5597 class template. */
5598
5599 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5600 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5601 /* You can't have a function template declaration in a local
5602 scope, nor you can you define a member of a class template in a
5603 local scope. */
5604 return true;
5605
5606 if ((TREE_CODE (decl) == TYPE_DECL
5607 && TREE_TYPE (decl)
5608 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5609 || (TREE_CODE (decl) == FUNCTION_DECL
5610 && LAMBDA_FUNCTION_P (decl)))
5611 /* A lambda doesn't have an explicit declaration; don't complain
5612 about the parms of the enclosing class. */
5613 return true;
5614
5615 if (current_class_type
5616 && !TYPE_BEING_DEFINED (current_class_type)
5617 && DECL_LANG_SPECIFIC (decl)
5618 && DECL_DECLARES_FUNCTION_P (decl)
5619 /* If this is either a friend defined in the scope of the class
5620 or a member function. */
5621 && (DECL_FUNCTION_MEMBER_P (decl)
5622 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5623 : DECL_FRIEND_CONTEXT (decl)
5624 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5625 : false)
5626 /* And, if it was a member function, it really was defined in
5627 the scope of the class. */
5628 && (!DECL_FUNCTION_MEMBER_P (decl)
5629 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5630 /* We already checked these parameters when the template was
5631 declared, so there's no need to do it again now. This function
5632 was defined in class scope, but we're processing its body now
5633 that the class is complete. */
5634 return true;
5635
5636 /* Core issue 226 (C++0x only): the following only applies to class
5637 templates. */
5638 if (is_primary
5639 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5640 {
5641 /* [temp.param]
5642
5643 If a template-parameter has a default template-argument, all
5644 subsequent template-parameters shall have a default
5645 template-argument supplied. */
5646 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5647 {
5648 tree inner_parms = TREE_VALUE (parm_level);
5649 int ntparms = TREE_VEC_LENGTH (inner_parms);
5650 int seen_def_arg_p = 0;
5651 int i;
5652
5653 for (i = 0; i < ntparms; ++i)
5654 {
5655 tree parm = TREE_VEC_ELT (inner_parms, i);
5656
5657 if (parm == error_mark_node)
5658 continue;
5659
5660 if (TREE_PURPOSE (parm))
5661 seen_def_arg_p = 1;
5662 else if (seen_def_arg_p
5663 && !template_parameter_pack_p (TREE_VALUE (parm)))
5664 {
5665 error ("no default argument for %qD", TREE_VALUE (parm));
5666 /* For better subsequent error-recovery, we indicate that
5667 there should have been a default argument. */
5668 TREE_PURPOSE (parm) = error_mark_node;
5669 no_errors = false;
5670 }
5671 else if (!is_partial
5672 && !is_friend_decl
5673 /* Don't complain about an enclosing partial
5674 specialization. */
5675 && parm_level == parms
5676 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5677 && i < ntparms - 1
5678 && template_parameter_pack_p (TREE_VALUE (parm))
5679 /* A fixed parameter pack will be partially
5680 instantiated into a fixed length list. */
5681 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5682 {
5683 /* A primary class template, primary variable template
5684 (DR 2032), or alias template can only have one
5685 parameter pack, at the end of the template
5686 parameter list. */
5687
5688 error ("parameter pack %q+D must be at the end of the"
5689 " template parameter list", TREE_VALUE (parm));
5690
5691 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5692 = error_mark_node;
5693 no_errors = false;
5694 }
5695 }
5696 }
5697 }
5698
5699 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5700 || is_partial
5701 || !is_primary
5702 || is_friend_decl)
5703 /* For an ordinary class template, default template arguments are
5704 allowed at the innermost level, e.g.:
5705 template <class T = int>
5706 struct S {};
5707 but, in a partial specialization, they're not allowed even
5708 there, as we have in [temp.class.spec]:
5709
5710 The template parameter list of a specialization shall not
5711 contain default template argument values.
5712
5713 So, for a partial specialization, or for a function template
5714 (in C++98/C++03), we look at all of them. */
5715 ;
5716 else
5717 /* But, for a primary class template that is not a partial
5718 specialization we look at all template parameters except the
5719 innermost ones. */
5720 parms = TREE_CHAIN (parms);
5721
5722 /* Figure out what error message to issue. */
5723 if (is_friend_decl == 2)
5724 msg = G_("default template arguments may not be used in function template "
5725 "friend re-declaration");
5726 else if (is_friend_decl)
5727 msg = G_("default template arguments may not be used in template "
5728 "friend declarations");
5729 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5730 msg = G_("default template arguments may not be used in function templates "
5731 "without %<-std=c++11%> or %<-std=gnu++11%>");
5732 else if (is_partial)
5733 msg = G_("default template arguments may not be used in "
5734 "partial specializations");
5735 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5736 msg = G_("default argument for template parameter for class enclosing %qD");
5737 else
5738 /* Per [temp.param]/9, "A default template-argument shall not be
5739 specified in the template-parameter-lists of the definition of
5740 a member of a class template that appears outside of the member's
5741 class.", thus if we aren't handling a member of a class template
5742 there is no need to examine the parameters. */
5743 return true;
5744
5745 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5746 /* If we're inside a class definition, there's no need to
5747 examine the parameters to the class itself. On the one
5748 hand, they will be checked when the class is defined, and,
5749 on the other, default arguments are valid in things like:
5750 template <class T = double>
5751 struct S { template <class U> void f(U); };
5752 Here the default argument for `S' has no bearing on the
5753 declaration of `f'. */
5754 last_level_to_check = template_class_depth (current_class_type) + 1;
5755 else
5756 /* Check everything. */
5757 last_level_to_check = 0;
5758
5759 for (parm_level = parms;
5760 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5761 parm_level = TREE_CHAIN (parm_level))
5762 {
5763 tree inner_parms = TREE_VALUE (parm_level);
5764 int i;
5765 int ntparms;
5766
5767 ntparms = TREE_VEC_LENGTH (inner_parms);
5768 for (i = 0; i < ntparms; ++i)
5769 {
5770 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5771 continue;
5772
5773 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5774 {
5775 if (msg)
5776 {
5777 no_errors = false;
5778 if (is_friend_decl == 2)
5779 return no_errors;
5780
5781 error (msg, decl);
5782 msg = 0;
5783 }
5784
5785 /* Clear out the default argument so that we are not
5786 confused later. */
5787 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5788 }
5789 }
5790
5791 /* At this point, if we're still interested in issuing messages,
5792 they must apply to classes surrounding the object declared. */
5793 if (msg)
5794 msg = G_("default argument for template parameter for class "
5795 "enclosing %qD");
5796 }
5797
5798 return no_errors;
5799}
5800
5801/* Worker for push_template_decl_real, called via
5802 for_each_template_parm. DATA is really an int, indicating the
5803 level of the parameters we are interested in. If T is a template
5804 parameter of that level, return nonzero. */
5805
5806static int
5807template_parm_this_level_p (tree t, void* data)
5808{
5809 int this_level = *(int *)data;
5810 int level;
5811
5812 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5813 level = TEMPLATE_PARM_LEVEL (t);
5814 else
5815 level = TEMPLATE_TYPE_LEVEL (t);
5816 return level == this_level;
5817}
5818
5819/* Worker for uses_outer_template_parms, called via for_each_template_parm.
5820 DATA is really an int, indicating the innermost outer level of parameters.
5821 If T is a template parameter of that level or further out, return
5822 nonzero. */
5823
5824static int
5825template_parm_outer_level (tree t, void *data)
5826{
5827 int this_level = *(int *)data;
5828 int level;
5829
5830 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5831 level = TEMPLATE_PARM_LEVEL (t);
5832 else
5833 level = TEMPLATE_TYPE_LEVEL (t);
5834 return level <= this_level;
5835}
5836
5837/* Creates a TEMPLATE_DECL for the indicated DECL using the template
5838 parameters given by current_template_args, or reuses a
5839 previously existing one, if appropriate. Returns the DECL, or an
5840 equivalent one, if it is replaced via a call to duplicate_decls.
5841
5842 If IS_FRIEND is true, DECL is a friend declaration. */
5843
5844tree
5845push_template_decl (tree decl, bool is_friend)
5846{
5847 if (decl == error_mark_node || !current_template_parms)
5848 return error_mark_node;
5849
5850 /* See if this is a partial specialization. */
5851 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5852 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5853 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5854 || (VAR_P (decl)
5855 && DECL_LANG_SPECIFIC (decl)
5856 && DECL_TEMPLATE_SPECIALIZATION (decl)
5857 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5858
5859 /* No surprising friend functions. */
5860 gcc_checking_assert (is_friend
5861 || !(TREE_CODE (decl) == FUNCTION_DECL
5862 && DECL_UNIQUE_FRIEND_P (decl)));
5863
5864 tree ctx;
5865 if (is_friend)
5866 /* For a friend, we want the context of the friend, not
5867 the type of which it is a friend. */
5868 ctx = CP_DECL_CONTEXT (decl);
5869 else if (CP_DECL_CONTEXT (decl)
5870 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5871 /* In the case of a virtual function, we want the class in which
5872 it is defined. */
5873 ctx = CP_DECL_CONTEXT (decl);
5874 else
5875 /* Otherwise, if we're currently defining some class, the DECL
5876 is assumed to be a member of the class. */
5877 ctx = current_scope ();
5878
5879 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5880 ctx = NULL_TREE;
5881
5882 if (!DECL_CONTEXT (decl))
5883 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5884
5885 /* See if this is a primary template. */
5886 bool is_primary = false;
5887 if (is_friend && ctx
5888 && uses_template_parms_level (ctx, current_template_depth))
5889 /* A friend template that specifies a class context, i.e.
5890 template <typename T> friend void A<T>::f();
5891 is not primary. */
5892 ;
5893 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5894 /* Lambdas are not primary. */
5895 ;
5896 else
5897 is_primary = template_parm_scope_p ();
5898
5899 /* True if the template is a member template, in the sense of
5900 [temp.mem]. */
5901 bool member_template_p = false;
5902
5903 if (is_primary)
5904 {
5905 warning (OPT_Wtemplates, "template %qD declared", decl);
5906
5907 if (DECL_CLASS_SCOPE_P (decl))
5908 member_template_p = true;
5909
5910 if (TREE_CODE (decl) == TYPE_DECL
5911 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5912 {
5913 error ("template class without a name");
5914 return error_mark_node;
5915 }
5916 else if (TREE_CODE (decl) == FUNCTION_DECL)
5917 {
5918 if (member_template_p)
5919 {
5920 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5921 error ("member template %qD may not have virt-specifiers", decl);
5922 }
5923 if (DECL_DESTRUCTOR_P (decl))
5924 {
5925 /* [temp.mem]
5926
5927 A destructor shall not be a member template. */
5928 error_at (DECL_SOURCE_LOCATION (decl),
5929 "destructor %qD declared as member template", decl);
5930 return error_mark_node;
5931 }
5932 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5933 && (!prototype_p (TREE_TYPE (decl))
5934 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5935 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5936 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5937 == void_list_node)))
5938 {
5939 /* [basic.stc.dynamic.allocation]
5940
5941 An allocation function can be a function
5942 template. ... Template allocation functions shall
5943 have two or more parameters. */
5944 error ("invalid template declaration of %qD", decl);
5945 return error_mark_node;
5946 }
5947 }
5948 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5949 && CLASS_TYPE_P (TREE_TYPE (decl)))
5950 /* Class template. */;
5951 else if (TREE_CODE (decl) == TYPE_DECL
5952 && TYPE_DECL_ALIAS_P (decl))
5953 /* alias-declaration */
5954 gcc_assert (!DECL_ARTIFICIAL (decl));
5955 else if (VAR_P (decl))
5956 /* C++14 variable template. */;
5957 else if (TREE_CODE (decl) == CONCEPT_DECL)
5958 /* C++20 concept definitions. */;
5959 else
5960 {
5961 error ("template declaration of %q#D", decl);
5962 return error_mark_node;
5963 }
5964 }
5965
5966 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5967 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5968 || (VAR_OR_FUNCTION_DECL_P (decl)
5969 && DECL_LOCAL_DECL_P (decl))));
5970
5971 /* Check to see that the rules regarding the use of default
5972 arguments are not being violated. We check args for a friend
5973 functions when we know whether it's a definition, introducing
5974 declaration or re-declaration. */
5975 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5976 check_default_tmpl_args (decl, current_template_parms,
5977 is_primary, is_partial, is_friend_decl: is_friend);
5978
5979 /* Ensure that there are no parameter packs in the type of this
5980 declaration that have not been expanded. */
5981 if (TREE_CODE (decl) == FUNCTION_DECL)
5982 {
5983 /* Check each of the arguments individually to see if there are
5984 any bare parameter packs. */
5985 tree type = TREE_TYPE (decl);
5986 tree arg = DECL_ARGUMENTS (decl);
5987 tree argtype = TYPE_ARG_TYPES (type);
5988
5989 while (arg && argtype)
5990 {
5991 if (!DECL_PACK_P (arg)
5992 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5993 {
5994 /* This is a PARM_DECL that contains unexpanded parameter
5995 packs. We have already complained about this in the
5996 check_for_bare_parameter_packs call, so just replace
5997 these types with ERROR_MARK_NODE. */
5998 TREE_TYPE (arg) = error_mark_node;
5999 TREE_VALUE (argtype) = error_mark_node;
6000 }
6001
6002 arg = DECL_CHAIN (arg);
6003 argtype = TREE_CHAIN (argtype);
6004 }
6005
6006 /* Check for bare parameter packs in the return type and the
6007 exception specifiers. */
6008 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
6009 /* Errors were already issued, set return type to int
6010 as the frontend doesn't expect error_mark_node as
6011 the return type. */
6012 TREE_TYPE (type) = integer_type_node;
6013 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
6014 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
6015 }
6016 else
6017 {
6018 if (check_for_bare_parameter_packs (t: is_typedef_decl (x: decl)
6019 ? DECL_ORIGINAL_TYPE (decl)
6020 : TREE_TYPE (decl)))
6021 {
6022 TREE_TYPE (decl) = error_mark_node;
6023 return error_mark_node;
6024 }
6025
6026 if (is_partial && VAR_P (decl)
6027 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
6028 return error_mark_node;
6029 }
6030
6031 if (is_partial)
6032 return process_partial_specialization (decl);
6033
6034 tree args = current_template_args ();
6035 tree tmpl = NULL_TREE;
6036 bool new_template_p = false;
6037 if (local_p)
6038 {
6039 /* Does not get a template head. */
6040 tmpl = NULL_TREE;
6041 gcc_checking_assert (!is_primary);
6042 }
6043 else if (!ctx
6044 || TREE_CODE (ctx) == FUNCTION_DECL
6045 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
6046 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
6047 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
6048 && DECL_TEMPLATE_INFO (decl))))
6049 {
6050 if (DECL_LANG_SPECIFIC (decl)
6051 && DECL_TEMPLATE_INFO (decl)
6052 && DECL_TI_TEMPLATE (decl))
6053 tmpl = DECL_TI_TEMPLATE (decl);
6054 /* If DECL is a TYPE_DECL for a class-template, then there won't
6055 be DECL_LANG_SPECIFIC. The information equivalent to
6056 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6057 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
6058 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
6059 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
6060 {
6061 /* Since a template declaration already existed for this
6062 class-type, we must be redeclaring it here. Make sure
6063 that the redeclaration is valid. */
6064 redeclare_class_template (TREE_TYPE (decl),
6065 current_template_parms,
6066 current_template_constraints ());
6067 /* We don't need to create a new TEMPLATE_DECL; just use the
6068 one we already had. */
6069 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6070 }
6071 else
6072 {
6073 tmpl = build_template_decl (decl, current_template_parms,
6074 member_template_p);
6075 new_template_p = true;
6076
6077 if (DECL_LANG_SPECIFIC (decl)
6078 && DECL_TEMPLATE_SPECIALIZATION (decl))
6079 {
6080 /* A specialization of a member template of a template
6081 class. */
6082 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
6083 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
6084 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
6085 }
6086 }
6087 }
6088 else
6089 {
6090 tree a, t, current, parms;
6091 int i;
6092 tree tinfo = get_template_info (t: decl);
6093
6094 if (!tinfo)
6095 {
6096 error ("template definition of non-template %q#D", decl);
6097 return error_mark_node;
6098 }
6099
6100 tmpl = TI_TEMPLATE (tinfo);
6101
6102 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
6103 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
6104 && DECL_TEMPLATE_SPECIALIZATION (decl)
6105 && DECL_MEMBER_TEMPLATE_P (tmpl))
6106 {
6107 /* The declaration is a specialization of a member
6108 template, declared outside the class. Therefore, the
6109 innermost template arguments will be NULL, so we
6110 replace them with the arguments determined by the
6111 earlier call to check_explicit_specialization. */
6112 args = DECL_TI_ARGS (decl);
6113
6114 tree new_tmpl
6115 = build_template_decl (decl, current_template_parms,
6116 member_template_p);
6117 DECL_TI_TEMPLATE (decl) = new_tmpl;
6118 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
6119 DECL_TEMPLATE_INFO (new_tmpl)
6120 = build_template_info (template_decl: tmpl, template_args: args);
6121
6122 register_specialization (spec: new_tmpl,
6123 tmpl: most_general_template (tmpl),
6124 args,
6125 is_friend, hash: 0);
6126 return decl;
6127 }
6128
6129 /* Make sure the template headers we got make sense. */
6130
6131 parms = DECL_TEMPLATE_PARMS (tmpl);
6132 i = TMPL_PARMS_DEPTH (parms);
6133 if (TMPL_ARGS_DEPTH (args) != i)
6134 {
6135 error ("expected %d levels of template parms for %q#D, got %d",
6136 i, decl, TMPL_ARGS_DEPTH (args));
6137 DECL_INTERFACE_KNOWN (decl) = 1;
6138 return error_mark_node;
6139 }
6140 else
6141 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6142 {
6143 a = TMPL_ARGS_LEVEL (args, i);
6144 t = INNERMOST_TEMPLATE_PARMS (parms);
6145
6146 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6147 {
6148 if (current == decl)
6149 error ("got %d template parameters for %q#D",
6150 TREE_VEC_LENGTH (a), decl);
6151 else
6152 error ("got %d template parameters for %q#T",
6153 TREE_VEC_LENGTH (a), current);
6154 error (" but %d required", TREE_VEC_LENGTH (t));
6155 /* Avoid crash in import_export_decl. */
6156 DECL_INTERFACE_KNOWN (decl) = 1;
6157 return error_mark_node;
6158 }
6159
6160 if (current == decl)
6161 current = ctx;
6162 else if (current == NULL_TREE)
6163 /* Can happen in erroneous input. */
6164 break;
6165 else
6166 current = get_containing_scope (current);
6167 }
6168
6169 /* Check that the parms are used in the appropriate qualifying scopes
6170 in the declarator. */
6171 if (!comp_template_args
6172 (TI_ARGS (tinfo),
6173 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6174 {
6175 error ("template arguments to %qD do not match original "
6176 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6177 if (!uses_template_parms (TI_ARGS (tinfo)))
6178 inform (input_location, "use %<template<>%> for"
6179 " an explicit specialization");
6180 /* Avoid crash in import_export_decl. */
6181 DECL_INTERFACE_KNOWN (decl) = 1;
6182 return error_mark_node;
6183 }
6184
6185 /* Check that the constraints for each enclosing template scope are
6186 consistent with the original declarations. */
6187 if (flag_concepts)
6188 {
6189 tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
6190 tree scope_parms = current_template_parms;
6191 if (PRIMARY_TEMPLATE_P (tmpl))
6192 {
6193 decl_parms = TREE_CHAIN (decl_parms);
6194 scope_parms = TREE_CHAIN (scope_parms);
6195 }
6196 while (decl_parms)
6197 {
6198 if (!template_requirements_equivalent_p (parms1: decl_parms, parms2: scope_parms))
6199 {
6200 error ("redeclaration of %qD with different constraints",
6201 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
6202 break;
6203 }
6204 decl_parms = TREE_CHAIN (decl_parms);
6205 scope_parms = TREE_CHAIN (scope_parms);
6206 }
6207 }
6208 }
6209
6210 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6211
6212 if (new_template_p)
6213 {
6214 /* Push template declarations for global functions and types.
6215 Note that we do not try to push a global template friend
6216 declared in a template class; such a thing may well depend on
6217 the template parameters of the class and we'll push it when
6218 instantiating the befriending class. */
6219 if (!ctx
6220 && !(is_friend && template_class_depth (current_class_type) > 0))
6221 {
6222 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6223 if (pushed == error_mark_node)
6224 return error_mark_node;
6225
6226 /* pushdecl may have found an existing template. */
6227 if (pushed != tmpl)
6228 {
6229 decl = DECL_TEMPLATE_RESULT (pushed);
6230 tmpl = NULL_TREE;
6231 }
6232 }
6233 else if (is_friend)
6234 {
6235 /* Record this decl as belonging to the current class. It's
6236 not chained onto anything else. */
6237 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6238 gcc_checking_assert (!DECL_CHAIN (tmpl));
6239 DECL_CHAIN (tmpl) = current_scope ();
6240 }
6241 }
6242 else if (tmpl)
6243 /* The type may have been completed, or (erroneously) changed. */
6244 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6245
6246 if (tmpl)
6247 {
6248 if (is_primary)
6249 {
6250 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6251
6252 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6253
6254 /* Give template template parms a DECL_CONTEXT of the template
6255 for which they are a parameter. */
6256 parms = INNERMOST_TEMPLATE_PARMS (parms);
6257 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6258 {
6259 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6260 if (TREE_CODE (parm) == TEMPLATE_DECL)
6261 DECL_CONTEXT (parm) = tmpl;
6262 }
6263
6264 if (TREE_CODE (decl) == TYPE_DECL
6265 && TYPE_DECL_ALIAS_P (decl))
6266 {
6267 if (tree constr
6268 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6269 {
6270 /* ??? Why don't we do this here for all templates? */
6271 constr = build_constraints (constr, NULL_TREE);
6272 set_constraints (decl, constr);
6273 }
6274 }
6275 }
6276
6277 /* The DECL_TI_ARGS of DECL contains full set of arguments
6278 referring wback to its most general template. If TMPL is a
6279 specialization, ARGS may only have the innermost set of
6280 arguments. Add the missing argument levels if necessary. */
6281 if (DECL_TEMPLATE_INFO (tmpl))
6282 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), extra_args: args);
6283
6284 tree info = build_template_info (template_decl: tmpl, template_args: args);
6285
6286 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6287 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6288 else
6289 {
6290 retrofit_lang_decl (decl);
6291 DECL_TEMPLATE_INFO (decl) = info;
6292 }
6293 }
6294
6295 if (flag_implicit_templates
6296 && !is_friend
6297 && TREE_PUBLIC (decl)
6298 && VAR_OR_FUNCTION_DECL_P (decl))
6299 /* Set DECL_COMDAT on template instantiations; if we force
6300 them to be emitted by explicit instantiation,
6301 mark_needed will tell cgraph to do the right thing. */
6302 DECL_COMDAT (decl) = true;
6303
6304 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6305
6306 return decl;
6307}
6308
6309/* FN is an inheriting constructor that inherits from the constructor
6310 template INHERITED; turn FN into a constructor template with a matching
6311 template header. */
6312
6313tree
6314add_inherited_template_parms (tree fn, tree inherited)
6315{
6316 tree inner_parms
6317 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6318 inner_parms = copy_node (inner_parms);
6319 tree parms
6320 = tree_cons (size_int (current_template_depth + 1),
6321 inner_parms, current_template_parms);
6322 tree tmpl = build_template_decl (decl: fn, parms, /*member*/member_template_p: true);
6323 tree args = template_parms_to_args (parms);
6324 DECL_TEMPLATE_INFO (fn) = build_template_info (template_decl: tmpl, template_args: args);
6325 DECL_ARTIFICIAL (tmpl) = true;
6326 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6327 return tmpl;
6328}
6329
6330/* Called when a class template TYPE is redeclared with the indicated
6331 template PARMS, e.g.:
6332
6333 template <class T> struct S;
6334 template <class T> struct S {}; */
6335
6336bool
6337redeclare_class_template (tree type, tree parms, tree cons)
6338{
6339 tree tmpl;
6340 tree tmpl_parms;
6341 int i;
6342
6343 if (!TYPE_TEMPLATE_INFO (type))
6344 {
6345 error ("%qT is not a template type", type);
6346 return false;
6347 }
6348
6349 tmpl = TYPE_TI_TEMPLATE (type);
6350 if (!PRIMARY_TEMPLATE_P (tmpl))
6351 /* The type is nested in some template class. Nothing to worry
6352 about here; there are no new template parameters for the nested
6353 type. */
6354 return true;
6355
6356 if (!parms)
6357 {
6358 error ("template specifiers not specified in declaration of %qD",
6359 tmpl);
6360 return false;
6361 }
6362
6363 parms = INNERMOST_TEMPLATE_PARMS (parms);
6364 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6365
6366 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6367 {
6368 error_n (input_location, TREE_VEC_LENGTH (parms),
6369 "redeclared with %d template parameter",
6370 "redeclared with %d template parameters",
6371 TREE_VEC_LENGTH (parms));
6372 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6373 "previous declaration %qD used %d template parameter",
6374 "previous declaration %qD used %d template parameters",
6375 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6376 return false;
6377 }
6378
6379 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6380 {
6381 tree tmpl_parm;
6382 tree parm;
6383
6384 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6385 || TREE_VEC_ELT (parms, i) == error_mark_node)
6386 continue;
6387
6388 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6389 if (error_operand_p (t: tmpl_parm))
6390 return false;
6391
6392 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6393
6394 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6395 TEMPLATE_DECL. */
6396 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6397 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6398 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6399 || (TREE_CODE (tmpl_parm) != PARM_DECL
6400 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6401 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6402 || (TREE_CODE (tmpl_parm) == PARM_DECL
6403 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6404 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6405 {
6406 auto_diagnostic_group d;
6407 error ("template parameter %q+#D", tmpl_parm);
6408 if (DECL_P (parm))
6409 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
6410 else
6411 inform (input_location, "redeclared here");
6412 return false;
6413 }
6414
6415 /* The parameters can be declared to introduce different
6416 constraints. */
6417 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6418 tree p2 = TREE_VEC_ELT (parms, i);
6419 if (!template_parameter_constraints_equivalent_p (parm1: p1, parm2: p2))
6420 {
6421 auto_diagnostic_group d;
6422 error ("declaration of template parameter %q+#D with different "
6423 "constraints", parm);
6424 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6425 "original declaration appeared here");
6426 return false;
6427 }
6428
6429 /* Give each template template parm in this redeclaration a
6430 DECL_CONTEXT of the template for which they are a parameter. */
6431 if (TREE_CODE (parm) == TEMPLATE_DECL)
6432 {
6433 gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
6434 || DECL_CONTEXT (parm) == tmpl);
6435 DECL_CONTEXT (parm) = tmpl;
6436 }
6437 }
6438
6439 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
6440 return false;
6441
6442 tree ci = get_constraints (tmpl);
6443 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6444 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6445
6446 /* Two classes with different constraints declare different entities. */
6447 if (!cp_tree_equal (req1, req2))
6448 {
6449 auto_diagnostic_group d;
6450 error_at (input_location, "redeclaration of %q#D with different "
6451 "constraints", tmpl);
6452 inform (DECL_SOURCE_LOCATION (tmpl),
6453 "original declaration appeared here");
6454 return false;
6455 }
6456
6457 return true;
6458}
6459
6460/* The actual substitution part of instantiate_non_dependent_expr,
6461 to be used when the caller has already checked
6462 !instantiation_dependent_uneval_expression_p (expr)
6463 and cleared processing_template_decl. */
6464
6465tree
6466instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6467{
6468 return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
6469}
6470
6471/* Instantiate the non-dependent expression EXPR. */
6472
6473tree
6474instantiate_non_dependent_expr (tree expr,
6475 tsubst_flags_t complain /* = tf_error */)
6476{
6477 if (expr == NULL_TREE)
6478 return NULL_TREE;
6479
6480 if (processing_template_decl)
6481 {
6482 /* The caller should have checked this already. */
6483 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
6484 processing_template_decl_sentinel s;
6485 expr = instantiate_non_dependent_expr_internal (expr, complain);
6486 }
6487 return expr;
6488}
6489
6490/* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6491 expression is dependent or non-constant. */
6492
6493tree
6494instantiate_non_dependent_or_null (tree expr)
6495{
6496 if (expr == NULL_TREE)
6497 return NULL_TREE;
6498 if (processing_template_decl)
6499 {
6500 if (!is_nondependent_constant_expression (expr))
6501 expr = NULL_TREE;
6502 else
6503 {
6504 processing_template_decl_sentinel s;
6505 expr = instantiate_non_dependent_expr_internal (expr, complain: tf_error);
6506 }
6507 }
6508 return expr;
6509}
6510
6511/* True iff T is a specialization of a variable template. */
6512
6513bool
6514variable_template_specialization_p (tree t)
6515{
6516 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6517 return false;
6518 tree tmpl = DECL_TI_TEMPLATE (t);
6519 return variable_template_p (t: tmpl);
6520}
6521
6522/* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6523 template declaration, or a TYPE_DECL for an alias declaration. */
6524
6525bool
6526alias_type_or_template_p (tree t)
6527{
6528 if (t == NULL_TREE)
6529 return false;
6530 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6531 || (TYPE_P (t)
6532 && TYPE_NAME (t)
6533 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6534 || DECL_ALIAS_TEMPLATE_P (t));
6535}
6536
6537/* If T is a specialization of an alias template, return it; otherwise return
6538 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6539
6540tree
6541alias_template_specialization_p (const_tree t,
6542 bool transparent_typedefs)
6543{
6544 if (!TYPE_P (t))
6545 return NULL_TREE;
6546
6547 /* It's an alias template specialization if it's an alias and its
6548 TYPE_NAME is a specialization of a primary template. */
6549 if (typedef_variant_p (type: t))
6550 {
6551 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6552 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6553 return CONST_CAST_TREE (t);
6554 if (transparent_typedefs)
6555 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6556 (TYPE_NAME (t)),
6557 transparent_typedefs);
6558 }
6559
6560 return NULL_TREE;
6561}
6562
6563/* A cache of the result of complex_alias_template_p. */
6564
6565static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
6566
6567/* Data structure for complex_alias_template_*. */
6568
6569struct uses_all_template_parms_data
6570{
6571 int level;
6572 tree *seen;
6573};
6574
6575/* walk_tree callback for complex_alias_template_p. */
6576
6577static tree
6578complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
6579{
6580 tree t = *tp;
6581 auto &data = *(struct uses_all_template_parms_data*)data_;
6582
6583 switch (TREE_CODE (t))
6584 {
6585 case TEMPLATE_TYPE_PARM:
6586 case TEMPLATE_PARM_INDEX:
6587 case TEMPLATE_TEMPLATE_PARM:
6588 case BOUND_TEMPLATE_TEMPLATE_PARM:
6589 {
6590 tree idx = get_template_parm_index (parm: t);
6591 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6592 data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
6593 }
6594
6595 default:;
6596 }
6597
6598 if (!PACK_EXPANSION_P (t))
6599 return 0;
6600
6601 /* An alias template with a pack expansion that expands a pack from the
6602 enclosing class needs to be considered complex, to avoid confusion with
6603 the same pack being used as an argument to the alias's own template
6604 parameter (91966). */
6605 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6606 pack = TREE_CHAIN (pack))
6607 {
6608 tree parm_pack = TREE_VALUE (pack);
6609 if (!TEMPLATE_PARM_P (parm_pack))
6610 continue;
6611 int idx, level;
6612 template_parm_level_and_index (parm_pack, &level, &idx);
6613 if (level < data.level)
6614 return t;
6615
6616 /* Consider the expanded packs to be used outside the expansion... */
6617 data.seen[idx] = boolean_true_node;
6618 }
6619
6620 /* ...but don't walk into the pattern. Consider PR104008:
6621
6622 template <typename T, typename... Ts>
6623 using IsOneOf = disjunction<is_same<T, Ts>...>;
6624
6625 where IsOneOf seemingly uses all of its template parameters in its
6626 expansion (and does not expand a pack from the enclosing class), so the
6627 alias was not marked as complex. However, if it is used like
6628 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6629 expansion. So only Ts is considered used by the pack expansion. */
6630 *walk_subtrees = false;
6631
6632 return 0;
6633}
6634
6635/* An alias template is complex from a SFINAE perspective if a template-id
6636 using that alias can be ill-formed when the expansion is not, as with
6637 the void_t template.
6638
6639 If this predicate returns true in the ordinary case, the out parameter
6640 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
6641 the I'th template parameter of the alias template is used in the alias. */
6642
6643static bool
6644complex_alias_template_p (const_tree tmpl, tree *seen_out)
6645{
6646 tmpl = most_general_template (tmpl);
6647 if (!PRIMARY_TEMPLATE_P (tmpl))
6648 return false;
6649
6650 /* A renaming alias isn't complex. */
6651 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6652 return false;
6653
6654 /* Any other constrained alias is complex. */
6655 if (get_constraints (tmpl))
6656 return true;
6657
6658 if (!complex_alias_tmpl_info)
6659 complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (size: 13);
6660
6661 if (tree *slot = complex_alias_tmpl_info->get (k: tmpl))
6662 {
6663 tree result = *slot;
6664 if (result == boolean_false_node)
6665 return false;
6666 if (result == boolean_true_node)
6667 return true;
6668 gcc_assert (TREE_CODE (result) == TREE_VEC);
6669 if (seen_out)
6670 *seen_out = result;
6671 return true;
6672 }
6673
6674 struct uses_all_template_parms_data data;
6675 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6676 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6677 data.level = TMPL_PARMS_DEPTH (parms);
6678 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6679 tree seen = make_tree_vec (len);
6680 data.seen = TREE_VEC_BEGIN (seen);
6681 for (int i = 0; i < len; ++i)
6682 data.seen[i] = boolean_false_node;
6683
6684 if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
6685 {
6686 complex_alias_tmpl_info->put (k: tmpl, boolean_true_node);
6687 return true;
6688 }
6689
6690 for (int i = 0; i < len; ++i)
6691 if (data.seen[i] != boolean_true_node)
6692 {
6693 complex_alias_tmpl_info->put (k: tmpl, v: seen);
6694 if (seen_out)
6695 *seen_out = seen;
6696 return true;
6697 }
6698
6699 complex_alias_tmpl_info->put (k: tmpl, boolean_false_node);
6700 return false;
6701}
6702
6703/* If T is a specialization of a complex alias template with a dependent
6704 argument for an unused template parameter, return it; otherwise return
6705 NULL_TREE. If T is a typedef to such a specialization, return the
6706 specialization. */
6707
6708tree
6709dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6710{
6711 if (t == error_mark_node)
6712 return NULL_TREE;
6713 gcc_assert (TYPE_P (t));
6714
6715 if (!processing_template_decl || !typedef_variant_p (type: t))
6716 return NULL_TREE;
6717
6718 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6719 {
6720 tree seen = NULL_TREE;
6721 if (complex_alias_template_p (TI_TEMPLATE (tinfo), seen_out: &seen))
6722 {
6723 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6724 if (!seen)
6725 {
6726 if (any_dependent_template_arguments_p (args))
6727 return CONST_CAST_TREE (t);
6728 }
6729 else
6730 {
6731 gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
6732 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
6733 if (TREE_VEC_ELT (seen, i) != boolean_true_node
6734 && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
6735 return CONST_CAST_TREE (t);
6736 }
6737
6738 return NULL_TREE;
6739 }
6740 }
6741
6742 if (transparent_typedefs)
6743 {
6744 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6745 return dependent_alias_template_spec_p (t: utype, transparent_typedefs);
6746 }
6747
6748 return NULL_TREE;
6749}
6750
6751/* Return the number of innermost template parameters in TMPL. */
6752
6753static int
6754num_innermost_template_parms (const_tree tmpl)
6755{
6756 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6757 return TREE_VEC_LENGTH (parms);
6758}
6759
6760/* Return either TMPL or another template that it is equivalent to under DR
6761 1286: An alias that just changes the name of a template is equivalent to
6762 the other template. */
6763
6764static tree
6765get_underlying_template (tree tmpl)
6766{
6767 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6768 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6769 {
6770 /* Determine if the alias is equivalent to an underlying template. */
6771 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6772 /* The underlying type may have been ill-formed. Don't proceed. */
6773 if (!orig_type)
6774 break;
6775 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6776 if (!tinfo)
6777 break;
6778
6779 tree underlying = TI_TEMPLATE (tinfo);
6780 if (!PRIMARY_TEMPLATE_P (underlying)
6781 || (num_innermost_template_parms (tmpl)
6782 != num_innermost_template_parms (tmpl: underlying)))
6783 break;
6784
6785 /* Does the alias add cv-quals? */
6786 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6787 break;
6788
6789 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6790 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6791 break;
6792
6793 /* Are any default template arguments equivalent? */
6794 tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6795 tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
6796 const int nparms = TREE_VEC_LENGTH (aparms);
6797 for (int i = 0; i < nparms; ++i)
6798 {
6799 tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
6800 tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
6801 if (!template_args_equal (adefarg, udefarg))
6802 goto top_break;
6803 }
6804
6805 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6806 it's appropriate to treat a less-constrained alias as equivalent. */
6807 if (!at_least_as_constrained (underlying, tmpl))
6808 break;
6809
6810 /* Alias is equivalent. Strip it and repeat. */
6811 tmpl = underlying;
6812 }
6813 top_break:;
6814
6815 return tmpl;
6816}
6817
6818/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6819 must be a reference-to-function or a pointer-to-function type, as specified
6820 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6821 and check that the resulting function has external linkage. */
6822
6823static tree
6824convert_nontype_argument_function (tree type, tree expr,
6825 tsubst_flags_t complain)
6826{
6827 tree fns = expr;
6828 tree fn, fn_no_ptr;
6829 linkage_kind linkage;
6830
6831 fn = instantiate_type (type, fns, tf_none);
6832 if (fn == error_mark_node)
6833 return error_mark_node;
6834
6835 if (value_dependent_expression_p (fn))
6836 goto accept;
6837
6838 fn_no_ptr = fn;
6839 if (REFERENCE_REF_P (fn_no_ptr))
6840 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6841 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6842 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6843 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6844 if (BASELINK_P (fn_no_ptr))
6845 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6846
6847 /* [temp.arg.nontype]/1
6848
6849 A template-argument for a non-type, non-template template-parameter
6850 shall be one of:
6851 [...]
6852 -- the address of an object or function with external [C++11: or
6853 internal] linkage. */
6854
6855 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6856 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6857 {
6858 if (complain & tf_error)
6859 {
6860 location_t loc = cp_expr_loc_or_input_loc (t: expr);
6861 error_at (loc, "%qE is not a valid template argument for type %qT",
6862 expr, type);
6863 if (TYPE_PTR_P (type))
6864 inform (loc, "it must be the address of a function "
6865 "with external linkage");
6866 else
6867 inform (loc, "it must be the name of a function with "
6868 "external linkage");
6869 }
6870 return NULL_TREE;
6871 }
6872
6873 linkage = decl_linkage (fn_no_ptr);
6874 if ((cxx_dialect < cxx11 && linkage != lk_external)
6875 || (cxx_dialect < cxx17 && linkage == lk_none))
6876 {
6877 if (complain & tf_error)
6878 {
6879 location_t loc = cp_expr_loc_or_input_loc (t: expr);
6880 if (cxx_dialect >= cxx11)
6881 error_at (loc, "%qE is not a valid template argument for type "
6882 "%qT because %qD has no linkage",
6883 expr, type, fn_no_ptr);
6884 else
6885 error_at (loc, "%qE is not a valid template argument for type "
6886 "%qT because %qD does not have external linkage",
6887 expr, type, fn_no_ptr);
6888 }
6889 return NULL_TREE;
6890 }
6891
6892 accept:
6893 if (TYPE_REF_P (type))
6894 {
6895 if (REFERENCE_REF_P (fn))
6896 fn = TREE_OPERAND (fn, 0);
6897 else
6898 fn = build_address (fn);
6899 }
6900 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6901 fn = build_nop (type, fn);
6902
6903 return fn;
6904}
6905
6906/* Subroutine of convert_nontype_argument.
6907 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6908 Emit an error otherwise. */
6909
6910static bool
6911check_valid_ptrmem_cst_expr (tree type, tree expr,
6912 tsubst_flags_t complain)
6913{
6914 tree orig_expr = expr;
6915 STRIP_NOPS (expr);
6916 if (null_ptr_cst_p (expr))
6917 return true;
6918 if (TREE_CODE (expr) == PTRMEM_CST
6919 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6920 PTRMEM_CST_CLASS (expr)))
6921 return true;
6922 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6923 return true;
6924 if (processing_template_decl
6925 && TREE_CODE (expr) == ADDR_EXPR
6926 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6927 return true;
6928 if (complain & tf_error)
6929 {
6930 location_t loc = cp_expr_loc_or_input_loc (t: orig_expr);
6931 error_at (loc, "%qE is not a valid template argument for type %qT",
6932 orig_expr, type);
6933 if (TREE_CODE (expr) != PTRMEM_CST)
6934 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6935 else
6936 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6937 }
6938 return false;
6939}
6940
6941/* Returns TRUE iff the address of OP is value-dependent.
6942
6943 14.6.2.4 [temp.dep.temp]:
6944 A non-integral non-type template-argument is dependent if its type is
6945 dependent or it has either of the following forms
6946 qualified-id
6947 & qualified-id
6948 and contains a nested-name-specifier which specifies a class-name that
6949 names a dependent type.
6950
6951 We generalize this to just say that the address of a member of a
6952 dependent class is value-dependent; the above doesn't cover the
6953 address of a static data member named with an unqualified-id. */
6954
6955static bool
6956has_value_dependent_address (tree op)
6957{
6958 STRIP_ANY_LOCATION_WRAPPER (op);
6959
6960 /* We could use get_inner_reference here, but there's no need;
6961 this is only relevant for template non-type arguments, which
6962 can only be expressed as &id-expression. */
6963 if (DECL_P (op))
6964 {
6965 tree ctx = CP_DECL_CONTEXT (op);
6966
6967 if (TYPE_P (ctx) && dependent_type_p (ctx))
6968 return true;
6969
6970 if (VAR_P (op)
6971 && TREE_STATIC (op)
6972 && TREE_CODE (ctx) == FUNCTION_DECL
6973 && type_dependent_expression_p (ctx))
6974 return true;
6975 }
6976
6977 return false;
6978}
6979
6980/* The next set of functions are used for providing helpful explanatory
6981 diagnostics for failed overload resolution. Their messages should be
6982 indented by two spaces for consistency with the messages in
6983 call.cc */
6984
6985static int
6986unify_success (bool /*explain_p*/)
6987{
6988 return 0;
6989}
6990
6991/* Other failure functions should call this one, to provide a single function
6992 for setting a breakpoint on. */
6993
6994static int
6995unify_invalid (bool /*explain_p*/)
6996{
6997 return 1;
6998}
6999
7000static int
7001unify_parameter_deduction_failure (bool explain_p, tree parm)
7002{
7003 if (explain_p)
7004 inform (input_location,
7005 " couldn%'t deduce template parameter %qD", parm);
7006 return unify_invalid (explain_p);
7007}
7008
7009static int
7010unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
7011{
7012 if (explain_p)
7013 inform (input_location,
7014 " types %qT and %qT have incompatible cv-qualifiers",
7015 parm, arg);
7016 return unify_invalid (explain_p);
7017}
7018
7019static int
7020unify_type_mismatch (bool explain_p, tree parm, tree arg)
7021{
7022 if (explain_p)
7023 inform (input_location, " mismatched types %qT and %qT", parm, arg);
7024 return unify_invalid (explain_p);
7025}
7026
7027static int
7028unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
7029{
7030 if (explain_p)
7031 inform (input_location,
7032 " template parameter %qD is not a parameter pack, but "
7033 "argument %qD is",
7034 parm, arg);
7035 return unify_invalid (explain_p);
7036}
7037
7038static int
7039unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
7040{
7041 if (explain_p)
7042 inform (input_location,
7043 " template argument %qE does not match "
7044 "pointer-to-member constant %qE",
7045 arg, parm);
7046 return unify_invalid (explain_p);
7047}
7048
7049static int
7050unify_expression_unequal (bool explain_p, tree parm, tree arg)
7051{
7052 if (explain_p)
7053 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
7054 return unify_invalid (explain_p);
7055}
7056
7057static int
7058unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
7059{
7060 if (explain_p)
7061 inform (input_location,
7062 " inconsistent parameter pack deduction with %qT and %qT",
7063 old_arg, new_arg);
7064 return unify_invalid (explain_p);
7065}
7066
7067static int
7068unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
7069{
7070 if (explain_p)
7071 {
7072 if (TYPE_P (parm))
7073 inform (input_location,
7074 " deduced conflicting types for parameter %qT (%qT and %qT)",
7075 parm, first, second);
7076 else
7077 inform (input_location,
7078 " deduced conflicting values for non-type parameter "
7079 "%qE (%qE and %qE)", parm, first, second);
7080 }
7081 return unify_invalid (explain_p);
7082}
7083
7084static int
7085unify_vla_arg (bool explain_p, tree arg)
7086{
7087 if (explain_p)
7088 inform (input_location,
7089 " variable-sized array type %qT is not "
7090 "a valid template argument",
7091 arg);
7092 return unify_invalid (explain_p);
7093}
7094
7095static int
7096unify_method_type_error (bool explain_p, tree arg)
7097{
7098 if (explain_p)
7099 inform (input_location,
7100 " member function type %qT is not a valid template argument",
7101 arg);
7102 return unify_invalid (explain_p);
7103}
7104
7105static int
7106unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
7107{
7108 if (explain_p)
7109 {
7110 if (least_p)
7111 inform_n (input_location, wanted,
7112 " candidate expects at least %d argument, %d provided",
7113 " candidate expects at least %d arguments, %d provided",
7114 wanted, have);
7115 else
7116 inform_n (input_location, wanted,
7117 " candidate expects %d argument, %d provided",
7118 " candidate expects %d arguments, %d provided",
7119 wanted, have);
7120 }
7121 return unify_invalid (explain_p);
7122}
7123
7124static int
7125unify_too_many_arguments (bool explain_p, int have, int wanted)
7126{
7127 return unify_arity (explain_p, have, wanted);
7128}
7129
7130static int
7131unify_too_few_arguments (bool explain_p, int have, int wanted,
7132 bool least_p = false)
7133{
7134 return unify_arity (explain_p, have, wanted, least_p);
7135}
7136
7137static int
7138unify_arg_conversion (bool explain_p, tree to_type,
7139 tree from_type, tree arg)
7140{
7141 if (explain_p)
7142 inform (cp_expr_loc_or_input_loc (t: arg),
7143 " cannot convert %qE (type %qT) to type %qT",
7144 arg, from_type, to_type);
7145 return unify_invalid (explain_p);
7146}
7147
7148static int
7149unify_no_common_base (bool explain_p, enum template_base_result r,
7150 tree parm, tree arg)
7151{
7152 if (explain_p)
7153 switch (r)
7154 {
7155 case tbr_ambiguous_baseclass:
7156 inform (input_location, " %qT is an ambiguous base class of %qT",
7157 parm, arg);
7158 break;
7159 default:
7160 inform (input_location, " %qT is not derived from %qT", arg, parm);
7161 break;
7162 }
7163 return unify_invalid (explain_p);
7164}
7165
7166static int
7167unify_inconsistent_template_template_parameters (bool explain_p)
7168{
7169 if (explain_p)
7170 inform (input_location,
7171 " template parameters of a template template argument are "
7172 "inconsistent with other deduced template arguments");
7173 return unify_invalid (explain_p);
7174}
7175
7176static int
7177unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
7178{
7179 if (explain_p)
7180 inform (input_location,
7181 " cannot deduce a template for %qT from non-template type %qT",
7182 parm, arg);
7183 return unify_invalid (explain_p);
7184}
7185
7186static int
7187unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
7188{
7189 if (explain_p)
7190 inform (input_location,
7191 " template argument %qE does not match %qE", arg, parm);
7192 return unify_invalid (explain_p);
7193}
7194
7195/* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7196 argument for TYPE, points to an unsuitable object.
7197
7198 Also adjust the type of the index in C++20 array subobject references. */
7199
7200static bool
7201invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7202{
7203 switch (TREE_CODE (expr))
7204 {
7205 CASE_CONVERT:
7206 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7207 complain);
7208
7209 case TARGET_EXPR:
7210 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7211 complain);
7212
7213 case CONSTRUCTOR:
7214 {
7215 for (auto &e: CONSTRUCTOR_ELTS (expr))
7216 if (invalid_tparm_referent_p (TREE_TYPE (e.value), expr: e.value, complain))
7217 return true;
7218 }
7219 break;
7220
7221 case ADDR_EXPR:
7222 {
7223 tree decl = TREE_OPERAND (expr, 0);
7224
7225 if (cxx_dialect >= cxx20)
7226 while (TREE_CODE (decl) == COMPONENT_REF
7227 || TREE_CODE (decl) == ARRAY_REF)
7228 {
7229 tree &op = TREE_OPERAND (decl, 1);
7230 if (TREE_CODE (decl) == ARRAY_REF
7231 && TREE_CODE (op) == INTEGER_CST)
7232 /* Canonicalize array offsets to ptrdiff_t; how they were
7233 written doesn't matter for subobject identity. */
7234 op = fold_convert (ptrdiff_type_node, op);
7235 decl = TREE_OPERAND (decl, 0);
7236 }
7237
7238 if (!VAR_OR_FUNCTION_DECL_P (decl))
7239 {
7240 if (complain & tf_error)
7241 error_at (cp_expr_loc_or_input_loc (t: expr),
7242 "%qE is not a valid template argument of type %qT "
7243 "because %qE is not a variable or function",
7244 expr, type, decl);
7245 return true;
7246 }
7247 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7248 {
7249 if (complain & tf_error)
7250 error_at (cp_expr_loc_or_input_loc (t: expr),
7251 "%qE is not a valid template argument of type %qT "
7252 "in C++98 because %qD does not have external linkage",
7253 expr, type, decl);
7254 return true;
7255 }
7256 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7257 && decl_linkage (decl) == lk_none)
7258 {
7259 if (complain & tf_error)
7260 error_at (cp_expr_loc_or_input_loc (t: expr),
7261 "%qE is not a valid template argument of type %qT "
7262 "because %qD has no linkage", expr, type, decl);
7263 return true;
7264 }
7265 /* C++17: For a non-type template-parameter of reference or pointer
7266 type, the value of the constant expression shall not refer to (or
7267 for a pointer type, shall not be the address of):
7268 * a subobject (4.5),
7269 * a temporary object (15.2),
7270 * a string literal (5.13.5),
7271 * the result of a typeid expression (8.2.8), or
7272 * a predefined __func__ variable (11.4.1). */
7273 else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
7274 && !DECL_NTTP_OBJECT_P (decl))
7275 {
7276 gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl));
7277 if (complain & tf_error)
7278 error ("the address of %qD is not a valid template argument",
7279 decl);
7280 return true;
7281 }
7282 else if (cxx_dialect < cxx20
7283 && !(same_type_ignoring_top_level_qualifiers_p
7284 (strip_array_types (TREE_TYPE (type)),
7285 strip_array_types (TREE_TYPE (decl)))))
7286 {
7287 if (complain & tf_error)
7288 error ("the address of the %qT subobject of %qD is not a "
7289 "valid template argument", TREE_TYPE (type), decl);
7290 return true;
7291 }
7292 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7293 {
7294 if (complain & tf_error)
7295 error ("the address of %qD is not a valid template argument "
7296 "because it does not have static storage duration",
7297 decl);
7298 return true;
7299 }
7300 }
7301 break;
7302
7303 default:
7304 if (!INDIRECT_TYPE_P (type))
7305 /* We're only concerned about pointers and references here. */;
7306 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7307 /* Null pointer values are OK in C++11. */;
7308 else
7309 {
7310 if (VAR_P (expr))
7311 {
7312 if (complain & tf_error)
7313 error ("%qD is not a valid template argument "
7314 "because %qD is a variable, not the address of "
7315 "a variable", expr, expr);
7316 return true;
7317 }
7318 else
7319 {
7320 if (complain & tf_error)
7321 error ("%qE is not a valid template argument for %qT "
7322 "because it is not the address of a variable",
7323 expr, type);
7324 return true;
7325 }
7326 }
7327 }
7328 return false;
7329
7330}
7331
7332/* Return a VAR_DECL for the C++20 template parameter object corresponding to
7333 template argument EXPR. */
7334
7335static tree
7336create_template_parm_object (tree expr, tsubst_flags_t complain)
7337{
7338 tree orig = expr;
7339 if (TREE_CODE (expr) == TARGET_EXPR)
7340 expr = TARGET_EXPR_INITIAL (expr);
7341
7342 if (!TREE_CONSTANT (expr))
7343 {
7344 if ((complain & tf_error)
7345 && require_rvalue_constant_expression (orig))
7346 cxx_constant_value (orig);
7347 return error_mark_node;
7348 }
7349 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7350 return error_mark_node;
7351
7352 /* This is no longer a compound literal. */
7353 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7354
7355 return get_template_parm_object (expr, mangle: mangle_template_parm_object (expr));
7356}
7357
7358/* The template arguments corresponding to template parameter objects of types
7359 that contain pointers to members. */
7360
7361static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7362
7363/* Find or build an nttp object for (already-validated) EXPR with name
7364 NAME. */
7365
7366tree
7367get_template_parm_object (tree expr, tree name)
7368{
7369 tree decl = get_global_binding (id: name);
7370 if (decl)
7371 return decl;
7372
7373 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7374 decl = create_temporary_var (type);
7375 DECL_NTTP_OBJECT_P (decl) = true;
7376 DECL_CONTEXT (decl) = NULL_TREE;
7377 TREE_STATIC (decl) = true;
7378 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7379 TREE_READONLY (decl) = true;
7380 DECL_NAME (decl) = name;
7381 SET_DECL_ASSEMBLER_NAME (decl, name);
7382 comdat_linkage (decl);
7383
7384 if (!zero_init_p (type))
7385 {
7386 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7387 lower_var_init before we're done mangling. So store the original
7388 value elsewhere. */
7389 tree copy = unshare_constructor (expr);
7390 hash_map_safe_put<hm_ggc> (h&: tparm_obj_values, k: decl, v: copy);
7391 }
7392
7393 pushdecl_top_level_and_finish (decl, expr);
7394
7395 return decl;
7396}
7397
7398/* Return the actual template argument corresponding to template parameter
7399 object VAR. */
7400
7401tree
7402tparm_object_argument (tree var)
7403{
7404 if (zero_init_p (TREE_TYPE (var)))
7405 return DECL_INITIAL (var);
7406 return *(tparm_obj_values->get (k: var));
7407}
7408
7409/* Attempt to convert the non-type template parameter EXPR to the
7410 indicated TYPE. If the conversion is successful, return the
7411 converted value. If the conversion is unsuccessful, return
7412 NULL_TREE if we issued an error message, or error_mark_node if we
7413 did not. We issue error messages for out-and-out bad template
7414 parameters, but not simply because the conversion failed, since we
7415 might be just trying to do argument deduction. Both TYPE and EXPR
7416 must be non-dependent.
7417
7418 The conversion follows the special rules described in
7419 [temp.arg.nontype], and it is much more strict than an implicit
7420 conversion.
7421
7422 This function is called twice for each template argument (see
7423 lookup_template_class for a more accurate description of this
7424 problem). This means that we need to handle expressions which
7425 are not valid in a C++ source, but can be created from the
7426 first call (for instance, casts to perform conversions). These
7427 hacks can go away after we fix the double coercion problem. */
7428
7429static tree
7430convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7431{
7432 tree expr_type;
7433 location_t loc = cp_expr_loc_or_input_loc (t: expr);
7434
7435 /* Detect immediately string literals as invalid non-type argument.
7436 This special-case is not needed for correctness (we would easily
7437 catch this later), but only to provide better diagnostic for this
7438 common user mistake. As suggested by DR 100, we do not mention
7439 linkage issues in the diagnostic as this is not the point. */
7440 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7441 {
7442 if (complain & tf_error)
7443 error ("%qE is not a valid template argument for type %qT "
7444 "because string literals can never be used in this context",
7445 expr, type);
7446 return NULL_TREE;
7447 }
7448
7449 /* Add the ADDR_EXPR now for the benefit of
7450 value_dependent_expression_p. */
7451 if (TYPE_PTROBV_P (type)
7452 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7453 {
7454 expr = decay_conversion (expr, complain);
7455 if (expr == error_mark_node)
7456 return error_mark_node;
7457 }
7458
7459 /* If we are in a template, EXPR may be non-dependent, but still
7460 have a syntactic, rather than semantic, form. For example, EXPR
7461 might be a SCOPE_REF, rather than the VAR_DECL to which the
7462 SCOPE_REF refers. Preserving the qualifying scope is necessary
7463 so that access checking can be performed when the template is
7464 instantiated -- but here we need the resolved form so that we can
7465 convert the argument. */
7466 bool non_dep = false;
7467 if (TYPE_REF_OBJ_P (type)
7468 && has_value_dependent_address (op: expr))
7469 /* If we want the address and it's value-dependent, don't fold. */;
7470 else if (processing_template_decl
7471 && !instantiation_dependent_expression_p (expr))
7472 non_dep = true;
7473 if (error_operand_p (t: expr))
7474 return error_mark_node;
7475 expr_type = TREE_TYPE (expr);
7476
7477 /* If the argument is non-dependent, perform any conversions in
7478 non-dependent context as well. */
7479 processing_template_decl_sentinel s (non_dep);
7480 if (non_dep)
7481 expr = instantiate_non_dependent_expr_internal (expr, complain);
7482
7483 bool val_dep_p = value_dependent_expression_p (expr);
7484 if (val_dep_p)
7485 expr = canonicalize_expr_argument (expr, complain);
7486 else
7487 STRIP_ANY_LOCATION_WRAPPER (expr);
7488
7489 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7490 to a non-type argument of "nullptr". */
7491 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7492 expr = fold_simple (convert (type, expr));
7493
7494 /* In C++11, integral or enumeration non-type template arguments can be
7495 arbitrary constant expressions. Pointer and pointer to
7496 member arguments can be general constant expressions that evaluate
7497 to a null value, but otherwise still need to be of a specific form. */
7498 if (cxx_dialect >= cxx11)
7499 {
7500 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7501 /* A PTRMEM_CST is already constant, and a valid template
7502 argument for a parameter of pointer to member type, we just want
7503 to leave it in that form rather than lower it to a
7504 CONSTRUCTOR. */;
7505 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7506 || cxx_dialect >= cxx17)
7507 {
7508 /* C++17: A template-argument for a non-type template-parameter shall
7509 be a converted constant expression (8.20) of the type of the
7510 template-parameter. */
7511 expr = build_converted_constant_expr (type, expr, complain);
7512 if (expr == error_mark_node)
7513 /* Make sure we return NULL_TREE only if we have really issued
7514 an error, as described above. */
7515 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7516 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7517 {
7518 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7519 return expr;
7520 }
7521 expr = maybe_constant_value (expr, NULL_TREE, mce_true);
7522 expr = convert_from_reference (expr);
7523 /* EXPR may have become value-dependent. */
7524 val_dep_p = value_dependent_expression_p (expr);
7525 }
7526 else if (TYPE_PTR_OR_PTRMEM_P (type))
7527 {
7528 tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
7529 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7530 : null_member_pointer_value_p (folded))
7531 expr = folded;
7532 }
7533 }
7534
7535 if (TYPE_REF_P (type))
7536 expr = mark_lvalue_use (expr);
7537 else
7538 expr = mark_rvalue_use (expr);
7539
7540 /* HACK: Due to double coercion, we can get a
7541 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7542 which is the tree that we built on the first call (see
7543 below when coercing to reference to object or to reference to
7544 function). We just strip everything and get to the arg.
7545 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7546 for examples. */
7547 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7548 {
7549 /* Check this before we strip *& to avoid redundancy. */
7550 if (!mark_single_function (expr, complain))
7551 return error_mark_node;
7552
7553 tree probe_type, probe = expr;
7554 if (REFERENCE_REF_P (probe))
7555 probe = TREE_OPERAND (probe, 0);
7556 probe_type = TREE_TYPE (probe);
7557 if (TREE_CODE (probe) == NOP_EXPR)
7558 {
7559 /* ??? Maybe we could use convert_from_reference here, but we
7560 would need to relax its constraints because the NOP_EXPR
7561 could actually change the type to something more cv-qualified,
7562 and this is not folded by convert_from_reference. */
7563 tree addr = TREE_OPERAND (probe, 0);
7564 if (TYPE_REF_P (probe_type)
7565 && TREE_CODE (addr) == ADDR_EXPR
7566 && TYPE_PTR_P (TREE_TYPE (addr))
7567 && (same_type_ignoring_top_level_qualifiers_p
7568 (TREE_TYPE (probe_type),
7569 TREE_TYPE (TREE_TYPE (addr)))))
7570 {
7571 expr = TREE_OPERAND (addr, 0);
7572 expr_type = TREE_TYPE (probe_type);
7573 }
7574 }
7575 }
7576
7577 /* [temp.arg.nontype]/5, bullet 1
7578
7579 For a non-type template-parameter of integral or enumeration type,
7580 integral promotions (_conv.prom_) and integral conversions
7581 (_conv.integral_) are applied. */
7582 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7583 || SCALAR_FLOAT_TYPE_P (type))
7584 {
7585 if (cxx_dialect < cxx11)
7586 {
7587 tree t = build_converted_constant_expr (type, expr, complain);
7588 t = maybe_constant_value (t);
7589 if (t != error_mark_node)
7590 expr = t;
7591 }
7592
7593 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7594 return error_mark_node;
7595
7596 /* Notice that there are constant expressions like '4 % 0' which
7597 do not fold into integer constants. */
7598 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7599 {
7600 if (complain & tf_error)
7601 {
7602 int errs = errorcount, warns = warningcount + werrorcount;
7603 if (!require_potential_constant_expression (expr))
7604 expr = error_mark_node;
7605 else
7606 expr = cxx_constant_value (expr);
7607 if (errorcount > errs || warningcount + werrorcount > warns)
7608 inform (loc, "in template argument for type %qT", type);
7609 if (expr == error_mark_node)
7610 return NULL_TREE;
7611 /* else cxx_constant_value complained but gave us
7612 a real constant, so go ahead. */
7613 if (!CONSTANT_CLASS_P (expr))
7614 {
7615 /* Some assemble time constant expressions like
7616 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7617 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7618 as we can emit them into .rodata initializers of
7619 variables, yet they can't fold into an INTEGER_CST at
7620 compile time. Refuse them here. */
7621 gcc_checking_assert (reduced_constant_expression_p (expr));
7622 error_at (loc, "template argument %qE for type %qT not "
7623 "a compile-time constant", expr, type);
7624 return NULL_TREE;
7625 }
7626 }
7627 else
7628 return NULL_TREE;
7629 }
7630
7631 /* Avoid typedef problems. */
7632 if (TREE_TYPE (expr) != type)
7633 expr = fold_convert (type, expr);
7634 }
7635 /* [temp.arg.nontype]/5, bullet 2
7636
7637 For a non-type template-parameter of type pointer to object,
7638 qualification conversions (_conv.qual_) and the array-to-pointer
7639 conversion (_conv.array_) are applied. */
7640 else if (TYPE_PTROBV_P (type))
7641 {
7642 tree decayed = expr;
7643
7644 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7645 decay_conversion or an explicit cast. If it's a problematic cast,
7646 we'll complain about it below. */
7647 if (TREE_CODE (expr) == NOP_EXPR)
7648 {
7649 tree probe = expr;
7650 STRIP_NOPS (probe);
7651 if (TREE_CODE (probe) == ADDR_EXPR
7652 && TYPE_PTR_P (TREE_TYPE (probe)))
7653 {
7654 expr = probe;
7655 expr_type = TREE_TYPE (expr);
7656 }
7657 }
7658
7659 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7660
7661 A template-argument for a non-type, non-template template-parameter
7662 shall be one of: [...]
7663
7664 -- the name of a non-type template-parameter;
7665 -- the address of an object or function with external linkage, [...]
7666 expressed as "& id-expression" where the & is optional if the name
7667 refers to a function or array, or if the corresponding
7668 template-parameter is a reference.
7669
7670 Here, we do not care about functions, as they are invalid anyway
7671 for a parameter of type pointer-to-object. */
7672
7673 if (val_dep_p)
7674 /* Non-type template parameters are OK. */
7675 ;
7676 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7677 /* Null pointer values are OK in C++11. */;
7678 else if (TREE_CODE (expr) != ADDR_EXPR
7679 && !INDIRECT_TYPE_P (expr_type))
7680 /* Other values, like integer constants, might be valid
7681 non-type arguments of some other type. */
7682 return error_mark_node;
7683 else if (invalid_tparm_referent_p (type, expr, complain))
7684 return NULL_TREE;
7685
7686 expr = decayed;
7687
7688 expr = perform_qualification_conversions (type, expr);
7689 if (expr == error_mark_node)
7690 return error_mark_node;
7691 }
7692 /* [temp.arg.nontype]/5, bullet 3
7693
7694 For a non-type template-parameter of type reference to object, no
7695 conversions apply. The type referred to by the reference may be more
7696 cv-qualified than the (otherwise identical) type of the
7697 template-argument. The template-parameter is bound directly to the
7698 template-argument, which must be an lvalue. */
7699 else if (TYPE_REF_OBJ_P (type))
7700 {
7701 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7702 expr_type))
7703 return error_mark_node;
7704
7705 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7706 {
7707 if (complain & tf_error)
7708 error ("%qE is not a valid template argument for type %qT "
7709 "because of conflicts in cv-qualification", expr, type);
7710 return NULL_TREE;
7711 }
7712
7713 if (!lvalue_p (expr))
7714 {
7715 if (complain & tf_error)
7716 error ("%qE is not a valid template argument for type %qT "
7717 "because it is not an lvalue", expr, type);
7718 return NULL_TREE;
7719 }
7720
7721 /* [temp.arg.nontype]/1
7722
7723 A template-argument for a non-type, non-template template-parameter
7724 shall be one of: [...]
7725
7726 -- the address of an object or function with external linkage. */
7727 if (INDIRECT_REF_P (expr)
7728 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7729 {
7730 expr = TREE_OPERAND (expr, 0);
7731 if (DECL_P (expr))
7732 {
7733 if (complain & tf_error)
7734 error ("%q#D is not a valid template argument for type %qT "
7735 "because a reference variable does not have a constant "
7736 "address", expr, type);
7737 return NULL_TREE;
7738 }
7739 }
7740
7741 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7742 /* OK, dependent reference. We don't want to ask whether a DECL is
7743 itself value-dependent, since what we want here is its address. */;
7744 else
7745 {
7746 expr = build_address (expr);
7747
7748 if (invalid_tparm_referent_p (type, expr, complain))
7749 return NULL_TREE;
7750 }
7751
7752 if (!same_type_p (type, TREE_TYPE (expr)))
7753 expr = build_nop (type, expr);
7754 }
7755 /* [temp.arg.nontype]/5, bullet 4
7756
7757 For a non-type template-parameter of type pointer to function, only
7758 the function-to-pointer conversion (_conv.func_) is applied. If the
7759 template-argument represents a set of overloaded functions (or a
7760 pointer to such), the matching function is selected from the set
7761 (_over.over_). */
7762 else if (TYPE_PTRFN_P (type))
7763 {
7764 /* If the argument is a template-id, we might not have enough
7765 context information to decay the pointer. */
7766 if (!type_unknown_p (expr: expr_type))
7767 {
7768 expr = decay_conversion (expr, complain);
7769 if (expr == error_mark_node)
7770 return error_mark_node;
7771 }
7772
7773 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7774 /* Null pointer values are OK in C++11. */
7775 return perform_qualification_conversions (type, expr);
7776
7777 expr = convert_nontype_argument_function (type, expr, complain);
7778 if (!expr || expr == error_mark_node)
7779 return expr;
7780 }
7781 /* [temp.arg.nontype]/5, bullet 5
7782
7783 For a non-type template-parameter of type reference to function, no
7784 conversions apply. If the template-argument represents a set of
7785 overloaded functions, the matching function is selected from the set
7786 (_over.over_). */
7787 else if (TYPE_REFFN_P (type))
7788 {
7789 if (TREE_CODE (expr) == ADDR_EXPR)
7790 {
7791 if (complain & tf_error)
7792 {
7793 error ("%qE is not a valid template argument for type %qT "
7794 "because it is a pointer", expr, type);
7795 inform (input_location, "try using %qE instead",
7796 TREE_OPERAND (expr, 0));
7797 }
7798 return NULL_TREE;
7799 }
7800
7801 expr = convert_nontype_argument_function (type, expr, complain);
7802 if (!expr || expr == error_mark_node)
7803 return expr;
7804 }
7805 /* [temp.arg.nontype]/5, bullet 6
7806
7807 For a non-type template-parameter of type pointer to member function,
7808 no conversions apply. If the template-argument represents a set of
7809 overloaded member functions, the matching member function is selected
7810 from the set (_over.over_). */
7811 else if (TYPE_PTRMEMFUNC_P (type))
7812 {
7813 expr = instantiate_type (type, expr, tf_none);
7814 if (expr == error_mark_node)
7815 return error_mark_node;
7816
7817 /* [temp.arg.nontype] bullet 1 says the pointer to member
7818 expression must be a pointer-to-member constant. */
7819 if (!val_dep_p
7820 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7821 return NULL_TREE;
7822
7823 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7824 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7825 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7826 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7827 }
7828 /* [temp.arg.nontype]/5, bullet 7
7829
7830 For a non-type template-parameter of type pointer to data member,
7831 qualification conversions (_conv.qual_) are applied. */
7832 else if (TYPE_PTRDATAMEM_P (type))
7833 {
7834 /* [temp.arg.nontype] bullet 1 says the pointer to member
7835 expression must be a pointer-to-member constant. */
7836 if (!val_dep_p
7837 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7838 return NULL_TREE;
7839
7840 expr = perform_qualification_conversions (type, expr);
7841 if (expr == error_mark_node)
7842 return expr;
7843 }
7844 else if (NULLPTR_TYPE_P (type))
7845 {
7846 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7847 {
7848 if (complain & tf_error)
7849 error ("%qE is not a valid template argument for type %qT "
7850 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7851 return NULL_TREE;
7852 }
7853 return expr;
7854 }
7855 else if (CLASS_TYPE_P (type))
7856 {
7857 /* Replace the argument with a reference to the corresponding template
7858 parameter object. */
7859 if (!val_dep_p)
7860 expr = create_template_parm_object (expr, complain);
7861 if (expr == error_mark_node)
7862 return NULL_TREE;
7863 }
7864 /* A template non-type parameter must be one of the above. */
7865 else
7866 gcc_unreachable ();
7867
7868 /* Sanity check: did we actually convert the argument to the
7869 right type? */
7870 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7871 (type, TREE_TYPE (expr)));
7872 return convert_from_reference (expr);
7873}
7874
7875/* Subroutine of coerce_template_template_parms, which returns 1 if
7876 PARM_PARM and ARG_PARM match using the rule for the template
7877 parameters of template template parameters. Both PARM and ARG are
7878 template parameters; the rest of the arguments are the same as for
7879 coerce_template_template_parms.
7880 */
7881static int
7882coerce_template_template_parm (tree parm,
7883 tree arg,
7884 tsubst_flags_t complain,
7885 tree in_decl,
7886 tree outer_args)
7887{
7888 if (arg == NULL_TREE || error_operand_p (t: arg)
7889 || parm == NULL_TREE || error_operand_p (t: parm))
7890 return 0;
7891
7892 if (TREE_CODE (arg) != TREE_CODE (parm))
7893 return 0;
7894
7895 switch (TREE_CODE (parm))
7896 {
7897 case TEMPLATE_DECL:
7898 /* We encounter instantiations of templates like
7899 template <template <template <class> class> class TT>
7900 class C; */
7901 {
7902 if (!coerce_template_template_parms
7903 (parm, arg, complain, in_decl, outer_args))
7904 return 0;
7905 }
7906 /* Fall through. */
7907
7908 case TYPE_DECL:
7909 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7910 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7911 /* Argument is a parameter pack but parameter is not. */
7912 return 0;
7913 break;
7914
7915 case PARM_DECL:
7916 /* The tsubst call is used to handle cases such as
7917
7918 template <int> class C {};
7919 template <class T, template <T> class TT> class D {};
7920 D<int, C> d;
7921
7922 i.e. the parameter list of TT depends on earlier parameters. */
7923 if (!uses_template_parms (TREE_TYPE (arg)))
7924 {
7925 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7926 if (!uses_template_parms (t)
7927 && !same_type_p (t, TREE_TYPE (arg)))
7928 return 0;
7929 }
7930
7931 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7932 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7933 /* Argument is a parameter pack but parameter is not. */
7934 return 0;
7935
7936 break;
7937
7938 default:
7939 gcc_unreachable ();
7940 }
7941
7942 return 1;
7943}
7944
7945/* Coerce template argument list ARGLIST for use with template
7946 template-parameter TEMPL. */
7947
7948static tree
7949coerce_template_args_for_ttp (tree templ, tree arglist,
7950 tsubst_flags_t complain)
7951{
7952 /* Consider an example where a template template parameter declared as
7953
7954 template <class T, class U = std::allocator<T> > class TT
7955
7956 The template parameter level of T and U are one level larger than
7957 of TT. To proper process the default argument of U, say when an
7958 instantiation `TT<int>' is seen, we need to build the full
7959 arguments containing {int} as the innermost level. Outer levels,
7960 available when not appearing as default template argument, can be
7961 obtained from the arguments of the enclosing template.
7962
7963 Suppose that TT is later substituted with std::vector. The above
7964 instantiation is `TT<int, std::allocator<T> >' with TT at
7965 level 1, and T at level 2, while the template arguments at level 1
7966 becomes {std::vector} and the inner level 2 is {int}. */
7967
7968 tree outer = DECL_CONTEXT (templ);
7969 if (outer)
7970 outer = generic_targs_for (tmpl: outer);
7971 else if (current_template_parms)
7972 {
7973 /* This is an argument of the current template, so we haven't set
7974 DECL_CONTEXT yet. We can also get here when level-lowering a
7975 bound ttp. */
7976 tree relevant_template_parms;
7977
7978 /* Parameter levels that are greater than the level of the given
7979 template template parm are irrelevant. */
7980 relevant_template_parms = current_template_parms;
7981 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7982 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7983 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7984
7985 outer = template_parms_to_args (parms: relevant_template_parms);
7986 }
7987
7988 if (outer)
7989 arglist = add_to_template_args (args: outer, extra_args: arglist);
7990
7991 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7992 return coerce_template_parms (parmlist, arglist, templ, complain);
7993}
7994
7995/* A cache of template template parameters with match-all default
7996 arguments. */
7997static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7998
7999/* T is a bound template template-parameter. Copy its arguments into default
8000 arguments of the template template-parameter's template parameters. */
8001
8002static tree
8003add_defaults_to_ttp (tree otmpl)
8004{
8005 if (tree *c = hash_map_safe_get (h: defaulted_ttp_cache, k: otmpl))
8006 return *c;
8007
8008 tree ntmpl = copy_node (otmpl);
8009
8010 tree ntype = copy_node (TREE_TYPE (otmpl));
8011 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
8012 TYPE_MAIN_VARIANT (ntype) = ntype;
8013 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
8014 TYPE_NAME (ntype) = ntmpl;
8015 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
8016
8017 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
8018 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
8019 TEMPLATE_PARM_DECL (idx) = ntmpl;
8020 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
8021
8022 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
8023 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
8024 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
8025 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
8026 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
8027 {
8028 tree o = TREE_VEC_ELT (vec, i);
8029 if (!template_parameter_pack_p (TREE_VALUE (o)))
8030 {
8031 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
8032 TREE_PURPOSE (n) = any_targ_node;
8033 }
8034 }
8035
8036 tree oresult = DECL_TEMPLATE_RESULT (otmpl);
8037 tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
8038 tree gen_ntmpl;
8039 if (gen_otmpl == otmpl)
8040 gen_ntmpl = ntmpl;
8041 else
8042 gen_ntmpl = add_defaults_to_ttp (otmpl: gen_otmpl);
8043
8044 tree nresult = copy_decl (oresult);
8045 DECL_TEMPLATE_INFO (nresult)
8046 = build_template_info (template_decl: gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
8047 DECL_TEMPLATE_RESULT (ntmpl) = nresult;
8048
8049 hash_map_safe_put<hm_ggc> (h&: defaulted_ttp_cache, k: otmpl, v: ntmpl);
8050 return ntmpl;
8051}
8052
8053/* ARG is a bound potential template template-argument, and PARGS is a list
8054 of arguments for the corresponding template template-parameter. Adjust
8055 PARGS as appropriate for application to ARG's template, and if ARG is a
8056 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
8057 arguments to the template template parameter. */
8058
8059static tree
8060coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
8061{
8062 ++processing_template_decl;
8063 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
8064 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8065 {
8066 /* When comparing two template template-parameters in partial ordering,
8067 rewrite the one currently being used as an argument to have default
8068 arguments for all parameters. */
8069 arg_tmpl = add_defaults_to_ttp (otmpl: arg_tmpl);
8070 pargs = coerce_template_args_for_ttp (templ: arg_tmpl, arglist: pargs, complain);
8071 if (pargs != error_mark_node)
8072 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
8073 TYPE_TI_ARGS (arg));
8074 }
8075 else
8076 {
8077 tree aparms
8078 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
8079 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
8080 }
8081 --processing_template_decl;
8082 return pargs;
8083}
8084
8085/* Subroutine of unify for the case when PARM is a
8086 BOUND_TEMPLATE_TEMPLATE_PARM. */
8087
8088static int
8089unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
8090 bool explain_p)
8091{
8092 tree parmvec = TYPE_TI_ARGS (parm);
8093 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
8094
8095 /* The template template parm might be variadic and the argument
8096 not, so flatten both argument lists. */
8097 parmvec = expand_template_argument_pack (args: parmvec);
8098 argvec = expand_template_argument_pack (args: argvec);
8099
8100 if (flag_new_ttp)
8101 {
8102 /* In keeping with P0522R0, adjust P's template arguments
8103 to apply to A's template; then flatten it again. */
8104 tree nparmvec = coerce_ttp_args_for_tta (arg, pargs: parmvec, complain: tf_none);
8105 nparmvec = expand_template_argument_pack (args: nparmvec);
8106
8107 if (unify (tparms, targs, nparmvec, argvec,
8108 UNIFY_ALLOW_NONE, explain_p))
8109 return 1;
8110
8111 /* If the P0522 adjustment eliminated a pack expansion, deduce
8112 empty packs. */
8113 if (flag_new_ttp
8114 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
8115 && unify_pack_expansion (tparms, targs, parmvec, argvec,
8116 DEDUCE_EXACT, /*sub*/true, explain_p))
8117 return 1;
8118 }
8119 else
8120 {
8121 /* Deduce arguments T, i from TT<T> or TT<i>.
8122 We check each element of PARMVEC and ARGVEC individually
8123 rather than the whole TREE_VEC since they can have
8124 different number of elements, which is allowed under N2555. */
8125
8126 int len = TREE_VEC_LENGTH (parmvec);
8127
8128 /* Check if the parameters end in a pack, making them
8129 variadic. */
8130 int parm_variadic_p = 0;
8131 if (len > 0
8132 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
8133 parm_variadic_p = 1;
8134
8135 for (int i = 0; i < len - parm_variadic_p; ++i)
8136 /* If the template argument list of P contains a pack
8137 expansion that is not the last template argument, the
8138 entire template argument list is a non-deduced
8139 context. */
8140 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
8141 return unify_success (explain_p);
8142
8143 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
8144 return unify_too_few_arguments (explain_p,
8145 TREE_VEC_LENGTH (argvec), wanted: len);
8146
8147 for (int i = 0; i < len - parm_variadic_p; ++i)
8148 if (unify (tparms, targs,
8149 TREE_VEC_ELT (parmvec, i),
8150 TREE_VEC_ELT (argvec, i),
8151 UNIFY_ALLOW_NONE, explain_p))
8152 return 1;
8153
8154 if (parm_variadic_p
8155 && unify_pack_expansion (tparms, targs,
8156 parmvec, argvec,
8157 DEDUCE_EXACT,
8158 /*subr=*/true, explain_p))
8159 return 1;
8160 }
8161
8162 return 0;
8163}
8164
8165/* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
8166 template template parameters.
8167
8168 Consider the example:
8169 template <class T> class A;
8170 template<template <class U> class TT> class B;
8171
8172 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
8173 and OUTER_ARGS contains A. */
8174
8175static int
8176coerce_template_template_parms (tree parm_tmpl,
8177 tree arg_tmpl,
8178 tsubst_flags_t complain,
8179 tree in_decl,
8180 tree outer_args)
8181{
8182 int nparms, nargs, i;
8183 tree parm, arg;
8184 int variadic_p = 0;
8185
8186 tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
8187 tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
8188 tree gen_arg_tmpl = most_general_template (arg_tmpl);
8189 tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
8190
8191 nparms = TREE_VEC_LENGTH (parm_parms);
8192 nargs = TREE_VEC_LENGTH (arg_parms);
8193
8194 if (flag_new_ttp)
8195 {
8196 /* P0522R0: A template template-parameter P is at least as specialized as
8197 a template template-argument A if, given the following rewrite to two
8198 function templates, the function template corresponding to P is at
8199 least as specialized as the function template corresponding to A
8200 according to the partial ordering rules for function templates
8201 ([temp.func.order]). Given an invented class template X with the
8202 template parameter list of A (including default arguments):
8203
8204 * Each of the two function templates has the same template parameters,
8205 respectively, as P or A.
8206
8207 * Each function template has a single function parameter whose type is
8208 a specialization of X with template arguments corresponding to the
8209 template parameters from the respective function template where, for
8210 each template parameter PP in the template parameter list of the
8211 function template, a corresponding template argument AA is formed. If
8212 PP declares a parameter pack, then AA is the pack expansion
8213 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8214
8215 If the rewrite produces an invalid type, then P is not at least as
8216 specialized as A. */
8217
8218 /* So coerce P's args to apply to A's parms, and then deduce between A's
8219 args and the converted args. If that succeeds, A is at least as
8220 specialized as P, so they match.*/
8221 processing_template_decl_sentinel ptds (/*reset*/false);
8222 ++processing_template_decl;
8223
8224 tree pargs = template_parms_level_to_args (parms: parm_parms);
8225
8226 /* PARM and ARG might be at different template depths, and we want to
8227 pass the right additional levels of args when coercing PARGS to
8228 ARG_PARMS in case we need to do any substitution into non-type
8229 template parameter types.
8230
8231 OUTER_ARGS are not the right outer levels in this case, as they are
8232 the args we're building up for PARM, and for the coercion we want the
8233 args for ARG. If DECL_CONTEXT isn't set for a template template
8234 parameter, we can assume that it's in the current scope. */
8235 tree ctx = DECL_CONTEXT (arg_tmpl);
8236 if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8237 ctx = current_scope ();
8238 tree scope_args = NULL_TREE;
8239 if (tree tinfo = get_template_info (t: ctx))
8240 scope_args = TI_ARGS (tinfo);
8241 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8242 {
8243 int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
8244 int scope_depth = TMPL_ARGS_DEPTH (scope_args);
8245 tree full_pargs = make_tree_vec (level + 1);
8246
8247 /* Only use as many levels from the scope as needed
8248 (excluding the level of ARG). */
8249 for (int i = 0; i < level - 1; ++i)
8250 if (i < scope_depth)
8251 TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
8252 else
8253 TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
8254
8255 /* Add the arguments that appear at the levels of ARG. */
8256 tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
8257 adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
8258 TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
8259
8260 TREE_VEC_ELT (full_pargs, level) = pargs;
8261 pargs = full_pargs;
8262 }
8263 else
8264 pargs = add_to_template_args (args: scope_args, extra_args: pargs);
8265
8266 pargs = coerce_template_parms (gen_arg_parms, pargs,
8267 NULL_TREE, tf_none);
8268 if (pargs != error_mark_node)
8269 {
8270 tree targs = make_tree_vec (nargs);
8271 tree aargs = template_parms_level_to_args (parms: arg_parms);
8272 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8273 /*explain*/false))
8274 return 1;
8275 }
8276 }
8277
8278 /* Determine whether we have a parameter pack at the end of the
8279 template template parameter's template parameter list. */
8280 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8281 {
8282 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8283
8284 if (error_operand_p (t: parm))
8285 return 0;
8286
8287 switch (TREE_CODE (parm))
8288 {
8289 case TEMPLATE_DECL:
8290 case TYPE_DECL:
8291 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8292 variadic_p = 1;
8293 break;
8294
8295 case PARM_DECL:
8296 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8297 variadic_p = 1;
8298 break;
8299
8300 default:
8301 gcc_unreachable ();
8302 }
8303 }
8304
8305 if (nargs != nparms
8306 && !(variadic_p && nargs >= nparms - 1))
8307 return 0;
8308
8309 /* Check all of the template parameters except the parameter pack at
8310 the end (if any). */
8311 for (i = 0; i < nparms - variadic_p; ++i)
8312 {
8313 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8314 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8315 continue;
8316
8317 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8318 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8319
8320 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8321 outer_args))
8322 return 0;
8323
8324 }
8325
8326 if (variadic_p)
8327 {
8328 /* Check each of the template parameters in the template
8329 argument against the template parameter pack at the end of
8330 the template template parameter. */
8331 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8332 return 0;
8333
8334 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8335
8336 for (; i < nargs; ++i)
8337 {
8338 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8339 continue;
8340
8341 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8342
8343 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8344 outer_args))
8345 return 0;
8346 }
8347 }
8348
8349 return 1;
8350}
8351
8352/* Verifies that the deduced template arguments (in TARGS) for the
8353 template template parameters (in TPARMS) represent valid bindings,
8354 by comparing the template parameter list of each template argument
8355 to the template parameter list of its corresponding template
8356 template parameter, in accordance with DR150. This
8357 routine can only be called after all template arguments have been
8358 deduced. It will return TRUE if all of the template template
8359 parameter bindings are okay, FALSE otherwise. */
8360bool
8361template_template_parm_bindings_ok_p (tree tparms, tree targs)
8362{
8363 int i, ntparms = TREE_VEC_LENGTH (tparms);
8364 bool ret = true;
8365
8366 /* We're dealing with template parms in this process. */
8367 ++processing_template_decl;
8368
8369 targs = INNERMOST_TEMPLATE_ARGS (targs);
8370
8371 for (i = 0; i < ntparms; ++i)
8372 {
8373 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8374 tree targ = TREE_VEC_ELT (targs, i);
8375
8376 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8377 {
8378 tree packed_args = NULL_TREE;
8379 int idx, len = 1;
8380
8381 if (ARGUMENT_PACK_P (targ))
8382 {
8383 /* Look inside the argument pack. */
8384 packed_args = ARGUMENT_PACK_ARGS (targ);
8385 len = TREE_VEC_LENGTH (packed_args);
8386 }
8387
8388 for (idx = 0; idx < len; ++idx)
8389 {
8390 if (packed_args)
8391 /* Extract the next argument from the argument
8392 pack. */
8393 targ = TREE_VEC_ELT (packed_args, idx);
8394
8395 if (PACK_EXPANSION_P (targ))
8396 /* Look at the pattern of the pack expansion. */
8397 targ = PACK_EXPANSION_PATTERN (targ);
8398
8399 /* Extract the template parameters from the template
8400 argument. */
8401 if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8402 targ = TYPE_NAME (targ);
8403
8404 /* Verify that we can coerce the template template
8405 parameters from the template argument to the template
8406 parameter. This requires an exact match. */
8407 if (TREE_CODE (targ) == TEMPLATE_DECL
8408 && !coerce_template_template_parms
8409 (parm_tmpl: tparm,
8410 arg_tmpl: targ,
8411 complain: tf_none,
8412 in_decl: tparm,
8413 outer_args: targs))
8414 {
8415 ret = false;
8416 goto out;
8417 }
8418 }
8419 }
8420 }
8421
8422 out:
8423
8424 --processing_template_decl;
8425 return ret;
8426}
8427
8428/* Since type attributes aren't mangled, we need to strip them from
8429 template type arguments. */
8430
8431tree
8432canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8433{
8434 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8435 return arg;
8436 bool removed_attributes = false;
8437 tree canon = strip_typedefs (arg, &removed_attributes);
8438 if (removed_attributes
8439 && (complain & tf_warning))
8440 warning (OPT_Wignored_attributes,
8441 "ignoring attributes on template argument %qT", arg);
8442 return canon;
8443}
8444
8445/* And from inside dependent non-type arguments like sizeof(Type). */
8446
8447static tree
8448canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8449{
8450 if (!arg || arg == error_mark_node)
8451 return arg;
8452 bool removed_attributes = false;
8453 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8454 if (removed_attributes
8455 && (complain & tf_warning))
8456 warning (OPT_Wignored_attributes,
8457 "ignoring attributes in template argument %qE", arg);
8458 return canon;
8459}
8460
8461/* A template declaration can be substituted for a constrained
8462 template template parameter only when the argument is no more
8463 constrained than the parameter. */
8464
8465static bool
8466is_compatible_template_arg (tree parm, tree arg, tree args)
8467{
8468 tree parm_cons = get_constraints (parm);
8469
8470 /* For now, allow constrained template template arguments
8471 and unconstrained template template parameters. */
8472 if (parm_cons == NULL_TREE)
8473 return true;
8474
8475 /* If the template parameter is constrained, we need to rewrite its
8476 constraints in terms of the ARG's template parameters. This ensures
8477 that all of the template parameter types will have the same depth.
8478
8479 Note that this is only valid when coerce_template_template_parm is
8480 true for the innermost template parameters of PARM and ARG. In other
8481 words, because coercion is successful, this conversion will be valid. */
8482 tree new_args = NULL_TREE;
8483 if (parm_cons)
8484 {
8485 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8486 new_args = template_parms_level_to_args (parms: aparms);
8487 new_args = add_to_template_args (args, extra_args: new_args);
8488 ++processing_template_decl;
8489 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8490 tf_none, NULL_TREE);
8491 --processing_template_decl;
8492 if (parm_cons == error_mark_node)
8493 return false;
8494 }
8495
8496 return weakly_subsumes (parm_cons, arg);
8497}
8498
8499// Convert a placeholder argument into a binding to the original
8500// parameter. The original parameter is saved as the TREE_TYPE of
8501// ARG.
8502static inline tree
8503convert_wildcard_argument (tree parm, tree arg)
8504{
8505 TREE_TYPE (arg) = parm;
8506 return arg;
8507}
8508
8509/* We can't fully resolve ARG given as a non-type template argument to TYPE,
8510 because one of them is dependent. But we need to represent the
8511 conversion for the benefit of cp_tree_equal. */
8512
8513static tree
8514maybe_convert_nontype_argument (tree type, tree arg, bool force)
8515{
8516 /* Auto parms get no conversion. */
8517 if (type_uses_auto (type))
8518 return arg;
8519 /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
8520 That would complicate other things, and it doesn't seem necessary. */
8521 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
8522 return arg;
8523 /* We don't need or want to add this conversion now if we're going to use the
8524 argument for deduction. */
8525 if (!value_dependent_expression_p (arg))
8526 force = false;
8527 else if (!force)
8528 return arg;
8529
8530 type = cv_unqualified (type);
8531 tree argtype = TREE_TYPE (arg);
8532 if (argtype && same_type_p (type, argtype))
8533 return arg;
8534
8535 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8536 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8537 IMPLICIT_CONV_EXPR_FORCED (arg) = force;
8538 return arg;
8539}
8540
8541/* Convert the indicated template ARG as necessary to match the
8542 indicated template PARM. Returns the converted ARG, or
8543 error_mark_node if the conversion was unsuccessful. Error and
8544 warning messages are issued under control of COMPLAIN. This
8545 conversion is for the Ith parameter in the parameter list. ARGS is
8546 the full set of template arguments deduced so far. */
8547
8548static tree
8549convert_template_argument (tree parm,
8550 tree arg,
8551 tree args,
8552 tsubst_flags_t complain,
8553 int i,
8554 tree in_decl)
8555{
8556 tree orig_arg;
8557 tree val;
8558 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8559
8560 if (parm == error_mark_node || error_operand_p (t: arg))
8561 return error_mark_node;
8562
8563 /* Trivially convert placeholders. */
8564 if (TREE_CODE (arg) == WILDCARD_DECL)
8565 return convert_wildcard_argument (parm, arg);
8566
8567 if (arg == any_targ_node)
8568 return arg;
8569
8570 if (TREE_CODE (arg) == TREE_LIST
8571 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8572 {
8573 /* The template argument was the name of some
8574 member function. That's usually
8575 invalid, but static members are OK. In any
8576 case, grab the underlying fields/functions
8577 and issue an error later if required. */
8578 TREE_TYPE (arg) = unknown_type_node;
8579 }
8580
8581 orig_arg = arg;
8582
8583 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8584 requires_type = (TREE_CODE (parm) == TYPE_DECL
8585 || requires_tmpl_type);
8586
8587 /* When determining whether an argument pack expansion is a template,
8588 look at the pattern. */
8589 if (PACK_EXPANSION_P (arg))
8590 arg = PACK_EXPANSION_PATTERN (arg);
8591
8592 /* Deal with an injected-class-name used as a template template arg. */
8593 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8594 {
8595 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8596 if (TREE_CODE (t) == TEMPLATE_DECL)
8597 {
8598 if (cxx_dialect >= cxx11)
8599 /* OK under DR 1004. */;
8600 else if (complain & tf_warning_or_error)
8601 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8602 " used as template template argument", TYPE_NAME (arg));
8603 else if (flag_pedantic_errors)
8604 t = arg;
8605
8606 arg = t;
8607 }
8608 }
8609
8610 is_tmpl_type =
8611 ((TREE_CODE (arg) == TEMPLATE_DECL
8612 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8613 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8614 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8615 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8616
8617 if (is_tmpl_type
8618 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8619 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8620 arg = TYPE_STUB_DECL (arg);
8621
8622 is_type = TYPE_P (arg) || is_tmpl_type;
8623
8624 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8625 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8626 {
8627 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8628 {
8629 if (complain & tf_error)
8630 error ("invalid use of destructor %qE as a type", orig_arg);
8631 return error_mark_node;
8632 }
8633
8634 permerror (input_location,
8635 "to refer to a type member of a template parameter, "
8636 "use %<typename %E%>", orig_arg);
8637
8638 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8639 TREE_OPERAND (arg, 1),
8640 typename_type,
8641 complain);
8642 arg = orig_arg;
8643 is_type = 1;
8644 }
8645 if (is_type != requires_type)
8646 {
8647 if (in_decl)
8648 {
8649 if (complain & tf_error)
8650 {
8651 error ("type/value mismatch at argument %d in template "
8652 "parameter list for %qD",
8653 i + 1, in_decl);
8654 if (is_type)
8655 {
8656 /* The template argument is a type, but we're expecting
8657 an expression. */
8658 inform (input_location,
8659 " expected a constant of type %qT, got %qT",
8660 TREE_TYPE (parm),
8661 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8662 /* [temp.arg]/2: "In a template-argument, an ambiguity
8663 between a type-id and an expression is resolved to a
8664 type-id, regardless of the form of the corresponding
8665 template-parameter." So give the user a clue. */
8666 if (TREE_CODE (arg) == FUNCTION_TYPE)
8667 inform (input_location, " ambiguous template argument "
8668 "for non-type template parameter is treated as "
8669 "function type");
8670 }
8671 else if (requires_tmpl_type)
8672 inform (input_location,
8673 " expected a class template, got %qE", orig_arg);
8674 else
8675 inform (input_location,
8676 " expected a type, got %qE", orig_arg);
8677 }
8678 }
8679 return error_mark_node;
8680 }
8681 if (is_tmpl_type ^ requires_tmpl_type)
8682 {
8683 if (in_decl && (complain & tf_error))
8684 {
8685 error ("type/value mismatch at argument %d in template "
8686 "parameter list for %qD",
8687 i + 1, in_decl);
8688 if (is_tmpl_type)
8689 inform (input_location,
8690 " expected a type, got %qT", DECL_NAME (arg));
8691 else
8692 inform (input_location,
8693 " expected a class template, got %qT", orig_arg);
8694 }
8695 return error_mark_node;
8696 }
8697
8698 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8699 /* We already did the appropriate conversion when packing args. */
8700 val = orig_arg;
8701 else if (is_type)
8702 {
8703 if (requires_tmpl_type)
8704 {
8705 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8706 /* The number of argument required is not known yet.
8707 Just accept it for now. */
8708 val = orig_arg;
8709 else
8710 {
8711 /* Strip alias templates that are equivalent to another
8712 template. */
8713 arg = get_underlying_template (tmpl: arg);
8714
8715 if (coerce_template_template_parms (parm_tmpl: parm, arg_tmpl: arg,
8716 complain, in_decl,
8717 outer_args: args))
8718 {
8719 val = arg;
8720
8721 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8722 TEMPLATE_DECL. */
8723 if (val != error_mark_node)
8724 {
8725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8726 val = TREE_TYPE (val);
8727 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8728 val = make_pack_expansion (arg: val, complain);
8729 }
8730 }
8731 else
8732 {
8733 if (in_decl && (complain & tf_error))
8734 {
8735 error ("type/value mismatch at argument %d in "
8736 "template parameter list for %qD",
8737 i + 1, in_decl);
8738 inform (input_location,
8739 " expected a template of type %qD, got %qT",
8740 parm, orig_arg);
8741 }
8742
8743 val = error_mark_node;
8744 }
8745
8746 // Check that the constraints are compatible before allowing the
8747 // substitution.
8748 if (val != error_mark_node)
8749 if (!is_compatible_template_arg (parm, arg, args))
8750 {
8751 if (in_decl && (complain & tf_error))
8752 {
8753 error ("constraint mismatch at argument %d in "
8754 "template parameter list for %qD",
8755 i + 1, in_decl);
8756 inform (input_location, " expected %qD but got %qD",
8757 parm, arg);
8758 }
8759 val = error_mark_node;
8760 }
8761 }
8762 }
8763 else
8764 val = orig_arg;
8765 /* We only form one instance of each template specialization.
8766 Therefore, if we use a non-canonical variant (i.e., a
8767 typedef), any future messages referring to the type will use
8768 the typedef, which is confusing if those future uses do not
8769 themselves also use the typedef. */
8770 if (TYPE_P (val))
8771 val = canonicalize_type_argument (arg: val, complain);
8772 }
8773 else
8774 {
8775 tree t = TREE_TYPE (parm);
8776
8777 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8778 > TMPL_ARGS_DEPTH (args))
8779 /* We don't have enough levels of args to do any substitution. This
8780 can happen in the context of -fnew-ttp-matching. */;
8781 else if (tree a = type_uses_auto (t))
8782 {
8783 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8784 LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
8785 if (t == error_mark_node)
8786 return error_mark_node;
8787 }
8788 else
8789 t = tsubst (t, args, complain, in_decl);
8790
8791 /* Perform array-to-pointer and function-to-pointer conversion
8792 as per [temp.param]/10. */
8793 t = type_decays_to (t);
8794
8795 if (invalid_nontype_parm_type_p (t, complain))
8796 return error_mark_node;
8797
8798 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8799 this non-type template parameter, as per [temp.param]/6. */
8800 t = cv_unqualified (t);
8801
8802 if (t != TREE_TYPE (parm))
8803 t = canonicalize_type_argument (arg: t, complain);
8804
8805 /* We need to handle arguments for alias or concept templates
8806 differently: we need to force building an IMPLICIT_CONV_EXPR, because
8807 these arguments are going to be substituted directly into the
8808 dependent type; they might not get another chance at
8809 convert_nontype_argument. But if the argument ends up here again for
8810 a template that isn't one of those, remove the conversion for
8811 consistency between naming the same dependent type directly or through
8812 an alias. */
8813 bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
8814 || concept_definition_p (t: in_decl));
8815 if (!force_conv
8816 && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
8817 && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
8818 && same_type_p (TREE_TYPE (orig_arg), t))
8819 orig_arg = TREE_OPERAND (orig_arg, 0);
8820
8821 if (!type_dependent_expression_p (orig_arg)
8822 && !uses_template_parms (t))
8823 /* We used to call digest_init here. However, digest_init
8824 will report errors, which we don't want when complain
8825 is zero. More importantly, digest_init will try too
8826 hard to convert things: for example, `0' should not be
8827 converted to pointer type at this point according to
8828 the standard. Accepting this is not merely an
8829 extension, since deciding whether or not these
8830 conversions can occur is part of determining which
8831 function template to call, or whether a given explicit
8832 argument specification is valid. */
8833 val = convert_nontype_argument (type: t, expr: orig_arg, complain);
8834 else
8835 {
8836 val = canonicalize_expr_argument (arg: orig_arg, complain);
8837 val = maybe_convert_nontype_argument (type: t, arg: val, force: force_conv);
8838 }
8839
8840 if (val == NULL_TREE)
8841 val = error_mark_node;
8842 else if (val == error_mark_node && (complain & tf_error))
8843 error_at (cp_expr_loc_or_input_loc (t: orig_arg),
8844 "could not convert template argument %qE from %qT to %qT",
8845 orig_arg, TREE_TYPE (orig_arg), t);
8846
8847 if (INDIRECT_REF_P (val))
8848 {
8849 /* Reject template arguments that are references to built-in
8850 functions with no library fallbacks. */
8851 const_tree inner = TREE_OPERAND (val, 0);
8852 const_tree innertype = TREE_TYPE (inner);
8853 if (innertype
8854 && TYPE_REF_P (innertype)
8855 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8856 && TREE_OPERAND_LENGTH (inner) > 0
8857 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8858 return error_mark_node;
8859 }
8860
8861 if (TREE_CODE (val) == SCOPE_REF)
8862 {
8863 /* Strip typedefs from the SCOPE_REF. */
8864 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8865 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8866 complain);
8867 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8868 QUALIFIED_NAME_IS_TEMPLATE (val));
8869 }
8870 }
8871
8872 return val;
8873}
8874
8875/* Coerces the remaining template arguments in INNER_ARGS (from
8876 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8877 Returns the coerced argument pack. PARM_IDX is the position of this
8878 parameter in the template parameter list. ARGS is the original
8879 template argument list. */
8880static tree
8881coerce_template_parameter_pack (tree parms,
8882 int parm_idx,
8883 tree args,
8884 tree inner_args,
8885 int arg_idx,
8886 tree new_args,
8887 int* lost,
8888 tree in_decl,
8889 tsubst_flags_t complain)
8890{
8891 tree parm = TREE_VEC_ELT (parms, parm_idx);
8892 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8893 tree packed_args;
8894 tree argument_pack;
8895 tree packed_parms = NULL_TREE;
8896
8897 if (arg_idx > nargs)
8898 arg_idx = nargs;
8899
8900 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8901 {
8902 /* When the template parameter is a non-type template parameter pack
8903 or template template parameter pack whose type or template
8904 parameters use parameter packs, we know exactly how many arguments
8905 we are looking for. Build a vector of the instantiated decls for
8906 these template parameters in PACKED_PARMS. */
8907 /* We can't use make_pack_expansion here because it would interpret a
8908 _DECL as a use rather than a declaration. */
8909 tree decl = TREE_VALUE (parm);
8910 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8911 PACK_EXPANSION_PATTERN (exp) = decl;
8912 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8913 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8914
8915 TREE_VEC_LENGTH (args)--;
8916 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8917 TREE_VEC_LENGTH (args)++;
8918
8919 if (packed_parms == error_mark_node)
8920 return error_mark_node;
8921
8922 /* If we're doing a partial instantiation of a member template,
8923 verify that all of the types used for the non-type
8924 template parameter pack are, in fact, valid for non-type
8925 template parameters. */
8926 if (arg_idx < nargs
8927 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8928 {
8929 int j, len = TREE_VEC_LENGTH (packed_parms);
8930 for (j = 0; j < len; ++j)
8931 {
8932 tree t = TREE_VEC_ELT (packed_parms, j);
8933 if (TREE_CODE (t) == PARM_DECL
8934 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8935 return error_mark_node;
8936 }
8937 /* We don't know how many args we have yet, just
8938 use the unconverted ones for now. */
8939 return NULL_TREE;
8940 }
8941
8942 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8943 }
8944 /* Check if we have a placeholder pack, which indicates we're
8945 in the context of a introduction list. In that case we want
8946 to match this pack to the single placeholder. */
8947 else if (arg_idx < nargs
8948 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8949 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8950 {
8951 nargs = arg_idx + 1;
8952 packed_args = make_tree_vec (1);
8953 }
8954 else
8955 packed_args = make_tree_vec (nargs - arg_idx);
8956
8957 /* Convert the remaining arguments, which will be a part of the
8958 parameter pack "parm". */
8959 int first_pack_arg = arg_idx;
8960 for (; arg_idx < nargs; ++arg_idx)
8961 {
8962 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8963 tree actual_parm = TREE_VALUE (parm);
8964 int pack_idx = arg_idx - first_pack_arg;
8965
8966 if (packed_parms)
8967 {
8968 /* Once we've packed as many args as we have types, stop. */
8969 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8970 break;
8971 else if (PACK_EXPANSION_P (arg))
8972 /* We don't know how many args we have yet, just
8973 use the unconverted ones for now. */
8974 return NULL_TREE;
8975 else
8976 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8977 }
8978
8979 if (arg == error_mark_node)
8980 {
8981 if (complain & tf_error)
8982 error ("template argument %d is invalid", arg_idx + 1);
8983 }
8984 else
8985 arg = convert_template_argument (parm: actual_parm,
8986 arg, args: new_args, complain, i: parm_idx,
8987 in_decl);
8988 if (arg == error_mark_node)
8989 (*lost)++;
8990 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8991 }
8992
8993 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8994 && TREE_VEC_LENGTH (packed_args) > 0)
8995 {
8996 if (complain & tf_error)
8997 error ("wrong number of template arguments (%d, should be %d)",
8998 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8999 return error_mark_node;
9000 }
9001
9002 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
9003 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
9004 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
9005 else
9006 {
9007 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
9008 TREE_CONSTANT (argument_pack) = 1;
9009 }
9010
9011 ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
9012 if (CHECKING_P)
9013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
9014 TREE_VEC_LENGTH (packed_args));
9015 return argument_pack;
9016}
9017
9018/* Returns the number of pack expansions in the template argument vector
9019 ARGS. */
9020
9021static int
9022pack_expansion_args_count (tree args)
9023{
9024 int i;
9025 int count = 0;
9026 if (args)
9027 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
9028 {
9029 tree elt = TREE_VEC_ELT (args, i);
9030 if (elt && PACK_EXPANSION_P (elt))
9031 ++count;
9032 }
9033 return count;
9034}
9035
9036/* Convert all template arguments to their appropriate types, and
9037 return a vector containing the innermost resulting template
9038 arguments. If any error occurs, return error_mark_node. Error and
9039 warning messages are issued under control of COMPLAIN.
9040
9041 If PARMS represents all template parameters levels, this function
9042 returns a vector of vectors representing all the resulting argument
9043 levels. Note that in this case, only the innermost arguments are
9044 coerced because the outermost ones are supposed to have been coerced
9045 already. Otherwise, if PARMS represents only (the innermost) vector
9046 of parameters, this function returns a vector containing just the
9047 innermost resulting arguments.
9048
9049 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
9050 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
9051 arguments not specified in ARGS must have default arguments which
9052 we'll use to fill in ARGS. */
9053
9054tree
9055coerce_template_parms (tree parms,
9056 tree args,
9057 tree in_decl,
9058 tsubst_flags_t complain,
9059 bool require_all_args /* = true */)
9060{
9061 int nparms, nargs, parm_idx, arg_idx, lost = 0;
9062 tree orig_inner_args;
9063 tree inner_args;
9064
9065 /* When used as a boolean value, indicates whether this is a
9066 variadic template parameter list. Since it's an int, we can also
9067 subtract it from nparms to get the number of non-variadic
9068 parameters. */
9069 int variadic_p = 0;
9070 int variadic_args_p = 0;
9071 int post_variadic_parms = 0;
9072
9073 /* Adjustment to nparms for fixed parameter packs. */
9074 int fixed_pack_adjust = 0;
9075 int fixed_packs = 0;
9076 int missing = 0;
9077
9078 /* Likewise for parameters with default arguments. */
9079 int default_p = 0;
9080
9081 if (args == error_mark_node)
9082 return error_mark_node;
9083
9084 bool return_full_args = false;
9085 if (TREE_CODE (parms) == TREE_LIST)
9086 {
9087 if (TMPL_PARMS_DEPTH (parms) > 1)
9088 {
9089 gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
9090 return_full_args = true;
9091 }
9092 parms = INNERMOST_TEMPLATE_PARMS (parms);
9093 }
9094
9095 nparms = TREE_VEC_LENGTH (parms);
9096
9097 /* Determine if there are any parameter packs or default arguments. */
9098 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
9099 {
9100 tree parm = TREE_VEC_ELT (parms, parm_idx);
9101 if (variadic_p)
9102 ++post_variadic_parms;
9103 if (template_parameter_pack_p (TREE_VALUE (parm)))
9104 ++variadic_p;
9105 if (TREE_PURPOSE (parm))
9106 ++default_p;
9107 }
9108
9109 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
9110 /* If there are no parameters that follow a parameter pack, we need to
9111 expand any argument packs so that we can deduce a parameter pack from
9112 some non-packed args followed by an argument pack, as in variadic85.C.
9113 If there are such parameters, we need to leave argument packs intact
9114 so the arguments are assigned properly. This can happen when dealing
9115 with a nested class inside a partial specialization of a class
9116 template, as in variadic92.C, or when deducing a template parameter pack
9117 from a sub-declarator, as in variadic114.C. */
9118 if (!post_variadic_parms)
9119 inner_args = expand_template_argument_pack (args: inner_args);
9120
9121 /* Count any pack expansion args. */
9122 variadic_args_p = pack_expansion_args_count (args: inner_args);
9123
9124 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
9125 if ((nargs - variadic_args_p > nparms && !variadic_p)
9126 || (nargs < nparms - variadic_p
9127 && require_all_args
9128 && !variadic_args_p
9129 && (TREE_VEC_ELT (parms, nargs) != error_mark_node
9130 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
9131 {
9132 bad_nargs:
9133 if (complain & tf_error)
9134 {
9135 if (variadic_p || default_p)
9136 {
9137 nparms -= variadic_p + default_p;
9138 error ("wrong number of template arguments "
9139 "(%d, should be at least %d)", nargs, nparms);
9140 }
9141 else
9142 error ("wrong number of template arguments "
9143 "(%d, should be %d)", nargs, nparms);
9144
9145 if (in_decl)
9146 inform (DECL_SOURCE_LOCATION (in_decl),
9147 "provided for %qD", in_decl);
9148 }
9149
9150 return error_mark_node;
9151 }
9152 /* We can't pass a pack expansion to a non-pack parameter of an alias
9153 template (DR 1430). */
9154 else if (in_decl
9155 && (DECL_ALIAS_TEMPLATE_P (in_decl)
9156 || concept_definition_p (t: in_decl))
9157 && variadic_args_p
9158 && nargs - variadic_args_p < nparms - variadic_p)
9159 {
9160 if (complain & tf_error)
9161 {
9162 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
9163 {
9164 tree arg = TREE_VEC_ELT (inner_args, i);
9165 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
9166
9167 if (PACK_EXPANSION_P (arg)
9168 && !template_parameter_pack_p (parm))
9169 {
9170 if (DECL_ALIAS_TEMPLATE_P (in_decl))
9171 error_at (location_of (arg),
9172 "pack expansion argument for non-pack parameter "
9173 "%qD of alias template %qD", parm, in_decl);
9174 else
9175 error_at (location_of (arg),
9176 "pack expansion argument for non-pack parameter "
9177 "%qD of concept %qD", parm, in_decl);
9178 inform (DECL_SOURCE_LOCATION (parm), "declared here");
9179 goto found;
9180 }
9181 }
9182 gcc_unreachable ();
9183 found:;
9184 }
9185 return error_mark_node;
9186 }
9187
9188 /* We need to evaluate the template arguments, even though this
9189 template-id may be nested within a "sizeof". */
9190 cp_evaluated ev;
9191
9192 tree new_args = add_outermost_template_args (args, extra_args: make_tree_vec (nparms));
9193 tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
9194 int pack_adjust = 0;
9195 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
9196 {
9197 tree arg;
9198 tree parm;
9199
9200 /* Get the Ith template parameter. */
9201 parm = TREE_VEC_ELT (parms, parm_idx);
9202
9203 if (parm == error_mark_node)
9204 {
9205 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
9206 continue;
9207 }
9208
9209 /* Calculate the next argument. */
9210 if (arg_idx < nargs)
9211 arg = TREE_VEC_ELT (inner_args, arg_idx);
9212 else
9213 arg = NULL_TREE;
9214
9215 if (template_parameter_pack_p (TREE_VALUE (parm))
9216 && (arg || require_all_args || !(complain & tf_partial))
9217 && !(arg && ARGUMENT_PACK_P (arg)))
9218 {
9219 /* Some arguments will be placed in the
9220 template parameter pack PARM. */
9221 arg = coerce_template_parameter_pack (parms, parm_idx, args,
9222 inner_args, arg_idx,
9223 new_args, lost: &lost,
9224 in_decl, complain);
9225
9226 if (arg == NULL_TREE)
9227 {
9228 /* We don't know how many args we have yet, just use the
9229 unconverted (and still packed) ones for now. */
9230 new_inner_args = orig_inner_args;
9231 arg_idx = nargs;
9232 break;
9233 }
9234
9235 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
9236
9237 /* Store this argument. */
9238 if (arg == error_mark_node)
9239 {
9240 lost++;
9241 /* We are done with all of the arguments. */
9242 arg_idx = nargs;
9243 break;
9244 }
9245 else
9246 {
9247 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
9248 arg_idx += pack_adjust;
9249 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
9250 {
9251 ++fixed_packs;
9252 fixed_pack_adjust += pack_adjust;
9253 }
9254 }
9255
9256 continue;
9257 }
9258 else if (arg)
9259 {
9260 if (PACK_EXPANSION_P (arg))
9261 {
9262 /* "If every valid specialization of a variadic template
9263 requires an empty template parameter pack, the template is
9264 ill-formed, no diagnostic required." So check that the
9265 pattern works with this parameter. */
9266 tree pattern = PACK_EXPANSION_PATTERN (arg);
9267 tree conv = convert_template_argument (TREE_VALUE (parm),
9268 arg: pattern, args: new_args,
9269 complain, i: parm_idx,
9270 in_decl);
9271 if (conv == error_mark_node)
9272 {
9273 if (complain & tf_error)
9274 inform (input_location, "so any instantiation with a "
9275 "non-empty parameter pack would be ill-formed");
9276 ++lost;
9277 }
9278 else if (TYPE_P (conv) && !TYPE_P (pattern))
9279 /* Recover from missing typename. */
9280 TREE_VEC_ELT (inner_args, arg_idx)
9281 = make_pack_expansion (arg: conv, complain);
9282
9283 /* We don't know how many args we have yet, just
9284 use the unconverted ones for now. */
9285 new_inner_args = inner_args;
9286 arg_idx = nargs;
9287 break;
9288 }
9289 }
9290 else if (require_all_args)
9291 {
9292 /* There must be a default arg in this case. */
9293 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9294 complain, in_decl);
9295 /* The position of the first default template argument,
9296 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9297 Record that. */
9298 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9299 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9300 arg_idx - pack_adjust);
9301 }
9302 else
9303 break;
9304
9305 if (arg == error_mark_node)
9306 {
9307 if (complain & tf_error)
9308 error ("template argument %d is invalid", arg_idx + 1);
9309 }
9310 else if (!arg)
9311 {
9312 /* This can occur if there was an error in the template
9313 parameter list itself (which we would already have
9314 reported) that we are trying to recover from, e.g., a class
9315 template with a parameter list such as
9316 template<typename..., typename> (cpp0x/variadic150.C). */
9317 ++lost;
9318
9319 /* This can also happen with a fixed parameter pack (71834). */
9320 if (arg_idx >= nargs)
9321 ++missing;
9322 }
9323 else
9324 arg = convert_template_argument (TREE_VALUE (parm),
9325 arg, args: new_args, complain,
9326 i: parm_idx, in_decl);
9327
9328 if (arg == error_mark_node)
9329 lost++;
9330
9331 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9332 }
9333
9334 if (missing || arg_idx < nargs - variadic_args_p)
9335 {
9336 /* If we had fixed parameter packs, we didn't know how many arguments we
9337 actually needed earlier; now we do. */
9338 nparms += fixed_pack_adjust;
9339 variadic_p -= fixed_packs;
9340 goto bad_nargs;
9341 }
9342
9343 if (arg_idx < nargs)
9344 {
9345 /* We had some pack expansion arguments that will only work if the packs
9346 are empty, but wait until instantiation time to complain.
9347 See variadic-ttp3.C. */
9348
9349 /* Except that we can't provide empty packs to alias templates or
9350 concepts when there are no corresponding parameters. Basically,
9351 we can get here with this:
9352
9353 template<typename T> concept C = true;
9354
9355 template<typename... Args>
9356 requires C<Args...>
9357 void f();
9358
9359 When parsing C<Args...>, we try to form a concept check of
9360 C<?, Args...>. Without the extra check for substituting an empty
9361 pack past the last parameter, we can accept the check as valid.
9362
9363 FIXME: This may be valid for alias templates (but I doubt it).
9364
9365 FIXME: The error could be better also. */
9366 if (in_decl && concept_definition_p (t: in_decl))
9367 {
9368 if (complain & tf_error)
9369 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9370 "too many arguments");
9371 return error_mark_node;
9372 }
9373
9374 int len = nparms + (nargs - arg_idx);
9375 tree args = make_tree_vec (len);
9376 int i = 0;
9377 for (; i < nparms; ++i)
9378 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9379 for (; i < len; ++i, ++arg_idx)
9380 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9381 arg_idx - pack_adjust);
9382 new_inner_args = args;
9383 }
9384
9385 if (lost)
9386 {
9387 gcc_assert (!(complain & tf_error) || seen_error ());
9388 return error_mark_node;
9389 }
9390
9391 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9392 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9393 TREE_VEC_LENGTH (new_inner_args));
9394
9395 return return_full_args ? new_args : new_inner_args;
9396}
9397
9398/* Returns true if T is a wrapper to make a C++20 template parameter
9399 object const. */
9400
9401static bool
9402class_nttp_const_wrapper_p (tree t)
9403{
9404 if (cxx_dialect < cxx20)
9405 return false;
9406 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9407 && CP_TYPE_CONST_P (TREE_TYPE (t))
9408 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9409}
9410
9411/* Returns 1 if template args OT and NT are equivalent. */
9412
9413int
9414template_args_equal (tree ot, tree nt)
9415{
9416 if (nt == ot)
9417 return 1;
9418 if (nt == NULL_TREE || ot == NULL_TREE)
9419 return false;
9420 if (nt == any_targ_node || ot == any_targ_node)
9421 return true;
9422
9423 if (class_nttp_const_wrapper_p (t: nt))
9424 nt = TREE_OPERAND (nt, 0);
9425 if (class_nttp_const_wrapper_p (t: ot))
9426 ot = TREE_OPERAND (ot, 0);
9427
9428 /* DR 1558: Don't treat an alias template specialization with dependent
9429 arguments as equivalent to its underlying type when used as a template
9430 argument; we need them to be distinct so that we substitute into the
9431 specialization arguments at instantiation time. And aliases can't be
9432 equivalent without being ==, so we don't need to look any deeper.
9433
9434 During partial ordering, however, we need to treat them normally so we can
9435 order uses of the same alias with different cv-qualification (79960). */
9436 auto cso = make_temp_override (var&: comparing_dependent_aliases);
9437 if (!comparing_for_partial_ordering)
9438 ++comparing_dependent_aliases;
9439
9440 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9441 /* For member templates */
9442 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9443 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9444 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9445 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9446 PACK_EXPANSION_PATTERN (nt))
9447 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9448 PACK_EXPANSION_EXTRA_ARGS (nt)));
9449 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9450 return cp_tree_equal (ot, nt);
9451 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9452 gcc_unreachable ();
9453 else if (TYPE_P (nt) || TYPE_P (ot))
9454 {
9455 if (!(TYPE_P (nt) && TYPE_P (ot)))
9456 return false;
9457 return same_type_p (ot, nt);
9458 }
9459 else
9460 {
9461 /* Try to treat a template non-type argument that has been converted
9462 to the parameter type as equivalent to one that hasn't yet. */
9463 for (enum tree_code code1 = TREE_CODE (ot);
9464 CONVERT_EXPR_CODE_P (code1)
9465 || code1 == NON_LVALUE_EXPR;
9466 code1 = TREE_CODE (ot))
9467 ot = TREE_OPERAND (ot, 0);
9468
9469 for (enum tree_code code2 = TREE_CODE (nt);
9470 CONVERT_EXPR_CODE_P (code2)
9471 || code2 == NON_LVALUE_EXPR;
9472 code2 = TREE_CODE (nt))
9473 nt = TREE_OPERAND (nt, 0);
9474
9475 return cp_tree_equal (ot, nt);
9476 }
9477}
9478
9479/* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9480 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9481 NEWARG_PTR with the offending arguments if they are non-NULL. */
9482
9483bool
9484comp_template_args (tree oldargs, tree newargs,
9485 tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
9486{
9487 if (oldargs == newargs)
9488 return true;
9489
9490 if (!oldargs || !newargs)
9491 return false;
9492
9493 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9494 return false;
9495
9496 for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9497 {
9498 tree nt = TREE_VEC_ELT (newargs, i);
9499 tree ot = TREE_VEC_ELT (oldargs, i);
9500
9501 if (! template_args_equal (ot, nt))
9502 {
9503 if (oldarg_ptr != NULL)
9504 *oldarg_ptr = ot;
9505 if (newarg_ptr != NULL)
9506 *newarg_ptr = nt;
9507 return false;
9508 }
9509 }
9510 return true;
9511}
9512
9513static bool
9514comp_template_args_porder (tree oargs, tree nargs)
9515{
9516 ++comparing_for_partial_ordering;
9517 bool equal = comp_template_args (oldargs: oargs, newargs: nargs);
9518 --comparing_for_partial_ordering;
9519 return equal;
9520}
9521
9522/* Implement a freelist interface for objects of type T.
9523
9524 Head is a separate object, rather than a regular member, so that we
9525 can define it as a GTY deletable pointer, which is highly
9526 desirable. A data member could be declared that way, but then the
9527 containing object would implicitly get GTY((user)), which would
9528 prevent us from instantiating freelists as global objects.
9529 Although this way we can create freelist global objects, they're
9530 such thin wrappers that instantiating temporaries at every use
9531 loses nothing and saves permanent storage for the freelist object.
9532
9533 Member functions next, anew, poison and reinit have default
9534 implementations that work for most of the types we're interested
9535 in, but if they don't work for some type, they should be explicitly
9536 specialized. See the comments before them for requirements, and
9537 the example specializations for the tree_list_freelist. */
9538template <typename T>
9539class freelist
9540{
9541 /* Return the next object in a chain. We could just do type
9542 punning, but if we access the object with its underlying type, we
9543 avoid strict-aliasing trouble. This needs only work between
9544 poison and reinit. */
9545 static T *&next (T *obj) { return obj->next; }
9546
9547 /* Return a newly allocated, uninitialized or minimally-initialized
9548 object of type T. Any initialization performed by anew should
9549 either remain across the life of the object and the execution of
9550 poison, or be redone by reinit. */
9551 static T *anew () { return ggc_alloc<T> (); }
9552
9553 /* Optionally scribble all over the bits holding the object, so that
9554 they become (mostly?) uninitialized memory. This is called while
9555 preparing to make the object part of the free list. */
9556 static void poison (T *obj) {
9557 T *p ATTRIBUTE_UNUSED = obj;
9558 T **q ATTRIBUTE_UNUSED = &next (obj);
9559
9560#ifdef ENABLE_GC_CHECKING
9561 /* Poison the data, to indicate the data is garbage. */
9562 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9563 memset (p, 0xa5, sizeof (*p));
9564#endif
9565 /* Let valgrind know the object is free. */
9566 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9567
9568 /* Let valgrind know the next portion of the object is available,
9569 but uninitialized. */
9570 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9571 }
9572
9573 /* Bring an object that underwent at least one lifecycle after anew
9574 and before the most recent free and poison, back to a usable
9575 state, reinitializing whatever is needed for it to be
9576 functionally equivalent to an object just allocated and returned
9577 by anew. This may poison or clear the next field, used by
9578 freelist housekeeping after poison was called. */
9579 static void reinit (T *obj) {
9580 T **q ATTRIBUTE_UNUSED = &next (obj);
9581
9582#ifdef ENABLE_GC_CHECKING
9583 memset (q, 0xa5, sizeof (*q));
9584#endif
9585 /* Let valgrind know the entire object is available, but
9586 uninitialized. */
9587 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9588 }
9589
9590 /* Reference a GTY-deletable pointer that points to the first object
9591 in the free list proper. */
9592 T *&head;
9593public:
9594 /* Construct a freelist object chaining objects off of HEAD. */
9595 freelist (T *&head) : head(head) {}
9596
9597 /* Add OBJ to the free object list. The former head becomes OBJ's
9598 successor. */
9599 void free (T *obj)
9600 {
9601 poison (obj);
9602 next (obj) = head;
9603 head = obj;
9604 }
9605
9606 /* Take an object from the free list, if one is available, or
9607 allocate a new one. Objects taken from the free list should be
9608 regarded as filled with garbage, except for bits that are
9609 configured to be preserved across free and alloc. */
9610 T *alloc ()
9611 {
9612 if (head)
9613 {
9614 T *obj = head;
9615 head = next (obj: head);
9616 reinit (obj);
9617 return obj;
9618 }
9619 else
9620 return anew ();
9621 }
9622};
9623
9624/* Explicitly specialize the interfaces for freelist<tree_node>: we
9625 want to allocate a TREE_LIST using the usual interface, and ensure
9626 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9627 build_tree_list logic in reinit, so this could go out of sync. */
9628template <>
9629inline tree &
9630freelist<tree_node>::next (tree obj)
9631{
9632 return TREE_CHAIN (obj);
9633}
9634template <>
9635inline tree
9636freelist<tree_node>::anew ()
9637{
9638 return build_tree_list (NULL, NULL);
9639}
9640template <>
9641inline void
9642freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9643{
9644 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9645 tree p ATTRIBUTE_UNUSED = obj;
9646 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9647 tree *q ATTRIBUTE_UNUSED = &next (obj);
9648
9649#ifdef ENABLE_GC_CHECKING
9650 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9651
9652 /* Poison the data, to indicate the data is garbage. */
9653 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9654 memset (s: p, c: 0xa5, n: size);
9655#endif
9656 /* Let valgrind know the object is free. */
9657 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9658 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9659 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9660 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9661
9662#ifdef ENABLE_GC_CHECKING
9663 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9664 /* Keep TREE_CHAIN functional. */
9665 TREE_SET_CODE (obj, TREE_LIST);
9666#else
9667 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9668#endif
9669}
9670template <>
9671inline void
9672freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9673{
9674 tree_common *c ATTRIBUTE_UNUSED = &obj->common;
9675
9676#ifdef ENABLE_GC_CHECKING
9677 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9678 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9679 memset (s: obj, c: 0, n: sizeof (tree_list));
9680#endif
9681
9682 /* Let valgrind know the entire object is available, but
9683 uninitialized. */
9684 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9685
9686#ifdef ENABLE_GC_CHECKING
9687 TREE_SET_CODE (obj, TREE_LIST);
9688#else
9689 TREE_CHAIN (obj) = NULL_TREE;
9690 TREE_TYPE (obj) = NULL_TREE;
9691#endif
9692 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
9693}
9694
9695/* Point to the first object in the TREE_LIST freelist. */
9696static GTY((deletable)) tree tree_list_freelist_head;
9697/* Return the/an actual TREE_LIST freelist. */
9698static inline freelist<tree_node>
9699tree_list_freelist ()
9700{
9701 return tree_list_freelist_head;
9702}
9703
9704/* Point to the first object in the tinst_level freelist. */
9705static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9706/* Return the/an actual tinst_level freelist. */
9707static inline freelist<tinst_level>
9708tinst_level_freelist ()
9709{
9710 return tinst_level_freelist_head;
9711}
9712
9713/* Point to the first object in the pending_template freelist. */
9714static GTY((deletable)) pending_template *pending_template_freelist_head;
9715/* Return the/an actual pending_template freelist. */
9716static inline freelist<pending_template>
9717pending_template_freelist ()
9718{
9719 return pending_template_freelist_head;
9720}
9721
9722/* Build the TREE_LIST object out of a split list, store it
9723 permanently, and return it. */
9724tree
9725tinst_level::to_list ()
9726{
9727 gcc_assert (split_list_p ());
9728 tree ret = tree_list_freelist ().alloc ();
9729 TREE_PURPOSE (ret) = tldcl;
9730 TREE_VALUE (ret) = targs;
9731 tldcl = ret;
9732 targs = NULL;
9733 gcc_assert (tree_list_p ());
9734 return ret;
9735}
9736
9737const unsigned short tinst_level::refcount_infinity;
9738
9739/* Increment OBJ's refcount unless it is already infinite. */
9740static tinst_level *
9741inc_refcount_use (tinst_level *obj)
9742{
9743 if (obj && obj->refcount != tinst_level::refcount_infinity)
9744 ++obj->refcount;
9745 return obj;
9746}
9747
9748/* Release storage for OBJ and node, if it's a TREE_LIST. */
9749void
9750tinst_level::free (tinst_level *obj)
9751{
9752 if (obj->tree_list_p ())
9753 tree_list_freelist ().free (obj: obj->get_node ());
9754 tinst_level_freelist ().free (obj);
9755}
9756
9757/* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9758 OBJ's DECL and OBJ, and start over with the tinst_level object that
9759 used to be referenced by OBJ's NEXT. */
9760static void
9761dec_refcount_use (tinst_level *obj)
9762{
9763 while (obj
9764 && obj->refcount != tinst_level::refcount_infinity
9765 && !--obj->refcount)
9766 {
9767 tinst_level *next = obj->next;
9768 tinst_level::free (obj);
9769 obj = next;
9770 }
9771}
9772
9773/* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9774 and of the former PTR. Omitting the second argument is equivalent
9775 to passing (T*)NULL; this is allowed because passing the
9776 zero-valued integral constant NULL confuses type deduction and/or
9777 overload resolution. */
9778template <typename T>
9779static void
9780set_refcount_ptr (T *& ptr, T *obj = NULL)
9781{
9782 T *save = ptr;
9783 ptr = inc_refcount_use (obj);
9784 dec_refcount_use (save);
9785}
9786
9787static void
9788add_pending_template (tree d)
9789{
9790 tree ti = (TYPE_P (d)
9791 ? CLASSTYPE_TEMPLATE_INFO (d)
9792 : DECL_TEMPLATE_INFO (d));
9793 struct pending_template *pt;
9794 int level;
9795
9796 if (TI_PENDING_TEMPLATE_FLAG (ti))
9797 return;
9798
9799 /* We are called both from instantiate_decl, where we've already had a
9800 tinst_level pushed, and instantiate_template, where we haven't.
9801 Compensate. */
9802 gcc_assert (TREE_CODE (d) != TREE_LIST);
9803 level = !current_tinst_level
9804 || current_tinst_level->maybe_get_node () != d;
9805
9806 if (level)
9807 push_tinst_level (d);
9808
9809 pt = pending_template_freelist ().alloc ();
9810 pt->next = NULL;
9811 pt->tinst = NULL;
9812 set_refcount_ptr (ptr&: pt->tinst, obj: current_tinst_level);
9813 if (last_pending_template)
9814 last_pending_template->next = pt;
9815 else
9816 pending_templates = pt;
9817
9818 last_pending_template = pt;
9819
9820 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9821
9822 if (level)
9823 pop_tinst_level ();
9824}
9825
9826
9827/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9828 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9829 documentation for TEMPLATE_ID_EXPR. */
9830
9831tree
9832lookup_template_function (tree fns, tree arglist)
9833{
9834 if (fns == error_mark_node || arglist == error_mark_node)
9835 return error_mark_node;
9836
9837 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9838
9839 if (!is_overloaded_fn (fns) && !identifier_p (t: fns))
9840 {
9841 error ("%q#D is not a function template", fns);
9842 return error_mark_node;
9843 }
9844
9845 if (BASELINK_P (fns))
9846 {
9847 fns = copy_node (fns);
9848 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9849 unknown_type_node,
9850 BASELINK_FUNCTIONS (fns),
9851 arglist);
9852 return fns;
9853 }
9854
9855 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9856}
9857
9858/* Within the scope of a template class S<T>, the name S gets bound
9859 (in build_self_reference) to a TYPE_DECL for the class, not a
9860 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9861 or one of its enclosing classes, and that type is a template,
9862 return the associated TEMPLATE_DECL. Otherwise, the original
9863 DECL is returned.
9864
9865 Also handle the case when DECL is a TREE_LIST of ambiguous
9866 injected-class-names from different bases. */
9867
9868tree
9869maybe_get_template_decl_from_type_decl (tree decl)
9870{
9871 if (decl == NULL_TREE)
9872 return decl;
9873
9874 /* DR 176: A lookup that finds an injected-class-name (10.2
9875 [class.member.lookup]) can result in an ambiguity in certain cases
9876 (for example, if it is found in more than one base class). If all of
9877 the injected-class-names that are found refer to specializations of
9878 the same class template, and if the name is followed by a
9879 template-argument-list, the reference refers to the class template
9880 itself and not a specialization thereof, and is not ambiguous. */
9881 if (TREE_CODE (decl) == TREE_LIST)
9882 {
9883 tree t, tmpl = NULL_TREE;
9884 for (t = decl; t; t = TREE_CHAIN (t))
9885 {
9886 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9887 if (!tmpl)
9888 tmpl = elt;
9889 else if (tmpl != elt)
9890 break;
9891 }
9892 if (tmpl && t == NULL_TREE)
9893 return tmpl;
9894 else
9895 return decl;
9896 }
9897
9898 return (decl != NULL_TREE
9899 && DECL_SELF_REFERENCE_P (decl)
9900 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9901 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9902}
9903
9904/* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9905 parameters, find the desired type.
9906
9907 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9908
9909 IN_DECL, if non-NULL, is the template declaration we are trying to
9910 instantiate.
9911
9912 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9913 the class we are looking up.
9914
9915 Issue error and warning messages under control of COMPLAIN.
9916
9917 If the template class is really a local class in a template
9918 function, then the FUNCTION_CONTEXT is the function in which it is
9919 being instantiated.
9920
9921 ??? Note that this function is currently called *twice* for each
9922 template-id: the first time from the parser, while creating the
9923 incomplete type (finish_template_type), and the second type during the
9924 real instantiation (instantiate_template_class). This is surely something
9925 that we want to avoid. It also causes some problems with argument
9926 coercion (see convert_nontype_argument for more information on this). */
9927
9928tree
9929lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9930 int entering_scope, tsubst_flags_t complain)
9931{
9932 auto_timevar tv (TV_TEMPLATE_INST);
9933
9934 tree templ = NULL_TREE, parmlist;
9935 tree t;
9936 spec_entry **slot;
9937 spec_entry *entry;
9938 spec_entry elt;
9939 hashval_t hash;
9940
9941 if (identifier_p (t: d1))
9942 {
9943 tree value = innermost_non_namespace_value (d1);
9944 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9945 templ = value;
9946 else
9947 {
9948 if (context)
9949 push_decl_namespace (context);
9950 templ = lookup_name (name: d1);
9951 templ = maybe_get_template_decl_from_type_decl (decl: templ);
9952 if (context)
9953 pop_decl_namespace ();
9954 }
9955 }
9956 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9957 {
9958 tree type = TREE_TYPE (d1);
9959
9960 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9961 an implicit typename for the second A. Deal with it. */
9962 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9963 type = TREE_TYPE (type);
9964
9965 if (CLASSTYPE_TEMPLATE_INFO (type))
9966 {
9967 templ = CLASSTYPE_TI_TEMPLATE (type);
9968 d1 = DECL_NAME (templ);
9969 }
9970 }
9971 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9972 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9973 {
9974 templ = TYPE_TI_TEMPLATE (d1);
9975 d1 = DECL_NAME (templ);
9976 }
9977 else if (DECL_TYPE_TEMPLATE_P (d1))
9978 {
9979 templ = d1;
9980 d1 = DECL_NAME (templ);
9981 }
9982 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9983 {
9984 templ = d1;
9985 d1 = DECL_NAME (templ);
9986 }
9987
9988 /* Issue an error message if we didn't find a template. */
9989 if (! templ)
9990 {
9991 if (complain & tf_error)
9992 error ("%qT is not a template", d1);
9993 return error_mark_node;
9994 }
9995
9996 if (TREE_CODE (templ) != TEMPLATE_DECL
9997 /* Make sure it's a user visible template, if it was named by
9998 the user. */
9999 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
10000 && !PRIMARY_TEMPLATE_P (templ)))
10001 {
10002 if (complain & tf_error)
10003 {
10004 error ("non-template type %qT used as a template", d1);
10005 if (in_decl)
10006 error ("for template declaration %q+D", in_decl);
10007 }
10008 return error_mark_node;
10009 }
10010
10011 complain &= ~tf_user;
10012
10013 /* An alias that just changes the name of a template is equivalent to the
10014 other template, so if any of the arguments are pack expansions, strip
10015 the alias to avoid problems with a pack expansion passed to a non-pack
10016 alias template parameter (DR 1430). */
10017 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
10018 templ = get_underlying_template (tmpl: templ);
10019
10020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10021 {
10022 tree parm;
10023 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
10024 if (arglist2 == error_mark_node
10025 || (!uses_template_parms (arglist2)
10026 && check_instantiated_args (templ, arglist2, complain)))
10027 return error_mark_node;
10028
10029 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
10030 return parm;
10031 }
10032 else
10033 {
10034 tree template_type = TREE_TYPE (templ);
10035 tree gen_tmpl;
10036 tree type_decl;
10037 tree found = NULL_TREE;
10038 int arg_depth;
10039 int parm_depth;
10040 int is_dependent_type;
10041 int use_partial_inst_tmpl = false;
10042
10043 if (template_type == error_mark_node)
10044 /* An error occurred while building the template TEMPL, and a
10045 diagnostic has most certainly been emitted for that
10046 already. Let's propagate that error. */
10047 return error_mark_node;
10048
10049 gen_tmpl = most_general_template (templ);
10050 if (modules_p ())
10051 lazy_load_pendings (decl: gen_tmpl);
10052
10053 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
10054 parm_depth = TMPL_PARMS_DEPTH (parmlist);
10055 arg_depth = TMPL_ARGS_DEPTH (arglist);
10056
10057 if (arg_depth == 1 && parm_depth > 1)
10058 {
10059 /* We've been given an incomplete set of template arguments.
10060 For example, given:
10061
10062 template <class T> struct S1 {
10063 template <class U> struct S2 {};
10064 template <class U> struct S2<U*> {};
10065 };
10066
10067 we will be called with an ARGLIST of `U*', but the
10068 TEMPLATE will be `template <class T> template
10069 <class U> struct S1<T>::S2'. We must fill in the missing
10070 arguments. */
10071 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
10072 arglist = add_outermost_template_args (TI_ARGS (ti), extra_args: arglist);
10073 arg_depth = TMPL_ARGS_DEPTH (arglist);
10074 }
10075
10076 /* Now we should have enough arguments. */
10077 gcc_assert (parm_depth == arg_depth);
10078
10079 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10080 {
10081 /* The user referred to a specialization of an alias
10082 template represented by GEN_TMPL.
10083
10084 [temp.alias]/2 says:
10085
10086 When a template-id refers to the specialization of an
10087 alias template, it is equivalent to the associated
10088 type obtained by substitution of its
10089 template-arguments for the template-parameters in the
10090 type-id of the alias template. */
10091
10092 t = instantiate_alias_template (gen_tmpl, arglist, complain);
10093 /* Note that the call above (by indirectly calling
10094 register_specialization in tsubst_decl) registers the
10095 TYPE_DECL representing the specialization of the alias
10096 template. So next time someone substitutes ARGLIST for
10097 the template parms into the alias template (GEN_TMPL),
10098 she'll get that TYPE_DECL back. */
10099
10100 if (t == error_mark_node)
10101 return error_mark_node;
10102 return TREE_TYPE (t);
10103 }
10104
10105 /* From here on, we're only interested in the most general
10106 template. */
10107
10108 /* Shortcut looking up the current class scope again. */
10109 for (tree cur = current_nonlambda_class_type ();
10110 cur != NULL_TREE;
10111 cur = get_containing_scope (cur))
10112 {
10113 if (!CLASS_TYPE_P (cur))
10114 continue;
10115
10116 tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
10117 if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
10118 break;
10119
10120 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
10121 && comp_template_args (oldargs: arglist, TI_ARGS (ti)))
10122 return cur;
10123 }
10124
10125 /* Calculate the BOUND_ARGS. These will be the args that are
10126 actually tsubst'd into the definition to create the
10127 instantiation. */
10128 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10129 arglist = coerce_template_parms (parms: parmlist, args: arglist, in_decl: gen_tmpl, complain);
10130
10131 if (arglist == error_mark_node)
10132 /* We were unable to bind the arguments. */
10133 return error_mark_node;
10134
10135 /* In the scope of a template class, explicit references to the
10136 template class refer to the type of the template, not any
10137 instantiation of it. For example, in:
10138
10139 template <class T> class C { void f(C<T>); }
10140
10141 the `C<T>' is just the same as `C'. Outside of the
10142 class, however, such a reference is an instantiation. */
10143 if (entering_scope
10144 || !PRIMARY_TEMPLATE_P (gen_tmpl)
10145 || currently_open_class (template_type))
10146 {
10147 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
10148
10149 if (tinfo && comp_template_args (TI_ARGS (tinfo), newargs: arglist))
10150 return template_type;
10151 }
10152
10153 /* If we already have this specialization, return it. */
10154 elt.tmpl = gen_tmpl;
10155 elt.args = arglist;
10156 elt.spec = NULL_TREE;
10157 hash = spec_hasher::hash (e: &elt);
10158 entry = type_specializations->find_with_hash (comparable: &elt, hash);
10159
10160 if (entry)
10161 return entry->spec;
10162
10163 /* If the template's constraints are not satisfied,
10164 then we cannot form a valid type.
10165
10166 Note that the check is deferred until after the hash
10167 lookup. This prevents redundant checks on previously
10168 instantiated specializations. */
10169 if (flag_concepts
10170 && !constraints_satisfied_p (gen_tmpl, arglist))
10171 {
10172 if (complain & tf_error)
10173 {
10174 auto_diagnostic_group d;
10175 error ("template constraint failure for %qD", gen_tmpl);
10176 diagnose_constraints (input_location, gen_tmpl, arglist);
10177 }
10178 return error_mark_node;
10179 }
10180
10181 is_dependent_type = uses_template_parms (arglist);
10182
10183 /* If the deduced arguments are invalid, then the binding
10184 failed. */
10185 if (!is_dependent_type
10186 && check_instantiated_args (gen_tmpl,
10187 INNERMOST_TEMPLATE_ARGS (arglist),
10188 complain))
10189 return error_mark_node;
10190
10191 if (!is_dependent_type
10192 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10193 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
10194 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
10195 /* This occurs when the user has tried to define a tagged type
10196 in a scope that forbids it. We emitted an error during the
10197 parse. We didn't complete the bail out then, so here we
10198 are. */
10199 return error_mark_node;
10200
10201 context = DECL_CONTEXT (gen_tmpl);
10202 if (context && TYPE_P (context))
10203 {
10204 if (!uses_template_parms (DECL_CONTEXT (templ)))
10205 /* If the context of the partially instantiated template is
10206 already non-dependent, then we might as well use it. */
10207 context = DECL_CONTEXT (templ);
10208 else
10209 {
10210 context = tsubst_aggr_type (context, arglist,
10211 complain, in_decl, true);
10212 /* Try completing the enclosing context if it's not already so. */
10213 if (context != error_mark_node
10214 && !COMPLETE_TYPE_P (context))
10215 {
10216 context = complete_type (context);
10217 if (COMPLETE_TYPE_P (context))
10218 {
10219 /* Completion could have caused us to register the desired
10220 specialization already, so check the table again. */
10221 entry = type_specializations->find_with_hash (comparable: &elt, hash);
10222 if (entry)
10223 return entry->spec;
10224 }
10225 }
10226 }
10227 }
10228 else
10229 context = tsubst (context, arglist, complain, in_decl);
10230
10231 if (context == error_mark_node)
10232 return error_mark_node;
10233
10234 if (!context)
10235 context = global_namespace;
10236
10237 /* Create the type. */
10238 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10239 {
10240 if (!is_dependent_type)
10241 {
10242 set_current_access_from_decl (TYPE_NAME (template_type));
10243 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10244 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10245 arglist, complain, in_decl),
10246 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10247 arglist, complain, in_decl),
10248 SCOPED_ENUM_P (template_type), NULL);
10249
10250 if (t == error_mark_node)
10251 return t;
10252 }
10253 else
10254 {
10255 /* We don't want to call start_enum for this type, since
10256 the values for the enumeration constants may involve
10257 template parameters. And, no one should be interested
10258 in the enumeration constants for such a type. */
10259 t = cxx_make_type (ENUMERAL_TYPE);
10260 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10261 }
10262 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10263 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10264 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10265 }
10266 else if (CLASS_TYPE_P (template_type))
10267 {
10268 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10269 instantiated here. */
10270 gcc_assert (!LAMBDA_TYPE_P (template_type));
10271
10272 t = make_class_type (TREE_CODE (template_type));
10273 CLASSTYPE_DECLARED_CLASS (t)
10274 = CLASSTYPE_DECLARED_CLASS (template_type);
10275 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10276
10277 /* A local class. Make sure the decl gets registered properly. */
10278 if (context == current_function_decl)
10279 if (pushtag (DECL_NAME (gen_tmpl), t)
10280 == error_mark_node)
10281 return error_mark_node;
10282
10283 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), newargs: arglist))
10284 /* This instantiation is another name for the primary
10285 template type. Set the TYPE_CANONICAL field
10286 appropriately. */
10287 TYPE_CANONICAL (t) = template_type;
10288 else if (any_template_arguments_need_structural_equality_p (arglist))
10289 SET_TYPE_STRUCTURAL_EQUALITY (t);
10290 }
10291 else
10292 gcc_unreachable ();
10293
10294 /* If we called start_enum or pushtag above, this information
10295 will already be set up. */
10296 type_decl = TYPE_NAME (t);
10297 if (!type_decl)
10298 {
10299 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10300
10301 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10302 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10303 DECL_SOURCE_LOCATION (type_decl)
10304 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10305 }
10306
10307 set_instantiating_module (type_decl);
10308 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10309 of export flag. We want to propagate this because it might
10310 be a friend declaration that pushes a new hidden binding. */
10311 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10312
10313 if (CLASS_TYPE_P (template_type))
10314 {
10315 TREE_PRIVATE (type_decl)
10316 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10317 TREE_PROTECTED (type_decl)
10318 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10319 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10320 {
10321 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10322 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10323 }
10324 }
10325
10326 if (OVERLOAD_TYPE_P (t))
10327 {
10328 static const char *tags[] = {"abi_tag", "may_alias"};
10329
10330 for (unsigned ix = 0; ix != 2; ix++)
10331 {
10332 tree attributes
10333 = lookup_attribute (attr_name: tags[ix], TYPE_ATTRIBUTES (template_type));
10334
10335 if (attributes)
10336 TYPE_ATTRIBUTES (t)
10337 = tree_cons (TREE_PURPOSE (attributes),
10338 TREE_VALUE (attributes),
10339 TYPE_ATTRIBUTES (t));
10340 }
10341 }
10342
10343 /* Let's consider the explicit specialization of a member
10344 of a class template specialization that is implicitly instantiated,
10345 e.g.:
10346 template<class T>
10347 struct S
10348 {
10349 template<class U> struct M {}; //#0
10350 };
10351
10352 template<>
10353 template<>
10354 struct S<int>::M<char> //#1
10355 {
10356 int i;
10357 };
10358 [temp.expl.spec]/4 says this is valid.
10359
10360 In this case, when we write:
10361 S<int>::M<char> m;
10362
10363 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10364 the one of #0.
10365
10366 When we encounter #1, we want to store the partial instantiation
10367 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10368
10369 For all cases other than this "explicit specialization of member of a
10370 class template", we just want to store the most general template into
10371 the CLASSTYPE_TI_TEMPLATE of M.
10372
10373 This case of "explicit specialization of member of a class template"
10374 only happens when:
10375 1/ the enclosing class is an instantiation of, and therefore not
10376 the same as, the context of the most general template, and
10377 2/ we aren't looking at the partial instantiation itself, i.e.
10378 the innermost arguments are not the same as the innermost parms of
10379 the most general template.
10380
10381 So it's only when 1/ and 2/ happens that we want to use the partial
10382 instantiation of the member template in lieu of its most general
10383 template. */
10384
10385 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10386 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10387 /* the enclosing class must be an instantiation... */
10388 && CLASS_TYPE_P (context)
10389 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10390 {
10391 TREE_VEC_LENGTH (arglist)--;
10392 ++processing_template_decl;
10393 tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
10394 tree partial_inst_args =
10395 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10396 arglist, complain, NULL_TREE);
10397 --processing_template_decl;
10398 TREE_VEC_LENGTH (arglist)++;
10399 if (partial_inst_args == error_mark_node)
10400 return error_mark_node;
10401 use_partial_inst_tmpl =
10402 /*...and we must not be looking at the partial instantiation
10403 itself. */
10404 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10405 newargs: partial_inst_args);
10406 }
10407
10408 if (!use_partial_inst_tmpl)
10409 /* This case is easy; there are no member templates involved. */
10410 found = gen_tmpl;
10411 else
10412 {
10413 /* This is a full instantiation of a member template. Find
10414 the partial instantiation of which this is an instance. */
10415
10416 /* Temporarily reduce by one the number of levels in the ARGLIST
10417 so as to avoid comparing the last set of arguments. */
10418 TREE_VEC_LENGTH (arglist)--;
10419 /* We don't use COMPLAIN in the following call because this isn't
10420 the immediate context of deduction. For instance, tf_partial
10421 could be set here as we might be at the beginning of template
10422 argument deduction when any explicitly specified template
10423 arguments are substituted into the function type. tf_partial
10424 could lead into trouble because we wouldn't find the partial
10425 instantiation that might have been created outside tf_partial
10426 context, because the levels of template parameters wouldn't
10427 match, because in a tf_partial context, tsubst doesn't reduce
10428 TEMPLATE_PARM_LEVEL. */
10429 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10430 TREE_VEC_LENGTH (arglist)++;
10431 found = (TREE_CODE (found) == TEMPLATE_DECL
10432 ? found
10433 : CLASSTYPE_TI_TEMPLATE (found));
10434
10435 if (DECL_CLASS_TEMPLATE_P (found)
10436 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10437 {
10438 /* If this partial instantiation is specialized, we want to
10439 use it for hash table lookup. */
10440 elt.tmpl = found;
10441 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10442 hash = spec_hasher::hash (e: &elt);
10443 }
10444 }
10445
10446 /* Build template info for the new specialization. */
10447 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10448
10449 elt.spec = t;
10450 slot = type_specializations->find_slot_with_hash (comparable: &elt, hash, insert: INSERT);
10451 gcc_checking_assert (*slot == NULL);
10452 entry = ggc_alloc<spec_entry> ();
10453 *entry = elt;
10454 *slot = entry;
10455
10456 /* Note this use of the partial instantiation so we can check it
10457 later in maybe_process_partial_specialization. */
10458 DECL_TEMPLATE_INSTANTIATIONS (found)
10459 = tree_cons (arglist, t,
10460 DECL_TEMPLATE_INSTANTIATIONS (found));
10461
10462 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10463 && !uses_template_parms (current_nonlambda_scope ()))
10464 /* Now that the type has been registered on the instantiations
10465 list, we set up the enumerators. Because the enumeration
10466 constants may involve the enumeration type itself, we make
10467 sure to register the type first, and then create the
10468 constants. That way, doing tsubst_expr for the enumeration
10469 constants won't result in recursive calls here; we'll find
10470 the instantiation and exit above. */
10471 tsubst_enum (template_type, t, arglist);
10472
10473 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10474 /* If the type makes use of template parameters, the
10475 code that generates debugging information will crash. */
10476 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10477
10478 /* Possibly limit visibility based on template args. */
10479 TREE_PUBLIC (type_decl) = 1;
10480 determine_visibility (type_decl);
10481
10482 inherit_targ_abi_tags (t);
10483
10484 return t;
10485 }
10486}
10487
10488/* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10489
10490tree
10491lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
10492{
10493 if (flag_concepts && variable_concept_p (t: templ))
10494 return build_concept_check (templ, arglist, tf_none);
10495
10496 tree gen_templ = most_general_template (templ);
10497 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
10498 arglist = add_outermost_template_args (args: templ, extra_args: arglist);
10499 arglist = coerce_template_parms (parms, args: arglist, in_decl: templ, complain);
10500 if (arglist == error_mark_node)
10501 return error_mark_node;
10502
10503 /* The type of the expression is NULL_TREE since the template-id could refer
10504 to an explicit or partial specialization. */
10505 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10506}
10507
10508/* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
10509 not dependent. */
10510
10511tree
10512finish_template_variable (tree var, tsubst_flags_t complain)
10513{
10514 tree templ = TREE_OPERAND (var, 0);
10515 tree arglist = TREE_OPERAND (var, 1);
10516
10517 /* If the template or arguments are dependent, then we
10518 can't resolve the TEMPLATE_ID_EXPR yet. */
10519 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
10520 || any_dependent_template_arguments_p (arglist))
10521 return var;
10522
10523 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10524 {
10525 if (complain & tf_error)
10526 {
10527 auto_diagnostic_group d;
10528 error ("use of invalid variable template %qE", var);
10529 diagnose_constraints (location_of (var), templ, arglist);
10530 }
10531 return error_mark_node;
10532 }
10533
10534 return instantiate_template (templ, arglist, complain);
10535}
10536
10537/* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10538 TARGS template args, and instantiate it if it's not dependent. */
10539
10540tree
10541lookup_and_finish_template_variable (tree templ, tree targs,
10542 tsubst_flags_t complain)
10543{
10544 tree var = lookup_template_variable (templ, arglist: targs, complain);
10545 if (var == error_mark_node)
10546 return error_mark_node;
10547 var = finish_template_variable (var, complain);
10548 mark_used (var, complain);
10549 return var;
10550}
10551
10552/* If the set of template parameters PARMS contains a template parameter
10553 at the given LEVEL and INDEX, then return this parameter. Otherwise
10554 return NULL_TREE. */
10555
10556static tree
10557corresponding_template_parameter_list (tree parms, int level, int index)
10558{
10559 while (TMPL_PARMS_DEPTH (parms) > level)
10560 parms = TREE_CHAIN (parms);
10561
10562 if (TMPL_PARMS_DEPTH (parms) != level
10563 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10564 return NULL_TREE;
10565
10566 return TREE_VEC_ELT (TREE_VALUE (parms), index);
10567}
10568
10569/* Return the TREE_LIST for the template parameter from PARMS that positionally
10570 corresponds to the template parameter PARM, or else return NULL_TREE. */
10571
10572static tree
10573corresponding_template_parameter_list (tree parms, tree parm)
10574{
10575 int level, index;
10576 template_parm_level_and_index (parm, &level, &index);
10577 return corresponding_template_parameter_list (parms, level, index);
10578}
10579
10580/* As above, but pull out the actual parameter. */
10581
10582static tree
10583corresponding_template_parameter (tree parms, tree parm)
10584{
10585 tree list = corresponding_template_parameter_list (parms, parm);
10586 if (!list)
10587 return NULL_TREE;
10588
10589 tree t = TREE_VALUE (list);
10590 /* As in template_parm_to_arg. */
10591 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10592 t = TREE_TYPE (t);
10593 else
10594 t = DECL_INITIAL (t);
10595
10596 gcc_assert (TEMPLATE_PARM_P (t));
10597 return t;
10598}
10599
10600struct pair_fn_data
10601{
10602 tree_fn_t fn;
10603 tree_fn_t any_fn;
10604 void *data;
10605 /* True when we should also visit template parameters that occur in
10606 non-deduced contexts. */
10607 bool include_nondeduced_p;
10608 hash_set<tree> *visited;
10609};
10610
10611/* Called from for_each_template_parm via walk_tree. */
10612
10613static tree
10614for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10615{
10616 tree t = *tp;
10617 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10618 tree_fn_t fn = pfd->fn;
10619 void *data = pfd->data;
10620 tree result = NULL_TREE;
10621
10622#define WALK_SUBTREE(NODE) \
10623 do \
10624 { \
10625 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10626 pfd->include_nondeduced_p, \
10627 pfd->any_fn); \
10628 if (result) goto out; \
10629 } \
10630 while (0)
10631
10632 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10633 return t;
10634
10635 if (TYPE_P (t)
10636 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10637 WALK_SUBTREE (TYPE_CONTEXT (t));
10638
10639 switch (TREE_CODE (t))
10640 {
10641 case RECORD_TYPE:
10642 if (TYPE_PTRMEMFUNC_P (t))
10643 break;
10644 /* Fall through. */
10645
10646 case UNION_TYPE:
10647 case ENUMERAL_TYPE:
10648 if (!TYPE_TEMPLATE_INFO (t))
10649 *walk_subtrees = 0;
10650 else
10651 WALK_SUBTREE (TYPE_TI_ARGS (t));
10652 break;
10653
10654 case INTEGER_TYPE:
10655 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10656 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10657 break;
10658
10659 case METHOD_TYPE:
10660 /* Since we're not going to walk subtrees, we have to do this
10661 explicitly here. */
10662 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10663 /* Fall through. */
10664
10665 case FUNCTION_TYPE:
10666 /* Check the return type. */
10667 WALK_SUBTREE (TREE_TYPE (t));
10668
10669 /* Check the parameter types. Since default arguments are not
10670 instantiated until they are needed, the TYPE_ARG_TYPES may
10671 contain expressions that involve template parameters. But,
10672 no-one should be looking at them yet. And, once they're
10673 instantiated, they don't contain template parameters, so
10674 there's no point in looking at them then, either. */
10675 {
10676 tree parm;
10677
10678 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10679 WALK_SUBTREE (TREE_VALUE (parm));
10680
10681 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10682 want walk_tree walking into them itself. */
10683 *walk_subtrees = 0;
10684 }
10685
10686 if (flag_noexcept_type)
10687 {
10688 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10689 if (spec)
10690 WALK_SUBTREE (TREE_PURPOSE (spec));
10691 }
10692 break;
10693
10694 case TYPEOF_TYPE:
10695 case DECLTYPE_TYPE:
10696 if (pfd->include_nondeduced_p
10697 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10698 pfd->visited,
10699 pfd->include_nondeduced_p,
10700 pfd->any_fn))
10701 return error_mark_node;
10702 *walk_subtrees = false;
10703 break;
10704
10705 case TRAIT_TYPE:
10706 if (pfd->include_nondeduced_p)
10707 {
10708 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
10709 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
10710 }
10711 *walk_subtrees = false;
10712 break;
10713
10714 case FUNCTION_DECL:
10715 case VAR_DECL:
10716 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10717 WALK_SUBTREE (DECL_TI_ARGS (t));
10718 break;
10719
10720 case PARM_DECL:
10721 WALK_SUBTREE (TREE_TYPE (t));
10722 break;
10723
10724 case CONST_DECL:
10725 if (DECL_TEMPLATE_PARM_P (t))
10726 WALK_SUBTREE (DECL_INITIAL (t));
10727 if (DECL_CONTEXT (t)
10728 && pfd->include_nondeduced_p)
10729 WALK_SUBTREE (DECL_CONTEXT (t));
10730 break;
10731
10732 case BOUND_TEMPLATE_TEMPLATE_PARM:
10733 /* Record template parameters such as `T' inside `TT<T>'. */
10734 WALK_SUBTREE (TYPE_TI_ARGS (t));
10735 /* Fall through. */
10736
10737 case TEMPLATE_TEMPLATE_PARM:
10738 case TEMPLATE_TYPE_PARM:
10739 case TEMPLATE_PARM_INDEX:
10740 if (fn && (*fn)(t, data))
10741 return t;
10742 else if (!fn)
10743 return t;
10744 break;
10745
10746 case TEMPLATE_DECL:
10747 /* A template template parameter is encountered. */
10748 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10749 WALK_SUBTREE (TREE_TYPE (t));
10750
10751 /* Already substituted template template parameter */
10752 *walk_subtrees = 0;
10753 break;
10754
10755 case TYPENAME_TYPE:
10756 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10757 partial instantiation. */
10758 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10759 *walk_subtrees = 0;
10760 break;
10761
10762 case INDIRECT_REF:
10763 case COMPONENT_REF:
10764 /* If there's no type, then this thing must be some expression
10765 involving template parameters. */
10766 if (!fn && !TREE_TYPE (t))
10767 return error_mark_node;
10768 break;
10769
10770 case CONSTRUCTOR:
10771 case TRAIT_EXPR:
10772 case PLUS_EXPR:
10773 case MULT_EXPR:
10774 case SCOPE_REF:
10775 /* These are non-deduced contexts. */
10776 if (!pfd->include_nondeduced_p)
10777 *walk_subtrees = 0;
10778 break;
10779
10780 case MODOP_EXPR:
10781 case CAST_EXPR:
10782 case IMPLICIT_CONV_EXPR:
10783 case REINTERPRET_CAST_EXPR:
10784 case CONST_CAST_EXPR:
10785 case STATIC_CAST_EXPR:
10786 case DYNAMIC_CAST_EXPR:
10787 case ARROW_EXPR:
10788 case DOTSTAR_EXPR:
10789 case TYPEID_EXPR:
10790 case PSEUDO_DTOR_EXPR:
10791 if (!fn)
10792 return error_mark_node;
10793 break;
10794
10795 default:
10796 break;
10797 }
10798
10799 #undef WALK_SUBTREE
10800
10801 /* We didn't find any template parameters we liked. */
10802 out:
10803 return result;
10804}
10805
10806/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10807 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10808 call FN with the parameter and the DATA.
10809 If FN returns nonzero, the iteration is terminated, and
10810 for_each_template_parm returns 1. Otherwise, the iteration
10811 continues. If FN never returns a nonzero value, the value
10812 returned by for_each_template_parm is 0. If FN is NULL, it is
10813 considered to be the function which always returns 1.
10814
10815 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10816 parameters that occur in non-deduced contexts. When false, only
10817 visits those template parameters that can be deduced. */
10818
10819static tree
10820for_each_template_parm (tree t, tree_fn_t fn, void* data,
10821 hash_set<tree> *visited,
10822 bool include_nondeduced_p,
10823 tree_fn_t any_fn)
10824{
10825 struct pair_fn_data pfd;
10826 tree result;
10827
10828 /* Set up. */
10829 pfd.fn = fn;
10830 pfd.any_fn = any_fn;
10831 pfd.data = data;
10832 pfd.include_nondeduced_p = include_nondeduced_p;
10833
10834 /* Walk the tree. (Conceptually, we would like to walk without
10835 duplicates, but for_each_template_parm_r recursively calls
10836 for_each_template_parm, so we would need to reorganize a fair
10837 bit to use walk_tree_without_duplicates, so we keep our own
10838 visited list.) */
10839 if (visited)
10840 pfd.visited = visited;
10841 else
10842 pfd.visited = new hash_set<tree>;
10843 result = cp_walk_tree (&t,
10844 for_each_template_parm_r,
10845 &pfd,
10846 pfd.visited);
10847
10848 /* Clean up. */
10849 if (!visited)
10850 {
10851 delete pfd.visited;
10852 pfd.visited = 0;
10853 }
10854
10855 return result;
10856}
10857
10858struct find_template_parameter_info
10859{
10860 explicit find_template_parameter_info (tree ctx_parms)
10861 : ctx_parms (ctx_parms),
10862 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10863 {}
10864
10865 hash_set<tree> visited;
10866 hash_set<tree> parms;
10867 tree parm_list = NULL_TREE;
10868 tree *parm_list_tail = &parm_list;
10869 tree ctx_parms;
10870 int max_depth;
10871
10872 tree find_in (tree);
10873 tree find_in_recursive (tree);
10874 bool found (tree);
10875 unsigned num_found () { return parms.elements (); }
10876};
10877
10878/* Appends the declaration of T to the list in DATA. */
10879
10880static int
10881keep_template_parm (tree t, void* data)
10882{
10883 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10884
10885 /* Template parameters declared within the expression are not part of
10886 the parameter mapping. For example, in this concept:
10887
10888 template<typename T>
10889 concept C = requires { <expr> } -> same_as<int>;
10890
10891 the return specifier same_as<int> declares a new decltype parameter
10892 that must not be part of the parameter mapping. The same is true
10893 for generic lambda parameters, lambda template parameters, etc. */
10894 int level;
10895 int index;
10896 template_parm_level_and_index (t, &level, &index);
10897 if (level == 0 || level > ftpi->max_depth)
10898 return 0;
10899
10900 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10901 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10902 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10903 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10904
10905 /* This template parameter might be an argument to a cached dependent
10906 specalization that was formed earlier inside some other template, in
10907 which case the parameter is not among the ones that are in-scope.
10908 Look in CTX_PARMS to find the corresponding in-scope template
10909 parameter, and use it instead. */
10910 if (tree in_scope = corresponding_template_parameter (parms: ftpi->ctx_parms, parm: t))
10911 t = in_scope;
10912
10913 /* Arguments like const T yield parameters like const T. This means that
10914 a template-id like X<T, const T> would yield two distinct parameters:
10915 T and const T. Adjust types to their unqualified versions. */
10916 if (TYPE_P (t))
10917 t = TYPE_MAIN_VARIANT (t);
10918 if (!ftpi->parms.add (k: t))
10919 {
10920 /* Append T to PARM_LIST. */
10921 tree node = build_tree_list (NULL_TREE, t);
10922 *ftpi->parm_list_tail = node;
10923 ftpi->parm_list_tail = &TREE_CHAIN (node);
10924 }
10925
10926 /* Verify the parameter we found has a valid index. */
10927 if (flag_checking)
10928 {
10929 tree parms = ftpi->ctx_parms;
10930 while (TMPL_PARMS_DEPTH (parms) > level)
10931 parms = TREE_CHAIN (parms);
10932 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10933 gcc_assert (index < len);
10934 }
10935
10936 return 0;
10937}
10938
10939/* Ensure that we recursively examine certain terms that are not normally
10940 visited in for_each_template_parm_r. */
10941
10942static int
10943any_template_parm_r (tree t, void *data)
10944{
10945 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10946
10947#define WALK_SUBTREE(NODE) \
10948 do \
10949 { \
10950 for_each_template_parm (NODE, keep_template_parm, data, \
10951 &ftpi->visited, true, \
10952 any_template_parm_r); \
10953 } \
10954 while (0)
10955
10956 /* A mention of a member alias/typedef is a use of all of its template
10957 arguments, including those from the enclosing class, so we don't use
10958 alias_template_specialization_p here. */
10959 if (TYPE_P (t) && typedef_variant_p (type: t))
10960 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10961 WALK_SUBTREE (TI_ARGS (tinfo));
10962
10963 switch (TREE_CODE (t))
10964 {
10965 case TEMPLATE_TYPE_PARM:
10966 /* Type constraints of a placeholder type may contain parameters. */
10967 if (is_auto (t))
10968 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10969 WALK_SUBTREE (constr);
10970 break;
10971
10972 case TEMPLATE_ID_EXPR:
10973 /* Search through references to variable templates. */
10974 WALK_SUBTREE (TREE_OPERAND (t, 0));
10975 WALK_SUBTREE (TREE_OPERAND (t, 1));
10976 break;
10977
10978 case TEMPLATE_PARM_INDEX:
10979 WALK_SUBTREE (TREE_TYPE (t));
10980 break;
10981
10982 case TEMPLATE_DECL:
10983 /* If T is a member template that shares template parameters with
10984 ctx_parms, we need to mark all those parameters for mapping.
10985 To that end, it should suffice to just walk the DECL_CONTEXT of
10986 the template (assuming the template is not overly general). */
10987 WALK_SUBTREE (DECL_CONTEXT (t));
10988 break;
10989
10990 case LAMBDA_EXPR:
10991 {
10992 /* Look in the parms and body. */
10993 tree fn = lambda_function (t);
10994 WALK_SUBTREE (TREE_TYPE (fn));
10995 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10996 }
10997 break;
10998
10999 case IDENTIFIER_NODE:
11000 if (IDENTIFIER_CONV_OP_P (t))
11001 /* The conversion-type-id of a conversion operator may be dependent. */
11002 WALK_SUBTREE (TREE_TYPE (t));
11003 break;
11004
11005 case CONVERT_EXPR:
11006 if (is_dummy_object (t))
11007 WALK_SUBTREE (TREE_TYPE (t));
11008 break;
11009
11010 default:
11011 break;
11012 }
11013
11014 /* Keep walking. */
11015 return 0;
11016}
11017
11018/* Look through T for template parameters. */
11019
11020tree
11021find_template_parameter_info::find_in (tree t)
11022{
11023 return for_each_template_parm (t, fn: keep_template_parm, data: this, visited: &visited,
11024 /*include_nondeduced*/include_nondeduced_p: true,
11025 any_fn: any_template_parm_r);
11026}
11027
11028/* As above, but also recursively look into the default arguments of template
11029 parameters we found. Used for alias CTAD. */
11030
11031tree
11032find_template_parameter_info::find_in_recursive (tree t)
11033{
11034 if (tree r = find_in (t))
11035 return r;
11036 /* Since newly found parms are added to the end of the list, we
11037 can just walk it until we reach the end. */
11038 for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
11039 {
11040 tree parm = TREE_VALUE (pl);
11041 tree list = corresponding_template_parameter_list (parms: ctx_parms, parm);
11042 if (tree r = find_in (TREE_PURPOSE (list)))
11043 return r;
11044 }
11045 return NULL_TREE;
11046}
11047
11048/* True if PARM was found by a previous call to find_in. PARM can be a
11049 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */
11050
11051bool
11052find_template_parameter_info::found (tree parm)
11053{
11054 if (TREE_CODE (parm) == TREE_LIST)
11055 parm = TREE_VALUE (parm);
11056 if (TREE_CODE (parm) == TYPE_DECL
11057 || TREE_CODE (parm) == TEMPLATE_DECL)
11058 parm = TREE_TYPE (parm);
11059 else
11060 parm = DECL_INITIAL (parm);
11061 gcc_checking_assert (TEMPLATE_PARM_P (parm));
11062 return parms.contains (k: parm);
11063}
11064
11065/* Returns a list of unique template parameters found within T, where CTX_PARMS
11066 are the template parameters in scope. */
11067
11068tree
11069find_template_parameters (tree t, tree ctx_parms)
11070{
11071 if (!ctx_parms)
11072 return NULL_TREE;
11073
11074 find_template_parameter_info ftpi (ctx_parms);
11075 ftpi.find_in (t);
11076 return ftpi.parm_list;
11077}
11078
11079/* Returns true if T depends on any template parameter. */
11080
11081bool
11082uses_template_parms (tree t)
11083{
11084 if (t == NULL_TREE || t == error_mark_node)
11085 return false;
11086
11087 /* Namespaces can't depend on any template parameters. */
11088 if (TREE_CODE (t) == NAMESPACE_DECL)
11089 return false;
11090
11091 processing_template_decl_sentinel ptds (/*reset*/false);
11092 ++processing_template_decl;
11093
11094 if (TYPE_P (t))
11095 return dependent_type_p (t);
11096 else if (TREE_CODE (t) == TREE_VEC)
11097 return any_dependent_template_arguments_p (t);
11098 else if (TREE_CODE (t) == TREE_LIST)
11099 return (uses_template_parms (TREE_VALUE (t))
11100 || uses_template_parms (TREE_CHAIN (t)));
11101 else if (TREE_CODE (t) == TYPE_DECL)
11102 return dependent_type_p (TREE_TYPE (t));
11103 else
11104 return instantiation_dependent_expression_p (t);
11105}
11106
11107/* Returns true if T depends on any template parameter with level LEVEL. */
11108
11109bool
11110uses_template_parms_level (tree t, int level)
11111{
11112 return for_each_template_parm (t, fn: template_parm_this_level_p, data: &level, NULL,
11113 /*include_nondeduced_p=*/true);
11114}
11115
11116/* Returns true if the signature of DECL depends on any template parameter from
11117 its enclosing class. */
11118
11119static bool
11120uses_outer_template_parms (tree decl)
11121{
11122 int depth;
11123 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11124 depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
11125 else
11126 depth = template_class_depth (CP_DECL_CONTEXT (decl));
11127 if (depth == 0)
11128 return false;
11129 if (for_each_template_parm (TREE_TYPE (decl), fn: template_parm_outer_level,
11130 data: &depth, NULL, /*include_nondeduced_p=*/true))
11131 return true;
11132 if (PRIMARY_TEMPLATE_P (decl)
11133 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11134 {
11135 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
11136 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
11137 {
11138 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
11139 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
11140 if (TREE_CODE (parm) == PARM_DECL
11141 && for_each_template_parm (TREE_TYPE (parm),
11142 fn: template_parm_outer_level,
11143 data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true))
11144 return true;
11145 if (TREE_CODE (parm) == TEMPLATE_DECL
11146 && uses_outer_template_parms (decl: parm))
11147 return true;
11148 if (defarg
11149 && for_each_template_parm (t: defarg, fn: template_parm_outer_level,
11150 data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true))
11151 return true;
11152 }
11153 }
11154 if (uses_outer_template_parms_in_constraints (decl))
11155 return true;
11156 return false;
11157}
11158
11159/* Returns true if the constraints of DECL depend on any template parameters
11160 from its enclosing scope. */
11161
11162bool
11163uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
11164{
11165 tree ci = get_constraints (decl);
11166 if (ci)
11167 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
11168 if (!ci)
11169 return false;
11170 if (!ctx)
11171 {
11172 if (tree fc = DECL_FRIEND_CONTEXT (decl))
11173 ctx = fc;
11174 else
11175 ctx = CP_DECL_CONTEXT (decl);
11176 }
11177 int depth = template_class_depth (type: ctx);
11178 if (depth == 0)
11179 return false;
11180 return for_each_template_parm (t: ci, fn: template_parm_outer_level,
11181 data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true);
11182}
11183
11184/* Returns TRUE iff INST is an instantiation we don't need to do in an
11185 ill-formed translation unit, i.e. a variable or function that isn't
11186 usable in a constant expression. */
11187
11188static inline bool
11189neglectable_inst_p (tree d)
11190{
11191 return (d && DECL_P (d)
11192 && !undeduced_auto_decl (d)
11193 && !(TREE_CODE (d) == FUNCTION_DECL
11194 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
11195 : decl_maybe_constant_var_p (d)));
11196}
11197
11198/* Returns TRUE iff we should refuse to instantiate DECL because it's
11199 neglectable and instantiated from within an erroneous instantiation. */
11200
11201static bool
11202limit_bad_template_recursion (tree decl)
11203{
11204 struct tinst_level *lev = current_tinst_level;
11205 int errs = errorcount + sorrycount;
11206 if (errs == 0 || !neglectable_inst_p (d: decl))
11207 return false;
11208
11209 /* Avoid instantiating members of an ill-formed class. */
11210 bool refuse
11211 = (DECL_CLASS_SCOPE_P (decl)
11212 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
11213
11214 if (!refuse)
11215 {
11216 for (; lev; lev = lev->next)
11217 if (neglectable_inst_p (d: lev->maybe_get_node ()))
11218 break;
11219 refuse = (lev && errs > lev->errors);
11220 }
11221
11222 if (refuse)
11223 {
11224 /* Don't warn about it not being defined. */
11225 suppress_warning (decl, OPT_Wunused);
11226 tree clone;
11227 FOR_EACH_CLONE (clone, decl)
11228 suppress_warning (clone, OPT_Wunused);
11229 }
11230 return refuse;
11231}
11232
11233static int tinst_depth;
11234extern int max_tinst_depth;
11235int depth_reached;
11236
11237static GTY(()) struct tinst_level *last_error_tinst_level;
11238
11239/* We're starting to instantiate D; record the template instantiation context
11240 at LOC for diagnostics and to restore it later. */
11241
11242bool
11243push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
11244{
11245 struct tinst_level *new_level;
11246
11247 if (tinst_depth >= max_tinst_depth)
11248 {
11249 /* Tell error.cc not to try to instantiate any templates. */
11250 at_eof = 3;
11251 fatal_error (input_location,
11252 "template instantiation depth exceeds maximum of %d"
11253 " (use %<-ftemplate-depth=%> to increase the maximum)",
11254 max_tinst_depth);
11255 return false;
11256 }
11257
11258 /* If the current instantiation caused problems, don't let it instantiate
11259 anything else. Do allow deduction substitution and decls usable in
11260 constant expressions. */
11261 if (!targs && limit_bad_template_recursion (decl: tldcl))
11262 {
11263 /* Avoid no_linkage_errors and unused function (and all other)
11264 warnings for this decl. */
11265 suppress_warning (tldcl);
11266 return false;
11267 }
11268
11269 /* When not -quiet, dump template instantiations other than functions, since
11270 announce_function will take care of those. */
11271 if (!quiet_flag && !targs
11272 && TREE_CODE (tldcl) != TREE_LIST
11273 && TREE_CODE (tldcl) != FUNCTION_DECL)
11274 fprintf (stderr, format: " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
11275
11276 new_level = tinst_level_freelist ().alloc ();
11277 new_level->tldcl = tldcl;
11278 new_level->targs = targs;
11279 new_level->locus = loc;
11280 new_level->errors = errorcount + sorrycount;
11281 new_level->next = NULL;
11282 new_level->refcount = 0;
11283 new_level->path = new_level->visible = nullptr;
11284 set_refcount_ptr (ptr&: new_level->next, obj: current_tinst_level);
11285 set_refcount_ptr (ptr&: current_tinst_level, obj: new_level);
11286
11287 ++tinst_depth;
11288 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11289 depth_reached = tinst_depth;
11290
11291 return true;
11292}
11293
11294/* We're starting substitution of TMPL<ARGS>; record the template
11295 substitution context for diagnostics and to restore it later. */
11296
11297bool
11298push_tinst_level (tree tmpl, tree args)
11299{
11300 return push_tinst_level_loc (tldcl: tmpl, targs: args, loc: input_location);
11301}
11302
11303/* We're starting to instantiate D; record INPUT_LOCATION and the
11304 template instantiation context for diagnostics and to restore it
11305 later. */
11306
11307bool
11308push_tinst_level (tree d)
11309{
11310 return push_tinst_level_loc (d, input_location);
11311}
11312
11313/* Likewise, but record LOC as the program location. */
11314
11315bool
11316push_tinst_level_loc (tree d, location_t loc)
11317{
11318 gcc_assert (TREE_CODE (d) != TREE_LIST);
11319 return push_tinst_level_loc (tldcl: d, NULL, loc);
11320}
11321
11322/* We're done instantiating this template; return to the instantiation
11323 context. */
11324
11325void
11326pop_tinst_level (void)
11327{
11328 /* Restore the filename and line number stashed away when we started
11329 this instantiation. */
11330 input_location = current_tinst_level->locus;
11331 set_refcount_ptr (ptr&: current_tinst_level, obj: current_tinst_level->next);
11332 --tinst_depth;
11333}
11334
11335/* We're instantiating a deferred template; restore the template
11336 instantiation context in which the instantiation was requested, which
11337 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11338
11339static tree
11340reopen_tinst_level (struct tinst_level *level)
11341{
11342 struct tinst_level *t;
11343
11344 tinst_depth = 0;
11345 for (t = level; t; t = t->next)
11346 ++tinst_depth;
11347
11348 set_refcount_ptr (ptr&: current_tinst_level, obj: level);
11349 pop_tinst_level ();
11350 if (current_tinst_level)
11351 current_tinst_level->errors = errorcount+sorrycount;
11352 return level->maybe_get_node ();
11353}
11354
11355/* Returns the TINST_LEVEL which gives the original instantiation
11356 context. */
11357
11358struct tinst_level *
11359outermost_tinst_level (void)
11360{
11361 struct tinst_level *level = current_tinst_level;
11362 if (level)
11363 while (level->next)
11364 level = level->next;
11365 return level;
11366}
11367
11368/* True iff T is a friend function declaration that is not itself a template
11369 and is not defined in a class template. */
11370
11371bool
11372non_templated_friend_p (tree t)
11373{
11374 if (t && TREE_CODE (t) == FUNCTION_DECL
11375 && DECL_UNIQUE_FRIEND_P (t))
11376 {
11377 tree ti = DECL_TEMPLATE_INFO (t);
11378 if (!ti)
11379 return true;
11380 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11381 if (DECL_FRIEND_CONTEXT (t))
11382 return false;
11383 /* Non-templated friends in a class template are still represented with a
11384 TEMPLATE_DECL; check that its primary template is the befriending
11385 class. Note that DECL_PRIMARY_TEMPLATE is null for
11386 template <class T> friend A<T>::f(); */
11387 tree tmpl = TI_TEMPLATE (ti);
11388 tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
11389 return (primary && primary != tmpl);
11390 }
11391 else
11392 return false;
11393}
11394
11395/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11396 vector of template arguments, as for tsubst.
11397
11398 Returns an appropriate tsubst'd friend declaration. */
11399
11400static tree
11401tsubst_friend_function (tree decl, tree args)
11402{
11403 tree new_friend;
11404
11405 if (TREE_CODE (decl) == FUNCTION_DECL
11406 && DECL_TEMPLATE_INSTANTIATION (decl)
11407 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11408 /* This was a friend declared with an explicit template
11409 argument list, e.g.:
11410
11411 friend void f<>(T);
11412
11413 to indicate that f was a template instantiation, not a new
11414 function declaration. Now, we have to figure out what
11415 instantiation of what template. */
11416 {
11417 tree template_id, arglist, fns;
11418 tree new_args;
11419 tree tmpl;
11420 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11421
11422 /* Friend functions are looked up in the containing namespace scope.
11423 We must enter that scope, to avoid finding member functions of the
11424 current class with same name. */
11425 push_nested_namespace (ns);
11426 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11427 tf_warning_or_error, NULL_TREE);
11428 pop_nested_namespace (ns);
11429 arglist = tsubst (DECL_TI_ARGS (decl), args,
11430 tf_warning_or_error, NULL_TREE);
11431 template_id = lookup_template_function (fns, arglist);
11432
11433 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11434 tmpl = determine_specialization (template_id, decl: new_friend,
11435 targs_out: &new_args,
11436 /*need_member_template=*/0,
11437 TREE_VEC_LENGTH (args),
11438 tsk: tsk_none);
11439 return instantiate_template (tmpl, new_args, tf_error);
11440 }
11441
11442 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11443 if (new_friend == error_mark_node)
11444 return error_mark_node;
11445
11446 /* The NEW_FRIEND will look like an instantiation, to the
11447 compiler, but is not an instantiation from the point of view of
11448 the language. For example, we might have had:
11449
11450 template <class T> struct S {
11451 template <class U> friend void f(T, U);
11452 };
11453
11454 Then, in S<int>, template <class U> void f(int, U) is not an
11455 instantiation of anything. */
11456
11457 DECL_USE_TEMPLATE (new_friend) = 0;
11458 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11459 {
11460 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11461 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11462 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11463 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11464
11465 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11466 match in decls_match. */
11467 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11468 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11469 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11470 if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
11471 {
11472 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11473 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11474 tsubst_each_template_parm_constraints (parms, args,
11475 tf_warning_or_error);
11476 }
11477 }
11478
11479 /* The mangled name for the NEW_FRIEND is incorrect. The function
11480 is not a template instantiation and should not be mangled like
11481 one. Therefore, we forget the mangling here; we'll recompute it
11482 later if we need it. */
11483 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11484 {
11485 SET_DECL_RTL (new_friend, NULL);
11486 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11487 }
11488
11489 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11490 {
11491 tree old_decl;
11492 tree ns;
11493
11494 /* We must save some information from NEW_FRIEND before calling
11495 duplicate decls since that function will free NEW_FRIEND if
11496 possible. */
11497 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11498 tree new_friend_result_template_info = NULL_TREE;
11499 bool new_friend_is_defn =
11500 (new_friend_template_info
11501 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11502 (template_for_substitution (new_friend)))
11503 != NULL_TREE));
11504 tree not_tmpl = new_friend;
11505
11506 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11507 {
11508 /* This declaration is a `primary' template. */
11509 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11510
11511 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11512 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11513 }
11514
11515 /* Inside pushdecl_namespace_level, we will push into the
11516 current namespace. However, the friend function should go
11517 into the namespace of the template. */
11518 ns = decl_namespace_context (new_friend);
11519 push_nested_namespace (ns);
11520 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11521 pop_nested_namespace (ns);
11522
11523 if (old_decl == error_mark_node)
11524 return error_mark_node;
11525
11526 if (old_decl != new_friend)
11527 {
11528 /* This new friend declaration matched an existing
11529 declaration. For example, given:
11530
11531 template <class T> void f(T);
11532 template <class U> class C {
11533 template <class T> friend void f(T) {}
11534 };
11535
11536 the friend declaration actually provides the definition
11537 of `f', once C has been instantiated for some type. So,
11538 old_decl will be the out-of-class template declaration,
11539 while new_friend is the in-class definition.
11540
11541 But, if `f' was called before this point, the
11542 instantiation of `f' will have DECL_TI_ARGS corresponding
11543 to `T' but not to `U', references to which might appear
11544 in the definition of `f'. Previously, the most general
11545 template for an instantiation of `f' was the out-of-class
11546 version; now it is the in-class version. Therefore, we
11547 run through all specialization of `f', adding to their
11548 DECL_TI_ARGS appropriately. In particular, they need a
11549 new set of outer arguments, corresponding to the
11550 arguments for this class instantiation.
11551
11552 The same situation can arise with something like this:
11553
11554 friend void f(int);
11555 template <class T> class C {
11556 friend void f(T) {}
11557 };
11558
11559 when `C<int>' is instantiated. Now, `f(int)' is defined
11560 in the class. */
11561
11562 if (!new_friend_is_defn)
11563 /* On the other hand, if the in-class declaration does
11564 *not* provide a definition, then we don't want to alter
11565 existing definitions. We can just leave everything
11566 alone. */
11567 ;
11568 else
11569 {
11570 tree new_template = TI_TEMPLATE (new_friend_template_info);
11571 tree new_args = TI_ARGS (new_friend_template_info);
11572
11573 /* Overwrite whatever template info was there before, if
11574 any, with the new template information pertaining to
11575 the declaration. */
11576 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11577
11578 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11579 {
11580 /* We should have called reregister_specialization in
11581 duplicate_decls. */
11582 gcc_assert (retrieve_specialization (new_template,
11583 new_args, 0)
11584 == old_decl);
11585
11586 /* Instantiate it if the global has already been used. */
11587 if (DECL_ODR_USED (old_decl))
11588 instantiate_decl (old_decl, /*defer_ok=*/true,
11589 /*expl_inst_class_mem_p=*/false);
11590 }
11591 else
11592 {
11593 tree t;
11594
11595 /* Indicate that the old function template is a partial
11596 instantiation. */
11597 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11598 = new_friend_result_template_info;
11599
11600 gcc_assert (new_template
11601 == most_general_template (new_template));
11602 gcc_assert (new_template != old_decl);
11603
11604 /* Reassign any specializations already in the hash table
11605 to the new more general template, and add the
11606 additional template args. */
11607 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11608 t != NULL_TREE;
11609 t = TREE_CHAIN (t))
11610 {
11611 tree spec = TREE_VALUE (t);
11612 spec_entry elt;
11613
11614 elt.tmpl = old_decl;
11615 elt.args = DECL_TI_ARGS (spec);
11616 elt.spec = NULL_TREE;
11617
11618 decl_specializations->remove_elt (value: &elt);
11619
11620 DECL_TI_ARGS (spec)
11621 = add_outermost_template_args (args: new_args,
11622 DECL_TI_ARGS (spec));
11623
11624 register_specialization
11625 (spec, tmpl: new_template, DECL_TI_ARGS (spec), is_friend: true, hash: 0);
11626
11627 }
11628 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11629 }
11630 }
11631
11632 /* The information from NEW_FRIEND has been merged into OLD_DECL
11633 by duplicate_decls. */
11634 new_friend = old_decl;
11635 }
11636
11637 /* We've just introduced a namespace-scope function in the purview
11638 without necessarily having opened the enclosing namespace, so
11639 make sure the namespace is in the purview now too. */
11640 if (modules_p ()
11641 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
11642 && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
11643 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
11644 }
11645 else
11646 {
11647 tree context = DECL_CONTEXT (new_friend);
11648 bool dependent_p;
11649
11650 /* In the code
11651 template <class T> class C {
11652 template <class U> friend void C1<U>::f (); // case 1
11653 friend void C2<T>::f (); // case 2
11654 };
11655 we only need to make sure CONTEXT is a complete type for
11656 case 2. To distinguish between the two cases, we note that
11657 CONTEXT of case 1 remains dependent type after tsubst while
11658 this isn't true for case 2. */
11659 ++processing_template_decl;
11660 dependent_p = dependent_type_p (context);
11661 --processing_template_decl;
11662
11663 if (!dependent_p
11664 && !complete_type_or_else (context, NULL_TREE))
11665 return error_mark_node;
11666
11667 if (COMPLETE_TYPE_P (context))
11668 {
11669 tree fn = new_friend;
11670 /* do_friend adds the TEMPLATE_DECL for any member friend
11671 template even if it isn't a member template, i.e.
11672 template <class T> friend A<T>::f();
11673 Look through it in that case. */
11674 if (TREE_CODE (fn) == TEMPLATE_DECL
11675 && !PRIMARY_TEMPLATE_P (fn))
11676 fn = DECL_TEMPLATE_RESULT (fn);
11677 /* Check to see that the declaration is really present, and,
11678 possibly obtain an improved declaration. */
11679 fn = check_classfn (context, fn, NULL_TREE);
11680
11681 if (fn)
11682 new_friend = fn;
11683 }
11684 }
11685
11686 return new_friend;
11687}
11688
11689/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11690 template arguments, as for tsubst.
11691
11692 Returns an appropriate tsubst'd friend type or error_mark_node on
11693 failure. */
11694
11695static tree
11696tsubst_friend_class (tree friend_tmpl, tree args)
11697{
11698 tree tmpl;
11699
11700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11701 {
11702 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11703 return TREE_TYPE (tmpl);
11704 }
11705
11706 tree context = CP_DECL_CONTEXT (friend_tmpl);
11707 if (TREE_CODE (context) == NAMESPACE_DECL)
11708 push_nested_namespace (context);
11709 else
11710 {
11711 context = tsubst (context, args, tf_error, NULL_TREE);
11712 push_nested_class (context);
11713 }
11714
11715 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11716 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11717
11718 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11719 {
11720 /* The friend template has already been declared. Just
11721 check to see that the declarations match, and install any new
11722 default parameters. We must tsubst the default parameters,
11723 of course. We only need the innermost template parameters
11724 because that is all that redeclare_class_template will look
11725 at. */
11726 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11727 > TMPL_ARGS_DEPTH (args))
11728 {
11729 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11730 args, tf_warning_or_error);
11731 tsubst_each_template_parm_constraints (parms, args,
11732 tf_warning_or_error);
11733 location_t saved_input_location = input_location;
11734 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11735 tree cons = get_constraints (friend_tmpl);
11736 ++processing_template_decl;
11737 cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
11738 DECL_FRIEND_CONTEXT (friend_tmpl));
11739 --processing_template_decl;
11740 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11741 input_location = saved_input_location;
11742 }
11743 }
11744 else
11745 {
11746 /* The friend template has not already been declared. In this
11747 case, the instantiation of the template class will cause the
11748 injection of this template into the namespace scope. */
11749 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11750
11751 if (tmpl != error_mark_node)
11752 {
11753 /* The new TMPL is not an instantiation of anything, so we
11754 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11755 for the new type because that is supposed to be the
11756 corresponding template decl, i.e., TMPL. */
11757 DECL_USE_TEMPLATE (tmpl) = 0;
11758 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11759 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11760 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11761 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11762
11763 /* Substitute into and set the constraints on the new declaration. */
11764 if (tree ci = get_constraints (friend_tmpl))
11765 {
11766 ++processing_template_decl;
11767 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11768 DECL_FRIEND_CONTEXT (friend_tmpl));
11769 --processing_template_decl;
11770 set_constraints (tmpl, ci);
11771 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
11772 args, tf_warning_or_error);
11773 }
11774
11775 /* Inject this template into the enclosing namspace scope. */
11776 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11777 }
11778 }
11779
11780 if (TREE_CODE (context) == NAMESPACE_DECL)
11781 pop_nested_namespace (context);
11782 else
11783 pop_nested_class ();
11784
11785 return TREE_TYPE (tmpl);
11786}
11787
11788/* Returns zero if TYPE cannot be completed later due to circularity.
11789 Otherwise returns one. */
11790
11791static int
11792can_complete_type_without_circularity (tree type)
11793{
11794 if (type == NULL_TREE || type == error_mark_node)
11795 return 0;
11796 else if (COMPLETE_TYPE_P (type))
11797 return 1;
11798 else if (TREE_CODE (type) == ARRAY_TYPE)
11799 return can_complete_type_without_circularity (TREE_TYPE (type));
11800 else if (CLASS_TYPE_P (type)
11801 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11802 return 0;
11803 else
11804 return 1;
11805}
11806
11807static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11808 tsubst_flags_t, tree);
11809
11810/* Instantiate the contract statement. */
11811
11812static tree
11813tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
11814 tree in_decl)
11815{
11816 tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
11817 bool auto_p = type_uses_auto (type);
11818
11819 tree r = copy_node (t);
11820
11821 /* Rebuild the result variable. */
11822 if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
11823 {
11824 tree oldvar = POSTCONDITION_IDENTIFIER (t);
11825
11826 tree newvar = copy_node (oldvar);
11827 TREE_TYPE (newvar) = type;
11828 DECL_CONTEXT (newvar) = decl;
11829 POSTCONDITION_IDENTIFIER (r) = newvar;
11830
11831 /* Make sure the postcondition is valid. */
11832 location_t loc = DECL_SOURCE_LOCATION (oldvar);
11833 if (!auto_p)
11834 if (!check_postcondition_result (decl, type, loc))
11835 return invalidate_contract (r);
11836
11837 /* Make the variable available for lookup. */
11838 register_local_specialization (spec: newvar, tmpl: oldvar);
11839 }
11840
11841 /* Instantiate the condition. If the return type is undeduced, process
11842 the expression as if inside a template to avoid spurious type errors. */
11843 if (auto_p)
11844 ++processing_template_decl;
11845 ++processing_contract_condition;
11846 CONTRACT_CONDITION (r)
11847 = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
11848 --processing_contract_condition;
11849 if (auto_p)
11850 --processing_template_decl;
11851
11852 /* And the comment. */
11853 CONTRACT_COMMENT (r)
11854 = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
11855
11856 return r;
11857}
11858
11859/* Update T by instantiating its contract attribute. */
11860
11861static void
11862tsubst_contract_attribute (tree decl, tree t, tree args,
11863 tsubst_flags_t complain, tree in_decl)
11864{
11865 /* For non-specializations, adjust the current declaration to the most general
11866 version of in_decl. Because we defer the instantiation of contracts as long
11867 as possible, they are still written in terms of the parameters (and return
11868 type) of the most general template. */
11869 tree tmpl = DECL_TI_TEMPLATE (in_decl);
11870 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
11871 in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
11872 local_specialization_stack specs (lss_copy);
11873 register_parameter_specializations (in_decl, decl);
11874
11875 /* Get the contract to be instantiated. */
11876 tree contract = CONTRACT_STATEMENT (t);
11877
11878 /* Use the complete set of template arguments for instantiation. The
11879 contract may not have been instantiated and still refer to outer levels
11880 of template parameters. */
11881 args = DECL_TI_ARGS (decl);
11882
11883 /* For member functions, make this available for semantic analysis. */
11884 tree save_ccp = current_class_ptr;
11885 tree save_ccr = current_class_ref;
11886 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
11887 {
11888 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11889 tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
11890 inject_this_parameter (this_type, cp_type_quals (this_type));
11891 }
11892
11893 contract = tsubst_contract (decl, t: contract, args, complain, in_decl);
11894
11895 current_class_ptr = save_ccp;
11896 current_class_ref = save_ccr;
11897
11898 /* Rebuild the attribute. */
11899 TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
11900}
11901
11902/* Rebuild the attribute list for DECL, substituting into contracts
11903 as needed. */
11904
11905void
11906tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
11907{
11908 tree list = copy_list (DECL_ATTRIBUTES (decl));
11909 for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
11910 {
11911 if (cxx_contract_attribute_p (attr))
11912 tsubst_contract_attribute (decl, t: attr, args, complain, in_decl);
11913 }
11914 DECL_ATTRIBUTES (decl) = list;
11915}
11916
11917/* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11918 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11919
11920static tree
11921tsubst_attribute (tree t, tree *decl_p, tree args,
11922 tsubst_flags_t complain, tree in_decl)
11923{
11924 gcc_assert (ATTR_IS_DEPENDENT (t));
11925
11926 /* Note that contract attributes are never substituted from this function.
11927 Their instantiation is triggered by regenerate_from_template_decl when
11928 we instantiate the body of the function. */
11929
11930 tree val = TREE_VALUE (t);
11931 if (val == NULL_TREE)
11932 /* Nothing to do. */;
11933 else if ((flag_openmp || flag_openmp_simd)
11934 && is_attribute_p (attr_name: "omp declare simd",
11935 ident: get_attribute_name (t)))
11936 {
11937 tree clauses = TREE_VALUE (val);
11938 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11939 complain, in_decl);
11940 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11941 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11942 tree parms = DECL_ARGUMENTS (*decl_p);
11943 clauses
11944 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11945 if (clauses)
11946 val = build_tree_list (NULL_TREE, clauses);
11947 else
11948 val = NULL_TREE;
11949 }
11950 else if (flag_openmp
11951 && is_attribute_p (attr_name: "omp declare variant base",
11952 ident: get_attribute_name (t)))
11953 {
11954 ++cp_unevaluated_operand;
11955 tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
11956 --cp_unevaluated_operand;
11957 tree chain = TREE_CHAIN (val);
11958 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11959 tree ctx = copy_list (TREE_VALUE (val));
11960 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
11961 {
11962 enum omp_tss_code set = OMP_TSS_CODE (tss);
11963 tree selectors = NULL_TREE;
11964 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
11965 ts = TREE_CHAIN (ts))
11966 {
11967 tree properties = NULL_TREE;
11968 tree scoreval = NULL_TREE;
11969 /* FIXME: The body of this loop should really be dispatching
11970 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
11971 of having hard-wired knowledge of specific selectors. */
11972 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
11973 && set == OMP_TRAIT_SET_CONSTRUCT)
11974 {
11975 tree clauses = OMP_TS_PROPERTIES (ts);
11976 clauses = tsubst_omp_clauses (clauses,
11977 C_ORT_OMP_DECLARE_SIMD, args,
11978 complain, in_decl);
11979 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11980 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11981 properties = clauses;
11982 }
11983 else
11984 {
11985 tree v = OMP_TS_SCORE (ts);
11986 if (v)
11987 {
11988 v = tsubst_expr (v, args, complain, in_decl);
11989 v = fold_non_dependent_expr (v);
11990 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11991 || TREE_CODE (v) != INTEGER_CST)
11992 {
11993 location_t loc
11994 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
11995 or_loc: match_loc);
11996 error_at (loc, "score argument must be "
11997 "constant integer expression");
11998 return NULL_TREE;
11999 }
12000 else if (tree_int_cst_sgn (v) < 0)
12001 {
12002 location_t loc
12003 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
12004 or_loc: match_loc);
12005 error_at (loc, "score argument must be "
12006 "non-negative");
12007 return NULL_TREE;
12008 }
12009 scoreval = v;
12010 }
12011 properties = copy_list (OMP_TS_PROPERTIES (ts));
12012 for (tree p = properties; p; p = TREE_CHAIN (p))
12013 if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
12014 continue;
12015 else if (OMP_TP_VALUE (p))
12016 {
12017 bool allow_string
12018 = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
12019 || set != OMP_TRAIT_SET_USER);
12020 tree v = OMP_TP_VALUE (p);
12021 if (TREE_CODE (v) == STRING_CST && allow_string)
12022 continue;
12023 v = tsubst_expr (v, args, complain, in_decl);
12024 v = fold_non_dependent_expr (v);
12025 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
12026 || !tree_fits_shwi_p (v))
12027 {
12028 location_t loc
12029 = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
12030 or_loc: match_loc);
12031 if (allow_string)
12032 error_at (loc, "property must be constant "
12033 "integer expression or string "
12034 "literal");
12035 else
12036 error_at (loc, "property must be constant "
12037 "integer expression");
12038 return NULL_TREE;
12039 }
12040 OMP_TP_VALUE (p) = v;
12041 }
12042 }
12043 selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
12044 properties, selectors);
12045 }
12046 OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
12047 }
12048 val = tree_cons (varid, ctx, chain);
12049 }
12050 /* If the first attribute argument is an identifier, don't
12051 pass it through tsubst. Attributes like mode, format,
12052 cleanup and several target specific attributes expect it
12053 unmodified. */
12054 else if (attribute_takes_identifier_p (get_attribute_name (t)))
12055 {
12056 tree chain
12057 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
12058 if (chain != TREE_CHAIN (val))
12059 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
12060 }
12061 else if (PACK_EXPANSION_P (val))
12062 {
12063 /* An attribute pack expansion. */
12064 tree purp = TREE_PURPOSE (t);
12065 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
12066 if (pack == error_mark_node)
12067 return error_mark_node;
12068 int len = TREE_VEC_LENGTH (pack);
12069 tree list = NULL_TREE;
12070 tree *q = &list;
12071 for (int i = 0; i < len; ++i)
12072 {
12073 tree elt = TREE_VEC_ELT (pack, i);
12074 *q = build_tree_list (purp, elt);
12075 q = &TREE_CHAIN (*q);
12076 }
12077 return list;
12078 }
12079 else
12080 val = tsubst_expr (val, args, complain, in_decl);
12081
12082 if (val == error_mark_node)
12083 return error_mark_node;
12084 if (val != TREE_VALUE (t))
12085 return build_tree_list (TREE_PURPOSE (t), val);
12086 return t;
12087}
12088
12089/* Instantiate any dependent attributes in ATTRIBUTES, returning either it
12090 unchanged or a new TREE_LIST chain. */
12091
12092static tree
12093tsubst_attributes (tree attributes, tree args,
12094 tsubst_flags_t complain, tree in_decl)
12095{
12096 tree last_dep = NULL_TREE;
12097
12098 for (tree t = attributes; t; t = TREE_CHAIN (t))
12099 if (ATTR_IS_DEPENDENT (t))
12100 {
12101 last_dep = t;
12102 attributes = copy_list (attributes);
12103 break;
12104 }
12105
12106 if (last_dep)
12107 for (tree *p = &attributes; *p; )
12108 {
12109 tree t = *p;
12110 if (ATTR_IS_DEPENDENT (t))
12111 {
12112 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
12113 if (subst != t)
12114 {
12115 *p = subst;
12116 while (*p)
12117 p = &TREE_CHAIN (*p);
12118 *p = TREE_CHAIN (t);
12119 continue;
12120 }
12121 }
12122 p = &TREE_CHAIN (*p);
12123 }
12124
12125 return attributes;
12126}
12127
12128/* Apply any attributes which had to be deferred until instantiation
12129 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
12130 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
12131 false on error. */
12132
12133static bool
12134apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
12135 tree args, tsubst_flags_t complain, tree in_decl)
12136{
12137 tree t;
12138 tree *p;
12139
12140 if (attributes == NULL_TREE)
12141 return true;
12142
12143 if (DECL_P (*decl_p))
12144 {
12145 if (TREE_TYPE (*decl_p) == error_mark_node)
12146 return false;
12147 p = &DECL_ATTRIBUTES (*decl_p);
12148 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
12149 to our attributes parameter. */
12150 gcc_assert (*p == attributes);
12151 }
12152 else
12153 {
12154 p = &TYPE_ATTRIBUTES (*decl_p);
12155 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
12156 lookup_template_class_1, and should be preserved. */
12157 gcc_assert (*p != attributes);
12158 while (*p)
12159 p = &TREE_CHAIN (*p);
12160 }
12161
12162 /* save_template_attributes puts the dependent attributes at the beginning of
12163 the list; find the non-dependent ones. */
12164 for (t = attributes; t; t = TREE_CHAIN (t))
12165 if (!ATTR_IS_DEPENDENT (t))
12166 break;
12167 tree nondep = t;
12168
12169 /* Apply any non-dependent attributes. */
12170 *p = nondep;
12171
12172 if (nondep == attributes)
12173 return true;
12174
12175 /* And then any dependent ones. */
12176 tree late_attrs = NULL_TREE;
12177 tree *q = &late_attrs;
12178 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
12179 {
12180 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
12181 if (*q == error_mark_node)
12182 return false;
12183 if (*q == t)
12184 {
12185 *q = copy_node (t);
12186 TREE_CHAIN (*q) = NULL_TREE;
12187 }
12188 while (*q)
12189 q = &TREE_CHAIN (*q);
12190 }
12191
12192 /* cplus_decl_attributes can add some attributes implicitly. For templates,
12193 those attributes should have been added already when those templates were
12194 parsed, and shouldn't be added based on from which context they are
12195 first time instantiated. */
12196 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
12197 auto o2 = make_temp_override (optimization_current_node,
12198 optimization_default_node);
12199 auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
12200 auto o4 = make_temp_override (var&: scope_chain->omp_declare_target_attribute,
12201 NULL);
12202 auto o5 = make_temp_override (var&: scope_chain->omp_begin_assumes, NULL);
12203
12204 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
12205
12206 return true;
12207}
12208
12209/* The template TMPL is being instantiated with the template arguments TARGS.
12210 Perform the access checks that we deferred when parsing the template. */
12211
12212static void
12213perform_instantiation_time_access_checks (tree tmpl, tree targs)
12214{
12215 unsigned i;
12216 deferred_access_check *chk;
12217
12218 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
12219 return;
12220
12221 if (vec<deferred_access_check, va_gc> *access_checks
12222 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
12223 FOR_EACH_VEC_ELT (*access_checks, i, chk)
12224 {
12225 tree decl = chk->decl;
12226 tree diag_decl = chk->diag_decl;
12227 tree type_scope = TREE_TYPE (chk->binfo);
12228
12229 if (uses_template_parms (t: type_scope))
12230 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
12231
12232 /* Make access check error messages point to the location
12233 of the use of the typedef. */
12234 iloc_sentinel ils (chk->loc);
12235 perform_or_defer_access_check (TYPE_BINFO (type_scope),
12236 decl, diag_decl, tf_warning_or_error);
12237 }
12238}
12239
12240tree
12241instantiate_class_template (tree type)
12242{
12243 auto_timevar tv (TV_TEMPLATE_INST);
12244
12245 tree templ, args, pattern, t, member;
12246 tree typedecl;
12247 tree pbinfo;
12248 tree base_list;
12249 unsigned int saved_maximum_field_alignment;
12250 tree fn_context;
12251
12252 if (type == error_mark_node)
12253 return error_mark_node;
12254
12255 if (COMPLETE_OR_OPEN_TYPE_P (type)
12256 || uses_template_parms (t: type))
12257 return type;
12258
12259 /* Figure out which template is being instantiated. */
12260 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
12261 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
12262
12263 /* Mark the type as in the process of being defined. */
12264 TYPE_BEING_DEFINED (type) = 1;
12265
12266 /* We may be in the middle of deferred access check. Disable
12267 it now. */
12268 deferring_access_check_sentinel acs (dk_no_deferred);
12269
12270 /* Determine what specialization of the original template to
12271 instantiate. */
12272 t = most_specialized_partial_spec (type, tf_warning_or_error);
12273 if (t == error_mark_node)
12274 return error_mark_node;
12275 else if (t)
12276 {
12277 /* This TYPE is actually an instantiation of a partial
12278 specialization. We replace the innermost set of ARGS with
12279 the arguments appropriate for substitution. For example,
12280 given:
12281
12282 template <class T> struct S {};
12283 template <class T> struct S<T*> {};
12284
12285 and supposing that we are instantiating S<int*>, ARGS will
12286 presently be {int*} -- but we need {int}. */
12287 pattern = TREE_TYPE (TI_TEMPLATE (t));
12288 args = TI_ARGS (t);
12289 }
12290 else
12291 {
12292 pattern = TREE_TYPE (templ);
12293 args = CLASSTYPE_TI_ARGS (type);
12294 }
12295
12296 /* If the template we're instantiating is incomplete, then clearly
12297 there's nothing we can do. */
12298 if (!COMPLETE_TYPE_P (pattern))
12299 {
12300 /* We can try again later. */
12301 TYPE_BEING_DEFINED (type) = 0;
12302 return type;
12303 }
12304
12305 /* If we've recursively instantiated too many templates, stop. */
12306 if (! push_tinst_level (d: type))
12307 return type;
12308
12309 int saved_unevaluated_operand = cp_unevaluated_operand;
12310 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12311
12312 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
12313 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12314 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
12315 fn_context = error_mark_node;
12316 if (!fn_context)
12317 push_to_top_level ();
12318 else
12319 {
12320 cp_unevaluated_operand = 0;
12321 c_inhibit_evaluation_warnings = 0;
12322 }
12323
12324 mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
12325
12326 /* Use #pragma pack from the template context. */
12327 saved_maximum_field_alignment = maximum_field_alignment;
12328 maximum_field_alignment = TYPE_PRECISION (pattern);
12329
12330 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
12331
12332 /* Set the input location to the most specialized template definition.
12333 This is needed if tsubsting causes an error. */
12334 typedecl = TYPE_MAIN_DECL (pattern);
12335 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
12336 DECL_SOURCE_LOCATION (typedecl);
12337
12338 set_instantiating_module (TYPE_NAME (type));
12339
12340 TYPE_PACKED (type) = TYPE_PACKED (pattern);
12341 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
12342 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
12343 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
12344 if (ANON_AGGR_TYPE_P (pattern))
12345 SET_ANON_AGGR_TYPE_P (type);
12346 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
12347 {
12348 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
12349 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
12350 /* Adjust visibility for template arguments. */
12351 determine_visibility (TYPE_MAIN_DECL (type));
12352 }
12353 if (CLASS_TYPE_P (type))
12354 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
12355
12356 pbinfo = TYPE_BINFO (pattern);
12357
12358 /* We should never instantiate a nested class before its enclosing
12359 class; we need to look up the nested class by name before we can
12360 instantiate it, and that lookup should instantiate the enclosing
12361 class. */
12362 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
12363 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
12364
12365 base_list = NULL_TREE;
12366 /* Defer access checking while we substitute into the types named in
12367 the base-clause. */
12368 push_deferring_access_checks (dk_deferred);
12369 if (BINFO_N_BASE_BINFOS (pbinfo))
12370 {
12371 tree pbase_binfo;
12372 int i;
12373
12374 /* Substitute into each of the bases to determine the actual
12375 basetypes. */
12376 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
12377 {
12378 tree base;
12379 tree access = BINFO_BASE_ACCESS (pbinfo, i);
12380 tree expanded_bases = NULL_TREE;
12381 int idx, len = 1;
12382
12383 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
12384 {
12385 expanded_bases =
12386 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
12387 args, tf_error, NULL_TREE);
12388 if (expanded_bases == error_mark_node)
12389 continue;
12390
12391 len = TREE_VEC_LENGTH (expanded_bases);
12392 }
12393
12394 for (idx = 0; idx < len; idx++)
12395 {
12396 if (expanded_bases)
12397 /* Extract the already-expanded base class. */
12398 base = TREE_VEC_ELT (expanded_bases, idx);
12399 else
12400 /* Substitute to figure out the base class. */
12401 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
12402 NULL_TREE);
12403
12404 if (base == error_mark_node)
12405 continue;
12406
12407 base_list = tree_cons (access, base, base_list);
12408 if (BINFO_VIRTUAL_P (pbase_binfo))
12409 TREE_TYPE (base_list) = integer_type_node;
12410 }
12411 }
12412
12413 /* The list is now in reverse order; correct that. */
12414 base_list = nreverse (base_list);
12415 }
12416 /* Now call xref_basetypes to set up all the base-class
12417 information. */
12418 xref_basetypes (type, base_list);
12419
12420 apply_late_template_attributes (decl_p: &type, TYPE_ATTRIBUTES (pattern),
12421 attr_flags: (int) ATTR_FLAG_TYPE_IN_PLACE,
12422 args, complain: tf_error, NULL_TREE);
12423 fixup_attribute_variants (type);
12424
12425 /* Now that our base classes are set up, enter the scope of the
12426 class, so that name lookups into base classes, etc. will work
12427 correctly. This is precisely analogous to what we do in
12428 begin_class_definition when defining an ordinary non-template
12429 class, except we also need to push the enclosing classes. */
12430 push_nested_class (type);
12431
12432 /* Now check accessibility of the types named in its base-clause,
12433 relative to the scope of the class. */
12434 pop_to_parent_deferring_access_checks ();
12435
12436 /* A vector to hold members marked with attribute used. */
12437 auto_vec<tree> used;
12438
12439 /* Now members are processed in the order of declaration. */
12440 for (member = CLASSTYPE_DECL_LIST (pattern);
12441 member; member = TREE_CHAIN (member))
12442 {
12443 tree t = TREE_VALUE (member);
12444
12445 if (TREE_PURPOSE (member))
12446 {
12447 if (TYPE_P (t))
12448 {
12449 if (LAMBDA_TYPE_P (t))
12450 /* A closure type for a lambda in an NSDMI or default argument.
12451 Ignore it; it will be regenerated when needed. */
12452 continue;
12453
12454 /* If the member is a class template, we've
12455 already substituted its type. */
12456 if (CLASS_TYPE_P (t)
12457 && CLASSTYPE_IS_TEMPLATE (t))
12458 continue;
12459
12460 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
12461 if (newtag == error_mark_node)
12462 continue;
12463
12464 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
12465 {
12466 tree name = TYPE_IDENTIFIER (t);
12467
12468 /* Now, install the tag. We don't use pushtag
12469 because that does too much work -- creating an
12470 implicit typedef, which we've already done. */
12471 set_identifier_type_value (name, TYPE_NAME (newtag));
12472 maybe_add_class_template_decl_list (type, newtag, false);
12473 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12474 determine_visibility (TYPE_NAME (newtag));
12475 }
12476 }
12477 else if (DECL_DECLARES_FUNCTION_P (t))
12478 {
12479 tree r;
12480
12481 if (TREE_CODE (t) == TEMPLATE_DECL)
12482 ++processing_template_decl;
12483 r = tsubst (t, args, tf_error, NULL_TREE);
12484 if (TREE_CODE (t) == TEMPLATE_DECL)
12485 --processing_template_decl;
12486
12487 set_current_access_from_decl (r);
12488 finish_member_declaration (r);
12489 /* Instantiate members marked with attribute used. */
12490 if (r != error_mark_node && DECL_PRESERVE_P (r))
12491 used.safe_push (obj: r);
12492 if (TREE_CODE (r) == FUNCTION_DECL
12493 && DECL_OMP_DECLARE_REDUCTION_P (r))
12494 cp_check_omp_declare_reduction (r);
12495 }
12496 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12497 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12498 /* A closure type for a lambda in an NSDMI or default argument.
12499 Ignore it; it will be regenerated when needed. */;
12500 else
12501 {
12502 /* Build new TYPE_FIELDS. */
12503 if (TREE_CODE (t) == STATIC_ASSERT)
12504 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
12505 else if (TREE_CODE (t) != CONST_DECL)
12506 {
12507 tree r;
12508 tree vec = NULL_TREE;
12509 int len = 1;
12510
12511 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12512 /* The file and line for this declaration, to
12513 assist in error message reporting. Since we
12514 called push_tinst_level above, we don't need to
12515 restore these. */
12516 input_location = DECL_SOURCE_LOCATION (t);
12517
12518 if (TREE_CODE (t) == TEMPLATE_DECL)
12519 ++processing_template_decl;
12520 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12521 if (TREE_CODE (t) == TEMPLATE_DECL)
12522 --processing_template_decl;
12523
12524 if (TREE_CODE (r) == TREE_VEC)
12525 {
12526 /* A capture pack became multiple fields. */
12527 vec = r;
12528 len = TREE_VEC_LENGTH (vec);
12529 }
12530
12531 for (int i = 0; i < len; ++i)
12532 {
12533 if (vec)
12534 r = TREE_VEC_ELT (vec, i);
12535 if (VAR_P (r))
12536 {
12537 /* In [temp.inst]:
12538
12539 [t]he initialization (and any associated
12540 side-effects) of a static data member does
12541 not occur unless the static data member is
12542 itself used in a way that requires the
12543 definition of the static data member to
12544 exist.
12545
12546 Therefore, we do not substitute into the
12547 initialized for the static data member here. */
12548 finish_static_data_member_decl
12549 (r,
12550 /*init=*/NULL_TREE,
12551 /*init_const_expr_p=*/false,
12552 /*asmspec_tree=*/NULL_TREE,
12553 /*flags=*/0);
12554 /* Instantiate members marked with attribute used. */
12555 if (r != error_mark_node && DECL_PRESERVE_P (r))
12556 used.safe_push (obj: r);
12557 }
12558 else if (TREE_CODE (r) == FIELD_DECL)
12559 {
12560 /* Determine whether R has a valid type and can be
12561 completed later. If R is invalid, then its type
12562 is replaced by error_mark_node. */
12563 tree rtype = TREE_TYPE (r);
12564 if (can_complete_type_without_circularity (type: rtype))
12565 complete_type (rtype);
12566
12567 if (!complete_or_array_type_p (type: rtype))
12568 {
12569 /* If R's type couldn't be completed and
12570 it isn't a flexible array member (whose
12571 type is incomplete by definition) give
12572 an error. */
12573 cxx_incomplete_type_error (value: r, type: rtype);
12574 TREE_TYPE (r) = error_mark_node;
12575 }
12576 else if (TREE_CODE (rtype) == ARRAY_TYPE
12577 && TYPE_DOMAIN (rtype) == NULL_TREE
12578 && (TREE_CODE (type) == UNION_TYPE
12579 || TREE_CODE (type) == QUAL_UNION_TYPE))
12580 {
12581 error ("flexible array member %qD in union", r);
12582 TREE_TYPE (r) = error_mark_node;
12583 }
12584 else if (!verify_type_context (input_location,
12585 TCTX_FIELD, rtype))
12586 TREE_TYPE (r) = error_mark_node;
12587 }
12588
12589 /* If it is a TYPE_DECL for a class-scoped
12590 ENUMERAL_TYPE, such a thing will already have
12591 been added to the field list by tsubst_enum
12592 in finish_member_declaration case above. */
12593 if (!(TREE_CODE (r) == TYPE_DECL
12594 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12595 && DECL_ARTIFICIAL (r)))
12596 {
12597 set_current_access_from_decl (r);
12598 finish_member_declaration (r);
12599 }
12600 }
12601 }
12602 }
12603 }
12604 else
12605 {
12606 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12607 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12608 {
12609 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12610
12611 tree friend_type = t;
12612 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12613 {
12614 /* template <class T> friend class C; */
12615 friend_type = tsubst_friend_class (friend_tmpl: friend_type, args);
12616 }
12617 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12618 {
12619 /* template <class T> friend class C::D; */
12620 friend_type = tsubst (friend_type, args,
12621 tf_warning_or_error, NULL_TREE);
12622 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12623 friend_type = TREE_TYPE (friend_type);
12624 }
12625 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12626 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12627 {
12628 /* This could be either
12629
12630 friend class T::C;
12631
12632 when dependent_type_p is false or
12633
12634 template <class U> friend class T::C;
12635
12636 otherwise. */
12637 /* Bump processing_template_decl in case this is something like
12638 template <class T> friend struct A<T>::B. */
12639 ++processing_template_decl;
12640 friend_type = tsubst (friend_type, args,
12641 tf_warning_or_error, NULL_TREE);
12642 --processing_template_decl;
12643 }
12644 else if (uses_template_parms (t: friend_type))
12645 /* friend class C<T>; */
12646 friend_type = tsubst (friend_type, args,
12647 tf_warning_or_error, NULL_TREE);
12648
12649 /* Otherwise it's
12650
12651 friend class C;
12652
12653 where C is already declared or
12654
12655 friend class C<int>;
12656
12657 We don't have to do anything in these cases. */
12658
12659 if (friend_type != error_mark_node)
12660 make_friend_class (type, friend_type, /*complain=*/false);
12661 }
12662 else
12663 {
12664 /* Build new DECL_FRIENDLIST. */
12665 tree r;
12666
12667 /* The file and line for this declaration, to
12668 assist in error message reporting. Since we
12669 called push_tinst_level above, we don't need to
12670 restore these. */
12671 input_location = DECL_SOURCE_LOCATION (t);
12672
12673 if (TREE_CODE (t) == TEMPLATE_DECL)
12674 {
12675 ++processing_template_decl;
12676 push_deferring_access_checks (dk_no_check);
12677 }
12678
12679 r = tsubst_friend_function (decl: t, args);
12680 add_friend (type, r, /*complain=*/false);
12681 if (TREE_CODE (t) == TEMPLATE_DECL)
12682 {
12683 pop_deferring_access_checks ();
12684 --processing_template_decl;
12685 }
12686 }
12687 }
12688 }
12689
12690 if (fn_context)
12691 {
12692 /* Restore these before substituting into the lambda capture
12693 initializers. */
12694 cp_unevaluated_operand = saved_unevaluated_operand;
12695 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12696 }
12697
12698 /* Set the file and line number information to whatever is given for
12699 the class itself. This puts error messages involving generated
12700 implicit functions at a predictable point, and the same point
12701 that would be used for non-template classes. */
12702 input_location = DECL_SOURCE_LOCATION (typedecl);
12703
12704 unreverse_member_declarations (type);
12705 finish_struct_1 (type);
12706 TYPE_BEING_DEFINED (type) = 0;
12707
12708 /* Remember if instantiating this class ran into errors, so we can avoid
12709 instantiating member functions in limit_bad_template_recursion. We set
12710 this flag even if the problem was in another instantiation triggered by
12711 this one, as that will likely also cause trouble for member functions. */
12712 if (errorcount + sorrycount > current_tinst_level->errors)
12713 CLASSTYPE_ERRONEOUS (type) = true;
12714
12715 /* We don't instantiate default arguments for member functions. 14.7.1:
12716
12717 The implicit instantiation of a class template specialization causes
12718 the implicit instantiation of the declarations, but not of the
12719 definitions or default arguments, of the class member functions,
12720 member classes, static data members and member templates.... */
12721
12722 perform_instantiation_time_access_checks (tmpl: pattern, targs: args);
12723 perform_deferred_access_checks (tf_warning_or_error);
12724
12725 /* Now that we've gone through all the members, instantiate those
12726 marked with attribute used. We must do this in the context of
12727 the class -- not the context we pushed from, as that might be
12728 inside a template and change the behaviour of mark_used. */
12729 for (tree x : used)
12730 mark_used (x);
12731
12732 pop_nested_class ();
12733 maximum_field_alignment = saved_maximum_field_alignment;
12734 if (!fn_context)
12735 pop_from_top_level ();
12736 pop_tinst_level ();
12737
12738 /* The vtable for a template class can be emitted in any translation
12739 unit in which the class is instantiated. When there is no key
12740 method, however, finish_struct_1 will already have added TYPE to
12741 the keyed_classes. */
12742 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12743 vec_safe_push (v&: keyed_classes, obj: type);
12744
12745 return type;
12746}
12747
12748tree
12749tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12750{
12751 tree r;
12752
12753 if (!t)
12754 r = t;
12755 else if (TYPE_P (t))
12756 r = tsubst (t, args, complain, in_decl);
12757 else
12758 {
12759 if (!(complain & tf_warning))
12760 ++c_inhibit_evaluation_warnings;
12761 r = tsubst_expr (t, args, complain, in_decl);
12762 if (!(complain & tf_warning))
12763 --c_inhibit_evaluation_warnings;
12764 }
12765
12766 return r;
12767}
12768
12769/* Given a function parameter pack TMPL_PARM and some function parameters
12770 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12771 and set *SPEC_P to point at the next point in the list. */
12772
12773tree
12774extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12775{
12776 /* Collect all of the extra "packed" parameters into an
12777 argument pack. */
12778 tree argpack;
12779 tree spec_parm = *spec_p;
12780 int len;
12781
12782 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12783 if (tmpl_parm
12784 && !function_parameter_expanded_from_pack_p (param_decl: spec_parm, pack: tmpl_parm))
12785 break;
12786
12787 spec_parm = *spec_p;
12788 if (len == 1 && DECL_PACK_P (spec_parm))
12789 {
12790 /* The instantiation is still a parameter pack; don't wrap it in a
12791 NONTYPE_ARGUMENT_PACK. */
12792 argpack = spec_parm;
12793 spec_parm = DECL_CHAIN (spec_parm);
12794 }
12795 else
12796 {
12797 /* Fill in PARMVEC with all of the parameters. */
12798 tree parmvec = make_tree_vec (len);
12799 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12800 for (int i = 0; i < len; i++)
12801 {
12802 tree elt = spec_parm;
12803 if (DECL_PACK_P (elt))
12804 elt = make_pack_expansion (arg: elt);
12805 TREE_VEC_ELT (parmvec, i) = elt;
12806 spec_parm = DECL_CHAIN (spec_parm);
12807 }
12808
12809 /* Build the argument packs. */
12810 ARGUMENT_PACK_ARGS (argpack) = parmvec;
12811 }
12812 *spec_p = spec_parm;
12813
12814 return argpack;
12815}
12816
12817/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12818 NONTYPE_ARGUMENT_PACK. */
12819
12820static tree
12821make_fnparm_pack (tree spec_parm)
12822{
12823 return extract_fnparm_pack (NULL_TREE, spec_p: &spec_parm);
12824}
12825
12826/* Return 1 if the Ith element of the argument pack ARG_PACK is a
12827 pack expansion with no extra args, 2 if it has extra args, or 0
12828 if it is not a pack expansion. */
12829
12830static int
12831argument_pack_element_is_expansion_p (tree arg_pack, int i)
12832{
12833 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12834 /* We're being called before this happens in tsubst_pack_expansion. */
12835 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12836 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12837 if (i >= TREE_VEC_LENGTH (vec))
12838 return 0;
12839 tree elt = TREE_VEC_ELT (vec, i);
12840 if (DECL_P (elt))
12841 /* A decl pack is itself an expansion. */
12842 elt = TREE_TYPE (elt);
12843 if (!PACK_EXPANSION_P (elt))
12844 return 0;
12845 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12846 return 2;
12847 return 1;
12848}
12849
12850
12851/* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12852
12853static tree
12854make_argument_pack_select (tree arg_pack, unsigned index)
12855{
12856 tree aps = make_node (ARGUMENT_PACK_SELECT);
12857
12858 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12859 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12860
12861 return aps;
12862}
12863
12864/* This is a subroutine of tsubst_pack_expansion.
12865
12866 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12867 mechanism to store the (non complete list of) arguments of the
12868 substitution and return a non substituted pack expansion, in order
12869 to wait for when we have enough arguments to really perform the
12870 substitution. */
12871
12872static bool
12873use_pack_expansion_extra_args_p (tree t,
12874 tree parm_packs,
12875 int arg_pack_len,
12876 bool has_empty_arg)
12877{
12878 if (has_empty_arg
12879 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
12880 return true;
12881
12882 /* If one pack has an expansion and another pack has a normal
12883 argument or if one pack has an empty argument and an another
12884 one hasn't then tsubst_pack_expansion cannot perform the
12885 substitution and need to fall back on the
12886 PACK_EXPANSION_EXTRA mechanism. */
12887 if (parm_packs == NULL_TREE)
12888 return false;
12889 else if (has_empty_arg)
12890 {
12891 /* If all the actual packs are pack expansions, we can still
12892 subsitute directly. */
12893 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12894 {
12895 tree a = TREE_VALUE (p);
12896 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12897 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12898 a = ARGUMENT_PACK_ARGS (a);
12899 if (TREE_VEC_LENGTH (a) == 1)
12900 a = TREE_VEC_ELT (a, 0);
12901 if (PACK_EXPANSION_P (a))
12902 continue;
12903 return true;
12904 }
12905 return false;
12906 }
12907
12908 for (int i = 0 ; i < arg_pack_len; ++i)
12909 {
12910 bool has_expansion_arg = false;
12911 bool has_non_expansion_arg = false;
12912 for (tree parm_pack = parm_packs;
12913 parm_pack;
12914 parm_pack = TREE_CHAIN (parm_pack))
12915 {
12916 tree arg = TREE_VALUE (parm_pack);
12917
12918 int exp = argument_pack_element_is_expansion_p (arg_pack: arg, i);
12919 if (exp == 2)
12920 /* We can't substitute a pack expansion with extra args into
12921 our pattern. */
12922 return true;
12923 else if (exp)
12924 has_expansion_arg = true;
12925 else
12926 has_non_expansion_arg = true;
12927 }
12928
12929 if (has_expansion_arg && has_non_expansion_arg)
12930 {
12931 gcc_checking_assert (false);
12932 return true;
12933 }
12934 }
12935 return false;
12936}
12937
12938/* [temp.variadic]/6 says that:
12939
12940 The instantiation of a pack expansion [...]
12941 produces a list E1,E2, ..., En, where N is the number of elements
12942 in the pack expansion parameters.
12943
12944 This subroutine of tsubst_pack_expansion produces one of these Ei.
12945
12946 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12947 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12948 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12949 INDEX is the index 'i' of the element Ei to produce. ARGS,
12950 COMPLAIN, and IN_DECL are the same parameters as for the
12951 tsubst_pack_expansion function.
12952
12953 The function returns the resulting Ei upon successful completion,
12954 or error_mark_node.
12955
12956 Note that this function possibly modifies the ARGS parameter, so
12957 it's the responsibility of the caller to restore it. */
12958
12959static tree
12960gen_elem_of_pack_expansion_instantiation (tree pattern,
12961 tree parm_packs,
12962 unsigned index,
12963 tree args /* This parm gets
12964 modified. */,
12965 tsubst_flags_t complain,
12966 tree in_decl)
12967{
12968 tree t;
12969 bool ith_elem_is_expansion = false;
12970
12971 /* For each parameter pack, change the substitution of the parameter
12972 pack to the ith argument in its argument pack, then expand the
12973 pattern. */
12974 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12975 {
12976 tree parm = TREE_PURPOSE (pack);
12977 tree arg_pack = TREE_VALUE (pack);
12978 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12979
12980 ith_elem_is_expansion |=
12981 argument_pack_element_is_expansion_p (arg_pack, i: index);
12982
12983 /* Select the Ith argument from the pack. */
12984 if (TREE_CODE (parm) == PARM_DECL
12985 || VAR_P (parm)
12986 || TREE_CODE (parm) == FIELD_DECL)
12987 {
12988 if (index == 0)
12989 {
12990 aps = make_argument_pack_select (arg_pack, index);
12991 if (!mark_used (parm, complain) && !(complain & tf_error))
12992 return error_mark_node;
12993 register_local_specialization (spec: aps, tmpl: parm);
12994 }
12995 else
12996 aps = retrieve_local_specialization (tmpl: parm);
12997 }
12998 else
12999 {
13000 int idx, level;
13001 template_parm_level_and_index (parm, &level, &idx);
13002
13003 if (index == 0)
13004 {
13005 aps = make_argument_pack_select (arg_pack, index);
13006 /* Update the corresponding argument. */
13007 TMPL_ARG (args, level, idx) = aps;
13008 }
13009 else
13010 /* Re-use the ARGUMENT_PACK_SELECT. */
13011 aps = TMPL_ARG (args, level, idx);
13012 }
13013 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
13014 }
13015
13016 /* Substitute into the PATTERN with the (possibly altered)
13017 arguments. */
13018 if (pattern == in_decl)
13019 /* Expanding a fixed parameter pack from
13020 coerce_template_parameter_pack. */
13021 t = tsubst_decl (pattern, args, complain);
13022 else if (pattern == error_mark_node)
13023 t = error_mark_node;
13024 else if (!TYPE_P (pattern))
13025 t = tsubst_expr (pattern, args, complain, in_decl);
13026 else
13027 {
13028 t = tsubst (pattern, args, complain, in_decl);
13029 if (is_auto (t) && !ith_elem_is_expansion)
13030 /* When expanding the fake auto... pack expansion from add_capture, we
13031 need to mark that the expansion is no longer a pack. */
13032 TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
13033 }
13034
13035 /* If the Ith argument pack element is a pack expansion, then
13036 the Ith element resulting from the substituting is going to
13037 be a pack expansion as well. */
13038 if (ith_elem_is_expansion)
13039 t = make_pack_expansion (arg: t, complain);
13040
13041 return t;
13042}
13043
13044/* When the unexpanded parameter pack in a fold expression expands to an empty
13045 sequence, the value of the expression is as follows; the program is
13046 ill-formed if the operator is not listed in this table.
13047
13048 && true
13049 || false
13050 , void() */
13051
13052tree
13053expand_empty_fold (tree t, tsubst_flags_t complain)
13054{
13055 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
13056 if (!FOLD_EXPR_MODIFY_P (t))
13057 switch (code)
13058 {
13059 case TRUTH_ANDIF_EXPR:
13060 return boolean_true_node;
13061 case TRUTH_ORIF_EXPR:
13062 return boolean_false_node;
13063 case COMPOUND_EXPR:
13064 return void_node;
13065 default:
13066 break;
13067 }
13068
13069 if (complain & tf_error)
13070 error_at (location_of (t),
13071 "fold of empty expansion over %O", code);
13072 return error_mark_node;
13073}
13074
13075/* Given a fold-expression T and a current LEFT and RIGHT operand,
13076 form an expression that combines the two terms using the
13077 operator of T. */
13078
13079static tree
13080fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
13081{
13082 tree_code code = FOLD_EXPR_OP (t);
13083
13084 tree lookups = templated_operator_saved_lookups (t);
13085
13086 // Handle compound assignment operators.
13087 if (FOLD_EXPR_MODIFY_P (t))
13088 return build_x_modify_expr (input_location, left, code, right,
13089 lookups, complain);
13090
13091 warning_sentinel s(warn_parentheses);
13092 switch (code)
13093 {
13094 case COMPOUND_EXPR:
13095 return build_x_compound_expr (input_location, left, right,
13096 lookups, complain);
13097 default:
13098 return build_x_binary_op (input_location, code,
13099 left, TREE_CODE (left),
13100 right, TREE_CODE (right),
13101 lookups, /*overload=*/NULL,
13102 complain);
13103 }
13104}
13105
13106/* Substitute ARGS into the pack of a fold expression T. */
13107
13108static inline tree
13109tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13110{
13111 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
13112}
13113
13114/* Substitute ARGS into the pack of a fold expression T. */
13115
13116static inline tree
13117tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13118{
13119 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
13120}
13121
13122/* Expand a PACK of arguments into a grouped as left fold.
13123 Given a pack containing elements A0, A1, ..., An and an
13124 operator @, this builds the expression:
13125
13126 ((A0 @ A1) @ A2) ... @ An
13127
13128 Note that PACK must not be empty.
13129
13130 The operator is defined by the original fold expression T. */
13131
13132static tree
13133expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
13134{
13135 tree left = TREE_VEC_ELT (pack, 0);
13136 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
13137 {
13138 tree right = TREE_VEC_ELT (pack, i);
13139 left = fold_expression (t, left, right, complain);
13140 }
13141 return left;
13142}
13143
13144/* Substitute into a unary left fold expression. */
13145
13146static tree
13147tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
13148 tree in_decl)
13149{
13150 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13151 if (pack == error_mark_node)
13152 return error_mark_node;
13153 if (PACK_EXPANSION_P (pack))
13154 {
13155 tree r = copy_node (t);
13156 FOLD_EXPR_PACK (r) = pack;
13157 return r;
13158 }
13159 if (TREE_VEC_LENGTH (pack) == 0)
13160 return expand_empty_fold (t, complain);
13161 else
13162 return expand_left_fold (t, pack, complain);
13163}
13164
13165/* Substitute into a binary left fold expression.
13166
13167 Do ths by building a single (non-empty) vector of argumnts and
13168 building the expression from those elements. */
13169
13170static tree
13171tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
13172 tree in_decl)
13173{
13174 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13175 if (pack == error_mark_node)
13176 return error_mark_node;
13177 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13178 if (init == error_mark_node)
13179 return error_mark_node;
13180
13181 if (PACK_EXPANSION_P (pack))
13182 {
13183 tree r = copy_node (t);
13184 FOLD_EXPR_PACK (r) = pack;
13185 FOLD_EXPR_INIT (r) = init;
13186 return r;
13187 }
13188
13189 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
13190 TREE_VEC_ELT (vec, 0) = init;
13191 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
13192 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
13193
13194 return expand_left_fold (t, pack: vec, complain);
13195}
13196
13197/* Expand a PACK of arguments into a grouped as right fold.
13198 Given a pack containing elementns A0, A1, ..., and an
13199 operator @, this builds the expression:
13200
13201 A0@ ... (An-2 @ (An-1 @ An))
13202
13203 Note that PACK must not be empty.
13204
13205 The operator is defined by the original fold expression T. */
13206
13207tree
13208expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
13209{
13210 // Build the expression.
13211 int n = TREE_VEC_LENGTH (pack);
13212 tree right = TREE_VEC_ELT (pack, n - 1);
13213 for (--n; n != 0; --n)
13214 {
13215 tree left = TREE_VEC_ELT (pack, n - 1);
13216 right = fold_expression (t, left, right, complain);
13217 }
13218 return right;
13219}
13220
13221/* Substitute into a unary right fold expression. */
13222
13223static tree
13224tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
13225 tree in_decl)
13226{
13227 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13228 if (pack == error_mark_node)
13229 return error_mark_node;
13230 if (PACK_EXPANSION_P (pack))
13231 {
13232 tree r = copy_node (t);
13233 FOLD_EXPR_PACK (r) = pack;
13234 return r;
13235 }
13236 if (TREE_VEC_LENGTH (pack) == 0)
13237 return expand_empty_fold (t, complain);
13238 else
13239 return expand_right_fold (t, pack, complain);
13240}
13241
13242/* Substitute into a binary right fold expression.
13243
13244 Do ths by building a single (non-empty) vector of arguments and
13245 building the expression from those elements. */
13246
13247static tree
13248tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
13249 tree in_decl)
13250{
13251 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13252 if (pack == error_mark_node)
13253 return error_mark_node;
13254 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13255 if (init == error_mark_node)
13256 return error_mark_node;
13257
13258 if (PACK_EXPANSION_P (pack))
13259 {
13260 tree r = copy_node (t);
13261 FOLD_EXPR_PACK (r) = pack;
13262 FOLD_EXPR_INIT (r) = init;
13263 return r;
13264 }
13265
13266 int n = TREE_VEC_LENGTH (pack);
13267 tree vec = make_tree_vec (n + 1);
13268 for (int i = 0; i < n; ++i)
13269 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
13270 TREE_VEC_ELT (vec, n) = init;
13271
13272 return expand_right_fold (t, pack: vec, complain);
13273}
13274
13275/* Walk through the pattern of a pack expansion, adding everything in
13276 local_specializations to a list. */
13277
13278class el_data
13279{
13280public:
13281 /* Set of variables declared within the pattern. */
13282 hash_set<tree> internal;
13283 /* Set of AST nodes that have been visited by the traversal. */
13284 hash_set<tree> visited;
13285 /* List of local_specializations used within the pattern. */
13286 tree extra;
13287 tsubst_flags_t complain;
13288 /* True iff we don't want to walk into unevaluated contexts. */
13289 bool skip_unevaluated_operands = false;
13290 /* The unevaluated contexts that we avoided walking. */
13291 auto_vec<tree> skipped_trees;
13292
13293 el_data (tsubst_flags_t c)
13294 : extra (NULL_TREE), complain (c) {}
13295};
13296static tree
13297extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
13298{
13299 el_data &data = *reinterpret_cast<el_data*>(data_);
13300 tree *extra = &data.extra;
13301 tsubst_flags_t complain = data.complain;
13302
13303 if (data.skip_unevaluated_operands
13304 && unevaluated_p (TREE_CODE (*tp)))
13305 {
13306 data.skipped_trees.safe_push (obj: *tp);
13307 *walk_subtrees = 0;
13308 return NULL_TREE;
13309 }
13310
13311 if (TYPE_P (*tp) && typedef_variant_p (type: *tp))
13312 /* Remember local typedefs (85214). */
13313 tp = &TYPE_NAME (*tp);
13314
13315 if (has_extra_args_mechanism_p (t: *tp))
13316 /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
13317 potentially see a previously captured local in an evaluated context
13318 that's really only used in an unevaluated context (PR114303). This
13319 means callers of build_extra_args need to clear *_EXTRA_ARGS of the
13320 outermost tree. Nested *_EXTRA_ARGS should naturally be empty since
13321 the outermost (extra-args) tree will intercept any substitution before
13322 a nested tree can. */
13323 gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE);
13324
13325 if (TREE_CODE (*tp) == DECL_EXPR)
13326 {
13327 tree decl = DECL_EXPR_DECL (*tp);
13328 data.internal.add (k: decl);
13329 if (VAR_P (decl)
13330 && DECL_DECOMPOSITION_P (decl)
13331 && TREE_TYPE (decl) != error_mark_node)
13332 {
13333 gcc_assert (DECL_NAME (decl) == NULL_TREE);
13334 for (tree decl2 = DECL_CHAIN (decl);
13335 decl2
13336 && VAR_P (decl2)
13337 && DECL_DECOMPOSITION_P (decl2)
13338 && DECL_NAME (decl2)
13339 && TREE_TYPE (decl2) != error_mark_node;
13340 decl2 = DECL_CHAIN (decl2))
13341 {
13342 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
13343 data.internal.add (k: decl2);
13344 }
13345 }
13346 }
13347 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
13348 {
13349 /* Since we defer implicit capture, look in the parms and body. */
13350 tree fn = lambda_function (*tp);
13351 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
13352 &data.visited);
13353 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
13354 &data.visited);
13355 }
13356 else if (tree spec = retrieve_local_specialization (tmpl: *tp))
13357 {
13358 if (data.internal.contains (k: *tp))
13359 /* Don't mess with variables declared within the pattern. */
13360 return NULL_TREE;
13361 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13362 {
13363 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13364 tree args = ARGUMENT_PACK_ARGS (spec);
13365 if (TREE_VEC_LENGTH (args) == 1)
13366 {
13367 tree elt = TREE_VEC_ELT (args, 0);
13368 if (PACK_EXPANSION_P (elt))
13369 elt = PACK_EXPANSION_PATTERN (elt);
13370 if (DECL_PACK_P (elt))
13371 spec = elt;
13372 }
13373 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13374 {
13375 /* Handle lambda capture here, since we aren't doing any
13376 substitution now, and so tsubst_copy won't call
13377 process_outer_var_ref. */
13378 tree args = ARGUMENT_PACK_ARGS (spec);
13379 int len = TREE_VEC_LENGTH (args);
13380 for (int i = 0; i < len; ++i)
13381 {
13382 tree arg = TREE_VEC_ELT (args, i);
13383 tree carg = arg;
13384 if (outer_automatic_var_p (arg))
13385 carg = process_outer_var_ref (arg, complain);
13386 if (carg != arg)
13387 {
13388 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13389 proxies. */
13390 if (i == 0)
13391 {
13392 spec = copy_node (spec);
13393 args = copy_node (args);
13394 ARGUMENT_PACK_ARGS (spec) = args;
13395 register_local_specialization (spec, tmpl: *tp);
13396 }
13397 TREE_VEC_ELT (args, i) = carg;
13398 }
13399 }
13400 }
13401 }
13402 if (outer_automatic_var_p (spec))
13403 spec = process_outer_var_ref (spec, complain);
13404 *extra = tree_cons (*tp, spec, *extra);
13405 }
13406 return NULL_TREE;
13407}
13408static tree
13409extract_local_specs (tree pattern, tsubst_flags_t complain)
13410{
13411 el_data data (complain);
13412 /* Walk the pattern twice, ignoring unevaluated operands the first time
13413 around, so that if a local specialization appears in both an evaluated
13414 and unevaluated context we prefer to process it in the evaluated context
13415 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13416 context). */
13417 data.skip_unevaluated_operands = true;
13418 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13419 /* Now walk the unevaluated contexts we skipped the first time around. */
13420 data.skip_unevaluated_operands = false;
13421 for (tree t : data.skipped_trees)
13422 {
13423 data.visited.remove (k: t);
13424 cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
13425 }
13426 return data.extra;
13427}
13428
13429/* Extract any uses of local_specializations from PATTERN and add them to ARGS
13430 for use in PACK_EXPANSION_EXTRA_ARGS. */
13431
13432tree
13433build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
13434{
13435 /* Make a copy of the extra arguments so that they won't get changed
13436 out from under us. */
13437 tree extra = preserve_args (args: copy_template_args (args), /*cow_p=*/false);
13438 if (local_specializations)
13439 if (tree locals = extract_local_specs (pattern, complain))
13440 extra = tree_cons (NULL_TREE, extra, locals);
13441 return extra;
13442}
13443
13444/* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13445 normal template args to ARGS. */
13446
13447tree
13448add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
13449{
13450 if (extra && TREE_CODE (extra) == TREE_LIST)
13451 {
13452 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
13453 {
13454 /* The partial instantiation involved local declarations collected in
13455 extract_local_specs; map from the general template to our local
13456 context. */
13457 tree gen = TREE_PURPOSE (elt);
13458 tree inst = TREE_VALUE (elt);
13459 if (DECL_P (inst))
13460 if (tree local = retrieve_local_specialization (tmpl: inst))
13461 inst = local;
13462 /* else inst is already a full instantiation of the pack. */
13463 register_local_specialization (spec: inst, tmpl: gen);
13464 }
13465 gcc_assert (!TREE_PURPOSE (extra));
13466 extra = TREE_VALUE (extra);
13467 }
13468 if (uses_template_parms (t: extra))
13469 {
13470 /* This can happen after dependent substitution into a
13471 requires-expr or a lambda that uses constexpr if. */
13472 extra = tsubst_template_args (extra, args, complain, in_decl);
13473 args = add_outermost_template_args (args, extra_args: extra);
13474 }
13475 else
13476 args = add_to_template_args (args: extra, extra_args: args);
13477 return args;
13478}
13479
13480/* Substitute ARGS into T, which is an pack expansion
13481 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13482 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13483 (if only a partial substitution could be performed) or
13484 ERROR_MARK_NODE if there was an error. */
13485tree
13486tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13487 tree in_decl)
13488{
13489 tree pattern;
13490 tree pack, packs = NULL_TREE;
13491 bool unsubstituted_packs = false;
13492 int i, len = -1;
13493 tree result;
13494 bool need_local_specializations = false;
13495 int levels;
13496
13497 gcc_assert (PACK_EXPANSION_P (t));
13498 pattern = PACK_EXPANSION_PATTERN (t);
13499
13500 /* Add in any args remembered from an earlier partial instantiation. */
13501 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13502
13503 levels = TMPL_ARGS_DEPTH (args);
13504
13505 /* Determine the argument packs that will instantiate the parameter
13506 packs used in the expansion expression. While we're at it,
13507 compute the number of arguments to be expanded and make sure it
13508 is consistent. */
13509 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13510 pack = TREE_CHAIN (pack))
13511 {
13512 tree parm_pack = TREE_VALUE (pack);
13513 tree arg_pack = NULL_TREE;
13514 tree orig_arg = NULL_TREE;
13515 int level = 0;
13516
13517 if (TREE_CODE (parm_pack) == BASES)
13518 {
13519 gcc_assert (parm_pack == pattern);
13520 tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
13521 if (BASES_DIRECT (parm_pack))
13522 return calculate_direct_bases (type, complain);
13523 else
13524 return calculate_bases (type, complain);
13525 }
13526 else if (builtin_pack_call_p (call: parm_pack))
13527 {
13528 if (parm_pack != pattern)
13529 {
13530 if (complain & tf_error)
13531 sorry ("%qE is not the entire pattern of the pack expansion",
13532 parm_pack);
13533 return error_mark_node;
13534 }
13535 return expand_builtin_pack_call (call: parm_pack, args,
13536 complain, in_decl);
13537 }
13538 else if (TREE_CODE (parm_pack) == PARM_DECL)
13539 {
13540 /* We know we have correct local_specializations if this
13541 expansion is at function scope, or if we're dealing with a
13542 local parameter in a requires expression; for the latter,
13543 tsubst_requires_expr set it up appropriately. */
13544 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13545 arg_pack = retrieve_local_specialization (tmpl: parm_pack);
13546 else
13547 /* We can't rely on local_specializations for a parameter
13548 name used later in a function declaration (such as in a
13549 late-specified return type). Even if it exists, it might
13550 have the wrong value for a recursive call. */
13551 need_local_specializations = true;
13552
13553 if (!arg_pack)
13554 {
13555 /* This parameter pack was used in an unevaluated context. Just
13556 make a dummy decl, since it's only used for its type. */
13557 ++cp_unevaluated_operand;
13558 arg_pack = tsubst_decl (parm_pack, args, complain);
13559 --cp_unevaluated_operand;
13560 if (arg_pack && DECL_PACK_P (arg_pack))
13561 /* Partial instantiation of the parm_pack, we can't build
13562 up an argument pack yet. */
13563 arg_pack = NULL_TREE;
13564 else
13565 arg_pack = make_fnparm_pack (spec_parm: arg_pack);
13566 }
13567 else if (DECL_PACK_P (arg_pack))
13568 /* This argument pack isn't fully instantiated yet. */
13569 arg_pack = NULL_TREE;
13570 }
13571 else if (is_capture_proxy (parm_pack))
13572 {
13573 arg_pack = retrieve_local_specialization (tmpl: parm_pack);
13574 if (DECL_PACK_P (arg_pack))
13575 arg_pack = NULL_TREE;
13576 }
13577 else
13578 {
13579 int idx;
13580 template_parm_level_and_index (parm_pack, &level, &idx);
13581 if (level <= levels)
13582 arg_pack = TMPL_ARG (args, level, idx);
13583
13584 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13585 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13586 arg_pack = NULL_TREE;
13587 }
13588
13589 orig_arg = arg_pack;
13590 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13591 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13592
13593 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13594 /* This can only happen if we forget to expand an argument
13595 pack somewhere else. Just return an error, silently. */
13596 {
13597 result = make_tree_vec (1);
13598 TREE_VEC_ELT (result, 0) = error_mark_node;
13599 return result;
13600 }
13601
13602 if (arg_pack)
13603 {
13604 int my_len =
13605 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13606
13607 /* Don't bother trying to do a partial substitution with
13608 incomplete packs; we'll try again after deduction. */
13609 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13610 return t;
13611
13612 if (len < 0)
13613 len = my_len;
13614 else if (len != my_len)
13615 {
13616 if (!(complain & tf_error))
13617 /* Fail quietly. */;
13618 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13619 error ("mismatched argument pack lengths while expanding %qT",
13620 pattern);
13621 else
13622 error ("mismatched argument pack lengths while expanding %qE",
13623 pattern);
13624 return error_mark_node;
13625 }
13626
13627 /* Keep track of the parameter packs and their corresponding
13628 argument packs. */
13629 packs = tree_cons (parm_pack, arg_pack, packs);
13630 TREE_TYPE (packs) = orig_arg;
13631 }
13632 else
13633 {
13634 /* We can't substitute for this parameter pack. We use a flag as
13635 well as the missing_level counter because function parameter
13636 packs don't have a level. */
13637 gcc_assert (processing_template_decl || is_auto (parm_pack));
13638 unsubstituted_packs = true;
13639 }
13640 }
13641
13642 /* If the expansion is just T..., return the matching argument pack, unless
13643 we need to call convert_from_reference on all the elements. This is an
13644 important optimization; see c++/68422. */
13645 if (!unsubstituted_packs
13646 && TREE_PURPOSE (packs) == pattern)
13647 {
13648 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13649
13650 /* If the argument pack is a single pack expansion, pull it out. */
13651 if (TREE_VEC_LENGTH (args) == 1
13652 && pack_expansion_args_count (args))
13653 {
13654 tree arg = TREE_VEC_ELT (args, 0);
13655 if (PACK_EXPANSION_SIZEOF_P (t)
13656 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
13657 /* Except if this isn't a simple sizeof...(T) which gets sZ
13658 mangling, keep the TREE_VEC to get sP mangling. */;
13659 else
13660 return TREE_VEC_ELT (args, 0);
13661 }
13662
13663 /* Types need no adjustment, nor does sizeof..., and if we still have
13664 some pack expansion args we won't do anything yet. */
13665 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13666 || PACK_EXPANSION_SIZEOF_P (t)
13667 || pack_expansion_args_count (args))
13668 return args;
13669 /* Also optimize expression pack expansions if we can tell that the
13670 elements won't have reference type. */
13671 tree type = TREE_TYPE (pattern);
13672 if (type && !TYPE_REF_P (type)
13673 && !PACK_EXPANSION_P (type)
13674 && !WILDCARD_TYPE_P (type))
13675 return args;
13676 /* Otherwise use the normal path so we get convert_from_reference. */
13677 }
13678
13679 /* We cannot expand this expansion expression, because we don't have
13680 all of the argument packs we need. */
13681 if (use_pack_expansion_extra_args_p (t, parm_packs: packs, arg_pack_len: len, has_empty_arg: unsubstituted_packs))
13682 {
13683 /* We got some full packs, but we can't substitute them in until we
13684 have values for all the packs. So remember these until then. */
13685
13686 t = make_pack_expansion (arg: pattern, complain);
13687 PACK_EXPANSION_EXTRA_ARGS (t)
13688 = build_extra_args (pattern, args, complain);
13689 return t;
13690 }
13691
13692 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13693 type, so create our own local specializations map; the current map is
13694 either NULL or (in the case of recursive unification) might have
13695 bindings that we don't want to use or alter. */
13696 local_specialization_stack lss (need_local_specializations
13697 ? lss_blank : lss_nop);
13698
13699 if (unsubstituted_packs)
13700 {
13701 /* There were no real arguments, we're just replacing a parameter
13702 pack with another version of itself. Substitute into the
13703 pattern and return a PACK_EXPANSION_*. The caller will need to
13704 deal with that. */
13705 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13706 result = tsubst_expr (pattern, args, complain, in_decl);
13707 else
13708 result = tsubst (pattern, args, complain, in_decl);
13709 result = make_pack_expansion (arg: result, complain);
13710 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13711 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13712 if (PACK_EXPANSION_AUTO_P (t))
13713 {
13714 /* This is a fake auto... pack expansion created in add_capture with
13715 _PACKS that don't appear in the pattern. Copy one over. */
13716 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13717 pack = retrieve_local_specialization (TREE_VALUE (packs));
13718 gcc_checking_assert (DECL_PACK_P (pack));
13719 PACK_EXPANSION_PARAMETER_PACKS (result)
13720 = build_tree_list (NULL_TREE, pack);
13721 PACK_EXPANSION_AUTO_P (result) = true;
13722 }
13723 return result;
13724 }
13725
13726 gcc_assert (len >= 0);
13727
13728 /* For each argument in each argument pack, substitute into the
13729 pattern. */
13730 result = make_tree_vec (len);
13731 tree elem_args = copy_template_args (args);
13732 for (i = 0; i < len; ++i)
13733 {
13734 t = gen_elem_of_pack_expansion_instantiation (pattern, parm_packs: packs,
13735 index: i,
13736 args: elem_args, complain,
13737 in_decl);
13738 TREE_VEC_ELT (result, i) = t;
13739 if (t == error_mark_node)
13740 {
13741 result = error_mark_node;
13742 break;
13743 }
13744 }
13745
13746 /* Update ARGS to restore the substitution from parameter packs to
13747 their argument packs. */
13748 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13749 {
13750 tree parm = TREE_PURPOSE (pack);
13751
13752 if (TREE_CODE (parm) == PARM_DECL
13753 || VAR_P (parm)
13754 || TREE_CODE (parm) == FIELD_DECL)
13755 register_local_specialization (TREE_TYPE (pack), tmpl: parm);
13756 else
13757 {
13758 int idx, level;
13759
13760 if (TREE_VALUE (pack) == NULL_TREE)
13761 continue;
13762
13763 template_parm_level_and_index (parm, &level, &idx);
13764
13765 /* Update the corresponding argument. */
13766 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13767 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13768 TREE_TYPE (pack);
13769 else
13770 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13771 }
13772 }
13773
13774 /* If the dependent pack arguments were such that we end up with only a
13775 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13776 if (len == 1 && TREE_CODE (result) == TREE_VEC
13777 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13778 return TREE_VEC_ELT (result, 0);
13779
13780 return result;
13781}
13782
13783/* Make an argument pack out of the TREE_VEC VEC. */
13784
13785static tree
13786make_argument_pack (tree vec)
13787{
13788 tree pack;
13789
13790 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13791 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13792 else
13793 {
13794 pack = make_node (NONTYPE_ARGUMENT_PACK);
13795 TREE_CONSTANT (pack) = 1;
13796 }
13797 ARGUMENT_PACK_ARGS (pack) = vec;
13798 return pack;
13799}
13800
13801/* Return an exact copy of template args T that can be modified
13802 independently. */
13803
13804static tree
13805copy_template_args (tree t)
13806{
13807 if (t == error_mark_node)
13808 return t;
13809
13810 int len = TREE_VEC_LENGTH (t);
13811 tree new_vec = make_tree_vec (len);
13812
13813 for (int i = 0; i < len; ++i)
13814 {
13815 tree elt = TREE_VEC_ELT (t, i);
13816 if (elt && TREE_CODE (elt) == TREE_VEC)
13817 elt = copy_template_args (t: elt);
13818 TREE_VEC_ELT (new_vec, i) = elt;
13819 }
13820
13821 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13822 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13823
13824 return new_vec;
13825}
13826
13827/* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13828
13829tree
13830tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13831 tree in_decl)
13832{
13833 /* This flag is used only during deduction, and we don't expect to
13834 substitute such ARGUMENT_PACKs. */
13835 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
13836
13837 /* Substitute into each of the arguments. */
13838 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13839 args, complain, in_decl);
13840 if (pack_args == error_mark_node)
13841 return error_mark_node;
13842
13843 if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
13844 return orig_arg;
13845
13846 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13847 template parameter, we might be able to avoid allocating a new
13848 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13849 if the substituted result is identical to it. */
13850 if (tree parm = template_arg_to_parm (t: orig_arg))
13851 {
13852 int level, index;
13853 template_parm_level_and_index (parm, &level, &index);
13854 if (TMPL_ARGS_DEPTH (args) >= level)
13855 if (tree arg = TMPL_ARG (args, level, index))
13856 if (TREE_CODE (arg) == TREE_CODE (orig_arg)
13857 && ARGUMENT_PACK_ARGS (arg) == pack_args)
13858 {
13859 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
13860 return arg;
13861 }
13862 }
13863
13864 tree new_arg;
13865 if (TYPE_P (orig_arg))
13866 {
13867 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13868 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13869 }
13870 else
13871 {
13872 new_arg = make_node (TREE_CODE (orig_arg));
13873 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13874 }
13875 ARGUMENT_PACK_ARGS (new_arg) = pack_args;
13876 return new_arg;
13877}
13878
13879/* Substitute ARGS into the vector or list of template arguments T. */
13880
13881tree
13882tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13883{
13884 if (t == error_mark_node)
13885 return error_mark_node;
13886
13887 /* In "sizeof(X<I>)" we need to evaluate "I". */
13888 cp_evaluated ev;
13889
13890 const int len = TREE_VEC_LENGTH (t);
13891 tree *elts = XALLOCAVEC (tree, len);
13892 int expanded_len_adjust = 0;
13893
13894 /* True iff the substituted result is identical to T. */
13895 bool const_subst_p = true;
13896
13897 for (int i = 0; i < len; i++)
13898 {
13899 tree orig_arg = TREE_VEC_ELT (t, i);
13900 tree new_arg;
13901
13902 if (!orig_arg)
13903 new_arg = NULL_TREE;
13904 else if (TREE_CODE (orig_arg) == TREE_VEC)
13905 new_arg = tsubst_template_args (t: orig_arg, args, complain, in_decl);
13906 else if (PACK_EXPANSION_P (orig_arg))
13907 {
13908 /* Substitute into an expansion expression. */
13909 new_arg = tsubst_pack_expansion (t: orig_arg, args, complain, in_decl);
13910
13911 if (TREE_CODE (new_arg) == TREE_VEC)
13912 /* Add to the expanded length adjustment the number of
13913 expanded arguments. We subtract one from this
13914 measurement, because the argument pack expression
13915 itself is already counted as 1 in
13916 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13917 the argument pack is empty. */
13918 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13919 }
13920 else if (ARGUMENT_PACK_P (orig_arg))
13921 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13922 else
13923 new_arg = tsubst_template_arg (t: orig_arg, args, complain, in_decl);
13924
13925 if (new_arg == error_mark_node)
13926 return error_mark_node;
13927
13928 elts[i] = new_arg;
13929 if (new_arg != orig_arg)
13930 const_subst_p = false;
13931 }
13932
13933 if (const_subst_p)
13934 return t;
13935
13936 tree maybe_reuse = NULL_TREE;
13937
13938 /* If ARGS and T are both multi-level, the substituted result may be
13939 identical to ARGS. */
13940 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
13941 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
13942 && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
13943 maybe_reuse = args;
13944 /* If T appears to be a vector of generic template arguments, the
13945 substituted result may be identical to the corresponding level
13946 from ARGS. */
13947 else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
13948 {
13949 int level, index;
13950 template_parm_level_and_index (parm, &level, &index);
13951 if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
13952 maybe_reuse = TMPL_ARGS_LEVEL (args, level);
13953 }
13954
13955 /* If the substituted result is identical to MAYBE_REUSE, return
13956 it and avoid allocating a new TREE_VEC, as an optimization. */
13957 if (maybe_reuse != NULL_TREE
13958 && TREE_VEC_LENGTH (maybe_reuse) == len
13959 && std::equal (first1: elts, last1: elts+len, TREE_VEC_BEGIN (maybe_reuse)))
13960 return maybe_reuse;
13961
13962 /* If T consists of only a pack expansion for which substitution yielded
13963 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13964 of effectively making a copy. */
13965 if (len == 1
13966 && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
13967 && TREE_CODE (elts[0]) == TREE_VEC)
13968 return elts[0];
13969
13970 /* Make space for the expanded arguments coming from template
13971 argument packs. */
13972 tree r = make_tree_vec (len + expanded_len_adjust);
13973 /* T can contain TREE_VECs. That happens if T contains the
13974 arguments for a member template.
13975 In that case each TREE_VEC in T represents a level of template
13976 arguments, and T won't carry any non defaulted argument count.
13977 It will rather be the nested TREE_VECs that will carry one.
13978 In other words, T carries a non defaulted argument count only
13979 if it doesn't contain any nested TREE_VEC. */
13980 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
13981 {
13982 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13983 count += expanded_len_adjust;
13984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
13985 }
13986
13987 int out = 0;
13988 for (int i = 0; i < len; i++)
13989 {
13990 tree orig_arg = TREE_VEC_ELT (t, i);
13991 if (orig_arg
13992 && PACK_EXPANSION_P (orig_arg)
13993 && TREE_CODE (elts[i]) == TREE_VEC)
13994 {
13995 /* Now expand the template argument pack "in place". */
13996 for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13997 TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
13998 }
13999 else
14000 {
14001 TREE_VEC_ELT (r, out) = elts[i];
14002 out++;
14003 }
14004 }
14005 gcc_assert (out == TREE_VEC_LENGTH (r));
14006
14007 return r;
14008}
14009
14010/* Substitute ARGS into one level PARMS of template parameters. */
14011
14012static tree
14013tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
14014{
14015 if (parms == error_mark_node)
14016 return error_mark_node;
14017
14018 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
14019
14020 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
14021 {
14022 tree tuple = TREE_VEC_ELT (parms, i);
14023
14024 if (tuple == error_mark_node)
14025 continue;
14026
14027 TREE_VEC_ELT (new_vec, i) =
14028 tsubst_template_parm (tuple, args, complain);
14029 }
14030
14031 return new_vec;
14032}
14033
14034/* Return the result of substituting ARGS into the template parameters
14035 given by PARMS. If there are m levels of ARGS and m + n levels of
14036 PARMS, then the result will contain n levels of PARMS. For
14037 example, if PARMS is `template <class T> template <class U>
14038 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
14039 result will be `template <int*, double, class V>'. */
14040
14041static tree
14042tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
14043{
14044 tree r = NULL_TREE;
14045 tree* new_parms;
14046
14047 /* When substituting into a template, we must set
14048 PROCESSING_TEMPLATE_DECL as the template parameters may be
14049 dependent if they are based on one-another, and the dependency
14050 predicates are short-circuit outside of templates. */
14051 ++processing_template_decl;
14052
14053 for (new_parms = &r;
14054 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
14055 new_parms = &(TREE_CHAIN (*new_parms)),
14056 parms = TREE_CHAIN (parms))
14057 {
14058 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
14059 args, complain);
14060 *new_parms =
14061 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
14062 - TMPL_ARGS_DEPTH (args)),
14063 new_vec, NULL_TREE);
14064 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
14065 = TEMPLATE_PARMS_CONSTRAINTS (parms);
14066 }
14067
14068 --processing_template_decl;
14069
14070 return r;
14071}
14072
14073/* Return the result of substituting ARGS into one template parameter
14074 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
14075 parameter and which TREE_PURPOSE is the default argument of the
14076 template parameter. */
14077
14078static tree
14079tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
14080{
14081 tree default_value, parm_decl;
14082
14083 if (args == NULL_TREE
14084 || t == NULL_TREE
14085 || t == error_mark_node)
14086 return t;
14087
14088 gcc_assert (TREE_CODE (t) == TREE_LIST);
14089
14090 default_value = TREE_PURPOSE (t);
14091 parm_decl = TREE_VALUE (t);
14092
14093 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
14094 if (TREE_CODE (parm_decl) == PARM_DECL
14095 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
14096 parm_decl = error_mark_node;
14097 default_value = tsubst_template_arg (t: default_value, args,
14098 complain, NULL_TREE);
14099
14100 tree r = build_tree_list (default_value, parm_decl);
14101 TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
14102 return r;
14103}
14104
14105/* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
14106 parameter in PARMS for sake of declaration matching. */
14107
14108static void
14109tsubst_each_template_parm_constraints (tree parms, tree args,
14110 tsubst_flags_t complain)
14111{
14112 ++processing_template_decl;
14113 for (; parms; parms = TREE_CHAIN (parms))
14114 {
14115 tree level = TREE_VALUE (parms);
14116 for (tree parm : tree_vec_range (level))
14117 TEMPLATE_PARM_CONSTRAINTS (parm)
14118 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
14119 complain, NULL_TREE);
14120 }
14121 --processing_template_decl;
14122}
14123
14124/* Substitute the ARGS into the indicated aggregate (or enumeration)
14125 type T. If T is not an aggregate or enumeration type, it is
14126 handled as if by tsubst. IN_DECL is as for tsubst. If
14127 ENTERING_SCOPE is nonzero, T is the context for a template which
14128 we are presently tsubst'ing. Return the substituted value. */
14129
14130static tree
14131tsubst_aggr_type (tree t,
14132 tree args,
14133 tsubst_flags_t complain,
14134 tree in_decl,
14135 int entering_scope)
14136{
14137 if (t == NULL_TREE)
14138 return NULL_TREE;
14139
14140 /* Handle typedefs via tsubst so that they get consistently reused. */
14141 if (typedef_variant_p (type: t))
14142 {
14143 t = tsubst (t, args, complain, in_decl);
14144 if (t == error_mark_node)
14145 return error_mark_node;
14146
14147 /* The effect of entering_scope is that for a dependent specialization
14148 A<T>, lookup_template_class prefers to return A's primary template
14149 type instead of the implicit instantiation. So when entering_scope,
14150 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
14151 taking advantage of the fact that lookup_template_class links the two
14152 types by setting TYPE_CANONICAL of the latter to the former. */
14153 if (entering_scope
14154 && CLASS_TYPE_P (t)
14155 && dependent_type_p (t)
14156 && TYPE_TEMPLATE_INFO (t)
14157 && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t)))
14158 t = TYPE_CANONICAL (t);
14159
14160 return t;
14161 }
14162
14163 switch (TREE_CODE (t))
14164 {
14165 case RECORD_TYPE:
14166 case ENUMERAL_TYPE:
14167 case UNION_TYPE:
14168 return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope);
14169
14170 default:
14171 return tsubst (t, args, complain, in_decl);
14172 }
14173}
14174
14175/* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
14176 and ENUMERAL_TYPE cases of tsubst. */
14177
14178static tree
14179tsubst_aggr_type_1 (tree t,
14180 tree args,
14181 tsubst_flags_t complain,
14182 tree in_decl,
14183 int entering_scope)
14184{
14185 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
14186 {
14187 complain &= ~tf_qualifying_scope;
14188
14189 /* Figure out what arguments are appropriate for the
14190 type we are trying to find. For example, given:
14191
14192 template <class T> struct S;
14193 template <class T, class U> void f(T, U) { S<U> su; }
14194
14195 and supposing that we are instantiating f<int, double>,
14196 then our ARGS will be {int, double}, but, when looking up
14197 S we only want {double}. */
14198 tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
14199 complain, in_decl);
14200 if (argvec == error_mark_node)
14201 return error_mark_node;
14202
14203 tree r = lookup_template_class (d1: t, arglist: argvec, in_decl, NULL_TREE,
14204 entering_scope, complain);
14205 return cp_build_qualified_type (r, cp_type_quals (t), complain);
14206 }
14207 else
14208 /* This is not a template type, so there's nothing to do. */
14209 return t;
14210}
14211
14212/* Map from a FUNCTION_DECL to a vec of default argument instantiations,
14213 indexed in reverse order of the parameters. */
14214
14215static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
14216
14217/* Return a reference to the vec* of defarg insts for FN. */
14218
14219static vec<tree,va_gc> *&
14220defarg_insts_for (tree fn)
14221{
14222 if (!defarg_inst)
14223 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (n: 13);
14224 tree_vec_map in = { .base: { .from: fn }, .to: nullptr };
14225 tree_vec_map **slot
14226 = defarg_inst->find_slot_with_hash (comparable: &in, DECL_UID (fn), insert: INSERT);
14227 if (!*slot)
14228 {
14229 *slot = ggc_alloc<tree_vec_map> ();
14230 **slot = in;
14231 }
14232 return (*slot)->to;
14233}
14234
14235/* Substitute into the default argument ARG (a default argument for
14236 FN), which has the indicated TYPE. */
14237
14238tree
14239tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
14240 tsubst_flags_t complain)
14241{
14242 int errs = errorcount + sorrycount;
14243
14244 /* This can happen in invalid code. */
14245 if (TREE_CODE (arg) == DEFERRED_PARSE)
14246 return arg;
14247
14248 /* Shortcut {}. */
14249 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
14250 && CONSTRUCTOR_NELTS (arg) == 0)
14251 return arg;
14252
14253 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14254 parm = chain_index (parmnum, parm);
14255 tree parmtype = TREE_TYPE (parm);
14256 if (DECL_BY_REFERENCE (parm))
14257 parmtype = TREE_TYPE (parmtype);
14258 if (parmtype == error_mark_node)
14259 return error_mark_node;
14260
14261 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
14262
14263 /* Remember the location of the pointer to the vec rather than the location
14264 of the particular element, in case the vec grows in tsubst_expr. */
14265 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
14266 /* Index in reverse order to avoid allocating space for initial parameters
14267 that don't have default arguments. */
14268 unsigned ridx = list_length (parm);
14269 if (vec_safe_length (v: defs) < ridx)
14270 vec_safe_grow_cleared (v&: defs, len: ridx);
14271 else if (tree inst = (*defs)[ridx - 1])
14272 return inst;
14273
14274 /* This default argument came from a template. Instantiate the
14275 default argument here, not in tsubst. In the case of
14276 something like:
14277
14278 template <class T>
14279 struct S {
14280 static T t();
14281 void f(T = t());
14282 };
14283
14284 we must be careful to do name lookup in the scope of S<T>,
14285 rather than in the current class. */
14286 push_to_top_level ();
14287 push_access_scope (t: fn);
14288 push_deferring_access_checks (dk_no_deferred);
14289 /* So in_immediate_context knows this is a default argument. */
14290 begin_scope (sk_function_parms, fn);
14291 start_lambda_scope (decl: parm);
14292
14293 /* The default argument expression may cause implicitly defined
14294 member functions to be synthesized, which will result in garbage
14295 collection. We must treat this situation as if we were within
14296 the body of function so as to avoid collecting live data on the
14297 stack. */
14298 ++function_depth;
14299 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
14300 --function_depth;
14301
14302 finish_lambda_scope ();
14303
14304 /* Make sure the default argument is reasonable. */
14305 arg = check_default_argument (type, arg, complain);
14306
14307 if (errorcount+sorrycount > errs
14308 && (complain & tf_warning_or_error))
14309 inform (input_location,
14310 " when instantiating default argument for call to %qD", fn);
14311
14312 leave_scope ();
14313 pop_deferring_access_checks ();
14314 pop_access_scope (t: fn);
14315 pop_from_top_level ();
14316
14317 if (arg != error_mark_node && !cp_unevaluated_operand)
14318 (*defs)[ridx - 1] = arg;
14319
14320 return arg;
14321}
14322
14323/* Substitute into all the default arguments for FN. */
14324
14325static void
14326tsubst_default_arguments (tree fn, tsubst_flags_t complain)
14327{
14328 tree arg;
14329 tree tmpl_args;
14330
14331 tmpl_args = DECL_TI_ARGS (fn);
14332
14333 /* If this function is not yet instantiated, we certainly don't need
14334 its default arguments. */
14335 if (uses_template_parms (t: tmpl_args))
14336 return;
14337 /* Don't do this again for clones. */
14338 if (DECL_CLONED_FUNCTION_P (fn))
14339 return;
14340
14341 int i = 0;
14342 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
14343 arg;
14344 arg = TREE_CHAIN (arg), ++i)
14345 if (TREE_PURPOSE (arg))
14346 TREE_PURPOSE (arg) = tsubst_default_argument (fn, parmnum: i,
14347 TREE_VALUE (arg),
14348 TREE_PURPOSE (arg),
14349 complain);
14350}
14351
14352/* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14353static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
14354
14355/* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14356
14357void
14358store_explicit_specifier (tree v, tree t)
14359{
14360 if (!explicit_specifier_map)
14361 explicit_specifier_map = decl_tree_cache_map::create_ggc (size: 37);
14362 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
14363 explicit_specifier_map->put (k: v, v: t);
14364}
14365
14366/* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14367
14368tree
14369lookup_explicit_specifier (tree v)
14370{
14371 return *explicit_specifier_map->get (k: v);
14372}
14373
14374/* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14375 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14376 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14377 identical to T. */
14378
14379static tree
14380rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
14381 tree raises, tsubst_flags_t complain)
14382{
14383 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
14384
14385 tree new_type;
14386 if (TREE_CODE (t) == FUNCTION_TYPE)
14387 {
14388 new_type = build_function_type (return_type, arg_types);
14389 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
14390 }
14391 else
14392 {
14393 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14394 /* Don't pick up extra function qualifiers from the basetype. */
14395 r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
14396 if (! MAYBE_CLASS_TYPE_P (r))
14397 {
14398 /* [temp.deduct]
14399
14400 Type deduction may fail for any of the following
14401 reasons:
14402
14403 -- Attempting to create "pointer to member of T" when T
14404 is not a class type. */
14405 if (complain & tf_error)
14406 error ("creating pointer to member function of non-class type %qT",
14407 r);
14408 return error_mark_node;
14409 }
14410
14411 new_type = build_method_type_directly (r, return_type,
14412 TREE_CHAIN (arg_types));
14413 }
14414 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
14415
14416 cp_ref_qualifier rqual = type_memfn_rqual (t);
14417 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14418 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
14419}
14420
14421/* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14422 each of its formal parameters. If there is a disagreement then rebuild
14423 DECL's function type according to its formal parameter types, as part of a
14424 resolution for Core issues 1001/1322. */
14425
14426static void
14427maybe_rebuild_function_decl_type (tree decl)
14428{
14429 bool function_type_needs_rebuilding = false;
14430 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
14431 {
14432 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
14433 while (parm_type_list && parm_type_list != void_list_node)
14434 {
14435 tree parm_type = TREE_VALUE (parm_type_list);
14436 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14437 if (!same_type_p (parm_type, formal_parm_type_unqual))
14438 {
14439 function_type_needs_rebuilding = true;
14440 break;
14441 }
14442
14443 parm_list = DECL_CHAIN (parm_list);
14444 parm_type_list = TREE_CHAIN (parm_type_list);
14445 }
14446 }
14447
14448 if (!function_type_needs_rebuilding)
14449 return;
14450
14451 const tree fntype = TREE_TYPE (decl);
14452 tree parm_list = DECL_ARGUMENTS (decl);
14453 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
14454 tree new_parm_type_list = NULL_TREE;
14455 tree *q = &new_parm_type_list;
14456 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
14457 {
14458 *q = copy_node (old_parm_type_list);
14459 parm_list = DECL_CHAIN (parm_list);
14460 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14461 q = &TREE_CHAIN (*q);
14462 }
14463 while (old_parm_type_list && old_parm_type_list != void_list_node)
14464 {
14465 *q = copy_node (old_parm_type_list);
14466 tree *new_parm_type = &TREE_VALUE (*q);
14467 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14468 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
14469 *new_parm_type = formal_parm_type_unqual;
14470
14471 parm_list = DECL_CHAIN (parm_list);
14472 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14473 q = &TREE_CHAIN (*q);
14474 }
14475 if (old_parm_type_list == void_list_node)
14476 *q = void_list_node;
14477
14478 TREE_TYPE (decl)
14479 = rebuild_function_or_method_type (t: fntype,
14480 TREE_TYPE (fntype), arg_types: new_parm_type_list,
14481 TYPE_RAISES_EXCEPTIONS (fntype), complain: tf_none);
14482}
14483
14484/* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14485
14486static tree
14487tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
14488 tree lambda_fntype, bool use_spec_table = true)
14489{
14490 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
14491 hashval_t hash = 0;
14492 tree in_decl = t;
14493
14494 /* Nobody should be tsubst'ing into non-template functions. */
14495 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
14496 || DECL_LOCAL_DECL_P (t));
14497
14498 if (DECL_LOCAL_DECL_P (t))
14499 {
14500 if (tree spec = retrieve_local_specialization (tmpl: t))
14501 return spec;
14502 }
14503 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
14504 {
14505 /* If T is not dependent, just return it. */
14506 if (!uses_template_parms (DECL_TI_ARGS (t))
14507 && !LAMBDA_FUNCTION_P (t))
14508 return t;
14509
14510 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14511 if (non_templated_friend_p (t))
14512 goto friend_case;
14513
14514 /* Calculate the most general template of which R is a
14515 specialization. */
14516 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
14517
14518 /* We're substituting a lambda function under tsubst_lambda_expr but not
14519 directly from it; find the matching function we're already inside.
14520 But don't do this if T is a generic lambda with a single level of
14521 template parms, as in that case we're doing a normal instantiation. */
14522 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
14523 && (!generic_lambda_fn_p (t)
14524 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
14525 return enclosing_instantiation_of (tctx: t);
14526
14527 /* Calculate the complete set of arguments used to
14528 specialize R. */
14529 if (use_spec_table && !lambda_fntype)
14530 {
14531 argvec = tsubst_template_args (DECL_TI_ARGS
14532 (DECL_TEMPLATE_RESULT
14533 (DECL_TI_TEMPLATE (t))),
14534 args, complain, in_decl);
14535 if (argvec == error_mark_node)
14536 return error_mark_node;
14537
14538 /* Check to see if we already have this specialization. */
14539 hash = spec_hasher::hash (tmpl: gen_tmpl, args: argvec);
14540 if (tree spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash))
14541 /* The spec for these args might be a partial instantiation of the
14542 template, but here what we want is the FUNCTION_DECL. */
14543 return STRIP_TEMPLATE (spec);
14544 }
14545 else
14546 argvec = args;
14547 }
14548 else
14549 {
14550 /* This special case arises when we have something like this:
14551
14552 template <class T> struct S {
14553 friend void f<int>(int, double);
14554 };
14555
14556 Here, the DECL_TI_TEMPLATE for the friend declaration
14557 will be an IDENTIFIER_NODE. We are being called from
14558 tsubst_friend_function, and we want only to create a
14559 new decl (R) with appropriate types so that we can call
14560 determine_specialization. */
14561 friend_case:
14562 gen_tmpl = NULL_TREE;
14563 argvec = NULL_TREE;
14564 }
14565
14566 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14567 : NULL_TREE);
14568 tree ctx = closure ? closure : DECL_CONTEXT (t);
14569 bool member = ctx && TYPE_P (ctx);
14570
14571 /* If this is a static or xobj lambda, remove the 'this' pointer added in
14572 tsubst_lambda_expr now that we know the closure type. */
14573 if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
14574 lambda_fntype = static_fn_type (lambda_fntype);
14575
14576 if (member && !closure)
14577 ctx = tsubst_aggr_type (t: ctx, args,
14578 complain, in_decl: t, /*entering_scope=*/1);
14579
14580 tree type = (lambda_fntype ? lambda_fntype
14581 : tsubst (TREE_TYPE (t), args,
14582 complain | tf_fndecl_type, in_decl));
14583 if (type == error_mark_node)
14584 return error_mark_node;
14585
14586 /* If we hit excessive deduction depth, the type is bogus even if
14587 it isn't error_mark_node, so don't build a decl. */
14588 if (excessive_deduction_depth)
14589 return error_mark_node;
14590
14591 /* We do NOT check for matching decls pushed separately at this
14592 point, as they may not represent instantiations of this
14593 template, and in any case are considered separate under the
14594 discrete model. */
14595 tree r = copy_decl (t);
14596 DECL_USE_TEMPLATE (r) = 0;
14597 TREE_TYPE (r) = type;
14598 /* Clear out the mangled name and RTL for the instantiation. */
14599 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14600 SET_DECL_RTL (r, NULL);
14601 /* Leave DECL_INITIAL set on deleted instantiations. */
14602 if (!DECL_DELETED_FN (r))
14603 DECL_INITIAL (r) = NULL_TREE;
14604 DECL_CONTEXT (r) = ctx;
14605 set_instantiating_module (r);
14606
14607 /* Handle explicit(dependent-expr). */
14608 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14609 {
14610 tree spec = lookup_explicit_specifier (v: t);
14611 spec = tsubst_expr (spec, args, complain, in_decl);
14612 spec = build_explicit_specifier (spec, complain);
14613 if (spec == error_mark_node)
14614 return error_mark_node;
14615 if (instantiation_dependent_expression_p (spec))
14616 store_explicit_specifier (v: r, t: spec);
14617 else
14618 {
14619 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14620 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
14621 }
14622 }
14623
14624 /* OpenMP UDRs have the only argument a reference to the declared
14625 type. We want to diagnose if the declared type is a reference,
14626 which is invalid, but as references to references are usually
14627 quietly merged, diagnose it here. */
14628 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14629 {
14630 tree argtype
14631 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14632 argtype = tsubst (argtype, args, complain, in_decl);
14633 if (TYPE_REF_P (argtype))
14634 error_at (DECL_SOURCE_LOCATION (t),
14635 "reference type %qT in "
14636 "%<#pragma omp declare reduction%>", argtype);
14637 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), c: '~') == NULL)
14638 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14639 argtype);
14640 }
14641
14642 if (member && DECL_CONV_FN_P (r))
14643 /* Type-conversion operator. Reconstruct the name, in
14644 case it's the name of one of the template's parameters. */
14645 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14646
14647 tree parms = DECL_ARGUMENTS (t);
14648 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14649 parms = DECL_CHAIN (parms);
14650 parms = tsubst (parms, args, complain, t);
14651 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14652 DECL_CONTEXT (parm) = r;
14653 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14654 {
14655 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14656 DECL_NAME (tparm) = closure_identifier;
14657 DECL_CHAIN (tparm) = parms;
14658 parms = tparm;
14659 }
14660 DECL_ARGUMENTS (r) = parms;
14661 DECL_RESULT (r) = NULL_TREE;
14662
14663 maybe_rebuild_function_decl_type (decl: r);
14664
14665 TREE_STATIC (r) = 0;
14666 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14667 DECL_EXTERNAL (r) = 1;
14668 /* If this is an instantiation of a function with internal
14669 linkage, we already know what object file linkage will be
14670 assigned to the instantiation. */
14671 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14672 DECL_DEFER_OUTPUT (r) = 0;
14673 DECL_CHAIN (r) = NULL_TREE;
14674 DECL_PENDING_INLINE_INFO (r) = 0;
14675 DECL_PENDING_INLINE_P (r) = 0;
14676 DECL_SAVED_TREE (r) = NULL_TREE;
14677 DECL_STRUCT_FUNCTION (r) = NULL;
14678 TREE_USED (r) = 0;
14679 /* We'll re-clone as appropriate in instantiate_template. */
14680 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14681
14682 /* If we aren't complaining now, return on error before we register
14683 the specialization so that we'll complain eventually. */
14684 if ((complain & tf_error) == 0
14685 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14686 && !grok_op_properties (r, /*complain=*/false))
14687 return error_mark_node;
14688
14689 /* If we are looking at an xobj lambda, we might need to check the type of
14690 its xobj parameter. */
14691 if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
14692 {
14693 tree closure_obj = DECL_CONTEXT (r);
14694 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
14695 tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
14696
14697 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
14698 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
14699 /* If a lambda has an empty capture clause, an xobj parameter of
14700 unrelated type is not an error. */;
14701 else if (dependent_type_p (obj_param))
14702 /* If we are coming from tsubst_lambda_expr we might not have
14703 substituted into our xobj parameter yet. We can't error out until
14704 we know what the type really is so do nothing...
14705 ...but if we are instantiating the call op for real and we don't
14706 have a real type then something has gone incredibly wrong. */
14707 gcc_assert (lambda_fntype);
14708 else
14709 {
14710 /* We have a lambda with captures, and know the type of the xobj
14711 parameter, time to check it. */
14712 tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
14713 if (!same_or_base_type_p (closure_obj, obj_param_type))
14714 {
14715 /* This error does not emit when the lambda's call operator
14716 template is instantiated by taking its address, such as in
14717 the following case:
14718
14719 auto f = [x = 0](this auto&&){};
14720 int (*fp)(int&) = &decltype(f)::operator();
14721
14722 It only emits when explicitly calling the call operator with
14723 an explicit template parameter:
14724
14725 template<typename T>
14726 struct S : T {
14727 using T::operator();
14728 operator int() const {return {};}
14729 };
14730
14731 auto s = S{[x = 0](this auto&&) {}};
14732 s.operator()<int>();
14733
14734 This is due to resolve_address_of_overloaded_function being
14735 deficient at reporting candidates when overload resolution
14736 fails.
14737
14738 This diagnostic will be active in the first case if/when
14739 resolve_address_of_overloaded_function is fixed to properly
14740 emit candidates upon failure to resolve to an overload. */
14741 if (complain & tf_error)
14742 error ("a lambda with captures may not have an explicit "
14743 "object parameter of an unrelated type");
14744 return error_mark_node;
14745 }
14746 }
14747 }
14748
14749 /* Associate the constraints directly with the instantiation. We
14750 don't substitute through the constraints; that's only done when
14751 they are checked. */
14752 if (tree ci = get_constraints (t))
14753 set_constraints (r, ci);
14754
14755 if (DECL_FRIEND_CONTEXT (t))
14756 SET_DECL_FRIEND_CONTEXT (r,
14757 tsubst (DECL_FRIEND_CONTEXT (t),
14758 args, complain, in_decl));
14759
14760 if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0,
14761 args, complain, in_decl))
14762 return error_mark_node;
14763
14764 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14765 this in the special friend case mentioned above where
14766 GEN_TMPL is NULL. */
14767 if (gen_tmpl && !closure)
14768 {
14769 DECL_TEMPLATE_INFO (r)
14770 = build_template_info (template_decl: gen_tmpl, template_args: argvec);
14771 SET_DECL_IMPLICIT_INSTANTIATION (r);
14772
14773 if (use_spec_table)
14774 {
14775 tree new_r
14776 = register_specialization (spec: r, tmpl: gen_tmpl, args: argvec, is_friend: false, hash);
14777 if (new_r != r)
14778 /* We instantiated this while substituting into
14779 the type earlier (template/friend54.C). */
14780 return new_r;
14781 }
14782
14783 /* We're not supposed to instantiate default arguments
14784 until they are called, for a template. But, for a
14785 declaration like:
14786
14787 template <class T> void f ()
14788 { extern void g(int i = T()); }
14789
14790 we should do the substitution when the template is
14791 instantiated. We handle the member function case in
14792 instantiate_class_template since the default arguments
14793 might refer to other members of the class. */
14794 if (!member
14795 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14796 && !uses_template_parms (t: argvec))
14797 tsubst_default_arguments (fn: r, complain);
14798 }
14799 else if (DECL_LOCAL_DECL_P (r))
14800 {
14801 if (!cp_unevaluated_operand)
14802 register_local_specialization (spec: r, tmpl: t);
14803 }
14804 else
14805 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14806
14807 /* Copy the list of befriending classes. */
14808 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14809 *friends;
14810 friends = &TREE_CHAIN (*friends))
14811 {
14812 *friends = copy_node (*friends);
14813 TREE_VALUE (*friends)
14814 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14815 }
14816
14817 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14818 {
14819 maybe_retrofit_in_chrg (r);
14820 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14821 return error_mark_node;
14822 /* If this is an instantiation of a member template, clone it.
14823 If it isn't, that'll be handled by
14824 clone_constructors_and_destructors. */
14825 if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
14826 clone_cdtor (r, /*update_methods=*/false);
14827 }
14828 else if ((complain & tf_error) != 0
14829 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14830 && !grok_op_properties (r, /*complain=*/true))
14831 return error_mark_node;
14832
14833 /* Possibly limit visibility based on template args. */
14834 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14835 if (DECL_VISIBILITY_SPECIFIED (t))
14836 {
14837 DECL_VISIBILITY_SPECIFIED (r) = 0;
14838 DECL_ATTRIBUTES (r)
14839 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14840 }
14841 determine_visibility (r);
14842 if (DECL_SECTION_NAME (t))
14843 set_decl_section_name (r, t);
14844 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14845 && COMPLETE_TYPE_P (DECL_CONTEXT (r))
14846 && !processing_template_decl)
14847 defaulted_late_check (r);
14848
14849 if (flag_openmp)
14850 if (tree attr = lookup_attribute (attr_name: "omp declare variant base",
14851 DECL_ATTRIBUTES (r)))
14852 omp_declare_variant_finalize (r, attr);
14853
14854 return r;
14855}
14856
14857/* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14858
14859static tree
14860tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14861 tree lambda_fntype, tree lambda_tparms)
14862{
14863 /* We can get here when processing a member function template,
14864 member class template, or template template parameter. */
14865 tree decl = DECL_TEMPLATE_RESULT (t);
14866 tree in_decl = t;
14867 tree spec;
14868 tree tmpl_args;
14869 tree full_args = NULL_TREE;
14870 tree r;
14871 hashval_t hash = 0;
14872
14873 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14874 {
14875 /* Template template parameter is treated here. */
14876 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14877 if (new_type == error_mark_node)
14878 r = error_mark_node;
14879 /* If we get a real template back, return it. This can happen in
14880 the context of most_specialized_partial_spec. */
14881 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14882 r = new_type;
14883 else
14884 /* The new TEMPLATE_DECL was built in
14885 reduce_template_parm_level. */
14886 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14887 return r;
14888 }
14889
14890 if (!lambda_fntype)
14891 {
14892 /* We might already have an instance of this template.
14893 The ARGS are for the surrounding class type, so the
14894 full args contain the tsubst'd args for the context,
14895 plus the innermost args from the template decl. */
14896 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14897 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14898 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14899 /* Because this is a template, the arguments will still be
14900 dependent, even after substitution. If
14901 PROCESSING_TEMPLATE_DECL is not set, the dependency
14902 predicates will short-circuit. */
14903 ++processing_template_decl;
14904 full_args = tsubst_template_args (t: tmpl_args, args,
14905 complain, in_decl);
14906 --processing_template_decl;
14907 if (full_args == error_mark_node)
14908 return error_mark_node;
14909
14910 /* If this is a default template template argument,
14911 tsubst might not have changed anything. */
14912 if (full_args == tmpl_args)
14913 return t;
14914
14915 hash = spec_hasher::hash (tmpl: t, args: full_args);
14916 spec = retrieve_specialization (tmpl: t, args: full_args, hash);
14917 if (spec != NULL_TREE)
14918 {
14919 if (TYPE_P (spec))
14920 /* Type partial instantiations are stored as the type by
14921 lookup_template_class_1, not here as the template. */
14922 spec = CLASSTYPE_TI_TEMPLATE (spec);
14923 else if (TREE_CODE (spec) != TEMPLATE_DECL)
14924 spec = DECL_TI_TEMPLATE (spec);
14925 return spec;
14926 }
14927 }
14928
14929 /* Make a new template decl. It will be similar to the
14930 original, but will record the current template arguments.
14931 We also create a new function declaration, which is just
14932 like the old one, but points to this new template, rather
14933 than the old one. */
14934 r = copy_decl (t);
14935 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14936 DECL_CHAIN (r) = NULL_TREE;
14937
14938 // Build new template info linking to the original template decl.
14939 if (!lambda_fntype)
14940 {
14941 DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: t, template_args: args);
14942 SET_DECL_IMPLICIT_INSTANTIATION (r);
14943 }
14944 else
14945 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14946
14947 /* The template parameters for this new template are all the
14948 template parameters for the old template, except the
14949 outermost level of parameters. */
14950 auto tparm_guard = make_temp_override (current_template_parms);
14951 DECL_TEMPLATE_PARMS (r)
14952 = current_template_parms
14953 = (lambda_tparms
14954 ? lambda_tparms
14955 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14956 complain));
14957
14958 bool class_p = false;
14959 tree inner = decl;
14960 ++processing_template_decl;
14961 if (TREE_CODE (inner) == FUNCTION_DECL)
14962 inner = tsubst_function_decl (t: inner, args, complain, lambda_fntype,
14963 /*use_spec_table=*/false);
14964 else
14965 {
14966 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14967 {
14968 class_p = true;
14969 inner = TREE_TYPE (inner);
14970 }
14971 if (class_p)
14972 inner = tsubst_aggr_type (t: inner, args, complain,
14973 in_decl, /*entering*/entering_scope: 1);
14974 else
14975 inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
14976 }
14977 --processing_template_decl;
14978 if (inner == error_mark_node)
14979 return error_mark_node;
14980
14981 if (class_p)
14982 {
14983 /* For a partial specialization, we need to keep pointing to
14984 the primary template. */
14985 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14986 {
14987 CLASSTYPE_TI_TEMPLATE (inner) = r;
14988 CLASSTYPE_USE_TEMPLATE (inner) = 0;
14989 }
14990
14991 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14992 inner = TYPE_MAIN_DECL (inner);
14993 }
14994 else if (lambda_fntype)
14995 {
14996 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14997 DECL_TEMPLATE_INFO (inner) = build_template_info (template_decl: r, template_args: args);
14998 }
14999 else
15000 {
15001 DECL_TI_TEMPLATE (inner) = r;
15002 /* Set DECL_TI_ARGS to the full set of template arguments,
15003 which tsubst_function_decl / tsubst_decl didn't do due to
15004 use_spec_table=false. */
15005 DECL_TI_ARGS (inner) = full_args;
15006 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
15007 }
15008
15009 DECL_TEMPLATE_RESULT (r) = inner;
15010 TREE_TYPE (r) = TREE_TYPE (inner);
15011 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
15012
15013 if (modules_p ())
15014 {
15015 /* Propagate module information from the decl. */
15016 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
15017 if (DECL_LANG_SPECIFIC (inner))
15018 /* If this is a constrained template, the above tsubst of
15019 inner can find the unconstrained template, which may have
15020 come from an import. This is ok, because we don't
15021 register this instantiation (see below). */
15022 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
15023 || (TEMPLATE_PARMS_CONSTRAINTS
15024 (DECL_TEMPLATE_PARMS (t))));
15025 }
15026
15027 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
15028 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
15029
15030 if (PRIMARY_TEMPLATE_P (t))
15031 DECL_PRIMARY_TEMPLATE (r) = r;
15032
15033 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r) = false;
15034
15035 if (!lambda_fntype && !class_p)
15036 {
15037 /* Record this non-type partial instantiation. */
15038 /* FIXME we'd like to always register the TEMPLATE_DECL, or always
15039 the DECL_TEMPLATE_RESULT, but it seems the modules code relies
15040 on this current behavior. */
15041 if (TREE_CODE (inner) == FUNCTION_DECL)
15042 register_specialization (spec: r, tmpl: t, args: full_args, is_friend: false, hash);
15043 else
15044 register_specialization (spec: inner, tmpl: t, args: full_args, is_friend: false, hash);
15045 }
15046
15047 return r;
15048}
15049
15050/* True if FN is the op() for a lambda in an uninstantiated template. */
15051
15052bool
15053lambda_fn_in_template_p (tree fn)
15054{
15055 if (!fn || !LAMBDA_FUNCTION_P (fn))
15056 return false;
15057 tree closure = DECL_CONTEXT (fn);
15058 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
15059}
15060
15061/* True if FN is the substitution (via tsubst_lambda_expr) of a function for
15062 which the above is true. */
15063
15064bool
15065regenerated_lambda_fn_p (tree fn)
15066{
15067 if (!fn || !LAMBDA_FUNCTION_P (fn))
15068 return false;
15069 tree closure = DECL_CONTEXT (fn);
15070 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
15071 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
15072}
15073
15074/* Return the LAMBDA_EXPR from which T was ultimately regenerated.
15075 If T is not a regenerated LAMBDA_EXPR, return T. */
15076
15077tree
15078most_general_lambda (tree t)
15079{
15080 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15081 t = TI_TEMPLATE (ti);
15082 return t;
15083}
15084
15085/* Return the set of template arguments used to regenerate the lambda T
15086 from its most general lambda. */
15087
15088tree
15089lambda_regenerating_args (tree t)
15090{
15091 if (LAMBDA_FUNCTION_P (t))
15092 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
15093 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
15094 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15095 return TI_ARGS (ti);
15096 else
15097 return NULL_TREE;
15098}
15099
15100/* We're instantiating a variable from template function TCTX. Return the
15101 corresponding current enclosing scope. We can match them up using
15102 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
15103 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
15104 the template definition in regenerate_decl_from_template. */
15105
15106static tree
15107enclosing_instantiation_of (tree tctx)
15108{
15109 tree fn = current_function_decl;
15110
15111 /* We shouldn't ever need to do this for other artificial functions. */
15112 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
15113
15114 for (; fn; fn = decl_function_context (fn))
15115 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
15116 return fn;
15117 gcc_unreachable ();
15118}
15119
15120/* Substitute the ARGS into the T, which is a _DECL. Return the
15121 result of the substitution. Issue error and warning messages under
15122 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up
15123 and insert into the specializations table or if we can assume it's
15124 the caller's responsibility; this is used by instantiate_template
15125 to avoid doing some redundant work. */
15126
15127static tree
15128tsubst_decl (tree t, tree args, tsubst_flags_t complain,
15129 bool use_spec_table /* = true */)
15130{
15131#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15132 location_t saved_loc;
15133 tree r = NULL_TREE;
15134 tree in_decl = t;
15135 hashval_t hash = 0;
15136
15137 if (t == error_mark_node)
15138 return error_mark_node;
15139
15140 /* Set the filename and linenumber to improve error-reporting. */
15141 saved_loc = input_location;
15142 input_location = DECL_SOURCE_LOCATION (t);
15143
15144 switch (TREE_CODE (t))
15145 {
15146 case TEMPLATE_DECL:
15147 r = tsubst_template_decl (t, args, complain,
15148 /*lambda_fntype=*/NULL_TREE,
15149 /*lambda_tparms=*/NULL_TREE);
15150 break;
15151
15152 case FUNCTION_DECL:
15153 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
15154 use_spec_table);
15155 break;
15156
15157 case PARM_DECL:
15158 {
15159 tree type = NULL_TREE;
15160 int i, len = 1;
15161 tree expanded_types = NULL_TREE;
15162 tree prev_r = NULL_TREE;
15163 tree first_r = NULL_TREE;
15164
15165 if (DECL_PACK_P (t))
15166 {
15167 /* If there is a local specialization that isn't a
15168 parameter pack, it means that we're doing a "simple"
15169 substitution from inside tsubst_pack_expansion. Just
15170 return the local specialization (which will be a single
15171 parm). */
15172 tree spec = retrieve_local_specialization (tmpl: t);
15173 if (spec
15174 && TREE_CODE (spec) == PARM_DECL
15175 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
15176 RETURN (spec);
15177
15178 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15179 the parameters in this function parameter pack. */
15180 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15181 complain, in_decl);
15182 if (TREE_CODE (expanded_types) == TREE_VEC)
15183 {
15184 len = TREE_VEC_LENGTH (expanded_types);
15185
15186 /* Zero-length parameter packs are boring. Just substitute
15187 into the chain. */
15188 if (len == 0 && !cp_unevaluated_operand)
15189 RETURN (tsubst (TREE_CHAIN (t), args, complain,
15190 TREE_CHAIN (t)));
15191 }
15192 else
15193 {
15194 /* All we did was update the type. Make a note of that. */
15195 type = expanded_types;
15196 expanded_types = NULL_TREE;
15197 }
15198 }
15199
15200 /* Loop through all of the parameters we'll build. When T is
15201 a function parameter pack, LEN is the number of expanded
15202 types in EXPANDED_TYPES; otherwise, LEN is 1. */
15203 r = NULL_TREE;
15204 for (i = 0; i < len; ++i)
15205 {
15206 prev_r = r;
15207 r = copy_node (t);
15208 if (DECL_TEMPLATE_PARM_P (t))
15209 SET_DECL_TEMPLATE_PARM_P (r);
15210
15211 if (expanded_types)
15212 /* We're on the Ith parameter of the function parameter
15213 pack. */
15214 {
15215 /* Get the Ith type. */
15216 type = TREE_VEC_ELT (expanded_types, i);
15217
15218 /* Rename the parameter to include the index. */
15219 DECL_NAME (r)
15220 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15221 }
15222 else if (!type)
15223 /* We're dealing with a normal parameter. */
15224 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15225
15226 type = type_decays_to (type);
15227 TREE_TYPE (r) = type;
15228 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15229
15230 if (DECL_INITIAL (r))
15231 {
15232 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
15233 DECL_INITIAL (r) = TREE_TYPE (r);
15234 else
15235 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
15236 complain, in_decl);
15237 }
15238
15239 DECL_CONTEXT (r) = NULL_TREE;
15240
15241 if (!DECL_TEMPLATE_PARM_P (r))
15242 DECL_ARG_TYPE (r) = type_passed_as (type);
15243
15244 if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0,
15245 args, complain, in_decl))
15246 return error_mark_node;
15247
15248 /* Keep track of the first new parameter we
15249 generate. That's what will be returned to the
15250 caller. */
15251 if (!first_r)
15252 first_r = r;
15253
15254 /* Build a proper chain of parameters when substituting
15255 into a function parameter pack. */
15256 if (prev_r)
15257 DECL_CHAIN (prev_r) = r;
15258 }
15259
15260 /* If cp_unevaluated_operand is set, we're just looking for a
15261 single dummy parameter, so don't keep going. */
15262 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
15263 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
15264 complain, DECL_CHAIN (t));
15265
15266 /* FIRST_R contains the start of the chain we've built. */
15267 r = first_r;
15268 }
15269 break;
15270
15271 case FIELD_DECL:
15272 {
15273 tree type = NULL_TREE;
15274 tree vec = NULL_TREE;
15275 tree expanded_types = NULL_TREE;
15276 int len = 1;
15277
15278 if (PACK_EXPANSION_P (TREE_TYPE (t)))
15279 {
15280 /* This field is a lambda capture pack. Return a TREE_VEC of
15281 the expanded fields to instantiate_class_template_1. */
15282 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15283 complain, in_decl);
15284 if (TREE_CODE (expanded_types) == TREE_VEC)
15285 {
15286 len = TREE_VEC_LENGTH (expanded_types);
15287 vec = make_tree_vec (len);
15288 }
15289 else
15290 {
15291 /* All we did was update the type. Make a note of that. */
15292 type = expanded_types;
15293 expanded_types = NULL_TREE;
15294 }
15295 }
15296
15297 for (int i = 0; i < len; ++i)
15298 {
15299 r = copy_decl (t);
15300 if (expanded_types)
15301 {
15302 type = TREE_VEC_ELT (expanded_types, i);
15303 DECL_NAME (r)
15304 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15305 }
15306 else if (!type)
15307 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15308
15309 if (type == error_mark_node)
15310 RETURN (error_mark_node);
15311 TREE_TYPE (r) = type;
15312 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15313
15314 if (DECL_C_BIT_FIELD (r))
15315 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
15316 number of bits. */
15317 DECL_BIT_FIELD_REPRESENTATIVE (r)
15318 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
15319 complain, in_decl);
15320 if (DECL_INITIAL (t))
15321 {
15322 /* Set up DECL_TEMPLATE_INFO so that we can get at the
15323 NSDMI in perform_member_init. Still set DECL_INITIAL
15324 so that we know there is one. */
15325 DECL_INITIAL (r) = void_node;
15326 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
15327 retrofit_lang_decl (r);
15328 DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: t, template_args: args);
15329 }
15330 /* We don't have to set DECL_CONTEXT here; it is set by
15331 finish_member_declaration. */
15332 DECL_CHAIN (r) = NULL_TREE;
15333
15334 if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0,
15335 args, complain, in_decl))
15336 return error_mark_node;
15337
15338 if (vec)
15339 TREE_VEC_ELT (vec, i) = r;
15340 }
15341
15342 if (vec)
15343 r = vec;
15344 }
15345 break;
15346
15347 case USING_DECL:
15348 /* We reach here only for member using decls. We also need to check
15349 uses_template_parms because DECL_DEPENDENT_P is not set for a
15350 using-declaration that designates a member of the current
15351 instantiation (c++/53549). */
15352 if (DECL_DEPENDENT_P (t)
15353 || uses_template_parms (USING_DECL_SCOPE (t)))
15354 {
15355 /* True iff this using-decl was written as a pack expansion
15356 (and a pack appeared in its scope or name). If a pack
15357 appeared in both, we expand the packs separately and
15358 manually merge them. */
15359 bool variadic_p = false;
15360
15361 tree scope = USING_DECL_SCOPE (t);
15362 if (PACK_EXPANSION_P (scope))
15363 {
15364 scope = tsubst_pack_expansion (t: scope, args,
15365 complain: complain | tf_qualifying_scope,
15366 in_decl);
15367 variadic_p = true;
15368 }
15369 else
15370 scope = tsubst_scope (scope, args, complain, in_decl);
15371
15372 tree name = DECL_NAME (t);
15373 if (IDENTIFIER_CONV_OP_P (name)
15374 && PACK_EXPANSION_P (TREE_TYPE (name)))
15375 {
15376 name = tsubst_pack_expansion (TREE_TYPE (name), args,
15377 complain, in_decl);
15378 if (name == error_mark_node)
15379 {
15380 r = error_mark_node;
15381 break;
15382 }
15383 for (tree& elt : tree_vec_range (name))
15384 elt = make_conv_op_name (elt);
15385 variadic_p = true;
15386 }
15387 else
15388 name = tsubst_name (name, args, complain, in_decl);
15389
15390 int len;
15391 if (!variadic_p)
15392 len = 1;
15393 else if (TREE_CODE (scope) == TREE_VEC
15394 && TREE_CODE (name) == TREE_VEC)
15395 {
15396 if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
15397 {
15398 error ("mismatched argument pack lengths (%d vs %d)",
15399 TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
15400 r = error_mark_node;
15401 break;
15402 }
15403 len = TREE_VEC_LENGTH (scope);
15404 }
15405 else if (TREE_CODE (scope) == TREE_VEC)
15406 len = TREE_VEC_LENGTH (scope);
15407 else /* TREE_CODE (name) == TREE_VEC */
15408 len = TREE_VEC_LENGTH (name);
15409
15410 r = make_tree_vec (len);
15411 for (int i = 0; i < len; ++i)
15412 {
15413 tree escope = (TREE_CODE (scope) == TREE_VEC
15414 ? TREE_VEC_ELT (scope, i)
15415 : scope);
15416 tree ename = (TREE_CODE (name) == TREE_VEC
15417 ? TREE_VEC_ELT (name, i)
15418 : name);
15419 tree elt = do_class_using_decl (escope, ename);
15420 if (!elt)
15421 {
15422 r = error_mark_node;
15423 break;
15424 }
15425 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
15426 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
15427 TREE_VEC_ELT (r, i) = elt;
15428 }
15429
15430 if (!variadic_p && r != error_mark_node)
15431 r = TREE_VEC_ELT (r, 0);
15432 }
15433 else
15434 {
15435 r = copy_node (t);
15436 DECL_CHAIN (r) = NULL_TREE;
15437 }
15438 break;
15439
15440 case TYPE_DECL:
15441 case VAR_DECL:
15442 {
15443 tree argvec = NULL_TREE;
15444 tree gen_tmpl = NULL_TREE;
15445 tree tmpl = NULL_TREE;
15446 tree type = NULL_TREE;
15447
15448 if (TREE_TYPE (t) == error_mark_node)
15449 RETURN (error_mark_node);
15450
15451 if (TREE_CODE (t) == TYPE_DECL
15452 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
15453 {
15454 /* If this is the canonical decl, we don't have to
15455 mess with instantiations, and often we can't (for
15456 typename, template type parms and such). Note that
15457 TYPE_NAME is not correct for the above test if
15458 we've copied the type for a typedef. */
15459 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15460 if (type == error_mark_node)
15461 RETURN (error_mark_node);
15462 r = TYPE_NAME (type);
15463 break;
15464 }
15465
15466 /* Check to see if we already have the specialization we
15467 need. */
15468 tree spec = NULL_TREE;
15469 bool local_p = false;
15470 tree ctx = DECL_CONTEXT (t);
15471 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
15472 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
15473 {
15474 local_p = false;
15475 if (DECL_CLASS_SCOPE_P (t))
15476 {
15477 ctx = tsubst_aggr_type (t: ctx, args,
15478 complain,
15479 in_decl, /*entering_scope=*/1);
15480 if (DECL_SELF_REFERENCE_P (t))
15481 /* The context and type of an injected-class-name are
15482 the same, so we don't need to substitute both. */
15483 type = ctx;
15484 /* If CTX is unchanged, then T is in fact the
15485 specialization we want. That situation occurs when
15486 referencing a static data member within in its own
15487 class. We can use pointer equality, rather than
15488 same_type_p, because DECL_CONTEXT is always
15489 canonical... */
15490 if (ctx == DECL_CONTEXT (t)
15491 /* ... unless T is a member template; in which
15492 case our caller can be willing to create a
15493 specialization of that template represented
15494 by T. */
15495 && !(DECL_TI_TEMPLATE (t)
15496 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
15497 spec = t;
15498 }
15499
15500 if (!spec)
15501 {
15502 tmpl = DECL_TI_TEMPLATE (t);
15503 if (use_spec_table)
15504 {
15505 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
15506 if (argvec == error_mark_node)
15507 RETURN (error_mark_node);
15508 gen_tmpl = most_general_template (tmpl);
15509 hash = spec_hasher::hash (tmpl: gen_tmpl, args: argvec);
15510 spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash);
15511 }
15512 else
15513 argvec = args;
15514 }
15515 }
15516 else
15517 {
15518 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
15519 /* Subsequent calls to pushdecl will fill this in. */
15520 ctx = NULL_TREE;
15521 /* A local variable. */
15522 local_p = true;
15523 /* Unless this is a reference to a static variable from an
15524 enclosing function, in which case we need to fill it in now. */
15525 if (TREE_STATIC (t))
15526 {
15527 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
15528 if (fn != current_function_decl)
15529 ctx = fn;
15530 }
15531 spec = retrieve_local_specialization (tmpl: t);
15532 }
15533 /* If we already have the specialization we need, there is
15534 nothing more to do. */
15535 if (spec)
15536 {
15537 r = spec;
15538 break;
15539 }
15540
15541 /* Create a new node for the specialization we need. */
15542 if (type == NULL_TREE)
15543 {
15544 if (is_typedef_decl (x: t))
15545 type = DECL_ORIGINAL_TYPE (t);
15546 else
15547 type = TREE_TYPE (t);
15548 if (VAR_P (t)
15549 && VAR_HAD_UNKNOWN_BOUND (t)
15550 && type != error_mark_node)
15551 type = strip_array_domain (type);
15552 tsubst_flags_t tcomplain = complain;
15553 if (VAR_P (t))
15554 tcomplain |= tf_tst_ok;
15555 type = tsubst (type, args, tcomplain, in_decl);
15556 /* Substituting the type might have recursively instantiated this
15557 same alias (c++/86171). */
15558 if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
15559 && (spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash)))
15560 {
15561 r = spec;
15562 break;
15563 }
15564 }
15565 if (type == error_mark_node && !(complain & tf_error))
15566 RETURN (error_mark_node);
15567 r = copy_decl (t);
15568 if (VAR_P (r))
15569 {
15570 DECL_INITIALIZED_P (r) = 0;
15571 DECL_TEMPLATE_INSTANTIATED (r) = 0;
15572 if (TREE_CODE (type) == FUNCTION_TYPE)
15573 {
15574 /* It may seem that this case cannot occur, since:
15575
15576 typedef void f();
15577 void g() { f x; }
15578
15579 declares a function, not a variable. However:
15580
15581 typedef void f();
15582 template <typename T> void g() { T t; }
15583 template void g<f>();
15584
15585 is an attempt to declare a variable with function
15586 type. */
15587 error ("variable %qD has function type",
15588 /* R is not yet sufficiently initialized, so we
15589 just use its name. */
15590 DECL_NAME (r));
15591 RETURN (error_mark_node);
15592 }
15593 type = complete_type (type);
15594 /* Wait until cp_finish_decl to set this again, to handle
15595 circular dependency (template/instantiate6.C). */
15596 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
15597 type = check_var_type (DECL_NAME (r), type,
15598 DECL_SOURCE_LOCATION (r));
15599 if (DECL_HAS_VALUE_EXPR_P (t))
15600 {
15601 tree ve = DECL_VALUE_EXPR (t);
15602 /* If the DECL_VALUE_EXPR is converted to the declared type,
15603 preserve the identity so that gimplify_type_sizes works. */
15604 bool nop = (TREE_CODE (ve) == NOP_EXPR);
15605 if (nop)
15606 ve = TREE_OPERAND (ve, 0);
15607 ve = tsubst_expr (ve, args, complain, in_decl);
15608 if (REFERENCE_REF_P (ve))
15609 {
15610 gcc_assert (TYPE_REF_P (type));
15611 ve = TREE_OPERAND (ve, 0);
15612 }
15613 if (nop)
15614 ve = build_nop (type, ve);
15615 else if (DECL_LANG_SPECIFIC (t)
15616 && DECL_OMP_PRIVATIZED_MEMBER (t)
15617 && TREE_CODE (ve) == COMPONENT_REF
15618 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
15619 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
15620 type = TREE_TYPE (ve);
15621 else
15622 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
15623 == TYPE_MAIN_VARIANT (type));
15624 SET_DECL_VALUE_EXPR (r, ve);
15625 }
15626 if (CP_DECL_THREAD_LOCAL_P (r)
15627 && !processing_template_decl)
15628 set_decl_tls_model (r, decl_default_tls_model (r));
15629 }
15630 else if (DECL_SELF_REFERENCE_P (t))
15631 SET_DECL_SELF_REFERENCE_P (r);
15632 TREE_TYPE (r) = type;
15633 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15634 DECL_CONTEXT (r) = ctx;
15635 /* Clear out the mangled name and RTL for the instantiation. */
15636 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
15637 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
15638 SET_DECL_RTL (r, NULL);
15639 set_instantiating_module (r);
15640
15641 /* The initializer must not be expanded until it is required;
15642 see [temp.inst]. */
15643 DECL_INITIAL (r) = NULL_TREE;
15644 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
15645 if (VAR_P (r))
15646 {
15647 if (DECL_LANG_SPECIFIC (r))
15648 SET_DECL_DEPENDENT_INIT_P (r, false);
15649
15650 SET_DECL_MODE (r, VOIDmode);
15651
15652 /* Possibly limit visibility based on template args. */
15653 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15654 if (DECL_VISIBILITY_SPECIFIED (t))
15655 {
15656 DECL_VISIBILITY_SPECIFIED (r) = 0;
15657 DECL_ATTRIBUTES (r)
15658 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15659 }
15660 determine_visibility (r);
15661 if ((!local_p || TREE_STATIC (t)) && DECL_SECTION_NAME (t))
15662 set_decl_section_name (r, t);
15663 }
15664
15665 if (!local_p)
15666 {
15667 /* A static data member declaration is always marked
15668 external when it is declared in-class, even if an
15669 initializer is present. We mimic the non-template
15670 processing here. */
15671 DECL_EXTERNAL (r) = 1;
15672 if (DECL_NAMESPACE_SCOPE_P (t))
15673 DECL_NOT_REALLY_EXTERN (r) = 1;
15674
15675 DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: tmpl, template_args: argvec);
15676 SET_DECL_IMPLICIT_INSTANTIATION (r);
15677 if (use_spec_table)
15678 register_specialization (spec: r, tmpl: gen_tmpl, args: argvec, is_friend: false, hash);
15679 }
15680 else
15681 {
15682 if (DECL_LANG_SPECIFIC (r))
15683 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15684 if (!cp_unevaluated_operand)
15685 register_local_specialization (spec: r, tmpl: t);
15686 }
15687
15688 DECL_CHAIN (r) = NULL_TREE;
15689
15690 if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r),
15691 /*flags=*/attr_flags: 0,
15692 args, complain, in_decl))
15693 return error_mark_node;
15694
15695 /* Preserve a typedef that names a type. */
15696 if (is_typedef_decl (x: r) && type != error_mark_node)
15697 {
15698 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
15699 set_underlying_type (r);
15700
15701 /* common_handle_aligned_attribute doesn't apply the alignment
15702 to DECL_ORIGINAL_TYPE. */
15703 if (TYPE_USER_ALIGN (TREE_TYPE (t)))
15704 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
15705 TYPE_ALIGN (TREE_TYPE (t)));
15706 }
15707
15708 layout_decl (r, 0);
15709 }
15710 break;
15711
15712 default:
15713 gcc_unreachable ();
15714 }
15715#undef RETURN
15716
15717 out:
15718 /* Restore the file and line information. */
15719 input_location = saved_loc;
15720
15721 return r;
15722}
15723
15724/* Substitute into the complete parameter type list PARMS. */
15725
15726tree
15727tsubst_function_parms (tree parms,
15728 tree args,
15729 tsubst_flags_t complain,
15730 tree in_decl)
15731{
15732 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
15733}
15734
15735/* Substitute into the ARG_TYPES of a function type.
15736 If END is a TREE_CHAIN, leave it and any following types
15737 un-substituted. */
15738
15739static tree
15740tsubst_arg_types (tree arg_types,
15741 tree args,
15742 tree end,
15743 tsubst_flags_t complain,
15744 tree in_decl)
15745{
15746 tree type = NULL_TREE;
15747 int len = 1;
15748 tree expanded_args = NULL_TREE;
15749
15750 if (!arg_types || arg_types == void_list_node || arg_types == end)
15751 return arg_types;
15752
15753 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
15754 {
15755 /* For a pack expansion, perform substitution on the
15756 entire expression. Later on, we'll handle the arguments
15757 one-by-one. */
15758 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
15759 args, complain, in_decl);
15760
15761 if (TREE_CODE (expanded_args) == TREE_VEC)
15762 /* So that we'll spin through the parameters, one by one. */
15763 len = TREE_VEC_LENGTH (expanded_args);
15764 else
15765 {
15766 /* We only partially substituted into the parameter
15767 pack. Our type is TYPE_PACK_EXPANSION. */
15768 type = expanded_args;
15769 expanded_args = NULL_TREE;
15770 }
15771 }
15772 else
15773 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
15774
15775 /* Check if a substituted type is erroneous before substituting into
15776 the rest of the chain. */
15777 for (int i = 0; i < len; i++)
15778 {
15779 if (expanded_args)
15780 type = TREE_VEC_ELT (expanded_args, i);
15781
15782 if (type == error_mark_node)
15783 return error_mark_node;
15784 if (VOID_TYPE_P (type))
15785 {
15786 if (complain & tf_error)
15787 {
15788 error ("invalid parameter type %qT", type);
15789 if (in_decl)
15790 error ("in declaration %q+D", in_decl);
15791 }
15792 return error_mark_node;
15793 }
15794 }
15795
15796 /* We do not substitute into default arguments here. The standard
15797 mandates that they be instantiated only when needed, which is
15798 done in build_over_call. */
15799 tree default_arg = TREE_PURPOSE (arg_types);
15800
15801 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15802 since the new op() won't have any associated template arguments for us
15803 to refer to later. */
15804 if (lambda_fn_in_template_p (fn: in_decl)
15805 || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
15806 && DECL_LOCAL_DECL_P (in_decl)))
15807 default_arg = tsubst_expr (default_arg, args, complain, in_decl);
15808
15809 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15810 args, end, complain, in_decl);
15811 if (remaining_arg_types == error_mark_node)
15812 return error_mark_node;
15813
15814 for (int i = len-1; i >= 0; i--)
15815 {
15816 if (expanded_args)
15817 type = TREE_VEC_ELT (expanded_args, i);
15818
15819 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15820 top-level qualifiers as required. */
15821 type = cv_unqualified (type_decays_to (type));
15822
15823 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15824 {
15825 /* We've instantiated a template before its default arguments
15826 have been parsed. This can happen for a nested template
15827 class, and is not an error unless we require the default
15828 argument in a call of this function. */
15829 remaining_arg_types
15830 = tree_cons (default_arg, type, remaining_arg_types);
15831 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15832 obj: remaining_arg_types);
15833 }
15834 else
15835 remaining_arg_types
15836 = hash_tree_cons (default_arg, type, remaining_arg_types);
15837 }
15838
15839 return remaining_arg_types;
15840}
15841
15842/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15843 *not* handle the exception-specification for FNTYPE, because the
15844 initial substitution of explicitly provided template parameters
15845 during argument deduction forbids substitution into the
15846 exception-specification:
15847
15848 [temp.deduct]
15849
15850 All references in the function type of the function template to the
15851 corresponding template parameters are replaced by the specified tem-
15852 plate argument values. If a substitution in a template parameter or
15853 in the function type of the function template results in an invalid
15854 type, type deduction fails. [Note: The equivalent substitution in
15855 exception specifications is done only when the function is instanti-
15856 ated, at which point a program is ill-formed if the substitution
15857 results in an invalid type.] */
15858
15859static tree
15860tsubst_function_type (tree t,
15861 tree args,
15862 tsubst_flags_t complain,
15863 tree in_decl)
15864{
15865 tree return_type;
15866 tree arg_types = NULL_TREE;
15867
15868 /* The TYPE_CONTEXT is not used for function/method types. */
15869 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15870
15871 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15872 failure. */
15873 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15874
15875 if (late_return_type_p)
15876 {
15877 /* Substitute the argument types. */
15878 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15879 complain, in_decl);
15880 if (arg_types == error_mark_node)
15881 return error_mark_node;
15882
15883 tree save_ccp = current_class_ptr;
15884 tree save_ccr = current_class_ref;
15885 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15886 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15887 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15888 if (do_inject)
15889 {
15890 /* DR 1207: 'this' is in scope in the trailing return type. */
15891 inject_this_parameter (this_type, cp_type_quals (this_type));
15892 }
15893
15894 /* Substitute the return type. */
15895 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15896
15897 if (do_inject)
15898 {
15899 current_class_ptr = save_ccp;
15900 current_class_ref = save_ccr;
15901 }
15902 }
15903 else
15904 /* Substitute the return type. */
15905 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15906
15907 if (return_type == error_mark_node)
15908 return error_mark_node;
15909 /* DR 486 clarifies that creation of a function type with an
15910 invalid return type is a deduction failure. */
15911 if (TREE_CODE (return_type) == ARRAY_TYPE
15912 || TREE_CODE (return_type) == FUNCTION_TYPE)
15913 {
15914 if (complain & tf_error)
15915 {
15916 if (TREE_CODE (return_type) == ARRAY_TYPE)
15917 error ("function returning an array");
15918 else
15919 error ("function returning a function");
15920 }
15921 return error_mark_node;
15922 }
15923
15924 if (!late_return_type_p)
15925 {
15926 /* Substitute the argument types. */
15927 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15928 complain, in_decl);
15929 if (arg_types == error_mark_node)
15930 return error_mark_node;
15931 }
15932
15933 /* Construct a new type node and return it. */
15934 return rebuild_function_or_method_type (t, return_type, arg_types,
15935 /*raises=*/NULL_TREE, complain);
15936}
15937
15938/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15939 ARGS into that specification, and return the substituted
15940 specification. If there is no specification, return NULL_TREE. */
15941
15942static tree
15943tsubst_exception_specification (tree fntype,
15944 tree args,
15945 tsubst_flags_t complain,
15946 tree in_decl,
15947 bool defer_ok)
15948{
15949 tree specs;
15950 tree new_specs;
15951
15952 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15953 new_specs = NULL_TREE;
15954 if (specs && TREE_PURPOSE (specs))
15955 {
15956 /* A noexcept-specifier. */
15957 tree expr = TREE_PURPOSE (specs);
15958 if (TREE_CODE (expr) == INTEGER_CST)
15959 new_specs = expr;
15960 else if (defer_ok)
15961 {
15962 /* Defer instantiation of noexcept-specifiers to avoid
15963 excessive instantiations (c++/49107). */
15964 new_specs = make_node (DEFERRED_NOEXCEPT);
15965 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15966 {
15967 /* We already partially instantiated this member template,
15968 so combine the new args with the old. */
15969 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15970 = DEFERRED_NOEXCEPT_PATTERN (expr);
15971 DEFERRED_NOEXCEPT_ARGS (new_specs)
15972 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), extra_args: args);
15973 }
15974 else
15975 {
15976 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15977 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15978 }
15979 }
15980 else
15981 {
15982 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15983 {
15984 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15985 extra_args: args);
15986 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15987 }
15988 new_specs = tsubst_expr (expr, args, complain, in_decl);
15989 }
15990 new_specs = build_noexcept_spec (new_specs, complain);
15991 /* We've instantiated a template before a noexcept-specifier
15992 contained therein has been parsed. This can happen for
15993 a nested template class:
15994
15995 struct S {
15996 template<typename> struct B { B() noexcept(...); };
15997 struct A : B<int> { ... use B() ... };
15998 };
15999
16000 where completing B<int> will trigger instantiating the
16001 noexcept, even though we only parse it at the end of S. */
16002 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
16003 {
16004 gcc_checking_assert (defer_ok);
16005 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), obj: new_specs);
16006 }
16007 }
16008 else if (specs)
16009 {
16010 if (! TREE_VALUE (specs))
16011 new_specs = specs;
16012 else
16013 while (specs)
16014 {
16015 tree spec;
16016 int i, len = 1;
16017 tree expanded_specs = NULL_TREE;
16018
16019 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
16020 {
16021 /* Expand the pack expansion type. */
16022 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
16023 args, complain,
16024 in_decl);
16025
16026 if (expanded_specs == error_mark_node)
16027 return error_mark_node;
16028 else if (TREE_CODE (expanded_specs) == TREE_VEC)
16029 len = TREE_VEC_LENGTH (expanded_specs);
16030 else
16031 {
16032 /* We're substituting into a member template, so
16033 we got a TYPE_PACK_EXPANSION back. Add that
16034 expansion and move on. */
16035 gcc_assert (TREE_CODE (expanded_specs)
16036 == TYPE_PACK_EXPANSION);
16037 new_specs = add_exception_specifier (new_specs,
16038 expanded_specs,
16039 complain);
16040 specs = TREE_CHAIN (specs);
16041 continue;
16042 }
16043 }
16044
16045 for (i = 0; i < len; ++i)
16046 {
16047 if (expanded_specs)
16048 spec = TREE_VEC_ELT (expanded_specs, i);
16049 else
16050 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
16051 if (spec == error_mark_node)
16052 return spec;
16053 new_specs = add_exception_specifier (new_specs, spec,
16054 complain);
16055 }
16056
16057 specs = TREE_CHAIN (specs);
16058 }
16059 }
16060 return new_specs;
16061}
16062
16063/* Substitute through a TREE_LIST of types or expressions, handling pack
16064 expansions. */
16065
16066tree
16067tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16068{
16069 if (t == void_list_node)
16070 return t;
16071
16072 tree purpose = TREE_PURPOSE (t);
16073 tree purposevec = NULL_TREE;
16074 if (!purpose)
16075 ;
16076 else if (PACK_EXPANSION_P (purpose))
16077 {
16078 purpose = tsubst_pack_expansion (t: purpose, args, complain, in_decl);
16079 if (TREE_CODE (purpose) == TREE_VEC)
16080 purposevec = purpose;
16081 }
16082 else if (TYPE_P (purpose))
16083 purpose = tsubst (purpose, args, complain, in_decl);
16084 else
16085 purpose = tsubst_expr (purpose, args, complain, in_decl);
16086 if (purpose == error_mark_node || purposevec == error_mark_node)
16087 return error_mark_node;
16088
16089 tree value = TREE_VALUE (t);
16090 tree valuevec = NULL_TREE;
16091 if (!value)
16092 ;
16093 else if (PACK_EXPANSION_P (value))
16094 {
16095 value = tsubst_pack_expansion (t: value, args, complain, in_decl);
16096 if (TREE_CODE (value) == TREE_VEC)
16097 valuevec = value;
16098 }
16099 else if (TYPE_P (value))
16100 value = tsubst (value, args, complain, in_decl);
16101 else
16102 value = tsubst_expr (value, args, complain, in_decl);
16103 if (value == error_mark_node || valuevec == error_mark_node)
16104 return error_mark_node;
16105
16106 tree chain = TREE_CHAIN (t);
16107 if (!chain)
16108 ;
16109 else if (TREE_CODE (chain) == TREE_LIST)
16110 chain = tsubst_tree_list (t: chain, args, complain, in_decl);
16111 else if (TYPE_P (chain))
16112 chain = tsubst (chain, args, complain, in_decl);
16113 else
16114 chain = tsubst_expr (chain, args, complain, in_decl);
16115 if (chain == error_mark_node)
16116 return error_mark_node;
16117
16118 if (purpose == TREE_PURPOSE (t)
16119 && value == TREE_VALUE (t)
16120 && chain == TREE_CHAIN (t))
16121 return t;
16122
16123 int len;
16124 /* Determine the number of arguments. */
16125 if (purposevec)
16126 {
16127 len = TREE_VEC_LENGTH (purposevec);
16128 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16129 }
16130 else if (valuevec)
16131 len = TREE_VEC_LENGTH (valuevec);
16132 else
16133 len = 1;
16134
16135 for (int i = len; i-- > 0; )
16136 {
16137 if (purposevec)
16138 purpose = TREE_VEC_ELT (purposevec, i);
16139 if (valuevec)
16140 value = TREE_VEC_ELT (valuevec, i);
16141
16142 if (value && TYPE_P (value))
16143 chain = hash_tree_cons (purpose, value, chain);
16144 else
16145 chain = tree_cons (purpose, value, chain);
16146 }
16147
16148 return chain;
16149}
16150
16151/* Take the tree structure T and replace template parameters used
16152 therein with the argument vector ARGS. IN_DECL is an associated
16153 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
16154 Issue error and warning messages under control of COMPLAIN. Note
16155 that we must be relatively non-tolerant of extensions here, in
16156 order to preserve conformance; if we allow substitutions that
16157 should not be allowed, we may allow argument deductions that should
16158 not succeed, and therefore report ambiguous overload situations
16159 where there are none. In theory, we could allow the substitution,
16160 but indicate that it should have failed, and allow our caller to
16161 make sure that the right thing happens, but we don't try to do this
16162 yet.
16163
16164 This function is used for dealing with types, decls and the like;
16165 for expressions, use tsubst_expr or tsubst_copy. */
16166
16167tree
16168tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16169{
16170 enum tree_code code;
16171 tree type, r = NULL_TREE;
16172
16173 if (t == NULL_TREE || t == error_mark_node
16174 || t == integer_type_node
16175 || t == void_type_node
16176 || t == char_type_node
16177 || t == unknown_type_node
16178 || TREE_CODE (t) == NAMESPACE_DECL
16179 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
16180 return t;
16181
16182 tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
16183 complain &= ~tf_tst_ok;
16184
16185 tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
16186 complain &= ~tf_qualifying_scope;
16187
16188 if (DECL_P (t))
16189 return tsubst_decl (t, args, complain);
16190
16191 if (args == NULL_TREE)
16192 return t;
16193
16194 code = TREE_CODE (t);
16195
16196 gcc_assert (code != IDENTIFIER_NODE);
16197 type = TREE_TYPE (t);
16198
16199 gcc_assert (type != unknown_type_node);
16200
16201 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
16202 return d;
16203
16204 /* Reuse typedefs. We need to do this to handle dependent attributes,
16205 such as attribute aligned. */
16206 if (TYPE_P (t)
16207 && typedef_variant_p (type: t))
16208 {
16209 tree decl = TYPE_NAME (t);
16210
16211 if (alias_template_specialization_p (t, transparent_typedefs: nt_opaque))
16212 {
16213 /* DECL represents an alias template and we want to
16214 instantiate it. */
16215 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16216 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16217 r = instantiate_alias_template (tmpl, gen_args, complain);
16218 }
16219 else if (DECL_CLASS_SCOPE_P (decl)
16220 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
16221 && uses_template_parms (DECL_CONTEXT (decl)))
16222 {
16223 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16224 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16225 r = retrieve_specialization (tmpl, args: gen_args, hash: 0);
16226 }
16227 else if (DECL_FUNCTION_SCOPE_P (decl)
16228 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
16229 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
16230 r = retrieve_local_specialization (tmpl: decl);
16231 else
16232 /* The typedef is from a non-template context. */
16233 return t;
16234
16235 if (r)
16236 {
16237 r = TREE_TYPE (r);
16238 r = cp_build_qualified_type
16239 (r, cp_type_quals (t) | cp_type_quals (r),
16240 complain | tf_ignore_bad_quals);
16241 return r;
16242 }
16243 else
16244 {
16245 /* We don't have an instantiation yet, so drop the typedef. */
16246 int quals = cp_type_quals (t);
16247 t = DECL_ORIGINAL_TYPE (decl);
16248 t = cp_build_qualified_type (t, quals,
16249 complain | tf_ignore_bad_quals);
16250 }
16251 }
16252
16253 bool fndecl_type = (complain & tf_fndecl_type);
16254 complain &= ~tf_fndecl_type;
16255
16256 if (type
16257 && code != TYPENAME_TYPE
16258 && code != TEMPLATE_TYPE_PARM
16259 && code != TEMPLATE_PARM_INDEX
16260 && code != IDENTIFIER_NODE
16261 && code != FUNCTION_TYPE
16262 && code != METHOD_TYPE)
16263 type = tsubst (t: type, args, complain, in_decl);
16264 if (type == error_mark_node)
16265 return error_mark_node;
16266
16267 switch (code)
16268 {
16269 case RECORD_TYPE:
16270 if (TYPE_PTRMEMFUNC_P (t))
16271 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
16272 /* Fall through. */
16273 case UNION_TYPE:
16274 case ENUMERAL_TYPE:
16275 return tsubst_aggr_type_1 (t, args, complain, in_decl,
16276 /*entering_scope=*/0);
16277
16278 case ERROR_MARK:
16279 case IDENTIFIER_NODE:
16280 case VOID_TYPE:
16281 case OPAQUE_TYPE:
16282 case REAL_TYPE:
16283 case COMPLEX_TYPE:
16284 case VECTOR_TYPE:
16285 case BOOLEAN_TYPE:
16286 case NULLPTR_TYPE:
16287 case LANG_TYPE:
16288 return t;
16289
16290 case INTEGER_TYPE:
16291 if (t == integer_type_node)
16292 return t;
16293
16294 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
16295 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
16296 return t;
16297
16298 {
16299 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
16300
16301 max = tsubst_expr (omax, args, complain, in_decl);
16302
16303 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
16304 needed. */
16305 if (TREE_CODE (max) == NOP_EXPR
16306 && TREE_SIDE_EFFECTS (omax)
16307 && !TREE_TYPE (max))
16308 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
16309
16310 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
16311 with TREE_SIDE_EFFECTS that indicates this is not an integral
16312 constant expression. */
16313 if (processing_template_decl
16314 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
16315 {
16316 gcc_assert (TREE_CODE (max) == NOP_EXPR);
16317 TREE_SIDE_EFFECTS (max) = 1;
16318 }
16319
16320 return compute_array_index_type (NULL_TREE, max, complain);
16321 }
16322
16323 case TEMPLATE_TYPE_PARM:
16324 if (TEMPLATE_TYPE_LEVEL (t) == 0)
16325 {
16326 /* This is either an ordinary level-less auto or a CTAD placeholder
16327 auto. These get replaced only via do_auto_deduction which, in the
16328 ordinary case, temporarily overrides its level to 1 before calling
16329 tsubst. CTAD placeholders are replaced via do_class_deduction. */
16330 gcc_checking_assert (is_auto (t));
16331 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
16332 if (!tmpl)
16333 /* Ordinary level-less auto has nothing to substitute. */
16334 return t;
16335
16336 /* Substitute the template of this CTAD placeholder. */
16337 tmpl = tsubst_expr (tmpl, args, complain, in_decl);
16338 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16339 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
16340
16341 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
16342 return make_template_placeholder (tmpl);
16343 else
16344 return t;
16345 }
16346 /* Fall through. */
16347 case TEMPLATE_TEMPLATE_PARM:
16348 case BOUND_TEMPLATE_TEMPLATE_PARM:
16349 case TEMPLATE_PARM_INDEX:
16350 {
16351 int idx;
16352 int level;
16353 int levels;
16354 tree arg = NULL_TREE;
16355
16356 r = NULL_TREE;
16357
16358 gcc_assert (TREE_VEC_LENGTH (args) > 0);
16359 template_parm_level_and_index (t, &level, &idx);
16360
16361 levels = TMPL_ARGS_DEPTH (args);
16362 if (level <= levels
16363 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
16364 {
16365 arg = TMPL_ARG (args, level, idx);
16366
16367 /* See through ARGUMENT_PACK_SELECT arguments. */
16368 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
16369 arg = argument_pack_select_arg (t: arg);
16370 }
16371
16372 if (arg == error_mark_node)
16373 return error_mark_node;
16374 else if (arg != NULL_TREE)
16375 {
16376 if (ARGUMENT_PACK_P (arg))
16377 /* If ARG is an argument pack, we don't actually want to
16378 perform a substitution here, because substitutions
16379 for argument packs are only done
16380 element-by-element. We can get to this point when
16381 substituting the type of a non-type template
16382 parameter pack, when that type actually contains
16383 template parameter packs from an outer template, e.g.,
16384
16385 template<typename... Types> struct A {
16386 template<Types... Values> struct B { };
16387 }; */
16388 return t;
16389
16390 if (code == TEMPLATE_TYPE_PARM)
16391 {
16392 int quals;
16393
16394 /* When building concept checks for the purpose of
16395 deducing placeholders, we can end up with wildcards
16396 where types are expected. Adjust this to the deduced
16397 value. */
16398 if (TREE_CODE (arg) == WILDCARD_DECL)
16399 arg = TREE_TYPE (TREE_TYPE (arg));
16400
16401 gcc_assert (TYPE_P (arg));
16402
16403 quals = cp_type_quals (arg) | cp_type_quals (t);
16404
16405 return cp_build_qualified_type
16406 (arg, quals, complain | tf_ignore_bad_quals);
16407 }
16408 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
16409 {
16410 /* We are processing a type constructed from a
16411 template template parameter. */
16412 tree argvec = tsubst (TYPE_TI_ARGS (t),
16413 args, complain, in_decl);
16414 if (argvec == error_mark_node)
16415 return error_mark_node;
16416
16417 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
16418 || TREE_CODE (arg) == TEMPLATE_DECL
16419 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
16420
16421 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
16422 /* Consider this code:
16423
16424 template <template <class> class Template>
16425 struct Internal {
16426 template <class Arg> using Bind = Template<Arg>;
16427 };
16428
16429 template <template <class> class Template, class Arg>
16430 using Instantiate = Template<Arg>; //#0
16431
16432 template <template <class> class Template,
16433 class Argument>
16434 using Bind =
16435 Instantiate<Internal<Template>::template Bind,
16436 Argument>; //#1
16437
16438 When #1 is parsed, the
16439 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16440 parameter `Template' in #0 matches the
16441 UNBOUND_CLASS_TEMPLATE representing the argument
16442 `Internal<Template>::template Bind'; We then want
16443 to assemble the type `Bind<Argument>' that can't
16444 be fully created right now, because
16445 `Internal<Template>' not being complete, the Bind
16446 template cannot be looked up in that context. So
16447 we need to "store" `Bind<Argument>' for later
16448 when the context of Bind becomes complete. Let's
16449 store that in a TYPENAME_TYPE. */
16450 return make_typename_type (TYPE_CONTEXT (arg),
16451 build_nt (TEMPLATE_ID_EXPR,
16452 TYPE_IDENTIFIER (arg),
16453 argvec),
16454 typename_type,
16455 complain);
16456
16457 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16458 are resolving nested-types in the signature of a
16459 member function templates. Otherwise ARG is a
16460 TEMPLATE_DECL and is the real template to be
16461 instantiated. */
16462 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16463 arg = TYPE_NAME (arg);
16464
16465 r = lookup_template_class (d1: arg,
16466 arglist: argvec, in_decl,
16467 DECL_CONTEXT (arg),
16468 /*entering_scope=*/0,
16469 complain);
16470 return cp_build_qualified_type
16471 (r, cp_type_quals (t) | cp_type_quals (r), complain);
16472 }
16473 else if (code == TEMPLATE_TEMPLATE_PARM)
16474 return arg;
16475 else
16476 /* TEMPLATE_PARM_INDEX. */
16477 return convert_from_reference (unshare_expr (arg));
16478 }
16479
16480 if (level == 1)
16481 /* This can happen during the attempted tsubst'ing in
16482 unify. This means that we don't yet have any information
16483 about the template parameter in question. */
16484 return t;
16485
16486 /* Early in template argument deduction substitution, we don't
16487 want to reduce the level of 'auto', or it will be confused
16488 with a normal template parm in subsequent deduction.
16489 Similarly, don't reduce the level of template parameters to
16490 avoid mismatches when deducing their types. */
16491 if (complain & tf_partial)
16492 return t;
16493
16494 /* If we get here, we must have been looking at a parm for a
16495 more deeply nested template. Make a new version of this
16496 template parameter, but with a lower level. */
16497 int quals;
16498 switch (code)
16499 {
16500 case TEMPLATE_TYPE_PARM:
16501 case TEMPLATE_TEMPLATE_PARM:
16502 quals = cp_type_quals (t);
16503 if (quals)
16504 {
16505 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
16506 t = TYPE_MAIN_VARIANT (t);
16507 }
16508
16509 if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
16510 if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
16511 && (code == TEMPLATE_TYPE_PARM
16512 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
16513 /* Cache lowering a type parameter or a simple template
16514 template parameter. */
16515 r = TREE_TYPE (d);
16516
16517 if (!r)
16518 {
16519 r = copy_type (t);
16520 TEMPLATE_TYPE_PARM_INDEX (r)
16521 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
16522 type: r, levels, args, complain);
16523 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
16524 TYPE_MAIN_VARIANT (r) = r;
16525 TYPE_POINTER_TO (r) = NULL_TREE;
16526 TYPE_REFERENCE_TO (r) = NULL_TREE;
16527
16528 if (code == TEMPLATE_TYPE_PARM)
16529 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
16530 /* Propagate constraints on placeholders since they are
16531 only instantiated during satisfaction. */
16532 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
16533
16534 if (TYPE_STRUCTURAL_EQUALITY_P (t))
16535 SET_TYPE_STRUCTURAL_EQUALITY (r);
16536 else
16537 TYPE_CANONICAL (r) = canonical_type_parameter (type: r);
16538 }
16539
16540 if (quals)
16541 r = cp_build_qualified_type (r, quals,
16542 complain | tf_ignore_bad_quals);
16543 break;
16544
16545 case BOUND_TEMPLATE_TEMPLATE_PARM:
16546 {
16547 tree tinfo = TYPE_TEMPLATE_INFO (t);
16548 /* We might need to substitute into the types of non-type
16549 template parameters. This also lowers the level of
16550 the ttp appropriately. */
16551 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
16552 complain, in_decl);
16553 if (tmpl == error_mark_node)
16554 return error_mark_node;
16555 tree argvec = tsubst (TI_ARGS (tinfo), args,
16556 complain, in_decl);
16557 if (argvec == error_mark_node)
16558 return error_mark_node;
16559 r = lookup_template_class (d1: tmpl, arglist: argvec, in_decl, NULL_TREE,
16560 /*entering_scope=*/false, complain);
16561 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16562 break;
16563 }
16564
16565 case TEMPLATE_PARM_INDEX:
16566 /* OK, now substitute the type of the non-type parameter. We
16567 couldn't do it earlier because it might be an auto parameter,
16568 and we wouldn't need to if we had an argument. */
16569 type = tsubst (t: type, args, complain, in_decl);
16570 if (type == error_mark_node)
16571 return error_mark_node;
16572 r = reduce_template_parm_level (index: t, type, levels, args, complain);
16573 break;
16574
16575 default:
16576 gcc_unreachable ();
16577 }
16578
16579 return r;
16580 }
16581
16582 case TREE_LIST:
16583 return tsubst_tree_list (t, args, complain, in_decl);
16584
16585 case TREE_BINFO:
16586 /* We should never be tsubsting a binfo. */
16587 gcc_unreachable ();
16588
16589 case TREE_VEC:
16590 /* A vector of template arguments. */
16591 gcc_assert (!type);
16592 return tsubst_template_args (t, args, complain, in_decl);
16593
16594 case POINTER_TYPE:
16595 case REFERENCE_TYPE:
16596 {
16597 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
16598 return t;
16599
16600 /* [temp.deduct]
16601
16602 Type deduction may fail for any of the following
16603 reasons:
16604
16605 -- Attempting to create a pointer to reference type.
16606 -- Attempting to create a reference to a reference type or
16607 a reference to void.
16608
16609 Core issue 106 says that creating a reference to a reference
16610 during instantiation is no longer a cause for failure. We
16611 only enforce this check in strict C++98 mode. */
16612 if ((TYPE_REF_P (type)
16613 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
16614 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
16615 {
16616 static location_t last_loc;
16617
16618 /* We keep track of the last time we issued this error
16619 message to avoid spewing a ton of messages during a
16620 single bad template instantiation. */
16621 if (complain & tf_error
16622 && last_loc != input_location)
16623 {
16624 if (VOID_TYPE_P (type))
16625 error ("forming reference to void");
16626 else if (code == POINTER_TYPE)
16627 error ("forming pointer to reference type %qT", type);
16628 else
16629 error ("forming reference to reference type %qT", type);
16630 last_loc = input_location;
16631 }
16632
16633 return error_mark_node;
16634 }
16635 else if (TREE_CODE (type) == FUNCTION_TYPE
16636 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
16637 || type_memfn_rqual (type) != REF_QUAL_NONE))
16638 {
16639 if (complain & tf_error)
16640 {
16641 if (code == POINTER_TYPE)
16642 error ("forming pointer to qualified function type %qT",
16643 type);
16644 else
16645 error ("forming reference to qualified function type %qT",
16646 type);
16647 }
16648 return error_mark_node;
16649 }
16650 else if (code == POINTER_TYPE)
16651 {
16652 r = build_pointer_type (type);
16653 if (TREE_CODE (type) == METHOD_TYPE)
16654 r = build_ptrmemfunc_type (r);
16655 }
16656 else if (TYPE_REF_P (type))
16657 /* In C++0x, during template argument substitution, when there is an
16658 attempt to create a reference to a reference type, reference
16659 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16660
16661 "If a template-argument for a template-parameter T names a type
16662 that is a reference to a type A, an attempt to create the type
16663 'lvalue reference to cv T' creates the type 'lvalue reference to
16664 A,' while an attempt to create the type type rvalue reference to
16665 cv T' creates the type T"
16666 */
16667 r = cp_build_reference_type
16668 (TREE_TYPE (type),
16669 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
16670 else
16671 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
16672 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16673
16674 if (r != error_mark_node)
16675 /* Will this ever be needed for TYPE_..._TO values? */
16676 layout_type (r);
16677
16678 return r;
16679 }
16680 case OFFSET_TYPE:
16681 {
16682 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
16683 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
16684 {
16685 /* [temp.deduct]
16686
16687 Type deduction may fail for any of the following
16688 reasons:
16689
16690 -- Attempting to create "pointer to member of T" when T
16691 is not a class type. */
16692 if (complain & tf_error)
16693 error ("creating pointer to member of non-class type %qT", r);
16694 return error_mark_node;
16695 }
16696 if (TYPE_REF_P (type))
16697 {
16698 if (complain & tf_error)
16699 error ("creating pointer to member reference type %qT", type);
16700 return error_mark_node;
16701 }
16702 if (VOID_TYPE_P (type))
16703 {
16704 if (complain & tf_error)
16705 error ("creating pointer to member of type void");
16706 return error_mark_node;
16707 }
16708 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
16709 if (TREE_CODE (type) == FUNCTION_TYPE)
16710 {
16711 /* The type of the implicit object parameter gets its
16712 cv-qualifiers from the FUNCTION_TYPE. */
16713 tree memptr;
16714 tree method_type
16715 = build_memfn_type (type, r, type_memfn_quals (type),
16716 type_memfn_rqual (type));
16717 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
16718 return cp_build_qualified_type (memptr, cp_type_quals (t),
16719 complain);
16720 }
16721 else
16722 return cp_build_qualified_type (build_ptrmem_type (r, type),
16723 cp_type_quals (t),
16724 complain);
16725 }
16726 case FUNCTION_TYPE:
16727 case METHOD_TYPE:
16728 {
16729 tree fntype;
16730 tree specs;
16731 fntype = tsubst_function_type (t, args, complain, in_decl);
16732 if (fntype == error_mark_node)
16733 return error_mark_node;
16734
16735 /* Substitute the exception specification. */
16736 specs = tsubst_exception_specification (fntype: t, args, complain, in_decl,
16737 /*defer_ok*/fndecl_type);
16738 if (specs == error_mark_node)
16739 return error_mark_node;
16740 if (specs)
16741 fntype = build_exception_variant (fntype, specs);
16742 return fntype;
16743 }
16744 case ARRAY_TYPE:
16745 {
16746 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
16747 if (domain == error_mark_node)
16748 return error_mark_node;
16749
16750 /* As an optimization, we avoid regenerating the array type if
16751 it will obviously be the same as T. */
16752 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
16753 return t;
16754
16755 /* These checks should match the ones in create_array_type_for_decl.
16756
16757 [temp.deduct]
16758
16759 The deduction may fail for any of the following reasons:
16760
16761 -- Attempting to create an array with an element type that
16762 is void, a function type, or a reference type, or [DR337]
16763 an abstract class type. */
16764 if (VOID_TYPE_P (type)
16765 || TREE_CODE (type) == FUNCTION_TYPE
16766 || (TREE_CODE (type) == ARRAY_TYPE
16767 && TYPE_DOMAIN (type) == NULL_TREE)
16768 || TYPE_REF_P (type))
16769 {
16770 if (complain & tf_error)
16771 error ("creating array of %qT", type);
16772 return error_mark_node;
16773 }
16774
16775 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
16776 !(complain & tf_error)))
16777 return error_mark_node;
16778
16779 r = build_cplus_array_type (type, domain);
16780
16781 if (!valid_array_size_p (input_location, r, in_decl,
16782 (complain & tf_error)))
16783 return error_mark_node;
16784
16785 if (TYPE_USER_ALIGN (t))
16786 {
16787 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
16788 TYPE_USER_ALIGN (r) = 1;
16789 }
16790
16791 return r;
16792 }
16793
16794 case TYPENAME_TYPE:
16795 {
16796 tree ctx = TYPE_CONTEXT (t);
16797 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16798 {
16799 ctx = tsubst_pack_expansion (t: ctx, args,
16800 complain: complain | tf_qualifying_scope,
16801 in_decl);
16802 if (ctx == error_mark_node
16803 || TREE_VEC_LENGTH (ctx) > 1)
16804 return error_mark_node;
16805 if (TREE_VEC_LENGTH (ctx) == 0)
16806 {
16807 if (complain & tf_error)
16808 error ("%qD is instantiated for an empty pack",
16809 TYPENAME_TYPE_FULLNAME (t));
16810 return error_mark_node;
16811 }
16812 ctx = TREE_VEC_ELT (ctx, 0);
16813 }
16814 else
16815 ctx = tsubst_aggr_type (t: ctx, args,
16816 complain: complain | tf_qualifying_scope,
16817 in_decl, /*entering_scope=*/1);
16818 if (ctx == error_mark_node)
16819 return error_mark_node;
16820
16821 tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
16822 complain, in_decl);
16823 if (f == error_mark_node)
16824 return error_mark_node;
16825
16826 if (!MAYBE_CLASS_TYPE_P (ctx))
16827 {
16828 if (complain & tf_error)
16829 error ("%qT is not a class, struct, or union type", ctx);
16830 return error_mark_node;
16831 }
16832 else if (!uses_template_parms (t: ctx) && !TYPE_BEING_DEFINED (ctx))
16833 {
16834 /* Normally, make_typename_type does not require that the CTX
16835 have complete type in order to allow things like:
16836
16837 template <class T> struct S { typename S<T>::X Y; };
16838
16839 But, such constructs have already been resolved by this
16840 point, so here CTX really should have complete type, unless
16841 it's a partial instantiation. */
16842 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16843 return error_mark_node;
16844 }
16845
16846 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
16847 tags. TYPENAME_TYPE should probably remember the exact tag that
16848 was written. */
16849 enum tag_types tag_type
16850 = TYPENAME_IS_CLASS_P (t) ? class_type
16851 : TYPENAME_IS_ENUM_P (t) ? enum_type
16852 : typename_type;
16853 tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
16854 tcomplain |= tst_ok_flag | qualifying_scope_flag;
16855 f = make_typename_type (ctx, f, tag_type, tcomplain);
16856 if (f == error_mark_node)
16857 return f;
16858 if (TREE_CODE (f) == TYPE_DECL)
16859 {
16860 complain |= tf_ignore_bad_quals;
16861 f = TREE_TYPE (f);
16862 }
16863
16864 if (TREE_CODE (f) != TYPENAME_TYPE)
16865 {
16866 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16867 {
16868 if (complain & tf_error)
16869 error ("%qT resolves to %qT, which is not an enumeration type",
16870 t, f);
16871 else
16872 return error_mark_node;
16873 }
16874 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16875 {
16876 if (complain & tf_error)
16877 error ("%qT resolves to %qT, which is not a class type",
16878 t, f);
16879 else
16880 return error_mark_node;
16881 }
16882 }
16883
16884 return cp_build_qualified_type
16885 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16886 }
16887
16888 case UNBOUND_CLASS_TEMPLATE:
16889 {
16890 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16891 in_decl, /*entering_scope=*/1);
16892 tree name = TYPE_IDENTIFIER (t);
16893 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16894
16895 if (ctx == error_mark_node || name == error_mark_node)
16896 return error_mark_node;
16897
16898 if (parm_list)
16899 parm_list = tsubst_template_parms (parms: parm_list, args, complain);
16900 return make_unbound_class_template (ctx, name, parm_list, complain);
16901 }
16902
16903 case TYPEOF_TYPE:
16904 {
16905 tree type;
16906
16907 ++cp_unevaluated_operand;
16908 ++c_inhibit_evaluation_warnings;
16909
16910 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
16911
16912 --cp_unevaluated_operand;
16913 --c_inhibit_evaluation_warnings;
16914
16915 type = finish_typeof (type);
16916 return cp_build_qualified_type (type,
16917 cp_type_quals (t)
16918 | cp_type_quals (type),
16919 complain);
16920 }
16921
16922 case DECLTYPE_TYPE:
16923 {
16924 tree type;
16925
16926 ++cp_unevaluated_operand;
16927 ++c_inhibit_evaluation_warnings;
16928
16929 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
16930 complain|tf_decltype, in_decl);
16931
16932 --cp_unevaluated_operand;
16933 --c_inhibit_evaluation_warnings;
16934
16935 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16936 type = lambda_capture_field_type (type,
16937 false /*explicit_init*/,
16938 DECLTYPE_FOR_REF_CAPTURE (t));
16939 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16940 type = lambda_proxy_type (type);
16941 else
16942 {
16943 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16944 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16945 && EXPR_P (type))
16946 /* In a template ~id could be either a complement expression
16947 or an unqualified-id naming a destructor; if instantiating
16948 it produces an expression, it's not an id-expression or
16949 member access. */
16950 id = false;
16951 type = finish_decltype_type (type, id, complain);
16952 }
16953 return cp_build_qualified_type (type,
16954 cp_type_quals (t)
16955 | cp_type_quals (type),
16956 complain | tf_ignore_bad_quals);
16957 }
16958
16959 case TRAIT_TYPE:
16960 {
16961 tree type1 = TRAIT_TYPE_TYPE1 (t);
16962 if (TYPE_P (type1))
16963 type1 = tsubst (t: type1, args, complain, in_decl);
16964 else
16965 type1 = tsubst_expr (type1, args, complain, in_decl);
16966 tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
16967 type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
16968 return cp_build_qualified_type (type,
16969 cp_type_quals (t) | cp_type_quals (type),
16970 complain | tf_ignore_bad_quals);
16971 }
16972
16973 case TYPE_ARGUMENT_PACK:
16974 case NONTYPE_ARGUMENT_PACK:
16975 return tsubst_argument_pack (orig_arg: t, args, complain, in_decl);
16976
16977 case VOID_CST:
16978 case INTEGER_CST:
16979 case REAL_CST:
16980 case STRING_CST:
16981 case PLUS_EXPR:
16982 case MINUS_EXPR:
16983 case NEGATE_EXPR:
16984 case NOP_EXPR:
16985 case INDIRECT_REF:
16986 case ADDR_EXPR:
16987 case CALL_EXPR:
16988 case ARRAY_REF:
16989 case SCOPE_REF:
16990 case OMP_ARRAY_SECTION:
16991 /* We should use one of the expression tsubsts for these codes. */
16992 gcc_unreachable ();
16993
16994 default:
16995 sorry ("use of %qs in template", get_tree_code_name (code));
16996 return error_mark_node;
16997 }
16998}
16999
17000/* Convenience wrapper over tsubst for substituting into the LHS
17001 of the :: scope resolution operator. */
17002
17003static tree
17004tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17005{
17006 gcc_checking_assert (TYPE_P (t));
17007 return tsubst (t, args, complain: complain | tf_qualifying_scope, in_decl);
17008}
17009
17010/* Convenience wrapper over tsubst for substituting into an id-expression
17011 without resolving its terminal name. */
17012
17013static tree
17014tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17015{
17016 return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
17017}
17018
17019/* OLDFNS is a lookup set of member functions from some class template, and
17020 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
17021 of that class template. Return the subset of NEWFNS which are
17022 specializations of a function from OLDFNS. */
17023
17024static tree
17025filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
17026{
17027 /* Record all member functions from the old lookup set OLDFNS into
17028 VISIBLE_SET. */
17029 hash_set<tree> visible_set;
17030 bool seen_dep_using = false;
17031 for (tree fn : lkp_range (oldfns))
17032 {
17033 if (TREE_CODE (fn) == USING_DECL)
17034 {
17035 /* Imprecisely handle dependent using-decl by keeping all members
17036 in the new lookup set that are defined in a base class, i.e.
17037 members that could plausibly have been introduced by this
17038 dependent using-decl.
17039 FIXME: Track which members are introduced by a dependent
17040 using-decl precisely, perhaps by performing another lookup
17041 from the substituted USING_DECL_SCOPE. */
17042 gcc_checking_assert (DECL_DEPENDENT_P (fn));
17043 seen_dep_using = true;
17044 }
17045 else
17046 visible_set.add (k: fn);
17047 }
17048
17049 /* Returns true iff (a less specialized version of) FN appeared in
17050 the old lookup set OLDFNS. */
17051 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
17052 if (DECL_CONTEXT (fn) != newtype)
17053 /* FN is a member function from a base class, introduced via a
17054 using-decl; if it might have been introduced by a dependent
17055 using-decl then just conservatively keep it, otherwise look
17056 in the old lookup set for FN exactly. */
17057 return seen_dep_using || visible_set.contains (k: fn);
17058 else if (TREE_CODE (fn) == TEMPLATE_DECL)
17059 /* FN is a member function template from the current class;
17060 look in the old lookup set for the TEMPLATE_DECL from which
17061 it was specialized. */
17062 return visible_set.contains (DECL_TI_TEMPLATE (fn));
17063 else
17064 /* FN is a non-template member function from the current class;
17065 look in the old lookup set for the FUNCTION_DECL from which
17066 it was specialized. */
17067 return visible_set.contains (DECL_TEMPLATE_RESULT
17068 (DECL_TI_TEMPLATE (fn)));
17069 };
17070
17071 bool lookup_changed_p = false;
17072 for (tree fn : lkp_range (newfns))
17073 if (!visible_p (fn))
17074 {
17075 lookup_changed_p = true;
17076 break;
17077 }
17078 if (!lookup_changed_p)
17079 return newfns;
17080
17081 /* Filter out from NEWFNS the member functions that weren't
17082 previously visible according to OLDFNS. */
17083 tree filtered_fns = NULL_TREE;
17084 unsigned filtered_size = 0;
17085 for (tree fn : lkp_range (newfns))
17086 if (visible_p (fn))
17087 {
17088 filtered_fns = lookup_add (fns: fn, lookup: filtered_fns);
17089 filtered_size++;
17090 }
17091 gcc_checking_assert (seen_dep_using
17092 ? filtered_size >= visible_set.elements ()
17093 : filtered_size == visible_set.elements ());
17094
17095 return filtered_fns;
17096}
17097
17098/* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
17099 expression on the left-hand side of the "." or "->" operator. We
17100 only do the lookup if we had a dependent BASELINK. Otherwise we
17101 adjust it onto the instantiated heirarchy. */
17102
17103static tree
17104tsubst_baselink (tree baselink, tree object_type,
17105 tree args, tsubst_flags_t complain, tree in_decl)
17106{
17107 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
17108 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
17109 qualifying_scope = tsubst (t: qualifying_scope, args, complain, in_decl);
17110
17111 tree optype = BASELINK_OPTYPE (baselink);
17112 optype = tsubst (t: optype, args, complain, in_decl);
17113
17114 tree template_args = NULL_TREE;
17115 bool template_id_p = false;
17116 tree fns = BASELINK_FUNCTIONS (baselink);
17117 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
17118 {
17119 template_id_p = true;
17120 template_args = TREE_OPERAND (fns, 1);
17121 fns = TREE_OPERAND (fns, 0);
17122 if (template_args)
17123 template_args = tsubst_template_args (t: template_args, args,
17124 complain, in_decl);
17125 }
17126
17127 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
17128 binfo_type = tsubst (t: binfo_type, args, complain, in_decl);
17129 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
17130 || optype != BASELINK_OPTYPE (baselink));
17131
17132 if (dependent_p)
17133 {
17134 tree name = OVL_NAME (fns);
17135 if (IDENTIFIER_CONV_OP_P (name))
17136 name = make_conv_op_name (optype);
17137
17138 /* See maybe_dependent_member_ref. */
17139 if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
17140 {
17141 if (template_id_p)
17142 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
17143 template_args);
17144 return build_qualified_name (NULL_TREE, qualifying_scope, name,
17145 /* ::template */false);
17146 }
17147
17148 if (name == complete_dtor_identifier)
17149 /* Treat as-if non-dependent below. */
17150 dependent_p = false;
17151
17152 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
17153 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
17154 complain);
17155 if (maybe_incomplete)
17156 {
17157 /* Filter out from the new lookup set those functions which didn't
17158 appear in the original lookup set (in a less specialized form).
17159 This is needed to preserve the consistency of member lookup
17160 performed in an incomplete-class context, within which
17161 later-declared members ought to remain invisible. */
17162 BASELINK_FUNCTIONS (baselink)
17163 = filter_memfn_lookup (oldfns: fns, BASELINK_FUNCTIONS (baselink),
17164 newtype: binfo_type);
17165 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
17166 }
17167
17168 if (!baselink)
17169 {
17170 if ((complain & tf_error)
17171 && constructor_name_p (name, qualifying_scope))
17172 error ("cannot call constructor %<%T::%D%> directly",
17173 qualifying_scope, name);
17174 return error_mark_node;
17175 }
17176
17177 fns = BASELINK_FUNCTIONS (baselink);
17178 }
17179 else
17180 {
17181 /* We're going to overwrite pieces below, make a duplicate. */
17182 baselink = copy_node (baselink);
17183
17184 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
17185 {
17186 /* The decl we found was from non-dependent scope, but we still need
17187 to update the binfos for the instantiated qualifying_scope. */
17188 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
17189 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
17190 ba_unique, nullptr, complain);
17191 }
17192 }
17193
17194 /* If lookup found a single function, mark it as used at this point.
17195 (If lookup found multiple functions the one selected later by
17196 overload resolution will be marked as used at that point.) */
17197 if (!template_id_p && !really_overloaded_fn (fns))
17198 {
17199 tree fn = OVL_FIRST (fns);
17200 bool ok = mark_used (fn, complain);
17201 if (!ok && !(complain & tf_error))
17202 return error_mark_node;
17203 if (ok && BASELINK_P (baselink))
17204 /* We might have instantiated an auto function. */
17205 TREE_TYPE (baselink) = TREE_TYPE (fn);
17206 }
17207
17208 if (BASELINK_P (baselink))
17209 {
17210 /* Add back the template arguments, if present. */
17211 if (template_id_p)
17212 BASELINK_FUNCTIONS (baselink)
17213 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
17214
17215 /* Update the conversion operator type. */
17216 BASELINK_OPTYPE (baselink) = optype;
17217 }
17218
17219 if (!object_type)
17220 object_type = current_class_type;
17221
17222 if (qualified_p || !dependent_p)
17223 {
17224 baselink = adjust_result_of_qualified_name_lookup (baselink,
17225 qualifying_scope,
17226 object_type);
17227 if (!qualified_p)
17228 /* We need to call adjust_result_of_qualified_name_lookup in case the
17229 destructor names a base class, but we unset BASELINK_QUALIFIED_P
17230 so that we still get virtual function binding. */
17231 BASELINK_QUALIFIED_P (baselink) = false;
17232 }
17233
17234 return baselink;
17235}
17236
17237/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
17238 true if the qualified-id will be a postfix-expression in-and-of
17239 itself; false if more of the postfix-expression follows the
17240 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
17241 of "&". */
17242
17243static tree
17244tsubst_qualified_id (tree qualified_id, tree args,
17245 tsubst_flags_t complain, tree in_decl,
17246 bool done, bool address_p)
17247{
17248 tree expr;
17249 tree scope;
17250 tree name;
17251 bool is_template;
17252 tree template_args;
17253 location_t loc = EXPR_LOCATION (qualified_id);
17254
17255 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
17256
17257 /* Figure out what name to look up. */
17258 name = TREE_OPERAND (qualified_id, 1);
17259 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17260 {
17261 is_template = true;
17262 template_args = TREE_OPERAND (name, 1);
17263 if (template_args)
17264 template_args = tsubst_template_args (t: template_args, args,
17265 complain, in_decl);
17266 if (template_args == error_mark_node)
17267 return error_mark_node;
17268 name = TREE_OPERAND (name, 0);
17269 }
17270 else
17271 {
17272 is_template = false;
17273 template_args = NULL_TREE;
17274 }
17275
17276 /* Substitute into the qualifying scope. When there are no ARGS, we
17277 are just trying to simplify a non-dependent expression. In that
17278 case the qualifying scope may be dependent, and, in any case,
17279 substituting will not help. */
17280 scope = TREE_OPERAND (qualified_id, 0);
17281 if (args)
17282 {
17283 scope = tsubst_scope (t: scope, args, complain, in_decl);
17284 expr = tsubst_name (t: name, args, complain, in_decl);
17285 }
17286 else
17287 expr = name;
17288
17289 if (dependent_scope_p (scope))
17290 {
17291 if (TREE_CODE (expr) == SCOPE_REF)
17292 /* We built one in tsubst_baselink. */
17293 gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
17294 else
17295 {
17296 if (is_template)
17297 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
17298 template_args);
17299 expr = build_qualified_name (NULL_TREE, scope, expr,
17300 QUALIFIED_NAME_IS_TEMPLATE
17301 (qualified_id));
17302 }
17303 REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
17304 return expr;
17305 }
17306
17307 if (!BASELINK_P (name) && !DECL_P (expr))
17308 {
17309 if (TREE_CODE (expr) == BIT_NOT_EXPR)
17310 {
17311 /* A BIT_NOT_EXPR is used to represent a destructor. */
17312 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
17313 {
17314 error ("qualifying type %qT does not match destructor name ~%qT",
17315 scope, TREE_OPERAND (expr, 0));
17316 expr = error_mark_node;
17317 }
17318 else
17319 expr = lookup_qualified_name (scope, complete_dtor_identifier,
17320 LOOK_want::NORMAL, false);
17321 }
17322 else
17323 expr = lookup_qualified_name (scope, name: expr, LOOK_want::NORMAL, false);
17324 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
17325 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
17326 {
17327 if (complain & tf_error)
17328 {
17329 error ("dependent-name %qE is parsed as a non-type, but "
17330 "instantiation yields a type", qualified_id);
17331 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
17332 }
17333 return error_mark_node;
17334 }
17335 }
17336
17337 if (DECL_P (expr))
17338 {
17339 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
17340 scope, complain))
17341 return error_mark_node;
17342 /* Remember that there was a reference to this entity. */
17343 if (!mark_used (expr, complain) && !(complain & tf_error))
17344 return error_mark_node;
17345 }
17346
17347 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
17348 {
17349 if (complain & tf_error)
17350 qualified_name_lookup_error (scope,
17351 TREE_OPERAND (qualified_id, 1),
17352 expr, input_location);
17353 return error_mark_node;
17354 }
17355
17356 if (is_template)
17357 {
17358 /* We may be repeating a check already done during parsing, but
17359 if it was well-formed and passed then, it will pass again
17360 now, and if it didn't, we wouldn't have got here. The case
17361 we want to catch is when we couldn't tell then, and can now,
17362 namely when templ prior to substitution was an
17363 identifier. */
17364 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
17365 return error_mark_node;
17366
17367 if (variable_template_p (t: expr))
17368 expr = lookup_and_finish_template_variable (templ: expr, targs: template_args,
17369 complain);
17370 else
17371 expr = lookup_template_function (fns: expr, arglist: template_args);
17372 }
17373
17374 if (expr == error_mark_node && complain & tf_error)
17375 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
17376 expr, input_location);
17377 else if (TYPE_P (scope))
17378 {
17379 expr = (adjust_result_of_qualified_name_lookup
17380 (expr, scope, current_nonlambda_class_type ()));
17381 expr = (finish_qualified_id_expr
17382 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
17383 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
17384 /*template_arg_p=*/false, complain));
17385 }
17386
17387 /* Expressions do not generally have reference type. */
17388 if (TREE_CODE (expr) != SCOPE_REF
17389 /* However, if we're about to form a pointer-to-member, we just
17390 want the referenced member referenced. */
17391 && TREE_CODE (expr) != OFFSET_REF)
17392 expr = convert_from_reference (expr);
17393
17394 if (REF_PARENTHESIZED_P (qualified_id))
17395 expr = force_paren_expr (expr);
17396
17397 expr = maybe_wrap_with_location (expr, loc);
17398
17399 return expr;
17400}
17401
17402/* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
17403 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17404 for tsubst. */
17405
17406static tree
17407tsubst_init (tree init, tree decl, tree args,
17408 tsubst_flags_t complain, tree in_decl)
17409{
17410 if (!init)
17411 return NULL_TREE;
17412
17413 init = tsubst_expr (init, args, complain, in_decl);
17414
17415 tree type = TREE_TYPE (decl);
17416
17417 if (!init && type != error_mark_node)
17418 {
17419 if (tree auto_node = type_uses_auto (type))
17420 {
17421 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17422 {
17423 if (complain & tf_error)
17424 error ("initializer for %q#D expands to an empty list "
17425 "of expressions", decl);
17426 return error_mark_node;
17427 }
17428 }
17429 else if (!dependent_type_p (type))
17430 {
17431 /* If we had an initializer but it
17432 instantiated to nothing,
17433 value-initialize the object. This will
17434 only occur when the initializer was a
17435 pack expansion where the parameter packs
17436 used in that expansion were of length
17437 zero. */
17438 init = build_value_init (type, complain);
17439 if (TREE_CODE (init) == AGGR_INIT_EXPR)
17440 init = get_target_expr (init, complain);
17441 if (TREE_CODE (init) == TARGET_EXPR)
17442 TARGET_EXPR_DIRECT_INIT_P (init) = true;
17443 }
17444 }
17445
17446 return init;
17447}
17448
17449/* If T is a reference to a dependent member of the current instantiation C and
17450 we are trying to refer to that member in a partial instantiation of C,
17451 return a SCOPE_REF; otherwise, return NULL_TREE.
17452
17453 This can happen when forming a C++17 deduction guide, as in PR96199. */
17454
17455static tree
17456maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
17457 tree in_decl)
17458{
17459 if (!(complain & tf_dguide))
17460 return NULL_TREE;
17461
17462 tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
17463 if (!decl || !DECL_P (decl))
17464 return NULL_TREE;
17465
17466 tree ctx = context_for_name_lookup (decl);
17467 if (!CLASS_TYPE_P (ctx))
17468 return NULL_TREE;
17469
17470 ctx = tsubst (t: ctx, args, complain, in_decl);
17471 if (!dependent_scope_p (ctx))
17472 return NULL_TREE;
17473
17474 if (TYPE_P (t))
17475 {
17476 if (typedef_variant_p (type: t))
17477 t = strip_typedefs (t);
17478 tree decl = TYPE_NAME (t);
17479 if (decl)
17480 decl = maybe_dependent_member_ref (t: decl, args, complain, in_decl);
17481 if (!decl)
17482 return NULL_TREE;
17483 return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
17484 complain);
17485 }
17486
17487 tree name = DECL_NAME (t);
17488 tree fullname = name;
17489 if (instantiates_primary_template_p (node: t))
17490 {
17491 tree tinfo = get_template_info (t);
17492 name = DECL_NAME (TI_TEMPLATE (tinfo));
17493 tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
17494 targs = tsubst_template_args (t: targs, args, complain, in_decl);
17495 fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
17496 }
17497
17498 if (TREE_CODE (t) == TYPE_DECL)
17499 {
17500 if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
17501 && TYPE_NAME (TREE_TYPE (t)) == t)
17502 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17503 scope, but it doesn't need to be rewritten again. */
17504 return NULL_TREE;
17505 tree type = build_typename_type (ctx, name, fullname, typename_type);
17506 return TYPE_NAME (type);
17507 }
17508 else if (DECL_TYPE_TEMPLATE_P (t))
17509 return make_unbound_class_template (ctx, name,
17510 NULL_TREE, complain);
17511 else
17512 return build_qualified_name (NULL_TREE, ctx, fullname,
17513 TREE_CODE (t) == TEMPLATE_DECL);
17514}
17515
17516/* Helper function for tsubst_omp_clauses, used for instantiation of
17517 OMP_CLAUSE_DECL of clauses. */
17518
17519static tree
17520tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17521 tree in_decl, tree *iterator_cache)
17522{
17523 if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
17524 return decl;
17525
17526 /* Handle OpenMP iterators. */
17527 if (TREE_CODE (decl) == TREE_LIST
17528 && TREE_PURPOSE (decl)
17529 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17530 {
17531 tree ret;
17532 if (iterator_cache[0] == TREE_PURPOSE (decl))
17533 ret = iterator_cache[1];
17534 else
17535 {
17536 tree *tp = &ret;
17537 begin_scope (sk_omp, NULL);
17538 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17539 {
17540 *tp = copy_node (it);
17541 TREE_VEC_ELT (*tp, 0)
17542 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17543 DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
17544 pushdecl (TREE_VEC_ELT (*tp, 0));
17545 TREE_VEC_ELT (*tp, 1)
17546 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
17547 TREE_VEC_ELT (*tp, 2)
17548 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
17549 TREE_VEC_ELT (*tp, 3)
17550 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
17551 TREE_CHAIN (*tp) = NULL_TREE;
17552 tp = &TREE_CHAIN (*tp);
17553 }
17554 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17555 iterator_cache[0] = TREE_PURPOSE (decl);
17556 iterator_cache[1] = ret;
17557 }
17558 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17559 args, complain,
17560 in_decl, NULL));
17561 }
17562
17563 /* Handle an OpenMP array section represented as a TREE_LIST (or
17564 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17565 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17566 TREE_LIST. We can handle it exactly the same as an array section
17567 (purpose, value, and a chain), even though the nomenclature
17568 (low_bound, length, etc) is different. */
17569 if (TREE_CODE (decl) == TREE_LIST)
17570 {
17571 tree low_bound
17572 = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
17573 tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
17574 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17575 in_decl, NULL);
17576 if (TREE_PURPOSE (decl) == low_bound
17577 && TREE_VALUE (decl) == length
17578 && TREE_CHAIN (decl) == chain)
17579 return decl;
17580 tree ret = tree_cons (low_bound, length, chain);
17581 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
17582 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
17583 return ret;
17584 }
17585 else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
17586 {
17587 tree low_bound
17588 = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
17589 tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
17590 in_decl);
17591 tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
17592 complain, in_decl, NULL);
17593 if (TREE_OPERAND (decl, 0) == base
17594 && TREE_OPERAND (decl, 1) == low_bound
17595 && TREE_OPERAND (decl, 2) == length)
17596 return decl;
17597 return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
17598 length);
17599 }
17600 tree ret = tsubst_stmt (decl, args, complain, in_decl);
17601 /* Undo convert_from_reference tsubst_expr could have called. */
17602 if (decl
17603 && REFERENCE_REF_P (ret)
17604 && !REFERENCE_REF_P (decl))
17605 ret = TREE_OPERAND (ret, 0);
17606 return ret;
17607}
17608
17609/* Like tsubst_copy, but specifically for OpenMP clauses. */
17610
17611static tree
17612tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17613 tree args, tsubst_flags_t complain, tree in_decl)
17614{
17615 tree new_clauses = NULL_TREE, nc, oc;
17616 tree linear_no_step = NULL_TREE;
17617 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17618
17619 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17620 {
17621 nc = copy_node (oc);
17622 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17623 new_clauses = nc;
17624
17625 switch (OMP_CLAUSE_CODE (nc))
17626 {
17627 case OMP_CLAUSE_LASTPRIVATE:
17628 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17629 {
17630 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17631 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
17632 complain, in_decl);
17633 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17634 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17635 }
17636 /* FALLTHRU */
17637 case OMP_CLAUSE_PRIVATE:
17638 case OMP_CLAUSE_SHARED:
17639 case OMP_CLAUSE_FIRSTPRIVATE:
17640 case OMP_CLAUSE_COPYIN:
17641 case OMP_CLAUSE_COPYPRIVATE:
17642 case OMP_CLAUSE_UNIFORM:
17643 case OMP_CLAUSE_DEPEND:
17644 case OMP_CLAUSE_DOACROSS:
17645 case OMP_CLAUSE_AFFINITY:
17646 case OMP_CLAUSE_FROM:
17647 case OMP_CLAUSE_TO:
17648 case OMP_CLAUSE_MAP:
17649 case OMP_CLAUSE__CACHE_:
17650 case OMP_CLAUSE_NONTEMPORAL:
17651 case OMP_CLAUSE_USE_DEVICE_PTR:
17652 case OMP_CLAUSE_USE_DEVICE_ADDR:
17653 case OMP_CLAUSE_IS_DEVICE_PTR:
17654 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17655 case OMP_CLAUSE_INCLUSIVE:
17656 case OMP_CLAUSE_EXCLUSIVE:
17657 OMP_CLAUSE_DECL (nc)
17658 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17659 in_decl, iterator_cache);
17660 break;
17661 case OMP_CLAUSE_NUM_TEAMS:
17662 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
17663 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
17664 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
17665 complain, in_decl);
17666 /* FALLTHRU */
17667 case OMP_CLAUSE_TILE:
17668 case OMP_CLAUSE_IF:
17669 case OMP_CLAUSE_SELF:
17670 case OMP_CLAUSE_NUM_THREADS:
17671 case OMP_CLAUSE_SCHEDULE:
17672 case OMP_CLAUSE_COLLAPSE:
17673 case OMP_CLAUSE_FINAL:
17674 case OMP_CLAUSE_DEVICE:
17675 case OMP_CLAUSE_DIST_SCHEDULE:
17676 case OMP_CLAUSE_THREAD_LIMIT:
17677 case OMP_CLAUSE_SAFELEN:
17678 case OMP_CLAUSE_SIMDLEN:
17679 case OMP_CLAUSE_NUM_TASKS:
17680 case OMP_CLAUSE_GRAINSIZE:
17681 case OMP_CLAUSE_PRIORITY:
17682 case OMP_CLAUSE_ORDERED:
17683 case OMP_CLAUSE_HINT:
17684 case OMP_CLAUSE_FILTER:
17685 case OMP_CLAUSE_NUM_GANGS:
17686 case OMP_CLAUSE_NUM_WORKERS:
17687 case OMP_CLAUSE_VECTOR_LENGTH:
17688 case OMP_CLAUSE_WORKER:
17689 case OMP_CLAUSE_VECTOR:
17690 case OMP_CLAUSE_ASYNC:
17691 case OMP_CLAUSE_WAIT:
17692 case OMP_CLAUSE_DETACH:
17693 OMP_CLAUSE_OPERAND (nc, 0)
17694 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
17695 break;
17696 case OMP_CLAUSE_REDUCTION:
17697 case OMP_CLAUSE_IN_REDUCTION:
17698 case OMP_CLAUSE_TASK_REDUCTION:
17699 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17700 {
17701 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17702 if (TREE_CODE (placeholder) == SCOPE_REF)
17703 {
17704 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17705 complain, in_decl);
17706 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17707 = build_qualified_name (NULL_TREE, scope,
17708 TREE_OPERAND (placeholder, 1),
17709 false);
17710 }
17711 else
17712 gcc_assert (identifier_p (placeholder));
17713 }
17714 OMP_CLAUSE_DECL (nc)
17715 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17716 in_decl, NULL);
17717 break;
17718 case OMP_CLAUSE_GANG:
17719 case OMP_CLAUSE_ALIGNED:
17720 OMP_CLAUSE_DECL (nc)
17721 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17722 in_decl, NULL);
17723 OMP_CLAUSE_OPERAND (nc, 1)
17724 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
17725 break;
17726 case OMP_CLAUSE_ALLOCATE:
17727 OMP_CLAUSE_DECL (nc)
17728 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17729 in_decl, NULL);
17730 OMP_CLAUSE_OPERAND (nc, 1)
17731 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
17732 OMP_CLAUSE_OPERAND (nc, 2)
17733 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
17734 break;
17735 case OMP_CLAUSE_LINEAR:
17736 OMP_CLAUSE_DECL (nc)
17737 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17738 in_decl, NULL);
17739 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17740 {
17741 gcc_assert (!linear_no_step);
17742 linear_no_step = nc;
17743 }
17744 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17745 OMP_CLAUSE_LINEAR_STEP (nc)
17746 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17747 complain, in_decl, NULL);
17748 else
17749 OMP_CLAUSE_LINEAR_STEP (nc)
17750 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
17751 complain, in_decl);
17752 break;
17753 case OMP_CLAUSE_NOWAIT:
17754 case OMP_CLAUSE_DEFAULT:
17755 case OMP_CLAUSE_UNTIED:
17756 case OMP_CLAUSE_MERGEABLE:
17757 case OMP_CLAUSE_INBRANCH:
17758 case OMP_CLAUSE_NOTINBRANCH:
17759 case OMP_CLAUSE_PROC_BIND:
17760 case OMP_CLAUSE_FOR:
17761 case OMP_CLAUSE_PARALLEL:
17762 case OMP_CLAUSE_SECTIONS:
17763 case OMP_CLAUSE_TASKGROUP:
17764 case OMP_CLAUSE_NOGROUP:
17765 case OMP_CLAUSE_THREADS:
17766 case OMP_CLAUSE_SIMD:
17767 case OMP_CLAUSE_DEFAULTMAP:
17768 case OMP_CLAUSE_ORDER:
17769 case OMP_CLAUSE_BIND:
17770 case OMP_CLAUSE_INDEPENDENT:
17771 case OMP_CLAUSE_AUTO:
17772 case OMP_CLAUSE_SEQ:
17773 case OMP_CLAUSE_IF_PRESENT:
17774 case OMP_CLAUSE_FINALIZE:
17775 case OMP_CLAUSE_NOHOST:
17776 break;
17777 default:
17778 gcc_unreachable ();
17779 }
17780 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17781 switch (OMP_CLAUSE_CODE (nc))
17782 {
17783 case OMP_CLAUSE_SHARED:
17784 case OMP_CLAUSE_PRIVATE:
17785 case OMP_CLAUSE_FIRSTPRIVATE:
17786 case OMP_CLAUSE_LASTPRIVATE:
17787 case OMP_CLAUSE_COPYPRIVATE:
17788 case OMP_CLAUSE_LINEAR:
17789 case OMP_CLAUSE_REDUCTION:
17790 case OMP_CLAUSE_IN_REDUCTION:
17791 case OMP_CLAUSE_TASK_REDUCTION:
17792 case OMP_CLAUSE_USE_DEVICE_PTR:
17793 case OMP_CLAUSE_USE_DEVICE_ADDR:
17794 case OMP_CLAUSE_IS_DEVICE_PTR:
17795 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17796 case OMP_CLAUSE_INCLUSIVE:
17797 case OMP_CLAUSE_EXCLUSIVE:
17798 case OMP_CLAUSE_ALLOCATE:
17799 /* tsubst_expr on SCOPE_REF results in returning
17800 finish_non_static_data_member result. Undo that here. */
17801 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17802 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17803 == IDENTIFIER_NODE))
17804 {
17805 tree t = OMP_CLAUSE_DECL (nc);
17806 tree v = t;
17807 while (v)
17808 switch (TREE_CODE (v))
17809 {
17810 case COMPONENT_REF:
17811 case MEM_REF:
17812 case INDIRECT_REF:
17813 CASE_CONVERT:
17814 case POINTER_PLUS_EXPR:
17815 v = TREE_OPERAND (v, 0);
17816 continue;
17817 case PARM_DECL:
17818 if (DECL_CONTEXT (v) == current_function_decl
17819 && DECL_ARTIFICIAL (v)
17820 && DECL_NAME (v) == this_identifier)
17821 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17822 /* FALLTHRU */
17823 default:
17824 v = NULL_TREE;
17825 break;
17826 }
17827 }
17828 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17829 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17830 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17831 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17832 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17833 {
17834 tree decl = OMP_CLAUSE_DECL (nc);
17835 if (VAR_P (decl))
17836 {
17837 retrofit_lang_decl (decl);
17838 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17839 }
17840 }
17841 break;
17842 default:
17843 break;
17844 }
17845 }
17846
17847 new_clauses = nreverse (new_clauses);
17848 if (ort != C_ORT_OMP_DECLARE_SIMD)
17849 {
17850 new_clauses = finish_omp_clauses (new_clauses, ort);
17851 if (linear_no_step)
17852 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17853 if (nc == linear_no_step)
17854 {
17855 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17856 break;
17857 }
17858 }
17859 return new_clauses;
17860}
17861
17862/* Like tsubst_expr, but unshare TREE_LIST nodes. */
17863
17864static tree
17865tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17866 tree in_decl)
17867{
17868#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17869
17870 tree purpose, value, chain;
17871
17872 if (t == NULL)
17873 return t;
17874
17875 if (TREE_CODE (t) != TREE_LIST)
17876 return tsubst_expr (t, args, complain, in_decl);
17877
17878 if (t == void_list_node)
17879 return t;
17880
17881 purpose = TREE_PURPOSE (t);
17882 if (purpose)
17883 purpose = RECUR (purpose);
17884 value = TREE_VALUE (t);
17885 if (value)
17886 {
17887 if (TREE_CODE (value) != LABEL_DECL)
17888 value = RECUR (value);
17889 else
17890 {
17891 value = lookup_label (DECL_NAME (value));
17892 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17893 TREE_USED (value) = 1;
17894 }
17895 }
17896 chain = TREE_CHAIN (t);
17897 if (chain && chain != void_type_node)
17898 chain = RECUR (chain);
17899 return tree_cons (purpose, value, chain);
17900#undef RECUR
17901}
17902
17903/* Used to temporarily communicate the list of #pragma omp parallel
17904 clauses to #pragma omp for instantiation if they are combined
17905 together. */
17906
17907static tree *omp_parallel_combined_clauses;
17908
17909static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17910 cp_decomp *);
17911
17912/* Substitute one OMP_FOR iterator. */
17913
17914static bool
17915tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17916 tree initv, tree condv, tree incrv, tree *clauses,
17917 tree args, tsubst_flags_t complain, tree in_decl)
17918{
17919#define RECUR(NODE) \
17920 tsubst_stmt ((NODE), args, complain, in_decl)
17921 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17922 bool ret = false;
17923
17924 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17925 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17926
17927 decl = TREE_OPERAND (init, 0);
17928 init = TREE_OPERAND (init, 1);
17929 tree decl_expr = NULL_TREE;
17930 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17931 if (range_for)
17932 {
17933 bool decomp = false;
17934 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17935 {
17936 tree v = DECL_VALUE_EXPR (decl);
17937 if (TREE_CODE (v) == ARRAY_REF
17938 && VAR_P (TREE_OPERAND (v, 0))
17939 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17940 {
17941 cp_decomp decomp_d = { NULL_TREE, .count: 0 };
17942 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17943 maybe_push_decl (d);
17944 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17945 in_decl, &decomp_d);
17946 decomp = true;
17947 if (d == error_mark_node)
17948 decl = error_mark_node;
17949 else
17950 for (unsigned int i = 0; i < decomp_d.count; i++)
17951 {
17952 if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
17953 {
17954 tree v = build_nt (ARRAY_REF, d,
17955 size_int (decomp_d.count - i - 1),
17956 NULL_TREE, NULL_TREE);
17957 SET_DECL_VALUE_EXPR (decomp_d.decl, v);
17958 DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
17959 }
17960 fit_decomposition_lang_decl (decomp_d.decl, d);
17961 decomp_d.decl = DECL_CHAIN (decomp_d.decl);
17962 }
17963 }
17964 }
17965 decl = tsubst_decl (t: decl, args, complain);
17966 if (!decomp)
17967 maybe_push_decl (decl);
17968 }
17969 else if (init && TREE_CODE (init) == DECL_EXPR)
17970 {
17971 /* We need to jump through some hoops to handle declarations in the
17972 init-statement, since we might need to handle auto deduction,
17973 but we need to keep control of initialization. */
17974 decl_expr = init;
17975 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17976 decl = tsubst_decl (t: decl, args, complain);
17977 }
17978 else
17979 {
17980 if (TREE_CODE (decl) == SCOPE_REF)
17981 {
17982 decl = RECUR (decl);
17983 if (TREE_CODE (decl) == COMPONENT_REF)
17984 {
17985 tree v = decl;
17986 while (v)
17987 switch (TREE_CODE (v))
17988 {
17989 case COMPONENT_REF:
17990 case MEM_REF:
17991 case INDIRECT_REF:
17992 CASE_CONVERT:
17993 case POINTER_PLUS_EXPR:
17994 v = TREE_OPERAND (v, 0);
17995 continue;
17996 case PARM_DECL:
17997 if (DECL_CONTEXT (v) == current_function_decl
17998 && DECL_ARTIFICIAL (v)
17999 && DECL_NAME (v) == this_identifier)
18000 {
18001 decl = TREE_OPERAND (decl, 1);
18002 decl = omp_privatize_field (decl, false);
18003 }
18004 /* FALLTHRU */
18005 default:
18006 v = NULL_TREE;
18007 break;
18008 }
18009 }
18010 }
18011 else
18012 decl = RECUR (decl);
18013 }
18014 if (init && TREE_CODE (init) == TREE_VEC)
18015 {
18016 init = copy_node (init);
18017 TREE_VEC_ELT (init, 0)
18018 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
18019 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
18020 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
18021 }
18022 else
18023 init = RECUR (init);
18024
18025 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
18026 {
18027 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
18028 if (TREE_CODE (o) == TREE_LIST)
18029 TREE_VEC_ELT (orig_declv, i)
18030 = tree_cons (RECUR (TREE_PURPOSE (o)),
18031 RECUR (TREE_VALUE (o)),
18032 NULL_TREE);
18033 else
18034 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
18035 }
18036
18037 if (range_for)
18038 {
18039 tree this_pre_body = NULL_TREE;
18040 tree orig_init = NULL_TREE;
18041 tree orig_decl = NULL_TREE;
18042 tree init_sl = NULL_TREE;
18043 cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
18044 orig_init, cond, incr);
18045 if (orig_decl)
18046 {
18047 if (orig_declv == NULL_TREE)
18048 orig_declv = copy_node (declv);
18049 TREE_VEC_ELT (orig_declv, i) = orig_decl;
18050 ret = true;
18051 }
18052 else if (orig_declv)
18053 TREE_VEC_ELT (orig_declv, i) = decl;
18054 }
18055
18056 tree auto_node = type_uses_auto (TREE_TYPE (decl));
18057 if (!range_for && auto_node && init)
18058 TREE_TYPE (decl)
18059 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
18060
18061 gcc_assert (!type_dependent_expression_p (decl));
18062
18063 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
18064 {
18065 if (decl_expr)
18066 {
18067 /* Declare the variable, but don't let that initialize it. */
18068 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
18069 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
18070 RECUR (decl_expr);
18071 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
18072 }
18073
18074 if (!range_for)
18075 {
18076 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18077 if (COMPARISON_CLASS_P (cond)
18078 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
18079 {
18080 tree lhs = RECUR (TREE_OPERAND (cond, 0));
18081 tree rhs = copy_node (TREE_OPERAND (cond, 1));
18082 TREE_VEC_ELT (rhs, 0)
18083 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
18084 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
18085 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
18086 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
18087 lhs, rhs);
18088 }
18089 else
18090 cond = RECUR (cond);
18091 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18092 if (TREE_CODE (incr) == MODIFY_EXPR)
18093 {
18094 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18095 tree rhs = RECUR (TREE_OPERAND (incr, 1));
18096 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
18097 NOP_EXPR, rhs, NULL_TREE, complain);
18098 }
18099 else
18100 incr = RECUR (incr);
18101 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18102 TREE_VEC_ELT (orig_declv, i) = decl;
18103 }
18104 TREE_VEC_ELT (declv, i) = decl;
18105 TREE_VEC_ELT (initv, i) = init;
18106 TREE_VEC_ELT (condv, i) = cond;
18107 TREE_VEC_ELT (incrv, i) = incr;
18108 return ret;
18109 }
18110
18111 if (decl_expr)
18112 {
18113 /* Declare and initialize the variable. */
18114 RECUR (decl_expr);
18115 init = NULL_TREE;
18116 }
18117 else if (init)
18118 {
18119 tree *pc;
18120 int j;
18121 for (j = ((omp_parallel_combined_clauses == NULL
18122 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
18123 {
18124 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
18125 {
18126 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
18127 && OMP_CLAUSE_DECL (*pc) == decl)
18128 break;
18129 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
18130 && OMP_CLAUSE_DECL (*pc) == decl)
18131 {
18132 if (j)
18133 break;
18134 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18135 tree c = *pc;
18136 *pc = OMP_CLAUSE_CHAIN (c);
18137 OMP_CLAUSE_CHAIN (c) = *clauses;
18138 *clauses = c;
18139 }
18140 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
18141 && OMP_CLAUSE_DECL (*pc) == decl)
18142 {
18143 error ("iteration variable %qD should not be firstprivate",
18144 decl);
18145 *pc = OMP_CLAUSE_CHAIN (*pc);
18146 }
18147 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18148 && OMP_CLAUSE_DECL (*pc) == decl)
18149 {
18150 error ("iteration variable %qD should not be reduction",
18151 decl);
18152 *pc = OMP_CLAUSE_CHAIN (*pc);
18153 }
18154 else
18155 pc = &OMP_CLAUSE_CHAIN (*pc);
18156 }
18157 if (*pc)
18158 break;
18159 }
18160 if (*pc == NULL_TREE)
18161 {
18162 tree c = build_omp_clause (input_location,
18163 TREE_CODE (t) == OMP_LOOP
18164 ? OMP_CLAUSE_LASTPRIVATE
18165 : OMP_CLAUSE_PRIVATE);
18166 OMP_CLAUSE_DECL (c) = decl;
18167 c = finish_omp_clauses (c, C_ORT_OMP);
18168 if (c)
18169 {
18170 OMP_CLAUSE_CHAIN (c) = *clauses;
18171 *clauses = c;
18172 }
18173 }
18174 }
18175 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18176 if (COMPARISON_CLASS_P (cond))
18177 {
18178 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18179 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18180 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18181 }
18182 else
18183 cond = RECUR (cond);
18184 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18185 switch (TREE_CODE (incr))
18186 {
18187 case PREINCREMENT_EXPR:
18188 case PREDECREMENT_EXPR:
18189 case POSTINCREMENT_EXPR:
18190 case POSTDECREMENT_EXPR:
18191 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18192 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18193 break;
18194 case MODIFY_EXPR:
18195 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18196 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18197 {
18198 tree rhs = TREE_OPERAND (incr, 1);
18199 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18200 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18201 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18202 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18203 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18204 rhs0, rhs1));
18205 }
18206 else
18207 incr = RECUR (incr);
18208 break;
18209 case MODOP_EXPR:
18210 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18211 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18212 {
18213 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18214 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18215 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18216 TREE_TYPE (decl), lhs,
18217 RECUR (TREE_OPERAND (incr, 2))));
18218 }
18219 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18220 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18221 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18222 {
18223 tree rhs = TREE_OPERAND (incr, 2);
18224 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18225 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18226 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18227 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18228 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18229 rhs0, rhs1));
18230 }
18231 else
18232 incr = RECUR (incr);
18233 break;
18234 default:
18235 incr = RECUR (incr);
18236 break;
18237 }
18238
18239 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18240 TREE_VEC_ELT (orig_declv, i) = decl;
18241 TREE_VEC_ELT (declv, i) = decl;
18242 TREE_VEC_ELT (initv, i) = init;
18243 TREE_VEC_ELT (condv, i) = cond;
18244 TREE_VEC_ELT (incrv, i) = incr;
18245 return false;
18246#undef RECUR
18247}
18248
18249/* Helper function of tsubst_expr, find OMP_TEAMS inside
18250 of OMP_TARGET's body. */
18251
18252static tree
18253tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18254{
18255 *walk_subtrees = 0;
18256 switch (TREE_CODE (*tp))
18257 {
18258 case OMP_TEAMS:
18259 return *tp;
18260 case BIND_EXPR:
18261 case STATEMENT_LIST:
18262 *walk_subtrees = 1;
18263 break;
18264 default:
18265 break;
18266 }
18267 return NULL_TREE;
18268}
18269
18270/* Helper function for tsubst_expr. For decomposition declaration
18271 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18272 also the corresponding decls representing the identifiers
18273 of the decomposition declaration. Return DECL if successful
18274 or error_mark_node otherwise, set *FIRST to the first decl
18275 in the list chained through DECL_CHAIN and *CNT to the number
18276 of such decls. */
18277
18278static tree
18279tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18280 tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
18281{
18282 tree decl2, decl3, prev = decl;
18283 decomp->count = 0;
18284 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18285 for (decl2 = DECL_CHAIN (pattern_decl);
18286 decl2
18287 && VAR_P (decl2)
18288 && DECL_DECOMPOSITION_P (decl2)
18289 && DECL_NAME (decl2);
18290 decl2 = DECL_CHAIN (decl2))
18291 {
18292 if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
18293 {
18294 gcc_assert (errorcount);
18295 return error_mark_node;
18296 }
18297 decomp->count++;
18298 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18299 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18300 tree v = DECL_VALUE_EXPR (decl2);
18301 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18302 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18303 decl3 = tsubst (t: decl2, args, complain, in_decl);
18304 SET_DECL_VALUE_EXPR (decl2, v);
18305 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18306 if (VAR_P (decl3))
18307 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18308 else
18309 {
18310 gcc_assert (errorcount);
18311 decl = error_mark_node;
18312 continue;
18313 }
18314 maybe_push_decl (decl3);
18315 if (error_operand_p (t: decl3))
18316 decl = error_mark_node;
18317 else if (decl != error_mark_node
18318 && DECL_CHAIN (decl3) != prev
18319 && decl != prev)
18320 {
18321 gcc_assert (errorcount);
18322 decl = error_mark_node;
18323 }
18324 else
18325 prev = decl3;
18326 }
18327 decomp->decl = prev;
18328 return decl;
18329}
18330
18331/* Return the proper local_specialization for init-capture pack DECL. */
18332
18333static tree
18334lookup_init_capture_pack (tree decl)
18335{
18336 /* We handle normal pack captures by forwarding to the specialization of the
18337 captured parameter. We can't do that for pack init-captures; we need them
18338 to have their own local_specialization. We created the individual
18339 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18340 when we process the DECL_EXPR for the pack init-capture in the template.
18341 So, how do we find them? We don't know the capture proxy pack when
18342 building the individual resulting proxies, and we don't know the
18343 individual proxies when instantiating the pack. What we have in common is
18344 the FIELD_DECL.
18345
18346 So...when we instantiate the FIELD_DECL, we stick the result in
18347 local_specializations. Then at the DECL_EXPR we look up that result, see
18348 how many elements it has, synthesize the names, and look them up. */
18349
18350 tree cname = DECL_NAME (decl);
18351 tree val = DECL_VALUE_EXPR (decl);
18352 tree field = TREE_OPERAND (val, 1);
18353 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18354 tree fpack = retrieve_local_specialization (tmpl: field);
18355 if (fpack == error_mark_node)
18356 return error_mark_node;
18357
18358 int len = 1;
18359 tree vec = NULL_TREE;
18360 tree r = NULL_TREE;
18361 if (TREE_CODE (fpack) == TREE_VEC)
18362 {
18363 len = TREE_VEC_LENGTH (fpack);
18364 vec = make_tree_vec (len);
18365 r = make_node (NONTYPE_ARGUMENT_PACK);
18366 ARGUMENT_PACK_ARGS (r) = vec;
18367 }
18368 for (int i = 0; i < len; ++i)
18369 {
18370 tree ename = vec ? make_ith_pack_parameter_name (name: cname, i) : cname;
18371 tree elt = lookup_name (name: ename);
18372 if (vec)
18373 TREE_VEC_ELT (vec, i) = elt;
18374 else
18375 r = elt;
18376 }
18377 return r;
18378}
18379
18380/* T is an operand of a template tree being substituted. Return whether
18381 T is dependent such that we should suppress some warnings that would
18382 make sense if the substituted expression were written directly, like
18383 template <int I> bool f() { return I == 2; }
18384 We don't want to warn when instantiating f that comparing two constants
18385 always has the same value.
18386
18387 This is a more limited concept of dependence than instantiation-dependent;
18388 here we don't care whether substitution could fail. */
18389
18390static bool
18391dependent_operand_p (tree t)
18392{
18393 while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
18394 t = TREE_OPERAND (t, 0);
18395 ++processing_template_decl;
18396 bool r = (potential_constant_expression (t)
18397 ? value_dependent_expression_p (t)
18398 : type_dependent_expression_p (t));
18399 --processing_template_decl;
18400 return r;
18401}
18402
18403/* A superset of tsubst_expr that also handles statement trees. */
18404
18405static tree
18406tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18407{
18408#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18409#define RECUR(NODE) \
18410 tsubst_stmt ((NODE), args, complain, in_decl)
18411
18412 tree stmt, tmp;
18413 tree r;
18414 location_t loc;
18415
18416 if (t == NULL_TREE || t == error_mark_node)
18417 return t;
18418
18419 loc = input_location;
18420 if (location_t eloc = cp_expr_location (t_: t))
18421 input_location = eloc;
18422 if (STATEMENT_CODE_P (TREE_CODE (t)))
18423 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18424
18425 switch (TREE_CODE (t))
18426 {
18427 case STATEMENT_LIST:
18428 {
18429 for (tree stmt : tsi_range (t))
18430 RECUR (stmt);
18431 break;
18432 }
18433
18434 case CTOR_INITIALIZER:
18435 finish_mem_initializers (tsubst_initializer_list
18436 (TREE_OPERAND (t, 0), args));
18437 break;
18438
18439 case RETURN_EXPR:
18440 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18441 break;
18442
18443 case CO_RETURN_EXPR:
18444 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18445 break;
18446
18447 case EXPR_STMT:
18448 tmp = RECUR (EXPR_STMT_EXPR (t));
18449 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18450 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18451 else
18452 finish_expr_stmt (tmp);
18453 break;
18454
18455 case USING_STMT:
18456 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18457 break;
18458
18459 case PRECONDITION_STMT:
18460 case POSTCONDITION_STMT:
18461 gcc_unreachable ();
18462
18463 case ASSERTION_STMT:
18464 {
18465 r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
18466 if (r != error_mark_node)
18467 add_stmt (r);
18468 RETURN (r);
18469 }
18470 break;
18471
18472 case DECL_EXPR:
18473 {
18474 tree decl, pattern_decl;
18475 tree init;
18476
18477 pattern_decl = decl = DECL_EXPR_DECL (t);
18478 if (TREE_CODE (decl) == LABEL_DECL)
18479 finish_label_decl (DECL_NAME (decl));
18480 else if (TREE_CODE (decl) == USING_DECL)
18481 {
18482 tree scope = USING_DECL_SCOPE (decl);
18483 if (DECL_DEPENDENT_P (decl))
18484 {
18485 scope = tsubst (t: scope, args, complain, in_decl);
18486 if (!MAYBE_CLASS_TYPE_P (scope)
18487 && TREE_CODE (scope) != ENUMERAL_TYPE)
18488 {
18489 if (complain & tf_error)
18490 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18491 "class, namespace, or enumeration", scope);
18492 return error_mark_node;
18493 }
18494 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18495 }
18496 else
18497 {
18498 /* This is a non-dependent using-decl, and we'll have
18499 used the names it found during template parsing. We do
18500 not want to do the lookup again, because we might not
18501 find the things we found then. */
18502 gcc_checking_assert (scope == tsubst (scope, args,
18503 complain, in_decl));
18504 /* We still need to push the bindings so that we can look up
18505 this name later. */
18506 push_using_decl_bindings (DECL_NAME (decl),
18507 USING_DECL_DECLS (decl));
18508 }
18509 }
18510 else if (is_capture_proxy (decl)
18511 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18512 {
18513 /* We're in tsubst_lambda_expr, we've already inserted a new
18514 capture proxy, so look it up and register it. */
18515 tree inst;
18516 if (!DECL_PACK_P (decl))
18517 {
18518 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18519 LOOK_want::HIDDEN_LAMBDA);
18520 gcc_assert (inst != decl && is_capture_proxy (inst));
18521 }
18522 else if (is_normal_capture_proxy (decl))
18523 {
18524 inst = (retrieve_local_specialization
18525 (DECL_CAPTURED_VARIABLE (decl)));
18526 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18527 || DECL_PACK_P (inst));
18528 }
18529 else
18530 inst = lookup_init_capture_pack (decl);
18531
18532 register_local_specialization (spec: inst, tmpl: decl);
18533 break;
18534 }
18535 else if (DECL_PRETTY_FUNCTION_P (decl))
18536 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18537 DECL_NAME (decl),
18538 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18539 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18540 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18541 /* Don't copy the old closure; we'll create a new one in
18542 tsubst_lambda_expr. */
18543 break;
18544 else
18545 {
18546 init = DECL_INITIAL (decl);
18547 decl = tsubst (t: decl, args, complain, in_decl);
18548 if (decl != error_mark_node)
18549 {
18550 /* By marking the declaration as instantiated, we avoid
18551 trying to instantiate it. Since instantiate_decl can't
18552 handle local variables, and since we've already done
18553 all that needs to be done, that's the right thing to
18554 do. */
18555 if (VAR_P (decl))
18556 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18557 if (VAR_P (decl) && !DECL_NAME (decl)
18558 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18559 /* Anonymous aggregates are a special case. */
18560 finish_anon_union (decl);
18561 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18562 {
18563 DECL_CONTEXT (decl) = current_function_decl;
18564 if (DECL_NAME (decl) == this_identifier)
18565 {
18566 tree lam = DECL_CONTEXT (current_function_decl);
18567 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18568 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18569 }
18570 insert_capture_proxy (decl);
18571 }
18572 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18573 /* We already did a pushtag. */;
18574 else if (VAR_OR_FUNCTION_DECL_P (decl)
18575 && DECL_LOCAL_DECL_P (decl))
18576 {
18577 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18578 DECL_CONTEXT (decl) = NULL_TREE;
18579 decl = pushdecl (decl);
18580 if (TREE_CODE (decl) == FUNCTION_DECL
18581 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18582 && cp_check_omp_declare_reduction (decl))
18583 instantiate_body (pattern: pattern_decl, args, d: decl, nested: true);
18584 }
18585 else
18586 {
18587 bool const_init = false;
18588 cp_decomp decomp_d, *decomp = NULL;
18589 tree ndecl = error_mark_node;
18590 tree asmspec_tree = NULL_TREE;
18591 maybe_push_decl (decl);
18592
18593 if (VAR_P (decl)
18594 && DECL_LANG_SPECIFIC (decl)
18595 && DECL_OMP_PRIVATIZED_MEMBER (decl))
18596 break;
18597
18598 if (VAR_P (decl)
18599 && DECL_DECOMPOSITION_P (decl)
18600 && TREE_TYPE (pattern_decl) != error_mark_node)
18601 {
18602 decomp = &decomp_d;
18603 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18604 complain, in_decl, decomp);
18605 }
18606
18607 init = tsubst_init (init, decl, args, complain, in_decl);
18608
18609 if (VAR_P (decl))
18610 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18611 (pattern_decl));
18612
18613 /* In a non-template function, VLA type declarations are
18614 handled in grokdeclarator; for templates, handle them
18615 now. */
18616 predeclare_vla (decl);
18617
18618 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18619 {
18620 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18621 const char *asmspec = IDENTIFIER_POINTER (id);
18622 gcc_assert (asmspec[0] == '*');
18623 asmspec_tree
18624 = build_string (IDENTIFIER_LENGTH (id) - 1,
18625 asmspec + 1);
18626 TREE_TYPE (asmspec_tree) = char_array_type_node;
18627 }
18628
18629 cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
18630 decomp);
18631
18632 if (ndecl != error_mark_node)
18633 cp_finish_decomp (ndecl, decomp);
18634 }
18635 }
18636 }
18637
18638 break;
18639 }
18640
18641 case FOR_STMT:
18642 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18643 RECUR (FOR_INIT_STMT (t));
18644 finish_init_stmt (stmt);
18645 tmp = RECUR (FOR_COND (t));
18646 finish_for_cond (tmp, stmt, false, 0, false);
18647 tmp = RECUR (FOR_EXPR (t));
18648 finish_for_expr (tmp, stmt);
18649 {
18650 bool prev = note_iteration_stmt_body_start ();
18651 RECUR (FOR_BODY (t));
18652 note_iteration_stmt_body_end (prev);
18653 }
18654 finish_for_stmt (stmt);
18655 break;
18656
18657 case RANGE_FOR_STMT:
18658 {
18659 /* Construct another range_for, if this is not a final
18660 substitution (for inside a generic lambda of a
18661 template). Otherwise convert to a regular for. */
18662 tree decl, expr;
18663 stmt = (processing_template_decl
18664 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18665 : begin_for_stmt (NULL_TREE, NULL_TREE));
18666 RECUR (RANGE_FOR_INIT_STMT (t));
18667 decl = RANGE_FOR_DECL (t);
18668 decl = tsubst (t: decl, args, complain, in_decl);
18669 maybe_push_decl (decl);
18670 expr = RECUR (RANGE_FOR_EXPR (t));
18671
18672 cp_decomp decomp_d, *decomp = NULL;
18673 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18674 {
18675 decomp = &decomp_d;
18676 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18677 complain, in_decl, decomp);
18678 }
18679
18680 tree unroll = RECUR (RANGE_FOR_UNROLL (t));
18681 if (unroll)
18682 unroll
18683 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
18684 unroll);
18685 if (processing_template_decl)
18686 {
18687 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18688 RANGE_FOR_UNROLL (stmt) = unroll;
18689 RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
18690 finish_range_for_decl (stmt, decl, expr);
18691 if (decomp && decl != error_mark_node)
18692 cp_finish_decomp (decl, decomp);
18693 }
18694 else
18695 stmt = cp_convert_range_for (stmt, decl, expr, decomp,
18696 RANGE_FOR_IVDEP (t), unroll,
18697 RANGE_FOR_NOVECTOR (t));
18698
18699 bool prev = note_iteration_stmt_body_start ();
18700 RECUR (RANGE_FOR_BODY (t));
18701 note_iteration_stmt_body_end (prev);
18702 finish_for_stmt (stmt);
18703 }
18704 break;
18705
18706 case WHILE_STMT:
18707 stmt = begin_while_stmt ();
18708 tmp = RECUR (WHILE_COND (t));
18709 finish_while_stmt_cond (tmp, stmt, false, 0, false);
18710 {
18711 bool prev = note_iteration_stmt_body_start ();
18712 RECUR (WHILE_BODY (t));
18713 note_iteration_stmt_body_end (prev);
18714 }
18715 finish_while_stmt (stmt);
18716 break;
18717
18718 case DO_STMT:
18719 stmt = begin_do_stmt ();
18720 {
18721 bool prev = note_iteration_stmt_body_start ();
18722 RECUR (DO_BODY (t));
18723 note_iteration_stmt_body_end (prev);
18724 }
18725 finish_do_body (stmt);
18726 tmp = RECUR (DO_COND (t));
18727 finish_do_stmt (tmp, stmt, false, 0, false);
18728 break;
18729
18730 case IF_STMT:
18731 stmt = begin_if_stmt ();
18732 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18733 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
18734 if (IF_STMT_CONSTEXPR_P (t))
18735 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
18736 {
18737 tree cond = IF_COND (t);
18738 bool was_dep = dependent_operand_p (t: cond);
18739 cond = RECUR (cond);
18740 warning_sentinel s1(warn_address, was_dep);
18741 tmp = finish_if_stmt_cond (cond, stmt);
18742 }
18743 if (IF_STMT_CONSTEXPR_P (t)
18744 && instantiation_dependent_expression_p (tmp))
18745 {
18746 /* We're partially instantiating a generic lambda, but the condition
18747 of the constexpr if is still dependent. Don't substitute into the
18748 branches now, just remember the template arguments. */
18749 do_poplevel (IF_SCOPE (stmt));
18750 IF_SCOPE (stmt) = NULL_TREE;
18751 IF_COND (stmt) = IF_COND (t);
18752 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18753 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18754 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (pattern: stmt, args, complain);
18755 add_stmt (stmt);
18756 break;
18757 }
18758 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18759 /* Don't instantiate the THEN_CLAUSE. */;
18760 else if (IF_STMT_CONSTEVAL_P (t))
18761 {
18762 bool save_in_consteval_if_p = in_consteval_if_p;
18763 in_consteval_if_p = true;
18764 RECUR (THEN_CLAUSE (t));
18765 in_consteval_if_p = save_in_consteval_if_p;
18766 }
18767 else
18768 {
18769 tree folded = fold_non_dependent_expr (tmp, complain);
18770 bool inhibit = integer_zerop (folded);
18771 if (inhibit)
18772 ++c_inhibit_evaluation_warnings;
18773 RECUR (THEN_CLAUSE (t));
18774 if (inhibit)
18775 --c_inhibit_evaluation_warnings;
18776 }
18777 finish_then_clause (stmt);
18778
18779 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18780 /* Don't instantiate the ELSE_CLAUSE. */;
18781 else if (ELSE_CLAUSE (t))
18782 {
18783 tree folded = fold_non_dependent_expr (tmp, complain);
18784 bool inhibit = integer_nonzerop (folded);
18785 begin_else_clause (stmt);
18786 if (inhibit)
18787 ++c_inhibit_evaluation_warnings;
18788 RECUR (ELSE_CLAUSE (t));
18789 if (inhibit)
18790 --c_inhibit_evaluation_warnings;
18791 finish_else_clause (stmt);
18792 }
18793
18794 finish_if_stmt (stmt);
18795 break;
18796
18797 case BIND_EXPR:
18798 if (BIND_EXPR_BODY_BLOCK (t))
18799 stmt = begin_function_body ();
18800 else
18801 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18802 ? BCS_TRY_BLOCK : 0);
18803
18804 RECUR (BIND_EXPR_BODY (t));
18805
18806 if (BIND_EXPR_BODY_BLOCK (t))
18807 finish_function_body (stmt);
18808 else
18809 finish_compound_stmt (stmt);
18810 break;
18811
18812 case BREAK_STMT:
18813 finish_break_stmt ();
18814 break;
18815
18816 case CONTINUE_STMT:
18817 finish_continue_stmt ();
18818 break;
18819
18820 case SWITCH_STMT:
18821 stmt = begin_switch_stmt ();
18822 tmp = RECUR (SWITCH_STMT_COND (t));
18823 finish_switch_cond (tmp, stmt);
18824 RECUR (SWITCH_STMT_BODY (t));
18825 finish_switch_stmt (stmt);
18826 break;
18827
18828 case CASE_LABEL_EXPR:
18829 {
18830 tree decl = CASE_LABEL (t);
18831 tree low = RECUR (CASE_LOW (t));
18832 tree high = RECUR (CASE_HIGH (t));
18833 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18834 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18835 {
18836 tree label = CASE_LABEL (l);
18837 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18838 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18839 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18840 }
18841 }
18842 break;
18843
18844 case LABEL_EXPR:
18845 {
18846 tree decl = LABEL_EXPR_LABEL (t);
18847 tree label;
18848
18849 label = finish_label_stmt (DECL_NAME (decl));
18850 if (TREE_CODE (label) == LABEL_DECL)
18851 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18852 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18853 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18854 }
18855 break;
18856
18857 case GOTO_EXPR:
18858 tmp = GOTO_DESTINATION (t);
18859 if (TREE_CODE (tmp) != LABEL_DECL)
18860 /* Computed goto's must be tsubst'd into. On the other hand,
18861 non-computed gotos must not be; the identifier in question
18862 will have no binding. */
18863 tmp = RECUR (tmp);
18864 else
18865 tmp = DECL_NAME (tmp);
18866 finish_goto_stmt (tmp);
18867 break;
18868
18869 case ASM_EXPR:
18870 {
18871 tree string = RECUR (ASM_STRING (t));
18872 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18873 complain, in_decl);
18874 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18875 complain, in_decl);
18876 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18877 complain, in_decl);
18878 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18879 complain, in_decl);
18880 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18881 outputs, inputs, clobbers, labels,
18882 ASM_INLINE_P (t));
18883 tree asm_expr = tmp;
18884 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18885 asm_expr = TREE_OPERAND (asm_expr, 0);
18886 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18887 }
18888 break;
18889
18890 case TRY_BLOCK:
18891 if (CLEANUP_P (t))
18892 {
18893 stmt = begin_try_block ();
18894 RECUR (TRY_STMTS (t));
18895 finish_cleanup_try_block (stmt);
18896 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18897 }
18898 else
18899 {
18900 tree compound_stmt = NULL_TREE;
18901
18902 if (FN_TRY_BLOCK_P (t))
18903 stmt = begin_function_try_block (&compound_stmt);
18904 else
18905 stmt = begin_try_block ();
18906
18907 RECUR (TRY_STMTS (t));
18908
18909 if (FN_TRY_BLOCK_P (t))
18910 finish_function_try_block (stmt);
18911 else
18912 finish_try_block (stmt);
18913
18914 RECUR (TRY_HANDLERS (t));
18915 if (FN_TRY_BLOCK_P (t))
18916 finish_function_handler_sequence (stmt, compound_stmt);
18917 else
18918 finish_handler_sequence (stmt);
18919 }
18920 break;
18921
18922 case HANDLER:
18923 {
18924 tree decl = HANDLER_PARMS (t);
18925
18926 if (decl)
18927 {
18928 decl = tsubst (t: decl, args, complain, in_decl);
18929 /* Prevent instantiate_decl from trying to instantiate
18930 this variable. We've already done all that needs to be
18931 done. */
18932 if (decl != error_mark_node)
18933 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18934 }
18935 stmt = begin_handler ();
18936 finish_handler_parms (decl, stmt);
18937 RECUR (HANDLER_BODY (t));
18938 finish_handler (stmt);
18939 }
18940 break;
18941
18942 case TAG_DEFN:
18943 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18944 if (dependent_type_p (tmp))
18945 /* This is a partial instantiation, try again when full. */
18946 add_stmt (build_min (TAG_DEFN, tmp));
18947 else if (CLASS_TYPE_P (tmp))
18948 {
18949 /* Local classes are not independent templates; they are
18950 instantiated along with their containing function. And this
18951 way we don't have to deal with pushing out of one local class
18952 to instantiate a member of another local class. */
18953 /* Closures are handled by the LAMBDA_EXPR. */
18954 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18955 complete_type (tmp);
18956 tree save_ccp = current_class_ptr;
18957 tree save_ccr = current_class_ref;
18958 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18959 if ((VAR_P (fld)
18960 || (TREE_CODE (fld) == FUNCTION_DECL
18961 && !DECL_ARTIFICIAL (fld)))
18962 && DECL_TEMPLATE_INSTANTIATION (fld))
18963 instantiate_decl (fld, /*defer_ok=*/false,
18964 /*expl_inst_class=*/false);
18965 else if (TREE_CODE (fld) == FIELD_DECL)
18966 maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
18967 current_class_ptr = save_ccp;
18968 current_class_ref = save_ccr;
18969 }
18970 break;
18971
18972 case STATIC_ASSERT:
18973 {
18974 tree condition, message;
18975
18976 ++c_inhibit_evaluation_warnings;
18977 condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
18978 complain, in_decl);
18979 message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
18980 complain, in_decl);
18981 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
18982 && TREE_CODE (message) == STRING_CST)
18983 message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
18984 code: PAREN_EXPR, TREE_TYPE (message), arg1: message);
18985 --c_inhibit_evaluation_warnings;
18986
18987 finish_static_assert (condition, message,
18988 STATIC_ASSERT_SOURCE_LOCATION (t),
18989 /*member_p=*/false, /*show_expr_p=*/true);
18990 }
18991 break;
18992
18993 case OACC_KERNELS:
18994 case OACC_PARALLEL:
18995 case OACC_SERIAL:
18996 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), ort: C_ORT_ACC_TARGET, args,
18997 complain, in_decl);
18998 stmt = begin_omp_parallel ();
18999 RECUR (OMP_BODY (t));
19000 finish_omp_construct (TREE_CODE (t), stmt, tmp);
19001 break;
19002
19003 case OMP_PARALLEL:
19004 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
19005 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), ort: C_ORT_OMP, args,
19006 complain, in_decl);
19007 if (OMP_PARALLEL_COMBINED (t))
19008 omp_parallel_combined_clauses = &tmp;
19009 stmt = begin_omp_parallel ();
19010 RECUR (OMP_PARALLEL_BODY (t));
19011 gcc_assert (omp_parallel_combined_clauses == NULL);
19012 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
19013 = OMP_PARALLEL_COMBINED (t);
19014 pop_omp_privatization_clauses (r);
19015 break;
19016
19017 case OMP_TASK:
19018 if (OMP_TASK_BODY (t) == NULL_TREE)
19019 {
19020 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), ort: C_ORT_OMP, args,
19021 complain, in_decl);
19022 t = copy_node (t);
19023 OMP_TASK_CLAUSES (t) = tmp;
19024 add_stmt (t);
19025 break;
19026 }
19027 r = push_omp_privatization_clauses (false);
19028 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), ort: C_ORT_OMP, args,
19029 complain, in_decl);
19030 stmt = begin_omp_task ();
19031 RECUR (OMP_TASK_BODY (t));
19032 finish_omp_task (tmp, stmt);
19033 pop_omp_privatization_clauses (r);
19034 break;
19035
19036 case OMP_FOR:
19037 case OMP_LOOP:
19038 case OMP_SIMD:
19039 case OMP_DISTRIBUTE:
19040 case OMP_TASKLOOP:
19041 case OACC_LOOP:
19042 {
19043 tree clauses, body, pre_body;
19044 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
19045 tree orig_declv = NULL_TREE;
19046 tree incrv = NULL_TREE;
19047 enum c_omp_region_type ort = C_ORT_OMP;
19048 bool any_range_for = false;
19049 int i;
19050
19051 if (TREE_CODE (t) == OACC_LOOP)
19052 ort = C_ORT_ACC;
19053
19054 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
19055 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
19056 in_decl);
19057 if (OMP_FOR_INIT (t) != NULL_TREE)
19058 {
19059 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19060 if (OMP_FOR_ORIG_DECLS (t))
19061 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19062 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19063 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19064 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19065 }
19066
19067 keep_next_level (true);
19068 stmt = begin_omp_structured_block ();
19069
19070 pre_body = push_stmt_list ();
19071 RECUR (OMP_FOR_PRE_BODY (t));
19072 pre_body = pop_stmt_list (pre_body);
19073
19074 if (OMP_FOR_INIT (t) != NULL_TREE)
19075 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19076 any_range_for
19077 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
19078 condv, incrv, clauses: &clauses, args,
19079 complain, in_decl);
19080 omp_parallel_combined_clauses = NULL;
19081
19082 if (any_range_for)
19083 {
19084 gcc_assert (orig_declv);
19085 body = begin_omp_structured_block ();
19086 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19087 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
19088 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
19089 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
19090 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
19091 TREE_VEC_ELT (declv, i));
19092 }
19093 else
19094 body = push_stmt_list ();
19095 RECUR (OMP_FOR_BODY (t));
19096 if (any_range_for)
19097 body = finish_omp_structured_block (body);
19098 else
19099 body = pop_stmt_list (body);
19100
19101 if (OMP_FOR_INIT (t) != NULL_TREE)
19102 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
19103 orig_declv, initv, condv, incrv, body, pre_body,
19104 NULL, clauses);
19105 else
19106 {
19107 t = make_node (TREE_CODE (t));
19108 TREE_TYPE (t) = void_type_node;
19109 OMP_FOR_BODY (t) = body;
19110 OMP_FOR_PRE_BODY (t) = pre_body;
19111 OMP_FOR_CLAUSES (t) = clauses;
19112 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
19113 add_stmt (t);
19114 }
19115
19116 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
19117 t));
19118 pop_omp_privatization_clauses (r);
19119 }
19120 break;
19121
19122 case OMP_SECTIONS:
19123 case OMP_MASKED:
19124 omp_parallel_combined_clauses = NULL;
19125 /* FALLTHRU */
19126 case OMP_SINGLE:
19127 case OMP_SCOPE:
19128 case OMP_TEAMS:
19129 case OMP_CRITICAL:
19130 case OMP_TASKGROUP:
19131 case OMP_SCAN:
19132 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
19133 && OMP_TEAMS_COMBINED (t));
19134 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), ort: C_ORT_OMP, args, complain,
19135 in_decl);
19136 if (TREE_CODE (t) == OMP_TEAMS)
19137 {
19138 keep_next_level (true);
19139 stmt = begin_omp_structured_block ();
19140 RECUR (OMP_BODY (t));
19141 stmt = finish_omp_structured_block (stmt);
19142 }
19143 else
19144 {
19145 stmt = push_stmt_list ();
19146 RECUR (OMP_BODY (t));
19147 stmt = pop_stmt_list (stmt);
19148 }
19149
19150 if (TREE_CODE (t) == OMP_CRITICAL
19151 && tmp != NULL_TREE
19152 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
19153 {
19154 error_at (OMP_CLAUSE_LOCATION (tmp),
19155 "%<#pragma omp critical%> with %<hint%> clause requires "
19156 "a name, except when %<omp_sync_hint_none%> is used");
19157 RETURN (error_mark_node);
19158 }
19159 t = copy_node (t);
19160 OMP_BODY (t) = stmt;
19161 OMP_CLAUSES (t) = tmp;
19162 add_stmt (t);
19163 pop_omp_privatization_clauses (r);
19164 break;
19165
19166 case OMP_DEPOBJ:
19167 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
19168 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
19169 {
19170 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
19171 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
19172 {
19173 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), ort: C_ORT_OMP,
19174 args, complain, in_decl);
19175 if (tmp == NULL_TREE)
19176 tmp = error_mark_node;
19177 }
19178 else
19179 {
19180 kind = (enum omp_clause_depend_kind)
19181 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
19182 tmp = NULL_TREE;
19183 }
19184 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
19185 }
19186 else
19187 finish_omp_depobj (EXPR_LOCATION (t), r,
19188 OMP_CLAUSE_DEPEND_INVALID,
19189 OMP_DEPOBJ_CLAUSES (t));
19190 break;
19191
19192 case OACC_DATA:
19193 case OMP_TARGET_DATA:
19194 case OMP_TARGET:
19195 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
19196 TREE_CODE (t) == OACC_DATA
19197 ? C_ORT_ACC
19198 : TREE_CODE (t) == OMP_TARGET
19199 ? C_ORT_OMP_TARGET : C_ORT_OMP,
19200 args, complain, in_decl);
19201 keep_next_level (true);
19202 stmt = begin_omp_structured_block ();
19203
19204 RECUR (OMP_BODY (t));
19205 stmt = finish_omp_structured_block (stmt);
19206
19207 t = copy_node (t);
19208 OMP_BODY (t) = stmt;
19209 OMP_CLAUSES (t) = tmp;
19210
19211 if (TREE_CODE (t) == OMP_TARGET)
19212 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
19213 &OMP_CLAUSES (t));
19214
19215 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19216 {
19217 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19218 if (teams)
19219 /* For combined target teams, ensure the num_teams and
19220 thread_limit clause expressions are evaluated on the host,
19221 before entering the target construct. */
19222 for (tree c = OMP_TEAMS_CLAUSES (teams);
19223 c; c = OMP_CLAUSE_CHAIN (c))
19224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19226 for (int i = 0;
19227 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
19228 if (OMP_CLAUSE_OPERAND (c, i)
19229 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
19230 {
19231 tree expr = OMP_CLAUSE_OPERAND (c, i);
19232 expr = force_target_expr (TREE_TYPE (expr), expr,
19233 tf_none);
19234 if (expr == error_mark_node)
19235 continue;
19236 tmp = TARGET_EXPR_SLOT (expr);
19237 add_stmt (expr);
19238 OMP_CLAUSE_OPERAND (c, i) = expr;
19239 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19240 OMP_CLAUSE_FIRSTPRIVATE);
19241 OMP_CLAUSE_DECL (tc) = tmp;
19242 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19243 OMP_TARGET_CLAUSES (t) = tc;
19244 }
19245 }
19246 add_stmt (t);
19247 break;
19248
19249 case OACC_DECLARE:
19250 t = copy_node (t);
19251 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), ort: C_ORT_ACC, args,
19252 complain, in_decl);
19253 OACC_DECLARE_CLAUSES (t) = tmp;
19254 add_stmt (t);
19255 break;
19256
19257 case OMP_TARGET_UPDATE:
19258 case OMP_TARGET_ENTER_DATA:
19259 case OMP_TARGET_EXIT_DATA:
19260 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), ort: C_ORT_OMP, args,
19261 complain, in_decl);
19262 t = copy_node (t);
19263 OMP_STANDALONE_CLAUSES (t) = tmp;
19264 add_stmt (t);
19265 break;
19266
19267 case OACC_CACHE:
19268 case OACC_ENTER_DATA:
19269 case OACC_EXIT_DATA:
19270 case OACC_UPDATE:
19271 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), ort: C_ORT_ACC, args,
19272 complain, in_decl);
19273 t = copy_node (t);
19274 OMP_STANDALONE_CLAUSES (t) = tmp;
19275 add_stmt (t);
19276 break;
19277
19278 case OMP_ORDERED:
19279 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), ort: C_ORT_OMP, args,
19280 complain, in_decl);
19281 if (OMP_BODY (t))
19282 {
19283 stmt = push_stmt_list ();
19284 RECUR (OMP_BODY (t));
19285 stmt = pop_stmt_list (stmt);
19286 }
19287 else
19288 stmt = NULL_TREE;
19289
19290 t = copy_node (t);
19291 OMP_BODY (t) = stmt;
19292 OMP_ORDERED_CLAUSES (t) = tmp;
19293 add_stmt (t);
19294 break;
19295
19296 case OMP_MASTER:
19297 case OMP_STRUCTURED_BLOCK:
19298 omp_parallel_combined_clauses = NULL;
19299 /* FALLTHRU */
19300 case OMP_SECTION:
19301 stmt = push_stmt_list ();
19302 RECUR (OMP_BODY (t));
19303 stmt = pop_stmt_list (stmt);
19304
19305 t = copy_node (t);
19306 OMP_BODY (t) = stmt;
19307 add_stmt (t);
19308 break;
19309
19310 case OMP_ATOMIC:
19311 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19312 tmp = NULL_TREE;
19313 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19314 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), ort: C_ORT_OMP, args,
19315 complain, in_decl);
19316 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19317 {
19318 tree op1 = TREE_OPERAND (t, 1);
19319 tree rhs1 = NULL_TREE;
19320 tree r = NULL_TREE;
19321 tree lhs, rhs;
19322 if (TREE_CODE (op1) == COMPOUND_EXPR)
19323 {
19324 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19325 op1 = TREE_OPERAND (op1, 1);
19326 }
19327 if (TREE_CODE (op1) == COND_EXPR)
19328 {
19329 gcc_assert (rhs1 == NULL_TREE);
19330 tree c = TREE_OPERAND (op1, 0);
19331 if (TREE_CODE (c) == MODIFY_EXPR)
19332 {
19333 r = RECUR (TREE_OPERAND (c, 0));
19334 c = TREE_OPERAND (c, 1);
19335 }
19336 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19337 rhs = RECUR (TREE_OPERAND (c, 1));
19338 lhs = RECUR (TREE_OPERAND (op1, 2));
19339 rhs1 = RECUR (TREE_OPERAND (op1, 1));
19340 }
19341 else
19342 {
19343 lhs = RECUR (TREE_OPERAND (op1, 0));
19344 rhs = RECUR (TREE_OPERAND (op1, 1));
19345 }
19346 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19347 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
19348 tmp, OMP_ATOMIC_MEMORY_ORDER (t),
19349 OMP_ATOMIC_WEAK (t));
19350 }
19351 else
19352 {
19353 tree op1 = TREE_OPERAND (t, 1);
19354 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19355 tree rhs1 = NULL_TREE, r = NULL_TREE;
19356 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19357 enum tree_code opcode = NOP_EXPR;
19358 if (code == OMP_ATOMIC_READ)
19359 {
19360 v = RECUR (TREE_OPERAND (op1, 0));
19361 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19362 }
19363 else if (code == OMP_ATOMIC_CAPTURE_OLD
19364 || code == OMP_ATOMIC_CAPTURE_NEW)
19365 {
19366 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19367 v = RECUR (TREE_OPERAND (op1, 0));
19368 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19369 if (TREE_CODE (op11) == COMPOUND_EXPR)
19370 {
19371 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19372 op11 = TREE_OPERAND (op11, 1);
19373 }
19374 if (TREE_CODE (op11) == COND_EXPR)
19375 {
19376 gcc_assert (rhs1 == NULL_TREE);
19377 tree c = TREE_OPERAND (op11, 0);
19378 if (TREE_CODE (c) == MODIFY_EXPR)
19379 {
19380 r = RECUR (TREE_OPERAND (c, 0));
19381 c = TREE_OPERAND (c, 1);
19382 }
19383 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19384 rhs = RECUR (TREE_OPERAND (c, 1));
19385 lhs = RECUR (TREE_OPERAND (op11, 2));
19386 rhs1 = RECUR (TREE_OPERAND (op11, 1));
19387 }
19388 else
19389 {
19390 lhs = RECUR (TREE_OPERAND (op11, 0));
19391 rhs = RECUR (TREE_OPERAND (op11, 1));
19392 }
19393 opcode = TREE_CODE (op11);
19394 if (opcode == MODIFY_EXPR)
19395 opcode = NOP_EXPR;
19396 }
19397 else
19398 {
19399 code = OMP_ATOMIC;
19400 lhs = RECUR (TREE_OPERAND (op1, 0));
19401 rhs = RECUR (TREE_OPERAND (op1, 1));
19402 }
19403 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19404 lhs1, rhs1, r, tmp,
19405 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
19406 }
19407 break;
19408
19409 case TRANSACTION_EXPR:
19410 {
19411 int flags = 0;
19412 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19413 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19414
19415 if (TRANSACTION_EXPR_IS_STMT (t))
19416 {
19417 tree body = TRANSACTION_EXPR_BODY (t);
19418 tree noex = NULL_TREE;
19419 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19420 {
19421 noex = MUST_NOT_THROW_COND (body);
19422 if (noex == NULL_TREE)
19423 noex = boolean_true_node;
19424 body = TREE_OPERAND (body, 0);
19425 }
19426 stmt = begin_transaction_stmt (input_location, NULL, flags);
19427 RECUR (body);
19428 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19429 }
19430 else
19431 {
19432 stmt = build_transaction_expr (EXPR_LOCATION (t),
19433 RECUR (TRANSACTION_EXPR_BODY (t)),
19434 flags, NULL_TREE);
19435 RETURN (stmt);
19436 }
19437 }
19438 break;
19439
19440 case MUST_NOT_THROW_EXPR:
19441 {
19442 tree op0 = RECUR (TREE_OPERAND (t, 0));
19443 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19444 RETURN (build_must_not_throw_expr (op0, cond));
19445 }
19446
19447 case EXPR_PACK_EXPANSION:
19448 error ("invalid use of pack expansion expression");
19449 RETURN (error_mark_node);
19450
19451 case NONTYPE_ARGUMENT_PACK:
19452 error ("use %<...%> to expand argument pack");
19453 RETURN (error_mark_node);
19454
19455 case COMPOUND_EXPR:
19456 tmp = RECUR (TREE_OPERAND (t, 0));
19457 if (tmp == NULL_TREE)
19458 /* If the first operand was a statement, we're done with it. */
19459 RETURN (RECUR (TREE_OPERAND (t, 1)));
19460 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19461 RECUR (TREE_OPERAND (t, 1)),
19462 templated_operator_saved_lookups (t),
19463 complain));
19464
19465 case PREDICT_EXPR:
19466 RETURN (add_stmt (copy_node (t)));
19467
19468 case ANNOTATE_EXPR:
19469 {
19470 /* Although ANNOTATE_EXPR is an expression, it can only appear in
19471 WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
19472 using tsubst_stmt rather than tsubst_expr and can contain
19473 DECL_EXPRs. */
19474 tree op1 = RECUR (TREE_OPERAND (t, 0));
19475 tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
19476 tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
19477 if (TREE_CODE (op2) == INTEGER_CST
19478 && wi::to_widest (t: op2) == (int) annot_expr_unroll_kind)
19479 op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
19480 op3);
19481 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19482 TREE_TYPE (op1), op1, op2, op3));
19483 }
19484
19485 default:
19486 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19487
19488 RETURN (tsubst_expr (t, args, complain, in_decl));
19489 }
19490
19491 RETURN (NULL_TREE);
19492 out:
19493 input_location = loc;
19494 return r;
19495#undef RECUR
19496#undef RETURN
19497}
19498
19499/* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19500 function. For description of the body see comment above
19501 cp_parser_omp_declare_reduction_exprs. */
19502
19503static void
19504tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19505{
19506 if (t == NULL_TREE || t == error_mark_node)
19507 return;
19508
19509 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19510
19511 tree_stmt_iterator tsi;
19512 int i;
19513 tree stmts[7];
19514 memset (s: stmts, c: 0, n: sizeof stmts);
19515 for (i = 0, tsi = tsi_start (t);
19516 i < 7 && !tsi_end_p (i: tsi);
19517 i++, tsi_next (i: &tsi))
19518 stmts[i] = tsi_stmt (i: tsi);
19519 gcc_assert (tsi_end_p (tsi));
19520
19521 if (i >= 3)
19522 {
19523 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19524 && TREE_CODE (stmts[1]) == DECL_EXPR);
19525 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19526 args, complain, in_decl);
19527 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19528 args, complain, in_decl);
19529 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19530 expect to be pushing it. */
19531 DECL_CONTEXT (omp_out) = current_function_decl;
19532 DECL_CONTEXT (omp_in) = current_function_decl;
19533 keep_next_level (true);
19534 tree block = begin_omp_structured_block ();
19535 tsubst_stmt (t: stmts[2], args, complain, in_decl);
19536 block = finish_omp_structured_block (block);
19537 block = maybe_cleanup_point_expr_void (block);
19538 add_decl_expr (omp_out);
19539 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
19540 add_decl_expr (omp_in);
19541 finish_expr_stmt (block);
19542 }
19543 if (i >= 6)
19544 {
19545 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19546 && TREE_CODE (stmts[4]) == DECL_EXPR);
19547 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19548 args, complain, in_decl);
19549 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19550 args, complain, in_decl);
19551 DECL_CONTEXT (omp_priv) = current_function_decl;
19552 DECL_CONTEXT (omp_orig) = current_function_decl;
19553 keep_next_level (true);
19554 tree block = begin_omp_structured_block ();
19555 tsubst_stmt (t: stmts[5], args, complain, in_decl);
19556 block = finish_omp_structured_block (block);
19557 block = maybe_cleanup_point_expr_void (block);
19558 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19559 add_decl_expr (omp_priv);
19560 add_decl_expr (omp_orig);
19561 finish_expr_stmt (block);
19562 if (i == 7)
19563 add_decl_expr (omp_orig);
19564 }
19565}
19566
19567/* T is a postfix-expression that is not being used in a function
19568 call. Return the substituted version of T. */
19569
19570static tree
19571tsubst_non_call_postfix_expression (tree t, tree args,
19572 tsubst_flags_t complain,
19573 tree in_decl)
19574{
19575 if (TREE_CODE (t) == SCOPE_REF)
19576 t = tsubst_qualified_id (qualified_id: t, args, complain, in_decl,
19577 /*done=*/false, /*address_p=*/false);
19578 else
19579 t = tsubst_expr (t, args, complain, in_decl);
19580
19581 return t;
19582}
19583
19584/* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19585 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19586 dependent init-capture. EXPLICIT_P is true if the original list had
19587 explicit captures. */
19588
19589static void
19590prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
19591 tsubst_flags_t complain)
19592{
19593 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19594 {
19595 tree type = NULL_TREE;
19596 if (!init)
19597 {
19598 if (complain & tf_error)
19599 error ("empty initializer in lambda init-capture");
19600 init = error_mark_node;
19601 }
19602 else if (TREE_CODE (init) == TREE_LIST)
19603 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19604 if (!type)
19605 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19606 TREE_TYPE (field) = type;
19607 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19608 }
19609 list = tree_cons (field, init, list);
19610 LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
19611}
19612
19613/* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19614 instantiation context. Instantiating a pack expansion containing a lambda
19615 might result in multiple lambdas all based on the same lambda in the
19616 template. */
19617
19618tree
19619tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19620{
19621 tree oldfn = lambda_function (t);
19622 in_decl = oldfn;
19623
19624 args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
19625 if (processing_template_decl
19626 && (!in_template_context || any_dependent_template_arguments_p (args)))
19627 {
19628 /* Defer templated substitution into a lambda-expr if we lost the
19629 necessary template context. This may happen for a lambda-expr
19630 used as a default template argument.
19631
19632 Defer dependent substitution as well so that we don't prematurely
19633 lower the level of a deduced return type or any other auto or
19634 template parameter belonging to the lambda. */
19635 t = copy_node (t);
19636 LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
19637 LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (pattern: t, args, complain);
19638 return t;
19639 }
19640
19641 tree r = build_lambda_expr ();
19642
19643 LAMBDA_EXPR_LOCATION (r)
19644 = LAMBDA_EXPR_LOCATION (t);
19645 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19646 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19647 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19648 LAMBDA_EXPR_REGEN_INFO (r)
19649 = build_template_info (template_decl: t, template_args: add_to_template_args (TI_ARGS (ti),
19650 extra_args: preserve_args (args)));
19651 else
19652 LAMBDA_EXPR_REGEN_INFO (r)
19653 = build_template_info (template_decl: t, template_args: preserve_args (args));
19654
19655 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19656 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19657
19658 vec<tree,va_gc>* field_packs = NULL;
19659 unsigned name_independent_cnt = 0;
19660 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19661 cap = TREE_CHAIN (cap))
19662 {
19663 tree ofield = TREE_PURPOSE (cap);
19664 tree init = TREE_VALUE (cap);
19665 if (PACK_EXPANSION_P (init))
19666 init = tsubst_pack_expansion (t: init, args, complain, in_decl);
19667 else
19668 init = tsubst_expr (init, args, complain, in_decl);
19669
19670 if (init == error_mark_node)
19671 return error_mark_node;
19672
19673 if (init && TREE_CODE (init) == TREE_LIST)
19674 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19675
19676 if (!processing_template_decl
19677 && init && TREE_CODE (init) != TREE_VEC
19678 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19679 {
19680 /* For a VLA, simply tsubsting the field type won't work, we need to
19681 go through add_capture again. XXX do we want to do this for all
19682 captures? */
19683 tree name = (get_identifier
19684 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19685 tree ftype = TREE_TYPE (ofield);
19686 bool by_ref = (TYPE_REF_P (ftype)
19687 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19688 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19689 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
19690 &name_independent_cnt);
19691 continue;
19692 }
19693
19694 if (PACK_EXPANSION_P (ofield))
19695 ofield = PACK_EXPANSION_PATTERN (ofield);
19696 tree field = tsubst_decl (t: ofield, args, complain);
19697
19698 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19699 {
19700 /* Remember these for when we've pushed local_specializations. */
19701 vec_safe_push (v&: field_packs, obj: ofield);
19702 vec_safe_push (v&: field_packs, obj: field);
19703 }
19704
19705 if (field == error_mark_node)
19706 return error_mark_node;
19707
19708 if (TREE_CODE (field) == TREE_VEC)
19709 {
19710 int len = TREE_VEC_LENGTH (field);
19711 gcc_assert (TREE_CODE (init) == TREE_VEC
19712 && TREE_VEC_LENGTH (init) == len);
19713 for (int i = 0; i < len; ++i)
19714 prepend_one_capture (TREE_VEC_ELT (field, i),
19715 TREE_VEC_ELT (init, i),
19716 LAMBDA_EXPR_CAPTURE_LIST (r),
19717 LAMBDA_CAPTURE_EXPLICIT_P (cap),
19718 complain);
19719 }
19720 else
19721 {
19722 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19723 LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
19724
19725 if (id_equal (DECL_NAME (field), str: "__this"))
19726 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19727 }
19728 }
19729
19730 tree type = begin_lambda_type (r);
19731 if (type == error_mark_node)
19732 {
19733 gcc_checking_assert (!(complain & tf_error) || seen_error ());
19734 return error_mark_node;
19735 }
19736
19737 if (LAMBDA_EXPR_EXTRA_SCOPE (t))
19738 record_lambda_scope (lambda: r);
19739 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
19740 /* If we're pushed into another scope (PR105652), fix it. */
19741 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
19742 = TYPE_CONTEXT (TREE_TYPE (t));
19743 record_lambda_scope_discriminator (lambda: r);
19744
19745 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19746 determine_visibility (TYPE_NAME (type));
19747
19748 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19749
19750 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19751 ? DECL_TI_TEMPLATE (oldfn)
19752 : NULL_TREE);
19753
19754 tree tparms = NULL_TREE;
19755 if (oldtmpl)
19756 tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
19757
19758 tree fntype = static_fn_type (oldfn);
19759
19760 tree saved_ctp = current_template_parms;
19761 if (oldtmpl)
19762 {
19763 ++processing_template_decl;
19764 current_template_parms = tparms;
19765 }
19766 fntype = tsubst (t: fntype, args, complain, in_decl);
19767 if (oldtmpl)
19768 {
19769 current_template_parms = saved_ctp;
19770 --processing_template_decl;
19771 }
19772
19773 if (fntype == error_mark_node)
19774 r = error_mark_node;
19775 else
19776 {
19777 /* The body of a lambda-expression is not a subexpression of the
19778 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19779 which would be skipped if cp_unevaluated_operand. */
19780 cp_evaluated ev;
19781
19782 /* Fix the type of 'this'.
19783 For static and xobj member functions we use this to transport the
19784 lambda's closure type. It appears that in the regular case the
19785 object parameter is still pulled off, and then re-added again anyway.
19786 So perhaps we could do something better here? */
19787 fntype = build_memfn_type (fntype, type,
19788 type_memfn_quals (fntype),
19789 type_memfn_rqual (fntype));
19790 tree inst = (oldtmpl
19791 ? tsubst_template_decl (t: oldtmpl, args, complain,
19792 lambda_fntype: fntype, lambda_tparms: tparms)
19793 : tsubst_function_decl (t: oldfn, args, complain, lambda_fntype: fntype));
19794 if (inst == error_mark_node)
19795 {
19796 r = error_mark_node;
19797 goto out;
19798 }
19799 finish_member_declaration (inst);
19800 record_lambda_scope_sig_discriminator (lambda: r, fn: inst);
19801
19802 tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
19803
19804 /* Let finish_function set this. */
19805 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19806
19807 bool nested = cfun;
19808 if (nested)
19809 push_function_context ();
19810 else
19811 /* Still increment function_depth so that we don't GC in the
19812 middle of an expression. */
19813 ++function_depth;
19814
19815 local_specialization_stack s (lss_copy);
19816
19817 bool save_in_consteval_if_p = in_consteval_if_p;
19818 in_consteval_if_p = false;
19819
19820 tree body = start_lambda_function (fn, lambda_expr: r);
19821
19822 /* Now record them for lookup_init_capture_pack. */
19823 int fplen = vec_safe_length (v: field_packs);
19824 for (int i = 0; i < fplen; )
19825 {
19826 tree pack = (*field_packs)[i++];
19827 tree inst = (*field_packs)[i++];
19828 register_local_specialization (spec: inst, tmpl: pack);
19829 }
19830 release_tree_vector (field_packs);
19831
19832 register_parameter_specializations (oldfn, fn);
19833
19834 if (oldtmpl)
19835 {
19836 /* We might not partially instantiate some parts of the function, so
19837 copy these flags from the original template. */
19838 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19839 current_function_returns_value = ol->returns_value;
19840 current_function_returns_null = ol->returns_null;
19841 current_function_returns_abnormally = ol->returns_abnormally;
19842 current_function_infinite_loop = ol->infinite_loop;
19843 }
19844
19845 /* [temp.deduct] A lambda-expression appearing in a function type or a
19846 template parameter is not considered part of the immediate context for
19847 the purposes of template argument deduction. */
19848 complain = tf_warning_or_error;
19849
19850 tree saved = DECL_SAVED_TREE (oldfn);
19851 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19852 /* We already have a body block from start_lambda_function, we don't
19853 need another to confuse NRV (91217). */
19854 saved = BIND_EXPR_BODY (saved);
19855
19856 tsubst_stmt (t: saved, args, complain, in_decl: r);
19857
19858 finish_lambda_function (body);
19859
19860 in_consteval_if_p = save_in_consteval_if_p;
19861
19862 if (nested)
19863 pop_function_context ();
19864 else
19865 --function_depth;
19866
19867 /* The capture list was built up in reverse order; fix that now. */
19868 LAMBDA_EXPR_CAPTURE_LIST (r)
19869 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19870
19871 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19872
19873 maybe_add_lambda_conv_op (type);
19874 }
19875
19876out:
19877 finish_struct (type, /*attr*/NULL_TREE);
19878
19879 insert_pending_capture_proxies ();
19880
19881 return r;
19882}
19883
19884/* Subroutine of maybe_fold_fn_template_args. */
19885
19886static bool
19887fold_targs_r (tree targs, tsubst_flags_t complain)
19888{
19889 int len = TREE_VEC_LENGTH (targs);
19890 for (int i = 0; i < len; ++i)
19891 {
19892 tree &elt = TREE_VEC_ELT (targs, i);
19893 if (!elt || TYPE_P (elt)
19894 || TREE_CODE (elt) == TEMPLATE_DECL)
19895 continue;
19896 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
19897 {
19898 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
19899 return false;
19900 }
19901 else if (/* We can only safely preevaluate scalar prvalues. */
19902 SCALAR_TYPE_P (TREE_TYPE (elt))
19903 && !glvalue_p (elt)
19904 && !TREE_CONSTANT (elt))
19905 {
19906 elt = cxx_constant_value (t: elt, complain);
19907 if (elt == error_mark_node)
19908 return false;
19909 }
19910 }
19911
19912 return true;
19913}
19914
19915/* Try to do constant evaluation of any explicit template arguments in FN
19916 before overload resolution, to get any errors only once. Return true iff
19917 we didn't have any problems folding. */
19918
19919static bool
19920maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
19921{
19922 if (processing_template_decl || fn == NULL_TREE)
19923 return true;
19924 if (fn == error_mark_node)
19925 return false;
19926 if (TREE_CODE (fn) == OFFSET_REF
19927 || TREE_CODE (fn) == COMPONENT_REF)
19928 fn = TREE_OPERAND (fn, 1);
19929 if (BASELINK_P (fn))
19930 fn = BASELINK_FUNCTIONS (fn);
19931 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
19932 return true;
19933 tree targs = TREE_OPERAND (fn, 1);
19934 if (targs == NULL_TREE)
19935 return true;
19936 if (targs == error_mark_node)
19937 return false;
19938 return fold_targs_r (targs, complain);
19939}
19940
19941/* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */
19942
19943static void
19944tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
19945 tree in_decl, releasing_vec &call_args)
19946{
19947 unsigned int nargs = call_expr_nargs (t);
19948 for (unsigned int i = 0; i < nargs; ++i)
19949 {
19950 tree arg = CALL_EXPR_ARG (t, i);
19951
19952 if (!PACK_EXPANSION_P (arg))
19953 vec_safe_push (r&: call_args, t: tsubst_expr (arg, args, complain, in_decl));
19954 else
19955 {
19956 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19957 arg = tsubst_pack_expansion (t: arg, args, complain, in_decl);
19958 if (TREE_CODE (arg) == TREE_VEC)
19959 {
19960 unsigned int len, j;
19961
19962 len = TREE_VEC_LENGTH (arg);
19963 for (j = 0; j < len; ++j)
19964 {
19965 tree value = TREE_VEC_ELT (arg, j);
19966 if (value != NULL_TREE)
19967 value = convert_from_reference (value);
19968 vec_safe_push (r&: call_args, t: value);
19969 }
19970 }
19971 else
19972 /* A partial substitution. Add one entry. */
19973 vec_safe_push (r&: call_args, t: arg);
19974 }
19975 }
19976}
19977
19978/* Like tsubst but deals with expressions and performs semantic
19979 analysis. */
19980
19981tree
19982tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19983{
19984#define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19985#define RECUR(NODE) \
19986 tsubst_expr (NODE, args, complain, in_decl)
19987
19988 tree retval, op1;
19989 location_t save_loc;
19990
19991 if (t == NULL_TREE || t == error_mark_node)
19992 return t;
19993
19994 save_loc = input_location;
19995 if (location_t eloc = cp_expr_location (t_: t))
19996 input_location = eloc;
19997
19998 /* N3276 decltype magic only applies to calls at the top level or on the
19999 right side of a comma. */
20000 tsubst_flags_t decltype_flag = (complain & tf_decltype);
20001 complain &= ~tf_decltype;
20002
20003 /* This flag only applies to id-expressions at the top level, and
20004 controls resolution thereof. */
20005 tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
20006 complain &= ~tf_no_name_lookup;
20007
20008 if (!no_name_lookup_flag)
20009 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
20010 return d;
20011
20012 switch (TREE_CODE (t))
20013 {
20014 case USING_DECL:
20015 t = DECL_NAME (t);
20016 /* Fall through. */
20017 case IDENTIFIER_NODE:
20018 {
20019 tree decl;
20020 cp_id_kind idk;
20021 const char *error_msg;
20022
20023 if (IDENTIFIER_CONV_OP_P (t))
20024 {
20025 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20026 t = make_conv_op_name (new_type);
20027 }
20028
20029 if (no_name_lookup_flag)
20030 RETURN (t);
20031
20032 /* Look up the name. */
20033 decl = lookup_name (name: t);
20034
20035 /* By convention, expressions use ERROR_MARK_NODE to indicate
20036 failure, not NULL_TREE. */
20037 if (decl == NULL_TREE)
20038 decl = error_mark_node;
20039
20040 decl = finish_id_expression (t, decl, NULL_TREE,
20041 &idk,
20042 /*i_c_e_p=*/false,
20043 /*allow_i_c_e_p=*/true,
20044 /*non_i_c_e_p=*/nullptr,
20045 /*template_p=*/false,
20046 /*done=*/true,
20047 /*address_p=*/false,
20048 /*template_arg_p=*/false,
20049 &error_msg,
20050 input_location);
20051 if (error_msg)
20052 error (error_msg);
20053 if (identifier_p (t: decl))
20054 {
20055 if (complain & tf_error)
20056 unqualified_name_lookup_error (decl);
20057 decl = error_mark_node;
20058 }
20059 RETURN (decl);
20060 }
20061
20062 case TEMPLATE_ID_EXPR:
20063 {
20064 tree object;
20065 tree templ = TREE_OPERAND (t, 0);
20066 tree targs = TREE_OPERAND (t, 1);
20067
20068 if (no_name_lookup_flag)
20069 templ = tsubst_name (t: templ, args, complain, in_decl);
20070 else
20071 templ = tsubst_expr (t: templ, args, complain, in_decl);
20072
20073 if (targs)
20074 targs = tsubst_template_args (t: targs, args, complain, in_decl);
20075 if (targs == error_mark_node)
20076 RETURN (error_mark_node);
20077
20078 if (TREE_CODE (templ) == SCOPE_REF)
20079 {
20080 tree name = TREE_OPERAND (templ, 1);
20081 tree tid = lookup_template_function (fns: name, arglist: targs);
20082 TREE_OPERAND (templ, 1) = tid;
20083 RETURN (templ);
20084 }
20085
20086 if (concept_definition_p (t: templ))
20087 {
20088 tree check = build_concept_check (templ, targs, complain);
20089 if (check == error_mark_node)
20090 RETURN (error_mark_node);
20091
20092 tree id = unpack_concept_check (check);
20093
20094 /* If we built a function concept check, return the underlying
20095 template-id. So we can evaluate it as a function call. */
20096 if (function_concept_p (TREE_OPERAND (id, 0)))
20097 RETURN (id);
20098
20099 RETURN (check);
20100 }
20101
20102 if (variable_template_p (t: templ))
20103 {
20104 if (no_name_lookup_flag)
20105 RETURN (lookup_template_variable (templ, targs, complain));
20106
20107 tree r = lookup_and_finish_template_variable (templ, targs,
20108 complain);
20109 r = convert_from_reference (r);
20110 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
20111 RETURN (r);
20112 }
20113
20114 if (TREE_CODE (templ) == COMPONENT_REF)
20115 {
20116 object = TREE_OPERAND (templ, 0);
20117 templ = TREE_OPERAND (templ, 1);
20118 }
20119 else
20120 object = NULL_TREE;
20121
20122 tree tid = lookup_template_function (fns: templ, arglist: targs);
20123 protected_set_expr_location (tid, EXPR_LOCATION (t));
20124
20125 if (object)
20126 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
20127 object, tid, NULL_TREE));
20128 else if (no_name_lookup_flag)
20129 RETURN (tid);
20130 else if (identifier_p (t: templ))
20131 {
20132 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20133 name lookup found nothing when parsing the template name. */
20134 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
20135 RETURN (tid);
20136 }
20137 else
20138 RETURN (baselink_for_fns (tid));
20139 }
20140
20141 case INDIRECT_REF:
20142 {
20143 tree r = RECUR (TREE_OPERAND (t, 0));
20144
20145 if (REFERENCE_REF_P (t))
20146 {
20147 /* A type conversion to reference type will be enclosed in
20148 such an indirect ref, but the substitution of the cast
20149 will have also added such an indirect ref. */
20150 r = convert_from_reference (r);
20151 }
20152 else
20153 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
20154 templated_operator_saved_lookups (t),
20155 complain|decltype_flag);
20156
20157 if (REF_PARENTHESIZED_P (t))
20158 r = force_paren_expr (r);
20159
20160 RETURN (r);
20161 }
20162
20163 case MEM_REF:
20164 {
20165 tree op0 = RECUR (TREE_OPERAND (t, 0));
20166 tree op1 = RECUR (TREE_OPERAND (t, 0));
20167 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20168 RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
20169 }
20170
20171 case NOP_EXPR:
20172 {
20173 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20174 tree op0 = RECUR (TREE_OPERAND (t, 0));
20175 RETURN (build_nop (type, op0));
20176 }
20177
20178 case IMPLICIT_CONV_EXPR:
20179 {
20180 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20181 tree expr = RECUR (TREE_OPERAND (t, 0));
20182 if (dependent_type_p (type) || type_dependent_expression_p (expr))
20183 {
20184 retval = copy_node (t);
20185 TREE_TYPE (retval) = type;
20186 TREE_OPERAND (retval, 0) = expr;
20187 RETURN (retval);
20188 }
20189 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
20190 {
20191 tree r = convert_nontype_argument (type, expr, complain);
20192 if (r == NULL_TREE)
20193 r = error_mark_node;
20194 RETURN (r);
20195 }
20196 int flags = LOOKUP_IMPLICIT;
20197 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
20198 flags = LOOKUP_NORMAL;
20199 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
20200 flags |= LOOKUP_NO_NARROWING;
20201 RETURN (perform_implicit_conversion_flags (type, expr, complain,
20202 flags));
20203 }
20204
20205 case CONVERT_EXPR:
20206 {
20207 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20208 tree op0 = RECUR (TREE_OPERAND (t, 0));
20209 if (op0 == error_mark_node)
20210 RETURN (error_mark_node);
20211 RETURN (build1 (CONVERT_EXPR, type, op0));
20212 }
20213
20214 case CAST_EXPR:
20215 case REINTERPRET_CAST_EXPR:
20216 case CONST_CAST_EXPR:
20217 case DYNAMIC_CAST_EXPR:
20218 case STATIC_CAST_EXPR:
20219 {
20220 tree type;
20221 tree op, r = NULL_TREE;
20222
20223 tsubst_flags_t tcomplain = complain;
20224 if (TREE_CODE (t) == CAST_EXPR)
20225 tcomplain |= tf_tst_ok;
20226 type = tsubst (TREE_TYPE (t), args, complain: tcomplain, in_decl);
20227
20228 op = RECUR (TREE_OPERAND (t, 0));
20229
20230 warning_sentinel s(warn_useless_cast);
20231 warning_sentinel s2(warn_ignored_qualifiers);
20232 warning_sentinel s3(warn_int_in_bool_context);
20233 switch (TREE_CODE (t))
20234 {
20235 case CAST_EXPR:
20236 r = build_functional_cast (input_location, type, op, complain);
20237 break;
20238 case REINTERPRET_CAST_EXPR:
20239 r = build_reinterpret_cast (input_location, type, op, complain);
20240 break;
20241 case CONST_CAST_EXPR:
20242 r = build_const_cast (input_location, type, op, complain);
20243 break;
20244 case DYNAMIC_CAST_EXPR:
20245 r = build_dynamic_cast (input_location, type, op, complain);
20246 break;
20247 case STATIC_CAST_EXPR:
20248 r = build_static_cast (input_location, type, op, complain);
20249 if (IMPLICIT_RVALUE_P (t))
20250 set_implicit_rvalue_p (r);
20251 break;
20252 default:
20253 gcc_unreachable ();
20254 }
20255
20256 RETURN (r);
20257 }
20258
20259 case BIT_CAST_EXPR:
20260 {
20261 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20262 tree op0 = RECUR (TREE_OPERAND (t, 0));
20263 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
20264 }
20265
20266 case POSTDECREMENT_EXPR:
20267 case POSTINCREMENT_EXPR:
20268 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20269 args, complain, in_decl);
20270 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
20271 templated_operator_saved_lookups (t),
20272 complain|decltype_flag));
20273
20274 case BIT_NOT_EXPR:
20275 if (identifier_p (TREE_OPERAND (t, 0)))
20276 {
20277 gcc_checking_assert (no_name_lookup_flag);
20278 RETURN (t);
20279 }
20280 else if (TYPE_P (TREE_OPERAND (t, 0)))
20281 {
20282 gcc_checking_assert (no_name_lookup_flag);
20283 tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
20284 RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
20285 }
20286 /* Fall through. */
20287 case PREDECREMENT_EXPR:
20288 case PREINCREMENT_EXPR:
20289 case NEGATE_EXPR:
20290 case ABS_EXPR:
20291 case TRUTH_NOT_EXPR:
20292 case UNARY_PLUS_EXPR: /* Unary + */
20293 case REALPART_EXPR:
20294 case IMAGPART_EXPR:
20295 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
20296 RECUR (TREE_OPERAND (t, 0)),
20297 templated_operator_saved_lookups (t),
20298 complain|decltype_flag));
20299
20300 case EXCESS_PRECISION_EXPR:
20301 {
20302 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20303 tree op0 = RECUR (TREE_OPERAND (t, 0));
20304 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
20305 RETURN (op0);
20306 RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
20307 type, op0));
20308 }
20309
20310 case FIX_TRUNC_EXPR:
20311 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20312 gcc_unreachable ();
20313
20314 case ADDR_EXPR:
20315 op1 = TREE_OPERAND (t, 0);
20316 if (TREE_CODE (op1) == LABEL_DECL)
20317 RETURN (finish_label_address_expr (DECL_NAME (op1),
20318 EXPR_LOCATION (op1)));
20319 if (TREE_CODE (op1) == SCOPE_REF)
20320 op1 = tsubst_qualified_id (qualified_id: op1, args, complain, in_decl,
20321 /*done=*/true, /*address_p=*/true);
20322 else
20323 op1 = tsubst_non_call_postfix_expression (t: op1, args, complain,
20324 in_decl);
20325 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
20326 templated_operator_saved_lookups (t),
20327 complain|decltype_flag));
20328
20329 case PLUS_EXPR:
20330 case MINUS_EXPR:
20331 case MULT_EXPR:
20332 case TRUNC_DIV_EXPR:
20333 case CEIL_DIV_EXPR:
20334 case FLOOR_DIV_EXPR:
20335 case ROUND_DIV_EXPR:
20336 case EXACT_DIV_EXPR:
20337 case BIT_AND_EXPR:
20338 case BIT_IOR_EXPR:
20339 case BIT_XOR_EXPR:
20340 case TRUNC_MOD_EXPR:
20341 case FLOOR_MOD_EXPR:
20342 case TRUTH_ANDIF_EXPR:
20343 case TRUTH_ORIF_EXPR:
20344 case TRUTH_AND_EXPR:
20345 case TRUTH_OR_EXPR:
20346 case RSHIFT_EXPR:
20347 case LSHIFT_EXPR:
20348 case EQ_EXPR:
20349 case NE_EXPR:
20350 case MAX_EXPR:
20351 case MIN_EXPR:
20352 case LE_EXPR:
20353 case GE_EXPR:
20354 case LT_EXPR:
20355 case GT_EXPR:
20356 case SPACESHIP_EXPR:
20357 case MEMBER_REF:
20358 case DOTSTAR_EXPR:
20359 {
20360 /* If either OP0 or OP1 was value- or type-dependent, suppress
20361 warnings that depend on the range of the types involved. */
20362 tree op0 = TREE_OPERAND (t, 0);
20363 tree op1 = TREE_OPERAND (t, 1);
20364 const bool was_dep = (dependent_operand_p (t: op0)
20365 || dependent_operand_p (t: op1));
20366 op0 = RECUR (op0);
20367 op1 = RECUR (op1);
20368
20369 warning_sentinel s1(warn_type_limits, was_dep);
20370 warning_sentinel s2(warn_div_by_zero, was_dep);
20371 warning_sentinel s3(warn_logical_op, was_dep);
20372 warning_sentinel s4(warn_tautological_compare, was_dep);
20373 warning_sentinel s5(warn_address, was_dep);
20374
20375 tree r = build_x_binary_op
20376 (input_location, TREE_CODE (t),
20377 op0,
20378 (warning_suppressed_p (TREE_OPERAND (t, 0))
20379 ? ERROR_MARK
20380 : TREE_CODE (TREE_OPERAND (t, 0))),
20381 op1,
20382 (warning_suppressed_p (TREE_OPERAND (t, 1))
20383 ? ERROR_MARK
20384 : TREE_CODE (TREE_OPERAND (t, 1))),
20385 templated_operator_saved_lookups (t),
20386 /*overload=*/NULL,
20387 complain|decltype_flag);
20388 if (EXPR_P (r))
20389 copy_warning (r, t);
20390
20391 RETURN (r);
20392 }
20393
20394 case POINTER_PLUS_EXPR:
20395 {
20396 tree op0 = RECUR (TREE_OPERAND (t, 0));
20397 if (op0 == error_mark_node)
20398 RETURN (error_mark_node);
20399 tree op1 = RECUR (TREE_OPERAND (t, 1));
20400 if (op1 == error_mark_node)
20401 RETURN (error_mark_node);
20402 RETURN (fold_build_pointer_plus (op0, op1));
20403 }
20404
20405 case SCOPE_REF:
20406 if (no_name_lookup_flag)
20407 {
20408 tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
20409 tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
20410 RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
20411 QUALIFIED_NAME_IS_TEMPLATE (t)));
20412 }
20413 else
20414 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
20415 /*address_p=*/false));
20416
20417 case BASELINK:
20418 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
20419 args, complain, in_decl));
20420
20421 case ARRAY_REF:
20422 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20423 args, complain, in_decl);
20424 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
20425 && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
20426 == ovl_op_identifier (code: ARRAY_REF)))
20427 {
20428 tree c = TREE_OPERAND (t, 1);
20429 releasing_vec index_exp_list;
20430 tsubst_call_args (t: c, args, complain, in_decl, call_args&: index_exp_list);
20431
20432 tree r;
20433 if (vec_safe_length (r&: index_exp_list) == 1
20434 && !PACK_EXPANSION_P (index_exp_list[0]))
20435 r = grok_array_decl (EXPR_LOCATION (t), op1,
20436 index_exp_list[0], NULL,
20437 complain | decltype_flag);
20438 else
20439 r = grok_array_decl (EXPR_LOCATION (t), op1,
20440 NULL_TREE, &index_exp_list,
20441 complain | decltype_flag);
20442 RETURN (r);
20443 }
20444 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
20445 RECUR (TREE_OPERAND (t, 1)),
20446 complain|decltype_flag));
20447
20448 case OMP_ARRAY_SECTION:
20449 {
20450 tree op0 = RECUR (TREE_OPERAND (t, 0));
20451 tree op1 = NULL_TREE, op2 = NULL_TREE;
20452 if (op0 == error_mark_node)
20453 RETURN (error_mark_node);
20454 if (TREE_OPERAND (t, 1))
20455 {
20456 op1 = RECUR (TREE_OPERAND (t, 1));
20457 if (op1 == error_mark_node)
20458 RETURN (error_mark_node);
20459 }
20460 if (TREE_OPERAND (t, 2))
20461 {
20462 op2 = RECUR (TREE_OPERAND (t, 2));
20463 if (op2 == error_mark_node)
20464 RETURN (error_mark_node);
20465 }
20466 RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
20467 }
20468
20469 case SIZEOF_EXPR:
20470 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
20471 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
20472 {
20473 tree expanded, op = TREE_OPERAND (t, 0);
20474 int len = 0;
20475
20476 if (SIZEOF_EXPR_TYPE_P (t))
20477 op = TREE_TYPE (op);
20478
20479 ++cp_unevaluated_operand;
20480 ++c_inhibit_evaluation_warnings;
20481 /* We only want to compute the number of arguments. */
20482 if (PACK_EXPANSION_P (op))
20483 expanded = tsubst_pack_expansion (t: op, args, complain, in_decl);
20484 else
20485 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
20486 args, complain, in_decl);
20487 --cp_unevaluated_operand;
20488 --c_inhibit_evaluation_warnings;
20489
20490 if (TREE_CODE (expanded) == TREE_VEC)
20491 {
20492 len = TREE_VEC_LENGTH (expanded);
20493 /* Set TREE_USED for the benefit of -Wunused. */
20494 for (int i = 0; i < len; i++)
20495 if (DECL_P (TREE_VEC_ELT (expanded, i)))
20496 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
20497 }
20498
20499 if (expanded == error_mark_node)
20500 RETURN (error_mark_node);
20501 else if (PACK_EXPANSION_P (expanded)
20502 || (TREE_CODE (expanded) == TREE_VEC
20503 && pack_expansion_args_count (args: expanded)))
20504
20505 {
20506 if (PACK_EXPANSION_P (expanded))
20507 /* OK. */;
20508 else
20509 expanded = make_argument_pack (vec: expanded);
20510
20511 if (TYPE_P (expanded))
20512 RETURN (cxx_sizeof_or_alignof_type (input_location,
20513 expanded, SIZEOF_EXPR,
20514 false,
20515 complain & tf_error));
20516 else
20517 RETURN (cxx_sizeof_or_alignof_expr (input_location,
20518 expanded, SIZEOF_EXPR,
20519 false,
20520 complain & tf_error));
20521 }
20522 else
20523 RETURN (build_int_cst (size_type_node, len));
20524 }
20525 /* Fall through */
20526
20527 case ALIGNOF_EXPR:
20528 {
20529 tree r;
20530
20531 op1 = TREE_OPERAND (t, 0);
20532 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
20533 op1 = TREE_TYPE (op1);
20534 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
20535 && ALIGNOF_EXPR_STD_P (t));
20536 if (!args)
20537 {
20538 /* When there are no ARGS, we are trying to evaluate a
20539 non-dependent expression from the parser. Trying to do
20540 the substitutions may not work. */
20541 if (!TYPE_P (op1))
20542 op1 = TREE_TYPE (op1);
20543 }
20544 else
20545 {
20546 ++cp_unevaluated_operand;
20547 ++c_inhibit_evaluation_warnings;
20548 if (TYPE_P (op1))
20549 op1 = tsubst (t: op1, args, complain, in_decl);
20550 else
20551 op1 = tsubst_expr (t: op1, args, complain, in_decl);
20552 --cp_unevaluated_operand;
20553 --c_inhibit_evaluation_warnings;
20554 }
20555 if (TYPE_P (op1))
20556 r = cxx_sizeof_or_alignof_type (input_location,
20557 op1, TREE_CODE (t), std_alignof,
20558 complain & tf_error);
20559 else
20560 r = cxx_sizeof_or_alignof_expr (input_location,
20561 op1, TREE_CODE (t), std_alignof,
20562 complain & tf_error);
20563 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
20564 {
20565 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
20566 {
20567 if (!processing_template_decl && TYPE_P (op1))
20568 {
20569 r = build_min (SIZEOF_EXPR, size_type_node,
20570 build1 (NOP_EXPR, op1, error_mark_node));
20571 SIZEOF_EXPR_TYPE_P (r) = 1;
20572 }
20573 else
20574 r = build_min (SIZEOF_EXPR, size_type_node, op1);
20575 TREE_SIDE_EFFECTS (r) = 0;
20576 TREE_READONLY (r) = 1;
20577 }
20578 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
20579 }
20580 RETURN (r);
20581 }
20582
20583 case AT_ENCODE_EXPR:
20584 {
20585 op1 = TREE_OPERAND (t, 0);
20586 ++cp_unevaluated_operand;
20587 ++c_inhibit_evaluation_warnings;
20588 op1 = tsubst (t: op1, args, complain, in_decl);
20589 --cp_unevaluated_operand;
20590 --c_inhibit_evaluation_warnings;
20591 RETURN (objc_build_encode_expr (op1));
20592 }
20593
20594 case NOEXCEPT_EXPR:
20595 op1 = TREE_OPERAND (t, 0);
20596 ++cp_unevaluated_operand;
20597 ++c_inhibit_evaluation_warnings;
20598 ++cp_noexcept_operand;
20599 op1 = tsubst_expr (t: op1, args, complain, in_decl);
20600 --cp_unevaluated_operand;
20601 --c_inhibit_evaluation_warnings;
20602 --cp_noexcept_operand;
20603 RETURN (finish_noexcept_expr (op1, complain));
20604
20605 case MODOP_EXPR:
20606 {
20607 warning_sentinel s(warn_div_by_zero);
20608 tree lhs = RECUR (TREE_OPERAND (t, 0));
20609 tree rhs = RECUR (TREE_OPERAND (t, 2));
20610
20611 tree r = build_x_modify_expr
20612 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
20613 templated_operator_saved_lookups (t),
20614 complain|decltype_flag);
20615 /* TREE_NO_WARNING must be set if either the expression was
20616 parenthesized or it uses an operator such as >>= rather
20617 than plain assignment. In the former case, it was already
20618 set and must be copied. In the latter case,
20619 build_x_modify_expr sets it and it must not be reset
20620 here. */
20621 if (warning_suppressed_p (t, OPT_Wparentheses))
20622 suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
20623
20624 RETURN (r);
20625 }
20626
20627 case ARROW_EXPR:
20628 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20629 args, complain, in_decl);
20630 /* Remember that there was a reference to this entity. */
20631 if (DECL_P (op1)
20632 && !mark_used (op1, complain) && !(complain & tf_error))
20633 RETURN (error_mark_node);
20634 RETURN (build_x_arrow (input_location, op1, complain));
20635
20636 case NEW_EXPR:
20637 {
20638 tree placement = RECUR (TREE_OPERAND (t, 0));
20639 tree init = RECUR (TREE_OPERAND (t, 3));
20640 vec<tree, va_gc> *placement_vec;
20641 vec<tree, va_gc> *init_vec;
20642 tree ret;
20643 location_t loc = EXPR_LOCATION (t);
20644
20645 if (placement == NULL_TREE)
20646 placement_vec = NULL;
20647 else if (placement == error_mark_node)
20648 RETURN (error_mark_node);
20649 else
20650 {
20651 placement_vec = make_tree_vector ();
20652 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20653 vec_safe_push (v&: placement_vec, TREE_VALUE (placement));
20654 }
20655
20656 /* If there was an initializer in the original tree, but it
20657 instantiated to an empty list, then we should pass a
20658 non-NULL empty vector to tell build_new that it was an
20659 empty initializer() rather than no initializer. This can
20660 only happen when the initializer is a pack expansion whose
20661 parameter packs are of length zero. */
20662 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20663 init_vec = NULL;
20664 else if (init == error_mark_node)
20665 RETURN (error_mark_node);
20666 else
20667 {
20668 init_vec = make_tree_vector ();
20669 if (init == void_node)
20670 gcc_assert (init_vec != NULL);
20671 else
20672 {
20673 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20674 vec_safe_push (v&: init_vec, TREE_VALUE (init));
20675 }
20676 }
20677
20678 /* Avoid passing an enclosing decl to valid_array_size_p. */
20679 in_decl = NULL_TREE;
20680
20681 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20682 tree op2 = RECUR (TREE_OPERAND (t, 2));
20683 ret = build_new (loc, &placement_vec, op1, op2,
20684 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20685 complain);
20686
20687 if (placement_vec != NULL)
20688 release_tree_vector (placement_vec);
20689 if (init_vec != NULL)
20690 release_tree_vector (init_vec);
20691
20692 RETURN (ret);
20693 }
20694
20695 case DELETE_EXPR:
20696 {
20697 tree op0 = RECUR (TREE_OPERAND (t, 0));
20698 tree op1 = RECUR (TREE_OPERAND (t, 1));
20699 RETURN (delete_sanity (input_location, op0, op1,
20700 DELETE_EXPR_USE_VEC (t),
20701 DELETE_EXPR_USE_GLOBAL (t),
20702 complain));
20703 }
20704
20705 case COMPOUND_EXPR:
20706 {
20707 tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
20708 complain: complain & ~tf_decltype, in_decl);
20709 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20710 op0,
20711 RECUR (TREE_OPERAND (t, 1)),
20712 templated_operator_saved_lookups (t),
20713 complain|decltype_flag));
20714 }
20715
20716 case CALL_EXPR:
20717 {
20718 tree function;
20719 unsigned int nargs;
20720 bool qualified_p;
20721 bool koenig_p;
20722 tree ret;
20723
20724 function = CALL_EXPR_FN (t);
20725 /* Internal function with no arguments. */
20726 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20727 RETURN (t);
20728
20729 /* When we parsed the expression, we determined whether or
20730 not Koenig lookup should be performed. */
20731 koenig_p = KOENIG_LOOKUP_P (t);
20732 if (function == NULL_TREE)
20733 {
20734 koenig_p = false;
20735 qualified_p = false;
20736 }
20737 else if (TREE_CODE (function) == SCOPE_REF)
20738 {
20739 qualified_p = true;
20740 function = tsubst_qualified_id (qualified_id: function, args, complain, in_decl,
20741 /*done=*/false,
20742 /*address_p=*/false);
20743 }
20744 else if (CALL_EXPR_STATIC_CHAIN (t)
20745 && TREE_CODE (function) == FUNCTION_DECL
20746 && fndecl_built_in_p (node: function, name1: BUILT_IN_CLASSIFY_TYPE))
20747 {
20748 tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
20749 in_decl);
20750 if (dependent_type_p (type))
20751 {
20752 ret = build_vl_exp (CALL_EXPR, 4);
20753 CALL_EXPR_FN (ret) = function;
20754 CALL_EXPR_STATIC_CHAIN (ret) = type;
20755 CALL_EXPR_ARG (ret, 0)
20756 = build_min (SIZEOF_EXPR, size_type_node, type);
20757 TREE_TYPE (ret) = integer_type_node;
20758 }
20759 else
20760 ret = build_int_cst (integer_type_node, type_to_class (type));
20761 RETURN (ret);
20762 }
20763 else if (koenig_p
20764 && (identifier_p (t: function)
20765 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20766 && identifier_p (TREE_OPERAND (function, 0)))))
20767 {
20768 /* Do nothing; calling tsubst_expr on an identifier
20769 would incorrectly perform unqualified lookup again.
20770
20771 Note that we can also have an IDENTIFIER_NODE if the earlier
20772 unqualified lookup found a dependent local extern declaration
20773 (as per finish_call_expr); in that case koenig_p will be false
20774 and we do want to do the lookup again to find the substituted
20775 declaration. */
20776 qualified_p = false;
20777
20778 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20779 function = tsubst_name (t: function, args, complain, in_decl);
20780 }
20781 else
20782 {
20783 if (TREE_CODE (function) == COMPONENT_REF)
20784 {
20785 tree op = TREE_OPERAND (function, 1);
20786
20787 qualified_p = (TREE_CODE (op) == SCOPE_REF
20788 || (BASELINK_P (op)
20789 && BASELINK_QUALIFIED_P (op)));
20790 }
20791 else
20792 qualified_p = false;
20793
20794 if (TREE_CODE (function) == ADDR_EXPR
20795 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20796 /* Avoid error about taking the address of a constructor. */
20797 function = TREE_OPERAND (function, 0);
20798
20799 tsubst_flags_t subcomplain = complain;
20800 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20801 /* When KOENIG_P, we don't want to mark_used the callee before
20802 augmenting the overload set via ADL, so during this initial
20803 substitution we disable mark_used by setting tf_conv (68942). */
20804 subcomplain |= tf_conv;
20805 function = tsubst_expr (t: function, args, complain: subcomplain, in_decl);
20806
20807 if (BASELINK_P (function))
20808 qualified_p = true;
20809 }
20810
20811 nargs = call_expr_nargs (t);
20812 releasing_vec call_args;
20813 tsubst_call_args (t, args, complain, in_decl, call_args);
20814
20815 /* Stripped-down processing for a call in a thunk. Specifically, in
20816 the thunk template for a generic lambda. */
20817 if (call_from_lambda_thunk_p (t))
20818 {
20819 /* Now that we've expanded any packs, the number of call args
20820 might be different. */
20821 unsigned int cargs = call_args->length ();
20822 tree thisarg = NULL_TREE;
20823 if (TREE_CODE (function) == COMPONENT_REF)
20824 {
20825 thisarg = TREE_OPERAND (function, 0);
20826 if (TREE_CODE (thisarg) == INDIRECT_REF)
20827 thisarg = TREE_OPERAND (thisarg, 0);
20828 function = TREE_OPERAND (function, 1);
20829 if (TREE_CODE (function) == BASELINK)
20830 function = BASELINK_FUNCTIONS (function);
20831 }
20832 /* We aren't going to do normal overload resolution, so force the
20833 template-id to resolve. */
20834 function = resolve_nondeduced_context (function, complain);
20835 for (unsigned i = 0; i < cargs; ++i)
20836 {
20837 /* In a thunk, pass through args directly, without any
20838 conversions. */
20839 tree arg = (*call_args)[i];
20840 while (TREE_CODE (arg) != PARM_DECL)
20841 arg = TREE_OPERAND (arg, 0);
20842 (*call_args)[i] = arg;
20843 }
20844 if (thisarg)
20845 {
20846 /* If there are no other args, just push 'this'. */
20847 if (cargs == 0)
20848 vec_safe_push (r&: call_args, t: thisarg);
20849 else
20850 {
20851 /* Otherwise, shift the other args over to make room. */
20852 tree last = (*call_args)[cargs - 1];
20853 vec_safe_push (r&: call_args, t: last);
20854 for (int i = cargs - 1; i > 0; --i)
20855 (*call_args)[i] = (*call_args)[i - 1];
20856 (*call_args)[0] = thisarg;
20857 }
20858 }
20859 ret = build_call_a (function, call_args->length (),
20860 call_args->address ());
20861 /* The thunk location is not interesting. */
20862 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20863 CALL_FROM_THUNK_P (ret) = true;
20864 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20865 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20866
20867 RETURN (ret);
20868 }
20869
20870 /* We do not perform argument-dependent lookup if normal
20871 lookup finds a non-function, in accordance with the
20872 resolution of DR 218. */
20873 if (koenig_p
20874 && ((is_overloaded_fn (function)
20875 /* If lookup found a member function, the Koenig lookup is
20876 not appropriate, even if an unqualified-name was used
20877 to denote the function. */
20878 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20879 || identifier_p (t: function)
20880 /* C++20 P0846: Lookup found nothing. */
20881 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20882 && identifier_p (TREE_OPERAND (function, 0))))
20883 /* Only do this when substitution turns a dependent call
20884 into a non-dependent call. */
20885 && type_dependent_expression_p_push (t)
20886 && !any_type_dependent_arguments_p (call_args))
20887 function = perform_koenig_lookup (function, call_args, tf_none);
20888
20889 if (function != NULL_TREE
20890 && (identifier_p (t: function)
20891 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20892 && identifier_p (TREE_OPERAND (function, 0))
20893 && !any_dependent_template_arguments_p (TREE_OPERAND
20894 (function, 1))))
20895 && !any_type_dependent_arguments_p (call_args))
20896 {
20897 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
20898 if (template_id_p)
20899 function = TREE_OPERAND (function, 0);
20900 if (koenig_p && (complain & tf_warning_or_error))
20901 {
20902 /* For backwards compatibility and good diagnostics, try
20903 the unqualified lookup again if we aren't in SFINAE
20904 context. */
20905 tree unq = tsubst_expr (t: function, args, complain, in_decl);
20906 if (unq == error_mark_node)
20907 RETURN (error_mark_node);
20908
20909 if (unq != function)
20910 {
20911 char const *const msg
20912 = G_("%qD was not declared in this scope, "
20913 "and no declarations were found by "
20914 "argument-dependent lookup at the point "
20915 "of instantiation");
20916
20917 bool in_lambda = (current_class_type
20918 && LAMBDA_TYPE_P (current_class_type));
20919 /* In a lambda fn, we have to be careful to not
20920 introduce new this captures. Legacy code can't
20921 be using lambdas anyway, so it's ok to be
20922 stricter. Be strict with C++20 template-id ADL too.
20923 And be strict if we're already failing anyway. */
20924 bool strict = in_lambda || template_id_p || seen_error();
20925 bool diag = true;
20926 if (strict)
20927 error_at (cp_expr_loc_or_input_loc (t),
20928 msg, function);
20929 else
20930 diag = permerror (cp_expr_loc_or_input_loc (t),
20931 msg, function);
20932 if (diag)
20933 {
20934 tree fn = unq;
20935
20936 if (INDIRECT_REF_P (fn))
20937 fn = TREE_OPERAND (fn, 0);
20938 if (is_overloaded_fn (fn))
20939 fn = get_first_fn (fn);
20940
20941 if (!DECL_P (fn))
20942 /* Can't say anything more. */;
20943 else if (DECL_CLASS_SCOPE_P (fn))
20944 {
20945 location_t loc = cp_expr_loc_or_input_loc (t);
20946 inform (loc,
20947 "declarations in dependent base %qT are "
20948 "not found by unqualified lookup",
20949 DECL_CLASS_CONTEXT (fn));
20950 if (current_class_ptr)
20951 inform (loc,
20952 "use %<this->%D%> instead", function);
20953 else
20954 inform (loc,
20955 "use %<%T::%D%> instead",
20956 current_class_name, function);
20957 }
20958 else
20959 inform (DECL_SOURCE_LOCATION (fn),
20960 "%qD declared here, later in the "
20961 "translation unit", fn);
20962 if (strict)
20963 RETURN (error_mark_node);
20964 }
20965
20966 function = unq;
20967 }
20968 }
20969 if (identifier_p (t: function))
20970 {
20971 if (complain & tf_error)
20972 unqualified_name_lookup_error (function);
20973 RETURN (error_mark_node);
20974 }
20975 }
20976
20977 /* Remember that there was a reference to this entity. */
20978 if (function != NULL_TREE
20979 && DECL_P (function)
20980 && !mark_used (function, complain) && !(complain & tf_error))
20981 RETURN (error_mark_node);
20982
20983 if (!maybe_fold_fn_template_args (fn: function, complain))
20984 return error_mark_node;
20985
20986 /* Put back tf_decltype for the actual call. */
20987 complain |= decltype_flag;
20988
20989 if (function == NULL_TREE)
20990 switch (CALL_EXPR_IFN (t))
20991 {
20992 case IFN_LAUNDER:
20993 gcc_assert (nargs == 1);
20994 if (vec_safe_length (r&: call_args) != 1)
20995 {
20996 error_at (cp_expr_loc_or_input_loc (t),
20997 "wrong number of arguments to "
20998 "%<__builtin_launder%>");
20999 ret = error_mark_node;
21000 }
21001 else
21002 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
21003 (*call_args)[0], complain);
21004 break;
21005
21006 case IFN_VEC_CONVERT:
21007 gcc_assert (nargs == 1);
21008 if (vec_safe_length (r&: call_args) != 1)
21009 {
21010 error_at (cp_expr_loc_or_input_loc (t),
21011 "wrong number of arguments to "
21012 "%<__builtin_convertvector%>");
21013 ret = error_mark_node;
21014 break;
21015 }
21016 ret = cp_build_vec_convert ((*call_args)[0], input_location,
21017 tsubst (TREE_TYPE (t), args,
21018 complain, in_decl),
21019 complain);
21020 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
21021 RETURN (ret);
21022 break;
21023
21024 case IFN_SHUFFLEVECTOR:
21025 {
21026 ret = build_x_shufflevector (input_location, call_args,
21027 complain);
21028 if (ret != error_mark_node)
21029 RETURN (ret);
21030 break;
21031 }
21032
21033 case IFN_ASSUME:
21034 gcc_assert (nargs == 1);
21035 if (vec_safe_length (r&: call_args) != 1)
21036 {
21037 error_at (cp_expr_loc_or_input_loc (t),
21038 "wrong number of arguments to "
21039 "%<assume%> attribute");
21040 ret = error_mark_node;
21041 }
21042 else
21043 {
21044 tree &arg = (*call_args)[0];
21045 if (!type_dependent_expression_p (arg))
21046 arg = contextual_conv_bool (arg, tf_warning_or_error);
21047 if (error_operand_p (t: arg))
21048 {
21049 ret = error_mark_node;
21050 break;
21051 }
21052 ret = build_assume_call (EXPR_LOCATION (t), arg);
21053 RETURN (ret);
21054 }
21055 break;
21056
21057 default:
21058 /* Unsupported internal function with arguments. */
21059 gcc_unreachable ();
21060 }
21061 else if (TREE_CODE (function) == OFFSET_REF
21062 || TREE_CODE (function) == DOTSTAR_EXPR
21063 || TREE_CODE (function) == MEMBER_REF)
21064 ret = build_offset_ref_call_from_tree (function, &call_args,
21065 complain);
21066 else if (concept_check_p (t: function))
21067 {
21068 /* FUNCTION is a template-id referring to a concept definition. */
21069 tree id = unpack_concept_check (function);
21070 tree tmpl = TREE_OPERAND (id, 0);
21071 tree args = TREE_OPERAND (id, 1);
21072
21073 /* Calls to standard and variable concepts should have been
21074 previously diagnosed. */
21075 gcc_assert (function_concept_p (tmpl));
21076
21077 /* Ensure the result is wrapped as a call expression. */
21078 ret = build_concept_check (tmpl, args, tf_warning_or_error);
21079 }
21080 else
21081 ret = finish_call_expr (function, &call_args,
21082 /*disallow_virtual=*/qualified_p,
21083 koenig_p,
21084 complain);
21085
21086 if (ret != error_mark_node)
21087 {
21088 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
21089 bool ord = CALL_EXPR_ORDERED_ARGS (t);
21090 bool rev = CALL_EXPR_REVERSE_ARGS (t);
21091 if (op || ord || rev)
21092 if (tree call = extract_call_expr (ret))
21093 {
21094 CALL_EXPR_OPERATOR_SYNTAX (call) = op;
21095 CALL_EXPR_ORDERED_ARGS (call) = ord;
21096 CALL_EXPR_REVERSE_ARGS (call) = rev;
21097 }
21098 if (warning_suppressed_p (t, OPT_Wpessimizing_move))
21099 /* This also suppresses -Wredundant-move. */
21100 suppress_warning (ret, OPT_Wpessimizing_move);
21101 }
21102
21103 RETURN (ret);
21104 }
21105
21106 case COND_EXPR:
21107 {
21108 tree cond = RECUR (TREE_OPERAND (t, 0));
21109 cond = mark_rvalue_use (cond);
21110 tree folded_cond = fold_non_dependent_expr (cond, complain);
21111 tree exp1, exp2;
21112
21113 if (TREE_CODE (folded_cond) == INTEGER_CST)
21114 {
21115 if (integer_zerop (folded_cond))
21116 {
21117 ++c_inhibit_evaluation_warnings;
21118 exp1 = RECUR (TREE_OPERAND (t, 1));
21119 --c_inhibit_evaluation_warnings;
21120 exp2 = RECUR (TREE_OPERAND (t, 2));
21121 }
21122 else
21123 {
21124 exp1 = RECUR (TREE_OPERAND (t, 1));
21125 ++c_inhibit_evaluation_warnings;
21126 exp2 = RECUR (TREE_OPERAND (t, 2));
21127 --c_inhibit_evaluation_warnings;
21128 }
21129 cond = folded_cond;
21130 }
21131 else
21132 {
21133 exp1 = RECUR (TREE_OPERAND (t, 1));
21134 exp2 = RECUR (TREE_OPERAND (t, 2));
21135 }
21136
21137 warning_sentinel s(warn_duplicated_branches);
21138 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
21139 cond, exp1, exp2, complain));
21140 }
21141
21142 case PSEUDO_DTOR_EXPR:
21143 {
21144 tree op0 = RECUR (TREE_OPERAND (t, 0));
21145 tree op1 = RECUR (TREE_OPERAND (t, 1));
21146 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
21147 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
21148 input_location));
21149 }
21150
21151 case TREE_LIST:
21152 RETURN (tsubst_tree_list (t, args, complain, in_decl));
21153
21154 case COMPONENT_REF:
21155 {
21156 tree object;
21157 tree object_type;
21158 tree member;
21159 tree r;
21160
21161 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21162 args, complain, in_decl);
21163 /* Remember that there was a reference to this entity. */
21164 if (DECL_P (object)
21165 && !mark_used (object, complain) && !(complain & tf_error))
21166 RETURN (error_mark_node);
21167 object_type = TREE_TYPE (object);
21168
21169 member = TREE_OPERAND (t, 1);
21170 if (BASELINK_P (member))
21171 member = tsubst_baselink (baselink: member,
21172 object_type: non_reference (TREE_TYPE (object)),
21173 args, complain, in_decl);
21174 else
21175 member = tsubst_name (t: member, args, complain, in_decl);
21176 if (member == error_mark_node)
21177 RETURN (error_mark_node);
21178
21179 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
21180 && TREE_CODE (member) == FIELD_DECL)
21181 {
21182 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
21183 RETURN (r);
21184 }
21185 else if (TREE_CODE (member) == FIELD_DECL)
21186 {
21187 r = finish_non_static_data_member (member, object, NULL_TREE,
21188 complain);
21189 if (TREE_CODE (r) == COMPONENT_REF)
21190 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
21191 RETURN (r);
21192 }
21193 else if (type_dependent_expression_p (object))
21194 /* We can't do much here. */;
21195 else if (!CLASS_TYPE_P (object_type))
21196 {
21197 if (scalarish_type_p (object_type))
21198 {
21199 tree s = NULL_TREE;
21200 tree dtor = member;
21201
21202 if (TREE_CODE (dtor) == SCOPE_REF)
21203 {
21204 s = TREE_OPERAND (dtor, 0);
21205 dtor = TREE_OPERAND (dtor, 1);
21206 }
21207 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
21208 {
21209 dtor = TREE_OPERAND (dtor, 0);
21210 if (TYPE_P (dtor))
21211 RETURN (finish_pseudo_destructor_expr
21212 (object, s, dtor, input_location));
21213 }
21214 }
21215 }
21216 else if (TREE_CODE (member) == SCOPE_REF
21217 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
21218 {
21219 /* Lookup the template functions now that we know what the
21220 scope is. */
21221 tree scope = TREE_OPERAND (member, 0);
21222 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
21223 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
21224 member = lookup_qualified_name (scope, name: tmpl, LOOK_want::NORMAL,
21225 /*complain=*/false);
21226 if (BASELINK_P (member))
21227 {
21228 BASELINK_FUNCTIONS (member)
21229 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
21230 args);
21231 member = (adjust_result_of_qualified_name_lookup
21232 (member, BINFO_TYPE (BASELINK_BINFO (member)),
21233 object_type));
21234 }
21235 else
21236 {
21237 qualified_name_lookup_error (scope, tmpl, member,
21238 input_location);
21239 RETURN (error_mark_node);
21240 }
21241 }
21242 else if (TREE_CODE (member) == SCOPE_REF
21243 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
21244 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
21245 {
21246 if (complain & tf_error)
21247 {
21248 if (TYPE_P (TREE_OPERAND (member, 0)))
21249 error ("%qT is not a class or namespace",
21250 TREE_OPERAND (member, 0));
21251 else
21252 error ("%qD is not a class or namespace",
21253 TREE_OPERAND (member, 0));
21254 }
21255 RETURN (error_mark_node);
21256 }
21257
21258 r = finish_class_member_access_expr (object, member,
21259 /*template_p=*/false,
21260 complain);
21261 if (REF_PARENTHESIZED_P (t))
21262 r = force_paren_expr (r);
21263 RETURN (r);
21264 }
21265
21266 case THROW_EXPR:
21267 RETURN (build_throw
21268 (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
21269
21270 case CONSTRUCTOR:
21271 {
21272 vec<constructor_elt, va_gc> *n;
21273 constructor_elt *ce;
21274 unsigned HOST_WIDE_INT idx;
21275 bool process_index_p;
21276 int newlen;
21277 bool need_copy_p = false;
21278 tree r;
21279
21280 tsubst_flags_t tcomplain = complain;
21281 if (COMPOUND_LITERAL_P (t))
21282 tcomplain |= tf_tst_ok;
21283 tree type = tsubst (TREE_TYPE (t), args, complain: tcomplain, in_decl);
21284 if (type == error_mark_node)
21285 RETURN (error_mark_node);
21286
21287 /* We do not want to process the index of aggregate
21288 initializers as they are identifier nodes which will be
21289 looked up by digest_init. */
21290 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
21291
21292 if (null_member_pointer_value_p (t))
21293 {
21294 gcc_assert (same_type_p (type, TREE_TYPE (t)));
21295 RETURN (t);
21296 }
21297
21298 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
21299 newlen = vec_safe_length (v: n);
21300 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
21301 {
21302 if (ce->index && process_index_p
21303 /* An identifier index is looked up in the type
21304 being initialized, not the current scope. */
21305 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
21306 ce->index = RECUR (ce->index);
21307
21308 if (PACK_EXPANSION_P (ce->value))
21309 {
21310 /* Substitute into the pack expansion. */
21311 ce->value = tsubst_pack_expansion (t: ce->value, args, complain,
21312 in_decl);
21313
21314 if (ce->value == error_mark_node
21315 || PACK_EXPANSION_P (ce->value))
21316 ;
21317 else if (TREE_VEC_LENGTH (ce->value) == 1)
21318 /* Just move the argument into place. */
21319 ce->value = TREE_VEC_ELT (ce->value, 0);
21320 else
21321 {
21322 /* Update the length of the final CONSTRUCTOR
21323 arguments vector, and note that we will need to
21324 copy.*/
21325 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
21326 need_copy_p = true;
21327 }
21328 }
21329 else
21330 ce->value = RECUR (ce->value);
21331 }
21332
21333 if (need_copy_p)
21334 {
21335 vec<constructor_elt, va_gc> *old_n = n;
21336
21337 vec_alloc (v&: n, nelems: newlen);
21338 FOR_EACH_VEC_ELT (*old_n, idx, ce)
21339 {
21340 if (TREE_CODE (ce->value) == TREE_VEC)
21341 {
21342 int i, len = TREE_VEC_LENGTH (ce->value);
21343 for (i = 0; i < len; ++i)
21344 CONSTRUCTOR_APPEND_ELT (n, 0,
21345 TREE_VEC_ELT (ce->value, i));
21346 }
21347 else
21348 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
21349 }
21350 }
21351
21352 r = build_constructor (init_list_type_node, n);
21353 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
21354 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
21355 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
21356
21357 if (TREE_HAS_CONSTRUCTOR (t))
21358 {
21359 fcl_t cl = fcl_functional;
21360 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
21361 cl = fcl_c99;
21362 RETURN (finish_compound_literal (type, r, complain, cl));
21363 }
21364
21365 TREE_TYPE (r) = type;
21366 RETURN (r);
21367 }
21368
21369 case TYPEID_EXPR:
21370 {
21371 tree operand_0 = TREE_OPERAND (t, 0);
21372 if (TYPE_P (operand_0))
21373 {
21374 operand_0 = tsubst (t: operand_0, args, complain, in_decl);
21375 RETURN (get_typeid (operand_0, complain));
21376 }
21377 else
21378 {
21379 operand_0 = RECUR (operand_0);
21380 RETURN (build_typeid (operand_0, complain));
21381 }
21382 }
21383
21384 case FUNCTION_DECL:
21385 case PARM_DECL:
21386 case VAR_DECL:
21387 if (!args)
21388 RETURN (t);
21389 tree r;
21390 if (VAR_OR_FUNCTION_DECL_P (t)
21391 && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
21392 r = tsubst_decl (t, args, complain);
21393 else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
21394 {
21395 /* Local specialization will usually have been created when
21396 we instantiated the DECL_EXPR_DECL. */
21397 r = retrieve_local_specialization (tmpl: t);
21398 if (!r)
21399 {
21400 /* We're in a generic lambda referencing a local extern
21401 from an outer block-scope of a non-template. */
21402 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
21403 r = t;
21404 }
21405 }
21406 else if (local_variable_p (t)
21407 && ((r = retrieve_local_specialization (tmpl: t))
21408 || TREE_CODE (t) == PARM_DECL
21409 || uses_template_parms (DECL_CONTEXT (t))))
21410 {
21411 if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
21412 {
21413 /* We get here for a use of 'this' in an NSDMI. */
21414 if (DECL_NAME (t) == this_identifier && current_class_ptr)
21415 RETURN (current_class_ptr);
21416
21417 /* This can happen for a parameter name used later in a function
21418 declaration (such as in a late-specified return type). Just
21419 make a dummy decl, since it's only used for its type. */
21420 gcc_assert (cp_unevaluated_operand);
21421 r = tsubst_decl (t, args, complain);
21422 /* Give it the template pattern as its context; its true context
21423 hasn't been instantiated yet and this is good enough for
21424 mangling. */
21425 DECL_CONTEXT (r) = DECL_CONTEXT (t);
21426 }
21427 else if (r == NULL_TREE)
21428 {
21429 /* First try name lookup to find the instantiation. */
21430 r = lookup_name (DECL_NAME (t));
21431 if (r)
21432 {
21433 if (!VAR_P (r))
21434 {
21435 /* During error-recovery we may find a non-variable,
21436 even an OVERLOAD: just bail out and avoid ICEs and
21437 duplicate diagnostics (c++/62207). */
21438 gcc_assert (seen_error ());
21439 RETURN (error_mark_node);
21440 }
21441 if (!is_capture_proxy (r))
21442 {
21443 /* Make sure the one we found is the one we want. */
21444 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
21445 if (ctx != DECL_CONTEXT (r))
21446 r = NULL_TREE;
21447 }
21448 }
21449
21450 if (r)
21451 /* OK */;
21452 else
21453 {
21454 /* This can happen for a variable used in a
21455 late-specified return type of a local lambda, or for a
21456 local static or constant. Building a new VAR_DECL
21457 should be OK in all those cases. */
21458 r = tsubst_decl (t, args, complain);
21459 if (local_specializations)
21460 /* Avoid infinite recursion (79640). */
21461 register_local_specialization (spec: r, tmpl: t);
21462 if (decl_maybe_constant_var_p (r))
21463 {
21464 /* We can't call cp_finish_decl, so handle the
21465 initializer by hand. */
21466 tree init = tsubst_init (DECL_INITIAL (t), decl: r, args,
21467 complain, in_decl);
21468 if (!processing_template_decl)
21469 init = maybe_constant_init (init);
21470 if (processing_template_decl
21471 ? potential_constant_expression (init)
21472 : reduced_constant_expression_p (init))
21473 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
21474 = TREE_CONSTANT (r) = true;
21475 DECL_INITIAL (r) = init;
21476 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
21477 TREE_TYPE (r)
21478 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
21479 complain, adc_variable_type);
21480 }
21481 gcc_assert (cp_unevaluated_operand
21482 || processing_contract_condition
21483 || TREE_STATIC (r)
21484 || decl_constant_var_p (r)
21485 || seen_error ());
21486 if (!processing_template_decl
21487 && !TREE_STATIC (r))
21488 r = process_outer_var_ref (r, complain);
21489 }
21490 /* Remember this for subsequent uses. */
21491 if (local_specializations)
21492 register_local_specialization (spec: r, tmpl: t);
21493 }
21494 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
21495 r = argument_pack_select_arg (t: r);
21496 }
21497 else
21498 r = t;
21499 if (!mark_used (r, complain))
21500 RETURN (error_mark_node);
21501
21502 if (!no_name_lookup_flag
21503 && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
21504 {
21505 /* ??? We're doing a subset of finish_id_expression here. */
21506 if (tree wrap = maybe_get_tls_wrapper_call (r))
21507 /* Replace an evaluated use of the thread_local variable with
21508 a call to its wrapper. */
21509 r = wrap;
21510 else if (outer_automatic_var_p (r))
21511 r = process_outer_var_ref (r, complain);
21512
21513 if (!TYPE_REF_P (TREE_TYPE (t)))
21514 /* If the original type was a reference, we'll be wrapped in
21515 the appropriate INDIRECT_REF. */
21516 r = convert_from_reference (r);
21517 }
21518 RETURN (r);
21519
21520 case CONST_DECL:
21521 {
21522 tree enum_type;
21523 tree v;
21524
21525 if (DECL_TEMPLATE_PARM_P (t))
21526 RETURN (RECUR (DECL_INITIAL (t)));
21527 if (!uses_template_parms (DECL_CONTEXT (t)))
21528 RETURN (t);
21529
21530 /* Unfortunately, we cannot just call lookup_name here.
21531 Consider:
21532
21533 template <int I> int f() {
21534 enum E { a = I };
21535 struct S { void g() { E e = a; } };
21536 };
21537
21538 When we instantiate f<7>::S::g(), say, lookup_name is not
21539 clever enough to find f<7>::a. */
21540 enum_type
21541 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
21542 /*entering_scope=*/0);
21543
21544 for (v = TYPE_VALUES (enum_type);
21545 v != NULL_TREE;
21546 v = TREE_CHAIN (v))
21547 if (TREE_PURPOSE (v) == DECL_NAME (t))
21548 RETURN (TREE_VALUE (v));
21549
21550 /* We didn't find the name. That should never happen; if
21551 name-lookup found it during preliminary parsing, we
21552 should find it again here during instantiation. */
21553 gcc_unreachable ();
21554 RETURN (t);
21555 }
21556
21557 case FIELD_DECL:
21558 if (DECL_CONTEXT (t))
21559 {
21560 tree ctx;
21561
21562 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
21563 /*entering_scope=*/1);
21564 if (ctx != DECL_CONTEXT (t))
21565 {
21566 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
21567 if (!r)
21568 {
21569 if (complain & tf_error)
21570 error ("using invalid field %qD", t);
21571 RETURN (error_mark_node);
21572 }
21573 RETURN (r);
21574 }
21575 }
21576 RETURN (t);
21577
21578 case NAMESPACE_DECL:
21579 case OVERLOAD:
21580 RETURN (t);
21581
21582 case TEMPLATE_DECL:
21583 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
21584 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
21585 args, complain, in_decl));
21586 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
21587 RETURN (tsubst (t, args, complain, in_decl));
21588 else if (DECL_CLASS_SCOPE_P (t)
21589 && uses_template_parms (DECL_CONTEXT (t)))
21590 {
21591 /* Template template argument like the following example need
21592 special treatment:
21593
21594 template <template <class> class TT> struct C {};
21595 template <class T> struct D {
21596 template <class U> struct E {};
21597 C<E> c; // #1
21598 };
21599 D<int> d; // #2
21600
21601 We are processing the template argument `E' in #1 for
21602 the template instantiation #2. Originally, `E' is a
21603 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
21604 have to substitute this with one having context `D<int>'. */
21605
21606 tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
21607 in_decl, /*entering_scope=*/true);
21608 RETURN (lookup_field (context, DECL_NAME(t), 0, false));
21609 }
21610 else
21611 /* Ordinary template template argument. */
21612 RETURN (t);
21613
21614 case TEMPLATE_PARM_INDEX:
21615 case TYPE_DECL:
21616 RETURN (tsubst (t, args, complain, in_decl));
21617
21618 case CLEANUP_POINT_EXPR:
21619 /* We shouldn't have built any of these during initial template
21620 generation. Instead, they should be built during instantiation
21621 in response to the saved STMT_IS_FULL_EXPR_P setting. */
21622 gcc_unreachable ();
21623
21624 case OFFSET_REF:
21625 {
21626 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21627 tree op0 = RECUR (TREE_OPERAND (t, 0));
21628 tree op1 = RECUR (TREE_OPERAND (t, 1));
21629 r = build2 (OFFSET_REF, type, op0, op1);
21630 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
21631 if (!mark_used (TREE_OPERAND (r, 1), complain)
21632 && !(complain & tf_error))
21633 RETURN (error_mark_node);
21634 RETURN (r);
21635 }
21636
21637 case EXPR_PACK_EXPANSION:
21638 error ("invalid use of pack expansion expression");
21639 RETURN (error_mark_node);
21640
21641 case NONTYPE_ARGUMENT_PACK:
21642 error ("use %<...%> to expand argument pack");
21643 RETURN (error_mark_node);
21644
21645 case VOID_CST:
21646 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
21647 RETURN (t);
21648
21649 case INTEGER_CST:
21650 case REAL_CST:
21651 case COMPLEX_CST:
21652 case VECTOR_CST:
21653 {
21654 /* Instantiate any typedefs in the type. */
21655 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21656 r = fold_convert (type, t);
21657 gcc_assert (TREE_CODE (r) == TREE_CODE (t));
21658 RETURN (r);
21659 }
21660
21661 case STRING_CST:
21662 {
21663 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21664 r = t;
21665 if (type != TREE_TYPE (t))
21666 {
21667 r = copy_node (t);
21668 TREE_TYPE (r) = type;
21669 }
21670 RETURN (r);
21671 }
21672
21673 case PTRMEM_CST:
21674 /* These can sometimes show up in a partial instantiation, but never
21675 involve template parms. */
21676 gcc_assert (!uses_template_parms (t));
21677 RETURN (t);
21678
21679 case UNARY_LEFT_FOLD_EXPR:
21680 RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
21681 case UNARY_RIGHT_FOLD_EXPR:
21682 RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
21683 case BINARY_LEFT_FOLD_EXPR:
21684 RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
21685 case BINARY_RIGHT_FOLD_EXPR:
21686 RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
21687 case PREDICT_EXPR:
21688 RETURN (t);
21689
21690 case DEBUG_BEGIN_STMT:
21691 /* ??? There's no point in copying it for now, but maybe some
21692 day it will contain more information, such as a pointer back
21693 to the containing function, inlined copy or so. */
21694 RETURN (t);
21695
21696 case CO_YIELD_EXPR:
21697 RETURN (finish_co_yield_expr (input_location,
21698 RECUR (TREE_OPERAND (t, 0))));
21699
21700 case CO_AWAIT_EXPR:
21701 RETURN (finish_co_await_expr (input_location,
21702 RECUR (TREE_OPERAND (t, 0))));
21703
21704 case VA_ARG_EXPR:
21705 {
21706 tree op0 = RECUR (TREE_OPERAND (t, 0));
21707 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21708 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
21709 }
21710
21711 case OFFSETOF_EXPR:
21712 {
21713 tree object_ptr
21714 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
21715 RETURN (finish_offsetof (object_ptr,
21716 RECUR (TREE_OPERAND (t, 0)),
21717 EXPR_LOCATION (t)));
21718 }
21719
21720 case ADDRESSOF_EXPR:
21721 RETURN (cp_build_addressof (EXPR_LOCATION (t),
21722 RECUR (TREE_OPERAND (t, 0)), complain));
21723
21724 case TRAIT_EXPR:
21725 {
21726 tree type1 = TRAIT_EXPR_TYPE1 (t);
21727 if (TYPE_P (type1))
21728 type1 = tsubst (t: type1, args, complain, in_decl);
21729 else
21730 type1 = tsubst_expr (t: type1, args, complain, in_decl);
21731 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
21732 complain, in_decl);
21733 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
21734 TRAIT_EXPR_KIND (t), type1, type2));
21735 }
21736
21737 case STMT_EXPR:
21738 {
21739 tree old_stmt_expr = cur_stmt_expr;
21740 tree stmt_expr = begin_stmt_expr ();
21741
21742 cur_stmt_expr = stmt_expr;
21743 tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
21744 stmt_expr = finish_stmt_expr (stmt_expr, false);
21745 cur_stmt_expr = old_stmt_expr;
21746
21747 /* If the resulting list of expression statement is empty,
21748 fold it further into void_node. */
21749 if (empty_expr_stmt_p (stmt_expr))
21750 stmt_expr = void_node;
21751
21752 RETURN (stmt_expr);
21753 }
21754
21755 case LAMBDA_EXPR:
21756 {
21757 if (complain & tf_partial)
21758 {
21759 /* We don't have a full set of template arguments yet; don't touch
21760 the lambda at all. */
21761 gcc_assert (processing_template_decl);
21762 return t;
21763 }
21764 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
21765
21766 RETURN (build_lambda_object (r));
21767 }
21768
21769 case TRANSACTION_EXPR:
21770 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
21771 RETURN (tsubst_stmt (t, args, complain, in_decl));
21772
21773 case PAREN_EXPR:
21774 if (REF_PARENTHESIZED_P (t))
21775 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
21776 else
21777 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21778 {
21779 tree op0 = RECUR (TREE_OPERAND (t, 0));
21780 RETURN (build1_loc (input_location, PAREN_EXPR,
21781 TREE_TYPE (op0), op0));
21782 }
21783
21784 case VEC_PERM_EXPR:
21785 {
21786 tree op0 = RECUR (TREE_OPERAND (t, 0));
21787 tree op1 = RECUR (TREE_OPERAND (t, 1));
21788 tree op2 = RECUR (TREE_OPERAND (t, 2));
21789 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
21790 complain));
21791 }
21792
21793 case REQUIRES_EXPR:
21794 {
21795 complain &= ~tf_warning_or_error;
21796 tree r = tsubst_requires_expr (t, args, complain, in_decl);
21797 RETURN (r);
21798 }
21799
21800 case RANGE_EXPR:
21801 /* No need to substitute further, a RANGE_EXPR will always be built
21802 with constant operands. */
21803 RETURN (t);
21804
21805 case NON_LVALUE_EXPR:
21806 case VIEW_CONVERT_EXPR:
21807 {
21808 tree op = RECUR (TREE_OPERAND (t, 0));
21809
21810 if (location_wrapper_p (exp: t))
21811 /* We need to do this here as well as in tsubst_copy so we get the
21812 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21813 RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
21814
21815 gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
21816 if (REF_PARENTHESIZED_P (t))
21817 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21818 RETURN (finish_parenthesized_expr (op));
21819
21820 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21821 parameter object const. */
21822 if (TREE_TYPE (op) == NULL_TREE
21823 || !CP_TYPE_CONST_P (TREE_TYPE (op)))
21824 {
21825 /* The template argument is not const, presumably because
21826 it is still dependent, and so not the const template parm
21827 object. */
21828 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21829 if (TREE_CODE (op) == CONSTRUCTOR
21830 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
21831 {
21832 /* Don't add a wrapper to these. */
21833 op = copy_node (op);
21834 TREE_TYPE (op) = type;
21835 }
21836 else
21837 /* Do add a wrapper otherwise (in particular, if op is
21838 another TEMPLATE_PARM_INDEX). */
21839 op = build1 (VIEW_CONVERT_EXPR, type, op);
21840 }
21841 RETURN (op);
21842 }
21843
21844 default:
21845 /* Handle Objective-C++ constructs, if appropriate. */
21846 if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
21847 RETURN (subst);
21848
21849 /* We shouldn't get here, but keep going if !flag_checking. */
21850 if (flag_checking)
21851 gcc_unreachable ();
21852 RETURN (t);
21853 }
21854
21855#undef RECUR
21856#undef RETURN
21857 out:
21858 input_location = save_loc;
21859 return retval;
21860}
21861
21862/* Verify that the instantiated ARGS are valid. For type arguments,
21863 make sure that the type's linkage is ok. For non-type arguments,
21864 make sure they are constants if they are integral or enumerations.
21865 Emit an error under control of COMPLAIN, and return TRUE on error. */
21866
21867static bool
21868check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
21869{
21870 if (dependent_template_arg_p (t))
21871 return false;
21872 if (ARGUMENT_PACK_P (t))
21873 {
21874 tree vec = ARGUMENT_PACK_ARGS (t);
21875 int len = TREE_VEC_LENGTH (vec);
21876 bool result = false;
21877 int i;
21878
21879 for (i = 0; i < len; ++i)
21880 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
21881 result = true;
21882 return result;
21883 }
21884 else if (TYPE_P (t))
21885 {
21886 /* [basic.link]: A name with no linkage (notably, the name
21887 of a class or enumeration declared in a local scope)
21888 shall not be used to declare an entity with linkage.
21889 This implies that names with no linkage cannot be used as
21890 template arguments
21891
21892 DR 757 relaxes this restriction for C++0x. */
21893 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
21894 : no_linkage_check (t, /*relaxed_p=*/false));
21895
21896 if (nt)
21897 {
21898 /* DR 488 makes use of a type with no linkage cause
21899 type deduction to fail. */
21900 if (complain & tf_error)
21901 {
21902 if (TYPE_UNNAMED_P (nt))
21903 error ("%qT is/uses unnamed type", t);
21904 else
21905 error ("template argument for %qD uses local type %qT",
21906 tmpl, t);
21907 }
21908 return true;
21909 }
21910 /* In order to avoid all sorts of complications, we do not
21911 allow variably-modified types as template arguments. */
21912 else if (variably_modified_type_p (t, NULL_TREE))
21913 {
21914 if (complain & tf_error)
21915 error ("%qT is a variably modified type", t);
21916 return true;
21917 }
21918 }
21919 /* Class template and alias template arguments should be OK. */
21920 else if (DECL_TYPE_TEMPLATE_P (t))
21921 ;
21922 /* A non-type argument of integral or enumerated type must be a
21923 constant. */
21924 else if (TREE_TYPE (t)
21925 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
21926 && !REFERENCE_REF_P (t)
21927 && !TREE_CONSTANT (t))
21928 {
21929 if (complain & tf_error)
21930 error ("integral expression %qE is not constant", t);
21931 return true;
21932 }
21933 return false;
21934}
21935
21936static bool
21937check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
21938{
21939 int ix, len = DECL_NTPARMS (tmpl);
21940 bool result = false;
21941
21942 for (ix = 0; ix != len; ix++)
21943 {
21944 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
21945 result = true;
21946 }
21947 if (result && (complain & tf_error))
21948 error (" trying to instantiate %qD", tmpl);
21949 return result;
21950}
21951
21952/* Call mark_used on each entity within the non-type template arguments in
21953 ARGS for an instantiation of TMPL, to ensure that each such entity is
21954 considered odr-used (and therefore marked for instantiation) regardless of
21955 whether the specialization was first formed in a template context (which
21956 inhibits mark_used).
21957
21958 This function assumes push_to_top_level has been called beforehand. */
21959
21960static void
21961mark_template_arguments_used (tree tmpl, tree args)
21962{
21963 /* It suffices to do this only when instantiating a primary template. */
21964 if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
21965 return;
21966
21967 /* We already marked outer arguments when specializing the context. */
21968 args = INNERMOST_TEMPLATE_ARGS (args);
21969
21970 for (tree arg : tree_vec_range (args))
21971 {
21972 /* A (pointer/reference to) function or variable NTTP argument. */
21973 if (TREE_CODE (arg) == ADDR_EXPR
21974 || TREE_CODE (arg) == INDIRECT_REF)
21975 {
21976 while (TREE_CODE (arg) == ADDR_EXPR
21977 || REFERENCE_REF_P (arg)
21978 || CONVERT_EXPR_P (arg))
21979 arg = TREE_OPERAND (arg, 0);
21980 if (VAR_OR_FUNCTION_DECL_P (arg))
21981 {
21982 /* Pass tf_none to avoid duplicate diagnostics: if this call
21983 fails then an earlier call to mark_used for this argument
21984 must have also failed and emitted a diagnostic. */
21985 bool ok = mark_used (arg, tf_none);
21986 gcc_checking_assert (ok || seen_error ());
21987 }
21988 }
21989 /* A class NTTP argument. */
21990 else if (VAR_P (arg)
21991 && DECL_NTTP_OBJECT_P (arg))
21992 {
21993 auto mark_used_r = [](tree *tp, int *, void *) {
21994 if (VAR_OR_FUNCTION_DECL_P (*tp))
21995 {
21996 bool ok = mark_used (*tp, tf_none);
21997 gcc_checking_assert (ok || seen_error ());
21998 }
21999 return NULL_TREE;
22000 };
22001 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
22002 mark_used_r, nullptr);
22003 }
22004 }
22005}
22006
22007/* We're out of SFINAE context now, so generate diagnostics for the access
22008 errors we saw earlier when instantiating D from TMPL and ARGS. */
22009
22010static void
22011recheck_decl_substitution (tree d, tree tmpl, tree args)
22012{
22013 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
22014 tree type = TREE_TYPE (pattern);
22015 location_t loc = input_location;
22016
22017 push_access_scope (t: d);
22018 push_deferring_access_checks (dk_no_deferred);
22019 input_location = DECL_SOURCE_LOCATION (pattern);
22020 tsubst (t: type, args, complain: tf_warning_or_error, in_decl: d);
22021 input_location = loc;
22022 pop_deferring_access_checks ();
22023 pop_access_scope (t: d);
22024}
22025
22026/* Instantiate the indicated variable, function, or alias template TMPL with
22027 the template arguments in TARG_PTR. */
22028
22029tree
22030instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
22031{
22032 auto_timevar tv (TV_TEMPLATE_INST);
22033
22034 tree targ_ptr = orig_args;
22035 tree fndecl;
22036 tree gen_tmpl;
22037 bool access_ok = true;
22038
22039 if (tmpl == error_mark_node)
22040 return error_mark_node;
22041
22042 /* The other flags are not relevant anymore here, especially tf_partial
22043 shouldn't be set. For instance, we may be called while doing a partial
22044 substitution of a template variable, but the type of the variable
22045 template may be auto, in which case we will call do_auto_deduction
22046 in mark_used (which clears tf_partial) and the auto must be properly
22047 reduced at that time for the deduction to work. */
22048 complain &= tf_warning_or_error;
22049
22050 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
22051
22052 if (modules_p ())
22053 lazy_load_pendings (decl: tmpl);
22054
22055 /* If this function is a clone, handle it specially. */
22056 if (DECL_CLONED_FUNCTION_P (tmpl))
22057 {
22058 tree spec;
22059 tree clone;
22060
22061 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
22062 DECL_CLONED_FUNCTION. */
22063 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
22064 orig_args: targ_ptr, complain);
22065 if (spec == error_mark_node)
22066 return error_mark_node;
22067
22068 /* Look for the clone. */
22069 FOR_EACH_CLONE (clone, spec)
22070 if (DECL_NAME (clone) == DECL_NAME (tmpl))
22071 return clone;
22072 /* We should always have found the clone by now. */
22073 gcc_unreachable ();
22074 return NULL_TREE;
22075 }
22076
22077 if (targ_ptr == error_mark_node)
22078 return error_mark_node;
22079
22080 /* Check to see if we already have this specialization. */
22081 gen_tmpl = most_general_template (tmpl);
22082 if (TMPL_ARGS_DEPTH (targ_ptr)
22083 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
22084 /* targ_ptr only has the innermost template args, so add the outer ones
22085 from tmpl, which could be either a partial instantiation or gen_tmpl (in
22086 the case of a non-dependent call within a template definition). */
22087 targ_ptr = (add_outermost_template_args
22088 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
22089 extra_args: targ_ptr));
22090
22091 hashval_t hash = spec_hasher::hash (tmpl: gen_tmpl, args: targ_ptr);
22092 tree spec = retrieve_specialization (tmpl: gen_tmpl, args: targ_ptr, hash);
22093
22094 gcc_checking_assert (tmpl == gen_tmpl
22095 || ((fndecl
22096 = retrieve_specialization (tmpl, orig_args, 0))
22097 == spec)
22098 || fndecl == NULL_TREE);
22099
22100 if (spec != NULL_TREE)
22101 {
22102 if (FNDECL_HAS_ACCESS_ERRORS (spec))
22103 {
22104 if (complain & tf_error)
22105 recheck_decl_substitution (d: spec, tmpl: gen_tmpl, args: targ_ptr);
22106 return error_mark_node;
22107 }
22108 return spec;
22109 }
22110
22111 if (check_instantiated_args (tmpl: gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
22112 complain))
22113 return error_mark_node;
22114
22115 /* We are building a FUNCTION_DECL, during which the access of its
22116 parameters and return types have to be checked. However this
22117 FUNCTION_DECL which is the desired context for access checking
22118 is not built yet. We solve this chicken-and-egg problem by
22119 deferring all checks until we have the FUNCTION_DECL. */
22120 push_deferring_access_checks (dk_deferred);
22121
22122 /* Instantiation of the function happens in the context of the function
22123 template, not the context of the overload resolution we're doing. */
22124 push_to_top_level ();
22125 /* If there are dependent arguments, e.g. because we're doing partial
22126 ordering, make sure processing_template_decl stays set. */
22127 if (uses_template_parms (t: targ_ptr))
22128 ++processing_template_decl;
22129 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22130 {
22131 tree ctx;
22132 if (!uses_template_parms (DECL_CONTEXT (tmpl)))
22133 /* If the context of the partially instantiated template is
22134 already non-dependent, then we might as well use it. */
22135 ctx = DECL_CONTEXT (tmpl);
22136 else
22137 ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), args: targ_ptr,
22138 complain, in_decl: gen_tmpl, entering_scope: true);
22139 push_nested_class (ctx);
22140 }
22141
22142 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
22143
22144 tree partial_ti = NULL_TREE;
22145 fndecl = NULL_TREE;
22146 if (VAR_P (pattern))
22147 {
22148 /* We need to determine if we're using a partial or explicit
22149 specialization now, because the type of the variable could be
22150 different. */
22151 tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
22152 partial_ti = most_specialized_partial_spec (tid, complain);
22153 if (partial_ti == error_mark_node)
22154 pattern = error_mark_node;
22155 else if (partial_ti)
22156 {
22157 tree partial_tmpl = TI_TEMPLATE (partial_ti);
22158 tree partial_args = TI_ARGS (partial_ti);
22159 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
22160 fndecl = tsubst_decl (t: partial_pat, args: partial_args, complain,
22161 /*use_spec_table=*/false);
22162 }
22163 }
22164
22165 /* Substitute template parameters to obtain the specialization. */
22166 if (fndecl == NULL_TREE)
22167 fndecl = tsubst_decl (t: pattern, args: targ_ptr, complain, /*use_spec_table=*/false);
22168 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22169 pop_nested_class ();
22170 pop_from_top_level ();
22171
22172 if (fndecl == error_mark_node)
22173 {
22174 pop_deferring_access_checks ();
22175 return error_mark_node;
22176 }
22177
22178 /* The DECL_TI_TEMPLATE should always be the immediate parent
22179 template, not the most general template. */
22180 DECL_TI_TEMPLATE (fndecl) = tmpl;
22181 DECL_TI_ARGS (fndecl) = targ_ptr;
22182 if (VAR_P (pattern))
22183 {
22184 /* Now that we we've formed this variable template specialization,
22185 remember the result of most_specialized_partial_spec for it. */
22186 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
22187
22188 /* And remember if the variable was declared with []. */
22189 if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
22190 && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
22191 SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
22192 }
22193
22194 fndecl = register_specialization (spec: fndecl, tmpl: gen_tmpl, args: targ_ptr, is_friend: false, hash);
22195 if (fndecl == error_mark_node)
22196 return error_mark_node;
22197
22198 set_instantiating_module (fndecl);
22199
22200 /* Now we know the specialization, compute access previously
22201 deferred. Do no access control for inheriting constructors,
22202 as we already checked access for the inherited constructor. */
22203 if (!(flag_new_inheriting_ctors
22204 && DECL_INHERITED_CTOR (fndecl)))
22205 {
22206 push_access_scope (t: fndecl);
22207 if (!perform_deferred_access_checks (complain))
22208 access_ok = false;
22209 pop_access_scope (t: fndecl);
22210 }
22211 pop_deferring_access_checks ();
22212
22213 /* If we've just instantiated the main entry point for a function,
22214 instantiate all the alternate entry points as well. We do this
22215 by cloning the instantiation of the main entry point, not by
22216 instantiating the template clones. */
22217 if (tree chain = DECL_CHAIN (gen_tmpl))
22218 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
22219 clone_cdtor (fndecl, /*update_methods=*/false);
22220
22221 if (!access_ok)
22222 {
22223 if (!(complain & tf_error))
22224 {
22225 /* Remember to reinstantiate when we're out of SFINAE so the user
22226 can see the errors. */
22227 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
22228 }
22229 return error_mark_node;
22230 }
22231
22232 return fndecl;
22233}
22234
22235/* Instantiate the alias template TMPL with ARGS. Also push a template
22236 instantiation level, which instantiate_template doesn't do because
22237 functions and variables have sufficient context established by the
22238 callers. */
22239
22240static tree
22241instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
22242{
22243 if (tmpl == error_mark_node || args == error_mark_node)
22244 return error_mark_node;
22245
22246 args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
22247 args, in_decl: tmpl, complain);
22248 if (args == error_mark_node)
22249 return error_mark_node;
22250
22251 /* FIXME check for satisfaction in check_instantiated_args. */
22252 if (!constraints_satisfied_p (tmpl, args))
22253 {
22254 if (complain & tf_error)
22255 {
22256 auto_diagnostic_group d;
22257 error ("template constraint failure for %qD", tmpl);
22258 diagnose_constraints (input_location, tmpl, args);
22259 }
22260 return error_mark_node;
22261 }
22262
22263 if (!push_tinst_level (tmpl, args))
22264 return error_mark_node;
22265 tree r = instantiate_template (tmpl, orig_args: args, complain);
22266 pop_tinst_level ();
22267
22268 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), transparent_typedefs: nt_opaque))
22269 {
22270 /* An alias template specialization can be dependent
22271 even if its underlying type is not. */
22272 TYPE_DEPENDENT_P (d) = true;
22273 TYPE_DEPENDENT_P_VALID (d) = true;
22274 /* Sometimes a dependent alias spec is equivalent to its expansion,
22275 sometimes not. So always use structural_comptypes. */
22276 SET_TYPE_STRUCTURAL_EQUALITY (d);
22277 }
22278
22279 return r;
22280}
22281
22282/* PARM is a template parameter pack for FN. Returns true iff
22283 PARM is used in a deducible way in the argument list of FN. */
22284
22285static bool
22286pack_deducible_p (tree parm, tree fn)
22287{
22288 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
22289 for (; t; t = TREE_CHAIN (t))
22290 {
22291 tree type = TREE_VALUE (t);
22292 tree packs;
22293 if (!PACK_EXPANSION_P (type))
22294 continue;
22295 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
22296 packs; packs = TREE_CHAIN (packs))
22297 if (template_args_equal (TREE_VALUE (packs), nt: parm))
22298 {
22299 /* The template parameter pack is used in a function parameter
22300 pack. If this is the end of the parameter list, the
22301 template parameter pack is deducible. */
22302 if (TREE_CHAIN (t) == void_list_node)
22303 return true;
22304 else
22305 /* Otherwise, not. Well, it could be deduced from
22306 a non-pack parameter, but doing so would end up with
22307 a deduction mismatch, so don't bother. */
22308 return false;
22309 }
22310 }
22311 /* The template parameter pack isn't used in any function parameter
22312 packs, but it might be used deeper, e.g. tuple<Args...>. */
22313 return true;
22314}
22315
22316/* Subroutine of fn_type_unification: check non-dependent parms for
22317 convertibility. */
22318
22319static int
22320check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
22321 tree fn, unification_kind_t strict, int flags,
22322 struct conversion **convs, bool explain_p,
22323 bool noninst_only_p)
22324{
22325 /* Non-constructor methods need to leave a conversion for 'this', which
22326 isn't included in nargs here. */
22327 unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
22328 && !DECL_CONSTRUCTOR_P (fn));
22329
22330 for (unsigned ia = 0;
22331 parms && parms != void_list_node && ia < nargs; )
22332 {
22333 tree parm = TREE_VALUE (parms);
22334
22335 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22336 && (!TREE_CHAIN (parms)
22337 || TREE_CHAIN (parms) == void_list_node))
22338 /* For a function parameter pack that occurs at the end of the
22339 parameter-declaration-list, the type A of each remaining
22340 argument of the call is compared with the type P of the
22341 declarator-id of the function parameter pack. */
22342 break;
22343
22344 parms = TREE_CHAIN (parms);
22345
22346 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22347 /* For a function parameter pack that does not occur at the
22348 end of the parameter-declaration-list, the type of the
22349 parameter pack is a non-deduced context. */
22350 continue;
22351
22352 if (!uses_template_parms (t: parm))
22353 {
22354 tree arg = args[ia];
22355 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
22356 int lflags = conv_flags (ia, nargs, fn, arg, flags);
22357
22358 if (check_non_deducible_conversion (parm, arg, strict, lflags,
22359 conv_p, explain_p, noninst_only_p))
22360 return 1;
22361 }
22362
22363 ++ia;
22364 }
22365
22366 return 0;
22367}
22368
22369/* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22370 NARGS elements of the arguments that are being used when calling
22371 it. TARGS is a vector into which the deduced template arguments
22372 are placed.
22373
22374 Returns either a FUNCTION_DECL for the matching specialization of FN or
22375 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22376 true, diagnostics will be printed to explain why it failed.
22377
22378 If FN is a conversion operator, or we are trying to produce a specific
22379 specialization, RETURN_TYPE is the return type desired.
22380
22381 The EXPLICIT_TARGS are explicit template arguments provided via a
22382 template-id.
22383
22384 The parameter STRICT is one of:
22385
22386 DEDUCE_CALL:
22387 We are deducing arguments for a function call, as in
22388 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22389 deducing arguments for a call to the result of a conversion
22390 function template, as in [over.call.object].
22391
22392 DEDUCE_CONV:
22393 We are deducing arguments for a conversion function, as in
22394 [temp.deduct.conv].
22395
22396 DEDUCE_EXACT:
22397 We are deducing arguments when doing an explicit instantiation
22398 as in [temp.explicit], when determining an explicit specialization
22399 as in [temp.expl.spec], or when taking the address of a function
22400 template, as in [temp.deduct.funcaddr]. */
22401
22402tree
22403fn_type_unification (tree fn,
22404 tree explicit_targs,
22405 tree targs,
22406 const tree *args,
22407 unsigned int nargs,
22408 tree return_type,
22409 unification_kind_t strict,
22410 int flags,
22411 struct conversion **convs,
22412 bool explain_p,
22413 bool decltype_p)
22414{
22415 tree parms;
22416 tree fntype;
22417 tree decl = NULL_TREE;
22418 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22419 bool ok;
22420 static int deduction_depth;
22421 /* type_unification_real will pass back any access checks from default
22422 template argument substitution. */
22423 vec<deferred_access_check, va_gc> *checks = NULL;
22424 /* We don't have all the template args yet. */
22425 bool incomplete = true;
22426
22427 tree orig_fn = fn;
22428 if (flag_new_inheriting_ctors)
22429 fn = strip_inheriting_ctors (fn);
22430
22431 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
22432 tree r = error_mark_node;
22433
22434 tree full_targs = targs;
22435 if (TMPL_ARGS_DEPTH (targs)
22436 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
22437 full_targs = (add_outermost_template_args
22438 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
22439 extra_args: targs));
22440
22441 if (decltype_p)
22442 complain |= tf_decltype;
22443
22444 /* In C++0x, it's possible to have a function template whose type depends
22445 on itself recursively. This is most obvious with decltype, but can also
22446 occur with enumeration scope (c++/48969). So we need to catch infinite
22447 recursion and reject the substitution at deduction time; this function
22448 will return error_mark_node for any repeated substitution.
22449
22450 This also catches excessive recursion such as when f<N> depends on
22451 f<N-1> across all integers, and returns error_mark_node for all the
22452 substitutions back up to the initial one.
22453
22454 This is, of course, not reentrant. */
22455 if (excessive_deduction_depth)
22456 return error_mark_node;
22457 ++deduction_depth;
22458
22459 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
22460
22461 fntype = TREE_TYPE (fn);
22462 if (explicit_targs)
22463 {
22464 /* [temp.deduct]
22465
22466 The specified template arguments must match the template
22467 parameters in kind (i.e., type, nontype, template), and there
22468 must not be more arguments than there are parameters;
22469 otherwise type deduction fails.
22470
22471 Nontype arguments must match the types of the corresponding
22472 nontype template parameters, or must be convertible to the
22473 types of the corresponding nontype parameters as specified in
22474 _temp.arg.nontype_, otherwise type deduction fails.
22475
22476 All references in the function type of the function template
22477 to the corresponding template parameters are replaced by the
22478 specified template argument values. If a substitution in a
22479 template parameter or in the function type of the function
22480 template results in an invalid type, type deduction fails. */
22481 int i, len = TREE_VEC_LENGTH (tparms);
22482 location_t loc = input_location;
22483 incomplete = false;
22484
22485 if (explicit_targs == error_mark_node)
22486 goto fail;
22487
22488 if (TMPL_ARGS_DEPTH (explicit_targs)
22489 < TMPL_ARGS_DEPTH (full_targs))
22490 explicit_targs = add_outermost_template_args (args: full_targs,
22491 extra_args: explicit_targs);
22492
22493 /* Adjust any explicit template arguments before entering the
22494 substitution context. */
22495 explicit_targs
22496 = (coerce_template_parms (parms: tparms, args: explicit_targs, in_decl: fn,
22497 complain: complain|tf_partial,
22498 /*require_all_args=*/false));
22499 if (explicit_targs == error_mark_node)
22500 goto fail;
22501
22502 /* Substitute the explicit args into the function type. This is
22503 necessary so that, for instance, explicitly declared function
22504 arguments can match null pointed constants. If we were given
22505 an incomplete set of explicit args, we must not do semantic
22506 processing during substitution as we could create partial
22507 instantiations. */
22508 for (i = 0; i < len; i++)
22509 {
22510 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
22511 bool parameter_pack = false;
22512 tree targ = TREE_VEC_ELT (explicit_targs, i);
22513
22514 /* Dig out the actual parm. */
22515 if (TREE_CODE (parm) == TYPE_DECL
22516 || TREE_CODE (parm) == TEMPLATE_DECL)
22517 {
22518 parm = TREE_TYPE (parm);
22519 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
22520 }
22521 else if (TREE_CODE (parm) == PARM_DECL)
22522 {
22523 parm = DECL_INITIAL (parm);
22524 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
22525 }
22526
22527 if (targ == NULL_TREE)
22528 /* No explicit argument for this template parameter. */
22529 incomplete = true;
22530 else if (parameter_pack && pack_deducible_p (parm, fn))
22531 {
22532 /* Mark the argument pack as "incomplete". We could
22533 still deduce more arguments during unification.
22534 We remove this mark in type_unification_real. */
22535 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
22536 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
22537 = ARGUMENT_PACK_ARGS (targ);
22538
22539 /* We have some incomplete argument packs. */
22540 incomplete = true;
22541 }
22542 }
22543
22544 if (incomplete)
22545 {
22546 if (!push_tinst_level (tmpl: fn, args: explicit_targs))
22547 {
22548 excessive_deduction_depth = true;
22549 goto fail;
22550 }
22551 ++processing_template_decl;
22552 input_location = DECL_SOURCE_LOCATION (fn);
22553 /* Ignore any access checks; we'll see them again in
22554 instantiate_template and they might have the wrong
22555 access path at this point. */
22556 push_deferring_access_checks (dk_deferred);
22557 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
22558 fntype = tsubst (TREE_TYPE (fn), args: explicit_targs, complain: ecomplain, NULL_TREE);
22559 pop_deferring_access_checks ();
22560 input_location = loc;
22561 --processing_template_decl;
22562 pop_tinst_level ();
22563
22564 if (fntype == error_mark_node)
22565 goto fail;
22566 }
22567
22568 /* Place the explicitly specified arguments in TARGS. */
22569 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
22570 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
22571 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
22572 if (!incomplete && CHECKING_P
22573 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22574 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22575 (targs, NUM_TMPL_ARGS (explicit_targs));
22576 }
22577
22578 if (return_type && strict != DEDUCE_CALL)
22579 {
22580 tree *new_args = XALLOCAVEC (tree, nargs + 1);
22581 new_args[0] = return_type;
22582 memcpy (dest: new_args + 1, src: args, n: nargs * sizeof (tree));
22583 args = new_args;
22584 ++nargs;
22585 }
22586
22587 if (!incomplete)
22588 goto deduced;
22589
22590 /* Never do unification on the 'this' parameter. */
22591 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
22592
22593 if (return_type && strict == DEDUCE_CALL)
22594 {
22595 /* We're deducing for a call to the result of a template conversion
22596 function. The parms we really want are in return_type. */
22597 if (INDIRECT_TYPE_P (return_type))
22598 return_type = TREE_TYPE (return_type);
22599 parms = TYPE_ARG_TYPES (return_type);
22600 }
22601 else if (return_type)
22602 {
22603 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
22604 }
22605
22606 /* We allow incomplete unification without an error message here
22607 because the standard doesn't seem to explicitly prohibit it. Our
22608 callers must be ready to deal with unification failures in any
22609 event. */
22610
22611 /* If we aren't explaining yet, push tinst context so we can see where
22612 any errors (e.g. from class instantiations triggered by instantiation
22613 of default template arguments) come from. If we are explaining, this
22614 context is redundant. */
22615 if (!explain_p && !push_tinst_level (tmpl: fn, args: targs))
22616 {
22617 excessive_deduction_depth = true;
22618 goto fail;
22619 }
22620
22621 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22622 full_targs, parms, args, nargs, /*subr=*/0,
22623 strict, &checks, explain_p);
22624 if (!explain_p)
22625 pop_tinst_level ();
22626 if (!ok)
22627 goto fail;
22628
22629 /* Now that we have bindings for all of the template arguments,
22630 ensure that the arguments deduced for the template template
22631 parameters have compatible template parameter lists. We cannot
22632 check this property before we have deduced all template
22633 arguments, because the template parameter types of a template
22634 template parameter might depend on prior template parameters
22635 deduced after the template template parameter. The following
22636 ill-formed example illustrates this issue:
22637
22638 template<typename T, template<T> class C> void f(C<5>, T);
22639
22640 template<int N> struct X {};
22641
22642 void g() {
22643 f(X<5>(), 5l); // error: template argument deduction fails
22644 }
22645
22646 The template parameter list of 'C' depends on the template type
22647 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22648 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22649 time that we deduce 'C'. */
22650 if (!template_template_parm_bindings_ok_p
22651 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
22652 {
22653 unify_inconsistent_template_template_parameters (explain_p);
22654 goto fail;
22655 }
22656
22657 deduced:
22658
22659 /* As a refinement of CWG2369, check first and foremost non-dependent
22660 conversions that we know are not going to induce template instantiation
22661 (PR99599). */
22662 if (strict == DEDUCE_CALL
22663 && incomplete
22664 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22665 convs, explain_p,
22666 /*noninst_only_p=*/true))
22667 goto fail;
22668
22669 /* CWG2369: Check satisfaction before non-deducible conversions. */
22670 if (!constraints_satisfied_p (fn, targs))
22671 {
22672 if (explain_p)
22673 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
22674 goto fail;
22675 }
22676
22677 /* DR 1391: All parameters have args, now check non-dependent parms for
22678 convertibility. We don't do this if all args were explicitly specified,
22679 as the standard says that we substitute explicit args immediately. */
22680 if (incomplete
22681 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22682 convs, explain_p,
22683 /*noninst_only_p=*/false))
22684 goto fail;
22685
22686 /* All is well so far. Now, check:
22687
22688 [temp.deduct]
22689
22690 When all template arguments have been deduced, all uses of
22691 template parameters in nondeduced contexts are replaced with
22692 the corresponding deduced argument values. If the
22693 substitution results in an invalid type, as described above,
22694 type deduction fails. */
22695 if (!push_tinst_level (tmpl: fn, args: targs))
22696 {
22697 excessive_deduction_depth = true;
22698 goto fail;
22699 }
22700
22701 /* Also collect access checks from the instantiation. */
22702 reopen_deferring_access_checks (checks);
22703
22704 decl = instantiate_template (tmpl: fn, orig_args: targs, complain);
22705
22706 checks = get_deferred_access_checks ();
22707 pop_deferring_access_checks ();
22708
22709 pop_tinst_level ();
22710
22711 if (decl == error_mark_node)
22712 goto fail;
22713
22714 /* Now perform any access checks encountered during substitution. */
22715 push_access_scope (t: decl);
22716 ok = perform_access_checks (checks, complain);
22717 pop_access_scope (t: decl);
22718 if (!ok)
22719 goto fail;
22720
22721 /* If we're looking for an exact match, check that what we got
22722 is indeed an exact match. It might not be if some template
22723 parameters are used in non-deduced contexts. But don't check
22724 for an exact match if we have dependent template arguments;
22725 in that case we're doing partial ordering, and we already know
22726 that we have two candidates that will provide the actual type. */
22727 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
22728 {
22729 tree substed = TREE_TYPE (decl);
22730 unsigned int i;
22731
22732 tree sarg
22733 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
22734 if (return_type)
22735 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
22736 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
22737 if (!same_type_p (args[i], TREE_VALUE (sarg)))
22738 {
22739 unify_type_mismatch (explain_p, parm: args[i],
22740 TREE_VALUE (sarg));
22741 goto fail;
22742 }
22743 if ((i < nargs || sarg)
22744 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22745 doesn't contain the trailing void, and conv fns are always (). */
22746 && !DECL_CONV_FN_P (decl))
22747 {
22748 unsigned nsargs = i + list_length (sarg);
22749 unify_arity (explain_p, have: nargs, wanted: nsargs);
22750 goto fail;
22751 }
22752 }
22753
22754 /* After doing deduction with the inherited constructor, actually return an
22755 instantiation of the inheriting constructor. */
22756 if (orig_fn != fn)
22757 decl = instantiate_template (tmpl: orig_fn, orig_args: targs, complain);
22758
22759 r = decl;
22760
22761 fail:
22762 --deduction_depth;
22763 if (excessive_deduction_depth)
22764 {
22765 if (deduction_depth == 0)
22766 /* Reset once we're all the way out. */
22767 excessive_deduction_depth = false;
22768 }
22769
22770 return r;
22771}
22772
22773/* Returns true iff PARM is a forwarding reference in the context of
22774 template argument deduction for TMPL. */
22775
22776static bool
22777forwarding_reference_p (tree parm, tree tmpl)
22778{
22779 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22780 cv-unqualified template parameter ..." */
22781 if (TYPE_REF_P (parm)
22782 && TYPE_REF_IS_RVALUE (parm)
22783 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
22784 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
22785 {
22786 parm = TREE_TYPE (parm);
22787 /* [temp.deduct.call], "... that does not represent a template parameter
22788 of a class template (during class template argument deduction)." */
22789 if (tmpl
22790 && deduction_guide_p (tmpl)
22791 && DECL_ARTIFICIAL (tmpl))
22792 {
22793 /* Since the template parameters of a synthesized guide consist of
22794 the template parameters of the class template followed by those of
22795 the constructor (if any), we can tell if PARM represents a template
22796 parameter of the class template by comparing its index with the
22797 arity of the class template. */
22798 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
22799 if (TEMPLATE_TYPE_IDX (parm)
22800 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
22801 return false;
22802 }
22803 return true;
22804 }
22805 return false;
22806}
22807
22808/* Adjust types before performing type deduction, as described in
22809 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22810 sections are symmetric. PARM is the type of a function parameter
22811 or the return type of the conversion function. ARG is the type of
22812 the argument passed to the call, or the type of the value
22813 initialized with the result of the conversion function.
22814 ARG_EXPR is the original argument expression, which may be null. */
22815
22816static int
22817maybe_adjust_types_for_deduction (tree tparms,
22818 unification_kind_t strict,
22819 tree* parm,
22820 tree* arg,
22821 tree arg_expr)
22822{
22823 int result = 0;
22824
22825 switch (strict)
22826 {
22827 case DEDUCE_CALL:
22828 break;
22829
22830 case DEDUCE_CONV:
22831 /* [temp.deduct.conv] First remove a reference type on parm.
22832 DRs 322 & 976 affected this. */
22833 if (TYPE_REF_P (*parm))
22834 *parm = TREE_TYPE (*parm);
22835
22836 /* Swap PARM and ARG throughout the remainder of this
22837 function; the handling is precisely symmetric since PARM
22838 will initialize ARG rather than vice versa. */
22839 std::swap (a&: parm, b&: arg);
22840
22841 break;
22842
22843 case DEDUCE_EXACT:
22844 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22845 too, but here handle it by stripping the reference from PARM
22846 rather than by adding it to ARG. */
22847 if (forwarding_reference_p (parm: *parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22848 && TYPE_REF_P (*arg)
22849 && !TYPE_REF_IS_RVALUE (*arg))
22850 *parm = TREE_TYPE (*parm);
22851 /* Nothing else to do in this case. */
22852 return 0;
22853
22854 default:
22855 gcc_unreachable ();
22856 }
22857
22858 if (!TYPE_REF_P (*parm))
22859 {
22860 /* [temp.deduct.call]
22861
22862 If P is not a reference type:
22863
22864 --If A is an array type, the pointer type produced by the
22865 array-to-pointer standard conversion (_conv.array_) is
22866 used in place of A for type deduction; otherwise,
22867
22868 --If A is a function type, the pointer type produced by
22869 the function-to-pointer standard conversion
22870 (_conv.func_) is used in place of A for type deduction;
22871 otherwise,
22872
22873 --If A is a cv-qualified type, the top level
22874 cv-qualifiers of A's type are ignored for type
22875 deduction. */
22876 if (TREE_CODE (*arg) == ARRAY_TYPE)
22877 *arg = build_pointer_type (TREE_TYPE (*arg));
22878 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
22879 *arg = build_pointer_type (*arg);
22880 else
22881 *arg = TYPE_MAIN_VARIANT (*arg);
22882 }
22883
22884 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22885 an lvalue, the type 'lvalue reference to A' is used in place of A for
22886 type deduction." */
22887 if (forwarding_reference_p (parm: *parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22888 && (arg_expr ? lvalue_p (arg_expr)
22889 /* try_one_overload doesn't provide an arg_expr, but
22890 functions are always lvalues. */
22891 : TREE_CODE (*arg) == FUNCTION_TYPE))
22892 *arg = build_reference_type (*arg);
22893
22894 /* [temp.deduct.call]
22895
22896 If P is a cv-qualified type, the top level cv-qualifiers
22897 of P's type are ignored for type deduction. If P is a
22898 reference type, the type referred to by P is used for
22899 type deduction. */
22900 *parm = TYPE_MAIN_VARIANT (*parm);
22901 if (TYPE_REF_P (*parm))
22902 {
22903 *parm = TREE_TYPE (*parm);
22904 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22905 }
22906
22907 return result;
22908}
22909
22910/* Return true if computing a conversion from FROM to TO might induce template
22911 instantiation. Conversely, if this predicate returns false then computing
22912 the conversion definitely won't induce template instantiation. */
22913
22914static bool
22915conversion_may_instantiate_p (tree to, tree from)
22916{
22917 to = non_reference (to);
22918 from = non_reference (from);
22919
22920 bool ptr_conv_p = false;
22921 if (TYPE_PTR_P (to)
22922 && TYPE_PTR_P (from))
22923 {
22924 to = TREE_TYPE (to);
22925 from = TREE_TYPE (from);
22926 ptr_conv_p = true;
22927 }
22928
22929 /* If one of the types is a not-yet-instantiated class template
22930 specialization, then computing the conversion might instantiate
22931 it in order to inspect bases, conversion functions and/or
22932 converting constructors. */
22933 if ((CLASS_TYPE_P (to)
22934 && !COMPLETE_TYPE_P (to)
22935 && CLASSTYPE_TEMPLATE_INSTANTIATION (to))
22936 || (CLASS_TYPE_P (from)
22937 && !COMPLETE_TYPE_P (from)
22938 && CLASSTYPE_TEMPLATE_INSTANTIATION (from)))
22939 return true;
22940
22941 /* Converting from one pointer type to another, or between
22942 reference-related types, always yields a standard conversion. */
22943 if (ptr_conv_p || reference_related_p (to, from))
22944 return false;
22945
22946 /* Converting to a non-aggregate class type will consider its
22947 user-declared constructors, which might induce instantiation. */
22948 if (CLASS_TYPE_P (to)
22949 && CLASSTYPE_NON_AGGREGATE (to))
22950 return true;
22951
22952 /* Similarly, converting from a class type will consider its conversion
22953 functions. */
22954 if (CLASS_TYPE_P (from)
22955 && TYPE_HAS_CONVERSION (from))
22956 return true;
22957
22958 /* Otherwise, computing this conversion definitely won't induce
22959 template instantiation. */
22960 return false;
22961}
22962
22963/* Subroutine of fn_type_unification. PARM is a function parameter of a
22964 template which doesn't contain any deducible template parameters; check if
22965 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22966 unify_one_argument. */
22967
22968static int
22969check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
22970 int flags, struct conversion **conv_p,
22971 bool explain_p, bool noninst_only_p)
22972{
22973 tree type;
22974
22975 if (!TYPE_P (arg))
22976 type = TREE_TYPE (arg);
22977 else
22978 type = arg;
22979
22980 if (same_type_p (parm, type))
22981 return unify_success (explain_p);
22982
22983 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22984 if (strict == DEDUCE_CONV)
22985 {
22986 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
22987 return unify_success (explain_p);
22988 }
22989 else if (strict == DEDUCE_CALL)
22990 {
22991 if (conv_p && *conv_p)
22992 {
22993 /* This conversion was already computed earlier (when
22994 computing only non-instantiating conversions). */
22995 gcc_checking_assert (!noninst_only_p);
22996 return unify_success (explain_p);
22997 }
22998
22999 if (noninst_only_p
23000 && conversion_may_instantiate_p (to: parm, from: type))
23001 return unify_success (explain_p);
23002
23003 bool ok = false;
23004 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
23005 if (conv_p)
23006 /* Avoid recalculating this in add_function_candidate. */
23007 ok = (*conv_p
23008 = good_conversion (parm, type, conv_arg, flags, complain));
23009 else
23010 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
23011 if (ok)
23012 return unify_success (explain_p);
23013 }
23014
23015 if (strict == DEDUCE_EXACT)
23016 return unify_type_mismatch (explain_p, parm, arg);
23017 else
23018 return unify_arg_conversion (explain_p, to_type: parm, from_type: type, arg);
23019}
23020
23021static bool uses_deducible_template_parms (tree type);
23022
23023/* Returns true iff the expression EXPR is one from which a template
23024 argument can be deduced. In other words, if it's an undecorated
23025 use of a template non-type parameter. */
23026
23027static bool
23028deducible_expression (tree expr)
23029{
23030 /* Strip implicit conversions and implicit INDIRECT_REFs. */
23031 while (CONVERT_EXPR_P (expr)
23032 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
23033 || REFERENCE_REF_P (expr))
23034 expr = TREE_OPERAND (expr, 0);
23035 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
23036}
23037
23038/* Returns true iff the array domain DOMAIN uses a template parameter in a
23039 deducible way; that is, if it has a max value of <PARM> - 1. */
23040
23041static bool
23042deducible_array_bound (tree domain)
23043{
23044 if (domain == NULL_TREE)
23045 return false;
23046
23047 tree max = TYPE_MAX_VALUE (domain);
23048 if (TREE_CODE (max) != MINUS_EXPR)
23049 return false;
23050
23051 return deducible_expression (TREE_OPERAND (max, 0));
23052}
23053
23054/* Returns true iff the template arguments ARGS use a template parameter
23055 in a deducible way. */
23056
23057static bool
23058deducible_template_args (tree args)
23059{
23060 for (tree elt : tree_vec_range (args))
23061 {
23062 bool deducible;
23063 if (ARGUMENT_PACK_P (elt))
23064 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
23065 else
23066 {
23067 if (PACK_EXPANSION_P (elt))
23068 elt = PACK_EXPANSION_PATTERN (elt);
23069 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
23070 deducible = true;
23071 else if (TYPE_P (elt))
23072 deducible = uses_deducible_template_parms (type: elt);
23073 else
23074 deducible = deducible_expression (expr: elt);
23075 }
23076 if (deducible)
23077 return true;
23078 }
23079 return false;
23080}
23081
23082/* Returns true iff TYPE contains any deducible references to template
23083 parameters, as per 14.8.2.5. */
23084
23085static bool
23086uses_deducible_template_parms (tree type)
23087{
23088 if (PACK_EXPANSION_P (type))
23089 type = PACK_EXPANSION_PATTERN (type);
23090
23091 /* T
23092 cv-list T
23093 TT<T>
23094 TT<i>
23095 TT<> */
23096 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23097 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23098 return true;
23099
23100 /* T*
23101 T&
23102 T&& */
23103 if (INDIRECT_TYPE_P (type))
23104 return uses_deducible_template_parms (TREE_TYPE (type));
23105
23106 /* T[integer-constant ]
23107 type [i] */
23108 if (TREE_CODE (type) == ARRAY_TYPE)
23109 return (uses_deducible_template_parms (TREE_TYPE (type))
23110 || deducible_array_bound (TYPE_DOMAIN (type)));
23111
23112 /* T type ::*
23113 type T::*
23114 T T::*
23115 T (type ::*)()
23116 type (T::*)()
23117 type (type ::*)(T)
23118 type (T::*)(T)
23119 T (type ::*)(T)
23120 T (T::*)()
23121 T (T::*)(T) */
23122 if (TYPE_PTRMEM_P (type))
23123 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
23124 || (uses_deducible_template_parms
23125 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
23126
23127 /* template-name <T> (where template-name refers to a class template)
23128 template-name <i> (where template-name refers to a class template) */
23129 if (CLASS_TYPE_P (type)
23130 && CLASSTYPE_TEMPLATE_INFO (type)
23131 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
23132 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
23133 (CLASSTYPE_TI_ARGS (type)));
23134
23135 /* type (T)
23136 T()
23137 T(T) */
23138 if (FUNC_OR_METHOD_TYPE_P (type))
23139 {
23140 if (uses_deducible_template_parms (TREE_TYPE (type)))
23141 return true;
23142 tree parm = TYPE_ARG_TYPES (type);
23143 if (TREE_CODE (type) == METHOD_TYPE)
23144 parm = TREE_CHAIN (parm);
23145 for (; parm; parm = TREE_CHAIN (parm))
23146 if (uses_deducible_template_parms (TREE_VALUE (parm)))
23147 return true;
23148 if (flag_noexcept_type
23149 && TYPE_RAISES_EXCEPTIONS (type)
23150 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
23151 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
23152 return true;
23153 }
23154
23155 return false;
23156}
23157
23158/* Subroutine of type_unification_real and unify_pack_expansion to
23159 handle unification of a single P/A pair. Parameters are as
23160 for those functions. */
23161
23162static int
23163unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
23164 int subr, unification_kind_t strict,
23165 bool explain_p)
23166{
23167 tree arg_expr = NULL_TREE;
23168 int arg_strict;
23169
23170 if (arg == error_mark_node || parm == error_mark_node)
23171 return unify_invalid (explain_p);
23172 if (arg == unknown_type_node)
23173 /* We can't deduce anything from this, but we might get all the
23174 template args from other function args. */
23175 return unify_success (explain_p);
23176
23177 /* Implicit conversions (Clause 4) will be performed on a function
23178 argument to convert it to the type of the corresponding function
23179 parameter if the parameter type contains no template-parameters that
23180 participate in template argument deduction. */
23181 if (strict != DEDUCE_EXACT
23182 && TYPE_P (parm) && !uses_deducible_template_parms (type: parm))
23183 /* For function parameters with no deducible template parameters,
23184 just return. We'll check non-dependent conversions later. */
23185 return unify_success (explain_p);
23186
23187 switch (strict)
23188 {
23189 case DEDUCE_CALL:
23190 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
23191 | UNIFY_ALLOW_MORE_CV_QUAL
23192 | UNIFY_ALLOW_DERIVED);
23193 break;
23194
23195 case DEDUCE_CONV:
23196 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
23197 break;
23198
23199 case DEDUCE_EXACT:
23200 arg_strict = UNIFY_ALLOW_NONE;
23201 break;
23202
23203 default:
23204 gcc_unreachable ();
23205 }
23206
23207 /* We only do these transformations if this is the top-level
23208 parameter_type_list in a call or declaration matching; in other
23209 situations (nested function declarators, template argument lists) we
23210 won't be comparing a type to an expression, and we don't do any type
23211 adjustments. */
23212 if (!subr)
23213 {
23214 if (!TYPE_P (arg))
23215 {
23216 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
23217 if (type_unknown_p (expr: arg))
23218 {
23219 /* [temp.deduct.type] A template-argument can be
23220 deduced from a pointer to function or pointer
23221 to member function argument if the set of
23222 overloaded functions does not contain function
23223 templates and at most one of a set of
23224 overloaded functions provides a unique
23225 match. */
23226 resolve_overloaded_unification (tparms, targs, parm,
23227 arg, strict,
23228 arg_strict, explain_p);
23229 /* If a unique match was not found, this is a
23230 non-deduced context, so we still succeed. */
23231 return unify_success (explain_p);
23232 }
23233
23234 arg_expr = arg;
23235 arg = unlowered_expr_type (arg);
23236 if (arg == error_mark_node)
23237 return unify_invalid (explain_p);
23238 }
23239
23240 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23241 parm: &parm, arg: &arg, arg_expr);
23242 }
23243 else
23244 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
23245 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
23246 return unify_template_argument_mismatch (explain_p, parm, arg);
23247
23248 /* For deduction from an init-list we need the actual list. */
23249 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
23250 arg = arg_expr;
23251 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
23252}
23253
23254/* for_each_template_parm callback that always returns 0. */
23255
23256static int
23257zero_r (tree, void *)
23258{
23259 return 0;
23260}
23261
23262/* for_each_template_parm any_fn callback to handle deduction of a template
23263 type argument from the type of an array bound. */
23264
23265static int
23266array_deduction_r (tree t, void *data)
23267{
23268 tree_pair_p d = (tree_pair_p)data;
23269 tree &tparms = d->purpose;
23270 tree &targs = d->value;
23271
23272 if (TREE_CODE (t) == ARRAY_TYPE)
23273 if (tree dom = TYPE_DOMAIN (t))
23274 if (tree max = TYPE_MAX_VALUE (dom))
23275 {
23276 if (TREE_CODE (max) == MINUS_EXPR)
23277 max = TREE_OPERAND (max, 0);
23278 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
23279 unify (tparms, targs, TREE_TYPE (max), size_type_node,
23280 UNIFY_ALLOW_NONE, /*explain*/false);
23281 }
23282
23283 /* Keep walking. */
23284 return 0;
23285}
23286
23287/* Try to deduce any not-yet-deduced template type arguments from the type of
23288 an array bound. This is handled separately from unify because 14.8.2.5 says
23289 "The type of a type parameter is only deduced from an array bound if it is
23290 not otherwise deduced." */
23291
23292static void
23293try_array_deduction (tree tparms, tree targs, tree parm)
23294{
23295 tree_pair_s data = { .purpose: tparms, .value: targs };
23296 hash_set<tree> visited;
23297 for_each_template_parm (t: parm, fn: zero_r, data: &data, visited: &visited,
23298 /*nondeduced*/include_nondeduced_p: false, any_fn: array_deduction_r);
23299}
23300
23301/* Most parms like fn_type_unification.
23302
23303 If SUBR is 1, we're being called recursively (to unify the
23304 arguments of a function or method parameter of a function
23305 template).
23306
23307 CHECKS is a pointer to a vector of access checks encountered while
23308 substituting default template arguments. */
23309
23310static int
23311type_unification_real (tree tparms,
23312 tree full_targs,
23313 tree xparms,
23314 const tree *xargs,
23315 unsigned int xnargs,
23316 int subr,
23317 unification_kind_t strict,
23318 vec<deferred_access_check, va_gc> **checks,
23319 bool explain_p)
23320{
23321 tree parm, arg;
23322 int i;
23323 int ntparms = TREE_VEC_LENGTH (tparms);
23324 int saw_undeduced = 0;
23325 tree parms;
23326 const tree *args;
23327 unsigned int nargs;
23328 unsigned int ia;
23329
23330 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
23331 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
23332 gcc_assert (ntparms > 0);
23333
23334 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
23335
23336 /* Reset the number of non-defaulted template arguments contained
23337 in TARGS. */
23338 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
23339
23340 again:
23341 parms = xparms;
23342 args = xargs;
23343 nargs = xnargs;
23344
23345 /* Only fn_type_unification cares about terminal void. */
23346 if (nargs && args[nargs-1] == void_type_node)
23347 --nargs;
23348
23349 ia = 0;
23350 while (parms && parms != void_list_node
23351 && ia < nargs)
23352 {
23353 parm = TREE_VALUE (parms);
23354
23355 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
23356 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
23357 /* For a function parameter pack that occurs at the end of the
23358 parameter-declaration-list, the type A of each remaining
23359 argument of the call is compared with the type P of the
23360 declarator-id of the function parameter pack. */
23361 break;
23362
23363 parms = TREE_CHAIN (parms);
23364
23365 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
23366 /* For a function parameter pack that does not occur at the
23367 end of the parameter-declaration-list, the type of the
23368 parameter pack is a non-deduced context. */
23369 continue;
23370
23371 /* [temp.deduct.conv] only applies to the deduction of the return
23372 type, which is always the first argument here. Other arguments
23373 (notably, explicit object parameters) should undergo normal
23374 call-like unification. */
23375 unification_kind_t kind = strict;
23376 if (strict == DEDUCE_CONV && ia > 0)
23377 kind = DEDUCE_CALL;
23378
23379 arg = args[ia];
23380 ++ia;
23381
23382 if (unify_one_argument (tparms, targs: full_targs, parm, arg, subr, strict: kind,
23383 explain_p))
23384 return 1;
23385 }
23386
23387 if (parms
23388 && parms != void_list_node
23389 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
23390 {
23391 gcc_assert (strict != DEDUCE_CONV);
23392
23393 /* Unify the remaining arguments with the pack expansion type. */
23394 tree argvec;
23395 tree parmvec = make_tree_vec (1);
23396
23397 /* Allocate a TREE_VEC and copy in all of the arguments */
23398 argvec = make_tree_vec (nargs - ia);
23399 for (i = 0; ia < nargs; ++ia, ++i)
23400 TREE_VEC_ELT (argvec, i) = args[ia];
23401
23402 /* Copy the parameter into parmvec. */
23403 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
23404 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
23405 /*subr=*/subr, explain_p))
23406 return 1;
23407
23408 /* Advance to the end of the list of parameters. */
23409 parms = TREE_CHAIN (parms);
23410 }
23411
23412 /* Fail if we've reached the end of the parm list, and more args
23413 are present, and the parm list isn't variadic. */
23414 if (ia < nargs && parms == void_list_node)
23415 return unify_too_many_arguments (explain_p, have: nargs, wanted: ia);
23416 /* Fail if parms are left and they don't have default values and
23417 they aren't all deduced as empty packs (c++/57397). This is
23418 consistent with sufficient_parms_p. */
23419 if (parms && parms != void_list_node
23420 && TREE_PURPOSE (parms) == NULL_TREE)
23421 {
23422 unsigned int count = nargs;
23423 tree p = parms;
23424 bool type_pack_p;
23425 do
23426 {
23427 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
23428 if (!type_pack_p)
23429 count++;
23430 p = TREE_CHAIN (p);
23431 }
23432 while (p && p != void_list_node);
23433 if (count != nargs)
23434 return unify_too_few_arguments (explain_p, have: ia, wanted: count,
23435 least_p: type_pack_p);
23436 }
23437
23438 if (!subr)
23439 {
23440 tsubst_flags_t complain = (explain_p
23441 ? tf_warning_or_error
23442 : tf_none);
23443 bool tried_array_deduction = (cxx_dialect < cxx17);
23444
23445 for (i = 0; i < ntparms; i++)
23446 {
23447 tree targ = TREE_VEC_ELT (targs, i);
23448 tree tparm = TREE_VEC_ELT (tparms, i);
23449
23450 /* Clear the "incomplete" flags on all argument packs now so that
23451 substituting them into later default arguments works. */
23452 if (targ && ARGUMENT_PACK_P (targ))
23453 {
23454 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
23455 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
23456 }
23457
23458 if (targ || tparm == error_mark_node)
23459 continue;
23460 tparm = TREE_VALUE (tparm);
23461
23462 if (TREE_CODE (tparm) == TYPE_DECL
23463 && !tried_array_deduction)
23464 {
23465 try_array_deduction (tparms, targs, parm: xparms);
23466 tried_array_deduction = true;
23467 if (TREE_VEC_ELT (targs, i))
23468 continue;
23469 }
23470
23471 /* If this is an undeduced nontype parameter that depends on
23472 a type parameter, try another pass; its type may have been
23473 deduced from a later argument than the one from which
23474 this parameter can be deduced. */
23475 if (TREE_CODE (tparm) == PARM_DECL
23476 && !is_auto (TREE_TYPE (tparm))
23477 && uses_template_parms (TREE_TYPE (tparm))
23478 && saw_undeduced < 2)
23479 {
23480 saw_undeduced = 1;
23481 continue;
23482 }
23483
23484 /* Core issue #226 (C++0x) [temp.deduct]:
23485
23486 If a template argument has not been deduced, its
23487 default template argument, if any, is used.
23488
23489 When we are in C++98 mode, TREE_PURPOSE will either
23490 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23491 to explicitly check cxx_dialect here. */
23492 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
23493 /* OK, there is a default argument. Wait until after the
23494 conversion check to do substitution. */
23495 continue;
23496
23497 /* If the type parameter is a parameter pack, then it will
23498 be deduced to an empty parameter pack. */
23499 if (template_parameter_pack_p (parm: tparm))
23500 {
23501 tree arg;
23502
23503 if (TREE_CODE (tparm) == PARM_DECL)
23504 {
23505 arg = make_node (NONTYPE_ARGUMENT_PACK);
23506 TREE_CONSTANT (arg) = 1;
23507 }
23508 else
23509 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
23510
23511 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
23512
23513 TREE_VEC_ELT (targs, i) = arg;
23514 continue;
23515 }
23516
23517 return unify_parameter_deduction_failure (explain_p, parm: tparm);
23518 }
23519
23520 /* During partial ordering, we deduce dependent template args. */
23521 bool any_dependent_targs = false;
23522
23523 /* Now substitute into the default template arguments. */
23524 for (i = 0; i < ntparms; i++)
23525 {
23526 tree targ = TREE_VEC_ELT (targs, i);
23527 tree tparm = TREE_VEC_ELT (tparms, i);
23528
23529 if (targ)
23530 {
23531 if (!any_dependent_targs && dependent_template_arg_p (targ))
23532 any_dependent_targs = true;
23533 continue;
23534 }
23535 if (tparm == error_mark_node)
23536 continue;
23537
23538 tree parm = TREE_VALUE (tparm);
23539 tree arg = TREE_PURPOSE (tparm);
23540 reopen_deferring_access_checks (*checks);
23541 location_t save_loc = input_location;
23542 if (DECL_P (parm))
23543 input_location = DECL_SOURCE_LOCATION (parm);
23544
23545 if (saw_undeduced == 1
23546 && TREE_CODE (parm) == PARM_DECL
23547 && !is_auto (TREE_TYPE (parm))
23548 && uses_template_parms (TREE_TYPE (parm)))
23549 {
23550 /* The type of this non-type parameter depends on undeduced
23551 parameters. Don't try to use its default argument yet,
23552 since we might deduce an argument for it on the next pass,
23553 but do check whether the arguments we already have cause
23554 substitution failure, so that that happens before we try
23555 later default arguments (78489). */
23556 ++processing_template_decl;
23557 tree type = tsubst (TREE_TYPE (parm), args: full_targs, complain,
23558 NULL_TREE);
23559 --processing_template_decl;
23560 if (type == error_mark_node)
23561 arg = error_mark_node;
23562 else
23563 arg = NULL_TREE;
23564 }
23565 else
23566 {
23567 /* Even if the call is happening in template context, getting
23568 here means it's non-dependent, and a default argument is
23569 considered a separate definition under [temp.decls], so we can
23570 do this substitution without processing_template_decl. This
23571 is important if the default argument contains something that
23572 might be instantiation-dependent like access (87480). */
23573 processing_template_decl_sentinel s (!any_dependent_targs);
23574 tree substed = NULL_TREE;
23575 if (saw_undeduced == 1 && !any_dependent_targs)
23576 {
23577 /* First instatiate in template context, in case we still
23578 depend on undeduced template parameters. */
23579 ++processing_template_decl;
23580 substed = tsubst_template_arg (t: arg, args: full_targs, complain,
23581 NULL_TREE);
23582 --processing_template_decl;
23583 if (substed != error_mark_node
23584 && !uses_template_parms (t: substed))
23585 /* We replaced all the tparms, substitute again out of
23586 template context. */
23587 substed = NULL_TREE;
23588 }
23589 if (!substed)
23590 substed = tsubst_template_arg (t: arg, args: full_targs, complain,
23591 NULL_TREE);
23592
23593 if (!uses_template_parms (t: substed))
23594 arg = convert_template_argument (parm, arg: substed, args: full_targs,
23595 complain, i, NULL_TREE);
23596 else if (saw_undeduced == 1)
23597 arg = NULL_TREE;
23598 else if (!any_dependent_targs)
23599 arg = error_mark_node;
23600 }
23601
23602 input_location = save_loc;
23603 *checks = get_deferred_access_checks ();
23604 pop_deferring_access_checks ();
23605
23606 if (arg == error_mark_node)
23607 return 1;
23608 else if (arg)
23609 {
23610 TREE_VEC_ELT (targs, i) = arg;
23611 /* The position of the first default template argument,
23612 is also the number of non-defaulted arguments in TARGS.
23613 Record that. */
23614 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23615 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
23616 }
23617 }
23618
23619 if (saw_undeduced++ == 1)
23620 goto again;
23621 }
23622
23623 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23624 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
23625
23626 return unify_success (explain_p);
23627}
23628
23629/* Subroutine of type_unification_real. Args are like the variables
23630 at the call site. ARG is an overloaded function (or template-id);
23631 we try deducing template args from each of the overloads, and if
23632 only one succeeds, we go with that. Modifies TARGS and returns
23633 true on success. */
23634
23635static bool
23636resolve_overloaded_unification (tree tparms,
23637 tree targs,
23638 tree parm,
23639 tree arg,
23640 unification_kind_t strict,
23641 int sub_strict,
23642 bool explain_p)
23643{
23644 tree tempargs = copy_node (targs);
23645 int good = 0;
23646 tree goodfn = NULL_TREE;
23647 bool addr_p;
23648
23649 if (TREE_CODE (arg) == ADDR_EXPR)
23650 {
23651 arg = TREE_OPERAND (arg, 0);
23652 addr_p = true;
23653 }
23654 else
23655 addr_p = false;
23656
23657 if (TREE_CODE (arg) == COMPONENT_REF)
23658 /* Handle `&x' where `x' is some static or non-static member
23659 function name. */
23660 arg = TREE_OPERAND (arg, 1);
23661
23662 if (TREE_CODE (arg) == OFFSET_REF)
23663 arg = TREE_OPERAND (arg, 1);
23664
23665 /* Strip baselink information. */
23666 if (BASELINK_P (arg))
23667 arg = BASELINK_FUNCTIONS (arg);
23668
23669 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
23670 {
23671 /* If we got some explicit template args, we need to plug them into
23672 the affected templates before we try to unify, in case the
23673 explicit args will completely resolve the templates in question. */
23674
23675 int ok = 0;
23676 tree expl_subargs = TREE_OPERAND (arg, 1);
23677 arg = TREE_OPERAND (arg, 0);
23678
23679 for (lkp_iterator iter (arg); iter; ++iter)
23680 {
23681 tree fn = *iter;
23682 tree subargs, elem;
23683
23684 if (TREE_CODE (fn) != TEMPLATE_DECL)
23685 continue;
23686
23687 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23688 args: expl_subargs, NULL_TREE, complain: tf_none);
23689 if (subargs != error_mark_node
23690 && !any_dependent_template_arguments_p (subargs))
23691 {
23692 fn = instantiate_template (tmpl: fn, orig_args: subargs, complain: tf_none);
23693 if (!constraints_satisfied_p (fn))
23694 continue;
23695 if (undeduced_auto_decl (fn))
23696 {
23697 /* Instantiate the function to deduce its return type. */
23698 ++function_depth;
23699 instantiate_decl (fn, /*defer*/false, /*class*/false);
23700 --function_depth;
23701 }
23702
23703 if (flag_noexcept_type)
23704 maybe_instantiate_noexcept (fn, tf_none);
23705
23706 elem = TREE_TYPE (fn);
23707 if (try_one_overload (tparms, targs, tempargs, parm,
23708 elem, strict, sub_strict, addr_p, explain_p)
23709 && (!goodfn || !same_type_p (goodfn, elem)))
23710 {
23711 goodfn = elem;
23712 ++good;
23713 }
23714 }
23715 else if (subargs)
23716 ++ok;
23717 }
23718 /* If no templates (or more than one) are fully resolved by the
23719 explicit arguments, this template-id is a non-deduced context; it
23720 could still be OK if we deduce all template arguments for the
23721 enclosing call through other arguments. */
23722 if (good != 1)
23723 good = ok;
23724 }
23725 else if (!OVL_P (arg))
23726 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23727 -- but the deduction does not succeed because the expression is
23728 not just the function on its own. */
23729 return false;
23730 else
23731 for (lkp_iterator iter (arg); iter; ++iter)
23732 {
23733 tree fn = *iter;
23734 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
23735 strict, sub_strict, addr_p, explain_p)
23736 && (!goodfn || !decls_match (goodfn, fn)))
23737 {
23738 goodfn = fn;
23739 ++good;
23740 }
23741 }
23742
23743 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23744 to function or pointer to member function argument if the set of
23745 overloaded functions does not contain function templates and at most
23746 one of a set of overloaded functions provides a unique match.
23747
23748 So if we found multiple possibilities, we return success but don't
23749 deduce anything. */
23750
23751 if (good == 1)
23752 {
23753 int i = TREE_VEC_LENGTH (targs);
23754 for (; i--; )
23755 if (TREE_VEC_ELT (tempargs, i))
23756 {
23757 tree old = TREE_VEC_ELT (targs, i);
23758 tree new_ = TREE_VEC_ELT (tempargs, i);
23759 if (new_ && old && ARGUMENT_PACK_P (old)
23760 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
23761 /* Don't forget explicit template arguments in a pack. */
23762 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
23763 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
23764 TREE_VEC_ELT (targs, i) = new_;
23765 }
23766 }
23767 if (good)
23768 return true;
23769
23770 return false;
23771}
23772
23773/* Core DR 115: In contexts where deduction is done and fails, or in
23774 contexts where deduction is not done, if a template argument list is
23775 specified and it, along with any default template arguments, identifies
23776 a single function template specialization, then the template-id is an
23777 lvalue for the function template specialization. */
23778
23779tree
23780resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
23781{
23782 tree expr, offset, baselink;
23783 bool addr;
23784
23785 if (!type_unknown_p (expr: orig_expr))
23786 return orig_expr;
23787
23788 expr = orig_expr;
23789 addr = false;
23790 offset = NULL_TREE;
23791 baselink = NULL_TREE;
23792
23793 if (TREE_CODE (expr) == ADDR_EXPR)
23794 {
23795 expr = TREE_OPERAND (expr, 0);
23796 addr = true;
23797 }
23798 if (TREE_CODE (expr) == OFFSET_REF)
23799 {
23800 offset = expr;
23801 expr = TREE_OPERAND (expr, 1);
23802 }
23803 if (BASELINK_P (expr))
23804 {
23805 baselink = expr;
23806 expr = BASELINK_FUNCTIONS (expr);
23807 }
23808
23809 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
23810 {
23811 int good = 0;
23812 tree goodfn = NULL_TREE;
23813
23814 /* If we got some explicit template args, we need to plug them into
23815 the affected templates before we try to unify, in case the
23816 explicit args will completely resolve the templates in question. */
23817
23818 tree expl_subargs = TREE_OPERAND (expr, 1);
23819 tree arg = TREE_OPERAND (expr, 0);
23820 tree badfn = NULL_TREE;
23821 tree badargs = NULL_TREE;
23822
23823 for (lkp_iterator iter (arg); iter; ++iter)
23824 {
23825 tree fn = *iter;
23826 tree subargs, elem;
23827
23828 if (TREE_CODE (fn) != TEMPLATE_DECL)
23829 continue;
23830
23831 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23832 args: expl_subargs, NULL_TREE, complain: tf_none);
23833 if (subargs != error_mark_node
23834 && !any_dependent_template_arguments_p (subargs))
23835 {
23836 elem = instantiate_template (tmpl: fn, orig_args: subargs, complain: tf_none);
23837 if (elem == error_mark_node)
23838 {
23839 badfn = fn;
23840 badargs = subargs;
23841 }
23842 else if (elem && (!goodfn || !decls_match (goodfn, elem))
23843 && constraints_satisfied_p (elem))
23844 {
23845 goodfn = elem;
23846 ++good;
23847 }
23848 }
23849 }
23850 if (good == 1)
23851 {
23852 mark_used (goodfn);
23853 expr = goodfn;
23854 if (baselink)
23855 expr = build_baselink (BASELINK_BINFO (baselink),
23856 BASELINK_ACCESS_BINFO (baselink),
23857 expr, BASELINK_OPTYPE (baselink));
23858 if (offset)
23859 {
23860 tree base
23861 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
23862 expr = build_offset_ref (base, expr, addr, complain);
23863 }
23864 if (addr)
23865 expr = cp_build_addr_expr (expr, complain);
23866 return expr;
23867 }
23868 else if (good == 0 && badargs && (complain & tf_error))
23869 /* There were no good options and at least one bad one, so let the
23870 user know what the problem is. */
23871 instantiate_template (tmpl: badfn, orig_args: badargs, complain);
23872 }
23873 return orig_expr;
23874}
23875
23876/* As above, but error out if the expression remains overloaded. */
23877
23878tree
23879resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
23880{
23881 exp = resolve_nondeduced_context (orig_expr: exp, complain);
23882 if (type_unknown_p (expr: exp))
23883 {
23884 if (complain & tf_error)
23885 cxx_incomplete_type_error (value: exp, TREE_TYPE (exp));
23886 return error_mark_node;
23887 }
23888 return exp;
23889}
23890
23891/* Subroutine of resolve_overloaded_unification; does deduction for a single
23892 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23893 different overloads deduce different arguments for a given parm.
23894 ADDR_P is true if the expression for which deduction is being
23895 performed was of the form "& fn" rather than simply "fn".
23896
23897 Returns 1 on success. */
23898
23899static int
23900try_one_overload (tree tparms,
23901 tree orig_targs,
23902 tree targs,
23903 tree parm,
23904 tree arg,
23905 unification_kind_t strict,
23906 int sub_strict,
23907 bool addr_p,
23908 bool explain_p)
23909{
23910 int nargs;
23911 tree tempargs;
23912 int i;
23913
23914 if (arg == error_mark_node)
23915 return 0;
23916
23917 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23918 to function or pointer to member function argument if the set of
23919 overloaded functions does not contain function templates and at most
23920 one of a set of overloaded functions provides a unique match.
23921
23922 So if this is a template, just return success. */
23923
23924 if (uses_template_parms (t: arg))
23925 return 1;
23926
23927 if (TREE_CODE (arg) == METHOD_TYPE)
23928 arg = build_ptrmemfunc_type (build_pointer_type (arg));
23929 else if (addr_p)
23930 arg = build_pointer_type (arg);
23931
23932 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23933 parm: &parm, arg: &arg, NULL_TREE);
23934
23935 /* We don't copy orig_targs for this because if we have already deduced
23936 some template args from previous args, unify would complain when we
23937 try to deduce a template parameter for the same argument, even though
23938 there isn't really a conflict. */
23939 nargs = TREE_VEC_LENGTH (targs);
23940 tempargs = make_tree_vec (nargs);
23941
23942 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
23943 return 0;
23944
23945 /* First make sure we didn't deduce anything that conflicts with
23946 explicitly specified args. */
23947 for (i = nargs; i--; )
23948 {
23949 tree elt = TREE_VEC_ELT (tempargs, i);
23950 tree oldelt = TREE_VEC_ELT (orig_targs, i);
23951
23952 if (!elt)
23953 /*NOP*/;
23954 else if (uses_template_parms (t: elt))
23955 /* Since we're unifying against ourselves, we will fill in
23956 template args used in the function parm list with our own
23957 template parms. Discard them. */
23958 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
23959 else if (oldelt && ARGUMENT_PACK_P (oldelt))
23960 {
23961 /* Check that the argument at each index of the deduced argument pack
23962 is equivalent to the corresponding explicitly specified argument.
23963 We may have deduced more arguments than were explicitly specified,
23964 and that's OK. */
23965
23966 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23967 that's wrong if we deduce the same argument pack from multiple
23968 function arguments: it's only incomplete the first time. */
23969
23970 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
23971 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
23972
23973 if (TREE_VEC_LENGTH (deduced_pack)
23974 < TREE_VEC_LENGTH (explicit_pack))
23975 return 0;
23976
23977 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
23978 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
23979 TREE_VEC_ELT (deduced_pack, j)))
23980 return 0;
23981 }
23982 else if (oldelt && !template_args_equal (ot: oldelt, nt: elt))
23983 return 0;
23984 }
23985
23986 for (i = nargs; i--; )
23987 {
23988 tree elt = TREE_VEC_ELT (tempargs, i);
23989
23990 if (elt)
23991 TREE_VEC_ELT (targs, i) = elt;
23992 }
23993
23994 return 1;
23995}
23996
23997/* PARM is a template class (perhaps with unbound template
23998 parameters). ARG is a fully instantiated type. If ARG can be
23999 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
24000 TARGS are as for unify. */
24001
24002static tree
24003try_class_unification (tree tparms, tree targs, tree parm, tree arg,
24004 bool explain_p)
24005{
24006 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24007 return NULL_TREE;
24008 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24009 /* Matches anything. */;
24010 else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
24011 return NULL_TREE;
24012
24013 /* We need to make a new template argument vector for the call to
24014 unify. If we used TARGS, we'd clutter it up with the result of
24015 the attempted unification, even if this class didn't work out.
24016 We also don't want to commit ourselves to all the unifications
24017 we've already done, since unification is supposed to be done on
24018 an argument-by-argument basis. In other words, consider the
24019 following pathological case:
24020
24021 template <int I, int J, int K>
24022 struct S {};
24023
24024 template <int I, int J>
24025 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
24026
24027 template <int I, int J, int K>
24028 void f(S<I, J, K>, S<I, I, I>);
24029
24030 void g() {
24031 S<0, 0, 0> s0;
24032 S<0, 1, 2> s2;
24033
24034 f(s0, s2);
24035 }
24036
24037 Now, by the time we consider the unification involving `s2', we
24038 already know that we must have `f<0, 0, 0>'. But, even though
24039 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
24040 because there are two ways to unify base classes of S<0, 1, 2>
24041 with S<I, I, I>. If we kept the already deduced knowledge, we
24042 would reject the possibility I=1. */
24043 targs = copy_template_args (t: targs);
24044 for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
24045 targ = NULL_TREE;
24046
24047 int err;
24048 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24049 err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
24050 else
24051 err = unify (tparms, targs,
24052 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
24053 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
24054 UNIFY_ALLOW_NONE, explain_p);
24055
24056 return err ? NULL_TREE : arg;
24057}
24058
24059/* Given a template type PARM and a class type ARG, find the unique
24060 base type in ARG that is an instance of PARM. We do not examine
24061 ARG itself; only its base-classes. If there is not exactly one
24062 appropriate base class, return NULL_TREE. PARM may be the type of
24063 a partial specialization, as well as a plain template type. Used
24064 by unify. */
24065
24066static enum template_base_result
24067get_template_base (tree tparms, tree targs, tree parm, tree arg,
24068 bool explain_p, tree *result)
24069{
24070 tree rval = NULL_TREE;
24071 tree binfo;
24072
24073 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
24074
24075 binfo = TYPE_BINFO (complete_type (arg));
24076 if (!binfo)
24077 {
24078 /* The type could not be completed. */
24079 *result = NULL_TREE;
24080 return tbr_incomplete_type;
24081 }
24082
24083 /* Walk in inheritance graph order. The search order is not
24084 important, and this avoids multiple walks of virtual bases. */
24085 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
24086 {
24087 tree r = try_class_unification (tparms, targs, parm,
24088 BINFO_TYPE (binfo), explain_p);
24089
24090 if (r)
24091 {
24092 /* If there is more than one satisfactory baseclass, then:
24093
24094 [temp.deduct.call]
24095
24096 If they yield more than one possible deduced A, the type
24097 deduction fails.
24098
24099 applies. */
24100 if (rval && !same_type_p (r, rval))
24101 {
24102 /* [temp.deduct.call]/4.3: If there is a class C that is a
24103 (direct or indirect) base class of D and derived (directly or
24104 indirectly) from a class B and that would be a valid deduced
24105 A, the deduced A cannot be B or pointer to B, respectively. */
24106 if (DERIVED_FROM_P (r, rval))
24107 /* Ignore r. */
24108 continue;
24109 else if (DERIVED_FROM_P (rval, r))
24110 /* Ignore rval. */;
24111 else
24112 {
24113 *result = NULL_TREE;
24114 return tbr_ambiguous_baseclass;
24115 }
24116 }
24117
24118 rval = r;
24119 }
24120 }
24121
24122 *result = rval;
24123 return tbr_success;
24124}
24125
24126/* Returns the level of DECL, which declares a template parameter. */
24127
24128static int
24129template_decl_level (tree decl)
24130{
24131 switch (TREE_CODE (decl))
24132 {
24133 case TYPE_DECL:
24134 case TEMPLATE_DECL:
24135 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
24136
24137 case PARM_DECL:
24138 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
24139
24140 default:
24141 gcc_unreachable ();
24142 }
24143 return 0;
24144}
24145
24146/* Decide whether ARG can be unified with PARM, considering only the
24147 cv-qualifiers of each type, given STRICT as documented for unify.
24148 Returns nonzero iff the unification is OK on that basis. */
24149
24150static int
24151check_cv_quals_for_unify (int strict, tree arg, tree parm)
24152{
24153 int arg_quals = cp_type_quals (arg);
24154 int parm_quals = cp_type_quals (parm);
24155
24156 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24157 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24158 {
24159 /* Although a CVR qualifier is ignored when being applied to a
24160 substituted template parameter ([8.3.2]/1 for example), that
24161 does not allow us to unify "const T" with "int&" because both
24162 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
24163 It is ok when we're allowing additional CV qualifiers
24164 at the outer level [14.8.2.1]/3,1st bullet. */
24165 if ((TYPE_REF_P (arg)
24166 || FUNC_OR_METHOD_TYPE_P (arg))
24167 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
24168 return 0;
24169
24170 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
24171 && (parm_quals & TYPE_QUAL_RESTRICT))
24172 return 0;
24173 }
24174
24175 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24176 && (arg_quals & parm_quals) != parm_quals)
24177 return 0;
24178
24179 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
24180 && (parm_quals & arg_quals) != arg_quals)
24181 return 0;
24182
24183 return 1;
24184}
24185
24186/* Determines the LEVEL and INDEX for the template parameter PARM. */
24187void
24188template_parm_level_and_index (tree parm, int* level, int* index)
24189{
24190 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24191 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24192 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24193 {
24194 *index = TEMPLATE_TYPE_IDX (parm);
24195 *level = TEMPLATE_TYPE_LEVEL (parm);
24196 }
24197 else
24198 {
24199 *index = TEMPLATE_PARM_IDX (parm);
24200 *level = TEMPLATE_PARM_LEVEL (parm);
24201 }
24202}
24203
24204#define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
24205 do { \
24206 if (unify (TP, TA, P, A, S, EP)) \
24207 return 1; \
24208 } while (0)
24209
24210/* Unifies the remaining arguments in PACKED_ARGS with the pack
24211 expansion at the end of PACKED_PARMS. Returns 0 if the type
24212 deduction succeeds, 1 otherwise. STRICT is the same as in
24213 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
24214 function call argument list. We'll need to adjust the arguments to make them
24215 types. SUBR tells us if this is from a recursive call to
24216 type_unification_real, or for comparing two template argument
24217 lists. */
24218
24219static int
24220unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
24221 tree packed_args, unification_kind_t strict,
24222 bool subr, bool explain_p)
24223{
24224 tree parm
24225 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
24226 tree pattern = PACK_EXPANSION_PATTERN (parm);
24227 tree pack, packs = NULL_TREE;
24228 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
24229
24230 /* Add in any args remembered from an earlier partial instantiation. */
24231 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), extra_args: targs);
24232 int levels = TMPL_ARGS_DEPTH (targs);
24233
24234 packed_args = expand_template_argument_pack (args: packed_args);
24235
24236 int len = TREE_VEC_LENGTH (packed_args);
24237
24238 /* Determine the parameter packs we will be deducing from the
24239 pattern, and record their current deductions. */
24240 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
24241 pack; pack = TREE_CHAIN (pack))
24242 {
24243 tree parm_pack = TREE_VALUE (pack);
24244 int idx, level;
24245
24246 /* Only template parameter packs can be deduced, not e.g. function
24247 parameter packs or __bases or __integer_pack. */
24248 if (!TEMPLATE_PARM_P (parm_pack))
24249 continue;
24250
24251 /* Determine the index and level of this parameter pack. */
24252 template_parm_level_and_index (parm: parm_pack, level: &level, index: &idx);
24253 if (level > levels)
24254 continue;
24255
24256 /* Keep track of the parameter packs and their corresponding
24257 argument packs. */
24258 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
24259 TREE_TYPE (packs) = make_tree_vec (len - start);
24260 }
24261
24262 /* Loop through all of the arguments that have not yet been
24263 unified and unify each with the pattern. */
24264 for (i = start; i < len; i++)
24265 {
24266 tree parm;
24267 bool any_explicit = false;
24268 tree arg = TREE_VEC_ELT (packed_args, i);
24269
24270 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
24271 or the element of its argument pack at the current index if
24272 this argument was explicitly specified. */
24273 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24274 {
24275 int idx, level;
24276 tree arg, pargs;
24277 template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx);
24278
24279 arg = NULL_TREE;
24280 if (TREE_VALUE (pack)
24281 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
24282 && (i - start < TREE_VEC_LENGTH (pargs)))
24283 {
24284 any_explicit = true;
24285 arg = TREE_VEC_ELT (pargs, i - start);
24286 }
24287 TMPL_ARG (targs, level, idx) = arg;
24288 }
24289
24290 /* If we had explicit template arguments, substitute them into the
24291 pattern before deduction. */
24292 if (any_explicit)
24293 {
24294 /* Some arguments might still be unspecified or dependent. */
24295 bool dependent;
24296 ++processing_template_decl;
24297 dependent = any_dependent_template_arguments_p (targs);
24298 if (!dependent)
24299 --processing_template_decl;
24300 parm = tsubst (t: pattern, args: targs,
24301 complain: explain_p ? tf_warning_or_error : tf_none,
24302 NULL_TREE);
24303 if (dependent)
24304 --processing_template_decl;
24305 if (parm == error_mark_node)
24306 return 1;
24307 }
24308 else
24309 parm = pattern;
24310
24311 /* Unify the pattern with the current argument. */
24312 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
24313 explain_p))
24314 return 1;
24315
24316 /* For each parameter pack, collect the deduced value. */
24317 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24318 {
24319 int idx, level;
24320 template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx);
24321
24322 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
24323 TMPL_ARG (targs, level, idx);
24324 }
24325 }
24326
24327 /* Verify that the results of unification with the parameter packs
24328 produce results consistent with what we've seen before, and make
24329 the deduced argument packs available. */
24330 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24331 {
24332 tree old_pack = TREE_VALUE (pack);
24333 tree new_args = TREE_TYPE (pack);
24334 int i, len = TREE_VEC_LENGTH (new_args);
24335 int idx, level;
24336 bool nondeduced_p = false;
24337
24338 /* By default keep the original deduced argument pack.
24339 If necessary, more specific code is going to update the
24340 resulting deduced argument later down in this function. */
24341 template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx);
24342 TMPL_ARG (targs, level, idx) = old_pack;
24343
24344 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24345 actually deduce anything. */
24346 for (i = 0; i < len && !nondeduced_p; ++i)
24347 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
24348 nondeduced_p = true;
24349 if (nondeduced_p)
24350 continue;
24351
24352 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
24353 {
24354 /* If we had fewer function args than explicit template args,
24355 just use the explicits. */
24356 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24357 int explicit_len = TREE_VEC_LENGTH (explicit_args);
24358 if (len < explicit_len)
24359 new_args = explicit_args;
24360 }
24361
24362 if (!old_pack)
24363 {
24364 tree result;
24365 /* Build the deduced *_ARGUMENT_PACK. */
24366 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
24367 {
24368 result = make_node (NONTYPE_ARGUMENT_PACK);
24369 TREE_CONSTANT (result) = 1;
24370 }
24371 else
24372 result = cxx_make_type (TYPE_ARGUMENT_PACK);
24373
24374 ARGUMENT_PACK_ARGS (result) = new_args;
24375
24376 /* Note the deduced argument packs for this parameter
24377 pack. */
24378 TMPL_ARG (targs, level, idx) = result;
24379 }
24380 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
24381 && (ARGUMENT_PACK_ARGS (old_pack)
24382 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
24383 {
24384 /* We only had the explicitly-provided arguments before, but
24385 now we have a complete set of arguments. */
24386 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24387
24388 ARGUMENT_PACK_ARGS (old_pack) = new_args;
24389 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
24390 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
24391 }
24392 else
24393 {
24394 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
24395 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
24396 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
24397 /* During template argument deduction for the aggregate deduction
24398 candidate, the number of elements in a trailing parameter pack
24399 is only deduced from the number of remaining function
24400 arguments if it is not otherwise deduced. */
24401 if (cxx_dialect >= cxx20
24402 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
24403 /* FIXME This isn't set properly for partial instantiations. */
24404 && TPARMS_PRIMARY_TEMPLATE (tparms)
24405 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
24406 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
24407 if (!comp_template_args (oldargs: old_args, newargs: new_args,
24408 oldarg_ptr: &bad_old_arg, newarg_ptr: &bad_new_arg))
24409 /* Inconsistent unification of this parameter pack. */
24410 return unify_parameter_pack_inconsistent (explain_p,
24411 old_arg: bad_old_arg,
24412 new_arg: bad_new_arg);
24413 }
24414 }
24415
24416 return unify_success (explain_p);
24417}
24418
24419/* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24420 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24421 parameters and return value are as for unify. */
24422
24423static int
24424unify_array_domain (tree tparms, tree targs,
24425 tree parm_dom, tree arg_dom,
24426 bool explain_p)
24427{
24428 tree parm_max;
24429 tree arg_max;
24430 bool parm_cst;
24431 bool arg_cst;
24432
24433 /* Our representation of array types uses "N - 1" as the
24434 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24435 not an integer constant. We cannot unify arbitrarily
24436 complex expressions, so we eliminate the MINUS_EXPRs
24437 here. */
24438 parm_max = TYPE_MAX_VALUE (parm_dom);
24439 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
24440 if (!parm_cst)
24441 {
24442 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
24443 parm_max = TREE_OPERAND (parm_max, 0);
24444 }
24445 arg_max = TYPE_MAX_VALUE (arg_dom);
24446 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
24447 if (!arg_cst)
24448 {
24449 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24450 trying to unify the type of a variable with the type
24451 of a template parameter. For example:
24452
24453 template <unsigned int N>
24454 void f (char (&) [N]);
24455 int g();
24456 void h(int i) {
24457 char a[g(i)];
24458 f(a);
24459 }
24460
24461 Here, the type of the ARG will be "int [g(i)]", and
24462 may be a SAVE_EXPR, etc. */
24463 if (TREE_CODE (arg_max) != MINUS_EXPR)
24464 return unify_vla_arg (explain_p, arg: arg_dom);
24465 arg_max = TREE_OPERAND (arg_max, 0);
24466 }
24467
24468 /* If only one of the bounds used a MINUS_EXPR, compensate
24469 by adding one to the other bound. */
24470 if (parm_cst && !arg_cst)
24471 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
24472 integer_type_node,
24473 parm_max,
24474 integer_one_node);
24475 else if (arg_cst && !parm_cst)
24476 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
24477 integer_type_node,
24478 arg_max,
24479 integer_one_node);
24480
24481 return unify (tparms, targs, parm_max, arg_max,
24482 UNIFY_ALLOW_INTEGER, explain_p);
24483}
24484
24485/* Returns whether T, a P or A in unify, is a type, template or expression. */
24486
24487enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
24488
24489static pa_kind_t
24490pa_kind (tree t)
24491{
24492 if (PACK_EXPANSION_P (t))
24493 t = PACK_EXPANSION_PATTERN (t);
24494 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
24495 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
24496 || DECL_TYPE_TEMPLATE_P (t))
24497 return pa_tmpl;
24498 else if (TYPE_P (t))
24499 return pa_type;
24500 else
24501 return pa_expr;
24502}
24503
24504/* Deduce the value of template parameters. TPARMS is the (innermost)
24505 set of template parameters to a template. TARGS is the bindings
24506 for those template parameters, as determined thus far; TARGS may
24507 include template arguments for outer levels of template parameters
24508 as well. PARM is a parameter to a template function, or a
24509 subcomponent of that parameter; ARG is the corresponding argument.
24510 This function attempts to match PARM with ARG in a manner
24511 consistent with the existing assignments in TARGS. If more values
24512 are deduced, then TARGS is updated.
24513
24514 Returns 0 if the type deduction succeeds, 1 otherwise. The
24515 parameter STRICT is a bitwise or of the following flags:
24516
24517 UNIFY_ALLOW_NONE:
24518 Require an exact match between PARM and ARG.
24519 UNIFY_ALLOW_MORE_CV_QUAL:
24520 Allow the deduced ARG to be more cv-qualified (by qualification
24521 conversion) than ARG.
24522 UNIFY_ALLOW_LESS_CV_QUAL:
24523 Allow the deduced ARG to be less cv-qualified than ARG.
24524 UNIFY_ALLOW_DERIVED:
24525 Allow the deduced ARG to be a template base class of ARG,
24526 or a pointer to a template base class of the type pointed to by
24527 ARG.
24528 UNIFY_ALLOW_INTEGER:
24529 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24530 case for more information.
24531 UNIFY_ALLOW_OUTER_LEVEL:
24532 This is the outermost level of a deduction. Used to determine validity
24533 of qualification conversions. A valid qualification conversion must
24534 have const qualified pointers leading up to the inner type which
24535 requires additional CV quals, except at the outer level, where const
24536 is not required [conv.qual]. It would be normal to set this flag in
24537 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24538 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24539 This is the outermost level of a deduction, and PARM can be more CV
24540 qualified at this point.
24541 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24542 This is the outermost level of a deduction, and PARM can be less CV
24543 qualified at this point. */
24544
24545static int
24546unify (tree tparms, tree targs, tree parm, tree arg, int strict,
24547 bool explain_p)
24548{
24549 int idx;
24550 tree targ;
24551 tree tparm;
24552 int strict_in = strict;
24553 tsubst_flags_t complain = (explain_p
24554 ? tf_warning_or_error
24555 : tf_none);
24556
24557 /* I don't think this will do the right thing with respect to types.
24558 But the only case I've seen it in so far has been array bounds, where
24559 signedness is the only information lost, and I think that will be
24560 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24561 finish_id_expression_1, and are also OK. */
24562 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
24563 parm = TREE_OPERAND (parm, 0);
24564
24565 if (arg == error_mark_node)
24566 return unify_invalid (explain_p);
24567 if (arg == unknown_type_node
24568 || arg == init_list_type_node)
24569 /* We can't deduce anything from this, but we might get all the
24570 template args from other function args. */
24571 return unify_success (explain_p);
24572
24573 if (parm == any_targ_node || arg == any_targ_node)
24574 return unify_success (explain_p);
24575
24576 /* If PARM uses template parameters, then we can't bail out here,
24577 even if ARG == PARM, since we won't record unifications for the
24578 template parameters. We might need them if we're trying to
24579 figure out which of two things is more specialized. */
24580 if (arg == parm
24581 && (DECL_P (parm) || !uses_template_parms (t: parm)))
24582 return unify_success (explain_p);
24583
24584 /* Handle init lists early, so the rest of the function can assume
24585 we're dealing with a type. */
24586 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
24587 {
24588 tree elttype;
24589 tree orig_parm = parm;
24590
24591 if (!is_std_init_list (parm)
24592 && TREE_CODE (parm) != ARRAY_TYPE)
24593 /* We can only deduce from an initializer list argument if the
24594 parameter is std::initializer_list or an array; otherwise this
24595 is a non-deduced context. */
24596 return unify_success (explain_p);
24597
24598 if (TREE_CODE (parm) == ARRAY_TYPE)
24599 elttype = TREE_TYPE (parm);
24600 else
24601 {
24602 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
24603 /* Deduction is defined in terms of a single type, so just punt
24604 on the (bizarre) std::initializer_list<T...>. */
24605 if (PACK_EXPANSION_P (elttype))
24606 return unify_success (explain_p);
24607 }
24608
24609 if (strict != DEDUCE_EXACT
24610 && TYPE_P (elttype)
24611 && !uses_deducible_template_parms (type: elttype))
24612 /* If ELTTYPE has no deducible template parms, skip deduction from
24613 the list elements. */;
24614 else
24615 for (auto &e: CONSTRUCTOR_ELTS (arg))
24616 {
24617 tree elt = e.value;
24618 int elt_strict = strict;
24619
24620 if (elt == error_mark_node)
24621 return unify_invalid (explain_p);
24622
24623 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
24624 {
24625 tree type = TREE_TYPE (elt);
24626 if (type == error_mark_node)
24627 return unify_invalid (explain_p);
24628 /* It should only be possible to get here for a call. */
24629 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
24630 elt_strict |= maybe_adjust_types_for_deduction
24631 (tparms, strict: DEDUCE_CALL, parm: &elttype, arg: &type, arg_expr: elt);
24632 elt = type;
24633 }
24634
24635 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
24636 explain_p);
24637 }
24638
24639 if (TREE_CODE (parm) == ARRAY_TYPE
24640 && deducible_array_bound (TYPE_DOMAIN (parm)))
24641 {
24642 /* Also deduce from the length of the initializer list. */
24643 tree max = size_int (CONSTRUCTOR_NELTS (arg));
24644 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
24645 if (idx == error_mark_node)
24646 return unify_invalid (explain_p);
24647 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
24648 arg_dom: idx, explain_p);
24649 }
24650
24651 /* If the std::initializer_list<T> deduction worked, replace the
24652 deduced A with std::initializer_list<A>. */
24653 if (orig_parm != parm)
24654 {
24655 idx = TEMPLATE_TYPE_IDX (orig_parm);
24656 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24657 targ = listify (targ);
24658 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
24659 }
24660 return unify_success (explain_p);
24661 }
24662
24663 /* If parm and arg aren't the same kind of thing (template, type, or
24664 expression), fail early. */
24665 if (pa_kind (t: parm) != pa_kind (t: arg))
24666 return unify_invalid (explain_p);
24667
24668 /* Immediately reject some pairs that won't unify because of
24669 cv-qualification mismatches. */
24670 if (TREE_CODE (arg) == TREE_CODE (parm)
24671 && TYPE_P (arg)
24672 /* It is the elements of the array which hold the cv quals of an array
24673 type, and the elements might be template type parms. We'll check
24674 when we recurse. */
24675 && TREE_CODE (arg) != ARRAY_TYPE
24676 /* We check the cv-qualifiers when unifying with template type
24677 parameters below. We want to allow ARG `const T' to unify with
24678 PARM `T' for example, when computing which of two templates
24679 is more specialized, for example. */
24680 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
24681 && !check_cv_quals_for_unify (strict: strict_in, arg, parm))
24682 return unify_cv_qual_mismatch (explain_p, parm, arg);
24683
24684 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
24685 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
24686 && !FUNC_OR_METHOD_TYPE_P (parm))
24687 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
24688 /* PMFs recurse at the same level, so don't strip this yet. */
24689 if (!TYPE_PTRMEMFUNC_P (parm))
24690 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
24691 strict &= ~UNIFY_ALLOW_DERIVED;
24692 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
24693 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
24694
24695 switch (TREE_CODE (parm))
24696 {
24697 case TYPENAME_TYPE:
24698 case SCOPE_REF:
24699 case UNBOUND_CLASS_TEMPLATE:
24700 /* In a type which contains a nested-name-specifier, template
24701 argument values cannot be deduced for template parameters used
24702 within the nested-name-specifier. */
24703 return unify_success (explain_p);
24704
24705 case TEMPLATE_TYPE_PARM:
24706 case TEMPLATE_TEMPLATE_PARM:
24707 case BOUND_TEMPLATE_TEMPLATE_PARM:
24708 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24709 if (error_operand_p (t: tparm))
24710 return unify_invalid (explain_p);
24711
24712 if (TEMPLATE_TYPE_LEVEL (parm)
24713 != template_decl_level (decl: tparm))
24714 /* The PARM is not one we're trying to unify. Just check
24715 to see if it matches ARG. */
24716 {
24717 if (TREE_CODE (arg) == TREE_CODE (parm)
24718 && (is_auto (parm) ? is_auto (arg)
24719 : same_type_p (parm, arg)))
24720 return unify_success (explain_p);
24721 else
24722 return unify_type_mismatch (explain_p, parm, arg);
24723 }
24724 idx = TEMPLATE_TYPE_IDX (parm);
24725 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24726 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
24727 if (error_operand_p (t: tparm))
24728 return unify_invalid (explain_p);
24729
24730 /* Check for mixed types and values. */
24731 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24732 && TREE_CODE (tparm) != TYPE_DECL)
24733 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24734 && TREE_CODE (tparm) != TEMPLATE_DECL))
24735 gcc_unreachable ();
24736
24737 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24738 {
24739 if ((strict_in & UNIFY_ALLOW_DERIVED)
24740 && CLASS_TYPE_P (arg))
24741 {
24742 /* First try to match ARG directly. */
24743 tree t = try_class_unification (tparms, targs, parm, arg,
24744 explain_p);
24745 if (!t)
24746 {
24747 /* Otherwise, look for a suitable base of ARG, as below. */
24748 enum template_base_result r;
24749 r = get_template_base (tparms, targs, parm, arg,
24750 explain_p, result: &t);
24751 if (!t)
24752 return unify_no_common_base (explain_p, r, parm, arg);
24753 arg = t;
24754 }
24755 }
24756 /* ARG must be constructed from a template class or a template
24757 template parameter. */
24758 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
24759 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24760 return unify_template_deduction_failure (explain_p, parm, arg);
24761
24762 /* Deduce arguments T, i from TT<T> or TT<i>. */
24763 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
24764 return 1;
24765
24766 arg = TYPE_TI_TEMPLATE (arg);
24767 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
24768 /* If the template is a template template parameter, use the
24769 TEMPLATE_TEMPLATE_PARM for matching. */
24770 arg = TREE_TYPE (arg);
24771
24772 /* Fall through to deduce template name. */
24773 }
24774
24775 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24776 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24777 {
24778 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24779
24780 /* Simple cases: Value already set, does match or doesn't. */
24781 if (targ != NULL_TREE && template_args_equal (ot: targ, nt: arg))
24782 return unify_success (explain_p);
24783 else if (targ)
24784 return unify_inconsistency (explain_p, parm, first: targ, second: arg);
24785 }
24786 else
24787 {
24788 /* If PARM is `const T' and ARG is only `int', we don't have
24789 a match unless we are allowing additional qualification.
24790 If ARG is `const int' and PARM is just `T' that's OK;
24791 that binds `const int' to `T'. */
24792 if (!check_cv_quals_for_unify (strict: strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
24793 arg, parm))
24794 return unify_cv_qual_mismatch (explain_p, parm, arg);
24795
24796 /* Consider the case where ARG is `const volatile int' and
24797 PARM is `const T'. Then, T should be `volatile int'. */
24798 arg = cp_build_qualified_type
24799 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
24800 if (arg == error_mark_node)
24801 return unify_invalid (explain_p);
24802
24803 /* Simple cases: Value already set, does match or doesn't. */
24804 if (targ != NULL_TREE && same_type_p (targ, arg))
24805 return unify_success (explain_p);
24806 else if (targ)
24807 return unify_inconsistency (explain_p, parm, first: targ, second: arg);
24808
24809 /* Make sure that ARG is not a variable-sized array. (Note
24810 that were talking about variable-sized arrays (like
24811 `int[n]'), rather than arrays of unknown size (like
24812 `int[]').) We'll get very confused by such a type since
24813 the bound of the array is not constant, and therefore
24814 not mangleable. Besides, such types are not allowed in
24815 ISO C++, so we can do as we please here. We do allow
24816 them for 'auto' deduction, since that isn't ABI-exposed. */
24817 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
24818 return unify_vla_arg (explain_p, arg);
24819
24820 /* Strip typedefs as in convert_template_argument. */
24821 arg = canonicalize_type_argument (arg, complain: tf_none);
24822 }
24823
24824 /* If ARG is a parameter pack or an expansion, we cannot unify
24825 against it unless PARM is also a parameter pack. */
24826 if ((template_parameter_pack_p (parm: arg) || PACK_EXPANSION_P (arg))
24827 && !template_parameter_pack_p (parm))
24828 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24829
24830 /* If the argument deduction results is a METHOD_TYPE,
24831 then there is a problem.
24832 METHOD_TYPE doesn't map to any real C++ type the result of
24833 the deduction cannot be of that type. */
24834 if (TREE_CODE (arg) == METHOD_TYPE)
24835 return unify_method_type_error (explain_p, arg);
24836
24837 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24838 return unify_success (explain_p);
24839
24840 case TEMPLATE_PARM_INDEX:
24841 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24842 if (error_operand_p (t: tparm))
24843 return unify_invalid (explain_p);
24844
24845 if (TEMPLATE_PARM_LEVEL (parm)
24846 != template_decl_level (decl: tparm))
24847 {
24848 /* The PARM is not one we're trying to unify. Just check
24849 to see if it matches ARG. */
24850 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
24851 && cp_tree_equal (parm, arg));
24852 if (result)
24853 unify_expression_unequal (explain_p, parm, arg);
24854 return result;
24855 }
24856
24857 idx = TEMPLATE_PARM_IDX (parm);
24858 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24859
24860 if (targ)
24861 {
24862 if ((strict & UNIFY_ALLOW_INTEGER)
24863 && TREE_TYPE (targ) && TREE_TYPE (arg)
24864 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
24865 /* We're deducing from an array bound, the type doesn't matter.
24866 This conversion should match the one below. */
24867 arg = fold (build_nop (TREE_TYPE (targ), arg));
24868 int x = !cp_tree_equal (targ, arg);
24869 if (x)
24870 unify_inconsistency (explain_p, parm, first: targ, second: arg);
24871 return x;
24872 }
24873
24874 /* [temp.deduct.type] If, in the declaration of a function template
24875 with a non-type template-parameter, the non-type
24876 template-parameter is used in an expression in the function
24877 parameter-list and, if the corresponding template-argument is
24878 deduced, the template-argument type shall match the type of the
24879 template-parameter exactly, except that a template-argument
24880 deduced from an array bound may be of any integral type.
24881 The non-type parameter might use already deduced type parameters. */
24882 tparm = TREE_TYPE (parm);
24883 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
24884 /* We don't have enough levels of args to do any substitution. This
24885 can happen in the context of -fnew-ttp-matching. */;
24886 else
24887 {
24888 ++processing_template_decl;
24889 tparm = tsubst (t: tparm, args: targs, complain: tf_none, NULL_TREE);
24890 --processing_template_decl;
24891
24892 if (tree a = type_uses_auto (tparm))
24893 {
24894 tparm = do_auto_deduction (tparm, arg, a,
24895 complain, adc_unify, targs,
24896 LOOKUP_NORMAL,
24897 TPARMS_PRIMARY_TEMPLATE (tparms));
24898 if (tparm == error_mark_node)
24899 return 1;
24900 }
24901 }
24902
24903 if (!TREE_TYPE (arg)
24904 || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
24905 /* Template-parameter dependent expression. Just accept it for now.
24906 It will later be processed in convert_template_argument. */
24907 ;
24908 else if (same_type_ignoring_top_level_qualifiers_p
24909 (non_reference (TREE_TYPE (arg)),
24910 non_reference (tparm)))
24911 /* OK. Ignore top-level quals here because a class-type template
24912 parameter object is const. */;
24913 else if ((strict & UNIFY_ALLOW_INTEGER)
24914 && CP_INTEGRAL_TYPE_P (tparm))
24915 /* Convert the ARG to the type of PARM; the deduced non-type
24916 template argument must exactly match the types of the
24917 corresponding parameter. This conversion should match the
24918 one above. */
24919 arg = fold (build_nop (tparm, arg));
24920 else if (uses_template_parms (t: tparm))
24921 {
24922 /* We haven't deduced the type of this parameter yet. */
24923 if (cxx_dialect >= cxx17
24924 /* We deduce from array bounds in try_array_deduction. */
24925 && !(strict & UNIFY_ALLOW_INTEGER)
24926 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
24927 {
24928 /* Deduce it from the non-type argument. As above, ignore
24929 top-level quals here too. */
24930 tree atype = cv_unqualified (TREE_TYPE (arg));
24931 RECUR_AND_CHECK_FAILURE (tparms, targs,
24932 tparm, atype,
24933 UNIFY_ALLOW_NONE, explain_p);
24934 /* Now check whether the type of this parameter is still
24935 dependent, and give up if so. */
24936 ++processing_template_decl;
24937 tparm = tsubst (TREE_TYPE (parm), args: targs, complain: tf_none, NULL_TREE);
24938 --processing_template_decl;
24939 if (uses_template_parms (t: tparm))
24940 return unify_success (explain_p);
24941 }
24942 else
24943 /* Try again later. */
24944 return unify_success (explain_p);
24945 }
24946 else
24947 return unify_type_mismatch (explain_p, parm: tparm, TREE_TYPE (arg));
24948
24949 /* If ARG is a parameter pack or an expansion, we cannot unify
24950 against it unless PARM is also a parameter pack. */
24951 if ((template_parameter_pack_p (parm: arg) || PACK_EXPANSION_P (arg))
24952 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
24953 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24954
24955 {
24956 bool removed_attr = false;
24957 arg = strip_typedefs_expr (arg, &removed_attr);
24958 }
24959 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24960 return unify_success (explain_p);
24961
24962 case PTRMEM_CST:
24963 {
24964 /* A pointer-to-member constant can be unified only with
24965 another constant. */
24966 if (TREE_CODE (arg) != PTRMEM_CST)
24967 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
24968
24969 /* Just unify the class member. It would be useless (and possibly
24970 wrong, depending on the strict flags) to unify also
24971 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24972 arg refer to the same variable, even if through different
24973 classes. For instance:
24974
24975 struct A { int x; };
24976 struct B : A { };
24977
24978 Unification of &A::x and &B::x must succeed. */
24979 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
24980 PTRMEM_CST_MEMBER (arg), strict, explain_p);
24981 }
24982
24983 case POINTER_TYPE:
24984 {
24985 if (!TYPE_PTR_P (arg))
24986 return unify_type_mismatch (explain_p, parm, arg);
24987
24988 /* [temp.deduct.call]
24989
24990 A can be another pointer or pointer to member type that can
24991 be converted to the deduced A via a qualification
24992 conversion (_conv.qual_).
24993
24994 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24995 This will allow for additional cv-qualification of the
24996 pointed-to types if appropriate. */
24997
24998 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
24999 /* The derived-to-base conversion only persists through one
25000 level of pointers. */
25001 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
25002
25003 return unify (tparms, targs, TREE_TYPE (parm),
25004 TREE_TYPE (arg), strict, explain_p);
25005 }
25006
25007 case REFERENCE_TYPE:
25008 if (!TYPE_REF_P (arg))
25009 return unify_type_mismatch (explain_p, parm, arg);
25010 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25011 strict: strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25012
25013 case ARRAY_TYPE:
25014 if (TREE_CODE (arg) != ARRAY_TYPE)
25015 return unify_type_mismatch (explain_p, parm, arg);
25016 if ((TYPE_DOMAIN (parm) == NULL_TREE)
25017 != (TYPE_DOMAIN (arg) == NULL_TREE))
25018 return unify_type_mismatch (explain_p, parm, arg);
25019 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25020 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25021 if (TYPE_DOMAIN (parm) != NULL_TREE)
25022 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
25023 TYPE_DOMAIN (arg), explain_p);
25024 return unify_success (explain_p);
25025
25026 case REAL_TYPE:
25027 case COMPLEX_TYPE:
25028 case VECTOR_TYPE:
25029 case INTEGER_TYPE:
25030 case BOOLEAN_TYPE:
25031 case ENUMERAL_TYPE:
25032 case VOID_TYPE:
25033 case OPAQUE_TYPE:
25034 case NULLPTR_TYPE:
25035 if (TREE_CODE (arg) != TREE_CODE (parm))
25036 return unify_type_mismatch (explain_p, parm, arg);
25037
25038 /* We have already checked cv-qualification at the top of the
25039 function. */
25040 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
25041 return unify_type_mismatch (explain_p, parm, arg);
25042
25043 /* As far as unification is concerned, this wins. Later checks
25044 will invalidate it if necessary. */
25045 return unify_success (explain_p);
25046
25047 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
25048 /* Type INTEGER_CST can come from ordinary constant template args. */
25049 case INTEGER_CST:
25050 case REAL_CST:
25051 if (TREE_TYPE (arg) == NULL_TREE
25052 || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
25053 return unify_template_argument_mismatch (explain_p, parm, arg);
25054 while (CONVERT_EXPR_P (arg))
25055 arg = TREE_OPERAND (arg, 0);
25056
25057 if (TREE_CODE (arg) != TREE_CODE (parm))
25058 return unify_template_argument_mismatch (explain_p, parm, arg);
25059 return (simple_cst_equal (parm, arg)
25060 ? unify_success (explain_p)
25061 : unify_template_argument_mismatch (explain_p, parm, arg));
25062
25063 case TREE_VEC:
25064 {
25065 int i, len, argslen;
25066 int parm_variadic_p = 0;
25067
25068 if (TREE_CODE (arg) != TREE_VEC)
25069 return unify_template_argument_mismatch (explain_p, parm, arg);
25070
25071 len = TREE_VEC_LENGTH (parm);
25072 argslen = TREE_VEC_LENGTH (arg);
25073
25074 /* Check for pack expansions in the parameters. */
25075 for (i = 0; i < len; ++i)
25076 {
25077 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
25078 {
25079 if (i == len - 1)
25080 /* We can unify against something with a trailing
25081 parameter pack. */
25082 parm_variadic_p = 1;
25083 else
25084 /* [temp.deduct.type]/9: If the template argument list of
25085 P contains a pack expansion that is not the last
25086 template argument, the entire template argument list
25087 is a non-deduced context. */
25088 return unify_success (explain_p);
25089 }
25090 }
25091
25092 /* If we don't have enough arguments to satisfy the parameters
25093 (not counting the pack expression at the end), or we have
25094 too many arguments for a parameter list that doesn't end in
25095 a pack expression, we can't unify. */
25096 if (parm_variadic_p
25097 ? argslen < len - parm_variadic_p
25098 : argslen != len)
25099 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), wanted: len);
25100
25101 /* Unify all of the parameters that precede the (optional)
25102 pack expression. */
25103 for (i = 0; i < len - parm_variadic_p; ++i)
25104 {
25105 RECUR_AND_CHECK_FAILURE (tparms, targs,
25106 TREE_VEC_ELT (parm, i),
25107 TREE_VEC_ELT (arg, i),
25108 UNIFY_ALLOW_NONE, explain_p);
25109 }
25110 if (parm_variadic_p)
25111 return unify_pack_expansion (tparms, targs, packed_parms: parm, packed_args: arg,
25112 strict: DEDUCE_EXACT,
25113 /*subr=*/true, explain_p);
25114 return unify_success (explain_p);
25115 }
25116
25117 case RECORD_TYPE:
25118 case UNION_TYPE:
25119 if (TREE_CODE (arg) != TREE_CODE (parm))
25120 return unify_type_mismatch (explain_p, parm, arg);
25121
25122 if (TYPE_PTRMEMFUNC_P (parm))
25123 {
25124 if (!TYPE_PTRMEMFUNC_P (arg))
25125 return unify_type_mismatch (explain_p, parm, arg);
25126
25127 return unify (tparms, targs,
25128 TYPE_PTRMEMFUNC_FN_TYPE (parm),
25129 TYPE_PTRMEMFUNC_FN_TYPE (arg),
25130 strict, explain_p);
25131 }
25132 else if (TYPE_PTRMEMFUNC_P (arg))
25133 return unify_type_mismatch (explain_p, parm, arg);
25134
25135 if (CLASSTYPE_TEMPLATE_INFO (parm))
25136 {
25137 tree t = NULL_TREE;
25138
25139 if (strict_in & UNIFY_ALLOW_DERIVED)
25140 {
25141 /* First, we try to unify the PARM and ARG directly. */
25142 t = try_class_unification (tparms, targs,
25143 parm, arg, explain_p);
25144
25145 if (!t)
25146 {
25147 /* Fallback to the special case allowed in
25148 [temp.deduct.call]:
25149
25150 If P is a class, and P has the form
25151 template-id, then A can be a derived class of
25152 the deduced A. Likewise, if P is a pointer to
25153 a class of the form template-id, A can be a
25154 pointer to a derived class pointed to by the
25155 deduced A. */
25156 enum template_base_result r;
25157 r = get_template_base (tparms, targs, parm, arg,
25158 explain_p, result: &t);
25159
25160 if (!t)
25161 {
25162 /* Don't give the derived diagnostic if we're
25163 already dealing with the same template. */
25164 bool same_template
25165 = (CLASSTYPE_TEMPLATE_INFO (arg)
25166 && (CLASSTYPE_TI_TEMPLATE (parm)
25167 == CLASSTYPE_TI_TEMPLATE (arg)));
25168 return unify_no_common_base (explain_p: explain_p && !same_template,
25169 r, parm, arg);
25170 }
25171 }
25172 }
25173 else if (CLASSTYPE_TEMPLATE_INFO (arg)
25174 && (CLASSTYPE_TI_TEMPLATE (parm)
25175 == CLASSTYPE_TI_TEMPLATE (arg)))
25176 /* Perhaps PARM is something like S<U> and ARG is S<int>.
25177 Then, we should unify `int' and `U'. */
25178 t = arg;
25179 else
25180 /* There's no chance of unification succeeding. */
25181 return unify_type_mismatch (explain_p, parm, arg);
25182
25183 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
25184 return unify (tparms, targs,
25185 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
25186 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
25187 UNIFY_ALLOW_NONE, explain_p);
25188 else
25189 return unify_success (explain_p);
25190 }
25191 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
25192 return unify_type_mismatch (explain_p, parm, arg);
25193 return unify_success (explain_p);
25194
25195 case METHOD_TYPE:
25196 case FUNCTION_TYPE:
25197 {
25198 unsigned int nargs;
25199 tree *args;
25200 tree a;
25201 unsigned int i;
25202
25203 if (TREE_CODE (arg) != TREE_CODE (parm))
25204 return unify_type_mismatch (explain_p, parm, arg);
25205
25206 /* CV qualifications for methods can never be deduced, they must
25207 match exactly. We need to check them explicitly here,
25208 because type_unification_real treats them as any other
25209 cv-qualified parameter. */
25210 if (TREE_CODE (parm) == METHOD_TYPE
25211 && (!check_cv_quals_for_unify
25212 (UNIFY_ALLOW_NONE,
25213 arg: class_of_this_parm (fntype: arg),
25214 parm: class_of_this_parm (fntype: parm))))
25215 return unify_cv_qual_mismatch (explain_p, parm, arg);
25216 if (TREE_CODE (arg) == FUNCTION_TYPE
25217 && type_memfn_quals (parm) != type_memfn_quals (arg))
25218 return unify_cv_qual_mismatch (explain_p, parm, arg);
25219 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
25220 return unify_type_mismatch (explain_p, parm, arg);
25221
25222 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
25223 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
25224
25225 nargs = list_length (TYPE_ARG_TYPES (arg));
25226 args = XALLOCAVEC (tree, nargs);
25227 for (a = TYPE_ARG_TYPES (arg), i = 0;
25228 a != NULL_TREE && a != void_list_node;
25229 a = TREE_CHAIN (a), ++i)
25230 args[i] = TREE_VALUE (a);
25231 nargs = i;
25232
25233 if (type_unification_real (tparms, full_targs: targs, TYPE_ARG_TYPES (parm),
25234 xargs: args, xnargs: nargs, subr: 1, strict: DEDUCE_EXACT,
25235 NULL, explain_p))
25236 return 1;
25237
25238 if (flag_noexcept_type)
25239 {
25240 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
25241 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
25242 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
25243 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
25244 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
25245 && uses_template_parms (TREE_PURPOSE (pspec)))
25246 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
25247 TREE_PURPOSE (aspec),
25248 UNIFY_ALLOW_NONE, explain_p);
25249 else
25250 {
25251 bool pn = nothrow_spec_p (pspec);
25252 bool an = nothrow_spec_p (aspec);
25253 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
25254 /more/ noexcept, since function pointer conversions are the
25255 reverse of qualification conversions. */
25256 if (an == pn
25257 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25258 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25259 /* OK. */;
25260 else
25261 return unify_type_mismatch (explain_p, parm, arg);
25262 }
25263 }
25264 if (flag_tm)
25265 {
25266 /* As for noexcept. */
25267 bool pn = tx_safe_fn_type_p (parm);
25268 bool an = tx_safe_fn_type_p (arg);
25269 if (an == pn
25270 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25271 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25272 /* OK. */;
25273 else
25274 return unify_type_mismatch (explain_p, parm, arg);
25275 }
25276
25277 return 0;
25278 }
25279
25280 case OFFSET_TYPE:
25281 /* Unify a pointer to member with a pointer to member function, which
25282 deduces the type of the member as a function type. */
25283 if (TYPE_PTRMEMFUNC_P (arg))
25284 {
25285 /* Check top-level cv qualifiers */
25286 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
25287 return unify_cv_qual_mismatch (explain_p, parm, arg);
25288
25289 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
25290 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
25291 UNIFY_ALLOW_NONE, explain_p);
25292
25293 /* Determine the type of the function we are unifying against. */
25294 tree fntype = static_fn_type (arg);
25295
25296 return unify (tparms, targs, TREE_TYPE (parm), arg: fntype, strict, explain_p);
25297 }
25298
25299 if (TREE_CODE (arg) != OFFSET_TYPE)
25300 return unify_type_mismatch (explain_p, parm, arg);
25301 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
25302 TYPE_OFFSET_BASETYPE (arg),
25303 UNIFY_ALLOW_NONE, explain_p);
25304 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25305 strict, explain_p);
25306
25307 case CONST_DECL:
25308 /* CONST_DECL should already have been folded to its DECL_INITIAL. */
25309 gcc_unreachable ();
25310
25311 case FIELD_DECL:
25312 case FUNCTION_DECL:
25313 case TEMPLATE_DECL:
25314 /* Matched cases are handled by the ARG == PARM test above. */
25315 return unify_template_argument_mismatch (explain_p, parm, arg);
25316
25317 case VAR_DECL:
25318 /* We might get a variable as a non-type template argument in parm if the
25319 corresponding parameter is type-dependent. Make any necessary
25320 adjustments based on whether arg is a reference. */
25321 if (CONSTANT_CLASS_P (arg))
25322 parm = fold_non_dependent_expr (parm, complain);
25323 else if (REFERENCE_REF_P (arg))
25324 {
25325 tree sub = TREE_OPERAND (arg, 0);
25326 STRIP_NOPS (sub);
25327 if (TREE_CODE (sub) == ADDR_EXPR)
25328 arg = TREE_OPERAND (sub, 0);
25329 }
25330 /* Now use the normal expression code to check whether they match. */
25331 goto expr;
25332
25333 case TYPE_ARGUMENT_PACK:
25334 case NONTYPE_ARGUMENT_PACK:
25335 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
25336 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
25337
25338 case TYPEOF_TYPE:
25339 case DECLTYPE_TYPE:
25340 case TRAIT_TYPE:
25341 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
25342 or TRAIT_TYPE nodes. */
25343 return unify_success (explain_p);
25344
25345 case ERROR_MARK:
25346 /* Unification fails if we hit an error node. */
25347 return unify_invalid (explain_p);
25348
25349 case INDIRECT_REF:
25350 if (REFERENCE_REF_P (parm))
25351 {
25352 bool pexp = PACK_EXPANSION_P (arg);
25353 if (pexp)
25354 arg = PACK_EXPANSION_PATTERN (arg);
25355 if (REFERENCE_REF_P (arg))
25356 arg = TREE_OPERAND (arg, 0);
25357 if (pexp)
25358 arg = make_pack_expansion (arg, complain);
25359 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
25360 strict, explain_p);
25361 }
25362 /* FALLTHRU */
25363
25364 default:
25365 /* An unresolved overload is a nondeduced context. */
25366 if (is_overloaded_fn (parm) || type_unknown_p (expr: parm))
25367 return unify_success (explain_p);
25368 gcc_assert (EXPR_P (parm)
25369 || TREE_CODE (parm) == CONSTRUCTOR
25370 || TREE_CODE (parm) == TRAIT_EXPR);
25371 expr:
25372 /* We must be looking at an expression. This can happen with
25373 something like:
25374
25375 template <int I>
25376 void foo(S<I>, S<I + 2>);
25377
25378 or
25379
25380 template<typename T>
25381 void foo(A<T, T{}>);
25382
25383 This is a "non-deduced context":
25384
25385 [deduct.type]
25386
25387 The non-deduced contexts are:
25388
25389 --A non-type template argument or an array bound in which
25390 a subexpression references a template parameter.
25391
25392 In these cases, we assume deduction succeeded, but don't
25393 actually infer any unifications. */
25394
25395 if (!uses_template_parms (t: parm)
25396 && !template_args_equal (ot: parm, nt: arg))
25397 return unify_expression_unequal (explain_p, parm, arg);
25398 else
25399 return unify_success (explain_p);
25400 }
25401}
25402#undef RECUR_AND_CHECK_FAILURE
25403
25404/* Note that DECL can be defined in this translation unit, if
25405 required. */
25406
25407static void
25408mark_definable (tree decl)
25409{
25410 tree clone;
25411 DECL_NOT_REALLY_EXTERN (decl) = 1;
25412 FOR_EACH_CLONE (clone, decl)
25413 DECL_NOT_REALLY_EXTERN (clone) = 1;
25414}
25415
25416/* Called if RESULT is explicitly instantiated, or is a member of an
25417 explicitly instantiated class. */
25418
25419void
25420mark_decl_instantiated (tree result, int extern_p)
25421{
25422 SET_DECL_EXPLICIT_INSTANTIATION (result);
25423
25424 /* If this entity has already been written out, it's too late to
25425 make any modifications. */
25426 if (TREE_ASM_WRITTEN (result))
25427 return;
25428
25429 /* consteval functions are never emitted. */
25430 if (TREE_CODE (result) == FUNCTION_DECL
25431 && DECL_IMMEDIATE_FUNCTION_P (result))
25432 return;
25433
25434 /* For anonymous namespace we don't need to do anything. */
25435 if (decl_internal_context_p (result))
25436 {
25437 gcc_assert (!TREE_PUBLIC (result));
25438 return;
25439 }
25440
25441 if (TREE_CODE (result) != FUNCTION_DECL)
25442 /* The TREE_PUBLIC flag for function declarations will have been
25443 set correctly by tsubst. */
25444 TREE_PUBLIC (result) = 1;
25445
25446 if (extern_p)
25447 {
25448 DECL_EXTERNAL (result) = 1;
25449 DECL_NOT_REALLY_EXTERN (result) = 0;
25450 }
25451 else
25452 {
25453 mark_definable (decl: result);
25454 mark_needed (result);
25455 /* Always make artificials weak. */
25456 if (DECL_ARTIFICIAL (result) && flag_weak)
25457 comdat_linkage (result);
25458 /* For WIN32 we also want to put explicit instantiations in
25459 linkonce sections. */
25460 else if (TREE_PUBLIC (result))
25461 maybe_make_one_only (result);
25462 if (TREE_CODE (result) == FUNCTION_DECL
25463 && DECL_TEMPLATE_INSTANTIATED (result))
25464 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25465 since start_preparsed_function wouldn't have if we had an earlier
25466 extern explicit instantiation. */
25467 DECL_EXTERNAL (result) = 0;
25468 }
25469
25470 /* If EXTERN_P, then this function will not be emitted -- unless
25471 followed by an explicit instantiation, at which point its linkage
25472 will be adjusted. If !EXTERN_P, then this function will be
25473 emitted here. In neither circumstance do we want
25474 import_export_decl to adjust the linkage. */
25475 DECL_INTERFACE_KNOWN (result) = 1;
25476}
25477
25478/* Subroutine of more_specialized_fn: check whether TARGS is missing any
25479 important template arguments. If any are missing, we check whether
25480 they're important by using error_mark_node for substituting into any
25481 args that were used for partial ordering (the ones between ARGS and END)
25482 and seeing if it bubbles up. */
25483
25484static bool
25485check_undeduced_parms (tree targs, tree args, tree end)
25486{
25487 bool found = false;
25488 for (tree& targ : tree_vec_range (targs))
25489 if (targ == NULL_TREE)
25490 {
25491 found = true;
25492 targ = error_mark_node;
25493 }
25494 if (found)
25495 {
25496 tree substed = tsubst_arg_types (arg_types: args, args: targs, end, complain: tf_none, NULL_TREE);
25497 if (substed == error_mark_node)
25498 return true;
25499 }
25500 return false;
25501}
25502
25503/* Given two function templates PAT1 and PAT2, return:
25504
25505 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25506 -1 if PAT2 is more specialized than PAT1.
25507 0 if neither is more specialized.
25508
25509 LEN indicates the number of parameters we should consider
25510 (defaulted parameters should not be considered).
25511
25512 The 1998 std underspecified function template partial ordering, and
25513 DR214 addresses the issue. We take pairs of arguments, one from
25514 each of the templates, and deduce them against each other. One of
25515 the templates will be more specialized if all the *other*
25516 template's arguments deduce against its arguments and at least one
25517 of its arguments *does* *not* deduce against the other template's
25518 corresponding argument. Deduction is done as for class templates.
25519 The arguments used in deduction have reference and top level cv
25520 qualifiers removed. Iff both arguments were originally reference
25521 types *and* deduction succeeds in both directions, an lvalue reference
25522 wins against an rvalue reference and otherwise the template
25523 with the more cv-qualified argument wins for that pairing (if
25524 neither is more cv-qualified, they both are equal). Unlike regular
25525 deduction, after all the arguments have been deduced in this way,
25526 we do *not* verify the deduced template argument values can be
25527 substituted into non-deduced contexts.
25528
25529 The logic can be a bit confusing here, because we look at deduce1 and
25530 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25531 can find template arguments for pat1 to make arg1 look like arg2, that
25532 means that arg2 is at least as specialized as arg1. */
25533
25534int
25535more_specialized_fn (tree pat1, tree pat2, int len)
25536{
25537 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
25538 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
25539 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
25540 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
25541 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
25542 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
25543 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
25544 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
25545 tree origs1, origs2;
25546 bool lose1 = false;
25547 bool lose2 = false;
25548
25549 /* C++17 [temp.func.order]/3 (CWG532)
25550
25551 If only one of the function templates M is a non-static member of some
25552 class A, M is considered to have a new first parameter inserted in its
25553 function parameter list. Given cv as the cv-qualifiers of M (if any), the
25554 new parameter is of type "rvalue reference to cv A" if the optional
25555 ref-qualifier of M is && or if M has no ref-qualifier and the first
25556 parameter of the other template has rvalue reference type. Otherwise, the
25557 new parameter is of type "lvalue reference to cv A". */
25558
25559 if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
25560 {
25561 /* Note C++20 DR2445 extended the above to static member functions, but
25562 I think the old G++ behavior of just skipping the object
25563 parameter when comparing to a static member function was better, so
25564 let's stick with that for now. This is CWG2834. --jason 2023-12 */
25565 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
25566 {
25567 len--; /* LEN is the number of significant arguments for DECL1 */
25568 args1 = TREE_CHAIN (args1);
25569 }
25570 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
25571 args2 = TREE_CHAIN (args2);
25572 }
25573 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
25574 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
25575 {
25576 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
25577 would break e.g. cpp1y/lambda-generic-variadic5.C. */
25578 len--;
25579 args1 = TREE_CHAIN (args1);
25580 args2 = TREE_CHAIN (args2);
25581 }
25582 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
25583 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
25584 {
25585 /* The other is a non-member or explicit object member function;
25586 rewrite the implicit object parameter to a reference. */
25587 tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
25588 tree &nsargs = ns == decl2 ? args2 : args1;
25589 tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
25590
25591 nsargs = TREE_CHAIN (nsargs);
25592
25593 cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
25594 if (rqual == REF_QUAL_NONE)
25595 {
25596 tree otherfirst = ns == decl1 ? args2 : args1;
25597 otherfirst = TREE_VALUE (otherfirst);
25598 if (TREE_CODE (otherfirst) == REFERENCE_TYPE
25599 && TYPE_REF_IS_RVALUE (otherfirst))
25600 rqual = REF_QUAL_RVALUE;
25601 }
25602 obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
25603 nsargs = tree_cons (NULL_TREE, obtype, nsargs);
25604 }
25605
25606 /* If only one is a conversion operator, they are unordered. */
25607 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
25608 return 0;
25609
25610 /* Consider the return type for a conversion function */
25611 if (DECL_CONV_FN_P (decl1))
25612 {
25613 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
25614 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
25615 len++;
25616 }
25617
25618 processing_template_decl++;
25619
25620 origs1 = args1;
25621 origs2 = args2;
25622
25623 while (len--
25624 /* Stop when an ellipsis is seen. */
25625 && args1 != NULL_TREE && args2 != NULL_TREE)
25626 {
25627 tree arg1 = TREE_VALUE (args1);
25628 tree arg2 = TREE_VALUE (args2);
25629 int deduce1, deduce2;
25630 int quals1 = -1;
25631 int quals2 = -1;
25632 int ref1 = 0;
25633 int ref2 = 0;
25634
25635 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25636 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25637 {
25638 /* When both arguments are pack expansions, we need only
25639 unify the patterns themselves. */
25640 arg1 = PACK_EXPANSION_PATTERN (arg1);
25641 arg2 = PACK_EXPANSION_PATTERN (arg2);
25642
25643 /* This is the last comparison we need to do. */
25644 len = 0;
25645 }
25646
25647 if (TYPE_REF_P (arg1))
25648 {
25649 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
25650 arg1 = TREE_TYPE (arg1);
25651 quals1 = cp_type_quals (arg1);
25652 }
25653
25654 if (TYPE_REF_P (arg2))
25655 {
25656 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
25657 arg2 = TREE_TYPE (arg2);
25658 quals2 = cp_type_quals (arg2);
25659 }
25660
25661 arg1 = TYPE_MAIN_VARIANT (arg1);
25662 arg2 = TYPE_MAIN_VARIANT (arg2);
25663
25664 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
25665 {
25666 int i, len2 = remaining_arguments (args2);
25667 tree parmvec = make_tree_vec (1);
25668 tree argvec = make_tree_vec (len2);
25669 tree ta = args2;
25670
25671 /* Setup the parameter vector, which contains only ARG1. */
25672 TREE_VEC_ELT (parmvec, 0) = arg1;
25673
25674 /* Setup the argument vector, which contains the remaining
25675 arguments. */
25676 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
25677 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25678
25679 deduce1 = (unify_pack_expansion (tparms: tparms1, targs: targs1, packed_parms: parmvec,
25680 packed_args: argvec, strict: DEDUCE_EXACT,
25681 /*subr=*/true, /*explain_p=*/false)
25682 == 0);
25683
25684 /* We cannot deduce in the other direction, because ARG1 is
25685 a pack expansion but ARG2 is not. */
25686 deduce2 = 0;
25687 }
25688 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25689 {
25690 int i, len1 = remaining_arguments (args1);
25691 tree parmvec = make_tree_vec (1);
25692 tree argvec = make_tree_vec (len1);
25693 tree ta = args1;
25694
25695 /* Setup the parameter vector, which contains only ARG1. */
25696 TREE_VEC_ELT (parmvec, 0) = arg2;
25697
25698 /* Setup the argument vector, which contains the remaining
25699 arguments. */
25700 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
25701 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25702
25703 deduce2 = (unify_pack_expansion (tparms: tparms2, targs: targs2, packed_parms: parmvec,
25704 packed_args: argvec, strict: DEDUCE_EXACT,
25705 /*subr=*/true, /*explain_p=*/false)
25706 == 0);
25707
25708 /* We cannot deduce in the other direction, because ARG2 is
25709 a pack expansion but ARG1 is not.*/
25710 deduce1 = 0;
25711 }
25712
25713 else
25714 {
25715 /* The normal case, where neither argument is a pack
25716 expansion. */
25717 deduce1 = (unify (tparms: tparms1, targs: targs1, parm: arg1, arg: arg2,
25718 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25719 == 0);
25720 deduce2 = (unify (tparms: tparms2, targs: targs2, parm: arg2, arg: arg1,
25721 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25722 == 0);
25723 }
25724
25725 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25726 arg2, then arg2 is not as specialized as arg1. */
25727 if (!deduce1)
25728 lose2 = true;
25729 if (!deduce2)
25730 lose1 = true;
25731
25732 /* "If, for a given type, deduction succeeds in both directions
25733 (i.e., the types are identical after the transformations above)
25734 and both P and A were reference types (before being replaced with
25735 the type referred to above):
25736 - if the type from the argument template was an lvalue reference and
25737 the type from the parameter template was not, the argument type is
25738 considered to be more specialized than the other; otherwise,
25739 - if the type from the argument template is more cv-qualified
25740 than the type from the parameter template (as described above),
25741 the argument type is considered to be more specialized than the other;
25742 otherwise,
25743 - neither type is more specialized than the other." */
25744
25745 if (deduce1 && deduce2)
25746 {
25747 if (ref1 && ref2 && ref1 != ref2)
25748 {
25749 if (ref1 > ref2)
25750 lose1 = true;
25751 else
25752 lose2 = true;
25753 }
25754 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
25755 {
25756 if ((quals1 & quals2) == quals2)
25757 lose2 = true;
25758 if ((quals1 & quals2) == quals1)
25759 lose1 = true;
25760 }
25761 }
25762
25763 if (lose1 && lose2)
25764 /* We've failed to deduce something in either direction.
25765 These must be unordered. */
25766 break;
25767
25768 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25769 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25770 /* We have already processed all of the arguments in our
25771 handing of the pack expansion type. */
25772 len = 0;
25773
25774 args1 = TREE_CHAIN (args1);
25775 args2 = TREE_CHAIN (args2);
25776 }
25777
25778 /* "In most cases, all template parameters must have values in order for
25779 deduction to succeed, but for partial ordering purposes a template
25780 parameter may remain without a value provided it is not used in the
25781 types being used for partial ordering."
25782
25783 Thus, if we are missing any of the targs1 we need to substitute into
25784 origs1, then pat2 is not as specialized as pat1. This can happen when
25785 there is a nondeduced context. */
25786 if (!lose2 && check_undeduced_parms (targs: targs1, args: origs1, end: args1))
25787 lose2 = true;
25788 if (!lose1 && check_undeduced_parms (targs: targs2, args: origs2, end: args2))
25789 lose1 = true;
25790
25791 processing_template_decl--;
25792
25793 /* If both deductions succeed, the partial ordering selects the more
25794 constrained template. */
25795 /* P2113: If the corresponding template-parameters of the
25796 template-parameter-lists are not equivalent ([temp.over.link]) or if
25797 the function parameters that positionally correspond between the two
25798 templates are not of the same type, neither template is more
25799 specialized than the other. */
25800 if (!lose1 && !lose2
25801 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
25802 DECL_TEMPLATE_PARMS (pat2))
25803 && compparms (origs1, origs2))
25804 {
25805 int winner = more_constrained (decl1, decl2);
25806 if (winner > 0)
25807 lose2 = true;
25808 else if (winner < 0)
25809 lose1 = true;
25810 }
25811
25812 /* All things being equal, if the next argument is a pack expansion
25813 for one function but not for the other, prefer the
25814 non-variadic function. FIXME this is bogus; see c++/41958. */
25815 if (lose1 == lose2
25816 && args1 && TREE_VALUE (args1)
25817 && args2 && TREE_VALUE (args2))
25818 {
25819 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
25820 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
25821 }
25822
25823 if (lose1 == lose2)
25824 return 0;
25825 else if (!lose1)
25826 return 1;
25827 else
25828 return -1;
25829}
25830
25831/* Determine which of two partial specializations of TMPL is more
25832 specialized.
25833
25834 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25835 to the first partial specialization. The TREE_PURPOSE is the
25836 innermost set of template parameters for the partial
25837 specialization. PAT2 is similar, but for the second template.
25838
25839 Return 1 if the first partial specialization is more specialized;
25840 -1 if the second is more specialized; 0 if neither is more
25841 specialized.
25842
25843 See [temp.class.order] for information about determining which of
25844 two templates is more specialized. */
25845
25846static int
25847more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
25848{
25849 tree targs;
25850 int winner = 0;
25851 bool any_deductions = false;
25852
25853 tree tmpl1 = TREE_VALUE (pat1);
25854 tree tmpl2 = TREE_VALUE (pat2);
25855 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
25856 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
25857
25858 /* Just like what happens for functions, if we are ordering between
25859 different template specializations, we may encounter dependent
25860 types in the arguments, and we need our dependency check functions
25861 to behave correctly. */
25862 ++processing_template_decl;
25863 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
25864 if (targs)
25865 {
25866 --winner;
25867 any_deductions = true;
25868 }
25869
25870 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
25871 if (targs)
25872 {
25873 ++winner;
25874 any_deductions = true;
25875 }
25876 --processing_template_decl;
25877
25878 /* If both deductions succeed, the partial ordering selects the more
25879 constrained template. */
25880 if (!winner && any_deductions)
25881 winner = more_constrained (tmpl1, tmpl2);
25882
25883 /* In the case of a tie where at least one of the templates
25884 has a parameter pack at the end, the template with the most
25885 non-packed parameters wins. */
25886 if (winner == 0
25887 && any_deductions
25888 && (template_args_variadic_p (TREE_PURPOSE (pat1))
25889 || template_args_variadic_p (TREE_PURPOSE (pat2))))
25890 {
25891 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
25892 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
25893 int len1 = TREE_VEC_LENGTH (args1);
25894 int len2 = TREE_VEC_LENGTH (args2);
25895
25896 /* We don't count the pack expansion at the end. */
25897 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
25898 --len1;
25899 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
25900 --len2;
25901
25902 if (len1 > len2)
25903 return 1;
25904 else if (len1 < len2)
25905 return -1;
25906 }
25907
25908 return winner;
25909}
25910
25911/* Return the template arguments that will produce the function signature
25912 DECL from the function template FN, with the explicit template
25913 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25914 also match. Return NULL_TREE if no satisfactory arguments could be
25915 found. */
25916
25917static tree
25918get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
25919{
25920 int ntparms = DECL_NTPARMS (fn);
25921 tree targs = make_tree_vec (ntparms);
25922 tree decl_type = TREE_TYPE (decl);
25923 tree decl_arg_types;
25924 tree *args;
25925 unsigned int nargs, ix;
25926 tree arg;
25927
25928 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
25929
25930 /* Never do unification on the 'this' parameter. */
25931 decl_arg_types = skip_artificial_parms_for (decl,
25932 TYPE_ARG_TYPES (decl_type));
25933
25934 nargs = list_length (decl_arg_types);
25935 args = XALLOCAVEC (tree, nargs);
25936 for (arg = decl_arg_types, ix = 0;
25937 arg != NULL_TREE;
25938 arg = TREE_CHAIN (arg), ++ix)
25939 args[ix] = TREE_VALUE (arg);
25940
25941 if (fn_type_unification (fn, explicit_targs: explicit_args, targs,
25942 args, nargs: ix,
25943 return_type: (check_rettype || DECL_CONV_FN_P (fn)
25944 ? TREE_TYPE (decl_type) : NULL_TREE),
25945 strict: DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
25946 /*explain_p=*/false,
25947 /*decltype*/decltype_p: false)
25948 == error_mark_node)
25949 return NULL_TREE;
25950
25951 return targs;
25952}
25953
25954/* Return the innermost template arguments that, when applied to a partial
25955 specialization SPEC_TMPL of TMPL, yield the ARGS.
25956
25957 For example, suppose we have:
25958
25959 template <class T, class U> struct S {};
25960 template <class T> struct S<T*, int> {};
25961
25962 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25963 partial specialization and the ARGS will be {double*, int}. The resulting
25964 vector will be {double}, indicating that `T' is bound to `double'. */
25965
25966static tree
25967get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
25968{
25969 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
25970 tree spec_args
25971 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
25972 int i, ntparms = TREE_VEC_LENGTH (tparms);
25973 tree deduced_args;
25974 tree innermost_deduced_args;
25975
25976 innermost_deduced_args = make_tree_vec (ntparms);
25977 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25978 {
25979 deduced_args = copy_node (args);
25980 SET_TMPL_ARGS_LEVEL (deduced_args,
25981 TMPL_ARGS_DEPTH (deduced_args),
25982 innermost_deduced_args);
25983 }
25984 else
25985 deduced_args = innermost_deduced_args;
25986
25987 bool tried_array_deduction = (cxx_dialect < cxx17);
25988 again:
25989 if (unify (tparms, targs: deduced_args,
25990 INNERMOST_TEMPLATE_ARGS (spec_args),
25991 INNERMOST_TEMPLATE_ARGS (args),
25992 UNIFY_ALLOW_NONE, /*explain_p=*/false))
25993 return NULL_TREE;
25994
25995 for (i = 0; i < ntparms; ++i)
25996 if (! TREE_VEC_ELT (innermost_deduced_args, i))
25997 {
25998 if (!tried_array_deduction)
25999 {
26000 try_array_deduction (tparms, targs: innermost_deduced_args,
26001 INNERMOST_TEMPLATE_ARGS (spec_args));
26002 tried_array_deduction = true;
26003 if (TREE_VEC_ELT (innermost_deduced_args, i))
26004 goto again;
26005 }
26006 return NULL_TREE;
26007 }
26008
26009 if (!push_tinst_level (tmpl: spec_tmpl, args: deduced_args))
26010 {
26011 excessive_deduction_depth = true;
26012 return NULL_TREE;
26013 }
26014
26015 /* Verify that nondeduced template arguments agree with the type
26016 obtained from argument deduction.
26017
26018 For example:
26019
26020 struct A { typedef int X; };
26021 template <class T, class U> struct C {};
26022 template <class T> struct C<T, typename T::X> {};
26023
26024 Then with the instantiation `C<A, int>', we can deduce that
26025 `T' is `A' but unify () does not check whether `typename T::X'
26026 is `int'. */
26027 spec_args = tsubst (t: spec_args, args: deduced_args, complain: tf_none, NULL_TREE);
26028
26029 if (spec_args != error_mark_node)
26030 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
26031 INNERMOST_TEMPLATE_ARGS (spec_args),
26032 in_decl: tmpl, complain: tf_none, require_all_args: false);
26033
26034 pop_tinst_level ();
26035
26036 if (spec_args == error_mark_node
26037 /* We only need to check the innermost arguments; the other
26038 arguments will always agree. */
26039 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
26040 INNERMOST_TEMPLATE_ARGS (args)))
26041 return NULL_TREE;
26042
26043 /* Now that we have bindings for all of the template arguments,
26044 ensure that the arguments deduced for the template template
26045 parameters have compatible template parameter lists. See the use
26046 of template_template_parm_bindings_ok_p in fn_type_unification
26047 for more information. */
26048 if (!template_template_parm_bindings_ok_p (tparms, targs: deduced_args))
26049 return NULL_TREE;
26050
26051 return deduced_args;
26052}
26053
26054// Compare two function templates T1 and T2 by deducing bindings
26055// from one against the other. If both deductions succeed, compare
26056// constraints to see which is more constrained.
26057static int
26058more_specialized_inst (tree t1, tree t2)
26059{
26060 int fate = 0;
26061 int count = 0;
26062
26063 if (get_bindings (fn: t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, check_rettype: true))
26064 {
26065 --fate;
26066 ++count;
26067 }
26068
26069 if (get_bindings (fn: t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, check_rettype: true))
26070 {
26071 ++fate;
26072 ++count;
26073 }
26074
26075 // If both deductions succeed, then one may be more constrained.
26076 if (count == 2 && fate == 0)
26077 fate = more_constrained (t1, t2);
26078
26079 return fate;
26080}
26081
26082/* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
26083 Return the TREE_LIST node with the most specialized template, if
26084 any. If there is no most specialized template, the error_mark_node
26085 is returned.
26086
26087 Note that this function does not look at, or modify, the
26088 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
26089 returned is one of the elements of INSTANTIATIONS, callers may
26090 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
26091 and retrieve it from the value returned. */
26092
26093tree
26094most_specialized_instantiation (tree templates)
26095{
26096 tree fn, champ;
26097
26098 ++processing_template_decl;
26099
26100 champ = templates;
26101 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
26102 {
26103 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
26104 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
26105 if (fate == -1)
26106 champ = fn;
26107 else if (!fate)
26108 {
26109 /* Equally specialized, move to next function. If there
26110 is no next function, nothing's most specialized. */
26111 fn = TREE_CHAIN (fn);
26112 champ = fn;
26113 if (!fn)
26114 break;
26115 }
26116 }
26117
26118 if (champ)
26119 /* Now verify that champ is better than everything earlier in the
26120 instantiation list. */
26121 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
26122 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
26123 {
26124 champ = NULL_TREE;
26125 break;
26126 }
26127 }
26128
26129 processing_template_decl--;
26130
26131 if (!champ)
26132 return error_mark_node;
26133
26134 return champ;
26135}
26136
26137/* If DECL is a specialization of some template, return the most
26138 general such template. Otherwise, returns NULL_TREE.
26139
26140 For example, given:
26141
26142 template <class T> struct S { template <class U> void f(U); };
26143
26144 if TMPL is `template <class U> void S<int>::f(U)' this will return
26145 the full template. This function will not trace past partial
26146 specializations, however. For example, given in addition:
26147
26148 template <class T> struct S<T*> { template <class U> void f(U); };
26149
26150 if TMPL is `template <class U> void S<int*>::f(U)' this will return
26151 `template <class T> template <class U> S<T*>::f(U)'. */
26152
26153tree
26154most_general_template (const_tree decl)
26155{
26156 if (TREE_CODE (decl) != TEMPLATE_DECL)
26157 {
26158 if (tree tinfo = get_template_info (t: decl))
26159 decl = TI_TEMPLATE (tinfo);
26160 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
26161 template friend, or a FIELD_DECL for a capture pack. */
26162 if (TREE_CODE (decl) != TEMPLATE_DECL)
26163 return NULL_TREE;
26164 }
26165
26166 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
26167 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
26168
26169 /* Look for more and more general templates. */
26170 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
26171 {
26172 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
26173 (See cp-tree.h for details.) */
26174 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
26175 break;
26176
26177 if (CLASS_TYPE_P (TREE_TYPE (decl))
26178 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
26179 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
26180 break;
26181
26182 /* Stop if we run into an explicitly specialized class template. */
26183 if (!DECL_NAMESPACE_SCOPE_P (decl)
26184 && DECL_CONTEXT (decl)
26185 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
26186 break;
26187
26188 decl = DECL_TI_TEMPLATE (decl);
26189 }
26190
26191 return CONST_CAST_TREE (decl);
26192}
26193
26194/* Return the most specialized of the template partial specializations
26195 which can produce TARGET, a specialization of some class or variable
26196 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
26197 TEMPLATE_DECL node corresponding to the partial specialization, while
26198 the TI_ARGS is the set of template arguments that must be substituted
26199 into the template pattern in order to generate TARGET. The result is
26200 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
26201 RECHECKING is true.
26202
26203 If the choice of partial specialization is ambiguous, a diagnostic
26204 is issued, and the error_mark_node is returned. If there are no
26205 partial specializations matching TARGET, then NULL_TREE is
26206 returned, indicating that the primary template should be used. */
26207
26208tree
26209most_specialized_partial_spec (tree target, tsubst_flags_t complain,
26210 bool rechecking /* = false */)
26211{
26212 tree tinfo = NULL_TREE;
26213 tree tmpl, args, decl;
26214 if (TYPE_P (target))
26215 {
26216 tinfo = CLASSTYPE_TEMPLATE_INFO (target);
26217 tmpl = TI_TEMPLATE (tinfo);
26218 args = TI_ARGS (tinfo);
26219 decl = TYPE_NAME (target);
26220 }
26221 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
26222 {
26223 tmpl = TREE_OPERAND (target, 0);
26224 args = TREE_OPERAND (target, 1);
26225 decl = DECL_TEMPLATE_RESULT (tmpl);
26226 }
26227 else if (VAR_P (target))
26228 {
26229 tinfo = DECL_TEMPLATE_INFO (target);
26230 tmpl = TI_TEMPLATE (tinfo);
26231 args = TI_ARGS (tinfo);
26232 decl = target;
26233 }
26234 else
26235 gcc_unreachable ();
26236
26237 if (!PRIMARY_TEMPLATE_P (tmpl))
26238 return NULL_TREE;
26239
26240 if (!rechecking
26241 && tinfo
26242 && (VAR_P (target) || COMPLETE_TYPE_P (target)))
26243 return TI_PARTIAL_INFO (tinfo);
26244
26245 tree main_tmpl = most_general_template (decl: tmpl);
26246 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
26247 if (!specs)
26248 /* There are no partial specializations of this template. */
26249 return NULL_TREE;
26250
26251 push_access_scope_guard pas (decl);
26252 deferring_access_check_sentinel acs (dk_no_deferred);
26253
26254 /* For determining which partial specialization to use, only the
26255 innermost args are interesting. */
26256 tree outer_args = NULL_TREE;
26257 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
26258 {
26259 outer_args = strip_innermost_template_args (args, extra_levels: 1);
26260 args = INNERMOST_TEMPLATE_ARGS (args);
26261 }
26262
26263 /* The caller hasn't called push_to_top_level yet, but we need
26264 get_partial_spec_bindings to be done in non-template context so that we'll
26265 fully resolve everything. */
26266 processing_template_decl_sentinel ptds;
26267
26268 tree list = NULL_TREE;
26269 for (tree t = specs; t; t = TREE_CHAIN (t))
26270 {
26271 const tree ospec_tmpl = TREE_VALUE (t);
26272
26273 tree spec_tmpl;
26274 if (outer_args)
26275 {
26276 /* Substitute in the template args from the enclosing class. */
26277 ++processing_template_decl;
26278 spec_tmpl = tsubst (t: ospec_tmpl, args: outer_args, complain: tf_none, NULL_TREE);
26279 --processing_template_decl;
26280 if (spec_tmpl == error_mark_node)
26281 return error_mark_node;
26282 }
26283 else
26284 spec_tmpl = ospec_tmpl;
26285
26286 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
26287 if (spec_args)
26288 {
26289 if (outer_args)
26290 spec_args = add_to_template_args (args: outer_args, extra_args: spec_args);
26291
26292 /* Keep the candidate only if its constraints are satisfied. */
26293 if (constraints_satisfied_p (ospec_tmpl, spec_args))
26294 list = tree_cons (spec_args, ospec_tmpl, list);
26295 }
26296 }
26297
26298 if (! list)
26299 return NULL_TREE;
26300
26301 tree champ = list;
26302 bool ambiguous_p = false;
26303 for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
26304 {
26305 int fate = more_specialized_partial_spec (tmpl, pat1: champ, pat2: t);
26306 if (fate == 1)
26307 ;
26308 else
26309 {
26310 if (fate == 0)
26311 {
26312 t = TREE_CHAIN (t);
26313 if (! t)
26314 {
26315 ambiguous_p = true;
26316 break;
26317 }
26318 }
26319 champ = t;
26320 }
26321 }
26322
26323 if (!ambiguous_p)
26324 for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
26325 {
26326 int fate = more_specialized_partial_spec (tmpl, pat1: champ, pat2: t);
26327 if (fate != 1)
26328 {
26329 ambiguous_p = true;
26330 break;
26331 }
26332 }
26333
26334 if (ambiguous_p)
26335 {
26336 const char *str;
26337 char *spaces = NULL;
26338 if (!(complain & tf_error))
26339 return error_mark_node;
26340 if (TYPE_P (target))
26341 error ("ambiguous template instantiation for %q#T", target);
26342 else
26343 error ("ambiguous template instantiation for %q#D", target);
26344 str = ngettext (msgid1: "candidate is:", msgid2: "candidates are:", n: list_length (list));
26345 for (tree t = list; t; t = TREE_CHAIN (t))
26346 {
26347 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
26348 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
26349 "%s %#qS", spaces ? spaces : str, subst);
26350 spaces = spaces ? spaces : get_spaces (str);
26351 }
26352 free (ptr: spaces);
26353 return error_mark_node;
26354 }
26355
26356 tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
26357 if (!rechecking && tinfo)
26358 TI_PARTIAL_INFO (tinfo) = result;
26359 return result;
26360}
26361
26362/* Explicitly instantiate DECL. */
26363
26364void
26365do_decl_instantiation (tree decl, tree storage)
26366{
26367 tree result = NULL_TREE;
26368 int extern_p = 0;
26369
26370 if (!decl || decl == error_mark_node)
26371 /* An error occurred, for which grokdeclarator has already issued
26372 an appropriate message. */
26373 return;
26374 else if (! DECL_LANG_SPECIFIC (decl))
26375 {
26376 error ("explicit instantiation of non-template %q#D", decl);
26377 return;
26378 }
26379 else if (DECL_DECLARED_CONCEPT_P (decl))
26380 {
26381 if (VAR_P (decl))
26382 error ("explicit instantiation of variable concept %q#D", decl);
26383 else
26384 error ("explicit instantiation of function concept %q#D", decl);
26385 return;
26386 }
26387
26388 bool var_templ = (DECL_TEMPLATE_INFO (decl)
26389 && variable_template_p (DECL_TI_TEMPLATE (decl)));
26390
26391 if (VAR_P (decl) && !var_templ)
26392 {
26393 /* There is an asymmetry here in the way VAR_DECLs and
26394 FUNCTION_DECLs are handled by grokdeclarator. In the case of
26395 the latter, the DECL we get back will be marked as a
26396 template instantiation, and the appropriate
26397 DECL_TEMPLATE_INFO will be set up. This does not happen for
26398 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
26399 should handle VAR_DECLs as it currently handles
26400 FUNCTION_DECLs. */
26401 if (!DECL_CLASS_SCOPE_P (decl))
26402 {
26403 error ("%qD is not a static data member of a class template", decl);
26404 return;
26405 }
26406 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
26407 if (!result || !VAR_P (result))
26408 {
26409 error ("no matching template for %qD found", decl);
26410 return;
26411 }
26412 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
26413 {
26414 error ("type %qT for explicit instantiation %qD does not match "
26415 "declared type %qT", TREE_TYPE (result), decl,
26416 TREE_TYPE (decl));
26417 return;
26418 }
26419 }
26420 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
26421 {
26422 error ("explicit instantiation of %q#D", decl);
26423 return;
26424 }
26425 else
26426 result = decl;
26427
26428 /* Check for various error cases. Note that if the explicit
26429 instantiation is valid the RESULT will currently be marked as an
26430 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26431 until we get here. */
26432
26433 if (DECL_TEMPLATE_SPECIALIZATION (result))
26434 {
26435 /* DR 259 [temp.spec].
26436
26437 Both an explicit instantiation and a declaration of an explicit
26438 specialization shall not appear in a program unless the explicit
26439 instantiation follows a declaration of the explicit specialization.
26440
26441 For a given set of template parameters, if an explicit
26442 instantiation of a template appears after a declaration of an
26443 explicit specialization for that template, the explicit
26444 instantiation has no effect. */
26445 return;
26446 }
26447 else if (DECL_EXPLICIT_INSTANTIATION (result))
26448 {
26449 /* [temp.spec]
26450
26451 No program shall explicitly instantiate any template more
26452 than once.
26453
26454 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26455 the first instantiation was `extern' and the second is not,
26456 and EXTERN_P for the opposite case. */
26457 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
26458 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
26459 /* If an "extern" explicit instantiation follows an ordinary
26460 explicit instantiation, the template is instantiated. */
26461 if (extern_p)
26462 return;
26463 }
26464 else if (!DECL_IMPLICIT_INSTANTIATION (result))
26465 {
26466 error ("no matching template for %qD found", result);
26467 return;
26468 }
26469 else if (!DECL_TEMPLATE_INFO (result))
26470 {
26471 permerror (input_location, "explicit instantiation of non-template %q#D", result);
26472 return;
26473 }
26474
26475 if (storage == NULL_TREE)
26476 ;
26477 else if (storage == ridpointers[(int) RID_EXTERN])
26478 {
26479 if (cxx_dialect == cxx98)
26480 pedwarn (input_location, OPT_Wpedantic,
26481 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26482 "instantiations");
26483 extern_p = 1;
26484 }
26485 else
26486 error ("storage class %qD applied to template instantiation", storage);
26487
26488 check_explicit_instantiation_namespace (spec: result);
26489 mark_decl_instantiated (result, extern_p);
26490 if (! extern_p)
26491 instantiate_decl (result, /*defer_ok=*/true,
26492 /*expl_inst_class_mem_p=*/false);
26493}
26494
26495static void
26496mark_class_instantiated (tree t, int extern_p)
26497{
26498 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
26499 SET_CLASSTYPE_INTERFACE_KNOWN (t);
26500 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
26501 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
26502 if (! extern_p)
26503 {
26504 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
26505 rest_of_type_compilation (t, 1);
26506 }
26507}
26508
26509/* Perform an explicit instantiation of template class T. STORAGE, if
26510 non-null, is the RID for extern, inline or static. COMPLAIN is
26511 nonzero if this is called from the parser, zero if called recursively,
26512 since the standard is unclear (as detailed below). */
26513
26514void
26515do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
26516{
26517 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
26518 {
26519 if (tree ti = TYPE_TEMPLATE_INFO (t))
26520 error ("explicit instantiation of non-class template %qD",
26521 TI_TEMPLATE (ti));
26522 else
26523 error ("explicit instantiation of non-template type %qT", t);
26524 return;
26525 }
26526
26527 complete_type (t);
26528
26529 if (!COMPLETE_TYPE_P (t))
26530 {
26531 if (complain & tf_error)
26532 error ("explicit instantiation of %q#T before definition of template",
26533 t);
26534 return;
26535 }
26536
26537 /* At most one of these will be true. */
26538 bool extern_p = false;
26539 bool nomem_p = false;
26540 bool static_p = false;
26541
26542 if (storage != NULL_TREE)
26543 {
26544 if (storage == ridpointers[(int) RID_EXTERN])
26545 {
26546 if (cxx_dialect == cxx98)
26547 pedwarn (input_location, OPT_Wpedantic,
26548 "ISO C++ 1998 forbids the use of %<extern%> on "
26549 "explicit instantiations");
26550 }
26551 else
26552 pedwarn (input_location, OPT_Wpedantic,
26553 "ISO C++ forbids the use of %qE"
26554 " on explicit instantiations", storage);
26555
26556 if (storage == ridpointers[(int) RID_INLINE])
26557 nomem_p = true;
26558 else if (storage == ridpointers[(int) RID_EXTERN])
26559 extern_p = true;
26560 else if (storage == ridpointers[(int) RID_STATIC])
26561 static_p = true;
26562 else
26563 error ("storage class %qD applied to template instantiation",
26564 storage);
26565 }
26566
26567 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
26568 /* DR 259 [temp.spec].
26569
26570 Both an explicit instantiation and a declaration of an explicit
26571 specialization shall not appear in a program unless the
26572 explicit instantiation follows a declaration of the explicit
26573 specialization.
26574
26575 For a given set of template parameters, if an explicit
26576 instantiation of a template appears after a declaration of an
26577 explicit specialization for that template, the explicit
26578 instantiation has no effect. */
26579 return;
26580
26581 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
26582 {
26583 /* We've already instantiated the template. */
26584
26585 /* [temp.spec]
26586
26587 No program shall explicitly instantiate any template more
26588 than once.
26589
26590 If EXTERN_P then this is ok. */
26591 if (!extern_p && (complain & tf_error))
26592 permerror (input_location,
26593 "duplicate explicit instantiation of %q#T", t);
26594
26595 return;
26596 }
26597
26598 check_explicit_instantiation_namespace (TYPE_NAME (t));
26599 mark_class_instantiated (t, extern_p);
26600
26601 if (nomem_p)
26602 return;
26603
26604 /* In contrast to implicit instantiation, where only the
26605 declarations, and not the definitions, of members are
26606 instantiated, we have here:
26607
26608 [temp.explicit]
26609
26610 An explicit instantiation that names a class template
26611 specialization is also an explicit instantiation of the same
26612 kind (declaration or definition) of each of its members (not
26613 including members inherited from base classes and members
26614 that are templates) that has not been previously explicitly
26615 specialized in the translation unit containing the explicit
26616 instantiation, provided that the associated constraints, if
26617 any, of that member are satisfied by the template arguments
26618 of the explicit instantiation. */
26619 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
26620 if ((VAR_P (fld)
26621 || (TREE_CODE (fld) == FUNCTION_DECL
26622 && !static_p
26623 && user_provided_p (fld)))
26624 && DECL_TEMPLATE_INSTANTIATION (fld)
26625 && constraints_satisfied_p (fld))
26626 {
26627 mark_decl_instantiated (result: fld, extern_p);
26628 if (! extern_p)
26629 instantiate_decl (fld, /*defer_ok=*/true,
26630 /*expl_inst_class_mem_p=*/true);
26631 }
26632 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
26633 {
26634 tree type = TREE_TYPE (fld);
26635
26636 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26637 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
26638 do_type_instantiation (t: type, storage, complain: 0);
26639 }
26640}
26641
26642/* Given a function DECL, which is a specialization of TMPL, modify
26643 DECL to be a re-instantiation of TMPL with the same template
26644 arguments. TMPL should be the template into which tsubst'ing
26645 should occur for DECL, not the most general template.
26646
26647 One reason for doing this is a scenario like this:
26648
26649 template <class T>
26650 void f(const T&, int i);
26651
26652 void g() { f(3, 7); }
26653
26654 template <class T>
26655 void f(const T& t, const int i) { }
26656
26657 Note that when the template is first instantiated, with
26658 instantiate_template, the resulting DECL will have no name for the
26659 first parameter, and the wrong type for the second. So, when we go
26660 to instantiate the DECL, we regenerate it. */
26661
26662static void
26663regenerate_decl_from_template (tree decl, tree tmpl, tree args)
26664{
26665 /* The arguments used to instantiate DECL, from the most general
26666 template. */
26667 tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
26668
26669 /* Make sure that we can see identifiers, and compute access correctly. */
26670 push_access_scope (t: decl);
26671
26672 if (TREE_CODE (decl) == FUNCTION_DECL)
26673 {
26674 tree specs;
26675 int args_depth;
26676 int parms_depth;
26677
26678 /* Don't bother with this for unique friends that can't be redeclared and
26679 might change type if regenerated (PR69836). */
26680 if (DECL_UNIQUE_FRIEND_P (decl))
26681 goto done;
26682
26683 /* Use the source location of the definition. */
26684 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
26685
26686 args_depth = TMPL_ARGS_DEPTH (args);
26687 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
26688 if (args_depth > parms_depth)
26689 args = get_innermost_template_args (args, n: parms_depth);
26690
26691 /* Instantiate a dynamic exception-specification. noexcept will be
26692 handled below. */
26693 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
26694 if (TREE_VALUE (raises))
26695 {
26696 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
26697 args, complain: tf_error, NULL_TREE,
26698 /*defer_ok*/false);
26699 if (specs && specs != error_mark_node)
26700 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
26701 specs);
26702 }
26703
26704 /* Merge parameter declarations. */
26705 if (tree pattern_parm
26706 = skip_artificial_parms_for (code_pattern,
26707 DECL_ARGUMENTS (code_pattern)))
26708 {
26709 tree *p = &DECL_ARGUMENTS (decl);
26710 for (int skip = num_artificial_parms_for (decl); skip; --skip)
26711 p = &DECL_CHAIN (*p);
26712 *p = tsubst_decl (t: pattern_parm, args, complain: tf_error);
26713 for (tree t = *p; t; t = DECL_CHAIN (t))
26714 DECL_CONTEXT (t) = decl;
26715 }
26716
26717 if (DECL_CONTRACTS (decl))
26718 {
26719 /* If we're regenerating a specialization, the contracts will have
26720 been copied from the most general template. Replace those with
26721 the ones from the actual specialization. */
26722 tree tmpl = DECL_TI_TEMPLATE (decl);
26723 if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
26724 {
26725 remove_contract_attributes (decl);
26726 copy_contract_attributes (decl, code_pattern);
26727 }
26728
26729 tsubst_contract_attributes (decl, args, complain: tf_warning_or_error, in_decl: code_pattern);
26730 }
26731
26732 /* Merge additional specifiers from the CODE_PATTERN. */
26733 if (DECL_DECLARED_INLINE_P (code_pattern)
26734 && !DECL_DECLARED_INLINE_P (decl))
26735 DECL_DECLARED_INLINE_P (decl) = 1;
26736
26737 maybe_instantiate_noexcept (decl, tf_error);
26738 }
26739 else if (VAR_P (decl))
26740 {
26741 start_lambda_scope (decl);
26742 DECL_INITIAL (decl) =
26743 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
26744 complain: tf_error, DECL_TI_TEMPLATE (decl));
26745 finish_lambda_scope ();
26746 if (VAR_HAD_UNKNOWN_BOUND (decl))
26747 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
26748 complain: tf_error, DECL_TI_TEMPLATE (decl));
26749 }
26750 else
26751 gcc_unreachable ();
26752
26753 done:
26754 pop_access_scope (t: decl);
26755}
26756
26757/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26758 substituted to get DECL. */
26759
26760tree
26761template_for_substitution (tree decl)
26762{
26763 tree tmpl = DECL_TI_TEMPLATE (decl);
26764
26765 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26766 for the instantiation. This is not always the most general
26767 template. Consider, for example:
26768
26769 template <class T>
26770 struct S { template <class U> void f();
26771 template <> void f<int>(); };
26772
26773 and an instantiation of S<double>::f<int>. We want TD to be the
26774 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26775 while (/* An instantiation cannot have a definition, so we need a
26776 more general template. */
26777 DECL_TEMPLATE_INSTANTIATION (tmpl)
26778 /* We must also deal with friend templates. Given:
26779
26780 template <class T> struct S {
26781 template <class U> friend void f() {};
26782 };
26783
26784 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26785 so far as the language is concerned, but that's still
26786 where we get the pattern for the instantiation from. On
26787 other hand, if the definition comes outside the class, say:
26788
26789 template <class T> struct S {
26790 template <class U> friend void f();
26791 };
26792 template <class U> friend void f() {}
26793
26794 we don't need to look any further. That's what the check for
26795 DECL_INITIAL is for. */
26796 || (TREE_CODE (decl) == FUNCTION_DECL
26797 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
26798 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
26799 {
26800 /* The present template, TD, should not be a definition. If it
26801 were a definition, we should be using it! Note that we
26802 cannot restructure the loop to just keep going until we find
26803 a template with a definition, since that might go too far if
26804 a specialization was declared, but not defined. */
26805
26806 /* Fetch the more general template. */
26807 tmpl = DECL_TI_TEMPLATE (tmpl);
26808 }
26809
26810 return tmpl;
26811}
26812
26813/* Returns true if we need to instantiate this template instance even if we
26814 know we aren't going to emit it. */
26815
26816bool
26817always_instantiate_p (tree decl)
26818{
26819 /* We always instantiate inline functions so that we can inline them. An
26820 explicit instantiation declaration prohibits implicit instantiation of
26821 non-inline functions. With high levels of optimization, we would
26822 normally inline non-inline functions -- but we're not allowed to do
26823 that for "extern template" functions. Therefore, we check
26824 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26825 return ((TREE_CODE (decl) == FUNCTION_DECL
26826 && (DECL_DECLARED_INLINE_P (decl)
26827 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
26828 /* And we need to instantiate static data members so that
26829 their initializers are available in integral constant
26830 expressions. */
26831 || (VAR_P (decl)
26832 && decl_maybe_constant_var_p (decl)));
26833}
26834
26835/* If FN has a noexcept-specifier that hasn't been instantiated yet,
26836 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26837 error, true otherwise. */
26838
26839bool
26840maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
26841{
26842 if (fn == error_mark_node)
26843 return false;
26844
26845 /* Don't instantiate a noexcept-specification from template context. */
26846 if (processing_template_decl
26847 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
26848 return true;
26849
26850 tree fntype = TREE_TYPE (fn);
26851 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
26852
26853 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
26854 && DECL_MAYBE_DELETED (fn))
26855 {
26856 if (fn == current_function_decl)
26857 /* We're in start_preparsed_function, keep going. */
26858 return true;
26859
26860 ++function_depth;
26861 maybe_synthesize_method (fn);
26862 --function_depth;
26863 return !DECL_DELETED_FN (fn);
26864 }
26865
26866 if (!spec || !TREE_PURPOSE (spec))
26867 return true;
26868
26869 tree noex = TREE_PURPOSE (spec);
26870 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26871 && TREE_CODE (noex) != DEFERRED_PARSE)
26872 return true;
26873
26874 tree orig_fn = NULL_TREE;
26875 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26876 its FUNCTION_DECL for the rest of this function -- push_access_scope
26877 doesn't accept TEMPLATE_DECLs. */
26878 if (DECL_FUNCTION_TEMPLATE_P (fn))
26879 {
26880 orig_fn = fn;
26881 fn = DECL_TEMPLATE_RESULT (fn);
26882 }
26883
26884 if (DECL_CLONED_FUNCTION_P (fn))
26885 {
26886 tree prime = DECL_CLONED_FUNCTION (fn);
26887 if (!maybe_instantiate_noexcept (fn: prime, complain))
26888 return false;
26889 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
26890 }
26891 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
26892 {
26893 static hash_set<tree>* fns = new hash_set<tree>;
26894 bool added = false;
26895 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
26896 {
26897 spec = get_defaulted_eh_spec (fn, complain);
26898 if (spec == error_mark_node)
26899 /* This might have failed because of an unparsed DMI, so
26900 let's try again later. */
26901 return false;
26902 }
26903 else if (!(added = !fns->add (k: fn)))
26904 {
26905 /* If hash_set::add returns true, the element was already there. */
26906 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
26907 DECL_SOURCE_LOCATION (fn));
26908 error_at (loc,
26909 "exception specification of %qD depends on itself",
26910 fn);
26911 spec = noexcept_false_spec;
26912 }
26913 else if (push_tinst_level (d: fn))
26914 {
26915 const bool push_to_top = maybe_push_to_top_level (fn);
26916 push_access_scope (t: fn);
26917 push_deferring_access_checks (dk_no_deferred);
26918 input_location = DECL_SOURCE_LOCATION (fn);
26919
26920 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
26921 && !DECL_LOCAL_DECL_P (fn))
26922 {
26923 /* If needed, set current_class_ptr for the benefit of
26924 tsubst_copy/PARM_DECL. */
26925 tree this_parm = DECL_ARGUMENTS (fn);
26926 current_class_ptr = NULL_TREE;
26927 current_class_ref = cp_build_fold_indirect_ref (this_parm);
26928 current_class_ptr = this_parm;
26929 }
26930
26931 /* If this function is represented by a TEMPLATE_DECL, then
26932 the deferred noexcept-specification might still contain
26933 dependent types, even after substitution. And we need the
26934 dependency check functions to work in build_noexcept_spec. */
26935 if (orig_fn)
26936 ++processing_template_decl;
26937
26938 /* Do deferred instantiation of the noexcept-specifier. */
26939 noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
26940 DEFERRED_NOEXCEPT_ARGS (noex),
26941 complain: tf_warning_or_error, in_decl: fn);
26942 /* Build up the noexcept-specification. */
26943 spec = build_noexcept_spec (noex, tf_warning_or_error);
26944
26945 if (orig_fn)
26946 --processing_template_decl;
26947
26948 pop_deferring_access_checks ();
26949 pop_access_scope (t: fn);
26950 pop_tinst_level ();
26951 maybe_pop_from_top_level (push_to_top);
26952 }
26953 else
26954 spec = noexcept_false_spec;
26955
26956 if (added)
26957 fns->remove (k: fn);
26958 }
26959
26960 if (spec == error_mark_node)
26961 {
26962 /* This failed with a hard error, so let's go with false. */
26963 gcc_assert (seen_error ());
26964 spec = noexcept_false_spec;
26965 }
26966
26967 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
26968 if (orig_fn)
26969 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
26970
26971 return true;
26972}
26973
26974/* We're starting to process the function INST, an instantiation of PATTERN;
26975 add their parameters to local_specializations. */
26976
26977void
26978register_parameter_specializations (tree pattern, tree inst)
26979{
26980 tree tmpl_parm = DECL_ARGUMENTS (pattern);
26981 tree spec_parm = DECL_ARGUMENTS (inst);
26982 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
26983 {
26984 register_local_specialization (spec: spec_parm, tmpl: tmpl_parm);
26985 spec_parm = skip_artificial_parms_for (inst, spec_parm);
26986 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
26987 }
26988 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
26989 {
26990 if (!DECL_PACK_P (tmpl_parm))
26991 {
26992 register_local_specialization (spec: spec_parm, tmpl: tmpl_parm);
26993 spec_parm = DECL_CHAIN (spec_parm);
26994 }
26995 else
26996 {
26997 /* Register the (value) argument pack as a specialization of
26998 TMPL_PARM, then move on. */
26999 tree argpack = extract_fnparm_pack (tmpl_parm, spec_p: &spec_parm);
27000 register_local_specialization (spec: argpack, tmpl: tmpl_parm);
27001 }
27002 }
27003 gcc_assert (!spec_parm);
27004}
27005
27006/* Instantiate the body of D using PATTERN with ARGS. We have
27007 already determined PATTERN is the correct template to use.
27008 NESTED_P is true if this is a nested function, in which case
27009 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
27010
27011static void
27012instantiate_body (tree pattern, tree args, tree d, bool nested_p)
27013{
27014 tree td = NULL_TREE;
27015 tree code_pattern = pattern;
27016
27017 if (!nested_p)
27018 {
27019 td = pattern;
27020 code_pattern = DECL_TEMPLATE_RESULT (td);
27021 }
27022 else
27023 /* Only OMP reductions are nested. */
27024 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
27025
27026 vec<tree> omp_privatization_save;
27027 if (current_function_decl)
27028 save_omp_privatization_clauses (omp_privatization_save);
27029
27030 bool push_to_top = maybe_push_to_top_level (d);
27031
27032 mark_template_arguments_used (tmpl: pattern, args);
27033
27034 if (VAR_P (d))
27035 {
27036 /* The variable might be a lambda's extra scope, and that
27037 lambda's visibility depends on D's. */
27038 maybe_commonize_var (d);
27039 determine_visibility (d);
27040 }
27041
27042 /* Mark D as instantiated so that recursive calls to
27043 instantiate_decl do not try to instantiate it again. */
27044 DECL_TEMPLATE_INSTANTIATED (d) = 1;
27045
27046 if (td)
27047 /* Regenerate the declaration in case the template has been modified
27048 by a subsequent redeclaration. */
27049 regenerate_decl_from_template (decl: d, tmpl: td, args);
27050
27051 /* We already set the file and line above. Reset them now in case
27052 they changed as a result of calling regenerate_decl_from_template. */
27053 input_location = DECL_SOURCE_LOCATION (d);
27054
27055 if (VAR_P (d))
27056 {
27057 /* Clear out DECL_RTL; whatever was there before may not be right
27058 since we've reset the type of the declaration. */
27059 SET_DECL_RTL (d, NULL);
27060 DECL_IN_AGGR_P (d) = 0;
27061
27062 /* The initializer is placed in DECL_INITIAL by
27063 regenerate_decl_from_template so we don't need to
27064 push/pop_access_scope again here. Pull it out so that
27065 cp_finish_decl can process it. */
27066 bool const_init = false;
27067 tree init = DECL_INITIAL (d);
27068 DECL_INITIAL (d) = NULL_TREE;
27069 DECL_INITIALIZED_P (d) = 0;
27070
27071 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
27072 initializer. That function will defer actual emission until
27073 we have a chance to determine linkage. */
27074 DECL_EXTERNAL (d) = 0;
27075
27076 /* Enter the scope of D so that access-checking works correctly. */
27077 bool enter_context = DECL_CLASS_SCOPE_P (d);
27078 if (enter_context)
27079 push_nested_class (DECL_CONTEXT (d));
27080
27081 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
27082 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
27083
27084 if (enter_context)
27085 pop_nested_class ();
27086 }
27087 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
27088 synthesize_method (d);
27089 else if (TREE_CODE (d) == FUNCTION_DECL)
27090 {
27091 /* Set up the list of local specializations. */
27092 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
27093 tree block = NULL_TREE;
27094
27095 /* Set up context. */
27096 if (nested_p)
27097 block = push_stmt_list ();
27098 else
27099 {
27100 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
27101
27102 perform_instantiation_time_access_checks (tmpl: code_pattern, targs: args);
27103 }
27104
27105 /* Create substitution entries for the parameters. */
27106 register_parameter_specializations (pattern: code_pattern, inst: d);
27107
27108 /* Substitute into the body of the function. */
27109 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27110 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
27111 complain: tf_warning_or_error, in_decl: d);
27112 else
27113 {
27114 tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
27115 complain: tf_warning_or_error, DECL_TI_TEMPLATE (d));
27116
27117 /* Set the current input_location to the end of the function
27118 so that finish_function knows where we are. */
27119 input_location
27120 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
27121
27122 /* Remember if we saw an infinite loop in the template. */
27123 current_function_infinite_loop
27124 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
27125 }
27126
27127 /* Finish the function. */
27128 if (nested_p)
27129 DECL_SAVED_TREE (d) = pop_stmt_list (block);
27130 else
27131 {
27132 d = finish_function (/*inline_p=*/false);
27133 expand_or_defer_fn (d);
27134 }
27135
27136 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27137 cp_check_omp_declare_reduction (d);
27138 }
27139
27140 /* We're not deferring instantiation any more. */
27141 if (!nested_p)
27142 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
27143
27144 maybe_pop_from_top_level (push_to_top);
27145
27146 if (current_function_decl)
27147 restore_omp_privatization_clauses (omp_privatization_save);
27148}
27149
27150/* Produce the definition of D, a _DECL generated from a template. If
27151 DEFER_OK is true, then we don't have to actually do the
27152 instantiation now; we just have to do it sometime. Normally it is
27153 an error if this is an explicit instantiation but D is undefined.
27154 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
27155 instantiated class template. */
27156
27157tree
27158instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
27159{
27160 tree tmpl = DECL_TI_TEMPLATE (d);
27161 tree gen_args;
27162 tree args;
27163 tree td;
27164 tree code_pattern;
27165 tree spec;
27166 tree gen_tmpl;
27167 bool pattern_defined;
27168 location_t saved_loc = input_location;
27169 bool external_p;
27170 bool deleted_p;
27171
27172 /* This function should only be used to instantiate templates for
27173 functions and static member variables. */
27174 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
27175
27176 /* A concept is never instantiated. */
27177 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
27178
27179 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
27180
27181 if (modules_p ())
27182 /* We may have a pending instantiation of D itself. */
27183 lazy_load_pendings (decl: d);
27184
27185 /* Variables are never deferred; if instantiation is required, they
27186 are instantiated right away. That allows for better code in the
27187 case that an expression refers to the value of the variable --
27188 if the variable has a constant value the referring expression can
27189 take advantage of that fact. */
27190 if (VAR_P (d))
27191 defer_ok = false;
27192
27193 /* Don't instantiate cloned functions. Instead, instantiate the
27194 functions they cloned. */
27195 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
27196 d = DECL_CLONED_FUNCTION (d);
27197
27198 if (DECL_TEMPLATE_INSTANTIATED (d)
27199 || TREE_TYPE (d) == error_mark_node
27200 || (TREE_CODE (d) == FUNCTION_DECL
27201 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
27202 || DECL_TEMPLATE_SPECIALIZATION (d))
27203 /* D has already been instantiated or explicitly specialized, so
27204 there's nothing for us to do here.
27205
27206 It might seem reasonable to check whether or not D is an explicit
27207 instantiation, and, if so, stop here. But when an explicit
27208 instantiation is deferred until the end of the compilation,
27209 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
27210 the instantiation. */
27211 return d;
27212
27213 /* Check to see whether we know that this template will be
27214 instantiated in some other file, as with "extern template"
27215 extension. */
27216 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
27217
27218 /* In general, we do not instantiate such templates. */
27219 if (external_p && !always_instantiate_p (decl: d))
27220 return d;
27221
27222 gen_tmpl = most_general_template (decl: tmpl);
27223 gen_args = DECL_TI_ARGS (d);
27224
27225 /* We should already have the extra args. */
27226 gcc_checking_assert (tmpl == gen_tmpl
27227 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
27228 == TMPL_ARGS_DEPTH (gen_args)));
27229 /* And what's in the hash table should match D. */
27230 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
27231 == d
27232 || spec == NULL_TREE);
27233
27234 /* This needs to happen before any tsubsting. */
27235 if (! push_tinst_level (d))
27236 return d;
27237
27238 auto_timevar tv (TV_TEMPLATE_INST);
27239
27240 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
27241 for the instantiation. */
27242 td = template_for_substitution (decl: d);
27243 args = gen_args;
27244
27245 if (variable_template_specialization_p (t: d))
27246 {
27247 /* Look up an explicit specialization, if any. */
27248 tree partial_ti = most_specialized_partial_spec (target: d, complain: tf_warning_or_error);
27249 if (partial_ti && partial_ti != error_mark_node)
27250 {
27251 td = TI_TEMPLATE (partial_ti);
27252 args = TI_ARGS (partial_ti);
27253 }
27254 }
27255
27256 code_pattern = DECL_TEMPLATE_RESULT (td);
27257
27258 /* We should never be trying to instantiate a member of a class
27259 template or partial specialization. */
27260 gcc_assert (d != code_pattern);
27261
27262 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
27263 || DECL_TEMPLATE_SPECIALIZATION (td))
27264 /* In the case of a friend template whose definition is provided
27265 outside the class, we may have too many arguments. Drop the
27266 ones we don't need. The same is true for specializations. */
27267 args = get_innermost_template_args
27268 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
27269
27270 if (TREE_CODE (d) == FUNCTION_DECL)
27271 {
27272 deleted_p = DECL_DELETED_FN (code_pattern);
27273 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
27274 && DECL_INITIAL (code_pattern) != error_mark_node)
27275 || DECL_DEFAULTED_FN (code_pattern)
27276 || deleted_p);
27277 }
27278 else
27279 {
27280 deleted_p = false;
27281 if (DECL_CLASS_SCOPE_P (code_pattern))
27282 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
27283 else
27284 pattern_defined = ! DECL_EXTERNAL (code_pattern);
27285 }
27286
27287 /* We may be in the middle of deferred access check. Disable it now. */
27288 push_deferring_access_checks (dk_no_deferred);
27289
27290 /* Unless an explicit instantiation directive has already determined
27291 the linkage of D, remember that a definition is available for
27292 this entity. */
27293 if (pattern_defined
27294 && !DECL_INTERFACE_KNOWN (d)
27295 && !DECL_NOT_REALLY_EXTERN (d))
27296 mark_definable (decl: d);
27297
27298 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
27299 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
27300 input_location = DECL_SOURCE_LOCATION (d);
27301
27302 /* If D is a member of an explicitly instantiated class template,
27303 and no definition is available, treat it like an implicit
27304 instantiation. */
27305 if (!pattern_defined && expl_inst_class_mem_p
27306 && DECL_EXPLICIT_INSTANTIATION (d))
27307 {
27308 /* Leave linkage flags alone on instantiations with anonymous
27309 visibility. */
27310 if (TREE_PUBLIC (d))
27311 {
27312 DECL_NOT_REALLY_EXTERN (d) = 0;
27313 DECL_INTERFACE_KNOWN (d) = 0;
27314 }
27315 SET_DECL_IMPLICIT_INSTANTIATION (d);
27316 }
27317
27318 /* Defer all other templates, unless we have been explicitly
27319 forbidden from doing so. */
27320 if (/* If there is no definition, we cannot instantiate the
27321 template. */
27322 ! pattern_defined
27323 /* If it's OK to postpone instantiation, do so. */
27324 || defer_ok
27325 /* If this is a static data member that will be defined
27326 elsewhere, we don't want to instantiate the entire data
27327 member, but we do want to instantiate the initializer so that
27328 we can substitute that elsewhere. */
27329 || (external_p && VAR_P (d))
27330 /* Handle here a deleted function too, avoid generating
27331 its body (c++/61080). */
27332 || deleted_p)
27333 {
27334 /* The definition of the static data member is now required so
27335 we must substitute the initializer. */
27336 if (VAR_P (d)
27337 && !DECL_INITIAL (d)
27338 && DECL_INITIAL (code_pattern))
27339 {
27340 tree ns;
27341 tree init;
27342 bool const_init = false;
27343 bool enter_context = DECL_CLASS_SCOPE_P (d);
27344
27345 ns = decl_namespace_context (d);
27346 push_nested_namespace (ns);
27347 if (enter_context)
27348 push_nested_class (DECL_CONTEXT (d));
27349 init = tsubst_expr (DECL_INITIAL (code_pattern),
27350 args,
27351 complain: tf_warning_or_error, NULL_TREE);
27352 /* If instantiating the initializer involved instantiating this
27353 again, don't call cp_finish_decl twice. */
27354 if (!DECL_INITIAL (d))
27355 {
27356 /* Make sure the initializer is still constant, in case of
27357 circular dependency (template/instantiate6.C). */
27358 const_init
27359 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
27360 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
27361 /*asmspec_tree=*/NULL_TREE, 0);
27362 }
27363 if (enter_context)
27364 pop_nested_class ();
27365 pop_nested_namespace (ns);
27366 }
27367
27368 /* We restore the source position here because it's used by
27369 add_pending_template. */
27370 input_location = saved_loc;
27371
27372 if (at_eof && !pattern_defined
27373 && DECL_EXPLICIT_INSTANTIATION (d)
27374 && DECL_NOT_REALLY_EXTERN (d))
27375 /* [temp.explicit]
27376
27377 The definition of a non-exported function template, a
27378 non-exported member function template, or a non-exported
27379 member function or static data member of a class template
27380 shall be present in every translation unit in which it is
27381 explicitly instantiated. */
27382 permerror (input_location, "explicit instantiation of %qD "
27383 "but no definition available", d);
27384
27385 /* If we're in unevaluated context, we just wanted to get the
27386 constant value; this isn't an odr use, so don't queue
27387 a full instantiation. */
27388 if (!cp_unevaluated_operand
27389 /* ??? Historically, we have instantiated inline functions, even
27390 when marked as "extern template". */
27391 && !(external_p && VAR_P (d)))
27392 add_pending_template (d);
27393 }
27394 else
27395 {
27396 set_instantiating_module (d);
27397 if (variable_template_p (t: gen_tmpl))
27398 note_vague_linkage_variable (d);
27399 instantiate_body (pattern: td, args, d, nested_p: false);
27400 }
27401
27402 pop_deferring_access_checks ();
27403 pop_tinst_level ();
27404 input_location = saved_loc;
27405
27406 return d;
27407}
27408
27409/* Run through the list of templates that we wish we could
27410 instantiate, and instantiate any we can. RETRIES is the
27411 number of times we retry pending template instantiation. */
27412
27413void
27414instantiate_pending_templates (int retries)
27415{
27416 int reconsider;
27417 location_t saved_loc = input_location;
27418
27419 /* Instantiating templates may trigger vtable generation. This in turn
27420 may require further template instantiations. We place a limit here
27421 to avoid infinite loop. */
27422 if (pending_templates && retries >= max_tinst_depth)
27423 {
27424 tree decl = pending_templates->tinst->maybe_get_node ();
27425
27426 fatal_error (input_location,
27427 "template instantiation depth exceeds maximum of %d"
27428 " instantiating %q+D, possibly from virtual table generation"
27429 " (use %<-ftemplate-depth=%> to increase the maximum)",
27430 max_tinst_depth, decl);
27431 if (TREE_CODE (decl) == FUNCTION_DECL)
27432 /* Pretend that we defined it. */
27433 DECL_INITIAL (decl) = error_mark_node;
27434 return;
27435 }
27436
27437 do
27438 {
27439 struct pending_template **t = &pending_templates;
27440 struct pending_template *last = NULL;
27441 reconsider = 0;
27442 while (*t)
27443 {
27444 tree instantiation = reopen_tinst_level (level: (*t)->tinst);
27445 bool complete = false;
27446
27447 if (TYPE_P (instantiation))
27448 {
27449 if (!COMPLETE_TYPE_P (instantiation))
27450 {
27451 instantiate_class_template (type: instantiation);
27452 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
27453 for (tree fld = TYPE_FIELDS (instantiation);
27454 fld; fld = TREE_CHAIN (fld))
27455 if ((VAR_P (fld)
27456 || (TREE_CODE (fld) == FUNCTION_DECL
27457 && !DECL_ARTIFICIAL (fld)))
27458 && DECL_TEMPLATE_INSTANTIATION (fld))
27459 instantiate_decl (d: fld,
27460 /*defer_ok=*/false,
27461 /*expl_inst_class_mem_p=*/false);
27462
27463 if (COMPLETE_TYPE_P (instantiation))
27464 reconsider = 1;
27465 }
27466
27467 complete = COMPLETE_TYPE_P (instantiation);
27468 }
27469 else
27470 {
27471 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
27472 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
27473 {
27474 instantiation
27475 = instantiate_decl (d: instantiation,
27476 /*defer_ok=*/false,
27477 /*expl_inst_class_mem_p=*/false);
27478 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
27479 reconsider = 1;
27480 }
27481
27482 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
27483 || DECL_TEMPLATE_INSTANTIATED (instantiation));
27484 }
27485
27486 if (complete)
27487 {
27488 /* If INSTANTIATION has been instantiated, then we don't
27489 need to consider it again in the future. */
27490 struct pending_template *drop = *t;
27491 *t = (*t)->next;
27492 set_refcount_ptr (ptr&: drop->tinst);
27493 pending_template_freelist ().free (obj: drop);
27494 }
27495 else
27496 {
27497 last = *t;
27498 t = &(*t)->next;
27499 }
27500 tinst_depth = 0;
27501 set_refcount_ptr (ptr&: current_tinst_level);
27502 }
27503 last_pending_template = last;
27504 }
27505 while (reconsider);
27506
27507 input_location = saved_loc;
27508}
27509
27510/* Substitute ARGVEC into T, which is a list of initializers for
27511 either base class or a non-static data member. The TREE_PURPOSEs
27512 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27513 instantiate_decl. */
27514
27515static tree
27516tsubst_initializer_list (tree t, tree argvec)
27517{
27518 tree inits = NULL_TREE;
27519 tree target_ctor = error_mark_node;
27520
27521 for (; t; t = TREE_CHAIN (t))
27522 {
27523 tree decl;
27524 tree init;
27525 tree expanded_bases = NULL_TREE;
27526 tree expanded_arguments = NULL_TREE;
27527 int i, len = 1;
27528
27529 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
27530 {
27531 tree expr;
27532 tree arg;
27533
27534 /* Expand the base class expansion type into separate base
27535 classes. */
27536 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), args: argvec,
27537 complain: tf_warning_or_error,
27538 NULL_TREE);
27539 if (expanded_bases == error_mark_node)
27540 continue;
27541
27542 /* We'll be building separate TREE_LISTs of arguments for
27543 each base. */
27544 len = TREE_VEC_LENGTH (expanded_bases);
27545 expanded_arguments = make_tree_vec (len);
27546 for (i = 0; i < len; i++)
27547 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
27548
27549 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27550 expand each argument in the TREE_VALUE of t. */
27551 expr = make_node (EXPR_PACK_EXPANSION);
27552 PACK_EXPANSION_LOCAL_P (expr) = true;
27553 PACK_EXPANSION_PARAMETER_PACKS (expr) =
27554 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
27555
27556 if (TREE_VALUE (t) == void_type_node)
27557 /* VOID_TYPE_NODE is used to indicate
27558 value-initialization. */
27559 {
27560 for (i = 0; i < len; i++)
27561 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
27562 }
27563 else
27564 {
27565 /* Substitute parameter packs into each argument in the
27566 TREE_LIST. */
27567 in_base_initializer = 1;
27568 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
27569 {
27570 tree expanded_exprs;
27571
27572 /* Expand the argument. */
27573 tree value;
27574 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27575 value = TREE_VALUE (arg);
27576 else
27577 {
27578 value = expr;
27579 PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
27580 }
27581 expanded_exprs
27582 = tsubst_pack_expansion (t: value, args: argvec,
27583 complain: tf_warning_or_error,
27584 NULL_TREE);
27585 if (expanded_exprs == error_mark_node)
27586 continue;
27587
27588 /* Prepend each of the expanded expressions to the
27589 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27590 for (i = 0; i < len; i++)
27591 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27592 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
27593 TREE_VEC_ELT (expanded_arguments, i)
27594 = tree_cons (NULL_TREE,
27595 TREE_VEC_ELT (expanded_exprs, j),
27596 TREE_VEC_ELT (expanded_arguments, i));
27597 else
27598 TREE_VEC_ELT (expanded_arguments, i)
27599 = tree_cons (NULL_TREE,
27600 TREE_VEC_ELT (expanded_exprs, i),
27601 TREE_VEC_ELT (expanded_arguments, i));
27602 }
27603 in_base_initializer = 0;
27604
27605 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27606 since we built them backwards. */
27607 for (i = 0; i < len; i++)
27608 {
27609 TREE_VEC_ELT (expanded_arguments, i) =
27610 nreverse (TREE_VEC_ELT (expanded_arguments, i));
27611 }
27612 }
27613 }
27614
27615 for (i = 0; i < len; ++i)
27616 {
27617 if (expanded_bases)
27618 {
27619 decl = TREE_VEC_ELT (expanded_bases, i);
27620 decl = expand_member_init (decl);
27621 init = TREE_VEC_ELT (expanded_arguments, i);
27622 }
27623 else
27624 {
27625 tree tmp;
27626 if (TYPE_P (TREE_PURPOSE (t)))
27627 decl = tsubst (TREE_PURPOSE (t), args: argvec,
27628 complain: tf_warning_or_error, NULL_TREE);
27629 else
27630 decl = tsubst_expr (TREE_PURPOSE (t), args: argvec,
27631 complain: tf_warning_or_error, NULL_TREE);
27632
27633 decl = expand_member_init (decl);
27634 if (decl && !DECL_P (decl))
27635 in_base_initializer = 1;
27636
27637 init = TREE_VALUE (t);
27638 tmp = init;
27639 if (init != void_type_node)
27640 init = tsubst_expr (t: init, args: argvec,
27641 complain: tf_warning_or_error, NULL_TREE);
27642 if (init == NULL_TREE && tmp != NULL_TREE)
27643 /* If we had an initializer but it instantiated to nothing,
27644 value-initialize the object. This will only occur when
27645 the initializer was a pack expansion where the parameter
27646 packs used in that expansion were of length zero. */
27647 init = void_type_node;
27648 in_base_initializer = 0;
27649 }
27650
27651 if (target_ctor != error_mark_node
27652 && init != error_mark_node)
27653 {
27654 error ("mem-initializer for %qD follows constructor delegation",
27655 decl);
27656 return inits;
27657 }
27658 /* Look for a target constructor. */
27659 if (init != error_mark_node
27660 && decl && CLASS_TYPE_P (decl)
27661 && same_type_p (decl, current_class_type))
27662 {
27663 maybe_warn_cpp0x (str: CPP0X_DELEGATING_CTORS);
27664 if (inits)
27665 {
27666 error ("constructor delegation follows mem-initializer for %qD",
27667 TREE_PURPOSE (inits));
27668 continue;
27669 }
27670 target_ctor = init;
27671 }
27672
27673 if (decl)
27674 {
27675 init = build_tree_list (decl, init);
27676 /* Carry over the dummy TREE_TYPE node containing the source
27677 location. */
27678 TREE_TYPE (init) = TREE_TYPE (t);
27679 TREE_CHAIN (init) = inits;
27680 inits = init;
27681 }
27682 }
27683 }
27684 return inits;
27685}
27686
27687/* Instantiate an enumerated type. TAG is the template type, NEWTAG
27688 is the instantiation (which should have been created with
27689 start_enum) and ARGS are the template arguments to use. */
27690
27691static void
27692tsubst_enum (tree tag, tree newtag, tree args)
27693{
27694 tree e;
27695
27696 if (SCOPED_ENUM_P (newtag))
27697 begin_scope (sk_scoped_enum, newtag);
27698
27699 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
27700 {
27701 tree value;
27702 tree decl = TREE_VALUE (e);
27703
27704 /* Note that in a template enum, the TREE_VALUE is the
27705 CONST_DECL, not the corresponding INTEGER_CST. */
27706 value = tsubst_expr (DECL_INITIAL (decl),
27707 args, complain: tf_warning_or_error, NULL_TREE);
27708
27709 /* Give this enumeration constant the correct access. */
27710 set_current_access_from_decl (decl);
27711
27712 /* Actually build the enumerator itself. Here we're assuming that
27713 enumerators can't have dependent attributes. */
27714 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
27715 DECL_ATTRIBUTES (decl),
27716 DECL_SOURCE_LOCATION (decl));
27717 /* Attribute deprecated without an argument isn't sticky: it'll
27718 melt into a tree flag, so we need to propagate the flag here,
27719 since we just created a new enumerator. */
27720 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
27721 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
27722 }
27723
27724 if (SCOPED_ENUM_P (newtag))
27725 finish_scope ();
27726
27727 finish_enum_value_list (newtag);
27728 finish_enum (newtag);
27729
27730 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
27731 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
27732 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
27733 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
27734}
27735
27736/* DECL is a FUNCTION_DECL that is a template specialization. Return
27737 its type -- but without substituting the innermost set of template
27738 arguments. So, innermost set of template parameters will appear in
27739 the type. */
27740
27741tree
27742get_mostly_instantiated_function_type (tree decl)
27743{
27744 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27745 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
27746}
27747
27748/* Return truthvalue if we're processing a template different from
27749 the last one involved in diagnostics. */
27750bool
27751problematic_instantiation_changed (void)
27752{
27753 return current_tinst_level != last_error_tinst_level;
27754}
27755
27756/* Remember current template involved in diagnostics. */
27757void
27758record_last_problematic_instantiation (void)
27759{
27760 set_refcount_ptr (ptr&: last_error_tinst_level, obj: current_tinst_level);
27761}
27762
27763struct tinst_level *
27764current_instantiation (void)
27765{
27766 return current_tinst_level;
27767}
27768
27769/* Return TRUE if current_function_decl is being instantiated, false
27770 otherwise. */
27771
27772bool
27773instantiating_current_function_p (void)
27774{
27775 return (current_instantiation ()
27776 && (current_instantiation ()->maybe_get_node ()
27777 == current_function_decl));
27778}
27779
27780/* [temp.param] Check that template non-type parm TYPE is of an allowable
27781 type. Return false for ok, true for disallowed. Issue error and
27782 inform messages under control of COMPLAIN. */
27783
27784static bool
27785invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
27786{
27787 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
27788 return false;
27789 else if (TYPE_PTR_P (type))
27790 return false;
27791 else if (TYPE_REF_P (type)
27792 && !TYPE_REF_IS_RVALUE (type))
27793 return false;
27794 else if (TYPE_PTRMEM_P (type))
27795 return false;
27796 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
27797 {
27798 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
27799 {
27800 if (complain & tf_error)
27801 error ("non-type template parameters of deduced class type only "
27802 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27803 return true;
27804 }
27805 return false;
27806 }
27807 else if (TREE_CODE (type) == NULLPTR_TYPE)
27808 return false;
27809 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
27810 && cxx_dialect < cxx11)
27811 /* Fall through; before C++11 alias templates, a bound ttp
27812 always instantiates into a class type. */;
27813 else if (WILDCARD_TYPE_P (type))
27814 /* Any other wildcard type not already handled above is allowed. */
27815 return false;
27816 else if (TREE_CODE (type) == COMPLEX_TYPE)
27817 /* Fall through. */;
27818 else if (VOID_TYPE_P (type))
27819 /* Fall through. */;
27820 else if (cxx_dialect >= cxx20)
27821 {
27822 if (dependent_type_p (type))
27823 return false;
27824 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
27825 return true;
27826 if (structural_type_p (type))
27827 return false;
27828 if (complain & tf_error)
27829 {
27830 auto_diagnostic_group d;
27831 error ("%qT is not a valid type for a template non-type "
27832 "parameter because it is not structural", type);
27833 structural_type_p (type, true);
27834 }
27835 return true;
27836 }
27837 else if (CLASS_TYPE_P (type))
27838 {
27839 if (complain & tf_error)
27840 error ("non-type template parameters of class type only available "
27841 "with %<-std=c++20%> or %<-std=gnu++20%>");
27842 return true;
27843 }
27844
27845 if (complain & tf_error)
27846 {
27847 if (type == error_mark_node)
27848 inform (input_location, "invalid template non-type parameter");
27849 else
27850 error ("%q#T is not a valid type for a template non-type parameter",
27851 type);
27852 }
27853 return true;
27854}
27855
27856/* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27857
27858static bool
27859value_dependent_noexcept_spec_p (tree type)
27860{
27861 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
27862 if (tree noex = TREE_PURPOSE (spec))
27863 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27864 affect overload resolution and treating it as dependent breaks
27865 things. Same for an unparsed noexcept expression. */
27866 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
27867 && TREE_CODE (noex) != DEFERRED_PARSE
27868 && value_dependent_expression_p (noex))
27869 return true;
27870
27871 return false;
27872}
27873
27874/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27875 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27876
27877static bool
27878dependent_type_p_r (tree type)
27879{
27880 tree scope;
27881
27882 /* [temp.dep.type]
27883
27884 A type is dependent if it is:
27885
27886 -- a template parameter. Template template parameters are types
27887 for us (since TYPE_P holds true for them) so we handle
27888 them here. */
27889 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27890 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
27891 return true;
27892 /* -- a qualified-id with a nested-name-specifier which contains a
27893 class-name that names a dependent type or whose unqualified-id
27894 names a dependent type. */
27895 if (TREE_CODE (type) == TYPENAME_TYPE)
27896 return true;
27897
27898 /* An alias template specialization can be dependent even if the
27899 resulting type is not. */
27900 if (dependent_alias_template_spec_p (t: type, transparent_typedefs: nt_transparent))
27901 return true;
27902
27903 /* -- a cv-qualified type where the cv-unqualified type is
27904 dependent.
27905 No code is necessary for this bullet; the code below handles
27906 cv-qualified types, and we don't want to strip aliases with
27907 TYPE_MAIN_VARIANT because of DR 1558. */
27908 /* -- a compound type constructed from any dependent type. */
27909 if (TYPE_PTRMEM_P (type))
27910 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
27911 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27912 (type)));
27913 else if (INDIRECT_TYPE_P (type))
27914 return dependent_type_p (TREE_TYPE (type));
27915 else if (FUNC_OR_METHOD_TYPE_P (type))
27916 {
27917 tree arg_type;
27918
27919 if (dependent_type_p (TREE_TYPE (type)))
27920 return true;
27921 for (arg_type = TYPE_ARG_TYPES (type);
27922 arg_type;
27923 arg_type = TREE_CHAIN (arg_type))
27924 if (dependent_type_p (TREE_VALUE (arg_type)))
27925 return true;
27926 if (cxx_dialect >= cxx17
27927 && value_dependent_noexcept_spec_p (type))
27928 /* A value-dependent noexcept-specifier makes the type dependent. */
27929 return true;
27930 return false;
27931 }
27932 /* -- an array type constructed from any dependent type or whose
27933 size is specified by a constant expression that is
27934 value-dependent.
27935
27936 We checked for type- and value-dependence of the bounds in
27937 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27938 if (TREE_CODE (type) == ARRAY_TYPE)
27939 {
27940 if (TYPE_DOMAIN (type)
27941 && dependent_type_p (TYPE_DOMAIN (type)))
27942 return true;
27943 return dependent_type_p (TREE_TYPE (type));
27944 }
27945
27946 /* -- a template-id in which either the template name is a template
27947 parameter ... */
27948 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
27949 return true;
27950 /* ... or any of the template arguments is a dependent type or
27951 an expression that is type-dependent or value-dependent. */
27952 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
27953 && (any_dependent_template_arguments_p
27954 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
27955 return true;
27956
27957 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27958 dependent; if the argument of the `typeof' expression is not
27959 type-dependent, then it should already been have resolved. */
27960 if (TREE_CODE (type) == TYPEOF_TYPE
27961 || TREE_CODE (type) == DECLTYPE_TYPE
27962 || TREE_CODE (type) == TRAIT_TYPE)
27963 return true;
27964
27965 /* A template argument pack is dependent if any of its packed
27966 arguments are. */
27967 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
27968 {
27969 tree args = ARGUMENT_PACK_ARGS (type);
27970 for (tree arg : tree_vec_range (args))
27971 if (dependent_template_arg_p (arg))
27972 return true;
27973 }
27974
27975 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27976 be template parameters. */
27977 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
27978 return true;
27979
27980 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
27981 return true;
27982
27983 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
27984 return true;
27985
27986 /* The standard does not specifically mention types that are local
27987 to template functions or local classes, but they should be
27988 considered dependent too. For example:
27989
27990 template <int I> void f() {
27991 enum E { a = I };
27992 S<sizeof (E)> s;
27993 }
27994
27995 The size of `E' cannot be known until the value of `I' has been
27996 determined. Therefore, `E' must be considered dependent. */
27997 scope = TYPE_CONTEXT (type);
27998 if (scope && TYPE_P (scope))
27999 return dependent_type_p (scope);
28000 /* Don't use type_dependent_expression_p here, as it can lead
28001 to infinite recursion trying to determine whether a lambda
28002 nested in a lambda is dependent (c++/47687). */
28003 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
28004 && DECL_LANG_SPECIFIC (scope)
28005 && DECL_TEMPLATE_INFO (scope)
28006 && (any_dependent_template_arguments_p
28007 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
28008 return true;
28009
28010 /* Other types are non-dependent. */
28011 return false;
28012}
28013
28014/* Returns TRUE if TYPE is dependent, in the sense of
28015 [temp.dep.type]. Note that a NULL type is considered dependent. */
28016
28017bool
28018dependent_type_p (tree type)
28019{
28020 /* If there are no template parameters in scope, then there can't be
28021 any dependent types. */
28022 if (!processing_template_decl)
28023 {
28024 /* If we are not processing a template, then nobody should be
28025 providing us with a dependent type. */
28026 gcc_assert (type);
28027 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
28028 return false;
28029 }
28030
28031 /* If the type is NULL, we have not computed a type for the entity
28032 in question; in that case, the type is dependent. */
28033 if (!type)
28034 return true;
28035
28036 /* Erroneous types can be considered non-dependent. */
28037 if (type == error_mark_node)
28038 return false;
28039
28040 /* If we have not already computed the appropriate value for TYPE,
28041 do so now. */
28042 if (!TYPE_DEPENDENT_P_VALID (type))
28043 {
28044 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
28045 TYPE_DEPENDENT_P_VALID (type) = 1;
28046 }
28047
28048 return TYPE_DEPENDENT_P (type);
28049}
28050
28051/* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
28052 lookup. In other words, a dependent type that is not the current
28053 instantiation. */
28054
28055bool
28056dependent_scope_p (tree scope)
28057{
28058 return (scope && TYPE_P (scope) && dependent_type_p (type: scope)
28059 && !currently_open_class (scope));
28060}
28061
28062/* True if we might find more declarations in SCOPE during instantiation than
28063 we can when parsing the template. */
28064
28065bool
28066dependentish_scope_p (tree scope)
28067{
28068 return dependent_scope_p (scope) || any_dependent_bases_p (scope);
28069}
28070
28071/* T is a SCOPE_REF. Return whether it represents a non-static member of
28072 an unknown base of 'this' (and is therefore instantiation-dependent). */
28073
28074static bool
28075unknown_base_ref_p (tree t)
28076{
28077 if (!current_class_ptr)
28078 return false;
28079
28080 tree mem = TREE_OPERAND (t, 1);
28081 if (shared_member_p (mem))
28082 return false;
28083
28084 tree cur = current_nonlambda_class_type ();
28085 if (!any_dependent_bases_p (cur))
28086 return false;
28087
28088 tree ctx = TREE_OPERAND (t, 0);
28089 if (DERIVED_FROM_P (ctx, cur))
28090 return false;
28091
28092 return true;
28093}
28094
28095/* T is a SCOPE_REF; return whether we need to consider it
28096 instantiation-dependent so that we can check access at instantiation
28097 time even though we know which member it resolves to. */
28098
28099static bool
28100instantiation_dependent_scope_ref_p (tree t)
28101{
28102 if (DECL_P (TREE_OPERAND (t, 1))
28103 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
28104 && !dependent_scope_p (TREE_OPERAND (t, 0))
28105 && !unknown_base_ref_p (t)
28106 && accessible_in_template_p (TREE_OPERAND (t, 0),
28107 TREE_OPERAND (t, 1)))
28108 return false;
28109 else
28110 return true;
28111}
28112
28113/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
28114 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
28115 expression. */
28116
28117/* Note that this predicate is not appropriate for general expressions;
28118 only constant expressions (that satisfy potential_constant_expression)
28119 can be tested for value dependence. */
28120
28121bool
28122value_dependent_expression_p (tree expression)
28123{
28124 if (!processing_template_decl || expression == NULL_TREE)
28125 return false;
28126
28127 /* A type-dependent expression is also value-dependent. */
28128 if (type_dependent_expression_p (expression))
28129 return true;
28130
28131 switch (TREE_CODE (expression))
28132 {
28133 case BASELINK:
28134 /* A dependent member function of the current instantiation. */
28135 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
28136
28137 case FUNCTION_DECL:
28138 /* A dependent member function of the current instantiation. */
28139 if (DECL_CLASS_SCOPE_P (expression)
28140 && dependent_type_p (DECL_CONTEXT (expression)))
28141 return true;
28142 break;
28143
28144 case IDENTIFIER_NODE:
28145 /* A name that has not been looked up -- must be dependent. */
28146 return true;
28147
28148 case TEMPLATE_PARM_INDEX:
28149 /* A non-type template parm. */
28150 return true;
28151
28152 case CONST_DECL:
28153 /* A non-type template parm. */
28154 if (DECL_TEMPLATE_PARM_P (expression))
28155 return true;
28156 return value_dependent_expression_p (DECL_INITIAL (expression));
28157
28158 case VAR_DECL:
28159 /* A constant with literal type and is initialized
28160 with an expression that is value-dependent. */
28161 if (DECL_DEPENDENT_INIT_P (expression))
28162 return true;
28163 if (DECL_HAS_VALUE_EXPR_P (expression))
28164 {
28165 tree value_expr = DECL_VALUE_EXPR (expression);
28166 if (value_dependent_expression_p (expression: value_expr)
28167 /* __PRETTY_FUNCTION__ inside a template function is dependent
28168 on the name of the function. */
28169 || (DECL_PRETTY_FUNCTION_P (expression)
28170 /* It might be used in a template, but not a template
28171 function, in which case its DECL_VALUE_EXPR will be
28172 "top level". */
28173 && value_expr == error_mark_node))
28174 return true;
28175 }
28176 else if (TYPE_REF_P (TREE_TYPE (expression)))
28177 /* FIXME cp_finish_decl doesn't fold reference initializers. */
28178 return true;
28179 /* We have a constexpr variable and we're processing a template. When
28180 there's lifetime extension involved (for which finish_compound_literal
28181 used to create a temporary), we'll not be able to evaluate the
28182 variable until instantiating, so pretend it's value-dependent. */
28183 else if (DECL_DECLARED_CONSTEXPR_P (expression)
28184 && !TREE_CONSTANT (expression))
28185 return true;
28186 return false;
28187
28188 case DYNAMIC_CAST_EXPR:
28189 case STATIC_CAST_EXPR:
28190 case CONST_CAST_EXPR:
28191 case REINTERPRET_CAST_EXPR:
28192 case CAST_EXPR:
28193 case IMPLICIT_CONV_EXPR:
28194 /* These expressions are value-dependent if the type to which
28195 the cast occurs is dependent or the expression being casted
28196 is value-dependent. */
28197 {
28198 tree type = TREE_TYPE (expression);
28199
28200 if (dependent_type_p (type))
28201 return true;
28202
28203 /* A functional cast has a list of operands. */
28204 expression = TREE_OPERAND (expression, 0);
28205 if (!expression)
28206 {
28207 /* If there are no operands, it must be an expression such
28208 as "int()". This should not happen for aggregate types
28209 because it would form non-constant expressions. */
28210 gcc_assert (cxx_dialect >= cxx11
28211 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
28212
28213 return false;
28214 }
28215
28216 if (TREE_CODE (expression) == TREE_LIST)
28217 return any_value_dependent_elements_p (expression);
28218
28219 if (TREE_CODE (type) == REFERENCE_TYPE
28220 && has_value_dependent_address (op: expression))
28221 return true;
28222
28223 return value_dependent_expression_p (expression);
28224 }
28225
28226 case SIZEOF_EXPR:
28227 if (SIZEOF_EXPR_TYPE_P (expression))
28228 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
28229 /* FALLTHRU */
28230 case ALIGNOF_EXPR:
28231 case TYPEID_EXPR:
28232 /* A `sizeof' expression is value-dependent if the operand is
28233 type-dependent or is a pack expansion. */
28234 expression = TREE_OPERAND (expression, 0);
28235 if (PACK_EXPANSION_P (expression))
28236 return true;
28237 else if (TYPE_P (expression))
28238 return dependent_type_p (type: expression);
28239 return instantiation_dependent_uneval_expression_p (expression);
28240
28241 case AT_ENCODE_EXPR:
28242 /* An 'encode' expression is value-dependent if the operand is
28243 type-dependent. */
28244 expression = TREE_OPERAND (expression, 0);
28245 return dependent_type_p (type: expression);
28246
28247 case NOEXCEPT_EXPR:
28248 expression = TREE_OPERAND (expression, 0);
28249 return instantiation_dependent_uneval_expression_p (expression);
28250
28251 case SCOPE_REF:
28252 /* All instantiation-dependent expressions should also be considered
28253 value-dependent. */
28254 return instantiation_dependent_scope_ref_p (t: expression);
28255
28256 case COMPONENT_REF:
28257 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
28258 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
28259
28260 case NONTYPE_ARGUMENT_PACK:
28261 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
28262 is value-dependent. */
28263 for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
28264 if (value_dependent_expression_p (expression: arg))
28265 return true;
28266 return false;
28267
28268 case TRAIT_EXPR:
28269 {
28270 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
28271 return true;
28272
28273 tree type2 = TRAIT_EXPR_TYPE2 (expression);
28274 if (!type2)
28275 return false;
28276
28277 if (TREE_CODE (type2) != TREE_VEC)
28278 return dependent_type_p (type: type2);
28279
28280 for (tree arg : tree_vec_range (type2))
28281 if (dependent_type_p (type: arg))
28282 return true;
28283
28284 return false;
28285 }
28286
28287 case MODOP_EXPR:
28288 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
28289 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
28290
28291 case ARRAY_REF:
28292 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
28293 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
28294
28295 case ADDR_EXPR:
28296 {
28297 tree op = TREE_OPERAND (expression, 0);
28298 return (value_dependent_expression_p (expression: op)
28299 || has_value_dependent_address (op));
28300 }
28301
28302 case REQUIRES_EXPR:
28303 /* Treat all requires-expressions as value-dependent so
28304 we don't try to fold them. */
28305 return true;
28306
28307 case TYPE_REQ:
28308 return dependent_type_p (TREE_OPERAND (expression, 0));
28309
28310 case CALL_EXPR:
28311 {
28312 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
28313 return true;
28314 tree fn = get_callee_fndecl (expression);
28315 int i, nargs;
28316 nargs = call_expr_nargs (expression);
28317 for (i = 0; i < nargs; ++i)
28318 {
28319 tree op = CALL_EXPR_ARG (expression, i);
28320 /* In a call to a constexpr member function, look through the
28321 implicit ADDR_EXPR on the object argument so that it doesn't
28322 cause the call to be considered value-dependent. We also
28323 look through it in potential_constant_expression. */
28324 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
28325 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
28326 && TREE_CODE (op) == ADDR_EXPR)
28327 op = TREE_OPERAND (op, 0);
28328 if (value_dependent_expression_p (expression: op))
28329 return true;
28330 }
28331 return false;
28332 }
28333
28334 case TEMPLATE_ID_EXPR:
28335 return concept_definition_p (TREE_OPERAND (expression, 0))
28336 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
28337
28338 case CONSTRUCTOR:
28339 {
28340 unsigned ix;
28341 tree val;
28342 if (dependent_type_p (TREE_TYPE (expression)))
28343 return true;
28344 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
28345 if (value_dependent_expression_p (expression: val))
28346 return true;
28347 return false;
28348 }
28349
28350 case STMT_EXPR:
28351 /* Treat a GNU statement expression as dependent to avoid crashing
28352 under instantiate_non_dependent_expr; it can't be constant. */
28353 return true;
28354
28355 case NEW_EXPR:
28356 case VEC_NEW_EXPR:
28357 /* The second operand is a type, which type_dependent_expression_p
28358 (and therefore value_dependent_expression_p) doesn't want to see. */
28359 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
28360 || value_dependent_expression_p (TREE_OPERAND (expression, 2))
28361 || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
28362
28363 default:
28364 /* A constant expression is value-dependent if any subexpression is
28365 value-dependent. */
28366 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
28367 {
28368 case tcc_reference:
28369 case tcc_unary:
28370 case tcc_comparison:
28371 case tcc_binary:
28372 case tcc_expression:
28373 case tcc_vl_exp:
28374 {
28375 int i, len = cp_tree_operand_length (expression);
28376
28377 for (i = 0; i < len; i++)
28378 {
28379 tree t = TREE_OPERAND (expression, i);
28380
28381 /* In some cases, some of the operands may be missing.
28382 (For example, in the case of PREDECREMENT_EXPR, the
28383 amount to increment by may be missing.) That doesn't
28384 make the expression dependent. */
28385 if (t && value_dependent_expression_p (expression: t))
28386 return true;
28387 }
28388 }
28389 break;
28390 default:
28391 break;
28392 }
28393 break;
28394 }
28395
28396 /* The expression is not value-dependent. */
28397 return false;
28398}
28399
28400/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
28401 [temp.dep.expr]. Note that an expression with no type is
28402 considered dependent. Other parts of the compiler arrange for an
28403 expression with type-dependent subexpressions to have no type, so
28404 this function doesn't have to be fully recursive. */
28405
28406bool
28407type_dependent_expression_p (tree expression)
28408{
28409 if (!processing_template_decl)
28410 return false;
28411
28412 if (expression == NULL_TREE || expression == error_mark_node)
28413 return false;
28414
28415 gcc_checking_assert (!TYPE_P (expression));
28416
28417 STRIP_ANY_LOCATION_WRAPPER (expression);
28418
28419 /* An unresolved name is always dependent. */
28420 if (identifier_p (t: expression)
28421 || TREE_CODE (expression) == USING_DECL
28422 || TREE_CODE (expression) == WILDCARD_DECL)
28423 return true;
28424
28425 /* A lambda-expression in template context is dependent. dependent_type_p is
28426 true for a lambda in the scope of a class or function template, but that
28427 doesn't cover all template contexts, like a default template argument. */
28428 if (TREE_CODE (expression) == LAMBDA_EXPR)
28429 return true;
28430
28431 /* A fold expression is type-dependent. */
28432 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
28433 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
28434 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
28435 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
28436 return true;
28437
28438 /* Some expression forms are never type-dependent. */
28439 if (TREE_CODE (expression) == SIZEOF_EXPR
28440 || TREE_CODE (expression) == ALIGNOF_EXPR
28441 || TREE_CODE (expression) == AT_ENCODE_EXPR
28442 || TREE_CODE (expression) == NOEXCEPT_EXPR
28443 || TREE_CODE (expression) == TRAIT_EXPR
28444 || TREE_CODE (expression) == TYPEID_EXPR
28445 || TREE_CODE (expression) == DELETE_EXPR
28446 || TREE_CODE (expression) == VEC_DELETE_EXPR
28447 || TREE_CODE (expression) == THROW_EXPR
28448 || TREE_CODE (expression) == REQUIRES_EXPR)
28449 return false;
28450
28451 /* The types of these expressions depends only on the type to which
28452 the cast occurs. */
28453 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
28454 || TREE_CODE (expression) == STATIC_CAST_EXPR
28455 || TREE_CODE (expression) == CONST_CAST_EXPR
28456 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
28457 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
28458 || TREE_CODE (expression) == CAST_EXPR)
28459 return dependent_type_p (TREE_TYPE (expression));
28460
28461 /* The types of these expressions depends only on the type created
28462 by the expression. */
28463 if (TREE_CODE (expression) == NEW_EXPR
28464 || TREE_CODE (expression) == VEC_NEW_EXPR)
28465 {
28466 /* For NEW_EXPR tree nodes created inside a template, either
28467 the object type itself or a TREE_LIST may appear as the
28468 operand 1. */
28469 tree type = TREE_OPERAND (expression, 1);
28470 if (TREE_CODE (type) == TREE_LIST)
28471 /* This is an array type. We need to check array dimensions
28472 as well. */
28473 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
28474 || value_dependent_expression_p
28475 (TREE_OPERAND (TREE_VALUE (type), 1));
28476 /* Array type whose dimension has to be deduced. */
28477 else if (TREE_CODE (type) == ARRAY_TYPE
28478 && TREE_OPERAND (expression, 2) == NULL_TREE)
28479 return true;
28480 else
28481 return dependent_type_p (type);
28482 }
28483
28484 if (TREE_CODE (expression) == SCOPE_REF)
28485 {
28486 tree scope = TREE_OPERAND (expression, 0);
28487 tree name = TREE_OPERAND (expression, 1);
28488
28489 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28490 contains an identifier associated by name lookup with one or more
28491 declarations declared with a dependent type, or...a
28492 nested-name-specifier or qualified-id that names a member of an
28493 unknown specialization. */
28494 return (type_dependent_expression_p (expression: name)
28495 || dependent_scope_p (scope));
28496 }
28497
28498 if (TREE_CODE (expression) == TEMPLATE_DECL
28499 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
28500 return uses_outer_template_parms (decl: expression);
28501
28502 if (TREE_CODE (expression) == STMT_EXPR)
28503 expression = stmt_expr_value_expr (expression);
28504
28505 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
28506 {
28507 for (auto &elt : CONSTRUCTOR_ELTS (expression))
28508 if (type_dependent_expression_p (expression: elt.value))
28509 return true;
28510 return false;
28511 }
28512
28513 /* A static data member of the current instantiation with incomplete
28514 array type is type-dependent, as the definition and specializations
28515 can have different bounds. */
28516 if (VAR_P (expression)
28517 && DECL_CLASS_SCOPE_P (expression)
28518 && dependent_type_p (DECL_CONTEXT (expression))
28519 && VAR_HAD_UNKNOWN_BOUND (expression))
28520 return true;
28521
28522 /* An array of unknown bound depending on a variadic parameter, eg:
28523
28524 template<typename... Args>
28525 void foo (Args... args)
28526 {
28527 int arr[] = { args... };
28528 }
28529
28530 template<int... vals>
28531 void bar ()
28532 {
28533 int arr[] = { vals... };
28534 }
28535
28536 If the array has no length and has an initializer, it must be that
28537 we couldn't determine its length in cp_complete_array_type because
28538 it is dependent. */
28539 if (((VAR_P (expression) && DECL_INITIAL (expression))
28540 || COMPOUND_LITERAL_P (expression))
28541 && TREE_TYPE (expression) != NULL_TREE
28542 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
28543 && !TYPE_DOMAIN (TREE_TYPE (expression)))
28544 return true;
28545
28546 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28547 if (BASELINK_P (expression))
28548 {
28549 if (BASELINK_OPTYPE (expression)
28550 && dependent_type_p (BASELINK_OPTYPE (expression)))
28551 return true;
28552 expression = BASELINK_FUNCTIONS (expression);
28553 }
28554
28555 /* A function or variable template-id is type-dependent if it has any
28556 dependent template arguments. */
28557 if (VAR_OR_FUNCTION_DECL_P (expression)
28558 && DECL_LANG_SPECIFIC (expression)
28559 && DECL_TEMPLATE_INFO (expression))
28560 {
28561 /* Consider the innermost template arguments, since those are the ones
28562 that come from the template-id; the template arguments for the
28563 enclosing class do not make it type-dependent unless they are used in
28564 the type of the decl. */
28565 if (instantiates_primary_template_p (node: expression)
28566 && (any_dependent_template_arguments_p
28567 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
28568 return true;
28569 }
28570
28571 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28572 type-dependent. Checking this is important for functions with auto return
28573 type, which looks like a dependent type. */
28574 if (TREE_CODE (expression) == FUNCTION_DECL
28575 && !(DECL_CLASS_SCOPE_P (expression)
28576 && dependent_type_p (DECL_CONTEXT (expression)))
28577 && !(DECL_LANG_SPECIFIC (expression)
28578 && DECL_UNIQUE_FRIEND_P (expression)
28579 && (!DECL_FRIEND_CONTEXT (expression)
28580 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
28581 && !DECL_LOCAL_DECL_P (expression))
28582 {
28583 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
28584 || undeduced_auto_decl (expression));
28585 return false;
28586 }
28587
28588 /* Otherwise, its constraints could still depend on outer template parameters
28589 from its (dependent) scope. */
28590 if (TREE_CODE (expression) == FUNCTION_DECL
28591 /* As an optimization, check this cheaper sufficient condition first.
28592 (At this point we've established that we're looking at a member of
28593 a dependent class, so it makes sense to start treating say undeduced
28594 auto as dependent.) */
28595 && !dependent_type_p (TREE_TYPE (expression))
28596 && uses_outer_template_parms_in_constraints (decl: expression))
28597 return true;
28598
28599 /* Always dependent, on the number of arguments if nothing else. */
28600 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
28601 return true;
28602
28603 if (TREE_TYPE (expression) == unknown_type_node)
28604 {
28605 if (TREE_CODE (expression) == ADDR_EXPR)
28606 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
28607 if (TREE_CODE (expression) == COMPONENT_REF
28608 || TREE_CODE (expression) == OFFSET_REF)
28609 {
28610 if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
28611 return true;
28612 expression = TREE_OPERAND (expression, 1);
28613 if (identifier_p (t: expression))
28614 return false;
28615 }
28616 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28617 if (TREE_CODE (expression) == SCOPE_REF)
28618 return false;
28619
28620 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28621 if (TREE_CODE (expression) == CO_AWAIT_EXPR
28622 || TREE_CODE (expression) == CO_YIELD_EXPR)
28623 return true;
28624
28625 if (BASELINK_P (expression))
28626 {
28627 if (BASELINK_OPTYPE (expression)
28628 && dependent_type_p (BASELINK_OPTYPE (expression)))
28629 return true;
28630 expression = BASELINK_FUNCTIONS (expression);
28631 }
28632
28633 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
28634 {
28635 if (any_dependent_template_arguments_p
28636 (TREE_OPERAND (expression, 1)))
28637 return true;
28638 expression = TREE_OPERAND (expression, 0);
28639 if (identifier_p (t: expression))
28640 return true;
28641 }
28642
28643 gcc_assert (OVL_P (expression));
28644
28645 for (lkp_iterator iter (expression); iter; ++iter)
28646 if (type_dependent_expression_p (expression: *iter))
28647 return true;
28648
28649 return false;
28650 }
28651
28652 /* The type of a non-type template parm declared with a placeholder type
28653 depends on the corresponding template argument, even though
28654 placeholders are not normally considered dependent. */
28655 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
28656 && is_auto (TREE_TYPE (expression)))
28657 return true;
28658
28659 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
28660
28661 /* Dependent type attributes might not have made it from the decl to
28662 the type yet. */
28663 if (DECL_P (expression)
28664 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
28665 return true;
28666
28667 return (dependent_type_p (TREE_TYPE (expression)));
28668}
28669
28670/* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28671 type-dependent if the expression refers to a member of the current
28672 instantiation and the type of the referenced member is dependent, or the
28673 class member access expression refers to a member of an unknown
28674 specialization.
28675
28676 This function returns true if the OBJECT in such a class member access
28677 expression is of an unknown specialization. */
28678
28679bool
28680type_dependent_object_expression_p (tree object)
28681{
28682 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28683 dependent. */
28684 if (TREE_CODE (object) == IDENTIFIER_NODE)
28685 return true;
28686 tree scope = TREE_TYPE (object);
28687 return (!scope || dependent_scope_p (scope));
28688}
28689
28690/* walk_tree callback function for instantiation_dependent_expression_p,
28691 below. Returns non-zero if a dependent subexpression is found. */
28692
28693static tree
28694instantiation_dependent_r (tree *tp, int *walk_subtrees,
28695 void * /*data*/)
28696{
28697 if (TYPE_P (*tp))
28698 {
28699 /* We don't have to worry about decltype currently because decltype
28700 of an instantiation-dependent expr is a dependent type. This
28701 might change depending on the resolution of DR 1172. */
28702 *walk_subtrees = false;
28703 return NULL_TREE;
28704 }
28705 enum tree_code code = TREE_CODE (*tp);
28706 switch (code)
28707 {
28708 /* Don't treat an argument list as dependent just because it has no
28709 TREE_TYPE. */
28710 case TREE_LIST:
28711 case TREE_VEC:
28712 case NONTYPE_ARGUMENT_PACK:
28713 return NULL_TREE;
28714
28715 case TEMPLATE_PARM_INDEX:
28716 if (dependent_type_p (TREE_TYPE (*tp)))
28717 return *tp;
28718 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
28719 return *tp;
28720 /* We'll check value-dependence separately. */
28721 return NULL_TREE;
28722
28723 /* Handle expressions with type operands. */
28724 case SIZEOF_EXPR:
28725 case ALIGNOF_EXPR:
28726 case TYPEID_EXPR:
28727 case AT_ENCODE_EXPR:
28728 {
28729 tree op = TREE_OPERAND (*tp, 0);
28730 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
28731 op = TREE_TYPE (op);
28732 if (TYPE_P (op))
28733 {
28734 if (dependent_type_p (type: op))
28735 return *tp;
28736 else
28737 {
28738 *walk_subtrees = false;
28739 return NULL_TREE;
28740 }
28741 }
28742 break;
28743 }
28744
28745 case COMPONENT_REF:
28746 if (identifier_p (TREE_OPERAND (*tp, 1)))
28747 /* In a template, finish_class_member_access_expr creates a
28748 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28749 type-dependent, so that we can check access control at
28750 instantiation time (PR 42277). See also Core issue 1273. */
28751 return *tp;
28752 break;
28753
28754 case SCOPE_REF:
28755 if (instantiation_dependent_scope_ref_p (t: *tp))
28756 return *tp;
28757 else
28758 break;
28759
28760 /* Treat statement-expressions as dependent. */
28761 case BIND_EXPR:
28762 return *tp;
28763
28764 /* Treat requires-expressions as dependent. */
28765 case REQUIRES_EXPR:
28766 return *tp;
28767
28768 case CONSTRUCTOR:
28769 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
28770 return *tp;
28771 break;
28772
28773 case TEMPLATE_DECL:
28774 case FUNCTION_DECL:
28775 /* Before C++17, a noexcept-specifier isn't part of the function type
28776 so it doesn't affect type dependence, but we still want to consider it
28777 for instantiation dependence. */
28778 if (cxx_dialect < cxx17
28779 && DECL_DECLARES_FUNCTION_P (*tp)
28780 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
28781 return *tp;
28782 break;
28783
28784 default:
28785 break;
28786 }
28787
28788 if (type_dependent_expression_p (expression: *tp))
28789 return *tp;
28790 else
28791 return NULL_TREE;
28792}
28793
28794/* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28795 sense defined by the ABI:
28796
28797 "An expression is instantiation-dependent if it is type-dependent
28798 or value-dependent, or it has a subexpression that is type-dependent
28799 or value-dependent."
28800
28801 Except don't actually check value-dependence for unevaluated expressions,
28802 because in sizeof(i) we don't care about the value of i. Checking
28803 type-dependence will in turn check value-dependence of array bounds/template
28804 arguments as needed. */
28805
28806bool
28807instantiation_dependent_uneval_expression_p (tree expression)
28808{
28809 tree result;
28810
28811 if (!processing_template_decl)
28812 return false;
28813
28814 if (expression == error_mark_node)
28815 return false;
28816
28817 result = cp_walk_tree_without_duplicates (&expression,
28818 instantiation_dependent_r, NULL);
28819 return result != NULL_TREE;
28820}
28821
28822/* As above, but also check value-dependence of the expression as a whole. */
28823
28824bool
28825instantiation_dependent_expression_p (tree expression)
28826{
28827 return (instantiation_dependent_uneval_expression_p (expression)
28828 || (processing_template_decl
28829 && potential_constant_expression (expression)
28830 && value_dependent_expression_p (expression)));
28831}
28832
28833/* Like type_dependent_expression_p, but it also works while not processing
28834 a template definition, i.e. during substitution or mangling. */
28835
28836bool
28837type_dependent_expression_p_push (tree expr)
28838{
28839 bool b;
28840 ++processing_template_decl;
28841 b = type_dependent_expression_p (expression: expr);
28842 --processing_template_decl;
28843 return b;
28844}
28845
28846/* Returns TRUE if ARGS contains a type-dependent expression. */
28847
28848bool
28849any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
28850{
28851 if (!processing_template_decl || !args)
28852 return false;
28853
28854 for (tree arg : *args)
28855 if (type_dependent_expression_p (expression: arg))
28856 return true;
28857
28858 return false;
28859}
28860
28861/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28862 expressions) contains any type-dependent expressions. */
28863
28864bool
28865any_type_dependent_elements_p (const_tree list)
28866{
28867 for (; list; list = TREE_CHAIN (list))
28868 if (type_dependent_expression_p (TREE_VALUE (list)))
28869 return true;
28870
28871 return false;
28872}
28873
28874/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28875 expressions) contains any value-dependent expressions. */
28876
28877bool
28878any_value_dependent_elements_p (const_tree list)
28879{
28880 for (; list; list = TREE_CHAIN (list))
28881 if (value_dependent_expression_p (TREE_VALUE (list)))
28882 return true;
28883
28884 return false;
28885}
28886
28887/* Returns TRUE if the ARG (a template argument) is dependent. */
28888
28889bool
28890dependent_template_arg_p (tree arg)
28891{
28892 if (!processing_template_decl)
28893 return false;
28894
28895 /* Assume a template argument that was wrongly written by the user
28896 is dependent. This is consistent with what
28897 any_dependent_template_arguments_p [that calls this function]
28898 does. */
28899 if (!arg || arg == error_mark_node)
28900 return true;
28901
28902 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
28903 arg = argument_pack_select_arg (t: arg);
28904
28905 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
28906 return true;
28907 if (TREE_CODE (arg) == TEMPLATE_DECL)
28908 {
28909 if (DECL_TEMPLATE_PARM_P (arg))
28910 return true;
28911 /* A member template of a dependent class is not necessarily
28912 type-dependent, but it is a dependent template argument because it
28913 will be a member of an unknown specialization to that template. */
28914 tree scope = CP_DECL_CONTEXT (arg);
28915 return TYPE_P (scope) && dependent_type_p (type: scope);
28916 }
28917 else if (ARGUMENT_PACK_P (arg))
28918 {
28919 tree args = ARGUMENT_PACK_ARGS (arg);
28920 for (tree arg : tree_vec_range (args))
28921 if (dependent_template_arg_p (arg))
28922 return true;
28923 return false;
28924 }
28925 else if (TYPE_P (arg))
28926 return dependent_type_p (type: arg);
28927 else
28928 return value_dependent_expression_p (expression: arg);
28929}
28930
28931/* Identify any expressions that use function parms. */
28932
28933static tree
28934find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
28935{
28936 tree t = *tp;
28937 if (TREE_CODE (t) == PARM_DECL)
28938 {
28939 *walk_subtrees = 0;
28940 return t;
28941 }
28942 return NULL_TREE;
28943}
28944
28945/* Returns true if a type specialization formed using the template
28946 arguments ARGS needs to use structural equality. */
28947
28948bool
28949any_template_arguments_need_structural_equality_p (tree args)
28950{
28951 int i;
28952 int j;
28953
28954 if (!args)
28955 return false;
28956 if (args == error_mark_node)
28957 return true;
28958
28959 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28960 {
28961 tree level = TMPL_ARGS_LEVEL (args, i + 1);
28962 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28963 {
28964 tree arg = TREE_VEC_ELT (level, j);
28965 tree packed_args = NULL_TREE;
28966 int k, len = 1;
28967
28968 if (ARGUMENT_PACK_P (arg))
28969 {
28970 /* Look inside the argument pack. */
28971 packed_args = ARGUMENT_PACK_ARGS (arg);
28972 len = TREE_VEC_LENGTH (packed_args);
28973 }
28974
28975 for (k = 0; k < len; ++k)
28976 {
28977 if (packed_args)
28978 arg = TREE_VEC_ELT (packed_args, k);
28979
28980 if (error_operand_p (t: arg))
28981 return true;
28982 else if (TREE_CODE (arg) == TEMPLATE_DECL)
28983 continue;
28984 else if (arg == any_targ_node)
28985 /* An any_targ_node argument (added by add_defaults_to_ttp)
28986 makes the corresponding specialization not canonicalizable,
28987 since template_args_equal always return true for it. We
28988 may see this when called from bind_template_template_parm. */
28989 return true;
28990 /* Checking current_function_decl because this structural
28991 comparison is only necessary for redeclaration. */
28992 else if (!current_function_decl
28993 && dependent_template_arg_p (arg)
28994 && (cp_walk_tree_without_duplicates
28995 (&arg, find_parm_usage_r, NULL)))
28996 /* The identity of a class template specialization that uses
28997 a function parameter depends on the identity of the function.
28998 And if this specialization appeared in the trailing return
28999 type thereof, we don't know the identity of the function
29000 (e.g. if it's a redeclaration or a new function) until we
29001 form its signature and go through duplicate_decls. Thus
29002 it's unsafe to decide on a canonical type now (which depends
29003 on the DECL_CONTEXT of the function parameter, which can get
29004 mutated after the fact by duplicate_decls), so just require
29005 structural equality in this case (PR52830). */
29006 return true;
29007 else if (TYPE_P (arg)
29008 && TYPE_STRUCTURAL_EQUALITY_P (arg)
29009 && dependent_alias_template_spec_p (t: arg, transparent_typedefs: nt_transparent))
29010 /* Require structural equality for specializations written
29011 in terms of a dependent alias template specialization. */
29012 return true;
29013 else if (CLASS_TYPE_P (arg)
29014 && TYPE_TEMPLATE_INFO (arg)
29015 && TYPE_STRUCTURAL_EQUALITY_P (arg))
29016 /* Require structural equality for specializations written
29017 in terms of a class template specialization that itself
29018 needs structural equality. */
29019 return true;
29020 }
29021 }
29022 }
29023
29024 return false;
29025}
29026
29027/* Returns true if ARGS (a collection of template arguments) contains
29028 any dependent arguments. */
29029
29030bool
29031any_dependent_template_arguments_p (const_tree args)
29032{
29033 if (args == error_mark_node)
29034 return true;
29035 if (!processing_template_decl || !args)
29036 return false;
29037
29038 for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
29039 {
29040 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29041 for (tree arg : tree_vec_range (CONST_CAST_TREE (level)))
29042 if (dependent_template_arg_p (arg))
29043 return true;
29044 }
29045
29046 return false;
29047}
29048
29049/* Returns true if ARGS contains any errors. */
29050
29051bool
29052any_erroneous_template_args_p (const_tree args)
29053{
29054 int i;
29055 int j;
29056
29057 if (args == error_mark_node)
29058 return true;
29059
29060 if (args && TREE_CODE (args) != TREE_VEC)
29061 {
29062 if (tree ti = get_template_info (t: args))
29063 args = TI_ARGS (ti);
29064 else
29065 args = NULL_TREE;
29066 }
29067
29068 if (!args)
29069 return false;
29070
29071 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
29072 {
29073 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29074 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
29075 if (error_operand_p (TREE_VEC_ELT (level, j)))
29076 return true;
29077 }
29078
29079 return false;
29080}
29081
29082/* Returns TRUE if the template TMPL is type-dependent. */
29083
29084bool
29085dependent_template_p (tree tmpl)
29086{
29087 if (TREE_CODE (tmpl) == OVERLOAD)
29088 {
29089 for (lkp_iterator iter (tmpl); iter; ++iter)
29090 if (dependent_template_p (tmpl: *iter))
29091 return true;
29092 return false;
29093 }
29094
29095 /* Template template parameters are dependent. */
29096 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
29097 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
29098 return true;
29099 /* So are names that have not been looked up. */
29100 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (t: tmpl))
29101 return true;
29102 return false;
29103}
29104
29105/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
29106
29107bool
29108dependent_template_id_p (tree tmpl, tree args)
29109{
29110 return (dependent_template_p (tmpl)
29111 || any_dependent_template_arguments_p (args));
29112}
29113
29114/* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
29115 are dependent. */
29116
29117bool
29118dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
29119{
29120 int i;
29121
29122 if (!processing_template_decl)
29123 return false;
29124
29125 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
29126 {
29127 tree decl = TREE_VEC_ELT (declv, i);
29128 tree init = TREE_VEC_ELT (initv, i);
29129 tree cond = TREE_VEC_ELT (condv, i);
29130 tree incr = TREE_VEC_ELT (incrv, i);
29131
29132 if (type_dependent_expression_p (expression: decl)
29133 || TREE_CODE (decl) == SCOPE_REF)
29134 return true;
29135
29136 if (init && type_dependent_expression_p (expression: init))
29137 return true;
29138
29139 if (cond == global_namespace)
29140 return true;
29141
29142 if (type_dependent_expression_p (expression: cond))
29143 return true;
29144
29145 if (COMPARISON_CLASS_P (cond)
29146 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
29147 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
29148 return true;
29149
29150 if (TREE_CODE (incr) == MODOP_EXPR)
29151 {
29152 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
29153 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
29154 return true;
29155 }
29156 else if (type_dependent_expression_p (expression: incr))
29157 return true;
29158 else if (TREE_CODE (incr) == MODIFY_EXPR)
29159 {
29160 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
29161 return true;
29162 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
29163 {
29164 tree t = TREE_OPERAND (incr, 1);
29165 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
29166 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
29167 return true;
29168
29169 /* If this loop has a class iterator with != comparison
29170 with increment other than i++/++i/i--/--i, make sure the
29171 increment is constant. */
29172 if (CLASS_TYPE_P (TREE_TYPE (decl))
29173 && TREE_CODE (cond) == NE_EXPR)
29174 {
29175 if (TREE_OPERAND (t, 0) == decl)
29176 t = TREE_OPERAND (t, 1);
29177 else
29178 t = TREE_OPERAND (t, 0);
29179 if (TREE_CODE (t) != INTEGER_CST)
29180 return true;
29181 }
29182 }
29183 }
29184 }
29185
29186 return false;
29187}
29188
29189/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
29190 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
29191 no such TYPE can be found. Note that this function peers inside
29192 uninstantiated templates and therefore should be used only in
29193 extremely limited situations. ONLY_CURRENT_P restricts this
29194 peering to the currently open classes hierarchy (which is required
29195 when comparing types). */
29196
29197tree
29198resolve_typename_type (tree type, bool only_current_p)
29199{
29200 tree scope;
29201 tree name;
29202 tree decl;
29203 int quals;
29204 tree pushed_scope;
29205 tree result;
29206
29207 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
29208
29209 scope = TYPE_CONTEXT (type);
29210 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
29211 gcc_checking_assert (uses_template_parms (scope));
29212
29213 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
29214 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
29215 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
29216 representing the typedef. In that case TYPE_IDENTIFIER (type) is
29217 not the non-qualified identifier of the TYPENAME_TYPE anymore.
29218 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
29219 the TYPENAME_TYPE instead, we avoid messing up with a possible
29220 typedef variant case. */
29221 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
29222
29223 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
29224 it first before we can figure out what NAME refers to. */
29225 if (TREE_CODE (scope) == TYPENAME_TYPE)
29226 {
29227 if (TYPENAME_IS_RESOLVING_P (scope))
29228 /* Given a class template A with a dependent base with nested type C,
29229 typedef typename A::C::C C will land us here, as trying to resolve
29230 the initial A::C leads to the local C typedef, which leads back to
29231 A::C::C. So we break the recursion now. */
29232 return type;
29233 else
29234 scope = resolve_typename_type (type: scope, only_current_p);
29235 }
29236 /* If we don't know what SCOPE refers to, then we cannot resolve the
29237 TYPENAME_TYPE. */
29238 if (!CLASS_TYPE_P (scope))
29239 return type;
29240 /* If this is a typedef, we don't want to look inside (c++/11987). */
29241 if (typedef_variant_p (type))
29242 return type;
29243 /* If SCOPE isn't the template itself, it will not have a valid
29244 TYPE_FIELDS list. */
29245 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
29246 /* scope is either the template itself or a compatible instantiation
29247 like X<T>, so look up the name in the original template. */
29248 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
29249 /* If scope has no fields, it can't be a current instantiation. Check this
29250 before currently_open_class to avoid infinite recursion (71515). */
29251 if (!TYPE_FIELDS (scope))
29252 return type;
29253 /* If the SCOPE is not the current instantiation, there's no reason
29254 to look inside it. */
29255 if (only_current_p && !currently_open_class (scope))
29256 return type;
29257 /* Enter the SCOPE so that name lookup will be resolved as if we
29258 were in the class definition. In particular, SCOPE will no
29259 longer be considered a dependent type. */
29260 pushed_scope = push_scope (scope);
29261 /* Look up the declaration. */
29262 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
29263 tf_warning_or_error);
29264
29265 result = NULL_TREE;
29266
29267 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
29268 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
29269 tree fullname = TYPENAME_TYPE_FULLNAME (type);
29270 if (!decl)
29271 /*nop*/;
29272 else if (identifier_p (t: fullname)
29273 && TREE_CODE (decl) == TYPE_DECL)
29274 {
29275 result = TREE_TYPE (decl);
29276 if (result == error_mark_node)
29277 result = NULL_TREE;
29278 }
29279 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
29280 && DECL_CLASS_TEMPLATE_P (decl))
29281 {
29282 /* Obtain the template and the arguments. */
29283 tree tmpl = TREE_OPERAND (fullname, 0);
29284 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
29285 {
29286 /* We get here with a plain identifier because a previous tentative
29287 parse of the nested-name-specifier as part of a ptr-operator saw
29288 ::template X<A>. The use of ::template is necessary in a
29289 ptr-operator, but wrong in a declarator-id.
29290
29291 [temp.names]: In a qualified-id of a declarator-id, the keyword
29292 template shall not appear at the top level. */
29293 pedwarn (cp_expr_loc_or_input_loc (t: fullname), OPT_Wpedantic,
29294 "keyword %<template%> not allowed in declarator-id");
29295 tmpl = decl;
29296 }
29297 tree args = TREE_OPERAND (fullname, 1);
29298 /* Instantiate the template. */
29299 result = lookup_template_class (d1: tmpl, arglist: args, NULL_TREE, NULL_TREE,
29300 /*entering_scope=*/true,
29301 complain: tf_error | tf_user);
29302 if (result == error_mark_node)
29303 result = NULL_TREE;
29304 }
29305
29306 /* Leave the SCOPE. */
29307 if (pushed_scope)
29308 pop_scope (pushed_scope);
29309
29310 /* If we failed to resolve it, return the original typename. */
29311 if (!result)
29312 return type;
29313
29314 /* If lookup found a typename type, resolve that too. */
29315 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
29316 {
29317 /* Ill-formed programs can cause infinite recursion here, so we
29318 must catch that. */
29319 TYPENAME_IS_RESOLVING_P (result) = 1;
29320 result = resolve_typename_type (type: result, only_current_p);
29321 TYPENAME_IS_RESOLVING_P (result) = 0;
29322 }
29323
29324 /* Qualify the resulting type. */
29325 quals = cp_type_quals (type);
29326 if (quals)
29327 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
29328
29329 return result;
29330}
29331
29332/* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29333 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29334 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29335
29336static tree
29337make_auto_1 (tree name, bool set_canonical, int level = -1)
29338{
29339 if (level == -1)
29340 level = current_template_depth + 1;
29341 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
29342 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
29343 TYPE_STUB_DECL (au) = TYPE_NAME (au);
29344 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
29345 (index: 0, level, orig_level: level, TYPE_NAME (au), NULL_TREE);
29346 if (set_canonical)
29347 TYPE_CANONICAL (au) = canonical_type_parameter (type: au);
29348 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
29349 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
29350 if (name == decltype_auto_identifier)
29351 AUTO_IS_DECLTYPE (au) = true;
29352
29353 return au;
29354}
29355
29356tree
29357make_decltype_auto (void)
29358{
29359 return make_auto_1 (decltype_auto_identifier, set_canonical: true);
29360}
29361
29362tree
29363make_auto (void)
29364{
29365 return make_auto_1 (auto_identifier, set_canonical: true);
29366}
29367
29368/* Return a C++17 deduction placeholder for class template TMPL.
29369 There are represented as an 'auto' with the special level 0 and
29370 CLASS_PLACEHOLDER_TEMPLATE set. */
29371
29372tree
29373make_template_placeholder (tree tmpl)
29374{
29375 tree t = make_auto_1 (auto_identifier, set_canonical: false, /*level=*/0);
29376 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
29377 /* Our canonical type depends on the placeholder. */
29378 TYPE_CANONICAL (t) = canonical_type_parameter (type: t);
29379 return t;
29380}
29381
29382/* True iff T is a C++17 class template deduction placeholder. */
29383
29384bool
29385template_placeholder_p (tree t)
29386{
29387 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
29388}
29389
29390/* Return an auto for an explicit cast expression auto(x).
29391 Like CTAD placeholders, these have level 0 so that they're
29392 not accidentally replaced via tsubst and are always directly
29393 resolved via do_auto_deduction. */
29394
29395tree
29396make_cast_auto ()
29397{
29398 return make_auto_1 (auto_identifier, set_canonical: true, /*level=*/0);
29399}
29400
29401/* Make a "constrained auto" type-specifier. This is an auto or
29402 decltype(auto) type with constraints that must be associated after
29403 deduction. The constraint is formed from the given concept CON
29404 and its optional sequence of template arguments ARGS.
29405
29406 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29407
29408static tree
29409make_constrained_placeholder_type (tree type, tree con, tree args)
29410{
29411 /* Build the constraint. */
29412 tree tmpl = DECL_TI_TEMPLATE (con);
29413 tree expr = tmpl;
29414 if (TREE_CODE (con) == FUNCTION_DECL)
29415 expr = ovl_make (fn: tmpl);
29416 ++processing_template_decl;
29417 expr = build_concept_check (expr, type, args, tf_warning_or_error);
29418 --processing_template_decl;
29419
29420 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
29421 = build_tree_list (current_template_parms, expr);
29422
29423 /* Our canonical type depends on the constraint. */
29424 TYPE_CANONICAL (type) = canonical_type_parameter (type);
29425
29426 /* Attach the constraint to the type declaration. */
29427 return TYPE_NAME (type);
29428}
29429
29430/* Make a "constrained auto" type-specifier. */
29431
29432tree
29433make_constrained_auto (tree con, tree args)
29434{
29435 tree type = make_auto_1 (auto_identifier, set_canonical: false);
29436 return make_constrained_placeholder_type (type, con, args);
29437}
29438
29439/* Make a "constrained decltype(auto)" type-specifier. */
29440
29441tree
29442make_constrained_decltype_auto (tree con, tree args)
29443{
29444 tree type = make_auto_1 (decltype_auto_identifier, set_canonical: false);
29445 return make_constrained_placeholder_type (type, con, args);
29446}
29447
29448/* Returns true if the placeholder type constraint T has any dependent
29449 (explicit) template arguments. */
29450
29451static bool
29452placeholder_type_constraint_dependent_p (tree t)
29453{
29454 tree id = unpack_concept_check (t);
29455 tree args = TREE_OPERAND (id, 1);
29456 tree first = TREE_VEC_ELT (args, 0);
29457 if (ARGUMENT_PACK_P (first))
29458 {
29459 args = expand_template_argument_pack (args);
29460 first = TREE_VEC_ELT (args, 0);
29461 }
29462 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
29463 || is_auto (first));
29464 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
29465 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
29466 return true;
29467 return false;
29468}
29469
29470/* Build and return a concept definition. Like other templates, the
29471 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29472 the TEMPLATE_DECL. */
29473
29474tree
29475finish_concept_definition (cp_expr id, tree init, tree attrs)
29476{
29477 gcc_assert (identifier_p (id));
29478 gcc_assert (processing_template_decl);
29479
29480 location_t loc = id.get_location();
29481
29482 /* A concept-definition shall not have associated constraints. */
29483 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
29484 {
29485 error_at (loc, "a concept cannot be constrained");
29486 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
29487 }
29488
29489 /* A concept-definition shall appear in namespace scope. Templates
29490 aren't allowed in block scope, so we only need to check for class
29491 scope. */
29492 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29493 {
29494 error_at (loc, "concept %qE not in namespace scope", *id);
29495 return error_mark_node;
29496 }
29497
29498 if (current_template_depth > 1)
29499 {
29500 error_at (loc, "concept %qE has multiple template parameter lists", *id);
29501 return error_mark_node;
29502 }
29503
29504 /* Initially build the concept declaration; its type is bool. */
29505 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
29506 DECL_CONTEXT (decl) = current_scope ();
29507 DECL_INITIAL (decl) = init;
29508
29509 if (attrs)
29510 cplus_decl_attributes (&decl, attrs, 0);
29511
29512 set_originating_module (decl, friend_p: false);
29513
29514 /* Push the enclosing template. */
29515 return push_template_decl (decl);
29516}
29517
29518/* Given type ARG, return std::initializer_list<ARG>. */
29519
29520static tree
29521listify (tree arg)
29522{
29523 tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
29524
29525 if (std_init_list == error_mark_node
29526 || !DECL_CLASS_TEMPLATE_P (std_init_list))
29527 {
29528 gcc_rich_location richloc (input_location);
29529 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
29530 error_at (&richloc,
29531 "deducing from brace-enclosed initializer list"
29532 " requires %<#include <initializer_list>%>");
29533
29534 return error_mark_node;
29535 }
29536 tree argvec = make_tree_vec (1);
29537 TREE_VEC_ELT (argvec, 0) = arg;
29538
29539 return lookup_template_class (d1: std_init_list, arglist: argvec, NULL_TREE,
29540 NULL_TREE, entering_scope: 0, complain: tf_warning_or_error);
29541}
29542
29543/* Replace auto in TYPE with std::initializer_list<auto>. */
29544
29545static tree
29546listify_autos (tree type, tree auto_node)
29547{
29548 tree init_auto = listify (arg: strip_top_quals (auto_node));
29549 tree argvec = make_tree_vec (1);
29550 TREE_VEC_ELT (argvec, 0) = init_auto;
29551 if (processing_template_decl)
29552 argvec = add_to_template_args (args: current_template_args (), extra_args: argvec);
29553 return tsubst (t: type, args: argvec, complain: tf_warning_or_error, NULL_TREE);
29554}
29555
29556/* Hash traits for hashing possibly constrained 'auto'
29557 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29558
29559struct auto_hash : default_hash_traits<tree>
29560{
29561 static inline hashval_t hash (tree);
29562 static inline bool equal (tree, tree);
29563};
29564
29565/* Hash the 'auto' T. */
29566
29567inline hashval_t
29568auto_hash::hash (tree t)
29569{
29570 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
29571 /* Matching constrained-type-specifiers denote the same template
29572 parameter, so hash the constraint. */
29573 return hash_placeholder_constraint (c);
29574 else
29575 /* But unconstrained autos are all separate, so just hash the pointer. */
29576 return iterative_hash_object (t, 0);
29577}
29578
29579/* Compare two 'auto's. */
29580
29581inline bool
29582auto_hash::equal (tree t1, tree t2)
29583{
29584 if (t1 == t2)
29585 return true;
29586
29587 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
29588 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
29589
29590 /* Two unconstrained autos are distinct. */
29591 if (!c1 || !c2)
29592 return false;
29593
29594 return equivalent_placeholder_constraints (c1, c2);
29595}
29596
29597/* for_each_template_parm callback for extract_autos: if t is a (possibly
29598 constrained) auto, add it to the vector. */
29599
29600static int
29601extract_autos_r (tree t, void *data)
29602{
29603 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
29604 if (is_auto (t) && !template_placeholder_p (t))
29605 {
29606 /* All the autos were built with index 0; fix that up now. */
29607 tree *p = hash.find_slot (value: t, insert: INSERT);
29608 int idx;
29609 if (*p)
29610 /* If this is a repeated constrained-type-specifier, use the index we
29611 chose before. */
29612 idx = TEMPLATE_TYPE_IDX (*p);
29613 else
29614 {
29615 /* Otherwise this is new, so use the current count. */
29616 *p = t;
29617 idx = hash.elements () - 1;
29618 }
29619 if (idx != TEMPLATE_TYPE_IDX (t))
29620 {
29621 gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0);
29622 gcc_checking_assert (TYPE_CANONICAL (t) != t);
29623 TEMPLATE_TYPE_IDX (t) = idx;
29624 TYPE_CANONICAL (t) = canonical_type_parameter (type: t);
29625 }
29626 }
29627
29628 /* Always keep walking. */
29629 return 0;
29630}
29631
29632/* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29633 says they can appear anywhere in the type. */
29634
29635static tree
29636extract_autos (tree type)
29637{
29638 hash_set<tree> visited;
29639 hash_table<auto_hash> hash (2);
29640
29641 for_each_template_parm (t: type, fn: extract_autos_r, data: &hash, visited: &visited, include_nondeduced_p: true);
29642
29643 tree tree_vec = make_tree_vec (hash.elements());
29644 for (tree elt : hash)
29645 {
29646 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
29647 TREE_VEC_ELT (tree_vec, i)
29648 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
29649 }
29650
29651 return tree_vec;
29652}
29653
29654/* The stem for deduction guide names. */
29655const char *const dguide_base = "__dguide_";
29656
29657/* Return the name for a deduction guide for class template TMPL. */
29658
29659tree
29660dguide_name (tree tmpl)
29661{
29662 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
29663 tree tname = TYPE_IDENTIFIER (type);
29664 char *buf = (char *) alloca (1 + strlen (dguide_base)
29665 + IDENTIFIER_LENGTH (tname));
29666 memcpy (dest: buf, src: dguide_base, n: strlen (s: dguide_base));
29667 memcpy (dest: buf + strlen (s: dguide_base), IDENTIFIER_POINTER (tname),
29668 IDENTIFIER_LENGTH (tname) + 1);
29669 tree dname = get_identifier (buf);
29670 TREE_TYPE (dname) = type;
29671 return dname;
29672}
29673
29674/* True if NAME is the name of a deduction guide. */
29675
29676bool
29677dguide_name_p (tree name)
29678{
29679 return (TREE_CODE (name) == IDENTIFIER_NODE
29680 && TREE_TYPE (name)
29681 && startswith (IDENTIFIER_POINTER (name), prefix: dguide_base));
29682}
29683
29684/* True if FN is a deduction guide. */
29685
29686bool
29687deduction_guide_p (const_tree fn)
29688{
29689 if (DECL_P (fn))
29690 if (tree name = DECL_NAME (fn))
29691 return dguide_name_p (name);
29692 return false;
29693}
29694
29695/* True if FN is the copy deduction guide, i.e. A(A)->A. */
29696
29697bool
29698copy_guide_p (const_tree fn)
29699{
29700 gcc_assert (deduction_guide_p (fn));
29701 if (!DECL_ARTIFICIAL (fn))
29702 return false;
29703 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
29704 return (TREE_CHAIN (parms) == void_list_node
29705 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
29706}
29707
29708/* True if FN is a guide generated from a constructor template. */
29709
29710bool
29711template_guide_p (const_tree fn)
29712{
29713 gcc_assert (deduction_guide_p (fn));
29714 if (!DECL_ARTIFICIAL (fn))
29715 return false;
29716 tree tmpl = DECL_TI_TEMPLATE (fn);
29717 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
29718 return PRIMARY_TEMPLATE_P (org);
29719 return false;
29720}
29721
29722/* True if FN is an aggregate initialization guide or the copy deduction
29723 guide. */
29724
29725bool
29726builtin_guide_p (const_tree fn)
29727{
29728 if (!deduction_guide_p (fn))
29729 return false;
29730 if (!DECL_ARTIFICIAL (fn))
29731 /* Explicitly declared. */
29732 return false;
29733 if (DECL_ABSTRACT_ORIGIN (fn))
29734 /* Derived from a constructor. */
29735 return false;
29736 return true;
29737}
29738
29739/* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29740 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29741 template parameter types. Note that the handling of template template
29742 parameters relies on current_template_parms being set appropriately for the
29743 new template. */
29744
29745static tree
29746rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
29747 tree tsubst_args, tsubst_flags_t complain)
29748{
29749 if (olddecl == error_mark_node)
29750 return error_mark_node;
29751
29752 tree oldidx = get_template_parm_index (parm: olddecl);
29753
29754 tree newtype;
29755 if (TREE_CODE (olddecl) == TYPE_DECL
29756 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29757 {
29758 tree oldtype = TREE_TYPE (olddecl);
29759 newtype = cxx_make_type (TREE_CODE (oldtype));
29760 TYPE_MAIN_VARIANT (newtype) = newtype;
29761 }
29762 else
29763 {
29764 newtype = TREE_TYPE (olddecl);
29765 if (type_uses_auto (newtype))
29766 {
29767 // Substitute once to fix references to other template parameters.
29768 newtype = tsubst (t: newtype, args: tsubst_args,
29769 complain: complain|tf_partial, NULL_TREE);
29770 // Now substitute again to reduce the level of the auto.
29771 newtype = tsubst (t: newtype, args: current_template_args (),
29772 complain, NULL_TREE);
29773 }
29774 else
29775 newtype = tsubst (t: newtype, args: tsubst_args,
29776 complain, NULL_TREE);
29777 }
29778
29779 tree newdecl
29780 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
29781 DECL_NAME (olddecl), newtype);
29782 SET_DECL_TEMPLATE_PARM_P (newdecl);
29783
29784 tree newidx;
29785 if (TREE_CODE (olddecl) == TYPE_DECL
29786 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29787 {
29788 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
29789 = build_template_parm_index (index, level, orig_level: level,
29790 decl: newdecl, type: newtype);
29791 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29792 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29793 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
29794
29795 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
29796 {
29797 tree newresult
29798 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
29799 DECL_NAME (olddecl), newtype);
29800 DECL_ARTIFICIAL (newresult) = true;
29801 DECL_TEMPLATE_RESULT (newdecl) = newresult;
29802 // First create a copy (ttargs) of tsubst_args with an
29803 // additional level for the template template parameter's own
29804 // template parameters (ttparms).
29805 tree ttparms = (INNERMOST_TEMPLATE_PARMS
29806 (DECL_TEMPLATE_PARMS (olddecl)));
29807 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
29808 tree ttargs = make_tree_vec (depth + 1);
29809 for (int i = 0; i < depth; ++i)
29810 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
29811 TREE_VEC_ELT (ttargs, depth)
29812 = template_parms_level_to_args (parms: ttparms);
29813 // Substitute ttargs into ttparms to fix references to
29814 // other template parameters.
29815 ttparms = tsubst_template_parms_level (parms: ttparms, args: ttargs,
29816 complain: complain|tf_partial);
29817 // Now substitute again with args based on tparms, to reduce
29818 // the level of the ttparms.
29819 ttargs = current_template_args ();
29820 ttparms = tsubst_template_parms_level (parms: ttparms, args: ttargs,
29821 complain);
29822 // Finally, tack the adjusted parms onto tparms.
29823 ttparms = tree_cons (size_int (level + 1), ttparms,
29824 copy_node (current_template_parms));
29825 // As with all template template parms, the parameter list captured
29826 // by this template template parm that corresponds to its own level
29827 // should be empty. This avoids infinite recursion when structurally
29828 // comparing two such rewritten template template parms (PR102479).
29829 gcc_assert (!TREE_VEC_LENGTH
29830 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
29831 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
29832 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
29833 // All done.
29834 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
29835 DECL_TEMPLATE_INFO (newresult)
29836 = build_template_info (template_decl: newdecl, template_args: template_parms_to_args (parms: ttparms));
29837 }
29838
29839 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
29840 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
29841 else
29842 TYPE_CANONICAL (newtype) = canonical_type_parameter (type: newtype);
29843 }
29844 else
29845 {
29846 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
29847 tree newconst
29848 = build_decl (DECL_SOURCE_LOCATION (oldconst),
29849 TREE_CODE (oldconst),
29850 DECL_NAME (oldconst), newtype);
29851 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
29852 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
29853 SET_DECL_TEMPLATE_PARM_P (newconst);
29854 newidx = build_template_parm_index (index, level, orig_level: level,
29855 decl: newconst, type: newtype);
29856 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29857 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29858 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
29859 }
29860
29861 return newdecl;
29862}
29863
29864/* As rewrite_template_parm, but for the whole TREE_LIST representing a
29865 template parameter. */
29866
29867static tree
29868rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
29869 tree targs, unsigned targs_index, tsubst_flags_t complain)
29870{
29871 tree olddecl = TREE_VALUE (oldelt);
29872 tree newdecl = rewrite_template_parm (olddecl, index, level,
29873 tsubst_args: targs, complain);
29874 if (newdecl == error_mark_node)
29875 return error_mark_node;
29876 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
29877 args: targs, complain, NULL_TREE);
29878 tree list = build_tree_list (newdef, newdecl);
29879 TEMPLATE_PARM_CONSTRAINTS (list)
29880 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
29881 targs, complain, NULL_TREE);
29882 int depth = TMPL_ARGS_DEPTH (targs);
29883 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (t: list);
29884 return list;
29885}
29886
29887/* Returns a C++17 class deduction guide template based on the constructor
29888 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29889 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29890 aggregate initialization guide. OUTER_ARGS are the template arguments
29891 for the enclosing scope of the class. */
29892
29893static tree
29894build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
29895{
29896 tree tparms, targs, fparms, fargs, ci;
29897 bool memtmpl = false;
29898 bool explicit_p;
29899 location_t loc;
29900 tree fn_tmpl = NULL_TREE;
29901
29902 if (outer_args)
29903 {
29904 ++processing_template_decl;
29905 type = tsubst (t: type, args: outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
29906 --processing_template_decl;
29907 }
29908
29909 if (!DECL_DECLARES_FUNCTION_P (ctor))
29910 {
29911 if (TYPE_P (ctor))
29912 {
29913 bool copy_p = TYPE_REF_P (ctor);
29914 if (copy_p)
29915 fparms = tree_cons (NULL_TREE, type, void_list_node);
29916 else
29917 fparms = void_list_node;
29918 }
29919 else if (TREE_CODE (ctor) == TREE_LIST)
29920 fparms = ctor;
29921 else
29922 gcc_unreachable ();
29923
29924 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
29925 tparms = DECL_TEMPLATE_PARMS (ctmpl);
29926 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
29927 ci = NULL_TREE;
29928 fargs = NULL_TREE;
29929 loc = DECL_SOURCE_LOCATION (ctmpl);
29930 explicit_p = false;
29931 }
29932 else
29933 {
29934 ++processing_template_decl;
29935 bool ok = true;
29936
29937 complain |= tf_dguide;
29938
29939 fn_tmpl
29940 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
29941 : DECL_TI_TEMPLATE (ctor));
29942 if (outer_args)
29943 fn_tmpl = tsubst (t: fn_tmpl, args: outer_args, complain, in_decl: ctor);
29944 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
29945
29946 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
29947 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29948 fully specialized args for the enclosing class. Strip those off, as
29949 the deduction guide won't have those template parameters. */
29950 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
29951 TMPL_PARMS_DEPTH (tparms));
29952 /* Discard the 'this' parameter. */
29953 fparms = FUNCTION_ARG_CHAIN (ctor);
29954 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
29955 ci = get_constraints (ctor);
29956 loc = DECL_SOURCE_LOCATION (ctor);
29957 explicit_p = DECL_NONCONVERTING_P (ctor);
29958
29959 if (PRIMARY_TEMPLATE_P (fn_tmpl))
29960 {
29961 memtmpl = true;
29962
29963 /* For a member template constructor, we need to flatten the two
29964 template parameter lists into one, and then adjust the function
29965 signature accordingly. This gets...complicated. */
29966 tree save_parms = current_template_parms;
29967
29968 /* For a member template we should have two levels of parms/args, one
29969 for the class and one for the constructor. We stripped
29970 specialized args for further enclosing classes above. */
29971 const int depth = 2;
29972 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
29973
29974 /* Template args for translating references to the two-level template
29975 parameters into references to the one-level template parameters we
29976 are creating. */
29977 tree tsubst_args = copy_node (targs);
29978 TMPL_ARGS_LEVEL (tsubst_args, depth)
29979 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
29980
29981 /* Template parms for the constructor template. */
29982 tree ftparms = TREE_VALUE (tparms);
29983 unsigned flen = TREE_VEC_LENGTH (ftparms);
29984 /* Template parms for the class template. */
29985 tparms = TREE_CHAIN (tparms);
29986 tree ctparms = TREE_VALUE (tparms);
29987 unsigned clen = TREE_VEC_LENGTH (ctparms);
29988 /* Template parms for the deduction guide start as a copy of the
29989 template parms for the class. We set current_template_parms for
29990 lookup_template_class_1. */
29991 current_template_parms = tparms = copy_node (tparms);
29992 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
29993 for (unsigned i = 0; i < clen; ++i)
29994 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
29995
29996 /* Now we need to rewrite the constructor parms to append them to the
29997 class parms. */
29998 for (unsigned i = 0; i < flen; ++i)
29999 {
30000 unsigned index = i + clen;
30001 unsigned level = 1;
30002 tree oldelt = TREE_VEC_ELT (ftparms, i);
30003 tree newelt
30004 = rewrite_tparm_list (oldelt, index, level,
30005 targs: tsubst_args, targs_index: i, complain);
30006 if (newelt == error_mark_node)
30007 ok = false;
30008 TREE_VEC_ELT (new_vec, index) = newelt;
30009 }
30010
30011 /* Now we have a final set of template parms to substitute into the
30012 function signature. */
30013 targs = template_parms_to_args (parms: tparms);
30014 fparms = tsubst_arg_types (arg_types: fparms, args: tsubst_args, NULL_TREE,
30015 complain, in_decl: ctor);
30016 if (fparms == error_mark_node)
30017 ok = false;
30018 if (ci)
30019 {
30020 if (outer_args)
30021 /* FIXME: We'd like to avoid substituting outer template
30022 arguments into the constraint ahead of time, but the
30023 construction of tsubst_args assumes that outer arguments
30024 are already substituted in. */
30025 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30026 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
30027 }
30028
30029 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
30030 cp_unevaluated_operand. */
30031 cp_evaluated ev;
30032 fargs = tsubst (t: fargs, args: tsubst_args, complain, in_decl: ctor);
30033 current_template_parms = save_parms;
30034 }
30035 else
30036 {
30037 /* Substitute in the same arguments to rewrite class members into
30038 references to members of an unknown specialization. */
30039 cp_evaluated ev;
30040 fparms = tsubst_arg_types (arg_types: fparms, args: targs, NULL_TREE, complain, in_decl: ctor);
30041 fargs = tsubst (t: fargs, args: targs, complain, in_decl: ctor);
30042 if (ci)
30043 {
30044 if (outer_args)
30045 /* FIXME: As above. */
30046 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30047 ci = tsubst_constraint_info (ci, targs, complain, ctor);
30048 }
30049 }
30050
30051 --processing_template_decl;
30052 if (!ok)
30053 return error_mark_node;
30054 }
30055
30056 if (!memtmpl)
30057 {
30058 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
30059 tparms = copy_node (tparms);
30060 INNERMOST_TEMPLATE_PARMS (tparms)
30061 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
30062 }
30063
30064 tree fntype = build_function_type (type, fparms);
30065 tree ded_fn = build_lang_decl_loc (loc,
30066 FUNCTION_DECL,
30067 dguide_name (tmpl: type), fntype);
30068 DECL_ARGUMENTS (ded_fn) = fargs;
30069 DECL_ARTIFICIAL (ded_fn) = true;
30070 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
30071 tree ded_tmpl = build_template_decl (decl: ded_fn, parms: tparms, /*member*/member_template_p: false);
30072 DECL_ARTIFICIAL (ded_tmpl) = true;
30073 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (template_decl: ded_tmpl, template_args: targs);
30074 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
30075 if (DECL_P (ctor))
30076 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
30077 if (ci)
30078 set_constraints (ded_tmpl, ci);
30079
30080 return ded_tmpl;
30081}
30082
30083/* Add to LIST the member types for the reshaped initializer CTOR. */
30084
30085static tree
30086collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
30087{
30088 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
30089 tree idx, val; unsigned i;
30090 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
30091 {
30092 tree ftype = elt ? elt : TREE_TYPE (idx);
30093 if (BRACE_ENCLOSED_INITIALIZER_P (val)
30094 && CONSTRUCTOR_BRACES_ELIDED_P (val))
30095 {
30096 tree subelt = NULL_TREE;
30097 if (TREE_CODE (ftype) == ARRAY_TYPE)
30098 subelt = TREE_TYPE (ftype);
30099 list = collect_ctor_idx_types (ctor: val, list, elt: subelt);
30100 continue;
30101 }
30102 tree arg = NULL_TREE;
30103 if (i == v->length() - 1
30104 && PACK_EXPANSION_P (ftype))
30105 /* Give the trailing pack expansion parameter a default argument to
30106 match aggregate initialization behavior, even if we deduce the
30107 length of the pack separately to more than we have initializers. */
30108 arg = build_constructor (init_list_type_node, NULL);
30109 /* if ei is of array type and xi is a braced-init-list or string literal,
30110 Ti is an rvalue reference to the declared type of ei */
30111 STRIP_ANY_LOCATION_WRAPPER (val);
30112 if (TREE_CODE (ftype) == ARRAY_TYPE
30113 && (BRACE_ENCLOSED_INITIALIZER_P (val)
30114 || TREE_CODE (val) == STRING_CST))
30115 {
30116 if (TREE_CODE (val) == STRING_CST)
30117 ftype = cp_build_qualified_type
30118 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
30119 ftype = (cp_build_reference_type
30120 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
30121 }
30122 list = tree_cons (arg, ftype, list);
30123 }
30124
30125 return list;
30126}
30127
30128/* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
30129
30130static bool
30131is_spec_or_derived (tree etype, tree tmpl)
30132{
30133 if (!etype || !CLASS_TYPE_P (etype))
30134 return false;
30135
30136 etype = cv_unqualified (etype);
30137 tree type = TREE_TYPE (tmpl);
30138 tree tparms = (INNERMOST_TEMPLATE_PARMS
30139 (DECL_TEMPLATE_PARMS (tmpl)));
30140 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
30141 int err = unify (tparms, targs, parm: type, arg: etype,
30142 UNIFY_ALLOW_DERIVED, /*explain*/explain_p: false);
30143 ggc_free (targs);
30144 return !err;
30145}
30146
30147/* Return a C++20 aggregate deduction candidate for TYPE initialized from
30148 INIT. */
30149
30150static tree
30151maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
30152{
30153 if (cxx_dialect < cxx20)
30154 return NULL_TREE;
30155
30156 if (init == NULL_TREE)
30157 return NULL_TREE;
30158
30159 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30160 {
30161 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30162 tree tinfo = get_template_info (t: under);
30163 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
30164 return alias_ctad_tweaks (tmpl, guide);
30165 return NULL_TREE;
30166 }
30167
30168 /* We might be creating a guide for a class member template, e.g.,
30169
30170 template<typename U> struct A {
30171 template<typename T> struct B { T t; };
30172 };
30173
30174 At this point, A will have been instantiated. Below, we need to
30175 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
30176 const bool member_template_p
30177 = (DECL_TEMPLATE_INFO (tmpl)
30178 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
30179 tree type = TREE_TYPE (tmpl);
30180 tree template_type = (member_template_p
30181 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
30182 : type);
30183 if (!CP_AGGREGATE_TYPE_P (template_type))
30184 return NULL_TREE;
30185
30186 /* No aggregate candidate for copy-initialization. */
30187 if (args->length() == 1)
30188 {
30189 tree val = (*args)[0];
30190 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
30191 return NULL_TREE;
30192 }
30193
30194 /* If we encounter a problem, we just won't add the candidate. */
30195 tsubst_flags_t complain = tf_none;
30196
30197 tree parms = NULL_TREE;
30198 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30199 {
30200 init = reshape_init (template_type, init, complain);
30201 if (init == error_mark_node)
30202 return NULL_TREE;
30203 parms = collect_ctor_idx_types (ctor: init, list: parms);
30204 /* If we're creating a deduction guide for a member class template,
30205 we've used the original template pattern type for the reshape_init
30206 above; this is done because we want PARMS to be a template parameter
30207 type, something that can be deduced when used as a function template
30208 parameter. At this point the outer class template has already been
30209 partially instantiated (we deferred the deduction until the enclosing
30210 scope is non-dependent). Therefore we have to partially instantiate
30211 PARMS, so that its template level is properly reduced and we don't get
30212 mismatches when deducing types using the guide with PARMS. */
30213 if (member_template_p)
30214 {
30215 ++processing_template_decl;
30216 parms = tsubst (t: parms, DECL_TI_ARGS (tmpl), complain, in_decl: init);
30217 --processing_template_decl;
30218 }
30219 }
30220 else if (TREE_CODE (init) == TREE_LIST)
30221 {
30222 int len = list_length (init);
30223 for (tree field = TYPE_FIELDS (type);
30224 len;
30225 --len, field = DECL_CHAIN (field))
30226 {
30227 field = next_aggregate_field (field);
30228 if (!field)
30229 return NULL_TREE;
30230 tree ftype = finish_decltype_type (field, true, complain);
30231 parms = tree_cons (NULL_TREE, ftype, parms);
30232 }
30233 }
30234 else
30235 /* Aggregate initialization doesn't apply to an initializer expression. */
30236 return NULL_TREE;
30237
30238 if (parms)
30239 {
30240 tree last = parms;
30241 parms = nreverse (parms);
30242 TREE_CHAIN (last) = void_list_node;
30243 tree guide = build_deduction_guide (type, ctor: parms, NULL_TREE, complain);
30244 return guide;
30245 }
30246
30247 return NULL_TREE;
30248}
30249
30250/* UGUIDES are the deduction guides for the underlying template of alias
30251 template TMPL; adjust them to be deduction guides for TMPL.
30252
30253 This routine also handles C++23 inherited CTAD, in which case TMPL is a
30254 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
30255 the template parameter list of the alias template (equivalently, of the
30256 derived class) and TREE_VALUE the defining-type-id (equivalently, the
30257 base whose guides we're inheriting). UGUIDES are the base's guides. */
30258
30259static tree
30260alias_ctad_tweaks (tree tmpl, tree uguides)
30261{
30262 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
30263 class type (9.2.8.2) where the template-name names an alias template A,
30264 the defining-type-id of A must be of the form
30265
30266 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30267
30268 as specified in 9.2.8.2. The guides of A are the set of functions or
30269 function templates formed as follows. For each function or function
30270 template f in the guides of the template named by the simple-template-id
30271 of the defining-type-id, the template arguments of the return type of f
30272 are deduced from the defining-type-id of A according to the process in
30273 13.10.2.5 with the exception that deduction does not fail if not all
30274 template arguments are deduced. Let g denote the result of substituting
30275 these deductions into f. If substitution succeeds, form a function or
30276 function template f' with the following properties and add it to the set
30277 of guides of A:
30278
30279 * The function type of f' is the function type of g.
30280
30281 * If f is a function template, f' is a function template whose template
30282 parameter list consists of all the template parameters of A (including
30283 their default template arguments) that appear in the above deductions or
30284 (recursively) in their default template arguments, followed by the
30285 template parameters of f that were not deduced (including their default
30286 template arguments), otherwise f' is not a function template.
30287
30288 * The associated constraints (13.5.2) are the conjunction of the
30289 associated constraints of g and a constraint that is satisfied if and only
30290 if the arguments of A are deducible (see below) from the return type.
30291
30292 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
30293 be so as well.
30294
30295 * If f was generated from a deduction-guide (12.4.1.8), then f' is
30296 considered to be so as well.
30297
30298 * The explicit-specifier of f' is the explicit-specifier of g (if
30299 any). */
30300
30301 enum { alias, inherited } ctad_kind;
30302 tree atype, fullatparms, utype;
30303 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
30304 {
30305 ctad_kind = alias;
30306 atype = TREE_TYPE (tmpl);
30307 fullatparms = DECL_TEMPLATE_PARMS (tmpl);
30308 utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30309 }
30310 else
30311 {
30312 ctad_kind = inherited;
30313 atype = NULL_TREE;
30314 fullatparms = TREE_PURPOSE (tmpl);
30315 utype = TREE_VALUE (tmpl);
30316 }
30317
30318 tsubst_flags_t complain = tf_warning_or_error;
30319 tree aguides = NULL_TREE;
30320 tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
30321 unsigned natparms = TREE_VEC_LENGTH (atparms);
30322 for (ovl_iterator iter (uguides); iter; ++iter)
30323 {
30324 tree f = *iter;
30325 tree in_decl = f;
30326 location_t loc = DECL_SOURCE_LOCATION (f);
30327 tree ret = TREE_TYPE (TREE_TYPE (f));
30328 tree fprime = f;
30329 if (TREE_CODE (f) == TEMPLATE_DECL)
30330 {
30331 processing_template_decl_sentinel ptds (/*reset*/false);
30332 ++processing_template_decl;
30333
30334 /* Deduce template arguments for f from the type-id of A. */
30335 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
30336 unsigned len = TREE_VEC_LENGTH (ftparms);
30337 tree targs = make_tree_vec (len);
30338 int err = unify (tparms: ftparms, targs, parm: ret, arg: utype, UNIFY_ALLOW_NONE, explain_p: false);
30339 if (err)
30340 /* CWG2664: Discard any deductions, still build the guide. */
30341 for (unsigned i = 0; i < len; ++i)
30342 TREE_VEC_ELT (targs, i) = NULL_TREE;
30343
30344 /* The number of parms for f' is the number of parms of A used in
30345 the deduced arguments plus non-deduced parms of f. */
30346 unsigned ndlen = 0;
30347 unsigned j;
30348 for (unsigned i = 0; i < len; ++i)
30349 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30350 ++ndlen;
30351 find_template_parameter_info ftpi (fullatparms);
30352 ftpi.find_in_recursive (t: targs);
30353 unsigned nusedatparms = ftpi.num_found ();
30354 unsigned nfparms = nusedatparms + ndlen;
30355 tree gtparms = make_tree_vec (nfparms);
30356
30357 /* Set current_template_parms as in build_deduction_guide. */
30358 auto ctp = make_temp_override (current_template_parms);
30359 current_template_parms = copy_node (fullatparms);
30360 TREE_VALUE (current_template_parms) = gtparms;
30361
30362 j = 0;
30363 unsigned level = 1;
30364
30365 /* First copy over the used parms of A. */
30366 tree atargs = make_tree_vec (natparms);
30367 for (unsigned i = 0; i < natparms; ++i)
30368 {
30369 tree elt = TREE_VEC_ELT (atparms, i);
30370 if (ftpi.found (parm: elt))
30371 {
30372 unsigned index = j++;
30373 tree nelt = rewrite_tparm_list (oldelt: elt, index, level,
30374 targs: atargs, targs_index: i, complain);
30375 TREE_VEC_ELT (gtparms, index) = nelt;
30376 }
30377 }
30378 gcc_checking_assert (j == nusedatparms);
30379
30380 /* Adjust the deduced template args for f to refer to the A parms
30381 with their new indexes. */
30382 if (nusedatparms && nusedatparms != natparms)
30383 targs = tsubst_template_args (t: targs, args: atargs, complain, in_decl);
30384
30385 /* Now rewrite the non-deduced parms of f. */
30386 for (unsigned i = 0; ndlen && i < len; ++i)
30387 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30388 {
30389 --ndlen;
30390 unsigned index = j++;
30391 tree oldlist = TREE_VEC_ELT (ftparms, i);
30392 tree list = rewrite_tparm_list (oldelt: oldlist, index, level,
30393 targs, targs_index: i, complain);
30394 TREE_VEC_ELT (gtparms, index) = list;
30395 }
30396 gtparms = build_tree_list (size_one_node, gtparms);
30397
30398 /* Substitute the deduced arguments plus the rewritten template
30399 parameters into f to get g. This covers the type, copyness,
30400 guideness, and explicit-specifier. */
30401 tree g;
30402 {
30403 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30404 if cp_unevaluated_operand. */
30405 cp_evaluated ev;
30406 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), args: targs, complain,
30407 /*use_spec_table=*/false);
30408 }
30409 if (g == error_mark_node)
30410 continue;
30411 if (nfparms == 0)
30412 {
30413 /* The targs are all non-dependent, so g isn't a template. */
30414 fprime = g;
30415 ret = TREE_TYPE (TREE_TYPE (fprime));
30416 goto non_template;
30417 }
30418 DECL_USE_TEMPLATE (g) = 0;
30419 fprime = build_template_decl (decl: g, parms: gtparms, member_template_p: false);
30420 DECL_TEMPLATE_RESULT (fprime) = g;
30421 TREE_TYPE (fprime) = TREE_TYPE (g);
30422 tree gtargs = template_parms_to_args (parms: gtparms);
30423 DECL_TEMPLATE_INFO (g) = build_template_info (template_decl: fprime, template_args: gtargs);
30424 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
30425
30426 /* Substitute the associated constraints. */
30427 tree ci = get_constraints (f);
30428 if (ci)
30429 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
30430 if (ci == error_mark_node)
30431 continue;
30432
30433 /* Add a constraint that the return type matches the instantiation of
30434 A with the same template arguments. */
30435 ret = TREE_TYPE (TREE_TYPE (fprime));
30436 if (ctad_kind == alias
30437 && (!same_type_p (atype, ret)
30438 /* FIXME this should mean they don't compare as equivalent. */
30439 || dependent_alias_template_spec_p (t: atype, transparent_typedefs: nt_opaque)))
30440 {
30441 tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
30442 ci = append_constraint (ci, same);
30443 }
30444
30445 if (ci)
30446 {
30447 remove_constraints (fprime);
30448 set_constraints (fprime, ci);
30449 }
30450 }
30451 else
30452 {
30453 /* For a non-template deduction guide, if the arguments of A aren't
30454 deducible from the return type, don't add the candidate. */
30455 non_template:
30456 if (ctad_kind == alias
30457 && !type_targs_deducible_from (tmpl, ret))
30458 continue;
30459 }
30460
30461 /* Rewrite the return type of the inherited guide in terms of the
30462 derived class. This is specified as replacing the return type R
30463 with typename CC<R>::type where the partially specialized CC maps a
30464 base class specialization to a specialization of the derived class
30465 having such a base (inducing substitution failure if no such derived
30466 class exists).
30467
30468 As specified this mapping would be done at instantiation time using
30469 non-dependent template arguments, but we do it ahead of time using
30470 the generic arguments. This seems to be good enough since generic
30471 deduction should succeed only if concrete deduction would. */
30472 if (ctad_kind == inherited)
30473 {
30474 processing_template_decl_sentinel ptds (/*reset*/false);
30475 if (TREE_CODE (fprime) == TEMPLATE_DECL)
30476 ++processing_template_decl;
30477
30478 tree targs = type_targs_deducible_from (tmpl, ret);
30479 if (!targs)
30480 continue;
30481
30482 if (TREE_CODE (f) != TEMPLATE_DECL)
30483 fprime = copy_decl (fprime);
30484 tree fntype = TREE_TYPE (fprime);
30485 ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), arglist: targs,
30486 in_decl, NULL_TREE, entering_scope: false, complain);
30487 fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype));
30488 TREE_TYPE (fprime) = fntype;
30489 if (TREE_CODE (fprime) == TEMPLATE_DECL)
30490 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
30491 }
30492
30493 aguides = lookup_add (fns: fprime, lookup: aguides);
30494 }
30495
30496 return aguides;
30497}
30498
30499/* CTOR is a using-decl inheriting the constructors of some base of the class
30500 template TMPL; adjust the base's guides be deduction guides for TMPL. */
30501
30502static tree
30503inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
30504{
30505 /* [over.match.class.deduct]: In addition, if C is defined and inherits
30506 constructors ([namespace.udecl]) from a direct base class denoted in the
30507 base-specifier-list by a class-or-decltype B, let A be an alias template
30508 whose template parameter list is that of C and whose defining-type-id is
30509 B. If A is a deducible template ([dcl.type.simple]), the set contains the
30510 guides of A with the return type R of each guide replaced with typename
30511 CC::type given a class template
30512
30513 template <typename> class CC;
30514
30515 whose primary template is not defined and with a single partial
30516 specialization whose template parameter list is that of A and whose
30517 template argument list is a specialization of A with the template argument
30518 list of A ([temp.dep.type]) having a member typedef type designating a
30519 template specialization with the template argument list of A but with C as
30520 the template. */
30521
30522 /* FIXME: Also recognize inherited constructors of the form 'using C::B::B',
30523 which seem to be represented with TYPENAME_TYPE C::B as USING_DECL_SCOPE?
30524 And recognize constructors inherited from a non-dependent base class, which
30525 seem to be missing from the overload set entirely? */
30526 tree scope = USING_DECL_SCOPE (ctor);
30527 if (!CLASS_TYPE_P (scope)
30528 || !CLASSTYPE_TEMPLATE_INFO (scope)
30529 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
30530 return NULL_TREE;
30531
30532 tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
30533 bool any_dguides_p;
30534 tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
30535 any_dguides_p, complain);
30536 return alias_ctad_tweaks (tmpl: t, uguides);
30537}
30538
30539/* If template arguments for TMPL can be deduced from TYPE, return
30540 the deduced arguments, otherwise return NULL_TREE.
30541 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
30542 [over.match.class.deduct].
30543
30544 This check is specified in terms of partial specialization, so the behavior
30545 should be parallel to that of get_partial_spec_bindings. */
30546
30547tree
30548type_targs_deducible_from (tree tmpl, tree type)
30549{
30550 tree tparms, ttype;
30551 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
30552 {
30553 /* If tmpl is a class template, this is trivial: it's deducible if
30554 TYPE is a specialization of TMPL. */
30555 if (DECL_CLASS_TEMPLATE_P (tmpl))
30556 {
30557 if (CLASS_TYPE_P (type)
30558 && CLASSTYPE_TEMPLATE_INFO (type)
30559 && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
30560 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
30561 else
30562 return NULL_TREE;
30563 }
30564
30565 /* Otherwise it's an alias template. */
30566 tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
30567 ttype = TREE_TYPE (tmpl);
30568 }
30569 else
30570 {
30571 /* TMPL is a synthetic alias template represented as a TREE_LIST as
30572 per alias_ctad_tweaks. */
30573 tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
30574 ttype = TREE_VALUE (tmpl);
30575 tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype));
30576 }
30577
30578 int len = TREE_VEC_LENGTH (tparms);
30579 tree targs = make_tree_vec (len);
30580 bool tried_array_deduction = (cxx_dialect < cxx17);
30581
30582 again:
30583 if (unify (tparms, targs, parm: ttype, arg: type,
30584 UNIFY_ALLOW_NONE, explain_p: false))
30585 return NULL_TREE;
30586
30587 /* We don't fail on an undeduced targ the second time through (like
30588 get_partial_spec_bindings) because we're going to try defaults. */
30589 for (int i = 0; i < len; ++i)
30590 if (! TREE_VEC_ELT (targs, i))
30591 {
30592 tree tparm = TREE_VEC_ELT (tparms, i);
30593 tparm = TREE_VALUE (tparm);
30594
30595 if (!tried_array_deduction
30596 && TREE_CODE (tparm) == TYPE_DECL)
30597 {
30598 try_array_deduction (tparms, targs, parm: ttype);
30599 tried_array_deduction = true;
30600 if (TREE_VEC_ELT (targs, i))
30601 goto again;
30602 }
30603 /* If the type parameter is a parameter pack, then it will be deduced
30604 to an empty parameter pack. This is another case that doesn't model
30605 well as partial specialization. */
30606 if (template_parameter_pack_p (parm: tparm))
30607 {
30608 tree arg;
30609 if (TREE_CODE (tparm) == PARM_DECL)
30610 {
30611 arg = make_node (NONTYPE_ARGUMENT_PACK);
30612 TREE_CONSTANT (arg) = 1;
30613 }
30614 else
30615 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
30616 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
30617 TREE_VEC_ELT (targs, i) = arg;
30618 }
30619 }
30620
30621 /* Maybe add in default template args. This seems like a flaw in the
30622 specification in terms of partial specialization, since it says the
30623 partial specialization has the template parameter list of A, but a
30624 partial specialization can't have default targs. */
30625 targs = coerce_template_parms (parms: tparms, args: targs, in_decl: tmpl, complain: tf_none);
30626 if (targs == error_mark_node)
30627 return NULL_TREE;
30628
30629 /* I believe we don't need the template_template_parm_bindings_ok_p call
30630 because coerce_template_parms did coerce_template_template_parms. */
30631
30632 if (!constraints_satisfied_p (tmpl, targs))
30633 return NULL_TREE;
30634
30635 return targs;
30636}
30637
30638/* Return artificial deduction guides built from the constructors of class
30639 template TMPL. */
30640
30641static tree
30642ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
30643{
30644 tree outer_args = outer_template_args (decl: tmpl);
30645 tree type = TREE_TYPE (most_general_template (tmpl));
30646
30647 tree cands = NULL_TREE;
30648
30649 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
30650 {
30651 /* We handle C++23 inherited CTAD below. */
30652 if (iter.using_p ())
30653 continue;
30654
30655 tree guide = build_deduction_guide (type, ctor: *iter, outer_args, complain);
30656 cands = lookup_add (fns: guide, lookup: cands);
30657 }
30658
30659 if (cxx_dialect >= cxx23)
30660 for (tree ctor : ovl_range (CLASSTYPE_CONSTRUCTORS (type)))
30661 if (TREE_CODE (ctor) == USING_DECL)
30662 {
30663 tree uguides = inherited_ctad_tweaks (tmpl, ctor, complain);
30664 if (uguides)
30665 cands = lookup_add (fns: uguides, lookup: cands);
30666 }
30667
30668 /* Add implicit default constructor deduction guide. */
30669 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
30670 {
30671 tree guide = build_deduction_guide (type, ctor: type, outer_args,
30672 complain);
30673 cands = lookup_add (fns: guide, lookup: cands);
30674 }
30675
30676 /* Add copy guide. */
30677 {
30678 tree gtype = build_reference_type (type);
30679 tree guide = build_deduction_guide (type, ctor: gtype, outer_args,
30680 complain);
30681 cands = lookup_add (fns: guide, lookup: cands);
30682 }
30683
30684 return cands;
30685}
30686
30687static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
30688
30689/* Return the non-aggregate deduction guides for deducible template TMPL. The
30690 aggregate candidate is added separately because it depends on the
30691 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30692 guide. */
30693
30694static tree
30695deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
30696{
30697 tree guides = NULL_TREE;
30698 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30699 {
30700 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30701 tree tinfo = get_template_info (t: under);
30702 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
30703 complain);
30704 }
30705 else
30706 {
30707 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
30708 name: dguide_name (tmpl),
30709 LOOK_want::NORMAL, /*complain*/false);
30710 if (guides == error_mark_node)
30711 guides = NULL_TREE;
30712 else
30713 any_dguides_p = true;
30714 }
30715
30716 /* Cache the deduction guides for a template. We also remember the result of
30717 lookup, and rebuild everything if it changes; should be very rare. */
30718 /* FIXME: Also rebuild if this is a class template that inherits guides from a
30719 base class, and lookup for the latter changed. */
30720 tree_pair_p cache = NULL;
30721 if (tree_pair_p &r
30722 = hash_map_safe_get_or_insert<hm_ggc> (h&: dguide_cache, k: tmpl))
30723 {
30724 cache = r;
30725 if (cache->purpose == guides)
30726 return cache->value;
30727 }
30728 else
30729 {
30730 r = cache = ggc_cleared_alloc<tree_pair_s> ();
30731 cache->purpose = guides;
30732 }
30733
30734 tree cands = NULL_TREE;
30735 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30736 cands = alias_ctad_tweaks (tmpl, uguides: guides);
30737 else
30738 {
30739 cands = ctor_deduction_guides_for (tmpl, complain);
30740 for (ovl_iterator it (guides); it; ++it)
30741 cands = lookup_add (fns: *it, lookup: cands);
30742 }
30743
30744 cache->value = cands;
30745 return cands;
30746}
30747
30748/* Return whether TMPL is a (class template argument-) deducible template. */
30749
30750bool
30751ctad_template_p (tree tmpl)
30752{
30753 /* A deducible template is either a class template or is an alias template
30754 whose defining-type-id is of the form
30755
30756 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30757
30758 where the nested-name-specifier (if any) is non-dependent and the
30759 template-name of the simple-template-id names a deducible template. */
30760
30761 if (DECL_CLASS_TEMPLATE_P (tmpl)
30762 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30763 return true;
30764 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
30765 return false;
30766 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30767 if (tree tinfo = get_template_info (t: orig))
30768 return ctad_template_p (TI_TEMPLATE (tinfo));
30769 return false;
30770}
30771
30772/* Deduce template arguments for the class template placeholder PTYPE for
30773 template TMPL based on the initializer INIT, and return the resulting
30774 type. */
30775
30776static tree
30777do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
30778 int flags, tsubst_flags_t complain)
30779{
30780 /* We should have handled this in the caller. */
30781 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30782 return ptype;
30783
30784 /* If the class was erroneous, don't try to deduce, because that
30785 can generate a lot of diagnostic. */
30786 if (TREE_TYPE (tmpl)
30787 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
30788 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
30789 return ptype;
30790
30791 /* Wait until the enclosing scope is non-dependent. */
30792 if (DECL_CLASS_SCOPE_P (tmpl)
30793 && dependent_type_p (DECL_CONTEXT (tmpl)))
30794 return ptype;
30795
30796 /* Initializing one placeholder from another. */
30797 if (init
30798 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
30799 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
30800 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
30801 == TEMPLATE_PARM_INDEX)))
30802 && is_auto (TREE_TYPE (init))
30803 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
30804 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
30805
30806 if (!ctad_template_p (tmpl))
30807 {
30808 if (complain & tf_error)
30809 error ("non-deducible template %qT used without template arguments", tmpl);
30810 return error_mark_node;
30811 }
30812 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
30813 {
30814 if (complain & tf_error)
30815 {
30816 /* Be permissive with equivalent alias templates. */
30817 tree u = get_underlying_template (tmpl);
30818 diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
30819 bool complained
30820 = emit_diagnostic (dk, input_location, 0,
30821 "alias template deduction only available "
30822 "with %<-std=c++20%> or %<-std=gnu++20%>");
30823 if (u == tmpl)
30824 return error_mark_node;
30825 else if (complained)
30826 {
30827 inform (input_location, "use %qD directly instead", u);
30828 tmpl = u;
30829 }
30830 }
30831 else
30832 return error_mark_node;
30833 }
30834
30835 /* Wait until the initializer is non-dependent. */
30836 if (type_dependent_expression_p (expression: init))
30837 return ptype;
30838
30839 if (outer_targs)
30840 {
30841 int args_depth = TMPL_ARGS_DEPTH (outer_targs);
30842 int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
30843 if (parms_depth > 1)
30844 {
30845 /* Substitute outer arguments into this CTAD template from the
30846 current instantiation. */
30847 int want = std::min (a: args_depth, b: parms_depth - 1);
30848 outer_targs = strip_innermost_template_args (args: outer_targs,
30849 extra_levels: args_depth - want);
30850 tmpl = tsubst (t: tmpl, args: outer_targs, complain, NULL_TREE);
30851 if (tmpl == error_mark_node)
30852 return error_mark_node;
30853 }
30854 }
30855
30856 /* Don't bother with the alias rules for an equivalent template. */
30857 tmpl = get_underlying_template (tmpl);
30858
30859 tree type = TREE_TYPE (tmpl);
30860
30861 bool try_list_cand = false;
30862 bool list_init_p = false;
30863
30864 releasing_vec rv_args = NULL;
30865 vec<tree,va_gc> *&args = *&rv_args;
30866 if (init == NULL_TREE)
30867 args = make_tree_vector ();
30868 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
30869 {
30870 list_init_p = true;
30871 try_list_cand = true;
30872 if (CONSTRUCTOR_NELTS (init) == 1
30873 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
30874 {
30875 /* As an exception, the first phase in 16.3.1.7 (considering the
30876 initializer list as a single argument) is omitted if the
30877 initializer list consists of a single expression of type cv U,
30878 where U is a specialization of C or a class derived from a
30879 specialization of C. */
30880 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
30881 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
30882 try_list_cand = false;
30883 }
30884 if (try_list_cand || is_std_init_list (type))
30885 args = make_tree_vector_single (init);
30886 else
30887 args = make_tree_vector_from_ctor (init);
30888 }
30889 else if (TREE_CODE (init) == TREE_LIST)
30890 args = make_tree_vector_from_list (init);
30891 else
30892 args = make_tree_vector_single (init);
30893
30894 /* Do this now to avoid problems with erroneous args later on. */
30895 args = resolve_args (args, complain);
30896 if (args == NULL)
30897 return error_mark_node;
30898
30899 bool any_dguides_p = false;
30900 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
30901 if (cands == error_mark_node)
30902 return error_mark_node;
30903
30904 /* Prune explicit deduction guides in copy-initialization context (but
30905 not copy-list-initialization). */
30906 bool elided = false;
30907 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
30908 {
30909 for (lkp_iterator iter (cands); !elided && iter; ++iter)
30910 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30911 elided = true;
30912
30913 if (elided)
30914 {
30915 /* Found a nonconverting guide, prune the candidates. */
30916 tree pruned = NULL_TREE;
30917 for (lkp_iterator iter (cands); iter; ++iter)
30918 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30919 pruned = lookup_add (fns: *iter, lookup: pruned);
30920
30921 cands = pruned;
30922 }
30923 }
30924
30925 if (!any_dguides_p)
30926 if (tree guide = maybe_aggr_guide (tmpl, init, args))
30927 cands = lookup_add (fns: guide, lookup: cands);
30928
30929 tree fndecl = error_mark_node;
30930
30931 /* If this is list-initialization and the class has a list guide, first
30932 try deducing from the list as a single argument, as [over.match.list]. */
30933 if (try_list_cand)
30934 {
30935 tree list_cands = NULL_TREE;
30936 for (tree dg : lkp_range (cands))
30937 if (is_list_ctor (dg))
30938 list_cands = lookup_add (fns: dg, lookup: list_cands);
30939 if (list_cands)
30940 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
30941 if (fndecl == error_mark_node)
30942 {
30943 /* That didn't work, now try treating the list as a sequence of
30944 arguments. */
30945 release_tree_vector (args);
30946 args = make_tree_vector_from_ctor (init);
30947 args = resolve_args (args, complain);
30948 if (args == NULL)
30949 return error_mark_node;
30950 }
30951 }
30952
30953 if (elided && !cands)
30954 {
30955 error ("cannot deduce template arguments for copy-initialization"
30956 " of %qT, as it has no non-explicit deduction guides or "
30957 "user-declared constructors", type);
30958 return error_mark_node;
30959 }
30960 else if (!cands && fndecl == error_mark_node)
30961 {
30962 error ("cannot deduce template arguments of %qT, as it has no viable "
30963 "deduction guides", type);
30964 return error_mark_node;
30965 }
30966
30967 if (fndecl == error_mark_node)
30968 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
30969
30970 if (fndecl == error_mark_node)
30971 {
30972 if (complain & tf_warning_or_error)
30973 {
30974 error ("class template argument deduction failed:");
30975 perform_dguide_overload_resolution (cands, args, complain);
30976 if (elided)
30977 inform (input_location, "explicit deduction guides not considered "
30978 "for copy-initialization");
30979 }
30980 return error_mark_node;
30981 }
30982 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30983 constructor is chosen, the initialization is ill-formed. */
30984 else if (flags & LOOKUP_ONLYCONVERTING)
30985 {
30986 if (DECL_NONCONVERTING_P (fndecl))
30987 {
30988 if (complain & tf_warning_or_error)
30989 {
30990 // TODO: Pass down location from cp_finish_decl.
30991 error ("class template argument deduction for %qT failed: "
30992 "explicit deduction guide selected in "
30993 "copy-list-initialization", type);
30994 inform (DECL_SOURCE_LOCATION (fndecl),
30995 "explicit deduction guide declared here");
30996
30997 }
30998 return error_mark_node;
30999 }
31000 }
31001
31002 /* If CTAD succeeded but the type doesn't have any explicit deduction
31003 guides, this deduction might not be what the user intended. */
31004 if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
31005 {
31006 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
31007 || global_dc->m_warn_system_headers)
31008 && warning (OPT_Wctad_maybe_unsupported,
31009 "%qT may not intend to support class template argument "
31010 "deduction", type))
31011 inform (input_location, "add a deduction guide to suppress this "
31012 "warning");
31013 }
31014
31015 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
31016 cp_type_quals (ptype));
31017}
31018
31019/* Return true if INIT is an unparenthesized id-expression or an
31020 unparenthesized class member access. Used for the argument of
31021 decltype(auto). */
31022
31023bool
31024unparenthesized_id_or_class_member_access_p (tree init)
31025{
31026 STRIP_ANY_LOCATION_WRAPPER (init);
31027
31028 /* We need to be able to tell '(r)' and 'r' apart (when it's of
31029 reference type). Only the latter is an id-expression. */
31030 if (REFERENCE_REF_P (init)
31031 && !REF_PARENTHESIZED_P (init))
31032 init = TREE_OPERAND (init, 0);
31033 return (DECL_P (init)
31034 || ((TREE_CODE (init) == COMPONENT_REF
31035 || TREE_CODE (init) == SCOPE_REF)
31036 && !REF_PARENTHESIZED_P (init)));
31037}
31038
31039/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
31040 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
31041 The CONTEXT determines the context in which auto deduction is performed
31042 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
31043
31044 OUTER_TARGS is used during template argument deduction (context == adc_unify)
31045 to properly substitute the result. It's also used in the adc_unify and
31046 adc_requirement contexts to communicate the necessary template arguments
31047 to satisfaction. OUTER_TARGS is ignored in other contexts.
31048
31049 Additionally for adc_unify contexts TMPL is the template for which TYPE
31050 is a template parameter type.
31051
31052 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
31053 scope may be appended to the list of deduced template arguments prior to
31054 determining constraint satisfaction as appropriate. */
31055
31056tree
31057do_auto_deduction (tree type, tree init, tree auto_node,
31058 tsubst_flags_t complain /* = tf_warning_or_error */,
31059 auto_deduction_context context /* = adc_unspecified */,
31060 tree outer_targs /* = NULL_TREE */,
31061 int flags /* = LOOKUP_NORMAL */,
31062 tree tmpl /* = NULL_TREE */)
31063{
31064 if (type == error_mark_node || init == error_mark_node)
31065 return error_mark_node;
31066
31067 if (init && type_dependent_expression_p (expression: init)
31068 && context != adc_unify)
31069 /* Defining a subset of type-dependent expressions that we can deduce
31070 from ahead of time isn't worth the trouble. */
31071 return type;
31072
31073 /* Similarly, we can't deduce from another undeduced decl. */
31074 if (init && undeduced_auto_decl (init))
31075 return type;
31076
31077 /* We may be doing a partial substitution, but we still want to replace
31078 auto_node. */
31079 complain &= ~tf_partial;
31080
31081 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
31082 {
31083 /* We don't recurse here because we can't deduce from a nested
31084 initializer_list. */
31085 if (CONSTRUCTOR_ELTS (init))
31086 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
31087 elt.value = resolve_nondeduced_context (orig_expr: elt.value, complain);
31088 }
31089 else if (init)
31090 init = resolve_nondeduced_context (orig_expr: init, complain);
31091
31092 /* In C++23, we must deduce the type to int&& for code like
31093 decltype(auto) f(int&& x) { return (x); }
31094 or
31095 auto&& f(int x) { return x; }
31096 so we use treat_lvalue_as_rvalue_p. But don't do it for
31097 decltype(auto) f(int x) { return x; }
31098 where we should deduce 'int' rather than 'int&&'; transmogrifying
31099 INIT to an rvalue would break that. */
31100 tree r;
31101 if (cxx_dialect >= cxx23
31102 && context == adc_return_type
31103 && (!AUTO_IS_DECLTYPE (auto_node)
31104 || !unparenthesized_id_or_class_member_access_p (init))
31105 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
31106 /*return*/true)))
31107 init = r;
31108
31109 if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
31110 /* C++17 class template argument deduction. */
31111 return do_class_deduction (ptype: type, tmpl: ctmpl, init, outer_targs, flags, complain);
31112
31113 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
31114 /* Nothing we can do with this, even in deduction context. */
31115 return type;
31116
31117 location_t loc = cp_expr_loc_or_input_loc (t: init);
31118
31119 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
31120 with either a new invented type template parameter U or, if the
31121 initializer is a braced-init-list (8.5.4), with
31122 std::initializer_list<U>. */
31123 if (BRACE_ENCLOSED_INITIALIZER_P (init))
31124 {
31125 if (!DIRECT_LIST_INIT_P (init))
31126 type = listify_autos (type, auto_node);
31127 else if (CONSTRUCTOR_NELTS (init) == 1)
31128 init = CONSTRUCTOR_ELT (init, 0)->value;
31129 else
31130 {
31131 if (complain & tf_warning_or_error)
31132 {
31133 if (permerror (loc, "direct-list-initialization of "
31134 "%<auto%> requires exactly one element"))
31135 inform (loc,
31136 "for deduction to %<std::initializer_list%>, use copy-"
31137 "list-initialization (i.e. add %<=%> before the %<{%>)");
31138 }
31139 type = listify_autos (type, auto_node);
31140 }
31141 }
31142
31143 if (type == error_mark_node || init == error_mark_node)
31144 return error_mark_node;
31145
31146 tree targs;
31147 if (context == adc_decomp_type
31148 && auto_node == type
31149 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
31150 {
31151 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
31152 and initializer has array type, deduce cv-qualified array type. */
31153 targs = make_tree_vec (1);
31154 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
31155 }
31156 else if (AUTO_IS_DECLTYPE (auto_node))
31157 {
31158 const bool id = unparenthesized_id_or_class_member_access_p (init);
31159 tree deduced = finish_decltype_type (init, id, complain);
31160 deduced = canonicalize_type_argument (arg: deduced, complain);
31161 if (deduced == error_mark_node)
31162 return error_mark_node;
31163 targs = make_tree_vec (1);
31164 TREE_VEC_ELT (targs, 0) = deduced;
31165 }
31166 else
31167 {
31168 if (error_operand_p (t: init))
31169 return error_mark_node;
31170
31171 tree parms = build_tree_list (NULL_TREE, type);
31172 tree tparms;
31173
31174 if (flag_concepts_ts)
31175 tparms = extract_autos (type);
31176 else
31177 {
31178 tparms = make_tree_vec (1);
31179 TREE_VEC_ELT (tparms, 0)
31180 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
31181 }
31182
31183 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
31184 int val = type_unification_real (tparms, full_targs: targs, xparms: parms, xargs: &init, xnargs: 1, subr: 0,
31185 strict: DEDUCE_CALL,
31186 NULL, /*explain_p=*/false);
31187 if (val > 0)
31188 {
31189 if (processing_template_decl)
31190 /* Try again at instantiation time. */
31191 return type;
31192 if (type && type != error_mark_node
31193 && (complain & tf_error))
31194 /* If type is error_mark_node a diagnostic must have been
31195 emitted by now. Also, having a mention to '<type error>'
31196 in the diagnostic is not really useful to the user. */
31197 {
31198 if (cfun
31199 && FNDECL_USED_AUTO (current_function_decl)
31200 && (auto_node
31201 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
31202 && LAMBDA_FUNCTION_P (current_function_decl))
31203 error_at (loc, "unable to deduce lambda return type from %qE",
31204 init);
31205 else
31206 error_at (loc, "unable to deduce %qT from %qE", type, init);
31207 type_unification_real (tparms, full_targs: targs, xparms: parms, xargs: &init, xnargs: 1, subr: 0,
31208 strict: DEDUCE_CALL,
31209 NULL, /*explain_p=*/true);
31210 }
31211 return error_mark_node;
31212 }
31213 }
31214
31215 /* Check any placeholder constraints against the deduced type. */
31216 if (processing_template_decl && context == adc_unify)
31217 /* Constraints will be checked after deduction. */;
31218 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
31219 {
31220 if (processing_template_decl)
31221 {
31222 gcc_checking_assert (context == adc_variable_type
31223 || context == adc_return_type
31224 || context == adc_decomp_type);
31225 gcc_checking_assert (!type_dependent_expression_p (init));
31226 /* If the constraint is dependent, we need to wait until
31227 instantiation time to resolve the placeholder. */
31228 if (placeholder_type_constraint_dependent_p (t: constr))
31229 return type;
31230 }
31231
31232 if (context == adc_return_type
31233 || context == adc_variable_type
31234 || context == adc_decomp_type)
31235 if (tree fn = current_function_decl)
31236 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
31237 {
31238 outer_targs = DECL_TEMPLATE_INFO (fn)
31239 ? DECL_TI_ARGS (fn) : NULL_TREE;
31240 if (LAMBDA_FUNCTION_P (fn))
31241 {
31242 /* As in satisfy_declaration_constraints. */
31243 tree regen_args = lambda_regenerating_args (t: fn);
31244 if (outer_targs)
31245 outer_targs = add_to_template_args (args: regen_args, extra_args: outer_targs);
31246 else
31247 outer_targs = regen_args;
31248 }
31249 }
31250
31251 tree full_targs = outer_targs;
31252 if (context == adc_unify && tmpl)
31253 full_targs = add_outermost_template_args (args: tmpl, extra_args: full_targs);
31254 full_targs = add_to_template_args (args: full_targs, extra_args: targs);
31255
31256 /* HACK: Compensate for callers not always communicating all levels of
31257 outer template arguments by filling in the outermost missing levels
31258 with dummy levels before checking satisfaction. We'll still crash
31259 if the constraint depends on a template argument belonging to one of
31260 these missing levels, but this hack otherwise allows us to handle a
31261 large subset of possible constraints (including all non-dependent
31262 constraints). */
31263 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
31264 - TMPL_ARGS_DEPTH (full_targs)))
31265 {
31266 tree dummy_levels = make_tree_vec (missing_levels);
31267 for (int i = 0; i < missing_levels; ++i)
31268 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
31269 full_targs = add_to_template_args (args: dummy_levels, extra_args: full_targs);
31270 }
31271
31272 if (!constraints_satisfied_p (auto_node, full_targs))
31273 {
31274 if (complain & tf_warning_or_error)
31275 {
31276 auto_diagnostic_group d;
31277 switch (context)
31278 {
31279 case adc_unspecified:
31280 case adc_unify:
31281 error_at (loc, "placeholder constraints not satisfied");
31282 break;
31283 case adc_variable_type:
31284 case adc_decomp_type:
31285 error_at (loc, "deduced initializer does not satisfy "
31286 "placeholder constraints");
31287 break;
31288 case adc_return_type:
31289 error_at (loc, "deduced return type does not satisfy "
31290 "placeholder constraints");
31291 break;
31292 case adc_requirement:
31293 error_at (loc, "deduced expression type does not satisfy "
31294 "placeholder constraints");
31295 break;
31296 }
31297 diagnose_constraints (loc, auto_node, full_targs);
31298 }
31299 return error_mark_node;
31300 }
31301 }
31302
31303 if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
31304 {
31305 /* Substitute this level-less auto via tsubst by temporarily
31306 overriding its level to 1. */
31307 TEMPLATE_TYPE_LEVEL (auto_node) = 1;
31308 type = tsubst (t: type, args: targs, complain, NULL_TREE);
31309 TEMPLATE_TYPE_LEVEL (auto_node) = 0;
31310 return type;
31311 }
31312
31313 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
31314 /* The outer template arguments are already substituted into type
31315 (but we still may have used them for constraint checking above). */;
31316 else if (context == adc_unify)
31317 targs = add_to_template_args (args: outer_targs, extra_args: targs);
31318 else if (processing_template_decl)
31319 targs = add_to_template_args (args: current_template_args (), extra_args: targs);
31320 return tsubst (t: type, args: targs, complain, NULL_TREE);
31321}
31322
31323/* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
31324 result. */
31325
31326tree
31327splice_late_return_type (tree type, tree late_return_type)
31328{
31329 if (late_return_type)
31330 {
31331 gcc_assert (is_auto (type) || seen_error ());
31332 return late_return_type;
31333 }
31334
31335 if (tree auto_node = find_type_usage (t: type, pred: is_auto))
31336 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
31337 {
31338 /* In an abbreviated function template we didn't know we were dealing
31339 with a function template when we saw the auto return type, so rebuild
31340 the return type using an auto with the correct level. */
31341 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), set_canonical: false);
31342 tree auto_vec = make_tree_vec (1);
31343 TREE_VEC_ELT (auto_vec, 0) = new_auto;
31344 tree targs = add_outermost_template_args (args: current_template_args (),
31345 extra_args: auto_vec);
31346 /* Also rebuild the constraint info in terms of the new auto. */
31347 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
31348 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
31349 = build_tree_list (current_template_parms,
31350 tsubst_constraint (TREE_VALUE (ci), targs,
31351 tf_none, NULL_TREE));
31352 TYPE_CANONICAL (new_auto) = canonical_type_parameter (type: new_auto);
31353 return tsubst (t: type, args: targs, complain: tf_none, NULL_TREE);
31354 }
31355 return type;
31356}
31357
31358/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
31359 'decltype(auto)' or a deduced class template. */
31360
31361bool
31362is_auto (const_tree type)
31363{
31364 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
31365 && (TYPE_IDENTIFIER (type) == auto_identifier
31366 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
31367 return true;
31368 else
31369 return false;
31370}
31371
31372/* for_each_template_parm callback for type_uses_auto. */
31373
31374int
31375is_auto_r (tree tp, void */*data*/)
31376{
31377 return is_auto (type: tp);
31378}
31379
31380/* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
31381 a use of `auto'. Returns NULL_TREE otherwise. */
31382
31383tree
31384type_uses_auto (tree type)
31385{
31386 if (type == NULL_TREE)
31387 return NULL_TREE;
31388
31389 /* For parameter packs, check the contents of the pack. */
31390 if (PACK_EXPANSION_P (type))
31391 type = PACK_EXPANSION_PATTERN (type);
31392
31393 if (flag_concepts_ts)
31394 {
31395 /* The Concepts TS allows multiple autos in one type-specifier; just
31396 return the first one we find, do_auto_deduction will collect all of
31397 them. */
31398 if (uses_template_parms (t: type))
31399 return for_each_template_parm (t: type, fn: is_auto_r, /*data*/NULL,
31400 /*visited*/NULL, /*nondeduced*/include_nondeduced_p: false);
31401 else
31402 return NULL_TREE;
31403 }
31404 else
31405 return find_type_usage (t: type, pred: is_auto);
31406}
31407
31408/* Report ill-formed occurrences of auto types in ARGUMENTS. If
31409 concepts are enabled, auto is acceptable in template arguments, but
31410 only when TEMPL identifies a template class. Return TRUE if any
31411 such errors were reported. */
31412
31413bool
31414check_auto_in_tmpl_args (tree tmpl, tree args)
31415{
31416 if (!flag_concepts_ts)
31417 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
31418 have already been rejected by the parser more generally. */
31419 return false;
31420
31421 /* If there were previous errors, nevermind. */
31422 if (!args || TREE_CODE (args) != TREE_VEC)
31423 return false;
31424
31425 /* If TMPL is an identifier, we're parsing and we can't tell yet
31426 whether TMPL is supposed to be a type, a function or a variable.
31427 We'll only be able to tell during template substitution, so we
31428 expect to be called again then. If concepts are enabled and we
31429 know we have a type, we're ok. */
31430 if (identifier_p (t: tmpl)
31431 || (DECL_P (tmpl)
31432 && (DECL_TYPE_TEMPLATE_P (tmpl)
31433 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
31434 return false;
31435
31436 /* Quickly search for any occurrences of auto; usually there won't
31437 be any, and then we'll avoid allocating the vector. */
31438 if (!type_uses_auto (type: args))
31439 return false;
31440
31441 bool errors = false;
31442
31443 tree vec = extract_autos (type: args);
31444 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
31445 {
31446 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
31447 error_at (DECL_SOURCE_LOCATION (xauto),
31448 "invalid use of %qT in template argument", xauto);
31449 errors = true;
31450 }
31451
31452 return errors;
31453}
31454
31455/* Recursively walk over && expressions searching for EXPR. Return a reference
31456 to that expression. */
31457
31458static tree *find_template_requirement (tree *t, tree key)
31459{
31460 if (*t == key)
31461 return t;
31462 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
31463 {
31464 if (tree *p = find_template_requirement (t: &TREE_OPERAND (*t, 0), key))
31465 return p;
31466 if (tree *p = find_template_requirement (t: &TREE_OPERAND (*t, 1), key))
31467 return p;
31468 }
31469 return 0;
31470}
31471
31472/* Convert the generic type parameters in PARM that match the types given in the
31473 range [START_IDX, END_IDX) from the current_template_parms into generic type
31474 packs. */
31475
31476tree
31477convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
31478{
31479 tree current = current_template_parms;
31480 int depth = TMPL_PARMS_DEPTH (current);
31481 current = INNERMOST_TEMPLATE_PARMS (current);
31482 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
31483
31484 for (int i = 0; i < start_idx; ++i)
31485 TREE_VEC_ELT (replacement, i)
31486 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
31487
31488 for (int i = start_idx; i < end_idx; ++i)
31489 {
31490 /* Create a distinct parameter pack type from the current parm and add it
31491 to the replacement args to tsubst below into the generic function
31492 parameter. */
31493 tree node = TREE_VEC_ELT (current, i);
31494 tree o = TREE_TYPE (TREE_VALUE (node));
31495 tree t = copy_type (o);
31496 TEMPLATE_TYPE_PARM_INDEX (t)
31497 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
31498 type: t, levels: 0, args: 0, complain: tf_none);
31499 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
31500 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
31501 TYPE_MAIN_VARIANT (t) = t;
31502 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
31503 TYPE_CANONICAL (t) = canonical_type_parameter (type: t);
31504 TREE_VEC_ELT (replacement, i) = t;
31505
31506 /* Replace the current template parameter with new pack. */
31507 TREE_VALUE (node) = TREE_CHAIN (t);
31508
31509 /* Surgically adjust the associated constraint of adjusted parameter
31510 and it's corresponding contribution to the current template
31511 requirements. */
31512 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
31513 {
31514 tree id = unpack_concept_check (constr);
31515 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
31516 /* Use UNKNOWN_LOCATION so write_template_args can tell the
31517 difference between this and a fold the user wrote. */
31518 location_t loc = UNKNOWN_LOCATION;
31519 tree fold = finish_left_unary_fold_expr (loc, constr,
31520 TRUTH_ANDIF_EXPR);
31521 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
31522
31523 /* If there was a constraint, we also need to replace that in
31524 the template requirements, which we've already built. */
31525 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
31526 reqs = find_template_requirement (t: reqs, key: constr);
31527 *reqs = fold;
31528 }
31529 }
31530
31531 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
31532 TREE_VEC_ELT (replacement, i)
31533 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
31534
31535 /* If there are more levels then build up the replacement with the outer
31536 template parms. */
31537 if (depth > 1)
31538 replacement = add_to_template_args (args: template_parms_to_args
31539 (TREE_CHAIN (current_template_parms)),
31540 extra_args: replacement);
31541
31542 return tsubst (t: parm, args: replacement, complain: tf_none, NULL_TREE);
31543}
31544
31545/* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
31546 0..N-1. */
31547
31548void
31549declare_integer_pack (void)
31550{
31551 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
31552 build_function_type_list (integer_type_node,
31553 integer_type_node,
31554 NULL_TREE),
31555 NULL_TREE, ECF_CONST);
31556 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
31557 set_decl_built_in_function (decl: ipfn, fclass: BUILT_IN_FRONTEND,
31558 fcode: CP_BUILT_IN_INTEGER_PACK);
31559}
31560
31561/* Walk the decl or type specialization table calling FN on each
31562 entry. */
31563
31564void
31565walk_specializations (bool decls_p,
31566 void (*fn) (bool decls_p, spec_entry *entry, void *data),
31567 void *data)
31568{
31569 spec_hash_table *table = decls_p ? decl_specializations
31570 : type_specializations;
31571 spec_hash_table::iterator end (table->end ());
31572 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
31573 fn (decls_p, *iter, data);
31574}
31575
31576/* Lookup the specialization of *ELT, in the decl or type
31577 specialization table. Return the SPEC that's already there, or
31578 NULL if nothing. */
31579
31580tree
31581match_mergeable_specialization (bool decl_p, spec_entry *elt)
31582{
31583 hash_table<spec_hasher> *specializations
31584 = decl_p ? decl_specializations : type_specializations;
31585 hashval_t hash = spec_hasher::hash (e: elt);
31586 auto *slot = specializations->find_slot_with_hash (comparable: elt, hash, insert: NO_INSERT);
31587
31588 if (slot)
31589 return (*slot)->spec;
31590
31591 return NULL_TREE;
31592}
31593
31594/* Return flags encoding whether SPEC is on the instantiation and/or
31595 specialization lists of TMPL. */
31596
31597unsigned
31598get_mergeable_specialization_flags (tree tmpl, tree decl)
31599{
31600 unsigned flags = 0;
31601
31602 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
31603 inst; inst = TREE_CHAIN (inst))
31604 if (TREE_VALUE (inst) == decl)
31605 {
31606 flags |= 1;
31607 break;
31608 }
31609
31610 if (CLASS_TYPE_P (TREE_TYPE (decl))
31611 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
31612 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
31613 /* Only need to search if DECL is a partial specialization. */
31614 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
31615 part; part = TREE_CHAIN (part))
31616 if (TREE_VALUE (part) == decl)
31617 {
31618 flags |= 2;
31619 break;
31620 }
31621
31622 return flags;
31623}
31624
31625/* Add a new specialization described by SPEC. DECL is the
31626 maybe-template decl and FLAGS is as returned from
31627 get_mergeable_specialization_flags. */
31628
31629void
31630add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
31631 unsigned flags)
31632{
31633 hashval_t hash = spec_hasher::hash (e: elt);
31634 if (decl_p)
31635 {
31636 auto *slot = decl_specializations->find_slot_with_hash (comparable: elt, hash, insert: INSERT);
31637
31638 gcc_checking_assert (!*slot);
31639 auto entry = ggc_alloc<spec_entry> ();
31640 *entry = *elt;
31641 *slot = entry;
31642 }
31643 else
31644 {
31645 auto *slot = type_specializations->find_slot_with_hash (comparable: elt, hash, insert: INSERT);
31646
31647 /* We don't distinguish different constrained partial type
31648 specializations, so there could be duplicates. Everything else
31649 must be new. */
31650 if (!(flags & 2 && *slot))
31651 {
31652 gcc_checking_assert (!*slot);
31653
31654 auto entry = ggc_alloc<spec_entry> ();
31655 *entry = *elt;
31656 *slot = entry;
31657 }
31658 }
31659
31660 if (flags & 1)
31661 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
31662 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
31663
31664 if (flags & 2)
31665 {
31666 /* A partial specialization. */
31667 tree cons = tree_cons (elt->args, decl,
31668 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
31669 TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
31670 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
31671 }
31672}
31673
31674/* Set up the hash tables for template instantiations. */
31675
31676void
31677init_template_processing (void)
31678{
31679 decl_specializations = hash_table<spec_hasher>::create_ggc (n: 37);
31680 type_specializations = hash_table<spec_hasher>::create_ggc (n: 37);
31681
31682 if (cxx_dialect >= cxx11)
31683 declare_integer_pack ();
31684}
31685
31686/* Print stats about the template hash tables for -fstats. */
31687
31688void
31689print_template_statistics (void)
31690{
31691 fprintf (stderr, format: "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
31692 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
31693 (fmt_size_t) decl_specializations->size (),
31694 (fmt_size_t) decl_specializations->elements (),
31695 decl_specializations->collisions ());
31696 fprintf (stderr, format: "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
31697 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
31698 (fmt_size_t) type_specializations->size (),
31699 (fmt_size_t) type_specializations->elements (),
31700 type_specializations->collisions ());
31701}
31702
31703#if CHECKING_P
31704
31705namespace selftest {
31706
31707/* Verify that type_dependent_expression_p () works correctly, even
31708 in the presence of location wrapper nodes. */
31709
31710static void
31711test_type_dependent_expression_p ()
31712{
31713 location_t loc = BUILTINS_LOCATION;
31714
31715 tree name = get_identifier ("foo");
31716
31717 /* If no templates are involved, nothing is type-dependent. */
31718 gcc_assert (!processing_template_decl);
31719 ASSERT_FALSE (type_dependent_expression_p (name));
31720
31721 ++processing_template_decl;
31722
31723 /* Within a template, an unresolved name is always type-dependent. */
31724 ASSERT_TRUE (type_dependent_expression_p (name));
31725
31726 /* Ensure it copes with NULL_TREE and errors. */
31727 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
31728 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
31729
31730 /* A USING_DECL in a template should be type-dependent, even if wrapped
31731 with a location wrapper (PR c++/83799). */
31732 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
31733 TREE_TYPE (using_decl) = integer_type_node;
31734 ASSERT_TRUE (type_dependent_expression_p (using_decl));
31735 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
31736 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
31737 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
31738
31739 --processing_template_decl;
31740}
31741
31742/* Run all of the selftests within this file. */
31743
31744void
31745cp_pt_cc_tests ()
31746{
31747 test_type_dependent_expression_p ();
31748}
31749
31750} // namespace selftest
31751
31752#endif /* #if CHECKING_P */
31753
31754#include "gt-cp-pt.h"
31755

source code of gcc/cp/pt.cc