Debian Codegraph @

gcc/cp/pt.cc

raw
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2025 Free Software Foundation, Inc.
3 Written by Ken Raeburn ([email protected]) while at Watchmaker Computing.
4 Rewritten by Jason Merrill ([email protected]).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 #include "pretty-print-markup.h"
52
53 /* The type of functions taking a tree, and some additional data, and
54 returning an int. */
55 typedef int (*tree_fn_t) (tree, void*);
56
57 /* The PENDING_TEMPLATES is a list of templates whose instantiations
58 have been deferred, either because their definitions were not yet
59 available, or because we were putting off doing the work. */
60 struct GTY ((chain_next ("%h.next"))) pending_template
61 {
62 struct pending_template *next;
63 struct tinst_level *tinst;
64 };
65
66 static GTY(()) struct pending_template *pending_templates;
67 static GTY(()) struct pending_template *last_pending_template;
68
69 int processing_template_parmlist;
70 static int template_header_count;
71
72 static vec<int> inline_parm_levels;
73
74 static GTY(()) struct tinst_level *current_tinst_level;
75
76 static GTY(()) vec<tree, va_gc> *saved_access_scope;
77
78 /* Live only within one (recursive) call to tsubst_expr. We use
79 this to pass the statement expression node from the STMT_EXPR
80 to the EXPR_STMT that is its result. */
81 static tree cur_stmt_expr;
82
83 // -------------------------------------------------------------------------- //
84 // Local Specialization Stack
85 //
86 // Implementation of the RAII helper for creating new local
87 // specializations.
88 local_specialization_stack::local_specialization_stack (lss_policy policy)
89 : saved (local_specializations)
90 {
91 if (policy == lss_nop)
92 ;
93 else if (policy == lss_blank || !saved)
94 local_specializations = new hash_map<tree, tree>;
95 else
96 local_specializations = new hash_map<tree, tree>(*saved);
97 }
98
99 local_specialization_stack::~local_specialization_stack ()
100 {
101 if (local_specializations != saved)
102 {
103 delete local_specializations;
104 local_specializations = saved;
105 }
106 }
107
108 /* True if we've recursed into fn_type_unification too many times. */
109 static bool excessive_deduction_depth;
110
111 struct spec_hasher : ggc_ptr_hash<spec_entry>
112 {
113 static hashval_t hash (tree, tree);
114 static hashval_t hash (spec_entry *);
115 static bool equal (spec_entry *, spec_entry *);
116 };
117
118 /* The general template is not in these tables. */
119 typedef hash_table<spec_hasher> spec_hash_table;
120 static GTY (()) spec_hash_table *decl_specializations;
121 static GTY (()) spec_hash_table *type_specializations;
122
123 /* Contains canonical template parameter types. The vector is indexed by
124 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
125 TREE_LIST, whose TREE_VALUEs contain the canonical template
126 parameters of various types and levels. */
127 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
128
129 #define UNIFY_ALLOW_NONE 0
130 #define UNIFY_ALLOW_MORE_CV_QUAL 1
131 #define UNIFY_ALLOW_LESS_CV_QUAL 2
132 #define UNIFY_ALLOW_DERIVED 4
133 #define UNIFY_ALLOW_INTEGER 8
134 #define UNIFY_ALLOW_OUTER_LEVEL 16
135 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
136 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
137
138 enum template_base_result {
139 tbr_incomplete_type,
140 tbr_ambiguous_baseclass,
141 tbr_success
142 };
143
144 static bool resolve_overloaded_unification (tree, tree, tree, tree,
145 unification_kind_t, int,
146 bool);
147 static int try_one_overload (tree, tree, tree, tree, tree,
148 unification_kind_t, int, bool, bool);
149 static int unify (tree, tree, tree, tree, int, bool);
150 static void add_pending_template (tree);
151 static tree reopen_tinst_level (struct tinst_level *);
152 static tree tsubst_initializer_list (tree, tree);
153 static tree get_partial_spec_bindings (tree, tree, tree);
154 static void tsubst_enum (tree, tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
157 struct conversion **, bool, bool);
158 static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
159 tree*, tree*, tree);
160 static int type_unification_real (tree, tree, tree, const tree *,
161 unsigned int, int, unification_kind_t,
162 vec<deferred_access_check, va_gc> **,
163 bool);
164 static void note_template_header (int);
165 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
166 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
167 static tree convert_template_argument (tree, tree, tree,
168 tsubst_flags_t, int, tree);
169 static tree for_each_template_parm (tree, tree_fn_t, void*,
170 hash_set<tree> *, bool, tree_fn_t = NULL);
171 static tree expand_template_argument_pack (tree);
172 static tree build_template_parm_index (int, int, int, tree, tree);
173 static bool inline_needs_template_parms (tree, bool);
174 static void push_inline_template_parms_recursive (tree, int);
175 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
176 static int mark_template_parm (tree, void *);
177 static int template_parm_this_level_p (tree, void *);
178 static tree tsubst_friend_function (tree, tree);
179 static tree tsubst_friend_class (tree, tree);
180 static int can_complete_type_without_circularity (tree);
181 static tree get_bindings (tree, tree, tree, bool);
182 static int template_decl_level (tree);
183 static int check_cv_quals_for_unify (int, tree, tree);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
188 static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
189 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
191 static bool check_specialization_scope (void);
192 static tree process_partial_specialization (tree);
193 static enum template_base_result get_template_base (tree, tree, tree, tree,
194 bool , tree *);
195 static tree try_class_unification (tree, tree, tree, tree, bool);
196 static bool class_nttp_const_wrapper_p (tree t);
197 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
198 tree, tree);
199 static bool template_template_parm_bindings_ok_p (tree, tree);
200 static void tsubst_default_arguments (tree, tsubst_flags_t);
201 static tree for_each_template_parm_r (tree *, int *, void *);
202 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
203 static void copy_default_args_to_explicit_spec (tree);
204 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
205 static bool dependent_template_arg_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
209 static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
210 static tree tsubst_name (tree, tree, tsubst_flags_t, tree);
211 static void perform_instantiation_time_access_checks (tree, tree);
212 static tree listify (tree);
213 static tree listify_autos (tree, tree);
214 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
215 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
216 static tree get_underlying_template (tree);
217 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
218 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 static tree make_argument_pack (tree);
220 static tree enclosing_instantiation_of (tree tctx);
221 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
222 static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
223 static void mark_template_arguments_used (tree, tree);
224 static bool uses_outer_template_parms (tree);
225 static tree alias_ctad_tweaks (tree, tree);
226 static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
227 static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
228
229 /* Make the current scope suitable for access checking when we are
230 processing T. T can be FUNCTION_DECL for instantiated function
231 template, VAR_DECL for static member variable, or TYPE_DECL for
232 for a class or alias template (needed by instantiate_decl). */
233
234 void
235 push_access_scope (tree t)
236 {
237 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
238 || TREE_CODE (t) == TYPE_DECL);
239
240 if (DECL_FRIEND_CONTEXT (t))
241 push_nested_class (DECL_FRIEND_CONTEXT (t));
242 else if (DECL_IMPLICIT_TYPEDEF_P (t)
243 && CLASS_TYPE_P (TREE_TYPE (t)))
244 push_nested_class (TREE_TYPE (t));
245 else if (DECL_CLASS_SCOPE_P (t))
246 push_nested_class (DECL_CONTEXT (t));
247 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
248 /* An artificial deduction guide should have the same access as
249 the constructor. */
250 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
251 else
252 push_to_top_level ();
253
254 if (TREE_CODE (t) == FUNCTION_DECL)
255 {
256 vec_safe_push (saved_access_scope, current_function_decl);
257 current_function_decl = t;
258 }
259 }
260
261 /* Restore the scope set up by push_access_scope. T is the node we
262 are processing. */
263
264 void
265 pop_access_scope (tree t)
266 {
267 if (TREE_CODE (t) == FUNCTION_DECL)
268 current_function_decl = saved_access_scope->pop();
269
270 if (DECL_FRIEND_CONTEXT (t)
271 || (DECL_IMPLICIT_TYPEDEF_P (t)
272 && CLASS_TYPE_P (TREE_TYPE (t)))
273 || DECL_CLASS_SCOPE_P (t)
274 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
275 pop_nested_class ();
276 else
277 pop_from_top_level ();
278 }
279
280 /* Do any processing required when DECL (a member template
281 declaration) is finished. Returns the TEMPLATE_DECL corresponding
282 to DECL, unless it is a specialization, in which case the DECL
283 itself is returned. */
284
285 tree
286 finish_member_template_decl (tree decl)
287 {
288 if (decl == error_mark_node)
289 return error_mark_node;
290
291 gcc_assert (DECL_P (decl));
292
293 if (TREE_CODE (decl) == TYPE_DECL)
294 {
295 tree type;
296
297 type = TREE_TYPE (decl);
298 if (type == error_mark_node)
299 return error_mark_node;
300 if (MAYBE_CLASS_TYPE_P (type)
301 && CLASSTYPE_TEMPLATE_INFO (type)
302 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
303 {
304 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
305 check_member_template (tmpl);
306 return tmpl;
307 }
308 return NULL_TREE;
309 }
310 else if (TREE_CODE (decl) == FIELD_DECL)
311 error_at (DECL_SOURCE_LOCATION (decl),
312 "data member %qD cannot be a member template", decl);
313 else if (DECL_TEMPLATE_INFO (decl))
314 {
315 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
316 {
317 check_member_template (DECL_TI_TEMPLATE (decl));
318 return DECL_TI_TEMPLATE (decl);
319 }
320 else
321 return NULL_TREE;
322 }
323 else
324 error_at (DECL_SOURCE_LOCATION (decl),
325 "invalid member template declaration %qD", decl);
326
327 return error_mark_node;
328 }
329
330 /* Create a template info node. */
331
332 tree
333 build_template_info (tree template_decl, tree template_args)
334 {
335 tree result = make_node (TEMPLATE_INFO);
336 TI_TEMPLATE (result) = template_decl;
337 TI_ARGS (result) = template_args;
338 return result;
339 }
340
341 /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */
342
343 static tree
344 decl_template_info (const_tree decl)
345 {
346 /* This needs to match template_info_decl_check. */
347 if (DECL_LANG_SPECIFIC (decl))
348 switch (TREE_CODE (decl))
349 {
350 case FUNCTION_DECL:
351 if (DECL_THUNK_P (decl))
352 break;
353 gcc_fallthrough ();
354 case VAR_DECL:
355 case FIELD_DECL:
356 case TYPE_DECL:
357 case CONCEPT_DECL:
358 case TEMPLATE_DECL:
359 return DECL_TEMPLATE_INFO (decl);
360
361 default:
362 break;
363 }
364 return NULL_TREE;
365 }
366
367 /* Return the template info node corresponding to T, whatever T is. */
368
369 tree
370 get_template_info (const_tree t)
371 {
372 tree tinfo = NULL_TREE;
373
374 if (!t || t == error_mark_node)
375 return NULL;
376
377 if (TREE_CODE (t) == NAMESPACE_DECL
378 || TREE_CODE (t) == PARM_DECL)
379 return NULL;
380
381 if (DECL_P (t))
382 tinfo = decl_template_info (t);
383
384 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
385 t = TREE_TYPE (t);
386
387 if (OVERLOAD_TYPE_P (t))
388 tinfo = TYPE_TEMPLATE_INFO (t);
389 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
390 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
391
392 return tinfo;
393 }
394
395 /* Returns the template nesting level of the indicated class TYPE.
396
397 For example, in:
398 template <class T>
399 struct A
400 {
401 template <class U>
402 struct B {};
403 };
404
405 A<T>::B<U> has depth two, while A<T> has depth one.
406 Both A<T>::B<int> and A<int>::B<U> have depth one, if
407 they are instantiations, not specializations.
408
409 This function is guaranteed to return 0 if passed NULL_TREE so
410 that, for example, `template_class_depth (current_class_type)' is
411 always safe. */
412
413 int
414 template_class_depth (tree type)
415 {
416 int depth;
417
418 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
419 {
420 tree tinfo = get_template_info (type);
421
422 if (tinfo
423 && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
424 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
425 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
426 ++depth;
427
428 if (DECL_P (type))
429 {
430 if (tree fctx = DECL_FRIEND_CONTEXT (type))
431 type = fctx;
432 else
433 type = CP_DECL_CONTEXT (type);
434 }
435 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
436 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
437 else
438 type = CP_TYPE_CONTEXT (type);
439 }
440
441 return depth;
442 }
443
444 /* Return TRUE if NODE instantiates a template that has arguments of
445 its own, be it directly a primary template or indirectly through a
446 partial specializations. */
447 static bool
448 instantiates_primary_template_p (tree node)
449 {
450 tree tinfo = get_template_info (node);
451 if (!tinfo)
452 return false;
453
454 tree tmpl = TI_TEMPLATE (tinfo);
455 if (PRIMARY_TEMPLATE_P (tmpl))
456 return true;
457
458 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
459 return false;
460
461 /* So now we know we have a specialization, but it could be a full
462 or a partial specialization. To tell which, compare the depth of
463 its template arguments with those of its context. */
464
465 tree ctxt = DECL_CONTEXT (tmpl);
466 tree ctinfo = get_template_info (ctxt);
467 if (!ctinfo)
468 return true;
469
470 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
471 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
472 }
473
474 /* Subroutine of maybe_begin_member_template_processing.
475 Returns true if processing DECL needs us to push template parms. */
476
477 static bool
478 inline_needs_template_parms (tree decl, bool nsdmi)
479 {
480 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
481 return false;
482
483 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
484 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
485 }
486
487 /* Subroutine of maybe_begin_member_template_processing.
488 Push the template parms in PARMS, starting from LEVELS steps into the
489 chain, and ending at the beginning, since template parms are listed
490 innermost first. */
491
492 static void
493 push_inline_template_parms_recursive (tree parmlist, int levels)
494 {
495 tree parms = TREE_VALUE (parmlist);
496 int i;
497
498 if (levels > 1)
499 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
500
501 ++processing_template_decl;
502 current_template_parms
503 = tree_cons (size_int (current_template_depth + 1),
504 parms, current_template_parms);
505 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
506 = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
507 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
508
509 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
510 NULL);
511 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
512 {
513 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
514
515 if (error_operand_p (parm))
516 continue;
517
518 gcc_assert (DECL_P (parm));
519
520 switch (TREE_CODE (parm))
521 {
522 case TYPE_DECL:
523 case TEMPLATE_DECL:
524 pushdecl (parm);
525 break;
526
527 case PARM_DECL:
528 /* Push the CONST_DECL. */
529 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
530 break;
531
532 default:
533 gcc_unreachable ();
534 }
535 }
536 }
537
538 /* Restore the template parameter context for a member template, a
539 friend template defined in a class definition, or a non-template
540 member of template class. */
541
542 void
543 maybe_begin_member_template_processing (tree decl)
544 {
545 tree parms;
546 int levels = 0;
547 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
548
549 if (nsdmi)
550 {
551 tree ctx = DECL_CONTEXT (decl);
552 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
553 /* Disregard full specializations (c++/60999). */
554 && uses_template_parms (ctx)
555 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
556 }
557
558 if (inline_needs_template_parms (decl, nsdmi))
559 {
560 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
561 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
562
563 if (DECL_TEMPLATE_SPECIALIZATION (decl))
564 {
565 --levels;
566 parms = TREE_CHAIN (parms);
567 }
568
569 push_inline_template_parms_recursive (parms, levels);
570 }
571
572 /* Remember how many levels of template parameters we pushed so that
573 we can pop them later. */
574 inline_parm_levels.safe_push (levels);
575 }
576
577 /* Undo the effects of maybe_begin_member_template_processing. */
578
579 void
580 maybe_end_member_template_processing (void)
581 {
582 int i;
583 int last;
584
585 if (inline_parm_levels.length () == 0)
586 return;
587
588 last = inline_parm_levels.pop ();
589 for (i = 0; i < last; ++i)
590 {
591 --processing_template_decl;
592 current_template_parms = TREE_CHAIN (current_template_parms);
593 poplevel (0, 0, 0);
594 }
595 }
596
597 /* Return a new template argument vector which contains all of ARGS,
598 but has as its innermost set of arguments the EXTRA_ARGS. */
599
600 tree
601 add_to_template_args (tree args, tree extra_args)
602 {
603 tree new_args;
604 int extra_depth;
605 int i;
606 int j;
607
608 if (args == NULL_TREE || extra_args == error_mark_node)
609 return extra_args;
610
611 extra_depth = TMPL_ARGS_DEPTH (extra_args);
612 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
613
614 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
615 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
616
617 for (j = 1; j <= extra_depth; ++j, ++i)
618 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
619
620 return new_args;
621 }
622
623 /* Like add_to_template_args, but only the outermost ARGS are added to
624 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
625 (EXTRA_ARGS) levels are added. This function is used to combine
626 the template arguments from a partial instantiation with the
627 template arguments used to attain the full instantiation from the
628 partial instantiation.
629
630 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
631
632 tree
633 add_outermost_template_args (tree args, tree extra_args)
634 {
635 tree new_args;
636
637 if (!args)
638 return extra_args;
639 if (TREE_CODE (args) == TEMPLATE_DECL)
640 {
641 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
642 args = TI_ARGS (ti);
643 }
644
645 /* If there are more levels of EXTRA_ARGS than there are ARGS,
646 something very fishy is going on. */
647 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
648
649 /* If *all* the new arguments will be the EXTRA_ARGS, just return
650 them. */
651 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
652 return extra_args;
653
654 /* For the moment, we make ARGS look like it contains fewer levels. */
655 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
656
657 new_args = add_to_template_args (args, extra_args);
658
659 /* Now, we restore ARGS to its full dimensions. */
660 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
661
662 return new_args;
663 }
664
665 /* Return the N levels of innermost template arguments from the ARGS. */
666
667 tree
668 get_innermost_template_args (tree args, int n)
669 {
670 tree new_args;
671 int extra_levels;
672 int i;
673
674 gcc_assert (n >= 0);
675
676 /* If N is 1, just return the innermost set of template arguments. */
677 if (n == 1)
678 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
679
680 /* If we're not removing anything, just return the arguments we were
681 given. */
682 extra_levels = TMPL_ARGS_DEPTH (args) - n;
683 gcc_assert (extra_levels >= 0);
684 if (extra_levels == 0)
685 return args;
686
687 /* Make a new set of arguments, not containing the outer arguments. */
688 new_args = make_tree_vec (n);
689 for (i = 1; i <= n; ++i)
690 SET_TMPL_ARGS_LEVEL (new_args, i,
691 TMPL_ARGS_LEVEL (args, i + extra_levels));
692
693 return new_args;
694 }
695
696 /* The inverse of get_innermost_template_args: Return all but the innermost
697 EXTRA_LEVELS levels of template arguments from the ARGS. */
698
699 static tree
700 strip_innermost_template_args (tree args, int extra_levels)
701 {
702 tree new_args;
703 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
704 int i;
705
706 gcc_assert (n >= 0);
707
708 /* If N is 1, just return the outermost set of template arguments. */
709 if (n == 1)
710 return TMPL_ARGS_LEVEL (args, 1);
711
712 /* If we're not removing anything, just return the arguments we were
713 given. */
714 gcc_assert (extra_levels >= 0);
715 if (extra_levels == 0)
716 return args;
717
718 /* Make a new set of arguments, not containing the inner arguments. */
719 new_args = make_tree_vec (n);
720 for (i = 1; i <= n; ++i)
721 SET_TMPL_ARGS_LEVEL (new_args, i,
722 TMPL_ARGS_LEVEL (args, i));
723
724 return new_args;
725 }
726
727 /* We've got a template header coming up; push to a new level for storing
728 the parms. */
729
730 void
731 begin_template_parm_list (void)
732 {
733 /* We use a non-tag-transparent scope here, which causes pushtag to
734 put tags in this scope, rather than in the enclosing class or
735 namespace scope. This is the right thing, since we want
736 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
737 global template class, push_template_decl handles putting the
738 TEMPLATE_DECL into top-level scope. For a nested template class,
739 e.g.:
740
741 template <class T> struct S1 {
742 template <class T> struct S2 {};
743 };
744
745 pushtag contains special code to insert the TEMPLATE_DECL for S2
746 at the right scope. */
747 begin_scope (sk_template_parms, NULL);
748 ++processing_template_decl;
749 ++processing_template_parmlist;
750 note_template_header (0);
751
752 /* Add a dummy parameter level while we process the parameter list. */
753 current_template_parms
754 = tree_cons (size_int (current_template_depth + 1),
755 make_tree_vec (0),
756 current_template_parms);
757 }
758
759 /* This routine is called when a specialization is declared. If it is
760 invalid to declare a specialization here, an error is reported and
761 false is returned, otherwise this routine will return true. */
762
763 static bool
764 check_specialization_scope (void)
765 {
766 tree scope = current_scope ();
767
768 /* [temp.expl.spec]
769
770 An explicit specialization shall be declared in the namespace of
771 which the template is a member, or, for member templates, in the
772 namespace of which the enclosing class or enclosing class
773 template is a member. An explicit specialization of a member
774 function, member class or static data member of a class template
775 shall be declared in the namespace of which the class template
776 is a member. */
777 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
778 {
779 error ("explicit specialization in non-namespace scope %qD", scope);
780 return false;
781 }
782
783 /* [temp.expl.spec]
784
785 In an explicit specialization declaration for a member of a class
786 template or a member template that appears in namespace scope,
787 the member template and some of its enclosing class templates may
788 remain unspecialized, except that the declaration shall not
789 explicitly specialize a class member template if its enclosing
790 class templates are not explicitly specialized as well. */
791 if (current_template_parms)
792 {
793 error ("enclosing class templates are not explicitly specialized");
794 return false;
795 }
796
797 return true;
798 }
799
800 /* We've just seen template <>. */
801
802 bool
803 begin_specialization (void)
804 {
805 begin_scope (sk_template_spec, NULL);
806 note_template_header (1);
807 return check_specialization_scope ();
808 }
809
810 /* Called at then end of processing a declaration preceded by
811 template<>. */
812
813 void
814 end_specialization (void)
815 {
816 finish_scope ();
817 reset_specialization ();
818 }
819
820 /* Any template <>'s that we have seen thus far are not referring to a
821 function specialization. */
822
823 void
824 reset_specialization (void)
825 {
826 processing_specialization = 0;
827 template_header_count = 0;
828 }
829
830 /* We've just seen a template header. If SPECIALIZATION is nonzero,
831 it was of the form template <>. */
832
833 static void
834 note_template_header (int specialization)
835 {
836 processing_specialization = specialization;
837 template_header_count++;
838 }
839
840 /* We're beginning an explicit instantiation. */
841
842 void
843 begin_explicit_instantiation (void)
844 {
845 gcc_assert (!processing_explicit_instantiation);
846 processing_explicit_instantiation = true;
847 }
848
849
850 void
851 end_explicit_instantiation (void)
852 {
853 gcc_assert (processing_explicit_instantiation);
854 processing_explicit_instantiation = false;
855 }
856
857 /* An explicit specialization or partial specialization of TMPL is being
858 declared. Check that the namespace in which the specialization is
859 occurring is permissible. Returns false iff it is invalid to
860 specialize TMPL in the current namespace. */
861
862 static bool
863 check_specialization_namespace (tree tmpl)
864 {
865 tree tpl_ns = decl_namespace_context (tmpl);
866
867 /* [tmpl.expl.spec]
868
869 An explicit specialization shall be declared in a namespace enclosing the
870 specialized template. An explicit specialization whose declarator-id is
871 not qualified shall be declared in the nearest enclosing namespace of the
872 template, or, if the namespace is inline (7.3.1), any namespace from its
873 enclosing namespace set. */
874 if (current_scope() != DECL_CONTEXT (tmpl)
875 && !at_namespace_scope_p ())
876 {
877 error ("specialization of %qD must appear at namespace scope", tmpl);
878 return false;
879 }
880
881 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
882 /* Same or enclosing namespace. */
883 return true;
884 else
885 {
886 auto_diagnostic_group d;
887 if (permerror (input_location,
888 "specialization of %qD in different namespace", tmpl))
889 inform (DECL_SOURCE_LOCATION (tmpl),
890 " from definition of %q#D", tmpl);
891 return false;
892 }
893 }
894
895 /* SPEC is an explicit instantiation. Check that it is valid to
896 perform this explicit instantiation in the current namespace. */
897
898 static void
899 check_explicit_instantiation_namespace (tree spec)
900 {
901 tree ns;
902
903 /* DR 275: An explicit instantiation shall appear in an enclosing
904 namespace of its template. */
905 ns = decl_namespace_context (spec);
906 if (!is_nested_namespace (current_namespace, ns))
907 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
908 "(which does not enclose namespace %qD)",
909 spec, current_namespace, ns);
910 }
911
912 /* Returns true if TYPE is a new partial specialization that needs to be
913 set up. This may also modify TYPE to point to the correct (new or
914 existing) constrained partial specialization. */
915
916 static bool
917 maybe_new_partial_specialization (tree& type)
918 {
919 /* An implicit instantiation of an incomplete type implies
920 the definition of a new class template.
921
922 template<typename T>
923 struct S;
924
925 template<typename T>
926 struct S<T*>;
927
928 Here, S<T*> is an implicit instantiation of S whose type
929 is incomplete. */
930 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
931 return true;
932
933 /* It can also be the case that TYPE is a completed specialization.
934 Continuing the previous example, suppose we also declare:
935
936 template<typename T>
937 requires Integral<T>
938 struct S<T*>;
939
940 Here, S<T*> refers to the specialization S<T*> defined
941 above. However, we need to differentiate definitions because
942 we intend to define a new partial specialization. In this case,
943 we rely on the fact that the constraints are different for
944 this declaration than that above.
945
946 Note that we also get here for injected class names and
947 late-parsed template definitions. We must ensure that we
948 do not create new type declarations for those cases. */
949 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
950 {
951 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
952 tree args = CLASSTYPE_TI_ARGS (type);
953
954 /* If there are no template parameters, this cannot be a new
955 partial template specialization? */
956 if (!current_template_parms)
957 return false;
958
959 /* The injected-class-name is not a new partial specialization. */
960 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
961 return false;
962
963 /* If the constraints are not the same as those of the primary
964 then, we can probably create a new specialization. */
965 tree type_constr = current_template_constraints ();
966
967 if (type == TREE_TYPE (tmpl))
968 {
969 tree main_constr = get_constraints (tmpl);
970 if (equivalent_constraints (type_constr, main_constr))
971 return false;
972 }
973
974 /* Also, if there's a pre-existing specialization with matching
975 constraints, then this also isn't new. */
976 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
977 while (specs)
978 {
979 tree spec_tmpl = TREE_VALUE (specs);
980 tree spec_args = TREE_PURPOSE (specs);
981 tree spec_constr = get_constraints (spec_tmpl);
982 if (comp_template_args (args, spec_args)
983 && equivalent_constraints (type_constr, spec_constr))
984 {
985 type = TREE_TYPE (spec_tmpl);
986 return false;
987 }
988 specs = TREE_CHAIN (specs);
989 }
990
991 /* Create a new type node (and corresponding type decl)
992 for the newly declared specialization. */
993 tree t = make_class_type (TREE_CODE (type));
994 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
995 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
996 TYPE_CONTEXT (t) = TYPE_CONTEXT (type);
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
1022 tree
1023 maybe_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 auto_diagnostic_group d;
1129 if (permerror (input_location,
1130 "specialization of %qD in different namespace",
1131 type))
1132 inform (DECL_SOURCE_LOCATION (tmpl),
1133 "from definition of %q#D", tmpl);
1134 }
1135
1136 /* Check for invalid specialization after instantiation:
1137
1138 template <> template <> class C<int>::D<int>;
1139 template <> template <class U> class C<int>::D; */
1140
1141 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1142 t; t = TREE_CHAIN (t))
1143 {
1144 tree inst = TREE_VALUE (t);
1145 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1146 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1147 {
1148 /* We already have a full specialization of this partial
1149 instantiation, or a full specialization has been
1150 looked up but not instantiated. Reassign it to the
1151 new member specialization template. */
1152 spec_entry elt;
1153 spec_entry *entry;
1154
1155 elt.tmpl = most_general_template (tmpl);
1156 elt.args = CLASSTYPE_TI_ARGS (inst);
1157 elt.spec = inst;
1158
1159 type_specializations->remove_elt (&elt);
1160
1161 elt.tmpl = tmpl;
1162 CLASSTYPE_TI_ARGS (inst)
1163 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1164 elt.hash = 0; /* Recalculate after changing tmpl/args. */
1165
1166 spec_entry **slot
1167 = type_specializations->find_slot (&elt, INSERT);
1168 entry = ggc_alloc<spec_entry> ();
1169 *entry = elt;
1170 *slot = entry;
1171 }
1172 else
1173 /* But if we've had an implicit instantiation, that's a
1174 problem ([temp.expl.spec]/6). */
1175 error ("specialization %qT after instantiation %qT",
1176 type, inst);
1177 }
1178
1179 /* Make sure that the specialization is valid. */
1180 if (!redeclare_class_template (type, current_template_parms,
1181 current_template_constraints ()))
1182 return error_mark_node;
1183
1184 /* Mark TYPE as a specialization. And as a result, we only
1185 have one level of template argument for the innermost
1186 class template. */
1187 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1188 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1189 CLASSTYPE_TI_ARGS (type)
1190 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1191 }
1192 }
1193 else if (processing_specialization)
1194 {
1195 /* Someday C++0x may allow for enum template specialization. */
1196 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1197 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1198 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1199 "of %qD not allowed by ISO C++", type);
1200 else
1201 {
1202 error ("explicit specialization of non-template %qT", type);
1203 return error_mark_node;
1204 }
1205 }
1206
1207 return type;
1208 }
1209
1210 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1211 gone through coerce_template_parms by now. */
1212
1213 static void
1214 verify_unstripped_args_1 (tree inner)
1215 {
1216 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1217 {
1218 tree arg = TREE_VEC_ELT (inner, i);
1219 if (TREE_CODE (arg) == TEMPLATE_DECL)
1220 /* OK */;
1221 else if (TYPE_P (arg))
1222 gcc_assert (strip_typedefs (arg, NULL) == arg);
1223 else if (ARGUMENT_PACK_P (arg))
1224 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1225 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1226 /* Allow typedefs on the type of a non-type argument, since a
1227 parameter can have them. */;
1228 else
1229 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1230 }
1231 }
1232
1233 static void
1234 verify_unstripped_args (tree args)
1235 {
1236 ++processing_template_decl;
1237 if (!any_dependent_template_arguments_p (args))
1238 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1239 --processing_template_decl;
1240 }
1241
1242 /* Retrieve the specialization (in the sense of [temp.spec] - a
1243 specialization is either an instantiation or an explicit
1244 specialization) of TMPL for the given template ARGS. If there is
1245 no such specialization, return NULL_TREE. The ARGS are a vector of
1246 arguments, or a vector of vectors of arguments, in the case of
1247 templates with more than one level of parameters.
1248
1249 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1250 then we search for a partial specialization matching ARGS. This
1251 parameter is ignored if TMPL is not a class template.
1252
1253 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1254 result is a NONTYPE_ARGUMENT_PACK. */
1255
1256 static tree
1257 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1258 {
1259 if (tmpl == NULL_TREE)
1260 return NULL_TREE;
1261
1262 if (args == error_mark_node)
1263 return NULL_TREE;
1264
1265 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1266 || TREE_CODE (tmpl) == FIELD_DECL);
1267
1268 /* There should be as many levels of arguments as there are
1269 levels of parameters. */
1270 gcc_assert (TMPL_ARGS_DEPTH (args)
1271 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1272 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1273 : template_class_depth (DECL_CONTEXT (tmpl))));
1274
1275 if (flag_checking)
1276 verify_unstripped_args (args);
1277
1278 /* Lambda functions in templates aren't instantiated normally, but through
1279 tsubst_lambda_expr. */
1280 if (lambda_fn_in_template_p (tmpl))
1281 return NULL_TREE;
1282
1283 spec_entry elt;
1284 elt.tmpl = tmpl;
1285 elt.args = args;
1286 elt.hash = hash;
1287
1288 spec_hash_table *specializations;
1289 if (DECL_CLASS_TEMPLATE_P (tmpl))
1290 specializations = type_specializations;
1291 else
1292 specializations = decl_specializations;
1293
1294 if (spec_entry *found = specializations->find (&elt))
1295 return found->spec;
1296
1297 return NULL_TREE;
1298 }
1299
1300 /* Like retrieve_specialization, but for local declarations. */
1301
1302 tree
1303 retrieve_local_specialization (tree tmpl)
1304 {
1305 if (local_specializations == NULL)
1306 return NULL_TREE;
1307
1308 tree *slot = local_specializations->get (tmpl);
1309 return slot ? *slot : NULL_TREE;
1310 }
1311
1312 /* Returns nonzero iff DECL is a specialization of TMPL. */
1313
1314 int
1315 is_specialization_of (tree decl, tree tmpl)
1316 {
1317 tree t;
1318
1319 if (TREE_CODE (decl) == FUNCTION_DECL)
1320 {
1321 for (t = decl;
1322 t != NULL_TREE;
1323 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1324 if (t == tmpl)
1325 return 1;
1326 }
1327 else
1328 {
1329 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1330
1331 for (t = TREE_TYPE (decl);
1332 t != NULL_TREE;
1333 t = CLASSTYPE_USE_TEMPLATE (t)
1334 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1335 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1336 return 1;
1337 }
1338
1339 return 0;
1340 }
1341
1342 /* Returns nonzero iff DECL is a specialization of friend declaration
1343 FRIEND_DECL according to [temp.friend]. */
1344
1345 bool
1346 is_specialization_of_friend (tree decl, tree friend_decl)
1347 {
1348 bool need_template = true;
1349 int template_depth;
1350
1351 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1352 || TREE_CODE (decl) == TYPE_DECL);
1353
1354 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1355 of a template class, we want to check if DECL is a specialization
1356 if this. */
1357 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1358 && DECL_CLASS_SCOPE_P (friend_decl)
1359 && DECL_TEMPLATE_INFO (friend_decl)
1360 && !DECL_USE_TEMPLATE (friend_decl))
1361 {
1362 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1363 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1364 need_template = false;
1365 }
1366 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1367 && !PRIMARY_TEMPLATE_P (friend_decl))
1368 need_template = false;
1369
1370 /* There is nothing to do if this is not a template friend. */
1371 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1372 return false;
1373
1374 if (is_specialization_of (decl, friend_decl))
1375 return true;
1376
1377 /* [temp.friend/6]
1378 A member of a class template may be declared to be a friend of a
1379 non-template class. In this case, the corresponding member of
1380 every specialization of the class template is a friend of the
1381 class granting friendship.
1382
1383 For example, given a template friend declaration
1384
1385 template <class T> friend void A<T>::f();
1386
1387 the member function below is considered a friend
1388
1389 template <> struct A<int> {
1390 void f();
1391 };
1392
1393 For this type of template friend, TEMPLATE_DEPTH below will be
1394 nonzero. To determine if DECL is a friend of FRIEND, we first
1395 check if the enclosing class is a specialization of another. */
1396
1397 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1398 if (template_depth
1399 && DECL_CLASS_SCOPE_P (decl)
1400 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1401 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1402 {
1403 /* Next, we check the members themselves. In order to handle
1404 a few tricky cases, such as when FRIEND_DECL's are
1405
1406 template <class T> friend void A<T>::g(T t);
1407 template <class T> template <T t> friend void A<T>::h();
1408
1409 and DECL's are
1410
1411 void A<int>::g(int);
1412 template <int> void A<int>::h();
1413
1414 we need to figure out ARGS, the template arguments from
1415 the context of DECL. This is required for template substitution
1416 of `T' in the function parameter of `g' and template parameter
1417 of `h' in the above examples. Here ARGS corresponds to `int'. */
1418
1419 tree context = DECL_CONTEXT (decl);
1420 tree args = NULL_TREE;
1421 int current_depth = 0;
1422
1423 while (current_depth < template_depth)
1424 {
1425 if (CLASSTYPE_TEMPLATE_INFO (context))
1426 {
1427 if (current_depth == 0)
1428 args = TYPE_TI_ARGS (context);
1429 else
1430 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1431 current_depth++;
1432 }
1433 context = TYPE_CONTEXT (context);
1434 }
1435
1436 if (TREE_CODE (decl) == FUNCTION_DECL)
1437 {
1438 bool is_template;
1439 tree friend_type;
1440 tree decl_type;
1441 tree friend_args_type;
1442 tree decl_args_type;
1443
1444 /* Make sure that both DECL and FRIEND_DECL are templates or
1445 non-templates. */
1446 is_template = DECL_TEMPLATE_INFO (decl)
1447 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1448 if (need_template ^ is_template)
1449 return false;
1450 else if (is_template)
1451 {
1452 /* If both are templates, check template parameter list. */
1453 tree friend_parms
1454 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1455 args, tf_none);
1456 if (!comp_template_parms
1457 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1458 friend_parms))
1459 return false;
1460
1461 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1462 }
1463 else
1464 decl_type = TREE_TYPE (decl);
1465
1466 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1467 tf_none, NULL_TREE);
1468 if (friend_type == error_mark_node)
1469 return false;
1470
1471 /* Check if return types match. */
1472 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1473 return false;
1474
1475 /* Check if function parameter types match, ignoring the
1476 `this' parameter. */
1477 friend_args_type = TYPE_ARG_TYPES (friend_type);
1478 decl_args_type = TYPE_ARG_TYPES (decl_type);
1479 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
1480 friend_args_type = TREE_CHAIN (friend_args_type);
1481 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
1482 decl_args_type = TREE_CHAIN (decl_args_type);
1483
1484 return compparms (decl_args_type, friend_args_type);
1485 }
1486 else
1487 {
1488 /* DECL is a TYPE_DECL */
1489 bool is_template;
1490 tree decl_type = TREE_TYPE (decl);
1491
1492 /* Make sure that both DECL and FRIEND_DECL are templates or
1493 non-templates. */
1494 is_template
1495 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1496 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1497
1498 if (need_template ^ is_template)
1499 return false;
1500 else if (is_template)
1501 {
1502 tree friend_parms;
1503 /* If both are templates, check the name of the two
1504 TEMPLATE_DECL's first because is_friend didn't. */
1505 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1506 != DECL_NAME (friend_decl))
1507 return false;
1508
1509 /* Now check template parameter list. */
1510 friend_parms
1511 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1512 args, tf_none);
1513 return comp_template_parms
1514 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1515 friend_parms);
1516 }
1517 else
1518 return (DECL_NAME (decl)
1519 == DECL_NAME (friend_decl));
1520 }
1521 }
1522 return false;
1523 }
1524
1525 /* Register the specialization SPEC as a specialization of TMPL with
1526 the indicated ARGS. IS_FRIEND indicates whether the specialization
1527 is actually just a friend declaration. ATTRLIST is the list of
1528 attributes that the specialization is declared with or NULL when
1529 it isn't. Returns SPEC, or an equivalent prior declaration, if
1530 available.
1531
1532 We also store instantiations of field packs in the hash table, even
1533 though they are not themselves templates, to make lookup easier. */
1534
1535 static tree
1536 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1537 hashval_t hash)
1538 {
1539 tree fn;
1540
1541 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1542 || (TREE_CODE (tmpl) == FIELD_DECL
1543 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1544
1545 spec_entry elt;
1546 elt.tmpl = tmpl;
1547 elt.args = args;
1548 elt.spec = spec;
1549 elt.hash = hash;
1550
1551 spec_entry **slot = decl_specializations->find_slot (&elt, INSERT);
1552 if (*slot)
1553 fn = (*slot)->spec;
1554 else
1555 fn = NULL_TREE;
1556
1557 /* We can sometimes try to re-register a specialization that we've
1558 already got. In particular, regenerate_decl_from_template calls
1559 duplicate_decls which will update the specialization list. But,
1560 we'll still get called again here anyhow. It's more convenient
1561 to simply allow this than to try to prevent it. */
1562 if (fn == spec)
1563 return spec;
1564 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1565 {
1566 if (DECL_TEMPLATE_INSTANTIATION (fn))
1567 {
1568 if (DECL_ODR_USED (fn)
1569 || DECL_EXPLICIT_INSTANTIATION (fn))
1570 {
1571 error ("specialization of %qD after instantiation",
1572 fn);
1573 return error_mark_node;
1574 }
1575 else
1576 {
1577 tree clone;
1578 /* This situation should occur only if the first
1579 specialization is an implicit instantiation, the
1580 second is an explicit specialization, and the
1581 implicit instantiation has not yet been used. That
1582 situation can occur if we have implicitly
1583 instantiated a member function and then specialized
1584 it later.
1585
1586 We can also wind up here if a friend declaration that
1587 looked like an instantiation turns out to be a
1588 specialization:
1589
1590 template <class T> void foo(T);
1591 class S { friend void foo<>(int) };
1592 template <> void foo(int);
1593
1594 We transform the existing DECL in place so that any
1595 pointers to it become pointers to the updated
1596 declaration.
1597
1598 If there was a definition for the template, but not
1599 for the specialization, we want this to look as if
1600 there were no definition, and vice versa. */
1601 DECL_INITIAL (fn) = NULL_TREE;
1602 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1603
1604 /* The call to duplicate_decls will have applied
1605 [temp.expl.spec]:
1606
1607 An explicit specialization of a function template
1608 is inline only if it is explicitly declared to be,
1609 and independently of whether its function template
1610 is.
1611
1612 to the primary function; now copy the inline bits to
1613 the various clones. */
1614 FOR_EACH_CLONE (clone, fn)
1615 {
1616 DECL_DECLARED_INLINE_P (clone)
1617 = DECL_DECLARED_INLINE_P (fn);
1618 DECL_SOURCE_LOCATION (clone)
1619 = DECL_SOURCE_LOCATION (fn);
1620 DECL_DELETED_FN (clone)
1621 = DECL_DELETED_FN (fn);
1622 }
1623 check_specialization_namespace (tmpl);
1624
1625 return fn;
1626 }
1627 }
1628 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1629 {
1630 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1631 if (dd == error_mark_node)
1632 /* We've already complained in duplicate_decls. */
1633 return error_mark_node;
1634
1635 if (dd == NULL_TREE && DECL_INITIAL (spec))
1636 /* Dup decl failed, but this is a new definition. Set the
1637 line number so any errors match this new
1638 definition. */
1639 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1640
1641 return fn;
1642 }
1643 }
1644 else if (fn)
1645 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1646
1647 /* A specialization must be declared in the same namespace as the
1648 template it is specializing. */
1649 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1650 && !check_specialization_namespace (tmpl))
1651 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1652
1653 spec_entry *entry = ggc_alloc<spec_entry> ();
1654 gcc_assert (tmpl && args && spec);
1655 *entry = elt;
1656 *slot = entry;
1657 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1658 && PRIMARY_TEMPLATE_P (tmpl)
1659 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1660 || module_maybe_has_cmi_p ()
1661 || variable_template_p (tmpl))
1662 /* If TMPL is a forward declaration of a template function, keep a list
1663 of all specializations in case we need to reassign them to a friend
1664 template later in tsubst_friend_function.
1665
1666 If we're building a CMI, keep a list for all function templates.
1667
1668 Also keep a list of all variable template instantiations so that
1669 process_partial_specialization can check whether a later partial
1670 specialization would have used it. */
1671 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1672 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1673
1674 return spec;
1675 }
1676
1677 /* Restricts tree and type comparisons. */
1678 int comparing_specializations;
1679 int comparing_dependent_aliases;
1680
1681 /* Whether we are comparing template arguments during partial ordering
1682 (and therefore want the comparison to look through dependent alias
1683 template specializations). */
1684
1685 static int comparing_for_partial_ordering;
1686
1687 /* Returns true iff two spec_entry nodes are equivalent. */
1688
1689 bool
1690 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1691 {
1692 int equal;
1693
1694 ++comparing_specializations;
1695 ++comparing_dependent_aliases;
1696 ++processing_template_decl;
1697 equal = (e1->tmpl == e2->tmpl
1698 && comp_template_args (e1->args, e2->args));
1699 if (equal && flag_concepts
1700 /* tmpl could be a FIELD_DECL for a capture pack. */
1701 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1702 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1703 && uses_template_parms (e1->args))
1704 {
1705 /* Partial specializations of a variable template can be distinguished by
1706 constraints. */
1707 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1708 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1709 equal = equivalent_constraints (c1, c2);
1710 }
1711 --processing_template_decl;
1712 --comparing_dependent_aliases;
1713 --comparing_specializations;
1714
1715 return equal;
1716 }
1717
1718 /* Returns a hash for a template TMPL and template arguments ARGS. */
1719
1720 static hashval_t
1721 hash_tmpl_and_args (tree tmpl, tree args)
1722 {
1723 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1724 return iterative_hash_template_arg (args, val);
1725 }
1726
1727 hashval_t
1728 spec_hasher::hash (tree tmpl, tree args)
1729 {
1730 ++comparing_specializations;
1731 hashval_t val = hash_tmpl_and_args (tmpl, args);
1732 --comparing_specializations;
1733 return val;
1734 }
1735
1736 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1737 ignoring SPEC. */
1738
1739 hashval_t
1740 spec_hasher::hash (spec_entry *e)
1741 {
1742 if (e->hash == 0)
1743 e->hash = hash (e->tmpl, e->args);
1744 return e->hash;
1745 }
1746
1747 /* Recursively calculate a hash value for a template argument ARG, for use
1748 in the hash tables of template specializations. We must be
1749 careful to (at least) skip the same entities template_args_equal
1750 does. */
1751
1752 hashval_t
1753 iterative_hash_template_arg (tree arg, hashval_t val)
1754 {
1755 if (arg == NULL_TREE)
1756 return iterative_hash_hashval_t (0, val);
1757
1758 if (!TYPE_P (arg))
1759 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1760 while (CONVERT_EXPR_P (arg)
1761 || TREE_CODE (arg) == NON_LVALUE_EXPR
1762 || class_nttp_const_wrapper_p (arg))
1763 arg = TREE_OPERAND (arg, 0);
1764
1765 enum tree_code code = TREE_CODE (arg);
1766
1767 val = iterative_hash_hashval_t (code, val);
1768
1769 switch (code)
1770 {
1771 case ARGUMENT_PACK_SELECT:
1772 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1773 preserving it in a hash table, which is bad because it will change
1774 meaning when gen_elem_of_pack_expansion_instantiation changes the
1775 ARGUMENT_PACK_SELECT_INDEX. */
1776 gcc_unreachable ();
1777
1778 case ERROR_MARK:
1779 return val;
1780
1781 case IDENTIFIER_NODE:
1782 return iterative_hash_hashval_t (IDENTIFIER_HASH_VALUE (arg), val);
1783
1784 case TREE_VEC:
1785 for (tree elt : tree_vec_range (arg))
1786 val = iterative_hash_template_arg (elt, val);
1787 return val;
1788
1789 case TYPE_PACK_EXPANSION:
1790 case EXPR_PACK_EXPANSION:
1791 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1792 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1793
1794 case PACK_INDEX_TYPE:
1795 case PACK_INDEX_EXPR:
1796 val = iterative_hash_template_arg (PACK_INDEX_PACK (arg), val);
1797 return iterative_hash_template_arg (PACK_INDEX_INDEX (arg), val);
1798
1799 case TYPE_ARGUMENT_PACK:
1800 case NONTYPE_ARGUMENT_PACK:
1801 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1802
1803 case TREE_LIST:
1804 for (; arg; arg = TREE_CHAIN (arg))
1805 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1806 return val;
1807
1808 case OVERLOAD:
1809 for (lkp_iterator iter (arg); iter; ++iter)
1810 val = iterative_hash_template_arg (*iter, val);
1811 return val;
1812
1813 case CONSTRUCTOR:
1814 {
1815 iterative_hash_template_arg (TREE_TYPE (arg), val);
1816 for (auto &e: CONSTRUCTOR_ELTS (arg))
1817 {
1818 val = iterative_hash_template_arg (e.index, val);
1819 val = iterative_hash_template_arg (e.value, val);
1820 }
1821 return val;
1822 }
1823
1824 case PARM_DECL:
1825 if (!DECL_ARTIFICIAL (arg))
1826 {
1827 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1828 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1829 }
1830 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1831
1832 case TEMPLATE_DECL:
1833 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
1834 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1835 break;
1836
1837 case TARGET_EXPR:
1838 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1839
1840 case PTRMEM_CST:
1841 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1842 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1843
1844 case TEMPLATE_PARM_INDEX:
1845 val = iterative_hash_template_arg
1846 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1847 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1848 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1849
1850 case TRAIT_EXPR:
1851 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1852 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1853 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1854
1855 case BASELINK:
1856 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1857 val);
1858 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1859 val);
1860
1861 case MODOP_EXPR:
1862 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1863 code = TREE_CODE (TREE_OPERAND (arg, 1));
1864 val = iterative_hash_object (code, val);
1865 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1866
1867 case LAMBDA_EXPR:
1868 /* [temp.over.link] Two lambda-expressions are never considered
1869 equivalent.
1870
1871 So just hash the closure type. */
1872 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1873
1874 case CAST_EXPR:
1875 case IMPLICIT_CONV_EXPR:
1876 case STATIC_CAST_EXPR:
1877 case REINTERPRET_CAST_EXPR:
1878 case CONST_CAST_EXPR:
1879 case DYNAMIC_CAST_EXPR:
1880 case NEW_EXPR:
1881 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1882 /* Now hash operands as usual. */
1883 break;
1884
1885 case CALL_EXPR:
1886 {
1887 tree fn = CALL_EXPR_FN (arg);
1888 if (tree name = call_expr_dependent_name (arg))
1889 {
1890 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1891 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1892 fn = name;
1893 }
1894 val = iterative_hash_template_arg (fn, val);
1895 call_expr_arg_iterator ai;
1896 for (tree x = first_call_expr_arg (arg, &ai); x;
1897 x = next_call_expr_arg (&ai))
1898 val = iterative_hash_template_arg (x, val);
1899 return val;
1900 }
1901
1902 default:
1903 break;
1904 }
1905
1906 char tclass = TREE_CODE_CLASS (code);
1907 switch (tclass)
1908 {
1909 case tcc_type:
1910 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1911 {
1912 // We want an alias specialization that survived strip_typedefs
1913 // to hash differently from its TYPE_CANONICAL, to avoid hash
1914 // collisions that compare as different in template_args_equal.
1915 // These could be dependent specializations that strip_typedefs
1916 // left alone for example.
1917 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1918 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1919 }
1920
1921 switch (code)
1922 {
1923 case DECLTYPE_TYPE:
1924 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1925 break;
1926
1927 case TYPENAME_TYPE:
1928 if (comparing_specializations)
1929 {
1930 /* Hash the components that are relevant to TYPENAME_TYPE
1931 equivalence as determined by structural_comptypes. We
1932 can only coherently do this when comparing_specializations
1933 is set, because otherwise structural_comptypes tries
1934 resolving TYPENAME_TYPE via the current instantiation. */
1935 tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
1936 tree fullname = TYPENAME_TYPE_FULLNAME (arg);
1937 val = iterative_hash_template_arg (context, val);
1938 val = iterative_hash_template_arg (fullname, val);
1939 }
1940 break;
1941
1942 default:
1943 if (tree canonical = TYPE_CANONICAL (arg))
1944 val = iterative_hash_hashval_t (TYPE_HASH (canonical), val);
1945 else if (tree ti = TYPE_TEMPLATE_INFO (arg))
1946 {
1947 val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
1948 val = iterative_hash_template_arg (TI_ARGS (ti), val);
1949 }
1950 break;
1951 }
1952
1953 return val;
1954
1955 case tcc_declaration:
1956 case tcc_constant:
1957 return iterative_hash_expr (arg, val);
1958
1959 default:
1960 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1961 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1962 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1963 return val;
1964 }
1965 }
1966
1967 /* Unregister the specialization SPEC as a specialization of TMPL.
1968 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1969 if the SPEC was listed as a specialization of TMPL.
1970
1971 Note that SPEC has been ggc_freed, so we can't look inside it. */
1972
1973 bool
1974 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1975 {
1976 spec_entry *entry;
1977 spec_entry elt;
1978
1979 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1980 elt.args = TI_ARGS (tinfo);
1981
1982 entry = decl_specializations->find (&elt);
1983 if (entry != NULL)
1984 {
1985 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1986 gcc_assert (new_spec != NULL_TREE);
1987 entry->spec = new_spec;
1988
1989 /* We need to also remove SPEC from DECL_TEMPLATE_INSTANTIATIONS
1990 if it was placed there. */
1991 for (tree *inst = &DECL_TEMPLATE_INSTANTIATIONS (elt.tmpl);
1992 *inst; inst = &TREE_CHAIN (*inst))
1993 if (TREE_VALUE (*inst) == spec)
1994 {
1995 *inst = TREE_CHAIN (*inst);
1996 break;
1997 }
1998
1999 return 1;
2000 }
2001
2002 return 0;
2003 }
2004
2005 /* Like register_specialization, but for local declarations. We are
2006 registering SPEC, an instantiation of TMPL. */
2007
2008 void
2009 register_local_specialization (tree spec, tree tmpl)
2010 {
2011 gcc_assert (tmpl != spec);
2012 local_specializations->put (tmpl, spec);
2013 }
2014
2015 /* Registers T as a specialization of itself. This is used to preserve
2016 the references to already-parsed parameters when instantiating
2017 postconditions. */
2018
2019 void
2020 register_local_identity (tree t)
2021 {
2022 local_specializations->put (t, t);
2023 }
2024
2025 /* TYPE is a class type. Returns true if TYPE is an explicitly
2026 specialized class. */
2027
2028 bool
2029 explicit_class_specialization_p (tree type)
2030 {
2031 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2032 return false;
2033 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2034 }
2035
2036 /* Print the list of functions at FNS, going through all the overloads
2037 for each element of the list. Alternatively, FNS cannot be a
2038 TREE_LIST, in which case it will be printed together with all the
2039 overloads.
2040
2041 MORE and *STR should respectively be FALSE and NULL when the function
2042 is called from the outside. They are used internally on recursive
2043 calls. print_candidates manages the two parameters and leaves NULL
2044 in *STR when it ends. */
2045
2046 static void
2047 print_candidates_1 (tree fns, char **str, bool more = false)
2048 {
2049 if (TREE_CODE (fns) == TREE_LIST)
2050 for (; fns; fns = TREE_CHAIN (fns))
2051 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2052 else
2053 for (lkp_iterator iter (fns); iter;)
2054 {
2055 tree cand = *iter;
2056 ++iter;
2057
2058 const char *pfx = *str;
2059 if (!pfx)
2060 {
2061 if (more || iter)
2062 pfx = _("candidates are:");
2063 else
2064 pfx = _("candidate is:");
2065 *str = get_spaces (pfx);
2066 }
2067 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2068 }
2069 }
2070
2071 /* Print the list of candidate FNS in an error message. FNS can also
2072 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2073
2074 void
2075 print_candidates (tree fns)
2076 {
2077 char *str = NULL;
2078 print_candidates_1 (fns, &str);
2079 free (str);
2080 }
2081
2082 /* Get a (possibly) constrained template declaration for the
2083 purpose of ordering candidates. */
2084 static tree
2085 get_template_for_ordering (tree list)
2086 {
2087 gcc_assert (TREE_CODE (list) == TREE_LIST);
2088 tree f = TREE_VALUE (list);
2089 if (f == NULL_TREE)
2090 /* Also handle a list from resolve_address_of_overloaded_function with the
2091 function in TREE_PURPOSE. */
2092 f = TREE_PURPOSE (list);
2093 if (tree ti = DECL_TEMPLATE_INFO (f))
2094 return TI_TEMPLATE (ti);
2095 return f;
2096 }
2097
2098 /* Among candidates having the same signature, return the
2099 most constrained or NULL_TREE if there is no best candidate.
2100 If the signatures of candidates vary (e.g., template
2101 specialization vs. member function), then there can be no
2102 most constrained.
2103
2104 Note that we don't compare constraints on the functions
2105 themselves, but rather those of their templates. */
2106 tree
2107 most_constrained_function (tree candidates)
2108 {
2109 // Try to find the best candidate in a first pass.
2110 tree champ = candidates;
2111 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2112 {
2113 int winner = more_constrained (get_template_for_ordering (champ),
2114 get_template_for_ordering (c));
2115 if (winner == -1)
2116 champ = c; // The candidate is more constrained
2117 else if (winner == 0)
2118 return NULL_TREE; // Neither is more constrained
2119 }
2120
2121 // Verify that the champ is better than previous candidates.
2122 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2123 if (!more_constrained (get_template_for_ordering (champ),
2124 get_template_for_ordering (c)))
2125 return NULL_TREE;
2126 }
2127
2128 return champ;
2129 }
2130
2131
2132 /* Returns the template (one of the functions given by TEMPLATE_ID)
2133 which can be specialized to match the indicated DECL with the
2134 explicit template args given in TEMPLATE_ID. The DECL may be
2135 NULL_TREE if none is available. In that case, the functions in
2136 TEMPLATE_ID are non-members.
2137
2138 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2139 specialization of a member template.
2140
2141 The TEMPLATE_COUNT is the number of references to qualifying
2142 template classes that appeared in the name of the function. See
2143 check_explicit_specialization for a more accurate description.
2144
2145 TSK indicates what kind of template declaration (if any) is being
2146 declared. TSK_TEMPLATE indicates that the declaration given by
2147 DECL, though a FUNCTION_DECL, has template parameters, and is
2148 therefore a template function.
2149
2150 The template args (those explicitly specified and those deduced)
2151 are output in a newly created vector *TARGS_OUT.
2152
2153 If it is impossible to determine the result, an error message is
2154 issued. The error_mark_node is returned to indicate failure. */
2155
2156 static tree
2157 determine_specialization (tree template_id,
2158 tree decl,
2159 tree* targs_out,
2160 int need_member_template,
2161 int template_count,
2162 tmpl_spec_kind tsk)
2163 {
2164 tree fns;
2165 tree targs;
2166 tree explicit_targs;
2167 tree candidates = NULL_TREE;
2168
2169 /* A TREE_LIST of templates of which DECL may be a specialization.
2170 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2171 corresponding TREE_PURPOSE is the set of template arguments that,
2172 when used to instantiate the template, would produce a function
2173 with the signature of DECL. */
2174 tree templates = NULL_TREE;
2175 int header_count;
2176 cp_binding_level *b;
2177
2178 *targs_out = NULL_TREE;
2179
2180 if (template_id == error_mark_node || decl == error_mark_node)
2181 return error_mark_node;
2182
2183 /* We shouldn't be specializing a member template of an
2184 unspecialized class template; we already gave an error in
2185 check_specialization_scope, now avoid crashing. */
2186 if (!VAR_P (decl)
2187 && template_count && DECL_CLASS_SCOPE_P (decl)
2188 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2189 {
2190 gcc_assert (errorcount);
2191 return error_mark_node;
2192 }
2193
2194 fns = TREE_OPERAND (template_id, 0);
2195 explicit_targs = TREE_OPERAND (template_id, 1);
2196
2197 if (fns == error_mark_node)
2198 return error_mark_node;
2199
2200 /* Check for baselinks. */
2201 if (BASELINK_P (fns))
2202 fns = BASELINK_FUNCTIONS (fns);
2203
2204 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2205 {
2206 error_at (DECL_SOURCE_LOCATION (decl),
2207 "%qD is not a function template", fns);
2208 return error_mark_node;
2209 }
2210 else if (VAR_P (decl) && !variable_template_p (fns))
2211 {
2212 error ("%qD is not a variable template", fns);
2213 return error_mark_node;
2214 }
2215
2216 /* Count the number of template headers specified for this
2217 specialization. */
2218 header_count = 0;
2219 for (b = current_binding_level;
2220 b->kind == sk_template_parms;
2221 b = b->level_chain)
2222 ++header_count;
2223
2224 tree orig_fns = fns;
2225 bool header_mismatch = false;
2226
2227 if (variable_template_p (fns))
2228 {
2229 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2230 targs = coerce_template_parms (parms, explicit_targs, fns,
2231 tf_warning_or_error);
2232 if (targs != error_mark_node
2233 && constraints_satisfied_p (fns, targs))
2234 templates = tree_cons (targs, fns, templates);
2235 }
2236 else for (lkp_iterator iter (fns); iter; ++iter)
2237 {
2238 tree fn = *iter;
2239
2240 if (TREE_CODE (fn) == TEMPLATE_DECL)
2241 {
2242 tree decl_arg_types;
2243 tree fn_arg_types;
2244
2245 /* In case of explicit specialization, we need to check if
2246 the number of template headers appearing in the specialization
2247 is correct. This is usually done in check_explicit_specialization,
2248 but the check done there cannot be exhaustive when specializing
2249 member functions. Consider the following code:
2250
2251 template <> void A<int>::f(int);
2252 template <> template <> void A<int>::f(int);
2253
2254 Assuming that A<int> is not itself an explicit specialization
2255 already, the first line specializes "f" which is a non-template
2256 member function, whilst the second line specializes "f" which
2257 is a template member function. So both lines are syntactically
2258 correct, and check_explicit_specialization does not reject
2259 them.
2260
2261 Here, we can do better, as we are matching the specialization
2262 against the declarations. We count the number of template
2263 headers, and we check if they match TEMPLATE_COUNT + 1
2264 (TEMPLATE_COUNT is the number of qualifying template classes,
2265 plus there must be another header for the member template
2266 itself).
2267
2268 Notice that if header_count is zero, this is not a
2269 specialization but rather a template instantiation, so there
2270 is no check we can perform here. */
2271 if (header_count && header_count != template_count + 1)
2272 {
2273 header_mismatch = true;
2274 continue;
2275 }
2276
2277 /* Check that the number of template arguments at the
2278 innermost level for DECL is the same as for FN. */
2279 if (current_binding_level->kind == sk_template_parms
2280 && !current_binding_level->explicit_spec_p
2281 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2282 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2283 (current_template_parms))))
2284 continue;
2285
2286 /* DECL might be a specialization of FN. */
2287 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2288 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2289
2290 /* For a non-static member function, we need to make sure
2291 that the const qualification is the same. Since
2292 get_bindings does not try to merge the "this" parameter,
2293 we must do the comparison explicitly. */
2294 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
2295 {
2296 if (!same_type_p (TREE_VALUE (fn_arg_types),
2297 TREE_VALUE (decl_arg_types)))
2298 continue;
2299
2300 /* And the ref-qualification. */
2301 if (type_memfn_rqual (TREE_TYPE (decl))
2302 != type_memfn_rqual (TREE_TYPE (fn)))
2303 continue;
2304 }
2305
2306 /* Skip the "this" parameter and, for constructors of
2307 classes with virtual bases, the VTT parameter. A
2308 full specialization of a constructor will have a VTT
2309 parameter, but a template never will. */
2310 decl_arg_types
2311 = skip_artificial_parms_for (decl, decl_arg_types);
2312 fn_arg_types
2313 = skip_artificial_parms_for (fn, fn_arg_types);
2314
2315 /* Function templates cannot be specializations; there are
2316 no partial specializations of functions. Therefore, if
2317 the type of DECL does not match FN, there is no
2318 match.
2319
2320 Note that it should never be the case that we have both
2321 candidates added here, and for regular member functions
2322 below. */
2323 if (tsk == tsk_template)
2324 {
2325 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2326 current_template_parms))
2327 continue;
2328 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2329 TREE_TYPE (TREE_TYPE (fn))))
2330 continue;
2331 if (!compparms (fn_arg_types, decl_arg_types))
2332 continue;
2333
2334 tree freq = get_constraints (fn);
2335 tree dreq = get_constraints (decl);
2336 if (!freq != !dreq)
2337 continue;
2338 if (freq)
2339 {
2340 /* C++20 CA104: Substitute directly into the
2341 constraint-expression. */
2342 tree fargs = DECL_TI_ARGS (fn);
2343 tsubst_flags_t complain = tf_none;
2344 freq = tsubst_constraint_info (freq, fargs, complain, fn);
2345 if (!cp_tree_equal (freq, dreq))
2346 continue;
2347 }
2348
2349 candidates = tree_cons (NULL_TREE, fn, candidates);
2350 continue;
2351 }
2352
2353 /* See whether this function might be a specialization of this
2354 template. Suppress access control because we might be trying
2355 to make this specialization a friend, and we have already done
2356 access control for the declaration of the specialization. */
2357 push_deferring_access_checks (dk_no_check);
2358 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2359 pop_deferring_access_checks ();
2360
2361 if (!targs)
2362 /* We cannot deduce template arguments that when used to
2363 specialize TMPL will produce DECL. */
2364 continue;
2365
2366 if (uses_template_parms (targs))
2367 /* We deduced something involving 'auto', which isn't a valid
2368 template argument. */
2369 continue;
2370
2371 /* Save this template, and the arguments deduced. */
2372 templates = tree_cons (targs, fn, templates);
2373 }
2374 else if (need_member_template)
2375 /* FN is an ordinary member function, and we need a
2376 specialization of a member template. */
2377 ;
2378 else if (TREE_CODE (fn) != FUNCTION_DECL)
2379 /* We can get IDENTIFIER_NODEs here in certain erroneous
2380 cases. */
2381 ;
2382 else if (!DECL_FUNCTION_MEMBER_P (fn))
2383 /* This is just an ordinary non-member function. Nothing can
2384 be a specialization of that. */
2385 ;
2386 else if (DECL_ARTIFICIAL (fn))
2387 /* Cannot specialize functions that are created implicitly. */
2388 ;
2389 else
2390 {
2391 tree decl_arg_types;
2392
2393 /* This is an ordinary member function. However, since
2394 we're here, we can assume its enclosing class is a
2395 template class. For example,
2396
2397 template <typename T> struct S { void f(); };
2398 template <> void S<int>::f() {}
2399
2400 Here, S<int>::f is a non-template, but S<int> is a
2401 template class. If FN has the same type as DECL, we
2402 might be in business. */
2403
2404 if (!DECL_TEMPLATE_INFO (fn))
2405 /* Its enclosing class is an explicit specialization
2406 of a template class. This is not a candidate. */
2407 continue;
2408
2409 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2410 TREE_TYPE (TREE_TYPE (fn))))
2411 /* The return types differ. */
2412 continue;
2413
2414 /* Adjust the type of DECL in case FN is a static member. */
2415 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2416 if (DECL_STATIC_FUNCTION_P (fn)
2417 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2418 decl_arg_types = TREE_CHAIN (decl_arg_types);
2419
2420 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2421 decl_arg_types))
2422 continue;
2423
2424 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2425 && (type_memfn_rqual (TREE_TYPE (decl))
2426 != type_memfn_rqual (TREE_TYPE (fn))))
2427 continue;
2428
2429 // If the deduced arguments do not satisfy the constraints,
2430 // this is not a candidate.
2431 if (flag_concepts && !constraints_satisfied_p (fn))
2432 continue;
2433
2434 // Add the candidate.
2435 candidates = tree_cons (NULL_TREE, fn, candidates);
2436 }
2437 }
2438
2439 if (templates && TREE_CHAIN (templates))
2440 {
2441 /* We have:
2442
2443 [temp.expl.spec]
2444
2445 It is possible for a specialization with a given function
2446 signature to be instantiated from more than one function
2447 template. In such cases, explicit specification of the
2448 template arguments must be used to uniquely identify the
2449 function template specialization being specialized.
2450
2451 Note that here, there's no suggestion that we're supposed to
2452 determine which of the candidate templates is most
2453 specialized. However, we, also have:
2454
2455 [temp.func.order]
2456
2457 Partial ordering of overloaded function template
2458 declarations is used in the following contexts to select
2459 the function template to which a function template
2460 specialization refers:
2461
2462 -- when an explicit specialization refers to a function
2463 template.
2464
2465 So, we do use the partial ordering rules, at least for now.
2466 This extension can only serve to make invalid programs valid,
2467 so it's safe. And, there is strong anecdotal evidence that
2468 the committee intended the partial ordering rules to apply;
2469 the EDG front end has that behavior, and John Spicer claims
2470 that the committee simply forgot to delete the wording in
2471 [temp.expl.spec]. */
2472 tree tmpl = most_specialized_instantiation (templates);
2473 if (tmpl != error_mark_node)
2474 {
2475 templates = tmpl;
2476 TREE_CHAIN (templates) = NULL_TREE;
2477 }
2478 }
2479
2480 // Concepts allows multiple declarations of member functions
2481 // with the same signature. Like above, we need to rely on
2482 // on the partial ordering of those candidates to determine which
2483 // is the best.
2484 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2485 {
2486 if (tree cand = most_constrained_function (candidates))
2487 {
2488 candidates = cand;
2489 TREE_CHAIN (cand) = NULL_TREE;
2490 }
2491 }
2492
2493 if (templates == NULL_TREE && candidates == NULL_TREE)
2494 {
2495 auto_diagnostic_group d;
2496 error ("template-id %qD for %q+D does not match any template "
2497 "declaration", template_id, decl);
2498 if (header_mismatch)
2499 inform (DECL_SOURCE_LOCATION (decl),
2500 "saw %d %<template<>%>, need %d for "
2501 "specializing a member function template",
2502 header_count, template_count + 1);
2503 print_candidates (orig_fns);
2504 return error_mark_node;
2505 }
2506 else if ((templates && TREE_CHAIN (templates))
2507 || (candidates && TREE_CHAIN (candidates))
2508 || (templates && candidates))
2509 {
2510 auto_diagnostic_group d;
2511 error ("ambiguous template specialization %qD for %q+D",
2512 template_id, decl);
2513 candidates = chainon (candidates, templates);
2514 print_candidates (candidates);
2515 return error_mark_node;
2516 }
2517
2518 /* We have one, and exactly one, match. */
2519 if (candidates)
2520 {
2521 tree fn = TREE_VALUE (candidates);
2522 *targs_out = copy_node (DECL_TI_ARGS (fn));
2523
2524 /* Propagate the candidate's constraints to the declaration. */
2525 if (tsk != tsk_template)
2526 set_constraints (decl, get_constraints (fn));
2527
2528 /* DECL is a re-declaration or partial instantiation of a template
2529 function. */
2530 if (TREE_CODE (fn) == TEMPLATE_DECL)
2531 return fn;
2532 /* It was a specialization of an ordinary member function in a
2533 template class. */
2534 return DECL_TI_TEMPLATE (fn);
2535 }
2536
2537 /* It was a specialization of a template. */
2538 tree tmpl = TREE_VALUE (templates);
2539 *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
2540
2541 /* Propagate the template's constraints to the declaration. */
2542 if (tsk != tsk_template)
2543 set_constraints (decl, get_constraints (tmpl));
2544
2545 return tmpl;
2546 }
2547
2548 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2549 but with the default argument values filled in from those in the
2550 TMPL_TYPES. */
2551
2552 static tree
2553 copy_default_args_to_explicit_spec_1 (tree spec_types,
2554 tree tmpl_types)
2555 {
2556 tree new_spec_types;
2557
2558 if (!spec_types)
2559 return NULL_TREE;
2560
2561 if (spec_types == void_list_node)
2562 return void_list_node;
2563
2564 /* Substitute into the rest of the list. */
2565 new_spec_types =
2566 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2567 TREE_CHAIN (tmpl_types));
2568
2569 /* Add the default argument for this parameter. */
2570 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2571 TREE_VALUE (spec_types),
2572 new_spec_types);
2573 }
2574
2575 /* DECL is an explicit specialization. Replicate default arguments
2576 from the template it specializes. (That way, code like:
2577
2578 template <class T> void f(T = 3);
2579 template <> void f(double);
2580 void g () { f (); }
2581
2582 works, as required.) An alternative approach would be to look up
2583 the correct default arguments at the call-site, but this approach
2584 is consistent with how implicit instantiations are handled. */
2585
2586 static void
2587 copy_default_args_to_explicit_spec (tree decl)
2588 {
2589 tree tmpl;
2590 tree spec_types;
2591 tree tmpl_types;
2592 tree new_spec_types;
2593 tree old_type;
2594 tree new_type;
2595 tree t;
2596 tree object_type = NULL_TREE;
2597 tree in_charge = NULL_TREE;
2598 tree vtt = NULL_TREE;
2599
2600 /* See if there's anything we need to do. */
2601 tmpl = DECL_TI_TEMPLATE (decl);
2602 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2603 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2604 if (TREE_PURPOSE (t))
2605 break;
2606 if (!t)
2607 return;
2608
2609 old_type = TREE_TYPE (decl);
2610 spec_types = TYPE_ARG_TYPES (old_type);
2611
2612 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2613 {
2614 /* Remove the this pointer, but remember the object's type for
2615 CV quals. */
2616 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2617 spec_types = TREE_CHAIN (spec_types);
2618 tmpl_types = TREE_CHAIN (tmpl_types);
2619
2620 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2621 {
2622 /* DECL may contain more parameters than TMPL due to the extra
2623 in-charge parameter in constructors and destructors. */
2624 in_charge = spec_types;
2625 spec_types = TREE_CHAIN (spec_types);
2626 }
2627 if (DECL_HAS_VTT_PARM_P (decl))
2628 {
2629 vtt = spec_types;
2630 spec_types = TREE_CHAIN (spec_types);
2631 }
2632 }
2633
2634 /* Compute the merged default arguments. */
2635 new_spec_types =
2636 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2637
2638 /* Compute the new FUNCTION_TYPE. */
2639 if (object_type)
2640 {
2641 if (vtt)
2642 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2643 TREE_VALUE (vtt),
2644 new_spec_types);
2645
2646 if (in_charge)
2647 /* Put the in-charge parameter back. */
2648 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2649 TREE_VALUE (in_charge),
2650 new_spec_types);
2651
2652 new_type = build_method_type_directly (object_type,
2653 TREE_TYPE (old_type),
2654 new_spec_types);
2655 }
2656 else
2657 new_type = build_function_type (TREE_TYPE (old_type),
2658 new_spec_types);
2659 new_type = cp_build_type_attribute_variant (new_type,
2660 TYPE_ATTRIBUTES (old_type));
2661 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2662
2663 TREE_TYPE (decl) = new_type;
2664 }
2665
2666 /* Return the number of template headers we expect to see for a definition
2667 or specialization of CTYPE or one of its non-template members. */
2668
2669 int
2670 num_template_headers_for_class (tree ctype)
2671 {
2672 int num_templates = 0;
2673
2674 while (ctype && CLASS_TYPE_P (ctype))
2675 {
2676 /* You're supposed to have one `template <...>' for every
2677 template class, but you don't need one for a full
2678 specialization. For example:
2679
2680 template <class T> struct S{};
2681 template <> struct S<int> { void f(); };
2682 void S<int>::f () {}
2683
2684 is correct; there shouldn't be a `template <>' for the
2685 definition of `S<int>::f'. */
2686 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2687 /* If CTYPE does not have template information of any
2688 kind, then it is not a template, nor is it nested
2689 within a template. */
2690 break;
2691 if (explicit_class_specialization_p (ctype))
2692 break;
2693 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2694 ++num_templates;
2695
2696 ctype = TYPE_CONTEXT (ctype);
2697 }
2698
2699 return num_templates;
2700 }
2701
2702 /* Do a simple sanity check on the template headers that precede the
2703 variable declaration DECL. */
2704
2705 void
2706 check_template_variable (tree decl)
2707 {
2708 tree ctx = CP_DECL_CONTEXT (decl);
2709 int wanted = num_template_headers_for_class (ctx);
2710 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2711 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2712 {
2713 if (cxx_dialect < cxx14)
2714 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2715 "variable templates only available with "
2716 "%<-std=c++14%> or %<-std=gnu++14%>");
2717
2718 // Namespace-scope variable templates should have a template header.
2719 ++wanted;
2720 }
2721 if (template_header_count > wanted)
2722 {
2723 auto_diagnostic_group d;
2724 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2725 "too many template headers for %qD "
2726 "(should be %d)",
2727 decl, wanted);
2728 if (warned && CLASS_TYPE_P (ctx)
2729 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2730 inform (DECL_SOURCE_LOCATION (decl),
2731 "members of an explicitly specialized class are defined "
2732 "without a template header");
2733 }
2734 }
2735
2736 /* An explicit specialization whose declarator-id or class-head-name is not
2737 qualified shall be declared in the nearest enclosing namespace of the
2738 template, or, if the namespace is inline (7.3.1), any namespace from its
2739 enclosing namespace set.
2740
2741 If the name declared in the explicit instantiation is an unqualified name,
2742 the explicit instantiation shall appear in the namespace where its template
2743 is declared or, if that namespace is inline (7.3.1), any namespace from its
2744 enclosing namespace set. */
2745
2746 void
2747 check_unqualified_spec_or_inst (tree t, location_t loc)
2748 {
2749 tree tmpl = most_general_template (t);
2750 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2751 && !is_nested_namespace (current_namespace,
2752 CP_DECL_CONTEXT (tmpl), true))
2753 {
2754 if (processing_specialization)
2755 permerror (loc, "explicit specialization of %qD outside its "
2756 "namespace must use a nested-name-specifier", tmpl);
2757 else if (processing_explicit_instantiation
2758 && cxx_dialect >= cxx11)
2759 /* This was allowed in C++98, so only pedwarn. */
2760 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2761 "outside its namespace must use a nested-name-"
2762 "specifier", tmpl);
2763 }
2764 }
2765
2766 /* Warn for a template specialization SPEC that is missing some of a set
2767 of function or type attributes that the template TEMPL is declared with.
2768 ATTRLIST is a list of additional attributes that SPEC should be taken
2769 to ultimately be declared with. */
2770
2771 static void
2772 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2773 {
2774 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2775 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2776
2777 /* Avoid warning if the difference between the primary and
2778 the specialization is not in one of the attributes below. */
2779 const char* const blacklist[] = {
2780 "alloc_align", "alloc_size", "assume_aligned", "format",
2781 "format_arg", "malloc", "nonnull", NULL
2782 };
2783
2784 /* Put together a list of the black listed attributes that the primary
2785 template is declared with that the specialization is not, in case
2786 it's not apparent from the most recent declaration of the primary. */
2787 auto_vec<const char *> mismatches;
2788 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2789 blacklist, mismatches);
2790
2791 if (!nattrs)
2792 return;
2793
2794 auto_diagnostic_group d;
2795 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2796 "explicit specialization %q#D may be missing attributes",
2797 spec))
2798 {
2799 pp_markup::comma_separated_quoted_strings e (mismatches);
2800 inform (DECL_SOURCE_LOCATION (tmpl),
2801 nattrs > 1
2802 ? G_("missing primary template attributes %e")
2803 : G_("missing primary template attribute %e"),
2804 &e);
2805 }
2806 }
2807
2808 /* Check to see if the function just declared, as indicated in
2809 DECLARATOR, and in DECL, is a specialization of a function
2810 template. We may also discover that the declaration is an explicit
2811 instantiation at this point.
2812
2813 Returns DECL, or an equivalent declaration that should be used
2814 instead if all goes well. Issues an error message if something is
2815 amiss. Returns error_mark_node if the error is not easily
2816 recoverable.
2817
2818 FLAGS is a bitmask consisting of the following flags:
2819
2820 2: The function has a definition.
2821 4: The function is a friend.
2822
2823 The TEMPLATE_COUNT is the number of references to qualifying
2824 template classes that appeared in the name of the function. For
2825 example, in
2826
2827 template <class T> struct S { void f(); };
2828 void S<int>::f();
2829
2830 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2831 classes are not counted in the TEMPLATE_COUNT, so that in
2832
2833 template <class T> struct S {};
2834 template <> struct S<int> { void f(); }
2835 template <> void S<int>::f();
2836
2837 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2838 invalid; there should be no template <>.)
2839
2840 If the function is a specialization, it is marked as such via
2841 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2842 is set up correctly, and it is added to the list of specializations
2843 for that template. */
2844
2845 tree
2846 check_explicit_specialization (tree declarator,
2847 tree decl,
2848 int template_count,
2849 int flags,
2850 tree attrlist)
2851 {
2852 int have_def = flags & 2;
2853 int is_friend = flags & 4;
2854 bool is_concept = flags & 8;
2855 int specialization = 0;
2856 int explicit_instantiation = 0;
2857 int member_specialization = 0;
2858 tree ctype = DECL_CLASS_CONTEXT (decl);
2859 tree dname = DECL_NAME (decl);
2860 tmpl_spec_kind tsk;
2861
2862 if (is_friend)
2863 {
2864 if (!processing_specialization)
2865 tsk = tsk_none;
2866 else
2867 tsk = tsk_excessive_parms;
2868 }
2869 else
2870 tsk = current_tmpl_spec_kind (template_count);
2871
2872 switch (tsk)
2873 {
2874 case tsk_none:
2875 if (processing_specialization && !VAR_P (decl))
2876 {
2877 specialization = 1;
2878 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2879 }
2880 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
2881 || (DECL_LANG_SPECIFIC (decl)
2882 && DECL_IMPLICIT_INSTANTIATION (decl)))
2883 {
2884 if (is_friend)
2885 /* This could be something like:
2886
2887 template <class T> void f(T);
2888 class S { friend void f<>(int); } */
2889 specialization = 1;
2890 else
2891 {
2892 /* This case handles bogus declarations like template <>
2893 template <class T> void f<int>(); */
2894
2895 error_at (cp_expr_loc_or_input_loc (declarator),
2896 "template-id %qE in declaration of primary template",
2897 declarator);
2898 return decl;
2899 }
2900 }
2901 break;
2902
2903 case tsk_invalid_member_spec:
2904 /* The error has already been reported in
2905 check_specialization_scope. */
2906 return error_mark_node;
2907
2908 case tsk_invalid_expl_inst:
2909 error ("template parameter list used in explicit instantiation");
2910
2911 /* Fall through. */
2912
2913 case tsk_expl_inst:
2914 if (have_def)
2915 error ("definition provided for explicit instantiation");
2916
2917 explicit_instantiation = 1;
2918 break;
2919
2920 case tsk_excessive_parms:
2921 case tsk_insufficient_parms:
2922 if (tsk == tsk_excessive_parms)
2923 error ("too many template parameter lists in declaration of %qD",
2924 decl);
2925 else if (template_header_count)
2926 error("too few template parameter lists in declaration of %qD", decl);
2927 else
2928 error("explicit specialization of %qD must be introduced by "
2929 "%<template <>%>", decl);
2930
2931 /* Fall through. */
2932 case tsk_expl_spec:
2933 if (is_concept)
2934 error ("explicit specialization declared %<concept%>");
2935
2936 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2937 /* In cases like template<> constexpr bool v = true;
2938 We'll give an error in check_template_variable. */
2939 break;
2940
2941 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2942 if (ctype)
2943 member_specialization = 1;
2944 else
2945 specialization = 1;
2946 break;
2947
2948 case tsk_template:
2949 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2950 {
2951 /* This case handles bogus declarations like template <>
2952 template <class T> void f<int>(); */
2953
2954 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2955 error_at (cp_expr_loc_or_input_loc (declarator),
2956 "template-id %qE in declaration of primary template",
2957 declarator);
2958 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2959 {
2960 /* Partial specialization of variable template. */
2961 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2962 specialization = 1;
2963 goto ok;
2964 }
2965 else if (cxx_dialect < cxx14)
2966 error_at (cp_expr_loc_or_input_loc (declarator),
2967 "non-type partial specialization %qE "
2968 "is not allowed", declarator);
2969 else
2970 error_at (cp_expr_loc_or_input_loc (declarator),
2971 "non-class, non-variable partial specialization %qE "
2972 "is not allowed", declarator);
2973 return decl;
2974 ok:;
2975 }
2976
2977 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2978 /* This is a specialization of a member template, without
2979 specialization the containing class. Something like:
2980
2981 template <class T> struct S {
2982 template <class U> void f (U);
2983 };
2984 template <> template <class U> void S<int>::f(U) {}
2985
2986 That's a specialization -- but of the entire template. */
2987 specialization = 1;
2988 break;
2989
2990 default:
2991 gcc_unreachable ();
2992 }
2993
2994 if ((specialization || member_specialization)
2995 /* This doesn't apply to variable templates. */
2996 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2997 {
2998 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2999 for (; t; t = TREE_CHAIN (t))
3000 if (TREE_PURPOSE (t))
3001 {
3002 permerror (input_location,
3003 "default argument specified in explicit specialization");
3004 break;
3005 }
3006 }
3007
3008 if (specialization || member_specialization || explicit_instantiation)
3009 {
3010 tree tmpl = NULL_TREE;
3011 tree targs = NULL_TREE;
3012 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
3013 bool found_hidden = false;
3014
3015 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3016 if (!was_template_id)
3017 {
3018 tree fns;
3019
3020 gcc_assert (identifier_p (declarator));
3021 if (ctype)
3022 fns = dname;
3023 else
3024 {
3025 /* If there is no class context, the explicit instantiation
3026 must be at namespace scope. */
3027 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3028
3029 /* Find the namespace binding, using the declaration
3030 context. */
3031 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3032 LOOK_want::NORMAL, true);
3033 if (fns == error_mark_node)
3034 {
3035 /* If lookup fails, look for a friend declaration so we can
3036 give a better diagnostic. */
3037 fns = (lookup_qualified_name
3038 (CP_DECL_CONTEXT (decl), dname,
3039 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3040 /*complain*/true));
3041 found_hidden = true;
3042 }
3043
3044 if (fns == error_mark_node || !is_overloaded_fn (fns))
3045 {
3046 error ("%qD is not a template function", dname);
3047 fns = error_mark_node;
3048 }
3049 }
3050
3051 declarator = lookup_template_function (fns, NULL_TREE);
3052 }
3053
3054 if (declarator == error_mark_node)
3055 return error_mark_node;
3056
3057 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3058 {
3059 if (!explicit_instantiation)
3060 /* A specialization in class scope. This is invalid,
3061 but the error will already have been flagged by
3062 check_specialization_scope. */
3063 return error_mark_node;
3064 else
3065 {
3066 /* It's not valid to write an explicit instantiation in
3067 class scope, e.g.:
3068
3069 class C { template void f(); }
3070
3071 This case is caught by the parser. However, on
3072 something like:
3073
3074 template class C { void f(); };
3075
3076 (which is invalid) we can get here. The error will be
3077 issued later. */
3078 ;
3079 }
3080
3081 return decl;
3082 }
3083 else if (ctype != NULL_TREE
3084 && (identifier_p (TREE_OPERAND (declarator, 0))))
3085 {
3086 // We'll match variable templates in start_decl.
3087 if (VAR_P (decl))
3088 return decl;
3089
3090 /* Find the list of functions in ctype that have the same
3091 name as the declared function. */
3092 tree name = TREE_OPERAND (declarator, 0);
3093
3094 if (constructor_name_p (name, ctype))
3095 {
3096 if (DECL_CONSTRUCTOR_P (decl)
3097 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3098 : !CLASSTYPE_DESTRUCTOR (ctype))
3099 {
3100 /* From [temp.expl.spec]:
3101
3102 If such an explicit specialization for the member
3103 of a class template names an implicitly-declared
3104 special member function (clause _special_), the
3105 program is ill-formed.
3106
3107 Similar language is found in [temp.explicit]. */
3108 error ("specialization of implicitly-declared special member function");
3109 return error_mark_node;
3110 }
3111
3112 name = DECL_NAME (decl);
3113 }
3114
3115 /* For a type-conversion operator, We might be looking for
3116 `operator int' which will be a specialization of
3117 `operator T'. Grab all the conversion operators, and
3118 then select from them. */
3119 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3120 ? conv_op_identifier : name);
3121
3122 if (fns == NULL_TREE)
3123 {
3124 error ("no member function %qD declared in %qT", name, ctype);
3125 return error_mark_node;
3126 }
3127 else
3128 TREE_OPERAND (declarator, 0) = fns;
3129 }
3130
3131 /* Figure out what exactly is being specialized at this point.
3132 Note that for an explicit instantiation, even one for a
3133 member function, we cannot tell a priori whether the
3134 instantiation is for a member template, or just a member
3135 function of a template class. Even if a member template is
3136 being instantiated, the member template arguments may be
3137 elided if they can be deduced from the rest of the
3138 declaration. */
3139 tmpl = determine_specialization (declarator, decl,
3140 &targs,
3141 member_specialization,
3142 template_count,
3143 tsk);
3144
3145 if (!tmpl || tmpl == error_mark_node)
3146 /* We couldn't figure out what this declaration was
3147 specializing. */
3148 return error_mark_node;
3149 else
3150 {
3151 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3152 {
3153 auto_diagnostic_group d;
3154 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3155 "friend declaration %qD is not visible to "
3156 "explicit specialization", tmpl))
3157 inform (DECL_SOURCE_LOCATION (tmpl),
3158 "friend declaration here");
3159 }
3160
3161 if (!ctype && !is_friend
3162 && CP_DECL_CONTEXT (decl) == current_namespace)
3163 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3164
3165 tree gen_tmpl = most_general_template (tmpl);
3166
3167 if (explicit_instantiation)
3168 {
3169 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3170 is done by do_decl_instantiation later. */
3171
3172 int arg_depth = TMPL_ARGS_DEPTH (targs);
3173 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3174
3175 if (arg_depth > parm_depth)
3176 {
3177 /* If TMPL is not the most general template (for
3178 example, if TMPL is a friend template that is
3179 injected into namespace scope), then there will
3180 be too many levels of TARGS. Remove some of them
3181 here. */
3182 int i;
3183 tree new_targs;
3184
3185 new_targs = make_tree_vec (parm_depth);
3186 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3187 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3188 = TREE_VEC_ELT (targs, i);
3189 targs = new_targs;
3190 }
3191
3192 return instantiate_template (tmpl, targs, tf_error);
3193 }
3194
3195 /* If we thought that the DECL was a member function, but it
3196 turns out to be specializing a static member function,
3197 make DECL a static member function as well. */
3198 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3199 && DECL_STATIC_FUNCTION_P (tmpl)
3200 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
3201 revert_static_member_fn (decl);
3202
3203 /* If this is a specialization of a member template of a
3204 template class, we want to return the TEMPLATE_DECL, not
3205 the specialization of it. */
3206 if (tsk == tsk_template && !was_template_id)
3207 {
3208 tree result = DECL_TEMPLATE_RESULT (tmpl);
3209 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3210 DECL_INITIAL (result) = NULL_TREE;
3211 if (have_def)
3212 {
3213 tree parm;
3214 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3215 DECL_SOURCE_LOCATION (result)
3216 = DECL_SOURCE_LOCATION (decl);
3217 /* We want to use the argument list specified in the
3218 definition, not in the original declaration. */
3219 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3220 for (parm = DECL_ARGUMENTS (result); parm;
3221 parm = DECL_CHAIN (parm))
3222 DECL_CONTEXT (parm) = result;
3223 }
3224 decl = register_specialization (tmpl, gen_tmpl, targs,
3225 is_friend, 0);
3226 remove_contract_attributes (result);
3227 return decl;
3228 }
3229
3230 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3231 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3232
3233 if (was_template_id)
3234 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3235
3236 /* Inherit default function arguments from the template
3237 DECL is specializing. */
3238 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3239 copy_default_args_to_explicit_spec (decl);
3240
3241 /* This specialization has the same protection as the
3242 template it specializes. */
3243 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3244 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3245
3246 /* 7.1.1-1 [dcl.stc]
3247
3248 A storage-class-specifier shall not be specified in an
3249 explicit specialization...
3250
3251 The parser rejects these, so unless action is taken here,
3252 explicit function specializations will always appear with
3253 global linkage.
3254
3255 The action recommended by the C++ CWG in response to C++
3256 defect report 605 is to make the storage class and linkage
3257 of the explicit specialization match the templated function:
3258
3259 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3260 */
3261 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3262 {
3263 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3264 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3265
3266 /* This specialization has the same linkage and visibility as
3267 the function template it specializes. */
3268 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3269 if (! TREE_PUBLIC (decl))
3270 {
3271 DECL_INTERFACE_KNOWN (decl) = 1;
3272 DECL_NOT_REALLY_EXTERN (decl) = 1;
3273 }
3274 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3275 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3276 {
3277 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3278 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3279 }
3280 }
3281
3282 /* If DECL is a friend declaration, declared using an
3283 unqualified name, the namespace associated with DECL may
3284 have been set incorrectly. For example, in:
3285
3286 template <typename T> void f(T);
3287 namespace N {
3288 struct S { friend void f<int>(int); }
3289 }
3290
3291 we will have set the DECL_CONTEXT for the friend
3292 declaration to N, rather than to the global namespace. */
3293 if (DECL_NAMESPACE_SCOPE_P (decl))
3294 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3295
3296 if (is_friend && !have_def)
3297 /* This is not really a declaration of a specialization.
3298 It's just the name of an instantiation. But, it's not
3299 a request for an instantiation, either. */
3300 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3301 else if (TREE_CODE (decl) == FUNCTION_DECL)
3302 /* A specialization is not necessarily COMDAT. */
3303 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3304 && DECL_DECLARED_INLINE_P (decl));
3305 else if (VAR_P (decl))
3306 DECL_COMDAT (decl) = false;
3307
3308 /* If this is a full specialization, register it so that we can find
3309 it again. Partial specializations will be registered in
3310 process_partial_specialization. */
3311 if (!processing_template_decl)
3312 {
3313 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3314
3315 decl = register_specialization (decl, gen_tmpl, targs,
3316 is_friend, 0);
3317 }
3318
3319 /* If this is a specialization, splice any contracts that may have
3320 been inherited from the template, removing them. */
3321 if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
3322 remove_contract_attributes (decl);
3323
3324 /* A 'structor should already have clones. */
3325 gcc_assert (decl == error_mark_node
3326 || variable_template_p (tmpl)
3327 || !(DECL_CONSTRUCTOR_P (decl)
3328 || DECL_DESTRUCTOR_P (decl))
3329 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3330 }
3331 }
3332
3333 return decl;
3334 }
3335
3336 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3337 parameters. These are represented in the same format used for
3338 DECL_TEMPLATE_PARMS. */
3339
3340 int
3341 comp_template_parms (const_tree parms1, const_tree parms2)
3342 {
3343 if (parms1 == parms2)
3344 return 1;
3345
3346 tree t1 = TREE_VALUE (parms1);
3347 tree t2 = TREE_VALUE (parms2);
3348 int i;
3349
3350 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3351 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3352
3353 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3354 return 0;
3355
3356 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3357 {
3358 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3359 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3360
3361 /* If either of the template parameters are invalid, assume
3362 they match for the sake of error recovery. */
3363 if (error_operand_p (parm1) || error_operand_p (parm2))
3364 return 1;
3365
3366 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3367 return 0;
3368
3369 if (TREE_CODE (parm1) == TYPE_DECL
3370 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
3371 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
3372 continue;
3373 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3374 return 0;
3375 }
3376
3377 return 1;
3378 }
3379
3380 /* Returns true if two template parameters are declared with
3381 equivalent constraints. */
3382
3383 static bool
3384 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3385 {
3386 tree req1 = TREE_TYPE (parm1);
3387 tree req2 = TREE_TYPE (parm2);
3388 if (!req1 != !req2)
3389 return false;
3390 if (req1)
3391 return cp_tree_equal (req1, req2);
3392 return true;
3393 }
3394
3395 /* Returns true when two template parameters are equivalent. */
3396
3397 static bool
3398 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3399 {
3400 tree decl1 = TREE_VALUE (parm1);
3401 tree decl2 = TREE_VALUE (parm2);
3402
3403 /* If either of the template parameters are invalid, assume
3404 they match for the sake of error recovery. */
3405 if (error_operand_p (decl1) || error_operand_p (decl2))
3406 return true;
3407
3408 /* ... they declare parameters of the same kind. */
3409 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3410 return false;
3411
3412 /* ... one parameter was introduced by a parameter declaration, then
3413 both are. This case arises as a result of eagerly rewriting declarations
3414 during parsing. */
3415 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
3416 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
3417 return false;
3418
3419 /* ... if either declares a pack, they both do. */
3420 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3421 return false;
3422
3423 if (TREE_CODE (decl1) == PARM_DECL)
3424 {
3425 /* ... if they declare non-type parameters, the types are equivalent. */
3426 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3427 return false;
3428 }
3429 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3430 {
3431 /* ... if they declare template template parameters, their template
3432 parameter lists are equivalent. */
3433 if (!template_heads_equivalent_p (decl1, decl2))
3434 return false;
3435 }
3436
3437 /* ... if they are declared with a qualified-concept name, they both
3438 are, and those names are equivalent. */
3439 return template_parameter_constraints_equivalent_p (parm1, parm2);
3440 }
3441
3442 /* Returns true if two template parameters lists are equivalent.
3443 Two template parameter lists are equivalent if they have the
3444 same length and their corresponding parameters are equivalent.
3445
3446 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3447 data structure returned by DECL_TEMPLATE_PARMS.
3448
3449 This is generally the same implementation as comp_template_parms
3450 except that it also the concept names and arguments used to
3451 introduce parameters. */
3452
3453 static bool
3454 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3455 {
3456 if (parms1 == parms2)
3457 return true;
3458
3459 tree list1 = TREE_VALUE (parms1);
3460 tree list2 = TREE_VALUE (parms2);
3461
3462 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3463 return 0;
3464
3465 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3466 {
3467 tree parm1 = TREE_VEC_ELT (list1, i);
3468 tree parm2 = TREE_VEC_ELT (list2, i);
3469 if (!template_parameters_equivalent_p (parm1, parm2))
3470 return false;
3471 }
3472
3473 return true;
3474 }
3475
3476 /* Return true if the requires-clause of the template parameter lists are
3477 equivalent and false otherwise. */
3478 static bool
3479 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3480 {
3481 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3482 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3483 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3484 return false;
3485 if (!cp_tree_equal (req1, req2))
3486 return false;
3487 return true;
3488 }
3489
3490 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3491 Two template heads are equivalent if their template parameter
3492 lists are equivalent and their requires clauses are equivalent.
3493
3494 In pre-C++20, this is equivalent to calling comp_template_parms
3495 for the template parameters of TMPL1 and TMPL2. */
3496
3497 bool
3498 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3499 {
3500 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3501 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3502
3503 /* ... have the same number of template parameters, and their
3504 corresponding parameters are equivalent. */
3505 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3506 return false;
3507
3508 /* ... if either has a requires-clause, they both do and their
3509 corresponding constraint-expressions are equivalent. */
3510 return template_requirements_equivalent_p (parms1, parms2);
3511 }
3512
3513 /* Determine whether PARM is a parameter pack. */
3514
3515 bool
3516 template_parameter_pack_p (const_tree parm)
3517 {
3518 /* Determine if we have a non-type template parameter pack. */
3519 if (TREE_CODE (parm) == PARM_DECL)
3520 return (DECL_TEMPLATE_PARM_P (parm)
3521 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3522 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3523 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3524
3525 /* If this is a list of template parameters, we could get a
3526 TYPE_DECL or a TEMPLATE_DECL. */
3527 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3528 parm = TREE_TYPE (parm);
3529
3530 /* Otherwise it must be a type template parameter. */
3531 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3532 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3533 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3534 }
3535
3536 /* Determine if T is a function parameter pack. */
3537
3538 bool
3539 function_parameter_pack_p (const_tree t)
3540 {
3541 if (t && TREE_CODE (t) == PARM_DECL)
3542 return DECL_PACK_P (t);
3543 return false;
3544 }
3545
3546 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3547 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3548
3549 tree
3550 get_function_template_decl (const_tree primary_func_tmpl_inst)
3551 {
3552 if (! primary_func_tmpl_inst
3553 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3554 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3555 return NULL;
3556
3557 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3558 }
3559
3560 /* Return true iff the function parameter PARAM_DECL was expanded
3561 from the function parameter pack PACK. */
3562
3563 bool
3564 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3565 {
3566 if (DECL_ARTIFICIAL (param_decl)
3567 || !function_parameter_pack_p (pack))
3568 return false;
3569
3570 /* The parameter pack and its pack arguments have the same
3571 DECL_PARM_INDEX. */
3572 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3573 }
3574
3575 /* Determine whether ARGS describes a variadic template args list,
3576 i.e., one that is terminated by a template argument pack. */
3577
3578 static bool
3579 template_args_variadic_p (tree args)
3580 {
3581 int nargs;
3582 tree last_parm;
3583
3584 if (args == NULL_TREE)
3585 return false;
3586
3587 args = INNERMOST_TEMPLATE_ARGS (args);
3588 nargs = TREE_VEC_LENGTH (args);
3589
3590 if (nargs == 0)
3591 return false;
3592
3593 last_parm = TREE_VEC_ELT (args, nargs - 1);
3594
3595 return ARGUMENT_PACK_P (last_parm);
3596 }
3597
3598 /* Generate a new name for the parameter pack name NAME (an
3599 IDENTIFIER_NODE) that incorporates its */
3600
3601 static tree
3602 make_ith_pack_parameter_name (tree name, int i)
3603 {
3604 /* Munge the name to include the parameter index. */
3605 #define NUMBUF_LEN 128
3606 char numbuf[NUMBUF_LEN];
3607 char* newname;
3608 int newname_len;
3609
3610 if (name == NULL_TREE)
3611 return name;
3612 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3613 newname_len = IDENTIFIER_LENGTH (name)
3614 + strlen (numbuf) + 2;
3615 newname = (char*)alloca (newname_len);
3616 snprintf (newname, newname_len,
3617 "%s#%i", IDENTIFIER_POINTER (name), i);
3618 return get_identifier (newname);
3619 }
3620
3621 /* Return true if T is a primary function, class or alias template
3622 specialization, not including the template pattern. */
3623
3624 bool
3625 primary_template_specialization_p (const_tree t)
3626 {
3627 if (!t)
3628 return false;
3629
3630 if (VAR_OR_FUNCTION_DECL_P (t))
3631 return (DECL_LANG_SPECIFIC (t)
3632 && DECL_USE_TEMPLATE (t)
3633 && DECL_TEMPLATE_INFO (t)
3634 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3635 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3636 return (CLASSTYPE_TEMPLATE_INFO (t)
3637 && CLASSTYPE_USE_TEMPLATE (t)
3638 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3639 else if (alias_template_specialization_p (t, nt_transparent))
3640 return true;
3641 return false;
3642 }
3643
3644 /* Return true if PARM is a template template parameter. */
3645
3646 bool
3647 template_template_parameter_p (const_tree parm)
3648 {
3649 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3650 }
3651
3652 /* Return true iff PARM is a DECL representing a type template
3653 parameter. */
3654
3655 bool
3656 template_type_parameter_p (const_tree parm)
3657 {
3658 return (parm
3659 && (TREE_CODE (parm) == TYPE_DECL
3660 || TREE_CODE (parm) == TEMPLATE_DECL)
3661 && DECL_TEMPLATE_PARM_P (parm));
3662 }
3663
3664 /* Return the template parameters of T if T is a
3665 primary template instantiation, NULL otherwise. */
3666
3667 tree
3668 get_primary_template_innermost_parameters (const_tree t)
3669 {
3670 tree parms = NULL, template_info = NULL;
3671
3672 if ((template_info = get_template_info (t))
3673 && primary_template_specialization_p (t))
3674 parms = INNERMOST_TEMPLATE_PARMS
3675 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3676
3677 return parms;
3678 }
3679
3680 /* Returns the template arguments of T if T is a template instantiation,
3681 NULL otherwise. */
3682
3683 tree
3684 get_template_innermost_arguments (const_tree t)
3685 {
3686 tree args = NULL, template_info = NULL;
3687
3688 if ((template_info = get_template_info (t))
3689 && TI_ARGS (template_info))
3690 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3691
3692 return args;
3693 }
3694
3695 /* Return the argument pack elements of T if T is a template argument pack,
3696 NULL otherwise. */
3697
3698 tree
3699 get_template_argument_pack_elems (const_tree t)
3700 {
3701 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3702 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3703 return NULL;
3704
3705 return ARGUMENT_PACK_ARGS (t);
3706 }
3707
3708 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3709 ARGUMENT_PACK_SELECT represents. */
3710
3711 static tree
3712 argument_pack_select_arg (tree t)
3713 {
3714 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3715 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3716
3717 /* If the selected argument is an expansion E, that most likely means we were
3718 called from gen_elem_of_pack_expansion_instantiation during the
3719 substituting of an argument pack (of which the Ith element is a pack
3720 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3721 In this case, the Ith element resulting from this substituting is going to
3722 be a pack expansion, which pattern is the pattern of E. Let's return the
3723 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3724 resulting pack expansion from it. */
3725 if (PACK_EXPANSION_P (arg))
3726 {
3727 /* Make sure we aren't throwing away arg info. */
3728 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3729 arg = PACK_EXPANSION_PATTERN (arg);
3730 }
3731
3732 return arg;
3733 }
3734
3735 /* Return a modification of ARGS that's suitable for preserving inside a hash
3736 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3737 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3738
3739 static tree
3740 preserve_args (tree args, bool cow_p = true)
3741 {
3742 if (!args)
3743 return NULL_TREE;
3744
3745 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
3746 {
3747 tree t = TREE_VEC_ELT (args, i);
3748 tree r;
3749 if (!t)
3750 r = NULL_TREE;
3751 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
3752 r = argument_pack_select_arg (t);
3753 else if (TREE_CODE (t) == TREE_VEC)
3754 r = preserve_args (t, cow_p);
3755 else
3756 r = t;
3757 if (r != t)
3758 {
3759 if (cow_p)
3760 {
3761 args = copy_template_args (args);
3762 cow_p = false;
3763 }
3764 TREE_VEC_ELT (args, i) = r;
3765 }
3766 }
3767
3768 return args;
3769 }
3770
3771 /* True iff FN is a function representing a built-in variadic parameter
3772 pack. */
3773
3774 bool
3775 builtin_pack_fn_p (tree fn)
3776 {
3777 if (!fn
3778 || TREE_CODE (fn) != FUNCTION_DECL
3779 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3780 return false;
3781
3782 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3783 return true;
3784
3785 return false;
3786 }
3787
3788 /* True iff CALL is a call to a function representing a built-in variadic
3789 parameter pack. */
3790
3791 static bool
3792 builtin_pack_call_p (tree call)
3793 {
3794 if (TREE_CODE (call) != CALL_EXPR)
3795 return false;
3796 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3797 }
3798
3799 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3800
3801 static tree
3802 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3803 tree in_decl)
3804 {
3805 tree ohi = CALL_EXPR_ARG (call, 0);
3806 tree hi = tsubst_expr (ohi, args, complain, in_decl);
3807
3808 if (instantiation_dependent_expression_p (hi))
3809 {
3810 if (hi != ohi)
3811 {
3812 call = copy_node (call);
3813 CALL_EXPR_ARG (call, 0) = hi;
3814 }
3815 tree ex = make_pack_expansion (call, complain);
3816 tree vec = make_tree_vec (1);
3817 TREE_VEC_ELT (vec, 0) = ex;
3818 return vec;
3819 }
3820 else
3821 {
3822 hi = instantiate_non_dependent_expr (hi, complain);
3823 hi = cxx_constant_value (hi, complain);
3824 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3825
3826 /* Calculate the largest value of len that won't make the size of the vec
3827 overflow an int. The compiler will exceed resource limits long before
3828 this, but it seems a decent place to diagnose. */
3829 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3830
3831 if (len < 0 || len > max)
3832 {
3833 if ((complain & tf_error)
3834 && hi != error_mark_node)
3835 error ("argument to %<__integer_pack%> must be between 0 and %d",
3836 max);
3837 return error_mark_node;
3838 }
3839
3840 tree vec = make_tree_vec (len);
3841
3842 for (int i = 0; i < len; ++i)
3843 TREE_VEC_ELT (vec, i) = size_int (i);
3844
3845 return vec;
3846 }
3847 }
3848
3849 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3850 CALL. */
3851
3852 static tree
3853 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3854 tree in_decl)
3855 {
3856 if (!builtin_pack_call_p (call))
3857 return NULL_TREE;
3858
3859 tree fn = CALL_EXPR_FN (call);
3860
3861 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3862 return expand_integer_pack (call, args, complain, in_decl);
3863
3864 return NULL_TREE;
3865 }
3866
3867 /* Return true if the tree T has the extra args mechanism for
3868 avoiding partial instantiation. */
3869
3870 static bool
3871 has_extra_args_mechanism_p (const_tree t)
3872 {
3873 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */
3874 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */
3875 || (TREE_CODE (t) == IF_STMT
3876 && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS */
3877 || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS */
3878 }
3879
3880 /* Return *_EXTRA_ARGS of the given supported tree T. */
3881
3882 static tree&
3883 tree_extra_args (tree t)
3884 {
3885 gcc_checking_assert (has_extra_args_mechanism_p (t));
3886
3887 if (PACK_EXPANSION_P (t))
3888 return PACK_EXPANSION_EXTRA_ARGS (t);
3889 else if (TREE_CODE (t) == REQUIRES_EXPR)
3890 return REQUIRES_EXPR_EXTRA_ARGS (t);
3891 else if (TREE_CODE (t) == IF_STMT
3892 && IF_STMT_CONSTEXPR_P (t))
3893 return IF_STMT_EXTRA_ARGS (t);
3894 else if (TREE_CODE (t) == LAMBDA_EXPR)
3895 return LAMBDA_EXPR_EXTRA_ARGS (t);
3896
3897 gcc_unreachable ();
3898 }
3899
3900 /* Structure used to track the progress of find_parameter_packs_r. */
3901 struct find_parameter_pack_data
3902 {
3903 /* TREE_LIST that will contain all of the parameter packs found by
3904 the traversal. */
3905 tree* parameter_packs;
3906
3907 /* Set of AST nodes that have been visited by the traversal. */
3908 hash_set<tree> *visited;
3909
3910 /* True iff we found a subtree that has the extra args mechanism. */
3911 bool found_extra_args_tree_p = false;
3912 };
3913
3914 /* Identifies all of the argument packs that occur in a template
3915 argument and appends them to the TREE_LIST inside DATA, which is a
3916 find_parameter_pack_data structure. This is a subroutine of
3917 make_pack_expansion and uses_parameter_packs. */
3918 static tree
3919 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3920 {
3921 tree t = *tp;
3922 struct find_parameter_pack_data* ppd =
3923 (struct find_parameter_pack_data*)data;
3924 bool parameter_pack_p = false;
3925
3926 #define WALK_SUBTREE(NODE) \
3927 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3928 ppd, ppd->visited) \
3929
3930 /* Don't look through typedefs; we are interested in whether a
3931 parameter pack is actually written in the expression/type we're
3932 looking at, not the target type. */
3933 if (TYPE_P (t) && typedef_variant_p (t))
3934 {
3935 /* But do look at arguments for an alias template. */
3936 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3937 cp_walk_tree (&TI_ARGS (tinfo),
3938 &find_parameter_packs_r,
3939 ppd, ppd->visited);
3940 *walk_subtrees = 0;
3941 return NULL_TREE;
3942 }
3943
3944 /* Identify whether this is a parameter pack or not. */
3945 switch (TREE_CODE (t))
3946 {
3947 case TEMPLATE_PARM_INDEX:
3948 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3949 parameter_pack_p = true;
3950 break;
3951
3952 case TEMPLATE_TYPE_PARM:
3953 t = TYPE_MAIN_VARIANT (t);
3954 /* FALLTHRU */
3955 case TEMPLATE_TEMPLATE_PARM:
3956 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3957 parameter_pack_p = true;
3958 break;
3959
3960 case FIELD_DECL:
3961 case PARM_DECL:
3962 if (DECL_PACK_P (t))
3963 {
3964 /* We don't want to walk into the type of a PARM_DECL,
3965 because we don't want to see the type parameter pack. */
3966 *walk_subtrees = 0;
3967 parameter_pack_p = true;
3968 }
3969 break;
3970
3971 case VAR_DECL:
3972 if (DECL_PACK_P (t))
3973 {
3974 /* We don't want to walk into the type of a variadic capture proxy,
3975 because we don't want to see the type parameter pack. */
3976 *walk_subtrees = 0;
3977 parameter_pack_p = true;
3978 }
3979 else if (variable_template_specialization_p (t))
3980 {
3981 cp_walk_tree (&DECL_TI_ARGS (t),
3982 find_parameter_packs_r,
3983 ppd, ppd->visited);
3984 *walk_subtrees = 0;
3985 }
3986 break;
3987
3988 case CALL_EXPR:
3989 if (builtin_pack_call_p (t))
3990 parameter_pack_p = true;
3991 break;
3992
3993 case BASES:
3994 parameter_pack_p = true;
3995 break;
3996 default:
3997 /* Not a parameter pack. */
3998 break;
3999 }
4000
4001 if (parameter_pack_p)
4002 {
4003 /* Add this parameter pack to the list. */
4004 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
4005 }
4006
4007 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
4008 ppd->found_extra_args_tree_p = true;
4009
4010 if (TYPE_P (t))
4011 cp_walk_tree (&TYPE_CONTEXT (t),
4012 &find_parameter_packs_r, ppd, ppd->visited);
4013
4014 /* This switch statement will return immediately if we don't find a
4015 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4016 switch (TREE_CODE (t))
4017 {
4018 case BOUND_TEMPLATE_TEMPLATE_PARM:
4019 /* Check the template itself. */
4020 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
4021 &find_parameter_packs_r, ppd, ppd->visited);
4022 return NULL_TREE;
4023
4024 case TEMPLATE_PARM_INDEX:
4025 if (parameter_pack_p)
4026 WALK_SUBTREE (TREE_TYPE (t));
4027 return NULL_TREE;
4028
4029 case DECL_EXPR:
4030 {
4031 tree decl = DECL_EXPR_DECL (t);
4032 /* Ignore the declaration of a capture proxy for a parameter pack. */
4033 if (is_capture_proxy (decl))
4034 *walk_subtrees = 0;
4035 if (is_typedef_decl (decl))
4036 /* Since we stop at typedefs above, we need to look through them at
4037 the point of the DECL_EXPR. */
4038 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4039 &find_parameter_packs_r, ppd, ppd->visited);
4040 return NULL_TREE;
4041 }
4042
4043 case TEMPLATE_DECL:
4044 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4045 return NULL_TREE;
4046 cp_walk_tree (&TREE_TYPE (t),
4047 &find_parameter_packs_r, ppd, ppd->visited);
4048 return NULL_TREE;
4049
4050 case TYPE_PACK_EXPANSION:
4051 case EXPR_PACK_EXPANSION:
4052 *walk_subtrees = 0;
4053 return NULL_TREE;
4054
4055 case PACK_INDEX_TYPE:
4056 case PACK_INDEX_EXPR:
4057 /* We can have an expansion of an expansion, such as "Ts...[Is]...",
4058 so do look into the index (but not the pack). */
4059 cp_walk_tree (&PACK_INDEX_INDEX (t), &find_parameter_packs_r, ppd,
4060 ppd->visited);
4061 *walk_subtrees = 0;
4062 return NULL_TREE;
4063
4064 case INTEGER_TYPE:
4065 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4066 ppd, ppd->visited);
4067 *walk_subtrees = 0;
4068 return NULL_TREE;
4069
4070 case IDENTIFIER_NODE:
4071 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4072 ppd->visited);
4073 *walk_subtrees = 0;
4074 return NULL_TREE;
4075
4076 case LAMBDA_EXPR:
4077 {
4078 /* Since we defer implicit capture, look in the parms and body. */
4079 tree fn = lambda_function (t);
4080 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4081 ppd->visited);
4082 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4083 ppd->visited);
4084 return NULL_TREE;
4085 }
4086
4087 case DECLTYPE_TYPE:
4088 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4089 ppd, ppd->visited);
4090 *walk_subtrees = 0;
4091 return NULL_TREE;
4092
4093 case IF_STMT:
4094 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4095 ppd, ppd->visited);
4096 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4097 ppd, ppd->visited);
4098 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4099 ppd, ppd->visited);
4100 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4101 *walk_subtrees = 0;
4102 return NULL_TREE;
4103
4104 case TAG_DEFN:
4105 t = TREE_TYPE (t);
4106 if (CLASS_TYPE_P (t))
4107 {
4108 /* Local class, need to look through the whole definition.
4109 TYPE_BINFO might be unset for a partial instantiation. */
4110 if (TYPE_BINFO (t))
4111 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4112 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4113 ppd, ppd->visited);
4114 }
4115 else
4116 /* Enum, look at the values. */
4117 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4118 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4119 &find_parameter_packs_r,
4120 ppd, ppd->visited);
4121 return NULL_TREE;
4122
4123 case FUNCTION_TYPE:
4124 case METHOD_TYPE:
4125 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4126 break;
4127
4128 default:
4129 return NULL_TREE;
4130 }
4131
4132 #undef WALK_SUBTREE
4133
4134 return NULL_TREE;
4135 }
4136
4137 /* Determines if the expression or type T uses any parameter packs. */
4138 tree
4139 uses_parameter_packs (tree t)
4140 {
4141 tree parameter_packs = NULL_TREE;
4142 struct find_parameter_pack_data ppd;
4143 ppd.parameter_packs = &parameter_packs;
4144 ppd.visited = new hash_set<tree>;
4145 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4146 delete ppd.visited;
4147 return parameter_packs;
4148 }
4149
4150 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4151 representation a base-class initializer into a parameter pack
4152 expansion. If all goes well, the resulting node will be an
4153 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4154 respectively. */
4155 tree
4156 make_pack_expansion (tree arg, tsubst_flags_t complain)
4157 {
4158 tree result;
4159 tree parameter_packs = NULL_TREE;
4160 bool for_types = false;
4161 struct find_parameter_pack_data ppd;
4162
4163 if (!arg || arg == error_mark_node)
4164 return arg;
4165
4166 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4167 {
4168 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4169 class initializer. In this case, the TREE_PURPOSE will be a
4170 _TYPE node (representing the base class expansion we're
4171 initializing) and the TREE_VALUE will be a TREE_LIST
4172 containing the initialization arguments.
4173
4174 The resulting expansion looks somewhat different from most
4175 expansions. Rather than returning just one _EXPANSION, we
4176 return a TREE_LIST whose TREE_PURPOSE is a
4177 TYPE_PACK_EXPANSION containing the bases that will be
4178 initialized. The TREE_VALUE will be identical to the
4179 original TREE_VALUE, which is a list of arguments that will
4180 be passed to each base. We do not introduce any new pack
4181 expansion nodes into the TREE_VALUE (although it is possible
4182 that some already exist), because the TREE_PURPOSE and
4183 TREE_VALUE all need to be expanded together with the same
4184 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4185 resulting TREE_PURPOSE will mention the parameter packs in
4186 both the bases and the arguments to the bases. */
4187 tree purpose;
4188 tree value;
4189 tree parameter_packs = NULL_TREE;
4190
4191 /* Determine which parameter packs will be used by the base
4192 class expansion. */
4193 ppd.visited = new hash_set<tree>;
4194 ppd.parameter_packs = &parameter_packs;
4195 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4196 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4197 &ppd, ppd.visited);
4198
4199 if (parameter_packs == NULL_TREE)
4200 {
4201 if (complain & tf_error)
4202 error ("base initializer expansion %qT contains no parameter packs",
4203 arg);
4204 delete ppd.visited;
4205 return error_mark_node;
4206 }
4207
4208 if (TREE_VALUE (arg) != void_type_node)
4209 {
4210 /* Collect the sets of parameter packs used in each of the
4211 initialization arguments. */
4212 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4213 {
4214 /* Determine which parameter packs will be expanded in this
4215 argument. */
4216 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4217 &ppd, ppd.visited);
4218 }
4219 }
4220
4221 delete ppd.visited;
4222
4223 /* Create the pack expansion type for the base type. */
4224 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4225 PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
4226 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4227 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4228
4229 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4230 they will rarely be compared to anything. */
4231 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4232
4233 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4234 }
4235
4236 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4237 for_types = true;
4238
4239 /* Build the PACK_EXPANSION_* node. */
4240 result = for_types
4241 ? cxx_make_type (TYPE_PACK_EXPANSION)
4242 : make_node (EXPR_PACK_EXPANSION);
4243 PACK_EXPANSION_PATTERN (result) = arg;
4244 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4245 {
4246 /* Propagate type and const-expression information. */
4247 TREE_TYPE (result) = TREE_TYPE (arg);
4248 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4249 /* Mark this read now, since the expansion might be length 0. */
4250 mark_exp_read (arg);
4251 }
4252 else
4253 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4254 they will rarely be compared to anything. */
4255 SET_TYPE_STRUCTURAL_EQUALITY (result);
4256
4257 /* Determine which parameter packs will be expanded. */
4258 ppd.parameter_packs = &parameter_packs;
4259 ppd.visited = new hash_set<tree>;
4260 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4261 delete ppd.visited;
4262
4263 /* Make sure we found some parameter packs. */
4264 if (parameter_packs == NULL_TREE)
4265 {
4266 if (complain & tf_error)
4267 {
4268 if (TYPE_P (arg))
4269 error ("expansion pattern %qT contains no parameter packs", arg);
4270 else
4271 error ("expansion pattern %qE contains no parameter packs", arg);
4272 }
4273 return error_mark_node;
4274 }
4275 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4276
4277 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4278 if (ppd.found_extra_args_tree_p)
4279 /* If the pattern of this pack expansion contains a subtree that has
4280 the extra args mechanism for avoiding partial instantiation, then
4281 force this pack expansion to also use extra args. Otherwise
4282 partial instantiation of this pack expansion may not lower the
4283 level of some parameter packs within the pattern, which would
4284 confuse tsubst_pack_expansion later (PR101764). */
4285 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
4286
4287 return result;
4288 }
4289
4290 /* Create a PACK_INDEX_* using the pack expansion PACK and index INDEX. */
4291
4292 tree
4293 make_pack_index (tree pack, tree index)
4294 {
4295 if (pack == error_mark_node)
4296 return error_mark_node;
4297
4298 bool for_types;
4299 if (TREE_CODE (pack) == TYPE_PACK_EXPANSION)
4300 for_types = true;
4301 else if (TREE_CODE (pack) == EXPR_PACK_EXPANSION)
4302 for_types = false;
4303 else
4304 {
4305 /* Maybe we've already partially substituted the pack. */
4306 gcc_checking_assert (TREE_CODE (pack) == TREE_VEC);
4307 for_types = TYPE_P (TREE_VEC_ELT (pack, 0));
4308 }
4309
4310 tree t = (for_types
4311 ? cxx_make_type (PACK_INDEX_TYPE)
4312 : make_node (PACK_INDEX_EXPR));
4313 PACK_INDEX_PACK (t) = pack;
4314 PACK_INDEX_INDEX (t) = index;
4315 if (TREE_CODE (t) == PACK_INDEX_TYPE)
4316 SET_TYPE_STRUCTURAL_EQUALITY (t);
4317 return t;
4318 }
4319
4320 /* Checks T for any "bare" parameter packs, which have not yet been
4321 expanded, and issues an error if any are found. This operation can
4322 only be done on full expressions or types (e.g., an expression
4323 statement, "if" condition, etc.), because we could have expressions like:
4324
4325 foo(f(g(h(args)))...)
4326
4327 where "args" is a parameter pack. check_for_bare_parameter_packs
4328 should not be called for the subexpressions args, h(args),
4329 g(h(args)), or f(g(h(args))), because we would produce erroneous
4330 error messages.
4331
4332 Returns TRUE and emits an error if there were bare parameter packs,
4333 returns FALSE otherwise. */
4334 bool
4335 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4336 {
4337 tree parameter_packs = NULL_TREE;
4338 struct find_parameter_pack_data ppd;
4339
4340 if (!processing_template_decl || !t || t == error_mark_node)
4341 return false;
4342
4343 if (TREE_CODE (t) == TYPE_DECL)
4344 t = TREE_TYPE (t);
4345
4346 ppd.parameter_packs = &parameter_packs;
4347 ppd.visited = new hash_set<tree>;
4348 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4349 delete ppd.visited;
4350
4351 if (!parameter_packs)
4352 return false;
4353
4354 if (loc == UNKNOWN_LOCATION)
4355 loc = cp_expr_loc_or_input_loc (t);
4356
4357 /* It's OK for a lambda to have an unexpanded parameter pack from the
4358 containing context, but do complain about unexpanded capture packs. */
4359 tree lam = current_lambda_expr ();
4360 if (lam)
4361 lam = TREE_TYPE (lam);
4362
4363 if (lam && lam != current_class_type)
4364 {
4365 /* We're in a lambda, but it isn't the innermost class.
4366 This should work, but currently doesn't. */
4367 sorry_at (loc, "unexpanded parameter pack in local class in lambda");
4368 return true;
4369 }
4370
4371 if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
4372 for (; parameter_packs;
4373 parameter_packs = TREE_CHAIN (parameter_packs))
4374 {
4375 tree pack = TREE_VALUE (parameter_packs);
4376 if (is_capture_proxy (pack)
4377 || (TREE_CODE (pack) == PARM_DECL
4378 && DECL_CONTEXT (pack)
4379 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
4380 break;
4381 }
4382
4383 if (parameter_packs)
4384 {
4385 auto_diagnostic_group d;
4386 error_at (loc, "parameter packs not expanded with %<...%>:");
4387 while (parameter_packs)
4388 {
4389 tree pack = TREE_VALUE (parameter_packs);
4390 tree name = NULL_TREE;
4391
4392 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4393 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4394 name = TYPE_NAME (pack);
4395 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4396 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4397 else if (TREE_CODE (pack) == CALL_EXPR)
4398 name = DECL_NAME (CALL_EXPR_FN (pack));
4399 else
4400 name = DECL_NAME (pack);
4401
4402 if (name)
4403 inform (loc, " %qD", name);
4404 else
4405 inform (loc, " %s", "<anonymous>");
4406
4407 parameter_packs = TREE_CHAIN (parameter_packs);
4408 }
4409
4410 return true;
4411 }
4412
4413 return false;
4414 }
4415
4416 /* Expand any parameter packs that occur in the template arguments in
4417 ARGS. */
4418 tree
4419 expand_template_argument_pack (tree args)
4420 {
4421 if (args == error_mark_node)
4422 return error_mark_node;
4423
4424 tree result_args = NULL_TREE;
4425 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4426 int num_result_args = -1;
4427 int non_default_args_count = -1;
4428
4429 /* First, determine if we need to expand anything, and the number of
4430 slots we'll need. */
4431 for (in_arg = 0; in_arg < nargs; ++in_arg)
4432 {
4433 tree arg = TREE_VEC_ELT (args, in_arg);
4434 if (arg == NULL_TREE)
4435 return args;
4436 if (ARGUMENT_PACK_P (arg))
4437 {
4438 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4439 if (num_result_args < 0)
4440 num_result_args = in_arg + num_packed;
4441 else
4442 num_result_args += num_packed;
4443 }
4444 else
4445 {
4446 if (num_result_args >= 0)
4447 num_result_args++;
4448 }
4449 }
4450
4451 /* If no expansion is necessary, we're done. */
4452 if (num_result_args < 0)
4453 return args;
4454
4455 /* Expand arguments. */
4456 result_args = make_tree_vec (num_result_args);
4457 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4458 non_default_args_count =
4459 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4460 for (in_arg = 0; in_arg < nargs; ++in_arg)
4461 {
4462 tree arg = TREE_VEC_ELT (args, in_arg);
4463 if (ARGUMENT_PACK_P (arg))
4464 {
4465 tree packed = ARGUMENT_PACK_ARGS (arg);
4466 int i, num_packed = TREE_VEC_LENGTH (packed);
4467 for (i = 0; i < num_packed; ++i, ++out_arg)
4468 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4469 if (non_default_args_count > 0)
4470 non_default_args_count += num_packed - 1;
4471 }
4472 else
4473 {
4474 TREE_VEC_ELT (result_args, out_arg) = arg;
4475 ++out_arg;
4476 }
4477 }
4478 if (non_default_args_count >= 0)
4479 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4480 return result_args;
4481 }
4482
4483 /* Checks if DECL shadows a template parameter.
4484
4485 [temp.local]: A template-parameter shall not be redeclared within its
4486 scope (including nested scopes).
4487
4488 Emits an error and returns TRUE if the DECL shadows a parameter,
4489 returns FALSE otherwise. */
4490
4491 bool
4492 check_template_shadow (tree decl)
4493 {
4494 tree olddecl;
4495
4496 /* If we're not in a template, we can't possibly shadow a template
4497 parameter. */
4498 if (!current_template_parms)
4499 return true;
4500
4501 /* Figure out what we're shadowing. */
4502 decl = OVL_FIRST (decl);
4503 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4504
4505 /* If there's no previous binding for this name, we're not shadowing
4506 anything, let alone a template parameter. */
4507 if (!olddecl)
4508 return true;
4509
4510 /* If we're not shadowing a template parameter, we're done. Note
4511 that OLDDECL might be an OVERLOAD (or perhaps even an
4512 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4513 node. */
4514 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4515 return true;
4516
4517 /* We check for decl != olddecl to avoid bogus errors for using a
4518 name inside a class. We check TPFI to avoid duplicate errors for
4519 inline member templates. */
4520 if (decl == olddecl
4521 || (DECL_TEMPLATE_PARM_P (decl)
4522 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4523 return true;
4524
4525 /* Don't complain about the injected class name, as we've already
4526 complained about the class itself. */
4527 if (DECL_SELF_REFERENCE_P (decl))
4528 return false;
4529
4530 auto_diagnostic_group d;
4531 if (DECL_TEMPLATE_PARM_P (decl))
4532 error ("declaration of template parameter %q+D shadows "
4533 "template parameter", decl);
4534 else
4535 error ("declaration of %q+#D shadows template parameter", decl);
4536 inform (DECL_SOURCE_LOCATION (olddecl),
4537 "template parameter %qD declared here", olddecl);
4538 return false;
4539 }
4540
4541 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4542 ORIG_LEVEL, DECL, and TYPE. */
4543
4544 static tree
4545 build_template_parm_index (int index,
4546 int level,
4547 int orig_level,
4548 tree decl,
4549 tree type)
4550 {
4551 tree t = make_node (TEMPLATE_PARM_INDEX);
4552 TEMPLATE_PARM_IDX (t) = index;
4553 TEMPLATE_PARM_LEVEL (t) = level;
4554 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4555 TEMPLATE_PARM_DECL (t) = decl;
4556 TREE_TYPE (t) = type;
4557 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4558 TREE_READONLY (t) = TREE_READONLY (decl);
4559
4560 return t;
4561 }
4562
4563 struct ctp_hasher : ggc_ptr_hash<tree_node>
4564 {
4565 static hashval_t hash (tree t)
4566 {
4567 ++comparing_specializations;
4568 tree_code code = TREE_CODE (t);
4569 hashval_t val = iterative_hash_object (code, 0);
4570 val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
4571 val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
4572 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
4573 {
4574 val
4575 = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
4576 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
4577 val = iterative_hash_placeholder_constraint (c, val);
4578 }
4579 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
4580 val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
4581 --comparing_specializations;
4582 return val;
4583 }
4584
4585 static bool equal (tree t, tree u)
4586 {
4587 ++comparing_specializations;
4588 bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
4589 --comparing_specializations;
4590 return eq;
4591 }
4592 };
4593
4594 static GTY (()) hash_table<ctp_hasher> *ctp_table;
4595
4596 /* Find the canonical type parameter for the given template type
4597 parameter. Returns the canonical type parameter, which may be TYPE
4598 if no such parameter existed. */
4599
4600 tree
4601 canonical_type_parameter (tree type)
4602 {
4603 if (ctp_table == NULL)
4604 ctp_table = hash_table<ctp_hasher>::create_ggc (61);
4605
4606 tree& slot = *ctp_table->find_slot (type, INSERT);
4607 if (slot == NULL_TREE)
4608 slot = type;
4609 return slot;
4610 }
4611
4612 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4613 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4614 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4615 new one is created. */
4616
4617 static tree
4618 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4619 tsubst_flags_t complain)
4620 {
4621 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4622 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4623 != TEMPLATE_PARM_LEVEL (index) - levels)
4624 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4625 {
4626 tree orig_decl = TEMPLATE_PARM_DECL (index);
4627
4628 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4629 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4630 type);
4631 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4632 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4633 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4634 DECL_ARTIFICIAL (decl) = 1;
4635 SET_DECL_TEMPLATE_PARM_P (decl);
4636
4637 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4638 TEMPLATE_PARM_LEVEL (index) - levels,
4639 TEMPLATE_PARM_ORIG_LEVEL (index),
4640 decl, type);
4641 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4642 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4643 = TEMPLATE_PARM_PARAMETER_PACK (index);
4644
4645 /* Template template parameters need this. */
4646 tree inner = decl;
4647 if (TREE_CODE (decl) == TEMPLATE_DECL)
4648 {
4649 inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
4650 TYPE_DECL, DECL_NAME (decl), type);
4651 DECL_TEMPLATE_RESULT (decl) = inner;
4652 DECL_ARTIFICIAL (inner) = true;
4653 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
4654 args, complain);
4655 DECL_TEMPLATE_PARMS (decl) = parms;
4656 tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
4657 DECL_TEMPLATE_INFO (inner)
4658 = build_template_info (DECL_TI_TEMPLATE (orig_inner),
4659 template_parms_to_args (parms));
4660 }
4661
4662 /* Attach the TPI to the decl. */
4663 if (TREE_CODE (inner) == TYPE_DECL)
4664 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4665 else
4666 DECL_INITIAL (decl) = tpi;
4667 }
4668
4669 return TEMPLATE_PARM_DESCENDANTS (index);
4670 }
4671
4672 /* Process information from new template parameter PARM and append it
4673 to the LIST being built. This new parameter is a non-type
4674 parameter iff IS_NON_TYPE is true. This new parameter is a
4675 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4676 is in PARM_LOC. */
4677
4678 tree
4679 process_template_parm (tree list, location_t parm_loc, tree parm,
4680 bool is_non_type, bool is_parameter_pack)
4681 {
4682 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4683 tree prev = NULL_TREE;
4684 int idx = 0;
4685
4686 if (list)
4687 {
4688 prev = tree_last (list);
4689
4690 tree p = TREE_VALUE (prev);
4691 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4692 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4693 else if (TREE_CODE (p) == PARM_DECL)
4694 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4695
4696 ++idx;
4697 }
4698
4699 tree decl = NULL_TREE;
4700 tree defval = TREE_PURPOSE (parm);
4701 tree constr = TREE_TYPE (parm);
4702
4703 if (is_non_type)
4704 {
4705 parm = TREE_VALUE (parm);
4706
4707 SET_DECL_TEMPLATE_PARM_P (parm);
4708
4709 if (TREE_TYPE (parm) != error_mark_node)
4710 {
4711 /* [temp.param]
4712
4713 The top-level cv-qualifiers on the template-parameter are
4714 ignored when determining its type. */
4715 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4716 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4717 TREE_TYPE (parm) = error_mark_node;
4718 else if (uses_parameter_packs (TREE_TYPE (parm))
4719 && !is_parameter_pack
4720 /* If we're in a nested template parameter list, the template
4721 template parameter could be a parameter pack. */
4722 && processing_template_parmlist == 1)
4723 {
4724 /* This template parameter is not a parameter pack, but it
4725 should be. Complain about "bare" parameter packs. */
4726 check_for_bare_parameter_packs (TREE_TYPE (parm));
4727
4728 /* Recover by calling this a parameter pack. */
4729 is_parameter_pack = true;
4730 }
4731 }
4732
4733 /* A template parameter is not modifiable. */
4734 TREE_CONSTANT (parm) = 1;
4735 TREE_READONLY (parm) = 1;
4736 decl = build_decl (parm_loc,
4737 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4738 TREE_CONSTANT (decl) = 1;
4739 TREE_READONLY (decl) = 1;
4740 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4741 = build_template_parm_index (idx, current_template_depth,
4742 current_template_depth,
4743 decl, TREE_TYPE (parm));
4744
4745 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4746 = is_parameter_pack;
4747 }
4748 else
4749 {
4750 tree t;
4751 parm = TREE_VALUE (TREE_VALUE (parm));
4752
4753 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4754 {
4755 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4756 /* This is for distinguishing between real templates and template
4757 template parameters */
4758 TREE_TYPE (parm) = t;
4759
4760 /* any_template_parm_r expects to be able to get the targs of a
4761 DECL_TEMPLATE_RESULT. */
4762 tree result = DECL_TEMPLATE_RESULT (parm);
4763 TREE_TYPE (result) = t;
4764 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4765 tree tinfo = build_template_info (parm, args);
4766 retrofit_lang_decl (result);
4767 DECL_TEMPLATE_INFO (result) = tinfo;
4768
4769 decl = parm;
4770 }
4771 else
4772 {
4773 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4774 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4775 decl = build_decl (parm_loc,
4776 TYPE_DECL, parm, t);
4777 }
4778
4779 TYPE_NAME (t) = decl;
4780 TYPE_STUB_DECL (t) = decl;
4781 parm = decl;
4782 TEMPLATE_TYPE_PARM_INDEX (t)
4783 = build_template_parm_index (idx, current_template_depth,
4784 current_template_depth,
4785 decl, TREE_TYPE (parm));
4786 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4787 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4788 }
4789 DECL_ARTIFICIAL (decl) = 1;
4790 SET_DECL_TEMPLATE_PARM_P (decl);
4791
4792 if (TREE_CODE (parm) == TEMPLATE_DECL
4793 && !uses_outer_template_parms (parm))
4794 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
4795
4796 /* Build requirements for the type/template parameter.
4797 This must be done after SET_DECL_TEMPLATE_PARM_P or
4798 process_template_parm could fail. */
4799 tree reqs = finish_shorthand_constraint (parm, constr);
4800
4801 decl = pushdecl (decl);
4802 if (!is_non_type)
4803 parm = decl;
4804
4805 /* Build the parameter node linking the parameter declaration,
4806 its default argument (if any), and its constraints (if any). */
4807 parm = build_tree_list (defval, parm);
4808 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4809
4810 if (prev)
4811 TREE_CHAIN (prev) = parm;
4812 else
4813 list = parm;
4814
4815 return list;
4816 }
4817
4818 /* The end of a template parameter list has been reached. Process the
4819 tree list into a parameter vector, converting each parameter into a more
4820 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4821 as PARM_DECLs. */
4822
4823 tree
4824 end_template_parm_list (tree parms)
4825 {
4826 tree saved_parmlist = make_tree_vec (list_length (parms));
4827
4828 /* Pop the dummy parameter level and add the real one. We do not
4829 morph the dummy parameter in place, as it might have been
4830 captured by a (nested) template-template-parm. */
4831 current_template_parms = TREE_CHAIN (current_template_parms);
4832
4833 current_template_parms
4834 = tree_cons (size_int (current_template_depth + 1),
4835 saved_parmlist, current_template_parms);
4836
4837 for (unsigned ix = 0; parms; ix++)
4838 {
4839 tree parm = parms;
4840 parms = TREE_CHAIN (parms);
4841 TREE_CHAIN (parm) = NULL_TREE;
4842
4843 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4844 }
4845
4846 --processing_template_parmlist;
4847
4848 return saved_parmlist;
4849 }
4850
4851 // Explicitly indicate the end of the template parameter list. We assume
4852 // that the current template parameters have been constructed and/or
4853 // managed explicitly, as when creating new template template parameters
4854 // from a shorthand constraint.
4855 void
4856 end_template_parm_list ()
4857 {
4858 --processing_template_parmlist;
4859 }
4860
4861 /* end_template_decl is called after a template declaration is seen. */
4862
4863 void
4864 end_template_decl (void)
4865 {
4866 reset_specialization ();
4867
4868 if (! processing_template_decl)
4869 return;
4870
4871 /* This matches the pushlevel in begin_template_parm_list. */
4872 finish_scope ();
4873
4874 --processing_template_decl;
4875 current_template_parms = TREE_CHAIN (current_template_parms);
4876 }
4877
4878 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4879 thereof, and converts it into an argument suitable to be passed to
4880 the type substitution functions. Note that if the TREE_LIST contains
4881 an error_mark node, the returned argument is error_mark_node. */
4882
4883 tree
4884 template_parm_to_arg (tree t)
4885 {
4886 if (!t)
4887 return NULL_TREE;
4888
4889 if (TREE_CODE (t) == TREE_LIST)
4890 t = TREE_VALUE (t);
4891
4892 if (error_operand_p (t))
4893 return error_mark_node;
4894
4895 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4896 {
4897 if (TREE_CODE (t) == TYPE_DECL
4898 || TREE_CODE (t) == TEMPLATE_DECL)
4899 t = TREE_TYPE (t);
4900 else
4901 t = DECL_INITIAL (t);
4902 }
4903
4904 gcc_assert (TEMPLATE_PARM_P (t));
4905
4906 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4907 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4908 {
4909 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4910 {
4911 /* Turn this argument into a TYPE_ARGUMENT_PACK
4912 with a single element, which expands T. */
4913 tree vec = make_tree_vec (1);
4914 if (CHECKING_P)
4915 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4916
4917 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4918
4919 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4920 ARGUMENT_PACK_ARGS (t) = vec;
4921 }
4922 }
4923 else
4924 {
4925 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4926 {
4927 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4928 with a single element, which expands T. */
4929 tree vec = make_tree_vec (1);
4930 if (CHECKING_P)
4931 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4932
4933 t = convert_from_reference (t);
4934 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4935
4936 t = make_node (NONTYPE_ARGUMENT_PACK);
4937 ARGUMENT_PACK_ARGS (t) = vec;
4938 }
4939 else
4940 t = convert_from_reference (t);
4941 }
4942 return t;
4943 }
4944
4945 /* If T looks like a generic template argument produced by template_parm_to_arg,
4946 return the corresponding template parameter, otherwise return NULL_TREE. */
4947
4948 static tree
4949 template_arg_to_parm (tree t)
4950 {
4951 if (t == NULL_TREE)
4952 return NULL_TREE;
4953
4954 if (ARGUMENT_PACK_P (t))
4955 {
4956 tree args = ARGUMENT_PACK_ARGS (t);
4957 if (TREE_VEC_LENGTH (args) == 1
4958 && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
4959 t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
4960 }
4961
4962 if (REFERENCE_REF_P (t))
4963 t = TREE_OPERAND (t, 0);
4964
4965 if (TEMPLATE_PARM_P (t))
4966 return t;
4967 else
4968 return NULL_TREE;
4969 }
4970
4971 /* Given a single level of template parameters (a TREE_VEC), return it
4972 as a set of template arguments. */
4973
4974 tree
4975 template_parms_level_to_args (tree parms)
4976 {
4977 parms = copy_node (parms);
4978 TREE_TYPE (parms) = NULL_TREE;
4979 for (tree& parm : tree_vec_range (parms))
4980 parm = template_parm_to_arg (parm);
4981
4982 if (CHECKING_P)
4983 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
4984
4985 return parms;
4986 }
4987
4988 /* Given a set of template parameters, return them as a set of template
4989 arguments. The template parameters are represented as a TREE_VEC, in
4990 the form documented in cp-tree.h for template arguments. */
4991
4992 tree
4993 template_parms_to_args (tree parms)
4994 {
4995 tree header;
4996 tree args = NULL_TREE;
4997 int length = TMPL_PARMS_DEPTH (parms);
4998 int l = length;
4999
5000 /* If there is only one level of template parameters, we do not
5001 create a TREE_VEC of TREE_VECs. Instead, we return a single
5002 TREE_VEC containing the arguments. */
5003 if (length > 1)
5004 args = make_tree_vec (length);
5005
5006 for (header = parms; header; header = TREE_CHAIN (header))
5007 {
5008 tree a = template_parms_level_to_args (TREE_VALUE (header));
5009
5010 if (length > 1)
5011 TREE_VEC_ELT (args, --l) = a;
5012 else
5013 args = a;
5014 }
5015
5016 return args;
5017 }
5018
5019 /* Within the declaration of a template, return the currently active
5020 template parameters as an argument TREE_VEC. */
5021
5022 static tree
5023 current_template_args (void)
5024 {
5025 return template_parms_to_args (current_template_parms);
5026 }
5027
5028 /* Return the fully generic arguments for of TMPL, i.e. what
5029 current_template_args would be while parsing it. */
5030
5031 tree
5032 generic_targs_for (tree tmpl)
5033 {
5034 if (tmpl == NULL_TREE)
5035 return NULL_TREE;
5036 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
5037 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
5038 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
5039 template parameter, it has no TEMPLATE_INFO; for a partial
5040 specialization, it has the arguments for the primary template, and we
5041 want the arguments for the partial specialization. */;
5042 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
5043 if (tree ti = get_template_info (result))
5044 return TI_ARGS (ti);
5045 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
5046 }
5047
5048 /* Return the template arguments corresponding to the template parameters of
5049 DECL's enclosing scope. When DECL is a member of a partial specialization,
5050 this returns the arguments for the partial specialization as opposed to those
5051 for the primary template, which is the main difference between this function
5052 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */
5053
5054 tree
5055 outer_template_args (const_tree decl)
5056 {
5057 if (TREE_CODE (decl) == TEMPLATE_DECL)
5058 decl = DECL_TEMPLATE_RESULT (decl);
5059 tree ti = get_template_info (decl);
5060 if (!ti)
5061 return NULL_TREE;
5062 tree args = TI_ARGS (ti);
5063 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
5064 return args;
5065 if (TMPL_ARGS_DEPTH (args) == 1)
5066 return NULL_TREE;
5067 return strip_innermost_template_args (args, 1);
5068 }
5069
5070 /* Update the declared TYPE by doing any lookups which were thought to be
5071 dependent, but are not now that we know the SCOPE of the declarator. */
5072
5073 tree
5074 maybe_update_decl_type (tree orig_type, tree scope)
5075 {
5076 tree type = orig_type;
5077
5078 if (type == NULL_TREE)
5079 return type;
5080
5081 if (TREE_CODE (orig_type) == TYPE_DECL)
5082 type = TREE_TYPE (type);
5083
5084 if (scope && TYPE_P (scope) && dependent_type_p (scope)
5085 && dependent_type_p (type)
5086 /* Don't bother building up the args in this case. */
5087 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
5088 {
5089 /* tsubst in the args corresponding to the template parameters,
5090 including auto if present. Most things will be unchanged, but
5091 make_typename_type and tsubst_qualified_id will resolve
5092 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5093 tree args = current_template_args ();
5094 tree auto_node = type_uses_auto (type);
5095 tree pushed;
5096 if (auto_node)
5097 {
5098 tree auto_vec = make_tree_vec (1);
5099 TREE_VEC_ELT (auto_vec, 0) = auto_node;
5100 args = add_to_template_args (args, auto_vec);
5101 }
5102 pushed = push_scope (scope);
5103 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
5104 if (pushed)
5105 pop_scope (scope);
5106 }
5107
5108 if (type == error_mark_node)
5109 return orig_type;
5110
5111 if (TREE_CODE (orig_type) == TYPE_DECL)
5112 {
5113 if (same_type_p (type, TREE_TYPE (orig_type)))
5114 type = orig_type;
5115 else
5116 type = TYPE_NAME (type);
5117 }
5118 return type;
5119 }
5120
5121 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5122 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5123 the new template is a member template. */
5124
5125 static tree
5126 build_template_decl (tree decl, tree parms, bool member_template_p)
5127 {
5128 gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
5129
5130 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
5131 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
5132 DECL_TEMPLATE_PARMS (tmpl) = parms;
5133 DECL_TEMPLATE_RESULT (tmpl) = decl;
5134 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
5135 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5136 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
5137 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
5138
5139 /* Propagate module information from the decl. */
5140 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
5141
5142 return tmpl;
5143 }
5144
5145 struct template_parm_data
5146 {
5147 /* The level of the template parameters we are currently
5148 processing. */
5149 int level;
5150
5151 /* The index of the specialization argument we are currently
5152 processing. */
5153 int current_arg;
5154
5155 /* An array whose size is the number of template parameters. The
5156 elements are nonzero if the parameter has been used in any one
5157 of the arguments processed so far. */
5158 int* parms;
5159
5160 /* An array whose size is the number of template arguments. The
5161 elements are nonzero if the argument makes use of template
5162 parameters of this level. */
5163 int* arg_uses_template_parms;
5164 };
5165
5166 /* Subroutine of push_template_decl used to see if each template
5167 parameter in a partial specialization is used in the explicit
5168 argument list. If T is of the LEVEL given in DATA (which is
5169 treated as a template_parm_data*), then DATA->PARMS is marked
5170 appropriately. */
5171
5172 static int
5173 mark_template_parm (tree t, void* data)
5174 {
5175 int level;
5176 int idx;
5177 struct template_parm_data* tpd = (struct template_parm_data*) data;
5178
5179 template_parm_level_and_index (t, &level, &idx);
5180
5181 if (level == tpd->level)
5182 {
5183 tpd->parms[idx] = 1;
5184 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5185 }
5186
5187 /* In C++17 the type of a non-type argument is a deduced context. */
5188 if (cxx_dialect >= cxx17
5189 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5190 for_each_template_parm (TREE_TYPE (t),
5191 &mark_template_parm,
5192 data,
5193 NULL,
5194 /*include_nondeduced_p=*/false);
5195
5196 /* Return zero so that for_each_template_parm will continue the
5197 traversal of the tree; we want to mark *every* template parm. */
5198 return 0;
5199 }
5200
5201 /* Process the partial specialization DECL. */
5202
5203 static tree
5204 process_partial_specialization (tree decl)
5205 {
5206 tree type = TREE_TYPE (decl);
5207 tree tinfo = get_template_info (decl);
5208 tree maintmpl = TI_TEMPLATE (tinfo);
5209 tree specargs = TI_ARGS (tinfo);
5210 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5211 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5212 tree inner_parms;
5213 tree inst;
5214 int nargs = TREE_VEC_LENGTH (inner_args);
5215 int ntparms;
5216 int i;
5217 struct template_parm_data tpd;
5218 struct template_parm_data tpd2;
5219
5220 gcc_assert (current_template_parms);
5221
5222 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5223 ntparms = TREE_VEC_LENGTH (inner_parms);
5224
5225 /* We check that each of the template parameters given in the
5226 partial specialization is used in the argument list to the
5227 specialization. For example:
5228
5229 template <class T> struct S;
5230 template <class T> struct S<T*>;
5231
5232 The second declaration is OK because `T*' uses the template
5233 parameter T, whereas
5234
5235 template <class T> struct S<int>;
5236
5237 is no good. Even trickier is:
5238
5239 template <class T>
5240 struct S1
5241 {
5242 template <class U>
5243 struct S2;
5244 template <class U>
5245 struct S2<T>;
5246 };
5247
5248 The S2<T> declaration is actually invalid; it is a
5249 full-specialization. Of course,
5250
5251 template <class U>
5252 struct S2<T (*)(U)>;
5253
5254 or some such would have been OK. */
5255 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5256 tpd.parms = XALLOCAVEC (int, ntparms);
5257 memset (tpd.parms, 0, sizeof (int) * ntparms);
5258
5259 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5260 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5261 for (i = 0; i < nargs; ++i)
5262 {
5263 tpd.current_arg = i;
5264 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5265 &mark_template_parm,
5266 &tpd,
5267 NULL,
5268 /*include_nondeduced_p=*/false);
5269 }
5270
5271 {
5272 auto_diagnostic_group d;
5273 bool did_error_intro = false;
5274 for (i = 0; i < ntparms; ++i)
5275 if (tpd.parms[i] == 0)
5276 {
5277 /* One of the template parms was not used in a deduced context in the
5278 specialization. */
5279 if (!did_error_intro)
5280 {
5281 error ("template parameters not deducible in "
5282 "partial specialization:");
5283 did_error_intro = true;
5284 }
5285
5286 inform (input_location, " %qD",
5287 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5288 }
5289
5290 if (did_error_intro)
5291 return error_mark_node;
5292 }
5293
5294 /* [temp.class.spec]
5295
5296 The argument list of the specialization shall not be identical to
5297 the implicit argument list of the primary template. */
5298 tree main_args
5299 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5300 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5301 && (!flag_concepts
5302 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5303 {
5304 auto_diagnostic_group d;
5305 if (!flag_concepts)
5306 error ("partial specialization %q+D does not specialize "
5307 "any template arguments; to define the primary template, "
5308 "remove the template argument list", decl);
5309 else
5310 error ("partial specialization %q+D does not specialize any "
5311 "template arguments and is not more constrained than "
5312 "the primary template; to define the primary template, "
5313 "remove the template argument list", decl);
5314 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5315 }
5316
5317 /* A partial specialization that replaces multiple parameters of the
5318 primary template with a pack expansion is less specialized for those
5319 parameters. */
5320 if (nargs < DECL_NTPARMS (maintmpl))
5321 {
5322 auto_diagnostic_group d;
5323 error ("partial specialization is not more specialized than the "
5324 "primary template because it replaces multiple parameters "
5325 "with a pack expansion");
5326 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5327 /* Avoid crash in process_partial_specialization. */
5328 return decl;
5329 }
5330
5331 else if (nargs > DECL_NTPARMS (maintmpl))
5332 {
5333 auto_diagnostic_group d;
5334 error ("too many arguments for partial specialization %qT", type);
5335 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5336 /* Avoid crash below. */
5337 return decl;
5338 }
5339
5340 /* If we aren't in a dependent class, we can actually try deduction. */
5341 else if (tpd.level == 1
5342 /* FIXME we should be able to handle a partial specialization of a
5343 partial instantiation, but currently we can't (c++/41727). */
5344 && TMPL_ARGS_DEPTH (specargs) == 1
5345 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5346 {
5347 auto_diagnostic_group d;
5348 if (pedwarn (input_location, 0,
5349 "partial specialization %qD is not more specialized than",
5350 decl))
5351 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5352 maintmpl);
5353 }
5354
5355 /* [temp.spec.partial]
5356
5357 The type of a template parameter corresponding to a specialized
5358 non-type argument shall not be dependent on a parameter of the
5359 specialization.
5360
5361 Also, we verify that pack expansions only occur at the
5362 end of the argument list. */
5363 tpd2.parms = 0;
5364 for (i = 0; i < nargs; ++i)
5365 {
5366 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5367 tree arg = TREE_VEC_ELT (inner_args, i);
5368 tree packed_args = NULL_TREE;
5369 int j, len = 1;
5370
5371 if (ARGUMENT_PACK_P (arg))
5372 {
5373 /* Extract the arguments from the argument pack. We'll be
5374 iterating over these in the following loop. */
5375 packed_args = ARGUMENT_PACK_ARGS (arg);
5376 len = TREE_VEC_LENGTH (packed_args);
5377 }
5378
5379 for (j = 0; j < len; j++)
5380 {
5381 if (packed_args)
5382 /* Get the Jth argument in the parameter pack. */
5383 arg = TREE_VEC_ELT (packed_args, j);
5384
5385 if (PACK_EXPANSION_P (arg))
5386 {
5387 /* Pack expansions must come at the end of the
5388 argument list. */
5389 if ((packed_args && j < len - 1)
5390 || (!packed_args && i < nargs - 1))
5391 {
5392 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5393 error ("parameter pack argument %qE must be at the "
5394 "end of the template argument list", arg);
5395 else
5396 error ("parameter pack argument %qT must be at the "
5397 "end of the template argument list", arg);
5398 }
5399 }
5400
5401 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5402 /* We only care about the pattern. */
5403 arg = PACK_EXPANSION_PATTERN (arg);
5404
5405 if (/* These first two lines are the `non-type' bit. */
5406 !TYPE_P (arg)
5407 && TREE_CODE (arg) != TEMPLATE_DECL
5408 /* This next two lines are the `argument expression is not just a
5409 simple identifier' condition and also the `specialized
5410 non-type argument' bit. */
5411 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5412 && !((REFERENCE_REF_P (arg)
5413 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5414 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5415 {
5416 /* Look at the corresponding template parameter,
5417 marking which template parameters its type depends
5418 upon. */
5419 tree type = TREE_TYPE (parm);
5420
5421 if (!tpd2.parms)
5422 {
5423 /* We haven't yet initialized TPD2. Do so now. */
5424 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5425 /* The number of parameters here is the number in the
5426 main template, which, as checked in the assertion
5427 above, is NARGS. */
5428 tpd2.parms = XALLOCAVEC (int, nargs);
5429 tpd2.level =
5430 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5431 }
5432
5433 /* Mark the template parameters. But this time, we're
5434 looking for the template parameters of the main
5435 template, not in the specialization. */
5436 tpd2.current_arg = i;
5437 tpd2.arg_uses_template_parms[i] = 0;
5438 memset (tpd2.parms, 0, sizeof (int) * nargs);
5439 for_each_template_parm (type,
5440 &mark_template_parm,
5441 &tpd2,
5442 NULL,
5443 /*include_nondeduced_p=*/false);
5444
5445 if (tpd2.arg_uses_template_parms [i])
5446 {
5447 /* The type depended on some template parameters.
5448 If they are fully specialized in the
5449 specialization, that's OK. */
5450 int j;
5451 int count = 0;
5452 for (j = 0; j < nargs; ++j)
5453 if (tpd2.parms[j] != 0
5454 && tpd.arg_uses_template_parms [j])
5455 ++count;
5456 if (count != 0)
5457 error_n (input_location, count,
5458 "type %qT of template argument %qE depends "
5459 "on a template parameter",
5460 "type %qT of template argument %qE depends "
5461 "on template parameters",
5462 type,
5463 arg);
5464 }
5465 }
5466 }
5467 }
5468
5469 /* We should only get here once. */
5470 if (TREE_CODE (decl) == TYPE_DECL)
5471 gcc_assert (!COMPLETE_TYPE_P (type));
5472
5473 // Build the template decl.
5474 tree tmpl = build_template_decl (decl, current_template_parms,
5475 DECL_MEMBER_TEMPLATE_P (maintmpl));
5476 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5477 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5478 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5479
5480 /* Give template template parms a DECL_CONTEXT of the template
5481 for which they are a parameter. */
5482 for (i = 0; i < ntparms; ++i)
5483 {
5484 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5485 if (TREE_CODE (parm) == TEMPLATE_DECL)
5486 DECL_CONTEXT (parm) = tmpl;
5487 }
5488
5489 if (VAR_P (decl))
5490 {
5491 /* We didn't register this in check_explicit_specialization so we could
5492 wait until the constraints were set. */
5493 tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
5494 if (reg != decl)
5495 /* Redeclaration. */
5496 return reg;
5497 }
5498 else
5499 associate_classtype_constraints (type);
5500
5501 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5502 = tree_cons (specargs, tmpl,
5503 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5504 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5505 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */
5506 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
5507 TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
5508
5509 set_defining_module_for_partial_spec (decl);
5510
5511 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5512 inst = TREE_CHAIN (inst))
5513 {
5514 tree instance = TREE_VALUE (inst);
5515 if (TYPE_P (instance)
5516 ? (COMPLETE_TYPE_P (instance)
5517 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5518 : DECL_TEMPLATE_INSTANTIATION (instance))
5519 {
5520 tree partial_ti = most_specialized_partial_spec (instance, tf_none,
5521 /*rechecking=*/true);
5522 tree inst_decl = (DECL_P (instance)
5523 ? instance : TYPE_NAME (instance));
5524 if (!partial_ti)
5525 /* OK */;
5526 else if (partial_ti == error_mark_node)
5527 permerror (input_location,
5528 "declaration of %qD ambiguates earlier template "
5529 "instantiation for %qD", decl, inst_decl);
5530 else if (TI_TEMPLATE (partial_ti) == tmpl)
5531 permerror (input_location,
5532 "partial specialization of %qD after instantiation "
5533 "of %qD", decl, inst_decl);
5534 }
5535 }
5536
5537 return decl;
5538 }
5539
5540 /* PARM is a template parameter of some form; return the corresponding
5541 TEMPLATE_PARM_INDEX. */
5542
5543 static tree
5544 get_template_parm_index (tree parm)
5545 {
5546 if (TREE_CODE (parm) == PARM_DECL
5547 || TREE_CODE (parm) == CONST_DECL)
5548 parm = DECL_INITIAL (parm);
5549 else if (TREE_CODE (parm) == TYPE_DECL
5550 || TREE_CODE (parm) == TEMPLATE_DECL)
5551 parm = TREE_TYPE (parm);
5552 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5553 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5554 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5555 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5556 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5557 return parm;
5558 }
5559
5560 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5561 parameter packs used by the template parameter PARM. */
5562
5563 static void
5564 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5565 {
5566 /* A type parm can't refer to another parm. */
5567 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5568 return;
5569 else if (TREE_CODE (parm) == PARM_DECL)
5570 {
5571 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5572 ppd, ppd->visited);
5573 return;
5574 }
5575
5576 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5577
5578 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5579 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5580 {
5581 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5582 if (template_parameter_pack_p (p))
5583 /* Any packs in the type are expanded by this parameter. */;
5584 else
5585 fixed_parameter_pack_p_1 (p, ppd);
5586 }
5587 }
5588
5589 /* PARM is a template parameter pack. Return any parameter packs used in
5590 its type or the type of any of its template parameters. If there are
5591 any such packs, it will be instantiated into a fixed template parameter
5592 list by partial instantiation rather than be fully deduced. */
5593
5594 tree
5595 fixed_parameter_pack_p (tree parm)
5596 {
5597 /* This can only be true in a member template. */
5598 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5599 return NULL_TREE;
5600 /* This can only be true for a parameter pack. */
5601 if (!template_parameter_pack_p (parm))
5602 return NULL_TREE;
5603 /* A type parm can't refer to another parm. */
5604 if (TREE_CODE (parm) == TYPE_DECL)
5605 return NULL_TREE;
5606
5607 tree parameter_packs = NULL_TREE;
5608 struct find_parameter_pack_data ppd;
5609 ppd.parameter_packs = &parameter_packs;
5610 ppd.visited = new hash_set<tree>;
5611
5612 fixed_parameter_pack_p_1 (parm, &ppd);
5613
5614 delete ppd.visited;
5615 return parameter_packs;
5616 }
5617
5618 /* Check that a template declaration's use of default arguments and
5619 parameter packs is not invalid. Here, PARMS are the template
5620 parameters. IS_PRIMARY is true if DECL is the thing declared by
5621 a primary template. IS_PARTIAL is true if DECL is a partial
5622 specialization.
5623
5624 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5625 function template declaration or a friend class template
5626 declaration. In the function case, 1 indicates a declaration, 2
5627 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5628 emitted for extraneous default arguments.
5629
5630 Returns TRUE if there were no errors found, FALSE otherwise. */
5631
5632 bool
5633 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5634 bool is_partial, int is_friend_decl)
5635 {
5636 const char *msg;
5637 int last_level_to_check;
5638 tree parm_level;
5639 bool no_errors = true;
5640
5641 /* [temp.param]
5642
5643 A default template-argument shall not be specified in a
5644 function template declaration or a function template definition, nor
5645 in the template-parameter-list of the definition of a member of a
5646 class template. */
5647
5648 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5649 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5650 /* You can't have a function template declaration in a local
5651 scope, nor you can you define a member of a class template in a
5652 local scope. */
5653 return true;
5654
5655 if ((DECL_IMPLICIT_TYPEDEF_P (decl)
5656 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5657 || (TREE_CODE (decl) == FUNCTION_DECL
5658 && LAMBDA_FUNCTION_P (decl)))
5659 /* A lambda doesn't have an explicit declaration; don't complain
5660 about the parms of the enclosing class. */
5661 return true;
5662
5663 if (current_class_type
5664 && !TYPE_BEING_DEFINED (current_class_type)
5665 && DECL_LANG_SPECIFIC (decl)
5666 && DECL_DECLARES_FUNCTION_P (decl)
5667 /* If this is either a friend defined in the scope of the class
5668 or a member function. */
5669 && (DECL_FUNCTION_MEMBER_P (decl)
5670 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5671 : DECL_FRIEND_CONTEXT (decl)
5672 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5673 : false)
5674 /* And, if it was a member function, it really was defined in
5675 the scope of the class. */
5676 && (!DECL_FUNCTION_MEMBER_P (decl)
5677 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5678 /* We already checked these parameters when the template was
5679 declared, so there's no need to do it again now. This function
5680 was defined in class scope, but we're processing its body now
5681 that the class is complete. */
5682 return true;
5683
5684 /* Core issue 226 (C++0x only): the following only applies to class
5685 templates. */
5686 if (is_primary
5687 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5688 {
5689 /* [temp.param]
5690
5691 If a template-parameter has a default template-argument, all
5692 subsequent template-parameters shall have a default
5693 template-argument supplied. */
5694 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5695 {
5696 tree inner_parms = TREE_VALUE (parm_level);
5697 int ntparms = TREE_VEC_LENGTH (inner_parms);
5698 int seen_def_arg_p = 0;
5699 int i;
5700
5701 for (i = 0; i < ntparms; ++i)
5702 {
5703 tree parm = TREE_VEC_ELT (inner_parms, i);
5704
5705 if (parm == error_mark_node)
5706 continue;
5707
5708 if (TREE_PURPOSE (parm))
5709 seen_def_arg_p = 1;
5710 else if (seen_def_arg_p
5711 && !template_parameter_pack_p (TREE_VALUE (parm)))
5712 {
5713 error ("no default argument for %qD", TREE_VALUE (parm));
5714 /* For better subsequent error-recovery, we indicate that
5715 there should have been a default argument. */
5716 TREE_PURPOSE (parm) = error_mark_node;
5717 no_errors = false;
5718 }
5719 else if (!is_partial
5720 && !is_friend_decl
5721 /* Don't complain about an enclosing partial
5722 specialization. */
5723 && parm_level == parms
5724 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5725 && i < ntparms - 1
5726 && template_parameter_pack_p (TREE_VALUE (parm))
5727 /* A fixed parameter pack will be partially
5728 instantiated into a fixed length list. */
5729 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5730 {
5731 /* A primary class template, primary variable template
5732 (DR 2032), or alias template can only have one
5733 parameter pack, at the end of the template
5734 parameter list. */
5735
5736 error ("parameter pack %q+D must be at the end of the"
5737 " template parameter list", TREE_VALUE (parm));
5738
5739 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5740 = error_mark_node;
5741 no_errors = false;
5742 }
5743 }
5744 }
5745 }
5746
5747 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5748 || is_partial
5749 || !is_primary
5750 || is_friend_decl)
5751 /* For an ordinary class template, default template arguments are
5752 allowed at the innermost level, e.g.:
5753 template <class T = int>
5754 struct S {};
5755 but, in a partial specialization, they're not allowed even
5756 there, as we have in [temp.class.spec]:
5757
5758 The template parameter list of a specialization shall not
5759 contain default template argument values.
5760
5761 So, for a partial specialization, or for a function template
5762 (in C++98/C++03), we look at all of them. */
5763 ;
5764 else
5765 /* But, for a primary class template that is not a partial
5766 specialization we look at all template parameters except the
5767 innermost ones. */
5768 parms = TREE_CHAIN (parms);
5769
5770 /* Figure out what error message to issue. */
5771 if (is_friend_decl == 2)
5772 msg = G_("default template arguments may not be used in function template "
5773 "friend re-declaration");
5774 else if (is_friend_decl)
5775 msg = G_("default template arguments may not be used in template "
5776 "friend declarations");
5777 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5778 msg = G_("default template arguments may not be used in function templates "
5779 "without %<-std=c++11%> or %<-std=gnu++11%>");
5780 else if (is_partial)
5781 msg = G_("default template arguments may not be used in "
5782 "partial specializations");
5783 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5784 msg = G_("default argument for template parameter for class enclosing %qD");
5785 else
5786 /* Per [temp.param]/9, "A default template-argument shall not be
5787 specified in the template-parameter-lists of the definition of
5788 a member of a class template that appears outside of the member's
5789 class.", thus if we aren't handling a member of a class template
5790 there is no need to examine the parameters. */
5791 return true;
5792
5793 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5794 /* If we're inside a class definition, there's no need to
5795 examine the parameters to the class itself. On the one
5796 hand, they will be checked when the class is defined, and,
5797 on the other, default arguments are valid in things like:
5798 template <class T = double>
5799 struct S { template <class U> void f(U); };
5800 Here the default argument for `S' has no bearing on the
5801 declaration of `f'. */
5802 last_level_to_check = template_class_depth (current_class_type) + 1;
5803 else
5804 /* Check everything. */
5805 last_level_to_check = 0;
5806
5807 for (parm_level = parms;
5808 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5809 parm_level = TREE_CHAIN (parm_level))
5810 {
5811 tree inner_parms = TREE_VALUE (parm_level);
5812 int i;
5813 int ntparms;
5814
5815 ntparms = TREE_VEC_LENGTH (inner_parms);
5816 for (i = 0; i < ntparms; ++i)
5817 {
5818 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5819 continue;
5820
5821 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5822 {
5823 if (msg)
5824 {
5825 no_errors = false;
5826 if (is_friend_decl == 2)
5827 return no_errors;
5828
5829 error (msg, decl);
5830 msg = 0;
5831 }
5832
5833 /* Clear out the default argument so that we are not
5834 confused later. */
5835 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5836 }
5837 }
5838
5839 /* At this point, if we're still interested in issuing messages,
5840 they must apply to classes surrounding the object declared. */
5841 if (msg)
5842 msg = G_("default argument for template parameter for class "
5843 "enclosing %qD");
5844 }
5845
5846 return no_errors;
5847 }
5848
5849 /* Worker for push_template_decl_real, called via
5850 for_each_template_parm. DATA is really an int, indicating the
5851 level of the parameters we are interested in. If T is a template
5852 parameter of that level, return nonzero. */
5853
5854 static int
5855 template_parm_this_level_p (tree t, void* data)
5856 {
5857 int this_level = *(int *)data;
5858 int level;
5859
5860 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5861 level = TEMPLATE_PARM_LEVEL (t);
5862 else
5863 level = TEMPLATE_TYPE_LEVEL (t);
5864 return level == this_level;
5865 }
5866
5867 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5868 DATA is really an int, indicating the innermost outer level of parameters.
5869 If T is a template parameter of that level or further out, return
5870 nonzero. */
5871
5872 static int
5873 template_parm_outer_level (tree t, void *data)
5874 {
5875 int this_level = *(int *)data;
5876 int level;
5877
5878 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5879 level = TEMPLATE_PARM_LEVEL (t);
5880 else
5881 level = TEMPLATE_TYPE_LEVEL (t);
5882 return level <= this_level;
5883 }
5884
5885 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5886 parameters given by current_template_args, or reuses a
5887 previously existing one, if appropriate. Returns the DECL, or an
5888 equivalent one, if it is replaced via a call to duplicate_decls.
5889
5890 If IS_FRIEND is true, DECL is a friend declaration. */
5891
5892 tree
5893 push_template_decl (tree decl, bool is_friend)
5894 {
5895 if (decl == error_mark_node || !current_template_parms)
5896 return error_mark_node;
5897
5898 /* See if this is a partial specialization. */
5899 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5900 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5901 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5902 || (VAR_P (decl)
5903 && DECL_LANG_SPECIFIC (decl)
5904 && DECL_TEMPLATE_SPECIALIZATION (decl)
5905 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5906
5907 /* No surprising friend functions. */
5908 gcc_checking_assert (is_friend
5909 || !(TREE_CODE (decl) == FUNCTION_DECL
5910 && DECL_UNIQUE_FRIEND_P (decl)));
5911
5912 tree ctx;
5913 if (is_friend)
5914 /* For a friend, we want the context of the friend, not
5915 the type of which it is a friend. */
5916 ctx = CP_DECL_CONTEXT (decl);
5917 else if (CP_DECL_CONTEXT (decl)
5918 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5919 /* In the case of a virtual function, we want the class in which
5920 it is defined. */
5921 ctx = CP_DECL_CONTEXT (decl);
5922 else
5923 /* Otherwise, if we're currently defining some class, the DECL
5924 is assumed to be a member of the class. */
5925 ctx = current_scope ();
5926
5927 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5928 ctx = NULL_TREE;
5929
5930 if (!DECL_CONTEXT (decl))
5931 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5932
5933 /* See if this is a primary template. */
5934 bool is_primary = false;
5935 if (is_friend && ctx
5936 && uses_template_parms_level (ctx, current_template_depth))
5937 /* A friend template that specifies a class context, i.e.
5938 template <typename T> friend void A<T>::f();
5939 is not primary. */
5940 ;
5941 else if (DECL_IMPLICIT_TYPEDEF_P (decl) && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5942 /* Lambdas are not primary. */
5943 ;
5944 else
5945 is_primary = template_parm_scope_p ();
5946
5947 /* True if the template is a member template, in the sense of
5948 [temp.mem]. */
5949 bool member_template_p = false;
5950
5951 if (is_primary)
5952 {
5953 warning (OPT_Wtemplates, "template %qD declared", decl);
5954
5955 if (DECL_CLASS_SCOPE_P (decl))
5956 member_template_p = true;
5957
5958 if (TREE_CODE (decl) == TYPE_DECL
5959 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5960 {
5961 error ("template class without a name");
5962 return error_mark_node;
5963 }
5964 else if (TREE_CODE (decl) == FUNCTION_DECL)
5965 {
5966 if (member_template_p)
5967 {
5968 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5969 error ("member template %qD may not have virt-specifiers", decl);
5970 }
5971 if (DECL_DESTRUCTOR_P (decl))
5972 {
5973 /* [temp.mem]
5974
5975 A destructor shall not be a member template. */
5976 error_at (DECL_SOURCE_LOCATION (decl),
5977 "destructor %qD declared as member template", decl);
5978 return error_mark_node;
5979 }
5980 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5981 && (!prototype_p (TREE_TYPE (decl))
5982 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5983 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5984 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5985 == void_list_node)))
5986 {
5987 /* [basic.stc.dynamic.allocation]
5988
5989 An allocation function can be a function
5990 template. ... Template allocation functions shall
5991 have two or more parameters. */
5992 error ("invalid template declaration of %qD", decl);
5993 return error_mark_node;
5994 }
5995 }
5996 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5997 && CLASS_TYPE_P (TREE_TYPE (decl)))
5998 /* Class template. */;
5999 else if (TREE_CODE (decl) == TYPE_DECL
6000 && TYPE_DECL_ALIAS_P (decl))
6001 /* alias-declaration */
6002 gcc_assert (!DECL_ARTIFICIAL (decl));
6003 else if (VAR_P (decl))
6004 /* C++14 variable template. */;
6005 else if (TREE_CODE (decl) == CONCEPT_DECL)
6006 /* C++20 concept definitions. */;
6007 else
6008 {
6009 error ("template declaration of %q#D", decl);
6010 return error_mark_node;
6011 }
6012 }
6013
6014 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
6015 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
6016 || (VAR_OR_FUNCTION_DECL_P (decl)
6017 && DECL_LOCAL_DECL_P (decl))));
6018
6019 /* Check to see that the rules regarding the use of default
6020 arguments are not being violated. We check args for a friend
6021 functions when we know whether it's a definition, introducing
6022 declaration or re-declaration. */
6023 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
6024 check_default_tmpl_args (decl, current_template_parms,
6025 is_primary, is_partial, is_friend);
6026
6027 /* Ensure that there are no parameter packs in the type of this
6028 declaration that have not been expanded. */
6029 if (TREE_CODE (decl) == FUNCTION_DECL)
6030 {
6031 /* Check each of the arguments individually to see if there are
6032 any bare parameter packs. */
6033 tree type = TREE_TYPE (decl);
6034 tree arg = DECL_ARGUMENTS (decl);
6035 tree argtype = TYPE_ARG_TYPES (type);
6036
6037 while (arg && argtype)
6038 {
6039 if (!DECL_PACK_P (arg)
6040 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
6041 {
6042 /* This is a PARM_DECL that contains unexpanded parameter
6043 packs. We have already complained about this in the
6044 check_for_bare_parameter_packs call, so just replace
6045 these types with ERROR_MARK_NODE. */
6046 TREE_TYPE (arg) = error_mark_node;
6047 TREE_VALUE (argtype) = error_mark_node;
6048 }
6049
6050 arg = DECL_CHAIN (arg);
6051 argtype = TREE_CHAIN (argtype);
6052 }
6053
6054 /* Check for bare parameter packs in the return type and the
6055 exception specifiers. */
6056 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
6057 /* Errors were already issued, set return type to int
6058 as the frontend doesn't expect error_mark_node as
6059 the return type. */
6060 TREE_TYPE (type) = integer_type_node;
6061 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
6062 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
6063 }
6064 else
6065 {
6066 if (check_for_bare_parameter_packs (is_typedef_decl (decl)
6067 ? DECL_ORIGINAL_TYPE (decl)
6068 : TREE_TYPE (decl)))
6069 {
6070 TREE_TYPE (decl) = error_mark_node;
6071 return error_mark_node;
6072 }
6073
6074 if (is_partial && VAR_P (decl)
6075 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
6076 return error_mark_node;
6077 }
6078
6079 if (is_partial)
6080 return process_partial_specialization (decl);
6081
6082 tree args = current_template_args ();
6083 tree tmpl = NULL_TREE;
6084 bool new_template_p = false;
6085 if (local_p)
6086 {
6087 /* Does not get a template head. */
6088 tmpl = NULL_TREE;
6089 gcc_checking_assert (!is_primary);
6090 }
6091 else if (!ctx
6092 || TREE_CODE (ctx) == FUNCTION_DECL
6093 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
6094 || (DECL_IMPLICIT_TYPEDEF_P (decl)
6095 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
6096 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
6097 && DECL_TEMPLATE_INFO (decl))))
6098 {
6099 if (DECL_LANG_SPECIFIC (decl)
6100 && DECL_TEMPLATE_INFO (decl)
6101 && DECL_TI_TEMPLATE (decl))
6102 tmpl = DECL_TI_TEMPLATE (decl);
6103 /* If DECL is a TYPE_DECL for a class-template, then there won't
6104 be DECL_LANG_SPECIFIC. The information equivalent to
6105 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6106 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
6107 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
6108 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
6109 {
6110 /* Since a template declaration already existed for this
6111 class-type, we must be redeclaring it here. Make sure
6112 that the redeclaration is valid. */
6113 redeclare_class_template (TREE_TYPE (decl),
6114 current_template_parms,
6115 current_template_constraints ());
6116 /* We don't need to create a new TEMPLATE_DECL; just use the
6117 one we already had. */
6118 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6119 }
6120 else
6121 {
6122 tmpl = build_template_decl (decl, current_template_parms,
6123 member_template_p);
6124 new_template_p = true;
6125
6126 if (DECL_LANG_SPECIFIC (decl)
6127 && DECL_TEMPLATE_SPECIALIZATION (decl))
6128 {
6129 /* A specialization of a member template of a template
6130 class. */
6131 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
6132 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
6133 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
6134 }
6135 }
6136 }
6137 else
6138 {
6139 tree a, t, current, parms;
6140 int i;
6141 tree tinfo = get_template_info (decl);
6142
6143 if (!tinfo)
6144 {
6145 error ("template definition of non-template %q#D", decl);
6146 return error_mark_node;
6147 }
6148
6149 tmpl = TI_TEMPLATE (tinfo);
6150
6151 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
6152 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
6153 && DECL_TEMPLATE_SPECIALIZATION (decl)
6154 && DECL_MEMBER_TEMPLATE_P (tmpl))
6155 {
6156 /* The declaration is a specialization of a member
6157 template, declared outside the class. Therefore, the
6158 innermost template arguments will be NULL, so we
6159 replace them with the arguments determined by the
6160 earlier call to check_explicit_specialization. */
6161 args = DECL_TI_ARGS (decl);
6162
6163 tree new_tmpl
6164 = build_template_decl (decl, current_template_parms,
6165 member_template_p);
6166 DECL_TI_TEMPLATE (decl) = new_tmpl;
6167 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
6168 DECL_TEMPLATE_INFO (new_tmpl)
6169 = build_template_info (tmpl, args);
6170
6171 register_specialization (new_tmpl,
6172 most_general_template (tmpl),
6173 args,
6174 is_friend, 0);
6175 return decl;
6176 }
6177
6178 /* Make sure the template headers we got make sense. */
6179
6180 parms = DECL_TEMPLATE_PARMS (tmpl);
6181 i = TMPL_PARMS_DEPTH (parms);
6182 if (TMPL_ARGS_DEPTH (args) != i)
6183 {
6184 error ("expected %d levels of template parms for %q#D, got %d",
6185 i, decl, TMPL_ARGS_DEPTH (args));
6186 DECL_INTERFACE_KNOWN (decl) = 1;
6187 return error_mark_node;
6188 }
6189 else
6190 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6191 {
6192 a = TMPL_ARGS_LEVEL (args, i);
6193 t = INNERMOST_TEMPLATE_PARMS (parms);
6194
6195 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6196 {
6197 if (current == decl)
6198 error ("got %d template parameters for %q#D",
6199 TREE_VEC_LENGTH (a), decl);
6200 else
6201 error ("got %d template parameters for %q#T",
6202 TREE_VEC_LENGTH (a), current);
6203 error (" but %d required", TREE_VEC_LENGTH (t));
6204 /* Avoid crash in import_export_decl. */
6205 DECL_INTERFACE_KNOWN (decl) = 1;
6206 return error_mark_node;
6207 }
6208
6209 if (current == decl)
6210 current = ctx;
6211 else if (current == NULL_TREE)
6212 /* Can happen in erroneous input. */
6213 break;
6214 else
6215 current = get_containing_scope (current);
6216 }
6217
6218 /* Check that the parms are used in the appropriate qualifying scopes
6219 in the declarator. */
6220 if (!comp_template_args
6221 (TI_ARGS (tinfo),
6222 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6223 {
6224 auto_diagnostic_group d;
6225 error ("template arguments to %qD do not match original "
6226 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6227 if (!uses_template_parms (TI_ARGS (tinfo)))
6228 inform (input_location, "use %<template<>%> for"
6229 " an explicit specialization");
6230 /* Avoid crash in import_export_decl. */
6231 DECL_INTERFACE_KNOWN (decl) = 1;
6232 return error_mark_node;
6233 }
6234
6235 /* Check that the constraints for each enclosing template scope are
6236 consistent with the original declarations. */
6237 if (flag_concepts)
6238 {
6239 tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
6240 tree scope_parms = current_template_parms;
6241 if (PRIMARY_TEMPLATE_P (tmpl))
6242 {
6243 decl_parms = TREE_CHAIN (decl_parms);
6244 scope_parms = TREE_CHAIN (scope_parms);
6245 }
6246 while (decl_parms)
6247 {
6248 if (!template_requirements_equivalent_p (decl_parms, scope_parms))
6249 {
6250 error ("redeclaration of %qD with different constraints",
6251 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
6252 break;
6253 }
6254 decl_parms = TREE_CHAIN (decl_parms);
6255 scope_parms = TREE_CHAIN (scope_parms);
6256 }
6257 }
6258 }
6259
6260 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6261
6262 if (new_template_p)
6263 {
6264 /* Push template declarations for global functions and types.
6265 Note that we do not try to push a global template friend
6266 declared in a template class; such a thing may well depend on
6267 the template parameters of the class and we'll push it when
6268 instantiating the befriending class. */
6269 if (!ctx
6270 && !(is_friend && template_class_depth (current_class_type) > 0))
6271 {
6272 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6273 if (pushed == error_mark_node)
6274 return error_mark_node;
6275
6276 /* pushdecl may have found an existing template. */
6277 if (pushed != tmpl)
6278 {
6279 decl = DECL_TEMPLATE_RESULT (pushed);
6280 tmpl = NULL_TREE;
6281 }
6282 }
6283 else if (is_friend)
6284 {
6285 /* Record this decl as belonging to the current class. It's
6286 not chained onto anything else. */
6287 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6288 gcc_checking_assert (!DECL_CHAIN (tmpl));
6289 DECL_CHAIN (tmpl) = current_scope ();
6290 }
6291 }
6292 else if (tmpl)
6293 /* The type may have been completed, or (erroneously) changed. */
6294 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6295
6296 if (tmpl)
6297 {
6298 if (is_primary)
6299 {
6300 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6301
6302 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6303
6304 /* Give template template parms a DECL_CONTEXT of the template
6305 for which they are a parameter. */
6306 parms = INNERMOST_TEMPLATE_PARMS (parms);
6307 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6308 {
6309 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6310 if (TREE_CODE (parm) == TEMPLATE_DECL)
6311 DECL_CONTEXT (parm) = tmpl;
6312 }
6313
6314 if (TREE_CODE (decl) == TYPE_DECL
6315 && TYPE_DECL_ALIAS_P (decl))
6316 {
6317 if (tree constr
6318 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6319 {
6320 /* ??? Why don't we do this here for all templates? */
6321 constr = build_constraints (constr, NULL_TREE);
6322 set_constraints (decl, constr);
6323 }
6324 }
6325 }
6326
6327 /* The DECL_TI_ARGS of DECL contains full set of arguments
6328 referring wback to its most general template. If TMPL is a
6329 specialization, ARGS may only have the innermost set of
6330 arguments. Add the missing argument levels if necessary. */
6331 if (DECL_TEMPLATE_INFO (tmpl))
6332 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6333
6334 tree info = build_template_info (tmpl, args);
6335
6336 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6337 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6338 else
6339 {
6340 retrofit_lang_decl (decl);
6341 DECL_TEMPLATE_INFO (decl) = info;
6342 }
6343 }
6344
6345 if (is_typedef_decl (decl)
6346 && (dependent_opaque_alias_p (TREE_TYPE (decl))
6347 || dependent_alias_template_spec_p (TREE_TYPE (decl), nt_opaque)))
6348 {
6349 /* Manually mark such aliases as dependent so that dependent_type_p_r
6350 doesn't have to handle them. */
6351 TYPE_DEPENDENT_P_VALID (TREE_TYPE (decl)) = true;
6352 TYPE_DEPENDENT_P (TREE_TYPE (decl)) = true;
6353 /* The identity of such aliases is hairy; see structural_comptypes. */
6354 SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (decl));
6355 }
6356
6357 if (flag_implicit_templates
6358 && !is_friend
6359 && TREE_PUBLIC (decl)
6360 && VAR_OR_FUNCTION_DECL_P (decl))
6361 /* Set DECL_COMDAT on template instantiations; if we force
6362 them to be emitted by explicit instantiation,
6363 mark_needed will tell cgraph to do the right thing. */
6364 DECL_COMDAT (decl) = true;
6365
6366 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6367
6368 return decl;
6369 }
6370
6371 /* FN is an inheriting constructor that inherits from the constructor
6372 template INHERITED; turn FN into a constructor template with a matching
6373 template header. */
6374
6375 tree
6376 add_inherited_template_parms (tree fn, tree inherited)
6377 {
6378 tree inner_parms
6379 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6380 inner_parms = copy_node (inner_parms);
6381 tree parms
6382 = tree_cons (size_int (current_template_depth + 1),
6383 inner_parms, current_template_parms);
6384 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6385 tree args = template_parms_to_args (parms);
6386 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6387 DECL_ARTIFICIAL (tmpl) = true;
6388 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6389 return tmpl;
6390 }
6391
6392 /* Called when a class template TYPE is redeclared with the indicated
6393 template PARMS, e.g.:
6394
6395 template <class T> struct S;
6396 template <class T> struct S {}; */
6397
6398 bool
6399 redeclare_class_template (tree type, tree parms, tree cons)
6400 {
6401 tree tmpl;
6402 tree tmpl_parms;
6403 int i;
6404
6405 if (!TYPE_TEMPLATE_INFO (type))
6406 {
6407 error ("%qT is not a template type", type);
6408 return false;
6409 }
6410
6411 tmpl = TYPE_TI_TEMPLATE (type);
6412 if (!PRIMARY_TEMPLATE_P (tmpl))
6413 /* The type is nested in some template class. Nothing to worry
6414 about here; there are no new template parameters for the nested
6415 type. */
6416 return true;
6417
6418 if (!parms)
6419 {
6420 error ("template specifiers not specified in declaration of %qD",
6421 tmpl);
6422 return false;
6423 }
6424
6425 parms = INNERMOST_TEMPLATE_PARMS (parms);
6426 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6427
6428 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6429 {
6430 auto_diagnostic_group d;
6431 error_n (input_location, TREE_VEC_LENGTH (parms),
6432 "redeclared with %d template parameter",
6433 "redeclared with %d template parameters",
6434 TREE_VEC_LENGTH (parms));
6435 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6436 "previous declaration %qD used %d template parameter",
6437 "previous declaration %qD used %d template parameters",
6438 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6439 return false;
6440 }
6441
6442 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6443 {
6444 tree tmpl_parm;
6445 tree parm;
6446
6447 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6448 || TREE_VEC_ELT (parms, i) == error_mark_node)
6449 continue;
6450
6451 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6452 if (error_operand_p (tmpl_parm))
6453 return false;
6454
6455 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6456
6457 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6458 TEMPLATE_DECL. */
6459 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6460 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6461 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6462 || (TREE_CODE (tmpl_parm) != PARM_DECL
6463 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6464 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6465 || (TREE_CODE (tmpl_parm) == PARM_DECL
6466 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6467 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6468 {
6469 auto_diagnostic_group d;
6470 error ("template parameter %q+#D", tmpl_parm);
6471 if (DECL_P (parm))
6472 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
6473 else
6474 inform (input_location, "redeclared here");
6475 return false;
6476 }
6477
6478 /* The parameters can be declared to introduce different
6479 constraints. */
6480 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6481 tree p2 = TREE_VEC_ELT (parms, i);
6482 if (!template_parameter_constraints_equivalent_p (p1, p2))
6483 {
6484 auto_diagnostic_group d;
6485 error ("declaration of template parameter %q+#D with different "
6486 "constraints", parm);
6487 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6488 "original declaration appeared here");
6489 return false;
6490 }
6491
6492 /* Give each template template parm in this redeclaration a
6493 DECL_CONTEXT of the template for which they are a parameter. */
6494 if (TREE_CODE (parm) == TEMPLATE_DECL)
6495 {
6496 gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
6497 || DECL_CONTEXT (parm) == tmpl);
6498 DECL_CONTEXT (parm) = tmpl;
6499 }
6500 }
6501
6502 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
6503 return false;
6504
6505 tree ci = get_constraints (tmpl);
6506 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6507 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6508
6509 /* Two classes with different constraints declare different entities. */
6510 if (!cp_tree_equal (req1, req2))
6511 {
6512 auto_diagnostic_group d;
6513 error_at (input_location, "redeclaration of %q#D with different "
6514 "constraints", tmpl);
6515 inform (DECL_SOURCE_LOCATION (tmpl),
6516 "original declaration appeared here");
6517 return false;
6518 }
6519
6520 return true;
6521 }
6522
6523 /* The actual substitution part of instantiate_non_dependent_expr,
6524 to be used when the caller has already checked
6525 !instantiation_dependent_uneval_expression_p (expr)
6526 and cleared processing_template_decl. */
6527
6528 tree
6529 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6530 {
6531 return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
6532 }
6533
6534 /* Instantiate the non-dependent expression EXPR. */
6535
6536 tree
6537 instantiate_non_dependent_expr (tree expr,
6538 tsubst_flags_t complain /* = tf_error */)
6539 {
6540 if (expr == NULL_TREE)
6541 return NULL_TREE;
6542
6543 if (processing_template_decl)
6544 {
6545 /* The caller should have checked this already. */
6546 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
6547 processing_template_decl_sentinel s;
6548 expr = instantiate_non_dependent_expr_internal (expr, complain);
6549 }
6550 return expr;
6551 }
6552
6553 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6554 expression is dependent or non-constant. */
6555
6556 tree
6557 instantiate_non_dependent_or_null (tree expr)
6558 {
6559 if (expr == NULL_TREE)
6560 return NULL_TREE;
6561 if (processing_template_decl)
6562 {
6563 if (!is_nondependent_constant_expression (expr))
6564 expr = NULL_TREE;
6565 else
6566 {
6567 processing_template_decl_sentinel s;
6568 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6569 }
6570 }
6571 return expr;
6572 }
6573
6574 /* True iff T is a specialization of a variable template. */
6575
6576 bool
6577 variable_template_specialization_p (tree t)
6578 {
6579 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6580 return false;
6581 tree tmpl = DECL_TI_TEMPLATE (t);
6582 return variable_template_p (tmpl);
6583 }
6584
6585 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6586 template declaration, or a TYPE_DECL for an alias declaration. */
6587
6588 bool
6589 alias_type_or_template_p (tree t)
6590 {
6591 if (t == NULL_TREE)
6592 return false;
6593 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6594 || (TYPE_P (t)
6595 && TYPE_NAME (t)
6596 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6597 || DECL_ALIAS_TEMPLATE_P (t));
6598 }
6599
6600 /* If T is a specialization of an alias template, return it; otherwise return
6601 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6602
6603 tree
6604 alias_template_specialization_p (const_tree t,
6605 bool transparent_typedefs)
6606 {
6607 if (!TYPE_P (t))
6608 return NULL_TREE;
6609
6610 /* It's an alias template specialization if it's an alias and its
6611 TYPE_NAME is a specialization of a primary template. */
6612 if (typedef_variant_p (t))
6613 {
6614 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6615 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6616 return CONST_CAST_TREE (t);
6617 if (transparent_typedefs && !dependent_opaque_alias_p (t))
6618 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6619 (TYPE_NAME (t)),
6620 transparent_typedefs);
6621 }
6622
6623 return NULL_TREE;
6624 }
6625
6626 /* A cache of the result of complex_alias_template_p. */
6627
6628 static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
6629
6630 /* Data structure for complex_alias_template_*. */
6631
6632 struct uses_all_template_parms_data
6633 {
6634 int level;
6635 tree *seen;
6636 };
6637
6638 /* walk_tree callback for complex_alias_template_p. */
6639
6640 static tree
6641 complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
6642 {
6643 tree t = *tp;
6644 auto &data = *(struct uses_all_template_parms_data*)data_;
6645
6646 switch (TREE_CODE (t))
6647 {
6648 case TEMPLATE_TYPE_PARM:
6649 case TEMPLATE_PARM_INDEX:
6650 case TEMPLATE_TEMPLATE_PARM:
6651 case BOUND_TEMPLATE_TEMPLATE_PARM:
6652 {
6653 tree idx = get_template_parm_index (t);
6654 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6655 data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
6656 }
6657
6658 default:;
6659 }
6660
6661 if (!PACK_EXPANSION_P (t))
6662 return 0;
6663
6664 /* An alias template with a pack expansion that expands a pack from the
6665 enclosing class needs to be considered complex, to avoid confusion with
6666 the same pack being used as an argument to the alias's own template
6667 parameter (91966). */
6668 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6669 pack = TREE_CHAIN (pack))
6670 {
6671 tree parm_pack = TREE_VALUE (pack);
6672 if (!TEMPLATE_PARM_P (parm_pack))
6673 continue;
6674 int idx, level;
6675 template_parm_level_and_index (parm_pack, &level, &idx);
6676 if (level < data.level)
6677 return t;
6678
6679 /* Consider the expanded packs to be used outside the expansion... */
6680 data.seen[idx] = boolean_true_node;
6681 }
6682
6683 /* ...but don't walk into the pattern. Consider PR104008:
6684
6685 template <typename T, typename... Ts>
6686 using IsOneOf = disjunction<is_same<T, Ts>...>;
6687
6688 where IsOneOf seemingly uses all of its template parameters in its
6689 expansion (and does not expand a pack from the enclosing class), so the
6690 alias was not marked as complex. However, if it is used like
6691 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6692 expansion. So only Ts is considered used by the pack expansion. */
6693 *walk_subtrees = false;
6694
6695 return 0;
6696 }
6697
6698 /* An alias template is complex from a SFINAE perspective if a template-id
6699 using that alias can be ill-formed when the expansion is not, as with
6700 the void_t template.
6701
6702 If this predicate returns true in the ordinary case, the out parameter
6703 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
6704 the I'th template parameter of the alias template is used in the alias. */
6705
6706 static bool
6707 complex_alias_template_p (const_tree tmpl, tree *seen_out)
6708 {
6709 tmpl = most_general_template (tmpl);
6710 if (!PRIMARY_TEMPLATE_P (tmpl))
6711 return false;
6712
6713 /* A renaming alias isn't complex. */
6714 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6715 return false;
6716
6717 /* Any other constrained alias is complex. */
6718 if (get_constraints (tmpl))
6719 return true;
6720
6721 /* An alias with dependent type attributes is complex. */
6722 if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
6723 return true;
6724
6725 if (!complex_alias_tmpl_info)
6726 complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
6727
6728 if (tree *slot = complex_alias_tmpl_info->get (tmpl))
6729 {
6730 tree result = *slot;
6731 if (result == boolean_false_node)
6732 return false;
6733 if (result == boolean_true_node)
6734 return true;
6735 gcc_assert (TREE_CODE (result) == TREE_VEC);
6736 if (seen_out)
6737 *seen_out = result;
6738 return true;
6739 }
6740
6741 struct uses_all_template_parms_data data;
6742 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6743 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6744 data.level = TMPL_PARMS_DEPTH (parms);
6745 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6746 tree seen = make_tree_vec (len);
6747 data.seen = TREE_VEC_BEGIN (seen);
6748 for (int i = 0; i < len; ++i)
6749 data.seen[i] = boolean_false_node;
6750
6751 if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
6752 {
6753 complex_alias_tmpl_info->put (tmpl, boolean_true_node);
6754 return true;
6755 }
6756
6757 for (int i = 0; i < len; ++i)
6758 if (data.seen[i] != boolean_true_node)
6759 {
6760 complex_alias_tmpl_info->put (tmpl, seen);
6761 if (seen_out)
6762 *seen_out = seen;
6763 return true;
6764 }
6765
6766 complex_alias_tmpl_info->put (tmpl, boolean_false_node);
6767 return false;
6768 }
6769
6770 /* If T is a specialization of a complex alias template with a dependent
6771 argument for an unused template parameter, return it; otherwise return
6772 NULL_TREE. If T is a typedef to such a specialization, return the
6773 specialization. This predicate is usually checked alongside
6774 dependent_opaque_alias_p. Whereas dependent_opaque_alias_p checks
6775 type equivalence of an alias vs its expansion, this predicate more
6776 broadly checks SFINAE equivalence. */
6777
6778 tree
6779 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6780 {
6781 if (t == error_mark_node)
6782 return NULL_TREE;
6783 gcc_assert (TYPE_P (t));
6784
6785 if (!processing_template_decl || !typedef_variant_p (t))
6786 return NULL_TREE;
6787
6788 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6789 {
6790 tree seen = NULL_TREE;
6791 if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
6792 {
6793 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6794 if (!seen)
6795 {
6796 if (any_dependent_template_arguments_p (args))
6797 return CONST_CAST_TREE (t);
6798 }
6799 else
6800 {
6801 gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
6802 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
6803 if (TREE_VEC_ELT (seen, i) != boolean_true_node
6804 && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
6805 return CONST_CAST_TREE (t);
6806 }
6807
6808 return NULL_TREE;
6809 }
6810 }
6811
6812 if (transparent_typedefs && !dependent_opaque_alias_p (t))
6813 {
6814 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6815 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6816 }
6817
6818 return NULL_TREE;
6819 }
6820
6821 /* Return true if substituting into T would yield a different type than
6822 substituting into its expansion. This predicate is usually checked
6823 alongside dependent_alias_template_spec_p. */
6824
6825 bool
6826 dependent_opaque_alias_p (const_tree t)
6827 {
6828 auto any_lambda_targ_p = [] (tree args)
6829 {
6830 for (tree arg : tree_vec_range (args))
6831 if (TREE_CODE (arg) == LAMBDA_EXPR)
6832 return true;
6833 return false;
6834 };
6835
6836 return (TYPE_P (t)
6837 && typedef_variant_p (t)
6838 && (any_dependent_type_attributes_p (DECL_ATTRIBUTES
6839 (TYPE_NAME (t)))
6840 /* Treat a dependent decltype(lambda) alias as opaque so that we
6841 don't prematurely strip it when used as a template argument.
6842 Otherwise substitution into each occurrence of the (stripped)
6843 alias would incorrectly yield a distinct lambda type. */
6844 || (TREE_CODE (t) == DECLTYPE_TYPE
6845 && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == LAMBDA_EXPR
6846 && !typedef_variant_p (DECL_ORIGINAL_TYPE (TYPE_NAME (t))))
6847 /* Also treat an alias to A<lambda> as opaque so that it doesn't
6848 "leak" into a deeper template context which would cause us to
6849 over substitute into the lambda. */
6850 /* FIXME These lambda checks don't recognize deeply nested lambda
6851 subexpressions, and we can't use walk_tree here because it's
6852 slow. Maybe a tree flag indicating typedef opaqueness? */
6853 || (TYPE_TEMPLATE_INFO (t)
6854 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
6855 && any_lambda_targ_p (INNERMOST_TEMPLATE_ARGS
6856 (TYPE_TI_ARGS (t))))));
6857 }
6858
6859 /* Return the number of innermost template parameters in TMPL. */
6860
6861 static int
6862 num_innermost_template_parms (const_tree tmpl)
6863 {
6864 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6865 return TREE_VEC_LENGTH (parms);
6866 }
6867
6868 /* Return either TMPL or another template that it is equivalent to under DR
6869 1286: An alias that just changes the name of a template is equivalent to
6870 the other template. */
6871
6872 static tree
6873 get_underlying_template (tree tmpl)
6874 {
6875 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6876 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6877 {
6878 /* Determine if the alias is equivalent to an underlying template. */
6879 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6880 /* The underlying type may have been ill-formed. Don't proceed. */
6881 if (!orig_type)
6882 break;
6883 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6884 if (!tinfo)
6885 break;
6886
6887 tree underlying = TI_TEMPLATE (tinfo);
6888 if (!PRIMARY_TEMPLATE_P (underlying)
6889 || (num_innermost_template_parms (tmpl)
6890 != num_innermost_template_parms (underlying)))
6891 break;
6892
6893 /* Does the alias add cv-quals? */
6894 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6895 break;
6896
6897 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6898 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6899 break;
6900
6901 /* Are any default template arguments equivalent? */
6902 tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6903 tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
6904 const int nparms = TREE_VEC_LENGTH (aparms);
6905 for (int i = 0; i < nparms; ++i)
6906 {
6907 tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
6908 tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
6909 if (!template_args_equal (adefarg, udefarg))
6910 goto top_break;
6911 }
6912
6913 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6914 it's appropriate to treat a less-constrained alias as equivalent. */
6915 if (!at_least_as_constrained (underlying, tmpl))
6916 break;
6917
6918 /* If TMPL adds dependent type attributes, it isn't equivalent. */
6919 if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
6920 break;
6921
6922 /* Alias is equivalent. Strip it and repeat. */
6923 tmpl = underlying;
6924 }
6925 top_break:;
6926
6927 return tmpl;
6928 }
6929
6930 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6931 must be a reference-to-function or a pointer-to-function type, as specified
6932 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6933 and check that the resulting function has external linkage. */
6934
6935 static tree
6936 convert_nontype_argument_function (tree type, tree expr,
6937 tsubst_flags_t complain)
6938 {
6939 tree fns = expr;
6940 tree fn, fn_no_ptr;
6941 linkage_kind linkage;
6942
6943 fn = instantiate_type (type, fns, tf_none);
6944 if (fn == error_mark_node)
6945 return error_mark_node;
6946
6947 if (value_dependent_expression_p (fn))
6948 goto accept;
6949
6950 fn_no_ptr = fn;
6951 if (REFERENCE_REF_P (fn_no_ptr))
6952 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6953 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6954 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6955 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6956 if (BASELINK_P (fn_no_ptr))
6957 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6958
6959 /* [temp.arg.nontype]/1
6960
6961 A template-argument for a non-type, non-template template-parameter
6962 shall be one of:
6963 [...]
6964 -- the address of an object or function with external [C++11: or
6965 internal] linkage. */
6966
6967 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6968 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6969 {
6970 if (complain & tf_error)
6971 {
6972 auto_diagnostic_group d;
6973 location_t loc = cp_expr_loc_or_input_loc (expr);
6974 error_at (loc, "%qE is not a valid template argument for type %qT",
6975 expr, type);
6976 if (TYPE_PTR_P (type))
6977 inform (loc, "it must be the address of a function "
6978 "with external linkage");
6979 else
6980 inform (loc, "it must be the name of a function with "
6981 "external linkage");
6982 }
6983 return NULL_TREE;
6984 }
6985
6986 linkage = decl_linkage (fn_no_ptr);
6987 if ((cxx_dialect < cxx11 && linkage != lk_external)
6988 || (cxx_dialect < cxx17 && linkage == lk_none))
6989 {
6990 if (complain & tf_error)
6991 {
6992 location_t loc = cp_expr_loc_or_input_loc (expr);
6993 if (cxx_dialect >= cxx11)
6994 error_at (loc, "%qE is not a valid template argument for type "
6995 "%qT because %qD has no linkage",
6996 expr, type, fn_no_ptr);
6997 else
6998 error_at (loc, "%qE is not a valid template argument for type "
6999 "%qT because %qD does not have external linkage",
7000 expr, type, fn_no_ptr);
7001 }
7002 return NULL_TREE;
7003 }
7004
7005 accept:
7006 if (TYPE_REF_P (type))
7007 {
7008 if (REFERENCE_REF_P (fn))
7009 fn = TREE_OPERAND (fn, 0);
7010 else
7011 fn = build_address (fn);
7012 }
7013 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
7014 fn = build_nop (type, fn);
7015
7016 return fn;
7017 }
7018
7019 /* Subroutine of convert_nontype_argument.
7020 Check if EXPR of type TYPE is a valid pointer-to-member constant.
7021 Emit an error otherwise. */
7022
7023 static bool
7024 check_valid_ptrmem_cst_expr (tree type, tree expr,
7025 tsubst_flags_t complain)
7026 {
7027 tree orig_expr = expr;
7028 STRIP_NOPS (expr);
7029 if (null_ptr_cst_p (expr))
7030 return true;
7031 if (TREE_CODE (expr) == PTRMEM_CST
7032 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
7033 PTRMEM_CST_CLASS (expr)))
7034 return true;
7035 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
7036 return true;
7037 if (processing_template_decl
7038 && TREE_CODE (expr) == ADDR_EXPR
7039 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
7040 return true;
7041 if (complain & tf_error)
7042 {
7043 auto_diagnostic_group d;
7044 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
7045 error_at (loc, "%qE is not a valid template argument for type %qT",
7046 orig_expr, type);
7047 if (TREE_CODE (expr) != PTRMEM_CST)
7048 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
7049 else
7050 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
7051 }
7052 return false;
7053 }
7054
7055 /* Returns TRUE iff the address of OP is value-dependent.
7056
7057 14.6.2.4 [temp.dep.temp]:
7058 A non-integral non-type template-argument is dependent if its type is
7059 dependent or it has either of the following forms
7060 qualified-id
7061 & qualified-id
7062 and contains a nested-name-specifier which specifies a class-name that
7063 names a dependent type.
7064
7065 We generalize this to just say that the address of a member of a
7066 dependent class is value-dependent; the above doesn't cover the
7067 address of a static data member named with an unqualified-id. */
7068
7069 static bool
7070 has_value_dependent_address (tree op)
7071 {
7072 STRIP_ANY_LOCATION_WRAPPER (op);
7073
7074 /* We could use get_inner_reference here, but there's no need;
7075 this is only relevant for template non-type arguments, which
7076 can only be expressed as &id-expression. */
7077 if (DECL_P (op))
7078 {
7079 tree ctx = CP_DECL_CONTEXT (op);
7080
7081 if (TYPE_P (ctx) && dependent_type_p (ctx))
7082 return true;
7083
7084 if (VAR_P (op)
7085 && TREE_STATIC (op)
7086 && TREE_CODE (ctx) == FUNCTION_DECL
7087 && DECL_TEMPLATE_INFO (ctx)
7088 && any_dependent_template_arguments_p (DECL_TI_ARGS (ctx)))
7089 return true;
7090 }
7091
7092 return false;
7093 }
7094
7095 /* The next set of functions are used for providing helpful explanatory
7096 diagnostics for failed overload resolution. Their messages should be
7097 indented by two spaces for consistency with the messages in
7098 call.cc */
7099
7100 static int
7101 unify_success (bool /*explain_p*/)
7102 {
7103 return 0;
7104 }
7105
7106 /* Other failure functions should call this one, to provide a single function
7107 for setting a breakpoint on. */
7108
7109 static int
7110 unify_invalid (bool /*explain_p*/)
7111 {
7112 return 1;
7113 }
7114
7115 static int
7116 unify_parameter_deduction_failure (bool explain_p, tree parm)
7117 {
7118 if (explain_p)
7119 inform (input_location,
7120 " couldn%'t deduce template parameter %qD", parm);
7121 return unify_invalid (explain_p);
7122 }
7123
7124 static int
7125 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
7126 {
7127 if (explain_p)
7128 inform (input_location,
7129 " types %qT and %qT have incompatible cv-qualifiers",
7130 parm, arg);
7131 return unify_invalid (explain_p);
7132 }
7133
7134 static int
7135 unify_type_mismatch (bool explain_p, tree parm, tree arg)
7136 {
7137 if (explain_p)
7138 inform (input_location, " mismatched types %qT and %qT", parm, arg);
7139 return unify_invalid (explain_p);
7140 }
7141
7142 static int
7143 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
7144 {
7145 if (explain_p)
7146 inform (input_location,
7147 " template parameter %qD is not a parameter pack, but "
7148 "argument %qD is",
7149 parm, arg);
7150 return unify_invalid (explain_p);
7151 }
7152
7153 static int
7154 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
7155 {
7156 if (explain_p)
7157 inform (input_location,
7158 " template argument %qE does not match "
7159 "pointer-to-member constant %qE",
7160 arg, parm);
7161 return unify_invalid (explain_p);
7162 }
7163
7164 static int
7165 unify_expression_unequal (bool explain_p, tree parm, tree arg)
7166 {
7167 if (explain_p)
7168 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
7169 return unify_invalid (explain_p);
7170 }
7171
7172 static int
7173 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
7174 {
7175 if (explain_p)
7176 inform (input_location,
7177 " inconsistent parameter pack deduction with %qT and %qT",
7178 old_arg, new_arg);
7179 return unify_invalid (explain_p);
7180 }
7181
7182 static int
7183 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
7184 {
7185 if (explain_p)
7186 {
7187 if (TYPE_P (parm))
7188 inform (input_location,
7189 " deduced conflicting types for parameter %qT (%qT and %qT)",
7190 parm, first, second);
7191 else
7192 inform (input_location,
7193 " deduced conflicting values for non-type parameter "
7194 "%qE (%qE and %qE)", parm, first, second);
7195 }
7196 return unify_invalid (explain_p);
7197 }
7198
7199 static int
7200 unify_vla_arg (bool explain_p, tree arg)
7201 {
7202 if (explain_p)
7203 inform (input_location,
7204 " variable-sized array type %qT is not "
7205 "a valid template argument",
7206 arg);
7207 return unify_invalid (explain_p);
7208 }
7209
7210 static int
7211 unify_method_type_error (bool explain_p, tree arg)
7212 {
7213 if (explain_p)
7214 inform (input_location,
7215 " member function type %qT is not a valid template argument",
7216 arg);
7217 return unify_invalid (explain_p);
7218 }
7219
7220 static int
7221 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
7222 {
7223 if (explain_p)
7224 {
7225 if (least_p)
7226 inform_n (input_location, wanted,
7227 " candidate expects at least %d argument, %d provided",
7228 " candidate expects at least %d arguments, %d provided",
7229 wanted, have);
7230 else
7231 inform_n (input_location, wanted,
7232 " candidate expects %d argument, %d provided",
7233 " candidate expects %d arguments, %d provided",
7234 wanted, have);
7235 }
7236 return unify_invalid (explain_p);
7237 }
7238
7239 static int
7240 unify_too_many_arguments (bool explain_p, int have, int wanted)
7241 {
7242 return unify_arity (explain_p, have, wanted);
7243 }
7244
7245 static int
7246 unify_too_few_arguments (bool explain_p, int have, int wanted,
7247 bool least_p = false)
7248 {
7249 return unify_arity (explain_p, have, wanted, least_p);
7250 }
7251
7252 static int
7253 unify_arg_conversion (bool explain_p, tree to_type,
7254 tree from_type, tree arg)
7255 {
7256 if (explain_p)
7257 inform (cp_expr_loc_or_input_loc (arg),
7258 " cannot convert %qE (type %qT) to type %qT",
7259 arg, from_type, to_type);
7260 return unify_invalid (explain_p);
7261 }
7262
7263 static int
7264 unify_no_common_base (bool explain_p, enum template_base_result r,
7265 tree parm, tree arg)
7266 {
7267 if (explain_p)
7268 switch (r)
7269 {
7270 case tbr_ambiguous_baseclass:
7271 inform (input_location, " %qT is an ambiguous base class of %qT",
7272 parm, arg);
7273 break;
7274 default:
7275 inform (input_location, " %qT is not derived from %qT", arg, parm);
7276 break;
7277 }
7278 return unify_invalid (explain_p);
7279 }
7280
7281 static int
7282 unify_inconsistent_template_template_parameters (bool explain_p)
7283 {
7284 if (explain_p)
7285 inform (input_location,
7286 " template parameters of a template template argument are "
7287 "inconsistent with other deduced template arguments");
7288 return unify_invalid (explain_p);
7289 }
7290
7291 static int
7292 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
7293 {
7294 if (explain_p)
7295 inform (input_location,
7296 " cannot deduce a template for %qT from non-template type %qT",
7297 parm, arg);
7298 return unify_invalid (explain_p);
7299 }
7300
7301 static int
7302 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
7303 {
7304 if (explain_p)
7305 inform (input_location,
7306 " template argument %qE does not match %qE", arg, parm);
7307 return unify_invalid (explain_p);
7308 }
7309
7310 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7311 argument for TYPE, points to an unsuitable object.
7312
7313 Also adjust the type of the index in C++20 array subobject references. */
7314
7315 static bool
7316 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7317 {
7318 switch (TREE_CODE (expr))
7319 {
7320 CASE_CONVERT:
7321 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7322 complain);
7323
7324 case TARGET_EXPR:
7325 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7326 complain);
7327
7328 case CONSTRUCTOR:
7329 {
7330 for (auto &e: CONSTRUCTOR_ELTS (expr))
7331 if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
7332 return true;
7333 }
7334 break;
7335
7336 case ADDR_EXPR:
7337 {
7338 tree decl = TREE_OPERAND (expr, 0);
7339
7340 if (cxx_dialect >= cxx20)
7341 while (TREE_CODE (decl) == COMPONENT_REF
7342 || TREE_CODE (decl) == ARRAY_REF)
7343 {
7344 tree &op = TREE_OPERAND (decl, 1);
7345 if (TREE_CODE (decl) == ARRAY_REF
7346 && TREE_CODE (op) == INTEGER_CST)
7347 /* Canonicalize array offsets to ptrdiff_t; how they were
7348 written doesn't matter for subobject identity. */
7349 op = fold_convert (ptrdiff_type_node, op);
7350 decl = TREE_OPERAND (decl, 0);
7351 }
7352
7353 if (!VAR_OR_FUNCTION_DECL_P (decl))
7354 {
7355 if (complain & tf_error)
7356 error_at (cp_expr_loc_or_input_loc (expr),
7357 "%qE is not a valid template argument of type %qT "
7358 "because %qE is not a variable or function",
7359 expr, type, decl);
7360 return true;
7361 }
7362 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7363 {
7364 if (complain & tf_error)
7365 error_at (cp_expr_loc_or_input_loc (expr),
7366 "%qE is not a valid template argument of type %qT "
7367 "in C++98 because %qD does not have external linkage",
7368 expr, type, decl);
7369 return true;
7370 }
7371 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7372 && decl_linkage (decl) == lk_none)
7373 {
7374 if (complain & tf_error)
7375 error_at (cp_expr_loc_or_input_loc (expr),
7376 "%qE is not a valid template argument of type %qT "
7377 "because %qD has no linkage", expr, type, decl);
7378 return true;
7379 }
7380 /* C++17: For a non-type template-parameter of reference or pointer
7381 type, the value of the constant expression shall not refer to (or
7382 for a pointer type, shall not be the address of):
7383 * a subobject (4.5),
7384 * a temporary object (15.2),
7385 * a string literal (5.13.5),
7386 * the result of a typeid expression (8.2.8), or
7387 * a predefined __func__ variable (11.4.1). */
7388 else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
7389 && !DECL_NTTP_OBJECT_P (decl))
7390 {
7391 gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl));
7392 if (complain & tf_error)
7393 error ("the address of %qD is not a valid template argument",
7394 decl);
7395 return true;
7396 }
7397 else if (cxx_dialect < cxx20
7398 && !(same_type_ignoring_top_level_qualifiers_p
7399 (strip_array_types (TREE_TYPE (type)),
7400 strip_array_types (TREE_TYPE (decl)))))
7401 {
7402 if (complain & tf_error)
7403 error ("the address of the %qT subobject of %qD is not a "
7404 "valid template argument", TREE_TYPE (type), decl);
7405 return true;
7406 }
7407 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7408 {
7409 if (complain & tf_error)
7410 error ("the address of %qD is not a valid template argument "
7411 "because it does not have static storage duration",
7412 decl);
7413 return true;
7414 }
7415 }
7416 break;
7417
7418 default:
7419 if (!INDIRECT_TYPE_P (type))
7420 /* We're only concerned about pointers and references here. */;
7421 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7422 /* Null pointer values are OK in C++11. */;
7423 else
7424 {
7425 if (VAR_P (expr))
7426 {
7427 if (complain & tf_error)
7428 error ("%qD is not a valid template argument "
7429 "because %qD is a variable, not the address of "
7430 "a variable", expr, expr);
7431 return true;
7432 }
7433 else
7434 {
7435 if (complain & tf_error)
7436 error ("%qE is not a valid template argument for %qT "
7437 "because it is not the address of a variable",
7438 expr, type);
7439 return true;
7440 }
7441 }
7442 }
7443 return false;
7444
7445 }
7446
7447 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7448 template argument EXPR. */
7449
7450 static tree
7451 create_template_parm_object (tree expr, tsubst_flags_t complain)
7452 {
7453 tree orig = expr;
7454 if (TREE_CODE (expr) == TARGET_EXPR)
7455 expr = TARGET_EXPR_INITIAL (expr);
7456
7457 if (!TREE_CONSTANT (expr))
7458 {
7459 if ((complain & tf_error)
7460 && require_rvalue_constant_expression (orig))
7461 cxx_constant_value (orig);
7462 return error_mark_node;
7463 }
7464 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7465 return error_mark_node;
7466
7467 /* This is no longer a compound literal. */
7468 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7469
7470 return get_template_parm_object (expr, mangle_template_parm_object (expr));
7471 }
7472
7473 /* The template arguments corresponding to template parameter objects of types
7474 that contain pointers to members. */
7475
7476 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7477
7478 /* Find or build an nttp object for (already-validated) EXPR with name
7479 NAME. When CHECK_INIT is false we don't need to process the initialiser,
7480 it's already been done. */
7481
7482 tree
7483 get_template_parm_object (tree expr, tree name, bool check_init/*=true*/)
7484 {
7485 tree decl = get_global_binding (name);
7486 if (decl)
7487 return decl;
7488
7489 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7490 decl = create_temporary_var (type);
7491 DECL_NTTP_OBJECT_P (decl) = true;
7492 DECL_CONTEXT (decl) = NULL_TREE;
7493 TREE_STATIC (decl) = true;
7494 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7495 TREE_READONLY (decl) = true;
7496 DECL_NAME (decl) = name;
7497 SET_DECL_ASSEMBLER_NAME (decl, name);
7498 comdat_linkage (decl);
7499
7500 if (!zero_init_p (type))
7501 {
7502 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7503 lower_var_init before we're done mangling. So store the original
7504 value elsewhere. We only need to unshare EXPR if it's not yet
7505 been processed. */
7506 tree copy = check_init ? unshare_constructor (expr) : expr;
7507 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7508 }
7509
7510 if (!check_init)
7511 {
7512 /* The EXPR is the already processed initializer, set it on the NTTP
7513 object now so that cp_finish_decl doesn't do it again later. */
7514 gcc_checking_assert (reduced_constant_expression_p (expr));
7515 DECL_INITIAL (decl) = expr;
7516 DECL_INITIALIZED_P (decl) = true;
7517 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7518 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7519 if (!TYPE_REF_P (type))
7520 TREE_CONSTANT (decl) = true;
7521 }
7522
7523 pushdecl_top_level_and_finish (decl, expr);
7524
7525 return decl;
7526 }
7527
7528 /* Return the actual template argument corresponding to template parameter
7529 object VAR. */
7530
7531 tree
7532 tparm_object_argument (tree var)
7533 {
7534 if (zero_init_p (TREE_TYPE (var)))
7535 return DECL_INITIAL (var);
7536 return *(tparm_obj_values->get (var));
7537 }
7538
7539 /* Attempt to convert the non-type template parameter EXPR to the
7540 indicated TYPE. If the conversion is successful, return the
7541 converted value. If the conversion is unsuccessful, return
7542 NULL_TREE if we issued an error message, or error_mark_node if we
7543 did not. We issue error messages for out-and-out bad template
7544 parameters, but not simply because the conversion failed, since we
7545 might be just trying to do argument deduction. Both TYPE and EXPR
7546 must be non-dependent.
7547
7548 The conversion follows the special rules described in
7549 [temp.arg.nontype], and it is much more strict than an implicit
7550 conversion.
7551
7552 This function is called twice for each template argument (see
7553 lookup_template_class for a more accurate description of this
7554 problem). This means that we need to handle expressions which
7555 are not valid in a C++ source, but can be created from the
7556 first call (for instance, casts to perform conversions). These
7557 hacks can go away after we fix the double coercion problem. */
7558
7559 static tree
7560 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7561 {
7562 tree expr_type;
7563 location_t loc = cp_expr_loc_or_input_loc (expr);
7564
7565 /* Detect immediately string literals as invalid non-type argument.
7566 This special-case is not needed for correctness (we would easily
7567 catch this later), but only to provide better diagnostic for this
7568 common user mistake. As suggested by DR 100, we do not mention
7569 linkage issues in the diagnostic as this is not the point. */
7570 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7571 {
7572 if (complain & tf_error)
7573 error ("%qE is not a valid template argument for type %qT "
7574 "because string literals can never be used in this context",
7575 expr, type);
7576 return NULL_TREE;
7577 }
7578
7579 /* Add the ADDR_EXPR now for the benefit of
7580 value_dependent_expression_p. */
7581 if (TYPE_PTROBV_P (type)
7582 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7583 {
7584 expr = decay_conversion (expr, complain);
7585 if (expr == error_mark_node)
7586 return error_mark_node;
7587 }
7588
7589 /* If we are in a template, EXPR may be non-dependent, but still
7590 have a syntactic, rather than semantic, form. For example, EXPR
7591 might be a SCOPE_REF, rather than the VAR_DECL to which the
7592 SCOPE_REF refers. Preserving the qualifying scope is necessary
7593 so that access checking can be performed when the template is
7594 instantiated -- but here we need the resolved form so that we can
7595 convert the argument. */
7596 bool non_dep = false;
7597 if (TYPE_REF_OBJ_P (type)
7598 && has_value_dependent_address (expr))
7599 /* If we want the address and it's value-dependent, don't fold. */;
7600 else if (processing_template_decl
7601 && !instantiation_dependent_expression_p (expr))
7602 non_dep = true;
7603 if (error_operand_p (expr))
7604 return error_mark_node;
7605 expr_type = TREE_TYPE (expr);
7606
7607 /* If the argument is non-dependent, perform any conversions in
7608 non-dependent context as well. */
7609 processing_template_decl_sentinel s (non_dep);
7610 if (non_dep)
7611 expr = instantiate_non_dependent_expr_internal (expr, complain);
7612
7613 bool val_dep_p = value_dependent_expression_p (expr);
7614 if (val_dep_p)
7615 expr = canonicalize_expr_argument (expr, complain);
7616 else
7617 STRIP_ANY_LOCATION_WRAPPER (expr);
7618
7619 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7620 to a non-type argument of "nullptr". */
7621 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7622 expr = fold_simple (convert (type, expr));
7623
7624 /* In C++11, integral or enumeration non-type template arguments can be
7625 arbitrary constant expressions. Pointer and pointer to
7626 member arguments can be general constant expressions that evaluate
7627 to a null value, but otherwise still need to be of a specific form. */
7628 if (cxx_dialect >= cxx11)
7629 {
7630 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7631 /* A PTRMEM_CST is already constant, and a valid template
7632 argument for a parameter of pointer to member type, we just want
7633 to leave it in that form rather than lower it to a
7634 CONSTRUCTOR. */;
7635 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7636 || cxx_dialect >= cxx17)
7637 {
7638 /* C++17: A template-argument for a non-type template-parameter shall
7639 be a converted constant expression (8.20) of the type of the
7640 template-parameter. */
7641 expr = build_converted_constant_expr (type, expr, complain);
7642 if (expr == error_mark_node)
7643 /* Make sure we return NULL_TREE only if we have really issued
7644 an error, as described above. */
7645 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7646 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7647 {
7648 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7649 return expr;
7650 }
7651 expr = maybe_constant_value (expr, NULL_TREE, mce_true);
7652 expr = convert_from_reference (expr);
7653 /* EXPR may have become value-dependent. */
7654 val_dep_p = value_dependent_expression_p (expr);
7655 }
7656 else if (TYPE_PTR_OR_PTRMEM_P (type))
7657 {
7658 tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
7659 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7660 : null_member_pointer_value_p (folded))
7661 expr = folded;
7662 }
7663 }
7664
7665 if (TYPE_REF_P (type))
7666 expr = mark_lvalue_use (expr);
7667 else
7668 expr = mark_rvalue_use (expr);
7669
7670 /* HACK: Due to double coercion, we can get a
7671 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7672 which is the tree that we built on the first call (see
7673 below when coercing to reference to object or to reference to
7674 function). We just strip everything and get to the arg.
7675 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7676 for examples. */
7677 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7678 {
7679 /* Check this before we strip *& to avoid redundancy. */
7680 if (!mark_single_function (expr, complain))
7681 return error_mark_node;
7682
7683 tree probe_type, probe = expr;
7684 if (REFERENCE_REF_P (probe))
7685 probe = TREE_OPERAND (probe, 0);
7686 probe_type = TREE_TYPE (probe);
7687 if (TREE_CODE (probe) == NOP_EXPR)
7688 {
7689 /* ??? Maybe we could use convert_from_reference here, but we
7690 would need to relax its constraints because the NOP_EXPR
7691 could actually change the type to something more cv-qualified,
7692 and this is not folded by convert_from_reference. */
7693 tree addr = TREE_OPERAND (probe, 0);
7694 if (TYPE_REF_P (probe_type)
7695 && TREE_CODE (addr) == ADDR_EXPR
7696 && TYPE_PTR_P (TREE_TYPE (addr))
7697 && (same_type_ignoring_top_level_qualifiers_p
7698 (TREE_TYPE (probe_type),
7699 TREE_TYPE (TREE_TYPE (addr)))))
7700 {
7701 expr = TREE_OPERAND (addr, 0);
7702 expr_type = TREE_TYPE (probe_type);
7703 }
7704 }
7705 }
7706
7707 /* [temp.arg.nontype]/5, bullet 1
7708
7709 For a non-type template-parameter of integral or enumeration type,
7710 integral promotions (_conv.prom_) and integral conversions
7711 (_conv.integral_) are applied. */
7712 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7713 || SCALAR_FLOAT_TYPE_P (type))
7714 {
7715 if (cxx_dialect < cxx11)
7716 {
7717 tree t = build_converted_constant_expr (type, expr, complain);
7718 t = maybe_constant_value (t);
7719 if (t != error_mark_node)
7720 expr = t;
7721 }
7722
7723 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7724 return error_mark_node;
7725
7726 /* Notice that there are constant expressions like '4 % 0' which
7727 do not fold into integer constants. */
7728 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7729 {
7730 if (complain & tf_error)
7731 {
7732 int errs = errorcount, warns = warningcount + werrorcount;
7733 if (!require_potential_constant_expression (expr))
7734 expr = error_mark_node;
7735 else
7736 expr = cxx_constant_value (expr);
7737 if (errorcount > errs || warningcount + werrorcount > warns)
7738 inform (loc, "in template argument for type %qT", type);
7739 if (expr == error_mark_node)
7740 return NULL_TREE;
7741 /* else cxx_constant_value complained but gave us
7742 a real constant, so go ahead. */
7743 if (!CONSTANT_CLASS_P (expr))
7744 {
7745 /* Some assemble time constant expressions like
7746 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7747 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7748 as we can emit them into .rodata initializers of
7749 variables, yet they can't fold into an INTEGER_CST at
7750 compile time. Refuse them here. */
7751 gcc_checking_assert (reduced_constant_expression_p (expr));
7752 error_at (loc, "template argument %qE for type %qT not "
7753 "a compile-time constant", expr, type);
7754 return NULL_TREE;
7755 }
7756 }
7757 else
7758 return NULL_TREE;
7759 }
7760
7761 /* Avoid typedef problems. */
7762 if (TREE_TYPE (expr) != type)
7763 expr = fold_convert (type, expr);
7764 }
7765 /* [temp.arg.nontype]/5, bullet 2
7766
7767 For a non-type template-parameter of type pointer to object,
7768 qualification conversions (_conv.qual_) and the array-to-pointer
7769 conversion (_conv.array_) are applied. */
7770 else if (TYPE_PTROBV_P (type))
7771 {
7772 tree decayed = expr;
7773
7774 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7775 decay_conversion or an explicit cast. If it's a problematic cast,
7776 we'll complain about it below. */
7777 if (TREE_CODE (expr) == NOP_EXPR)
7778 {
7779 tree probe = expr;
7780 STRIP_NOPS (probe);
7781 if (TREE_CODE (probe) == ADDR_EXPR
7782 && TYPE_PTR_P (TREE_TYPE (probe)))
7783 {
7784 expr = probe;
7785 expr_type = TREE_TYPE (expr);
7786 }
7787 }
7788
7789 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7790
7791 A template-argument for a non-type, non-template template-parameter
7792 shall be one of: [...]
7793
7794 -- the name of a non-type template-parameter;
7795 -- the address of an object or function with external linkage, [...]
7796 expressed as "& id-expression" where the & is optional if the name
7797 refers to a function or array, or if the corresponding
7798 template-parameter is a reference.
7799
7800 Here, we do not care about functions, as they are invalid anyway
7801 for a parameter of type pointer-to-object. */
7802
7803 if (val_dep_p)
7804 /* Non-type template parameters are OK. */
7805 ;
7806 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7807 /* Null pointer values are OK in C++11. */;
7808 else if (TREE_CODE (expr) != ADDR_EXPR
7809 && !INDIRECT_TYPE_P (expr_type))
7810 /* Other values, like integer constants, might be valid
7811 non-type arguments of some other type. */
7812 return error_mark_node;
7813 else if (invalid_tparm_referent_p (type, expr, complain))
7814 return NULL_TREE;
7815
7816 expr = decayed;
7817
7818 expr = perform_qualification_conversions (type, expr);
7819 if (expr == error_mark_node)
7820 return error_mark_node;
7821 }
7822 /* [temp.arg.nontype]/5, bullet 3
7823
7824 For a non-type template-parameter of type reference to object, no
7825 conversions apply. The type referred to by the reference may be more
7826 cv-qualified than the (otherwise identical) type of the
7827 template-argument. The template-parameter is bound directly to the
7828 template-argument, which must be an lvalue. */
7829 else if (TYPE_REF_OBJ_P (type))
7830 {
7831 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7832 expr_type))
7833 return error_mark_node;
7834
7835 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7836 {
7837 if (complain & tf_error)
7838 error ("%qE is not a valid template argument for type %qT "
7839 "because of conflicts in cv-qualification", expr, type);
7840 return NULL_TREE;
7841 }
7842
7843 if (!lvalue_p (expr))
7844 {
7845 if (complain & tf_error)
7846 error ("%qE is not a valid template argument for type %qT "
7847 "because it is not an lvalue", expr, type);
7848 return NULL_TREE;
7849 }
7850
7851 /* [temp.arg.nontype]/1
7852
7853 A template-argument for a non-type, non-template template-parameter
7854 shall be one of: [...]
7855
7856 -- the address of an object or function with external linkage. */
7857 if (INDIRECT_REF_P (expr)
7858 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7859 {
7860 expr = TREE_OPERAND (expr, 0);
7861 if (DECL_P (expr))
7862 {
7863 if (complain & tf_error)
7864 error ("%q#D is not a valid template argument for type %qT "
7865 "because a reference variable does not have a constant "
7866 "address", expr, type);
7867 return NULL_TREE;
7868 }
7869 }
7870
7871 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7872 /* OK, dependent reference. We don't want to ask whether a DECL is
7873 itself value-dependent, since what we want here is its address. */;
7874 else
7875 {
7876 expr = build_address (expr);
7877
7878 if (invalid_tparm_referent_p (type, expr, complain))
7879 return NULL_TREE;
7880 }
7881
7882 if (!same_type_p (type, TREE_TYPE (expr)))
7883 expr = build_nop (type, expr);
7884 }
7885 /* [temp.arg.nontype]/5, bullet 4
7886
7887 For a non-type template-parameter of type pointer to function, only
7888 the function-to-pointer conversion (_conv.func_) is applied. If the
7889 template-argument represents a set of overloaded functions (or a
7890 pointer to such), the matching function is selected from the set
7891 (_over.over_). */
7892 else if (TYPE_PTRFN_P (type))
7893 {
7894 /* If the argument is a template-id, we might not have enough
7895 context information to decay the pointer. */
7896 if (!type_unknown_p (expr_type))
7897 {
7898 expr = decay_conversion (expr, complain);
7899 if (expr == error_mark_node)
7900 return error_mark_node;
7901 }
7902
7903 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7904 /* Null pointer values are OK in C++11. */
7905 return perform_qualification_conversions (type, expr);
7906
7907 expr = convert_nontype_argument_function (type, expr, complain);
7908 if (!expr || expr == error_mark_node)
7909 return expr;
7910 }
7911 /* [temp.arg.nontype]/5, bullet 5
7912
7913 For a non-type template-parameter of type reference to function, no
7914 conversions apply. If the template-argument represents a set of
7915 overloaded functions, the matching function is selected from the set
7916 (_over.over_). */
7917 else if (TYPE_REFFN_P (type))
7918 {
7919 if (TREE_CODE (expr) == ADDR_EXPR)
7920 {
7921 if (complain & tf_error)
7922 {
7923 auto_diagnostic_group d;
7924 error ("%qE is not a valid template argument for type %qT "
7925 "because it is a pointer", expr, type);
7926 inform (input_location, "try using %qE instead",
7927 TREE_OPERAND (expr, 0));
7928 }
7929 return NULL_TREE;
7930 }
7931
7932 expr = convert_nontype_argument_function (type, expr, complain);
7933 if (!expr || expr == error_mark_node)
7934 return expr;
7935 }
7936 /* [temp.arg.nontype]/5, bullet 6
7937
7938 For a non-type template-parameter of type pointer to member function,
7939 no conversions apply. If the template-argument represents a set of
7940 overloaded member functions, the matching member function is selected
7941 from the set (_over.over_). */
7942 else if (TYPE_PTRMEMFUNC_P (type))
7943 {
7944 expr = instantiate_type (type, expr, tf_none);
7945 if (expr == error_mark_node)
7946 return error_mark_node;
7947
7948 /* [temp.arg.nontype] bullet 1 says the pointer to member
7949 expression must be a pointer-to-member constant. */
7950 if (!val_dep_p
7951 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7952 return NULL_TREE;
7953
7954 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7955 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7956 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7957 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7958 }
7959 /* [temp.arg.nontype]/5, bullet 7
7960
7961 For a non-type template-parameter of type pointer to data member,
7962 qualification conversions (_conv.qual_) are applied. */
7963 else if (TYPE_PTRDATAMEM_P (type))
7964 {
7965 /* [temp.arg.nontype] bullet 1 says the pointer to member
7966 expression must be a pointer-to-member constant. */
7967 if (!val_dep_p
7968 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7969 return NULL_TREE;
7970
7971 expr = perform_qualification_conversions (type, expr);
7972 if (expr == error_mark_node)
7973 return expr;
7974 }
7975 else if (NULLPTR_TYPE_P (type))
7976 {
7977 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7978 {
7979 if (complain & tf_error)
7980 error ("%qE is not a valid template argument for type %qT "
7981 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7982 return NULL_TREE;
7983 }
7984 return expr;
7985 }
7986 else if (CLASS_TYPE_P (type))
7987 {
7988 /* Replace the argument with a reference to the corresponding template
7989 parameter object. */
7990 if (!val_dep_p)
7991 expr = create_template_parm_object (expr, complain);
7992 if (expr == error_mark_node)
7993 return NULL_TREE;
7994 }
7995 /* A template non-type parameter must be one of the above. */
7996 else
7997 gcc_unreachable ();
7998
7999 /* Sanity check: did we actually convert the argument to the
8000 right type? */
8001 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8002 (type, TREE_TYPE (expr)));
8003 return convert_from_reference (expr);
8004 }
8005
8006 /* Subroutine of coerce_template_template_parms, which returns true if
8007 PARM and ARG match using the rule for the template parameters of
8008 template template parameters. Both PARM and ARG are template parameters;
8009 the rest of the arguments are the same as for
8010 coerce_template_template_parms. */
8011
8012 static bool
8013 coerce_template_template_parm (tree parm, tree arg, tsubst_flags_t complain,
8014 tree in_decl, tree outer_args)
8015 {
8016 if (arg == NULL_TREE || error_operand_p (arg)
8017 || parm == NULL_TREE || error_operand_p (parm))
8018 return false;
8019
8020 if (TREE_CODE (arg) != TREE_CODE (parm))
8021 return false;
8022
8023 switch (TREE_CODE (parm))
8024 {
8025 case TEMPLATE_DECL:
8026 /* We encounter instantiations of templates like
8027 template <template <template <class> class> class TT>
8028 class C; */
8029 {
8030 if (!coerce_template_template_parms
8031 (parm, arg, complain, in_decl, outer_args))
8032 return false;
8033 }
8034 /* Fall through. */
8035
8036 case TYPE_DECL:
8037 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
8038 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8039 /* Argument is a parameter pack but parameter is not. */
8040 return false;
8041 break;
8042
8043 case PARM_DECL:
8044 /* The tsubst call is used to handle cases such as
8045
8046 template <int> class C {};
8047 template <class T, template <T> class TT> class D {};
8048 D<int, C> d;
8049
8050 i.e. the parameter list of TT depends on earlier parameters. */
8051 if (!uses_template_parms (TREE_TYPE (arg)))
8052 {
8053 ++processing_template_decl;
8054 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
8055 --processing_template_decl;
8056 if (!uses_template_parms (t)
8057 && !same_type_p (t, TREE_TYPE (arg)))
8058 return false;
8059 }
8060
8061 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
8062 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8063 /* Argument is a parameter pack but parameter is not. */
8064 return false;
8065
8066 break;
8067
8068 default:
8069 gcc_unreachable ();
8070 }
8071
8072 return true;
8073 }
8074
8075 /* Coerce template argument list ARGLIST for use with template
8076 template-parameter TEMPL. */
8077
8078 static tree
8079 coerce_template_args_for_ttp (tree templ, tree arglist,
8080 tsubst_flags_t complain)
8081 {
8082 /* Consider an example where a template template parameter declared as
8083
8084 template <class T, class U = std::allocator<T> > class TT
8085
8086 The template parameter level of T and U are one level larger than
8087 of TT. To proper process the default argument of U, say when an
8088 instantiation `TT<int>' is seen, we need to build the full
8089 arguments containing {int} as the innermost level. Outer levels,
8090 available when not appearing as default template argument, can be
8091 obtained from the arguments of the enclosing template.
8092
8093 Suppose that TT is later substituted with std::vector. The above
8094 instantiation is `TT<int, std::allocator<T> >' with TT at
8095 level 1, and T at level 2, while the template arguments at level 1
8096 becomes {std::vector} and the inner level 2 is {int}. */
8097
8098 tree outer = DECL_CONTEXT (templ);
8099 if (outer)
8100 outer = generic_targs_for (outer);
8101 else if (current_template_parms)
8102 {
8103 /* This is an argument of the current template, so we haven't set
8104 DECL_CONTEXT yet. We can also get here when level-lowering a
8105 bound ttp. */
8106 tree relevant_template_parms;
8107
8108 /* Parameter levels that are greater than the level of the given
8109 template template parm are irrelevant. */
8110 relevant_template_parms = current_template_parms;
8111 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8112 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8113 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8114
8115 outer = template_parms_to_args (relevant_template_parms);
8116 }
8117
8118 if (outer)
8119 arglist = add_to_template_args (outer, arglist);
8120
8121 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8122 return coerce_template_parms (parmlist, arglist, templ, complain);
8123 }
8124
8125 /* A cache of template template parameters with match-all default
8126 arguments. */
8127 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
8128
8129 /* T is a bound template template-parameter. Copy its arguments into default
8130 arguments of the template template-parameter's template parameters. */
8131
8132 static tree
8133 add_defaults_to_ttp (tree otmpl)
8134 {
8135 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
8136 return *c;
8137
8138 tree ntmpl = copy_node (otmpl);
8139
8140 tree ntype = copy_node (TREE_TYPE (otmpl));
8141 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
8142 TYPE_MAIN_VARIANT (ntype) = ntype;
8143 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
8144 TYPE_NAME (ntype) = ntmpl;
8145 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
8146
8147 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
8148 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
8149 TEMPLATE_PARM_DECL (idx) = ntmpl;
8150 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
8151
8152 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
8153 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
8154 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
8155 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
8156 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
8157 {
8158 tree o = TREE_VEC_ELT (vec, i);
8159 if (!template_parameter_pack_p (TREE_VALUE (o)))
8160 {
8161 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
8162 TREE_PURPOSE (n) = any_targ_node;
8163 }
8164 }
8165
8166 tree oresult = DECL_TEMPLATE_RESULT (otmpl);
8167 tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
8168 tree gen_ntmpl;
8169 if (gen_otmpl == otmpl)
8170 gen_ntmpl = ntmpl;
8171 else
8172 gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
8173
8174 tree nresult = copy_decl (oresult);
8175 DECL_TEMPLATE_INFO (nresult)
8176 = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
8177 DECL_TEMPLATE_RESULT (ntmpl) = nresult;
8178
8179 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
8180 return ntmpl;
8181 }
8182
8183 /* ARG is a bound potential template template-argument, and PARGS is a list
8184 of arguments for the corresponding template template-parameter. Adjust
8185 PARGS as appropriate for application to ARG's template, and if ARG is a
8186 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
8187 arguments to the template template parameter. */
8188
8189 static tree
8190 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
8191 {
8192 ++processing_template_decl;
8193 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
8194 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8195 {
8196 /* When comparing two template template-parameters in partial ordering,
8197 rewrite the one currently being used as an argument to have default
8198 arguments for all parameters. */
8199 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
8200 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
8201 if (pargs != error_mark_node)
8202 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
8203 TYPE_TI_ARGS (arg));
8204 }
8205 else
8206 {
8207 tree aparms
8208 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
8209 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
8210 }
8211 --processing_template_decl;
8212 return pargs;
8213 }
8214
8215 /* Subroutine of unify for the case when PARM is a
8216 BOUND_TEMPLATE_TEMPLATE_PARM. */
8217
8218 static int
8219 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
8220 bool explain_p)
8221 {
8222 tree parmvec = TYPE_TI_ARGS (parm);
8223 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
8224
8225 /* The template template parm might be variadic and the argument
8226 not, so flatten both argument lists. */
8227 parmvec = expand_template_argument_pack (parmvec);
8228 argvec = expand_template_argument_pack (argvec);
8229
8230 if (flag_new_ttp)
8231 {
8232 /* In keeping with P0522R0, adjust P's template arguments
8233 to apply to A's template; then flatten it again. */
8234 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
8235 nparmvec = expand_template_argument_pack (nparmvec);
8236
8237 if (unify (tparms, targs, nparmvec, argvec,
8238 UNIFY_ALLOW_NONE, explain_p))
8239 return 1;
8240
8241 /* If the P0522 adjustment eliminated a pack expansion, deduce
8242 empty packs. */
8243 if (flag_new_ttp
8244 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
8245 && unify_pack_expansion (tparms, targs, parmvec, argvec,
8246 DEDUCE_EXACT, /*sub*/true, explain_p))
8247 return 1;
8248 }
8249 else
8250 {
8251 /* Deduce arguments T, i from TT<T> or TT<i>.
8252 We check each element of PARMVEC and ARGVEC individually
8253 rather than the whole TREE_VEC since they can have
8254 different number of elements, which is allowed under N2555. */
8255
8256 int len = TREE_VEC_LENGTH (parmvec);
8257
8258 /* Check if the parameters end in a pack, making them
8259 variadic. */
8260 int parm_variadic_p = 0;
8261 if (len > 0
8262 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
8263 parm_variadic_p = 1;
8264
8265 for (int i = 0; i < len - parm_variadic_p; ++i)
8266 /* If the template argument list of P contains a pack
8267 expansion that is not the last template argument, the
8268 entire template argument list is a non-deduced
8269 context. */
8270 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
8271 return unify_success (explain_p);
8272
8273 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
8274 return unify_too_few_arguments (explain_p,
8275 TREE_VEC_LENGTH (argvec), len);
8276
8277 for (int i = 0; i < len - parm_variadic_p; ++i)
8278 if (unify (tparms, targs,
8279 TREE_VEC_ELT (parmvec, i),
8280 TREE_VEC_ELT (argvec, i),
8281 UNIFY_ALLOW_NONE, explain_p))
8282 return 1;
8283
8284 if (parm_variadic_p
8285 && unify_pack_expansion (tparms, targs,
8286 parmvec, argvec,
8287 DEDUCE_EXACT,
8288 /*subr=*/true, explain_p))
8289 return 1;
8290 }
8291
8292 return 0;
8293 }
8294
8295 /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
8296 template template parameters.
8297
8298 Consider the example:
8299 template <class T> class A;
8300 template<template <class U> class TT> class B;
8301
8302 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
8303 and OUTER_ARGS contains A. */
8304
8305 static int
8306 coerce_template_template_parms (tree parm_tmpl,
8307 tree arg_tmpl,
8308 tsubst_flags_t complain,
8309 tree in_decl,
8310 tree outer_args)
8311 {
8312 int nparms, nargs, i;
8313 tree parm, arg;
8314 int variadic_p = 0;
8315
8316 tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
8317 tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
8318 tree gen_arg_tmpl = most_general_template (arg_tmpl);
8319 tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
8320
8321 nparms = TREE_VEC_LENGTH (parm_parms);
8322 nargs = TREE_VEC_LENGTH (arg_parms);
8323
8324 if (flag_new_ttp)
8325 {
8326 /* P0522R0: A template template-parameter P is at least as specialized as
8327 a template template-argument A if, given the following rewrite to two
8328 function templates, the function template corresponding to P is at
8329 least as specialized as the function template corresponding to A
8330 according to the partial ordering rules for function templates
8331 ([temp.func.order]). Given an invented class template X with the
8332 template parameter list of A (including default arguments):
8333
8334 * Each of the two function templates has the same template parameters,
8335 respectively, as P or A.
8336
8337 * Each function template has a single function parameter whose type is
8338 a specialization of X with template arguments corresponding to the
8339 template parameters from the respective function template where, for
8340 each template parameter PP in the template parameter list of the
8341 function template, a corresponding template argument AA is formed. If
8342 PP declares a parameter pack, then AA is the pack expansion
8343 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8344
8345 If the rewrite produces an invalid type, then P is not at least as
8346 specialized as A. */
8347
8348 /* So coerce P's args to apply to A's parms, and then deduce between A's
8349 args and the converted args. If that succeeds, A is at least as
8350 specialized as P, so they match.*/
8351 processing_template_decl_sentinel ptds (/*reset*/false);
8352 ++processing_template_decl;
8353
8354 tree pargs = template_parms_level_to_args (parm_parms);
8355
8356 /* PARM and ARG might be at different template depths, and we want to
8357 pass the right additional levels of args when coercing PARGS to
8358 ARG_PARMS in case we need to do any substitution into non-type
8359 template parameter types.
8360
8361 OUTER_ARGS are not the right outer levels in this case, as they are
8362 the args we're building up for PARM, and for the coercion we want the
8363 args for ARG. If DECL_CONTEXT isn't set for a template template
8364 parameter, we can assume that it's in the current scope. */
8365 tree ctx = DECL_CONTEXT (arg_tmpl);
8366 if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8367 ctx = current_scope ();
8368 tree scope_args = NULL_TREE;
8369 if (tree tinfo = get_template_info (ctx))
8370 scope_args = TI_ARGS (tinfo);
8371 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8372 {
8373 int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
8374 int scope_depth = TMPL_ARGS_DEPTH (scope_args);
8375 tree full_pargs = make_tree_vec (level + 1);
8376
8377 /* Only use as many levels from the scope as needed
8378 (excluding the level of ARG). */
8379 for (int i = 0; i < level - 1; ++i)
8380 if (i < scope_depth)
8381 TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
8382 else
8383 TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
8384
8385 /* Add the arguments that appear at the levels of ARG. */
8386 tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
8387 adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
8388 TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
8389
8390 TREE_VEC_ELT (full_pargs, level) = pargs;
8391 pargs = full_pargs;
8392 }
8393 else
8394 pargs = add_to_template_args (scope_args, pargs);
8395
8396 pargs = coerce_template_parms (gen_arg_parms, pargs,
8397 NULL_TREE, tf_none);
8398 if (pargs != error_mark_node)
8399 {
8400 tree targs = make_tree_vec (nargs);
8401 tree aargs = template_parms_level_to_args (arg_parms);
8402 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8403 /*explain*/false))
8404 return 1;
8405 }
8406 }
8407
8408 /* Determine whether we have a parameter pack at the end of the
8409 template template parameter's template parameter list. */
8410 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8411 {
8412 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8413
8414 if (error_operand_p (parm))
8415 return 0;
8416
8417 switch (TREE_CODE (parm))
8418 {
8419 case TEMPLATE_DECL:
8420 case TYPE_DECL:
8421 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8422 variadic_p = 1;
8423 break;
8424
8425 case PARM_DECL:
8426 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8427 variadic_p = 1;
8428 break;
8429
8430 default:
8431 gcc_unreachable ();
8432 }
8433 }
8434
8435 if (nargs != nparms
8436 && !(variadic_p && nargs >= nparms - 1))
8437 return 0;
8438
8439 /* Check all of the template parameters except the parameter pack at
8440 the end (if any). */
8441 for (i = 0; i < nparms - variadic_p; ++i)
8442 {
8443 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8444 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8445 continue;
8446
8447 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8448 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8449
8450 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8451 outer_args))
8452 return 0;
8453
8454 }
8455
8456 if (variadic_p)
8457 {
8458 /* Check each of the template parameters in the template
8459 argument against the template parameter pack at the end of
8460 the template template parameter. */
8461 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8462 return 0;
8463
8464 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8465
8466 for (; i < nargs; ++i)
8467 {
8468 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8469 continue;
8470
8471 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8472
8473 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8474 outer_args))
8475 return 0;
8476 }
8477 }
8478
8479 return 1;
8480 }
8481
8482 /* Verifies that the deduced template arguments (in TARGS) for the
8483 template template parameters (in TPARMS) represent valid bindings,
8484 by comparing the template parameter list of each template argument
8485 to the template parameter list of its corresponding template
8486 template parameter, in accordance with DR150. This
8487 routine can only be called after all template arguments have been
8488 deduced. It will return TRUE if all of the template template
8489 parameter bindings are okay, FALSE otherwise. */
8490 bool
8491 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8492 {
8493 int i, ntparms = TREE_VEC_LENGTH (tparms);
8494 bool ret = true;
8495
8496 /* We're dealing with template parms in this process. */
8497 ++processing_template_decl;
8498
8499 targs = INNERMOST_TEMPLATE_ARGS (targs);
8500
8501 for (i = 0; i < ntparms; ++i)
8502 {
8503 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8504 tree targ = TREE_VEC_ELT (targs, i);
8505
8506 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8507 {
8508 tree packed_args = NULL_TREE;
8509 int idx, len = 1;
8510
8511 if (ARGUMENT_PACK_P (targ))
8512 {
8513 /* Look inside the argument pack. */
8514 packed_args = ARGUMENT_PACK_ARGS (targ);
8515 len = TREE_VEC_LENGTH (packed_args);
8516 }
8517
8518 for (idx = 0; idx < len; ++idx)
8519 {
8520 if (packed_args)
8521 /* Extract the next argument from the argument
8522 pack. */
8523 targ = TREE_VEC_ELT (packed_args, idx);
8524
8525 if (PACK_EXPANSION_P (targ))
8526 /* Look at the pattern of the pack expansion. */
8527 targ = PACK_EXPANSION_PATTERN (targ);
8528
8529 /* Extract the template parameters from the template
8530 argument. */
8531 if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8532 targ = TYPE_NAME (targ);
8533
8534 /* Verify that we can coerce the template template
8535 parameters from the template argument to the template
8536 parameter. This requires an exact match. */
8537 if (TREE_CODE (targ) == TEMPLATE_DECL
8538 && !coerce_template_template_parms
8539 (tparm,
8540 targ,
8541 tf_none,
8542 tparm,
8543 targs))
8544 {
8545 ret = false;
8546 goto out;
8547 }
8548 }
8549 }
8550 }
8551
8552 out:
8553
8554 --processing_template_decl;
8555 return ret;
8556 }
8557
8558 /* Since type attributes aren't mangled, we need to strip them from
8559 template type arguments. */
8560
8561 tree
8562 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8563 {
8564 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8565 return arg;
8566 bool removed_attributes = false;
8567 tree canon = strip_typedefs (arg, &removed_attributes);
8568 if (removed_attributes
8569 && (complain & tf_warning))
8570 warning (OPT_Wignored_attributes,
8571 "ignoring attributes on template argument %qT", arg);
8572 return canon;
8573 }
8574
8575 /* And from inside dependent non-type arguments like sizeof(Type). */
8576
8577 static tree
8578 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8579 {
8580 if (!arg || arg == error_mark_node)
8581 return arg;
8582 bool removed_attributes = false;
8583 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8584 if (removed_attributes
8585 && (complain & tf_warning))
8586 warning (OPT_Wignored_attributes,
8587 "ignoring attributes in template argument %qE", arg);
8588 return canon;
8589 }
8590
8591 /* A template declaration can be substituted for a constrained
8592 template template parameter only when the argument is no more
8593 constrained than the parameter. */
8594
8595 static bool
8596 is_compatible_template_arg (tree parm, tree arg, tree args)
8597 {
8598 tree parm_cons = get_constraints (parm);
8599
8600 /* For now, allow constrained template template arguments
8601 and unconstrained template template parameters. */
8602 if (parm_cons == NULL_TREE)
8603 return true;
8604
8605 /* If the template parameter is constrained, we need to rewrite its
8606 constraints in terms of the ARG's template parameters. This ensures
8607 that all of the template parameter types will have the same depth.
8608
8609 Note that this is only valid when coerce_template_template_parm is
8610 true for the innermost template parameters of PARM and ARG. In other
8611 words, because coercion is successful, this conversion will be valid. */
8612 tree new_args = NULL_TREE;
8613 if (parm_cons)
8614 {
8615 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8616 new_args = template_parms_level_to_args (aparms);
8617 new_args = add_to_template_args (args, new_args);
8618 ++processing_template_decl;
8619 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8620 tf_none, NULL_TREE);
8621 --processing_template_decl;
8622 if (parm_cons == error_mark_node)
8623 return false;
8624 }
8625
8626 return ttp_subsumes (parm_cons, arg);
8627 }
8628
8629 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8630 because one of them is dependent. But we need to represent the
8631 conversion for the benefit of cp_tree_equal. */
8632
8633 static tree
8634 maybe_convert_nontype_argument (tree type, tree arg, bool force)
8635 {
8636 /* Auto parms get no conversion. */
8637 if (type_uses_auto (type))
8638 return arg;
8639 /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
8640 That would complicate other things, and it doesn't seem necessary. */
8641 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
8642 return arg;
8643 /* We don't need or want to add this conversion now if we're going to use the
8644 argument for deduction. */
8645 if (!value_dependent_expression_p (arg))
8646 force = false;
8647 else if (!force)
8648 return arg;
8649
8650 type = cv_unqualified (type);
8651 tree argtype = TREE_TYPE (arg);
8652 if (argtype && same_type_p (type, argtype))
8653 return arg;
8654
8655 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8656 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8657 IMPLICIT_CONV_EXPR_FORCED (arg) = force;
8658 return arg;
8659 }
8660
8661 /* Convert the indicated template ARG as necessary to match the
8662 indicated template PARM. Returns the converted ARG, or
8663 error_mark_node if the conversion was unsuccessful. Error and
8664 warning messages are issued under control of COMPLAIN. This
8665 conversion is for the Ith parameter in the parameter list. ARGS is
8666 the full set of template arguments deduced so far. */
8667
8668 static tree
8669 convert_template_argument (tree parm,
8670 tree arg,
8671 tree args,
8672 tsubst_flags_t complain,
8673 int i,
8674 tree in_decl)
8675 {
8676 tree orig_arg;
8677 tree val;
8678 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8679
8680 if (parm == error_mark_node || error_operand_p (arg))
8681 return error_mark_node;
8682
8683 if (arg == any_targ_node)
8684 return arg;
8685
8686 if (TREE_CODE (arg) == TREE_LIST
8687 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8688 {
8689 /* The template argument was the name of some
8690 member function. That's usually
8691 invalid, but static members are OK. In any
8692 case, grab the underlying fields/functions
8693 and issue an error later if required. */
8694 TREE_TYPE (arg) = unknown_type_node;
8695 }
8696
8697 orig_arg = arg;
8698
8699 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8700 requires_type = (TREE_CODE (parm) == TYPE_DECL
8701 || requires_tmpl_type);
8702
8703 /* When determining whether an argument pack expansion is a template,
8704 look at the pattern. */
8705 if (PACK_EXPANSION_P (arg))
8706 arg = PACK_EXPANSION_PATTERN (arg);
8707
8708 /* Deal with an injected-class-name used as a template template arg. */
8709 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8710 {
8711 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8712 if (TREE_CODE (t) == TEMPLATE_DECL)
8713 {
8714 if (cxx_dialect >= cxx11)
8715 /* OK under DR 1004. */;
8716 else if (complain & tf_warning_or_error)
8717 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8718 " used as template template argument", TYPE_NAME (arg));
8719 else if (flag_pedantic_errors)
8720 t = arg;
8721
8722 arg = t;
8723 }
8724 }
8725
8726 is_tmpl_type =
8727 ((TREE_CODE (arg) == TEMPLATE_DECL
8728 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8729 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8730 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8731 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8732
8733 if (is_tmpl_type
8734 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8735 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8736 arg = TYPE_STUB_DECL (arg);
8737
8738 is_type = TYPE_P (arg) || is_tmpl_type;
8739
8740 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8741 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8742 {
8743 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8744 {
8745 if (complain & tf_error)
8746 error ("invalid use of destructor %qE as a type", orig_arg);
8747 return error_mark_node;
8748 }
8749
8750 permerror (input_location,
8751 "to refer to a type member of a template parameter, "
8752 "use %<typename %E%>", orig_arg);
8753
8754 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8755 TREE_OPERAND (arg, 1),
8756 typename_type,
8757 complain);
8758 arg = orig_arg;
8759 is_type = 1;
8760 }
8761 if (is_type != requires_type)
8762 {
8763 if (in_decl)
8764 {
8765 if (complain & tf_error)
8766 {
8767 auto_diagnostic_group d;
8768 error ("type/value mismatch at argument %d in template "
8769 "parameter list for %qD",
8770 i + 1, in_decl);
8771 if (is_type)
8772 {
8773 /* The template argument is a type, but we're expecting
8774 an expression. */
8775 inform (input_location,
8776 " expected a constant of type %qT, got %qT",
8777 TREE_TYPE (parm),
8778 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8779 /* [temp.arg]/2: "In a template-argument, an ambiguity
8780 between a type-id and an expression is resolved to a
8781 type-id, regardless of the form of the corresponding
8782 template-parameter." So give the user a clue. */
8783 if (TREE_CODE (arg) == FUNCTION_TYPE)
8784 inform (input_location, " ambiguous template argument "
8785 "for non-type template parameter is treated as "
8786 "function type");
8787 }
8788 else if (requires_tmpl_type)
8789 inform (input_location,
8790 " expected a class template, got %qE", orig_arg);
8791 else
8792 inform (input_location,
8793 " expected a type, got %qE", orig_arg);
8794 }
8795 }
8796 return error_mark_node;
8797 }
8798 if (is_tmpl_type ^ requires_tmpl_type)
8799 {
8800 if (in_decl && (complain & tf_error))
8801 {
8802 auto_diagnostic_group d;
8803 error ("type/value mismatch at argument %d in template "
8804 "parameter list for %qD",
8805 i + 1, in_decl);
8806 if (is_tmpl_type)
8807 inform (input_location,
8808 " expected a type, got %qT", DECL_NAME (arg));
8809 else
8810 inform (input_location,
8811 " expected a class template, got %qT", orig_arg);
8812 }
8813 return error_mark_node;
8814 }
8815
8816 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8817 /* We already did the appropriate conversion when packing args. */
8818 val = orig_arg;
8819 else if (is_type)
8820 {
8821 if (requires_tmpl_type)
8822 {
8823 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8824 /* The number of argument required is not known yet.
8825 Just accept it for now. */
8826 val = orig_arg;
8827 else
8828 {
8829 /* Strip alias templates that are equivalent to another
8830 template. */
8831 arg = get_underlying_template (arg);
8832
8833 if (coerce_template_template_parms (parm, arg,
8834 complain, in_decl,
8835 args))
8836 {
8837 val = arg;
8838
8839 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8840 TEMPLATE_DECL. */
8841 if (val != error_mark_node)
8842 {
8843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8844 val = TREE_TYPE (val);
8845 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8846 val = make_pack_expansion (val, complain);
8847 }
8848 }
8849 else
8850 {
8851 if (in_decl && (complain & tf_error))
8852 {
8853 auto_diagnostic_group d;
8854 error ("type/value mismatch at argument %d in "
8855 "template parameter list for %qD",
8856 i + 1, in_decl);
8857 inform (input_location,
8858 " expected a template of type %qD, got %qT",
8859 parm, orig_arg);
8860 }
8861
8862 val = error_mark_node;
8863 }
8864
8865 // Check that the constraints are compatible before allowing the
8866 // substitution.
8867 if (val != error_mark_node)
8868 if (!is_compatible_template_arg (parm, arg, args))
8869 {
8870 if (in_decl && (complain & tf_error))
8871 {
8872 auto_diagnostic_group d;
8873 error ("constraint mismatch at argument %d in "
8874 "template parameter list for %qD",
8875 i + 1, in_decl);
8876 inform (input_location, " expected %qD but got %qD",
8877 parm, arg);
8878 }
8879 val = error_mark_node;
8880 }
8881 }
8882 }
8883 else
8884 val = orig_arg;
8885 /* We only form one instance of each template specialization.
8886 Therefore, if we use a non-canonical variant (i.e., a
8887 typedef), any future messages referring to the type will use
8888 the typedef, which is confusing if those future uses do not
8889 themselves also use the typedef. */
8890 if (TYPE_P (val))
8891 val = canonicalize_type_argument (val, complain);
8892 }
8893 else
8894 {
8895 tree t = TREE_TYPE (parm);
8896
8897 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8898 > TMPL_ARGS_DEPTH (args))
8899 /* We don't have enough levels of args to do any substitution. This
8900 can happen in the context of -fnew-ttp-matching. */;
8901 else if (tree a = type_uses_auto (t))
8902 {
8903 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8904 LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
8905 if (t == error_mark_node)
8906 return error_mark_node;
8907 }
8908 else
8909 t = tsubst (t, args, complain, in_decl);
8910
8911 /* Perform array-to-pointer and function-to-pointer conversion
8912 as per [temp.param]/10. */
8913 t = type_decays_to (t);
8914
8915 if (invalid_nontype_parm_type_p (t, complain))
8916 return error_mark_node;
8917
8918 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8919 this non-type template parameter, as per [temp.param]/6. */
8920 t = cv_unqualified (t);
8921
8922 if (t != TREE_TYPE (parm))
8923 t = canonicalize_type_argument (t, complain);
8924
8925 /* We need to handle arguments for alias or concept templates
8926 differently: we need to force building an IMPLICIT_CONV_EXPR, because
8927 these arguments are going to be substituted directly into the
8928 dependent type; they might not get another chance at
8929 convert_nontype_argument. But if the argument ends up here again for
8930 a template that isn't one of those, remove the conversion for
8931 consistency between naming the same dependent type directly or through
8932 an alias. */
8933 bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
8934 || concept_definition_p (in_decl));
8935 if (!force_conv
8936 && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
8937 && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
8938 && same_type_p (TREE_TYPE (orig_arg), t))
8939 orig_arg = TREE_OPERAND (orig_arg, 0);
8940
8941 if (!type_dependent_expression_p (orig_arg)
8942 && !uses_template_parms (t))
8943 /* We used to call digest_init here. However, digest_init
8944 will report errors, which we don't want when complain
8945 is zero. More importantly, digest_init will try too
8946 hard to convert things: for example, `0' should not be
8947 converted to pointer type at this point according to
8948 the standard. Accepting this is not merely an
8949 extension, since deciding whether or not these
8950 conversions can occur is part of determining which
8951 function template to call, or whether a given explicit
8952 argument specification is valid. */
8953 val = convert_nontype_argument (t, orig_arg, complain);
8954 else
8955 {
8956 val = canonicalize_expr_argument (orig_arg, complain);
8957 val = maybe_convert_nontype_argument (t, val, force_conv);
8958 }
8959
8960 if (val == NULL_TREE)
8961 val = error_mark_node;
8962 else if (val == error_mark_node && (complain & tf_error))
8963 error_at (cp_expr_loc_or_input_loc (orig_arg),
8964 "could not convert template argument %qE from %qT to %qT",
8965 orig_arg, TREE_TYPE (orig_arg), t);
8966
8967 if (INDIRECT_REF_P (val))
8968 {
8969 /* Reject template arguments that are references to built-in
8970 functions with no library fallbacks. */
8971 const_tree inner = TREE_OPERAND (val, 0);
8972 const_tree innertype = TREE_TYPE (inner);
8973 if (innertype
8974 && TYPE_REF_P (innertype)
8975 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8976 && TREE_OPERAND_LENGTH (inner) > 0
8977 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8978 return error_mark_node;
8979 }
8980
8981 if (TREE_CODE (val) == SCOPE_REF)
8982 {
8983 /* Strip typedefs from the SCOPE_REF. */
8984 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8985 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8986 complain);
8987 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8988 QUALIFIED_NAME_IS_TEMPLATE (val));
8989 }
8990 }
8991
8992 return val;
8993 }
8994
8995 /* Coerces the remaining template arguments in INNER_ARGS (from
8996 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8997 Returns the coerced argument pack. PARM_IDX is the position of this
8998 parameter in the template parameter list. ARGS is the original
8999 template argument list. */
9000 static tree
9001 coerce_template_parameter_pack (tree parms,
9002 int parm_idx,
9003 tree args,
9004 tree inner_args,
9005 int arg_idx,
9006 tree new_args,
9007 int* lost,
9008 tree in_decl,
9009 tsubst_flags_t complain)
9010 {
9011 tree parm = TREE_VEC_ELT (parms, parm_idx);
9012 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
9013 tree packed_args;
9014 tree argument_pack;
9015 tree packed_parms = NULL_TREE;
9016
9017 if (arg_idx > nargs)
9018 arg_idx = nargs;
9019
9020 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
9021 {
9022 /* When the template parameter is a non-type template parameter pack
9023 or template template parameter pack whose type or template
9024 parameters use parameter packs, we know exactly how many arguments
9025 we are looking for. Build a vector of the instantiated decls for
9026 these template parameters in PACKED_PARMS. */
9027 /* We can't use make_pack_expansion here because it would interpret a
9028 _DECL as a use rather than a declaration. */
9029 tree decl = TREE_VALUE (parm);
9030 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
9031 PACK_EXPANSION_PATTERN (exp) = decl;
9032 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
9033 SET_TYPE_STRUCTURAL_EQUALITY (exp);
9034
9035 TREE_VEC_LENGTH (args)--;
9036 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
9037 TREE_VEC_LENGTH (args)++;
9038
9039 if (packed_parms == error_mark_node)
9040 return error_mark_node;
9041
9042 /* If we're doing a partial instantiation of a member template,
9043 verify that all of the types used for the non-type
9044 template parameter pack are, in fact, valid for non-type
9045 template parameters. */
9046 if (arg_idx < nargs
9047 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
9048 {
9049 int j, len = TREE_VEC_LENGTH (packed_parms);
9050 for (j = 0; j < len; ++j)
9051 {
9052 tree t = TREE_VEC_ELT (packed_parms, j);
9053 if (TREE_CODE (t) == PARM_DECL
9054 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
9055 return error_mark_node;
9056 }
9057 /* We don't know how many args we have yet, just
9058 use the unconverted ones for now. */
9059 return NULL_TREE;
9060 }
9061
9062 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
9063 }
9064 else
9065 packed_args = make_tree_vec (nargs - arg_idx);
9066
9067 /* Convert the remaining arguments, which will be a part of the
9068 parameter pack "parm". */
9069 int first_pack_arg = arg_idx;
9070 for (; arg_idx < nargs; ++arg_idx)
9071 {
9072 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
9073 tree actual_parm = TREE_VALUE (parm);
9074 int pack_idx = arg_idx - first_pack_arg;
9075
9076 if (packed_parms)
9077 {
9078 /* Once we've packed as many args as we have types, stop. */
9079 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
9080 break;
9081 else if (PACK_EXPANSION_P (arg))
9082 /* We don't know how many args we have yet, just
9083 use the unconverted ones for now. */
9084 return NULL_TREE;
9085 else
9086 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
9087 }
9088
9089 if (arg == error_mark_node)
9090 {
9091 if (complain & tf_error)
9092 error ("template argument %d is invalid", arg_idx + 1);
9093 }
9094 else
9095 arg = convert_template_argument (actual_parm,
9096 arg, new_args, complain, parm_idx,
9097 in_decl);
9098 if (arg == error_mark_node)
9099 (*lost)++;
9100 TREE_VEC_ELT (packed_args, pack_idx) = arg;
9101 }
9102
9103 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
9104 && TREE_VEC_LENGTH (packed_args) > 0)
9105 {
9106 if (complain & tf_error)
9107 error ("wrong number of template arguments (%d, should be %d)",
9108 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
9109 return error_mark_node;
9110 }
9111
9112 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
9113 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
9114 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
9115 else
9116 {
9117 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
9118 TREE_CONSTANT (argument_pack) = 1;
9119 }
9120
9121 ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
9122 if (CHECKING_P)
9123 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
9124 TREE_VEC_LENGTH (packed_args));
9125 return argument_pack;
9126 }
9127
9128 /* Returns the number of pack expansions in the template argument vector
9129 ARGS. */
9130
9131 static int
9132 pack_expansion_args_count (tree args)
9133 {
9134 int i;
9135 int count = 0;
9136 if (args)
9137 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
9138 {
9139 tree elt = TREE_VEC_ELT (args, i);
9140 if (elt && PACK_EXPANSION_P (elt))
9141 ++count;
9142 }
9143 return count;
9144 }
9145
9146 /* Convert all template arguments to their appropriate types, and
9147 return a vector containing the innermost resulting template
9148 arguments. If any error occurs, return error_mark_node. Error and
9149 warning messages are issued under control of COMPLAIN.
9150
9151 If PARMS represents all template parameters levels, this function
9152 returns a vector of vectors representing all the resulting argument
9153 levels. Note that in this case, only the innermost arguments are
9154 coerced because the outermost ones are supposed to have been coerced
9155 already. Otherwise, if PARMS represents only (the innermost) vector
9156 of parameters, this function returns a vector containing just the
9157 innermost resulting arguments.
9158
9159 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
9160 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
9161 arguments not specified in ARGS must have default arguments which
9162 we'll use to fill in ARGS. */
9163
9164 tree
9165 coerce_template_parms (tree parms,
9166 tree args,
9167 tree in_decl,
9168 tsubst_flags_t complain,
9169 bool require_all_args /* = true */)
9170 {
9171 int nparms, nargs, parm_idx, arg_idx, lost = 0;
9172 tree orig_inner_args;
9173 tree inner_args;
9174
9175 /* When used as a boolean value, indicates whether this is a
9176 variadic template parameter list. Since it's an int, we can also
9177 subtract it from nparms to get the number of non-variadic
9178 parameters. */
9179 int variadic_p = 0;
9180 int variadic_args_p = 0;
9181 int post_variadic_parms = 0;
9182
9183 /* Adjustment to nparms for fixed parameter packs. */
9184 int fixed_pack_adjust = 0;
9185 int fixed_packs = 0;
9186 int missing = 0;
9187
9188 /* Likewise for parameters with default arguments. */
9189 int default_p = 0;
9190
9191 if (args == error_mark_node)
9192 return error_mark_node;
9193
9194 bool return_full_args = false;
9195 if (TREE_CODE (parms) == TREE_LIST)
9196 {
9197 if (TMPL_PARMS_DEPTH (parms) > 1)
9198 {
9199 gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
9200 return_full_args = true;
9201 }
9202 parms = INNERMOST_TEMPLATE_PARMS (parms);
9203 }
9204
9205 nparms = TREE_VEC_LENGTH (parms);
9206
9207 /* Determine if there are any parameter packs or default arguments. */
9208 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
9209 {
9210 tree parm = TREE_VEC_ELT (parms, parm_idx);
9211 if (variadic_p)
9212 ++post_variadic_parms;
9213 if (template_parameter_pack_p (TREE_VALUE (parm)))
9214 ++variadic_p;
9215 if (TREE_PURPOSE (parm))
9216 ++default_p;
9217 }
9218
9219 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
9220 /* If there are no parameters that follow a parameter pack, we need to
9221 expand any argument packs so that we can deduce a parameter pack from
9222 some non-packed args followed by an argument pack, as in variadic85.C.
9223 If there are such parameters, we need to leave argument packs intact
9224 so the arguments are assigned properly. This can happen when dealing
9225 with a nested class inside a partial specialization of a class
9226 template, as in variadic92.C, or when deducing a template parameter pack
9227 from a sub-declarator, as in variadic114.C. */
9228 if (!post_variadic_parms)
9229 inner_args = expand_template_argument_pack (inner_args);
9230
9231 /* Count any pack expansion args. */
9232 variadic_args_p = pack_expansion_args_count (inner_args);
9233
9234 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
9235 if ((nargs - variadic_args_p > nparms && !variadic_p)
9236 || (nargs < nparms - variadic_p
9237 && require_all_args
9238 && !variadic_args_p
9239 && (TREE_VEC_ELT (parms, nargs) != error_mark_node
9240 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
9241 {
9242 bad_nargs:
9243 if (complain & tf_error)
9244 {
9245 auto_diagnostic_group d;
9246 if (variadic_p || default_p)
9247 {
9248 nparms -= variadic_p + default_p;
9249 error ("wrong number of template arguments "
9250 "(%d, should be at least %d)", nargs, nparms);
9251 }
9252 else
9253 error ("wrong number of template arguments "
9254 "(%d, should be %d)", nargs, nparms);
9255
9256 if (in_decl)
9257 inform (DECL_SOURCE_LOCATION (in_decl),
9258 "provided for %qD", in_decl);
9259 }
9260
9261 return error_mark_node;
9262 }
9263 /* We can't pass a pack expansion to a non-pack parameter of an alias
9264 template (DR 1430). */
9265 else if (in_decl
9266 && (DECL_ALIAS_TEMPLATE_P (in_decl)
9267 || concept_definition_p (in_decl))
9268 && variadic_args_p
9269 && nargs - variadic_args_p < nparms - variadic_p)
9270 {
9271 if (complain & tf_error)
9272 {
9273 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
9274 {
9275 tree arg = TREE_VEC_ELT (inner_args, i);
9276 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
9277
9278 if (PACK_EXPANSION_P (arg)
9279 && !template_parameter_pack_p (parm))
9280 {
9281 auto_diagnostic_group d;
9282 if (DECL_ALIAS_TEMPLATE_P (in_decl))
9283 error_at (location_of (arg),
9284 "pack expansion argument for non-pack parameter "
9285 "%qD of alias template %qD", parm, in_decl);
9286 else
9287 error_at (location_of (arg),
9288 "pack expansion argument for non-pack parameter "
9289 "%qD of concept %qD", parm, in_decl);
9290 inform (DECL_SOURCE_LOCATION (parm), "declared here");
9291 goto found;
9292 }
9293 }
9294 gcc_unreachable ();
9295 found:;
9296 }
9297 return error_mark_node;
9298 }
9299
9300 /* We need to evaluate the template arguments, even though this
9301 template-id may be nested within a "sizeof". */
9302 cp_evaluated ev;
9303
9304 tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
9305 tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
9306 int pack_adjust = 0;
9307 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
9308 {
9309 tree arg;
9310 tree parm;
9311
9312 /* Get the Ith template parameter. */
9313 parm = TREE_VEC_ELT (parms, parm_idx);
9314
9315 if (parm == error_mark_node)
9316 {
9317 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
9318 continue;
9319 }
9320
9321 /* Calculate the next argument. */
9322 if (arg_idx < nargs)
9323 arg = TREE_VEC_ELT (inner_args, arg_idx);
9324 else
9325 arg = NULL_TREE;
9326
9327 if (template_parameter_pack_p (TREE_VALUE (parm))
9328 && (arg || require_all_args || !(complain & tf_partial))
9329 && !(arg && ARGUMENT_PACK_P (arg)))
9330 {
9331 /* Some arguments will be placed in the
9332 template parameter pack PARM. */
9333 arg = coerce_template_parameter_pack (parms, parm_idx, args,
9334 inner_args, arg_idx,
9335 new_args, &lost,
9336 in_decl, complain);
9337
9338 if (arg == NULL_TREE)
9339 {
9340 /* We don't know how many args we have yet, just use the
9341 unconverted (and still packed) ones for now. */
9342 ggc_free (new_inner_args);
9343 new_inner_args = strip_typedefs (orig_inner_args,
9344 /*remove_attrs=*/nullptr,
9345 STF_KEEP_INJ_CLASS_NAME);
9346 arg_idx = nargs;
9347 break;
9348 }
9349
9350 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
9351
9352 /* Store this argument. */
9353 if (arg == error_mark_node)
9354 {
9355 lost++;
9356 /* We are done with all of the arguments. */
9357 arg_idx = nargs;
9358 break;
9359 }
9360 else
9361 {
9362 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
9363 arg_idx += pack_adjust;
9364 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
9365 {
9366 ++fixed_packs;
9367 fixed_pack_adjust += pack_adjust;
9368 }
9369 }
9370
9371 continue;
9372 }
9373 else if (arg)
9374 {
9375 if (PACK_EXPANSION_P (arg))
9376 {
9377 /* "If every valid specialization of a variadic template
9378 requires an empty template parameter pack, the template is
9379 ill-formed, no diagnostic required." So check that the
9380 pattern works with this parameter. */
9381 tree pattern = PACK_EXPANSION_PATTERN (arg);
9382 tree conv = convert_template_argument (TREE_VALUE (parm),
9383 pattern, new_args,
9384 complain, parm_idx,
9385 in_decl);
9386 if (conv == error_mark_node)
9387 {
9388 if (complain & tf_error)
9389 inform (input_location, "so any instantiation with a "
9390 "non-empty parameter pack would be ill-formed");
9391 ++lost;
9392 }
9393 else if (TYPE_P (conv) && !TYPE_P (pattern))
9394 /* Recover from missing typename. */
9395 TREE_VEC_ELT (inner_args, arg_idx)
9396 = make_pack_expansion (conv, complain);
9397
9398 /* We don't know how many args we have yet, just
9399 use the unconverted (but unpacked) ones for now. */
9400 ggc_free (new_inner_args);
9401 new_inner_args = strip_typedefs (inner_args,
9402 /*remove_attrs=*/nullptr,
9403 STF_KEEP_INJ_CLASS_NAME);
9404 arg_idx = nargs;
9405 break;
9406 }
9407 }
9408 else if (require_all_args)
9409 {
9410 /* There must be a default arg in this case. */
9411 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9412 complain | (processing_template_decl
9413 ? tf_partial : tf_none),
9414 in_decl);
9415 /* The position of the first default template argument,
9416 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9417 Record that. */
9418 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9419 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9420 arg_idx - pack_adjust);
9421 }
9422 else
9423 break;
9424
9425 if (arg == error_mark_node)
9426 {
9427 if (complain & tf_error)
9428 error ("template argument %d is invalid", arg_idx + 1);
9429 }
9430 else if (!arg)
9431 {
9432 /* This can occur if there was an error in the template
9433 parameter list itself (which we would already have
9434 reported) that we are trying to recover from, e.g., a class
9435 template with a parameter list such as
9436 template<typename..., typename> (cpp0x/variadic150.C). */
9437 ++lost;
9438
9439 /* This can also happen with a fixed parameter pack (71834). */
9440 if (arg_idx >= nargs)
9441 ++missing;
9442 }
9443 else
9444 arg = convert_template_argument (TREE_VALUE (parm),
9445 arg, new_args, complain,
9446 parm_idx, in_decl);
9447
9448 if (arg == error_mark_node)
9449 lost++;
9450
9451 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9452 }
9453
9454 if (missing || arg_idx < nargs - variadic_args_p)
9455 {
9456 /* If we had fixed parameter packs, we didn't know how many arguments we
9457 actually needed earlier; now we do. */
9458 nparms += fixed_pack_adjust;
9459 variadic_p -= fixed_packs;
9460 goto bad_nargs;
9461 }
9462
9463 if (arg_idx < nargs)
9464 {
9465 /* We had some pack expansion arguments that will only work if the packs
9466 are empty, but wait until instantiation time to complain.
9467 See variadic-ttp3.C. */
9468
9469 /* Except that we can't provide empty packs to alias templates or
9470 concepts when there are no corresponding parameters. Basically,
9471 we can get here with this:
9472
9473 template<typename T> concept C = true;
9474
9475 template<typename... Args>
9476 requires C<Args...>
9477 void f();
9478
9479 When parsing C<Args...>, we try to form a concept check of
9480 C<?, Args...>. Without the extra check for substituting an empty
9481 pack past the last parameter, we can accept the check as valid.
9482
9483 FIXME: This may be valid for alias templates (but I doubt it).
9484
9485 FIXME: The error could be better also. */
9486 if (in_decl && concept_definition_p (in_decl))
9487 {
9488 if (complain & tf_error)
9489 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9490 "too many arguments");
9491 return error_mark_node;
9492 }
9493
9494 int len = nparms + (nargs - arg_idx);
9495 tree args = make_tree_vec (len);
9496 int i = 0;
9497 for (; i < nparms; ++i)
9498 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9499 for (; i < len; ++i, ++arg_idx)
9500 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9501 arg_idx - pack_adjust);
9502 new_inner_args = args;
9503 }
9504
9505 if (lost)
9506 {
9507 gcc_assert (!(complain & tf_error) || seen_error ());
9508 return error_mark_node;
9509 }
9510
9511 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9512 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9513 TREE_VEC_LENGTH (new_inner_args));
9514
9515 /* If we expanded packs in inner_args and aren't returning it now, the
9516 expanded vec is garbage. */
9517 if (inner_args != new_inner_args
9518 && inner_args != orig_inner_args)
9519 ggc_free (inner_args);
9520
9521 return return_full_args ? new_args : new_inner_args;
9522 }
9523
9524 /* Returns true if T is a wrapper to make a C++20 template parameter
9525 object const. */
9526
9527 static bool
9528 class_nttp_const_wrapper_p (tree t)
9529 {
9530 if (cxx_dialect < cxx20)
9531 return false;
9532 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9533 && CP_TYPE_CONST_P (TREE_TYPE (t))
9534 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9535 }
9536
9537 /* Returns 1 if template args OT and NT are equivalent. */
9538
9539 int
9540 template_args_equal (tree ot, tree nt)
9541 {
9542 if (nt == ot)
9543 return 1;
9544 if (nt == NULL_TREE || ot == NULL_TREE)
9545 return false;
9546 if (nt == any_targ_node || ot == any_targ_node)
9547 return true;
9548
9549 if (class_nttp_const_wrapper_p (nt))
9550 nt = TREE_OPERAND (nt, 0);
9551 if (class_nttp_const_wrapper_p (ot))
9552 ot = TREE_OPERAND (ot, 0);
9553
9554 /* DR 1558: Don't treat an alias template specialization with dependent
9555 arguments as equivalent to its underlying type when used as a template
9556 argument; we need them to be distinct so that we substitute into the
9557 specialization arguments at instantiation time. And aliases can't be
9558 equivalent without being ==, so we don't need to look any deeper.
9559
9560 During partial ordering, however, we need to treat them normally so we can
9561 order uses of the same alias with different cv-qualification (79960). */
9562 auto cso = make_temp_override (comparing_dependent_aliases);
9563 if (!comparing_for_partial_ordering)
9564 ++comparing_dependent_aliases;
9565
9566 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9567 /* For member templates */
9568 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9569 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9570 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9571 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9572 PACK_EXPANSION_PATTERN (nt))
9573 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9574 PACK_EXPANSION_EXTRA_ARGS (nt)));
9575 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9576 return cp_tree_equal (ot, nt);
9577 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9578 gcc_unreachable ();
9579 else if (TYPE_P (nt) || TYPE_P (ot))
9580 {
9581 if (!(TYPE_P (nt) && TYPE_P (ot)))
9582 return false;
9583 return same_type_p (ot, nt);
9584 }
9585 else
9586 {
9587 /* Try to treat a template non-type argument that has been converted
9588 to the parameter type as equivalent to one that hasn't yet. */
9589 for (enum tree_code code1 = TREE_CODE (ot);
9590 CONVERT_EXPR_CODE_P (code1)
9591 || code1 == NON_LVALUE_EXPR;
9592 code1 = TREE_CODE (ot))
9593 ot = TREE_OPERAND (ot, 0);
9594
9595 for (enum tree_code code2 = TREE_CODE (nt);
9596 CONVERT_EXPR_CODE_P (code2)
9597 || code2 == NON_LVALUE_EXPR;
9598 code2 = TREE_CODE (nt))
9599 nt = TREE_OPERAND (nt, 0);
9600
9601 return cp_tree_equal (ot, nt);
9602 }
9603 }
9604
9605 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9606 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9607 NEWARG_PTR with the offending arguments if they are non-NULL. */
9608
9609 bool
9610 comp_template_args (tree oldargs, tree newargs,
9611 tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
9612 {
9613 if (oldargs == newargs)
9614 return true;
9615
9616 if (!oldargs || !newargs)
9617 return false;
9618
9619 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9620 return false;
9621
9622 for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9623 {
9624 tree nt = TREE_VEC_ELT (newargs, i);
9625 tree ot = TREE_VEC_ELT (oldargs, i);
9626
9627 if (! template_args_equal (ot, nt))
9628 {
9629 if (oldarg_ptr != NULL)
9630 *oldarg_ptr = ot;
9631 if (newarg_ptr != NULL)
9632 *newarg_ptr = nt;
9633 return false;
9634 }
9635 }
9636 return true;
9637 }
9638
9639 static bool
9640 comp_template_args_porder (tree oargs, tree nargs)
9641 {
9642 ++comparing_for_partial_ordering;
9643 bool equal = comp_template_args (oargs, nargs);
9644 --comparing_for_partial_ordering;
9645 return equal;
9646 }
9647
9648 /* Implement a freelist interface for objects of type T.
9649
9650 Head is a separate object, rather than a regular member, so that we
9651 can define it as a GTY deletable pointer, which is highly
9652 desirable. A data member could be declared that way, but then the
9653 containing object would implicitly get GTY((user)), which would
9654 prevent us from instantiating freelists as global objects.
9655 Although this way we can create freelist global objects, they're
9656 such thin wrappers that instantiating temporaries at every use
9657 loses nothing and saves permanent storage for the freelist object.
9658
9659 Member functions next, anew, poison and reinit have default
9660 implementations that work for most of the types we're interested
9661 in, but if they don't work for some type, they should be explicitly
9662 specialized. See the comments before them for requirements, and
9663 the example specializations for the tree_list_freelist. */
9664 template <typename T>
9665 class freelist
9666 {
9667 /* Return the next object in a chain. We could just do type
9668 punning, but if we access the object with its underlying type, we
9669 avoid strict-aliasing trouble. This needs only work between
9670 poison and reinit. */
9671 static T *&next (T *obj) { return obj->next; }
9672
9673 /* Return a newly allocated, uninitialized or minimally-initialized
9674 object of type T. Any initialization performed by anew should
9675 either remain across the life of the object and the execution of
9676 poison, or be redone by reinit. */
9677 static T *anew () { return ggc_alloc<T> (); }
9678
9679 /* Optionally scribble all over the bits holding the object, so that
9680 they become (mostly?) uninitialized memory. This is called while
9681 preparing to make the object part of the free list. */
9682 static void poison (T *obj) {
9683 T *p ATTRIBUTE_UNUSED = obj;
9684 T **q ATTRIBUTE_UNUSED = &next (obj);
9685
9686 #ifdef ENABLE_GC_CHECKING
9687 /* Poison the data, to indicate the data is garbage. */
9688 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9689 memset (p, 0xa5, sizeof (*p));
9690 #endif
9691 /* Let valgrind know the object is free. */
9692 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9693
9694 /* Let valgrind know the next portion of the object is available,
9695 but uninitialized. */
9696 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9697 }
9698
9699 /* Bring an object that underwent at least one lifecycle after anew
9700 and before the most recent free and poison, back to a usable
9701 state, reinitializing whatever is needed for it to be
9702 functionally equivalent to an object just allocated and returned
9703 by anew. This may poison or clear the next field, used by
9704 freelist housekeeping after poison was called. */
9705 static void reinit (T *obj) {
9706 T **q ATTRIBUTE_UNUSED = &next (obj);
9707
9708 #ifdef ENABLE_GC_CHECKING
9709 memset (q, 0xa5, sizeof (*q));
9710 #endif
9711 /* Let valgrind know the entire object is available, but
9712 uninitialized. */
9713 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9714 }
9715
9716 /* Reference a GTY-deletable pointer that points to the first object
9717 in the free list proper. */
9718 T *&head;
9719 public:
9720 /* Construct a freelist object chaining objects off of HEAD. */
9721 freelist (T *&head) : head(head) {}
9722
9723 /* Add OBJ to the free object list. The former head becomes OBJ's
9724 successor. */
9725 void free (T *obj)
9726 {
9727 poison (obj);
9728 next (obj) = head;
9729 head = obj;
9730 }
9731
9732 /* Take an object from the free list, if one is available, or
9733 allocate a new one. Objects taken from the free list should be
9734 regarded as filled with garbage, except for bits that are
9735 configured to be preserved across free and alloc. */
9736 T *alloc ()
9737 {
9738 if (head)
9739 {
9740 T *obj = head;
9741 head = next (head);
9742 reinit (obj);
9743 return obj;
9744 }
9745 else
9746 return anew ();
9747 }
9748 };
9749
9750 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9751 want to allocate a TREE_LIST using the usual interface, and ensure
9752 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9753 build_tree_list logic in reinit, so this could go out of sync. */
9754 template <>
9755 inline tree &
9756 freelist<tree_node>::next (tree obj)
9757 {
9758 return TREE_CHAIN (obj);
9759 }
9760 template <>
9761 inline tree
9762 freelist<tree_node>::anew ()
9763 {
9764 return build_tree_list (NULL, NULL);
9765 }
9766 template <>
9767 inline void
9768 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9769 {
9770 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9771 tree p ATTRIBUTE_UNUSED = obj;
9772 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9773 tree *q ATTRIBUTE_UNUSED = &next (obj);
9774
9775 #ifdef ENABLE_GC_CHECKING
9776 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9777
9778 /* Poison the data, to indicate the data is garbage. */
9779 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9780 memset (p, 0xa5, size);
9781 #endif
9782 /* Let valgrind know the object is free. */
9783 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9784 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9785 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9786 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9787
9788 #ifdef ENABLE_GC_CHECKING
9789 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9790 /* Keep TREE_CHAIN functional. */
9791 TREE_SET_CODE (obj, TREE_LIST);
9792 #else
9793 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9794 #endif
9795 }
9796 template <>
9797 inline void
9798 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9799 {
9800 tree_common *c ATTRIBUTE_UNUSED = &obj->common;
9801
9802 #ifdef ENABLE_GC_CHECKING
9803 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9804 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9805 memset (obj, 0, sizeof (tree_list));
9806 #endif
9807
9808 /* Let valgrind know the entire object is available, but
9809 uninitialized. */
9810 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9811
9812 #ifdef ENABLE_GC_CHECKING
9813 TREE_SET_CODE (obj, TREE_LIST);
9814 #else
9815 TREE_CHAIN (obj) = NULL_TREE;
9816 TREE_TYPE (obj) = NULL_TREE;
9817 #endif
9818 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
9819 }
9820
9821 /* Point to the first object in the TREE_LIST freelist. */
9822 static GTY((deletable)) tree tree_list_freelist_head;
9823 /* Return the/an actual TREE_LIST freelist. */
9824 static inline freelist<tree_node>
9825 tree_list_freelist ()
9826 {
9827 return tree_list_freelist_head;
9828 }
9829
9830 /* Point to the first object in the tinst_level freelist. */
9831 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9832 /* Return the/an actual tinst_level freelist. */
9833 static inline freelist<tinst_level>
9834 tinst_level_freelist ()
9835 {
9836 return tinst_level_freelist_head;
9837 }
9838
9839 /* Point to the first object in the pending_template freelist. */
9840 static GTY((deletable)) pending_template *pending_template_freelist_head;
9841 /* Return the/an actual pending_template freelist. */
9842 static inline freelist<pending_template>
9843 pending_template_freelist ()
9844 {
9845 return pending_template_freelist_head;
9846 }
9847
9848 /* Build the TREE_LIST object out of a split list, store it
9849 permanently, and return it. */
9850 tree
9851 tinst_level::to_list ()
9852 {
9853 gcc_assert (split_list_p ());
9854 tree ret = tree_list_freelist ().alloc ();
9855 TREE_PURPOSE (ret) = tldcl;
9856 TREE_VALUE (ret) = targs;
9857 tldcl = ret;
9858 targs = NULL;
9859 gcc_assert (tree_list_p ());
9860 return ret;
9861 }
9862
9863 const unsigned short tinst_level::refcount_infinity;
9864
9865 /* Increment OBJ's refcount unless it is already infinite. */
9866 static tinst_level *
9867 inc_refcount_use (tinst_level *obj)
9868 {
9869 if (obj && obj->refcount != tinst_level::refcount_infinity)
9870 ++obj->refcount;
9871 return obj;
9872 }
9873
9874 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9875 void
9876 tinst_level::free (tinst_level *obj)
9877 {
9878 if (obj->tree_list_p ())
9879 tree_list_freelist ().free (obj->get_node ());
9880 tinst_level_freelist ().free (obj);
9881 }
9882
9883 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9884 OBJ's DECL and OBJ, and start over with the tinst_level object that
9885 used to be referenced by OBJ's NEXT. */
9886 static void
9887 dec_refcount_use (tinst_level *obj)
9888 {
9889 while (obj
9890 && obj->refcount != tinst_level::refcount_infinity
9891 && !--obj->refcount)
9892 {
9893 tinst_level *next = obj->next;
9894 tinst_level::free (obj);
9895 obj = next;
9896 }
9897 }
9898
9899 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9900 and of the former PTR. Omitting the second argument is equivalent
9901 to passing (T*)NULL; this is allowed because passing the
9902 zero-valued integral constant NULL confuses type deduction and/or
9903 overload resolution. */
9904 template <typename T>
9905 static void
9906 set_refcount_ptr (T *& ptr, T *obj = NULL)
9907 {
9908 T *save = ptr;
9909 ptr = inc_refcount_use (obj);
9910 dec_refcount_use (save);
9911 }
9912
9913 static void
9914 add_pending_template (tree d)
9915 {
9916 tree ti = (TYPE_P (d)
9917 ? CLASSTYPE_TEMPLATE_INFO (d)
9918 : DECL_TEMPLATE_INFO (d));
9919 struct pending_template *pt;
9920 int level;
9921
9922 if (TI_PENDING_TEMPLATE_FLAG (ti))
9923 return;
9924
9925 /* We are called both from instantiate_decl, where we've already had a
9926 tinst_level pushed, and instantiate_template, where we haven't.
9927 Compensate. */
9928 gcc_assert (TREE_CODE (d) != TREE_LIST);
9929 level = !current_tinst_level
9930 || current_tinst_level->maybe_get_node () != d;
9931
9932 if (level)
9933 push_tinst_level (d);
9934
9935 pt = pending_template_freelist ().alloc ();
9936 pt->next = NULL;
9937 pt->tinst = NULL;
9938 set_refcount_ptr (pt->tinst, current_tinst_level);
9939 if (last_pending_template)
9940 last_pending_template->next = pt;
9941 else
9942 pending_templates = pt;
9943
9944 last_pending_template = pt;
9945
9946 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9947
9948 if (level)
9949 pop_tinst_level ();
9950 }
9951
9952 /* Emit a diagnostic about instantiating a reference to TU-local entity E. */
9953
9954 static void
9955 complain_about_tu_local_entity (tree e)
9956 {
9957 auto_diagnostic_group d;
9958 error ("instantiation exposes TU-local entity %qD",
9959 TU_LOCAL_ENTITY_NAME (e));
9960 inform (TU_LOCAL_ENTITY_LOCATION (e), "declared here");
9961 }
9962
9963 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9964 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9965 documentation for TEMPLATE_ID_EXPR. */
9966
9967 tree
9968 lookup_template_function (tree fns, tree arglist)
9969 {
9970 if (fns == error_mark_node || arglist == error_mark_node)
9971 return error_mark_node;
9972
9973 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9974
9975 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9976 {
9977 error ("%q#D is not a function template", fns);
9978 return error_mark_node;
9979 }
9980
9981 if (BASELINK_P (fns))
9982 {
9983 fns = copy_node (fns);
9984 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9985 unknown_type_node,
9986 BASELINK_FUNCTIONS (fns),
9987 arglist);
9988 return fns;
9989 }
9990
9991 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9992 }
9993
9994 /* Within the scope of a template class S<T>, the name S gets bound
9995 (in build_self_reference) to a TYPE_DECL for the class, not a
9996 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9997 or one of its enclosing classes, and that type is a template,
9998 return the associated TEMPLATE_DECL. Otherwise, the original
9999 DECL is returned.
10000
10001 Also handle the case when DECL is a TREE_LIST of ambiguous
10002 injected-class-names from different bases. */
10003
10004 tree
10005 maybe_get_template_decl_from_type_decl (tree decl)
10006 {
10007 if (decl == NULL_TREE)
10008 return decl;
10009
10010 /* DR 176: A lookup that finds an injected-class-name (10.2
10011 [class.member.lookup]) can result in an ambiguity in certain cases
10012 (for example, if it is found in more than one base class). If all of
10013 the injected-class-names that are found refer to specializations of
10014 the same class template, and if the name is followed by a
10015 template-argument-list, the reference refers to the class template
10016 itself and not a specialization thereof, and is not ambiguous. */
10017 if (TREE_CODE (decl) == TREE_LIST)
10018 {
10019 tree t, tmpl = NULL_TREE;
10020 for (t = decl; t; t = TREE_CHAIN (t))
10021 {
10022 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
10023 if (!tmpl)
10024 tmpl = elt;
10025 else if (tmpl != elt)
10026 break;
10027 }
10028 if (tmpl && t == NULL_TREE)
10029 return tmpl;
10030 else
10031 return decl;
10032 }
10033
10034 return (decl != NULL_TREE
10035 && DECL_SELF_REFERENCE_P (decl)
10036 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
10037 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
10038 }
10039
10040 /* If TYPE is the generic implicit instantiation A<T>, return the primary
10041 template type A<T> (which is suitable for entering into, e.g. for defining
10042 a member of A<T>), otherwise return TYPE. */
10043
10044 tree
10045 adjust_type_for_entering_scope (tree type)
10046 {
10047 if (CLASS_TYPE_P (type)
10048 && dependent_type_p (type)
10049 && TYPE_TEMPLATE_INFO (type)
10050 /* We detect the generic implicit instantiation A<T> by inspecting
10051 TYPE_CANONICAL, which lookup_template_class sets to the primary
10052 template type A<T>. */
10053 && TYPE_CANONICAL (type) == TREE_TYPE (TYPE_TI_TEMPLATE (type)))
10054 type = TYPE_CANONICAL (type);
10055
10056 return type;
10057 }
10058
10059 /* Convenience wrapper over tsubst that does adjust_type_for_entering_scope
10060 on the result. */
10061
10062 static tree
10063 tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10064 {
10065 t = tsubst (t, args, complain, in_decl);
10066 return adjust_type_for_entering_scope (t);
10067 }
10068
10069 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
10070 parameters, find the desired type.
10071
10072 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
10073
10074 If D1 is an identifier and CONTEXT is non-NULL, then the lookup is
10075 carried out in CONTEXT. Currently, only namespaces are supported for
10076 CONTEXT.
10077
10078 If D1 is an identifier and CONTEXT is NULL, the lookup is performed
10079 in the innermost non-namespace binding.
10080
10081 Otherwise CONTEXT is ignored and no lookup is carried out.
10082
10083 IN_DECL, if non-NULL, is the template declaration we are trying to
10084 instantiate.
10085
10086 Issue error and warning messages under control of COMPLAIN.
10087
10088 ??? Note that this function is currently called *twice* for each
10089 template-id: the first time from the parser, while creating the
10090 incomplete type (finish_template_type), and the second type during the
10091 real instantiation (instantiate_template_class). This is surely something
10092 that we want to avoid. It also causes some problems with argument
10093 coercion (see convert_nontype_argument for more information on this). */
10094
10095 tree
10096 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10097 tsubst_flags_t complain)
10098 {
10099 auto_timevar tv (TV_TEMPLATE_INST);
10100
10101 tree templ = NULL_TREE, parmlist;
10102 tree t;
10103 spec_entry **slot;
10104 spec_entry *entry;
10105
10106 if (identifier_p (d1) && context)
10107 {
10108 gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL);
10109 push_decl_namespace (context);
10110 templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL);
10111 pop_decl_namespace ();
10112 }
10113 else if (identifier_p (d1))
10114 {
10115 tree value = innermost_non_namespace_value (d1);
10116 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
10117 templ = value;
10118 else
10119 {
10120 templ = lookup_name (d1);
10121 templ = maybe_get_template_decl_from_type_decl (templ);
10122 }
10123 }
10124 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
10125 {
10126 tree type = TREE_TYPE (d1);
10127
10128 /* If we are declaring a constructor, say A<T>::A<T>, we will get
10129 an implicit typename for the second A. Deal with it. */
10130 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
10131 type = TREE_TYPE (type);
10132
10133 if (CLASSTYPE_TEMPLATE_INFO (type))
10134 {
10135 templ = CLASSTYPE_TI_TEMPLATE (type);
10136 d1 = DECL_NAME (templ);
10137 }
10138 }
10139 else if (TREE_CODE (d1) == ENUMERAL_TYPE
10140 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
10141 {
10142 templ = TYPE_TI_TEMPLATE (d1);
10143 d1 = DECL_NAME (templ);
10144 }
10145 else if (DECL_TYPE_TEMPLATE_P (d1))
10146 {
10147 templ = d1;
10148 d1 = DECL_NAME (templ);
10149 }
10150 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
10151 {
10152 templ = d1;
10153 d1 = DECL_NAME (templ);
10154 }
10155
10156 /* Issue an error message if we didn't find a template. */
10157 if (! templ)
10158 {
10159 if (complain & tf_error)
10160 error ("%qT is not a template", d1);
10161 return error_mark_node;
10162 }
10163
10164 if (TREE_CODE (templ) != TEMPLATE_DECL
10165 /* Make sure it's a user visible template, if it was named by
10166 the user. */
10167 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
10168 && !PRIMARY_TEMPLATE_P (templ)))
10169 {
10170 if (complain & tf_error)
10171 {
10172 error ("non-template type %qT used as a template", d1);
10173 if (in_decl)
10174 error ("for template declaration %q+D", in_decl);
10175 }
10176 return error_mark_node;
10177 }
10178
10179 complain &= ~tf_user;
10180
10181 /* An alias that just changes the name of a template is equivalent to the
10182 other template, so if any of the arguments are pack expansions, strip
10183 the alias to avoid problems with a pack expansion passed to a non-pack
10184 alias template parameter (DR 1430). */
10185 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
10186 templ = get_underlying_template (templ);
10187
10188 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10189 {
10190 tree parm;
10191 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
10192 if (arglist2 == error_mark_node
10193 || (!uses_template_parms (arglist2)
10194 && check_instantiated_args (templ, arglist2, complain)))
10195 return error_mark_node;
10196
10197 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
10198 return parm;
10199 }
10200 else
10201 {
10202 tree template_type = TREE_TYPE (templ);
10203 tree gen_tmpl;
10204 tree type_decl;
10205 tree found = NULL_TREE;
10206 int arg_depth;
10207 int parm_depth;
10208 int is_dependent_type;
10209 int use_partial_inst_tmpl = false;
10210
10211 if (template_type == error_mark_node)
10212 /* An error occurred while building the template TEMPL, and a
10213 diagnostic has most certainly been emitted for that
10214 already. Let's propagate that error. */
10215 return error_mark_node;
10216
10217 gen_tmpl = most_general_template (templ);
10218 if (modules_p ())
10219 lazy_load_pendings (gen_tmpl);
10220
10221 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
10222 parm_depth = TMPL_PARMS_DEPTH (parmlist);
10223 arg_depth = TMPL_ARGS_DEPTH (arglist);
10224
10225 if (arg_depth == 1 && parm_depth > 1)
10226 {
10227 /* We've been given an incomplete set of template arguments.
10228 For example, given:
10229
10230 template <class T> struct S1 {
10231 template <class U> struct S2 {};
10232 template <class U> struct S2<U*> {};
10233 };
10234
10235 we will be called with an ARGLIST of `U*', but the
10236 TEMPLATE will be `template <class T> template
10237 <class U> struct S1<T>::S2'. We must fill in the missing
10238 arguments. */
10239 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
10240 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
10241 arg_depth = TMPL_ARGS_DEPTH (arglist);
10242 }
10243
10244 /* Now we should have enough arguments. */
10245 gcc_assert (parm_depth == arg_depth);
10246
10247 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10248 {
10249 /* The user referred to a specialization of an alias
10250 template represented by GEN_TMPL.
10251
10252 [temp.alias]/2 says:
10253
10254 When a template-id refers to the specialization of an
10255 alias template, it is equivalent to the associated
10256 type obtained by substitution of its
10257 template-arguments for the template-parameters in the
10258 type-id of the alias template. */
10259
10260 t = instantiate_alias_template (gen_tmpl, arglist, complain);
10261 /* Note that the call above (by indirectly calling
10262 register_specialization in tsubst_decl) registers the
10263 TYPE_DECL representing the specialization of the alias
10264 template. So next time someone substitutes ARGLIST for
10265 the template parms into the alias template (GEN_TMPL),
10266 she'll get that TYPE_DECL back. */
10267
10268 if (t == error_mark_node)
10269 return error_mark_node;
10270 return TREE_TYPE (t);
10271 }
10272
10273 /* From here on, we're only interested in the most general
10274 template. */
10275
10276 /* Shortcut looking up the current class scope again. */
10277 for (tree cur = current_nonlambda_class_type ();
10278 cur != NULL_TREE;
10279 cur = get_containing_scope (cur))
10280 {
10281 if (!CLASS_TYPE_P (cur))
10282 continue;
10283
10284 tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
10285 if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
10286 break;
10287
10288 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
10289 && comp_template_args (arglist, TI_ARGS (ti)))
10290 return cur;
10291 }
10292
10293 /* Calculate the BOUND_ARGS. These will be the args that are
10294 actually tsubst'd into the definition to create the
10295 instantiation. */
10296 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10297 arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
10298
10299 if (arglist == error_mark_node)
10300 /* We were unable to bind the arguments. */
10301 return error_mark_node;
10302
10303 /* In the scope of a template class, explicit references to the
10304 template class refer to the type of the template, not any
10305 instantiation of it. For example, in:
10306
10307 template <class T> class C { void f(C<T>); }
10308
10309 the `C<T>' is just the same as `C'. Outside of the
10310 class, however, such a reference is an instantiation.
10311 One can use adjust_type_for_entering_scope to make
10312 this adjustment as needed. */
10313 if (!PRIMARY_TEMPLATE_P (gen_tmpl)
10314 || currently_open_class (template_type))
10315 {
10316 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
10317
10318 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
10319 return template_type;
10320 }
10321
10322 /* If we already have this specialization, return it. */
10323 spec_entry elt;
10324 elt.tmpl = gen_tmpl;
10325 elt.args = arglist;
10326 entry = type_specializations->find (&elt);
10327
10328 if (entry)
10329 return entry->spec;
10330
10331 /* If the template's constraints are not satisfied,
10332 then we cannot form a valid type.
10333
10334 Note that the check is deferred until after the hash
10335 lookup. This prevents redundant checks on previously
10336 instantiated specializations. */
10337 if (flag_concepts
10338 && !constraints_satisfied_p (gen_tmpl, arglist))
10339 {
10340 if (complain & tf_error)
10341 {
10342 auto_diagnostic_group d;
10343 error ("template constraint failure for %qD", gen_tmpl);
10344 diagnose_constraints (input_location, gen_tmpl, arglist);
10345 }
10346 return error_mark_node;
10347 }
10348
10349 is_dependent_type = uses_template_parms (arglist);
10350
10351 /* If the deduced arguments are invalid, then the binding
10352 failed. */
10353 if (!is_dependent_type
10354 && check_instantiated_args (gen_tmpl,
10355 INNERMOST_TEMPLATE_ARGS (arglist),
10356 complain))
10357 return error_mark_node;
10358
10359 if (!is_dependent_type
10360 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10361 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
10362 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
10363 /* This occurs when the user has tried to define a tagged type
10364 in a scope that forbids it. We emitted an error during the
10365 parse. We didn't complete the bail out then, so here we
10366 are. */
10367 return error_mark_node;
10368
10369 context = DECL_CONTEXT (gen_tmpl);
10370 if (context && TYPE_P (context))
10371 {
10372 if (!uses_template_parms (DECL_CONTEXT (templ)))
10373 /* If the context of the partially instantiated template is
10374 already non-dependent, then we might as well use it. */
10375 context = DECL_CONTEXT (templ);
10376 else
10377 {
10378 context = tsubst_entering_scope (context, arglist,
10379 complain, in_decl);
10380 /* Try completing the enclosing context if it's not already so. */
10381 if (context != error_mark_node
10382 && !COMPLETE_TYPE_P (context))
10383 {
10384 context = complete_type (context);
10385 if (COMPLETE_TYPE_P (context))
10386 {
10387 /* Completion could have caused us to register the desired
10388 specialization already, so check the table again. */
10389 entry = type_specializations->find (&elt);
10390 if (entry)
10391 return entry->spec;
10392 }
10393 }
10394 }
10395 }
10396 else
10397 context = tsubst (context, arglist, complain, in_decl);
10398
10399 if (context == error_mark_node)
10400 return error_mark_node;
10401
10402 if (!context)
10403 context = global_namespace;
10404
10405 /* Create the type. */
10406 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10407 {
10408 if (!is_dependent_type)
10409 {
10410 set_current_access_from_decl (TYPE_NAME (template_type));
10411 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10412 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10413 arglist, complain, in_decl),
10414 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10415 arglist, complain, in_decl),
10416 SCOPED_ENUM_P (template_type), NULL);
10417
10418 if (t == error_mark_node)
10419 return t;
10420 }
10421 else
10422 {
10423 /* We don't want to call start_enum for this type, since
10424 the values for the enumeration constants may involve
10425 template parameters. And, no one should be interested
10426 in the enumeration constants for such a type. */
10427 t = cxx_make_type (ENUMERAL_TYPE);
10428 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10429 }
10430 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10431 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10432 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10433 }
10434 else if (CLASS_TYPE_P (template_type))
10435 {
10436 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10437 instantiated here. */
10438 gcc_assert (!LAMBDA_TYPE_P (template_type));
10439
10440 t = make_class_type (TREE_CODE (template_type));
10441 CLASSTYPE_DECLARED_CLASS (t)
10442 = CLASSTYPE_DECLARED_CLASS (template_type);
10443 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10444
10445 /* A local class. Make sure the decl gets registered properly. */
10446 if (context == current_function_decl)
10447 if (pushtag (DECL_NAME (gen_tmpl), t)
10448 == error_mark_node)
10449 return error_mark_node;
10450
10451 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10452 /* This instantiation is another name for the primary
10453 template type. Set the TYPE_CANONICAL field
10454 appropriately. */
10455 TYPE_CANONICAL (t) = template_type;
10456 else if (any_template_arguments_need_structural_equality_p (arglist))
10457 SET_TYPE_STRUCTURAL_EQUALITY (t);
10458 }
10459 else
10460 gcc_unreachable ();
10461
10462 /* If we called start_enum or pushtag above, this information
10463 will already be set up. */
10464 type_decl = TYPE_NAME (t);
10465 if (!type_decl)
10466 {
10467 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10468
10469 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10470 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10471 DECL_SOURCE_LOCATION (type_decl)
10472 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10473 }
10474
10475 set_instantiating_module (type_decl);
10476 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10477 of export flag. We want to propagate this because it might
10478 be a friend declaration that pushes a new hidden binding. */
10479 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10480
10481 if (CLASS_TYPE_P (template_type))
10482 {
10483 TREE_PRIVATE (type_decl)
10484 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10485 TREE_PROTECTED (type_decl)
10486 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10487 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10488 {
10489 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10490 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10491 }
10492 }
10493
10494 if (OVERLOAD_TYPE_P (t))
10495 {
10496 static const char *tags[] = {"abi_tag", "may_alias"};
10497
10498 for (unsigned ix = 0; ix != 2; ix++)
10499 {
10500 tree attributes
10501 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10502
10503 if (attributes)
10504 TYPE_ATTRIBUTES (t)
10505 = tree_cons (TREE_PURPOSE (attributes),
10506 TREE_VALUE (attributes),
10507 TYPE_ATTRIBUTES (t));
10508 }
10509 }
10510
10511 /* Let's consider the explicit specialization of a member
10512 of a class template specialization that is implicitly instantiated,
10513 e.g.:
10514 template<class T>
10515 struct S
10516 {
10517 template<class U> struct M {}; //#0
10518 };
10519
10520 template<>
10521 template<>
10522 struct S<int>::M<char> //#1
10523 {
10524 int i;
10525 };
10526 [temp.expl.spec]/4 says this is valid.
10527
10528 In this case, when we write:
10529 S<int>::M<char> m;
10530
10531 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10532 the one of #0.
10533
10534 When we encounter #1, we want to store the partial instantiation
10535 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10536
10537 For all cases other than this "explicit specialization of member of a
10538 class template", we just want to store the most general template into
10539 the CLASSTYPE_TI_TEMPLATE of M.
10540
10541 This case of "explicit specialization of member of a class template"
10542 only happens when:
10543 1/ the enclosing class is an instantiation of, and therefore not
10544 the same as, the context of the most general template, and
10545 2/ we aren't looking at the partial instantiation itself, i.e.
10546 the innermost arguments are not the same as the innermost parms of
10547 the most general template.
10548
10549 So it's only when 1/ and 2/ happens that we want to use the partial
10550 instantiation of the member template in lieu of its most general
10551 template. */
10552
10553 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10554 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10555 /* the enclosing class must be an instantiation... */
10556 && CLASS_TYPE_P (context)
10557 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10558 {
10559 TREE_VEC_LENGTH (arglist)--;
10560 ++processing_template_decl;
10561 tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
10562 tree partial_inst_args =
10563 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10564 arglist, complain, NULL_TREE);
10565 --processing_template_decl;
10566 TREE_VEC_LENGTH (arglist)++;
10567 if (partial_inst_args == error_mark_node)
10568 return error_mark_node;
10569 use_partial_inst_tmpl =
10570 /*...and we must not be looking at the partial instantiation
10571 itself. */
10572 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10573 partial_inst_args);
10574 }
10575
10576 if (!use_partial_inst_tmpl)
10577 /* This case is easy; there are no member templates involved. */
10578 found = gen_tmpl;
10579 else
10580 {
10581 /* This is a full instantiation of a member template. Find
10582 the partial instantiation of which this is an instance. */
10583
10584 /* Temporarily reduce by one the number of levels in the ARGLIST
10585 so as to avoid comparing the last set of arguments. */
10586 TREE_VEC_LENGTH (arglist)--;
10587 /* We don't use COMPLAIN in the following call because this isn't
10588 the immediate context of deduction. For instance, tf_partial
10589 could be set here as we might be at the beginning of template
10590 argument deduction when any explicitly specified template
10591 arguments are substituted into the function type. tf_partial
10592 could lead into trouble because we wouldn't find the partial
10593 instantiation that might have been created outside tf_partial
10594 context, because the levels of template parameters wouldn't
10595 match, because in a tf_partial context, tsubst doesn't reduce
10596 TEMPLATE_PARM_LEVEL. */
10597 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10598 TREE_VEC_LENGTH (arglist)++;
10599 found = (TREE_CODE (found) == TEMPLATE_DECL
10600 ? found
10601 : CLASSTYPE_TI_TEMPLATE (found));
10602
10603 if (DECL_CLASS_TEMPLATE_P (found)
10604 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10605 {
10606 /* If this partial instantiation is specialized, we want to
10607 use it for hash table lookup. */
10608 elt.tmpl = found;
10609 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10610 elt.hash = 0; /* Recalculate after changing tmpl/args. */
10611 }
10612 }
10613
10614 /* Build template info for the new specialization. */
10615 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10616
10617 elt.spec = t;
10618 slot = type_specializations->find_slot (&elt, INSERT);
10619 gcc_checking_assert (*slot == NULL);
10620 entry = ggc_alloc<spec_entry> ();
10621 *entry = elt;
10622 *slot = entry;
10623
10624 /* Note this use of the partial instantiation so we can check it
10625 later in maybe_process_partial_specialization. */
10626 DECL_TEMPLATE_INSTANTIATIONS (found)
10627 = tree_cons (arglist, t,
10628 DECL_TEMPLATE_INSTANTIATIONS (found));
10629
10630 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10631 && !uses_template_parms (current_nonlambda_scope ()))
10632 /* Now that the type has been registered on the instantiations
10633 list, we set up the enumerators. Because the enumeration
10634 constants may involve the enumeration type itself, we make
10635 sure to register the type first, and then create the
10636 constants. That way, doing tsubst_expr for the enumeration
10637 constants won't result in recursive calls here; we'll find
10638 the instantiation and exit above. */
10639 tsubst_enum (template_type, t, arglist);
10640
10641 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10642 /* If the type makes use of template parameters, the
10643 code that generates debugging information will crash. */
10644 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10645
10646 /* Possibly limit visibility based on template args. */
10647 TREE_PUBLIC (type_decl) = 1;
10648 determine_visibility (type_decl);
10649
10650 inherit_targ_abi_tags (t);
10651
10652 return t;
10653 }
10654 }
10655
10656 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10657
10658 tree
10659 lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
10660 {
10661 tree gen_templ = most_general_template (templ);
10662 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
10663 arglist = add_outermost_template_args (templ, arglist);
10664 arglist = coerce_template_parms (parms, arglist, templ, complain);
10665 if (arglist == error_mark_node)
10666 return error_mark_node;
10667
10668 /* The type of the expression is NULL_TREE since the template-id could refer
10669 to an explicit or partial specialization. */
10670 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10671 }
10672
10673 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
10674 not dependent. */
10675
10676 tree
10677 finish_template_variable (tree var, tsubst_flags_t complain)
10678 {
10679 tree templ = TREE_OPERAND (var, 0);
10680 tree arglist = TREE_OPERAND (var, 1);
10681
10682 /* If the template or arguments are dependent, then we
10683 can't resolve the TEMPLATE_ID_EXPR yet. */
10684 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
10685 || any_dependent_template_arguments_p (arglist))
10686 return var;
10687
10688 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10689 {
10690 if (complain & tf_error)
10691 {
10692 auto_diagnostic_group d;
10693 error ("use of invalid variable template %qE", var);
10694 diagnose_constraints (location_of (var), templ, arglist);
10695 }
10696 return error_mark_node;
10697 }
10698
10699 return instantiate_template (templ, arglist, complain);
10700 }
10701
10702 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10703 TARGS template args, and instantiate it if it's not dependent. */
10704
10705 tree
10706 lookup_and_finish_template_variable (tree templ, tree targs,
10707 tsubst_flags_t complain)
10708 {
10709 tree var = lookup_template_variable (templ, targs, complain);
10710 if (var == error_mark_node)
10711 return error_mark_node;
10712 var = finish_template_variable (var, complain);
10713 mark_used (var, complain);
10714 return var;
10715 }
10716
10717 /* If the set of template parameters PARMS contains a template parameter
10718 at the given LEVEL and INDEX, then return this parameter. Otherwise
10719 return NULL_TREE. */
10720
10721 static tree
10722 corresponding_template_parameter_list (tree parms, int level, int index)
10723 {
10724 while (TMPL_PARMS_DEPTH (parms) > level)
10725 parms = TREE_CHAIN (parms);
10726
10727 if (TMPL_PARMS_DEPTH (parms) != level
10728 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10729 return NULL_TREE;
10730
10731 return TREE_VEC_ELT (TREE_VALUE (parms), index);
10732 }
10733
10734 /* Return the TREE_LIST for the template parameter from PARMS that positionally
10735 corresponds to the template parameter PARM, or else return NULL_TREE. */
10736
10737 static tree
10738 corresponding_template_parameter_list (tree parms, tree parm)
10739 {
10740 int level, index;
10741 template_parm_level_and_index (parm, &level, &index);
10742 return corresponding_template_parameter_list (parms, level, index);
10743 }
10744
10745 /* As above, but pull out the actual parameter. */
10746
10747 static tree
10748 corresponding_template_parameter (tree parms, tree parm)
10749 {
10750 tree list = corresponding_template_parameter_list (parms, parm);
10751 if (!list)
10752 return NULL_TREE;
10753
10754 tree t = TREE_VALUE (list);
10755 /* As in template_parm_to_arg. */
10756 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10757 t = TREE_TYPE (t);
10758 else
10759 t = DECL_INITIAL (t);
10760
10761 gcc_assert (TEMPLATE_PARM_P (t));
10762 return t;
10763 }
10764
10765 struct pair_fn_data
10766 {
10767 tree_fn_t fn;
10768 tree_fn_t any_fn;
10769 void *data;
10770 /* True when we should also visit template parameters that occur in
10771 non-deduced contexts. */
10772 bool include_nondeduced_p;
10773 hash_set<tree> *visited;
10774 };
10775
10776 /* Called from for_each_template_parm via walk_tree. */
10777
10778 static tree
10779 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10780 {
10781 tree t = *tp;
10782 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10783 tree_fn_t fn = pfd->fn;
10784 void *data = pfd->data;
10785 tree result = NULL_TREE;
10786
10787 #define WALK_SUBTREE(NODE) \
10788 do \
10789 { \
10790 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10791 pfd->include_nondeduced_p, \
10792 pfd->any_fn); \
10793 if (result) goto out; \
10794 } \
10795 while (0)
10796
10797 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10798 return t;
10799
10800 if (TYPE_P (t)
10801 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10802 WALK_SUBTREE (TYPE_CONTEXT (t));
10803
10804 switch (TREE_CODE (t))
10805 {
10806 case RECORD_TYPE:
10807 if (TYPE_PTRMEMFUNC_P (t))
10808 break;
10809 /* Fall through. */
10810
10811 case UNION_TYPE:
10812 case ENUMERAL_TYPE:
10813 if (!TYPE_TEMPLATE_INFO (t))
10814 *walk_subtrees = 0;
10815 else
10816 WALK_SUBTREE (TYPE_TI_ARGS (t));
10817 break;
10818
10819 case METHOD_TYPE:
10820 /* Since we're not going to walk subtrees, we have to do this
10821 explicitly here. */
10822 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10823 /* Fall through. */
10824
10825 case FUNCTION_TYPE:
10826 /* Check the return type. */
10827 WALK_SUBTREE (TREE_TYPE (t));
10828
10829 /* Check the parameter types. Since default arguments are not
10830 instantiated until they are needed, the TYPE_ARG_TYPES may
10831 contain expressions that involve template parameters. But,
10832 no-one should be looking at them yet. And, once they're
10833 instantiated, they don't contain template parameters, so
10834 there's no point in looking at them then, either. */
10835 {
10836 tree parm;
10837
10838 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10839 WALK_SUBTREE (TREE_VALUE (parm));
10840
10841 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10842 want walk_tree walking into them itself. */
10843 *walk_subtrees = 0;
10844 }
10845
10846 if (flag_noexcept_type)
10847 {
10848 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10849 if (spec)
10850 WALK_SUBTREE (TREE_PURPOSE (spec));
10851 }
10852 break;
10853
10854 case TYPEOF_TYPE:
10855 case DECLTYPE_TYPE:
10856 if (pfd->include_nondeduced_p
10857 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10858 pfd->visited,
10859 pfd->include_nondeduced_p,
10860 pfd->any_fn))
10861 return error_mark_node;
10862 *walk_subtrees = false;
10863 break;
10864
10865 case TRAIT_TYPE:
10866 if (pfd->include_nondeduced_p)
10867 {
10868 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
10869 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
10870 }
10871 *walk_subtrees = false;
10872 break;
10873
10874 case FUNCTION_DECL:
10875 case VAR_DECL:
10876 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10877 WALK_SUBTREE (DECL_TI_ARGS (t));
10878 break;
10879
10880 case PARM_DECL:
10881 WALK_SUBTREE (TREE_TYPE (t));
10882 break;
10883
10884 case CONST_DECL:
10885 if (DECL_TEMPLATE_PARM_P (t))
10886 WALK_SUBTREE (DECL_INITIAL (t));
10887 if (DECL_CONTEXT (t)
10888 && pfd->include_nondeduced_p)
10889 WALK_SUBTREE (DECL_CONTEXT (t));
10890 break;
10891
10892 case BOUND_TEMPLATE_TEMPLATE_PARM:
10893 /* Record template parameters such as `T' inside `TT<T>'. */
10894 WALK_SUBTREE (TYPE_TI_ARGS (t));
10895 /* Fall through. */
10896
10897 case TEMPLATE_TEMPLATE_PARM:
10898 case TEMPLATE_TYPE_PARM:
10899 case TEMPLATE_PARM_INDEX:
10900 if (fn && (*fn)(t, data))
10901 return t;
10902 else if (!fn)
10903 return t;
10904 break;
10905
10906 case TEMPLATE_DECL:
10907 /* A template template parameter is encountered. */
10908 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10909 WALK_SUBTREE (TREE_TYPE (t));
10910
10911 /* Already substituted template template parameter */
10912 *walk_subtrees = 0;
10913 break;
10914
10915 case TYPENAME_TYPE:
10916 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10917 partial instantiation. */
10918 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10919 *walk_subtrees = 0;
10920 break;
10921
10922 case INDIRECT_REF:
10923 case COMPONENT_REF:
10924 /* If there's no type, then this thing must be some expression
10925 involving template parameters. */
10926 if (!fn && !TREE_TYPE (t))
10927 return error_mark_node;
10928 break;
10929
10930 case CONSTRUCTOR:
10931 case TRAIT_EXPR:
10932 case PLUS_EXPR:
10933 case MULT_EXPR:
10934 case SCOPE_REF:
10935 /* These are non-deduced contexts. */
10936 if (!pfd->include_nondeduced_p)
10937 *walk_subtrees = 0;
10938 break;
10939
10940 case MODOP_EXPR:
10941 case CAST_EXPR:
10942 case IMPLICIT_CONV_EXPR:
10943 case REINTERPRET_CAST_EXPR:
10944 case CONST_CAST_EXPR:
10945 case STATIC_CAST_EXPR:
10946 case DYNAMIC_CAST_EXPR:
10947 case ARROW_EXPR:
10948 case DOTSTAR_EXPR:
10949 case TYPEID_EXPR:
10950 case PSEUDO_DTOR_EXPR:
10951 if (!fn)
10952 return error_mark_node;
10953 break;
10954
10955 default:
10956 break;
10957 }
10958
10959 #undef WALK_SUBTREE
10960
10961 /* We didn't find any template parameters we liked. */
10962 out:
10963 return result;
10964 }
10965
10966 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10967 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10968 call FN with the parameter and the DATA.
10969 If FN returns nonzero, the iteration is terminated, and
10970 for_each_template_parm returns 1. Otherwise, the iteration
10971 continues. If FN never returns a nonzero value, the value
10972 returned by for_each_template_parm is 0. If FN is NULL, it is
10973 considered to be the function which always returns 1.
10974
10975 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10976 parameters that occur in non-deduced contexts. When false, only
10977 visits those template parameters that can be deduced. */
10978
10979 static tree
10980 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10981 hash_set<tree> *visited,
10982 bool include_nondeduced_p,
10983 tree_fn_t any_fn)
10984 {
10985 struct pair_fn_data pfd;
10986 tree result;
10987
10988 /* Set up. */
10989 pfd.fn = fn;
10990 pfd.any_fn = any_fn;
10991 pfd.data = data;
10992 pfd.include_nondeduced_p = include_nondeduced_p;
10993
10994 /* Walk the tree. (Conceptually, we would like to walk without
10995 duplicates, but for_each_template_parm_r recursively calls
10996 for_each_template_parm, so we would need to reorganize a fair
10997 bit to use walk_tree_without_duplicates, so we keep our own
10998 visited list.) */
10999 if (visited)
11000 pfd.visited = visited;
11001 else
11002 pfd.visited = new hash_set<tree>;
11003 result = cp_walk_tree (&t,
11004 for_each_template_parm_r,
11005 &pfd,
11006 pfd.visited);
11007
11008 /* Clean up. */
11009 if (!visited)
11010 {
11011 delete pfd.visited;
11012 pfd.visited = 0;
11013 }
11014
11015 return result;
11016 }
11017
11018 struct find_template_parameter_info
11019 {
11020 explicit find_template_parameter_info (tree ctx_parms)
11021 : ctx_parms (ctx_parms),
11022 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
11023 {}
11024
11025 hash_set<tree> visited;
11026 hash_set<tree> parms;
11027 tree parm_list = NULL_TREE;
11028 tree *parm_list_tail = &parm_list;
11029 tree ctx_parms;
11030 int max_depth;
11031
11032 tree find_in (tree);
11033 tree find_in_recursive (tree);
11034 bool found (tree);
11035 unsigned num_found () { return parms.elements (); }
11036 };
11037
11038 /* Appends the declaration of T to the list in DATA. */
11039
11040 static int
11041 keep_template_parm (tree t, void* data)
11042 {
11043 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
11044
11045 /* Template parameters declared within the expression are not part of
11046 the parameter mapping. For example, in this concept:
11047
11048 template<typename T>
11049 concept C = requires { <expr> } -> same_as<int>;
11050
11051 the return specifier same_as<int> declares a new decltype parameter
11052 that must not be part of the parameter mapping. The same is true
11053 for generic lambda parameters, lambda template parameters, etc. */
11054 int level;
11055 int index;
11056 template_parm_level_and_index (t, &level, &index);
11057 if (level == 0 || level > ftpi->max_depth)
11058 return 0;
11059
11060 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
11061 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
11062 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
11063 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
11064
11065 /* This template parameter might be an argument to a cached dependent
11066 specalization that was formed earlier inside some other template, in
11067 which case the parameter is not among the ones that are in-scope.
11068 Look in CTX_PARMS to find the corresponding in-scope template
11069 parameter, and use it instead. */
11070 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
11071 t = in_scope;
11072
11073 /* Arguments like const T yield parameters like const T. This means that
11074 a template-id like X<T, const T> would yield two distinct parameters:
11075 T and const T. Adjust types to their unqualified versions. */
11076 if (TYPE_P (t))
11077 t = TYPE_MAIN_VARIANT (t);
11078 if (!ftpi->parms.add (t))
11079 {
11080 /* Append T to PARM_LIST. */
11081 tree node = build_tree_list (NULL_TREE, t);
11082 *ftpi->parm_list_tail = node;
11083 ftpi->parm_list_tail = &TREE_CHAIN (node);
11084 }
11085
11086 /* Verify the parameter we found has a valid index. */
11087 if (flag_checking)
11088 {
11089 tree parms = ftpi->ctx_parms;
11090 while (TMPL_PARMS_DEPTH (parms) > level)
11091 parms = TREE_CHAIN (parms);
11092 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
11093 gcc_assert (index < len);
11094 }
11095
11096 return 0;
11097 }
11098
11099 /* Ensure that we recursively examine certain terms that are not normally
11100 visited in for_each_template_parm_r. */
11101
11102 static int
11103 any_template_parm_r (tree t, void *data)
11104 {
11105 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
11106
11107 #define WALK_SUBTREE(NODE) \
11108 do \
11109 { \
11110 for_each_template_parm (NODE, keep_template_parm, data, \
11111 &ftpi->visited, true, \
11112 any_template_parm_r); \
11113 } \
11114 while (0)
11115
11116 /* A mention of a member alias/typedef is a use of all of its template
11117 arguments, including those from the enclosing class, so we don't use
11118 alias_template_specialization_p here. */
11119 if (TYPE_P (t) && typedef_variant_p (t))
11120 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
11121 WALK_SUBTREE (TI_ARGS (tinfo));
11122
11123 switch (TREE_CODE (t))
11124 {
11125 case TEMPLATE_TYPE_PARM:
11126 /* Type constraints of a placeholder type may contain parameters. */
11127 if (is_auto (t))
11128 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
11129 WALK_SUBTREE (constr);
11130 break;
11131
11132 case TEMPLATE_ID_EXPR:
11133 /* Search through references to variable templates. */
11134 WALK_SUBTREE (TREE_OPERAND (t, 0));
11135 WALK_SUBTREE (TREE_OPERAND (t, 1));
11136 break;
11137
11138 case TEMPLATE_PARM_INDEX:
11139 /* No need to consider template parameters within the type of an NTTP:
11140 substitution into an NTTP is done directly with the corresponding
11141 template argument, and its type only comes into play earlier during
11142 coercion. */
11143 break;
11144
11145 case TEMPLATE_DECL:
11146 /* If T is a member template that shares template parameters with
11147 ctx_parms, we need to mark all those parameters for mapping.
11148 To that end, it should suffice to just walk the DECL_CONTEXT of
11149 the template (assuming the template is not overly general). */
11150 WALK_SUBTREE (DECL_CONTEXT (t));
11151 break;
11152
11153 case LAMBDA_EXPR:
11154 {
11155 /* TREE_STATIC on LAMBDA_EXPR_EXTRA_ARGS means a full set of
11156 arguments, so we can just look there; they will replace
11157 any template parms in the rest of the LAMBDA_EXPR. */
11158 if (tree args = LAMBDA_EXPR_EXTRA_ARGS (t))
11159 {
11160 WALK_SUBTREE (args);
11161 /* Without TREE_STATIC the args are just outer levels, so we'd
11162 still need to look through the lambda for just inner
11163 parameters. Hopefully that's not necessary. */
11164 gcc_checking_assert (TREE_STATIC (args));
11165 return 0;
11166 }
11167 /* Look in the parms and body. */
11168 tree fn = lambda_function (t);
11169 WALK_SUBTREE (TREE_TYPE (fn));
11170 WALK_SUBTREE (DECL_SAVED_TREE (fn));
11171 }
11172 break;
11173
11174 case IDENTIFIER_NODE:
11175 if (IDENTIFIER_CONV_OP_P (t))
11176 /* The conversion-type-id of a conversion operator may be dependent. */
11177 WALK_SUBTREE (TREE_TYPE (t));
11178 break;
11179
11180 case CONVERT_EXPR:
11181 if (is_dummy_object (t))
11182 WALK_SUBTREE (TREE_TYPE (t));
11183 break;
11184
11185 default:
11186 break;
11187 }
11188
11189 /* Keep walking. */
11190 return 0;
11191 }
11192
11193 /* Look through T for template parameters. */
11194
11195 tree
11196 find_template_parameter_info::find_in (tree t)
11197 {
11198 return for_each_template_parm (t, keep_template_parm, this, &visited,
11199 /*include_nondeduced*/true,
11200 any_template_parm_r);
11201 }
11202
11203 /* As above, but also recursively look into the default arguments of template
11204 parameters we found. Used for alias CTAD. */
11205
11206 tree
11207 find_template_parameter_info::find_in_recursive (tree t)
11208 {
11209 if (tree r = find_in (t))
11210 return r;
11211 /* Since newly found parms are added to the end of the list, we
11212 can just walk it until we reach the end. */
11213 for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
11214 {
11215 tree parm = TREE_VALUE (pl);
11216 tree list = corresponding_template_parameter_list (ctx_parms, parm);
11217 if (tree r = find_in (TREE_PURPOSE (list)))
11218 return r;
11219 }
11220 return NULL_TREE;
11221 }
11222
11223 /* True if PARM was found by a previous call to find_in. PARM can be a
11224 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */
11225
11226 bool
11227 find_template_parameter_info::found (tree parm)
11228 {
11229 if (TREE_CODE (parm) == TREE_LIST)
11230 parm = TREE_VALUE (parm);
11231 if (TREE_CODE (parm) == TYPE_DECL
11232 || TREE_CODE (parm) == TEMPLATE_DECL)
11233 parm = TREE_TYPE (parm);
11234 else
11235 parm = DECL_INITIAL (parm);
11236 gcc_checking_assert (TEMPLATE_PARM_P (parm));
11237 return parms.contains (parm);
11238 }
11239
11240 /* Returns a list of unique template parameters found within T, where CTX_PARMS
11241 are the template parameters in scope. */
11242
11243 tree
11244 find_template_parameters (tree t, tree ctx_parms)
11245 {
11246 if (!ctx_parms)
11247 return NULL_TREE;
11248
11249 find_template_parameter_info ftpi (ctx_parms);
11250 ftpi.find_in (t);
11251 return ftpi.parm_list;
11252 }
11253
11254 /* Returns true if T depends on any template parameter. */
11255
11256 bool
11257 uses_template_parms (tree t)
11258 {
11259 if (t == NULL_TREE || t == error_mark_node)
11260 return false;
11261
11262 /* Namespaces can't depend on any template parameters. */
11263 if (TREE_CODE (t) == NAMESPACE_DECL)
11264 return false;
11265
11266 processing_template_decl_sentinel ptds (/*reset*/false);
11267 ++processing_template_decl;
11268
11269 if (TYPE_P (t))
11270 return dependent_type_p (t);
11271 else if (TREE_CODE (t) == TREE_VEC)
11272 return any_dependent_template_arguments_p (t);
11273 else if (TREE_CODE (t) == TREE_LIST)
11274 return (uses_template_parms (TREE_VALUE (t))
11275 || uses_template_parms (TREE_CHAIN (t)));
11276 else if (TREE_CODE (t) == TYPE_DECL)
11277 return dependent_type_p (TREE_TYPE (t));
11278 else
11279 return instantiation_dependent_expression_p (t);
11280 }
11281
11282 /* Returns true if T depends on any template parameter with level LEVEL. */
11283
11284 bool
11285 uses_template_parms_level (tree t, int level)
11286 {
11287 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
11288 /*include_nondeduced_p=*/true);
11289 }
11290
11291 /* Returns true if the signature of DECL depends on any template parameter from
11292 its enclosing class. */
11293
11294 static bool
11295 uses_outer_template_parms (tree decl)
11296 {
11297 int depth;
11298 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11299 depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
11300 else
11301 depth = template_class_depth (CP_DECL_CONTEXT (decl));
11302 if (depth == 0)
11303 return false;
11304 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
11305 &depth, NULL, /*include_nondeduced_p=*/true))
11306 return true;
11307 if (PRIMARY_TEMPLATE_P (decl)
11308 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11309 {
11310 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
11311 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
11312 {
11313 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
11314 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
11315 if (TREE_CODE (parm) == PARM_DECL
11316 && for_each_template_parm (TREE_TYPE (parm),
11317 template_parm_outer_level,
11318 &depth, NULL, /*nondeduced*/true))
11319 return true;
11320 if (TREE_CODE (parm) == TEMPLATE_DECL
11321 && uses_outer_template_parms (parm))
11322 return true;
11323 if (defarg
11324 && for_each_template_parm (defarg, template_parm_outer_level,
11325 &depth, NULL, /*nondeduced*/true))
11326 return true;
11327 }
11328 }
11329 if (uses_outer_template_parms_in_constraints (decl))
11330 return true;
11331 return false;
11332 }
11333
11334 /* Returns true if the constraints of DECL depend on any template parameters
11335 from its enclosing scope. */
11336
11337 bool
11338 uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
11339 {
11340 tree ci = get_constraints (decl);
11341 if (ci)
11342 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
11343 if (!ci)
11344 return false;
11345 if (!ctx)
11346 {
11347 if (tree fc = DECL_FRIEND_CONTEXT (decl))
11348 ctx = fc;
11349 else
11350 ctx = CP_DECL_CONTEXT (decl);
11351 }
11352 int depth = template_class_depth (ctx);
11353 if (depth == 0)
11354 return false;
11355 return for_each_template_parm (ci, template_parm_outer_level,
11356 &depth, NULL, /*nondeduced*/true);
11357 }
11358
11359 /* Returns TRUE iff INST is an instantiation we don't need to do in an
11360 ill-formed translation unit, i.e. a variable or function that isn't
11361 usable in a constant expression. */
11362
11363 static inline bool
11364 neglectable_inst_p (tree d)
11365 {
11366 return (d && DECL_P (d)
11367 && !undeduced_auto_decl (d)
11368 && !(TREE_CODE (d) == FUNCTION_DECL
11369 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
11370 : decl_maybe_constant_var_p (d)));
11371 }
11372
11373 /* Returns TRUE iff we should refuse to instantiate DECL because it's
11374 neglectable and instantiated from within an erroneous instantiation. */
11375
11376 static bool
11377 limit_bad_template_recursion (tree decl)
11378 {
11379 struct tinst_level *lev = current_tinst_level;
11380 int errs = errorcount + sorrycount;
11381 if (errs == 0 || !neglectable_inst_p (decl))
11382 return false;
11383
11384 /* Avoid instantiating members of an ill-formed class. */
11385 bool refuse
11386 = (DECL_CLASS_SCOPE_P (decl)
11387 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
11388
11389 if (!refuse)
11390 {
11391 for (; lev; lev = lev->next)
11392 if (neglectable_inst_p (lev->maybe_get_node ()))
11393 break;
11394 refuse = (lev && errs > lev->errors);
11395 }
11396
11397 if (refuse)
11398 {
11399 /* Don't warn about it not being defined. */
11400 suppress_warning (decl, OPT_Wunused);
11401 tree clone;
11402 FOR_EACH_CLONE (clone, decl)
11403 suppress_warning (clone, OPT_Wunused);
11404 }
11405 return refuse;
11406 }
11407
11408 static int tinst_depth;
11409 extern int max_tinst_depth;
11410 int depth_reached;
11411
11412 static GTY(()) struct tinst_level *last_error_tinst_level;
11413
11414 /* We're starting to instantiate D; record the template instantiation context
11415 at LOC for diagnostics and to restore it later. */
11416
11417 bool
11418 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
11419 {
11420 struct tinst_level *new_level;
11421
11422 if (tinst_depth >= max_tinst_depth)
11423 {
11424 /* Tell error.cc not to try to instantiate any templates. */
11425 at_eof = 3;
11426 fatal_error (input_location,
11427 "template instantiation depth exceeds maximum of %d"
11428 " (use %<-ftemplate-depth=%> to increase the maximum)",
11429 max_tinst_depth);
11430 return false;
11431 }
11432
11433 /* If the current instantiation caused problems, don't let it instantiate
11434 anything else. Do allow deduction substitution and decls usable in
11435 constant expressions. */
11436 if (!targs && limit_bad_template_recursion (tldcl))
11437 {
11438 /* Avoid no_linkage_errors and unused function (and all other)
11439 warnings for this decl. */
11440 suppress_warning (tldcl);
11441 return false;
11442 }
11443
11444 /* When not -quiet, dump template instantiations other than functions, since
11445 announce_function will take care of those. */
11446 if (!quiet_flag && !targs
11447 && TREE_CODE (tldcl) != TREE_LIST
11448 && TREE_CODE (tldcl) != FUNCTION_DECL)
11449 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
11450
11451 new_level = tinst_level_freelist ().alloc ();
11452 new_level->tldcl = tldcl;
11453 new_level->targs = targs;
11454 new_level->locus = loc;
11455 new_level->errors = errorcount + sorrycount;
11456 new_level->next = NULL;
11457 new_level->refcount = 0;
11458 new_level->path = new_level->visible = nullptr;
11459 set_refcount_ptr (new_level->next, current_tinst_level);
11460 set_refcount_ptr (current_tinst_level, new_level);
11461
11462 ++tinst_depth;
11463 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11464 depth_reached = tinst_depth;
11465
11466 return true;
11467 }
11468
11469 /* We're starting substitution of TMPL<ARGS>; record the template
11470 substitution context for diagnostics and to restore it later. */
11471
11472 bool
11473 push_tinst_level (tree tmpl, tree args)
11474 {
11475 return push_tinst_level_loc (tmpl, args, input_location);
11476 }
11477
11478 /* We're starting to instantiate D; record INPUT_LOCATION and the
11479 template instantiation context for diagnostics and to restore it
11480 later. */
11481
11482 bool
11483 push_tinst_level (tree d)
11484 {
11485 return push_tinst_level_loc (d, input_location);
11486 }
11487
11488 /* Likewise, but record LOC as the program location. */
11489
11490 bool
11491 push_tinst_level_loc (tree d, location_t loc)
11492 {
11493 gcc_assert (TREE_CODE (d) != TREE_LIST);
11494 return push_tinst_level_loc (d, NULL, loc);
11495 }
11496
11497 /* We're done instantiating this template; return to the instantiation
11498 context. */
11499
11500 void
11501 pop_tinst_level (void)
11502 {
11503 /* Restore the filename and line number stashed away when we started
11504 this instantiation. */
11505 input_location = current_tinst_level->locus;
11506 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11507 --tinst_depth;
11508 }
11509
11510 /* We're instantiating a deferred template; restore the template
11511 instantiation context in which the instantiation was requested, which
11512 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11513
11514 static tree
11515 reopen_tinst_level (struct tinst_level *level)
11516 {
11517 struct tinst_level *t;
11518
11519 tinst_depth = 0;
11520 for (t = level; t; t = t->next)
11521 ++tinst_depth;
11522
11523 set_refcount_ptr (current_tinst_level, level);
11524 pop_tinst_level ();
11525 if (current_tinst_level)
11526 current_tinst_level->errors = errorcount+sorrycount;
11527
11528 tree decl = level->maybe_get_node ();
11529 if (decl && modules_p ())
11530 {
11531 /* An instantiation is in module purview only if it had an explicit
11532 instantiation definition in module purview; mark_decl_instantiated uses
11533 set_instantiating_module to set the flag in that case. */
11534 if (DECL_MODULE_PURVIEW_P (decl))
11535 module_kind |= MK_PURVIEW;
11536 else
11537 module_kind &= ~MK_PURVIEW;
11538 }
11539 return decl;
11540 }
11541
11542 /* Returns the TINST_LEVEL which gives the original instantiation
11543 context. */
11544
11545 struct tinst_level *
11546 outermost_tinst_level (void)
11547 {
11548 struct tinst_level *level = current_tinst_level;
11549 if (level)
11550 while (level->next)
11551 level = level->next;
11552 return level;
11553 }
11554
11555 /* True iff T is a friend function declaration that is not itself a template
11556 and is not defined in a class template. */
11557
11558 bool
11559 non_templated_friend_p (tree t)
11560 {
11561 if (t && TREE_CODE (t) == FUNCTION_DECL
11562 && DECL_UNIQUE_FRIEND_P (t))
11563 {
11564 tree ti = DECL_TEMPLATE_INFO (t);
11565 if (!ti)
11566 return true;
11567 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11568 if (DECL_FRIEND_CONTEXT (t))
11569 return false;
11570 /* Non-templated friends in a class template are still represented with a
11571 TEMPLATE_DECL; check that its primary template is the befriending
11572 class. Note that DECL_PRIMARY_TEMPLATE is null for
11573 template <class T> friend A<T>::f(); */
11574 tree tmpl = TI_TEMPLATE (ti);
11575 tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
11576 return (primary && primary != tmpl);
11577 }
11578 else
11579 return false;
11580 }
11581
11582 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11583 vector of template arguments, as for tsubst.
11584
11585 Returns an appropriate tsubst'd friend declaration. */
11586
11587 static tree
11588 tsubst_friend_function (tree decl, tree args)
11589 {
11590 tree new_friend;
11591
11592 if (TREE_CODE (decl) == FUNCTION_DECL
11593 && DECL_TEMPLATE_INSTANTIATION (decl)
11594 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11595 /* This was a friend declared with an explicit template
11596 argument list, e.g.:
11597
11598 friend void f<>(T);
11599
11600 to indicate that f was a template instantiation, not a new
11601 function declaration. Now, we have to figure out what
11602 instantiation of what template. */
11603 {
11604 tree template_id, arglist, fns;
11605 tree new_args;
11606 tree tmpl;
11607 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11608
11609 /* Friend functions are looked up in the containing namespace scope.
11610 We must enter that scope, to avoid finding member functions of the
11611 current class with same name. */
11612 push_nested_namespace (ns);
11613 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11614 tf_warning_or_error, NULL_TREE);
11615 pop_nested_namespace (ns);
11616 arglist = tsubst (DECL_TI_ARGS (decl), args,
11617 tf_warning_or_error, NULL_TREE);
11618 template_id = lookup_template_function (fns, arglist);
11619
11620 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11621 tmpl = determine_specialization (template_id, new_friend,
11622 &new_args,
11623 /*need_member_template=*/0,
11624 TREE_VEC_LENGTH (args),
11625 tsk_none);
11626 return instantiate_template (tmpl, new_args, tf_error);
11627 }
11628
11629 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11630 if (new_friend == error_mark_node)
11631 return error_mark_node;
11632
11633 /* The NEW_FRIEND will look like an instantiation, to the
11634 compiler, but is not an instantiation from the point of view of
11635 the language. For example, we might have had:
11636
11637 template <class T> struct S {
11638 template <class U> friend void f(T, U);
11639 };
11640
11641 Then, in S<int>, template <class U> void f(int, U) is not an
11642 instantiation of anything. */
11643
11644 DECL_USE_TEMPLATE (new_friend) = 0;
11645 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11646 {
11647 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11648 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11649 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11650 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11651
11652 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11653 match in decls_match. */
11654 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11655 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11656 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11657 if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
11658 {
11659 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11660 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11661 tsubst_each_template_parm_constraints (parms, args,
11662 tf_warning_or_error);
11663 }
11664 }
11665
11666 /* The mangled name for the NEW_FRIEND is incorrect. The function
11667 is not a template instantiation and should not be mangled like
11668 one. Therefore, we forget the mangling here; we'll recompute it
11669 later if we need it. */
11670 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11671 {
11672 SET_DECL_RTL (new_friend, NULL);
11673 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11674 }
11675
11676 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11677 {
11678 tree old_decl;
11679 tree ns;
11680
11681 /* We must save some information from NEW_FRIEND before calling
11682 duplicate decls since that function will free NEW_FRIEND if
11683 possible. */
11684 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11685 tree new_friend_result_template_info = NULL_TREE;
11686 bool new_friend_is_defn =
11687 (new_friend_template_info
11688 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11689 (template_for_substitution (new_friend)))
11690 != NULL_TREE));
11691 tree not_tmpl = new_friend;
11692
11693 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11694 {
11695 /* This declaration is a `primary' template. */
11696 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11697
11698 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11699 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11700 }
11701
11702 /* We need to propagate module attachment for the new friend from the
11703 owner of this template. */
11704 propagate_defining_module (new_friend, decl);
11705
11706 /* Inside pushdecl_namespace_level, we will push into the
11707 current namespace. However, the friend function should go
11708 into the namespace of the template. */
11709 ns = decl_namespace_context (new_friend);
11710 push_nested_namespace (ns);
11711 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11712 pop_nested_namespace (ns);
11713
11714 if (old_decl == error_mark_node)
11715 return error_mark_node;
11716
11717 if (old_decl != new_friend)
11718 {
11719 /* This new friend declaration matched an existing
11720 declaration. For example, given:
11721
11722 template <class T> void f(T);
11723 template <class U> class C {
11724 template <class T> friend void f(T) {}
11725 };
11726
11727 the friend declaration actually provides the definition
11728 of `f', once C has been instantiated for some type. So,
11729 old_decl will be the out-of-class template declaration,
11730 while new_friend is the in-class definition.
11731
11732 But, if `f' was called before this point, the
11733 instantiation of `f' will have DECL_TI_ARGS corresponding
11734 to `T' but not to `U', references to which might appear
11735 in the definition of `f'. Previously, the most general
11736 template for an instantiation of `f' was the out-of-class
11737 version; now it is the in-class version. Therefore, we
11738 run through all specialization of `f', adding to their
11739 DECL_TI_ARGS appropriately. In particular, they need a
11740 new set of outer arguments, corresponding to the
11741 arguments for this class instantiation.
11742
11743 The same situation can arise with something like this:
11744
11745 friend void f(int);
11746 template <class T> class C {
11747 friend void f(T) {}
11748 };
11749
11750 when `C<int>' is instantiated. Now, `f(int)' is defined
11751 in the class. */
11752
11753 if (!new_friend_is_defn)
11754 /* On the other hand, if the in-class declaration does
11755 *not* provide a definition, then we don't want to alter
11756 existing definitions. We can just leave everything
11757 alone. */
11758 ;
11759 else
11760 {
11761 tree old_template = most_general_template (old_decl);
11762 tree new_template = TI_TEMPLATE (new_friend_template_info);
11763 tree new_args = TI_ARGS (new_friend_template_info);
11764
11765 /* Overwrite whatever template info was there before, if
11766 any, with the new template information pertaining to
11767 the declaration. */
11768 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11769
11770 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11771 {
11772 /* We should have called reregister_specialization in
11773 duplicate_decls. */
11774 gcc_assert (retrieve_specialization (new_template,
11775 new_args, 0)
11776 == old_decl);
11777
11778 /* Instantiate it if the global has already been used. */
11779 if (DECL_ODR_USED (old_decl))
11780 instantiate_decl (old_decl, /*defer_ok=*/true,
11781 /*expl_inst_class_mem_p=*/false);
11782 }
11783 else
11784 {
11785 tree t;
11786
11787 /* Indicate that the old function template is a partial
11788 instantiation. */
11789 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11790 = new_friend_result_template_info;
11791
11792 gcc_assert (new_template
11793 == most_general_template (new_template));
11794 gcc_assert (new_template != old_decl);
11795
11796 /* Reassign any specializations already in the hash table
11797 to the new more general template, and add the
11798 additional template args. */
11799 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template);
11800 t != NULL_TREE;
11801 t = TREE_CHAIN (t))
11802 {
11803 tree spec = TREE_VALUE (t);
11804 spec_entry elt;
11805
11806 elt.tmpl = old_decl;
11807 elt.args = DECL_TI_ARGS (spec);
11808 elt.spec = NULL_TREE;
11809
11810 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec))
11811 && !is_specialization_of_friend (spec, new_template))
11812 continue;
11813
11814 decl_specializations->remove_elt (&elt);
11815
11816 tree& spec_args = DECL_TI_ARGS (spec);
11817 spec_args = add_outermost_template_args
11818 (new_args, INNERMOST_TEMPLATE_ARGS (spec_args));
11819
11820 register_specialization
11821 (spec, new_template, spec_args, true, 0);
11822
11823 }
11824 DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE;
11825 }
11826 }
11827
11828 /* The information from NEW_FRIEND has been merged into OLD_DECL
11829 by duplicate_decls. */
11830 new_friend = old_decl;
11831 }
11832
11833 /* We've just introduced a namespace-scope function in the purview
11834 without necessarily having opened the enclosing namespace, so
11835 make sure the namespace is in the purview now too. */
11836 if (modules_p ()
11837 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
11838 && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
11839 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
11840 }
11841 else
11842 {
11843 tree context = DECL_CONTEXT (new_friend);
11844 bool dependent_p;
11845
11846 /* In the code
11847 template <class T> class C {
11848 template <class U> friend void C1<U>::f (); // case 1
11849 friend void C2<T>::f (); // case 2
11850 };
11851 we only need to make sure CONTEXT is a complete type for
11852 case 2. To distinguish between the two cases, we note that
11853 CONTEXT of case 1 remains dependent type after tsubst while
11854 this isn't true for case 2. */
11855 ++processing_template_decl;
11856 dependent_p = dependent_type_p (context);
11857 --processing_template_decl;
11858
11859 if (!dependent_p
11860 && !complete_type_or_else (context, NULL_TREE))
11861 return error_mark_node;
11862
11863 if (COMPLETE_TYPE_P (context))
11864 {
11865 tree fn = new_friend;
11866 /* do_friend adds the TEMPLATE_DECL for any member friend
11867 template even if it isn't a member template, i.e.
11868 template <class T> friend A<T>::f();
11869 Look through it in that case. */
11870 if (TREE_CODE (fn) == TEMPLATE_DECL
11871 && !PRIMARY_TEMPLATE_P (fn))
11872 fn = DECL_TEMPLATE_RESULT (fn);
11873 /* Check to see that the declaration is really present, and,
11874 possibly obtain an improved declaration. */
11875 fn = check_classfn (context, fn, NULL_TREE);
11876
11877 if (fn)
11878 new_friend = fn;
11879 }
11880 }
11881
11882 return new_friend;
11883 }
11884
11885 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11886 template arguments, as for tsubst.
11887
11888 Returns an appropriate tsubst'd friend type or error_mark_node on
11889 failure. */
11890
11891 static tree
11892 tsubst_friend_class (tree friend_tmpl, tree args)
11893 {
11894 tree tmpl;
11895
11896 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11897 {
11898 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11899 return TREE_TYPE (tmpl);
11900 }
11901
11902 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) == 1)
11903 /* The template has already been fully substituted, e.g. for
11904
11905 template <typename> friend class ::C;
11906
11907 so we can just return it directly. */
11908 return TREE_TYPE (friend_tmpl);
11909
11910 tree context = CP_DECL_CONTEXT (friend_tmpl);
11911 if (TREE_CODE (context) == NAMESPACE_DECL)
11912 push_nested_namespace (context);
11913 else
11914 {
11915 context = tsubst (context, args, tf_error, NULL_TREE);
11916 push_nested_class (context);
11917 }
11918
11919 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11920 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11921
11922 if (!tmpl)
11923 /* If we didn't find by name lookup, the type may still exist but as a
11924 'hidden' import; we should check for this too to avoid accidentally
11925 instantiating a duplicate. */
11926 tmpl = lookup_imported_hidden_friend (friend_tmpl);
11927
11928 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11929 {
11930 /* The friend template has already been declared. Just
11931 check to see that the declarations match, and install any new
11932 default parameters. We must tsubst the default parameters,
11933 of course. We only need the innermost template parameters
11934 because that is all that redeclare_class_template will look
11935 at. */
11936
11937 if (modules_p ())
11938 /* Check that the existing declaration's module attachment is
11939 compatible with the attachment of the friend template. */
11940 module_may_redeclare (tmpl, friend_tmpl);
11941
11942 if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (friend_tmpl))
11943 {
11944 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11945 args, tf_warning_or_error);
11946 tsubst_each_template_parm_constraints (parms, args,
11947 tf_warning_or_error);
11948 location_t saved_input_location = input_location;
11949 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11950 tree cons = get_constraints (friend_tmpl);
11951 ++processing_template_decl;
11952 cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
11953 DECL_FRIEND_CONTEXT (friend_tmpl));
11954 --processing_template_decl;
11955 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11956 input_location = saved_input_location;
11957 }
11958 }
11959 else
11960 {
11961 /* The friend template has not already been declared. In this
11962 case, the instantiation of the template class will cause the
11963 injection of this template into the namespace scope. */
11964 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11965
11966 if (tmpl != error_mark_node)
11967 {
11968 /* The new TMPL is not an instantiation of anything, so we
11969 forget its origins. It is also not a specialization of
11970 anything. We don't reset CLASSTYPE_TI_TEMPLATE
11971 for the new type because that is supposed to be the
11972 corresponding template decl, i.e., TMPL. */
11973 spec_entry elt;
11974 elt.tmpl = friend_tmpl;
11975 elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl));
11976 elt.spec = TREE_TYPE (tmpl);
11977 type_specializations->remove_elt (&elt);
11978
11979 DECL_USE_TEMPLATE (tmpl) = 0;
11980 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11981 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11982 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11983 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11984 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false;
11985
11986 /* Substitute into and set the constraints on the new declaration. */
11987 if (tree ci = get_constraints (friend_tmpl))
11988 {
11989 ++processing_template_decl;
11990 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11991 DECL_FRIEND_CONTEXT (friend_tmpl));
11992 --processing_template_decl;
11993 set_constraints (tmpl, ci);
11994 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
11995 args, tf_warning_or_error);
11996 }
11997
11998 /* We need to propagate the attachment of the original template to the
11999 newly instantiated template type. */
12000 propagate_defining_module (tmpl, friend_tmpl);
12001
12002 /* Inject this template into the enclosing namspace scope. */
12003 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
12004 }
12005 }
12006
12007 if (TREE_CODE (context) == NAMESPACE_DECL)
12008 pop_nested_namespace (context);
12009 else
12010 pop_nested_class ();
12011
12012 return TREE_TYPE (tmpl);
12013 }
12014
12015 /* Returns zero if TYPE cannot be completed later due to circularity.
12016 Otherwise returns one. */
12017
12018 static int
12019 can_complete_type_without_circularity (tree type)
12020 {
12021 if (type == NULL_TREE || type == error_mark_node)
12022 return 0;
12023 else if (COMPLETE_TYPE_P (type))
12024 return 1;
12025 else if (TREE_CODE (type) == ARRAY_TYPE)
12026 return can_complete_type_without_circularity (TREE_TYPE (type));
12027 else if (CLASS_TYPE_P (type)
12028 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
12029 return 0;
12030 else
12031 return 1;
12032 }
12033
12034 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
12035 tsubst_flags_t, tree);
12036
12037 /* Instantiate the contract statement. */
12038
12039 static tree
12040 tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
12041 tree in_decl)
12042 {
12043 tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
12044 bool auto_p = type_uses_auto (type);
12045
12046 tree r = copy_node (t);
12047
12048 /* Rebuild the result variable. */
12049 if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
12050 {
12051 tree oldvar = POSTCONDITION_IDENTIFIER (t);
12052
12053 tree newvar = copy_node (oldvar);
12054 TREE_TYPE (newvar) = type;
12055 DECL_CONTEXT (newvar) = decl;
12056 POSTCONDITION_IDENTIFIER (r) = newvar;
12057
12058 /* Make sure the postcondition is valid. */
12059 location_t loc = DECL_SOURCE_LOCATION (oldvar);
12060 if (!auto_p)
12061 if (!check_postcondition_result (decl, type, loc))
12062 return invalidate_contract (r);
12063
12064 /* Make the variable available for lookup. */
12065 register_local_specialization (newvar, oldvar);
12066 }
12067
12068 /* Instantiate the condition. If the return type is undeduced, process
12069 the expression as if inside a template to avoid spurious type errors. */
12070 if (auto_p)
12071 ++processing_template_decl;
12072 ++processing_contract_condition;
12073 CONTRACT_CONDITION (r)
12074 = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
12075 --processing_contract_condition;
12076 if (auto_p)
12077 --processing_template_decl;
12078
12079 /* And the comment. */
12080 CONTRACT_COMMENT (r)
12081 = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
12082
12083 return r;
12084 }
12085
12086 /* Update T by instantiating its contract attribute. */
12087
12088 static void
12089 tsubst_contract_attribute (tree decl, tree t, tree args,
12090 tsubst_flags_t complain, tree in_decl)
12091 {
12092 /* For non-specializations, adjust the current declaration to the most general
12093 version of in_decl. Because we defer the instantiation of contracts as long
12094 as possible, they are still written in terms of the parameters (and return
12095 type) of the most general template. */
12096 tree tmpl = DECL_TI_TEMPLATE (in_decl);
12097 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
12098 in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
12099 local_specialization_stack specs (lss_copy);
12100 register_parameter_specializations (in_decl, decl);
12101
12102 /* Get the contract to be instantiated. */
12103 tree contract = CONTRACT_STATEMENT (t);
12104
12105 /* Use the complete set of template arguments for instantiation. The
12106 contract may not have been instantiated and still refer to outer levels
12107 of template parameters. */
12108 args = DECL_TI_ARGS (decl);
12109
12110 /* For member functions, make this available for semantic analysis. */
12111 tree save_ccp = current_class_ptr;
12112 tree save_ccr = current_class_ref;
12113 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
12114 {
12115 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12116 tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
12117 inject_this_parameter (this_type, cp_type_quals (this_type));
12118 }
12119
12120 contract = tsubst_contract (decl, contract, args, complain, in_decl);
12121
12122 current_class_ptr = save_ccp;
12123 current_class_ref = save_ccr;
12124
12125 /* Rebuild the attribute. */
12126 TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
12127 }
12128
12129 /* Rebuild the attribute list for DECL, substituting into contracts
12130 as needed. */
12131
12132 void
12133 tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
12134 {
12135 tree list = copy_list (DECL_ATTRIBUTES (decl));
12136 for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
12137 {
12138 if (cxx_contract_attribute_p (attr))
12139 tsubst_contract_attribute (decl, attr, args, complain, in_decl);
12140 }
12141 DECL_ATTRIBUTES (decl) = list;
12142 }
12143
12144 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
12145 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
12146
12147 static tree
12148 tsubst_attribute (tree t, tree *decl_p, tree args,
12149 tsubst_flags_t complain, tree in_decl)
12150 {
12151 gcc_assert (ATTR_IS_DEPENDENT (t));
12152
12153 /* Note that contract attributes are never substituted from this function.
12154 Their instantiation is triggered by regenerate_from_template_decl when
12155 we instantiate the body of the function. */
12156
12157 tree val = TREE_VALUE (t);
12158 if (val == NULL_TREE)
12159 /* Nothing to do. */;
12160 else if ((flag_openmp || flag_openmp_simd)
12161 && is_attribute_p ("omp declare simd",
12162 get_attribute_name (t)))
12163 {
12164 tree clauses = TREE_VALUE (val);
12165 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
12166 complain, in_decl);
12167 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
12168 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
12169 tree parms = DECL_ARGUMENTS (*decl_p);
12170 clauses
12171 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
12172 if (clauses)
12173 val = build_tree_list (NULL_TREE, clauses);
12174 else
12175 val = NULL_TREE;
12176 }
12177 else if (flag_openmp
12178 && is_attribute_p ("omp declare variant base",
12179 get_attribute_name (t)))
12180 {
12181 ++cp_unevaluated_operand;
12182 tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
12183 --cp_unevaluated_operand;
12184 tree chain = TREE_CHAIN (val);
12185 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
12186 tree ctx = copy_list (TREE_VALUE (val));
12187 tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
12188 if (append_args_list
12189 && TREE_VALUE (append_args_list)
12190 && TREE_CHAIN (TREE_VALUE (append_args_list)))
12191 {
12192 append_args_list = TREE_VALUE (append_args_list);
12193 append_args_list = TREE_VALUE (TREE_CHAIN (append_args_list));
12194 for (; append_args_list;
12195 append_args_list = TREE_CHAIN (append_args_list))
12196 {
12197 tree pref_list = TREE_VALUE (append_args_list);
12198 if (pref_list == NULL_TREE || TREE_CODE (pref_list) != TREE_LIST)
12199 continue;
12200 tree fr_list = TREE_VALUE (pref_list);
12201 int len = TREE_VEC_LENGTH (fr_list);
12202 for (int i = 0; i < len; i++)
12203 {
12204 tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
12205 /* Preserve NOP_EXPR to have a location. */
12206 if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
12207 TREE_OPERAND (*fr_expr, 0)
12208 = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
12209 in_decl);
12210 else
12211 *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
12212 }
12213 }
12214 }
12215 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
12216 {
12217 enum omp_tss_code set = OMP_TSS_CODE (tss);
12218 tree selectors = NULL_TREE;
12219 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
12220 ts = TREE_CHAIN (ts))
12221 {
12222 tree properties = NULL_TREE;
12223 tree scoreval = NULL_TREE;
12224 /* FIXME: The body of this loop should really be dispatching
12225 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
12226 of having hard-wired knowledge of specific selectors. */
12227 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
12228 && set == OMP_TRAIT_SET_CONSTRUCT)
12229 {
12230 tree clauses = OMP_TS_PROPERTIES (ts);
12231 clauses = tsubst_omp_clauses (clauses,
12232 C_ORT_OMP_DECLARE_SIMD, args,
12233 complain, in_decl);
12234 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
12235 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
12236 properties = clauses;
12237 }
12238 else
12239 {
12240 tree v = OMP_TS_SCORE (ts);
12241 if (v)
12242 {
12243 v = tsubst_expr (v, args, complain, in_decl);
12244 v = fold_non_dependent_expr (v);
12245 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
12246 || TREE_CODE (v) != INTEGER_CST)
12247 {
12248 location_t loc
12249 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
12250 match_loc);
12251 error_at (loc, "score argument must be "
12252 "constant integer expression");
12253 return NULL_TREE;
12254 }
12255 else if (tree_int_cst_sgn (v) < 0)
12256 {
12257 location_t loc
12258 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
12259 match_loc);
12260 error_at (loc, "score argument must be "
12261 "non-negative");
12262 return NULL_TREE;
12263 }
12264 scoreval = v;
12265 }
12266 properties = copy_list (OMP_TS_PROPERTIES (ts));
12267 for (tree p = properties; p; p = TREE_CHAIN (p))
12268 if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
12269 continue;
12270 else if (OMP_TP_VALUE (p))
12271 {
12272 bool allow_string
12273 = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
12274 || set != OMP_TRAIT_SET_USER);
12275 tree v = OMP_TP_VALUE (p);
12276 if (TREE_CODE (v) == STRING_CST && allow_string)
12277 continue;
12278 v = tsubst_expr (v, args, complain, in_decl);
12279 v = fold_non_dependent_expr (v);
12280 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
12281 || !tree_fits_shwi_p (v))
12282 {
12283 location_t loc
12284 = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
12285 match_loc);
12286 if (allow_string)
12287 error_at (loc, "property must be constant "
12288 "integer expression or string "
12289 "literal");
12290 else
12291 error_at (loc, "property must be constant "
12292 "integer expression");
12293 return NULL_TREE;
12294 }
12295 OMP_TP_VALUE (p) = v;
12296 }
12297 }
12298 selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
12299 properties, selectors);
12300 }
12301 OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
12302 }
12303 if (varid == error_mark_node)
12304 val = error_mark_node;
12305 else
12306 val = tree_cons (varid, ctx, chain);
12307 }
12308 /* If the first attribute argument is an identifier, don't
12309 pass it through tsubst. Attributes like mode, format,
12310 cleanup and several target specific attributes expect it
12311 unmodified. */
12312 else if (attribute_takes_identifier_p (get_attribute_name (t)))
12313 {
12314 tree chain
12315 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
12316 if (chain != TREE_CHAIN (val))
12317 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
12318 }
12319 else if (PACK_EXPANSION_P (val))
12320 {
12321 /* An attribute pack expansion. */
12322 tree purp = TREE_PURPOSE (t);
12323 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
12324 if (pack == error_mark_node)
12325 return error_mark_node;
12326 int len = TREE_VEC_LENGTH (pack);
12327 tree list = NULL_TREE;
12328 tree *q = &list;
12329 for (int i = 0; i < len; ++i)
12330 {
12331 tree elt = TREE_VEC_ELT (pack, i);
12332 *q = build_tree_list (purp, elt);
12333 q = &TREE_CHAIN (*q);
12334 }
12335 return list;
12336 }
12337 else
12338 val = tsubst_expr (val, args, complain, in_decl);
12339
12340 if (val == error_mark_node)
12341 return error_mark_node;
12342 if (val != TREE_VALUE (t))
12343 return build_tree_list (TREE_PURPOSE (t), val);
12344 return t;
12345 }
12346
12347 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
12348 unchanged or a new TREE_LIST chain. */
12349
12350 static tree
12351 tsubst_attributes (tree attributes, tree args,
12352 tsubst_flags_t complain, tree in_decl)
12353 {
12354 tree last_dep = NULL_TREE;
12355
12356 for (tree t = attributes; t; t = TREE_CHAIN (t))
12357 if (ATTR_IS_DEPENDENT (t))
12358 {
12359 last_dep = t;
12360 attributes = copy_list (attributes);
12361 break;
12362 }
12363
12364 if (last_dep)
12365 for (tree *p = &attributes; *p; )
12366 {
12367 tree t = *p;
12368 if (ATTR_IS_DEPENDENT (t))
12369 {
12370 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
12371 if (subst != t)
12372 {
12373 *p = subst;
12374 while (*p)
12375 p = &TREE_CHAIN (*p);
12376 *p = TREE_CHAIN (t);
12377 continue;
12378 }
12379 }
12380 p = &TREE_CHAIN (*p);
12381 }
12382
12383 return attributes;
12384 }
12385
12386 /* Apply any attributes which had to be deferred until instantiation
12387 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
12388 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
12389 false on error. */
12390
12391 static bool
12392 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
12393 tree args, tsubst_flags_t complain, tree in_decl)
12394 {
12395 tree t;
12396 tree *p;
12397
12398 if (attributes == NULL_TREE)
12399 return true;
12400
12401 if (DECL_P (*decl_p))
12402 {
12403 if (TREE_TYPE (*decl_p) == error_mark_node)
12404 return false;
12405 p = &DECL_ATTRIBUTES (*decl_p);
12406 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
12407 to our attributes parameter. */
12408 gcc_assert (*p == attributes);
12409 }
12410 else if (FUNC_OR_METHOD_TYPE_P (*decl_p)
12411 || (attr_flags & ATTR_FLAG_TYPE_IN_PLACE) == 0)
12412 p = NULL;
12413 else
12414 {
12415 p = &TYPE_ATTRIBUTES (*decl_p);
12416 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
12417 lookup_template_class_1, and should be preserved. */
12418 gcc_assert (*p != attributes);
12419 while (*p)
12420 p = &TREE_CHAIN (*p);
12421 }
12422
12423 /* save_template_attributes puts the dependent attributes at the beginning of
12424 the list; find the non-dependent ones. */
12425 for (t = attributes; t; t = TREE_CHAIN (t))
12426 if (!ATTR_IS_DEPENDENT (t))
12427 break;
12428 tree nondep = t;
12429
12430 /* Apply any non-dependent attributes. */
12431 if (p)
12432 *p = nondep;
12433 else if (nondep)
12434 *decl_p = cp_build_type_attribute_variant (*decl_p, nondep);
12435
12436 if (nondep == attributes)
12437 return true;
12438
12439 /* And then any dependent ones. */
12440 tree late_attrs = NULL_TREE;
12441 tree *q = &late_attrs;
12442 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
12443 {
12444 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
12445 if (*q == error_mark_node)
12446 return false;
12447 if (*q == t)
12448 {
12449 *q = copy_node (t);
12450 TREE_CHAIN (*q) = NULL_TREE;
12451 }
12452 while (*q)
12453 q = &TREE_CHAIN (*q);
12454 }
12455
12456 /* cplus_decl_attributes can add some attributes implicitly. For templates,
12457 those attributes should have been added already when those templates were
12458 parsed, and shouldn't be added based on from which context they are
12459 first time instantiated. */
12460 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
12461 auto o2 = make_temp_override (optimization_current_node,
12462 optimization_default_node);
12463 auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
12464 auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
12465 NULL);
12466 auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
12467 auto o6 = make_temp_override (target_option_current_node,
12468 target_option_default_node);
12469
12470 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
12471
12472 return true;
12473 }
12474
12475 /* The template TMPL is being instantiated with the template arguments TARGS.
12476 Perform the access checks that we deferred when parsing the template. */
12477
12478 static void
12479 perform_instantiation_time_access_checks (tree tmpl, tree targs)
12480 {
12481 unsigned i;
12482 deferred_access_check *chk;
12483
12484 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
12485 return;
12486
12487 if (vec<deferred_access_check, va_gc> *access_checks
12488 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
12489 FOR_EACH_VEC_ELT (*access_checks, i, chk)
12490 {
12491 tree decl = chk->decl;
12492 tree diag_decl = chk->diag_decl;
12493 tree type_scope = TREE_TYPE (chk->binfo);
12494
12495 if (uses_template_parms (type_scope))
12496 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
12497
12498 /* Make access check error messages point to the location
12499 of the use of the typedef. */
12500 iloc_sentinel ils (chk->loc);
12501 perform_or_defer_access_check (TYPE_BINFO (type_scope),
12502 decl, diag_decl, tf_warning_or_error);
12503 }
12504 }
12505
12506 /* If the template T that we're about to instantiate contained errors at
12507 parse time that we downgraded into warnings or suppressed, diagnose the
12508 error now to render the TU ill-formed (if the TU has not already been
12509 deemed ill-formed by an earlier error). */
12510
12511 static void
12512 maybe_diagnose_erroneous_template (tree t)
12513 {
12514 if (erroneous_templates && !(seen_error) ())
12515 if (location_t *error_loc = erroneous_templates->get (t))
12516 {
12517 auto_diagnostic_group d;
12518 location_t decl_loc = location_of (t);
12519 error_at (decl_loc, "instantiating erroneous template");
12520 inform (*error_loc, "first error appeared here");
12521 }
12522 }
12523
12524 tree
12525 instantiate_class_template (tree type)
12526 {
12527 auto_timevar tv (TV_TEMPLATE_INST);
12528
12529 tree templ, args, pattern, t, member;
12530 tree typedecl;
12531 tree pbinfo;
12532 tree base_list;
12533 unsigned int saved_maximum_field_alignment;
12534 tree fn_context;
12535
12536 if (type == error_mark_node)
12537 return error_mark_node;
12538
12539 if (COMPLETE_OR_OPEN_TYPE_P (type)
12540 || uses_template_parms (type))
12541 return type;
12542
12543 /* Figure out which template is being instantiated. */
12544 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
12545 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
12546
12547 /* Mark the type as in the process of being defined. */
12548 TYPE_BEING_DEFINED (type) = 1;
12549
12550 /* We may be in the middle of deferred access check. Disable
12551 it now. */
12552 deferring_access_check_sentinel acs (dk_no_deferred);
12553
12554 /* Determine what specialization of the original template to
12555 instantiate. */
12556 t = most_specialized_partial_spec (type, tf_warning_or_error);
12557 if (t == error_mark_node)
12558 return error_mark_node;
12559 else if (t)
12560 {
12561 /* This TYPE is actually an instantiation of a partial
12562 specialization. We replace the innermost set of ARGS with
12563 the arguments appropriate for substitution. For example,
12564 given:
12565
12566 template <class T> struct S {};
12567 template <class T> struct S<T*> {};
12568
12569 and supposing that we are instantiating S<int*>, ARGS will
12570 presently be {int*} -- but we need {int}. */
12571 pattern = TREE_TYPE (TI_TEMPLATE (t));
12572 args = TI_ARGS (t);
12573 }
12574 else
12575 {
12576 pattern = TREE_TYPE (templ);
12577 args = CLASSTYPE_TI_ARGS (type);
12578 }
12579
12580 /* If the template we're instantiating is incomplete, then clearly
12581 there's nothing we can do. */
12582 if (!COMPLETE_TYPE_P (pattern))
12583 {
12584 /* We can try again later. */
12585 TYPE_BEING_DEFINED (type) = 0;
12586 return type;
12587 }
12588
12589 /* If we've recursively instantiated too many templates, stop. */
12590 if (! push_tinst_level (type))
12591 return type;
12592
12593 maybe_diagnose_erroneous_template (t ? TI_TEMPLATE (t) : templ);
12594
12595 int saved_unevaluated_operand = cp_unevaluated_operand;
12596 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12597
12598 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
12599 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12600 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
12601 fn_context = error_mark_node;
12602 if (!fn_context)
12603 push_to_top_level ();
12604 else
12605 {
12606 cp_unevaluated_operand = 0;
12607 c_inhibit_evaluation_warnings = 0;
12608 }
12609
12610 mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
12611
12612 /* Use #pragma pack from the template context. */
12613 saved_maximum_field_alignment = maximum_field_alignment;
12614 maximum_field_alignment = TYPE_PRECISION (pattern);
12615
12616 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
12617
12618 /* Set the input location to the most specialized template definition.
12619 This is needed if tsubsting causes an error. */
12620 typedecl = TYPE_MAIN_DECL (pattern);
12621 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
12622 DECL_SOURCE_LOCATION (typedecl);
12623
12624 set_instantiating_module (TYPE_NAME (type));
12625
12626 TYPE_PACKED (type) = TYPE_PACKED (pattern);
12627 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
12628 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
12629 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
12630 if (ANON_AGGR_TYPE_P (pattern))
12631 SET_ANON_AGGR_TYPE_P (type);
12632 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
12633 {
12634 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
12635 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
12636 /* Adjust visibility for template arguments. */
12637 determine_visibility (TYPE_MAIN_DECL (type));
12638 }
12639 if (CLASS_TYPE_P (type))
12640 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
12641
12642 pbinfo = TYPE_BINFO (pattern);
12643
12644 /* We should never instantiate a nested class before its enclosing
12645 class; we need to look up the nested class by name before we can
12646 instantiate it, and that lookup should instantiate the enclosing
12647 class. */
12648 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
12649 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
12650
12651 /* When instantiating nested lambdas, ensure that they get the mangling
12652 scope of the new class type. */
12653 start_lambda_scope (TYPE_NAME (type));
12654
12655 base_list = NULL_TREE;
12656 /* Defer access checking while we substitute into the types named in
12657 the base-clause. */
12658 push_deferring_access_checks (dk_deferred);
12659 if (BINFO_N_BASE_BINFOS (pbinfo))
12660 {
12661 tree pbase_binfo;
12662 int i;
12663
12664 /* Substitute into each of the bases to determine the actual
12665 basetypes. */
12666 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
12667 {
12668 tree base;
12669 tree access = BINFO_BASE_ACCESS (pbinfo, i);
12670 tree expanded_bases = NULL_TREE;
12671 int idx, len = 1;
12672
12673 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
12674 {
12675 expanded_bases =
12676 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
12677 args, tf_error, NULL_TREE);
12678 if (expanded_bases == error_mark_node)
12679 continue;
12680
12681 len = TREE_VEC_LENGTH (expanded_bases);
12682 }
12683
12684 for (idx = 0; idx < len; idx++)
12685 {
12686 if (expanded_bases)
12687 /* Extract the already-expanded base class. */
12688 base = TREE_VEC_ELT (expanded_bases, idx);
12689 else
12690 /* Substitute to figure out the base class. */
12691 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
12692 NULL_TREE);
12693
12694 if (base == error_mark_node)
12695 continue;
12696
12697 base_list = tree_cons (access, base, base_list);
12698 if (BINFO_VIRTUAL_P (pbase_binfo))
12699 TREE_TYPE (base_list) = integer_type_node;
12700 }
12701 }
12702
12703 /* The list is now in reverse order; correct that. */
12704 base_list = nreverse (base_list);
12705 }
12706 /* Now call xref_basetypes to set up all the base-class
12707 information. */
12708 xref_basetypes (type, base_list);
12709
12710 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
12711 (int) ATTR_FLAG_TYPE_IN_PLACE,
12712 args, tf_error, NULL_TREE);
12713 fixup_attribute_variants (type);
12714
12715 /* Now that our base classes are set up, enter the scope of the
12716 class, so that name lookups into base classes, etc. will work
12717 correctly. This is precisely analogous to what we do in
12718 begin_class_definition when defining an ordinary non-template
12719 class, except we also need to push the enclosing classes. */
12720 push_nested_class (type);
12721
12722 /* Now check accessibility of the types named in its base-clause,
12723 relative to the scope of the class. */
12724 pop_to_parent_deferring_access_checks ();
12725
12726 /* A vector to hold members marked with attribute used. */
12727 auto_vec<tree> used;
12728
12729 /* Now members are processed in the order of declaration. */
12730 for (member = CLASSTYPE_DECL_LIST (pattern);
12731 member; member = TREE_CHAIN (member))
12732 {
12733 tree t = TREE_VALUE (member);
12734
12735 if (TREE_PURPOSE (member))
12736 {
12737 if (TYPE_P (t))
12738 {
12739 if (LAMBDA_TYPE_P (t))
12740 /* A closure type for a lambda in an NSDMI or default argument.
12741 Ignore it; it will be regenerated when needed. */
12742 continue;
12743
12744 /* If the member is a class template, we've
12745 already substituted its type. */
12746 if (CLASS_TYPE_P (t)
12747 && CLASSTYPE_IS_TEMPLATE (t))
12748 continue;
12749
12750 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
12751 if (newtag == error_mark_node)
12752 continue;
12753
12754 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
12755 {
12756 tree name = TYPE_IDENTIFIER (t);
12757
12758 /* Now, install the tag. We don't use pushtag
12759 because that does too much work -- creating an
12760 implicit typedef, which we've already done. */
12761 set_identifier_type_value (name, TYPE_NAME (newtag));
12762 maybe_add_class_template_decl_list (type, newtag, false);
12763 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12764 determine_visibility (TYPE_NAME (newtag));
12765 }
12766 }
12767 else if (DECL_DECLARES_FUNCTION_P (t))
12768 {
12769 tree r;
12770
12771 if (TREE_CODE (t) == TEMPLATE_DECL)
12772 ++processing_template_decl;
12773 r = tsubst (t, args, tf_error, NULL_TREE);
12774 if (TREE_CODE (t) == TEMPLATE_DECL)
12775 --processing_template_decl;
12776
12777 set_current_access_from_decl (r);
12778 finish_member_declaration (r);
12779 /* Instantiate members marked with attribute used. */
12780 if (r != error_mark_node && DECL_PRESERVE_P (r))
12781 used.safe_push (r);
12782 if (TREE_CODE (r) == FUNCTION_DECL
12783 && DECL_OMP_DECLARE_REDUCTION_P (r))
12784 cp_check_omp_declare_reduction (r);
12785 }
12786 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12787 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12788 /* A closure type for a lambda in an NSDMI or default argument.
12789 Ignore it; it will be regenerated when needed. */;
12790 else
12791 {
12792 /* Build new TYPE_FIELDS. */
12793 if (TREE_CODE (t) == STATIC_ASSERT)
12794 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
12795 else if (TREE_CODE (t) != CONST_DECL)
12796 {
12797 tree r;
12798 tree vec = NULL_TREE;
12799 int len = 1;
12800
12801 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12802 /* The file and line for this declaration, to
12803 assist in error message reporting. Since we
12804 called push_tinst_level above, we don't need to
12805 restore these. */
12806 input_location = DECL_SOURCE_LOCATION (t);
12807
12808 if (TREE_CODE (t) == TEMPLATE_DECL)
12809 ++processing_template_decl;
12810 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12811 if (TREE_CODE (t) == TEMPLATE_DECL)
12812 --processing_template_decl;
12813
12814 if (TREE_CODE (r) == TREE_VEC)
12815 {
12816 /* A capture pack became multiple fields. */
12817 vec = r;
12818 len = TREE_VEC_LENGTH (vec);
12819 }
12820
12821 for (int i = 0; i < len; ++i)
12822 {
12823 if (vec)
12824 r = TREE_VEC_ELT (vec, i);
12825 if (VAR_P (r))
12826 {
12827 /* In [temp.inst]:
12828
12829 [t]he initialization (and any associated
12830 side-effects) of a static data member does
12831 not occur unless the static data member is
12832 itself used in a way that requires the
12833 definition of the static data member to
12834 exist.
12835
12836 Therefore, we do not substitute into the
12837 initialized for the static data member here. */
12838 finish_static_data_member_decl
12839 (r,
12840 /*init=*/NULL_TREE,
12841 /*init_const_expr_p=*/false,
12842 /*asmspec_tree=*/NULL_TREE,
12843 /*flags=*/0);
12844 /* Instantiate members marked with attribute used. */
12845 if (r != error_mark_node && DECL_PRESERVE_P (r))
12846 used.safe_push (r);
12847 }
12848 else if (TREE_CODE (r) == FIELD_DECL)
12849 {
12850 /* Determine whether R has a valid type and can be
12851 completed later. If R is invalid, then its type
12852 is replaced by error_mark_node. */
12853 tree rtype = TREE_TYPE (r);
12854 if (can_complete_type_without_circularity (rtype))
12855 complete_type (rtype);
12856
12857 if (!complete_or_array_type_p (rtype))
12858 {
12859 /* If R's type couldn't be completed and
12860 it isn't a flexible array member (whose
12861 type is incomplete by definition) give
12862 an error. */
12863 cxx_incomplete_type_error (r, rtype);
12864 TREE_TYPE (r) = error_mark_node;
12865 }
12866 else if (TREE_CODE (rtype) == ARRAY_TYPE
12867 && TYPE_DOMAIN (rtype) == NULL_TREE
12868 && (TREE_CODE (type) == UNION_TYPE
12869 || TREE_CODE (type) == QUAL_UNION_TYPE))
12870 {
12871 error ("flexible array member %qD in union", r);
12872 TREE_TYPE (r) = error_mark_node;
12873 }
12874 else if (!verify_type_context (input_location,
12875 TCTX_FIELD, rtype))
12876 TREE_TYPE (r) = error_mark_node;
12877 }
12878
12879 /* If it is a TYPE_DECL for a class-scoped
12880 ENUMERAL_TYPE, such a thing will already have
12881 been added to the field list by tsubst_enum
12882 in finish_member_declaration case above. */
12883 if (!(TREE_CODE (r) == TYPE_DECL
12884 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12885 && DECL_ARTIFICIAL (r)))
12886 {
12887 set_current_access_from_decl (r);
12888 finish_member_declaration (r);
12889 }
12890 }
12891 }
12892 }
12893 }
12894 else
12895 {
12896 if (TREE_CODE (t) == TU_LOCAL_ENTITY)
12897 /* Ignore. */;
12898 else if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12899 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12900 {
12901 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12902
12903 tree friend_type = t;
12904 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12905 {
12906 /* template <class T> friend class C; */
12907 friend_type = tsubst_friend_class (friend_type, args);
12908 }
12909 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12910 {
12911 /* template <class T> friend class C::D; */
12912 friend_type = tsubst (friend_type, args,
12913 tf_warning_or_error, NULL_TREE);
12914 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12915 friend_type = TREE_TYPE (friend_type);
12916 }
12917 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12918 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12919 {
12920 /* This could be either
12921
12922 friend class T::C;
12923
12924 when dependent_type_p is false or
12925
12926 template <class U> friend class T::C;
12927
12928 otherwise. */
12929 /* Bump processing_template_decl in case this is something like
12930 template <class T> friend struct A<T>::B. */
12931 ++processing_template_decl;
12932 friend_type = tsubst (friend_type, args,
12933 tf_warning_or_error, NULL_TREE);
12934 --processing_template_decl;
12935 }
12936 else if (PACK_EXPANSION_P (friend_type))
12937 {
12938 friend_type = tsubst_pack_expansion (friend_type, args,
12939 tf_warning_or_error,
12940 NULL_TREE);
12941 if (friend_type != error_mark_node)
12942 {
12943 unsigned int len = TREE_VEC_LENGTH (friend_type);
12944 for (unsigned int idx = 0; idx < len; ++idx)
12945 if (TREE_VEC_ELT (friend_type, idx) != error_mark_node)
12946 make_friend_class (type,
12947 TREE_VEC_ELT (friend_type, idx),
12948 /*complain=*/false);
12949 }
12950 friend_type = error_mark_node;
12951 }
12952 else if (uses_template_parms (friend_type))
12953 /* friend class C<T>; */
12954 friend_type = tsubst (friend_type, args,
12955 tf_warning_or_error, NULL_TREE);
12956
12957 /* Otherwise it's
12958
12959 friend class C;
12960
12961 where C is already declared or
12962
12963 friend class C<int>;
12964
12965 We don't have to do anything in these cases. */
12966
12967 if (friend_type != error_mark_node)
12968 make_friend_class (type, friend_type, /*complain=*/false);
12969 }
12970 else
12971 {
12972 /* Build new DECL_FRIENDLIST. */
12973 tree r;
12974
12975 /* The file and line for this declaration, to
12976 assist in error message reporting. Since we
12977 called push_tinst_level above, we don't need to
12978 restore these. */
12979 input_location = DECL_SOURCE_LOCATION (t);
12980
12981 if (TREE_CODE (t) == TEMPLATE_DECL)
12982 {
12983 ++processing_template_decl;
12984 push_deferring_access_checks (dk_no_check);
12985 }
12986
12987 r = tsubst_friend_function (t, args);
12988 add_friend (type, r, /*complain=*/false);
12989 if (TREE_CODE (t) == TEMPLATE_DECL)
12990 {
12991 pop_deferring_access_checks ();
12992 --processing_template_decl;
12993 }
12994 }
12995 }
12996 }
12997
12998 if (fn_context)
12999 {
13000 /* Restore these before substituting into the lambda capture
13001 initializers. */
13002 cp_unevaluated_operand = saved_unevaluated_operand;
13003 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
13004 }
13005
13006 /* Set the file and line number information to whatever is given for
13007 the class itself. This puts error messages involving generated
13008 implicit functions at a predictable point, and the same point
13009 that would be used for non-template classes. */
13010 input_location = DECL_SOURCE_LOCATION (typedecl);
13011
13012 unreverse_member_declarations (type);
13013 finish_struct_1 (type);
13014 TYPE_BEING_DEFINED (type) = 0;
13015
13016 finish_lambda_scope ();
13017
13018 /* Remember if instantiating this class ran into errors, so we can avoid
13019 instantiating member functions in limit_bad_template_recursion. We set
13020 this flag even if the problem was in another instantiation triggered by
13021 this one, as that will likely also cause trouble for member functions. */
13022 if (errorcount + sorrycount > current_tinst_level->errors)
13023 CLASSTYPE_ERRONEOUS (type) = true;
13024
13025 /* We don't instantiate default arguments for member functions. 14.7.1:
13026
13027 The implicit instantiation of a class template specialization causes
13028 the implicit instantiation of the declarations, but not of the
13029 definitions or default arguments, of the class member functions,
13030 member classes, static data members and member templates.... */
13031
13032 perform_instantiation_time_access_checks (pattern, args);
13033 perform_deferred_access_checks (tf_warning_or_error);
13034
13035 /* Now that we've gone through all the members, instantiate those
13036 marked with attribute used. We must do this in the context of
13037 the class -- not the context we pushed from, as that might be
13038 inside a template and change the behaviour of mark_used. */
13039 for (tree x : used)
13040 mark_used (x);
13041
13042 pop_nested_class ();
13043 maximum_field_alignment = saved_maximum_field_alignment;
13044 if (!fn_context)
13045 pop_from_top_level ();
13046 pop_tinst_level ();
13047
13048 /* The vtable for a template class can be emitted in any translation
13049 unit in which the class is instantiated. When there is no key
13050 method, however, finish_struct_1 will already have added TYPE to
13051 the keyed_classes. */
13052 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
13053 vec_safe_push (keyed_classes, type);
13054
13055 return type;
13056 }
13057
13058 tree
13059 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13060 {
13061 tree r;
13062
13063 if (!t)
13064 r = t;
13065 else if (TYPE_P (t))
13066 r = tsubst (t, args, complain, in_decl);
13067 else
13068 {
13069 if (!(complain & tf_warning))
13070 ++c_inhibit_evaluation_warnings;
13071 r = tsubst_expr (t, args, complain, in_decl);
13072 if (!(complain & tf_warning))
13073 --c_inhibit_evaluation_warnings;
13074 }
13075
13076 return r;
13077 }
13078
13079 /* Given a function parameter pack TMPL_PARM and some function parameters
13080 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
13081 and set *SPEC_P to point at the next point in the list. */
13082
13083 tree
13084 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
13085 {
13086 /* Collect all of the extra "packed" parameters into an
13087 argument pack. */
13088 tree argpack;
13089 tree spec_parm = *spec_p;
13090 int len;
13091
13092 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
13093 if (tmpl_parm
13094 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
13095 break;
13096
13097 spec_parm = *spec_p;
13098 if (len == 1 && DECL_PACK_P (spec_parm))
13099 {
13100 /* The instantiation is still a parameter pack; don't wrap it in a
13101 NONTYPE_ARGUMENT_PACK. */
13102 argpack = spec_parm;
13103 spec_parm = DECL_CHAIN (spec_parm);
13104 }
13105 else
13106 {
13107 /* Fill in PARMVEC with all of the parameters. */
13108 tree parmvec = make_tree_vec (len);
13109 argpack = make_node (NONTYPE_ARGUMENT_PACK);
13110 for (int i = 0; i < len; i++)
13111 {
13112 tree elt = spec_parm;
13113 if (DECL_PACK_P (elt))
13114 elt = make_pack_expansion (elt);
13115 TREE_VEC_ELT (parmvec, i) = elt;
13116 spec_parm = DECL_CHAIN (spec_parm);
13117 }
13118
13119 /* Build the argument packs. */
13120 ARGUMENT_PACK_ARGS (argpack) = parmvec;
13121 }
13122 *spec_p = spec_parm;
13123
13124 return argpack;
13125 }
13126
13127 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
13128 NONTYPE_ARGUMENT_PACK. */
13129
13130 static tree
13131 make_fnparm_pack (tree spec_parm)
13132 {
13133 return extract_fnparm_pack (NULL_TREE, &spec_parm);
13134 }
13135
13136 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
13137 pack expansion with no extra args, 2 if it has extra args, or 0
13138 if it is not a pack expansion. */
13139
13140 static int
13141 argument_pack_element_is_expansion_p (tree arg_pack, int i)
13142 {
13143 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13144 /* We're being called before this happens in tsubst_pack_expansion. */
13145 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13146 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
13147 if (i >= TREE_VEC_LENGTH (vec))
13148 return 0;
13149 tree elt = TREE_VEC_ELT (vec, i);
13150 if (DECL_P (elt))
13151 /* A decl pack is itself an expansion. */
13152 elt = TREE_TYPE (elt);
13153 if (!PACK_EXPANSION_P (elt))
13154 return 0;
13155 if (PACK_EXPANSION_EXTRA_ARGS (elt))
13156 return 2;
13157 return 1;
13158 }
13159
13160
13161 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
13162
13163 static tree
13164 make_argument_pack_select (tree arg_pack, unsigned index)
13165 {
13166 tree aps = make_node (ARGUMENT_PACK_SELECT);
13167
13168 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
13169 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
13170
13171 return aps;
13172 }
13173
13174 /* This is a subroutine of tsubst_pack_expansion.
13175
13176 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
13177 mechanism to store the (non complete list of) arguments of the
13178 substitution and return a non substituted pack expansion, in order
13179 to wait for when we have enough arguments to really perform the
13180 substitution. */
13181
13182 static bool
13183 use_pack_expansion_extra_args_p (tree t,
13184 tree parm_packs,
13185 int arg_pack_len,
13186 bool has_empty_arg)
13187 {
13188 if (has_empty_arg
13189 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
13190 return true;
13191
13192 /* If one pack has an expansion and another pack has a normal
13193 argument or if one pack has an empty argument and an another
13194 one hasn't then tsubst_pack_expansion cannot perform the
13195 substitution and need to fall back on the
13196 PACK_EXPANSION_EXTRA mechanism. */
13197 if (parm_packs == NULL_TREE)
13198 return false;
13199 else if (has_empty_arg)
13200 {
13201 /* If all the actual packs are pack expansions, we can still
13202 subsitute directly. */
13203 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
13204 {
13205 tree a = TREE_VALUE (p);
13206 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
13207 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
13208 a = ARGUMENT_PACK_ARGS (a);
13209 if (TREE_VEC_LENGTH (a) == 1)
13210 a = TREE_VEC_ELT (a, 0);
13211 if (PACK_EXPANSION_P (a))
13212 continue;
13213 return true;
13214 }
13215 return false;
13216 }
13217
13218 for (int i = 0 ; i < arg_pack_len; ++i)
13219 {
13220 bool has_expansion_arg = false;
13221 bool has_non_expansion_arg = false;
13222 for (tree parm_pack = parm_packs;
13223 parm_pack;
13224 parm_pack = TREE_CHAIN (parm_pack))
13225 {
13226 tree arg = TREE_VALUE (parm_pack);
13227
13228 int exp = argument_pack_element_is_expansion_p (arg, i);
13229 if (exp == 2)
13230 /* We can't substitute a pack expansion with extra args into
13231 our pattern. */
13232 return true;
13233 else if (exp)
13234 has_expansion_arg = true;
13235 else
13236 has_non_expansion_arg = true;
13237 }
13238
13239 if (has_expansion_arg && has_non_expansion_arg)
13240 {
13241 /* We can get here with:
13242
13243 template <class... Ts> struct X {
13244 template <class... Us> using Y = Z<void(Ts, Us)...>;
13245 };
13246 template <class A, class... P>
13247 using foo = X<int, int>::Y<A, P...>;
13248
13249 where we compare int and A and then the second int and P...,
13250 whose expansion-ness doesn't match, but that's OK. */
13251 return true;
13252 }
13253 }
13254 return false;
13255 }
13256
13257 /* [temp.variadic]/6 says that:
13258
13259 The instantiation of a pack expansion [...]
13260 produces a list E1,E2, ..., En, where N is the number of elements
13261 in the pack expansion parameters.
13262
13263 This subroutine of tsubst_pack_expansion produces one of these Ei.
13264
13265 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
13266 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
13267 PATTERN, and each TREE_VALUE is its corresponding argument pack.
13268 INDEX is the index 'i' of the element Ei to produce. ARGS,
13269 COMPLAIN, and IN_DECL are the same parameters as for the
13270 tsubst_pack_expansion function.
13271
13272 The function returns the resulting Ei upon successful completion,
13273 or error_mark_node.
13274
13275 Note that this function possibly modifies the ARGS parameter, so
13276 it's the responsibility of the caller to restore it. */
13277
13278 static tree
13279 gen_elem_of_pack_expansion_instantiation (tree pattern,
13280 tree parm_packs,
13281 unsigned index,
13282 tree args /* This parm gets
13283 modified. */,
13284 tsubst_flags_t complain,
13285 tree in_decl)
13286 {
13287 tree t;
13288 bool ith_elem_is_expansion = false;
13289
13290 /* For each parameter pack, change the substitution of the parameter
13291 pack to the ith argument in its argument pack, then expand the
13292 pattern. */
13293 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
13294 {
13295 tree parm = TREE_PURPOSE (pack);
13296 tree arg_pack = TREE_VALUE (pack);
13297 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
13298
13299 ith_elem_is_expansion |=
13300 argument_pack_element_is_expansion_p (arg_pack, index);
13301
13302 /* Select the Ith argument from the pack. */
13303 if (TREE_CODE (parm) == PARM_DECL
13304 || VAR_P (parm)
13305 || TREE_CODE (parm) == FIELD_DECL)
13306 {
13307 if (index == 0)
13308 {
13309 aps = make_argument_pack_select (arg_pack, index);
13310 if (!mark_used (parm, complain) && !(complain & tf_error))
13311 return error_mark_node;
13312 register_local_specialization (aps, parm);
13313 }
13314 else
13315 aps = retrieve_local_specialization (parm);
13316 }
13317 else
13318 {
13319 int idx, level;
13320 template_parm_level_and_index (parm, &level, &idx);
13321
13322 if (index == 0)
13323 {
13324 aps = make_argument_pack_select (arg_pack, index);
13325 /* Update the corresponding argument. */
13326 TMPL_ARG (args, level, idx) = aps;
13327 }
13328 else
13329 /* Re-use the ARGUMENT_PACK_SELECT. */
13330 aps = TMPL_ARG (args, level, idx);
13331 }
13332 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
13333 }
13334
13335 /* Substitute into the PATTERN with the (possibly altered)
13336 arguments. */
13337 if (pattern == in_decl)
13338 /* Expanding a fixed parameter pack from
13339 coerce_template_parameter_pack. */
13340 t = tsubst_decl (pattern, args, complain);
13341 else if (pattern == error_mark_node)
13342 t = error_mark_node;
13343 else if (!TYPE_P (pattern))
13344 t = tsubst_expr (pattern, args, complain, in_decl);
13345 else
13346 {
13347 t = tsubst (pattern, args, complain, in_decl);
13348 if (is_auto (t) && !ith_elem_is_expansion)
13349 /* When expanding the fake auto... pack expansion from add_capture, we
13350 need to mark that the expansion is no longer a pack. */
13351 TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
13352 }
13353
13354 /* If the Ith argument pack element is a pack expansion, then
13355 the Ith element resulting from the substituting is going to
13356 be a pack expansion as well. */
13357 if (ith_elem_is_expansion)
13358 t = make_pack_expansion (t, complain);
13359
13360 return t;
13361 }
13362
13363 /* When the unexpanded parameter pack in a fold expression expands to an empty
13364 sequence, the value of the expression is as follows; the program is
13365 ill-formed if the operator is not listed in this table.
13366
13367 && true
13368 || false
13369 , void() */
13370
13371 tree
13372 expand_empty_fold (tree t, tsubst_flags_t complain)
13373 {
13374 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
13375 if (!FOLD_EXPR_MODIFY_P (t))
13376 switch (code)
13377 {
13378 case TRUTH_ANDIF_EXPR:
13379 return boolean_true_node;
13380 case TRUTH_ORIF_EXPR:
13381 return boolean_false_node;
13382 case COMPOUND_EXPR:
13383 return void_node;
13384 default:
13385 break;
13386 }
13387
13388 if (complain & tf_error)
13389 error_at (location_of (t),
13390 "fold of empty expansion over %O", code);
13391 return error_mark_node;
13392 }
13393
13394 /* Given a fold-expression T and a current LEFT and RIGHT operand,
13395 form an expression that combines the two terms using the
13396 operator of T. */
13397
13398 static tree
13399 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
13400 {
13401 tree_code code = FOLD_EXPR_OP (t);
13402
13403 tree lookups = templated_operator_saved_lookups (t);
13404
13405 // Handle compound assignment operators.
13406 if (FOLD_EXPR_MODIFY_P (t))
13407 return build_x_modify_expr (input_location, left, code, right,
13408 lookups, complain);
13409
13410 warning_sentinel s(warn_parentheses);
13411 switch (code)
13412 {
13413 case COMPOUND_EXPR:
13414 return build_x_compound_expr (input_location, left, right,
13415 lookups, complain);
13416 default:
13417 return build_x_binary_op (input_location, code,
13418 left, TREE_CODE (left),
13419 right, TREE_CODE (right),
13420 lookups, /*overload=*/NULL,
13421 complain);
13422 }
13423 }
13424
13425 /* Substitute ARGS into the pack of a fold expression T. */
13426
13427 static inline tree
13428 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13429 {
13430 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
13431 }
13432
13433 /* Substitute ARGS into the pack of a fold expression T. */
13434
13435 static inline tree
13436 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13437 {
13438 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
13439 }
13440
13441 /* Expand a PACK of arguments into a grouped as left fold.
13442 Given a pack containing elements A0, A1, ..., An and an
13443 operator @, this builds the expression:
13444
13445 ((A0 @ A1) @ A2) ... @ An
13446
13447 Note that PACK must not be empty.
13448
13449 The operator is defined by the original fold expression T. */
13450
13451 static tree
13452 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
13453 {
13454 tree left = TREE_VEC_ELT (pack, 0);
13455 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
13456 {
13457 tree right = TREE_VEC_ELT (pack, i);
13458 left = fold_expression (t, left, right, complain);
13459 }
13460 return left;
13461 }
13462
13463 /* Substitute into a unary left fold expression. */
13464
13465 static tree
13466 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
13467 tree in_decl)
13468 {
13469 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13470 if (pack == error_mark_node)
13471 return error_mark_node;
13472 if (PACK_EXPANSION_P (pack))
13473 {
13474 tree r = copy_node (t);
13475 FOLD_EXPR_PACK (r) = pack;
13476 return r;
13477 }
13478 if (TREE_VEC_LENGTH (pack) == 0)
13479 return expand_empty_fold (t, complain);
13480 else
13481 return expand_left_fold (t, pack, complain);
13482 }
13483
13484 /* Substitute into a binary left fold expression.
13485
13486 Do ths by building a single (non-empty) vector of argumnts and
13487 building the expression from those elements. */
13488
13489 static tree
13490 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
13491 tree in_decl)
13492 {
13493 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13494 if (pack == error_mark_node)
13495 return error_mark_node;
13496 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13497 if (init == error_mark_node)
13498 return error_mark_node;
13499
13500 if (PACK_EXPANSION_P (pack))
13501 {
13502 tree r = copy_node (t);
13503 FOLD_EXPR_PACK (r) = pack;
13504 FOLD_EXPR_INIT (r) = init;
13505 return r;
13506 }
13507
13508 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
13509 TREE_VEC_ELT (vec, 0) = init;
13510 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
13511 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
13512
13513 return expand_left_fold (t, vec, complain);
13514 }
13515
13516 /* Expand a PACK of arguments into a grouped as right fold.
13517 Given a pack containing elementns A0, A1, ..., and an
13518 operator @, this builds the expression:
13519
13520 A0@ ... (An-2 @ (An-1 @ An))
13521
13522 Note that PACK must not be empty.
13523
13524 The operator is defined by the original fold expression T. */
13525
13526 tree
13527 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
13528 {
13529 // Build the expression.
13530 int n = TREE_VEC_LENGTH (pack);
13531 tree right = TREE_VEC_ELT (pack, n - 1);
13532 for (--n; n != 0; --n)
13533 {
13534 tree left = TREE_VEC_ELT (pack, n - 1);
13535 right = fold_expression (t, left, right, complain);
13536 }
13537 return right;
13538 }
13539
13540 /* Substitute into a unary right fold expression. */
13541
13542 static tree
13543 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
13544 tree in_decl)
13545 {
13546 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13547 if (pack == error_mark_node)
13548 return error_mark_node;
13549 if (PACK_EXPANSION_P (pack))
13550 {
13551 tree r = copy_node (t);
13552 FOLD_EXPR_PACK (r) = pack;
13553 return r;
13554 }
13555 if (TREE_VEC_LENGTH (pack) == 0)
13556 return expand_empty_fold (t, complain);
13557 else
13558 return expand_right_fold (t, pack, complain);
13559 }
13560
13561 /* Substitute into a binary right fold expression.
13562
13563 Do ths by building a single (non-empty) vector of arguments and
13564 building the expression from those elements. */
13565
13566 static tree
13567 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
13568 tree in_decl)
13569 {
13570 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13571 if (pack == error_mark_node)
13572 return error_mark_node;
13573 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13574 if (init == error_mark_node)
13575 return error_mark_node;
13576
13577 if (PACK_EXPANSION_P (pack))
13578 {
13579 tree r = copy_node (t);
13580 FOLD_EXPR_PACK (r) = pack;
13581 FOLD_EXPR_INIT (r) = init;
13582 return r;
13583 }
13584
13585 int n = TREE_VEC_LENGTH (pack);
13586 tree vec = make_tree_vec (n + 1);
13587 for (int i = 0; i < n; ++i)
13588 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
13589 TREE_VEC_ELT (vec, n) = init;
13590
13591 return expand_right_fold (t, vec, complain);
13592 }
13593
13594 /* Walk through the pattern of a pack expansion, adding everything in
13595 local_specializations to a list. */
13596
13597 class el_data
13598 {
13599 public:
13600 /* Set of variables declared within the pattern. */
13601 hash_set<tree> internal;
13602 /* Set of AST nodes that have been visited by the traversal. */
13603 hash_set<tree> visited;
13604 /* List of local_specializations used within the pattern. */
13605 tree extra;
13606 tsubst_flags_t complain;
13607 /* True iff we don't want to walk into unevaluated contexts. */
13608 bool skip_unevaluated_operands = false;
13609 /* The unevaluated contexts that we avoided walking. */
13610 auto_vec<tree> skipped_trees;
13611
13612 el_data (tsubst_flags_t c)
13613 : extra (NULL_TREE), complain (c) {}
13614 };
13615 static tree
13616 extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
13617 {
13618 el_data &data = *reinterpret_cast<el_data*>(data_);
13619 tree *extra = &data.extra;
13620 tsubst_flags_t complain = data.complain;
13621
13622 if (data.skip_unevaluated_operands
13623 && unevaluated_p (TREE_CODE (*tp)))
13624 {
13625 data.skipped_trees.safe_push (*tp);
13626 *walk_subtrees = 0;
13627 return NULL_TREE;
13628 }
13629
13630 if (TYPE_P (*tp) && typedef_variant_p (*tp))
13631 /* Remember local typedefs (85214). */
13632 tp = &TYPE_NAME (*tp);
13633
13634 if (has_extra_args_mechanism_p (*tp))
13635 /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
13636 potentially see a previously captured local in an evaluated context
13637 that's really only used in an unevaluated context (PR114303). This
13638 means callers of build_extra_args need to clear *_EXTRA_ARGS of the
13639 outermost tree. Nested *_EXTRA_ARGS should naturally be empty since
13640 the outermost (extra-args) tree will intercept any substitution before
13641 a nested tree can. */
13642 gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE
13643 /* Except a lambda nested inside an extra-args tree
13644 can have extra args if we deferred partial
13645 substitution into it at template parse time. But
13646 we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway. */
13647 || TREE_CODE (*tp) == LAMBDA_EXPR);
13648
13649 if (TREE_CODE (*tp) == DECL_EXPR)
13650 {
13651 tree decl = DECL_EXPR_DECL (*tp);
13652 data.internal.add (decl);
13653 if (DECL_DECOMPOSITION_P (decl)
13654 && TREE_TYPE (decl) != error_mark_node)
13655 {
13656 gcc_assert (DECL_NAME (decl) == NULL_TREE);
13657 for (tree decl2 = DECL_CHAIN (decl);
13658 decl2
13659 && DECL_DECOMPOSITION_P (decl2)
13660 && DECL_NAME (decl2)
13661 && TREE_TYPE (decl2) != error_mark_node;
13662 decl2 = DECL_CHAIN (decl2))
13663 {
13664 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
13665 data.internal.add (decl2);
13666 }
13667 }
13668 }
13669 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
13670 {
13671 /* Since we defer implicit capture, look in the parms and body. */
13672 tree fn = lambda_function (*tp);
13673 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
13674 &data.visited);
13675 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
13676 &data.visited);
13677 }
13678 else if (tree spec = retrieve_local_specialization (*tp))
13679 {
13680 if (data.internal.contains (*tp))
13681 /* Don't mess with variables declared within the pattern. */
13682 return NULL_TREE;
13683 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13684 {
13685 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13686 tree args = ARGUMENT_PACK_ARGS (spec);
13687 if (TREE_VEC_LENGTH (args) == 1)
13688 {
13689 tree elt = TREE_VEC_ELT (args, 0);
13690 if (PACK_EXPANSION_P (elt))
13691 elt = PACK_EXPANSION_PATTERN (elt);
13692 if (DECL_PACK_P (elt))
13693 spec = elt;
13694 }
13695 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13696 {
13697 /* Handle lambda capture here, since we aren't doing any
13698 substitution now, and so tsubst_copy won't call
13699 process_outer_var_ref. */
13700 tree args = ARGUMENT_PACK_ARGS (spec);
13701 int len = TREE_VEC_LENGTH (args);
13702 for (int i = 0; i < len; ++i)
13703 {
13704 tree arg = TREE_VEC_ELT (args, i);
13705 tree carg = arg;
13706 if (outer_automatic_var_p (arg))
13707 carg = process_outer_var_ref (arg, complain);
13708 if (carg != arg)
13709 {
13710 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13711 proxies. */
13712 if (i == 0)
13713 {
13714 spec = copy_node (spec);
13715 args = copy_node (args);
13716 ARGUMENT_PACK_ARGS (spec) = args;
13717 register_local_specialization (spec, *tp);
13718 }
13719 TREE_VEC_ELT (args, i) = carg;
13720 }
13721 }
13722 }
13723 }
13724 if (outer_automatic_var_p (spec))
13725 spec = process_outer_var_ref (spec, complain);
13726 *extra = tree_cons (*tp, spec, *extra);
13727 }
13728 return NULL_TREE;
13729 }
13730 static tree
13731 extract_local_specs (tree pattern, tsubst_flags_t complain)
13732 {
13733 el_data data (complain);
13734 /* Walk the pattern twice, ignoring unevaluated operands the first time
13735 around, so that if a local specialization appears in both an evaluated
13736 and unevaluated context we prefer to process it in the evaluated context
13737 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13738 context). */
13739 data.skip_unevaluated_operands = true;
13740 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13741 /* Now walk the unevaluated contexts we skipped the first time around. */
13742 data.skip_unevaluated_operands = false;
13743 for (tree t : data.skipped_trees)
13744 {
13745 data.visited.remove (t);
13746 cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
13747 }
13748 return data.extra;
13749 }
13750
13751 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13752 for use in PACK_EXPANSION_EXTRA_ARGS. */
13753
13754 tree
13755 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
13756 {
13757 /* Make a copy of the extra arguments so that they won't get changed
13758 out from under us. */
13759 tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
13760 if ((complain & tf_partial) || TREE_STATIC (args))
13761 /* Remember whether this is a partial substitution. */
13762 TREE_STATIC (extra) = true;
13763 if (local_specializations)
13764 if (tree locals = extract_local_specs (pattern, complain))
13765 extra = tree_cons (NULL_TREE, extra, locals);
13766 return extra;
13767 }
13768
13769 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13770 normal template args to ARGS. */
13771
13772 tree
13773 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
13774 {
13775 if (!extra)
13776 return args;
13777
13778 if (TREE_CODE (extra) == TREE_LIST)
13779 {
13780 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
13781 {
13782 /* The partial instantiation involved local declarations collected in
13783 extract_local_specs; map from the general template to our local
13784 context. */
13785 tree gen = TREE_PURPOSE (elt);
13786 tree inst = TREE_VALUE (elt);
13787 if (DECL_P (inst))
13788 if (tree local = retrieve_local_specialization (inst))
13789 inst = local;
13790 /* else inst is already a full instantiation of the pack. */
13791 register_local_specialization (inst, gen);
13792 if (is_normal_capture_proxy (gen))
13793 register_local_specialization (inst, DECL_CAPTURED_VARIABLE (gen));
13794 }
13795 gcc_assert (!TREE_PURPOSE (extra));
13796 extra = TREE_VALUE (extra);
13797 }
13798 if (TREE_STATIC (extra))
13799 {
13800 /* This is a partial substitution into e.g. a requires-expr or lambda-expr
13801 inside a default template argument; we expect 'extra' to be a full set
13802 of template arguments for the template context, so it suffices to just
13803 substitute into them. */
13804 args = tsubst_template_args (extra, args, complain, in_decl);
13805 if (processing_template_decl)
13806 /* A templated substitution into a partial substitution is still a
13807 partial substitution. */
13808 TREE_STATIC (args) = true;
13809 }
13810 else
13811 args = add_to_template_args (extra, args);
13812 return args;
13813 }
13814
13815 /* Substitute ARGS into T, which is a pack expansion
13816 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13817 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13818 (if only a partial substitution could be performed) or
13819 ERROR_MARK_NODE if there was an error. */
13820
13821 tree
13822 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13823 tree in_decl)
13824 {
13825 tree pattern;
13826 tree pack, packs = NULL_TREE;
13827 bool unsubstituted_packs = false;
13828 int i, len = -1;
13829 tree result;
13830 bool need_local_specializations = false;
13831 int levels;
13832
13833 gcc_assert (PACK_EXPANSION_P (t));
13834 pattern = PACK_EXPANSION_PATTERN (t);
13835
13836 /* Add in any args remembered from an earlier partial instantiation. */
13837 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13838
13839 levels = TMPL_ARGS_DEPTH (args);
13840
13841 /* Determine the argument packs that will instantiate the parameter
13842 packs used in the expansion expression. While we're at it,
13843 compute the number of arguments to be expanded and make sure it
13844 is consistent. */
13845 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13846 pack = TREE_CHAIN (pack))
13847 {
13848 tree parm_pack = TREE_VALUE (pack);
13849 tree arg_pack = NULL_TREE;
13850 tree orig_arg = NULL_TREE;
13851 int level = 0;
13852
13853 if (TREE_CODE (parm_pack) == BASES)
13854 {
13855 gcc_assert (parm_pack == pattern);
13856 tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
13857 if (BASES_DIRECT (parm_pack))
13858 return calculate_direct_bases (type, complain);
13859 else
13860 return calculate_bases (type, complain);
13861 }
13862 else if (builtin_pack_call_p (parm_pack))
13863 {
13864 if (parm_pack != pattern)
13865 {
13866 if (complain & tf_error)
13867 sorry ("%qE is not the entire pattern of the pack expansion",
13868 parm_pack);
13869 return error_mark_node;
13870 }
13871 return expand_builtin_pack_call (parm_pack, args,
13872 complain, in_decl);
13873 }
13874 else if (TREE_CODE (parm_pack) == PARM_DECL)
13875 {
13876 /* We know we have correct local_specializations if this
13877 expansion is at function scope, or if we're dealing with a
13878 local parameter in a requires expression; for the latter,
13879 tsubst_requires_expr set it up appropriately. */
13880 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13881 arg_pack = retrieve_local_specialization (parm_pack);
13882 else
13883 /* We can't rely on local_specializations for a parameter
13884 name used later in a function declaration (such as in a
13885 late-specified return type). Even if it exists, it might
13886 have the wrong value for a recursive call. */
13887 need_local_specializations = true;
13888
13889 if (!arg_pack)
13890 {
13891 /* This parameter pack was used in an unevaluated context. Just
13892 make a dummy decl, since it's only used for its type. */
13893 ++cp_unevaluated_operand;
13894 arg_pack = tsubst_decl (parm_pack, args, complain);
13895 --cp_unevaluated_operand;
13896 if (arg_pack && DECL_PACK_P (arg_pack))
13897 /* Partial instantiation of the parm_pack, we can't build
13898 up an argument pack yet. */
13899 arg_pack = NULL_TREE;
13900 else
13901 arg_pack = make_fnparm_pack (arg_pack);
13902 }
13903 else if (DECL_PACK_P (arg_pack))
13904 /* This argument pack isn't fully instantiated yet. */
13905 arg_pack = NULL_TREE;
13906 }
13907 else if (is_capture_proxy (parm_pack))
13908 {
13909 arg_pack = retrieve_local_specialization (parm_pack);
13910 if (DECL_PACK_P (arg_pack))
13911 arg_pack = NULL_TREE;
13912 }
13913 else
13914 {
13915 int idx;
13916 template_parm_level_and_index (parm_pack, &level, &idx);
13917 if (level <= levels)
13918 arg_pack = TMPL_ARG (args, level, idx);
13919
13920 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13921 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13922 arg_pack = NULL_TREE;
13923 }
13924
13925 orig_arg = arg_pack;
13926 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13927 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13928
13929 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13930 /* This can only happen if we forget to expand an argument
13931 pack somewhere else. Just return an error, silently. */
13932 {
13933 result = make_tree_vec (1);
13934 TREE_VEC_ELT (result, 0) = error_mark_node;
13935 return result;
13936 }
13937
13938 if (arg_pack)
13939 {
13940 int my_len =
13941 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13942
13943 /* Don't bother trying to do a partial substitution with
13944 incomplete packs; we'll try again after deduction. */
13945 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13946 return t;
13947
13948 if (len < 0)
13949 len = my_len;
13950 else if (len != my_len)
13951 {
13952 if (!(complain & tf_error))
13953 /* Fail quietly. */;
13954 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13955 error ("mismatched argument pack lengths while expanding %qT",
13956 pattern);
13957 else
13958 error ("mismatched argument pack lengths while expanding %qE",
13959 pattern);
13960 return error_mark_node;
13961 }
13962
13963 /* Keep track of the parameter packs and their corresponding
13964 argument packs. */
13965 packs = tree_cons (parm_pack, arg_pack, packs);
13966 TREE_TYPE (packs) = orig_arg;
13967 }
13968 else
13969 {
13970 /* We can't substitute for this parameter pack. We use a flag as
13971 well as the missing_level counter because function parameter
13972 packs don't have a level. */
13973 gcc_assert (processing_template_decl || is_auto (parm_pack)
13974 || args == NULL_TREE);
13975 unsubstituted_packs = true;
13976 }
13977 }
13978
13979 /* If the expansion is just T..., return the matching argument pack, unless
13980 we need to call convert_from_reference on all the elements. This is an
13981 important optimization; see c++/68422. */
13982 if (!unsubstituted_packs
13983 && TREE_PURPOSE (packs) == pattern)
13984 {
13985 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13986
13987 /* If the argument pack is a single pack expansion, pull it out. */
13988 if (TREE_VEC_LENGTH (args) == 1
13989 && pack_expansion_args_count (args))
13990 {
13991 tree arg = TREE_VEC_ELT (args, 0);
13992 if (PACK_EXPANSION_SIZEOF_P (t)
13993 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
13994 /* Except if this isn't a simple sizeof...(T) which gets sZ
13995 mangling, keep the TREE_VEC to get sP mangling. */;
13996 else
13997 return TREE_VEC_ELT (args, 0);
13998 }
13999
14000 /* Types need no adjustment, nor does sizeof..., and if we still have
14001 some pack expansion args we won't do anything yet. */
14002 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
14003 || PACK_EXPANSION_SIZEOF_P (t)
14004 || pack_expansion_args_count (args))
14005 return args;
14006 /* Also optimize expression pack expansions if we can tell that the
14007 elements won't have reference type. */
14008 tree type = TREE_TYPE (pattern);
14009 if (type && !TYPE_REF_P (type)
14010 && !PACK_EXPANSION_P (type)
14011 && !WILDCARD_TYPE_P (type))
14012 return args;
14013 /* Otherwise use the normal path so we get convert_from_reference. */
14014 }
14015
14016 /* We cannot expand this expansion expression, because we don't have
14017 all of the argument packs we need. */
14018 if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
14019 {
14020 /* We got some full packs, but we can't substitute them in until we
14021 have values for all the packs. So remember these until then. */
14022
14023 t = make_pack_expansion (pattern, complain);
14024 PACK_EXPANSION_EXTRA_ARGS (t)
14025 = build_extra_args (pattern, args, complain);
14026 return t;
14027 }
14028
14029 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
14030 type, so create our own local specializations map; the current map is
14031 either NULL or (in the case of recursive unification) might have
14032 bindings that we don't want to use or alter. */
14033 local_specialization_stack lss (need_local_specializations
14034 ? lss_blank : lss_nop);
14035
14036 if (unsubstituted_packs)
14037 {
14038 /* There were no real arguments, we're just replacing a parameter
14039 pack with another version of itself. Substitute into the
14040 pattern and return a PACK_EXPANSION_*. The caller will need to
14041 deal with that. */
14042 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
14043 result = tsubst_expr (pattern, args, complain, in_decl);
14044 else
14045 result = tsubst (pattern, args, complain, in_decl);
14046 result = make_pack_expansion (result, complain);
14047 if (result == error_mark_node)
14048 return error_mark_node;
14049 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
14050 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
14051 if (PACK_EXPANSION_AUTO_P (t))
14052 {
14053 /* This is a fake auto... pack expansion created in add_capture with
14054 _PACKS that don't appear in the pattern. Copy one over. */
14055 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
14056 pack = retrieve_local_specialization (TREE_VALUE (packs));
14057 gcc_checking_assert (DECL_PACK_P (pack));
14058 PACK_EXPANSION_PARAMETER_PACKS (result)
14059 = build_tree_list (NULL_TREE, pack);
14060 PACK_EXPANSION_AUTO_P (result) = true;
14061 }
14062 return result;
14063 }
14064
14065 gcc_assert (len >= 0);
14066
14067 /* For each argument in each argument pack, substitute into the
14068 pattern. */
14069 result = make_tree_vec (len);
14070 tree elem_args = copy_template_args (args);
14071 for (i = 0; i < len; ++i)
14072 {
14073 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
14074 i,
14075 elem_args, complain,
14076 in_decl);
14077 TREE_VEC_ELT (result, i) = t;
14078 if (t == error_mark_node)
14079 {
14080 result = error_mark_node;
14081 break;
14082 }
14083 }
14084
14085 /* Update ARGS to restore the substitution from parameter packs to
14086 their argument packs. */
14087 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14088 {
14089 tree parm = TREE_PURPOSE (pack);
14090
14091 if (TREE_CODE (parm) == PARM_DECL
14092 || VAR_P (parm)
14093 || TREE_CODE (parm) == FIELD_DECL)
14094 register_local_specialization (TREE_TYPE (pack), parm);
14095 else
14096 {
14097 int idx, level;
14098
14099 if (TREE_VALUE (pack) == NULL_TREE)
14100 continue;
14101
14102 template_parm_level_and_index (parm, &level, &idx);
14103
14104 /* Update the corresponding argument. */
14105 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14106 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
14107 TREE_TYPE (pack);
14108 else
14109 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
14110 }
14111 }
14112
14113 /* If the dependent pack arguments were such that we end up with only a
14114 single pack expansion again, there's no need to keep it in a TREE_VEC. */
14115 if (len == 1 && TREE_CODE (result) == TREE_VEC
14116 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
14117 return TREE_VEC_ELT (result, 0);
14118
14119 return result;
14120 }
14121
14122 /* Substitute ARGS into T, which is a TREE_VEC. This function creates a new
14123 TREE_VEC rather than substituting the elements in-place. */
14124
14125 static tree
14126 tsubst_tree_vec (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14127 {
14128 const int len = TREE_VEC_LENGTH (t);
14129 tree r = make_tree_vec (len);
14130 for (int i = 0; i < len; ++i)
14131 {
14132 tree arg = TREE_VEC_ELT (t, i);
14133 if (TYPE_P (arg))
14134 TREE_VEC_ELT (r, i) = tsubst (arg, args, complain, in_decl);
14135 else
14136 TREE_VEC_ELT (r, i) = tsubst_expr (arg, args, complain, in_decl);
14137 }
14138 return r;
14139 }
14140
14141 /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or
14142 PACK_INDEX_EXPR). Returns a single type or expression, a PACK_INDEX_*
14143 node if only a partial substitution could be performed, or ERROR_MARK_NODE
14144 if there was an error. */
14145
14146 tree
14147 tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14148 {
14149 tree pack = PACK_INDEX_PACK (t);
14150 if (PACK_EXPANSION_P (pack))
14151 pack = tsubst_pack_expansion (pack, args, complain, in_decl);
14152 else
14153 {
14154 /* PACK can be {*args#0} whose args#0's value-expr refers to
14155 a partially instantiated closure. Let tsubst find the
14156 fully-instantiated one. */
14157 gcc_assert (TREE_CODE (pack) == TREE_VEC);
14158 pack = tsubst_tree_vec (pack, args, complain, in_decl);
14159 }
14160 if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0)
14161 {
14162 if (complain & tf_error)
14163 error ("cannot index an empty pack");
14164 return error_mark_node;
14165 }
14166 tree index = tsubst_expr (PACK_INDEX_INDEX (t), args, complain, in_decl);
14167 const bool parenthesized_p = (TREE_CODE (t) == PACK_INDEX_EXPR
14168 && PACK_INDEX_PARENTHESIZED_P (t));
14169 tree r;
14170 if (!type_dependent_expression_p (index))
14171 index = build_converted_constant_expr (size_type_node, index, complain);
14172 if (error_operand_p (index))
14173 return error_mark_node;
14174 if (!value_dependent_expression_p (index) && TREE_CODE (pack) == TREE_VEC)
14175 r = pack_index_element (index, pack, parenthesized_p, complain);
14176 else
14177 r = make_pack_index (pack, index);
14178 if (TREE_CODE (t) == PACK_INDEX_TYPE)
14179 r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
14180 complain | tf_ignore_bad_quals);
14181 return r;
14182 }
14183
14184 /* Make an argument pack out of the TREE_VEC VEC. */
14185
14186 static tree
14187 make_argument_pack (tree vec)
14188 {
14189 tree pack;
14190
14191 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
14192 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
14193 else
14194 {
14195 pack = make_node (NONTYPE_ARGUMENT_PACK);
14196 TREE_CONSTANT (pack) = 1;
14197 }
14198 ARGUMENT_PACK_ARGS (pack) = vec;
14199 return pack;
14200 }
14201
14202 /* Return an exact copy of template args T that can be modified
14203 independently. */
14204
14205 static tree
14206 copy_template_args (tree t)
14207 {
14208 if (t == error_mark_node)
14209 return t;
14210
14211 int len = TREE_VEC_LENGTH (t);
14212 tree new_vec = make_tree_vec (len);
14213
14214 for (int i = 0; i < len; ++i)
14215 {
14216 tree elt = TREE_VEC_ELT (t, i);
14217 if (elt && TREE_CODE (elt) == TREE_VEC)
14218 elt = copy_template_args (elt);
14219 TREE_VEC_ELT (new_vec, i) = elt;
14220 }
14221
14222 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
14223 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
14224
14225 return new_vec;
14226 }
14227
14228 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
14229
14230 tree
14231 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
14232 tree in_decl)
14233 {
14234 /* This flag is used only during deduction, and we don't expect to
14235 substitute such ARGUMENT_PACKs. */
14236 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
14237
14238 /* Substitute into each of the arguments. */
14239 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
14240 args, complain, in_decl);
14241 if (pack_args == error_mark_node)
14242 return error_mark_node;
14243
14244 if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
14245 return orig_arg;
14246
14247 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
14248 template parameter, we might be able to avoid allocating a new
14249 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
14250 if the substituted result is identical to it. */
14251 if (tree parm = template_arg_to_parm (orig_arg))
14252 {
14253 int level, index;
14254 template_parm_level_and_index (parm, &level, &index);
14255 if (TMPL_ARGS_DEPTH (args) >= level)
14256 if (tree arg = TMPL_ARG (args, level, index))
14257 if (TREE_CODE (arg) == TREE_CODE (orig_arg)
14258 && ARGUMENT_PACK_ARGS (arg) == pack_args)
14259 {
14260 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
14261 return arg;
14262 }
14263 }
14264
14265 tree new_arg;
14266 if (TYPE_P (orig_arg))
14267 {
14268 new_arg = cxx_make_type (TREE_CODE (orig_arg));
14269 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
14270 }
14271 else
14272 {
14273 new_arg = make_node (TREE_CODE (orig_arg));
14274 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
14275 }
14276 ARGUMENT_PACK_ARGS (new_arg) = pack_args;
14277 return new_arg;
14278 }
14279
14280 /* Substitute ARGS into the vector or list of template arguments T. */
14281
14282 tree
14283 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14284 {
14285 if (t == error_mark_node)
14286 return error_mark_node;
14287
14288 /* In "sizeof(X<I>)" we need to evaluate "I". */
14289 cp_evaluated ev;
14290
14291 const int len = TREE_VEC_LENGTH (t);
14292 tree *elts = XALLOCAVEC (tree, len);
14293 int expanded_len_adjust = 0;
14294
14295 /* True iff the substituted result is identical to T. */
14296 bool const_subst_p = true;
14297
14298 for (int i = 0; i < len; i++)
14299 {
14300 tree orig_arg = TREE_VEC_ELT (t, i);
14301 tree new_arg;
14302
14303 if (!orig_arg)
14304 new_arg = NULL_TREE;
14305 else if (TREE_CODE (orig_arg) == TREE_VEC)
14306 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
14307 else if (PACK_EXPANSION_P (orig_arg))
14308 {
14309 /* Substitute into an expansion expression. */
14310 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
14311
14312 if (TREE_CODE (new_arg) == TREE_VEC)
14313 /* Add to the expanded length adjustment the number of
14314 expanded arguments. We subtract one from this
14315 measurement, because the argument pack expression
14316 itself is already counted as 1 in
14317 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
14318 the argument pack is empty. */
14319 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
14320 }
14321 else if (ARGUMENT_PACK_P (orig_arg))
14322 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
14323 else
14324 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
14325
14326 if (new_arg == error_mark_node)
14327 return error_mark_node;
14328
14329 elts[i] = new_arg;
14330 if (new_arg != orig_arg)
14331 const_subst_p = false;
14332 }
14333
14334 if (const_subst_p)
14335 return t;
14336
14337 tree maybe_reuse = NULL_TREE;
14338
14339 /* If ARGS and T are both multi-level, the substituted result may be
14340 identical to ARGS. */
14341 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
14342 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
14343 && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
14344 maybe_reuse = args;
14345 /* If T appears to be a vector of generic template arguments, the
14346 substituted result may be identical to the corresponding level
14347 from ARGS. */
14348 else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
14349 {
14350 int level, index;
14351 template_parm_level_and_index (parm, &level, &index);
14352 if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
14353 maybe_reuse = TMPL_ARGS_LEVEL (args, level);
14354 }
14355
14356 /* If the substituted result is identical to MAYBE_REUSE, return
14357 it and avoid allocating a new TREE_VEC, as an optimization. */
14358 if (maybe_reuse != NULL_TREE
14359 && TREE_VEC_LENGTH (maybe_reuse) == len
14360 && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
14361 return maybe_reuse;
14362
14363 /* If T consists of only a pack expansion for which substitution yielded
14364 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
14365 of effectively making a copy. */
14366 if (len == 1
14367 && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
14368 && TREE_CODE (elts[0]) == TREE_VEC)
14369 return elts[0];
14370
14371 /* Make space for the expanded arguments coming from template
14372 argument packs. */
14373 tree r = make_tree_vec (len + expanded_len_adjust);
14374 /* T can contain TREE_VECs. That happens if T contains the
14375 arguments for a member template.
14376 In that case each TREE_VEC in T represents a level of template
14377 arguments, and T won't carry any non defaulted argument count.
14378 It will rather be the nested TREE_VECs that will carry one.
14379 In other words, T carries a non defaulted argument count only
14380 if it doesn't contain any nested TREE_VEC. */
14381 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
14382 {
14383 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
14384 count += expanded_len_adjust;
14385 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
14386 }
14387
14388 int out = 0;
14389 for (int i = 0; i < len; i++)
14390 {
14391 tree orig_arg = TREE_VEC_ELT (t, i);
14392 if (orig_arg
14393 && PACK_EXPANSION_P (orig_arg)
14394 && TREE_CODE (elts[i]) == TREE_VEC)
14395 {
14396 /* Now expand the template argument pack "in place". */
14397 for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
14398 TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
14399 }
14400 else
14401 {
14402 TREE_VEC_ELT (r, out) = elts[i];
14403 out++;
14404 }
14405 }
14406 gcc_assert (out == TREE_VEC_LENGTH (r));
14407
14408 return r;
14409 }
14410
14411 /* Substitute ARGS into one level PARMS of template parameters. */
14412
14413 static tree
14414 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
14415 {
14416 if (parms == error_mark_node)
14417 return error_mark_node;
14418
14419 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
14420
14421 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
14422 {
14423 tree tuple = TREE_VEC_ELT (parms, i);
14424
14425 if (tuple == error_mark_node)
14426 continue;
14427
14428 TREE_VEC_ELT (new_vec, i) =
14429 tsubst_template_parm (tuple, args, complain);
14430 }
14431
14432 return new_vec;
14433 }
14434
14435 /* Return the result of substituting ARGS into the template parameters
14436 given by PARMS. If there are m levels of ARGS and m + n levels of
14437 PARMS, then the result will contain n levels of PARMS. For
14438 example, if PARMS is `template <class T> template <class U>
14439 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
14440 result will be `template <int*, double, class V>'. */
14441
14442 static tree
14443 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
14444 {
14445 tree r = NULL_TREE;
14446 tree* new_parms;
14447
14448 /* When substituting into a template, we must set
14449 PROCESSING_TEMPLATE_DECL as the template parameters may be
14450 dependent if they are based on one-another, and the dependency
14451 predicates are short-circuit outside of templates. */
14452 ++processing_template_decl;
14453
14454 for (new_parms = &r;
14455 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
14456 new_parms = &(TREE_CHAIN (*new_parms)),
14457 parms = TREE_CHAIN (parms))
14458 {
14459 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
14460 args, complain);
14461 *new_parms =
14462 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
14463 - TMPL_ARGS_DEPTH (args)),
14464 new_vec, NULL_TREE);
14465 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
14466 = TEMPLATE_PARMS_CONSTRAINTS (parms);
14467 }
14468
14469 --processing_template_decl;
14470
14471 return r;
14472 }
14473
14474 /* Return the result of substituting ARGS into one template parameter
14475 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
14476 parameter and which TREE_PURPOSE is the default argument of the
14477 template parameter. */
14478
14479 static tree
14480 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
14481 {
14482 tree default_value, parm_decl;
14483
14484 if (args == NULL_TREE
14485 || t == NULL_TREE
14486 || t == error_mark_node)
14487 return t;
14488
14489 gcc_assert (TREE_CODE (t) == TREE_LIST);
14490
14491 default_value = TREE_PURPOSE (t);
14492 parm_decl = TREE_VALUE (t);
14493
14494 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
14495 if (TREE_CODE (parm_decl) == PARM_DECL
14496 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
14497 parm_decl = error_mark_node;
14498 default_value = tsubst_template_arg (default_value, args,
14499 complain, NULL_TREE);
14500
14501 tree r = build_tree_list (default_value, parm_decl);
14502 TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
14503 return r;
14504 }
14505
14506 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
14507 parameter in PARMS for sake of declaration matching. */
14508
14509 static void
14510 tsubst_each_template_parm_constraints (tree parms, tree args,
14511 tsubst_flags_t complain)
14512 {
14513 ++processing_template_decl;
14514 for (; parms; parms = TREE_CHAIN (parms))
14515 {
14516 tree level = TREE_VALUE (parms);
14517 for (tree parm : tree_vec_range (level))
14518 TEMPLATE_PARM_CONSTRAINTS (parm)
14519 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
14520 complain, NULL_TREE);
14521 }
14522 --processing_template_decl;
14523 }
14524
14525 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
14526 indexed in reverse order of the parameters. */
14527
14528 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
14529
14530 /* Return a reference to the vec* of defarg insts for FN. */
14531
14532 static vec<tree,va_gc> *&
14533 defarg_insts_for (tree fn)
14534 {
14535 if (!defarg_inst)
14536 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
14537 tree_vec_map in = { { fn }, nullptr };
14538 tree_vec_map **slot
14539 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
14540 if (!*slot)
14541 {
14542 *slot = ggc_alloc<tree_vec_map> ();
14543 **slot = in;
14544 }
14545 return (*slot)->to;
14546 }
14547
14548 /* Substitute into the default argument ARG (a default argument for
14549 FN), which has the indicated TYPE. */
14550
14551 tree
14552 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
14553 tsubst_flags_t complain)
14554 {
14555 int errs = errorcount + sorrycount;
14556
14557 /* This can happen in invalid code. */
14558 if (TREE_CODE (arg) == DEFERRED_PARSE)
14559 return arg;
14560
14561 /* Shortcut {}. */
14562 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
14563 && CONSTRUCTOR_NELTS (arg) == 0)
14564 return arg;
14565
14566 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14567 parm = chain_index (parmnum, parm);
14568 tree parmtype = TREE_TYPE (parm);
14569 if (DECL_BY_REFERENCE (parm))
14570 parmtype = TREE_TYPE (parmtype);
14571 if (parmtype == error_mark_node)
14572 return error_mark_node;
14573
14574 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
14575
14576 /* Remember the location of the pointer to the vec rather than the location
14577 of the particular element, in case the vec grows in tsubst_expr. */
14578 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
14579 /* Index in reverse order to avoid allocating space for initial parameters
14580 that don't have default arguments. */
14581 unsigned ridx = list_length (parm);
14582 if (vec_safe_length (defs) < ridx)
14583 vec_safe_grow_cleared (defs, ridx);
14584 else if (tree inst = (*defs)[ridx - 1])
14585 return inst;
14586
14587 /* This default argument came from a template. Instantiate the
14588 default argument here, not in tsubst. In the case of
14589 something like:
14590
14591 template <class T>
14592 struct S {
14593 static T t();
14594 void f(T = t());
14595 };
14596
14597 we must be careful to do name lookup in the scope of S<T>,
14598 rather than in the current class. */
14599 push_to_top_level ();
14600 push_access_scope (fn);
14601 push_deferring_access_checks (dk_no_deferred);
14602 /* So in_immediate_context knows this is a default argument. */
14603 begin_scope (sk_function_parms, fn);
14604 start_lambda_scope (parm);
14605
14606 /* The default argument expression may cause implicitly defined
14607 member functions to be synthesized, which will result in garbage
14608 collection. We must treat this situation as if we were within
14609 the body of function so as to avoid collecting live data on the
14610 stack. */
14611 ++function_depth;
14612 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
14613 --function_depth;
14614
14615 finish_lambda_scope ();
14616
14617 /* Make sure the default argument is reasonable. */
14618 arg = check_default_argument (type, arg, complain);
14619
14620 if (errorcount+sorrycount > errs
14621 && (complain & tf_warning_or_error))
14622 inform (input_location,
14623 " when instantiating default argument for call to %qD", fn);
14624
14625 leave_scope ();
14626 pop_deferring_access_checks ();
14627 pop_access_scope (fn);
14628 pop_from_top_level ();
14629
14630 if (arg != error_mark_node && !cp_unevaluated_operand)
14631 (*defs)[ridx - 1] = arg;
14632
14633 return arg;
14634 }
14635
14636 /* Substitute into all the default arguments for FN. */
14637
14638 static void
14639 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
14640 {
14641 tree arg;
14642 tree tmpl_args;
14643
14644 tmpl_args = DECL_TI_ARGS (fn);
14645
14646 /* If this function is not yet instantiated, we certainly don't need
14647 its default arguments. */
14648 if (uses_template_parms (tmpl_args))
14649 return;
14650 /* Don't do this again for clones. */
14651 if (DECL_CLONED_FUNCTION_P (fn))
14652 return;
14653
14654 int i = 0;
14655 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
14656 arg;
14657 arg = TREE_CHAIN (arg), ++i)
14658 if (TREE_PURPOSE (arg))
14659 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
14660 TREE_VALUE (arg),
14661 TREE_PURPOSE (arg),
14662 complain);
14663 }
14664
14665 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14666 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
14667
14668 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14669
14670 void
14671 store_explicit_specifier (tree v, tree t)
14672 {
14673 if (!explicit_specifier_map)
14674 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
14675 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
14676 explicit_specifier_map->put (v, t);
14677 }
14678
14679 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14680
14681 tree
14682 lookup_explicit_specifier (tree v)
14683 {
14684 return *explicit_specifier_map->get (v);
14685 }
14686
14687 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14688 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14689 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14690 identical to T. */
14691
14692 static tree
14693 rebuild_function_or_method_type (tree t, tree args, tree return_type,
14694 tree arg_types, tree raises,
14695 tsubst_flags_t complain)
14696 {
14697 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
14698
14699 tree new_type;
14700 if (TREE_CODE (t) == FUNCTION_TYPE)
14701 {
14702 new_type = build_function_type (return_type, arg_types);
14703 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
14704 }
14705 else
14706 {
14707 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14708 /* Don't pick up extra function qualifiers from the basetype. */
14709 r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
14710 if (! MAYBE_CLASS_TYPE_P (r))
14711 {
14712 /* [temp.deduct]
14713
14714 Type deduction may fail for any of the following
14715 reasons:
14716
14717 -- Attempting to create "pointer to member of T" when T
14718 is not a class type. */
14719 if (complain & tf_error)
14720 error ("creating pointer to member function of non-class type %qT",
14721 r);
14722 return error_mark_node;
14723 }
14724
14725 new_type = build_method_type_directly (r, return_type,
14726 TREE_CHAIN (arg_types));
14727 }
14728 if (!apply_late_template_attributes (&new_type, TYPE_ATTRIBUTES (t), 0,
14729 args, complain, NULL_TREE))
14730 return error_mark_node;
14731
14732 cp_ref_qualifier rqual = type_memfn_rqual (t);
14733 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14734 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
14735 }
14736
14737 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14738 each of its formal parameters. If there is a disagreement then rebuild
14739 DECL's function type according to its formal parameter types, as part of a
14740 resolution for Core issues 1001/1322. */
14741
14742 static void
14743 maybe_rebuild_function_decl_type (tree decl, tree args)
14744 {
14745 bool function_type_needs_rebuilding = false;
14746 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
14747 {
14748 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
14749 while (parm_type_list && parm_type_list != void_list_node)
14750 {
14751 tree parm_type = TREE_VALUE (parm_type_list);
14752 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14753 if (!same_type_p (parm_type, formal_parm_type_unqual))
14754 {
14755 function_type_needs_rebuilding = true;
14756 break;
14757 }
14758
14759 parm_list = DECL_CHAIN (parm_list);
14760 parm_type_list = TREE_CHAIN (parm_type_list);
14761 }
14762 }
14763
14764 if (!function_type_needs_rebuilding)
14765 return;
14766
14767 const tree fntype = TREE_TYPE (decl);
14768 tree parm_list = DECL_ARGUMENTS (decl);
14769 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
14770 tree new_parm_type_list = NULL_TREE;
14771 tree *q = &new_parm_type_list;
14772 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
14773 {
14774 *q = copy_node (old_parm_type_list);
14775 parm_list = DECL_CHAIN (parm_list);
14776 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14777 q = &TREE_CHAIN (*q);
14778 }
14779 while (old_parm_type_list && old_parm_type_list != void_list_node)
14780 {
14781 *q = copy_node (old_parm_type_list);
14782 tree *new_parm_type = &TREE_VALUE (*q);
14783 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14784 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
14785 *new_parm_type = formal_parm_type_unqual;
14786
14787 parm_list = DECL_CHAIN (parm_list);
14788 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14789 q = &TREE_CHAIN (*q);
14790 }
14791 if (old_parm_type_list == void_list_node)
14792 *q = void_list_node;
14793
14794 TREE_TYPE (decl)
14795 = rebuild_function_or_method_type (fntype, args,
14796 TREE_TYPE (fntype), new_parm_type_list,
14797 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
14798 }
14799
14800 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14801
14802 static tree
14803 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
14804 tree lambda_fntype, bool use_spec_table = true)
14805 {
14806 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
14807 hashval_t hash = 0;
14808 tree in_decl = t;
14809
14810 /* Nobody should be tsubst'ing into non-template functions. */
14811 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
14812 || DECL_LOCAL_DECL_P (t));
14813
14814 if (DECL_LOCAL_DECL_P (t))
14815 {
14816 if (tree spec = retrieve_local_specialization (t))
14817 return spec;
14818 }
14819 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
14820 {
14821 /* If T is not dependent, just return it. */
14822 if (!uses_template_parms (DECL_TI_ARGS (t))
14823 && !LAMBDA_FUNCTION_P (t))
14824 return t;
14825
14826 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14827 if (non_templated_friend_p (t))
14828 goto friend_case;
14829
14830 /* Calculate the most general template of which R is a
14831 specialization. */
14832 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
14833
14834 /* We're substituting a lambda function under tsubst_lambda_expr but not
14835 directly from it; find the matching function we're already inside.
14836 But don't do this if T is a generic lambda with a single level of
14837 template parms, as in that case we're doing a normal instantiation. */
14838 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
14839 && (!generic_lambda_fn_p (t)
14840 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
14841 return enclosing_instantiation_of (t);
14842
14843 /* Calculate the complete set of arguments used to
14844 specialize R. */
14845 if (use_spec_table && !lambda_fntype)
14846 {
14847 argvec = tsubst_template_args (DECL_TI_ARGS
14848 (DECL_TEMPLATE_RESULT
14849 (DECL_TI_TEMPLATE (t))),
14850 args, complain, in_decl);
14851 if (argvec == error_mark_node)
14852 return error_mark_node;
14853
14854 /* Check to see if we already have this specialization. */
14855 hash = spec_hasher::hash (gen_tmpl, argvec);
14856 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
14857 /* The spec for these args might be a partial instantiation of the
14858 template, but here what we want is the FUNCTION_DECL. */
14859 return STRIP_TEMPLATE (spec);
14860 }
14861 else
14862 argvec = args;
14863 }
14864 else
14865 {
14866 /* This special case arises when we have something like this:
14867
14868 template <class T> struct S {
14869 friend void f<int>(int, double);
14870 };
14871
14872 Here, the DECL_TI_TEMPLATE for the friend declaration
14873 will be an IDENTIFIER_NODE. We are being called from
14874 tsubst_friend_function, and we want only to create a
14875 new decl (R) with appropriate types so that we can call
14876 determine_specialization. */
14877 friend_case:
14878 gen_tmpl = NULL_TREE;
14879 argvec = NULL_TREE;
14880 }
14881
14882 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14883 : NULL_TREE);
14884 tree ctx = closure ? closure : DECL_CONTEXT (t);
14885 bool member = ctx && TYPE_P (ctx);
14886
14887 /* If this is a static or xobj lambda, remove the 'this' pointer added in
14888 tsubst_lambda_expr now that we know the closure type. */
14889 if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
14890 lambda_fntype = static_fn_type (lambda_fntype);
14891
14892 if (member && !closure)
14893 ctx = tsubst_entering_scope (ctx, args, complain, t);
14894
14895 tree type = (lambda_fntype ? lambda_fntype
14896 : tsubst (TREE_TYPE (t), args,
14897 complain | tf_fndecl_type, in_decl));
14898 if (type == error_mark_node)
14899 return error_mark_node;
14900
14901 /* If we hit excessive deduction depth, the type is bogus even if
14902 it isn't error_mark_node, so don't build a decl. */
14903 if (excessive_deduction_depth)
14904 return error_mark_node;
14905
14906 /* We do NOT check for matching decls pushed separately at this
14907 point, as they may not represent instantiations of this
14908 template, and in any case are considered separate under the
14909 discrete model. */
14910 tree r = copy_decl (t);
14911 DECL_USE_TEMPLATE (r) = 0;
14912 TREE_TYPE (r) = type;
14913 /* Clear out the mangled name and RTL for the instantiation. */
14914 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14915 SET_DECL_RTL (r, NULL);
14916 /* Leave DECL_INITIAL set on deleted instantiations. */
14917 if (!DECL_DELETED_FN (r))
14918 DECL_INITIAL (r) = NULL_TREE;
14919 DECL_CONTEXT (r) = ctx;
14920 set_instantiating_module (r);
14921
14922 /* Handle explicit(dependent-expr). */
14923 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14924 {
14925 tree spec = lookup_explicit_specifier (t);
14926 spec = tsubst_expr (spec, args, complain, in_decl);
14927 spec = build_explicit_specifier (spec, complain);
14928 if (spec == error_mark_node)
14929 return error_mark_node;
14930 if (instantiation_dependent_expression_p (spec))
14931 store_explicit_specifier (r, spec);
14932 else
14933 {
14934 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14935 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
14936 }
14937 }
14938
14939 /* OpenMP UDRs have the only argument a reference to the declared
14940 type. We want to diagnose if the declared type is a reference,
14941 which is invalid, but as references to references are usually
14942 quietly merged, diagnose it here. */
14943 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14944 {
14945 tree argtype
14946 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14947 argtype = tsubst (argtype, args, complain, in_decl);
14948 if (TYPE_REF_P (argtype))
14949 error_at (DECL_SOURCE_LOCATION (t),
14950 "reference type %qT in "
14951 "%<#pragma omp declare reduction%>", argtype);
14952 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14953 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14954 argtype);
14955 }
14956
14957 if (member && DECL_CONV_FN_P (r))
14958 /* Type-conversion operator. Reconstruct the name, in
14959 case it's the name of one of the template's parameters. */
14960 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14961
14962 tree parms = DECL_ARGUMENTS (t);
14963 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14964 parms = DECL_CHAIN (parms);
14965 parms = tsubst (parms, args, complain, t);
14966 if (parms == error_mark_node)
14967 return error_mark_node;
14968 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14969 DECL_CONTEXT (parm) = r;
14970 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14971 {
14972 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14973 DECL_NAME (tparm) = closure_identifier;
14974 DECL_CHAIN (tparm) = parms;
14975 parms = tparm;
14976 }
14977 DECL_ARGUMENTS (r) = parms;
14978 DECL_RESULT (r) = NULL_TREE;
14979
14980 maybe_rebuild_function_decl_type (r, args);
14981
14982 TREE_STATIC (r) = 0;
14983 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14984 DECL_EXTERNAL (r) = 1;
14985 /* If this is an instantiation of a function with internal
14986 linkage, we already know what object file linkage will be
14987 assigned to the instantiation. */
14988 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14989 DECL_DEFER_OUTPUT (r) = 0;
14990 DECL_CHAIN (r) = NULL_TREE;
14991 DECL_PENDING_INLINE_INFO (r) = 0;
14992 DECL_PENDING_INLINE_P (r) = 0;
14993 DECL_SAVED_TREE (r) = NULL_TREE;
14994 DECL_STRUCT_FUNCTION (r) = NULL;
14995 TREE_USED (r) = 0;
14996 /* We'll re-clone as appropriate in instantiate_template. */
14997 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14998
14999 /* If we aren't complaining now, return on error before we register
15000 the specialization so that we'll complain eventually. */
15001 if ((complain & tf_error) == 0
15002 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
15003 && !grok_op_properties (r, /*complain=*/false))
15004 return error_mark_node;
15005
15006 /* If we are looking at an xobj lambda, we might need to check the type of
15007 its xobj parameter. */
15008 if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
15009 {
15010 tree closure_obj = DECL_CONTEXT (r);
15011 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
15012 tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
15013
15014 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
15015 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
15016 /* If a lambda has an empty capture clause, an xobj parameter of
15017 unrelated type is not an error. */;
15018 else if (dependent_type_p (obj_param))
15019 /* If we are coming from tsubst_lambda_expr we might not have
15020 substituted into our xobj parameter yet. We can't error out until
15021 we know what the type really is so do nothing...
15022 ...but if we are instantiating the call op for real and we don't
15023 have a real type then something has gone incredibly wrong. */
15024 gcc_assert (lambda_fntype);
15025 else
15026 {
15027 /* We have a lambda with captures, and know the type of the xobj
15028 parameter, time to check it. */
15029 tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
15030 if (!same_or_base_type_p (closure_obj, obj_param_type))
15031 {
15032 /* This error does not emit when the lambda's call operator
15033 template is instantiated by taking its address, such as in
15034 the following case:
15035
15036 auto f = [x = 0](this auto&&){};
15037 int (*fp)(int&) = &decltype(f)::operator();
15038
15039 It only emits when explicitly calling the call operator with
15040 an explicit template parameter:
15041
15042 template<typename T>
15043 struct S : T {
15044 using T::operator();
15045 operator int() const {return {};}
15046 };
15047
15048 auto s = S{[x = 0](this auto&&) {}};
15049 s.operator()<int>();
15050
15051 This is due to resolve_address_of_overloaded_function being
15052 deficient at reporting candidates when overload resolution
15053 fails.
15054
15055 This diagnostic will be active in the first case if/when
15056 resolve_address_of_overloaded_function is fixed to properly
15057 emit candidates upon failure to resolve to an overload. */
15058 if (complain & tf_error)
15059 error ("a lambda with captures may not have an explicit "
15060 "object parameter of an unrelated type");
15061 return error_mark_node;
15062 }
15063 }
15064 }
15065
15066 /* Associate the constraints directly with the instantiation. We
15067 don't substitute through the constraints; that's only done when
15068 they are checked. */
15069 if (tree ci = get_constraints (t))
15070 set_constraints (r, ci);
15071
15072 if (DECL_FRIEND_CONTEXT (t))
15073 SET_DECL_FRIEND_CONTEXT (r,
15074 tsubst (DECL_FRIEND_CONTEXT (t),
15075 args, complain, in_decl));
15076
15077 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
15078 args, complain, in_decl))
15079 return error_mark_node;
15080
15081 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
15082 this in the special friend case mentioned above where
15083 GEN_TMPL is NULL. */
15084 if (gen_tmpl && !closure)
15085 {
15086 DECL_TEMPLATE_INFO (r)
15087 = build_template_info (gen_tmpl, argvec);
15088 SET_DECL_IMPLICIT_INSTANTIATION (r);
15089
15090 if (use_spec_table)
15091 {
15092 tree new_r
15093 = register_specialization (r, gen_tmpl, argvec, false, hash);
15094 if (new_r != r)
15095 /* We instantiated this while substituting into
15096 the type earlier (template/friend54.C). */
15097 return new_r;
15098 }
15099
15100 /* We're not supposed to instantiate default arguments
15101 until they are called, for a template. But, for a
15102 declaration like:
15103
15104 template <class T> void f ()
15105 { extern void g(int i = T()); }
15106
15107 we should do the substitution when the template is
15108 instantiated. We handle the member function case in
15109 instantiate_class_template since the default arguments
15110 might refer to other members of the class. */
15111 if (!member
15112 && !PRIMARY_TEMPLATE_P (gen_tmpl)
15113 && !uses_template_parms (argvec))
15114 tsubst_default_arguments (r, complain);
15115 }
15116 else if (DECL_LOCAL_DECL_P (r))
15117 {
15118 if (!cp_unevaluated_operand)
15119 register_local_specialization (r, t);
15120 }
15121 else
15122 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15123
15124 /* Copy the list of befriending classes. */
15125 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
15126 *friends;
15127 friends = &TREE_CHAIN (*friends))
15128 {
15129 *friends = copy_node (*friends);
15130 TREE_VALUE (*friends)
15131 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
15132 }
15133
15134 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
15135 {
15136 maybe_retrofit_in_chrg (r);
15137 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
15138 return error_mark_node;
15139 /* If this is an instantiation of a member template, clone it.
15140 If it isn't, that'll be handled by
15141 clone_constructors_and_destructors. */
15142 if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
15143 clone_cdtor (r, /*update_methods=*/false);
15144 }
15145 else if ((complain & tf_error) != 0
15146 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
15147 && !grok_op_properties (r, /*complain=*/true))
15148 return error_mark_node;
15149
15150 /* Possibly limit visibility based on template args. */
15151 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15152 if (DECL_VISIBILITY_SPECIFIED (t))
15153 {
15154 DECL_VISIBILITY_SPECIFIED (r) = 0;
15155 DECL_ATTRIBUTES (r)
15156 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15157 }
15158 determine_visibility (r);
15159 if (DECL_SECTION_NAME (t))
15160 set_decl_section_name (r, t);
15161 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
15162 && COMPLETE_TYPE_P (DECL_CONTEXT (r))
15163 && !processing_template_decl)
15164 defaulted_late_check (r);
15165
15166 if (flag_openmp)
15167 if (tree attr = lookup_attribute ("omp declare variant base",
15168 DECL_ATTRIBUTES (r)))
15169 omp_declare_variant_finalize (r, attr);
15170
15171 return r;
15172 }
15173
15174 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
15175
15176 static tree
15177 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
15178 tree lambda_fntype, tree lambda_tparms)
15179 {
15180 /* We can get here when processing a member function template,
15181 member class template, or template template parameter. */
15182 tree decl = DECL_TEMPLATE_RESULT (t);
15183 tree in_decl = t;
15184 tree spec;
15185 tree tmpl_args;
15186 tree full_args = NULL_TREE;
15187 tree r;
15188 hashval_t hash = 0;
15189
15190 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15191 {
15192 /* Template template parameter is treated here. */
15193 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15194 if (new_type == error_mark_node)
15195 r = error_mark_node;
15196 /* If we get a real template back, return it. This can happen in
15197 the context of most_specialized_partial_spec. */
15198 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
15199 r = new_type;
15200 else
15201 /* The new TEMPLATE_DECL was built in
15202 reduce_template_parm_level. */
15203 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
15204 return r;
15205 }
15206
15207 if (!lambda_fntype)
15208 {
15209 /* We might already have an instance of this template.
15210 The ARGS are for the surrounding class type, so the
15211 full args contain the tsubst'd args for the context,
15212 plus the innermost args from the template decl. */
15213 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
15214 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
15215 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
15216 /* Because this is a template, the arguments will still be
15217 dependent, even after substitution. If
15218 PROCESSING_TEMPLATE_DECL is not set, the dependency
15219 predicates will short-circuit. */
15220 ++processing_template_decl;
15221 full_args = tsubst_template_args (tmpl_args, args,
15222 complain, in_decl);
15223 --processing_template_decl;
15224 if (full_args == error_mark_node)
15225 return error_mark_node;
15226
15227 /* If this is a default template template argument,
15228 tsubst might not have changed anything. */
15229 if (full_args == tmpl_args)
15230 return t;
15231
15232 hash = spec_hasher::hash (t, full_args);
15233 spec = retrieve_specialization (t, full_args, hash);
15234 if (spec != NULL_TREE)
15235 {
15236 if (TYPE_P (spec))
15237 /* Type partial instantiations are stored as the type by
15238 lookup_template_class_1, not here as the template. */
15239 spec = CLASSTYPE_TI_TEMPLATE (spec);
15240 else if (TREE_CODE (spec) != TEMPLATE_DECL)
15241 spec = DECL_TI_TEMPLATE (spec);
15242 return spec;
15243 }
15244 }
15245
15246 /* Make a new template decl. It will be similar to the
15247 original, but will record the current template arguments.
15248 We also create a new function declaration, which is just
15249 like the old one, but points to this new template, rather
15250 than the old one. */
15251 r = copy_decl (t);
15252 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
15253 DECL_CHAIN (r) = NULL_TREE;
15254
15255 // Build new template info linking to the original template decl.
15256 if (!lambda_fntype)
15257 {
15258 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
15259 SET_DECL_IMPLICIT_INSTANTIATION (r);
15260 }
15261 else
15262 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15263
15264 /* The template parameters for this new template are all the
15265 template parameters for the old template, except the
15266 outermost level of parameters. */
15267 auto tparm_guard = make_temp_override (current_template_parms);
15268 DECL_TEMPLATE_PARMS (r)
15269 = current_template_parms
15270 = (lambda_tparms
15271 ? lambda_tparms
15272 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
15273 complain));
15274
15275 bool class_p = false;
15276 tree inner = decl;
15277 ++processing_template_decl;
15278 if (TREE_CODE (inner) == FUNCTION_DECL)
15279 inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
15280 /*use_spec_table=*/false);
15281 else
15282 {
15283 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
15284 {
15285 class_p = true;
15286 inner = TREE_TYPE (inner);
15287 }
15288 if (class_p)
15289 inner = tsubst_entering_scope (inner, args, complain, in_decl);
15290 else
15291 inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
15292 }
15293 --processing_template_decl;
15294 if (inner == error_mark_node)
15295 return error_mark_node;
15296
15297 if (class_p)
15298 {
15299 /* For a partial specialization, we need to keep pointing to
15300 the primary template. */
15301 if (!DECL_TEMPLATE_SPECIALIZATION (t))
15302 {
15303 CLASSTYPE_TI_TEMPLATE (inner) = r;
15304 CLASSTYPE_USE_TEMPLATE (inner) = 0;
15305 }
15306
15307 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
15308 inner = TYPE_MAIN_DECL (inner);
15309 }
15310 else if (lambda_fntype)
15311 {
15312 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
15313 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
15314 }
15315 else
15316 {
15317 DECL_TI_TEMPLATE (inner) = r;
15318 /* Set DECL_TI_ARGS to the full set of template arguments,
15319 which tsubst_function_decl / tsubst_decl didn't do due to
15320 use_spec_table=false. */
15321 DECL_TI_ARGS (inner) = full_args;
15322 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
15323 }
15324
15325 DECL_TEMPLATE_RESULT (r) = inner;
15326 TREE_TYPE (r) = TREE_TYPE (inner);
15327 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
15328
15329 if (modules_p ())
15330 {
15331 /* Propagate module information from the decl. */
15332 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
15333 if (DECL_LANG_SPECIFIC (inner))
15334 /* If this is a constrained template, the above tsubst of
15335 inner can find the unconstrained template, which may have
15336 come from an import. This is ok, because we don't
15337 register this instantiation (see below). */
15338 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
15339 || (TEMPLATE_PARMS_CONSTRAINTS
15340 (DECL_TEMPLATE_PARMS (t))));
15341 }
15342
15343 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
15344 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
15345
15346 if (PRIMARY_TEMPLATE_P (t))
15347 DECL_PRIMARY_TEMPLATE (r) = r;
15348
15349 if (!lambda_fntype && !class_p)
15350 {
15351 /* Record this non-type partial instantiation. */
15352 /* FIXME we'd like to always register the TEMPLATE_DECL, or always
15353 the DECL_TEMPLATE_RESULT, but it seems the modules code relies
15354 on this current behavior. */
15355 if (TREE_CODE (inner) == FUNCTION_DECL)
15356 register_specialization (r, t, full_args, false, hash);
15357 else
15358 register_specialization (inner, t, full_args, false, hash);
15359 }
15360
15361 return r;
15362 }
15363
15364 /* True if FN is the op() for a lambda in an uninstantiated template. */
15365
15366 bool
15367 lambda_fn_in_template_p (tree fn)
15368 {
15369 if (!fn || !LAMBDA_FUNCTION_P (fn))
15370 return false;
15371 tree closure = DECL_CONTEXT (fn);
15372 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
15373 }
15374
15375 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
15376 which the above is true. */
15377
15378 bool
15379 regenerated_lambda_fn_p (tree fn)
15380 {
15381 if (!fn || !LAMBDA_FUNCTION_P (fn))
15382 return false;
15383 tree closure = DECL_CONTEXT (fn);
15384 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
15385 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
15386 }
15387
15388 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
15389 If T is not a regenerated LAMBDA_EXPR, return T. */
15390
15391 tree
15392 most_general_lambda (tree t)
15393 {
15394 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15395 t = TI_TEMPLATE (ti);
15396 return t;
15397 }
15398
15399 /* Return the set of template arguments used to regenerate the lambda T
15400 from its most general lambda. */
15401
15402 tree
15403 lambda_regenerating_args (tree t)
15404 {
15405 if (LAMBDA_FUNCTION_P (t))
15406 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
15407 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
15408 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15409 return TI_ARGS (ti);
15410 else
15411 return NULL_TREE;
15412 }
15413
15414 /* We're instantiating a variable from template function TCTX. Return the
15415 corresponding current enclosing scope. We can match them up using
15416 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
15417 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
15418 the template definition in regenerate_decl_from_template. */
15419
15420 static tree
15421 enclosing_instantiation_of (tree tctx)
15422 {
15423 tree fn = current_function_decl;
15424
15425 /* We shouldn't ever need to do this for other artificial functions. */
15426 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
15427
15428 for (; fn; fn = decl_function_context (fn))
15429 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
15430 return fn;
15431 gcc_unreachable ();
15432 }
15433
15434 /* Substitute the ARGS into the T, which is a _DECL. Return the
15435 result of the substitution. Issue error and warning messages under
15436 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up
15437 and insert into the specializations table or if we can assume it's
15438 the caller's responsibility; this is used by instantiate_template
15439 to avoid doing some redundant work. */
15440
15441 static tree
15442 tsubst_decl (tree t, tree args, tsubst_flags_t complain,
15443 bool use_spec_table /* = true */)
15444 {
15445 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15446 location_t saved_loc;
15447 tree r = NULL_TREE;
15448 tree in_decl = t;
15449 hashval_t hash = 0;
15450
15451 if (t == error_mark_node)
15452 return error_mark_node;
15453
15454 /* Set the filename and linenumber to improve error-reporting. */
15455 saved_loc = input_location;
15456 input_location = DECL_SOURCE_LOCATION (t);
15457
15458 switch (TREE_CODE (t))
15459 {
15460 case TEMPLATE_DECL:
15461 r = tsubst_template_decl (t, args, complain,
15462 /*lambda_fntype=*/NULL_TREE,
15463 /*lambda_tparms=*/NULL_TREE);
15464 break;
15465
15466 case FUNCTION_DECL:
15467 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
15468 use_spec_table);
15469 break;
15470
15471 case PARM_DECL:
15472 {
15473 tree type = NULL_TREE;
15474 int i, len = 1;
15475 tree expanded_types = NULL_TREE;
15476 tree prev_r = NULL_TREE;
15477 tree first_r = NULL_TREE;
15478
15479 if (DECL_PACK_P (t))
15480 {
15481 /* If there is a local specialization that isn't a
15482 parameter pack, it means that we're doing a "simple"
15483 substitution from inside tsubst_pack_expansion. Just
15484 return the local specialization (which will be a single
15485 parm). */
15486 tree spec = retrieve_local_specialization (t);
15487 if (spec
15488 && TREE_CODE (spec) == PARM_DECL
15489 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
15490 RETURN (spec);
15491
15492 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15493 the parameters in this function parameter pack. */
15494 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15495 complain, in_decl);
15496 if (TREE_CODE (expanded_types) == TREE_VEC)
15497 {
15498 len = TREE_VEC_LENGTH (expanded_types);
15499
15500 /* Zero-length parameter packs are boring. Just substitute
15501 into the chain. */
15502 if (len == 0 && !cp_unevaluated_operand)
15503 RETURN (tsubst (TREE_CHAIN (t), args, complain,
15504 TREE_CHAIN (t)));
15505 }
15506 else
15507 {
15508 /* All we did was update the type. Make a note of that. */
15509 type = expanded_types;
15510 expanded_types = NULL_TREE;
15511 }
15512 }
15513
15514 /* Loop through all of the parameters we'll build. When T is
15515 a function parameter pack, LEN is the number of expanded
15516 types in EXPANDED_TYPES; otherwise, LEN is 1. */
15517 r = NULL_TREE;
15518 for (i = 0; i < len; ++i)
15519 {
15520 prev_r = r;
15521 r = copy_node (t);
15522 if (DECL_TEMPLATE_PARM_P (t))
15523 SET_DECL_TEMPLATE_PARM_P (r);
15524
15525 if (expanded_types)
15526 /* We're on the Ith parameter of the function parameter
15527 pack. */
15528 {
15529 /* Get the Ith type. */
15530 type = TREE_VEC_ELT (expanded_types, i);
15531
15532 /* Rename the parameter to include the index. */
15533 DECL_NAME (r)
15534 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15535 }
15536 else if (!type)
15537 /* We're dealing with a normal parameter. */
15538 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15539
15540 if (type == error_mark_node && !(complain & tf_error))
15541 RETURN (error_mark_node);
15542
15543 type = type_decays_to (type);
15544 TREE_TYPE (r) = type;
15545 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15546
15547 if (DECL_INITIAL (r))
15548 {
15549 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
15550 DECL_INITIAL (r) = TREE_TYPE (r);
15551 else
15552 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
15553 complain, in_decl);
15554 }
15555
15556 DECL_CONTEXT (r) = NULL_TREE;
15557
15558 if (!DECL_TEMPLATE_PARM_P (r))
15559 DECL_ARG_TYPE (r) = type_passed_as (type);
15560
15561 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
15562 args, complain, in_decl))
15563 return error_mark_node;
15564
15565 /* Keep track of the first new parameter we
15566 generate. That's what will be returned to the
15567 caller. */
15568 if (!first_r)
15569 first_r = r;
15570
15571 /* Build a proper chain of parameters when substituting
15572 into a function parameter pack. */
15573 if (prev_r)
15574 DECL_CHAIN (prev_r) = r;
15575 }
15576
15577 /* If cp_unevaluated_operand is set, we're just looking for a
15578 single dummy parameter, so don't keep going. */
15579 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
15580 {
15581 tree chain = tsubst (DECL_CHAIN (t), args,
15582 complain, DECL_CHAIN (t));
15583 if (chain == error_mark_node)
15584 RETURN (error_mark_node);
15585 DECL_CHAIN (r) = chain;
15586 }
15587
15588 /* FIRST_R contains the start of the chain we've built. */
15589 r = first_r;
15590 }
15591 break;
15592
15593 case FIELD_DECL:
15594 {
15595 tree type = NULL_TREE;
15596 tree vec = NULL_TREE;
15597 tree expanded_types = NULL_TREE;
15598 int len = 1;
15599
15600 if (PACK_EXPANSION_P (TREE_TYPE (t)))
15601 {
15602 /* This field is a lambda capture pack. Return a TREE_VEC of
15603 the expanded fields to instantiate_class_template_1. */
15604 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15605 complain, in_decl);
15606 if (TREE_CODE (expanded_types) == TREE_VEC)
15607 {
15608 len = TREE_VEC_LENGTH (expanded_types);
15609 vec = make_tree_vec (len);
15610 }
15611 else
15612 {
15613 /* All we did was update the type. Make a note of that. */
15614 type = expanded_types;
15615 expanded_types = NULL_TREE;
15616 }
15617 }
15618
15619 for (int i = 0; i < len; ++i)
15620 {
15621 r = copy_decl (t);
15622 if (expanded_types)
15623 {
15624 type = TREE_VEC_ELT (expanded_types, i);
15625 DECL_NAME (r)
15626 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15627 }
15628 else if (!type)
15629 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15630
15631 if (type == error_mark_node)
15632 RETURN (error_mark_node);
15633 TREE_TYPE (r) = type;
15634 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15635
15636 if (DECL_C_BIT_FIELD (r))
15637 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
15638 number of bits. */
15639 DECL_BIT_FIELD_REPRESENTATIVE (r)
15640 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
15641 complain, in_decl);
15642 if (DECL_INITIAL (t))
15643 {
15644 /* Set up DECL_TEMPLATE_INFO so that we can get at the
15645 NSDMI in perform_member_init. Still set DECL_INITIAL
15646 so that we know there is one. */
15647 DECL_INITIAL (r) = void_node;
15648 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
15649 retrofit_lang_decl (r);
15650 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
15651 }
15652 /* We don't have to set DECL_CONTEXT here; it is set by
15653 finish_member_declaration. */
15654 DECL_CHAIN (r) = NULL_TREE;
15655
15656 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
15657 args, complain, in_decl))
15658 return error_mark_node;
15659
15660 if (vec)
15661 TREE_VEC_ELT (vec, i) = r;
15662 }
15663
15664 if (vec)
15665 r = vec;
15666 }
15667 break;
15668
15669 case USING_DECL:
15670 /* We reach here only for member using decls. We also need to check
15671 uses_template_parms because DECL_DEPENDENT_P is not set for a
15672 using-declaration that designates a member of the current
15673 instantiation (c++/53549). */
15674 if (DECL_DEPENDENT_P (t)
15675 || uses_template_parms (USING_DECL_SCOPE (t)))
15676 {
15677 /* True iff this using-decl was written as a pack expansion
15678 (and a pack appeared in its scope or name). If a pack
15679 appeared in both, we expand the packs separately and
15680 manually merge them. */
15681 bool variadic_p = false;
15682
15683 tree scope = USING_DECL_SCOPE (t);
15684 if (PACK_EXPANSION_P (scope))
15685 {
15686 scope = tsubst_pack_expansion (scope, args,
15687 complain | tf_qualifying_scope,
15688 in_decl);
15689 variadic_p = true;
15690 }
15691 else
15692 scope = tsubst_scope (scope, args, complain, in_decl);
15693
15694 tree name = DECL_NAME (t);
15695 if (IDENTIFIER_CONV_OP_P (name)
15696 && PACK_EXPANSION_P (TREE_TYPE (name)))
15697 {
15698 name = tsubst_pack_expansion (TREE_TYPE (name), args,
15699 complain, in_decl);
15700 if (name == error_mark_node)
15701 {
15702 r = error_mark_node;
15703 break;
15704 }
15705 for (tree& elt : tree_vec_range (name))
15706 elt = make_conv_op_name (elt);
15707 variadic_p = true;
15708 }
15709 else
15710 name = tsubst_name (name, args, complain, in_decl);
15711
15712 int len;
15713 if (!variadic_p)
15714 len = 1;
15715 else if (TREE_CODE (scope) == TREE_VEC
15716 && TREE_CODE (name) == TREE_VEC)
15717 {
15718 if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
15719 {
15720 error ("mismatched argument pack lengths (%d vs %d)",
15721 TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
15722 r = error_mark_node;
15723 break;
15724 }
15725 len = TREE_VEC_LENGTH (scope);
15726 }
15727 else if (TREE_CODE (scope) == TREE_VEC)
15728 len = TREE_VEC_LENGTH (scope);
15729 else /* TREE_CODE (name) == TREE_VEC */
15730 len = TREE_VEC_LENGTH (name);
15731
15732 r = make_tree_vec (len);
15733 for (int i = 0; i < len; ++i)
15734 {
15735 tree escope = (TREE_CODE (scope) == TREE_VEC
15736 ? TREE_VEC_ELT (scope, i)
15737 : scope);
15738 tree ename = (TREE_CODE (name) == TREE_VEC
15739 ? TREE_VEC_ELT (name, i)
15740 : name);
15741 tree elt = do_class_using_decl (escope, ename);
15742 if (!elt)
15743 {
15744 r = error_mark_node;
15745 break;
15746 }
15747 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
15748 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
15749 TREE_VEC_ELT (r, i) = elt;
15750 }
15751
15752 if (!variadic_p && r != error_mark_node)
15753 r = TREE_VEC_ELT (r, 0);
15754 }
15755 else
15756 {
15757 r = copy_node (t);
15758 DECL_CHAIN (r) = NULL_TREE;
15759 }
15760 break;
15761
15762 case TYPE_DECL:
15763 case VAR_DECL:
15764 {
15765 tree argvec = NULL_TREE;
15766 tree gen_tmpl = NULL_TREE;
15767 tree tmpl = NULL_TREE;
15768 tree type = NULL_TREE;
15769
15770 if (TREE_TYPE (t) == error_mark_node)
15771 RETURN (error_mark_node);
15772
15773 if (TREE_CODE (t) == TYPE_DECL
15774 && (TREE_CODE (TREE_TYPE (t)) == TU_LOCAL_ENTITY
15775 || t == TYPE_MAIN_DECL (TREE_TYPE (t))))
15776 {
15777 /* If this is the canonical decl, we don't have to
15778 mess with instantiations, and often we can't (for
15779 typename, template type parms and such). Note that
15780 TYPE_NAME is not correct for the above test if
15781 we've copied the type for a typedef. */
15782 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15783 if (type == error_mark_node)
15784 RETURN (error_mark_node);
15785 r = TYPE_NAME (type);
15786 break;
15787 }
15788
15789 /* Check to see if we already have the specialization we
15790 need. */
15791 tree spec = NULL_TREE;
15792 bool local_p = false;
15793 tree ctx = DECL_CONTEXT (t);
15794 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
15795 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
15796 {
15797 local_p = false;
15798 if (DECL_CLASS_SCOPE_P (t))
15799 {
15800 ctx = tsubst_entering_scope (ctx, args, complain, in_decl);
15801 if (DECL_SELF_REFERENCE_P (t))
15802 /* The context and type of an injected-class-name are
15803 the same, so we don't need to substitute both. */
15804 type = ctx;
15805 /* If CTX is unchanged, then T is in fact the
15806 specialization we want. That situation occurs when
15807 referencing a static data member within in its own
15808 class. We can use pointer equality, rather than
15809 same_type_p, because DECL_CONTEXT is always
15810 canonical... */
15811 if (ctx == DECL_CONTEXT (t)
15812 /* ... unless T is a member template; in which
15813 case our caller can be willing to create a
15814 specialization of that template represented
15815 by T. */
15816 && !(DECL_TI_TEMPLATE (t)
15817 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
15818 spec = t;
15819 }
15820
15821 if (!spec)
15822 {
15823 tmpl = DECL_TI_TEMPLATE (t);
15824 if (use_spec_table)
15825 {
15826 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
15827 if (argvec == error_mark_node)
15828 RETURN (error_mark_node);
15829 gen_tmpl = most_general_template (tmpl);
15830 hash = spec_hasher::hash (gen_tmpl, argvec);
15831 spec = retrieve_specialization (gen_tmpl, argvec, hash);
15832 }
15833 else
15834 argvec = args;
15835 }
15836 }
15837 else
15838 {
15839 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
15840 /* Subsequent calls to pushdecl will fill this in. */
15841 ctx = NULL_TREE;
15842 /* A local variable. */
15843 local_p = true;
15844 /* Unless this is a reference to a static variable from an
15845 enclosing function, in which case we need to fill it in now. */
15846 if (TREE_STATIC (t))
15847 {
15848 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
15849 if (fn != current_function_decl)
15850 ctx = fn;
15851 }
15852 spec = retrieve_local_specialization (t);
15853 }
15854 /* If we already have the specialization we need, there is
15855 nothing more to do. */
15856 if (spec)
15857 {
15858 r = spec;
15859 break;
15860 }
15861
15862 /* Create a new node for the specialization we need. */
15863 if (type == NULL_TREE)
15864 {
15865 if (is_typedef_decl (t))
15866 type = DECL_ORIGINAL_TYPE (t);
15867 else
15868 type = TREE_TYPE (t);
15869 if (VAR_P (t)
15870 && VAR_HAD_UNKNOWN_BOUND (t)
15871 && type != error_mark_node)
15872 type = strip_array_domain (type);
15873 tsubst_flags_t tcomplain = complain;
15874 if (VAR_P (t))
15875 tcomplain |= tf_tst_ok;
15876 type = tsubst (type, args, tcomplain, in_decl);
15877 /* Substituting the type might have recursively instantiated this
15878 same alias (c++/86171). */
15879 if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
15880 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
15881 {
15882 r = spec;
15883 break;
15884 }
15885 }
15886 if (type == error_mark_node && !(complain & tf_error))
15887 RETURN (error_mark_node);
15888 r = copy_decl (t);
15889 if (VAR_P (r))
15890 {
15891 DECL_INITIALIZED_P (r) = 0;
15892 DECL_TEMPLATE_INSTANTIATED (r) = 0;
15893 if (TREE_CODE (type) == FUNCTION_TYPE)
15894 {
15895 /* It may seem that this case cannot occur, since:
15896
15897 typedef void f();
15898 void g() { f x; }
15899
15900 declares a function, not a variable. However:
15901
15902 typedef void f();
15903 template <typename T> void g() { T t; }
15904 template void g<f>();
15905
15906 is an attempt to declare a variable with function
15907 type. */
15908 error ("variable %qD has function type",
15909 /* R is not yet sufficiently initialized, so we
15910 just use its name. */
15911 DECL_NAME (r));
15912 RETURN (error_mark_node);
15913 }
15914 type = complete_type (type);
15915 /* Wait until cp_finish_decl to set this again, to handle
15916 circular dependency (template/instantiate6.C). */
15917 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
15918 type = check_var_type (DECL_NAME (r), type,
15919 DECL_SOURCE_LOCATION (r));
15920 if (DECL_HAS_VALUE_EXPR_P (t))
15921 {
15922 tree ve = DECL_VALUE_EXPR (t);
15923 /* If the DECL_VALUE_EXPR is converted to the declared type,
15924 preserve the identity so that gimplify_type_sizes works. */
15925 bool nop = (TREE_CODE (ve) == NOP_EXPR);
15926 if (nop)
15927 ve = TREE_OPERAND (ve, 0);
15928 ve = tsubst_expr (ve, args, complain, in_decl);
15929 if (REFERENCE_REF_P (ve))
15930 {
15931 gcc_assert (TYPE_REF_P (type));
15932 ve = TREE_OPERAND (ve, 0);
15933 }
15934 if (nop)
15935 ve = build_nop (type, ve);
15936 else if (DECL_LANG_SPECIFIC (t)
15937 && DECL_OMP_PRIVATIZED_MEMBER (t)
15938 && TREE_CODE (ve) == COMPONENT_REF
15939 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
15940 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
15941 type = TREE_TYPE (ve);
15942 else
15943 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
15944 == TYPE_MAIN_VARIANT (type));
15945 SET_DECL_VALUE_EXPR (r, ve);
15946 }
15947 if (CP_DECL_THREAD_LOCAL_P (r)
15948 && !processing_template_decl)
15949 set_decl_tls_model (r, decl_default_tls_model (r));
15950 }
15951 else if (DECL_SELF_REFERENCE_P (t))
15952 SET_DECL_SELF_REFERENCE_P (r);
15953 TREE_TYPE (r) = type;
15954 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15955 DECL_CONTEXT (r) = ctx;
15956 /* Clear out the mangled name and RTL for the instantiation. */
15957 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
15958 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
15959 SET_DECL_RTL (r, NULL);
15960 set_instantiating_module (r);
15961
15962 /* The initializer must not be expanded until it is required;
15963 see [temp.inst]. */
15964 DECL_INITIAL (r) = NULL_TREE;
15965 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
15966 if (VAR_P (r))
15967 {
15968 if (DECL_LANG_SPECIFIC (r))
15969 SET_DECL_DEPENDENT_INIT_P (r, false);
15970
15971 SET_DECL_MODE (r, VOIDmode);
15972
15973 /* Possibly limit visibility based on template args. */
15974 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15975 if (DECL_VISIBILITY_SPECIFIED (t))
15976 {
15977 DECL_VISIBILITY_SPECIFIED (r) = 0;
15978 DECL_ATTRIBUTES (r)
15979 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15980 }
15981 determine_visibility (r);
15982 if ((!local_p || TREE_STATIC (t)) && DECL_SECTION_NAME (t))
15983 set_decl_section_name (r, t);
15984 }
15985
15986 if (!local_p)
15987 {
15988 /* A static data member declaration is always marked
15989 external when it is declared in-class, even if an
15990 initializer is present. We mimic the non-template
15991 processing here. */
15992 DECL_EXTERNAL (r) = 1;
15993 if (DECL_NAMESPACE_SCOPE_P (t))
15994 DECL_NOT_REALLY_EXTERN (r) = 1;
15995
15996 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
15997 SET_DECL_IMPLICIT_INSTANTIATION (r);
15998 if (use_spec_table)
15999 register_specialization (r, gen_tmpl, argvec, false, hash);
16000 }
16001 else
16002 {
16003 if (DECL_LANG_SPECIFIC (r))
16004 DECL_TEMPLATE_INFO (r) = NULL_TREE;
16005 if (!cp_unevaluated_operand)
16006 register_local_specialization (r, t);
16007 }
16008
16009 DECL_CHAIN (r) = NULL_TREE;
16010
16011 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
16012 /*flags=*/0,
16013 args, complain, in_decl))
16014 return error_mark_node;
16015
16016 /* Preserve a typedef that names a type. */
16017 if (is_typedef_decl (r) && type != error_mark_node)
16018 {
16019 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
16020 set_underlying_type (r);
16021
16022 /* common_handle_aligned_attribute doesn't apply the alignment
16023 to DECL_ORIGINAL_TYPE. */
16024 if (TYPE_USER_ALIGN (TREE_TYPE (t)))
16025 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
16026 TYPE_ALIGN (TREE_TYPE (t)));
16027
16028 /* Preserve structural-ness of a partially instantiated typedef. */
16029 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))
16030 && dependent_type_p (TREE_TYPE (r)))
16031 SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (r));
16032 }
16033
16034 layout_decl (r, 0);
16035 }
16036 break;
16037
16038 default:
16039 gcc_unreachable ();
16040 }
16041 #undef RETURN
16042
16043 out:
16044 /* Restore the file and line information. */
16045 input_location = saved_loc;
16046
16047 return r;
16048 }
16049
16050 /* Substitute into the complete parameter type list PARMS. */
16051
16052 tree
16053 tsubst_function_parms (tree parms,
16054 tree args,
16055 tsubst_flags_t complain,
16056 tree in_decl)
16057 {
16058 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
16059 }
16060
16061 /* Substitute into the ARG_TYPES of a function type.
16062 If END is a TREE_CHAIN, leave it and any following types
16063 un-substituted. */
16064
16065 static tree
16066 tsubst_arg_types (tree arg_types,
16067 tree args,
16068 tree end,
16069 tsubst_flags_t complain,
16070 tree in_decl)
16071 {
16072 tree type = NULL_TREE;
16073 int len = 1;
16074 tree expanded_args = NULL_TREE;
16075
16076 if (!arg_types || arg_types == void_list_node || arg_types == end)
16077 return arg_types;
16078
16079 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
16080 {
16081 /* For a pack expansion, perform substitution on the
16082 entire expression. Later on, we'll handle the arguments
16083 one-by-one. */
16084 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
16085 args, complain, in_decl);
16086
16087 if (TREE_CODE (expanded_args) == TREE_VEC)
16088 /* So that we'll spin through the parameters, one by one. */
16089 len = TREE_VEC_LENGTH (expanded_args);
16090 else
16091 {
16092 /* We only partially substituted into the parameter
16093 pack. Our type is TYPE_PACK_EXPANSION. */
16094 type = expanded_args;
16095 expanded_args = NULL_TREE;
16096 }
16097 }
16098 else
16099 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
16100
16101 /* Check if a substituted type is erroneous before substituting into
16102 the rest of the chain. */
16103 for (int i = 0; i < len; i++)
16104 {
16105 if (expanded_args)
16106 type = TREE_VEC_ELT (expanded_args, i);
16107
16108 if (type == error_mark_node)
16109 return error_mark_node;
16110 if (VOID_TYPE_P (type))
16111 {
16112 if (complain & tf_error)
16113 {
16114 error ("invalid parameter type %qT", type);
16115 if (in_decl)
16116 error ("in declaration %q+D", in_decl);
16117 }
16118 return error_mark_node;
16119 }
16120 }
16121
16122 /* We do not substitute into default arguments here. The standard
16123 mandates that they be instantiated only when needed, which is
16124 done in build_over_call. */
16125 tree default_arg = TREE_PURPOSE (arg_types);
16126
16127 /* Except that we do substitute default arguments under tsubst_lambda_expr,
16128 since the new op() won't have any associated template arguments for us
16129 to refer to later. */
16130 if (lambda_fn_in_template_p (in_decl)
16131 || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
16132 && DECL_LOCAL_DECL_P (in_decl)))
16133 default_arg = tsubst_expr (default_arg, args, complain, in_decl);
16134
16135 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
16136 args, end, complain, in_decl);
16137 if (remaining_arg_types == error_mark_node)
16138 return error_mark_node;
16139
16140 for (int i = len-1; i >= 0; i--)
16141 {
16142 if (expanded_args)
16143 type = TREE_VEC_ELT (expanded_args, i);
16144
16145 /* Do array-to-pointer, function-to-pointer conversion, and ignore
16146 top-level qualifiers as required. */
16147 type = cv_unqualified (type_decays_to (type));
16148
16149 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
16150 {
16151 /* We've instantiated a template before its default arguments
16152 have been parsed. This can happen for a nested template
16153 class, and is not an error unless we require the default
16154 argument in a call of this function. */
16155 remaining_arg_types
16156 = tree_cons (default_arg, type, remaining_arg_types);
16157 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
16158 remaining_arg_types);
16159 }
16160 else
16161 remaining_arg_types
16162 = hash_tree_cons (default_arg, type, remaining_arg_types);
16163 }
16164
16165 return remaining_arg_types;
16166 }
16167
16168 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
16169 *not* handle the exception-specification for FNTYPE, because the
16170 initial substitution of explicitly provided template parameters
16171 during argument deduction forbids substitution into the
16172 exception-specification:
16173
16174 [temp.deduct]
16175
16176 All references in the function type of the function template to the
16177 corresponding template parameters are replaced by the specified tem-
16178 plate argument values. If a substitution in a template parameter or
16179 in the function type of the function template results in an invalid
16180 type, type deduction fails. [Note: The equivalent substitution in
16181 exception specifications is done only when the function is instanti-
16182 ated, at which point a program is ill-formed if the substitution
16183 results in an invalid type.] */
16184
16185 static tree
16186 tsubst_function_type (tree t,
16187 tree args,
16188 tsubst_flags_t complain,
16189 tree in_decl)
16190 {
16191 tree return_type;
16192 tree arg_types = NULL_TREE;
16193
16194 /* The TYPE_CONTEXT is not used for function/method types. */
16195 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
16196
16197 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
16198 failure. */
16199 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
16200
16201 if (late_return_type_p)
16202 {
16203 /* Substitute the argument types. */
16204 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
16205 complain, in_decl);
16206 if (arg_types == error_mark_node)
16207 return error_mark_node;
16208
16209 tree save_ccp = current_class_ptr;
16210 tree save_ccr = current_class_ref;
16211 tree this_type = (TREE_CODE (t) == METHOD_TYPE
16212 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
16213 bool do_inject = this_type && CLASS_TYPE_P (this_type);
16214 if (do_inject)
16215 {
16216 /* DR 1207: 'this' is in scope in the trailing return type. */
16217 inject_this_parameter (this_type, cp_type_quals (this_type));
16218 }
16219
16220 /* Substitute the return type. */
16221 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16222
16223 if (do_inject)
16224 {
16225 current_class_ptr = save_ccp;
16226 current_class_ref = save_ccr;
16227 }
16228 }
16229 else
16230 /* Substitute the return type. */
16231 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16232
16233 if (return_type == error_mark_node)
16234 return error_mark_node;
16235 /* DR 486 clarifies that creation of a function type with an
16236 invalid return type is a deduction failure. */
16237 if (TREE_CODE (return_type) == ARRAY_TYPE
16238 || TREE_CODE (return_type) == FUNCTION_TYPE)
16239 {
16240 if (complain & tf_error)
16241 {
16242 if (TREE_CODE (return_type) == ARRAY_TYPE)
16243 error ("function returning an array");
16244 else
16245 error ("function returning a function");
16246 }
16247 return error_mark_node;
16248 }
16249
16250 if (!late_return_type_p)
16251 {
16252 /* Substitute the argument types. */
16253 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
16254 complain, in_decl);
16255 if (arg_types == error_mark_node)
16256 return error_mark_node;
16257 }
16258
16259 /* Construct a new type node and return it. */
16260 return rebuild_function_or_method_type (t, args, return_type, arg_types,
16261 /*raises=*/NULL_TREE, complain);
16262 }
16263
16264 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
16265 ARGS into that specification, and return the substituted
16266 specification. If there is no specification, return NULL_TREE. */
16267
16268 static tree
16269 tsubst_exception_specification (tree fntype,
16270 tree args,
16271 tsubst_flags_t complain,
16272 tree in_decl,
16273 bool defer_ok)
16274 {
16275 tree specs;
16276 tree new_specs;
16277
16278 specs = TYPE_RAISES_EXCEPTIONS (fntype);
16279 new_specs = NULL_TREE;
16280 if (specs && TREE_PURPOSE (specs))
16281 {
16282 /* A noexcept-specifier. */
16283 tree expr = TREE_PURPOSE (specs);
16284 if (TREE_CODE (expr) == INTEGER_CST)
16285 new_specs = expr;
16286 else if (defer_ok)
16287 {
16288 /* Defer instantiation of noexcept-specifiers to avoid
16289 excessive instantiations (c++/49107). */
16290 new_specs = make_node (DEFERRED_NOEXCEPT);
16291 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
16292 {
16293 /* We already partially instantiated this member template,
16294 so combine the new args with the old. */
16295 DEFERRED_NOEXCEPT_PATTERN (new_specs)
16296 = DEFERRED_NOEXCEPT_PATTERN (expr);
16297 DEFERRED_NOEXCEPT_ARGS (new_specs)
16298 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
16299 }
16300 else
16301 {
16302 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
16303 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
16304 }
16305 }
16306 else
16307 {
16308 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
16309 {
16310 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
16311 args);
16312 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
16313 }
16314 new_specs = tsubst_expr (expr, args, complain, in_decl);
16315 }
16316 new_specs = build_noexcept_spec (new_specs, complain);
16317 /* We've instantiated a template before a noexcept-specifier
16318 contained therein has been parsed. This can happen for
16319 a nested template class:
16320
16321 struct S {
16322 template<typename> struct B { B() noexcept(...); };
16323 struct A : B<int> { ... use B() ... };
16324 };
16325
16326 where completing B<int> will trigger instantiating the
16327 noexcept, even though we only parse it at the end of S. */
16328 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
16329 {
16330 gcc_checking_assert (defer_ok);
16331 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
16332 }
16333 }
16334 else if (specs)
16335 {
16336 if (! TREE_VALUE (specs))
16337 new_specs = specs;
16338 else
16339 while (specs)
16340 {
16341 tree spec;
16342 int i, len = 1;
16343 tree expanded_specs = NULL_TREE;
16344
16345 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
16346 {
16347 /* Expand the pack expansion type. */
16348 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
16349 args, complain,
16350 in_decl);
16351
16352 if (expanded_specs == error_mark_node)
16353 return error_mark_node;
16354 else if (TREE_CODE (expanded_specs) == TREE_VEC)
16355 len = TREE_VEC_LENGTH (expanded_specs);
16356 else
16357 {
16358 /* We're substituting into a member template, so
16359 we got a TYPE_PACK_EXPANSION back. Add that
16360 expansion and move on. */
16361 gcc_assert (TREE_CODE (expanded_specs)
16362 == TYPE_PACK_EXPANSION);
16363 new_specs = add_exception_specifier (new_specs,
16364 expanded_specs,
16365 complain);
16366 specs = TREE_CHAIN (specs);
16367 continue;
16368 }
16369 }
16370
16371 for (i = 0; i < len; ++i)
16372 {
16373 if (expanded_specs)
16374 spec = TREE_VEC_ELT (expanded_specs, i);
16375 else
16376 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
16377 if (spec == error_mark_node)
16378 return spec;
16379 new_specs = add_exception_specifier (new_specs, spec,
16380 complain);
16381 }
16382
16383 specs = TREE_CHAIN (specs);
16384 }
16385 }
16386 return new_specs;
16387 }
16388
16389 /* Substitute through a TREE_LIST of types or expressions, handling pack
16390 expansions. */
16391
16392 tree
16393 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16394 {
16395 if (t == void_list_node)
16396 return t;
16397
16398 tree purpose = TREE_PURPOSE (t);
16399 tree purposevec = NULL_TREE;
16400 if (!purpose)
16401 ;
16402 else if (PACK_EXPANSION_P (purpose))
16403 {
16404 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
16405 if (TREE_CODE (purpose) == TREE_VEC)
16406 purposevec = purpose;
16407 }
16408 else if (TYPE_P (purpose))
16409 purpose = tsubst (purpose, args, complain, in_decl);
16410 else
16411 purpose = tsubst_expr (purpose, args, complain, in_decl);
16412 if (purpose == error_mark_node || purposevec == error_mark_node)
16413 return error_mark_node;
16414
16415 tree value = TREE_VALUE (t);
16416 tree valuevec = NULL_TREE;
16417 if (!value)
16418 ;
16419 else if (PACK_EXPANSION_P (value))
16420 {
16421 value = tsubst_pack_expansion (value, args, complain, in_decl);
16422 if (TREE_CODE (value) == TREE_VEC)
16423 valuevec = value;
16424 }
16425 else if (TYPE_P (value))
16426 value = tsubst (value, args, complain, in_decl);
16427 else
16428 value = tsubst_expr (value, args, complain, in_decl);
16429 if (value == error_mark_node || valuevec == error_mark_node)
16430 return error_mark_node;
16431
16432 tree chain = TREE_CHAIN (t);
16433 if (!chain)
16434 ;
16435 else if (TREE_CODE (chain) == TREE_LIST)
16436 chain = tsubst_tree_list (chain, args, complain, in_decl);
16437 else if (TYPE_P (chain))
16438 chain = tsubst (chain, args, complain, in_decl);
16439 else
16440 chain = tsubst_expr (chain, args, complain, in_decl);
16441 if (chain == error_mark_node)
16442 return error_mark_node;
16443
16444 if (purpose == TREE_PURPOSE (t)
16445 && value == TREE_VALUE (t)
16446 && chain == TREE_CHAIN (t))
16447 return t;
16448
16449 int len;
16450 /* Determine the number of arguments. */
16451 if (purposevec)
16452 {
16453 len = TREE_VEC_LENGTH (purposevec);
16454 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16455 }
16456 else if (valuevec)
16457 len = TREE_VEC_LENGTH (valuevec);
16458 else
16459 len = 1;
16460
16461 for (int i = len; i-- > 0; )
16462 {
16463 if (purposevec)
16464 purpose = TREE_VEC_ELT (purposevec, i);
16465 if (valuevec)
16466 value = TREE_VEC_ELT (valuevec, i);
16467
16468 if (value && TYPE_P (value))
16469 chain = hash_tree_cons (purpose, value, chain);
16470 else
16471 chain = tree_cons (purpose, value, chain);
16472 }
16473
16474 return chain;
16475 }
16476
16477 /* Take the tree structure T and replace template parameters used
16478 therein with the argument vector ARGS. IN_DECL is an associated
16479 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
16480 Issue error and warning messages under control of COMPLAIN. Note
16481 that we must be relatively non-tolerant of extensions here, in
16482 order to preserve conformance; if we allow substitutions that
16483 should not be allowed, we may allow argument deductions that should
16484 not succeed, and therefore report ambiguous overload situations
16485 where there are none. In theory, we could allow the substitution,
16486 but indicate that it should have failed, and allow our caller to
16487 make sure that the right thing happens, but we don't try to do this
16488 yet.
16489
16490 This function is used for dealing with types, decls and the like;
16491 for expressions, use tsubst_expr or tsubst_copy. */
16492
16493 tree
16494 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16495 {
16496 enum tree_code code;
16497 tree type, r = NULL_TREE;
16498
16499 if (t == NULL_TREE || t == error_mark_node
16500 || t == integer_type_node
16501 || t == void_type_node
16502 || t == char_type_node
16503 || t == unknown_type_node
16504 || TREE_CODE (t) == NAMESPACE_DECL
16505 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
16506 return t;
16507
16508 /* Any instantiation of a template containing a TU-local entity is an
16509 exposure, so always issue a hard error irrespective of complain. */
16510 if (TREE_CODE (t) == TU_LOCAL_ENTITY)
16511 {
16512 complain_about_tu_local_entity (t);
16513 return error_mark_node;
16514 }
16515
16516 tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
16517 complain &= ~tf_tst_ok;
16518
16519 tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
16520 complain &= ~tf_qualifying_scope;
16521
16522 if (DECL_P (t))
16523 return tsubst_decl (t, args, complain);
16524
16525 if (args == NULL_TREE)
16526 return t;
16527
16528 code = TREE_CODE (t);
16529
16530 gcc_assert (code != IDENTIFIER_NODE);
16531 type = TREE_TYPE (t);
16532
16533 gcc_assert (type != unknown_type_node);
16534
16535 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
16536 return d;
16537
16538 /* Reuse typedefs. We need to do this to handle dependent attributes,
16539 such as attribute aligned. */
16540 if (TYPE_P (t)
16541 && typedef_variant_p (t))
16542 {
16543 tree decl = TYPE_NAME (t);
16544
16545 if (alias_template_specialization_p (t, nt_opaque))
16546 {
16547 /* DECL represents an alias template and we want to
16548 instantiate it. */
16549 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16550 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16551 r = instantiate_alias_template (tmpl, gen_args, complain);
16552 }
16553 else if (DECL_CLASS_SCOPE_P (decl)
16554 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
16555 && uses_template_parms (DECL_CONTEXT (decl)))
16556 {
16557 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16558 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16559 r = retrieve_specialization (tmpl, gen_args, 0);
16560 }
16561 else if (DECL_FUNCTION_SCOPE_P (decl)
16562 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
16563 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
16564 r = retrieve_local_specialization (decl);
16565 else
16566 /* The typedef is from a non-template context. */
16567 return t;
16568
16569 if (r)
16570 {
16571 r = TREE_TYPE (r);
16572 r = cp_build_qualified_type
16573 (r, cp_type_quals (t) | cp_type_quals (r),
16574 complain | tf_ignore_bad_quals);
16575 return r;
16576 }
16577 else
16578 {
16579 /* We don't have an instantiation yet, so drop the typedef. */
16580 int quals = cp_type_quals (t);
16581 t = DECL_ORIGINAL_TYPE (decl);
16582 t = cp_build_qualified_type (t, quals,
16583 complain | tf_ignore_bad_quals);
16584 }
16585 }
16586
16587 bool fndecl_type = (complain & tf_fndecl_type);
16588 complain &= ~tf_fndecl_type;
16589
16590 if (type
16591 && code != TYPENAME_TYPE
16592 && code != TEMPLATE_TYPE_PARM
16593 && code != TEMPLATE_PARM_INDEX
16594 && code != IDENTIFIER_NODE
16595 && code != FUNCTION_TYPE
16596 && code != METHOD_TYPE
16597 && code != PACK_INDEX_TYPE)
16598 type = tsubst (type, args, complain, in_decl);
16599 if (type == error_mark_node)
16600 return error_mark_node;
16601
16602 switch (code)
16603 {
16604 case RECORD_TYPE:
16605 if (TYPE_PTRMEMFUNC_P (t))
16606 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
16607 /* Fall through. */
16608 case UNION_TYPE:
16609 case ENUMERAL_TYPE:
16610 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
16611 {
16612 /* Figure out what arguments are appropriate for the
16613 type we are trying to find. For example, given:
16614
16615 template <class T> struct S;
16616 template <class T, class U> void f(T, U) { S<U> su; }
16617
16618 and supposing that we are instantiating f<int, double>,
16619 then our ARGS will be {int, double}, but, when looking up
16620 S we only want {double}. */
16621 tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
16622 complain, in_decl);
16623 if (argvec == error_mark_node)
16624 return error_mark_node;
16625
16626 tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
16627 complain);
16628 return cp_build_qualified_type (r, cp_type_quals (t), complain);
16629 }
16630 else
16631 /* This is not a template type, so there's nothing to do. */
16632 return t;
16633
16634 case ERROR_MARK:
16635 case IDENTIFIER_NODE:
16636 case VOID_TYPE:
16637 case OPAQUE_TYPE:
16638 case REAL_TYPE:
16639 case COMPLEX_TYPE:
16640 case VECTOR_TYPE:
16641 case BOOLEAN_TYPE:
16642 case NULLPTR_TYPE:
16643 case LANG_TYPE:
16644 return t;
16645
16646 case INTEGER_TYPE:
16647 if (t == integer_type_node)
16648 return t;
16649
16650 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
16651 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
16652 return t;
16653
16654 {
16655 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
16656
16657 max = tsubst_expr (omax, args, complain, in_decl);
16658
16659 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
16660 needed. */
16661 if (TREE_CODE (max) == NOP_EXPR
16662 && TREE_SIDE_EFFECTS (omax)
16663 && !TREE_TYPE (max))
16664 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
16665
16666 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
16667 with TREE_SIDE_EFFECTS that indicates this is not an integral
16668 constant expression. */
16669 if (processing_template_decl
16670 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
16671 {
16672 gcc_assert (TREE_CODE (max) == NOP_EXPR);
16673 TREE_SIDE_EFFECTS (max) = 1;
16674 }
16675
16676 return compute_array_index_type (NULL_TREE, max, complain);
16677 }
16678
16679 case TEMPLATE_TYPE_PARM:
16680 if (TEMPLATE_TYPE_LEVEL (t) == 0)
16681 {
16682 /* This is either an ordinary level-less auto or a CTAD placeholder
16683 auto. These get replaced only via do_auto_deduction which, in the
16684 ordinary case, temporarily overrides its level to 1 before calling
16685 tsubst. CTAD placeholders are replaced via do_class_deduction. */
16686 gcc_checking_assert (is_auto (t));
16687 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
16688 if (!tmpl)
16689 /* Ordinary level-less auto has nothing to substitute. */
16690 return t;
16691
16692 /* Substitute the template of this CTAD placeholder. */
16693 tmpl = tsubst_expr (tmpl, args, complain, in_decl);
16694 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16695 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
16696
16697 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
16698 return make_template_placeholder (tmpl);
16699 else
16700 return t;
16701 }
16702 /* Fall through. */
16703 case TEMPLATE_TEMPLATE_PARM:
16704 case BOUND_TEMPLATE_TEMPLATE_PARM:
16705 case TEMPLATE_PARM_INDEX:
16706 {
16707 int idx;
16708 int level;
16709 int levels;
16710 tree arg = NULL_TREE;
16711
16712 r = NULL_TREE;
16713
16714 gcc_assert (TREE_VEC_LENGTH (args) > 0);
16715 template_parm_level_and_index (t, &level, &idx);
16716
16717 levels = TMPL_ARGS_DEPTH (args);
16718 if (level <= levels
16719 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
16720 {
16721 arg = TMPL_ARG (args, level, idx);
16722
16723 /* See through ARGUMENT_PACK_SELECT arguments. */
16724 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
16725 arg = argument_pack_select_arg (arg);
16726 }
16727
16728 if (arg == error_mark_node)
16729 return error_mark_node;
16730 else if (arg != NULL_TREE)
16731 {
16732 if (ARGUMENT_PACK_P (arg))
16733 /* If ARG is an argument pack, we don't actually want to
16734 perform a substitution here, because substitutions
16735 for argument packs are only done
16736 element-by-element. We can get to this point when
16737 substituting the type of a non-type template
16738 parameter pack, when that type actually contains
16739 template parameter packs from an outer template, e.g.,
16740
16741 template<typename... Types> struct A {
16742 template<Types... Values> struct B { };
16743 }; */
16744 return t;
16745
16746 if (code == TEMPLATE_TYPE_PARM)
16747 {
16748 int quals;
16749
16750 gcc_assert (TYPE_P (arg));
16751
16752 quals = cp_type_quals (arg) | cp_type_quals (t);
16753
16754 return cp_build_qualified_type
16755 (arg, quals, complain | tf_ignore_bad_quals);
16756 }
16757 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
16758 {
16759 /* We are processing a type constructed from a
16760 template template parameter. */
16761 tree argvec = tsubst (TYPE_TI_ARGS (t),
16762 args, complain, in_decl);
16763 if (argvec == error_mark_node)
16764 return error_mark_node;
16765
16766 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
16767 || TREE_CODE (arg) == TEMPLATE_DECL
16768 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
16769
16770 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
16771 /* Consider this code:
16772
16773 template <template <class> class Template>
16774 struct Internal {
16775 template <class Arg> using Bind = Template<Arg>;
16776 };
16777
16778 template <template <class> class Template, class Arg>
16779 using Instantiate = Template<Arg>; //#0
16780
16781 template <template <class> class Template,
16782 class Argument>
16783 using Bind =
16784 Instantiate<Internal<Template>::template Bind,
16785 Argument>; //#1
16786
16787 When #1 is parsed, the
16788 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16789 parameter `Template' in #0 matches the
16790 UNBOUND_CLASS_TEMPLATE representing the argument
16791 `Internal<Template>::template Bind'; We then want
16792 to assemble the type `Bind<Argument>' that can't
16793 be fully created right now, because
16794 `Internal<Template>' not being complete, the Bind
16795 template cannot be looked up in that context. So
16796 we need to "store" `Bind<Argument>' for later
16797 when the context of Bind becomes complete. Let's
16798 store that in a TYPENAME_TYPE. */
16799 return make_typename_type (TYPE_CONTEXT (arg),
16800 build_nt (TEMPLATE_ID_EXPR,
16801 TYPE_IDENTIFIER (arg),
16802 argvec),
16803 typename_type,
16804 complain);
16805
16806 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16807 are resolving nested-types in the signature of a
16808 member function templates. Otherwise ARG is a
16809 TEMPLATE_DECL and is the real template to be
16810 instantiated. */
16811 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16812 arg = TYPE_NAME (arg);
16813
16814 r = lookup_template_class (arg,
16815 argvec, in_decl,
16816 DECL_CONTEXT (arg),
16817 complain);
16818 return cp_build_qualified_type
16819 (r, cp_type_quals (t) | cp_type_quals (r), complain);
16820 }
16821 else if (code == TEMPLATE_TEMPLATE_PARM)
16822 return arg;
16823 else
16824 /* TEMPLATE_PARM_INDEX. */
16825 return convert_from_reference (unshare_expr (arg));
16826 }
16827
16828 if (level == 1)
16829 /* This can happen during the attempted tsubst'ing in
16830 unify. This means that we don't yet have any information
16831 about the template parameter in question. */
16832 return t;
16833
16834 /* Early in template argument deduction substitution, we don't
16835 want to reduce the level of 'auto', or it will be confused
16836 with a normal template parm in subsequent deduction.
16837 Similarly, don't reduce the level of template parameters to
16838 avoid mismatches when deducing their types. */
16839 if (complain & tf_partial)
16840 return t;
16841
16842 /* If we get here, we must have been looking at a parm for a
16843 more deeply nested template. Make a new version of this
16844 template parameter, but with a lower level. */
16845 int quals;
16846 switch (code)
16847 {
16848 case TEMPLATE_TYPE_PARM:
16849 case TEMPLATE_TEMPLATE_PARM:
16850 quals = cp_type_quals (t);
16851 if (quals)
16852 {
16853 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
16854 t = TYPE_MAIN_VARIANT (t);
16855 }
16856
16857 if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
16858 if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
16859 && (code == TEMPLATE_TYPE_PARM
16860 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
16861 /* Cache lowering a type parameter or a simple template
16862 template parameter. */
16863 r = TREE_TYPE (d);
16864
16865 if (!r)
16866 {
16867 r = copy_type (t);
16868 TEMPLATE_TYPE_PARM_INDEX (r)
16869 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
16870 r, levels, args, complain);
16871 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
16872 TYPE_MAIN_VARIANT (r) = r;
16873 TYPE_POINTER_TO (r) = NULL_TREE;
16874 TYPE_REFERENCE_TO (r) = NULL_TREE;
16875
16876 if (code == TEMPLATE_TYPE_PARM)
16877 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
16878 /* Propagate constraints on placeholders since they are
16879 only instantiated during satisfaction. */
16880 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
16881
16882 if (TYPE_STRUCTURAL_EQUALITY_P (t))
16883 SET_TYPE_STRUCTURAL_EQUALITY (r);
16884 else
16885 TYPE_CANONICAL (r) = canonical_type_parameter (r);
16886 }
16887
16888 if (quals)
16889 r = cp_build_qualified_type (r, quals,
16890 complain | tf_ignore_bad_quals);
16891 break;
16892
16893 case BOUND_TEMPLATE_TEMPLATE_PARM:
16894 {
16895 tree tinfo = TYPE_TEMPLATE_INFO (t);
16896 /* We might need to substitute into the types of non-type
16897 template parameters. This also lowers the level of
16898 the ttp appropriately. */
16899 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
16900 complain, in_decl);
16901 if (tmpl == error_mark_node)
16902 return error_mark_node;
16903 tree argvec = tsubst (TI_ARGS (tinfo), args,
16904 complain, in_decl);
16905 if (argvec == error_mark_node)
16906 return error_mark_node;
16907 r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
16908 complain);
16909 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16910 break;
16911 }
16912
16913 case TEMPLATE_PARM_INDEX:
16914 /* OK, now substitute the type of the non-type parameter. We
16915 couldn't do it earlier because it might be an auto parameter,
16916 and we wouldn't need to if we had an argument. */
16917 type = tsubst (type, args, complain, in_decl);
16918 if (type == error_mark_node)
16919 return error_mark_node;
16920 r = reduce_template_parm_level (t, type, levels, args, complain);
16921 break;
16922
16923 default:
16924 gcc_unreachable ();
16925 }
16926
16927 return r;
16928 }
16929
16930 case TREE_LIST:
16931 return tsubst_tree_list (t, args, complain, in_decl);
16932
16933 case TREE_BINFO:
16934 /* We should never be tsubsting a binfo. */
16935 gcc_unreachable ();
16936
16937 case TREE_VEC:
16938 /* A vector of template arguments. */
16939 gcc_assert (!type);
16940 return tsubst_template_args (t, args, complain, in_decl);
16941
16942 case POINTER_TYPE:
16943 case REFERENCE_TYPE:
16944 {
16945 if (type == TREE_TYPE (t)
16946 && TREE_CODE (type) != METHOD_TYPE
16947 && (TYPE_ATTRIBUTES (t) == NULL_TREE
16948 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
16949 return t;
16950
16951 /* [temp.deduct]
16952
16953 Type deduction may fail for any of the following
16954 reasons:
16955
16956 -- Attempting to create a pointer to reference type.
16957 -- Attempting to create a reference to a reference type or
16958 a reference to void.
16959
16960 Core issue 106 says that creating a reference to a reference
16961 during instantiation is no longer a cause for failure. We
16962 only enforce this check in strict C++98 mode. */
16963 if ((TYPE_REF_P (type)
16964 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
16965 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
16966 {
16967 static location_t last_loc;
16968
16969 /* We keep track of the last time we issued this error
16970 message to avoid spewing a ton of messages during a
16971 single bad template instantiation. */
16972 if (complain & tf_error
16973 && last_loc != input_location)
16974 {
16975 if (VOID_TYPE_P (type))
16976 error ("forming reference to void");
16977 else if (code == POINTER_TYPE)
16978 error ("forming pointer to reference type %qT", type);
16979 else
16980 error ("forming reference to reference type %qT", type);
16981 last_loc = input_location;
16982 }
16983
16984 return error_mark_node;
16985 }
16986 else if (TREE_CODE (type) == FUNCTION_TYPE
16987 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
16988 || type_memfn_rqual (type) != REF_QUAL_NONE))
16989 {
16990 if (complain & tf_error)
16991 {
16992 if (code == POINTER_TYPE)
16993 error ("forming pointer to qualified function type %qT",
16994 type);
16995 else
16996 error ("forming reference to qualified function type %qT",
16997 type);
16998 }
16999 return error_mark_node;
17000 }
17001 else if (code == POINTER_TYPE)
17002 {
17003 r = build_pointer_type (type);
17004 if (TREE_CODE (type) == METHOD_TYPE)
17005 r = build_ptrmemfunc_type (r);
17006 }
17007 else if (TYPE_REF_P (type))
17008 /* In C++0x, during template argument substitution, when there is an
17009 attempt to create a reference to a reference type, reference
17010 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
17011
17012 "If a template-argument for a template-parameter T names a type
17013 that is a reference to a type A, an attempt to create the type
17014 'lvalue reference to cv T' creates the type 'lvalue reference to
17015 A,' while an attempt to create the type type rvalue reference to
17016 cv T' creates the type T"
17017 */
17018 r = cp_build_reference_type (TREE_TYPE (type),
17019 TYPE_REF_IS_RVALUE (t)
17020 && TYPE_REF_IS_RVALUE (type));
17021 else
17022 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
17023 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
17024
17025 if (r != error_mark_node)
17026 /* Will this ever be needed for TYPE_..._TO values? */
17027 layout_type (r);
17028
17029 if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
17030 /*flags=*/0,
17031 args, complain, in_decl))
17032 return error_mark_node;
17033
17034 return r;
17035 }
17036 case OFFSET_TYPE:
17037 {
17038 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
17039 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
17040 {
17041 /* [temp.deduct]
17042
17043 Type deduction may fail for any of the following
17044 reasons:
17045
17046 -- Attempting to create "pointer to member of T" when T
17047 is not a class type. */
17048 if (complain & tf_error)
17049 error ("creating pointer to member of non-class type %qT", r);
17050 return error_mark_node;
17051 }
17052 if (TYPE_REF_P (type))
17053 {
17054 if (complain & tf_error)
17055 error ("creating pointer to member reference type %qT", type);
17056 return error_mark_node;
17057 }
17058 if (VOID_TYPE_P (type))
17059 {
17060 if (complain & tf_error)
17061 error ("creating pointer to member of type void");
17062 return error_mark_node;
17063 }
17064 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
17065 if (TREE_CODE (type) == FUNCTION_TYPE)
17066 {
17067 /* The type of the implicit object parameter gets its
17068 cv-qualifiers from the FUNCTION_TYPE. */
17069 tree memptr;
17070 tree method_type
17071 = build_memfn_type (type, r, type_memfn_quals (type),
17072 type_memfn_rqual (type));
17073 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
17074 return cp_build_qualified_type (memptr, cp_type_quals (t),
17075 complain);
17076 }
17077 else
17078 return cp_build_qualified_type (build_ptrmem_type (r, type),
17079 cp_type_quals (t),
17080 complain);
17081 }
17082 case FUNCTION_TYPE:
17083 case METHOD_TYPE:
17084 {
17085 tree fntype;
17086 tree specs;
17087 fntype = tsubst_function_type (t, args, complain, in_decl);
17088 if (fntype == error_mark_node)
17089 return error_mark_node;
17090
17091 /* Substitute the exception specification. */
17092 specs = tsubst_exception_specification (t, args, complain, in_decl,
17093 /*defer_ok*/fndecl_type);
17094 if (specs == error_mark_node)
17095 return error_mark_node;
17096 if (specs)
17097 fntype = build_exception_variant (fntype, specs);
17098 return fntype;
17099 }
17100 case ARRAY_TYPE:
17101 {
17102 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
17103 if (domain == error_mark_node)
17104 return error_mark_node;
17105
17106 /* As an optimization, we avoid regenerating the array type if
17107 it will obviously be the same as T. */
17108 if (type == TREE_TYPE (t)
17109 && domain == TYPE_DOMAIN (t)
17110 && (TYPE_ATTRIBUTES (t) == NULL_TREE
17111 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
17112 return t;
17113
17114 /* These checks should match the ones in create_array_type_for_decl.
17115
17116 [temp.deduct]
17117
17118 The deduction may fail for any of the following reasons:
17119
17120 -- Attempting to create an array with an element type that
17121 is void, a function type, or a reference type, or [DR337]
17122 an abstract class type. */
17123 if (VOID_TYPE_P (type)
17124 || TREE_CODE (type) == FUNCTION_TYPE
17125 || (TREE_CODE (type) == ARRAY_TYPE
17126 && TYPE_DOMAIN (type) == NULL_TREE)
17127 || TYPE_REF_P (type))
17128 {
17129 if (complain & tf_error)
17130 error ("creating array of %qT", type);
17131 return error_mark_node;
17132 }
17133
17134 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
17135 !(complain & tf_error)))
17136 return error_mark_node;
17137
17138 r = build_cplus_array_type (type, domain);
17139
17140 if (!valid_array_size_p (input_location, r, in_decl,
17141 (complain & tf_error)))
17142 return error_mark_node;
17143
17144 if (TYPE_USER_ALIGN (t))
17145 {
17146 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
17147 TYPE_USER_ALIGN (r) = 1;
17148 }
17149
17150 if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
17151 /*flags=*/0,
17152 args, complain, in_decl))
17153 return error_mark_node;
17154
17155 return r;
17156 }
17157
17158 case TYPENAME_TYPE:
17159 {
17160 tree ctx = TYPE_CONTEXT (t);
17161 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
17162 {
17163 ctx = tsubst_pack_expansion (ctx, args,
17164 complain | tf_qualifying_scope,
17165 in_decl);
17166 if (ctx == error_mark_node)
17167 return error_mark_node;
17168 if (TREE_VEC_LENGTH (ctx) > 1)
17169 {
17170 if (complain & tf_error)
17171 error ("%qD expanded to more than one element",
17172 TYPENAME_TYPE_FULLNAME (t));
17173 return error_mark_node;
17174 }
17175 if (TREE_VEC_LENGTH (ctx) == 0)
17176 {
17177 if (complain & tf_error)
17178 error ("%qD is instantiated for an empty pack",
17179 TYPENAME_TYPE_FULLNAME (t));
17180 return error_mark_node;
17181 }
17182 ctx = TREE_VEC_ELT (ctx, 0);
17183 }
17184 else
17185 ctx = tsubst_entering_scope (ctx, args,
17186 complain | tf_qualifying_scope,
17187 in_decl);
17188 if (ctx == error_mark_node)
17189 return error_mark_node;
17190
17191 tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
17192 complain, in_decl);
17193 if (f == error_mark_node)
17194 return error_mark_node;
17195
17196 if (!MAYBE_CLASS_TYPE_P (ctx))
17197 {
17198 if (complain & tf_error)
17199 error ("%qT is not a class, struct, or union type", ctx);
17200 return error_mark_node;
17201 }
17202 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
17203 {
17204 /* Normally, make_typename_type does not require that the CTX
17205 have complete type in order to allow things like:
17206
17207 template <class T> struct S { typename S<T>::X Y; };
17208
17209 But, such constructs have already been resolved by this
17210 point, so here CTX really should have complete type, unless
17211 it's a partial instantiation. */
17212 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
17213 return error_mark_node;
17214 }
17215
17216 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
17217 tags. TYPENAME_TYPE should probably remember the exact tag that
17218 was written. */
17219 enum tag_types tag_type
17220 = TYPENAME_IS_CLASS_P (t) ? class_type
17221 : TYPENAME_IS_ENUM_P (t) ? enum_type
17222 : typename_type;
17223 tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
17224 tcomplain |= tst_ok_flag | qualifying_scope_flag;
17225 f = make_typename_type (ctx, f, tag_type, tcomplain);
17226 if (f == error_mark_node)
17227 return f;
17228 if (TREE_CODE (f) == TYPE_DECL)
17229 {
17230 complain |= tf_ignore_bad_quals;
17231 f = TREE_TYPE (f);
17232 }
17233
17234 if (!WILDCARD_TYPE_P (f))
17235 {
17236 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
17237 {
17238 if (complain & tf_error)
17239 error ("%qT resolves to %qT, which is not an enumeration type",
17240 t, f);
17241 else
17242 return error_mark_node;
17243 }
17244 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
17245 {
17246 if (complain & tf_error)
17247 error ("%qT resolves to %qT, which is not a class type",
17248 t, f);
17249 else
17250 return error_mark_node;
17251 }
17252 }
17253
17254 return cp_build_qualified_type
17255 (f, cp_type_quals (f) | cp_type_quals (t), complain);
17256 }
17257
17258 case UNBOUND_CLASS_TEMPLATE:
17259 {
17260 tree name = TYPE_IDENTIFIER (t);
17261 if (name == error_mark_node)
17262 return error_mark_node;
17263
17264 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
17265 parm_list = tsubst_template_parms (parm_list, args, complain);
17266 if (parm_list == error_mark_node)
17267 return error_mark_node;
17268
17269 if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
17270 ++processing_template_decl;
17271 tree ctx = tsubst_entering_scope (TYPE_CONTEXT (t), args,
17272 complain, in_decl);
17273 if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
17274 --processing_template_decl;
17275 if (ctx == error_mark_node)
17276 return error_mark_node;
17277
17278 return make_unbound_class_template (ctx, name, parm_list, complain);
17279 }
17280
17281 case TYPEOF_TYPE:
17282 {
17283 tree type;
17284
17285 ++cp_unevaluated_operand;
17286 ++c_inhibit_evaluation_warnings;
17287
17288 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
17289
17290 --cp_unevaluated_operand;
17291 --c_inhibit_evaluation_warnings;
17292
17293 type = finish_typeof (type);
17294 return cp_build_qualified_type (type,
17295 cp_type_quals (t)
17296 | cp_type_quals (type),
17297 complain);
17298 }
17299
17300 case DECLTYPE_TYPE:
17301 {
17302 tree type;
17303
17304 ++cp_unevaluated_operand;
17305 ++c_inhibit_evaluation_warnings;
17306
17307 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
17308 complain|tf_decltype, in_decl);
17309
17310 --cp_unevaluated_operand;
17311 --c_inhibit_evaluation_warnings;
17312
17313 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
17314 type = lambda_capture_field_type (type,
17315 false /*explicit_init*/,
17316 DECLTYPE_FOR_REF_CAPTURE (t));
17317 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
17318 type = lambda_proxy_type (type);
17319 else
17320 {
17321 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
17322 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
17323 && EXPR_P (type))
17324 /* In a template ~id could be either a complement expression
17325 or an unqualified-id naming a destructor; if instantiating
17326 it produces an expression, it's not an id-expression or
17327 member access. */
17328 id = false;
17329 type = finish_decltype_type (type, id, complain);
17330 }
17331 return cp_build_qualified_type (type,
17332 cp_type_quals (t)
17333 | cp_type_quals (type),
17334 complain | tf_ignore_bad_quals);
17335 }
17336
17337 case TRAIT_TYPE:
17338 {
17339 tree type1 = TRAIT_TYPE_TYPE1 (t);
17340 if (TYPE_P (type1))
17341 type1 = tsubst (type1, args, complain, in_decl);
17342 else
17343 type1 = tsubst_expr (type1, args, complain, in_decl);
17344 tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
17345 type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
17346 return cp_build_qualified_type (type,
17347 cp_type_quals (t) | cp_type_quals (type),
17348 complain | tf_ignore_bad_quals);
17349 }
17350
17351 case TYPE_ARGUMENT_PACK:
17352 case NONTYPE_ARGUMENT_PACK:
17353 return tsubst_argument_pack (t, args, complain, in_decl);
17354
17355 case PACK_INDEX_TYPE:
17356 return tsubst_pack_index (t, args, complain, in_decl);
17357
17358 case VOID_CST:
17359 case INTEGER_CST:
17360 case REAL_CST:
17361 case STRING_CST:
17362 case PLUS_EXPR:
17363 case MINUS_EXPR:
17364 case NEGATE_EXPR:
17365 case NOP_EXPR:
17366 case INDIRECT_REF:
17367 case ADDR_EXPR:
17368 case CALL_EXPR:
17369 case ARRAY_REF:
17370 case SCOPE_REF:
17371 case OMP_ARRAY_SECTION:
17372 /* We should use one of the expression tsubsts for these codes. */
17373 gcc_unreachable ();
17374
17375 default:
17376 sorry ("use of %qs in template", get_tree_code_name (code));
17377 return error_mark_node;
17378 }
17379 }
17380
17381 /* Convenience wrapper over tsubst for substituting into the LHS
17382 of the :: scope resolution operator. */
17383
17384 static tree
17385 tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17386 {
17387 gcc_checking_assert (TYPE_P (t));
17388 return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
17389 }
17390
17391 /* Convenience wrapper over tsubst for substituting into an id-expression
17392 without resolving its terminal name. */
17393
17394 static tree
17395 tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17396 {
17397 return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
17398 }
17399
17400 /* OLDFNS is a lookup set of member functions from some class template, and
17401 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
17402 of that class template. Return the subset of NEWFNS which are
17403 specializations of a function from OLDFNS. */
17404
17405 static tree
17406 filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
17407 {
17408 /* Record all member functions from the old lookup set OLDFNS into
17409 VISIBLE_SET. */
17410 hash_set<tree> visible_set;
17411 bool seen_dep_using = false;
17412 for (tree fn : lkp_range (oldfns))
17413 {
17414 if (TREE_CODE (fn) == USING_DECL)
17415 {
17416 /* Imprecisely handle dependent using-decl by keeping all members
17417 in the new lookup set that are defined in a base class, i.e.
17418 members that could plausibly have been introduced by this
17419 dependent using-decl.
17420 FIXME: Track which members are introduced by a dependent
17421 using-decl precisely, perhaps by performing another lookup
17422 from the substituted USING_DECL_SCOPE. */
17423 gcc_checking_assert (DECL_DEPENDENT_P (fn));
17424 seen_dep_using = true;
17425 }
17426 else
17427 visible_set.add (fn);
17428 }
17429
17430 /* Returns true iff (a less specialized version of) FN appeared in
17431 the old lookup set OLDFNS. */
17432 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
17433 if (DECL_CONTEXT (fn) != newtype)
17434 /* FN is a member function from a base class, introduced via a
17435 using-decl; if it might have been introduced by a dependent
17436 using-decl then just conservatively keep it, otherwise look
17437 in the old lookup set for FN exactly. */
17438 return seen_dep_using || visible_set.contains (fn);
17439 else if (TREE_CODE (fn) == TEMPLATE_DECL)
17440 /* FN is a member function template from the current class;
17441 look in the old lookup set for the TEMPLATE_DECL from which
17442 it was specialized. */
17443 return visible_set.contains (DECL_TI_TEMPLATE (fn));
17444 else
17445 /* FN is a non-template member function from the current class;
17446 look in the old lookup set for the FUNCTION_DECL from which
17447 it was specialized. */
17448 return visible_set.contains (DECL_TEMPLATE_RESULT
17449 (DECL_TI_TEMPLATE (fn)));
17450 };
17451
17452 bool lookup_changed_p = false;
17453 for (tree fn : lkp_range (newfns))
17454 if (!visible_p (fn))
17455 {
17456 lookup_changed_p = true;
17457 break;
17458 }
17459 if (!lookup_changed_p)
17460 return newfns;
17461
17462 /* Filter out from NEWFNS the member functions that weren't
17463 previously visible according to OLDFNS. */
17464 tree filtered_fns = NULL_TREE;
17465 unsigned filtered_size = 0;
17466 for (tree fn : lkp_range (newfns))
17467 if (visible_p (fn))
17468 {
17469 filtered_fns = lookup_add (fn, filtered_fns);
17470 filtered_size++;
17471 }
17472 gcc_checking_assert (seen_dep_using
17473 ? filtered_size >= visible_set.elements ()
17474 : filtered_size == visible_set.elements ());
17475
17476 return filtered_fns;
17477 }
17478
17479 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
17480 expression on the left-hand side of the "." or "->" operator. We
17481 only do the lookup if we had a dependent BASELINK. Otherwise we
17482 adjust it onto the instantiated heirarchy. */
17483
17484 static tree
17485 tsubst_baselink (tree baselink, tree object_type,
17486 tree args, tsubst_flags_t complain, tree in_decl)
17487 {
17488 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
17489 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
17490 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
17491
17492 tree optype = BASELINK_OPTYPE (baselink);
17493 optype = tsubst (optype, args, complain, in_decl);
17494
17495 tree template_args = NULL_TREE;
17496 bool template_id_p = false;
17497 tree fns = BASELINK_FUNCTIONS (baselink);
17498 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
17499 {
17500 template_id_p = true;
17501 template_args = TREE_OPERAND (fns, 1);
17502 fns = TREE_OPERAND (fns, 0);
17503 if (template_args)
17504 template_args = tsubst_template_args (template_args, args,
17505 complain, in_decl);
17506 if (template_args == error_mark_node)
17507 return error_mark_node;
17508 }
17509
17510 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
17511 binfo_type = tsubst (binfo_type, args, complain, in_decl);
17512 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
17513 || optype != BASELINK_OPTYPE (baselink));
17514
17515 if (dependent_p)
17516 {
17517 tree name = OVL_NAME (fns);
17518 if (IDENTIFIER_CONV_OP_P (name))
17519 name = make_conv_op_name (optype);
17520
17521 /* See maybe_dependent_member_ref. */
17522 if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
17523 {
17524 if (template_id_p)
17525 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
17526 template_args);
17527 return build_qualified_name (NULL_TREE, qualifying_scope, name,
17528 /* ::template */false);
17529 }
17530
17531 if (name == complete_dtor_identifier)
17532 /* Treat as-if non-dependent below. */
17533 dependent_p = false;
17534
17535 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
17536 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
17537 complain);
17538 if (!baselink)
17539 {
17540 if ((complain & tf_error)
17541 && constructor_name_p (name, qualifying_scope))
17542 error ("cannot call constructor %<%T::%D%> directly",
17543 qualifying_scope, name);
17544 return error_mark_node;
17545 }
17546
17547 if (maybe_incomplete)
17548 {
17549 /* Filter out from the new lookup set those functions which didn't
17550 appear in the original lookup set (in a less specialized form).
17551 This is needed to preserve the consistency of member lookup
17552 performed in an incomplete-class context, within which
17553 later-declared members ought to remain invisible. */
17554 BASELINK_FUNCTIONS (baselink)
17555 = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
17556 binfo_type);
17557 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
17558 }
17559
17560 fns = BASELINK_FUNCTIONS (baselink);
17561 }
17562 else
17563 {
17564 /* We're going to overwrite pieces below, make a duplicate. */
17565 baselink = copy_node (baselink);
17566
17567 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
17568 {
17569 /* The decl we found was from non-dependent scope, but we still need
17570 to update the binfos for the instantiated qualifying_scope. */
17571 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
17572 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
17573 ba_unique, nullptr, complain);
17574 }
17575 }
17576
17577 /* If lookup found a single function, mark it as used at this point.
17578 (If lookup found multiple functions the one selected later by
17579 overload resolution will be marked as used at that point.) */
17580 if (!template_id_p && !really_overloaded_fn (fns))
17581 {
17582 tree fn = OVL_FIRST (fns);
17583 bool ok = mark_used (fn, complain);
17584 if (!ok && !(complain & tf_error))
17585 return error_mark_node;
17586 if (ok && BASELINK_P (baselink))
17587 /* We might have instantiated an auto function. */
17588 TREE_TYPE (baselink) = TREE_TYPE (fn);
17589 }
17590
17591 if (BASELINK_P (baselink))
17592 {
17593 /* Add back the template arguments, if present. */
17594 if (template_id_p)
17595 BASELINK_FUNCTIONS (baselink)
17596 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
17597
17598 /* Update the conversion operator type. */
17599 BASELINK_OPTYPE (baselink) = optype;
17600 }
17601
17602 if (!object_type)
17603 object_type = current_class_type;
17604
17605 if (qualified_p || !dependent_p)
17606 {
17607 baselink = adjust_result_of_qualified_name_lookup (baselink,
17608 qualifying_scope,
17609 object_type);
17610 if (!qualified_p)
17611 /* We need to call adjust_result_of_qualified_name_lookup in case the
17612 destructor names a base class, but we unset BASELINK_QUALIFIED_P
17613 so that we still get virtual function binding. */
17614 BASELINK_QUALIFIED_P (baselink) = false;
17615 }
17616
17617 return baselink;
17618 }
17619
17620 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
17621 true if the qualified-id will be a postfix-expression in-and-of
17622 itself; false if more of the postfix-expression follows the
17623 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
17624 of "&". */
17625
17626 static tree
17627 tsubst_qualified_id (tree qualified_id, tree args,
17628 tsubst_flags_t complain, tree in_decl,
17629 bool done, bool address_p)
17630 {
17631 tree expr;
17632 tree scope;
17633 tree name;
17634 bool is_template;
17635 tree template_args;
17636 location_t loc = EXPR_LOCATION (qualified_id);
17637
17638 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
17639
17640 /* Figure out what name to look up. */
17641 name = TREE_OPERAND (qualified_id, 1);
17642 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17643 {
17644 is_template = true;
17645 template_args = TREE_OPERAND (name, 1);
17646 if (template_args)
17647 template_args = tsubst_template_args (template_args, args,
17648 complain, in_decl);
17649 if (template_args == error_mark_node)
17650 return error_mark_node;
17651 name = TREE_OPERAND (name, 0);
17652 }
17653 else
17654 {
17655 is_template = false;
17656 template_args = NULL_TREE;
17657 }
17658
17659 /* Substitute into the qualifying scope. When there are no ARGS, we
17660 are just trying to simplify a non-dependent expression. In that
17661 case the qualifying scope may be dependent, and, in any case,
17662 substituting will not help. */
17663 scope = TREE_OPERAND (qualified_id, 0);
17664 if (args)
17665 {
17666 scope = tsubst_scope (scope, args, complain, in_decl);
17667 expr = tsubst_name (name, args, complain, in_decl);
17668 }
17669 else
17670 expr = name;
17671
17672 if (dependent_scope_p (scope))
17673 {
17674 if (TREE_CODE (expr) == SCOPE_REF)
17675 /* We built one in tsubst_baselink. */
17676 gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
17677 else
17678 {
17679 if (is_template)
17680 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
17681 template_args);
17682 expr = build_qualified_name (NULL_TREE, scope, expr,
17683 QUALIFIED_NAME_IS_TEMPLATE
17684 (qualified_id));
17685 }
17686 REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
17687 return expr;
17688 }
17689
17690 if (!BASELINK_P (name) && !DECL_P (expr))
17691 {
17692 if (TREE_CODE (expr) == BIT_NOT_EXPR)
17693 {
17694 /* A BIT_NOT_EXPR is used to represent a destructor. */
17695 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
17696 {
17697 error ("qualifying type %qT does not match destructor name ~%qT",
17698 scope, TREE_OPERAND (expr, 0));
17699 expr = error_mark_node;
17700 }
17701 else
17702 expr = lookup_qualified_name (scope, complete_dtor_identifier,
17703 LOOK_want::NORMAL, false);
17704 }
17705 else
17706 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
17707 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
17708 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
17709 {
17710 if (complain & tf_error)
17711 {
17712 auto_diagnostic_group d;
17713 error ("dependent-name %qE is parsed as a non-type, but "
17714 "instantiation yields a type", qualified_id);
17715 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
17716 }
17717 return error_mark_node;
17718 }
17719 }
17720
17721 if (DECL_P (expr))
17722 {
17723 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
17724 scope, complain))
17725 return error_mark_node;
17726 /* Remember that there was a reference to this entity. */
17727 if (!mark_used (expr, complain) && !(complain & tf_error))
17728 return error_mark_node;
17729 }
17730
17731 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
17732 {
17733 if (complain & tf_error)
17734 qualified_name_lookup_error (scope,
17735 TREE_OPERAND (qualified_id, 1),
17736 expr, input_location);
17737 return error_mark_node;
17738 }
17739
17740 if (is_template)
17741 {
17742 if (variable_template_p (expr))
17743 expr = lookup_and_finish_template_variable (expr, template_args,
17744 complain);
17745 else
17746 expr = lookup_template_function (expr, template_args);
17747 }
17748
17749 if (expr == error_mark_node && complain & tf_error)
17750 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
17751 expr, input_location);
17752 else if (TYPE_P (scope))
17753 {
17754 expr = (adjust_result_of_qualified_name_lookup
17755 (expr, scope, current_nonlambda_class_type ()));
17756 expr = (finish_qualified_id_expr
17757 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
17758 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
17759 /*template_arg_p=*/false, complain));
17760 }
17761
17762 /* Expressions do not generally have reference type. */
17763 if (TREE_CODE (expr) != SCOPE_REF
17764 /* However, if we're about to form a pointer-to-member, we just
17765 want the referenced member referenced. */
17766 && TREE_CODE (expr) != OFFSET_REF)
17767 expr = convert_from_reference (expr);
17768
17769 if (REF_PARENTHESIZED_P (qualified_id))
17770 expr = force_paren_expr (expr);
17771
17772 expr = maybe_wrap_with_location (expr, loc);
17773
17774 return expr;
17775 }
17776
17777 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
17778 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17779 for tsubst. */
17780
17781 static tree
17782 tsubst_init (tree init, tree decl, tree args,
17783 tsubst_flags_t complain, tree in_decl)
17784 {
17785 if (!init)
17786 return NULL_TREE;
17787
17788 init = tsubst_expr (init, args, complain, in_decl);
17789
17790 tree type = TREE_TYPE (decl);
17791
17792 if (!init && type != error_mark_node)
17793 {
17794 if (tree auto_node = type_uses_auto (type))
17795 {
17796 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17797 {
17798 if (complain & tf_error)
17799 error ("initializer for %q#D expands to an empty list "
17800 "of expressions", decl);
17801 return error_mark_node;
17802 }
17803 }
17804 else if (!dependent_type_p (type))
17805 {
17806 /* If we had an initializer but it
17807 instantiated to nothing,
17808 value-initialize the object. This will
17809 only occur when the initializer was a
17810 pack expansion where the parameter packs
17811 used in that expansion were of length
17812 zero. */
17813 init = build_value_init (type, complain);
17814 if (TREE_CODE (init) == AGGR_INIT_EXPR)
17815 init = get_target_expr (init, complain);
17816 if (TREE_CODE (init) == TARGET_EXPR)
17817 TARGET_EXPR_DIRECT_INIT_P (init) = true;
17818 }
17819 }
17820
17821 return init;
17822 }
17823
17824 /* If T is a reference to a dependent member of the current instantiation C and
17825 we are trying to refer to that member in a partial instantiation of C,
17826 return a SCOPE_REF; otherwise, return NULL_TREE.
17827
17828 This can happen when forming a C++17 deduction guide, as in PR96199. */
17829
17830 static tree
17831 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
17832 tree in_decl)
17833 {
17834 if (!(complain & tf_dguide))
17835 return NULL_TREE;
17836
17837 tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
17838 if (!decl || !DECL_P (decl))
17839 return NULL_TREE;
17840
17841 tree ctx = context_for_name_lookup (decl);
17842 if (!CLASS_TYPE_P (ctx))
17843 return NULL_TREE;
17844
17845 ctx = tsubst (ctx, args, complain, in_decl);
17846 if (!dependent_scope_p (ctx))
17847 return NULL_TREE;
17848
17849 if (TYPE_P (t))
17850 {
17851 bool stripped = false;
17852 if (typedef_variant_p (t))
17853 {
17854 /* Since this transformation may undesirably turn a deduced context
17855 into a non-deduced one, we'd rather strip typedefs than perform
17856 the transformation. */
17857 tree u = strip_typedefs (t);
17858 if (u != t)
17859 {
17860 stripped = true;
17861 t = u;
17862 }
17863 }
17864 decl = TYPE_NAME (t);
17865 if (decl)
17866 decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
17867 if (!decl)
17868 {
17869 if (stripped)
17870 /* The original type was an alias from the current instantiation
17871 which we stripped to something outside it. At this point we
17872 need to commit to using the stripped type rather than deferring
17873 to the caller (which would use the original type), to ensure
17874 eligible bits of the stripped type get transformed. */
17875 return tsubst (t, args, complain, in_decl);
17876 else
17877 /* The original type wasn't a typedef, and we decided it doesn't
17878 need rewriting, so just let the caller (tsubst) substitute it
17879 normally. */
17880 return NULL_TREE;
17881 }
17882 return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
17883 complain);
17884 }
17885
17886 tree name = DECL_NAME (t);
17887 tree fullname = name;
17888 if (instantiates_primary_template_p (t))
17889 {
17890 tree tinfo = get_template_info (t);
17891 name = DECL_NAME (TI_TEMPLATE (tinfo));
17892 tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
17893 targs = tsubst_template_args (targs, args, complain, in_decl);
17894 fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
17895 }
17896
17897 if (TREE_CODE (t) == TYPE_DECL)
17898 {
17899 if (!is_typedef_decl (t)
17900 && TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
17901 && TYPE_NAME (TREE_TYPE (t)) == t)
17902 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17903 scope, but it doesn't need to be rewritten again. */
17904 return NULL_TREE;
17905 tree type = build_typename_type (ctx, name, fullname, typename_type);
17906 return TYPE_NAME (type);
17907 }
17908 else if (DECL_TYPE_TEMPLATE_P (t))
17909 return make_unbound_class_template (ctx, name,
17910 NULL_TREE, complain);
17911 else
17912 return build_qualified_name (NULL_TREE, ctx, fullname,
17913 TREE_CODE (t) == TEMPLATE_DECL);
17914 }
17915
17916 /* Helper function for tsubst_omp_clauses, used for instantiation of
17917 OMP_CLAUSE_DECL of clauses. */
17918
17919 static tree
17920 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17921 tree in_decl, tree *iterator_cache)
17922 {
17923 if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
17924 return decl;
17925
17926 /* Handle OpenMP iterators. */
17927 if (TREE_CODE (decl) == TREE_LIST
17928 && TREE_PURPOSE (decl)
17929 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17930 {
17931 tree ret;
17932 if (iterator_cache[0] == TREE_PURPOSE (decl))
17933 ret = iterator_cache[1];
17934 else
17935 {
17936 tree *tp = &ret;
17937 begin_scope (sk_omp, NULL);
17938 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17939 {
17940 *tp = copy_node (it);
17941 TREE_VEC_ELT (*tp, 0)
17942 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17943 DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
17944 pushdecl (TREE_VEC_ELT (*tp, 0));
17945 TREE_VEC_ELT (*tp, 1)
17946 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
17947 TREE_VEC_ELT (*tp, 2)
17948 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
17949 TREE_VEC_ELT (*tp, 3)
17950 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
17951 TREE_CHAIN (*tp) = NULL_TREE;
17952 tp = &TREE_CHAIN (*tp);
17953 }
17954 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17955 iterator_cache[0] = TREE_PURPOSE (decl);
17956 iterator_cache[1] = ret;
17957 }
17958 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17959 args, complain,
17960 in_decl, NULL));
17961 }
17962
17963 /* Handle an OpenMP array section represented as a TREE_LIST (or
17964 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17965 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17966 TREE_LIST. We can handle it exactly the same as an array section
17967 (purpose, value, and a chain), even though the nomenclature
17968 (low_bound, length, etc) is different. */
17969 if (TREE_CODE (decl) == TREE_LIST)
17970 {
17971 tree low_bound
17972 = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
17973 tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
17974 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17975 in_decl, NULL);
17976 if (TREE_PURPOSE (decl) == low_bound
17977 && TREE_VALUE (decl) == length
17978 && TREE_CHAIN (decl) == chain)
17979 return decl;
17980 tree ret = tree_cons (low_bound, length, chain);
17981 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
17982 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
17983 return ret;
17984 }
17985 else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
17986 {
17987 tree low_bound
17988 = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
17989 tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
17990 in_decl);
17991 tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
17992 complain, in_decl, NULL);
17993 if (TREE_OPERAND (decl, 0) == base
17994 && TREE_OPERAND (decl, 1) == low_bound
17995 && TREE_OPERAND (decl, 2) == length)
17996 return decl;
17997 return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
17998 length);
17999 }
18000 tree ret = tsubst_stmt (decl, args, complain, in_decl);
18001 /* Undo convert_from_reference tsubst_expr could have called. */
18002 if (decl
18003 && REFERENCE_REF_P (ret)
18004 && !REFERENCE_REF_P (decl))
18005 ret = TREE_OPERAND (ret, 0);
18006 return ret;
18007 }
18008
18009 /* Like tsubst_copy, but specifically for OpenMP clauses. */
18010
18011 static tree
18012 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
18013 tree args, tsubst_flags_t complain, tree in_decl)
18014 {
18015 tree new_clauses = NULL_TREE, nc, oc;
18016 tree linear_no_step = NULL_TREE;
18017 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
18018
18019 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
18020 {
18021 nc = copy_node (oc);
18022 OMP_CLAUSE_CHAIN (nc) = new_clauses;
18023 new_clauses = nc;
18024
18025 switch (OMP_CLAUSE_CODE (nc))
18026 {
18027 case OMP_CLAUSE_LASTPRIVATE:
18028 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
18029 {
18030 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
18031 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
18032 complain, in_decl);
18033 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
18034 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
18035 }
18036 /* FALLTHRU */
18037 case OMP_CLAUSE_PRIVATE:
18038 case OMP_CLAUSE_SHARED:
18039 case OMP_CLAUSE_FIRSTPRIVATE:
18040 case OMP_CLAUSE_COPYIN:
18041 case OMP_CLAUSE_COPYPRIVATE:
18042 case OMP_CLAUSE_UNIFORM:
18043 case OMP_CLAUSE_DEPEND:
18044 case OMP_CLAUSE_DOACROSS:
18045 case OMP_CLAUSE_AFFINITY:
18046 case OMP_CLAUSE_FROM:
18047 case OMP_CLAUSE_TO:
18048 case OMP_CLAUSE_MAP:
18049 case OMP_CLAUSE__CACHE_:
18050 case OMP_CLAUSE_NONTEMPORAL:
18051 case OMP_CLAUSE_USE_DEVICE_PTR:
18052 case OMP_CLAUSE_USE_DEVICE_ADDR:
18053 case OMP_CLAUSE_IS_DEVICE_PTR:
18054 case OMP_CLAUSE_HAS_DEVICE_ADDR:
18055 case OMP_CLAUSE_INCLUSIVE:
18056 case OMP_CLAUSE_EXCLUSIVE:
18057 OMP_CLAUSE_DECL (nc)
18058 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18059 in_decl, iterator_cache);
18060 break;
18061 case OMP_CLAUSE_NUM_TEAMS:
18062 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
18063 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
18064 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
18065 complain, in_decl);
18066 /* FALLTHRU */
18067 case OMP_CLAUSE_TILE:
18068 case OMP_CLAUSE_IF:
18069 case OMP_CLAUSE_SELF:
18070 case OMP_CLAUSE_NUM_THREADS:
18071 case OMP_CLAUSE_SCHEDULE:
18072 case OMP_CLAUSE_COLLAPSE:
18073 case OMP_CLAUSE_FINAL:
18074 case OMP_CLAUSE_DEVICE:
18075 case OMP_CLAUSE_DIST_SCHEDULE:
18076 case OMP_CLAUSE_THREAD_LIMIT:
18077 case OMP_CLAUSE_SAFELEN:
18078 case OMP_CLAUSE_SIMDLEN:
18079 case OMP_CLAUSE_NUM_TASKS:
18080 case OMP_CLAUSE_GRAINSIZE:
18081 case OMP_CLAUSE_PRIORITY:
18082 case OMP_CLAUSE_ORDERED:
18083 case OMP_CLAUSE_HINT:
18084 case OMP_CLAUSE_FILTER:
18085 case OMP_CLAUSE_NUM_GANGS:
18086 case OMP_CLAUSE_NUM_WORKERS:
18087 case OMP_CLAUSE_VECTOR_LENGTH:
18088 case OMP_CLAUSE_WORKER:
18089 case OMP_CLAUSE_VECTOR:
18090 case OMP_CLAUSE_ASYNC:
18091 case OMP_CLAUSE_WAIT:
18092 case OMP_CLAUSE_DETACH:
18093 OMP_CLAUSE_OPERAND (nc, 0)
18094 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
18095 break;
18096 case OMP_CLAUSE_PARTIAL:
18097 OMP_CLAUSE_PARTIAL_EXPR (nc)
18098 = tsubst_expr (OMP_CLAUSE_PARTIAL_EXPR (oc), args, complain,
18099 in_decl);
18100 break;
18101 case OMP_CLAUSE_SIZES:
18102 OMP_CLAUSE_SIZES_LIST (nc)
18103 = tsubst_expr (OMP_CLAUSE_SIZES_LIST (oc), args, complain,
18104 in_decl);
18105 break;
18106 case OMP_CLAUSE_NOCONTEXT:
18107 OMP_CLAUSE_NOCONTEXT_EXPR (nc)
18108 = tsubst_expr (OMP_CLAUSE_NOCONTEXT_EXPR (oc), args, complain,
18109 in_decl);
18110 break;
18111 case OMP_CLAUSE_NOVARIANTS:
18112 OMP_CLAUSE_NOVARIANTS_EXPR (nc)
18113 = tsubst_expr (OMP_CLAUSE_NOVARIANTS_EXPR (oc), args, complain,
18114 in_decl);
18115 break;
18116 case OMP_CLAUSE_REDUCTION:
18117 case OMP_CLAUSE_IN_REDUCTION:
18118 case OMP_CLAUSE_TASK_REDUCTION:
18119 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
18120 {
18121 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
18122 if (TREE_CODE (placeholder) == SCOPE_REF)
18123 {
18124 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
18125 complain, in_decl);
18126 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
18127 = build_qualified_name (NULL_TREE, scope,
18128 TREE_OPERAND (placeholder, 1),
18129 false);
18130 }
18131 else
18132 gcc_assert (identifier_p (placeholder));
18133 }
18134 OMP_CLAUSE_DECL (nc)
18135 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18136 in_decl, NULL);
18137 break;
18138 case OMP_CLAUSE_GANG:
18139 case OMP_CLAUSE_ALIGNED:
18140 OMP_CLAUSE_DECL (nc)
18141 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18142 in_decl, NULL);
18143 OMP_CLAUSE_OPERAND (nc, 1)
18144 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
18145 break;
18146 case OMP_CLAUSE_ALLOCATE:
18147 OMP_CLAUSE_DECL (nc)
18148 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18149 in_decl, NULL);
18150 OMP_CLAUSE_OPERAND (nc, 1)
18151 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
18152 OMP_CLAUSE_OPERAND (nc, 2)
18153 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
18154 break;
18155 case OMP_CLAUSE_LINEAR:
18156 OMP_CLAUSE_DECL (nc)
18157 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18158 in_decl, NULL);
18159 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
18160 {
18161 gcc_assert (!linear_no_step);
18162 linear_no_step = nc;
18163 }
18164 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
18165 OMP_CLAUSE_LINEAR_STEP (nc)
18166 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
18167 complain, in_decl, NULL);
18168 else
18169 OMP_CLAUSE_LINEAR_STEP (nc)
18170 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
18171 complain, in_decl);
18172 break;
18173 case OMP_CLAUSE_INIT:
18174 if (ort == C_ORT_OMP_INTEROP
18175 && OMP_CLAUSE_INIT_PREFER_TYPE (nc)
18176 && TREE_CODE (OMP_CLAUSE_INIT_PREFER_TYPE (nc)) == TREE_LIST
18177 && (OMP_CLAUSE_CHAIN (nc) == NULL_TREE
18178 || OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (nc) ) != OMP_CLAUSE_INIT
18179 || (OMP_CLAUSE_INIT_PREFER_TYPE (nc)
18180 != OMP_CLAUSE_INIT_PREFER_TYPE (OMP_CLAUSE_CHAIN (nc) ))))
18181 {
18182 tree pref_list = OMP_CLAUSE_INIT_PREFER_TYPE (nc);
18183 tree fr_list = TREE_VALUE (pref_list);
18184 int len = TREE_VEC_LENGTH (fr_list);
18185 for (int i = 0; i < len; i++)
18186 {
18187 tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
18188 /* Preserve NOP_EXPR to have a location. */
18189 if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
18190 TREE_OPERAND (*fr_expr, 0)
18191 = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
18192 in_decl);
18193 else
18194 *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
18195 }
18196 }
18197 /* FALLTHRU */
18198 case OMP_CLAUSE_DESTROY:
18199 case OMP_CLAUSE_USE:
18200 case OMP_CLAUSE_INTEROP:
18201 OMP_CLAUSE_OPERAND (nc, 0)
18202 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
18203 break;
18204 case OMP_CLAUSE_NOWAIT:
18205 case OMP_CLAUSE_DEFAULT:
18206 case OMP_CLAUSE_UNTIED:
18207 case OMP_CLAUSE_MERGEABLE:
18208 case OMP_CLAUSE_INBRANCH:
18209 case OMP_CLAUSE_NOTINBRANCH:
18210 case OMP_CLAUSE_PROC_BIND:
18211 case OMP_CLAUSE_FOR:
18212 case OMP_CLAUSE_PARALLEL:
18213 case OMP_CLAUSE_SECTIONS:
18214 case OMP_CLAUSE_TASKGROUP:
18215 case OMP_CLAUSE_NOGROUP:
18216 case OMP_CLAUSE_THREADS:
18217 case OMP_CLAUSE_SIMD:
18218 case OMP_CLAUSE_DEFAULTMAP:
18219 case OMP_CLAUSE_ORDER:
18220 case OMP_CLAUSE_BIND:
18221 case OMP_CLAUSE_INDEPENDENT:
18222 case OMP_CLAUSE_AUTO:
18223 case OMP_CLAUSE_SEQ:
18224 case OMP_CLAUSE_IF_PRESENT:
18225 case OMP_CLAUSE_FINALIZE:
18226 case OMP_CLAUSE_NOHOST:
18227 case OMP_CLAUSE_FULL:
18228 break;
18229 default:
18230 gcc_unreachable ();
18231 }
18232 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
18233 switch (OMP_CLAUSE_CODE (nc))
18234 {
18235 case OMP_CLAUSE_SHARED:
18236 case OMP_CLAUSE_PRIVATE:
18237 case OMP_CLAUSE_FIRSTPRIVATE:
18238 case OMP_CLAUSE_LASTPRIVATE:
18239 case OMP_CLAUSE_COPYPRIVATE:
18240 case OMP_CLAUSE_LINEAR:
18241 case OMP_CLAUSE_REDUCTION:
18242 case OMP_CLAUSE_IN_REDUCTION:
18243 case OMP_CLAUSE_TASK_REDUCTION:
18244 case OMP_CLAUSE_USE_DEVICE_PTR:
18245 case OMP_CLAUSE_USE_DEVICE_ADDR:
18246 case OMP_CLAUSE_IS_DEVICE_PTR:
18247 case OMP_CLAUSE_HAS_DEVICE_ADDR:
18248 case OMP_CLAUSE_INCLUSIVE:
18249 case OMP_CLAUSE_EXCLUSIVE:
18250 case OMP_CLAUSE_ALLOCATE:
18251 /* tsubst_expr on SCOPE_REF results in returning
18252 finish_non_static_data_member result. Undo that here. */
18253 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
18254 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
18255 == IDENTIFIER_NODE))
18256 {
18257 tree t = OMP_CLAUSE_DECL (nc);
18258 tree v = t;
18259 while (v)
18260 switch (TREE_CODE (v))
18261 {
18262 case COMPONENT_REF:
18263 case MEM_REF:
18264 case INDIRECT_REF:
18265 CASE_CONVERT:
18266 case POINTER_PLUS_EXPR:
18267 v = TREE_OPERAND (v, 0);
18268 continue;
18269 case PARM_DECL:
18270 if (DECL_CONTEXT (v) == current_function_decl
18271 && DECL_ARTIFICIAL (v)
18272 && DECL_NAME (v) == this_identifier)
18273 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
18274 /* FALLTHRU */
18275 default:
18276 v = NULL_TREE;
18277 break;
18278 }
18279 }
18280 else if (VAR_P (OMP_CLAUSE_DECL (oc))
18281 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
18282 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
18283 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
18284 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
18285 {
18286 tree decl = OMP_CLAUSE_DECL (nc);
18287 if (VAR_P (decl))
18288 {
18289 retrofit_lang_decl (decl);
18290 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
18291 }
18292 }
18293 break;
18294 default:
18295 break;
18296 }
18297 }
18298
18299 new_clauses = nreverse (new_clauses);
18300 if (ort != C_ORT_OMP_DECLARE_SIMD)
18301 {
18302 new_clauses = finish_omp_clauses (new_clauses, ort);
18303 if (linear_no_step)
18304 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
18305 if (nc == linear_no_step)
18306 {
18307 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
18308 break;
18309 }
18310 }
18311 return new_clauses;
18312 }
18313
18314 /* Like tsubst_copy, but specifically for OpenMP context selectors. */
18315 static tree
18316 tsubst_omp_context_selector (tree ctx, tree args, tsubst_flags_t complain,
18317 tree in_decl)
18318 {
18319 tree new_ctx = NULL_TREE;
18320 for (tree set = ctx; set; set = TREE_CHAIN (set))
18321 {
18322 tree selectors = NULL_TREE;
18323 for (tree sel = OMP_TSS_TRAIT_SELECTORS (set); sel;
18324 sel = TREE_CHAIN (sel))
18325 {
18326 enum omp_ts_code code = OMP_TS_CODE (sel);
18327 tree properties = NULL_TREE;
18328 tree score = OMP_TS_SCORE (sel);
18329 tree t;
18330
18331 if (score)
18332 {
18333 score = tsubst_expr (score, args, complain, in_decl);
18334 score = fold_non_dependent_expr (score);
18335 if (!value_dependent_expression_p (score)
18336 && !type_dependent_expression_p (score))
18337 {
18338 if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
18339 || TREE_CODE (score) != INTEGER_CST)
18340 {
18341 error_at (cp_expr_loc_or_input_loc (score),
18342 "%<score%> argument must "
18343 "be constant integer expression");
18344 score = NULL_TREE;
18345 }
18346 else if (tree_int_cst_sgn (score) < 0)
18347 {
18348 error_at (cp_expr_loc_or_input_loc (score),
18349 "%<score%> argument must be non-negative");
18350 score = NULL_TREE;
18351 }
18352 }
18353 }
18354
18355 switch (omp_ts_map[OMP_TS_CODE (sel)].tp_type)
18356 {
18357 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
18358 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
18359 t = tsubst_expr (OMP_TP_VALUE (OMP_TS_PROPERTIES (sel)),
18360 args, complain, in_decl);
18361 t = fold_non_dependent_expr (t);
18362 if (!value_dependent_expression_p (t)
18363 && !type_dependent_expression_p (t)
18364 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
18365 error_at (cp_expr_loc_or_input_loc (t),
18366 "property must be integer expression");
18367 else
18368 properties = make_trait_property (NULL_TREE, t, NULL_TREE);
18369 break;
18370 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
18371 if (OMP_TS_CODE (sel) == OMP_TRAIT_CONSTRUCT_SIMD)
18372 properties = tsubst_omp_clauses (OMP_TS_PROPERTIES (sel),
18373 C_ORT_OMP_DECLARE_SIMD,
18374 args, complain, in_decl);
18375 break;
18376 default:
18377 /* Nothing to do here, just copy. */
18378 for (tree prop = OMP_TS_PROPERTIES (sel);
18379 prop; prop = TREE_CHAIN (prop))
18380 properties = make_trait_property (OMP_TP_NAME (prop),
18381 OMP_TP_VALUE (prop),
18382 properties);
18383 }
18384 selectors = make_trait_selector (code, score, properties, selectors);
18385 }
18386 new_ctx = make_trait_set_selector (OMP_TSS_CODE (set),
18387 nreverse (selectors),
18388 new_ctx);
18389 }
18390 return nreverse (new_ctx);
18391 }
18392
18393
18394 /* Like tsubst_expr, but unshare TREE_LIST nodes. */
18395
18396 static tree
18397 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
18398 tree in_decl)
18399 {
18400 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
18401
18402 tree purpose, value, chain;
18403
18404 if (t == NULL)
18405 return t;
18406
18407 if (TREE_CODE (t) != TREE_LIST)
18408 return tsubst_expr (t, args, complain, in_decl);
18409
18410 if (t == void_list_node)
18411 return t;
18412
18413 purpose = TREE_PURPOSE (t);
18414 if (purpose)
18415 purpose = RECUR (purpose);
18416 value = TREE_VALUE (t);
18417 if (value)
18418 {
18419 if (TREE_CODE (value) != LABEL_DECL)
18420 value = RECUR (value);
18421 else
18422 {
18423 value = lookup_label (DECL_NAME (value));
18424 gcc_assert (TREE_CODE (value) == LABEL_DECL);
18425 TREE_USED (value) = 1;
18426 }
18427 }
18428 chain = TREE_CHAIN (t);
18429 if (chain && chain != void_type_node)
18430 chain = RECUR (chain);
18431 return tree_cons (purpose, value, chain);
18432 #undef RECUR
18433 }
18434
18435 /* Used to temporarily communicate the list of #pragma omp parallel
18436 clauses to #pragma omp for instantiation if they are combined
18437 together. */
18438
18439 static tree *omp_parallel_combined_clauses;
18440
18441 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
18442 cp_decomp *);
18443
18444 /* Substitute one OMP_FOR iterator. */
18445
18446 static bool
18447 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
18448 tree initv, tree condv, tree incrv, tree *clauses,
18449 tree args, tsubst_flags_t complain, tree in_decl)
18450 {
18451 #define RECUR(NODE) \
18452 tsubst_stmt ((NODE), args, complain, in_decl)
18453 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
18454 bool ret = false;
18455
18456 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
18457 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
18458
18459 decl = TREE_OPERAND (init, 0);
18460 init = TREE_OPERAND (init, 1);
18461 tree decl_expr = NULL_TREE;
18462 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
18463 if (range_for)
18464 {
18465 bool decomp = false;
18466 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
18467 {
18468 tree v = DECL_VALUE_EXPR (decl);
18469 if (TREE_CODE (v) == ARRAY_REF
18470 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
18471 {
18472 cp_decomp decomp_d = { NULL_TREE, 0 };
18473 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
18474 maybe_push_decl (d);
18475 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
18476 in_decl, &decomp_d);
18477 decomp = true;
18478 if (d == error_mark_node)
18479 decl = error_mark_node;
18480 else
18481 for (unsigned int i = 0; i < decomp_d.count; i++)
18482 {
18483 if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
18484 {
18485 tree v = build_nt (ARRAY_REF, d,
18486 size_int (decomp_d.count - i - 1),
18487 NULL_TREE, NULL_TREE);
18488 SET_DECL_VALUE_EXPR (decomp_d.decl, v);
18489 DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
18490 }
18491 fit_decomposition_lang_decl (decomp_d.decl, d);
18492 decomp_d.decl = DECL_CHAIN (decomp_d.decl);
18493 }
18494 }
18495 }
18496 decl = tsubst_decl (decl, args, complain);
18497 if (!decomp)
18498 maybe_push_decl (decl);
18499 }
18500 else if (init && TREE_CODE (init) == DECL_EXPR)
18501 {
18502 /* We need to jump through some hoops to handle declarations in the
18503 init-statement, since we might need to handle auto deduction,
18504 but we need to keep control of initialization. */
18505 decl_expr = init;
18506 init = DECL_INITIAL (DECL_EXPR_DECL (init));
18507 decl = tsubst_decl (decl, args, complain);
18508 }
18509 else
18510 {
18511 if (TREE_CODE (decl) == SCOPE_REF)
18512 {
18513 decl = RECUR (decl);
18514 if (TREE_CODE (decl) == COMPONENT_REF)
18515 {
18516 tree v = decl;
18517 while (v)
18518 switch (TREE_CODE (v))
18519 {
18520 case COMPONENT_REF:
18521 case MEM_REF:
18522 case INDIRECT_REF:
18523 CASE_CONVERT:
18524 case POINTER_PLUS_EXPR:
18525 v = TREE_OPERAND (v, 0);
18526 continue;
18527 case PARM_DECL:
18528 if (DECL_CONTEXT (v) == current_function_decl
18529 && DECL_ARTIFICIAL (v)
18530 && DECL_NAME (v) == this_identifier)
18531 {
18532 decl = TREE_OPERAND (decl, 1);
18533 decl = omp_privatize_field (decl, false);
18534 }
18535 /* FALLTHRU */
18536 default:
18537 v = NULL_TREE;
18538 break;
18539 }
18540 }
18541 }
18542 else
18543 decl = RECUR (decl);
18544 }
18545 if (init && TREE_CODE (init) == TREE_VEC)
18546 {
18547 init = copy_node (init);
18548 TREE_VEC_ELT (init, 0)
18549 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
18550 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
18551 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
18552 }
18553 else
18554 init = RECUR (init);
18555
18556 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
18557 {
18558 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
18559 if (TREE_CODE (o) == TREE_LIST)
18560 TREE_VEC_ELT (orig_declv, i)
18561 = tree_cons (RECUR (TREE_PURPOSE (o)),
18562 RECUR (TREE_VALUE (o)),
18563 NULL_TREE);
18564 else
18565 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
18566 }
18567
18568 if (range_for)
18569 {
18570 tree this_pre_body = NULL_TREE;
18571 tree orig_init = NULL_TREE;
18572 tree orig_decl = NULL_TREE;
18573 tree init_sl = NULL_TREE;
18574 cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
18575 orig_init, cond, incr, true);
18576 if (orig_decl)
18577 {
18578 if (orig_declv == NULL_TREE)
18579 orig_declv = copy_node (declv);
18580 TREE_VEC_ELT (orig_declv, i) = orig_decl;
18581 ret = true;
18582 }
18583 else if (orig_declv)
18584 TREE_VEC_ELT (orig_declv, i) = decl;
18585 }
18586
18587 tree auto_node = type_uses_auto (TREE_TYPE (decl));
18588 if (!range_for && auto_node && init)
18589 TREE_TYPE (decl)
18590 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
18591
18592 gcc_assert (!type_dependent_expression_p (decl));
18593
18594 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
18595 {
18596 if (decl_expr)
18597 {
18598 /* Declare the variable, but don't let that initialize it. */
18599 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
18600 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
18601 RECUR (decl_expr);
18602 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
18603 }
18604
18605 if (!range_for)
18606 {
18607 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18608 if (COMPARISON_CLASS_P (cond)
18609 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
18610 {
18611 tree lhs = RECUR (TREE_OPERAND (cond, 0));
18612 tree rhs = copy_node (TREE_OPERAND (cond, 1));
18613 TREE_VEC_ELT (rhs, 0)
18614 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
18615 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
18616 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
18617 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
18618 lhs, rhs);
18619 }
18620 else
18621 cond = RECUR (cond);
18622 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18623 if (TREE_CODE (incr) == MODIFY_EXPR)
18624 {
18625 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18626 tree rhs = RECUR (TREE_OPERAND (incr, 1));
18627 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
18628 NOP_EXPR, rhs, NULL_TREE, complain);
18629 }
18630 else
18631 incr = RECUR (incr);
18632 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18633 TREE_VEC_ELT (orig_declv, i) = decl;
18634 }
18635 TREE_VEC_ELT (declv, i) = decl;
18636 TREE_VEC_ELT (initv, i) = init;
18637 TREE_VEC_ELT (condv, i) = cond;
18638 TREE_VEC_ELT (incrv, i) = incr;
18639 return ret;
18640 }
18641
18642 if (decl_expr)
18643 {
18644 /* Declare and initialize the variable. */
18645 RECUR (decl_expr);
18646 init = NULL_TREE;
18647 }
18648 else if (init)
18649 {
18650 tree *pc;
18651 int j;
18652 for (j = ((omp_parallel_combined_clauses == NULL
18653 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
18654 {
18655 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
18656 {
18657 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
18658 && OMP_CLAUSE_DECL (*pc) == decl)
18659 break;
18660 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
18661 && OMP_CLAUSE_DECL (*pc) == decl)
18662 {
18663 if (j)
18664 break;
18665 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18666 tree c = *pc;
18667 *pc = OMP_CLAUSE_CHAIN (c);
18668 OMP_CLAUSE_CHAIN (c) = *clauses;
18669 *clauses = c;
18670 }
18671 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
18672 && OMP_CLAUSE_DECL (*pc) == decl)
18673 {
18674 error ("iteration variable %qD should not be firstprivate",
18675 decl);
18676 *pc = OMP_CLAUSE_CHAIN (*pc);
18677 }
18678 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18679 && OMP_CLAUSE_DECL (*pc) == decl)
18680 {
18681 error ("iteration variable %qD should not be reduction",
18682 decl);
18683 *pc = OMP_CLAUSE_CHAIN (*pc);
18684 }
18685 else
18686 pc = &OMP_CLAUSE_CHAIN (*pc);
18687 }
18688 if (*pc)
18689 break;
18690 }
18691 if (*pc == NULL_TREE)
18692 {
18693 tree c = build_omp_clause (input_location,
18694 TREE_CODE (t) == OMP_LOOP
18695 ? OMP_CLAUSE_LASTPRIVATE
18696 : OMP_CLAUSE_PRIVATE);
18697 OMP_CLAUSE_DECL (c) = decl;
18698 c = finish_omp_clauses (c, C_ORT_OMP);
18699 if (c)
18700 {
18701 OMP_CLAUSE_CHAIN (c) = *clauses;
18702 *clauses = c;
18703 }
18704 }
18705 }
18706 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18707 if (COMPARISON_CLASS_P (cond))
18708 {
18709 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18710 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18711 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18712 }
18713 else
18714 cond = RECUR (cond);
18715 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18716 switch (TREE_CODE (incr))
18717 {
18718 case PREINCREMENT_EXPR:
18719 case PREDECREMENT_EXPR:
18720 case POSTINCREMENT_EXPR:
18721 case POSTDECREMENT_EXPR:
18722 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18723 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18724 break;
18725 case MODIFY_EXPR:
18726 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18727 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18728 {
18729 tree rhs = TREE_OPERAND (incr, 1);
18730 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18731 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18732 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18733 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18734 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18735 rhs0, rhs1));
18736 }
18737 else
18738 incr = RECUR (incr);
18739 break;
18740 case MODOP_EXPR:
18741 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18742 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18743 {
18744 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18745 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18746 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18747 TREE_TYPE (decl), lhs,
18748 RECUR (TREE_OPERAND (incr, 2))));
18749 }
18750 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18751 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18752 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18753 {
18754 tree rhs = TREE_OPERAND (incr, 2);
18755 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18756 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18757 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18758 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18759 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18760 rhs0, rhs1));
18761 }
18762 else
18763 incr = RECUR (incr);
18764 break;
18765 default:
18766 incr = RECUR (incr);
18767 break;
18768 }
18769
18770 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18771 TREE_VEC_ELT (orig_declv, i) = decl;
18772 TREE_VEC_ELT (declv, i) = decl;
18773 TREE_VEC_ELT (initv, i) = init;
18774 TREE_VEC_ELT (condv, i) = cond;
18775 TREE_VEC_ELT (incrv, i) = incr;
18776 return false;
18777 #undef RECUR
18778 }
18779
18780 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18781 of OMP_TARGET's body. */
18782
18783 static tree
18784 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18785 {
18786 *walk_subtrees = 0;
18787 switch (TREE_CODE (*tp))
18788 {
18789 case OMP_TEAMS:
18790 return *tp;
18791 case BIND_EXPR:
18792 case STATEMENT_LIST:
18793 *walk_subtrees = 1;
18794 break;
18795 default:
18796 break;
18797 }
18798 return NULL_TREE;
18799 }
18800
18801 /* Helper function for tsubst_expr. For decomposition declaration
18802 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18803 also the corresponding decls representing the identifiers
18804 of the decomposition declaration. Return DECL if successful
18805 or error_mark_node otherwise, set *FIRST to the first decl
18806 in the list chained through DECL_CHAIN and *CNT to the number
18807 of such decls. */
18808
18809 static tree
18810 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18811 tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
18812 {
18813 tree decl2, decl3, prev = decl;
18814 decomp->count = 0;
18815 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18816 for (decl2 = DECL_CHAIN (pattern_decl);
18817 decl2
18818 && DECL_DECOMPOSITION_P (decl2)
18819 && DECL_NAME (decl2);
18820 decl2 = DECL_CHAIN (decl2))
18821 {
18822 if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
18823 {
18824 gcc_assert (errorcount);
18825 return error_mark_node;
18826 }
18827 decomp->count++;
18828 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18829 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18830 tree v = DECL_VALUE_EXPR (decl2);
18831 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18832 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18833 decl3 = tsubst (decl2, args, complain, in_decl);
18834 SET_DECL_VALUE_EXPR (decl2, v);
18835 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18836 if (VAR_P (decl3))
18837 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18838 else
18839 {
18840 gcc_assert (errorcount);
18841 decl = error_mark_node;
18842 continue;
18843 }
18844 maybe_push_decl (decl3);
18845 if (error_operand_p (decl3))
18846 decl = error_mark_node;
18847 else if (decl != error_mark_node
18848 && DECL_CHAIN (decl3) != prev
18849 && decl != prev)
18850 {
18851 gcc_assert (errorcount);
18852 decl = error_mark_node;
18853 }
18854 else
18855 prev = decl3;
18856 }
18857 decomp->decl = prev;
18858 return decl;
18859 }
18860
18861 /* Return the proper local_specialization for init-capture pack DECL. */
18862
18863 static tree
18864 lookup_init_capture_pack (tree decl)
18865 {
18866 /* We handle normal pack captures by forwarding to the specialization of the
18867 captured parameter. We can't do that for pack init-captures; we need them
18868 to have their own local_specialization. We created the individual
18869 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18870 when we process the DECL_EXPR for the pack init-capture in the template.
18871 So, how do we find them? We don't know the capture proxy pack when
18872 building the individual resulting proxies, and we don't know the
18873 individual proxies when instantiating the pack. What we have in common is
18874 the FIELD_DECL.
18875
18876 So...when we instantiate the FIELD_DECL, we stick the result in
18877 local_specializations. Then at the DECL_EXPR we look up that result, see
18878 how many elements it has, synthesize the names, and look them up. */
18879
18880 tree cname = DECL_NAME (decl);
18881 tree val = DECL_VALUE_EXPR (decl);
18882 tree field = TREE_OPERAND (val, 1);
18883 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18884 tree fpack = retrieve_local_specialization (field);
18885 if (fpack == error_mark_node)
18886 return error_mark_node;
18887
18888 int len = 1;
18889 tree vec = NULL_TREE;
18890 tree r = NULL_TREE;
18891 if (TREE_CODE (fpack) == TREE_VEC)
18892 {
18893 len = TREE_VEC_LENGTH (fpack);
18894 vec = make_tree_vec (len);
18895 r = make_node (NONTYPE_ARGUMENT_PACK);
18896 ARGUMENT_PACK_ARGS (r) = vec;
18897 }
18898 for (int i = 0; i < len; ++i)
18899 {
18900 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18901 tree elt = lookup_name (ename);
18902 if (vec)
18903 TREE_VEC_ELT (vec, i) = elt;
18904 else
18905 r = elt;
18906 }
18907 return r;
18908 }
18909
18910 /* T is an operand of a template tree being substituted. Return whether
18911 T is dependent such that we should suppress some warnings that would
18912 make sense if the substituted expression were written directly, like
18913 template <int I> bool f() { return I == 2; }
18914 We don't want to warn when instantiating f that comparing two constants
18915 always has the same value.
18916
18917 This is a more limited concept of dependence than instantiation-dependent;
18918 here we don't care whether substitution could fail. */
18919
18920 static bool
18921 dependent_operand_p (tree t)
18922 {
18923 while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
18924 t = TREE_OPERAND (t, 0);
18925
18926 ++processing_template_decl;
18927 bool r = (potential_constant_expression (t)
18928 ? value_dependent_expression_p (t)
18929 : type_dependent_expression_p (t));
18930 --processing_template_decl;
18931 return r;
18932 }
18933
18934 /* A superset of tsubst_expr that also handles statement trees. */
18935
18936 static tree
18937 tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18938 {
18939 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18940 #define RECUR(NODE) \
18941 tsubst_stmt ((NODE), args, complain, in_decl)
18942
18943 tree stmt, tmp;
18944 tree r;
18945 location_t loc;
18946
18947 if (t == NULL_TREE || t == error_mark_node)
18948 return t;
18949
18950 loc = input_location;
18951 if (location_t eloc = cp_expr_location (t))
18952 input_location = eloc;
18953 if (STATEMENT_CODE_P (TREE_CODE (t)))
18954 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18955
18956 switch (TREE_CODE (t))
18957 {
18958 case STATEMENT_LIST:
18959 {
18960 for (tree stmt : tsi_range (t))
18961 RECUR (stmt);
18962 break;
18963 }
18964
18965 case CTOR_INITIALIZER:
18966 finish_mem_initializers (tsubst_initializer_list
18967 (TREE_OPERAND (t, 0), args));
18968 break;
18969
18970 case RETURN_EXPR:
18971 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18972 break;
18973
18974 case CO_RETURN_EXPR:
18975 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18976 break;
18977
18978 case EXPR_STMT:
18979 tmp = RECUR (EXPR_STMT_EXPR (t));
18980 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18981 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18982 else
18983 finish_expr_stmt (tmp);
18984 break;
18985
18986 case USING_STMT:
18987 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18988 break;
18989
18990 case PRECONDITION_STMT:
18991 case POSTCONDITION_STMT:
18992 gcc_unreachable ();
18993
18994 case ASSERTION_STMT:
18995 {
18996 r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
18997 if (r != error_mark_node)
18998 add_stmt (r);
18999 RETURN (r);
19000 }
19001 break;
19002
19003 case DECL_EXPR:
19004 {
19005 tree decl, pattern_decl;
19006 tree init;
19007
19008 pattern_decl = decl = DECL_EXPR_DECL (t);
19009 if (TREE_CODE (decl) == LABEL_DECL)
19010 finish_label_decl (DECL_NAME (decl));
19011 else if (TREE_CODE (decl) == USING_DECL)
19012 {
19013 tree scope = USING_DECL_SCOPE (decl);
19014 if (DECL_DEPENDENT_P (decl))
19015 {
19016 scope = tsubst (scope, args, complain, in_decl);
19017 if (!MAYBE_CLASS_TYPE_P (scope)
19018 && TREE_CODE (scope) != ENUMERAL_TYPE)
19019 {
19020 if (complain & tf_error)
19021 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
19022 "class, namespace, or enumeration", scope);
19023 return error_mark_node;
19024 }
19025 finish_nonmember_using_decl (scope, DECL_NAME (decl));
19026 }
19027 else
19028 {
19029 /* This is a non-dependent using-decl, and we'll have
19030 used the names it found during template parsing. We do
19031 not want to do the lookup again, because we might not
19032 find the things we found then. */
19033 gcc_checking_assert (scope == tsubst (scope, args,
19034 complain, in_decl));
19035 /* We still need to push the bindings so that we can look up
19036 this name later. */
19037 push_using_decl_bindings (DECL_NAME (decl),
19038 USING_DECL_DECLS (decl));
19039 }
19040 }
19041 else if (is_capture_proxy (decl)
19042 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
19043 {
19044 /* We're in tsubst_lambda_expr, we've already inserted a new
19045 capture proxy, so look it up and register it. */
19046 tree inst;
19047 if (!DECL_PACK_P (decl))
19048 {
19049 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
19050 LOOK_want::HIDDEN_LAMBDA);
19051 gcc_assert (inst != decl && is_capture_proxy (inst));
19052 }
19053 else if (is_normal_capture_proxy (decl))
19054 {
19055 inst = (retrieve_local_specialization
19056 (DECL_CAPTURED_VARIABLE (decl)));
19057 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
19058 || DECL_PACK_P (inst));
19059 }
19060 else
19061 inst = lookup_init_capture_pack (decl);
19062
19063 register_local_specialization (inst, decl);
19064 break;
19065 }
19066 else if (DECL_PRETTY_FUNCTION_P (decl))
19067 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
19068 DECL_NAME (decl),
19069 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
19070 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
19071 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
19072 /* Don't copy the old closure; we'll create a new one in
19073 tsubst_lambda_expr. */
19074 break;
19075 else
19076 {
19077 init = DECL_INITIAL (decl);
19078 decl = tsubst (decl, args, complain, in_decl);
19079 if (decl != error_mark_node)
19080 {
19081 /* By marking the declaration as instantiated, we avoid
19082 trying to instantiate it. Since instantiate_decl can't
19083 handle local variables, and since we've already done
19084 all that needs to be done, that's the right thing to
19085 do. */
19086 if (VAR_P (decl))
19087 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
19088 if (VAR_P (decl) && !DECL_NAME (decl)
19089 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
19090 /* Anonymous aggregates are a special case. */
19091 finish_anon_union (decl);
19092 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
19093 {
19094 DECL_CONTEXT (decl) = current_function_decl;
19095 insert_capture_proxy (decl);
19096 }
19097 else if (DECL_IMPLICIT_TYPEDEF_P (t))
19098 /* We already did a pushtag. */;
19099 else if (VAR_OR_FUNCTION_DECL_P (decl)
19100 && DECL_LOCAL_DECL_P (decl))
19101 {
19102 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19103 DECL_CONTEXT (decl) = NULL_TREE;
19104 decl = pushdecl (decl);
19105 if (TREE_CODE (decl) == FUNCTION_DECL
19106 && DECL_OMP_DECLARE_REDUCTION_P (decl)
19107 && cp_check_omp_declare_reduction (decl))
19108 instantiate_body (pattern_decl, args, decl, true);
19109 }
19110 else
19111 {
19112 bool const_init = false;
19113 cp_decomp decomp_d, *decomp = NULL;
19114 tree asmspec_tree = NULL_TREE;
19115 maybe_push_decl (decl);
19116
19117 if (VAR_P (decl)
19118 && DECL_LANG_SPECIFIC (decl)
19119 && DECL_OMP_PRIVATIZED_MEMBER (decl))
19120 break;
19121
19122 if (DECL_DECOMPOSITION_P (decl)
19123 && TREE_TYPE (pattern_decl) != error_mark_node)
19124 {
19125 decomp = &decomp_d;
19126 if (tsubst_decomp_names (decl, pattern_decl, args,
19127 complain, in_decl, decomp)
19128 == error_mark_node)
19129 decomp = NULL;
19130 }
19131
19132 init = tsubst_init (init, decl, args, complain, in_decl);
19133
19134 if (VAR_P (decl))
19135 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
19136 (pattern_decl));
19137
19138 /* In a non-template function, VLA type declarations are
19139 handled in grokdeclarator; for templates, handle them
19140 now. */
19141 predeclare_vla (decl);
19142
19143 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
19144 {
19145 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
19146 const char *asmspec = IDENTIFIER_POINTER (id);
19147 gcc_assert (asmspec[0] == '*');
19148 asmspec_tree
19149 = build_string (IDENTIFIER_LENGTH (id) - 1,
19150 asmspec + 1);
19151 TREE_TYPE (asmspec_tree) = char_array_type_node;
19152 }
19153
19154 cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
19155 decomp);
19156 }
19157 }
19158 }
19159
19160 break;
19161 }
19162
19163 case FOR_STMT:
19164 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
19165 RECUR (FOR_INIT_STMT (t));
19166 finish_init_stmt (stmt);
19167 tmp = RECUR (FOR_COND (t));
19168 finish_for_cond (tmp, stmt, false, 0, false);
19169 tmp = RECUR (FOR_EXPR (t));
19170 finish_for_expr (tmp, stmt);
19171 {
19172 bool prev = note_iteration_stmt_body_start ();
19173 RECUR (FOR_BODY (t));
19174 note_iteration_stmt_body_end (prev);
19175 }
19176 finish_for_stmt (stmt);
19177 break;
19178
19179 case RANGE_FOR_STMT:
19180 {
19181 /* Construct another range_for, if this is not a final
19182 substitution (for inside a generic lambda of a
19183 template). Otherwise convert to a regular for. */
19184 tree decl, expr;
19185 stmt = (processing_template_decl
19186 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
19187 : begin_for_stmt (NULL_TREE, NULL_TREE));
19188 RECUR (RANGE_FOR_INIT_STMT (t));
19189 decl = RANGE_FOR_DECL (t);
19190 decl = tsubst (decl, args, complain, in_decl);
19191 maybe_push_decl (decl);
19192 expr = RECUR (RANGE_FOR_EXPR (t));
19193
19194 cp_decomp decomp_d, *decomp = NULL;
19195 if (DECL_DECOMPOSITION_P (decl))
19196 {
19197 decomp = &decomp_d;
19198 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
19199 complain, in_decl, decomp);
19200 }
19201
19202 tree unroll = RECUR (RANGE_FOR_UNROLL (t));
19203 if (unroll)
19204 unroll
19205 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
19206 unroll);
19207 if (processing_template_decl)
19208 {
19209 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
19210 RANGE_FOR_UNROLL (stmt) = unroll;
19211 RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
19212 finish_range_for_decl (stmt, decl, expr);
19213 if (decomp && decl != error_mark_node)
19214 cp_finish_decomp (decl, decomp);
19215 }
19216 else
19217 stmt = cp_convert_range_for (stmt, decl, expr, decomp,
19218 RANGE_FOR_IVDEP (t), unroll,
19219 RANGE_FOR_NOVECTOR (t));
19220
19221 bool prev = note_iteration_stmt_body_start ();
19222 RECUR (RANGE_FOR_BODY (t));
19223 note_iteration_stmt_body_end (prev);
19224 finish_for_stmt (stmt);
19225 }
19226 break;
19227
19228 case WHILE_STMT:
19229 stmt = begin_while_stmt ();
19230 tmp = RECUR (WHILE_COND (t));
19231 finish_while_stmt_cond (tmp, stmt, false, 0, false);
19232 {
19233 bool prev = note_iteration_stmt_body_start ();
19234 RECUR (WHILE_BODY (t));
19235 note_iteration_stmt_body_end (prev);
19236 }
19237 finish_while_stmt (stmt);
19238 break;
19239
19240 case DO_STMT:
19241 stmt = begin_do_stmt ();
19242 {
19243 bool prev = note_iteration_stmt_body_start ();
19244 RECUR (DO_BODY (t));
19245 note_iteration_stmt_body_end (prev);
19246 }
19247 finish_do_body (stmt);
19248 tmp = RECUR (DO_COND (t));
19249 finish_do_stmt (tmp, stmt, false, 0, false);
19250 break;
19251
19252 case IF_STMT:
19253 stmt = begin_if_stmt ();
19254 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
19255 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
19256 if (IF_STMT_CONSTEXPR_P (t))
19257 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
19258 {
19259 tree cond = IF_COND (t);
19260 bool was_dep = dependent_operand_p (cond);
19261 cond = RECUR (cond);
19262 warning_sentinel s1(warn_address, was_dep);
19263 tmp = finish_if_stmt_cond (cond, stmt);
19264 }
19265 if (IF_STMT_CONSTEXPR_P (t)
19266 && instantiation_dependent_expression_p (tmp))
19267 {
19268 /* We're partially instantiating a generic lambda, but the condition
19269 of the constexpr if is still dependent. Don't substitute into the
19270 branches now, just remember the template arguments. */
19271 do_poplevel (IF_SCOPE (stmt));
19272 IF_SCOPE (stmt) = NULL_TREE;
19273 IF_COND (stmt) = IF_COND (t);
19274 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
19275 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
19276 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
19277 add_stmt (stmt);
19278 break;
19279 }
19280 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
19281 /* Don't instantiate the THEN_CLAUSE. */;
19282 else if (IF_STMT_CONSTEVAL_P (t))
19283 {
19284 bool save_in_consteval_if_p = in_consteval_if_p;
19285 in_consteval_if_p = true;
19286 RECUR (THEN_CLAUSE (t));
19287 in_consteval_if_p = save_in_consteval_if_p;
19288 }
19289 else
19290 {
19291 tree folded = fold_non_dependent_expr (tmp, complain);
19292 bool inhibit = integer_zerop (folded);
19293 if (inhibit)
19294 ++c_inhibit_evaluation_warnings;
19295 RECUR (THEN_CLAUSE (t));
19296 if (inhibit)
19297 --c_inhibit_evaluation_warnings;
19298 }
19299 finish_then_clause (stmt);
19300
19301 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
19302 /* Don't instantiate the ELSE_CLAUSE. */;
19303 else if (ELSE_CLAUSE (t))
19304 {
19305 tree folded = fold_non_dependent_expr (tmp, complain);
19306 bool inhibit = integer_nonzerop (folded);
19307 begin_else_clause (stmt);
19308 if (inhibit)
19309 ++c_inhibit_evaluation_warnings;
19310 RECUR (ELSE_CLAUSE (t));
19311 if (inhibit)
19312 --c_inhibit_evaluation_warnings;
19313 finish_else_clause (stmt);
19314 }
19315
19316 finish_if_stmt (stmt);
19317 break;
19318
19319 case BIND_EXPR:
19320 if (BIND_EXPR_BODY_BLOCK (t))
19321 stmt = begin_function_body ();
19322 else
19323 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
19324 ? BCS_TRY_BLOCK : 0);
19325
19326 RECUR (BIND_EXPR_BODY (t));
19327
19328 if (BIND_EXPR_BODY_BLOCK (t))
19329 finish_function_body (stmt);
19330 else
19331 finish_compound_stmt (stmt);
19332 break;
19333
19334 case BREAK_STMT:
19335 finish_break_stmt ();
19336 break;
19337
19338 case CONTINUE_STMT:
19339 finish_continue_stmt ();
19340 break;
19341
19342 case SWITCH_STMT:
19343 stmt = begin_switch_stmt ();
19344 tmp = RECUR (SWITCH_STMT_COND (t));
19345 finish_switch_cond (tmp, stmt);
19346 RECUR (SWITCH_STMT_BODY (t));
19347 finish_switch_stmt (stmt);
19348 break;
19349
19350 case CASE_LABEL_EXPR:
19351 {
19352 tree decl = CASE_LABEL (t);
19353 tree low = RECUR (CASE_LOW (t));
19354 tree high = RECUR (CASE_HIGH (t));
19355 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
19356 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
19357 {
19358 tree label = CASE_LABEL (l);
19359 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
19360 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
19361 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
19362 }
19363 }
19364 break;
19365
19366 case LABEL_EXPR:
19367 {
19368 tree decl = LABEL_EXPR_LABEL (t);
19369 tree label = finish_label_stmt (DECL_NAME (decl));
19370 if (TREE_CODE (label) == LABEL_DECL)
19371 {
19372 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
19373 copy_warning (label, decl);
19374 }
19375 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
19376 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
19377 }
19378 break;
19379
19380 case GOTO_EXPR:
19381 tmp = GOTO_DESTINATION (t);
19382 if (TREE_CODE (tmp) != LABEL_DECL)
19383 /* Computed goto's must be tsubst'd into. On the other hand,
19384 non-computed gotos must not be; the identifier in question
19385 will have no binding. */
19386 tmp = RECUR (tmp);
19387 else
19388 tmp = DECL_NAME (tmp);
19389 finish_goto_stmt (tmp);
19390 break;
19391
19392 case ASM_EXPR:
19393 {
19394 tree string = RECUR (ASM_STRING (t));
19395 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
19396 complain, in_decl);
19397 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
19398 complain, in_decl);
19399 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
19400 complain, in_decl);
19401 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
19402 complain, in_decl);
19403 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
19404 outputs, inputs, clobbers, labels,
19405 ASM_INLINE_P (t), false);
19406 tree asm_expr = tmp;
19407 if (asm_expr != error_mark_node)
19408 {
19409 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
19410 asm_expr = TREE_OPERAND (asm_expr, 0);
19411 ASM_BASIC_P (asm_expr) = ASM_BASIC_P (t);
19412 }
19413 }
19414 break;
19415
19416 case TRY_BLOCK:
19417 if (CLEANUP_P (t))
19418 {
19419 stmt = begin_try_block ();
19420 RECUR (TRY_STMTS (t));
19421 finish_cleanup_try_block (stmt);
19422 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
19423 }
19424 else
19425 {
19426 tree compound_stmt = NULL_TREE;
19427
19428 if (FN_TRY_BLOCK_P (t))
19429 stmt = begin_function_try_block (&compound_stmt);
19430 else
19431 stmt = begin_try_block ();
19432
19433 RECUR (TRY_STMTS (t));
19434
19435 if (FN_TRY_BLOCK_P (t))
19436 finish_function_try_block (stmt);
19437 else
19438 finish_try_block (stmt);
19439
19440 RECUR (TRY_HANDLERS (t));
19441 if (FN_TRY_BLOCK_P (t))
19442 finish_function_handler_sequence (stmt, compound_stmt);
19443 else
19444 finish_handler_sequence (stmt);
19445 }
19446 break;
19447
19448 case HANDLER:
19449 {
19450 tree decl = HANDLER_PARMS (t);
19451
19452 if (decl)
19453 {
19454 decl = tsubst (decl, args, complain, in_decl);
19455 /* Prevent instantiate_decl from trying to instantiate
19456 this variable. We've already done all that needs to be
19457 done. */
19458 if (decl != error_mark_node)
19459 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
19460 }
19461 stmt = begin_handler ();
19462 finish_handler_parms (decl, stmt);
19463 RECUR (HANDLER_BODY (t));
19464 finish_handler (stmt);
19465 }
19466 break;
19467
19468 case TAG_DEFN:
19469 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
19470 if (dependent_type_p (tmp))
19471 /* This is a partial instantiation, try again when full. */
19472 add_stmt (build_min (TAG_DEFN, tmp));
19473 else if (CLASS_TYPE_P (tmp))
19474 {
19475 /* Local classes are not independent templates; they are
19476 instantiated along with their containing function. And this
19477 way we don't have to deal with pushing out of one local class
19478 to instantiate a member of another local class. */
19479 /* Closures are handled by the LAMBDA_EXPR. */
19480 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
19481 complete_type (tmp);
19482 tree save_ccp = current_class_ptr;
19483 tree save_ccr = current_class_ref;
19484 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
19485 if ((VAR_P (fld)
19486 || (TREE_CODE (fld) == FUNCTION_DECL
19487 && !DECL_ARTIFICIAL (fld)))
19488 && DECL_TEMPLATE_INSTANTIATION (fld))
19489 instantiate_decl (fld, /*defer_ok=*/false,
19490 /*expl_inst_class=*/false);
19491 else if (TREE_CODE (fld) == FIELD_DECL)
19492 maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
19493 current_class_ptr = save_ccp;
19494 current_class_ref = save_ccr;
19495 }
19496 break;
19497
19498 case STATIC_ASSERT:
19499 {
19500 tree condition, message;
19501
19502 ++c_inhibit_evaluation_warnings;
19503 condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
19504 complain, in_decl);
19505 message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
19506 complain, in_decl);
19507 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
19508 && TREE_CODE (message) == STRING_CST)
19509 message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
19510 PAREN_EXPR, TREE_TYPE (message), message);
19511 --c_inhibit_evaluation_warnings;
19512
19513 finish_static_assert (condition, message,
19514 STATIC_ASSERT_SOURCE_LOCATION (t),
19515 /*member_p=*/false, /*show_expr_p=*/true);
19516 }
19517 break;
19518
19519 case OACC_KERNELS:
19520 case OACC_PARALLEL:
19521 case OACC_SERIAL:
19522 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
19523 complain, in_decl);
19524 stmt = begin_omp_parallel ();
19525 RECUR (OMP_BODY (t));
19526 finish_omp_construct (TREE_CODE (t), stmt, tmp);
19527 break;
19528
19529 case OMP_PARALLEL:
19530 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
19531 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
19532 complain, in_decl);
19533 if (OMP_PARALLEL_COMBINED (t))
19534 omp_parallel_combined_clauses = &tmp;
19535 stmt = begin_omp_parallel ();
19536 RECUR (OMP_PARALLEL_BODY (t));
19537 gcc_assert (omp_parallel_combined_clauses == NULL);
19538 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
19539 = OMP_PARALLEL_COMBINED (t);
19540 pop_omp_privatization_clauses (r);
19541 break;
19542
19543 case OMP_TASK:
19544 if (OMP_TASK_BODY (t) == NULL_TREE)
19545 {
19546 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19547 complain, in_decl);
19548 t = copy_node (t);
19549 OMP_TASK_CLAUSES (t) = tmp;
19550 add_stmt (t);
19551 break;
19552 }
19553 r = push_omp_privatization_clauses (false);
19554 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19555 complain, in_decl);
19556 stmt = begin_omp_task ();
19557 RECUR (OMP_TASK_BODY (t));
19558 finish_omp_task (tmp, stmt);
19559 pop_omp_privatization_clauses (r);
19560 break;
19561
19562 case OMP_FOR:
19563 case OMP_LOOP:
19564 case OMP_SIMD:
19565 case OMP_DISTRIBUTE:
19566 case OMP_TASKLOOP:
19567 case OMP_TILE:
19568 case OMP_UNROLL:
19569 case OACC_LOOP:
19570 {
19571 tree clauses, body, pre_body;
19572 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
19573 tree orig_declv = NULL_TREE;
19574 tree incrv = NULL_TREE;
19575 enum c_omp_region_type ort = C_ORT_OMP;
19576 bool any_range_for = false;
19577 int i;
19578
19579 if (TREE_CODE (t) == OACC_LOOP)
19580 ort = C_ORT_ACC;
19581
19582 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
19583 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
19584 in_decl);
19585 if (OMP_FOR_INIT (t) != NULL_TREE)
19586 {
19587 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19588 if (OMP_FOR_ORIG_DECLS (t))
19589 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19590 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19591 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19592 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19593 }
19594
19595 keep_next_level (true);
19596 stmt = begin_omp_structured_block ();
19597
19598 pre_body = push_stmt_list ();
19599 RECUR (OMP_FOR_PRE_BODY (t));
19600 pre_body = pop_stmt_list (pre_body);
19601
19602 if (OMP_FOR_INIT (t) != NULL_TREE)
19603 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19604 {
19605 if (TREE_VEC_ELT (OMP_FOR_INIT (t), i))
19606 any_range_for
19607 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
19608 condv, incrv, &clauses, args,
19609 complain, in_decl);
19610 else
19611 {
19612 TREE_VEC_ELT (declv, i) = global_namespace;
19613 TREE_VEC_ELT (initv, i) = NULL_TREE;
19614 TREE_VEC_ELT (condv, i) = NULL_TREE;
19615 TREE_VEC_ELT (incrv, i) = NULL_TREE;
19616 if (orig_declv)
19617 TREE_VEC_ELT (orig_declv, i) = NULL_TREE;
19618 }
19619 }
19620 omp_parallel_combined_clauses = NULL;
19621
19622 if (any_range_for)
19623 {
19624 gcc_assert (orig_declv);
19625 body = begin_omp_structured_block ();
19626 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19627 if (TREE_VEC_ELT (declv, i) != global_namespace
19628 && TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
19629 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
19630 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
19631 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
19632 TREE_VEC_ELT (declv, i));
19633 }
19634 else
19635 body = push_stmt_list ();
19636 RECUR (OMP_FOR_BODY (t));
19637 if (any_range_for)
19638 body = finish_omp_structured_block (body);
19639 else
19640 body = pop_stmt_list (body);
19641
19642 if (OMP_FOR_INIT (t) != NULL_TREE)
19643 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
19644 orig_declv, initv, condv, incrv, body, pre_body,
19645 NULL, clauses);
19646 else
19647 {
19648 t = make_node (TREE_CODE (t));
19649 TREE_TYPE (t) = void_type_node;
19650 OMP_FOR_BODY (t) = body;
19651 OMP_FOR_PRE_BODY (t) = pre_body;
19652 OMP_FOR_CLAUSES (t) = clauses;
19653 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
19654 add_stmt (t);
19655 }
19656
19657 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
19658 t));
19659 pop_omp_privatization_clauses (r);
19660
19661 if (any_range_for && !processing_template_decl && t)
19662 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19663 if (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i)
19664 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t),
19665 i)) == TREE_LIST)
19666 {
19667 tree v = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
19668 if (TREE_CHAIN (v) == NULL_TREE
19669 || TREE_CODE (TREE_CHAIN (v)) != TREE_VEC)
19670 continue;
19671 v = TREE_VEC_ELT (TREE_CHAIN (v), 0);
19672 gcc_assert (VAR_P (v) && DECL_NAME (v) == NULL_TREE);
19673 DECL_NAME (v) = for_range_identifier;
19674 }
19675 }
19676 break;
19677
19678 case OMP_SECTIONS:
19679 case OMP_MASKED:
19680 omp_parallel_combined_clauses = NULL;
19681 /* FALLTHRU */
19682 case OMP_SINGLE:
19683 case OMP_SCOPE:
19684 case OMP_TEAMS:
19685 case OMP_CRITICAL:
19686 case OMP_TASKGROUP:
19687 case OMP_SCAN:
19688 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
19689 && OMP_TEAMS_COMBINED (t));
19690 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
19691 in_decl);
19692 if (TREE_CODE (t) == OMP_TEAMS)
19693 {
19694 keep_next_level (true);
19695 stmt = begin_omp_structured_block ();
19696 RECUR (OMP_BODY (t));
19697 stmt = finish_omp_structured_block (stmt);
19698 }
19699 else
19700 {
19701 stmt = push_stmt_list ();
19702 RECUR (OMP_BODY (t));
19703 stmt = pop_stmt_list (stmt);
19704 }
19705
19706 if (TREE_CODE (t) == OMP_CRITICAL
19707 && tmp != NULL_TREE
19708 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
19709 {
19710 error_at (OMP_CLAUSE_LOCATION (tmp),
19711 "%<#pragma omp critical%> with %<hint%> clause requires "
19712 "a name, except when %<omp_sync_hint_none%> is used");
19713 RETURN (error_mark_node);
19714 }
19715 t = copy_node (t);
19716 OMP_BODY (t) = stmt;
19717 OMP_CLAUSES (t) = tmp;
19718 add_stmt (t);
19719 pop_omp_privatization_clauses (r);
19720 break;
19721
19722 case OMP_DEPOBJ:
19723 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
19724 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
19725 {
19726 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
19727 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
19728 {
19729 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
19730 args, complain, in_decl);
19731 if (tmp == NULL_TREE)
19732 tmp = error_mark_node;
19733 }
19734 else
19735 {
19736 kind = (enum omp_clause_depend_kind)
19737 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
19738 tmp = NULL_TREE;
19739 }
19740 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
19741 }
19742 else
19743 finish_omp_depobj (EXPR_LOCATION (t), r,
19744 OMP_CLAUSE_DEPEND_INVALID,
19745 OMP_DEPOBJ_CLAUSES (t));
19746 break;
19747
19748 case OACC_DATA:
19749 case OMP_TARGET_DATA:
19750 case OMP_TARGET:
19751 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
19752 TREE_CODE (t) == OACC_DATA
19753 ? C_ORT_ACC
19754 : TREE_CODE (t) == OMP_TARGET
19755 ? C_ORT_OMP_TARGET : C_ORT_OMP,
19756 args, complain, in_decl);
19757 keep_next_level (true);
19758 stmt = begin_omp_structured_block ();
19759
19760 RECUR (OMP_BODY (t));
19761 stmt = finish_omp_structured_block (stmt);
19762
19763 t = copy_node (t);
19764 OMP_BODY (t) = stmt;
19765 OMP_CLAUSES (t) = tmp;
19766
19767 if (TREE_CODE (t) == OMP_TARGET)
19768 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
19769 &OMP_CLAUSES (t));
19770
19771 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19772 {
19773 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19774 if (teams)
19775 /* For combined target teams, ensure the num_teams and
19776 thread_limit clause expressions are evaluated on the host,
19777 before entering the target construct. */
19778 for (tree c = OMP_TEAMS_CLAUSES (teams);
19779 c; c = OMP_CLAUSE_CHAIN (c))
19780 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19781 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19782 for (int i = 0;
19783 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
19784 if (OMP_CLAUSE_OPERAND (c, i)
19785 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
19786 {
19787 tree expr = OMP_CLAUSE_OPERAND (c, i);
19788 expr = force_target_expr (TREE_TYPE (expr), expr,
19789 tf_none);
19790 if (expr == error_mark_node)
19791 continue;
19792 tmp = TARGET_EXPR_SLOT (expr);
19793 add_stmt (expr);
19794 OMP_CLAUSE_OPERAND (c, i) = expr;
19795 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19796 OMP_CLAUSE_FIRSTPRIVATE);
19797 OMP_CLAUSE_DECL (tc) = tmp;
19798 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19799 OMP_TARGET_CLAUSES (t) = tc;
19800 }
19801 }
19802 add_stmt (t);
19803 break;
19804
19805 case OACC_DECLARE:
19806 t = copy_node (t);
19807 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
19808 complain, in_decl);
19809 OACC_DECLARE_CLAUSES (t) = tmp;
19810 add_stmt (t);
19811 break;
19812
19813 case OMP_TARGET_UPDATE:
19814 case OMP_TARGET_ENTER_DATA:
19815 case OMP_TARGET_EXIT_DATA:
19816 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
19817 complain, in_decl);
19818 t = copy_node (t);
19819 OMP_STANDALONE_CLAUSES (t) = tmp;
19820 add_stmt (t);
19821 break;
19822
19823 case OACC_CACHE:
19824 case OACC_ENTER_DATA:
19825 case OACC_EXIT_DATA:
19826 case OACC_UPDATE:
19827 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
19828 complain, in_decl);
19829 t = copy_node (t);
19830 OMP_STANDALONE_CLAUSES (t) = tmp;
19831 add_stmt (t);
19832 break;
19833
19834 case OMP_ORDERED:
19835 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
19836 complain, in_decl);
19837 if (OMP_BODY (t))
19838 {
19839 stmt = push_stmt_list ();
19840 RECUR (OMP_BODY (t));
19841 stmt = pop_stmt_list (stmt);
19842 }
19843 else
19844 stmt = NULL_TREE;
19845
19846 t = copy_node (t);
19847 OMP_BODY (t) = stmt;
19848 OMP_ORDERED_CLAUSES (t) = tmp;
19849 add_stmt (t);
19850 break;
19851
19852 case OMP_MASTER:
19853 case OMP_STRUCTURED_BLOCK:
19854 omp_parallel_combined_clauses = NULL;
19855 /* FALLTHRU */
19856 case OMP_SECTION:
19857 stmt = push_stmt_list ();
19858 RECUR (OMP_BODY (t));
19859 stmt = pop_stmt_list (stmt);
19860
19861 t = copy_node (t);
19862 OMP_BODY (t) = stmt;
19863 add_stmt (t);
19864 break;
19865
19866 case OMP_DISPATCH:
19867 tmp = tsubst_omp_clauses (OMP_DISPATCH_CLAUSES (t), C_ORT_OMP, args,
19868 complain, in_decl);
19869 stmt = RECUR (OMP_DISPATCH_BODY (t));
19870 t = copy_node (t);
19871 OMP_DISPATCH_BODY (t) = stmt;
19872 OMP_DISPATCH_CLAUSES (t) = tmp;
19873 add_stmt (t);
19874 break;
19875
19876 case OMP_ATOMIC:
19877 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19878 tmp = NULL_TREE;
19879 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19880 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
19881 complain, in_decl);
19882 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19883 {
19884 tree op1 = TREE_OPERAND (t, 1);
19885 tree rhs1 = NULL_TREE;
19886 tree r = NULL_TREE;
19887 tree lhs, rhs;
19888 if (TREE_CODE (op1) == COMPOUND_EXPR)
19889 {
19890 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19891 op1 = TREE_OPERAND (op1, 1);
19892 }
19893 if (TREE_CODE (op1) == COND_EXPR)
19894 {
19895 gcc_assert (rhs1 == NULL_TREE);
19896 tree c = TREE_OPERAND (op1, 0);
19897 if (TREE_CODE (c) == MODIFY_EXPR)
19898 {
19899 r = RECUR (TREE_OPERAND (c, 0));
19900 c = TREE_OPERAND (c, 1);
19901 }
19902 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19903 rhs = RECUR (TREE_OPERAND (c, 1));
19904 lhs = RECUR (TREE_OPERAND (op1, 2));
19905 rhs1 = RECUR (TREE_OPERAND (op1, 1));
19906 }
19907 else
19908 {
19909 lhs = RECUR (TREE_OPERAND (op1, 0));
19910 rhs = RECUR (TREE_OPERAND (op1, 1));
19911 }
19912 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19913 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
19914 tmp, OMP_ATOMIC_MEMORY_ORDER (t),
19915 OMP_ATOMIC_WEAK (t));
19916 }
19917 else
19918 {
19919 tree op1 = TREE_OPERAND (t, 1);
19920 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19921 tree rhs1 = NULL_TREE, r = NULL_TREE;
19922 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19923 enum tree_code opcode = NOP_EXPR;
19924 if (code == OMP_ATOMIC_READ)
19925 {
19926 v = RECUR (TREE_OPERAND (op1, 0));
19927 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19928 }
19929 else if (code == OMP_ATOMIC_CAPTURE_OLD
19930 || code == OMP_ATOMIC_CAPTURE_NEW)
19931 {
19932 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19933 v = RECUR (TREE_OPERAND (op1, 0));
19934 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19935 if (TREE_CODE (op11) == COMPOUND_EXPR)
19936 {
19937 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19938 op11 = TREE_OPERAND (op11, 1);
19939 }
19940 if (TREE_CODE (op11) == COND_EXPR)
19941 {
19942 gcc_assert (rhs1 == NULL_TREE);
19943 tree c = TREE_OPERAND (op11, 0);
19944 if (TREE_CODE (c) == MODIFY_EXPR)
19945 {
19946 r = RECUR (TREE_OPERAND (c, 0));
19947 c = TREE_OPERAND (c, 1);
19948 }
19949 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19950 rhs = RECUR (TREE_OPERAND (c, 1));
19951 lhs = RECUR (TREE_OPERAND (op11, 2));
19952 rhs1 = RECUR (TREE_OPERAND (op11, 1));
19953 }
19954 else
19955 {
19956 lhs = RECUR (TREE_OPERAND (op11, 0));
19957 rhs = RECUR (TREE_OPERAND (op11, 1));
19958 }
19959 opcode = TREE_CODE (op11);
19960 if (opcode == MODIFY_EXPR)
19961 opcode = NOP_EXPR;
19962 }
19963 else
19964 {
19965 code = OMP_ATOMIC;
19966 lhs = RECUR (TREE_OPERAND (op1, 0));
19967 rhs = RECUR (TREE_OPERAND (op1, 1));
19968 }
19969 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19970 lhs1, rhs1, r, tmp,
19971 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
19972 }
19973 break;
19974
19975 case OMP_METADIRECTIVE:
19976 {
19977 tree variants = NULL_TREE;
19978 for (tree v = OMP_METADIRECTIVE_VARIANTS (t); v; v = TREE_CHAIN (v))
19979 {
19980 tree ctx = OMP_METADIRECTIVE_VARIANT_SELECTOR (v);
19981 tree directive = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (v);
19982 tree body = OMP_METADIRECTIVE_VARIANT_BODY (v);
19983 tree s;
19984
19985 /* CTX is null if this is the default variant. */
19986 if (ctx)
19987 {
19988 ctx = tsubst_omp_context_selector (ctx, args, complain,
19989 in_decl);
19990 /* Remove the selector from further consideration if it can be
19991 evaluated as a non-match at this point. */
19992 if (omp_context_selector_matches (ctx, NULL_TREE, false) == 0)
19993 continue;
19994 }
19995 s = push_stmt_list ();
19996 RECUR (directive);
19997 directive = pop_stmt_list (s);
19998 if (body)
19999 {
20000 s = push_stmt_list ();
20001 RECUR (body);
20002 body = pop_stmt_list (s);
20003 }
20004 variants
20005 = chainon (variants,
20006 make_omp_metadirective_variant (ctx, directive,
20007 body));
20008 }
20009 t = copy_node (t);
20010 OMP_METADIRECTIVE_VARIANTS (t) = variants;
20011
20012 /* Try to resolve the metadirective early. */
20013 vec<struct omp_variant> candidates
20014 = omp_early_resolve_metadirective (t);
20015 if (!candidates.is_empty ())
20016 t = c_omp_expand_variant_construct (candidates);
20017 add_stmt (t);
20018 break;
20019 }
20020
20021 case TRANSACTION_EXPR:
20022 {
20023 int flags = 0;
20024 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
20025 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
20026
20027 if (TRANSACTION_EXPR_IS_STMT (t))
20028 {
20029 tree body = TRANSACTION_EXPR_BODY (t);
20030 tree noex = NULL_TREE;
20031 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
20032 {
20033 noex = MUST_NOT_THROW_COND (body);
20034 if (noex == NULL_TREE)
20035 noex = boolean_true_node;
20036 body = TREE_OPERAND (body, 0);
20037 }
20038 stmt = begin_transaction_stmt (input_location, NULL, flags);
20039 RECUR (body);
20040 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
20041 }
20042 else
20043 {
20044 stmt = build_transaction_expr (EXPR_LOCATION (t),
20045 RECUR (TRANSACTION_EXPR_BODY (t)),
20046 flags, NULL_TREE);
20047 RETURN (stmt);
20048 }
20049 }
20050 break;
20051
20052 case OMP_INTEROP:
20053 tmp = tsubst_omp_clauses (OMP_INTEROP_CLAUSES (t), C_ORT_OMP_INTEROP,
20054 args, complain, in_decl);
20055 t = copy_node (t);
20056 OMP_INTEROP_CLAUSES (t) = tmp;
20057 add_stmt (t);
20058 break;
20059
20060 case MUST_NOT_THROW_EXPR:
20061 {
20062 tree op0 = RECUR (TREE_OPERAND (t, 0));
20063 tree cond = RECUR (MUST_NOT_THROW_COND (t));
20064 RETURN (build_must_not_throw_expr (op0, cond));
20065 }
20066
20067 case EXPR_PACK_EXPANSION:
20068 error ("invalid use of pack expansion expression");
20069 RETURN (error_mark_node);
20070
20071 case NONTYPE_ARGUMENT_PACK:
20072 error ("use %<...%> to expand argument pack");
20073 RETURN (error_mark_node);
20074
20075 case COMPOUND_EXPR:
20076 tmp = RECUR (TREE_OPERAND (t, 0));
20077 if (tmp == NULL_TREE)
20078 /* If the first operand was a statement, we're done with it. */
20079 RETURN (RECUR (TREE_OPERAND (t, 1)));
20080 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
20081 RECUR (TREE_OPERAND (t, 1)),
20082 templated_operator_saved_lookups (t),
20083 complain));
20084
20085 case PREDICT_EXPR:
20086 RETURN (add_stmt (copy_node (t)));
20087
20088 case ANNOTATE_EXPR:
20089 {
20090 /* Although ANNOTATE_EXPR is an expression, it can only appear in
20091 WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
20092 using tsubst_stmt rather than tsubst_expr and can contain
20093 DECL_EXPRs. */
20094 tree op1 = RECUR (TREE_OPERAND (t, 0));
20095 tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
20096 tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
20097 if (TREE_CODE (op2) == INTEGER_CST
20098 && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
20099 op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
20100 op3);
20101 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
20102 TREE_TYPE (op1), op1, op2, op3));
20103 }
20104
20105 default:
20106 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
20107
20108 RETURN (tsubst_expr (t, args, complain, in_decl));
20109 }
20110
20111 RETURN (NULL_TREE);
20112 out:
20113 input_location = loc;
20114 return r;
20115 #undef RECUR
20116 #undef RETURN
20117 }
20118
20119 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
20120 function. For description of the body see comment above
20121 cp_parser_omp_declare_reduction_exprs. */
20122
20123 static void
20124 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
20125 {
20126 if (t == NULL_TREE || t == error_mark_node)
20127 return;
20128
20129 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
20130
20131 tree_stmt_iterator tsi;
20132 int i;
20133 tree stmts[7];
20134 memset (stmts, 0, sizeof stmts);
20135 for (i = 0, tsi = tsi_start (t);
20136 i < 7 && !tsi_end_p (tsi);
20137 i++, tsi_next (&tsi))
20138 stmts[i] = tsi_stmt (tsi);
20139 gcc_assert (tsi_end_p (tsi));
20140
20141 if (i >= 3)
20142 {
20143 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
20144 && TREE_CODE (stmts[1]) == DECL_EXPR);
20145 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
20146 args, complain, in_decl);
20147 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
20148 args, complain, in_decl);
20149 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
20150 expect to be pushing it. */
20151 DECL_CONTEXT (omp_out) = current_function_decl;
20152 DECL_CONTEXT (omp_in) = current_function_decl;
20153 keep_next_level (true);
20154 tree block = begin_omp_structured_block ();
20155 tsubst_stmt (stmts[2], args, complain, in_decl);
20156 block = finish_omp_structured_block (block);
20157 block = maybe_cleanup_point_expr_void (block);
20158 add_decl_expr (omp_out);
20159 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
20160 add_decl_expr (omp_in);
20161 finish_expr_stmt (block);
20162 }
20163 if (i >= 6)
20164 {
20165 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
20166 && TREE_CODE (stmts[4]) == DECL_EXPR);
20167 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
20168 args, complain, in_decl);
20169 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
20170 args, complain, in_decl);
20171 DECL_CONTEXT (omp_priv) = current_function_decl;
20172 DECL_CONTEXT (omp_orig) = current_function_decl;
20173 keep_next_level (true);
20174 tree block = begin_omp_structured_block ();
20175 tsubst_stmt (stmts[5], args, complain, in_decl);
20176 block = finish_omp_structured_block (block);
20177 block = maybe_cleanup_point_expr_void (block);
20178 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
20179 add_decl_expr (omp_priv);
20180 add_decl_expr (omp_orig);
20181 finish_expr_stmt (block);
20182 if (i == 7)
20183 add_decl_expr (omp_orig);
20184 }
20185 }
20186
20187 /* T is a postfix-expression that is not being used in a function
20188 call. Return the substituted version of T. */
20189
20190 static tree
20191 tsubst_non_call_postfix_expression (tree t, tree args,
20192 tsubst_flags_t complain,
20193 tree in_decl)
20194 {
20195 if (TREE_CODE (t) == SCOPE_REF)
20196 t = tsubst_qualified_id (t, args, complain, in_decl,
20197 /*done=*/false, /*address_p=*/false);
20198 else
20199 t = tsubst_expr (t, args, complain, in_decl);
20200
20201 return t;
20202 }
20203
20204 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
20205 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
20206 dependent init-capture. EXPLICIT_P is true if the original list had
20207 explicit captures. */
20208
20209 static void
20210 prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
20211 tsubst_flags_t complain)
20212 {
20213 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
20214 {
20215 tree type = NULL_TREE;
20216 if (!init)
20217 {
20218 if (complain & tf_error)
20219 error ("empty initializer in lambda init-capture");
20220 init = error_mark_node;
20221 }
20222 else if (TREE_CODE (init) == TREE_LIST)
20223 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
20224 if (!type)
20225 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
20226 TREE_TYPE (field) = type;
20227 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
20228 }
20229 list = tree_cons (field, init, list);
20230 LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
20231 }
20232
20233 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
20234 instantiation context. Instantiating a pack expansion containing a lambda
20235 might result in multiple lambdas all based on the same lambda in the
20236 template. */
20237
20238 tree
20239 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
20240 {
20241 tree oldfn = lambda_function (t);
20242 in_decl = oldfn;
20243
20244 args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
20245 if (processing_template_decl
20246 && (!in_template_context || (complain & tf_partial)
20247 || LAMBDA_EXPR_EXTRA_ARGS (t)))
20248 {
20249 /* Defer templated substitution into a lambda-expr if we lost the
20250 necessary template context. This may happen for a lambda-expr
20251 used as a default template argument.
20252
20253 Defer dependent substitution as well so that we don't prematurely
20254 lower the level of a deduced return type or any other auto or
20255 template parameter belonging to the lambda.
20256
20257 Finally, if a substitution into this lambda was previously
20258 deferred, keep deferring until the final (non-templated)
20259 substitution. */
20260 t = copy_node (t);
20261 LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
20262 LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
20263 return t;
20264 }
20265
20266 tree r = build_lambda_expr ();
20267
20268 LAMBDA_EXPR_LOCATION (r)
20269 = LAMBDA_EXPR_LOCATION (t);
20270 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
20271 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
20272 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
20273 LAMBDA_EXPR_REGEN_INFO (r)
20274 = build_template_info (t, add_to_template_args (TI_ARGS (ti),
20275 preserve_args (args)));
20276 else
20277 LAMBDA_EXPR_REGEN_INFO (r)
20278 = build_template_info (t, preserve_args (args));
20279
20280 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
20281
20282 vec<tree,va_gc>* field_packs = NULL;
20283 unsigned name_independent_cnt = 0;
20284 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
20285 cap = TREE_CHAIN (cap))
20286 {
20287 tree ofield = TREE_PURPOSE (cap);
20288 tree init = TREE_VALUE (cap);
20289 if (PACK_EXPANSION_P (init))
20290 init = tsubst_pack_expansion (init, args, complain, in_decl);
20291 else
20292 init = tsubst_expr (init, args, complain, in_decl);
20293
20294 if (init == error_mark_node)
20295 return error_mark_node;
20296
20297 if (init && TREE_CODE (init) == TREE_LIST)
20298 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
20299
20300 if (!processing_template_decl
20301 && init && TREE_CODE (init) != TREE_VEC
20302 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
20303 {
20304 /* For a VLA, simply tsubsting the field type won't work, we need to
20305 go through add_capture again. XXX do we want to do this for all
20306 captures? */
20307 tree name = (get_identifier
20308 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
20309 tree ftype = TREE_TYPE (ofield);
20310 bool by_ref = (TYPE_REF_P (ftype)
20311 || (TREE_CODE (ftype) == DECLTYPE_TYPE
20312 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
20313 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
20314 &name_independent_cnt);
20315 continue;
20316 }
20317
20318 if (PACK_EXPANSION_P (ofield))
20319 ofield = PACK_EXPANSION_PATTERN (ofield);
20320 tree field = tsubst_decl (ofield, args, complain);
20321
20322 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
20323 {
20324 /* Remember these for when we've pushed local_specializations. */
20325 vec_safe_push (field_packs, ofield);
20326 vec_safe_push (field_packs, field);
20327 }
20328
20329 if (field == error_mark_node)
20330 return error_mark_node;
20331
20332 if (TREE_CODE (field) == TREE_VEC)
20333 {
20334 int len = TREE_VEC_LENGTH (field);
20335 gcc_assert (TREE_CODE (init) == TREE_VEC
20336 && TREE_VEC_LENGTH (init) == len);
20337 for (int i = 0; i < len; ++i)
20338 prepend_one_capture (TREE_VEC_ELT (field, i),
20339 TREE_VEC_ELT (init, i),
20340 LAMBDA_EXPR_CAPTURE_LIST (r),
20341 LAMBDA_CAPTURE_EXPLICIT_P (cap),
20342 complain);
20343 }
20344 else
20345 {
20346 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
20347 LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
20348
20349 if (id_equal (DECL_NAME (field), "__this"))
20350 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
20351 }
20352 }
20353
20354 tree type = begin_lambda_type (r);
20355 if (type == error_mark_node)
20356 {
20357 gcc_checking_assert (!(complain & tf_error) || seen_error ());
20358 return error_mark_node;
20359 }
20360
20361 if (LAMBDA_EXPR_EXTRA_SCOPE (t))
20362 record_lambda_scope (r);
20363 if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
20364 /* If we're pushed into another scope (PR105652), fix it. */
20365 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
20366 = TYPE_CONTEXT (TREE_TYPE (t));
20367 record_lambda_scope_discriminator (r);
20368
20369 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
20370 determine_visibility (TYPE_NAME (type));
20371
20372 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
20373
20374 tree oldtmpl = (generic_lambda_fn_p (oldfn)
20375 ? DECL_TI_TEMPLATE (oldfn)
20376 : NULL_TREE);
20377
20378 tree tparms = NULL_TREE;
20379 if (oldtmpl)
20380 tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
20381
20382 tree fntype = static_fn_type (oldfn);
20383
20384 tree saved_ctp = current_template_parms;
20385 if (oldtmpl)
20386 {
20387 ++processing_template_decl;
20388 current_template_parms = tparms;
20389 }
20390 fntype = tsubst (fntype, args, complain, in_decl);
20391 if (oldtmpl)
20392 {
20393 current_template_parms = saved_ctp;
20394 --processing_template_decl;
20395 }
20396
20397 if (fntype == error_mark_node)
20398 r = error_mark_node;
20399 else
20400 {
20401 /* The body of a lambda-expression is not a subexpression of the
20402 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
20403 which would be skipped if cp_unevaluated_operand. */
20404 cp_evaluated ev;
20405
20406 /* Fix the type of 'this'.
20407 For static and xobj member functions we use this to transport the
20408 lambda's closure type. It appears that in the regular case the
20409 object parameter is still pulled off, and then re-added again anyway.
20410 So perhaps we could do something better here? */
20411 fntype = build_memfn_type (fntype, type,
20412 type_memfn_quals (fntype),
20413 type_memfn_rqual (fntype));
20414 tree inst = (oldtmpl
20415 ? tsubst_template_decl (oldtmpl, args, complain,
20416 fntype, tparms)
20417 : tsubst_function_decl (oldfn, args, complain, fntype));
20418 if (inst == error_mark_node)
20419 {
20420 r = error_mark_node;
20421 goto out;
20422 }
20423 finish_member_declaration (inst);
20424 record_lambda_scope_sig_discriminator (r, inst);
20425
20426 tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
20427
20428 /* Let finish_function set this. */
20429 DECL_DECLARED_CONSTEXPR_P (fn) = false;
20430
20431 bool nested = cfun;
20432 if (nested)
20433 push_function_context ();
20434 else
20435 /* Still increment function_depth so that we don't GC in the
20436 middle of an expression. */
20437 ++function_depth;
20438
20439 local_specialization_stack s (lss_copy);
20440
20441 bool save_in_consteval_if_p = in_consteval_if_p;
20442 in_consteval_if_p = false;
20443
20444 tree body = start_lambda_function (fn, r);
20445
20446 /* Now record them for lookup_init_capture_pack. */
20447 int fplen = vec_safe_length (field_packs);
20448 for (int i = 0; i < fplen; )
20449 {
20450 tree pack = (*field_packs)[i++];
20451 tree inst = (*field_packs)[i++];
20452 register_local_specialization (inst, pack);
20453 }
20454 release_tree_vector (field_packs);
20455
20456 register_parameter_specializations (oldfn, fn);
20457
20458 if (oldtmpl)
20459 {
20460 /* We might not partially instantiate some parts of the function, so
20461 copy these flags from the original template. */
20462 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
20463 current_function_returns_value = ol->returns_value;
20464 current_function_returns_null = ol->returns_null;
20465 current_function_returns_abnormally = ol->returns_abnormally;
20466 current_function_infinite_loop = ol->infinite_loop;
20467 }
20468
20469 /* [temp.deduct] A lambda-expression appearing in a function type or a
20470 template parameter is not considered part of the immediate context for
20471 the purposes of template argument deduction. */
20472 complain = tf_warning_or_error;
20473
20474 tree saved = DECL_SAVED_TREE (oldfn);
20475 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
20476 /* We already have a body block from start_lambda_function, we don't
20477 need another to confuse NRV (91217). */
20478 saved = BIND_EXPR_BODY (saved);
20479
20480 tsubst_stmt (saved, args, complain, r);
20481
20482 finish_lambda_function (body);
20483
20484 in_consteval_if_p = save_in_consteval_if_p;
20485
20486 if (nested)
20487 pop_function_context ();
20488 else
20489 --function_depth;
20490
20491 /* The capture list was built up in reverse order; fix that now. */
20492 LAMBDA_EXPR_CAPTURE_LIST (r)
20493 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
20494
20495 maybe_add_lambda_conv_op (type);
20496 }
20497
20498 out:
20499 finish_struct (type, /*attr*/NULL_TREE);
20500
20501 insert_pending_capture_proxies ();
20502
20503 return r;
20504 }
20505
20506 /* Subroutine of maybe_fold_fn_template_args. */
20507
20508 static bool
20509 fold_targs_r (tree targs, tsubst_flags_t complain)
20510 {
20511 int len = TREE_VEC_LENGTH (targs);
20512 for (int i = 0; i < len; ++i)
20513 {
20514 tree &elt = TREE_VEC_ELT (targs, i);
20515 if (!elt || TYPE_P (elt)
20516 || TREE_CODE (elt) == TEMPLATE_DECL)
20517 continue;
20518 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
20519 {
20520 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
20521 return false;
20522 }
20523 else if (/* We can only safely preevaluate scalar prvalues. */
20524 SCALAR_TYPE_P (TREE_TYPE (elt))
20525 && !glvalue_p (elt)
20526 && !TREE_CONSTANT (elt))
20527 {
20528 elt = cxx_constant_value (elt, complain);
20529 if (elt == error_mark_node)
20530 return false;
20531 }
20532 }
20533
20534 return true;
20535 }
20536
20537 /* Try to do constant evaluation of any explicit template arguments in FN
20538 before overload resolution, to get any errors only once. Return true iff
20539 we didn't have any problems folding. */
20540
20541 static bool
20542 maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
20543 {
20544 if (processing_template_decl || fn == NULL_TREE)
20545 return true;
20546 if (fn == error_mark_node)
20547 return false;
20548 if (TREE_CODE (fn) == OFFSET_REF
20549 || TREE_CODE (fn) == COMPONENT_REF)
20550 fn = TREE_OPERAND (fn, 1);
20551 if (BASELINK_P (fn))
20552 fn = BASELINK_FUNCTIONS (fn);
20553 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
20554 return true;
20555 tree targs = TREE_OPERAND (fn, 1);
20556 if (targs == NULL_TREE)
20557 return true;
20558 if (targs == error_mark_node)
20559 return false;
20560 return fold_targs_r (targs, complain);
20561 }
20562
20563 /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */
20564
20565 static void
20566 tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
20567 tree in_decl, releasing_vec &call_args)
20568 {
20569 unsigned int nargs = call_expr_nargs (t);
20570 for (unsigned int i = 0; i < nargs; ++i)
20571 {
20572 tree arg = CALL_EXPR_ARG (t, i);
20573
20574 if (!PACK_EXPANSION_P (arg))
20575 vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
20576 else
20577 {
20578 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
20579 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20580 if (TREE_CODE (arg) == TREE_VEC)
20581 {
20582 unsigned int len, j;
20583
20584 len = TREE_VEC_LENGTH (arg);
20585 for (j = 0; j < len; ++j)
20586 {
20587 tree value = TREE_VEC_ELT (arg, j);
20588 if (value != NULL_TREE)
20589 value = convert_from_reference (value);
20590 vec_safe_push (call_args, value);
20591 }
20592 }
20593 else
20594 /* A partial substitution. Add one entry. */
20595 vec_safe_push (call_args, arg);
20596 }
20597 }
20598 }
20599
20600 /* Like tsubst but deals with expressions and performs semantic
20601 analysis. */
20602
20603 tree
20604 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
20605 {
20606 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
20607 #define RECUR(NODE) \
20608 tsubst_expr (NODE, args, complain, in_decl)
20609
20610 tree retval, op1;
20611 location_t save_loc;
20612
20613 if (t == NULL_TREE || t == error_mark_node)
20614 return t;
20615
20616 save_loc = input_location;
20617 if (location_t eloc = cp_expr_location (t))
20618 input_location = eloc;
20619
20620 /* N3276 decltype magic only applies to calls at the top level or on the
20621 right side of a comma. */
20622 tsubst_flags_t decltype_flag = (complain & tf_decltype);
20623 complain &= ~tf_decltype;
20624
20625 /* This flag only applies to id-expressions at the top level, and
20626 controls resolution thereof. */
20627 tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
20628 complain &= ~tf_no_name_lookup;
20629
20630 if (!no_name_lookup_flag)
20631 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
20632 return d;
20633
20634 switch (TREE_CODE (t))
20635 {
20636 case USING_DECL:
20637 t = DECL_NAME (t);
20638 /* Fall through. */
20639 case IDENTIFIER_NODE:
20640 {
20641 tree decl;
20642 cp_id_kind idk;
20643 const char *error_msg;
20644
20645 if (IDENTIFIER_CONV_OP_P (t))
20646 {
20647 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20648 t = make_conv_op_name (new_type);
20649 }
20650
20651 if (no_name_lookup_flag)
20652 RETURN (t);
20653
20654 /* Look up the name. */
20655 decl = lookup_name (t);
20656
20657 /* By convention, expressions use ERROR_MARK_NODE to indicate
20658 failure, not NULL_TREE. */
20659 if (decl == NULL_TREE)
20660 decl = error_mark_node;
20661
20662 decl = finish_id_expression (t, decl, NULL_TREE,
20663 &idk,
20664 /*i_c_e_p=*/false,
20665 /*allow_i_c_e_p=*/true,
20666 /*non_i_c_e_p=*/nullptr,
20667 /*template_p=*/false,
20668 /*done=*/true,
20669 /*address_p=*/false,
20670 /*template_arg_p=*/false,
20671 &error_msg,
20672 input_location);
20673 if (error_msg)
20674 error (error_msg);
20675 if (identifier_p (decl))
20676 {
20677 if (complain & tf_error)
20678 unqualified_name_lookup_error (decl);
20679 decl = error_mark_node;
20680 }
20681 RETURN (decl);
20682 }
20683
20684 case TEMPLATE_ID_EXPR:
20685 {
20686 tree object;
20687 tree templ = TREE_OPERAND (t, 0);
20688 tree targs = TREE_OPERAND (t, 1);
20689
20690 if (no_name_lookup_flag)
20691 templ = tsubst_name (templ, args, complain, in_decl);
20692 else
20693 templ = tsubst_expr (templ, args, complain, in_decl);
20694
20695 if (targs)
20696 targs = tsubst_template_args (targs, args, complain, in_decl);
20697 if (targs == error_mark_node)
20698 RETURN (error_mark_node);
20699
20700 if (TREE_CODE (templ) == SCOPE_REF)
20701 {
20702 tree name = TREE_OPERAND (templ, 1);
20703 tree tid = lookup_template_function (name, targs);
20704 TREE_OPERAND (templ, 1) = tid;
20705 RETURN (templ);
20706 }
20707
20708 if (concept_definition_p (templ))
20709 {
20710 tree check = build_concept_check (templ, targs, complain);
20711 if (check == error_mark_node)
20712 RETURN (error_mark_node);
20713 RETURN (check);
20714 }
20715
20716 if (variable_template_p (templ))
20717 {
20718 if (no_name_lookup_flag)
20719 RETURN (lookup_template_variable (templ, targs, complain));
20720
20721 tree r = lookup_and_finish_template_variable (templ, targs,
20722 complain);
20723 r = convert_from_reference (r);
20724 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
20725 RETURN (r);
20726 }
20727
20728 if (TREE_CODE (templ) == COMPONENT_REF)
20729 {
20730 object = TREE_OPERAND (templ, 0);
20731 templ = TREE_OPERAND (templ, 1);
20732 }
20733 else
20734 object = NULL_TREE;
20735
20736 tree tid = lookup_template_function (templ, targs);
20737 protected_set_expr_location (tid, EXPR_LOCATION (t));
20738
20739 if (object)
20740 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
20741 object, tid, NULL_TREE));
20742 else if (no_name_lookup_flag)
20743 RETURN (tid);
20744 else if (identifier_p (templ))
20745 {
20746 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20747 name lookup found nothing when parsing the template name. */
20748 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
20749 RETURN (tid);
20750 }
20751 else
20752 RETURN (baselink_for_fns (tid));
20753 }
20754
20755 case INDIRECT_REF:
20756 {
20757 tree r = RECUR (TREE_OPERAND (t, 0));
20758
20759 if (REFERENCE_REF_P (t))
20760 {
20761 /* A type conversion to reference type will be enclosed in
20762 such an indirect ref, but the substitution of the cast
20763 will have also added such an indirect ref. */
20764 r = convert_from_reference (r);
20765 }
20766 else
20767 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
20768 templated_operator_saved_lookups (t),
20769 complain|decltype_flag);
20770
20771 if (REF_PARENTHESIZED_P (t))
20772 r = force_paren_expr (r);
20773
20774 RETURN (r);
20775 }
20776
20777 case MEM_REF:
20778 {
20779 tree op0 = RECUR (TREE_OPERAND (t, 0));
20780 tree op1 = RECUR (TREE_OPERAND (t, 0));
20781 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20782 RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
20783 }
20784
20785 case NOP_EXPR:
20786 {
20787 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20788 tree op0 = RECUR (TREE_OPERAND (t, 0));
20789 RETURN (build_nop (type, op0));
20790 }
20791
20792 case IMPLICIT_CONV_EXPR:
20793 {
20794 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20795 if (type == error_mark_node)
20796 RETURN (error_mark_node);
20797 tree expr = RECUR (TREE_OPERAND (t, 0));
20798 if (dependent_type_p (type) || type_dependent_expression_p (expr))
20799 {
20800 retval = copy_node (t);
20801 TREE_TYPE (retval) = type;
20802 TREE_OPERAND (retval, 0) = expr;
20803 RETURN (retval);
20804 }
20805 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
20806 {
20807 tree r = convert_nontype_argument (type, expr, complain);
20808 if (r == NULL_TREE)
20809 r = error_mark_node;
20810 RETURN (r);
20811 }
20812 int flags = LOOKUP_IMPLICIT;
20813 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
20814 flags = LOOKUP_NORMAL;
20815 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
20816 flags |= LOOKUP_NO_NARROWING;
20817 RETURN (perform_implicit_conversion_flags (type, expr, complain,
20818 flags));
20819 }
20820
20821 case CONVERT_EXPR:
20822 {
20823 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20824 tree op0 = RECUR (TREE_OPERAND (t, 0));
20825 if (op0 == error_mark_node)
20826 RETURN (error_mark_node);
20827 RETURN (build1 (CONVERT_EXPR, type, op0));
20828 }
20829
20830 case CAST_EXPR:
20831 case REINTERPRET_CAST_EXPR:
20832 case CONST_CAST_EXPR:
20833 case DYNAMIC_CAST_EXPR:
20834 case STATIC_CAST_EXPR:
20835 {
20836 tree type;
20837 tree op, r = NULL_TREE;
20838
20839 tsubst_flags_t tcomplain = complain;
20840 if (TREE_CODE (t) == CAST_EXPR)
20841 tcomplain |= tf_tst_ok;
20842 type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
20843
20844 op = RECUR (TREE_OPERAND (t, 0));
20845
20846 warning_sentinel s(warn_useless_cast);
20847 warning_sentinel s2(warn_ignored_qualifiers);
20848 warning_sentinel s3(warn_int_in_bool_context);
20849 switch (TREE_CODE (t))
20850 {
20851 case CAST_EXPR:
20852 r = build_functional_cast (input_location, type, op, complain);
20853 break;
20854 case REINTERPRET_CAST_EXPR:
20855 r = build_reinterpret_cast (input_location, type, op, complain);
20856 break;
20857 case CONST_CAST_EXPR:
20858 r = build_const_cast (input_location, type, op, complain);
20859 break;
20860 case DYNAMIC_CAST_EXPR:
20861 r = build_dynamic_cast (input_location, type, op, complain);
20862 break;
20863 case STATIC_CAST_EXPR:
20864 r = build_static_cast (input_location, type, op, complain);
20865 if (IMPLICIT_RVALUE_P (t))
20866 set_implicit_rvalue_p (r);
20867 break;
20868 default:
20869 gcc_unreachable ();
20870 }
20871
20872 RETURN (r);
20873 }
20874
20875 case BIT_CAST_EXPR:
20876 {
20877 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20878 tree op0 = RECUR (TREE_OPERAND (t, 0));
20879 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
20880 }
20881
20882 case POSTDECREMENT_EXPR:
20883 case POSTINCREMENT_EXPR:
20884 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20885 args, complain, in_decl);
20886 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
20887 templated_operator_saved_lookups (t),
20888 complain|decltype_flag));
20889
20890 case BIT_NOT_EXPR:
20891 if (identifier_p (TREE_OPERAND (t, 0)))
20892 {
20893 gcc_checking_assert (no_name_lookup_flag);
20894 RETURN (t);
20895 }
20896 else if (TYPE_P (TREE_OPERAND (t, 0)))
20897 {
20898 gcc_checking_assert (no_name_lookup_flag);
20899 tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
20900 RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
20901 }
20902 /* Fall through. */
20903 case PREDECREMENT_EXPR:
20904 case PREINCREMENT_EXPR:
20905 case NEGATE_EXPR:
20906 case ABS_EXPR:
20907 case TRUTH_NOT_EXPR:
20908 case UNARY_PLUS_EXPR: /* Unary + */
20909 case REALPART_EXPR:
20910 case IMAGPART_EXPR:
20911 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
20912 RECUR (TREE_OPERAND (t, 0)),
20913 templated_operator_saved_lookups (t),
20914 complain|decltype_flag));
20915
20916 case EXCESS_PRECISION_EXPR:
20917 {
20918 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20919 tree op0 = RECUR (TREE_OPERAND (t, 0));
20920 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
20921 RETURN (op0);
20922 RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
20923 type, op0));
20924 }
20925
20926 case FIX_TRUNC_EXPR:
20927 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20928 gcc_unreachable ();
20929
20930 case ADDR_EXPR:
20931 op1 = TREE_OPERAND (t, 0);
20932 if (TREE_CODE (op1) == LABEL_DECL)
20933 RETURN (finish_label_address_expr (DECL_NAME (op1),
20934 EXPR_LOCATION (op1)));
20935 if (TREE_CODE (op1) == SCOPE_REF)
20936 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
20937 /*done=*/true, /*address_p=*/true);
20938 else
20939 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
20940 in_decl);
20941 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
20942 templated_operator_saved_lookups (t),
20943 complain|decltype_flag));
20944
20945 case PLUS_EXPR:
20946 case MINUS_EXPR:
20947 case MULT_EXPR:
20948 case TRUNC_DIV_EXPR:
20949 case CEIL_DIV_EXPR:
20950 case FLOOR_DIV_EXPR:
20951 case ROUND_DIV_EXPR:
20952 case EXACT_DIV_EXPR:
20953 case BIT_AND_EXPR:
20954 case BIT_IOR_EXPR:
20955 case BIT_XOR_EXPR:
20956 case TRUNC_MOD_EXPR:
20957 case FLOOR_MOD_EXPR:
20958 case TRUTH_ANDIF_EXPR:
20959 case TRUTH_ORIF_EXPR:
20960 case TRUTH_AND_EXPR:
20961 case TRUTH_OR_EXPR:
20962 case RSHIFT_EXPR:
20963 case LSHIFT_EXPR:
20964 case EQ_EXPR:
20965 case NE_EXPR:
20966 case MAX_EXPR:
20967 case MIN_EXPR:
20968 case LE_EXPR:
20969 case GE_EXPR:
20970 case LT_EXPR:
20971 case GT_EXPR:
20972 case SPACESHIP_EXPR:
20973 case MEMBER_REF:
20974 case DOTSTAR_EXPR:
20975 {
20976 /* If either OP0 or OP1 was value- or type-dependent, suppress
20977 warnings that depend on the range of the types involved. */
20978 tree op0 = TREE_OPERAND (t, 0);
20979 tree op1 = TREE_OPERAND (t, 1);
20980 const bool was_dep = (dependent_operand_p (op0)
20981 || dependent_operand_p (op1));
20982 op0 = RECUR (op0);
20983 op1 = RECUR (op1);
20984
20985 warning_sentinel s1(warn_type_limits, was_dep);
20986 warning_sentinel s2(warn_div_by_zero, was_dep);
20987 warning_sentinel s3(warn_logical_op, was_dep);
20988 warning_sentinel s4(warn_tautological_compare, was_dep);
20989 warning_sentinel s5(warn_address, was_dep);
20990
20991 tree r = build_x_binary_op
20992 (input_location, TREE_CODE (t),
20993 op0,
20994 (warning_suppressed_p (TREE_OPERAND (t, 0))
20995 ? ERROR_MARK
20996 : TREE_CODE (TREE_OPERAND (t, 0))),
20997 op1,
20998 (warning_suppressed_p (TREE_OPERAND (t, 1))
20999 ? ERROR_MARK
21000 : TREE_CODE (TREE_OPERAND (t, 1))),
21001 templated_operator_saved_lookups (t),
21002 /*overload=*/NULL,
21003 complain|decltype_flag);
21004 if (EXPR_P (r))
21005 copy_warning (r, t);
21006
21007 RETURN (r);
21008 }
21009
21010 case POINTER_PLUS_EXPR:
21011 {
21012 tree op0 = RECUR (TREE_OPERAND (t, 0));
21013 if (op0 == error_mark_node)
21014 RETURN (error_mark_node);
21015 tree op1 = RECUR (TREE_OPERAND (t, 1));
21016 if (op1 == error_mark_node)
21017 RETURN (error_mark_node);
21018 RETURN (fold_build_pointer_plus (op0, op1));
21019 }
21020
21021 case SCOPE_REF:
21022 if (no_name_lookup_flag)
21023 {
21024 tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
21025 tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
21026 RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
21027 QUALIFIED_NAME_IS_TEMPLATE (t)));
21028 }
21029 else
21030 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
21031 /*address_p=*/false));
21032
21033 case BASELINK:
21034 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
21035 args, complain, in_decl));
21036
21037 case ARRAY_REF:
21038 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21039 args, complain, in_decl);
21040 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
21041 && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
21042 == ovl_op_identifier (ARRAY_REF)))
21043 {
21044 tree c = TREE_OPERAND (t, 1);
21045 releasing_vec index_exp_list;
21046 tsubst_call_args (c, args, complain, in_decl, index_exp_list);
21047
21048 tree r;
21049 if (vec_safe_length (index_exp_list) == 1
21050 && !PACK_EXPANSION_P (index_exp_list[0]))
21051 r = grok_array_decl (EXPR_LOCATION (t), op1,
21052 index_exp_list[0], NULL,
21053 complain | decltype_flag);
21054 else
21055 r = grok_array_decl (EXPR_LOCATION (t), op1,
21056 NULL_TREE, &index_exp_list,
21057 complain | decltype_flag);
21058 RETURN (r);
21059 }
21060 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
21061 RECUR (TREE_OPERAND (t, 1)),
21062 complain|decltype_flag));
21063
21064 case OMP_ARRAY_SECTION:
21065 {
21066 tree op0 = RECUR (TREE_OPERAND (t, 0));
21067 tree op1 = NULL_TREE, op2 = NULL_TREE;
21068 if (op0 == error_mark_node)
21069 RETURN (error_mark_node);
21070 if (TREE_OPERAND (t, 1))
21071 {
21072 op1 = RECUR (TREE_OPERAND (t, 1));
21073 if (op1 == error_mark_node)
21074 RETURN (error_mark_node);
21075 }
21076 if (TREE_OPERAND (t, 2))
21077 {
21078 op2 = RECUR (TREE_OPERAND (t, 2));
21079 if (op2 == error_mark_node)
21080 RETURN (error_mark_node);
21081 }
21082 RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
21083 }
21084
21085 case SIZEOF_EXPR:
21086 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
21087 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
21088 {
21089 tree expanded, op = TREE_OPERAND (t, 0);
21090 int len = 0;
21091
21092 if (SIZEOF_EXPR_TYPE_P (t))
21093 op = TREE_TYPE (op);
21094
21095 ++cp_unevaluated_operand;
21096 ++c_inhibit_evaluation_warnings;
21097 /* We only want to compute the number of arguments. */
21098 if (PACK_EXPANSION_P (op))
21099 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
21100 else
21101 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
21102 args, complain, in_decl);
21103 --cp_unevaluated_operand;
21104 --c_inhibit_evaluation_warnings;
21105
21106 if (TREE_CODE (expanded) == TREE_VEC)
21107 {
21108 len = TREE_VEC_LENGTH (expanded);
21109 /* Set TREE_USED for the benefit of -Wunused. */
21110 for (int i = 0; i < len; i++)
21111 if (DECL_P (TREE_VEC_ELT (expanded, i)))
21112 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
21113 }
21114
21115 if (expanded == error_mark_node)
21116 RETURN (error_mark_node);
21117 else if (PACK_EXPANSION_P (expanded)
21118 || (TREE_CODE (expanded) == TREE_VEC
21119 && pack_expansion_args_count (expanded)))
21120
21121 {
21122 if (PACK_EXPANSION_P (expanded))
21123 /* OK. */;
21124 else
21125 expanded = make_argument_pack (expanded);
21126
21127 if (TYPE_P (expanded))
21128 RETURN (cxx_sizeof_or_alignof_type (input_location,
21129 expanded, SIZEOF_EXPR,
21130 false,
21131 complain & tf_error));
21132 else
21133 RETURN (cxx_sizeof_or_alignof_expr (input_location,
21134 expanded, SIZEOF_EXPR,
21135 false,
21136 complain & tf_error));
21137 }
21138 else
21139 RETURN (build_int_cst (size_type_node, len));
21140 }
21141 /* Fall through */
21142
21143 case ALIGNOF_EXPR:
21144 {
21145 tree r;
21146
21147 op1 = TREE_OPERAND (t, 0);
21148 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
21149 op1 = TREE_TYPE (op1);
21150 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
21151 && ALIGNOF_EXPR_STD_P (t));
21152 if (!args)
21153 {
21154 /* When there are no ARGS, we are trying to evaluate a
21155 non-dependent expression from the parser. Trying to do
21156 the substitutions may not work. */
21157 if (!TYPE_P (op1))
21158 op1 = TREE_TYPE (op1);
21159 }
21160 else
21161 {
21162 ++cp_unevaluated_operand;
21163 ++c_inhibit_evaluation_warnings;
21164 if (TYPE_P (op1))
21165 op1 = tsubst (op1, args, complain, in_decl);
21166 else
21167 op1 = tsubst_expr (op1, args, complain, in_decl);
21168 --cp_unevaluated_operand;
21169 --c_inhibit_evaluation_warnings;
21170 }
21171 if (TYPE_P (op1))
21172 r = cxx_sizeof_or_alignof_type (input_location,
21173 op1, TREE_CODE (t), std_alignof,
21174 complain & tf_error);
21175 else
21176 r = cxx_sizeof_or_alignof_expr (input_location,
21177 op1, TREE_CODE (t), std_alignof,
21178 complain & tf_error);
21179 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
21180 {
21181 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
21182 {
21183 if (!processing_template_decl && TYPE_P (op1))
21184 {
21185 r = build_min (SIZEOF_EXPR, size_type_node,
21186 build1 (NOP_EXPR, op1, error_mark_node));
21187 SIZEOF_EXPR_TYPE_P (r) = 1;
21188 }
21189 else
21190 r = build_min (SIZEOF_EXPR, size_type_node, op1);
21191 TREE_SIDE_EFFECTS (r) = 0;
21192 TREE_READONLY (r) = 1;
21193 }
21194 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
21195 copy_warning (r, t);
21196 }
21197 RETURN (r);
21198 }
21199
21200 case AT_ENCODE_EXPR:
21201 {
21202 op1 = TREE_OPERAND (t, 0);
21203 ++cp_unevaluated_operand;
21204 ++c_inhibit_evaluation_warnings;
21205 op1 = tsubst (op1, args, complain, in_decl);
21206 --cp_unevaluated_operand;
21207 --c_inhibit_evaluation_warnings;
21208 RETURN (objc_build_encode_expr (op1));
21209 }
21210
21211 case NOEXCEPT_EXPR:
21212 op1 = TREE_OPERAND (t, 0);
21213 ++cp_unevaluated_operand;
21214 ++c_inhibit_evaluation_warnings;
21215 ++cp_noexcept_operand;
21216 op1 = tsubst_expr (op1, args, complain, in_decl);
21217 --cp_unevaluated_operand;
21218 --c_inhibit_evaluation_warnings;
21219 --cp_noexcept_operand;
21220 RETURN (finish_noexcept_expr (op1, complain));
21221
21222 case MODOP_EXPR:
21223 {
21224 warning_sentinel s(warn_div_by_zero);
21225 tree lhs = RECUR (TREE_OPERAND (t, 0));
21226 tree rhs = RECUR (TREE_OPERAND (t, 2));
21227
21228 tree r = build_x_modify_expr
21229 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
21230 templated_operator_saved_lookups (t),
21231 complain|decltype_flag);
21232 /* TREE_NO_WARNING must be set if either the expression was
21233 parenthesized or it uses an operator such as >>= rather
21234 than plain assignment. In the former case, it was already
21235 set and must be copied. In the latter case,
21236 build_x_modify_expr sets it and it must not be reset
21237 here. */
21238 if (warning_suppressed_p (t, OPT_Wparentheses))
21239 suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
21240
21241 RETURN (r);
21242 }
21243
21244 case ARROW_EXPR:
21245 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21246 args, complain, in_decl);
21247 /* Remember that there was a reference to this entity. */
21248 if (DECL_P (op1)
21249 && !mark_used (op1, complain) && !(complain & tf_error))
21250 RETURN (error_mark_node);
21251 RETURN (build_x_arrow (input_location, op1, complain));
21252
21253 case NEW_EXPR:
21254 {
21255 tree placement = RECUR (TREE_OPERAND (t, 0));
21256 tree init = RECUR (TREE_OPERAND (t, 3));
21257 vec<tree, va_gc> *placement_vec;
21258 vec<tree, va_gc> *init_vec;
21259 tree ret;
21260 location_t loc = EXPR_LOCATION (t);
21261
21262 if (placement == NULL_TREE)
21263 placement_vec = NULL;
21264 else if (placement == error_mark_node)
21265 RETURN (error_mark_node);
21266 else
21267 {
21268 placement_vec = make_tree_vector ();
21269 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
21270 vec_safe_push (placement_vec, TREE_VALUE (placement));
21271 }
21272
21273 /* If there was an initializer in the original tree, but it
21274 instantiated to an empty list, then we should pass a
21275 non-NULL empty vector to tell build_new that it was an
21276 empty initializer() rather than no initializer. This can
21277 only happen when the initializer is a pack expansion whose
21278 parameter packs are of length zero. */
21279 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
21280 init_vec = NULL;
21281 else if (init == error_mark_node)
21282 RETURN (error_mark_node);
21283 else
21284 {
21285 init_vec = make_tree_vector ();
21286 if (init == void_node)
21287 gcc_assert (init_vec != NULL);
21288 else
21289 {
21290 for (; init != NULL_TREE; init = TREE_CHAIN (init))
21291 vec_safe_push (init_vec, TREE_VALUE (init));
21292 }
21293 }
21294
21295 /* Avoid passing an enclosing decl to valid_array_size_p. */
21296 in_decl = NULL_TREE;
21297
21298 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
21299 tree op2 = RECUR (TREE_OPERAND (t, 2));
21300 ret = build_new (loc, &placement_vec, op1, op2,
21301 &init_vec, NEW_EXPR_USE_GLOBAL (t),
21302 complain);
21303
21304 if (placement_vec != NULL)
21305 release_tree_vector (placement_vec);
21306 if (init_vec != NULL)
21307 release_tree_vector (init_vec);
21308
21309 RETURN (ret);
21310 }
21311
21312 case DELETE_EXPR:
21313 {
21314 tree op0 = RECUR (TREE_OPERAND (t, 0));
21315 tree op1 = RECUR (TREE_OPERAND (t, 1));
21316 RETURN (delete_sanity (input_location, op0, op1,
21317 DELETE_EXPR_USE_VEC (t),
21318 DELETE_EXPR_USE_GLOBAL (t),
21319 complain));
21320 }
21321
21322 case COMPOUND_EXPR:
21323 {
21324 tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
21325 complain & ~tf_decltype, in_decl);
21326 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
21327 op0,
21328 RECUR (TREE_OPERAND (t, 1)),
21329 templated_operator_saved_lookups (t),
21330 complain|decltype_flag));
21331 }
21332
21333 case CALL_EXPR:
21334 {
21335 tree function;
21336 unsigned int nargs;
21337 bool qualified_p;
21338 bool koenig_p;
21339 tree ret;
21340
21341 function = CALL_EXPR_FN (t);
21342 /* Internal function with no arguments. */
21343 if (function == NULL_TREE && call_expr_nargs (t) == 0)
21344 RETURN (t);
21345
21346 /* When we parsed the expression, we determined whether or
21347 not Koenig lookup should be performed. */
21348 koenig_p = KOENIG_LOOKUP_P (t);
21349 if (function == NULL_TREE)
21350 {
21351 koenig_p = false;
21352 qualified_p = false;
21353 }
21354 else if (TREE_CODE (function) == SCOPE_REF)
21355 {
21356 qualified_p = true;
21357 function = tsubst_qualified_id (function, args, complain, in_decl,
21358 /*done=*/false,
21359 /*address_p=*/false);
21360 }
21361 else if (CALL_EXPR_STATIC_CHAIN (t)
21362 && TREE_CODE (function) == FUNCTION_DECL
21363 && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
21364 {
21365 tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
21366 in_decl);
21367 if (dependent_type_p (type))
21368 {
21369 ret = build_vl_exp (CALL_EXPR, 4);
21370 CALL_EXPR_FN (ret) = function;
21371 CALL_EXPR_STATIC_CHAIN (ret) = type;
21372 CALL_EXPR_ARG (ret, 0)
21373 = build_min (SIZEOF_EXPR, size_type_node, type);
21374 TREE_TYPE (ret) = integer_type_node;
21375 }
21376 else
21377 ret = build_int_cst (integer_type_node, type_to_class (type));
21378 RETURN (ret);
21379 }
21380 else if (koenig_p
21381 && (identifier_p (function)
21382 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
21383 && identifier_p (TREE_OPERAND (function, 0)))))
21384 {
21385 /* Do nothing; calling tsubst_expr on an identifier
21386 would incorrectly perform unqualified lookup again.
21387
21388 Note that we can also have an IDENTIFIER_NODE if the earlier
21389 unqualified lookup found a dependent local extern declaration
21390 (as per finish_call_expr); in that case koenig_p will be false
21391 and we do want to do the lookup again to find the substituted
21392 declaration. */
21393 qualified_p = false;
21394
21395 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
21396 function = tsubst_name (function, args, complain, in_decl);
21397 }
21398 else
21399 {
21400 if (TREE_CODE (function) == COMPONENT_REF)
21401 {
21402 tree op = TREE_OPERAND (function, 1);
21403
21404 qualified_p = (TREE_CODE (op) == SCOPE_REF
21405 || (BASELINK_P (op)
21406 && BASELINK_QUALIFIED_P (op)));
21407 }
21408 else
21409 qualified_p = false;
21410
21411 if (TREE_CODE (function) == ADDR_EXPR
21412 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
21413 /* Avoid error about taking the address of a constructor. */
21414 function = TREE_OPERAND (function, 0);
21415
21416 tsubst_flags_t subcomplain = complain;
21417 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
21418 /* When KOENIG_P, we don't want to mark_used the callee before
21419 augmenting the overload set via ADL, so during this initial
21420 substitution we disable mark_used by setting tf_conv (68942). */
21421 subcomplain |= tf_conv;
21422 function = tsubst_expr (function, args, subcomplain, in_decl);
21423
21424 if (BASELINK_P (function))
21425 qualified_p = true;
21426 }
21427
21428 nargs = call_expr_nargs (t);
21429 releasing_vec call_args;
21430 tsubst_call_args (t, args, complain, in_decl, call_args);
21431
21432 /* Stripped-down processing for a call in a thunk. Specifically, in
21433 the thunk template for a generic lambda. */
21434 if (call_from_lambda_thunk_p (t))
21435 {
21436 /* Now that we've expanded any packs, the number of call args
21437 might be different. */
21438 unsigned int cargs = call_args->length ();
21439 tree thisarg = NULL_TREE;
21440 if (TREE_CODE (function) == COMPONENT_REF)
21441 {
21442 thisarg = TREE_OPERAND (function, 0);
21443 if (TREE_CODE (thisarg) == INDIRECT_REF)
21444 thisarg = TREE_OPERAND (thisarg, 0);
21445 function = TREE_OPERAND (function, 1);
21446 if (TREE_CODE (function) == BASELINK)
21447 function = BASELINK_FUNCTIONS (function);
21448 }
21449 /* We aren't going to do normal overload resolution, so force the
21450 template-id to resolve. */
21451 function = resolve_nondeduced_context (function, complain);
21452 for (unsigned i = 0; i < cargs; ++i)
21453 {
21454 /* In a thunk, pass through args directly, without any
21455 conversions. */
21456 tree arg = (*call_args)[i];
21457 while (TREE_CODE (arg) != PARM_DECL)
21458 arg = TREE_OPERAND (arg, 0);
21459 (*call_args)[i] = arg;
21460 }
21461 if (thisarg)
21462 {
21463 /* If there are no other args, just push 'this'. */
21464 if (cargs == 0)
21465 vec_safe_push (call_args, thisarg);
21466 else
21467 {
21468 /* Otherwise, shift the other args over to make room. */
21469 tree last = (*call_args)[cargs - 1];
21470 vec_safe_push (call_args, last);
21471 for (int i = cargs - 1; i > 0; --i)
21472 (*call_args)[i] = (*call_args)[i - 1];
21473 (*call_args)[0] = thisarg;
21474 }
21475 }
21476 ret = build_call_a (function, call_args->length (),
21477 call_args->address ());
21478 /* The thunk location is not interesting. */
21479 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
21480 CALL_FROM_THUNK_P (ret) = true;
21481 if (CLASS_TYPE_P (TREE_TYPE (ret)))
21482 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
21483
21484 RETURN (ret);
21485 }
21486
21487 /* We do not perform argument-dependent lookup if normal
21488 lookup finds a non-function, in accordance with the
21489 resolution of DR 218. */
21490 if (koenig_p
21491 && ((is_overloaded_fn (function)
21492 /* If lookup found a member function, the Koenig lookup is
21493 not appropriate, even if an unqualified-name was used
21494 to denote the function. */
21495 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
21496 || identifier_p (function)
21497 /* C++20 P0846: Lookup found nothing. */
21498 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
21499 && identifier_p (TREE_OPERAND (function, 0))))
21500 /* Only do this when substitution turns a dependent call
21501 into a non-dependent call. */
21502 && type_dependent_expression_p_push (t)
21503 && !any_type_dependent_arguments_p (call_args))
21504 function = perform_koenig_lookup (function, call_args, tf_none);
21505
21506 if (function != NULL_TREE
21507 && (identifier_p (function)
21508 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
21509 && identifier_p (TREE_OPERAND (function, 0))
21510 && !any_dependent_template_arguments_p (TREE_OPERAND
21511 (function, 1))))
21512 && !any_type_dependent_arguments_p (call_args))
21513 {
21514 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
21515 if (template_id_p)
21516 function = TREE_OPERAND (function, 0);
21517 if (koenig_p && (complain & tf_warning_or_error))
21518 {
21519 /* For backwards compatibility and good diagnostics, try
21520 the unqualified lookup again if we aren't in SFINAE
21521 context. */
21522 tree unq = tsubst_expr (function, args, complain, in_decl);
21523 if (unq == error_mark_node)
21524 RETURN (error_mark_node);
21525
21526 if (unq != function)
21527 {
21528 auto_diagnostic_group d;
21529 char const *const msg
21530 = G_("%qD was not declared in this scope, "
21531 "and no declarations were found by "
21532 "argument-dependent lookup at the point "
21533 "of instantiation");
21534
21535 bool in_lambda = (current_class_type
21536 && LAMBDA_TYPE_P (current_class_type));
21537 /* In a lambda fn, we have to be careful to not
21538 introduce new this captures. Legacy code can't
21539 be using lambdas anyway, so it's ok to be
21540 stricter. Be strict with C++20 template-id ADL too.
21541 And be strict if we're already failing anyway. */
21542 bool strict = in_lambda || template_id_p || seen_error ();
21543 bool diag = true;
21544 if (strict)
21545 error_at (cp_expr_loc_or_input_loc (t),
21546 msg, function);
21547 else
21548 diag = permerror (cp_expr_loc_or_input_loc (t),
21549 msg, function);
21550 if (diag)
21551 {
21552 tree fn = unq;
21553
21554 if (INDIRECT_REF_P (fn))
21555 fn = TREE_OPERAND (fn, 0);
21556 if (is_overloaded_fn (fn))
21557 fn = get_first_fn (fn);
21558
21559 if (!DECL_P (fn))
21560 /* Can't say anything more. */;
21561 else if (DECL_CLASS_SCOPE_P (fn))
21562 {
21563 location_t loc = cp_expr_loc_or_input_loc (t);
21564 inform (loc,
21565 "declarations in dependent base %qT are "
21566 "not found by unqualified lookup",
21567 DECL_CLASS_CONTEXT (fn));
21568 if (current_class_ptr)
21569 inform (loc,
21570 "use %<this->%D%> instead", function);
21571 else
21572 inform (loc,
21573 "use %<%T::%D%> instead",
21574 current_class_name, function);
21575 }
21576 else
21577 inform (DECL_SOURCE_LOCATION (fn),
21578 "%qD declared here, later in the "
21579 "translation unit", fn);
21580 if (strict)
21581 RETURN (error_mark_node);
21582 }
21583
21584 function = unq;
21585 }
21586 }
21587 if (identifier_p (function))
21588 {
21589 if (complain & tf_error)
21590 unqualified_name_lookup_error (function);
21591 RETURN (error_mark_node);
21592 }
21593 }
21594
21595 /* Remember that there was a reference to this entity. */
21596 if (function != NULL_TREE
21597 && DECL_P (function)
21598 && !mark_used (function, complain) && !(complain & tf_error))
21599 RETURN (error_mark_node);
21600
21601 if (!maybe_fold_fn_template_args (function, complain))
21602 return error_mark_node;
21603
21604 /* Put back tf_decltype for the actual call. */
21605 complain |= decltype_flag;
21606
21607 if (function == NULL_TREE)
21608 switch (CALL_EXPR_IFN (t))
21609 {
21610 case IFN_LAUNDER:
21611 gcc_assert (nargs == 1);
21612 if (vec_safe_length (call_args) != 1)
21613 {
21614 error_at (cp_expr_loc_or_input_loc (t),
21615 "wrong number of arguments to "
21616 "%<__builtin_launder%>");
21617 ret = error_mark_node;
21618 }
21619 else
21620 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
21621 (*call_args)[0], complain);
21622 break;
21623
21624 case IFN_VEC_CONVERT:
21625 gcc_assert (nargs == 1);
21626 if (vec_safe_length (call_args) != 1)
21627 {
21628 error_at (cp_expr_loc_or_input_loc (t),
21629 "wrong number of arguments to "
21630 "%<__builtin_convertvector%>");
21631 ret = error_mark_node;
21632 break;
21633 }
21634 ret = cp_build_vec_convert ((*call_args)[0], input_location,
21635 tsubst (TREE_TYPE (t), args,
21636 complain, in_decl),
21637 complain);
21638 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
21639 RETURN (ret);
21640 break;
21641
21642 case IFN_SHUFFLEVECTOR:
21643 {
21644 ret = build_x_shufflevector (input_location, call_args,
21645 complain);
21646 if (ret != error_mark_node)
21647 RETURN (ret);
21648 break;
21649 }
21650
21651 case IFN_ASSUME:
21652 gcc_assert (nargs == 1);
21653 if (vec_safe_length (call_args) != 1)
21654 {
21655 error_at (cp_expr_loc_or_input_loc (t),
21656 "wrong number of arguments to "
21657 "%<assume%> attribute");
21658 ret = error_mark_node;
21659 }
21660 else
21661 {
21662 tree &arg = (*call_args)[0];
21663 if (!type_dependent_expression_p (arg))
21664 arg = contextual_conv_bool (arg, tf_warning_or_error);
21665 if (error_operand_p (arg))
21666 {
21667 ret = error_mark_node;
21668 break;
21669 }
21670 ret = build_assume_call (EXPR_LOCATION (t), arg);
21671 RETURN (ret);
21672 }
21673 break;
21674
21675 case IFN_GOMP_DISPATCH:
21676 ret = build_call_expr_internal_loc (EXPR_LOCATION (t),
21677 IFN_GOMP_DISPATCH,
21678 TREE_TYPE (call_args[0]), 1,
21679 call_args[0]);
21680 RETURN (ret);
21681 break;
21682
21683 default:
21684 /* Unsupported internal function with arguments. */
21685 gcc_unreachable ();
21686 }
21687 else if (TREE_CODE (function) == OFFSET_REF
21688 || TREE_CODE (function) == DOTSTAR_EXPR
21689 || TREE_CODE (function) == MEMBER_REF)
21690 ret = build_offset_ref_call_from_tree (function, &call_args,
21691 complain);
21692 else if (concept_check_p (function))
21693 /* Calls to concepts should have been previously diagnosed. */
21694 gcc_assert (false);
21695 else
21696 ret = finish_call_expr (function, &call_args,
21697 /*disallow_virtual=*/qualified_p,
21698 koenig_p,
21699 complain);
21700
21701 if (ret != error_mark_node)
21702 {
21703 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
21704 bool ord = CALL_EXPR_ORDERED_ARGS (t);
21705 bool rev = CALL_EXPR_REVERSE_ARGS (t);
21706 bool mtc = false;
21707 if (TREE_CODE (t) == CALL_EXPR)
21708 mtc = CALL_EXPR_MUST_TAIL_CALL (t);
21709 if (op || ord || rev || mtc)
21710 if (tree call = extract_call_expr (ret))
21711 {
21712 CALL_EXPR_OPERATOR_SYNTAX (call) = op;
21713 CALL_EXPR_ORDERED_ARGS (call) = ord;
21714 CALL_EXPR_REVERSE_ARGS (call) = rev;
21715 if (TREE_CODE (call) == CALL_EXPR)
21716 CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
21717 else if (TREE_CODE (call) == AGGR_INIT_EXPR)
21718 AGGR_INIT_EXPR_MUST_TAIL (call) = mtc;
21719 }
21720 if (CALL_FROM_NEW_OR_DELETE_P (t))
21721 {
21722 tree call = extract_call_expr (ret);
21723 tree fn = cp_get_callee_fndecl_nofold (call);
21724 if (fn ? !DECL_IS_REPLACEABLE_OPERATOR (fn)
21725 : !processing_template_decl)
21726 {
21727 auto_diagnostic_group d;
21728 location_t loc = cp_expr_loc_or_input_loc (t);
21729 if (!fn || IDENTIFIER_NEW_OP_P (DECL_NAME (fn)))
21730 error_at (loc, "call to %<__builtin_operator_new%> "
21731 "does not select replaceable global "
21732 "allocation function");
21733 else
21734 error_at (loc, "call to %<__builtin_operator_delete%> "
21735 "does not select replaceable global "
21736 "deallocation function");
21737 if (fn)
21738 inform (DECL_SOURCE_LOCATION (fn),
21739 "selected function declared here");
21740 }
21741 else if (call && TREE_CODE (call) == CALL_EXPR)
21742 CALL_FROM_NEW_OR_DELETE_P (call) = 1;
21743 }
21744 if (warning_suppressed_p (t, OPT_Wpessimizing_move))
21745 /* This also suppresses -Wredundant-move. */
21746 suppress_warning (ret, OPT_Wpessimizing_move);
21747 }
21748
21749 RETURN (ret);
21750 }
21751
21752 case COND_EXPR:
21753 {
21754 tree cond = RECUR (TREE_OPERAND (t, 0));
21755 cond = mark_rvalue_use (cond);
21756 tree folded_cond = fold_non_dependent_expr (cond, complain);
21757 tree exp1, exp2;
21758
21759 if (TREE_CODE (folded_cond) == INTEGER_CST)
21760 {
21761 if (integer_zerop (folded_cond))
21762 {
21763 ++c_inhibit_evaluation_warnings;
21764 exp1 = RECUR (TREE_OPERAND (t, 1));
21765 --c_inhibit_evaluation_warnings;
21766 exp2 = RECUR (TREE_OPERAND (t, 2));
21767 }
21768 else
21769 {
21770 exp1 = RECUR (TREE_OPERAND (t, 1));
21771 ++c_inhibit_evaluation_warnings;
21772 exp2 = RECUR (TREE_OPERAND (t, 2));
21773 --c_inhibit_evaluation_warnings;
21774 }
21775 cond = folded_cond;
21776 }
21777 else
21778 {
21779 exp1 = RECUR (TREE_OPERAND (t, 1));
21780 exp2 = RECUR (TREE_OPERAND (t, 2));
21781 }
21782
21783 warning_sentinel s(warn_duplicated_branches);
21784 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
21785 cond, exp1, exp2, complain));
21786 }
21787
21788 case PSEUDO_DTOR_EXPR:
21789 {
21790 tree op0 = RECUR (TREE_OPERAND (t, 0));
21791 tree op1 = RECUR (TREE_OPERAND (t, 1));
21792 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
21793 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
21794 input_location, complain));
21795 }
21796
21797 case TREE_LIST:
21798 RETURN (tsubst_tree_list (t, args, complain, in_decl));
21799
21800 case COMPONENT_REF:
21801 {
21802 tree object;
21803 tree object_type;
21804 tree member;
21805 tree r;
21806
21807 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21808 args, complain, in_decl);
21809 /* Remember that there was a reference to this entity. */
21810 if (DECL_P (object)
21811 && !mark_used (object, complain) && !(complain & tf_error))
21812 RETURN (error_mark_node);
21813 object_type = TREE_TYPE (object);
21814
21815 member = TREE_OPERAND (t, 1);
21816 if (BASELINK_P (member))
21817 member = tsubst_baselink (member,
21818 non_reference (TREE_TYPE (object)),
21819 args, complain, in_decl);
21820 else
21821 member = tsubst_name (member, args, complain, in_decl);
21822 if (member == error_mark_node)
21823 RETURN (error_mark_node);
21824
21825 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
21826 && TREE_CODE (member) == FIELD_DECL)
21827 {
21828 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
21829 RETURN (r);
21830 }
21831 else if (TREE_CODE (member) == FIELD_DECL)
21832 {
21833 /* Assume access of this FIELD_DECL has already been checked; we
21834 don't recheck it to avoid bogus access errors when substituting
21835 a reduced constant initializer (97740). */
21836 gcc_checking_assert (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL);
21837 push_deferring_access_checks (dk_deferred);
21838 r = finish_non_static_data_member (member, object, NULL_TREE,
21839 complain);
21840 pop_deferring_access_checks ();
21841 if (REF_PARENTHESIZED_P (t))
21842 r = force_paren_expr (r);
21843 RETURN (r);
21844 }
21845 else if (type_dependent_expression_p (object))
21846 /* We can't do much here. */;
21847 else if (!CLASS_TYPE_P (object_type))
21848 {
21849 if (scalarish_type_p (object_type))
21850 {
21851 tree s = NULL_TREE;
21852 tree dtor = member;
21853
21854 if (TREE_CODE (dtor) == SCOPE_REF)
21855 {
21856 s = TREE_OPERAND (dtor, 0);
21857 dtor = TREE_OPERAND (dtor, 1);
21858 }
21859 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
21860 {
21861 dtor = TREE_OPERAND (dtor, 0);
21862 if (TYPE_P (dtor))
21863 RETURN (finish_pseudo_destructor_expr
21864 (object, s, dtor, input_location, complain));
21865 }
21866 }
21867 }
21868 else if (TREE_CODE (member) == SCOPE_REF
21869 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR
21870 && identifier_p (TREE_OPERAND (TREE_OPERAND (member, 1), 0)))
21871 {
21872 /* Lookup the template functions now that we know what the
21873 scope is. */
21874 tree scope = TREE_OPERAND (member, 0);
21875 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
21876 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
21877 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
21878 /*complain=*/false);
21879 if (BASELINK_P (member))
21880 {
21881 BASELINK_FUNCTIONS (member)
21882 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
21883 args);
21884 member = (adjust_result_of_qualified_name_lookup
21885 (member, BINFO_TYPE (BASELINK_BINFO (member)),
21886 object_type));
21887 }
21888 else
21889 {
21890 qualified_name_lookup_error (scope, tmpl, member,
21891 input_location);
21892 RETURN (error_mark_node);
21893 }
21894 }
21895 else if (TREE_CODE (member) == SCOPE_REF
21896 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
21897 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
21898 {
21899 if (complain & tf_error)
21900 {
21901 if (TYPE_P (TREE_OPERAND (member, 0)))
21902 error ("%qT is not a class or namespace",
21903 TREE_OPERAND (member, 0));
21904 else
21905 error ("%qD is not a class or namespace",
21906 TREE_OPERAND (member, 0));
21907 }
21908 RETURN (error_mark_node);
21909 }
21910
21911 r = finish_class_member_access_expr (object, member,
21912 /*template_p=*/false,
21913 complain);
21914 if (REF_PARENTHESIZED_P (t))
21915 r = force_paren_expr (r);
21916 RETURN (r);
21917 }
21918
21919 case THROW_EXPR:
21920 RETURN (build_throw
21921 (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
21922
21923 case CONSTRUCTOR:
21924 {
21925 vec<constructor_elt, va_gc> *n;
21926 constructor_elt *ce;
21927 unsigned HOST_WIDE_INT idx;
21928 bool process_index_p;
21929 int newlen;
21930 bool need_copy_p = false;
21931 tree r;
21932
21933 tsubst_flags_t tcomplain = complain;
21934 if (COMPOUND_LITERAL_P (t))
21935 tcomplain |= tf_tst_ok;
21936 tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
21937 if (type == error_mark_node)
21938 RETURN (error_mark_node);
21939
21940 /* We do not want to process the index of aggregate
21941 initializers as they are identifier nodes which will be
21942 looked up by digest_init. */
21943 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
21944
21945 if (null_member_pointer_value_p (t))
21946 {
21947 gcc_assert (same_type_p (type, TREE_TYPE (t)));
21948 RETURN (t);
21949 }
21950
21951 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
21952 newlen = vec_safe_length (n);
21953 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
21954 {
21955 if (ce->index && process_index_p
21956 /* An identifier index is looked up in the type
21957 being initialized, not the current scope. */
21958 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
21959 ce->index = RECUR (ce->index);
21960
21961 if (PACK_EXPANSION_P (ce->value))
21962 {
21963 /* Substitute into the pack expansion. */
21964 ce->value = tsubst_pack_expansion (ce->value, args, complain,
21965 in_decl);
21966
21967 if (ce->value == error_mark_node
21968 || PACK_EXPANSION_P (ce->value))
21969 ;
21970 else if (TREE_VEC_LENGTH (ce->value) == 1)
21971 /* Just move the argument into place. */
21972 ce->value = TREE_VEC_ELT (ce->value, 0);
21973 else
21974 {
21975 /* Update the length of the final CONSTRUCTOR
21976 arguments vector, and note that we will need to
21977 copy.*/
21978 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
21979 need_copy_p = true;
21980 }
21981 }
21982 else
21983 ce->value = RECUR (ce->value);
21984 }
21985
21986 if (need_copy_p)
21987 {
21988 vec<constructor_elt, va_gc> *old_n = n;
21989
21990 vec_alloc (n, newlen);
21991 FOR_EACH_VEC_ELT (*old_n, idx, ce)
21992 {
21993 if (TREE_CODE (ce->value) == TREE_VEC)
21994 {
21995 int i, len = TREE_VEC_LENGTH (ce->value);
21996 for (i = 0; i < len; ++i)
21997 CONSTRUCTOR_APPEND_ELT (n, 0,
21998 TREE_VEC_ELT (ce->value, i));
21999 }
22000 else
22001 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
22002 }
22003 }
22004
22005 r = build_constructor (init_list_type_node, n);
22006 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
22007 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
22008 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
22009
22010 if (TREE_HAS_CONSTRUCTOR (t))
22011 {
22012 fcl_t cl = fcl_functional;
22013 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
22014 cl = fcl_c99;
22015 RETURN (finish_compound_literal (type, r, complain, cl));
22016 }
22017
22018 TREE_TYPE (r) = type;
22019 RETURN (r);
22020 }
22021
22022 case TYPEID_EXPR:
22023 {
22024 tree operand_0 = TREE_OPERAND (t, 0);
22025 if (TYPE_P (operand_0))
22026 {
22027 operand_0 = tsubst (operand_0, args, complain, in_decl);
22028 RETURN (get_typeid (operand_0, complain));
22029 }
22030 else
22031 {
22032 operand_0 = RECUR (operand_0);
22033 RETURN (build_typeid (operand_0, complain));
22034 }
22035 }
22036
22037 case FUNCTION_DECL:
22038 case PARM_DECL:
22039 case VAR_DECL:
22040 if (!args)
22041 RETURN (t);
22042 tree r;
22043 if (VAR_OR_FUNCTION_DECL_P (t)
22044 && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
22045 r = tsubst_decl (t, args, complain);
22046 else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
22047 {
22048 /* Local specialization will usually have been created when
22049 we instantiated the DECL_EXPR_DECL. */
22050 r = retrieve_local_specialization (t);
22051 if (!r)
22052 {
22053 /* We're in a generic lambda referencing a local extern
22054 from an outer block-scope of a non-template. */
22055 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
22056 r = t;
22057 }
22058 }
22059 else if (local_variable_p (t)
22060 && ((r = retrieve_local_specialization (t))
22061 || TREE_CODE (t) == PARM_DECL
22062 || uses_template_parms (DECL_CONTEXT (t))))
22063 {
22064 if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
22065 {
22066 /* We get here for a use of 'this' in an NSDMI. */
22067 if (DECL_NAME (t) == this_identifier && current_class_ptr
22068 && !LAMBDA_TYPE_P (TREE_TYPE (TREE_TYPE (current_class_ptr))))
22069 RETURN (current_class_ptr);
22070
22071 /* This can happen for a parameter name used later in a function
22072 declaration (such as in a late-specified return type). Just
22073 make a dummy decl, since it's only used for its type. */
22074 gcc_assert (cp_unevaluated_operand);
22075 r = tsubst_decl (t, args, complain);
22076 /* Give it the template pattern as its context; its true context
22077 hasn't been instantiated yet and this is good enough for
22078 mangling. */
22079 DECL_CONTEXT (r) = DECL_CONTEXT (t);
22080 }
22081 else if (r == NULL_TREE)
22082 {
22083 /* First try name lookup to find the instantiation. */
22084 if (DECL_NAME (t))
22085 r = lookup_name (DECL_NAME (t));
22086 if (r)
22087 {
22088 if (!VAR_P (r))
22089 {
22090 /* During error-recovery we may find a non-variable,
22091 even an OVERLOAD: just bail out and avoid ICEs and
22092 duplicate diagnostics (c++/62207). */
22093 gcc_assert (seen_error ());
22094 RETURN (error_mark_node);
22095 }
22096 if (!is_capture_proxy (r))
22097 {
22098 /* Make sure the one we found is the one we want. */
22099 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
22100 if (ctx != DECL_CONTEXT (r))
22101 r = NULL_TREE;
22102 }
22103 }
22104
22105 if (r)
22106 /* OK */;
22107 else
22108 {
22109 /* This can happen for a variable used in a
22110 late-specified return type of a local lambda, or for a
22111 local static or constant. Building a new VAR_DECL
22112 should be OK in all those cases. */
22113 r = tsubst_decl (t, args, complain);
22114 if (local_specializations)
22115 /* Avoid infinite recursion (79640). */
22116 register_local_specialization (r, t);
22117 if (decl_maybe_constant_var_p (r))
22118 {
22119 /* We can't call cp_finish_decl, so handle the
22120 initializer by hand. */
22121 tree init = tsubst_init (DECL_INITIAL (t), r, args,
22122 complain, in_decl);
22123 if (!processing_template_decl)
22124 init = maybe_constant_init (init);
22125 if (processing_template_decl
22126 ? potential_constant_expression (init)
22127 : reduced_constant_expression_p (init))
22128 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
22129 = TREE_CONSTANT (r) = true;
22130 DECL_INITIAL (r) = init;
22131 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
22132 TREE_TYPE (r)
22133 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
22134 complain, adc_variable_type);
22135 }
22136 gcc_assert (cp_unevaluated_operand
22137 || processing_contract_condition
22138 || TREE_STATIC (r)
22139 || decl_constant_var_p (r)
22140 || seen_error ());
22141 if (!processing_template_decl
22142 && !TREE_STATIC (r))
22143 r = process_outer_var_ref (r, complain);
22144 }
22145 /* Remember this for subsequent uses. */
22146 if (local_specializations)
22147 register_local_specialization (r, t);
22148 }
22149 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
22150 r = argument_pack_select_arg (r);
22151 }
22152 else
22153 r = t;
22154 if (!mark_used (r, complain))
22155 RETURN (error_mark_node);
22156
22157 if (!no_name_lookup_flag
22158 && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
22159 {
22160 /* ??? We're doing a subset of finish_id_expression here. */
22161 if (tree wrap = maybe_get_tls_wrapper_call (r))
22162 /* Replace an evaluated use of the thread_local variable with
22163 a call to its wrapper. */
22164 r = wrap;
22165 else if (outer_automatic_var_p (r))
22166 r = process_outer_var_ref (r, complain);
22167
22168 if (!TYPE_REF_P (TREE_TYPE (t)))
22169 /* If the original type was a reference, we'll be wrapped in
22170 the appropriate INDIRECT_REF. */
22171 r = convert_from_reference (r);
22172 }
22173 RETURN (r);
22174
22175 case CONST_DECL:
22176 {
22177 tree enum_type;
22178 tree v;
22179
22180 if (DECL_TEMPLATE_PARM_P (t))
22181 RETURN (RECUR (DECL_INITIAL (t)));
22182 if (!args || !uses_template_parms (DECL_CONTEXT (t)))
22183 RETURN (t);
22184
22185 /* Unfortunately, we cannot just call lookup_name here.
22186 Consider:
22187
22188 template <int I> int f() {
22189 enum E { a = I };
22190 struct S { void g() { E e = a; } };
22191 };
22192
22193 When we instantiate f<7>::S::g(), say, lookup_name is not
22194 clever enough to find f<7>::a. */
22195 enum_type = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
22196
22197 for (v = TYPE_VALUES (enum_type);
22198 v != NULL_TREE;
22199 v = TREE_CHAIN (v))
22200 if (TREE_PURPOSE (v) == DECL_NAME (t))
22201 RETURN (TREE_VALUE (v));
22202
22203 /* We didn't find the name. That should never happen; if
22204 name-lookup found it during preliminary parsing, we
22205 should find it again here during instantiation. */
22206 gcc_unreachable ();
22207 RETURN (t);
22208 }
22209
22210 case FIELD_DECL:
22211 if (DECL_CONTEXT (t))
22212 {
22213 tree ctx;
22214
22215 ctx = tsubst_entering_scope (DECL_CONTEXT (t), args,
22216 complain, in_decl);
22217 if (ctx != DECL_CONTEXT (t))
22218 {
22219 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
22220 if (!r)
22221 {
22222 if (complain & tf_error)
22223 error ("using invalid field %qD", t);
22224 RETURN (error_mark_node);
22225 }
22226 RETURN (r);
22227 }
22228 }
22229 RETURN (t);
22230
22231 case NAMESPACE_DECL:
22232 RETURN (t);
22233
22234 case OVERLOAD:
22235 if (modules_p ())
22236 for (tree ovl : lkp_range (t))
22237 if (TREE_CODE (ovl) == TU_LOCAL_ENTITY)
22238 {
22239 complain_about_tu_local_entity (ovl);
22240 RETURN (error_mark_node);
22241 }
22242 RETURN (t);
22243
22244 case TEMPLATE_DECL:
22245 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
22246 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
22247 args, complain, in_decl));
22248 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
22249 RETURN (tsubst (t, args, complain, in_decl));
22250 else if (DECL_CLASS_SCOPE_P (t)
22251 && uses_template_parms (DECL_CONTEXT (t)))
22252 {
22253 /* Template template argument like the following example need
22254 special treatment:
22255
22256 template <template <class> class TT> struct C {};
22257 template <class T> struct D {
22258 template <class U> struct E {};
22259 C<E> c; // #1
22260 };
22261 D<int> d; // #2
22262
22263 We are processing the template argument `E' in #1 for
22264 the template instantiation #2. Originally, `E' is a
22265 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
22266 have to substitute this with one having context `D<int>'. */
22267
22268 tree context = tsubst_entering_scope (DECL_CONTEXT (t), args,
22269 complain, in_decl);
22270 RETURN (lookup_field (context, DECL_NAME(t), 0, false));
22271 }
22272 else
22273 /* Ordinary template template argument. */
22274 RETURN (t);
22275
22276 case TEMPLATE_PARM_INDEX:
22277 case TYPE_DECL:
22278 RETURN (tsubst (t, args, complain, in_decl));
22279
22280 case CLEANUP_POINT_EXPR:
22281 /* We shouldn't have built any of these during initial template
22282 generation. Instead, they should be built during instantiation
22283 in response to the saved STMT_IS_FULL_EXPR_P setting. */
22284 gcc_unreachable ();
22285
22286 case OFFSET_REF:
22287 {
22288 /* We should only get here for an OFFSET_REF like A::m; a .* in a
22289 template is represented as a DOTSTAR_EXPR. */
22290 gcc_checking_assert
22291 (same_type_p (TREE_TYPE (t), TREE_TYPE (TREE_OPERAND (t, 1))));
22292 tree op0 = RECUR (TREE_OPERAND (t, 0));
22293 tree op1 = RECUR (TREE_OPERAND (t, 1));
22294 tree type = TREE_TYPE (op1);
22295 r = build2 (OFFSET_REF, type, op0, op1);
22296 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
22297 if (!mark_used (op1, complain)
22298 && !(complain & tf_error))
22299 RETURN (error_mark_node);
22300 RETURN (r);
22301 }
22302
22303 case PACK_INDEX_EXPR:
22304 RETURN (tsubst_pack_index (t, args, complain, in_decl));
22305
22306 case EXPR_PACK_EXPANSION:
22307 error ("invalid use of pack expansion expression");
22308 RETURN (error_mark_node);
22309
22310 case NONTYPE_ARGUMENT_PACK:
22311 error ("use %<...%> to expand argument pack");
22312 RETURN (error_mark_node);
22313
22314 case VOID_CST:
22315 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
22316 RETURN (t);
22317
22318 case INTEGER_CST:
22319 case REAL_CST:
22320 case COMPLEX_CST:
22321 case VECTOR_CST:
22322 {
22323 /* Instantiate any typedefs in the type. */
22324 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
22325 r = fold_convert (type, t);
22326 gcc_assert (TREE_CODE (r) == TREE_CODE (t));
22327 RETURN (r);
22328 }
22329
22330 case STRING_CST:
22331 {
22332 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
22333 r = t;
22334 if (type != TREE_TYPE (t))
22335 {
22336 r = copy_node (t);
22337 TREE_TYPE (r) = type;
22338 }
22339 RETURN (r);
22340 }
22341
22342 case RAW_DATA_CST:
22343 {
22344 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
22345 r = copy_node (t);
22346 TREE_TYPE (r) = type;
22347 RETURN (r);
22348 }
22349
22350 case PTRMEM_CST:
22351 /* These can sometimes show up in a partial instantiation, but never
22352 involve template parms. */
22353 gcc_assert (!uses_template_parms (t));
22354 RETURN (t);
22355
22356 case UNARY_LEFT_FOLD_EXPR:
22357 RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
22358 case UNARY_RIGHT_FOLD_EXPR:
22359 RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
22360 case BINARY_LEFT_FOLD_EXPR:
22361 RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
22362 case BINARY_RIGHT_FOLD_EXPR:
22363 RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
22364 case PREDICT_EXPR:
22365 RETURN (t);
22366
22367 case DEBUG_BEGIN_STMT:
22368 /* ??? There's no point in copying it for now, but maybe some
22369 day it will contain more information, such as a pointer back
22370 to the containing function, inlined copy or so. */
22371 RETURN (t);
22372
22373 case CO_YIELD_EXPR:
22374 RETURN (finish_co_yield_expr (input_location,
22375 RECUR (TREE_OPERAND (t, 0))));
22376
22377 case CO_AWAIT_EXPR:
22378 RETURN (finish_co_await_expr (input_location,
22379 RECUR (TREE_OPERAND (t, 0))));
22380
22381 case VA_ARG_EXPR:
22382 {
22383 tree op0 = RECUR (TREE_OPERAND (t, 0));
22384 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
22385 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
22386 }
22387
22388 case OFFSETOF_EXPR:
22389 {
22390 tree object_ptr
22391 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
22392 RETURN (finish_offsetof (object_ptr,
22393 RECUR (TREE_OPERAND (t, 0)),
22394 EXPR_LOCATION (t)));
22395 }
22396
22397 case ADDRESSOF_EXPR:
22398 RETURN (cp_build_addressof (EXPR_LOCATION (t),
22399 RECUR (TREE_OPERAND (t, 0)), complain));
22400
22401 case TRAIT_EXPR:
22402 {
22403 tree type1 = TRAIT_EXPR_TYPE1 (t);
22404 if (TYPE_P (type1))
22405 type1 = tsubst (type1, args, complain, in_decl);
22406 else
22407 type1 = tsubst_expr (type1, args, complain, in_decl);
22408 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
22409 complain, in_decl);
22410 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
22411 TRAIT_EXPR_KIND (t), type1, type2));
22412 }
22413
22414 case STMT_EXPR:
22415 {
22416 tree old_stmt_expr = cur_stmt_expr;
22417 tree stmt_expr = begin_stmt_expr ();
22418
22419 cur_stmt_expr = stmt_expr;
22420 tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
22421 stmt_expr = finish_stmt_expr (stmt_expr, false);
22422 cur_stmt_expr = old_stmt_expr;
22423
22424 /* If the resulting list of expression statement is empty,
22425 fold it further into void_node. */
22426 if (empty_expr_stmt_p (stmt_expr))
22427 stmt_expr = void_node;
22428
22429 RETURN (stmt_expr);
22430 }
22431
22432 case LAMBDA_EXPR:
22433 {
22434 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
22435
22436 RETURN (build_lambda_object (r));
22437 }
22438
22439 case TRANSACTION_EXPR:
22440 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
22441 RETURN (tsubst_stmt (t, args, complain, in_decl));
22442
22443 case PAREN_EXPR:
22444 if (REF_PARENTHESIZED_P (t))
22445 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
22446 else
22447 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
22448 {
22449 tree op0 = RECUR (TREE_OPERAND (t, 0));
22450 RETURN (build1_loc (input_location, PAREN_EXPR,
22451 TREE_TYPE (op0), op0));
22452 }
22453
22454 case VEC_PERM_EXPR:
22455 {
22456 tree op0 = RECUR (TREE_OPERAND (t, 0));
22457 tree op1 = RECUR (TREE_OPERAND (t, 1));
22458 tree op2 = RECUR (TREE_OPERAND (t, 2));
22459 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
22460 complain));
22461 }
22462
22463 case REQUIRES_EXPR:
22464 {
22465 complain &= ~tf_warning_or_error;
22466 tree r = tsubst_requires_expr (t, args, complain, in_decl);
22467 RETURN (r);
22468 }
22469
22470 case RANGE_EXPR:
22471 /* No need to substitute further, a RANGE_EXPR will always be built
22472 with constant operands. */
22473 RETURN (t);
22474
22475 case NON_LVALUE_EXPR:
22476 case VIEW_CONVERT_EXPR:
22477 {
22478 tree op = RECUR (TREE_OPERAND (t, 0));
22479
22480 if (location_wrapper_p (t))
22481 /* We need to do this here as well as in tsubst_copy so we get the
22482 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
22483 RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
22484
22485 gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
22486 if (REF_PARENTHESIZED_P (t))
22487 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
22488 RETURN (finish_parenthesized_expr (op));
22489
22490 /* Otherwise, we're dealing with a wrapper to make a C++20 template
22491 parameter object const. */
22492 if (TREE_TYPE (op) == NULL_TREE
22493 || !CP_TYPE_CONST_P (TREE_TYPE (op)))
22494 {
22495 /* The template argument is not const, presumably because
22496 it is still dependent, and so not the const template parm
22497 object. */
22498 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
22499 if (TREE_CODE (op) == CONSTRUCTOR
22500 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
22501 {
22502 /* Don't add a wrapper to these. */
22503 op = copy_node (op);
22504 TREE_TYPE (op) = type;
22505 }
22506 else
22507 /* Do add a wrapper otherwise (in particular, if op is
22508 another TEMPLATE_PARM_INDEX). */
22509 op = build1 (VIEW_CONVERT_EXPR, type, op);
22510 }
22511 RETURN (op);
22512 }
22513
22514 case TU_LOCAL_ENTITY:
22515 complain_about_tu_local_entity (t);
22516 RETURN (error_mark_node);
22517
22518 default:
22519 /* Handle Objective-C++ constructs, if appropriate. */
22520 if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
22521 RETURN (subst);
22522
22523 /* We shouldn't get here, but keep going if !flag_checking. */
22524 if (flag_checking)
22525 gcc_unreachable ();
22526 RETURN (t);
22527 }
22528
22529 #undef RECUR
22530 #undef RETURN
22531 out:
22532 input_location = save_loc;
22533 return retval;
22534 }
22535
22536 /* Verify that the instantiated ARGS are valid. For type arguments,
22537 make sure that the type's linkage is ok. For non-type arguments,
22538 make sure they are constants if they are integral or enumerations.
22539 Emit an error under control of COMPLAIN, and return TRUE on error. */
22540
22541 static bool
22542 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
22543 {
22544 if (dependent_template_arg_p (t))
22545 return false;
22546 if (ARGUMENT_PACK_P (t))
22547 {
22548 tree vec = ARGUMENT_PACK_ARGS (t);
22549 int len = TREE_VEC_LENGTH (vec);
22550 bool result = false;
22551 int i;
22552
22553 for (i = 0; i < len; ++i)
22554 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
22555 result = true;
22556 return result;
22557 }
22558 else if (TYPE_P (t))
22559 {
22560 /* [basic.link]: A name with no linkage (notably, the name
22561 of a class or enumeration declared in a local scope)
22562 shall not be used to declare an entity with linkage.
22563 This implies that names with no linkage cannot be used as
22564 template arguments
22565
22566 DR 757 relaxes this restriction for C++0x. */
22567 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
22568 : no_linkage_check (t, /*relaxed_p=*/false));
22569
22570 if (nt)
22571 {
22572 /* DR 488 makes use of a type with no linkage cause
22573 type deduction to fail. */
22574 if (complain & tf_error)
22575 {
22576 if (TYPE_UNNAMED_P (nt))
22577 error ("%qT is/uses unnamed type", t);
22578 else
22579 error ("template argument for %qD uses local type %qT",
22580 tmpl, t);
22581 }
22582 return true;
22583 }
22584 /* In order to avoid all sorts of complications, we do not
22585 allow variably-modified types as template arguments. */
22586 else if (variably_modified_type_p (t, NULL_TREE))
22587 {
22588 if (complain & tf_error)
22589 error ("%qT is a variably modified type", t);
22590 return true;
22591 }
22592 }
22593 /* Class template and alias template arguments should be OK. */
22594 else if (DECL_TYPE_TEMPLATE_P (t))
22595 ;
22596 /* A non-type argument of integral or enumerated type must be a
22597 constant. */
22598 else if (TREE_TYPE (t)
22599 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
22600 && !REFERENCE_REF_P (t)
22601 && !TREE_CONSTANT (t))
22602 {
22603 if (complain & tf_error)
22604 error ("integral expression %qE is not constant", t);
22605 return true;
22606 }
22607 return false;
22608 }
22609
22610 static bool
22611 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
22612 {
22613 int ix, len = DECL_NTPARMS (tmpl);
22614 bool result = false;
22615
22616 for (ix = 0; ix != len; ix++)
22617 {
22618 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
22619 result = true;
22620 }
22621 if (result && (complain & tf_error))
22622 error (" trying to instantiate %qD", tmpl);
22623 return result;
22624 }
22625
22626 /* Call mark_used on each entity within the non-type template arguments in
22627 ARGS for an instantiation of TMPL, to ensure that each such entity is
22628 considered odr-used (and therefore marked for instantiation) regardless of
22629 whether the specialization was first formed in a template context (which
22630 inhibits mark_used).
22631
22632 This function assumes push_to_top_level has been called beforehand. */
22633
22634 static void
22635 mark_template_arguments_used (tree tmpl, tree args)
22636 {
22637 /* It suffices to do this only when instantiating a primary template. */
22638 if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
22639 return;
22640
22641 /* We already marked outer arguments when specializing the context. */
22642 args = INNERMOST_TEMPLATE_ARGS (args);
22643
22644 for (tree arg : tree_vec_range (args))
22645 {
22646 /* A (pointer/reference to) function or variable NTTP argument. */
22647 if (TREE_CODE (arg) == ADDR_EXPR
22648 || TREE_CODE (arg) == INDIRECT_REF)
22649 {
22650 while (TREE_CODE (arg) == ADDR_EXPR
22651 || REFERENCE_REF_P (arg)
22652 || CONVERT_EXPR_P (arg))
22653 arg = TREE_OPERAND (arg, 0);
22654 if (VAR_OR_FUNCTION_DECL_P (arg))
22655 {
22656 /* Pass tf_none to avoid duplicate diagnostics: if this call
22657 fails then an earlier call to mark_used for this argument
22658 must have also failed and emitted a diagnostic. */
22659 bool ok = mark_used (arg, tf_none);
22660 gcc_checking_assert (ok || seen_error ());
22661 }
22662 }
22663 /* A member function pointer. */
22664 else if (TREE_CODE (arg) == PTRMEM_CST)
22665 {
22666 bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none);
22667 gcc_checking_assert (ok || seen_error ());
22668 }
22669 /* A class NTTP argument. */
22670 else if (VAR_P (arg)
22671 && DECL_NTTP_OBJECT_P (arg))
22672 {
22673 auto mark_used_r = [](tree *tp, int *, void *) {
22674 if (VAR_OR_FUNCTION_DECL_P (*tp))
22675 {
22676 bool ok = mark_used (*tp, tf_none);
22677 gcc_checking_assert (ok || seen_error ());
22678 }
22679 return NULL_TREE;
22680 };
22681 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
22682 mark_used_r, nullptr);
22683 }
22684 }
22685 }
22686
22687 /* We're out of SFINAE context now, so generate diagnostics for the access
22688 errors we saw earlier when instantiating D from TMPL and ARGS. */
22689
22690 static void
22691 recheck_decl_substitution (tree d, tree tmpl, tree args)
22692 {
22693 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
22694 tree type = TREE_TYPE (pattern);
22695 location_t loc = input_location;
22696
22697 push_access_scope (d);
22698 push_deferring_access_checks (dk_no_deferred);
22699 input_location = DECL_SOURCE_LOCATION (pattern);
22700 tsubst (type, args, tf_warning_or_error, d);
22701 input_location = loc;
22702 pop_deferring_access_checks ();
22703 pop_access_scope (d);
22704 }
22705
22706 /* Instantiate the indicated variable, function, or alias template TMPL with
22707 the template arguments in TARG_PTR. */
22708
22709 tree
22710 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
22711 {
22712 auto_timevar tv (TV_TEMPLATE_INST);
22713
22714 tree targ_ptr = orig_args;
22715 tree fndecl;
22716 tree gen_tmpl;
22717 bool access_ok = true;
22718
22719 if (tmpl == error_mark_node)
22720 return error_mark_node;
22721
22722 /* The other flags are not relevant anymore here, especially tf_partial
22723 shouldn't be set. For instance, we may be called while doing a partial
22724 substitution of a template variable, but the type of the variable
22725 template may be auto, in which case we will call do_auto_deduction
22726 in mark_used (which clears tf_partial) and the auto must be properly
22727 reduced at that time for the deduction to work.
22728
22729 Note that later below we set tf_partial iff there are dependent arguments;
22730 the above is concerned specifically about the non-dependent case. */
22731 complain &= tf_warning_or_error;
22732
22733 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
22734
22735 if (modules_p ())
22736 lazy_load_pendings (tmpl);
22737
22738 /* If this function is a clone, handle it specially. */
22739 if (DECL_CLONED_FUNCTION_P (tmpl))
22740 {
22741 tree spec;
22742 tree clone;
22743
22744 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
22745 DECL_CLONED_FUNCTION. */
22746 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
22747 targ_ptr, complain);
22748 if (spec == error_mark_node)
22749 return error_mark_node;
22750
22751 /* Look for the clone. */
22752 FOR_EACH_CLONE (clone, spec)
22753 if (DECL_NAME (clone) == DECL_NAME (tmpl))
22754 return clone;
22755 /* We should always have found the clone by now. */
22756 gcc_unreachable ();
22757 return NULL_TREE;
22758 }
22759
22760 if (targ_ptr == error_mark_node)
22761 return error_mark_node;
22762
22763 /* Check to see if we already have this specialization. */
22764 gen_tmpl = most_general_template (tmpl);
22765 if (TMPL_ARGS_DEPTH (targ_ptr)
22766 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
22767 /* targ_ptr only has the innermost template args, so add the outer ones
22768 from tmpl, which could be either a partial instantiation or gen_tmpl (in
22769 the case of a non-dependent call within a template definition). */
22770 targ_ptr = (add_outermost_template_args
22771 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
22772 targ_ptr));
22773
22774 hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
22775 tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
22776
22777 gcc_checking_assert (tmpl == gen_tmpl
22778 || ((fndecl
22779 = retrieve_specialization (tmpl, orig_args, 0))
22780 == spec)
22781 || fndecl == NULL_TREE);
22782
22783 if (spec != NULL_TREE)
22784 {
22785 if (FNDECL_HAS_ACCESS_ERRORS (spec))
22786 {
22787 if (complain & tf_error)
22788 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
22789 return error_mark_node;
22790 }
22791 return spec;
22792 }
22793
22794 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
22795 complain))
22796 return error_mark_node;
22797
22798 /* We are building a FUNCTION_DECL, during which the access of its
22799 parameters and return types have to be checked. However this
22800 FUNCTION_DECL which is the desired context for access checking
22801 is not built yet. We solve this chicken-and-egg problem by
22802 deferring all checks until we have the FUNCTION_DECL. */
22803 deferring_access_check_sentinel dacs (dk_deferred);
22804
22805 /* Instantiation of the function happens in the context of the function
22806 template, not the context of the overload resolution we're doing. */
22807 push_to_top_level ();
22808 /* If there are dependent arguments, e.g. because we're doing partial
22809 ordering, make sure processing_template_decl stays set. And set
22810 tf_partial mainly for benefit of instantiation of alias templates
22811 that contain extra-args trees. */
22812 if (uses_template_parms (targ_ptr))
22813 {
22814 ++processing_template_decl;
22815 complain |= tf_partial;
22816 }
22817 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22818 {
22819 tree ctx;
22820 if (!uses_template_parms (DECL_CONTEXT (tmpl)))
22821 /* If the context of the partially instantiated template is
22822 already non-dependent, then we might as well use it. */
22823 ctx = DECL_CONTEXT (tmpl);
22824 else
22825 ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), targ_ptr,
22826 complain, gen_tmpl);
22827 push_nested_class (ctx);
22828 }
22829
22830 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
22831
22832 tree partial_ti = NULL_TREE;
22833 fndecl = NULL_TREE;
22834 if (VAR_P (pattern))
22835 {
22836 /* We need to determine if we're using a partial or explicit
22837 specialization now, because the type of the variable could be
22838 different. */
22839 tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
22840 partial_ti = most_specialized_partial_spec (tid, complain);
22841 if (partial_ti == error_mark_node)
22842 pattern = error_mark_node;
22843 else if (partial_ti)
22844 {
22845 tree partial_tmpl = TI_TEMPLATE (partial_ti);
22846 tree partial_args = TI_ARGS (partial_ti);
22847 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
22848 fndecl = tsubst_decl (partial_pat, partial_args, complain,
22849 /*use_spec_table=*/false);
22850 }
22851 }
22852
22853 /* Substitute template parameters to obtain the specialization. */
22854 if (fndecl == NULL_TREE)
22855 fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
22856 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22857 pop_nested_class ();
22858 pop_from_top_level ();
22859
22860 if (fndecl == error_mark_node)
22861 return error_mark_node;
22862
22863 /* Substituting the type might have recursively instantiated this
22864 same alias (c++/117530). */
22865 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)
22866 && (spec = retrieve_specialization (gen_tmpl, targ_ptr, hash)))
22867 return spec;
22868
22869 /* The DECL_TI_TEMPLATE should always be the immediate parent
22870 template, not the most general template. */
22871 DECL_TI_TEMPLATE (fndecl) = tmpl;
22872 DECL_TI_ARGS (fndecl) = targ_ptr;
22873 if (VAR_P (pattern))
22874 {
22875 /* Now that we we've formed this variable template specialization,
22876 remember the result of most_specialized_partial_spec for it. */
22877 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
22878
22879 /* And remember if the variable was declared with []. */
22880 if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
22881 && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
22882 SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
22883 }
22884
22885 fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
22886 if (fndecl == error_mark_node)
22887 return error_mark_node;
22888
22889 set_instantiating_module (fndecl);
22890
22891 /* Now we know the specialization, compute access previously
22892 deferred. Do no access control for inheriting constructors,
22893 as we already checked access for the inherited constructor. */
22894 if (!(flag_new_inheriting_ctors
22895 && DECL_INHERITED_CTOR (fndecl)))
22896 {
22897 push_access_scope (fndecl);
22898 if (!perform_deferred_access_checks (complain))
22899 access_ok = false;
22900 pop_access_scope (fndecl);
22901 }
22902
22903 /* If we've just instantiated the main entry point for a function,
22904 instantiate all the alternate entry points as well. We do this
22905 by cloning the instantiation of the main entry point, not by
22906 instantiating the template clones. */
22907 if (tree chain = DECL_CHAIN (gen_tmpl))
22908 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
22909 clone_cdtor (fndecl, /*update_methods=*/false);
22910
22911 if (!access_ok)
22912 {
22913 if (!(complain & tf_error))
22914 {
22915 /* Remember to reinstantiate when we're out of SFINAE so the user
22916 can see the errors. */
22917 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
22918 }
22919 return error_mark_node;
22920 }
22921
22922 return fndecl;
22923 }
22924
22925 /* Instantiate the alias template TMPL with ARGS. Also push a template
22926 instantiation level, which instantiate_template doesn't do because
22927 functions and variables have sufficient context established by the
22928 callers. */
22929
22930 static tree
22931 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
22932 {
22933 if (tmpl == error_mark_node || args == error_mark_node)
22934 return error_mark_node;
22935
22936 args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
22937 args, tmpl, complain);
22938 if (args == error_mark_node)
22939 return error_mark_node;
22940
22941 /* FIXME check for satisfaction in check_instantiated_args. */
22942 if (!constraints_satisfied_p (tmpl, args))
22943 {
22944 if (complain & tf_error)
22945 {
22946 auto_diagnostic_group d;
22947 error ("template constraint failure for %qD", tmpl);
22948 diagnose_constraints (input_location, tmpl, args);
22949 }
22950 return error_mark_node;
22951 }
22952
22953 if (!push_tinst_level (tmpl, args))
22954 return error_mark_node;
22955 tree r = instantiate_template (tmpl, args, complain);
22956 pop_tinst_level ();
22957
22958 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
22959 {
22960 /* Note this is also done at parse time from push_template_decl. */
22961 /* An alias template specialization can be dependent
22962 even if its underlying type is not. */
22963 TYPE_DEPENDENT_P (d) = true;
22964 TYPE_DEPENDENT_P_VALID (d) = true;
22965 /* Sometimes a dependent alias spec is equivalent to its expansion,
22966 sometimes not. So always use structural_comptypes. */
22967 SET_TYPE_STRUCTURAL_EQUALITY (d);
22968 }
22969
22970 return r;
22971 }
22972
22973 /* PARM is a template parameter pack for FN. Returns true iff
22974 PARM is used in a deducible way in the argument list of FN. */
22975
22976 static bool
22977 pack_deducible_p (tree parm, tree fn)
22978 {
22979 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
22980 for (; t; t = TREE_CHAIN (t))
22981 {
22982 tree type = TREE_VALUE (t);
22983 tree packs;
22984 if (!PACK_EXPANSION_P (type))
22985 continue;
22986 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
22987 packs; packs = TREE_CHAIN (packs))
22988 if (template_args_equal (TREE_VALUE (packs), parm))
22989 {
22990 /* The template parameter pack is used in a function parameter
22991 pack. If this is the end of the parameter list, the
22992 template parameter pack is deducible. */
22993 if (TREE_CHAIN (t) == void_list_node)
22994 return true;
22995 else
22996 /* Otherwise, not. Well, it could be deduced from
22997 a non-pack parameter, but doing so would end up with
22998 a deduction mismatch, so don't bother. */
22999 return false;
23000 }
23001 }
23002 /* The template parameter pack isn't used in any function parameter
23003 packs, but it might be used deeper, e.g. tuple<Args...>. */
23004 return true;
23005 }
23006
23007 /* Subroutine of fn_type_unification: check non-dependent parms for
23008 convertibility. */
23009
23010 static int
23011 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
23012 tree fn, unification_kind_t strict, int flags,
23013 struct conversion **convs, bool explain_p,
23014 bool noninst_only_p)
23015 {
23016 /* Non-constructor methods need to leave a conversion for 'this', which
23017 isn't included in nargs here. */
23018 unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
23019 && !DECL_CONSTRUCTOR_P (fn));
23020
23021 for (unsigned ia = 0;
23022 parms && parms != void_list_node && ia < nargs; )
23023 {
23024 tree parm = TREE_VALUE (parms);
23025
23026 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
23027 && (!TREE_CHAIN (parms)
23028 || TREE_CHAIN (parms) == void_list_node))
23029 /* For a function parameter pack that occurs at the end of the
23030 parameter-declaration-list, the type A of each remaining
23031 argument of the call is compared with the type P of the
23032 declarator-id of the function parameter pack. */
23033 break;
23034
23035 parms = TREE_CHAIN (parms);
23036
23037 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
23038 /* For a function parameter pack that does not occur at the
23039 end of the parameter-declaration-list, the type of the
23040 parameter pack is a non-deduced context. */
23041 continue;
23042
23043 if (!uses_template_parms (parm))
23044 {
23045 tree arg = args[ia];
23046 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
23047 int lflags = conv_flags (ia, nargs, fn, arg, flags);
23048
23049 if (check_non_deducible_conversion (parm, arg, strict, lflags,
23050 conv_p, explain_p, noninst_only_p))
23051 return 1;
23052 }
23053
23054 ++ia;
23055 }
23056
23057 return 0;
23058 }
23059
23060 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
23061 NARGS elements of the arguments that are being used when calling
23062 it. TARGS is a vector into which the deduced template arguments
23063 are placed.
23064
23065 Returns either a FUNCTION_DECL for the matching specialization of FN or
23066 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
23067 true, diagnostics will be printed to explain why it failed.
23068
23069 If FN is a conversion operator, or we are trying to produce a specific
23070 specialization, RETURN_TYPE is the return type desired.
23071
23072 The EXPLICIT_TARGS are explicit template arguments provided via a
23073 template-id.
23074
23075 The parameter STRICT is one of:
23076
23077 DEDUCE_CALL:
23078 We are deducing arguments for a function call, as in
23079 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
23080 deducing arguments for a call to the result of a conversion
23081 function template, as in [over.call.object].
23082
23083 DEDUCE_CONV:
23084 We are deducing arguments for a conversion function, as in
23085 [temp.deduct.conv].
23086
23087 DEDUCE_EXACT:
23088 We are deducing arguments when doing an explicit instantiation
23089 as in [temp.explicit], when determining an explicit specialization
23090 as in [temp.expl.spec], or when taking the address of a function
23091 template, as in [temp.deduct.funcaddr]. */
23092
23093 tree
23094 fn_type_unification (tree fn,
23095 tree explicit_targs,
23096 tree targs,
23097 const tree *args,
23098 unsigned int nargs,
23099 tree return_type,
23100 unification_kind_t strict,
23101 int flags,
23102 struct conversion **convs,
23103 bool explain_p,
23104 bool decltype_p)
23105 {
23106 tree parms;
23107 tree fntype;
23108 tree decl = NULL_TREE;
23109 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
23110 bool ok;
23111 static int deduction_depth;
23112 /* type_unification_real will pass back any access checks from default
23113 template argument substitution. */
23114 vec<deferred_access_check, va_gc> *checks = NULL;
23115 /* We don't have all the template args yet. */
23116 bool incomplete = true;
23117
23118 tree orig_fn = fn;
23119 if (flag_new_inheriting_ctors)
23120 fn = strip_inheriting_ctors (fn);
23121
23122 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
23123 tree r = error_mark_node;
23124
23125 tree full_targs = targs;
23126 if (TMPL_ARGS_DEPTH (targs)
23127 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
23128 full_targs = (add_outermost_template_args
23129 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
23130 targs));
23131
23132 if (decltype_p)
23133 complain |= tf_decltype;
23134
23135 /* In C++0x, it's possible to have a function template whose type depends
23136 on itself recursively. This is most obvious with decltype, but can also
23137 occur with enumeration scope (c++/48969). So we need to catch infinite
23138 recursion and reject the substitution at deduction time; this function
23139 will return error_mark_node for any repeated substitution.
23140
23141 This also catches excessive recursion such as when f<N> depends on
23142 f<N-1> across all integers, and returns error_mark_node for all the
23143 substitutions back up to the initial one.
23144
23145 This is, of course, not reentrant. */
23146 if (excessive_deduction_depth)
23147 return error_mark_node;
23148 ++deduction_depth;
23149
23150 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
23151
23152 fntype = TREE_TYPE (fn);
23153 if (explicit_targs)
23154 {
23155 /* [temp.deduct]
23156
23157 The specified template arguments must match the template
23158 parameters in kind (i.e., type, nontype, template), and there
23159 must not be more arguments than there are parameters;
23160 otherwise type deduction fails.
23161
23162 Nontype arguments must match the types of the corresponding
23163 nontype template parameters, or must be convertible to the
23164 types of the corresponding nontype parameters as specified in
23165 _temp.arg.nontype_, otherwise type deduction fails.
23166
23167 All references in the function type of the function template
23168 to the corresponding template parameters are replaced by the
23169 specified template argument values. If a substitution in a
23170 template parameter or in the function type of the function
23171 template results in an invalid type, type deduction fails. */
23172 int i, len = TREE_VEC_LENGTH (tparms);
23173 location_t loc = input_location;
23174 incomplete = false;
23175
23176 if (explicit_targs == error_mark_node)
23177 goto fail;
23178
23179 if (TMPL_ARGS_DEPTH (explicit_targs)
23180 < TMPL_ARGS_DEPTH (full_targs))
23181 explicit_targs = add_outermost_template_args (full_targs,
23182 explicit_targs);
23183
23184 /* Adjust any explicit template arguments before entering the
23185 substitution context. */
23186 explicit_targs
23187 = (coerce_template_parms (tparms, explicit_targs, fn,
23188 complain|tf_partial,
23189 /*require_all_args=*/false));
23190 if (explicit_targs == error_mark_node)
23191 goto fail;
23192
23193 /* Substitute the explicit args into the function type. This is
23194 necessary so that, for instance, explicitly declared function
23195 arguments can match null pointed constants. If we were given
23196 an incomplete set of explicit args, we must not do semantic
23197 processing during substitution as we could create partial
23198 instantiations. */
23199 for (i = 0; i < len; i++)
23200 {
23201 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
23202 bool parameter_pack = false;
23203 tree targ = TREE_VEC_ELT (explicit_targs, i);
23204
23205 /* Dig out the actual parm. */
23206 if (TREE_CODE (parm) == TYPE_DECL
23207 || TREE_CODE (parm) == TEMPLATE_DECL)
23208 {
23209 parm = TREE_TYPE (parm);
23210 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
23211 }
23212 else if (TREE_CODE (parm) == PARM_DECL)
23213 {
23214 parm = DECL_INITIAL (parm);
23215 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
23216 }
23217
23218 if (targ == NULL_TREE)
23219 /* No explicit argument for this template parameter. */
23220 incomplete = true;
23221 else if (parameter_pack && pack_deducible_p (parm, fn))
23222 {
23223 /* Mark the argument pack as "incomplete". We could
23224 still deduce more arguments during unification.
23225 We remove this mark in type_unification_real. */
23226 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
23227 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
23228 = ARGUMENT_PACK_ARGS (targ);
23229
23230 /* We have some incomplete argument packs. */
23231 incomplete = true;
23232 }
23233 }
23234
23235 if (incomplete)
23236 {
23237 if (!push_tinst_level (fn, explicit_targs))
23238 {
23239 excessive_deduction_depth = true;
23240 goto fail;
23241 }
23242 ++processing_template_decl;
23243 input_location = DECL_SOURCE_LOCATION (fn);
23244 /* Ignore any access checks; we'll see them again in
23245 instantiate_template and they might have the wrong
23246 access path at this point. */
23247 push_deferring_access_checks (dk_deferred);
23248 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
23249 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
23250 pop_deferring_access_checks ();
23251 input_location = loc;
23252 --processing_template_decl;
23253 pop_tinst_level ();
23254
23255 if (fntype == error_mark_node)
23256 goto fail;
23257 }
23258
23259 /* Place the explicitly specified arguments in TARGS. */
23260 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
23261 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
23262 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
23263 if (!incomplete && CHECKING_P
23264 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23265 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
23266 (targs, NUM_TMPL_ARGS (explicit_targs));
23267 }
23268
23269 if (return_type && strict != DEDUCE_CALL)
23270 {
23271 tree *new_args = XALLOCAVEC (tree, nargs + 1);
23272 new_args[0] = return_type;
23273 memcpy (new_args + 1, args, nargs * sizeof (tree));
23274 args = new_args;
23275 ++nargs;
23276 }
23277
23278 if (!incomplete)
23279 goto deduced;
23280
23281 /* Never do unification on the 'this' parameter. */
23282 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
23283
23284 if (return_type && strict == DEDUCE_CALL)
23285 {
23286 /* We're deducing for a call to the result of a template conversion
23287 function. The parms we really want are in return_type. */
23288 if (INDIRECT_TYPE_P (return_type))
23289 return_type = TREE_TYPE (return_type);
23290 parms = TYPE_ARG_TYPES (return_type);
23291 }
23292 else if (return_type)
23293 {
23294 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
23295 }
23296
23297 /* We allow incomplete unification without an error message here
23298 because the standard doesn't seem to explicitly prohibit it. Our
23299 callers must be ready to deal with unification failures in any
23300 event. */
23301
23302 /* If we aren't explaining yet, push tinst context so we can see where
23303 any errors (e.g. from class instantiations triggered by instantiation
23304 of default template arguments) come from. If we are explaining, this
23305 context is redundant. */
23306 if (!explain_p && !push_tinst_level (fn, targs))
23307 {
23308 excessive_deduction_depth = true;
23309 goto fail;
23310 }
23311
23312 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23313 full_targs, parms, args, nargs, /*subr=*/0,
23314 strict, &checks, explain_p);
23315 if (!explain_p)
23316 pop_tinst_level ();
23317 if (!ok)
23318 goto fail;
23319
23320 /* Now that we have bindings for all of the template arguments,
23321 ensure that the arguments deduced for the template template
23322 parameters have compatible template parameter lists. We cannot
23323 check this property before we have deduced all template
23324 arguments, because the template parameter types of a template
23325 template parameter might depend on prior template parameters
23326 deduced after the template template parameter. The following
23327 ill-formed example illustrates this issue:
23328
23329 template<typename T, template<T> class C> void f(C<5>, T);
23330
23331 template<int N> struct X {};
23332
23333 void g() {
23334 f(X<5>(), 5l); // error: template argument deduction fails
23335 }
23336
23337 The template parameter list of 'C' depends on the template type
23338 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
23339 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
23340 time that we deduce 'C'. */
23341 if (!template_template_parm_bindings_ok_p
23342 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
23343 {
23344 unify_inconsistent_template_template_parameters (explain_p);
23345 goto fail;
23346 }
23347
23348 deduced:
23349
23350 /* As a refinement of CWG2369, check first and foremost non-dependent
23351 conversions that we know are not going to induce template instantiation
23352 (PR99599). */
23353 if (strict == DEDUCE_CALL
23354 && incomplete
23355 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
23356 convs, explain_p,
23357 /*noninst_only_p=*/true))
23358 goto fail;
23359
23360 /* CWG2369: Check satisfaction before non-deducible conversions. */
23361 if (!constraints_satisfied_p (fn, targs))
23362 {
23363 if (explain_p)
23364 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
23365 goto fail;
23366 }
23367
23368 /* DR 1391: All parameters have args, now check non-dependent parms for
23369 convertibility. We don't do this if all args were explicitly specified,
23370 as the standard says that we substitute explicit args immediately. */
23371 if (incomplete
23372 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
23373 convs, explain_p,
23374 /*noninst_only_p=*/false))
23375 goto fail;
23376
23377 /* All is well so far. Now, check:
23378
23379 [temp.deduct]
23380
23381 When all template arguments have been deduced, all uses of
23382 template parameters in nondeduced contexts are replaced with
23383 the corresponding deduced argument values. If the
23384 substitution results in an invalid type, as described above,
23385 type deduction fails. */
23386 if (!push_tinst_level (fn, targs))
23387 {
23388 excessive_deduction_depth = true;
23389 goto fail;
23390 }
23391
23392 /* Also collect access checks from the instantiation. */
23393 reopen_deferring_access_checks (checks);
23394
23395 decl = instantiate_template (fn, targs, complain);
23396
23397 checks = get_deferred_access_checks ();
23398 pop_deferring_access_checks ();
23399
23400 pop_tinst_level ();
23401
23402 if (decl == error_mark_node)
23403 goto fail;
23404
23405 /* Now perform any access checks encountered during substitution. */
23406 push_access_scope (decl);
23407 ok = perform_access_checks (checks, complain);
23408 pop_access_scope (decl);
23409 if (!ok)
23410 goto fail;
23411
23412 /* If we're looking for an exact match, check that what we got
23413 is indeed an exact match. It might not be if some template
23414 parameters are used in non-deduced contexts. But don't check
23415 for an exact match if we have dependent template arguments;
23416 in that case we're doing partial ordering, and we already know
23417 that we have two candidates that will provide the actual type. */
23418 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
23419 {
23420 tree substed = TREE_TYPE (decl);
23421 unsigned int i;
23422
23423 tree sarg
23424 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
23425 if (return_type)
23426 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
23427 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
23428 if (!same_type_p (args[i], TREE_VALUE (sarg)))
23429 {
23430 unify_type_mismatch (explain_p, args[i],
23431 TREE_VALUE (sarg));
23432 goto fail;
23433 }
23434 if ((i < nargs || sarg)
23435 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
23436 doesn't contain the trailing void, and conv fns are always (). */
23437 && !DECL_CONV_FN_P (decl))
23438 {
23439 unsigned nsargs = i + list_length (sarg);
23440 unify_arity (explain_p, nargs, nsargs);
23441 goto fail;
23442 }
23443 }
23444
23445 /* After doing deduction with the inherited constructor, actually return an
23446 instantiation of the inheriting constructor. */
23447 if (orig_fn != fn)
23448 decl = instantiate_template (orig_fn, targs, complain);
23449
23450 r = decl;
23451
23452 fail:
23453 --deduction_depth;
23454 if (excessive_deduction_depth)
23455 {
23456 if (deduction_depth == 0)
23457 /* Reset once we're all the way out. */
23458 excessive_deduction_depth = false;
23459 }
23460
23461 return r;
23462 }
23463
23464 /* Returns true iff PARM is a forwarding reference in the context of
23465 template argument deduction for TMPL. */
23466
23467 static bool
23468 forwarding_reference_p (tree parm, tree tmpl)
23469 {
23470 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
23471 cv-unqualified template parameter ..." */
23472 if (TYPE_REF_P (parm)
23473 && TYPE_REF_IS_RVALUE (parm)
23474 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
23475 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
23476 {
23477 parm = TREE_TYPE (parm);
23478 /* [temp.deduct.call], "... that does not represent a template parameter
23479 of a class template (during class template argument deduction)." */
23480 if (tmpl
23481 && deduction_guide_p (tmpl)
23482 && DECL_ARTIFICIAL (tmpl))
23483 {
23484 /* Since the template parameters of a synthesized guide consist of
23485 the template parameters of the class template followed by those of
23486 the constructor (if any), we can tell if PARM represents a template
23487 parameter of the class template by comparing its index with the
23488 arity of the class template. */
23489 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
23490 if (TEMPLATE_TYPE_IDX (parm)
23491 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
23492 return false;
23493 }
23494 return true;
23495 }
23496 return false;
23497 }
23498
23499 /* Adjust types before performing type deduction, as described in
23500 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
23501 sections are symmetric. PARM is the type of a function parameter
23502 or the return type of the conversion function. ARG is the type of
23503 the argument passed to the call, or the type of the value
23504 initialized with the result of the conversion function.
23505 ARG_EXPR is the original argument expression, which may be null. */
23506
23507 static int
23508 maybe_adjust_types_for_deduction (tree tparms,
23509 unification_kind_t strict,
23510 tree* parm,
23511 tree* arg,
23512 tree arg_expr)
23513 {
23514 int result = 0;
23515
23516 switch (strict)
23517 {
23518 case DEDUCE_CALL:
23519 break;
23520
23521 case DEDUCE_CONV:
23522 /* [temp.deduct.conv] First remove a reference type on parm.
23523 DRs 322 & 976 affected this. */
23524 if (TYPE_REF_P (*parm))
23525 *parm = TREE_TYPE (*parm);
23526
23527 /* Swap PARM and ARG throughout the remainder of this
23528 function; the handling is precisely symmetric since PARM
23529 will initialize ARG rather than vice versa. */
23530 std::swap (parm, arg);
23531
23532 break;
23533
23534 case DEDUCE_EXACT:
23535 /* Core issue #873: Do the DR606 thing (see below) for these cases,
23536 too, but here handle it by stripping the reference from PARM
23537 rather than by adding it to ARG. */
23538 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
23539 && TYPE_REF_P (*arg)
23540 && !TYPE_REF_IS_RVALUE (*arg))
23541 *parm = TREE_TYPE (*parm);
23542 /* Nothing else to do in this case. */
23543 return 0;
23544
23545 default:
23546 gcc_unreachable ();
23547 }
23548
23549 if (!TYPE_REF_P (*parm))
23550 {
23551 /* [temp.deduct.call]
23552
23553 If P is not a reference type:
23554
23555 --If A is an array type, the pointer type produced by the
23556 array-to-pointer standard conversion (_conv.array_) is
23557 used in place of A for type deduction; otherwise,
23558
23559 --If A is a function type, the pointer type produced by
23560 the function-to-pointer standard conversion
23561 (_conv.func_) is used in place of A for type deduction;
23562 otherwise,
23563
23564 --If A is a cv-qualified type, the top level
23565 cv-qualifiers of A's type are ignored for type
23566 deduction. */
23567 if (TREE_CODE (*arg) == ARRAY_TYPE)
23568 *arg = build_pointer_type (TREE_TYPE (*arg));
23569 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
23570 *arg = build_pointer_type (*arg);
23571 else
23572 *arg = TYPE_MAIN_VARIANT (*arg);
23573 }
23574
23575 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
23576 an lvalue, the type 'lvalue reference to A' is used in place of A for
23577 type deduction." */
23578 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
23579 && (arg_expr ? lvalue_p (arg_expr)
23580 /* try_one_overload doesn't provide an arg_expr, but
23581 functions are always lvalues. */
23582 : TREE_CODE (*arg) == FUNCTION_TYPE))
23583 *arg = build_reference_type (*arg);
23584
23585 /* [temp.deduct.call]
23586
23587 If P is a cv-qualified type, the top level cv-qualifiers
23588 of P's type are ignored for type deduction. If P is a
23589 reference type, the type referred to by P is used for
23590 type deduction. */
23591 *parm = TYPE_MAIN_VARIANT (*parm);
23592 if (TYPE_REF_P (*parm))
23593 {
23594 *parm = TREE_TYPE (*parm);
23595 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23596 }
23597
23598 return result;
23599 }
23600
23601 /* Return true if computing a conversion from FROM to TO might induce template
23602 instantiation. Conversely, if this predicate returns false then computing
23603 the conversion definitely won't induce template instantiation. */
23604
23605 static bool
23606 conversion_may_instantiate_p (tree to, tree from)
23607 {
23608 to = non_reference (to);
23609 from = non_reference (from);
23610
23611 bool ptr_conv_p = false;
23612 if (TYPE_PTR_P (to)
23613 && TYPE_PTR_P (from))
23614 {
23615 to = TREE_TYPE (to);
23616 from = TREE_TYPE (from);
23617 ptr_conv_p = true;
23618 }
23619
23620 /* If one of the types is a not-yet-instantiated class template
23621 specialization, then computing the conversion might instantiate
23622 it in order to inspect bases, conversion functions and/or
23623 converting constructors. */
23624 if ((CLASS_TYPE_P (to)
23625 && !COMPLETE_TYPE_P (to)
23626 && CLASSTYPE_TEMPLATE_INSTANTIATION (to))
23627 || (CLASS_TYPE_P (from)
23628 && !COMPLETE_TYPE_P (from)
23629 && CLASSTYPE_TEMPLATE_INSTANTIATION (from)))
23630 return true;
23631
23632 /* Converting from one pointer type to another, or between
23633 reference-related types, always yields a standard conversion. */
23634 if (ptr_conv_p || reference_related_p (to, from))
23635 return false;
23636
23637 /* Converting to a non-aggregate class type will consider its
23638 user-declared constructors, which might induce instantiation. */
23639 if (CLASS_TYPE_P (to)
23640 && CLASSTYPE_NON_AGGREGATE (to))
23641 return true;
23642
23643 /* Similarly, converting from a class type will consider its conversion
23644 functions. */
23645 if (CLASS_TYPE_P (from)
23646 && TYPE_HAS_CONVERSION (from))
23647 return true;
23648
23649 /* Otherwise, computing this conversion definitely won't induce
23650 template instantiation. */
23651 return false;
23652 }
23653
23654 /* Subroutine of fn_type_unification. PARM is a function parameter of a
23655 template which doesn't contain any deducible template parameters; check if
23656 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
23657 unify_one_argument. */
23658
23659 static int
23660 check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
23661 int flags, struct conversion **conv_p,
23662 bool explain_p, bool noninst_only_p)
23663 {
23664 tree type;
23665
23666 if (!TYPE_P (arg))
23667 type = TREE_TYPE (arg);
23668 else
23669 type = arg;
23670
23671 if (same_type_p (parm, type))
23672 return unify_success (explain_p);
23673
23674 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
23675 if (strict == DEDUCE_CONV)
23676 {
23677 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
23678 return unify_success (explain_p);
23679 }
23680 else if (strict == DEDUCE_CALL)
23681 {
23682 if (conv_p && *conv_p)
23683 {
23684 /* This conversion was already computed earlier (when
23685 computing only non-instantiating conversions). */
23686 gcc_checking_assert (!noninst_only_p);
23687 return unify_success (explain_p);
23688 }
23689
23690 if (noninst_only_p
23691 && conversion_may_instantiate_p (parm, type))
23692 return unify_success (explain_p);
23693
23694 bool ok = false;
23695 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
23696 if (conv_p)
23697 /* Avoid recalculating this in add_function_candidate. */
23698 ok = (*conv_p
23699 = good_conversion (parm, type, conv_arg, flags, complain));
23700 else
23701 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
23702 if (ok)
23703 return unify_success (explain_p);
23704 }
23705
23706 if (strict == DEDUCE_EXACT)
23707 return unify_type_mismatch (explain_p, parm, arg);
23708 else
23709 return unify_arg_conversion (explain_p, parm, type, arg);
23710 }
23711
23712 static bool uses_deducible_template_parms (tree type);
23713
23714 /* Returns true iff the expression EXPR is one from which a template
23715 argument can be deduced. In other words, if it's an undecorated
23716 use of a template non-type parameter. */
23717
23718 static bool
23719 deducible_expression (tree expr)
23720 {
23721 /* Strip implicit conversions and implicit INDIRECT_REFs. */
23722 while (CONVERT_EXPR_P (expr)
23723 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
23724 || TREE_CODE (expr) == IMPLICIT_CONV_EXPR
23725 || REFERENCE_REF_P (expr))
23726 expr = TREE_OPERAND (expr, 0);
23727 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
23728 }
23729
23730 /* Returns true iff the array domain DOMAIN uses a template parameter in a
23731 deducible way; that is, if it has a max value of <PARM> - 1. */
23732
23733 static bool
23734 deducible_array_bound (tree domain)
23735 {
23736 if (domain == NULL_TREE)
23737 return false;
23738
23739 tree max = TYPE_MAX_VALUE (domain);
23740 if (TREE_CODE (max) != MINUS_EXPR)
23741 return false;
23742
23743 return deducible_expression (TREE_OPERAND (max, 0));
23744 }
23745
23746 /* Returns true iff the template arguments ARGS use a template parameter
23747 in a deducible way. */
23748
23749 static bool
23750 deducible_template_args (tree args)
23751 {
23752 for (tree elt : tree_vec_range (args))
23753 {
23754 bool deducible;
23755 if (ARGUMENT_PACK_P (elt))
23756 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
23757 else
23758 {
23759 if (PACK_EXPANSION_P (elt))
23760 elt = PACK_EXPANSION_PATTERN (elt);
23761 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
23762 deducible = true;
23763 else if (TYPE_P (elt))
23764 deducible = uses_deducible_template_parms (elt);
23765 else
23766 deducible = deducible_expression (elt);
23767 }
23768 if (deducible)
23769 return true;
23770 }
23771 return false;
23772 }
23773
23774 /* Returns true iff TYPE contains any deducible references to template
23775 parameters, as per 14.8.2.5. */
23776
23777 static bool
23778 uses_deducible_template_parms (tree type)
23779 {
23780 if (PACK_EXPANSION_P (type))
23781 type = PACK_EXPANSION_PATTERN (type);
23782
23783 /* T
23784 cv-list T
23785 TT<T>
23786 TT<i>
23787 TT<> */
23788 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23789 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23790 return true;
23791
23792 /* T*
23793 T&
23794 T&& */
23795 if (INDIRECT_TYPE_P (type))
23796 return uses_deducible_template_parms (TREE_TYPE (type));
23797
23798 /* T[integer-constant ]
23799 type [i] */
23800 if (TREE_CODE (type) == ARRAY_TYPE)
23801 return (uses_deducible_template_parms (TREE_TYPE (type))
23802 || deducible_array_bound (TYPE_DOMAIN (type)));
23803
23804 /* T type ::*
23805 type T::*
23806 T T::*
23807 T (type ::*)()
23808 type (T::*)()
23809 type (type ::*)(T)
23810 type (T::*)(T)
23811 T (type ::*)(T)
23812 T (T::*)()
23813 T (T::*)(T) */
23814 if (TYPE_PTRMEM_P (type))
23815 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
23816 || (uses_deducible_template_parms
23817 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
23818
23819 /* template-name <T> (where template-name refers to a class template)
23820 template-name <i> (where template-name refers to a class template) */
23821 if (CLASS_TYPE_P (type)
23822 && CLASSTYPE_TEMPLATE_INFO (type)
23823 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
23824 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
23825 (CLASSTYPE_TI_ARGS (type)));
23826
23827 /* type (T)
23828 T()
23829 T(T) */
23830 if (FUNC_OR_METHOD_TYPE_P (type))
23831 {
23832 if (uses_deducible_template_parms (TREE_TYPE (type)))
23833 return true;
23834 tree parm = TYPE_ARG_TYPES (type);
23835 if (TREE_CODE (type) == METHOD_TYPE)
23836 parm = TREE_CHAIN (parm);
23837 for (; parm; parm = TREE_CHAIN (parm))
23838 if (uses_deducible_template_parms (TREE_VALUE (parm)))
23839 return true;
23840 if (flag_noexcept_type
23841 && TYPE_RAISES_EXCEPTIONS (type)
23842 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
23843 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
23844 return true;
23845 }
23846
23847 return false;
23848 }
23849
23850 /* Subroutine of type_unification_real and unify_pack_expansion to
23851 handle unification of a single P/A pair. Parameters are as
23852 for those functions. */
23853
23854 static int
23855 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
23856 int subr, unification_kind_t strict,
23857 bool explain_p)
23858 {
23859 tree arg_expr = NULL_TREE;
23860 int arg_strict;
23861
23862 if (arg == error_mark_node || parm == error_mark_node)
23863 return unify_invalid (explain_p);
23864 if (arg == unknown_type_node)
23865 /* We can't deduce anything from this, but we might get all the
23866 template args from other function args. */
23867 return unify_success (explain_p);
23868
23869 /* Implicit conversions (Clause 4) will be performed on a function
23870 argument to convert it to the type of the corresponding function
23871 parameter if the parameter type contains no template-parameters that
23872 participate in template argument deduction. */
23873 if (strict != DEDUCE_EXACT
23874 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
23875 /* For function parameters with no deducible template parameters,
23876 just return. We'll check non-dependent conversions later. */
23877 return unify_success (explain_p);
23878
23879 switch (strict)
23880 {
23881 case DEDUCE_CALL:
23882 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
23883 | UNIFY_ALLOW_MORE_CV_QUAL
23884 | UNIFY_ALLOW_DERIVED);
23885 break;
23886
23887 case DEDUCE_CONV:
23888 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
23889 break;
23890
23891 case DEDUCE_EXACT:
23892 arg_strict = UNIFY_ALLOW_NONE;
23893 break;
23894
23895 default:
23896 gcc_unreachable ();
23897 }
23898
23899 /* We only do these transformations if this is the top-level
23900 parameter_type_list in a call or declaration matching; in other
23901 situations (nested function declarators, template argument lists) we
23902 won't be comparing a type to an expression, and we don't do any type
23903 adjustments. */
23904 if (!subr)
23905 {
23906 if (!TYPE_P (arg))
23907 {
23908 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
23909 if (type_unknown_p (arg))
23910 {
23911 /* [temp.deduct.type] A template-argument can be
23912 deduced from a pointer to function or pointer
23913 to member function argument if the set of
23914 overloaded functions does not contain function
23915 templates and at most one of a set of
23916 overloaded functions provides a unique
23917 match. */
23918 resolve_overloaded_unification (tparms, targs, parm,
23919 arg, strict,
23920 arg_strict, explain_p);
23921 /* If a unique match was not found, this is a
23922 non-deduced context, so we still succeed. */
23923 return unify_success (explain_p);
23924 }
23925
23926 arg_expr = arg;
23927 arg = unlowered_expr_type (arg);
23928 if (arg == error_mark_node)
23929 return unify_invalid (explain_p);
23930 }
23931
23932 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23933 &parm, &arg, arg_expr);
23934 }
23935 else
23936 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
23937 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
23938 return unify_template_argument_mismatch (explain_p, parm, arg);
23939
23940 /* For deduction from an init-list we need the actual list. */
23941 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
23942 arg = arg_expr;
23943 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
23944 }
23945
23946 /* for_each_template_parm callback that always returns 0. */
23947
23948 static int
23949 zero_r (tree, void *)
23950 {
23951 return 0;
23952 }
23953
23954 /* for_each_template_parm any_fn callback to handle deduction of a template
23955 type argument from the type of an array bound. */
23956
23957 static int
23958 array_deduction_r (tree t, void *data)
23959 {
23960 tree_pair_p d = (tree_pair_p)data;
23961 tree &tparms = d->purpose;
23962 tree &targs = d->value;
23963
23964 if (TREE_CODE (t) == ARRAY_TYPE)
23965 if (tree dom = TYPE_DOMAIN (t))
23966 if (tree max = TYPE_MAX_VALUE (dom))
23967 {
23968 if (TREE_CODE (max) == MINUS_EXPR)
23969 max = TREE_OPERAND (max, 0);
23970 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
23971 unify (tparms, targs, TREE_TYPE (max), size_type_node,
23972 UNIFY_ALLOW_NONE, /*explain*/false);
23973 }
23974
23975 /* Keep walking. */
23976 return 0;
23977 }
23978
23979 /* Try to deduce any not-yet-deduced template type arguments from the type of
23980 an array bound. This is handled separately from unify because 14.8.2.5 says
23981 "The type of a type parameter is only deduced from an array bound if it is
23982 not otherwise deduced." */
23983
23984 static void
23985 try_array_deduction (tree tparms, tree targs, tree parm)
23986 {
23987 tree_pair_s data = { tparms, targs };
23988 hash_set<tree> visited;
23989 for_each_template_parm (parm, zero_r, &data, &visited,
23990 /*nondeduced*/false, array_deduction_r);
23991 }
23992
23993 /* Most parms like fn_type_unification.
23994
23995 If SUBR is 1, we're being called recursively (to unify the
23996 arguments of a function or method parameter of a function
23997 template).
23998
23999 CHECKS is a pointer to a vector of access checks encountered while
24000 substituting default template arguments. */
24001
24002 static int
24003 type_unification_real (tree tparms,
24004 tree full_targs,
24005 tree xparms,
24006 const tree *xargs,
24007 unsigned int xnargs,
24008 int subr,
24009 unification_kind_t strict,
24010 vec<deferred_access_check, va_gc> **checks,
24011 bool explain_p)
24012 {
24013 tree parm, arg;
24014 int i;
24015 int ntparms = TREE_VEC_LENGTH (tparms);
24016 int saw_undeduced = 0;
24017 tree parms;
24018 const tree *args;
24019 unsigned int nargs;
24020 unsigned int ia;
24021
24022 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
24023 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
24024 gcc_assert (ntparms > 0);
24025
24026 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
24027
24028 /* Reset the number of non-defaulted template arguments contained
24029 in TARGS. */
24030 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
24031
24032 again:
24033 parms = xparms;
24034 args = xargs;
24035 nargs = xnargs;
24036
24037 /* Only fn_type_unification cares about terminal void. */
24038 if (nargs && args[nargs-1] == void_type_node)
24039 --nargs;
24040
24041 ia = 0;
24042 while (parms && parms != void_list_node
24043 && ia < nargs)
24044 {
24045 parm = TREE_VALUE (parms);
24046
24047 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
24048 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
24049 /* For a function parameter pack that occurs at the end of the
24050 parameter-declaration-list, the type A of each remaining
24051 argument of the call is compared with the type P of the
24052 declarator-id of the function parameter pack. */
24053 break;
24054
24055 parms = TREE_CHAIN (parms);
24056
24057 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
24058 /* For a function parameter pack that does not occur at the
24059 end of the parameter-declaration-list, the type of the
24060 parameter pack is a non-deduced context. */
24061 continue;
24062
24063 /* [temp.deduct.conv] only applies to the deduction of the return
24064 type, which is always the first argument here. Other arguments
24065 (notably, explicit object parameters) should undergo normal
24066 call-like unification. */
24067 unification_kind_t kind = strict;
24068 if (strict == DEDUCE_CONV && ia > 0)
24069 kind = DEDUCE_CALL;
24070
24071 arg = args[ia];
24072 ++ia;
24073
24074 if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
24075 explain_p))
24076 return 1;
24077 }
24078
24079 if (parms
24080 && parms != void_list_node
24081 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
24082 {
24083 gcc_assert (strict != DEDUCE_CONV);
24084
24085 /* Unify the remaining arguments with the pack expansion type. */
24086 tree argvec;
24087 tree parmvec = make_tree_vec (1);
24088
24089 /* Allocate a TREE_VEC and copy in all of the arguments */
24090 argvec = make_tree_vec (nargs - ia);
24091 for (i = 0; ia < nargs; ++ia, ++i)
24092 TREE_VEC_ELT (argvec, i) = args[ia];
24093
24094 /* Copy the parameter into parmvec. */
24095 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
24096 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
24097 /*subr=*/subr, explain_p))
24098 return 1;
24099
24100 /* Advance to the end of the list of parameters. */
24101 parms = TREE_CHAIN (parms);
24102 }
24103
24104 /* Fail if we've reached the end of the parm list, and more args
24105 are present, and the parm list isn't variadic. */
24106 if (ia < nargs && parms == void_list_node)
24107 return unify_too_many_arguments (explain_p, nargs, ia);
24108 /* Fail if parms are left and they don't have default values and
24109 they aren't all deduced as empty packs (c++/57397). This is
24110 consistent with sufficient_parms_p. */
24111 if (parms && parms != void_list_node
24112 && TREE_PURPOSE (parms) == NULL_TREE)
24113 {
24114 unsigned int count = nargs;
24115 tree p = parms;
24116 bool type_pack_p;
24117 do
24118 {
24119 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
24120 if (!type_pack_p)
24121 count++;
24122 p = TREE_CHAIN (p);
24123 }
24124 while (p && p != void_list_node);
24125 if (count != nargs)
24126 return unify_too_few_arguments (explain_p, ia, count,
24127 type_pack_p);
24128 }
24129
24130 if (!subr)
24131 {
24132 tsubst_flags_t complain = (explain_p
24133 ? tf_warning_or_error
24134 : tf_none);
24135 bool tried_array_deduction = (cxx_dialect < cxx17);
24136
24137 for (i = 0; i < ntparms; i++)
24138 {
24139 tree targ = TREE_VEC_ELT (targs, i);
24140 tree tparm = TREE_VEC_ELT (tparms, i);
24141
24142 /* Clear the "incomplete" flags on all argument packs now so that
24143 substituting them into later default arguments works. */
24144 if (targ && ARGUMENT_PACK_P (targ))
24145 {
24146 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
24147 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
24148 }
24149
24150 if (targ || tparm == error_mark_node)
24151 continue;
24152 tparm = TREE_VALUE (tparm);
24153
24154 if (TREE_CODE (tparm) == TYPE_DECL
24155 && !tried_array_deduction)
24156 {
24157 try_array_deduction (tparms, targs, xparms);
24158 tried_array_deduction = true;
24159 if (TREE_VEC_ELT (targs, i))
24160 continue;
24161 }
24162
24163 /* If this is an undeduced nontype parameter that depends on
24164 a type parameter, try another pass; its type may have been
24165 deduced from a later argument than the one from which
24166 this parameter can be deduced. */
24167 if (TREE_CODE (tparm) == PARM_DECL
24168 && !is_auto (TREE_TYPE (tparm))
24169 && uses_template_parms (TREE_TYPE (tparm))
24170 && saw_undeduced < 2)
24171 {
24172 saw_undeduced = 1;
24173 continue;
24174 }
24175
24176 /* Core issue #226 (C++0x) [temp.deduct]:
24177
24178 If a template argument has not been deduced, its
24179 default template argument, if any, is used.
24180
24181 When we are in C++98 mode, TREE_PURPOSE will either
24182 be NULL_TREE or ERROR_MARK_NODE, so we do not need
24183 to explicitly check cxx_dialect here. */
24184 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
24185 /* OK, there is a default argument. Wait until after the
24186 conversion check to do substitution. */
24187 continue;
24188
24189 /* If the type parameter is a parameter pack, then it will
24190 be deduced to an empty parameter pack. */
24191 if (template_parameter_pack_p (tparm))
24192 {
24193 tree arg;
24194
24195 if (TREE_CODE (tparm) == PARM_DECL)
24196 {
24197 arg = make_node (NONTYPE_ARGUMENT_PACK);
24198 TREE_CONSTANT (arg) = 1;
24199 }
24200 else
24201 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
24202
24203 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
24204
24205 TREE_VEC_ELT (targs, i) = arg;
24206 continue;
24207 }
24208
24209 return unify_parameter_deduction_failure (explain_p, tparm);
24210 }
24211
24212 /* During partial ordering, we deduce dependent template args. */
24213 bool any_dependent_targs = false;
24214
24215 /* Now substitute into the default template arguments. */
24216 for (i = 0; i < ntparms; i++)
24217 {
24218 tree targ = TREE_VEC_ELT (targs, i);
24219 tree tparm = TREE_VEC_ELT (tparms, i);
24220
24221 if (targ)
24222 {
24223 if (!any_dependent_targs && dependent_template_arg_p (targ))
24224 any_dependent_targs = true;
24225 continue;
24226 }
24227 if (tparm == error_mark_node)
24228 continue;
24229
24230 tree parm = TREE_VALUE (tparm);
24231 tree arg = TREE_PURPOSE (tparm);
24232 reopen_deferring_access_checks (*checks);
24233 location_t save_loc = input_location;
24234 if (DECL_P (parm))
24235 input_location = DECL_SOURCE_LOCATION (parm);
24236
24237 if (saw_undeduced == 1
24238 && TREE_CODE (parm) == PARM_DECL
24239 && !is_auto (TREE_TYPE (parm))
24240 && uses_template_parms (TREE_TYPE (parm)))
24241 {
24242 /* The type of this non-type parameter depends on undeduced
24243 parameters. Don't try to use its default argument yet,
24244 since we might deduce an argument for it on the next pass,
24245 but do check whether the arguments we already have cause
24246 substitution failure, so that that happens before we try
24247 later default arguments (78489). */
24248 ++processing_template_decl;
24249 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
24250 NULL_TREE);
24251 --processing_template_decl;
24252 if (type == error_mark_node)
24253 arg = error_mark_node;
24254 else
24255 arg = NULL_TREE;
24256 }
24257 else
24258 {
24259 /* Even if the call is happening in template context, getting
24260 here means it's non-dependent, and a default argument is
24261 considered a separate definition under [temp.decls], so we can
24262 do this substitution without processing_template_decl. This
24263 is important if the default argument contains something that
24264 might be instantiation-dependent like access (87480). */
24265 processing_template_decl_sentinel s (!any_dependent_targs);
24266
24267 tree used_tparms = NULL_TREE;
24268 if (saw_undeduced == 1)
24269 {
24270 tree tparms_list = build_tree_list (size_int (1), tparms);
24271 used_tparms = find_template_parameters (arg, tparms_list);
24272 for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms))
24273 {
24274 int level, index;
24275 template_parm_level_and_index (TREE_VALUE (used_tparms),
24276 &level, &index);
24277 if (TREE_VEC_ELT (targs, index) == NULL_TREE)
24278 break;
24279 }
24280 }
24281
24282 if (!used_tparms)
24283 {
24284 /* All template parameters within this default argument are
24285 deduced, so we can use it. */
24286 arg = tsubst_template_arg (arg, full_targs, complain,
24287 NULL_TREE);
24288 arg = convert_template_argument (parm, arg, full_targs,
24289 complain, i, NULL_TREE);
24290 }
24291 else if (saw_undeduced == 1)
24292 arg = NULL_TREE;
24293 else if (!any_dependent_targs)
24294 arg = error_mark_node;
24295 }
24296
24297 input_location = save_loc;
24298 *checks = get_deferred_access_checks ();
24299 pop_deferring_access_checks ();
24300
24301 if (arg == error_mark_node)
24302 return 1;
24303 else if (arg)
24304 {
24305 TREE_VEC_ELT (targs, i) = arg;
24306 /* The position of the first default template argument,
24307 is also the number of non-defaulted arguments in TARGS.
24308 Record that. */
24309 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
24310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
24311 }
24312 }
24313
24314 if (saw_undeduced++ == 1)
24315 goto again;
24316 }
24317
24318 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
24319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
24320
24321 return unify_success (explain_p);
24322 }
24323
24324 /* Subroutine of type_unification_real. Args are like the variables
24325 at the call site. ARG is an overloaded function (or template-id);
24326 we try deducing template args from each of the overloads, and if
24327 only one succeeds, we go with that. Modifies TARGS and returns
24328 true on success. */
24329
24330 static bool
24331 resolve_overloaded_unification (tree tparms,
24332 tree targs,
24333 tree parm,
24334 tree arg,
24335 unification_kind_t strict,
24336 int sub_strict,
24337 bool explain_p)
24338 {
24339 tree tempargs = copy_node (targs);
24340 int good = 0;
24341 tree goodfn = NULL_TREE;
24342 bool addr_p;
24343
24344 if (TREE_CODE (arg) == ADDR_EXPR)
24345 {
24346 arg = TREE_OPERAND (arg, 0);
24347 addr_p = true;
24348 }
24349 else
24350 addr_p = false;
24351
24352 if (TREE_CODE (arg) == COMPONENT_REF)
24353 /* Handle `&x' where `x' is some static or non-static member
24354 function name. */
24355 arg = TREE_OPERAND (arg, 1);
24356
24357 if (TREE_CODE (arg) == OFFSET_REF)
24358 arg = TREE_OPERAND (arg, 1);
24359
24360 /* Strip baselink information. */
24361 if (BASELINK_P (arg))
24362 arg = BASELINK_FUNCTIONS (arg);
24363
24364 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
24365 {
24366 /* If we got some explicit template args, we need to plug them into
24367 the affected templates before we try to unify, in case the
24368 explicit args will completely resolve the templates in question. */
24369
24370 int ok = 0;
24371 tree expl_subargs = TREE_OPERAND (arg, 1);
24372 arg = TREE_OPERAND (arg, 0);
24373
24374 for (lkp_iterator iter (arg); iter; ++iter)
24375 {
24376 tree fn = *iter;
24377 tree subargs, elem;
24378
24379 if (TREE_CODE (fn) != TEMPLATE_DECL)
24380 continue;
24381
24382 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
24383 expl_subargs, NULL_TREE, tf_none);
24384 if (subargs != error_mark_node
24385 && !any_dependent_template_arguments_p (subargs))
24386 {
24387 fn = instantiate_template (fn, subargs, tf_none);
24388 if (!constraints_satisfied_p (fn))
24389 continue;
24390 if (undeduced_auto_decl (fn))
24391 {
24392 /* Instantiate the function to deduce its return type. */
24393 ++function_depth;
24394 instantiate_decl (fn, /*defer*/false, /*class*/false);
24395 --function_depth;
24396 }
24397
24398 if (flag_noexcept_type)
24399 maybe_instantiate_noexcept (fn, tf_none);
24400
24401 elem = TREE_TYPE (fn);
24402 if (try_one_overload (tparms, targs, tempargs, parm,
24403 elem, strict, sub_strict, addr_p, explain_p)
24404 && (!goodfn || !same_type_p (goodfn, elem)))
24405 {
24406 goodfn = elem;
24407 ++good;
24408 }
24409 }
24410 else if (subargs)
24411 ++ok;
24412 }
24413 /* If no templates (or more than one) are fully resolved by the
24414 explicit arguments, this template-id is a non-deduced context; it
24415 could still be OK if we deduce all template arguments for the
24416 enclosing call through other arguments. */
24417 if (good != 1)
24418 good = ok;
24419 }
24420 else if (!OVL_P (arg))
24421 /* If ARG is, for example, "(0, &f)" then its type will be unknown
24422 -- but the deduction does not succeed because the expression is
24423 not just the function on its own. */
24424 return false;
24425 else
24426 for (lkp_iterator iter (arg); iter; ++iter)
24427 {
24428 tree fn = *iter;
24429 if (flag_noexcept_type)
24430 maybe_instantiate_noexcept (fn, tf_none);
24431 if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn))
24432 continue;
24433 tree elem = TREE_TYPE (fn);
24434 if (try_one_overload (tparms, targs, tempargs, parm, elem,
24435 strict, sub_strict, addr_p, explain_p)
24436 && (!goodfn || !same_type_p (goodfn, elem)))
24437 {
24438 goodfn = elem;
24439 ++good;
24440 }
24441 }
24442
24443 /* [temp.deduct.type] A template-argument can be deduced from a pointer
24444 to function or pointer to member function argument if the set of
24445 overloaded functions does not contain function templates and at most
24446 one of a set of overloaded functions provides a unique match.
24447
24448 CWG2918 allows multiple functions to match if they all have the same type,
24449 so that we can choose the most constrained later.
24450
24451 So if we found multiple possibilities, we return success but don't
24452 deduce anything. */
24453
24454 if (good == 1)
24455 {
24456 int i = TREE_VEC_LENGTH (targs);
24457 for (; i--; )
24458 if (TREE_VEC_ELT (tempargs, i))
24459 {
24460 tree old = TREE_VEC_ELT (targs, i);
24461 tree new_ = TREE_VEC_ELT (tempargs, i);
24462 if (new_ && old && ARGUMENT_PACK_P (old)
24463 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
24464 /* Don't forget explicit template arguments in a pack. */
24465 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
24466 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
24467 TREE_VEC_ELT (targs, i) = new_;
24468 }
24469 }
24470 if (good)
24471 return true;
24472
24473 return false;
24474 }
24475
24476 /* Core DR 115: In contexts where deduction is done and fails, or in
24477 contexts where deduction is not done, if a template argument list is
24478 specified and it, along with any default template arguments, identifies
24479 a single function template specialization, then the template-id is an
24480 lvalue for the function template specialization. */
24481
24482 tree
24483 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
24484 {
24485 tree expr, offset, baselink;
24486 bool addr;
24487
24488 if (!type_unknown_p (orig_expr))
24489 return orig_expr;
24490
24491 expr = orig_expr;
24492 addr = false;
24493 offset = NULL_TREE;
24494 baselink = NULL_TREE;
24495
24496 if (TREE_CODE (expr) == ADDR_EXPR)
24497 {
24498 expr = TREE_OPERAND (expr, 0);
24499 addr = true;
24500 }
24501 if (TREE_CODE (expr) == OFFSET_REF)
24502 {
24503 offset = expr;
24504 expr = TREE_OPERAND (expr, 1);
24505 }
24506 if (BASELINK_P (expr))
24507 {
24508 baselink = expr;
24509 expr = BASELINK_FUNCTIONS (expr);
24510 }
24511
24512 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
24513 {
24514 int good = 0;
24515 tree goodfn = NULL_TREE;
24516
24517 /* If we got some explicit template args, we need to plug them into
24518 the affected templates before we try to unify, in case the
24519 explicit args will completely resolve the templates in question. */
24520
24521 tree expl_subargs = TREE_OPERAND (expr, 1);
24522 tree arg = TREE_OPERAND (expr, 0);
24523 tree badfn = NULL_TREE;
24524 tree badargs = NULL_TREE;
24525
24526 for (lkp_iterator iter (arg); iter; ++iter)
24527 {
24528 tree fn = *iter;
24529 tree subargs, elem;
24530
24531 if (TREE_CODE (fn) != TEMPLATE_DECL)
24532 continue;
24533
24534 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
24535 expl_subargs, NULL_TREE, tf_none);
24536 if (subargs != error_mark_node
24537 && !any_dependent_template_arguments_p (subargs))
24538 {
24539 elem = instantiate_template (fn, subargs, tf_none);
24540 if (elem == error_mark_node)
24541 {
24542 badfn = fn;
24543 badargs = subargs;
24544 }
24545 else if (elem && (!goodfn || !decls_match (goodfn, elem))
24546 && constraints_satisfied_p (elem))
24547 {
24548 goodfn = elem;
24549 ++good;
24550 }
24551 }
24552 }
24553 if (good == 1)
24554 {
24555 if (!mark_used (goodfn, complain) && !(complain & tf_error))
24556 return error_mark_node;
24557 expr = goodfn;
24558 if (baselink)
24559 expr = build_baselink (BASELINK_BINFO (baselink),
24560 BASELINK_ACCESS_BINFO (baselink),
24561 expr, BASELINK_OPTYPE (baselink));
24562 if (offset)
24563 {
24564 tree base
24565 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
24566 expr = build_offset_ref (base, expr, addr, complain);
24567 }
24568 if (addr)
24569 expr = cp_build_addr_expr (expr, complain);
24570 return expr;
24571 }
24572 else if (good == 0 && badargs && (complain & tf_error))
24573 /* There were no good options and at least one bad one, so let the
24574 user know what the problem is. */
24575 instantiate_template (badfn, badargs, complain);
24576 }
24577 return orig_expr;
24578 }
24579
24580 /* As above, but error out if the expression remains overloaded. */
24581
24582 tree
24583 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
24584 {
24585 exp = resolve_nondeduced_context (exp, complain);
24586 if (type_unknown_p (exp))
24587 {
24588 if (complain & tf_error)
24589 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
24590 return error_mark_node;
24591 }
24592 return exp;
24593 }
24594
24595 /* Subroutine of resolve_overloaded_unification; does deduction for a single
24596 overload. Fills TARGS with any deduced arguments, or error_mark_node if
24597 different overloads deduce different arguments for a given parm.
24598 ADDR_P is true if the expression for which deduction is being
24599 performed was of the form "& fn" rather than simply "fn".
24600
24601 Returns 1 on success. */
24602
24603 static int
24604 try_one_overload (tree tparms,
24605 tree orig_targs,
24606 tree targs,
24607 tree parm,
24608 tree arg,
24609 unification_kind_t strict,
24610 int sub_strict,
24611 bool addr_p,
24612 bool explain_p)
24613 {
24614 int nargs;
24615 tree tempargs;
24616 int i;
24617
24618 if (arg == error_mark_node)
24619 return 0;
24620
24621 /* [temp.deduct.type] A template-argument can be deduced from a pointer
24622 to function or pointer to member function argument if the set of
24623 overloaded functions does not contain function templates and at most
24624 one of a set of overloaded functions provides a unique match.
24625
24626 So if this is a template, just return success. */
24627
24628 if (uses_template_parms (arg))
24629 return 1;
24630
24631 if (TREE_CODE (arg) == METHOD_TYPE)
24632 arg = build_ptrmemfunc_type (build_pointer_type (arg));
24633 else if (addr_p)
24634 arg = build_pointer_type (arg);
24635
24636 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
24637 &parm, &arg, NULL_TREE);
24638
24639 /* We don't copy orig_targs for this because if we have already deduced
24640 some template args from previous args, unify would complain when we
24641 try to deduce a template parameter for the same argument, even though
24642 there isn't really a conflict. */
24643 nargs = TREE_VEC_LENGTH (targs);
24644 tempargs = make_tree_vec (nargs);
24645
24646 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
24647 return 0;
24648
24649 /* First make sure we didn't deduce anything that conflicts with
24650 explicitly specified args. */
24651 for (i = nargs; i--; )
24652 {
24653 tree elt = TREE_VEC_ELT (tempargs, i);
24654 tree oldelt = TREE_VEC_ELT (orig_targs, i);
24655
24656 if (!elt)
24657 /*NOP*/;
24658 else if (uses_template_parms (elt))
24659 /* Since we're unifying against ourselves, we will fill in
24660 template args used in the function parm list with our own
24661 template parms. Discard them. */
24662 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
24663 else if (oldelt && ARGUMENT_PACK_P (oldelt))
24664 {
24665 /* Check that the argument at each index of the deduced argument pack
24666 is equivalent to the corresponding explicitly specified argument.
24667 We may have deduced more arguments than were explicitly specified,
24668 and that's OK. */
24669
24670 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
24671 that's wrong if we deduce the same argument pack from multiple
24672 function arguments: it's only incomplete the first time. */
24673
24674 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
24675 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
24676
24677 if (TREE_VEC_LENGTH (deduced_pack)
24678 < TREE_VEC_LENGTH (explicit_pack))
24679 return 0;
24680
24681 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
24682 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
24683 TREE_VEC_ELT (deduced_pack, j)))
24684 return 0;
24685 }
24686 else if (oldelt && !template_args_equal (oldelt, elt))
24687 return 0;
24688 }
24689
24690 for (i = nargs; i--; )
24691 {
24692 tree elt = TREE_VEC_ELT (tempargs, i);
24693
24694 if (elt)
24695 TREE_VEC_ELT (targs, i) = elt;
24696 }
24697
24698 return 1;
24699 }
24700
24701 /* PARM is a template class (perhaps with unbound template
24702 parameters). ARG is a fully instantiated type. If ARG can be
24703 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
24704 TARGS are as for unify. */
24705
24706 static tree
24707 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
24708 bool explain_p)
24709 {
24710 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24711 return NULL_TREE;
24712 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24713 /* Matches anything. */;
24714 else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
24715 return NULL_TREE;
24716
24717 /* We need to make a new template argument vector for the call to
24718 unify. If we used TARGS, we'd clutter it up with the result of
24719 the attempted unification, even if this class didn't work out.
24720 We also don't want to commit ourselves to all the unifications
24721 we've already done, since unification is supposed to be done on
24722 an argument-by-argument basis. In other words, consider the
24723 following pathological case:
24724
24725 template <int I, int J, int K>
24726 struct S {};
24727
24728 template <int I, int J>
24729 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
24730
24731 template <int I, int J, int K>
24732 void f(S<I, J, K>, S<I, I, I>);
24733
24734 void g() {
24735 S<0, 0, 0> s0;
24736 S<0, 1, 2> s2;
24737
24738 f(s0, s2);
24739 }
24740
24741 Now, by the time we consider the unification involving `s2', we
24742 already know that we must have `f<0, 0, 0>'. But, even though
24743 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
24744 because there are two ways to unify base classes of S<0, 1, 2>
24745 with S<I, I, I>. If we kept the already deduced knowledge, we
24746 would reject the possibility I=1. */
24747 targs = copy_template_args (targs);
24748 for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
24749 targ = NULL_TREE;
24750
24751 int err;
24752 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24753 err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
24754 else
24755 err = unify (tparms, targs,
24756 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
24757 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
24758 UNIFY_ALLOW_NONE, explain_p);
24759
24760 return err ? NULL_TREE : arg;
24761 }
24762
24763 /* Given a template type PARM and a class type ARG, find the unique
24764 base type in ARG that is an instance of PARM. We do not examine
24765 ARG itself; only its base-classes. If there is not exactly one
24766 appropriate base class, return NULL_TREE. PARM may be the type of
24767 a partial specialization, as well as a plain template type. Used
24768 by unify. */
24769
24770 static enum template_base_result
24771 get_template_base (tree tparms, tree targs, tree parm, tree arg,
24772 bool explain_p, tree *result)
24773 {
24774 tree rval = NULL_TREE;
24775 tree binfo;
24776
24777 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
24778
24779 binfo = TYPE_BINFO (complete_type (arg));
24780 if (!binfo)
24781 {
24782 /* The type could not be completed. */
24783 *result = NULL_TREE;
24784 return tbr_incomplete_type;
24785 }
24786
24787 /* Walk in inheritance graph order. The search order is not
24788 important, and this avoids multiple walks of virtual bases. */
24789 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
24790 {
24791 tree r = try_class_unification (tparms, targs, parm,
24792 BINFO_TYPE (binfo), explain_p);
24793
24794 if (r)
24795 {
24796 /* If there is more than one satisfactory baseclass, then:
24797
24798 [temp.deduct.call]
24799
24800 If they yield more than one possible deduced A, the type
24801 deduction fails.
24802
24803 applies. */
24804 if (rval && !same_type_p (r, rval))
24805 {
24806 /* [temp.deduct.call]/4.3: If there is a class C that is a
24807 (direct or indirect) base class of D and derived (directly or
24808 indirectly) from a class B and that would be a valid deduced
24809 A, the deduced A cannot be B or pointer to B, respectively. */
24810 if (DERIVED_FROM_P (r, rval))
24811 /* Ignore r. */
24812 continue;
24813 else if (DERIVED_FROM_P (rval, r))
24814 /* Ignore rval. */;
24815 else
24816 {
24817 *result = NULL_TREE;
24818 return tbr_ambiguous_baseclass;
24819 }
24820 }
24821
24822 rval = r;
24823 }
24824 }
24825
24826 *result = rval;
24827 return tbr_success;
24828 }
24829
24830 /* Returns the level of DECL, which declares a template parameter. */
24831
24832 static int
24833 template_decl_level (tree decl)
24834 {
24835 switch (TREE_CODE (decl))
24836 {
24837 case TYPE_DECL:
24838 case TEMPLATE_DECL:
24839 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
24840
24841 case PARM_DECL:
24842 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
24843
24844 default:
24845 gcc_unreachable ();
24846 }
24847 return 0;
24848 }
24849
24850 /* Decide whether ARG can be unified with PARM, considering only the
24851 cv-qualifiers of each type, given STRICT as documented for unify.
24852 Returns nonzero iff the unification is OK on that basis. */
24853
24854 static int
24855 check_cv_quals_for_unify (int strict, tree arg, tree parm)
24856 {
24857 int arg_quals = cp_type_quals (arg);
24858 int parm_quals = cp_type_quals (parm);
24859
24860 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24861 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24862 {
24863 /* Although a CVR qualifier is ignored when being applied to a
24864 substituted template parameter ([8.3.2]/1 for example), that
24865 does not allow us to unify "const T" with "int&" because both
24866 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
24867 It is ok when we're allowing additional CV qualifiers
24868 at the outer level [14.8.2.1]/3,1st bullet. */
24869 if ((TYPE_REF_P (arg)
24870 || FUNC_OR_METHOD_TYPE_P (arg))
24871 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
24872 return 0;
24873
24874 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
24875 && (parm_quals & TYPE_QUAL_RESTRICT))
24876 return 0;
24877 }
24878
24879 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24880 && (arg_quals & parm_quals) != parm_quals)
24881 return 0;
24882
24883 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
24884 && (parm_quals & arg_quals) != arg_quals)
24885 return 0;
24886
24887 return 1;
24888 }
24889
24890 /* Determines the LEVEL and INDEX for the template parameter PARM. */
24891 void
24892 template_parm_level_and_index (tree parm, int* level, int* index)
24893 {
24894 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24895 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24896 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24897 {
24898 *index = TEMPLATE_TYPE_IDX (parm);
24899 *level = TEMPLATE_TYPE_LEVEL (parm);
24900 }
24901 else
24902 {
24903 *index = TEMPLATE_PARM_IDX (parm);
24904 *level = TEMPLATE_PARM_LEVEL (parm);
24905 }
24906 }
24907
24908 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
24909 do { \
24910 if (unify (TP, TA, P, A, S, EP)) \
24911 return 1; \
24912 } while (0)
24913
24914 /* Unifies the remaining arguments in PACKED_ARGS with the pack
24915 expansion at the end of PACKED_PARMS. Returns 0 if the type
24916 deduction succeeds, 1 otherwise. STRICT is the same as in
24917 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
24918 function call argument list. We'll need to adjust the arguments to make them
24919 types. SUBR tells us if this is from a recursive call to
24920 type_unification_real, or for comparing two template argument
24921 lists. */
24922
24923 static int
24924 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
24925 tree packed_args, unification_kind_t strict,
24926 bool subr, bool explain_p)
24927 {
24928 tree parm
24929 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
24930 tree pattern = PACK_EXPANSION_PATTERN (parm);
24931 tree pack, packs = NULL_TREE;
24932 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
24933
24934 /* Add in any args remembered from an earlier partial instantiation. */
24935 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
24936 int levels = TMPL_ARGS_DEPTH (targs);
24937
24938 packed_args = expand_template_argument_pack (packed_args);
24939
24940 int len = TREE_VEC_LENGTH (packed_args);
24941
24942 /* Determine the parameter packs we will be deducing from the
24943 pattern, and record their current deductions. */
24944 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
24945 pack; pack = TREE_CHAIN (pack))
24946 {
24947 tree parm_pack = TREE_VALUE (pack);
24948 int idx, level;
24949
24950 /* Only template parameter packs can be deduced, not e.g. function
24951 parameter packs or __bases or __integer_pack. */
24952 if (!TEMPLATE_PARM_P (parm_pack))
24953 continue;
24954
24955 /* Determine the index and level of this parameter pack. */
24956 template_parm_level_and_index (parm_pack, &level, &idx);
24957 if (level > levels)
24958 continue;
24959
24960 /* Keep track of the parameter packs and their corresponding
24961 argument packs. */
24962 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
24963 TREE_TYPE (packs) = make_tree_vec (len - start);
24964 }
24965
24966 /* Loop through all of the arguments that have not yet been
24967 unified and unify each with the pattern. */
24968 for (i = start; i < len; i++)
24969 {
24970 tree parm;
24971 bool any_explicit = false;
24972 tree arg = TREE_VEC_ELT (packed_args, i);
24973
24974 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
24975 or the element of its argument pack at the current index if
24976 this argument was explicitly specified. */
24977 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24978 {
24979 int idx, level;
24980 tree arg, pargs;
24981 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
24982
24983 arg = NULL_TREE;
24984 if (TREE_VALUE (pack)
24985 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
24986 && (i - start < TREE_VEC_LENGTH (pargs)))
24987 {
24988 any_explicit = true;
24989 arg = TREE_VEC_ELT (pargs, i - start);
24990 }
24991 TMPL_ARG (targs, level, idx) = arg;
24992 }
24993
24994 /* If we had explicit template arguments, substitute them into the
24995 pattern before deduction. */
24996 if (any_explicit)
24997 {
24998 /* Some arguments might still be unspecified or dependent. */
24999 bool dependent;
25000 ++processing_template_decl;
25001 dependent = any_dependent_template_arguments_p (targs);
25002 if (!dependent)
25003 --processing_template_decl;
25004 parm = tsubst (pattern, targs,
25005 explain_p ? tf_warning_or_error : tf_none,
25006 NULL_TREE);
25007 if (dependent)
25008 --processing_template_decl;
25009 if (parm == error_mark_node)
25010 return 1;
25011 }
25012 else
25013 parm = pattern;
25014
25015 /* Unify the pattern with the current argument. */
25016 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
25017 explain_p))
25018 return 1;
25019
25020 /* For each parameter pack, collect the deduced value. */
25021 for (pack = packs; pack; pack = TREE_CHAIN (pack))
25022 {
25023 int idx, level;
25024 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
25025
25026 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
25027 TMPL_ARG (targs, level, idx);
25028 }
25029 }
25030
25031 /* Verify that the results of unification with the parameter packs
25032 produce results consistent with what we've seen before, and make
25033 the deduced argument packs available. */
25034 for (pack = packs; pack; pack = TREE_CHAIN (pack))
25035 {
25036 tree old_pack = TREE_VALUE (pack);
25037 tree new_args = TREE_TYPE (pack);
25038 int i, len = TREE_VEC_LENGTH (new_args);
25039 int idx, level;
25040 bool nondeduced_p = false;
25041
25042 /* By default keep the original deduced argument pack.
25043 If necessary, more specific code is going to update the
25044 resulting deduced argument later down in this function. */
25045 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
25046 TMPL_ARG (targs, level, idx) = old_pack;
25047
25048 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
25049 actually deduce anything. */
25050 for (i = 0; i < len && !nondeduced_p; ++i)
25051 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
25052 nondeduced_p = true;
25053 if (nondeduced_p)
25054 continue;
25055
25056 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
25057 {
25058 /* If we had fewer function args than explicit template args,
25059 just use the explicits. */
25060 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
25061 int explicit_len = TREE_VEC_LENGTH (explicit_args);
25062 if (len < explicit_len)
25063 new_args = explicit_args;
25064 }
25065
25066 if (!old_pack)
25067 {
25068 tree result;
25069 /* Build the deduced *_ARGUMENT_PACK. */
25070 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
25071 {
25072 result = make_node (NONTYPE_ARGUMENT_PACK);
25073 TREE_CONSTANT (result) = 1;
25074 }
25075 else
25076 result = cxx_make_type (TYPE_ARGUMENT_PACK);
25077
25078 ARGUMENT_PACK_ARGS (result) = new_args;
25079
25080 /* Note the deduced argument packs for this parameter
25081 pack. */
25082 TMPL_ARG (targs, level, idx) = result;
25083 }
25084 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
25085 && (ARGUMENT_PACK_ARGS (old_pack)
25086 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
25087 {
25088 /* We only had the explicitly-provided arguments before, but
25089 now we have a complete set of arguments. */
25090 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
25091
25092 ARGUMENT_PACK_ARGS (old_pack) = new_args;
25093 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
25094 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
25095 }
25096 else
25097 {
25098 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
25099 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
25100 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
25101 /* During template argument deduction for the aggregate deduction
25102 candidate, the number of elements in a trailing parameter pack
25103 is only deduced from the number of remaining function
25104 arguments if it is not otherwise deduced. */
25105 if (cxx_dialect >= cxx20
25106 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
25107 /* FIXME This isn't set properly for partial instantiations. */
25108 && TPARMS_PRIMARY_TEMPLATE (tparms)
25109 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
25110 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
25111 if (!comp_template_args (old_args, new_args,
25112 &bad_old_arg, &bad_new_arg))
25113 /* Inconsistent unification of this parameter pack. */
25114 return unify_parameter_pack_inconsistent (explain_p,
25115 bad_old_arg,
25116 bad_new_arg);
25117 }
25118 }
25119
25120 return unify_success (explain_p);
25121 }
25122
25123 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
25124 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
25125 parameters and return value are as for unify. */
25126
25127 static int
25128 unify_array_domain (tree tparms, tree targs,
25129 tree parm_dom, tree arg_dom,
25130 bool explain_p)
25131 {
25132 tree parm_max;
25133 tree arg_max;
25134 bool parm_cst;
25135 bool arg_cst;
25136
25137 /* Our representation of array types uses "N - 1" as the
25138 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
25139 not an integer constant. We cannot unify arbitrarily
25140 complex expressions, so we eliminate the MINUS_EXPRs
25141 here. */
25142 parm_max = TYPE_MAX_VALUE (parm_dom);
25143 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
25144 if (!parm_cst)
25145 {
25146 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
25147 parm_max = TREE_OPERAND (parm_max, 0);
25148 }
25149 arg_max = TYPE_MAX_VALUE (arg_dom);
25150 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
25151 if (!arg_cst)
25152 {
25153 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
25154 trying to unify the type of a variable with the type
25155 of a template parameter. For example:
25156
25157 template <unsigned int N>
25158 void f (char (&) [N]);
25159 int g();
25160 void h(int i) {
25161 char a[g(i)];
25162 f(a);
25163 }
25164
25165 Here, the type of the ARG will be "int [g(i)]", and
25166 may be a SAVE_EXPR, etc. */
25167 if (TREE_CODE (arg_max) != MINUS_EXPR)
25168 return unify_vla_arg (explain_p, arg_dom);
25169 arg_max = TREE_OPERAND (arg_max, 0);
25170 }
25171
25172 /* If only one of the bounds used a MINUS_EXPR, compensate
25173 by adding one to the other bound. */
25174 if (parm_cst && !arg_cst)
25175 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
25176 integer_type_node,
25177 parm_max,
25178 integer_one_node);
25179 else if (arg_cst && !parm_cst)
25180 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
25181 integer_type_node,
25182 arg_max,
25183 integer_one_node);
25184
25185 return unify (tparms, targs, parm_max, arg_max,
25186 UNIFY_ALLOW_INTEGER, explain_p);
25187 }
25188
25189 /* Returns whether T, a P or A in unify, is a type, template or expression. */
25190
25191 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
25192
25193 static pa_kind_t
25194 pa_kind (tree t)
25195 {
25196 if (PACK_EXPANSION_P (t))
25197 t = PACK_EXPANSION_PATTERN (t);
25198 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
25199 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
25200 || DECL_TYPE_TEMPLATE_P (t))
25201 return pa_tmpl;
25202 else if (TYPE_P (t))
25203 return pa_type;
25204 else
25205 return pa_expr;
25206 }
25207
25208 /* Deduce the value of template parameters. TPARMS is the (innermost)
25209 set of template parameters to a template. TARGS is the bindings
25210 for those template parameters, as determined thus far; TARGS may
25211 include template arguments for outer levels of template parameters
25212 as well. PARM is a parameter to a template function, or a
25213 subcomponent of that parameter; ARG is the corresponding argument.
25214 This function attempts to match PARM with ARG in a manner
25215 consistent with the existing assignments in TARGS. If more values
25216 are deduced, then TARGS is updated.
25217
25218 Returns 0 if the type deduction succeeds, 1 otherwise. The
25219 parameter STRICT is a bitwise or of the following flags:
25220
25221 UNIFY_ALLOW_NONE:
25222 Require an exact match between PARM and ARG.
25223 UNIFY_ALLOW_MORE_CV_QUAL:
25224 Allow the deduced ARG to be more cv-qualified (by qualification
25225 conversion) than ARG.
25226 UNIFY_ALLOW_LESS_CV_QUAL:
25227 Allow the deduced ARG to be less cv-qualified than ARG.
25228 UNIFY_ALLOW_DERIVED:
25229 Allow the deduced ARG to be a template base class of ARG,
25230 or a pointer to a template base class of the type pointed to by
25231 ARG.
25232 UNIFY_ALLOW_INTEGER:
25233 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
25234 case for more information.
25235 UNIFY_ALLOW_OUTER_LEVEL:
25236 This is the outermost level of a deduction. Used to determine validity
25237 of qualification conversions. A valid qualification conversion must
25238 have const qualified pointers leading up to the inner type which
25239 requires additional CV quals, except at the outer level, where const
25240 is not required [conv.qual]. It would be normal to set this flag in
25241 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
25242 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
25243 This is the outermost level of a deduction, and PARM can be more CV
25244 qualified at this point.
25245 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
25246 This is the outermost level of a deduction, and PARM can be less CV
25247 qualified at this point. */
25248
25249 static int
25250 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
25251 bool explain_p)
25252 {
25253 int idx;
25254 tree targ;
25255 tree tparm;
25256 int strict_in = strict;
25257 tsubst_flags_t complain = (explain_p
25258 ? tf_warning_or_error
25259 : tf_none);
25260
25261 if (arg == error_mark_node)
25262 return unify_invalid (explain_p);
25263 if (arg == unknown_type_node
25264 || arg == init_list_type_node)
25265 /* We can't deduce anything from this, but we might get all the
25266 template args from other function args. */
25267 return unify_success (explain_p);
25268
25269 if (parm == any_targ_node || arg == any_targ_node)
25270 return unify_success (explain_p);
25271
25272 /* Strip conversions that will interfere with NTTP deduction.
25273 I don't think this will do the right thing with respect to types.
25274 But the only case I've seen it in so far has been array bounds, where
25275 signedness is the only information lost, and I think that will be
25276 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
25277 finish_id_expression_1, and are also OK. */
25278 if (deducible_expression (parm))
25279 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR
25280 || TREE_CODE (parm) == IMPLICIT_CONV_EXPR)
25281 parm = TREE_OPERAND (parm, 0);
25282
25283 /* If PARM uses template parameters, then we can't bail out here,
25284 even if ARG == PARM, since we won't record unifications for the
25285 template parameters. We might need them if we're trying to
25286 figure out which of two things is more specialized. */
25287 if (arg == parm
25288 && (DECL_P (parm) || !uses_template_parms (parm)))
25289 return unify_success (explain_p);
25290
25291 /* Handle init lists early, so the rest of the function can assume
25292 we're dealing with a type. */
25293 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
25294 {
25295 tree elttype;
25296 tree orig_parm = parm;
25297
25298 if (!is_std_init_list (parm)
25299 && TREE_CODE (parm) != ARRAY_TYPE)
25300 /* We can only deduce from an initializer list argument if the
25301 parameter is std::initializer_list or an array; otherwise this
25302 is a non-deduced context. */
25303 return unify_success (explain_p);
25304
25305 if (TREE_CODE (parm) == ARRAY_TYPE)
25306 elttype = TREE_TYPE (parm);
25307 else
25308 {
25309 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
25310 /* Deduction is defined in terms of a single type, so just punt
25311 on the (bizarre) std::initializer_list<T...>. */
25312 if (PACK_EXPANSION_P (elttype))
25313 return unify_success (explain_p);
25314 }
25315
25316 if (strict != DEDUCE_EXACT
25317 && TYPE_P (elttype)
25318 && !uses_deducible_template_parms (elttype))
25319 /* If ELTTYPE has no deducible template parms, skip deduction from
25320 the list elements. */;
25321 else
25322 for (auto &e: CONSTRUCTOR_ELTS (arg))
25323 {
25324 tree elt = e.value;
25325 int elt_strict = strict;
25326
25327 if (elt == error_mark_node)
25328 return unify_invalid (explain_p);
25329
25330 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
25331 {
25332 tree type = TREE_TYPE (elt);
25333 if (type == error_mark_node)
25334 return unify_invalid (explain_p);
25335 /* It should only be possible to get here for a call. */
25336 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
25337 elt_strict |= maybe_adjust_types_for_deduction
25338 (tparms, DEDUCE_CALL, &elttype, &type, elt);
25339 elt = type;
25340 }
25341
25342 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
25343 explain_p);
25344 }
25345
25346 if (TREE_CODE (parm) == ARRAY_TYPE
25347 && deducible_array_bound (TYPE_DOMAIN (parm)))
25348 {
25349 /* Also deduce from the length of the initializer list. */
25350 tree max = size_int (count_ctor_elements (arg));
25351 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
25352 if (idx == error_mark_node)
25353 return unify_invalid (explain_p);
25354 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
25355 idx, explain_p);
25356 }
25357
25358 /* If the std::initializer_list<T> deduction worked, replace the
25359 deduced A with std::initializer_list<A>. */
25360 if (orig_parm != parm)
25361 {
25362 idx = TEMPLATE_TYPE_IDX (orig_parm);
25363 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
25364 targ = listify (targ);
25365 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
25366 }
25367 return unify_success (explain_p);
25368 }
25369
25370 /* If parm and arg aren't the same kind of thing (template, type, or
25371 expression), fail early. */
25372 if (pa_kind (parm) != pa_kind (arg))
25373 return unify_invalid (explain_p);
25374
25375 /* Immediately reject some pairs that won't unify because of
25376 cv-qualification mismatches. */
25377 if (TREE_CODE (arg) == TREE_CODE (parm)
25378 && TYPE_P (arg)
25379 /* It is the elements of the array which hold the cv quals of an array
25380 type, and the elements might be template type parms. We'll check
25381 when we recurse. */
25382 && TREE_CODE (arg) != ARRAY_TYPE
25383 /* We check the cv-qualifiers when unifying with template type
25384 parameters below. We want to allow ARG `const T' to unify with
25385 PARM `T' for example, when computing which of two templates
25386 is more specialized, for example. */
25387 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
25388 && !check_cv_quals_for_unify (strict_in, arg, parm))
25389 return unify_cv_qual_mismatch (explain_p, parm, arg);
25390
25391 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
25392 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
25393 && !FUNC_OR_METHOD_TYPE_P (parm))
25394 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
25395 /* PMFs recurse at the same level, so don't strip this yet. */
25396 if (!TYPE_PTRMEMFUNC_P (parm))
25397 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
25398 strict &= ~UNIFY_ALLOW_DERIVED;
25399 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
25400 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
25401
25402 switch (TREE_CODE (parm))
25403 {
25404 case TYPENAME_TYPE:
25405 case SCOPE_REF:
25406 case UNBOUND_CLASS_TEMPLATE:
25407 /* In a type which contains a nested-name-specifier, template
25408 argument values cannot be deduced for template parameters used
25409 within the nested-name-specifier. */
25410 return unify_success (explain_p);
25411
25412 case TEMPLATE_TYPE_PARM:
25413 case TEMPLATE_TEMPLATE_PARM:
25414 case BOUND_TEMPLATE_TEMPLATE_PARM:
25415 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
25416 if (error_operand_p (tparm))
25417 return unify_invalid (explain_p);
25418
25419 if (TEMPLATE_TYPE_LEVEL (parm)
25420 != template_decl_level (tparm))
25421 /* The PARM is not one we're trying to unify. Just check
25422 to see if it matches ARG. */
25423 {
25424 if (TREE_CODE (arg) == TREE_CODE (parm)
25425 && (is_auto (parm) ? is_auto (arg)
25426 : same_type_p (parm, arg)))
25427 return unify_success (explain_p);
25428 else
25429 return unify_type_mismatch (explain_p, parm, arg);
25430 }
25431 idx = TEMPLATE_TYPE_IDX (parm);
25432 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
25433 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
25434 if (error_operand_p (tparm))
25435 return unify_invalid (explain_p);
25436
25437 /* Check for mixed types and values. */
25438 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
25439 && TREE_CODE (tparm) != TYPE_DECL)
25440 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
25441 && TREE_CODE (tparm) != TEMPLATE_DECL))
25442 gcc_unreachable ();
25443
25444 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
25445 {
25446 if ((strict_in & UNIFY_ALLOW_DERIVED)
25447 && CLASS_TYPE_P (arg))
25448 {
25449 /* First try to match ARG directly. */
25450 tree t = try_class_unification (tparms, targs, parm, arg,
25451 explain_p);
25452 if (!t)
25453 {
25454 /* Otherwise, look for a suitable base of ARG, as below. */
25455 enum template_base_result r;
25456 r = get_template_base (tparms, targs, parm, arg,
25457 explain_p, &t);
25458 if (!t)
25459 return unify_no_common_base (explain_p, r, parm, arg);
25460 arg = t;
25461 }
25462 }
25463 /* ARG must be constructed from a template class or a template
25464 template parameter. */
25465 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
25466 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
25467 return unify_template_deduction_failure (explain_p, parm, arg);
25468
25469 /* Deduce arguments T, i from TT<T> or TT<i>. */
25470 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
25471 return 1;
25472
25473 arg = TYPE_TI_TEMPLATE (arg);
25474 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
25475 /* If the template is a template template parameter, use the
25476 TEMPLATE_TEMPLATE_PARM for matching. */
25477 arg = TREE_TYPE (arg);
25478
25479 /* Fall through to deduce template name. */
25480 }
25481
25482 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
25483 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
25484 {
25485 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
25486
25487 /* Simple cases: Value already set, does match or doesn't. */
25488 if (targ != NULL_TREE && template_args_equal (targ, arg))
25489 return unify_success (explain_p);
25490 else if (targ)
25491 return unify_inconsistency (explain_p, parm, targ, arg);
25492 }
25493 else
25494 {
25495 /* If PARM is `const T' and ARG is only `int', we don't have
25496 a match unless we are allowing additional qualification.
25497 If ARG is `const int' and PARM is just `T' that's OK;
25498 that binds `const int' to `T'. */
25499 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
25500 arg, parm))
25501 return unify_cv_qual_mismatch (explain_p, parm, arg);
25502
25503 /* Consider the case where ARG is `const volatile int' and
25504 PARM is `const T'. Then, T should be `volatile int'. */
25505 arg = cp_build_qualified_type
25506 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
25507 if (arg == error_mark_node)
25508 return unify_invalid (explain_p);
25509
25510 /* Simple cases: Value already set, does match or doesn't. */
25511 if (targ != NULL_TREE && same_type_p (targ, arg))
25512 return unify_success (explain_p);
25513 else if (targ)
25514 return unify_inconsistency (explain_p, parm, targ, arg);
25515
25516 /* Make sure that ARG is not a variable-sized array. (Note
25517 that were talking about variable-sized arrays (like
25518 `int[n]'), rather than arrays of unknown size (like
25519 `int[]').) We'll get very confused by such a type since
25520 the bound of the array is not constant, and therefore
25521 not mangleable. Besides, such types are not allowed in
25522 ISO C++, so we can do as we please here. We do allow
25523 them for 'auto' deduction, since that isn't ABI-exposed. */
25524 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
25525 return unify_vla_arg (explain_p, arg);
25526
25527 /* Strip typedefs as in convert_template_argument. */
25528 arg = canonicalize_type_argument (arg, tf_none);
25529 }
25530
25531 /* If ARG is a parameter pack or an expansion, we cannot unify
25532 against it unless PARM is also a parameter pack. */
25533 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
25534 && !template_parameter_pack_p (parm))
25535 return unify_parameter_pack_mismatch (explain_p, parm, arg);
25536
25537 /* If the argument deduction results is a METHOD_TYPE,
25538 then there is a problem.
25539 METHOD_TYPE doesn't map to any real C++ type the result of
25540 the deduction cannot be of that type. */
25541 if (TREE_CODE (arg) == METHOD_TYPE)
25542 return unify_method_type_error (explain_p, arg);
25543
25544 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
25545 return unify_success (explain_p);
25546
25547 case TEMPLATE_PARM_INDEX:
25548 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
25549 if (error_operand_p (tparm))
25550 return unify_invalid (explain_p);
25551
25552 if (TEMPLATE_PARM_LEVEL (parm)
25553 != template_decl_level (tparm))
25554 {
25555 /* The PARM is not one we're trying to unify. Just check
25556 to see if it matches ARG. */
25557 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
25558 && cp_tree_equal (parm, arg));
25559 if (result)
25560 unify_expression_unequal (explain_p, parm, arg);
25561 return result;
25562 }
25563
25564 idx = TEMPLATE_PARM_IDX (parm);
25565 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
25566
25567 if (targ)
25568 {
25569 if ((strict & UNIFY_ALLOW_INTEGER)
25570 && TREE_TYPE (targ) && TREE_TYPE (arg)
25571 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
25572 /* We're deducing from an array bound, the type doesn't matter.
25573 This conversion should match the one below. */
25574 arg = fold (build_nop (TREE_TYPE (targ), arg));
25575 int x = !cp_tree_equal (targ, arg);
25576 if (x)
25577 unify_inconsistency (explain_p, parm, targ, arg);
25578 return x;
25579 }
25580
25581 /* [temp.deduct.type] If, in the declaration of a function template
25582 with a non-type template-parameter, the non-type
25583 template-parameter is used in an expression in the function
25584 parameter-list and, if the corresponding template-argument is
25585 deduced, the template-argument type shall match the type of the
25586 template-parameter exactly, except that a template-argument
25587 deduced from an array bound may be of any integral type.
25588 The non-type parameter might use already deduced type parameters. */
25589 tparm = TREE_TYPE (parm);
25590 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
25591 /* We don't have enough levels of args to do any substitution. This
25592 can happen in the context of -fnew-ttp-matching. */;
25593 else
25594 {
25595 ++processing_template_decl;
25596 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
25597 --processing_template_decl;
25598
25599 if (tree a = type_uses_auto (tparm))
25600 {
25601 tparm = do_auto_deduction (tparm, arg, a,
25602 complain, adc_unify, targs,
25603 LOOKUP_NORMAL,
25604 TPARMS_PRIMARY_TEMPLATE (tparms));
25605 if (tparm == error_mark_node)
25606 return 1;
25607 }
25608 }
25609
25610 if (!TREE_TYPE (arg)
25611 || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
25612 /* Template-parameter dependent expression. Just accept it for now.
25613 It will later be processed in convert_template_argument. */
25614 ;
25615 else if (same_type_ignoring_top_level_qualifiers_p
25616 (non_reference (TREE_TYPE (arg)),
25617 non_reference (tparm)))
25618 /* OK. Ignore top-level quals here because a class-type template
25619 parameter object is const. */;
25620 else if ((strict & UNIFY_ALLOW_INTEGER)
25621 && CP_INTEGRAL_TYPE_P (tparm))
25622 /* Convert the ARG to the type of PARM; the deduced non-type
25623 template argument must exactly match the types of the
25624 corresponding parameter. This conversion should match the
25625 one above. */
25626 arg = fold (build_nop (tparm, arg));
25627 else if (uses_template_parms (tparm))
25628 {
25629 /* We haven't deduced the type of this parameter yet. */
25630 if (cxx_dialect >= cxx17
25631 /* We deduce from array bounds in try_array_deduction. */
25632 && !(strict & UNIFY_ALLOW_INTEGER)
25633 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
25634 {
25635 /* Deduce it from the non-type argument. As above, ignore
25636 top-level quals here too. */
25637 tree atype = cv_unqualified (TREE_TYPE (arg));
25638 RECUR_AND_CHECK_FAILURE (tparms, targs,
25639 tparm, atype,
25640 UNIFY_ALLOW_NONE, explain_p);
25641 /* Now check whether the type of this parameter is still
25642 dependent, and give up if so. */
25643 ++processing_template_decl;
25644 tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
25645 --processing_template_decl;
25646 if (uses_template_parms (tparm))
25647 return unify_success (explain_p);
25648 }
25649 else
25650 /* Try again later. */
25651 return unify_success (explain_p);
25652 }
25653 else
25654 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
25655
25656 /* If ARG is a parameter pack or an expansion, we cannot unify
25657 against it unless PARM is also a parameter pack. */
25658 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
25659 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
25660 return unify_parameter_pack_mismatch (explain_p, parm, arg);
25661
25662 {
25663 bool removed_attr = false;
25664 arg = strip_typedefs_expr (arg, &removed_attr);
25665 }
25666 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
25667 return unify_success (explain_p);
25668
25669 case PTRMEM_CST:
25670 {
25671 /* A pointer-to-member constant can be unified only with
25672 another constant. */
25673 if (TREE_CODE (arg) != PTRMEM_CST)
25674 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
25675
25676 /* Just unify the class member. It would be useless (and possibly
25677 wrong, depending on the strict flags) to unify also
25678 PTRMEM_CST_CLASS, because we want to be sure that both parm and
25679 arg refer to the same variable, even if through different
25680 classes. For instance:
25681
25682 struct A { int x; };
25683 struct B : A { };
25684
25685 Unification of &A::x and &B::x must succeed. */
25686 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
25687 PTRMEM_CST_MEMBER (arg), strict, explain_p);
25688 }
25689
25690 case POINTER_TYPE:
25691 {
25692 if (!TYPE_PTR_P (arg))
25693 return unify_type_mismatch (explain_p, parm, arg);
25694
25695 /* [temp.deduct.call]
25696
25697 A can be another pointer or pointer to member type that can
25698 be converted to the deduced A via a qualification
25699 conversion (_conv.qual_).
25700
25701 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
25702 This will allow for additional cv-qualification of the
25703 pointed-to types if appropriate. */
25704
25705 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
25706 /* The derived-to-base conversion only persists through one
25707 level of pointers. */
25708 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
25709
25710 return unify (tparms, targs, TREE_TYPE (parm),
25711 TREE_TYPE (arg), strict, explain_p);
25712 }
25713
25714 case REFERENCE_TYPE:
25715 if (!TYPE_REF_P (arg)
25716 || TYPE_REF_IS_RVALUE (parm) != TYPE_REF_IS_RVALUE (arg))
25717 return unify_type_mismatch (explain_p, parm, arg);
25718 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25719 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25720
25721 case ARRAY_TYPE:
25722 if (TREE_CODE (arg) != ARRAY_TYPE)
25723 return unify_type_mismatch (explain_p, parm, arg);
25724 if ((TYPE_DOMAIN (parm) == NULL_TREE)
25725 != (TYPE_DOMAIN (arg) == NULL_TREE))
25726 return unify_type_mismatch (explain_p, parm, arg);
25727 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25728 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25729 if (TYPE_DOMAIN (parm) != NULL_TREE)
25730 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
25731 TYPE_DOMAIN (arg), explain_p);
25732 return unify_success (explain_p);
25733
25734 case REAL_TYPE:
25735 case COMPLEX_TYPE:
25736 case VECTOR_TYPE:
25737 case INTEGER_TYPE:
25738 case BOOLEAN_TYPE:
25739 case ENUMERAL_TYPE:
25740 case VOID_TYPE:
25741 case OPAQUE_TYPE:
25742 case NULLPTR_TYPE:
25743 if (TREE_CODE (arg) != TREE_CODE (parm))
25744 return unify_type_mismatch (explain_p, parm, arg);
25745
25746 /* We have already checked cv-qualification at the top of the
25747 function. */
25748 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
25749 return unify_type_mismatch (explain_p, parm, arg);
25750
25751 /* As far as unification is concerned, this wins. Later checks
25752 will invalidate it if necessary. */
25753 return unify_success (explain_p);
25754
25755 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
25756 /* Type INTEGER_CST can come from ordinary constant template args. */
25757 case INTEGER_CST:
25758 case REAL_CST:
25759 if (TREE_TYPE (arg) == NULL_TREE
25760 || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
25761 return unify_template_argument_mismatch (explain_p, parm, arg);
25762 while (CONVERT_EXPR_P (arg))
25763 arg = TREE_OPERAND (arg, 0);
25764
25765 if (TREE_CODE (arg) != TREE_CODE (parm))
25766 return unify_template_argument_mismatch (explain_p, parm, arg);
25767 return (simple_cst_equal (parm, arg)
25768 ? unify_success (explain_p)
25769 : unify_template_argument_mismatch (explain_p, parm, arg));
25770
25771 case TREE_VEC:
25772 {
25773 int i, len, argslen;
25774 int parm_variadic_p = 0;
25775
25776 if (TREE_CODE (arg) != TREE_VEC)
25777 return unify_template_argument_mismatch (explain_p, parm, arg);
25778
25779 len = TREE_VEC_LENGTH (parm);
25780 argslen = TREE_VEC_LENGTH (arg);
25781
25782 /* Check for pack expansions in the parameters. */
25783 for (i = 0; i < len; ++i)
25784 {
25785 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
25786 {
25787 if (i == len - 1)
25788 /* We can unify against something with a trailing
25789 parameter pack. */
25790 parm_variadic_p = 1;
25791 else
25792 /* [temp.deduct.type]/9: If the template argument list of
25793 P contains a pack expansion that is not the last
25794 template argument, the entire template argument list
25795 is a non-deduced context. */
25796 return unify_success (explain_p);
25797 }
25798 }
25799
25800 /* If we don't have enough arguments to satisfy the parameters
25801 (not counting the pack expression at the end), or we have
25802 too many arguments for a parameter list that doesn't end in
25803 a pack expression, we can't unify. */
25804 if (parm_variadic_p
25805 ? argslen < len - parm_variadic_p
25806 : argslen != len)
25807 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
25808
25809 /* Unify all of the parameters that precede the (optional)
25810 pack expression. */
25811 for (i = 0; i < len - parm_variadic_p; ++i)
25812 {
25813 RECUR_AND_CHECK_FAILURE (tparms, targs,
25814 TREE_VEC_ELT (parm, i),
25815 TREE_VEC_ELT (arg, i),
25816 UNIFY_ALLOW_NONE, explain_p);
25817 }
25818 if (parm_variadic_p)
25819 return unify_pack_expansion (tparms, targs, parm, arg,
25820 DEDUCE_EXACT,
25821 /*subr=*/true, explain_p);
25822 return unify_success (explain_p);
25823 }
25824
25825 case RECORD_TYPE:
25826 case UNION_TYPE:
25827 if (TREE_CODE (arg) != TREE_CODE (parm))
25828 return unify_type_mismatch (explain_p, parm, arg);
25829
25830 if (TYPE_PTRMEMFUNC_P (parm))
25831 {
25832 if (!TYPE_PTRMEMFUNC_P (arg))
25833 return unify_type_mismatch (explain_p, parm, arg);
25834
25835 return unify (tparms, targs,
25836 TYPE_PTRMEMFUNC_FN_TYPE (parm),
25837 TYPE_PTRMEMFUNC_FN_TYPE (arg),
25838 strict, explain_p);
25839 }
25840 else if (TYPE_PTRMEMFUNC_P (arg))
25841 return unify_type_mismatch (explain_p, parm, arg);
25842
25843 if (CLASSTYPE_TEMPLATE_INFO (parm))
25844 {
25845 tree t = NULL_TREE;
25846
25847 if (strict_in & UNIFY_ALLOW_DERIVED)
25848 {
25849 /* First, we try to unify the PARM and ARG directly. */
25850 t = try_class_unification (tparms, targs,
25851 parm, arg, explain_p);
25852
25853 if (!t)
25854 {
25855 /* Fallback to the special case allowed in
25856 [temp.deduct.call]:
25857
25858 If P is a class, and P has the form
25859 template-id, then A can be a derived class of
25860 the deduced A. Likewise, if P is a pointer to
25861 a class of the form template-id, A can be a
25862 pointer to a derived class pointed to by the
25863 deduced A. */
25864 enum template_base_result r;
25865 r = get_template_base (tparms, targs, parm, arg,
25866 explain_p, &t);
25867
25868 if (!t)
25869 {
25870 /* Don't give the derived diagnostic if we're
25871 already dealing with the same template. */
25872 bool same_template
25873 = (CLASSTYPE_TEMPLATE_INFO (arg)
25874 && (CLASSTYPE_TI_TEMPLATE (parm)
25875 == CLASSTYPE_TI_TEMPLATE (arg)));
25876 return unify_no_common_base (explain_p && !same_template,
25877 r, parm, arg);
25878 }
25879 }
25880 }
25881 else if (CLASSTYPE_TEMPLATE_INFO (arg)
25882 && (CLASSTYPE_TI_TEMPLATE (parm)
25883 == CLASSTYPE_TI_TEMPLATE (arg)))
25884 /* Perhaps PARM is something like S<U> and ARG is S<int>.
25885 Then, we should unify `int' and `U'. */
25886 t = arg;
25887 else
25888 /* There's no chance of unification succeeding. */
25889 return unify_type_mismatch (explain_p, parm, arg);
25890
25891 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
25892 return unify (tparms, targs,
25893 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
25894 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
25895 UNIFY_ALLOW_NONE, explain_p);
25896 gcc_checking_assert (t == arg);
25897 }
25898
25899 if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
25900 return unify_type_mismatch (explain_p, parm, arg);
25901 return unify_success (explain_p);
25902
25903 case METHOD_TYPE:
25904 case FUNCTION_TYPE:
25905 {
25906 unsigned int nargs;
25907 tree *args;
25908 tree a;
25909 unsigned int i;
25910
25911 if (TREE_CODE (arg) != TREE_CODE (parm))
25912 return unify_type_mismatch (explain_p, parm, arg);
25913
25914 /* CV qualifications for methods can never be deduced, they must
25915 match exactly. We need to check them explicitly here,
25916 because type_unification_real treats them as any other
25917 cv-qualified parameter. */
25918 if (TREE_CODE (parm) == METHOD_TYPE
25919 && (!check_cv_quals_for_unify
25920 (UNIFY_ALLOW_NONE,
25921 class_of_this_parm (arg),
25922 class_of_this_parm (parm))))
25923 return unify_cv_qual_mismatch (explain_p, parm, arg);
25924 if (TREE_CODE (arg) == FUNCTION_TYPE
25925 && type_memfn_quals (parm) != type_memfn_quals (arg))
25926 return unify_cv_qual_mismatch (explain_p, parm, arg);
25927 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
25928 return unify_type_mismatch (explain_p, parm, arg);
25929
25930 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
25931 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
25932
25933 nargs = list_length (TYPE_ARG_TYPES (arg));
25934 args = XALLOCAVEC (tree, nargs);
25935 for (a = TYPE_ARG_TYPES (arg), i = 0;
25936 a != NULL_TREE && a != void_list_node;
25937 a = TREE_CHAIN (a), ++i)
25938 args[i] = TREE_VALUE (a);
25939 nargs = i;
25940
25941 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
25942 args, nargs, 1, DEDUCE_EXACT,
25943 NULL, explain_p))
25944 return 1;
25945
25946 if (flag_noexcept_type)
25947 {
25948 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
25949 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
25950 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
25951 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
25952 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
25953 && uses_template_parms (TREE_PURPOSE (pspec)))
25954 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
25955 TREE_PURPOSE (aspec),
25956 UNIFY_ALLOW_NONE, explain_p);
25957 else
25958 {
25959 bool pn = nothrow_spec_p (pspec);
25960 bool an = nothrow_spec_p (aspec);
25961 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
25962 /more/ noexcept, since function pointer conversions are the
25963 reverse of qualification conversions. */
25964 if (an == pn
25965 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25966 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25967 /* OK. */;
25968 else
25969 return unify_type_mismatch (explain_p, parm, arg);
25970 }
25971 }
25972 if (flag_tm)
25973 {
25974 /* As for noexcept. */
25975 bool pn = tx_safe_fn_type_p (parm);
25976 bool an = tx_safe_fn_type_p (arg);
25977 if (an == pn
25978 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25979 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25980 /* OK. */;
25981 else
25982 return unify_type_mismatch (explain_p, parm, arg);
25983 }
25984
25985 return 0;
25986 }
25987
25988 case OFFSET_TYPE:
25989 /* Unify a pointer to member with a pointer to member function, which
25990 deduces the type of the member as a function type. */
25991 if (TYPE_PTRMEMFUNC_P (arg))
25992 {
25993 /* Check top-level cv qualifiers */
25994 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
25995 return unify_cv_qual_mismatch (explain_p, parm, arg);
25996
25997 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
25998 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
25999 UNIFY_ALLOW_NONE, explain_p);
26000
26001 /* Determine the type of the function we are unifying against. */
26002 tree fntype = static_fn_type (arg);
26003
26004 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
26005 }
26006
26007 if (TREE_CODE (arg) != OFFSET_TYPE)
26008 return unify_type_mismatch (explain_p, parm, arg);
26009 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
26010 TYPE_OFFSET_BASETYPE (arg),
26011 UNIFY_ALLOW_NONE, explain_p);
26012 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
26013 strict, explain_p);
26014
26015 case CONST_DECL:
26016 /* CONST_DECL should already have been folded to its DECL_INITIAL. */
26017 gcc_unreachable ();
26018
26019 case FIELD_DECL:
26020 case FUNCTION_DECL:
26021 case TEMPLATE_DECL:
26022 /* Matched cases are handled by the ARG == PARM test above. */
26023 return unify_template_argument_mismatch (explain_p, parm, arg);
26024
26025 case VAR_DECL:
26026 /* We might get a variable as a non-type template argument in parm if the
26027 corresponding parameter is type-dependent. Make any necessary
26028 adjustments based on whether arg is a reference. */
26029 if (CONSTANT_CLASS_P (arg))
26030 parm = fold_non_dependent_expr (parm, complain);
26031 else if (REFERENCE_REF_P (arg))
26032 {
26033 tree sub = TREE_OPERAND (arg, 0);
26034 STRIP_NOPS (sub);
26035 if (TREE_CODE (sub) == ADDR_EXPR)
26036 arg = TREE_OPERAND (sub, 0);
26037 }
26038 /* Now use the normal expression code to check whether they match. */
26039 goto expr;
26040
26041 case TYPE_ARGUMENT_PACK:
26042 case NONTYPE_ARGUMENT_PACK:
26043 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
26044 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
26045
26046 case TYPEOF_TYPE:
26047 case DECLTYPE_TYPE:
26048 case TRAIT_TYPE:
26049 case PACK_INDEX_TYPE:
26050 /* These are non-deduced contexts. */
26051 return unify_success (explain_p);
26052
26053 case ERROR_MARK:
26054 /* Unification fails if we hit an error node. */
26055 return unify_invalid (explain_p);
26056
26057 case INDIRECT_REF:
26058 if (REFERENCE_REF_P (parm))
26059 {
26060 bool pexp = PACK_EXPANSION_P (arg);
26061 if (pexp)
26062 arg = PACK_EXPANSION_PATTERN (arg);
26063 if (REFERENCE_REF_P (arg))
26064 arg = TREE_OPERAND (arg, 0);
26065 if (pexp)
26066 arg = make_pack_expansion (arg, complain);
26067 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
26068 strict, explain_p);
26069 }
26070 /* FALLTHRU */
26071
26072 default:
26073 /* An unresolved overload is a nondeduced context. */
26074 if (is_overloaded_fn (parm) || type_unknown_p (parm))
26075 return unify_success (explain_p);
26076 gcc_assert (EXPR_P (parm)
26077 || TREE_CODE (parm) == CONSTRUCTOR
26078 || TREE_CODE (parm) == LAMBDA_EXPR
26079 || TREE_CODE (parm) == TRAIT_EXPR);
26080 expr:
26081 /* We must be looking at an expression. This can happen with
26082 something like:
26083
26084 template <int I>
26085 void foo(S<I>, S<I + 2>);
26086
26087 or
26088
26089 template<typename T>
26090 void foo(A<T, T{}>);
26091
26092 This is a "non-deduced context":
26093
26094 [deduct.type]
26095
26096 The non-deduced contexts are:
26097
26098 --A non-type template argument or an array bound in which
26099 a subexpression references a template parameter.
26100
26101 In these cases, we assume deduction succeeded, but don't
26102 actually infer any unifications. */
26103
26104 if (!uses_template_parms (parm)
26105 && !template_args_equal (parm, arg))
26106 return unify_expression_unequal (explain_p, parm, arg);
26107 else
26108 return unify_success (explain_p);
26109 }
26110 }
26111 #undef RECUR_AND_CHECK_FAILURE
26112
26113 /* Note that DECL can be defined in this translation unit, if
26114 required. */
26115
26116 static void
26117 mark_definable (tree decl)
26118 {
26119 tree clone;
26120 DECL_NOT_REALLY_EXTERN (decl) = 1;
26121 FOR_EACH_CLONE (clone, decl)
26122 DECL_NOT_REALLY_EXTERN (clone) = 1;
26123 }
26124
26125 /* DECL is an explicit instantiation definition, ensure that it will
26126 be written out here and that it won't clash with other instantiations
26127 in other translation units. */
26128
26129 void
26130 setup_explicit_instantiation_definition_linkage (tree decl)
26131 {
26132 mark_definable (decl);
26133 mark_needed (decl);
26134 /* Always make artificials weak. */
26135 if (DECL_ARTIFICIAL (decl) && flag_weak)
26136 comdat_linkage (decl);
26137 /* We also want to put explicit instantiations in linkonce sections. */
26138 else if (TREE_PUBLIC (decl))
26139 maybe_make_one_only (decl);
26140 }
26141
26142 /* Called if RESULT is explicitly instantiated, or is a member of an
26143 explicitly instantiated class. */
26144
26145 void
26146 mark_decl_instantiated (tree result, int extern_p)
26147 {
26148 SET_DECL_EXPLICIT_INSTANTIATION (result);
26149
26150 /* If this entity has already been written out, it's too late to
26151 make any modifications. */
26152 if (TREE_ASM_WRITTEN (result))
26153 return;
26154
26155 /* consteval functions are never emitted. */
26156 if (TREE_CODE (result) == FUNCTION_DECL
26157 && DECL_IMMEDIATE_FUNCTION_P (result))
26158 return;
26159
26160 /* For anonymous namespace we don't need to do anything. */
26161 if (decl_internal_context_p (result))
26162 {
26163 gcc_assert (!TREE_PUBLIC (result));
26164 return;
26165 }
26166
26167 if (TREE_CODE (result) != FUNCTION_DECL)
26168 /* The TREE_PUBLIC flag for function declarations will have been
26169 set correctly by tsubst. */
26170 TREE_PUBLIC (result) = 1;
26171
26172 if (extern_p)
26173 {
26174 DECL_EXTERNAL (result) = 1;
26175 DECL_NOT_REALLY_EXTERN (result) = 0;
26176 }
26177 else
26178 {
26179 set_instantiating_module (result);
26180 setup_explicit_instantiation_definition_linkage (result);
26181 if (TREE_CODE (result) == FUNCTION_DECL
26182 && DECL_TEMPLATE_INSTANTIATED (result))
26183 /* If the function has already been instantiated, clear DECL_EXTERNAL,
26184 since start_preparsed_function wouldn't have if we had an earlier
26185 extern explicit instantiation. */
26186 DECL_EXTERNAL (result) = 0;
26187 }
26188
26189 /* If EXTERN_P, then this function will not be emitted -- unless
26190 followed by an explicit instantiation, at which point its linkage
26191 will be adjusted. If !EXTERN_P, then this function will be
26192 emitted here. In neither circumstance do we want
26193 import_export_decl to adjust the linkage. */
26194 DECL_INTERFACE_KNOWN (result) = 1;
26195 }
26196
26197 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
26198 important template arguments. If any are missing, we check whether
26199 they're important by using error_mark_node for substituting into any
26200 args that were used for partial ordering (the ones between ARGS and END)
26201 and seeing if it bubbles up. */
26202
26203 static bool
26204 check_undeduced_parms (tree targs, tree args, tree end)
26205 {
26206 bool found = false;
26207 for (tree& targ : tree_vec_range (targs))
26208 if (targ == NULL_TREE)
26209 {
26210 found = true;
26211 targ = error_mark_node;
26212 }
26213 if (found)
26214 {
26215 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
26216 if (substed == error_mark_node)
26217 return true;
26218 }
26219 return false;
26220 }
26221
26222 /* Given two function templates PAT1 and PAT2, return:
26223
26224 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
26225 -1 if PAT2 is more specialized than PAT1.
26226 0 if neither is more specialized.
26227
26228 LEN indicates the number of parameters we should consider
26229 (defaulted parameters should not be considered).
26230
26231 The 1998 std underspecified function template partial ordering, and
26232 DR214 addresses the issue. We take pairs of arguments, one from
26233 each of the templates, and deduce them against each other. One of
26234 the templates will be more specialized if all the *other*
26235 template's arguments deduce against its arguments and at least one
26236 of its arguments *does* *not* deduce against the other template's
26237 corresponding argument. Deduction is done as for class templates.
26238 The arguments used in deduction have reference and top level cv
26239 qualifiers removed. Iff both arguments were originally reference
26240 types *and* deduction succeeds in both directions, an lvalue reference
26241 wins against an rvalue reference and otherwise the template
26242 with the more cv-qualified argument wins for that pairing (if
26243 neither is more cv-qualified, they both are equal). Unlike regular
26244 deduction, after all the arguments have been deduced in this way,
26245 we do *not* verify the deduced template argument values can be
26246 substituted into non-deduced contexts.
26247
26248 The logic can be a bit confusing here, because we look at deduce1 and
26249 targs1 to see if pat2 is at least as specialized, and vice versa; if we
26250 can find template arguments for pat1 to make arg1 look like arg2, that
26251 means that arg2 is at least as specialized as arg1. */
26252
26253 int
26254 more_specialized_fn (tree pat1, tree pat2, int len)
26255 {
26256 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
26257 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
26258 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
26259 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
26260 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
26261 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
26262 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
26263 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
26264 tree origs1, origs2;
26265 bool lose1 = false;
26266 bool lose2 = false;
26267
26268 /* C++17 [temp.func.order]/3 (CWG532)
26269
26270 If only one of the function templates M is a non-static member of some
26271 class A, M is considered to have a new first parameter inserted in its
26272 function parameter list. Given cv as the cv-qualifiers of M (if any), the
26273 new parameter is of type "rvalue reference to cv A" if the optional
26274 ref-qualifier of M is && or if M has no ref-qualifier and the first
26275 parameter of the other template has rvalue reference type. Otherwise, the
26276 new parameter is of type "lvalue reference to cv A". */
26277
26278 if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
26279 {
26280 /* Note C++20 DR2445 extended the above to static member functions, but
26281 I think the old G++ behavior of just skipping the object
26282 parameter when comparing to a static member function was better, so
26283 let's stick with that for now. This is CWG2834. --jason 2023-12 */
26284 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
26285 {
26286 len--; /* LEN is the number of significant arguments for DECL1 */
26287 args1 = TREE_CHAIN (args1);
26288 }
26289 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
26290 args2 = TREE_CHAIN (args2);
26291 }
26292 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
26293 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
26294 {
26295 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
26296 would break e.g. cpp1y/lambda-generic-variadic5.C. */
26297 len--;
26298 args1 = TREE_CHAIN (args1);
26299 args2 = TREE_CHAIN (args2);
26300 }
26301 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
26302 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
26303 {
26304 /* The other is a non-member or explicit object member function;
26305 rewrite the implicit object parameter to a reference. */
26306 tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
26307 tree &nsargs = ns == decl2 ? args2 : args1;
26308 tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
26309
26310 nsargs = TREE_CHAIN (nsargs);
26311
26312 cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
26313 if (rqual == REF_QUAL_NONE)
26314 {
26315 tree otherfirst = ns == decl1 ? args2 : args1;
26316 otherfirst = TREE_VALUE (otherfirst);
26317 if (TREE_CODE (otherfirst) == REFERENCE_TYPE
26318 && TYPE_REF_IS_RVALUE (otherfirst))
26319 rqual = REF_QUAL_RVALUE;
26320 }
26321 obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
26322 nsargs = tree_cons (NULL_TREE, obtype, nsargs);
26323 }
26324
26325 /* If only one is a conversion operator, they are unordered. */
26326 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
26327 return 0;
26328
26329 /* Consider the return type for a conversion function */
26330 if (DECL_CONV_FN_P (decl1))
26331 {
26332 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
26333 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
26334 len++;
26335 }
26336
26337 processing_template_decl++;
26338
26339 origs1 = args1;
26340 origs2 = args2;
26341
26342 while (len--
26343 /* Stop when an ellipsis is seen. */
26344 && args1 != NULL_TREE && args2 != NULL_TREE)
26345 {
26346 tree arg1 = TREE_VALUE (args1);
26347 tree arg2 = TREE_VALUE (args2);
26348 int deduce1, deduce2;
26349 int quals1 = -1;
26350 int quals2 = -1;
26351 int ref1 = 0;
26352 int ref2 = 0;
26353
26354 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
26355 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
26356 {
26357 /* When both arguments are pack expansions, we need only
26358 unify the patterns themselves. */
26359 arg1 = PACK_EXPANSION_PATTERN (arg1);
26360 arg2 = PACK_EXPANSION_PATTERN (arg2);
26361
26362 /* This is the last comparison we need to do. */
26363 len = 0;
26364 }
26365
26366 if (TYPE_REF_P (arg1))
26367 {
26368 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
26369 arg1 = TREE_TYPE (arg1);
26370 quals1 = cp_type_quals (arg1);
26371 }
26372
26373 if (TYPE_REF_P (arg2))
26374 {
26375 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
26376 arg2 = TREE_TYPE (arg2);
26377 quals2 = cp_type_quals (arg2);
26378 }
26379
26380 arg1 = TYPE_MAIN_VARIANT (arg1);
26381 arg2 = TYPE_MAIN_VARIANT (arg2);
26382
26383 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
26384 {
26385 int i, len2 = remaining_arguments (args2);
26386 tree parmvec = make_tree_vec (1);
26387 tree argvec = make_tree_vec (len2);
26388 tree ta = args2;
26389
26390 /* Setup the parameter vector, which contains only ARG1. */
26391 TREE_VEC_ELT (parmvec, 0) = arg1;
26392
26393 /* Setup the argument vector, which contains the remaining
26394 arguments. */
26395 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
26396 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
26397
26398 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
26399 argvec, DEDUCE_EXACT,
26400 /*subr=*/true, /*explain_p=*/false)
26401 == 0);
26402
26403 /* We cannot deduce in the other direction, because ARG1 is
26404 a pack expansion but ARG2 is not. */
26405 deduce2 = 0;
26406 }
26407 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
26408 {
26409 int i, len1 = remaining_arguments (args1);
26410 tree parmvec = make_tree_vec (1);
26411 tree argvec = make_tree_vec (len1);
26412 tree ta = args1;
26413
26414 /* Setup the parameter vector, which contains only ARG1. */
26415 TREE_VEC_ELT (parmvec, 0) = arg2;
26416
26417 /* Setup the argument vector, which contains the remaining
26418 arguments. */
26419 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
26420 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
26421
26422 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
26423 argvec, DEDUCE_EXACT,
26424 /*subr=*/true, /*explain_p=*/false)
26425 == 0);
26426
26427 /* We cannot deduce in the other direction, because ARG2 is
26428 a pack expansion but ARG1 is not.*/
26429 deduce1 = 0;
26430 }
26431
26432 else
26433 {
26434 /* The normal case, where neither argument is a pack
26435 expansion. */
26436 deduce1 = (unify (tparms1, targs1, arg1, arg2,
26437 UNIFY_ALLOW_NONE, /*explain_p=*/false)
26438 == 0);
26439 deduce2 = (unify (tparms2, targs2, arg2, arg1,
26440 UNIFY_ALLOW_NONE, /*explain_p=*/false)
26441 == 0);
26442 }
26443
26444 /* If we couldn't deduce arguments for tparms1 to make arg1 match
26445 arg2, then arg2 is not as specialized as arg1. */
26446 if (!deduce1)
26447 lose2 = true;
26448 if (!deduce2)
26449 lose1 = true;
26450
26451 /* "If, for a given type, deduction succeeds in both directions
26452 (i.e., the types are identical after the transformations above)
26453 and both P and A were reference types (before being replaced with
26454 the type referred to above):
26455 - if the type from the argument template was an lvalue reference and
26456 the type from the parameter template was not, the argument type is
26457 considered to be more specialized than the other; otherwise,
26458 - if the type from the argument template is more cv-qualified
26459 than the type from the parameter template (as described above),
26460 the argument type is considered to be more specialized than the other;
26461 otherwise,
26462 - neither type is more specialized than the other." */
26463
26464 if (deduce1 && deduce2)
26465 {
26466 if (ref1 && ref2 && ref1 != ref2)
26467 {
26468 if (ref1 > ref2)
26469 lose1 = true;
26470 else
26471 lose2 = true;
26472 }
26473 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
26474 {
26475 if ((quals1 & quals2) == quals2)
26476 lose2 = true;
26477 if ((quals1 & quals2) == quals1)
26478 lose1 = true;
26479 }
26480 }
26481
26482 if (lose1 && lose2)
26483 /* We've failed to deduce something in either direction.
26484 These must be unordered. */
26485 break;
26486
26487 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
26488 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
26489 /* We have already processed all of the arguments in our
26490 handing of the pack expansion type. */
26491 len = 0;
26492
26493 args1 = TREE_CHAIN (args1);
26494 args2 = TREE_CHAIN (args2);
26495 }
26496
26497 /* "In most cases, all template parameters must have values in order for
26498 deduction to succeed, but for partial ordering purposes a template
26499 parameter may remain without a value provided it is not used in the
26500 types being used for partial ordering."
26501
26502 Thus, if we are missing any of the targs1 we need to substitute into
26503 origs1, then pat2 is not as specialized as pat1. This can happen when
26504 there is a nondeduced context. */
26505 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
26506 lose2 = true;
26507 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
26508 lose1 = true;
26509
26510 processing_template_decl--;
26511
26512 /* If both deductions succeed, the partial ordering selects the more
26513 constrained template. */
26514 /* P2113: If the corresponding template-parameters of the
26515 template-parameter-lists are not equivalent ([temp.over.link]) or if
26516 the function parameters that positionally correspond between the two
26517 templates are not of the same type, neither template is more
26518 specialized than the other. */
26519 if (!lose1 && !lose2
26520 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
26521 DECL_TEMPLATE_PARMS (pat2))
26522 && compparms (origs1, origs2))
26523 {
26524 int winner = more_constrained (decl1, decl2);
26525 if (winner > 0)
26526 lose2 = true;
26527 else if (winner < 0)
26528 lose1 = true;
26529 }
26530
26531 /* All things being equal, if the next argument is a pack expansion
26532 for one function but not for the other, prefer the
26533 non-variadic function. FIXME this is bogus; see c++/41958. */
26534 if (lose1 == lose2
26535 && args1 && TREE_VALUE (args1)
26536 && args2 && TREE_VALUE (args2))
26537 {
26538 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
26539 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
26540 }
26541
26542 if (lose1 == lose2)
26543 return 0;
26544 else if (!lose1)
26545 return 1;
26546 else
26547 return -1;
26548 }
26549
26550 /* Determine which of two partial specializations of TMPL is more
26551 specialized.
26552
26553 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
26554 to the first partial specialization. The TREE_PURPOSE is the
26555 innermost set of template parameters for the partial
26556 specialization. PAT2 is similar, but for the second template.
26557
26558 Return 1 if the first partial specialization is more specialized;
26559 -1 if the second is more specialized; 0 if neither is more
26560 specialized.
26561
26562 See [temp.class.order] for information about determining which of
26563 two templates is more specialized. */
26564
26565 static int
26566 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
26567 {
26568 tree targs;
26569 int winner = 0;
26570 bool any_deductions = false;
26571
26572 tree tmpl1 = TREE_VALUE (pat1);
26573 tree tmpl2 = TREE_VALUE (pat2);
26574 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
26575 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
26576
26577 /* Just like what happens for functions, if we are ordering between
26578 different template specializations, we may encounter dependent
26579 types in the arguments, and we need our dependency check functions
26580 to behave correctly. */
26581 ++processing_template_decl;
26582 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
26583 if (targs)
26584 {
26585 --winner;
26586 any_deductions = true;
26587 }
26588
26589 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
26590 if (targs)
26591 {
26592 ++winner;
26593 any_deductions = true;
26594 }
26595 --processing_template_decl;
26596
26597 /* If both deductions succeed, the partial ordering selects the more
26598 constrained template. */
26599 if (!winner && any_deductions)
26600 winner = more_constrained (tmpl1, tmpl2);
26601
26602 /* In the case of a tie where at least one of the templates
26603 has a parameter pack at the end, the template with the most
26604 non-packed parameters wins. */
26605 if (winner == 0
26606 && any_deductions
26607 && (template_args_variadic_p (TREE_PURPOSE (pat1))
26608 || template_args_variadic_p (TREE_PURPOSE (pat2))))
26609 {
26610 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
26611 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
26612 int len1 = TREE_VEC_LENGTH (args1);
26613 int len2 = TREE_VEC_LENGTH (args2);
26614
26615 /* We don't count the pack expansion at the end. */
26616 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
26617 --len1;
26618 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
26619 --len2;
26620
26621 if (len1 > len2)
26622 return 1;
26623 else if (len1 < len2)
26624 return -1;
26625 }
26626
26627 return winner;
26628 }
26629
26630 /* Return the template arguments that will produce the function signature
26631 DECL from the function template FN, with the explicit template
26632 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
26633 also match. Return NULL_TREE if no satisfactory arguments could be
26634 found. */
26635
26636 static tree
26637 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
26638 {
26639 int ntparms = DECL_NTPARMS (fn);
26640 tree targs = make_tree_vec (ntparms);
26641 tree decl_type = TREE_TYPE (decl);
26642 tree decl_arg_types;
26643 tree *args;
26644 unsigned int nargs, ix;
26645 tree arg;
26646
26647 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
26648
26649 /* Never do unification on the 'this' parameter. */
26650 decl_arg_types = skip_artificial_parms_for (decl,
26651 TYPE_ARG_TYPES (decl_type));
26652
26653 nargs = list_length (decl_arg_types);
26654 args = XALLOCAVEC (tree, nargs);
26655 for (arg = decl_arg_types, ix = 0;
26656 arg != NULL_TREE;
26657 arg = TREE_CHAIN (arg), ++ix)
26658 args[ix] = TREE_VALUE (arg);
26659
26660 if (fn_type_unification (fn, explicit_args, targs,
26661 args, ix,
26662 (check_rettype || DECL_CONV_FN_P (fn)
26663 ? TREE_TYPE (decl_type) : NULL_TREE),
26664 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
26665 /*explain_p=*/false,
26666 /*decltype*/false)
26667 == error_mark_node)
26668 return NULL_TREE;
26669
26670 return targs;
26671 }
26672
26673 /* Return the innermost template arguments that, when applied to a partial
26674 specialization SPEC_TMPL of TMPL, yield the ARGS.
26675
26676 For example, suppose we have:
26677
26678 template <class T, class U> struct S {};
26679 template <class T> struct S<T*, int> {};
26680
26681 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
26682 partial specialization and the ARGS will be {double*, int}. The resulting
26683 vector will be {double}, indicating that `T' is bound to `double'. */
26684
26685 static tree
26686 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
26687 {
26688 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
26689 tree spec_args
26690 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
26691 int i, ntparms = TREE_VEC_LENGTH (tparms);
26692 tree deduced_args;
26693 tree innermost_deduced_args;
26694
26695 innermost_deduced_args = make_tree_vec (ntparms);
26696 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
26697 {
26698 deduced_args = copy_node (args);
26699 SET_TMPL_ARGS_LEVEL (deduced_args,
26700 TMPL_ARGS_DEPTH (deduced_args),
26701 innermost_deduced_args);
26702 }
26703 else
26704 deduced_args = innermost_deduced_args;
26705
26706 bool tried_array_deduction = (cxx_dialect < cxx17);
26707 again:
26708 if (unify (tparms, deduced_args,
26709 INNERMOST_TEMPLATE_ARGS (spec_args),
26710 INNERMOST_TEMPLATE_ARGS (args),
26711 UNIFY_ALLOW_NONE, /*explain_p=*/false))
26712 return NULL_TREE;
26713
26714 for (i = 0; i < ntparms; ++i)
26715 if (! TREE_VEC_ELT (innermost_deduced_args, i))
26716 {
26717 if (!tried_array_deduction)
26718 {
26719 try_array_deduction (tparms, innermost_deduced_args,
26720 INNERMOST_TEMPLATE_ARGS (spec_args));
26721 tried_array_deduction = true;
26722 if (TREE_VEC_ELT (innermost_deduced_args, i))
26723 goto again;
26724 }
26725 return NULL_TREE;
26726 }
26727
26728 if (!push_tinst_level (spec_tmpl, deduced_args))
26729 {
26730 excessive_deduction_depth = true;
26731 return NULL_TREE;
26732 }
26733
26734 /* Verify that nondeduced template arguments agree with the type
26735 obtained from argument deduction.
26736
26737 For example:
26738
26739 struct A { typedef int X; };
26740 template <class T, class U> struct C {};
26741 template <class T> struct C<T, typename T::X> {};
26742
26743 Then with the instantiation `C<A, int>', we can deduce that
26744 `T' is `A' but unify () does not check whether `typename T::X'
26745 is `int'. */
26746 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
26747
26748 if (spec_args != error_mark_node)
26749 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
26750 INNERMOST_TEMPLATE_ARGS (spec_args),
26751 tmpl, tf_none, false);
26752
26753 pop_tinst_level ();
26754
26755 if (spec_args == error_mark_node
26756 /* We only need to check the innermost arguments; the other
26757 arguments will always agree. */
26758 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
26759 INNERMOST_TEMPLATE_ARGS (args)))
26760 return NULL_TREE;
26761
26762 /* Now that we have bindings for all of the template arguments,
26763 ensure that the arguments deduced for the template template
26764 parameters have compatible template parameter lists. See the use
26765 of template_template_parm_bindings_ok_p in fn_type_unification
26766 for more information. */
26767 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
26768 return NULL_TREE;
26769
26770 return deduced_args;
26771 }
26772
26773 // Compare two function templates T1 and T2 by deducing bindings
26774 // from one against the other. If both deductions succeed, compare
26775 // constraints to see which is more constrained.
26776 static int
26777 more_specialized_inst (tree t1, tree t2)
26778 {
26779 int fate = 0;
26780 int count = 0;
26781
26782 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
26783 {
26784 --fate;
26785 ++count;
26786 }
26787
26788 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
26789 {
26790 ++fate;
26791 ++count;
26792 }
26793
26794 // If both deductions succeed, then one may be more constrained.
26795 if (count == 2 && fate == 0)
26796 fate = more_constrained (t1, t2);
26797
26798 return fate;
26799 }
26800
26801 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
26802 Return the TREE_LIST node with the most specialized template, if
26803 any. If there is no most specialized template, the error_mark_node
26804 is returned.
26805
26806 Note that this function does not look at, or modify, the
26807 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
26808 returned is one of the elements of INSTANTIATIONS, callers may
26809 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
26810 and retrieve it from the value returned. */
26811
26812 tree
26813 most_specialized_instantiation (tree templates)
26814 {
26815 tree fn, champ;
26816
26817 ++processing_template_decl;
26818
26819 champ = templates;
26820 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
26821 {
26822 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
26823 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
26824 if (fate == -1)
26825 champ = fn;
26826 else if (!fate)
26827 {
26828 /* Equally specialized, move to next function. If there
26829 is no next function, nothing's most specialized. */
26830 fn = TREE_CHAIN (fn);
26831 champ = fn;
26832 if (!fn)
26833 break;
26834 }
26835 }
26836
26837 if (champ)
26838 /* Now verify that champ is better than everything earlier in the
26839 instantiation list. */
26840 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
26841 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
26842 {
26843 champ = NULL_TREE;
26844 break;
26845 }
26846 }
26847
26848 processing_template_decl--;
26849
26850 if (!champ)
26851 return error_mark_node;
26852
26853 return champ;
26854 }
26855
26856 /* If DECL is a specialization of some template, return the most
26857 general such template. Otherwise, returns NULL_TREE.
26858
26859 For example, given:
26860
26861 template <class T> struct S { template <class U> void f(U); };
26862
26863 if TMPL is `template <class U> void S<int>::f(U)' this will return
26864 the full template. This function will not trace past partial
26865 specializations, however. For example, given in addition:
26866
26867 template <class T> struct S<T*> { template <class U> void f(U); };
26868
26869 if TMPL is `template <class U> void S<int*>::f(U)' this will return
26870 `template <class T> template <class U> S<T*>::f(U)'. */
26871
26872 tree
26873 most_general_template (const_tree decl)
26874 {
26875 if (TREE_CODE (decl) != TEMPLATE_DECL)
26876 {
26877 if (tree tinfo = get_template_info (decl))
26878 decl = TI_TEMPLATE (tinfo);
26879 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
26880 template friend, or a FIELD_DECL for a capture pack. */
26881 if (TREE_CODE (decl) != TEMPLATE_DECL)
26882 return NULL_TREE;
26883 }
26884
26885 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
26886 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
26887
26888 /* Look for more and more general templates. */
26889 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
26890 {
26891 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
26892 (See cp-tree.h for details.) */
26893 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
26894 break;
26895
26896 if (CLASS_TYPE_P (TREE_TYPE (decl))
26897 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
26898 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
26899 break;
26900
26901 /* Stop if we run into an explicitly specialized class template. */
26902 if (!DECL_NAMESPACE_SCOPE_P (decl)
26903 && DECL_CONTEXT (decl)
26904 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
26905 break;
26906
26907 decl = DECL_TI_TEMPLATE (decl);
26908 }
26909
26910 return CONST_CAST_TREE (decl);
26911 }
26912
26913 /* Return the most specialized of the template partial specializations
26914 which can produce TARGET, a specialization of some class or variable
26915 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
26916 TEMPLATE_DECL node corresponding to the partial specialization, while
26917 the TI_ARGS is the set of template arguments that must be substituted
26918 into the template pattern in order to generate TARGET. The result is
26919 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
26920 RECHECKING is true.
26921
26922 If the choice of partial specialization is ambiguous, a diagnostic
26923 is issued, and the error_mark_node is returned. If there are no
26924 partial specializations matching TARGET, then NULL_TREE is
26925 returned, indicating that the primary template should be used. */
26926
26927 tree
26928 most_specialized_partial_spec (tree target, tsubst_flags_t complain,
26929 bool rechecking /* = false */)
26930 {
26931 tree tinfo = NULL_TREE;
26932 tree tmpl, args, decl;
26933 if (TYPE_P (target))
26934 {
26935 tinfo = CLASSTYPE_TEMPLATE_INFO (target);
26936 tmpl = TI_TEMPLATE (tinfo);
26937 args = TI_ARGS (tinfo);
26938 decl = TYPE_NAME (target);
26939 }
26940 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
26941 {
26942 tmpl = TREE_OPERAND (target, 0);
26943 args = TREE_OPERAND (target, 1);
26944 decl = DECL_TEMPLATE_RESULT (tmpl);
26945 }
26946 else if (VAR_P (target))
26947 {
26948 tinfo = DECL_TEMPLATE_INFO (target);
26949 tmpl = TI_TEMPLATE (tinfo);
26950 args = TI_ARGS (tinfo);
26951 decl = target;
26952 }
26953 else
26954 gcc_unreachable ();
26955
26956 if (!PRIMARY_TEMPLATE_P (tmpl))
26957 return NULL_TREE;
26958
26959 if (!rechecking
26960 && tinfo
26961 && (VAR_P (target) || COMPLETE_TYPE_P (target)))
26962 return TI_PARTIAL_INFO (tinfo);
26963
26964 tree main_tmpl = most_general_template (tmpl);
26965 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
26966 if (!specs)
26967 /* There are no partial specializations of this template. */
26968 return NULL_TREE;
26969
26970 push_access_scope_guard pas (decl);
26971 deferring_access_check_sentinel acs (dk_no_deferred);
26972
26973 /* For determining which partial specialization to use, only the
26974 innermost args are interesting. */
26975 tree outer_args = NULL_TREE;
26976 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
26977 {
26978 outer_args = strip_innermost_template_args (args, 1);
26979 args = INNERMOST_TEMPLATE_ARGS (args);
26980 }
26981
26982 /* The caller hasn't called push_to_top_level yet, but we need
26983 get_partial_spec_bindings to be done in non-template context so that we'll
26984 fully resolve everything. */
26985 processing_template_decl_sentinel ptds;
26986
26987 tree list = NULL_TREE;
26988 for (tree t = specs; t; t = TREE_CHAIN (t))
26989 {
26990 const tree ospec_tmpl = TREE_VALUE (t);
26991
26992 tree spec_tmpl;
26993 if (outer_args)
26994 {
26995 /* Substitute in the template args from the enclosing class. */
26996 ++processing_template_decl;
26997 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
26998 --processing_template_decl;
26999 if (spec_tmpl == error_mark_node)
27000 return error_mark_node;
27001 }
27002 else
27003 spec_tmpl = ospec_tmpl;
27004
27005 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
27006 if (spec_args)
27007 {
27008 if (outer_args)
27009 spec_args = add_to_template_args (outer_args, spec_args);
27010
27011 /* Keep the candidate only if its constraints are satisfied. */
27012 if (constraints_satisfied_p (ospec_tmpl, spec_args))
27013 list = tree_cons (spec_args, ospec_tmpl, list);
27014 }
27015 }
27016
27017 if (! list)
27018 return NULL_TREE;
27019
27020 tree champ = list;
27021 bool ambiguous_p = false;
27022 for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
27023 {
27024 int fate = more_specialized_partial_spec (tmpl, champ, t);
27025 if (fate == 1)
27026 ;
27027 else
27028 {
27029 if (fate == 0)
27030 {
27031 t = TREE_CHAIN (t);
27032 if (! t)
27033 {
27034 ambiguous_p = true;
27035 break;
27036 }
27037 }
27038 champ = t;
27039 }
27040 }
27041
27042 if (!ambiguous_p)
27043 for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
27044 {
27045 int fate = more_specialized_partial_spec (tmpl, champ, t);
27046 if (fate != 1)
27047 {
27048 ambiguous_p = true;
27049 break;
27050 }
27051 }
27052
27053 if (ambiguous_p)
27054 {
27055 const char *str;
27056 char *spaces = NULL;
27057 if (!(complain & tf_error))
27058 return error_mark_node;
27059 auto_diagnostic_group d;
27060 if (TYPE_P (target))
27061 error ("ambiguous template instantiation for %q#T", target);
27062 else
27063 error ("ambiguous template instantiation for %q#D", target);
27064 str = ngettext ("candidate is:", "candidates are:", list_length (list));
27065 for (tree t = list; t; t = TREE_CHAIN (t))
27066 {
27067 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
27068 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
27069 "%s %#qS", spaces ? spaces : str, subst);
27070 spaces = spaces ? spaces : get_spaces (str);
27071 }
27072 free (spaces);
27073 return error_mark_node;
27074 }
27075
27076 tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
27077 if (!rechecking && tinfo)
27078 TI_PARTIAL_INFO (tinfo) = result;
27079 return result;
27080 }
27081
27082 /* Explicitly instantiate DECL. */
27083
27084 void
27085 do_decl_instantiation (tree decl, tree storage)
27086 {
27087 tree result = NULL_TREE;
27088 int extern_p = 0;
27089
27090 if (!decl || decl == error_mark_node)
27091 /* An error occurred, for which grokdeclarator has already issued
27092 an appropriate message. */
27093 return;
27094 else if (! DECL_LANG_SPECIFIC (decl))
27095 {
27096 error ("explicit instantiation of non-template %q#D", decl);
27097 return;
27098 }
27099
27100 bool var_templ = (DECL_TEMPLATE_INFO (decl)
27101 && variable_template_p (DECL_TI_TEMPLATE (decl)));
27102
27103 if (VAR_P (decl) && !var_templ)
27104 {
27105 /* There is an asymmetry here in the way VAR_DECLs and
27106 FUNCTION_DECLs are handled by grokdeclarator. In the case of
27107 the latter, the DECL we get back will be marked as a
27108 template instantiation, and the appropriate
27109 DECL_TEMPLATE_INFO will be set up. This does not happen for
27110 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
27111 should handle VAR_DECLs as it currently handles
27112 FUNCTION_DECLs. */
27113 if (!DECL_CLASS_SCOPE_P (decl))
27114 {
27115 error ("%qD is not a static data member of a class template", decl);
27116 return;
27117 }
27118 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
27119 if (!result || !VAR_P (result))
27120 {
27121 error ("no matching template for %qD found", decl);
27122 return;
27123 }
27124 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
27125 {
27126 error ("type %qT for explicit instantiation %qD does not match "
27127 "declared type %qT", TREE_TYPE (result), decl,
27128 TREE_TYPE (decl));
27129 return;
27130 }
27131 }
27132 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
27133 {
27134 error ("explicit instantiation of %q#D", decl);
27135 return;
27136 }
27137 else
27138 result = decl;
27139
27140 /* Check for various error cases. Note that if the explicit
27141 instantiation is valid the RESULT will currently be marked as an
27142 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
27143 until we get here. */
27144
27145 if (DECL_TEMPLATE_SPECIALIZATION (result))
27146 {
27147 /* DR 259 [temp.spec].
27148
27149 Both an explicit instantiation and a declaration of an explicit
27150 specialization shall not appear in a program unless the explicit
27151 instantiation follows a declaration of the explicit specialization.
27152
27153 For a given set of template parameters, if an explicit
27154 instantiation of a template appears after a declaration of an
27155 explicit specialization for that template, the explicit
27156 instantiation has no effect. */
27157 return;
27158 }
27159 else if (DECL_EXPLICIT_INSTANTIATION (result))
27160 {
27161 /* [temp.spec]
27162
27163 No program shall explicitly instantiate any template more
27164 than once.
27165
27166 We check DECL_NOT_REALLY_EXTERN so as not to complain when
27167 the first instantiation was `extern' and the second is not,
27168 and EXTERN_P for the opposite case. */
27169 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
27170 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
27171 /* If an "extern" explicit instantiation follows an ordinary
27172 explicit instantiation, the template is instantiated. */
27173 if (extern_p)
27174 return;
27175 }
27176 else if (!DECL_IMPLICIT_INSTANTIATION (result))
27177 {
27178 error ("no matching template for %qD found", result);
27179 return;
27180 }
27181 else if (!DECL_TEMPLATE_INFO (result))
27182 {
27183 permerror (input_location, "explicit instantiation of non-template %q#D", result);
27184 return;
27185 }
27186
27187 if (storage == NULL_TREE)
27188 ;
27189 else if (storage == ridpointers[(int) RID_EXTERN])
27190 {
27191 if (cxx_dialect == cxx98 && pedantic)
27192 pedwarn (input_location, OPT_Wc__11_extensions,
27193 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
27194 "instantiations");
27195 extern_p = 1;
27196 }
27197 else
27198 error ("storage class %qD applied to template instantiation", storage);
27199
27200 check_explicit_instantiation_namespace (result);
27201 mark_decl_instantiated (result, extern_p);
27202 if (! extern_p)
27203 instantiate_decl (result, /*defer_ok=*/true,
27204 /*expl_inst_class_mem_p=*/false);
27205 }
27206
27207 static void
27208 mark_class_instantiated (tree t, int extern_p)
27209 {
27210 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
27211 SET_CLASSTYPE_INTERFACE_KNOWN (t);
27212 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
27213 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
27214 if (! extern_p)
27215 {
27216 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
27217 rest_of_type_compilation (t, 1);
27218 }
27219 }
27220
27221 /* Perform an explicit instantiation of template class T. STORAGE, if
27222 non-null, is the RID for extern, inline or static. COMPLAIN is
27223 nonzero if this is called from the parser, zero if called recursively,
27224 since the standard is unclear (as detailed below). */
27225
27226 void
27227 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
27228 {
27229 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
27230 {
27231 if (tree ti = TYPE_TEMPLATE_INFO (t))
27232 error ("explicit instantiation of non-class template %qD",
27233 TI_TEMPLATE (ti));
27234 else
27235 error ("explicit instantiation of non-template type %qT", t);
27236 return;
27237 }
27238
27239 complete_type (t);
27240
27241 if (!COMPLETE_TYPE_P (t))
27242 {
27243 if (complain & tf_error)
27244 error ("explicit instantiation of %q#T before definition of template",
27245 t);
27246 return;
27247 }
27248
27249 /* At most one of these will be true. */
27250 bool extern_p = false;
27251 bool nomem_p = false;
27252 bool static_p = false;
27253
27254 if (storage != NULL_TREE)
27255 {
27256 if (storage == ridpointers[(int) RID_EXTERN])
27257 {
27258 if (cxx_dialect == cxx98 && pedantic)
27259 pedwarn (input_location, OPT_Wc__11_extensions,
27260 "ISO C++ 1998 forbids the use of %<extern%> on "
27261 "explicit instantiations");
27262 }
27263 else
27264 pedwarn (input_location, OPT_Wpedantic,
27265 "ISO C++ forbids the use of %qE"
27266 " on explicit instantiations", storage);
27267
27268 if (storage == ridpointers[(int) RID_INLINE])
27269 nomem_p = true;
27270 else if (storage == ridpointers[(int) RID_EXTERN])
27271 extern_p = true;
27272 else if (storage == ridpointers[(int) RID_STATIC])
27273 static_p = true;
27274 else
27275 error ("storage class %qD applied to template instantiation",
27276 storage);
27277 }
27278
27279 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
27280 /* DR 259 [temp.spec].
27281
27282 Both an explicit instantiation and a declaration of an explicit
27283 specialization shall not appear in a program unless the
27284 explicit instantiation follows a declaration of the explicit
27285 specialization.
27286
27287 For a given set of template parameters, if an explicit
27288 instantiation of a template appears after a declaration of an
27289 explicit specialization for that template, the explicit
27290 instantiation has no effect. */
27291 return;
27292
27293 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
27294 {
27295 /* We've already instantiated the template. */
27296
27297 /* [temp.spec]
27298
27299 No program shall explicitly instantiate any template more
27300 than once.
27301
27302 If EXTERN_P then this is ok. */
27303 if (!extern_p && (complain & tf_error))
27304 permerror (input_location,
27305 "duplicate explicit instantiation of %q#T", t);
27306
27307 return;
27308 }
27309
27310 check_explicit_instantiation_namespace (TYPE_NAME (t));
27311 mark_class_instantiated (t, extern_p);
27312
27313 if (nomem_p)
27314 return;
27315
27316 /* In contrast to implicit instantiation, where only the
27317 declarations, and not the definitions, of members are
27318 instantiated, we have here:
27319
27320 [temp.explicit]
27321
27322 An explicit instantiation that names a class template
27323 specialization is also an explicit instantiation of the same
27324 kind (declaration or definition) of each of its members (not
27325 including members inherited from base classes and members
27326 that are templates) that has not been previously explicitly
27327 specialized in the translation unit containing the explicit
27328 instantiation, provided that the associated constraints, if
27329 any, of that member are satisfied by the template arguments
27330 of the explicit instantiation. */
27331 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
27332 if ((VAR_P (fld)
27333 || (TREE_CODE (fld) == FUNCTION_DECL
27334 && !static_p
27335 && user_provided_p (fld)))
27336 && DECL_TEMPLATE_INSTANTIATION (fld)
27337 && constraints_satisfied_p (fld))
27338 {
27339 mark_decl_instantiated (fld, extern_p);
27340 if (! extern_p)
27341 instantiate_decl (fld, /*defer_ok=*/true,
27342 /*expl_inst_class_mem_p=*/true);
27343 }
27344 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
27345 {
27346 tree type = TREE_TYPE (fld);
27347
27348 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
27349 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
27350 do_type_instantiation (type, storage, 0);
27351 }
27352 }
27353
27354 /* Given a function DECL, which is a specialization of TMPL, modify
27355 DECL to be a re-instantiation of TMPL with the same template
27356 arguments. TMPL should be the template into which tsubst'ing
27357 should occur for DECL, not the most general template.
27358
27359 One reason for doing this is a scenario like this:
27360
27361 template <class T>
27362 void f(const T&, int i);
27363
27364 void g() { f(3, 7); }
27365
27366 template <class T>
27367 void f(const T& t, const int i) { }
27368
27369 Note that when the template is first instantiated, with
27370 instantiate_template, the resulting DECL will have no name for the
27371 first parameter, and the wrong type for the second. So, when we go
27372 to instantiate the DECL, we regenerate it. */
27373
27374 static void
27375 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
27376 {
27377 /* The arguments used to instantiate DECL, from the most general
27378 template. */
27379 tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
27380
27381 /* Make sure that we can see identifiers, and compute access correctly. */
27382 push_access_scope (decl);
27383
27384 if (TREE_CODE (decl) == FUNCTION_DECL)
27385 {
27386 tree specs;
27387 int args_depth;
27388 int parms_depth;
27389
27390 /* Don't bother with this for unique friends that can't be redeclared and
27391 might change type if regenerated (PR69836). */
27392 if (DECL_UNIQUE_FRIEND_P (decl))
27393 goto done;
27394
27395 /* A template with a lambda in the signature also changes type if
27396 regenerated (PR119401). */
27397 walk_tree_fn find_lambda
27398 = [](tree *tp, int *, void *)
27399 {
27400 if (TREE_CODE (*tp) == LAMBDA_EXPR)
27401 return *tp;
27402 return NULL_TREE;
27403 };
27404 if (cp_walk_tree_without_duplicates
27405 (&TREE_TYPE (tmpl), find_lambda, nullptr))
27406 goto done;
27407
27408 /* Use the source location of the definition. */
27409 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
27410
27411 args_depth = TMPL_ARGS_DEPTH (args);
27412 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
27413 if (args_depth > parms_depth)
27414 args = get_innermost_template_args (args, parms_depth);
27415
27416 /* Instantiate a dynamic exception-specification. noexcept will be
27417 handled below. */
27418 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
27419 if (TREE_VALUE (raises))
27420 {
27421 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
27422 args, tf_error, NULL_TREE,
27423 /*defer_ok*/false);
27424 if (specs && specs != error_mark_node)
27425 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
27426 specs);
27427 }
27428
27429 /* Merge parameter declarations. */
27430 if (tree pattern_parm
27431 = skip_artificial_parms_for (code_pattern,
27432 DECL_ARGUMENTS (code_pattern)))
27433 {
27434 tree *p = &DECL_ARGUMENTS (decl);
27435 for (int skip = num_artificial_parms_for (decl); skip; --skip)
27436 p = &DECL_CHAIN (*p);
27437 *p = tsubst_decl (pattern_parm, args, tf_error);
27438 for (tree t = *p; t; t = DECL_CHAIN (t))
27439 DECL_CONTEXT (t) = decl;
27440 }
27441
27442 if (DECL_CONTRACTS (decl))
27443 {
27444 /* If we're regenerating a specialization, the contracts will have
27445 been copied from the most general template. Replace those with
27446 the ones from the actual specialization. */
27447 tree tmpl = DECL_TI_TEMPLATE (decl);
27448 if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
27449 {
27450 remove_contract_attributes (decl);
27451 copy_contract_attributes (decl, code_pattern);
27452 }
27453
27454 tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
27455 }
27456
27457 /* Merge additional specifiers from the CODE_PATTERN. */
27458 if (DECL_DECLARED_INLINE_P (code_pattern)
27459 && !DECL_DECLARED_INLINE_P (decl))
27460 DECL_DECLARED_INLINE_P (decl) = 1;
27461
27462 maybe_instantiate_noexcept (decl, tf_error);
27463 }
27464 else if (VAR_P (decl))
27465 {
27466 start_lambda_scope (decl);
27467 DECL_INITIAL (decl) =
27468 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
27469 tf_error, DECL_TI_TEMPLATE (decl));
27470 finish_lambda_scope ();
27471 if (VAR_HAD_UNKNOWN_BOUND (decl))
27472 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
27473 tf_error, DECL_TI_TEMPLATE (decl));
27474 }
27475 else
27476 gcc_unreachable ();
27477
27478 done:
27479 pop_access_scope (decl);
27480 }
27481
27482 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
27483 substituted to get DECL. */
27484
27485 tree
27486 template_for_substitution (tree decl)
27487 {
27488 tree tmpl = DECL_TI_TEMPLATE (decl);
27489
27490 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
27491 for the instantiation. This is not always the most general
27492 template. Consider, for example:
27493
27494 template <class T>
27495 struct S { template <class U> void f();
27496 template <> void f<int>(); };
27497
27498 and an instantiation of S<double>::f<int>. We want TD to be the
27499 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
27500 while (/* An instantiation cannot have a definition, so we need a
27501 more general template. */
27502 DECL_TEMPLATE_INSTANTIATION (tmpl)
27503 /* We must also deal with friend templates. Given:
27504
27505 template <class T> struct S {
27506 template <class U> friend void f() {};
27507 };
27508
27509 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
27510 so far as the language is concerned, but that's still
27511 where we get the pattern for the instantiation from. On
27512 other hand, if the definition comes outside the class, say:
27513
27514 template <class T> struct S {
27515 template <class U> friend void f();
27516 };
27517 template <class U> friend void f() {}
27518
27519 we don't need to look any further. That's what the check for
27520 DECL_INITIAL is for. */
27521 || (TREE_CODE (decl) == FUNCTION_DECL
27522 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
27523 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
27524 {
27525 /* The present template, TD, should not be a definition. If it
27526 were a definition, we should be using it! Note that we
27527 cannot restructure the loop to just keep going until we find
27528 a template with a definition, since that might go too far if
27529 a specialization was declared, but not defined. */
27530
27531 /* Fetch the more general template. */
27532 tmpl = DECL_TI_TEMPLATE (tmpl);
27533 }
27534
27535 return tmpl;
27536 }
27537
27538 /* Returns true if we need to instantiate this template instance even if we
27539 know we aren't going to emit it. */
27540
27541 bool
27542 always_instantiate_p (tree decl)
27543 {
27544 /* We always instantiate inline functions so that we can inline them. An
27545 explicit instantiation declaration prohibits implicit instantiation of
27546 non-inline functions. With high levels of optimization, we would
27547 normally inline non-inline functions -- but we're not allowed to do
27548 that for "extern template" functions. Therefore, we check
27549 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
27550 return ((TREE_CODE (decl) == FUNCTION_DECL
27551 && (DECL_DECLARED_INLINE_P (decl)
27552 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
27553 /* And we need to instantiate static data members so that
27554 their initializers are available in integral constant
27555 expressions. */
27556 || (VAR_P (decl)
27557 && decl_maybe_constant_var_p (decl)));
27558 }
27559
27560 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
27561 instantiate it now, modifying TREE_TYPE (fn). Returns false on
27562 error, true otherwise. */
27563
27564 bool
27565 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
27566 {
27567 if (fn == error_mark_node)
27568 return false;
27569
27570 /* Don't instantiate a noexcept-specification from template context. */
27571 if (processing_template_decl
27572 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
27573 return true;
27574
27575 tree fntype = TREE_TYPE (fn);
27576 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
27577
27578 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
27579 && DECL_MAYBE_DELETED (fn))
27580 {
27581 if (fn == current_function_decl)
27582 /* We're in start_preparsed_function, keep going. */
27583 return true;
27584
27585 ++function_depth;
27586 maybe_synthesize_method (fn);
27587 --function_depth;
27588 return !DECL_DELETED_FN (fn);
27589 }
27590
27591 if (!spec || !TREE_PURPOSE (spec))
27592 return true;
27593
27594 tree noex = TREE_PURPOSE (spec);
27595 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
27596 && TREE_CODE (noex) != DEFERRED_PARSE)
27597 return true;
27598
27599 tree orig_fn = NULL_TREE;
27600 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
27601 its FUNCTION_DECL for the rest of this function -- push_access_scope
27602 doesn't accept TEMPLATE_DECLs. */
27603 if (DECL_FUNCTION_TEMPLATE_P (fn))
27604 {
27605 orig_fn = fn;
27606 fn = DECL_TEMPLATE_RESULT (fn);
27607 }
27608
27609 if (DECL_CLONED_FUNCTION_P (fn))
27610 {
27611 tree prime = DECL_CLONED_FUNCTION (fn);
27612 if (!maybe_instantiate_noexcept (prime, complain))
27613 return false;
27614 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
27615 }
27616 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
27617 {
27618 static hash_set<tree>* fns = new hash_set<tree>;
27619 bool added = false;
27620 tree pattern = DEFERRED_NOEXCEPT_PATTERN (noex);
27621 if (pattern == NULL_TREE)
27622 {
27623 spec = get_defaulted_eh_spec (fn, complain);
27624 if (spec == error_mark_node)
27625 /* This might have failed because of an unparsed DMI, so
27626 let's try again later. */
27627 return false;
27628 }
27629 else if (!(added = !fns->add (fn)))
27630 {
27631 /* If hash_set::add returns true, the element was already there. */
27632 location_t loc = cp_expr_loc_or_loc (pattern,
27633 DECL_SOURCE_LOCATION (fn));
27634 error_at (loc,
27635 "exception specification of %qD depends on itself",
27636 fn);
27637 spec = noexcept_false_spec;
27638 }
27639 else if (TREE_CODE (pattern) == DEFERRED_PARSE)
27640 {
27641 error ("exception specification of %qD is not available "
27642 "until end of class definition", fn);
27643 spec = noexcept_false_spec;
27644 }
27645 else if (push_tinst_level (fn))
27646 {
27647 const bool push_to_top = maybe_push_to_top_level (fn);
27648 push_access_scope (fn);
27649 push_deferring_access_checks (dk_no_deferred);
27650 input_location = DECL_SOURCE_LOCATION (fn);
27651
27652 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
27653 && !DECL_LOCAL_DECL_P (fn))
27654 {
27655 /* If needed, set current_class_ptr for the benefit of
27656 tsubst_copy/PARM_DECL. */
27657 tree this_parm = DECL_ARGUMENTS (fn);
27658 current_class_ptr = NULL_TREE;
27659 current_class_ref = cp_build_fold_indirect_ref (this_parm);
27660 current_class_ptr = this_parm;
27661 }
27662
27663 /* If this function is represented by a TEMPLATE_DECL, then
27664 the deferred noexcept-specification might still contain
27665 dependent types, even after substitution. And we need the
27666 dependency check functions to work in build_noexcept_spec. */
27667 if (orig_fn)
27668 ++processing_template_decl;
27669
27670 /* Do deferred instantiation of the noexcept-specifier. */
27671 noex = tsubst_expr (pattern, DEFERRED_NOEXCEPT_ARGS (noex),
27672 tf_warning_or_error, fn);
27673 /* Build up the noexcept-specification. */
27674 spec = build_noexcept_spec (noex, tf_warning_or_error);
27675
27676 if (orig_fn)
27677 --processing_template_decl;
27678
27679 pop_deferring_access_checks ();
27680 pop_access_scope (fn);
27681 pop_tinst_level ();
27682 maybe_pop_from_top_level (push_to_top);
27683 }
27684 else
27685 spec = noexcept_false_spec;
27686
27687 if (added)
27688 fns->remove (fn);
27689 }
27690
27691 if (spec == error_mark_node)
27692 {
27693 /* This failed with a hard error, so let's go with false. */
27694 gcc_assert (seen_error ());
27695 spec = noexcept_false_spec;
27696 }
27697
27698 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
27699 if (orig_fn)
27700 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
27701
27702 return true;
27703 }
27704
27705 /* We're starting to process the function INST, an instantiation of PATTERN;
27706 add their parameters to local_specializations. */
27707
27708 void
27709 register_parameter_specializations (tree pattern, tree inst)
27710 {
27711 tree tmpl_parm = DECL_ARGUMENTS (pattern);
27712 tree spec_parm = DECL_ARGUMENTS (inst);
27713 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
27714 {
27715 register_local_specialization (spec_parm, tmpl_parm);
27716 spec_parm = skip_artificial_parms_for (inst, spec_parm);
27717 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
27718 }
27719 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
27720 {
27721 if (!DECL_PACK_P (tmpl_parm))
27722 {
27723 register_local_specialization (spec_parm, tmpl_parm);
27724 spec_parm = DECL_CHAIN (spec_parm);
27725 }
27726 else
27727 {
27728 /* Register the (value) argument pack as a specialization of
27729 TMPL_PARM, then move on. */
27730 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
27731 register_local_specialization (argpack, tmpl_parm);
27732 }
27733 }
27734 gcc_assert (!spec_parm);
27735 }
27736
27737 /* Instantiate the body of D using PATTERN with ARGS. We have
27738 already determined PATTERN is the correct template to use.
27739 NESTED_P is true if this is a nested function, in which case
27740 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
27741
27742 static void
27743 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
27744 {
27745 tree td = NULL_TREE;
27746 tree code_pattern = pattern;
27747
27748 if (!nested_p)
27749 {
27750 td = pattern;
27751 code_pattern = DECL_TEMPLATE_RESULT (td);
27752 }
27753 else
27754 /* Only OMP reductions are nested. */
27755 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
27756
27757 vec<tree> omp_privatization_save;
27758 if (current_function_decl)
27759 save_omp_privatization_clauses (omp_privatization_save);
27760
27761 bool push_to_top = maybe_push_to_top_level (d);
27762
27763 mark_template_arguments_used (pattern, args);
27764
27765 if (VAR_P (d))
27766 {
27767 /* The variable might be a lambda's extra scope, and that
27768 lambda's visibility depends on D's. */
27769 maybe_commonize_var (d);
27770 determine_visibility (d);
27771 }
27772
27773 /* Mark D as instantiated so that recursive calls to
27774 instantiate_decl do not try to instantiate it again. */
27775 DECL_TEMPLATE_INSTANTIATED (d) = 1;
27776
27777 if (td)
27778 /* Regenerate the declaration in case the template has been modified
27779 by a subsequent redeclaration. */
27780 regenerate_decl_from_template (d, td, args);
27781
27782 /* We already set the file and line above. Reset them now in case
27783 they changed as a result of calling regenerate_decl_from_template. */
27784 input_location = DECL_SOURCE_LOCATION (d);
27785
27786 if (VAR_P (d))
27787 {
27788 /* Clear out DECL_RTL; whatever was there before may not be right
27789 since we've reset the type of the declaration. */
27790 SET_DECL_RTL (d, NULL);
27791 DECL_IN_AGGR_P (d) = 0;
27792
27793 /* The initializer is placed in DECL_INITIAL by
27794 regenerate_decl_from_template so we don't need to
27795 push/pop_access_scope again here. Pull it out so that
27796 cp_finish_decl can process it. */
27797 bool const_init = false;
27798 tree init = DECL_INITIAL (d);
27799 DECL_INITIAL (d) = NULL_TREE;
27800 DECL_INITIALIZED_P (d) = 0;
27801
27802 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
27803 initializer. That function will defer actual emission until
27804 we have a chance to determine linkage. */
27805 DECL_EXTERNAL (d) = 0;
27806
27807 /* Enter the scope of D so that access-checking works correctly. */
27808 bool enter_context = DECL_CLASS_SCOPE_P (d);
27809 if (enter_context)
27810 push_nested_class (DECL_CONTEXT (d));
27811
27812 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
27813 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
27814
27815 if (enter_context)
27816 pop_nested_class ();
27817 }
27818 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
27819 synthesize_method (d);
27820 else if (TREE_CODE (d) == FUNCTION_DECL)
27821 {
27822 /* Set up the list of local specializations. */
27823 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
27824 tree block = NULL_TREE;
27825
27826 /* Set up context. */
27827 if (nested_p)
27828 block = push_stmt_list ();
27829 else
27830 {
27831 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
27832
27833 perform_instantiation_time_access_checks (code_pattern, args);
27834 }
27835
27836 /* Create substitution entries for the parameters. */
27837 register_parameter_specializations (code_pattern, d);
27838
27839 /* Substitute into the body of the function. */
27840 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27841 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
27842 tf_warning_or_error, d);
27843 else
27844 {
27845 tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
27846 tf_warning_or_error, DECL_TI_TEMPLATE (d));
27847
27848 /* Set the current input_location to the end of the function
27849 so that finish_function knows where we are. */
27850 input_location
27851 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
27852
27853 /* Remember if we saw an infinite loop in the template. */
27854 current_function_infinite_loop
27855 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
27856 }
27857
27858 /* Finish the function. */
27859 if (nested_p)
27860 DECL_SAVED_TREE (d) = pop_stmt_list (block);
27861 else
27862 {
27863 d = finish_function (/*inline_p=*/false);
27864 expand_or_defer_fn (d);
27865 }
27866
27867 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27868 cp_check_omp_declare_reduction (d);
27869
27870 if (int errs = errorcount + sorrycount)
27871 if (errs > current_tinst_level->errors)
27872 if (function *f = DECL_STRUCT_FUNCTION (d))
27873 f->language->erroneous = true;
27874 }
27875
27876 /* We're not deferring instantiation any more. */
27877 if (!nested_p)
27878 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
27879
27880 maybe_pop_from_top_level (push_to_top);
27881
27882 if (current_function_decl)
27883 restore_omp_privatization_clauses (omp_privatization_save);
27884 }
27885
27886 /* Produce the definition of D, a _DECL generated from a template. If
27887 DEFER_OK is true, then we don't have to actually do the
27888 instantiation now; we just have to do it sometime. Normally it is
27889 an error if this is an explicit instantiation but D is undefined.
27890 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
27891 instantiated class template. */
27892
27893 tree
27894 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
27895 {
27896 tree tmpl = DECL_TI_TEMPLATE (d);
27897 tree gen_args;
27898 tree args;
27899 tree td;
27900 tree code_pattern;
27901 tree spec;
27902 tree gen_tmpl;
27903 bool pattern_defined;
27904 location_t saved_loc = input_location;
27905 bool external_p;
27906 bool deleted_p;
27907
27908 /* This function should only be used to instantiate templates for
27909 functions and static member variables. */
27910 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
27911
27912 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
27913
27914 if (modules_p ())
27915 /* We may have a pending instantiation of D itself. */
27916 lazy_load_pendings (d);
27917
27918 /* Variables are never deferred; if instantiation is required, they
27919 are instantiated right away. That allows for better code in the
27920 case that an expression refers to the value of the variable --
27921 if the variable has a constant value the referring expression can
27922 take advantage of that fact. */
27923 if (VAR_P (d))
27924 defer_ok = false;
27925
27926 /* Don't instantiate cloned functions. Instead, instantiate the
27927 functions they cloned. */
27928 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
27929 d = DECL_CLONED_FUNCTION (d);
27930
27931 if (DECL_TEMPLATE_INSTANTIATED (d)
27932 || TREE_TYPE (d) == error_mark_node
27933 || (TREE_CODE (d) == FUNCTION_DECL
27934 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
27935 || DECL_TEMPLATE_SPECIALIZATION (d))
27936 /* D has already been instantiated or explicitly specialized, so
27937 there's nothing for us to do here.
27938
27939 It might seem reasonable to check whether or not D is an explicit
27940 instantiation, and, if so, stop here. But when an explicit
27941 instantiation is deferred until the end of the compilation,
27942 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
27943 the instantiation. */
27944 return d;
27945
27946 /* Check to see whether we know that this template will be
27947 instantiated in some other file, as with "extern template"
27948 extension. */
27949 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
27950
27951 /* In general, we do not instantiate such templates. */
27952 if (external_p && !always_instantiate_p (d))
27953 return d;
27954
27955 gen_tmpl = most_general_template (tmpl);
27956 gen_args = DECL_TI_ARGS (d);
27957
27958 /* We should already have the extra args. */
27959 gcc_checking_assert (tmpl == gen_tmpl
27960 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
27961 == TMPL_ARGS_DEPTH (gen_args)));
27962 /* And what's in the hash table should match D. */
27963 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
27964 == d
27965 || spec == NULL_TREE);
27966
27967 /* This needs to happen before any tsubsting. */
27968 if (! push_tinst_level (d))
27969 return d;
27970
27971 auto_timevar tv (TV_TEMPLATE_INST);
27972
27973 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
27974 for the instantiation. */
27975 td = template_for_substitution (d);
27976 args = gen_args;
27977
27978 if (variable_template_specialization_p (d))
27979 {
27980 /* Look up an explicit specialization, if any. */
27981 tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
27982 if (partial_ti && partial_ti != error_mark_node)
27983 {
27984 td = TI_TEMPLATE (partial_ti);
27985 args = TI_ARGS (partial_ti);
27986 }
27987 }
27988
27989 maybe_diagnose_erroneous_template (td);
27990
27991 code_pattern = DECL_TEMPLATE_RESULT (td);
27992
27993 /* We should never be trying to instantiate a member of a class
27994 template or partial specialization. */
27995 gcc_assert (d != code_pattern);
27996
27997 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
27998 || DECL_TEMPLATE_SPECIALIZATION (td))
27999 /* In the case of a friend template whose definition is provided
28000 outside the class, we may have too many arguments. Drop the
28001 ones we don't need. The same is true for specializations. */
28002 args = get_innermost_template_args
28003 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
28004
28005 if (TREE_CODE (d) == FUNCTION_DECL)
28006 {
28007 deleted_p = DECL_DELETED_FN (code_pattern);
28008 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
28009 && DECL_INITIAL (code_pattern) != error_mark_node)
28010 || DECL_DEFAULTED_FN (code_pattern)
28011 || deleted_p);
28012 }
28013 else
28014 {
28015 deleted_p = false;
28016 if (DECL_CLASS_SCOPE_P (code_pattern))
28017 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
28018 else
28019 pattern_defined = ! DECL_EXTERNAL (code_pattern);
28020 }
28021
28022 /* We may be in the middle of deferred access check. Disable it now. */
28023 push_deferring_access_checks (dk_no_deferred);
28024
28025 /* Unless an explicit instantiation directive has already determined
28026 the linkage of D, remember that a definition is available for
28027 this entity. */
28028 if (pattern_defined
28029 && !DECL_INTERFACE_KNOWN (d)
28030 && !DECL_NOT_REALLY_EXTERN (d))
28031 mark_definable (d);
28032
28033 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
28034 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
28035 input_location = DECL_SOURCE_LOCATION (d);
28036
28037 /* If D is a member of an explicitly instantiated class template,
28038 and no definition is available, treat it like an implicit
28039 instantiation. */
28040 if (!pattern_defined && expl_inst_class_mem_p
28041 && DECL_EXPLICIT_INSTANTIATION (d))
28042 {
28043 /* Leave linkage flags alone on instantiations with anonymous
28044 visibility. */
28045 if (TREE_PUBLIC (d))
28046 {
28047 DECL_NOT_REALLY_EXTERN (d) = 0;
28048 DECL_INTERFACE_KNOWN (d) = 0;
28049 }
28050 SET_DECL_IMPLICIT_INSTANTIATION (d);
28051 }
28052
28053 /* Defer all other templates, unless we have been explicitly
28054 forbidden from doing so. */
28055 if (/* If there is no definition, we cannot instantiate the
28056 template. */
28057 ! pattern_defined
28058 /* If it's OK to postpone instantiation, do so. */
28059 || defer_ok
28060 /* If this is a static data member that will be defined
28061 elsewhere, we don't want to instantiate the entire data
28062 member, but we do want to instantiate the initializer so that
28063 we can substitute that elsewhere. */
28064 || (external_p && VAR_P (d))
28065 /* Handle here a deleted function too, avoid generating
28066 its body (c++/61080). */
28067 || deleted_p)
28068 {
28069 /* The definition of the static data member is now required so
28070 we must substitute the initializer. */
28071 if (VAR_P (d)
28072 && !DECL_INITIAL (d)
28073 && DECL_INITIAL (code_pattern))
28074 {
28075 tree ns;
28076 tree init;
28077 bool const_init = false;
28078 bool enter_context = DECL_CLASS_SCOPE_P (d);
28079
28080 ns = decl_namespace_context (d);
28081 push_nested_namespace (ns);
28082 if (enter_context)
28083 push_nested_class (DECL_CONTEXT (d));
28084 init = tsubst_expr (DECL_INITIAL (code_pattern),
28085 args,
28086 tf_warning_or_error, NULL_TREE);
28087 /* If instantiating the initializer involved instantiating this
28088 again, don't call cp_finish_decl twice. */
28089 if (!DECL_INITIAL (d))
28090 {
28091 /* Make sure the initializer is still constant, in case of
28092 circular dependency (template/instantiate6.C). */
28093 const_init
28094 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
28095 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
28096 /*asmspec_tree=*/NULL_TREE, 0);
28097 }
28098 if (enter_context)
28099 pop_nested_class ();
28100 pop_nested_namespace (ns);
28101 }
28102
28103 /* We restore the source position here because it's used by
28104 add_pending_template. */
28105 input_location = saved_loc;
28106
28107 if (at_eof && !pattern_defined
28108 && DECL_EXPLICIT_INSTANTIATION (d)
28109 && DECL_NOT_REALLY_EXTERN (d))
28110 /* [temp.explicit]
28111
28112 The definition of a non-exported function template, a
28113 non-exported member function template, or a non-exported
28114 member function or static data member of a class template
28115 shall be present in every translation unit in which it is
28116 explicitly instantiated. */
28117 permerror (input_location, "explicit instantiation of %qD "
28118 "but no definition available", d);
28119
28120 /* If we're in unevaluated context, we just wanted to get the
28121 constant value; this isn't an odr use, so don't queue
28122 a full instantiation. */
28123 if (!cp_unevaluated_operand
28124 /* ??? Historically, we have instantiated inline functions, even
28125 when marked as "extern template". */
28126 && !(external_p && VAR_P (d)))
28127 add_pending_template (d);
28128 }
28129 else
28130 {
28131 set_instantiating_module (d);
28132 if (variable_template_p (gen_tmpl))
28133 note_vague_linkage_variable (d);
28134 instantiate_body (td, args, d, false);
28135 }
28136
28137 pop_deferring_access_checks ();
28138 pop_tinst_level ();
28139 input_location = saved_loc;
28140
28141 return d;
28142 }
28143
28144 /* Run through the list of templates that we wish we could
28145 instantiate, and instantiate any we can. RETRIES is the
28146 number of times we retry pending template instantiation. */
28147
28148 void
28149 instantiate_pending_templates (int retries)
28150 {
28151 int reconsider;
28152 location_t saved_loc = input_location;
28153 unsigned saved_module_kind = module_kind;
28154
28155 /* Instantiating templates may trigger vtable generation. This in turn
28156 may require further template instantiations. We place a limit here
28157 to avoid infinite loop. */
28158 if (pending_templates && retries >= max_tinst_depth)
28159 {
28160 tree decl = pending_templates->tinst->maybe_get_node ();
28161
28162 fatal_error (input_location,
28163 "template instantiation depth exceeds maximum of %d"
28164 " instantiating %q+D, possibly from virtual table generation"
28165 " (use %<-ftemplate-depth=%> to increase the maximum)",
28166 max_tinst_depth, decl);
28167 if (TREE_CODE (decl) == FUNCTION_DECL)
28168 /* Pretend that we defined it. */
28169 DECL_INITIAL (decl) = error_mark_node;
28170 return;
28171 }
28172
28173 do
28174 {
28175 struct pending_template **t = &pending_templates;
28176 struct pending_template *last = NULL;
28177 reconsider = 0;
28178 while (*t)
28179 {
28180 tree instantiation = reopen_tinst_level ((*t)->tinst);
28181 bool complete = false;
28182
28183 if (TYPE_P (instantiation))
28184 {
28185 if (!COMPLETE_TYPE_P (instantiation))
28186 {
28187 instantiate_class_template (instantiation);
28188 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
28189 for (tree fld = TYPE_FIELDS (instantiation);
28190 fld; fld = TREE_CHAIN (fld))
28191 if ((VAR_P (fld)
28192 || (TREE_CODE (fld) == FUNCTION_DECL
28193 && !DECL_ARTIFICIAL (fld)))
28194 && DECL_TEMPLATE_INSTANTIATION (fld))
28195 instantiate_decl (fld,
28196 /*defer_ok=*/false,
28197 /*expl_inst_class_mem_p=*/false);
28198
28199 if (COMPLETE_TYPE_P (instantiation))
28200 reconsider = 1;
28201 }
28202
28203 complete = COMPLETE_TYPE_P (instantiation);
28204 }
28205 else
28206 {
28207 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
28208 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
28209 {
28210 instantiation
28211 = instantiate_decl (instantiation,
28212 /*defer_ok=*/false,
28213 /*expl_inst_class_mem_p=*/false);
28214 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
28215 reconsider = 1;
28216 }
28217
28218 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
28219 || DECL_TEMPLATE_INSTANTIATED (instantiation));
28220 }
28221
28222 if (complete)
28223 {
28224 /* If INSTANTIATION has been instantiated, then we don't
28225 need to consider it again in the future. */
28226 struct pending_template *drop = *t;
28227 *t = (*t)->next;
28228 set_refcount_ptr (drop->tinst);
28229 pending_template_freelist ().free (drop);
28230 }
28231 else
28232 {
28233 last = *t;
28234 t = &(*t)->next;
28235 }
28236 tinst_depth = 0;
28237 set_refcount_ptr (current_tinst_level);
28238 }
28239 last_pending_template = last;
28240 }
28241 while (reconsider);
28242
28243 input_location = saved_loc;
28244 module_kind = saved_module_kind;
28245 }
28246
28247 /* Substitute ARGVEC into T, which is a list of initializers for
28248 either base class or a non-static data member. The TREE_PURPOSEs
28249 are DECLs, and the TREE_VALUEs are the initializer values. Used by
28250 instantiate_decl. */
28251
28252 static tree
28253 tsubst_initializer_list (tree t, tree argvec)
28254 {
28255 tree inits = NULL_TREE;
28256 tree target_ctor = error_mark_node;
28257
28258 for (; t; t = TREE_CHAIN (t))
28259 {
28260 tree decl;
28261 tree init;
28262 tree expanded_bases = NULL_TREE;
28263 tree expanded_arguments = NULL_TREE;
28264 int i, len = 1;
28265
28266 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
28267 {
28268 tree expr;
28269 tree arg;
28270
28271 /* Expand the base class expansion type into separate base
28272 classes. */
28273 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
28274 tf_warning_or_error,
28275 NULL_TREE);
28276 if (expanded_bases == error_mark_node)
28277 continue;
28278
28279 /* We'll be building separate TREE_LISTs of arguments for
28280 each base. */
28281 len = TREE_VEC_LENGTH (expanded_bases);
28282 expanded_arguments = make_tree_vec (len);
28283 for (i = 0; i < len; i++)
28284 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
28285
28286 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
28287 expand each argument in the TREE_VALUE of t. */
28288 expr = make_node (EXPR_PACK_EXPANSION);
28289 PACK_EXPANSION_LOCAL_P (expr) = true;
28290 PACK_EXPANSION_PARAMETER_PACKS (expr) =
28291 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
28292
28293 if (TREE_VALUE (t) == void_type_node)
28294 /* VOID_TYPE_NODE is used to indicate
28295 value-initialization. */
28296 {
28297 for (i = 0; i < len; i++)
28298 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
28299 }
28300 else
28301 {
28302 /* Substitute parameter packs into each argument in the
28303 TREE_LIST. */
28304 in_base_initializer = 1;
28305 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
28306 {
28307 tree expanded_exprs;
28308
28309 /* Expand the argument. */
28310 tree value;
28311 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
28312 value = TREE_VALUE (arg);
28313 else
28314 {
28315 value = expr;
28316 PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
28317 }
28318 expanded_exprs
28319 = tsubst_pack_expansion (value, argvec,
28320 tf_warning_or_error,
28321 NULL_TREE);
28322 if (expanded_exprs == error_mark_node)
28323 continue;
28324
28325 /* Prepend each of the expanded expressions to the
28326 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
28327 for (i = 0; i < len; i++)
28328 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
28329 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
28330 TREE_VEC_ELT (expanded_arguments, i)
28331 = tree_cons (NULL_TREE,
28332 TREE_VEC_ELT (expanded_exprs, j),
28333 TREE_VEC_ELT (expanded_arguments, i));
28334 else
28335 TREE_VEC_ELT (expanded_arguments, i)
28336 = tree_cons (NULL_TREE,
28337 TREE_VEC_ELT (expanded_exprs, i),
28338 TREE_VEC_ELT (expanded_arguments, i));
28339 }
28340 in_base_initializer = 0;
28341
28342 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
28343 since we built them backwards. */
28344 for (i = 0; i < len; i++)
28345 {
28346 TREE_VEC_ELT (expanded_arguments, i) =
28347 nreverse (TREE_VEC_ELT (expanded_arguments, i));
28348 }
28349 }
28350 }
28351
28352 for (i = 0; i < len; ++i)
28353 {
28354 if (expanded_bases)
28355 {
28356 decl = TREE_VEC_ELT (expanded_bases, i);
28357 decl = expand_member_init (decl);
28358 init = TREE_VEC_ELT (expanded_arguments, i);
28359 }
28360 else
28361 {
28362 tree tmp;
28363 if (TYPE_P (TREE_PURPOSE (t)))
28364 decl = tsubst (TREE_PURPOSE (t), argvec,
28365 tf_warning_or_error, NULL_TREE);
28366 else
28367 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
28368 tf_warning_or_error, NULL_TREE);
28369
28370 decl = expand_member_init (decl);
28371 if (decl && !DECL_P (decl))
28372 in_base_initializer = 1;
28373
28374 init = TREE_VALUE (t);
28375 tmp = init;
28376 if (init != void_type_node)
28377 init = tsubst_expr (init, argvec,
28378 tf_warning_or_error, NULL_TREE);
28379 if (init == NULL_TREE && tmp != NULL_TREE)
28380 /* If we had an initializer but it instantiated to nothing,
28381 value-initialize the object. This will only occur when
28382 the initializer was a pack expansion where the parameter
28383 packs used in that expansion were of length zero. */
28384 init = void_type_node;
28385 in_base_initializer = 0;
28386 }
28387
28388 if (target_ctor != error_mark_node
28389 && init != error_mark_node)
28390 {
28391 error ("mem-initializer for %qD follows constructor delegation",
28392 decl);
28393 return inits;
28394 }
28395 /* Look for a target constructor. */
28396 if (init != error_mark_node
28397 && decl && CLASS_TYPE_P (decl)
28398 && same_type_p (decl, current_class_type))
28399 {
28400 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
28401 if (inits)
28402 {
28403 error ("constructor delegation follows mem-initializer for %qD",
28404 TREE_PURPOSE (inits));
28405 continue;
28406 }
28407 target_ctor = init;
28408 }
28409
28410 if (decl)
28411 {
28412 init = build_tree_list (decl, init);
28413 /* Carry over the dummy TREE_TYPE node containing the source
28414 location. */
28415 TREE_TYPE (init) = TREE_TYPE (t);
28416 TREE_CHAIN (init) = inits;
28417 inits = init;
28418 }
28419 }
28420 }
28421 return inits;
28422 }
28423
28424 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
28425 is the instantiation (which should have been created with
28426 start_enum) and ARGS are the template arguments to use. */
28427
28428 static void
28429 tsubst_enum (tree tag, tree newtag, tree args)
28430 {
28431 tree e;
28432
28433 if (SCOPED_ENUM_P (newtag))
28434 begin_scope (sk_scoped_enum, newtag);
28435
28436 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
28437 {
28438 tree value;
28439 tree decl = TREE_VALUE (e);
28440
28441 /* Note that in a template enum, the TREE_VALUE is the
28442 CONST_DECL, not the corresponding INTEGER_CST. */
28443 value = tsubst_expr (DECL_INITIAL (decl),
28444 args, tf_warning_or_error, NULL_TREE);
28445
28446 /* Give this enumeration constant the correct access. */
28447 set_current_access_from_decl (decl);
28448
28449 /* Actually build the enumerator itself. Here we're assuming that
28450 enumerators can't have dependent attributes. */
28451 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
28452 DECL_ATTRIBUTES (decl),
28453 DECL_SOURCE_LOCATION (decl));
28454 /* Attribute deprecated without an argument isn't sticky: it'll
28455 melt into a tree flag, so we need to propagate the flag here,
28456 since we just created a new enumerator. */
28457 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
28458 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
28459 }
28460
28461 if (SCOPED_ENUM_P (newtag))
28462 finish_scope ();
28463
28464 finish_enum_value_list (newtag);
28465 finish_enum (newtag);
28466
28467 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
28468 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
28469 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
28470 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
28471 }
28472
28473 /* DECL is a FUNCTION_DECL that is a template specialization. Return
28474 its type -- but without substituting the innermost set of template
28475 arguments. So, innermost set of template parameters will appear in
28476 the type. */
28477
28478 tree
28479 get_mostly_instantiated_function_type (tree decl)
28480 {
28481 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
28482 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
28483 }
28484
28485 /* Return truthvalue if we're processing a template different from
28486 the last one involved in diagnostics. */
28487 bool
28488 problematic_instantiation_changed (void)
28489 {
28490 return current_tinst_level != last_error_tinst_level;
28491 }
28492
28493 /* Remember current template involved in diagnostics. */
28494 void
28495 record_last_problematic_instantiation (void)
28496 {
28497 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
28498 }
28499
28500 struct tinst_level *
28501 current_instantiation (void)
28502 {
28503 return current_tinst_level;
28504 }
28505
28506 /* Return TRUE if current_function_decl is being instantiated, false
28507 otherwise. */
28508
28509 bool
28510 instantiating_current_function_p (void)
28511 {
28512 return (current_instantiation ()
28513 && (current_instantiation ()->maybe_get_node ()
28514 == current_function_decl));
28515 }
28516
28517 /* [temp.param] Check that template non-type parm TYPE is of an allowable
28518 type. Return false for ok, true for disallowed. Issue error and
28519 inform messages under control of COMPLAIN. */
28520
28521 static bool
28522 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
28523 {
28524 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
28525 return false;
28526 else if (TYPE_PTR_P (type))
28527 return false;
28528 else if (TYPE_REF_P (type)
28529 && !TYPE_REF_IS_RVALUE (type))
28530 return false;
28531 else if (TYPE_PTRMEM_P (type))
28532 return false;
28533 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
28534 {
28535 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
28536 {
28537 if (complain & tf_error)
28538 error ("non-type template parameters of deduced class type only "
28539 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28540 return true;
28541 }
28542 return false;
28543 }
28544 else if (TREE_CODE (type) == NULLPTR_TYPE)
28545 return false;
28546 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
28547 && cxx_dialect < cxx11)
28548 /* Fall through; before C++11 alias templates, a bound ttp
28549 always instantiates into a class type. */;
28550 else if (WILDCARD_TYPE_P (type))
28551 /* Any other wildcard type not already handled above is allowed. */
28552 return false;
28553 else if (TREE_CODE (type) == COMPLEX_TYPE)
28554 /* Fall through. */;
28555 else if (VOID_TYPE_P (type))
28556 /* Fall through. */;
28557 else if (cxx_dialect >= cxx20)
28558 {
28559 if (dependent_type_p (type))
28560 return false;
28561 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
28562 return true;
28563 if (structural_type_p (type))
28564 return false;
28565 if (complain & tf_error)
28566 {
28567 auto_diagnostic_group d;
28568 error ("%qT is not a valid type for a template non-type "
28569 "parameter because it is not structural", type);
28570 structural_type_p (type, true);
28571 }
28572 return true;
28573 }
28574 else if (CLASS_TYPE_P (type))
28575 {
28576 if (complain & tf_error)
28577 error ("non-type template parameters of class type only available "
28578 "with %<-std=c++20%> or %<-std=gnu++20%>");
28579 return true;
28580 }
28581
28582 if (complain & tf_error)
28583 {
28584 if (type == error_mark_node)
28585 inform (input_location, "invalid template non-type parameter");
28586 else
28587 error ("%q#T is not a valid type for a template non-type parameter",
28588 type);
28589 }
28590 return true;
28591 }
28592
28593 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
28594
28595 static bool
28596 value_dependent_noexcept_spec_p (tree type)
28597 {
28598 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
28599 if (tree noex = TREE_PURPOSE (spec))
28600 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
28601 affect overload resolution and treating it as dependent breaks
28602 things. Same for an unparsed noexcept expression. */
28603 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
28604 && TREE_CODE (noex) != DEFERRED_PARSE
28605 && value_dependent_expression_p (noex))
28606 return true;
28607
28608 return false;
28609 }
28610
28611 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
28612 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
28613
28614 static bool
28615 dependent_type_p_r (tree type)
28616 {
28617 tree scope;
28618
28619 /* [temp.dep.type]
28620
28621 A type is dependent if it is:
28622
28623 -- a template parameter. Template template parameters are types
28624 for us (since TYPE_P holds true for them) so we handle
28625 them here. */
28626 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28627 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
28628 return true;
28629 /* -- a qualified-id with a nested-name-specifier which contains a
28630 class-name that names a dependent type or whose unqualified-id
28631 names a dependent type. */
28632 if (TREE_CODE (type) == TYPENAME_TYPE)
28633 return true;
28634
28635 /* -- a cv-qualified type where the cv-unqualified type is
28636 dependent.
28637 No code is necessary for this bullet; the code below handles
28638 cv-qualified types, and we don't want to strip aliases with
28639 TYPE_MAIN_VARIANT because of DR 1558. */
28640 /* -- a compound type constructed from any dependent type. */
28641 if (TYPE_PTRMEM_P (type))
28642 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
28643 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
28644 (type)));
28645 else if (INDIRECT_TYPE_P (type))
28646 return dependent_type_p (TREE_TYPE (type));
28647 else if (FUNC_OR_METHOD_TYPE_P (type))
28648 {
28649 tree arg_type;
28650
28651 if (dependent_type_p (TREE_TYPE (type)))
28652 return true;
28653 for (arg_type = TYPE_ARG_TYPES (type);
28654 arg_type;
28655 arg_type = TREE_CHAIN (arg_type))
28656 if (dependent_type_p (TREE_VALUE (arg_type)))
28657 return true;
28658 if (cxx_dialect >= cxx17
28659 && value_dependent_noexcept_spec_p (type))
28660 /* A value-dependent noexcept-specifier makes the type dependent. */
28661 return true;
28662 return false;
28663 }
28664 /* -- an array type constructed from any dependent type or whose
28665 size is specified by a constant expression that is
28666 value-dependent.
28667
28668 We checked for type- and value-dependence of the bounds in
28669 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
28670 if (TREE_CODE (type) == ARRAY_TYPE)
28671 {
28672 if (TYPE_DOMAIN (type)
28673 && dependent_type_p (TYPE_DOMAIN (type)))
28674 return true;
28675 return dependent_type_p (TREE_TYPE (type));
28676 }
28677
28678 /* -- a template-id in which either the template name is a template
28679 parameter ... */
28680 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
28681 return true;
28682 /* ... or any of the template arguments is a dependent type or
28683 an expression that is type-dependent or value-dependent. */
28684 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
28685 && (any_dependent_template_arguments_p
28686 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
28687 return true;
28688
28689 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
28690 dependent; if the argument of the `typeof' expression is not
28691 type-dependent, then it should already been have resolved. */
28692 if (TREE_CODE (type) == TYPEOF_TYPE
28693 || TREE_CODE (type) == DECLTYPE_TYPE
28694 || TREE_CODE (type) == TRAIT_TYPE)
28695 return true;
28696
28697 /* A template argument pack is dependent if any of its packed
28698 arguments are. */
28699 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
28700 {
28701 tree args = ARGUMENT_PACK_ARGS (type);
28702 for (tree arg : tree_vec_range (args))
28703 if (dependent_template_arg_p (arg))
28704 return true;
28705 }
28706
28707 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
28708 be template parameters. This includes pack-index-specifiers. */
28709 if (TREE_CODE (type) == TYPE_PACK_EXPANSION
28710 || TREE_CODE (type) == PACK_INDEX_TYPE)
28711 return true;
28712
28713 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
28714 return true;
28715
28716 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
28717 return true;
28718
28719 /* The standard does not specifically mention types that are local
28720 to template functions or local classes, but they should be
28721 considered dependent too. For example:
28722
28723 template <int I> void f() {
28724 enum E { a = I };
28725 S<sizeof (E)> s;
28726 }
28727
28728 The size of `E' cannot be known until the value of `I' has been
28729 determined. Therefore, `E' must be considered dependent. */
28730 scope = TYPE_CONTEXT (type);
28731 if (scope && TYPE_P (scope))
28732 return dependent_type_p (scope);
28733 /* Don't use type_dependent_expression_p here, as it can lead
28734 to infinite recursion trying to determine whether a lambda
28735 nested in a lambda is dependent (c++/47687). */
28736 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
28737 && DECL_LANG_SPECIFIC (scope)
28738 && DECL_TEMPLATE_INFO (scope)
28739 && (any_dependent_template_arguments_p
28740 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
28741 return true;
28742
28743 /* Other types are non-dependent. */
28744 return false;
28745 }
28746
28747 /* Returns TRUE if TYPE is dependent, in the sense of
28748 [temp.dep.type]. Note that a NULL type is considered dependent. */
28749
28750 bool
28751 dependent_type_p (tree type)
28752 {
28753 /* If there are no template parameters in scope, then there can't be
28754 any dependent types. */
28755 if (!processing_template_decl)
28756 {
28757 /* If we are not processing a template, then nobody should be
28758 providing us with a dependent type. */
28759 gcc_assert (type);
28760 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)
28761 || seen_error ());
28762 return false;
28763 }
28764
28765 /* If the type is NULL, we have not computed a type for the entity
28766 in question; in that case, the type is dependent. */
28767 if (!type)
28768 return true;
28769
28770 /* Erroneous types can be considered non-dependent. */
28771 if (type == error_mark_node)
28772 return false;
28773
28774 /* If we have not already computed the appropriate value for TYPE,
28775 do so now. */
28776 if (!TYPE_DEPENDENT_P_VALID (type))
28777 {
28778 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
28779 TYPE_DEPENDENT_P_VALID (type) = 1;
28780 }
28781
28782 return TYPE_DEPENDENT_P (type);
28783 }
28784
28785 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
28786 lookup. In other words, a dependent type that is not the current
28787 instantiation. */
28788
28789 bool
28790 dependent_scope_p (tree scope)
28791 {
28792 return (scope && TYPE_P (scope) && dependent_type_p (scope)
28793 && !currently_open_class (scope));
28794 }
28795
28796 /* True if we might find more declarations in SCOPE during instantiation than
28797 we can when parsing the template. */
28798
28799 bool
28800 dependentish_scope_p (tree scope)
28801 {
28802 return dependent_scope_p (scope) || any_dependent_bases_p (scope)
28803 /* A noexcept-spec is a complete-class context, so this should never hold.
28804 But since we don't implement deferred noexcept-spec parsing of a friend
28805 declaration (PR114764) we compensate by treating the current
28806 instantiation as dependent to avoid bogus name lookup failures in this
28807 case (PR122668). */
28808 || (cp_noexcept_operand
28809 && CLASS_TYPE_P (scope)
28810 && TYPE_BEING_DEFINED (scope)
28811 && dependent_type_p (scope));
28812 }
28813
28814 /* T is a SCOPE_REF. Return whether it represents a non-static member of
28815 an unknown base of 'this' (and is therefore instantiation-dependent). */
28816
28817 static bool
28818 unknown_base_ref_p (tree t)
28819 {
28820 if (!current_class_ptr)
28821 return false;
28822
28823 tree mem = TREE_OPERAND (t, 1);
28824 if (shared_member_p (mem))
28825 return false;
28826
28827 tree cur = current_nonlambda_class_type ();
28828 if (!any_dependent_bases_p (cur))
28829 return false;
28830
28831 tree ctx = TREE_OPERAND (t, 0);
28832 if (DERIVED_FROM_P (ctx, cur))
28833 return false;
28834
28835 return true;
28836 }
28837
28838 /* T is a SCOPE_REF; return whether we need to consider it
28839 instantiation-dependent so that we can check access at instantiation
28840 time even though we know which member it resolves to. */
28841
28842 static bool
28843 instantiation_dependent_scope_ref_p (tree t)
28844 {
28845 if (DECL_P (TREE_OPERAND (t, 1))
28846 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
28847 && !dependent_scope_p (TREE_OPERAND (t, 0))
28848 && !unknown_base_ref_p (t)
28849 && accessible_in_template_p (TREE_OPERAND (t, 0),
28850 TREE_OPERAND (t, 1)))
28851 return false;
28852 else
28853 return true;
28854 }
28855
28856 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
28857 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
28858 expression. */
28859
28860 /* Note that this predicate is not appropriate for general expressions;
28861 only constant expressions (that satisfy potential_constant_expression)
28862 can be tested for value dependence. */
28863
28864 bool
28865 value_dependent_expression_p (tree expression)
28866 {
28867 if (!processing_template_decl || expression == NULL_TREE)
28868 return false;
28869
28870 /* A type-dependent expression is also value-dependent. */
28871 if (type_dependent_expression_p (expression))
28872 return true;
28873
28874 switch (TREE_CODE (expression))
28875 {
28876 case BASELINK:
28877 /* A dependent member function of the current instantiation. */
28878 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
28879
28880 case FUNCTION_DECL:
28881 /* A dependent member function of the current instantiation. */
28882 if (DECL_CLASS_SCOPE_P (expression)
28883 && dependent_type_p (DECL_CONTEXT (expression)))
28884 return true;
28885 break;
28886
28887 case IDENTIFIER_NODE:
28888 /* A name that has not been looked up -- must be dependent. */
28889 return true;
28890
28891 case TEMPLATE_PARM_INDEX:
28892 /* A non-type template parm. */
28893 return true;
28894
28895 case CONST_DECL:
28896 /* A non-type template parm. */
28897 if (DECL_TEMPLATE_PARM_P (expression))
28898 return true;
28899 return value_dependent_expression_p (DECL_INITIAL (expression));
28900
28901 case VAR_DECL:
28902 /* A constant with literal type and is initialized
28903 with an expression that is value-dependent. */
28904 if (DECL_DEPENDENT_INIT_P (expression))
28905 return true;
28906 if (DECL_HAS_VALUE_EXPR_P (expression))
28907 {
28908 tree value_expr = DECL_VALUE_EXPR (expression);
28909 if (value_dependent_expression_p (value_expr)
28910 /* __PRETTY_FUNCTION__ inside a template function is dependent
28911 on the name of the function. */
28912 || (DECL_PRETTY_FUNCTION_P (expression)
28913 /* It might be used in a template, but not a template
28914 function, in which case its DECL_VALUE_EXPR will be
28915 "top level". */
28916 && value_expr == error_mark_node))
28917 return true;
28918 }
28919 else if (TYPE_REF_P (TREE_TYPE (expression)))
28920 /* FIXME cp_finish_decl doesn't fold reference initializers. */
28921 return true;
28922 /* We have a constexpr variable and we're processing a template. When
28923 there's lifetime extension involved (for which finish_compound_literal
28924 used to create a temporary), we'll not be able to evaluate the
28925 variable until instantiating, so pretend it's value-dependent. */
28926 else if (DECL_DECLARED_CONSTEXPR_P (expression)
28927 && !TREE_CONSTANT (expression))
28928 return true;
28929 return false;
28930
28931 case DYNAMIC_CAST_EXPR:
28932 case STATIC_CAST_EXPR:
28933 case CONST_CAST_EXPR:
28934 case REINTERPRET_CAST_EXPR:
28935 case CAST_EXPR:
28936 case IMPLICIT_CONV_EXPR:
28937 /* These expressions are value-dependent if the type to which
28938 the cast occurs is dependent or the expression being casted
28939 is value-dependent. */
28940 {
28941 tree type = TREE_TYPE (expression);
28942
28943 if (dependent_type_p (type))
28944 return true;
28945
28946 /* A functional cast has a list of operands. */
28947 expression = TREE_OPERAND (expression, 0);
28948 if (!expression)
28949 {
28950 /* If there are no operands, it must be an expression such
28951 as "int()". This should not happen for aggregate types
28952 because it would form non-constant expressions. */
28953 gcc_assert (cxx_dialect >= cxx11
28954 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
28955
28956 return false;
28957 }
28958
28959 if (TREE_CODE (expression) == TREE_LIST)
28960 return any_value_dependent_elements_p (expression);
28961
28962 if (TREE_CODE (type) == REFERENCE_TYPE
28963 && has_value_dependent_address (expression))
28964 return true;
28965
28966 return value_dependent_expression_p (expression);
28967 }
28968
28969 case SIZEOF_EXPR:
28970 if (SIZEOF_EXPR_TYPE_P (expression))
28971 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
28972 /* FALLTHRU */
28973 case ALIGNOF_EXPR:
28974 case TYPEID_EXPR:
28975 /* A `sizeof' expression is value-dependent if the operand is
28976 type-dependent or is a pack expansion. */
28977 expression = TREE_OPERAND (expression, 0);
28978 if (PACK_EXPANSION_P (expression))
28979 return true;
28980 else if (TYPE_P (expression))
28981 return dependent_type_p (expression);
28982 return instantiation_dependent_uneval_expression_p (expression);
28983
28984 case AT_ENCODE_EXPR:
28985 /* An 'encode' expression is value-dependent if the operand is
28986 type-dependent. */
28987 expression = TREE_OPERAND (expression, 0);
28988 return dependent_type_p (expression);
28989
28990 case NOEXCEPT_EXPR:
28991 expression = TREE_OPERAND (expression, 0);
28992 return instantiation_dependent_uneval_expression_p (expression);
28993
28994 case SCOPE_REF:
28995 /* All instantiation-dependent expressions should also be considered
28996 value-dependent. */
28997 return instantiation_dependent_scope_ref_p (expression);
28998
28999 case COMPONENT_REF:
29000 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
29001 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
29002
29003 case NONTYPE_ARGUMENT_PACK:
29004 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
29005 is value-dependent. */
29006 for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
29007 if (value_dependent_expression_p (arg))
29008 return true;
29009 return false;
29010
29011 case TRAIT_EXPR:
29012 {
29013 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
29014 return true;
29015
29016 tree type2 = TRAIT_EXPR_TYPE2 (expression);
29017 if (!type2)
29018 return false;
29019
29020 if (TREE_CODE (type2) != TREE_VEC)
29021 return dependent_type_p (type2);
29022
29023 for (tree arg : tree_vec_range (type2))
29024 if (dependent_type_p (arg))
29025 return true;
29026
29027 return false;
29028 }
29029
29030 case MODOP_EXPR:
29031 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
29032 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
29033
29034 case ARRAY_REF:
29035 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
29036 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
29037
29038 case ADDR_EXPR:
29039 {
29040 tree op = TREE_OPERAND (expression, 0);
29041 return (value_dependent_expression_p (op)
29042 || has_value_dependent_address (op));
29043 }
29044
29045 case REQUIRES_EXPR:
29046 /* Treat all requires-expressions as value-dependent so
29047 we don't try to fold them. */
29048 return true;
29049
29050 case TYPE_REQ:
29051 return dependent_type_p (TREE_OPERAND (expression, 0));
29052
29053 case CALL_EXPR:
29054 {
29055 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
29056 return true;
29057 tree fn = get_callee_fndecl (expression);
29058 int i, nargs;
29059 nargs = call_expr_nargs (expression);
29060 for (i = 0; i < nargs; ++i)
29061 {
29062 tree op = CALL_EXPR_ARG (expression, i);
29063 /* In a call to a constexpr member function, look through the
29064 implicit ADDR_EXPR on the object argument so that it doesn't
29065 cause the call to be considered value-dependent. We also
29066 look through it in potential_constant_expression. */
29067 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
29068 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
29069 && TREE_CODE (op) == ADDR_EXPR)
29070 op = TREE_OPERAND (op, 0);
29071 if (value_dependent_expression_p (op))
29072 return true;
29073 }
29074 return false;
29075 }
29076
29077 case TEMPLATE_ID_EXPR:
29078 return concept_definition_p (TREE_OPERAND (expression, 0))
29079 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
29080
29081 case CONSTRUCTOR:
29082 {
29083 unsigned ix;
29084 tree val;
29085 if (dependent_type_p (TREE_TYPE (expression)))
29086 return true;
29087 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
29088 if (value_dependent_expression_p (val))
29089 return true;
29090 return false;
29091 }
29092
29093 case STMT_EXPR:
29094 /* Treat a GNU statement expression as dependent to avoid crashing
29095 under instantiate_non_dependent_expr; it can't be constant. */
29096 return true;
29097
29098 case NEW_EXPR:
29099 case VEC_NEW_EXPR:
29100 /* The second operand is a type, which type_dependent_expression_p
29101 (and therefore value_dependent_expression_p) doesn't want to see. */
29102 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
29103 || value_dependent_expression_p (TREE_OPERAND (expression, 2))
29104 || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
29105
29106 default:
29107 /* A constant expression is value-dependent if any subexpression is
29108 value-dependent. */
29109 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
29110 {
29111 case tcc_reference:
29112 case tcc_unary:
29113 case tcc_comparison:
29114 case tcc_binary:
29115 case tcc_expression:
29116 case tcc_vl_exp:
29117 {
29118 int i, len = cp_tree_operand_length (expression);
29119
29120 for (i = 0; i < len; i++)
29121 {
29122 tree t = TREE_OPERAND (expression, i);
29123
29124 /* In some cases, some of the operands may be missing.
29125 (For example, in the case of PREDECREMENT_EXPR, the
29126 amount to increment by may be missing.) That doesn't
29127 make the expression dependent. */
29128 if (t && value_dependent_expression_p (t))
29129 return true;
29130 }
29131 }
29132 break;
29133 default:
29134 break;
29135 }
29136 break;
29137 }
29138
29139 /* The expression is not value-dependent. */
29140 return false;
29141 }
29142
29143 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
29144 [temp.dep.expr]. Note that an expression with no type is
29145 considered dependent. Other parts of the compiler arrange for an
29146 expression with type-dependent subexpressions to have no type, so
29147 this function doesn't have to be fully recursive. */
29148
29149 bool
29150 type_dependent_expression_p (tree expression)
29151 {
29152 if (!processing_template_decl)
29153 return false;
29154
29155 if (expression == NULL_TREE || expression == error_mark_node)
29156 return false;
29157
29158 gcc_checking_assert (!TYPE_P (expression));
29159
29160 STRIP_ANY_LOCATION_WRAPPER (expression);
29161
29162 /* Assume a TU-local entity is not dependent, we'll error later when
29163 instantiating anyway. */
29164 if (TREE_CODE (expression) == TU_LOCAL_ENTITY)
29165 return false;
29166
29167 /* An unresolved name is always dependent. */
29168 if (identifier_p (expression)
29169 || TREE_CODE (expression) == USING_DECL)
29170 return true;
29171
29172 /* A lambda-expression in template context is dependent. dependent_type_p is
29173 true for a lambda in the scope of a class or function template, but that
29174 doesn't cover all template contexts, like a default template argument. */
29175 if (TREE_CODE (expression) == LAMBDA_EXPR)
29176 return true;
29177
29178 /* A fold expression is type-dependent. */
29179 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
29180 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
29181 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
29182 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
29183 return true;
29184
29185 /* Some expression forms are never type-dependent. */
29186 if (TREE_CODE (expression) == SIZEOF_EXPR
29187 || TREE_CODE (expression) == ALIGNOF_EXPR
29188 || TREE_CODE (expression) == AT_ENCODE_EXPR
29189 || TREE_CODE (expression) == NOEXCEPT_EXPR
29190 || TREE_CODE (expression) == TRAIT_EXPR
29191 || TREE_CODE (expression) == TYPEID_EXPR
29192 || TREE_CODE (expression) == DELETE_EXPR
29193 || TREE_CODE (expression) == VEC_DELETE_EXPR
29194 || TREE_CODE (expression) == THROW_EXPR
29195 || TREE_CODE (expression) == REQUIRES_EXPR)
29196 return false;
29197
29198 /* The types of these expressions depends only on the type to which
29199 the cast occurs. */
29200 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
29201 || TREE_CODE (expression) == STATIC_CAST_EXPR
29202 || TREE_CODE (expression) == CONST_CAST_EXPR
29203 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
29204 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
29205 || TREE_CODE (expression) == CAST_EXPR)
29206 return dependent_type_p (TREE_TYPE (expression));
29207
29208 /* The types of these expressions depends only on the type created
29209 by the expression. */
29210 if (TREE_CODE (expression) == NEW_EXPR
29211 || TREE_CODE (expression) == VEC_NEW_EXPR)
29212 {
29213 /* For NEW_EXPR tree nodes created inside a template, either
29214 the object type itself or a TREE_LIST may appear as the
29215 operand 1. */
29216 tree type = TREE_OPERAND (expression, 1);
29217 if (TREE_CODE (type) == TREE_LIST)
29218 /* This is an array type. We need to check array dimensions
29219 as well. */
29220 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
29221 || value_dependent_expression_p
29222 (TREE_OPERAND (TREE_VALUE (type), 1));
29223 /* Array type whose dimension has to be deduced. */
29224 else if (TREE_CODE (type) == ARRAY_TYPE
29225 && TREE_OPERAND (expression, 2) == NULL_TREE)
29226 return true;
29227 else
29228 return dependent_type_p (type);
29229 }
29230
29231 if (TREE_CODE (expression) == SCOPE_REF)
29232 {
29233 tree scope = TREE_OPERAND (expression, 0);
29234 tree name = TREE_OPERAND (expression, 1);
29235
29236 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
29237 contains an identifier associated by name lookup with one or more
29238 declarations declared with a dependent type, or...a
29239 nested-name-specifier or qualified-id that names a member of an
29240 unknown specialization. */
29241 return (type_dependent_expression_p (name)
29242 || dependent_scope_p (scope));
29243 }
29244
29245 if (TREE_CODE (expression) == TEMPLATE_DECL
29246 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
29247 return uses_outer_template_parms (expression);
29248
29249 if (TREE_CODE (expression) == STMT_EXPR)
29250 expression = stmt_expr_value_expr (expression);
29251
29252 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
29253 {
29254 for (auto &elt : CONSTRUCTOR_ELTS (expression))
29255 if (type_dependent_expression_p (elt.value))
29256 return true;
29257 return false;
29258 }
29259
29260 /* A static data member of the current instantiation with incomplete
29261 array type is type-dependent, as the definition and specializations
29262 can have different bounds. */
29263 if (VAR_P (expression)
29264 && DECL_CLASS_SCOPE_P (expression)
29265 && dependent_type_p (DECL_CONTEXT (expression))
29266 && VAR_HAD_UNKNOWN_BOUND (expression))
29267 return true;
29268
29269 /* An array of unknown bound depending on a variadic parameter, eg:
29270
29271 template<typename... Args>
29272 void foo (Args... args)
29273 {
29274 int arr[] = { args... };
29275 }
29276
29277 template<int... vals>
29278 void bar ()
29279 {
29280 int arr[] = { vals... };
29281 }
29282
29283 If the array has no length and has an initializer, it must be that
29284 we couldn't determine its length in cp_complete_array_type because
29285 it is dependent. */
29286 if (((VAR_P (expression) && DECL_INITIAL (expression))
29287 || COMPOUND_LITERAL_P (expression))
29288 && TREE_TYPE (expression) != NULL_TREE
29289 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
29290 && !TYPE_DOMAIN (TREE_TYPE (expression)))
29291 return true;
29292
29293 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
29294 if (BASELINK_P (expression))
29295 {
29296 if (BASELINK_OPTYPE (expression)
29297 && dependent_type_p (BASELINK_OPTYPE (expression)))
29298 return true;
29299 expression = BASELINK_FUNCTIONS (expression);
29300 }
29301
29302 /* A function or variable template-id is type-dependent if it has any
29303 dependent template arguments. */
29304 if (VAR_OR_FUNCTION_DECL_P (expression)
29305 && DECL_LANG_SPECIFIC (expression)
29306 && DECL_TEMPLATE_INFO (expression))
29307 {
29308 /* Consider the innermost template arguments, since those are the ones
29309 that come from the template-id; the template arguments for the
29310 enclosing class do not make it type-dependent unless they are used in
29311 the type of the decl. */
29312 if (instantiates_primary_template_p (expression)
29313 && (any_dependent_template_arguments_p
29314 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
29315 return true;
29316 }
29317
29318 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
29319 type-dependent. Checking this is important for functions with auto return
29320 type, which looks like a dependent type. */
29321 if (TREE_CODE (expression) == FUNCTION_DECL
29322 && !(DECL_CLASS_SCOPE_P (expression)
29323 && dependent_type_p (DECL_CONTEXT (expression)))
29324 && !(DECL_LANG_SPECIFIC (expression)
29325 && DECL_UNIQUE_FRIEND_P (expression)
29326 && (!DECL_FRIEND_CONTEXT (expression)
29327 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
29328 && !DECL_LOCAL_DECL_P (expression))
29329 {
29330 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
29331 || undeduced_auto_decl (expression));
29332 return false;
29333 }
29334
29335 /* Otherwise, its constraints could still depend on outer template parameters
29336 from its (dependent) scope. */
29337 if (TREE_CODE (expression) == FUNCTION_DECL
29338 /* As an optimization, check this cheaper sufficient condition first.
29339 (At this point we've established that we're looking at a member of
29340 a dependent class, so it makes sense to start treating say undeduced
29341 auto as dependent.) */
29342 && !dependent_type_p (TREE_TYPE (expression))
29343 && uses_outer_template_parms_in_constraints (expression))
29344 return true;
29345
29346 /* Always dependent, on the number of arguments if nothing else. */
29347 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
29348 return true;
29349
29350 /* [temp.dep.expr]: "A pack-index-expression is type-dependent if its
29351 id-expression is type-dependent." */
29352 if (TREE_CODE (expression) == PACK_INDEX_EXPR)
29353 return type_dependent_expression_p (PACK_INDEX_PACK (expression));
29354
29355 if (TREE_TYPE (expression) == unknown_type_node)
29356 {
29357 if (TREE_CODE (expression) == ADDR_EXPR)
29358 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
29359 if (TREE_CODE (expression) == COMPONENT_REF
29360 || TREE_CODE (expression) == OFFSET_REF)
29361 {
29362 if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
29363 return true;
29364 expression = TREE_OPERAND (expression, 1);
29365 if (identifier_p (expression))
29366 return false;
29367 }
29368 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
29369 if (TREE_CODE (expression) == SCOPE_REF)
29370 return false;
29371
29372 if (BASELINK_P (expression))
29373 {
29374 if (BASELINK_OPTYPE (expression)
29375 && dependent_type_p (BASELINK_OPTYPE (expression)))
29376 return true;
29377 expression = BASELINK_FUNCTIONS (expression);
29378 }
29379
29380 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
29381 {
29382 tree args = TREE_OPERAND (expression, 1);
29383 if (any_dependent_template_arguments_p (args))
29384 return true;
29385 /* Arguments of a function template-id aren't necessarily coerced
29386 yet so we must conservatively assume that the address (and not
29387 just value) of the argument matters as per [temp.dep.temp]/3. */
29388 for (tree arg : tree_vec_range (args))
29389 if (has_value_dependent_address (arg))
29390 return true;
29391 expression = TREE_OPERAND (expression, 0);
29392 if (identifier_p (expression))
29393 return true;
29394 }
29395
29396 gcc_assert (OVL_P (expression));
29397
29398 for (lkp_iterator iter (expression); iter; ++iter)
29399 if (type_dependent_expression_p (*iter))
29400 return true;
29401
29402 return false;
29403 }
29404
29405 /* The type of a non-type template parm declared with a placeholder type
29406 depends on the corresponding template argument, even though
29407 placeholders are not normally considered dependent. */
29408 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
29409 && is_auto (TREE_TYPE (expression)))
29410 return true;
29411
29412 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
29413
29414 /* Dependent type attributes might not have made it from the decl to
29415 the type yet. */
29416 if (DECL_P (expression)
29417 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
29418 return true;
29419
29420 return (dependent_type_p (TREE_TYPE (expression)));
29421 }
29422
29423 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
29424 type-dependent if the expression refers to a member of the current
29425 instantiation and the type of the referenced member is dependent, or the
29426 class member access expression refers to a member of an unknown
29427 specialization.
29428
29429 This function returns true if the OBJECT in such a class member access
29430 expression is of an unknown specialization. */
29431
29432 bool
29433 type_dependent_object_expression_p (tree object)
29434 {
29435 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
29436 dependent. */
29437 if (TREE_CODE (object) == IDENTIFIER_NODE)
29438 return true;
29439 tree scope = TREE_TYPE (object);
29440 return (!scope || dependent_scope_p (scope));
29441 }
29442
29443 /* walk_tree callback function for instantiation_dependent_expression_p,
29444 below. Returns non-zero if a dependent subexpression is found. */
29445
29446 static tree
29447 instantiation_dependent_r (tree *tp, int *walk_subtrees,
29448 void * /*data*/)
29449 {
29450 if (TYPE_P (*tp))
29451 {
29452 /* We don't have to worry about decltype currently because decltype
29453 of an instantiation-dependent expr is a dependent type. This
29454 might change depending on the resolution of DR 1172. */
29455 *walk_subtrees = false;
29456 return NULL_TREE;
29457 }
29458 enum tree_code code = TREE_CODE (*tp);
29459 switch (code)
29460 {
29461 /* Don't treat an argument list as dependent just because it has no
29462 TREE_TYPE. */
29463 case TREE_LIST:
29464 case TREE_VEC:
29465 case NONTYPE_ARGUMENT_PACK:
29466 return NULL_TREE;
29467
29468 case TEMPLATE_PARM_INDEX:
29469 if (dependent_type_p (TREE_TYPE (*tp)))
29470 return *tp;
29471 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
29472 return *tp;
29473 /* We'll check value-dependence separately. */
29474 return NULL_TREE;
29475
29476 /* Handle expressions with type operands. */
29477 case SIZEOF_EXPR:
29478 case ALIGNOF_EXPR:
29479 case TYPEID_EXPR:
29480 case AT_ENCODE_EXPR:
29481 {
29482 tree op = TREE_OPERAND (*tp, 0);
29483 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
29484 op = TREE_TYPE (op);
29485 if (TYPE_P (op))
29486 {
29487 if (dependent_type_p (op))
29488 return *tp;
29489 else
29490 {
29491 *walk_subtrees = false;
29492 return NULL_TREE;
29493 }
29494 }
29495 break;
29496 }
29497
29498 case COMPONENT_REF:
29499 if (identifier_p (TREE_OPERAND (*tp, 1)))
29500 /* In a template, finish_class_member_access_expr creates a
29501 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
29502 type-dependent, so that we can check access control at
29503 instantiation time (PR 42277). See also Core issue 1273. */
29504 return *tp;
29505 break;
29506
29507 case SCOPE_REF:
29508 if (instantiation_dependent_scope_ref_p (*tp))
29509 return *tp;
29510 else
29511 break;
29512
29513 /* Treat statement-expressions as dependent. */
29514 case BIND_EXPR:
29515 return *tp;
29516
29517 /* Treat requires-expressions as dependent. */
29518 case REQUIRES_EXPR:
29519 return *tp;
29520
29521 case CONSTRUCTOR:
29522 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
29523 return *tp;
29524 break;
29525
29526 case TEMPLATE_DECL:
29527 case FUNCTION_DECL:
29528 /* Before C++17, a noexcept-specifier isn't part of the function type
29529 so it doesn't affect type dependence, but we still want to consider it
29530 for instantiation dependence. */
29531 if (cxx_dialect < cxx17
29532 && DECL_DECLARES_FUNCTION_P (*tp)
29533 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
29534 return *tp;
29535 break;
29536
29537 default:
29538 break;
29539 }
29540
29541 if (type_dependent_expression_p (*tp))
29542 return *tp;
29543 else
29544 return NULL_TREE;
29545 }
29546
29547 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
29548 sense defined by the ABI:
29549
29550 "An expression is instantiation-dependent if it is type-dependent
29551 or value-dependent, or it has a subexpression that is type-dependent
29552 or value-dependent."
29553
29554 Except don't actually check value-dependence for unevaluated expressions,
29555 because in sizeof(i) we don't care about the value of i. Checking
29556 type-dependence will in turn check value-dependence of array bounds/template
29557 arguments as needed. */
29558
29559 bool
29560 instantiation_dependent_uneval_expression_p (tree expression)
29561 {
29562 tree result;
29563
29564 if (!processing_template_decl)
29565 return false;
29566
29567 if (expression == error_mark_node)
29568 return false;
29569
29570 result = cp_walk_tree_without_duplicates (&expression,
29571 instantiation_dependent_r, NULL);
29572 return result != NULL_TREE;
29573 }
29574
29575 /* As above, but also check value-dependence of the expression as a whole. */
29576
29577 bool
29578 instantiation_dependent_expression_p (tree expression)
29579 {
29580 return (instantiation_dependent_uneval_expression_p (expression)
29581 || (processing_template_decl
29582 && potential_constant_expression (expression)
29583 && value_dependent_expression_p (expression)));
29584 }
29585
29586 /* Like type_dependent_expression_p, but it also works while not processing
29587 a template definition, i.e. during substitution or mangling. */
29588
29589 bool
29590 type_dependent_expression_p_push (tree expr)
29591 {
29592 bool b;
29593 ++processing_template_decl;
29594 b = type_dependent_expression_p (expr);
29595 --processing_template_decl;
29596 return b;
29597 }
29598
29599 /* Returns TRUE if ARGS contains a type-dependent expression. */
29600
29601 bool
29602 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
29603 {
29604 if (!processing_template_decl || !args)
29605 return false;
29606
29607 for (tree arg : *args)
29608 if (type_dependent_expression_p (arg))
29609 return true;
29610
29611 return false;
29612 }
29613
29614 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
29615 expressions) contains any type-dependent expressions. */
29616
29617 bool
29618 any_type_dependent_elements_p (const_tree list)
29619 {
29620 for (; list; list = TREE_CHAIN (list))
29621 if (type_dependent_expression_p (TREE_VALUE (list)))
29622 return true;
29623
29624 return false;
29625 }
29626
29627 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
29628 expressions) contains any value-dependent expressions. */
29629
29630 bool
29631 any_value_dependent_elements_p (const_tree list)
29632 {
29633 for (; list; list = TREE_CHAIN (list))
29634 if (value_dependent_expression_p (TREE_VALUE (list)))
29635 return true;
29636
29637 return false;
29638 }
29639
29640 /* Returns TRUE if the ARG (a template argument) is dependent. */
29641
29642 bool
29643 dependent_template_arg_p (tree arg)
29644 {
29645 if (!processing_template_decl)
29646 return false;
29647
29648 /* Assume a template argument that was wrongly written by the user
29649 is dependent. This is consistent with what
29650 any_dependent_template_arguments_p [that calls this function]
29651 does. */
29652 if (!arg || arg == error_mark_node)
29653 return true;
29654
29655 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
29656 arg = argument_pack_select_arg (arg);
29657
29658 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
29659 return true;
29660 if (TREE_CODE (arg) == TEMPLATE_DECL)
29661 {
29662 if (DECL_TEMPLATE_PARM_P (arg))
29663 return true;
29664 /* A member template of a dependent class is not necessarily
29665 type-dependent, but it is a dependent template argument because it
29666 will be a member of an unknown specialization to that template. */
29667 tree scope = CP_DECL_CONTEXT (arg);
29668 return TYPE_P (scope) && dependent_type_p (scope);
29669 }
29670 else if (ARGUMENT_PACK_P (arg))
29671 {
29672 tree args = ARGUMENT_PACK_ARGS (arg);
29673 for (tree arg : tree_vec_range (args))
29674 if (dependent_template_arg_p (arg))
29675 return true;
29676 return false;
29677 }
29678 else if (TYPE_P (arg))
29679 return dependent_type_p (arg);
29680 else
29681 return value_dependent_expression_p (arg);
29682 }
29683
29684 /* Identify any expressions that use function parms. */
29685
29686 static tree
29687 find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
29688 {
29689 tree t = *tp;
29690 if (TREE_CODE (t) == PARM_DECL)
29691 {
29692 *walk_subtrees = 0;
29693 return t;
29694 }
29695 return NULL_TREE;
29696 }
29697
29698 /* Returns true if a type specialization formed using the template
29699 arguments ARGS needs to use structural equality. */
29700
29701 bool
29702 any_template_arguments_need_structural_equality_p (tree args)
29703 {
29704 int i;
29705 int j;
29706
29707 if (!args)
29708 return false;
29709 if (args == error_mark_node)
29710 return true;
29711
29712 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
29713 {
29714 tree level = TMPL_ARGS_LEVEL (args, i + 1);
29715 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
29716 {
29717 tree arg = TREE_VEC_ELT (level, j);
29718 tree packed_args = NULL_TREE;
29719 int k, len = 1;
29720
29721 if (ARGUMENT_PACK_P (arg))
29722 {
29723 /* Look inside the argument pack. */
29724 packed_args = ARGUMENT_PACK_ARGS (arg);
29725 len = TREE_VEC_LENGTH (packed_args);
29726 }
29727
29728 for (k = 0; k < len; ++k)
29729 {
29730 if (packed_args)
29731 arg = TREE_VEC_ELT (packed_args, k);
29732
29733 if (error_operand_p (arg))
29734 return true;
29735 else if (TREE_CODE (arg) == TEMPLATE_DECL)
29736 continue;
29737 else if (arg == any_targ_node)
29738 /* An any_targ_node argument (added by add_defaults_to_ttp)
29739 makes the corresponding specialization not canonicalizable,
29740 since template_args_equal always return true for it. We
29741 may see this when called from bind_template_template_parm. */
29742 return true;
29743 /* Checking current_function_decl because this structural
29744 comparison is only necessary for redeclaration. */
29745 else if (!current_function_decl
29746 && dependent_template_arg_p (arg)
29747 && (cp_walk_tree_without_duplicates
29748 (&arg, find_parm_usage_r, NULL)))
29749 /* The identity of a class template specialization that uses
29750 a function parameter depends on the identity of the function.
29751 And if this specialization appeared in the trailing return
29752 type thereof, we don't know the identity of the function
29753 (e.g. if it's a redeclaration or a new function) until we
29754 form its signature and go through duplicate_decls. Thus
29755 it's unsafe to decide on a canonical type now (which depends
29756 on the DECL_CONTEXT of the function parameter, which can get
29757 mutated after the fact by duplicate_decls), so just require
29758 structural equality in this case (PR52830). */
29759 return true;
29760 else if (TYPE_P (arg)
29761 && TYPE_STRUCTURAL_EQUALITY_P (arg)
29762 && (dependent_alias_template_spec_p (arg, nt_opaque)
29763 || dependent_opaque_alias_p (arg)))
29764 /* Require structural equality for specializations written
29765 in terms of a dependent alias template specialization. */
29766 return true;
29767 else if (CLASS_TYPE_P (arg)
29768 && TYPE_TEMPLATE_INFO (arg)
29769 && TYPE_STRUCTURAL_EQUALITY_P (arg))
29770 /* Require structural equality for specializations written
29771 in terms of a class template specialization that itself
29772 needs structural equality. */
29773 return true;
29774 }
29775 }
29776 }
29777
29778 return false;
29779 }
29780
29781 /* Returns true if ARGS (a collection of template arguments) contains
29782 any dependent arguments. */
29783
29784 bool
29785 any_dependent_template_arguments_p (const_tree args)
29786 {
29787 if (args == error_mark_node)
29788 return true;
29789 if (!processing_template_decl || !args)
29790 return false;
29791
29792 for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
29793 {
29794 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29795 for (tree arg : tree_vec_range (CONST_CAST_TREE (level)))
29796 if (dependent_template_arg_p (arg))
29797 return true;
29798 }
29799
29800 return false;
29801 }
29802
29803 /* Returns true if ARGS contains any errors. */
29804
29805 bool
29806 any_erroneous_template_args_p (const_tree args)
29807 {
29808 int i;
29809 int j;
29810
29811 if (args == error_mark_node)
29812 return true;
29813
29814 if (args && TREE_CODE (args) != TREE_VEC)
29815 {
29816 if (tree ti = get_template_info (args))
29817 args = TI_ARGS (ti);
29818 else
29819 args = NULL_TREE;
29820 }
29821
29822 if (!args)
29823 return false;
29824
29825 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
29826 {
29827 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29828 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
29829 if (error_operand_p (TREE_VEC_ELT (level, j)))
29830 return true;
29831 }
29832
29833 return false;
29834 }
29835
29836 /* Returns TRUE if the template TMPL is type-dependent. */
29837
29838 bool
29839 dependent_template_p (tree tmpl)
29840 {
29841 if (TREE_CODE (tmpl) == OVERLOAD)
29842 {
29843 for (lkp_iterator iter (tmpl); iter; ++iter)
29844 if (dependent_template_p (*iter))
29845 return true;
29846 return false;
29847 }
29848
29849 /* Template template parameters are dependent. */
29850 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
29851 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
29852 return true;
29853 /* So are names that have not been looked up. */
29854 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
29855 return true;
29856 return false;
29857 }
29858
29859 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
29860
29861 bool
29862 dependent_template_id_p (tree tmpl, tree args)
29863 {
29864 return (dependent_template_p (tmpl)
29865 || any_dependent_template_arguments_p (args));
29866 }
29867
29868 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
29869 are dependent. BODY is the body to use for loop transforming
29870 constructs. */
29871
29872 bool
29873 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv, tree body)
29874 {
29875 int i, k;
29876
29877 if (!processing_template_decl)
29878 return false;
29879
29880 for (i = 0, k = 0; i < TREE_VEC_LENGTH (declv); i++)
29881 {
29882 tree decl = TREE_VEC_ELT (declv, i);
29883 tree init = TREE_VEC_ELT (initv, i);
29884 tree cond = TREE_VEC_ELT (condv, i);
29885 tree incr = TREE_VEC_ELT (incrv, i);
29886
29887 if (decl == NULL_TREE)
29888 {
29889 tree stmt = body;
29890 int j = c_omp_find_generated_loop (stmt, k++, cp_walk_subtrees);
29891 init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
29892 decl = TREE_OPERAND (init, 0);
29893 cond = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
29894 incr = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
29895 }
29896
29897 if (type_dependent_expression_p (decl)
29898 || TREE_CODE (decl) == SCOPE_REF)
29899 return true;
29900
29901 if (init && type_dependent_expression_p (init))
29902 return true;
29903
29904 if (cond == global_namespace)
29905 return true;
29906
29907 if (type_dependent_expression_p (cond))
29908 return true;
29909
29910 if (COMPARISON_CLASS_P (cond)
29911 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
29912 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
29913 return true;
29914
29915 if (TREE_CODE (incr) == MODOP_EXPR)
29916 {
29917 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
29918 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
29919 return true;
29920 }
29921 else if (type_dependent_expression_p (incr))
29922 return true;
29923 else if (TREE_CODE (incr) == MODIFY_EXPR)
29924 {
29925 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
29926 return true;
29927 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
29928 {
29929 tree t = TREE_OPERAND (incr, 1);
29930 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
29931 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
29932 return true;
29933
29934 /* If this loop has a class iterator with != comparison
29935 with increment other than i++/++i/i--/--i, make sure the
29936 increment is constant. */
29937 if (CLASS_TYPE_P (TREE_TYPE (decl))
29938 && TREE_CODE (cond) == NE_EXPR)
29939 {
29940 if (TREE_OPERAND (t, 0) == decl)
29941 t = TREE_OPERAND (t, 1);
29942 else
29943 t = TREE_OPERAND (t, 0);
29944 if (TREE_CODE (t) != INTEGER_CST)
29945 return true;
29946 }
29947 }
29948 }
29949 }
29950
29951 return false;
29952 }
29953
29954 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
29955 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
29956 no such TYPE can be found. Note that this function peers inside
29957 uninstantiated templates and therefore should be used only in
29958 extremely limited situations. ONLY_CURRENT_P restricts this
29959 peering to the currently open classes hierarchy (which is required
29960 when comparing types). */
29961
29962 tree
29963 resolve_typename_type (tree type, bool only_current_p)
29964 {
29965 tree scope;
29966 tree name;
29967 tree decl;
29968 int quals;
29969 tree pushed_scope;
29970 tree result;
29971
29972 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
29973
29974 scope = TYPE_CONTEXT (type);
29975 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
29976 gcc_checking_assert (uses_template_parms (scope));
29977
29978 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
29979 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
29980 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
29981 representing the typedef. In that case TYPE_IDENTIFIER (type) is
29982 not the non-qualified identifier of the TYPENAME_TYPE anymore.
29983 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
29984 the TYPENAME_TYPE instead, we avoid messing up with a possible
29985 typedef variant case. */
29986 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
29987
29988 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
29989 it first before we can figure out what NAME refers to. */
29990 if (TREE_CODE (scope) == TYPENAME_TYPE)
29991 {
29992 if (TYPENAME_IS_RESOLVING_P (scope))
29993 /* Given a class template A with a dependent base with nested type C,
29994 typedef typename A::C::C C will land us here, as trying to resolve
29995 the initial A::C leads to the local C typedef, which leads back to
29996 A::C::C. So we break the recursion now. */
29997 return type;
29998 else
29999 scope = resolve_typename_type (scope, only_current_p);
30000 }
30001 /* If we don't know what SCOPE refers to, then we cannot resolve the
30002 TYPENAME_TYPE. */
30003 if (!CLASS_TYPE_P (scope))
30004 return type;
30005 /* If this is a typedef, we don't want to look inside (c++/11987). */
30006 if (typedef_variant_p (type))
30007 return type;
30008 /* If SCOPE isn't the template itself, it will not have a valid
30009 TYPE_FIELDS list. */
30010 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
30011 /* scope is either the template itself or a compatible instantiation
30012 like X<T>, so look up the name in the original template. */
30013 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
30014 /* If scope has no fields, it can't be a current instantiation. Check this
30015 before currently_open_class to avoid infinite recursion (71515). */
30016 if (!TYPE_FIELDS (scope))
30017 return type;
30018 /* If the SCOPE is not the current instantiation, there's no reason
30019 to look inside it. */
30020 if (only_current_p && !currently_open_class (scope))
30021 return type;
30022 /* Enter the SCOPE so that name lookup will be resolved as if we
30023 were in the class definition. In particular, SCOPE will no
30024 longer be considered a dependent type. */
30025 pushed_scope = push_scope (scope);
30026 /* Look up the declaration. */
30027 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
30028 tf_warning_or_error);
30029
30030 result = NULL_TREE;
30031
30032 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
30033 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
30034 tree fullname = TYPENAME_TYPE_FULLNAME (type);
30035 if (!decl)
30036 /*nop*/;
30037 else if (identifier_p (fullname)
30038 && TREE_CODE (decl) == TYPE_DECL)
30039 {
30040 result = TREE_TYPE (decl);
30041 if (result == error_mark_node)
30042 result = NULL_TREE;
30043 }
30044 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
30045 && DECL_CLASS_TEMPLATE_P (decl))
30046 {
30047 /* Obtain the template and the arguments. */
30048 tree tmpl = TREE_OPERAND (fullname, 0);
30049 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
30050 {
30051 /* We get here with a plain identifier because a previous tentative
30052 parse of the nested-name-specifier as part of a ptr-operator saw
30053 ::template X<A>. The use of ::template is necessary in a
30054 ptr-operator, but wrong in a declarator-id.
30055
30056 [temp.names]: In a qualified-id of a declarator-id, the keyword
30057 template shall not appear at the top level. */
30058 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
30059 "keyword %<template%> not allowed in declarator-id");
30060 tmpl = decl;
30061 }
30062 tree args = TREE_OPERAND (fullname, 1);
30063 /* Instantiate the template. */
30064 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
30065 tf_error | tf_user);
30066 result = adjust_type_for_entering_scope (result);
30067 if (result == error_mark_node)
30068 result = NULL_TREE;
30069 }
30070
30071 /* Leave the SCOPE. */
30072 if (pushed_scope)
30073 pop_scope (pushed_scope);
30074
30075 /* If we failed to resolve it, return the original typename. */
30076 if (!result)
30077 return type;
30078
30079 /* If lookup found a typename type, resolve that too. */
30080 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
30081 {
30082 /* Ill-formed programs can cause infinite recursion here, so we
30083 must catch that. */
30084 TYPENAME_IS_RESOLVING_P (result) = 1;
30085 result = resolve_typename_type (result, only_current_p);
30086 TYPENAME_IS_RESOLVING_P (result) = 0;
30087 }
30088
30089 /* Qualify the resulting type. */
30090 quals = cp_type_quals (type);
30091 if (quals)
30092 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
30093
30094 return result;
30095 }
30096
30097 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
30098 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
30099 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
30100
30101 static tree
30102 make_auto_1 (tree name, bool set_canonical, int level = -1)
30103 {
30104 if (level == -1)
30105 level = current_template_depth + 1;
30106 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
30107 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
30108 TYPE_STUB_DECL (au) = TYPE_NAME (au);
30109 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
30110 (0, level, level, TYPE_NAME (au), NULL_TREE);
30111 if (set_canonical)
30112 TYPE_CANONICAL (au) = canonical_type_parameter (au);
30113 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
30114 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
30115 if (name == decltype_auto_identifier)
30116 AUTO_IS_DECLTYPE (au) = true;
30117
30118 return au;
30119 }
30120
30121 tree
30122 make_decltype_auto (void)
30123 {
30124 return make_auto_1 (decltype_auto_identifier, true);
30125 }
30126
30127 tree
30128 make_auto (void)
30129 {
30130 return make_auto_1 (auto_identifier, true);
30131 }
30132
30133 /* Return a C++17 deduction placeholder for class template TMPL.
30134 There are represented as an 'auto' with the special level 0 and
30135 CLASS_PLACEHOLDER_TEMPLATE set. */
30136
30137 tree
30138 make_template_placeholder (tree tmpl)
30139 {
30140 tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
30141 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
30142 /* Our canonical type depends on the placeholder. */
30143 TYPE_CANONICAL (t) = canonical_type_parameter (t);
30144 return t;
30145 }
30146
30147 /* True iff T is a C++17 class template deduction placeholder. */
30148
30149 bool
30150 template_placeholder_p (tree t)
30151 {
30152 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
30153 }
30154
30155 /* Return an auto for an explicit cast expression auto(x).
30156 Like CTAD placeholders, these have level 0 so that they're
30157 not accidentally replaced via tsubst and are always directly
30158 resolved via do_auto_deduction. */
30159
30160 tree
30161 make_cast_auto ()
30162 {
30163 return make_auto_1 (auto_identifier, true, /*level=*/0);
30164 }
30165
30166 /* Make a "constrained auto" type-specifier. This is an auto or
30167 decltype(auto) type with constraints that must be associated after
30168 deduction. The constraint is formed from the given concept CON
30169 and its optional sequence of template arguments ARGS.
30170
30171 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
30172
30173 static tree
30174 make_constrained_placeholder_type (tree type, tree con, tree args)
30175 {
30176 /* Build the constraint. */
30177 tree tmpl = DECL_TI_TEMPLATE (con);
30178 ++processing_template_decl;
30179 tree expr = build_concept_check (tmpl, type, args, tf_warning_or_error);
30180 --processing_template_decl;
30181
30182 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
30183 = build_tree_list (current_template_parms, expr);
30184
30185 /* Our canonical type depends on the constraint. */
30186 TYPE_CANONICAL (type) = canonical_type_parameter (type);
30187
30188 /* Attach the constraint to the type declaration. */
30189 return TYPE_NAME (type);
30190 }
30191
30192 /* Make a "constrained auto" type-specifier. */
30193
30194 tree
30195 make_constrained_auto (tree con, tree args)
30196 {
30197 tree type = make_auto_1 (auto_identifier, false);
30198 return make_constrained_placeholder_type (type, con, args);
30199 }
30200
30201 /* Make a "constrained decltype(auto)" type-specifier. */
30202
30203 tree
30204 make_constrained_decltype_auto (tree con, tree args)
30205 {
30206 tree type = make_auto_1 (decltype_auto_identifier, false);
30207 return make_constrained_placeholder_type (type, con, args);
30208 }
30209
30210 /* Create an "auto..." type-specifier. */
30211
30212 tree
30213 make_auto_pack ()
30214 {
30215 tree type = make_auto_1 (auto_identifier, false);
30216 TEMPLATE_TYPE_PARAMETER_PACK (type) = true;
30217 /* Our canonical type depends on being a pack. */
30218 TYPE_CANONICAL (type) = canonical_type_parameter (type);
30219 return type;
30220 }
30221
30222 /* Returns true if the placeholder type constraint T has any dependent
30223 (explicit) template arguments. */
30224
30225 static bool
30226 placeholder_type_constraint_dependent_p (tree t)
30227 {
30228 gcc_assert (concept_check_p (t));
30229 tree args = TREE_OPERAND (t, 1);
30230 tree first = TREE_VEC_ELT (args, 0);
30231 if (ARGUMENT_PACK_P (first))
30232 {
30233 args = expand_template_argument_pack (args);
30234 first = TREE_VEC_ELT (args, 0);
30235 }
30236 gcc_checking_assert (is_auto (first));
30237 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
30238 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
30239 return true;
30240 return false;
30241 }
30242
30243 /* Prepare and return a concept definition. */
30244
30245 tree
30246 start_concept_definition (cp_expr id)
30247 {
30248 gcc_assert (identifier_p (id));
30249 gcc_assert (processing_template_decl);
30250
30251 location_t loc = id.get_location();
30252
30253 /* A concept-definition shall not have associated constraints. */
30254 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
30255 {
30256 error_at (loc, "a concept cannot be constrained");
30257 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
30258 }
30259
30260 /* A concept-definition shall appear in namespace scope. Templates
30261 aren't allowed in block scope, so we only need to check for class
30262 scope. */
30263 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
30264 {
30265 error_at (loc, "concept %qE not in namespace scope", *id);
30266 return error_mark_node;
30267 }
30268
30269 if (current_template_depth > 1)
30270 {
30271 error_at (loc, "concept %qE has multiple template parameter lists", *id);
30272 return error_mark_node;
30273 }
30274
30275 /* Initially build the concept declaration; its type is bool. */
30276 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
30277 DECL_CONTEXT (decl) = current_scope ();
30278 TREE_PUBLIC (decl) = true;
30279
30280 return decl;
30281 }
30282
30283 /* Finish building a concept definition. Like other templates, the
30284 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
30285 the TEMPLATE_DECL. */
30286
30287 tree
30288 finish_concept_definition (tree decl, tree init, tree attrs)
30289 {
30290 DECL_INITIAL (decl) = init;
30291
30292 if (attrs)
30293 cplus_decl_attributes (&decl, attrs, 0);
30294
30295 set_originating_module (decl, false);
30296 check_module_decl_linkage (decl);
30297
30298 /* Push the enclosing template. */
30299 return push_template_decl (decl);
30300 }
30301
30302 /* Given type ARG, return std::initializer_list<ARG>. */
30303
30304 static tree
30305 listify (tree arg)
30306 {
30307 tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
30308
30309 if (std_init_list == error_mark_node
30310 || !DECL_CLASS_TEMPLATE_P (std_init_list))
30311 {
30312 gcc_rich_location richloc (input_location);
30313 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
30314 error_at (&richloc,
30315 "deducing from brace-enclosed initializer list"
30316 " requires %<#include <initializer_list>%>");
30317
30318 return error_mark_node;
30319 }
30320 tree argvec = make_tree_vec (1);
30321 TREE_VEC_ELT (argvec, 0) = arg;
30322
30323 return lookup_template_class (std_init_list, argvec, NULL_TREE,
30324 NULL_TREE, tf_warning_or_error);
30325 }
30326
30327 /* Replace auto in TYPE with std::initializer_list<auto>. */
30328
30329 static tree
30330 listify_autos (tree type, tree auto_node)
30331 {
30332 tree init_auto = listify (strip_top_quals (auto_node));
30333 tree argvec = make_tree_vec (1);
30334 TREE_VEC_ELT (argvec, 0) = init_auto;
30335 if (processing_template_decl)
30336 argvec = add_to_template_args (current_template_args (), argvec);
30337 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
30338 }
30339
30340 /* Hash traits for hashing possibly constrained 'auto'
30341 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
30342
30343 struct auto_hash : default_hash_traits<tree>
30344 {
30345 static inline hashval_t hash (tree);
30346 static inline bool equal (tree, tree);
30347 };
30348
30349 /* Hash the 'auto' T. */
30350
30351 inline hashval_t
30352 auto_hash::hash (tree t)
30353 {
30354 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
30355 /* Matching constrained-type-specifiers denote the same template
30356 parameter, so hash the constraint. */
30357 return iterative_hash_placeholder_constraint (c, 0);
30358 else
30359 /* But unconstrained autos are all separate, so just hash the pointer. */
30360 return iterative_hash_object (t, 0);
30361 }
30362
30363 /* Compare two 'auto's. */
30364
30365 inline bool
30366 auto_hash::equal (tree t1, tree t2)
30367 {
30368 if (t1 == t2)
30369 return true;
30370
30371 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
30372 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
30373
30374 /* Two unconstrained autos are distinct. */
30375 if (!c1 || !c2)
30376 return false;
30377
30378 return equivalent_placeholder_constraints (c1, c2);
30379 }
30380
30381 /* The stem for deduction guide names. */
30382 const char *const dguide_base = "__dguide_";
30383
30384 /* Return the name for a deduction guide for class template TMPL. */
30385
30386 tree
30387 dguide_name (tree tmpl)
30388 {
30389 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
30390 tree tname = TYPE_IDENTIFIER (type);
30391 char *buf = (char *) alloca (1 + strlen (dguide_base)
30392 + IDENTIFIER_LENGTH (tname));
30393 memcpy (buf, dguide_base, strlen (dguide_base));
30394 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
30395 IDENTIFIER_LENGTH (tname) + 1);
30396 tree dname = get_identifier (buf);
30397 TREE_TYPE (dname) = type;
30398 return dname;
30399 }
30400
30401 /* True if NAME is the name of a deduction guide. */
30402
30403 bool
30404 dguide_name_p (tree name)
30405 {
30406 return (TREE_CODE (name) == IDENTIFIER_NODE
30407 && TREE_TYPE (name)
30408 && startswith (IDENTIFIER_POINTER (name), dguide_base));
30409 }
30410
30411 /* True if FN is a deduction guide. */
30412
30413 bool
30414 deduction_guide_p (const_tree fn)
30415 {
30416 if (DECL_P (fn))
30417 if (tree name = DECL_NAME (fn))
30418 return dguide_name_p (name);
30419 return false;
30420 }
30421
30422 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
30423
30424 bool
30425 copy_guide_p (const_tree fn)
30426 {
30427 gcc_assert (deduction_guide_p (fn));
30428 if (!DECL_ARTIFICIAL (fn))
30429 return false;
30430 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
30431 return (TREE_CHAIN (parms) == void_list_node
30432 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
30433 }
30434
30435 /* True if FN is a guide generated from a constructor template. */
30436
30437 bool
30438 template_guide_p (const_tree fn)
30439 {
30440 gcc_assert (deduction_guide_p (fn));
30441 if (!DECL_ARTIFICIAL (fn))
30442 return false;
30443 tree tmpl = DECL_TI_TEMPLATE (fn);
30444 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
30445 return PRIMARY_TEMPLATE_P (org);
30446 return false;
30447 }
30448
30449 /* True if FN is an aggregate initialization guide or the copy deduction
30450 guide. */
30451
30452 bool
30453 builtin_guide_p (const_tree fn)
30454 {
30455 if (!deduction_guide_p (fn))
30456 return false;
30457 if (!DECL_ARTIFICIAL (fn))
30458 /* Explicitly declared. */
30459 return false;
30460 if (DECL_ABSTRACT_ORIGIN (fn))
30461 /* Derived from a constructor. */
30462 return false;
30463 return true;
30464 }
30465
30466 /* True if FN is a C++23 inherited guide. */
30467
30468 bool
30469 inherited_guide_p (const_tree fn)
30470 {
30471 gcc_assert (deduction_guide_p (fn));
30472 return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE;
30473 }
30474
30475 /* Set the base class BASE from which the transformed guide FN
30476 was inherited as part of C++23 inherited CTAD. */
30477
30478 static void
30479 set_inherited_guide_context (const_tree fn, tree base)
30480 {
30481 gcc_assert (deduction_guide_p (fn));
30482 LANG_DECL_FN_CHECK (fn)->context = base;
30483 }
30484
30485 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
30486 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
30487 template parameter types. Note that the handling of template template
30488 parameters relies on current_template_parms being set appropriately for the
30489 new template. */
30490
30491 static tree
30492 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
30493 tree tsubst_args, tsubst_flags_t complain)
30494 {
30495 if (olddecl == error_mark_node)
30496 return error_mark_node;
30497
30498 tree oldidx = get_template_parm_index (olddecl);
30499
30500 tree newtype;
30501 if (TREE_CODE (olddecl) == TYPE_DECL
30502 || TREE_CODE (olddecl) == TEMPLATE_DECL)
30503 {
30504 tree oldtype = TREE_TYPE (olddecl);
30505 newtype = cxx_make_type (TREE_CODE (oldtype));
30506 TYPE_MAIN_VARIANT (newtype) = newtype;
30507 }
30508 else
30509 {
30510 newtype = TREE_TYPE (olddecl);
30511 if (type_uses_auto (newtype))
30512 {
30513 // Substitute once to fix references to other template parameters.
30514 newtype = tsubst (newtype, tsubst_args,
30515 complain|tf_partial, NULL_TREE);
30516 // Now substitute again to reduce the level of the auto.
30517 newtype = tsubst (newtype, current_template_args (),
30518 complain, NULL_TREE);
30519 }
30520 else
30521 newtype = tsubst (newtype, tsubst_args,
30522 complain, NULL_TREE);
30523 }
30524
30525 tree newdecl
30526 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
30527 DECL_NAME (olddecl), newtype);
30528 SET_DECL_TEMPLATE_PARM_P (newdecl);
30529
30530 tree newidx;
30531 if (TREE_CODE (olddecl) == TYPE_DECL
30532 || TREE_CODE (olddecl) == TEMPLATE_DECL)
30533 {
30534 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
30535 = build_template_parm_index (index, level, level,
30536 newdecl, newtype);
30537 TEMPLATE_PARM_PARAMETER_PACK (newidx)
30538 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
30539 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
30540
30541 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
30542 {
30543 tree newresult
30544 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
30545 DECL_NAME (olddecl), newtype);
30546 DECL_ARTIFICIAL (newresult) = true;
30547 DECL_TEMPLATE_RESULT (newdecl) = newresult;
30548 // First create a copy (ttargs) of tsubst_args with an
30549 // additional level for the template template parameter's own
30550 // template parameters (ttparms).
30551 tree ttparms = (INNERMOST_TEMPLATE_PARMS
30552 (DECL_TEMPLATE_PARMS (olddecl)));
30553 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
30554 tree ttargs = make_tree_vec (depth + 1);
30555 for (int i = 0; i < depth; ++i)
30556 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
30557 TREE_VEC_ELT (ttargs, depth)
30558 = template_parms_level_to_args (ttparms);
30559 // Substitute ttargs into ttparms to fix references to
30560 // other template parameters.
30561 ttparms = tsubst_template_parms_level (ttparms, ttargs,
30562 complain|tf_partial);
30563 // Now substitute again with args based on tparms, to reduce
30564 // the level of the ttparms.
30565 ttargs = current_template_args ();
30566 ttparms = tsubst_template_parms_level (ttparms, ttargs,
30567 complain);
30568 // Finally, tack the adjusted parms onto tparms.
30569 ttparms = tree_cons (size_int (level + 1), ttparms,
30570 copy_node (current_template_parms));
30571 // As with all template template parms, the parameter list captured
30572 // by this template template parm that corresponds to its own level
30573 // should be empty. This avoids infinite recursion when structurally
30574 // comparing two such rewritten template template parms (PR102479).
30575 gcc_assert (!TREE_VEC_LENGTH
30576 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
30577 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
30578 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
30579 // All done.
30580 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
30581 DECL_TEMPLATE_INFO (newresult)
30582 = build_template_info (newdecl, template_parms_to_args (ttparms));
30583 }
30584
30585 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
30586 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
30587 else
30588 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
30589 }
30590 else
30591 {
30592 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
30593 tree newconst
30594 = build_decl (DECL_SOURCE_LOCATION (oldconst),
30595 TREE_CODE (oldconst),
30596 DECL_NAME (oldconst), newtype);
30597 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
30598 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
30599 SET_DECL_TEMPLATE_PARM_P (newconst);
30600 newidx = build_template_parm_index (index, level, level,
30601 newconst, newtype);
30602 TEMPLATE_PARM_PARAMETER_PACK (newidx)
30603 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
30604 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
30605 }
30606
30607 return newdecl;
30608 }
30609
30610 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
30611 template parameter. */
30612
30613 static tree
30614 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
30615 tree targs, unsigned targs_index, tsubst_flags_t complain)
30616 {
30617 tree olddecl = TREE_VALUE (oldelt);
30618 tree newdecl = rewrite_template_parm (olddecl, index, level,
30619 targs, complain);
30620 if (newdecl == error_mark_node)
30621 return error_mark_node;
30622 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
30623 targs, complain, NULL_TREE);
30624 tree list = build_tree_list (newdef, newdecl);
30625 TEMPLATE_PARM_CONSTRAINTS (list)
30626 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
30627 targs, complain, NULL_TREE);
30628 int depth = TMPL_ARGS_DEPTH (targs);
30629 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
30630 return list;
30631 }
30632
30633 /* Returns a C++17 class deduction guide template based on the constructor
30634 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
30635 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
30636 aggregate initialization guide. OUTER_ARGS are the template arguments
30637 for the enclosing scope of the class. */
30638
30639 static tree
30640 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
30641 {
30642 tree tparms, targs, fparms, fargs, ci;
30643 bool memtmpl = false;
30644 bool explicit_p;
30645 location_t loc;
30646 tree fn_tmpl = NULL_TREE;
30647
30648 if (outer_args)
30649 {
30650 ++processing_template_decl;
30651 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
30652 --processing_template_decl;
30653 }
30654
30655 if (!DECL_DECLARES_FUNCTION_P (ctor))
30656 {
30657 if (TYPE_P (ctor))
30658 {
30659 bool copy_p = TYPE_REF_P (ctor);
30660 if (copy_p)
30661 fparms = tree_cons (NULL_TREE, type, void_list_node);
30662 else
30663 fparms = void_list_node;
30664 }
30665 else if (TREE_CODE (ctor) == TREE_LIST)
30666 fparms = ctor;
30667 else
30668 gcc_unreachable ();
30669
30670 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
30671 tparms = DECL_TEMPLATE_PARMS (ctmpl);
30672 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
30673 ci = NULL_TREE;
30674 fargs = NULL_TREE;
30675 loc = DECL_SOURCE_LOCATION (ctmpl);
30676 explicit_p = false;
30677 }
30678 else
30679 {
30680 ++processing_template_decl;
30681 bool ok = true;
30682
30683 complain |= tf_dguide;
30684
30685 fn_tmpl
30686 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
30687 : DECL_TI_TEMPLATE (ctor));
30688 if (outer_args)
30689 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
30690 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
30691
30692 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
30693 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
30694 fully specialized args for the enclosing class. Strip those off, as
30695 the deduction guide won't have those template parameters. */
30696 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
30697 TMPL_PARMS_DEPTH (tparms));
30698 /* Discard the 'this' parameter. */
30699 fparms = FUNCTION_ARG_CHAIN (ctor);
30700 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
30701 ci = get_constraints (ctor);
30702 loc = DECL_SOURCE_LOCATION (ctor);
30703 explicit_p = DECL_NONCONVERTING_P (ctor);
30704
30705 if (PRIMARY_TEMPLATE_P (fn_tmpl))
30706 {
30707 memtmpl = true;
30708
30709 /* For a member template constructor, we need to flatten the two
30710 template parameter lists into one, and then adjust the function
30711 signature accordingly. This gets...complicated. */
30712 tree save_parms = current_template_parms;
30713
30714 /* For a member template we should have two levels of parms/args, one
30715 for the class and one for the constructor. We stripped
30716 specialized args for further enclosing classes above. */
30717 const int depth = 2;
30718 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
30719
30720 /* Template args for translating references to the two-level template
30721 parameters into references to the one-level template parameters we
30722 are creating. */
30723 tree tsubst_args = copy_node (targs);
30724 TMPL_ARGS_LEVEL (tsubst_args, depth)
30725 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
30726
30727 /* Template parms for the constructor template. */
30728 tree ftparms = TREE_VALUE (tparms);
30729 unsigned flen = TREE_VEC_LENGTH (ftparms);
30730 /* Template parms for the class template. */
30731 tparms = TREE_CHAIN (tparms);
30732 tree ctparms = TREE_VALUE (tparms);
30733 unsigned clen = TREE_VEC_LENGTH (ctparms);
30734 /* Template parms for the deduction guide start as a copy of the
30735 template parms for the class. We set current_template_parms for
30736 lookup_template_class_1. */
30737 current_template_parms = tparms = copy_node (tparms);
30738 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
30739 for (unsigned i = 0; i < clen; ++i)
30740 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
30741
30742 /* Now we need to rewrite the constructor parms to append them to the
30743 class parms. */
30744 for (unsigned i = 0; i < flen; ++i)
30745 {
30746 unsigned index = i + clen;
30747 unsigned level = 1;
30748 tree oldelt = TREE_VEC_ELT (ftparms, i);
30749 tree newelt
30750 = rewrite_tparm_list (oldelt, index, level,
30751 tsubst_args, i, complain);
30752 if (newelt == error_mark_node)
30753 ok = false;
30754 TREE_VEC_ELT (new_vec, index) = newelt;
30755 }
30756
30757 /* Now we have a final set of template parms to substitute into the
30758 function signature. */
30759 targs = template_parms_to_args (tparms);
30760 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
30761 complain, ctor);
30762 if (fparms == error_mark_node)
30763 ok = false;
30764 if (ci)
30765 {
30766 if (outer_args)
30767 /* FIXME: We'd like to avoid substituting outer template
30768 arguments into the constraint ahead of time, but the
30769 construction of tsubst_args assumes that outer arguments
30770 are already substituted in. */
30771 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30772 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
30773 }
30774
30775 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
30776 cp_unevaluated_operand. */
30777 cp_evaluated ev;
30778 fargs = tsubst (fargs, tsubst_args, complain, ctor);
30779 current_template_parms = save_parms;
30780 }
30781 else
30782 {
30783 /* Substitute in the same arguments to rewrite class members into
30784 references to members of an unknown specialization. */
30785 cp_evaluated ev;
30786 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
30787 if (fparms == error_mark_node)
30788 ok = false;
30789 fargs = tsubst (fargs, targs, complain, ctor);
30790 if (ci)
30791 {
30792 if (outer_args)
30793 /* FIXME: As above. */
30794 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30795 ci = tsubst_constraint_info (ci, targs, complain, ctor);
30796 }
30797 }
30798
30799 --processing_template_decl;
30800 if (!ok)
30801 return error_mark_node;
30802 }
30803
30804 if (!memtmpl)
30805 {
30806 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
30807 tparms = copy_node (tparms);
30808 INNERMOST_TEMPLATE_PARMS (tparms)
30809 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
30810 }
30811
30812 tree fntype = build_function_type (type, fparms);
30813 tree ded_fn = build_lang_decl_loc (loc,
30814 FUNCTION_DECL,
30815 dguide_name (type), fntype);
30816 DECL_ARGUMENTS (ded_fn) = fargs;
30817 DECL_ARTIFICIAL (ded_fn) = true;
30818 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
30819 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
30820 DECL_ARTIFICIAL (ded_tmpl) = true;
30821 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
30822 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
30823 if (DECL_P (ctor))
30824 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
30825 if (ci)
30826 set_constraints (ded_tmpl, ci);
30827
30828 return ded_tmpl;
30829 }
30830
30831 /* Add to LIST the member types for the reshaped initializer CTOR. */
30832
30833 static tree
30834 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
30835 {
30836 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
30837 tree idx, val; unsigned i;
30838 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
30839 {
30840 tree ftype = elt ? elt : TREE_TYPE (idx);
30841 if (BRACE_ENCLOSED_INITIALIZER_P (val)
30842 && CONSTRUCTOR_BRACES_ELIDED_P (val))
30843 {
30844 tree subelt = NULL_TREE;
30845 if (TREE_CODE (ftype) == ARRAY_TYPE)
30846 subelt = TREE_TYPE (ftype);
30847 list = collect_ctor_idx_types (val, list, subelt);
30848 continue;
30849 }
30850 tree arg = NULL_TREE;
30851 if (i == v->length() - 1
30852 && PACK_EXPANSION_P (ftype))
30853 /* Give the trailing pack expansion parameter a default argument to
30854 match aggregate initialization behavior, even if we deduce the
30855 length of the pack separately to more than we have initializers. */
30856 arg = build_constructor (init_list_type_node, NULL);
30857 /* if ei is of array type and xi is a braced-init-list or string literal,
30858 Ti is an rvalue reference to the declared type of ei */
30859 STRIP_ANY_LOCATION_WRAPPER (val);
30860 if (TREE_CODE (ftype) == ARRAY_TYPE
30861 && (BRACE_ENCLOSED_INITIALIZER_P (val)
30862 || TREE_CODE (val) == STRING_CST))
30863 {
30864 if (TREE_CODE (val) == STRING_CST)
30865 ftype = cp_build_qualified_type
30866 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
30867 ftype = (cp_build_reference_type
30868 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
30869 }
30870 list = tree_cons (arg, ftype, list);
30871 }
30872
30873 return list;
30874 }
30875
30876 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
30877
30878 static bool
30879 is_spec_or_derived (tree etype, tree tmpl)
30880 {
30881 if (!etype || !CLASS_TYPE_P (etype))
30882 return false;
30883
30884 etype = cv_unqualified (etype);
30885 tree type = TREE_TYPE (tmpl);
30886 tree tparms = (INNERMOST_TEMPLATE_PARMS
30887 (DECL_TEMPLATE_PARMS (tmpl)));
30888 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
30889 int err = unify (tparms, targs, type, etype,
30890 UNIFY_ALLOW_DERIVED, /*explain*/false);
30891 ggc_free (targs);
30892 return !err;
30893 }
30894
30895 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
30896 INIT. */
30897
30898 static tree
30899 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
30900 {
30901 if (cxx_dialect < cxx20)
30902 return NULL_TREE;
30903
30904 if (init == NULL_TREE)
30905 return NULL_TREE;
30906
30907 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30908 {
30909 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30910 tree tinfo = get_template_info (under);
30911 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
30912 return alias_ctad_tweaks (tmpl, guide);
30913 return NULL_TREE;
30914 }
30915
30916 /* We might be creating a guide for a class member template, e.g.,
30917
30918 template<typename U> struct A {
30919 template<typename T> struct B { T t; };
30920 };
30921
30922 At this point, A will have been instantiated. Below, we need to
30923 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
30924 const bool member_template_p
30925 = (DECL_TEMPLATE_INFO (tmpl)
30926 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
30927 tree type = TREE_TYPE (tmpl);
30928 tree template_type = (member_template_p
30929 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
30930 : type);
30931 if (!CP_AGGREGATE_TYPE_P (template_type))
30932 return NULL_TREE;
30933
30934 /* No aggregate candidate for copy-initialization. */
30935 if (args->length() == 1)
30936 {
30937 tree val = (*args)[0];
30938 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
30939 return NULL_TREE;
30940 }
30941
30942 /* If we encounter a problem, we just won't add the candidate. */
30943 tsubst_flags_t complain = tf_none;
30944
30945 tree parms = NULL_TREE;
30946 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30947 {
30948 init = reshape_init (template_type, init, complain);
30949 if (init == error_mark_node)
30950 return NULL_TREE;
30951 parms = collect_ctor_idx_types (init, parms);
30952 }
30953 else if (TREE_CODE (init) == TREE_LIST)
30954 {
30955 int len = list_length (init);
30956 for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type)))
30957 {
30958 if (!len)
30959 break;
30960 parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms);
30961 --len;
30962 }
30963 for (tree field = TYPE_FIELDS (template_type);
30964 len;
30965 --len, field = DECL_CHAIN (field))
30966 {
30967 field = next_aggregate_field (field);
30968 if (!field)
30969 return NULL_TREE;
30970 tree ftype = finish_decltype_type (field, true, complain);
30971 parms = tree_cons (NULL_TREE, ftype, parms);
30972 }
30973 }
30974 else
30975 /* Aggregate initialization doesn't apply to an initializer expression. */
30976 return NULL_TREE;
30977
30978 /* If we're creating a deduction guide for a member class template,
30979 we've used the original template pattern type for the reshape_init
30980 above; this is done because we want PARMS to be a template parameter
30981 type, something that can be deduced when used as a function template
30982 parameter. At this point the outer class template has already been
30983 partially instantiated (we deferred the deduction until the enclosing
30984 scope is non-dependent). Therefore we have to partially instantiate
30985 PARMS, so that its template level is properly reduced and we don't get
30986 mismatches when deducing types using the guide with PARMS. */
30987 if (member_template_p)
30988 {
30989 ++processing_template_decl;
30990 parms = tsubst (parms, outer_template_args (tmpl), complain, init);
30991 --processing_template_decl;
30992 }
30993
30994 if (parms)
30995 {
30996 tree last = parms;
30997 parms = nreverse (parms);
30998 TREE_CHAIN (last) = void_list_node;
30999 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
31000 return guide;
31001 }
31002
31003 return NULL_TREE;
31004 }
31005
31006 /* UGUIDES are the deduction guides for the underlying template of alias
31007 template TMPL; adjust them to be deduction guides for TMPL.
31008
31009 This routine also handles C++23 inherited CTAD, in which case TMPL is a
31010 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
31011 the template parameter list of the alias template (equivalently, of the
31012 derived class) and TREE_VALUE the defining-type-id (equivalently, the
31013 base whose guides we're inheriting). UGUIDES are the base's guides. */
31014
31015 static tree
31016 alias_ctad_tweaks (tree tmpl, tree uguides)
31017 {
31018 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
31019 class type (9.2.8.2) where the template-name names an alias template A,
31020 the defining-type-id of A must be of the form
31021
31022 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
31023
31024 as specified in 9.2.8.2. The guides of A are the set of functions or
31025 function templates formed as follows. For each function or function
31026 template f in the guides of the template named by the simple-template-id
31027 of the defining-type-id, the template arguments of the return type of f
31028 are deduced from the defining-type-id of A according to the process in
31029 13.10.2.5 with the exception that deduction does not fail if not all
31030 template arguments are deduced. Let g denote the result of substituting
31031 these deductions into f. If substitution succeeds, form a function or
31032 function template f' with the following properties and add it to the set
31033 of guides of A:
31034
31035 * The function type of f' is the function type of g.
31036
31037 * If f is a function template, f' is a function template whose template
31038 parameter list consists of all the template parameters of A (including
31039 their default template arguments) that appear in the above deductions or
31040 (recursively) in their default template arguments, followed by the
31041 template parameters of f that were not deduced (including their default
31042 template arguments), otherwise f' is not a function template.
31043
31044 * The associated constraints (13.5.2) are the conjunction of the
31045 associated constraints of g and a constraint that is satisfied if and only
31046 if the arguments of A are deducible (see below) from the return type.
31047
31048 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
31049 be so as well.
31050
31051 * If f was generated from a deduction-guide (12.4.1.8), then f' is
31052 considered to be so as well.
31053
31054 * The explicit-specifier of f' is the explicit-specifier of g (if
31055 any). */
31056
31057 enum { alias, inherited } ctad_kind;
31058 tree atype, fullatparms, utype, name;
31059 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
31060 {
31061 ctad_kind = alias;
31062 atype = TREE_TYPE (tmpl);
31063 fullatparms = DECL_TEMPLATE_PARMS (tmpl);
31064 utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
31065 name = dguide_name (tmpl);
31066 }
31067 else
31068 {
31069 ctad_kind = inherited;
31070 atype = NULL_TREE;
31071 fullatparms = TREE_PURPOSE (tmpl);
31072 utype = TREE_VALUE (tmpl);
31073 name = dguide_name (TPARMS_PRIMARY_TEMPLATE
31074 (INNERMOST_TEMPLATE_PARMS (fullatparms)));
31075 }
31076
31077 tsubst_flags_t complain = tf_partial;
31078 tree aguides = NULL_TREE;
31079 tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
31080 unsigned natparms = TREE_VEC_LENGTH (atparms);
31081 for (tree f : lkp_range (uguides))
31082 {
31083 tree in_decl = f;
31084 location_t loc = DECL_SOURCE_LOCATION (f);
31085 tree ret = TREE_TYPE (TREE_TYPE (f));
31086 tree fprime = f;
31087 if (TREE_CODE (f) == TEMPLATE_DECL)
31088 {
31089 processing_template_decl_sentinel ptds (/*reset*/false);
31090 ++processing_template_decl;
31091
31092 /* Deduce template arguments for f from the type-id of A. */
31093 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
31094 unsigned len = TREE_VEC_LENGTH (ftparms);
31095 tree targs = make_tree_vec (len);
31096 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
31097 if (err)
31098 /* CWG2664: Discard any deductions, still build the guide. */
31099 for (unsigned i = 0; i < len; ++i)
31100 TREE_VEC_ELT (targs, i) = NULL_TREE;
31101
31102 /* The number of parms for f' is the number of parms of A used in
31103 the deduced arguments plus non-deduced parms of f. */
31104 unsigned ndlen = 0;
31105 unsigned j;
31106 for (unsigned i = 0; i < len; ++i)
31107 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
31108 ++ndlen;
31109 find_template_parameter_info ftpi (fullatparms);
31110 ftpi.find_in_recursive (targs);
31111 unsigned nusedatparms = ftpi.num_found ();
31112 unsigned nfparms = nusedatparms + ndlen;
31113 tree gtparms = make_tree_vec (nfparms);
31114
31115 /* Set current_template_parms as in build_deduction_guide. */
31116 auto ctp = make_temp_override (current_template_parms);
31117 current_template_parms = copy_node (fullatparms);
31118 TREE_VALUE (current_template_parms) = gtparms;
31119
31120 j = 0;
31121 unsigned level = 1;
31122
31123 /* First copy over the used parms of A. */
31124 tree atargs = make_tree_vec (natparms);
31125 for (unsigned i = 0; i < natparms; ++i)
31126 {
31127 tree elt = TREE_VEC_ELT (atparms, i);
31128 if (ftpi.found (elt))
31129 {
31130 unsigned index = j++;
31131 tree nelt = rewrite_tparm_list (elt, index, level,
31132 atargs, i, complain);
31133 TREE_VEC_ELT (gtparms, index) = nelt;
31134 }
31135 }
31136 gcc_checking_assert (j == nusedatparms);
31137
31138 /* Adjust the deduced template args for f to refer to the A parms
31139 with their new indexes. */
31140 if (nusedatparms && nusedatparms != natparms)
31141 targs = tsubst_template_args (targs, atargs, complain, in_decl);
31142
31143 /* Now rewrite the non-deduced parms of f. */
31144 for (unsigned i = 0; ndlen && i < len; ++i)
31145 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
31146 {
31147 --ndlen;
31148 unsigned index = j++;
31149 tree oldlist = TREE_VEC_ELT (ftparms, i);
31150 tree list = rewrite_tparm_list (oldlist, index, level,
31151 targs, i, complain);
31152 TREE_VEC_ELT (gtparms, index) = list;
31153 }
31154 gtparms = build_tree_list (size_one_node, gtparms);
31155
31156 /* Substitute the deduced arguments plus the rewritten template
31157 parameters into f to get g. This covers the type, copyness,
31158 guideness, and explicit-specifier. */
31159 tree g;
31160 {
31161 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
31162 if cp_unevaluated_operand. */
31163 cp_evaluated ev;
31164 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
31165 /*use_spec_table=*/false);
31166 }
31167 if (g == error_mark_node)
31168 continue;
31169 DECL_NAME (g) = name;
31170 if (nfparms == 0)
31171 {
31172 /* The targs are all non-dependent, so g isn't a template. */
31173 fprime = g;
31174 ret = TREE_TYPE (TREE_TYPE (fprime));
31175 goto non_template;
31176 }
31177 DECL_USE_TEMPLATE (g) = 0;
31178 fprime = build_template_decl (g, gtparms, false);
31179 DECL_TEMPLATE_RESULT (fprime) = g;
31180 TREE_TYPE (fprime) = TREE_TYPE (g);
31181 tree gtargs = template_parms_to_args (gtparms);
31182 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
31183 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
31184
31185 /* Substitute the associated constraints. */
31186 tree ci = get_constraints (f);
31187 if (ci)
31188 {
31189 if (tree outer_targs = outer_template_args (f))
31190 ci = tsubst_constraint_info (ci, outer_targs,
31191 complain & ~tf_partial, in_decl);
31192 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
31193 }
31194 if (ci == error_mark_node)
31195 continue;
31196
31197 /* Add a constraint that the return type matches the instantiation of
31198 A with the same template arguments. */
31199 ret = TREE_TYPE (TREE_TYPE (fprime));
31200 if (ctad_kind == alias
31201 /* Use template_args_equal instead of same_type_p to get the
31202 comparing_dependent_aliases behavior. */
31203 && !template_args_equal (atype, ret))
31204 {
31205 tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
31206 ci = append_constraint (ci, same);
31207 }
31208
31209 if (ci)
31210 {
31211 remove_constraints (fprime);
31212 set_constraints (fprime, ci);
31213 }
31214 }
31215 else
31216 {
31217 /* For a non-template deduction guide, if the arguments of A aren't
31218 deducible from the return type, don't add the candidate. */
31219 non_template:
31220 if (ctad_kind == alias
31221 && !type_targs_deducible_from (tmpl, ret))
31222 continue;
31223 }
31224
31225 /* Rewrite the return type of the inherited guide in terms of the
31226 derived class. This is specified as replacing the return type R
31227 with typename CC<R>::type where the partially specialized CC maps a
31228 base class specialization to a specialization of the derived class
31229 having such a base (inducing substitution failure if no such derived
31230 class exists).
31231
31232 As specified this mapping would be done at instantiation time using
31233 non-dependent template arguments, but we do it ahead of time using
31234 the generic arguments. This seems to be good enough since generic
31235 deduction should succeed only if concrete deduction would. */
31236 if (ctad_kind == inherited)
31237 {
31238 processing_template_decl_sentinel ptds (/*reset*/false);
31239 if (TREE_CODE (fprime) == TEMPLATE_DECL)
31240 ++processing_template_decl;
31241
31242 tree targs = type_targs_deducible_from (tmpl, ret);
31243 if (!targs)
31244 continue;
31245
31246 if (TREE_CODE (f) != TEMPLATE_DECL)
31247 fprime = copy_decl (fprime);
31248 tree fntype = TREE_TYPE (fprime);
31249 ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
31250 in_decl, NULL_TREE, complain);
31251 fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype));
31252 TREE_TYPE (fprime) = fntype;
31253 if (TREE_CODE (fprime) == TEMPLATE_DECL)
31254 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
31255 set_inherited_guide_context (fprime, utype);
31256 }
31257
31258 aguides = lookup_add (fprime, aguides);
31259 }
31260
31261 return aguides;
31262 }
31263
31264 /* CTOR is a using-decl inheriting the constructors of some base of the class
31265 template TMPL; adjust the base's guides be deduction guides for TMPL. */
31266
31267 static tree
31268 inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
31269 {
31270 /* [over.match.class.deduct]: In addition, if C is defined and inherits
31271 constructors ([namespace.udecl]) from a direct base class denoted in the
31272 base-specifier-list by a class-or-decltype B, let A be an alias template
31273 whose template parameter list is that of C and whose defining-type-id is
31274 B. If A is a deducible template ([dcl.type.simple]), the set contains the
31275 guides of A with the return type R of each guide replaced with typename
31276 CC::type given a class template
31277
31278 template <typename> class CC;
31279
31280 whose primary template is not defined and with a single partial
31281 specialization whose template parameter list is that of A and whose
31282 template argument list is a specialization of A with the template argument
31283 list of A ([temp.dep.type]) having a member typedef type designating a
31284 template specialization with the template argument list of A but with C as
31285 the template. */
31286
31287 tree scope = USING_DECL_SCOPE (ctor);
31288 if (TREE_CODE (scope) == TYPENAME_TYPE
31289 && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope))
31290 == TYPENAME_TYPE_FULLNAME (scope)))
31291 /* Recognize using B<T>::B::B as an inherited constructor. */
31292 /* FIXME: Also recognize using C::B::B? We might have to call
31293 resolve_typename_type for that. */
31294 scope = TYPE_CONTEXT (scope);
31295 if (!CLASS_TYPE_P (scope)
31296 || !CLASSTYPE_TEMPLATE_INFO (scope)
31297 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
31298 return NULL_TREE;
31299
31300 tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
31301 bool any_dguides_p;
31302 tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
31303 any_dguides_p, complain);
31304 return alias_ctad_tweaks (t, uguides);
31305 }
31306
31307 /* If template arguments for TMPL can be deduced from TYPE, return
31308 the deduced arguments, otherwise return NULL_TREE.
31309 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
31310 [over.match.class.deduct].
31311
31312 This check is specified in terms of partial specialization, so the behavior
31313 should be parallel to that of get_partial_spec_bindings. */
31314
31315 tree
31316 type_targs_deducible_from (tree tmpl, tree type)
31317 {
31318 tree tparms, ttype;
31319 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
31320 {
31321 /* If tmpl is a class template, this is trivial: it's deducible if
31322 TYPE is a specialization of TMPL. */
31323 if (DECL_CLASS_TEMPLATE_P (tmpl))
31324 {
31325 if (CLASS_TYPE_P (type)
31326 && CLASSTYPE_TEMPLATE_INFO (type)
31327 && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
31328 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
31329 else
31330 return NULL_TREE;
31331 }
31332
31333 /* Otherwise it's an alias template. */
31334 tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
31335 ttype = TREE_TYPE (tmpl);
31336 }
31337 else
31338 {
31339 /* TMPL is a synthetic alias template represented as a TREE_LIST as
31340 per alias_ctad_tweaks. */
31341 tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
31342 ttype = TREE_VALUE (tmpl);
31343 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
31344 if (!ti)
31345 /* TTYPE is a typedef to a template-id. */
31346 ti = TYPE_TEMPLATE_INFO (ttype);
31347 tmpl = TI_TEMPLATE (ti);
31348 }
31349
31350 int len = TREE_VEC_LENGTH (tparms);
31351 tree targs = make_tree_vec (len);
31352 bool tried_array_deduction = (cxx_dialect < cxx17);
31353
31354 again:
31355 if (unify (tparms, targs, ttype, type,
31356 UNIFY_ALLOW_NONE, false))
31357 return NULL_TREE;
31358
31359 /* We don't fail on an undeduced targ the second time through (like
31360 get_partial_spec_bindings) because we're going to try defaults. */
31361 for (int i = 0; i < len; ++i)
31362 if (! TREE_VEC_ELT (targs, i))
31363 {
31364 tree tparm = TREE_VEC_ELT (tparms, i);
31365 tparm = TREE_VALUE (tparm);
31366
31367 if (!tried_array_deduction
31368 && TREE_CODE (tparm) == TYPE_DECL)
31369 {
31370 try_array_deduction (tparms, targs, ttype);
31371 tried_array_deduction = true;
31372 if (TREE_VEC_ELT (targs, i))
31373 goto again;
31374 }
31375 /* If the type parameter is a parameter pack, then it will be deduced
31376 to an empty parameter pack. This is another case that doesn't model
31377 well as partial specialization. */
31378 if (template_parameter_pack_p (tparm))
31379 {
31380 tree arg;
31381 if (TREE_CODE (tparm) == PARM_DECL)
31382 {
31383 arg = make_node (NONTYPE_ARGUMENT_PACK);
31384 TREE_CONSTANT (arg) = 1;
31385 }
31386 else
31387 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
31388 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
31389 TREE_VEC_ELT (targs, i) = arg;
31390 }
31391 }
31392
31393 /* Maybe add in default template args. This seems like a flaw in the
31394 specification in terms of partial specialization, since it says the
31395 partial specialization has the template parameter list of A, but a
31396 partial specialization can't have default targs. */
31397 targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
31398 if (targs == error_mark_node)
31399 return NULL_TREE;
31400
31401 /* I believe we don't need the template_template_parm_bindings_ok_p call
31402 because coerce_template_parms did coerce_template_template_parms. */
31403
31404 if (!constraints_satisfied_p (tmpl, targs))
31405 return NULL_TREE;
31406
31407 return targs;
31408 }
31409
31410 /* Return artificial deduction guides built from the constructors of class
31411 template TMPL. */
31412
31413 static tree
31414 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
31415 {
31416 tree outer_args = outer_template_args (tmpl);
31417 tree type = TREE_TYPE (most_general_template (tmpl));
31418
31419 tree cands = NULL_TREE;
31420
31421 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
31422 {
31423 /* We handle C++23 inherited CTAD below. */
31424 if (iter.using_p ())
31425 continue;
31426
31427 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
31428 cands = lookup_add (guide, cands);
31429 }
31430
31431 if (cxx_dialect >= cxx23)
31432 /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if
31433 e.g. the class also has a user-defined constructor. So instead iterate
31434 over TYPE_FIELDS manually to robustly find all relevant using-decls. */
31435 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
31436 if (TREE_CODE (field) == USING_DECL
31437 && DECL_NAME (field) == ctor_identifier)
31438 {
31439 tree uguides = inherited_ctad_tweaks (tmpl, field, complain);
31440 if (uguides)
31441 cands = lookup_add (uguides, cands);
31442 }
31443
31444 /* Add implicit default constructor deduction guide. */
31445 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
31446 {
31447 tree guide = build_deduction_guide (type, type, outer_args,
31448 complain);
31449 cands = lookup_add (guide, cands);
31450 }
31451
31452 /* Add copy guide. */
31453 {
31454 tree gtype = build_reference_type (type);
31455 tree guide = build_deduction_guide (type, gtype, outer_args,
31456 complain);
31457 cands = lookup_add (guide, cands);
31458 }
31459
31460 return cands;
31461 }
31462
31463 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
31464
31465 /* Return the non-aggregate deduction guides for deducible template TMPL. The
31466 aggregate candidate is added separately because it depends on the
31467 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
31468 guide. */
31469
31470 static tree
31471 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
31472 {
31473 tree guides = NULL_TREE;
31474 if (DECL_ALIAS_TEMPLATE_P (tmpl))
31475 {
31476 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
31477 tree tinfo = get_template_info (under);
31478 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
31479 complain);
31480 }
31481 else
31482 {
31483 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
31484 dguide_name (tmpl),
31485 LOOK_want::ANY_REACHABLE,
31486 /*complain=*/false);
31487 if (guides == error_mark_node)
31488 guides = NULL_TREE;
31489 else
31490 any_dguides_p = true;
31491 }
31492
31493 /* Cache the deduction guides for a template. We also remember the result of
31494 lookup, and rebuild everything if it changes; should be very rare. */
31495 /* FIXME: Also rebuild if this is a class template that inherits guides from a
31496 base class, and lookup for the latter changed. */
31497 tree_pair_p cache = NULL;
31498 if (tree_pair_p &r
31499 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
31500 {
31501 cache = r;
31502 if (cache->purpose == guides)
31503 return cache->value;
31504 }
31505 else
31506 {
31507 r = cache = ggc_cleared_alloc<tree_pair_s> ();
31508 cache->purpose = guides;
31509 }
31510
31511 tree cands = NULL_TREE;
31512 if (DECL_ALIAS_TEMPLATE_P (tmpl))
31513 cands = alias_ctad_tweaks (tmpl, guides);
31514 else
31515 {
31516 cands = ctor_deduction_guides_for (tmpl, complain);
31517 for (lkp_iterator it (guides); it; ++it)
31518 cands = lookup_add (*it, cands);
31519 }
31520
31521 cache->value = cands;
31522 return cands;
31523 }
31524
31525 /* Return whether TMPL is a (class template argument-) deducible template. */
31526
31527 bool
31528 ctad_template_p (tree tmpl)
31529 {
31530 /* A deducible template is either a class template or is an alias template
31531 whose defining-type-id is of the form
31532
31533 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
31534
31535 where the nested-name-specifier (if any) is non-dependent and the
31536 template-name of the simple-template-id names a deducible template. */
31537
31538 if (DECL_CLASS_TEMPLATE_P (tmpl)
31539 && IDENTIFIER_TRAIT_P (DECL_NAME (tmpl)))
31540 /* Don't consider CTAD for templates with the same name as a trait; that
31541 is ambiguous with e.g. __is_invocable(_Fn,_Args...). */
31542 return false;
31543 if (DECL_CLASS_TEMPLATE_P (tmpl)
31544 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31545 return true;
31546 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
31547 return false;
31548 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
31549 if (tree tinfo = get_template_info (orig))
31550 return ctad_template_p (TI_TEMPLATE (tinfo));
31551 return false;
31552 }
31553
31554 /* Deduce template arguments for the class template placeholder PTYPE for
31555 template TMPL based on the initializer INIT, and return the resulting
31556 type. */
31557
31558 static tree
31559 do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
31560 int flags, tsubst_flags_t complain)
31561 {
31562 /* We should have handled this in the caller. */
31563 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31564 return ptype;
31565
31566 /* If the class was erroneous, don't try to deduce, because that
31567 can generate a lot of diagnostic. */
31568 if (TREE_TYPE (tmpl)
31569 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
31570 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
31571 return ptype;
31572
31573 /* Wait until the enclosing scope is non-dependent. */
31574 if (DECL_CLASS_SCOPE_P (tmpl)
31575 && dependent_type_p (DECL_CONTEXT (tmpl)))
31576 return ptype;
31577
31578 /* Initializing one placeholder from another. */
31579 if (init
31580 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
31581 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
31582 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
31583 == TEMPLATE_PARM_INDEX)))
31584 && is_auto (TREE_TYPE (init))
31585 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
31586 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
31587
31588 if (!ctad_template_p (tmpl))
31589 {
31590 if (complain & tf_error)
31591 error ("non-deducible template %qT used without template arguments", tmpl);
31592 return error_mark_node;
31593 }
31594 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
31595 {
31596 if (complain & tf_error)
31597 {
31598 /* Be permissive with equivalent alias templates. */
31599 tree u = get_underlying_template (tmpl);
31600 auto_diagnostic_group d;
31601 diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
31602 bool complained
31603 = emit_diagnostic (dk, input_location, 0,
31604 "alias template deduction only available "
31605 "with %<-std=c++20%> or %<-std=gnu++20%>");
31606 if (u == tmpl)
31607 return error_mark_node;
31608 else if (complained)
31609 {
31610 inform (input_location, "use %qD directly instead", u);
31611 tmpl = u;
31612 }
31613 }
31614 else
31615 return error_mark_node;
31616 }
31617
31618 /* Wait until the initializer is non-dependent. */
31619 if (type_dependent_expression_p (init))
31620 return ptype;
31621
31622 if (outer_targs)
31623 {
31624 int args_depth = TMPL_ARGS_DEPTH (outer_targs);
31625 int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
31626 if (parms_depth > 1)
31627 {
31628 /* Substitute outer arguments into this CTAD template from the
31629 current instantiation. */
31630 int want = std::min (args_depth, parms_depth - 1);
31631 outer_targs = strip_innermost_template_args (outer_targs,
31632 args_depth - want);
31633 tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
31634 if (tmpl == error_mark_node)
31635 return error_mark_node;
31636 }
31637 }
31638
31639 /* Don't bother with the alias rules for an equivalent template. */
31640 tmpl = get_underlying_template (tmpl);
31641
31642 tree type = TREE_TYPE (tmpl);
31643
31644 bool try_list_cand = false;
31645 bool list_init_p = false;
31646
31647 releasing_vec rv_args = NULL;
31648 vec<tree,va_gc> *&args = *&rv_args;
31649 if (init == NULL_TREE)
31650 args = make_tree_vector ();
31651 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
31652 {
31653 list_init_p = true;
31654 try_list_cand = true;
31655 if (CONSTRUCTOR_NELTS (init) == 1
31656 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
31657 {
31658 /* As an exception, the first phase in 16.3.1.7 (considering the
31659 initializer list as a single argument) is omitted if the
31660 initializer list consists of a single expression of type cv U,
31661 where U is a specialization of C or a class derived from a
31662 specialization of C. */
31663 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
31664 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
31665 try_list_cand = false;
31666 }
31667 if (try_list_cand || is_std_init_list (type))
31668 args = make_tree_vector_single (init);
31669 else
31670 args = make_tree_vector_from_ctor (init);
31671 }
31672 else if (TREE_CODE (init) == TREE_LIST)
31673 args = make_tree_vector_from_list (init);
31674 else
31675 args = make_tree_vector_single (init);
31676
31677 /* Do this now to avoid problems with erroneous args later on. */
31678 args = resolve_args (args, complain);
31679 if (args == NULL)
31680 return error_mark_node;
31681
31682 bool any_dguides_p = false;
31683 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
31684 if (cands == error_mark_node)
31685 return error_mark_node;
31686
31687 /* Prune explicit deduction guides in copy-initialization context (but
31688 not copy-list-initialization). */
31689 bool elided = false;
31690 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
31691 {
31692 for (lkp_iterator iter (cands); !elided && iter; ++iter)
31693 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
31694 elided = true;
31695
31696 if (elided)
31697 {
31698 /* Found a nonconverting guide, prune the candidates. */
31699 tree pruned = NULL_TREE;
31700 for (lkp_iterator iter (cands); iter; ++iter)
31701 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
31702 pruned = lookup_add (*iter, pruned);
31703
31704 cands = pruned;
31705 }
31706 }
31707
31708 if (!any_dguides_p)
31709 if (tree guide = maybe_aggr_guide (tmpl, init, args))
31710 cands = lookup_add (guide, cands);
31711
31712 tree fndecl = error_mark_node;
31713
31714 /* If this is list-initialization and the class has a list guide, first
31715 try deducing from the list as a single argument, as [over.match.list]. */
31716 if (try_list_cand)
31717 {
31718 tree list_cands = NULL_TREE;
31719 for (tree dg : lkp_range (cands))
31720 if (is_list_ctor (dg))
31721 list_cands = lookup_add (dg, list_cands);
31722 if (list_cands)
31723 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
31724 if (fndecl == error_mark_node)
31725 {
31726 /* That didn't work, now try treating the list as a sequence of
31727 arguments. */
31728 release_tree_vector (args);
31729 args = make_tree_vector_from_ctor (init);
31730 args = resolve_args (args, complain);
31731 if (args == NULL)
31732 return error_mark_node;
31733 }
31734 }
31735
31736 if (elided && !cands)
31737 {
31738 error ("cannot deduce template arguments for copy-initialization"
31739 " of %qT, as it has no non-explicit deduction guides or "
31740 "user-declared constructors", type);
31741 return error_mark_node;
31742 }
31743 else if (!cands && fndecl == error_mark_node)
31744 {
31745 error ("cannot deduce template arguments of %qT, as it has no viable "
31746 "deduction guides", type);
31747 return error_mark_node;
31748 }
31749
31750 if (fndecl == error_mark_node)
31751 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
31752
31753 if (fndecl == error_mark_node)
31754 {
31755 if (complain & tf_warning_or_error)
31756 {
31757 auto_diagnostic_group d;
31758 error ("class template argument deduction failed:");
31759 perform_dguide_overload_resolution (cands, args, complain);
31760 if (elided)
31761 inform (input_location, "explicit deduction guides not considered "
31762 "for copy-initialization");
31763 }
31764 return error_mark_node;
31765 }
31766 /* [over.match.list]/1: In copy-list-initialization, if an explicit
31767 constructor is chosen, the initialization is ill-formed. */
31768 else if (flags & LOOKUP_ONLYCONVERTING)
31769 {
31770 if (DECL_NONCONVERTING_P (fndecl))
31771 {
31772 if (complain & tf_warning_or_error)
31773 {
31774 // TODO: Pass down location from cp_finish_decl.
31775 auto_diagnostic_group d;
31776 error ("class template argument deduction for %qT failed: "
31777 "explicit deduction guide selected in "
31778 "copy-list-initialization", type);
31779 inform (DECL_SOURCE_LOCATION (fndecl),
31780 "explicit deduction guide declared here");
31781
31782 }
31783 return error_mark_node;
31784 }
31785 }
31786
31787 /* If CTAD succeeded but the type doesn't have any explicit deduction
31788 guides, this deduction might not be what the user intended. */
31789 if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
31790 {
31791 auto_diagnostic_group d;
31792 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
31793 || global_dc->m_warn_system_headers)
31794 && warning (OPT_Wctad_maybe_unsupported,
31795 "%qT may not intend to support class template argument "
31796 "deduction", type))
31797 inform (input_location, "add a deduction guide to suppress this "
31798 "warning");
31799 }
31800
31801 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
31802 cp_type_quals (ptype));
31803 }
31804
31805 /* Return true if INIT is an unparenthesized id-expression or an
31806 unparenthesized class member access. Used for the argument of
31807 decltype(auto). */
31808
31809 bool
31810 unparenthesized_id_or_class_member_access_p (tree init)
31811 {
31812 STRIP_ANY_LOCATION_WRAPPER (init);
31813
31814 /* We need to be able to tell '(r)' and 'r' apart (when it's of
31815 reference type). Only the latter is an id-expression. */
31816 if (REFERENCE_REF_P (init)
31817 && !REF_PARENTHESIZED_P (init))
31818 init = TREE_OPERAND (init, 0);
31819 return (DECL_P (init)
31820 || ((TREE_CODE (init) == COMPONENT_REF
31821 || TREE_CODE (init) == SCOPE_REF)
31822 && !REF_PARENTHESIZED_P (init)));
31823 }
31824
31825 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
31826 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
31827 The CONTEXT determines the context in which auto deduction is performed
31828 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
31829
31830 OUTER_TARGS is used during template argument deduction (context == adc_unify)
31831 to properly substitute the result. It's also used in the adc_unify and
31832 adc_requirement contexts to communicate the necessary template arguments
31833 to satisfaction. OUTER_TARGS is ignored in other contexts.
31834
31835 Additionally for adc_unify contexts TMPL is the template for which TYPE
31836 is a template parameter type.
31837
31838 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
31839 scope may be appended to the list of deduced template arguments prior to
31840 determining constraint satisfaction as appropriate. */
31841
31842 tree
31843 do_auto_deduction (tree type, tree init, tree auto_node,
31844 tsubst_flags_t complain /* = tf_warning_or_error */,
31845 auto_deduction_context context /* = adc_unspecified */,
31846 tree outer_targs /* = NULL_TREE */,
31847 int flags /* = LOOKUP_NORMAL */,
31848 tree tmpl /* = NULL_TREE */)
31849 {
31850 if (type == error_mark_node || init == error_mark_node)
31851 return error_mark_node;
31852
31853 if (init && type_dependent_expression_p (init)
31854 && context != adc_unify)
31855 /* Defining a subset of type-dependent expressions that we can deduce
31856 from ahead of time isn't worth the trouble. */
31857 return type;
31858
31859 /* Similarly, we can't deduce from another undeduced decl. */
31860 if (init && undeduced_auto_decl (init))
31861 return type;
31862
31863 /* We may be doing a partial substitution, but we still want to replace
31864 auto_node. */
31865 complain &= ~tf_partial;
31866
31867 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
31868 {
31869 /* We don't recurse here because we can't deduce from a nested
31870 initializer_list. */
31871 if (CONSTRUCTOR_ELTS (init))
31872 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
31873 elt.value = resolve_nondeduced_context (elt.value, complain);
31874 }
31875 else if (init)
31876 init = resolve_nondeduced_context (init, complain);
31877
31878 /* In C++23, we must deduce the type to int&& for code like
31879 decltype(auto) f(int&& x) { return (x); }
31880 or
31881 auto&& f(int x) { return x; }
31882 so we use treat_lvalue_as_rvalue_p. But don't do it for
31883 decltype(auto) f(int x) { return x; }
31884 where we should deduce 'int' rather than 'int&&'; transmogrifying
31885 INIT to an rvalue would break that. */
31886 tree r;
31887 if (cxx_dialect >= cxx23
31888 && context == adc_return_type
31889 && (!AUTO_IS_DECLTYPE (auto_node)
31890 || !unparenthesized_id_or_class_member_access_p (init))
31891 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
31892 /*return*/true)))
31893 init = r;
31894
31895 if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
31896 /* C++17 class template argument deduction. */
31897 return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
31898
31899 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
31900 /* Nothing we can do with this, even in deduction context. */
31901 return type;
31902
31903 location_t loc = cp_expr_loc_or_input_loc (init);
31904
31905 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
31906 with either a new invented type template parameter U or, if the
31907 initializer is a braced-init-list (8.5.4), with
31908 std::initializer_list<U>. */
31909 if (BRACE_ENCLOSED_INITIALIZER_P (init))
31910 {
31911 if (!DIRECT_LIST_INIT_P (init))
31912 type = listify_autos (type, auto_node);
31913 else if (CONSTRUCTOR_NELTS (init) == 1)
31914 init = CONSTRUCTOR_ELT (init, 0)->value;
31915 else
31916 {
31917 if (complain & tf_warning_or_error)
31918 {
31919 auto_diagnostic_group d;
31920 if (permerror (loc, "direct-list-initialization of "
31921 "%<auto%> requires exactly one element"))
31922 inform (loc,
31923 "for deduction to %<std::initializer_list%>, use copy-"
31924 "list-initialization (i.e. add %<=%> before the %<{%>)");
31925 }
31926 type = listify_autos (type, auto_node);
31927 }
31928 }
31929
31930 if (type == error_mark_node || init == error_mark_node)
31931 return error_mark_node;
31932
31933 tree targs;
31934 if (context == adc_decomp_type
31935 && auto_node == type
31936 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
31937 {
31938 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
31939 and initializer has array type, deduce cv-qualified array type. */
31940 targs = make_tree_vec (1);
31941 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
31942 }
31943 else if (AUTO_IS_DECLTYPE (auto_node))
31944 {
31945 const bool id = unparenthesized_id_or_class_member_access_p (init);
31946 tree deduced = finish_decltype_type (init, id, complain);
31947 deduced = canonicalize_type_argument (deduced, complain);
31948 if (deduced == error_mark_node)
31949 return error_mark_node;
31950 targs = make_tree_vec (1);
31951 TREE_VEC_ELT (targs, 0) = deduced;
31952 }
31953 else
31954 {
31955 if (error_operand_p (init))
31956 return error_mark_node;
31957
31958 tree parms = build_tree_list (NULL_TREE, type);
31959 tree tparms = make_tree_vec (1);
31960 TREE_VEC_ELT (tparms, 0)
31961 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
31962
31963 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
31964 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
31965 DEDUCE_CALL,
31966 NULL, /*explain_p=*/false);
31967 if (val > 0)
31968 {
31969 if (processing_template_decl)
31970 /* Try again at instantiation time. */
31971 return type;
31972 if (type && type != error_mark_node
31973 && (complain & tf_error))
31974 /* If type is error_mark_node a diagnostic must have been
31975 emitted by now. Also, having a mention to '<type error>'
31976 in the diagnostic is not really useful to the user. */
31977 {
31978 if (cfun
31979 && FNDECL_USED_AUTO (current_function_decl)
31980 && (auto_node
31981 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
31982 && LAMBDA_FUNCTION_P (current_function_decl))
31983 error_at (loc, "unable to deduce lambda return type from %qE",
31984 init);
31985 else
31986 error_at (loc, "unable to deduce %qT from %qE", type, init);
31987 type_unification_real (tparms, targs, parms, &init, 1, 0,
31988 DEDUCE_CALL,
31989 NULL, /*explain_p=*/true);
31990 }
31991 return error_mark_node;
31992 }
31993 }
31994
31995 /* Check any placeholder constraints against the deduced type. */
31996 if (processing_template_decl && context == adc_unify)
31997 /* Constraints will be checked after deduction. */;
31998 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
31999 {
32000 if (processing_template_decl)
32001 {
32002 gcc_checking_assert (context == adc_variable_type
32003 || context == adc_return_type
32004 || context == adc_decomp_type);
32005 gcc_checking_assert (!type_dependent_expression_p (init));
32006 /* If the constraint is dependent, we need to wait until
32007 instantiation time to resolve the placeholder. */
32008 if (placeholder_type_constraint_dependent_p (constr))
32009 return type;
32010 }
32011
32012 if (context == adc_return_type
32013 || context == adc_variable_type
32014 || context == adc_decomp_type)
32015 if (tree fn = current_function_decl)
32016 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
32017 {
32018 outer_targs = DECL_TEMPLATE_INFO (fn)
32019 ? DECL_TI_ARGS (fn) : NULL_TREE;
32020 if (LAMBDA_FUNCTION_P (fn))
32021 {
32022 /* As in satisfy_declaration_constraints. */
32023 tree regen_args = lambda_regenerating_args (fn);
32024 if (outer_targs)
32025 outer_targs = add_to_template_args (regen_args, outer_targs);
32026 else
32027 outer_targs = regen_args;
32028 }
32029 }
32030
32031 tree full_targs = outer_targs;
32032 if (context == adc_unify && tmpl)
32033 full_targs = add_outermost_template_args (tmpl, full_targs);
32034 full_targs = add_to_template_args (full_targs, targs);
32035
32036 /* HACK: Compensate for callers not always communicating all levels of
32037 outer template arguments by filling in the outermost missing levels
32038 with dummy levels before checking satisfaction. We'll still crash
32039 if the constraint depends on a template argument belonging to one of
32040 these missing levels, but this hack otherwise allows us to handle a
32041 large subset of possible constraints (including all non-dependent
32042 constraints). */
32043 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
32044 - TMPL_ARGS_DEPTH (full_targs)))
32045 {
32046 tree dummy_levels = make_tree_vec (missing_levels);
32047 for (int i = 0; i < missing_levels; ++i)
32048 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
32049 full_targs = add_to_template_args (dummy_levels, full_targs);
32050 }
32051
32052 if (!constraints_satisfied_p (auto_node, full_targs))
32053 {
32054 if (complain & tf_warning_or_error)
32055 {
32056 auto_diagnostic_group d;
32057 switch (context)
32058 {
32059 case adc_unspecified:
32060 case adc_unify:
32061 error_at (loc, "placeholder constraints not satisfied");
32062 break;
32063 case adc_variable_type:
32064 case adc_decomp_type:
32065 error_at (loc, "deduced initializer does not satisfy "
32066 "placeholder constraints");
32067 break;
32068 case adc_return_type:
32069 error_at (loc, "deduced return type does not satisfy "
32070 "placeholder constraints");
32071 break;
32072 case adc_requirement:
32073 error_at (loc, "deduced expression type does not satisfy "
32074 "placeholder constraints");
32075 break;
32076 }
32077 diagnose_constraints (loc, auto_node, full_targs);
32078 }
32079 return error_mark_node;
32080 }
32081 }
32082
32083 if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
32084 {
32085 /* Substitute this level-less auto via tsubst by temporarily
32086 overriding its level to 1. */
32087 TEMPLATE_TYPE_LEVEL (auto_node) = 1;
32088 type = tsubst (type, targs, complain, NULL_TREE);
32089 TEMPLATE_TYPE_LEVEL (auto_node) = 0;
32090 return type;
32091 }
32092
32093 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
32094 /* The outer template arguments are already substituted into type
32095 (but we still may have used them for constraint checking above). */;
32096 else if (context == adc_unify)
32097 targs = add_to_template_args (outer_targs, targs);
32098 else if (processing_template_decl)
32099 targs = add_to_template_args (current_template_args (), targs);
32100 return tsubst (type, targs, complain, NULL_TREE);
32101 }
32102
32103 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
32104 result. */
32105
32106 tree
32107 splice_late_return_type (tree type, tree late_return_type)
32108 {
32109 if (late_return_type)
32110 {
32111 gcc_assert (is_auto (type) || seen_error ());
32112 return late_return_type;
32113 }
32114
32115 if (tree auto_node = find_type_usage (type, is_auto))
32116 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
32117 {
32118 /* In an abbreviated function template we didn't know we were dealing
32119 with a function template when we saw the auto return type, so rebuild
32120 the return type using an auto with the correct level. */
32121 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
32122 tree auto_vec = make_tree_vec (1);
32123 TREE_VEC_ELT (auto_vec, 0) = new_auto;
32124 tree targs = add_outermost_template_args (current_template_args (),
32125 auto_vec);
32126 /* Also rebuild the constraint info in terms of the new auto. */
32127 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
32128 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
32129 = build_tree_list (current_template_parms,
32130 tsubst_constraint (TREE_VALUE (ci), targs,
32131 tf_none, NULL_TREE));
32132 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
32133 return tsubst (type, targs, tf_none, NULL_TREE);
32134 }
32135 return type;
32136 }
32137
32138 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
32139 'decltype(auto)' or a deduced class template. */
32140
32141 bool
32142 is_auto (const_tree type)
32143 {
32144 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
32145 && (TYPE_IDENTIFIER (type) == auto_identifier
32146 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
32147 return true;
32148 else
32149 return false;
32150 }
32151
32152 /* for_each_template_parm callback for type_uses_auto. */
32153
32154 int
32155 is_auto_r (tree tp, void */*data*/)
32156 {
32157 return is_auto (tp);
32158 }
32159
32160 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
32161 a use of `auto'. Returns NULL_TREE otherwise. */
32162
32163 tree
32164 type_uses_auto (tree type)
32165 {
32166 if (type == NULL_TREE)
32167 return NULL_TREE;
32168
32169 /* For parameter packs, check the contents of the pack. */
32170 if (PACK_EXPANSION_P (type))
32171 type = PACK_EXPANSION_PATTERN (type);
32172
32173 return find_type_usage (type, is_auto);
32174 }
32175
32176 /* Recursively walk over && expressions searching for EXPR. Return a reference
32177 to that expression. */
32178
32179 static tree *find_template_requirement (tree *t, tree key)
32180 {
32181 if (*t == key)
32182 return t;
32183 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
32184 {
32185 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
32186 return p;
32187 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
32188 return p;
32189 }
32190 return 0;
32191 }
32192
32193 /* Convert the generic type parameters in PARM that match the types given in the
32194 range [START_IDX, END_IDX) from the current_template_parms into generic type
32195 packs. */
32196
32197 tree
32198 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
32199 {
32200 tree current = current_template_parms;
32201 int depth = TMPL_PARMS_DEPTH (current);
32202 current = INNERMOST_TEMPLATE_PARMS (current);
32203 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
32204
32205 for (int i = 0; i < start_idx; ++i)
32206 TREE_VEC_ELT (replacement, i)
32207 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
32208
32209 for (int i = start_idx; i < end_idx; ++i)
32210 {
32211 /* Create a distinct parameter pack type from the current parm and add it
32212 to the replacement args to tsubst below into the generic function
32213 parameter. */
32214 tree node = TREE_VEC_ELT (current, i);
32215 tree o = TREE_TYPE (TREE_VALUE (node));
32216 tree t = copy_type (o);
32217 TEMPLATE_TYPE_PARM_INDEX (t)
32218 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
32219 t, 0, 0, tf_none);
32220 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
32221 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
32222 TYPE_MAIN_VARIANT (t) = t;
32223 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
32224 TYPE_CANONICAL (t) = canonical_type_parameter (t);
32225 TREE_VEC_ELT (replacement, i) = t;
32226
32227 /* Replace the current template parameter with new pack. */
32228 TREE_VALUE (node) = TREE_CHAIN (t);
32229
32230 /* Surgically adjust the associated constraint of adjusted parameter
32231 and it's corresponding contribution to the current template
32232 requirements. */
32233 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
32234 {
32235 gcc_assert (concept_check_p (constr));
32236 TREE_VEC_ELT (TREE_OPERAND (constr, 1), 0) = t;
32237 /* Use UNKNOWN_LOCATION so write_template_args can tell the
32238 difference between this and a fold the user wrote. */
32239 location_t loc = UNKNOWN_LOCATION;
32240 tree fold = finish_left_unary_fold_expr (loc, constr,
32241 TRUTH_ANDIF_EXPR);
32242 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
32243
32244 /* If there was a constraint, we also need to replace that in
32245 the template requirements, which we've already built. */
32246 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
32247 reqs = find_template_requirement (reqs, constr);
32248 *reqs = fold;
32249 }
32250 }
32251
32252 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
32253 TREE_VEC_ELT (replacement, i)
32254 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
32255
32256 /* If there are more levels then build up the replacement with the outer
32257 template parms. */
32258 if (depth > 1)
32259 replacement = add_to_template_args (template_parms_to_args
32260 (TREE_CHAIN (current_template_parms)),
32261 replacement);
32262
32263 return tsubst (parm, replacement, tf_none, NULL_TREE);
32264 }
32265
32266 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
32267 0..N-1. */
32268
32269 void
32270 declare_integer_pack (void)
32271 {
32272 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
32273 build_function_type_list (integer_type_node,
32274 integer_type_node,
32275 NULL_TREE),
32276 NULL_TREE, ECF_CONST);
32277 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
32278 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
32279 CP_BUILT_IN_INTEGER_PACK);
32280 }
32281
32282 /* Walk the decl or type specialization table calling FN on each
32283 entry. */
32284
32285 void
32286 walk_specializations (bool decls_p,
32287 void (*fn) (bool decls_p, spec_entry *entry, void *data),
32288 void *data)
32289 {
32290 spec_hash_table *table = decls_p ? decl_specializations
32291 : type_specializations;
32292 spec_hash_table::iterator end (table->end ());
32293 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
32294 fn (decls_p, *iter, data);
32295 }
32296
32297 /* Lookup the specialization of *ELT, in the decl or type
32298 specialization table. Return the SPEC that's already there, or
32299 NULL if nothing. */
32300
32301 tree
32302 match_mergeable_specialization (bool decl_p, spec_entry *elt)
32303 {
32304 hash_table<spec_hasher> *specializations
32305 = decl_p ? decl_specializations : type_specializations;
32306 auto *slot = specializations->find_slot (elt, NO_INSERT);
32307
32308 if (slot)
32309 return (*slot)->spec;
32310
32311 return NULL_TREE;
32312 }
32313
32314 /* Return flags encoding whether SPEC is on the instantiation and/or
32315 specialization lists of TMPL. */
32316
32317 unsigned
32318 get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl)
32319 {
32320 unsigned flags = 0;
32321
32322 tree spec = decl_p ? decl : TREE_TYPE (decl);
32323 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
32324 inst; inst = TREE_CHAIN (inst))
32325 if (TREE_VALUE (inst) == spec)
32326 {
32327 flags |= 1;
32328 break;
32329 }
32330
32331 if (CLASS_TYPE_P (TREE_TYPE (decl))
32332 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
32333 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
32334 /* Only need to search if DECL is a partial specialization. */
32335 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
32336 part; part = TREE_CHAIN (part))
32337 if (TREE_VALUE (part) == decl)
32338 {
32339 flags |= 2;
32340 break;
32341 }
32342
32343 return flags;
32344 }
32345
32346 /* Add a new specialization described by SPEC. DECL is the
32347 maybe-template decl and FLAGS is as returned from
32348 get_mergeable_specialization_flags. */
32349
32350 void
32351 add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
32352 unsigned flags)
32353 {
32354 if (decl_p)
32355 {
32356 auto *slot = decl_specializations->find_slot (elt, INSERT);
32357
32358 gcc_checking_assert (!*slot);
32359 auto entry = ggc_alloc<spec_entry> ();
32360 *entry = *elt;
32361 *slot = entry;
32362 }
32363 else
32364 {
32365 auto *slot = type_specializations->find_slot (elt, INSERT);
32366
32367 /* We don't distinguish different constrained partial type
32368 specializations, so there could be duplicates. In that case we
32369 must propagate TYPE_CANONICAL so that they are treated as the
32370 same type. Everything else must be new. */
32371 if (*slot)
32372 {
32373 gcc_checking_assert (flags & 2);
32374 TYPE_CANONICAL (elt->spec) = TYPE_CANONICAL ((*slot)->spec);
32375 }
32376 else
32377 {
32378 auto entry = ggc_alloc<spec_entry> ();
32379 *entry = *elt;
32380 *slot = entry;
32381 }
32382 }
32383
32384 if (flags & 1)
32385 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
32386 = tree_cons (elt->args, elt->spec,
32387 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
32388
32389 if (flags & 2)
32390 {
32391 /* A partial specialization. */
32392 tree cons = tree_cons (elt->args, decl,
32393 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
32394 TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
32395 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
32396 set_defining_module_for_partial_spec (STRIP_TEMPLATE (decl));
32397 }
32398 }
32399
32400 /* Set up the hash tables for template instantiations. */
32401
32402 void
32403 init_template_processing (void)
32404 {
32405 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
32406 type_specializations = hash_table<spec_hasher>::create_ggc (37);
32407
32408 if (cxx_dialect >= cxx11)
32409 declare_integer_pack ();
32410 }
32411
32412 /* Print stats about the template hash tables for -fstats. */
32413
32414 void
32415 print_template_statistics (void)
32416 {
32417 fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
32418 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
32419 (fmt_size_t) decl_specializations->size (),
32420 (fmt_size_t) decl_specializations->elements (),
32421 decl_specializations->collisions ());
32422 fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
32423 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
32424 (fmt_size_t) type_specializations->size (),
32425 (fmt_size_t) type_specializations->elements (),
32426 type_specializations->collisions ());
32427 }
32428
32429 #if CHECKING_P
32430
32431 namespace selftest {
32432
32433 /* Verify that type_dependent_expression_p () works correctly, even
32434 in the presence of location wrapper nodes. */
32435
32436 static void
32437 test_type_dependent_expression_p ()
32438 {
32439 location_t loc = BUILTINS_LOCATION;
32440
32441 tree name = get_identifier ("foo");
32442
32443 /* If no templates are involved, nothing is type-dependent. */
32444 gcc_assert (!processing_template_decl);
32445 ASSERT_FALSE (type_dependent_expression_p (name));
32446
32447 ++processing_template_decl;
32448
32449 /* Within a template, an unresolved name is always type-dependent. */
32450 ASSERT_TRUE (type_dependent_expression_p (name));
32451
32452 /* Ensure it copes with NULL_TREE and errors. */
32453 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
32454 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
32455
32456 /* A USING_DECL in a template should be type-dependent, even if wrapped
32457 with a location wrapper (PR c++/83799). */
32458 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
32459 TREE_TYPE (using_decl) = integer_type_node;
32460 ASSERT_TRUE (type_dependent_expression_p (using_decl));
32461 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
32462 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
32463 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
32464
32465 --processing_template_decl;
32466 }
32467
32468 /* Run all of the selftests within this file. */
32469
32470 void
32471 cp_pt_cc_tests ()
32472 {
32473 test_type_dependent_expression_p ();
32474 }
32475
32476 } // namespace selftest
32477
32478 #endif /* #if CHECKING_P */
32479
32480 #include "gt-cp-pt.h"