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