]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.cc
c++: templated substitution into lambda-expr [PR114393]
[gcc.git] / gcc / cp / pt.cc
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
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
52 /* The type of functions taking a tree, and some additional data, and
53 returning an int. */
54 typedef int (*tree_fn_t) (tree, void*);
55
56 /* The PENDING_TEMPLATES is a list of templates whose instantiations
57 have been deferred, either because their definitions were not yet
58 available, or because we were putting off doing the work. */
59 struct GTY ((chain_next ("%h.next"))) pending_template
60 {
61 struct pending_template *next;
62 struct tinst_level *tinst;
63 };
64
65 static GTY(()) struct pending_template *pending_templates;
66 static GTY(()) struct pending_template *last_pending_template;
67
68 int processing_template_parmlist;
69 static int template_header_count;
70
71 static vec<int> inline_parm_levels;
72
73 static GTY(()) struct tinst_level *current_tinst_level;
74
75 static GTY(()) vec<tree, va_gc> *saved_access_scope;
76
77 /* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80 static tree cur_stmt_expr;
81
82 // -------------------------------------------------------------------------- //
83 // Local Specialization Stack
84 //
85 // Implementation of the RAII helper for creating new local
86 // specializations.
87 local_specialization_stack::local_specialization_stack (lss_policy policy)
88 : saved (local_specializations)
89 {
90 if (policy == lss_nop)
91 ;
92 else if (policy == lss_blank || !saved)
93 local_specializations = new hash_map<tree, tree>;
94 else
95 local_specializations = new hash_map<tree, tree>(*saved);
96 }
97
98 local_specialization_stack::~local_specialization_stack ()
99 {
100 if (local_specializations != saved)
101 {
102 delete local_specializations;
103 local_specializations = saved;
104 }
105 }
106
107 /* True if we've recursed into fn_type_unification too many times. */
108 static bool excessive_deduction_depth;
109
110 struct spec_hasher : ggc_ptr_hash<spec_entry>
111 {
112 static hashval_t hash (tree, tree);
113 static hashval_t hash (spec_entry *);
114 static bool equal (spec_entry *, spec_entry *);
115 };
116
117 /* The general template is not in these tables. */
118 typedef hash_table<spec_hasher> spec_hash_table;
119 static GTY (()) spec_hash_table *decl_specializations;
120 static GTY (()) spec_hash_table *type_specializations;
121
122 /* Contains canonical template parameter types. The vector is indexed by
123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
124 TREE_LIST, whose TREE_VALUEs contain the canonical template
125 parameters of various types and levels. */
126 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
127
128 #define UNIFY_ALLOW_NONE 0
129 #define UNIFY_ALLOW_MORE_CV_QUAL 1
130 #define UNIFY_ALLOW_LESS_CV_QUAL 2
131 #define UNIFY_ALLOW_DERIVED 4
132 #define UNIFY_ALLOW_INTEGER 8
133 #define UNIFY_ALLOW_OUTER_LEVEL 16
134 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
135 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
136
137 enum template_base_result {
138 tbr_incomplete_type,
139 tbr_ambiguous_baseclass,
140 tbr_success
141 };
142
143 static bool resolve_overloaded_unification (tree, tree, tree, tree,
144 unification_kind_t, int,
145 bool);
146 static int try_one_overload (tree, tree, tree, tree, tree,
147 unification_kind_t, int, bool, bool);
148 static int unify (tree, tree, tree, tree, int, bool);
149 static void add_pending_template (tree);
150 static tree reopen_tinst_level (struct tinst_level *);
151 static tree tsubst_initializer_list (tree, tree);
152 static tree get_partial_spec_bindings (tree, tree, tree);
153 static void tsubst_enum (tree, tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
156 struct conversion **, bool, bool);
157 static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
158 tree*, tree*, tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187 static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
188 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
189 static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int);
190 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
192 static bool check_specialization_scope (void);
193 static tree process_partial_specialization (tree);
194 static enum template_base_result get_template_base (tree, tree, tree, tree,
195 bool , tree *);
196 static tree try_class_unification (tree, tree, tree, tree, bool);
197 static bool class_nttp_const_wrapper_p (tree t);
198 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
199 tree, tree);
200 static bool template_template_parm_bindings_ok_p (tree, tree);
201 static void tsubst_default_arguments (tree, tsubst_flags_t);
202 static tree for_each_template_parm_r (tree *, int *, void *);
203 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
204 static void copy_default_args_to_explicit_spec (tree);
205 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
206 static bool dependent_template_arg_p (tree);
207 static bool dependent_type_p_r (tree);
208 static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
210 static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_name (tree, tree, tsubst_flags_t, tree);
212 static void perform_instantiation_time_access_checks (tree, tree);
213 static tree listify (tree);
214 static tree listify_autos (tree, tree);
215 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
216 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
217 static tree get_underlying_template (tree);
218 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
219 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
220 static tree make_argument_pack (tree);
221 static tree enclosing_instantiation_of (tree tctx);
222 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
223 static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
224 static void mark_template_arguments_used (tree, tree);
225 static bool uses_outer_template_parms (tree);
226 static tree alias_ctad_tweaks (tree, tree);
227 static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
228 static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 for a class or alias template (needed by instantiate_decl). */
234
235 void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_IMPLICIT_TYPEDEF_P (t)
244 && CLASS_TYPE_P (TREE_TYPE (t)))
245 push_nested_class (TREE_TYPE (t));
246 else if (DECL_CLASS_SCOPE_P (t))
247 push_nested_class (DECL_CONTEXT (t));
248 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
249 /* An artificial deduction guide should have the same access as
250 the constructor. */
251 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
252 else
253 push_to_top_level ();
254
255 if (TREE_CODE (t) == FUNCTION_DECL)
256 {
257 vec_safe_push (saved_access_scope, current_function_decl);
258 current_function_decl = t;
259 }
260 }
261
262 /* Restore the scope set up by push_access_scope. T is the node we
263 are processing. */
264
265 void
266 pop_access_scope (tree t)
267 {
268 if (TREE_CODE (t) == FUNCTION_DECL)
269 current_function_decl = saved_access_scope->pop();
270
271 if (DECL_FRIEND_CONTEXT (t)
272 || (DECL_IMPLICIT_TYPEDEF_P (t)
273 && CLASS_TYPE_P (TREE_TYPE (t)))
274 || DECL_CLASS_SCOPE_P (t)
275 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
276 pop_nested_class ();
277 else
278 pop_from_top_level ();
279 }
280
281 /* Do any processing required when DECL (a member template
282 declaration) is finished. Returns the TEMPLATE_DECL corresponding
283 to DECL, unless it is a specialization, in which case the DECL
284 itself is returned. */
285
286 tree
287 finish_member_template_decl (tree decl)
288 {
289 if (decl == error_mark_node)
290 return error_mark_node;
291
292 gcc_assert (DECL_P (decl));
293
294 if (TREE_CODE (decl) == TYPE_DECL)
295 {
296 tree type;
297
298 type = TREE_TYPE (decl);
299 if (type == error_mark_node)
300 return error_mark_node;
301 if (MAYBE_CLASS_TYPE_P (type)
302 && CLASSTYPE_TEMPLATE_INFO (type)
303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
304 {
305 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
306 check_member_template (tmpl);
307 return tmpl;
308 }
309 return NULL_TREE;
310 }
311 else if (TREE_CODE (decl) == FIELD_DECL)
312 error_at (DECL_SOURCE_LOCATION (decl),
313 "data member %qD cannot be a member template", decl);
314 else if (DECL_TEMPLATE_INFO (decl))
315 {
316 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
317 {
318 check_member_template (DECL_TI_TEMPLATE (decl));
319 return DECL_TI_TEMPLATE (decl);
320 }
321 else
322 return NULL_TREE;
323 }
324 else
325 error_at (DECL_SOURCE_LOCATION (decl),
326 "invalid member template declaration %qD", decl);
327
328 return error_mark_node;
329 }
330
331 /* Create a template info node. */
332
333 tree
334 build_template_info (tree template_decl, tree template_args)
335 {
336 tree result = make_node (TEMPLATE_INFO);
337 TI_TEMPLATE (result) = template_decl;
338 TI_ARGS (result) = template_args;
339 return result;
340 }
341
342 /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */
343
344 static tree
345 decl_template_info (const_tree decl)
346 {
347 /* This needs to match template_info_decl_check. */
348 if (DECL_LANG_SPECIFIC (decl))
349 switch (TREE_CODE (decl))
350 {
351 case FUNCTION_DECL:
352 if (DECL_THUNK_P (decl))
353 break;
354 gcc_fallthrough ();
355 case VAR_DECL:
356 case FIELD_DECL:
357 case TYPE_DECL:
358 case CONCEPT_DECL:
359 case TEMPLATE_DECL:
360 return DECL_TEMPLATE_INFO (decl);
361
362 default:
363 break;
364 }
365 return NULL_TREE;
366 }
367
368 /* Return the template info node corresponding to T, whatever T is. */
369
370 tree
371 get_template_info (const_tree t)
372 {
373 tree tinfo = NULL_TREE;
374
375 if (!t || t == error_mark_node)
376 return NULL;
377
378 if (TREE_CODE (t) == NAMESPACE_DECL
379 || TREE_CODE (t) == PARM_DECL)
380 return NULL;
381
382 if (DECL_P (t))
383 tinfo = decl_template_info (t);
384
385 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
386 t = TREE_TYPE (t);
387
388 if (OVERLOAD_TYPE_P (t))
389 tinfo = TYPE_TEMPLATE_INFO (t);
390 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
391 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
392
393 return tinfo;
394 }
395
396 /* Returns the template nesting level of the indicated class TYPE.
397
398 For example, in:
399 template <class T>
400 struct A
401 {
402 template <class U>
403 struct B {};
404 };
405
406 A<T>::B<U> has depth two, while A<T> has depth one.
407 Both A<T>::B<int> and A<int>::B<U> have depth one, if
408 they are instantiations, not specializations.
409
410 This function is guaranteed to return 0 if passed NULL_TREE so
411 that, for example, `template_class_depth (current_class_type)' is
412 always safe. */
413
414 int
415 template_class_depth (tree type)
416 {
417 int depth;
418
419 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
420 {
421 tree tinfo = get_template_info (type);
422
423 if (tinfo
424 && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
425 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
426 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
427 ++depth;
428
429 if (DECL_P (type))
430 {
431 if (tree fctx = DECL_FRIEND_CONTEXT (type))
432 type = fctx;
433 else
434 type = CP_DECL_CONTEXT (type);
435 }
436 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
437 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
438 else
439 type = CP_TYPE_CONTEXT (type);
440 }
441
442 return depth;
443 }
444
445 /* Return TRUE if NODE instantiates a template that has arguments of
446 its own, be it directly a primary template or indirectly through a
447 partial specializations. */
448 static bool
449 instantiates_primary_template_p (tree node)
450 {
451 tree tinfo = get_template_info (node);
452 if (!tinfo)
453 return false;
454
455 tree tmpl = TI_TEMPLATE (tinfo);
456 if (PRIMARY_TEMPLATE_P (tmpl))
457 return true;
458
459 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
460 return false;
461
462 /* So now we know we have a specialization, but it could be a full
463 or a partial specialization. To tell which, compare the depth of
464 its template arguments with those of its context. */
465
466 tree ctxt = DECL_CONTEXT (tmpl);
467 tree ctinfo = get_template_info (ctxt);
468 if (!ctinfo)
469 return true;
470
471 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
472 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
473 }
474
475 /* Subroutine of maybe_begin_member_template_processing.
476 Returns true if processing DECL needs us to push template parms. */
477
478 static bool
479 inline_needs_template_parms (tree decl, bool nsdmi)
480 {
481 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
482 return false;
483
484 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
485 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
486 }
487
488 /* Subroutine of maybe_begin_member_template_processing.
489 Push the template parms in PARMS, starting from LEVELS steps into the
490 chain, and ending at the beginning, since template parms are listed
491 innermost first. */
492
493 static void
494 push_inline_template_parms_recursive (tree parmlist, int levels)
495 {
496 tree parms = TREE_VALUE (parmlist);
497 int i;
498
499 if (levels > 1)
500 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
501
502 ++processing_template_decl;
503 current_template_parms
504 = tree_cons (size_int (current_template_depth + 1),
505 parms, current_template_parms);
506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
507 = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
508 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
509
510 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
511 NULL);
512 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
513 {
514 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
515
516 if (error_operand_p (parm))
517 continue;
518
519 gcc_assert (DECL_P (parm));
520
521 switch (TREE_CODE (parm))
522 {
523 case TYPE_DECL:
524 case TEMPLATE_DECL:
525 pushdecl (parm);
526 break;
527
528 case PARM_DECL:
529 /* Push the CONST_DECL. */
530 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
531 break;
532
533 default:
534 gcc_unreachable ();
535 }
536 }
537 }
538
539 /* Restore the template parameter context for a member template, a
540 friend template defined in a class definition, or a non-template
541 member of template class. */
542
543 void
544 maybe_begin_member_template_processing (tree decl)
545 {
546 tree parms;
547 int levels = 0;
548 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
549
550 if (nsdmi)
551 {
552 tree ctx = DECL_CONTEXT (decl);
553 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
554 /* Disregard full specializations (c++/60999). */
555 && uses_template_parms (ctx)
556 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
557 }
558
559 if (inline_needs_template_parms (decl, nsdmi))
560 {
561 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
562 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
563
564 if (DECL_TEMPLATE_SPECIALIZATION (decl))
565 {
566 --levels;
567 parms = TREE_CHAIN (parms);
568 }
569
570 push_inline_template_parms_recursive (parms, levels);
571 }
572
573 /* Remember how many levels of template parameters we pushed so that
574 we can pop them later. */
575 inline_parm_levels.safe_push (levels);
576 }
577
578 /* Undo the effects of maybe_begin_member_template_processing. */
579
580 void
581 maybe_end_member_template_processing (void)
582 {
583 int i;
584 int last;
585
586 if (inline_parm_levels.length () == 0)
587 return;
588
589 last = inline_parm_levels.pop ();
590 for (i = 0; i < last; ++i)
591 {
592 --processing_template_decl;
593 current_template_parms = TREE_CHAIN (current_template_parms);
594 poplevel (0, 0, 0);
595 }
596 }
597
598 /* Return a new template argument vector which contains all of ARGS,
599 but has as its innermost set of arguments the EXTRA_ARGS. */
600
601 tree
602 add_to_template_args (tree args, tree extra_args)
603 {
604 tree new_args;
605 int extra_depth;
606 int i;
607 int j;
608
609 if (args == NULL_TREE || extra_args == error_mark_node)
610 return extra_args;
611
612 extra_depth = TMPL_ARGS_DEPTH (extra_args);
613 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
614
615 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
616 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
617
618 for (j = 1; j <= extra_depth; ++j, ++i)
619 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
620
621 return new_args;
622 }
623
624 /* Like add_to_template_args, but only the outermost ARGS are added to
625 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
626 (EXTRA_ARGS) levels are added. This function is used to combine
627 the template arguments from a partial instantiation with the
628 template arguments used to attain the full instantiation from the
629 partial instantiation.
630
631 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
632
633 tree
634 add_outermost_template_args (tree args, tree extra_args)
635 {
636 tree new_args;
637
638 if (!args)
639 return extra_args;
640 if (TREE_CODE (args) == TEMPLATE_DECL)
641 {
642 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
643 args = TI_ARGS (ti);
644 }
645
646 /* If there are more levels of EXTRA_ARGS than there are ARGS,
647 something very fishy is going on. */
648 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
649
650 /* If *all* the new arguments will be the EXTRA_ARGS, just return
651 them. */
652 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
653 return extra_args;
654
655 /* For the moment, we make ARGS look like it contains fewer levels. */
656 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
657
658 new_args = add_to_template_args (args, extra_args);
659
660 /* Now, we restore ARGS to its full dimensions. */
661 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
662
663 return new_args;
664 }
665
666 /* Return the N levels of innermost template arguments from the ARGS. */
667
668 tree
669 get_innermost_template_args (tree args, int n)
670 {
671 tree new_args;
672 int extra_levels;
673 int i;
674
675 gcc_assert (n >= 0);
676
677 /* If N is 1, just return the innermost set of template arguments. */
678 if (n == 1)
679 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
680
681 /* If we're not removing anything, just return the arguments we were
682 given. */
683 extra_levels = TMPL_ARGS_DEPTH (args) - n;
684 gcc_assert (extra_levels >= 0);
685 if (extra_levels == 0)
686 return args;
687
688 /* Make a new set of arguments, not containing the outer arguments. */
689 new_args = make_tree_vec (n);
690 for (i = 1; i <= n; ++i)
691 SET_TMPL_ARGS_LEVEL (new_args, i,
692 TMPL_ARGS_LEVEL (args, i + extra_levels));
693
694 return new_args;
695 }
696
697 /* The inverse of get_innermost_template_args: Return all but the innermost
698 EXTRA_LEVELS levels of template arguments from the ARGS. */
699
700 static tree
701 strip_innermost_template_args (tree args, int extra_levels)
702 {
703 tree new_args;
704 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
705 int i;
706
707 gcc_assert (n >= 0);
708
709 /* If N is 1, just return the outermost set of template arguments. */
710 if (n == 1)
711 return TMPL_ARGS_LEVEL (args, 1);
712
713 /* If we're not removing anything, just return the arguments we were
714 given. */
715 gcc_assert (extra_levels >= 0);
716 if (extra_levels == 0)
717 return args;
718
719 /* Make a new set of arguments, not containing the inner arguments. */
720 new_args = make_tree_vec (n);
721 for (i = 1; i <= n; ++i)
722 SET_TMPL_ARGS_LEVEL (new_args, i,
723 TMPL_ARGS_LEVEL (args, i));
724
725 return new_args;
726 }
727
728 /* We've got a template header coming up; push to a new level for storing
729 the parms. */
730
731 void
732 begin_template_parm_list (void)
733 {
734 /* We use a non-tag-transparent scope here, which causes pushtag to
735 put tags in this scope, rather than in the enclosing class or
736 namespace scope. This is the right thing, since we want
737 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
738 global template class, push_template_decl handles putting the
739 TEMPLATE_DECL into top-level scope. For a nested template class,
740 e.g.:
741
742 template <class T> struct S1 {
743 template <class T> struct S2 {};
744 };
745
746 pushtag contains special code to insert the TEMPLATE_DECL for S2
747 at the right scope. */
748 begin_scope (sk_template_parms, NULL);
749 ++processing_template_decl;
750 ++processing_template_parmlist;
751 note_template_header (0);
752
753 /* Add a dummy parameter level while we process the parameter list. */
754 current_template_parms
755 = tree_cons (size_int (current_template_depth + 1),
756 make_tree_vec (0),
757 current_template_parms);
758 }
759
760 /* This routine is called when a specialization is declared. If it is
761 invalid to declare a specialization here, an error is reported and
762 false is returned, otherwise this routine will return true. */
763
764 static bool
765 check_specialization_scope (void)
766 {
767 tree scope = current_scope ();
768
769 /* [temp.expl.spec]
770
771 An explicit specialization shall be declared in the namespace of
772 which the template is a member, or, for member templates, in the
773 namespace of which the enclosing class or enclosing class
774 template is a member. An explicit specialization of a member
775 function, member class or static data member of a class template
776 shall be declared in the namespace of which the class template
777 is a member. */
778 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
779 {
780 error ("explicit specialization in non-namespace scope %qD", scope);
781 return false;
782 }
783
784 /* [temp.expl.spec]
785
786 In an explicit specialization declaration for a member of a class
787 template or a member template that appears in namespace scope,
788 the member template and some of its enclosing class templates may
789 remain unspecialized, except that the declaration shall not
790 explicitly specialize a class member template if its enclosing
791 class templates are not explicitly specialized as well. */
792 if (current_template_parms)
793 {
794 error ("enclosing class templates are not explicitly specialized");
795 return false;
796 }
797
798 return true;
799 }
800
801 /* We've just seen template <>. */
802
803 bool
804 begin_specialization (void)
805 {
806 begin_scope (sk_template_spec, NULL);
807 note_template_header (1);
808 return check_specialization_scope ();
809 }
810
811 /* Called at then end of processing a declaration preceded by
812 template<>. */
813
814 void
815 end_specialization (void)
816 {
817 finish_scope ();
818 reset_specialization ();
819 }
820
821 /* Any template <>'s that we have seen thus far are not referring to a
822 function specialization. */
823
824 void
825 reset_specialization (void)
826 {
827 processing_specialization = 0;
828 template_header_count = 0;
829 }
830
831 /* We've just seen a template header. If SPECIALIZATION is nonzero,
832 it was of the form template <>. */
833
834 static void
835 note_template_header (int specialization)
836 {
837 processing_specialization = specialization;
838 template_header_count++;
839 }
840
841 /* We're beginning an explicit instantiation. */
842
843 void
844 begin_explicit_instantiation (void)
845 {
846 gcc_assert (!processing_explicit_instantiation);
847 processing_explicit_instantiation = true;
848 }
849
850
851 void
852 end_explicit_instantiation (void)
853 {
854 gcc_assert (processing_explicit_instantiation);
855 processing_explicit_instantiation = false;
856 }
857
858 /* An explicit specialization or partial specialization of TMPL is being
859 declared. Check that the namespace in which the specialization is
860 occurring is permissible. Returns false iff it is invalid to
861 specialize TMPL in the current namespace. */
862
863 static bool
864 check_specialization_namespace (tree tmpl)
865 {
866 tree tpl_ns = decl_namespace_context (tmpl);
867
868 /* [tmpl.expl.spec]
869
870 An explicit specialization shall be declared in a namespace enclosing the
871 specialized template. An explicit specialization whose declarator-id is
872 not qualified shall be declared in the nearest enclosing namespace of the
873 template, or, if the namespace is inline (7.3.1), any namespace from its
874 enclosing namespace set. */
875 if (current_scope() != DECL_CONTEXT (tmpl)
876 && !at_namespace_scope_p ())
877 {
878 error ("specialization of %qD must appear at namespace scope", tmpl);
879 return false;
880 }
881
882 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
883 /* Same or enclosing namespace. */
884 return true;
885 else
886 {
887 auto_diagnostic_group d;
888 if (permerror (input_location,
889 "specialization of %qD in different namespace", tmpl))
890 inform (DECL_SOURCE_LOCATION (tmpl),
891 " from definition of %q#D", tmpl);
892 return false;
893 }
894 }
895
896 /* SPEC is an explicit instantiation. Check that it is valid to
897 perform this explicit instantiation in the current namespace. */
898
899 static void
900 check_explicit_instantiation_namespace (tree spec)
901 {
902 tree ns;
903
904 /* DR 275: An explicit instantiation shall appear in an enclosing
905 namespace of its template. */
906 ns = decl_namespace_context (spec);
907 if (!is_nested_namespace (current_namespace, ns))
908 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
909 "(which does not enclose namespace %qD)",
910 spec, current_namespace, ns);
911 }
912
913 /* Returns true if TYPE is a new partial specialization that needs to be
914 set up. This may also modify TYPE to point to the correct (new or
915 existing) constrained partial specialization. */
916
917 static bool
918 maybe_new_partial_specialization (tree& type)
919 {
920 /* An implicit instantiation of an incomplete type implies
921 the definition of a new class template.
922
923 template<typename T>
924 struct S;
925
926 template<typename T>
927 struct S<T*>;
928
929 Here, S<T*> is an implicit instantiation of S whose type
930 is incomplete. */
931 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
932 return true;
933
934 /* It can also be the case that TYPE is a completed specialization.
935 Continuing the previous example, suppose we also declare:
936
937 template<typename T>
938 requires Integral<T>
939 struct S<T*>;
940
941 Here, S<T*> refers to the specialization S<T*> defined
942 above. However, we need to differentiate definitions because
943 we intend to define a new partial specialization. In this case,
944 we rely on the fact that the constraints are different for
945 this declaration than that above.
946
947 Note that we also get here for injected class names and
948 late-parsed template definitions. We must ensure that we
949 do not create new type declarations for those cases. */
950 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
951 {
952 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
953 tree args = CLASSTYPE_TI_ARGS (type);
954
955 /* If there are no template parameters, this cannot be a new
956 partial template specialization? */
957 if (!current_template_parms)
958 return false;
959
960 /* The injected-class-name is not a new partial specialization. */
961 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
962 return false;
963
964 /* If the constraints are not the same as those of the primary
965 then, we can probably create a new specialization. */
966 tree type_constr = current_template_constraints ();
967
968 if (type == TREE_TYPE (tmpl))
969 {
970 tree main_constr = get_constraints (tmpl);
971 if (equivalent_constraints (type_constr, main_constr))
972 return false;
973 }
974
975 /* Also, if there's a pre-existing specialization with matching
976 constraints, then this also isn't new. */
977 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
978 while (specs)
979 {
980 tree spec_tmpl = TREE_VALUE (specs);
981 tree spec_args = TREE_PURPOSE (specs);
982 tree spec_constr = get_constraints (spec_tmpl);
983 if (comp_template_args (args, spec_args)
984 && equivalent_constraints (type_constr, spec_constr))
985 {
986 type = TREE_TYPE (spec_tmpl);
987 return false;
988 }
989 specs = TREE_CHAIN (specs);
990 }
991
992 /* Create a new type node (and corresponding type decl)
993 for the newly declared specialization. */
994 tree t = make_class_type (TREE_CODE (type));
995 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
996 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
997
998 /* We only need a separate type node for storing the definition of this
999 partial specialization; uses of S<T*> are unconstrained, so all are
1000 equivalent. So keep TYPE_CANONICAL the same. */
1001 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
1002
1003 /* Build the corresponding type decl. */
1004 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
1005 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
1006 DECL_SOURCE_LOCATION (d) = input_location;
1007 TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
1008
1009 set_instantiating_module (d);
1010 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
1011
1012 type = t;
1013 return true;
1014 }
1015
1016 return false;
1017 }
1018
1019 /* The TYPE is being declared. If it is a template type, that means it
1020 is a partial specialization. Do appropriate error-checking. */
1021
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 if (permerror (input_location,
1129 "specialization of %qD in different namespace",
1130 type))
1131 inform (DECL_SOURCE_LOCATION (tmpl),
1132 "from definition of %q#D", tmpl);
1133 }
1134
1135 /* Check for invalid specialization after instantiation:
1136
1137 template <> template <> class C<int>::D<int>;
1138 template <> template <class U> class C<int>::D; */
1139
1140 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1141 t; t = TREE_CHAIN (t))
1142 {
1143 tree inst = TREE_VALUE (t);
1144 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1145 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1146 {
1147 /* We already have a full specialization of this partial
1148 instantiation, or a full specialization has been
1149 looked up but not instantiated. Reassign it to the
1150 new member specialization template. */
1151 spec_entry elt;
1152 spec_entry *entry;
1153
1154 elt.tmpl = most_general_template (tmpl);
1155 elt.args = CLASSTYPE_TI_ARGS (inst);
1156 elt.spec = inst;
1157
1158 type_specializations->remove_elt (&elt);
1159
1160 elt.tmpl = tmpl;
1161 CLASSTYPE_TI_ARGS (inst)
1162 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1163
1164 spec_entry **slot
1165 = type_specializations->find_slot (&elt, INSERT);
1166 entry = ggc_alloc<spec_entry> ();
1167 *entry = elt;
1168 *slot = entry;
1169 }
1170 else
1171 /* But if we've had an implicit instantiation, that's a
1172 problem ([temp.expl.spec]/6). */
1173 error ("specialization %qT after instantiation %qT",
1174 type, inst);
1175 }
1176
1177 /* Mark TYPE as a specialization. And as a result, we only
1178 have one level of template argument for the innermost
1179 class template. */
1180 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1181 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1182 CLASSTYPE_TI_ARGS (type)
1183 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1184 }
1185 }
1186 else if (processing_specialization)
1187 {
1188 /* Someday C++0x may allow for enum template specialization. */
1189 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1190 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1191 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1192 "of %qD not allowed by ISO C++", type);
1193 else
1194 {
1195 error ("explicit specialization of non-template %qT", type);
1196 return error_mark_node;
1197 }
1198 }
1199
1200 return type;
1201 }
1202
1203 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1204 gone through coerce_template_parms by now. */
1205
1206 static void
1207 verify_unstripped_args_1 (tree inner)
1208 {
1209 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1210 {
1211 tree arg = TREE_VEC_ELT (inner, i);
1212 if (TREE_CODE (arg) == TEMPLATE_DECL)
1213 /* OK */;
1214 else if (TYPE_P (arg))
1215 gcc_assert (strip_typedefs (arg, NULL) == arg);
1216 else if (ARGUMENT_PACK_P (arg))
1217 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1218 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1219 /* Allow typedefs on the type of a non-type argument, since a
1220 parameter can have them. */;
1221 else
1222 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1223 }
1224 }
1225
1226 static void
1227 verify_unstripped_args (tree args)
1228 {
1229 ++processing_template_decl;
1230 if (!any_dependent_template_arguments_p (args))
1231 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1232 --processing_template_decl;
1233 }
1234
1235 /* Retrieve the specialization (in the sense of [temp.spec] - a
1236 specialization is either an instantiation or an explicit
1237 specialization) of TMPL for the given template ARGS. If there is
1238 no such specialization, return NULL_TREE. The ARGS are a vector of
1239 arguments, or a vector of vectors of arguments, in the case of
1240 templates with more than one level of parameters.
1241
1242 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1243 then we search for a partial specialization matching ARGS. This
1244 parameter is ignored if TMPL is not a class template.
1245
1246 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1247 result is a NONTYPE_ARGUMENT_PACK. */
1248
1249 static tree
1250 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1251 {
1252 if (tmpl == NULL_TREE)
1253 return NULL_TREE;
1254
1255 if (args == error_mark_node)
1256 return NULL_TREE;
1257
1258 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1259 || TREE_CODE (tmpl) == FIELD_DECL);
1260
1261 /* There should be as many levels of arguments as there are
1262 levels of parameters. */
1263 gcc_assert (TMPL_ARGS_DEPTH (args)
1264 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1265 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1266 : template_class_depth (DECL_CONTEXT (tmpl))));
1267
1268 if (flag_checking)
1269 verify_unstripped_args (args);
1270
1271 /* Lambda functions in templates aren't instantiated normally, but through
1272 tsubst_lambda_expr. */
1273 if (lambda_fn_in_template_p (tmpl))
1274 return NULL_TREE;
1275
1276 spec_entry elt;
1277 elt.tmpl = tmpl;
1278 elt.args = args;
1279 elt.spec = NULL_TREE;
1280
1281 spec_hash_table *specializations;
1282 if (DECL_CLASS_TEMPLATE_P (tmpl))
1283 specializations = type_specializations;
1284 else
1285 specializations = decl_specializations;
1286
1287 if (hash == 0)
1288 hash = spec_hasher::hash (&elt);
1289 if (spec_entry *found = specializations->find_with_hash (&elt, hash))
1290 return found->spec;
1291
1292 return NULL_TREE;
1293 }
1294
1295 /* Like retrieve_specialization, but for local declarations. */
1296
1297 tree
1298 retrieve_local_specialization (tree tmpl)
1299 {
1300 if (local_specializations == NULL)
1301 return NULL_TREE;
1302
1303 tree *slot = local_specializations->get (tmpl);
1304 return slot ? *slot : NULL_TREE;
1305 }
1306
1307 /* Returns nonzero iff DECL is a specialization of TMPL. */
1308
1309 int
1310 is_specialization_of (tree decl, tree tmpl)
1311 {
1312 tree t;
1313
1314 if (TREE_CODE (decl) == FUNCTION_DECL)
1315 {
1316 for (t = decl;
1317 t != NULL_TREE;
1318 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1319 if (t == tmpl)
1320 return 1;
1321 }
1322 else
1323 {
1324 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1325
1326 for (t = TREE_TYPE (decl);
1327 t != NULL_TREE;
1328 t = CLASSTYPE_USE_TEMPLATE (t)
1329 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1330 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1331 return 1;
1332 }
1333
1334 return 0;
1335 }
1336
1337 /* Returns nonzero iff DECL is a specialization of friend declaration
1338 FRIEND_DECL according to [temp.friend]. */
1339
1340 bool
1341 is_specialization_of_friend (tree decl, tree friend_decl)
1342 {
1343 bool need_template = true;
1344 int template_depth;
1345
1346 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1347 || TREE_CODE (decl) == TYPE_DECL);
1348
1349 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1350 of a template class, we want to check if DECL is a specialization
1351 if this. */
1352 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1353 && DECL_CLASS_SCOPE_P (friend_decl)
1354 && DECL_TEMPLATE_INFO (friend_decl)
1355 && !DECL_USE_TEMPLATE (friend_decl))
1356 {
1357 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1358 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1359 need_template = false;
1360 }
1361 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1362 && !PRIMARY_TEMPLATE_P (friend_decl))
1363 need_template = false;
1364
1365 /* There is nothing to do if this is not a template friend. */
1366 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1367 return false;
1368
1369 if (is_specialization_of (decl, friend_decl))
1370 return true;
1371
1372 /* [temp.friend/6]
1373 A member of a class template may be declared to be a friend of a
1374 non-template class. In this case, the corresponding member of
1375 every specialization of the class template is a friend of the
1376 class granting friendship.
1377
1378 For example, given a template friend declaration
1379
1380 template <class T> friend void A<T>::f();
1381
1382 the member function below is considered a friend
1383
1384 template <> struct A<int> {
1385 void f();
1386 };
1387
1388 For this type of template friend, TEMPLATE_DEPTH below will be
1389 nonzero. To determine if DECL is a friend of FRIEND, we first
1390 check if the enclosing class is a specialization of another. */
1391
1392 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1393 if (template_depth
1394 && DECL_CLASS_SCOPE_P (decl)
1395 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1396 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1397 {
1398 /* Next, we check the members themselves. In order to handle
1399 a few tricky cases, such as when FRIEND_DECL's are
1400
1401 template <class T> friend void A<T>::g(T t);
1402 template <class T> template <T t> friend void A<T>::h();
1403
1404 and DECL's are
1405
1406 void A<int>::g(int);
1407 template <int> void A<int>::h();
1408
1409 we need to figure out ARGS, the template arguments from
1410 the context of DECL. This is required for template substitution
1411 of `T' in the function parameter of `g' and template parameter
1412 of `h' in the above examples. Here ARGS corresponds to `int'. */
1413
1414 tree context = DECL_CONTEXT (decl);
1415 tree args = NULL_TREE;
1416 int current_depth = 0;
1417
1418 while (current_depth < template_depth)
1419 {
1420 if (CLASSTYPE_TEMPLATE_INFO (context))
1421 {
1422 if (current_depth == 0)
1423 args = TYPE_TI_ARGS (context);
1424 else
1425 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1426 current_depth++;
1427 }
1428 context = TYPE_CONTEXT (context);
1429 }
1430
1431 if (TREE_CODE (decl) == FUNCTION_DECL)
1432 {
1433 bool is_template;
1434 tree friend_type;
1435 tree decl_type;
1436 tree friend_args_type;
1437 tree decl_args_type;
1438
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template = DECL_TEMPLATE_INFO (decl)
1442 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1443 if (need_template ^ is_template)
1444 return false;
1445 else if (is_template)
1446 {
1447 /* If both are templates, check template parameter list. */
1448 tree friend_parms
1449 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1450 args, tf_none);
1451 if (!comp_template_parms
1452 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1453 friend_parms))
1454 return false;
1455
1456 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1457 }
1458 else
1459 decl_type = TREE_TYPE (decl);
1460
1461 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1462 tf_none, NULL_TREE);
1463 if (friend_type == error_mark_node)
1464 return false;
1465
1466 /* Check if return types match. */
1467 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1468 return false;
1469
1470 /* Check if function parameter types match, ignoring the
1471 `this' parameter. */
1472 friend_args_type = TYPE_ARG_TYPES (friend_type);
1473 decl_args_type = TYPE_ARG_TYPES (decl_type);
1474 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
1475 friend_args_type = TREE_CHAIN (friend_args_type);
1476 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
1477 decl_args_type = TREE_CHAIN (decl_args_type);
1478
1479 return compparms (decl_args_type, friend_args_type);
1480 }
1481 else
1482 {
1483 /* DECL is a TYPE_DECL */
1484 bool is_template;
1485 tree decl_type = TREE_TYPE (decl);
1486
1487 /* Make sure that both DECL and FRIEND_DECL are templates or
1488 non-templates. */
1489 is_template
1490 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1491 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1492
1493 if (need_template ^ is_template)
1494 return false;
1495 else if (is_template)
1496 {
1497 tree friend_parms;
1498 /* If both are templates, check the name of the two
1499 TEMPLATE_DECL's first because is_friend didn't. */
1500 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1501 != DECL_NAME (friend_decl))
1502 return false;
1503
1504 /* Now check template parameter list. */
1505 friend_parms
1506 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1507 args, tf_none);
1508 return comp_template_parms
1509 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1510 friend_parms);
1511 }
1512 else
1513 return (DECL_NAME (decl)
1514 == DECL_NAME (friend_decl));
1515 }
1516 }
1517 return false;
1518 }
1519
1520 /* Register the specialization SPEC as a specialization of TMPL with
1521 the indicated ARGS. IS_FRIEND indicates whether the specialization
1522 is actually just a friend declaration. ATTRLIST is the list of
1523 attributes that the specialization is declared with or NULL when
1524 it isn't. Returns SPEC, or an equivalent prior declaration, if
1525 available.
1526
1527 We also store instantiations of field packs in the hash table, even
1528 though they are not themselves templates, to make lookup easier. */
1529
1530 static tree
1531 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1532 hashval_t hash)
1533 {
1534 tree fn;
1535
1536 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1537 || (TREE_CODE (tmpl) == FIELD_DECL
1538 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1539
1540 spec_entry elt;
1541 elt.tmpl = tmpl;
1542 elt.args = args;
1543 elt.spec = spec;
1544
1545 if (hash == 0)
1546 hash = spec_hasher::hash (&elt);
1547
1548 spec_entry **slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1549 if (*slot)
1550 fn = (*slot)->spec;
1551 else
1552 fn = NULL_TREE;
1553
1554 /* We can sometimes try to re-register a specialization that we've
1555 already got. In particular, regenerate_decl_from_template calls
1556 duplicate_decls which will update the specialization list. But,
1557 we'll still get called again here anyhow. It's more convenient
1558 to simply allow this than to try to prevent it. */
1559 if (fn == spec)
1560 return spec;
1561 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1562 {
1563 if (DECL_TEMPLATE_INSTANTIATION (fn))
1564 {
1565 if (DECL_ODR_USED (fn)
1566 || DECL_EXPLICIT_INSTANTIATION (fn))
1567 {
1568 error ("specialization of %qD after instantiation",
1569 fn);
1570 return error_mark_node;
1571 }
1572 else
1573 {
1574 tree clone;
1575 /* This situation should occur only if the first
1576 specialization is an implicit instantiation, the
1577 second is an explicit specialization, and the
1578 implicit instantiation has not yet been used. That
1579 situation can occur if we have implicitly
1580 instantiated a member function and then specialized
1581 it later.
1582
1583 We can also wind up here if a friend declaration that
1584 looked like an instantiation turns out to be a
1585 specialization:
1586
1587 template <class T> void foo(T);
1588 class S { friend void foo<>(int) };
1589 template <> void foo(int);
1590
1591 We transform the existing DECL in place so that any
1592 pointers to it become pointers to the updated
1593 declaration.
1594
1595 If there was a definition for the template, but not
1596 for the specialization, we want this to look as if
1597 there were no definition, and vice versa. */
1598 DECL_INITIAL (fn) = NULL_TREE;
1599 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1600
1601 /* The call to duplicate_decls will have applied
1602 [temp.expl.spec]:
1603
1604 An explicit specialization of a function template
1605 is inline only if it is explicitly declared to be,
1606 and independently of whether its function template
1607 is.
1608
1609 to the primary function; now copy the inline bits to
1610 the various clones. */
1611 FOR_EACH_CLONE (clone, fn)
1612 {
1613 DECL_DECLARED_INLINE_P (clone)
1614 = DECL_DECLARED_INLINE_P (fn);
1615 DECL_SOURCE_LOCATION (clone)
1616 = DECL_SOURCE_LOCATION (fn);
1617 DECL_DELETED_FN (clone)
1618 = DECL_DELETED_FN (fn);
1619 }
1620 check_specialization_namespace (tmpl);
1621
1622 return fn;
1623 }
1624 }
1625 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1626 {
1627 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1628 if (dd == error_mark_node)
1629 /* We've already complained in duplicate_decls. */
1630 return error_mark_node;
1631
1632 if (dd == NULL_TREE && DECL_INITIAL (spec))
1633 /* Dup decl failed, but this is a new definition. Set the
1634 line number so any errors match this new
1635 definition. */
1636 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1637
1638 return fn;
1639 }
1640 }
1641 else if (fn)
1642 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1643
1644 /* A specialization must be declared in the same namespace as the
1645 template it is specializing. */
1646 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1647 && !check_specialization_namespace (tmpl))
1648 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1649
1650 spec_entry *entry = ggc_alloc<spec_entry> ();
1651 gcc_assert (tmpl && args && spec);
1652 *entry = elt;
1653 *slot = entry;
1654 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1655 && PRIMARY_TEMPLATE_P (tmpl)
1656 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1657 || variable_template_p (tmpl))
1658 /* If TMPL is a forward declaration of a template function, keep a list
1659 of all specializations in case we need to reassign them to a friend
1660 template later in tsubst_friend_function.
1661
1662 Also keep a list of all variable template instantiations so that
1663 process_partial_specialization can check whether a later partial
1664 specialization would have used it. */
1665 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1666 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1667
1668 return spec;
1669 }
1670
1671 /* Restricts tree and type comparisons. */
1672 int comparing_specializations;
1673 int comparing_dependent_aliases;
1674
1675 /* Whether we are comparing template arguments during partial ordering
1676 (and therefore want the comparison to look through dependent alias
1677 template specializations). */
1678
1679 static int comparing_for_partial_ordering;
1680
1681 /* Returns true iff two spec_entry nodes are equivalent. */
1682
1683 bool
1684 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1685 {
1686 int equal;
1687
1688 ++comparing_specializations;
1689 ++comparing_dependent_aliases;
1690 ++processing_template_decl;
1691 equal = (e1->tmpl == e2->tmpl
1692 && comp_template_args (e1->args, e2->args));
1693 if (equal && flag_concepts
1694 /* tmpl could be a FIELD_DECL for a capture pack. */
1695 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1696 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1697 && uses_template_parms (e1->args))
1698 {
1699 /* Partial specializations of a variable template can be distinguished by
1700 constraints. */
1701 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1702 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1703 equal = equivalent_constraints (c1, c2);
1704 }
1705 --processing_template_decl;
1706 --comparing_dependent_aliases;
1707 --comparing_specializations;
1708
1709 return equal;
1710 }
1711
1712 /* Returns a hash for a template TMPL and template arguments ARGS. */
1713
1714 static hashval_t
1715 hash_tmpl_and_args (tree tmpl, tree args)
1716 {
1717 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1718 return iterative_hash_template_arg (args, val);
1719 }
1720
1721 hashval_t
1722 spec_hasher::hash (tree tmpl, tree args)
1723 {
1724 ++comparing_specializations;
1725 hashval_t val = hash_tmpl_and_args (tmpl, args);
1726 --comparing_specializations;
1727 return val;
1728 }
1729
1730 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1731 ignoring SPEC. */
1732
1733 hashval_t
1734 spec_hasher::hash (spec_entry *e)
1735 {
1736 return spec_hasher::hash (e->tmpl, e->args);
1737 }
1738
1739 /* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. We must be
1741 careful to (at least) skip the same entities template_args_equal
1742 does. */
1743
1744 hashval_t
1745 iterative_hash_template_arg (tree arg, hashval_t val)
1746 {
1747 if (arg == NULL_TREE)
1748 return iterative_hash_object (arg, val);
1749
1750 if (!TYPE_P (arg))
1751 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1752 while (CONVERT_EXPR_P (arg)
1753 || TREE_CODE (arg) == NON_LVALUE_EXPR
1754 || class_nttp_const_wrapper_p (arg))
1755 arg = TREE_OPERAND (arg, 0);
1756
1757 enum tree_code code = TREE_CODE (arg);
1758
1759 val = iterative_hash_object (code, val);
1760
1761 switch (code)
1762 {
1763 case ARGUMENT_PACK_SELECT:
1764 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1765 preserving it in a hash table, which is bad because it will change
1766 meaning when gen_elem_of_pack_expansion_instantiation changes the
1767 ARGUMENT_PACK_SELECT_INDEX. */
1768 gcc_unreachable ();
1769
1770 case ERROR_MARK:
1771 return val;
1772
1773 case IDENTIFIER_NODE:
1774 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1775
1776 case TREE_VEC:
1777 for (tree elt : tree_vec_range (arg))
1778 val = iterative_hash_template_arg (elt, val);
1779 return val;
1780
1781 case TYPE_PACK_EXPANSION:
1782 case EXPR_PACK_EXPANSION:
1783 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1784 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1785
1786 case TYPE_ARGUMENT_PACK:
1787 case NONTYPE_ARGUMENT_PACK:
1788 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1789
1790 case TREE_LIST:
1791 for (; arg; arg = TREE_CHAIN (arg))
1792 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1793 return val;
1794
1795 case OVERLOAD:
1796 for (lkp_iterator iter (arg); iter; ++iter)
1797 val = iterative_hash_template_arg (*iter, val);
1798 return val;
1799
1800 case CONSTRUCTOR:
1801 {
1802 iterative_hash_template_arg (TREE_TYPE (arg), val);
1803 for (auto &e: CONSTRUCTOR_ELTS (arg))
1804 {
1805 val = iterative_hash_template_arg (e.index, val);
1806 val = iterative_hash_template_arg (e.value, val);
1807 }
1808 return val;
1809 }
1810
1811 case PARM_DECL:
1812 if (!DECL_ARTIFICIAL (arg))
1813 {
1814 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1815 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1816 }
1817 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1818
1819 case TEMPLATE_DECL:
1820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
1821 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1822 break;
1823
1824 case TARGET_EXPR:
1825 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1826
1827 case PTRMEM_CST:
1828 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1829 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1830
1831 case TEMPLATE_PARM_INDEX:
1832 val = iterative_hash_template_arg
1833 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1834 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1835 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1836
1837 case TRAIT_EXPR:
1838 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1839 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1840 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1841
1842 case BASELINK:
1843 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1844 val);
1845 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1846 val);
1847
1848 case MODOP_EXPR:
1849 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1850 code = TREE_CODE (TREE_OPERAND (arg, 1));
1851 val = iterative_hash_object (code, val);
1852 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1853
1854 case LAMBDA_EXPR:
1855 /* [temp.over.link] Two lambda-expressions are never considered
1856 equivalent.
1857
1858 So just hash the closure type. */
1859 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1860
1861 case CAST_EXPR:
1862 case IMPLICIT_CONV_EXPR:
1863 case STATIC_CAST_EXPR:
1864 case REINTERPRET_CAST_EXPR:
1865 case CONST_CAST_EXPR:
1866 case DYNAMIC_CAST_EXPR:
1867 case NEW_EXPR:
1868 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1869 /* Now hash operands as usual. */
1870 break;
1871
1872 case CALL_EXPR:
1873 {
1874 tree fn = CALL_EXPR_FN (arg);
1875 if (tree name = call_expr_dependent_name (arg))
1876 {
1877 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1878 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1879 fn = name;
1880 }
1881 val = iterative_hash_template_arg (fn, val);
1882 call_expr_arg_iterator ai;
1883 for (tree x = first_call_expr_arg (arg, &ai); x;
1884 x = next_call_expr_arg (&ai))
1885 val = iterative_hash_template_arg (x, val);
1886 return val;
1887 }
1888
1889 default:
1890 break;
1891 }
1892
1893 char tclass = TREE_CODE_CLASS (code);
1894 switch (tclass)
1895 {
1896 case tcc_type:
1897 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1898 {
1899 // We want an alias specialization that survived strip_typedefs
1900 // to hash differently from its TYPE_CANONICAL, to avoid hash
1901 // collisions that compare as different in template_args_equal.
1902 // These could be dependent specializations that strip_typedefs
1903 // left alone, or untouched specializations because
1904 // coerce_template_parms returns the unconverted template
1905 // arguments if it sees incomplete argument packs.
1906 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1907 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1908 }
1909
1910 switch (code)
1911 {
1912 case DECLTYPE_TYPE:
1913 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1914 break;
1915
1916 case TYPENAME_TYPE:
1917 if (comparing_specializations)
1918 {
1919 /* Hash the components that are relevant to TYPENAME_TYPE
1920 equivalence as determined by structural_comptypes. We
1921 can only coherently do this when comparing_specializations
1922 is set, because otherwise structural_comptypes tries
1923 resolving TYPENAME_TYPE via the current instantiation. */
1924 tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
1925 tree fullname = TYPENAME_TYPE_FULLNAME (arg);
1926 val = iterative_hash_template_arg (context, val);
1927 val = iterative_hash_template_arg (fullname, val);
1928 }
1929 break;
1930
1931 default:
1932 if (tree canonical = TYPE_CANONICAL (arg))
1933 val = iterative_hash_object (TYPE_HASH (canonical), val);
1934 else if (tree ti = TYPE_TEMPLATE_INFO (arg))
1935 {
1936 val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
1937 val = iterative_hash_template_arg (TI_ARGS (ti), val);
1938 }
1939 break;
1940 }
1941
1942 return val;
1943
1944 case tcc_declaration:
1945 case tcc_constant:
1946 return iterative_hash_expr (arg, val);
1947
1948 default:
1949 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1950 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1951 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1952 return val;
1953 }
1954 }
1955
1956 /* Unregister the specialization SPEC as a specialization of TMPL.
1957 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1958 if the SPEC was listed as a specialization of TMPL.
1959
1960 Note that SPEC has been ggc_freed, so we can't look inside it. */
1961
1962 bool
1963 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1964 {
1965 spec_entry *entry;
1966 spec_entry elt;
1967
1968 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1969 elt.args = TI_ARGS (tinfo);
1970 elt.spec = NULL_TREE;
1971
1972 entry = decl_specializations->find (&elt);
1973 if (entry != NULL)
1974 {
1975 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1976 gcc_assert (new_spec != NULL_TREE);
1977 entry->spec = new_spec;
1978 return 1;
1979 }
1980
1981 return 0;
1982 }
1983
1984 /* Like register_specialization, but for local declarations. We are
1985 registering SPEC, an instantiation of TMPL. */
1986
1987 void
1988 register_local_specialization (tree spec, tree tmpl)
1989 {
1990 gcc_assert (tmpl != spec);
1991 local_specializations->put (tmpl, spec);
1992 }
1993
1994 /* Registers T as a specialization of itself. This is used to preserve
1995 the references to already-parsed parameters when instantiating
1996 postconditions. */
1997
1998 void
1999 register_local_identity (tree t)
2000 {
2001 local_specializations->put (t, t);
2002 }
2003
2004 /* TYPE is a class type. Returns true if TYPE is an explicitly
2005 specialized class. */
2006
2007 bool
2008 explicit_class_specialization_p (tree type)
2009 {
2010 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2011 return false;
2012 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2013 }
2014
2015 /* Print the list of functions at FNS, going through all the overloads
2016 for each element of the list. Alternatively, FNS cannot be a
2017 TREE_LIST, in which case it will be printed together with all the
2018 overloads.
2019
2020 MORE and *STR should respectively be FALSE and NULL when the function
2021 is called from the outside. They are used internally on recursive
2022 calls. print_candidates manages the two parameters and leaves NULL
2023 in *STR when it ends. */
2024
2025 static void
2026 print_candidates_1 (tree fns, char **str, bool more = false)
2027 {
2028 if (TREE_CODE (fns) == TREE_LIST)
2029 for (; fns; fns = TREE_CHAIN (fns))
2030 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2031 else
2032 for (lkp_iterator iter (fns); iter;)
2033 {
2034 tree cand = *iter;
2035 ++iter;
2036
2037 const char *pfx = *str;
2038 if (!pfx)
2039 {
2040 if (more || iter)
2041 pfx = _("candidates are:");
2042 else
2043 pfx = _("candidate is:");
2044 *str = get_spaces (pfx);
2045 }
2046 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2047 }
2048 }
2049
2050 /* Print the list of candidate FNS in an error message. FNS can also
2051 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2052
2053 void
2054 print_candidates (tree fns)
2055 {
2056 char *str = NULL;
2057 print_candidates_1 (fns, &str);
2058 free (str);
2059 }
2060
2061 /* Get a (possibly) constrained template declaration for the
2062 purpose of ordering candidates. */
2063 static tree
2064 get_template_for_ordering (tree list)
2065 {
2066 gcc_assert (TREE_CODE (list) == TREE_LIST);
2067 tree f = TREE_VALUE (list);
2068 if (tree ti = DECL_TEMPLATE_INFO (f))
2069 return TI_TEMPLATE (ti);
2070 return f;
2071 }
2072
2073 /* Among candidates having the same signature, return the
2074 most constrained or NULL_TREE if there is no best candidate.
2075 If the signatures of candidates vary (e.g., template
2076 specialization vs. member function), then there can be no
2077 most constrained.
2078
2079 Note that we don't compare constraints on the functions
2080 themselves, but rather those of their templates. */
2081 static tree
2082 most_constrained_function (tree candidates)
2083 {
2084 // Try to find the best candidate in a first pass.
2085 tree champ = candidates;
2086 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2087 {
2088 int winner = more_constrained (get_template_for_ordering (champ),
2089 get_template_for_ordering (c));
2090 if (winner == -1)
2091 champ = c; // The candidate is more constrained
2092 else if (winner == 0)
2093 return NULL_TREE; // Neither is more constrained
2094 }
2095
2096 // Verify that the champ is better than previous candidates.
2097 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2098 if (!more_constrained (get_template_for_ordering (champ),
2099 get_template_for_ordering (c)))
2100 return NULL_TREE;
2101 }
2102
2103 return champ;
2104 }
2105
2106
2107 /* Returns the template (one of the functions given by TEMPLATE_ID)
2108 which can be specialized to match the indicated DECL with the
2109 explicit template args given in TEMPLATE_ID. The DECL may be
2110 NULL_TREE if none is available. In that case, the functions in
2111 TEMPLATE_ID are non-members.
2112
2113 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2114 specialization of a member template.
2115
2116 The TEMPLATE_COUNT is the number of references to qualifying
2117 template classes that appeared in the name of the function. See
2118 check_explicit_specialization for a more accurate description.
2119
2120 TSK indicates what kind of template declaration (if any) is being
2121 declared. TSK_TEMPLATE indicates that the declaration given by
2122 DECL, though a FUNCTION_DECL, has template parameters, and is
2123 therefore a template function.
2124
2125 The template args (those explicitly specified and those deduced)
2126 are output in a newly created vector *TARGS_OUT.
2127
2128 If it is impossible to determine the result, an error message is
2129 issued. The error_mark_node is returned to indicate failure. */
2130
2131 static tree
2132 determine_specialization (tree template_id,
2133 tree decl,
2134 tree* targs_out,
2135 int need_member_template,
2136 int template_count,
2137 tmpl_spec_kind tsk)
2138 {
2139 tree fns;
2140 tree targs;
2141 tree explicit_targs;
2142 tree candidates = NULL_TREE;
2143
2144 /* A TREE_LIST of templates of which DECL may be a specialization.
2145 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2146 corresponding TREE_PURPOSE is the set of template arguments that,
2147 when used to instantiate the template, would produce a function
2148 with the signature of DECL. */
2149 tree templates = NULL_TREE;
2150 int header_count;
2151 cp_binding_level *b;
2152
2153 *targs_out = NULL_TREE;
2154
2155 if (template_id == error_mark_node || decl == error_mark_node)
2156 return error_mark_node;
2157
2158 /* We shouldn't be specializing a member template of an
2159 unspecialized class template; we already gave an error in
2160 check_specialization_scope, now avoid crashing. */
2161 if (!VAR_P (decl)
2162 && template_count && DECL_CLASS_SCOPE_P (decl)
2163 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2164 {
2165 gcc_assert (errorcount);
2166 return error_mark_node;
2167 }
2168
2169 fns = TREE_OPERAND (template_id, 0);
2170 explicit_targs = TREE_OPERAND (template_id, 1);
2171
2172 if (fns == error_mark_node)
2173 return error_mark_node;
2174
2175 /* Check for baselinks. */
2176 if (BASELINK_P (fns))
2177 fns = BASELINK_FUNCTIONS (fns);
2178
2179 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2180 {
2181 error_at (DECL_SOURCE_LOCATION (decl),
2182 "%qD is not a function template", fns);
2183 return error_mark_node;
2184 }
2185 else if (VAR_P (decl) && !variable_template_p (fns))
2186 {
2187 error ("%qD is not a variable template", fns);
2188 return error_mark_node;
2189 }
2190
2191 /* Count the number of template headers specified for this
2192 specialization. */
2193 header_count = 0;
2194 for (b = current_binding_level;
2195 b->kind == sk_template_parms;
2196 b = b->level_chain)
2197 ++header_count;
2198
2199 tree orig_fns = fns;
2200 bool header_mismatch = false;
2201
2202 if (variable_template_p (fns))
2203 {
2204 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2205 targs = coerce_template_parms (parms, explicit_targs, fns,
2206 tf_warning_or_error);
2207 if (targs != error_mark_node
2208 && constraints_satisfied_p (fns, targs))
2209 templates = tree_cons (targs, fns, templates);
2210 }
2211 else for (lkp_iterator iter (fns); iter; ++iter)
2212 {
2213 tree fn = *iter;
2214
2215 if (TREE_CODE (fn) == TEMPLATE_DECL)
2216 {
2217 tree decl_arg_types;
2218 tree fn_arg_types;
2219
2220 /* In case of explicit specialization, we need to check if
2221 the number of template headers appearing in the specialization
2222 is correct. This is usually done in check_explicit_specialization,
2223 but the check done there cannot be exhaustive when specializing
2224 member functions. Consider the following code:
2225
2226 template <> void A<int>::f(int);
2227 template <> template <> void A<int>::f(int);
2228
2229 Assuming that A<int> is not itself an explicit specialization
2230 already, the first line specializes "f" which is a non-template
2231 member function, whilst the second line specializes "f" which
2232 is a template member function. So both lines are syntactically
2233 correct, and check_explicit_specialization does not reject
2234 them.
2235
2236 Here, we can do better, as we are matching the specialization
2237 against the declarations. We count the number of template
2238 headers, and we check if they match TEMPLATE_COUNT + 1
2239 (TEMPLATE_COUNT is the number of qualifying template classes,
2240 plus there must be another header for the member template
2241 itself).
2242
2243 Notice that if header_count is zero, this is not a
2244 specialization but rather a template instantiation, so there
2245 is no check we can perform here. */
2246 if (header_count && header_count != template_count + 1)
2247 {
2248 header_mismatch = true;
2249 continue;
2250 }
2251
2252 /* Check that the number of template arguments at the
2253 innermost level for DECL is the same as for FN. */
2254 if (current_binding_level->kind == sk_template_parms
2255 && !current_binding_level->explicit_spec_p
2256 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2257 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2258 (current_template_parms))))
2259 continue;
2260
2261 /* DECL might be a specialization of FN. */
2262 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2263 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2264
2265 /* For a non-static member function, we need to make sure
2266 that the const qualification is the same. Since
2267 get_bindings does not try to merge the "this" parameter,
2268 we must do the comparison explicitly. */
2269 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
2270 {
2271 if (!same_type_p (TREE_VALUE (fn_arg_types),
2272 TREE_VALUE (decl_arg_types)))
2273 continue;
2274
2275 /* And the ref-qualification. */
2276 if (type_memfn_rqual (TREE_TYPE (decl))
2277 != type_memfn_rqual (TREE_TYPE (fn)))
2278 continue;
2279 }
2280
2281 /* Skip the "this" parameter and, for constructors of
2282 classes with virtual bases, the VTT parameter. A
2283 full specialization of a constructor will have a VTT
2284 parameter, but a template never will. */
2285 decl_arg_types
2286 = skip_artificial_parms_for (decl, decl_arg_types);
2287 fn_arg_types
2288 = skip_artificial_parms_for (fn, fn_arg_types);
2289
2290 /* Function templates cannot be specializations; there are
2291 no partial specializations of functions. Therefore, if
2292 the type of DECL does not match FN, there is no
2293 match.
2294
2295 Note that it should never be the case that we have both
2296 candidates added here, and for regular member functions
2297 below. */
2298 if (tsk == tsk_template)
2299 {
2300 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2301 current_template_parms))
2302 continue;
2303 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2304 TREE_TYPE (TREE_TYPE (fn))))
2305 continue;
2306 if (!compparms (fn_arg_types, decl_arg_types))
2307 continue;
2308
2309 tree freq = get_constraints (fn);
2310 tree dreq = get_constraints (decl);
2311 if (!freq != !dreq)
2312 continue;
2313 if (freq)
2314 {
2315 /* C++20 CA104: Substitute directly into the
2316 constraint-expression. */
2317 tree fargs = DECL_TI_ARGS (fn);
2318 tsubst_flags_t complain = tf_none;
2319 freq = tsubst_constraint_info (freq, fargs, complain, fn);
2320 if (!cp_tree_equal (freq, dreq))
2321 continue;
2322 }
2323
2324 candidates = tree_cons (NULL_TREE, fn, candidates);
2325 continue;
2326 }
2327
2328 /* See whether this function might be a specialization of this
2329 template. Suppress access control because we might be trying
2330 to make this specialization a friend, and we have already done
2331 access control for the declaration of the specialization. */
2332 push_deferring_access_checks (dk_no_check);
2333 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2334 pop_deferring_access_checks ();
2335
2336 if (!targs)
2337 /* We cannot deduce template arguments that when used to
2338 specialize TMPL will produce DECL. */
2339 continue;
2340
2341 if (uses_template_parms (targs))
2342 /* We deduced something involving 'auto', which isn't a valid
2343 template argument. */
2344 continue;
2345
2346 /* Save this template, and the arguments deduced. */
2347 templates = tree_cons (targs, fn, templates);
2348 }
2349 else if (need_member_template)
2350 /* FN is an ordinary member function, and we need a
2351 specialization of a member template. */
2352 ;
2353 else if (TREE_CODE (fn) != FUNCTION_DECL)
2354 /* We can get IDENTIFIER_NODEs here in certain erroneous
2355 cases. */
2356 ;
2357 else if (!DECL_FUNCTION_MEMBER_P (fn))
2358 /* This is just an ordinary non-member function. Nothing can
2359 be a specialization of that. */
2360 ;
2361 else if (DECL_ARTIFICIAL (fn))
2362 /* Cannot specialize functions that are created implicitly. */
2363 ;
2364 else
2365 {
2366 tree decl_arg_types;
2367
2368 /* This is an ordinary member function. However, since
2369 we're here, we can assume its enclosing class is a
2370 template class. For example,
2371
2372 template <typename T> struct S { void f(); };
2373 template <> void S<int>::f() {}
2374
2375 Here, S<int>::f is a non-template, but S<int> is a
2376 template class. If FN has the same type as DECL, we
2377 might be in business. */
2378
2379 if (!DECL_TEMPLATE_INFO (fn))
2380 /* Its enclosing class is an explicit specialization
2381 of a template class. This is not a candidate. */
2382 continue;
2383
2384 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2385 TREE_TYPE (TREE_TYPE (fn))))
2386 /* The return types differ. */
2387 continue;
2388
2389 /* Adjust the type of DECL in case FN is a static member. */
2390 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2391 if (DECL_STATIC_FUNCTION_P (fn)
2392 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2393 decl_arg_types = TREE_CHAIN (decl_arg_types);
2394
2395 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2396 decl_arg_types))
2397 continue;
2398
2399 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2400 && (type_memfn_rqual (TREE_TYPE (decl))
2401 != type_memfn_rqual (TREE_TYPE (fn))))
2402 continue;
2403
2404 // If the deduced arguments do not satisfy the constraints,
2405 // this is not a candidate.
2406 if (flag_concepts && !constraints_satisfied_p (fn))
2407 continue;
2408
2409 // Add the candidate.
2410 candidates = tree_cons (NULL_TREE, fn, candidates);
2411 }
2412 }
2413
2414 if (templates && TREE_CHAIN (templates))
2415 {
2416 /* We have:
2417
2418 [temp.expl.spec]
2419
2420 It is possible for a specialization with a given function
2421 signature to be instantiated from more than one function
2422 template. In such cases, explicit specification of the
2423 template arguments must be used to uniquely identify the
2424 function template specialization being specialized.
2425
2426 Note that here, there's no suggestion that we're supposed to
2427 determine which of the candidate templates is most
2428 specialized. However, we, also have:
2429
2430 [temp.func.order]
2431
2432 Partial ordering of overloaded function template
2433 declarations is used in the following contexts to select
2434 the function template to which a function template
2435 specialization refers:
2436
2437 -- when an explicit specialization refers to a function
2438 template.
2439
2440 So, we do use the partial ordering rules, at least for now.
2441 This extension can only serve to make invalid programs valid,
2442 so it's safe. And, there is strong anecdotal evidence that
2443 the committee intended the partial ordering rules to apply;
2444 the EDG front end has that behavior, and John Spicer claims
2445 that the committee simply forgot to delete the wording in
2446 [temp.expl.spec]. */
2447 tree tmpl = most_specialized_instantiation (templates);
2448 if (tmpl != error_mark_node)
2449 {
2450 templates = tmpl;
2451 TREE_CHAIN (templates) = NULL_TREE;
2452 }
2453 }
2454
2455 // Concepts allows multiple declarations of member functions
2456 // with the same signature. Like above, we need to rely on
2457 // on the partial ordering of those candidates to determine which
2458 // is the best.
2459 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2460 {
2461 if (tree cand = most_constrained_function (candidates))
2462 {
2463 candidates = cand;
2464 TREE_CHAIN (cand) = NULL_TREE;
2465 }
2466 }
2467
2468 if (templates == NULL_TREE && candidates == NULL_TREE)
2469 {
2470 error ("template-id %qD for %q+D does not match any template "
2471 "declaration", template_id, decl);
2472 if (header_mismatch)
2473 inform (DECL_SOURCE_LOCATION (decl),
2474 "saw %d %<template<>%>, need %d for "
2475 "specializing a member function template",
2476 header_count, template_count + 1);
2477 print_candidates (orig_fns);
2478 return error_mark_node;
2479 }
2480 else if ((templates && TREE_CHAIN (templates))
2481 || (candidates && TREE_CHAIN (candidates))
2482 || (templates && candidates))
2483 {
2484 error ("ambiguous template specialization %qD for %q+D",
2485 template_id, decl);
2486 candidates = chainon (candidates, templates);
2487 print_candidates (candidates);
2488 return error_mark_node;
2489 }
2490
2491 /* We have one, and exactly one, match. */
2492 if (candidates)
2493 {
2494 tree fn = TREE_VALUE (candidates);
2495 *targs_out = copy_node (DECL_TI_ARGS (fn));
2496
2497 /* Propagate the candidate's constraints to the declaration. */
2498 if (tsk != tsk_template)
2499 set_constraints (decl, get_constraints (fn));
2500
2501 /* DECL is a re-declaration or partial instantiation of a template
2502 function. */
2503 if (TREE_CODE (fn) == TEMPLATE_DECL)
2504 return fn;
2505 /* It was a specialization of an ordinary member function in a
2506 template class. */
2507 return DECL_TI_TEMPLATE (fn);
2508 }
2509
2510 /* It was a specialization of a template. */
2511 tree tmpl = TREE_VALUE (templates);
2512 *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
2513
2514 /* Propagate the template's constraints to the declaration. */
2515 if (tsk != tsk_template)
2516 set_constraints (decl, get_constraints (tmpl));
2517
2518 return tmpl;
2519 }
2520
2521 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2522 but with the default argument values filled in from those in the
2523 TMPL_TYPES. */
2524
2525 static tree
2526 copy_default_args_to_explicit_spec_1 (tree spec_types,
2527 tree tmpl_types)
2528 {
2529 tree new_spec_types;
2530
2531 if (!spec_types)
2532 return NULL_TREE;
2533
2534 if (spec_types == void_list_node)
2535 return void_list_node;
2536
2537 /* Substitute into the rest of the list. */
2538 new_spec_types =
2539 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2540 TREE_CHAIN (tmpl_types));
2541
2542 /* Add the default argument for this parameter. */
2543 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2544 TREE_VALUE (spec_types),
2545 new_spec_types);
2546 }
2547
2548 /* DECL is an explicit specialization. Replicate default arguments
2549 from the template it specializes. (That way, code like:
2550
2551 template <class T> void f(T = 3);
2552 template <> void f(double);
2553 void g () { f (); }
2554
2555 works, as required.) An alternative approach would be to look up
2556 the correct default arguments at the call-site, but this approach
2557 is consistent with how implicit instantiations are handled. */
2558
2559 static void
2560 copy_default_args_to_explicit_spec (tree decl)
2561 {
2562 tree tmpl;
2563 tree spec_types;
2564 tree tmpl_types;
2565 tree new_spec_types;
2566 tree old_type;
2567 tree new_type;
2568 tree t;
2569 tree object_type = NULL_TREE;
2570 tree in_charge = NULL_TREE;
2571 tree vtt = NULL_TREE;
2572
2573 /* See if there's anything we need to do. */
2574 tmpl = DECL_TI_TEMPLATE (decl);
2575 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2576 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2577 if (TREE_PURPOSE (t))
2578 break;
2579 if (!t)
2580 return;
2581
2582 old_type = TREE_TYPE (decl);
2583 spec_types = TYPE_ARG_TYPES (old_type);
2584
2585 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
2586 {
2587 /* Remove the this pointer, but remember the object's type for
2588 CV quals. */
2589 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2590 spec_types = TREE_CHAIN (spec_types);
2591 tmpl_types = TREE_CHAIN (tmpl_types);
2592
2593 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2594 {
2595 /* DECL may contain more parameters than TMPL due to the extra
2596 in-charge parameter in constructors and destructors. */
2597 in_charge = spec_types;
2598 spec_types = TREE_CHAIN (spec_types);
2599 }
2600 if (DECL_HAS_VTT_PARM_P (decl))
2601 {
2602 vtt = spec_types;
2603 spec_types = TREE_CHAIN (spec_types);
2604 }
2605 }
2606
2607 /* Compute the merged default arguments. */
2608 new_spec_types =
2609 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2610
2611 /* Compute the new FUNCTION_TYPE. */
2612 if (object_type)
2613 {
2614 if (vtt)
2615 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2616 TREE_VALUE (vtt),
2617 new_spec_types);
2618
2619 if (in_charge)
2620 /* Put the in-charge parameter back. */
2621 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2622 TREE_VALUE (in_charge),
2623 new_spec_types);
2624
2625 new_type = build_method_type_directly (object_type,
2626 TREE_TYPE (old_type),
2627 new_spec_types);
2628 }
2629 else
2630 new_type = build_function_type (TREE_TYPE (old_type),
2631 new_spec_types);
2632 new_type = cp_build_type_attribute_variant (new_type,
2633 TYPE_ATTRIBUTES (old_type));
2634 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2635
2636 TREE_TYPE (decl) = new_type;
2637 }
2638
2639 /* Return the number of template headers we expect to see for a definition
2640 or specialization of CTYPE or one of its non-template members. */
2641
2642 int
2643 num_template_headers_for_class (tree ctype)
2644 {
2645 int num_templates = 0;
2646
2647 while (ctype && CLASS_TYPE_P (ctype))
2648 {
2649 /* You're supposed to have one `template <...>' for every
2650 template class, but you don't need one for a full
2651 specialization. For example:
2652
2653 template <class T> struct S{};
2654 template <> struct S<int> { void f(); };
2655 void S<int>::f () {}
2656
2657 is correct; there shouldn't be a `template <>' for the
2658 definition of `S<int>::f'. */
2659 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2660 /* If CTYPE does not have template information of any
2661 kind, then it is not a template, nor is it nested
2662 within a template. */
2663 break;
2664 if (explicit_class_specialization_p (ctype))
2665 break;
2666 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2667 ++num_templates;
2668
2669 ctype = TYPE_CONTEXT (ctype);
2670 }
2671
2672 return num_templates;
2673 }
2674
2675 /* Do a simple sanity check on the template headers that precede the
2676 variable declaration DECL. */
2677
2678 void
2679 check_template_variable (tree decl)
2680 {
2681 tree ctx = CP_DECL_CONTEXT (decl);
2682 int wanted = num_template_headers_for_class (ctx);
2683 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2684 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2685 {
2686 if (cxx_dialect < cxx14)
2687 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2688 "variable templates only available with "
2689 "%<-std=c++14%> or %<-std=gnu++14%>");
2690
2691 // Namespace-scope variable templates should have a template header.
2692 ++wanted;
2693 }
2694 if (template_header_count > wanted)
2695 {
2696 auto_diagnostic_group d;
2697 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2698 "too many template headers for %qD "
2699 "(should be %d)",
2700 decl, wanted);
2701 if (warned && CLASS_TYPE_P (ctx)
2702 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2703 inform (DECL_SOURCE_LOCATION (decl),
2704 "members of an explicitly specialized class are defined "
2705 "without a template header");
2706 }
2707 }
2708
2709 /* An explicit specialization whose declarator-id or class-head-name is not
2710 qualified shall be declared in the nearest enclosing namespace of the
2711 template, or, if the namespace is inline (7.3.1), any namespace from its
2712 enclosing namespace set.
2713
2714 If the name declared in the explicit instantiation is an unqualified name,
2715 the explicit instantiation shall appear in the namespace where its template
2716 is declared or, if that namespace is inline (7.3.1), any namespace from its
2717 enclosing namespace set. */
2718
2719 void
2720 check_unqualified_spec_or_inst (tree t, location_t loc)
2721 {
2722 tree tmpl = most_general_template (t);
2723 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2724 && !is_nested_namespace (current_namespace,
2725 CP_DECL_CONTEXT (tmpl), true))
2726 {
2727 if (processing_specialization)
2728 permerror (loc, "explicit specialization of %qD outside its "
2729 "namespace must use a nested-name-specifier", tmpl);
2730 else if (processing_explicit_instantiation
2731 && cxx_dialect >= cxx11)
2732 /* This was allowed in C++98, so only pedwarn. */
2733 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2734 "outside its namespace must use a nested-name-"
2735 "specifier", tmpl);
2736 }
2737 }
2738
2739 /* Warn for a template specialization SPEC that is missing some of a set
2740 of function or type attributes that the template TEMPL is declared with.
2741 ATTRLIST is a list of additional attributes that SPEC should be taken
2742 to ultimately be declared with. */
2743
2744 static void
2745 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2746 {
2747 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2748 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2749
2750 /* Avoid warning if the difference between the primary and
2751 the specialization is not in one of the attributes below. */
2752 const char* const blacklist[] = {
2753 "alloc_align", "alloc_size", "assume_aligned", "format",
2754 "format_arg", "malloc", "nonnull", NULL
2755 };
2756
2757 /* Put together a list of the black listed attributes that the primary
2758 template is declared with that the specialization is not, in case
2759 it's not apparent from the most recent declaration of the primary. */
2760 pretty_printer str;
2761 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2762 blacklist, &str);
2763
2764 if (!nattrs)
2765 return;
2766
2767 auto_diagnostic_group d;
2768 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2769 "explicit specialization %q#D may be missing attributes",
2770 spec))
2771 inform (DECL_SOURCE_LOCATION (tmpl),
2772 nattrs > 1
2773 ? G_("missing primary template attributes %s")
2774 : G_("missing primary template attribute %s"),
2775 pp_formatted_text (&str));
2776 }
2777
2778 /* Check to see if the function just declared, as indicated in
2779 DECLARATOR, and in DECL, is a specialization of a function
2780 template. We may also discover that the declaration is an explicit
2781 instantiation at this point.
2782
2783 Returns DECL, or an equivalent declaration that should be used
2784 instead if all goes well. Issues an error message if something is
2785 amiss. Returns error_mark_node if the error is not easily
2786 recoverable.
2787
2788 FLAGS is a bitmask consisting of the following flags:
2789
2790 2: The function has a definition.
2791 4: The function is a friend.
2792
2793 The TEMPLATE_COUNT is the number of references to qualifying
2794 template classes that appeared in the name of the function. For
2795 example, in
2796
2797 template <class T> struct S { void f(); };
2798 void S<int>::f();
2799
2800 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2801 classes are not counted in the TEMPLATE_COUNT, so that in
2802
2803 template <class T> struct S {};
2804 template <> struct S<int> { void f(); }
2805 template <> void S<int>::f();
2806
2807 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2808 invalid; there should be no template <>.)
2809
2810 If the function is a specialization, it is marked as such via
2811 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2812 is set up correctly, and it is added to the list of specializations
2813 for that template. */
2814
2815 tree
2816 check_explicit_specialization (tree declarator,
2817 tree decl,
2818 int template_count,
2819 int flags,
2820 tree attrlist)
2821 {
2822 int have_def = flags & 2;
2823 int is_friend = flags & 4;
2824 bool is_concept = flags & 8;
2825 int specialization = 0;
2826 int explicit_instantiation = 0;
2827 int member_specialization = 0;
2828 tree ctype = DECL_CLASS_CONTEXT (decl);
2829 tree dname = DECL_NAME (decl);
2830 tmpl_spec_kind tsk;
2831
2832 if (is_friend)
2833 {
2834 if (!processing_specialization)
2835 tsk = tsk_none;
2836 else
2837 tsk = tsk_excessive_parms;
2838 }
2839 else
2840 tsk = current_tmpl_spec_kind (template_count);
2841
2842 switch (tsk)
2843 {
2844 case tsk_none:
2845 if (processing_specialization && !VAR_P (decl))
2846 {
2847 specialization = 1;
2848 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2849 }
2850 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
2851 || (DECL_LANG_SPECIFIC (decl)
2852 && DECL_IMPLICIT_INSTANTIATION (decl)))
2853 {
2854 if (is_friend)
2855 /* This could be something like:
2856
2857 template <class T> void f(T);
2858 class S { friend void f<>(int); } */
2859 specialization = 1;
2860 else
2861 {
2862 /* This case handles bogus declarations like template <>
2863 template <class T> void f<int>(); */
2864
2865 error_at (cp_expr_loc_or_input_loc (declarator),
2866 "template-id %qE in declaration of primary template",
2867 declarator);
2868 return decl;
2869 }
2870 }
2871 break;
2872
2873 case tsk_invalid_member_spec:
2874 /* The error has already been reported in
2875 check_specialization_scope. */
2876 return error_mark_node;
2877
2878 case tsk_invalid_expl_inst:
2879 error ("template parameter list used in explicit instantiation");
2880
2881 /* Fall through. */
2882
2883 case tsk_expl_inst:
2884 if (have_def)
2885 error ("definition provided for explicit instantiation");
2886
2887 explicit_instantiation = 1;
2888 break;
2889
2890 case tsk_excessive_parms:
2891 case tsk_insufficient_parms:
2892 if (tsk == tsk_excessive_parms)
2893 error ("too many template parameter lists in declaration of %qD",
2894 decl);
2895 else if (template_header_count)
2896 error("too few template parameter lists in declaration of %qD", decl);
2897 else
2898 error("explicit specialization of %qD must be introduced by "
2899 "%<template <>%>", decl);
2900
2901 /* Fall through. */
2902 case tsk_expl_spec:
2903 if (is_concept)
2904 error ("explicit specialization declared %<concept%>");
2905
2906 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2907 /* In cases like template<> constexpr bool v = true;
2908 We'll give an error in check_template_variable. */
2909 break;
2910
2911 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2912 if (ctype)
2913 member_specialization = 1;
2914 else
2915 specialization = 1;
2916 break;
2917
2918 case tsk_template:
2919 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2920 {
2921 /* This case handles bogus declarations like template <>
2922 template <class T> void f<int>(); */
2923
2924 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2925 error_at (cp_expr_loc_or_input_loc (declarator),
2926 "template-id %qE in declaration of primary template",
2927 declarator);
2928 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2929 {
2930 /* Partial specialization of variable template. */
2931 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2932 specialization = 1;
2933 goto ok;
2934 }
2935 else if (cxx_dialect < cxx14)
2936 error_at (cp_expr_loc_or_input_loc (declarator),
2937 "non-type partial specialization %qE "
2938 "is not allowed", declarator);
2939 else
2940 error_at (cp_expr_loc_or_input_loc (declarator),
2941 "non-class, non-variable partial specialization %qE "
2942 "is not allowed", declarator);
2943 return decl;
2944 ok:;
2945 }
2946
2947 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2948 /* This is a specialization of a member template, without
2949 specialization the containing class. Something like:
2950
2951 template <class T> struct S {
2952 template <class U> void f (U);
2953 };
2954 template <> template <class U> void S<int>::f(U) {}
2955
2956 That's a specialization -- but of the entire template. */
2957 specialization = 1;
2958 break;
2959
2960 default:
2961 gcc_unreachable ();
2962 }
2963
2964 if ((specialization || member_specialization)
2965 /* This doesn't apply to variable templates. */
2966 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2967 {
2968 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2969 for (; t; t = TREE_CHAIN (t))
2970 if (TREE_PURPOSE (t))
2971 {
2972 permerror (input_location,
2973 "default argument specified in explicit specialization");
2974 break;
2975 }
2976 }
2977
2978 if (specialization || member_specialization || explicit_instantiation)
2979 {
2980 tree tmpl = NULL_TREE;
2981 tree targs = NULL_TREE;
2982 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2983 bool found_hidden = false;
2984
2985 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2986 if (!was_template_id)
2987 {
2988 tree fns;
2989
2990 gcc_assert (identifier_p (declarator));
2991 if (ctype)
2992 fns = dname;
2993 else
2994 {
2995 /* If there is no class context, the explicit instantiation
2996 must be at namespace scope. */
2997 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2998
2999 /* Find the namespace binding, using the declaration
3000 context. */
3001 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3002 LOOK_want::NORMAL, true);
3003 if (fns == error_mark_node)
3004 {
3005 /* If lookup fails, look for a friend declaration so we can
3006 give a better diagnostic. */
3007 fns = (lookup_qualified_name
3008 (CP_DECL_CONTEXT (decl), dname,
3009 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3010 /*complain*/true));
3011 found_hidden = true;
3012 }
3013
3014 if (fns == error_mark_node || !is_overloaded_fn (fns))
3015 {
3016 error ("%qD is not a template function", dname);
3017 fns = error_mark_node;
3018 }
3019 }
3020
3021 declarator = lookup_template_function (fns, NULL_TREE);
3022 }
3023
3024 if (declarator == error_mark_node)
3025 return error_mark_node;
3026
3027 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3028 {
3029 if (!explicit_instantiation)
3030 /* A specialization in class scope. This is invalid,
3031 but the error will already have been flagged by
3032 check_specialization_scope. */
3033 return error_mark_node;
3034 else
3035 {
3036 /* It's not valid to write an explicit instantiation in
3037 class scope, e.g.:
3038
3039 class C { template void f(); }
3040
3041 This case is caught by the parser. However, on
3042 something like:
3043
3044 template class C { void f(); };
3045
3046 (which is invalid) we can get here. The error will be
3047 issued later. */
3048 ;
3049 }
3050
3051 return decl;
3052 }
3053 else if (ctype != NULL_TREE
3054 && (identifier_p (TREE_OPERAND (declarator, 0))))
3055 {
3056 // We'll match variable templates in start_decl.
3057 if (VAR_P (decl))
3058 return decl;
3059
3060 /* Find the list of functions in ctype that have the same
3061 name as the declared function. */
3062 tree name = TREE_OPERAND (declarator, 0);
3063
3064 if (constructor_name_p (name, ctype))
3065 {
3066 if (DECL_CONSTRUCTOR_P (decl)
3067 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3068 : !CLASSTYPE_DESTRUCTOR (ctype))
3069 {
3070 /* From [temp.expl.spec]:
3071
3072 If such an explicit specialization for the member
3073 of a class template names an implicitly-declared
3074 special member function (clause _special_), the
3075 program is ill-formed.
3076
3077 Similar language is found in [temp.explicit]. */
3078 error ("specialization of implicitly-declared special member function");
3079 return error_mark_node;
3080 }
3081
3082 name = DECL_NAME (decl);
3083 }
3084
3085 /* For a type-conversion operator, We might be looking for
3086 `operator int' which will be a specialization of
3087 `operator T'. Grab all the conversion operators, and
3088 then select from them. */
3089 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3090 ? conv_op_identifier : name);
3091
3092 if (fns == NULL_TREE)
3093 {
3094 error ("no member function %qD declared in %qT", name, ctype);
3095 return error_mark_node;
3096 }
3097 else
3098 TREE_OPERAND (declarator, 0) = fns;
3099 }
3100
3101 /* Figure out what exactly is being specialized at this point.
3102 Note that for an explicit instantiation, even one for a
3103 member function, we cannot tell a priori whether the
3104 instantiation is for a member template, or just a member
3105 function of a template class. Even if a member template is
3106 being instantiated, the member template arguments may be
3107 elided if they can be deduced from the rest of the
3108 declaration. */
3109 tmpl = determine_specialization (declarator, decl,
3110 &targs,
3111 member_specialization,
3112 template_count,
3113 tsk);
3114
3115 if (!tmpl || tmpl == error_mark_node)
3116 /* We couldn't figure out what this declaration was
3117 specializing. */
3118 return error_mark_node;
3119 else
3120 {
3121 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3122 {
3123 auto_diagnostic_group d;
3124 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3125 "friend declaration %qD is not visible to "
3126 "explicit specialization", tmpl))
3127 inform (DECL_SOURCE_LOCATION (tmpl),
3128 "friend declaration here");
3129 }
3130
3131 if (!ctype && !is_friend
3132 && CP_DECL_CONTEXT (decl) == current_namespace)
3133 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3134
3135 tree gen_tmpl = most_general_template (tmpl);
3136
3137 if (explicit_instantiation)
3138 {
3139 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3140 is done by do_decl_instantiation later. */
3141
3142 int arg_depth = TMPL_ARGS_DEPTH (targs);
3143 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3144
3145 if (arg_depth > parm_depth)
3146 {
3147 /* If TMPL is not the most general template (for
3148 example, if TMPL is a friend template that is
3149 injected into namespace scope), then there will
3150 be too many levels of TARGS. Remove some of them
3151 here. */
3152 int i;
3153 tree new_targs;
3154
3155 new_targs = make_tree_vec (parm_depth);
3156 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3157 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3158 = TREE_VEC_ELT (targs, i);
3159 targs = new_targs;
3160 }
3161
3162 return instantiate_template (tmpl, targs, tf_error);
3163 }
3164
3165 /* If we thought that the DECL was a member function, but it
3166 turns out to be specializing a static member function,
3167 make DECL a static member function as well. */
3168 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3169 && DECL_STATIC_FUNCTION_P (tmpl)
3170 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
3171 revert_static_member_fn (decl);
3172
3173 /* If this is a specialization of a member template of a
3174 template class, we want to return the TEMPLATE_DECL, not
3175 the specialization of it. */
3176 if (tsk == tsk_template && !was_template_id)
3177 {
3178 tree result = DECL_TEMPLATE_RESULT (tmpl);
3179 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3180 DECL_INITIAL (result) = NULL_TREE;
3181 if (have_def)
3182 {
3183 tree parm;
3184 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3185 DECL_SOURCE_LOCATION (result)
3186 = DECL_SOURCE_LOCATION (decl);
3187 /* We want to use the argument list specified in the
3188 definition, not in the original declaration. */
3189 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3190 for (parm = DECL_ARGUMENTS (result); parm;
3191 parm = DECL_CHAIN (parm))
3192 DECL_CONTEXT (parm) = result;
3193 }
3194 decl = register_specialization (tmpl, gen_tmpl, targs,
3195 is_friend, 0);
3196 remove_contract_attributes (result);
3197 return decl;
3198 }
3199
3200 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3201 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3202
3203 if (was_template_id)
3204 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3205
3206 /* Inherit default function arguments from the template
3207 DECL is specializing. */
3208 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3209 copy_default_args_to_explicit_spec (decl);
3210
3211 /* This specialization has the same protection as the
3212 template it specializes. */
3213 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3214 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3215
3216 /* 7.1.1-1 [dcl.stc]
3217
3218 A storage-class-specifier shall not be specified in an
3219 explicit specialization...
3220
3221 The parser rejects these, so unless action is taken here,
3222 explicit function specializations will always appear with
3223 global linkage.
3224
3225 The action recommended by the C++ CWG in response to C++
3226 defect report 605 is to make the storage class and linkage
3227 of the explicit specialization match the templated function:
3228
3229 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3230 */
3231 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3232 {
3233 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3234 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3235
3236 /* A concept cannot be specialized. */
3237 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3238 {
3239 error ("explicit specialization of function concept %qD",
3240 gen_tmpl);
3241 return error_mark_node;
3242 }
3243
3244 /* This specialization has the same linkage and visibility as
3245 the function template it specializes. */
3246 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3247 if (! TREE_PUBLIC (decl))
3248 {
3249 DECL_INTERFACE_KNOWN (decl) = 1;
3250 DECL_NOT_REALLY_EXTERN (decl) = 1;
3251 }
3252 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3253 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3254 {
3255 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3256 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3257 }
3258 }
3259
3260 /* If DECL is a friend declaration, declared using an
3261 unqualified name, the namespace associated with DECL may
3262 have been set incorrectly. For example, in:
3263
3264 template <typename T> void f(T);
3265 namespace N {
3266 struct S { friend void f<int>(int); }
3267 }
3268
3269 we will have set the DECL_CONTEXT for the friend
3270 declaration to N, rather than to the global namespace. */
3271 if (DECL_NAMESPACE_SCOPE_P (decl))
3272 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3273
3274 if (is_friend && !have_def)
3275 /* This is not really a declaration of a specialization.
3276 It's just the name of an instantiation. But, it's not
3277 a request for an instantiation, either. */
3278 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3279 else if (TREE_CODE (decl) == FUNCTION_DECL)
3280 /* A specialization is not necessarily COMDAT. */
3281 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3282 && DECL_DECLARED_INLINE_P (decl));
3283 else if (VAR_P (decl))
3284 DECL_COMDAT (decl) = false;
3285
3286 /* If this is a full specialization, register it so that we can find
3287 it again. Partial specializations will be registered in
3288 process_partial_specialization. */
3289 if (!processing_template_decl)
3290 {
3291 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3292
3293 decl = register_specialization (decl, gen_tmpl, targs,
3294 is_friend, 0);
3295 }
3296
3297 /* If this is a specialization, splice any contracts that may have
3298 been inherited from the template, removing them. */
3299 if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
3300 remove_contract_attributes (decl);
3301
3302 /* A 'structor should already have clones. */
3303 gcc_assert (decl == error_mark_node
3304 || variable_template_p (tmpl)
3305 || !(DECL_CONSTRUCTOR_P (decl)
3306 || DECL_DESTRUCTOR_P (decl))
3307 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3308 }
3309 }
3310
3311 return decl;
3312 }
3313
3314 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3315 parameters. These are represented in the same format used for
3316 DECL_TEMPLATE_PARMS. */
3317
3318 int
3319 comp_template_parms (const_tree parms1, const_tree parms2)
3320 {
3321 if (parms1 == parms2)
3322 return 1;
3323
3324 tree t1 = TREE_VALUE (parms1);
3325 tree t2 = TREE_VALUE (parms2);
3326 int i;
3327
3328 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3329 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3330
3331 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3332 return 0;
3333
3334 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3335 {
3336 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3337 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3338
3339 /* If either of the template parameters are invalid, assume
3340 they match for the sake of error recovery. */
3341 if (error_operand_p (parm1) || error_operand_p (parm2))
3342 return 1;
3343
3344 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3345 return 0;
3346
3347 if (TREE_CODE (parm1) == TYPE_DECL
3348 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
3349 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
3350 continue;
3351 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3352 return 0;
3353 }
3354
3355 return 1;
3356 }
3357
3358 /* Returns true if two template parameters are declared with
3359 equivalent constraints. */
3360
3361 static bool
3362 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3363 {
3364 tree req1 = TREE_TYPE (parm1);
3365 tree req2 = TREE_TYPE (parm2);
3366 if (!req1 != !req2)
3367 return false;
3368 if (req1)
3369 return cp_tree_equal (req1, req2);
3370 return true;
3371 }
3372
3373 /* Returns true when two template parameters are equivalent. */
3374
3375 static bool
3376 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3377 {
3378 tree decl1 = TREE_VALUE (parm1);
3379 tree decl2 = TREE_VALUE (parm2);
3380
3381 /* If either of the template parameters are invalid, assume
3382 they match for the sake of error recovery. */
3383 if (error_operand_p (decl1) || error_operand_p (decl2))
3384 return true;
3385
3386 /* ... they declare parameters of the same kind. */
3387 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3388 return false;
3389
3390 /* ... one parameter was introduced by a parameter declaration, then
3391 both are. This case arises as a result of eagerly rewriting declarations
3392 during parsing. */
3393 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
3394 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
3395 return false;
3396
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3399 return false;
3400
3401 if (TREE_CODE (decl1) == PARM_DECL)
3402 {
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3405 return false;
3406 }
3407 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3408 {
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1, decl2))
3412 return false;
3413 }
3414
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1, parm2);
3418 }
3419
3420 /* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3423
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3426
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3430
3431 static bool
3432 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3433 {
3434 if (parms1 == parms2)
3435 return true;
3436
3437 tree list1 = TREE_VALUE (parms1);
3438 tree list2 = TREE_VALUE (parms2);
3439
3440 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3441 return 0;
3442
3443 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3444 {
3445 tree parm1 = TREE_VEC_ELT (list1, i);
3446 tree parm2 = TREE_VEC_ELT (list2, i);
3447 if (!template_parameters_equivalent_p (parm1, parm2))
3448 return false;
3449 }
3450
3451 return true;
3452 }
3453
3454 /* Return true if the requires-clause of the template parameter lists are
3455 equivalent and false otherwise. */
3456 static bool
3457 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3458 {
3459 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3460 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3461 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3462 return false;
3463 if (!cp_tree_equal (req1, req2))
3464 return false;
3465 return true;
3466 }
3467
3468 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3469 Two template heads are equivalent if their template parameter
3470 lists are equivalent and their requires clauses are equivalent.
3471
3472 In pre-C++20, this is equivalent to calling comp_template_parms
3473 for the template parameters of TMPL1 and TMPL2. */
3474
3475 bool
3476 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3477 {
3478 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3479 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3480
3481 /* Don't change the matching rules for pre-C++20. */
3482 if (cxx_dialect < cxx20)
3483 return comp_template_parms (parms1, parms2);
3484
3485 /* ... have the same number of template parameters, and their
3486 corresponding parameters are equivalent. */
3487 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3488 return false;
3489
3490 /* ... if either has a requires-clause, they both do and their
3491 corresponding constraint-expressions are equivalent. */
3492 return template_requirements_equivalent_p (parms1, parms2);
3493 }
3494
3495 /* Determine whether PARM is a parameter pack. */
3496
3497 bool
3498 template_parameter_pack_p (const_tree parm)
3499 {
3500 /* Determine if we have a non-type template parameter pack. */
3501 if (TREE_CODE (parm) == PARM_DECL)
3502 return (DECL_TEMPLATE_PARM_P (parm)
3503 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3504 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3505 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3506
3507 /* If this is a list of template parameters, we could get a
3508 TYPE_DECL or a TEMPLATE_DECL. */
3509 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3510 parm = TREE_TYPE (parm);
3511
3512 /* Otherwise it must be a type template parameter. */
3513 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3514 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3515 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3516 }
3517
3518 /* Determine if T is a function parameter pack. */
3519
3520 bool
3521 function_parameter_pack_p (const_tree t)
3522 {
3523 if (t && TREE_CODE (t) == PARM_DECL)
3524 return DECL_PACK_P (t);
3525 return false;
3526 }
3527
3528 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3529 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3530
3531 tree
3532 get_function_template_decl (const_tree primary_func_tmpl_inst)
3533 {
3534 if (! primary_func_tmpl_inst
3535 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3536 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3537 return NULL;
3538
3539 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3540 }
3541
3542 /* Return true iff the function parameter PARAM_DECL was expanded
3543 from the function parameter pack PACK. */
3544
3545 bool
3546 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3547 {
3548 if (DECL_ARTIFICIAL (param_decl)
3549 || !function_parameter_pack_p (pack))
3550 return false;
3551
3552 /* The parameter pack and its pack arguments have the same
3553 DECL_PARM_INDEX. */
3554 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3555 }
3556
3557 /* Determine whether ARGS describes a variadic template args list,
3558 i.e., one that is terminated by a template argument pack. */
3559
3560 static bool
3561 template_args_variadic_p (tree args)
3562 {
3563 int nargs;
3564 tree last_parm;
3565
3566 if (args == NULL_TREE)
3567 return false;
3568
3569 args = INNERMOST_TEMPLATE_ARGS (args);
3570 nargs = TREE_VEC_LENGTH (args);
3571
3572 if (nargs == 0)
3573 return false;
3574
3575 last_parm = TREE_VEC_ELT (args, nargs - 1);
3576
3577 return ARGUMENT_PACK_P (last_parm);
3578 }
3579
3580 /* Generate a new name for the parameter pack name NAME (an
3581 IDENTIFIER_NODE) that incorporates its */
3582
3583 static tree
3584 make_ith_pack_parameter_name (tree name, int i)
3585 {
3586 /* Munge the name to include the parameter index. */
3587 #define NUMBUF_LEN 128
3588 char numbuf[NUMBUF_LEN];
3589 char* newname;
3590 int newname_len;
3591
3592 if (name == NULL_TREE)
3593 return name;
3594 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3595 newname_len = IDENTIFIER_LENGTH (name)
3596 + strlen (numbuf) + 2;
3597 newname = (char*)alloca (newname_len);
3598 snprintf (newname, newname_len,
3599 "%s#%i", IDENTIFIER_POINTER (name), i);
3600 return get_identifier (newname);
3601 }
3602
3603 /* Return true if T is a primary function, class or alias template
3604 specialization, not including the template pattern. */
3605
3606 bool
3607 primary_template_specialization_p (const_tree t)
3608 {
3609 if (!t)
3610 return false;
3611
3612 if (VAR_OR_FUNCTION_DECL_P (t))
3613 return (DECL_LANG_SPECIFIC (t)
3614 && DECL_USE_TEMPLATE (t)
3615 && DECL_TEMPLATE_INFO (t)
3616 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3617 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3618 return (CLASSTYPE_TEMPLATE_INFO (t)
3619 && CLASSTYPE_USE_TEMPLATE (t)
3620 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3621 else if (alias_template_specialization_p (t, nt_transparent))
3622 return true;
3623 return false;
3624 }
3625
3626 /* Return true if PARM is a template template parameter. */
3627
3628 bool
3629 template_template_parameter_p (const_tree parm)
3630 {
3631 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3632 }
3633
3634 /* Return true iff PARM is a DECL representing a type template
3635 parameter. */
3636
3637 bool
3638 template_type_parameter_p (const_tree parm)
3639 {
3640 return (parm
3641 && (TREE_CODE (parm) == TYPE_DECL
3642 || TREE_CODE (parm) == TEMPLATE_DECL)
3643 && DECL_TEMPLATE_PARM_P (parm));
3644 }
3645
3646 /* Return the template parameters of T if T is a
3647 primary template instantiation, NULL otherwise. */
3648
3649 tree
3650 get_primary_template_innermost_parameters (const_tree t)
3651 {
3652 tree parms = NULL, template_info = NULL;
3653
3654 if ((template_info = get_template_info (t))
3655 && primary_template_specialization_p (t))
3656 parms = INNERMOST_TEMPLATE_PARMS
3657 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3658
3659 return parms;
3660 }
3661
3662 /* Returns the template arguments of T if T is a template instantiation,
3663 NULL otherwise. */
3664
3665 tree
3666 get_template_innermost_arguments (const_tree t)
3667 {
3668 tree args = NULL, template_info = NULL;
3669
3670 if ((template_info = get_template_info (t))
3671 && TI_ARGS (template_info))
3672 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3673
3674 return args;
3675 }
3676
3677 /* Return the argument pack elements of T if T is a template argument pack,
3678 NULL otherwise. */
3679
3680 tree
3681 get_template_argument_pack_elems (const_tree t)
3682 {
3683 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3684 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3685 return NULL;
3686
3687 return ARGUMENT_PACK_ARGS (t);
3688 }
3689
3690 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3691 ARGUMENT_PACK_SELECT represents. */
3692
3693 static tree
3694 argument_pack_select_arg (tree t)
3695 {
3696 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3697 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3698
3699 /* If the selected argument is an expansion E, that most likely means we were
3700 called from gen_elem_of_pack_expansion_instantiation during the
3701 substituting of an argument pack (of which the Ith element is a pack
3702 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3703 In this case, the Ith element resulting from this substituting is going to
3704 be a pack expansion, which pattern is the pattern of E. Let's return the
3705 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3706 resulting pack expansion from it. */
3707 if (PACK_EXPANSION_P (arg))
3708 {
3709 /* Make sure we aren't throwing away arg info. */
3710 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3711 arg = PACK_EXPANSION_PATTERN (arg);
3712 }
3713
3714 return arg;
3715 }
3716
3717 /* Return a modification of ARGS that's suitable for preserving inside a hash
3718 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3719 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3720
3721 static tree
3722 preserve_args (tree args, bool cow_p = true)
3723 {
3724 if (!args)
3725 return NULL_TREE;
3726
3727 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
3728 {
3729 tree t = TREE_VEC_ELT (args, i);
3730 tree r;
3731 if (!t)
3732 r = NULL_TREE;
3733 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
3734 r = argument_pack_select_arg (t);
3735 else if (TREE_CODE (t) == TREE_VEC)
3736 r = preserve_args (t, cow_p);
3737 else
3738 r = t;
3739 if (r != t)
3740 {
3741 if (cow_p)
3742 {
3743 args = copy_template_args (args);
3744 cow_p = false;
3745 }
3746 TREE_VEC_ELT (args, i) = r;
3747 }
3748 }
3749
3750 return args;
3751 }
3752
3753 /* True iff FN is a function representing a built-in variadic parameter
3754 pack. */
3755
3756 bool
3757 builtin_pack_fn_p (tree fn)
3758 {
3759 if (!fn
3760 || TREE_CODE (fn) != FUNCTION_DECL
3761 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3762 return false;
3763
3764 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3765 return true;
3766
3767 return false;
3768 }
3769
3770 /* True iff CALL is a call to a function representing a built-in variadic
3771 parameter pack. */
3772
3773 static bool
3774 builtin_pack_call_p (tree call)
3775 {
3776 if (TREE_CODE (call) != CALL_EXPR)
3777 return false;
3778 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3779 }
3780
3781 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3782
3783 static tree
3784 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3785 tree in_decl)
3786 {
3787 tree ohi = CALL_EXPR_ARG (call, 0);
3788 tree hi = tsubst_expr (ohi, args, complain, in_decl);
3789
3790 if (instantiation_dependent_expression_p (hi))
3791 {
3792 if (hi != ohi)
3793 {
3794 call = copy_node (call);
3795 CALL_EXPR_ARG (call, 0) = hi;
3796 }
3797 tree ex = make_pack_expansion (call, complain);
3798 tree vec = make_tree_vec (1);
3799 TREE_VEC_ELT (vec, 0) = ex;
3800 return vec;
3801 }
3802 else
3803 {
3804 hi = instantiate_non_dependent_expr (hi, complain);
3805 hi = cxx_constant_value (hi, complain);
3806 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3807
3808 /* Calculate the largest value of len that won't make the size of the vec
3809 overflow an int. The compiler will exceed resource limits long before
3810 this, but it seems a decent place to diagnose. */
3811 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3812
3813 if (len < 0 || len > max)
3814 {
3815 if ((complain & tf_error)
3816 && hi != error_mark_node)
3817 error ("argument to %<__integer_pack%> must be between 0 and %d",
3818 max);
3819 return error_mark_node;
3820 }
3821
3822 tree vec = make_tree_vec (len);
3823
3824 for (int i = 0; i < len; ++i)
3825 TREE_VEC_ELT (vec, i) = size_int (i);
3826
3827 return vec;
3828 }
3829 }
3830
3831 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3832 CALL. */
3833
3834 static tree
3835 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3836 tree in_decl)
3837 {
3838 if (!builtin_pack_call_p (call))
3839 return NULL_TREE;
3840
3841 tree fn = CALL_EXPR_FN (call);
3842
3843 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3844 return expand_integer_pack (call, args, complain, in_decl);
3845
3846 return NULL_TREE;
3847 }
3848
3849 /* Return true if the tree T has the extra args mechanism for
3850 avoiding partial instantiation. */
3851
3852 static bool
3853 has_extra_args_mechanism_p (const_tree t)
3854 {
3855 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */
3856 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */
3857 || (TREE_CODE (t) == IF_STMT
3858 && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS */
3859 || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS */
3860 }
3861
3862 /* Return *_EXTRA_ARGS of the given supported tree T. */
3863
3864 static tree&
3865 tree_extra_args (tree t)
3866 {
3867 gcc_checking_assert (has_extra_args_mechanism_p (t));
3868
3869 if (PACK_EXPANSION_P (t))
3870 return PACK_EXPANSION_EXTRA_ARGS (t);
3871 else if (TREE_CODE (t) == REQUIRES_EXPR)
3872 return REQUIRES_EXPR_EXTRA_ARGS (t);
3873 else if (TREE_CODE (t) == IF_STMT
3874 && IF_STMT_CONSTEXPR_P (t))
3875 return IF_STMT_EXTRA_ARGS (t);
3876 else if (TREE_CODE (t) == LAMBDA_EXPR)
3877 return LAMBDA_EXPR_EXTRA_ARGS (t);
3878
3879 gcc_unreachable ();
3880 }
3881
3882 /* Structure used to track the progress of find_parameter_packs_r. */
3883 struct find_parameter_pack_data
3884 {
3885 /* TREE_LIST that will contain all of the parameter packs found by
3886 the traversal. */
3887 tree* parameter_packs;
3888
3889 /* Set of AST nodes that have been visited by the traversal. */
3890 hash_set<tree> *visited;
3891
3892 /* True iff we're making a type pack expansion. */
3893 bool type_pack_expansion_p;
3894
3895 /* True iff we found a subtree that has the extra args mechanism. */
3896 bool found_extra_args_tree_p = false;
3897 };
3898
3899 /* Identifies all of the argument packs that occur in a template
3900 argument and appends them to the TREE_LIST inside DATA, which is a
3901 find_parameter_pack_data structure. This is a subroutine of
3902 make_pack_expansion and uses_parameter_packs. */
3903 static tree
3904 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3905 {
3906 tree t = *tp;
3907 struct find_parameter_pack_data* ppd =
3908 (struct find_parameter_pack_data*)data;
3909 bool parameter_pack_p = false;
3910
3911 #define WALK_SUBTREE(NODE) \
3912 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3913 ppd, ppd->visited) \
3914
3915 /* Don't look through typedefs; we are interested in whether a
3916 parameter pack is actually written in the expression/type we're
3917 looking at, not the target type. */
3918 if (TYPE_P (t) && typedef_variant_p (t))
3919 {
3920 /* But do look at arguments for an alias template. */
3921 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3922 cp_walk_tree (&TI_ARGS (tinfo),
3923 &find_parameter_packs_r,
3924 ppd, ppd->visited);
3925 *walk_subtrees = 0;
3926 return NULL_TREE;
3927 }
3928
3929 /* Identify whether this is a parameter pack or not. */
3930 switch (TREE_CODE (t))
3931 {
3932 case TEMPLATE_PARM_INDEX:
3933 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3934 parameter_pack_p = true;
3935 break;
3936
3937 case TEMPLATE_TYPE_PARM:
3938 t = TYPE_MAIN_VARIANT (t);
3939 /* FALLTHRU */
3940 case TEMPLATE_TEMPLATE_PARM:
3941 /* If the placeholder appears in the decl-specifier-seq of a function
3942 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3943 is a pack expansion, the invented template parameter is a template
3944 parameter pack. */
3945 if (ppd->type_pack_expansion_p && is_auto (t)
3946 && TEMPLATE_TYPE_LEVEL (t) != 0)
3947 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3948 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3949 parameter_pack_p = true;
3950 break;
3951
3952 case FIELD_DECL:
3953 case PARM_DECL:
3954 if (DECL_PACK_P (t))
3955 {
3956 /* We don't want to walk into the type of a PARM_DECL,
3957 because we don't want to see the type parameter pack. */
3958 *walk_subtrees = 0;
3959 parameter_pack_p = true;
3960 }
3961 break;
3962
3963 case VAR_DECL:
3964 if (DECL_PACK_P (t))
3965 {
3966 /* We don't want to walk into the type of a variadic capture proxy,
3967 because we don't want to see the type parameter pack. */
3968 *walk_subtrees = 0;
3969 parameter_pack_p = true;
3970 }
3971 else if (variable_template_specialization_p (t))
3972 {
3973 cp_walk_tree (&DECL_TI_ARGS (t),
3974 find_parameter_packs_r,
3975 ppd, ppd->visited);
3976 *walk_subtrees = 0;
3977 }
3978 break;
3979
3980 case CALL_EXPR:
3981 if (builtin_pack_call_p (t))
3982 parameter_pack_p = true;
3983 break;
3984
3985 case BASES:
3986 parameter_pack_p = true;
3987 break;
3988 default:
3989 /* Not a parameter pack. */
3990 break;
3991 }
3992
3993 if (parameter_pack_p)
3994 {
3995 /* Add this parameter pack to the list. */
3996 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3997 }
3998
3999 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
4000 ppd->found_extra_args_tree_p = true;
4001
4002 if (TYPE_P (t))
4003 cp_walk_tree (&TYPE_CONTEXT (t),
4004 &find_parameter_packs_r, ppd, ppd->visited);
4005
4006 /* This switch statement will return immediately if we don't find a
4007 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
4008 switch (TREE_CODE (t))
4009 {
4010 case BOUND_TEMPLATE_TEMPLATE_PARM:
4011 /* Check the template itself. */
4012 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
4013 &find_parameter_packs_r, ppd, ppd->visited);
4014 return NULL_TREE;
4015
4016 case DECL_EXPR:
4017 {
4018 tree decl = DECL_EXPR_DECL (t);
4019 /* Ignore the declaration of a capture proxy for a parameter pack. */
4020 if (is_capture_proxy (decl))
4021 *walk_subtrees = 0;
4022 if (is_typedef_decl (decl))
4023 /* Since we stop at typedefs above, we need to look through them at
4024 the point of the DECL_EXPR. */
4025 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4026 &find_parameter_packs_r, ppd, ppd->visited);
4027 return NULL_TREE;
4028 }
4029
4030 case TEMPLATE_DECL:
4031 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4032 return NULL_TREE;
4033 cp_walk_tree (&TREE_TYPE (t),
4034 &find_parameter_packs_r, ppd, ppd->visited);
4035 return NULL_TREE;
4036
4037 case TYPE_PACK_EXPANSION:
4038 case EXPR_PACK_EXPANSION:
4039 *walk_subtrees = 0;
4040 return NULL_TREE;
4041
4042 case INTEGER_TYPE:
4043 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4044 ppd, ppd->visited);
4045 *walk_subtrees = 0;
4046 return NULL_TREE;
4047
4048 case IDENTIFIER_NODE:
4049 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4050 ppd->visited);
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4053
4054 case LAMBDA_EXPR:
4055 {
4056 /* Since we defer implicit capture, look in the parms and body. */
4057 tree fn = lambda_function (t);
4058 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4059 ppd->visited);
4060 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4061 ppd->visited);
4062 return NULL_TREE;
4063 }
4064
4065 case DECLTYPE_TYPE:
4066 {
4067 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4068 type_pack_expansion_p to false so that any placeholders
4069 within the expression don't get marked as parameter packs. */
4070 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4071 ppd->type_pack_expansion_p = false;
4072 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4073 ppd, ppd->visited);
4074 ppd->type_pack_expansion_p = type_pack_expansion_p;
4075 *walk_subtrees = 0;
4076 return NULL_TREE;
4077 }
4078
4079 case IF_STMT:
4080 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4081 ppd, ppd->visited);
4082 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4083 ppd, ppd->visited);
4084 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4085 ppd, ppd->visited);
4086 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4087 *walk_subtrees = 0;
4088 return NULL_TREE;
4089
4090 case TAG_DEFN:
4091 t = TREE_TYPE (t);
4092 if (CLASS_TYPE_P (t))
4093 {
4094 /* Local class, need to look through the whole definition.
4095 TYPE_BINFO might be unset for a partial instantiation. */
4096 if (TYPE_BINFO (t))
4097 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4098 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4099 ppd, ppd->visited);
4100 }
4101 else
4102 /* Enum, look at the values. */
4103 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4104 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4105 &find_parameter_packs_r,
4106 ppd, ppd->visited);
4107 return NULL_TREE;
4108
4109 case FUNCTION_TYPE:
4110 case METHOD_TYPE:
4111 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4112 break;
4113
4114 default:
4115 return NULL_TREE;
4116 }
4117
4118 #undef WALK_SUBTREE
4119
4120 return NULL_TREE;
4121 }
4122
4123 /* Determines if the expression or type T uses any parameter packs. */
4124 tree
4125 uses_parameter_packs (tree t)
4126 {
4127 tree parameter_packs = NULL_TREE;
4128 struct find_parameter_pack_data ppd;
4129 ppd.parameter_packs = &parameter_packs;
4130 ppd.visited = new hash_set<tree>;
4131 ppd.type_pack_expansion_p = false;
4132 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4133 delete ppd.visited;
4134 return parameter_packs;
4135 }
4136
4137 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4138 representation a base-class initializer into a parameter pack
4139 expansion. If all goes well, the resulting node will be an
4140 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4141 respectively. */
4142 tree
4143 make_pack_expansion (tree arg, tsubst_flags_t complain)
4144 {
4145 tree result;
4146 tree parameter_packs = NULL_TREE;
4147 bool for_types = false;
4148 struct find_parameter_pack_data ppd;
4149
4150 if (!arg || arg == error_mark_node)
4151 return arg;
4152
4153 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4154 {
4155 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4156 class initializer. In this case, the TREE_PURPOSE will be a
4157 _TYPE node (representing the base class expansion we're
4158 initializing) and the TREE_VALUE will be a TREE_LIST
4159 containing the initialization arguments.
4160
4161 The resulting expansion looks somewhat different from most
4162 expansions. Rather than returning just one _EXPANSION, we
4163 return a TREE_LIST whose TREE_PURPOSE is a
4164 TYPE_PACK_EXPANSION containing the bases that will be
4165 initialized. The TREE_VALUE will be identical to the
4166 original TREE_VALUE, which is a list of arguments that will
4167 be passed to each base. We do not introduce any new pack
4168 expansion nodes into the TREE_VALUE (although it is possible
4169 that some already exist), because the TREE_PURPOSE and
4170 TREE_VALUE all need to be expanded together with the same
4171 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4172 resulting TREE_PURPOSE will mention the parameter packs in
4173 both the bases and the arguments to the bases. */
4174 tree purpose;
4175 tree value;
4176 tree parameter_packs = NULL_TREE;
4177
4178 /* Determine which parameter packs will be used by the base
4179 class expansion. */
4180 ppd.visited = new hash_set<tree>;
4181 ppd.parameter_packs = &parameter_packs;
4182 ppd.type_pack_expansion_p = false;
4183 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4184 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4185 &ppd, ppd.visited);
4186
4187 if (parameter_packs == NULL_TREE)
4188 {
4189 if (complain & tf_error)
4190 error ("base initializer expansion %qT contains no parameter packs",
4191 arg);
4192 delete ppd.visited;
4193 return error_mark_node;
4194 }
4195
4196 if (TREE_VALUE (arg) != void_type_node)
4197 {
4198 /* Collect the sets of parameter packs used in each of the
4199 initialization arguments. */
4200 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4201 {
4202 /* Determine which parameter packs will be expanded in this
4203 argument. */
4204 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4205 &ppd, ppd.visited);
4206 }
4207 }
4208
4209 delete ppd.visited;
4210
4211 /* Create the pack expansion type for the base type. */
4212 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4213 PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
4214 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4215 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4216
4217 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4218 they will rarely be compared to anything. */
4219 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4220
4221 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4222 }
4223
4224 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4225 for_types = true;
4226
4227 /* Build the PACK_EXPANSION_* node. */
4228 result = for_types
4229 ? cxx_make_type (TYPE_PACK_EXPANSION)
4230 : make_node (EXPR_PACK_EXPANSION);
4231 PACK_EXPANSION_PATTERN (result) = arg;
4232 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4233 {
4234 /* Propagate type and const-expression information. */
4235 TREE_TYPE (result) = TREE_TYPE (arg);
4236 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4237 /* Mark this read now, since the expansion might be length 0. */
4238 mark_exp_read (arg);
4239 }
4240 else
4241 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4242 they will rarely be compared to anything. */
4243 SET_TYPE_STRUCTURAL_EQUALITY (result);
4244
4245 /* Determine which parameter packs will be expanded. */
4246 ppd.parameter_packs = &parameter_packs;
4247 ppd.visited = new hash_set<tree>;
4248 ppd.type_pack_expansion_p = TYPE_P (arg);
4249 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4250 delete ppd.visited;
4251
4252 /* Make sure we found some parameter packs. */
4253 if (parameter_packs == NULL_TREE)
4254 {
4255 if (complain & tf_error)
4256 {
4257 if (TYPE_P (arg))
4258 error ("expansion pattern %qT contains no parameter packs", arg);
4259 else
4260 error ("expansion pattern %qE contains no parameter packs", arg);
4261 }
4262 return error_mark_node;
4263 }
4264 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4265
4266 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4267 if (ppd.found_extra_args_tree_p)
4268 /* If the pattern of this pack expansion contains a subtree that has
4269 the extra args mechanism for avoiding partial instantiation, then
4270 force this pack expansion to also use extra args. Otherwise
4271 partial instantiation of this pack expansion may not lower the
4272 level of some parameter packs within the pattern, which would
4273 confuse tsubst_pack_expansion later (PR101764). */
4274 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
4275
4276 return result;
4277 }
4278
4279 /* Checks T for any "bare" parameter packs, which have not yet been
4280 expanded, and issues an error if any are found. This operation can
4281 only be done on full expressions or types (e.g., an expression
4282 statement, "if" condition, etc.), because we could have expressions like:
4283
4284 foo(f(g(h(args)))...)
4285
4286 where "args" is a parameter pack. check_for_bare_parameter_packs
4287 should not be called for the subexpressions args, h(args),
4288 g(h(args)), or f(g(h(args))), because we would produce erroneous
4289 error messages.
4290
4291 Returns TRUE and emits an error if there were bare parameter packs,
4292 returns FALSE otherwise. */
4293 bool
4294 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4295 {
4296 tree parameter_packs = NULL_TREE;
4297 struct find_parameter_pack_data ppd;
4298
4299 if (!processing_template_decl || !t || t == error_mark_node)
4300 return false;
4301
4302 if (TREE_CODE (t) == TYPE_DECL)
4303 t = TREE_TYPE (t);
4304
4305 ppd.parameter_packs = &parameter_packs;
4306 ppd.visited = new hash_set<tree>;
4307 ppd.type_pack_expansion_p = false;
4308 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4309 delete ppd.visited;
4310
4311 if (!parameter_packs)
4312 return false;
4313
4314 if (loc == UNKNOWN_LOCATION)
4315 loc = cp_expr_loc_or_input_loc (t);
4316
4317 /* It's OK for a lambda to have an unexpanded parameter pack from the
4318 containing context, but do complain about unexpanded capture packs. */
4319 tree lam = current_lambda_expr ();
4320 if (lam)
4321 lam = TREE_TYPE (lam);
4322
4323 if (lam && lam != current_class_type)
4324 {
4325 /* We're in a lambda, but it isn't the innermost class.
4326 This should work, but currently doesn't. */
4327 sorry_at (loc, "unexpanded parameter pack in local class in lambda");
4328 return true;
4329 }
4330
4331 if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
4332 for (; parameter_packs;
4333 parameter_packs = TREE_CHAIN (parameter_packs))
4334 {
4335 tree pack = TREE_VALUE (parameter_packs);
4336 if (is_capture_proxy (pack)
4337 || (TREE_CODE (pack) == PARM_DECL
4338 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
4339 break;
4340 }
4341
4342 if (parameter_packs)
4343 {
4344 error_at (loc, "parameter packs not expanded with %<...%>:");
4345 while (parameter_packs)
4346 {
4347 tree pack = TREE_VALUE (parameter_packs);
4348 tree name = NULL_TREE;
4349
4350 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4351 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4352 name = TYPE_NAME (pack);
4353 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4354 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4355 else if (TREE_CODE (pack) == CALL_EXPR)
4356 name = DECL_NAME (CALL_EXPR_FN (pack));
4357 else
4358 name = DECL_NAME (pack);
4359
4360 if (name)
4361 inform (loc, " %qD", name);
4362 else
4363 inform (loc, " %s", "<anonymous>");
4364
4365 parameter_packs = TREE_CHAIN (parameter_packs);
4366 }
4367
4368 return true;
4369 }
4370
4371 return false;
4372 }
4373
4374 /* Expand any parameter packs that occur in the template arguments in
4375 ARGS. */
4376 tree
4377 expand_template_argument_pack (tree args)
4378 {
4379 if (args == error_mark_node)
4380 return error_mark_node;
4381
4382 tree result_args = NULL_TREE;
4383 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4384 int num_result_args = -1;
4385 int non_default_args_count = -1;
4386
4387 /* First, determine if we need to expand anything, and the number of
4388 slots we'll need. */
4389 for (in_arg = 0; in_arg < nargs; ++in_arg)
4390 {
4391 tree arg = TREE_VEC_ELT (args, in_arg);
4392 if (arg == NULL_TREE)
4393 return args;
4394 if (ARGUMENT_PACK_P (arg))
4395 {
4396 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4397 if (num_result_args < 0)
4398 num_result_args = in_arg + num_packed;
4399 else
4400 num_result_args += num_packed;
4401 }
4402 else
4403 {
4404 if (num_result_args >= 0)
4405 num_result_args++;
4406 }
4407 }
4408
4409 /* If no expansion is necessary, we're done. */
4410 if (num_result_args < 0)
4411 return args;
4412
4413 /* Expand arguments. */
4414 result_args = make_tree_vec (num_result_args);
4415 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4416 non_default_args_count =
4417 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4418 for (in_arg = 0; in_arg < nargs; ++in_arg)
4419 {
4420 tree arg = TREE_VEC_ELT (args, in_arg);
4421 if (ARGUMENT_PACK_P (arg))
4422 {
4423 tree packed = ARGUMENT_PACK_ARGS (arg);
4424 int i, num_packed = TREE_VEC_LENGTH (packed);
4425 for (i = 0; i < num_packed; ++i, ++out_arg)
4426 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4427 if (non_default_args_count > 0)
4428 non_default_args_count += num_packed - 1;
4429 }
4430 else
4431 {
4432 TREE_VEC_ELT (result_args, out_arg) = arg;
4433 ++out_arg;
4434 }
4435 }
4436 if (non_default_args_count >= 0)
4437 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4438 return result_args;
4439 }
4440
4441 /* Checks if DECL shadows a template parameter.
4442
4443 [temp.local]: A template-parameter shall not be redeclared within its
4444 scope (including nested scopes).
4445
4446 Emits an error and returns TRUE if the DECL shadows a parameter,
4447 returns FALSE otherwise. */
4448
4449 bool
4450 check_template_shadow (tree decl)
4451 {
4452 tree olddecl;
4453
4454 /* If we're not in a template, we can't possibly shadow a template
4455 parameter. */
4456 if (!current_template_parms)
4457 return true;
4458
4459 /* Figure out what we're shadowing. */
4460 decl = OVL_FIRST (decl);
4461 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4462
4463 /* If there's no previous binding for this name, we're not shadowing
4464 anything, let alone a template parameter. */
4465 if (!olddecl)
4466 return true;
4467
4468 /* If we're not shadowing a template parameter, we're done. Note
4469 that OLDDECL might be an OVERLOAD (or perhaps even an
4470 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4471 node. */
4472 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4473 return true;
4474
4475 /* We check for decl != olddecl to avoid bogus errors for using a
4476 name inside a class. We check TPFI to avoid duplicate errors for
4477 inline member templates. */
4478 if (decl == olddecl
4479 || (DECL_TEMPLATE_PARM_P (decl)
4480 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4481 return true;
4482
4483 /* Don't complain about the injected class name, as we've already
4484 complained about the class itself. */
4485 if (DECL_SELF_REFERENCE_P (decl))
4486 return false;
4487
4488 if (DECL_TEMPLATE_PARM_P (decl))
4489 error ("declaration of template parameter %q+D shadows "
4490 "template parameter", decl);
4491 else
4492 error ("declaration of %q+#D shadows template parameter", decl);
4493 inform (DECL_SOURCE_LOCATION (olddecl),
4494 "template parameter %qD declared here", olddecl);
4495 return false;
4496 }
4497
4498 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4499 ORIG_LEVEL, DECL, and TYPE. */
4500
4501 static tree
4502 build_template_parm_index (int index,
4503 int level,
4504 int orig_level,
4505 tree decl,
4506 tree type)
4507 {
4508 tree t = make_node (TEMPLATE_PARM_INDEX);
4509 TEMPLATE_PARM_IDX (t) = index;
4510 TEMPLATE_PARM_LEVEL (t) = level;
4511 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4512 TEMPLATE_PARM_DECL (t) = decl;
4513 TREE_TYPE (t) = type;
4514 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4515 TREE_READONLY (t) = TREE_READONLY (decl);
4516
4517 return t;
4518 }
4519
4520 struct ctp_hasher : ggc_ptr_hash<tree_node>
4521 {
4522 static hashval_t hash (tree t)
4523 {
4524 ++comparing_specializations;
4525 tree_code code = TREE_CODE (t);
4526 hashval_t val = iterative_hash_object (code, 0);
4527 val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
4528 val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
4529 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
4530 val = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
4531 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
4532 val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
4533 --comparing_specializations;
4534 return val;
4535 }
4536
4537 static bool equal (tree t, tree u)
4538 {
4539 ++comparing_specializations;
4540 bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
4541 --comparing_specializations;
4542 return eq;
4543 }
4544 };
4545
4546 static GTY (()) hash_table<ctp_hasher> *ctp_table;
4547
4548 /* Find the canonical type parameter for the given template type
4549 parameter. Returns the canonical type parameter, which may be TYPE
4550 if no such parameter existed. */
4551
4552 tree
4553 canonical_type_parameter (tree type)
4554 {
4555 if (ctp_table == NULL)
4556 ctp_table = hash_table<ctp_hasher>::create_ggc (61);
4557
4558 tree& slot = *ctp_table->find_slot (type, INSERT);
4559 if (slot == NULL_TREE)
4560 slot = type;
4561 return slot;
4562 }
4563
4564 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4565 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4566 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4567 new one is created. */
4568
4569 static tree
4570 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4571 tsubst_flags_t complain)
4572 {
4573 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4574 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4575 != TEMPLATE_PARM_LEVEL (index) - levels)
4576 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4577 {
4578 tree orig_decl = TEMPLATE_PARM_DECL (index);
4579
4580 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4581 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4582 type);
4583 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4584 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4585 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4586 DECL_ARTIFICIAL (decl) = 1;
4587 SET_DECL_TEMPLATE_PARM_P (decl);
4588
4589 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4590 TEMPLATE_PARM_LEVEL (index) - levels,
4591 TEMPLATE_PARM_ORIG_LEVEL (index),
4592 decl, type);
4593 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4594 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4595 = TEMPLATE_PARM_PARAMETER_PACK (index);
4596
4597 /* Template template parameters need this. */
4598 tree inner = decl;
4599 if (TREE_CODE (decl) == TEMPLATE_DECL)
4600 {
4601 inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
4602 TYPE_DECL, DECL_NAME (decl), type);
4603 DECL_TEMPLATE_RESULT (decl) = inner;
4604 DECL_ARTIFICIAL (inner) = true;
4605 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
4606 args, complain);
4607 DECL_TEMPLATE_PARMS (decl) = parms;
4608 tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
4609 DECL_TEMPLATE_INFO (inner)
4610 = build_template_info (DECL_TI_TEMPLATE (orig_inner),
4611 template_parms_to_args (parms));
4612 }
4613
4614 /* Attach the TPI to the decl. */
4615 if (TREE_CODE (inner) == TYPE_DECL)
4616 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4617 else
4618 DECL_INITIAL (decl) = tpi;
4619 }
4620
4621 return TEMPLATE_PARM_DESCENDANTS (index);
4622 }
4623
4624 /* Process information from new template parameter PARM and append it
4625 to the LIST being built. This new parameter is a non-type
4626 parameter iff IS_NON_TYPE is true. This new parameter is a
4627 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4628 is in PARM_LOC. */
4629
4630 tree
4631 process_template_parm (tree list, location_t parm_loc, tree parm,
4632 bool is_non_type, bool is_parameter_pack)
4633 {
4634 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4635 tree prev = NULL_TREE;
4636 int idx = 0;
4637
4638 if (list)
4639 {
4640 prev = tree_last (list);
4641
4642 tree p = TREE_VALUE (prev);
4643 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4644 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4645 else if (TREE_CODE (p) == PARM_DECL)
4646 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4647
4648 ++idx;
4649 }
4650
4651 tree decl = NULL_TREE;
4652 tree defval = TREE_PURPOSE (parm);
4653 tree constr = TREE_TYPE (parm);
4654
4655 if (is_non_type)
4656 {
4657 parm = TREE_VALUE (parm);
4658
4659 SET_DECL_TEMPLATE_PARM_P (parm);
4660
4661 if (TREE_TYPE (parm) != error_mark_node)
4662 {
4663 /* [temp.param]
4664
4665 The top-level cv-qualifiers on the template-parameter are
4666 ignored when determining its type. */
4667 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4668 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4669 TREE_TYPE (parm) = error_mark_node;
4670 else if (uses_parameter_packs (TREE_TYPE (parm))
4671 && !is_parameter_pack
4672 /* If we're in a nested template parameter list, the template
4673 template parameter could be a parameter pack. */
4674 && processing_template_parmlist == 1)
4675 {
4676 /* This template parameter is not a parameter pack, but it
4677 should be. Complain about "bare" parameter packs. */
4678 check_for_bare_parameter_packs (TREE_TYPE (parm));
4679
4680 /* Recover by calling this a parameter pack. */
4681 is_parameter_pack = true;
4682 }
4683 }
4684
4685 /* A template parameter is not modifiable. */
4686 TREE_CONSTANT (parm) = 1;
4687 TREE_READONLY (parm) = 1;
4688 decl = build_decl (parm_loc,
4689 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4690 TREE_CONSTANT (decl) = 1;
4691 TREE_READONLY (decl) = 1;
4692 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4693 = build_template_parm_index (idx, current_template_depth,
4694 current_template_depth,
4695 decl, TREE_TYPE (parm));
4696
4697 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4698 = is_parameter_pack;
4699 }
4700 else
4701 {
4702 tree t;
4703 parm = TREE_VALUE (TREE_VALUE (parm));
4704
4705 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4706 {
4707 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4708 /* This is for distinguishing between real templates and template
4709 template parameters */
4710 TREE_TYPE (parm) = t;
4711
4712 /* any_template_parm_r expects to be able to get the targs of a
4713 DECL_TEMPLATE_RESULT. */
4714 tree result = DECL_TEMPLATE_RESULT (parm);
4715 TREE_TYPE (result) = t;
4716 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4717 tree tinfo = build_template_info (parm, args);
4718 retrofit_lang_decl (result);
4719 DECL_TEMPLATE_INFO (result) = tinfo;
4720
4721 decl = parm;
4722 }
4723 else
4724 {
4725 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4726 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4727 decl = build_decl (parm_loc,
4728 TYPE_DECL, parm, t);
4729 }
4730
4731 TYPE_NAME (t) = decl;
4732 TYPE_STUB_DECL (t) = decl;
4733 parm = decl;
4734 TEMPLATE_TYPE_PARM_INDEX (t)
4735 = build_template_parm_index (idx, current_template_depth,
4736 current_template_depth,
4737 decl, TREE_TYPE (parm));
4738 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4739 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4740 }
4741 DECL_ARTIFICIAL (decl) = 1;
4742 SET_DECL_TEMPLATE_PARM_P (decl);
4743
4744 if (TREE_CODE (parm) == TEMPLATE_DECL
4745 && !uses_outer_template_parms (parm))
4746 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
4747
4748 /* Build requirements for the type/template parameter.
4749 This must be done after SET_DECL_TEMPLATE_PARM_P or
4750 process_template_parm could fail. */
4751 tree reqs = finish_shorthand_constraint (parm, constr);
4752
4753 decl = pushdecl (decl);
4754 if (!is_non_type)
4755 parm = decl;
4756
4757 /* Build the parameter node linking the parameter declaration,
4758 its default argument (if any), and its constraints (if any). */
4759 parm = build_tree_list (defval, parm);
4760 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4761
4762 if (prev)
4763 TREE_CHAIN (prev) = parm;
4764 else
4765 list = parm;
4766
4767 return list;
4768 }
4769
4770 /* The end of a template parameter list has been reached. Process the
4771 tree list into a parameter vector, converting each parameter into a more
4772 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4773 as PARM_DECLs. */
4774
4775 tree
4776 end_template_parm_list (tree parms)
4777 {
4778 tree saved_parmlist = make_tree_vec (list_length (parms));
4779
4780 /* Pop the dummy parameter level and add the real one. We do not
4781 morph the dummy parameter in place, as it might have been
4782 captured by a (nested) template-template-parm. */
4783 current_template_parms = TREE_CHAIN (current_template_parms);
4784
4785 current_template_parms
4786 = tree_cons (size_int (current_template_depth + 1),
4787 saved_parmlist, current_template_parms);
4788
4789 for (unsigned ix = 0; parms; ix++)
4790 {
4791 tree parm = parms;
4792 parms = TREE_CHAIN (parms);
4793 TREE_CHAIN (parm) = NULL_TREE;
4794
4795 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4796 }
4797
4798 --processing_template_parmlist;
4799
4800 return saved_parmlist;
4801 }
4802
4803 // Explicitly indicate the end of the template parameter list. We assume
4804 // that the current template parameters have been constructed and/or
4805 // managed explicitly, as when creating new template template parameters
4806 // from a shorthand constraint.
4807 void
4808 end_template_parm_list ()
4809 {
4810 --processing_template_parmlist;
4811 }
4812
4813 /* end_template_decl is called after a template declaration is seen. */
4814
4815 void
4816 end_template_decl (void)
4817 {
4818 reset_specialization ();
4819
4820 if (! processing_template_decl)
4821 return;
4822
4823 /* This matches the pushlevel in begin_template_parm_list. */
4824 finish_scope ();
4825
4826 --processing_template_decl;
4827 current_template_parms = TREE_CHAIN (current_template_parms);
4828 }
4829
4830 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4831 thereof, and converts it into an argument suitable to be passed to
4832 the type substitution functions. Note that if the TREE_LIST contains
4833 an error_mark node, the returned argument is error_mark_node. */
4834
4835 tree
4836 template_parm_to_arg (tree t)
4837 {
4838 if (!t)
4839 return NULL_TREE;
4840
4841 if (TREE_CODE (t) == TREE_LIST)
4842 t = TREE_VALUE (t);
4843
4844 if (error_operand_p (t))
4845 return error_mark_node;
4846
4847 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4848 {
4849 if (TREE_CODE (t) == TYPE_DECL
4850 || TREE_CODE (t) == TEMPLATE_DECL)
4851 t = TREE_TYPE (t);
4852 else
4853 t = DECL_INITIAL (t);
4854 }
4855
4856 gcc_assert (TEMPLATE_PARM_P (t));
4857
4858 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4859 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4860 {
4861 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4862 {
4863 /* Turn this argument into a TYPE_ARGUMENT_PACK
4864 with a single element, which expands T. */
4865 tree vec = make_tree_vec (1);
4866 if (CHECKING_P)
4867 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4868
4869 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4870
4871 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4872 ARGUMENT_PACK_ARGS (t) = vec;
4873 }
4874 }
4875 else
4876 {
4877 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4878 {
4879 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4880 with a single element, which expands T. */
4881 tree vec = make_tree_vec (1);
4882 if (CHECKING_P)
4883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4884
4885 t = convert_from_reference (t);
4886 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4887
4888 t = make_node (NONTYPE_ARGUMENT_PACK);
4889 ARGUMENT_PACK_ARGS (t) = vec;
4890 }
4891 else
4892 t = convert_from_reference (t);
4893 }
4894 return t;
4895 }
4896
4897 /* If T looks like a generic template argument produced by template_parm_to_arg,
4898 return the corresponding template parameter, otherwise return NULL_TREE. */
4899
4900 static tree
4901 template_arg_to_parm (tree t)
4902 {
4903 if (t == NULL_TREE)
4904 return NULL_TREE;
4905
4906 if (ARGUMENT_PACK_P (t))
4907 {
4908 tree args = ARGUMENT_PACK_ARGS (t);
4909 if (TREE_VEC_LENGTH (args) == 1
4910 && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
4911 t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
4912 }
4913
4914 if (REFERENCE_REF_P (t))
4915 t = TREE_OPERAND (t, 0);
4916
4917 if (TEMPLATE_PARM_P (t))
4918 return t;
4919 else
4920 return NULL_TREE;
4921 }
4922
4923 /* Given a single level of template parameters (a TREE_VEC), return it
4924 as a set of template arguments. */
4925
4926 tree
4927 template_parms_level_to_args (tree parms)
4928 {
4929 parms = copy_node (parms);
4930 TREE_TYPE (parms) = NULL_TREE;
4931 for (tree& parm : tree_vec_range (parms))
4932 parm = template_parm_to_arg (parm);
4933
4934 if (CHECKING_P)
4935 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
4936
4937 return parms;
4938 }
4939
4940 /* Given a set of template parameters, return them as a set of template
4941 arguments. The template parameters are represented as a TREE_VEC, in
4942 the form documented in cp-tree.h for template arguments. */
4943
4944 tree
4945 template_parms_to_args (tree parms)
4946 {
4947 tree header;
4948 tree args = NULL_TREE;
4949 int length = TMPL_PARMS_DEPTH (parms);
4950 int l = length;
4951
4952 /* If there is only one level of template parameters, we do not
4953 create a TREE_VEC of TREE_VECs. Instead, we return a single
4954 TREE_VEC containing the arguments. */
4955 if (length > 1)
4956 args = make_tree_vec (length);
4957
4958 for (header = parms; header; header = TREE_CHAIN (header))
4959 {
4960 tree a = template_parms_level_to_args (TREE_VALUE (header));
4961
4962 if (length > 1)
4963 TREE_VEC_ELT (args, --l) = a;
4964 else
4965 args = a;
4966 }
4967
4968 return args;
4969 }
4970
4971 /* Within the declaration of a template, return the currently active
4972 template parameters as an argument TREE_VEC. */
4973
4974 static tree
4975 current_template_args (void)
4976 {
4977 return template_parms_to_args (current_template_parms);
4978 }
4979
4980 /* Return the fully generic arguments for of TMPL, i.e. what
4981 current_template_args would be while parsing it. */
4982
4983 tree
4984 generic_targs_for (tree tmpl)
4985 {
4986 if (tmpl == NULL_TREE)
4987 return NULL_TREE;
4988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4989 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4990 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4991 template parameter, it has no TEMPLATE_INFO; for a partial
4992 specialization, it has the arguments for the primary template, and we
4993 want the arguments for the partial specialization. */;
4994 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4995 if (tree ti = get_template_info (result))
4996 return TI_ARGS (ti);
4997 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4998 }
4999
5000 /* Return the template arguments corresponding to the template parameters of
5001 DECL's enclosing scope. When DECL is a member of a partial specialization,
5002 this returns the arguments for the partial specialization as opposed to those
5003 for the primary template, which is the main difference between this function
5004 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */
5005
5006 tree
5007 outer_template_args (const_tree decl)
5008 {
5009 if (TREE_CODE (decl) == TEMPLATE_DECL)
5010 decl = DECL_TEMPLATE_RESULT (decl);
5011 tree ti = get_template_info (decl);
5012 if (!ti)
5013 return NULL_TREE;
5014 tree args = TI_ARGS (ti);
5015 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
5016 return args;
5017 if (TMPL_ARGS_DEPTH (args) == 1)
5018 return NULL_TREE;
5019 return strip_innermost_template_args (args, 1);
5020 }
5021
5022 /* Update the declared TYPE by doing any lookups which were thought to be
5023 dependent, but are not now that we know the SCOPE of the declarator. */
5024
5025 tree
5026 maybe_update_decl_type (tree orig_type, tree scope)
5027 {
5028 tree type = orig_type;
5029
5030 if (type == NULL_TREE)
5031 return type;
5032
5033 if (TREE_CODE (orig_type) == TYPE_DECL)
5034 type = TREE_TYPE (type);
5035
5036 if (scope && TYPE_P (scope) && dependent_type_p (scope)
5037 && dependent_type_p (type)
5038 /* Don't bother building up the args in this case. */
5039 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
5040 {
5041 /* tsubst in the args corresponding to the template parameters,
5042 including auto if present. Most things will be unchanged, but
5043 make_typename_type and tsubst_qualified_id will resolve
5044 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5045 tree args = current_template_args ();
5046 tree auto_node = type_uses_auto (type);
5047 tree pushed;
5048 if (auto_node)
5049 {
5050 tree auto_vec = make_tree_vec (1);
5051 TREE_VEC_ELT (auto_vec, 0) = auto_node;
5052 args = add_to_template_args (args, auto_vec);
5053 }
5054 pushed = push_scope (scope);
5055 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
5056 if (pushed)
5057 pop_scope (scope);
5058 }
5059
5060 if (type == error_mark_node)
5061 return orig_type;
5062
5063 if (TREE_CODE (orig_type) == TYPE_DECL)
5064 {
5065 if (same_type_p (type, TREE_TYPE (orig_type)))
5066 type = orig_type;
5067 else
5068 type = TYPE_NAME (type);
5069 }
5070 return type;
5071 }
5072
5073 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5074 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5075 the new template is a member template. */
5076
5077 static tree
5078 build_template_decl (tree decl, tree parms, bool member_template_p)
5079 {
5080 gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
5081
5082 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
5083 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
5084 DECL_TEMPLATE_PARMS (tmpl) = parms;
5085 DECL_TEMPLATE_RESULT (tmpl) = decl;
5086 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
5087 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5088 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
5089 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
5090
5091 /* Propagate module information from the decl. */
5092 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
5093
5094 return tmpl;
5095 }
5096
5097 struct template_parm_data
5098 {
5099 /* The level of the template parameters we are currently
5100 processing. */
5101 int level;
5102
5103 /* The index of the specialization argument we are currently
5104 processing. */
5105 int current_arg;
5106
5107 /* An array whose size is the number of template parameters. The
5108 elements are nonzero if the parameter has been used in any one
5109 of the arguments processed so far. */
5110 int* parms;
5111
5112 /* An array whose size is the number of template arguments. The
5113 elements are nonzero if the argument makes use of template
5114 parameters of this level. */
5115 int* arg_uses_template_parms;
5116 };
5117
5118 /* Subroutine of push_template_decl used to see if each template
5119 parameter in a partial specialization is used in the explicit
5120 argument list. If T is of the LEVEL given in DATA (which is
5121 treated as a template_parm_data*), then DATA->PARMS is marked
5122 appropriately. */
5123
5124 static int
5125 mark_template_parm (tree t, void* data)
5126 {
5127 int level;
5128 int idx;
5129 struct template_parm_data* tpd = (struct template_parm_data*) data;
5130
5131 template_parm_level_and_index (t, &level, &idx);
5132
5133 if (level == tpd->level)
5134 {
5135 tpd->parms[idx] = 1;
5136 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5137 }
5138
5139 /* In C++17 the type of a non-type argument is a deduced context. */
5140 if (cxx_dialect >= cxx17
5141 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5142 for_each_template_parm (TREE_TYPE (t),
5143 &mark_template_parm,
5144 data,
5145 NULL,
5146 /*include_nondeduced_p=*/false);
5147
5148 /* Return zero so that for_each_template_parm will continue the
5149 traversal of the tree; we want to mark *every* template parm. */
5150 return 0;
5151 }
5152
5153 /* Process the partial specialization DECL. */
5154
5155 static tree
5156 process_partial_specialization (tree decl)
5157 {
5158 tree type = TREE_TYPE (decl);
5159 tree tinfo = get_template_info (decl);
5160 tree maintmpl = TI_TEMPLATE (tinfo);
5161 tree specargs = TI_ARGS (tinfo);
5162 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5163 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5164 tree inner_parms;
5165 tree inst;
5166 int nargs = TREE_VEC_LENGTH (inner_args);
5167 int ntparms;
5168 int i;
5169 bool did_error_intro = false;
5170 struct template_parm_data tpd;
5171 struct template_parm_data tpd2;
5172
5173 gcc_assert (current_template_parms);
5174
5175 /* A concept cannot be specialized. */
5176 if (flag_concepts && variable_concept_p (maintmpl))
5177 {
5178 error ("specialization of variable concept %q#D", maintmpl);
5179 return error_mark_node;
5180 }
5181
5182 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5183 ntparms = TREE_VEC_LENGTH (inner_parms);
5184
5185 /* We check that each of the template parameters given in the
5186 partial specialization is used in the argument list to the
5187 specialization. For example:
5188
5189 template <class T> struct S;
5190 template <class T> struct S<T*>;
5191
5192 The second declaration is OK because `T*' uses the template
5193 parameter T, whereas
5194
5195 template <class T> struct S<int>;
5196
5197 is no good. Even trickier is:
5198
5199 template <class T>
5200 struct S1
5201 {
5202 template <class U>
5203 struct S2;
5204 template <class U>
5205 struct S2<T>;
5206 };
5207
5208 The S2<T> declaration is actually invalid; it is a
5209 full-specialization. Of course,
5210
5211 template <class U>
5212 struct S2<T (*)(U)>;
5213
5214 or some such would have been OK. */
5215 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5216 tpd.parms = XALLOCAVEC (int, ntparms);
5217 memset (tpd.parms, 0, sizeof (int) * ntparms);
5218
5219 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5220 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5221 for (i = 0; i < nargs; ++i)
5222 {
5223 tpd.current_arg = i;
5224 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5225 &mark_template_parm,
5226 &tpd,
5227 NULL,
5228 /*include_nondeduced_p=*/false);
5229 }
5230 for (i = 0; i < ntparms; ++i)
5231 if (tpd.parms[i] == 0)
5232 {
5233 /* One of the template parms was not used in a deduced context in the
5234 specialization. */
5235 if (!did_error_intro)
5236 {
5237 error ("template parameters not deducible in "
5238 "partial specialization:");
5239 did_error_intro = true;
5240 }
5241
5242 inform (input_location, " %qD",
5243 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5244 }
5245
5246 if (did_error_intro)
5247 return error_mark_node;
5248
5249 /* [temp.class.spec]
5250
5251 The argument list of the specialization shall not be identical to
5252 the implicit argument list of the primary template. */
5253 tree main_args
5254 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5255 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5256 && (!flag_concepts
5257 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5258 {
5259 if (!flag_concepts)
5260 error ("partial specialization %q+D does not specialize "
5261 "any template arguments; to define the primary template, "
5262 "remove the template argument list", decl);
5263 else
5264 error ("partial specialization %q+D does not specialize any "
5265 "template arguments and is not more constrained than "
5266 "the primary template; to define the primary template, "
5267 "remove the template argument list", decl);
5268 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5269 }
5270
5271 /* A partial specialization that replaces multiple parameters of the
5272 primary template with a pack expansion is less specialized for those
5273 parameters. */
5274 if (nargs < DECL_NTPARMS (maintmpl))
5275 {
5276 error ("partial specialization is not more specialized than the "
5277 "primary template because it replaces multiple parameters "
5278 "with a pack expansion");
5279 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5280 /* Avoid crash in process_partial_specialization. */
5281 return decl;
5282 }
5283
5284 else if (nargs > DECL_NTPARMS (maintmpl))
5285 {
5286 error ("too many arguments for partial specialization %qT", type);
5287 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5288 /* Avoid crash below. */
5289 return decl;
5290 }
5291
5292 /* If we aren't in a dependent class, we can actually try deduction. */
5293 else if (tpd.level == 1
5294 /* FIXME we should be able to handle a partial specialization of a
5295 partial instantiation, but currently we can't (c++/41727). */
5296 && TMPL_ARGS_DEPTH (specargs) == 1
5297 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5298 {
5299 auto_diagnostic_group d;
5300 if (pedwarn (input_location, 0,
5301 "partial specialization %qD is not more specialized than",
5302 decl))
5303 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5304 maintmpl);
5305 }
5306
5307 /* [temp.spec.partial]
5308
5309 The type of a template parameter corresponding to a specialized
5310 non-type argument shall not be dependent on a parameter of the
5311 specialization.
5312
5313 Also, we verify that pack expansions only occur at the
5314 end of the argument list. */
5315 tpd2.parms = 0;
5316 for (i = 0; i < nargs; ++i)
5317 {
5318 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5319 tree arg = TREE_VEC_ELT (inner_args, i);
5320 tree packed_args = NULL_TREE;
5321 int j, len = 1;
5322
5323 if (ARGUMENT_PACK_P (arg))
5324 {
5325 /* Extract the arguments from the argument pack. We'll be
5326 iterating over these in the following loop. */
5327 packed_args = ARGUMENT_PACK_ARGS (arg);
5328 len = TREE_VEC_LENGTH (packed_args);
5329 }
5330
5331 for (j = 0; j < len; j++)
5332 {
5333 if (packed_args)
5334 /* Get the Jth argument in the parameter pack. */
5335 arg = TREE_VEC_ELT (packed_args, j);
5336
5337 if (PACK_EXPANSION_P (arg))
5338 {
5339 /* Pack expansions must come at the end of the
5340 argument list. */
5341 if ((packed_args && j < len - 1)
5342 || (!packed_args && i < nargs - 1))
5343 {
5344 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5345 error ("parameter pack argument %qE must be at the "
5346 "end of the template argument list", arg);
5347 else
5348 error ("parameter pack argument %qT must be at the "
5349 "end of the template argument list", arg);
5350 }
5351 }
5352
5353 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5354 /* We only care about the pattern. */
5355 arg = PACK_EXPANSION_PATTERN (arg);
5356
5357 if (/* These first two lines are the `non-type' bit. */
5358 !TYPE_P (arg)
5359 && TREE_CODE (arg) != TEMPLATE_DECL
5360 /* This next two lines are the `argument expression is not just a
5361 simple identifier' condition and also the `specialized
5362 non-type argument' bit. */
5363 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5364 && !((REFERENCE_REF_P (arg)
5365 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5366 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5367 {
5368 /* Look at the corresponding template parameter,
5369 marking which template parameters its type depends
5370 upon. */
5371 tree type = TREE_TYPE (parm);
5372
5373 if (!tpd2.parms)
5374 {
5375 /* We haven't yet initialized TPD2. Do so now. */
5376 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5377 /* The number of parameters here is the number in the
5378 main template, which, as checked in the assertion
5379 above, is NARGS. */
5380 tpd2.parms = XALLOCAVEC (int, nargs);
5381 tpd2.level =
5382 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5383 }
5384
5385 /* Mark the template parameters. But this time, we're
5386 looking for the template parameters of the main
5387 template, not in the specialization. */
5388 tpd2.current_arg = i;
5389 tpd2.arg_uses_template_parms[i] = 0;
5390 memset (tpd2.parms, 0, sizeof (int) * nargs);
5391 for_each_template_parm (type,
5392 &mark_template_parm,
5393 &tpd2,
5394 NULL,
5395 /*include_nondeduced_p=*/false);
5396
5397 if (tpd2.arg_uses_template_parms [i])
5398 {
5399 /* The type depended on some template parameters.
5400 If they are fully specialized in the
5401 specialization, that's OK. */
5402 int j;
5403 int count = 0;
5404 for (j = 0; j < nargs; ++j)
5405 if (tpd2.parms[j] != 0
5406 && tpd.arg_uses_template_parms [j])
5407 ++count;
5408 if (count != 0)
5409 error_n (input_location, count,
5410 "type %qT of template argument %qE depends "
5411 "on a template parameter",
5412 "type %qT of template argument %qE depends "
5413 "on template parameters",
5414 type,
5415 arg);
5416 }
5417 }
5418 }
5419 }
5420
5421 /* We should only get here once. */
5422 if (TREE_CODE (decl) == TYPE_DECL)
5423 gcc_assert (!COMPLETE_TYPE_P (type));
5424
5425 // Build the template decl.
5426 tree tmpl = build_template_decl (decl, current_template_parms,
5427 DECL_MEMBER_TEMPLATE_P (maintmpl));
5428 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5429 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5430 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5431
5432 /* Give template template parms a DECL_CONTEXT of the template
5433 for which they are a parameter. */
5434 for (i = 0; i < ntparms; ++i)
5435 {
5436 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5437 if (TREE_CODE (parm) == TEMPLATE_DECL)
5438 DECL_CONTEXT (parm) = tmpl;
5439 }
5440
5441 if (VAR_P (decl))
5442 {
5443 /* We didn't register this in check_explicit_specialization so we could
5444 wait until the constraints were set. */
5445 tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
5446 if (reg != decl)
5447 /* Redeclaration. */
5448 return reg;
5449 }
5450 else
5451 associate_classtype_constraints (type);
5452
5453 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5454 = tree_cons (specargs, tmpl,
5455 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5456 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5457 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */
5458 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
5459 TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
5460
5461 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5462 inst = TREE_CHAIN (inst))
5463 {
5464 tree instance = TREE_VALUE (inst);
5465 if (TYPE_P (instance)
5466 ? (COMPLETE_TYPE_P (instance)
5467 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5468 : DECL_TEMPLATE_INSTANTIATION (instance))
5469 {
5470 tree partial_ti = most_specialized_partial_spec (instance, tf_none,
5471 /*rechecking=*/true);
5472 tree inst_decl = (DECL_P (instance)
5473 ? instance : TYPE_NAME (instance));
5474 if (!partial_ti)
5475 /* OK */;
5476 else if (partial_ti == error_mark_node)
5477 permerror (input_location,
5478 "declaration of %qD ambiguates earlier template "
5479 "instantiation for %qD", decl, inst_decl);
5480 else if (TI_TEMPLATE (partial_ti) == tmpl)
5481 permerror (input_location,
5482 "partial specialization of %qD after instantiation "
5483 "of %qD", decl, inst_decl);
5484 }
5485 }
5486
5487 return decl;
5488 }
5489
5490 /* PARM is a template parameter of some form; return the corresponding
5491 TEMPLATE_PARM_INDEX. */
5492
5493 static tree
5494 get_template_parm_index (tree parm)
5495 {
5496 if (TREE_CODE (parm) == PARM_DECL
5497 || TREE_CODE (parm) == CONST_DECL)
5498 parm = DECL_INITIAL (parm);
5499 else if (TREE_CODE (parm) == TYPE_DECL
5500 || TREE_CODE (parm) == TEMPLATE_DECL)
5501 parm = TREE_TYPE (parm);
5502 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5503 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5504 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5505 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5506 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5507 return parm;
5508 }
5509
5510 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5511 parameter packs used by the template parameter PARM. */
5512
5513 static void
5514 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5515 {
5516 /* A type parm can't refer to another parm. */
5517 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5518 return;
5519 else if (TREE_CODE (parm) == PARM_DECL)
5520 {
5521 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5522 ppd, ppd->visited);
5523 return;
5524 }
5525
5526 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5527
5528 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5529 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5530 {
5531 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5532 if (template_parameter_pack_p (p))
5533 /* Any packs in the type are expanded by this parameter. */;
5534 else
5535 fixed_parameter_pack_p_1 (p, ppd);
5536 }
5537 }
5538
5539 /* PARM is a template parameter pack. Return any parameter packs used in
5540 its type or the type of any of its template parameters. If there are
5541 any such packs, it will be instantiated into a fixed template parameter
5542 list by partial instantiation rather than be fully deduced. */
5543
5544 tree
5545 fixed_parameter_pack_p (tree parm)
5546 {
5547 /* This can only be true in a member template. */
5548 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5549 return NULL_TREE;
5550 /* This can only be true for a parameter pack. */
5551 if (!template_parameter_pack_p (parm))
5552 return NULL_TREE;
5553 /* A type parm can't refer to another parm. */
5554 if (TREE_CODE (parm) == TYPE_DECL)
5555 return NULL_TREE;
5556
5557 tree parameter_packs = NULL_TREE;
5558 struct find_parameter_pack_data ppd;
5559 ppd.parameter_packs = &parameter_packs;
5560 ppd.visited = new hash_set<tree>;
5561 ppd.type_pack_expansion_p = false;
5562
5563 fixed_parameter_pack_p_1 (parm, &ppd);
5564
5565 delete ppd.visited;
5566 return parameter_packs;
5567 }
5568
5569 /* Check that a template declaration's use of default arguments and
5570 parameter packs is not invalid. Here, PARMS are the template
5571 parameters. IS_PRIMARY is true if DECL is the thing declared by
5572 a primary template. IS_PARTIAL is true if DECL is a partial
5573 specialization.
5574
5575 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5576 function template declaration or a friend class template
5577 declaration. In the function case, 1 indicates a declaration, 2
5578 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5579 emitted for extraneous default arguments.
5580
5581 Returns TRUE if there were no errors found, FALSE otherwise. */
5582
5583 bool
5584 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5585 bool is_partial, int is_friend_decl)
5586 {
5587 const char *msg;
5588 int last_level_to_check;
5589 tree parm_level;
5590 bool no_errors = true;
5591
5592 /* [temp.param]
5593
5594 A default template-argument shall not be specified in a
5595 function template declaration or a function template definition, nor
5596 in the template-parameter-list of the definition of a member of a
5597 class template. */
5598
5599 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5600 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5601 /* You can't have a function template declaration in a local
5602 scope, nor you can you define a member of a class template in a
5603 local scope. */
5604 return true;
5605
5606 if ((TREE_CODE (decl) == TYPE_DECL
5607 && TREE_TYPE (decl)
5608 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5609 || (TREE_CODE (decl) == FUNCTION_DECL
5610 && LAMBDA_FUNCTION_P (decl)))
5611 /* A lambda doesn't have an explicit declaration; don't complain
5612 about the parms of the enclosing class. */
5613 return true;
5614
5615 if (current_class_type
5616 && !TYPE_BEING_DEFINED (current_class_type)
5617 && DECL_LANG_SPECIFIC (decl)
5618 && DECL_DECLARES_FUNCTION_P (decl)
5619 /* If this is either a friend defined in the scope of the class
5620 or a member function. */
5621 && (DECL_FUNCTION_MEMBER_P (decl)
5622 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5623 : DECL_FRIEND_CONTEXT (decl)
5624 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5625 : false)
5626 /* And, if it was a member function, it really was defined in
5627 the scope of the class. */
5628 && (!DECL_FUNCTION_MEMBER_P (decl)
5629 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5630 /* We already checked these parameters when the template was
5631 declared, so there's no need to do it again now. This function
5632 was defined in class scope, but we're processing its body now
5633 that the class is complete. */
5634 return true;
5635
5636 /* Core issue 226 (C++0x only): the following only applies to class
5637 templates. */
5638 if (is_primary
5639 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5640 {
5641 /* [temp.param]
5642
5643 If a template-parameter has a default template-argument, all
5644 subsequent template-parameters shall have a default
5645 template-argument supplied. */
5646 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5647 {
5648 tree inner_parms = TREE_VALUE (parm_level);
5649 int ntparms = TREE_VEC_LENGTH (inner_parms);
5650 int seen_def_arg_p = 0;
5651 int i;
5652
5653 for (i = 0; i < ntparms; ++i)
5654 {
5655 tree parm = TREE_VEC_ELT (inner_parms, i);
5656
5657 if (parm == error_mark_node)
5658 continue;
5659
5660 if (TREE_PURPOSE (parm))
5661 seen_def_arg_p = 1;
5662 else if (seen_def_arg_p
5663 && !template_parameter_pack_p (TREE_VALUE (parm)))
5664 {
5665 error ("no default argument for %qD", TREE_VALUE (parm));
5666 /* For better subsequent error-recovery, we indicate that
5667 there should have been a default argument. */
5668 TREE_PURPOSE (parm) = error_mark_node;
5669 no_errors = false;
5670 }
5671 else if (!is_partial
5672 && !is_friend_decl
5673 /* Don't complain about an enclosing partial
5674 specialization. */
5675 && parm_level == parms
5676 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5677 && i < ntparms - 1
5678 && template_parameter_pack_p (TREE_VALUE (parm))
5679 /* A fixed parameter pack will be partially
5680 instantiated into a fixed length list. */
5681 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5682 {
5683 /* A primary class template, primary variable template
5684 (DR 2032), or alias template can only have one
5685 parameter pack, at the end of the template
5686 parameter list. */
5687
5688 error ("parameter pack %q+D must be at the end of the"
5689 " template parameter list", TREE_VALUE (parm));
5690
5691 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5692 = error_mark_node;
5693 no_errors = false;
5694 }
5695 }
5696 }
5697 }
5698
5699 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5700 || is_partial
5701 || !is_primary
5702 || is_friend_decl)
5703 /* For an ordinary class template, default template arguments are
5704 allowed at the innermost level, e.g.:
5705 template <class T = int>
5706 struct S {};
5707 but, in a partial specialization, they're not allowed even
5708 there, as we have in [temp.class.spec]:
5709
5710 The template parameter list of a specialization shall not
5711 contain default template argument values.
5712
5713 So, for a partial specialization, or for a function template
5714 (in C++98/C++03), we look at all of them. */
5715 ;
5716 else
5717 /* But, for a primary class template that is not a partial
5718 specialization we look at all template parameters except the
5719 innermost ones. */
5720 parms = TREE_CHAIN (parms);
5721
5722 /* Figure out what error message to issue. */
5723 if (is_friend_decl == 2)
5724 msg = G_("default template arguments may not be used in function template "
5725 "friend re-declaration");
5726 else if (is_friend_decl)
5727 msg = G_("default template arguments may not be used in template "
5728 "friend declarations");
5729 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5730 msg = G_("default template arguments may not be used in function templates "
5731 "without %<-std=c++11%> or %<-std=gnu++11%>");
5732 else if (is_partial)
5733 msg = G_("default template arguments may not be used in "
5734 "partial specializations");
5735 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5736 msg = G_("default argument for template parameter for class enclosing %qD");
5737 else
5738 /* Per [temp.param]/9, "A default template-argument shall not be
5739 specified in the template-parameter-lists of the definition of
5740 a member of a class template that appears outside of the member's
5741 class.", thus if we aren't handling a member of a class template
5742 there is no need to examine the parameters. */
5743 return true;
5744
5745 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5746 /* If we're inside a class definition, there's no need to
5747 examine the parameters to the class itself. On the one
5748 hand, they will be checked when the class is defined, and,
5749 on the other, default arguments are valid in things like:
5750 template <class T = double>
5751 struct S { template <class U> void f(U); };
5752 Here the default argument for `S' has no bearing on the
5753 declaration of `f'. */
5754 last_level_to_check = template_class_depth (current_class_type) + 1;
5755 else
5756 /* Check everything. */
5757 last_level_to_check = 0;
5758
5759 for (parm_level = parms;
5760 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5761 parm_level = TREE_CHAIN (parm_level))
5762 {
5763 tree inner_parms = TREE_VALUE (parm_level);
5764 int i;
5765 int ntparms;
5766
5767 ntparms = TREE_VEC_LENGTH (inner_parms);
5768 for (i = 0; i < ntparms; ++i)
5769 {
5770 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5771 continue;
5772
5773 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5774 {
5775 if (msg)
5776 {
5777 no_errors = false;
5778 if (is_friend_decl == 2)
5779 return no_errors;
5780
5781 error (msg, decl);
5782 msg = 0;
5783 }
5784
5785 /* Clear out the default argument so that we are not
5786 confused later. */
5787 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5788 }
5789 }
5790
5791 /* At this point, if we're still interested in issuing messages,
5792 they must apply to classes surrounding the object declared. */
5793 if (msg)
5794 msg = G_("default argument for template parameter for class "
5795 "enclosing %qD");
5796 }
5797
5798 return no_errors;
5799 }
5800
5801 /* Worker for push_template_decl_real, called via
5802 for_each_template_parm. DATA is really an int, indicating the
5803 level of the parameters we are interested in. If T is a template
5804 parameter of that level, return nonzero. */
5805
5806 static int
5807 template_parm_this_level_p (tree t, void* data)
5808 {
5809 int this_level = *(int *)data;
5810 int level;
5811
5812 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5813 level = TEMPLATE_PARM_LEVEL (t);
5814 else
5815 level = TEMPLATE_TYPE_LEVEL (t);
5816 return level == this_level;
5817 }
5818
5819 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5820 DATA is really an int, indicating the innermost outer level of parameters.
5821 If T is a template parameter of that level or further out, return
5822 nonzero. */
5823
5824 static int
5825 template_parm_outer_level (tree t, void *data)
5826 {
5827 int this_level = *(int *)data;
5828 int level;
5829
5830 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5831 level = TEMPLATE_PARM_LEVEL (t);
5832 else
5833 level = TEMPLATE_TYPE_LEVEL (t);
5834 return level <= this_level;
5835 }
5836
5837 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5838 parameters given by current_template_args, or reuses a
5839 previously existing one, if appropriate. Returns the DECL, or an
5840 equivalent one, if it is replaced via a call to duplicate_decls.
5841
5842 If IS_FRIEND is true, DECL is a friend declaration. */
5843
5844 tree
5845 push_template_decl (tree decl, bool is_friend)
5846 {
5847 if (decl == error_mark_node || !current_template_parms)
5848 return error_mark_node;
5849
5850 /* See if this is a partial specialization. */
5851 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5852 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5853 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5854 || (VAR_P (decl)
5855 && DECL_LANG_SPECIFIC (decl)
5856 && DECL_TEMPLATE_SPECIALIZATION (decl)
5857 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5858
5859 /* No surprising friend functions. */
5860 gcc_checking_assert (is_friend
5861 || !(TREE_CODE (decl) == FUNCTION_DECL
5862 && DECL_UNIQUE_FRIEND_P (decl)));
5863
5864 tree ctx;
5865 if (is_friend)
5866 /* For a friend, we want the context of the friend, not
5867 the type of which it is a friend. */
5868 ctx = CP_DECL_CONTEXT (decl);
5869 else if (CP_DECL_CONTEXT (decl)
5870 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5871 /* In the case of a virtual function, we want the class in which
5872 it is defined. */
5873 ctx = CP_DECL_CONTEXT (decl);
5874 else
5875 /* Otherwise, if we're currently defining some class, the DECL
5876 is assumed to be a member of the class. */
5877 ctx = current_scope ();
5878
5879 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5880 ctx = NULL_TREE;
5881
5882 if (!DECL_CONTEXT (decl))
5883 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5884
5885 /* See if this is a primary template. */
5886 bool is_primary = false;
5887 if (is_friend && ctx
5888 && uses_template_parms_level (ctx, current_template_depth))
5889 /* A friend template that specifies a class context, i.e.
5890 template <typename T> friend void A<T>::f();
5891 is not primary. */
5892 ;
5893 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5894 /* Lambdas are not primary. */
5895 ;
5896 else
5897 is_primary = template_parm_scope_p ();
5898
5899 /* True if the template is a member template, in the sense of
5900 [temp.mem]. */
5901 bool member_template_p = false;
5902
5903 if (is_primary)
5904 {
5905 warning (OPT_Wtemplates, "template %qD declared", decl);
5906
5907 if (DECL_CLASS_SCOPE_P (decl))
5908 member_template_p = true;
5909
5910 if (TREE_CODE (decl) == TYPE_DECL
5911 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5912 {
5913 error ("template class without a name");
5914 return error_mark_node;
5915 }
5916 else if (TREE_CODE (decl) == FUNCTION_DECL)
5917 {
5918 if (member_template_p)
5919 {
5920 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5921 error ("member template %qD may not have virt-specifiers", decl);
5922 }
5923 if (DECL_DESTRUCTOR_P (decl))
5924 {
5925 /* [temp.mem]
5926
5927 A destructor shall not be a member template. */
5928 error_at (DECL_SOURCE_LOCATION (decl),
5929 "destructor %qD declared as member template", decl);
5930 return error_mark_node;
5931 }
5932 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5933 && (!prototype_p (TREE_TYPE (decl))
5934 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5935 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5936 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5937 == void_list_node)))
5938 {
5939 /* [basic.stc.dynamic.allocation]
5940
5941 An allocation function can be a function
5942 template. ... Template allocation functions shall
5943 have two or more parameters. */
5944 error ("invalid template declaration of %qD", decl);
5945 return error_mark_node;
5946 }
5947 }
5948 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5949 && CLASS_TYPE_P (TREE_TYPE (decl)))
5950 /* Class template. */;
5951 else if (TREE_CODE (decl) == TYPE_DECL
5952 && TYPE_DECL_ALIAS_P (decl))
5953 /* alias-declaration */
5954 gcc_assert (!DECL_ARTIFICIAL (decl));
5955 else if (VAR_P (decl))
5956 /* C++14 variable template. */;
5957 else if (TREE_CODE (decl) == CONCEPT_DECL)
5958 /* C++20 concept definitions. */;
5959 else
5960 {
5961 error ("template declaration of %q#D", decl);
5962 return error_mark_node;
5963 }
5964 }
5965
5966 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5967 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5968 || (VAR_OR_FUNCTION_DECL_P (decl)
5969 && DECL_LOCAL_DECL_P (decl))));
5970
5971 /* Check to see that the rules regarding the use of default
5972 arguments are not being violated. We check args for a friend
5973 functions when we know whether it's a definition, introducing
5974 declaration or re-declaration. */
5975 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5976 check_default_tmpl_args (decl, current_template_parms,
5977 is_primary, is_partial, is_friend);
5978
5979 /* Ensure that there are no parameter packs in the type of this
5980 declaration that have not been expanded. */
5981 if (TREE_CODE (decl) == FUNCTION_DECL)
5982 {
5983 /* Check each of the arguments individually to see if there are
5984 any bare parameter packs. */
5985 tree type = TREE_TYPE (decl);
5986 tree arg = DECL_ARGUMENTS (decl);
5987 tree argtype = TYPE_ARG_TYPES (type);
5988
5989 while (arg && argtype)
5990 {
5991 if (!DECL_PACK_P (arg)
5992 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5993 {
5994 /* This is a PARM_DECL that contains unexpanded parameter
5995 packs. We have already complained about this in the
5996 check_for_bare_parameter_packs call, so just replace
5997 these types with ERROR_MARK_NODE. */
5998 TREE_TYPE (arg) = error_mark_node;
5999 TREE_VALUE (argtype) = error_mark_node;
6000 }
6001
6002 arg = DECL_CHAIN (arg);
6003 argtype = TREE_CHAIN (argtype);
6004 }
6005
6006 /* Check for bare parameter packs in the return type and the
6007 exception specifiers. */
6008 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
6009 /* Errors were already issued, set return type to int
6010 as the frontend doesn't expect error_mark_node as
6011 the return type. */
6012 TREE_TYPE (type) = integer_type_node;
6013 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
6014 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
6015 }
6016 else
6017 {
6018 if (check_for_bare_parameter_packs (is_typedef_decl (decl)
6019 ? DECL_ORIGINAL_TYPE (decl)
6020 : TREE_TYPE (decl)))
6021 {
6022 TREE_TYPE (decl) = error_mark_node;
6023 return error_mark_node;
6024 }
6025
6026 if (is_partial && VAR_P (decl)
6027 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
6028 return error_mark_node;
6029 }
6030
6031 if (is_partial)
6032 return process_partial_specialization (decl);
6033
6034 tree args = current_template_args ();
6035 tree tmpl = NULL_TREE;
6036 bool new_template_p = false;
6037 if (local_p)
6038 {
6039 /* Does not get a template head. */
6040 tmpl = NULL_TREE;
6041 gcc_checking_assert (!is_primary);
6042 }
6043 else if (!ctx
6044 || TREE_CODE (ctx) == FUNCTION_DECL
6045 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
6046 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
6047 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
6048 && DECL_TEMPLATE_INFO (decl))))
6049 {
6050 if (DECL_LANG_SPECIFIC (decl)
6051 && DECL_TEMPLATE_INFO (decl)
6052 && DECL_TI_TEMPLATE (decl))
6053 tmpl = DECL_TI_TEMPLATE (decl);
6054 /* If DECL is a TYPE_DECL for a class-template, then there won't
6055 be DECL_LANG_SPECIFIC. The information equivalent to
6056 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6057 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
6058 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
6059 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
6060 {
6061 /* Since a template declaration already existed for this
6062 class-type, we must be redeclaring it here. Make sure
6063 that the redeclaration is valid. */
6064 redeclare_class_template (TREE_TYPE (decl),
6065 current_template_parms,
6066 current_template_constraints ());
6067 /* We don't need to create a new TEMPLATE_DECL; just use the
6068 one we already had. */
6069 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6070 }
6071 else
6072 {
6073 tmpl = build_template_decl (decl, current_template_parms,
6074 member_template_p);
6075 new_template_p = true;
6076
6077 if (DECL_LANG_SPECIFIC (decl)
6078 && DECL_TEMPLATE_SPECIALIZATION (decl))
6079 {
6080 /* A specialization of a member template of a template
6081 class. */
6082 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
6083 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
6084 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
6085 }
6086 }
6087 }
6088 else
6089 {
6090 tree a, t, current, parms;
6091 int i;
6092 tree tinfo = get_template_info (decl);
6093
6094 if (!tinfo)
6095 {
6096 error ("template definition of non-template %q#D", decl);
6097 return error_mark_node;
6098 }
6099
6100 tmpl = TI_TEMPLATE (tinfo);
6101
6102 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
6103 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
6104 && DECL_TEMPLATE_SPECIALIZATION (decl)
6105 && DECL_MEMBER_TEMPLATE_P (tmpl))
6106 {
6107 /* The declaration is a specialization of a member
6108 template, declared outside the class. Therefore, the
6109 innermost template arguments will be NULL, so we
6110 replace them with the arguments determined by the
6111 earlier call to check_explicit_specialization. */
6112 args = DECL_TI_ARGS (decl);
6113
6114 tree new_tmpl
6115 = build_template_decl (decl, current_template_parms,
6116 member_template_p);
6117 DECL_TI_TEMPLATE (decl) = new_tmpl;
6118 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
6119 DECL_TEMPLATE_INFO (new_tmpl)
6120 = build_template_info (tmpl, args);
6121
6122 register_specialization (new_tmpl,
6123 most_general_template (tmpl),
6124 args,
6125 is_friend, 0);
6126 return decl;
6127 }
6128
6129 /* Make sure the template headers we got make sense. */
6130
6131 parms = DECL_TEMPLATE_PARMS (tmpl);
6132 i = TMPL_PARMS_DEPTH (parms);
6133 if (TMPL_ARGS_DEPTH (args) != i)
6134 {
6135 error ("expected %d levels of template parms for %q#D, got %d",
6136 i, decl, TMPL_ARGS_DEPTH (args));
6137 DECL_INTERFACE_KNOWN (decl) = 1;
6138 return error_mark_node;
6139 }
6140 else
6141 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6142 {
6143 a = TMPL_ARGS_LEVEL (args, i);
6144 t = INNERMOST_TEMPLATE_PARMS (parms);
6145
6146 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6147 {
6148 if (current == decl)
6149 error ("got %d template parameters for %q#D",
6150 TREE_VEC_LENGTH (a), decl);
6151 else
6152 error ("got %d template parameters for %q#T",
6153 TREE_VEC_LENGTH (a), current);
6154 error (" but %d required", TREE_VEC_LENGTH (t));
6155 /* Avoid crash in import_export_decl. */
6156 DECL_INTERFACE_KNOWN (decl) = 1;
6157 return error_mark_node;
6158 }
6159
6160 if (current == decl)
6161 current = ctx;
6162 else if (current == NULL_TREE)
6163 /* Can happen in erroneous input. */
6164 break;
6165 else
6166 current = get_containing_scope (current);
6167 }
6168
6169 /* Check that the parms are used in the appropriate qualifying scopes
6170 in the declarator. */
6171 if (!comp_template_args
6172 (TI_ARGS (tinfo),
6173 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6174 {
6175 error ("template arguments to %qD do not match original "
6176 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6177 if (!uses_template_parms (TI_ARGS (tinfo)))
6178 inform (input_location, "use %<template<>%> for"
6179 " an explicit specialization");
6180 /* Avoid crash in import_export_decl. */
6181 DECL_INTERFACE_KNOWN (decl) = 1;
6182 return error_mark_node;
6183 }
6184
6185 /* Check that the constraints for each enclosing template scope are
6186 consistent with the original declarations. */
6187 if (flag_concepts)
6188 {
6189 tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
6190 tree scope_parms = current_template_parms;
6191 if (PRIMARY_TEMPLATE_P (tmpl))
6192 {
6193 decl_parms = TREE_CHAIN (decl_parms);
6194 scope_parms = TREE_CHAIN (scope_parms);
6195 }
6196 while (decl_parms)
6197 {
6198 if (!template_requirements_equivalent_p (decl_parms, scope_parms))
6199 {
6200 error ("redeclaration of %qD with different constraints",
6201 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
6202 break;
6203 }
6204 decl_parms = TREE_CHAIN (decl_parms);
6205 scope_parms = TREE_CHAIN (scope_parms);
6206 }
6207 }
6208 }
6209
6210 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6211
6212 if (new_template_p)
6213 {
6214 /* Push template declarations for global functions and types.
6215 Note that we do not try to push a global template friend
6216 declared in a template class; such a thing may well depend on
6217 the template parameters of the class and we'll push it when
6218 instantiating the befriending class. */
6219 if (!ctx
6220 && !(is_friend && template_class_depth (current_class_type) > 0))
6221 {
6222 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6223 if (pushed == error_mark_node)
6224 return error_mark_node;
6225
6226 /* pushdecl may have found an existing template. */
6227 if (pushed != tmpl)
6228 {
6229 decl = DECL_TEMPLATE_RESULT (pushed);
6230 tmpl = NULL_TREE;
6231 }
6232 }
6233 else if (is_friend)
6234 {
6235 /* Record this decl as belonging to the current class. It's
6236 not chained onto anything else. */
6237 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6238 gcc_checking_assert (!DECL_CHAIN (tmpl));
6239 DECL_CHAIN (tmpl) = current_scope ();
6240 }
6241 }
6242 else if (tmpl)
6243 /* The type may have been completed, or (erroneously) changed. */
6244 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6245
6246 if (tmpl)
6247 {
6248 if (is_primary)
6249 {
6250 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6251
6252 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6253
6254 /* Give template template parms a DECL_CONTEXT of the template
6255 for which they are a parameter. */
6256 parms = INNERMOST_TEMPLATE_PARMS (parms);
6257 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6258 {
6259 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6260 if (TREE_CODE (parm) == TEMPLATE_DECL)
6261 DECL_CONTEXT (parm) = tmpl;
6262 }
6263
6264 if (TREE_CODE (decl) == TYPE_DECL
6265 && TYPE_DECL_ALIAS_P (decl))
6266 {
6267 if (tree constr
6268 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6269 {
6270 /* ??? Why don't we do this here for all templates? */
6271 constr = build_constraints (constr, NULL_TREE);
6272 set_constraints (decl, constr);
6273 }
6274 }
6275 }
6276
6277 /* The DECL_TI_ARGS of DECL contains full set of arguments
6278 referring wback to its most general template. If TMPL is a
6279 specialization, ARGS may only have the innermost set of
6280 arguments. Add the missing argument levels if necessary. */
6281 if (DECL_TEMPLATE_INFO (tmpl))
6282 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6283
6284 tree info = build_template_info (tmpl, args);
6285
6286 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6287 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6288 else
6289 {
6290 retrofit_lang_decl (decl);
6291 DECL_TEMPLATE_INFO (decl) = info;
6292 }
6293 }
6294
6295 if (flag_implicit_templates
6296 && !is_friend
6297 && TREE_PUBLIC (decl)
6298 && VAR_OR_FUNCTION_DECL_P (decl))
6299 /* Set DECL_COMDAT on template instantiations; if we force
6300 them to be emitted by explicit instantiation,
6301 mark_needed will tell cgraph to do the right thing. */
6302 DECL_COMDAT (decl) = true;
6303
6304 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6305
6306 return decl;
6307 }
6308
6309 /* FN is an inheriting constructor that inherits from the constructor
6310 template INHERITED; turn FN into a constructor template with a matching
6311 template header. */
6312
6313 tree
6314 add_inherited_template_parms (tree fn, tree inherited)
6315 {
6316 tree inner_parms
6317 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6318 inner_parms = copy_node (inner_parms);
6319 tree parms
6320 = tree_cons (size_int (current_template_depth + 1),
6321 inner_parms, current_template_parms);
6322 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6323 tree args = template_parms_to_args (parms);
6324 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6325 DECL_ARTIFICIAL (tmpl) = true;
6326 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6327 return tmpl;
6328 }
6329
6330 /* Called when a class template TYPE is redeclared with the indicated
6331 template PARMS, e.g.:
6332
6333 template <class T> struct S;
6334 template <class T> struct S {}; */
6335
6336 bool
6337 redeclare_class_template (tree type, tree parms, tree cons)
6338 {
6339 tree tmpl;
6340 tree tmpl_parms;
6341 int i;
6342
6343 if (!TYPE_TEMPLATE_INFO (type))
6344 {
6345 error ("%qT is not a template type", type);
6346 return false;
6347 }
6348
6349 tmpl = TYPE_TI_TEMPLATE (type);
6350 if (!PRIMARY_TEMPLATE_P (tmpl))
6351 /* The type is nested in some template class. Nothing to worry
6352 about here; there are no new template parameters for the nested
6353 type. */
6354 return true;
6355
6356 if (!parms)
6357 {
6358 error ("template specifiers not specified in declaration of %qD",
6359 tmpl);
6360 return false;
6361 }
6362
6363 parms = INNERMOST_TEMPLATE_PARMS (parms);
6364 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6365
6366 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6367 {
6368 error_n (input_location, TREE_VEC_LENGTH (parms),
6369 "redeclared with %d template parameter",
6370 "redeclared with %d template parameters",
6371 TREE_VEC_LENGTH (parms));
6372 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6373 "previous declaration %qD used %d template parameter",
6374 "previous declaration %qD used %d template parameters",
6375 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6376 return false;
6377 }
6378
6379 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6380 {
6381 tree tmpl_parm;
6382 tree parm;
6383
6384 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6385 || TREE_VEC_ELT (parms, i) == error_mark_node)
6386 continue;
6387
6388 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6389 if (error_operand_p (tmpl_parm))
6390 return false;
6391
6392 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6393
6394 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6395 TEMPLATE_DECL. */
6396 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6397 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6398 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6399 || (TREE_CODE (tmpl_parm) != PARM_DECL
6400 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6401 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6402 || (TREE_CODE (tmpl_parm) == PARM_DECL
6403 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6404 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6405 {
6406 auto_diagnostic_group d;
6407 error ("template parameter %q+#D", tmpl_parm);
6408 if (DECL_P (parm))
6409 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
6410 else
6411 inform (input_location, "redeclared here");
6412 return false;
6413 }
6414
6415 /* The parameters can be declared to introduce different
6416 constraints. */
6417 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6418 tree p2 = TREE_VEC_ELT (parms, i);
6419 if (!template_parameter_constraints_equivalent_p (p1, p2))
6420 {
6421 auto_diagnostic_group d;
6422 error ("declaration of template parameter %q+#D with different "
6423 "constraints", parm);
6424 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6425 "original declaration appeared here");
6426 return false;
6427 }
6428
6429 /* Give each template template parm in this redeclaration a
6430 DECL_CONTEXT of the template for which they are a parameter. */
6431 if (TREE_CODE (parm) == TEMPLATE_DECL)
6432 {
6433 gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
6434 || DECL_CONTEXT (parm) == tmpl);
6435 DECL_CONTEXT (parm) = tmpl;
6436 }
6437 }
6438
6439 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
6440 return false;
6441
6442 tree ci = get_constraints (tmpl);
6443 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6444 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6445
6446 /* Two classes with different constraints declare different entities. */
6447 if (!cp_tree_equal (req1, req2))
6448 {
6449 auto_diagnostic_group d;
6450 error_at (input_location, "redeclaration of %q#D with different "
6451 "constraints", tmpl);
6452 inform (DECL_SOURCE_LOCATION (tmpl),
6453 "original declaration appeared here");
6454 return false;
6455 }
6456
6457 return true;
6458 }
6459
6460 /* The actual substitution part of instantiate_non_dependent_expr,
6461 to be used when the caller has already checked
6462 !instantiation_dependent_uneval_expression_p (expr)
6463 and cleared processing_template_decl. */
6464
6465 tree
6466 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6467 {
6468 return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
6469 }
6470
6471 /* Instantiate the non-dependent expression EXPR. */
6472
6473 tree
6474 instantiate_non_dependent_expr (tree expr,
6475 tsubst_flags_t complain /* = tf_error */)
6476 {
6477 if (expr == NULL_TREE)
6478 return NULL_TREE;
6479
6480 if (processing_template_decl)
6481 {
6482 /* The caller should have checked this already. */
6483 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
6484 processing_template_decl_sentinel s;
6485 expr = instantiate_non_dependent_expr_internal (expr, complain);
6486 }
6487 return expr;
6488 }
6489
6490 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6491 expression is dependent or non-constant. */
6492
6493 tree
6494 instantiate_non_dependent_or_null (tree expr)
6495 {
6496 if (expr == NULL_TREE)
6497 return NULL_TREE;
6498 if (processing_template_decl)
6499 {
6500 if (!is_nondependent_constant_expression (expr))
6501 expr = NULL_TREE;
6502 else
6503 {
6504 processing_template_decl_sentinel s;
6505 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6506 }
6507 }
6508 return expr;
6509 }
6510
6511 /* True iff T is a specialization of a variable template. */
6512
6513 bool
6514 variable_template_specialization_p (tree t)
6515 {
6516 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6517 return false;
6518 tree tmpl = DECL_TI_TEMPLATE (t);
6519 return variable_template_p (tmpl);
6520 }
6521
6522 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6523 template declaration, or a TYPE_DECL for an alias declaration. */
6524
6525 bool
6526 alias_type_or_template_p (tree t)
6527 {
6528 if (t == NULL_TREE)
6529 return false;
6530 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6531 || (TYPE_P (t)
6532 && TYPE_NAME (t)
6533 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6534 || DECL_ALIAS_TEMPLATE_P (t));
6535 }
6536
6537 /* If T is a specialization of an alias template, return it; otherwise return
6538 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6539
6540 tree
6541 alias_template_specialization_p (const_tree t,
6542 bool transparent_typedefs)
6543 {
6544 if (!TYPE_P (t))
6545 return NULL_TREE;
6546
6547 /* It's an alias template specialization if it's an alias and its
6548 TYPE_NAME is a specialization of a primary template. */
6549 if (typedef_variant_p (t))
6550 {
6551 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6552 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6553 return CONST_CAST_TREE (t);
6554 if (transparent_typedefs)
6555 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6556 (TYPE_NAME (t)),
6557 transparent_typedefs);
6558 }
6559
6560 return NULL_TREE;
6561 }
6562
6563 /* A cache of the result of complex_alias_template_p. */
6564
6565 static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
6566
6567 /* Data structure for complex_alias_template_*. */
6568
6569 struct uses_all_template_parms_data
6570 {
6571 int level;
6572 tree *seen;
6573 };
6574
6575 /* walk_tree callback for complex_alias_template_p. */
6576
6577 static tree
6578 complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
6579 {
6580 tree t = *tp;
6581 auto &data = *(struct uses_all_template_parms_data*)data_;
6582
6583 switch (TREE_CODE (t))
6584 {
6585 case TEMPLATE_TYPE_PARM:
6586 case TEMPLATE_PARM_INDEX:
6587 case TEMPLATE_TEMPLATE_PARM:
6588 case BOUND_TEMPLATE_TEMPLATE_PARM:
6589 {
6590 tree idx = get_template_parm_index (t);
6591 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6592 data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
6593 }
6594
6595 default:;
6596 }
6597
6598 if (!PACK_EXPANSION_P (t))
6599 return 0;
6600
6601 /* An alias template with a pack expansion that expands a pack from the
6602 enclosing class needs to be considered complex, to avoid confusion with
6603 the same pack being used as an argument to the alias's own template
6604 parameter (91966). */
6605 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6606 pack = TREE_CHAIN (pack))
6607 {
6608 tree parm_pack = TREE_VALUE (pack);
6609 if (!TEMPLATE_PARM_P (parm_pack))
6610 continue;
6611 int idx, level;
6612 template_parm_level_and_index (parm_pack, &level, &idx);
6613 if (level < data.level)
6614 return t;
6615
6616 /* Consider the expanded packs to be used outside the expansion... */
6617 data.seen[idx] = boolean_true_node;
6618 }
6619
6620 /* ...but don't walk into the pattern. Consider PR104008:
6621
6622 template <typename T, typename... Ts>
6623 using IsOneOf = disjunction<is_same<T, Ts>...>;
6624
6625 where IsOneOf seemingly uses all of its template parameters in its
6626 expansion (and does not expand a pack from the enclosing class), so the
6627 alias was not marked as complex. However, if it is used like
6628 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6629 expansion. So only Ts is considered used by the pack expansion. */
6630 *walk_subtrees = false;
6631
6632 return 0;
6633 }
6634
6635 /* An alias template is complex from a SFINAE perspective if a template-id
6636 using that alias can be ill-formed when the expansion is not, as with
6637 the void_t template.
6638
6639 If this predicate returns true in the ordinary case, the out parameter
6640 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
6641 the I'th template parameter of the alias template is used in the alias. */
6642
6643 static bool
6644 complex_alias_template_p (const_tree tmpl, tree *seen_out)
6645 {
6646 tmpl = most_general_template (tmpl);
6647 if (!PRIMARY_TEMPLATE_P (tmpl))
6648 return false;
6649
6650 /* A renaming alias isn't complex. */
6651 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6652 return false;
6653
6654 /* Any other constrained alias is complex. */
6655 if (get_constraints (tmpl))
6656 return true;
6657
6658 if (!complex_alias_tmpl_info)
6659 complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
6660
6661 if (tree *slot = complex_alias_tmpl_info->get (tmpl))
6662 {
6663 tree result = *slot;
6664 if (result == boolean_false_node)
6665 return false;
6666 if (result == boolean_true_node)
6667 return true;
6668 gcc_assert (TREE_CODE (result) == TREE_VEC);
6669 if (seen_out)
6670 *seen_out = result;
6671 return true;
6672 }
6673
6674 struct uses_all_template_parms_data data;
6675 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6676 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6677 data.level = TMPL_PARMS_DEPTH (parms);
6678 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6679 tree seen = make_tree_vec (len);
6680 data.seen = TREE_VEC_BEGIN (seen);
6681 for (int i = 0; i < len; ++i)
6682 data.seen[i] = boolean_false_node;
6683
6684 if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
6685 {
6686 complex_alias_tmpl_info->put (tmpl, boolean_true_node);
6687 return true;
6688 }
6689
6690 for (int i = 0; i < len; ++i)
6691 if (data.seen[i] != boolean_true_node)
6692 {
6693 complex_alias_tmpl_info->put (tmpl, seen);
6694 if (seen_out)
6695 *seen_out = seen;
6696 return true;
6697 }
6698
6699 complex_alias_tmpl_info->put (tmpl, boolean_false_node);
6700 return false;
6701 }
6702
6703 /* If T is a specialization of a complex alias template with a dependent
6704 argument for an unused template parameter, return it; otherwise return
6705 NULL_TREE. If T is a typedef to such a specialization, return the
6706 specialization. */
6707
6708 tree
6709 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6710 {
6711 if (t == error_mark_node)
6712 return NULL_TREE;
6713 gcc_assert (TYPE_P (t));
6714
6715 if (!processing_template_decl || !typedef_variant_p (t))
6716 return NULL_TREE;
6717
6718 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6719 {
6720 tree seen = NULL_TREE;
6721 if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
6722 {
6723 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6724 if (!seen)
6725 {
6726 if (any_dependent_template_arguments_p (args))
6727 return CONST_CAST_TREE (t);
6728 }
6729 else
6730 {
6731 gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
6732 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
6733 if (TREE_VEC_ELT (seen, i) != boolean_true_node
6734 && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
6735 return CONST_CAST_TREE (t);
6736 }
6737
6738 return NULL_TREE;
6739 }
6740 }
6741
6742 if (transparent_typedefs)
6743 {
6744 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6745 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6746 }
6747
6748 return NULL_TREE;
6749 }
6750
6751 /* Return the number of innermost template parameters in TMPL. */
6752
6753 static int
6754 num_innermost_template_parms (const_tree tmpl)
6755 {
6756 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6757 return TREE_VEC_LENGTH (parms);
6758 }
6759
6760 /* Return either TMPL or another template that it is equivalent to under DR
6761 1286: An alias that just changes the name of a template is equivalent to
6762 the other template. */
6763
6764 static tree
6765 get_underlying_template (tree tmpl)
6766 {
6767 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6768 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6769 {
6770 /* Determine if the alias is equivalent to an underlying template. */
6771 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6772 /* The underlying type may have been ill-formed. Don't proceed. */
6773 if (!orig_type)
6774 break;
6775 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6776 if (!tinfo)
6777 break;
6778
6779 tree underlying = TI_TEMPLATE (tinfo);
6780 if (!PRIMARY_TEMPLATE_P (underlying)
6781 || (num_innermost_template_parms (tmpl)
6782 != num_innermost_template_parms (underlying)))
6783 break;
6784
6785 /* Does the alias add cv-quals? */
6786 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6787 break;
6788
6789 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6790 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6791 break;
6792
6793 /* Are any default template arguments equivalent? */
6794 tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6795 tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
6796 const int nparms = TREE_VEC_LENGTH (aparms);
6797 for (int i = 0; i < nparms; ++i)
6798 {
6799 tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
6800 tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
6801 if (!template_args_equal (adefarg, udefarg))
6802 goto top_break;
6803 }
6804
6805 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6806 it's appropriate to treat a less-constrained alias as equivalent. */
6807 if (!at_least_as_constrained (underlying, tmpl))
6808 break;
6809
6810 /* Alias is equivalent. Strip it and repeat. */
6811 tmpl = underlying;
6812 }
6813 top_break:;
6814
6815 return tmpl;
6816 }
6817
6818 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6819 must be a reference-to-function or a pointer-to-function type, as specified
6820 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6821 and check that the resulting function has external linkage. */
6822
6823 static tree
6824 convert_nontype_argument_function (tree type, tree expr,
6825 tsubst_flags_t complain)
6826 {
6827 tree fns = expr;
6828 tree fn, fn_no_ptr;
6829 linkage_kind linkage;
6830
6831 fn = instantiate_type (type, fns, tf_none);
6832 if (fn == error_mark_node)
6833 return error_mark_node;
6834
6835 if (value_dependent_expression_p (fn))
6836 goto accept;
6837
6838 fn_no_ptr = fn;
6839 if (REFERENCE_REF_P (fn_no_ptr))
6840 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6841 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6842 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6843 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6844 if (BASELINK_P (fn_no_ptr))
6845 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6846
6847 /* [temp.arg.nontype]/1
6848
6849 A template-argument for a non-type, non-template template-parameter
6850 shall be one of:
6851 [...]
6852 -- the address of an object or function with external [C++11: or
6853 internal] linkage. */
6854
6855 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6856 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6857 {
6858 if (complain & tf_error)
6859 {
6860 location_t loc = cp_expr_loc_or_input_loc (expr);
6861 error_at (loc, "%qE is not a valid template argument for type %qT",
6862 expr, type);
6863 if (TYPE_PTR_P (type))
6864 inform (loc, "it must be the address of a function "
6865 "with external linkage");
6866 else
6867 inform (loc, "it must be the name of a function with "
6868 "external linkage");
6869 }
6870 return NULL_TREE;
6871 }
6872
6873 linkage = decl_linkage (fn_no_ptr);
6874 if ((cxx_dialect < cxx11 && linkage != lk_external)
6875 || (cxx_dialect < cxx17 && linkage == lk_none))
6876 {
6877 if (complain & tf_error)
6878 {
6879 location_t loc = cp_expr_loc_or_input_loc (expr);
6880 if (cxx_dialect >= cxx11)
6881 error_at (loc, "%qE is not a valid template argument for type "
6882 "%qT because %qD has no linkage",
6883 expr, type, fn_no_ptr);
6884 else
6885 error_at (loc, "%qE is not a valid template argument for type "
6886 "%qT because %qD does not have external linkage",
6887 expr, type, fn_no_ptr);
6888 }
6889 return NULL_TREE;
6890 }
6891
6892 accept:
6893 if (TYPE_REF_P (type))
6894 {
6895 if (REFERENCE_REF_P (fn))
6896 fn = TREE_OPERAND (fn, 0);
6897 else
6898 fn = build_address (fn);
6899 }
6900 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6901 fn = build_nop (type, fn);
6902
6903 return fn;
6904 }
6905
6906 /* Subroutine of convert_nontype_argument.
6907 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6908 Emit an error otherwise. */
6909
6910 static bool
6911 check_valid_ptrmem_cst_expr (tree type, tree expr,
6912 tsubst_flags_t complain)
6913 {
6914 tree orig_expr = expr;
6915 STRIP_NOPS (expr);
6916 if (null_ptr_cst_p (expr))
6917 return true;
6918 if (TREE_CODE (expr) == PTRMEM_CST
6919 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6920 PTRMEM_CST_CLASS (expr)))
6921 return true;
6922 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6923 return true;
6924 if (processing_template_decl
6925 && TREE_CODE (expr) == ADDR_EXPR
6926 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6927 return true;
6928 if (complain & tf_error)
6929 {
6930 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6931 error_at (loc, "%qE is not a valid template argument for type %qT",
6932 orig_expr, type);
6933 if (TREE_CODE (expr) != PTRMEM_CST)
6934 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6935 else
6936 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6937 }
6938 return false;
6939 }
6940
6941 /* Returns TRUE iff the address of OP is value-dependent.
6942
6943 14.6.2.4 [temp.dep.temp]:
6944 A non-integral non-type template-argument is dependent if its type is
6945 dependent or it has either of the following forms
6946 qualified-id
6947 & qualified-id
6948 and contains a nested-name-specifier which specifies a class-name that
6949 names a dependent type.
6950
6951 We generalize this to just say that the address of a member of a
6952 dependent class is value-dependent; the above doesn't cover the
6953 address of a static data member named with an unqualified-id. */
6954
6955 static bool
6956 has_value_dependent_address (tree op)
6957 {
6958 STRIP_ANY_LOCATION_WRAPPER (op);
6959
6960 /* We could use get_inner_reference here, but there's no need;
6961 this is only relevant for template non-type arguments, which
6962 can only be expressed as &id-expression. */
6963 if (DECL_P (op))
6964 {
6965 tree ctx = CP_DECL_CONTEXT (op);
6966
6967 if (TYPE_P (ctx) && dependent_type_p (ctx))
6968 return true;
6969
6970 if (VAR_P (op)
6971 && TREE_STATIC (op)
6972 && TREE_CODE (ctx) == FUNCTION_DECL
6973 && type_dependent_expression_p (ctx))
6974 return true;
6975 }
6976
6977 return false;
6978 }
6979
6980 /* The next set of functions are used for providing helpful explanatory
6981 diagnostics for failed overload resolution. Their messages should be
6982 indented by two spaces for consistency with the messages in
6983 call.cc */
6984
6985 static int
6986 unify_success (bool /*explain_p*/)
6987 {
6988 return 0;
6989 }
6990
6991 /* Other failure functions should call this one, to provide a single function
6992 for setting a breakpoint on. */
6993
6994 static int
6995 unify_invalid (bool /*explain_p*/)
6996 {
6997 return 1;
6998 }
6999
7000 static int
7001 unify_parameter_deduction_failure (bool explain_p, tree parm)
7002 {
7003 if (explain_p)
7004 inform (input_location,
7005 " couldn%'t deduce template parameter %qD", parm);
7006 return unify_invalid (explain_p);
7007 }
7008
7009 static int
7010 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
7011 {
7012 if (explain_p)
7013 inform (input_location,
7014 " types %qT and %qT have incompatible cv-qualifiers",
7015 parm, arg);
7016 return unify_invalid (explain_p);
7017 }
7018
7019 static int
7020 unify_type_mismatch (bool explain_p, tree parm, tree arg)
7021 {
7022 if (explain_p)
7023 inform (input_location, " mismatched types %qT and %qT", parm, arg);
7024 return unify_invalid (explain_p);
7025 }
7026
7027 static int
7028 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
7029 {
7030 if (explain_p)
7031 inform (input_location,
7032 " template parameter %qD is not a parameter pack, but "
7033 "argument %qD is",
7034 parm, arg);
7035 return unify_invalid (explain_p);
7036 }
7037
7038 static int
7039 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
7040 {
7041 if (explain_p)
7042 inform (input_location,
7043 " template argument %qE does not match "
7044 "pointer-to-member constant %qE",
7045 arg, parm);
7046 return unify_invalid (explain_p);
7047 }
7048
7049 static int
7050 unify_expression_unequal (bool explain_p, tree parm, tree arg)
7051 {
7052 if (explain_p)
7053 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
7054 return unify_invalid (explain_p);
7055 }
7056
7057 static int
7058 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
7059 {
7060 if (explain_p)
7061 inform (input_location,
7062 " inconsistent parameter pack deduction with %qT and %qT",
7063 old_arg, new_arg);
7064 return unify_invalid (explain_p);
7065 }
7066
7067 static int
7068 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
7069 {
7070 if (explain_p)
7071 {
7072 if (TYPE_P (parm))
7073 inform (input_location,
7074 " deduced conflicting types for parameter %qT (%qT and %qT)",
7075 parm, first, second);
7076 else
7077 inform (input_location,
7078 " deduced conflicting values for non-type parameter "
7079 "%qE (%qE and %qE)", parm, first, second);
7080 }
7081 return unify_invalid (explain_p);
7082 }
7083
7084 static int
7085 unify_vla_arg (bool explain_p, tree arg)
7086 {
7087 if (explain_p)
7088 inform (input_location,
7089 " variable-sized array type %qT is not "
7090 "a valid template argument",
7091 arg);
7092 return unify_invalid (explain_p);
7093 }
7094
7095 static int
7096 unify_method_type_error (bool explain_p, tree arg)
7097 {
7098 if (explain_p)
7099 inform (input_location,
7100 " member function type %qT is not a valid template argument",
7101 arg);
7102 return unify_invalid (explain_p);
7103 }
7104
7105 static int
7106 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
7107 {
7108 if (explain_p)
7109 {
7110 if (least_p)
7111 inform_n (input_location, wanted,
7112 " candidate expects at least %d argument, %d provided",
7113 " candidate expects at least %d arguments, %d provided",
7114 wanted, have);
7115 else
7116 inform_n (input_location, wanted,
7117 " candidate expects %d argument, %d provided",
7118 " candidate expects %d arguments, %d provided",
7119 wanted, have);
7120 }
7121 return unify_invalid (explain_p);
7122 }
7123
7124 static int
7125 unify_too_many_arguments (bool explain_p, int have, int wanted)
7126 {
7127 return unify_arity (explain_p, have, wanted);
7128 }
7129
7130 static int
7131 unify_too_few_arguments (bool explain_p, int have, int wanted,
7132 bool least_p = false)
7133 {
7134 return unify_arity (explain_p, have, wanted, least_p);
7135 }
7136
7137 static int
7138 unify_arg_conversion (bool explain_p, tree to_type,
7139 tree from_type, tree arg)
7140 {
7141 if (explain_p)
7142 inform (cp_expr_loc_or_input_loc (arg),
7143 " cannot convert %qE (type %qT) to type %qT",
7144 arg, from_type, to_type);
7145 return unify_invalid (explain_p);
7146 }
7147
7148 static int
7149 unify_no_common_base (bool explain_p, enum template_base_result r,
7150 tree parm, tree arg)
7151 {
7152 if (explain_p)
7153 switch (r)
7154 {
7155 case tbr_ambiguous_baseclass:
7156 inform (input_location, " %qT is an ambiguous base class of %qT",
7157 parm, arg);
7158 break;
7159 default:
7160 inform (input_location, " %qT is not derived from %qT", arg, parm);
7161 break;
7162 }
7163 return unify_invalid (explain_p);
7164 }
7165
7166 static int
7167 unify_inconsistent_template_template_parameters (bool explain_p)
7168 {
7169 if (explain_p)
7170 inform (input_location,
7171 " template parameters of a template template argument are "
7172 "inconsistent with other deduced template arguments");
7173 return unify_invalid (explain_p);
7174 }
7175
7176 static int
7177 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
7178 {
7179 if (explain_p)
7180 inform (input_location,
7181 " cannot deduce a template for %qT from non-template type %qT",
7182 parm, arg);
7183 return unify_invalid (explain_p);
7184 }
7185
7186 static int
7187 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
7188 {
7189 if (explain_p)
7190 inform (input_location,
7191 " template argument %qE does not match %qE", arg, parm);
7192 return unify_invalid (explain_p);
7193 }
7194
7195 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7196 argument for TYPE, points to an unsuitable object.
7197
7198 Also adjust the type of the index in C++20 array subobject references. */
7199
7200 static bool
7201 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7202 {
7203 switch (TREE_CODE (expr))
7204 {
7205 CASE_CONVERT:
7206 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7207 complain);
7208
7209 case TARGET_EXPR:
7210 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7211 complain);
7212
7213 case CONSTRUCTOR:
7214 {
7215 for (auto &e: CONSTRUCTOR_ELTS (expr))
7216 if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
7217 return true;
7218 }
7219 break;
7220
7221 case ADDR_EXPR:
7222 {
7223 tree decl = TREE_OPERAND (expr, 0);
7224
7225 if (cxx_dialect >= cxx20)
7226 while (TREE_CODE (decl) == COMPONENT_REF
7227 || TREE_CODE (decl) == ARRAY_REF)
7228 {
7229 tree &op = TREE_OPERAND (decl, 1);
7230 if (TREE_CODE (decl) == ARRAY_REF
7231 && TREE_CODE (op) == INTEGER_CST)
7232 /* Canonicalize array offsets to ptrdiff_t; how they were
7233 written doesn't matter for subobject identity. */
7234 op = fold_convert (ptrdiff_type_node, op);
7235 decl = TREE_OPERAND (decl, 0);
7236 }
7237
7238 if (!VAR_OR_FUNCTION_DECL_P (decl))
7239 {
7240 if (complain & tf_error)
7241 error_at (cp_expr_loc_or_input_loc (expr),
7242 "%qE is not a valid template argument of type %qT "
7243 "because %qE is not a variable or function",
7244 expr, type, decl);
7245 return true;
7246 }
7247 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7248 {
7249 if (complain & tf_error)
7250 error_at (cp_expr_loc_or_input_loc (expr),
7251 "%qE is not a valid template argument of type %qT "
7252 "in C++98 because %qD does not have external linkage",
7253 expr, type, decl);
7254 return true;
7255 }
7256 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7257 && decl_linkage (decl) == lk_none)
7258 {
7259 if (complain & tf_error)
7260 error_at (cp_expr_loc_or_input_loc (expr),
7261 "%qE is not a valid template argument of type %qT "
7262 "because %qD has no linkage", expr, type, decl);
7263 return true;
7264 }
7265 /* C++17: For a non-type template-parameter of reference or pointer
7266 type, the value of the constant expression shall not refer to (or
7267 for a pointer type, shall not be the address of):
7268 * a subobject (4.5),
7269 * a temporary object (15.2),
7270 * a string literal (5.13.5),
7271 * the result of a typeid expression (8.2.8), or
7272 * a predefined __func__ variable (11.4.1). */
7273 else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
7274 && !DECL_NTTP_OBJECT_P (decl))
7275 {
7276 gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl));
7277 if (complain & tf_error)
7278 error ("the address of %qD is not a valid template argument",
7279 decl);
7280 return true;
7281 }
7282 else if (cxx_dialect < cxx20
7283 && !(same_type_ignoring_top_level_qualifiers_p
7284 (strip_array_types (TREE_TYPE (type)),
7285 strip_array_types (TREE_TYPE (decl)))))
7286 {
7287 if (complain & tf_error)
7288 error ("the address of the %qT subobject of %qD is not a "
7289 "valid template argument", TREE_TYPE (type), decl);
7290 return true;
7291 }
7292 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7293 {
7294 if (complain & tf_error)
7295 error ("the address of %qD is not a valid template argument "
7296 "because it does not have static storage duration",
7297 decl);
7298 return true;
7299 }
7300 }
7301 break;
7302
7303 default:
7304 if (!INDIRECT_TYPE_P (type))
7305 /* We're only concerned about pointers and references here. */;
7306 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7307 /* Null pointer values are OK in C++11. */;
7308 else
7309 {
7310 if (VAR_P (expr))
7311 {
7312 if (complain & tf_error)
7313 error ("%qD is not a valid template argument "
7314 "because %qD is a variable, not the address of "
7315 "a variable", expr, expr);
7316 return true;
7317 }
7318 else
7319 {
7320 if (complain & tf_error)
7321 error ("%qE is not a valid template argument for %qT "
7322 "because it is not the address of a variable",
7323 expr, type);
7324 return true;
7325 }
7326 }
7327 }
7328 return false;
7329
7330 }
7331
7332 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7333 template argument EXPR. */
7334
7335 static tree
7336 create_template_parm_object (tree expr, tsubst_flags_t complain)
7337 {
7338 tree orig = expr;
7339 if (TREE_CODE (expr) == TARGET_EXPR)
7340 expr = TARGET_EXPR_INITIAL (expr);
7341
7342 if (!TREE_CONSTANT (expr))
7343 {
7344 if ((complain & tf_error)
7345 && require_rvalue_constant_expression (orig))
7346 cxx_constant_value (orig);
7347 return error_mark_node;
7348 }
7349 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7350 return error_mark_node;
7351
7352 /* This is no longer a compound literal. */
7353 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7354
7355 return get_template_parm_object (expr, mangle_template_parm_object (expr));
7356 }
7357
7358 /* The template arguments corresponding to template parameter objects of types
7359 that contain pointers to members. */
7360
7361 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7362
7363 /* Find or build an nttp object for (already-validated) EXPR with name
7364 NAME. */
7365
7366 tree
7367 get_template_parm_object (tree expr, tree name)
7368 {
7369 tree decl = get_global_binding (name);
7370 if (decl)
7371 return decl;
7372
7373 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7374 decl = create_temporary_var (type);
7375 DECL_NTTP_OBJECT_P (decl) = true;
7376 DECL_CONTEXT (decl) = NULL_TREE;
7377 TREE_STATIC (decl) = true;
7378 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7379 TREE_READONLY (decl) = true;
7380 DECL_NAME (decl) = name;
7381 SET_DECL_ASSEMBLER_NAME (decl, name);
7382 comdat_linkage (decl);
7383
7384 if (!zero_init_p (type))
7385 {
7386 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7387 lower_var_init before we're done mangling. So store the original
7388 value elsewhere. */
7389 tree copy = unshare_constructor (expr);
7390 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7391 }
7392
7393 pushdecl_top_level_and_finish (decl, expr);
7394
7395 return decl;
7396 }
7397
7398 /* Return the actual template argument corresponding to template parameter
7399 object VAR. */
7400
7401 tree
7402 tparm_object_argument (tree var)
7403 {
7404 if (zero_init_p (TREE_TYPE (var)))
7405 return DECL_INITIAL (var);
7406 return *(tparm_obj_values->get (var));
7407 }
7408
7409 /* Attempt to convert the non-type template parameter EXPR to the
7410 indicated TYPE. If the conversion is successful, return the
7411 converted value. If the conversion is unsuccessful, return
7412 NULL_TREE if we issued an error message, or error_mark_node if we
7413 did not. We issue error messages for out-and-out bad template
7414 parameters, but not simply because the conversion failed, since we
7415 might be just trying to do argument deduction. Both TYPE and EXPR
7416 must be non-dependent.
7417
7418 The conversion follows the special rules described in
7419 [temp.arg.nontype], and it is much more strict than an implicit
7420 conversion.
7421
7422 This function is called twice for each template argument (see
7423 lookup_template_class for a more accurate description of this
7424 problem). This means that we need to handle expressions which
7425 are not valid in a C++ source, but can be created from the
7426 first call (for instance, casts to perform conversions). These
7427 hacks can go away after we fix the double coercion problem. */
7428
7429 static tree
7430 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7431 {
7432 tree expr_type;
7433 location_t loc = cp_expr_loc_or_input_loc (expr);
7434
7435 /* Detect immediately string literals as invalid non-type argument.
7436 This special-case is not needed for correctness (we would easily
7437 catch this later), but only to provide better diagnostic for this
7438 common user mistake. As suggested by DR 100, we do not mention
7439 linkage issues in the diagnostic as this is not the point. */
7440 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7441 {
7442 if (complain & tf_error)
7443 error ("%qE is not a valid template argument for type %qT "
7444 "because string literals can never be used in this context",
7445 expr, type);
7446 return NULL_TREE;
7447 }
7448
7449 /* Add the ADDR_EXPR now for the benefit of
7450 value_dependent_expression_p. */
7451 if (TYPE_PTROBV_P (type)
7452 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7453 {
7454 expr = decay_conversion (expr, complain);
7455 if (expr == error_mark_node)
7456 return error_mark_node;
7457 }
7458
7459 /* If we are in a template, EXPR may be non-dependent, but still
7460 have a syntactic, rather than semantic, form. For example, EXPR
7461 might be a SCOPE_REF, rather than the VAR_DECL to which the
7462 SCOPE_REF refers. Preserving the qualifying scope is necessary
7463 so that access checking can be performed when the template is
7464 instantiated -- but here we need the resolved form so that we can
7465 convert the argument. */
7466 bool non_dep = false;
7467 if (TYPE_REF_OBJ_P (type)
7468 && has_value_dependent_address (expr))
7469 /* If we want the address and it's value-dependent, don't fold. */;
7470 else if (processing_template_decl
7471 && !instantiation_dependent_expression_p (expr))
7472 non_dep = true;
7473 if (error_operand_p (expr))
7474 return error_mark_node;
7475 expr_type = TREE_TYPE (expr);
7476
7477 /* If the argument is non-dependent, perform any conversions in
7478 non-dependent context as well. */
7479 processing_template_decl_sentinel s (non_dep);
7480 if (non_dep)
7481 expr = instantiate_non_dependent_expr_internal (expr, complain);
7482
7483 bool val_dep_p = value_dependent_expression_p (expr);
7484 if (val_dep_p)
7485 expr = canonicalize_expr_argument (expr, complain);
7486 else
7487 STRIP_ANY_LOCATION_WRAPPER (expr);
7488
7489 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7490 to a non-type argument of "nullptr". */
7491 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7492 expr = fold_simple (convert (type, expr));
7493
7494 /* In C++11, integral or enumeration non-type template arguments can be
7495 arbitrary constant expressions. Pointer and pointer to
7496 member arguments can be general constant expressions that evaluate
7497 to a null value, but otherwise still need to be of a specific form. */
7498 if (cxx_dialect >= cxx11)
7499 {
7500 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7501 /* A PTRMEM_CST is already constant, and a valid template
7502 argument for a parameter of pointer to member type, we just want
7503 to leave it in that form rather than lower it to a
7504 CONSTRUCTOR. */;
7505 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7506 || cxx_dialect >= cxx17)
7507 {
7508 /* C++17: A template-argument for a non-type template-parameter shall
7509 be a converted constant expression (8.20) of the type of the
7510 template-parameter. */
7511 expr = build_converted_constant_expr (type, expr, complain);
7512 if (expr == error_mark_node)
7513 /* Make sure we return NULL_TREE only if we have really issued
7514 an error, as described above. */
7515 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7516 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7517 {
7518 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7519 return expr;
7520 }
7521 expr = maybe_constant_value (expr, NULL_TREE, mce_true);
7522 expr = convert_from_reference (expr);
7523 /* EXPR may have become value-dependent. */
7524 val_dep_p = value_dependent_expression_p (expr);
7525 }
7526 else if (TYPE_PTR_OR_PTRMEM_P (type))
7527 {
7528 tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
7529 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7530 : null_member_pointer_value_p (folded))
7531 expr = folded;
7532 }
7533 }
7534
7535 if (TYPE_REF_P (type))
7536 expr = mark_lvalue_use (expr);
7537 else
7538 expr = mark_rvalue_use (expr);
7539
7540 /* HACK: Due to double coercion, we can get a
7541 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7542 which is the tree that we built on the first call (see
7543 below when coercing to reference to object or to reference to
7544 function). We just strip everything and get to the arg.
7545 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7546 for examples. */
7547 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7548 {
7549 /* Check this before we strip *& to avoid redundancy. */
7550 if (!mark_single_function (expr, complain))
7551 return error_mark_node;
7552
7553 tree probe_type, probe = expr;
7554 if (REFERENCE_REF_P (probe))
7555 probe = TREE_OPERAND (probe, 0);
7556 probe_type = TREE_TYPE (probe);
7557 if (TREE_CODE (probe) == NOP_EXPR)
7558 {
7559 /* ??? Maybe we could use convert_from_reference here, but we
7560 would need to relax its constraints because the NOP_EXPR
7561 could actually change the type to something more cv-qualified,
7562 and this is not folded by convert_from_reference. */
7563 tree addr = TREE_OPERAND (probe, 0);
7564 if (TYPE_REF_P (probe_type)
7565 && TREE_CODE (addr) == ADDR_EXPR
7566 && TYPE_PTR_P (TREE_TYPE (addr))
7567 && (same_type_ignoring_top_level_qualifiers_p
7568 (TREE_TYPE (probe_type),
7569 TREE_TYPE (TREE_TYPE (addr)))))
7570 {
7571 expr = TREE_OPERAND (addr, 0);
7572 expr_type = TREE_TYPE (probe_type);
7573 }
7574 }
7575 }
7576
7577 /* [temp.arg.nontype]/5, bullet 1
7578
7579 For a non-type template-parameter of integral or enumeration type,
7580 integral promotions (_conv.prom_) and integral conversions
7581 (_conv.integral_) are applied. */
7582 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7583 || SCALAR_FLOAT_TYPE_P (type))
7584 {
7585 if (cxx_dialect < cxx11)
7586 {
7587 tree t = build_converted_constant_expr (type, expr, complain);
7588 t = maybe_constant_value (t);
7589 if (t != error_mark_node)
7590 expr = t;
7591 }
7592
7593 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7594 return error_mark_node;
7595
7596 /* Notice that there are constant expressions like '4 % 0' which
7597 do not fold into integer constants. */
7598 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7599 {
7600 if (complain & tf_error)
7601 {
7602 int errs = errorcount, warns = warningcount + werrorcount;
7603 if (!require_potential_constant_expression (expr))
7604 expr = error_mark_node;
7605 else
7606 expr = cxx_constant_value (expr);
7607 if (errorcount > errs || warningcount + werrorcount > warns)
7608 inform (loc, "in template argument for type %qT", type);
7609 if (expr == error_mark_node)
7610 return NULL_TREE;
7611 /* else cxx_constant_value complained but gave us
7612 a real constant, so go ahead. */
7613 if (!CONSTANT_CLASS_P (expr))
7614 {
7615 /* Some assemble time constant expressions like
7616 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7617 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7618 as we can emit them into .rodata initializers of
7619 variables, yet they can't fold into an INTEGER_CST at
7620 compile time. Refuse them here. */
7621 gcc_checking_assert (reduced_constant_expression_p (expr));
7622 error_at (loc, "template argument %qE for type %qT not "
7623 "a compile-time constant", expr, type);
7624 return NULL_TREE;
7625 }
7626 }
7627 else
7628 return NULL_TREE;
7629 }
7630
7631 /* Avoid typedef problems. */
7632 if (TREE_TYPE (expr) != type)
7633 expr = fold_convert (type, expr);
7634 }
7635 /* [temp.arg.nontype]/5, bullet 2
7636
7637 For a non-type template-parameter of type pointer to object,
7638 qualification conversions (_conv.qual_) and the array-to-pointer
7639 conversion (_conv.array_) are applied. */
7640 else if (TYPE_PTROBV_P (type))
7641 {
7642 tree decayed = expr;
7643
7644 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7645 decay_conversion or an explicit cast. If it's a problematic cast,
7646 we'll complain about it below. */
7647 if (TREE_CODE (expr) == NOP_EXPR)
7648 {
7649 tree probe = expr;
7650 STRIP_NOPS (probe);
7651 if (TREE_CODE (probe) == ADDR_EXPR
7652 && TYPE_PTR_P (TREE_TYPE (probe)))
7653 {
7654 expr = probe;
7655 expr_type = TREE_TYPE (expr);
7656 }
7657 }
7658
7659 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7660
7661 A template-argument for a non-type, non-template template-parameter
7662 shall be one of: [...]
7663
7664 -- the name of a non-type template-parameter;
7665 -- the address of an object or function with external linkage, [...]
7666 expressed as "& id-expression" where the & is optional if the name
7667 refers to a function or array, or if the corresponding
7668 template-parameter is a reference.
7669
7670 Here, we do not care about functions, as they are invalid anyway
7671 for a parameter of type pointer-to-object. */
7672
7673 if (val_dep_p)
7674 /* Non-type template parameters are OK. */
7675 ;
7676 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7677 /* Null pointer values are OK in C++11. */;
7678 else if (TREE_CODE (expr) != ADDR_EXPR
7679 && !INDIRECT_TYPE_P (expr_type))
7680 /* Other values, like integer constants, might be valid
7681 non-type arguments of some other type. */
7682 return error_mark_node;
7683 else if (invalid_tparm_referent_p (type, expr, complain))
7684 return NULL_TREE;
7685
7686 expr = decayed;
7687
7688 expr = perform_qualification_conversions (type, expr);
7689 if (expr == error_mark_node)
7690 return error_mark_node;
7691 }
7692 /* [temp.arg.nontype]/5, bullet 3
7693
7694 For a non-type template-parameter of type reference to object, no
7695 conversions apply. The type referred to by the reference may be more
7696 cv-qualified than the (otherwise identical) type of the
7697 template-argument. The template-parameter is bound directly to the
7698 template-argument, which must be an lvalue. */
7699 else if (TYPE_REF_OBJ_P (type))
7700 {
7701 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7702 expr_type))
7703 return error_mark_node;
7704
7705 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7706 {
7707 if (complain & tf_error)
7708 error ("%qE is not a valid template argument for type %qT "
7709 "because of conflicts in cv-qualification", expr, type);
7710 return NULL_TREE;
7711 }
7712
7713 if (!lvalue_p (expr))
7714 {
7715 if (complain & tf_error)
7716 error ("%qE is not a valid template argument for type %qT "
7717 "because it is not an lvalue", expr, type);
7718 return NULL_TREE;
7719 }
7720
7721 /* [temp.arg.nontype]/1
7722
7723 A template-argument for a non-type, non-template template-parameter
7724 shall be one of: [...]
7725
7726 -- the address of an object or function with external linkage. */
7727 if (INDIRECT_REF_P (expr)
7728 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7729 {
7730 expr = TREE_OPERAND (expr, 0);
7731 if (DECL_P (expr))
7732 {
7733 if (complain & tf_error)
7734 error ("%q#D is not a valid template argument for type %qT "
7735 "because a reference variable does not have a constant "
7736 "address", expr, type);
7737 return NULL_TREE;
7738 }
7739 }
7740
7741 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7742 /* OK, dependent reference. We don't want to ask whether a DECL is
7743 itself value-dependent, since what we want here is its address. */;
7744 else
7745 {
7746 expr = build_address (expr);
7747
7748 if (invalid_tparm_referent_p (type, expr, complain))
7749 return NULL_TREE;
7750 }
7751
7752 if (!same_type_p (type, TREE_TYPE (expr)))
7753 expr = build_nop (type, expr);
7754 }
7755 /* [temp.arg.nontype]/5, bullet 4
7756
7757 For a non-type template-parameter of type pointer to function, only
7758 the function-to-pointer conversion (_conv.func_) is applied. If the
7759 template-argument represents a set of overloaded functions (or a
7760 pointer to such), the matching function is selected from the set
7761 (_over.over_). */
7762 else if (TYPE_PTRFN_P (type))
7763 {
7764 /* If the argument is a template-id, we might not have enough
7765 context information to decay the pointer. */
7766 if (!type_unknown_p (expr_type))
7767 {
7768 expr = decay_conversion (expr, complain);
7769 if (expr == error_mark_node)
7770 return error_mark_node;
7771 }
7772
7773 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7774 /* Null pointer values are OK in C++11. */
7775 return perform_qualification_conversions (type, expr);
7776
7777 expr = convert_nontype_argument_function (type, expr, complain);
7778 if (!expr || expr == error_mark_node)
7779 return expr;
7780 }
7781 /* [temp.arg.nontype]/5, bullet 5
7782
7783 For a non-type template-parameter of type reference to function, no
7784 conversions apply. If the template-argument represents a set of
7785 overloaded functions, the matching function is selected from the set
7786 (_over.over_). */
7787 else if (TYPE_REFFN_P (type))
7788 {
7789 if (TREE_CODE (expr) == ADDR_EXPR)
7790 {
7791 if (complain & tf_error)
7792 {
7793 error ("%qE is not a valid template argument for type %qT "
7794 "because it is a pointer", expr, type);
7795 inform (input_location, "try using %qE instead",
7796 TREE_OPERAND (expr, 0));
7797 }
7798 return NULL_TREE;
7799 }
7800
7801 expr = convert_nontype_argument_function (type, expr, complain);
7802 if (!expr || expr == error_mark_node)
7803 return expr;
7804 }
7805 /* [temp.arg.nontype]/5, bullet 6
7806
7807 For a non-type template-parameter of type pointer to member function,
7808 no conversions apply. If the template-argument represents a set of
7809 overloaded member functions, the matching member function is selected
7810 from the set (_over.over_). */
7811 else if (TYPE_PTRMEMFUNC_P (type))
7812 {
7813 expr = instantiate_type (type, expr, tf_none);
7814 if (expr == error_mark_node)
7815 return error_mark_node;
7816
7817 /* [temp.arg.nontype] bullet 1 says the pointer to member
7818 expression must be a pointer-to-member constant. */
7819 if (!val_dep_p
7820 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7821 return NULL_TREE;
7822
7823 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7824 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7825 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7826 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7827 }
7828 /* [temp.arg.nontype]/5, bullet 7
7829
7830 For a non-type template-parameter of type pointer to data member,
7831 qualification conversions (_conv.qual_) are applied. */
7832 else if (TYPE_PTRDATAMEM_P (type))
7833 {
7834 /* [temp.arg.nontype] bullet 1 says the pointer to member
7835 expression must be a pointer-to-member constant. */
7836 if (!val_dep_p
7837 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7838 return NULL_TREE;
7839
7840 expr = perform_qualification_conversions (type, expr);
7841 if (expr == error_mark_node)
7842 return expr;
7843 }
7844 else if (NULLPTR_TYPE_P (type))
7845 {
7846 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7847 {
7848 if (complain & tf_error)
7849 error ("%qE is not a valid template argument for type %qT "
7850 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7851 return NULL_TREE;
7852 }
7853 return expr;
7854 }
7855 else if (CLASS_TYPE_P (type))
7856 {
7857 /* Replace the argument with a reference to the corresponding template
7858 parameter object. */
7859 if (!val_dep_p)
7860 expr = create_template_parm_object (expr, complain);
7861 if (expr == error_mark_node)
7862 return NULL_TREE;
7863 }
7864 /* A template non-type parameter must be one of the above. */
7865 else
7866 gcc_unreachable ();
7867
7868 /* Sanity check: did we actually convert the argument to the
7869 right type? */
7870 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7871 (type, TREE_TYPE (expr)));
7872 return convert_from_reference (expr);
7873 }
7874
7875 /* Subroutine of coerce_template_template_parms, which returns 1 if
7876 PARM_PARM and ARG_PARM match using the rule for the template
7877 parameters of template template parameters. Both PARM and ARG are
7878 template parameters; the rest of the arguments are the same as for
7879 coerce_template_template_parms.
7880 */
7881 static int
7882 coerce_template_template_parm (tree parm,
7883 tree arg,
7884 tsubst_flags_t complain,
7885 tree in_decl,
7886 tree outer_args)
7887 {
7888 if (arg == NULL_TREE || error_operand_p (arg)
7889 || parm == NULL_TREE || error_operand_p (parm))
7890 return 0;
7891
7892 if (TREE_CODE (arg) != TREE_CODE (parm))
7893 return 0;
7894
7895 switch (TREE_CODE (parm))
7896 {
7897 case TEMPLATE_DECL:
7898 /* We encounter instantiations of templates like
7899 template <template <template <class> class> class TT>
7900 class C; */
7901 {
7902 if (!coerce_template_template_parms
7903 (parm, arg, complain, in_decl, outer_args))
7904 return 0;
7905 }
7906 /* Fall through. */
7907
7908 case TYPE_DECL:
7909 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7910 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7911 /* Argument is a parameter pack but parameter is not. */
7912 return 0;
7913 break;
7914
7915 case PARM_DECL:
7916 /* The tsubst call is used to handle cases such as
7917
7918 template <int> class C {};
7919 template <class T, template <T> class TT> class D {};
7920 D<int, C> d;
7921
7922 i.e. the parameter list of TT depends on earlier parameters. */
7923 if (!uses_template_parms (TREE_TYPE (arg)))
7924 {
7925 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7926 if (!uses_template_parms (t)
7927 && !same_type_p (t, TREE_TYPE (arg)))
7928 return 0;
7929 }
7930
7931 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7932 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7933 /* Argument is a parameter pack but parameter is not. */
7934 return 0;
7935
7936 break;
7937
7938 default:
7939 gcc_unreachable ();
7940 }
7941
7942 return 1;
7943 }
7944
7945 /* Coerce template argument list ARGLIST for use with template
7946 template-parameter TEMPL. */
7947
7948 static tree
7949 coerce_template_args_for_ttp (tree templ, tree arglist,
7950 tsubst_flags_t complain)
7951 {
7952 /* Consider an example where a template template parameter declared as
7953
7954 template <class T, class U = std::allocator<T> > class TT
7955
7956 The template parameter level of T and U are one level larger than
7957 of TT. To proper process the default argument of U, say when an
7958 instantiation `TT<int>' is seen, we need to build the full
7959 arguments containing {int} as the innermost level. Outer levels,
7960 available when not appearing as default template argument, can be
7961 obtained from the arguments of the enclosing template.
7962
7963 Suppose that TT is later substituted with std::vector. The above
7964 instantiation is `TT<int, std::allocator<T> >' with TT at
7965 level 1, and T at level 2, while the template arguments at level 1
7966 becomes {std::vector} and the inner level 2 is {int}. */
7967
7968 tree outer = DECL_CONTEXT (templ);
7969 if (outer)
7970 outer = generic_targs_for (outer);
7971 else if (current_template_parms)
7972 {
7973 /* This is an argument of the current template, so we haven't set
7974 DECL_CONTEXT yet. We can also get here when level-lowering a
7975 bound ttp. */
7976 tree relevant_template_parms;
7977
7978 /* Parameter levels that are greater than the level of the given
7979 template template parm are irrelevant. */
7980 relevant_template_parms = current_template_parms;
7981 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7982 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7983 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7984
7985 outer = template_parms_to_args (relevant_template_parms);
7986 }
7987
7988 if (outer)
7989 arglist = add_to_template_args (outer, arglist);
7990
7991 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7992 return coerce_template_parms (parmlist, arglist, templ, complain);
7993 }
7994
7995 /* A cache of template template parameters with match-all default
7996 arguments. */
7997 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7998
7999 /* T is a bound template template-parameter. Copy its arguments into default
8000 arguments of the template template-parameter's template parameters. */
8001
8002 static tree
8003 add_defaults_to_ttp (tree otmpl)
8004 {
8005 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
8006 return *c;
8007
8008 tree ntmpl = copy_node (otmpl);
8009
8010 tree ntype = copy_node (TREE_TYPE (otmpl));
8011 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
8012 TYPE_MAIN_VARIANT (ntype) = ntype;
8013 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
8014 TYPE_NAME (ntype) = ntmpl;
8015 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
8016
8017 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
8018 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
8019 TEMPLATE_PARM_DECL (idx) = ntmpl;
8020 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
8021
8022 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
8023 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
8024 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
8025 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
8026 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
8027 {
8028 tree o = TREE_VEC_ELT (vec, i);
8029 if (!template_parameter_pack_p (TREE_VALUE (o)))
8030 {
8031 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
8032 TREE_PURPOSE (n) = any_targ_node;
8033 }
8034 }
8035
8036 tree oresult = DECL_TEMPLATE_RESULT (otmpl);
8037 tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
8038 tree gen_ntmpl;
8039 if (gen_otmpl == otmpl)
8040 gen_ntmpl = ntmpl;
8041 else
8042 gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
8043
8044 tree nresult = copy_decl (oresult);
8045 DECL_TEMPLATE_INFO (nresult)
8046 = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
8047 DECL_TEMPLATE_RESULT (ntmpl) = nresult;
8048
8049 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
8050 return ntmpl;
8051 }
8052
8053 /* ARG is a bound potential template template-argument, and PARGS is a list
8054 of arguments for the corresponding template template-parameter. Adjust
8055 PARGS as appropriate for application to ARG's template, and if ARG is a
8056 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
8057 arguments to the template template parameter. */
8058
8059 static tree
8060 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
8061 {
8062 ++processing_template_decl;
8063 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
8064 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8065 {
8066 /* When comparing two template template-parameters in partial ordering,
8067 rewrite the one currently being used as an argument to have default
8068 arguments for all parameters. */
8069 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
8070 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
8071 if (pargs != error_mark_node)
8072 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
8073 TYPE_TI_ARGS (arg));
8074 }
8075 else
8076 {
8077 tree aparms
8078 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
8079 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
8080 }
8081 --processing_template_decl;
8082 return pargs;
8083 }
8084
8085 /* Subroutine of unify for the case when PARM is a
8086 BOUND_TEMPLATE_TEMPLATE_PARM. */
8087
8088 static int
8089 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
8090 bool explain_p)
8091 {
8092 tree parmvec = TYPE_TI_ARGS (parm);
8093 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
8094
8095 /* The template template parm might be variadic and the argument
8096 not, so flatten both argument lists. */
8097 parmvec = expand_template_argument_pack (parmvec);
8098 argvec = expand_template_argument_pack (argvec);
8099
8100 if (flag_new_ttp)
8101 {
8102 /* In keeping with P0522R0, adjust P's template arguments
8103 to apply to A's template; then flatten it again. */
8104 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
8105 nparmvec = expand_template_argument_pack (nparmvec);
8106
8107 if (unify (tparms, targs, nparmvec, argvec,
8108 UNIFY_ALLOW_NONE, explain_p))
8109 return 1;
8110
8111 /* If the P0522 adjustment eliminated a pack expansion, deduce
8112 empty packs. */
8113 if (flag_new_ttp
8114 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
8115 && unify_pack_expansion (tparms, targs, parmvec, argvec,
8116 DEDUCE_EXACT, /*sub*/true, explain_p))
8117 return 1;
8118 }
8119 else
8120 {
8121 /* Deduce arguments T, i from TT<T> or TT<i>.
8122 We check each element of PARMVEC and ARGVEC individually
8123 rather than the whole TREE_VEC since they can have
8124 different number of elements, which is allowed under N2555. */
8125
8126 int len = TREE_VEC_LENGTH (parmvec);
8127
8128 /* Check if the parameters end in a pack, making them
8129 variadic. */
8130 int parm_variadic_p = 0;
8131 if (len > 0
8132 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
8133 parm_variadic_p = 1;
8134
8135 for (int i = 0; i < len - parm_variadic_p; ++i)
8136 /* If the template argument list of P contains a pack
8137 expansion that is not the last template argument, the
8138 entire template argument list is a non-deduced
8139 context. */
8140 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
8141 return unify_success (explain_p);
8142
8143 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
8144 return unify_too_few_arguments (explain_p,
8145 TREE_VEC_LENGTH (argvec), len);
8146
8147 for (int i = 0; i < len - parm_variadic_p; ++i)
8148 if (unify (tparms, targs,
8149 TREE_VEC_ELT (parmvec, i),
8150 TREE_VEC_ELT (argvec, i),
8151 UNIFY_ALLOW_NONE, explain_p))
8152 return 1;
8153
8154 if (parm_variadic_p
8155 && unify_pack_expansion (tparms, targs,
8156 parmvec, argvec,
8157 DEDUCE_EXACT,
8158 /*subr=*/true, explain_p))
8159 return 1;
8160 }
8161
8162 return 0;
8163 }
8164
8165 /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
8166 template template parameters.
8167
8168 Consider the example:
8169 template <class T> class A;
8170 template<template <class U> class TT> class B;
8171
8172 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
8173 and OUTER_ARGS contains A. */
8174
8175 static int
8176 coerce_template_template_parms (tree parm_tmpl,
8177 tree arg_tmpl,
8178 tsubst_flags_t complain,
8179 tree in_decl,
8180 tree outer_args)
8181 {
8182 int nparms, nargs, i;
8183 tree parm, arg;
8184 int variadic_p = 0;
8185
8186 tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
8187 tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
8188 tree gen_arg_tmpl = most_general_template (arg_tmpl);
8189 tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
8190
8191 nparms = TREE_VEC_LENGTH (parm_parms);
8192 nargs = TREE_VEC_LENGTH (arg_parms);
8193
8194 if (flag_new_ttp)
8195 {
8196 /* P0522R0: A template template-parameter P is at least as specialized as
8197 a template template-argument A if, given the following rewrite to two
8198 function templates, the function template corresponding to P is at
8199 least as specialized as the function template corresponding to A
8200 according to the partial ordering rules for function templates
8201 ([temp.func.order]). Given an invented class template X with the
8202 template parameter list of A (including default arguments):
8203
8204 * Each of the two function templates has the same template parameters,
8205 respectively, as P or A.
8206
8207 * Each function template has a single function parameter whose type is
8208 a specialization of X with template arguments corresponding to the
8209 template parameters from the respective function template where, for
8210 each template parameter PP in the template parameter list of the
8211 function template, a corresponding template argument AA is formed. If
8212 PP declares a parameter pack, then AA is the pack expansion
8213 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8214
8215 If the rewrite produces an invalid type, then P is not at least as
8216 specialized as A. */
8217
8218 /* So coerce P's args to apply to A's parms, and then deduce between A's
8219 args and the converted args. If that succeeds, A is at least as
8220 specialized as P, so they match.*/
8221 processing_template_decl_sentinel ptds (/*reset*/false);
8222 ++processing_template_decl;
8223
8224 tree pargs = template_parms_level_to_args (parm_parms);
8225
8226 /* PARM and ARG might be at different template depths, and we want to
8227 pass the right additional levels of args when coercing PARGS to
8228 ARG_PARMS in case we need to do any substitution into non-type
8229 template parameter types.
8230
8231 OUTER_ARGS are not the right outer levels in this case, as they are
8232 the args we're building up for PARM, and for the coercion we want the
8233 args for ARG. If DECL_CONTEXT isn't set for a template template
8234 parameter, we can assume that it's in the current scope. */
8235 tree ctx = DECL_CONTEXT (arg_tmpl);
8236 if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8237 ctx = current_scope ();
8238 tree scope_args = NULL_TREE;
8239 if (tree tinfo = get_template_info (ctx))
8240 scope_args = TI_ARGS (tinfo);
8241 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
8242 {
8243 int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
8244 int scope_depth = TMPL_ARGS_DEPTH (scope_args);
8245 tree full_pargs = make_tree_vec (level + 1);
8246
8247 /* Only use as many levels from the scope as needed
8248 (excluding the level of ARG). */
8249 for (int i = 0; i < level - 1; ++i)
8250 if (i < scope_depth)
8251 TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
8252 else
8253 TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
8254
8255 /* Add the arguments that appear at the levels of ARG. */
8256 tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
8257 adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
8258 TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
8259
8260 TREE_VEC_ELT (full_pargs, level) = pargs;
8261 pargs = full_pargs;
8262 }
8263 else
8264 pargs = add_to_template_args (scope_args, pargs);
8265
8266 pargs = coerce_template_parms (gen_arg_parms, pargs,
8267 NULL_TREE, tf_none);
8268 if (pargs != error_mark_node)
8269 {
8270 tree targs = make_tree_vec (nargs);
8271 tree aargs = template_parms_level_to_args (arg_parms);
8272 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8273 /*explain*/false))
8274 return 1;
8275 }
8276 }
8277
8278 /* Determine whether we have a parameter pack at the end of the
8279 template template parameter's template parameter list. */
8280 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8281 {
8282 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8283
8284 if (error_operand_p (parm))
8285 return 0;
8286
8287 switch (TREE_CODE (parm))
8288 {
8289 case TEMPLATE_DECL:
8290 case TYPE_DECL:
8291 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8292 variadic_p = 1;
8293 break;
8294
8295 case PARM_DECL:
8296 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8297 variadic_p = 1;
8298 break;
8299
8300 default:
8301 gcc_unreachable ();
8302 }
8303 }
8304
8305 if (nargs != nparms
8306 && !(variadic_p && nargs >= nparms - 1))
8307 return 0;
8308
8309 /* Check all of the template parameters except the parameter pack at
8310 the end (if any). */
8311 for (i = 0; i < nparms - variadic_p; ++i)
8312 {
8313 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8314 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8315 continue;
8316
8317 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8318 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8319
8320 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8321 outer_args))
8322 return 0;
8323
8324 }
8325
8326 if (variadic_p)
8327 {
8328 /* Check each of the template parameters in the template
8329 argument against the template parameter pack at the end of
8330 the template template parameter. */
8331 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8332 return 0;
8333
8334 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8335
8336 for (; i < nargs; ++i)
8337 {
8338 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8339 continue;
8340
8341 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8342
8343 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8344 outer_args))
8345 return 0;
8346 }
8347 }
8348
8349 return 1;
8350 }
8351
8352 /* Verifies that the deduced template arguments (in TARGS) for the
8353 template template parameters (in TPARMS) represent valid bindings,
8354 by comparing the template parameter list of each template argument
8355 to the template parameter list of its corresponding template
8356 template parameter, in accordance with DR150. This
8357 routine can only be called after all template arguments have been
8358 deduced. It will return TRUE if all of the template template
8359 parameter bindings are okay, FALSE otherwise. */
8360 bool
8361 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8362 {
8363 int i, ntparms = TREE_VEC_LENGTH (tparms);
8364 bool ret = true;
8365
8366 /* We're dealing with template parms in this process. */
8367 ++processing_template_decl;
8368
8369 targs = INNERMOST_TEMPLATE_ARGS (targs);
8370
8371 for (i = 0; i < ntparms; ++i)
8372 {
8373 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8374 tree targ = TREE_VEC_ELT (targs, i);
8375
8376 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8377 {
8378 tree packed_args = NULL_TREE;
8379 int idx, len = 1;
8380
8381 if (ARGUMENT_PACK_P (targ))
8382 {
8383 /* Look inside the argument pack. */
8384 packed_args = ARGUMENT_PACK_ARGS (targ);
8385 len = TREE_VEC_LENGTH (packed_args);
8386 }
8387
8388 for (idx = 0; idx < len; ++idx)
8389 {
8390 if (packed_args)
8391 /* Extract the next argument from the argument
8392 pack. */
8393 targ = TREE_VEC_ELT (packed_args, idx);
8394
8395 if (PACK_EXPANSION_P (targ))
8396 /* Look at the pattern of the pack expansion. */
8397 targ = PACK_EXPANSION_PATTERN (targ);
8398
8399 /* Extract the template parameters from the template
8400 argument. */
8401 if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8402 targ = TYPE_NAME (targ);
8403
8404 /* Verify that we can coerce the template template
8405 parameters from the template argument to the template
8406 parameter. This requires an exact match. */
8407 if (TREE_CODE (targ) == TEMPLATE_DECL
8408 && !coerce_template_template_parms
8409 (tparm,
8410 targ,
8411 tf_none,
8412 tparm,
8413 targs))
8414 {
8415 ret = false;
8416 goto out;
8417 }
8418 }
8419 }
8420 }
8421
8422 out:
8423
8424 --processing_template_decl;
8425 return ret;
8426 }
8427
8428 /* Since type attributes aren't mangled, we need to strip them from
8429 template type arguments. */
8430
8431 tree
8432 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8433 {
8434 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8435 return arg;
8436 bool removed_attributes = false;
8437 tree canon = strip_typedefs (arg, &removed_attributes);
8438 if (removed_attributes
8439 && (complain & tf_warning))
8440 warning (OPT_Wignored_attributes,
8441 "ignoring attributes on template argument %qT", arg);
8442 return canon;
8443 }
8444
8445 /* And from inside dependent non-type arguments like sizeof(Type). */
8446
8447 static tree
8448 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8449 {
8450 if (!arg || arg == error_mark_node)
8451 return arg;
8452 bool removed_attributes = false;
8453 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8454 if (removed_attributes
8455 && (complain & tf_warning))
8456 warning (OPT_Wignored_attributes,
8457 "ignoring attributes in template argument %qE", arg);
8458 return canon;
8459 }
8460
8461 /* A template declaration can be substituted for a constrained
8462 template template parameter only when the argument is no more
8463 constrained than the parameter. */
8464
8465 static bool
8466 is_compatible_template_arg (tree parm, tree arg, tree args)
8467 {
8468 tree parm_cons = get_constraints (parm);
8469
8470 /* For now, allow constrained template template arguments
8471 and unconstrained template template parameters. */
8472 if (parm_cons == NULL_TREE)
8473 return true;
8474
8475 /* If the template parameter is constrained, we need to rewrite its
8476 constraints in terms of the ARG's template parameters. This ensures
8477 that all of the template parameter types will have the same depth.
8478
8479 Note that this is only valid when coerce_template_template_parm is
8480 true for the innermost template parameters of PARM and ARG. In other
8481 words, because coercion is successful, this conversion will be valid. */
8482 tree new_args = NULL_TREE;
8483 if (parm_cons)
8484 {
8485 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8486 new_args = template_parms_level_to_args (aparms);
8487 new_args = add_to_template_args (args, new_args);
8488 ++processing_template_decl;
8489 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8490 tf_none, NULL_TREE);
8491 --processing_template_decl;
8492 if (parm_cons == error_mark_node)
8493 return false;
8494 }
8495
8496 return weakly_subsumes (parm_cons, arg);
8497 }
8498
8499 // Convert a placeholder argument into a binding to the original
8500 // parameter. The original parameter is saved as the TREE_TYPE of
8501 // ARG.
8502 static inline tree
8503 convert_wildcard_argument (tree parm, tree arg)
8504 {
8505 TREE_TYPE (arg) = parm;
8506 return arg;
8507 }
8508
8509 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8510 because one of them is dependent. But we need to represent the
8511 conversion for the benefit of cp_tree_equal. */
8512
8513 static tree
8514 maybe_convert_nontype_argument (tree type, tree arg, bool force)
8515 {
8516 /* Auto parms get no conversion. */
8517 if (type_uses_auto (type))
8518 return arg;
8519 /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
8520 That would complicate other things, and it doesn't seem necessary. */
8521 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
8522 return arg;
8523 /* We don't need or want to add this conversion now if we're going to use the
8524 argument for deduction. */
8525 if (!value_dependent_expression_p (arg))
8526 force = false;
8527 else if (!force)
8528 return arg;
8529
8530 type = cv_unqualified (type);
8531 tree argtype = TREE_TYPE (arg);
8532 if (argtype && same_type_p (type, argtype))
8533 return arg;
8534
8535 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8536 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8537 IMPLICIT_CONV_EXPR_FORCED (arg) = force;
8538 return arg;
8539 }
8540
8541 /* Convert the indicated template ARG as necessary to match the
8542 indicated template PARM. Returns the converted ARG, or
8543 error_mark_node if the conversion was unsuccessful. Error and
8544 warning messages are issued under control of COMPLAIN. This
8545 conversion is for the Ith parameter in the parameter list. ARGS is
8546 the full set of template arguments deduced so far. */
8547
8548 static tree
8549 convert_template_argument (tree parm,
8550 tree arg,
8551 tree args,
8552 tsubst_flags_t complain,
8553 int i,
8554 tree in_decl)
8555 {
8556 tree orig_arg;
8557 tree val;
8558 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8559
8560 if (parm == error_mark_node || error_operand_p (arg))
8561 return error_mark_node;
8562
8563 /* Trivially convert placeholders. */
8564 if (TREE_CODE (arg) == WILDCARD_DECL)
8565 return convert_wildcard_argument (parm, arg);
8566
8567 if (arg == any_targ_node)
8568 return arg;
8569
8570 if (TREE_CODE (arg) == TREE_LIST
8571 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8572 {
8573 /* The template argument was the name of some
8574 member function. That's usually
8575 invalid, but static members are OK. In any
8576 case, grab the underlying fields/functions
8577 and issue an error later if required. */
8578 TREE_TYPE (arg) = unknown_type_node;
8579 }
8580
8581 orig_arg = arg;
8582
8583 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8584 requires_type = (TREE_CODE (parm) == TYPE_DECL
8585 || requires_tmpl_type);
8586
8587 /* When determining whether an argument pack expansion is a template,
8588 look at the pattern. */
8589 if (PACK_EXPANSION_P (arg))
8590 arg = PACK_EXPANSION_PATTERN (arg);
8591
8592 /* Deal with an injected-class-name used as a template template arg. */
8593 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8594 {
8595 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8596 if (TREE_CODE (t) == TEMPLATE_DECL)
8597 {
8598 if (cxx_dialect >= cxx11)
8599 /* OK under DR 1004. */;
8600 else if (complain & tf_warning_or_error)
8601 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8602 " used as template template argument", TYPE_NAME (arg));
8603 else if (flag_pedantic_errors)
8604 t = arg;
8605
8606 arg = t;
8607 }
8608 }
8609
8610 is_tmpl_type =
8611 ((TREE_CODE (arg) == TEMPLATE_DECL
8612 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8613 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8614 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8615 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8616
8617 if (is_tmpl_type
8618 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8619 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8620 arg = TYPE_STUB_DECL (arg);
8621
8622 is_type = TYPE_P (arg) || is_tmpl_type;
8623
8624 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8625 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8626 {
8627 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8628 {
8629 if (complain & tf_error)
8630 error ("invalid use of destructor %qE as a type", orig_arg);
8631 return error_mark_node;
8632 }
8633
8634 permerror (input_location,
8635 "to refer to a type member of a template parameter, "
8636 "use %<typename %E%>", orig_arg);
8637
8638 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8639 TREE_OPERAND (arg, 1),
8640 typename_type,
8641 complain);
8642 arg = orig_arg;
8643 is_type = 1;
8644 }
8645 if (is_type != requires_type)
8646 {
8647 if (in_decl)
8648 {
8649 if (complain & tf_error)
8650 {
8651 error ("type/value mismatch at argument %d in template "
8652 "parameter list for %qD",
8653 i + 1, in_decl);
8654 if (is_type)
8655 {
8656 /* The template argument is a type, but we're expecting
8657 an expression. */
8658 inform (input_location,
8659 " expected a constant of type %qT, got %qT",
8660 TREE_TYPE (parm),
8661 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8662 /* [temp.arg]/2: "In a template-argument, an ambiguity
8663 between a type-id and an expression is resolved to a
8664 type-id, regardless of the form of the corresponding
8665 template-parameter." So give the user a clue. */
8666 if (TREE_CODE (arg) == FUNCTION_TYPE)
8667 inform (input_location, " ambiguous template argument "
8668 "for non-type template parameter is treated as "
8669 "function type");
8670 }
8671 else if (requires_tmpl_type)
8672 inform (input_location,
8673 " expected a class template, got %qE", orig_arg);
8674 else
8675 inform (input_location,
8676 " expected a type, got %qE", orig_arg);
8677 }
8678 }
8679 return error_mark_node;
8680 }
8681 if (is_tmpl_type ^ requires_tmpl_type)
8682 {
8683 if (in_decl && (complain & tf_error))
8684 {
8685 error ("type/value mismatch at argument %d in template "
8686 "parameter list for %qD",
8687 i + 1, in_decl);
8688 if (is_tmpl_type)
8689 inform (input_location,
8690 " expected a type, got %qT", DECL_NAME (arg));
8691 else
8692 inform (input_location,
8693 " expected a class template, got %qT", orig_arg);
8694 }
8695 return error_mark_node;
8696 }
8697
8698 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8699 /* We already did the appropriate conversion when packing args. */
8700 val = orig_arg;
8701 else if (is_type)
8702 {
8703 if (requires_tmpl_type)
8704 {
8705 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8706 /* The number of argument required is not known yet.
8707 Just accept it for now. */
8708 val = orig_arg;
8709 else
8710 {
8711 /* Strip alias templates that are equivalent to another
8712 template. */
8713 arg = get_underlying_template (arg);
8714
8715 if (coerce_template_template_parms (parm, arg,
8716 complain, in_decl,
8717 args))
8718 {
8719 val = arg;
8720
8721 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8722 TEMPLATE_DECL. */
8723 if (val != error_mark_node)
8724 {
8725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8726 val = TREE_TYPE (val);
8727 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8728 val = make_pack_expansion (val, complain);
8729 }
8730 }
8731 else
8732 {
8733 if (in_decl && (complain & tf_error))
8734 {
8735 error ("type/value mismatch at argument %d in "
8736 "template parameter list for %qD",
8737 i + 1, in_decl);
8738 inform (input_location,
8739 " expected a template of type %qD, got %qT",
8740 parm, orig_arg);
8741 }
8742
8743 val = error_mark_node;
8744 }
8745
8746 // Check that the constraints are compatible before allowing the
8747 // substitution.
8748 if (val != error_mark_node)
8749 if (!is_compatible_template_arg (parm, arg, args))
8750 {
8751 if (in_decl && (complain & tf_error))
8752 {
8753 error ("constraint mismatch at argument %d in "
8754 "template parameter list for %qD",
8755 i + 1, in_decl);
8756 inform (input_location, " expected %qD but got %qD",
8757 parm, arg);
8758 }
8759 val = error_mark_node;
8760 }
8761 }
8762 }
8763 else
8764 val = orig_arg;
8765 /* We only form one instance of each template specialization.
8766 Therefore, if we use a non-canonical variant (i.e., a
8767 typedef), any future messages referring to the type will use
8768 the typedef, which is confusing if those future uses do not
8769 themselves also use the typedef. */
8770 if (TYPE_P (val))
8771 val = canonicalize_type_argument (val, complain);
8772 }
8773 else
8774 {
8775 tree t = TREE_TYPE (parm);
8776
8777 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8778 > TMPL_ARGS_DEPTH (args))
8779 /* We don't have enough levels of args to do any substitution. This
8780 can happen in the context of -fnew-ttp-matching. */;
8781 else if (tree a = type_uses_auto (t))
8782 {
8783 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8784 LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
8785 if (t == error_mark_node)
8786 return error_mark_node;
8787 }
8788 else
8789 t = tsubst (t, args, complain, in_decl);
8790
8791 /* Perform array-to-pointer and function-to-pointer conversion
8792 as per [temp.param]/10. */
8793 t = type_decays_to (t);
8794
8795 if (invalid_nontype_parm_type_p (t, complain))
8796 return error_mark_node;
8797
8798 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8799 this non-type template parameter, as per [temp.param]/6. */
8800 t = cv_unqualified (t);
8801
8802 if (t != TREE_TYPE (parm))
8803 t = canonicalize_type_argument (t, complain);
8804
8805 /* We need to handle arguments for alias or concept templates
8806 differently: we need to force building an IMPLICIT_CONV_EXPR, because
8807 these arguments are going to be substituted directly into the
8808 dependent type; they might not get another chance at
8809 convert_nontype_argument. But if the argument ends up here again for
8810 a template that isn't one of those, remove the conversion for
8811 consistency between naming the same dependent type directly or through
8812 an alias. */
8813 bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
8814 || concept_definition_p (in_decl));
8815 if (!force_conv
8816 && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
8817 && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
8818 && same_type_p (TREE_TYPE (orig_arg), t))
8819 orig_arg = TREE_OPERAND (orig_arg, 0);
8820
8821 if (!type_dependent_expression_p (orig_arg)
8822 && !uses_template_parms (t))
8823 /* We used to call digest_init here. However, digest_init
8824 will report errors, which we don't want when complain
8825 is zero. More importantly, digest_init will try too
8826 hard to convert things: for example, `0' should not be
8827 converted to pointer type at this point according to
8828 the standard. Accepting this is not merely an
8829 extension, since deciding whether or not these
8830 conversions can occur is part of determining which
8831 function template to call, or whether a given explicit
8832 argument specification is valid. */
8833 val = convert_nontype_argument (t, orig_arg, complain);
8834 else
8835 {
8836 val = canonicalize_expr_argument (orig_arg, complain);
8837 val = maybe_convert_nontype_argument (t, val, force_conv);
8838 }
8839
8840 if (val == NULL_TREE)
8841 val = error_mark_node;
8842 else if (val == error_mark_node && (complain & tf_error))
8843 error_at (cp_expr_loc_or_input_loc (orig_arg),
8844 "could not convert template argument %qE from %qT to %qT",
8845 orig_arg, TREE_TYPE (orig_arg), t);
8846
8847 if (INDIRECT_REF_P (val))
8848 {
8849 /* Reject template arguments that are references to built-in
8850 functions with no library fallbacks. */
8851 const_tree inner = TREE_OPERAND (val, 0);
8852 const_tree innertype = TREE_TYPE (inner);
8853 if (innertype
8854 && TYPE_REF_P (innertype)
8855 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8856 && TREE_OPERAND_LENGTH (inner) > 0
8857 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8858 return error_mark_node;
8859 }
8860
8861 if (TREE_CODE (val) == SCOPE_REF)
8862 {
8863 /* Strip typedefs from the SCOPE_REF. */
8864 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8865 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8866 complain);
8867 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8868 QUALIFIED_NAME_IS_TEMPLATE (val));
8869 }
8870 }
8871
8872 return val;
8873 }
8874
8875 /* Coerces the remaining template arguments in INNER_ARGS (from
8876 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8877 Returns the coerced argument pack. PARM_IDX is the position of this
8878 parameter in the template parameter list. ARGS is the original
8879 template argument list. */
8880 static tree
8881 coerce_template_parameter_pack (tree parms,
8882 int parm_idx,
8883 tree args,
8884 tree inner_args,
8885 int arg_idx,
8886 tree new_args,
8887 int* lost,
8888 tree in_decl,
8889 tsubst_flags_t complain)
8890 {
8891 tree parm = TREE_VEC_ELT (parms, parm_idx);
8892 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8893 tree packed_args;
8894 tree argument_pack;
8895 tree packed_parms = NULL_TREE;
8896
8897 if (arg_idx > nargs)
8898 arg_idx = nargs;
8899
8900 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8901 {
8902 /* When the template parameter is a non-type template parameter pack
8903 or template template parameter pack whose type or template
8904 parameters use parameter packs, we know exactly how many arguments
8905 we are looking for. Build a vector of the instantiated decls for
8906 these template parameters in PACKED_PARMS. */
8907 /* We can't use make_pack_expansion here because it would interpret a
8908 _DECL as a use rather than a declaration. */
8909 tree decl = TREE_VALUE (parm);
8910 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8911 PACK_EXPANSION_PATTERN (exp) = decl;
8912 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8913 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8914
8915 TREE_VEC_LENGTH (args)--;
8916 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8917 TREE_VEC_LENGTH (args)++;
8918
8919 if (packed_parms == error_mark_node)
8920 return error_mark_node;
8921
8922 /* If we're doing a partial instantiation of a member template,
8923 verify that all of the types used for the non-type
8924 template parameter pack are, in fact, valid for non-type
8925 template parameters. */
8926 if (arg_idx < nargs
8927 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8928 {
8929 int j, len = TREE_VEC_LENGTH (packed_parms);
8930 for (j = 0; j < len; ++j)
8931 {
8932 tree t = TREE_VEC_ELT (packed_parms, j);
8933 if (TREE_CODE (t) == PARM_DECL
8934 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8935 return error_mark_node;
8936 }
8937 /* We don't know how many args we have yet, just
8938 use the unconverted ones for now. */
8939 return NULL_TREE;
8940 }
8941
8942 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8943 }
8944 /* Check if we have a placeholder pack, which indicates we're
8945 in the context of a introduction list. In that case we want
8946 to match this pack to the single placeholder. */
8947 else if (arg_idx < nargs
8948 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8949 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8950 {
8951 nargs = arg_idx + 1;
8952 packed_args = make_tree_vec (1);
8953 }
8954 else
8955 packed_args = make_tree_vec (nargs - arg_idx);
8956
8957 /* Convert the remaining arguments, which will be a part of the
8958 parameter pack "parm". */
8959 int first_pack_arg = arg_idx;
8960 for (; arg_idx < nargs; ++arg_idx)
8961 {
8962 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8963 tree actual_parm = TREE_VALUE (parm);
8964 int pack_idx = arg_idx - first_pack_arg;
8965
8966 if (packed_parms)
8967 {
8968 /* Once we've packed as many args as we have types, stop. */
8969 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8970 break;
8971 else if (PACK_EXPANSION_P (arg))
8972 /* We don't know how many args we have yet, just
8973 use the unconverted ones for now. */
8974 return NULL_TREE;
8975 else
8976 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8977 }
8978
8979 if (arg == error_mark_node)
8980 {
8981 if (complain & tf_error)
8982 error ("template argument %d is invalid", arg_idx + 1);
8983 }
8984 else
8985 arg = convert_template_argument (actual_parm,
8986 arg, new_args, complain, parm_idx,
8987 in_decl);
8988 if (arg == error_mark_node)
8989 (*lost)++;
8990 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8991 }
8992
8993 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8994 && TREE_VEC_LENGTH (packed_args) > 0)
8995 {
8996 if (complain & tf_error)
8997 error ("wrong number of template arguments (%d, should be %d)",
8998 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8999 return error_mark_node;
9000 }
9001
9002 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
9003 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
9004 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
9005 else
9006 {
9007 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
9008 TREE_CONSTANT (argument_pack) = 1;
9009 }
9010
9011 ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
9012 if (CHECKING_P)
9013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
9014 TREE_VEC_LENGTH (packed_args));
9015 return argument_pack;
9016 }
9017
9018 /* Returns the number of pack expansions in the template argument vector
9019 ARGS. */
9020
9021 static int
9022 pack_expansion_args_count (tree args)
9023 {
9024 int i;
9025 int count = 0;
9026 if (args)
9027 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
9028 {
9029 tree elt = TREE_VEC_ELT (args, i);
9030 if (elt && PACK_EXPANSION_P (elt))
9031 ++count;
9032 }
9033 return count;
9034 }
9035
9036 /* Convert all template arguments to their appropriate types, and
9037 return a vector containing the innermost resulting template
9038 arguments. If any error occurs, return error_mark_node. Error and
9039 warning messages are issued under control of COMPLAIN.
9040
9041 If PARMS represents all template parameters levels, this function
9042 returns a vector of vectors representing all the resulting argument
9043 levels. Note that in this case, only the innermost arguments are
9044 coerced because the outermost ones are supposed to have been coerced
9045 already. Otherwise, if PARMS represents only (the innermost) vector
9046 of parameters, this function returns a vector containing just the
9047 innermost resulting arguments.
9048
9049 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
9050 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
9051 arguments not specified in ARGS must have default arguments which
9052 we'll use to fill in ARGS. */
9053
9054 tree
9055 coerce_template_parms (tree parms,
9056 tree args,
9057 tree in_decl,
9058 tsubst_flags_t complain,
9059 bool require_all_args /* = true */)
9060 {
9061 int nparms, nargs, parm_idx, arg_idx, lost = 0;
9062 tree orig_inner_args;
9063 tree inner_args;
9064
9065 /* When used as a boolean value, indicates whether this is a
9066 variadic template parameter list. Since it's an int, we can also
9067 subtract it from nparms to get the number of non-variadic
9068 parameters. */
9069 int variadic_p = 0;
9070 int variadic_args_p = 0;
9071 int post_variadic_parms = 0;
9072
9073 /* Adjustment to nparms for fixed parameter packs. */
9074 int fixed_pack_adjust = 0;
9075 int fixed_packs = 0;
9076 int missing = 0;
9077
9078 /* Likewise for parameters with default arguments. */
9079 int default_p = 0;
9080
9081 if (args == error_mark_node)
9082 return error_mark_node;
9083
9084 bool return_full_args = false;
9085 if (TREE_CODE (parms) == TREE_LIST)
9086 {
9087 if (TMPL_PARMS_DEPTH (parms) > 1)
9088 {
9089 gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
9090 return_full_args = true;
9091 }
9092 parms = INNERMOST_TEMPLATE_PARMS (parms);
9093 }
9094
9095 nparms = TREE_VEC_LENGTH (parms);
9096
9097 /* Determine if there are any parameter packs or default arguments. */
9098 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
9099 {
9100 tree parm = TREE_VEC_ELT (parms, parm_idx);
9101 if (variadic_p)
9102 ++post_variadic_parms;
9103 if (template_parameter_pack_p (TREE_VALUE (parm)))
9104 ++variadic_p;
9105 if (TREE_PURPOSE (parm))
9106 ++default_p;
9107 }
9108
9109 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
9110 /* If there are no parameters that follow a parameter pack, we need to
9111 expand any argument packs so that we can deduce a parameter pack from
9112 some non-packed args followed by an argument pack, as in variadic85.C.
9113 If there are such parameters, we need to leave argument packs intact
9114 so the arguments are assigned properly. This can happen when dealing
9115 with a nested class inside a partial specialization of a class
9116 template, as in variadic92.C, or when deducing a template parameter pack
9117 from a sub-declarator, as in variadic114.C. */
9118 if (!post_variadic_parms)
9119 inner_args = expand_template_argument_pack (inner_args);
9120
9121 /* Count any pack expansion args. */
9122 variadic_args_p = pack_expansion_args_count (inner_args);
9123
9124 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
9125 if ((nargs - variadic_args_p > nparms && !variadic_p)
9126 || (nargs < nparms - variadic_p
9127 && require_all_args
9128 && !variadic_args_p
9129 && (TREE_VEC_ELT (parms, nargs) != error_mark_node
9130 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
9131 {
9132 bad_nargs:
9133 if (complain & tf_error)
9134 {
9135 if (variadic_p || default_p)
9136 {
9137 nparms -= variadic_p + default_p;
9138 error ("wrong number of template arguments "
9139 "(%d, should be at least %d)", nargs, nparms);
9140 }
9141 else
9142 error ("wrong number of template arguments "
9143 "(%d, should be %d)", nargs, nparms);
9144
9145 if (in_decl)
9146 inform (DECL_SOURCE_LOCATION (in_decl),
9147 "provided for %qD", in_decl);
9148 }
9149
9150 return error_mark_node;
9151 }
9152 /* We can't pass a pack expansion to a non-pack parameter of an alias
9153 template (DR 1430). */
9154 else if (in_decl
9155 && (DECL_ALIAS_TEMPLATE_P (in_decl)
9156 || concept_definition_p (in_decl))
9157 && variadic_args_p
9158 && nargs - variadic_args_p < nparms - variadic_p)
9159 {
9160 if (complain & tf_error)
9161 {
9162 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
9163 {
9164 tree arg = TREE_VEC_ELT (inner_args, i);
9165 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
9166
9167 if (PACK_EXPANSION_P (arg)
9168 && !template_parameter_pack_p (parm))
9169 {
9170 if (DECL_ALIAS_TEMPLATE_P (in_decl))
9171 error_at (location_of (arg),
9172 "pack expansion argument for non-pack parameter "
9173 "%qD of alias template %qD", parm, in_decl);
9174 else
9175 error_at (location_of (arg),
9176 "pack expansion argument for non-pack parameter "
9177 "%qD of concept %qD", parm, in_decl);
9178 inform (DECL_SOURCE_LOCATION (parm), "declared here");
9179 goto found;
9180 }
9181 }
9182 gcc_unreachable ();
9183 found:;
9184 }
9185 return error_mark_node;
9186 }
9187
9188 /* We need to evaluate the template arguments, even though this
9189 template-id may be nested within a "sizeof". */
9190 cp_evaluated ev;
9191
9192 tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
9193 tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
9194 int pack_adjust = 0;
9195 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
9196 {
9197 tree arg;
9198 tree parm;
9199
9200 /* Get the Ith template parameter. */
9201 parm = TREE_VEC_ELT (parms, parm_idx);
9202
9203 if (parm == error_mark_node)
9204 {
9205 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
9206 continue;
9207 }
9208
9209 /* Calculate the next argument. */
9210 if (arg_idx < nargs)
9211 arg = TREE_VEC_ELT (inner_args, arg_idx);
9212 else
9213 arg = NULL_TREE;
9214
9215 if (template_parameter_pack_p (TREE_VALUE (parm))
9216 && (arg || require_all_args || !(complain & tf_partial))
9217 && !(arg && ARGUMENT_PACK_P (arg)))
9218 {
9219 /* Some arguments will be placed in the
9220 template parameter pack PARM. */
9221 arg = coerce_template_parameter_pack (parms, parm_idx, args,
9222 inner_args, arg_idx,
9223 new_args, &lost,
9224 in_decl, complain);
9225
9226 if (arg == NULL_TREE)
9227 {
9228 /* We don't know how many args we have yet, just use the
9229 unconverted (and still packed) ones for now. */
9230 new_inner_args = orig_inner_args;
9231 arg_idx = nargs;
9232 break;
9233 }
9234
9235 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
9236
9237 /* Store this argument. */
9238 if (arg == error_mark_node)
9239 {
9240 lost++;
9241 /* We are done with all of the arguments. */
9242 arg_idx = nargs;
9243 break;
9244 }
9245 else
9246 {
9247 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
9248 arg_idx += pack_adjust;
9249 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
9250 {
9251 ++fixed_packs;
9252 fixed_pack_adjust += pack_adjust;
9253 }
9254 }
9255
9256 continue;
9257 }
9258 else if (arg)
9259 {
9260 if (PACK_EXPANSION_P (arg))
9261 {
9262 /* "If every valid specialization of a variadic template
9263 requires an empty template parameter pack, the template is
9264 ill-formed, no diagnostic required." So check that the
9265 pattern works with this parameter. */
9266 tree pattern = PACK_EXPANSION_PATTERN (arg);
9267 tree conv = convert_template_argument (TREE_VALUE (parm),
9268 pattern, new_args,
9269 complain, parm_idx,
9270 in_decl);
9271 if (conv == error_mark_node)
9272 {
9273 if (complain & tf_error)
9274 inform (input_location, "so any instantiation with a "
9275 "non-empty parameter pack would be ill-formed");
9276 ++lost;
9277 }
9278 else if (TYPE_P (conv) && !TYPE_P (pattern))
9279 /* Recover from missing typename. */
9280 TREE_VEC_ELT (inner_args, arg_idx)
9281 = make_pack_expansion (conv, complain);
9282
9283 /* We don't know how many args we have yet, just
9284 use the unconverted ones for now. */
9285 new_inner_args = inner_args;
9286 arg_idx = nargs;
9287 break;
9288 }
9289 }
9290 else if (require_all_args)
9291 {
9292 /* There must be a default arg in this case. */
9293 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9294 complain, in_decl);
9295 /* The position of the first default template argument,
9296 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9297 Record that. */
9298 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9299 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9300 arg_idx - pack_adjust);
9301 }
9302 else
9303 break;
9304
9305 if (arg == error_mark_node)
9306 {
9307 if (complain & tf_error)
9308 error ("template argument %d is invalid", arg_idx + 1);
9309 }
9310 else if (!arg)
9311 {
9312 /* This can occur if there was an error in the template
9313 parameter list itself (which we would already have
9314 reported) that we are trying to recover from, e.g., a class
9315 template with a parameter list such as
9316 template<typename..., typename> (cpp0x/variadic150.C). */
9317 ++lost;
9318
9319 /* This can also happen with a fixed parameter pack (71834). */
9320 if (arg_idx >= nargs)
9321 ++missing;
9322 }
9323 else
9324 arg = convert_template_argument (TREE_VALUE (parm),
9325 arg, new_args, complain,
9326 parm_idx, in_decl);
9327
9328 if (arg == error_mark_node)
9329 lost++;
9330
9331 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9332 }
9333
9334 if (missing || arg_idx < nargs - variadic_args_p)
9335 {
9336 /* If we had fixed parameter packs, we didn't know how many arguments we
9337 actually needed earlier; now we do. */
9338 nparms += fixed_pack_adjust;
9339 variadic_p -= fixed_packs;
9340 goto bad_nargs;
9341 }
9342
9343 if (arg_idx < nargs)
9344 {
9345 /* We had some pack expansion arguments that will only work if the packs
9346 are empty, but wait until instantiation time to complain.
9347 See variadic-ttp3.C. */
9348
9349 /* Except that we can't provide empty packs to alias templates or
9350 concepts when there are no corresponding parameters. Basically,
9351 we can get here with this:
9352
9353 template<typename T> concept C = true;
9354
9355 template<typename... Args>
9356 requires C<Args...>
9357 void f();
9358
9359 When parsing C<Args...>, we try to form a concept check of
9360 C<?, Args...>. Without the extra check for substituting an empty
9361 pack past the last parameter, we can accept the check as valid.
9362
9363 FIXME: This may be valid for alias templates (but I doubt it).
9364
9365 FIXME: The error could be better also. */
9366 if (in_decl && concept_definition_p (in_decl))
9367 {
9368 if (complain & tf_error)
9369 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9370 "too many arguments");
9371 return error_mark_node;
9372 }
9373
9374 int len = nparms + (nargs - arg_idx);
9375 tree args = make_tree_vec (len);
9376 int i = 0;
9377 for (; i < nparms; ++i)
9378 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9379 for (; i < len; ++i, ++arg_idx)
9380 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9381 arg_idx - pack_adjust);
9382 new_inner_args = args;
9383 }
9384
9385 if (lost)
9386 {
9387 gcc_assert (!(complain & tf_error) || seen_error ());
9388 return error_mark_node;
9389 }
9390
9391 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9392 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9393 TREE_VEC_LENGTH (new_inner_args));
9394
9395 return return_full_args ? new_args : new_inner_args;
9396 }
9397
9398 /* Returns true if T is a wrapper to make a C++20 template parameter
9399 object const. */
9400
9401 static bool
9402 class_nttp_const_wrapper_p (tree t)
9403 {
9404 if (cxx_dialect < cxx20)
9405 return false;
9406 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9407 && CP_TYPE_CONST_P (TREE_TYPE (t))
9408 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9409 }
9410
9411 /* Returns 1 if template args OT and NT are equivalent. */
9412
9413 int
9414 template_args_equal (tree ot, tree nt)
9415 {
9416 if (nt == ot)
9417 return 1;
9418 if (nt == NULL_TREE || ot == NULL_TREE)
9419 return false;
9420 if (nt == any_targ_node || ot == any_targ_node)
9421 return true;
9422
9423 if (class_nttp_const_wrapper_p (nt))
9424 nt = TREE_OPERAND (nt, 0);
9425 if (class_nttp_const_wrapper_p (ot))
9426 ot = TREE_OPERAND (ot, 0);
9427
9428 /* DR 1558: Don't treat an alias template specialization with dependent
9429 arguments as equivalent to its underlying type when used as a template
9430 argument; we need them to be distinct so that we substitute into the
9431 specialization arguments at instantiation time. And aliases can't be
9432 equivalent without being ==, so we don't need to look any deeper.
9433
9434 During partial ordering, however, we need to treat them normally so we can
9435 order uses of the same alias with different cv-qualification (79960). */
9436 auto cso = make_temp_override (comparing_dependent_aliases);
9437 if (!comparing_for_partial_ordering)
9438 ++comparing_dependent_aliases;
9439
9440 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9441 /* For member templates */
9442 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9443 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9444 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9445 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9446 PACK_EXPANSION_PATTERN (nt))
9447 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9448 PACK_EXPANSION_EXTRA_ARGS (nt)));
9449 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9450 return cp_tree_equal (ot, nt);
9451 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9452 gcc_unreachable ();
9453 else if (TYPE_P (nt) || TYPE_P (ot))
9454 {
9455 if (!(TYPE_P (nt) && TYPE_P (ot)))
9456 return false;
9457 return same_type_p (ot, nt);
9458 }
9459 else
9460 {
9461 /* Try to treat a template non-type argument that has been converted
9462 to the parameter type as equivalent to one that hasn't yet. */
9463 for (enum tree_code code1 = TREE_CODE (ot);
9464 CONVERT_EXPR_CODE_P (code1)
9465 || code1 == NON_LVALUE_EXPR;
9466 code1 = TREE_CODE (ot))
9467 ot = TREE_OPERAND (ot, 0);
9468
9469 for (enum tree_code code2 = TREE_CODE (nt);
9470 CONVERT_EXPR_CODE_P (code2)
9471 || code2 == NON_LVALUE_EXPR;
9472 code2 = TREE_CODE (nt))
9473 nt = TREE_OPERAND (nt, 0);
9474
9475 return cp_tree_equal (ot, nt);
9476 }
9477 }
9478
9479 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9480 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9481 NEWARG_PTR with the offending arguments if they are non-NULL. */
9482
9483 bool
9484 comp_template_args (tree oldargs, tree newargs,
9485 tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
9486 {
9487 if (oldargs == newargs)
9488 return true;
9489
9490 if (!oldargs || !newargs)
9491 return false;
9492
9493 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9494 return false;
9495
9496 for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9497 {
9498 tree nt = TREE_VEC_ELT (newargs, i);
9499 tree ot = TREE_VEC_ELT (oldargs, i);
9500
9501 if (! template_args_equal (ot, nt))
9502 {
9503 if (oldarg_ptr != NULL)
9504 *oldarg_ptr = ot;
9505 if (newarg_ptr != NULL)
9506 *newarg_ptr = nt;
9507 return false;
9508 }
9509 }
9510 return true;
9511 }
9512
9513 static bool
9514 comp_template_args_porder (tree oargs, tree nargs)
9515 {
9516 ++comparing_for_partial_ordering;
9517 bool equal = comp_template_args (oargs, nargs);
9518 --comparing_for_partial_ordering;
9519 return equal;
9520 }
9521
9522 /* Implement a freelist interface for objects of type T.
9523
9524 Head is a separate object, rather than a regular member, so that we
9525 can define it as a GTY deletable pointer, which is highly
9526 desirable. A data member could be declared that way, but then the
9527 containing object would implicitly get GTY((user)), which would
9528 prevent us from instantiating freelists as global objects.
9529 Although this way we can create freelist global objects, they're
9530 such thin wrappers that instantiating temporaries at every use
9531 loses nothing and saves permanent storage for the freelist object.
9532
9533 Member functions next, anew, poison and reinit have default
9534 implementations that work for most of the types we're interested
9535 in, but if they don't work for some type, they should be explicitly
9536 specialized. See the comments before them for requirements, and
9537 the example specializations for the tree_list_freelist. */
9538 template <typename T>
9539 class freelist
9540 {
9541 /* Return the next object in a chain. We could just do type
9542 punning, but if we access the object with its underlying type, we
9543 avoid strict-aliasing trouble. This needs only work between
9544 poison and reinit. */
9545 static T *&next (T *obj) { return obj->next; }
9546
9547 /* Return a newly allocated, uninitialized or minimally-initialized
9548 object of type T. Any initialization performed by anew should
9549 either remain across the life of the object and the execution of
9550 poison, or be redone by reinit. */
9551 static T *anew () { return ggc_alloc<T> (); }
9552
9553 /* Optionally scribble all over the bits holding the object, so that
9554 they become (mostly?) uninitialized memory. This is called while
9555 preparing to make the object part of the free list. */
9556 static void poison (T *obj) {
9557 T *p ATTRIBUTE_UNUSED = obj;
9558 T **q ATTRIBUTE_UNUSED = &next (obj);
9559
9560 #ifdef ENABLE_GC_CHECKING
9561 /* Poison the data, to indicate the data is garbage. */
9562 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9563 memset (p, 0xa5, sizeof (*p));
9564 #endif
9565 /* Let valgrind know the object is free. */
9566 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9567
9568 /* Let valgrind know the next portion of the object is available,
9569 but uninitialized. */
9570 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9571 }
9572
9573 /* Bring an object that underwent at least one lifecycle after anew
9574 and before the most recent free and poison, back to a usable
9575 state, reinitializing whatever is needed for it to be
9576 functionally equivalent to an object just allocated and returned
9577 by anew. This may poison or clear the next field, used by
9578 freelist housekeeping after poison was called. */
9579 static void reinit (T *obj) {
9580 T **q ATTRIBUTE_UNUSED = &next (obj);
9581
9582 #ifdef ENABLE_GC_CHECKING
9583 memset (q, 0xa5, sizeof (*q));
9584 #endif
9585 /* Let valgrind know the entire object is available, but
9586 uninitialized. */
9587 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9588 }
9589
9590 /* Reference a GTY-deletable pointer that points to the first object
9591 in the free list proper. */
9592 T *&head;
9593 public:
9594 /* Construct a freelist object chaining objects off of HEAD. */
9595 freelist (T *&head) : head(head) {}
9596
9597 /* Add OBJ to the free object list. The former head becomes OBJ's
9598 successor. */
9599 void free (T *obj)
9600 {
9601 poison (obj);
9602 next (obj) = head;
9603 head = obj;
9604 }
9605
9606 /* Take an object from the free list, if one is available, or
9607 allocate a new one. Objects taken from the free list should be
9608 regarded as filled with garbage, except for bits that are
9609 configured to be preserved across free and alloc. */
9610 T *alloc ()
9611 {
9612 if (head)
9613 {
9614 T *obj = head;
9615 head = next (head);
9616 reinit (obj);
9617 return obj;
9618 }
9619 else
9620 return anew ();
9621 }
9622 };
9623
9624 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9625 want to allocate a TREE_LIST using the usual interface, and ensure
9626 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9627 build_tree_list logic in reinit, so this could go out of sync. */
9628 template <>
9629 inline tree &
9630 freelist<tree_node>::next (tree obj)
9631 {
9632 return TREE_CHAIN (obj);
9633 }
9634 template <>
9635 inline tree
9636 freelist<tree_node>::anew ()
9637 {
9638 return build_tree_list (NULL, NULL);
9639 }
9640 template <>
9641 inline void
9642 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9643 {
9644 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9645 tree p ATTRIBUTE_UNUSED = obj;
9646 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9647 tree *q ATTRIBUTE_UNUSED = &next (obj);
9648
9649 #ifdef ENABLE_GC_CHECKING
9650 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9651
9652 /* Poison the data, to indicate the data is garbage. */
9653 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9654 memset (p, 0xa5, size);
9655 #endif
9656 /* Let valgrind know the object is free. */
9657 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9658 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9659 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9660 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9661
9662 #ifdef ENABLE_GC_CHECKING
9663 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9664 /* Keep TREE_CHAIN functional. */
9665 TREE_SET_CODE (obj, TREE_LIST);
9666 #else
9667 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9668 #endif
9669 }
9670 template <>
9671 inline void
9672 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9673 {
9674 tree_common *c ATTRIBUTE_UNUSED = &obj->common;
9675
9676 #ifdef ENABLE_GC_CHECKING
9677 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9678 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9679 memset (obj, 0, sizeof (tree_list));
9680 #endif
9681
9682 /* Let valgrind know the entire object is available, but
9683 uninitialized. */
9684 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9685
9686 #ifdef ENABLE_GC_CHECKING
9687 TREE_SET_CODE (obj, TREE_LIST);
9688 #else
9689 TREE_CHAIN (obj) = NULL_TREE;
9690 TREE_TYPE (obj) = NULL_TREE;
9691 #endif
9692 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
9693 }
9694
9695 /* Point to the first object in the TREE_LIST freelist. */
9696 static GTY((deletable)) tree tree_list_freelist_head;
9697 /* Return the/an actual TREE_LIST freelist. */
9698 static inline freelist<tree_node>
9699 tree_list_freelist ()
9700 {
9701 return tree_list_freelist_head;
9702 }
9703
9704 /* Point to the first object in the tinst_level freelist. */
9705 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9706 /* Return the/an actual tinst_level freelist. */
9707 static inline freelist<tinst_level>
9708 tinst_level_freelist ()
9709 {
9710 return tinst_level_freelist_head;
9711 }
9712
9713 /* Point to the first object in the pending_template freelist. */
9714 static GTY((deletable)) pending_template *pending_template_freelist_head;
9715 /* Return the/an actual pending_template freelist. */
9716 static inline freelist<pending_template>
9717 pending_template_freelist ()
9718 {
9719 return pending_template_freelist_head;
9720 }
9721
9722 /* Build the TREE_LIST object out of a split list, store it
9723 permanently, and return it. */
9724 tree
9725 tinst_level::to_list ()
9726 {
9727 gcc_assert (split_list_p ());
9728 tree ret = tree_list_freelist ().alloc ();
9729 TREE_PURPOSE (ret) = tldcl;
9730 TREE_VALUE (ret) = targs;
9731 tldcl = ret;
9732 targs = NULL;
9733 gcc_assert (tree_list_p ());
9734 return ret;
9735 }
9736
9737 const unsigned short tinst_level::refcount_infinity;
9738
9739 /* Increment OBJ's refcount unless it is already infinite. */
9740 static tinst_level *
9741 inc_refcount_use (tinst_level *obj)
9742 {
9743 if (obj && obj->refcount != tinst_level::refcount_infinity)
9744 ++obj->refcount;
9745 return obj;
9746 }
9747
9748 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9749 void
9750 tinst_level::free (tinst_level *obj)
9751 {
9752 if (obj->tree_list_p ())
9753 tree_list_freelist ().free (obj->get_node ());
9754 tinst_level_freelist ().free (obj);
9755 }
9756
9757 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9758 OBJ's DECL and OBJ, and start over with the tinst_level object that
9759 used to be referenced by OBJ's NEXT. */
9760 static void
9761 dec_refcount_use (tinst_level *obj)
9762 {
9763 while (obj
9764 && obj->refcount != tinst_level::refcount_infinity
9765 && !--obj->refcount)
9766 {
9767 tinst_level *next = obj->next;
9768 tinst_level::free (obj);
9769 obj = next;
9770 }
9771 }
9772
9773 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9774 and of the former PTR. Omitting the second argument is equivalent
9775 to passing (T*)NULL; this is allowed because passing the
9776 zero-valued integral constant NULL confuses type deduction and/or
9777 overload resolution. */
9778 template <typename T>
9779 static void
9780 set_refcount_ptr (T *& ptr, T *obj = NULL)
9781 {
9782 T *save = ptr;
9783 ptr = inc_refcount_use (obj);
9784 dec_refcount_use (save);
9785 }
9786
9787 static void
9788 add_pending_template (tree d)
9789 {
9790 tree ti = (TYPE_P (d)
9791 ? CLASSTYPE_TEMPLATE_INFO (d)
9792 : DECL_TEMPLATE_INFO (d));
9793 struct pending_template *pt;
9794 int level;
9795
9796 if (TI_PENDING_TEMPLATE_FLAG (ti))
9797 return;
9798
9799 /* We are called both from instantiate_decl, where we've already had a
9800 tinst_level pushed, and instantiate_template, where we haven't.
9801 Compensate. */
9802 gcc_assert (TREE_CODE (d) != TREE_LIST);
9803 level = !current_tinst_level
9804 || current_tinst_level->maybe_get_node () != d;
9805
9806 if (level)
9807 push_tinst_level (d);
9808
9809 pt = pending_template_freelist ().alloc ();
9810 pt->next = NULL;
9811 pt->tinst = NULL;
9812 set_refcount_ptr (pt->tinst, current_tinst_level);
9813 if (last_pending_template)
9814 last_pending_template->next = pt;
9815 else
9816 pending_templates = pt;
9817
9818 last_pending_template = pt;
9819
9820 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9821
9822 if (level)
9823 pop_tinst_level ();
9824 }
9825
9826
9827 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9828 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9829 documentation for TEMPLATE_ID_EXPR. */
9830
9831 tree
9832 lookup_template_function (tree fns, tree arglist)
9833 {
9834 if (fns == error_mark_node || arglist == error_mark_node)
9835 return error_mark_node;
9836
9837 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9838
9839 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9840 {
9841 error ("%q#D is not a function template", fns);
9842 return error_mark_node;
9843 }
9844
9845 if (BASELINK_P (fns))
9846 {
9847 fns = copy_node (fns);
9848 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9849 unknown_type_node,
9850 BASELINK_FUNCTIONS (fns),
9851 arglist);
9852 return fns;
9853 }
9854
9855 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9856 }
9857
9858 /* Within the scope of a template class S<T>, the name S gets bound
9859 (in build_self_reference) to a TYPE_DECL for the class, not a
9860 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9861 or one of its enclosing classes, and that type is a template,
9862 return the associated TEMPLATE_DECL. Otherwise, the original
9863 DECL is returned.
9864
9865 Also handle the case when DECL is a TREE_LIST of ambiguous
9866 injected-class-names from different bases. */
9867
9868 tree
9869 maybe_get_template_decl_from_type_decl (tree decl)
9870 {
9871 if (decl == NULL_TREE)
9872 return decl;
9873
9874 /* DR 176: A lookup that finds an injected-class-name (10.2
9875 [class.member.lookup]) can result in an ambiguity in certain cases
9876 (for example, if it is found in more than one base class). If all of
9877 the injected-class-names that are found refer to specializations of
9878 the same class template, and if the name is followed by a
9879 template-argument-list, the reference refers to the class template
9880 itself and not a specialization thereof, and is not ambiguous. */
9881 if (TREE_CODE (decl) == TREE_LIST)
9882 {
9883 tree t, tmpl = NULL_TREE;
9884 for (t = decl; t; t = TREE_CHAIN (t))
9885 {
9886 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9887 if (!tmpl)
9888 tmpl = elt;
9889 else if (tmpl != elt)
9890 break;
9891 }
9892 if (tmpl && t == NULL_TREE)
9893 return tmpl;
9894 else
9895 return decl;
9896 }
9897
9898 return (decl != NULL_TREE
9899 && DECL_SELF_REFERENCE_P (decl)
9900 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9901 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9902 }
9903
9904 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9905 parameters, find the desired type.
9906
9907 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9908
9909 IN_DECL, if non-NULL, is the template declaration we are trying to
9910 instantiate.
9911
9912 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9913 the class we are looking up.
9914
9915 Issue error and warning messages under control of COMPLAIN.
9916
9917 If the template class is really a local class in a template
9918 function, then the FUNCTION_CONTEXT is the function in which it is
9919 being instantiated.
9920
9921 ??? Note that this function is currently called *twice* for each
9922 template-id: the first time from the parser, while creating the
9923 incomplete type (finish_template_type), and the second type during the
9924 real instantiation (instantiate_template_class). This is surely something
9925 that we want to avoid. It also causes some problems with argument
9926 coercion (see convert_nontype_argument for more information on this). */
9927
9928 tree
9929 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9930 int entering_scope, tsubst_flags_t complain)
9931 {
9932 auto_timevar tv (TV_TEMPLATE_INST);
9933
9934 tree templ = NULL_TREE, parmlist;
9935 tree t;
9936 spec_entry **slot;
9937 spec_entry *entry;
9938 spec_entry elt;
9939 hashval_t hash;
9940
9941 if (identifier_p (d1))
9942 {
9943 tree value = innermost_non_namespace_value (d1);
9944 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9945 templ = value;
9946 else
9947 {
9948 if (context)
9949 push_decl_namespace (context);
9950 templ = lookup_name (d1);
9951 templ = maybe_get_template_decl_from_type_decl (templ);
9952 if (context)
9953 pop_decl_namespace ();
9954 }
9955 }
9956 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9957 {
9958 tree type = TREE_TYPE (d1);
9959
9960 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9961 an implicit typename for the second A. Deal with it. */
9962 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9963 type = TREE_TYPE (type);
9964
9965 if (CLASSTYPE_TEMPLATE_INFO (type))
9966 {
9967 templ = CLASSTYPE_TI_TEMPLATE (type);
9968 d1 = DECL_NAME (templ);
9969 }
9970 }
9971 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9972 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9973 {
9974 templ = TYPE_TI_TEMPLATE (d1);
9975 d1 = DECL_NAME (templ);
9976 }
9977 else if (DECL_TYPE_TEMPLATE_P (d1))
9978 {
9979 templ = d1;
9980 d1 = DECL_NAME (templ);
9981 }
9982 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9983 {
9984 templ = d1;
9985 d1 = DECL_NAME (templ);
9986 }
9987
9988 /* Issue an error message if we didn't find a template. */
9989 if (! templ)
9990 {
9991 if (complain & tf_error)
9992 error ("%qT is not a template", d1);
9993 return error_mark_node;
9994 }
9995
9996 if (TREE_CODE (templ) != TEMPLATE_DECL
9997 /* Make sure it's a user visible template, if it was named by
9998 the user. */
9999 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
10000 && !PRIMARY_TEMPLATE_P (templ)))
10001 {
10002 if (complain & tf_error)
10003 {
10004 error ("non-template type %qT used as a template", d1);
10005 if (in_decl)
10006 error ("for template declaration %q+D", in_decl);
10007 }
10008 return error_mark_node;
10009 }
10010
10011 complain &= ~tf_user;
10012
10013 /* An alias that just changes the name of a template is equivalent to the
10014 other template, so if any of the arguments are pack expansions, strip
10015 the alias to avoid problems with a pack expansion passed to a non-pack
10016 alias template parameter (DR 1430). */
10017 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
10018 templ = get_underlying_template (templ);
10019
10020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10021 {
10022 tree parm;
10023 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
10024 if (arglist2 == error_mark_node
10025 || (!uses_template_parms (arglist2)
10026 && check_instantiated_args (templ, arglist2, complain)))
10027 return error_mark_node;
10028
10029 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
10030 return parm;
10031 }
10032 else
10033 {
10034 tree template_type = TREE_TYPE (templ);
10035 tree gen_tmpl;
10036 tree type_decl;
10037 tree found = NULL_TREE;
10038 int arg_depth;
10039 int parm_depth;
10040 int is_dependent_type;
10041 int use_partial_inst_tmpl = false;
10042
10043 if (template_type == error_mark_node)
10044 /* An error occurred while building the template TEMPL, and a
10045 diagnostic has most certainly been emitted for that
10046 already. Let's propagate that error. */
10047 return error_mark_node;
10048
10049 gen_tmpl = most_general_template (templ);
10050 if (modules_p ())
10051 lazy_load_pendings (gen_tmpl);
10052
10053 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
10054 parm_depth = TMPL_PARMS_DEPTH (parmlist);
10055 arg_depth = TMPL_ARGS_DEPTH (arglist);
10056
10057 if (arg_depth == 1 && parm_depth > 1)
10058 {
10059 /* We've been given an incomplete set of template arguments.
10060 For example, given:
10061
10062 template <class T> struct S1 {
10063 template <class U> struct S2 {};
10064 template <class U> struct S2<U*> {};
10065 };
10066
10067 we will be called with an ARGLIST of `U*', but the
10068 TEMPLATE will be `template <class T> template
10069 <class U> struct S1<T>::S2'. We must fill in the missing
10070 arguments. */
10071 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
10072 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
10073 arg_depth = TMPL_ARGS_DEPTH (arglist);
10074 }
10075
10076 /* Now we should have enough arguments. */
10077 gcc_assert (parm_depth == arg_depth);
10078
10079 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10080 {
10081 /* The user referred to a specialization of an alias
10082 template represented by GEN_TMPL.
10083
10084 [temp.alias]/2 says:
10085
10086 When a template-id refers to the specialization of an
10087 alias template, it is equivalent to the associated
10088 type obtained by substitution of its
10089 template-arguments for the template-parameters in the
10090 type-id of the alias template. */
10091
10092 t = instantiate_alias_template (gen_tmpl, arglist, complain);
10093 /* Note that the call above (by indirectly calling
10094 register_specialization in tsubst_decl) registers the
10095 TYPE_DECL representing the specialization of the alias
10096 template. So next time someone substitutes ARGLIST for
10097 the template parms into the alias template (GEN_TMPL),
10098 she'll get that TYPE_DECL back. */
10099
10100 if (t == error_mark_node)
10101 return error_mark_node;
10102 return TREE_TYPE (t);
10103 }
10104
10105 /* From here on, we're only interested in the most general
10106 template. */
10107
10108 /* Shortcut looking up the current class scope again. */
10109 for (tree cur = current_nonlambda_class_type ();
10110 cur != NULL_TREE;
10111 cur = get_containing_scope (cur))
10112 {
10113 if (!CLASS_TYPE_P (cur))
10114 continue;
10115
10116 tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
10117 if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
10118 break;
10119
10120 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
10121 && comp_template_args (arglist, TI_ARGS (ti)))
10122 return cur;
10123 }
10124
10125 /* Calculate the BOUND_ARGS. These will be the args that are
10126 actually tsubst'd into the definition to create the
10127 instantiation. */
10128 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10129 arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
10130
10131 if (arglist == error_mark_node)
10132 /* We were unable to bind the arguments. */
10133 return error_mark_node;
10134
10135 /* In the scope of a template class, explicit references to the
10136 template class refer to the type of the template, not any
10137 instantiation of it. For example, in:
10138
10139 template <class T> class C { void f(C<T>); }
10140
10141 the `C<T>' is just the same as `C'. Outside of the
10142 class, however, such a reference is an instantiation. */
10143 if (entering_scope
10144 || !PRIMARY_TEMPLATE_P (gen_tmpl)
10145 || currently_open_class (template_type))
10146 {
10147 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
10148
10149 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
10150 return template_type;
10151 }
10152
10153 /* If we already have this specialization, return it. */
10154 elt.tmpl = gen_tmpl;
10155 elt.args = arglist;
10156 elt.spec = NULL_TREE;
10157 hash = spec_hasher::hash (&elt);
10158 entry = type_specializations->find_with_hash (&elt, hash);
10159
10160 if (entry)
10161 return entry->spec;
10162
10163 /* If the template's constraints are not satisfied,
10164 then we cannot form a valid type.
10165
10166 Note that the check is deferred until after the hash
10167 lookup. This prevents redundant checks on previously
10168 instantiated specializations. */
10169 if (flag_concepts
10170 && !constraints_satisfied_p (gen_tmpl, arglist))
10171 {
10172 if (complain & tf_error)
10173 {
10174 auto_diagnostic_group d;
10175 error ("template constraint failure for %qD", gen_tmpl);
10176 diagnose_constraints (input_location, gen_tmpl, arglist);
10177 }
10178 return error_mark_node;
10179 }
10180
10181 is_dependent_type = uses_template_parms (arglist);
10182
10183 /* If the deduced arguments are invalid, then the binding
10184 failed. */
10185 if (!is_dependent_type
10186 && check_instantiated_args (gen_tmpl,
10187 INNERMOST_TEMPLATE_ARGS (arglist),
10188 complain))
10189 return error_mark_node;
10190
10191 if (!is_dependent_type
10192 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10193 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
10194 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
10195 /* This occurs when the user has tried to define a tagged type
10196 in a scope that forbids it. We emitted an error during the
10197 parse. We didn't complete the bail out then, so here we
10198 are. */
10199 return error_mark_node;
10200
10201 context = DECL_CONTEXT (gen_tmpl);
10202 if (context && TYPE_P (context))
10203 {
10204 if (!uses_template_parms (DECL_CONTEXT (templ)))
10205 /* If the context of the partially instantiated template is
10206 already non-dependent, then we might as well use it. */
10207 context = DECL_CONTEXT (templ);
10208 else
10209 {
10210 context = tsubst_aggr_type (context, arglist,
10211 complain, in_decl, true);
10212 /* Try completing the enclosing context if it's not already so. */
10213 if (context != error_mark_node
10214 && !COMPLETE_TYPE_P (context))
10215 {
10216 context = complete_type (context);
10217 if (COMPLETE_TYPE_P (context))
10218 {
10219 /* Completion could have caused us to register the desired
10220 specialization already, so check the table again. */
10221 entry = type_specializations->find_with_hash (&elt, hash);
10222 if (entry)
10223 return entry->spec;
10224 }
10225 }
10226 }
10227 }
10228 else
10229 context = tsubst (context, arglist, complain, in_decl);
10230
10231 if (context == error_mark_node)
10232 return error_mark_node;
10233
10234 if (!context)
10235 context = global_namespace;
10236
10237 /* Create the type. */
10238 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10239 {
10240 if (!is_dependent_type)
10241 {
10242 set_current_access_from_decl (TYPE_NAME (template_type));
10243 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10244 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10245 arglist, complain, in_decl),
10246 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10247 arglist, complain, in_decl),
10248 SCOPED_ENUM_P (template_type), NULL);
10249
10250 if (t == error_mark_node)
10251 return t;
10252 }
10253 else
10254 {
10255 /* We don't want to call start_enum for this type, since
10256 the values for the enumeration constants may involve
10257 template parameters. And, no one should be interested
10258 in the enumeration constants for such a type. */
10259 t = cxx_make_type (ENUMERAL_TYPE);
10260 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10261 }
10262 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10263 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10264 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10265 }
10266 else if (CLASS_TYPE_P (template_type))
10267 {
10268 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10269 instantiated here. */
10270 gcc_assert (!LAMBDA_TYPE_P (template_type));
10271
10272 t = make_class_type (TREE_CODE (template_type));
10273 CLASSTYPE_DECLARED_CLASS (t)
10274 = CLASSTYPE_DECLARED_CLASS (template_type);
10275 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10276
10277 /* A local class. Make sure the decl gets registered properly. */
10278 if (context == current_function_decl)
10279 if (pushtag (DECL_NAME (gen_tmpl), t)
10280 == error_mark_node)
10281 return error_mark_node;
10282
10283 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10284 /* This instantiation is another name for the primary
10285 template type. Set the TYPE_CANONICAL field
10286 appropriately. */
10287 TYPE_CANONICAL (t) = template_type;
10288 else if (any_template_arguments_need_structural_equality_p (arglist))
10289 SET_TYPE_STRUCTURAL_EQUALITY (t);
10290 }
10291 else
10292 gcc_unreachable ();
10293
10294 /* If we called start_enum or pushtag above, this information
10295 will already be set up. */
10296 type_decl = TYPE_NAME (t);
10297 if (!type_decl)
10298 {
10299 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10300
10301 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10302 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10303 DECL_SOURCE_LOCATION (type_decl)
10304 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10305 }
10306
10307 set_instantiating_module (type_decl);
10308 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10309 of export flag. We want to propagate this because it might
10310 be a friend declaration that pushes a new hidden binding. */
10311 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10312
10313 if (CLASS_TYPE_P (template_type))
10314 {
10315 TREE_PRIVATE (type_decl)
10316 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10317 TREE_PROTECTED (type_decl)
10318 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10319 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10320 {
10321 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10322 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10323 }
10324 }
10325
10326 if (OVERLOAD_TYPE_P (t))
10327 {
10328 static const char *tags[] = {"abi_tag", "may_alias"};
10329
10330 for (unsigned ix = 0; ix != 2; ix++)
10331 {
10332 tree attributes
10333 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10334
10335 if (attributes)
10336 TYPE_ATTRIBUTES (t)
10337 = tree_cons (TREE_PURPOSE (attributes),
10338 TREE_VALUE (attributes),
10339 TYPE_ATTRIBUTES (t));
10340 }
10341 }
10342
10343 /* Let's consider the explicit specialization of a member
10344 of a class template specialization that is implicitly instantiated,
10345 e.g.:
10346 template<class T>
10347 struct S
10348 {
10349 template<class U> struct M {}; //#0
10350 };
10351
10352 template<>
10353 template<>
10354 struct S<int>::M<char> //#1
10355 {
10356 int i;
10357 };
10358 [temp.expl.spec]/4 says this is valid.
10359
10360 In this case, when we write:
10361 S<int>::M<char> m;
10362
10363 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10364 the one of #0.
10365
10366 When we encounter #1, we want to store the partial instantiation
10367 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10368
10369 For all cases other than this "explicit specialization of member of a
10370 class template", we just want to store the most general template into
10371 the CLASSTYPE_TI_TEMPLATE of M.
10372
10373 This case of "explicit specialization of member of a class template"
10374 only happens when:
10375 1/ the enclosing class is an instantiation of, and therefore not
10376 the same as, the context of the most general template, and
10377 2/ we aren't looking at the partial instantiation itself, i.e.
10378 the innermost arguments are not the same as the innermost parms of
10379 the most general template.
10380
10381 So it's only when 1/ and 2/ happens that we want to use the partial
10382 instantiation of the member template in lieu of its most general
10383 template. */
10384
10385 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10386 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10387 /* the enclosing class must be an instantiation... */
10388 && CLASS_TYPE_P (context)
10389 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10390 {
10391 TREE_VEC_LENGTH (arglist)--;
10392 ++processing_template_decl;
10393 tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
10394 tree partial_inst_args =
10395 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10396 arglist, complain, NULL_TREE);
10397 --processing_template_decl;
10398 TREE_VEC_LENGTH (arglist)++;
10399 if (partial_inst_args == error_mark_node)
10400 return error_mark_node;
10401 use_partial_inst_tmpl =
10402 /*...and we must not be looking at the partial instantiation
10403 itself. */
10404 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10405 partial_inst_args);
10406 }
10407
10408 if (!use_partial_inst_tmpl)
10409 /* This case is easy; there are no member templates involved. */
10410 found = gen_tmpl;
10411 else
10412 {
10413 /* This is a full instantiation of a member template. Find
10414 the partial instantiation of which this is an instance. */
10415
10416 /* Temporarily reduce by one the number of levels in the ARGLIST
10417 so as to avoid comparing the last set of arguments. */
10418 TREE_VEC_LENGTH (arglist)--;
10419 /* We don't use COMPLAIN in the following call because this isn't
10420 the immediate context of deduction. For instance, tf_partial
10421 could be set here as we might be at the beginning of template
10422 argument deduction when any explicitly specified template
10423 arguments are substituted into the function type. tf_partial
10424 could lead into trouble because we wouldn't find the partial
10425 instantiation that might have been created outside tf_partial
10426 context, because the levels of template parameters wouldn't
10427 match, because in a tf_partial context, tsubst doesn't reduce
10428 TEMPLATE_PARM_LEVEL. */
10429 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10430 TREE_VEC_LENGTH (arglist)++;
10431 found = (TREE_CODE (found) == TEMPLATE_DECL
10432 ? found
10433 : CLASSTYPE_TI_TEMPLATE (found));
10434
10435 if (DECL_CLASS_TEMPLATE_P (found)
10436 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10437 {
10438 /* If this partial instantiation is specialized, we want to
10439 use it for hash table lookup. */
10440 elt.tmpl = found;
10441 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10442 hash = spec_hasher::hash (&elt);
10443 }
10444 }
10445
10446 /* Build template info for the new specialization. */
10447 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10448
10449 elt.spec = t;
10450 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10451 gcc_checking_assert (*slot == NULL);
10452 entry = ggc_alloc<spec_entry> ();
10453 *entry = elt;
10454 *slot = entry;
10455
10456 /* Note this use of the partial instantiation so we can check it
10457 later in maybe_process_partial_specialization. */
10458 DECL_TEMPLATE_INSTANTIATIONS (found)
10459 = tree_cons (arglist, t,
10460 DECL_TEMPLATE_INSTANTIATIONS (found));
10461
10462 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10463 && !uses_template_parms (current_nonlambda_scope ()))
10464 /* Now that the type has been registered on the instantiations
10465 list, we set up the enumerators. Because the enumeration
10466 constants may involve the enumeration type itself, we make
10467 sure to register the type first, and then create the
10468 constants. That way, doing tsubst_expr for the enumeration
10469 constants won't result in recursive calls here; we'll find
10470 the instantiation and exit above. */
10471 tsubst_enum (template_type, t, arglist);
10472
10473 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10474 /* If the type makes use of template parameters, the
10475 code that generates debugging information will crash. */
10476 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10477
10478 /* Possibly limit visibility based on template args. */
10479 TREE_PUBLIC (type_decl) = 1;
10480 determine_visibility (type_decl);
10481
10482 inherit_targ_abi_tags (t);
10483
10484 return t;
10485 }
10486 }
10487
10488 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10489
10490 tree
10491 lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
10492 {
10493 if (flag_concepts && variable_concept_p (templ))
10494 return build_concept_check (templ, arglist, tf_none);
10495
10496 tree gen_templ = most_general_template (templ);
10497 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
10498 arglist = add_outermost_template_args (templ, arglist);
10499 arglist = coerce_template_parms (parms, arglist, templ, complain);
10500 if (arglist == error_mark_node)
10501 return error_mark_node;
10502
10503 /* The type of the expression is NULL_TREE since the template-id could refer
10504 to an explicit or partial specialization. */
10505 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10506 }
10507
10508 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
10509 not dependent. */
10510
10511 tree
10512 finish_template_variable (tree var, tsubst_flags_t complain)
10513 {
10514 tree templ = TREE_OPERAND (var, 0);
10515 tree arglist = TREE_OPERAND (var, 1);
10516
10517 /* If the template or arguments are dependent, then we
10518 can't resolve the TEMPLATE_ID_EXPR yet. */
10519 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
10520 || any_dependent_template_arguments_p (arglist))
10521 return var;
10522
10523 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10524 {
10525 if (complain & tf_error)
10526 {
10527 auto_diagnostic_group d;
10528 error ("use of invalid variable template %qE", var);
10529 diagnose_constraints (location_of (var), templ, arglist);
10530 }
10531 return error_mark_node;
10532 }
10533
10534 return instantiate_template (templ, arglist, complain);
10535 }
10536
10537 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10538 TARGS template args, and instantiate it if it's not dependent. */
10539
10540 tree
10541 lookup_and_finish_template_variable (tree templ, tree targs,
10542 tsubst_flags_t complain)
10543 {
10544 tree var = lookup_template_variable (templ, targs, complain);
10545 if (var == error_mark_node)
10546 return error_mark_node;
10547 var = finish_template_variable (var, complain);
10548 mark_used (var, complain);
10549 return var;
10550 }
10551
10552 /* If the set of template parameters PARMS contains a template parameter
10553 at the given LEVEL and INDEX, then return this parameter. Otherwise
10554 return NULL_TREE. */
10555
10556 static tree
10557 corresponding_template_parameter_list (tree parms, int level, int index)
10558 {
10559 while (TMPL_PARMS_DEPTH (parms) > level)
10560 parms = TREE_CHAIN (parms);
10561
10562 if (TMPL_PARMS_DEPTH (parms) != level
10563 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10564 return NULL_TREE;
10565
10566 return TREE_VEC_ELT (TREE_VALUE (parms), index);
10567 }
10568
10569 /* Return the TREE_LIST for the template parameter from PARMS that positionally
10570 corresponds to the template parameter PARM, or else return NULL_TREE. */
10571
10572 static tree
10573 corresponding_template_parameter_list (tree parms, tree parm)
10574 {
10575 int level, index;
10576 template_parm_level_and_index (parm, &level, &index);
10577 return corresponding_template_parameter_list (parms, level, index);
10578 }
10579
10580 /* As above, but pull out the actual parameter. */
10581
10582 static tree
10583 corresponding_template_parameter (tree parms, tree parm)
10584 {
10585 tree list = corresponding_template_parameter_list (parms, parm);
10586 if (!list)
10587 return NULL_TREE;
10588
10589 tree t = TREE_VALUE (list);
10590 /* As in template_parm_to_arg. */
10591 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10592 t = TREE_TYPE (t);
10593 else
10594 t = DECL_INITIAL (t);
10595
10596 gcc_assert (TEMPLATE_PARM_P (t));
10597 return t;
10598 }
10599 \f
10600 struct pair_fn_data
10601 {
10602 tree_fn_t fn;
10603 tree_fn_t any_fn;
10604 void *data;
10605 /* True when we should also visit template parameters that occur in
10606 non-deduced contexts. */
10607 bool include_nondeduced_p;
10608 hash_set<tree> *visited;
10609 };
10610
10611 /* Called from for_each_template_parm via walk_tree. */
10612
10613 static tree
10614 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10615 {
10616 tree t = *tp;
10617 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10618 tree_fn_t fn = pfd->fn;
10619 void *data = pfd->data;
10620 tree result = NULL_TREE;
10621
10622 #define WALK_SUBTREE(NODE) \
10623 do \
10624 { \
10625 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10626 pfd->include_nondeduced_p, \
10627 pfd->any_fn); \
10628 if (result) goto out; \
10629 } \
10630 while (0)
10631
10632 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10633 return t;
10634
10635 if (TYPE_P (t)
10636 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10637 WALK_SUBTREE (TYPE_CONTEXT (t));
10638
10639 switch (TREE_CODE (t))
10640 {
10641 case RECORD_TYPE:
10642 if (TYPE_PTRMEMFUNC_P (t))
10643 break;
10644 /* Fall through. */
10645
10646 case UNION_TYPE:
10647 case ENUMERAL_TYPE:
10648 if (!TYPE_TEMPLATE_INFO (t))
10649 *walk_subtrees = 0;
10650 else
10651 WALK_SUBTREE (TYPE_TI_ARGS (t));
10652 break;
10653
10654 case INTEGER_TYPE:
10655 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10656 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10657 break;
10658
10659 case METHOD_TYPE:
10660 /* Since we're not going to walk subtrees, we have to do this
10661 explicitly here. */
10662 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10663 /* Fall through. */
10664
10665 case FUNCTION_TYPE:
10666 /* Check the return type. */
10667 WALK_SUBTREE (TREE_TYPE (t));
10668
10669 /* Check the parameter types. Since default arguments are not
10670 instantiated until they are needed, the TYPE_ARG_TYPES may
10671 contain expressions that involve template parameters. But,
10672 no-one should be looking at them yet. And, once they're
10673 instantiated, they don't contain template parameters, so
10674 there's no point in looking at them then, either. */
10675 {
10676 tree parm;
10677
10678 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10679 WALK_SUBTREE (TREE_VALUE (parm));
10680
10681 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10682 want walk_tree walking into them itself. */
10683 *walk_subtrees = 0;
10684 }
10685
10686 if (flag_noexcept_type)
10687 {
10688 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10689 if (spec)
10690 WALK_SUBTREE (TREE_PURPOSE (spec));
10691 }
10692 break;
10693
10694 case TYPEOF_TYPE:
10695 case DECLTYPE_TYPE:
10696 if (pfd->include_nondeduced_p
10697 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10698 pfd->visited,
10699 pfd->include_nondeduced_p,
10700 pfd->any_fn))
10701 return error_mark_node;
10702 *walk_subtrees = false;
10703 break;
10704
10705 case TRAIT_TYPE:
10706 if (pfd->include_nondeduced_p)
10707 {
10708 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
10709 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
10710 }
10711 *walk_subtrees = false;
10712 break;
10713
10714 case FUNCTION_DECL:
10715 case VAR_DECL:
10716 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10717 WALK_SUBTREE (DECL_TI_ARGS (t));
10718 break;
10719
10720 case PARM_DECL:
10721 WALK_SUBTREE (TREE_TYPE (t));
10722 break;
10723
10724 case CONST_DECL:
10725 if (DECL_TEMPLATE_PARM_P (t))
10726 WALK_SUBTREE (DECL_INITIAL (t));
10727 if (DECL_CONTEXT (t)
10728 && pfd->include_nondeduced_p)
10729 WALK_SUBTREE (DECL_CONTEXT (t));
10730 break;
10731
10732 case BOUND_TEMPLATE_TEMPLATE_PARM:
10733 /* Record template parameters such as `T' inside `TT<T>'. */
10734 WALK_SUBTREE (TYPE_TI_ARGS (t));
10735 /* Fall through. */
10736
10737 case TEMPLATE_TEMPLATE_PARM:
10738 case TEMPLATE_TYPE_PARM:
10739 case TEMPLATE_PARM_INDEX:
10740 if (fn && (*fn)(t, data))
10741 return t;
10742 else if (!fn)
10743 return t;
10744 break;
10745
10746 case TEMPLATE_DECL:
10747 /* A template template parameter is encountered. */
10748 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10749 WALK_SUBTREE (TREE_TYPE (t));
10750
10751 /* Already substituted template template parameter */
10752 *walk_subtrees = 0;
10753 break;
10754
10755 case TYPENAME_TYPE:
10756 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10757 partial instantiation. */
10758 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10759 *walk_subtrees = 0;
10760 break;
10761
10762 case INDIRECT_REF:
10763 case COMPONENT_REF:
10764 /* If there's no type, then this thing must be some expression
10765 involving template parameters. */
10766 if (!fn && !TREE_TYPE (t))
10767 return error_mark_node;
10768 break;
10769
10770 case CONSTRUCTOR:
10771 case TRAIT_EXPR:
10772 case PLUS_EXPR:
10773 case MULT_EXPR:
10774 case SCOPE_REF:
10775 /* These are non-deduced contexts. */
10776 if (!pfd->include_nondeduced_p)
10777 *walk_subtrees = 0;
10778 break;
10779
10780 case MODOP_EXPR:
10781 case CAST_EXPR:
10782 case IMPLICIT_CONV_EXPR:
10783 case REINTERPRET_CAST_EXPR:
10784 case CONST_CAST_EXPR:
10785 case STATIC_CAST_EXPR:
10786 case DYNAMIC_CAST_EXPR:
10787 case ARROW_EXPR:
10788 case DOTSTAR_EXPR:
10789 case TYPEID_EXPR:
10790 case PSEUDO_DTOR_EXPR:
10791 if (!fn)
10792 return error_mark_node;
10793 break;
10794
10795 default:
10796 break;
10797 }
10798
10799 #undef WALK_SUBTREE
10800
10801 /* We didn't find any template parameters we liked. */
10802 out:
10803 return result;
10804 }
10805
10806 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10807 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10808 call FN with the parameter and the DATA.
10809 If FN returns nonzero, the iteration is terminated, and
10810 for_each_template_parm returns 1. Otherwise, the iteration
10811 continues. If FN never returns a nonzero value, the value
10812 returned by for_each_template_parm is 0. If FN is NULL, it is
10813 considered to be the function which always returns 1.
10814
10815 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10816 parameters that occur in non-deduced contexts. When false, only
10817 visits those template parameters that can be deduced. */
10818
10819 static tree
10820 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10821 hash_set<tree> *visited,
10822 bool include_nondeduced_p,
10823 tree_fn_t any_fn)
10824 {
10825 struct pair_fn_data pfd;
10826 tree result;
10827
10828 /* Set up. */
10829 pfd.fn = fn;
10830 pfd.any_fn = any_fn;
10831 pfd.data = data;
10832 pfd.include_nondeduced_p = include_nondeduced_p;
10833
10834 /* Walk the tree. (Conceptually, we would like to walk without
10835 duplicates, but for_each_template_parm_r recursively calls
10836 for_each_template_parm, so we would need to reorganize a fair
10837 bit to use walk_tree_without_duplicates, so we keep our own
10838 visited list.) */
10839 if (visited)
10840 pfd.visited = visited;
10841 else
10842 pfd.visited = new hash_set<tree>;
10843 result = cp_walk_tree (&t,
10844 for_each_template_parm_r,
10845 &pfd,
10846 pfd.visited);
10847
10848 /* Clean up. */
10849 if (!visited)
10850 {
10851 delete pfd.visited;
10852 pfd.visited = 0;
10853 }
10854
10855 return result;
10856 }
10857
10858 struct find_template_parameter_info
10859 {
10860 explicit find_template_parameter_info (tree ctx_parms)
10861 : ctx_parms (ctx_parms),
10862 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10863 {}
10864
10865 hash_set<tree> visited;
10866 hash_set<tree> parms;
10867 tree parm_list = NULL_TREE;
10868 tree *parm_list_tail = &parm_list;
10869 tree ctx_parms;
10870 int max_depth;
10871
10872 tree find_in (tree);
10873 tree find_in_recursive (tree);
10874 bool found (tree);
10875 unsigned num_found () { return parms.elements (); }
10876 };
10877
10878 /* Appends the declaration of T to the list in DATA. */
10879
10880 static int
10881 keep_template_parm (tree t, void* data)
10882 {
10883 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10884
10885 /* Template parameters declared within the expression are not part of
10886 the parameter mapping. For example, in this concept:
10887
10888 template<typename T>
10889 concept C = requires { <expr> } -> same_as<int>;
10890
10891 the return specifier same_as<int> declares a new decltype parameter
10892 that must not be part of the parameter mapping. The same is true
10893 for generic lambda parameters, lambda template parameters, etc. */
10894 int level;
10895 int index;
10896 template_parm_level_and_index (t, &level, &index);
10897 if (level == 0 || level > ftpi->max_depth)
10898 return 0;
10899
10900 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10901 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10902 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10903 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10904
10905 /* This template parameter might be an argument to a cached dependent
10906 specalization that was formed earlier inside some other template, in
10907 which case the parameter is not among the ones that are in-scope.
10908 Look in CTX_PARMS to find the corresponding in-scope template
10909 parameter, and use it instead. */
10910 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10911 t = in_scope;
10912
10913 /* Arguments like const T yield parameters like const T. This means that
10914 a template-id like X<T, const T> would yield two distinct parameters:
10915 T and const T. Adjust types to their unqualified versions. */
10916 if (TYPE_P (t))
10917 t = TYPE_MAIN_VARIANT (t);
10918 if (!ftpi->parms.add (t))
10919 {
10920 /* Append T to PARM_LIST. */
10921 tree node = build_tree_list (NULL_TREE, t);
10922 *ftpi->parm_list_tail = node;
10923 ftpi->parm_list_tail = &TREE_CHAIN (node);
10924 }
10925
10926 /* Verify the parameter we found has a valid index. */
10927 if (flag_checking)
10928 {
10929 tree parms = ftpi->ctx_parms;
10930 while (TMPL_PARMS_DEPTH (parms) > level)
10931 parms = TREE_CHAIN (parms);
10932 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10933 gcc_assert (index < len);
10934 }
10935
10936 return 0;
10937 }
10938
10939 /* Ensure that we recursively examine certain terms that are not normally
10940 visited in for_each_template_parm_r. */
10941
10942 static int
10943 any_template_parm_r (tree t, void *data)
10944 {
10945 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10946
10947 #define WALK_SUBTREE(NODE) \
10948 do \
10949 { \
10950 for_each_template_parm (NODE, keep_template_parm, data, \
10951 &ftpi->visited, true, \
10952 any_template_parm_r); \
10953 } \
10954 while (0)
10955
10956 /* A mention of a member alias/typedef is a use of all of its template
10957 arguments, including those from the enclosing class, so we don't use
10958 alias_template_specialization_p here. */
10959 if (TYPE_P (t) && typedef_variant_p (t))
10960 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10961 WALK_SUBTREE (TI_ARGS (tinfo));
10962
10963 switch (TREE_CODE (t))
10964 {
10965 case TEMPLATE_TYPE_PARM:
10966 /* Type constraints of a placeholder type may contain parameters. */
10967 if (is_auto (t))
10968 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10969 WALK_SUBTREE (constr);
10970 break;
10971
10972 case TEMPLATE_ID_EXPR:
10973 /* Search through references to variable templates. */
10974 WALK_SUBTREE (TREE_OPERAND (t, 0));
10975 WALK_SUBTREE (TREE_OPERAND (t, 1));
10976 break;
10977
10978 case TEMPLATE_PARM_INDEX:
10979 WALK_SUBTREE (TREE_TYPE (t));
10980 break;
10981
10982 case TEMPLATE_DECL:
10983 /* If T is a member template that shares template parameters with
10984 ctx_parms, we need to mark all those parameters for mapping.
10985 To that end, it should suffice to just walk the DECL_CONTEXT of
10986 the template (assuming the template is not overly general). */
10987 WALK_SUBTREE (DECL_CONTEXT (t));
10988 break;
10989
10990 case LAMBDA_EXPR:
10991 {
10992 /* Look in the parms and body. */
10993 tree fn = lambda_function (t);
10994 WALK_SUBTREE (TREE_TYPE (fn));
10995 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10996 }
10997 break;
10998
10999 case IDENTIFIER_NODE:
11000 if (IDENTIFIER_CONV_OP_P (t))
11001 /* The conversion-type-id of a conversion operator may be dependent. */
11002 WALK_SUBTREE (TREE_TYPE (t));
11003 break;
11004
11005 case CONVERT_EXPR:
11006 if (is_dummy_object (t))
11007 WALK_SUBTREE (TREE_TYPE (t));
11008 break;
11009
11010 default:
11011 break;
11012 }
11013
11014 /* Keep walking. */
11015 return 0;
11016 }
11017
11018 /* Look through T for template parameters. */
11019
11020 tree
11021 find_template_parameter_info::find_in (tree t)
11022 {
11023 return for_each_template_parm (t, keep_template_parm, this, &visited,
11024 /*include_nondeduced*/true,
11025 any_template_parm_r);
11026 }
11027
11028 /* As above, but also recursively look into the default arguments of template
11029 parameters we found. Used for alias CTAD. */
11030
11031 tree
11032 find_template_parameter_info::find_in_recursive (tree t)
11033 {
11034 if (tree r = find_in (t))
11035 return r;
11036 /* Since newly found parms are added to the end of the list, we
11037 can just walk it until we reach the end. */
11038 for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
11039 {
11040 tree parm = TREE_VALUE (pl);
11041 tree list = corresponding_template_parameter_list (ctx_parms, parm);
11042 if (tree r = find_in (TREE_PURPOSE (list)))
11043 return r;
11044 }
11045 return NULL_TREE;
11046 }
11047
11048 /* True if PARM was found by a previous call to find_in. PARM can be a
11049 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */
11050
11051 bool
11052 find_template_parameter_info::found (tree parm)
11053 {
11054 if (TREE_CODE (parm) == TREE_LIST)
11055 parm = TREE_VALUE (parm);
11056 if (TREE_CODE (parm) == TYPE_DECL
11057 || TREE_CODE (parm) == TEMPLATE_DECL)
11058 parm = TREE_TYPE (parm);
11059 else
11060 parm = DECL_INITIAL (parm);
11061 gcc_checking_assert (TEMPLATE_PARM_P (parm));
11062 return parms.contains (parm);
11063 }
11064
11065 /* Returns a list of unique template parameters found within T, where CTX_PARMS
11066 are the template parameters in scope. */
11067
11068 tree
11069 find_template_parameters (tree t, tree ctx_parms)
11070 {
11071 if (!ctx_parms)
11072 return NULL_TREE;
11073
11074 find_template_parameter_info ftpi (ctx_parms);
11075 ftpi.find_in (t);
11076 return ftpi.parm_list;
11077 }
11078
11079 /* Returns true if T depends on any template parameter. */
11080
11081 bool
11082 uses_template_parms (tree t)
11083 {
11084 if (t == NULL_TREE || t == error_mark_node)
11085 return false;
11086
11087 /* Namespaces can't depend on any template parameters. */
11088 if (TREE_CODE (t) == NAMESPACE_DECL)
11089 return false;
11090
11091 processing_template_decl_sentinel ptds (/*reset*/false);
11092 ++processing_template_decl;
11093
11094 if (TYPE_P (t))
11095 return dependent_type_p (t);
11096 else if (TREE_CODE (t) == TREE_VEC)
11097 return any_dependent_template_arguments_p (t);
11098 else if (TREE_CODE (t) == TREE_LIST)
11099 return (uses_template_parms (TREE_VALUE (t))
11100 || uses_template_parms (TREE_CHAIN (t)));
11101 else if (TREE_CODE (t) == TYPE_DECL)
11102 return dependent_type_p (TREE_TYPE (t));
11103 else
11104 return instantiation_dependent_expression_p (t);
11105 }
11106
11107 /* Returns true if T depends on any template parameter with level LEVEL. */
11108
11109 bool
11110 uses_template_parms_level (tree t, int level)
11111 {
11112 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
11113 /*include_nondeduced_p=*/true);
11114 }
11115
11116 /* Returns true if the signature of DECL depends on any template parameter from
11117 its enclosing class. */
11118
11119 static bool
11120 uses_outer_template_parms (tree decl)
11121 {
11122 int depth;
11123 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11124 depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
11125 else
11126 depth = template_class_depth (CP_DECL_CONTEXT (decl));
11127 if (depth == 0)
11128 return false;
11129 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
11130 &depth, NULL, /*include_nondeduced_p=*/true))
11131 return true;
11132 if (PRIMARY_TEMPLATE_P (decl)
11133 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
11134 {
11135 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
11136 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
11137 {
11138 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
11139 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
11140 if (TREE_CODE (parm) == PARM_DECL
11141 && for_each_template_parm (TREE_TYPE (parm),
11142 template_parm_outer_level,
11143 &depth, NULL, /*nondeduced*/true))
11144 return true;
11145 if (TREE_CODE (parm) == TEMPLATE_DECL
11146 && uses_outer_template_parms (parm))
11147 return true;
11148 if (defarg
11149 && for_each_template_parm (defarg, template_parm_outer_level,
11150 &depth, NULL, /*nondeduced*/true))
11151 return true;
11152 }
11153 }
11154 if (uses_outer_template_parms_in_constraints (decl))
11155 return true;
11156 return false;
11157 }
11158
11159 /* Returns true if the constraints of DECL depend on any template parameters
11160 from its enclosing scope. */
11161
11162 bool
11163 uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
11164 {
11165 tree ci = get_constraints (decl);
11166 if (ci)
11167 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
11168 if (!ci)
11169 return false;
11170 if (!ctx)
11171 {
11172 if (tree fc = DECL_FRIEND_CONTEXT (decl))
11173 ctx = fc;
11174 else
11175 ctx = CP_DECL_CONTEXT (decl);
11176 }
11177 int depth = template_class_depth (ctx);
11178 if (depth == 0)
11179 return false;
11180 return for_each_template_parm (ci, template_parm_outer_level,
11181 &depth, NULL, /*nondeduced*/true);
11182 }
11183
11184 /* Returns TRUE iff INST is an instantiation we don't need to do in an
11185 ill-formed translation unit, i.e. a variable or function that isn't
11186 usable in a constant expression. */
11187
11188 static inline bool
11189 neglectable_inst_p (tree d)
11190 {
11191 return (d && DECL_P (d)
11192 && !undeduced_auto_decl (d)
11193 && !(TREE_CODE (d) == FUNCTION_DECL
11194 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
11195 : decl_maybe_constant_var_p (d)));
11196 }
11197
11198 /* Returns TRUE iff we should refuse to instantiate DECL because it's
11199 neglectable and instantiated from within an erroneous instantiation. */
11200
11201 static bool
11202 limit_bad_template_recursion (tree decl)
11203 {
11204 struct tinst_level *lev = current_tinst_level;
11205 int errs = errorcount + sorrycount;
11206 if (errs == 0 || !neglectable_inst_p (decl))
11207 return false;
11208
11209 /* Avoid instantiating members of an ill-formed class. */
11210 bool refuse
11211 = (DECL_CLASS_SCOPE_P (decl)
11212 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
11213
11214 if (!refuse)
11215 {
11216 for (; lev; lev = lev->next)
11217 if (neglectable_inst_p (lev->maybe_get_node ()))
11218 break;
11219 refuse = (lev && errs > lev->errors);
11220 }
11221
11222 if (refuse)
11223 {
11224 /* Don't warn about it not being defined. */
11225 suppress_warning (decl, OPT_Wunused);
11226 tree clone;
11227 FOR_EACH_CLONE (clone, decl)
11228 suppress_warning (clone, OPT_Wunused);
11229 }
11230 return refuse;
11231 }
11232
11233 static int tinst_depth;
11234 extern int max_tinst_depth;
11235 int depth_reached;
11236
11237 static GTY(()) struct tinst_level *last_error_tinst_level;
11238
11239 /* We're starting to instantiate D; record the template instantiation context
11240 at LOC for diagnostics and to restore it later. */
11241
11242 bool
11243 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
11244 {
11245 struct tinst_level *new_level;
11246
11247 if (tinst_depth >= max_tinst_depth)
11248 {
11249 /* Tell error.cc not to try to instantiate any templates. */
11250 at_eof = 3;
11251 fatal_error (input_location,
11252 "template instantiation depth exceeds maximum of %d"
11253 " (use %<-ftemplate-depth=%> to increase the maximum)",
11254 max_tinst_depth);
11255 return false;
11256 }
11257
11258 /* If the current instantiation caused problems, don't let it instantiate
11259 anything else. Do allow deduction substitution and decls usable in
11260 constant expressions. */
11261 if (!targs && limit_bad_template_recursion (tldcl))
11262 {
11263 /* Avoid no_linkage_errors and unused function (and all other)
11264 warnings for this decl. */
11265 suppress_warning (tldcl);
11266 return false;
11267 }
11268
11269 /* When not -quiet, dump template instantiations other than functions, since
11270 announce_function will take care of those. */
11271 if (!quiet_flag && !targs
11272 && TREE_CODE (tldcl) != TREE_LIST
11273 && TREE_CODE (tldcl) != FUNCTION_DECL)
11274 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
11275
11276 new_level = tinst_level_freelist ().alloc ();
11277 new_level->tldcl = tldcl;
11278 new_level->targs = targs;
11279 new_level->locus = loc;
11280 new_level->errors = errorcount + sorrycount;
11281 new_level->next = NULL;
11282 new_level->refcount = 0;
11283 new_level->path = new_level->visible = nullptr;
11284 set_refcount_ptr (new_level->next, current_tinst_level);
11285 set_refcount_ptr (current_tinst_level, new_level);
11286
11287 ++tinst_depth;
11288 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11289 depth_reached = tinst_depth;
11290
11291 return true;
11292 }
11293
11294 /* We're starting substitution of TMPL<ARGS>; record the template
11295 substitution context for diagnostics and to restore it later. */
11296
11297 bool
11298 push_tinst_level (tree tmpl, tree args)
11299 {
11300 return push_tinst_level_loc (tmpl, args, input_location);
11301 }
11302
11303 /* We're starting to instantiate D; record INPUT_LOCATION and the
11304 template instantiation context for diagnostics and to restore it
11305 later. */
11306
11307 bool
11308 push_tinst_level (tree d)
11309 {
11310 return push_tinst_level_loc (d, input_location);
11311 }
11312
11313 /* Likewise, but record LOC as the program location. */
11314
11315 bool
11316 push_tinst_level_loc (tree d, location_t loc)
11317 {
11318 gcc_assert (TREE_CODE (d) != TREE_LIST);
11319 return push_tinst_level_loc (d, NULL, loc);
11320 }
11321
11322 /* We're done instantiating this template; return to the instantiation
11323 context. */
11324
11325 void
11326 pop_tinst_level (void)
11327 {
11328 /* Restore the filename and line number stashed away when we started
11329 this instantiation. */
11330 input_location = current_tinst_level->locus;
11331 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11332 --tinst_depth;
11333 }
11334
11335 /* We're instantiating a deferred template; restore the template
11336 instantiation context in which the instantiation was requested, which
11337 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11338
11339 static tree
11340 reopen_tinst_level (struct tinst_level *level)
11341 {
11342 struct tinst_level *t;
11343
11344 tinst_depth = 0;
11345 for (t = level; t; t = t->next)
11346 ++tinst_depth;
11347
11348 set_refcount_ptr (current_tinst_level, level);
11349 pop_tinst_level ();
11350 if (current_tinst_level)
11351 current_tinst_level->errors = errorcount+sorrycount;
11352 return level->maybe_get_node ();
11353 }
11354
11355 /* Returns the TINST_LEVEL which gives the original instantiation
11356 context. */
11357
11358 struct tinst_level *
11359 outermost_tinst_level (void)
11360 {
11361 struct tinst_level *level = current_tinst_level;
11362 if (level)
11363 while (level->next)
11364 level = level->next;
11365 return level;
11366 }
11367
11368 /* True iff T is a friend function declaration that is not itself a template
11369 and is not defined in a class template. */
11370
11371 bool
11372 non_templated_friend_p (tree t)
11373 {
11374 if (t && TREE_CODE (t) == FUNCTION_DECL
11375 && DECL_UNIQUE_FRIEND_P (t))
11376 {
11377 tree ti = DECL_TEMPLATE_INFO (t);
11378 if (!ti)
11379 return true;
11380 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11381 if (DECL_FRIEND_CONTEXT (t))
11382 return false;
11383 /* Non-templated friends in a class template are still represented with a
11384 TEMPLATE_DECL; check that its primary template is the befriending
11385 class. Note that DECL_PRIMARY_TEMPLATE is null for
11386 template <class T> friend A<T>::f(); */
11387 tree tmpl = TI_TEMPLATE (ti);
11388 tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
11389 return (primary && primary != tmpl);
11390 }
11391 else
11392 return false;
11393 }
11394
11395 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11396 vector of template arguments, as for tsubst.
11397
11398 Returns an appropriate tsubst'd friend declaration. */
11399
11400 static tree
11401 tsubst_friend_function (tree decl, tree args)
11402 {
11403 tree new_friend;
11404
11405 if (TREE_CODE (decl) == FUNCTION_DECL
11406 && DECL_TEMPLATE_INSTANTIATION (decl)
11407 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11408 /* This was a friend declared with an explicit template
11409 argument list, e.g.:
11410
11411 friend void f<>(T);
11412
11413 to indicate that f was a template instantiation, not a new
11414 function declaration. Now, we have to figure out what
11415 instantiation of what template. */
11416 {
11417 tree template_id, arglist, fns;
11418 tree new_args;
11419 tree tmpl;
11420 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11421
11422 /* Friend functions are looked up in the containing namespace scope.
11423 We must enter that scope, to avoid finding member functions of the
11424 current class with same name. */
11425 push_nested_namespace (ns);
11426 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11427 tf_warning_or_error, NULL_TREE);
11428 pop_nested_namespace (ns);
11429 arglist = tsubst (DECL_TI_ARGS (decl), args,
11430 tf_warning_or_error, NULL_TREE);
11431 template_id = lookup_template_function (fns, arglist);
11432
11433 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11434 tmpl = determine_specialization (template_id, new_friend,
11435 &new_args,
11436 /*need_member_template=*/0,
11437 TREE_VEC_LENGTH (args),
11438 tsk_none);
11439 return instantiate_template (tmpl, new_args, tf_error);
11440 }
11441
11442 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11443 if (new_friend == error_mark_node)
11444 return error_mark_node;
11445
11446 /* The NEW_FRIEND will look like an instantiation, to the
11447 compiler, but is not an instantiation from the point of view of
11448 the language. For example, we might have had:
11449
11450 template <class T> struct S {
11451 template <class U> friend void f(T, U);
11452 };
11453
11454 Then, in S<int>, template <class U> void f(int, U) is not an
11455 instantiation of anything. */
11456
11457 DECL_USE_TEMPLATE (new_friend) = 0;
11458 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11459 {
11460 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11461 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11462 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11463 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11464
11465 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11466 match in decls_match. */
11467 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11468 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11469 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11470 if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
11471 {
11472 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11473 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11474 tsubst_each_template_parm_constraints (parms, args,
11475 tf_warning_or_error);
11476 }
11477 }
11478
11479 /* The mangled name for the NEW_FRIEND is incorrect. The function
11480 is not a template instantiation and should not be mangled like
11481 one. Therefore, we forget the mangling here; we'll recompute it
11482 later if we need it. */
11483 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11484 {
11485 SET_DECL_RTL (new_friend, NULL);
11486 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11487 }
11488
11489 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11490 {
11491 tree old_decl;
11492 tree ns;
11493
11494 /* We must save some information from NEW_FRIEND before calling
11495 duplicate decls since that function will free NEW_FRIEND if
11496 possible. */
11497 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11498 tree new_friend_result_template_info = NULL_TREE;
11499 bool new_friend_is_defn =
11500 (new_friend_template_info
11501 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11502 (template_for_substitution (new_friend)))
11503 != NULL_TREE));
11504 tree not_tmpl = new_friend;
11505
11506 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11507 {
11508 /* This declaration is a `primary' template. */
11509 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11510
11511 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11512 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11513 }
11514
11515 /* Inside pushdecl_namespace_level, we will push into the
11516 current namespace. However, the friend function should go
11517 into the namespace of the template. */
11518 ns = decl_namespace_context (new_friend);
11519 push_nested_namespace (ns);
11520 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11521 pop_nested_namespace (ns);
11522
11523 if (old_decl == error_mark_node)
11524 return error_mark_node;
11525
11526 if (old_decl != new_friend)
11527 {
11528 /* This new friend declaration matched an existing
11529 declaration. For example, given:
11530
11531 template <class T> void f(T);
11532 template <class U> class C {
11533 template <class T> friend void f(T) {}
11534 };
11535
11536 the friend declaration actually provides the definition
11537 of `f', once C has been instantiated for some type. So,
11538 old_decl will be the out-of-class template declaration,
11539 while new_friend is the in-class definition.
11540
11541 But, if `f' was called before this point, the
11542 instantiation of `f' will have DECL_TI_ARGS corresponding
11543 to `T' but not to `U', references to which might appear
11544 in the definition of `f'. Previously, the most general
11545 template for an instantiation of `f' was the out-of-class
11546 version; now it is the in-class version. Therefore, we
11547 run through all specialization of `f', adding to their
11548 DECL_TI_ARGS appropriately. In particular, they need a
11549 new set of outer arguments, corresponding to the
11550 arguments for this class instantiation.
11551
11552 The same situation can arise with something like this:
11553
11554 friend void f(int);
11555 template <class T> class C {
11556 friend void f(T) {}
11557 };
11558
11559 when `C<int>' is instantiated. Now, `f(int)' is defined
11560 in the class. */
11561
11562 if (!new_friend_is_defn)
11563 /* On the other hand, if the in-class declaration does
11564 *not* provide a definition, then we don't want to alter
11565 existing definitions. We can just leave everything
11566 alone. */
11567 ;
11568 else
11569 {
11570 tree new_template = TI_TEMPLATE (new_friend_template_info);
11571 tree new_args = TI_ARGS (new_friend_template_info);
11572
11573 /* Overwrite whatever template info was there before, if
11574 any, with the new template information pertaining to
11575 the declaration. */
11576 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11577
11578 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11579 {
11580 /* We should have called reregister_specialization in
11581 duplicate_decls. */
11582 gcc_assert (retrieve_specialization (new_template,
11583 new_args, 0)
11584 == old_decl);
11585
11586 /* Instantiate it if the global has already been used. */
11587 if (DECL_ODR_USED (old_decl))
11588 instantiate_decl (old_decl, /*defer_ok=*/true,
11589 /*expl_inst_class_mem_p=*/false);
11590 }
11591 else
11592 {
11593 tree t;
11594
11595 /* Indicate that the old function template is a partial
11596 instantiation. */
11597 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11598 = new_friend_result_template_info;
11599
11600 gcc_assert (new_template
11601 == most_general_template (new_template));
11602 gcc_assert (new_template != old_decl);
11603
11604 /* Reassign any specializations already in the hash table
11605 to the new more general template, and add the
11606 additional template args. */
11607 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11608 t != NULL_TREE;
11609 t = TREE_CHAIN (t))
11610 {
11611 tree spec = TREE_VALUE (t);
11612 spec_entry elt;
11613
11614 elt.tmpl = old_decl;
11615 elt.args = DECL_TI_ARGS (spec);
11616 elt.spec = NULL_TREE;
11617
11618 decl_specializations->remove_elt (&elt);
11619
11620 DECL_TI_ARGS (spec)
11621 = add_outermost_template_args (new_args,
11622 DECL_TI_ARGS (spec));
11623
11624 register_specialization
11625 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11626
11627 }
11628 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11629 }
11630 }
11631
11632 /* The information from NEW_FRIEND has been merged into OLD_DECL
11633 by duplicate_decls. */
11634 new_friend = old_decl;
11635 }
11636
11637 /* We've just introduced a namespace-scope function in the purview
11638 without necessarily having opened the enclosing namespace, so
11639 make sure the namespace is in the purview now too. */
11640 if (modules_p ()
11641 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
11642 && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
11643 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
11644 }
11645 else
11646 {
11647 tree context = DECL_CONTEXT (new_friend);
11648 bool dependent_p;
11649
11650 /* In the code
11651 template <class T> class C {
11652 template <class U> friend void C1<U>::f (); // case 1
11653 friend void C2<T>::f (); // case 2
11654 };
11655 we only need to make sure CONTEXT is a complete type for
11656 case 2. To distinguish between the two cases, we note that
11657 CONTEXT of case 1 remains dependent type after tsubst while
11658 this isn't true for case 2. */
11659 ++processing_template_decl;
11660 dependent_p = dependent_type_p (context);
11661 --processing_template_decl;
11662
11663 if (!dependent_p
11664 && !complete_type_or_else (context, NULL_TREE))
11665 return error_mark_node;
11666
11667 if (COMPLETE_TYPE_P (context))
11668 {
11669 tree fn = new_friend;
11670 /* do_friend adds the TEMPLATE_DECL for any member friend
11671 template even if it isn't a member template, i.e.
11672 template <class T> friend A<T>::f();
11673 Look through it in that case. */
11674 if (TREE_CODE (fn) == TEMPLATE_DECL
11675 && !PRIMARY_TEMPLATE_P (fn))
11676 fn = DECL_TEMPLATE_RESULT (fn);
11677 /* Check to see that the declaration is really present, and,
11678 possibly obtain an improved declaration. */
11679 fn = check_classfn (context, fn, NULL_TREE);
11680
11681 if (fn)
11682 new_friend = fn;
11683 }
11684 }
11685
11686 return new_friend;
11687 }
11688
11689 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11690 template arguments, as for tsubst.
11691
11692 Returns an appropriate tsubst'd friend type or error_mark_node on
11693 failure. */
11694
11695 static tree
11696 tsubst_friend_class (tree friend_tmpl, tree args)
11697 {
11698 tree tmpl;
11699
11700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11701 {
11702 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11703 return TREE_TYPE (tmpl);
11704 }
11705
11706 tree context = CP_DECL_CONTEXT (friend_tmpl);
11707 if (TREE_CODE (context) == NAMESPACE_DECL)
11708 push_nested_namespace (context);
11709 else
11710 {
11711 context = tsubst (context, args, tf_error, NULL_TREE);
11712 push_nested_class (context);
11713 }
11714
11715 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11716 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11717
11718 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11719 {
11720 /* The friend template has already been declared. Just
11721 check to see that the declarations match, and install any new
11722 default parameters. We must tsubst the default parameters,
11723 of course. We only need the innermost template parameters
11724 because that is all that redeclare_class_template will look
11725 at. */
11726 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11727 > TMPL_ARGS_DEPTH (args))
11728 {
11729 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11730 args, tf_warning_or_error);
11731 tsubst_each_template_parm_constraints (parms, args,
11732 tf_warning_or_error);
11733 location_t saved_input_location = input_location;
11734 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11735 tree cons = get_constraints (friend_tmpl);
11736 ++processing_template_decl;
11737 cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
11738 DECL_FRIEND_CONTEXT (friend_tmpl));
11739 --processing_template_decl;
11740 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11741 input_location = saved_input_location;
11742 }
11743 }
11744 else
11745 {
11746 /* The friend template has not already been declared. In this
11747 case, the instantiation of the template class will cause the
11748 injection of this template into the namespace scope. */
11749 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11750
11751 if (tmpl != error_mark_node)
11752 {
11753 /* The new TMPL is not an instantiation of anything, so we
11754 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11755 for the new type because that is supposed to be the
11756 corresponding template decl, i.e., TMPL. */
11757 DECL_USE_TEMPLATE (tmpl) = 0;
11758 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11759 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11760 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11761 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11762
11763 /* Substitute into and set the constraints on the new declaration. */
11764 if (tree ci = get_constraints (friend_tmpl))
11765 {
11766 ++processing_template_decl;
11767 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11768 DECL_FRIEND_CONTEXT (friend_tmpl));
11769 --processing_template_decl;
11770 set_constraints (tmpl, ci);
11771 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
11772 args, tf_warning_or_error);
11773 }
11774
11775 /* Inject this template into the enclosing namspace scope. */
11776 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11777 }
11778 }
11779
11780 if (TREE_CODE (context) == NAMESPACE_DECL)
11781 pop_nested_namespace (context);
11782 else
11783 pop_nested_class ();
11784
11785 return TREE_TYPE (tmpl);
11786 }
11787
11788 /* Returns zero if TYPE cannot be completed later due to circularity.
11789 Otherwise returns one. */
11790
11791 static int
11792 can_complete_type_without_circularity (tree type)
11793 {
11794 if (type == NULL_TREE || type == error_mark_node)
11795 return 0;
11796 else if (COMPLETE_TYPE_P (type))
11797 return 1;
11798 else if (TREE_CODE (type) == ARRAY_TYPE)
11799 return can_complete_type_without_circularity (TREE_TYPE (type));
11800 else if (CLASS_TYPE_P (type)
11801 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11802 return 0;
11803 else
11804 return 1;
11805 }
11806
11807 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11808 tsubst_flags_t, tree);
11809
11810 /* Instantiate the contract statement. */
11811
11812 static tree
11813 tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
11814 tree in_decl)
11815 {
11816 tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
11817 bool auto_p = type_uses_auto (type);
11818
11819 tree r = copy_node (t);
11820
11821 /* Rebuild the result variable. */
11822 if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
11823 {
11824 tree oldvar = POSTCONDITION_IDENTIFIER (t);
11825
11826 tree newvar = copy_node (oldvar);
11827 TREE_TYPE (newvar) = type;
11828 DECL_CONTEXT (newvar) = decl;
11829 POSTCONDITION_IDENTIFIER (r) = newvar;
11830
11831 /* Make sure the postcondition is valid. */
11832 location_t loc = DECL_SOURCE_LOCATION (oldvar);
11833 if (!auto_p)
11834 if (!check_postcondition_result (decl, type, loc))
11835 return invalidate_contract (r);
11836
11837 /* Make the variable available for lookup. */
11838 register_local_specialization (newvar, oldvar);
11839 }
11840
11841 /* Instantiate the condition. If the return type is undeduced, process
11842 the expression as if inside a template to avoid spurious type errors. */
11843 if (auto_p)
11844 ++processing_template_decl;
11845 ++processing_contract_condition;
11846 CONTRACT_CONDITION (r)
11847 = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
11848 --processing_contract_condition;
11849 if (auto_p)
11850 --processing_template_decl;
11851
11852 /* And the comment. */
11853 CONTRACT_COMMENT (r)
11854 = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
11855
11856 return r;
11857 }
11858
11859 /* Update T by instantiating its contract attribute. */
11860
11861 static void
11862 tsubst_contract_attribute (tree decl, tree t, tree args,
11863 tsubst_flags_t complain, tree in_decl)
11864 {
11865 /* For non-specializations, adjust the current declaration to the most general
11866 version of in_decl. Because we defer the instantiation of contracts as long
11867 as possible, they are still written in terms of the parameters (and return
11868 type) of the most general template. */
11869 tree tmpl = DECL_TI_TEMPLATE (in_decl);
11870 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
11871 in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
11872 local_specialization_stack specs (lss_copy);
11873 register_parameter_specializations (in_decl, decl);
11874
11875 /* Get the contract to be instantiated. */
11876 tree contract = CONTRACT_STATEMENT (t);
11877
11878 /* Use the complete set of template arguments for instantiation. The
11879 contract may not have been instantiated and still refer to outer levels
11880 of template parameters. */
11881 args = DECL_TI_ARGS (decl);
11882
11883 /* For member functions, make this available for semantic analysis. */
11884 tree save_ccp = current_class_ptr;
11885 tree save_ccr = current_class_ref;
11886 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
11887 {
11888 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11889 tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
11890 inject_this_parameter (this_type, cp_type_quals (this_type));
11891 }
11892
11893 contract = tsubst_contract (decl, contract, args, complain, in_decl);
11894
11895 current_class_ptr = save_ccp;
11896 current_class_ref = save_ccr;
11897
11898 /* Rebuild the attribute. */
11899 TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
11900 }
11901
11902 /* Rebuild the attribute list for DECL, substituting into contracts
11903 as needed. */
11904
11905 void
11906 tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
11907 {
11908 tree list = copy_list (DECL_ATTRIBUTES (decl));
11909 for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
11910 {
11911 if (cxx_contract_attribute_p (attr))
11912 tsubst_contract_attribute (decl, attr, args, complain, in_decl);
11913 }
11914 DECL_ATTRIBUTES (decl) = list;
11915 }
11916
11917 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11918 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11919
11920 static tree
11921 tsubst_attribute (tree t, tree *decl_p, tree args,
11922 tsubst_flags_t complain, tree in_decl)
11923 {
11924 gcc_assert (ATTR_IS_DEPENDENT (t));
11925
11926 /* Note that contract attributes are never substituted from this function.
11927 Their instantiation is triggered by regenerate_from_template_decl when
11928 we instantiate the body of the function. */
11929
11930 tree val = TREE_VALUE (t);
11931 if (val == NULL_TREE)
11932 /* Nothing to do. */;
11933 else if ((flag_openmp || flag_openmp_simd)
11934 && is_attribute_p ("omp declare simd",
11935 get_attribute_name (t)))
11936 {
11937 tree clauses = TREE_VALUE (val);
11938 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11939 complain, in_decl);
11940 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11941 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11942 tree parms = DECL_ARGUMENTS (*decl_p);
11943 clauses
11944 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11945 if (clauses)
11946 val = build_tree_list (NULL_TREE, clauses);
11947 else
11948 val = NULL_TREE;
11949 }
11950 else if (flag_openmp
11951 && is_attribute_p ("omp declare variant base",
11952 get_attribute_name (t)))
11953 {
11954 ++cp_unevaluated_operand;
11955 tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
11956 --cp_unevaluated_operand;
11957 tree chain = TREE_CHAIN (val);
11958 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11959 tree ctx = copy_list (TREE_VALUE (val));
11960 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
11961 {
11962 enum omp_tss_code set = OMP_TSS_CODE (tss);
11963 tree selectors = NULL_TREE;
11964 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
11965 ts = TREE_CHAIN (ts))
11966 {
11967 tree properties = NULL_TREE;
11968 tree scoreval = NULL_TREE;
11969 /* FIXME: The body of this loop should really be dispatching
11970 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
11971 of having hard-wired knowledge of specific selectors. */
11972 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
11973 && set == OMP_TRAIT_SET_CONSTRUCT)
11974 {
11975 tree clauses = OMP_TS_PROPERTIES (ts);
11976 clauses = tsubst_omp_clauses (clauses,
11977 C_ORT_OMP_DECLARE_SIMD, args,
11978 complain, in_decl);
11979 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11980 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11981 properties = clauses;
11982 }
11983 else
11984 {
11985 tree v = OMP_TS_SCORE (ts);
11986 if (v)
11987 {
11988 v = tsubst_expr (v, args, complain, in_decl);
11989 v = fold_non_dependent_expr (v);
11990 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11991 || TREE_CODE (v) != INTEGER_CST)
11992 {
11993 location_t loc
11994 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
11995 match_loc);
11996 error_at (loc, "score argument must be "
11997 "constant integer expression");
11998 return NULL_TREE;
11999 }
12000 else if (tree_int_cst_sgn (v) < 0)
12001 {
12002 location_t loc
12003 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
12004 match_loc);
12005 error_at (loc, "score argument must be "
12006 "non-negative");
12007 return NULL_TREE;
12008 }
12009 scoreval = v;
12010 }
12011 properties = copy_list (OMP_TS_PROPERTIES (ts));
12012 for (tree p = properties; p; p = TREE_CHAIN (p))
12013 if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
12014 continue;
12015 else if (OMP_TP_VALUE (p))
12016 {
12017 bool allow_string
12018 = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
12019 || set != OMP_TRAIT_SET_USER);
12020 tree v = OMP_TP_VALUE (p);
12021 if (TREE_CODE (v) == STRING_CST && allow_string)
12022 continue;
12023 v = tsubst_expr (v, args, complain, in_decl);
12024 v = fold_non_dependent_expr (v);
12025 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
12026 || !tree_fits_shwi_p (v))
12027 {
12028 location_t loc
12029 = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
12030 match_loc);
12031 if (allow_string)
12032 error_at (loc, "property must be constant "
12033 "integer expression or string "
12034 "literal");
12035 else
12036 error_at (loc, "property must be constant "
12037 "integer expression");
12038 return NULL_TREE;
12039 }
12040 OMP_TP_VALUE (p) = v;
12041 }
12042 }
12043 selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
12044 properties, selectors);
12045 }
12046 OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
12047 }
12048 val = tree_cons (varid, ctx, chain);
12049 }
12050 /* If the first attribute argument is an identifier, don't
12051 pass it through tsubst. Attributes like mode, format,
12052 cleanup and several target specific attributes expect it
12053 unmodified. */
12054 else if (attribute_takes_identifier_p (get_attribute_name (t)))
12055 {
12056 tree chain
12057 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
12058 if (chain != TREE_CHAIN (val))
12059 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
12060 }
12061 else if (PACK_EXPANSION_P (val))
12062 {
12063 /* An attribute pack expansion. */
12064 tree purp = TREE_PURPOSE (t);
12065 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
12066 if (pack == error_mark_node)
12067 return error_mark_node;
12068 int len = TREE_VEC_LENGTH (pack);
12069 tree list = NULL_TREE;
12070 tree *q = &list;
12071 for (int i = 0; i < len; ++i)
12072 {
12073 tree elt = TREE_VEC_ELT (pack, i);
12074 *q = build_tree_list (purp, elt);
12075 q = &TREE_CHAIN (*q);
12076 }
12077 return list;
12078 }
12079 else
12080 val = tsubst_expr (val, args, complain, in_decl);
12081
12082 if (val == error_mark_node)
12083 return error_mark_node;
12084 if (val != TREE_VALUE (t))
12085 return build_tree_list (TREE_PURPOSE (t), val);
12086 return t;
12087 }
12088
12089 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
12090 unchanged or a new TREE_LIST chain. */
12091
12092 static tree
12093 tsubst_attributes (tree attributes, tree args,
12094 tsubst_flags_t complain, tree in_decl)
12095 {
12096 tree last_dep = NULL_TREE;
12097
12098 for (tree t = attributes; t; t = TREE_CHAIN (t))
12099 if (ATTR_IS_DEPENDENT (t))
12100 {
12101 last_dep = t;
12102 attributes = copy_list (attributes);
12103 break;
12104 }
12105
12106 if (last_dep)
12107 for (tree *p = &attributes; *p; )
12108 {
12109 tree t = *p;
12110 if (ATTR_IS_DEPENDENT (t))
12111 {
12112 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
12113 if (subst != t)
12114 {
12115 *p = subst;
12116 while (*p)
12117 p = &TREE_CHAIN (*p);
12118 *p = TREE_CHAIN (t);
12119 continue;
12120 }
12121 }
12122 p = &TREE_CHAIN (*p);
12123 }
12124
12125 return attributes;
12126 }
12127
12128 /* Apply any attributes which had to be deferred until instantiation
12129 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
12130 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
12131 false on error. */
12132
12133 static bool
12134 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
12135 tree args, tsubst_flags_t complain, tree in_decl)
12136 {
12137 tree t;
12138 tree *p;
12139
12140 if (attributes == NULL_TREE)
12141 return true;
12142
12143 if (DECL_P (*decl_p))
12144 {
12145 if (TREE_TYPE (*decl_p) == error_mark_node)
12146 return false;
12147 p = &DECL_ATTRIBUTES (*decl_p);
12148 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
12149 to our attributes parameter. */
12150 gcc_assert (*p == attributes);
12151 }
12152 else
12153 {
12154 p = &TYPE_ATTRIBUTES (*decl_p);
12155 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
12156 lookup_template_class_1, and should be preserved. */
12157 gcc_assert (*p != attributes);
12158 while (*p)
12159 p = &TREE_CHAIN (*p);
12160 }
12161
12162 /* save_template_attributes puts the dependent attributes at the beginning of
12163 the list; find the non-dependent ones. */
12164 for (t = attributes; t; t = TREE_CHAIN (t))
12165 if (!ATTR_IS_DEPENDENT (t))
12166 break;
12167 tree nondep = t;
12168
12169 /* Apply any non-dependent attributes. */
12170 *p = nondep;
12171
12172 if (nondep == attributes)
12173 return true;
12174
12175 /* And then any dependent ones. */
12176 tree late_attrs = NULL_TREE;
12177 tree *q = &late_attrs;
12178 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
12179 {
12180 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
12181 if (*q == error_mark_node)
12182 return false;
12183 if (*q == t)
12184 {
12185 *q = copy_node (t);
12186 TREE_CHAIN (*q) = NULL_TREE;
12187 }
12188 while (*q)
12189 q = &TREE_CHAIN (*q);
12190 }
12191
12192 /* cplus_decl_attributes can add some attributes implicitly. For templates,
12193 those attributes should have been added already when those templates were
12194 parsed, and shouldn't be added based on from which context they are
12195 first time instantiated. */
12196 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
12197 auto o2 = make_temp_override (optimization_current_node,
12198 optimization_default_node);
12199 auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
12200 auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
12201 NULL);
12202 auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
12203
12204 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
12205
12206 return true;
12207 }
12208
12209 /* The template TMPL is being instantiated with the template arguments TARGS.
12210 Perform the access checks that we deferred when parsing the template. */
12211
12212 static void
12213 perform_instantiation_time_access_checks (tree tmpl, tree targs)
12214 {
12215 unsigned i;
12216 deferred_access_check *chk;
12217
12218 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
12219 return;
12220
12221 if (vec<deferred_access_check, va_gc> *access_checks
12222 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
12223 FOR_EACH_VEC_ELT (*access_checks, i, chk)
12224 {
12225 tree decl = chk->decl;
12226 tree diag_decl = chk->diag_decl;
12227 tree type_scope = TREE_TYPE (chk->binfo);
12228
12229 if (uses_template_parms (type_scope))
12230 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
12231
12232 /* Make access check error messages point to the location
12233 of the use of the typedef. */
12234 iloc_sentinel ils (chk->loc);
12235 perform_or_defer_access_check (TYPE_BINFO (type_scope),
12236 decl, diag_decl, tf_warning_or_error);
12237 }
12238 }
12239
12240 tree
12241 instantiate_class_template (tree type)
12242 {
12243 auto_timevar tv (TV_TEMPLATE_INST);
12244
12245 tree templ, args, pattern, t, member;
12246 tree typedecl;
12247 tree pbinfo;
12248 tree base_list;
12249 unsigned int saved_maximum_field_alignment;
12250 tree fn_context;
12251
12252 if (type == error_mark_node)
12253 return error_mark_node;
12254
12255 if (COMPLETE_OR_OPEN_TYPE_P (type)
12256 || uses_template_parms (type))
12257 return type;
12258
12259 /* Figure out which template is being instantiated. */
12260 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
12261 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
12262
12263 /* Mark the type as in the process of being defined. */
12264 TYPE_BEING_DEFINED (type) = 1;
12265
12266 /* We may be in the middle of deferred access check. Disable
12267 it now. */
12268 deferring_access_check_sentinel acs (dk_no_deferred);
12269
12270 /* Determine what specialization of the original template to
12271 instantiate. */
12272 t = most_specialized_partial_spec (type, tf_warning_or_error);
12273 if (t == error_mark_node)
12274 return error_mark_node;
12275 else if (t)
12276 {
12277 /* This TYPE is actually an instantiation of a partial
12278 specialization. We replace the innermost set of ARGS with
12279 the arguments appropriate for substitution. For example,
12280 given:
12281
12282 template <class T> struct S {};
12283 template <class T> struct S<T*> {};
12284
12285 and supposing that we are instantiating S<int*>, ARGS will
12286 presently be {int*} -- but we need {int}. */
12287 pattern = TREE_TYPE (TI_TEMPLATE (t));
12288 args = TI_ARGS (t);
12289 }
12290 else
12291 {
12292 pattern = TREE_TYPE (templ);
12293 args = CLASSTYPE_TI_ARGS (type);
12294 }
12295
12296 /* If the template we're instantiating is incomplete, then clearly
12297 there's nothing we can do. */
12298 if (!COMPLETE_TYPE_P (pattern))
12299 {
12300 /* We can try again later. */
12301 TYPE_BEING_DEFINED (type) = 0;
12302 return type;
12303 }
12304
12305 /* If we've recursively instantiated too many templates, stop. */
12306 if (! push_tinst_level (type))
12307 return type;
12308
12309 int saved_unevaluated_operand = cp_unevaluated_operand;
12310 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12311
12312 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
12313 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12314 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
12315 fn_context = error_mark_node;
12316 if (!fn_context)
12317 push_to_top_level ();
12318 else
12319 {
12320 cp_unevaluated_operand = 0;
12321 c_inhibit_evaluation_warnings = 0;
12322 }
12323
12324 mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
12325
12326 /* Use #pragma pack from the template context. */
12327 saved_maximum_field_alignment = maximum_field_alignment;
12328 maximum_field_alignment = TYPE_PRECISION (pattern);
12329
12330 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
12331
12332 /* Set the input location to the most specialized template definition.
12333 This is needed if tsubsting causes an error. */
12334 typedecl = TYPE_MAIN_DECL (pattern);
12335 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
12336 DECL_SOURCE_LOCATION (typedecl);
12337
12338 set_instantiating_module (TYPE_NAME (type));
12339
12340 TYPE_PACKED (type) = TYPE_PACKED (pattern);
12341 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
12342 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
12343 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
12344 if (ANON_AGGR_TYPE_P (pattern))
12345 SET_ANON_AGGR_TYPE_P (type);
12346 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
12347 {
12348 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
12349 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
12350 /* Adjust visibility for template arguments. */
12351 determine_visibility (TYPE_MAIN_DECL (type));
12352 }
12353 if (CLASS_TYPE_P (type))
12354 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
12355
12356 pbinfo = TYPE_BINFO (pattern);
12357
12358 /* We should never instantiate a nested class before its enclosing
12359 class; we need to look up the nested class by name before we can
12360 instantiate it, and that lookup should instantiate the enclosing
12361 class. */
12362 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
12363 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
12364
12365 base_list = NULL_TREE;
12366 /* Defer access checking while we substitute into the types named in
12367 the base-clause. */
12368 push_deferring_access_checks (dk_deferred);
12369 if (BINFO_N_BASE_BINFOS (pbinfo))
12370 {
12371 tree pbase_binfo;
12372 int i;
12373
12374 /* Substitute into each of the bases to determine the actual
12375 basetypes. */
12376 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
12377 {
12378 tree base;
12379 tree access = BINFO_BASE_ACCESS (pbinfo, i);
12380 tree expanded_bases = NULL_TREE;
12381 int idx, len = 1;
12382
12383 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
12384 {
12385 expanded_bases =
12386 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
12387 args, tf_error, NULL_TREE);
12388 if (expanded_bases == error_mark_node)
12389 continue;
12390
12391 len = TREE_VEC_LENGTH (expanded_bases);
12392 }
12393
12394 for (idx = 0; idx < len; idx++)
12395 {
12396 if (expanded_bases)
12397 /* Extract the already-expanded base class. */
12398 base = TREE_VEC_ELT (expanded_bases, idx);
12399 else
12400 /* Substitute to figure out the base class. */
12401 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
12402 NULL_TREE);
12403
12404 if (base == error_mark_node)
12405 continue;
12406
12407 base_list = tree_cons (access, base, base_list);
12408 if (BINFO_VIRTUAL_P (pbase_binfo))
12409 TREE_TYPE (base_list) = integer_type_node;
12410 }
12411 }
12412
12413 /* The list is now in reverse order; correct that. */
12414 base_list = nreverse (base_list);
12415 }
12416 /* Now call xref_basetypes to set up all the base-class
12417 information. */
12418 xref_basetypes (type, base_list);
12419
12420 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
12421 (int) ATTR_FLAG_TYPE_IN_PLACE,
12422 args, tf_error, NULL_TREE);
12423 fixup_attribute_variants (type);
12424
12425 /* Now that our base classes are set up, enter the scope of the
12426 class, so that name lookups into base classes, etc. will work
12427 correctly. This is precisely analogous to what we do in
12428 begin_class_definition when defining an ordinary non-template
12429 class, except we also need to push the enclosing classes. */
12430 push_nested_class (type);
12431
12432 /* Now check accessibility of the types named in its base-clause,
12433 relative to the scope of the class. */
12434 pop_to_parent_deferring_access_checks ();
12435
12436 /* A vector to hold members marked with attribute used. */
12437 auto_vec<tree> used;
12438
12439 /* Now members are processed in the order of declaration. */
12440 for (member = CLASSTYPE_DECL_LIST (pattern);
12441 member; member = TREE_CHAIN (member))
12442 {
12443 tree t = TREE_VALUE (member);
12444
12445 if (TREE_PURPOSE (member))
12446 {
12447 if (TYPE_P (t))
12448 {
12449 if (LAMBDA_TYPE_P (t))
12450 /* A closure type for a lambda in an NSDMI or default argument.
12451 Ignore it; it will be regenerated when needed. */
12452 continue;
12453
12454 /* If the member is a class template, we've
12455 already substituted its type. */
12456 if (CLASS_TYPE_P (t)
12457 && CLASSTYPE_IS_TEMPLATE (t))
12458 continue;
12459
12460 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
12461 if (newtag == error_mark_node)
12462 continue;
12463
12464 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
12465 {
12466 tree name = TYPE_IDENTIFIER (t);
12467
12468 /* Now, install the tag. We don't use pushtag
12469 because that does too much work -- creating an
12470 implicit typedef, which we've already done. */
12471 set_identifier_type_value (name, TYPE_NAME (newtag));
12472 maybe_add_class_template_decl_list (type, newtag, false);
12473 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12474 determine_visibility (TYPE_NAME (newtag));
12475 }
12476 }
12477 else if (DECL_DECLARES_FUNCTION_P (t))
12478 {
12479 tree r;
12480
12481 if (TREE_CODE (t) == TEMPLATE_DECL)
12482 ++processing_template_decl;
12483 r = tsubst (t, args, tf_error, NULL_TREE);
12484 if (TREE_CODE (t) == TEMPLATE_DECL)
12485 --processing_template_decl;
12486
12487 set_current_access_from_decl (r);
12488 finish_member_declaration (r);
12489 /* Instantiate members marked with attribute used. */
12490 if (r != error_mark_node && DECL_PRESERVE_P (r))
12491 used.safe_push (r);
12492 if (TREE_CODE (r) == FUNCTION_DECL
12493 && DECL_OMP_DECLARE_REDUCTION_P (r))
12494 cp_check_omp_declare_reduction (r);
12495 }
12496 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12497 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12498 /* A closure type for a lambda in an NSDMI or default argument.
12499 Ignore it; it will be regenerated when needed. */;
12500 else
12501 {
12502 /* Build new TYPE_FIELDS. */
12503 if (TREE_CODE (t) == STATIC_ASSERT)
12504 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
12505 else if (TREE_CODE (t) != CONST_DECL)
12506 {
12507 tree r;
12508 tree vec = NULL_TREE;
12509 int len = 1;
12510
12511 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12512 /* The file and line for this declaration, to
12513 assist in error message reporting. Since we
12514 called push_tinst_level above, we don't need to
12515 restore these. */
12516 input_location = DECL_SOURCE_LOCATION (t);
12517
12518 if (TREE_CODE (t) == TEMPLATE_DECL)
12519 ++processing_template_decl;
12520 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12521 if (TREE_CODE (t) == TEMPLATE_DECL)
12522 --processing_template_decl;
12523
12524 if (TREE_CODE (r) == TREE_VEC)
12525 {
12526 /* A capture pack became multiple fields. */
12527 vec = r;
12528 len = TREE_VEC_LENGTH (vec);
12529 }
12530
12531 for (int i = 0; i < len; ++i)
12532 {
12533 if (vec)
12534 r = TREE_VEC_ELT (vec, i);
12535 if (VAR_P (r))
12536 {
12537 /* In [temp.inst]:
12538
12539 [t]he initialization (and any associated
12540 side-effects) of a static data member does
12541 not occur unless the static data member is
12542 itself used in a way that requires the
12543 definition of the static data member to
12544 exist.
12545
12546 Therefore, we do not substitute into the
12547 initialized for the static data member here. */
12548 finish_static_data_member_decl
12549 (r,
12550 /*init=*/NULL_TREE,
12551 /*init_const_expr_p=*/false,
12552 /*asmspec_tree=*/NULL_TREE,
12553 /*flags=*/0);
12554 /* Instantiate members marked with attribute used. */
12555 if (r != error_mark_node && DECL_PRESERVE_P (r))
12556 used.safe_push (r);
12557 }
12558 else if (TREE_CODE (r) == FIELD_DECL)
12559 {
12560 /* Determine whether R has a valid type and can be
12561 completed later. If R is invalid, then its type
12562 is replaced by error_mark_node. */
12563 tree rtype = TREE_TYPE (r);
12564 if (can_complete_type_without_circularity (rtype))
12565 complete_type (rtype);
12566
12567 if (!complete_or_array_type_p (rtype))
12568 {
12569 /* If R's type couldn't be completed and
12570 it isn't a flexible array member (whose
12571 type is incomplete by definition) give
12572 an error. */
12573 cxx_incomplete_type_error (r, rtype);
12574 TREE_TYPE (r) = error_mark_node;
12575 }
12576 else if (TREE_CODE (rtype) == ARRAY_TYPE
12577 && TYPE_DOMAIN (rtype) == NULL_TREE
12578 && (TREE_CODE (type) == UNION_TYPE
12579 || TREE_CODE (type) == QUAL_UNION_TYPE))
12580 {
12581 error ("flexible array member %qD in union", r);
12582 TREE_TYPE (r) = error_mark_node;
12583 }
12584 else if (!verify_type_context (input_location,
12585 TCTX_FIELD, rtype))
12586 TREE_TYPE (r) = error_mark_node;
12587 }
12588
12589 /* If it is a TYPE_DECL for a class-scoped
12590 ENUMERAL_TYPE, such a thing will already have
12591 been added to the field list by tsubst_enum
12592 in finish_member_declaration case above. */
12593 if (!(TREE_CODE (r) == TYPE_DECL
12594 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12595 && DECL_ARTIFICIAL (r)))
12596 {
12597 set_current_access_from_decl (r);
12598 finish_member_declaration (r);
12599 }
12600 }
12601 }
12602 }
12603 }
12604 else
12605 {
12606 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12607 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12608 {
12609 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12610
12611 tree friend_type = t;
12612 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12613 {
12614 /* template <class T> friend class C; */
12615 friend_type = tsubst_friend_class (friend_type, args);
12616 }
12617 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12618 {
12619 /* template <class T> friend class C::D; */
12620 friend_type = tsubst (friend_type, args,
12621 tf_warning_or_error, NULL_TREE);
12622 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12623 friend_type = TREE_TYPE (friend_type);
12624 }
12625 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12626 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12627 {
12628 /* This could be either
12629
12630 friend class T::C;
12631
12632 when dependent_type_p is false or
12633
12634 template <class U> friend class T::C;
12635
12636 otherwise. */
12637 /* Bump processing_template_decl in case this is something like
12638 template <class T> friend struct A<T>::B. */
12639 ++processing_template_decl;
12640 friend_type = tsubst (friend_type, args,
12641 tf_warning_or_error, NULL_TREE);
12642 --processing_template_decl;
12643 }
12644 else if (uses_template_parms (friend_type))
12645 /* friend class C<T>; */
12646 friend_type = tsubst (friend_type, args,
12647 tf_warning_or_error, NULL_TREE);
12648
12649 /* Otherwise it's
12650
12651 friend class C;
12652
12653 where C is already declared or
12654
12655 friend class C<int>;
12656
12657 We don't have to do anything in these cases. */
12658
12659 if (friend_type != error_mark_node)
12660 make_friend_class (type, friend_type, /*complain=*/false);
12661 }
12662 else
12663 {
12664 /* Build new DECL_FRIENDLIST. */
12665 tree r;
12666
12667 /* The file and line for this declaration, to
12668 assist in error message reporting. Since we
12669 called push_tinst_level above, we don't need to
12670 restore these. */
12671 input_location = DECL_SOURCE_LOCATION (t);
12672
12673 if (TREE_CODE (t) == TEMPLATE_DECL)
12674 {
12675 ++processing_template_decl;
12676 push_deferring_access_checks (dk_no_check);
12677 }
12678
12679 r = tsubst_friend_function (t, args);
12680 add_friend (type, r, /*complain=*/false);
12681 if (TREE_CODE (t) == TEMPLATE_DECL)
12682 {
12683 pop_deferring_access_checks ();
12684 --processing_template_decl;
12685 }
12686 }
12687 }
12688 }
12689
12690 if (fn_context)
12691 {
12692 /* Restore these before substituting into the lambda capture
12693 initializers. */
12694 cp_unevaluated_operand = saved_unevaluated_operand;
12695 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12696 }
12697
12698 /* Set the file and line number information to whatever is given for
12699 the class itself. This puts error messages involving generated
12700 implicit functions at a predictable point, and the same point
12701 that would be used for non-template classes. */
12702 input_location = DECL_SOURCE_LOCATION (typedecl);
12703
12704 unreverse_member_declarations (type);
12705 finish_struct_1 (type);
12706 TYPE_BEING_DEFINED (type) = 0;
12707
12708 /* Remember if instantiating this class ran into errors, so we can avoid
12709 instantiating member functions in limit_bad_template_recursion. We set
12710 this flag even if the problem was in another instantiation triggered by
12711 this one, as that will likely also cause trouble for member functions. */
12712 if (errorcount + sorrycount > current_tinst_level->errors)
12713 CLASSTYPE_ERRONEOUS (type) = true;
12714
12715 /* We don't instantiate default arguments for member functions. 14.7.1:
12716
12717 The implicit instantiation of a class template specialization causes
12718 the implicit instantiation of the declarations, but not of the
12719 definitions or default arguments, of the class member functions,
12720 member classes, static data members and member templates.... */
12721
12722 perform_instantiation_time_access_checks (pattern, args);
12723 perform_deferred_access_checks (tf_warning_or_error);
12724
12725 /* Now that we've gone through all the members, instantiate those
12726 marked with attribute used. We must do this in the context of
12727 the class -- not the context we pushed from, as that might be
12728 inside a template and change the behaviour of mark_used. */
12729 for (tree x : used)
12730 mark_used (x);
12731
12732 pop_nested_class ();
12733 maximum_field_alignment = saved_maximum_field_alignment;
12734 if (!fn_context)
12735 pop_from_top_level ();
12736 pop_tinst_level ();
12737
12738 /* The vtable for a template class can be emitted in any translation
12739 unit in which the class is instantiated. When there is no key
12740 method, however, finish_struct_1 will already have added TYPE to
12741 the keyed_classes. */
12742 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12743 vec_safe_push (keyed_classes, type);
12744
12745 return type;
12746 }
12747
12748 tree
12749 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12750 {
12751 tree r;
12752
12753 if (!t)
12754 r = t;
12755 else if (TYPE_P (t))
12756 r = tsubst (t, args, complain, in_decl);
12757 else
12758 {
12759 if (!(complain & tf_warning))
12760 ++c_inhibit_evaluation_warnings;
12761 r = tsubst_expr (t, args, complain, in_decl);
12762 if (!(complain & tf_warning))
12763 --c_inhibit_evaluation_warnings;
12764 }
12765
12766 return r;
12767 }
12768
12769 /* Given a function parameter pack TMPL_PARM and some function parameters
12770 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12771 and set *SPEC_P to point at the next point in the list. */
12772
12773 tree
12774 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12775 {
12776 /* Collect all of the extra "packed" parameters into an
12777 argument pack. */
12778 tree argpack;
12779 tree spec_parm = *spec_p;
12780 int len;
12781
12782 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12783 if (tmpl_parm
12784 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12785 break;
12786
12787 spec_parm = *spec_p;
12788 if (len == 1 && DECL_PACK_P (spec_parm))
12789 {
12790 /* The instantiation is still a parameter pack; don't wrap it in a
12791 NONTYPE_ARGUMENT_PACK. */
12792 argpack = spec_parm;
12793 spec_parm = DECL_CHAIN (spec_parm);
12794 }
12795 else
12796 {
12797 /* Fill in PARMVEC with all of the parameters. */
12798 tree parmvec = make_tree_vec (len);
12799 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12800 for (int i = 0; i < len; i++)
12801 {
12802 tree elt = spec_parm;
12803 if (DECL_PACK_P (elt))
12804 elt = make_pack_expansion (elt);
12805 TREE_VEC_ELT (parmvec, i) = elt;
12806 spec_parm = DECL_CHAIN (spec_parm);
12807 }
12808
12809 /* Build the argument packs. */
12810 ARGUMENT_PACK_ARGS (argpack) = parmvec;
12811 }
12812 *spec_p = spec_parm;
12813
12814 return argpack;
12815 }
12816
12817 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12818 NONTYPE_ARGUMENT_PACK. */
12819
12820 static tree
12821 make_fnparm_pack (tree spec_parm)
12822 {
12823 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12824 }
12825
12826 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12827 pack expansion with no extra args, 2 if it has extra args, or 0
12828 if it is not a pack expansion. */
12829
12830 static int
12831 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12832 {
12833 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12834 /* We're being called before this happens in tsubst_pack_expansion. */
12835 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12836 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12837 if (i >= TREE_VEC_LENGTH (vec))
12838 return 0;
12839 tree elt = TREE_VEC_ELT (vec, i);
12840 if (DECL_P (elt))
12841 /* A decl pack is itself an expansion. */
12842 elt = TREE_TYPE (elt);
12843 if (!PACK_EXPANSION_P (elt))
12844 return 0;
12845 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12846 return 2;
12847 return 1;
12848 }
12849
12850
12851 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12852
12853 static tree
12854 make_argument_pack_select (tree arg_pack, unsigned index)
12855 {
12856 tree aps = make_node (ARGUMENT_PACK_SELECT);
12857
12858 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12859 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12860
12861 return aps;
12862 }
12863
12864 /* This is a subroutine of tsubst_pack_expansion.
12865
12866 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12867 mechanism to store the (non complete list of) arguments of the
12868 substitution and return a non substituted pack expansion, in order
12869 to wait for when we have enough arguments to really perform the
12870 substitution. */
12871
12872 static bool
12873 use_pack_expansion_extra_args_p (tree t,
12874 tree parm_packs,
12875 int arg_pack_len,
12876 bool has_empty_arg)
12877 {
12878 if (has_empty_arg
12879 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
12880 return true;
12881
12882 /* If one pack has an expansion and another pack has a normal
12883 argument or if one pack has an empty argument and an another
12884 one hasn't then tsubst_pack_expansion cannot perform the
12885 substitution and need to fall back on the
12886 PACK_EXPANSION_EXTRA mechanism. */
12887 if (parm_packs == NULL_TREE)
12888 return false;
12889 else if (has_empty_arg)
12890 {
12891 /* If all the actual packs are pack expansions, we can still
12892 subsitute directly. */
12893 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12894 {
12895 tree a = TREE_VALUE (p);
12896 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12897 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12898 a = ARGUMENT_PACK_ARGS (a);
12899 if (TREE_VEC_LENGTH (a) == 1)
12900 a = TREE_VEC_ELT (a, 0);
12901 if (PACK_EXPANSION_P (a))
12902 continue;
12903 return true;
12904 }
12905 return false;
12906 }
12907
12908 for (int i = 0 ; i < arg_pack_len; ++i)
12909 {
12910 bool has_expansion_arg = false;
12911 bool has_non_expansion_arg = false;
12912 for (tree parm_pack = parm_packs;
12913 parm_pack;
12914 parm_pack = TREE_CHAIN (parm_pack))
12915 {
12916 tree arg = TREE_VALUE (parm_pack);
12917
12918 int exp = argument_pack_element_is_expansion_p (arg, i);
12919 if (exp == 2)
12920 /* We can't substitute a pack expansion with extra args into
12921 our pattern. */
12922 return true;
12923 else if (exp)
12924 has_expansion_arg = true;
12925 else
12926 has_non_expansion_arg = true;
12927 }
12928
12929 if (has_expansion_arg && has_non_expansion_arg)
12930 {
12931 gcc_checking_assert (false);
12932 return true;
12933 }
12934 }
12935 return false;
12936 }
12937
12938 /* [temp.variadic]/6 says that:
12939
12940 The instantiation of a pack expansion [...]
12941 produces a list E1,E2, ..., En, where N is the number of elements
12942 in the pack expansion parameters.
12943
12944 This subroutine of tsubst_pack_expansion produces one of these Ei.
12945
12946 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12947 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12948 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12949 INDEX is the index 'i' of the element Ei to produce. ARGS,
12950 COMPLAIN, and IN_DECL are the same parameters as for the
12951 tsubst_pack_expansion function.
12952
12953 The function returns the resulting Ei upon successful completion,
12954 or error_mark_node.
12955
12956 Note that this function possibly modifies the ARGS parameter, so
12957 it's the responsibility of the caller to restore it. */
12958
12959 static tree
12960 gen_elem_of_pack_expansion_instantiation (tree pattern,
12961 tree parm_packs,
12962 unsigned index,
12963 tree args /* This parm gets
12964 modified. */,
12965 tsubst_flags_t complain,
12966 tree in_decl)
12967 {
12968 tree t;
12969 bool ith_elem_is_expansion = false;
12970
12971 /* For each parameter pack, change the substitution of the parameter
12972 pack to the ith argument in its argument pack, then expand the
12973 pattern. */
12974 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12975 {
12976 tree parm = TREE_PURPOSE (pack);
12977 tree arg_pack = TREE_VALUE (pack);
12978 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12979
12980 ith_elem_is_expansion |=
12981 argument_pack_element_is_expansion_p (arg_pack, index);
12982
12983 /* Select the Ith argument from the pack. */
12984 if (TREE_CODE (parm) == PARM_DECL
12985 || VAR_P (parm)
12986 || TREE_CODE (parm) == FIELD_DECL)
12987 {
12988 if (index == 0)
12989 {
12990 aps = make_argument_pack_select (arg_pack, index);
12991 if (!mark_used (parm, complain) && !(complain & tf_error))
12992 return error_mark_node;
12993 register_local_specialization (aps, parm);
12994 }
12995 else
12996 aps = retrieve_local_specialization (parm);
12997 }
12998 else
12999 {
13000 int idx, level;
13001 template_parm_level_and_index (parm, &level, &idx);
13002
13003 if (index == 0)
13004 {
13005 aps = make_argument_pack_select (arg_pack, index);
13006 /* Update the corresponding argument. */
13007 TMPL_ARG (args, level, idx) = aps;
13008 }
13009 else
13010 /* Re-use the ARGUMENT_PACK_SELECT. */
13011 aps = TMPL_ARG (args, level, idx);
13012 }
13013 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
13014 }
13015
13016 /* Substitute into the PATTERN with the (possibly altered)
13017 arguments. */
13018 if (pattern == in_decl)
13019 /* Expanding a fixed parameter pack from
13020 coerce_template_parameter_pack. */
13021 t = tsubst_decl (pattern, args, complain);
13022 else if (pattern == error_mark_node)
13023 t = error_mark_node;
13024 else if (!TYPE_P (pattern))
13025 t = tsubst_expr (pattern, args, complain, in_decl);
13026 else
13027 {
13028 t = tsubst (pattern, args, complain, in_decl);
13029 if (is_auto (t) && !ith_elem_is_expansion)
13030 /* When expanding the fake auto... pack expansion from add_capture, we
13031 need to mark that the expansion is no longer a pack. */
13032 TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
13033 }
13034
13035 /* If the Ith argument pack element is a pack expansion, then
13036 the Ith element resulting from the substituting is going to
13037 be a pack expansion as well. */
13038 if (ith_elem_is_expansion)
13039 t = make_pack_expansion (t, complain);
13040
13041 return t;
13042 }
13043
13044 /* When the unexpanded parameter pack in a fold expression expands to an empty
13045 sequence, the value of the expression is as follows; the program is
13046 ill-formed if the operator is not listed in this table.
13047
13048 && true
13049 || false
13050 , void() */
13051
13052 tree
13053 expand_empty_fold (tree t, tsubst_flags_t complain)
13054 {
13055 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
13056 if (!FOLD_EXPR_MODIFY_P (t))
13057 switch (code)
13058 {
13059 case TRUTH_ANDIF_EXPR:
13060 return boolean_true_node;
13061 case TRUTH_ORIF_EXPR:
13062 return boolean_false_node;
13063 case COMPOUND_EXPR:
13064 return void_node;
13065 default:
13066 break;
13067 }
13068
13069 if (complain & tf_error)
13070 error_at (location_of (t),
13071 "fold of empty expansion over %O", code);
13072 return error_mark_node;
13073 }
13074
13075 /* Given a fold-expression T and a current LEFT and RIGHT operand,
13076 form an expression that combines the two terms using the
13077 operator of T. */
13078
13079 static tree
13080 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
13081 {
13082 tree_code code = FOLD_EXPR_OP (t);
13083
13084 tree lookups = templated_operator_saved_lookups (t);
13085
13086 // Handle compound assignment operators.
13087 if (FOLD_EXPR_MODIFY_P (t))
13088 return build_x_modify_expr (input_location, left, code, right,
13089 lookups, complain);
13090
13091 warning_sentinel s(warn_parentheses);
13092 switch (code)
13093 {
13094 case COMPOUND_EXPR:
13095 return build_x_compound_expr (input_location, left, right,
13096 lookups, complain);
13097 default:
13098 return build_x_binary_op (input_location, code,
13099 left, TREE_CODE (left),
13100 right, TREE_CODE (right),
13101 lookups, /*overload=*/NULL,
13102 complain);
13103 }
13104 }
13105
13106 /* Substitute ARGS into the pack of a fold expression T. */
13107
13108 static inline tree
13109 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13110 {
13111 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
13112 }
13113
13114 /* Substitute ARGS into the pack of a fold expression T. */
13115
13116 static inline tree
13117 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13118 {
13119 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
13120 }
13121
13122 /* Expand a PACK of arguments into a grouped as left fold.
13123 Given a pack containing elements A0, A1, ..., An and an
13124 operator @, this builds the expression:
13125
13126 ((A0 @ A1) @ A2) ... @ An
13127
13128 Note that PACK must not be empty.
13129
13130 The operator is defined by the original fold expression T. */
13131
13132 static tree
13133 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
13134 {
13135 tree left = TREE_VEC_ELT (pack, 0);
13136 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
13137 {
13138 tree right = TREE_VEC_ELT (pack, i);
13139 left = fold_expression (t, left, right, complain);
13140 }
13141 return left;
13142 }
13143
13144 /* Substitute into a unary left fold expression. */
13145
13146 static tree
13147 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
13148 tree in_decl)
13149 {
13150 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13151 if (pack == error_mark_node)
13152 return error_mark_node;
13153 if (PACK_EXPANSION_P (pack))
13154 {
13155 tree r = copy_node (t);
13156 FOLD_EXPR_PACK (r) = pack;
13157 return r;
13158 }
13159 if (TREE_VEC_LENGTH (pack) == 0)
13160 return expand_empty_fold (t, complain);
13161 else
13162 return expand_left_fold (t, pack, complain);
13163 }
13164
13165 /* Substitute into a binary left fold expression.
13166
13167 Do ths by building a single (non-empty) vector of argumnts and
13168 building the expression from those elements. */
13169
13170 static tree
13171 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
13172 tree in_decl)
13173 {
13174 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13175 if (pack == error_mark_node)
13176 return error_mark_node;
13177 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13178 if (init == error_mark_node)
13179 return error_mark_node;
13180
13181 if (PACK_EXPANSION_P (pack))
13182 {
13183 tree r = copy_node (t);
13184 FOLD_EXPR_PACK (r) = pack;
13185 FOLD_EXPR_INIT (r) = init;
13186 return r;
13187 }
13188
13189 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
13190 TREE_VEC_ELT (vec, 0) = init;
13191 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
13192 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
13193
13194 return expand_left_fold (t, vec, complain);
13195 }
13196
13197 /* Expand a PACK of arguments into a grouped as right fold.
13198 Given a pack containing elementns A0, A1, ..., and an
13199 operator @, this builds the expression:
13200
13201 A0@ ... (An-2 @ (An-1 @ An))
13202
13203 Note that PACK must not be empty.
13204
13205 The operator is defined by the original fold expression T. */
13206
13207 tree
13208 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
13209 {
13210 // Build the expression.
13211 int n = TREE_VEC_LENGTH (pack);
13212 tree right = TREE_VEC_ELT (pack, n - 1);
13213 for (--n; n != 0; --n)
13214 {
13215 tree left = TREE_VEC_ELT (pack, n - 1);
13216 right = fold_expression (t, left, right, complain);
13217 }
13218 return right;
13219 }
13220
13221 /* Substitute into a unary right fold expression. */
13222
13223 static tree
13224 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
13225 tree in_decl)
13226 {
13227 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13228 if (pack == error_mark_node)
13229 return error_mark_node;
13230 if (PACK_EXPANSION_P (pack))
13231 {
13232 tree r = copy_node (t);
13233 FOLD_EXPR_PACK (r) = pack;
13234 return r;
13235 }
13236 if (TREE_VEC_LENGTH (pack) == 0)
13237 return expand_empty_fold (t, complain);
13238 else
13239 return expand_right_fold (t, pack, complain);
13240 }
13241
13242 /* Substitute into a binary right fold expression.
13243
13244 Do ths by building a single (non-empty) vector of arguments and
13245 building the expression from those elements. */
13246
13247 static tree
13248 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
13249 tree in_decl)
13250 {
13251 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13252 if (pack == error_mark_node)
13253 return error_mark_node;
13254 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13255 if (init == error_mark_node)
13256 return error_mark_node;
13257
13258 if (PACK_EXPANSION_P (pack))
13259 {
13260 tree r = copy_node (t);
13261 FOLD_EXPR_PACK (r) = pack;
13262 FOLD_EXPR_INIT (r) = init;
13263 return r;
13264 }
13265
13266 int n = TREE_VEC_LENGTH (pack);
13267 tree vec = make_tree_vec (n + 1);
13268 for (int i = 0; i < n; ++i)
13269 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
13270 TREE_VEC_ELT (vec, n) = init;
13271
13272 return expand_right_fold (t, vec, complain);
13273 }
13274
13275 /* Walk through the pattern of a pack expansion, adding everything in
13276 local_specializations to a list. */
13277
13278 class el_data
13279 {
13280 public:
13281 /* Set of variables declared within the pattern. */
13282 hash_set<tree> internal;
13283 /* Set of AST nodes that have been visited by the traversal. */
13284 hash_set<tree> visited;
13285 /* List of local_specializations used within the pattern. */
13286 tree extra;
13287 tsubst_flags_t complain;
13288 /* True iff we don't want to walk into unevaluated contexts. */
13289 bool skip_unevaluated_operands = false;
13290 /* The unevaluated contexts that we avoided walking. */
13291 auto_vec<tree> skipped_trees;
13292
13293 el_data (tsubst_flags_t c)
13294 : extra (NULL_TREE), complain (c) {}
13295 };
13296 static tree
13297 extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
13298 {
13299 el_data &data = *reinterpret_cast<el_data*>(data_);
13300 tree *extra = &data.extra;
13301 tsubst_flags_t complain = data.complain;
13302
13303 if (data.skip_unevaluated_operands
13304 && unevaluated_p (TREE_CODE (*tp)))
13305 {
13306 data.skipped_trees.safe_push (*tp);
13307 *walk_subtrees = 0;
13308 return NULL_TREE;
13309 }
13310
13311 if (TYPE_P (*tp) && typedef_variant_p (*tp))
13312 /* Remember local typedefs (85214). */
13313 tp = &TYPE_NAME (*tp);
13314
13315 if (has_extra_args_mechanism_p (*tp))
13316 /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
13317 potentially see a previously captured local in an evaluated context
13318 that's really only used in an unevaluated context (PR114303). This
13319 means callers of build_extra_args need to clear *_EXTRA_ARGS of the
13320 outermost tree. Nested *_EXTRA_ARGS should naturally be empty since
13321 the outermost (extra-args) tree will intercept any substitution before
13322 a nested tree can. */
13323 gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE);
13324
13325 if (TREE_CODE (*tp) == DECL_EXPR)
13326 {
13327 tree decl = DECL_EXPR_DECL (*tp);
13328 data.internal.add (decl);
13329 if (VAR_P (decl)
13330 && DECL_DECOMPOSITION_P (decl)
13331 && TREE_TYPE (decl) != error_mark_node)
13332 {
13333 gcc_assert (DECL_NAME (decl) == NULL_TREE);
13334 for (tree decl2 = DECL_CHAIN (decl);
13335 decl2
13336 && VAR_P (decl2)
13337 && DECL_DECOMPOSITION_P (decl2)
13338 && DECL_NAME (decl2)
13339 && TREE_TYPE (decl2) != error_mark_node;
13340 decl2 = DECL_CHAIN (decl2))
13341 {
13342 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
13343 data.internal.add (decl2);
13344 }
13345 }
13346 }
13347 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
13348 {
13349 /* Since we defer implicit capture, look in the parms and body. */
13350 tree fn = lambda_function (*tp);
13351 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
13352 &data.visited);
13353 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
13354 &data.visited);
13355 }
13356 else if (tree spec = retrieve_local_specialization (*tp))
13357 {
13358 if (data.internal.contains (*tp))
13359 /* Don't mess with variables declared within the pattern. */
13360 return NULL_TREE;
13361 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13362 {
13363 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13364 tree args = ARGUMENT_PACK_ARGS (spec);
13365 if (TREE_VEC_LENGTH (args) == 1)
13366 {
13367 tree elt = TREE_VEC_ELT (args, 0);
13368 if (PACK_EXPANSION_P (elt))
13369 elt = PACK_EXPANSION_PATTERN (elt);
13370 if (DECL_PACK_P (elt))
13371 spec = elt;
13372 }
13373 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13374 {
13375 /* Handle lambda capture here, since we aren't doing any
13376 substitution now, and so tsubst_copy won't call
13377 process_outer_var_ref. */
13378 tree args = ARGUMENT_PACK_ARGS (spec);
13379 int len = TREE_VEC_LENGTH (args);
13380 for (int i = 0; i < len; ++i)
13381 {
13382 tree arg = TREE_VEC_ELT (args, i);
13383 tree carg = arg;
13384 if (outer_automatic_var_p (arg))
13385 carg = process_outer_var_ref (arg, complain);
13386 if (carg != arg)
13387 {
13388 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13389 proxies. */
13390 if (i == 0)
13391 {
13392 spec = copy_node (spec);
13393 args = copy_node (args);
13394 ARGUMENT_PACK_ARGS (spec) = args;
13395 register_local_specialization (spec, *tp);
13396 }
13397 TREE_VEC_ELT (args, i) = carg;
13398 }
13399 }
13400 }
13401 }
13402 if (outer_automatic_var_p (spec))
13403 spec = process_outer_var_ref (spec, complain);
13404 *extra = tree_cons (*tp, spec, *extra);
13405 }
13406 return NULL_TREE;
13407 }
13408 static tree
13409 extract_local_specs (tree pattern, tsubst_flags_t complain)
13410 {
13411 el_data data (complain);
13412 /* Walk the pattern twice, ignoring unevaluated operands the first time
13413 around, so that if a local specialization appears in both an evaluated
13414 and unevaluated context we prefer to process it in the evaluated context
13415 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13416 context). */
13417 data.skip_unevaluated_operands = true;
13418 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13419 /* Now walk the unevaluated contexts we skipped the first time around. */
13420 data.skip_unevaluated_operands = false;
13421 for (tree t : data.skipped_trees)
13422 {
13423 data.visited.remove (t);
13424 cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
13425 }
13426 return data.extra;
13427 }
13428
13429 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13430 for use in PACK_EXPANSION_EXTRA_ARGS. */
13431
13432 tree
13433 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
13434 {
13435 /* Make a copy of the extra arguments so that they won't get changed
13436 out from under us. */
13437 tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
13438 if (local_specializations)
13439 if (tree locals = extract_local_specs (pattern, complain))
13440 extra = tree_cons (NULL_TREE, extra, locals);
13441 return extra;
13442 }
13443
13444 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13445 normal template args to ARGS. */
13446
13447 tree
13448 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
13449 {
13450 if (extra && TREE_CODE (extra) == TREE_LIST)
13451 {
13452 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
13453 {
13454 /* The partial instantiation involved local declarations collected in
13455 extract_local_specs; map from the general template to our local
13456 context. */
13457 tree gen = TREE_PURPOSE (elt);
13458 tree inst = TREE_VALUE (elt);
13459 if (DECL_P (inst))
13460 if (tree local = retrieve_local_specialization (inst))
13461 inst = local;
13462 /* else inst is already a full instantiation of the pack. */
13463 register_local_specialization (inst, gen);
13464 }
13465 gcc_assert (!TREE_PURPOSE (extra));
13466 extra = TREE_VALUE (extra);
13467 }
13468 if (uses_template_parms (extra))
13469 {
13470 /* This can happen after dependent substitution into a
13471 requires-expr or a lambda that uses constexpr if. */
13472 extra = tsubst_template_args (extra, args, complain, in_decl);
13473 args = add_outermost_template_args (args, extra);
13474 }
13475 else
13476 args = add_to_template_args (extra, args);
13477 return args;
13478 }
13479
13480 /* Substitute ARGS into T, which is an pack expansion
13481 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13482 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13483 (if only a partial substitution could be performed) or
13484 ERROR_MARK_NODE if there was an error. */
13485 tree
13486 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13487 tree in_decl)
13488 {
13489 tree pattern;
13490 tree pack, packs = NULL_TREE;
13491 bool unsubstituted_packs = false;
13492 int i, len = -1;
13493 tree result;
13494 bool need_local_specializations = false;
13495 int levels;
13496
13497 gcc_assert (PACK_EXPANSION_P (t));
13498 pattern = PACK_EXPANSION_PATTERN (t);
13499
13500 /* Add in any args remembered from an earlier partial instantiation. */
13501 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13502
13503 levels = TMPL_ARGS_DEPTH (args);
13504
13505 /* Determine the argument packs that will instantiate the parameter
13506 packs used in the expansion expression. While we're at it,
13507 compute the number of arguments to be expanded and make sure it
13508 is consistent. */
13509 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13510 pack = TREE_CHAIN (pack))
13511 {
13512 tree parm_pack = TREE_VALUE (pack);
13513 tree arg_pack = NULL_TREE;
13514 tree orig_arg = NULL_TREE;
13515 int level = 0;
13516
13517 if (TREE_CODE (parm_pack) == BASES)
13518 {
13519 gcc_assert (parm_pack == pattern);
13520 tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
13521 if (BASES_DIRECT (parm_pack))
13522 return calculate_direct_bases (type, complain);
13523 else
13524 return calculate_bases (type, complain);
13525 }
13526 else if (builtin_pack_call_p (parm_pack))
13527 {
13528 if (parm_pack != pattern)
13529 {
13530 if (complain & tf_error)
13531 sorry ("%qE is not the entire pattern of the pack expansion",
13532 parm_pack);
13533 return error_mark_node;
13534 }
13535 return expand_builtin_pack_call (parm_pack, args,
13536 complain, in_decl);
13537 }
13538 else if (TREE_CODE (parm_pack) == PARM_DECL)
13539 {
13540 /* We know we have correct local_specializations if this
13541 expansion is at function scope, or if we're dealing with a
13542 local parameter in a requires expression; for the latter,
13543 tsubst_requires_expr set it up appropriately. */
13544 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13545 arg_pack = retrieve_local_specialization (parm_pack);
13546 else
13547 /* We can't rely on local_specializations for a parameter
13548 name used later in a function declaration (such as in a
13549 late-specified return type). Even if it exists, it might
13550 have the wrong value for a recursive call. */
13551 need_local_specializations = true;
13552
13553 if (!arg_pack)
13554 {
13555 /* This parameter pack was used in an unevaluated context. Just
13556 make a dummy decl, since it's only used for its type. */
13557 ++cp_unevaluated_operand;
13558 arg_pack = tsubst_decl (parm_pack, args, complain);
13559 --cp_unevaluated_operand;
13560 if (arg_pack && DECL_PACK_P (arg_pack))
13561 /* Partial instantiation of the parm_pack, we can't build
13562 up an argument pack yet. */
13563 arg_pack = NULL_TREE;
13564 else
13565 arg_pack = make_fnparm_pack (arg_pack);
13566 }
13567 else if (DECL_PACK_P (arg_pack))
13568 /* This argument pack isn't fully instantiated yet. */
13569 arg_pack = NULL_TREE;
13570 }
13571 else if (is_capture_proxy (parm_pack))
13572 {
13573 arg_pack = retrieve_local_specialization (parm_pack);
13574 if (DECL_PACK_P (arg_pack))
13575 arg_pack = NULL_TREE;
13576 }
13577 else
13578 {
13579 int idx;
13580 template_parm_level_and_index (parm_pack, &level, &idx);
13581 if (level <= levels)
13582 arg_pack = TMPL_ARG (args, level, idx);
13583
13584 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13585 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13586 arg_pack = NULL_TREE;
13587 }
13588
13589 orig_arg = arg_pack;
13590 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13591 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13592
13593 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13594 /* This can only happen if we forget to expand an argument
13595 pack somewhere else. Just return an error, silently. */
13596 {
13597 result = make_tree_vec (1);
13598 TREE_VEC_ELT (result, 0) = error_mark_node;
13599 return result;
13600 }
13601
13602 if (arg_pack)
13603 {
13604 int my_len =
13605 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13606
13607 /* Don't bother trying to do a partial substitution with
13608 incomplete packs; we'll try again after deduction. */
13609 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13610 return t;
13611
13612 if (len < 0)
13613 len = my_len;
13614 else if (len != my_len)
13615 {
13616 if (!(complain & tf_error))
13617 /* Fail quietly. */;
13618 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13619 error ("mismatched argument pack lengths while expanding %qT",
13620 pattern);
13621 else
13622 error ("mismatched argument pack lengths while expanding %qE",
13623 pattern);
13624 return error_mark_node;
13625 }
13626
13627 /* Keep track of the parameter packs and their corresponding
13628 argument packs. */
13629 packs = tree_cons (parm_pack, arg_pack, packs);
13630 TREE_TYPE (packs) = orig_arg;
13631 }
13632 else
13633 {
13634 /* We can't substitute for this parameter pack. We use a flag as
13635 well as the missing_level counter because function parameter
13636 packs don't have a level. */
13637 gcc_assert (processing_template_decl || is_auto (parm_pack));
13638 unsubstituted_packs = true;
13639 }
13640 }
13641
13642 /* If the expansion is just T..., return the matching argument pack, unless
13643 we need to call convert_from_reference on all the elements. This is an
13644 important optimization; see c++/68422. */
13645 if (!unsubstituted_packs
13646 && TREE_PURPOSE (packs) == pattern)
13647 {
13648 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13649
13650 /* If the argument pack is a single pack expansion, pull it out. */
13651 if (TREE_VEC_LENGTH (args) == 1
13652 && pack_expansion_args_count (args))
13653 {
13654 tree arg = TREE_VEC_ELT (args, 0);
13655 if (PACK_EXPANSION_SIZEOF_P (t)
13656 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
13657 /* Except if this isn't a simple sizeof...(T) which gets sZ
13658 mangling, keep the TREE_VEC to get sP mangling. */;
13659 else
13660 return TREE_VEC_ELT (args, 0);
13661 }
13662
13663 /* Types need no adjustment, nor does sizeof..., and if we still have
13664 some pack expansion args we won't do anything yet. */
13665 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13666 || PACK_EXPANSION_SIZEOF_P (t)
13667 || pack_expansion_args_count (args))
13668 return args;
13669 /* Also optimize expression pack expansions if we can tell that the
13670 elements won't have reference type. */
13671 tree type = TREE_TYPE (pattern);
13672 if (type && !TYPE_REF_P (type)
13673 && !PACK_EXPANSION_P (type)
13674 && !WILDCARD_TYPE_P (type))
13675 return args;
13676 /* Otherwise use the normal path so we get convert_from_reference. */
13677 }
13678
13679 /* We cannot expand this expansion expression, because we don't have
13680 all of the argument packs we need. */
13681 if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
13682 {
13683 /* We got some full packs, but we can't substitute them in until we
13684 have values for all the packs. So remember these until then. */
13685
13686 t = make_pack_expansion (pattern, complain);
13687 PACK_EXPANSION_EXTRA_ARGS (t)
13688 = build_extra_args (pattern, args, complain);
13689 return t;
13690 }
13691
13692 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13693 type, so create our own local specializations map; the current map is
13694 either NULL or (in the case of recursive unification) might have
13695 bindings that we don't want to use or alter. */
13696 local_specialization_stack lss (need_local_specializations
13697 ? lss_blank : lss_nop);
13698
13699 if (unsubstituted_packs)
13700 {
13701 /* There were no real arguments, we're just replacing a parameter
13702 pack with another version of itself. Substitute into the
13703 pattern and return a PACK_EXPANSION_*. The caller will need to
13704 deal with that. */
13705 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13706 result = tsubst_expr (pattern, args, complain, in_decl);
13707 else
13708 result = tsubst (pattern, args, complain, in_decl);
13709 result = make_pack_expansion (result, complain);
13710 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13711 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13712 if (PACK_EXPANSION_AUTO_P (t))
13713 {
13714 /* This is a fake auto... pack expansion created in add_capture with
13715 _PACKS that don't appear in the pattern. Copy one over. */
13716 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13717 pack = retrieve_local_specialization (TREE_VALUE (packs));
13718 gcc_checking_assert (DECL_PACK_P (pack));
13719 PACK_EXPANSION_PARAMETER_PACKS (result)
13720 = build_tree_list (NULL_TREE, pack);
13721 PACK_EXPANSION_AUTO_P (result) = true;
13722 }
13723 return result;
13724 }
13725
13726 gcc_assert (len >= 0);
13727
13728 /* For each argument in each argument pack, substitute into the
13729 pattern. */
13730 result = make_tree_vec (len);
13731 tree elem_args = copy_template_args (args);
13732 for (i = 0; i < len; ++i)
13733 {
13734 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13735 i,
13736 elem_args, complain,
13737 in_decl);
13738 TREE_VEC_ELT (result, i) = t;
13739 if (t == error_mark_node)
13740 {
13741 result = error_mark_node;
13742 break;
13743 }
13744 }
13745
13746 /* Update ARGS to restore the substitution from parameter packs to
13747 their argument packs. */
13748 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13749 {
13750 tree parm = TREE_PURPOSE (pack);
13751
13752 if (TREE_CODE (parm) == PARM_DECL
13753 || VAR_P (parm)
13754 || TREE_CODE (parm) == FIELD_DECL)
13755 register_local_specialization (TREE_TYPE (pack), parm);
13756 else
13757 {
13758 int idx, level;
13759
13760 if (TREE_VALUE (pack) == NULL_TREE)
13761 continue;
13762
13763 template_parm_level_and_index (parm, &level, &idx);
13764
13765 /* Update the corresponding argument. */
13766 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13767 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13768 TREE_TYPE (pack);
13769 else
13770 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13771 }
13772 }
13773
13774 /* If the dependent pack arguments were such that we end up with only a
13775 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13776 if (len == 1 && TREE_CODE (result) == TREE_VEC
13777 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13778 return TREE_VEC_ELT (result, 0);
13779
13780 return result;
13781 }
13782
13783 /* Make an argument pack out of the TREE_VEC VEC. */
13784
13785 static tree
13786 make_argument_pack (tree vec)
13787 {
13788 tree pack;
13789
13790 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13791 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13792 else
13793 {
13794 pack = make_node (NONTYPE_ARGUMENT_PACK);
13795 TREE_CONSTANT (pack) = 1;
13796 }
13797 ARGUMENT_PACK_ARGS (pack) = vec;
13798 return pack;
13799 }
13800
13801 /* Return an exact copy of template args T that can be modified
13802 independently. */
13803
13804 static tree
13805 copy_template_args (tree t)
13806 {
13807 if (t == error_mark_node)
13808 return t;
13809
13810 int len = TREE_VEC_LENGTH (t);
13811 tree new_vec = make_tree_vec (len);
13812
13813 for (int i = 0; i < len; ++i)
13814 {
13815 tree elt = TREE_VEC_ELT (t, i);
13816 if (elt && TREE_CODE (elt) == TREE_VEC)
13817 elt = copy_template_args (elt);
13818 TREE_VEC_ELT (new_vec, i) = elt;
13819 }
13820
13821 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13822 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13823
13824 return new_vec;
13825 }
13826
13827 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13828
13829 tree
13830 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13831 tree in_decl)
13832 {
13833 /* This flag is used only during deduction, and we don't expect to
13834 substitute such ARGUMENT_PACKs. */
13835 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
13836
13837 /* Substitute into each of the arguments. */
13838 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13839 args, complain, in_decl);
13840 if (pack_args == error_mark_node)
13841 return error_mark_node;
13842
13843 if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
13844 return orig_arg;
13845
13846 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13847 template parameter, we might be able to avoid allocating a new
13848 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13849 if the substituted result is identical to it. */
13850 if (tree parm = template_arg_to_parm (orig_arg))
13851 {
13852 int level, index;
13853 template_parm_level_and_index (parm, &level, &index);
13854 if (TMPL_ARGS_DEPTH (args) >= level)
13855 if (tree arg = TMPL_ARG (args, level, index))
13856 if (TREE_CODE (arg) == TREE_CODE (orig_arg)
13857 && ARGUMENT_PACK_ARGS (arg) == pack_args)
13858 {
13859 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
13860 return arg;
13861 }
13862 }
13863
13864 tree new_arg;
13865 if (TYPE_P (orig_arg))
13866 {
13867 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13868 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13869 }
13870 else
13871 {
13872 new_arg = make_node (TREE_CODE (orig_arg));
13873 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13874 }
13875 ARGUMENT_PACK_ARGS (new_arg) = pack_args;
13876 return new_arg;
13877 }
13878
13879 /* Substitute ARGS into the vector or list of template arguments T. */
13880
13881 tree
13882 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13883 {
13884 if (t == error_mark_node)
13885 return error_mark_node;
13886
13887 /* In "sizeof(X<I>)" we need to evaluate "I". */
13888 cp_evaluated ev;
13889
13890 const int len = TREE_VEC_LENGTH (t);
13891 tree *elts = XALLOCAVEC (tree, len);
13892 int expanded_len_adjust = 0;
13893
13894 /* True iff the substituted result is identical to T. */
13895 bool const_subst_p = true;
13896
13897 for (int i = 0; i < len; i++)
13898 {
13899 tree orig_arg = TREE_VEC_ELT (t, i);
13900 tree new_arg;
13901
13902 if (!orig_arg)
13903 new_arg = NULL_TREE;
13904 else if (TREE_CODE (orig_arg) == TREE_VEC)
13905 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13906 else if (PACK_EXPANSION_P (orig_arg))
13907 {
13908 /* Substitute into an expansion expression. */
13909 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13910
13911 if (TREE_CODE (new_arg) == TREE_VEC)
13912 /* Add to the expanded length adjustment the number of
13913 expanded arguments. We subtract one from this
13914 measurement, because the argument pack expression
13915 itself is already counted as 1 in
13916 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13917 the argument pack is empty. */
13918 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13919 }
13920 else if (ARGUMENT_PACK_P (orig_arg))
13921 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13922 else
13923 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13924
13925 if (new_arg == error_mark_node)
13926 return error_mark_node;
13927
13928 elts[i] = new_arg;
13929 if (new_arg != orig_arg)
13930 const_subst_p = false;
13931 }
13932
13933 if (const_subst_p)
13934 return t;
13935
13936 tree maybe_reuse = NULL_TREE;
13937
13938 /* If ARGS and T are both multi-level, the substituted result may be
13939 identical to ARGS. */
13940 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
13941 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
13942 && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
13943 maybe_reuse = args;
13944 /* If T appears to be a vector of generic template arguments, the
13945 substituted result may be identical to the corresponding level
13946 from ARGS. */
13947 else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
13948 {
13949 int level, index;
13950 template_parm_level_and_index (parm, &level, &index);
13951 if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
13952 maybe_reuse = TMPL_ARGS_LEVEL (args, level);
13953 }
13954
13955 /* If the substituted result is identical to MAYBE_REUSE, return
13956 it and avoid allocating a new TREE_VEC, as an optimization. */
13957 if (maybe_reuse != NULL_TREE
13958 && TREE_VEC_LENGTH (maybe_reuse) == len
13959 && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
13960 return maybe_reuse;
13961
13962 /* If T consists of only a pack expansion for which substitution yielded
13963 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13964 of effectively making a copy. */
13965 if (len == 1
13966 && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
13967 && TREE_CODE (elts[0]) == TREE_VEC)
13968 return elts[0];
13969
13970 /* Make space for the expanded arguments coming from template
13971 argument packs. */
13972 tree r = make_tree_vec (len + expanded_len_adjust);
13973 /* T can contain TREE_VECs. That happens if T contains the
13974 arguments for a member template.
13975 In that case each TREE_VEC in T represents a level of template
13976 arguments, and T won't carry any non defaulted argument count.
13977 It will rather be the nested TREE_VECs that will carry one.
13978 In other words, T carries a non defaulted argument count only
13979 if it doesn't contain any nested TREE_VEC. */
13980 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
13981 {
13982 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13983 count += expanded_len_adjust;
13984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
13985 }
13986
13987 int out = 0;
13988 for (int i = 0; i < len; i++)
13989 {
13990 tree orig_arg = TREE_VEC_ELT (t, i);
13991 if (orig_arg
13992 && PACK_EXPANSION_P (orig_arg)
13993 && TREE_CODE (elts[i]) == TREE_VEC)
13994 {
13995 /* Now expand the template argument pack "in place". */
13996 for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13997 TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
13998 }
13999 else
14000 {
14001 TREE_VEC_ELT (r, out) = elts[i];
14002 out++;
14003 }
14004 }
14005 gcc_assert (out == TREE_VEC_LENGTH (r));
14006
14007 return r;
14008 }
14009
14010 /* Substitute ARGS into one level PARMS of template parameters. */
14011
14012 static tree
14013 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
14014 {
14015 if (parms == error_mark_node)
14016 return error_mark_node;
14017
14018 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
14019
14020 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
14021 {
14022 tree tuple = TREE_VEC_ELT (parms, i);
14023
14024 if (tuple == error_mark_node)
14025 continue;
14026
14027 TREE_VEC_ELT (new_vec, i) =
14028 tsubst_template_parm (tuple, args, complain);
14029 }
14030
14031 return new_vec;
14032 }
14033
14034 /* Return the result of substituting ARGS into the template parameters
14035 given by PARMS. If there are m levels of ARGS and m + n levels of
14036 PARMS, then the result will contain n levels of PARMS. For
14037 example, if PARMS is `template <class T> template <class U>
14038 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
14039 result will be `template <int*, double, class V>'. */
14040
14041 static tree
14042 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
14043 {
14044 tree r = NULL_TREE;
14045 tree* new_parms;
14046
14047 /* When substituting into a template, we must set
14048 PROCESSING_TEMPLATE_DECL as the template parameters may be
14049 dependent if they are based on one-another, and the dependency
14050 predicates are short-circuit outside of templates. */
14051 ++processing_template_decl;
14052
14053 for (new_parms = &r;
14054 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
14055 new_parms = &(TREE_CHAIN (*new_parms)),
14056 parms = TREE_CHAIN (parms))
14057 {
14058 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
14059 args, complain);
14060 *new_parms =
14061 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
14062 - TMPL_ARGS_DEPTH (args)),
14063 new_vec, NULL_TREE);
14064 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
14065 = TEMPLATE_PARMS_CONSTRAINTS (parms);
14066 }
14067
14068 --processing_template_decl;
14069
14070 return r;
14071 }
14072
14073 /* Return the result of substituting ARGS into one template parameter
14074 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
14075 parameter and which TREE_PURPOSE is the default argument of the
14076 template parameter. */
14077
14078 static tree
14079 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
14080 {
14081 tree default_value, parm_decl;
14082
14083 if (args == NULL_TREE
14084 || t == NULL_TREE
14085 || t == error_mark_node)
14086 return t;
14087
14088 gcc_assert (TREE_CODE (t) == TREE_LIST);
14089
14090 default_value = TREE_PURPOSE (t);
14091 parm_decl = TREE_VALUE (t);
14092
14093 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
14094 if (TREE_CODE (parm_decl) == PARM_DECL
14095 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
14096 parm_decl = error_mark_node;
14097 default_value = tsubst_template_arg (default_value, args,
14098 complain, NULL_TREE);
14099
14100 tree r = build_tree_list (default_value, parm_decl);
14101 TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
14102 return r;
14103 }
14104
14105 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
14106 parameter in PARMS for sake of declaration matching. */
14107
14108 static void
14109 tsubst_each_template_parm_constraints (tree parms, tree args,
14110 tsubst_flags_t complain)
14111 {
14112 ++processing_template_decl;
14113 for (; parms; parms = TREE_CHAIN (parms))
14114 {
14115 tree level = TREE_VALUE (parms);
14116 for (tree parm : tree_vec_range (level))
14117 TEMPLATE_PARM_CONSTRAINTS (parm)
14118 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
14119 complain, NULL_TREE);
14120 }
14121 --processing_template_decl;
14122 }
14123
14124 /* Substitute the ARGS into the indicated aggregate (or enumeration)
14125 type T. If T is not an aggregate or enumeration type, it is
14126 handled as if by tsubst. IN_DECL is as for tsubst. If
14127 ENTERING_SCOPE is nonzero, T is the context for a template which
14128 we are presently tsubst'ing. Return the substituted value. */
14129
14130 static tree
14131 tsubst_aggr_type (tree t,
14132 tree args,
14133 tsubst_flags_t complain,
14134 tree in_decl,
14135 int entering_scope)
14136 {
14137 if (t == NULL_TREE)
14138 return NULL_TREE;
14139
14140 /* Handle typedefs via tsubst so that they get consistently reused. */
14141 if (typedef_variant_p (t))
14142 {
14143 t = tsubst (t, args, complain, in_decl);
14144 if (t == error_mark_node)
14145 return error_mark_node;
14146
14147 /* The effect of entering_scope is that for a dependent specialization
14148 A<T>, lookup_template_class prefers to return A's primary template
14149 type instead of the implicit instantiation. So when entering_scope,
14150 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
14151 taking advantage of the fact that lookup_template_class links the two
14152 types by setting TYPE_CANONICAL of the latter to the former. */
14153 if (entering_scope
14154 && CLASS_TYPE_P (t)
14155 && dependent_type_p (t)
14156 && TYPE_TEMPLATE_INFO (t)
14157 && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t)))
14158 t = TYPE_CANONICAL (t);
14159
14160 return t;
14161 }
14162
14163 switch (TREE_CODE (t))
14164 {
14165 case RECORD_TYPE:
14166 case ENUMERAL_TYPE:
14167 case UNION_TYPE:
14168 return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope);
14169
14170 default:
14171 return tsubst (t, args, complain, in_decl);
14172 }
14173 }
14174
14175 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
14176 and ENUMERAL_TYPE cases of tsubst. */
14177
14178 static tree
14179 tsubst_aggr_type_1 (tree t,
14180 tree args,
14181 tsubst_flags_t complain,
14182 tree in_decl,
14183 int entering_scope)
14184 {
14185 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
14186 {
14187 complain &= ~tf_qualifying_scope;
14188
14189 /* Figure out what arguments are appropriate for the
14190 type we are trying to find. For example, given:
14191
14192 template <class T> struct S;
14193 template <class T, class U> void f(T, U) { S<U> su; }
14194
14195 and supposing that we are instantiating f<int, double>,
14196 then our ARGS will be {int, double}, but, when looking up
14197 S we only want {double}. */
14198 tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
14199 complain, in_decl);
14200 if (argvec == error_mark_node)
14201 return error_mark_node;
14202
14203 tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
14204 entering_scope, complain);
14205 return cp_build_qualified_type (r, cp_type_quals (t), complain);
14206 }
14207 else
14208 /* This is not a template type, so there's nothing to do. */
14209 return t;
14210 }
14211
14212 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
14213 indexed in reverse order of the parameters. */
14214
14215 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
14216
14217 /* Return a reference to the vec* of defarg insts for FN. */
14218
14219 static vec<tree,va_gc> *&
14220 defarg_insts_for (tree fn)
14221 {
14222 if (!defarg_inst)
14223 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
14224 tree_vec_map in = { { fn }, nullptr };
14225 tree_vec_map **slot
14226 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
14227 if (!*slot)
14228 {
14229 *slot = ggc_alloc<tree_vec_map> ();
14230 **slot = in;
14231 }
14232 return (*slot)->to;
14233 }
14234
14235 /* Substitute into the default argument ARG (a default argument for
14236 FN), which has the indicated TYPE. */
14237
14238 tree
14239 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
14240 tsubst_flags_t complain)
14241 {
14242 int errs = errorcount + sorrycount;
14243
14244 /* This can happen in invalid code. */
14245 if (TREE_CODE (arg) == DEFERRED_PARSE)
14246 return arg;
14247
14248 /* Shortcut {}. */
14249 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
14250 && CONSTRUCTOR_NELTS (arg) == 0)
14251 return arg;
14252
14253 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14254 parm = chain_index (parmnum, parm);
14255 tree parmtype = TREE_TYPE (parm);
14256 if (DECL_BY_REFERENCE (parm))
14257 parmtype = TREE_TYPE (parmtype);
14258 if (parmtype == error_mark_node)
14259 return error_mark_node;
14260
14261 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
14262
14263 /* Remember the location of the pointer to the vec rather than the location
14264 of the particular element, in case the vec grows in tsubst_expr. */
14265 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
14266 /* Index in reverse order to avoid allocating space for initial parameters
14267 that don't have default arguments. */
14268 unsigned ridx = list_length (parm);
14269 if (vec_safe_length (defs) < ridx)
14270 vec_safe_grow_cleared (defs, ridx);
14271 else if (tree inst = (*defs)[ridx - 1])
14272 return inst;
14273
14274 /* This default argument came from a template. Instantiate the
14275 default argument here, not in tsubst. In the case of
14276 something like:
14277
14278 template <class T>
14279 struct S {
14280 static T t();
14281 void f(T = t());
14282 };
14283
14284 we must be careful to do name lookup in the scope of S<T>,
14285 rather than in the current class. */
14286 push_to_top_level ();
14287 push_access_scope (fn);
14288 push_deferring_access_checks (dk_no_deferred);
14289 /* So in_immediate_context knows this is a default argument. */
14290 begin_scope (sk_function_parms, fn);
14291 start_lambda_scope (parm);
14292
14293 /* The default argument expression may cause implicitly defined
14294 member functions to be synthesized, which will result in garbage
14295 collection. We must treat this situation as if we were within
14296 the body of function so as to avoid collecting live data on the
14297 stack. */
14298 ++function_depth;
14299 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
14300 --function_depth;
14301
14302 finish_lambda_scope ();
14303
14304 /* Make sure the default argument is reasonable. */
14305 arg = check_default_argument (type, arg, complain);
14306
14307 if (errorcount+sorrycount > errs
14308 && (complain & tf_warning_or_error))
14309 inform (input_location,
14310 " when instantiating default argument for call to %qD", fn);
14311
14312 leave_scope ();
14313 pop_deferring_access_checks ();
14314 pop_access_scope (fn);
14315 pop_from_top_level ();
14316
14317 if (arg != error_mark_node && !cp_unevaluated_operand)
14318 (*defs)[ridx - 1] = arg;
14319
14320 return arg;
14321 }
14322
14323 /* Substitute into all the default arguments for FN. */
14324
14325 static void
14326 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
14327 {
14328 tree arg;
14329 tree tmpl_args;
14330
14331 tmpl_args = DECL_TI_ARGS (fn);
14332
14333 /* If this function is not yet instantiated, we certainly don't need
14334 its default arguments. */
14335 if (uses_template_parms (tmpl_args))
14336 return;
14337 /* Don't do this again for clones. */
14338 if (DECL_CLONED_FUNCTION_P (fn))
14339 return;
14340
14341 int i = 0;
14342 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
14343 arg;
14344 arg = TREE_CHAIN (arg), ++i)
14345 if (TREE_PURPOSE (arg))
14346 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
14347 TREE_VALUE (arg),
14348 TREE_PURPOSE (arg),
14349 complain);
14350 }
14351
14352 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14353 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
14354
14355 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14356
14357 void
14358 store_explicit_specifier (tree v, tree t)
14359 {
14360 if (!explicit_specifier_map)
14361 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
14362 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
14363 explicit_specifier_map->put (v, t);
14364 }
14365
14366 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14367
14368 tree
14369 lookup_explicit_specifier (tree v)
14370 {
14371 return *explicit_specifier_map->get (v);
14372 }
14373
14374 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14375 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14376 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14377 identical to T. */
14378
14379 static tree
14380 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
14381 tree raises, tsubst_flags_t complain)
14382 {
14383 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
14384
14385 tree new_type;
14386 if (TREE_CODE (t) == FUNCTION_TYPE)
14387 {
14388 new_type = build_function_type (return_type, arg_types);
14389 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
14390 }
14391 else
14392 {
14393 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14394 /* Don't pick up extra function qualifiers from the basetype. */
14395 r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
14396 if (! MAYBE_CLASS_TYPE_P (r))
14397 {
14398 /* [temp.deduct]
14399
14400 Type deduction may fail for any of the following
14401 reasons:
14402
14403 -- Attempting to create "pointer to member of T" when T
14404 is not a class type. */
14405 if (complain & tf_error)
14406 error ("creating pointer to member function of non-class type %qT",
14407 r);
14408 return error_mark_node;
14409 }
14410
14411 new_type = build_method_type_directly (r, return_type,
14412 TREE_CHAIN (arg_types));
14413 }
14414 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
14415
14416 cp_ref_qualifier rqual = type_memfn_rqual (t);
14417 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14418 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
14419 }
14420
14421 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14422 each of its formal parameters. If there is a disagreement then rebuild
14423 DECL's function type according to its formal parameter types, as part of a
14424 resolution for Core issues 1001/1322. */
14425
14426 static void
14427 maybe_rebuild_function_decl_type (tree decl)
14428 {
14429 bool function_type_needs_rebuilding = false;
14430 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
14431 {
14432 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
14433 while (parm_type_list && parm_type_list != void_list_node)
14434 {
14435 tree parm_type = TREE_VALUE (parm_type_list);
14436 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14437 if (!same_type_p (parm_type, formal_parm_type_unqual))
14438 {
14439 function_type_needs_rebuilding = true;
14440 break;
14441 }
14442
14443 parm_list = DECL_CHAIN (parm_list);
14444 parm_type_list = TREE_CHAIN (parm_type_list);
14445 }
14446 }
14447
14448 if (!function_type_needs_rebuilding)
14449 return;
14450
14451 const tree fntype = TREE_TYPE (decl);
14452 tree parm_list = DECL_ARGUMENTS (decl);
14453 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
14454 tree new_parm_type_list = NULL_TREE;
14455 tree *q = &new_parm_type_list;
14456 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
14457 {
14458 *q = copy_node (old_parm_type_list);
14459 parm_list = DECL_CHAIN (parm_list);
14460 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14461 q = &TREE_CHAIN (*q);
14462 }
14463 while (old_parm_type_list && old_parm_type_list != void_list_node)
14464 {
14465 *q = copy_node (old_parm_type_list);
14466 tree *new_parm_type = &TREE_VALUE (*q);
14467 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14468 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
14469 *new_parm_type = formal_parm_type_unqual;
14470
14471 parm_list = DECL_CHAIN (parm_list);
14472 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14473 q = &TREE_CHAIN (*q);
14474 }
14475 if (old_parm_type_list == void_list_node)
14476 *q = void_list_node;
14477
14478 TREE_TYPE (decl)
14479 = rebuild_function_or_method_type (fntype,
14480 TREE_TYPE (fntype), new_parm_type_list,
14481 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
14482 }
14483
14484 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14485
14486 static tree
14487 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
14488 tree lambda_fntype, bool use_spec_table = true)
14489 {
14490 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
14491 hashval_t hash = 0;
14492 tree in_decl = t;
14493
14494 /* Nobody should be tsubst'ing into non-template functions. */
14495 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
14496 || DECL_LOCAL_DECL_P (t));
14497
14498 if (DECL_LOCAL_DECL_P (t))
14499 {
14500 if (tree spec = retrieve_local_specialization (t))
14501 return spec;
14502 }
14503 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
14504 {
14505 /* If T is not dependent, just return it. */
14506 if (!uses_template_parms (DECL_TI_ARGS (t))
14507 && !LAMBDA_FUNCTION_P (t))
14508 return t;
14509
14510 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14511 if (non_templated_friend_p (t))
14512 goto friend_case;
14513
14514 /* Calculate the most general template of which R is a
14515 specialization. */
14516 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
14517
14518 /* We're substituting a lambda function under tsubst_lambda_expr but not
14519 directly from it; find the matching function we're already inside.
14520 But don't do this if T is a generic lambda with a single level of
14521 template parms, as in that case we're doing a normal instantiation. */
14522 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
14523 && (!generic_lambda_fn_p (t)
14524 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
14525 return enclosing_instantiation_of (t);
14526
14527 /* Calculate the complete set of arguments used to
14528 specialize R. */
14529 if (use_spec_table && !lambda_fntype)
14530 {
14531 argvec = tsubst_template_args (DECL_TI_ARGS
14532 (DECL_TEMPLATE_RESULT
14533 (DECL_TI_TEMPLATE (t))),
14534 args, complain, in_decl);
14535 if (argvec == error_mark_node)
14536 return error_mark_node;
14537
14538 /* Check to see if we already have this specialization. */
14539 hash = spec_hasher::hash (gen_tmpl, argvec);
14540 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
14541 /* The spec for these args might be a partial instantiation of the
14542 template, but here what we want is the FUNCTION_DECL. */
14543 return STRIP_TEMPLATE (spec);
14544 }
14545 else
14546 argvec = args;
14547 }
14548 else
14549 {
14550 /* This special case arises when we have something like this:
14551
14552 template <class T> struct S {
14553 friend void f<int>(int, double);
14554 };
14555
14556 Here, the DECL_TI_TEMPLATE for the friend declaration
14557 will be an IDENTIFIER_NODE. We are being called from
14558 tsubst_friend_function, and we want only to create a
14559 new decl (R) with appropriate types so that we can call
14560 determine_specialization. */
14561 friend_case:
14562 gen_tmpl = NULL_TREE;
14563 argvec = NULL_TREE;
14564 }
14565
14566 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14567 : NULL_TREE);
14568 tree ctx = closure ? closure : DECL_CONTEXT (t);
14569 bool member = ctx && TYPE_P (ctx);
14570
14571 /* If this is a static or xobj lambda, remove the 'this' pointer added in
14572 tsubst_lambda_expr now that we know the closure type. */
14573 if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
14574 lambda_fntype = static_fn_type (lambda_fntype);
14575
14576 if (member && !closure)
14577 ctx = tsubst_aggr_type (ctx, args,
14578 complain, t, /*entering_scope=*/1);
14579
14580 tree type = (lambda_fntype ? lambda_fntype
14581 : tsubst (TREE_TYPE (t), args,
14582 complain | tf_fndecl_type, in_decl));
14583 if (type == error_mark_node)
14584 return error_mark_node;
14585
14586 /* If we hit excessive deduction depth, the type is bogus even if
14587 it isn't error_mark_node, so don't build a decl. */
14588 if (excessive_deduction_depth)
14589 return error_mark_node;
14590
14591 /* We do NOT check for matching decls pushed separately at this
14592 point, as they may not represent instantiations of this
14593 template, and in any case are considered separate under the
14594 discrete model. */
14595 tree r = copy_decl (t);
14596 DECL_USE_TEMPLATE (r) = 0;
14597 TREE_TYPE (r) = type;
14598 /* Clear out the mangled name and RTL for the instantiation. */
14599 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14600 SET_DECL_RTL (r, NULL);
14601 /* Leave DECL_INITIAL set on deleted instantiations. */
14602 if (!DECL_DELETED_FN (r))
14603 DECL_INITIAL (r) = NULL_TREE;
14604 DECL_CONTEXT (r) = ctx;
14605 set_instantiating_module (r);
14606
14607 /* Handle explicit(dependent-expr). */
14608 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14609 {
14610 tree spec = lookup_explicit_specifier (t);
14611 spec = tsubst_expr (spec, args, complain, in_decl);
14612 spec = build_explicit_specifier (spec, complain);
14613 if (spec == error_mark_node)
14614 return error_mark_node;
14615 if (instantiation_dependent_expression_p (spec))
14616 store_explicit_specifier (r, spec);
14617 else
14618 {
14619 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14620 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
14621 }
14622 }
14623
14624 /* OpenMP UDRs have the only argument a reference to the declared
14625 type. We want to diagnose if the declared type is a reference,
14626 which is invalid, but as references to references are usually
14627 quietly merged, diagnose it here. */
14628 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14629 {
14630 tree argtype
14631 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14632 argtype = tsubst (argtype, args, complain, in_decl);
14633 if (TYPE_REF_P (argtype))
14634 error_at (DECL_SOURCE_LOCATION (t),
14635 "reference type %qT in "
14636 "%<#pragma omp declare reduction%>", argtype);
14637 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14638 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14639 argtype);
14640 }
14641
14642 if (member && DECL_CONV_FN_P (r))
14643 /* Type-conversion operator. Reconstruct the name, in
14644 case it's the name of one of the template's parameters. */
14645 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14646
14647 tree parms = DECL_ARGUMENTS (t);
14648 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14649 parms = DECL_CHAIN (parms);
14650 parms = tsubst (parms, args, complain, t);
14651 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14652 DECL_CONTEXT (parm) = r;
14653 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
14654 {
14655 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14656 DECL_NAME (tparm) = closure_identifier;
14657 DECL_CHAIN (tparm) = parms;
14658 parms = tparm;
14659 }
14660 DECL_ARGUMENTS (r) = parms;
14661 DECL_RESULT (r) = NULL_TREE;
14662
14663 maybe_rebuild_function_decl_type (r);
14664
14665 TREE_STATIC (r) = 0;
14666 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14667 DECL_EXTERNAL (r) = 1;
14668 /* If this is an instantiation of a function with internal
14669 linkage, we already know what object file linkage will be
14670 assigned to the instantiation. */
14671 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14672 DECL_DEFER_OUTPUT (r) = 0;
14673 DECL_CHAIN (r) = NULL_TREE;
14674 DECL_PENDING_INLINE_INFO (r) = 0;
14675 DECL_PENDING_INLINE_P (r) = 0;
14676 DECL_SAVED_TREE (r) = NULL_TREE;
14677 DECL_STRUCT_FUNCTION (r) = NULL;
14678 TREE_USED (r) = 0;
14679 /* We'll re-clone as appropriate in instantiate_template. */
14680 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14681
14682 /* If we aren't complaining now, return on error before we register
14683 the specialization so that we'll complain eventually. */
14684 if ((complain & tf_error) == 0
14685 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14686 && !grok_op_properties (r, /*complain=*/false))
14687 return error_mark_node;
14688
14689 /* If we are looking at an xobj lambda, we might need to check the type of
14690 its xobj parameter. */
14691 if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
14692 {
14693 tree closure_obj = DECL_CONTEXT (r);
14694 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
14695 tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
14696
14697 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
14698 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
14699 /* If a lambda has an empty capture clause, an xobj parameter of
14700 unrelated type is not an error. */;
14701 else if (dependent_type_p (obj_param))
14702 /* If we are coming from tsubst_lambda_expr we might not have
14703 substituted into our xobj parameter yet. We can't error out until
14704 we know what the type really is so do nothing...
14705 ...but if we are instantiating the call op for real and we don't
14706 have a real type then something has gone incredibly wrong. */
14707 gcc_assert (lambda_fntype);
14708 else
14709 {
14710 /* We have a lambda with captures, and know the type of the xobj
14711 parameter, time to check it. */
14712 tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
14713 if (!same_or_base_type_p (closure_obj, obj_param_type))
14714 {
14715 /* This error does not emit when the lambda's call operator
14716 template is instantiated by taking its address, such as in
14717 the following case:
14718
14719 auto f = [x = 0](this auto&&){};
14720 int (*fp)(int&) = &decltype(f)::operator();
14721
14722 It only emits when explicitly calling the call operator with
14723 an explicit template parameter:
14724
14725 template<typename T>
14726 struct S : T {
14727 using T::operator();
14728 operator int() const {return {};}
14729 };
14730
14731 auto s = S{[x = 0](this auto&&) {}};
14732 s.operator()<int>();
14733
14734 This is due to resolve_address_of_overloaded_function being
14735 deficient at reporting candidates when overload resolution
14736 fails.
14737
14738 This diagnostic will be active in the first case if/when
14739 resolve_address_of_overloaded_function is fixed to properly
14740 emit candidates upon failure to resolve to an overload. */
14741 if (complain & tf_error)
14742 error ("a lambda with captures may not have an explicit "
14743 "object parameter of an unrelated type");
14744 return error_mark_node;
14745 }
14746 }
14747 }
14748
14749 /* Associate the constraints directly with the instantiation. We
14750 don't substitute through the constraints; that's only done when
14751 they are checked. */
14752 if (tree ci = get_constraints (t))
14753 set_constraints (r, ci);
14754
14755 if (DECL_FRIEND_CONTEXT (t))
14756 SET_DECL_FRIEND_CONTEXT (r,
14757 tsubst (DECL_FRIEND_CONTEXT (t),
14758 args, complain, in_decl));
14759
14760 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14761 args, complain, in_decl))
14762 return error_mark_node;
14763
14764 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14765 this in the special friend case mentioned above where
14766 GEN_TMPL is NULL. */
14767 if (gen_tmpl && !closure)
14768 {
14769 DECL_TEMPLATE_INFO (r)
14770 = build_template_info (gen_tmpl, argvec);
14771 SET_DECL_IMPLICIT_INSTANTIATION (r);
14772
14773 if (use_spec_table)
14774 {
14775 tree new_r
14776 = register_specialization (r, gen_tmpl, argvec, false, hash);
14777 if (new_r != r)
14778 /* We instantiated this while substituting into
14779 the type earlier (template/friend54.C). */
14780 return new_r;
14781 }
14782
14783 /* We're not supposed to instantiate default arguments
14784 until they are called, for a template. But, for a
14785 declaration like:
14786
14787 template <class T> void f ()
14788 { extern void g(int i = T()); }
14789
14790 we should do the substitution when the template is
14791 instantiated. We handle the member function case in
14792 instantiate_class_template since the default arguments
14793 might refer to other members of the class. */
14794 if (!member
14795 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14796 && !uses_template_parms (argvec))
14797 tsubst_default_arguments (r, complain);
14798 }
14799 else if (DECL_LOCAL_DECL_P (r))
14800 {
14801 if (!cp_unevaluated_operand)
14802 register_local_specialization (r, t);
14803 }
14804 else
14805 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14806
14807 /* Copy the list of befriending classes. */
14808 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14809 *friends;
14810 friends = &TREE_CHAIN (*friends))
14811 {
14812 *friends = copy_node (*friends);
14813 TREE_VALUE (*friends)
14814 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14815 }
14816
14817 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14818 {
14819 maybe_retrofit_in_chrg (r);
14820 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14821 return error_mark_node;
14822 /* If this is an instantiation of a member template, clone it.
14823 If it isn't, that'll be handled by
14824 clone_constructors_and_destructors. */
14825 if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
14826 clone_cdtor (r, /*update_methods=*/false);
14827 }
14828 else if ((complain & tf_error) != 0
14829 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14830 && !grok_op_properties (r, /*complain=*/true))
14831 return error_mark_node;
14832
14833 /* Possibly limit visibility based on template args. */
14834 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14835 if (DECL_VISIBILITY_SPECIFIED (t))
14836 {
14837 DECL_VISIBILITY_SPECIFIED (r) = 0;
14838 DECL_ATTRIBUTES (r)
14839 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14840 }
14841 determine_visibility (r);
14842 if (DECL_SECTION_NAME (t))
14843 set_decl_section_name (r, t);
14844 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14845 && COMPLETE_TYPE_P (DECL_CONTEXT (r))
14846 && !processing_template_decl)
14847 defaulted_late_check (r);
14848
14849 if (flag_openmp)
14850 if (tree attr = lookup_attribute ("omp declare variant base",
14851 DECL_ATTRIBUTES (r)))
14852 omp_declare_variant_finalize (r, attr);
14853
14854 return r;
14855 }
14856
14857 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14858
14859 static tree
14860 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14861 tree lambda_fntype, tree lambda_tparms)
14862 {
14863 /* We can get here when processing a member function template,
14864 member class template, or template template parameter. */
14865 tree decl = DECL_TEMPLATE_RESULT (t);
14866 tree in_decl = t;
14867 tree spec;
14868 tree tmpl_args;
14869 tree full_args = NULL_TREE;
14870 tree r;
14871 hashval_t hash = 0;
14872
14873 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14874 {
14875 /* Template template parameter is treated here. */
14876 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14877 if (new_type == error_mark_node)
14878 r = error_mark_node;
14879 /* If we get a real template back, return it. This can happen in
14880 the context of most_specialized_partial_spec. */
14881 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14882 r = new_type;
14883 else
14884 /* The new TEMPLATE_DECL was built in
14885 reduce_template_parm_level. */
14886 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14887 return r;
14888 }
14889
14890 if (!lambda_fntype)
14891 {
14892 /* We might already have an instance of this template.
14893 The ARGS are for the surrounding class type, so the
14894 full args contain the tsubst'd args for the context,
14895 plus the innermost args from the template decl. */
14896 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14897 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14898 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14899 /* Because this is a template, the arguments will still be
14900 dependent, even after substitution. If
14901 PROCESSING_TEMPLATE_DECL is not set, the dependency
14902 predicates will short-circuit. */
14903 ++processing_template_decl;
14904 full_args = tsubst_template_args (tmpl_args, args,
14905 complain, in_decl);
14906 --processing_template_decl;
14907 if (full_args == error_mark_node)
14908 return error_mark_node;
14909
14910 /* If this is a default template template argument,
14911 tsubst might not have changed anything. */
14912 if (full_args == tmpl_args)
14913 return t;
14914
14915 hash = spec_hasher::hash (t, full_args);
14916 spec = retrieve_specialization (t, full_args, hash);
14917 if (spec != NULL_TREE)
14918 {
14919 if (TYPE_P (spec))
14920 /* Type partial instantiations are stored as the type by
14921 lookup_template_class_1, not here as the template. */
14922 spec = CLASSTYPE_TI_TEMPLATE (spec);
14923 else if (TREE_CODE (spec) != TEMPLATE_DECL)
14924 spec = DECL_TI_TEMPLATE (spec);
14925 return spec;
14926 }
14927 }
14928
14929 /* Make a new template decl. It will be similar to the
14930 original, but will record the current template arguments.
14931 We also create a new function declaration, which is just
14932 like the old one, but points to this new template, rather
14933 than the old one. */
14934 r = copy_decl (t);
14935 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14936 DECL_CHAIN (r) = NULL_TREE;
14937
14938 // Build new template info linking to the original template decl.
14939 if (!lambda_fntype)
14940 {
14941 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14942 SET_DECL_IMPLICIT_INSTANTIATION (r);
14943 }
14944 else
14945 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14946
14947 /* The template parameters for this new template are all the
14948 template parameters for the old template, except the
14949 outermost level of parameters. */
14950 auto tparm_guard = make_temp_override (current_template_parms);
14951 DECL_TEMPLATE_PARMS (r)
14952 = current_template_parms
14953 = (lambda_tparms
14954 ? lambda_tparms
14955 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14956 complain));
14957
14958 bool class_p = false;
14959 tree inner = decl;
14960 ++processing_template_decl;
14961 if (TREE_CODE (inner) == FUNCTION_DECL)
14962 inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
14963 /*use_spec_table=*/false);
14964 else
14965 {
14966 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14967 {
14968 class_p = true;
14969 inner = TREE_TYPE (inner);
14970 }
14971 if (class_p)
14972 inner = tsubst_aggr_type (inner, args, complain,
14973 in_decl, /*entering*/1);
14974 else
14975 inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
14976 }
14977 --processing_template_decl;
14978 if (inner == error_mark_node)
14979 return error_mark_node;
14980
14981 if (class_p)
14982 {
14983 /* For a partial specialization, we need to keep pointing to
14984 the primary template. */
14985 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14986 {
14987 CLASSTYPE_TI_TEMPLATE (inner) = r;
14988 CLASSTYPE_USE_TEMPLATE (inner) = 0;
14989 }
14990
14991 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14992 inner = TYPE_MAIN_DECL (inner);
14993 }
14994 else if (lambda_fntype)
14995 {
14996 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14997 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14998 }
14999 else
15000 {
15001 DECL_TI_TEMPLATE (inner) = r;
15002 /* Set DECL_TI_ARGS to the full set of template arguments,
15003 which tsubst_function_decl / tsubst_decl didn't do due to
15004 use_spec_table=false. */
15005 DECL_TI_ARGS (inner) = full_args;
15006 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
15007 }
15008
15009 DECL_TEMPLATE_RESULT (r) = inner;
15010 TREE_TYPE (r) = TREE_TYPE (inner);
15011 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
15012
15013 if (modules_p ())
15014 {
15015 /* Propagate module information from the decl. */
15016 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
15017 if (DECL_LANG_SPECIFIC (inner))
15018 /* If this is a constrained template, the above tsubst of
15019 inner can find the unconstrained template, which may have
15020 come from an import. This is ok, because we don't
15021 register this instantiation (see below). */
15022 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
15023 || (TEMPLATE_PARMS_CONSTRAINTS
15024 (DECL_TEMPLATE_PARMS (t))));
15025 }
15026
15027 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
15028 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
15029
15030 if (PRIMARY_TEMPLATE_P (t))
15031 DECL_PRIMARY_TEMPLATE (r) = r;
15032
15033 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r) = false;
15034
15035 if (!lambda_fntype && !class_p)
15036 {
15037 /* Record this non-type partial instantiation. */
15038 /* FIXME we'd like to always register the TEMPLATE_DECL, or always
15039 the DECL_TEMPLATE_RESULT, but it seems the modules code relies
15040 on this current behavior. */
15041 if (TREE_CODE (inner) == FUNCTION_DECL)
15042 register_specialization (r, t, full_args, false, hash);
15043 else
15044 register_specialization (inner, t, full_args, false, hash);
15045 }
15046
15047 return r;
15048 }
15049
15050 /* True if FN is the op() for a lambda in an uninstantiated template. */
15051
15052 bool
15053 lambda_fn_in_template_p (tree fn)
15054 {
15055 if (!fn || !LAMBDA_FUNCTION_P (fn))
15056 return false;
15057 tree closure = DECL_CONTEXT (fn);
15058 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
15059 }
15060
15061 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
15062 which the above is true. */
15063
15064 bool
15065 regenerated_lambda_fn_p (tree fn)
15066 {
15067 if (!fn || !LAMBDA_FUNCTION_P (fn))
15068 return false;
15069 tree closure = DECL_CONTEXT (fn);
15070 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
15071 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
15072 }
15073
15074 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
15075 If T is not a regenerated LAMBDA_EXPR, return T. */
15076
15077 tree
15078 most_general_lambda (tree t)
15079 {
15080 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15081 t = TI_TEMPLATE (ti);
15082 return t;
15083 }
15084
15085 /* Return the set of template arguments used to regenerate the lambda T
15086 from its most general lambda. */
15087
15088 tree
15089 lambda_regenerating_args (tree t)
15090 {
15091 if (LAMBDA_FUNCTION_P (t))
15092 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
15093 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
15094 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
15095 return TI_ARGS (ti);
15096 else
15097 return NULL_TREE;
15098 }
15099
15100 /* We're instantiating a variable from template function TCTX. Return the
15101 corresponding current enclosing scope. We can match them up using
15102 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
15103 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
15104 the template definition in regenerate_decl_from_template. */
15105
15106 static tree
15107 enclosing_instantiation_of (tree tctx)
15108 {
15109 tree fn = current_function_decl;
15110
15111 /* We shouldn't ever need to do this for other artificial functions. */
15112 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
15113
15114 for (; fn; fn = decl_function_context (fn))
15115 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
15116 return fn;
15117 gcc_unreachable ();
15118 }
15119
15120 /* Substitute the ARGS into the T, which is a _DECL. Return the
15121 result of the substitution. Issue error and warning messages under
15122 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up
15123 and insert into the specializations table or if we can assume it's
15124 the caller's responsibility; this is used by instantiate_template
15125 to avoid doing some redundant work. */
15126
15127 static tree
15128 tsubst_decl (tree t, tree args, tsubst_flags_t complain,
15129 bool use_spec_table /* = true */)
15130 {
15131 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15132 location_t saved_loc;
15133 tree r = NULL_TREE;
15134 tree in_decl = t;
15135 hashval_t hash = 0;
15136
15137 if (t == error_mark_node)
15138 return error_mark_node;
15139
15140 /* Set the filename and linenumber to improve error-reporting. */
15141 saved_loc = input_location;
15142 input_location = DECL_SOURCE_LOCATION (t);
15143
15144 switch (TREE_CODE (t))
15145 {
15146 case TEMPLATE_DECL:
15147 r = tsubst_template_decl (t, args, complain,
15148 /*lambda_fntype=*/NULL_TREE,
15149 /*lambda_tparms=*/NULL_TREE);
15150 break;
15151
15152 case FUNCTION_DECL:
15153 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
15154 use_spec_table);
15155 break;
15156
15157 case PARM_DECL:
15158 {
15159 tree type = NULL_TREE;
15160 int i, len = 1;
15161 tree expanded_types = NULL_TREE;
15162 tree prev_r = NULL_TREE;
15163 tree first_r = NULL_TREE;
15164
15165 if (DECL_PACK_P (t))
15166 {
15167 /* If there is a local specialization that isn't a
15168 parameter pack, it means that we're doing a "simple"
15169 substitution from inside tsubst_pack_expansion. Just
15170 return the local specialization (which will be a single
15171 parm). */
15172 tree spec = retrieve_local_specialization (t);
15173 if (spec
15174 && TREE_CODE (spec) == PARM_DECL
15175 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
15176 RETURN (spec);
15177
15178 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15179 the parameters in this function parameter pack. */
15180 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15181 complain, in_decl);
15182 if (TREE_CODE (expanded_types) == TREE_VEC)
15183 {
15184 len = TREE_VEC_LENGTH (expanded_types);
15185
15186 /* Zero-length parameter packs are boring. Just substitute
15187 into the chain. */
15188 if (len == 0 && !cp_unevaluated_operand)
15189 RETURN (tsubst (TREE_CHAIN (t), args, complain,
15190 TREE_CHAIN (t)));
15191 }
15192 else
15193 {
15194 /* All we did was update the type. Make a note of that. */
15195 type = expanded_types;
15196 expanded_types = NULL_TREE;
15197 }
15198 }
15199
15200 /* Loop through all of the parameters we'll build. When T is
15201 a function parameter pack, LEN is the number of expanded
15202 types in EXPANDED_TYPES; otherwise, LEN is 1. */
15203 r = NULL_TREE;
15204 for (i = 0; i < len; ++i)
15205 {
15206 prev_r = r;
15207 r = copy_node (t);
15208 if (DECL_TEMPLATE_PARM_P (t))
15209 SET_DECL_TEMPLATE_PARM_P (r);
15210
15211 if (expanded_types)
15212 /* We're on the Ith parameter of the function parameter
15213 pack. */
15214 {
15215 /* Get the Ith type. */
15216 type = TREE_VEC_ELT (expanded_types, i);
15217
15218 /* Rename the parameter to include the index. */
15219 DECL_NAME (r)
15220 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15221 }
15222 else if (!type)
15223 /* We're dealing with a normal parameter. */
15224 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15225
15226 type = type_decays_to (type);
15227 TREE_TYPE (r) = type;
15228 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15229
15230 if (DECL_INITIAL (r))
15231 {
15232 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
15233 DECL_INITIAL (r) = TREE_TYPE (r);
15234 else
15235 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
15236 complain, in_decl);
15237 }
15238
15239 DECL_CONTEXT (r) = NULL_TREE;
15240
15241 if (!DECL_TEMPLATE_PARM_P (r))
15242 DECL_ARG_TYPE (r) = type_passed_as (type);
15243
15244 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
15245 args, complain, in_decl))
15246 return error_mark_node;
15247
15248 /* Keep track of the first new parameter we
15249 generate. That's what will be returned to the
15250 caller. */
15251 if (!first_r)
15252 first_r = r;
15253
15254 /* Build a proper chain of parameters when substituting
15255 into a function parameter pack. */
15256 if (prev_r)
15257 DECL_CHAIN (prev_r) = r;
15258 }
15259
15260 /* If cp_unevaluated_operand is set, we're just looking for a
15261 single dummy parameter, so don't keep going. */
15262 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
15263 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
15264 complain, DECL_CHAIN (t));
15265
15266 /* FIRST_R contains the start of the chain we've built. */
15267 r = first_r;
15268 }
15269 break;
15270
15271 case FIELD_DECL:
15272 {
15273 tree type = NULL_TREE;
15274 tree vec = NULL_TREE;
15275 tree expanded_types = NULL_TREE;
15276 int len = 1;
15277
15278 if (PACK_EXPANSION_P (TREE_TYPE (t)))
15279 {
15280 /* This field is a lambda capture pack. Return a TREE_VEC of
15281 the expanded fields to instantiate_class_template_1. */
15282 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
15283 complain, in_decl);
15284 if (TREE_CODE (expanded_types) == TREE_VEC)
15285 {
15286 len = TREE_VEC_LENGTH (expanded_types);
15287 vec = make_tree_vec (len);
15288 }
15289 else
15290 {
15291 /* All we did was update the type. Make a note of that. */
15292 type = expanded_types;
15293 expanded_types = NULL_TREE;
15294 }
15295 }
15296
15297 for (int i = 0; i < len; ++i)
15298 {
15299 r = copy_decl (t);
15300 if (expanded_types)
15301 {
15302 type = TREE_VEC_ELT (expanded_types, i);
15303 DECL_NAME (r)
15304 = make_ith_pack_parameter_name (DECL_NAME (r), i);
15305 }
15306 else if (!type)
15307 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15308
15309 if (type == error_mark_node)
15310 RETURN (error_mark_node);
15311 TREE_TYPE (r) = type;
15312 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15313
15314 if (DECL_C_BIT_FIELD (r))
15315 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
15316 number of bits. */
15317 DECL_BIT_FIELD_REPRESENTATIVE (r)
15318 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
15319 complain, in_decl);
15320 if (DECL_INITIAL (t))
15321 {
15322 /* Set up DECL_TEMPLATE_INFO so that we can get at the
15323 NSDMI in perform_member_init. Still set DECL_INITIAL
15324 so that we know there is one. */
15325 DECL_INITIAL (r) = void_node;
15326 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
15327 retrofit_lang_decl (r);
15328 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
15329 }
15330 /* We don't have to set DECL_CONTEXT here; it is set by
15331 finish_member_declaration. */
15332 DECL_CHAIN (r) = NULL_TREE;
15333
15334 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
15335 args, complain, in_decl))
15336 return error_mark_node;
15337
15338 if (vec)
15339 TREE_VEC_ELT (vec, i) = r;
15340 }
15341
15342 if (vec)
15343 r = vec;
15344 }
15345 break;
15346
15347 case USING_DECL:
15348 /* We reach here only for member using decls. We also need to check
15349 uses_template_parms because DECL_DEPENDENT_P is not set for a
15350 using-declaration that designates a member of the current
15351 instantiation (c++/53549). */
15352 if (DECL_DEPENDENT_P (t)
15353 || uses_template_parms (USING_DECL_SCOPE (t)))
15354 {
15355 /* True iff this using-decl was written as a pack expansion
15356 (and a pack appeared in its scope or name). If a pack
15357 appeared in both, we expand the packs separately and
15358 manually merge them. */
15359 bool variadic_p = false;
15360
15361 tree scope = USING_DECL_SCOPE (t);
15362 if (PACK_EXPANSION_P (scope))
15363 {
15364 scope = tsubst_pack_expansion (scope, args,
15365 complain | tf_qualifying_scope,
15366 in_decl);
15367 variadic_p = true;
15368 }
15369 else
15370 scope = tsubst_scope (scope, args, complain, in_decl);
15371
15372 tree name = DECL_NAME (t);
15373 if (IDENTIFIER_CONV_OP_P (name)
15374 && PACK_EXPANSION_P (TREE_TYPE (name)))
15375 {
15376 name = tsubst_pack_expansion (TREE_TYPE (name), args,
15377 complain, in_decl);
15378 if (name == error_mark_node)
15379 {
15380 r = error_mark_node;
15381 break;
15382 }
15383 for (tree& elt : tree_vec_range (name))
15384 elt = make_conv_op_name (elt);
15385 variadic_p = true;
15386 }
15387 else
15388 name = tsubst_name (name, args, complain, in_decl);
15389
15390 int len;
15391 if (!variadic_p)
15392 len = 1;
15393 else if (TREE_CODE (scope) == TREE_VEC
15394 && TREE_CODE (name) == TREE_VEC)
15395 {
15396 if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
15397 {
15398 error ("mismatched argument pack lengths (%d vs %d)",
15399 TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
15400 r = error_mark_node;
15401 break;
15402 }
15403 len = TREE_VEC_LENGTH (scope);
15404 }
15405 else if (TREE_CODE (scope) == TREE_VEC)
15406 len = TREE_VEC_LENGTH (scope);
15407 else /* TREE_CODE (name) == TREE_VEC */
15408 len = TREE_VEC_LENGTH (name);
15409
15410 r = make_tree_vec (len);
15411 for (int i = 0; i < len; ++i)
15412 {
15413 tree escope = (TREE_CODE (scope) == TREE_VEC
15414 ? TREE_VEC_ELT (scope, i)
15415 : scope);
15416 tree ename = (TREE_CODE (name) == TREE_VEC
15417 ? TREE_VEC_ELT (name, i)
15418 : name);
15419 tree elt = do_class_using_decl (escope, ename);
15420 if (!elt)
15421 {
15422 r = error_mark_node;
15423 break;
15424 }
15425 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
15426 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
15427 TREE_VEC_ELT (r, i) = elt;
15428 }
15429
15430 if (!variadic_p && r != error_mark_node)
15431 r = TREE_VEC_ELT (r, 0);
15432 }
15433 else
15434 {
15435 r = copy_node (t);
15436 DECL_CHAIN (r) = NULL_TREE;
15437 }
15438 break;
15439
15440 case TYPE_DECL:
15441 case VAR_DECL:
15442 {
15443 tree argvec = NULL_TREE;
15444 tree gen_tmpl = NULL_TREE;
15445 tree tmpl = NULL_TREE;
15446 tree type = NULL_TREE;
15447
15448 if (TREE_TYPE (t) == error_mark_node)
15449 RETURN (error_mark_node);
15450
15451 if (TREE_CODE (t) == TYPE_DECL
15452 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
15453 {
15454 /* If this is the canonical decl, we don't have to
15455 mess with instantiations, and often we can't (for
15456 typename, template type parms and such). Note that
15457 TYPE_NAME is not correct for the above test if
15458 we've copied the type for a typedef. */
15459 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15460 if (type == error_mark_node)
15461 RETURN (error_mark_node);
15462 r = TYPE_NAME (type);
15463 break;
15464 }
15465
15466 /* Check to see if we already have the specialization we
15467 need. */
15468 tree spec = NULL_TREE;
15469 bool local_p = false;
15470 tree ctx = DECL_CONTEXT (t);
15471 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
15472 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
15473 {
15474 local_p = false;
15475 if (DECL_CLASS_SCOPE_P (t))
15476 {
15477 ctx = tsubst_aggr_type (ctx, args,
15478 complain,
15479 in_decl, /*entering_scope=*/1);
15480 if (DECL_SELF_REFERENCE_P (t))
15481 /* The context and type of an injected-class-name are
15482 the same, so we don't need to substitute both. */
15483 type = ctx;
15484 /* If CTX is unchanged, then T is in fact the
15485 specialization we want. That situation occurs when
15486 referencing a static data member within in its own
15487 class. We can use pointer equality, rather than
15488 same_type_p, because DECL_CONTEXT is always
15489 canonical... */
15490 if (ctx == DECL_CONTEXT (t)
15491 /* ... unless T is a member template; in which
15492 case our caller can be willing to create a
15493 specialization of that template represented
15494 by T. */
15495 && !(DECL_TI_TEMPLATE (t)
15496 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
15497 spec = t;
15498 }
15499
15500 if (!spec)
15501 {
15502 tmpl = DECL_TI_TEMPLATE (t);
15503 if (use_spec_table)
15504 {
15505 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
15506 if (argvec == error_mark_node)
15507 RETURN (error_mark_node);
15508 gen_tmpl = most_general_template (tmpl);
15509 hash = spec_hasher::hash (gen_tmpl, argvec);
15510 spec = retrieve_specialization (gen_tmpl, argvec, hash);
15511 }
15512 else
15513 argvec = args;
15514 }
15515 }
15516 else
15517 {
15518 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
15519 /* Subsequent calls to pushdecl will fill this in. */
15520 ctx = NULL_TREE;
15521 /* A local variable. */
15522 local_p = true;
15523 /* Unless this is a reference to a static variable from an
15524 enclosing function, in which case we need to fill it in now. */
15525 if (TREE_STATIC (t))
15526 {
15527 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
15528 if (fn != current_function_decl)
15529 ctx = fn;
15530 }
15531 spec = retrieve_local_specialization (t);
15532 }
15533 /* If we already have the specialization we need, there is
15534 nothing more to do. */
15535 if (spec)
15536 {
15537 r = spec;
15538 break;
15539 }
15540
15541 /* Create a new node for the specialization we need. */
15542 if (type == NULL_TREE)
15543 {
15544 if (is_typedef_decl (t))
15545 type = DECL_ORIGINAL_TYPE (t);
15546 else
15547 type = TREE_TYPE (t);
15548 if (VAR_P (t)
15549 && VAR_HAD_UNKNOWN_BOUND (t)
15550 && type != error_mark_node)
15551 type = strip_array_domain (type);
15552 tsubst_flags_t tcomplain = complain;
15553 if (VAR_P (t))
15554 tcomplain |= tf_tst_ok;
15555 type = tsubst (type, args, tcomplain, in_decl);
15556 /* Substituting the type might have recursively instantiated this
15557 same alias (c++/86171). */
15558 if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
15559 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
15560 {
15561 r = spec;
15562 break;
15563 }
15564 }
15565 if (type == error_mark_node && !(complain & tf_error))
15566 RETURN (error_mark_node);
15567 r = copy_decl (t);
15568 if (VAR_P (r))
15569 {
15570 DECL_INITIALIZED_P (r) = 0;
15571 DECL_TEMPLATE_INSTANTIATED (r) = 0;
15572 if (TREE_CODE (type) == FUNCTION_TYPE)
15573 {
15574 /* It may seem that this case cannot occur, since:
15575
15576 typedef void f();
15577 void g() { f x; }
15578
15579 declares a function, not a variable. However:
15580
15581 typedef void f();
15582 template <typename T> void g() { T t; }
15583 template void g<f>();
15584
15585 is an attempt to declare a variable with function
15586 type. */
15587 error ("variable %qD has function type",
15588 /* R is not yet sufficiently initialized, so we
15589 just use its name. */
15590 DECL_NAME (r));
15591 RETURN (error_mark_node);
15592 }
15593 type = complete_type (type);
15594 /* Wait until cp_finish_decl to set this again, to handle
15595 circular dependency (template/instantiate6.C). */
15596 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
15597 type = check_var_type (DECL_NAME (r), type,
15598 DECL_SOURCE_LOCATION (r));
15599 if (DECL_HAS_VALUE_EXPR_P (t))
15600 {
15601 tree ve = DECL_VALUE_EXPR (t);
15602 /* If the DECL_VALUE_EXPR is converted to the declared type,
15603 preserve the identity so that gimplify_type_sizes works. */
15604 bool nop = (TREE_CODE (ve) == NOP_EXPR);
15605 if (nop)
15606 ve = TREE_OPERAND (ve, 0);
15607 ve = tsubst_expr (ve, args, complain, in_decl);
15608 if (REFERENCE_REF_P (ve))
15609 {
15610 gcc_assert (TYPE_REF_P (type));
15611 ve = TREE_OPERAND (ve, 0);
15612 }
15613 if (nop)
15614 ve = build_nop (type, ve);
15615 else if (DECL_LANG_SPECIFIC (t)
15616 && DECL_OMP_PRIVATIZED_MEMBER (t)
15617 && TREE_CODE (ve) == COMPONENT_REF
15618 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
15619 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
15620 type = TREE_TYPE (ve);
15621 else
15622 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
15623 == TYPE_MAIN_VARIANT (type));
15624 SET_DECL_VALUE_EXPR (r, ve);
15625 }
15626 if (CP_DECL_THREAD_LOCAL_P (r)
15627 && !processing_template_decl)
15628 set_decl_tls_model (r, decl_default_tls_model (r));
15629 }
15630 else if (DECL_SELF_REFERENCE_P (t))
15631 SET_DECL_SELF_REFERENCE_P (r);
15632 TREE_TYPE (r) = type;
15633 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15634 DECL_CONTEXT (r) = ctx;
15635 /* Clear out the mangled name and RTL for the instantiation. */
15636 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
15637 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
15638 SET_DECL_RTL (r, NULL);
15639 set_instantiating_module (r);
15640
15641 /* The initializer must not be expanded until it is required;
15642 see [temp.inst]. */
15643 DECL_INITIAL (r) = NULL_TREE;
15644 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
15645 if (VAR_P (r))
15646 {
15647 if (DECL_LANG_SPECIFIC (r))
15648 SET_DECL_DEPENDENT_INIT_P (r, false);
15649
15650 SET_DECL_MODE (r, VOIDmode);
15651
15652 /* Possibly limit visibility based on template args. */
15653 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15654 if (DECL_VISIBILITY_SPECIFIED (t))
15655 {
15656 DECL_VISIBILITY_SPECIFIED (r) = 0;
15657 DECL_ATTRIBUTES (r)
15658 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15659 }
15660 determine_visibility (r);
15661 if ((!local_p || TREE_STATIC (t)) && DECL_SECTION_NAME (t))
15662 set_decl_section_name (r, t);
15663 }
15664
15665 if (!local_p)
15666 {
15667 /* A static data member declaration is always marked
15668 external when it is declared in-class, even if an
15669 initializer is present. We mimic the non-template
15670 processing here. */
15671 DECL_EXTERNAL (r) = 1;
15672 if (DECL_NAMESPACE_SCOPE_P (t))
15673 DECL_NOT_REALLY_EXTERN (r) = 1;
15674
15675 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
15676 SET_DECL_IMPLICIT_INSTANTIATION (r);
15677 if (use_spec_table)
15678 register_specialization (r, gen_tmpl, argvec, false, hash);
15679 }
15680 else
15681 {
15682 if (DECL_LANG_SPECIFIC (r))
15683 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15684 if (!cp_unevaluated_operand)
15685 register_local_specialization (r, t);
15686 }
15687
15688 DECL_CHAIN (r) = NULL_TREE;
15689
15690 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
15691 /*flags=*/0,
15692 args, complain, in_decl))
15693 return error_mark_node;
15694
15695 /* Preserve a typedef that names a type. */
15696 if (is_typedef_decl (r) && type != error_mark_node)
15697 {
15698 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
15699 set_underlying_type (r);
15700
15701 /* common_handle_aligned_attribute doesn't apply the alignment
15702 to DECL_ORIGINAL_TYPE. */
15703 if (TYPE_USER_ALIGN (TREE_TYPE (t)))
15704 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
15705 TYPE_ALIGN (TREE_TYPE (t)));
15706 }
15707
15708 layout_decl (r, 0);
15709 }
15710 break;
15711
15712 default:
15713 gcc_unreachable ();
15714 }
15715 #undef RETURN
15716
15717 out:
15718 /* Restore the file and line information. */
15719 input_location = saved_loc;
15720
15721 return r;
15722 }
15723
15724 /* Substitute into the complete parameter type list PARMS. */
15725
15726 tree
15727 tsubst_function_parms (tree parms,
15728 tree args,
15729 tsubst_flags_t complain,
15730 tree in_decl)
15731 {
15732 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
15733 }
15734
15735 /* Substitute into the ARG_TYPES of a function type.
15736 If END is a TREE_CHAIN, leave it and any following types
15737 un-substituted. */
15738
15739 static tree
15740 tsubst_arg_types (tree arg_types,
15741 tree args,
15742 tree end,
15743 tsubst_flags_t complain,
15744 tree in_decl)
15745 {
15746 tree type = NULL_TREE;
15747 int len = 1;
15748 tree expanded_args = NULL_TREE;
15749
15750 if (!arg_types || arg_types == void_list_node || arg_types == end)
15751 return arg_types;
15752
15753 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
15754 {
15755 /* For a pack expansion, perform substitution on the
15756 entire expression. Later on, we'll handle the arguments
15757 one-by-one. */
15758 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
15759 args, complain, in_decl);
15760
15761 if (TREE_CODE (expanded_args) == TREE_VEC)
15762 /* So that we'll spin through the parameters, one by one. */
15763 len = TREE_VEC_LENGTH (expanded_args);
15764 else
15765 {
15766 /* We only partially substituted into the parameter
15767 pack. Our type is TYPE_PACK_EXPANSION. */
15768 type = expanded_args;
15769 expanded_args = NULL_TREE;
15770 }
15771 }
15772 else
15773 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
15774
15775 /* Check if a substituted type is erroneous before substituting into
15776 the rest of the chain. */
15777 for (int i = 0; i < len; i++)
15778 {
15779 if (expanded_args)
15780 type = TREE_VEC_ELT (expanded_args, i);
15781
15782 if (type == error_mark_node)
15783 return error_mark_node;
15784 if (VOID_TYPE_P (type))
15785 {
15786 if (complain & tf_error)
15787 {
15788 error ("invalid parameter type %qT", type);
15789 if (in_decl)
15790 error ("in declaration %q+D", in_decl);
15791 }
15792 return error_mark_node;
15793 }
15794 }
15795
15796 /* We do not substitute into default arguments here. The standard
15797 mandates that they be instantiated only when needed, which is
15798 done in build_over_call. */
15799 tree default_arg = TREE_PURPOSE (arg_types);
15800
15801 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15802 since the new op() won't have any associated template arguments for us
15803 to refer to later. */
15804 if (lambda_fn_in_template_p (in_decl)
15805 || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
15806 && DECL_LOCAL_DECL_P (in_decl)))
15807 default_arg = tsubst_expr (default_arg, args, complain, in_decl);
15808
15809 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15810 args, end, complain, in_decl);
15811 if (remaining_arg_types == error_mark_node)
15812 return error_mark_node;
15813
15814 for (int i = len-1; i >= 0; i--)
15815 {
15816 if (expanded_args)
15817 type = TREE_VEC_ELT (expanded_args, i);
15818
15819 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15820 top-level qualifiers as required. */
15821 type = cv_unqualified (type_decays_to (type));
15822
15823 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15824 {
15825 /* We've instantiated a template before its default arguments
15826 have been parsed. This can happen for a nested template
15827 class, and is not an error unless we require the default
15828 argument in a call of this function. */
15829 remaining_arg_types
15830 = tree_cons (default_arg, type, remaining_arg_types);
15831 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15832 remaining_arg_types);
15833 }
15834 else
15835 remaining_arg_types
15836 = hash_tree_cons (default_arg, type, remaining_arg_types);
15837 }
15838
15839 return remaining_arg_types;
15840 }
15841
15842 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15843 *not* handle the exception-specification for FNTYPE, because the
15844 initial substitution of explicitly provided template parameters
15845 during argument deduction forbids substitution into the
15846 exception-specification:
15847
15848 [temp.deduct]
15849
15850 All references in the function type of the function template to the
15851 corresponding template parameters are replaced by the specified tem-
15852 plate argument values. If a substitution in a template parameter or
15853 in the function type of the function template results in an invalid
15854 type, type deduction fails. [Note: The equivalent substitution in
15855 exception specifications is done only when the function is instanti-
15856 ated, at which point a program is ill-formed if the substitution
15857 results in an invalid type.] */
15858
15859 static tree
15860 tsubst_function_type (tree t,
15861 tree args,
15862 tsubst_flags_t complain,
15863 tree in_decl)
15864 {
15865 tree return_type;
15866 tree arg_types = NULL_TREE;
15867
15868 /* The TYPE_CONTEXT is not used for function/method types. */
15869 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15870
15871 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15872 failure. */
15873 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15874
15875 if (late_return_type_p)
15876 {
15877 /* Substitute the argument types. */
15878 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15879 complain, in_decl);
15880 if (arg_types == error_mark_node)
15881 return error_mark_node;
15882
15883 tree save_ccp = current_class_ptr;
15884 tree save_ccr = current_class_ref;
15885 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15886 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15887 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15888 if (do_inject)
15889 {
15890 /* DR 1207: 'this' is in scope in the trailing return type. */
15891 inject_this_parameter (this_type, cp_type_quals (this_type));
15892 }
15893
15894 /* Substitute the return type. */
15895 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15896
15897 if (do_inject)
15898 {
15899 current_class_ptr = save_ccp;
15900 current_class_ref = save_ccr;
15901 }
15902 }
15903 else
15904 /* Substitute the return type. */
15905 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15906
15907 if (return_type == error_mark_node)
15908 return error_mark_node;
15909 /* DR 486 clarifies that creation of a function type with an
15910 invalid return type is a deduction failure. */
15911 if (TREE_CODE (return_type) == ARRAY_TYPE
15912 || TREE_CODE (return_type) == FUNCTION_TYPE)
15913 {
15914 if (complain & tf_error)
15915 {
15916 if (TREE_CODE (return_type) == ARRAY_TYPE)
15917 error ("function returning an array");
15918 else
15919 error ("function returning a function");
15920 }
15921 return error_mark_node;
15922 }
15923
15924 if (!late_return_type_p)
15925 {
15926 /* Substitute the argument types. */
15927 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15928 complain, in_decl);
15929 if (arg_types == error_mark_node)
15930 return error_mark_node;
15931 }
15932
15933 /* Construct a new type node and return it. */
15934 return rebuild_function_or_method_type (t, return_type, arg_types,
15935 /*raises=*/NULL_TREE, complain);
15936 }
15937
15938 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15939 ARGS into that specification, and return the substituted
15940 specification. If there is no specification, return NULL_TREE. */
15941
15942 static tree
15943 tsubst_exception_specification (tree fntype,
15944 tree args,
15945 tsubst_flags_t complain,
15946 tree in_decl,
15947 bool defer_ok)
15948 {
15949 tree specs;
15950 tree new_specs;
15951
15952 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15953 new_specs = NULL_TREE;
15954 if (specs && TREE_PURPOSE (specs))
15955 {
15956 /* A noexcept-specifier. */
15957 tree expr = TREE_PURPOSE (specs);
15958 if (TREE_CODE (expr) == INTEGER_CST)
15959 new_specs = expr;
15960 else if (defer_ok)
15961 {
15962 /* Defer instantiation of noexcept-specifiers to avoid
15963 excessive instantiations (c++/49107). */
15964 new_specs = make_node (DEFERRED_NOEXCEPT);
15965 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15966 {
15967 /* We already partially instantiated this member template,
15968 so combine the new args with the old. */
15969 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15970 = DEFERRED_NOEXCEPT_PATTERN (expr);
15971 DEFERRED_NOEXCEPT_ARGS (new_specs)
15972 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15973 }
15974 else
15975 {
15976 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15977 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15978 }
15979 }
15980 else
15981 {
15982 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15983 {
15984 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15985 args);
15986 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15987 }
15988 new_specs = tsubst_expr (expr, args, complain, in_decl);
15989 }
15990 new_specs = build_noexcept_spec (new_specs, complain);
15991 /* We've instantiated a template before a noexcept-specifier
15992 contained therein has been parsed. This can happen for
15993 a nested template class:
15994
15995 struct S {
15996 template<typename> struct B { B() noexcept(...); };
15997 struct A : B<int> { ... use B() ... };
15998 };
15999
16000 where completing B<int> will trigger instantiating the
16001 noexcept, even though we only parse it at the end of S. */
16002 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
16003 {
16004 gcc_checking_assert (defer_ok);
16005 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
16006 }
16007 }
16008 else if (specs)
16009 {
16010 if (! TREE_VALUE (specs))
16011 new_specs = specs;
16012 else
16013 while (specs)
16014 {
16015 tree spec;
16016 int i, len = 1;
16017 tree expanded_specs = NULL_TREE;
16018
16019 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
16020 {
16021 /* Expand the pack expansion type. */
16022 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
16023 args, complain,
16024 in_decl);
16025
16026 if (expanded_specs == error_mark_node)
16027 return error_mark_node;
16028 else if (TREE_CODE (expanded_specs) == TREE_VEC)
16029 len = TREE_VEC_LENGTH (expanded_specs);
16030 else
16031 {
16032 /* We're substituting into a member template, so
16033 we got a TYPE_PACK_EXPANSION back. Add that
16034 expansion and move on. */
16035 gcc_assert (TREE_CODE (expanded_specs)
16036 == TYPE_PACK_EXPANSION);
16037 new_specs = add_exception_specifier (new_specs,
16038 expanded_specs,
16039 complain);
16040 specs = TREE_CHAIN (specs);
16041 continue;
16042 }
16043 }
16044
16045 for (i = 0; i < len; ++i)
16046 {
16047 if (expanded_specs)
16048 spec = TREE_VEC_ELT (expanded_specs, i);
16049 else
16050 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
16051 if (spec == error_mark_node)
16052 return spec;
16053 new_specs = add_exception_specifier (new_specs, spec,
16054 complain);
16055 }
16056
16057 specs = TREE_CHAIN (specs);
16058 }
16059 }
16060 return new_specs;
16061 }
16062
16063 /* Substitute through a TREE_LIST of types or expressions, handling pack
16064 expansions. */
16065
16066 tree
16067 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16068 {
16069 if (t == void_list_node)
16070 return t;
16071
16072 tree purpose = TREE_PURPOSE (t);
16073 tree purposevec = NULL_TREE;
16074 if (!purpose)
16075 ;
16076 else if (PACK_EXPANSION_P (purpose))
16077 {
16078 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
16079 if (TREE_CODE (purpose) == TREE_VEC)
16080 purposevec = purpose;
16081 }
16082 else if (TYPE_P (purpose))
16083 purpose = tsubst (purpose, args, complain, in_decl);
16084 else
16085 purpose = tsubst_expr (purpose, args, complain, in_decl);
16086 if (purpose == error_mark_node || purposevec == error_mark_node)
16087 return error_mark_node;
16088
16089 tree value = TREE_VALUE (t);
16090 tree valuevec = NULL_TREE;
16091 if (!value)
16092 ;
16093 else if (PACK_EXPANSION_P (value))
16094 {
16095 value = tsubst_pack_expansion (value, args, complain, in_decl);
16096 if (TREE_CODE (value) == TREE_VEC)
16097 valuevec = value;
16098 }
16099 else if (TYPE_P (value))
16100 value = tsubst (value, args, complain, in_decl);
16101 else
16102 value = tsubst_expr (value, args, complain, in_decl);
16103 if (value == error_mark_node || valuevec == error_mark_node)
16104 return error_mark_node;
16105
16106 tree chain = TREE_CHAIN (t);
16107 if (!chain)
16108 ;
16109 else if (TREE_CODE (chain) == TREE_LIST)
16110 chain = tsubst_tree_list (chain, args, complain, in_decl);
16111 else if (TYPE_P (chain))
16112 chain = tsubst (chain, args, complain, in_decl);
16113 else
16114 chain = tsubst_expr (chain, args, complain, in_decl);
16115 if (chain == error_mark_node)
16116 return error_mark_node;
16117
16118 if (purpose == TREE_PURPOSE (t)
16119 && value == TREE_VALUE (t)
16120 && chain == TREE_CHAIN (t))
16121 return t;
16122
16123 int len;
16124 /* Determine the number of arguments. */
16125 if (purposevec)
16126 {
16127 len = TREE_VEC_LENGTH (purposevec);
16128 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16129 }
16130 else if (valuevec)
16131 len = TREE_VEC_LENGTH (valuevec);
16132 else
16133 len = 1;
16134
16135 for (int i = len; i-- > 0; )
16136 {
16137 if (purposevec)
16138 purpose = TREE_VEC_ELT (purposevec, i);
16139 if (valuevec)
16140 value = TREE_VEC_ELT (valuevec, i);
16141
16142 if (value && TYPE_P (value))
16143 chain = hash_tree_cons (purpose, value, chain);
16144 else
16145 chain = tree_cons (purpose, value, chain);
16146 }
16147
16148 return chain;
16149 }
16150
16151 /* Take the tree structure T and replace template parameters used
16152 therein with the argument vector ARGS. IN_DECL is an associated
16153 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
16154 Issue error and warning messages under control of COMPLAIN. Note
16155 that we must be relatively non-tolerant of extensions here, in
16156 order to preserve conformance; if we allow substitutions that
16157 should not be allowed, we may allow argument deductions that should
16158 not succeed, and therefore report ambiguous overload situations
16159 where there are none. In theory, we could allow the substitution,
16160 but indicate that it should have failed, and allow our caller to
16161 make sure that the right thing happens, but we don't try to do this
16162 yet.
16163
16164 This function is used for dealing with types, decls and the like;
16165 for expressions, use tsubst_expr or tsubst_copy. */
16166
16167 tree
16168 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16169 {
16170 enum tree_code code;
16171 tree type, r = NULL_TREE;
16172
16173 if (t == NULL_TREE || t == error_mark_node
16174 || t == integer_type_node
16175 || t == void_type_node
16176 || t == char_type_node
16177 || t == unknown_type_node
16178 || TREE_CODE (t) == NAMESPACE_DECL
16179 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
16180 return t;
16181
16182 tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
16183 complain &= ~tf_tst_ok;
16184
16185 tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
16186 complain &= ~tf_qualifying_scope;
16187
16188 if (DECL_P (t))
16189 return tsubst_decl (t, args, complain);
16190
16191 if (args == NULL_TREE)
16192 return t;
16193
16194 code = TREE_CODE (t);
16195
16196 gcc_assert (code != IDENTIFIER_NODE);
16197 type = TREE_TYPE (t);
16198
16199 gcc_assert (type != unknown_type_node);
16200
16201 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
16202 return d;
16203
16204 /* Reuse typedefs. We need to do this to handle dependent attributes,
16205 such as attribute aligned. */
16206 if (TYPE_P (t)
16207 && typedef_variant_p (t))
16208 {
16209 tree decl = TYPE_NAME (t);
16210
16211 if (alias_template_specialization_p (t, nt_opaque))
16212 {
16213 /* DECL represents an alias template and we want to
16214 instantiate it. */
16215 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16216 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16217 r = instantiate_alias_template (tmpl, gen_args, complain);
16218 }
16219 else if (DECL_CLASS_SCOPE_P (decl)
16220 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
16221 && uses_template_parms (DECL_CONTEXT (decl)))
16222 {
16223 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16224 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
16225 r = retrieve_specialization (tmpl, gen_args, 0);
16226 }
16227 else if (DECL_FUNCTION_SCOPE_P (decl)
16228 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
16229 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
16230 r = retrieve_local_specialization (decl);
16231 else
16232 /* The typedef is from a non-template context. */
16233 return t;
16234
16235 if (r)
16236 {
16237 r = TREE_TYPE (r);
16238 r = cp_build_qualified_type
16239 (r, cp_type_quals (t) | cp_type_quals (r),
16240 complain | tf_ignore_bad_quals);
16241 return r;
16242 }
16243 else
16244 {
16245 /* We don't have an instantiation yet, so drop the typedef. */
16246 int quals = cp_type_quals (t);
16247 t = DECL_ORIGINAL_TYPE (decl);
16248 t = cp_build_qualified_type (t, quals,
16249 complain | tf_ignore_bad_quals);
16250 }
16251 }
16252
16253 bool fndecl_type = (complain & tf_fndecl_type);
16254 complain &= ~tf_fndecl_type;
16255
16256 if (type
16257 && code != TYPENAME_TYPE
16258 && code != TEMPLATE_TYPE_PARM
16259 && code != TEMPLATE_PARM_INDEX
16260 && code != IDENTIFIER_NODE
16261 && code != FUNCTION_TYPE
16262 && code != METHOD_TYPE)
16263 type = tsubst (type, args, complain, in_decl);
16264 if (type == error_mark_node)
16265 return error_mark_node;
16266
16267 switch (code)
16268 {
16269 case RECORD_TYPE:
16270 if (TYPE_PTRMEMFUNC_P (t))
16271 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
16272 /* Fall through. */
16273 case UNION_TYPE:
16274 case ENUMERAL_TYPE:
16275 return tsubst_aggr_type_1 (t, args, complain, in_decl,
16276 /*entering_scope=*/0);
16277
16278 case ERROR_MARK:
16279 case IDENTIFIER_NODE:
16280 case VOID_TYPE:
16281 case OPAQUE_TYPE:
16282 case REAL_TYPE:
16283 case COMPLEX_TYPE:
16284 case VECTOR_TYPE:
16285 case BOOLEAN_TYPE:
16286 case NULLPTR_TYPE:
16287 case LANG_TYPE:
16288 return t;
16289
16290 case INTEGER_TYPE:
16291 if (t == integer_type_node)
16292 return t;
16293
16294 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
16295 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
16296 return t;
16297
16298 {
16299 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
16300
16301 max = tsubst_expr (omax, args, complain, in_decl);
16302
16303 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
16304 needed. */
16305 if (TREE_CODE (max) == NOP_EXPR
16306 && TREE_SIDE_EFFECTS (omax)
16307 && !TREE_TYPE (max))
16308 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
16309
16310 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
16311 with TREE_SIDE_EFFECTS that indicates this is not an integral
16312 constant expression. */
16313 if (processing_template_decl
16314 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
16315 {
16316 gcc_assert (TREE_CODE (max) == NOP_EXPR);
16317 TREE_SIDE_EFFECTS (max) = 1;
16318 }
16319
16320 return compute_array_index_type (NULL_TREE, max, complain);
16321 }
16322
16323 case TEMPLATE_TYPE_PARM:
16324 if (TEMPLATE_TYPE_LEVEL (t) == 0)
16325 {
16326 /* This is either an ordinary level-less auto or a CTAD placeholder
16327 auto. These get replaced only via do_auto_deduction which, in the
16328 ordinary case, temporarily overrides its level to 1 before calling
16329 tsubst. CTAD placeholders are replaced via do_class_deduction. */
16330 gcc_checking_assert (is_auto (t));
16331 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
16332 if (!tmpl)
16333 /* Ordinary level-less auto has nothing to substitute. */
16334 return t;
16335
16336 /* Substitute the template of this CTAD placeholder. */
16337 tmpl = tsubst_expr (tmpl, args, complain, in_decl);
16338 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16339 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
16340
16341 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
16342 return make_template_placeholder (tmpl);
16343 else
16344 return t;
16345 }
16346 /* Fall through. */
16347 case TEMPLATE_TEMPLATE_PARM:
16348 case BOUND_TEMPLATE_TEMPLATE_PARM:
16349 case TEMPLATE_PARM_INDEX:
16350 {
16351 int idx;
16352 int level;
16353 int levels;
16354 tree arg = NULL_TREE;
16355
16356 r = NULL_TREE;
16357
16358 gcc_assert (TREE_VEC_LENGTH (args) > 0);
16359 template_parm_level_and_index (t, &level, &idx);
16360
16361 levels = TMPL_ARGS_DEPTH (args);
16362 if (level <= levels
16363 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
16364 {
16365 arg = TMPL_ARG (args, level, idx);
16366
16367 /* See through ARGUMENT_PACK_SELECT arguments. */
16368 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
16369 arg = argument_pack_select_arg (arg);
16370 }
16371
16372 if (arg == error_mark_node)
16373 return error_mark_node;
16374 else if (arg != NULL_TREE)
16375 {
16376 if (ARGUMENT_PACK_P (arg))
16377 /* If ARG is an argument pack, we don't actually want to
16378 perform a substitution here, because substitutions
16379 for argument packs are only done
16380 element-by-element. We can get to this point when
16381 substituting the type of a non-type template
16382 parameter pack, when that type actually contains
16383 template parameter packs from an outer template, e.g.,
16384
16385 template<typename... Types> struct A {
16386 template<Types... Values> struct B { };
16387 }; */
16388 return t;
16389
16390 if (code == TEMPLATE_TYPE_PARM)
16391 {
16392 int quals;
16393
16394 /* When building concept checks for the purpose of
16395 deducing placeholders, we can end up with wildcards
16396 where types are expected. Adjust this to the deduced
16397 value. */
16398 if (TREE_CODE (arg) == WILDCARD_DECL)
16399 arg = TREE_TYPE (TREE_TYPE (arg));
16400
16401 gcc_assert (TYPE_P (arg));
16402
16403 quals = cp_type_quals (arg) | cp_type_quals (t);
16404
16405 return cp_build_qualified_type
16406 (arg, quals, complain | tf_ignore_bad_quals);
16407 }
16408 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
16409 {
16410 /* We are processing a type constructed from a
16411 template template parameter. */
16412 tree argvec = tsubst (TYPE_TI_ARGS (t),
16413 args, complain, in_decl);
16414 if (argvec == error_mark_node)
16415 return error_mark_node;
16416
16417 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
16418 || TREE_CODE (arg) == TEMPLATE_DECL
16419 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
16420
16421 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
16422 /* Consider this code:
16423
16424 template <template <class> class Template>
16425 struct Internal {
16426 template <class Arg> using Bind = Template<Arg>;
16427 };
16428
16429 template <template <class> class Template, class Arg>
16430 using Instantiate = Template<Arg>; //#0
16431
16432 template <template <class> class Template,
16433 class Argument>
16434 using Bind =
16435 Instantiate<Internal<Template>::template Bind,
16436 Argument>; //#1
16437
16438 When #1 is parsed, the
16439 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16440 parameter `Template' in #0 matches the
16441 UNBOUND_CLASS_TEMPLATE representing the argument
16442 `Internal<Template>::template Bind'; We then want
16443 to assemble the type `Bind<Argument>' that can't
16444 be fully created right now, because
16445 `Internal<Template>' not being complete, the Bind
16446 template cannot be looked up in that context. So
16447 we need to "store" `Bind<Argument>' for later
16448 when the context of Bind becomes complete. Let's
16449 store that in a TYPENAME_TYPE. */
16450 return make_typename_type (TYPE_CONTEXT (arg),
16451 build_nt (TEMPLATE_ID_EXPR,
16452 TYPE_IDENTIFIER (arg),
16453 argvec),
16454 typename_type,
16455 complain);
16456
16457 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16458 are resolving nested-types in the signature of a
16459 member function templates. Otherwise ARG is a
16460 TEMPLATE_DECL and is the real template to be
16461 instantiated. */
16462 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16463 arg = TYPE_NAME (arg);
16464
16465 r = lookup_template_class (arg,
16466 argvec, in_decl,
16467 DECL_CONTEXT (arg),
16468 /*entering_scope=*/0,
16469 complain);
16470 return cp_build_qualified_type
16471 (r, cp_type_quals (t) | cp_type_quals (r), complain);
16472 }
16473 else if (code == TEMPLATE_TEMPLATE_PARM)
16474 return arg;
16475 else
16476 /* TEMPLATE_PARM_INDEX. */
16477 return convert_from_reference (unshare_expr (arg));
16478 }
16479
16480 if (level == 1)
16481 /* This can happen during the attempted tsubst'ing in
16482 unify. This means that we don't yet have any information
16483 about the template parameter in question. */
16484 return t;
16485
16486 /* Early in template argument deduction substitution, we don't
16487 want to reduce the level of 'auto', or it will be confused
16488 with a normal template parm in subsequent deduction.
16489 Similarly, don't reduce the level of template parameters to
16490 avoid mismatches when deducing their types. */
16491 if (complain & tf_partial)
16492 return t;
16493
16494 /* If we get here, we must have been looking at a parm for a
16495 more deeply nested template. Make a new version of this
16496 template parameter, but with a lower level. */
16497 int quals;
16498 switch (code)
16499 {
16500 case TEMPLATE_TYPE_PARM:
16501 case TEMPLATE_TEMPLATE_PARM:
16502 quals = cp_type_quals (t);
16503 if (quals)
16504 {
16505 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
16506 t = TYPE_MAIN_VARIANT (t);
16507 }
16508
16509 if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
16510 if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
16511 && (code == TEMPLATE_TYPE_PARM
16512 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
16513 /* Cache lowering a type parameter or a simple template
16514 template parameter. */
16515 r = TREE_TYPE (d);
16516
16517 if (!r)
16518 {
16519 r = copy_type (t);
16520 TEMPLATE_TYPE_PARM_INDEX (r)
16521 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
16522 r, levels, args, complain);
16523 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
16524 TYPE_MAIN_VARIANT (r) = r;
16525 TYPE_POINTER_TO (r) = NULL_TREE;
16526 TYPE_REFERENCE_TO (r) = NULL_TREE;
16527
16528 if (code == TEMPLATE_TYPE_PARM)
16529 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
16530 /* Propagate constraints on placeholders since they are
16531 only instantiated during satisfaction. */
16532 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
16533
16534 if (TYPE_STRUCTURAL_EQUALITY_P (t))
16535 SET_TYPE_STRUCTURAL_EQUALITY (r);
16536 else
16537 TYPE_CANONICAL (r) = canonical_type_parameter (r);
16538 }
16539
16540 if (quals)
16541 r = cp_build_qualified_type (r, quals,
16542 complain | tf_ignore_bad_quals);
16543 break;
16544
16545 case BOUND_TEMPLATE_TEMPLATE_PARM:
16546 {
16547 tree tinfo = TYPE_TEMPLATE_INFO (t);
16548 /* We might need to substitute into the types of non-type
16549 template parameters. This also lowers the level of
16550 the ttp appropriately. */
16551 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
16552 complain, in_decl);
16553 if (tmpl == error_mark_node)
16554 return error_mark_node;
16555 tree argvec = tsubst (TI_ARGS (tinfo), args,
16556 complain, in_decl);
16557 if (argvec == error_mark_node)
16558 return error_mark_node;
16559 r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
16560 /*entering_scope=*/false, complain);
16561 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16562 break;
16563 }
16564
16565 case TEMPLATE_PARM_INDEX:
16566 /* OK, now substitute the type of the non-type parameter. We
16567 couldn't do it earlier because it might be an auto parameter,
16568 and we wouldn't need to if we had an argument. */
16569 type = tsubst (type, args, complain, in_decl);
16570 if (type == error_mark_node)
16571 return error_mark_node;
16572 r = reduce_template_parm_level (t, type, levels, args, complain);
16573 break;
16574
16575 default:
16576 gcc_unreachable ();
16577 }
16578
16579 return r;
16580 }
16581
16582 case TREE_LIST:
16583 return tsubst_tree_list (t, args, complain, in_decl);
16584
16585 case TREE_BINFO:
16586 /* We should never be tsubsting a binfo. */
16587 gcc_unreachable ();
16588
16589 case TREE_VEC:
16590 /* A vector of template arguments. */
16591 gcc_assert (!type);
16592 return tsubst_template_args (t, args, complain, in_decl);
16593
16594 case POINTER_TYPE:
16595 case REFERENCE_TYPE:
16596 {
16597 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
16598 return t;
16599
16600 /* [temp.deduct]
16601
16602 Type deduction may fail for any of the following
16603 reasons:
16604
16605 -- Attempting to create a pointer to reference type.
16606 -- Attempting to create a reference to a reference type or
16607 a reference to void.
16608
16609 Core issue 106 says that creating a reference to a reference
16610 during instantiation is no longer a cause for failure. We
16611 only enforce this check in strict C++98 mode. */
16612 if ((TYPE_REF_P (type)
16613 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
16614 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
16615 {
16616 static location_t last_loc;
16617
16618 /* We keep track of the last time we issued this error
16619 message to avoid spewing a ton of messages during a
16620 single bad template instantiation. */
16621 if (complain & tf_error
16622 && last_loc != input_location)
16623 {
16624 if (VOID_TYPE_P (type))
16625 error ("forming reference to void");
16626 else if (code == POINTER_TYPE)
16627 error ("forming pointer to reference type %qT", type);
16628 else
16629 error ("forming reference to reference type %qT", type);
16630 last_loc = input_location;
16631 }
16632
16633 return error_mark_node;
16634 }
16635 else if (TREE_CODE (type) == FUNCTION_TYPE
16636 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
16637 || type_memfn_rqual (type) != REF_QUAL_NONE))
16638 {
16639 if (complain & tf_error)
16640 {
16641 if (code == POINTER_TYPE)
16642 error ("forming pointer to qualified function type %qT",
16643 type);
16644 else
16645 error ("forming reference to qualified function type %qT",
16646 type);
16647 }
16648 return error_mark_node;
16649 }
16650 else if (code == POINTER_TYPE)
16651 {
16652 r = build_pointer_type (type);
16653 if (TREE_CODE (type) == METHOD_TYPE)
16654 r = build_ptrmemfunc_type (r);
16655 }
16656 else if (TYPE_REF_P (type))
16657 /* In C++0x, during template argument substitution, when there is an
16658 attempt to create a reference to a reference type, reference
16659 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16660
16661 "If a template-argument for a template-parameter T names a type
16662 that is a reference to a type A, an attempt to create the type
16663 'lvalue reference to cv T' creates the type 'lvalue reference to
16664 A,' while an attempt to create the type type rvalue reference to
16665 cv T' creates the type T"
16666 */
16667 r = cp_build_reference_type
16668 (TREE_TYPE (type),
16669 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
16670 else
16671 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
16672 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16673
16674 if (r != error_mark_node)
16675 /* Will this ever be needed for TYPE_..._TO values? */
16676 layout_type (r);
16677
16678 return r;
16679 }
16680 case OFFSET_TYPE:
16681 {
16682 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
16683 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
16684 {
16685 /* [temp.deduct]
16686
16687 Type deduction may fail for any of the following
16688 reasons:
16689
16690 -- Attempting to create "pointer to member of T" when T
16691 is not a class type. */
16692 if (complain & tf_error)
16693 error ("creating pointer to member of non-class type %qT", r);
16694 return error_mark_node;
16695 }
16696 if (TYPE_REF_P (type))
16697 {
16698 if (complain & tf_error)
16699 error ("creating pointer to member reference type %qT", type);
16700 return error_mark_node;
16701 }
16702 if (VOID_TYPE_P (type))
16703 {
16704 if (complain & tf_error)
16705 error ("creating pointer to member of type void");
16706 return error_mark_node;
16707 }
16708 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
16709 if (TREE_CODE (type) == FUNCTION_TYPE)
16710 {
16711 /* The type of the implicit object parameter gets its
16712 cv-qualifiers from the FUNCTION_TYPE. */
16713 tree memptr;
16714 tree method_type
16715 = build_memfn_type (type, r, type_memfn_quals (type),
16716 type_memfn_rqual (type));
16717 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
16718 return cp_build_qualified_type (memptr, cp_type_quals (t),
16719 complain);
16720 }
16721 else
16722 return cp_build_qualified_type (build_ptrmem_type (r, type),
16723 cp_type_quals (t),
16724 complain);
16725 }
16726 case FUNCTION_TYPE:
16727 case METHOD_TYPE:
16728 {
16729 tree fntype;
16730 tree specs;
16731 fntype = tsubst_function_type (t, args, complain, in_decl);
16732 if (fntype == error_mark_node)
16733 return error_mark_node;
16734
16735 /* Substitute the exception specification. */
16736 specs = tsubst_exception_specification (t, args, complain, in_decl,
16737 /*defer_ok*/fndecl_type);
16738 if (specs == error_mark_node)
16739 return error_mark_node;
16740 if (specs)
16741 fntype = build_exception_variant (fntype, specs);
16742 return fntype;
16743 }
16744 case ARRAY_TYPE:
16745 {
16746 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
16747 if (domain == error_mark_node)
16748 return error_mark_node;
16749
16750 /* As an optimization, we avoid regenerating the array type if
16751 it will obviously be the same as T. */
16752 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
16753 return t;
16754
16755 /* These checks should match the ones in create_array_type_for_decl.
16756
16757 [temp.deduct]
16758
16759 The deduction may fail for any of the following reasons:
16760
16761 -- Attempting to create an array with an element type that
16762 is void, a function type, or a reference type, or [DR337]
16763 an abstract class type. */
16764 if (VOID_TYPE_P (type)
16765 || TREE_CODE (type) == FUNCTION_TYPE
16766 || (TREE_CODE (type) == ARRAY_TYPE
16767 && TYPE_DOMAIN (type) == NULL_TREE)
16768 || TYPE_REF_P (type))
16769 {
16770 if (complain & tf_error)
16771 error ("creating array of %qT", type);
16772 return error_mark_node;
16773 }
16774
16775 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
16776 !(complain & tf_error)))
16777 return error_mark_node;
16778
16779 r = build_cplus_array_type (type, domain);
16780
16781 if (!valid_array_size_p (input_location, r, in_decl,
16782 (complain & tf_error)))
16783 return error_mark_node;
16784
16785 if (TYPE_USER_ALIGN (t))
16786 {
16787 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
16788 TYPE_USER_ALIGN (r) = 1;
16789 }
16790
16791 return r;
16792 }
16793
16794 case TYPENAME_TYPE:
16795 {
16796 tree ctx = TYPE_CONTEXT (t);
16797 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16798 {
16799 ctx = tsubst_pack_expansion (ctx, args,
16800 complain | tf_qualifying_scope,
16801 in_decl);
16802 if (ctx == error_mark_node
16803 || TREE_VEC_LENGTH (ctx) > 1)
16804 return error_mark_node;
16805 if (TREE_VEC_LENGTH (ctx) == 0)
16806 {
16807 if (complain & tf_error)
16808 error ("%qD is instantiated for an empty pack",
16809 TYPENAME_TYPE_FULLNAME (t));
16810 return error_mark_node;
16811 }
16812 ctx = TREE_VEC_ELT (ctx, 0);
16813 }
16814 else
16815 ctx = tsubst_aggr_type (ctx, args,
16816 complain | tf_qualifying_scope,
16817 in_decl, /*entering_scope=*/1);
16818 if (ctx == error_mark_node)
16819 return error_mark_node;
16820
16821 tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
16822 complain, in_decl);
16823 if (f == error_mark_node)
16824 return error_mark_node;
16825
16826 if (!MAYBE_CLASS_TYPE_P (ctx))
16827 {
16828 if (complain & tf_error)
16829 error ("%qT is not a class, struct, or union type", ctx);
16830 return error_mark_node;
16831 }
16832 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16833 {
16834 /* Normally, make_typename_type does not require that the CTX
16835 have complete type in order to allow things like:
16836
16837 template <class T> struct S { typename S<T>::X Y; };
16838
16839 But, such constructs have already been resolved by this
16840 point, so here CTX really should have complete type, unless
16841 it's a partial instantiation. */
16842 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16843 return error_mark_node;
16844 }
16845
16846 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
16847 tags. TYPENAME_TYPE should probably remember the exact tag that
16848 was written. */
16849 enum tag_types tag_type
16850 = TYPENAME_IS_CLASS_P (t) ? class_type
16851 : TYPENAME_IS_ENUM_P (t) ? enum_type
16852 : typename_type;
16853 tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
16854 tcomplain |= tst_ok_flag | qualifying_scope_flag;
16855 f = make_typename_type (ctx, f, tag_type, tcomplain);
16856 if (f == error_mark_node)
16857 return f;
16858 if (TREE_CODE (f) == TYPE_DECL)
16859 {
16860 complain |= tf_ignore_bad_quals;
16861 f = TREE_TYPE (f);
16862 }
16863
16864 if (TREE_CODE (f) != TYPENAME_TYPE)
16865 {
16866 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16867 {
16868 if (complain & tf_error)
16869 error ("%qT resolves to %qT, which is not an enumeration type",
16870 t, f);
16871 else
16872 return error_mark_node;
16873 }
16874 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16875 {
16876 if (complain & tf_error)
16877 error ("%qT resolves to %qT, which is not a class type",
16878 t, f);
16879 else
16880 return error_mark_node;
16881 }
16882 }
16883
16884 return cp_build_qualified_type
16885 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16886 }
16887
16888 case UNBOUND_CLASS_TEMPLATE:
16889 {
16890 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16891 in_decl, /*entering_scope=*/1);
16892 tree name = TYPE_IDENTIFIER (t);
16893 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16894
16895 if (ctx == error_mark_node || name == error_mark_node)
16896 return error_mark_node;
16897
16898 if (parm_list)
16899 parm_list = tsubst_template_parms (parm_list, args, complain);
16900 return make_unbound_class_template (ctx, name, parm_list, complain);
16901 }
16902
16903 case TYPEOF_TYPE:
16904 {
16905 tree type;
16906
16907 ++cp_unevaluated_operand;
16908 ++c_inhibit_evaluation_warnings;
16909
16910 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
16911
16912 --cp_unevaluated_operand;
16913 --c_inhibit_evaluation_warnings;
16914
16915 type = finish_typeof (type);
16916 return cp_build_qualified_type (type,
16917 cp_type_quals (t)
16918 | cp_type_quals (type),
16919 complain);
16920 }
16921
16922 case DECLTYPE_TYPE:
16923 {
16924 tree type;
16925
16926 ++cp_unevaluated_operand;
16927 ++c_inhibit_evaluation_warnings;
16928
16929 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
16930 complain|tf_decltype, in_decl);
16931
16932 --cp_unevaluated_operand;
16933 --c_inhibit_evaluation_warnings;
16934
16935 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16936 type = lambda_capture_field_type (type,
16937 false /*explicit_init*/,
16938 DECLTYPE_FOR_REF_CAPTURE (t));
16939 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16940 type = lambda_proxy_type (type);
16941 else
16942 {
16943 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16944 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16945 && EXPR_P (type))
16946 /* In a template ~id could be either a complement expression
16947 or an unqualified-id naming a destructor; if instantiating
16948 it produces an expression, it's not an id-expression or
16949 member access. */
16950 id = false;
16951 type = finish_decltype_type (type, id, complain);
16952 }
16953 return cp_build_qualified_type (type,
16954 cp_type_quals (t)
16955 | cp_type_quals (type),
16956 complain | tf_ignore_bad_quals);
16957 }
16958
16959 case TRAIT_TYPE:
16960 {
16961 tree type1 = TRAIT_TYPE_TYPE1 (t);
16962 if (TYPE_P (type1))
16963 type1 = tsubst (type1, args, complain, in_decl);
16964 else
16965 type1 = tsubst_expr (type1, args, complain, in_decl);
16966 tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
16967 type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
16968 return cp_build_qualified_type (type,
16969 cp_type_quals (t) | cp_type_quals (type),
16970 complain | tf_ignore_bad_quals);
16971 }
16972
16973 case TYPE_ARGUMENT_PACK:
16974 case NONTYPE_ARGUMENT_PACK:
16975 return tsubst_argument_pack (t, args, complain, in_decl);
16976
16977 case VOID_CST:
16978 case INTEGER_CST:
16979 case REAL_CST:
16980 case STRING_CST:
16981 case PLUS_EXPR:
16982 case MINUS_EXPR:
16983 case NEGATE_EXPR:
16984 case NOP_EXPR:
16985 case INDIRECT_REF:
16986 case ADDR_EXPR:
16987 case CALL_EXPR:
16988 case ARRAY_REF:
16989 case SCOPE_REF:
16990 case OMP_ARRAY_SECTION:
16991 /* We should use one of the expression tsubsts for these codes. */
16992 gcc_unreachable ();
16993
16994 default:
16995 sorry ("use of %qs in template", get_tree_code_name (code));
16996 return error_mark_node;
16997 }
16998 }
16999
17000 /* Convenience wrapper over tsubst for substituting into the LHS
17001 of the :: scope resolution operator. */
17002
17003 static tree
17004 tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17005 {
17006 gcc_checking_assert (TYPE_P (t));
17007 return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
17008 }
17009
17010 /* Convenience wrapper over tsubst for substituting into an id-expression
17011 without resolving its terminal name. */
17012
17013 static tree
17014 tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17015 {
17016 return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
17017 }
17018
17019 /* OLDFNS is a lookup set of member functions from some class template, and
17020 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
17021 of that class template. Return the subset of NEWFNS which are
17022 specializations of a function from OLDFNS. */
17023
17024 static tree
17025 filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
17026 {
17027 /* Record all member functions from the old lookup set OLDFNS into
17028 VISIBLE_SET. */
17029 hash_set<tree> visible_set;
17030 bool seen_dep_using = false;
17031 for (tree fn : lkp_range (oldfns))
17032 {
17033 if (TREE_CODE (fn) == USING_DECL)
17034 {
17035 /* Imprecisely handle dependent using-decl by keeping all members
17036 in the new lookup set that are defined in a base class, i.e.
17037 members that could plausibly have been introduced by this
17038 dependent using-decl.
17039 FIXME: Track which members are introduced by a dependent
17040 using-decl precisely, perhaps by performing another lookup
17041 from the substituted USING_DECL_SCOPE. */
17042 gcc_checking_assert (DECL_DEPENDENT_P (fn));
17043 seen_dep_using = true;
17044 }
17045 else
17046 visible_set.add (fn);
17047 }
17048
17049 /* Returns true iff (a less specialized version of) FN appeared in
17050 the old lookup set OLDFNS. */
17051 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
17052 if (DECL_CONTEXT (fn) != newtype)
17053 /* FN is a member function from a base class, introduced via a
17054 using-decl; if it might have been introduced by a dependent
17055 using-decl then just conservatively keep it, otherwise look
17056 in the old lookup set for FN exactly. */
17057 return seen_dep_using || visible_set.contains (fn);
17058 else if (TREE_CODE (fn) == TEMPLATE_DECL)
17059 /* FN is a member function template from the current class;
17060 look in the old lookup set for the TEMPLATE_DECL from which
17061 it was specialized. */
17062 return visible_set.contains (DECL_TI_TEMPLATE (fn));
17063 else
17064 /* FN is a non-template member function from the current class;
17065 look in the old lookup set for the FUNCTION_DECL from which
17066 it was specialized. */
17067 return visible_set.contains (DECL_TEMPLATE_RESULT
17068 (DECL_TI_TEMPLATE (fn)));
17069 };
17070
17071 bool lookup_changed_p = false;
17072 for (tree fn : lkp_range (newfns))
17073 if (!visible_p (fn))
17074 {
17075 lookup_changed_p = true;
17076 break;
17077 }
17078 if (!lookup_changed_p)
17079 return newfns;
17080
17081 /* Filter out from NEWFNS the member functions that weren't
17082 previously visible according to OLDFNS. */
17083 tree filtered_fns = NULL_TREE;
17084 unsigned filtered_size = 0;
17085 for (tree fn : lkp_range (newfns))
17086 if (visible_p (fn))
17087 {
17088 filtered_fns = lookup_add (fn, filtered_fns);
17089 filtered_size++;
17090 }
17091 gcc_checking_assert (seen_dep_using
17092 ? filtered_size >= visible_set.elements ()
17093 : filtered_size == visible_set.elements ());
17094
17095 return filtered_fns;
17096 }
17097
17098 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
17099 expression on the left-hand side of the "." or "->" operator. We
17100 only do the lookup if we had a dependent BASELINK. Otherwise we
17101 adjust it onto the instantiated heirarchy. */
17102
17103 static tree
17104 tsubst_baselink (tree baselink, tree object_type,
17105 tree args, tsubst_flags_t complain, tree in_decl)
17106 {
17107 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
17108 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
17109 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
17110
17111 tree optype = BASELINK_OPTYPE (baselink);
17112 optype = tsubst (optype, args, complain, in_decl);
17113
17114 tree template_args = NULL_TREE;
17115 bool template_id_p = false;
17116 tree fns = BASELINK_FUNCTIONS (baselink);
17117 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
17118 {
17119 template_id_p = true;
17120 template_args = TREE_OPERAND (fns, 1);
17121 fns = TREE_OPERAND (fns, 0);
17122 if (template_args)
17123 template_args = tsubst_template_args (template_args, args,
17124 complain, in_decl);
17125 }
17126
17127 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
17128 binfo_type = tsubst (binfo_type, args, complain, in_decl);
17129 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
17130 || optype != BASELINK_OPTYPE (baselink));
17131
17132 if (dependent_p)
17133 {
17134 tree name = OVL_NAME (fns);
17135 if (IDENTIFIER_CONV_OP_P (name))
17136 name = make_conv_op_name (optype);
17137
17138 /* See maybe_dependent_member_ref. */
17139 if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
17140 {
17141 if (template_id_p)
17142 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
17143 template_args);
17144 return build_qualified_name (NULL_TREE, qualifying_scope, name,
17145 /* ::template */false);
17146 }
17147
17148 if (name == complete_dtor_identifier)
17149 /* Treat as-if non-dependent below. */
17150 dependent_p = false;
17151
17152 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
17153 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
17154 complain);
17155 if (maybe_incomplete)
17156 {
17157 /* Filter out from the new lookup set those functions which didn't
17158 appear in the original lookup set (in a less specialized form).
17159 This is needed to preserve the consistency of member lookup
17160 performed in an incomplete-class context, within which
17161 later-declared members ought to remain invisible. */
17162 BASELINK_FUNCTIONS (baselink)
17163 = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
17164 binfo_type);
17165 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
17166 }
17167
17168 if (!baselink)
17169 {
17170 if ((complain & tf_error)
17171 && constructor_name_p (name, qualifying_scope))
17172 error ("cannot call constructor %<%T::%D%> directly",
17173 qualifying_scope, name);
17174 return error_mark_node;
17175 }
17176
17177 fns = BASELINK_FUNCTIONS (baselink);
17178 }
17179 else
17180 {
17181 /* We're going to overwrite pieces below, make a duplicate. */
17182 baselink = copy_node (baselink);
17183
17184 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
17185 {
17186 /* The decl we found was from non-dependent scope, but we still need
17187 to update the binfos for the instantiated qualifying_scope. */
17188 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
17189 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
17190 ba_unique, nullptr, complain);
17191 }
17192 }
17193
17194 /* If lookup found a single function, mark it as used at this point.
17195 (If lookup found multiple functions the one selected later by
17196 overload resolution will be marked as used at that point.) */
17197 if (!template_id_p && !really_overloaded_fn (fns))
17198 {
17199 tree fn = OVL_FIRST (fns);
17200 bool ok = mark_used (fn, complain);
17201 if (!ok && !(complain & tf_error))
17202 return error_mark_node;
17203 if (ok && BASELINK_P (baselink))
17204 /* We might have instantiated an auto function. */
17205 TREE_TYPE (baselink) = TREE_TYPE (fn);
17206 }
17207
17208 if (BASELINK_P (baselink))
17209 {
17210 /* Add back the template arguments, if present. */
17211 if (template_id_p)
17212 BASELINK_FUNCTIONS (baselink)
17213 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
17214
17215 /* Update the conversion operator type. */
17216 BASELINK_OPTYPE (baselink) = optype;
17217 }
17218
17219 if (!object_type)
17220 object_type = current_class_type;
17221
17222 if (qualified_p || !dependent_p)
17223 {
17224 baselink = adjust_result_of_qualified_name_lookup (baselink,
17225 qualifying_scope,
17226 object_type);
17227 if (!qualified_p)
17228 /* We need to call adjust_result_of_qualified_name_lookup in case the
17229 destructor names a base class, but we unset BASELINK_QUALIFIED_P
17230 so that we still get virtual function binding. */
17231 BASELINK_QUALIFIED_P (baselink) = false;
17232 }
17233
17234 return baselink;
17235 }
17236
17237 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
17238 true if the qualified-id will be a postfix-expression in-and-of
17239 itself; false if more of the postfix-expression follows the
17240 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
17241 of "&". */
17242
17243 static tree
17244 tsubst_qualified_id (tree qualified_id, tree args,
17245 tsubst_flags_t complain, tree in_decl,
17246 bool done, bool address_p)
17247 {
17248 tree expr;
17249 tree scope;
17250 tree name;
17251 bool is_template;
17252 tree template_args;
17253 location_t loc = EXPR_LOCATION (qualified_id);
17254
17255 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
17256
17257 /* Figure out what name to look up. */
17258 name = TREE_OPERAND (qualified_id, 1);
17259 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17260 {
17261 is_template = true;
17262 template_args = TREE_OPERAND (name, 1);
17263 if (template_args)
17264 template_args = tsubst_template_args (template_args, args,
17265 complain, in_decl);
17266 if (template_args == error_mark_node)
17267 return error_mark_node;
17268 name = TREE_OPERAND (name, 0);
17269 }
17270 else
17271 {
17272 is_template = false;
17273 template_args = NULL_TREE;
17274 }
17275
17276 /* Substitute into the qualifying scope. When there are no ARGS, we
17277 are just trying to simplify a non-dependent expression. In that
17278 case the qualifying scope may be dependent, and, in any case,
17279 substituting will not help. */
17280 scope = TREE_OPERAND (qualified_id, 0);
17281 if (args)
17282 {
17283 scope = tsubst_scope (scope, args, complain, in_decl);
17284 expr = tsubst_name (name, args, complain, in_decl);
17285 }
17286 else
17287 expr = name;
17288
17289 if (dependent_scope_p (scope))
17290 {
17291 if (TREE_CODE (expr) == SCOPE_REF)
17292 /* We built one in tsubst_baselink. */
17293 gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
17294 else
17295 {
17296 if (is_template)
17297 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
17298 template_args);
17299 expr = build_qualified_name (NULL_TREE, scope, expr,
17300 QUALIFIED_NAME_IS_TEMPLATE
17301 (qualified_id));
17302 }
17303 REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
17304 return expr;
17305 }
17306
17307 if (!BASELINK_P (name) && !DECL_P (expr))
17308 {
17309 if (TREE_CODE (expr) == BIT_NOT_EXPR)
17310 {
17311 /* A BIT_NOT_EXPR is used to represent a destructor. */
17312 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
17313 {
17314 error ("qualifying type %qT does not match destructor name ~%qT",
17315 scope, TREE_OPERAND (expr, 0));
17316 expr = error_mark_node;
17317 }
17318 else
17319 expr = lookup_qualified_name (scope, complete_dtor_identifier,
17320 LOOK_want::NORMAL, false);
17321 }
17322 else
17323 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
17324 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
17325 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
17326 {
17327 if (complain & tf_error)
17328 {
17329 error ("dependent-name %qE is parsed as a non-type, but "
17330 "instantiation yields a type", qualified_id);
17331 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
17332 }
17333 return error_mark_node;
17334 }
17335 }
17336
17337 if (DECL_P (expr))
17338 {
17339 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
17340 scope, complain))
17341 return error_mark_node;
17342 /* Remember that there was a reference to this entity. */
17343 if (!mark_used (expr, complain) && !(complain & tf_error))
17344 return error_mark_node;
17345 }
17346
17347 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
17348 {
17349 if (complain & tf_error)
17350 qualified_name_lookup_error (scope,
17351 TREE_OPERAND (qualified_id, 1),
17352 expr, input_location);
17353 return error_mark_node;
17354 }
17355
17356 if (is_template)
17357 {
17358 /* We may be repeating a check already done during parsing, but
17359 if it was well-formed and passed then, it will pass again
17360 now, and if it didn't, we wouldn't have got here. The case
17361 we want to catch is when we couldn't tell then, and can now,
17362 namely when templ prior to substitution was an
17363 identifier. */
17364 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
17365 return error_mark_node;
17366
17367 if (variable_template_p (expr))
17368 expr = lookup_and_finish_template_variable (expr, template_args,
17369 complain);
17370 else
17371 expr = lookup_template_function (expr, template_args);
17372 }
17373
17374 if (expr == error_mark_node && complain & tf_error)
17375 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
17376 expr, input_location);
17377 else if (TYPE_P (scope))
17378 {
17379 expr = (adjust_result_of_qualified_name_lookup
17380 (expr, scope, current_nonlambda_class_type ()));
17381 expr = (finish_qualified_id_expr
17382 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
17383 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
17384 /*template_arg_p=*/false, complain));
17385 }
17386
17387 /* Expressions do not generally have reference type. */
17388 if (TREE_CODE (expr) != SCOPE_REF
17389 /* However, if we're about to form a pointer-to-member, we just
17390 want the referenced member referenced. */
17391 && TREE_CODE (expr) != OFFSET_REF)
17392 expr = convert_from_reference (expr);
17393
17394 if (REF_PARENTHESIZED_P (qualified_id))
17395 expr = force_paren_expr (expr);
17396
17397 expr = maybe_wrap_with_location (expr, loc);
17398
17399 return expr;
17400 }
17401
17402 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
17403 initializer, DECL is the substituted VAR_DECL. Other arguments are as
17404 for tsubst. */
17405
17406 static tree
17407 tsubst_init (tree init, tree decl, tree args,
17408 tsubst_flags_t complain, tree in_decl)
17409 {
17410 if (!init)
17411 return NULL_TREE;
17412
17413 init = tsubst_expr (init, args, complain, in_decl);
17414
17415 tree type = TREE_TYPE (decl);
17416
17417 if (!init && type != error_mark_node)
17418 {
17419 if (tree auto_node = type_uses_auto (type))
17420 {
17421 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17422 {
17423 if (complain & tf_error)
17424 error ("initializer for %q#D expands to an empty list "
17425 "of expressions", decl);
17426 return error_mark_node;
17427 }
17428 }
17429 else if (!dependent_type_p (type))
17430 {
17431 /* If we had an initializer but it
17432 instantiated to nothing,
17433 value-initialize the object. This will
17434 only occur when the initializer was a
17435 pack expansion where the parameter packs
17436 used in that expansion were of length
17437 zero. */
17438 init = build_value_init (type, complain);
17439 if (TREE_CODE (init) == AGGR_INIT_EXPR)
17440 init = get_target_expr (init, complain);
17441 if (TREE_CODE (init) == TARGET_EXPR)
17442 TARGET_EXPR_DIRECT_INIT_P (init) = true;
17443 }
17444 }
17445
17446 return init;
17447 }
17448
17449 /* If T is a reference to a dependent member of the current instantiation C and
17450 we are trying to refer to that member in a partial instantiation of C,
17451 return a SCOPE_REF; otherwise, return NULL_TREE.
17452
17453 This can happen when forming a C++17 deduction guide, as in PR96199. */
17454
17455 static tree
17456 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
17457 tree in_decl)
17458 {
17459 if (!(complain & tf_dguide))
17460 return NULL_TREE;
17461
17462 tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
17463 if (!decl || !DECL_P (decl))
17464 return NULL_TREE;
17465
17466 tree ctx = context_for_name_lookup (decl);
17467 if (!CLASS_TYPE_P (ctx))
17468 return NULL_TREE;
17469
17470 ctx = tsubst (ctx, args, complain, in_decl);
17471 if (!dependent_scope_p (ctx))
17472 return NULL_TREE;
17473
17474 if (TYPE_P (t))
17475 {
17476 if (typedef_variant_p (t))
17477 t = strip_typedefs (t);
17478 tree decl = TYPE_NAME (t);
17479 if (decl)
17480 decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
17481 if (!decl)
17482 return NULL_TREE;
17483 return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
17484 complain);
17485 }
17486
17487 tree name = DECL_NAME (t);
17488 tree fullname = name;
17489 if (instantiates_primary_template_p (t))
17490 {
17491 tree tinfo = get_template_info (t);
17492 name = DECL_NAME (TI_TEMPLATE (tinfo));
17493 tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
17494 targs = tsubst_template_args (targs, args, complain, in_decl);
17495 fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
17496 }
17497
17498 if (TREE_CODE (t) == TYPE_DECL)
17499 {
17500 if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
17501 && TYPE_NAME (TREE_TYPE (t)) == t)
17502 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17503 scope, but it doesn't need to be rewritten again. */
17504 return NULL_TREE;
17505 tree type = build_typename_type (ctx, name, fullname, typename_type);
17506 return TYPE_NAME (type);
17507 }
17508 else if (DECL_TYPE_TEMPLATE_P (t))
17509 return make_unbound_class_template (ctx, name,
17510 NULL_TREE, complain);
17511 else
17512 return build_qualified_name (NULL_TREE, ctx, fullname,
17513 TREE_CODE (t) == TEMPLATE_DECL);
17514 }
17515
17516 /* Helper function for tsubst_omp_clauses, used for instantiation of
17517 OMP_CLAUSE_DECL of clauses. */
17518
17519 static tree
17520 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17521 tree in_decl, tree *iterator_cache)
17522 {
17523 if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
17524 return decl;
17525
17526 /* Handle OpenMP iterators. */
17527 if (TREE_CODE (decl) == TREE_LIST
17528 && TREE_PURPOSE (decl)
17529 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17530 {
17531 tree ret;
17532 if (iterator_cache[0] == TREE_PURPOSE (decl))
17533 ret = iterator_cache[1];
17534 else
17535 {
17536 tree *tp = &ret;
17537 begin_scope (sk_omp, NULL);
17538 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17539 {
17540 *tp = copy_node (it);
17541 TREE_VEC_ELT (*tp, 0)
17542 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17543 DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
17544 pushdecl (TREE_VEC_ELT (*tp, 0));
17545 TREE_VEC_ELT (*tp, 1)
17546 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
17547 TREE_VEC_ELT (*tp, 2)
17548 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
17549 TREE_VEC_ELT (*tp, 3)
17550 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
17551 TREE_CHAIN (*tp) = NULL_TREE;
17552 tp = &TREE_CHAIN (*tp);
17553 }
17554 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17555 iterator_cache[0] = TREE_PURPOSE (decl);
17556 iterator_cache[1] = ret;
17557 }
17558 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17559 args, complain,
17560 in_decl, NULL));
17561 }
17562
17563 /* Handle an OpenMP array section represented as a TREE_LIST (or
17564 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17565 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17566 TREE_LIST. We can handle it exactly the same as an array section
17567 (purpose, value, and a chain), even though the nomenclature
17568 (low_bound, length, etc) is different. */
17569 if (TREE_CODE (decl) == TREE_LIST)
17570 {
17571 tree low_bound
17572 = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
17573 tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
17574 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17575 in_decl, NULL);
17576 if (TREE_PURPOSE (decl) == low_bound
17577 && TREE_VALUE (decl) == length
17578 && TREE_CHAIN (decl) == chain)
17579 return decl;
17580 tree ret = tree_cons (low_bound, length, chain);
17581 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
17582 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
17583 return ret;
17584 }
17585 else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
17586 {
17587 tree low_bound
17588 = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
17589 tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
17590 in_decl);
17591 tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
17592 complain, in_decl, NULL);
17593 if (TREE_OPERAND (decl, 0) == base
17594 && TREE_OPERAND (decl, 1) == low_bound
17595 && TREE_OPERAND (decl, 2) == length)
17596 return decl;
17597 return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
17598 length);
17599 }
17600 tree ret = tsubst_stmt (decl, args, complain, in_decl);
17601 /* Undo convert_from_reference tsubst_expr could have called. */
17602 if (decl
17603 && REFERENCE_REF_P (ret)
17604 && !REFERENCE_REF_P (decl))
17605 ret = TREE_OPERAND (ret, 0);
17606 return ret;
17607 }
17608
17609 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17610
17611 static tree
17612 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17613 tree args, tsubst_flags_t complain, tree in_decl)
17614 {
17615 tree new_clauses = NULL_TREE, nc, oc;
17616 tree linear_no_step = NULL_TREE;
17617 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17618
17619 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17620 {
17621 nc = copy_node (oc);
17622 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17623 new_clauses = nc;
17624
17625 switch (OMP_CLAUSE_CODE (nc))
17626 {
17627 case OMP_CLAUSE_LASTPRIVATE:
17628 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17629 {
17630 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17631 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
17632 complain, in_decl);
17633 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17634 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17635 }
17636 /* FALLTHRU */
17637 case OMP_CLAUSE_PRIVATE:
17638 case OMP_CLAUSE_SHARED:
17639 case OMP_CLAUSE_FIRSTPRIVATE:
17640 case OMP_CLAUSE_COPYIN:
17641 case OMP_CLAUSE_COPYPRIVATE:
17642 case OMP_CLAUSE_UNIFORM:
17643 case OMP_CLAUSE_DEPEND:
17644 case OMP_CLAUSE_DOACROSS:
17645 case OMP_CLAUSE_AFFINITY:
17646 case OMP_CLAUSE_FROM:
17647 case OMP_CLAUSE_TO:
17648 case OMP_CLAUSE_MAP:
17649 case OMP_CLAUSE__CACHE_:
17650 case OMP_CLAUSE_NONTEMPORAL:
17651 case OMP_CLAUSE_USE_DEVICE_PTR:
17652 case OMP_CLAUSE_USE_DEVICE_ADDR:
17653 case OMP_CLAUSE_IS_DEVICE_PTR:
17654 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17655 case OMP_CLAUSE_INCLUSIVE:
17656 case OMP_CLAUSE_EXCLUSIVE:
17657 OMP_CLAUSE_DECL (nc)
17658 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17659 in_decl, iterator_cache);
17660 break;
17661 case OMP_CLAUSE_NUM_TEAMS:
17662 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
17663 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
17664 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
17665 complain, in_decl);
17666 /* FALLTHRU */
17667 case OMP_CLAUSE_TILE:
17668 case OMP_CLAUSE_IF:
17669 case OMP_CLAUSE_SELF:
17670 case OMP_CLAUSE_NUM_THREADS:
17671 case OMP_CLAUSE_SCHEDULE:
17672 case OMP_CLAUSE_COLLAPSE:
17673 case OMP_CLAUSE_FINAL:
17674 case OMP_CLAUSE_DEVICE:
17675 case OMP_CLAUSE_DIST_SCHEDULE:
17676 case OMP_CLAUSE_THREAD_LIMIT:
17677 case OMP_CLAUSE_SAFELEN:
17678 case OMP_CLAUSE_SIMDLEN:
17679 case OMP_CLAUSE_NUM_TASKS:
17680 case OMP_CLAUSE_GRAINSIZE:
17681 case OMP_CLAUSE_PRIORITY:
17682 case OMP_CLAUSE_ORDERED:
17683 case OMP_CLAUSE_HINT:
17684 case OMP_CLAUSE_FILTER:
17685 case OMP_CLAUSE_NUM_GANGS:
17686 case OMP_CLAUSE_NUM_WORKERS:
17687 case OMP_CLAUSE_VECTOR_LENGTH:
17688 case OMP_CLAUSE_WORKER:
17689 case OMP_CLAUSE_VECTOR:
17690 case OMP_CLAUSE_ASYNC:
17691 case OMP_CLAUSE_WAIT:
17692 case OMP_CLAUSE_DETACH:
17693 OMP_CLAUSE_OPERAND (nc, 0)
17694 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
17695 break;
17696 case OMP_CLAUSE_REDUCTION:
17697 case OMP_CLAUSE_IN_REDUCTION:
17698 case OMP_CLAUSE_TASK_REDUCTION:
17699 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17700 {
17701 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17702 if (TREE_CODE (placeholder) == SCOPE_REF)
17703 {
17704 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17705 complain, in_decl);
17706 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17707 = build_qualified_name (NULL_TREE, scope,
17708 TREE_OPERAND (placeholder, 1),
17709 false);
17710 }
17711 else
17712 gcc_assert (identifier_p (placeholder));
17713 }
17714 OMP_CLAUSE_DECL (nc)
17715 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17716 in_decl, NULL);
17717 break;
17718 case OMP_CLAUSE_GANG:
17719 case OMP_CLAUSE_ALIGNED:
17720 OMP_CLAUSE_DECL (nc)
17721 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17722 in_decl, NULL);
17723 OMP_CLAUSE_OPERAND (nc, 1)
17724 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
17725 break;
17726 case OMP_CLAUSE_ALLOCATE:
17727 OMP_CLAUSE_DECL (nc)
17728 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17729 in_decl, NULL);
17730 OMP_CLAUSE_OPERAND (nc, 1)
17731 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
17732 OMP_CLAUSE_OPERAND (nc, 2)
17733 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
17734 break;
17735 case OMP_CLAUSE_LINEAR:
17736 OMP_CLAUSE_DECL (nc)
17737 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17738 in_decl, NULL);
17739 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17740 {
17741 gcc_assert (!linear_no_step);
17742 linear_no_step = nc;
17743 }
17744 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17745 OMP_CLAUSE_LINEAR_STEP (nc)
17746 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17747 complain, in_decl, NULL);
17748 else
17749 OMP_CLAUSE_LINEAR_STEP (nc)
17750 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
17751 complain, in_decl);
17752 break;
17753 case OMP_CLAUSE_NOWAIT:
17754 case OMP_CLAUSE_DEFAULT:
17755 case OMP_CLAUSE_UNTIED:
17756 case OMP_CLAUSE_MERGEABLE:
17757 case OMP_CLAUSE_INBRANCH:
17758 case OMP_CLAUSE_NOTINBRANCH:
17759 case OMP_CLAUSE_PROC_BIND:
17760 case OMP_CLAUSE_FOR:
17761 case OMP_CLAUSE_PARALLEL:
17762 case OMP_CLAUSE_SECTIONS:
17763 case OMP_CLAUSE_TASKGROUP:
17764 case OMP_CLAUSE_NOGROUP:
17765 case OMP_CLAUSE_THREADS:
17766 case OMP_CLAUSE_SIMD:
17767 case OMP_CLAUSE_DEFAULTMAP:
17768 case OMP_CLAUSE_ORDER:
17769 case OMP_CLAUSE_BIND:
17770 case OMP_CLAUSE_INDEPENDENT:
17771 case OMP_CLAUSE_AUTO:
17772 case OMP_CLAUSE_SEQ:
17773 case OMP_CLAUSE_IF_PRESENT:
17774 case OMP_CLAUSE_FINALIZE:
17775 case OMP_CLAUSE_NOHOST:
17776 break;
17777 default:
17778 gcc_unreachable ();
17779 }
17780 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17781 switch (OMP_CLAUSE_CODE (nc))
17782 {
17783 case OMP_CLAUSE_SHARED:
17784 case OMP_CLAUSE_PRIVATE:
17785 case OMP_CLAUSE_FIRSTPRIVATE:
17786 case OMP_CLAUSE_LASTPRIVATE:
17787 case OMP_CLAUSE_COPYPRIVATE:
17788 case OMP_CLAUSE_LINEAR:
17789 case OMP_CLAUSE_REDUCTION:
17790 case OMP_CLAUSE_IN_REDUCTION:
17791 case OMP_CLAUSE_TASK_REDUCTION:
17792 case OMP_CLAUSE_USE_DEVICE_PTR:
17793 case OMP_CLAUSE_USE_DEVICE_ADDR:
17794 case OMP_CLAUSE_IS_DEVICE_PTR:
17795 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17796 case OMP_CLAUSE_INCLUSIVE:
17797 case OMP_CLAUSE_EXCLUSIVE:
17798 case OMP_CLAUSE_ALLOCATE:
17799 /* tsubst_expr on SCOPE_REF results in returning
17800 finish_non_static_data_member result. Undo that here. */
17801 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17802 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17803 == IDENTIFIER_NODE))
17804 {
17805 tree t = OMP_CLAUSE_DECL (nc);
17806 tree v = t;
17807 while (v)
17808 switch (TREE_CODE (v))
17809 {
17810 case COMPONENT_REF:
17811 case MEM_REF:
17812 case INDIRECT_REF:
17813 CASE_CONVERT:
17814 case POINTER_PLUS_EXPR:
17815 v = TREE_OPERAND (v, 0);
17816 continue;
17817 case PARM_DECL:
17818 if (DECL_CONTEXT (v) == current_function_decl
17819 && DECL_ARTIFICIAL (v)
17820 && DECL_NAME (v) == this_identifier)
17821 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17822 /* FALLTHRU */
17823 default:
17824 v = NULL_TREE;
17825 break;
17826 }
17827 }
17828 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17829 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17830 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17831 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17832 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17833 {
17834 tree decl = OMP_CLAUSE_DECL (nc);
17835 if (VAR_P (decl))
17836 {
17837 retrofit_lang_decl (decl);
17838 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17839 }
17840 }
17841 break;
17842 default:
17843 break;
17844 }
17845 }
17846
17847 new_clauses = nreverse (new_clauses);
17848 if (ort != C_ORT_OMP_DECLARE_SIMD)
17849 {
17850 new_clauses = finish_omp_clauses (new_clauses, ort);
17851 if (linear_no_step)
17852 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17853 if (nc == linear_no_step)
17854 {
17855 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17856 break;
17857 }
17858 }
17859 return new_clauses;
17860 }
17861
17862 /* Like tsubst_expr, but unshare TREE_LIST nodes. */
17863
17864 static tree
17865 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17866 tree in_decl)
17867 {
17868 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17869
17870 tree purpose, value, chain;
17871
17872 if (t == NULL)
17873 return t;
17874
17875 if (TREE_CODE (t) != TREE_LIST)
17876 return tsubst_expr (t, args, complain, in_decl);
17877
17878 if (t == void_list_node)
17879 return t;
17880
17881 purpose = TREE_PURPOSE (t);
17882 if (purpose)
17883 purpose = RECUR (purpose);
17884 value = TREE_VALUE (t);
17885 if (value)
17886 {
17887 if (TREE_CODE (value) != LABEL_DECL)
17888 value = RECUR (value);
17889 else
17890 {
17891 value = lookup_label (DECL_NAME (value));
17892 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17893 TREE_USED (value) = 1;
17894 }
17895 }
17896 chain = TREE_CHAIN (t);
17897 if (chain && chain != void_type_node)
17898 chain = RECUR (chain);
17899 return tree_cons (purpose, value, chain);
17900 #undef RECUR
17901 }
17902
17903 /* Used to temporarily communicate the list of #pragma omp parallel
17904 clauses to #pragma omp for instantiation if they are combined
17905 together. */
17906
17907 static tree *omp_parallel_combined_clauses;
17908
17909 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17910 cp_decomp *);
17911
17912 /* Substitute one OMP_FOR iterator. */
17913
17914 static bool
17915 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17916 tree initv, tree condv, tree incrv, tree *clauses,
17917 tree args, tsubst_flags_t complain, tree in_decl)
17918 {
17919 #define RECUR(NODE) \
17920 tsubst_stmt ((NODE), args, complain, in_decl)
17921 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17922 bool ret = false;
17923
17924 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17925 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17926
17927 decl = TREE_OPERAND (init, 0);
17928 init = TREE_OPERAND (init, 1);
17929 tree decl_expr = NULL_TREE;
17930 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17931 if (range_for)
17932 {
17933 bool decomp = false;
17934 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17935 {
17936 tree v = DECL_VALUE_EXPR (decl);
17937 if (TREE_CODE (v) == ARRAY_REF
17938 && VAR_P (TREE_OPERAND (v, 0))
17939 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17940 {
17941 cp_decomp decomp_d = { NULL_TREE, 0 };
17942 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17943 maybe_push_decl (d);
17944 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17945 in_decl, &decomp_d);
17946 decomp = true;
17947 if (d == error_mark_node)
17948 decl = error_mark_node;
17949 else
17950 for (unsigned int i = 0; i < decomp_d.count; i++)
17951 {
17952 if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
17953 {
17954 tree v = build_nt (ARRAY_REF, d,
17955 size_int (decomp_d.count - i - 1),
17956 NULL_TREE, NULL_TREE);
17957 SET_DECL_VALUE_EXPR (decomp_d.decl, v);
17958 DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
17959 }
17960 fit_decomposition_lang_decl (decomp_d.decl, d);
17961 decomp_d.decl = DECL_CHAIN (decomp_d.decl);
17962 }
17963 }
17964 }
17965 decl = tsubst_decl (decl, args, complain);
17966 if (!decomp)
17967 maybe_push_decl (decl);
17968 }
17969 else if (init && TREE_CODE (init) == DECL_EXPR)
17970 {
17971 /* We need to jump through some hoops to handle declarations in the
17972 init-statement, since we might need to handle auto deduction,
17973 but we need to keep control of initialization. */
17974 decl_expr = init;
17975 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17976 decl = tsubst_decl (decl, args, complain);
17977 }
17978 else
17979 {
17980 if (TREE_CODE (decl) == SCOPE_REF)
17981 {
17982 decl = RECUR (decl);
17983 if (TREE_CODE (decl) == COMPONENT_REF)
17984 {
17985 tree v = decl;
17986 while (v)
17987 switch (TREE_CODE (v))
17988 {
17989 case COMPONENT_REF:
17990 case MEM_REF:
17991 case INDIRECT_REF:
17992 CASE_CONVERT:
17993 case POINTER_PLUS_EXPR:
17994 v = TREE_OPERAND (v, 0);
17995 continue;
17996 case PARM_DECL:
17997 if (DECL_CONTEXT (v) == current_function_decl
17998 && DECL_ARTIFICIAL (v)
17999 && DECL_NAME (v) == this_identifier)
18000 {
18001 decl = TREE_OPERAND (decl, 1);
18002 decl = omp_privatize_field (decl, false);
18003 }
18004 /* FALLTHRU */
18005 default:
18006 v = NULL_TREE;
18007 break;
18008 }
18009 }
18010 }
18011 else
18012 decl = RECUR (decl);
18013 }
18014 if (init && TREE_CODE (init) == TREE_VEC)
18015 {
18016 init = copy_node (init);
18017 TREE_VEC_ELT (init, 0)
18018 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
18019 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
18020 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
18021 }
18022 else
18023 init = RECUR (init);
18024
18025 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
18026 {
18027 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
18028 if (TREE_CODE (o) == TREE_LIST)
18029 TREE_VEC_ELT (orig_declv, i)
18030 = tree_cons (RECUR (TREE_PURPOSE (o)),
18031 RECUR (TREE_VALUE (o)),
18032 NULL_TREE);
18033 else
18034 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
18035 }
18036
18037 if (range_for)
18038 {
18039 tree this_pre_body = NULL_TREE;
18040 tree orig_init = NULL_TREE;
18041 tree orig_decl = NULL_TREE;
18042 tree init_sl = NULL_TREE;
18043 cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
18044 orig_init, cond, incr);
18045 if (orig_decl)
18046 {
18047 if (orig_declv == NULL_TREE)
18048 orig_declv = copy_node (declv);
18049 TREE_VEC_ELT (orig_declv, i) = orig_decl;
18050 ret = true;
18051 }
18052 else if (orig_declv)
18053 TREE_VEC_ELT (orig_declv, i) = decl;
18054 }
18055
18056 tree auto_node = type_uses_auto (TREE_TYPE (decl));
18057 if (!range_for && auto_node && init)
18058 TREE_TYPE (decl)
18059 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
18060
18061 gcc_assert (!type_dependent_expression_p (decl));
18062
18063 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
18064 {
18065 if (decl_expr)
18066 {
18067 /* Declare the variable, but don't let that initialize it. */
18068 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
18069 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
18070 RECUR (decl_expr);
18071 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
18072 }
18073
18074 if (!range_for)
18075 {
18076 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18077 if (COMPARISON_CLASS_P (cond)
18078 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
18079 {
18080 tree lhs = RECUR (TREE_OPERAND (cond, 0));
18081 tree rhs = copy_node (TREE_OPERAND (cond, 1));
18082 TREE_VEC_ELT (rhs, 0)
18083 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
18084 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
18085 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
18086 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
18087 lhs, rhs);
18088 }
18089 else
18090 cond = RECUR (cond);
18091 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18092 if (TREE_CODE (incr) == MODIFY_EXPR)
18093 {
18094 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18095 tree rhs = RECUR (TREE_OPERAND (incr, 1));
18096 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
18097 NOP_EXPR, rhs, NULL_TREE, complain);
18098 }
18099 else
18100 incr = RECUR (incr);
18101 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18102 TREE_VEC_ELT (orig_declv, i) = decl;
18103 }
18104 TREE_VEC_ELT (declv, i) = decl;
18105 TREE_VEC_ELT (initv, i) = init;
18106 TREE_VEC_ELT (condv, i) = cond;
18107 TREE_VEC_ELT (incrv, i) = incr;
18108 return ret;
18109 }
18110
18111 if (decl_expr)
18112 {
18113 /* Declare and initialize the variable. */
18114 RECUR (decl_expr);
18115 init = NULL_TREE;
18116 }
18117 else if (init)
18118 {
18119 tree *pc;
18120 int j;
18121 for (j = ((omp_parallel_combined_clauses == NULL
18122 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
18123 {
18124 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
18125 {
18126 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
18127 && OMP_CLAUSE_DECL (*pc) == decl)
18128 break;
18129 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
18130 && OMP_CLAUSE_DECL (*pc) == decl)
18131 {
18132 if (j)
18133 break;
18134 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18135 tree c = *pc;
18136 *pc = OMP_CLAUSE_CHAIN (c);
18137 OMP_CLAUSE_CHAIN (c) = *clauses;
18138 *clauses = c;
18139 }
18140 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
18141 && OMP_CLAUSE_DECL (*pc) == decl)
18142 {
18143 error ("iteration variable %qD should not be firstprivate",
18144 decl);
18145 *pc = OMP_CLAUSE_CHAIN (*pc);
18146 }
18147 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18148 && OMP_CLAUSE_DECL (*pc) == decl)
18149 {
18150 error ("iteration variable %qD should not be reduction",
18151 decl);
18152 *pc = OMP_CLAUSE_CHAIN (*pc);
18153 }
18154 else
18155 pc = &OMP_CLAUSE_CHAIN (*pc);
18156 }
18157 if (*pc)
18158 break;
18159 }
18160 if (*pc == NULL_TREE)
18161 {
18162 tree c = build_omp_clause (input_location,
18163 TREE_CODE (t) == OMP_LOOP
18164 ? OMP_CLAUSE_LASTPRIVATE
18165 : OMP_CLAUSE_PRIVATE);
18166 OMP_CLAUSE_DECL (c) = decl;
18167 c = finish_omp_clauses (c, C_ORT_OMP);
18168 if (c)
18169 {
18170 OMP_CLAUSE_CHAIN (c) = *clauses;
18171 *clauses = c;
18172 }
18173 }
18174 }
18175 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18176 if (COMPARISON_CLASS_P (cond))
18177 {
18178 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18179 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18180 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18181 }
18182 else
18183 cond = RECUR (cond);
18184 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18185 switch (TREE_CODE (incr))
18186 {
18187 case PREINCREMENT_EXPR:
18188 case PREDECREMENT_EXPR:
18189 case POSTINCREMENT_EXPR:
18190 case POSTDECREMENT_EXPR:
18191 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18192 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18193 break;
18194 case MODIFY_EXPR:
18195 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18196 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18197 {
18198 tree rhs = TREE_OPERAND (incr, 1);
18199 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18200 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18201 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18202 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18203 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18204 rhs0, rhs1));
18205 }
18206 else
18207 incr = RECUR (incr);
18208 break;
18209 case MODOP_EXPR:
18210 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18211 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18212 {
18213 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18214 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18215 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18216 TREE_TYPE (decl), lhs,
18217 RECUR (TREE_OPERAND (incr, 2))));
18218 }
18219 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18220 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18221 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18222 {
18223 tree rhs = TREE_OPERAND (incr, 2);
18224 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18225 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18226 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18227 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18228 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18229 rhs0, rhs1));
18230 }
18231 else
18232 incr = RECUR (incr);
18233 break;
18234 default:
18235 incr = RECUR (incr);
18236 break;
18237 }
18238
18239 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18240 TREE_VEC_ELT (orig_declv, i) = decl;
18241 TREE_VEC_ELT (declv, i) = decl;
18242 TREE_VEC_ELT (initv, i) = init;
18243 TREE_VEC_ELT (condv, i) = cond;
18244 TREE_VEC_ELT (incrv, i) = incr;
18245 return false;
18246 #undef RECUR
18247 }
18248
18249 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18250 of OMP_TARGET's body. */
18251
18252 static tree
18253 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18254 {
18255 *walk_subtrees = 0;
18256 switch (TREE_CODE (*tp))
18257 {
18258 case OMP_TEAMS:
18259 return *tp;
18260 case BIND_EXPR:
18261 case STATEMENT_LIST:
18262 *walk_subtrees = 1;
18263 break;
18264 default:
18265 break;
18266 }
18267 return NULL_TREE;
18268 }
18269
18270 /* Helper function for tsubst_expr. For decomposition declaration
18271 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18272 also the corresponding decls representing the identifiers
18273 of the decomposition declaration. Return DECL if successful
18274 or error_mark_node otherwise, set *FIRST to the first decl
18275 in the list chained through DECL_CHAIN and *CNT to the number
18276 of such decls. */
18277
18278 static tree
18279 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18280 tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
18281 {
18282 tree decl2, decl3, prev = decl;
18283 decomp->count = 0;
18284 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18285 for (decl2 = DECL_CHAIN (pattern_decl);
18286 decl2
18287 && VAR_P (decl2)
18288 && DECL_DECOMPOSITION_P (decl2)
18289 && DECL_NAME (decl2);
18290 decl2 = DECL_CHAIN (decl2))
18291 {
18292 if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
18293 {
18294 gcc_assert (errorcount);
18295 return error_mark_node;
18296 }
18297 decomp->count++;
18298 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18299 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18300 tree v = DECL_VALUE_EXPR (decl2);
18301 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18302 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18303 decl3 = tsubst (decl2, args, complain, in_decl);
18304 SET_DECL_VALUE_EXPR (decl2, v);
18305 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18306 if (VAR_P (decl3))
18307 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18308 else
18309 {
18310 gcc_assert (errorcount);
18311 decl = error_mark_node;
18312 continue;
18313 }
18314 maybe_push_decl (decl3);
18315 if (error_operand_p (decl3))
18316 decl = error_mark_node;
18317 else if (decl != error_mark_node
18318 && DECL_CHAIN (decl3) != prev
18319 && decl != prev)
18320 {
18321 gcc_assert (errorcount);
18322 decl = error_mark_node;
18323 }
18324 else
18325 prev = decl3;
18326 }
18327 decomp->decl = prev;
18328 return decl;
18329 }
18330
18331 /* Return the proper local_specialization for init-capture pack DECL. */
18332
18333 static tree
18334 lookup_init_capture_pack (tree decl)
18335 {
18336 /* We handle normal pack captures by forwarding to the specialization of the
18337 captured parameter. We can't do that for pack init-captures; we need them
18338 to have their own local_specialization. We created the individual
18339 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18340 when we process the DECL_EXPR for the pack init-capture in the template.
18341 So, how do we find them? We don't know the capture proxy pack when
18342 building the individual resulting proxies, and we don't know the
18343 individual proxies when instantiating the pack. What we have in common is
18344 the FIELD_DECL.
18345
18346 So...when we instantiate the FIELD_DECL, we stick the result in
18347 local_specializations. Then at the DECL_EXPR we look up that result, see
18348 how many elements it has, synthesize the names, and look them up. */
18349
18350 tree cname = DECL_NAME (decl);
18351 tree val = DECL_VALUE_EXPR (decl);
18352 tree field = TREE_OPERAND (val, 1);
18353 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18354 tree fpack = retrieve_local_specialization (field);
18355 if (fpack == error_mark_node)
18356 return error_mark_node;
18357
18358 int len = 1;
18359 tree vec = NULL_TREE;
18360 tree r = NULL_TREE;
18361 if (TREE_CODE (fpack) == TREE_VEC)
18362 {
18363 len = TREE_VEC_LENGTH (fpack);
18364 vec = make_tree_vec (len);
18365 r = make_node (NONTYPE_ARGUMENT_PACK);
18366 ARGUMENT_PACK_ARGS (r) = vec;
18367 }
18368 for (int i = 0; i < len; ++i)
18369 {
18370 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18371 tree elt = lookup_name (ename);
18372 if (vec)
18373 TREE_VEC_ELT (vec, i) = elt;
18374 else
18375 r = elt;
18376 }
18377 return r;
18378 }
18379
18380 /* T is an operand of a template tree being substituted. Return whether
18381 T is dependent such that we should suppress some warnings that would
18382 make sense if the substituted expression were written directly, like
18383 template <int I> bool f() { return I == 2; }
18384 We don't want to warn when instantiating f that comparing two constants
18385 always has the same value.
18386
18387 This is a more limited concept of dependence than instantiation-dependent;
18388 here we don't care whether substitution could fail. */
18389
18390 static bool
18391 dependent_operand_p (tree t)
18392 {
18393 while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
18394 t = TREE_OPERAND (t, 0);
18395 ++processing_template_decl;
18396 bool r = (potential_constant_expression (t)
18397 ? value_dependent_expression_p (t)
18398 : type_dependent_expression_p (t));
18399 --processing_template_decl;
18400 return r;
18401 }
18402
18403 /* A superset of tsubst_expr that also handles statement trees. */
18404
18405 static tree
18406 tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18407 {
18408 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18409 #define RECUR(NODE) \
18410 tsubst_stmt ((NODE), args, complain, in_decl)
18411
18412 tree stmt, tmp;
18413 tree r;
18414 location_t loc;
18415
18416 if (t == NULL_TREE || t == error_mark_node)
18417 return t;
18418
18419 loc = input_location;
18420 if (location_t eloc = cp_expr_location (t))
18421 input_location = eloc;
18422 if (STATEMENT_CODE_P (TREE_CODE (t)))
18423 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18424
18425 switch (TREE_CODE (t))
18426 {
18427 case STATEMENT_LIST:
18428 {
18429 for (tree stmt : tsi_range (t))
18430 RECUR (stmt);
18431 break;
18432 }
18433
18434 case CTOR_INITIALIZER:
18435 finish_mem_initializers (tsubst_initializer_list
18436 (TREE_OPERAND (t, 0), args));
18437 break;
18438
18439 case RETURN_EXPR:
18440 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18441 break;
18442
18443 case CO_RETURN_EXPR:
18444 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18445 break;
18446
18447 case EXPR_STMT:
18448 tmp = RECUR (EXPR_STMT_EXPR (t));
18449 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18450 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18451 else
18452 finish_expr_stmt (tmp);
18453 break;
18454
18455 case USING_STMT:
18456 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18457 break;
18458
18459 case PRECONDITION_STMT:
18460 case POSTCONDITION_STMT:
18461 gcc_unreachable ();
18462
18463 case ASSERTION_STMT:
18464 {
18465 r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
18466 if (r != error_mark_node)
18467 add_stmt (r);
18468 RETURN (r);
18469 }
18470 break;
18471
18472 case DECL_EXPR:
18473 {
18474 tree decl, pattern_decl;
18475 tree init;
18476
18477 pattern_decl = decl = DECL_EXPR_DECL (t);
18478 if (TREE_CODE (decl) == LABEL_DECL)
18479 finish_label_decl (DECL_NAME (decl));
18480 else if (TREE_CODE (decl) == USING_DECL)
18481 {
18482 tree scope = USING_DECL_SCOPE (decl);
18483 if (DECL_DEPENDENT_P (decl))
18484 {
18485 scope = tsubst (scope, args, complain, in_decl);
18486 if (!MAYBE_CLASS_TYPE_P (scope)
18487 && TREE_CODE (scope) != ENUMERAL_TYPE)
18488 {
18489 if (complain & tf_error)
18490 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18491 "class, namespace, or enumeration", scope);
18492 return error_mark_node;
18493 }
18494 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18495 }
18496 else
18497 {
18498 /* This is a non-dependent using-decl, and we'll have
18499 used the names it found during template parsing. We do
18500 not want to do the lookup again, because we might not
18501 find the things we found then. */
18502 gcc_checking_assert (scope == tsubst (scope, args,
18503 complain, in_decl));
18504 /* We still need to push the bindings so that we can look up
18505 this name later. */
18506 push_using_decl_bindings (DECL_NAME (decl),
18507 USING_DECL_DECLS (decl));
18508 }
18509 }
18510 else if (is_capture_proxy (decl)
18511 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18512 {
18513 /* We're in tsubst_lambda_expr, we've already inserted a new
18514 capture proxy, so look it up and register it. */
18515 tree inst;
18516 if (!DECL_PACK_P (decl))
18517 {
18518 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18519 LOOK_want::HIDDEN_LAMBDA);
18520 gcc_assert (inst != decl && is_capture_proxy (inst));
18521 }
18522 else if (is_normal_capture_proxy (decl))
18523 {
18524 inst = (retrieve_local_specialization
18525 (DECL_CAPTURED_VARIABLE (decl)));
18526 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18527 || DECL_PACK_P (inst));
18528 }
18529 else
18530 inst = lookup_init_capture_pack (decl);
18531
18532 register_local_specialization (inst, decl);
18533 break;
18534 }
18535 else if (DECL_PRETTY_FUNCTION_P (decl))
18536 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18537 DECL_NAME (decl),
18538 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18539 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18540 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18541 /* Don't copy the old closure; we'll create a new one in
18542 tsubst_lambda_expr. */
18543 break;
18544 else
18545 {
18546 init = DECL_INITIAL (decl);
18547 decl = tsubst (decl, args, complain, in_decl);
18548 if (decl != error_mark_node)
18549 {
18550 /* By marking the declaration as instantiated, we avoid
18551 trying to instantiate it. Since instantiate_decl can't
18552 handle local variables, and since we've already done
18553 all that needs to be done, that's the right thing to
18554 do. */
18555 if (VAR_P (decl))
18556 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18557 if (VAR_P (decl) && !DECL_NAME (decl)
18558 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18559 /* Anonymous aggregates are a special case. */
18560 finish_anon_union (decl);
18561 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18562 {
18563 DECL_CONTEXT (decl) = current_function_decl;
18564 if (DECL_NAME (decl) == this_identifier)
18565 {
18566 tree lam = DECL_CONTEXT (current_function_decl);
18567 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18568 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18569 }
18570 insert_capture_proxy (decl);
18571 }
18572 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18573 /* We already did a pushtag. */;
18574 else if (VAR_OR_FUNCTION_DECL_P (decl)
18575 && DECL_LOCAL_DECL_P (decl))
18576 {
18577 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18578 DECL_CONTEXT (decl) = NULL_TREE;
18579 decl = pushdecl (decl);
18580 if (TREE_CODE (decl) == FUNCTION_DECL
18581 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18582 && cp_check_omp_declare_reduction (decl))
18583 instantiate_body (pattern_decl, args, decl, true);
18584 }
18585 else
18586 {
18587 bool const_init = false;
18588 cp_decomp decomp_d, *decomp = NULL;
18589 tree ndecl = error_mark_node;
18590 tree asmspec_tree = NULL_TREE;
18591 maybe_push_decl (decl);
18592
18593 if (VAR_P (decl)
18594 && DECL_LANG_SPECIFIC (decl)
18595 && DECL_OMP_PRIVATIZED_MEMBER (decl))
18596 break;
18597
18598 if (VAR_P (decl)
18599 && DECL_DECOMPOSITION_P (decl)
18600 && TREE_TYPE (pattern_decl) != error_mark_node)
18601 {
18602 decomp = &decomp_d;
18603 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18604 complain, in_decl, decomp);
18605 }
18606
18607 init = tsubst_init (init, decl, args, complain, in_decl);
18608
18609 if (VAR_P (decl))
18610 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18611 (pattern_decl));
18612
18613 /* In a non-template function, VLA type declarations are
18614 handled in grokdeclarator; for templates, handle them
18615 now. */
18616 predeclare_vla (decl);
18617
18618 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18619 {
18620 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18621 const char *asmspec = IDENTIFIER_POINTER (id);
18622 gcc_assert (asmspec[0] == '*');
18623 asmspec_tree
18624 = build_string (IDENTIFIER_LENGTH (id) - 1,
18625 asmspec + 1);
18626 TREE_TYPE (asmspec_tree) = char_array_type_node;
18627 }
18628
18629 cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
18630 decomp);
18631
18632 if (ndecl != error_mark_node)
18633 cp_finish_decomp (ndecl, decomp);
18634 }
18635 }
18636 }
18637
18638 break;
18639 }
18640
18641 case FOR_STMT:
18642 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18643 RECUR (FOR_INIT_STMT (t));
18644 finish_init_stmt (stmt);
18645 tmp = RECUR (FOR_COND (t));
18646 finish_for_cond (tmp, stmt, false, 0, false);
18647 tmp = RECUR (FOR_EXPR (t));
18648 finish_for_expr (tmp, stmt);
18649 {
18650 bool prev = note_iteration_stmt_body_start ();
18651 RECUR (FOR_BODY (t));
18652 note_iteration_stmt_body_end (prev);
18653 }
18654 finish_for_stmt (stmt);
18655 break;
18656
18657 case RANGE_FOR_STMT:
18658 {
18659 /* Construct another range_for, if this is not a final
18660 substitution (for inside a generic lambda of a
18661 template). Otherwise convert to a regular for. */
18662 tree decl, expr;
18663 stmt = (processing_template_decl
18664 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18665 : begin_for_stmt (NULL_TREE, NULL_TREE));
18666 RECUR (RANGE_FOR_INIT_STMT (t));
18667 decl = RANGE_FOR_DECL (t);
18668 decl = tsubst (decl, args, complain, in_decl);
18669 maybe_push_decl (decl);
18670 expr = RECUR (RANGE_FOR_EXPR (t));
18671
18672 cp_decomp decomp_d, *decomp = NULL;
18673 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18674 {
18675 decomp = &decomp_d;
18676 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18677 complain, in_decl, decomp);
18678 }
18679
18680 tree unroll = RECUR (RANGE_FOR_UNROLL (t));
18681 if (unroll)
18682 unroll
18683 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
18684 unroll);
18685 if (processing_template_decl)
18686 {
18687 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18688 RANGE_FOR_UNROLL (stmt) = unroll;
18689 RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
18690 finish_range_for_decl (stmt, decl, expr);
18691 if (decomp && decl != error_mark_node)
18692 cp_finish_decomp (decl, decomp);
18693 }
18694 else
18695 stmt = cp_convert_range_for (stmt, decl, expr, decomp,
18696 RANGE_FOR_IVDEP (t), unroll,
18697 RANGE_FOR_NOVECTOR (t));
18698
18699 bool prev = note_iteration_stmt_body_start ();
18700 RECUR (RANGE_FOR_BODY (t));
18701 note_iteration_stmt_body_end (prev);
18702 finish_for_stmt (stmt);
18703 }
18704 break;
18705
18706 case WHILE_STMT:
18707 stmt = begin_while_stmt ();
18708 tmp = RECUR (WHILE_COND (t));
18709 finish_while_stmt_cond (tmp, stmt, false, 0, false);
18710 {
18711 bool prev = note_iteration_stmt_body_start ();
18712 RECUR (WHILE_BODY (t));
18713 note_iteration_stmt_body_end (prev);
18714 }
18715 finish_while_stmt (stmt);
18716 break;
18717
18718 case DO_STMT:
18719 stmt = begin_do_stmt ();
18720 {
18721 bool prev = note_iteration_stmt_body_start ();
18722 RECUR (DO_BODY (t));
18723 note_iteration_stmt_body_end (prev);
18724 }
18725 finish_do_body (stmt);
18726 tmp = RECUR (DO_COND (t));
18727 finish_do_stmt (tmp, stmt, false, 0, false);
18728 break;
18729
18730 case IF_STMT:
18731 stmt = begin_if_stmt ();
18732 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18733 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
18734 if (IF_STMT_CONSTEXPR_P (t))
18735 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
18736 {
18737 tree cond = IF_COND (t);
18738 bool was_dep = dependent_operand_p (cond);
18739 cond = RECUR (cond);
18740 warning_sentinel s1(warn_address, was_dep);
18741 tmp = finish_if_stmt_cond (cond, stmt);
18742 }
18743 if (IF_STMT_CONSTEXPR_P (t)
18744 && instantiation_dependent_expression_p (tmp))
18745 {
18746 /* We're partially instantiating a generic lambda, but the condition
18747 of the constexpr if is still dependent. Don't substitute into the
18748 branches now, just remember the template arguments. */
18749 do_poplevel (IF_SCOPE (stmt));
18750 IF_SCOPE (stmt) = NULL_TREE;
18751 IF_COND (stmt) = IF_COND (t);
18752 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18753 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18754 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
18755 add_stmt (stmt);
18756 break;
18757 }
18758 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18759 /* Don't instantiate the THEN_CLAUSE. */;
18760 else if (IF_STMT_CONSTEVAL_P (t))
18761 {
18762 bool save_in_consteval_if_p = in_consteval_if_p;
18763 in_consteval_if_p = true;
18764 RECUR (THEN_CLAUSE (t));
18765 in_consteval_if_p = save_in_consteval_if_p;
18766 }
18767 else
18768 {
18769 tree folded = fold_non_dependent_expr (tmp, complain);
18770 bool inhibit = integer_zerop (folded);
18771 if (inhibit)
18772 ++c_inhibit_evaluation_warnings;
18773 RECUR (THEN_CLAUSE (t));
18774 if (inhibit)
18775 --c_inhibit_evaluation_warnings;
18776 }
18777 finish_then_clause (stmt);
18778
18779 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18780 /* Don't instantiate the ELSE_CLAUSE. */;
18781 else if (ELSE_CLAUSE (t))
18782 {
18783 tree folded = fold_non_dependent_expr (tmp, complain);
18784 bool inhibit = integer_nonzerop (folded);
18785 begin_else_clause (stmt);
18786 if (inhibit)
18787 ++c_inhibit_evaluation_warnings;
18788 RECUR (ELSE_CLAUSE (t));
18789 if (inhibit)
18790 --c_inhibit_evaluation_warnings;
18791 finish_else_clause (stmt);
18792 }
18793
18794 finish_if_stmt (stmt);
18795 break;
18796
18797 case BIND_EXPR:
18798 if (BIND_EXPR_BODY_BLOCK (t))
18799 stmt = begin_function_body ();
18800 else
18801 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18802 ? BCS_TRY_BLOCK : 0);
18803
18804 RECUR (BIND_EXPR_BODY (t));
18805
18806 if (BIND_EXPR_BODY_BLOCK (t))
18807 finish_function_body (stmt);
18808 else
18809 finish_compound_stmt (stmt);
18810 break;
18811
18812 case BREAK_STMT:
18813 finish_break_stmt ();
18814 break;
18815
18816 case CONTINUE_STMT:
18817 finish_continue_stmt ();
18818 break;
18819
18820 case SWITCH_STMT:
18821 stmt = begin_switch_stmt ();
18822 tmp = RECUR (SWITCH_STMT_COND (t));
18823 finish_switch_cond (tmp, stmt);
18824 RECUR (SWITCH_STMT_BODY (t));
18825 finish_switch_stmt (stmt);
18826 break;
18827
18828 case CASE_LABEL_EXPR:
18829 {
18830 tree decl = CASE_LABEL (t);
18831 tree low = RECUR (CASE_LOW (t));
18832 tree high = RECUR (CASE_HIGH (t));
18833 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18834 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18835 {
18836 tree label = CASE_LABEL (l);
18837 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18838 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18839 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18840 }
18841 }
18842 break;
18843
18844 case LABEL_EXPR:
18845 {
18846 tree decl = LABEL_EXPR_LABEL (t);
18847 tree label;
18848
18849 label = finish_label_stmt (DECL_NAME (decl));
18850 if (TREE_CODE (label) == LABEL_DECL)
18851 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18852 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18853 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18854 }
18855 break;
18856
18857 case GOTO_EXPR:
18858 tmp = GOTO_DESTINATION (t);
18859 if (TREE_CODE (tmp) != LABEL_DECL)
18860 /* Computed goto's must be tsubst'd into. On the other hand,
18861 non-computed gotos must not be; the identifier in question
18862 will have no binding. */
18863 tmp = RECUR (tmp);
18864 else
18865 tmp = DECL_NAME (tmp);
18866 finish_goto_stmt (tmp);
18867 break;
18868
18869 case ASM_EXPR:
18870 {
18871 tree string = RECUR (ASM_STRING (t));
18872 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18873 complain, in_decl);
18874 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18875 complain, in_decl);
18876 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18877 complain, in_decl);
18878 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18879 complain, in_decl);
18880 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18881 outputs, inputs, clobbers, labels,
18882 ASM_INLINE_P (t));
18883 tree asm_expr = tmp;
18884 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18885 asm_expr = TREE_OPERAND (asm_expr, 0);
18886 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18887 }
18888 break;
18889
18890 case TRY_BLOCK:
18891 if (CLEANUP_P (t))
18892 {
18893 stmt = begin_try_block ();
18894 RECUR (TRY_STMTS (t));
18895 finish_cleanup_try_block (stmt);
18896 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18897 }
18898 else
18899 {
18900 tree compound_stmt = NULL_TREE;
18901
18902 if (FN_TRY_BLOCK_P (t))
18903 stmt = begin_function_try_block (&compound_stmt);
18904 else
18905 stmt = begin_try_block ();
18906
18907 RECUR (TRY_STMTS (t));
18908
18909 if (FN_TRY_BLOCK_P (t))
18910 finish_function_try_block (stmt);
18911 else
18912 finish_try_block (stmt);
18913
18914 RECUR (TRY_HANDLERS (t));
18915 if (FN_TRY_BLOCK_P (t))
18916 finish_function_handler_sequence (stmt, compound_stmt);
18917 else
18918 finish_handler_sequence (stmt);
18919 }
18920 break;
18921
18922 case HANDLER:
18923 {
18924 tree decl = HANDLER_PARMS (t);
18925
18926 if (decl)
18927 {
18928 decl = tsubst (decl, args, complain, in_decl);
18929 /* Prevent instantiate_decl from trying to instantiate
18930 this variable. We've already done all that needs to be
18931 done. */
18932 if (decl != error_mark_node)
18933 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18934 }
18935 stmt = begin_handler ();
18936 finish_handler_parms (decl, stmt);
18937 RECUR (HANDLER_BODY (t));
18938 finish_handler (stmt);
18939 }
18940 break;
18941
18942 case TAG_DEFN:
18943 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18944 if (dependent_type_p (tmp))
18945 /* This is a partial instantiation, try again when full. */
18946 add_stmt (build_min (TAG_DEFN, tmp));
18947 else if (CLASS_TYPE_P (tmp))
18948 {
18949 /* Local classes are not independent templates; they are
18950 instantiated along with their containing function. And this
18951 way we don't have to deal with pushing out of one local class
18952 to instantiate a member of another local class. */
18953 /* Closures are handled by the LAMBDA_EXPR. */
18954 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18955 complete_type (tmp);
18956 tree save_ccp = current_class_ptr;
18957 tree save_ccr = current_class_ref;
18958 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18959 if ((VAR_P (fld)
18960 || (TREE_CODE (fld) == FUNCTION_DECL
18961 && !DECL_ARTIFICIAL (fld)))
18962 && DECL_TEMPLATE_INSTANTIATION (fld))
18963 instantiate_decl (fld, /*defer_ok=*/false,
18964 /*expl_inst_class=*/false);
18965 else if (TREE_CODE (fld) == FIELD_DECL)
18966 maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
18967 current_class_ptr = save_ccp;
18968 current_class_ref = save_ccr;
18969 }
18970 break;
18971
18972 case STATIC_ASSERT:
18973 {
18974 tree condition, message;
18975
18976 ++c_inhibit_evaluation_warnings;
18977 condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
18978 complain, in_decl);
18979 message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
18980 complain, in_decl);
18981 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
18982 && TREE_CODE (message) == STRING_CST)
18983 message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
18984 PAREN_EXPR, TREE_TYPE (message), message);
18985 --c_inhibit_evaluation_warnings;
18986
18987 finish_static_assert (condition, message,
18988 STATIC_ASSERT_SOURCE_LOCATION (t),
18989 /*member_p=*/false, /*show_expr_p=*/true);
18990 }
18991 break;
18992
18993 case OACC_KERNELS:
18994 case OACC_PARALLEL:
18995 case OACC_SERIAL:
18996 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
18997 complain, in_decl);
18998 stmt = begin_omp_parallel ();
18999 RECUR (OMP_BODY (t));
19000 finish_omp_construct (TREE_CODE (t), stmt, tmp);
19001 break;
19002
19003 case OMP_PARALLEL:
19004 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
19005 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
19006 complain, in_decl);
19007 if (OMP_PARALLEL_COMBINED (t))
19008 omp_parallel_combined_clauses = &tmp;
19009 stmt = begin_omp_parallel ();
19010 RECUR (OMP_PARALLEL_BODY (t));
19011 gcc_assert (omp_parallel_combined_clauses == NULL);
19012 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
19013 = OMP_PARALLEL_COMBINED (t);
19014 pop_omp_privatization_clauses (r);
19015 break;
19016
19017 case OMP_TASK:
19018 if (OMP_TASK_BODY (t) == NULL_TREE)
19019 {
19020 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19021 complain, in_decl);
19022 t = copy_node (t);
19023 OMP_TASK_CLAUSES (t) = tmp;
19024 add_stmt (t);
19025 break;
19026 }
19027 r = push_omp_privatization_clauses (false);
19028 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19029 complain, in_decl);
19030 stmt = begin_omp_task ();
19031 RECUR (OMP_TASK_BODY (t));
19032 finish_omp_task (tmp, stmt);
19033 pop_omp_privatization_clauses (r);
19034 break;
19035
19036 case OMP_FOR:
19037 case OMP_LOOP:
19038 case OMP_SIMD:
19039 case OMP_DISTRIBUTE:
19040 case OMP_TASKLOOP:
19041 case OACC_LOOP:
19042 {
19043 tree clauses, body, pre_body;
19044 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
19045 tree orig_declv = NULL_TREE;
19046 tree incrv = NULL_TREE;
19047 enum c_omp_region_type ort = C_ORT_OMP;
19048 bool any_range_for = false;
19049 int i;
19050
19051 if (TREE_CODE (t) == OACC_LOOP)
19052 ort = C_ORT_ACC;
19053
19054 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
19055 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
19056 in_decl);
19057 if (OMP_FOR_INIT (t) != NULL_TREE)
19058 {
19059 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19060 if (OMP_FOR_ORIG_DECLS (t))
19061 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19062 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19063 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19064 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19065 }
19066
19067 keep_next_level (true);
19068 stmt = begin_omp_structured_block ();
19069
19070 pre_body = push_stmt_list ();
19071 RECUR (OMP_FOR_PRE_BODY (t));
19072 pre_body = pop_stmt_list (pre_body);
19073
19074 if (OMP_FOR_INIT (t) != NULL_TREE)
19075 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19076 any_range_for
19077 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
19078 condv, incrv, &clauses, args,
19079 complain, in_decl);
19080 omp_parallel_combined_clauses = NULL;
19081
19082 if (any_range_for)
19083 {
19084 gcc_assert (orig_declv);
19085 body = begin_omp_structured_block ();
19086 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19087 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
19088 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
19089 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
19090 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
19091 TREE_VEC_ELT (declv, i));
19092 }
19093 else
19094 body = push_stmt_list ();
19095 RECUR (OMP_FOR_BODY (t));
19096 if (any_range_for)
19097 body = finish_omp_structured_block (body);
19098 else
19099 body = pop_stmt_list (body);
19100
19101 if (OMP_FOR_INIT (t) != NULL_TREE)
19102 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
19103 orig_declv, initv, condv, incrv, body, pre_body,
19104 NULL, clauses);
19105 else
19106 {
19107 t = make_node (TREE_CODE (t));
19108 TREE_TYPE (t) = void_type_node;
19109 OMP_FOR_BODY (t) = body;
19110 OMP_FOR_PRE_BODY (t) = pre_body;
19111 OMP_FOR_CLAUSES (t) = clauses;
19112 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
19113 add_stmt (t);
19114 }
19115
19116 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
19117 t));
19118 pop_omp_privatization_clauses (r);
19119 }
19120 break;
19121
19122 case OMP_SECTIONS:
19123 case OMP_MASKED:
19124 omp_parallel_combined_clauses = NULL;
19125 /* FALLTHRU */
19126 case OMP_SINGLE:
19127 case OMP_SCOPE:
19128 case OMP_TEAMS:
19129 case OMP_CRITICAL:
19130 case OMP_TASKGROUP:
19131 case OMP_SCAN:
19132 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
19133 && OMP_TEAMS_COMBINED (t));
19134 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
19135 in_decl);
19136 if (TREE_CODE (t) == OMP_TEAMS)
19137 {
19138 keep_next_level (true);
19139 stmt = begin_omp_structured_block ();
19140 RECUR (OMP_BODY (t));
19141 stmt = finish_omp_structured_block (stmt);
19142 }
19143 else
19144 {
19145 stmt = push_stmt_list ();
19146 RECUR (OMP_BODY (t));
19147 stmt = pop_stmt_list (stmt);
19148 }
19149
19150 if (TREE_CODE (t) == OMP_CRITICAL
19151 && tmp != NULL_TREE
19152 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
19153 {
19154 error_at (OMP_CLAUSE_LOCATION (tmp),
19155 "%<#pragma omp critical%> with %<hint%> clause requires "
19156 "a name, except when %<omp_sync_hint_none%> is used");
19157 RETURN (error_mark_node);
19158 }
19159 t = copy_node (t);
19160 OMP_BODY (t) = stmt;
19161 OMP_CLAUSES (t) = tmp;
19162 add_stmt (t);
19163 pop_omp_privatization_clauses (r);
19164 break;
19165
19166 case OMP_DEPOBJ:
19167 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
19168 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
19169 {
19170 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
19171 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
19172 {
19173 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
19174 args, complain, in_decl);
19175 if (tmp == NULL_TREE)
19176 tmp = error_mark_node;
19177 }
19178 else
19179 {
19180 kind = (enum omp_clause_depend_kind)
19181 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
19182 tmp = NULL_TREE;
19183 }
19184 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
19185 }
19186 else
19187 finish_omp_depobj (EXPR_LOCATION (t), r,
19188 OMP_CLAUSE_DEPEND_INVALID,
19189 OMP_DEPOBJ_CLAUSES (t));
19190 break;
19191
19192 case OACC_DATA:
19193 case OMP_TARGET_DATA:
19194 case OMP_TARGET:
19195 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
19196 TREE_CODE (t) == OACC_DATA
19197 ? C_ORT_ACC
19198 : TREE_CODE (t) == OMP_TARGET
19199 ? C_ORT_OMP_TARGET : C_ORT_OMP,
19200 args, complain, in_decl);
19201 keep_next_level (true);
19202 stmt = begin_omp_structured_block ();
19203
19204 RECUR (OMP_BODY (t));
19205 stmt = finish_omp_structured_block (stmt);
19206
19207 t = copy_node (t);
19208 OMP_BODY (t) = stmt;
19209 OMP_CLAUSES (t) = tmp;
19210
19211 if (TREE_CODE (t) == OMP_TARGET)
19212 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
19213 &OMP_CLAUSES (t));
19214
19215 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19216 {
19217 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19218 if (teams)
19219 /* For combined target teams, ensure the num_teams and
19220 thread_limit clause expressions are evaluated on the host,
19221 before entering the target construct. */
19222 for (tree c = OMP_TEAMS_CLAUSES (teams);
19223 c; c = OMP_CLAUSE_CHAIN (c))
19224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19226 for (int i = 0;
19227 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
19228 if (OMP_CLAUSE_OPERAND (c, i)
19229 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
19230 {
19231 tree expr = OMP_CLAUSE_OPERAND (c, i);
19232 expr = force_target_expr (TREE_TYPE (expr), expr,
19233 tf_none);
19234 if (expr == error_mark_node)
19235 continue;
19236 tmp = TARGET_EXPR_SLOT (expr);
19237 add_stmt (expr);
19238 OMP_CLAUSE_OPERAND (c, i) = expr;
19239 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19240 OMP_CLAUSE_FIRSTPRIVATE);
19241 OMP_CLAUSE_DECL (tc) = tmp;
19242 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19243 OMP_TARGET_CLAUSES (t) = tc;
19244 }
19245 }
19246 add_stmt (t);
19247 break;
19248
19249 case OACC_DECLARE:
19250 t = copy_node (t);
19251 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
19252 complain, in_decl);
19253 OACC_DECLARE_CLAUSES (t) = tmp;
19254 add_stmt (t);
19255 break;
19256
19257 case OMP_TARGET_UPDATE:
19258 case OMP_TARGET_ENTER_DATA:
19259 case OMP_TARGET_EXIT_DATA:
19260 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
19261 complain, in_decl);
19262 t = copy_node (t);
19263 OMP_STANDALONE_CLAUSES (t) = tmp;
19264 add_stmt (t);
19265 break;
19266
19267 case OACC_CACHE:
19268 case OACC_ENTER_DATA:
19269 case OACC_EXIT_DATA:
19270 case OACC_UPDATE:
19271 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
19272 complain, in_decl);
19273 t = copy_node (t);
19274 OMP_STANDALONE_CLAUSES (t) = tmp;
19275 add_stmt (t);
19276 break;
19277
19278 case OMP_ORDERED:
19279 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
19280 complain, in_decl);
19281 if (OMP_BODY (t))
19282 {
19283 stmt = push_stmt_list ();
19284 RECUR (OMP_BODY (t));
19285 stmt = pop_stmt_list (stmt);
19286 }
19287 else
19288 stmt = NULL_TREE;
19289
19290 t = copy_node (t);
19291 OMP_BODY (t) = stmt;
19292 OMP_ORDERED_CLAUSES (t) = tmp;
19293 add_stmt (t);
19294 break;
19295
19296 case OMP_MASTER:
19297 case OMP_STRUCTURED_BLOCK:
19298 omp_parallel_combined_clauses = NULL;
19299 /* FALLTHRU */
19300 case OMP_SECTION:
19301 stmt = push_stmt_list ();
19302 RECUR (OMP_BODY (t));
19303 stmt = pop_stmt_list (stmt);
19304
19305 t = copy_node (t);
19306 OMP_BODY (t) = stmt;
19307 add_stmt (t);
19308 break;
19309
19310 case OMP_ATOMIC:
19311 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19312 tmp = NULL_TREE;
19313 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19314 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
19315 complain, in_decl);
19316 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19317 {
19318 tree op1 = TREE_OPERAND (t, 1);
19319 tree rhs1 = NULL_TREE;
19320 tree r = NULL_TREE;
19321 tree lhs, rhs;
19322 if (TREE_CODE (op1) == COMPOUND_EXPR)
19323 {
19324 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19325 op1 = TREE_OPERAND (op1, 1);
19326 }
19327 if (TREE_CODE (op1) == COND_EXPR)
19328 {
19329 gcc_assert (rhs1 == NULL_TREE);
19330 tree c = TREE_OPERAND (op1, 0);
19331 if (TREE_CODE (c) == MODIFY_EXPR)
19332 {
19333 r = RECUR (TREE_OPERAND (c, 0));
19334 c = TREE_OPERAND (c, 1);
19335 }
19336 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19337 rhs = RECUR (TREE_OPERAND (c, 1));
19338 lhs = RECUR (TREE_OPERAND (op1, 2));
19339 rhs1 = RECUR (TREE_OPERAND (op1, 1));
19340 }
19341 else
19342 {
19343 lhs = RECUR (TREE_OPERAND (op1, 0));
19344 rhs = RECUR (TREE_OPERAND (op1, 1));
19345 }
19346 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19347 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
19348 tmp, OMP_ATOMIC_MEMORY_ORDER (t),
19349 OMP_ATOMIC_WEAK (t));
19350 }
19351 else
19352 {
19353 tree op1 = TREE_OPERAND (t, 1);
19354 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19355 tree rhs1 = NULL_TREE, r = NULL_TREE;
19356 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19357 enum tree_code opcode = NOP_EXPR;
19358 if (code == OMP_ATOMIC_READ)
19359 {
19360 v = RECUR (TREE_OPERAND (op1, 0));
19361 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19362 }
19363 else if (code == OMP_ATOMIC_CAPTURE_OLD
19364 || code == OMP_ATOMIC_CAPTURE_NEW)
19365 {
19366 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19367 v = RECUR (TREE_OPERAND (op1, 0));
19368 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19369 if (TREE_CODE (op11) == COMPOUND_EXPR)
19370 {
19371 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19372 op11 = TREE_OPERAND (op11, 1);
19373 }
19374 if (TREE_CODE (op11) == COND_EXPR)
19375 {
19376 gcc_assert (rhs1 == NULL_TREE);
19377 tree c = TREE_OPERAND (op11, 0);
19378 if (TREE_CODE (c) == MODIFY_EXPR)
19379 {
19380 r = RECUR (TREE_OPERAND (c, 0));
19381 c = TREE_OPERAND (c, 1);
19382 }
19383 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19384 rhs = RECUR (TREE_OPERAND (c, 1));
19385 lhs = RECUR (TREE_OPERAND (op11, 2));
19386 rhs1 = RECUR (TREE_OPERAND (op11, 1));
19387 }
19388 else
19389 {
19390 lhs = RECUR (TREE_OPERAND (op11, 0));
19391 rhs = RECUR (TREE_OPERAND (op11, 1));
19392 }
19393 opcode = TREE_CODE (op11);
19394 if (opcode == MODIFY_EXPR)
19395 opcode = NOP_EXPR;
19396 }
19397 else
19398 {
19399 code = OMP_ATOMIC;
19400 lhs = RECUR (TREE_OPERAND (op1, 0));
19401 rhs = RECUR (TREE_OPERAND (op1, 1));
19402 }
19403 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19404 lhs1, rhs1, r, tmp,
19405 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
19406 }
19407 break;
19408
19409 case TRANSACTION_EXPR:
19410 {
19411 int flags = 0;
19412 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19413 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19414
19415 if (TRANSACTION_EXPR_IS_STMT (t))
19416 {
19417 tree body = TRANSACTION_EXPR_BODY (t);
19418 tree noex = NULL_TREE;
19419 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19420 {
19421 noex = MUST_NOT_THROW_COND (body);
19422 if (noex == NULL_TREE)
19423 noex = boolean_true_node;
19424 body = TREE_OPERAND (body, 0);
19425 }
19426 stmt = begin_transaction_stmt (input_location, NULL, flags);
19427 RECUR (body);
19428 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19429 }
19430 else
19431 {
19432 stmt = build_transaction_expr (EXPR_LOCATION (t),
19433 RECUR (TRANSACTION_EXPR_BODY (t)),
19434 flags, NULL_TREE);
19435 RETURN (stmt);
19436 }
19437 }
19438 break;
19439
19440 case MUST_NOT_THROW_EXPR:
19441 {
19442 tree op0 = RECUR (TREE_OPERAND (t, 0));
19443 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19444 RETURN (build_must_not_throw_expr (op0, cond));
19445 }
19446
19447 case EXPR_PACK_EXPANSION:
19448 error ("invalid use of pack expansion expression");
19449 RETURN (error_mark_node);
19450
19451 case NONTYPE_ARGUMENT_PACK:
19452 error ("use %<...%> to expand argument pack");
19453 RETURN (error_mark_node);
19454
19455 case COMPOUND_EXPR:
19456 tmp = RECUR (TREE_OPERAND (t, 0));
19457 if (tmp == NULL_TREE)
19458 /* If the first operand was a statement, we're done with it. */
19459 RETURN (RECUR (TREE_OPERAND (t, 1)));
19460 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19461 RECUR (TREE_OPERAND (t, 1)),
19462 templated_operator_saved_lookups (t),
19463 complain));
19464
19465 case PREDICT_EXPR:
19466 RETURN (add_stmt (copy_node (t)));
19467
19468 case ANNOTATE_EXPR:
19469 {
19470 /* Although ANNOTATE_EXPR is an expression, it can only appear in
19471 WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
19472 using tsubst_stmt rather than tsubst_expr and can contain
19473 DECL_EXPRs. */
19474 tree op1 = RECUR (TREE_OPERAND (t, 0));
19475 tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
19476 tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
19477 if (TREE_CODE (op2) == INTEGER_CST
19478 && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
19479 op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
19480 op3);
19481 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19482 TREE_TYPE (op1), op1, op2, op3));
19483 }
19484
19485 default:
19486 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19487
19488 RETURN (tsubst_expr (t, args, complain, in_decl));
19489 }
19490
19491 RETURN (NULL_TREE);
19492 out:
19493 input_location = loc;
19494 return r;
19495 #undef RECUR
19496 #undef RETURN
19497 }
19498
19499 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19500 function. For description of the body see comment above
19501 cp_parser_omp_declare_reduction_exprs. */
19502
19503 static void
19504 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19505 {
19506 if (t == NULL_TREE || t == error_mark_node)
19507 return;
19508
19509 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19510
19511 tree_stmt_iterator tsi;
19512 int i;
19513 tree stmts[7];
19514 memset (stmts, 0, sizeof stmts);
19515 for (i = 0, tsi = tsi_start (t);
19516 i < 7 && !tsi_end_p (tsi);
19517 i++, tsi_next (&tsi))
19518 stmts[i] = tsi_stmt (tsi);
19519 gcc_assert (tsi_end_p (tsi));
19520
19521 if (i >= 3)
19522 {
19523 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19524 && TREE_CODE (stmts[1]) == DECL_EXPR);
19525 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19526 args, complain, in_decl);
19527 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19528 args, complain, in_decl);
19529 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19530 expect to be pushing it. */
19531 DECL_CONTEXT (omp_out) = current_function_decl;
19532 DECL_CONTEXT (omp_in) = current_function_decl;
19533 keep_next_level (true);
19534 tree block = begin_omp_structured_block ();
19535 tsubst_stmt (stmts[2], args, complain, in_decl);
19536 block = finish_omp_structured_block (block);
19537 block = maybe_cleanup_point_expr_void (block);
19538 add_decl_expr (omp_out);
19539 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
19540 add_decl_expr (omp_in);
19541 finish_expr_stmt (block);
19542 }
19543 if (i >= 6)
19544 {
19545 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19546 && TREE_CODE (stmts[4]) == DECL_EXPR);
19547 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19548 args, complain, in_decl);
19549 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19550 args, complain, in_decl);
19551 DECL_CONTEXT (omp_priv) = current_function_decl;
19552 DECL_CONTEXT (omp_orig) = current_function_decl;
19553 keep_next_level (true);
19554 tree block = begin_omp_structured_block ();
19555 tsubst_stmt (stmts[5], args, complain, in_decl);
19556 block = finish_omp_structured_block (block);
19557 block = maybe_cleanup_point_expr_void (block);
19558 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19559 add_decl_expr (omp_priv);
19560 add_decl_expr (omp_orig);
19561 finish_expr_stmt (block);
19562 if (i == 7)
19563 add_decl_expr (omp_orig);
19564 }
19565 }
19566
19567 /* T is a postfix-expression that is not being used in a function
19568 call. Return the substituted version of T. */
19569
19570 static tree
19571 tsubst_non_call_postfix_expression (tree t, tree args,
19572 tsubst_flags_t complain,
19573 tree in_decl)
19574 {
19575 if (TREE_CODE (t) == SCOPE_REF)
19576 t = tsubst_qualified_id (t, args, complain, in_decl,
19577 /*done=*/false, /*address_p=*/false);
19578 else
19579 t = tsubst_expr (t, args, complain, in_decl);
19580
19581 return t;
19582 }
19583
19584 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19585 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19586 dependent init-capture. EXPLICIT_P is true if the original list had
19587 explicit captures. */
19588
19589 static void
19590 prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
19591 tsubst_flags_t complain)
19592 {
19593 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19594 {
19595 tree type = NULL_TREE;
19596 if (!init)
19597 {
19598 if (complain & tf_error)
19599 error ("empty initializer in lambda init-capture");
19600 init = error_mark_node;
19601 }
19602 else if (TREE_CODE (init) == TREE_LIST)
19603 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19604 if (!type)
19605 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19606 TREE_TYPE (field) = type;
19607 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19608 }
19609 list = tree_cons (field, init, list);
19610 LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
19611 }
19612
19613 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19614 instantiation context. Instantiating a pack expansion containing a lambda
19615 might result in multiple lambdas all based on the same lambda in the
19616 template. */
19617
19618 tree
19619 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19620 {
19621 tree oldfn = lambda_function (t);
19622 in_decl = oldfn;
19623
19624 args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
19625 if (processing_template_decl && !in_template_context)
19626 {
19627 /* Defer templated substitution into a lambda-expr if we lost the
19628 necessary template context. This may happen for a lambda-expr
19629 used as a default template argument. */
19630 t = copy_node (t);
19631 LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
19632 LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
19633 return t;
19634 }
19635
19636 tree r = build_lambda_expr ();
19637
19638 LAMBDA_EXPR_LOCATION (r)
19639 = LAMBDA_EXPR_LOCATION (t);
19640 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19641 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19642 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19643 LAMBDA_EXPR_REGEN_INFO (r)
19644 = build_template_info (t, add_to_template_args (TI_ARGS (ti),
19645 preserve_args (args)));
19646 else
19647 LAMBDA_EXPR_REGEN_INFO (r)
19648 = build_template_info (t, preserve_args (args));
19649
19650 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19651 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19652
19653 vec<tree,va_gc>* field_packs = NULL;
19654 unsigned name_independent_cnt = 0;
19655 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19656 cap = TREE_CHAIN (cap))
19657 {
19658 tree ofield = TREE_PURPOSE (cap);
19659 tree init = TREE_VALUE (cap);
19660 if (PACK_EXPANSION_P (init))
19661 init = tsubst_pack_expansion (init, args, complain, in_decl);
19662 else
19663 init = tsubst_expr (init, args, complain, in_decl);
19664
19665 if (init == error_mark_node)
19666 return error_mark_node;
19667
19668 if (init && TREE_CODE (init) == TREE_LIST)
19669 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19670
19671 if (!processing_template_decl
19672 && init && TREE_CODE (init) != TREE_VEC
19673 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19674 {
19675 /* For a VLA, simply tsubsting the field type won't work, we need to
19676 go through add_capture again. XXX do we want to do this for all
19677 captures? */
19678 tree name = (get_identifier
19679 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19680 tree ftype = TREE_TYPE (ofield);
19681 bool by_ref = (TYPE_REF_P (ftype)
19682 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19683 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19684 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
19685 &name_independent_cnt);
19686 continue;
19687 }
19688
19689 if (PACK_EXPANSION_P (ofield))
19690 ofield = PACK_EXPANSION_PATTERN (ofield);
19691 tree field = tsubst_decl (ofield, args, complain);
19692
19693 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19694 {
19695 /* Remember these for when we've pushed local_specializations. */
19696 vec_safe_push (field_packs, ofield);
19697 vec_safe_push (field_packs, field);
19698 }
19699
19700 if (field == error_mark_node)
19701 return error_mark_node;
19702
19703 if (TREE_CODE (field) == TREE_VEC)
19704 {
19705 int len = TREE_VEC_LENGTH (field);
19706 gcc_assert (TREE_CODE (init) == TREE_VEC
19707 && TREE_VEC_LENGTH (init) == len);
19708 for (int i = 0; i < len; ++i)
19709 prepend_one_capture (TREE_VEC_ELT (field, i),
19710 TREE_VEC_ELT (init, i),
19711 LAMBDA_EXPR_CAPTURE_LIST (r),
19712 LAMBDA_CAPTURE_EXPLICIT_P (cap),
19713 complain);
19714 }
19715 else
19716 {
19717 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19718 LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
19719
19720 if (id_equal (DECL_NAME (field), "__this"))
19721 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19722 }
19723 }
19724
19725 tree type = begin_lambda_type (r);
19726 if (type == error_mark_node)
19727 {
19728 gcc_checking_assert (!(complain & tf_error) || seen_error ());
19729 return error_mark_node;
19730 }
19731
19732 if (LAMBDA_EXPR_EXTRA_SCOPE (t))
19733 record_lambda_scope (r);
19734 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
19735 /* If we're pushed into another scope (PR105652), fix it. */
19736 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
19737 = TYPE_CONTEXT (TREE_TYPE (t));
19738 record_lambda_scope_discriminator (r);
19739
19740 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19741 determine_visibility (TYPE_NAME (type));
19742
19743 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19744
19745 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19746 ? DECL_TI_TEMPLATE (oldfn)
19747 : NULL_TREE);
19748
19749 tree tparms = NULL_TREE;
19750 if (oldtmpl)
19751 tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
19752
19753 tree fntype = static_fn_type (oldfn);
19754
19755 tree saved_ctp = current_template_parms;
19756 if (oldtmpl)
19757 {
19758 ++processing_template_decl;
19759 current_template_parms = tparms;
19760 }
19761 fntype = tsubst (fntype, args, complain, in_decl);
19762 if (oldtmpl)
19763 {
19764 current_template_parms = saved_ctp;
19765 --processing_template_decl;
19766 }
19767
19768 if (fntype == error_mark_node)
19769 r = error_mark_node;
19770 else
19771 {
19772 /* The body of a lambda-expression is not a subexpression of the
19773 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19774 which would be skipped if cp_unevaluated_operand. */
19775 cp_evaluated ev;
19776
19777 /* Fix the type of 'this'.
19778 For static and xobj member functions we use this to transport the
19779 lambda's closure type. It appears that in the regular case the
19780 object parameter is still pulled off, and then re-added again anyway.
19781 So perhaps we could do something better here? */
19782 fntype = build_memfn_type (fntype, type,
19783 type_memfn_quals (fntype),
19784 type_memfn_rqual (fntype));
19785 tree inst = (oldtmpl
19786 ? tsubst_template_decl (oldtmpl, args, complain,
19787 fntype, tparms)
19788 : tsubst_function_decl (oldfn, args, complain, fntype));
19789 if (inst == error_mark_node)
19790 {
19791 r = error_mark_node;
19792 goto out;
19793 }
19794 finish_member_declaration (inst);
19795 record_lambda_scope_sig_discriminator (r, inst);
19796
19797 tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
19798
19799 /* Let finish_function set this. */
19800 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19801
19802 bool nested = cfun;
19803 if (nested)
19804 push_function_context ();
19805 else
19806 /* Still increment function_depth so that we don't GC in the
19807 middle of an expression. */
19808 ++function_depth;
19809
19810 local_specialization_stack s (lss_copy);
19811
19812 bool save_in_consteval_if_p = in_consteval_if_p;
19813 in_consteval_if_p = false;
19814
19815 tree body = start_lambda_function (fn, r);
19816
19817 /* Now record them for lookup_init_capture_pack. */
19818 int fplen = vec_safe_length (field_packs);
19819 for (int i = 0; i < fplen; )
19820 {
19821 tree pack = (*field_packs)[i++];
19822 tree inst = (*field_packs)[i++];
19823 register_local_specialization (inst, pack);
19824 }
19825 release_tree_vector (field_packs);
19826
19827 register_parameter_specializations (oldfn, fn);
19828
19829 if (oldtmpl)
19830 {
19831 /* We might not partially instantiate some parts of the function, so
19832 copy these flags from the original template. */
19833 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19834 current_function_returns_value = ol->returns_value;
19835 current_function_returns_null = ol->returns_null;
19836 current_function_returns_abnormally = ol->returns_abnormally;
19837 current_function_infinite_loop = ol->infinite_loop;
19838 }
19839
19840 /* [temp.deduct] A lambda-expression appearing in a function type or a
19841 template parameter is not considered part of the immediate context for
19842 the purposes of template argument deduction. */
19843 complain = tf_warning_or_error;
19844
19845 tree saved = DECL_SAVED_TREE (oldfn);
19846 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19847 /* We already have a body block from start_lambda_function, we don't
19848 need another to confuse NRV (91217). */
19849 saved = BIND_EXPR_BODY (saved);
19850
19851 tsubst_stmt (saved, args, complain, r);
19852
19853 finish_lambda_function (body);
19854
19855 in_consteval_if_p = save_in_consteval_if_p;
19856
19857 if (nested)
19858 pop_function_context ();
19859 else
19860 --function_depth;
19861
19862 /* The capture list was built up in reverse order; fix that now. */
19863 LAMBDA_EXPR_CAPTURE_LIST (r)
19864 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19865
19866 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19867
19868 maybe_add_lambda_conv_op (type);
19869 }
19870
19871 out:
19872 finish_struct (type, /*attr*/NULL_TREE);
19873
19874 insert_pending_capture_proxies ();
19875
19876 return r;
19877 }
19878
19879 /* Subroutine of maybe_fold_fn_template_args. */
19880
19881 static bool
19882 fold_targs_r (tree targs, tsubst_flags_t complain)
19883 {
19884 int len = TREE_VEC_LENGTH (targs);
19885 for (int i = 0; i < len; ++i)
19886 {
19887 tree &elt = TREE_VEC_ELT (targs, i);
19888 if (!elt || TYPE_P (elt)
19889 || TREE_CODE (elt) == TEMPLATE_DECL)
19890 continue;
19891 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
19892 {
19893 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
19894 return false;
19895 }
19896 else if (/* We can only safely preevaluate scalar prvalues. */
19897 SCALAR_TYPE_P (TREE_TYPE (elt))
19898 && !glvalue_p (elt)
19899 && !TREE_CONSTANT (elt))
19900 {
19901 elt = cxx_constant_value (elt, complain);
19902 if (elt == error_mark_node)
19903 return false;
19904 }
19905 }
19906
19907 return true;
19908 }
19909
19910 /* Try to do constant evaluation of any explicit template arguments in FN
19911 before overload resolution, to get any errors only once. Return true iff
19912 we didn't have any problems folding. */
19913
19914 static bool
19915 maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
19916 {
19917 if (processing_template_decl || fn == NULL_TREE)
19918 return true;
19919 if (fn == error_mark_node)
19920 return false;
19921 if (TREE_CODE (fn) == OFFSET_REF
19922 || TREE_CODE (fn) == COMPONENT_REF)
19923 fn = TREE_OPERAND (fn, 1);
19924 if (BASELINK_P (fn))
19925 fn = BASELINK_FUNCTIONS (fn);
19926 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
19927 return true;
19928 tree targs = TREE_OPERAND (fn, 1);
19929 if (targs == NULL_TREE)
19930 return true;
19931 if (targs == error_mark_node)
19932 return false;
19933 return fold_targs_r (targs, complain);
19934 }
19935
19936 /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */
19937
19938 static void
19939 tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
19940 tree in_decl, releasing_vec &call_args)
19941 {
19942 unsigned int nargs = call_expr_nargs (t);
19943 for (unsigned int i = 0; i < nargs; ++i)
19944 {
19945 tree arg = CALL_EXPR_ARG (t, i);
19946
19947 if (!PACK_EXPANSION_P (arg))
19948 vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
19949 else
19950 {
19951 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
19952 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19953 if (TREE_CODE (arg) == TREE_VEC)
19954 {
19955 unsigned int len, j;
19956
19957 len = TREE_VEC_LENGTH (arg);
19958 for (j = 0; j < len; ++j)
19959 {
19960 tree value = TREE_VEC_ELT (arg, j);
19961 if (value != NULL_TREE)
19962 value = convert_from_reference (value);
19963 vec_safe_push (call_args, value);
19964 }
19965 }
19966 else
19967 /* A partial substitution. Add one entry. */
19968 vec_safe_push (call_args, arg);
19969 }
19970 }
19971 }
19972
19973 /* Like tsubst but deals with expressions and performs semantic
19974 analysis. */
19975
19976 tree
19977 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19978 {
19979 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19980 #define RECUR(NODE) \
19981 tsubst_expr (NODE, args, complain, in_decl)
19982
19983 tree retval, op1;
19984 location_t save_loc;
19985
19986 if (t == NULL_TREE || t == error_mark_node)
19987 return t;
19988
19989 save_loc = input_location;
19990 if (location_t eloc = cp_expr_location (t))
19991 input_location = eloc;
19992
19993 /* N3276 decltype magic only applies to calls at the top level or on the
19994 right side of a comma. */
19995 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19996 complain &= ~tf_decltype;
19997
19998 /* This flag only applies to id-expressions at the top level, and
19999 controls resolution thereof. */
20000 tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
20001 complain &= ~tf_no_name_lookup;
20002
20003 if (!no_name_lookup_flag)
20004 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
20005 return d;
20006
20007 switch (TREE_CODE (t))
20008 {
20009 case USING_DECL:
20010 t = DECL_NAME (t);
20011 /* Fall through. */
20012 case IDENTIFIER_NODE:
20013 {
20014 tree decl;
20015 cp_id_kind idk;
20016 const char *error_msg;
20017
20018 if (IDENTIFIER_CONV_OP_P (t))
20019 {
20020 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20021 t = make_conv_op_name (new_type);
20022 }
20023
20024 if (no_name_lookup_flag)
20025 RETURN (t);
20026
20027 /* Look up the name. */
20028 decl = lookup_name (t);
20029
20030 /* By convention, expressions use ERROR_MARK_NODE to indicate
20031 failure, not NULL_TREE. */
20032 if (decl == NULL_TREE)
20033 decl = error_mark_node;
20034
20035 decl = finish_id_expression (t, decl, NULL_TREE,
20036 &idk,
20037 /*i_c_e_p=*/false,
20038 /*allow_i_c_e_p=*/true,
20039 /*non_i_c_e_p=*/nullptr,
20040 /*template_p=*/false,
20041 /*done=*/true,
20042 /*address_p=*/false,
20043 /*template_arg_p=*/false,
20044 &error_msg,
20045 input_location);
20046 if (error_msg)
20047 error (error_msg);
20048 if (identifier_p (decl))
20049 {
20050 if (complain & tf_error)
20051 unqualified_name_lookup_error (decl);
20052 decl = error_mark_node;
20053 }
20054 RETURN (decl);
20055 }
20056
20057 case TEMPLATE_ID_EXPR:
20058 {
20059 tree object;
20060 tree templ = TREE_OPERAND (t, 0);
20061 tree targs = TREE_OPERAND (t, 1);
20062
20063 if (no_name_lookup_flag)
20064 templ = tsubst_name (templ, args, complain, in_decl);
20065 else
20066 templ = tsubst_expr (templ, args, complain, in_decl);
20067
20068 if (targs)
20069 targs = tsubst_template_args (targs, args, complain, in_decl);
20070 if (targs == error_mark_node)
20071 RETURN (error_mark_node);
20072
20073 if (TREE_CODE (templ) == SCOPE_REF)
20074 {
20075 tree name = TREE_OPERAND (templ, 1);
20076 tree tid = lookup_template_function (name, targs);
20077 TREE_OPERAND (templ, 1) = tid;
20078 RETURN (templ);
20079 }
20080
20081 if (concept_definition_p (templ))
20082 {
20083 tree check = build_concept_check (templ, targs, complain);
20084 if (check == error_mark_node)
20085 RETURN (error_mark_node);
20086
20087 tree id = unpack_concept_check (check);
20088
20089 /* If we built a function concept check, return the underlying
20090 template-id. So we can evaluate it as a function call. */
20091 if (function_concept_p (TREE_OPERAND (id, 0)))
20092 RETURN (id);
20093
20094 RETURN (check);
20095 }
20096
20097 if (variable_template_p (templ))
20098 {
20099 if (no_name_lookup_flag)
20100 RETURN (lookup_template_variable (templ, targs, complain));
20101
20102 tree r = lookup_and_finish_template_variable (templ, targs,
20103 complain);
20104 r = convert_from_reference (r);
20105 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
20106 RETURN (r);
20107 }
20108
20109 if (TREE_CODE (templ) == COMPONENT_REF)
20110 {
20111 object = TREE_OPERAND (templ, 0);
20112 templ = TREE_OPERAND (templ, 1);
20113 }
20114 else
20115 object = NULL_TREE;
20116
20117 tree tid = lookup_template_function (templ, targs);
20118 protected_set_expr_location (tid, EXPR_LOCATION (t));
20119
20120 if (object)
20121 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
20122 object, tid, NULL_TREE));
20123 else if (no_name_lookup_flag)
20124 RETURN (tid);
20125 else if (identifier_p (templ))
20126 {
20127 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20128 name lookup found nothing when parsing the template name. */
20129 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
20130 RETURN (tid);
20131 }
20132 else
20133 RETURN (baselink_for_fns (tid));
20134 }
20135
20136 case INDIRECT_REF:
20137 {
20138 tree r = RECUR (TREE_OPERAND (t, 0));
20139
20140 if (REFERENCE_REF_P (t))
20141 {
20142 /* A type conversion to reference type will be enclosed in
20143 such an indirect ref, but the substitution of the cast
20144 will have also added such an indirect ref. */
20145 r = convert_from_reference (r);
20146 }
20147 else
20148 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
20149 templated_operator_saved_lookups (t),
20150 complain|decltype_flag);
20151
20152 if (REF_PARENTHESIZED_P (t))
20153 r = force_paren_expr (r);
20154
20155 RETURN (r);
20156 }
20157
20158 case MEM_REF:
20159 {
20160 tree op0 = RECUR (TREE_OPERAND (t, 0));
20161 tree op1 = RECUR (TREE_OPERAND (t, 0));
20162 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20163 RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
20164 }
20165
20166 case NOP_EXPR:
20167 {
20168 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20169 tree op0 = RECUR (TREE_OPERAND (t, 0));
20170 RETURN (build_nop (type, op0));
20171 }
20172
20173 case IMPLICIT_CONV_EXPR:
20174 {
20175 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20176 tree expr = RECUR (TREE_OPERAND (t, 0));
20177 if (dependent_type_p (type) || type_dependent_expression_p (expr))
20178 {
20179 retval = copy_node (t);
20180 TREE_TYPE (retval) = type;
20181 TREE_OPERAND (retval, 0) = expr;
20182 RETURN (retval);
20183 }
20184 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
20185 {
20186 tree r = convert_nontype_argument (type, expr, complain);
20187 if (r == NULL_TREE)
20188 r = error_mark_node;
20189 RETURN (r);
20190 }
20191 int flags = LOOKUP_IMPLICIT;
20192 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
20193 flags = LOOKUP_NORMAL;
20194 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
20195 flags |= LOOKUP_NO_NARROWING;
20196 RETURN (perform_implicit_conversion_flags (type, expr, complain,
20197 flags));
20198 }
20199
20200 case CONVERT_EXPR:
20201 {
20202 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20203 tree op0 = RECUR (TREE_OPERAND (t, 0));
20204 if (op0 == error_mark_node)
20205 RETURN (error_mark_node);
20206 RETURN (build1 (CONVERT_EXPR, type, op0));
20207 }
20208
20209 case CAST_EXPR:
20210 case REINTERPRET_CAST_EXPR:
20211 case CONST_CAST_EXPR:
20212 case DYNAMIC_CAST_EXPR:
20213 case STATIC_CAST_EXPR:
20214 {
20215 tree type;
20216 tree op, r = NULL_TREE;
20217
20218 tsubst_flags_t tcomplain = complain;
20219 if (TREE_CODE (t) == CAST_EXPR)
20220 tcomplain |= tf_tst_ok;
20221 type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
20222
20223 op = RECUR (TREE_OPERAND (t, 0));
20224
20225 warning_sentinel s(warn_useless_cast);
20226 warning_sentinel s2(warn_ignored_qualifiers);
20227 warning_sentinel s3(warn_int_in_bool_context);
20228 switch (TREE_CODE (t))
20229 {
20230 case CAST_EXPR:
20231 r = build_functional_cast (input_location, type, op, complain);
20232 break;
20233 case REINTERPRET_CAST_EXPR:
20234 r = build_reinterpret_cast (input_location, type, op, complain);
20235 break;
20236 case CONST_CAST_EXPR:
20237 r = build_const_cast (input_location, type, op, complain);
20238 break;
20239 case DYNAMIC_CAST_EXPR:
20240 r = build_dynamic_cast (input_location, type, op, complain);
20241 break;
20242 case STATIC_CAST_EXPR:
20243 r = build_static_cast (input_location, type, op, complain);
20244 if (IMPLICIT_RVALUE_P (t))
20245 set_implicit_rvalue_p (r);
20246 break;
20247 default:
20248 gcc_unreachable ();
20249 }
20250
20251 RETURN (r);
20252 }
20253
20254 case BIT_CAST_EXPR:
20255 {
20256 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20257 tree op0 = RECUR (TREE_OPERAND (t, 0));
20258 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
20259 }
20260
20261 case POSTDECREMENT_EXPR:
20262 case POSTINCREMENT_EXPR:
20263 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20264 args, complain, in_decl);
20265 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
20266 templated_operator_saved_lookups (t),
20267 complain|decltype_flag));
20268
20269 case BIT_NOT_EXPR:
20270 if (identifier_p (TREE_OPERAND (t, 0)))
20271 {
20272 gcc_checking_assert (no_name_lookup_flag);
20273 RETURN (t);
20274 }
20275 else if (TYPE_P (TREE_OPERAND (t, 0)))
20276 {
20277 gcc_checking_assert (no_name_lookup_flag);
20278 tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
20279 RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
20280 }
20281 /* Fall through. */
20282 case PREDECREMENT_EXPR:
20283 case PREINCREMENT_EXPR:
20284 case NEGATE_EXPR:
20285 case ABS_EXPR:
20286 case TRUTH_NOT_EXPR:
20287 case UNARY_PLUS_EXPR: /* Unary + */
20288 case REALPART_EXPR:
20289 case IMAGPART_EXPR:
20290 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
20291 RECUR (TREE_OPERAND (t, 0)),
20292 templated_operator_saved_lookups (t),
20293 complain|decltype_flag));
20294
20295 case EXCESS_PRECISION_EXPR:
20296 {
20297 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20298 tree op0 = RECUR (TREE_OPERAND (t, 0));
20299 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
20300 RETURN (op0);
20301 RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
20302 type, op0));
20303 }
20304
20305 case FIX_TRUNC_EXPR:
20306 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20307 gcc_unreachable ();
20308
20309 case ADDR_EXPR:
20310 op1 = TREE_OPERAND (t, 0);
20311 if (TREE_CODE (op1) == LABEL_DECL)
20312 RETURN (finish_label_address_expr (DECL_NAME (op1),
20313 EXPR_LOCATION (op1)));
20314 if (TREE_CODE (op1) == SCOPE_REF)
20315 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
20316 /*done=*/true, /*address_p=*/true);
20317 else
20318 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
20319 in_decl);
20320 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
20321 templated_operator_saved_lookups (t),
20322 complain|decltype_flag));
20323
20324 case PLUS_EXPR:
20325 case MINUS_EXPR:
20326 case MULT_EXPR:
20327 case TRUNC_DIV_EXPR:
20328 case CEIL_DIV_EXPR:
20329 case FLOOR_DIV_EXPR:
20330 case ROUND_DIV_EXPR:
20331 case EXACT_DIV_EXPR:
20332 case BIT_AND_EXPR:
20333 case BIT_IOR_EXPR:
20334 case BIT_XOR_EXPR:
20335 case TRUNC_MOD_EXPR:
20336 case FLOOR_MOD_EXPR:
20337 case TRUTH_ANDIF_EXPR:
20338 case TRUTH_ORIF_EXPR:
20339 case TRUTH_AND_EXPR:
20340 case TRUTH_OR_EXPR:
20341 case RSHIFT_EXPR:
20342 case LSHIFT_EXPR:
20343 case EQ_EXPR:
20344 case NE_EXPR:
20345 case MAX_EXPR:
20346 case MIN_EXPR:
20347 case LE_EXPR:
20348 case GE_EXPR:
20349 case LT_EXPR:
20350 case GT_EXPR:
20351 case SPACESHIP_EXPR:
20352 case MEMBER_REF:
20353 case DOTSTAR_EXPR:
20354 {
20355 /* If either OP0 or OP1 was value- or type-dependent, suppress
20356 warnings that depend on the range of the types involved. */
20357 tree op0 = TREE_OPERAND (t, 0);
20358 tree op1 = TREE_OPERAND (t, 1);
20359 const bool was_dep = (dependent_operand_p (op0)
20360 || dependent_operand_p (op1));
20361 op0 = RECUR (op0);
20362 op1 = RECUR (op1);
20363
20364 warning_sentinel s1(warn_type_limits, was_dep);
20365 warning_sentinel s2(warn_div_by_zero, was_dep);
20366 warning_sentinel s3(warn_logical_op, was_dep);
20367 warning_sentinel s4(warn_tautological_compare, was_dep);
20368 warning_sentinel s5(warn_address, was_dep);
20369
20370 tree r = build_x_binary_op
20371 (input_location, TREE_CODE (t),
20372 op0,
20373 (warning_suppressed_p (TREE_OPERAND (t, 0))
20374 ? ERROR_MARK
20375 : TREE_CODE (TREE_OPERAND (t, 0))),
20376 op1,
20377 (warning_suppressed_p (TREE_OPERAND (t, 1))
20378 ? ERROR_MARK
20379 : TREE_CODE (TREE_OPERAND (t, 1))),
20380 templated_operator_saved_lookups (t),
20381 /*overload=*/NULL,
20382 complain|decltype_flag);
20383 if (EXPR_P (r))
20384 copy_warning (r, t);
20385
20386 RETURN (r);
20387 }
20388
20389 case POINTER_PLUS_EXPR:
20390 {
20391 tree op0 = RECUR (TREE_OPERAND (t, 0));
20392 if (op0 == error_mark_node)
20393 RETURN (error_mark_node);
20394 tree op1 = RECUR (TREE_OPERAND (t, 1));
20395 if (op1 == error_mark_node)
20396 RETURN (error_mark_node);
20397 RETURN (fold_build_pointer_plus (op0, op1));
20398 }
20399
20400 case SCOPE_REF:
20401 if (no_name_lookup_flag)
20402 {
20403 tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
20404 tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
20405 RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
20406 QUALIFIED_NAME_IS_TEMPLATE (t)));
20407 }
20408 else
20409 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
20410 /*address_p=*/false));
20411
20412 case BASELINK:
20413 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
20414 args, complain, in_decl));
20415
20416 case ARRAY_REF:
20417 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20418 args, complain, in_decl);
20419 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
20420 && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
20421 == ovl_op_identifier (ARRAY_REF)))
20422 {
20423 tree c = TREE_OPERAND (t, 1);
20424 releasing_vec index_exp_list;
20425 tsubst_call_args (c, args, complain, in_decl, index_exp_list);
20426
20427 tree r;
20428 if (vec_safe_length (index_exp_list) == 1
20429 && !PACK_EXPANSION_P (index_exp_list[0]))
20430 r = grok_array_decl (EXPR_LOCATION (t), op1,
20431 index_exp_list[0], NULL,
20432 complain | decltype_flag);
20433 else
20434 r = grok_array_decl (EXPR_LOCATION (t), op1,
20435 NULL_TREE, &index_exp_list,
20436 complain | decltype_flag);
20437 RETURN (r);
20438 }
20439 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
20440 RECUR (TREE_OPERAND (t, 1)),
20441 complain|decltype_flag));
20442
20443 case OMP_ARRAY_SECTION:
20444 {
20445 tree op0 = RECUR (TREE_OPERAND (t, 0));
20446 tree op1 = NULL_TREE, op2 = NULL_TREE;
20447 if (op0 == error_mark_node)
20448 RETURN (error_mark_node);
20449 if (TREE_OPERAND (t, 1))
20450 {
20451 op1 = RECUR (TREE_OPERAND (t, 1));
20452 if (op1 == error_mark_node)
20453 RETURN (error_mark_node);
20454 }
20455 if (TREE_OPERAND (t, 2))
20456 {
20457 op2 = RECUR (TREE_OPERAND (t, 2));
20458 if (op2 == error_mark_node)
20459 RETURN (error_mark_node);
20460 }
20461 RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
20462 }
20463
20464 case SIZEOF_EXPR:
20465 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
20466 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
20467 {
20468 tree expanded, op = TREE_OPERAND (t, 0);
20469 int len = 0;
20470
20471 if (SIZEOF_EXPR_TYPE_P (t))
20472 op = TREE_TYPE (op);
20473
20474 ++cp_unevaluated_operand;
20475 ++c_inhibit_evaluation_warnings;
20476 /* We only want to compute the number of arguments. */
20477 if (PACK_EXPANSION_P (op))
20478 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
20479 else
20480 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
20481 args, complain, in_decl);
20482 --cp_unevaluated_operand;
20483 --c_inhibit_evaluation_warnings;
20484
20485 if (TREE_CODE (expanded) == TREE_VEC)
20486 {
20487 len = TREE_VEC_LENGTH (expanded);
20488 /* Set TREE_USED for the benefit of -Wunused. */
20489 for (int i = 0; i < len; i++)
20490 if (DECL_P (TREE_VEC_ELT (expanded, i)))
20491 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
20492 }
20493
20494 if (expanded == error_mark_node)
20495 RETURN (error_mark_node);
20496 else if (PACK_EXPANSION_P (expanded)
20497 || (TREE_CODE (expanded) == TREE_VEC
20498 && pack_expansion_args_count (expanded)))
20499
20500 {
20501 if (PACK_EXPANSION_P (expanded))
20502 /* OK. */;
20503 else
20504 expanded = make_argument_pack (expanded);
20505
20506 if (TYPE_P (expanded))
20507 RETURN (cxx_sizeof_or_alignof_type (input_location,
20508 expanded, SIZEOF_EXPR,
20509 false,
20510 complain & tf_error));
20511 else
20512 RETURN (cxx_sizeof_or_alignof_expr (input_location,
20513 expanded, SIZEOF_EXPR,
20514 false,
20515 complain & tf_error));
20516 }
20517 else
20518 RETURN (build_int_cst (size_type_node, len));
20519 }
20520 /* Fall through */
20521
20522 case ALIGNOF_EXPR:
20523 {
20524 tree r;
20525
20526 op1 = TREE_OPERAND (t, 0);
20527 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
20528 op1 = TREE_TYPE (op1);
20529 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
20530 && ALIGNOF_EXPR_STD_P (t));
20531 if (!args)
20532 {
20533 /* When there are no ARGS, we are trying to evaluate a
20534 non-dependent expression from the parser. Trying to do
20535 the substitutions may not work. */
20536 if (!TYPE_P (op1))
20537 op1 = TREE_TYPE (op1);
20538 }
20539 else
20540 {
20541 ++cp_unevaluated_operand;
20542 ++c_inhibit_evaluation_warnings;
20543 if (TYPE_P (op1))
20544 op1 = tsubst (op1, args, complain, in_decl);
20545 else
20546 op1 = tsubst_expr (op1, args, complain, in_decl);
20547 --cp_unevaluated_operand;
20548 --c_inhibit_evaluation_warnings;
20549 }
20550 if (TYPE_P (op1))
20551 r = cxx_sizeof_or_alignof_type (input_location,
20552 op1, TREE_CODE (t), std_alignof,
20553 complain & tf_error);
20554 else
20555 r = cxx_sizeof_or_alignof_expr (input_location,
20556 op1, TREE_CODE (t), std_alignof,
20557 complain & tf_error);
20558 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
20559 {
20560 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
20561 {
20562 if (!processing_template_decl && TYPE_P (op1))
20563 {
20564 r = build_min (SIZEOF_EXPR, size_type_node,
20565 build1 (NOP_EXPR, op1, error_mark_node));
20566 SIZEOF_EXPR_TYPE_P (r) = 1;
20567 }
20568 else
20569 r = build_min (SIZEOF_EXPR, size_type_node, op1);
20570 TREE_SIDE_EFFECTS (r) = 0;
20571 TREE_READONLY (r) = 1;
20572 }
20573 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
20574 }
20575 RETURN (r);
20576 }
20577
20578 case AT_ENCODE_EXPR:
20579 {
20580 op1 = TREE_OPERAND (t, 0);
20581 ++cp_unevaluated_operand;
20582 ++c_inhibit_evaluation_warnings;
20583 op1 = tsubst (op1, args, complain, in_decl);
20584 --cp_unevaluated_operand;
20585 --c_inhibit_evaluation_warnings;
20586 RETURN (objc_build_encode_expr (op1));
20587 }
20588
20589 case NOEXCEPT_EXPR:
20590 op1 = TREE_OPERAND (t, 0);
20591 ++cp_unevaluated_operand;
20592 ++c_inhibit_evaluation_warnings;
20593 ++cp_noexcept_operand;
20594 op1 = tsubst_expr (op1, args, complain, in_decl);
20595 --cp_unevaluated_operand;
20596 --c_inhibit_evaluation_warnings;
20597 --cp_noexcept_operand;
20598 RETURN (finish_noexcept_expr (op1, complain));
20599
20600 case MODOP_EXPR:
20601 {
20602 warning_sentinel s(warn_div_by_zero);
20603 tree lhs = RECUR (TREE_OPERAND (t, 0));
20604 tree rhs = RECUR (TREE_OPERAND (t, 2));
20605
20606 tree r = build_x_modify_expr
20607 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
20608 templated_operator_saved_lookups (t),
20609 complain|decltype_flag);
20610 /* TREE_NO_WARNING must be set if either the expression was
20611 parenthesized or it uses an operator such as >>= rather
20612 than plain assignment. In the former case, it was already
20613 set and must be copied. In the latter case,
20614 build_x_modify_expr sets it and it must not be reset
20615 here. */
20616 if (warning_suppressed_p (t, OPT_Wparentheses))
20617 suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
20618
20619 RETURN (r);
20620 }
20621
20622 case ARROW_EXPR:
20623 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20624 args, complain, in_decl);
20625 /* Remember that there was a reference to this entity. */
20626 if (DECL_P (op1)
20627 && !mark_used (op1, complain) && !(complain & tf_error))
20628 RETURN (error_mark_node);
20629 RETURN (build_x_arrow (input_location, op1, complain));
20630
20631 case NEW_EXPR:
20632 {
20633 tree placement = RECUR (TREE_OPERAND (t, 0));
20634 tree init = RECUR (TREE_OPERAND (t, 3));
20635 vec<tree, va_gc> *placement_vec;
20636 vec<tree, va_gc> *init_vec;
20637 tree ret;
20638 location_t loc = EXPR_LOCATION (t);
20639
20640 if (placement == NULL_TREE)
20641 placement_vec = NULL;
20642 else if (placement == error_mark_node)
20643 RETURN (error_mark_node);
20644 else
20645 {
20646 placement_vec = make_tree_vector ();
20647 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20648 vec_safe_push (placement_vec, TREE_VALUE (placement));
20649 }
20650
20651 /* If there was an initializer in the original tree, but it
20652 instantiated to an empty list, then we should pass a
20653 non-NULL empty vector to tell build_new that it was an
20654 empty initializer() rather than no initializer. This can
20655 only happen when the initializer is a pack expansion whose
20656 parameter packs are of length zero. */
20657 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20658 init_vec = NULL;
20659 else if (init == error_mark_node)
20660 RETURN (error_mark_node);
20661 else
20662 {
20663 init_vec = make_tree_vector ();
20664 if (init == void_node)
20665 gcc_assert (init_vec != NULL);
20666 else
20667 {
20668 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20669 vec_safe_push (init_vec, TREE_VALUE (init));
20670 }
20671 }
20672
20673 /* Avoid passing an enclosing decl to valid_array_size_p. */
20674 in_decl = NULL_TREE;
20675
20676 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20677 tree op2 = RECUR (TREE_OPERAND (t, 2));
20678 ret = build_new (loc, &placement_vec, op1, op2,
20679 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20680 complain);
20681
20682 if (placement_vec != NULL)
20683 release_tree_vector (placement_vec);
20684 if (init_vec != NULL)
20685 release_tree_vector (init_vec);
20686
20687 RETURN (ret);
20688 }
20689
20690 case DELETE_EXPR:
20691 {
20692 tree op0 = RECUR (TREE_OPERAND (t, 0));
20693 tree op1 = RECUR (TREE_OPERAND (t, 1));
20694 RETURN (delete_sanity (input_location, op0, op1,
20695 DELETE_EXPR_USE_VEC (t),
20696 DELETE_EXPR_USE_GLOBAL (t),
20697 complain));
20698 }
20699
20700 case COMPOUND_EXPR:
20701 {
20702 tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
20703 complain & ~tf_decltype, in_decl);
20704 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20705 op0,
20706 RECUR (TREE_OPERAND (t, 1)),
20707 templated_operator_saved_lookups (t),
20708 complain|decltype_flag));
20709 }
20710
20711 case CALL_EXPR:
20712 {
20713 tree function;
20714 unsigned int nargs;
20715 bool qualified_p;
20716 bool koenig_p;
20717 tree ret;
20718
20719 function = CALL_EXPR_FN (t);
20720 /* Internal function with no arguments. */
20721 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20722 RETURN (t);
20723
20724 /* When we parsed the expression, we determined whether or
20725 not Koenig lookup should be performed. */
20726 koenig_p = KOENIG_LOOKUP_P (t);
20727 if (function == NULL_TREE)
20728 {
20729 koenig_p = false;
20730 qualified_p = false;
20731 }
20732 else if (TREE_CODE (function) == SCOPE_REF)
20733 {
20734 qualified_p = true;
20735 function = tsubst_qualified_id (function, args, complain, in_decl,
20736 /*done=*/false,
20737 /*address_p=*/false);
20738 }
20739 else if (CALL_EXPR_STATIC_CHAIN (t)
20740 && TREE_CODE (function) == FUNCTION_DECL
20741 && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
20742 {
20743 tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
20744 in_decl);
20745 if (dependent_type_p (type))
20746 {
20747 ret = build_vl_exp (CALL_EXPR, 4);
20748 CALL_EXPR_FN (ret) = function;
20749 CALL_EXPR_STATIC_CHAIN (ret) = type;
20750 CALL_EXPR_ARG (ret, 0)
20751 = build_min (SIZEOF_EXPR, size_type_node, type);
20752 TREE_TYPE (ret) = integer_type_node;
20753 }
20754 else
20755 ret = build_int_cst (integer_type_node, type_to_class (type));
20756 RETURN (ret);
20757 }
20758 else if (koenig_p
20759 && (identifier_p (function)
20760 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20761 && identifier_p (TREE_OPERAND (function, 0)))))
20762 {
20763 /* Do nothing; calling tsubst_expr on an identifier
20764 would incorrectly perform unqualified lookup again.
20765
20766 Note that we can also have an IDENTIFIER_NODE if the earlier
20767 unqualified lookup found a dependent local extern declaration
20768 (as per finish_call_expr); in that case koenig_p will be false
20769 and we do want to do the lookup again to find the substituted
20770 declaration. */
20771 qualified_p = false;
20772
20773 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20774 function = tsubst_name (function, args, complain, in_decl);
20775 }
20776 else
20777 {
20778 if (TREE_CODE (function) == COMPONENT_REF)
20779 {
20780 tree op = TREE_OPERAND (function, 1);
20781
20782 qualified_p = (TREE_CODE (op) == SCOPE_REF
20783 || (BASELINK_P (op)
20784 && BASELINK_QUALIFIED_P (op)));
20785 }
20786 else
20787 qualified_p = false;
20788
20789 if (TREE_CODE (function) == ADDR_EXPR
20790 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20791 /* Avoid error about taking the address of a constructor. */
20792 function = TREE_OPERAND (function, 0);
20793
20794 tsubst_flags_t subcomplain = complain;
20795 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20796 /* When KOENIG_P, we don't want to mark_used the callee before
20797 augmenting the overload set via ADL, so during this initial
20798 substitution we disable mark_used by setting tf_conv (68942). */
20799 subcomplain |= tf_conv;
20800 function = tsubst_expr (function, args, subcomplain, in_decl);
20801
20802 if (BASELINK_P (function))
20803 qualified_p = true;
20804 }
20805
20806 nargs = call_expr_nargs (t);
20807 releasing_vec call_args;
20808 tsubst_call_args (t, args, complain, in_decl, call_args);
20809
20810 /* Stripped-down processing for a call in a thunk. Specifically, in
20811 the thunk template for a generic lambda. */
20812 if (call_from_lambda_thunk_p (t))
20813 {
20814 /* Now that we've expanded any packs, the number of call args
20815 might be different. */
20816 unsigned int cargs = call_args->length ();
20817 tree thisarg = NULL_TREE;
20818 if (TREE_CODE (function) == COMPONENT_REF)
20819 {
20820 thisarg = TREE_OPERAND (function, 0);
20821 if (TREE_CODE (thisarg) == INDIRECT_REF)
20822 thisarg = TREE_OPERAND (thisarg, 0);
20823 function = TREE_OPERAND (function, 1);
20824 if (TREE_CODE (function) == BASELINK)
20825 function = BASELINK_FUNCTIONS (function);
20826 }
20827 /* We aren't going to do normal overload resolution, so force the
20828 template-id to resolve. */
20829 function = resolve_nondeduced_context (function, complain);
20830 for (unsigned i = 0; i < cargs; ++i)
20831 {
20832 /* In a thunk, pass through args directly, without any
20833 conversions. */
20834 tree arg = (*call_args)[i];
20835 while (TREE_CODE (arg) != PARM_DECL)
20836 arg = TREE_OPERAND (arg, 0);
20837 (*call_args)[i] = arg;
20838 }
20839 if (thisarg)
20840 {
20841 /* If there are no other args, just push 'this'. */
20842 if (cargs == 0)
20843 vec_safe_push (call_args, thisarg);
20844 else
20845 {
20846 /* Otherwise, shift the other args over to make room. */
20847 tree last = (*call_args)[cargs - 1];
20848 vec_safe_push (call_args, last);
20849 for (int i = cargs - 1; i > 0; --i)
20850 (*call_args)[i] = (*call_args)[i - 1];
20851 (*call_args)[0] = thisarg;
20852 }
20853 }
20854 ret = build_call_a (function, call_args->length (),
20855 call_args->address ());
20856 /* The thunk location is not interesting. */
20857 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20858 CALL_FROM_THUNK_P (ret) = true;
20859 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20860 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20861
20862 RETURN (ret);
20863 }
20864
20865 /* We do not perform argument-dependent lookup if normal
20866 lookup finds a non-function, in accordance with the
20867 resolution of DR 218. */
20868 if (koenig_p
20869 && ((is_overloaded_fn (function)
20870 /* If lookup found a member function, the Koenig lookup is
20871 not appropriate, even if an unqualified-name was used
20872 to denote the function. */
20873 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20874 || identifier_p (function)
20875 /* C++20 P0846: Lookup found nothing. */
20876 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20877 && identifier_p (TREE_OPERAND (function, 0))))
20878 /* Only do this when substitution turns a dependent call
20879 into a non-dependent call. */
20880 && type_dependent_expression_p_push (t)
20881 && !any_type_dependent_arguments_p (call_args))
20882 function = perform_koenig_lookup (function, call_args, tf_none);
20883
20884 if (function != NULL_TREE
20885 && (identifier_p (function)
20886 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20887 && identifier_p (TREE_OPERAND (function, 0))
20888 && !any_dependent_template_arguments_p (TREE_OPERAND
20889 (function, 1))))
20890 && !any_type_dependent_arguments_p (call_args))
20891 {
20892 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
20893 if (template_id_p)
20894 function = TREE_OPERAND (function, 0);
20895 if (koenig_p && (complain & tf_warning_or_error))
20896 {
20897 /* For backwards compatibility and good diagnostics, try
20898 the unqualified lookup again if we aren't in SFINAE
20899 context. */
20900 tree unq = tsubst_expr (function, args, complain, in_decl);
20901 if (unq == error_mark_node)
20902 RETURN (error_mark_node);
20903
20904 if (unq != function)
20905 {
20906 char const *const msg
20907 = G_("%qD was not declared in this scope, "
20908 "and no declarations were found by "
20909 "argument-dependent lookup at the point "
20910 "of instantiation");
20911
20912 bool in_lambda = (current_class_type
20913 && LAMBDA_TYPE_P (current_class_type));
20914 /* In a lambda fn, we have to be careful to not
20915 introduce new this captures. Legacy code can't
20916 be using lambdas anyway, so it's ok to be
20917 stricter. Be strict with C++20 template-id ADL too.
20918 And be strict if we're already failing anyway. */
20919 bool strict = in_lambda || template_id_p || seen_error();
20920 bool diag = true;
20921 if (strict)
20922 error_at (cp_expr_loc_or_input_loc (t),
20923 msg, function);
20924 else
20925 diag = permerror (cp_expr_loc_or_input_loc (t),
20926 msg, function);
20927 if (diag)
20928 {
20929 tree fn = unq;
20930
20931 if (INDIRECT_REF_P (fn))
20932 fn = TREE_OPERAND (fn, 0);
20933 if (is_overloaded_fn (fn))
20934 fn = get_first_fn (fn);
20935
20936 if (!DECL_P (fn))
20937 /* Can't say anything more. */;
20938 else if (DECL_CLASS_SCOPE_P (fn))
20939 {
20940 location_t loc = cp_expr_loc_or_input_loc (t);
20941 inform (loc,
20942 "declarations in dependent base %qT are "
20943 "not found by unqualified lookup",
20944 DECL_CLASS_CONTEXT (fn));
20945 if (current_class_ptr)
20946 inform (loc,
20947 "use %<this->%D%> instead", function);
20948 else
20949 inform (loc,
20950 "use %<%T::%D%> instead",
20951 current_class_name, function);
20952 }
20953 else
20954 inform (DECL_SOURCE_LOCATION (fn),
20955 "%qD declared here, later in the "
20956 "translation unit", fn);
20957 if (strict)
20958 RETURN (error_mark_node);
20959 }
20960
20961 function = unq;
20962 }
20963 }
20964 if (identifier_p (function))
20965 {
20966 if (complain & tf_error)
20967 unqualified_name_lookup_error (function);
20968 RETURN (error_mark_node);
20969 }
20970 }
20971
20972 /* Remember that there was a reference to this entity. */
20973 if (function != NULL_TREE
20974 && DECL_P (function)
20975 && !mark_used (function, complain) && !(complain & tf_error))
20976 RETURN (error_mark_node);
20977
20978 if (!maybe_fold_fn_template_args (function, complain))
20979 return error_mark_node;
20980
20981 /* Put back tf_decltype for the actual call. */
20982 complain |= decltype_flag;
20983
20984 if (function == NULL_TREE)
20985 switch (CALL_EXPR_IFN (t))
20986 {
20987 case IFN_LAUNDER:
20988 gcc_assert (nargs == 1);
20989 if (vec_safe_length (call_args) != 1)
20990 {
20991 error_at (cp_expr_loc_or_input_loc (t),
20992 "wrong number of arguments to "
20993 "%<__builtin_launder%>");
20994 ret = error_mark_node;
20995 }
20996 else
20997 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20998 (*call_args)[0], complain);
20999 break;
21000
21001 case IFN_VEC_CONVERT:
21002 gcc_assert (nargs == 1);
21003 if (vec_safe_length (call_args) != 1)
21004 {
21005 error_at (cp_expr_loc_or_input_loc (t),
21006 "wrong number of arguments to "
21007 "%<__builtin_convertvector%>");
21008 ret = error_mark_node;
21009 break;
21010 }
21011 ret = cp_build_vec_convert ((*call_args)[0], input_location,
21012 tsubst (TREE_TYPE (t), args,
21013 complain, in_decl),
21014 complain);
21015 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
21016 RETURN (ret);
21017 break;
21018
21019 case IFN_SHUFFLEVECTOR:
21020 {
21021 ret = build_x_shufflevector (input_location, call_args,
21022 complain);
21023 if (ret != error_mark_node)
21024 RETURN (ret);
21025 break;
21026 }
21027
21028 case IFN_ASSUME:
21029 gcc_assert (nargs == 1);
21030 if (vec_safe_length (call_args) != 1)
21031 {
21032 error_at (cp_expr_loc_or_input_loc (t),
21033 "wrong number of arguments to "
21034 "%<assume%> attribute");
21035 ret = error_mark_node;
21036 }
21037 else
21038 {
21039 tree &arg = (*call_args)[0];
21040 if (!type_dependent_expression_p (arg))
21041 arg = contextual_conv_bool (arg, tf_warning_or_error);
21042 if (error_operand_p (arg))
21043 {
21044 ret = error_mark_node;
21045 break;
21046 }
21047 ret = build_assume_call (EXPR_LOCATION (t), arg);
21048 RETURN (ret);
21049 }
21050 break;
21051
21052 default:
21053 /* Unsupported internal function with arguments. */
21054 gcc_unreachable ();
21055 }
21056 else if (TREE_CODE (function) == OFFSET_REF
21057 || TREE_CODE (function) == DOTSTAR_EXPR
21058 || TREE_CODE (function) == MEMBER_REF)
21059 ret = build_offset_ref_call_from_tree (function, &call_args,
21060 complain);
21061 else if (concept_check_p (function))
21062 {
21063 /* FUNCTION is a template-id referring to a concept definition. */
21064 tree id = unpack_concept_check (function);
21065 tree tmpl = TREE_OPERAND (id, 0);
21066 tree args = TREE_OPERAND (id, 1);
21067
21068 /* Calls to standard and variable concepts should have been
21069 previously diagnosed. */
21070 gcc_assert (function_concept_p (tmpl));
21071
21072 /* Ensure the result is wrapped as a call expression. */
21073 ret = build_concept_check (tmpl, args, tf_warning_or_error);
21074 }
21075 else
21076 ret = finish_call_expr (function, &call_args,
21077 /*disallow_virtual=*/qualified_p,
21078 koenig_p,
21079 complain);
21080
21081 if (ret != error_mark_node)
21082 {
21083 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
21084 bool ord = CALL_EXPR_ORDERED_ARGS (t);
21085 bool rev = CALL_EXPR_REVERSE_ARGS (t);
21086 if (op || ord || rev)
21087 if (tree call = extract_call_expr (ret))
21088 {
21089 CALL_EXPR_OPERATOR_SYNTAX (call) = op;
21090 CALL_EXPR_ORDERED_ARGS (call) = ord;
21091 CALL_EXPR_REVERSE_ARGS (call) = rev;
21092 }
21093 if (warning_suppressed_p (t, OPT_Wpessimizing_move))
21094 /* This also suppresses -Wredundant-move. */
21095 suppress_warning (ret, OPT_Wpessimizing_move);
21096 }
21097
21098 RETURN (ret);
21099 }
21100
21101 case COND_EXPR:
21102 {
21103 tree cond = RECUR (TREE_OPERAND (t, 0));
21104 cond = mark_rvalue_use (cond);
21105 tree folded_cond = fold_non_dependent_expr (cond, complain);
21106 tree exp1, exp2;
21107
21108 if (TREE_CODE (folded_cond) == INTEGER_CST)
21109 {
21110 if (integer_zerop (folded_cond))
21111 {
21112 ++c_inhibit_evaluation_warnings;
21113 exp1 = RECUR (TREE_OPERAND (t, 1));
21114 --c_inhibit_evaluation_warnings;
21115 exp2 = RECUR (TREE_OPERAND (t, 2));
21116 }
21117 else
21118 {
21119 exp1 = RECUR (TREE_OPERAND (t, 1));
21120 ++c_inhibit_evaluation_warnings;
21121 exp2 = RECUR (TREE_OPERAND (t, 2));
21122 --c_inhibit_evaluation_warnings;
21123 }
21124 cond = folded_cond;
21125 }
21126 else
21127 {
21128 exp1 = RECUR (TREE_OPERAND (t, 1));
21129 exp2 = RECUR (TREE_OPERAND (t, 2));
21130 }
21131
21132 warning_sentinel s(warn_duplicated_branches);
21133 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
21134 cond, exp1, exp2, complain));
21135 }
21136
21137 case PSEUDO_DTOR_EXPR:
21138 {
21139 tree op0 = RECUR (TREE_OPERAND (t, 0));
21140 tree op1 = RECUR (TREE_OPERAND (t, 1));
21141 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
21142 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
21143 input_location));
21144 }
21145
21146 case TREE_LIST:
21147 RETURN (tsubst_tree_list (t, args, complain, in_decl));
21148
21149 case COMPONENT_REF:
21150 {
21151 tree object;
21152 tree object_type;
21153 tree member;
21154 tree r;
21155
21156 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21157 args, complain, in_decl);
21158 /* Remember that there was a reference to this entity. */
21159 if (DECL_P (object)
21160 && !mark_used (object, complain) && !(complain & tf_error))
21161 RETURN (error_mark_node);
21162 object_type = TREE_TYPE (object);
21163
21164 member = TREE_OPERAND (t, 1);
21165 if (BASELINK_P (member))
21166 member = tsubst_baselink (member,
21167 non_reference (TREE_TYPE (object)),
21168 args, complain, in_decl);
21169 else
21170 member = tsubst_name (member, args, complain, in_decl);
21171 if (member == error_mark_node)
21172 RETURN (error_mark_node);
21173
21174 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
21175 && TREE_CODE (member) == FIELD_DECL)
21176 {
21177 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
21178 RETURN (r);
21179 }
21180 else if (TREE_CODE (member) == FIELD_DECL)
21181 {
21182 r = finish_non_static_data_member (member, object, NULL_TREE,
21183 complain);
21184 if (TREE_CODE (r) == COMPONENT_REF)
21185 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
21186 RETURN (r);
21187 }
21188 else if (type_dependent_expression_p (object))
21189 /* We can't do much here. */;
21190 else if (!CLASS_TYPE_P (object_type))
21191 {
21192 if (scalarish_type_p (object_type))
21193 {
21194 tree s = NULL_TREE;
21195 tree dtor = member;
21196
21197 if (TREE_CODE (dtor) == SCOPE_REF)
21198 {
21199 s = TREE_OPERAND (dtor, 0);
21200 dtor = TREE_OPERAND (dtor, 1);
21201 }
21202 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
21203 {
21204 dtor = TREE_OPERAND (dtor, 0);
21205 if (TYPE_P (dtor))
21206 RETURN (finish_pseudo_destructor_expr
21207 (object, s, dtor, input_location));
21208 }
21209 }
21210 }
21211 else if (TREE_CODE (member) == SCOPE_REF
21212 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
21213 {
21214 /* Lookup the template functions now that we know what the
21215 scope is. */
21216 tree scope = TREE_OPERAND (member, 0);
21217 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
21218 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
21219 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
21220 /*complain=*/false);
21221 if (BASELINK_P (member))
21222 {
21223 BASELINK_FUNCTIONS (member)
21224 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
21225 args);
21226 member = (adjust_result_of_qualified_name_lookup
21227 (member, BINFO_TYPE (BASELINK_BINFO (member)),
21228 object_type));
21229 }
21230 else
21231 {
21232 qualified_name_lookup_error (scope, tmpl, member,
21233 input_location);
21234 RETURN (error_mark_node);
21235 }
21236 }
21237 else if (TREE_CODE (member) == SCOPE_REF
21238 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
21239 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
21240 {
21241 if (complain & tf_error)
21242 {
21243 if (TYPE_P (TREE_OPERAND (member, 0)))
21244 error ("%qT is not a class or namespace",
21245 TREE_OPERAND (member, 0));
21246 else
21247 error ("%qD is not a class or namespace",
21248 TREE_OPERAND (member, 0));
21249 }
21250 RETURN (error_mark_node);
21251 }
21252
21253 r = finish_class_member_access_expr (object, member,
21254 /*template_p=*/false,
21255 complain);
21256 if (REF_PARENTHESIZED_P (t))
21257 r = force_paren_expr (r);
21258 RETURN (r);
21259 }
21260
21261 case THROW_EXPR:
21262 RETURN (build_throw
21263 (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
21264
21265 case CONSTRUCTOR:
21266 {
21267 vec<constructor_elt, va_gc> *n;
21268 constructor_elt *ce;
21269 unsigned HOST_WIDE_INT idx;
21270 bool process_index_p;
21271 int newlen;
21272 bool need_copy_p = false;
21273 tree r;
21274
21275 tsubst_flags_t tcomplain = complain;
21276 if (COMPOUND_LITERAL_P (t))
21277 tcomplain |= tf_tst_ok;
21278 tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
21279 if (type == error_mark_node)
21280 RETURN (error_mark_node);
21281
21282 /* We do not want to process the index of aggregate
21283 initializers as they are identifier nodes which will be
21284 looked up by digest_init. */
21285 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
21286
21287 if (null_member_pointer_value_p (t))
21288 {
21289 gcc_assert (same_type_p (type, TREE_TYPE (t)));
21290 RETURN (t);
21291 }
21292
21293 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
21294 newlen = vec_safe_length (n);
21295 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
21296 {
21297 if (ce->index && process_index_p
21298 /* An identifier index is looked up in the type
21299 being initialized, not the current scope. */
21300 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
21301 ce->index = RECUR (ce->index);
21302
21303 if (PACK_EXPANSION_P (ce->value))
21304 {
21305 /* Substitute into the pack expansion. */
21306 ce->value = tsubst_pack_expansion (ce->value, args, complain,
21307 in_decl);
21308
21309 if (ce->value == error_mark_node
21310 || PACK_EXPANSION_P (ce->value))
21311 ;
21312 else if (TREE_VEC_LENGTH (ce->value) == 1)
21313 /* Just move the argument into place. */
21314 ce->value = TREE_VEC_ELT (ce->value, 0);
21315 else
21316 {
21317 /* Update the length of the final CONSTRUCTOR
21318 arguments vector, and note that we will need to
21319 copy.*/
21320 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
21321 need_copy_p = true;
21322 }
21323 }
21324 else
21325 ce->value = RECUR (ce->value);
21326 }
21327
21328 if (need_copy_p)
21329 {
21330 vec<constructor_elt, va_gc> *old_n = n;
21331
21332 vec_alloc (n, newlen);
21333 FOR_EACH_VEC_ELT (*old_n, idx, ce)
21334 {
21335 if (TREE_CODE (ce->value) == TREE_VEC)
21336 {
21337 int i, len = TREE_VEC_LENGTH (ce->value);
21338 for (i = 0; i < len; ++i)
21339 CONSTRUCTOR_APPEND_ELT (n, 0,
21340 TREE_VEC_ELT (ce->value, i));
21341 }
21342 else
21343 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
21344 }
21345 }
21346
21347 r = build_constructor (init_list_type_node, n);
21348 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
21349 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
21350 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
21351
21352 if (TREE_HAS_CONSTRUCTOR (t))
21353 {
21354 fcl_t cl = fcl_functional;
21355 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
21356 cl = fcl_c99;
21357 RETURN (finish_compound_literal (type, r, complain, cl));
21358 }
21359
21360 TREE_TYPE (r) = type;
21361 RETURN (r);
21362 }
21363
21364 case TYPEID_EXPR:
21365 {
21366 tree operand_0 = TREE_OPERAND (t, 0);
21367 if (TYPE_P (operand_0))
21368 {
21369 operand_0 = tsubst (operand_0, args, complain, in_decl);
21370 RETURN (get_typeid (operand_0, complain));
21371 }
21372 else
21373 {
21374 operand_0 = RECUR (operand_0);
21375 RETURN (build_typeid (operand_0, complain));
21376 }
21377 }
21378
21379 case FUNCTION_DECL:
21380 case PARM_DECL:
21381 case VAR_DECL:
21382 if (!args)
21383 RETURN (t);
21384 tree r;
21385 if (VAR_OR_FUNCTION_DECL_P (t)
21386 && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
21387 r = tsubst_decl (t, args, complain);
21388 else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
21389 {
21390 /* Local specialization will usually have been created when
21391 we instantiated the DECL_EXPR_DECL. */
21392 r = retrieve_local_specialization (t);
21393 if (!r)
21394 {
21395 /* We're in a generic lambda referencing a local extern
21396 from an outer block-scope of a non-template. */
21397 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
21398 r = t;
21399 }
21400 }
21401 else if (local_variable_p (t)
21402 && ((r = retrieve_local_specialization (t))
21403 || TREE_CODE (t) == PARM_DECL
21404 || uses_template_parms (DECL_CONTEXT (t))))
21405 {
21406 if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
21407 {
21408 /* We get here for a use of 'this' in an NSDMI. */
21409 if (DECL_NAME (t) == this_identifier && current_class_ptr)
21410 RETURN (current_class_ptr);
21411
21412 /* This can happen for a parameter name used later in a function
21413 declaration (such as in a late-specified return type). Just
21414 make a dummy decl, since it's only used for its type. */
21415 gcc_assert (cp_unevaluated_operand);
21416 r = tsubst_decl (t, args, complain);
21417 /* Give it the template pattern as its context; its true context
21418 hasn't been instantiated yet and this is good enough for
21419 mangling. */
21420 DECL_CONTEXT (r) = DECL_CONTEXT (t);
21421 }
21422 else if (r == NULL_TREE)
21423 {
21424 /* First try name lookup to find the instantiation. */
21425 r = lookup_name (DECL_NAME (t));
21426 if (r)
21427 {
21428 if (!VAR_P (r))
21429 {
21430 /* During error-recovery we may find a non-variable,
21431 even an OVERLOAD: just bail out and avoid ICEs and
21432 duplicate diagnostics (c++/62207). */
21433 gcc_assert (seen_error ());
21434 RETURN (error_mark_node);
21435 }
21436 if (!is_capture_proxy (r))
21437 {
21438 /* Make sure the one we found is the one we want. */
21439 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
21440 if (ctx != DECL_CONTEXT (r))
21441 r = NULL_TREE;
21442 }
21443 }
21444
21445 if (r)
21446 /* OK */;
21447 else
21448 {
21449 /* This can happen for a variable used in a
21450 late-specified return type of a local lambda, or for a
21451 local static or constant. Building a new VAR_DECL
21452 should be OK in all those cases. */
21453 r = tsubst_decl (t, args, complain);
21454 if (local_specializations)
21455 /* Avoid infinite recursion (79640). */
21456 register_local_specialization (r, t);
21457 if (decl_maybe_constant_var_p (r))
21458 {
21459 /* We can't call cp_finish_decl, so handle the
21460 initializer by hand. */
21461 tree init = tsubst_init (DECL_INITIAL (t), r, args,
21462 complain, in_decl);
21463 if (!processing_template_decl)
21464 init = maybe_constant_init (init);
21465 if (processing_template_decl
21466 ? potential_constant_expression (init)
21467 : reduced_constant_expression_p (init))
21468 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
21469 = TREE_CONSTANT (r) = true;
21470 DECL_INITIAL (r) = init;
21471 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
21472 TREE_TYPE (r)
21473 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
21474 complain, adc_variable_type);
21475 }
21476 gcc_assert (cp_unevaluated_operand
21477 || processing_contract_condition
21478 || TREE_STATIC (r)
21479 || decl_constant_var_p (r)
21480 || seen_error ());
21481 if (!processing_template_decl
21482 && !TREE_STATIC (r))
21483 r = process_outer_var_ref (r, complain);
21484 }
21485 /* Remember this for subsequent uses. */
21486 if (local_specializations)
21487 register_local_specialization (r, t);
21488 }
21489 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
21490 r = argument_pack_select_arg (r);
21491 }
21492 else
21493 r = t;
21494 if (!mark_used (r, complain))
21495 RETURN (error_mark_node);
21496
21497 if (!no_name_lookup_flag
21498 && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
21499 {
21500 /* ??? We're doing a subset of finish_id_expression here. */
21501 if (tree wrap = maybe_get_tls_wrapper_call (r))
21502 /* Replace an evaluated use of the thread_local variable with
21503 a call to its wrapper. */
21504 r = wrap;
21505 else if (outer_automatic_var_p (r))
21506 r = process_outer_var_ref (r, complain);
21507
21508 if (!TYPE_REF_P (TREE_TYPE (t)))
21509 /* If the original type was a reference, we'll be wrapped in
21510 the appropriate INDIRECT_REF. */
21511 r = convert_from_reference (r);
21512 }
21513 RETURN (r);
21514
21515 case CONST_DECL:
21516 {
21517 tree enum_type;
21518 tree v;
21519
21520 if (DECL_TEMPLATE_PARM_P (t))
21521 RETURN (RECUR (DECL_INITIAL (t)));
21522 if (!uses_template_parms (DECL_CONTEXT (t)))
21523 RETURN (t);
21524
21525 /* Unfortunately, we cannot just call lookup_name here.
21526 Consider:
21527
21528 template <int I> int f() {
21529 enum E { a = I };
21530 struct S { void g() { E e = a; } };
21531 };
21532
21533 When we instantiate f<7>::S::g(), say, lookup_name is not
21534 clever enough to find f<7>::a. */
21535 enum_type
21536 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
21537 /*entering_scope=*/0);
21538
21539 for (v = TYPE_VALUES (enum_type);
21540 v != NULL_TREE;
21541 v = TREE_CHAIN (v))
21542 if (TREE_PURPOSE (v) == DECL_NAME (t))
21543 RETURN (TREE_VALUE (v));
21544
21545 /* We didn't find the name. That should never happen; if
21546 name-lookup found it during preliminary parsing, we
21547 should find it again here during instantiation. */
21548 gcc_unreachable ();
21549 RETURN (t);
21550 }
21551
21552 case FIELD_DECL:
21553 if (DECL_CONTEXT (t))
21554 {
21555 tree ctx;
21556
21557 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
21558 /*entering_scope=*/1);
21559 if (ctx != DECL_CONTEXT (t))
21560 {
21561 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
21562 if (!r)
21563 {
21564 if (complain & tf_error)
21565 error ("using invalid field %qD", t);
21566 RETURN (error_mark_node);
21567 }
21568 RETURN (r);
21569 }
21570 }
21571 RETURN (t);
21572
21573 case NAMESPACE_DECL:
21574 case OVERLOAD:
21575 RETURN (t);
21576
21577 case TEMPLATE_DECL:
21578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
21579 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
21580 args, complain, in_decl));
21581 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
21582 RETURN (tsubst (t, args, complain, in_decl));
21583 else if (DECL_CLASS_SCOPE_P (t)
21584 && uses_template_parms (DECL_CONTEXT (t)))
21585 {
21586 /* Template template argument like the following example need
21587 special treatment:
21588
21589 template <template <class> class TT> struct C {};
21590 template <class T> struct D {
21591 template <class U> struct E {};
21592 C<E> c; // #1
21593 };
21594 D<int> d; // #2
21595
21596 We are processing the template argument `E' in #1 for
21597 the template instantiation #2. Originally, `E' is a
21598 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
21599 have to substitute this with one having context `D<int>'. */
21600
21601 tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
21602 in_decl, /*entering_scope=*/true);
21603 RETURN (lookup_field (context, DECL_NAME(t), 0, false));
21604 }
21605 else
21606 /* Ordinary template template argument. */
21607 RETURN (t);
21608
21609 case TEMPLATE_PARM_INDEX:
21610 case TYPE_DECL:
21611 RETURN (tsubst (t, args, complain, in_decl));
21612
21613 case CLEANUP_POINT_EXPR:
21614 /* We shouldn't have built any of these during initial template
21615 generation. Instead, they should be built during instantiation
21616 in response to the saved STMT_IS_FULL_EXPR_P setting. */
21617 gcc_unreachable ();
21618
21619 case OFFSET_REF:
21620 {
21621 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21622 tree op0 = RECUR (TREE_OPERAND (t, 0));
21623 tree op1 = RECUR (TREE_OPERAND (t, 1));
21624 r = build2 (OFFSET_REF, type, op0, op1);
21625 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
21626 if (!mark_used (TREE_OPERAND (r, 1), complain)
21627 && !(complain & tf_error))
21628 RETURN (error_mark_node);
21629 RETURN (r);
21630 }
21631
21632 case EXPR_PACK_EXPANSION:
21633 error ("invalid use of pack expansion expression");
21634 RETURN (error_mark_node);
21635
21636 case NONTYPE_ARGUMENT_PACK:
21637 error ("use %<...%> to expand argument pack");
21638 RETURN (error_mark_node);
21639
21640 case VOID_CST:
21641 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
21642 RETURN (t);
21643
21644 case INTEGER_CST:
21645 case REAL_CST:
21646 case COMPLEX_CST:
21647 case VECTOR_CST:
21648 {
21649 /* Instantiate any typedefs in the type. */
21650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21651 r = fold_convert (type, t);
21652 gcc_assert (TREE_CODE (r) == TREE_CODE (t));
21653 RETURN (r);
21654 }
21655
21656 case STRING_CST:
21657 {
21658 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21659 r = t;
21660 if (type != TREE_TYPE (t))
21661 {
21662 r = copy_node (t);
21663 TREE_TYPE (r) = type;
21664 }
21665 RETURN (r);
21666 }
21667
21668 case PTRMEM_CST:
21669 /* These can sometimes show up in a partial instantiation, but never
21670 involve template parms. */
21671 gcc_assert (!uses_template_parms (t));
21672 RETURN (t);
21673
21674 case UNARY_LEFT_FOLD_EXPR:
21675 RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
21676 case UNARY_RIGHT_FOLD_EXPR:
21677 RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
21678 case BINARY_LEFT_FOLD_EXPR:
21679 RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
21680 case BINARY_RIGHT_FOLD_EXPR:
21681 RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
21682 case PREDICT_EXPR:
21683 RETURN (t);
21684
21685 case DEBUG_BEGIN_STMT:
21686 /* ??? There's no point in copying it for now, but maybe some
21687 day it will contain more information, such as a pointer back
21688 to the containing function, inlined copy or so. */
21689 RETURN (t);
21690
21691 case CO_YIELD_EXPR:
21692 RETURN (finish_co_yield_expr (input_location,
21693 RECUR (TREE_OPERAND (t, 0))));
21694
21695 case CO_AWAIT_EXPR:
21696 RETURN (finish_co_await_expr (input_location,
21697 RECUR (TREE_OPERAND (t, 0))));
21698
21699 case VA_ARG_EXPR:
21700 {
21701 tree op0 = RECUR (TREE_OPERAND (t, 0));
21702 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21703 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
21704 }
21705
21706 case OFFSETOF_EXPR:
21707 {
21708 tree object_ptr
21709 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
21710 RETURN (finish_offsetof (object_ptr,
21711 RECUR (TREE_OPERAND (t, 0)),
21712 EXPR_LOCATION (t)));
21713 }
21714
21715 case ADDRESSOF_EXPR:
21716 RETURN (cp_build_addressof (EXPR_LOCATION (t),
21717 RECUR (TREE_OPERAND (t, 0)), complain));
21718
21719 case TRAIT_EXPR:
21720 {
21721 tree type1 = TRAIT_EXPR_TYPE1 (t);
21722 if (TYPE_P (type1))
21723 type1 = tsubst (type1, args, complain, in_decl);
21724 else
21725 type1 = tsubst_expr (type1, args, complain, in_decl);
21726 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
21727 complain, in_decl);
21728 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
21729 TRAIT_EXPR_KIND (t), type1, type2));
21730 }
21731
21732 case STMT_EXPR:
21733 {
21734 tree old_stmt_expr = cur_stmt_expr;
21735 tree stmt_expr = begin_stmt_expr ();
21736
21737 cur_stmt_expr = stmt_expr;
21738 tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
21739 stmt_expr = finish_stmt_expr (stmt_expr, false);
21740 cur_stmt_expr = old_stmt_expr;
21741
21742 /* If the resulting list of expression statement is empty,
21743 fold it further into void_node. */
21744 if (empty_expr_stmt_p (stmt_expr))
21745 stmt_expr = void_node;
21746
21747 RETURN (stmt_expr);
21748 }
21749
21750 case LAMBDA_EXPR:
21751 {
21752 if (complain & tf_partial)
21753 {
21754 /* We don't have a full set of template arguments yet; don't touch
21755 the lambda at all. */
21756 gcc_assert (processing_template_decl);
21757 return t;
21758 }
21759 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
21760
21761 RETURN (build_lambda_object (r));
21762 }
21763
21764 case TRANSACTION_EXPR:
21765 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
21766 RETURN (tsubst_stmt (t, args, complain, in_decl));
21767
21768 case PAREN_EXPR:
21769 if (REF_PARENTHESIZED_P (t))
21770 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
21771 else
21772 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21773 {
21774 tree op0 = RECUR (TREE_OPERAND (t, 0));
21775 RETURN (build1_loc (input_location, PAREN_EXPR,
21776 TREE_TYPE (op0), op0));
21777 }
21778
21779 case VEC_PERM_EXPR:
21780 {
21781 tree op0 = RECUR (TREE_OPERAND (t, 0));
21782 tree op1 = RECUR (TREE_OPERAND (t, 1));
21783 tree op2 = RECUR (TREE_OPERAND (t, 2));
21784 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
21785 complain));
21786 }
21787
21788 case REQUIRES_EXPR:
21789 {
21790 complain &= ~tf_warning_or_error;
21791 tree r = tsubst_requires_expr (t, args, complain, in_decl);
21792 RETURN (r);
21793 }
21794
21795 case RANGE_EXPR:
21796 /* No need to substitute further, a RANGE_EXPR will always be built
21797 with constant operands. */
21798 RETURN (t);
21799
21800 case NON_LVALUE_EXPR:
21801 case VIEW_CONVERT_EXPR:
21802 {
21803 tree op = RECUR (TREE_OPERAND (t, 0));
21804
21805 if (location_wrapper_p (t))
21806 /* We need to do this here as well as in tsubst_copy so we get the
21807 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21808 RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
21809
21810 gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
21811 if (REF_PARENTHESIZED_P (t))
21812 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21813 RETURN (finish_parenthesized_expr (op));
21814
21815 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21816 parameter object const. */
21817 if (TREE_TYPE (op) == NULL_TREE
21818 || !CP_TYPE_CONST_P (TREE_TYPE (op)))
21819 {
21820 /* The template argument is not const, presumably because
21821 it is still dependent, and so not the const template parm
21822 object. */
21823 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21824 if (TREE_CODE (op) == CONSTRUCTOR
21825 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
21826 {
21827 /* Don't add a wrapper to these. */
21828 op = copy_node (op);
21829 TREE_TYPE (op) = type;
21830 }
21831 else
21832 /* Do add a wrapper otherwise (in particular, if op is
21833 another TEMPLATE_PARM_INDEX). */
21834 op = build1 (VIEW_CONVERT_EXPR, type, op);
21835 }
21836 RETURN (op);
21837 }
21838
21839 default:
21840 /* Handle Objective-C++ constructs, if appropriate. */
21841 if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
21842 RETURN (subst);
21843
21844 /* We shouldn't get here, but keep going if !flag_checking. */
21845 if (flag_checking)
21846 gcc_unreachable ();
21847 RETURN (t);
21848 }
21849
21850 #undef RECUR
21851 #undef RETURN
21852 out:
21853 input_location = save_loc;
21854 return retval;
21855 }
21856
21857 /* Verify that the instantiated ARGS are valid. For type arguments,
21858 make sure that the type's linkage is ok. For non-type arguments,
21859 make sure they are constants if they are integral or enumerations.
21860 Emit an error under control of COMPLAIN, and return TRUE on error. */
21861
21862 static bool
21863 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
21864 {
21865 if (dependent_template_arg_p (t))
21866 return false;
21867 if (ARGUMENT_PACK_P (t))
21868 {
21869 tree vec = ARGUMENT_PACK_ARGS (t);
21870 int len = TREE_VEC_LENGTH (vec);
21871 bool result = false;
21872 int i;
21873
21874 for (i = 0; i < len; ++i)
21875 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
21876 result = true;
21877 return result;
21878 }
21879 else if (TYPE_P (t))
21880 {
21881 /* [basic.link]: A name with no linkage (notably, the name
21882 of a class or enumeration declared in a local scope)
21883 shall not be used to declare an entity with linkage.
21884 This implies that names with no linkage cannot be used as
21885 template arguments
21886
21887 DR 757 relaxes this restriction for C++0x. */
21888 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
21889 : no_linkage_check (t, /*relaxed_p=*/false));
21890
21891 if (nt)
21892 {
21893 /* DR 488 makes use of a type with no linkage cause
21894 type deduction to fail. */
21895 if (complain & tf_error)
21896 {
21897 if (TYPE_UNNAMED_P (nt))
21898 error ("%qT is/uses unnamed type", t);
21899 else
21900 error ("template argument for %qD uses local type %qT",
21901 tmpl, t);
21902 }
21903 return true;
21904 }
21905 /* In order to avoid all sorts of complications, we do not
21906 allow variably-modified types as template arguments. */
21907 else if (variably_modified_type_p (t, NULL_TREE))
21908 {
21909 if (complain & tf_error)
21910 error ("%qT is a variably modified type", t);
21911 return true;
21912 }
21913 }
21914 /* Class template and alias template arguments should be OK. */
21915 else if (DECL_TYPE_TEMPLATE_P (t))
21916 ;
21917 /* A non-type argument of integral or enumerated type must be a
21918 constant. */
21919 else if (TREE_TYPE (t)
21920 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
21921 && !REFERENCE_REF_P (t)
21922 && !TREE_CONSTANT (t))
21923 {
21924 if (complain & tf_error)
21925 error ("integral expression %qE is not constant", t);
21926 return true;
21927 }
21928 return false;
21929 }
21930
21931 static bool
21932 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
21933 {
21934 int ix, len = DECL_NTPARMS (tmpl);
21935 bool result = false;
21936
21937 for (ix = 0; ix != len; ix++)
21938 {
21939 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
21940 result = true;
21941 }
21942 if (result && (complain & tf_error))
21943 error (" trying to instantiate %qD", tmpl);
21944 return result;
21945 }
21946
21947 /* Call mark_used on each entity within the non-type template arguments in
21948 ARGS for an instantiation of TMPL, to ensure that each such entity is
21949 considered odr-used (and therefore marked for instantiation) regardless of
21950 whether the specialization was first formed in a template context (which
21951 inhibits mark_used).
21952
21953 This function assumes push_to_top_level has been called beforehand. */
21954
21955 static void
21956 mark_template_arguments_used (tree tmpl, tree args)
21957 {
21958 /* It suffices to do this only when instantiating a primary template. */
21959 if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
21960 return;
21961
21962 /* We already marked outer arguments when specializing the context. */
21963 args = INNERMOST_TEMPLATE_ARGS (args);
21964
21965 for (tree arg : tree_vec_range (args))
21966 {
21967 /* A (pointer/reference to) function or variable NTTP argument. */
21968 if (TREE_CODE (arg) == ADDR_EXPR
21969 || TREE_CODE (arg) == INDIRECT_REF)
21970 {
21971 while (TREE_CODE (arg) == ADDR_EXPR
21972 || REFERENCE_REF_P (arg)
21973 || CONVERT_EXPR_P (arg))
21974 arg = TREE_OPERAND (arg, 0);
21975 if (VAR_OR_FUNCTION_DECL_P (arg))
21976 {
21977 /* Pass tf_none to avoid duplicate diagnostics: if this call
21978 fails then an earlier call to mark_used for this argument
21979 must have also failed and emitted a diagnostic. */
21980 bool ok = mark_used (arg, tf_none);
21981 gcc_checking_assert (ok || seen_error ());
21982 }
21983 }
21984 /* A class NTTP argument. */
21985 else if (VAR_P (arg)
21986 && DECL_NTTP_OBJECT_P (arg))
21987 {
21988 auto mark_used_r = [](tree *tp, int *, void *) {
21989 if (VAR_OR_FUNCTION_DECL_P (*tp))
21990 {
21991 bool ok = mark_used (*tp, tf_none);
21992 gcc_checking_assert (ok || seen_error ());
21993 }
21994 return NULL_TREE;
21995 };
21996 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
21997 mark_used_r, nullptr);
21998 }
21999 }
22000 }
22001
22002 /* We're out of SFINAE context now, so generate diagnostics for the access
22003 errors we saw earlier when instantiating D from TMPL and ARGS. */
22004
22005 static void
22006 recheck_decl_substitution (tree d, tree tmpl, tree args)
22007 {
22008 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
22009 tree type = TREE_TYPE (pattern);
22010 location_t loc = input_location;
22011
22012 push_access_scope (d);
22013 push_deferring_access_checks (dk_no_deferred);
22014 input_location = DECL_SOURCE_LOCATION (pattern);
22015 tsubst (type, args, tf_warning_or_error, d);
22016 input_location = loc;
22017 pop_deferring_access_checks ();
22018 pop_access_scope (d);
22019 }
22020
22021 /* Instantiate the indicated variable, function, or alias template TMPL with
22022 the template arguments in TARG_PTR. */
22023
22024 tree
22025 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
22026 {
22027 auto_timevar tv (TV_TEMPLATE_INST);
22028
22029 tree targ_ptr = orig_args;
22030 tree fndecl;
22031 tree gen_tmpl;
22032 bool access_ok = true;
22033
22034 if (tmpl == error_mark_node)
22035 return error_mark_node;
22036
22037 /* The other flags are not relevant anymore here, especially tf_partial
22038 shouldn't be set. For instance, we may be called while doing a partial
22039 substitution of a template variable, but the type of the variable
22040 template may be auto, in which case we will call do_auto_deduction
22041 in mark_used (which clears tf_partial) and the auto must be properly
22042 reduced at that time for the deduction to work. */
22043 complain &= tf_warning_or_error;
22044
22045 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
22046
22047 if (modules_p ())
22048 lazy_load_pendings (tmpl);
22049
22050 /* If this function is a clone, handle it specially. */
22051 if (DECL_CLONED_FUNCTION_P (tmpl))
22052 {
22053 tree spec;
22054 tree clone;
22055
22056 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
22057 DECL_CLONED_FUNCTION. */
22058 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
22059 targ_ptr, complain);
22060 if (spec == error_mark_node)
22061 return error_mark_node;
22062
22063 /* Look for the clone. */
22064 FOR_EACH_CLONE (clone, spec)
22065 if (DECL_NAME (clone) == DECL_NAME (tmpl))
22066 return clone;
22067 /* We should always have found the clone by now. */
22068 gcc_unreachable ();
22069 return NULL_TREE;
22070 }
22071
22072 if (targ_ptr == error_mark_node)
22073 return error_mark_node;
22074
22075 /* Check to see if we already have this specialization. */
22076 gen_tmpl = most_general_template (tmpl);
22077 if (TMPL_ARGS_DEPTH (targ_ptr)
22078 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
22079 /* targ_ptr only has the innermost template args, so add the outer ones
22080 from tmpl, which could be either a partial instantiation or gen_tmpl (in
22081 the case of a non-dependent call within a template definition). */
22082 targ_ptr = (add_outermost_template_args
22083 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
22084 targ_ptr));
22085
22086 hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
22087 tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
22088
22089 gcc_checking_assert (tmpl == gen_tmpl
22090 || ((fndecl
22091 = retrieve_specialization (tmpl, orig_args, 0))
22092 == spec)
22093 || fndecl == NULL_TREE);
22094
22095 if (spec != NULL_TREE)
22096 {
22097 if (FNDECL_HAS_ACCESS_ERRORS (spec))
22098 {
22099 if (complain & tf_error)
22100 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
22101 return error_mark_node;
22102 }
22103 return spec;
22104 }
22105
22106 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
22107 complain))
22108 return error_mark_node;
22109
22110 /* We are building a FUNCTION_DECL, during which the access of its
22111 parameters and return types have to be checked. However this
22112 FUNCTION_DECL which is the desired context for access checking
22113 is not built yet. We solve this chicken-and-egg problem by
22114 deferring all checks until we have the FUNCTION_DECL. */
22115 push_deferring_access_checks (dk_deferred);
22116
22117 /* Instantiation of the function happens in the context of the function
22118 template, not the context of the overload resolution we're doing. */
22119 push_to_top_level ();
22120 /* If there are dependent arguments, e.g. because we're doing partial
22121 ordering, make sure processing_template_decl stays set. */
22122 if (uses_template_parms (targ_ptr))
22123 ++processing_template_decl;
22124 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22125 {
22126 tree ctx;
22127 if (!uses_template_parms (DECL_CONTEXT (tmpl)))
22128 /* If the context of the partially instantiated template is
22129 already non-dependent, then we might as well use it. */
22130 ctx = DECL_CONTEXT (tmpl);
22131 else
22132 ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
22133 complain, gen_tmpl, true);
22134 push_nested_class (ctx);
22135 }
22136
22137 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
22138
22139 tree partial_ti = NULL_TREE;
22140 fndecl = NULL_TREE;
22141 if (VAR_P (pattern))
22142 {
22143 /* We need to determine if we're using a partial or explicit
22144 specialization now, because the type of the variable could be
22145 different. */
22146 tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
22147 partial_ti = most_specialized_partial_spec (tid, complain);
22148 if (partial_ti == error_mark_node)
22149 pattern = error_mark_node;
22150 else if (partial_ti)
22151 {
22152 tree partial_tmpl = TI_TEMPLATE (partial_ti);
22153 tree partial_args = TI_ARGS (partial_ti);
22154 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
22155 fndecl = tsubst_decl (partial_pat, partial_args, complain,
22156 /*use_spec_table=*/false);
22157 }
22158 }
22159
22160 /* Substitute template parameters to obtain the specialization. */
22161 if (fndecl == NULL_TREE)
22162 fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
22163 if (DECL_CLASS_SCOPE_P (gen_tmpl))
22164 pop_nested_class ();
22165 pop_from_top_level ();
22166
22167 if (fndecl == error_mark_node)
22168 {
22169 pop_deferring_access_checks ();
22170 return error_mark_node;
22171 }
22172
22173 /* The DECL_TI_TEMPLATE should always be the immediate parent
22174 template, not the most general template. */
22175 DECL_TI_TEMPLATE (fndecl) = tmpl;
22176 DECL_TI_ARGS (fndecl) = targ_ptr;
22177 if (VAR_P (pattern))
22178 {
22179 /* Now that we we've formed this variable template specialization,
22180 remember the result of most_specialized_partial_spec for it. */
22181 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
22182
22183 /* And remember if the variable was declared with []. */
22184 if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
22185 && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
22186 SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
22187 }
22188
22189 fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
22190 if (fndecl == error_mark_node)
22191 return error_mark_node;
22192
22193 set_instantiating_module (fndecl);
22194
22195 /* Now we know the specialization, compute access previously
22196 deferred. Do no access control for inheriting constructors,
22197 as we already checked access for the inherited constructor. */
22198 if (!(flag_new_inheriting_ctors
22199 && DECL_INHERITED_CTOR (fndecl)))
22200 {
22201 push_access_scope (fndecl);
22202 if (!perform_deferred_access_checks (complain))
22203 access_ok = false;
22204 pop_access_scope (fndecl);
22205 }
22206 pop_deferring_access_checks ();
22207
22208 /* If we've just instantiated the main entry point for a function,
22209 instantiate all the alternate entry points as well. We do this
22210 by cloning the instantiation of the main entry point, not by
22211 instantiating the template clones. */
22212 if (tree chain = DECL_CHAIN (gen_tmpl))
22213 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
22214 clone_cdtor (fndecl, /*update_methods=*/false);
22215
22216 if (!access_ok)
22217 {
22218 if (!(complain & tf_error))
22219 {
22220 /* Remember to reinstantiate when we're out of SFINAE so the user
22221 can see the errors. */
22222 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
22223 }
22224 return error_mark_node;
22225 }
22226
22227 return fndecl;
22228 }
22229
22230 /* Instantiate the alias template TMPL with ARGS. Also push a template
22231 instantiation level, which instantiate_template doesn't do because
22232 functions and variables have sufficient context established by the
22233 callers. */
22234
22235 static tree
22236 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
22237 {
22238 if (tmpl == error_mark_node || args == error_mark_node)
22239 return error_mark_node;
22240
22241 args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
22242 args, tmpl, complain);
22243 if (args == error_mark_node)
22244 return error_mark_node;
22245
22246 /* FIXME check for satisfaction in check_instantiated_args. */
22247 if (!constraints_satisfied_p (tmpl, args))
22248 {
22249 if (complain & tf_error)
22250 {
22251 auto_diagnostic_group d;
22252 error ("template constraint failure for %qD", tmpl);
22253 diagnose_constraints (input_location, tmpl, args);
22254 }
22255 return error_mark_node;
22256 }
22257
22258 if (!push_tinst_level (tmpl, args))
22259 return error_mark_node;
22260 tree r = instantiate_template (tmpl, args, complain);
22261 pop_tinst_level ();
22262
22263 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
22264 {
22265 /* An alias template specialization can be dependent
22266 even if its underlying type is not. */
22267 TYPE_DEPENDENT_P (d) = true;
22268 TYPE_DEPENDENT_P_VALID (d) = true;
22269 /* Sometimes a dependent alias spec is equivalent to its expansion,
22270 sometimes not. So always use structural_comptypes. */
22271 SET_TYPE_STRUCTURAL_EQUALITY (d);
22272 }
22273
22274 return r;
22275 }
22276
22277 /* PARM is a template parameter pack for FN. Returns true iff
22278 PARM is used in a deducible way in the argument list of FN. */
22279
22280 static bool
22281 pack_deducible_p (tree parm, tree fn)
22282 {
22283 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
22284 for (; t; t = TREE_CHAIN (t))
22285 {
22286 tree type = TREE_VALUE (t);
22287 tree packs;
22288 if (!PACK_EXPANSION_P (type))
22289 continue;
22290 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
22291 packs; packs = TREE_CHAIN (packs))
22292 if (template_args_equal (TREE_VALUE (packs), parm))
22293 {
22294 /* The template parameter pack is used in a function parameter
22295 pack. If this is the end of the parameter list, the
22296 template parameter pack is deducible. */
22297 if (TREE_CHAIN (t) == void_list_node)
22298 return true;
22299 else
22300 /* Otherwise, not. Well, it could be deduced from
22301 a non-pack parameter, but doing so would end up with
22302 a deduction mismatch, so don't bother. */
22303 return false;
22304 }
22305 }
22306 /* The template parameter pack isn't used in any function parameter
22307 packs, but it might be used deeper, e.g. tuple<Args...>. */
22308 return true;
22309 }
22310
22311 /* Subroutine of fn_type_unification: check non-dependent parms for
22312 convertibility. */
22313
22314 static int
22315 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
22316 tree fn, unification_kind_t strict, int flags,
22317 struct conversion **convs, bool explain_p,
22318 bool noninst_only_p)
22319 {
22320 /* Non-constructor methods need to leave a conversion for 'this', which
22321 isn't included in nargs here. */
22322 unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
22323 && !DECL_CONSTRUCTOR_P (fn));
22324
22325 for (unsigned ia = 0;
22326 parms && parms != void_list_node && ia < nargs; )
22327 {
22328 tree parm = TREE_VALUE (parms);
22329
22330 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22331 && (!TREE_CHAIN (parms)
22332 || TREE_CHAIN (parms) == void_list_node))
22333 /* For a function parameter pack that occurs at the end of the
22334 parameter-declaration-list, the type A of each remaining
22335 argument of the call is compared with the type P of the
22336 declarator-id of the function parameter pack. */
22337 break;
22338
22339 parms = TREE_CHAIN (parms);
22340
22341 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22342 /* For a function parameter pack that does not occur at the
22343 end of the parameter-declaration-list, the type of the
22344 parameter pack is a non-deduced context. */
22345 continue;
22346
22347 if (!uses_template_parms (parm))
22348 {
22349 tree arg = args[ia];
22350 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
22351 int lflags = conv_flags (ia, nargs, fn, arg, flags);
22352
22353 if (check_non_deducible_conversion (parm, arg, strict, lflags,
22354 conv_p, explain_p, noninst_only_p))
22355 return 1;
22356 }
22357
22358 ++ia;
22359 }
22360
22361 return 0;
22362 }
22363
22364 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22365 NARGS elements of the arguments that are being used when calling
22366 it. TARGS is a vector into which the deduced template arguments
22367 are placed.
22368
22369 Returns either a FUNCTION_DECL for the matching specialization of FN or
22370 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22371 true, diagnostics will be printed to explain why it failed.
22372
22373 If FN is a conversion operator, or we are trying to produce a specific
22374 specialization, RETURN_TYPE is the return type desired.
22375
22376 The EXPLICIT_TARGS are explicit template arguments provided via a
22377 template-id.
22378
22379 The parameter STRICT is one of:
22380
22381 DEDUCE_CALL:
22382 We are deducing arguments for a function call, as in
22383 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22384 deducing arguments for a call to the result of a conversion
22385 function template, as in [over.call.object].
22386
22387 DEDUCE_CONV:
22388 We are deducing arguments for a conversion function, as in
22389 [temp.deduct.conv].
22390
22391 DEDUCE_EXACT:
22392 We are deducing arguments when doing an explicit instantiation
22393 as in [temp.explicit], when determining an explicit specialization
22394 as in [temp.expl.spec], or when taking the address of a function
22395 template, as in [temp.deduct.funcaddr]. */
22396
22397 tree
22398 fn_type_unification (tree fn,
22399 tree explicit_targs,
22400 tree targs,
22401 const tree *args,
22402 unsigned int nargs,
22403 tree return_type,
22404 unification_kind_t strict,
22405 int flags,
22406 struct conversion **convs,
22407 bool explain_p,
22408 bool decltype_p)
22409 {
22410 tree parms;
22411 tree fntype;
22412 tree decl = NULL_TREE;
22413 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22414 bool ok;
22415 static int deduction_depth;
22416 /* type_unification_real will pass back any access checks from default
22417 template argument substitution. */
22418 vec<deferred_access_check, va_gc> *checks = NULL;
22419 /* We don't have all the template args yet. */
22420 bool incomplete = true;
22421
22422 tree orig_fn = fn;
22423 if (flag_new_inheriting_ctors)
22424 fn = strip_inheriting_ctors (fn);
22425
22426 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
22427 tree r = error_mark_node;
22428
22429 tree full_targs = targs;
22430 if (TMPL_ARGS_DEPTH (targs)
22431 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
22432 full_targs = (add_outermost_template_args
22433 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
22434 targs));
22435
22436 if (decltype_p)
22437 complain |= tf_decltype;
22438
22439 /* In C++0x, it's possible to have a function template whose type depends
22440 on itself recursively. This is most obvious with decltype, but can also
22441 occur with enumeration scope (c++/48969). So we need to catch infinite
22442 recursion and reject the substitution at deduction time; this function
22443 will return error_mark_node for any repeated substitution.
22444
22445 This also catches excessive recursion such as when f<N> depends on
22446 f<N-1> across all integers, and returns error_mark_node for all the
22447 substitutions back up to the initial one.
22448
22449 This is, of course, not reentrant. */
22450 if (excessive_deduction_depth)
22451 return error_mark_node;
22452 ++deduction_depth;
22453
22454 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
22455
22456 fntype = TREE_TYPE (fn);
22457 if (explicit_targs)
22458 {
22459 /* [temp.deduct]
22460
22461 The specified template arguments must match the template
22462 parameters in kind (i.e., type, nontype, template), and there
22463 must not be more arguments than there are parameters;
22464 otherwise type deduction fails.
22465
22466 Nontype arguments must match the types of the corresponding
22467 nontype template parameters, or must be convertible to the
22468 types of the corresponding nontype parameters as specified in
22469 _temp.arg.nontype_, otherwise type deduction fails.
22470
22471 All references in the function type of the function template
22472 to the corresponding template parameters are replaced by the
22473 specified template argument values. If a substitution in a
22474 template parameter or in the function type of the function
22475 template results in an invalid type, type deduction fails. */
22476 int i, len = TREE_VEC_LENGTH (tparms);
22477 location_t loc = input_location;
22478 incomplete = false;
22479
22480 if (explicit_targs == error_mark_node)
22481 goto fail;
22482
22483 if (TMPL_ARGS_DEPTH (explicit_targs)
22484 < TMPL_ARGS_DEPTH (full_targs))
22485 explicit_targs = add_outermost_template_args (full_targs,
22486 explicit_targs);
22487
22488 /* Adjust any explicit template arguments before entering the
22489 substitution context. */
22490 explicit_targs
22491 = (coerce_template_parms (tparms, explicit_targs, fn,
22492 complain|tf_partial,
22493 /*require_all_args=*/false));
22494 if (explicit_targs == error_mark_node)
22495 goto fail;
22496
22497 /* Substitute the explicit args into the function type. This is
22498 necessary so that, for instance, explicitly declared function
22499 arguments can match null pointed constants. If we were given
22500 an incomplete set of explicit args, we must not do semantic
22501 processing during substitution as we could create partial
22502 instantiations. */
22503 for (i = 0; i < len; i++)
22504 {
22505 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
22506 bool parameter_pack = false;
22507 tree targ = TREE_VEC_ELT (explicit_targs, i);
22508
22509 /* Dig out the actual parm. */
22510 if (TREE_CODE (parm) == TYPE_DECL
22511 || TREE_CODE (parm) == TEMPLATE_DECL)
22512 {
22513 parm = TREE_TYPE (parm);
22514 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
22515 }
22516 else if (TREE_CODE (parm) == PARM_DECL)
22517 {
22518 parm = DECL_INITIAL (parm);
22519 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
22520 }
22521
22522 if (targ == NULL_TREE)
22523 /* No explicit argument for this template parameter. */
22524 incomplete = true;
22525 else if (parameter_pack && pack_deducible_p (parm, fn))
22526 {
22527 /* Mark the argument pack as "incomplete". We could
22528 still deduce more arguments during unification.
22529 We remove this mark in type_unification_real. */
22530 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
22531 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
22532 = ARGUMENT_PACK_ARGS (targ);
22533
22534 /* We have some incomplete argument packs. */
22535 incomplete = true;
22536 }
22537 }
22538
22539 if (incomplete)
22540 {
22541 if (!push_tinst_level (fn, explicit_targs))
22542 {
22543 excessive_deduction_depth = true;
22544 goto fail;
22545 }
22546 ++processing_template_decl;
22547 input_location = DECL_SOURCE_LOCATION (fn);
22548 /* Ignore any access checks; we'll see them again in
22549 instantiate_template and they might have the wrong
22550 access path at this point. */
22551 push_deferring_access_checks (dk_deferred);
22552 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
22553 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
22554 pop_deferring_access_checks ();
22555 input_location = loc;
22556 --processing_template_decl;
22557 pop_tinst_level ();
22558
22559 if (fntype == error_mark_node)
22560 goto fail;
22561 }
22562
22563 /* Place the explicitly specified arguments in TARGS. */
22564 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
22565 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
22566 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
22567 if (!incomplete && CHECKING_P
22568 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22569 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22570 (targs, NUM_TMPL_ARGS (explicit_targs));
22571 }
22572
22573 if (return_type && strict != DEDUCE_CALL)
22574 {
22575 tree *new_args = XALLOCAVEC (tree, nargs + 1);
22576 new_args[0] = return_type;
22577 memcpy (new_args + 1, args, nargs * sizeof (tree));
22578 args = new_args;
22579 ++nargs;
22580 }
22581
22582 if (!incomplete)
22583 goto deduced;
22584
22585 /* Never do unification on the 'this' parameter. */
22586 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
22587
22588 if (return_type && strict == DEDUCE_CALL)
22589 {
22590 /* We're deducing for a call to the result of a template conversion
22591 function. The parms we really want are in return_type. */
22592 if (INDIRECT_TYPE_P (return_type))
22593 return_type = TREE_TYPE (return_type);
22594 parms = TYPE_ARG_TYPES (return_type);
22595 }
22596 else if (return_type)
22597 {
22598 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
22599 }
22600
22601 /* We allow incomplete unification without an error message here
22602 because the standard doesn't seem to explicitly prohibit it. Our
22603 callers must be ready to deal with unification failures in any
22604 event. */
22605
22606 /* If we aren't explaining yet, push tinst context so we can see where
22607 any errors (e.g. from class instantiations triggered by instantiation
22608 of default template arguments) come from. If we are explaining, this
22609 context is redundant. */
22610 if (!explain_p && !push_tinst_level (fn, targs))
22611 {
22612 excessive_deduction_depth = true;
22613 goto fail;
22614 }
22615
22616 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22617 full_targs, parms, args, nargs, /*subr=*/0,
22618 strict, &checks, explain_p);
22619 if (!explain_p)
22620 pop_tinst_level ();
22621 if (!ok)
22622 goto fail;
22623
22624 /* Now that we have bindings for all of the template arguments,
22625 ensure that the arguments deduced for the template template
22626 parameters have compatible template parameter lists. We cannot
22627 check this property before we have deduced all template
22628 arguments, because the template parameter types of a template
22629 template parameter might depend on prior template parameters
22630 deduced after the template template parameter. The following
22631 ill-formed example illustrates this issue:
22632
22633 template<typename T, template<T> class C> void f(C<5>, T);
22634
22635 template<int N> struct X {};
22636
22637 void g() {
22638 f(X<5>(), 5l); // error: template argument deduction fails
22639 }
22640
22641 The template parameter list of 'C' depends on the template type
22642 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22643 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22644 time that we deduce 'C'. */
22645 if (!template_template_parm_bindings_ok_p
22646 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
22647 {
22648 unify_inconsistent_template_template_parameters (explain_p);
22649 goto fail;
22650 }
22651
22652 deduced:
22653
22654 /* As a refinement of CWG2369, check first and foremost non-dependent
22655 conversions that we know are not going to induce template instantiation
22656 (PR99599). */
22657 if (strict == DEDUCE_CALL
22658 && incomplete
22659 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22660 convs, explain_p,
22661 /*noninst_only_p=*/true))
22662 goto fail;
22663
22664 /* CWG2369: Check satisfaction before non-deducible conversions. */
22665 if (!constraints_satisfied_p (fn, targs))
22666 {
22667 if (explain_p)
22668 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
22669 goto fail;
22670 }
22671
22672 /* DR 1391: All parameters have args, now check non-dependent parms for
22673 convertibility. We don't do this if all args were explicitly specified,
22674 as the standard says that we substitute explicit args immediately. */
22675 if (incomplete
22676 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22677 convs, explain_p,
22678 /*noninst_only_p=*/false))
22679 goto fail;
22680
22681 /* All is well so far. Now, check:
22682
22683 [temp.deduct]
22684
22685 When all template arguments have been deduced, all uses of
22686 template parameters in nondeduced contexts are replaced with
22687 the corresponding deduced argument values. If the
22688 substitution results in an invalid type, as described above,
22689 type deduction fails. */
22690 if (!push_tinst_level (fn, targs))
22691 {
22692 excessive_deduction_depth = true;
22693 goto fail;
22694 }
22695
22696 /* Also collect access checks from the instantiation. */
22697 reopen_deferring_access_checks (checks);
22698
22699 decl = instantiate_template (fn, targs, complain);
22700
22701 checks = get_deferred_access_checks ();
22702 pop_deferring_access_checks ();
22703
22704 pop_tinst_level ();
22705
22706 if (decl == error_mark_node)
22707 goto fail;
22708
22709 /* Now perform any access checks encountered during substitution. */
22710 push_access_scope (decl);
22711 ok = perform_access_checks (checks, complain);
22712 pop_access_scope (decl);
22713 if (!ok)
22714 goto fail;
22715
22716 /* If we're looking for an exact match, check that what we got
22717 is indeed an exact match. It might not be if some template
22718 parameters are used in non-deduced contexts. But don't check
22719 for an exact match if we have dependent template arguments;
22720 in that case we're doing partial ordering, and we already know
22721 that we have two candidates that will provide the actual type. */
22722 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
22723 {
22724 tree substed = TREE_TYPE (decl);
22725 unsigned int i;
22726
22727 tree sarg
22728 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
22729 if (return_type)
22730 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
22731 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
22732 if (!same_type_p (args[i], TREE_VALUE (sarg)))
22733 {
22734 unify_type_mismatch (explain_p, args[i],
22735 TREE_VALUE (sarg));
22736 goto fail;
22737 }
22738 if ((i < nargs || sarg)
22739 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22740 doesn't contain the trailing void, and conv fns are always (). */
22741 && !DECL_CONV_FN_P (decl))
22742 {
22743 unsigned nsargs = i + list_length (sarg);
22744 unify_arity (explain_p, nargs, nsargs);
22745 goto fail;
22746 }
22747 }
22748
22749 /* After doing deduction with the inherited constructor, actually return an
22750 instantiation of the inheriting constructor. */
22751 if (orig_fn != fn)
22752 decl = instantiate_template (orig_fn, targs, complain);
22753
22754 r = decl;
22755
22756 fail:
22757 --deduction_depth;
22758 if (excessive_deduction_depth)
22759 {
22760 if (deduction_depth == 0)
22761 /* Reset once we're all the way out. */
22762 excessive_deduction_depth = false;
22763 }
22764
22765 return r;
22766 }
22767
22768 /* Returns true iff PARM is a forwarding reference in the context of
22769 template argument deduction for TMPL. */
22770
22771 static bool
22772 forwarding_reference_p (tree parm, tree tmpl)
22773 {
22774 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22775 cv-unqualified template parameter ..." */
22776 if (TYPE_REF_P (parm)
22777 && TYPE_REF_IS_RVALUE (parm)
22778 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
22779 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
22780 {
22781 parm = TREE_TYPE (parm);
22782 /* [temp.deduct.call], "... that does not represent a template parameter
22783 of a class template (during class template argument deduction)." */
22784 if (tmpl
22785 && deduction_guide_p (tmpl)
22786 && DECL_ARTIFICIAL (tmpl))
22787 {
22788 /* Since the template parameters of a synthesized guide consist of
22789 the template parameters of the class template followed by those of
22790 the constructor (if any), we can tell if PARM represents a template
22791 parameter of the class template by comparing its index with the
22792 arity of the class template. */
22793 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
22794 if (TEMPLATE_TYPE_IDX (parm)
22795 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
22796 return false;
22797 }
22798 return true;
22799 }
22800 return false;
22801 }
22802
22803 /* Adjust types before performing type deduction, as described in
22804 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22805 sections are symmetric. PARM is the type of a function parameter
22806 or the return type of the conversion function. ARG is the type of
22807 the argument passed to the call, or the type of the value
22808 initialized with the result of the conversion function.
22809 ARG_EXPR is the original argument expression, which may be null. */
22810
22811 static int
22812 maybe_adjust_types_for_deduction (tree tparms,
22813 unification_kind_t strict,
22814 tree* parm,
22815 tree* arg,
22816 tree arg_expr)
22817 {
22818 int result = 0;
22819
22820 switch (strict)
22821 {
22822 case DEDUCE_CALL:
22823 break;
22824
22825 case DEDUCE_CONV:
22826 /* [temp.deduct.conv] First remove a reference type on parm.
22827 DRs 322 & 976 affected this. */
22828 if (TYPE_REF_P (*parm))
22829 *parm = TREE_TYPE (*parm);
22830
22831 /* Swap PARM and ARG throughout the remainder of this
22832 function; the handling is precisely symmetric since PARM
22833 will initialize ARG rather than vice versa. */
22834 std::swap (parm, arg);
22835
22836 break;
22837
22838 case DEDUCE_EXACT:
22839 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22840 too, but here handle it by stripping the reference from PARM
22841 rather than by adding it to ARG. */
22842 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22843 && TYPE_REF_P (*arg)
22844 && !TYPE_REF_IS_RVALUE (*arg))
22845 *parm = TREE_TYPE (*parm);
22846 /* Nothing else to do in this case. */
22847 return 0;
22848
22849 default:
22850 gcc_unreachable ();
22851 }
22852
22853 if (!TYPE_REF_P (*parm))
22854 {
22855 /* [temp.deduct.call]
22856
22857 If P is not a reference type:
22858
22859 --If A is an array type, the pointer type produced by the
22860 array-to-pointer standard conversion (_conv.array_) is
22861 used in place of A for type deduction; otherwise,
22862
22863 --If A is a function type, the pointer type produced by
22864 the function-to-pointer standard conversion
22865 (_conv.func_) is used in place of A for type deduction;
22866 otherwise,
22867
22868 --If A is a cv-qualified type, the top level
22869 cv-qualifiers of A's type are ignored for type
22870 deduction. */
22871 if (TREE_CODE (*arg) == ARRAY_TYPE)
22872 *arg = build_pointer_type (TREE_TYPE (*arg));
22873 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
22874 *arg = build_pointer_type (*arg);
22875 else
22876 *arg = TYPE_MAIN_VARIANT (*arg);
22877 }
22878
22879 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22880 an lvalue, the type 'lvalue reference to A' is used in place of A for
22881 type deduction." */
22882 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22883 && (arg_expr ? lvalue_p (arg_expr)
22884 /* try_one_overload doesn't provide an arg_expr, but
22885 functions are always lvalues. */
22886 : TREE_CODE (*arg) == FUNCTION_TYPE))
22887 *arg = build_reference_type (*arg);
22888
22889 /* [temp.deduct.call]
22890
22891 If P is a cv-qualified type, the top level cv-qualifiers
22892 of P's type are ignored for type deduction. If P is a
22893 reference type, the type referred to by P is used for
22894 type deduction. */
22895 *parm = TYPE_MAIN_VARIANT (*parm);
22896 if (TYPE_REF_P (*parm))
22897 {
22898 *parm = TREE_TYPE (*parm);
22899 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22900 }
22901
22902 return result;
22903 }
22904
22905 /* Return true if computing a conversion from FROM to TO might induce template
22906 instantiation. Conversely, if this predicate returns false then computing
22907 the conversion definitely won't induce template instantiation. */
22908
22909 static bool
22910 conversion_may_instantiate_p (tree to, tree from)
22911 {
22912 to = non_reference (to);
22913 from = non_reference (from);
22914
22915 bool ptr_conv_p = false;
22916 if (TYPE_PTR_P (to)
22917 && TYPE_PTR_P (from))
22918 {
22919 to = TREE_TYPE (to);
22920 from = TREE_TYPE (from);
22921 ptr_conv_p = true;
22922 }
22923
22924 /* If one of the types is a not-yet-instantiated class template
22925 specialization, then computing the conversion might instantiate
22926 it in order to inspect bases, conversion functions and/or
22927 converting constructors. */
22928 if ((CLASS_TYPE_P (to)
22929 && !COMPLETE_TYPE_P (to)
22930 && CLASSTYPE_TEMPLATE_INSTANTIATION (to))
22931 || (CLASS_TYPE_P (from)
22932 && !COMPLETE_TYPE_P (from)
22933 && CLASSTYPE_TEMPLATE_INSTANTIATION (from)))
22934 return true;
22935
22936 /* Converting from one pointer type to another, or between
22937 reference-related types, always yields a standard conversion. */
22938 if (ptr_conv_p || reference_related_p (to, from))
22939 return false;
22940
22941 /* Converting to a non-aggregate class type will consider its
22942 user-declared constructors, which might induce instantiation. */
22943 if (CLASS_TYPE_P (to)
22944 && CLASSTYPE_NON_AGGREGATE (to))
22945 return true;
22946
22947 /* Similarly, converting from a class type will consider its conversion
22948 functions. */
22949 if (CLASS_TYPE_P (from)
22950 && TYPE_HAS_CONVERSION (from))
22951 return true;
22952
22953 /* Otherwise, computing this conversion definitely won't induce
22954 template instantiation. */
22955 return false;
22956 }
22957
22958 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22959 template which doesn't contain any deducible template parameters; check if
22960 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22961 unify_one_argument. */
22962
22963 static int
22964 check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
22965 int flags, struct conversion **conv_p,
22966 bool explain_p, bool noninst_only_p)
22967 {
22968 tree type;
22969
22970 if (!TYPE_P (arg))
22971 type = TREE_TYPE (arg);
22972 else
22973 type = arg;
22974
22975 if (same_type_p (parm, type))
22976 return unify_success (explain_p);
22977
22978 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22979 if (strict == DEDUCE_CONV)
22980 {
22981 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
22982 return unify_success (explain_p);
22983 }
22984 else if (strict == DEDUCE_CALL)
22985 {
22986 if (conv_p && *conv_p)
22987 {
22988 /* This conversion was already computed earlier (when
22989 computing only non-instantiating conversions). */
22990 gcc_checking_assert (!noninst_only_p);
22991 return unify_success (explain_p);
22992 }
22993
22994 if (noninst_only_p
22995 && conversion_may_instantiate_p (parm, type))
22996 return unify_success (explain_p);
22997
22998 bool ok = false;
22999 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
23000 if (conv_p)
23001 /* Avoid recalculating this in add_function_candidate. */
23002 ok = (*conv_p
23003 = good_conversion (parm, type, conv_arg, flags, complain));
23004 else
23005 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
23006 if (ok)
23007 return unify_success (explain_p);
23008 }
23009
23010 if (strict == DEDUCE_EXACT)
23011 return unify_type_mismatch (explain_p, parm, arg);
23012 else
23013 return unify_arg_conversion (explain_p, parm, type, arg);
23014 }
23015
23016 static bool uses_deducible_template_parms (tree type);
23017
23018 /* Returns true iff the expression EXPR is one from which a template
23019 argument can be deduced. In other words, if it's an undecorated
23020 use of a template non-type parameter. */
23021
23022 static bool
23023 deducible_expression (tree expr)
23024 {
23025 /* Strip implicit conversions and implicit INDIRECT_REFs. */
23026 while (CONVERT_EXPR_P (expr)
23027 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
23028 || REFERENCE_REF_P (expr))
23029 expr = TREE_OPERAND (expr, 0);
23030 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
23031 }
23032
23033 /* Returns true iff the array domain DOMAIN uses a template parameter in a
23034 deducible way; that is, if it has a max value of <PARM> - 1. */
23035
23036 static bool
23037 deducible_array_bound (tree domain)
23038 {
23039 if (domain == NULL_TREE)
23040 return false;
23041
23042 tree max = TYPE_MAX_VALUE (domain);
23043 if (TREE_CODE (max) != MINUS_EXPR)
23044 return false;
23045
23046 return deducible_expression (TREE_OPERAND (max, 0));
23047 }
23048
23049 /* Returns true iff the template arguments ARGS use a template parameter
23050 in a deducible way. */
23051
23052 static bool
23053 deducible_template_args (tree args)
23054 {
23055 for (tree elt : tree_vec_range (args))
23056 {
23057 bool deducible;
23058 if (ARGUMENT_PACK_P (elt))
23059 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
23060 else
23061 {
23062 if (PACK_EXPANSION_P (elt))
23063 elt = PACK_EXPANSION_PATTERN (elt);
23064 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
23065 deducible = true;
23066 else if (TYPE_P (elt))
23067 deducible = uses_deducible_template_parms (elt);
23068 else
23069 deducible = deducible_expression (elt);
23070 }
23071 if (deducible)
23072 return true;
23073 }
23074 return false;
23075 }
23076
23077 /* Returns true iff TYPE contains any deducible references to template
23078 parameters, as per 14.8.2.5. */
23079
23080 static bool
23081 uses_deducible_template_parms (tree type)
23082 {
23083 if (PACK_EXPANSION_P (type))
23084 type = PACK_EXPANSION_PATTERN (type);
23085
23086 /* T
23087 cv-list T
23088 TT<T>
23089 TT<i>
23090 TT<> */
23091 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23092 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23093 return true;
23094
23095 /* T*
23096 T&
23097 T&& */
23098 if (INDIRECT_TYPE_P (type))
23099 return uses_deducible_template_parms (TREE_TYPE (type));
23100
23101 /* T[integer-constant ]
23102 type [i] */
23103 if (TREE_CODE (type) == ARRAY_TYPE)
23104 return (uses_deducible_template_parms (TREE_TYPE (type))
23105 || deducible_array_bound (TYPE_DOMAIN (type)));
23106
23107 /* T type ::*
23108 type T::*
23109 T T::*
23110 T (type ::*)()
23111 type (T::*)()
23112 type (type ::*)(T)
23113 type (T::*)(T)
23114 T (type ::*)(T)
23115 T (T::*)()
23116 T (T::*)(T) */
23117 if (TYPE_PTRMEM_P (type))
23118 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
23119 || (uses_deducible_template_parms
23120 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
23121
23122 /* template-name <T> (where template-name refers to a class template)
23123 template-name <i> (where template-name refers to a class template) */
23124 if (CLASS_TYPE_P (type)
23125 && CLASSTYPE_TEMPLATE_INFO (type)
23126 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
23127 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
23128 (CLASSTYPE_TI_ARGS (type)));
23129
23130 /* type (T)
23131 T()
23132 T(T) */
23133 if (FUNC_OR_METHOD_TYPE_P (type))
23134 {
23135 if (uses_deducible_template_parms (TREE_TYPE (type)))
23136 return true;
23137 tree parm = TYPE_ARG_TYPES (type);
23138 if (TREE_CODE (type) == METHOD_TYPE)
23139 parm = TREE_CHAIN (parm);
23140 for (; parm; parm = TREE_CHAIN (parm))
23141 if (uses_deducible_template_parms (TREE_VALUE (parm)))
23142 return true;
23143 if (flag_noexcept_type
23144 && TYPE_RAISES_EXCEPTIONS (type)
23145 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
23146 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
23147 return true;
23148 }
23149
23150 return false;
23151 }
23152
23153 /* Subroutine of type_unification_real and unify_pack_expansion to
23154 handle unification of a single P/A pair. Parameters are as
23155 for those functions. */
23156
23157 static int
23158 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
23159 int subr, unification_kind_t strict,
23160 bool explain_p)
23161 {
23162 tree arg_expr = NULL_TREE;
23163 int arg_strict;
23164
23165 if (arg == error_mark_node || parm == error_mark_node)
23166 return unify_invalid (explain_p);
23167 if (arg == unknown_type_node)
23168 /* We can't deduce anything from this, but we might get all the
23169 template args from other function args. */
23170 return unify_success (explain_p);
23171
23172 /* Implicit conversions (Clause 4) will be performed on a function
23173 argument to convert it to the type of the corresponding function
23174 parameter if the parameter type contains no template-parameters that
23175 participate in template argument deduction. */
23176 if (strict != DEDUCE_EXACT
23177 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
23178 /* For function parameters with no deducible template parameters,
23179 just return. We'll check non-dependent conversions later. */
23180 return unify_success (explain_p);
23181
23182 switch (strict)
23183 {
23184 case DEDUCE_CALL:
23185 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
23186 | UNIFY_ALLOW_MORE_CV_QUAL
23187 | UNIFY_ALLOW_DERIVED);
23188 break;
23189
23190 case DEDUCE_CONV:
23191 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
23192 break;
23193
23194 case DEDUCE_EXACT:
23195 arg_strict = UNIFY_ALLOW_NONE;
23196 break;
23197
23198 default:
23199 gcc_unreachable ();
23200 }
23201
23202 /* We only do these transformations if this is the top-level
23203 parameter_type_list in a call or declaration matching; in other
23204 situations (nested function declarators, template argument lists) we
23205 won't be comparing a type to an expression, and we don't do any type
23206 adjustments. */
23207 if (!subr)
23208 {
23209 if (!TYPE_P (arg))
23210 {
23211 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
23212 if (type_unknown_p (arg))
23213 {
23214 /* [temp.deduct.type] A template-argument can be
23215 deduced from a pointer to function or pointer
23216 to member function argument if the set of
23217 overloaded functions does not contain function
23218 templates and at most one of a set of
23219 overloaded functions provides a unique
23220 match. */
23221 resolve_overloaded_unification (tparms, targs, parm,
23222 arg, strict,
23223 arg_strict, explain_p);
23224 /* If a unique match was not found, this is a
23225 non-deduced context, so we still succeed. */
23226 return unify_success (explain_p);
23227 }
23228
23229 arg_expr = arg;
23230 arg = unlowered_expr_type (arg);
23231 if (arg == error_mark_node)
23232 return unify_invalid (explain_p);
23233 }
23234
23235 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23236 &parm, &arg, arg_expr);
23237 }
23238 else
23239 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
23240 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
23241 return unify_template_argument_mismatch (explain_p, parm, arg);
23242
23243 /* For deduction from an init-list we need the actual list. */
23244 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
23245 arg = arg_expr;
23246 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
23247 }
23248
23249 /* for_each_template_parm callback that always returns 0. */
23250
23251 static int
23252 zero_r (tree, void *)
23253 {
23254 return 0;
23255 }
23256
23257 /* for_each_template_parm any_fn callback to handle deduction of a template
23258 type argument from the type of an array bound. */
23259
23260 static int
23261 array_deduction_r (tree t, void *data)
23262 {
23263 tree_pair_p d = (tree_pair_p)data;
23264 tree &tparms = d->purpose;
23265 tree &targs = d->value;
23266
23267 if (TREE_CODE (t) == ARRAY_TYPE)
23268 if (tree dom = TYPE_DOMAIN (t))
23269 if (tree max = TYPE_MAX_VALUE (dom))
23270 {
23271 if (TREE_CODE (max) == MINUS_EXPR)
23272 max = TREE_OPERAND (max, 0);
23273 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
23274 unify (tparms, targs, TREE_TYPE (max), size_type_node,
23275 UNIFY_ALLOW_NONE, /*explain*/false);
23276 }
23277
23278 /* Keep walking. */
23279 return 0;
23280 }
23281
23282 /* Try to deduce any not-yet-deduced template type arguments from the type of
23283 an array bound. This is handled separately from unify because 14.8.2.5 says
23284 "The type of a type parameter is only deduced from an array bound if it is
23285 not otherwise deduced." */
23286
23287 static void
23288 try_array_deduction (tree tparms, tree targs, tree parm)
23289 {
23290 tree_pair_s data = { tparms, targs };
23291 hash_set<tree> visited;
23292 for_each_template_parm (parm, zero_r, &data, &visited,
23293 /*nondeduced*/false, array_deduction_r);
23294 }
23295
23296 /* Most parms like fn_type_unification.
23297
23298 If SUBR is 1, we're being called recursively (to unify the
23299 arguments of a function or method parameter of a function
23300 template).
23301
23302 CHECKS is a pointer to a vector of access checks encountered while
23303 substituting default template arguments. */
23304
23305 static int
23306 type_unification_real (tree tparms,
23307 tree full_targs,
23308 tree xparms,
23309 const tree *xargs,
23310 unsigned int xnargs,
23311 int subr,
23312 unification_kind_t strict,
23313 vec<deferred_access_check, va_gc> **checks,
23314 bool explain_p)
23315 {
23316 tree parm, arg;
23317 int i;
23318 int ntparms = TREE_VEC_LENGTH (tparms);
23319 int saw_undeduced = 0;
23320 tree parms;
23321 const tree *args;
23322 unsigned int nargs;
23323 unsigned int ia;
23324
23325 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
23326 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
23327 gcc_assert (ntparms > 0);
23328
23329 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
23330
23331 /* Reset the number of non-defaulted template arguments contained
23332 in TARGS. */
23333 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
23334
23335 again:
23336 parms = xparms;
23337 args = xargs;
23338 nargs = xnargs;
23339
23340 /* Only fn_type_unification cares about terminal void. */
23341 if (nargs && args[nargs-1] == void_type_node)
23342 --nargs;
23343
23344 ia = 0;
23345 while (parms && parms != void_list_node
23346 && ia < nargs)
23347 {
23348 parm = TREE_VALUE (parms);
23349
23350 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
23351 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
23352 /* For a function parameter pack that occurs at the end of the
23353 parameter-declaration-list, the type A of each remaining
23354 argument of the call is compared with the type P of the
23355 declarator-id of the function parameter pack. */
23356 break;
23357
23358 parms = TREE_CHAIN (parms);
23359
23360 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
23361 /* For a function parameter pack that does not occur at the
23362 end of the parameter-declaration-list, the type of the
23363 parameter pack is a non-deduced context. */
23364 continue;
23365
23366 /* [temp.deduct.conv] only applies to the deduction of the return
23367 type, which is always the first argument here. Other arguments
23368 (notably, explicit object parameters) should undergo normal
23369 call-like unification. */
23370 unification_kind_t kind = strict;
23371 if (strict == DEDUCE_CONV && ia > 0)
23372 kind = DEDUCE_CALL;
23373
23374 arg = args[ia];
23375 ++ia;
23376
23377 if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
23378 explain_p))
23379 return 1;
23380 }
23381
23382 if (parms
23383 && parms != void_list_node
23384 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
23385 {
23386 gcc_assert (strict != DEDUCE_CONV);
23387
23388 /* Unify the remaining arguments with the pack expansion type. */
23389 tree argvec;
23390 tree parmvec = make_tree_vec (1);
23391
23392 /* Allocate a TREE_VEC and copy in all of the arguments */
23393 argvec = make_tree_vec (nargs - ia);
23394 for (i = 0; ia < nargs; ++ia, ++i)
23395 TREE_VEC_ELT (argvec, i) = args[ia];
23396
23397 /* Copy the parameter into parmvec. */
23398 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
23399 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
23400 /*subr=*/subr, explain_p))
23401 return 1;
23402
23403 /* Advance to the end of the list of parameters. */
23404 parms = TREE_CHAIN (parms);
23405 }
23406
23407 /* Fail if we've reached the end of the parm list, and more args
23408 are present, and the parm list isn't variadic. */
23409 if (ia < nargs && parms == void_list_node)
23410 return unify_too_many_arguments (explain_p, nargs, ia);
23411 /* Fail if parms are left and they don't have default values and
23412 they aren't all deduced as empty packs (c++/57397). This is
23413 consistent with sufficient_parms_p. */
23414 if (parms && parms != void_list_node
23415 && TREE_PURPOSE (parms) == NULL_TREE)
23416 {
23417 unsigned int count = nargs;
23418 tree p = parms;
23419 bool type_pack_p;
23420 do
23421 {
23422 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
23423 if (!type_pack_p)
23424 count++;
23425 p = TREE_CHAIN (p);
23426 }
23427 while (p && p != void_list_node);
23428 if (count != nargs)
23429 return unify_too_few_arguments (explain_p, ia, count,
23430 type_pack_p);
23431 }
23432
23433 if (!subr)
23434 {
23435 tsubst_flags_t complain = (explain_p
23436 ? tf_warning_or_error
23437 : tf_none);
23438 bool tried_array_deduction = (cxx_dialect < cxx17);
23439
23440 for (i = 0; i < ntparms; i++)
23441 {
23442 tree targ = TREE_VEC_ELT (targs, i);
23443 tree tparm = TREE_VEC_ELT (tparms, i);
23444
23445 /* Clear the "incomplete" flags on all argument packs now so that
23446 substituting them into later default arguments works. */
23447 if (targ && ARGUMENT_PACK_P (targ))
23448 {
23449 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
23450 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
23451 }
23452
23453 if (targ || tparm == error_mark_node)
23454 continue;
23455 tparm = TREE_VALUE (tparm);
23456
23457 if (TREE_CODE (tparm) == TYPE_DECL
23458 && !tried_array_deduction)
23459 {
23460 try_array_deduction (tparms, targs, xparms);
23461 tried_array_deduction = true;
23462 if (TREE_VEC_ELT (targs, i))
23463 continue;
23464 }
23465
23466 /* If this is an undeduced nontype parameter that depends on
23467 a type parameter, try another pass; its type may have been
23468 deduced from a later argument than the one from which
23469 this parameter can be deduced. */
23470 if (TREE_CODE (tparm) == PARM_DECL
23471 && !is_auto (TREE_TYPE (tparm))
23472 && uses_template_parms (TREE_TYPE (tparm))
23473 && saw_undeduced < 2)
23474 {
23475 saw_undeduced = 1;
23476 continue;
23477 }
23478
23479 /* Core issue #226 (C++0x) [temp.deduct]:
23480
23481 If a template argument has not been deduced, its
23482 default template argument, if any, is used.
23483
23484 When we are in C++98 mode, TREE_PURPOSE will either
23485 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23486 to explicitly check cxx_dialect here. */
23487 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
23488 /* OK, there is a default argument. Wait until after the
23489 conversion check to do substitution. */
23490 continue;
23491
23492 /* If the type parameter is a parameter pack, then it will
23493 be deduced to an empty parameter pack. */
23494 if (template_parameter_pack_p (tparm))
23495 {
23496 tree arg;
23497
23498 if (TREE_CODE (tparm) == PARM_DECL)
23499 {
23500 arg = make_node (NONTYPE_ARGUMENT_PACK);
23501 TREE_CONSTANT (arg) = 1;
23502 }
23503 else
23504 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
23505
23506 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
23507
23508 TREE_VEC_ELT (targs, i) = arg;
23509 continue;
23510 }
23511
23512 return unify_parameter_deduction_failure (explain_p, tparm);
23513 }
23514
23515 /* During partial ordering, we deduce dependent template args. */
23516 bool any_dependent_targs = false;
23517
23518 /* Now substitute into the default template arguments. */
23519 for (i = 0; i < ntparms; i++)
23520 {
23521 tree targ = TREE_VEC_ELT (targs, i);
23522 tree tparm = TREE_VEC_ELT (tparms, i);
23523
23524 if (targ)
23525 {
23526 if (!any_dependent_targs && dependent_template_arg_p (targ))
23527 any_dependent_targs = true;
23528 continue;
23529 }
23530 if (tparm == error_mark_node)
23531 continue;
23532
23533 tree parm = TREE_VALUE (tparm);
23534 tree arg = TREE_PURPOSE (tparm);
23535 reopen_deferring_access_checks (*checks);
23536 location_t save_loc = input_location;
23537 if (DECL_P (parm))
23538 input_location = DECL_SOURCE_LOCATION (parm);
23539
23540 if (saw_undeduced == 1
23541 && TREE_CODE (parm) == PARM_DECL
23542 && !is_auto (TREE_TYPE (parm))
23543 && uses_template_parms (TREE_TYPE (parm)))
23544 {
23545 /* The type of this non-type parameter depends on undeduced
23546 parameters. Don't try to use its default argument yet,
23547 since we might deduce an argument for it on the next pass,
23548 but do check whether the arguments we already have cause
23549 substitution failure, so that that happens before we try
23550 later default arguments (78489). */
23551 ++processing_template_decl;
23552 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
23553 NULL_TREE);
23554 --processing_template_decl;
23555 if (type == error_mark_node)
23556 arg = error_mark_node;
23557 else
23558 arg = NULL_TREE;
23559 }
23560 else
23561 {
23562 /* Even if the call is happening in template context, getting
23563 here means it's non-dependent, and a default argument is
23564 considered a separate definition under [temp.decls], so we can
23565 do this substitution without processing_template_decl. This
23566 is important if the default argument contains something that
23567 might be instantiation-dependent like access (87480). */
23568 processing_template_decl_sentinel s (!any_dependent_targs);
23569 tree substed = NULL_TREE;
23570 if (saw_undeduced == 1 && !any_dependent_targs)
23571 {
23572 /* First instatiate in template context, in case we still
23573 depend on undeduced template parameters. */
23574 ++processing_template_decl;
23575 substed = tsubst_template_arg (arg, full_targs, complain,
23576 NULL_TREE);
23577 --processing_template_decl;
23578 if (substed != error_mark_node
23579 && !uses_template_parms (substed))
23580 /* We replaced all the tparms, substitute again out of
23581 template context. */
23582 substed = NULL_TREE;
23583 }
23584 if (!substed)
23585 substed = tsubst_template_arg (arg, full_targs, complain,
23586 NULL_TREE);
23587
23588 if (!uses_template_parms (substed))
23589 arg = convert_template_argument (parm, substed, full_targs,
23590 complain, i, NULL_TREE);
23591 else if (saw_undeduced == 1)
23592 arg = NULL_TREE;
23593 else if (!any_dependent_targs)
23594 arg = error_mark_node;
23595 }
23596
23597 input_location = save_loc;
23598 *checks = get_deferred_access_checks ();
23599 pop_deferring_access_checks ();
23600
23601 if (arg == error_mark_node)
23602 return 1;
23603 else if (arg)
23604 {
23605 TREE_VEC_ELT (targs, i) = arg;
23606 /* The position of the first default template argument,
23607 is also the number of non-defaulted arguments in TARGS.
23608 Record that. */
23609 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23610 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
23611 }
23612 }
23613
23614 if (saw_undeduced++ == 1)
23615 goto again;
23616 }
23617
23618 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23619 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
23620
23621 return unify_success (explain_p);
23622 }
23623
23624 /* Subroutine of type_unification_real. Args are like the variables
23625 at the call site. ARG is an overloaded function (or template-id);
23626 we try deducing template args from each of the overloads, and if
23627 only one succeeds, we go with that. Modifies TARGS and returns
23628 true on success. */
23629
23630 static bool
23631 resolve_overloaded_unification (tree tparms,
23632 tree targs,
23633 tree parm,
23634 tree arg,
23635 unification_kind_t strict,
23636 int sub_strict,
23637 bool explain_p)
23638 {
23639 tree tempargs = copy_node (targs);
23640 int good = 0;
23641 tree goodfn = NULL_TREE;
23642 bool addr_p;
23643
23644 if (TREE_CODE (arg) == ADDR_EXPR)
23645 {
23646 arg = TREE_OPERAND (arg, 0);
23647 addr_p = true;
23648 }
23649 else
23650 addr_p = false;
23651
23652 if (TREE_CODE (arg) == COMPONENT_REF)
23653 /* Handle `&x' where `x' is some static or non-static member
23654 function name. */
23655 arg = TREE_OPERAND (arg, 1);
23656
23657 if (TREE_CODE (arg) == OFFSET_REF)
23658 arg = TREE_OPERAND (arg, 1);
23659
23660 /* Strip baselink information. */
23661 if (BASELINK_P (arg))
23662 arg = BASELINK_FUNCTIONS (arg);
23663
23664 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
23665 {
23666 /* If we got some explicit template args, we need to plug them into
23667 the affected templates before we try to unify, in case the
23668 explicit args will completely resolve the templates in question. */
23669
23670 int ok = 0;
23671 tree expl_subargs = TREE_OPERAND (arg, 1);
23672 arg = TREE_OPERAND (arg, 0);
23673
23674 for (lkp_iterator iter (arg); iter; ++iter)
23675 {
23676 tree fn = *iter;
23677 tree subargs, elem;
23678
23679 if (TREE_CODE (fn) != TEMPLATE_DECL)
23680 continue;
23681
23682 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23683 expl_subargs, NULL_TREE, tf_none);
23684 if (subargs != error_mark_node
23685 && !any_dependent_template_arguments_p (subargs))
23686 {
23687 fn = instantiate_template (fn, subargs, tf_none);
23688 if (!constraints_satisfied_p (fn))
23689 continue;
23690 if (undeduced_auto_decl (fn))
23691 {
23692 /* Instantiate the function to deduce its return type. */
23693 ++function_depth;
23694 instantiate_decl (fn, /*defer*/false, /*class*/false);
23695 --function_depth;
23696 }
23697
23698 if (flag_noexcept_type)
23699 maybe_instantiate_noexcept (fn, tf_none);
23700
23701 elem = TREE_TYPE (fn);
23702 if (try_one_overload (tparms, targs, tempargs, parm,
23703 elem, strict, sub_strict, addr_p, explain_p)
23704 && (!goodfn || !same_type_p (goodfn, elem)))
23705 {
23706 goodfn = elem;
23707 ++good;
23708 }
23709 }
23710 else if (subargs)
23711 ++ok;
23712 }
23713 /* If no templates (or more than one) are fully resolved by the
23714 explicit arguments, this template-id is a non-deduced context; it
23715 could still be OK if we deduce all template arguments for the
23716 enclosing call through other arguments. */
23717 if (good != 1)
23718 good = ok;
23719 }
23720 else if (!OVL_P (arg))
23721 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23722 -- but the deduction does not succeed because the expression is
23723 not just the function on its own. */
23724 return false;
23725 else
23726 for (lkp_iterator iter (arg); iter; ++iter)
23727 {
23728 tree fn = *iter;
23729 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
23730 strict, sub_strict, addr_p, explain_p)
23731 && (!goodfn || !decls_match (goodfn, fn)))
23732 {
23733 goodfn = fn;
23734 ++good;
23735 }
23736 }
23737
23738 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23739 to function or pointer to member function argument if the set of
23740 overloaded functions does not contain function templates and at most
23741 one of a set of overloaded functions provides a unique match.
23742
23743 So if we found multiple possibilities, we return success but don't
23744 deduce anything. */
23745
23746 if (good == 1)
23747 {
23748 int i = TREE_VEC_LENGTH (targs);
23749 for (; i--; )
23750 if (TREE_VEC_ELT (tempargs, i))
23751 {
23752 tree old = TREE_VEC_ELT (targs, i);
23753 tree new_ = TREE_VEC_ELT (tempargs, i);
23754 if (new_ && old && ARGUMENT_PACK_P (old)
23755 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
23756 /* Don't forget explicit template arguments in a pack. */
23757 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
23758 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
23759 TREE_VEC_ELT (targs, i) = new_;
23760 }
23761 }
23762 if (good)
23763 return true;
23764
23765 return false;
23766 }
23767
23768 /* Core DR 115: In contexts where deduction is done and fails, or in
23769 contexts where deduction is not done, if a template argument list is
23770 specified and it, along with any default template arguments, identifies
23771 a single function template specialization, then the template-id is an
23772 lvalue for the function template specialization. */
23773
23774 tree
23775 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
23776 {
23777 tree expr, offset, baselink;
23778 bool addr;
23779
23780 if (!type_unknown_p (orig_expr))
23781 return orig_expr;
23782
23783 expr = orig_expr;
23784 addr = false;
23785 offset = NULL_TREE;
23786 baselink = NULL_TREE;
23787
23788 if (TREE_CODE (expr) == ADDR_EXPR)
23789 {
23790 expr = TREE_OPERAND (expr, 0);
23791 addr = true;
23792 }
23793 if (TREE_CODE (expr) == OFFSET_REF)
23794 {
23795 offset = expr;
23796 expr = TREE_OPERAND (expr, 1);
23797 }
23798 if (BASELINK_P (expr))
23799 {
23800 baselink = expr;
23801 expr = BASELINK_FUNCTIONS (expr);
23802 }
23803
23804 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
23805 {
23806 int good = 0;
23807 tree goodfn = NULL_TREE;
23808
23809 /* If we got some explicit template args, we need to plug them into
23810 the affected templates before we try to unify, in case the
23811 explicit args will completely resolve the templates in question. */
23812
23813 tree expl_subargs = TREE_OPERAND (expr, 1);
23814 tree arg = TREE_OPERAND (expr, 0);
23815 tree badfn = NULL_TREE;
23816 tree badargs = NULL_TREE;
23817
23818 for (lkp_iterator iter (arg); iter; ++iter)
23819 {
23820 tree fn = *iter;
23821 tree subargs, elem;
23822
23823 if (TREE_CODE (fn) != TEMPLATE_DECL)
23824 continue;
23825
23826 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23827 expl_subargs, NULL_TREE, tf_none);
23828 if (subargs != error_mark_node
23829 && !any_dependent_template_arguments_p (subargs))
23830 {
23831 elem = instantiate_template (fn, subargs, tf_none);
23832 if (elem == error_mark_node)
23833 {
23834 badfn = fn;
23835 badargs = subargs;
23836 }
23837 else if (elem && (!goodfn || !decls_match (goodfn, elem))
23838 && constraints_satisfied_p (elem))
23839 {
23840 goodfn = elem;
23841 ++good;
23842 }
23843 }
23844 }
23845 if (good == 1)
23846 {
23847 mark_used (goodfn);
23848 expr = goodfn;
23849 if (baselink)
23850 expr = build_baselink (BASELINK_BINFO (baselink),
23851 BASELINK_ACCESS_BINFO (baselink),
23852 expr, BASELINK_OPTYPE (baselink));
23853 if (offset)
23854 {
23855 tree base
23856 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
23857 expr = build_offset_ref (base, expr, addr, complain);
23858 }
23859 if (addr)
23860 expr = cp_build_addr_expr (expr, complain);
23861 return expr;
23862 }
23863 else if (good == 0 && badargs && (complain & tf_error))
23864 /* There were no good options and at least one bad one, so let the
23865 user know what the problem is. */
23866 instantiate_template (badfn, badargs, complain);
23867 }
23868 return orig_expr;
23869 }
23870
23871 /* As above, but error out if the expression remains overloaded. */
23872
23873 tree
23874 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
23875 {
23876 exp = resolve_nondeduced_context (exp, complain);
23877 if (type_unknown_p (exp))
23878 {
23879 if (complain & tf_error)
23880 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
23881 return error_mark_node;
23882 }
23883 return exp;
23884 }
23885
23886 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23887 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23888 different overloads deduce different arguments for a given parm.
23889 ADDR_P is true if the expression for which deduction is being
23890 performed was of the form "& fn" rather than simply "fn".
23891
23892 Returns 1 on success. */
23893
23894 static int
23895 try_one_overload (tree tparms,
23896 tree orig_targs,
23897 tree targs,
23898 tree parm,
23899 tree arg,
23900 unification_kind_t strict,
23901 int sub_strict,
23902 bool addr_p,
23903 bool explain_p)
23904 {
23905 int nargs;
23906 tree tempargs;
23907 int i;
23908
23909 if (arg == error_mark_node)
23910 return 0;
23911
23912 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23913 to function or pointer to member function argument if the set of
23914 overloaded functions does not contain function templates and at most
23915 one of a set of overloaded functions provides a unique match.
23916
23917 So if this is a template, just return success. */
23918
23919 if (uses_template_parms (arg))
23920 return 1;
23921
23922 if (TREE_CODE (arg) == METHOD_TYPE)
23923 arg = build_ptrmemfunc_type (build_pointer_type (arg));
23924 else if (addr_p)
23925 arg = build_pointer_type (arg);
23926
23927 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23928 &parm, &arg, NULL_TREE);
23929
23930 /* We don't copy orig_targs for this because if we have already deduced
23931 some template args from previous args, unify would complain when we
23932 try to deduce a template parameter for the same argument, even though
23933 there isn't really a conflict. */
23934 nargs = TREE_VEC_LENGTH (targs);
23935 tempargs = make_tree_vec (nargs);
23936
23937 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
23938 return 0;
23939
23940 /* First make sure we didn't deduce anything that conflicts with
23941 explicitly specified args. */
23942 for (i = nargs; i--; )
23943 {
23944 tree elt = TREE_VEC_ELT (tempargs, i);
23945 tree oldelt = TREE_VEC_ELT (orig_targs, i);
23946
23947 if (!elt)
23948 /*NOP*/;
23949 else if (uses_template_parms (elt))
23950 /* Since we're unifying against ourselves, we will fill in
23951 template args used in the function parm list with our own
23952 template parms. Discard them. */
23953 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
23954 else if (oldelt && ARGUMENT_PACK_P (oldelt))
23955 {
23956 /* Check that the argument at each index of the deduced argument pack
23957 is equivalent to the corresponding explicitly specified argument.
23958 We may have deduced more arguments than were explicitly specified,
23959 and that's OK. */
23960
23961 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23962 that's wrong if we deduce the same argument pack from multiple
23963 function arguments: it's only incomplete the first time. */
23964
23965 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
23966 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
23967
23968 if (TREE_VEC_LENGTH (deduced_pack)
23969 < TREE_VEC_LENGTH (explicit_pack))
23970 return 0;
23971
23972 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
23973 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
23974 TREE_VEC_ELT (deduced_pack, j)))
23975 return 0;
23976 }
23977 else if (oldelt && !template_args_equal (oldelt, elt))
23978 return 0;
23979 }
23980
23981 for (i = nargs; i--; )
23982 {
23983 tree elt = TREE_VEC_ELT (tempargs, i);
23984
23985 if (elt)
23986 TREE_VEC_ELT (targs, i) = elt;
23987 }
23988
23989 return 1;
23990 }
23991
23992 /* PARM is a template class (perhaps with unbound template
23993 parameters). ARG is a fully instantiated type. If ARG can be
23994 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23995 TARGS are as for unify. */
23996
23997 static tree
23998 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
23999 bool explain_p)
24000 {
24001 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24002 return NULL_TREE;
24003 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24004 /* Matches anything. */;
24005 else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
24006 return NULL_TREE;
24007
24008 /* We need to make a new template argument vector for the call to
24009 unify. If we used TARGS, we'd clutter it up with the result of
24010 the attempted unification, even if this class didn't work out.
24011 We also don't want to commit ourselves to all the unifications
24012 we've already done, since unification is supposed to be done on
24013 an argument-by-argument basis. In other words, consider the
24014 following pathological case:
24015
24016 template <int I, int J, int K>
24017 struct S {};
24018
24019 template <int I, int J>
24020 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
24021
24022 template <int I, int J, int K>
24023 void f(S<I, J, K>, S<I, I, I>);
24024
24025 void g() {
24026 S<0, 0, 0> s0;
24027 S<0, 1, 2> s2;
24028
24029 f(s0, s2);
24030 }
24031
24032 Now, by the time we consider the unification involving `s2', we
24033 already know that we must have `f<0, 0, 0>'. But, even though
24034 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
24035 because there are two ways to unify base classes of S<0, 1, 2>
24036 with S<I, I, I>. If we kept the already deduced knowledge, we
24037 would reject the possibility I=1. */
24038 targs = copy_template_args (targs);
24039 for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
24040 targ = NULL_TREE;
24041
24042 int err;
24043 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24044 err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
24045 else
24046 err = unify (tparms, targs,
24047 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
24048 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
24049 UNIFY_ALLOW_NONE, explain_p);
24050
24051 return err ? NULL_TREE : arg;
24052 }
24053
24054 /* Given a template type PARM and a class type ARG, find the unique
24055 base type in ARG that is an instance of PARM. We do not examine
24056 ARG itself; only its base-classes. If there is not exactly one
24057 appropriate base class, return NULL_TREE. PARM may be the type of
24058 a partial specialization, as well as a plain template type. Used
24059 by unify. */
24060
24061 static enum template_base_result
24062 get_template_base (tree tparms, tree targs, tree parm, tree arg,
24063 bool explain_p, tree *result)
24064 {
24065 tree rval = NULL_TREE;
24066 tree binfo;
24067
24068 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
24069
24070 binfo = TYPE_BINFO (complete_type (arg));
24071 if (!binfo)
24072 {
24073 /* The type could not be completed. */
24074 *result = NULL_TREE;
24075 return tbr_incomplete_type;
24076 }
24077
24078 /* Walk in inheritance graph order. The search order is not
24079 important, and this avoids multiple walks of virtual bases. */
24080 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
24081 {
24082 tree r = try_class_unification (tparms, targs, parm,
24083 BINFO_TYPE (binfo), explain_p);
24084
24085 if (r)
24086 {
24087 /* If there is more than one satisfactory baseclass, then:
24088
24089 [temp.deduct.call]
24090
24091 If they yield more than one possible deduced A, the type
24092 deduction fails.
24093
24094 applies. */
24095 if (rval && !same_type_p (r, rval))
24096 {
24097 /* [temp.deduct.call]/4.3: If there is a class C that is a
24098 (direct or indirect) base class of D and derived (directly or
24099 indirectly) from a class B and that would be a valid deduced
24100 A, the deduced A cannot be B or pointer to B, respectively. */
24101 if (DERIVED_FROM_P (r, rval))
24102 /* Ignore r. */
24103 continue;
24104 else if (DERIVED_FROM_P (rval, r))
24105 /* Ignore rval. */;
24106 else
24107 {
24108 *result = NULL_TREE;
24109 return tbr_ambiguous_baseclass;
24110 }
24111 }
24112
24113 rval = r;
24114 }
24115 }
24116
24117 *result = rval;
24118 return tbr_success;
24119 }
24120
24121 /* Returns the level of DECL, which declares a template parameter. */
24122
24123 static int
24124 template_decl_level (tree decl)
24125 {
24126 switch (TREE_CODE (decl))
24127 {
24128 case TYPE_DECL:
24129 case TEMPLATE_DECL:
24130 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
24131
24132 case PARM_DECL:
24133 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
24134
24135 default:
24136 gcc_unreachable ();
24137 }
24138 return 0;
24139 }
24140
24141 /* Decide whether ARG can be unified with PARM, considering only the
24142 cv-qualifiers of each type, given STRICT as documented for unify.
24143 Returns nonzero iff the unification is OK on that basis. */
24144
24145 static int
24146 check_cv_quals_for_unify (int strict, tree arg, tree parm)
24147 {
24148 int arg_quals = cp_type_quals (arg);
24149 int parm_quals = cp_type_quals (parm);
24150
24151 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24152 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24153 {
24154 /* Although a CVR qualifier is ignored when being applied to a
24155 substituted template parameter ([8.3.2]/1 for example), that
24156 does not allow us to unify "const T" with "int&" because both
24157 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
24158 It is ok when we're allowing additional CV qualifiers
24159 at the outer level [14.8.2.1]/3,1st bullet. */
24160 if ((TYPE_REF_P (arg)
24161 || FUNC_OR_METHOD_TYPE_P (arg))
24162 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
24163 return 0;
24164
24165 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
24166 && (parm_quals & TYPE_QUAL_RESTRICT))
24167 return 0;
24168 }
24169
24170 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
24171 && (arg_quals & parm_quals) != parm_quals)
24172 return 0;
24173
24174 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
24175 && (parm_quals & arg_quals) != arg_quals)
24176 return 0;
24177
24178 return 1;
24179 }
24180
24181 /* Determines the LEVEL and INDEX for the template parameter PARM. */
24182 void
24183 template_parm_level_and_index (tree parm, int* level, int* index)
24184 {
24185 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24186 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24187 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24188 {
24189 *index = TEMPLATE_TYPE_IDX (parm);
24190 *level = TEMPLATE_TYPE_LEVEL (parm);
24191 }
24192 else
24193 {
24194 *index = TEMPLATE_PARM_IDX (parm);
24195 *level = TEMPLATE_PARM_LEVEL (parm);
24196 }
24197 }
24198
24199 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
24200 do { \
24201 if (unify (TP, TA, P, A, S, EP)) \
24202 return 1; \
24203 } while (0)
24204
24205 /* Unifies the remaining arguments in PACKED_ARGS with the pack
24206 expansion at the end of PACKED_PARMS. Returns 0 if the type
24207 deduction succeeds, 1 otherwise. STRICT is the same as in
24208 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
24209 function call argument list. We'll need to adjust the arguments to make them
24210 types. SUBR tells us if this is from a recursive call to
24211 type_unification_real, or for comparing two template argument
24212 lists. */
24213
24214 static int
24215 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
24216 tree packed_args, unification_kind_t strict,
24217 bool subr, bool explain_p)
24218 {
24219 tree parm
24220 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
24221 tree pattern = PACK_EXPANSION_PATTERN (parm);
24222 tree pack, packs = NULL_TREE;
24223 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
24224
24225 /* Add in any args remembered from an earlier partial instantiation. */
24226 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
24227 int levels = TMPL_ARGS_DEPTH (targs);
24228
24229 packed_args = expand_template_argument_pack (packed_args);
24230
24231 int len = TREE_VEC_LENGTH (packed_args);
24232
24233 /* Determine the parameter packs we will be deducing from the
24234 pattern, and record their current deductions. */
24235 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
24236 pack; pack = TREE_CHAIN (pack))
24237 {
24238 tree parm_pack = TREE_VALUE (pack);
24239 int idx, level;
24240
24241 /* Only template parameter packs can be deduced, not e.g. function
24242 parameter packs or __bases or __integer_pack. */
24243 if (!TEMPLATE_PARM_P (parm_pack))
24244 continue;
24245
24246 /* Determine the index and level of this parameter pack. */
24247 template_parm_level_and_index (parm_pack, &level, &idx);
24248 if (level > levels)
24249 continue;
24250
24251 /* Keep track of the parameter packs and their corresponding
24252 argument packs. */
24253 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
24254 TREE_TYPE (packs) = make_tree_vec (len - start);
24255 }
24256
24257 /* Loop through all of the arguments that have not yet been
24258 unified and unify each with the pattern. */
24259 for (i = start; i < len; i++)
24260 {
24261 tree parm;
24262 bool any_explicit = false;
24263 tree arg = TREE_VEC_ELT (packed_args, i);
24264
24265 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
24266 or the element of its argument pack at the current index if
24267 this argument was explicitly specified. */
24268 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24269 {
24270 int idx, level;
24271 tree arg, pargs;
24272 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
24273
24274 arg = NULL_TREE;
24275 if (TREE_VALUE (pack)
24276 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
24277 && (i - start < TREE_VEC_LENGTH (pargs)))
24278 {
24279 any_explicit = true;
24280 arg = TREE_VEC_ELT (pargs, i - start);
24281 }
24282 TMPL_ARG (targs, level, idx) = arg;
24283 }
24284
24285 /* If we had explicit template arguments, substitute them into the
24286 pattern before deduction. */
24287 if (any_explicit)
24288 {
24289 /* Some arguments might still be unspecified or dependent. */
24290 bool dependent;
24291 ++processing_template_decl;
24292 dependent = any_dependent_template_arguments_p (targs);
24293 if (!dependent)
24294 --processing_template_decl;
24295 parm = tsubst (pattern, targs,
24296 explain_p ? tf_warning_or_error : tf_none,
24297 NULL_TREE);
24298 if (dependent)
24299 --processing_template_decl;
24300 if (parm == error_mark_node)
24301 return 1;
24302 }
24303 else
24304 parm = pattern;
24305
24306 /* Unify the pattern with the current argument. */
24307 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
24308 explain_p))
24309 return 1;
24310
24311 /* For each parameter pack, collect the deduced value. */
24312 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24313 {
24314 int idx, level;
24315 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
24316
24317 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
24318 TMPL_ARG (targs, level, idx);
24319 }
24320 }
24321
24322 /* Verify that the results of unification with the parameter packs
24323 produce results consistent with what we've seen before, and make
24324 the deduced argument packs available. */
24325 for (pack = packs; pack; pack = TREE_CHAIN (pack))
24326 {
24327 tree old_pack = TREE_VALUE (pack);
24328 tree new_args = TREE_TYPE (pack);
24329 int i, len = TREE_VEC_LENGTH (new_args);
24330 int idx, level;
24331 bool nondeduced_p = false;
24332
24333 /* By default keep the original deduced argument pack.
24334 If necessary, more specific code is going to update the
24335 resulting deduced argument later down in this function. */
24336 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
24337 TMPL_ARG (targs, level, idx) = old_pack;
24338
24339 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24340 actually deduce anything. */
24341 for (i = 0; i < len && !nondeduced_p; ++i)
24342 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
24343 nondeduced_p = true;
24344 if (nondeduced_p)
24345 continue;
24346
24347 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
24348 {
24349 /* If we had fewer function args than explicit template args,
24350 just use the explicits. */
24351 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24352 int explicit_len = TREE_VEC_LENGTH (explicit_args);
24353 if (len < explicit_len)
24354 new_args = explicit_args;
24355 }
24356
24357 if (!old_pack)
24358 {
24359 tree result;
24360 /* Build the deduced *_ARGUMENT_PACK. */
24361 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
24362 {
24363 result = make_node (NONTYPE_ARGUMENT_PACK);
24364 TREE_CONSTANT (result) = 1;
24365 }
24366 else
24367 result = cxx_make_type (TYPE_ARGUMENT_PACK);
24368
24369 ARGUMENT_PACK_ARGS (result) = new_args;
24370
24371 /* Note the deduced argument packs for this parameter
24372 pack. */
24373 TMPL_ARG (targs, level, idx) = result;
24374 }
24375 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
24376 && (ARGUMENT_PACK_ARGS (old_pack)
24377 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
24378 {
24379 /* We only had the explicitly-provided arguments before, but
24380 now we have a complete set of arguments. */
24381 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24382
24383 ARGUMENT_PACK_ARGS (old_pack) = new_args;
24384 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
24385 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
24386 }
24387 else
24388 {
24389 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
24390 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
24391 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
24392 /* During template argument deduction for the aggregate deduction
24393 candidate, the number of elements in a trailing parameter pack
24394 is only deduced from the number of remaining function
24395 arguments if it is not otherwise deduced. */
24396 if (cxx_dialect >= cxx20
24397 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
24398 /* FIXME This isn't set properly for partial instantiations. */
24399 && TPARMS_PRIMARY_TEMPLATE (tparms)
24400 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
24401 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
24402 if (!comp_template_args (old_args, new_args,
24403 &bad_old_arg, &bad_new_arg))
24404 /* Inconsistent unification of this parameter pack. */
24405 return unify_parameter_pack_inconsistent (explain_p,
24406 bad_old_arg,
24407 bad_new_arg);
24408 }
24409 }
24410
24411 return unify_success (explain_p);
24412 }
24413
24414 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24415 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24416 parameters and return value are as for unify. */
24417
24418 static int
24419 unify_array_domain (tree tparms, tree targs,
24420 tree parm_dom, tree arg_dom,
24421 bool explain_p)
24422 {
24423 tree parm_max;
24424 tree arg_max;
24425 bool parm_cst;
24426 bool arg_cst;
24427
24428 /* Our representation of array types uses "N - 1" as the
24429 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24430 not an integer constant. We cannot unify arbitrarily
24431 complex expressions, so we eliminate the MINUS_EXPRs
24432 here. */
24433 parm_max = TYPE_MAX_VALUE (parm_dom);
24434 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
24435 if (!parm_cst)
24436 {
24437 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
24438 parm_max = TREE_OPERAND (parm_max, 0);
24439 }
24440 arg_max = TYPE_MAX_VALUE (arg_dom);
24441 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
24442 if (!arg_cst)
24443 {
24444 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24445 trying to unify the type of a variable with the type
24446 of a template parameter. For example:
24447
24448 template <unsigned int N>
24449 void f (char (&) [N]);
24450 int g();
24451 void h(int i) {
24452 char a[g(i)];
24453 f(a);
24454 }
24455
24456 Here, the type of the ARG will be "int [g(i)]", and
24457 may be a SAVE_EXPR, etc. */
24458 if (TREE_CODE (arg_max) != MINUS_EXPR)
24459 return unify_vla_arg (explain_p, arg_dom);
24460 arg_max = TREE_OPERAND (arg_max, 0);
24461 }
24462
24463 /* If only one of the bounds used a MINUS_EXPR, compensate
24464 by adding one to the other bound. */
24465 if (parm_cst && !arg_cst)
24466 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
24467 integer_type_node,
24468 parm_max,
24469 integer_one_node);
24470 else if (arg_cst && !parm_cst)
24471 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
24472 integer_type_node,
24473 arg_max,
24474 integer_one_node);
24475
24476 return unify (tparms, targs, parm_max, arg_max,
24477 UNIFY_ALLOW_INTEGER, explain_p);
24478 }
24479
24480 /* Returns whether T, a P or A in unify, is a type, template or expression. */
24481
24482 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
24483
24484 static pa_kind_t
24485 pa_kind (tree t)
24486 {
24487 if (PACK_EXPANSION_P (t))
24488 t = PACK_EXPANSION_PATTERN (t);
24489 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
24490 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
24491 || DECL_TYPE_TEMPLATE_P (t))
24492 return pa_tmpl;
24493 else if (TYPE_P (t))
24494 return pa_type;
24495 else
24496 return pa_expr;
24497 }
24498
24499 /* Deduce the value of template parameters. TPARMS is the (innermost)
24500 set of template parameters to a template. TARGS is the bindings
24501 for those template parameters, as determined thus far; TARGS may
24502 include template arguments for outer levels of template parameters
24503 as well. PARM is a parameter to a template function, or a
24504 subcomponent of that parameter; ARG is the corresponding argument.
24505 This function attempts to match PARM with ARG in a manner
24506 consistent with the existing assignments in TARGS. If more values
24507 are deduced, then TARGS is updated.
24508
24509 Returns 0 if the type deduction succeeds, 1 otherwise. The
24510 parameter STRICT is a bitwise or of the following flags:
24511
24512 UNIFY_ALLOW_NONE:
24513 Require an exact match between PARM and ARG.
24514 UNIFY_ALLOW_MORE_CV_QUAL:
24515 Allow the deduced ARG to be more cv-qualified (by qualification
24516 conversion) than ARG.
24517 UNIFY_ALLOW_LESS_CV_QUAL:
24518 Allow the deduced ARG to be less cv-qualified than ARG.
24519 UNIFY_ALLOW_DERIVED:
24520 Allow the deduced ARG to be a template base class of ARG,
24521 or a pointer to a template base class of the type pointed to by
24522 ARG.
24523 UNIFY_ALLOW_INTEGER:
24524 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24525 case for more information.
24526 UNIFY_ALLOW_OUTER_LEVEL:
24527 This is the outermost level of a deduction. Used to determine validity
24528 of qualification conversions. A valid qualification conversion must
24529 have const qualified pointers leading up to the inner type which
24530 requires additional CV quals, except at the outer level, where const
24531 is not required [conv.qual]. It would be normal to set this flag in
24532 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24533 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24534 This is the outermost level of a deduction, and PARM can be more CV
24535 qualified at this point.
24536 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24537 This is the outermost level of a deduction, and PARM can be less CV
24538 qualified at this point. */
24539
24540 static int
24541 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
24542 bool explain_p)
24543 {
24544 int idx;
24545 tree targ;
24546 tree tparm;
24547 int strict_in = strict;
24548 tsubst_flags_t complain = (explain_p
24549 ? tf_warning_or_error
24550 : tf_none);
24551
24552 /* I don't think this will do the right thing with respect to types.
24553 But the only case I've seen it in so far has been array bounds, where
24554 signedness is the only information lost, and I think that will be
24555 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24556 finish_id_expression_1, and are also OK. */
24557 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
24558 parm = TREE_OPERAND (parm, 0);
24559
24560 if (arg == error_mark_node)
24561 return unify_invalid (explain_p);
24562 if (arg == unknown_type_node
24563 || arg == init_list_type_node)
24564 /* We can't deduce anything from this, but we might get all the
24565 template args from other function args. */
24566 return unify_success (explain_p);
24567
24568 if (parm == any_targ_node || arg == any_targ_node)
24569 return unify_success (explain_p);
24570
24571 /* If PARM uses template parameters, then we can't bail out here,
24572 even if ARG == PARM, since we won't record unifications for the
24573 template parameters. We might need them if we're trying to
24574 figure out which of two things is more specialized. */
24575 if (arg == parm
24576 && (DECL_P (parm) || !uses_template_parms (parm)))
24577 return unify_success (explain_p);
24578
24579 /* Handle init lists early, so the rest of the function can assume
24580 we're dealing with a type. */
24581 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
24582 {
24583 tree elttype;
24584 tree orig_parm = parm;
24585
24586 if (!is_std_init_list (parm)
24587 && TREE_CODE (parm) != ARRAY_TYPE)
24588 /* We can only deduce from an initializer list argument if the
24589 parameter is std::initializer_list or an array; otherwise this
24590 is a non-deduced context. */
24591 return unify_success (explain_p);
24592
24593 if (TREE_CODE (parm) == ARRAY_TYPE)
24594 elttype = TREE_TYPE (parm);
24595 else
24596 {
24597 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
24598 /* Deduction is defined in terms of a single type, so just punt
24599 on the (bizarre) std::initializer_list<T...>. */
24600 if (PACK_EXPANSION_P (elttype))
24601 return unify_success (explain_p);
24602 }
24603
24604 if (strict != DEDUCE_EXACT
24605 && TYPE_P (elttype)
24606 && !uses_deducible_template_parms (elttype))
24607 /* If ELTTYPE has no deducible template parms, skip deduction from
24608 the list elements. */;
24609 else
24610 for (auto &e: CONSTRUCTOR_ELTS (arg))
24611 {
24612 tree elt = e.value;
24613 int elt_strict = strict;
24614
24615 if (elt == error_mark_node)
24616 return unify_invalid (explain_p);
24617
24618 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
24619 {
24620 tree type = TREE_TYPE (elt);
24621 if (type == error_mark_node)
24622 return unify_invalid (explain_p);
24623 /* It should only be possible to get here for a call. */
24624 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
24625 elt_strict |= maybe_adjust_types_for_deduction
24626 (tparms, DEDUCE_CALL, &elttype, &type, elt);
24627 elt = type;
24628 }
24629
24630 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
24631 explain_p);
24632 }
24633
24634 if (TREE_CODE (parm) == ARRAY_TYPE
24635 && deducible_array_bound (TYPE_DOMAIN (parm)))
24636 {
24637 /* Also deduce from the length of the initializer list. */
24638 tree max = size_int (CONSTRUCTOR_NELTS (arg));
24639 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
24640 if (idx == error_mark_node)
24641 return unify_invalid (explain_p);
24642 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
24643 idx, explain_p);
24644 }
24645
24646 /* If the std::initializer_list<T> deduction worked, replace the
24647 deduced A with std::initializer_list<A>. */
24648 if (orig_parm != parm)
24649 {
24650 idx = TEMPLATE_TYPE_IDX (orig_parm);
24651 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24652 targ = listify (targ);
24653 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
24654 }
24655 return unify_success (explain_p);
24656 }
24657
24658 /* If parm and arg aren't the same kind of thing (template, type, or
24659 expression), fail early. */
24660 if (pa_kind (parm) != pa_kind (arg))
24661 return unify_invalid (explain_p);
24662
24663 /* Immediately reject some pairs that won't unify because of
24664 cv-qualification mismatches. */
24665 if (TREE_CODE (arg) == TREE_CODE (parm)
24666 && TYPE_P (arg)
24667 /* It is the elements of the array which hold the cv quals of an array
24668 type, and the elements might be template type parms. We'll check
24669 when we recurse. */
24670 && TREE_CODE (arg) != ARRAY_TYPE
24671 /* We check the cv-qualifiers when unifying with template type
24672 parameters below. We want to allow ARG `const T' to unify with
24673 PARM `T' for example, when computing which of two templates
24674 is more specialized, for example. */
24675 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
24676 && !check_cv_quals_for_unify (strict_in, arg, parm))
24677 return unify_cv_qual_mismatch (explain_p, parm, arg);
24678
24679 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
24680 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
24681 && !FUNC_OR_METHOD_TYPE_P (parm))
24682 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
24683 /* PMFs recurse at the same level, so don't strip this yet. */
24684 if (!TYPE_PTRMEMFUNC_P (parm))
24685 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
24686 strict &= ~UNIFY_ALLOW_DERIVED;
24687 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
24688 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
24689
24690 switch (TREE_CODE (parm))
24691 {
24692 case TYPENAME_TYPE:
24693 case SCOPE_REF:
24694 case UNBOUND_CLASS_TEMPLATE:
24695 /* In a type which contains a nested-name-specifier, template
24696 argument values cannot be deduced for template parameters used
24697 within the nested-name-specifier. */
24698 return unify_success (explain_p);
24699
24700 case TEMPLATE_TYPE_PARM:
24701 case TEMPLATE_TEMPLATE_PARM:
24702 case BOUND_TEMPLATE_TEMPLATE_PARM:
24703 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24704 if (error_operand_p (tparm))
24705 return unify_invalid (explain_p);
24706
24707 if (TEMPLATE_TYPE_LEVEL (parm)
24708 != template_decl_level (tparm))
24709 /* The PARM is not one we're trying to unify. Just check
24710 to see if it matches ARG. */
24711 {
24712 if (TREE_CODE (arg) == TREE_CODE (parm)
24713 && (is_auto (parm) ? is_auto (arg)
24714 : same_type_p (parm, arg)))
24715 return unify_success (explain_p);
24716 else
24717 return unify_type_mismatch (explain_p, parm, arg);
24718 }
24719 idx = TEMPLATE_TYPE_IDX (parm);
24720 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24721 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
24722 if (error_operand_p (tparm))
24723 return unify_invalid (explain_p);
24724
24725 /* Check for mixed types and values. */
24726 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24727 && TREE_CODE (tparm) != TYPE_DECL)
24728 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24729 && TREE_CODE (tparm) != TEMPLATE_DECL))
24730 gcc_unreachable ();
24731
24732 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24733 {
24734 if ((strict_in & UNIFY_ALLOW_DERIVED)
24735 && CLASS_TYPE_P (arg))
24736 {
24737 /* First try to match ARG directly. */
24738 tree t = try_class_unification (tparms, targs, parm, arg,
24739 explain_p);
24740 if (!t)
24741 {
24742 /* Otherwise, look for a suitable base of ARG, as below. */
24743 enum template_base_result r;
24744 r = get_template_base (tparms, targs, parm, arg,
24745 explain_p, &t);
24746 if (!t)
24747 return unify_no_common_base (explain_p, r, parm, arg);
24748 arg = t;
24749 }
24750 }
24751 /* ARG must be constructed from a template class or a template
24752 template parameter. */
24753 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
24754 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24755 return unify_template_deduction_failure (explain_p, parm, arg);
24756
24757 /* Deduce arguments T, i from TT<T> or TT<i>. */
24758 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
24759 return 1;
24760
24761 arg = TYPE_TI_TEMPLATE (arg);
24762 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
24763 /* If the template is a template template parameter, use the
24764 TEMPLATE_TEMPLATE_PARM for matching. */
24765 arg = TREE_TYPE (arg);
24766
24767 /* Fall through to deduce template name. */
24768 }
24769
24770 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24771 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24772 {
24773 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24774
24775 /* Simple cases: Value already set, does match or doesn't. */
24776 if (targ != NULL_TREE && template_args_equal (targ, arg))
24777 return unify_success (explain_p);
24778 else if (targ)
24779 return unify_inconsistency (explain_p, parm, targ, arg);
24780 }
24781 else
24782 {
24783 /* If PARM is `const T' and ARG is only `int', we don't have
24784 a match unless we are allowing additional qualification.
24785 If ARG is `const int' and PARM is just `T' that's OK;
24786 that binds `const int' to `T'. */
24787 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
24788 arg, parm))
24789 return unify_cv_qual_mismatch (explain_p, parm, arg);
24790
24791 /* Consider the case where ARG is `const volatile int' and
24792 PARM is `const T'. Then, T should be `volatile int'. */
24793 arg = cp_build_qualified_type
24794 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
24795 if (arg == error_mark_node)
24796 return unify_invalid (explain_p);
24797
24798 /* Simple cases: Value already set, does match or doesn't. */
24799 if (targ != NULL_TREE && same_type_p (targ, arg))
24800 return unify_success (explain_p);
24801 else if (targ)
24802 return unify_inconsistency (explain_p, parm, targ, arg);
24803
24804 /* Make sure that ARG is not a variable-sized array. (Note
24805 that were talking about variable-sized arrays (like
24806 `int[n]'), rather than arrays of unknown size (like
24807 `int[]').) We'll get very confused by such a type since
24808 the bound of the array is not constant, and therefore
24809 not mangleable. Besides, such types are not allowed in
24810 ISO C++, so we can do as we please here. We do allow
24811 them for 'auto' deduction, since that isn't ABI-exposed. */
24812 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
24813 return unify_vla_arg (explain_p, arg);
24814
24815 /* Strip typedefs as in convert_template_argument. */
24816 arg = canonicalize_type_argument (arg, tf_none);
24817 }
24818
24819 /* If ARG is a parameter pack or an expansion, we cannot unify
24820 against it unless PARM is also a parameter pack. */
24821 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24822 && !template_parameter_pack_p (parm))
24823 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24824
24825 /* If the argument deduction results is a METHOD_TYPE,
24826 then there is a problem.
24827 METHOD_TYPE doesn't map to any real C++ type the result of
24828 the deduction cannot be of that type. */
24829 if (TREE_CODE (arg) == METHOD_TYPE)
24830 return unify_method_type_error (explain_p, arg);
24831
24832 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24833 return unify_success (explain_p);
24834
24835 case TEMPLATE_PARM_INDEX:
24836 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24837 if (error_operand_p (tparm))
24838 return unify_invalid (explain_p);
24839
24840 if (TEMPLATE_PARM_LEVEL (parm)
24841 != template_decl_level (tparm))
24842 {
24843 /* The PARM is not one we're trying to unify. Just check
24844 to see if it matches ARG. */
24845 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
24846 && cp_tree_equal (parm, arg));
24847 if (result)
24848 unify_expression_unequal (explain_p, parm, arg);
24849 return result;
24850 }
24851
24852 idx = TEMPLATE_PARM_IDX (parm);
24853 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24854
24855 if (targ)
24856 {
24857 if ((strict & UNIFY_ALLOW_INTEGER)
24858 && TREE_TYPE (targ) && TREE_TYPE (arg)
24859 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
24860 /* We're deducing from an array bound, the type doesn't matter.
24861 This conversion should match the one below. */
24862 arg = fold (build_nop (TREE_TYPE (targ), arg));
24863 int x = !cp_tree_equal (targ, arg);
24864 if (x)
24865 unify_inconsistency (explain_p, parm, targ, arg);
24866 return x;
24867 }
24868
24869 /* [temp.deduct.type] If, in the declaration of a function template
24870 with a non-type template-parameter, the non-type
24871 template-parameter is used in an expression in the function
24872 parameter-list and, if the corresponding template-argument is
24873 deduced, the template-argument type shall match the type of the
24874 template-parameter exactly, except that a template-argument
24875 deduced from an array bound may be of any integral type.
24876 The non-type parameter might use already deduced type parameters. */
24877 tparm = TREE_TYPE (parm);
24878 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
24879 /* We don't have enough levels of args to do any substitution. This
24880 can happen in the context of -fnew-ttp-matching. */;
24881 else
24882 {
24883 ++processing_template_decl;
24884 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
24885 --processing_template_decl;
24886
24887 if (tree a = type_uses_auto (tparm))
24888 {
24889 tparm = do_auto_deduction (tparm, arg, a,
24890 complain, adc_unify, targs,
24891 LOOKUP_NORMAL,
24892 TPARMS_PRIMARY_TEMPLATE (tparms));
24893 if (tparm == error_mark_node)
24894 return 1;
24895 }
24896 }
24897
24898 if (!TREE_TYPE (arg)
24899 || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
24900 /* Template-parameter dependent expression. Just accept it for now.
24901 It will later be processed in convert_template_argument. */
24902 ;
24903 else if (same_type_ignoring_top_level_qualifiers_p
24904 (non_reference (TREE_TYPE (arg)),
24905 non_reference (tparm)))
24906 /* OK. Ignore top-level quals here because a class-type template
24907 parameter object is const. */;
24908 else if ((strict & UNIFY_ALLOW_INTEGER)
24909 && CP_INTEGRAL_TYPE_P (tparm))
24910 /* Convert the ARG to the type of PARM; the deduced non-type
24911 template argument must exactly match the types of the
24912 corresponding parameter. This conversion should match the
24913 one above. */
24914 arg = fold (build_nop (tparm, arg));
24915 else if (uses_template_parms (tparm))
24916 {
24917 /* We haven't deduced the type of this parameter yet. */
24918 if (cxx_dialect >= cxx17
24919 /* We deduce from array bounds in try_array_deduction. */
24920 && !(strict & UNIFY_ALLOW_INTEGER)
24921 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
24922 {
24923 /* Deduce it from the non-type argument. As above, ignore
24924 top-level quals here too. */
24925 tree atype = cv_unqualified (TREE_TYPE (arg));
24926 RECUR_AND_CHECK_FAILURE (tparms, targs,
24927 tparm, atype,
24928 UNIFY_ALLOW_NONE, explain_p);
24929 /* Now check whether the type of this parameter is still
24930 dependent, and give up if so. */
24931 ++processing_template_decl;
24932 tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
24933 --processing_template_decl;
24934 if (uses_template_parms (tparm))
24935 return unify_success (explain_p);
24936 }
24937 else
24938 /* Try again later. */
24939 return unify_success (explain_p);
24940 }
24941 else
24942 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
24943
24944 /* If ARG is a parameter pack or an expansion, we cannot unify
24945 against it unless PARM is also a parameter pack. */
24946 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24947 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
24948 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24949
24950 {
24951 bool removed_attr = false;
24952 arg = strip_typedefs_expr (arg, &removed_attr);
24953 }
24954 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24955 return unify_success (explain_p);
24956
24957 case PTRMEM_CST:
24958 {
24959 /* A pointer-to-member constant can be unified only with
24960 another constant. */
24961 if (TREE_CODE (arg) != PTRMEM_CST)
24962 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
24963
24964 /* Just unify the class member. It would be useless (and possibly
24965 wrong, depending on the strict flags) to unify also
24966 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24967 arg refer to the same variable, even if through different
24968 classes. For instance:
24969
24970 struct A { int x; };
24971 struct B : A { };
24972
24973 Unification of &A::x and &B::x must succeed. */
24974 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
24975 PTRMEM_CST_MEMBER (arg), strict, explain_p);
24976 }
24977
24978 case POINTER_TYPE:
24979 {
24980 if (!TYPE_PTR_P (arg))
24981 return unify_type_mismatch (explain_p, parm, arg);
24982
24983 /* [temp.deduct.call]
24984
24985 A can be another pointer or pointer to member type that can
24986 be converted to the deduced A via a qualification
24987 conversion (_conv.qual_).
24988
24989 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24990 This will allow for additional cv-qualification of the
24991 pointed-to types if appropriate. */
24992
24993 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
24994 /* The derived-to-base conversion only persists through one
24995 level of pointers. */
24996 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
24997
24998 return unify (tparms, targs, TREE_TYPE (parm),
24999 TREE_TYPE (arg), strict, explain_p);
25000 }
25001
25002 case REFERENCE_TYPE:
25003 if (!TYPE_REF_P (arg))
25004 return unify_type_mismatch (explain_p, parm, arg);
25005 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25006 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25007
25008 case ARRAY_TYPE:
25009 if (TREE_CODE (arg) != ARRAY_TYPE)
25010 return unify_type_mismatch (explain_p, parm, arg);
25011 if ((TYPE_DOMAIN (parm) == NULL_TREE)
25012 != (TYPE_DOMAIN (arg) == NULL_TREE))
25013 return unify_type_mismatch (explain_p, parm, arg);
25014 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25015 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
25016 if (TYPE_DOMAIN (parm) != NULL_TREE)
25017 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
25018 TYPE_DOMAIN (arg), explain_p);
25019 return unify_success (explain_p);
25020
25021 case REAL_TYPE:
25022 case COMPLEX_TYPE:
25023 case VECTOR_TYPE:
25024 case INTEGER_TYPE:
25025 case BOOLEAN_TYPE:
25026 case ENUMERAL_TYPE:
25027 case VOID_TYPE:
25028 case OPAQUE_TYPE:
25029 case NULLPTR_TYPE:
25030 if (TREE_CODE (arg) != TREE_CODE (parm))
25031 return unify_type_mismatch (explain_p, parm, arg);
25032
25033 /* We have already checked cv-qualification at the top of the
25034 function. */
25035 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
25036 return unify_type_mismatch (explain_p, parm, arg);
25037
25038 /* As far as unification is concerned, this wins. Later checks
25039 will invalidate it if necessary. */
25040 return unify_success (explain_p);
25041
25042 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
25043 /* Type INTEGER_CST can come from ordinary constant template args. */
25044 case INTEGER_CST:
25045 case REAL_CST:
25046 if (TREE_TYPE (arg) == NULL_TREE
25047 || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
25048 return unify_template_argument_mismatch (explain_p, parm, arg);
25049 while (CONVERT_EXPR_P (arg))
25050 arg = TREE_OPERAND (arg, 0);
25051
25052 if (TREE_CODE (arg) != TREE_CODE (parm))
25053 return unify_template_argument_mismatch (explain_p, parm, arg);
25054 return (simple_cst_equal (parm, arg)
25055 ? unify_success (explain_p)
25056 : unify_template_argument_mismatch (explain_p, parm, arg));
25057
25058 case TREE_VEC:
25059 {
25060 int i, len, argslen;
25061 int parm_variadic_p = 0;
25062
25063 if (TREE_CODE (arg) != TREE_VEC)
25064 return unify_template_argument_mismatch (explain_p, parm, arg);
25065
25066 len = TREE_VEC_LENGTH (parm);
25067 argslen = TREE_VEC_LENGTH (arg);
25068
25069 /* Check for pack expansions in the parameters. */
25070 for (i = 0; i < len; ++i)
25071 {
25072 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
25073 {
25074 if (i == len - 1)
25075 /* We can unify against something with a trailing
25076 parameter pack. */
25077 parm_variadic_p = 1;
25078 else
25079 /* [temp.deduct.type]/9: If the template argument list of
25080 P contains a pack expansion that is not the last
25081 template argument, the entire template argument list
25082 is a non-deduced context. */
25083 return unify_success (explain_p);
25084 }
25085 }
25086
25087 /* If we don't have enough arguments to satisfy the parameters
25088 (not counting the pack expression at the end), or we have
25089 too many arguments for a parameter list that doesn't end in
25090 a pack expression, we can't unify. */
25091 if (parm_variadic_p
25092 ? argslen < len - parm_variadic_p
25093 : argslen != len)
25094 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
25095
25096 /* Unify all of the parameters that precede the (optional)
25097 pack expression. */
25098 for (i = 0; i < len - parm_variadic_p; ++i)
25099 {
25100 RECUR_AND_CHECK_FAILURE (tparms, targs,
25101 TREE_VEC_ELT (parm, i),
25102 TREE_VEC_ELT (arg, i),
25103 UNIFY_ALLOW_NONE, explain_p);
25104 }
25105 if (parm_variadic_p)
25106 return unify_pack_expansion (tparms, targs, parm, arg,
25107 DEDUCE_EXACT,
25108 /*subr=*/true, explain_p);
25109 return unify_success (explain_p);
25110 }
25111
25112 case RECORD_TYPE:
25113 case UNION_TYPE:
25114 if (TREE_CODE (arg) != TREE_CODE (parm))
25115 return unify_type_mismatch (explain_p, parm, arg);
25116
25117 if (TYPE_PTRMEMFUNC_P (parm))
25118 {
25119 if (!TYPE_PTRMEMFUNC_P (arg))
25120 return unify_type_mismatch (explain_p, parm, arg);
25121
25122 return unify (tparms, targs,
25123 TYPE_PTRMEMFUNC_FN_TYPE (parm),
25124 TYPE_PTRMEMFUNC_FN_TYPE (arg),
25125 strict, explain_p);
25126 }
25127 else if (TYPE_PTRMEMFUNC_P (arg))
25128 return unify_type_mismatch (explain_p, parm, arg);
25129
25130 if (CLASSTYPE_TEMPLATE_INFO (parm))
25131 {
25132 tree t = NULL_TREE;
25133
25134 if (strict_in & UNIFY_ALLOW_DERIVED)
25135 {
25136 /* First, we try to unify the PARM and ARG directly. */
25137 t = try_class_unification (tparms, targs,
25138 parm, arg, explain_p);
25139
25140 if (!t)
25141 {
25142 /* Fallback to the special case allowed in
25143 [temp.deduct.call]:
25144
25145 If P is a class, and P has the form
25146 template-id, then A can be a derived class of
25147 the deduced A. Likewise, if P is a pointer to
25148 a class of the form template-id, A can be a
25149 pointer to a derived class pointed to by the
25150 deduced A. */
25151 enum template_base_result r;
25152 r = get_template_base (tparms, targs, parm, arg,
25153 explain_p, &t);
25154
25155 if (!t)
25156 {
25157 /* Don't give the derived diagnostic if we're
25158 already dealing with the same template. */
25159 bool same_template
25160 = (CLASSTYPE_TEMPLATE_INFO (arg)
25161 && (CLASSTYPE_TI_TEMPLATE (parm)
25162 == CLASSTYPE_TI_TEMPLATE (arg)));
25163 return unify_no_common_base (explain_p && !same_template,
25164 r, parm, arg);
25165 }
25166 }
25167 }
25168 else if (CLASSTYPE_TEMPLATE_INFO (arg)
25169 && (CLASSTYPE_TI_TEMPLATE (parm)
25170 == CLASSTYPE_TI_TEMPLATE (arg)))
25171 /* Perhaps PARM is something like S<U> and ARG is S<int>.
25172 Then, we should unify `int' and `U'. */
25173 t = arg;
25174 else
25175 /* There's no chance of unification succeeding. */
25176 return unify_type_mismatch (explain_p, parm, arg);
25177
25178 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
25179 return unify (tparms, targs,
25180 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
25181 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
25182 UNIFY_ALLOW_NONE, explain_p);
25183 else
25184 return unify_success (explain_p);
25185 }
25186 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
25187 return unify_type_mismatch (explain_p, parm, arg);
25188 return unify_success (explain_p);
25189
25190 case METHOD_TYPE:
25191 case FUNCTION_TYPE:
25192 {
25193 unsigned int nargs;
25194 tree *args;
25195 tree a;
25196 unsigned int i;
25197
25198 if (TREE_CODE (arg) != TREE_CODE (parm))
25199 return unify_type_mismatch (explain_p, parm, arg);
25200
25201 /* CV qualifications for methods can never be deduced, they must
25202 match exactly. We need to check them explicitly here,
25203 because type_unification_real treats them as any other
25204 cv-qualified parameter. */
25205 if (TREE_CODE (parm) == METHOD_TYPE
25206 && (!check_cv_quals_for_unify
25207 (UNIFY_ALLOW_NONE,
25208 class_of_this_parm (arg),
25209 class_of_this_parm (parm))))
25210 return unify_cv_qual_mismatch (explain_p, parm, arg);
25211 if (TREE_CODE (arg) == FUNCTION_TYPE
25212 && type_memfn_quals (parm) != type_memfn_quals (arg))
25213 return unify_cv_qual_mismatch (explain_p, parm, arg);
25214 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
25215 return unify_type_mismatch (explain_p, parm, arg);
25216
25217 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
25218 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
25219
25220 nargs = list_length (TYPE_ARG_TYPES (arg));
25221 args = XALLOCAVEC (tree, nargs);
25222 for (a = TYPE_ARG_TYPES (arg), i = 0;
25223 a != NULL_TREE && a != void_list_node;
25224 a = TREE_CHAIN (a), ++i)
25225 args[i] = TREE_VALUE (a);
25226 nargs = i;
25227
25228 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
25229 args, nargs, 1, DEDUCE_EXACT,
25230 NULL, explain_p))
25231 return 1;
25232
25233 if (flag_noexcept_type)
25234 {
25235 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
25236 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
25237 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
25238 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
25239 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
25240 && uses_template_parms (TREE_PURPOSE (pspec)))
25241 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
25242 TREE_PURPOSE (aspec),
25243 UNIFY_ALLOW_NONE, explain_p);
25244 else
25245 {
25246 bool pn = nothrow_spec_p (pspec);
25247 bool an = nothrow_spec_p (aspec);
25248 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
25249 /more/ noexcept, since function pointer conversions are the
25250 reverse of qualification conversions. */
25251 if (an == pn
25252 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25253 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25254 /* OK. */;
25255 else
25256 return unify_type_mismatch (explain_p, parm, arg);
25257 }
25258 }
25259 if (flag_tm)
25260 {
25261 /* As for noexcept. */
25262 bool pn = tx_safe_fn_type_p (parm);
25263 bool an = tx_safe_fn_type_p (arg);
25264 if (an == pn
25265 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
25266 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
25267 /* OK. */;
25268 else
25269 return unify_type_mismatch (explain_p, parm, arg);
25270 }
25271
25272 return 0;
25273 }
25274
25275 case OFFSET_TYPE:
25276 /* Unify a pointer to member with a pointer to member function, which
25277 deduces the type of the member as a function type. */
25278 if (TYPE_PTRMEMFUNC_P (arg))
25279 {
25280 /* Check top-level cv qualifiers */
25281 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
25282 return unify_cv_qual_mismatch (explain_p, parm, arg);
25283
25284 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
25285 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
25286 UNIFY_ALLOW_NONE, explain_p);
25287
25288 /* Determine the type of the function we are unifying against. */
25289 tree fntype = static_fn_type (arg);
25290
25291 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
25292 }
25293
25294 if (TREE_CODE (arg) != OFFSET_TYPE)
25295 return unify_type_mismatch (explain_p, parm, arg);
25296 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
25297 TYPE_OFFSET_BASETYPE (arg),
25298 UNIFY_ALLOW_NONE, explain_p);
25299 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
25300 strict, explain_p);
25301
25302 case CONST_DECL:
25303 /* CONST_DECL should already have been folded to its DECL_INITIAL. */
25304 gcc_unreachable ();
25305
25306 case FIELD_DECL:
25307 case FUNCTION_DECL:
25308 case TEMPLATE_DECL:
25309 /* Matched cases are handled by the ARG == PARM test above. */
25310 return unify_template_argument_mismatch (explain_p, parm, arg);
25311
25312 case VAR_DECL:
25313 /* We might get a variable as a non-type template argument in parm if the
25314 corresponding parameter is type-dependent. Make any necessary
25315 adjustments based on whether arg is a reference. */
25316 if (CONSTANT_CLASS_P (arg))
25317 parm = fold_non_dependent_expr (parm, complain);
25318 else if (REFERENCE_REF_P (arg))
25319 {
25320 tree sub = TREE_OPERAND (arg, 0);
25321 STRIP_NOPS (sub);
25322 if (TREE_CODE (sub) == ADDR_EXPR)
25323 arg = TREE_OPERAND (sub, 0);
25324 }
25325 /* Now use the normal expression code to check whether they match. */
25326 goto expr;
25327
25328 case TYPE_ARGUMENT_PACK:
25329 case NONTYPE_ARGUMENT_PACK:
25330 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
25331 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
25332
25333 case TYPEOF_TYPE:
25334 case DECLTYPE_TYPE:
25335 case TRAIT_TYPE:
25336 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
25337 or TRAIT_TYPE nodes. */
25338 return unify_success (explain_p);
25339
25340 case ERROR_MARK:
25341 /* Unification fails if we hit an error node. */
25342 return unify_invalid (explain_p);
25343
25344 case INDIRECT_REF:
25345 if (REFERENCE_REF_P (parm))
25346 {
25347 bool pexp = PACK_EXPANSION_P (arg);
25348 if (pexp)
25349 arg = PACK_EXPANSION_PATTERN (arg);
25350 if (REFERENCE_REF_P (arg))
25351 arg = TREE_OPERAND (arg, 0);
25352 if (pexp)
25353 arg = make_pack_expansion (arg, complain);
25354 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
25355 strict, explain_p);
25356 }
25357 /* FALLTHRU */
25358
25359 default:
25360 /* An unresolved overload is a nondeduced context. */
25361 if (is_overloaded_fn (parm) || type_unknown_p (parm))
25362 return unify_success (explain_p);
25363 gcc_assert (EXPR_P (parm)
25364 || TREE_CODE (parm) == CONSTRUCTOR
25365 || TREE_CODE (parm) == TRAIT_EXPR);
25366 expr:
25367 /* We must be looking at an expression. This can happen with
25368 something like:
25369
25370 template <int I>
25371 void foo(S<I>, S<I + 2>);
25372
25373 or
25374
25375 template<typename T>
25376 void foo(A<T, T{}>);
25377
25378 This is a "non-deduced context":
25379
25380 [deduct.type]
25381
25382 The non-deduced contexts are:
25383
25384 --A non-type template argument or an array bound in which
25385 a subexpression references a template parameter.
25386
25387 In these cases, we assume deduction succeeded, but don't
25388 actually infer any unifications. */
25389
25390 if (!uses_template_parms (parm)
25391 && !template_args_equal (parm, arg))
25392 return unify_expression_unequal (explain_p, parm, arg);
25393 else
25394 return unify_success (explain_p);
25395 }
25396 }
25397 #undef RECUR_AND_CHECK_FAILURE
25398 \f
25399 /* Note that DECL can be defined in this translation unit, if
25400 required. */
25401
25402 static void
25403 mark_definable (tree decl)
25404 {
25405 tree clone;
25406 DECL_NOT_REALLY_EXTERN (decl) = 1;
25407 FOR_EACH_CLONE (clone, decl)
25408 DECL_NOT_REALLY_EXTERN (clone) = 1;
25409 }
25410
25411 /* Called if RESULT is explicitly instantiated, or is a member of an
25412 explicitly instantiated class. */
25413
25414 void
25415 mark_decl_instantiated (tree result, int extern_p)
25416 {
25417 SET_DECL_EXPLICIT_INSTANTIATION (result);
25418
25419 /* If this entity has already been written out, it's too late to
25420 make any modifications. */
25421 if (TREE_ASM_WRITTEN (result))
25422 return;
25423
25424 /* consteval functions are never emitted. */
25425 if (TREE_CODE (result) == FUNCTION_DECL
25426 && DECL_IMMEDIATE_FUNCTION_P (result))
25427 return;
25428
25429 /* For anonymous namespace we don't need to do anything. */
25430 if (decl_internal_context_p (result))
25431 {
25432 gcc_assert (!TREE_PUBLIC (result));
25433 return;
25434 }
25435
25436 if (TREE_CODE (result) != FUNCTION_DECL)
25437 /* The TREE_PUBLIC flag for function declarations will have been
25438 set correctly by tsubst. */
25439 TREE_PUBLIC (result) = 1;
25440
25441 if (extern_p)
25442 {
25443 DECL_EXTERNAL (result) = 1;
25444 DECL_NOT_REALLY_EXTERN (result) = 0;
25445 }
25446 else
25447 {
25448 mark_definable (result);
25449 mark_needed (result);
25450 /* Always make artificials weak. */
25451 if (DECL_ARTIFICIAL (result) && flag_weak)
25452 comdat_linkage (result);
25453 /* For WIN32 we also want to put explicit instantiations in
25454 linkonce sections. */
25455 else if (TREE_PUBLIC (result))
25456 maybe_make_one_only (result);
25457 if (TREE_CODE (result) == FUNCTION_DECL
25458 && DECL_TEMPLATE_INSTANTIATED (result))
25459 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25460 since start_preparsed_function wouldn't have if we had an earlier
25461 extern explicit instantiation. */
25462 DECL_EXTERNAL (result) = 0;
25463 }
25464
25465 /* If EXTERN_P, then this function will not be emitted -- unless
25466 followed by an explicit instantiation, at which point its linkage
25467 will be adjusted. If !EXTERN_P, then this function will be
25468 emitted here. In neither circumstance do we want
25469 import_export_decl to adjust the linkage. */
25470 DECL_INTERFACE_KNOWN (result) = 1;
25471 }
25472
25473 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
25474 important template arguments. If any are missing, we check whether
25475 they're important by using error_mark_node for substituting into any
25476 args that were used for partial ordering (the ones between ARGS and END)
25477 and seeing if it bubbles up. */
25478
25479 static bool
25480 check_undeduced_parms (tree targs, tree args, tree end)
25481 {
25482 bool found = false;
25483 for (tree& targ : tree_vec_range (targs))
25484 if (targ == NULL_TREE)
25485 {
25486 found = true;
25487 targ = error_mark_node;
25488 }
25489 if (found)
25490 {
25491 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
25492 if (substed == error_mark_node)
25493 return true;
25494 }
25495 return false;
25496 }
25497
25498 /* Given two function templates PAT1 and PAT2, return:
25499
25500 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25501 -1 if PAT2 is more specialized than PAT1.
25502 0 if neither is more specialized.
25503
25504 LEN indicates the number of parameters we should consider
25505 (defaulted parameters should not be considered).
25506
25507 The 1998 std underspecified function template partial ordering, and
25508 DR214 addresses the issue. We take pairs of arguments, one from
25509 each of the templates, and deduce them against each other. One of
25510 the templates will be more specialized if all the *other*
25511 template's arguments deduce against its arguments and at least one
25512 of its arguments *does* *not* deduce against the other template's
25513 corresponding argument. Deduction is done as for class templates.
25514 The arguments used in deduction have reference and top level cv
25515 qualifiers removed. Iff both arguments were originally reference
25516 types *and* deduction succeeds in both directions, an lvalue reference
25517 wins against an rvalue reference and otherwise the template
25518 with the more cv-qualified argument wins for that pairing (if
25519 neither is more cv-qualified, they both are equal). Unlike regular
25520 deduction, after all the arguments have been deduced in this way,
25521 we do *not* verify the deduced template argument values can be
25522 substituted into non-deduced contexts.
25523
25524 The logic can be a bit confusing here, because we look at deduce1 and
25525 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25526 can find template arguments for pat1 to make arg1 look like arg2, that
25527 means that arg2 is at least as specialized as arg1. */
25528
25529 int
25530 more_specialized_fn (tree pat1, tree pat2, int len)
25531 {
25532 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
25533 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
25534 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
25535 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
25536 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
25537 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
25538 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
25539 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
25540 tree origs1, origs2;
25541 bool lose1 = false;
25542 bool lose2 = false;
25543
25544 /* C++17 [temp.func.order]/3 (CWG532)
25545
25546 If only one of the function templates M is a non-static member of some
25547 class A, M is considered to have a new first parameter inserted in its
25548 function parameter list. Given cv as the cv-qualifiers of M (if any), the
25549 new parameter is of type "rvalue reference to cv A" if the optional
25550 ref-qualifier of M is && or if M has no ref-qualifier and the first
25551 parameter of the other template has rvalue reference type. Otherwise, the
25552 new parameter is of type "lvalue reference to cv A". */
25553
25554 if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
25555 {
25556 /* Note C++20 DR2445 extended the above to static member functions, but
25557 I think the old G++ behavior of just skipping the object
25558 parameter when comparing to a static member function was better, so
25559 let's stick with that for now. This is CWG2834. --jason 2023-12 */
25560 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
25561 {
25562 len--; /* LEN is the number of significant arguments for DECL1 */
25563 args1 = TREE_CHAIN (args1);
25564 }
25565 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
25566 args2 = TREE_CHAIN (args2);
25567 }
25568 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
25569 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
25570 {
25571 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
25572 would break e.g. cpp1y/lambda-generic-variadic5.C. */
25573 len--;
25574 args1 = TREE_CHAIN (args1);
25575 args2 = TREE_CHAIN (args2);
25576 }
25577 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
25578 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
25579 {
25580 /* The other is a non-member or explicit object member function;
25581 rewrite the implicit object parameter to a reference. */
25582 tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
25583 tree &nsargs = ns == decl2 ? args2 : args1;
25584 tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
25585
25586 nsargs = TREE_CHAIN (nsargs);
25587
25588 cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
25589 if (rqual == REF_QUAL_NONE)
25590 {
25591 tree otherfirst = ns == decl1 ? args2 : args1;
25592 otherfirst = TREE_VALUE (otherfirst);
25593 if (TREE_CODE (otherfirst) == REFERENCE_TYPE
25594 && TYPE_REF_IS_RVALUE (otherfirst))
25595 rqual = REF_QUAL_RVALUE;
25596 }
25597 obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
25598 nsargs = tree_cons (NULL_TREE, obtype, nsargs);
25599 }
25600
25601 /* If only one is a conversion operator, they are unordered. */
25602 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
25603 return 0;
25604
25605 /* Consider the return type for a conversion function */
25606 if (DECL_CONV_FN_P (decl1))
25607 {
25608 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
25609 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
25610 len++;
25611 }
25612
25613 processing_template_decl++;
25614
25615 origs1 = args1;
25616 origs2 = args2;
25617
25618 while (len--
25619 /* Stop when an ellipsis is seen. */
25620 && args1 != NULL_TREE && args2 != NULL_TREE)
25621 {
25622 tree arg1 = TREE_VALUE (args1);
25623 tree arg2 = TREE_VALUE (args2);
25624 int deduce1, deduce2;
25625 int quals1 = -1;
25626 int quals2 = -1;
25627 int ref1 = 0;
25628 int ref2 = 0;
25629
25630 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25631 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25632 {
25633 /* When both arguments are pack expansions, we need only
25634 unify the patterns themselves. */
25635 arg1 = PACK_EXPANSION_PATTERN (arg1);
25636 arg2 = PACK_EXPANSION_PATTERN (arg2);
25637
25638 /* This is the last comparison we need to do. */
25639 len = 0;
25640 }
25641
25642 if (TYPE_REF_P (arg1))
25643 {
25644 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
25645 arg1 = TREE_TYPE (arg1);
25646 quals1 = cp_type_quals (arg1);
25647 }
25648
25649 if (TYPE_REF_P (arg2))
25650 {
25651 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
25652 arg2 = TREE_TYPE (arg2);
25653 quals2 = cp_type_quals (arg2);
25654 }
25655
25656 arg1 = TYPE_MAIN_VARIANT (arg1);
25657 arg2 = TYPE_MAIN_VARIANT (arg2);
25658
25659 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
25660 {
25661 int i, len2 = remaining_arguments (args2);
25662 tree parmvec = make_tree_vec (1);
25663 tree argvec = make_tree_vec (len2);
25664 tree ta = args2;
25665
25666 /* Setup the parameter vector, which contains only ARG1. */
25667 TREE_VEC_ELT (parmvec, 0) = arg1;
25668
25669 /* Setup the argument vector, which contains the remaining
25670 arguments. */
25671 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
25672 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25673
25674 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
25675 argvec, DEDUCE_EXACT,
25676 /*subr=*/true, /*explain_p=*/false)
25677 == 0);
25678
25679 /* We cannot deduce in the other direction, because ARG1 is
25680 a pack expansion but ARG2 is not. */
25681 deduce2 = 0;
25682 }
25683 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25684 {
25685 int i, len1 = remaining_arguments (args1);
25686 tree parmvec = make_tree_vec (1);
25687 tree argvec = make_tree_vec (len1);
25688 tree ta = args1;
25689
25690 /* Setup the parameter vector, which contains only ARG1. */
25691 TREE_VEC_ELT (parmvec, 0) = arg2;
25692
25693 /* Setup the argument vector, which contains the remaining
25694 arguments. */
25695 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
25696 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25697
25698 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
25699 argvec, DEDUCE_EXACT,
25700 /*subr=*/true, /*explain_p=*/false)
25701 == 0);
25702
25703 /* We cannot deduce in the other direction, because ARG2 is
25704 a pack expansion but ARG1 is not.*/
25705 deduce1 = 0;
25706 }
25707
25708 else
25709 {
25710 /* The normal case, where neither argument is a pack
25711 expansion. */
25712 deduce1 = (unify (tparms1, targs1, arg1, arg2,
25713 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25714 == 0);
25715 deduce2 = (unify (tparms2, targs2, arg2, arg1,
25716 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25717 == 0);
25718 }
25719
25720 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25721 arg2, then arg2 is not as specialized as arg1. */
25722 if (!deduce1)
25723 lose2 = true;
25724 if (!deduce2)
25725 lose1 = true;
25726
25727 /* "If, for a given type, deduction succeeds in both directions
25728 (i.e., the types are identical after the transformations above)
25729 and both P and A were reference types (before being replaced with
25730 the type referred to above):
25731 - if the type from the argument template was an lvalue reference and
25732 the type from the parameter template was not, the argument type is
25733 considered to be more specialized than the other; otherwise,
25734 - if the type from the argument template is more cv-qualified
25735 than the type from the parameter template (as described above),
25736 the argument type is considered to be more specialized than the other;
25737 otherwise,
25738 - neither type is more specialized than the other." */
25739
25740 if (deduce1 && deduce2)
25741 {
25742 if (ref1 && ref2 && ref1 != ref2)
25743 {
25744 if (ref1 > ref2)
25745 lose1 = true;
25746 else
25747 lose2 = true;
25748 }
25749 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
25750 {
25751 if ((quals1 & quals2) == quals2)
25752 lose2 = true;
25753 if ((quals1 & quals2) == quals1)
25754 lose1 = true;
25755 }
25756 }
25757
25758 if (lose1 && lose2)
25759 /* We've failed to deduce something in either direction.
25760 These must be unordered. */
25761 break;
25762
25763 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25764 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25765 /* We have already processed all of the arguments in our
25766 handing of the pack expansion type. */
25767 len = 0;
25768
25769 args1 = TREE_CHAIN (args1);
25770 args2 = TREE_CHAIN (args2);
25771 }
25772
25773 /* "In most cases, all template parameters must have values in order for
25774 deduction to succeed, but for partial ordering purposes a template
25775 parameter may remain without a value provided it is not used in the
25776 types being used for partial ordering."
25777
25778 Thus, if we are missing any of the targs1 we need to substitute into
25779 origs1, then pat2 is not as specialized as pat1. This can happen when
25780 there is a nondeduced context. */
25781 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
25782 lose2 = true;
25783 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
25784 lose1 = true;
25785
25786 processing_template_decl--;
25787
25788 /* If both deductions succeed, the partial ordering selects the more
25789 constrained template. */
25790 /* P2113: If the corresponding template-parameters of the
25791 template-parameter-lists are not equivalent ([temp.over.link]) or if
25792 the function parameters that positionally correspond between the two
25793 templates are not of the same type, neither template is more
25794 specialized than the other. */
25795 if (!lose1 && !lose2
25796 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
25797 DECL_TEMPLATE_PARMS (pat2))
25798 && compparms (origs1, origs2))
25799 {
25800 int winner = more_constrained (decl1, decl2);
25801 if (winner > 0)
25802 lose2 = true;
25803 else if (winner < 0)
25804 lose1 = true;
25805 }
25806
25807 /* All things being equal, if the next argument is a pack expansion
25808 for one function but not for the other, prefer the
25809 non-variadic function. FIXME this is bogus; see c++/41958. */
25810 if (lose1 == lose2
25811 && args1 && TREE_VALUE (args1)
25812 && args2 && TREE_VALUE (args2))
25813 {
25814 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
25815 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
25816 }
25817
25818 if (lose1 == lose2)
25819 return 0;
25820 else if (!lose1)
25821 return 1;
25822 else
25823 return -1;
25824 }
25825
25826 /* Determine which of two partial specializations of TMPL is more
25827 specialized.
25828
25829 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25830 to the first partial specialization. The TREE_PURPOSE is the
25831 innermost set of template parameters for the partial
25832 specialization. PAT2 is similar, but for the second template.
25833
25834 Return 1 if the first partial specialization is more specialized;
25835 -1 if the second is more specialized; 0 if neither is more
25836 specialized.
25837
25838 See [temp.class.order] for information about determining which of
25839 two templates is more specialized. */
25840
25841 static int
25842 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
25843 {
25844 tree targs;
25845 int winner = 0;
25846 bool any_deductions = false;
25847
25848 tree tmpl1 = TREE_VALUE (pat1);
25849 tree tmpl2 = TREE_VALUE (pat2);
25850 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
25851 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
25852
25853 /* Just like what happens for functions, if we are ordering between
25854 different template specializations, we may encounter dependent
25855 types in the arguments, and we need our dependency check functions
25856 to behave correctly. */
25857 ++processing_template_decl;
25858 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
25859 if (targs)
25860 {
25861 --winner;
25862 any_deductions = true;
25863 }
25864
25865 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
25866 if (targs)
25867 {
25868 ++winner;
25869 any_deductions = true;
25870 }
25871 --processing_template_decl;
25872
25873 /* If both deductions succeed, the partial ordering selects the more
25874 constrained template. */
25875 if (!winner && any_deductions)
25876 winner = more_constrained (tmpl1, tmpl2);
25877
25878 /* In the case of a tie where at least one of the templates
25879 has a parameter pack at the end, the template with the most
25880 non-packed parameters wins. */
25881 if (winner == 0
25882 && any_deductions
25883 && (template_args_variadic_p (TREE_PURPOSE (pat1))
25884 || template_args_variadic_p (TREE_PURPOSE (pat2))))
25885 {
25886 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
25887 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
25888 int len1 = TREE_VEC_LENGTH (args1);
25889 int len2 = TREE_VEC_LENGTH (args2);
25890
25891 /* We don't count the pack expansion at the end. */
25892 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
25893 --len1;
25894 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
25895 --len2;
25896
25897 if (len1 > len2)
25898 return 1;
25899 else if (len1 < len2)
25900 return -1;
25901 }
25902
25903 return winner;
25904 }
25905
25906 /* Return the template arguments that will produce the function signature
25907 DECL from the function template FN, with the explicit template
25908 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25909 also match. Return NULL_TREE if no satisfactory arguments could be
25910 found. */
25911
25912 static tree
25913 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
25914 {
25915 int ntparms = DECL_NTPARMS (fn);
25916 tree targs = make_tree_vec (ntparms);
25917 tree decl_type = TREE_TYPE (decl);
25918 tree decl_arg_types;
25919 tree *args;
25920 unsigned int nargs, ix;
25921 tree arg;
25922
25923 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
25924
25925 /* Never do unification on the 'this' parameter. */
25926 decl_arg_types = skip_artificial_parms_for (decl,
25927 TYPE_ARG_TYPES (decl_type));
25928
25929 nargs = list_length (decl_arg_types);
25930 args = XALLOCAVEC (tree, nargs);
25931 for (arg = decl_arg_types, ix = 0;
25932 arg != NULL_TREE;
25933 arg = TREE_CHAIN (arg), ++ix)
25934 args[ix] = TREE_VALUE (arg);
25935
25936 if (fn_type_unification (fn, explicit_args, targs,
25937 args, ix,
25938 (check_rettype || DECL_CONV_FN_P (fn)
25939 ? TREE_TYPE (decl_type) : NULL_TREE),
25940 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
25941 /*explain_p=*/false,
25942 /*decltype*/false)
25943 == error_mark_node)
25944 return NULL_TREE;
25945
25946 return targs;
25947 }
25948
25949 /* Return the innermost template arguments that, when applied to a partial
25950 specialization SPEC_TMPL of TMPL, yield the ARGS.
25951
25952 For example, suppose we have:
25953
25954 template <class T, class U> struct S {};
25955 template <class T> struct S<T*, int> {};
25956
25957 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25958 partial specialization and the ARGS will be {double*, int}. The resulting
25959 vector will be {double}, indicating that `T' is bound to `double'. */
25960
25961 static tree
25962 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
25963 {
25964 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
25965 tree spec_args
25966 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
25967 int i, ntparms = TREE_VEC_LENGTH (tparms);
25968 tree deduced_args;
25969 tree innermost_deduced_args;
25970
25971 innermost_deduced_args = make_tree_vec (ntparms);
25972 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25973 {
25974 deduced_args = copy_node (args);
25975 SET_TMPL_ARGS_LEVEL (deduced_args,
25976 TMPL_ARGS_DEPTH (deduced_args),
25977 innermost_deduced_args);
25978 }
25979 else
25980 deduced_args = innermost_deduced_args;
25981
25982 bool tried_array_deduction = (cxx_dialect < cxx17);
25983 again:
25984 if (unify (tparms, deduced_args,
25985 INNERMOST_TEMPLATE_ARGS (spec_args),
25986 INNERMOST_TEMPLATE_ARGS (args),
25987 UNIFY_ALLOW_NONE, /*explain_p=*/false))
25988 return NULL_TREE;
25989
25990 for (i = 0; i < ntparms; ++i)
25991 if (! TREE_VEC_ELT (innermost_deduced_args, i))
25992 {
25993 if (!tried_array_deduction)
25994 {
25995 try_array_deduction (tparms, innermost_deduced_args,
25996 INNERMOST_TEMPLATE_ARGS (spec_args));
25997 tried_array_deduction = true;
25998 if (TREE_VEC_ELT (innermost_deduced_args, i))
25999 goto again;
26000 }
26001 return NULL_TREE;
26002 }
26003
26004 if (!push_tinst_level (spec_tmpl, deduced_args))
26005 {
26006 excessive_deduction_depth = true;
26007 return NULL_TREE;
26008 }
26009
26010 /* Verify that nondeduced template arguments agree with the type
26011 obtained from argument deduction.
26012
26013 For example:
26014
26015 struct A { typedef int X; };
26016 template <class T, class U> struct C {};
26017 template <class T> struct C<T, typename T::X> {};
26018
26019 Then with the instantiation `C<A, int>', we can deduce that
26020 `T' is `A' but unify () does not check whether `typename T::X'
26021 is `int'. */
26022 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
26023
26024 if (spec_args != error_mark_node)
26025 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
26026 INNERMOST_TEMPLATE_ARGS (spec_args),
26027 tmpl, tf_none, false);
26028
26029 pop_tinst_level ();
26030
26031 if (spec_args == error_mark_node
26032 /* We only need to check the innermost arguments; the other
26033 arguments will always agree. */
26034 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
26035 INNERMOST_TEMPLATE_ARGS (args)))
26036 return NULL_TREE;
26037
26038 /* Now that we have bindings for all of the template arguments,
26039 ensure that the arguments deduced for the template template
26040 parameters have compatible template parameter lists. See the use
26041 of template_template_parm_bindings_ok_p in fn_type_unification
26042 for more information. */
26043 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
26044 return NULL_TREE;
26045
26046 return deduced_args;
26047 }
26048
26049 // Compare two function templates T1 and T2 by deducing bindings
26050 // from one against the other. If both deductions succeed, compare
26051 // constraints to see which is more constrained.
26052 static int
26053 more_specialized_inst (tree t1, tree t2)
26054 {
26055 int fate = 0;
26056 int count = 0;
26057
26058 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
26059 {
26060 --fate;
26061 ++count;
26062 }
26063
26064 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
26065 {
26066 ++fate;
26067 ++count;
26068 }
26069
26070 // If both deductions succeed, then one may be more constrained.
26071 if (count == 2 && fate == 0)
26072 fate = more_constrained (t1, t2);
26073
26074 return fate;
26075 }
26076
26077 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
26078 Return the TREE_LIST node with the most specialized template, if
26079 any. If there is no most specialized template, the error_mark_node
26080 is returned.
26081
26082 Note that this function does not look at, or modify, the
26083 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
26084 returned is one of the elements of INSTANTIATIONS, callers may
26085 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
26086 and retrieve it from the value returned. */
26087
26088 tree
26089 most_specialized_instantiation (tree templates)
26090 {
26091 tree fn, champ;
26092
26093 ++processing_template_decl;
26094
26095 champ = templates;
26096 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
26097 {
26098 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
26099 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
26100 if (fate == -1)
26101 champ = fn;
26102 else if (!fate)
26103 {
26104 /* Equally specialized, move to next function. If there
26105 is no next function, nothing's most specialized. */
26106 fn = TREE_CHAIN (fn);
26107 champ = fn;
26108 if (!fn)
26109 break;
26110 }
26111 }
26112
26113 if (champ)
26114 /* Now verify that champ is better than everything earlier in the
26115 instantiation list. */
26116 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
26117 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
26118 {
26119 champ = NULL_TREE;
26120 break;
26121 }
26122 }
26123
26124 processing_template_decl--;
26125
26126 if (!champ)
26127 return error_mark_node;
26128
26129 return champ;
26130 }
26131
26132 /* If DECL is a specialization of some template, return the most
26133 general such template. Otherwise, returns NULL_TREE.
26134
26135 For example, given:
26136
26137 template <class T> struct S { template <class U> void f(U); };
26138
26139 if TMPL is `template <class U> void S<int>::f(U)' this will return
26140 the full template. This function will not trace past partial
26141 specializations, however. For example, given in addition:
26142
26143 template <class T> struct S<T*> { template <class U> void f(U); };
26144
26145 if TMPL is `template <class U> void S<int*>::f(U)' this will return
26146 `template <class T> template <class U> S<T*>::f(U)'. */
26147
26148 tree
26149 most_general_template (const_tree decl)
26150 {
26151 if (TREE_CODE (decl) != TEMPLATE_DECL)
26152 {
26153 if (tree tinfo = get_template_info (decl))
26154 decl = TI_TEMPLATE (tinfo);
26155 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
26156 template friend, or a FIELD_DECL for a capture pack. */
26157 if (TREE_CODE (decl) != TEMPLATE_DECL)
26158 return NULL_TREE;
26159 }
26160
26161 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
26162 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
26163
26164 /* Look for more and more general templates. */
26165 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
26166 {
26167 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
26168 (See cp-tree.h for details.) */
26169 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
26170 break;
26171
26172 if (CLASS_TYPE_P (TREE_TYPE (decl))
26173 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
26174 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
26175 break;
26176
26177 /* Stop if we run into an explicitly specialized class template. */
26178 if (!DECL_NAMESPACE_SCOPE_P (decl)
26179 && DECL_CONTEXT (decl)
26180 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
26181 break;
26182
26183 decl = DECL_TI_TEMPLATE (decl);
26184 }
26185
26186 return CONST_CAST_TREE (decl);
26187 }
26188
26189 /* Return the most specialized of the template partial specializations
26190 which can produce TARGET, a specialization of some class or variable
26191 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
26192 TEMPLATE_DECL node corresponding to the partial specialization, while
26193 the TI_ARGS is the set of template arguments that must be substituted
26194 into the template pattern in order to generate TARGET. The result is
26195 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
26196 RECHECKING is true.
26197
26198 If the choice of partial specialization is ambiguous, a diagnostic
26199 is issued, and the error_mark_node is returned. If there are no
26200 partial specializations matching TARGET, then NULL_TREE is
26201 returned, indicating that the primary template should be used. */
26202
26203 tree
26204 most_specialized_partial_spec (tree target, tsubst_flags_t complain,
26205 bool rechecking /* = false */)
26206 {
26207 tree tinfo = NULL_TREE;
26208 tree tmpl, args, decl;
26209 if (TYPE_P (target))
26210 {
26211 tinfo = CLASSTYPE_TEMPLATE_INFO (target);
26212 tmpl = TI_TEMPLATE (tinfo);
26213 args = TI_ARGS (tinfo);
26214 decl = TYPE_NAME (target);
26215 }
26216 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
26217 {
26218 tmpl = TREE_OPERAND (target, 0);
26219 args = TREE_OPERAND (target, 1);
26220 decl = DECL_TEMPLATE_RESULT (tmpl);
26221 }
26222 else if (VAR_P (target))
26223 {
26224 tinfo = DECL_TEMPLATE_INFO (target);
26225 tmpl = TI_TEMPLATE (tinfo);
26226 args = TI_ARGS (tinfo);
26227 decl = target;
26228 }
26229 else
26230 gcc_unreachable ();
26231
26232 if (!PRIMARY_TEMPLATE_P (tmpl))
26233 return NULL_TREE;
26234
26235 if (!rechecking
26236 && tinfo
26237 && (VAR_P (target) || COMPLETE_TYPE_P (target)))
26238 return TI_PARTIAL_INFO (tinfo);
26239
26240 tree main_tmpl = most_general_template (tmpl);
26241 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
26242 if (!specs)
26243 /* There are no partial specializations of this template. */
26244 return NULL_TREE;
26245
26246 push_access_scope_guard pas (decl);
26247 deferring_access_check_sentinel acs (dk_no_deferred);
26248
26249 /* For determining which partial specialization to use, only the
26250 innermost args are interesting. */
26251 tree outer_args = NULL_TREE;
26252 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
26253 {
26254 outer_args = strip_innermost_template_args (args, 1);
26255 args = INNERMOST_TEMPLATE_ARGS (args);
26256 }
26257
26258 /* The caller hasn't called push_to_top_level yet, but we need
26259 get_partial_spec_bindings to be done in non-template context so that we'll
26260 fully resolve everything. */
26261 processing_template_decl_sentinel ptds;
26262
26263 tree list = NULL_TREE;
26264 for (tree t = specs; t; t = TREE_CHAIN (t))
26265 {
26266 const tree ospec_tmpl = TREE_VALUE (t);
26267
26268 tree spec_tmpl;
26269 if (outer_args)
26270 {
26271 /* Substitute in the template args from the enclosing class. */
26272 ++processing_template_decl;
26273 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
26274 --processing_template_decl;
26275 if (spec_tmpl == error_mark_node)
26276 return error_mark_node;
26277 }
26278 else
26279 spec_tmpl = ospec_tmpl;
26280
26281 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
26282 if (spec_args)
26283 {
26284 if (outer_args)
26285 spec_args = add_to_template_args (outer_args, spec_args);
26286
26287 /* Keep the candidate only if its constraints are satisfied. */
26288 if (constraints_satisfied_p (ospec_tmpl, spec_args))
26289 list = tree_cons (spec_args, ospec_tmpl, list);
26290 }
26291 }
26292
26293 if (! list)
26294 return NULL_TREE;
26295
26296 tree champ = list;
26297 bool ambiguous_p = false;
26298 for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
26299 {
26300 int fate = more_specialized_partial_spec (tmpl, champ, t);
26301 if (fate == 1)
26302 ;
26303 else
26304 {
26305 if (fate == 0)
26306 {
26307 t = TREE_CHAIN (t);
26308 if (! t)
26309 {
26310 ambiguous_p = true;
26311 break;
26312 }
26313 }
26314 champ = t;
26315 }
26316 }
26317
26318 if (!ambiguous_p)
26319 for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
26320 {
26321 int fate = more_specialized_partial_spec (tmpl, champ, t);
26322 if (fate != 1)
26323 {
26324 ambiguous_p = true;
26325 break;
26326 }
26327 }
26328
26329 if (ambiguous_p)
26330 {
26331 const char *str;
26332 char *spaces = NULL;
26333 if (!(complain & tf_error))
26334 return error_mark_node;
26335 if (TYPE_P (target))
26336 error ("ambiguous template instantiation for %q#T", target);
26337 else
26338 error ("ambiguous template instantiation for %q#D", target);
26339 str = ngettext ("candidate is:", "candidates are:", list_length (list));
26340 for (tree t = list; t; t = TREE_CHAIN (t))
26341 {
26342 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
26343 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
26344 "%s %#qS", spaces ? spaces : str, subst);
26345 spaces = spaces ? spaces : get_spaces (str);
26346 }
26347 free (spaces);
26348 return error_mark_node;
26349 }
26350
26351 tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
26352 if (!rechecking && tinfo)
26353 TI_PARTIAL_INFO (tinfo) = result;
26354 return result;
26355 }
26356
26357 /* Explicitly instantiate DECL. */
26358
26359 void
26360 do_decl_instantiation (tree decl, tree storage)
26361 {
26362 tree result = NULL_TREE;
26363 int extern_p = 0;
26364
26365 if (!decl || decl == error_mark_node)
26366 /* An error occurred, for which grokdeclarator has already issued
26367 an appropriate message. */
26368 return;
26369 else if (! DECL_LANG_SPECIFIC (decl))
26370 {
26371 error ("explicit instantiation of non-template %q#D", decl);
26372 return;
26373 }
26374 else if (DECL_DECLARED_CONCEPT_P (decl))
26375 {
26376 if (VAR_P (decl))
26377 error ("explicit instantiation of variable concept %q#D", decl);
26378 else
26379 error ("explicit instantiation of function concept %q#D", decl);
26380 return;
26381 }
26382
26383 bool var_templ = (DECL_TEMPLATE_INFO (decl)
26384 && variable_template_p (DECL_TI_TEMPLATE (decl)));
26385
26386 if (VAR_P (decl) && !var_templ)
26387 {
26388 /* There is an asymmetry here in the way VAR_DECLs and
26389 FUNCTION_DECLs are handled by grokdeclarator. In the case of
26390 the latter, the DECL we get back will be marked as a
26391 template instantiation, and the appropriate
26392 DECL_TEMPLATE_INFO will be set up. This does not happen for
26393 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
26394 should handle VAR_DECLs as it currently handles
26395 FUNCTION_DECLs. */
26396 if (!DECL_CLASS_SCOPE_P (decl))
26397 {
26398 error ("%qD is not a static data member of a class template", decl);
26399 return;
26400 }
26401 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
26402 if (!result || !VAR_P (result))
26403 {
26404 error ("no matching template for %qD found", decl);
26405 return;
26406 }
26407 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
26408 {
26409 error ("type %qT for explicit instantiation %qD does not match "
26410 "declared type %qT", TREE_TYPE (result), decl,
26411 TREE_TYPE (decl));
26412 return;
26413 }
26414 }
26415 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
26416 {
26417 error ("explicit instantiation of %q#D", decl);
26418 return;
26419 }
26420 else
26421 result = decl;
26422
26423 /* Check for various error cases. Note that if the explicit
26424 instantiation is valid the RESULT will currently be marked as an
26425 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26426 until we get here. */
26427
26428 if (DECL_TEMPLATE_SPECIALIZATION (result))
26429 {
26430 /* DR 259 [temp.spec].
26431
26432 Both an explicit instantiation and a declaration of an explicit
26433 specialization shall not appear in a program unless the explicit
26434 instantiation follows a declaration of the explicit specialization.
26435
26436 For a given set of template parameters, if an explicit
26437 instantiation of a template appears after a declaration of an
26438 explicit specialization for that template, the explicit
26439 instantiation has no effect. */
26440 return;
26441 }
26442 else if (DECL_EXPLICIT_INSTANTIATION (result))
26443 {
26444 /* [temp.spec]
26445
26446 No program shall explicitly instantiate any template more
26447 than once.
26448
26449 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26450 the first instantiation was `extern' and the second is not,
26451 and EXTERN_P for the opposite case. */
26452 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
26453 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
26454 /* If an "extern" explicit instantiation follows an ordinary
26455 explicit instantiation, the template is instantiated. */
26456 if (extern_p)
26457 return;
26458 }
26459 else if (!DECL_IMPLICIT_INSTANTIATION (result))
26460 {
26461 error ("no matching template for %qD found", result);
26462 return;
26463 }
26464 else if (!DECL_TEMPLATE_INFO (result))
26465 {
26466 permerror (input_location, "explicit instantiation of non-template %q#D", result);
26467 return;
26468 }
26469
26470 if (storage == NULL_TREE)
26471 ;
26472 else if (storage == ridpointers[(int) RID_EXTERN])
26473 {
26474 if (cxx_dialect == cxx98)
26475 pedwarn (input_location, OPT_Wpedantic,
26476 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26477 "instantiations");
26478 extern_p = 1;
26479 }
26480 else
26481 error ("storage class %qD applied to template instantiation", storage);
26482
26483 check_explicit_instantiation_namespace (result);
26484 mark_decl_instantiated (result, extern_p);
26485 if (! extern_p)
26486 instantiate_decl (result, /*defer_ok=*/true,
26487 /*expl_inst_class_mem_p=*/false);
26488 }
26489
26490 static void
26491 mark_class_instantiated (tree t, int extern_p)
26492 {
26493 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
26494 SET_CLASSTYPE_INTERFACE_KNOWN (t);
26495 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
26496 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
26497 if (! extern_p)
26498 {
26499 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
26500 rest_of_type_compilation (t, 1);
26501 }
26502 }
26503
26504 /* Perform an explicit instantiation of template class T. STORAGE, if
26505 non-null, is the RID for extern, inline or static. COMPLAIN is
26506 nonzero if this is called from the parser, zero if called recursively,
26507 since the standard is unclear (as detailed below). */
26508
26509 void
26510 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
26511 {
26512 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
26513 {
26514 if (tree ti = TYPE_TEMPLATE_INFO (t))
26515 error ("explicit instantiation of non-class template %qD",
26516 TI_TEMPLATE (ti));
26517 else
26518 error ("explicit instantiation of non-template type %qT", t);
26519 return;
26520 }
26521
26522 complete_type (t);
26523
26524 if (!COMPLETE_TYPE_P (t))
26525 {
26526 if (complain & tf_error)
26527 error ("explicit instantiation of %q#T before definition of template",
26528 t);
26529 return;
26530 }
26531
26532 /* At most one of these will be true. */
26533 bool extern_p = false;
26534 bool nomem_p = false;
26535 bool static_p = false;
26536
26537 if (storage != NULL_TREE)
26538 {
26539 if (storage == ridpointers[(int) RID_EXTERN])
26540 {
26541 if (cxx_dialect == cxx98)
26542 pedwarn (input_location, OPT_Wpedantic,
26543 "ISO C++ 1998 forbids the use of %<extern%> on "
26544 "explicit instantiations");
26545 }
26546 else
26547 pedwarn (input_location, OPT_Wpedantic,
26548 "ISO C++ forbids the use of %qE"
26549 " on explicit instantiations", storage);
26550
26551 if (storage == ridpointers[(int) RID_INLINE])
26552 nomem_p = true;
26553 else if (storage == ridpointers[(int) RID_EXTERN])
26554 extern_p = true;
26555 else if (storage == ridpointers[(int) RID_STATIC])
26556 static_p = true;
26557 else
26558 error ("storage class %qD applied to template instantiation",
26559 storage);
26560 }
26561
26562 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
26563 /* DR 259 [temp.spec].
26564
26565 Both an explicit instantiation and a declaration of an explicit
26566 specialization shall not appear in a program unless the
26567 explicit instantiation follows a declaration of the explicit
26568 specialization.
26569
26570 For a given set of template parameters, if an explicit
26571 instantiation of a template appears after a declaration of an
26572 explicit specialization for that template, the explicit
26573 instantiation has no effect. */
26574 return;
26575
26576 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
26577 {
26578 /* We've already instantiated the template. */
26579
26580 /* [temp.spec]
26581
26582 No program shall explicitly instantiate any template more
26583 than once.
26584
26585 If EXTERN_P then this is ok. */
26586 if (!extern_p && (complain & tf_error))
26587 permerror (input_location,
26588 "duplicate explicit instantiation of %q#T", t);
26589
26590 return;
26591 }
26592
26593 check_explicit_instantiation_namespace (TYPE_NAME (t));
26594 mark_class_instantiated (t, extern_p);
26595
26596 if (nomem_p)
26597 return;
26598
26599 /* In contrast to implicit instantiation, where only the
26600 declarations, and not the definitions, of members are
26601 instantiated, we have here:
26602
26603 [temp.explicit]
26604
26605 An explicit instantiation that names a class template
26606 specialization is also an explicit instantiation of the same
26607 kind (declaration or definition) of each of its members (not
26608 including members inherited from base classes and members
26609 that are templates) that has not been previously explicitly
26610 specialized in the translation unit containing the explicit
26611 instantiation, provided that the associated constraints, if
26612 any, of that member are satisfied by the template arguments
26613 of the explicit instantiation. */
26614 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
26615 if ((VAR_P (fld)
26616 || (TREE_CODE (fld) == FUNCTION_DECL
26617 && !static_p
26618 && user_provided_p (fld)))
26619 && DECL_TEMPLATE_INSTANTIATION (fld)
26620 && constraints_satisfied_p (fld))
26621 {
26622 mark_decl_instantiated (fld, extern_p);
26623 if (! extern_p)
26624 instantiate_decl (fld, /*defer_ok=*/true,
26625 /*expl_inst_class_mem_p=*/true);
26626 }
26627 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
26628 {
26629 tree type = TREE_TYPE (fld);
26630
26631 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26632 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
26633 do_type_instantiation (type, storage, 0);
26634 }
26635 }
26636
26637 /* Given a function DECL, which is a specialization of TMPL, modify
26638 DECL to be a re-instantiation of TMPL with the same template
26639 arguments. TMPL should be the template into which tsubst'ing
26640 should occur for DECL, not the most general template.
26641
26642 One reason for doing this is a scenario like this:
26643
26644 template <class T>
26645 void f(const T&, int i);
26646
26647 void g() { f(3, 7); }
26648
26649 template <class T>
26650 void f(const T& t, const int i) { }
26651
26652 Note that when the template is first instantiated, with
26653 instantiate_template, the resulting DECL will have no name for the
26654 first parameter, and the wrong type for the second. So, when we go
26655 to instantiate the DECL, we regenerate it. */
26656
26657 static void
26658 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
26659 {
26660 /* The arguments used to instantiate DECL, from the most general
26661 template. */
26662 tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
26663
26664 /* Make sure that we can see identifiers, and compute access correctly. */
26665 push_access_scope (decl);
26666
26667 if (TREE_CODE (decl) == FUNCTION_DECL)
26668 {
26669 tree specs;
26670 int args_depth;
26671 int parms_depth;
26672
26673 /* Don't bother with this for unique friends that can't be redeclared and
26674 might change type if regenerated (PR69836). */
26675 if (DECL_UNIQUE_FRIEND_P (decl))
26676 goto done;
26677
26678 /* Use the source location of the definition. */
26679 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
26680
26681 args_depth = TMPL_ARGS_DEPTH (args);
26682 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
26683 if (args_depth > parms_depth)
26684 args = get_innermost_template_args (args, parms_depth);
26685
26686 /* Instantiate a dynamic exception-specification. noexcept will be
26687 handled below. */
26688 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
26689 if (TREE_VALUE (raises))
26690 {
26691 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
26692 args, tf_error, NULL_TREE,
26693 /*defer_ok*/false);
26694 if (specs && specs != error_mark_node)
26695 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
26696 specs);
26697 }
26698
26699 /* Merge parameter declarations. */
26700 if (tree pattern_parm
26701 = skip_artificial_parms_for (code_pattern,
26702 DECL_ARGUMENTS (code_pattern)))
26703 {
26704 tree *p = &DECL_ARGUMENTS (decl);
26705 for (int skip = num_artificial_parms_for (decl); skip; --skip)
26706 p = &DECL_CHAIN (*p);
26707 *p = tsubst_decl (pattern_parm, args, tf_error);
26708 for (tree t = *p; t; t = DECL_CHAIN (t))
26709 DECL_CONTEXT (t) = decl;
26710 }
26711
26712 if (DECL_CONTRACTS (decl))
26713 {
26714 /* If we're regenerating a specialization, the contracts will have
26715 been copied from the most general template. Replace those with
26716 the ones from the actual specialization. */
26717 tree tmpl = DECL_TI_TEMPLATE (decl);
26718 if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
26719 {
26720 remove_contract_attributes (decl);
26721 copy_contract_attributes (decl, code_pattern);
26722 }
26723
26724 tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
26725 }
26726
26727 /* Merge additional specifiers from the CODE_PATTERN. */
26728 if (DECL_DECLARED_INLINE_P (code_pattern)
26729 && !DECL_DECLARED_INLINE_P (decl))
26730 DECL_DECLARED_INLINE_P (decl) = 1;
26731
26732 maybe_instantiate_noexcept (decl, tf_error);
26733 }
26734 else if (VAR_P (decl))
26735 {
26736 start_lambda_scope (decl);
26737 DECL_INITIAL (decl) =
26738 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
26739 tf_error, DECL_TI_TEMPLATE (decl));
26740 finish_lambda_scope ();
26741 if (VAR_HAD_UNKNOWN_BOUND (decl))
26742 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
26743 tf_error, DECL_TI_TEMPLATE (decl));
26744 }
26745 else
26746 gcc_unreachable ();
26747
26748 done:
26749 pop_access_scope (decl);
26750 }
26751
26752 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26753 substituted to get DECL. */
26754
26755 tree
26756 template_for_substitution (tree decl)
26757 {
26758 tree tmpl = DECL_TI_TEMPLATE (decl);
26759
26760 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26761 for the instantiation. This is not always the most general
26762 template. Consider, for example:
26763
26764 template <class T>
26765 struct S { template <class U> void f();
26766 template <> void f<int>(); };
26767
26768 and an instantiation of S<double>::f<int>. We want TD to be the
26769 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26770 while (/* An instantiation cannot have a definition, so we need a
26771 more general template. */
26772 DECL_TEMPLATE_INSTANTIATION (tmpl)
26773 /* We must also deal with friend templates. Given:
26774
26775 template <class T> struct S {
26776 template <class U> friend void f() {};
26777 };
26778
26779 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26780 so far as the language is concerned, but that's still
26781 where we get the pattern for the instantiation from. On
26782 other hand, if the definition comes outside the class, say:
26783
26784 template <class T> struct S {
26785 template <class U> friend void f();
26786 };
26787 template <class U> friend void f() {}
26788
26789 we don't need to look any further. That's what the check for
26790 DECL_INITIAL is for. */
26791 || (TREE_CODE (decl) == FUNCTION_DECL
26792 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
26793 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
26794 {
26795 /* The present template, TD, should not be a definition. If it
26796 were a definition, we should be using it! Note that we
26797 cannot restructure the loop to just keep going until we find
26798 a template with a definition, since that might go too far if
26799 a specialization was declared, but not defined. */
26800
26801 /* Fetch the more general template. */
26802 tmpl = DECL_TI_TEMPLATE (tmpl);
26803 }
26804
26805 return tmpl;
26806 }
26807
26808 /* Returns true if we need to instantiate this template instance even if we
26809 know we aren't going to emit it. */
26810
26811 bool
26812 always_instantiate_p (tree decl)
26813 {
26814 /* We always instantiate inline functions so that we can inline them. An
26815 explicit instantiation declaration prohibits implicit instantiation of
26816 non-inline functions. With high levels of optimization, we would
26817 normally inline non-inline functions -- but we're not allowed to do
26818 that for "extern template" functions. Therefore, we check
26819 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26820 return ((TREE_CODE (decl) == FUNCTION_DECL
26821 && (DECL_DECLARED_INLINE_P (decl)
26822 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
26823 /* And we need to instantiate static data members so that
26824 their initializers are available in integral constant
26825 expressions. */
26826 || (VAR_P (decl)
26827 && decl_maybe_constant_var_p (decl)));
26828 }
26829
26830 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26831 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26832 error, true otherwise. */
26833
26834 bool
26835 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
26836 {
26837 if (fn == error_mark_node)
26838 return false;
26839
26840 /* Don't instantiate a noexcept-specification from template context. */
26841 if (processing_template_decl
26842 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
26843 return true;
26844
26845 tree fntype = TREE_TYPE (fn);
26846 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
26847
26848 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
26849 && DECL_MAYBE_DELETED (fn))
26850 {
26851 if (fn == current_function_decl)
26852 /* We're in start_preparsed_function, keep going. */
26853 return true;
26854
26855 ++function_depth;
26856 maybe_synthesize_method (fn);
26857 --function_depth;
26858 return !DECL_DELETED_FN (fn);
26859 }
26860
26861 if (!spec || !TREE_PURPOSE (spec))
26862 return true;
26863
26864 tree noex = TREE_PURPOSE (spec);
26865 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26866 && TREE_CODE (noex) != DEFERRED_PARSE)
26867 return true;
26868
26869 tree orig_fn = NULL_TREE;
26870 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26871 its FUNCTION_DECL for the rest of this function -- push_access_scope
26872 doesn't accept TEMPLATE_DECLs. */
26873 if (DECL_FUNCTION_TEMPLATE_P (fn))
26874 {
26875 orig_fn = fn;
26876 fn = DECL_TEMPLATE_RESULT (fn);
26877 }
26878
26879 if (DECL_CLONED_FUNCTION_P (fn))
26880 {
26881 tree prime = DECL_CLONED_FUNCTION (fn);
26882 if (!maybe_instantiate_noexcept (prime, complain))
26883 return false;
26884 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
26885 }
26886 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
26887 {
26888 static hash_set<tree>* fns = new hash_set<tree>;
26889 bool added = false;
26890 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
26891 {
26892 spec = get_defaulted_eh_spec (fn, complain);
26893 if (spec == error_mark_node)
26894 /* This might have failed because of an unparsed DMI, so
26895 let's try again later. */
26896 return false;
26897 }
26898 else if (!(added = !fns->add (fn)))
26899 {
26900 /* If hash_set::add returns true, the element was already there. */
26901 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
26902 DECL_SOURCE_LOCATION (fn));
26903 error_at (loc,
26904 "exception specification of %qD depends on itself",
26905 fn);
26906 spec = noexcept_false_spec;
26907 }
26908 else if (push_tinst_level (fn))
26909 {
26910 const bool push_to_top = maybe_push_to_top_level (fn);
26911 push_access_scope (fn);
26912 push_deferring_access_checks (dk_no_deferred);
26913 input_location = DECL_SOURCE_LOCATION (fn);
26914
26915 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
26916 && !DECL_LOCAL_DECL_P (fn))
26917 {
26918 /* If needed, set current_class_ptr for the benefit of
26919 tsubst_copy/PARM_DECL. */
26920 tree this_parm = DECL_ARGUMENTS (fn);
26921 current_class_ptr = NULL_TREE;
26922 current_class_ref = cp_build_fold_indirect_ref (this_parm);
26923 current_class_ptr = this_parm;
26924 }
26925
26926 /* If this function is represented by a TEMPLATE_DECL, then
26927 the deferred noexcept-specification might still contain
26928 dependent types, even after substitution. And we need the
26929 dependency check functions to work in build_noexcept_spec. */
26930 if (orig_fn)
26931 ++processing_template_decl;
26932
26933 /* Do deferred instantiation of the noexcept-specifier. */
26934 noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
26935 DEFERRED_NOEXCEPT_ARGS (noex),
26936 tf_warning_or_error, fn);
26937 /* Build up the noexcept-specification. */
26938 spec = build_noexcept_spec (noex, tf_warning_or_error);
26939
26940 if (orig_fn)
26941 --processing_template_decl;
26942
26943 pop_deferring_access_checks ();
26944 pop_access_scope (fn);
26945 pop_tinst_level ();
26946 maybe_pop_from_top_level (push_to_top);
26947 }
26948 else
26949 spec = noexcept_false_spec;
26950
26951 if (added)
26952 fns->remove (fn);
26953 }
26954
26955 if (spec == error_mark_node)
26956 {
26957 /* This failed with a hard error, so let's go with false. */
26958 gcc_assert (seen_error ());
26959 spec = noexcept_false_spec;
26960 }
26961
26962 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
26963 if (orig_fn)
26964 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
26965
26966 return true;
26967 }
26968
26969 /* We're starting to process the function INST, an instantiation of PATTERN;
26970 add their parameters to local_specializations. */
26971
26972 void
26973 register_parameter_specializations (tree pattern, tree inst)
26974 {
26975 tree tmpl_parm = DECL_ARGUMENTS (pattern);
26976 tree spec_parm = DECL_ARGUMENTS (inst);
26977 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
26978 {
26979 register_local_specialization (spec_parm, tmpl_parm);
26980 spec_parm = skip_artificial_parms_for (inst, spec_parm);
26981 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
26982 }
26983 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
26984 {
26985 if (!DECL_PACK_P (tmpl_parm))
26986 {
26987 register_local_specialization (spec_parm, tmpl_parm);
26988 spec_parm = DECL_CHAIN (spec_parm);
26989 }
26990 else
26991 {
26992 /* Register the (value) argument pack as a specialization of
26993 TMPL_PARM, then move on. */
26994 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
26995 register_local_specialization (argpack, tmpl_parm);
26996 }
26997 }
26998 gcc_assert (!spec_parm);
26999 }
27000
27001 /* Instantiate the body of D using PATTERN with ARGS. We have
27002 already determined PATTERN is the correct template to use.
27003 NESTED_P is true if this is a nested function, in which case
27004 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
27005
27006 static void
27007 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
27008 {
27009 tree td = NULL_TREE;
27010 tree code_pattern = pattern;
27011
27012 if (!nested_p)
27013 {
27014 td = pattern;
27015 code_pattern = DECL_TEMPLATE_RESULT (td);
27016 }
27017 else
27018 /* Only OMP reductions are nested. */
27019 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
27020
27021 vec<tree> omp_privatization_save;
27022 if (current_function_decl)
27023 save_omp_privatization_clauses (omp_privatization_save);
27024
27025 bool push_to_top = maybe_push_to_top_level (d);
27026
27027 mark_template_arguments_used (pattern, args);
27028
27029 if (VAR_P (d))
27030 {
27031 /* The variable might be a lambda's extra scope, and that
27032 lambda's visibility depends on D's. */
27033 maybe_commonize_var (d);
27034 determine_visibility (d);
27035 }
27036
27037 /* Mark D as instantiated so that recursive calls to
27038 instantiate_decl do not try to instantiate it again. */
27039 DECL_TEMPLATE_INSTANTIATED (d) = 1;
27040
27041 if (td)
27042 /* Regenerate the declaration in case the template has been modified
27043 by a subsequent redeclaration. */
27044 regenerate_decl_from_template (d, td, args);
27045
27046 /* We already set the file and line above. Reset them now in case
27047 they changed as a result of calling regenerate_decl_from_template. */
27048 input_location = DECL_SOURCE_LOCATION (d);
27049
27050 if (VAR_P (d))
27051 {
27052 /* Clear out DECL_RTL; whatever was there before may not be right
27053 since we've reset the type of the declaration. */
27054 SET_DECL_RTL (d, NULL);
27055 DECL_IN_AGGR_P (d) = 0;
27056
27057 /* The initializer is placed in DECL_INITIAL by
27058 regenerate_decl_from_template so we don't need to
27059 push/pop_access_scope again here. Pull it out so that
27060 cp_finish_decl can process it. */
27061 bool const_init = false;
27062 tree init = DECL_INITIAL (d);
27063 DECL_INITIAL (d) = NULL_TREE;
27064 DECL_INITIALIZED_P (d) = 0;
27065
27066 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
27067 initializer. That function will defer actual emission until
27068 we have a chance to determine linkage. */
27069 DECL_EXTERNAL (d) = 0;
27070
27071 /* Enter the scope of D so that access-checking works correctly. */
27072 bool enter_context = DECL_CLASS_SCOPE_P (d);
27073 if (enter_context)
27074 push_nested_class (DECL_CONTEXT (d));
27075
27076 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
27077 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
27078
27079 if (enter_context)
27080 pop_nested_class ();
27081 }
27082 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
27083 synthesize_method (d);
27084 else if (TREE_CODE (d) == FUNCTION_DECL)
27085 {
27086 /* Set up the list of local specializations. */
27087 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
27088 tree block = NULL_TREE;
27089
27090 /* Set up context. */
27091 if (nested_p)
27092 block = push_stmt_list ();
27093 else
27094 {
27095 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
27096
27097 perform_instantiation_time_access_checks (code_pattern, args);
27098 }
27099
27100 /* Create substitution entries for the parameters. */
27101 register_parameter_specializations (code_pattern, d);
27102
27103 /* Substitute into the body of the function. */
27104 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27105 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
27106 tf_warning_or_error, d);
27107 else
27108 {
27109 tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
27110 tf_warning_or_error, DECL_TI_TEMPLATE (d));
27111
27112 /* Set the current input_location to the end of the function
27113 so that finish_function knows where we are. */
27114 input_location
27115 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
27116
27117 /* Remember if we saw an infinite loop in the template. */
27118 current_function_infinite_loop
27119 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
27120 }
27121
27122 /* Finish the function. */
27123 if (nested_p)
27124 DECL_SAVED_TREE (d) = pop_stmt_list (block);
27125 else
27126 {
27127 d = finish_function (/*inline_p=*/false);
27128 expand_or_defer_fn (d);
27129 }
27130
27131 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
27132 cp_check_omp_declare_reduction (d);
27133 }
27134
27135 /* We're not deferring instantiation any more. */
27136 if (!nested_p)
27137 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
27138
27139 maybe_pop_from_top_level (push_to_top);
27140
27141 if (current_function_decl)
27142 restore_omp_privatization_clauses (omp_privatization_save);
27143 }
27144
27145 /* Produce the definition of D, a _DECL generated from a template. If
27146 DEFER_OK is true, then we don't have to actually do the
27147 instantiation now; we just have to do it sometime. Normally it is
27148 an error if this is an explicit instantiation but D is undefined.
27149 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
27150 instantiated class template. */
27151
27152 tree
27153 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
27154 {
27155 tree tmpl = DECL_TI_TEMPLATE (d);
27156 tree gen_args;
27157 tree args;
27158 tree td;
27159 tree code_pattern;
27160 tree spec;
27161 tree gen_tmpl;
27162 bool pattern_defined;
27163 location_t saved_loc = input_location;
27164 bool external_p;
27165 bool deleted_p;
27166
27167 /* This function should only be used to instantiate templates for
27168 functions and static member variables. */
27169 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
27170
27171 /* A concept is never instantiated. */
27172 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
27173
27174 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
27175
27176 if (modules_p ())
27177 /* We may have a pending instantiation of D itself. */
27178 lazy_load_pendings (d);
27179
27180 /* Variables are never deferred; if instantiation is required, they
27181 are instantiated right away. That allows for better code in the
27182 case that an expression refers to the value of the variable --
27183 if the variable has a constant value the referring expression can
27184 take advantage of that fact. */
27185 if (VAR_P (d))
27186 defer_ok = false;
27187
27188 /* Don't instantiate cloned functions. Instead, instantiate the
27189 functions they cloned. */
27190 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
27191 d = DECL_CLONED_FUNCTION (d);
27192
27193 if (DECL_TEMPLATE_INSTANTIATED (d)
27194 || TREE_TYPE (d) == error_mark_node
27195 || (TREE_CODE (d) == FUNCTION_DECL
27196 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
27197 || DECL_TEMPLATE_SPECIALIZATION (d))
27198 /* D has already been instantiated or explicitly specialized, so
27199 there's nothing for us to do here.
27200
27201 It might seem reasonable to check whether or not D is an explicit
27202 instantiation, and, if so, stop here. But when an explicit
27203 instantiation is deferred until the end of the compilation,
27204 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
27205 the instantiation. */
27206 return d;
27207
27208 /* Check to see whether we know that this template will be
27209 instantiated in some other file, as with "extern template"
27210 extension. */
27211 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
27212
27213 /* In general, we do not instantiate such templates. */
27214 if (external_p && !always_instantiate_p (d))
27215 return d;
27216
27217 gen_tmpl = most_general_template (tmpl);
27218 gen_args = DECL_TI_ARGS (d);
27219
27220 /* We should already have the extra args. */
27221 gcc_checking_assert (tmpl == gen_tmpl
27222 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
27223 == TMPL_ARGS_DEPTH (gen_args)));
27224 /* And what's in the hash table should match D. */
27225 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
27226 == d
27227 || spec == NULL_TREE);
27228
27229 /* This needs to happen before any tsubsting. */
27230 if (! push_tinst_level (d))
27231 return d;
27232
27233 auto_timevar tv (TV_TEMPLATE_INST);
27234
27235 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
27236 for the instantiation. */
27237 td = template_for_substitution (d);
27238 args = gen_args;
27239
27240 if (variable_template_specialization_p (d))
27241 {
27242 /* Look up an explicit specialization, if any. */
27243 tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
27244 if (partial_ti && partial_ti != error_mark_node)
27245 {
27246 td = TI_TEMPLATE (partial_ti);
27247 args = TI_ARGS (partial_ti);
27248 }
27249 }
27250
27251 code_pattern = DECL_TEMPLATE_RESULT (td);
27252
27253 /* We should never be trying to instantiate a member of a class
27254 template or partial specialization. */
27255 gcc_assert (d != code_pattern);
27256
27257 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
27258 || DECL_TEMPLATE_SPECIALIZATION (td))
27259 /* In the case of a friend template whose definition is provided
27260 outside the class, we may have too many arguments. Drop the
27261 ones we don't need. The same is true for specializations. */
27262 args = get_innermost_template_args
27263 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
27264
27265 if (TREE_CODE (d) == FUNCTION_DECL)
27266 {
27267 deleted_p = DECL_DELETED_FN (code_pattern);
27268 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
27269 && DECL_INITIAL (code_pattern) != error_mark_node)
27270 || DECL_DEFAULTED_FN (code_pattern)
27271 || deleted_p);
27272 }
27273 else
27274 {
27275 deleted_p = false;
27276 if (DECL_CLASS_SCOPE_P (code_pattern))
27277 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
27278 else
27279 pattern_defined = ! DECL_EXTERNAL (code_pattern);
27280 }
27281
27282 /* We may be in the middle of deferred access check. Disable it now. */
27283 push_deferring_access_checks (dk_no_deferred);
27284
27285 /* Unless an explicit instantiation directive has already determined
27286 the linkage of D, remember that a definition is available for
27287 this entity. */
27288 if (pattern_defined
27289 && !DECL_INTERFACE_KNOWN (d)
27290 && !DECL_NOT_REALLY_EXTERN (d))
27291 mark_definable (d);
27292
27293 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
27294 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
27295 input_location = DECL_SOURCE_LOCATION (d);
27296
27297 /* If D is a member of an explicitly instantiated class template,
27298 and no definition is available, treat it like an implicit
27299 instantiation. */
27300 if (!pattern_defined && expl_inst_class_mem_p
27301 && DECL_EXPLICIT_INSTANTIATION (d))
27302 {
27303 /* Leave linkage flags alone on instantiations with anonymous
27304 visibility. */
27305 if (TREE_PUBLIC (d))
27306 {
27307 DECL_NOT_REALLY_EXTERN (d) = 0;
27308 DECL_INTERFACE_KNOWN (d) = 0;
27309 }
27310 SET_DECL_IMPLICIT_INSTANTIATION (d);
27311 }
27312
27313 /* Defer all other templates, unless we have been explicitly
27314 forbidden from doing so. */
27315 if (/* If there is no definition, we cannot instantiate the
27316 template. */
27317 ! pattern_defined
27318 /* If it's OK to postpone instantiation, do so. */
27319 || defer_ok
27320 /* If this is a static data member that will be defined
27321 elsewhere, we don't want to instantiate the entire data
27322 member, but we do want to instantiate the initializer so that
27323 we can substitute that elsewhere. */
27324 || (external_p && VAR_P (d))
27325 /* Handle here a deleted function too, avoid generating
27326 its body (c++/61080). */
27327 || deleted_p)
27328 {
27329 /* The definition of the static data member is now required so
27330 we must substitute the initializer. */
27331 if (VAR_P (d)
27332 && !DECL_INITIAL (d)
27333 && DECL_INITIAL (code_pattern))
27334 {
27335 tree ns;
27336 tree init;
27337 bool const_init = false;
27338 bool enter_context = DECL_CLASS_SCOPE_P (d);
27339
27340 ns = decl_namespace_context (d);
27341 push_nested_namespace (ns);
27342 if (enter_context)
27343 push_nested_class (DECL_CONTEXT (d));
27344 init = tsubst_expr (DECL_INITIAL (code_pattern),
27345 args,
27346 tf_warning_or_error, NULL_TREE);
27347 /* If instantiating the initializer involved instantiating this
27348 again, don't call cp_finish_decl twice. */
27349 if (!DECL_INITIAL (d))
27350 {
27351 /* Make sure the initializer is still constant, in case of
27352 circular dependency (template/instantiate6.C). */
27353 const_init
27354 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
27355 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
27356 /*asmspec_tree=*/NULL_TREE, 0);
27357 }
27358 if (enter_context)
27359 pop_nested_class ();
27360 pop_nested_namespace (ns);
27361 }
27362
27363 /* We restore the source position here because it's used by
27364 add_pending_template. */
27365 input_location = saved_loc;
27366
27367 if (at_eof && !pattern_defined
27368 && DECL_EXPLICIT_INSTANTIATION (d)
27369 && DECL_NOT_REALLY_EXTERN (d))
27370 /* [temp.explicit]
27371
27372 The definition of a non-exported function template, a
27373 non-exported member function template, or a non-exported
27374 member function or static data member of a class template
27375 shall be present in every translation unit in which it is
27376 explicitly instantiated. */
27377 permerror (input_location, "explicit instantiation of %qD "
27378 "but no definition available", d);
27379
27380 /* If we're in unevaluated context, we just wanted to get the
27381 constant value; this isn't an odr use, so don't queue
27382 a full instantiation. */
27383 if (!cp_unevaluated_operand
27384 /* ??? Historically, we have instantiated inline functions, even
27385 when marked as "extern template". */
27386 && !(external_p && VAR_P (d)))
27387 add_pending_template (d);
27388 }
27389 else
27390 {
27391 set_instantiating_module (d);
27392 if (variable_template_p (gen_tmpl))
27393 note_vague_linkage_variable (d);
27394 instantiate_body (td, args, d, false);
27395 }
27396
27397 pop_deferring_access_checks ();
27398 pop_tinst_level ();
27399 input_location = saved_loc;
27400
27401 return d;
27402 }
27403
27404 /* Run through the list of templates that we wish we could
27405 instantiate, and instantiate any we can. RETRIES is the
27406 number of times we retry pending template instantiation. */
27407
27408 void
27409 instantiate_pending_templates (int retries)
27410 {
27411 int reconsider;
27412 location_t saved_loc = input_location;
27413
27414 /* Instantiating templates may trigger vtable generation. This in turn
27415 may require further template instantiations. We place a limit here
27416 to avoid infinite loop. */
27417 if (pending_templates && retries >= max_tinst_depth)
27418 {
27419 tree decl = pending_templates->tinst->maybe_get_node ();
27420
27421 fatal_error (input_location,
27422 "template instantiation depth exceeds maximum of %d"
27423 " instantiating %q+D, possibly from virtual table generation"
27424 " (use %<-ftemplate-depth=%> to increase the maximum)",
27425 max_tinst_depth, decl);
27426 if (TREE_CODE (decl) == FUNCTION_DECL)
27427 /* Pretend that we defined it. */
27428 DECL_INITIAL (decl) = error_mark_node;
27429 return;
27430 }
27431
27432 do
27433 {
27434 struct pending_template **t = &pending_templates;
27435 struct pending_template *last = NULL;
27436 reconsider = 0;
27437 while (*t)
27438 {
27439 tree instantiation = reopen_tinst_level ((*t)->tinst);
27440 bool complete = false;
27441
27442 if (TYPE_P (instantiation))
27443 {
27444 if (!COMPLETE_TYPE_P (instantiation))
27445 {
27446 instantiate_class_template (instantiation);
27447 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
27448 for (tree fld = TYPE_FIELDS (instantiation);
27449 fld; fld = TREE_CHAIN (fld))
27450 if ((VAR_P (fld)
27451 || (TREE_CODE (fld) == FUNCTION_DECL
27452 && !DECL_ARTIFICIAL (fld)))
27453 && DECL_TEMPLATE_INSTANTIATION (fld))
27454 instantiate_decl (fld,
27455 /*defer_ok=*/false,
27456 /*expl_inst_class_mem_p=*/false);
27457
27458 if (COMPLETE_TYPE_P (instantiation))
27459 reconsider = 1;
27460 }
27461
27462 complete = COMPLETE_TYPE_P (instantiation);
27463 }
27464 else
27465 {
27466 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
27467 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
27468 {
27469 instantiation
27470 = instantiate_decl (instantiation,
27471 /*defer_ok=*/false,
27472 /*expl_inst_class_mem_p=*/false);
27473 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
27474 reconsider = 1;
27475 }
27476
27477 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
27478 || DECL_TEMPLATE_INSTANTIATED (instantiation));
27479 }
27480
27481 if (complete)
27482 {
27483 /* If INSTANTIATION has been instantiated, then we don't
27484 need to consider it again in the future. */
27485 struct pending_template *drop = *t;
27486 *t = (*t)->next;
27487 set_refcount_ptr (drop->tinst);
27488 pending_template_freelist ().free (drop);
27489 }
27490 else
27491 {
27492 last = *t;
27493 t = &(*t)->next;
27494 }
27495 tinst_depth = 0;
27496 set_refcount_ptr (current_tinst_level);
27497 }
27498 last_pending_template = last;
27499 }
27500 while (reconsider);
27501
27502 input_location = saved_loc;
27503 }
27504
27505 /* Substitute ARGVEC into T, which is a list of initializers for
27506 either base class or a non-static data member. The TREE_PURPOSEs
27507 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27508 instantiate_decl. */
27509
27510 static tree
27511 tsubst_initializer_list (tree t, tree argvec)
27512 {
27513 tree inits = NULL_TREE;
27514 tree target_ctor = error_mark_node;
27515
27516 for (; t; t = TREE_CHAIN (t))
27517 {
27518 tree decl;
27519 tree init;
27520 tree expanded_bases = NULL_TREE;
27521 tree expanded_arguments = NULL_TREE;
27522 int i, len = 1;
27523
27524 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
27525 {
27526 tree expr;
27527 tree arg;
27528
27529 /* Expand the base class expansion type into separate base
27530 classes. */
27531 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
27532 tf_warning_or_error,
27533 NULL_TREE);
27534 if (expanded_bases == error_mark_node)
27535 continue;
27536
27537 /* We'll be building separate TREE_LISTs of arguments for
27538 each base. */
27539 len = TREE_VEC_LENGTH (expanded_bases);
27540 expanded_arguments = make_tree_vec (len);
27541 for (i = 0; i < len; i++)
27542 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
27543
27544 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27545 expand each argument in the TREE_VALUE of t. */
27546 expr = make_node (EXPR_PACK_EXPANSION);
27547 PACK_EXPANSION_LOCAL_P (expr) = true;
27548 PACK_EXPANSION_PARAMETER_PACKS (expr) =
27549 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
27550
27551 if (TREE_VALUE (t) == void_type_node)
27552 /* VOID_TYPE_NODE is used to indicate
27553 value-initialization. */
27554 {
27555 for (i = 0; i < len; i++)
27556 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
27557 }
27558 else
27559 {
27560 /* Substitute parameter packs into each argument in the
27561 TREE_LIST. */
27562 in_base_initializer = 1;
27563 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
27564 {
27565 tree expanded_exprs;
27566
27567 /* Expand the argument. */
27568 tree value;
27569 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27570 value = TREE_VALUE (arg);
27571 else
27572 {
27573 value = expr;
27574 PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
27575 }
27576 expanded_exprs
27577 = tsubst_pack_expansion (value, argvec,
27578 tf_warning_or_error,
27579 NULL_TREE);
27580 if (expanded_exprs == error_mark_node)
27581 continue;
27582
27583 /* Prepend each of the expanded expressions to the
27584 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27585 for (i = 0; i < len; i++)
27586 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27587 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
27588 TREE_VEC_ELT (expanded_arguments, i)
27589 = tree_cons (NULL_TREE,
27590 TREE_VEC_ELT (expanded_exprs, j),
27591 TREE_VEC_ELT (expanded_arguments, i));
27592 else
27593 TREE_VEC_ELT (expanded_arguments, i)
27594 = tree_cons (NULL_TREE,
27595 TREE_VEC_ELT (expanded_exprs, i),
27596 TREE_VEC_ELT (expanded_arguments, i));
27597 }
27598 in_base_initializer = 0;
27599
27600 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27601 since we built them backwards. */
27602 for (i = 0; i < len; i++)
27603 {
27604 TREE_VEC_ELT (expanded_arguments, i) =
27605 nreverse (TREE_VEC_ELT (expanded_arguments, i));
27606 }
27607 }
27608 }
27609
27610 for (i = 0; i < len; ++i)
27611 {
27612 if (expanded_bases)
27613 {
27614 decl = TREE_VEC_ELT (expanded_bases, i);
27615 decl = expand_member_init (decl);
27616 init = TREE_VEC_ELT (expanded_arguments, i);
27617 }
27618 else
27619 {
27620 tree tmp;
27621 if (TYPE_P (TREE_PURPOSE (t)))
27622 decl = tsubst (TREE_PURPOSE (t), argvec,
27623 tf_warning_or_error, NULL_TREE);
27624 else
27625 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
27626 tf_warning_or_error, NULL_TREE);
27627
27628 decl = expand_member_init (decl);
27629 if (decl && !DECL_P (decl))
27630 in_base_initializer = 1;
27631
27632 init = TREE_VALUE (t);
27633 tmp = init;
27634 if (init != void_type_node)
27635 init = tsubst_expr (init, argvec,
27636 tf_warning_or_error, NULL_TREE);
27637 if (init == NULL_TREE && tmp != NULL_TREE)
27638 /* If we had an initializer but it instantiated to nothing,
27639 value-initialize the object. This will only occur when
27640 the initializer was a pack expansion where the parameter
27641 packs used in that expansion were of length zero. */
27642 init = void_type_node;
27643 in_base_initializer = 0;
27644 }
27645
27646 if (target_ctor != error_mark_node
27647 && init != error_mark_node)
27648 {
27649 error ("mem-initializer for %qD follows constructor delegation",
27650 decl);
27651 return inits;
27652 }
27653 /* Look for a target constructor. */
27654 if (init != error_mark_node
27655 && decl && CLASS_TYPE_P (decl)
27656 && same_type_p (decl, current_class_type))
27657 {
27658 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
27659 if (inits)
27660 {
27661 error ("constructor delegation follows mem-initializer for %qD",
27662 TREE_PURPOSE (inits));
27663 continue;
27664 }
27665 target_ctor = init;
27666 }
27667
27668 if (decl)
27669 {
27670 init = build_tree_list (decl, init);
27671 /* Carry over the dummy TREE_TYPE node containing the source
27672 location. */
27673 TREE_TYPE (init) = TREE_TYPE (t);
27674 TREE_CHAIN (init) = inits;
27675 inits = init;
27676 }
27677 }
27678 }
27679 return inits;
27680 }
27681
27682 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
27683 is the instantiation (which should have been created with
27684 start_enum) and ARGS are the template arguments to use. */
27685
27686 static void
27687 tsubst_enum (tree tag, tree newtag, tree args)
27688 {
27689 tree e;
27690
27691 if (SCOPED_ENUM_P (newtag))
27692 begin_scope (sk_scoped_enum, newtag);
27693
27694 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
27695 {
27696 tree value;
27697 tree decl = TREE_VALUE (e);
27698
27699 /* Note that in a template enum, the TREE_VALUE is the
27700 CONST_DECL, not the corresponding INTEGER_CST. */
27701 value = tsubst_expr (DECL_INITIAL (decl),
27702 args, tf_warning_or_error, NULL_TREE);
27703
27704 /* Give this enumeration constant the correct access. */
27705 set_current_access_from_decl (decl);
27706
27707 /* Actually build the enumerator itself. Here we're assuming that
27708 enumerators can't have dependent attributes. */
27709 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
27710 DECL_ATTRIBUTES (decl),
27711 DECL_SOURCE_LOCATION (decl));
27712 /* Attribute deprecated without an argument isn't sticky: it'll
27713 melt into a tree flag, so we need to propagate the flag here,
27714 since we just created a new enumerator. */
27715 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
27716 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
27717 }
27718
27719 if (SCOPED_ENUM_P (newtag))
27720 finish_scope ();
27721
27722 finish_enum_value_list (newtag);
27723 finish_enum (newtag);
27724
27725 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
27726 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
27727 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
27728 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
27729 }
27730
27731 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27732 its type -- but without substituting the innermost set of template
27733 arguments. So, innermost set of template parameters will appear in
27734 the type. */
27735
27736 tree
27737 get_mostly_instantiated_function_type (tree decl)
27738 {
27739 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27740 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
27741 }
27742
27743 /* Return truthvalue if we're processing a template different from
27744 the last one involved in diagnostics. */
27745 bool
27746 problematic_instantiation_changed (void)
27747 {
27748 return current_tinst_level != last_error_tinst_level;
27749 }
27750
27751 /* Remember current template involved in diagnostics. */
27752 void
27753 record_last_problematic_instantiation (void)
27754 {
27755 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
27756 }
27757
27758 struct tinst_level *
27759 current_instantiation (void)
27760 {
27761 return current_tinst_level;
27762 }
27763
27764 /* Return TRUE if current_function_decl is being instantiated, false
27765 otherwise. */
27766
27767 bool
27768 instantiating_current_function_p (void)
27769 {
27770 return (current_instantiation ()
27771 && (current_instantiation ()->maybe_get_node ()
27772 == current_function_decl));
27773 }
27774
27775 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27776 type. Return false for ok, true for disallowed. Issue error and
27777 inform messages under control of COMPLAIN. */
27778
27779 static bool
27780 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
27781 {
27782 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
27783 return false;
27784 else if (TYPE_PTR_P (type))
27785 return false;
27786 else if (TYPE_REF_P (type)
27787 && !TYPE_REF_IS_RVALUE (type))
27788 return false;
27789 else if (TYPE_PTRMEM_P (type))
27790 return false;
27791 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
27792 {
27793 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
27794 {
27795 if (complain & tf_error)
27796 error ("non-type template parameters of deduced class type only "
27797 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27798 return true;
27799 }
27800 return false;
27801 }
27802 else if (TREE_CODE (type) == NULLPTR_TYPE)
27803 return false;
27804 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
27805 && cxx_dialect < cxx11)
27806 /* Fall through; before C++11 alias templates, a bound ttp
27807 always instantiates into a class type. */;
27808 else if (WILDCARD_TYPE_P (type))
27809 /* Any other wildcard type not already handled above is allowed. */
27810 return false;
27811 else if (TREE_CODE (type) == COMPLEX_TYPE)
27812 /* Fall through. */;
27813 else if (VOID_TYPE_P (type))
27814 /* Fall through. */;
27815 else if (cxx_dialect >= cxx20)
27816 {
27817 if (dependent_type_p (type))
27818 return false;
27819 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
27820 return true;
27821 if (structural_type_p (type))
27822 return false;
27823 if (complain & tf_error)
27824 {
27825 auto_diagnostic_group d;
27826 error ("%qT is not a valid type for a template non-type "
27827 "parameter because it is not structural", type);
27828 structural_type_p (type, true);
27829 }
27830 return true;
27831 }
27832 else if (CLASS_TYPE_P (type))
27833 {
27834 if (complain & tf_error)
27835 error ("non-type template parameters of class type only available "
27836 "with %<-std=c++20%> or %<-std=gnu++20%>");
27837 return true;
27838 }
27839
27840 if (complain & tf_error)
27841 {
27842 if (type == error_mark_node)
27843 inform (input_location, "invalid template non-type parameter");
27844 else
27845 error ("%q#T is not a valid type for a template non-type parameter",
27846 type);
27847 }
27848 return true;
27849 }
27850
27851 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27852
27853 static bool
27854 value_dependent_noexcept_spec_p (tree type)
27855 {
27856 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
27857 if (tree noex = TREE_PURPOSE (spec))
27858 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27859 affect overload resolution and treating it as dependent breaks
27860 things. Same for an unparsed noexcept expression. */
27861 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
27862 && TREE_CODE (noex) != DEFERRED_PARSE
27863 && value_dependent_expression_p (noex))
27864 return true;
27865
27866 return false;
27867 }
27868
27869 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27870 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27871
27872 static bool
27873 dependent_type_p_r (tree type)
27874 {
27875 tree scope;
27876
27877 /* [temp.dep.type]
27878
27879 A type is dependent if it is:
27880
27881 -- a template parameter. Template template parameters are types
27882 for us (since TYPE_P holds true for them) so we handle
27883 them here. */
27884 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27885 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
27886 return true;
27887 /* -- a qualified-id with a nested-name-specifier which contains a
27888 class-name that names a dependent type or whose unqualified-id
27889 names a dependent type. */
27890 if (TREE_CODE (type) == TYPENAME_TYPE)
27891 return true;
27892
27893 /* An alias template specialization can be dependent even if the
27894 resulting type is not. */
27895 if (dependent_alias_template_spec_p (type, nt_transparent))
27896 return true;
27897
27898 /* -- a cv-qualified type where the cv-unqualified type is
27899 dependent.
27900 No code is necessary for this bullet; the code below handles
27901 cv-qualified types, and we don't want to strip aliases with
27902 TYPE_MAIN_VARIANT because of DR 1558. */
27903 /* -- a compound type constructed from any dependent type. */
27904 if (TYPE_PTRMEM_P (type))
27905 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
27906 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27907 (type)));
27908 else if (INDIRECT_TYPE_P (type))
27909 return dependent_type_p (TREE_TYPE (type));
27910 else if (FUNC_OR_METHOD_TYPE_P (type))
27911 {
27912 tree arg_type;
27913
27914 if (dependent_type_p (TREE_TYPE (type)))
27915 return true;
27916 for (arg_type = TYPE_ARG_TYPES (type);
27917 arg_type;
27918 arg_type = TREE_CHAIN (arg_type))
27919 if (dependent_type_p (TREE_VALUE (arg_type)))
27920 return true;
27921 if (cxx_dialect >= cxx17
27922 && value_dependent_noexcept_spec_p (type))
27923 /* A value-dependent noexcept-specifier makes the type dependent. */
27924 return true;
27925 return false;
27926 }
27927 /* -- an array type constructed from any dependent type or whose
27928 size is specified by a constant expression that is
27929 value-dependent.
27930
27931 We checked for type- and value-dependence of the bounds in
27932 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27933 if (TREE_CODE (type) == ARRAY_TYPE)
27934 {
27935 if (TYPE_DOMAIN (type)
27936 && dependent_type_p (TYPE_DOMAIN (type)))
27937 return true;
27938 return dependent_type_p (TREE_TYPE (type));
27939 }
27940
27941 /* -- a template-id in which either the template name is a template
27942 parameter ... */
27943 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
27944 return true;
27945 /* ... or any of the template arguments is a dependent type or
27946 an expression that is type-dependent or value-dependent. */
27947 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
27948 && (any_dependent_template_arguments_p
27949 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
27950 return true;
27951
27952 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27953 dependent; if the argument of the `typeof' expression is not
27954 type-dependent, then it should already been have resolved. */
27955 if (TREE_CODE (type) == TYPEOF_TYPE
27956 || TREE_CODE (type) == DECLTYPE_TYPE
27957 || TREE_CODE (type) == TRAIT_TYPE)
27958 return true;
27959
27960 /* A template argument pack is dependent if any of its packed
27961 arguments are. */
27962 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
27963 {
27964 tree args = ARGUMENT_PACK_ARGS (type);
27965 for (tree arg : tree_vec_range (args))
27966 if (dependent_template_arg_p (arg))
27967 return true;
27968 }
27969
27970 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27971 be template parameters. */
27972 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
27973 return true;
27974
27975 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
27976 return true;
27977
27978 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
27979 return true;
27980
27981 /* The standard does not specifically mention types that are local
27982 to template functions or local classes, but they should be
27983 considered dependent too. For example:
27984
27985 template <int I> void f() {
27986 enum E { a = I };
27987 S<sizeof (E)> s;
27988 }
27989
27990 The size of `E' cannot be known until the value of `I' has been
27991 determined. Therefore, `E' must be considered dependent. */
27992 scope = TYPE_CONTEXT (type);
27993 if (scope && TYPE_P (scope))
27994 return dependent_type_p (scope);
27995 /* Don't use type_dependent_expression_p here, as it can lead
27996 to infinite recursion trying to determine whether a lambda
27997 nested in a lambda is dependent (c++/47687). */
27998 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
27999 && DECL_LANG_SPECIFIC (scope)
28000 && DECL_TEMPLATE_INFO (scope)
28001 && (any_dependent_template_arguments_p
28002 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
28003 return true;
28004
28005 /* Other types are non-dependent. */
28006 return false;
28007 }
28008
28009 /* Returns TRUE if TYPE is dependent, in the sense of
28010 [temp.dep.type]. Note that a NULL type is considered dependent. */
28011
28012 bool
28013 dependent_type_p (tree type)
28014 {
28015 /* If there are no template parameters in scope, then there can't be
28016 any dependent types. */
28017 if (!processing_template_decl)
28018 {
28019 /* If we are not processing a template, then nobody should be
28020 providing us with a dependent type. */
28021 gcc_assert (type);
28022 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
28023 return false;
28024 }
28025
28026 /* If the type is NULL, we have not computed a type for the entity
28027 in question; in that case, the type is dependent. */
28028 if (!type)
28029 return true;
28030
28031 /* Erroneous types can be considered non-dependent. */
28032 if (type == error_mark_node)
28033 return false;
28034
28035 /* If we have not already computed the appropriate value for TYPE,
28036 do so now. */
28037 if (!TYPE_DEPENDENT_P_VALID (type))
28038 {
28039 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
28040 TYPE_DEPENDENT_P_VALID (type) = 1;
28041 }
28042
28043 return TYPE_DEPENDENT_P (type);
28044 }
28045
28046 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
28047 lookup. In other words, a dependent type that is not the current
28048 instantiation. */
28049
28050 bool
28051 dependent_scope_p (tree scope)
28052 {
28053 return (scope && TYPE_P (scope) && dependent_type_p (scope)
28054 && !currently_open_class (scope));
28055 }
28056
28057 /* True if we might find more declarations in SCOPE during instantiation than
28058 we can when parsing the template. */
28059
28060 bool
28061 dependentish_scope_p (tree scope)
28062 {
28063 return dependent_scope_p (scope) || any_dependent_bases_p (scope);
28064 }
28065
28066 /* T is a SCOPE_REF. Return whether it represents a non-static member of
28067 an unknown base of 'this' (and is therefore instantiation-dependent). */
28068
28069 static bool
28070 unknown_base_ref_p (tree t)
28071 {
28072 if (!current_class_ptr)
28073 return false;
28074
28075 tree mem = TREE_OPERAND (t, 1);
28076 if (shared_member_p (mem))
28077 return false;
28078
28079 tree cur = current_nonlambda_class_type ();
28080 if (!any_dependent_bases_p (cur))
28081 return false;
28082
28083 tree ctx = TREE_OPERAND (t, 0);
28084 if (DERIVED_FROM_P (ctx, cur))
28085 return false;
28086
28087 return true;
28088 }
28089
28090 /* T is a SCOPE_REF; return whether we need to consider it
28091 instantiation-dependent so that we can check access at instantiation
28092 time even though we know which member it resolves to. */
28093
28094 static bool
28095 instantiation_dependent_scope_ref_p (tree t)
28096 {
28097 if (DECL_P (TREE_OPERAND (t, 1))
28098 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
28099 && !dependent_scope_p (TREE_OPERAND (t, 0))
28100 && !unknown_base_ref_p (t)
28101 && accessible_in_template_p (TREE_OPERAND (t, 0),
28102 TREE_OPERAND (t, 1)))
28103 return false;
28104 else
28105 return true;
28106 }
28107
28108 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
28109 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
28110 expression. */
28111
28112 /* Note that this predicate is not appropriate for general expressions;
28113 only constant expressions (that satisfy potential_constant_expression)
28114 can be tested for value dependence. */
28115
28116 bool
28117 value_dependent_expression_p (tree expression)
28118 {
28119 if (!processing_template_decl || expression == NULL_TREE)
28120 return false;
28121
28122 /* A type-dependent expression is also value-dependent. */
28123 if (type_dependent_expression_p (expression))
28124 return true;
28125
28126 switch (TREE_CODE (expression))
28127 {
28128 case BASELINK:
28129 /* A dependent member function of the current instantiation. */
28130 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
28131
28132 case FUNCTION_DECL:
28133 /* A dependent member function of the current instantiation. */
28134 if (DECL_CLASS_SCOPE_P (expression)
28135 && dependent_type_p (DECL_CONTEXT (expression)))
28136 return true;
28137 break;
28138
28139 case IDENTIFIER_NODE:
28140 /* A name that has not been looked up -- must be dependent. */
28141 return true;
28142
28143 case TEMPLATE_PARM_INDEX:
28144 /* A non-type template parm. */
28145 return true;
28146
28147 case CONST_DECL:
28148 /* A non-type template parm. */
28149 if (DECL_TEMPLATE_PARM_P (expression))
28150 return true;
28151 return value_dependent_expression_p (DECL_INITIAL (expression));
28152
28153 case VAR_DECL:
28154 /* A constant with literal type and is initialized
28155 with an expression that is value-dependent. */
28156 if (DECL_DEPENDENT_INIT_P (expression))
28157 return true;
28158 if (DECL_HAS_VALUE_EXPR_P (expression))
28159 {
28160 tree value_expr = DECL_VALUE_EXPR (expression);
28161 if (value_dependent_expression_p (value_expr)
28162 /* __PRETTY_FUNCTION__ inside a template function is dependent
28163 on the name of the function. */
28164 || (DECL_PRETTY_FUNCTION_P (expression)
28165 /* It might be used in a template, but not a template
28166 function, in which case its DECL_VALUE_EXPR will be
28167 "top level". */
28168 && value_expr == error_mark_node))
28169 return true;
28170 }
28171 else if (TYPE_REF_P (TREE_TYPE (expression)))
28172 /* FIXME cp_finish_decl doesn't fold reference initializers. */
28173 return true;
28174 /* We have a constexpr variable and we're processing a template. When
28175 there's lifetime extension involved (for which finish_compound_literal
28176 used to create a temporary), we'll not be able to evaluate the
28177 variable until instantiating, so pretend it's value-dependent. */
28178 else if (DECL_DECLARED_CONSTEXPR_P (expression)
28179 && !TREE_CONSTANT (expression))
28180 return true;
28181 return false;
28182
28183 case DYNAMIC_CAST_EXPR:
28184 case STATIC_CAST_EXPR:
28185 case CONST_CAST_EXPR:
28186 case REINTERPRET_CAST_EXPR:
28187 case CAST_EXPR:
28188 case IMPLICIT_CONV_EXPR:
28189 /* These expressions are value-dependent if the type to which
28190 the cast occurs is dependent or the expression being casted
28191 is value-dependent. */
28192 {
28193 tree type = TREE_TYPE (expression);
28194
28195 if (dependent_type_p (type))
28196 return true;
28197
28198 /* A functional cast has a list of operands. */
28199 expression = TREE_OPERAND (expression, 0);
28200 if (!expression)
28201 {
28202 /* If there are no operands, it must be an expression such
28203 as "int()". This should not happen for aggregate types
28204 because it would form non-constant expressions. */
28205 gcc_assert (cxx_dialect >= cxx11
28206 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
28207
28208 return false;
28209 }
28210
28211 if (TREE_CODE (expression) == TREE_LIST)
28212 return any_value_dependent_elements_p (expression);
28213
28214 if (TREE_CODE (type) == REFERENCE_TYPE
28215 && has_value_dependent_address (expression))
28216 return true;
28217
28218 return value_dependent_expression_p (expression);
28219 }
28220
28221 case SIZEOF_EXPR:
28222 if (SIZEOF_EXPR_TYPE_P (expression))
28223 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
28224 /* FALLTHRU */
28225 case ALIGNOF_EXPR:
28226 case TYPEID_EXPR:
28227 /* A `sizeof' expression is value-dependent if the operand is
28228 type-dependent or is a pack expansion. */
28229 expression = TREE_OPERAND (expression, 0);
28230 if (PACK_EXPANSION_P (expression))
28231 return true;
28232 else if (TYPE_P (expression))
28233 return dependent_type_p (expression);
28234 return instantiation_dependent_uneval_expression_p (expression);
28235
28236 case AT_ENCODE_EXPR:
28237 /* An 'encode' expression is value-dependent if the operand is
28238 type-dependent. */
28239 expression = TREE_OPERAND (expression, 0);
28240 return dependent_type_p (expression);
28241
28242 case NOEXCEPT_EXPR:
28243 expression = TREE_OPERAND (expression, 0);
28244 return instantiation_dependent_uneval_expression_p (expression);
28245
28246 case SCOPE_REF:
28247 /* All instantiation-dependent expressions should also be considered
28248 value-dependent. */
28249 return instantiation_dependent_scope_ref_p (expression);
28250
28251 case COMPONENT_REF:
28252 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
28253 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
28254
28255 case NONTYPE_ARGUMENT_PACK:
28256 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
28257 is value-dependent. */
28258 for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
28259 if (value_dependent_expression_p (arg))
28260 return true;
28261 return false;
28262
28263 case TRAIT_EXPR:
28264 {
28265 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
28266 return true;
28267
28268 tree type2 = TRAIT_EXPR_TYPE2 (expression);
28269 if (!type2)
28270 return false;
28271
28272 if (TREE_CODE (type2) != TREE_VEC)
28273 return dependent_type_p (type2);
28274
28275 for (tree arg : tree_vec_range (type2))
28276 if (dependent_type_p (arg))
28277 return true;
28278
28279 return false;
28280 }
28281
28282 case MODOP_EXPR:
28283 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
28284 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
28285
28286 case ARRAY_REF:
28287 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
28288 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
28289
28290 case ADDR_EXPR:
28291 {
28292 tree op = TREE_OPERAND (expression, 0);
28293 return (value_dependent_expression_p (op)
28294 || has_value_dependent_address (op));
28295 }
28296
28297 case REQUIRES_EXPR:
28298 /* Treat all requires-expressions as value-dependent so
28299 we don't try to fold them. */
28300 return true;
28301
28302 case TYPE_REQ:
28303 return dependent_type_p (TREE_OPERAND (expression, 0));
28304
28305 case CALL_EXPR:
28306 {
28307 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
28308 return true;
28309 tree fn = get_callee_fndecl (expression);
28310 int i, nargs;
28311 nargs = call_expr_nargs (expression);
28312 for (i = 0; i < nargs; ++i)
28313 {
28314 tree op = CALL_EXPR_ARG (expression, i);
28315 /* In a call to a constexpr member function, look through the
28316 implicit ADDR_EXPR on the object argument so that it doesn't
28317 cause the call to be considered value-dependent. We also
28318 look through it in potential_constant_expression. */
28319 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
28320 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
28321 && TREE_CODE (op) == ADDR_EXPR)
28322 op = TREE_OPERAND (op, 0);
28323 if (value_dependent_expression_p (op))
28324 return true;
28325 }
28326 return false;
28327 }
28328
28329 case TEMPLATE_ID_EXPR:
28330 return concept_definition_p (TREE_OPERAND (expression, 0))
28331 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
28332
28333 case CONSTRUCTOR:
28334 {
28335 unsigned ix;
28336 tree val;
28337 if (dependent_type_p (TREE_TYPE (expression)))
28338 return true;
28339 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
28340 if (value_dependent_expression_p (val))
28341 return true;
28342 return false;
28343 }
28344
28345 case STMT_EXPR:
28346 /* Treat a GNU statement expression as dependent to avoid crashing
28347 under instantiate_non_dependent_expr; it can't be constant. */
28348 return true;
28349
28350 case NEW_EXPR:
28351 case VEC_NEW_EXPR:
28352 /* The second operand is a type, which type_dependent_expression_p
28353 (and therefore value_dependent_expression_p) doesn't want to see. */
28354 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
28355 || value_dependent_expression_p (TREE_OPERAND (expression, 2))
28356 || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
28357
28358 default:
28359 /* A constant expression is value-dependent if any subexpression is
28360 value-dependent. */
28361 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
28362 {
28363 case tcc_reference:
28364 case tcc_unary:
28365 case tcc_comparison:
28366 case tcc_binary:
28367 case tcc_expression:
28368 case tcc_vl_exp:
28369 {
28370 int i, len = cp_tree_operand_length (expression);
28371
28372 for (i = 0; i < len; i++)
28373 {
28374 tree t = TREE_OPERAND (expression, i);
28375
28376 /* In some cases, some of the operands may be missing.
28377 (For example, in the case of PREDECREMENT_EXPR, the
28378 amount to increment by may be missing.) That doesn't
28379 make the expression dependent. */
28380 if (t && value_dependent_expression_p (t))
28381 return true;
28382 }
28383 }
28384 break;
28385 default:
28386 break;
28387 }
28388 break;
28389 }
28390
28391 /* The expression is not value-dependent. */
28392 return false;
28393 }
28394
28395 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
28396 [temp.dep.expr]. Note that an expression with no type is
28397 considered dependent. Other parts of the compiler arrange for an
28398 expression with type-dependent subexpressions to have no type, so
28399 this function doesn't have to be fully recursive. */
28400
28401 bool
28402 type_dependent_expression_p (tree expression)
28403 {
28404 if (!processing_template_decl)
28405 return false;
28406
28407 if (expression == NULL_TREE || expression == error_mark_node)
28408 return false;
28409
28410 gcc_checking_assert (!TYPE_P (expression));
28411
28412 STRIP_ANY_LOCATION_WRAPPER (expression);
28413
28414 /* An unresolved name is always dependent. */
28415 if (identifier_p (expression)
28416 || TREE_CODE (expression) == USING_DECL
28417 || TREE_CODE (expression) == WILDCARD_DECL)
28418 return true;
28419
28420 /* A lambda-expression in template context is dependent. dependent_type_p is
28421 true for a lambda in the scope of a class or function template, but that
28422 doesn't cover all template contexts, like a default template argument. */
28423 if (TREE_CODE (expression) == LAMBDA_EXPR)
28424 return true;
28425
28426 /* A fold expression is type-dependent. */
28427 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
28428 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
28429 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
28430 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
28431 return true;
28432
28433 /* Some expression forms are never type-dependent. */
28434 if (TREE_CODE (expression) == SIZEOF_EXPR
28435 || TREE_CODE (expression) == ALIGNOF_EXPR
28436 || TREE_CODE (expression) == AT_ENCODE_EXPR
28437 || TREE_CODE (expression) == NOEXCEPT_EXPR
28438 || TREE_CODE (expression) == TRAIT_EXPR
28439 || TREE_CODE (expression) == TYPEID_EXPR
28440 || TREE_CODE (expression) == DELETE_EXPR
28441 || TREE_CODE (expression) == VEC_DELETE_EXPR
28442 || TREE_CODE (expression) == THROW_EXPR
28443 || TREE_CODE (expression) == REQUIRES_EXPR)
28444 return false;
28445
28446 /* The types of these expressions depends only on the type to which
28447 the cast occurs. */
28448 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
28449 || TREE_CODE (expression) == STATIC_CAST_EXPR
28450 || TREE_CODE (expression) == CONST_CAST_EXPR
28451 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
28452 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
28453 || TREE_CODE (expression) == CAST_EXPR)
28454 return dependent_type_p (TREE_TYPE (expression));
28455
28456 /* The types of these expressions depends only on the type created
28457 by the expression. */
28458 if (TREE_CODE (expression) == NEW_EXPR
28459 || TREE_CODE (expression) == VEC_NEW_EXPR)
28460 {
28461 /* For NEW_EXPR tree nodes created inside a template, either
28462 the object type itself or a TREE_LIST may appear as the
28463 operand 1. */
28464 tree type = TREE_OPERAND (expression, 1);
28465 if (TREE_CODE (type) == TREE_LIST)
28466 /* This is an array type. We need to check array dimensions
28467 as well. */
28468 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
28469 || value_dependent_expression_p
28470 (TREE_OPERAND (TREE_VALUE (type), 1));
28471 /* Array type whose dimension has to be deduced. */
28472 else if (TREE_CODE (type) == ARRAY_TYPE
28473 && TREE_OPERAND (expression, 2) == NULL_TREE)
28474 return true;
28475 else
28476 return dependent_type_p (type);
28477 }
28478
28479 if (TREE_CODE (expression) == SCOPE_REF)
28480 {
28481 tree scope = TREE_OPERAND (expression, 0);
28482 tree name = TREE_OPERAND (expression, 1);
28483
28484 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28485 contains an identifier associated by name lookup with one or more
28486 declarations declared with a dependent type, or...a
28487 nested-name-specifier or qualified-id that names a member of an
28488 unknown specialization. */
28489 return (type_dependent_expression_p (name)
28490 || dependent_scope_p (scope));
28491 }
28492
28493 if (TREE_CODE (expression) == TEMPLATE_DECL
28494 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
28495 return uses_outer_template_parms (expression);
28496
28497 if (TREE_CODE (expression) == STMT_EXPR)
28498 expression = stmt_expr_value_expr (expression);
28499
28500 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
28501 {
28502 for (auto &elt : CONSTRUCTOR_ELTS (expression))
28503 if (type_dependent_expression_p (elt.value))
28504 return true;
28505 return false;
28506 }
28507
28508 /* A static data member of the current instantiation with incomplete
28509 array type is type-dependent, as the definition and specializations
28510 can have different bounds. */
28511 if (VAR_P (expression)
28512 && DECL_CLASS_SCOPE_P (expression)
28513 && dependent_type_p (DECL_CONTEXT (expression))
28514 && VAR_HAD_UNKNOWN_BOUND (expression))
28515 return true;
28516
28517 /* An array of unknown bound depending on a variadic parameter, eg:
28518
28519 template<typename... Args>
28520 void foo (Args... args)
28521 {
28522 int arr[] = { args... };
28523 }
28524
28525 template<int... vals>
28526 void bar ()
28527 {
28528 int arr[] = { vals... };
28529 }
28530
28531 If the array has no length and has an initializer, it must be that
28532 we couldn't determine its length in cp_complete_array_type because
28533 it is dependent. */
28534 if (((VAR_P (expression) && DECL_INITIAL (expression))
28535 || COMPOUND_LITERAL_P (expression))
28536 && TREE_TYPE (expression) != NULL_TREE
28537 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
28538 && !TYPE_DOMAIN (TREE_TYPE (expression)))
28539 return true;
28540
28541 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28542 if (BASELINK_P (expression))
28543 {
28544 if (BASELINK_OPTYPE (expression)
28545 && dependent_type_p (BASELINK_OPTYPE (expression)))
28546 return true;
28547 expression = BASELINK_FUNCTIONS (expression);
28548 }
28549
28550 /* A function or variable template-id is type-dependent if it has any
28551 dependent template arguments. */
28552 if (VAR_OR_FUNCTION_DECL_P (expression)
28553 && DECL_LANG_SPECIFIC (expression)
28554 && DECL_TEMPLATE_INFO (expression))
28555 {
28556 /* Consider the innermost template arguments, since those are the ones
28557 that come from the template-id; the template arguments for the
28558 enclosing class do not make it type-dependent unless they are used in
28559 the type of the decl. */
28560 if (instantiates_primary_template_p (expression)
28561 && (any_dependent_template_arguments_p
28562 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
28563 return true;
28564 }
28565
28566 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28567 type-dependent. Checking this is important for functions with auto return
28568 type, which looks like a dependent type. */
28569 if (TREE_CODE (expression) == FUNCTION_DECL
28570 && !(DECL_CLASS_SCOPE_P (expression)
28571 && dependent_type_p (DECL_CONTEXT (expression)))
28572 && !(DECL_LANG_SPECIFIC (expression)
28573 && DECL_UNIQUE_FRIEND_P (expression)
28574 && (!DECL_FRIEND_CONTEXT (expression)
28575 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
28576 && !DECL_LOCAL_DECL_P (expression))
28577 {
28578 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
28579 || undeduced_auto_decl (expression));
28580 return false;
28581 }
28582
28583 /* Otherwise, its constraints could still depend on outer template parameters
28584 from its (dependent) scope. */
28585 if (TREE_CODE (expression) == FUNCTION_DECL
28586 /* As an optimization, check this cheaper sufficient condition first.
28587 (At this point we've established that we're looking at a member of
28588 a dependent class, so it makes sense to start treating say undeduced
28589 auto as dependent.) */
28590 && !dependent_type_p (TREE_TYPE (expression))
28591 && uses_outer_template_parms_in_constraints (expression))
28592 return true;
28593
28594 /* Always dependent, on the number of arguments if nothing else. */
28595 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
28596 return true;
28597
28598 if (TREE_TYPE (expression) == unknown_type_node)
28599 {
28600 if (TREE_CODE (expression) == ADDR_EXPR)
28601 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
28602 if (TREE_CODE (expression) == COMPONENT_REF
28603 || TREE_CODE (expression) == OFFSET_REF)
28604 {
28605 if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
28606 return true;
28607 expression = TREE_OPERAND (expression, 1);
28608 if (identifier_p (expression))
28609 return false;
28610 }
28611 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28612 if (TREE_CODE (expression) == SCOPE_REF)
28613 return false;
28614
28615 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28616 if (TREE_CODE (expression) == CO_AWAIT_EXPR
28617 || TREE_CODE (expression) == CO_YIELD_EXPR)
28618 return true;
28619
28620 if (BASELINK_P (expression))
28621 {
28622 if (BASELINK_OPTYPE (expression)
28623 && dependent_type_p (BASELINK_OPTYPE (expression)))
28624 return true;
28625 expression = BASELINK_FUNCTIONS (expression);
28626 }
28627
28628 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
28629 {
28630 if (any_dependent_template_arguments_p
28631 (TREE_OPERAND (expression, 1)))
28632 return true;
28633 expression = TREE_OPERAND (expression, 0);
28634 if (identifier_p (expression))
28635 return true;
28636 }
28637
28638 gcc_assert (OVL_P (expression));
28639
28640 for (lkp_iterator iter (expression); iter; ++iter)
28641 if (type_dependent_expression_p (*iter))
28642 return true;
28643
28644 return false;
28645 }
28646
28647 /* The type of a non-type template parm declared with a placeholder type
28648 depends on the corresponding template argument, even though
28649 placeholders are not normally considered dependent. */
28650 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
28651 && is_auto (TREE_TYPE (expression)))
28652 return true;
28653
28654 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
28655
28656 /* Dependent type attributes might not have made it from the decl to
28657 the type yet. */
28658 if (DECL_P (expression)
28659 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
28660 return true;
28661
28662 return (dependent_type_p (TREE_TYPE (expression)));
28663 }
28664
28665 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28666 type-dependent if the expression refers to a member of the current
28667 instantiation and the type of the referenced member is dependent, or the
28668 class member access expression refers to a member of an unknown
28669 specialization.
28670
28671 This function returns true if the OBJECT in such a class member access
28672 expression is of an unknown specialization. */
28673
28674 bool
28675 type_dependent_object_expression_p (tree object)
28676 {
28677 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28678 dependent. */
28679 if (TREE_CODE (object) == IDENTIFIER_NODE)
28680 return true;
28681 tree scope = TREE_TYPE (object);
28682 return (!scope || dependent_scope_p (scope));
28683 }
28684
28685 /* walk_tree callback function for instantiation_dependent_expression_p,
28686 below. Returns non-zero if a dependent subexpression is found. */
28687
28688 static tree
28689 instantiation_dependent_r (tree *tp, int *walk_subtrees,
28690 void * /*data*/)
28691 {
28692 if (TYPE_P (*tp))
28693 {
28694 /* We don't have to worry about decltype currently because decltype
28695 of an instantiation-dependent expr is a dependent type. This
28696 might change depending on the resolution of DR 1172. */
28697 *walk_subtrees = false;
28698 return NULL_TREE;
28699 }
28700 enum tree_code code = TREE_CODE (*tp);
28701 switch (code)
28702 {
28703 /* Don't treat an argument list as dependent just because it has no
28704 TREE_TYPE. */
28705 case TREE_LIST:
28706 case TREE_VEC:
28707 case NONTYPE_ARGUMENT_PACK:
28708 return NULL_TREE;
28709
28710 case TEMPLATE_PARM_INDEX:
28711 if (dependent_type_p (TREE_TYPE (*tp)))
28712 return *tp;
28713 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
28714 return *tp;
28715 /* We'll check value-dependence separately. */
28716 return NULL_TREE;
28717
28718 /* Handle expressions with type operands. */
28719 case SIZEOF_EXPR:
28720 case ALIGNOF_EXPR:
28721 case TYPEID_EXPR:
28722 case AT_ENCODE_EXPR:
28723 {
28724 tree op = TREE_OPERAND (*tp, 0);
28725 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
28726 op = TREE_TYPE (op);
28727 if (TYPE_P (op))
28728 {
28729 if (dependent_type_p (op))
28730 return *tp;
28731 else
28732 {
28733 *walk_subtrees = false;
28734 return NULL_TREE;
28735 }
28736 }
28737 break;
28738 }
28739
28740 case COMPONENT_REF:
28741 if (identifier_p (TREE_OPERAND (*tp, 1)))
28742 /* In a template, finish_class_member_access_expr creates a
28743 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28744 type-dependent, so that we can check access control at
28745 instantiation time (PR 42277). See also Core issue 1273. */
28746 return *tp;
28747 break;
28748
28749 case SCOPE_REF:
28750 if (instantiation_dependent_scope_ref_p (*tp))
28751 return *tp;
28752 else
28753 break;
28754
28755 /* Treat statement-expressions as dependent. */
28756 case BIND_EXPR:
28757 return *tp;
28758
28759 /* Treat requires-expressions as dependent. */
28760 case REQUIRES_EXPR:
28761 return *tp;
28762
28763 case CONSTRUCTOR:
28764 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
28765 return *tp;
28766 break;
28767
28768 case TEMPLATE_DECL:
28769 case FUNCTION_DECL:
28770 /* Before C++17, a noexcept-specifier isn't part of the function type
28771 so it doesn't affect type dependence, but we still want to consider it
28772 for instantiation dependence. */
28773 if (cxx_dialect < cxx17
28774 && DECL_DECLARES_FUNCTION_P (*tp)
28775 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
28776 return *tp;
28777 break;
28778
28779 default:
28780 break;
28781 }
28782
28783 if (type_dependent_expression_p (*tp))
28784 return *tp;
28785 else
28786 return NULL_TREE;
28787 }
28788
28789 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28790 sense defined by the ABI:
28791
28792 "An expression is instantiation-dependent if it is type-dependent
28793 or value-dependent, or it has a subexpression that is type-dependent
28794 or value-dependent."
28795
28796 Except don't actually check value-dependence for unevaluated expressions,
28797 because in sizeof(i) we don't care about the value of i. Checking
28798 type-dependence will in turn check value-dependence of array bounds/template
28799 arguments as needed. */
28800
28801 bool
28802 instantiation_dependent_uneval_expression_p (tree expression)
28803 {
28804 tree result;
28805
28806 if (!processing_template_decl)
28807 return false;
28808
28809 if (expression == error_mark_node)
28810 return false;
28811
28812 result = cp_walk_tree_without_duplicates (&expression,
28813 instantiation_dependent_r, NULL);
28814 return result != NULL_TREE;
28815 }
28816
28817 /* As above, but also check value-dependence of the expression as a whole. */
28818
28819 bool
28820 instantiation_dependent_expression_p (tree expression)
28821 {
28822 return (instantiation_dependent_uneval_expression_p (expression)
28823 || (processing_template_decl
28824 && potential_constant_expression (expression)
28825 && value_dependent_expression_p (expression)));
28826 }
28827
28828 /* Like type_dependent_expression_p, but it also works while not processing
28829 a template definition, i.e. during substitution or mangling. */
28830
28831 bool
28832 type_dependent_expression_p_push (tree expr)
28833 {
28834 bool b;
28835 ++processing_template_decl;
28836 b = type_dependent_expression_p (expr);
28837 --processing_template_decl;
28838 return b;
28839 }
28840
28841 /* Returns TRUE if ARGS contains a type-dependent expression. */
28842
28843 bool
28844 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
28845 {
28846 if (!processing_template_decl || !args)
28847 return false;
28848
28849 for (tree arg : *args)
28850 if (type_dependent_expression_p (arg))
28851 return true;
28852
28853 return false;
28854 }
28855
28856 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28857 expressions) contains any type-dependent expressions. */
28858
28859 bool
28860 any_type_dependent_elements_p (const_tree list)
28861 {
28862 for (; list; list = TREE_CHAIN (list))
28863 if (type_dependent_expression_p (TREE_VALUE (list)))
28864 return true;
28865
28866 return false;
28867 }
28868
28869 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28870 expressions) contains any value-dependent expressions. */
28871
28872 bool
28873 any_value_dependent_elements_p (const_tree list)
28874 {
28875 for (; list; list = TREE_CHAIN (list))
28876 if (value_dependent_expression_p (TREE_VALUE (list)))
28877 return true;
28878
28879 return false;
28880 }
28881
28882 /* Returns TRUE if the ARG (a template argument) is dependent. */
28883
28884 bool
28885 dependent_template_arg_p (tree arg)
28886 {
28887 if (!processing_template_decl)
28888 return false;
28889
28890 /* Assume a template argument that was wrongly written by the user
28891 is dependent. This is consistent with what
28892 any_dependent_template_arguments_p [that calls this function]
28893 does. */
28894 if (!arg || arg == error_mark_node)
28895 return true;
28896
28897 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
28898 arg = argument_pack_select_arg (arg);
28899
28900 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
28901 return true;
28902 if (TREE_CODE (arg) == TEMPLATE_DECL)
28903 {
28904 if (DECL_TEMPLATE_PARM_P (arg))
28905 return true;
28906 /* A member template of a dependent class is not necessarily
28907 type-dependent, but it is a dependent template argument because it
28908 will be a member of an unknown specialization to that template. */
28909 tree scope = CP_DECL_CONTEXT (arg);
28910 return TYPE_P (scope) && dependent_type_p (scope);
28911 }
28912 else if (ARGUMENT_PACK_P (arg))
28913 {
28914 tree args = ARGUMENT_PACK_ARGS (arg);
28915 for (tree arg : tree_vec_range (args))
28916 if (dependent_template_arg_p (arg))
28917 return true;
28918 return false;
28919 }
28920 else if (TYPE_P (arg))
28921 return dependent_type_p (arg);
28922 else
28923 return value_dependent_expression_p (arg);
28924 }
28925
28926 /* Identify any expressions that use function parms. */
28927
28928 static tree
28929 find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
28930 {
28931 tree t = *tp;
28932 if (TREE_CODE (t) == PARM_DECL)
28933 {
28934 *walk_subtrees = 0;
28935 return t;
28936 }
28937 return NULL_TREE;
28938 }
28939
28940 /* Returns true if a type specialization formed using the template
28941 arguments ARGS needs to use structural equality. */
28942
28943 bool
28944 any_template_arguments_need_structural_equality_p (tree args)
28945 {
28946 int i;
28947 int j;
28948
28949 if (!args)
28950 return false;
28951 if (args == error_mark_node)
28952 return true;
28953
28954 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28955 {
28956 tree level = TMPL_ARGS_LEVEL (args, i + 1);
28957 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28958 {
28959 tree arg = TREE_VEC_ELT (level, j);
28960 tree packed_args = NULL_TREE;
28961 int k, len = 1;
28962
28963 if (ARGUMENT_PACK_P (arg))
28964 {
28965 /* Look inside the argument pack. */
28966 packed_args = ARGUMENT_PACK_ARGS (arg);
28967 len = TREE_VEC_LENGTH (packed_args);
28968 }
28969
28970 for (k = 0; k < len; ++k)
28971 {
28972 if (packed_args)
28973 arg = TREE_VEC_ELT (packed_args, k);
28974
28975 if (error_operand_p (arg))
28976 return true;
28977 else if (TREE_CODE (arg) == TEMPLATE_DECL)
28978 continue;
28979 else if (arg == any_targ_node)
28980 /* An any_targ_node argument (added by add_defaults_to_ttp)
28981 makes the corresponding specialization not canonicalizable,
28982 since template_args_equal always return true for it. We
28983 may see this when called from bind_template_template_parm. */
28984 return true;
28985 /* Checking current_function_decl because this structural
28986 comparison is only necessary for redeclaration. */
28987 else if (!current_function_decl
28988 && dependent_template_arg_p (arg)
28989 && (cp_walk_tree_without_duplicates
28990 (&arg, find_parm_usage_r, NULL)))
28991 /* The identity of a class template specialization that uses
28992 a function parameter depends on the identity of the function.
28993 And if this specialization appeared in the trailing return
28994 type thereof, we don't know the identity of the function
28995 (e.g. if it's a redeclaration or a new function) until we
28996 form its signature and go through duplicate_decls. Thus
28997 it's unsafe to decide on a canonical type now (which depends
28998 on the DECL_CONTEXT of the function parameter, which can get
28999 mutated after the fact by duplicate_decls), so just require
29000 structural equality in this case (PR52830). */
29001 return true;
29002 else if (TYPE_P (arg)
29003 && TYPE_STRUCTURAL_EQUALITY_P (arg)
29004 && dependent_alias_template_spec_p (arg, nt_transparent))
29005 /* Require structural equality for specializations written
29006 in terms of a dependent alias template specialization. */
29007 return true;
29008 else if (CLASS_TYPE_P (arg)
29009 && TYPE_TEMPLATE_INFO (arg)
29010 && TYPE_STRUCTURAL_EQUALITY_P (arg))
29011 /* Require structural equality for specializations written
29012 in terms of a class template specialization that itself
29013 needs structural equality. */
29014 return true;
29015 }
29016 }
29017 }
29018
29019 return false;
29020 }
29021
29022 /* Returns true if ARGS (a collection of template arguments) contains
29023 any dependent arguments. */
29024
29025 bool
29026 any_dependent_template_arguments_p (const_tree args)
29027 {
29028 if (args == error_mark_node)
29029 return true;
29030 if (!processing_template_decl || !args)
29031 return false;
29032
29033 for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
29034 {
29035 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29036 for (tree arg : tree_vec_range (CONST_CAST_TREE (level)))
29037 if (dependent_template_arg_p (arg))
29038 return true;
29039 }
29040
29041 return false;
29042 }
29043
29044 /* Returns true if ARGS contains any errors. */
29045
29046 bool
29047 any_erroneous_template_args_p (const_tree args)
29048 {
29049 int i;
29050 int j;
29051
29052 if (args == error_mark_node)
29053 return true;
29054
29055 if (args && TREE_CODE (args) != TREE_VEC)
29056 {
29057 if (tree ti = get_template_info (args))
29058 args = TI_ARGS (ti);
29059 else
29060 args = NULL_TREE;
29061 }
29062
29063 if (!args)
29064 return false;
29065
29066 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
29067 {
29068 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
29069 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
29070 if (error_operand_p (TREE_VEC_ELT (level, j)))
29071 return true;
29072 }
29073
29074 return false;
29075 }
29076
29077 /* Returns TRUE if the template TMPL is type-dependent. */
29078
29079 bool
29080 dependent_template_p (tree tmpl)
29081 {
29082 if (TREE_CODE (tmpl) == OVERLOAD)
29083 {
29084 for (lkp_iterator iter (tmpl); iter; ++iter)
29085 if (dependent_template_p (*iter))
29086 return true;
29087 return false;
29088 }
29089
29090 /* Template template parameters are dependent. */
29091 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
29092 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
29093 return true;
29094 /* So are names that have not been looked up. */
29095 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
29096 return true;
29097 return false;
29098 }
29099
29100 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
29101
29102 bool
29103 dependent_template_id_p (tree tmpl, tree args)
29104 {
29105 return (dependent_template_p (tmpl)
29106 || any_dependent_template_arguments_p (args));
29107 }
29108
29109 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
29110 are dependent. */
29111
29112 bool
29113 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
29114 {
29115 int i;
29116
29117 if (!processing_template_decl)
29118 return false;
29119
29120 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
29121 {
29122 tree decl = TREE_VEC_ELT (declv, i);
29123 tree init = TREE_VEC_ELT (initv, i);
29124 tree cond = TREE_VEC_ELT (condv, i);
29125 tree incr = TREE_VEC_ELT (incrv, i);
29126
29127 if (type_dependent_expression_p (decl)
29128 || TREE_CODE (decl) == SCOPE_REF)
29129 return true;
29130
29131 if (init && type_dependent_expression_p (init))
29132 return true;
29133
29134 if (cond == global_namespace)
29135 return true;
29136
29137 if (type_dependent_expression_p (cond))
29138 return true;
29139
29140 if (COMPARISON_CLASS_P (cond)
29141 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
29142 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
29143 return true;
29144
29145 if (TREE_CODE (incr) == MODOP_EXPR)
29146 {
29147 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
29148 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
29149 return true;
29150 }
29151 else if (type_dependent_expression_p (incr))
29152 return true;
29153 else if (TREE_CODE (incr) == MODIFY_EXPR)
29154 {
29155 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
29156 return true;
29157 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
29158 {
29159 tree t = TREE_OPERAND (incr, 1);
29160 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
29161 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
29162 return true;
29163
29164 /* If this loop has a class iterator with != comparison
29165 with increment other than i++/++i/i--/--i, make sure the
29166 increment is constant. */
29167 if (CLASS_TYPE_P (TREE_TYPE (decl))
29168 && TREE_CODE (cond) == NE_EXPR)
29169 {
29170 if (TREE_OPERAND (t, 0) == decl)
29171 t = TREE_OPERAND (t, 1);
29172 else
29173 t = TREE_OPERAND (t, 0);
29174 if (TREE_CODE (t) != INTEGER_CST)
29175 return true;
29176 }
29177 }
29178 }
29179 }
29180
29181 return false;
29182 }
29183
29184 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
29185 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
29186 no such TYPE can be found. Note that this function peers inside
29187 uninstantiated templates and therefore should be used only in
29188 extremely limited situations. ONLY_CURRENT_P restricts this
29189 peering to the currently open classes hierarchy (which is required
29190 when comparing types). */
29191
29192 tree
29193 resolve_typename_type (tree type, bool only_current_p)
29194 {
29195 tree scope;
29196 tree name;
29197 tree decl;
29198 int quals;
29199 tree pushed_scope;
29200 tree result;
29201
29202 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
29203
29204 scope = TYPE_CONTEXT (type);
29205 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
29206 gcc_checking_assert (uses_template_parms (scope));
29207
29208 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
29209 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
29210 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
29211 representing the typedef. In that case TYPE_IDENTIFIER (type) is
29212 not the non-qualified identifier of the TYPENAME_TYPE anymore.
29213 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
29214 the TYPENAME_TYPE instead, we avoid messing up with a possible
29215 typedef variant case. */
29216 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
29217
29218 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
29219 it first before we can figure out what NAME refers to. */
29220 if (TREE_CODE (scope) == TYPENAME_TYPE)
29221 {
29222 if (TYPENAME_IS_RESOLVING_P (scope))
29223 /* Given a class template A with a dependent base with nested type C,
29224 typedef typename A::C::C C will land us here, as trying to resolve
29225 the initial A::C leads to the local C typedef, which leads back to
29226 A::C::C. So we break the recursion now. */
29227 return type;
29228 else
29229 scope = resolve_typename_type (scope, only_current_p);
29230 }
29231 /* If we don't know what SCOPE refers to, then we cannot resolve the
29232 TYPENAME_TYPE. */
29233 if (!CLASS_TYPE_P (scope))
29234 return type;
29235 /* If this is a typedef, we don't want to look inside (c++/11987). */
29236 if (typedef_variant_p (type))
29237 return type;
29238 /* If SCOPE isn't the template itself, it will not have a valid
29239 TYPE_FIELDS list. */
29240 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
29241 /* scope is either the template itself or a compatible instantiation
29242 like X<T>, so look up the name in the original template. */
29243 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
29244 /* If scope has no fields, it can't be a current instantiation. Check this
29245 before currently_open_class to avoid infinite recursion (71515). */
29246 if (!TYPE_FIELDS (scope))
29247 return type;
29248 /* If the SCOPE is not the current instantiation, there's no reason
29249 to look inside it. */
29250 if (only_current_p && !currently_open_class (scope))
29251 return type;
29252 /* Enter the SCOPE so that name lookup will be resolved as if we
29253 were in the class definition. In particular, SCOPE will no
29254 longer be considered a dependent type. */
29255 pushed_scope = push_scope (scope);
29256 /* Look up the declaration. */
29257 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
29258 tf_warning_or_error);
29259
29260 result = NULL_TREE;
29261
29262 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
29263 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
29264 tree fullname = TYPENAME_TYPE_FULLNAME (type);
29265 if (!decl)
29266 /*nop*/;
29267 else if (identifier_p (fullname)
29268 && TREE_CODE (decl) == TYPE_DECL)
29269 {
29270 result = TREE_TYPE (decl);
29271 if (result == error_mark_node)
29272 result = NULL_TREE;
29273 }
29274 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
29275 && DECL_CLASS_TEMPLATE_P (decl))
29276 {
29277 /* Obtain the template and the arguments. */
29278 tree tmpl = TREE_OPERAND (fullname, 0);
29279 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
29280 {
29281 /* We get here with a plain identifier because a previous tentative
29282 parse of the nested-name-specifier as part of a ptr-operator saw
29283 ::template X<A>. The use of ::template is necessary in a
29284 ptr-operator, but wrong in a declarator-id.
29285
29286 [temp.names]: In a qualified-id of a declarator-id, the keyword
29287 template shall not appear at the top level. */
29288 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
29289 "keyword %<template%> not allowed in declarator-id");
29290 tmpl = decl;
29291 }
29292 tree args = TREE_OPERAND (fullname, 1);
29293 /* Instantiate the template. */
29294 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
29295 /*entering_scope=*/true,
29296 tf_error | tf_user);
29297 if (result == error_mark_node)
29298 result = NULL_TREE;
29299 }
29300
29301 /* Leave the SCOPE. */
29302 if (pushed_scope)
29303 pop_scope (pushed_scope);
29304
29305 /* If we failed to resolve it, return the original typename. */
29306 if (!result)
29307 return type;
29308
29309 /* If lookup found a typename type, resolve that too. */
29310 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
29311 {
29312 /* Ill-formed programs can cause infinite recursion here, so we
29313 must catch that. */
29314 TYPENAME_IS_RESOLVING_P (result) = 1;
29315 result = resolve_typename_type (result, only_current_p);
29316 TYPENAME_IS_RESOLVING_P (result) = 0;
29317 }
29318
29319 /* Qualify the resulting type. */
29320 quals = cp_type_quals (type);
29321 if (quals)
29322 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
29323
29324 return result;
29325 }
29326
29327 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29328 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29329 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29330
29331 static tree
29332 make_auto_1 (tree name, bool set_canonical, int level = -1)
29333 {
29334 if (level == -1)
29335 level = current_template_depth + 1;
29336 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
29337 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
29338 TYPE_STUB_DECL (au) = TYPE_NAME (au);
29339 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
29340 (0, level, level, TYPE_NAME (au), NULL_TREE);
29341 if (set_canonical)
29342 TYPE_CANONICAL (au) = canonical_type_parameter (au);
29343 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
29344 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
29345 if (name == decltype_auto_identifier)
29346 AUTO_IS_DECLTYPE (au) = true;
29347
29348 return au;
29349 }
29350
29351 tree
29352 make_decltype_auto (void)
29353 {
29354 return make_auto_1 (decltype_auto_identifier, true);
29355 }
29356
29357 tree
29358 make_auto (void)
29359 {
29360 return make_auto_1 (auto_identifier, true);
29361 }
29362
29363 /* Return a C++17 deduction placeholder for class template TMPL.
29364 There are represented as an 'auto' with the special level 0 and
29365 CLASS_PLACEHOLDER_TEMPLATE set. */
29366
29367 tree
29368 make_template_placeholder (tree tmpl)
29369 {
29370 tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
29371 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
29372 /* Our canonical type depends on the placeholder. */
29373 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29374 return t;
29375 }
29376
29377 /* True iff T is a C++17 class template deduction placeholder. */
29378
29379 bool
29380 template_placeholder_p (tree t)
29381 {
29382 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
29383 }
29384
29385 /* Return an auto for an explicit cast expression auto(x).
29386 Like CTAD placeholders, these have level 0 so that they're
29387 not accidentally replaced via tsubst and are always directly
29388 resolved via do_auto_deduction. */
29389
29390 tree
29391 make_cast_auto ()
29392 {
29393 return make_auto_1 (auto_identifier, true, /*level=*/0);
29394 }
29395
29396 /* Make a "constrained auto" type-specifier. This is an auto or
29397 decltype(auto) type with constraints that must be associated after
29398 deduction. The constraint is formed from the given concept CON
29399 and its optional sequence of template arguments ARGS.
29400
29401 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29402
29403 static tree
29404 make_constrained_placeholder_type (tree type, tree con, tree args)
29405 {
29406 /* Build the constraint. */
29407 tree tmpl = DECL_TI_TEMPLATE (con);
29408 tree expr = tmpl;
29409 if (TREE_CODE (con) == FUNCTION_DECL)
29410 expr = ovl_make (tmpl);
29411 ++processing_template_decl;
29412 expr = build_concept_check (expr, type, args, tf_warning_or_error);
29413 --processing_template_decl;
29414
29415 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
29416 = build_tree_list (current_template_parms, expr);
29417
29418 /* Our canonical type depends on the constraint. */
29419 TYPE_CANONICAL (type) = canonical_type_parameter (type);
29420
29421 /* Attach the constraint to the type declaration. */
29422 return TYPE_NAME (type);
29423 }
29424
29425 /* Make a "constrained auto" type-specifier. */
29426
29427 tree
29428 make_constrained_auto (tree con, tree args)
29429 {
29430 tree type = make_auto_1 (auto_identifier, false);
29431 return make_constrained_placeholder_type (type, con, args);
29432 }
29433
29434 /* Make a "constrained decltype(auto)" type-specifier. */
29435
29436 tree
29437 make_constrained_decltype_auto (tree con, tree args)
29438 {
29439 tree type = make_auto_1 (decltype_auto_identifier, false);
29440 return make_constrained_placeholder_type (type, con, args);
29441 }
29442
29443 /* Returns true if the placeholder type constraint T has any dependent
29444 (explicit) template arguments. */
29445
29446 static bool
29447 placeholder_type_constraint_dependent_p (tree t)
29448 {
29449 tree id = unpack_concept_check (t);
29450 tree args = TREE_OPERAND (id, 1);
29451 tree first = TREE_VEC_ELT (args, 0);
29452 if (ARGUMENT_PACK_P (first))
29453 {
29454 args = expand_template_argument_pack (args);
29455 first = TREE_VEC_ELT (args, 0);
29456 }
29457 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
29458 || is_auto (first));
29459 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
29460 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
29461 return true;
29462 return false;
29463 }
29464
29465 /* Build and return a concept definition. Like other templates, the
29466 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29467 the TEMPLATE_DECL. */
29468
29469 tree
29470 finish_concept_definition (cp_expr id, tree init, tree attrs)
29471 {
29472 gcc_assert (identifier_p (id));
29473 gcc_assert (processing_template_decl);
29474
29475 location_t loc = id.get_location();
29476
29477 /* A concept-definition shall not have associated constraints. */
29478 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
29479 {
29480 error_at (loc, "a concept cannot be constrained");
29481 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
29482 }
29483
29484 /* A concept-definition shall appear in namespace scope. Templates
29485 aren't allowed in block scope, so we only need to check for class
29486 scope. */
29487 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29488 {
29489 error_at (loc, "concept %qE not in namespace scope", *id);
29490 return error_mark_node;
29491 }
29492
29493 if (current_template_depth > 1)
29494 {
29495 error_at (loc, "concept %qE has multiple template parameter lists", *id);
29496 return error_mark_node;
29497 }
29498
29499 /* Initially build the concept declaration; its type is bool. */
29500 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
29501 DECL_CONTEXT (decl) = current_scope ();
29502 DECL_INITIAL (decl) = init;
29503
29504 if (attrs)
29505 cplus_decl_attributes (&decl, attrs, 0);
29506
29507 set_originating_module (decl, false);
29508
29509 /* Push the enclosing template. */
29510 return push_template_decl (decl);
29511 }
29512
29513 /* Given type ARG, return std::initializer_list<ARG>. */
29514
29515 static tree
29516 listify (tree arg)
29517 {
29518 tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
29519
29520 if (std_init_list == error_mark_node
29521 || !DECL_CLASS_TEMPLATE_P (std_init_list))
29522 {
29523 gcc_rich_location richloc (input_location);
29524 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
29525 error_at (&richloc,
29526 "deducing from brace-enclosed initializer list"
29527 " requires %<#include <initializer_list>%>");
29528
29529 return error_mark_node;
29530 }
29531 tree argvec = make_tree_vec (1);
29532 TREE_VEC_ELT (argvec, 0) = arg;
29533
29534 return lookup_template_class (std_init_list, argvec, NULL_TREE,
29535 NULL_TREE, 0, tf_warning_or_error);
29536 }
29537
29538 /* Replace auto in TYPE with std::initializer_list<auto>. */
29539
29540 static tree
29541 listify_autos (tree type, tree auto_node)
29542 {
29543 tree init_auto = listify (strip_top_quals (auto_node));
29544 tree argvec = make_tree_vec (1);
29545 TREE_VEC_ELT (argvec, 0) = init_auto;
29546 if (processing_template_decl)
29547 argvec = add_to_template_args (current_template_args (), argvec);
29548 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
29549 }
29550
29551 /* Hash traits for hashing possibly constrained 'auto'
29552 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29553
29554 struct auto_hash : default_hash_traits<tree>
29555 {
29556 static inline hashval_t hash (tree);
29557 static inline bool equal (tree, tree);
29558 };
29559
29560 /* Hash the 'auto' T. */
29561
29562 inline hashval_t
29563 auto_hash::hash (tree t)
29564 {
29565 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
29566 /* Matching constrained-type-specifiers denote the same template
29567 parameter, so hash the constraint. */
29568 return hash_placeholder_constraint (c);
29569 else
29570 /* But unconstrained autos are all separate, so just hash the pointer. */
29571 return iterative_hash_object (t, 0);
29572 }
29573
29574 /* Compare two 'auto's. */
29575
29576 inline bool
29577 auto_hash::equal (tree t1, tree t2)
29578 {
29579 if (t1 == t2)
29580 return true;
29581
29582 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
29583 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
29584
29585 /* Two unconstrained autos are distinct. */
29586 if (!c1 || !c2)
29587 return false;
29588
29589 return equivalent_placeholder_constraints (c1, c2);
29590 }
29591
29592 /* for_each_template_parm callback for extract_autos: if t is a (possibly
29593 constrained) auto, add it to the vector. */
29594
29595 static int
29596 extract_autos_r (tree t, void *data)
29597 {
29598 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
29599 if (is_auto (t) && !template_placeholder_p (t))
29600 {
29601 /* All the autos were built with index 0; fix that up now. */
29602 tree *p = hash.find_slot (t, INSERT);
29603 int idx;
29604 if (*p)
29605 /* If this is a repeated constrained-type-specifier, use the index we
29606 chose before. */
29607 idx = TEMPLATE_TYPE_IDX (*p);
29608 else
29609 {
29610 /* Otherwise this is new, so use the current count. */
29611 *p = t;
29612 idx = hash.elements () - 1;
29613 }
29614 if (idx != TEMPLATE_TYPE_IDX (t))
29615 {
29616 gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0);
29617 gcc_checking_assert (TYPE_CANONICAL (t) != t);
29618 TEMPLATE_TYPE_IDX (t) = idx;
29619 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29620 }
29621 }
29622
29623 /* Always keep walking. */
29624 return 0;
29625 }
29626
29627 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29628 says they can appear anywhere in the type. */
29629
29630 static tree
29631 extract_autos (tree type)
29632 {
29633 hash_set<tree> visited;
29634 hash_table<auto_hash> hash (2);
29635
29636 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
29637
29638 tree tree_vec = make_tree_vec (hash.elements());
29639 for (tree elt : hash)
29640 {
29641 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
29642 TREE_VEC_ELT (tree_vec, i)
29643 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
29644 }
29645
29646 return tree_vec;
29647 }
29648
29649 /* The stem for deduction guide names. */
29650 const char *const dguide_base = "__dguide_";
29651
29652 /* Return the name for a deduction guide for class template TMPL. */
29653
29654 tree
29655 dguide_name (tree tmpl)
29656 {
29657 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
29658 tree tname = TYPE_IDENTIFIER (type);
29659 char *buf = (char *) alloca (1 + strlen (dguide_base)
29660 + IDENTIFIER_LENGTH (tname));
29661 memcpy (buf, dguide_base, strlen (dguide_base));
29662 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
29663 IDENTIFIER_LENGTH (tname) + 1);
29664 tree dname = get_identifier (buf);
29665 TREE_TYPE (dname) = type;
29666 return dname;
29667 }
29668
29669 /* True if NAME is the name of a deduction guide. */
29670
29671 bool
29672 dguide_name_p (tree name)
29673 {
29674 return (TREE_CODE (name) == IDENTIFIER_NODE
29675 && TREE_TYPE (name)
29676 && startswith (IDENTIFIER_POINTER (name), dguide_base));
29677 }
29678
29679 /* True if FN is a deduction guide. */
29680
29681 bool
29682 deduction_guide_p (const_tree fn)
29683 {
29684 if (DECL_P (fn))
29685 if (tree name = DECL_NAME (fn))
29686 return dguide_name_p (name);
29687 return false;
29688 }
29689
29690 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29691
29692 bool
29693 copy_guide_p (const_tree fn)
29694 {
29695 gcc_assert (deduction_guide_p (fn));
29696 if (!DECL_ARTIFICIAL (fn))
29697 return false;
29698 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
29699 return (TREE_CHAIN (parms) == void_list_node
29700 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
29701 }
29702
29703 /* True if FN is a guide generated from a constructor template. */
29704
29705 bool
29706 template_guide_p (const_tree fn)
29707 {
29708 gcc_assert (deduction_guide_p (fn));
29709 if (!DECL_ARTIFICIAL (fn))
29710 return false;
29711 tree tmpl = DECL_TI_TEMPLATE (fn);
29712 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
29713 return PRIMARY_TEMPLATE_P (org);
29714 return false;
29715 }
29716
29717 /* True if FN is an aggregate initialization guide or the copy deduction
29718 guide. */
29719
29720 bool
29721 builtin_guide_p (const_tree fn)
29722 {
29723 if (!deduction_guide_p (fn))
29724 return false;
29725 if (!DECL_ARTIFICIAL (fn))
29726 /* Explicitly declared. */
29727 return false;
29728 if (DECL_ABSTRACT_ORIGIN (fn))
29729 /* Derived from a constructor. */
29730 return false;
29731 return true;
29732 }
29733
29734 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29735 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29736 template parameter types. Note that the handling of template template
29737 parameters relies on current_template_parms being set appropriately for the
29738 new template. */
29739
29740 static tree
29741 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
29742 tree tsubst_args, tsubst_flags_t complain)
29743 {
29744 if (olddecl == error_mark_node)
29745 return error_mark_node;
29746
29747 tree oldidx = get_template_parm_index (olddecl);
29748
29749 tree newtype;
29750 if (TREE_CODE (olddecl) == TYPE_DECL
29751 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29752 {
29753 tree oldtype = TREE_TYPE (olddecl);
29754 newtype = cxx_make_type (TREE_CODE (oldtype));
29755 TYPE_MAIN_VARIANT (newtype) = newtype;
29756 }
29757 else
29758 {
29759 newtype = TREE_TYPE (olddecl);
29760 if (type_uses_auto (newtype))
29761 {
29762 // Substitute once to fix references to other template parameters.
29763 newtype = tsubst (newtype, tsubst_args,
29764 complain|tf_partial, NULL_TREE);
29765 // Now substitute again to reduce the level of the auto.
29766 newtype = tsubst (newtype, current_template_args (),
29767 complain, NULL_TREE);
29768 }
29769 else
29770 newtype = tsubst (newtype, tsubst_args,
29771 complain, NULL_TREE);
29772 }
29773
29774 tree newdecl
29775 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
29776 DECL_NAME (olddecl), newtype);
29777 SET_DECL_TEMPLATE_PARM_P (newdecl);
29778
29779 tree newidx;
29780 if (TREE_CODE (olddecl) == TYPE_DECL
29781 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29782 {
29783 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
29784 = build_template_parm_index (index, level, level,
29785 newdecl, newtype);
29786 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29787 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29788 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
29789
29790 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
29791 {
29792 tree newresult
29793 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
29794 DECL_NAME (olddecl), newtype);
29795 DECL_ARTIFICIAL (newresult) = true;
29796 DECL_TEMPLATE_RESULT (newdecl) = newresult;
29797 // First create a copy (ttargs) of tsubst_args with an
29798 // additional level for the template template parameter's own
29799 // template parameters (ttparms).
29800 tree ttparms = (INNERMOST_TEMPLATE_PARMS
29801 (DECL_TEMPLATE_PARMS (olddecl)));
29802 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
29803 tree ttargs = make_tree_vec (depth + 1);
29804 for (int i = 0; i < depth; ++i)
29805 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
29806 TREE_VEC_ELT (ttargs, depth)
29807 = template_parms_level_to_args (ttparms);
29808 // Substitute ttargs into ttparms to fix references to
29809 // other template parameters.
29810 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29811 complain|tf_partial);
29812 // Now substitute again with args based on tparms, to reduce
29813 // the level of the ttparms.
29814 ttargs = current_template_args ();
29815 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29816 complain);
29817 // Finally, tack the adjusted parms onto tparms.
29818 ttparms = tree_cons (size_int (level + 1), ttparms,
29819 copy_node (current_template_parms));
29820 // As with all template template parms, the parameter list captured
29821 // by this template template parm that corresponds to its own level
29822 // should be empty. This avoids infinite recursion when structurally
29823 // comparing two such rewritten template template parms (PR102479).
29824 gcc_assert (!TREE_VEC_LENGTH
29825 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
29826 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
29827 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
29828 // All done.
29829 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
29830 DECL_TEMPLATE_INFO (newresult)
29831 = build_template_info (newdecl, template_parms_to_args (ttparms));
29832 }
29833
29834 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
29835 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
29836 else
29837 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
29838 }
29839 else
29840 {
29841 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
29842 tree newconst
29843 = build_decl (DECL_SOURCE_LOCATION (oldconst),
29844 TREE_CODE (oldconst),
29845 DECL_NAME (oldconst), newtype);
29846 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
29847 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
29848 SET_DECL_TEMPLATE_PARM_P (newconst);
29849 newidx = build_template_parm_index (index, level, level,
29850 newconst, newtype);
29851 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29852 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29853 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
29854 }
29855
29856 return newdecl;
29857 }
29858
29859 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29860 template parameter. */
29861
29862 static tree
29863 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
29864 tree targs, unsigned targs_index, tsubst_flags_t complain)
29865 {
29866 tree olddecl = TREE_VALUE (oldelt);
29867 tree newdecl = rewrite_template_parm (olddecl, index, level,
29868 targs, complain);
29869 if (newdecl == error_mark_node)
29870 return error_mark_node;
29871 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
29872 targs, complain, NULL_TREE);
29873 tree list = build_tree_list (newdef, newdecl);
29874 TEMPLATE_PARM_CONSTRAINTS (list)
29875 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
29876 targs, complain, NULL_TREE);
29877 int depth = TMPL_ARGS_DEPTH (targs);
29878 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
29879 return list;
29880 }
29881
29882 /* Returns a C++17 class deduction guide template based on the constructor
29883 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29884 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29885 aggregate initialization guide. OUTER_ARGS are the template arguments
29886 for the enclosing scope of the class. */
29887
29888 static tree
29889 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
29890 {
29891 tree tparms, targs, fparms, fargs, ci;
29892 bool memtmpl = false;
29893 bool explicit_p;
29894 location_t loc;
29895 tree fn_tmpl = NULL_TREE;
29896
29897 if (outer_args)
29898 {
29899 ++processing_template_decl;
29900 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
29901 --processing_template_decl;
29902 }
29903
29904 if (!DECL_DECLARES_FUNCTION_P (ctor))
29905 {
29906 if (TYPE_P (ctor))
29907 {
29908 bool copy_p = TYPE_REF_P (ctor);
29909 if (copy_p)
29910 fparms = tree_cons (NULL_TREE, type, void_list_node);
29911 else
29912 fparms = void_list_node;
29913 }
29914 else if (TREE_CODE (ctor) == TREE_LIST)
29915 fparms = ctor;
29916 else
29917 gcc_unreachable ();
29918
29919 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
29920 tparms = DECL_TEMPLATE_PARMS (ctmpl);
29921 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
29922 ci = NULL_TREE;
29923 fargs = NULL_TREE;
29924 loc = DECL_SOURCE_LOCATION (ctmpl);
29925 explicit_p = false;
29926 }
29927 else
29928 {
29929 ++processing_template_decl;
29930 bool ok = true;
29931
29932 complain |= tf_dguide;
29933
29934 fn_tmpl
29935 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
29936 : DECL_TI_TEMPLATE (ctor));
29937 if (outer_args)
29938 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
29939 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
29940
29941 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
29942 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29943 fully specialized args for the enclosing class. Strip those off, as
29944 the deduction guide won't have those template parameters. */
29945 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
29946 TMPL_PARMS_DEPTH (tparms));
29947 /* Discard the 'this' parameter. */
29948 fparms = FUNCTION_ARG_CHAIN (ctor);
29949 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
29950 ci = get_constraints (ctor);
29951 loc = DECL_SOURCE_LOCATION (ctor);
29952 explicit_p = DECL_NONCONVERTING_P (ctor);
29953
29954 if (PRIMARY_TEMPLATE_P (fn_tmpl))
29955 {
29956 memtmpl = true;
29957
29958 /* For a member template constructor, we need to flatten the two
29959 template parameter lists into one, and then adjust the function
29960 signature accordingly. This gets...complicated. */
29961 tree save_parms = current_template_parms;
29962
29963 /* For a member template we should have two levels of parms/args, one
29964 for the class and one for the constructor. We stripped
29965 specialized args for further enclosing classes above. */
29966 const int depth = 2;
29967 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
29968
29969 /* Template args for translating references to the two-level template
29970 parameters into references to the one-level template parameters we
29971 are creating. */
29972 tree tsubst_args = copy_node (targs);
29973 TMPL_ARGS_LEVEL (tsubst_args, depth)
29974 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
29975
29976 /* Template parms for the constructor template. */
29977 tree ftparms = TREE_VALUE (tparms);
29978 unsigned flen = TREE_VEC_LENGTH (ftparms);
29979 /* Template parms for the class template. */
29980 tparms = TREE_CHAIN (tparms);
29981 tree ctparms = TREE_VALUE (tparms);
29982 unsigned clen = TREE_VEC_LENGTH (ctparms);
29983 /* Template parms for the deduction guide start as a copy of the
29984 template parms for the class. We set current_template_parms for
29985 lookup_template_class_1. */
29986 current_template_parms = tparms = copy_node (tparms);
29987 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
29988 for (unsigned i = 0; i < clen; ++i)
29989 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
29990
29991 /* Now we need to rewrite the constructor parms to append them to the
29992 class parms. */
29993 for (unsigned i = 0; i < flen; ++i)
29994 {
29995 unsigned index = i + clen;
29996 unsigned level = 1;
29997 tree oldelt = TREE_VEC_ELT (ftparms, i);
29998 tree newelt
29999 = rewrite_tparm_list (oldelt, index, level,
30000 tsubst_args, i, complain);
30001 if (newelt == error_mark_node)
30002 ok = false;
30003 TREE_VEC_ELT (new_vec, index) = newelt;
30004 }
30005
30006 /* Now we have a final set of template parms to substitute into the
30007 function signature. */
30008 targs = template_parms_to_args (tparms);
30009 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
30010 complain, ctor);
30011 if (fparms == error_mark_node)
30012 ok = false;
30013 if (ci)
30014 {
30015 if (outer_args)
30016 /* FIXME: We'd like to avoid substituting outer template
30017 arguments into the constraint ahead of time, but the
30018 construction of tsubst_args assumes that outer arguments
30019 are already substituted in. */
30020 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30021 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
30022 }
30023
30024 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
30025 cp_unevaluated_operand. */
30026 cp_evaluated ev;
30027 fargs = tsubst (fargs, tsubst_args, complain, ctor);
30028 current_template_parms = save_parms;
30029 }
30030 else
30031 {
30032 /* Substitute in the same arguments to rewrite class members into
30033 references to members of an unknown specialization. */
30034 cp_evaluated ev;
30035 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
30036 fargs = tsubst (fargs, targs, complain, ctor);
30037 if (ci)
30038 {
30039 if (outer_args)
30040 /* FIXME: As above. */
30041 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
30042 ci = tsubst_constraint_info (ci, targs, complain, ctor);
30043 }
30044 }
30045
30046 --processing_template_decl;
30047 if (!ok)
30048 return error_mark_node;
30049 }
30050
30051 if (!memtmpl)
30052 {
30053 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
30054 tparms = copy_node (tparms);
30055 INNERMOST_TEMPLATE_PARMS (tparms)
30056 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
30057 }
30058
30059 tree fntype = build_function_type (type, fparms);
30060 tree ded_fn = build_lang_decl_loc (loc,
30061 FUNCTION_DECL,
30062 dguide_name (type), fntype);
30063 DECL_ARGUMENTS (ded_fn) = fargs;
30064 DECL_ARTIFICIAL (ded_fn) = true;
30065 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
30066 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
30067 DECL_ARTIFICIAL (ded_tmpl) = true;
30068 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
30069 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
30070 if (DECL_P (ctor))
30071 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
30072 if (ci)
30073 set_constraints (ded_tmpl, ci);
30074
30075 return ded_tmpl;
30076 }
30077
30078 /* Add to LIST the member types for the reshaped initializer CTOR. */
30079
30080 static tree
30081 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
30082 {
30083 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
30084 tree idx, val; unsigned i;
30085 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
30086 {
30087 tree ftype = elt ? elt : TREE_TYPE (idx);
30088 if (BRACE_ENCLOSED_INITIALIZER_P (val)
30089 && CONSTRUCTOR_BRACES_ELIDED_P (val))
30090 {
30091 tree subelt = NULL_TREE;
30092 if (TREE_CODE (ftype) == ARRAY_TYPE)
30093 subelt = TREE_TYPE (ftype);
30094 list = collect_ctor_idx_types (val, list, subelt);
30095 continue;
30096 }
30097 tree arg = NULL_TREE;
30098 if (i == v->length() - 1
30099 && PACK_EXPANSION_P (ftype))
30100 /* Give the trailing pack expansion parameter a default argument to
30101 match aggregate initialization behavior, even if we deduce the
30102 length of the pack separately to more than we have initializers. */
30103 arg = build_constructor (init_list_type_node, NULL);
30104 /* if ei is of array type and xi is a braced-init-list or string literal,
30105 Ti is an rvalue reference to the declared type of ei */
30106 STRIP_ANY_LOCATION_WRAPPER (val);
30107 if (TREE_CODE (ftype) == ARRAY_TYPE
30108 && (BRACE_ENCLOSED_INITIALIZER_P (val)
30109 || TREE_CODE (val) == STRING_CST))
30110 {
30111 if (TREE_CODE (val) == STRING_CST)
30112 ftype = cp_build_qualified_type
30113 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
30114 ftype = (cp_build_reference_type
30115 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
30116 }
30117 list = tree_cons (arg, ftype, list);
30118 }
30119
30120 return list;
30121 }
30122
30123 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
30124
30125 static bool
30126 is_spec_or_derived (tree etype, tree tmpl)
30127 {
30128 if (!etype || !CLASS_TYPE_P (etype))
30129 return false;
30130
30131 etype = cv_unqualified (etype);
30132 tree type = TREE_TYPE (tmpl);
30133 tree tparms = (INNERMOST_TEMPLATE_PARMS
30134 (DECL_TEMPLATE_PARMS (tmpl)));
30135 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
30136 int err = unify (tparms, targs, type, etype,
30137 UNIFY_ALLOW_DERIVED, /*explain*/false);
30138 ggc_free (targs);
30139 return !err;
30140 }
30141
30142 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
30143 INIT. */
30144
30145 static tree
30146 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
30147 {
30148 if (cxx_dialect < cxx20)
30149 return NULL_TREE;
30150
30151 if (init == NULL_TREE)
30152 return NULL_TREE;
30153
30154 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30155 {
30156 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30157 tree tinfo = get_template_info (under);
30158 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
30159 return alias_ctad_tweaks (tmpl, guide);
30160 return NULL_TREE;
30161 }
30162
30163 /* We might be creating a guide for a class member template, e.g.,
30164
30165 template<typename U> struct A {
30166 template<typename T> struct B { T t; };
30167 };
30168
30169 At this point, A will have been instantiated. Below, we need to
30170 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
30171 const bool member_template_p
30172 = (DECL_TEMPLATE_INFO (tmpl)
30173 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
30174 tree type = TREE_TYPE (tmpl);
30175 tree template_type = (member_template_p
30176 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
30177 : type);
30178 if (!CP_AGGREGATE_TYPE_P (template_type))
30179 return NULL_TREE;
30180
30181 /* No aggregate candidate for copy-initialization. */
30182 if (args->length() == 1)
30183 {
30184 tree val = (*args)[0];
30185 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
30186 return NULL_TREE;
30187 }
30188
30189 /* If we encounter a problem, we just won't add the candidate. */
30190 tsubst_flags_t complain = tf_none;
30191
30192 tree parms = NULL_TREE;
30193 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30194 {
30195 init = reshape_init (template_type, init, complain);
30196 if (init == error_mark_node)
30197 return NULL_TREE;
30198 parms = collect_ctor_idx_types (init, parms);
30199 /* If we're creating a deduction guide for a member class template,
30200 we've used the original template pattern type for the reshape_init
30201 above; this is done because we want PARMS to be a template parameter
30202 type, something that can be deduced when used as a function template
30203 parameter. At this point the outer class template has already been
30204 partially instantiated (we deferred the deduction until the enclosing
30205 scope is non-dependent). Therefore we have to partially instantiate
30206 PARMS, so that its template level is properly reduced and we don't get
30207 mismatches when deducing types using the guide with PARMS. */
30208 if (member_template_p)
30209 {
30210 ++processing_template_decl;
30211 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
30212 --processing_template_decl;
30213 }
30214 }
30215 else if (TREE_CODE (init) == TREE_LIST)
30216 {
30217 int len = list_length (init);
30218 for (tree field = TYPE_FIELDS (type);
30219 len;
30220 --len, field = DECL_CHAIN (field))
30221 {
30222 field = next_aggregate_field (field);
30223 if (!field)
30224 return NULL_TREE;
30225 tree ftype = finish_decltype_type (field, true, complain);
30226 parms = tree_cons (NULL_TREE, ftype, parms);
30227 }
30228 }
30229 else
30230 /* Aggregate initialization doesn't apply to an initializer expression. */
30231 return NULL_TREE;
30232
30233 if (parms)
30234 {
30235 tree last = parms;
30236 parms = nreverse (parms);
30237 TREE_CHAIN (last) = void_list_node;
30238 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
30239 return guide;
30240 }
30241
30242 return NULL_TREE;
30243 }
30244
30245 /* UGUIDES are the deduction guides for the underlying template of alias
30246 template TMPL; adjust them to be deduction guides for TMPL.
30247
30248 This routine also handles C++23 inherited CTAD, in which case TMPL is a
30249 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
30250 the template parameter list of the alias template (equivalently, of the
30251 derived class) and TREE_VALUE the defining-type-id (equivalently, the
30252 base whose guides we're inheriting). UGUIDES are the base's guides. */
30253
30254 static tree
30255 alias_ctad_tweaks (tree tmpl, tree uguides)
30256 {
30257 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
30258 class type (9.2.8.2) where the template-name names an alias template A,
30259 the defining-type-id of A must be of the form
30260
30261 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30262
30263 as specified in 9.2.8.2. The guides of A are the set of functions or
30264 function templates formed as follows. For each function or function
30265 template f in the guides of the template named by the simple-template-id
30266 of the defining-type-id, the template arguments of the return type of f
30267 are deduced from the defining-type-id of A according to the process in
30268 13.10.2.5 with the exception that deduction does not fail if not all
30269 template arguments are deduced. Let g denote the result of substituting
30270 these deductions into f. If substitution succeeds, form a function or
30271 function template f' with the following properties and add it to the set
30272 of guides of A:
30273
30274 * The function type of f' is the function type of g.
30275
30276 * If f is a function template, f' is a function template whose template
30277 parameter list consists of all the template parameters of A (including
30278 their default template arguments) that appear in the above deductions or
30279 (recursively) in their default template arguments, followed by the
30280 template parameters of f that were not deduced (including their default
30281 template arguments), otherwise f' is not a function template.
30282
30283 * The associated constraints (13.5.2) are the conjunction of the
30284 associated constraints of g and a constraint that is satisfied if and only
30285 if the arguments of A are deducible (see below) from the return type.
30286
30287 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
30288 be so as well.
30289
30290 * If f was generated from a deduction-guide (12.4.1.8), then f' is
30291 considered to be so as well.
30292
30293 * The explicit-specifier of f' is the explicit-specifier of g (if
30294 any). */
30295
30296 enum { alias, inherited } ctad_kind;
30297 tree atype, fullatparms, utype;
30298 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
30299 {
30300 ctad_kind = alias;
30301 atype = TREE_TYPE (tmpl);
30302 fullatparms = DECL_TEMPLATE_PARMS (tmpl);
30303 utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30304 }
30305 else
30306 {
30307 ctad_kind = inherited;
30308 atype = NULL_TREE;
30309 fullatparms = TREE_PURPOSE (tmpl);
30310 utype = TREE_VALUE (tmpl);
30311 }
30312
30313 tsubst_flags_t complain = tf_warning_or_error;
30314 tree aguides = NULL_TREE;
30315 tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
30316 unsigned natparms = TREE_VEC_LENGTH (atparms);
30317 for (ovl_iterator iter (uguides); iter; ++iter)
30318 {
30319 tree f = *iter;
30320 tree in_decl = f;
30321 location_t loc = DECL_SOURCE_LOCATION (f);
30322 tree ret = TREE_TYPE (TREE_TYPE (f));
30323 tree fprime = f;
30324 if (TREE_CODE (f) == TEMPLATE_DECL)
30325 {
30326 processing_template_decl_sentinel ptds (/*reset*/false);
30327 ++processing_template_decl;
30328
30329 /* Deduce template arguments for f from the type-id of A. */
30330 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
30331 unsigned len = TREE_VEC_LENGTH (ftparms);
30332 tree targs = make_tree_vec (len);
30333 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
30334 if (err)
30335 /* CWG2664: Discard any deductions, still build the guide. */
30336 for (unsigned i = 0; i < len; ++i)
30337 TREE_VEC_ELT (targs, i) = NULL_TREE;
30338
30339 /* The number of parms for f' is the number of parms of A used in
30340 the deduced arguments plus non-deduced parms of f. */
30341 unsigned ndlen = 0;
30342 unsigned j;
30343 for (unsigned i = 0; i < len; ++i)
30344 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30345 ++ndlen;
30346 find_template_parameter_info ftpi (fullatparms);
30347 ftpi.find_in_recursive (targs);
30348 unsigned nusedatparms = ftpi.num_found ();
30349 unsigned nfparms = nusedatparms + ndlen;
30350 tree gtparms = make_tree_vec (nfparms);
30351
30352 /* Set current_template_parms as in build_deduction_guide. */
30353 auto ctp = make_temp_override (current_template_parms);
30354 current_template_parms = copy_node (fullatparms);
30355 TREE_VALUE (current_template_parms) = gtparms;
30356
30357 j = 0;
30358 unsigned level = 1;
30359
30360 /* First copy over the used parms of A. */
30361 tree atargs = make_tree_vec (natparms);
30362 for (unsigned i = 0; i < natparms; ++i)
30363 {
30364 tree elt = TREE_VEC_ELT (atparms, i);
30365 if (ftpi.found (elt))
30366 {
30367 unsigned index = j++;
30368 tree nelt = rewrite_tparm_list (elt, index, level,
30369 atargs, i, complain);
30370 TREE_VEC_ELT (gtparms, index) = nelt;
30371 }
30372 }
30373 gcc_checking_assert (j == nusedatparms);
30374
30375 /* Adjust the deduced template args for f to refer to the A parms
30376 with their new indexes. */
30377 if (nusedatparms && nusedatparms != natparms)
30378 targs = tsubst_template_args (targs, atargs, complain, in_decl);
30379
30380 /* Now rewrite the non-deduced parms of f. */
30381 for (unsigned i = 0; ndlen && i < len; ++i)
30382 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30383 {
30384 --ndlen;
30385 unsigned index = j++;
30386 tree oldlist = TREE_VEC_ELT (ftparms, i);
30387 tree list = rewrite_tparm_list (oldlist, index, level,
30388 targs, i, complain);
30389 TREE_VEC_ELT (gtparms, index) = list;
30390 }
30391 gtparms = build_tree_list (size_one_node, gtparms);
30392
30393 /* Substitute the deduced arguments plus the rewritten template
30394 parameters into f to get g. This covers the type, copyness,
30395 guideness, and explicit-specifier. */
30396 tree g;
30397 {
30398 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30399 if cp_unevaluated_operand. */
30400 cp_evaluated ev;
30401 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
30402 /*use_spec_table=*/false);
30403 }
30404 if (g == error_mark_node)
30405 continue;
30406 if (nfparms == 0)
30407 {
30408 /* The targs are all non-dependent, so g isn't a template. */
30409 fprime = g;
30410 ret = TREE_TYPE (TREE_TYPE (fprime));
30411 goto non_template;
30412 }
30413 DECL_USE_TEMPLATE (g) = 0;
30414 fprime = build_template_decl (g, gtparms, false);
30415 DECL_TEMPLATE_RESULT (fprime) = g;
30416 TREE_TYPE (fprime) = TREE_TYPE (g);
30417 tree gtargs = template_parms_to_args (gtparms);
30418 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
30419 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
30420
30421 /* Substitute the associated constraints. */
30422 tree ci = get_constraints (f);
30423 if (ci)
30424 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
30425 if (ci == error_mark_node)
30426 continue;
30427
30428 /* Add a constraint that the return type matches the instantiation of
30429 A with the same template arguments. */
30430 ret = TREE_TYPE (TREE_TYPE (fprime));
30431 if (ctad_kind == alias
30432 && (!same_type_p (atype, ret)
30433 /* FIXME this should mean they don't compare as equivalent. */
30434 || dependent_alias_template_spec_p (atype, nt_opaque)))
30435 {
30436 tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
30437 ci = append_constraint (ci, same);
30438 }
30439
30440 if (ci)
30441 {
30442 remove_constraints (fprime);
30443 set_constraints (fprime, ci);
30444 }
30445 }
30446 else
30447 {
30448 /* For a non-template deduction guide, if the arguments of A aren't
30449 deducible from the return type, don't add the candidate. */
30450 non_template:
30451 if (ctad_kind == alias
30452 && !type_targs_deducible_from (tmpl, ret))
30453 continue;
30454 }
30455
30456 /* Rewrite the return type of the inherited guide in terms of the
30457 derived class. This is specified as replacing the return type R
30458 with typename CC<R>::type where the partially specialized CC maps a
30459 base class specialization to a specialization of the derived class
30460 having such a base (inducing substitution failure if no such derived
30461 class exists).
30462
30463 As specified this mapping would be done at instantiation time using
30464 non-dependent template arguments, but we do it ahead of time using
30465 the generic arguments. This seems to be good enough since generic
30466 deduction should succeed only if concrete deduction would. */
30467 if (ctad_kind == inherited)
30468 {
30469 processing_template_decl_sentinel ptds (/*reset*/false);
30470 if (TREE_CODE (fprime) == TEMPLATE_DECL)
30471 ++processing_template_decl;
30472
30473 tree targs = type_targs_deducible_from (tmpl, ret);
30474 if (!targs)
30475 continue;
30476
30477 if (TREE_CODE (f) != TEMPLATE_DECL)
30478 fprime = copy_decl (fprime);
30479 tree fntype = TREE_TYPE (fprime);
30480 ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
30481 in_decl, NULL_TREE, false, complain);
30482 fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype));
30483 TREE_TYPE (fprime) = fntype;
30484 if (TREE_CODE (fprime) == TEMPLATE_DECL)
30485 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
30486 }
30487
30488 aguides = lookup_add (fprime, aguides);
30489 }
30490
30491 return aguides;
30492 }
30493
30494 /* CTOR is a using-decl inheriting the constructors of some base of the class
30495 template TMPL; adjust the base's guides be deduction guides for TMPL. */
30496
30497 static tree
30498 inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
30499 {
30500 /* [over.match.class.deduct]: In addition, if C is defined and inherits
30501 constructors ([namespace.udecl]) from a direct base class denoted in the
30502 base-specifier-list by a class-or-decltype B, let A be an alias template
30503 whose template parameter list is that of C and whose defining-type-id is
30504 B. If A is a deducible template ([dcl.type.simple]), the set contains the
30505 guides of A with the return type R of each guide replaced with typename
30506 CC::type given a class template
30507
30508 template <typename> class CC;
30509
30510 whose primary template is not defined and with a single partial
30511 specialization whose template parameter list is that of A and whose
30512 template argument list is a specialization of A with the template argument
30513 list of A ([temp.dep.type]) having a member typedef type designating a
30514 template specialization with the template argument list of A but with C as
30515 the template. */
30516
30517 /* FIXME: Also recognize inherited constructors of the form 'using C::B::B',
30518 which seem to be represented with TYPENAME_TYPE C::B as USING_DECL_SCOPE?
30519 And recognize constructors inherited from a non-dependent base class, which
30520 seem to be missing from the overload set entirely? */
30521 tree scope = USING_DECL_SCOPE (ctor);
30522 if (!CLASS_TYPE_P (scope)
30523 || !CLASSTYPE_TEMPLATE_INFO (scope)
30524 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
30525 return NULL_TREE;
30526
30527 tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
30528 bool any_dguides_p;
30529 tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
30530 any_dguides_p, complain);
30531 return alias_ctad_tweaks (t, uguides);
30532 }
30533
30534 /* If template arguments for TMPL can be deduced from TYPE, return
30535 the deduced arguments, otherwise return NULL_TREE.
30536 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
30537 [over.match.class.deduct].
30538
30539 This check is specified in terms of partial specialization, so the behavior
30540 should be parallel to that of get_partial_spec_bindings. */
30541
30542 tree
30543 type_targs_deducible_from (tree tmpl, tree type)
30544 {
30545 tree tparms, ttype;
30546 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
30547 {
30548 /* If tmpl is a class template, this is trivial: it's deducible if
30549 TYPE is a specialization of TMPL. */
30550 if (DECL_CLASS_TEMPLATE_P (tmpl))
30551 {
30552 if (CLASS_TYPE_P (type)
30553 && CLASSTYPE_TEMPLATE_INFO (type)
30554 && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
30555 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
30556 else
30557 return NULL_TREE;
30558 }
30559
30560 /* Otherwise it's an alias template. */
30561 tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
30562 ttype = TREE_TYPE (tmpl);
30563 }
30564 else
30565 {
30566 /* TMPL is a synthetic alias template represented as a TREE_LIST as
30567 per alias_ctad_tweaks. */
30568 tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
30569 ttype = TREE_VALUE (tmpl);
30570 tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype));
30571 }
30572
30573 int len = TREE_VEC_LENGTH (tparms);
30574 tree targs = make_tree_vec (len);
30575 bool tried_array_deduction = (cxx_dialect < cxx17);
30576
30577 again:
30578 if (unify (tparms, targs, ttype, type,
30579 UNIFY_ALLOW_NONE, false))
30580 return NULL_TREE;
30581
30582 /* We don't fail on an undeduced targ the second time through (like
30583 get_partial_spec_bindings) because we're going to try defaults. */
30584 for (int i = 0; i < len; ++i)
30585 if (! TREE_VEC_ELT (targs, i))
30586 {
30587 tree tparm = TREE_VEC_ELT (tparms, i);
30588 tparm = TREE_VALUE (tparm);
30589
30590 if (!tried_array_deduction
30591 && TREE_CODE (tparm) == TYPE_DECL)
30592 {
30593 try_array_deduction (tparms, targs, ttype);
30594 tried_array_deduction = true;
30595 if (TREE_VEC_ELT (targs, i))
30596 goto again;
30597 }
30598 /* If the type parameter is a parameter pack, then it will be deduced
30599 to an empty parameter pack. This is another case that doesn't model
30600 well as partial specialization. */
30601 if (template_parameter_pack_p (tparm))
30602 {
30603 tree arg;
30604 if (TREE_CODE (tparm) == PARM_DECL)
30605 {
30606 arg = make_node (NONTYPE_ARGUMENT_PACK);
30607 TREE_CONSTANT (arg) = 1;
30608 }
30609 else
30610 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
30611 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
30612 TREE_VEC_ELT (targs, i) = arg;
30613 }
30614 }
30615
30616 /* Maybe add in default template args. This seems like a flaw in the
30617 specification in terms of partial specialization, since it says the
30618 partial specialization has the template parameter list of A, but a
30619 partial specialization can't have default targs. */
30620 targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
30621 if (targs == error_mark_node)
30622 return NULL_TREE;
30623
30624 /* I believe we don't need the template_template_parm_bindings_ok_p call
30625 because coerce_template_parms did coerce_template_template_parms. */
30626
30627 if (!constraints_satisfied_p (tmpl, targs))
30628 return NULL_TREE;
30629
30630 return targs;
30631 }
30632
30633 /* Return artificial deduction guides built from the constructors of class
30634 template TMPL. */
30635
30636 static tree
30637 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
30638 {
30639 tree outer_args = outer_template_args (tmpl);
30640 tree type = TREE_TYPE (most_general_template (tmpl));
30641
30642 tree cands = NULL_TREE;
30643
30644 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
30645 {
30646 /* We handle C++23 inherited CTAD below. */
30647 if (iter.using_p ())
30648 continue;
30649
30650 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
30651 cands = lookup_add (guide, cands);
30652 }
30653
30654 if (cxx_dialect >= cxx23)
30655 for (tree ctor : ovl_range (CLASSTYPE_CONSTRUCTORS (type)))
30656 if (TREE_CODE (ctor) == USING_DECL)
30657 {
30658 tree uguides = inherited_ctad_tweaks (tmpl, ctor, complain);
30659 if (uguides)
30660 cands = lookup_add (uguides, cands);
30661 }
30662
30663 /* Add implicit default constructor deduction guide. */
30664 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
30665 {
30666 tree guide = build_deduction_guide (type, type, outer_args,
30667 complain);
30668 cands = lookup_add (guide, cands);
30669 }
30670
30671 /* Add copy guide. */
30672 {
30673 tree gtype = build_reference_type (type);
30674 tree guide = build_deduction_guide (type, gtype, outer_args,
30675 complain);
30676 cands = lookup_add (guide, cands);
30677 }
30678
30679 return cands;
30680 }
30681
30682 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
30683
30684 /* Return the non-aggregate deduction guides for deducible template TMPL. The
30685 aggregate candidate is added separately because it depends on the
30686 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30687 guide. */
30688
30689 static tree
30690 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
30691 {
30692 tree guides = NULL_TREE;
30693 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30694 {
30695 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30696 tree tinfo = get_template_info (under);
30697 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
30698 complain);
30699 }
30700 else
30701 {
30702 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
30703 dguide_name (tmpl),
30704 LOOK_want::NORMAL, /*complain*/false);
30705 if (guides == error_mark_node)
30706 guides = NULL_TREE;
30707 else
30708 any_dguides_p = true;
30709 }
30710
30711 /* Cache the deduction guides for a template. We also remember the result of
30712 lookup, and rebuild everything if it changes; should be very rare. */
30713 /* FIXME: Also rebuild if this is a class template that inherits guides from a
30714 base class, and lookup for the latter changed. */
30715 tree_pair_p cache = NULL;
30716 if (tree_pair_p &r
30717 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
30718 {
30719 cache = r;
30720 if (cache->purpose == guides)
30721 return cache->value;
30722 }
30723 else
30724 {
30725 r = cache = ggc_cleared_alloc<tree_pair_s> ();
30726 cache->purpose = guides;
30727 }
30728
30729 tree cands = NULL_TREE;
30730 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30731 cands = alias_ctad_tweaks (tmpl, guides);
30732 else
30733 {
30734 cands = ctor_deduction_guides_for (tmpl, complain);
30735 for (ovl_iterator it (guides); it; ++it)
30736 cands = lookup_add (*it, cands);
30737 }
30738
30739 cache->value = cands;
30740 return cands;
30741 }
30742
30743 /* Return whether TMPL is a (class template argument-) deducible template. */
30744
30745 bool
30746 ctad_template_p (tree tmpl)
30747 {
30748 /* A deducible template is either a class template or is an alias template
30749 whose defining-type-id is of the form
30750
30751 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30752
30753 where the nested-name-specifier (if any) is non-dependent and the
30754 template-name of the simple-template-id names a deducible template. */
30755
30756 if (DECL_CLASS_TEMPLATE_P (tmpl)
30757 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30758 return true;
30759 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
30760 return false;
30761 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30762 if (tree tinfo = get_template_info (orig))
30763 return ctad_template_p (TI_TEMPLATE (tinfo));
30764 return false;
30765 }
30766
30767 /* Deduce template arguments for the class template placeholder PTYPE for
30768 template TMPL based on the initializer INIT, and return the resulting
30769 type. */
30770
30771 static tree
30772 do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
30773 int flags, tsubst_flags_t complain)
30774 {
30775 /* We should have handled this in the caller. */
30776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30777 return ptype;
30778
30779 /* If the class was erroneous, don't try to deduce, because that
30780 can generate a lot of diagnostic. */
30781 if (TREE_TYPE (tmpl)
30782 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
30783 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
30784 return ptype;
30785
30786 /* Wait until the enclosing scope is non-dependent. */
30787 if (DECL_CLASS_SCOPE_P (tmpl)
30788 && dependent_type_p (DECL_CONTEXT (tmpl)))
30789 return ptype;
30790
30791 /* Initializing one placeholder from another. */
30792 if (init
30793 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
30794 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
30795 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
30796 == TEMPLATE_PARM_INDEX)))
30797 && is_auto (TREE_TYPE (init))
30798 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
30799 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
30800
30801 if (!ctad_template_p (tmpl))
30802 {
30803 if (complain & tf_error)
30804 error ("non-deducible template %qT used without template arguments", tmpl);
30805 return error_mark_node;
30806 }
30807 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
30808 {
30809 if (complain & tf_error)
30810 {
30811 /* Be permissive with equivalent alias templates. */
30812 tree u = get_underlying_template (tmpl);
30813 diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
30814 bool complained
30815 = emit_diagnostic (dk, input_location, 0,
30816 "alias template deduction only available "
30817 "with %<-std=c++20%> or %<-std=gnu++20%>");
30818 if (u == tmpl)
30819 return error_mark_node;
30820 else if (complained)
30821 {
30822 inform (input_location, "use %qD directly instead", u);
30823 tmpl = u;
30824 }
30825 }
30826 else
30827 return error_mark_node;
30828 }
30829
30830 /* Wait until the initializer is non-dependent. */
30831 if (type_dependent_expression_p (init))
30832 return ptype;
30833
30834 if (outer_targs)
30835 {
30836 int args_depth = TMPL_ARGS_DEPTH (outer_targs);
30837 int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
30838 if (parms_depth > 1)
30839 {
30840 /* Substitute outer arguments into this CTAD template from the
30841 current instantiation. */
30842 int want = std::min (args_depth, parms_depth - 1);
30843 outer_targs = strip_innermost_template_args (outer_targs,
30844 args_depth - want);
30845 tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
30846 if (tmpl == error_mark_node)
30847 return error_mark_node;
30848 }
30849 }
30850
30851 /* Don't bother with the alias rules for an equivalent template. */
30852 tmpl = get_underlying_template (tmpl);
30853
30854 tree type = TREE_TYPE (tmpl);
30855
30856 bool try_list_cand = false;
30857 bool list_init_p = false;
30858
30859 releasing_vec rv_args = NULL;
30860 vec<tree,va_gc> *&args = *&rv_args;
30861 if (init == NULL_TREE)
30862 args = make_tree_vector ();
30863 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
30864 {
30865 list_init_p = true;
30866 try_list_cand = true;
30867 if (CONSTRUCTOR_NELTS (init) == 1
30868 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
30869 {
30870 /* As an exception, the first phase in 16.3.1.7 (considering the
30871 initializer list as a single argument) is omitted if the
30872 initializer list consists of a single expression of type cv U,
30873 where U is a specialization of C or a class derived from a
30874 specialization of C. */
30875 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
30876 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
30877 try_list_cand = false;
30878 }
30879 if (try_list_cand || is_std_init_list (type))
30880 args = make_tree_vector_single (init);
30881 else
30882 args = make_tree_vector_from_ctor (init);
30883 }
30884 else if (TREE_CODE (init) == TREE_LIST)
30885 args = make_tree_vector_from_list (init);
30886 else
30887 args = make_tree_vector_single (init);
30888
30889 /* Do this now to avoid problems with erroneous args later on. */
30890 args = resolve_args (args, complain);
30891 if (args == NULL)
30892 return error_mark_node;
30893
30894 bool any_dguides_p = false;
30895 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
30896 if (cands == error_mark_node)
30897 return error_mark_node;
30898
30899 /* Prune explicit deduction guides in copy-initialization context (but
30900 not copy-list-initialization). */
30901 bool elided = false;
30902 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
30903 {
30904 for (lkp_iterator iter (cands); !elided && iter; ++iter)
30905 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30906 elided = true;
30907
30908 if (elided)
30909 {
30910 /* Found a nonconverting guide, prune the candidates. */
30911 tree pruned = NULL_TREE;
30912 for (lkp_iterator iter (cands); iter; ++iter)
30913 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30914 pruned = lookup_add (*iter, pruned);
30915
30916 cands = pruned;
30917 }
30918 }
30919
30920 if (!any_dguides_p)
30921 if (tree guide = maybe_aggr_guide (tmpl, init, args))
30922 cands = lookup_add (guide, cands);
30923
30924 tree fndecl = error_mark_node;
30925
30926 /* If this is list-initialization and the class has a list guide, first
30927 try deducing from the list as a single argument, as [over.match.list]. */
30928 if (try_list_cand)
30929 {
30930 tree list_cands = NULL_TREE;
30931 for (tree dg : lkp_range (cands))
30932 if (is_list_ctor (dg))
30933 list_cands = lookup_add (dg, list_cands);
30934 if (list_cands)
30935 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
30936 if (fndecl == error_mark_node)
30937 {
30938 /* That didn't work, now try treating the list as a sequence of
30939 arguments. */
30940 release_tree_vector (args);
30941 args = make_tree_vector_from_ctor (init);
30942 args = resolve_args (args, complain);
30943 if (args == NULL)
30944 return error_mark_node;
30945 }
30946 }
30947
30948 if (elided && !cands)
30949 {
30950 error ("cannot deduce template arguments for copy-initialization"
30951 " of %qT, as it has no non-explicit deduction guides or "
30952 "user-declared constructors", type);
30953 return error_mark_node;
30954 }
30955 else if (!cands && fndecl == error_mark_node)
30956 {
30957 error ("cannot deduce template arguments of %qT, as it has no viable "
30958 "deduction guides", type);
30959 return error_mark_node;
30960 }
30961
30962 if (fndecl == error_mark_node)
30963 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
30964
30965 if (fndecl == error_mark_node)
30966 {
30967 if (complain & tf_warning_or_error)
30968 {
30969 error ("class template argument deduction failed:");
30970 perform_dguide_overload_resolution (cands, args, complain);
30971 if (elided)
30972 inform (input_location, "explicit deduction guides not considered "
30973 "for copy-initialization");
30974 }
30975 return error_mark_node;
30976 }
30977 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30978 constructor is chosen, the initialization is ill-formed. */
30979 else if (flags & LOOKUP_ONLYCONVERTING)
30980 {
30981 if (DECL_NONCONVERTING_P (fndecl))
30982 {
30983 if (complain & tf_warning_or_error)
30984 {
30985 // TODO: Pass down location from cp_finish_decl.
30986 error ("class template argument deduction for %qT failed: "
30987 "explicit deduction guide selected in "
30988 "copy-list-initialization", type);
30989 inform (DECL_SOURCE_LOCATION (fndecl),
30990 "explicit deduction guide declared here");
30991
30992 }
30993 return error_mark_node;
30994 }
30995 }
30996
30997 /* If CTAD succeeded but the type doesn't have any explicit deduction
30998 guides, this deduction might not be what the user intended. */
30999 if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
31000 {
31001 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
31002 || global_dc->m_warn_system_headers)
31003 && warning (OPT_Wctad_maybe_unsupported,
31004 "%qT may not intend to support class template argument "
31005 "deduction", type))
31006 inform (input_location, "add a deduction guide to suppress this "
31007 "warning");
31008 }
31009
31010 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
31011 cp_type_quals (ptype));
31012 }
31013
31014 /* Return true if INIT is an unparenthesized id-expression or an
31015 unparenthesized class member access. Used for the argument of
31016 decltype(auto). */
31017
31018 bool
31019 unparenthesized_id_or_class_member_access_p (tree init)
31020 {
31021 STRIP_ANY_LOCATION_WRAPPER (init);
31022
31023 /* We need to be able to tell '(r)' and 'r' apart (when it's of
31024 reference type). Only the latter is an id-expression. */
31025 if (REFERENCE_REF_P (init)
31026 && !REF_PARENTHESIZED_P (init))
31027 init = TREE_OPERAND (init, 0);
31028 return (DECL_P (init)
31029 || ((TREE_CODE (init) == COMPONENT_REF
31030 || TREE_CODE (init) == SCOPE_REF)
31031 && !REF_PARENTHESIZED_P (init)));
31032 }
31033
31034 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
31035 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
31036 The CONTEXT determines the context in which auto deduction is performed
31037 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
31038
31039 OUTER_TARGS is used during template argument deduction (context == adc_unify)
31040 to properly substitute the result. It's also used in the adc_unify and
31041 adc_requirement contexts to communicate the necessary template arguments
31042 to satisfaction. OUTER_TARGS is ignored in other contexts.
31043
31044 Additionally for adc_unify contexts TMPL is the template for which TYPE
31045 is a template parameter type.
31046
31047 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
31048 scope may be appended to the list of deduced template arguments prior to
31049 determining constraint satisfaction as appropriate. */
31050
31051 tree
31052 do_auto_deduction (tree type, tree init, tree auto_node,
31053 tsubst_flags_t complain /* = tf_warning_or_error */,
31054 auto_deduction_context context /* = adc_unspecified */,
31055 tree outer_targs /* = NULL_TREE */,
31056 int flags /* = LOOKUP_NORMAL */,
31057 tree tmpl /* = NULL_TREE */)
31058 {
31059 if (type == error_mark_node || init == error_mark_node)
31060 return error_mark_node;
31061
31062 if (init && type_dependent_expression_p (init)
31063 && context != adc_unify)
31064 /* Defining a subset of type-dependent expressions that we can deduce
31065 from ahead of time isn't worth the trouble. */
31066 return type;
31067
31068 /* Similarly, we can't deduce from another undeduced decl. */
31069 if (init && undeduced_auto_decl (init))
31070 return type;
31071
31072 /* We may be doing a partial substitution, but we still want to replace
31073 auto_node. */
31074 complain &= ~tf_partial;
31075
31076 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
31077 {
31078 /* We don't recurse here because we can't deduce from a nested
31079 initializer_list. */
31080 if (CONSTRUCTOR_ELTS (init))
31081 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
31082 elt.value = resolve_nondeduced_context (elt.value, complain);
31083 }
31084 else if (init)
31085 init = resolve_nondeduced_context (init, complain);
31086
31087 /* In C++23, we must deduce the type to int&& for code like
31088 decltype(auto) f(int&& x) { return (x); }
31089 or
31090 auto&& f(int x) { return x; }
31091 so we use treat_lvalue_as_rvalue_p. But don't do it for
31092 decltype(auto) f(int x) { return x; }
31093 where we should deduce 'int' rather than 'int&&'; transmogrifying
31094 INIT to an rvalue would break that. */
31095 tree r;
31096 if (cxx_dialect >= cxx23
31097 && context == adc_return_type
31098 && (!AUTO_IS_DECLTYPE (auto_node)
31099 || !unparenthesized_id_or_class_member_access_p (init))
31100 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
31101 /*return*/true)))
31102 init = r;
31103
31104 if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
31105 /* C++17 class template argument deduction. */
31106 return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
31107
31108 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
31109 /* Nothing we can do with this, even in deduction context. */
31110 return type;
31111
31112 location_t loc = cp_expr_loc_or_input_loc (init);
31113
31114 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
31115 with either a new invented type template parameter U or, if the
31116 initializer is a braced-init-list (8.5.4), with
31117 std::initializer_list<U>. */
31118 if (BRACE_ENCLOSED_INITIALIZER_P (init))
31119 {
31120 if (!DIRECT_LIST_INIT_P (init))
31121 type = listify_autos (type, auto_node);
31122 else if (CONSTRUCTOR_NELTS (init) == 1)
31123 init = CONSTRUCTOR_ELT (init, 0)->value;
31124 else
31125 {
31126 if (complain & tf_warning_or_error)
31127 {
31128 if (permerror (loc, "direct-list-initialization of "
31129 "%<auto%> requires exactly one element"))
31130 inform (loc,
31131 "for deduction to %<std::initializer_list%>, use copy-"
31132 "list-initialization (i.e. add %<=%> before the %<{%>)");
31133 }
31134 type = listify_autos (type, auto_node);
31135 }
31136 }
31137
31138 if (type == error_mark_node || init == error_mark_node)
31139 return error_mark_node;
31140
31141 tree targs;
31142 if (context == adc_decomp_type
31143 && auto_node == type
31144 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
31145 {
31146 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
31147 and initializer has array type, deduce cv-qualified array type. */
31148 targs = make_tree_vec (1);
31149 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
31150 }
31151 else if (AUTO_IS_DECLTYPE (auto_node))
31152 {
31153 const bool id = unparenthesized_id_or_class_member_access_p (init);
31154 tree deduced = finish_decltype_type (init, id, complain);
31155 deduced = canonicalize_type_argument (deduced, complain);
31156 if (deduced == error_mark_node)
31157 return error_mark_node;
31158 targs = make_tree_vec (1);
31159 TREE_VEC_ELT (targs, 0) = deduced;
31160 }
31161 else
31162 {
31163 if (error_operand_p (init))
31164 return error_mark_node;
31165
31166 tree parms = build_tree_list (NULL_TREE, type);
31167 tree tparms;
31168
31169 if (flag_concepts_ts)
31170 tparms = extract_autos (type);
31171 else
31172 {
31173 tparms = make_tree_vec (1);
31174 TREE_VEC_ELT (tparms, 0)
31175 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
31176 }
31177
31178 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
31179 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
31180 DEDUCE_CALL,
31181 NULL, /*explain_p=*/false);
31182 if (val > 0)
31183 {
31184 if (processing_template_decl)
31185 /* Try again at instantiation time. */
31186 return type;
31187 if (type && type != error_mark_node
31188 && (complain & tf_error))
31189 /* If type is error_mark_node a diagnostic must have been
31190 emitted by now. Also, having a mention to '<type error>'
31191 in the diagnostic is not really useful to the user. */
31192 {
31193 if (cfun
31194 && FNDECL_USED_AUTO (current_function_decl)
31195 && (auto_node
31196 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
31197 && LAMBDA_FUNCTION_P (current_function_decl))
31198 error_at (loc, "unable to deduce lambda return type from %qE",
31199 init);
31200 else
31201 error_at (loc, "unable to deduce %qT from %qE", type, init);
31202 type_unification_real (tparms, targs, parms, &init, 1, 0,
31203 DEDUCE_CALL,
31204 NULL, /*explain_p=*/true);
31205 }
31206 return error_mark_node;
31207 }
31208 }
31209
31210 /* Check any placeholder constraints against the deduced type. */
31211 if (processing_template_decl && context == adc_unify)
31212 /* Constraints will be checked after deduction. */;
31213 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
31214 {
31215 if (processing_template_decl)
31216 {
31217 gcc_checking_assert (context == adc_variable_type
31218 || context == adc_return_type
31219 || context == adc_decomp_type);
31220 gcc_checking_assert (!type_dependent_expression_p (init));
31221 /* If the constraint is dependent, we need to wait until
31222 instantiation time to resolve the placeholder. */
31223 if (placeholder_type_constraint_dependent_p (constr))
31224 return type;
31225 }
31226
31227 if (context == adc_return_type
31228 || context == adc_variable_type
31229 || context == adc_decomp_type)
31230 if (tree fn = current_function_decl)
31231 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
31232 {
31233 outer_targs = DECL_TEMPLATE_INFO (fn)
31234 ? DECL_TI_ARGS (fn) : NULL_TREE;
31235 if (LAMBDA_FUNCTION_P (fn))
31236 {
31237 /* As in satisfy_declaration_constraints. */
31238 tree regen_args = lambda_regenerating_args (fn);
31239 if (outer_targs)
31240 outer_targs = add_to_template_args (regen_args, outer_targs);
31241 else
31242 outer_targs = regen_args;
31243 }
31244 }
31245
31246 tree full_targs = outer_targs;
31247 if (context == adc_unify && tmpl)
31248 full_targs = add_outermost_template_args (tmpl, full_targs);
31249 full_targs = add_to_template_args (full_targs, targs);
31250
31251 /* HACK: Compensate for callers not always communicating all levels of
31252 outer template arguments by filling in the outermost missing levels
31253 with dummy levels before checking satisfaction. We'll still crash
31254 if the constraint depends on a template argument belonging to one of
31255 these missing levels, but this hack otherwise allows us to handle a
31256 large subset of possible constraints (including all non-dependent
31257 constraints). */
31258 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
31259 - TMPL_ARGS_DEPTH (full_targs)))
31260 {
31261 tree dummy_levels = make_tree_vec (missing_levels);
31262 for (int i = 0; i < missing_levels; ++i)
31263 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
31264 full_targs = add_to_template_args (dummy_levels, full_targs);
31265 }
31266
31267 if (!constraints_satisfied_p (auto_node, full_targs))
31268 {
31269 if (complain & tf_warning_or_error)
31270 {
31271 auto_diagnostic_group d;
31272 switch (context)
31273 {
31274 case adc_unspecified:
31275 case adc_unify:
31276 error_at (loc, "placeholder constraints not satisfied");
31277 break;
31278 case adc_variable_type:
31279 case adc_decomp_type:
31280 error_at (loc, "deduced initializer does not satisfy "
31281 "placeholder constraints");
31282 break;
31283 case adc_return_type:
31284 error_at (loc, "deduced return type does not satisfy "
31285 "placeholder constraints");
31286 break;
31287 case adc_requirement:
31288 error_at (loc, "deduced expression type does not satisfy "
31289 "placeholder constraints");
31290 break;
31291 }
31292 diagnose_constraints (loc, auto_node, full_targs);
31293 }
31294 return error_mark_node;
31295 }
31296 }
31297
31298 if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
31299 {
31300 /* Substitute this level-less auto via tsubst by temporarily
31301 overriding its level to 1. */
31302 TEMPLATE_TYPE_LEVEL (auto_node) = 1;
31303 type = tsubst (type, targs, complain, NULL_TREE);
31304 TEMPLATE_TYPE_LEVEL (auto_node) = 0;
31305 return type;
31306 }
31307
31308 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
31309 /* The outer template arguments are already substituted into type
31310 (but we still may have used them for constraint checking above). */;
31311 else if (context == adc_unify)
31312 targs = add_to_template_args (outer_targs, targs);
31313 else if (processing_template_decl)
31314 targs = add_to_template_args (current_template_args (), targs);
31315 return tsubst (type, targs, complain, NULL_TREE);
31316 }
31317
31318 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
31319 result. */
31320
31321 tree
31322 splice_late_return_type (tree type, tree late_return_type)
31323 {
31324 if (late_return_type)
31325 {
31326 gcc_assert (is_auto (type) || seen_error ());
31327 return late_return_type;
31328 }
31329
31330 if (tree auto_node = find_type_usage (type, is_auto))
31331 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
31332 {
31333 /* In an abbreviated function template we didn't know we were dealing
31334 with a function template when we saw the auto return type, so rebuild
31335 the return type using an auto with the correct level. */
31336 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
31337 tree auto_vec = make_tree_vec (1);
31338 TREE_VEC_ELT (auto_vec, 0) = new_auto;
31339 tree targs = add_outermost_template_args (current_template_args (),
31340 auto_vec);
31341 /* Also rebuild the constraint info in terms of the new auto. */
31342 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
31343 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
31344 = build_tree_list (current_template_parms,
31345 tsubst_constraint (TREE_VALUE (ci), targs,
31346 tf_none, NULL_TREE));
31347 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
31348 return tsubst (type, targs, tf_none, NULL_TREE);
31349 }
31350 return type;
31351 }
31352
31353 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
31354 'decltype(auto)' or a deduced class template. */
31355
31356 bool
31357 is_auto (const_tree type)
31358 {
31359 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
31360 && (TYPE_IDENTIFIER (type) == auto_identifier
31361 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
31362 return true;
31363 else
31364 return false;
31365 }
31366
31367 /* for_each_template_parm callback for type_uses_auto. */
31368
31369 int
31370 is_auto_r (tree tp, void */*data*/)
31371 {
31372 return is_auto (tp);
31373 }
31374
31375 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
31376 a use of `auto'. Returns NULL_TREE otherwise. */
31377
31378 tree
31379 type_uses_auto (tree type)
31380 {
31381 if (type == NULL_TREE)
31382 return NULL_TREE;
31383
31384 /* For parameter packs, check the contents of the pack. */
31385 if (PACK_EXPANSION_P (type))
31386 type = PACK_EXPANSION_PATTERN (type);
31387
31388 if (flag_concepts_ts)
31389 {
31390 /* The Concepts TS allows multiple autos in one type-specifier; just
31391 return the first one we find, do_auto_deduction will collect all of
31392 them. */
31393 if (uses_template_parms (type))
31394 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
31395 /*visited*/NULL, /*nondeduced*/false);
31396 else
31397 return NULL_TREE;
31398 }
31399 else
31400 return find_type_usage (type, is_auto);
31401 }
31402
31403 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
31404 concepts are enabled, auto is acceptable in template arguments, but
31405 only when TEMPL identifies a template class. Return TRUE if any
31406 such errors were reported. */
31407
31408 bool
31409 check_auto_in_tmpl_args (tree tmpl, tree args)
31410 {
31411 if (!flag_concepts_ts)
31412 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
31413 have already been rejected by the parser more generally. */
31414 return false;
31415
31416 /* If there were previous errors, nevermind. */
31417 if (!args || TREE_CODE (args) != TREE_VEC)
31418 return false;
31419
31420 /* If TMPL is an identifier, we're parsing and we can't tell yet
31421 whether TMPL is supposed to be a type, a function or a variable.
31422 We'll only be able to tell during template substitution, so we
31423 expect to be called again then. If concepts are enabled and we
31424 know we have a type, we're ok. */
31425 if (identifier_p (tmpl)
31426 || (DECL_P (tmpl)
31427 && (DECL_TYPE_TEMPLATE_P (tmpl)
31428 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
31429 return false;
31430
31431 /* Quickly search for any occurrences of auto; usually there won't
31432 be any, and then we'll avoid allocating the vector. */
31433 if (!type_uses_auto (args))
31434 return false;
31435
31436 bool errors = false;
31437
31438 tree vec = extract_autos (args);
31439 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
31440 {
31441 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
31442 error_at (DECL_SOURCE_LOCATION (xauto),
31443 "invalid use of %qT in template argument", xauto);
31444 errors = true;
31445 }
31446
31447 return errors;
31448 }
31449
31450 /* Recursively walk over && expressions searching for EXPR. Return a reference
31451 to that expression. */
31452
31453 static tree *find_template_requirement (tree *t, tree key)
31454 {
31455 if (*t == key)
31456 return t;
31457 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
31458 {
31459 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
31460 return p;
31461 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
31462 return p;
31463 }
31464 return 0;
31465 }
31466
31467 /* Convert the generic type parameters in PARM that match the types given in the
31468 range [START_IDX, END_IDX) from the current_template_parms into generic type
31469 packs. */
31470
31471 tree
31472 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
31473 {
31474 tree current = current_template_parms;
31475 int depth = TMPL_PARMS_DEPTH (current);
31476 current = INNERMOST_TEMPLATE_PARMS (current);
31477 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
31478
31479 for (int i = 0; i < start_idx; ++i)
31480 TREE_VEC_ELT (replacement, i)
31481 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
31482
31483 for (int i = start_idx; i < end_idx; ++i)
31484 {
31485 /* Create a distinct parameter pack type from the current parm and add it
31486 to the replacement args to tsubst below into the generic function
31487 parameter. */
31488 tree node = TREE_VEC_ELT (current, i);
31489 tree o = TREE_TYPE (TREE_VALUE (node));
31490 tree t = copy_type (o);
31491 TEMPLATE_TYPE_PARM_INDEX (t)
31492 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
31493 t, 0, 0, tf_none);
31494 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
31495 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
31496 TYPE_MAIN_VARIANT (t) = t;
31497 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
31498 TYPE_CANONICAL (t) = canonical_type_parameter (t);
31499 TREE_VEC_ELT (replacement, i) = t;
31500
31501 /* Replace the current template parameter with new pack. */
31502 TREE_VALUE (node) = TREE_CHAIN (t);
31503
31504 /* Surgically adjust the associated constraint of adjusted parameter
31505 and it's corresponding contribution to the current template
31506 requirements. */
31507 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
31508 {
31509 tree id = unpack_concept_check (constr);
31510 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
31511 /* Use UNKNOWN_LOCATION so write_template_args can tell the
31512 difference between this and a fold the user wrote. */
31513 location_t loc = UNKNOWN_LOCATION;
31514 tree fold = finish_left_unary_fold_expr (loc, constr,
31515 TRUTH_ANDIF_EXPR);
31516 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
31517
31518 /* If there was a constraint, we also need to replace that in
31519 the template requirements, which we've already built. */
31520 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
31521 reqs = find_template_requirement (reqs, constr);
31522 *reqs = fold;
31523 }
31524 }
31525
31526 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
31527 TREE_VEC_ELT (replacement, i)
31528 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
31529
31530 /* If there are more levels then build up the replacement with the outer
31531 template parms. */
31532 if (depth > 1)
31533 replacement = add_to_template_args (template_parms_to_args
31534 (TREE_CHAIN (current_template_parms)),
31535 replacement);
31536
31537 return tsubst (parm, replacement, tf_none, NULL_TREE);
31538 }
31539
31540 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
31541 0..N-1. */
31542
31543 void
31544 declare_integer_pack (void)
31545 {
31546 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
31547 build_function_type_list (integer_type_node,
31548 integer_type_node,
31549 NULL_TREE),
31550 NULL_TREE, ECF_CONST);
31551 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
31552 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
31553 CP_BUILT_IN_INTEGER_PACK);
31554 }
31555
31556 /* Walk the decl or type specialization table calling FN on each
31557 entry. */
31558
31559 void
31560 walk_specializations (bool decls_p,
31561 void (*fn) (bool decls_p, spec_entry *entry, void *data),
31562 void *data)
31563 {
31564 spec_hash_table *table = decls_p ? decl_specializations
31565 : type_specializations;
31566 spec_hash_table::iterator end (table->end ());
31567 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
31568 fn (decls_p, *iter, data);
31569 }
31570
31571 /* Lookup the specialization of *ELT, in the decl or type
31572 specialization table. Return the SPEC that's already there, or
31573 NULL if nothing. */
31574
31575 tree
31576 match_mergeable_specialization (bool decl_p, spec_entry *elt)
31577 {
31578 hash_table<spec_hasher> *specializations
31579 = decl_p ? decl_specializations : type_specializations;
31580 hashval_t hash = spec_hasher::hash (elt);
31581 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
31582
31583 if (slot)
31584 return (*slot)->spec;
31585
31586 return NULL_TREE;
31587 }
31588
31589 /* Return flags encoding whether SPEC is on the instantiation and/or
31590 specialization lists of TMPL. */
31591
31592 unsigned
31593 get_mergeable_specialization_flags (tree tmpl, tree decl)
31594 {
31595 unsigned flags = 0;
31596
31597 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
31598 inst; inst = TREE_CHAIN (inst))
31599 if (TREE_VALUE (inst) == decl)
31600 {
31601 flags |= 1;
31602 break;
31603 }
31604
31605 if (CLASS_TYPE_P (TREE_TYPE (decl))
31606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
31607 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
31608 /* Only need to search if DECL is a partial specialization. */
31609 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
31610 part; part = TREE_CHAIN (part))
31611 if (TREE_VALUE (part) == decl)
31612 {
31613 flags |= 2;
31614 break;
31615 }
31616
31617 return flags;
31618 }
31619
31620 /* Add a new specialization described by SPEC. DECL is the
31621 maybe-template decl and FLAGS is as returned from
31622 get_mergeable_specialization_flags. */
31623
31624 void
31625 add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
31626 unsigned flags)
31627 {
31628 hashval_t hash = spec_hasher::hash (elt);
31629 if (decl_p)
31630 {
31631 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
31632
31633 gcc_checking_assert (!*slot);
31634 auto entry = ggc_alloc<spec_entry> ();
31635 *entry = *elt;
31636 *slot = entry;
31637 }
31638 else
31639 {
31640 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
31641
31642 /* We don't distinguish different constrained partial type
31643 specializations, so there could be duplicates. Everything else
31644 must be new. */
31645 if (!(flags & 2 && *slot))
31646 {
31647 gcc_checking_assert (!*slot);
31648
31649 auto entry = ggc_alloc<spec_entry> ();
31650 *entry = *elt;
31651 *slot = entry;
31652 }
31653 }
31654
31655 if (flags & 1)
31656 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
31657 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
31658
31659 if (flags & 2)
31660 {
31661 /* A partial specialization. */
31662 tree cons = tree_cons (elt->args, decl,
31663 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
31664 TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
31665 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
31666 }
31667 }
31668
31669 /* Set up the hash tables for template instantiations. */
31670
31671 void
31672 init_template_processing (void)
31673 {
31674 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
31675 type_specializations = hash_table<spec_hasher>::create_ggc (37);
31676
31677 if (cxx_dialect >= cxx11)
31678 declare_integer_pack ();
31679 }
31680
31681 /* Print stats about the template hash tables for -fstats. */
31682
31683 void
31684 print_template_statistics (void)
31685 {
31686 fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
31687 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
31688 (fmt_size_t) decl_specializations->size (),
31689 (fmt_size_t) decl_specializations->elements (),
31690 decl_specializations->collisions ());
31691 fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
31692 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
31693 (fmt_size_t) type_specializations->size (),
31694 (fmt_size_t) type_specializations->elements (),
31695 type_specializations->collisions ());
31696 }
31697
31698 #if CHECKING_P
31699
31700 namespace selftest {
31701
31702 /* Verify that type_dependent_expression_p () works correctly, even
31703 in the presence of location wrapper nodes. */
31704
31705 static void
31706 test_type_dependent_expression_p ()
31707 {
31708 location_t loc = BUILTINS_LOCATION;
31709
31710 tree name = get_identifier ("foo");
31711
31712 /* If no templates are involved, nothing is type-dependent. */
31713 gcc_assert (!processing_template_decl);
31714 ASSERT_FALSE (type_dependent_expression_p (name));
31715
31716 ++processing_template_decl;
31717
31718 /* Within a template, an unresolved name is always type-dependent. */
31719 ASSERT_TRUE (type_dependent_expression_p (name));
31720
31721 /* Ensure it copes with NULL_TREE and errors. */
31722 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
31723 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
31724
31725 /* A USING_DECL in a template should be type-dependent, even if wrapped
31726 with a location wrapper (PR c++/83799). */
31727 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
31728 TREE_TYPE (using_decl) = integer_type_node;
31729 ASSERT_TRUE (type_dependent_expression_p (using_decl));
31730 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
31731 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
31732 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
31733
31734 --processing_template_decl;
31735 }
31736
31737 /* Run all of the selftests within this file. */
31738
31739 void
31740 cp_pt_cc_tests ()
31741 {
31742 test_type_dependent_expression_p ();
31743 }
31744
31745 } // namespace selftest
31746
31747 #endif /* #if CHECKING_P */
31748
31749 #include "gt-cp-pt.h"
This page took 1.483326 seconds and 5 git commands to generate.