]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / cp / pt.cc
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2023 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
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
53
54 /* The PENDING_TEMPLATES is a list of templates whose instantiations
55 have been deferred, either because their definitions were not yet
56 available, or because we were putting off doing the work. */
57 struct GTY ((chain_next ("%h.next"))) pending_template
58 {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static vec<int> inline_parm_levels;
70
71 static GTY(()) struct tinst_level *current_tinst_level;
72
73 static GTY(()) vec<tree, va_gc> *saved_access_scope;
74
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
79
80 // -------------------------------------------------------------------------- //
81 // Local Specialization Stack
82 //
83 // Implementation of the RAII helper for creating new local
84 // specializations.
85 local_specialization_stack::local_specialization_stack (lss_policy policy)
86 : saved (local_specializations)
87 {
88 if (policy == lss_nop)
89 ;
90 else if (policy == lss_blank || !saved)
91 local_specializations = new hash_map<tree, tree>;
92 else
93 local_specializations = new hash_map<tree, tree>(*saved);
94 }
95
96 local_specialization_stack::~local_specialization_stack ()
97 {
98 if (local_specializations != saved)
99 {
100 delete local_specializations;
101 local_specializations = saved;
102 }
103 }
104
105 /* True if we've recursed into fn_type_unification too many times. */
106 static bool excessive_deduction_depth;
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (tree, tree);
111 static hashval_t hash (spec_entry *);
112 static bool equal (spec_entry *, spec_entry *);
113 };
114
115 /* The general template is not in these tables. */
116 typedef hash_table<spec_hasher> spec_hash_table;
117 static GTY (()) spec_hash_table *decl_specializations;
118 static GTY (()) spec_hash_table *type_specializations;
119
120 /* Contains canonical template parameter types. The vector is indexed by
121 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
122 TREE_LIST, whose TREE_VALUEs contain the canonical template
123 parameters of various types and levels. */
124 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
125
126 #define UNIFY_ALLOW_NONE 0
127 #define UNIFY_ALLOW_MORE_CV_QUAL 1
128 #define UNIFY_ALLOW_LESS_CV_QUAL 2
129 #define UNIFY_ALLOW_DERIVED 4
130 #define UNIFY_ALLOW_INTEGER 8
131 #define UNIFY_ALLOW_OUTER_LEVEL 16
132 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
133 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
134
135 enum template_base_result {
136 tbr_incomplete_type,
137 tbr_ambiguous_baseclass,
138 tbr_success
139 };
140
141 static bool resolve_overloaded_unification (tree, tree, tree, tree,
142 unification_kind_t, int,
143 bool);
144 static int try_one_overload (tree, tree, tree, tree, tree,
145 unification_kind_t, int, bool, bool);
146 static int unify (tree, tree, tree, tree, int, bool);
147 static void add_pending_template (tree);
148 static tree reopen_tinst_level (struct tinst_level *);
149 static tree tsubst_initializer_list (tree, tree);
150 static tree get_partial_spec_bindings (tree, tree, tree);
151 static void tsubst_enum (tree, tree, tree);
152 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
153 static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
154 struct conversion **, bool);
155 static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
156 tree*, tree*, tree);
157 static int type_unification_real (tree, tree, tree, const tree *,
158 unsigned int, int, unification_kind_t,
159 vec<deferred_access_check, va_gc> **,
160 bool);
161 static void note_template_header (int);
162 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
163 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
164 static tree convert_template_argument (tree, tree, tree,
165 tsubst_flags_t, int, tree);
166 static tree for_each_template_parm (tree, tree_fn_t, void*,
167 hash_set<tree> *, bool, tree_fn_t = NULL);
168 static tree expand_template_argument_pack (tree);
169 static tree build_template_parm_index (int, int, int, tree, tree);
170 static bool inline_needs_template_parms (tree, bool);
171 static void push_inline_template_parms_recursive (tree, int);
172 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
173 static int mark_template_parm (tree, void *);
174 static int template_parm_this_level_p (tree, void *);
175 static tree tsubst_friend_function (tree, tree);
176 static tree tsubst_friend_class (tree, tree);
177 static int can_complete_type_without_circularity (tree);
178 static tree get_bindings (tree, tree, tree, bool);
179 static int template_decl_level (tree);
180 static int check_cv_quals_for_unify (int, tree, tree);
181 static int unify_pack_expansion (tree, tree, tree,
182 tree, unification_kind_t, bool, bool);
183 static tree copy_template_args (tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
186 tree most_specialized_partial_spec (tree, tsubst_flags_t);
187 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
188 static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int);
189 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
191 static bool check_specialization_scope (void);
192 static tree process_partial_specialization (tree);
193 static enum template_base_result get_template_base (tree, tree, tree, tree,
194 bool , tree *);
195 static tree try_class_unification (tree, tree, tree, tree, bool);
196 static bool class_nttp_const_wrapper_p (tree t);
197 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
198 tree, tree);
199 static bool template_template_parm_bindings_ok_p (tree, tree);
200 static void tsubst_default_arguments (tree, tsubst_flags_t);
201 static tree for_each_template_parm_r (tree *, int *, void *);
202 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
203 static void copy_default_args_to_explicit_spec (tree);
204 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
205 static bool dependent_template_arg_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t);
209 static void perform_instantiation_time_access_checks (tree, tree);
210 static tree listify (tree);
211 static tree listify_autos (tree, tree);
212 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
213 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
214 static bool complex_alias_template_p (const_tree tmpl);
215 static tree get_underlying_template (tree);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218 static tree make_argument_pack (tree);
219 static tree enclosing_instantiation_of (tree tctx);
220 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
221 static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
222
223 /* Make the current scope suitable for access checking when we are
224 processing T. T can be FUNCTION_DECL for instantiated function
225 template, VAR_DECL for static member variable, or TYPE_DECL for
226 for a class or alias template (needed by instantiate_decl). */
227
228 void
229 push_access_scope (tree t)
230 {
231 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
232 || TREE_CODE (t) == TYPE_DECL);
233
234 if (DECL_FRIEND_CONTEXT (t))
235 push_nested_class (DECL_FRIEND_CONTEXT (t));
236 else if (DECL_IMPLICIT_TYPEDEF_P (t)
237 && CLASS_TYPE_P (TREE_TYPE (t)))
238 push_nested_class (TREE_TYPE (t));
239 else if (DECL_CLASS_SCOPE_P (t))
240 push_nested_class (DECL_CONTEXT (t));
241 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
242 /* An artificial deduction guide should have the same access as
243 the constructor. */
244 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 vec_safe_push (saved_access_scope, current_function_decl);
251 current_function_decl = t;
252 }
253 }
254
255 /* Restore the scope set up by push_access_scope. T is the node we
256 are processing. */
257
258 void
259 pop_access_scope (tree t)
260 {
261 if (TREE_CODE (t) == FUNCTION_DECL)
262 current_function_decl = saved_access_scope->pop();
263
264 if (DECL_FRIEND_CONTEXT (t)
265 || (DECL_IMPLICIT_TYPEDEF_P (t)
266 && CLASS_TYPE_P (TREE_TYPE (t)))
267 || DECL_CLASS_SCOPE_P (t)
268 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error_at (DECL_SOURCE_LOCATION (decl),
306 "data member %qD cannot be a member template", decl);
307 else if (DECL_TEMPLATE_INFO (decl))
308 {
309 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
310 {
311 check_member_template (DECL_TI_TEMPLATE (decl));
312 return DECL_TI_TEMPLATE (decl);
313 }
314 else
315 return decl;
316 }
317 else
318 error_at (DECL_SOURCE_LOCATION (decl),
319 "invalid member template declaration %qD", decl);
320
321 return error_mark_node;
322 }
323
324 /* Create a template info node. */
325
326 tree
327 build_template_info (tree template_decl, tree template_args)
328 {
329 tree result = make_node (TEMPLATE_INFO);
330 TI_TEMPLATE (result) = template_decl;
331 TI_ARGS (result) = template_args;
332 return result;
333 }
334
335 /* Return the template info node corresponding to T, whatever T is. */
336
337 tree
338 get_template_info (const_tree t)
339 {
340 tree tinfo = NULL_TREE;
341
342 if (!t || t == error_mark_node)
343 return NULL;
344
345 if (TREE_CODE (t) == NAMESPACE_DECL
346 || TREE_CODE (t) == PARM_DECL)
347 return NULL;
348
349 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
350 tinfo = DECL_TEMPLATE_INFO (t);
351
352 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
353 t = TREE_TYPE (t);
354
355 if (OVERLOAD_TYPE_P (t))
356 tinfo = TYPE_TEMPLATE_INFO (t);
357 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
358 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
359
360 return tinfo;
361 }
362
363 /* Returns the template nesting level of the indicated class TYPE.
364
365 For example, in:
366 template <class T>
367 struct A
368 {
369 template <class U>
370 struct B {};
371 };
372
373 A<T>::B<U> has depth two, while A<T> has depth one.
374 Both A<T>::B<int> and A<int>::B<U> have depth one, if
375 they are instantiations, not specializations.
376
377 This function is guaranteed to return 0 if passed NULL_TREE so
378 that, for example, `template_class_depth (current_class_type)' is
379 always safe. */
380
381 int
382 template_class_depth (tree type)
383 {
384 int depth;
385
386 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
387 {
388 tree tinfo = get_template_info (type);
389
390 if (tinfo
391 && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
392 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
393 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
394 ++depth;
395
396 if (DECL_P (type))
397 {
398 if (tree fctx = DECL_FRIEND_CONTEXT (type))
399 type = fctx;
400 else
401 type = CP_DECL_CONTEXT (type);
402 }
403 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
404 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
405 else
406 type = CP_TYPE_CONTEXT (type);
407 }
408
409 return depth;
410 }
411
412 /* Return TRUE if NODE instantiates a template that has arguments of
413 its own, be it directly a primary template or indirectly through a
414 partial specializations. */
415 static bool
416 instantiates_primary_template_p (tree node)
417 {
418 tree tinfo = get_template_info (node);
419 if (!tinfo)
420 return false;
421
422 tree tmpl = TI_TEMPLATE (tinfo);
423 if (PRIMARY_TEMPLATE_P (tmpl))
424 return true;
425
426 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
427 return false;
428
429 /* So now we know we have a specialization, but it could be a full
430 or a partial specialization. To tell which, compare the depth of
431 its template arguments with those of its context. */
432
433 tree ctxt = DECL_CONTEXT (tmpl);
434 tree ctinfo = get_template_info (ctxt);
435 if (!ctinfo)
436 return true;
437
438 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
439 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
440 }
441
442 /* Subroutine of maybe_begin_member_template_processing.
443 Returns true if processing DECL needs us to push template parms. */
444
445 static bool
446 inline_needs_template_parms (tree decl, bool nsdmi)
447 {
448 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
449 return false;
450
451 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
452 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
453 }
454
455 /* Subroutine of maybe_begin_member_template_processing.
456 Push the template parms in PARMS, starting from LEVELS steps into the
457 chain, and ending at the beginning, since template parms are listed
458 innermost first. */
459
460 static void
461 push_inline_template_parms_recursive (tree parmlist, int levels)
462 {
463 tree parms = TREE_VALUE (parmlist);
464 int i;
465
466 if (levels > 1)
467 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
468
469 ++processing_template_decl;
470 current_template_parms
471 = tree_cons (size_int (current_template_depth + 1),
472 parms, current_template_parms);
473 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
474
475 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
476 NULL);
477 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
478 {
479 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
480
481 if (error_operand_p (parm))
482 continue;
483
484 gcc_assert (DECL_P (parm));
485
486 switch (TREE_CODE (parm))
487 {
488 case TYPE_DECL:
489 case TEMPLATE_DECL:
490 pushdecl (parm);
491 break;
492
493 case PARM_DECL:
494 /* Push the CONST_DECL. */
495 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
496 break;
497
498 default:
499 gcc_unreachable ();
500 }
501 }
502 }
503
504 /* Restore the template parameter context for a member template, a
505 friend template defined in a class definition, or a non-template
506 member of template class. */
507
508 void
509 maybe_begin_member_template_processing (tree decl)
510 {
511 tree parms;
512 int levels = 0;
513 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
514
515 if (nsdmi)
516 {
517 tree ctx = DECL_CONTEXT (decl);
518 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
519 /* Disregard full specializations (c++/60999). */
520 && uses_template_parms (ctx)
521 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
522 }
523
524 if (inline_needs_template_parms (decl, nsdmi))
525 {
526 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
527 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
528
529 if (DECL_TEMPLATE_SPECIALIZATION (decl))
530 {
531 --levels;
532 parms = TREE_CHAIN (parms);
533 }
534
535 push_inline_template_parms_recursive (parms, levels);
536 }
537
538 /* Remember how many levels of template parameters we pushed so that
539 we can pop them later. */
540 inline_parm_levels.safe_push (levels);
541 }
542
543 /* Undo the effects of maybe_begin_member_template_processing. */
544
545 void
546 maybe_end_member_template_processing (void)
547 {
548 int i;
549 int last;
550
551 if (inline_parm_levels.length () == 0)
552 return;
553
554 last = inline_parm_levels.pop ();
555 for (i = 0; i < last; ++i)
556 {
557 --processing_template_decl;
558 current_template_parms = TREE_CHAIN (current_template_parms);
559 poplevel (0, 0, 0);
560 }
561 }
562
563 /* Return a new template argument vector which contains all of ARGS,
564 but has as its innermost set of arguments the EXTRA_ARGS. */
565
566 tree
567 add_to_template_args (tree args, tree extra_args)
568 {
569 tree new_args;
570 int extra_depth;
571 int i;
572 int j;
573
574 if (args == NULL_TREE || extra_args == error_mark_node)
575 return extra_args;
576
577 extra_depth = TMPL_ARGS_DEPTH (extra_args);
578 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
579
580 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
582
583 for (j = 1; j <= extra_depth; ++j, ++i)
584 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
585
586 return new_args;
587 }
588
589 /* Like add_to_template_args, but only the outermost ARGS are added to
590 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
591 (EXTRA_ARGS) levels are added. This function is used to combine
592 the template arguments from a partial instantiation with the
593 template arguments used to attain the full instantiation from the
594 partial instantiation.
595
596 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
597
598 tree
599 add_outermost_template_args (tree args, tree extra_args)
600 {
601 tree new_args;
602
603 if (!args)
604 return extra_args;
605 if (TREE_CODE (args) == TEMPLATE_DECL)
606 {
607 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
608 args = TI_ARGS (ti);
609 }
610
611 /* If there are more levels of EXTRA_ARGS than there are ARGS,
612 something very fishy is going on. */
613 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
614
615 /* If *all* the new arguments will be the EXTRA_ARGS, just return
616 them. */
617 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
618 return extra_args;
619
620 /* For the moment, we make ARGS look like it contains fewer levels. */
621 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
622
623 new_args = add_to_template_args (args, extra_args);
624
625 /* Now, we restore ARGS to its full dimensions. */
626 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
627
628 return new_args;
629 }
630
631 /* Return the N levels of innermost template arguments from the ARGS. */
632
633 tree
634 get_innermost_template_args (tree args, int n)
635 {
636 tree new_args;
637 int extra_levels;
638 int i;
639
640 gcc_assert (n >= 0);
641
642 /* If N is 1, just return the innermost set of template arguments. */
643 if (n == 1)
644 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
645
646 /* If we're not removing anything, just return the arguments we were
647 given. */
648 extra_levels = TMPL_ARGS_DEPTH (args) - n;
649 gcc_assert (extra_levels >= 0);
650 if (extra_levels == 0)
651 return args;
652
653 /* Make a new set of arguments, not containing the outer arguments. */
654 new_args = make_tree_vec (n);
655 for (i = 1; i <= n; ++i)
656 SET_TMPL_ARGS_LEVEL (new_args, i,
657 TMPL_ARGS_LEVEL (args, i + extra_levels));
658
659 return new_args;
660 }
661
662 /* The inverse of get_innermost_template_args: Return all but the innermost
663 EXTRA_LEVELS levels of template arguments from the ARGS. */
664
665 static tree
666 strip_innermost_template_args (tree args, int extra_levels)
667 {
668 tree new_args;
669 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
670 int i;
671
672 gcc_assert (n >= 0);
673
674 /* If N is 1, just return the outermost set of template arguments. */
675 if (n == 1)
676 return TMPL_ARGS_LEVEL (args, 1);
677
678 /* If we're not removing anything, just return the arguments we were
679 given. */
680 gcc_assert (extra_levels >= 0);
681 if (extra_levels == 0)
682 return args;
683
684 /* Make a new set of arguments, not containing the inner arguments. */
685 new_args = make_tree_vec (n);
686 for (i = 1; i <= n; ++i)
687 SET_TMPL_ARGS_LEVEL (new_args, i,
688 TMPL_ARGS_LEVEL (args, i));
689
690 return new_args;
691 }
692
693 /* We've got a template header coming up; push to a new level for storing
694 the parms. */
695
696 void
697 begin_template_parm_list (void)
698 {
699 /* We use a non-tag-transparent scope here, which causes pushtag to
700 put tags in this scope, rather than in the enclosing class or
701 namespace scope. This is the right thing, since we want
702 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
703 global template class, push_template_decl handles putting the
704 TEMPLATE_DECL into top-level scope. For a nested template class,
705 e.g.:
706
707 template <class T> struct S1 {
708 template <class T> struct S2 {};
709 };
710
711 pushtag contains special code to insert the TEMPLATE_DECL for S2
712 at the right scope. */
713 begin_scope (sk_template_parms, NULL);
714 ++processing_template_decl;
715 ++processing_template_parmlist;
716 note_template_header (0);
717
718 /* Add a dummy parameter level while we process the parameter list. */
719 current_template_parms
720 = tree_cons (size_int (current_template_depth + 1),
721 make_tree_vec (0),
722 current_template_parms);
723 }
724
725 /* This routine is called when a specialization is declared. If it is
726 invalid to declare a specialization here, an error is reported and
727 false is returned, otherwise this routine will return true. */
728
729 static bool
730 check_specialization_scope (void)
731 {
732 tree scope = current_scope ();
733
734 /* [temp.expl.spec]
735
736 An explicit specialization shall be declared in the namespace of
737 which the template is a member, or, for member templates, in the
738 namespace of which the enclosing class or enclosing class
739 template is a member. An explicit specialization of a member
740 function, member class or static data member of a class template
741 shall be declared in the namespace of which the class template
742 is a member. */
743 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
744 {
745 error ("explicit specialization in non-namespace scope %qD", scope);
746 return false;
747 }
748
749 /* [temp.expl.spec]
750
751 In an explicit specialization declaration for a member of a class
752 template or a member template that appears in namespace scope,
753 the member template and some of its enclosing class templates may
754 remain unspecialized, except that the declaration shall not
755 explicitly specialize a class member template if its enclosing
756 class templates are not explicitly specialized as well. */
757 if (current_template_parms)
758 {
759 error ("enclosing class templates are not explicitly specialized");
760 return false;
761 }
762
763 return true;
764 }
765
766 /* We've just seen template <>. */
767
768 bool
769 begin_specialization (void)
770 {
771 begin_scope (sk_template_spec, NULL);
772 note_template_header (1);
773 return check_specialization_scope ();
774 }
775
776 /* Called at then end of processing a declaration preceded by
777 template<>. */
778
779 void
780 end_specialization (void)
781 {
782 finish_scope ();
783 reset_specialization ();
784 }
785
786 /* Any template <>'s that we have seen thus far are not referring to a
787 function specialization. */
788
789 void
790 reset_specialization (void)
791 {
792 processing_specialization = 0;
793 template_header_count = 0;
794 }
795
796 /* We've just seen a template header. If SPECIALIZATION is nonzero,
797 it was of the form template <>. */
798
799 static void
800 note_template_header (int specialization)
801 {
802 processing_specialization = specialization;
803 template_header_count++;
804 }
805
806 /* We're beginning an explicit instantiation. */
807
808 void
809 begin_explicit_instantiation (void)
810 {
811 gcc_assert (!processing_explicit_instantiation);
812 processing_explicit_instantiation = true;
813 }
814
815
816 void
817 end_explicit_instantiation (void)
818 {
819 gcc_assert (processing_explicit_instantiation);
820 processing_explicit_instantiation = false;
821 }
822
823 /* An explicit specialization or partial specialization of TMPL is being
824 declared. Check that the namespace in which the specialization is
825 occurring is permissible. Returns false iff it is invalid to
826 specialize TMPL in the current namespace. */
827
828 static bool
829 check_specialization_namespace (tree tmpl)
830 {
831 tree tpl_ns = decl_namespace_context (tmpl);
832
833 /* [tmpl.expl.spec]
834
835 An explicit specialization shall be declared in a namespace enclosing the
836 specialized template. An explicit specialization whose declarator-id is
837 not qualified shall be declared in the nearest enclosing namespace of the
838 template, or, if the namespace is inline (7.3.1), any namespace from its
839 enclosing namespace set. */
840 if (current_scope() != DECL_CONTEXT (tmpl)
841 && !at_namespace_scope_p ())
842 {
843 error ("specialization of %qD must appear at namespace scope", tmpl);
844 return false;
845 }
846
847 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
848 /* Same or enclosing namespace. */
849 return true;
850 else
851 {
852 auto_diagnostic_group d;
853 if (permerror (input_location,
854 "specialization of %qD in different namespace", tmpl))
855 inform (DECL_SOURCE_LOCATION (tmpl),
856 " from definition of %q#D", tmpl);
857 return false;
858 }
859 }
860
861 /* SPEC is an explicit instantiation. Check that it is valid to
862 perform this explicit instantiation in the current namespace. */
863
864 static void
865 check_explicit_instantiation_namespace (tree spec)
866 {
867 tree ns;
868
869 /* DR 275: An explicit instantiation shall appear in an enclosing
870 namespace of its template. */
871 ns = decl_namespace_context (spec);
872 if (!is_nested_namespace (current_namespace, ns))
873 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
874 "(which does not enclose namespace %qD)",
875 spec, current_namespace, ns);
876 }
877
878 /* Returns true if TYPE is a new partial specialization that needs to be
879 set up. This may also modify TYPE to point to the correct (new or
880 existing) constrained partial specialization. */
881
882 static bool
883 maybe_new_partial_specialization (tree& type)
884 {
885 /* An implicit instantiation of an incomplete type implies
886 the definition of a new class template.
887
888 template<typename T>
889 struct S;
890
891 template<typename T>
892 struct S<T*>;
893
894 Here, S<T*> is an implicit instantiation of S whose type
895 is incomplete. */
896 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
897 return true;
898
899 /* It can also be the case that TYPE is a completed specialization.
900 Continuing the previous example, suppose we also declare:
901
902 template<typename T>
903 requires Integral<T>
904 struct S<T*>;
905
906 Here, S<T*> refers to the specialization S<T*> defined
907 above. However, we need to differentiate definitions because
908 we intend to define a new partial specialization. In this case,
909 we rely on the fact that the constraints are different for
910 this declaration than that above.
911
912 Note that we also get here for injected class names and
913 late-parsed template definitions. We must ensure that we
914 do not create new type declarations for those cases. */
915 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
916 {
917 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
918 tree args = CLASSTYPE_TI_ARGS (type);
919
920 /* If there are no template parameters, this cannot be a new
921 partial template specialization? */
922 if (!current_template_parms)
923 return false;
924
925 /* The injected-class-name is not a new partial specialization. */
926 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
927 return false;
928
929 /* If the constraints are not the same as those of the primary
930 then, we can probably create a new specialization. */
931 tree type_constr = current_template_constraints ();
932
933 if (type == TREE_TYPE (tmpl))
934 {
935 tree main_constr = get_constraints (tmpl);
936 if (equivalent_constraints (type_constr, main_constr))
937 return false;
938 }
939
940 /* Also, if there's a pre-existing specialization with matching
941 constraints, then this also isn't new. */
942 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
943 while (specs)
944 {
945 tree spec_tmpl = TREE_VALUE (specs);
946 tree spec_args = TREE_PURPOSE (specs);
947 tree spec_constr = get_constraints (spec_tmpl);
948 if (comp_template_args (args, spec_args)
949 && equivalent_constraints (type_constr, spec_constr))
950 {
951 type = TREE_TYPE (spec_tmpl);
952 return false;
953 }
954 specs = TREE_CHAIN (specs);
955 }
956
957 /* Create a new type node (and corresponding type decl)
958 for the newly declared specialization. */
959 tree t = make_class_type (TREE_CODE (type));
960 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
961 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
962
963 /* We only need a separate type node for storing the definition of this
964 partial specialization; uses of S<T*> are unconstrained, so all are
965 equivalent. So keep TYPE_CANONICAL the same. */
966 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
967
968 /* Build the corresponding type decl. */
969 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
970 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
971 DECL_SOURCE_LOCATION (d) = input_location;
972 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
973 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
974
975 set_instantiating_module (d);
976 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
977
978 type = t;
979 return true;
980 }
981
982 return false;
983 }
984
985 /* The TYPE is being declared. If it is a template type, that means it
986 is a partial specialization. Do appropriate error-checking. */
987
988 tree
989 maybe_process_partial_specialization (tree type)
990 {
991 tree context;
992
993 if (type == error_mark_node)
994 return error_mark_node;
995
996 /* A lambda that appears in specialization context is not itself a
997 specialization. */
998 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
999 return type;
1000
1001 /* An injected-class-name is not a specialization. */
1002 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
1003 return type;
1004
1005 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1006 {
1007 error ("name of class shadows template template parameter %qD",
1008 TYPE_NAME (type));
1009 return error_mark_node;
1010 }
1011
1012 context = TYPE_CONTEXT (type);
1013
1014 if (TYPE_ALIAS_P (type))
1015 {
1016 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1017
1018 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1019 error ("specialization of alias template %qD",
1020 TI_TEMPLATE (tinfo));
1021 else
1022 error ("explicit specialization of non-template %qT", type);
1023 return error_mark_node;
1024 }
1025 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1026 {
1027 /* This is for ordinary explicit specialization and partial
1028 specialization of a template class such as:
1029
1030 template <> class C<int>;
1031
1032 or:
1033
1034 template <class T> class C<T*>;
1035
1036 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1037
1038 if (maybe_new_partial_specialization (type))
1039 {
1040 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
1041 && !at_namespace_scope_p ())
1042 return error_mark_node;
1043 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1044 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1045 if (processing_template_decl)
1046 {
1047 tree decl = push_template_decl (TYPE_MAIN_DECL (type));
1048 if (decl == error_mark_node)
1049 return error_mark_node;
1050 return TREE_TYPE (decl);
1051 }
1052 }
1053 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1054 error ("specialization of %qT after instantiation", type);
1055 else if (errorcount && !processing_specialization
1056 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1057 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1058 /* Trying to define a specialization either without a template<> header
1059 or in an inappropriate place. We've already given an error, so just
1060 bail now so we don't actually define the specialization. */
1061 return error_mark_node;
1062 }
1063 else if (CLASS_TYPE_P (type)
1064 && !CLASSTYPE_USE_TEMPLATE (type)
1065 && CLASSTYPE_TEMPLATE_INFO (type)
1066 && context && CLASS_TYPE_P (context)
1067 && CLASSTYPE_TEMPLATE_INFO (context))
1068 {
1069 /* This is for an explicit specialization of member class
1070 template according to [temp.expl.spec/18]:
1071
1072 template <> template <class U> class C<int>::D;
1073
1074 The context `C<int>' must be an implicit instantiation.
1075 Otherwise this is just a member class template declared
1076 earlier like:
1077
1078 template <> class C<int> { template <class U> class D; };
1079 template <> template <class U> class C<int>::D;
1080
1081 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1082 while in the second case, `C<int>::D' is a primary template
1083 and `C<T>::D' may not exist. */
1084
1085 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1086 && !COMPLETE_TYPE_P (type))
1087 {
1088 tree t;
1089 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1090
1091 if (current_namespace
1092 != decl_namespace_context (tmpl))
1093 {
1094 if (permerror (input_location,
1095 "specialization of %qD in different namespace",
1096 type))
1097 inform (DECL_SOURCE_LOCATION (tmpl),
1098 "from definition of %q#D", tmpl);
1099 }
1100
1101 /* Check for invalid specialization after instantiation:
1102
1103 template <> template <> class C<int>::D<int>;
1104 template <> template <class U> class C<int>::D; */
1105
1106 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1107 t; t = TREE_CHAIN (t))
1108 {
1109 tree inst = TREE_VALUE (t);
1110 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1111 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1112 {
1113 /* We already have a full specialization of this partial
1114 instantiation, or a full specialization has been
1115 looked up but not instantiated. Reassign it to the
1116 new member specialization template. */
1117 spec_entry elt;
1118 spec_entry *entry;
1119
1120 elt.tmpl = most_general_template (tmpl);
1121 elt.args = CLASSTYPE_TI_ARGS (inst);
1122 elt.spec = inst;
1123
1124 type_specializations->remove_elt (&elt);
1125
1126 elt.tmpl = tmpl;
1127 CLASSTYPE_TI_ARGS (inst)
1128 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1129
1130 spec_entry **slot
1131 = type_specializations->find_slot (&elt, INSERT);
1132 entry = ggc_alloc<spec_entry> ();
1133 *entry = elt;
1134 *slot = entry;
1135 }
1136 else
1137 /* But if we've had an implicit instantiation, that's a
1138 problem ([temp.expl.spec]/6). */
1139 error ("specialization %qT after instantiation %qT",
1140 type, inst);
1141 }
1142
1143 /* Mark TYPE as a specialization. And as a result, we only
1144 have one level of template argument for the innermost
1145 class template. */
1146 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1147 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1148 CLASSTYPE_TI_ARGS (type)
1149 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1150 }
1151 }
1152 else if (processing_specialization)
1153 {
1154 /* Someday C++0x may allow for enum template specialization. */
1155 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1156 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1157 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1158 "of %qD not allowed by ISO C++", type);
1159 else
1160 {
1161 error ("explicit specialization of non-template %qT", type);
1162 return error_mark_node;
1163 }
1164 }
1165
1166 return type;
1167 }
1168
1169 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1170 gone through coerce_template_parms by now. */
1171
1172 static void
1173 verify_unstripped_args_1 (tree inner)
1174 {
1175 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1176 {
1177 tree arg = TREE_VEC_ELT (inner, i);
1178 if (TREE_CODE (arg) == TEMPLATE_DECL)
1179 /* OK */;
1180 else if (TYPE_P (arg))
1181 gcc_assert (strip_typedefs (arg, NULL) == arg);
1182 else if (ARGUMENT_PACK_P (arg))
1183 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1184 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1185 /* Allow typedefs on the type of a non-type argument, since a
1186 parameter can have them. */;
1187 else
1188 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1189 }
1190 }
1191
1192 static void
1193 verify_unstripped_args (tree args)
1194 {
1195 ++processing_template_decl;
1196 if (!any_dependent_template_arguments_p (args))
1197 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1198 --processing_template_decl;
1199 }
1200
1201 /* Retrieve the specialization (in the sense of [temp.spec] - a
1202 specialization is either an instantiation or an explicit
1203 specialization) of TMPL for the given template ARGS. If there is
1204 no such specialization, return NULL_TREE. The ARGS are a vector of
1205 arguments, or a vector of vectors of arguments, in the case of
1206 templates with more than one level of parameters.
1207
1208 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1209 then we search for a partial specialization matching ARGS. This
1210 parameter is ignored if TMPL is not a class template.
1211
1212 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1213 result is a NONTYPE_ARGUMENT_PACK. */
1214
1215 static tree
1216 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1217 {
1218 if (tmpl == NULL_TREE)
1219 return NULL_TREE;
1220
1221 if (args == error_mark_node)
1222 return NULL_TREE;
1223
1224 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1225 || TREE_CODE (tmpl) == FIELD_DECL);
1226
1227 /* There should be as many levels of arguments as there are
1228 levels of parameters. */
1229 gcc_assert (TMPL_ARGS_DEPTH (args)
1230 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1231 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1232 : template_class_depth (DECL_CONTEXT (tmpl))));
1233
1234 if (flag_checking)
1235 verify_unstripped_args (args);
1236
1237 /* Lambda functions in templates aren't instantiated normally, but through
1238 tsubst_lambda_expr. */
1239 if (lambda_fn_in_template_p (tmpl))
1240 return NULL_TREE;
1241
1242 spec_entry elt;
1243 elt.tmpl = tmpl;
1244 elt.args = args;
1245 elt.spec = NULL_TREE;
1246
1247 spec_hash_table *specializations;
1248 if (DECL_CLASS_TEMPLATE_P (tmpl))
1249 specializations = type_specializations;
1250 else
1251 specializations = decl_specializations;
1252
1253 if (hash == 0)
1254 hash = spec_hasher::hash (&elt);
1255 if (spec_entry *found = specializations->find_with_hash (&elt, hash))
1256 return found->spec;
1257
1258 return NULL_TREE;
1259 }
1260
1261 /* Like retrieve_specialization, but for local declarations. */
1262
1263 tree
1264 retrieve_local_specialization (tree tmpl)
1265 {
1266 if (local_specializations == NULL)
1267 return NULL_TREE;
1268
1269 tree *slot = local_specializations->get (tmpl);
1270 return slot ? *slot : NULL_TREE;
1271 }
1272
1273 /* Returns nonzero iff DECL is a specialization of TMPL. */
1274
1275 int
1276 is_specialization_of (tree decl, tree tmpl)
1277 {
1278 tree t;
1279
1280 if (TREE_CODE (decl) == FUNCTION_DECL)
1281 {
1282 for (t = decl;
1283 t != NULL_TREE;
1284 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1285 if (t == tmpl)
1286 return 1;
1287 }
1288 else
1289 {
1290 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1291
1292 for (t = TREE_TYPE (decl);
1293 t != NULL_TREE;
1294 t = CLASSTYPE_USE_TEMPLATE (t)
1295 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1296 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1297 return 1;
1298 }
1299
1300 return 0;
1301 }
1302
1303 /* Returns nonzero iff DECL is a specialization of friend declaration
1304 FRIEND_DECL according to [temp.friend]. */
1305
1306 bool
1307 is_specialization_of_friend (tree decl, tree friend_decl)
1308 {
1309 bool need_template = true;
1310 int template_depth;
1311
1312 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1313 || TREE_CODE (decl) == TYPE_DECL);
1314
1315 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1316 of a template class, we want to check if DECL is a specialization
1317 if this. */
1318 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1319 && DECL_TEMPLATE_INFO (friend_decl)
1320 && !DECL_USE_TEMPLATE (friend_decl))
1321 {
1322 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1323 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1324 need_template = false;
1325 }
1326 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1327 && !PRIMARY_TEMPLATE_P (friend_decl))
1328 need_template = false;
1329
1330 /* There is nothing to do if this is not a template friend. */
1331 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1332 return false;
1333
1334 if (is_specialization_of (decl, friend_decl))
1335 return true;
1336
1337 /* [temp.friend/6]
1338 A member of a class template may be declared to be a friend of a
1339 non-template class. In this case, the corresponding member of
1340 every specialization of the class template is a friend of the
1341 class granting friendship.
1342
1343 For example, given a template friend declaration
1344
1345 template <class T> friend void A<T>::f();
1346
1347 the member function below is considered a friend
1348
1349 template <> struct A<int> {
1350 void f();
1351 };
1352
1353 For this type of template friend, TEMPLATE_DEPTH below will be
1354 nonzero. To determine if DECL is a friend of FRIEND, we first
1355 check if the enclosing class is a specialization of another. */
1356
1357 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1358 if (template_depth
1359 && DECL_CLASS_SCOPE_P (decl)
1360 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1361 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1362 {
1363 /* Next, we check the members themselves. In order to handle
1364 a few tricky cases, such as when FRIEND_DECL's are
1365
1366 template <class T> friend void A<T>::g(T t);
1367 template <class T> template <T t> friend void A<T>::h();
1368
1369 and DECL's are
1370
1371 void A<int>::g(int);
1372 template <int> void A<int>::h();
1373
1374 we need to figure out ARGS, the template arguments from
1375 the context of DECL. This is required for template substitution
1376 of `T' in the function parameter of `g' and template parameter
1377 of `h' in the above examples. Here ARGS corresponds to `int'. */
1378
1379 tree context = DECL_CONTEXT (decl);
1380 tree args = NULL_TREE;
1381 int current_depth = 0;
1382
1383 while (current_depth < template_depth)
1384 {
1385 if (CLASSTYPE_TEMPLATE_INFO (context))
1386 {
1387 if (current_depth == 0)
1388 args = TYPE_TI_ARGS (context);
1389 else
1390 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1391 current_depth++;
1392 }
1393 context = TYPE_CONTEXT (context);
1394 }
1395
1396 if (TREE_CODE (decl) == FUNCTION_DECL)
1397 {
1398 bool is_template;
1399 tree friend_type;
1400 tree decl_type;
1401 tree friend_args_type;
1402 tree decl_args_type;
1403
1404 /* Make sure that both DECL and FRIEND_DECL are templates or
1405 non-templates. */
1406 is_template = DECL_TEMPLATE_INFO (decl)
1407 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1408 if (need_template ^ is_template)
1409 return false;
1410 else if (is_template)
1411 {
1412 /* If both are templates, check template parameter list. */
1413 tree friend_parms
1414 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1415 args, tf_none);
1416 if (!comp_template_parms
1417 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1418 friend_parms))
1419 return false;
1420
1421 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1422 }
1423 else
1424 decl_type = TREE_TYPE (decl);
1425
1426 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1427 tf_none, NULL_TREE);
1428 if (friend_type == error_mark_node)
1429 return false;
1430
1431 /* Check if return types match. */
1432 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1433 return false;
1434
1435 /* Check if function parameter types match, ignoring the
1436 `this' parameter. */
1437 friend_args_type = TYPE_ARG_TYPES (friend_type);
1438 decl_args_type = TYPE_ARG_TYPES (decl_type);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1440 friend_args_type = TREE_CHAIN (friend_args_type);
1441 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1442 decl_args_type = TREE_CHAIN (decl_args_type);
1443
1444 return compparms (decl_args_type, friend_args_type);
1445 }
1446 else
1447 {
1448 /* DECL is a TYPE_DECL */
1449 bool is_template;
1450 tree decl_type = TREE_TYPE (decl);
1451
1452 /* Make sure that both DECL and FRIEND_DECL are templates or
1453 non-templates. */
1454 is_template
1455 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1456 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1457
1458 if (need_template ^ is_template)
1459 return false;
1460 else if (is_template)
1461 {
1462 tree friend_parms;
1463 /* If both are templates, check the name of the two
1464 TEMPLATE_DECL's first because is_friend didn't. */
1465 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1466 != DECL_NAME (friend_decl))
1467 return false;
1468
1469 /* Now check template parameter list. */
1470 friend_parms
1471 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1472 args, tf_none);
1473 return comp_template_parms
1474 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1475 friend_parms);
1476 }
1477 else
1478 return (DECL_NAME (decl)
1479 == DECL_NAME (friend_decl));
1480 }
1481 }
1482 return false;
1483 }
1484
1485 /* Register the specialization SPEC as a specialization of TMPL with
1486 the indicated ARGS. IS_FRIEND indicates whether the specialization
1487 is actually just a friend declaration. ATTRLIST is the list of
1488 attributes that the specialization is declared with or NULL when
1489 it isn't. Returns SPEC, or an equivalent prior declaration, if
1490 available.
1491
1492 We also store instantiations of field packs in the hash table, even
1493 though they are not themselves templates, to make lookup easier. */
1494
1495 static tree
1496 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1497 hashval_t hash)
1498 {
1499 tree fn;
1500
1501 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1502 || (TREE_CODE (tmpl) == FIELD_DECL
1503 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1504
1505 if (TREE_CODE (spec) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1513
1514 template <class T> void f(T t1, T t = T())
1515
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1518 return spec;
1519
1520 spec_entry elt;
1521 elt.tmpl = tmpl;
1522 elt.args = args;
1523 elt.spec = spec;
1524
1525 if (hash == 0)
1526 hash = spec_hasher::hash (&elt);
1527
1528 spec_entry **slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1529 if (*slot)
1530 fn = (*slot)->spec;
1531 else
1532 fn = NULL_TREE;
1533
1534 /* We can sometimes try to re-register a specialization that we've
1535 already got. In particular, regenerate_decl_from_template calls
1536 duplicate_decls which will update the specialization list. But,
1537 we'll still get called again here anyhow. It's more convenient
1538 to simply allow this than to try to prevent it. */
1539 if (fn == spec)
1540 return spec;
1541 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1542 {
1543 if (DECL_TEMPLATE_INSTANTIATION (fn))
1544 {
1545 if (DECL_ODR_USED (fn)
1546 || DECL_EXPLICIT_INSTANTIATION (fn))
1547 {
1548 error ("specialization of %qD after instantiation",
1549 fn);
1550 return error_mark_node;
1551 }
1552 else
1553 {
1554 tree clone;
1555 /* This situation should occur only if the first
1556 specialization is an implicit instantiation, the
1557 second is an explicit specialization, and the
1558 implicit instantiation has not yet been used. That
1559 situation can occur if we have implicitly
1560 instantiated a member function and then specialized
1561 it later.
1562
1563 We can also wind up here if a friend declaration that
1564 looked like an instantiation turns out to be a
1565 specialization:
1566
1567 template <class T> void foo(T);
1568 class S { friend void foo<>(int) };
1569 template <> void foo(int);
1570
1571 We transform the existing DECL in place so that any
1572 pointers to it become pointers to the updated
1573 declaration.
1574
1575 If there was a definition for the template, but not
1576 for the specialization, we want this to look as if
1577 there were no definition, and vice versa. */
1578 DECL_INITIAL (fn) = NULL_TREE;
1579 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1580
1581 /* The call to duplicate_decls will have applied
1582 [temp.expl.spec]:
1583
1584 An explicit specialization of a function template
1585 is inline only if it is explicitly declared to be,
1586 and independently of whether its function template
1587 is.
1588
1589 to the primary function; now copy the inline bits to
1590 the various clones. */
1591 FOR_EACH_CLONE (clone, fn)
1592 {
1593 DECL_DECLARED_INLINE_P (clone)
1594 = DECL_DECLARED_INLINE_P (fn);
1595 DECL_SOURCE_LOCATION (clone)
1596 = DECL_SOURCE_LOCATION (fn);
1597 DECL_DELETED_FN (clone)
1598 = DECL_DELETED_FN (fn);
1599 }
1600 check_specialization_namespace (tmpl);
1601
1602 return fn;
1603 }
1604 }
1605 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1606 {
1607 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1608 if (dd == error_mark_node)
1609 /* We've already complained in duplicate_decls. */
1610 return error_mark_node;
1611
1612 if (dd == NULL_TREE && DECL_INITIAL (spec))
1613 /* Dup decl failed, but this is a new definition. Set the
1614 line number so any errors match this new
1615 definition. */
1616 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1617
1618 return fn;
1619 }
1620 }
1621 else if (fn)
1622 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1623
1624 /* A specialization must be declared in the same namespace as the
1625 template it is specializing. */
1626 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1627 && !check_specialization_namespace (tmpl))
1628 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1629
1630 spec_entry *entry = ggc_alloc<spec_entry> ();
1631 gcc_assert (tmpl && args && spec);
1632 *entry = elt;
1633 *slot = entry;
1634 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1635 && PRIMARY_TEMPLATE_P (tmpl)
1636 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1637 || variable_template_p (tmpl))
1638 /* If TMPL is a forward declaration of a template function, keep a list
1639 of all specializations in case we need to reassign them to a friend
1640 template later in tsubst_friend_function.
1641
1642 Also keep a list of all variable template instantiations so that
1643 process_partial_specialization can check whether a later partial
1644 specialization would have used it. */
1645 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1646 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1647
1648 return spec;
1649 }
1650
1651 /* Restricts tree and type comparisons. */
1652 int comparing_specializations;
1653 int comparing_dependent_aliases;
1654
1655 /* Returns true iff two spec_entry nodes are equivalent. */
1656
1657 bool
1658 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1659 {
1660 int equal;
1661
1662 ++comparing_specializations;
1663 ++comparing_dependent_aliases;
1664 ++processing_template_decl;
1665 equal = (e1->tmpl == e2->tmpl
1666 && comp_template_args (e1->args, e2->args));
1667 if (equal && flag_concepts
1668 /* tmpl could be a FIELD_DECL for a capture pack. */
1669 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1670 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1671 && uses_template_parms (e1->args))
1672 {
1673 /* Partial specializations of a variable template can be distinguished by
1674 constraints. */
1675 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1676 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1677 equal = equivalent_constraints (c1, c2);
1678 }
1679 --processing_template_decl;
1680 --comparing_dependent_aliases;
1681 --comparing_specializations;
1682
1683 return equal;
1684 }
1685
1686 /* Returns a hash for a template TMPL and template arguments ARGS. */
1687
1688 static hashval_t
1689 hash_tmpl_and_args (tree tmpl, tree args)
1690 {
1691 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1692 return iterative_hash_template_arg (args, val);
1693 }
1694
1695 hashval_t
1696 spec_hasher::hash (tree tmpl, tree args)
1697 {
1698 ++comparing_specializations;
1699 hashval_t val = hash_tmpl_and_args (tmpl, args);
1700 --comparing_specializations;
1701 return val;
1702 }
1703
1704 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1705 ignoring SPEC. */
1706
1707 hashval_t
1708 spec_hasher::hash (spec_entry *e)
1709 {
1710 return spec_hasher::hash (e->tmpl, e->args);
1711 }
1712
1713 /* Recursively calculate a hash value for a template argument ARG, for use
1714 in the hash tables of template specializations. We must be
1715 careful to (at least) skip the same entities template_args_equal
1716 does. */
1717
1718 hashval_t
1719 iterative_hash_template_arg (tree arg, hashval_t val)
1720 {
1721 if (arg == NULL_TREE)
1722 return iterative_hash_object (arg, val);
1723
1724 if (!TYPE_P (arg))
1725 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1726 while (CONVERT_EXPR_P (arg)
1727 || TREE_CODE (arg) == NON_LVALUE_EXPR
1728 || class_nttp_const_wrapper_p (arg))
1729 arg = TREE_OPERAND (arg, 0);
1730
1731 enum tree_code code = TREE_CODE (arg);
1732
1733 val = iterative_hash_object (code, val);
1734
1735 switch (code)
1736 {
1737 case ARGUMENT_PACK_SELECT:
1738 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
1739 preserving it in a hash table, which is bad because it will change
1740 meaning when gen_elem_of_pack_expansion_instantiation changes the
1741 ARGUMENT_PACK_SELECT_INDEX. */
1742 gcc_unreachable ();
1743
1744 case ERROR_MARK:
1745 return val;
1746
1747 case IDENTIFIER_NODE:
1748 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1749
1750 case TREE_VEC:
1751 for (tree elt : tree_vec_range (arg))
1752 val = iterative_hash_template_arg (elt, val);
1753 return val;
1754
1755 case TYPE_PACK_EXPANSION:
1756 case EXPR_PACK_EXPANSION:
1757 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1758 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1759
1760 case TYPE_ARGUMENT_PACK:
1761 case NONTYPE_ARGUMENT_PACK:
1762 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1763
1764 case TREE_LIST:
1765 for (; arg; arg = TREE_CHAIN (arg))
1766 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1767 return val;
1768
1769 case OVERLOAD:
1770 for (lkp_iterator iter (arg); iter; ++iter)
1771 val = iterative_hash_template_arg (*iter, val);
1772 return val;
1773
1774 case CONSTRUCTOR:
1775 {
1776 iterative_hash_template_arg (TREE_TYPE (arg), val);
1777 for (auto &e: CONSTRUCTOR_ELTS (arg))
1778 {
1779 val = iterative_hash_template_arg (e.index, val);
1780 val = iterative_hash_template_arg (e.value, val);
1781 }
1782 return val;
1783 }
1784
1785 case PARM_DECL:
1786 if (!DECL_ARTIFICIAL (arg))
1787 {
1788 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1789 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1790 }
1791 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1792
1793 case TARGET_EXPR:
1794 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1795
1796 case PTRMEM_CST:
1797 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1798 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1799
1800 case TEMPLATE_PARM_INDEX:
1801 val = iterative_hash_template_arg
1802 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1803 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1804 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1805
1806 case TRAIT_EXPR:
1807 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1808 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1809 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1810
1811 case BASELINK:
1812 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1813 val);
1814 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1815 val);
1816
1817 case MODOP_EXPR:
1818 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1819 code = TREE_CODE (TREE_OPERAND (arg, 1));
1820 val = iterative_hash_object (code, val);
1821 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1822
1823 case LAMBDA_EXPR:
1824 /* [temp.over.link] Two lambda-expressions are never considered
1825 equivalent.
1826
1827 So just hash the closure type. */
1828 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1829
1830 case CAST_EXPR:
1831 case IMPLICIT_CONV_EXPR:
1832 case STATIC_CAST_EXPR:
1833 case REINTERPRET_CAST_EXPR:
1834 case CONST_CAST_EXPR:
1835 case DYNAMIC_CAST_EXPR:
1836 case NEW_EXPR:
1837 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1838 /* Now hash operands as usual. */
1839 break;
1840
1841 case CALL_EXPR:
1842 {
1843 tree fn = CALL_EXPR_FN (arg);
1844 if (tree name = dependent_name (fn))
1845 {
1846 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1847 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1848 fn = name;
1849 }
1850 val = iterative_hash_template_arg (fn, val);
1851 call_expr_arg_iterator ai;
1852 for (tree x = first_call_expr_arg (arg, &ai); x;
1853 x = next_call_expr_arg (&ai))
1854 val = iterative_hash_template_arg (x, val);
1855 return val;
1856 }
1857
1858 default:
1859 break;
1860 }
1861
1862 char tclass = TREE_CODE_CLASS (code);
1863 switch (tclass)
1864 {
1865 case tcc_type:
1866 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1867 {
1868 // We want an alias specialization that survived strip_typedefs
1869 // to hash differently from its TYPE_CANONICAL, to avoid hash
1870 // collisions that compare as different in template_args_equal.
1871 // These could be dependent specializations that strip_typedefs
1872 // left alone, or untouched specializations because
1873 // coerce_template_parms returns the unconverted template
1874 // arguments if it sees incomplete argument packs.
1875 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1876 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1877 }
1878
1879 switch (TREE_CODE (arg))
1880 {
1881 case TEMPLATE_TEMPLATE_PARM:
1882 {
1883 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1884
1885 /* Do not recurse with TPI directly, as that is unbounded
1886 recursion. */
1887 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1888 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1889 }
1890 break;
1891
1892 case DECLTYPE_TYPE:
1893 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1894 break;
1895
1896 case TYPENAME_TYPE:
1897 if (comparing_specializations)
1898 {
1899 /* Hash the components that are relevant to TYPENAME_TYPE
1900 equivalence as determined by structural_comptypes. We
1901 can only coherently do this when comparing_specializations
1902 is set, because otherwise structural_comptypes tries
1903 resolving TYPENAME_TYPE via the current instantiation. */
1904 tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
1905 tree fullname = TYPENAME_TYPE_FULLNAME (arg);
1906 val = iterative_hash_template_arg (context, val);
1907 val = iterative_hash_template_arg (fullname, val);
1908 }
1909 break;
1910
1911 default:
1912 if (tree canonical = TYPE_CANONICAL (arg))
1913 val = iterative_hash_object (TYPE_HASH (canonical), val);
1914 break;
1915 }
1916
1917 return val;
1918
1919 case tcc_declaration:
1920 case tcc_constant:
1921 return iterative_hash_expr (arg, val);
1922
1923 default:
1924 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1925 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1926 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1927 return val;
1928 }
1929 }
1930
1931 /* Unregister the specialization SPEC as a specialization of TMPL.
1932 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1933 if the SPEC was listed as a specialization of TMPL.
1934
1935 Note that SPEC has been ggc_freed, so we can't look inside it. */
1936
1937 bool
1938 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1939 {
1940 spec_entry *entry;
1941 spec_entry elt;
1942
1943 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1944 elt.args = TI_ARGS (tinfo);
1945 elt.spec = NULL_TREE;
1946
1947 entry = decl_specializations->find (&elt);
1948 if (entry != NULL)
1949 {
1950 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1951 gcc_assert (new_spec != NULL_TREE);
1952 entry->spec = new_spec;
1953 return 1;
1954 }
1955
1956 return 0;
1957 }
1958
1959 /* Like register_specialization, but for local declarations. We are
1960 registering SPEC, an instantiation of TMPL. */
1961
1962 void
1963 register_local_specialization (tree spec, tree tmpl)
1964 {
1965 gcc_assert (tmpl != spec);
1966 local_specializations->put (tmpl, spec);
1967 }
1968
1969 /* Registers T as a specialization of itself. This is used to preserve
1970 the references to already-parsed parameters when instantiating
1971 postconditions. */
1972
1973 void
1974 register_local_identity (tree t)
1975 {
1976 local_specializations->put (t, t);
1977 }
1978
1979 /* TYPE is a class type. Returns true if TYPE is an explicitly
1980 specialized class. */
1981
1982 bool
1983 explicit_class_specialization_p (tree type)
1984 {
1985 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1986 return false;
1987 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1988 }
1989
1990 /* Print the list of functions at FNS, going through all the overloads
1991 for each element of the list. Alternatively, FNS cannot be a
1992 TREE_LIST, in which case it will be printed together with all the
1993 overloads.
1994
1995 MORE and *STR should respectively be FALSE and NULL when the function
1996 is called from the outside. They are used internally on recursive
1997 calls. print_candidates manages the two parameters and leaves NULL
1998 in *STR when it ends. */
1999
2000 static void
2001 print_candidates_1 (tree fns, char **str, bool more = false)
2002 {
2003 if (TREE_CODE (fns) == TREE_LIST)
2004 for (; fns; fns = TREE_CHAIN (fns))
2005 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2006 else
2007 for (lkp_iterator iter (fns); iter;)
2008 {
2009 tree cand = *iter;
2010 ++iter;
2011
2012 const char *pfx = *str;
2013 if (!pfx)
2014 {
2015 if (more || iter)
2016 pfx = _("candidates are:");
2017 else
2018 pfx = _("candidate is:");
2019 *str = get_spaces (pfx);
2020 }
2021 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2022 }
2023 }
2024
2025 /* Print the list of candidate FNS in an error message. FNS can also
2026 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2027
2028 void
2029 print_candidates (tree fns)
2030 {
2031 char *str = NULL;
2032 print_candidates_1 (fns, &str);
2033 free (str);
2034 }
2035
2036 /* Get a (possibly) constrained template declaration for the
2037 purpose of ordering candidates. */
2038 static tree
2039 get_template_for_ordering (tree list)
2040 {
2041 gcc_assert (TREE_CODE (list) == TREE_LIST);
2042 tree f = TREE_VALUE (list);
2043 if (tree ti = DECL_TEMPLATE_INFO (f))
2044 return TI_TEMPLATE (ti);
2045 return f;
2046 }
2047
2048 /* Among candidates having the same signature, return the
2049 most constrained or NULL_TREE if there is no best candidate.
2050 If the signatures of candidates vary (e.g., template
2051 specialization vs. member function), then there can be no
2052 most constrained.
2053
2054 Note that we don't compare constraints on the functions
2055 themselves, but rather those of their templates. */
2056 static tree
2057 most_constrained_function (tree candidates)
2058 {
2059 // Try to find the best candidate in a first pass.
2060 tree champ = candidates;
2061 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2062 {
2063 int winner = more_constrained (get_template_for_ordering (champ),
2064 get_template_for_ordering (c));
2065 if (winner == -1)
2066 champ = c; // The candidate is more constrained
2067 else if (winner == 0)
2068 return NULL_TREE; // Neither is more constrained
2069 }
2070
2071 // Verify that the champ is better than previous candidates.
2072 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2073 if (!more_constrained (get_template_for_ordering (champ),
2074 get_template_for_ordering (c)))
2075 return NULL_TREE;
2076 }
2077
2078 return champ;
2079 }
2080
2081
2082 /* Returns the template (one of the functions given by TEMPLATE_ID)
2083 which can be specialized to match the indicated DECL with the
2084 explicit template args given in TEMPLATE_ID. The DECL may be
2085 NULL_TREE if none is available. In that case, the functions in
2086 TEMPLATE_ID are non-members.
2087
2088 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2089 specialization of a member template.
2090
2091 The TEMPLATE_COUNT is the number of references to qualifying
2092 template classes that appeared in the name of the function. See
2093 check_explicit_specialization for a more accurate description.
2094
2095 TSK indicates what kind of template declaration (if any) is being
2096 declared. TSK_TEMPLATE indicates that the declaration given by
2097 DECL, though a FUNCTION_DECL, has template parameters, and is
2098 therefore a template function.
2099
2100 The template args (those explicitly specified and those deduced)
2101 are output in a newly created vector *TARGS_OUT.
2102
2103 If it is impossible to determine the result, an error message is
2104 issued. The error_mark_node is returned to indicate failure. */
2105
2106 static tree
2107 determine_specialization (tree template_id,
2108 tree decl,
2109 tree* targs_out,
2110 int need_member_template,
2111 int template_count,
2112 tmpl_spec_kind tsk)
2113 {
2114 tree fns;
2115 tree targs;
2116 tree explicit_targs;
2117 tree candidates = NULL_TREE;
2118
2119 /* A TREE_LIST of templates of which DECL may be a specialization.
2120 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2121 corresponding TREE_PURPOSE is the set of template arguments that,
2122 when used to instantiate the template, would produce a function
2123 with the signature of DECL. */
2124 tree templates = NULL_TREE;
2125 int header_count;
2126 cp_binding_level *b;
2127
2128 *targs_out = NULL_TREE;
2129
2130 if (template_id == error_mark_node || decl == error_mark_node)
2131 return error_mark_node;
2132
2133 /* We shouldn't be specializing a member template of an
2134 unspecialized class template; we already gave an error in
2135 check_specialization_scope, now avoid crashing. */
2136 if (!VAR_P (decl)
2137 && template_count && DECL_CLASS_SCOPE_P (decl)
2138 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2139 {
2140 gcc_assert (errorcount);
2141 return error_mark_node;
2142 }
2143
2144 fns = TREE_OPERAND (template_id, 0);
2145 explicit_targs = TREE_OPERAND (template_id, 1);
2146
2147 if (fns == error_mark_node)
2148 return error_mark_node;
2149
2150 /* Check for baselinks. */
2151 if (BASELINK_P (fns))
2152 fns = BASELINK_FUNCTIONS (fns);
2153
2154 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2155 {
2156 error_at (DECL_SOURCE_LOCATION (decl),
2157 "%qD is not a function template", fns);
2158 return error_mark_node;
2159 }
2160 else if (VAR_P (decl) && !variable_template_p (fns))
2161 {
2162 error ("%qD is not a variable template", fns);
2163 return error_mark_node;
2164 }
2165
2166 /* Count the number of template headers specified for this
2167 specialization. */
2168 header_count = 0;
2169 for (b = current_binding_level;
2170 b->kind == sk_template_parms;
2171 b = b->level_chain)
2172 ++header_count;
2173
2174 tree orig_fns = fns;
2175 bool header_mismatch = false;
2176
2177 if (variable_template_p (fns))
2178 {
2179 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2180 targs = coerce_template_parms (parms, explicit_targs, fns,
2181 tf_warning_or_error);
2182 if (targs != error_mark_node
2183 && constraints_satisfied_p (fns, targs))
2184 templates = tree_cons (targs, fns, templates);
2185 }
2186 else for (lkp_iterator iter (fns); iter; ++iter)
2187 {
2188 tree fn = *iter;
2189
2190 if (TREE_CODE (fn) == TEMPLATE_DECL)
2191 {
2192 tree decl_arg_types;
2193 tree fn_arg_types;
2194
2195 /* In case of explicit specialization, we need to check if
2196 the number of template headers appearing in the specialization
2197 is correct. This is usually done in check_explicit_specialization,
2198 but the check done there cannot be exhaustive when specializing
2199 member functions. Consider the following code:
2200
2201 template <> void A<int>::f(int);
2202 template <> template <> void A<int>::f(int);
2203
2204 Assuming that A<int> is not itself an explicit specialization
2205 already, the first line specializes "f" which is a non-template
2206 member function, whilst the second line specializes "f" which
2207 is a template member function. So both lines are syntactically
2208 correct, and check_explicit_specialization does not reject
2209 them.
2210
2211 Here, we can do better, as we are matching the specialization
2212 against the declarations. We count the number of template
2213 headers, and we check if they match TEMPLATE_COUNT + 1
2214 (TEMPLATE_COUNT is the number of qualifying template classes,
2215 plus there must be another header for the member template
2216 itself).
2217
2218 Notice that if header_count is zero, this is not a
2219 specialization but rather a template instantiation, so there
2220 is no check we can perform here. */
2221 if (header_count && header_count != template_count + 1)
2222 {
2223 header_mismatch = true;
2224 continue;
2225 }
2226
2227 /* Check that the number of template arguments at the
2228 innermost level for DECL is the same as for FN. */
2229 if (current_binding_level->kind == sk_template_parms
2230 && !current_binding_level->explicit_spec_p
2231 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2232 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2233 (current_template_parms))))
2234 continue;
2235
2236 /* DECL might be a specialization of FN. */
2237 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2238 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2239
2240 /* For a non-static member function, we need to make sure
2241 that the const qualification is the same. Since
2242 get_bindings does not try to merge the "this" parameter,
2243 we must do the comparison explicitly. */
2244 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2245 {
2246 if (!same_type_p (TREE_VALUE (fn_arg_types),
2247 TREE_VALUE (decl_arg_types)))
2248 continue;
2249
2250 /* And the ref-qualification. */
2251 if (type_memfn_rqual (TREE_TYPE (decl))
2252 != type_memfn_rqual (TREE_TYPE (fn)))
2253 continue;
2254 }
2255
2256 /* Skip the "this" parameter and, for constructors of
2257 classes with virtual bases, the VTT parameter. A
2258 full specialization of a constructor will have a VTT
2259 parameter, but a template never will. */
2260 decl_arg_types
2261 = skip_artificial_parms_for (decl, decl_arg_types);
2262 fn_arg_types
2263 = skip_artificial_parms_for (fn, fn_arg_types);
2264
2265 /* Function templates cannot be specializations; there are
2266 no partial specializations of functions. Therefore, if
2267 the type of DECL does not match FN, there is no
2268 match.
2269
2270 Note that it should never be the case that we have both
2271 candidates added here, and for regular member functions
2272 below. */
2273 if (tsk == tsk_template)
2274 {
2275 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2276 current_template_parms))
2277 continue;
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2279 TREE_TYPE (TREE_TYPE (fn))))
2280 continue;
2281 if (!compparms (fn_arg_types, decl_arg_types))
2282 continue;
2283
2284 tree freq = get_constraints (fn);
2285 tree dreq = get_constraints (decl);
2286 if (!freq != !dreq)
2287 continue;
2288 if (freq)
2289 {
2290 /* C++20 CA104: Substitute directly into the
2291 constraint-expression. */
2292 tree fargs = DECL_TI_ARGS (fn);
2293 tsubst_flags_t complain = tf_none;
2294 freq = tsubst_constraint_info (freq, fargs, complain, fn);
2295 if (!cp_tree_equal (freq, dreq))
2296 continue;
2297 }
2298
2299 candidates = tree_cons (NULL_TREE, fn, candidates);
2300 continue;
2301 }
2302
2303 /* See whether this function might be a specialization of this
2304 template. Suppress access control because we might be trying
2305 to make this specialization a friend, and we have already done
2306 access control for the declaration of the specialization. */
2307 push_deferring_access_checks (dk_no_check);
2308 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2309 pop_deferring_access_checks ();
2310
2311 if (!targs)
2312 /* We cannot deduce template arguments that when used to
2313 specialize TMPL will produce DECL. */
2314 continue;
2315
2316 if (uses_template_parms (targs))
2317 /* We deduced something involving 'auto', which isn't a valid
2318 template argument. */
2319 continue;
2320
2321 /* Save this template, and the arguments deduced. */
2322 templates = tree_cons (targs, fn, templates);
2323 }
2324 else if (need_member_template)
2325 /* FN is an ordinary member function, and we need a
2326 specialization of a member template. */
2327 ;
2328 else if (TREE_CODE (fn) != FUNCTION_DECL)
2329 /* We can get IDENTIFIER_NODEs here in certain erroneous
2330 cases. */
2331 ;
2332 else if (!DECL_FUNCTION_MEMBER_P (fn))
2333 /* This is just an ordinary non-member function. Nothing can
2334 be a specialization of that. */
2335 ;
2336 else if (DECL_ARTIFICIAL (fn))
2337 /* Cannot specialize functions that are created implicitly. */
2338 ;
2339 else
2340 {
2341 tree decl_arg_types;
2342
2343 /* This is an ordinary member function. However, since
2344 we're here, we can assume its enclosing class is a
2345 template class. For example,
2346
2347 template <typename T> struct S { void f(); };
2348 template <> void S<int>::f() {}
2349
2350 Here, S<int>::f is a non-template, but S<int> is a
2351 template class. If FN has the same type as DECL, we
2352 might be in business. */
2353
2354 if (!DECL_TEMPLATE_INFO (fn))
2355 /* Its enclosing class is an explicit specialization
2356 of a template class. This is not a candidate. */
2357 continue;
2358
2359 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2360 TREE_TYPE (TREE_TYPE (fn))))
2361 /* The return types differ. */
2362 continue;
2363
2364 /* Adjust the type of DECL in case FN is a static member. */
2365 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2366 if (DECL_STATIC_FUNCTION_P (fn)
2367 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2368 decl_arg_types = TREE_CHAIN (decl_arg_types);
2369
2370 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2371 decl_arg_types))
2372 continue;
2373
2374 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2375 && (type_memfn_rqual (TREE_TYPE (decl))
2376 != type_memfn_rqual (TREE_TYPE (fn))))
2377 continue;
2378
2379 // If the deduced arguments do not satisfy the constraints,
2380 // this is not a candidate.
2381 if (flag_concepts && !constraints_satisfied_p (fn))
2382 continue;
2383
2384 // Add the candidate.
2385 candidates = tree_cons (NULL_TREE, fn, candidates);
2386 }
2387 }
2388
2389 if (templates && TREE_CHAIN (templates))
2390 {
2391 /* We have:
2392
2393 [temp.expl.spec]
2394
2395 It is possible for a specialization with a given function
2396 signature to be instantiated from more than one function
2397 template. In such cases, explicit specification of the
2398 template arguments must be used to uniquely identify the
2399 function template specialization being specialized.
2400
2401 Note that here, there's no suggestion that we're supposed to
2402 determine which of the candidate templates is most
2403 specialized. However, we, also have:
2404
2405 [temp.func.order]
2406
2407 Partial ordering of overloaded function template
2408 declarations is used in the following contexts to select
2409 the function template to which a function template
2410 specialization refers:
2411
2412 -- when an explicit specialization refers to a function
2413 template.
2414
2415 So, we do use the partial ordering rules, at least for now.
2416 This extension can only serve to make invalid programs valid,
2417 so it's safe. And, there is strong anecdotal evidence that
2418 the committee intended the partial ordering rules to apply;
2419 the EDG front end has that behavior, and John Spicer claims
2420 that the committee simply forgot to delete the wording in
2421 [temp.expl.spec]. */
2422 tree tmpl = most_specialized_instantiation (templates);
2423 if (tmpl != error_mark_node)
2424 {
2425 templates = tmpl;
2426 TREE_CHAIN (templates) = NULL_TREE;
2427 }
2428 }
2429
2430 // Concepts allows multiple declarations of member functions
2431 // with the same signature. Like above, we need to rely on
2432 // on the partial ordering of those candidates to determine which
2433 // is the best.
2434 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2435 {
2436 if (tree cand = most_constrained_function (candidates))
2437 {
2438 candidates = cand;
2439 TREE_CHAIN (cand) = NULL_TREE;
2440 }
2441 }
2442
2443 if (templates == NULL_TREE && candidates == NULL_TREE)
2444 {
2445 error ("template-id %qD for %q+D does not match any template "
2446 "declaration", template_id, decl);
2447 if (header_mismatch)
2448 inform (DECL_SOURCE_LOCATION (decl),
2449 "saw %d %<template<>%>, need %d for "
2450 "specializing a member function template",
2451 header_count, template_count + 1);
2452 print_candidates (orig_fns);
2453 return error_mark_node;
2454 }
2455 else if ((templates && TREE_CHAIN (templates))
2456 || (candidates && TREE_CHAIN (candidates))
2457 || (templates && candidates))
2458 {
2459 error ("ambiguous template specialization %qD for %q+D",
2460 template_id, decl);
2461 candidates = chainon (candidates, templates);
2462 print_candidates (candidates);
2463 return error_mark_node;
2464 }
2465
2466 /* We have one, and exactly one, match. */
2467 if (candidates)
2468 {
2469 tree fn = TREE_VALUE (candidates);
2470 *targs_out = copy_node (DECL_TI_ARGS (fn));
2471
2472 /* Propagate the candidate's constraints to the declaration. */
2473 if (tsk != tsk_template)
2474 set_constraints (decl, get_constraints (fn));
2475
2476 /* DECL is a re-declaration or partial instantiation of a template
2477 function. */
2478 if (TREE_CODE (fn) == TEMPLATE_DECL)
2479 return fn;
2480 /* It was a specialization of an ordinary member function in a
2481 template class. */
2482 return DECL_TI_TEMPLATE (fn);
2483 }
2484
2485 /* It was a specialization of a template. */
2486 tree tmpl = TREE_VALUE (templates);
2487 *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
2488
2489 /* Propagate the template's constraints to the declaration. */
2490 if (tsk != tsk_template)
2491 set_constraints (decl, get_constraints (tmpl));
2492
2493 return tmpl;
2494 }
2495
2496 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2497 but with the default argument values filled in from those in the
2498 TMPL_TYPES. */
2499
2500 static tree
2501 copy_default_args_to_explicit_spec_1 (tree spec_types,
2502 tree tmpl_types)
2503 {
2504 tree new_spec_types;
2505
2506 if (!spec_types)
2507 return NULL_TREE;
2508
2509 if (spec_types == void_list_node)
2510 return void_list_node;
2511
2512 /* Substitute into the rest of the list. */
2513 new_spec_types =
2514 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2515 TREE_CHAIN (tmpl_types));
2516
2517 /* Add the default argument for this parameter. */
2518 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2519 TREE_VALUE (spec_types),
2520 new_spec_types);
2521 }
2522
2523 /* DECL is an explicit specialization. Replicate default arguments
2524 from the template it specializes. (That way, code like:
2525
2526 template <class T> void f(T = 3);
2527 template <> void f(double);
2528 void g () { f (); }
2529
2530 works, as required.) An alternative approach would be to look up
2531 the correct default arguments at the call-site, but this approach
2532 is consistent with how implicit instantiations are handled. */
2533
2534 static void
2535 copy_default_args_to_explicit_spec (tree decl)
2536 {
2537 tree tmpl;
2538 tree spec_types;
2539 tree tmpl_types;
2540 tree new_spec_types;
2541 tree old_type;
2542 tree new_type;
2543 tree t;
2544 tree object_type = NULL_TREE;
2545 tree in_charge = NULL_TREE;
2546 tree vtt = NULL_TREE;
2547
2548 /* See if there's anything we need to do. */
2549 tmpl = DECL_TI_TEMPLATE (decl);
2550 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2551 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2552 if (TREE_PURPOSE (t))
2553 break;
2554 if (!t)
2555 return;
2556
2557 old_type = TREE_TYPE (decl);
2558 spec_types = TYPE_ARG_TYPES (old_type);
2559
2560 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2561 {
2562 /* Remove the this pointer, but remember the object's type for
2563 CV quals. */
2564 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2565 spec_types = TREE_CHAIN (spec_types);
2566 tmpl_types = TREE_CHAIN (tmpl_types);
2567
2568 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2569 {
2570 /* DECL may contain more parameters than TMPL due to the extra
2571 in-charge parameter in constructors and destructors. */
2572 in_charge = spec_types;
2573 spec_types = TREE_CHAIN (spec_types);
2574 }
2575 if (DECL_HAS_VTT_PARM_P (decl))
2576 {
2577 vtt = spec_types;
2578 spec_types = TREE_CHAIN (spec_types);
2579 }
2580 }
2581
2582 /* Compute the merged default arguments. */
2583 new_spec_types =
2584 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2585
2586 /* Compute the new FUNCTION_TYPE. */
2587 if (object_type)
2588 {
2589 if (vtt)
2590 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2591 TREE_VALUE (vtt),
2592 new_spec_types);
2593
2594 if (in_charge)
2595 /* Put the in-charge parameter back. */
2596 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2597 TREE_VALUE (in_charge),
2598 new_spec_types);
2599
2600 new_type = build_method_type_directly (object_type,
2601 TREE_TYPE (old_type),
2602 new_spec_types);
2603 }
2604 else
2605 new_type = build_function_type (TREE_TYPE (old_type),
2606 new_spec_types);
2607 new_type = cp_build_type_attribute_variant (new_type,
2608 TYPE_ATTRIBUTES (old_type));
2609 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2610
2611 TREE_TYPE (decl) = new_type;
2612 }
2613
2614 /* Return the number of template headers we expect to see for a definition
2615 or specialization of CTYPE or one of its non-template members. */
2616
2617 int
2618 num_template_headers_for_class (tree ctype)
2619 {
2620 int num_templates = 0;
2621
2622 while (ctype && CLASS_TYPE_P (ctype))
2623 {
2624 /* You're supposed to have one `template <...>' for every
2625 template class, but you don't need one for a full
2626 specialization. For example:
2627
2628 template <class T> struct S{};
2629 template <> struct S<int> { void f(); };
2630 void S<int>::f () {}
2631
2632 is correct; there shouldn't be a `template <>' for the
2633 definition of `S<int>::f'. */
2634 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2635 /* If CTYPE does not have template information of any
2636 kind, then it is not a template, nor is it nested
2637 within a template. */
2638 break;
2639 if (explicit_class_specialization_p (ctype))
2640 break;
2641 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2642 ++num_templates;
2643
2644 ctype = TYPE_CONTEXT (ctype);
2645 }
2646
2647 return num_templates;
2648 }
2649
2650 /* Do a simple sanity check on the template headers that precede the
2651 variable declaration DECL. */
2652
2653 void
2654 check_template_variable (tree decl)
2655 {
2656 tree ctx = CP_DECL_CONTEXT (decl);
2657 int wanted = num_template_headers_for_class (ctx);
2658 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2659 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2660 {
2661 if (cxx_dialect < cxx14)
2662 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2663 "variable templates only available with "
2664 "%<-std=c++14%> or %<-std=gnu++14%>");
2665
2666 // Namespace-scope variable templates should have a template header.
2667 ++wanted;
2668 }
2669 if (template_header_count > wanted)
2670 {
2671 auto_diagnostic_group d;
2672 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2673 "too many template headers for %qD "
2674 "(should be %d)",
2675 decl, wanted);
2676 if (warned && CLASS_TYPE_P (ctx)
2677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2678 inform (DECL_SOURCE_LOCATION (decl),
2679 "members of an explicitly specialized class are defined "
2680 "without a template header");
2681 }
2682 }
2683
2684 /* An explicit specialization whose declarator-id or class-head-name is not
2685 qualified shall be declared in the nearest enclosing namespace of the
2686 template, or, if the namespace is inline (7.3.1), any namespace from its
2687 enclosing namespace set.
2688
2689 If the name declared in the explicit instantiation is an unqualified name,
2690 the explicit instantiation shall appear in the namespace where its template
2691 is declared or, if that namespace is inline (7.3.1), any namespace from its
2692 enclosing namespace set. */
2693
2694 void
2695 check_unqualified_spec_or_inst (tree t, location_t loc)
2696 {
2697 tree tmpl = most_general_template (t);
2698 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2699 && !is_nested_namespace (current_namespace,
2700 CP_DECL_CONTEXT (tmpl), true))
2701 {
2702 if (processing_specialization)
2703 permerror (loc, "explicit specialization of %qD outside its "
2704 "namespace must use a nested-name-specifier", tmpl);
2705 else if (processing_explicit_instantiation
2706 && cxx_dialect >= cxx11)
2707 /* This was allowed in C++98, so only pedwarn. */
2708 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2709 "outside its namespace must use a nested-name-"
2710 "specifier", tmpl);
2711 }
2712 }
2713
2714 /* Warn for a template specialization SPEC that is missing some of a set
2715 of function or type attributes that the template TEMPL is declared with.
2716 ATTRLIST is a list of additional attributes that SPEC should be taken
2717 to ultimately be declared with. */
2718
2719 static void
2720 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2721 {
2722 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2723 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2724
2725 /* Avoid warning if the difference between the primary and
2726 the specialization is not in one of the attributes below. */
2727 const char* const blacklist[] = {
2728 "alloc_align", "alloc_size", "assume_aligned", "format",
2729 "format_arg", "malloc", "nonnull", NULL
2730 };
2731
2732 /* Put together a list of the black listed attributes that the primary
2733 template is declared with that the specialization is not, in case
2734 it's not apparent from the most recent declaration of the primary. */
2735 pretty_printer str;
2736 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2737 blacklist, &str);
2738
2739 if (!nattrs)
2740 return;
2741
2742 auto_diagnostic_group d;
2743 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2744 "explicit specialization %q#D may be missing attributes",
2745 spec))
2746 inform (DECL_SOURCE_LOCATION (tmpl),
2747 nattrs > 1
2748 ? G_("missing primary template attributes %s")
2749 : G_("missing primary template attribute %s"),
2750 pp_formatted_text (&str));
2751 }
2752
2753 /* Check to see if the function just declared, as indicated in
2754 DECLARATOR, and in DECL, is a specialization of a function
2755 template. We may also discover that the declaration is an explicit
2756 instantiation at this point.
2757
2758 Returns DECL, or an equivalent declaration that should be used
2759 instead if all goes well. Issues an error message if something is
2760 amiss. Returns error_mark_node if the error is not easily
2761 recoverable.
2762
2763 FLAGS is a bitmask consisting of the following flags:
2764
2765 2: The function has a definition.
2766 4: The function is a friend.
2767
2768 The TEMPLATE_COUNT is the number of references to qualifying
2769 template classes that appeared in the name of the function. For
2770 example, in
2771
2772 template <class T> struct S { void f(); };
2773 void S<int>::f();
2774
2775 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2776 classes are not counted in the TEMPLATE_COUNT, so that in
2777
2778 template <class T> struct S {};
2779 template <> struct S<int> { void f(); }
2780 template <> void S<int>::f();
2781
2782 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2783 invalid; there should be no template <>.)
2784
2785 If the function is a specialization, it is marked as such via
2786 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2787 is set up correctly, and it is added to the list of specializations
2788 for that template. */
2789
2790 tree
2791 check_explicit_specialization (tree declarator,
2792 tree decl,
2793 int template_count,
2794 int flags,
2795 tree attrlist)
2796 {
2797 int have_def = flags & 2;
2798 int is_friend = flags & 4;
2799 bool is_concept = flags & 8;
2800 int specialization = 0;
2801 int explicit_instantiation = 0;
2802 int member_specialization = 0;
2803 tree ctype = DECL_CLASS_CONTEXT (decl);
2804 tree dname = DECL_NAME (decl);
2805 tmpl_spec_kind tsk;
2806
2807 if (is_friend)
2808 {
2809 if (!processing_specialization)
2810 tsk = tsk_none;
2811 else
2812 tsk = tsk_excessive_parms;
2813 }
2814 else
2815 tsk = current_tmpl_spec_kind (template_count);
2816
2817 switch (tsk)
2818 {
2819 case tsk_none:
2820 if (processing_specialization && !VAR_P (decl))
2821 {
2822 specialization = 1;
2823 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2824 }
2825 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
2826 || (DECL_LANG_SPECIFIC (decl)
2827 && DECL_IMPLICIT_INSTANTIATION (decl)))
2828 {
2829 if (is_friend)
2830 /* This could be something like:
2831
2832 template <class T> void f(T);
2833 class S { friend void f<>(int); } */
2834 specialization = 1;
2835 else
2836 {
2837 /* This case handles bogus declarations like template <>
2838 template <class T> void f<int>(); */
2839
2840 error_at (cp_expr_loc_or_input_loc (declarator),
2841 "template-id %qE in declaration of primary template",
2842 declarator);
2843 return decl;
2844 }
2845 }
2846 break;
2847
2848 case tsk_invalid_member_spec:
2849 /* The error has already been reported in
2850 check_specialization_scope. */
2851 return error_mark_node;
2852
2853 case tsk_invalid_expl_inst:
2854 error ("template parameter list used in explicit instantiation");
2855
2856 /* Fall through. */
2857
2858 case tsk_expl_inst:
2859 if (have_def)
2860 error ("definition provided for explicit instantiation");
2861
2862 explicit_instantiation = 1;
2863 break;
2864
2865 case tsk_excessive_parms:
2866 case tsk_insufficient_parms:
2867 if (tsk == tsk_excessive_parms)
2868 error ("too many template parameter lists in declaration of %qD",
2869 decl);
2870 else if (template_header_count)
2871 error("too few template parameter lists in declaration of %qD", decl);
2872 else
2873 error("explicit specialization of %qD must be introduced by "
2874 "%<template <>%>", decl);
2875
2876 /* Fall through. */
2877 case tsk_expl_spec:
2878 if (is_concept)
2879 error ("explicit specialization declared %<concept%>");
2880
2881 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2882 /* In cases like template<> constexpr bool v = true;
2883 We'll give an error in check_template_variable. */
2884 break;
2885
2886 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2887 if (ctype)
2888 member_specialization = 1;
2889 else
2890 specialization = 1;
2891 break;
2892
2893 case tsk_template:
2894 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2895 {
2896 /* This case handles bogus declarations like template <>
2897 template <class T> void f<int>(); */
2898
2899 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2900 error_at (cp_expr_loc_or_input_loc (declarator),
2901 "template-id %qE in declaration of primary template",
2902 declarator);
2903 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2904 {
2905 /* Partial specialization of variable template. */
2906 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2907 specialization = 1;
2908 goto ok;
2909 }
2910 else if (cxx_dialect < cxx14)
2911 error_at (cp_expr_loc_or_input_loc (declarator),
2912 "non-type partial specialization %qE "
2913 "is not allowed", declarator);
2914 else
2915 error_at (cp_expr_loc_or_input_loc (declarator),
2916 "non-class, non-variable partial specialization %qE "
2917 "is not allowed", declarator);
2918 return decl;
2919 ok:;
2920 }
2921
2922 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2923 /* This is a specialization of a member template, without
2924 specialization the containing class. Something like:
2925
2926 template <class T> struct S {
2927 template <class U> void f (U);
2928 };
2929 template <> template <class U> void S<int>::f(U) {}
2930
2931 That's a specialization -- but of the entire template. */
2932 specialization = 1;
2933 break;
2934
2935 default:
2936 gcc_unreachable ();
2937 }
2938
2939 if ((specialization || member_specialization)
2940 /* This doesn't apply to variable templates. */
2941 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2942 {
2943 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2944 for (; t; t = TREE_CHAIN (t))
2945 if (TREE_PURPOSE (t))
2946 {
2947 permerror (input_location,
2948 "default argument specified in explicit specialization");
2949 break;
2950 }
2951 }
2952
2953 if (specialization || member_specialization || explicit_instantiation)
2954 {
2955 tree tmpl = NULL_TREE;
2956 tree targs = NULL_TREE;
2957 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2958 bool found_hidden = false;
2959
2960 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2961 if (!was_template_id)
2962 {
2963 tree fns;
2964
2965 gcc_assert (identifier_p (declarator));
2966 if (ctype)
2967 fns = dname;
2968 else
2969 {
2970 /* If there is no class context, the explicit instantiation
2971 must be at namespace scope. */
2972 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2973
2974 /* Find the namespace binding, using the declaration
2975 context. */
2976 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2977 LOOK_want::NORMAL, true);
2978 if (fns == error_mark_node)
2979 {
2980 /* If lookup fails, look for a friend declaration so we can
2981 give a better diagnostic. */
2982 fns = (lookup_qualified_name
2983 (CP_DECL_CONTEXT (decl), dname,
2984 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
2985 /*complain*/true));
2986 found_hidden = true;
2987 }
2988
2989 if (fns == error_mark_node || !is_overloaded_fn (fns))
2990 {
2991 error ("%qD is not a template function", dname);
2992 fns = error_mark_node;
2993 }
2994 }
2995
2996 declarator = lookup_template_function (fns, NULL_TREE);
2997 }
2998
2999 if (declarator == error_mark_node)
3000 return error_mark_node;
3001
3002 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3003 {
3004 if (!explicit_instantiation)
3005 /* A specialization in class scope. This is invalid,
3006 but the error will already have been flagged by
3007 check_specialization_scope. */
3008 return error_mark_node;
3009 else
3010 {
3011 /* It's not valid to write an explicit instantiation in
3012 class scope, e.g.:
3013
3014 class C { template void f(); }
3015
3016 This case is caught by the parser. However, on
3017 something like:
3018
3019 template class C { void f(); };
3020
3021 (which is invalid) we can get here. The error will be
3022 issued later. */
3023 ;
3024 }
3025
3026 return decl;
3027 }
3028 else if (ctype != NULL_TREE
3029 && (identifier_p (TREE_OPERAND (declarator, 0))))
3030 {
3031 // We'll match variable templates in start_decl.
3032 if (VAR_P (decl))
3033 return decl;
3034
3035 /* Find the list of functions in ctype that have the same
3036 name as the declared function. */
3037 tree name = TREE_OPERAND (declarator, 0);
3038
3039 if (constructor_name_p (name, ctype))
3040 {
3041 if (DECL_CONSTRUCTOR_P (decl)
3042 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3043 : !CLASSTYPE_DESTRUCTOR (ctype))
3044 {
3045 /* From [temp.expl.spec]:
3046
3047 If such an explicit specialization for the member
3048 of a class template names an implicitly-declared
3049 special member function (clause _special_), the
3050 program is ill-formed.
3051
3052 Similar language is found in [temp.explicit]. */
3053 error ("specialization of implicitly-declared special member function");
3054 return error_mark_node;
3055 }
3056
3057 name = DECL_NAME (decl);
3058 }
3059
3060 /* For a type-conversion operator, We might be looking for
3061 `operator int' which will be a specialization of
3062 `operator T'. Grab all the conversion operators, and
3063 then select from them. */
3064 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3065 ? conv_op_identifier : name);
3066
3067 if (fns == NULL_TREE)
3068 {
3069 error ("no member function %qD declared in %qT", name, ctype);
3070 return error_mark_node;
3071 }
3072 else
3073 TREE_OPERAND (declarator, 0) = fns;
3074 }
3075
3076 /* Figure out what exactly is being specialized at this point.
3077 Note that for an explicit instantiation, even one for a
3078 member function, we cannot tell a priori whether the
3079 instantiation is for a member template, or just a member
3080 function of a template class. Even if a member template is
3081 being instantiated, the member template arguments may be
3082 elided if they can be deduced from the rest of the
3083 declaration. */
3084 tmpl = determine_specialization (declarator, decl,
3085 &targs,
3086 member_specialization,
3087 template_count,
3088 tsk);
3089
3090 if (!tmpl || tmpl == error_mark_node)
3091 /* We couldn't figure out what this declaration was
3092 specializing. */
3093 return error_mark_node;
3094 else
3095 {
3096 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3097 {
3098 auto_diagnostic_group d;
3099 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3100 "friend declaration %qD is not visible to "
3101 "explicit specialization", tmpl))
3102 inform (DECL_SOURCE_LOCATION (tmpl),
3103 "friend declaration here");
3104 }
3105
3106 if (!ctype && !is_friend
3107 && CP_DECL_CONTEXT (decl) == current_namespace)
3108 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3109
3110 tree gen_tmpl = most_general_template (tmpl);
3111
3112 if (explicit_instantiation)
3113 {
3114 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3115 is done by do_decl_instantiation later. */
3116
3117 int arg_depth = TMPL_ARGS_DEPTH (targs);
3118 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3119
3120 if (arg_depth > parm_depth)
3121 {
3122 /* If TMPL is not the most general template (for
3123 example, if TMPL is a friend template that is
3124 injected into namespace scope), then there will
3125 be too many levels of TARGS. Remove some of them
3126 here. */
3127 int i;
3128 tree new_targs;
3129
3130 new_targs = make_tree_vec (parm_depth);
3131 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3132 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3133 = TREE_VEC_ELT (targs, i);
3134 targs = new_targs;
3135 }
3136
3137 return instantiate_template (tmpl, targs, tf_error);
3138 }
3139
3140 /* If we thought that the DECL was a member function, but it
3141 turns out to be specializing a static member function,
3142 make DECL a static member function as well. */
3143 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3144 && DECL_STATIC_FUNCTION_P (tmpl)
3145 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3146 revert_static_member_fn (decl);
3147
3148 /* If this is a specialization of a member template of a
3149 template class, we want to return the TEMPLATE_DECL, not
3150 the specialization of it. */
3151 if (tsk == tsk_template && !was_template_id)
3152 {
3153 tree result = DECL_TEMPLATE_RESULT (tmpl);
3154 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3155 DECL_INITIAL (result) = NULL_TREE;
3156 if (have_def)
3157 {
3158 tree parm;
3159 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3160 DECL_SOURCE_LOCATION (result)
3161 = DECL_SOURCE_LOCATION (decl);
3162 /* We want to use the argument list specified in the
3163 definition, not in the original declaration. */
3164 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3165 for (parm = DECL_ARGUMENTS (result); parm;
3166 parm = DECL_CHAIN (parm))
3167 DECL_CONTEXT (parm) = result;
3168 }
3169 decl = register_specialization (tmpl, gen_tmpl, targs,
3170 is_friend, 0);
3171 remove_contract_attributes (result);
3172 return decl;
3173 }
3174
3175 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3176 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3177
3178 if (was_template_id)
3179 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3180
3181 /* Inherit default function arguments from the template
3182 DECL is specializing. */
3183 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3184 copy_default_args_to_explicit_spec (decl);
3185
3186 /* This specialization has the same protection as the
3187 template it specializes. */
3188 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3189 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3190
3191 /* 7.1.1-1 [dcl.stc]
3192
3193 A storage-class-specifier shall not be specified in an
3194 explicit specialization...
3195
3196 The parser rejects these, so unless action is taken here,
3197 explicit function specializations will always appear with
3198 global linkage.
3199
3200 The action recommended by the C++ CWG in response to C++
3201 defect report 605 is to make the storage class and linkage
3202 of the explicit specialization match the templated function:
3203
3204 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3205 */
3206 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3207 {
3208 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3209 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3210
3211 /* A concept cannot be specialized. */
3212 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3213 {
3214 error ("explicit specialization of function concept %qD",
3215 gen_tmpl);
3216 return error_mark_node;
3217 }
3218
3219 /* This specialization has the same linkage and visibility as
3220 the function template it specializes. */
3221 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3222 if (! TREE_PUBLIC (decl))
3223 {
3224 DECL_INTERFACE_KNOWN (decl) = 1;
3225 DECL_NOT_REALLY_EXTERN (decl) = 1;
3226 }
3227 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3228 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3229 {
3230 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3231 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3232 }
3233 }
3234
3235 /* If DECL is a friend declaration, declared using an
3236 unqualified name, the namespace associated with DECL may
3237 have been set incorrectly. For example, in:
3238
3239 template <typename T> void f(T);
3240 namespace N {
3241 struct S { friend void f<int>(int); }
3242 }
3243
3244 we will have set the DECL_CONTEXT for the friend
3245 declaration to N, rather than to the global namespace. */
3246 if (DECL_NAMESPACE_SCOPE_P (decl))
3247 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3248
3249 if (is_friend && !have_def)
3250 /* This is not really a declaration of a specialization.
3251 It's just the name of an instantiation. But, it's not
3252 a request for an instantiation, either. */
3253 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3254 else if (TREE_CODE (decl) == FUNCTION_DECL)
3255 /* A specialization is not necessarily COMDAT. */
3256 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3257 && DECL_DECLARED_INLINE_P (decl));
3258 else if (VAR_P (decl))
3259 DECL_COMDAT (decl) = false;
3260
3261 /* If this is a full specialization, register it so that we can find
3262 it again. Partial specializations will be registered in
3263 process_partial_specialization. */
3264 if (!processing_template_decl)
3265 {
3266 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3267
3268 decl = register_specialization (decl, gen_tmpl, targs,
3269 is_friend, 0);
3270 }
3271
3272 /* If this is a specialization, splice any contracts that may have
3273 been inherited from the template, removing them. */
3274 if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
3275 remove_contract_attributes (decl);
3276
3277 /* A 'structor should already have clones. */
3278 gcc_assert (decl == error_mark_node
3279 || variable_template_p (tmpl)
3280 || !(DECL_CONSTRUCTOR_P (decl)
3281 || DECL_DESTRUCTOR_P (decl))
3282 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3283 }
3284 }
3285
3286 return decl;
3287 }
3288
3289 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3290 parameters. These are represented in the same format used for
3291 DECL_TEMPLATE_PARMS. */
3292
3293 int
3294 comp_template_parms (const_tree parms1, const_tree parms2)
3295 {
3296 const_tree p1;
3297 const_tree p2;
3298
3299 if (parms1 == parms2)
3300 return 1;
3301
3302 for (p1 = parms1, p2 = parms2;
3303 p1 != NULL_TREE && p2 != NULL_TREE;
3304 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3305 {
3306 tree t1 = TREE_VALUE (p1);
3307 tree t2 = TREE_VALUE (p2);
3308 int i;
3309
3310 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3311 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3312
3313 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3314 return 0;
3315
3316 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3317 {
3318 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3319 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3320
3321 /* If either of the template parameters are invalid, assume
3322 they match for the sake of error recovery. */
3323 if (error_operand_p (parm1) || error_operand_p (parm2))
3324 return 1;
3325
3326 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3327 return 0;
3328
3329 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3330 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3331 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3332 continue;
3333 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3334 return 0;
3335 }
3336 }
3337
3338 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3339 /* One set of parameters has more parameters lists than the
3340 other. */
3341 return 0;
3342
3343 return 1;
3344 }
3345
3346 /* Returns true if two template parameters are declared with
3347 equivalent constraints. */
3348
3349 static bool
3350 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3351 {
3352 tree req1 = TREE_TYPE (parm1);
3353 tree req2 = TREE_TYPE (parm2);
3354 if (!req1 != !req2)
3355 return false;
3356 if (req1)
3357 return cp_tree_equal (req1, req2);
3358 return true;
3359 }
3360
3361 /* Returns true when two template parameters are equivalent. */
3362
3363 static bool
3364 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3365 {
3366 tree decl1 = TREE_VALUE (parm1);
3367 tree decl2 = TREE_VALUE (parm2);
3368
3369 /* If either of the template parameters are invalid, assume
3370 they match for the sake of error recovery. */
3371 if (error_operand_p (decl1) || error_operand_p (decl2))
3372 return true;
3373
3374 /* ... they declare parameters of the same kind. */
3375 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3376 return false;
3377
3378 /* ... one parameter was introduced by a parameter declaration, then
3379 both are. This case arises as a result of eagerly rewriting declarations
3380 during parsing. */
3381 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3382 return false;
3383
3384 /* ... if either declares a pack, they both do. */
3385 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3386 return false;
3387
3388 if (TREE_CODE (decl1) == PARM_DECL)
3389 {
3390 /* ... if they declare non-type parameters, the types are equivalent. */
3391 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3392 return false;
3393 }
3394 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3395 {
3396 /* ... if they declare template template parameters, their template
3397 parameter lists are equivalent. */
3398 if (!template_heads_equivalent_p (decl1, decl2))
3399 return false;
3400 }
3401
3402 /* ... if they are declared with a qualified-concept name, they both
3403 are, and those names are equivalent. */
3404 return template_parameter_constraints_equivalent_p (parm1, parm2);
3405 }
3406
3407 /* Returns true if two template parameters lists are equivalent.
3408 Two template parameter lists are equivalent if they have the
3409 same length and their corresponding parameters are equivalent.
3410
3411 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3412 data structure returned by DECL_TEMPLATE_PARMS.
3413
3414 This is generally the same implementation as comp_template_parms
3415 except that it also the concept names and arguments used to
3416 introduce parameters. */
3417
3418 static bool
3419 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3420 {
3421 if (parms1 == parms2)
3422 return true;
3423
3424 const_tree p1 = parms1;
3425 const_tree p2 = parms2;
3426 while (p1 != NULL_TREE && p2 != NULL_TREE)
3427 {
3428 tree list1 = TREE_VALUE (p1);
3429 tree list2 = TREE_VALUE (p2);
3430
3431 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3432 return 0;
3433
3434 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3435 {
3436 tree parm1 = TREE_VEC_ELT (list1, i);
3437 tree parm2 = TREE_VEC_ELT (list2, i);
3438 if (!template_parameters_equivalent_p (parm1, parm2))
3439 return false;
3440 }
3441
3442 p1 = TREE_CHAIN (p1);
3443 p2 = TREE_CHAIN (p2);
3444 }
3445
3446 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3447 return false;
3448
3449 return true;
3450 }
3451
3452 /* Return true if the requires-clause of the template parameter lists are
3453 equivalent and false otherwise. */
3454 static bool
3455 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3456 {
3457 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3458 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3459 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3460 return false;
3461 if (!cp_tree_equal (req1, req2))
3462 return false;
3463 return true;
3464 }
3465
3466 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3467 Two template heads are equivalent if their template parameter
3468 lists are equivalent and their requires clauses are equivalent.
3469
3470 In pre-C++20, this is equivalent to calling comp_template_parms
3471 for the template parameters of TMPL1 and TMPL2. */
3472
3473 bool
3474 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3475 {
3476 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3477 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3478
3479 /* Don't change the matching rules for pre-C++20. */
3480 if (cxx_dialect < cxx20)
3481 return comp_template_parms (parms1, parms2);
3482
3483 /* ... have the same number of template parameters, and their
3484 corresponding parameters are equivalent. */
3485 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3486 return false;
3487
3488 /* ... if either has a requires-clause, they both do and their
3489 corresponding constraint-expressions are equivalent. */
3490 return template_requirements_equivalent_p (parms1, parms2);
3491 }
3492
3493 /* Determine whether PARM is a parameter pack. */
3494
3495 bool
3496 template_parameter_pack_p (const_tree parm)
3497 {
3498 /* Determine if we have a non-type template parameter pack. */
3499 if (TREE_CODE (parm) == PARM_DECL)
3500 return (DECL_TEMPLATE_PARM_P (parm)
3501 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3502 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3503 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3504
3505 /* If this is a list of template parameters, we could get a
3506 TYPE_DECL or a TEMPLATE_DECL. */
3507 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3508 parm = TREE_TYPE (parm);
3509
3510 /* Otherwise it must be a type template parameter. */
3511 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3512 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3513 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3514 }
3515
3516 /* Determine if T is a function parameter pack. */
3517
3518 bool
3519 function_parameter_pack_p (const_tree t)
3520 {
3521 if (t && TREE_CODE (t) == PARM_DECL)
3522 return DECL_PACK_P (t);
3523 return false;
3524 }
3525
3526 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3527 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3528
3529 tree
3530 get_function_template_decl (const_tree primary_func_tmpl_inst)
3531 {
3532 if (! primary_func_tmpl_inst
3533 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3534 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3535 return NULL;
3536
3537 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3538 }
3539
3540 /* Return true iff the function parameter PARAM_DECL was expanded
3541 from the function parameter pack PACK. */
3542
3543 bool
3544 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3545 {
3546 if (DECL_ARTIFICIAL (param_decl)
3547 || !function_parameter_pack_p (pack))
3548 return false;
3549
3550 /* The parameter pack and its pack arguments have the same
3551 DECL_PARM_INDEX. */
3552 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3553 }
3554
3555 /* Determine whether ARGS describes a variadic template args list,
3556 i.e., one that is terminated by a template argument pack. */
3557
3558 static bool
3559 template_args_variadic_p (tree args)
3560 {
3561 int nargs;
3562 tree last_parm;
3563
3564 if (args == NULL_TREE)
3565 return false;
3566
3567 args = INNERMOST_TEMPLATE_ARGS (args);
3568 nargs = TREE_VEC_LENGTH (args);
3569
3570 if (nargs == 0)
3571 return false;
3572
3573 last_parm = TREE_VEC_ELT (args, nargs - 1);
3574
3575 return ARGUMENT_PACK_P (last_parm);
3576 }
3577
3578 /* Generate a new name for the parameter pack name NAME (an
3579 IDENTIFIER_NODE) that incorporates its */
3580
3581 static tree
3582 make_ith_pack_parameter_name (tree name, int i)
3583 {
3584 /* Munge the name to include the parameter index. */
3585 #define NUMBUF_LEN 128
3586 char numbuf[NUMBUF_LEN];
3587 char* newname;
3588 int newname_len;
3589
3590 if (name == NULL_TREE)
3591 return name;
3592 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3593 newname_len = IDENTIFIER_LENGTH (name)
3594 + strlen (numbuf) + 2;
3595 newname = (char*)alloca (newname_len);
3596 snprintf (newname, newname_len,
3597 "%s#%i", IDENTIFIER_POINTER (name), i);
3598 return get_identifier (newname);
3599 }
3600
3601 /* Return true if T is a primary function, class or alias template
3602 specialization, not including the template pattern. */
3603
3604 bool
3605 primary_template_specialization_p (const_tree t)
3606 {
3607 if (!t)
3608 return false;
3609
3610 if (VAR_OR_FUNCTION_DECL_P (t))
3611 return (DECL_LANG_SPECIFIC (t)
3612 && DECL_USE_TEMPLATE (t)
3613 && DECL_TEMPLATE_INFO (t)
3614 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3615 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3616 return (CLASSTYPE_TEMPLATE_INFO (t)
3617 && CLASSTYPE_USE_TEMPLATE (t)
3618 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3619 else if (alias_template_specialization_p (t, nt_transparent))
3620 return true;
3621 return false;
3622 }
3623
3624 /* Return true if PARM is a template template parameter. */
3625
3626 bool
3627 template_template_parameter_p (const_tree parm)
3628 {
3629 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3630 }
3631
3632 /* Return true iff PARM is a DECL representing a type template
3633 parameter. */
3634
3635 bool
3636 template_type_parameter_p (const_tree parm)
3637 {
3638 return (parm
3639 && (TREE_CODE (parm) == TYPE_DECL
3640 || TREE_CODE (parm) == TEMPLATE_DECL)
3641 && DECL_TEMPLATE_PARM_P (parm));
3642 }
3643
3644 /* Return the template parameters of T if T is a
3645 primary template instantiation, NULL otherwise. */
3646
3647 tree
3648 get_primary_template_innermost_parameters (const_tree t)
3649 {
3650 tree parms = NULL, template_info = NULL;
3651
3652 if ((template_info = get_template_info (t))
3653 && primary_template_specialization_p (t))
3654 parms = INNERMOST_TEMPLATE_PARMS
3655 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3656
3657 return parms;
3658 }
3659
3660 /* Returns the template arguments of T if T is a template instantiation,
3661 NULL otherwise. */
3662
3663 tree
3664 get_template_innermost_arguments (const_tree t)
3665 {
3666 tree args = NULL, template_info = NULL;
3667
3668 if ((template_info = get_template_info (t))
3669 && TI_ARGS (template_info))
3670 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3671
3672 return args;
3673 }
3674
3675 /* Return the argument pack elements of T if T is a template argument pack,
3676 NULL otherwise. */
3677
3678 tree
3679 get_template_argument_pack_elems (const_tree t)
3680 {
3681 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3682 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3683 return NULL;
3684
3685 return ARGUMENT_PACK_ARGS (t);
3686 }
3687
3688 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3689 ARGUMENT_PACK_SELECT represents. */
3690
3691 static tree
3692 argument_pack_select_arg (tree t)
3693 {
3694 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3695 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3696
3697 /* If the selected argument is an expansion E, that most likely means we were
3698 called from gen_elem_of_pack_expansion_instantiation during the
3699 substituting of an argument pack (of which the Ith element is a pack
3700 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3701 In this case, the Ith element resulting from this substituting is going to
3702 be a pack expansion, which pattern is the pattern of E. Let's return the
3703 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3704 resulting pack expansion from it. */
3705 if (PACK_EXPANSION_P (arg))
3706 {
3707 /* Make sure we aren't throwing away arg info. */
3708 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3709 arg = PACK_EXPANSION_PATTERN (arg);
3710 }
3711
3712 return arg;
3713 }
3714
3715 /* Return a modification of ARGS that's suitable for preserving inside a hash
3716 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its
3717 underlying argument. ARGS is copied (upon modification) iff COW_P. */
3718
3719 static tree
3720 preserve_args (tree args, bool cow_p = true)
3721 {
3722 if (!args)
3723 return NULL_TREE;
3724
3725 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
3726 {
3727 tree t = TREE_VEC_ELT (args, i);
3728 tree r;
3729 if (!t)
3730 r = NULL_TREE;
3731 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
3732 r = argument_pack_select_arg (t);
3733 else if (TREE_CODE (t) == TREE_VEC)
3734 r = preserve_args (t, cow_p);
3735 else
3736 r = t;
3737 if (r != t)
3738 {
3739 if (cow_p)
3740 {
3741 args = copy_template_args (args);
3742 cow_p = false;
3743 }
3744 TREE_VEC_ELT (args, i) = r;
3745 }
3746 }
3747
3748 return args;
3749 }
3750
3751 /* True iff FN is a function representing a built-in variadic parameter
3752 pack. */
3753
3754 bool
3755 builtin_pack_fn_p (tree fn)
3756 {
3757 if (!fn
3758 || TREE_CODE (fn) != FUNCTION_DECL
3759 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3760 return false;
3761
3762 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3763 return true;
3764
3765 return false;
3766 }
3767
3768 /* True iff CALL is a call to a function representing a built-in variadic
3769 parameter pack. */
3770
3771 static bool
3772 builtin_pack_call_p (tree call)
3773 {
3774 if (TREE_CODE (call) != CALL_EXPR)
3775 return false;
3776 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3777 }
3778
3779 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3780
3781 static tree
3782 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3783 tree in_decl)
3784 {
3785 tree ohi = CALL_EXPR_ARG (call, 0);
3786 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl);
3787
3788 if (instantiation_dependent_expression_p (hi))
3789 {
3790 if (hi != ohi)
3791 {
3792 call = copy_node (call);
3793 CALL_EXPR_ARG (call, 0) = hi;
3794 }
3795 tree ex = make_pack_expansion (call, complain);
3796 tree vec = make_tree_vec (1);
3797 TREE_VEC_ELT (vec, 0) = ex;
3798 return vec;
3799 }
3800 else
3801 {
3802 hi = instantiate_non_dependent_expr (hi, complain);
3803 hi = cxx_constant_value (hi, complain);
3804 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3805
3806 /* Calculate the largest value of len that won't make the size of the vec
3807 overflow an int. The compiler will exceed resource limits long before
3808 this, but it seems a decent place to diagnose. */
3809 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3810
3811 if (len < 0 || len > max)
3812 {
3813 if ((complain & tf_error)
3814 && hi != error_mark_node)
3815 error ("argument to %<__integer_pack%> must be between 0 and %d",
3816 max);
3817 return error_mark_node;
3818 }
3819
3820 tree vec = make_tree_vec (len);
3821
3822 for (int i = 0; i < len; ++i)
3823 TREE_VEC_ELT (vec, i) = size_int (i);
3824
3825 return vec;
3826 }
3827 }
3828
3829 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3830 CALL. */
3831
3832 static tree
3833 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3834 tree in_decl)
3835 {
3836 if (!builtin_pack_call_p (call))
3837 return NULL_TREE;
3838
3839 tree fn = CALL_EXPR_FN (call);
3840
3841 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3842 return expand_integer_pack (call, args, complain, in_decl);
3843
3844 return NULL_TREE;
3845 }
3846
3847 /* Return true if the tree T has the extra args mechanism for
3848 avoiding partial instantiation. */
3849
3850 static bool
3851 has_extra_args_mechanism_p (const_tree t)
3852 {
3853 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */
3854 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */
3855 || (TREE_CODE (t) == IF_STMT
3856 && IF_STMT_CONSTEXPR_P (t))); /* IF_STMT_EXTRA_ARGS */
3857 }
3858
3859 /* Structure used to track the progress of find_parameter_packs_r. */
3860 struct find_parameter_pack_data
3861 {
3862 /* TREE_LIST that will contain all of the parameter packs found by
3863 the traversal. */
3864 tree* parameter_packs;
3865
3866 /* Set of AST nodes that have been visited by the traversal. */
3867 hash_set<tree> *visited;
3868
3869 /* True iff we're making a type pack expansion. */
3870 bool type_pack_expansion_p;
3871
3872 /* True iff we found a subtree that has the extra args mechanism. */
3873 bool found_extra_args_tree_p = false;
3874 };
3875
3876 /* Identifies all of the argument packs that occur in a template
3877 argument and appends them to the TREE_LIST inside DATA, which is a
3878 find_parameter_pack_data structure. This is a subroutine of
3879 make_pack_expansion and uses_parameter_packs. */
3880 static tree
3881 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3882 {
3883 tree t = *tp;
3884 struct find_parameter_pack_data* ppd =
3885 (struct find_parameter_pack_data*)data;
3886 bool parameter_pack_p = false;
3887
3888 #define WALK_SUBTREE(NODE) \
3889 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3890 ppd, ppd->visited) \
3891
3892 /* Don't look through typedefs; we are interested in whether a
3893 parameter pack is actually written in the expression/type we're
3894 looking at, not the target type. */
3895 if (TYPE_P (t) && typedef_variant_p (t))
3896 {
3897 /* But do look at arguments for an alias template. */
3898 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3899 cp_walk_tree (&TI_ARGS (tinfo),
3900 &find_parameter_packs_r,
3901 ppd, ppd->visited);
3902 *walk_subtrees = 0;
3903 return NULL_TREE;
3904 }
3905
3906 /* Identify whether this is a parameter pack or not. */
3907 switch (TREE_CODE (t))
3908 {
3909 case TEMPLATE_PARM_INDEX:
3910 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3911 parameter_pack_p = true;
3912 break;
3913
3914 case TEMPLATE_TYPE_PARM:
3915 t = TYPE_MAIN_VARIANT (t);
3916 /* FALLTHRU */
3917 case TEMPLATE_TEMPLATE_PARM:
3918 /* If the placeholder appears in the decl-specifier-seq of a function
3919 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3920 is a pack expansion, the invented template parameter is a template
3921 parameter pack. */
3922 if (ppd->type_pack_expansion_p && is_auto (t))
3923 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3924 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3925 parameter_pack_p = true;
3926 break;
3927
3928 case FIELD_DECL:
3929 case PARM_DECL:
3930 if (DECL_PACK_P (t))
3931 {
3932 /* We don't want to walk into the type of a PARM_DECL,
3933 because we don't want to see the type parameter pack. */
3934 *walk_subtrees = 0;
3935 parameter_pack_p = true;
3936 }
3937 break;
3938
3939 case VAR_DECL:
3940 if (DECL_PACK_P (t))
3941 {
3942 /* We don't want to walk into the type of a variadic capture proxy,
3943 because we don't want to see the type parameter pack. */
3944 *walk_subtrees = 0;
3945 parameter_pack_p = true;
3946 }
3947 else if (variable_template_specialization_p (t))
3948 {
3949 cp_walk_tree (&DECL_TI_ARGS (t),
3950 find_parameter_packs_r,
3951 ppd, ppd->visited);
3952 *walk_subtrees = 0;
3953 }
3954 break;
3955
3956 case CALL_EXPR:
3957 if (builtin_pack_call_p (t))
3958 parameter_pack_p = true;
3959 break;
3960
3961 case BASES:
3962 parameter_pack_p = true;
3963 break;
3964 default:
3965 /* Not a parameter pack. */
3966 break;
3967 }
3968
3969 if (parameter_pack_p)
3970 {
3971 /* Add this parameter pack to the list. */
3972 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3973 }
3974
3975 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
3976 ppd->found_extra_args_tree_p = true;
3977
3978 if (TYPE_P (t))
3979 cp_walk_tree (&TYPE_CONTEXT (t),
3980 &find_parameter_packs_r, ppd, ppd->visited);
3981
3982 /* This switch statement will return immediately if we don't find a
3983 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3984 switch (TREE_CODE (t))
3985 {
3986 case BOUND_TEMPLATE_TEMPLATE_PARM:
3987 /* Check the template itself. */
3988 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3989 &find_parameter_packs_r, ppd, ppd->visited);
3990 return NULL_TREE;
3991
3992 case DECL_EXPR:
3993 {
3994 tree decl = DECL_EXPR_DECL (t);
3995 /* Ignore the declaration of a capture proxy for a parameter pack. */
3996 if (is_capture_proxy (decl))
3997 *walk_subtrees = 0;
3998 if (is_typedef_decl (decl))
3999 /* Since we stop at typedefs above, we need to look through them at
4000 the point of the DECL_EXPR. */
4001 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
4002 &find_parameter_packs_r, ppd, ppd->visited);
4003 return NULL_TREE;
4004 }
4005
4006 case TEMPLATE_DECL:
4007 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4008 return NULL_TREE;
4009 cp_walk_tree (&TREE_TYPE (t),
4010 &find_parameter_packs_r, ppd, ppd->visited);
4011 return NULL_TREE;
4012
4013 case TYPE_PACK_EXPANSION:
4014 case EXPR_PACK_EXPANSION:
4015 *walk_subtrees = 0;
4016 return NULL_TREE;
4017
4018 case INTEGER_TYPE:
4019 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4020 ppd, ppd->visited);
4021 *walk_subtrees = 0;
4022 return NULL_TREE;
4023
4024 case IDENTIFIER_NODE:
4025 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4026 ppd->visited);
4027 *walk_subtrees = 0;
4028 return NULL_TREE;
4029
4030 case LAMBDA_EXPR:
4031 {
4032 /* Since we defer implicit capture, look in the parms and body. */
4033 tree fn = lambda_function (t);
4034 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4035 ppd->visited);
4036 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4037 ppd->visited);
4038 return NULL_TREE;
4039 }
4040
4041 case DECLTYPE_TYPE:
4042 {
4043 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4044 type_pack_expansion_p to false so that any placeholders
4045 within the expression don't get marked as parameter packs. */
4046 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4047 ppd->type_pack_expansion_p = false;
4048 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4049 ppd, ppd->visited);
4050 ppd->type_pack_expansion_p = type_pack_expansion_p;
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4053 }
4054
4055 case IF_STMT:
4056 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4057 ppd, ppd->visited);
4058 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4059 ppd, ppd->visited);
4060 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4061 ppd, ppd->visited);
4062 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4063 *walk_subtrees = 0;
4064 return NULL_TREE;
4065
4066 case TAG_DEFN:
4067 t = TREE_TYPE (t);
4068 if (CLASS_TYPE_P (t))
4069 /* Local class, need to look through the whole definition. */
4070 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4071 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4072 ppd, ppd->visited);
4073 else
4074 /* Enum, look at the values. */
4075 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4076 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4077 &find_parameter_packs_r,
4078 ppd, ppd->visited);
4079 return NULL_TREE;
4080
4081 case FUNCTION_TYPE:
4082 case METHOD_TYPE:
4083 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4084 break;
4085
4086 default:
4087 return NULL_TREE;
4088 }
4089
4090 #undef WALK_SUBTREE
4091
4092 return NULL_TREE;
4093 }
4094
4095 /* Determines if the expression or type T uses any parameter packs. */
4096 tree
4097 uses_parameter_packs (tree t)
4098 {
4099 tree parameter_packs = NULL_TREE;
4100 struct find_parameter_pack_data ppd;
4101 ppd.parameter_packs = &parameter_packs;
4102 ppd.visited = new hash_set<tree>;
4103 ppd.type_pack_expansion_p = false;
4104 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4105 delete ppd.visited;
4106 return parameter_packs;
4107 }
4108
4109 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4110 representation a base-class initializer into a parameter pack
4111 expansion. If all goes well, the resulting node will be an
4112 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4113 respectively. */
4114 tree
4115 make_pack_expansion (tree arg, tsubst_flags_t complain)
4116 {
4117 tree result;
4118 tree parameter_packs = NULL_TREE;
4119 bool for_types = false;
4120 struct find_parameter_pack_data ppd;
4121
4122 if (!arg || arg == error_mark_node)
4123 return arg;
4124
4125 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4126 {
4127 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4128 class initializer. In this case, the TREE_PURPOSE will be a
4129 _TYPE node (representing the base class expansion we're
4130 initializing) and the TREE_VALUE will be a TREE_LIST
4131 containing the initialization arguments.
4132
4133 The resulting expansion looks somewhat different from most
4134 expansions. Rather than returning just one _EXPANSION, we
4135 return a TREE_LIST whose TREE_PURPOSE is a
4136 TYPE_PACK_EXPANSION containing the bases that will be
4137 initialized. The TREE_VALUE will be identical to the
4138 original TREE_VALUE, which is a list of arguments that will
4139 be passed to each base. We do not introduce any new pack
4140 expansion nodes into the TREE_VALUE (although it is possible
4141 that some already exist), because the TREE_PURPOSE and
4142 TREE_VALUE all need to be expanded together with the same
4143 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4144 resulting TREE_PURPOSE will mention the parameter packs in
4145 both the bases and the arguments to the bases. */
4146 tree purpose;
4147 tree value;
4148 tree parameter_packs = NULL_TREE;
4149
4150 /* Determine which parameter packs will be used by the base
4151 class expansion. */
4152 ppd.visited = new hash_set<tree>;
4153 ppd.parameter_packs = &parameter_packs;
4154 ppd.type_pack_expansion_p = false;
4155 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4156 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4157 &ppd, ppd.visited);
4158
4159 if (parameter_packs == NULL_TREE)
4160 {
4161 if (complain & tf_error)
4162 error ("base initializer expansion %qT contains no parameter packs",
4163 arg);
4164 delete ppd.visited;
4165 return error_mark_node;
4166 }
4167
4168 if (TREE_VALUE (arg) != void_type_node)
4169 {
4170 /* Collect the sets of parameter packs used in each of the
4171 initialization arguments. */
4172 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4173 {
4174 /* Determine which parameter packs will be expanded in this
4175 argument. */
4176 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4177 &ppd, ppd.visited);
4178 }
4179 }
4180
4181 delete ppd.visited;
4182
4183 /* Create the pack expansion type for the base type. */
4184 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4185 PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
4186 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4187 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4188
4189 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4190 they will rarely be compared to anything. */
4191 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4192
4193 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4194 }
4195
4196 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4197 for_types = true;
4198
4199 /* Build the PACK_EXPANSION_* node. */
4200 result = for_types
4201 ? cxx_make_type (TYPE_PACK_EXPANSION)
4202 : make_node (EXPR_PACK_EXPANSION);
4203 PACK_EXPANSION_PATTERN (result) = arg;
4204 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4205 {
4206 /* Propagate type and const-expression information. */
4207 TREE_TYPE (result) = TREE_TYPE (arg);
4208 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4209 /* Mark this read now, since the expansion might be length 0. */
4210 mark_exp_read (arg);
4211 }
4212 else
4213 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4214 they will rarely be compared to anything. */
4215 SET_TYPE_STRUCTURAL_EQUALITY (result);
4216
4217 /* Determine which parameter packs will be expanded. */
4218 ppd.parameter_packs = &parameter_packs;
4219 ppd.visited = new hash_set<tree>;
4220 ppd.type_pack_expansion_p = TYPE_P (arg);
4221 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4222 delete ppd.visited;
4223
4224 /* Make sure we found some parameter packs. */
4225 if (parameter_packs == NULL_TREE)
4226 {
4227 if (complain & tf_error)
4228 {
4229 if (TYPE_P (arg))
4230 error ("expansion pattern %qT contains no parameter packs", arg);
4231 else
4232 error ("expansion pattern %qE contains no parameter packs", arg);
4233 }
4234 return error_mark_node;
4235 }
4236 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4237
4238 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4239 if (ppd.found_extra_args_tree_p)
4240 /* If the pattern of this pack expansion contains a subtree that has
4241 the extra args mechanism for avoiding partial instantiation, then
4242 force this pack expansion to also use extra args. Otherwise
4243 partial instantiation of this pack expansion may not lower the
4244 level of some parameter packs within the pattern, which would
4245 confuse tsubst_pack_expansion later (PR101764). */
4246 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
4247
4248 return result;
4249 }
4250
4251 /* Checks T for any "bare" parameter packs, which have not yet been
4252 expanded, and issues an error if any are found. This operation can
4253 only be done on full expressions or types (e.g., an expression
4254 statement, "if" condition, etc.), because we could have expressions like:
4255
4256 foo(f(g(h(args)))...)
4257
4258 where "args" is a parameter pack. check_for_bare_parameter_packs
4259 should not be called for the subexpressions args, h(args),
4260 g(h(args)), or f(g(h(args))), because we would produce erroneous
4261 error messages.
4262
4263 Returns TRUE and emits an error if there were bare parameter packs,
4264 returns FALSE otherwise. */
4265 bool
4266 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4267 {
4268 tree parameter_packs = NULL_TREE;
4269 struct find_parameter_pack_data ppd;
4270
4271 if (!processing_template_decl || !t || t == error_mark_node)
4272 return false;
4273
4274 if (TREE_CODE (t) == TYPE_DECL)
4275 t = TREE_TYPE (t);
4276
4277 ppd.parameter_packs = &parameter_packs;
4278 ppd.visited = new hash_set<tree>;
4279 ppd.type_pack_expansion_p = false;
4280 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4281 delete ppd.visited;
4282
4283 if (!parameter_packs)
4284 return false;
4285
4286 if (loc == UNKNOWN_LOCATION)
4287 loc = cp_expr_loc_or_input_loc (t);
4288
4289 /* It's OK for a lambda to have an unexpanded parameter pack from the
4290 containing context, but do complain about unexpanded capture packs. */
4291 tree lam = current_lambda_expr ();
4292 if (lam)
4293 lam = TREE_TYPE (lam);
4294
4295 if (lam && lam != current_class_type)
4296 {
4297 /* We're in a lambda, but it isn't the innermost class.
4298 This should work, but currently doesn't. */
4299 sorry_at (loc, "unexpanded parameter pack in local class in lambda");
4300 return true;
4301 }
4302
4303 if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
4304 for (; parameter_packs;
4305 parameter_packs = TREE_CHAIN (parameter_packs))
4306 {
4307 tree pack = TREE_VALUE (parameter_packs);
4308 if (is_capture_proxy (pack)
4309 || (TREE_CODE (pack) == PARM_DECL
4310 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
4311 break;
4312 }
4313
4314 if (parameter_packs)
4315 {
4316 error_at (loc, "parameter packs not expanded with %<...%>:");
4317 while (parameter_packs)
4318 {
4319 tree pack = TREE_VALUE (parameter_packs);
4320 tree name = NULL_TREE;
4321
4322 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4323 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4324 name = TYPE_NAME (pack);
4325 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4326 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4327 else if (TREE_CODE (pack) == CALL_EXPR)
4328 name = DECL_NAME (CALL_EXPR_FN (pack));
4329 else
4330 name = DECL_NAME (pack);
4331
4332 if (name)
4333 inform (loc, " %qD", name);
4334 else
4335 inform (loc, " %s", "<anonymous>");
4336
4337 parameter_packs = TREE_CHAIN (parameter_packs);
4338 }
4339
4340 return true;
4341 }
4342
4343 return false;
4344 }
4345
4346 /* Expand any parameter packs that occur in the template arguments in
4347 ARGS. */
4348 tree
4349 expand_template_argument_pack (tree args)
4350 {
4351 if (args == error_mark_node)
4352 return error_mark_node;
4353
4354 tree result_args = NULL_TREE;
4355 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4356 int num_result_args = -1;
4357 int non_default_args_count = -1;
4358
4359 /* First, determine if we need to expand anything, and the number of
4360 slots we'll need. */
4361 for (in_arg = 0; in_arg < nargs; ++in_arg)
4362 {
4363 tree arg = TREE_VEC_ELT (args, in_arg);
4364 if (arg == NULL_TREE)
4365 return args;
4366 if (ARGUMENT_PACK_P (arg))
4367 {
4368 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4369 if (num_result_args < 0)
4370 num_result_args = in_arg + num_packed;
4371 else
4372 num_result_args += num_packed;
4373 }
4374 else
4375 {
4376 if (num_result_args >= 0)
4377 num_result_args++;
4378 }
4379 }
4380
4381 /* If no expansion is necessary, we're done. */
4382 if (num_result_args < 0)
4383 return args;
4384
4385 /* Expand arguments. */
4386 result_args = make_tree_vec (num_result_args);
4387 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4388 non_default_args_count =
4389 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4390 for (in_arg = 0; in_arg < nargs; ++in_arg)
4391 {
4392 tree arg = TREE_VEC_ELT (args, in_arg);
4393 if (ARGUMENT_PACK_P (arg))
4394 {
4395 tree packed = ARGUMENT_PACK_ARGS (arg);
4396 int i, num_packed = TREE_VEC_LENGTH (packed);
4397 for (i = 0; i < num_packed; ++i, ++out_arg)
4398 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4399 if (non_default_args_count > 0)
4400 non_default_args_count += num_packed - 1;
4401 }
4402 else
4403 {
4404 TREE_VEC_ELT (result_args, out_arg) = arg;
4405 ++out_arg;
4406 }
4407 }
4408 if (non_default_args_count >= 0)
4409 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4410 return result_args;
4411 }
4412
4413 /* Checks if DECL shadows a template parameter.
4414
4415 [temp.local]: A template-parameter shall not be redeclared within its
4416 scope (including nested scopes).
4417
4418 Emits an error and returns TRUE if the DECL shadows a parameter,
4419 returns FALSE otherwise. */
4420
4421 bool
4422 check_template_shadow (tree decl)
4423 {
4424 tree olddecl;
4425
4426 /* If we're not in a template, we can't possibly shadow a template
4427 parameter. */
4428 if (!current_template_parms)
4429 return true;
4430
4431 /* Figure out what we're shadowing. */
4432 decl = OVL_FIRST (decl);
4433 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4434
4435 /* If there's no previous binding for this name, we're not shadowing
4436 anything, let alone a template parameter. */
4437 if (!olddecl)
4438 return true;
4439
4440 /* If we're not shadowing a template parameter, we're done. Note
4441 that OLDDECL might be an OVERLOAD (or perhaps even an
4442 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4443 node. */
4444 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4445 return true;
4446
4447 /* We check for decl != olddecl to avoid bogus errors for using a
4448 name inside a class. We check TPFI to avoid duplicate errors for
4449 inline member templates. */
4450 if (decl == olddecl
4451 || (DECL_TEMPLATE_PARM_P (decl)
4452 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4453 return true;
4454
4455 /* Don't complain about the injected class name, as we've already
4456 complained about the class itself. */
4457 if (DECL_SELF_REFERENCE_P (decl))
4458 return false;
4459
4460 if (DECL_TEMPLATE_PARM_P (decl))
4461 error ("declaration of template parameter %q+D shadows "
4462 "template parameter", decl);
4463 else
4464 error ("declaration of %q+#D shadows template parameter", decl);
4465 inform (DECL_SOURCE_LOCATION (olddecl),
4466 "template parameter %qD declared here", olddecl);
4467 return false;
4468 }
4469
4470 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4471 ORIG_LEVEL, DECL, and TYPE. */
4472
4473 static tree
4474 build_template_parm_index (int index,
4475 int level,
4476 int orig_level,
4477 tree decl,
4478 tree type)
4479 {
4480 tree t = make_node (TEMPLATE_PARM_INDEX);
4481 TEMPLATE_PARM_IDX (t) = index;
4482 TEMPLATE_PARM_LEVEL (t) = level;
4483 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4484 TEMPLATE_PARM_DECL (t) = decl;
4485 TREE_TYPE (t) = type;
4486 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4487 TREE_READONLY (t) = TREE_READONLY (decl);
4488
4489 return t;
4490 }
4491
4492 struct ctp_hasher : ggc_ptr_hash<tree_node>
4493 {
4494 static hashval_t hash (tree t)
4495 {
4496 ++comparing_specializations;
4497 tree_code code = TREE_CODE (t);
4498 hashval_t val = iterative_hash_object (code, 0);
4499 val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
4500 val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
4501 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
4502 val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
4503 --comparing_specializations;
4504 return val;
4505 }
4506
4507 static bool equal (tree t, tree u)
4508 {
4509 ++comparing_specializations;
4510 bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
4511 --comparing_specializations;
4512 return eq;
4513 }
4514 };
4515
4516 static GTY (()) hash_table<ctp_hasher> *ctp_table;
4517
4518 /* Find the canonical type parameter for the given template type
4519 parameter. Returns the canonical type parameter, which may be TYPE
4520 if no such parameter existed. */
4521
4522 tree
4523 canonical_type_parameter (tree type)
4524 {
4525 if (ctp_table == NULL)
4526 ctp_table = hash_table<ctp_hasher>::create_ggc (61);
4527
4528 tree& slot = *ctp_table->find_slot (type, INSERT);
4529 if (slot == NULL_TREE)
4530 slot = type;
4531 return slot;
4532 }
4533
4534 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4535 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4536 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4537 new one is created. */
4538
4539 static tree
4540 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4541 tsubst_flags_t complain)
4542 {
4543 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4544 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4545 != TEMPLATE_PARM_LEVEL (index) - levels)
4546 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4547 {
4548 tree orig_decl = TEMPLATE_PARM_DECL (index);
4549
4550 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4551 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4552 type);
4553 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4554 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4555 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4556 DECL_ARTIFICIAL (decl) = 1;
4557 SET_DECL_TEMPLATE_PARM_P (decl);
4558
4559 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4560 TEMPLATE_PARM_LEVEL (index) - levels,
4561 TEMPLATE_PARM_ORIG_LEVEL (index),
4562 decl, type);
4563 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4564 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4565 = TEMPLATE_PARM_PARAMETER_PACK (index);
4566
4567 /* Template template parameters need this. */
4568 tree inner = decl;
4569 if (TREE_CODE (decl) == TEMPLATE_DECL)
4570 {
4571 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4572 TYPE_DECL, DECL_NAME (decl), type);
4573 DECL_TEMPLATE_RESULT (decl) = inner;
4574 DECL_ARTIFICIAL (inner) = true;
4575 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4576 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4577 }
4578
4579 /* Attach the TPI to the decl. */
4580 if (TREE_CODE (inner) == TYPE_DECL)
4581 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4582 else
4583 DECL_INITIAL (decl) = tpi;
4584 }
4585
4586 return TEMPLATE_PARM_DESCENDANTS (index);
4587 }
4588
4589 /* Process information from new template parameter PARM and append it
4590 to the LIST being built. This new parameter is a non-type
4591 parameter iff IS_NON_TYPE is true. This new parameter is a
4592 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4593 is in PARM_LOC. */
4594
4595 tree
4596 process_template_parm (tree list, location_t parm_loc, tree parm,
4597 bool is_non_type, bool is_parameter_pack)
4598 {
4599 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4600 tree prev = NULL_TREE;
4601 int idx = 0;
4602
4603 if (list)
4604 {
4605 prev = tree_last (list);
4606
4607 tree p = TREE_VALUE (prev);
4608 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4609 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4610 else if (TREE_CODE (p) == PARM_DECL)
4611 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4612
4613 ++idx;
4614 }
4615
4616 tree decl = NULL_TREE;
4617 tree defval = TREE_PURPOSE (parm);
4618 tree constr = TREE_TYPE (parm);
4619
4620 if (is_non_type)
4621 {
4622 parm = TREE_VALUE (parm);
4623
4624 SET_DECL_TEMPLATE_PARM_P (parm);
4625
4626 if (TREE_TYPE (parm) != error_mark_node)
4627 {
4628 /* [temp.param]
4629
4630 The top-level cv-qualifiers on the template-parameter are
4631 ignored when determining its type. */
4632 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4633 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4634 TREE_TYPE (parm) = error_mark_node;
4635 else if (uses_parameter_packs (TREE_TYPE (parm))
4636 && !is_parameter_pack
4637 /* If we're in a nested template parameter list, the template
4638 template parameter could be a parameter pack. */
4639 && processing_template_parmlist == 1)
4640 {
4641 /* This template parameter is not a parameter pack, but it
4642 should be. Complain about "bare" parameter packs. */
4643 check_for_bare_parameter_packs (TREE_TYPE (parm));
4644
4645 /* Recover by calling this a parameter pack. */
4646 is_parameter_pack = true;
4647 }
4648 }
4649
4650 /* A template parameter is not modifiable. */
4651 TREE_CONSTANT (parm) = 1;
4652 TREE_READONLY (parm) = 1;
4653 decl = build_decl (parm_loc,
4654 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4655 TREE_CONSTANT (decl) = 1;
4656 TREE_READONLY (decl) = 1;
4657 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4658 = build_template_parm_index (idx, current_template_depth,
4659 current_template_depth,
4660 decl, TREE_TYPE (parm));
4661
4662 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4663 = is_parameter_pack;
4664 }
4665 else
4666 {
4667 tree t;
4668 parm = TREE_VALUE (TREE_VALUE (parm));
4669
4670 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4671 {
4672 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4673 /* This is for distinguishing between real templates and template
4674 template parameters */
4675 TREE_TYPE (parm) = t;
4676
4677 /* any_template_parm_r expects to be able to get the targs of a
4678 DECL_TEMPLATE_RESULT. */
4679 tree result = DECL_TEMPLATE_RESULT (parm);
4680 TREE_TYPE (result) = t;
4681 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4682 tree tinfo = build_template_info (parm, args);
4683 retrofit_lang_decl (result);
4684 DECL_TEMPLATE_INFO (result) = tinfo;
4685
4686 decl = parm;
4687 }
4688 else
4689 {
4690 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4691 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4692 decl = build_decl (parm_loc,
4693 TYPE_DECL, parm, t);
4694 }
4695
4696 TYPE_NAME (t) = decl;
4697 TYPE_STUB_DECL (t) = decl;
4698 parm = decl;
4699 TEMPLATE_TYPE_PARM_INDEX (t)
4700 = build_template_parm_index (idx, current_template_depth,
4701 current_template_depth,
4702 decl, TREE_TYPE (parm));
4703 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4704 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4705 }
4706 DECL_ARTIFICIAL (decl) = 1;
4707 SET_DECL_TEMPLATE_PARM_P (decl);
4708
4709 /* Build requirements for the type/template parameter.
4710 This must be done after SET_DECL_TEMPLATE_PARM_P or
4711 process_template_parm could fail. */
4712 tree reqs = finish_shorthand_constraint (parm, constr);
4713
4714 decl = pushdecl (decl);
4715 if (!is_non_type)
4716 parm = decl;
4717
4718 /* Build the parameter node linking the parameter declaration,
4719 its default argument (if any), and its constraints (if any). */
4720 parm = build_tree_list (defval, parm);
4721 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4722
4723 if (prev)
4724 TREE_CHAIN (prev) = parm;
4725 else
4726 list = parm;
4727
4728 return list;
4729 }
4730
4731 /* The end of a template parameter list has been reached. Process the
4732 tree list into a parameter vector, converting each parameter into a more
4733 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4734 as PARM_DECLs. */
4735
4736 tree
4737 end_template_parm_list (tree parms)
4738 {
4739 tree saved_parmlist = make_tree_vec (list_length (parms));
4740
4741 /* Pop the dummy parameter level and add the real one. We do not
4742 morph the dummy parameter in place, as it might have been
4743 captured by a (nested) template-template-parm. */
4744 current_template_parms = TREE_CHAIN (current_template_parms);
4745
4746 current_template_parms
4747 = tree_cons (size_int (current_template_depth + 1),
4748 saved_parmlist, current_template_parms);
4749
4750 for (unsigned ix = 0; parms; ix++)
4751 {
4752 tree parm = parms;
4753 parms = TREE_CHAIN (parms);
4754 TREE_CHAIN (parm) = NULL_TREE;
4755
4756 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4757 }
4758
4759 --processing_template_parmlist;
4760
4761 return saved_parmlist;
4762 }
4763
4764 // Explicitly indicate the end of the template parameter list. We assume
4765 // that the current template parameters have been constructed and/or
4766 // managed explicitly, as when creating new template template parameters
4767 // from a shorthand constraint.
4768 void
4769 end_template_parm_list ()
4770 {
4771 --processing_template_parmlist;
4772 }
4773
4774 /* end_template_decl is called after a template declaration is seen. */
4775
4776 void
4777 end_template_decl (void)
4778 {
4779 reset_specialization ();
4780
4781 if (! processing_template_decl)
4782 return;
4783
4784 /* This matches the pushlevel in begin_template_parm_list. */
4785 finish_scope ();
4786
4787 --processing_template_decl;
4788 current_template_parms = TREE_CHAIN (current_template_parms);
4789 }
4790
4791 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4792 thereof, and converts it into an argument suitable to be passed to
4793 the type substitution functions. Note that if the TREE_LIST contains
4794 an error_mark node, the returned argument is error_mark_node. */
4795
4796 tree
4797 template_parm_to_arg (tree t)
4798 {
4799 if (!t)
4800 return NULL_TREE;
4801
4802 if (TREE_CODE (t) == TREE_LIST)
4803 t = TREE_VALUE (t);
4804
4805 if (error_operand_p (t))
4806 return error_mark_node;
4807
4808 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4809 {
4810 if (TREE_CODE (t) == TYPE_DECL
4811 || TREE_CODE (t) == TEMPLATE_DECL)
4812 t = TREE_TYPE (t);
4813 else
4814 t = DECL_INITIAL (t);
4815 }
4816
4817 gcc_assert (TEMPLATE_PARM_P (t));
4818
4819 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4820 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4821 {
4822 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4823 {
4824 /* Turn this argument into a TYPE_ARGUMENT_PACK
4825 with a single element, which expands T. */
4826 tree vec = make_tree_vec (1);
4827 if (CHECKING_P)
4828 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4829
4830 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4831
4832 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4833 ARGUMENT_PACK_ARGS (t) = vec;
4834 }
4835 }
4836 else
4837 {
4838 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4839 {
4840 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4841 with a single element, which expands T. */
4842 tree vec = make_tree_vec (1);
4843 if (CHECKING_P)
4844 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4845
4846 t = convert_from_reference (t);
4847 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4848
4849 t = make_node (NONTYPE_ARGUMENT_PACK);
4850 ARGUMENT_PACK_ARGS (t) = vec;
4851 }
4852 else
4853 t = convert_from_reference (t);
4854 }
4855 return t;
4856 }
4857
4858 /* If T looks like a generic template argument produced by template_parm_to_arg,
4859 return the corresponding template parameter, otherwise return NULL_TREE. */
4860
4861 static tree
4862 template_arg_to_parm (tree t)
4863 {
4864 if (t == NULL_TREE)
4865 return NULL_TREE;
4866
4867 if (ARGUMENT_PACK_P (t))
4868 {
4869 tree args = ARGUMENT_PACK_ARGS (t);
4870 if (TREE_VEC_LENGTH (args) == 1
4871 && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
4872 t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
4873 }
4874
4875 if (REFERENCE_REF_P (t))
4876 t = TREE_OPERAND (t, 0);
4877
4878 if (TEMPLATE_PARM_P (t))
4879 return t;
4880 else
4881 return NULL_TREE;
4882 }
4883
4884 /* Given a single level of template parameters (a TREE_VEC), return it
4885 as a set of template arguments. */
4886
4887 tree
4888 template_parms_level_to_args (tree parms)
4889 {
4890 parms = copy_node (parms);
4891 TREE_TYPE (parms) = NULL_TREE;
4892 for (tree& parm : tree_vec_range (parms))
4893 parm = template_parm_to_arg (parm);
4894
4895 if (CHECKING_P)
4896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
4897
4898 return parms;
4899 }
4900
4901 /* Given a set of template parameters, return them as a set of template
4902 arguments. The template parameters are represented as a TREE_VEC, in
4903 the form documented in cp-tree.h for template arguments. */
4904
4905 tree
4906 template_parms_to_args (tree parms)
4907 {
4908 tree header;
4909 tree args = NULL_TREE;
4910 int length = TMPL_PARMS_DEPTH (parms);
4911 int l = length;
4912
4913 /* If there is only one level of template parameters, we do not
4914 create a TREE_VEC of TREE_VECs. Instead, we return a single
4915 TREE_VEC containing the arguments. */
4916 if (length > 1)
4917 args = make_tree_vec (length);
4918
4919 for (header = parms; header; header = TREE_CHAIN (header))
4920 {
4921 tree a = template_parms_level_to_args (TREE_VALUE (header));
4922
4923 if (length > 1)
4924 TREE_VEC_ELT (args, --l) = a;
4925 else
4926 args = a;
4927 }
4928
4929 return args;
4930 }
4931
4932 /* Within the declaration of a template, return the currently active
4933 template parameters as an argument TREE_VEC. */
4934
4935 static tree
4936 current_template_args (void)
4937 {
4938 return template_parms_to_args (current_template_parms);
4939 }
4940
4941 /* Return the fully generic arguments for of TMPL, i.e. what
4942 current_template_args would be while parsing it. */
4943
4944 tree
4945 generic_targs_for (tree tmpl)
4946 {
4947 if (tmpl == NULL_TREE)
4948 return NULL_TREE;
4949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4950 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4951 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4952 template parameter, it has no TEMPLATE_INFO; for a partial
4953 specialization, it has the arguments for the primary template, and we
4954 want the arguments for the partial specialization. */;
4955 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4956 if (tree ti = get_template_info (result))
4957 return TI_ARGS (ti);
4958 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4959 }
4960
4961 /* Return the template arguments corresponding to the template parameters of
4962 TMPL's enclosing scope. When TMPL is a member of a partial specialization,
4963 this returns the arguments for the partial specialization as opposed to those
4964 for the primary template, which is the main difference between this function
4965 and simply using e.g. the TYPE_TI_ARGS of TMPL's DECL_CONTEXT. */
4966
4967 tree
4968 outer_template_args (tree tmpl)
4969 {
4970 tree ti = get_template_info (DECL_TEMPLATE_RESULT (tmpl));
4971 if (!ti)
4972 return NULL_TREE;
4973 tree args = TI_ARGS (ti);
4974 if (!PRIMARY_TEMPLATE_P (tmpl))
4975 return args;
4976 if (TMPL_ARGS_DEPTH (args) == 1)
4977 return NULL_TREE;
4978 args = copy_node (args);
4979 --TREE_VEC_LENGTH (args);
4980 return args;
4981 }
4982
4983 /* Update the declared TYPE by doing any lookups which were thought to be
4984 dependent, but are not now that we know the SCOPE of the declarator. */
4985
4986 tree
4987 maybe_update_decl_type (tree orig_type, tree scope)
4988 {
4989 tree type = orig_type;
4990
4991 if (type == NULL_TREE)
4992 return type;
4993
4994 if (TREE_CODE (orig_type) == TYPE_DECL)
4995 type = TREE_TYPE (type);
4996
4997 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4998 && dependent_type_p (type)
4999 /* Don't bother building up the args in this case. */
5000 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
5001 {
5002 /* tsubst in the args corresponding to the template parameters,
5003 including auto if present. Most things will be unchanged, but
5004 make_typename_type and tsubst_qualified_id will resolve
5005 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
5006 tree args = current_template_args ();
5007 tree auto_node = type_uses_auto (type);
5008 tree pushed;
5009 if (auto_node)
5010 {
5011 tree auto_vec = make_tree_vec (1);
5012 TREE_VEC_ELT (auto_vec, 0) = auto_node;
5013 args = add_to_template_args (args, auto_vec);
5014 }
5015 pushed = push_scope (scope);
5016 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
5017 if (pushed)
5018 pop_scope (scope);
5019 }
5020
5021 if (type == error_mark_node)
5022 return orig_type;
5023
5024 if (TREE_CODE (orig_type) == TYPE_DECL)
5025 {
5026 if (same_type_p (type, TREE_TYPE (orig_type)))
5027 type = orig_type;
5028 else
5029 type = TYPE_NAME (type);
5030 }
5031 return type;
5032 }
5033
5034 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
5035 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
5036 the new template is a member template. */
5037
5038 static tree
5039 build_template_decl (tree decl, tree parms, bool member_template_p)
5040 {
5041 gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
5042
5043 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
5044 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
5045 DECL_TEMPLATE_PARMS (tmpl) = parms;
5046 DECL_TEMPLATE_RESULT (tmpl) = decl;
5047 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
5048 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5049 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
5050 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
5051
5052 /* Propagate module information from the decl. */
5053 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
5054
5055 return tmpl;
5056 }
5057
5058 struct template_parm_data
5059 {
5060 /* The level of the template parameters we are currently
5061 processing. */
5062 int level;
5063
5064 /* The index of the specialization argument we are currently
5065 processing. */
5066 int current_arg;
5067
5068 /* An array whose size is the number of template parameters. The
5069 elements are nonzero if the parameter has been used in any one
5070 of the arguments processed so far. */
5071 int* parms;
5072
5073 /* An array whose size is the number of template arguments. The
5074 elements are nonzero if the argument makes use of template
5075 parameters of this level. */
5076 int* arg_uses_template_parms;
5077 };
5078
5079 /* Subroutine of push_template_decl used to see if each template
5080 parameter in a partial specialization is used in the explicit
5081 argument list. If T is of the LEVEL given in DATA (which is
5082 treated as a template_parm_data*), then DATA->PARMS is marked
5083 appropriately. */
5084
5085 static int
5086 mark_template_parm (tree t, void* data)
5087 {
5088 int level;
5089 int idx;
5090 struct template_parm_data* tpd = (struct template_parm_data*) data;
5091
5092 template_parm_level_and_index (t, &level, &idx);
5093
5094 if (level == tpd->level)
5095 {
5096 tpd->parms[idx] = 1;
5097 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
5098 }
5099
5100 /* In C++17 the type of a non-type argument is a deduced context. */
5101 if (cxx_dialect >= cxx17
5102 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5103 for_each_template_parm (TREE_TYPE (t),
5104 &mark_template_parm,
5105 data,
5106 NULL,
5107 /*include_nondeduced_p=*/false);
5108
5109 /* Return zero so that for_each_template_parm will continue the
5110 traversal of the tree; we want to mark *every* template parm. */
5111 return 0;
5112 }
5113
5114 /* Process the partial specialization DECL. */
5115
5116 static tree
5117 process_partial_specialization (tree decl)
5118 {
5119 tree type = TREE_TYPE (decl);
5120 tree tinfo = get_template_info (decl);
5121 tree maintmpl = TI_TEMPLATE (tinfo);
5122 tree specargs = TI_ARGS (tinfo);
5123 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5124 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5125 tree inner_parms;
5126 tree inst;
5127 int nargs = TREE_VEC_LENGTH (inner_args);
5128 int ntparms;
5129 int i;
5130 bool did_error_intro = false;
5131 struct template_parm_data tpd;
5132 struct template_parm_data tpd2;
5133
5134 gcc_assert (current_template_parms);
5135
5136 /* A concept cannot be specialized. */
5137 if (flag_concepts && variable_concept_p (maintmpl))
5138 {
5139 error ("specialization of variable concept %q#D", maintmpl);
5140 return error_mark_node;
5141 }
5142
5143 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5144 ntparms = TREE_VEC_LENGTH (inner_parms);
5145
5146 /* We check that each of the template parameters given in the
5147 partial specialization is used in the argument list to the
5148 specialization. For example:
5149
5150 template <class T> struct S;
5151 template <class T> struct S<T*>;
5152
5153 The second declaration is OK because `T*' uses the template
5154 parameter T, whereas
5155
5156 template <class T> struct S<int>;
5157
5158 is no good. Even trickier is:
5159
5160 template <class T>
5161 struct S1
5162 {
5163 template <class U>
5164 struct S2;
5165 template <class U>
5166 struct S2<T>;
5167 };
5168
5169 The S2<T> declaration is actually invalid; it is a
5170 full-specialization. Of course,
5171
5172 template <class U>
5173 struct S2<T (*)(U)>;
5174
5175 or some such would have been OK. */
5176 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5177 tpd.parms = XALLOCAVEC (int, ntparms);
5178 memset (tpd.parms, 0, sizeof (int) * ntparms);
5179
5180 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5181 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5182 for (i = 0; i < nargs; ++i)
5183 {
5184 tpd.current_arg = i;
5185 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5186 &mark_template_parm,
5187 &tpd,
5188 NULL,
5189 /*include_nondeduced_p=*/false);
5190 }
5191 for (i = 0; i < ntparms; ++i)
5192 if (tpd.parms[i] == 0)
5193 {
5194 /* One of the template parms was not used in a deduced context in the
5195 specialization. */
5196 if (!did_error_intro)
5197 {
5198 error ("template parameters not deducible in "
5199 "partial specialization:");
5200 did_error_intro = true;
5201 }
5202
5203 inform (input_location, " %qD",
5204 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5205 }
5206
5207 if (did_error_intro)
5208 return error_mark_node;
5209
5210 /* [temp.class.spec]
5211
5212 The argument list of the specialization shall not be identical to
5213 the implicit argument list of the primary template. */
5214 tree main_args
5215 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5216 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5217 && (!flag_concepts
5218 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5219 {
5220 if (!flag_concepts)
5221 error ("partial specialization %q+D does not specialize "
5222 "any template arguments; to define the primary template, "
5223 "remove the template argument list", decl);
5224 else
5225 error ("partial specialization %q+D does not specialize any "
5226 "template arguments and is not more constrained than "
5227 "the primary template; to define the primary template, "
5228 "remove the template argument list", decl);
5229 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5230 }
5231
5232 /* A partial specialization that replaces multiple parameters of the
5233 primary template with a pack expansion is less specialized for those
5234 parameters. */
5235 if (nargs < DECL_NTPARMS (maintmpl))
5236 {
5237 error ("partial specialization is not more specialized than the "
5238 "primary template because it replaces multiple parameters "
5239 "with a pack expansion");
5240 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5241 /* Avoid crash in process_partial_specialization. */
5242 return decl;
5243 }
5244
5245 else if (nargs > DECL_NTPARMS (maintmpl))
5246 {
5247 error ("too many arguments for partial specialization %qT", type);
5248 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5249 /* Avoid crash below. */
5250 return decl;
5251 }
5252
5253 /* If we aren't in a dependent class, we can actually try deduction. */
5254 else if (tpd.level == 1
5255 /* FIXME we should be able to handle a partial specialization of a
5256 partial instantiation, but currently we can't (c++/41727). */
5257 && TMPL_ARGS_DEPTH (specargs) == 1
5258 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5259 {
5260 auto_diagnostic_group d;
5261 if (pedwarn (input_location, 0,
5262 "partial specialization %qD is not more specialized than",
5263 decl))
5264 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5265 maintmpl);
5266 }
5267
5268 /* [temp.spec.partial]
5269
5270 The type of a template parameter corresponding to a specialized
5271 non-type argument shall not be dependent on a parameter of the
5272 specialization.
5273
5274 Also, we verify that pack expansions only occur at the
5275 end of the argument list. */
5276 tpd2.parms = 0;
5277 for (i = 0; i < nargs; ++i)
5278 {
5279 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5280 tree arg = TREE_VEC_ELT (inner_args, i);
5281 tree packed_args = NULL_TREE;
5282 int j, len = 1;
5283
5284 if (ARGUMENT_PACK_P (arg))
5285 {
5286 /* Extract the arguments from the argument pack. We'll be
5287 iterating over these in the following loop. */
5288 packed_args = ARGUMENT_PACK_ARGS (arg);
5289 len = TREE_VEC_LENGTH (packed_args);
5290 }
5291
5292 for (j = 0; j < len; j++)
5293 {
5294 if (packed_args)
5295 /* Get the Jth argument in the parameter pack. */
5296 arg = TREE_VEC_ELT (packed_args, j);
5297
5298 if (PACK_EXPANSION_P (arg))
5299 {
5300 /* Pack expansions must come at the end of the
5301 argument list. */
5302 if ((packed_args && j < len - 1)
5303 || (!packed_args && i < nargs - 1))
5304 {
5305 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5306 error ("parameter pack argument %qE must be at the "
5307 "end of the template argument list", arg);
5308 else
5309 error ("parameter pack argument %qT must be at the "
5310 "end of the template argument list", arg);
5311 }
5312 }
5313
5314 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5315 /* We only care about the pattern. */
5316 arg = PACK_EXPANSION_PATTERN (arg);
5317
5318 if (/* These first two lines are the `non-type' bit. */
5319 !TYPE_P (arg)
5320 && TREE_CODE (arg) != TEMPLATE_DECL
5321 /* This next two lines are the `argument expression is not just a
5322 simple identifier' condition and also the `specialized
5323 non-type argument' bit. */
5324 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5325 && !((REFERENCE_REF_P (arg)
5326 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5327 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5328 {
5329 /* Look at the corresponding template parameter,
5330 marking which template parameters its type depends
5331 upon. */
5332 tree type = TREE_TYPE (parm);
5333
5334 if (!tpd2.parms)
5335 {
5336 /* We haven't yet initialized TPD2. Do so now. */
5337 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5338 /* The number of parameters here is the number in the
5339 main template, which, as checked in the assertion
5340 above, is NARGS. */
5341 tpd2.parms = XALLOCAVEC (int, nargs);
5342 tpd2.level =
5343 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5344 }
5345
5346 /* Mark the template parameters. But this time, we're
5347 looking for the template parameters of the main
5348 template, not in the specialization. */
5349 tpd2.current_arg = i;
5350 tpd2.arg_uses_template_parms[i] = 0;
5351 memset (tpd2.parms, 0, sizeof (int) * nargs);
5352 for_each_template_parm (type,
5353 &mark_template_parm,
5354 &tpd2,
5355 NULL,
5356 /*include_nondeduced_p=*/false);
5357
5358 if (tpd2.arg_uses_template_parms [i])
5359 {
5360 /* The type depended on some template parameters.
5361 If they are fully specialized in the
5362 specialization, that's OK. */
5363 int j;
5364 int count = 0;
5365 for (j = 0; j < nargs; ++j)
5366 if (tpd2.parms[j] != 0
5367 && tpd.arg_uses_template_parms [j])
5368 ++count;
5369 if (count != 0)
5370 error_n (input_location, count,
5371 "type %qT of template argument %qE depends "
5372 "on a template parameter",
5373 "type %qT of template argument %qE depends "
5374 "on template parameters",
5375 type,
5376 arg);
5377 }
5378 }
5379 }
5380 }
5381
5382 /* We should only get here once. */
5383 if (TREE_CODE (decl) == TYPE_DECL)
5384 gcc_assert (!COMPLETE_TYPE_P (type));
5385
5386 // Build the template decl.
5387 tree tmpl = build_template_decl (decl, current_template_parms,
5388 DECL_MEMBER_TEMPLATE_P (maintmpl));
5389 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5390 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5391 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5392
5393 /* Give template template parms a DECL_CONTEXT of the template
5394 for which they are a parameter. */
5395 for (i = 0; i < ntparms; ++i)
5396 {
5397 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5398 if (TREE_CODE (parm) == TEMPLATE_DECL)
5399 DECL_CONTEXT (parm) = tmpl;
5400 }
5401
5402 if (VAR_P (decl))
5403 /* We didn't register this in check_explicit_specialization so we could
5404 wait until the constraints were set. */
5405 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5406 else
5407 associate_classtype_constraints (type);
5408
5409 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5410 = tree_cons (specargs, tmpl,
5411 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5412 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5413
5414 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5415 inst = TREE_CHAIN (inst))
5416 {
5417 tree instance = TREE_VALUE (inst);
5418 if (TYPE_P (instance)
5419 ? (COMPLETE_TYPE_P (instance)
5420 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5421 : DECL_TEMPLATE_INSTANTIATION (instance))
5422 {
5423 tree spec = most_specialized_partial_spec (instance, tf_none);
5424 tree inst_decl = (DECL_P (instance)
5425 ? instance : TYPE_NAME (instance));
5426 if (!spec)
5427 /* OK */;
5428 else if (spec == error_mark_node)
5429 permerror (input_location,
5430 "declaration of %qD ambiguates earlier template "
5431 "instantiation for %qD", decl, inst_decl);
5432 else if (TREE_VALUE (spec) == tmpl)
5433 permerror (input_location,
5434 "partial specialization of %qD after instantiation "
5435 "of %qD", decl, inst_decl);
5436 }
5437 }
5438
5439 return decl;
5440 }
5441
5442 /* PARM is a template parameter of some form; return the corresponding
5443 TEMPLATE_PARM_INDEX. */
5444
5445 static tree
5446 get_template_parm_index (tree parm)
5447 {
5448 if (TREE_CODE (parm) == PARM_DECL
5449 || TREE_CODE (parm) == CONST_DECL)
5450 parm = DECL_INITIAL (parm);
5451 else if (TREE_CODE (parm) == TYPE_DECL
5452 || TREE_CODE (parm) == TEMPLATE_DECL)
5453 parm = TREE_TYPE (parm);
5454 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5455 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5456 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5457 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5458 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5459 return parm;
5460 }
5461
5462 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5463 parameter packs used by the template parameter PARM. */
5464
5465 static void
5466 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5467 {
5468 /* A type parm can't refer to another parm. */
5469 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5470 return;
5471 else if (TREE_CODE (parm) == PARM_DECL)
5472 {
5473 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5474 ppd, ppd->visited);
5475 return;
5476 }
5477
5478 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5479
5480 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5481 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5482 {
5483 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5484 if (template_parameter_pack_p (p))
5485 /* Any packs in the type are expanded by this parameter. */;
5486 else
5487 fixed_parameter_pack_p_1 (p, ppd);
5488 }
5489 }
5490
5491 /* PARM is a template parameter pack. Return any parameter packs used in
5492 its type or the type of any of its template parameters. If there are
5493 any such packs, it will be instantiated into a fixed template parameter
5494 list by partial instantiation rather than be fully deduced. */
5495
5496 tree
5497 fixed_parameter_pack_p (tree parm)
5498 {
5499 /* This can only be true in a member template. */
5500 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5501 return NULL_TREE;
5502 /* This can only be true for a parameter pack. */
5503 if (!template_parameter_pack_p (parm))
5504 return NULL_TREE;
5505 /* A type parm can't refer to another parm. */
5506 if (TREE_CODE (parm) == TYPE_DECL)
5507 return NULL_TREE;
5508
5509 tree parameter_packs = NULL_TREE;
5510 struct find_parameter_pack_data ppd;
5511 ppd.parameter_packs = &parameter_packs;
5512 ppd.visited = new hash_set<tree>;
5513 ppd.type_pack_expansion_p = false;
5514
5515 fixed_parameter_pack_p_1 (parm, &ppd);
5516
5517 delete ppd.visited;
5518 return parameter_packs;
5519 }
5520
5521 /* Check that a template declaration's use of default arguments and
5522 parameter packs is not invalid. Here, PARMS are the template
5523 parameters. IS_PRIMARY is true if DECL is the thing declared by
5524 a primary template. IS_PARTIAL is true if DECL is a partial
5525 specialization.
5526
5527 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5528 function template declaration or a friend class template
5529 declaration. In the function case, 1 indicates a declaration, 2
5530 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5531 emitted for extraneous default arguments.
5532
5533 Returns TRUE if there were no errors found, FALSE otherwise. */
5534
5535 bool
5536 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5537 bool is_partial, int is_friend_decl)
5538 {
5539 const char *msg;
5540 int last_level_to_check;
5541 tree parm_level;
5542 bool no_errors = true;
5543
5544 /* [temp.param]
5545
5546 A default template-argument shall not be specified in a
5547 function template declaration or a function template definition, nor
5548 in the template-parameter-list of the definition of a member of a
5549 class template. */
5550
5551 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5552 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5553 /* You can't have a function template declaration in a local
5554 scope, nor you can you define a member of a class template in a
5555 local scope. */
5556 return true;
5557
5558 if ((TREE_CODE (decl) == TYPE_DECL
5559 && TREE_TYPE (decl)
5560 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5561 || (TREE_CODE (decl) == FUNCTION_DECL
5562 && LAMBDA_FUNCTION_P (decl)))
5563 /* A lambda doesn't have an explicit declaration; don't complain
5564 about the parms of the enclosing class. */
5565 return true;
5566
5567 if (current_class_type
5568 && !TYPE_BEING_DEFINED (current_class_type)
5569 && DECL_LANG_SPECIFIC (decl)
5570 && DECL_DECLARES_FUNCTION_P (decl)
5571 /* If this is either a friend defined in the scope of the class
5572 or a member function. */
5573 && (DECL_FUNCTION_MEMBER_P (decl)
5574 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5575 : DECL_FRIEND_CONTEXT (decl)
5576 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5577 : false)
5578 /* And, if it was a member function, it really was defined in
5579 the scope of the class. */
5580 && (!DECL_FUNCTION_MEMBER_P (decl)
5581 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5582 /* We already checked these parameters when the template was
5583 declared, so there's no need to do it again now. This function
5584 was defined in class scope, but we're processing its body now
5585 that the class is complete. */
5586 return true;
5587
5588 /* Core issue 226 (C++0x only): the following only applies to class
5589 templates. */
5590 if (is_primary
5591 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5592 {
5593 /* [temp.param]
5594
5595 If a template-parameter has a default template-argument, all
5596 subsequent template-parameters shall have a default
5597 template-argument supplied. */
5598 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5599 {
5600 tree inner_parms = TREE_VALUE (parm_level);
5601 int ntparms = TREE_VEC_LENGTH (inner_parms);
5602 int seen_def_arg_p = 0;
5603 int i;
5604
5605 for (i = 0; i < ntparms; ++i)
5606 {
5607 tree parm = TREE_VEC_ELT (inner_parms, i);
5608
5609 if (parm == error_mark_node)
5610 continue;
5611
5612 if (TREE_PURPOSE (parm))
5613 seen_def_arg_p = 1;
5614 else if (seen_def_arg_p
5615 && !template_parameter_pack_p (TREE_VALUE (parm)))
5616 {
5617 error ("no default argument for %qD", TREE_VALUE (parm));
5618 /* For better subsequent error-recovery, we indicate that
5619 there should have been a default argument. */
5620 TREE_PURPOSE (parm) = error_mark_node;
5621 no_errors = false;
5622 }
5623 else if (!is_partial
5624 && !is_friend_decl
5625 /* Don't complain about an enclosing partial
5626 specialization. */
5627 && parm_level == parms
5628 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5629 && i < ntparms - 1
5630 && template_parameter_pack_p (TREE_VALUE (parm))
5631 /* A fixed parameter pack will be partially
5632 instantiated into a fixed length list. */
5633 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5634 {
5635 /* A primary class template, primary variable template
5636 (DR 2032), or alias template can only have one
5637 parameter pack, at the end of the template
5638 parameter list. */
5639
5640 error ("parameter pack %q+D must be at the end of the"
5641 " template parameter list", TREE_VALUE (parm));
5642
5643 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5644 = error_mark_node;
5645 no_errors = false;
5646 }
5647 }
5648 }
5649 }
5650
5651 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5652 || is_partial
5653 || !is_primary
5654 || is_friend_decl)
5655 /* For an ordinary class template, default template arguments are
5656 allowed at the innermost level, e.g.:
5657 template <class T = int>
5658 struct S {};
5659 but, in a partial specialization, they're not allowed even
5660 there, as we have in [temp.class.spec]:
5661
5662 The template parameter list of a specialization shall not
5663 contain default template argument values.
5664
5665 So, for a partial specialization, or for a function template
5666 (in C++98/C++03), we look at all of them. */
5667 ;
5668 else
5669 /* But, for a primary class template that is not a partial
5670 specialization we look at all template parameters except the
5671 innermost ones. */
5672 parms = TREE_CHAIN (parms);
5673
5674 /* Figure out what error message to issue. */
5675 if (is_friend_decl == 2)
5676 msg = G_("default template arguments may not be used in function template "
5677 "friend re-declaration");
5678 else if (is_friend_decl)
5679 msg = G_("default template arguments may not be used in template "
5680 "friend declarations");
5681 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5682 msg = G_("default template arguments may not be used in function templates "
5683 "without %<-std=c++11%> or %<-std=gnu++11%>");
5684 else if (is_partial)
5685 msg = G_("default template arguments may not be used in "
5686 "partial specializations");
5687 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5688 msg = G_("default argument for template parameter for class enclosing %qD");
5689 else
5690 /* Per [temp.param]/9, "A default template-argument shall not be
5691 specified in the template-parameter-lists of the definition of
5692 a member of a class template that appears outside of the member's
5693 class.", thus if we aren't handling a member of a class template
5694 there is no need to examine the parameters. */
5695 return true;
5696
5697 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5698 /* If we're inside a class definition, there's no need to
5699 examine the parameters to the class itself. On the one
5700 hand, they will be checked when the class is defined, and,
5701 on the other, default arguments are valid in things like:
5702 template <class T = double>
5703 struct S { template <class U> void f(U); };
5704 Here the default argument for `S' has no bearing on the
5705 declaration of `f'. */
5706 last_level_to_check = template_class_depth (current_class_type) + 1;
5707 else
5708 /* Check everything. */
5709 last_level_to_check = 0;
5710
5711 for (parm_level = parms;
5712 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5713 parm_level = TREE_CHAIN (parm_level))
5714 {
5715 tree inner_parms = TREE_VALUE (parm_level);
5716 int i;
5717 int ntparms;
5718
5719 ntparms = TREE_VEC_LENGTH (inner_parms);
5720 for (i = 0; i < ntparms; ++i)
5721 {
5722 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5723 continue;
5724
5725 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5726 {
5727 if (msg)
5728 {
5729 no_errors = false;
5730 if (is_friend_decl == 2)
5731 return no_errors;
5732
5733 error (msg, decl);
5734 msg = 0;
5735 }
5736
5737 /* Clear out the default argument so that we are not
5738 confused later. */
5739 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5740 }
5741 }
5742
5743 /* At this point, if we're still interested in issuing messages,
5744 they must apply to classes surrounding the object declared. */
5745 if (msg)
5746 msg = G_("default argument for template parameter for class "
5747 "enclosing %qD");
5748 }
5749
5750 return no_errors;
5751 }
5752
5753 /* Worker for push_template_decl_real, called via
5754 for_each_template_parm. DATA is really an int, indicating the
5755 level of the parameters we are interested in. If T is a template
5756 parameter of that level, return nonzero. */
5757
5758 static int
5759 template_parm_this_level_p (tree t, void* data)
5760 {
5761 int this_level = *(int *)data;
5762 int level;
5763
5764 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5765 level = TEMPLATE_PARM_LEVEL (t);
5766 else
5767 level = TEMPLATE_TYPE_LEVEL (t);
5768 return level == this_level;
5769 }
5770
5771 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5772 DATA is really an int, indicating the innermost outer level of parameters.
5773 If T is a template parameter of that level or further out, return
5774 nonzero. */
5775
5776 static int
5777 template_parm_outer_level (tree t, void *data)
5778 {
5779 int this_level = *(int *)data;
5780 int level;
5781
5782 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5783 level = TEMPLATE_PARM_LEVEL (t);
5784 else
5785 level = TEMPLATE_TYPE_LEVEL (t);
5786 return level <= this_level;
5787 }
5788
5789 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5790 parameters given by current_template_args, or reuses a
5791 previously existing one, if appropriate. Returns the DECL, or an
5792 equivalent one, if it is replaced via a call to duplicate_decls.
5793
5794 If IS_FRIEND is true, DECL is a friend declaration. */
5795
5796 tree
5797 push_template_decl (tree decl, bool is_friend)
5798 {
5799 if (decl == error_mark_node || !current_template_parms)
5800 return error_mark_node;
5801
5802 /* See if this is a partial specialization. */
5803 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5804 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5805 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5806 || (VAR_P (decl)
5807 && DECL_LANG_SPECIFIC (decl)
5808 && DECL_TEMPLATE_SPECIALIZATION (decl)
5809 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5810
5811 /* No surprising friend functions. */
5812 gcc_checking_assert (is_friend
5813 || !(TREE_CODE (decl) == FUNCTION_DECL
5814 && DECL_UNIQUE_FRIEND_P (decl)));
5815
5816 tree ctx;
5817 if (is_friend)
5818 /* For a friend, we want the context of the friend, not
5819 the type of which it is a friend. */
5820 ctx = CP_DECL_CONTEXT (decl);
5821 else if (CP_DECL_CONTEXT (decl)
5822 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5823 /* In the case of a virtual function, we want the class in which
5824 it is defined. */
5825 ctx = CP_DECL_CONTEXT (decl);
5826 else
5827 /* Otherwise, if we're currently defining some class, the DECL
5828 is assumed to be a member of the class. */
5829 ctx = current_scope ();
5830
5831 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5832 ctx = NULL_TREE;
5833
5834 if (!DECL_CONTEXT (decl))
5835 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5836
5837 /* See if this is a primary template. */
5838 bool is_primary = false;
5839 if (is_friend && ctx
5840 && uses_template_parms_level (ctx, current_template_depth))
5841 /* A friend template that specifies a class context, i.e.
5842 template <typename T> friend void A<T>::f();
5843 is not primary. */
5844 ;
5845 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5846 /* Lambdas are not primary. */
5847 ;
5848 else
5849 is_primary = template_parm_scope_p ();
5850
5851 /* True if the template is a member template, in the sense of
5852 [temp.mem]. */
5853 bool member_template_p = false;
5854
5855 if (is_primary)
5856 {
5857 warning (OPT_Wtemplates, "template %qD declared", decl);
5858
5859 if (DECL_CLASS_SCOPE_P (decl))
5860 member_template_p = true;
5861
5862 if (TREE_CODE (decl) == TYPE_DECL
5863 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5864 {
5865 error ("template class without a name");
5866 return error_mark_node;
5867 }
5868 else if (TREE_CODE (decl) == FUNCTION_DECL)
5869 {
5870 if (member_template_p)
5871 {
5872 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5873 error ("member template %qD may not have virt-specifiers", decl);
5874 }
5875 if (DECL_DESTRUCTOR_P (decl))
5876 {
5877 /* [temp.mem]
5878
5879 A destructor shall not be a member template. */
5880 error_at (DECL_SOURCE_LOCATION (decl),
5881 "destructor %qD declared as member template", decl);
5882 return error_mark_node;
5883 }
5884 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5885 && (!prototype_p (TREE_TYPE (decl))
5886 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5887 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5888 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5889 == void_list_node)))
5890 {
5891 /* [basic.stc.dynamic.allocation]
5892
5893 An allocation function can be a function
5894 template. ... Template allocation functions shall
5895 have two or more parameters. */
5896 error ("invalid template declaration of %qD", decl);
5897 return error_mark_node;
5898 }
5899 }
5900 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5901 && CLASS_TYPE_P (TREE_TYPE (decl)))
5902 /* Class template. */;
5903 else if (TREE_CODE (decl) == TYPE_DECL
5904 && TYPE_DECL_ALIAS_P (decl))
5905 /* alias-declaration */
5906 gcc_assert (!DECL_ARTIFICIAL (decl));
5907 else if (VAR_P (decl))
5908 /* C++14 variable template. */;
5909 else if (TREE_CODE (decl) == CONCEPT_DECL)
5910 /* C++20 concept definitions. */;
5911 else
5912 {
5913 error ("template declaration of %q#D", decl);
5914 return error_mark_node;
5915 }
5916 }
5917
5918 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5919 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5920 || (VAR_OR_FUNCTION_DECL_P (decl)
5921 && DECL_LOCAL_DECL_P (decl))));
5922
5923 /* Check to see that the rules regarding the use of default
5924 arguments are not being violated. We check args for a friend
5925 functions when we know whether it's a definition, introducing
5926 declaration or re-declaration. */
5927 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5928 check_default_tmpl_args (decl, current_template_parms,
5929 is_primary, is_partial, is_friend);
5930
5931 /* Ensure that there are no parameter packs in the type of this
5932 declaration that have not been expanded. */
5933 if (TREE_CODE (decl) == FUNCTION_DECL)
5934 {
5935 /* Check each of the arguments individually to see if there are
5936 any bare parameter packs. */
5937 tree type = TREE_TYPE (decl);
5938 tree arg = DECL_ARGUMENTS (decl);
5939 tree argtype = TYPE_ARG_TYPES (type);
5940
5941 while (arg && argtype)
5942 {
5943 if (!DECL_PACK_P (arg)
5944 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5945 {
5946 /* This is a PARM_DECL that contains unexpanded parameter
5947 packs. We have already complained about this in the
5948 check_for_bare_parameter_packs call, so just replace
5949 these types with ERROR_MARK_NODE. */
5950 TREE_TYPE (arg) = error_mark_node;
5951 TREE_VALUE (argtype) = error_mark_node;
5952 }
5953
5954 arg = DECL_CHAIN (arg);
5955 argtype = TREE_CHAIN (argtype);
5956 }
5957
5958 /* Check for bare parameter packs in the return type and the
5959 exception specifiers. */
5960 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5961 /* Errors were already issued, set return type to int
5962 as the frontend doesn't expect error_mark_node as
5963 the return type. */
5964 TREE_TYPE (type) = integer_type_node;
5965 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5966 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5967 }
5968 else
5969 {
5970 if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5971 ? DECL_ORIGINAL_TYPE (decl)
5972 : TREE_TYPE (decl)))
5973 {
5974 TREE_TYPE (decl) = error_mark_node;
5975 return error_mark_node;
5976 }
5977
5978 if (is_partial && VAR_P (decl)
5979 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
5980 return error_mark_node;
5981 }
5982
5983 if (is_partial)
5984 return process_partial_specialization (decl);
5985
5986 tree args = current_template_args ();
5987 tree tmpl = NULL_TREE;
5988 bool new_template_p = false;
5989 if (local_p)
5990 {
5991 /* Does not get a template head. */
5992 tmpl = NULL_TREE;
5993 gcc_checking_assert (!is_primary);
5994 }
5995 else if (!ctx
5996 || TREE_CODE (ctx) == FUNCTION_DECL
5997 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5998 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5999 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
6000 && DECL_TEMPLATE_INFO (decl))))
6001 {
6002 if (DECL_LANG_SPECIFIC (decl)
6003 && DECL_TEMPLATE_INFO (decl)
6004 && DECL_TI_TEMPLATE (decl))
6005 tmpl = DECL_TI_TEMPLATE (decl);
6006 /* If DECL is a TYPE_DECL for a class-template, then there won't
6007 be DECL_LANG_SPECIFIC. The information equivalent to
6008 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
6009 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
6010 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
6011 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
6012 {
6013 /* Since a template declaration already existed for this
6014 class-type, we must be redeclaring it here. Make sure
6015 that the redeclaration is valid. */
6016 redeclare_class_template (TREE_TYPE (decl),
6017 current_template_parms,
6018 current_template_constraints ());
6019 /* We don't need to create a new TEMPLATE_DECL; just use the
6020 one we already had. */
6021 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6022 }
6023 else
6024 {
6025 tmpl = build_template_decl (decl, current_template_parms,
6026 member_template_p);
6027 new_template_p = true;
6028
6029 if (DECL_LANG_SPECIFIC (decl)
6030 && DECL_TEMPLATE_SPECIALIZATION (decl))
6031 {
6032 /* A specialization of a member template of a template
6033 class. */
6034 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
6035 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
6036 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
6037 }
6038 }
6039 }
6040 else
6041 {
6042 tree a, t, current, parms;
6043 int i;
6044 tree tinfo = get_template_info (decl);
6045
6046 if (!tinfo)
6047 {
6048 error ("template definition of non-template %q#D", decl);
6049 return error_mark_node;
6050 }
6051
6052 tmpl = TI_TEMPLATE (tinfo);
6053
6054 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
6055 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
6056 && DECL_TEMPLATE_SPECIALIZATION (decl)
6057 && DECL_MEMBER_TEMPLATE_P (tmpl))
6058 {
6059 /* The declaration is a specialization of a member
6060 template, declared outside the class. Therefore, the
6061 innermost template arguments will be NULL, so we
6062 replace them with the arguments determined by the
6063 earlier call to check_explicit_specialization. */
6064 args = DECL_TI_ARGS (decl);
6065
6066 tree new_tmpl
6067 = build_template_decl (decl, current_template_parms,
6068 member_template_p);
6069 DECL_TI_TEMPLATE (decl) = new_tmpl;
6070 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
6071 DECL_TEMPLATE_INFO (new_tmpl)
6072 = build_template_info (tmpl, args);
6073
6074 register_specialization (new_tmpl,
6075 most_general_template (tmpl),
6076 args,
6077 is_friend, 0);
6078 return decl;
6079 }
6080
6081 /* Make sure the template headers we got make sense. */
6082
6083 parms = DECL_TEMPLATE_PARMS (tmpl);
6084 i = TMPL_PARMS_DEPTH (parms);
6085 if (TMPL_ARGS_DEPTH (args) != i)
6086 {
6087 error ("expected %d levels of template parms for %q#D, got %d",
6088 i, decl, TMPL_ARGS_DEPTH (args));
6089 DECL_INTERFACE_KNOWN (decl) = 1;
6090 return error_mark_node;
6091 }
6092 else
6093 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
6094 {
6095 a = TMPL_ARGS_LEVEL (args, i);
6096 t = INNERMOST_TEMPLATE_PARMS (parms);
6097
6098 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6099 {
6100 if (current == decl)
6101 error ("got %d template parameters for %q#D",
6102 TREE_VEC_LENGTH (a), decl);
6103 else
6104 error ("got %d template parameters for %q#T",
6105 TREE_VEC_LENGTH (a), current);
6106 error (" but %d required", TREE_VEC_LENGTH (t));
6107 /* Avoid crash in import_export_decl. */
6108 DECL_INTERFACE_KNOWN (decl) = 1;
6109 return error_mark_node;
6110 }
6111
6112 if (current == decl)
6113 current = ctx;
6114 else if (current == NULL_TREE)
6115 /* Can happen in erroneous input. */
6116 break;
6117 else
6118 current = get_containing_scope (current);
6119 }
6120
6121 /* Check that the parms are used in the appropriate qualifying scopes
6122 in the declarator. */
6123 if (!comp_template_args
6124 (TI_ARGS (tinfo),
6125 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6126 {
6127 error ("template arguments to %qD do not match original "
6128 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6129 if (!uses_template_parms (TI_ARGS (tinfo)))
6130 inform (input_location, "use %<template<>%> for"
6131 " an explicit specialization");
6132 /* Avoid crash in import_export_decl. */
6133 DECL_INTERFACE_KNOWN (decl) = 1;
6134 return error_mark_node;
6135 }
6136 }
6137
6138 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6139
6140 if (new_template_p)
6141 {
6142 /* Push template declarations for global functions and types.
6143 Note that we do not try to push a global template friend
6144 declared in a template class; such a thing may well depend on
6145 the template parameters of the class and we'll push it when
6146 instantiating the befriending class. */
6147 if (!ctx
6148 && !(is_friend && template_class_depth (current_class_type) > 0))
6149 {
6150 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6151 if (pushed == error_mark_node)
6152 return error_mark_node;
6153
6154 /* pushdecl may have found an existing template. */
6155 if (pushed != tmpl)
6156 {
6157 decl = DECL_TEMPLATE_RESULT (pushed);
6158 tmpl = NULL_TREE;
6159 }
6160 }
6161 else if (is_friend)
6162 {
6163 /* Record this decl as belonging to the current class. It's
6164 not chained onto anything else. */
6165 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6166 gcc_checking_assert (!DECL_CHAIN (tmpl));
6167 DECL_CHAIN (tmpl) = current_scope ();
6168 }
6169 }
6170 else if (tmpl)
6171 /* The type may have been completed, or (erroneously) changed. */
6172 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6173
6174 if (tmpl)
6175 {
6176 if (is_primary)
6177 {
6178 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6179
6180 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6181
6182 /* Give template template parms a DECL_CONTEXT of the template
6183 for which they are a parameter. */
6184 parms = INNERMOST_TEMPLATE_PARMS (parms);
6185 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6186 {
6187 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6188 if (TREE_CODE (parm) == TEMPLATE_DECL)
6189 DECL_CONTEXT (parm) = tmpl;
6190 }
6191
6192 if (TREE_CODE (decl) == TYPE_DECL
6193 && TYPE_DECL_ALIAS_P (decl))
6194 {
6195 if (tree constr
6196 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6197 {
6198 /* ??? Why don't we do this here for all templates? */
6199 constr = build_constraints (constr, NULL_TREE);
6200 set_constraints (decl, constr);
6201 }
6202 if (complex_alias_template_p (tmpl))
6203 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6204 }
6205 }
6206
6207 /* The DECL_TI_ARGS of DECL contains full set of arguments
6208 referring wback to its most general template. If TMPL is a
6209 specialization, ARGS may only have the innermost set of
6210 arguments. Add the missing argument levels if necessary. */
6211 if (DECL_TEMPLATE_INFO (tmpl))
6212 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6213
6214 tree info = build_template_info (tmpl, args);
6215
6216 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6217 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6218 else
6219 {
6220 retrofit_lang_decl (decl);
6221 DECL_TEMPLATE_INFO (decl) = info;
6222 }
6223 }
6224
6225 if (flag_implicit_templates
6226 && !is_friend
6227 && TREE_PUBLIC (decl)
6228 && VAR_OR_FUNCTION_DECL_P (decl))
6229 /* Set DECL_COMDAT on template instantiations; if we force
6230 them to be emitted by explicit instantiation,
6231 mark_needed will tell cgraph to do the right thing. */
6232 DECL_COMDAT (decl) = true;
6233
6234 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6235
6236 return decl;
6237 }
6238
6239 /* FN is an inheriting constructor that inherits from the constructor
6240 template INHERITED; turn FN into a constructor template with a matching
6241 template header. */
6242
6243 tree
6244 add_inherited_template_parms (tree fn, tree inherited)
6245 {
6246 tree inner_parms
6247 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6248 inner_parms = copy_node (inner_parms);
6249 tree parms
6250 = tree_cons (size_int (current_template_depth + 1),
6251 inner_parms, current_template_parms);
6252 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6253 tree args = template_parms_to_args (parms);
6254 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6255 DECL_ARTIFICIAL (tmpl) = true;
6256 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6257 return tmpl;
6258 }
6259
6260 /* Called when a class template TYPE is redeclared with the indicated
6261 template PARMS, e.g.:
6262
6263 template <class T> struct S;
6264 template <class T> struct S {}; */
6265
6266 bool
6267 redeclare_class_template (tree type, tree parms, tree cons)
6268 {
6269 tree tmpl;
6270 tree tmpl_parms;
6271 int i;
6272
6273 if (!TYPE_TEMPLATE_INFO (type))
6274 {
6275 error ("%qT is not a template type", type);
6276 return false;
6277 }
6278
6279 tmpl = TYPE_TI_TEMPLATE (type);
6280 if (!PRIMARY_TEMPLATE_P (tmpl))
6281 /* The type is nested in some template class. Nothing to worry
6282 about here; there are no new template parameters for the nested
6283 type. */
6284 return true;
6285
6286 if (!parms)
6287 {
6288 error ("template specifiers not specified in declaration of %qD",
6289 tmpl);
6290 return false;
6291 }
6292
6293 parms = INNERMOST_TEMPLATE_PARMS (parms);
6294 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6295
6296 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6297 {
6298 error_n (input_location, TREE_VEC_LENGTH (parms),
6299 "redeclared with %d template parameter",
6300 "redeclared with %d template parameters",
6301 TREE_VEC_LENGTH (parms));
6302 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6303 "previous declaration %qD used %d template parameter",
6304 "previous declaration %qD used %d template parameters",
6305 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6306 return false;
6307 }
6308
6309 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6310 {
6311 tree tmpl_parm;
6312 tree parm;
6313
6314 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6315 || TREE_VEC_ELT (parms, i) == error_mark_node)
6316 continue;
6317
6318 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6319 if (error_operand_p (tmpl_parm))
6320 return false;
6321
6322 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6323
6324 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6325 TEMPLATE_DECL. */
6326 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6327 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6328 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6329 || (TREE_CODE (tmpl_parm) != PARM_DECL
6330 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6331 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6332 || (TREE_CODE (tmpl_parm) == PARM_DECL
6333 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6334 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6335 {
6336 auto_diagnostic_group d;
6337 error ("template parameter %q+#D", tmpl_parm);
6338 if (DECL_P (parm))
6339 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
6340 else
6341 inform (input_location, "redeclared here");
6342 return false;
6343 }
6344
6345 /* The parameters can be declared to introduce different
6346 constraints. */
6347 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6348 tree p2 = TREE_VEC_ELT (parms, i);
6349 if (!template_parameter_constraints_equivalent_p (p1, p2))
6350 {
6351 auto_diagnostic_group d;
6352 error ("declaration of template parameter %q+#D with different "
6353 "constraints", parm);
6354 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6355 "original declaration appeared here");
6356 return false;
6357 }
6358
6359 /* Give each template template parm in this redeclaration a
6360 DECL_CONTEXT of the template for which they are a parameter. */
6361 if (TREE_CODE (parm) == TEMPLATE_DECL)
6362 {
6363 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6364 DECL_CONTEXT (parm) = tmpl;
6365 }
6366 }
6367
6368 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
6369 return false;
6370
6371 tree ci = get_constraints (tmpl);
6372 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6373 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6374
6375 /* Two classes with different constraints declare different entities. */
6376 if (!cp_tree_equal (req1, req2))
6377 {
6378 auto_diagnostic_group d;
6379 error_at (input_location, "redeclaration %q#D with different "
6380 "constraints", tmpl);
6381 inform (DECL_SOURCE_LOCATION (tmpl),
6382 "original declaration appeared here");
6383 return false;
6384 }
6385
6386 return true;
6387 }
6388
6389 /* The actual substitution part of instantiate_non_dependent_expr,
6390 to be used when the caller has already checked
6391 !instantiation_dependent_uneval_expression_p (expr)
6392 and cleared processing_template_decl. */
6393
6394 tree
6395 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6396 {
6397 return tsubst_copy_and_build (expr,
6398 /*args=*/NULL_TREE,
6399 complain,
6400 /*in_decl=*/NULL_TREE);
6401 }
6402
6403 /* Instantiate the non-dependent expression EXPR. */
6404
6405 tree
6406 instantiate_non_dependent_expr (tree expr,
6407 tsubst_flags_t complain /* = tf_error */)
6408 {
6409 if (expr == NULL_TREE)
6410 return NULL_TREE;
6411
6412 if (processing_template_decl)
6413 {
6414 /* The caller should have checked this already. */
6415 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
6416 processing_template_decl_sentinel s;
6417 expr = instantiate_non_dependent_expr_internal (expr, complain);
6418 }
6419 return expr;
6420 }
6421
6422 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
6423 expression is dependent or non-constant. */
6424
6425 tree
6426 instantiate_non_dependent_or_null (tree expr)
6427 {
6428 if (expr == NULL_TREE)
6429 return NULL_TREE;
6430 if (processing_template_decl)
6431 {
6432 if (!is_nondependent_constant_expression (expr))
6433 expr = NULL_TREE;
6434 else
6435 {
6436 processing_template_decl_sentinel s;
6437 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6438 }
6439 }
6440 return expr;
6441 }
6442
6443 /* True iff T is a specialization of a variable template. */
6444
6445 bool
6446 variable_template_specialization_p (tree t)
6447 {
6448 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6449 return false;
6450 tree tmpl = DECL_TI_TEMPLATE (t);
6451 return variable_template_p (tmpl);
6452 }
6453
6454 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6455 template declaration, or a TYPE_DECL for an alias declaration. */
6456
6457 bool
6458 alias_type_or_template_p (tree t)
6459 {
6460 if (t == NULL_TREE)
6461 return false;
6462 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6463 || (TYPE_P (t)
6464 && TYPE_NAME (t)
6465 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6466 || DECL_ALIAS_TEMPLATE_P (t));
6467 }
6468
6469 /* If T is a specialization of an alias template, return it; otherwise return
6470 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6471
6472 tree
6473 alias_template_specialization_p (const_tree t,
6474 bool transparent_typedefs)
6475 {
6476 if (!TYPE_P (t))
6477 return NULL_TREE;
6478
6479 /* It's an alias template specialization if it's an alias and its
6480 TYPE_NAME is a specialization of a primary template. */
6481 if (typedef_variant_p (t))
6482 {
6483 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6484 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6485 return CONST_CAST_TREE (t);
6486 if (transparent_typedefs)
6487 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6488 (TYPE_NAME (t)),
6489 transparent_typedefs);
6490 }
6491
6492 return NULL_TREE;
6493 }
6494
6495 /* Data structure for complex_alias_template_*. */
6496
6497 struct uses_all_template_parms_data
6498 {
6499 int level;
6500 bool *seen;
6501 };
6502
6503 /* walk_tree callback for complex_alias_template_p. */
6504
6505 static tree
6506 complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
6507 {
6508 tree t = *tp;
6509 auto &data = *(struct uses_all_template_parms_data*)data_;
6510
6511 switch (TREE_CODE (t))
6512 {
6513 case TEMPLATE_TYPE_PARM:
6514 case TEMPLATE_PARM_INDEX:
6515 case TEMPLATE_TEMPLATE_PARM:
6516 case BOUND_TEMPLATE_TEMPLATE_PARM:
6517 {
6518 tree idx = get_template_parm_index (t);
6519 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6520 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6521 }
6522
6523 default:;
6524 }
6525
6526 if (!PACK_EXPANSION_P (t))
6527 return 0;
6528
6529 /* An alias template with a pack expansion that expands a pack from the
6530 enclosing class needs to be considered complex, to avoid confusion with
6531 the same pack being used as an argument to the alias's own template
6532 parameter (91966). */
6533 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6534 pack = TREE_CHAIN (pack))
6535 {
6536 tree parm_pack = TREE_VALUE (pack);
6537 if (!TEMPLATE_PARM_P (parm_pack))
6538 continue;
6539 int idx, level;
6540 template_parm_level_and_index (parm_pack, &level, &idx);
6541 if (level < data.level)
6542 return t;
6543
6544 /* Consider the expanded packs to be used outside the expansion... */
6545 data.seen[idx] = true;
6546 }
6547
6548 /* ...but don't walk into the pattern. Consider PR104008:
6549
6550 template <typename T, typename... Ts>
6551 using IsOneOf = disjunction<is_same<T, Ts>...>;
6552
6553 where IsOneOf seemingly uses all of its template parameters in its
6554 expansion (and does not expand a pack from the enclosing class), so the
6555 alias was not marked as complex. However, if it is used like
6556 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
6557 expansion. So only Ts is considered used by the pack expansion. */
6558 *walk_subtrees = false;
6559
6560 return 0;
6561 }
6562
6563 /* An alias template is complex from a SFINAE perspective if a template-id
6564 using that alias can be ill-formed when the expansion is not, as with
6565 the void_t template.
6566
6567 Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
6568 template arguments are empty packs. */
6569
6570 static bool
6571 complex_alias_template_p (const_tree tmpl)
6572 {
6573 /* A renaming alias isn't complex. */
6574 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6575 return false;
6576
6577 /* Any other constrained alias is complex. */
6578 if (get_constraints (tmpl))
6579 return true;
6580
6581 struct uses_all_template_parms_data data;
6582 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6583 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6584 data.level = TMPL_PARMS_DEPTH (parms);
6585 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6586 data.seen = XALLOCAVEC (bool, len);
6587 for (int i = 0; i < len; ++i)
6588 data.seen[i] = false;
6589
6590 if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
6591 return true;
6592 for (int i = 0; i < len; ++i)
6593 if (!data.seen[i])
6594 return true;
6595 return false;
6596 }
6597
6598 /* If T is a specialization of a complex alias template with dependent
6599 template-arguments, return it; otherwise return NULL_TREE. If T is a
6600 typedef to such a specialization, return the specialization. */
6601
6602 tree
6603 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6604 {
6605 if (t == error_mark_node)
6606 return NULL_TREE;
6607 gcc_assert (TYPE_P (t));
6608
6609 if (!typedef_variant_p (t))
6610 return NULL_TREE;
6611
6612 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6613 if (tinfo
6614 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6615 && (any_dependent_template_arguments_p
6616 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6617 return CONST_CAST_TREE (t);
6618
6619 if (transparent_typedefs)
6620 {
6621 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6622 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6623 }
6624
6625 return NULL_TREE;
6626 }
6627
6628 /* Return the number of innermost template parameters in TMPL. */
6629
6630 static int
6631 num_innermost_template_parms (const_tree tmpl)
6632 {
6633 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6634 return TREE_VEC_LENGTH (parms);
6635 }
6636
6637 /* Return either TMPL or another template that it is equivalent to under DR
6638 1286: An alias that just changes the name of a template is equivalent to
6639 the other template. */
6640
6641 static tree
6642 get_underlying_template (tree tmpl)
6643 {
6644 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6645 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6646 {
6647 /* Determine if the alias is equivalent to an underlying template. */
6648 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6649 /* The underlying type may have been ill-formed. Don't proceed. */
6650 if (!orig_type)
6651 break;
6652 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6653 if (!tinfo)
6654 break;
6655
6656 tree underlying = TI_TEMPLATE (tinfo);
6657 if (!PRIMARY_TEMPLATE_P (underlying)
6658 || (num_innermost_template_parms (tmpl)
6659 != num_innermost_template_parms (underlying)))
6660 break;
6661
6662 /* Does the alias add cv-quals? */
6663 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6664 break;
6665
6666 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6667 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6668 break;
6669
6670 /* Are any default template arguments equivalent? */
6671 tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6672 tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
6673 const int nparms = TREE_VEC_LENGTH (aparms);
6674 for (int i = 0; i < nparms; ++i)
6675 {
6676 tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
6677 tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
6678 if (!template_args_equal (adefarg, udefarg))
6679 goto top_break;
6680 }
6681
6682 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6683 it's appropriate to treat a less-constrained alias as equivalent. */
6684 if (!at_least_as_constrained (underlying, tmpl))
6685 break;
6686
6687 /* Alias is equivalent. Strip it and repeat. */
6688 tmpl = underlying;
6689 }
6690 top_break:;
6691
6692 return tmpl;
6693 }
6694
6695 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6696 must be a reference-to-function or a pointer-to-function type, as specified
6697 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6698 and check that the resulting function has external linkage. */
6699
6700 static tree
6701 convert_nontype_argument_function (tree type, tree expr,
6702 tsubst_flags_t complain)
6703 {
6704 tree fns = expr;
6705 tree fn, fn_no_ptr;
6706 linkage_kind linkage;
6707
6708 fn = instantiate_type (type, fns, tf_none);
6709 if (fn == error_mark_node)
6710 return error_mark_node;
6711
6712 if (value_dependent_expression_p (fn))
6713 goto accept;
6714
6715 fn_no_ptr = fn;
6716 if (REFERENCE_REF_P (fn_no_ptr))
6717 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6718 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6719 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6720 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6721 if (BASELINK_P (fn_no_ptr))
6722 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6723
6724 /* [temp.arg.nontype]/1
6725
6726 A template-argument for a non-type, non-template template-parameter
6727 shall be one of:
6728 [...]
6729 -- the address of an object or function with external [C++11: or
6730 internal] linkage. */
6731
6732 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6733 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6734 {
6735 if (complain & tf_error)
6736 {
6737 location_t loc = cp_expr_loc_or_input_loc (expr);
6738 error_at (loc, "%qE is not a valid template argument for type %qT",
6739 expr, type);
6740 if (TYPE_PTR_P (type))
6741 inform (loc, "it must be the address of a function "
6742 "with external linkage");
6743 else
6744 inform (loc, "it must be the name of a function with "
6745 "external linkage");
6746 }
6747 return NULL_TREE;
6748 }
6749
6750 linkage = decl_linkage (fn_no_ptr);
6751 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6752 {
6753 if (complain & tf_error)
6754 {
6755 location_t loc = cp_expr_loc_or_input_loc (expr);
6756 if (cxx_dialect >= cxx11)
6757 error_at (loc, "%qE is not a valid template argument for type "
6758 "%qT because %qD has no linkage",
6759 expr, type, fn_no_ptr);
6760 else
6761 error_at (loc, "%qE is not a valid template argument for type "
6762 "%qT because %qD does not have external linkage",
6763 expr, type, fn_no_ptr);
6764 }
6765 return NULL_TREE;
6766 }
6767
6768 accept:
6769 if (TYPE_REF_P (type))
6770 {
6771 if (REFERENCE_REF_P (fn))
6772 fn = TREE_OPERAND (fn, 0);
6773 else
6774 fn = build_address (fn);
6775 }
6776 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6777 fn = build_nop (type, fn);
6778
6779 return fn;
6780 }
6781
6782 /* Subroutine of convert_nontype_argument.
6783 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6784 Emit an error otherwise. */
6785
6786 static bool
6787 check_valid_ptrmem_cst_expr (tree type, tree expr,
6788 tsubst_flags_t complain)
6789 {
6790 tree orig_expr = expr;
6791 STRIP_NOPS (expr);
6792 if (null_ptr_cst_p (expr))
6793 return true;
6794 if (TREE_CODE (expr) == PTRMEM_CST
6795 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6796 PTRMEM_CST_CLASS (expr)))
6797 return true;
6798 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6799 return true;
6800 if (processing_template_decl
6801 && TREE_CODE (expr) == ADDR_EXPR
6802 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6803 return true;
6804 if (complain & tf_error)
6805 {
6806 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6807 error_at (loc, "%qE is not a valid template argument for type %qT",
6808 orig_expr, type);
6809 if (TREE_CODE (expr) != PTRMEM_CST)
6810 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6811 else
6812 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6813 }
6814 return false;
6815 }
6816
6817 /* Returns TRUE iff the address of OP is value-dependent.
6818
6819 14.6.2.4 [temp.dep.temp]:
6820 A non-integral non-type template-argument is dependent if its type is
6821 dependent or it has either of the following forms
6822 qualified-id
6823 & qualified-id
6824 and contains a nested-name-specifier which specifies a class-name that
6825 names a dependent type.
6826
6827 We generalize this to just say that the address of a member of a
6828 dependent class is value-dependent; the above doesn't cover the
6829 address of a static data member named with an unqualified-id. */
6830
6831 static bool
6832 has_value_dependent_address (tree op)
6833 {
6834 STRIP_ANY_LOCATION_WRAPPER (op);
6835
6836 /* We could use get_inner_reference here, but there's no need;
6837 this is only relevant for template non-type arguments, which
6838 can only be expressed as &id-expression. */
6839 if (DECL_P (op))
6840 {
6841 tree ctx = CP_DECL_CONTEXT (op);
6842
6843 if (TYPE_P (ctx) && dependent_type_p (ctx))
6844 return true;
6845
6846 if (VAR_P (op)
6847 && TREE_STATIC (op)
6848 && TREE_CODE (ctx) == FUNCTION_DECL
6849 && type_dependent_expression_p (ctx))
6850 return true;
6851 }
6852
6853 return false;
6854 }
6855
6856 /* The next set of functions are used for providing helpful explanatory
6857 diagnostics for failed overload resolution. Their messages should be
6858 indented by two spaces for consistency with the messages in
6859 call.cc */
6860
6861 static int
6862 unify_success (bool /*explain_p*/)
6863 {
6864 return 0;
6865 }
6866
6867 /* Other failure functions should call this one, to provide a single function
6868 for setting a breakpoint on. */
6869
6870 static int
6871 unify_invalid (bool /*explain_p*/)
6872 {
6873 return 1;
6874 }
6875
6876 static int
6877 unify_parameter_deduction_failure (bool explain_p, tree parm)
6878 {
6879 if (explain_p)
6880 inform (input_location,
6881 " couldn%'t deduce template parameter %qD", parm);
6882 return unify_invalid (explain_p);
6883 }
6884
6885 static int
6886 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6887 {
6888 if (explain_p)
6889 inform (input_location,
6890 " types %qT and %qT have incompatible cv-qualifiers",
6891 parm, arg);
6892 return unify_invalid (explain_p);
6893 }
6894
6895 static int
6896 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6897 {
6898 if (explain_p)
6899 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6900 return unify_invalid (explain_p);
6901 }
6902
6903 static int
6904 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6905 {
6906 if (explain_p)
6907 inform (input_location,
6908 " template parameter %qD is not a parameter pack, but "
6909 "argument %qD is",
6910 parm, arg);
6911 return unify_invalid (explain_p);
6912 }
6913
6914 static int
6915 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6916 {
6917 if (explain_p)
6918 inform (input_location,
6919 " template argument %qE does not match "
6920 "pointer-to-member constant %qE",
6921 arg, parm);
6922 return unify_invalid (explain_p);
6923 }
6924
6925 static int
6926 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6927 {
6928 if (explain_p)
6929 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6930 return unify_invalid (explain_p);
6931 }
6932
6933 static int
6934 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6935 {
6936 if (explain_p)
6937 inform (input_location,
6938 " inconsistent parameter pack deduction with %qT and %qT",
6939 old_arg, new_arg);
6940 return unify_invalid (explain_p);
6941 }
6942
6943 static int
6944 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6945 {
6946 if (explain_p)
6947 {
6948 if (TYPE_P (parm))
6949 inform (input_location,
6950 " deduced conflicting types for parameter %qT (%qT and %qT)",
6951 parm, first, second);
6952 else
6953 inform (input_location,
6954 " deduced conflicting values for non-type parameter "
6955 "%qE (%qE and %qE)", parm, first, second);
6956 }
6957 return unify_invalid (explain_p);
6958 }
6959
6960 static int
6961 unify_vla_arg (bool explain_p, tree arg)
6962 {
6963 if (explain_p)
6964 inform (input_location,
6965 " variable-sized array type %qT is not "
6966 "a valid template argument",
6967 arg);
6968 return unify_invalid (explain_p);
6969 }
6970
6971 static int
6972 unify_method_type_error (bool explain_p, tree arg)
6973 {
6974 if (explain_p)
6975 inform (input_location,
6976 " member function type %qT is not a valid template argument",
6977 arg);
6978 return unify_invalid (explain_p);
6979 }
6980
6981 static int
6982 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6983 {
6984 if (explain_p)
6985 {
6986 if (least_p)
6987 inform_n (input_location, wanted,
6988 " candidate expects at least %d argument, %d provided",
6989 " candidate expects at least %d arguments, %d provided",
6990 wanted, have);
6991 else
6992 inform_n (input_location, wanted,
6993 " candidate expects %d argument, %d provided",
6994 " candidate expects %d arguments, %d provided",
6995 wanted, have);
6996 }
6997 return unify_invalid (explain_p);
6998 }
6999
7000 static int
7001 unify_too_many_arguments (bool explain_p, int have, int wanted)
7002 {
7003 return unify_arity (explain_p, have, wanted);
7004 }
7005
7006 static int
7007 unify_too_few_arguments (bool explain_p, int have, int wanted,
7008 bool least_p = false)
7009 {
7010 return unify_arity (explain_p, have, wanted, least_p);
7011 }
7012
7013 static int
7014 unify_arg_conversion (bool explain_p, tree to_type,
7015 tree from_type, tree arg)
7016 {
7017 if (explain_p)
7018 inform (cp_expr_loc_or_input_loc (arg),
7019 " cannot convert %qE (type %qT) to type %qT",
7020 arg, from_type, to_type);
7021 return unify_invalid (explain_p);
7022 }
7023
7024 static int
7025 unify_no_common_base (bool explain_p, enum template_base_result r,
7026 tree parm, tree arg)
7027 {
7028 if (explain_p)
7029 switch (r)
7030 {
7031 case tbr_ambiguous_baseclass:
7032 inform (input_location, " %qT is an ambiguous base class of %qT",
7033 parm, arg);
7034 break;
7035 default:
7036 inform (input_location, " %qT is not derived from %qT", arg, parm);
7037 break;
7038 }
7039 return unify_invalid (explain_p);
7040 }
7041
7042 static int
7043 unify_inconsistent_template_template_parameters (bool explain_p)
7044 {
7045 if (explain_p)
7046 inform (input_location,
7047 " template parameters of a template template argument are "
7048 "inconsistent with other deduced template arguments");
7049 return unify_invalid (explain_p);
7050 }
7051
7052 static int
7053 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
7054 {
7055 if (explain_p)
7056 inform (input_location,
7057 " cannot deduce a template for %qT from non-template type %qT",
7058 parm, arg);
7059 return unify_invalid (explain_p);
7060 }
7061
7062 static int
7063 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
7064 {
7065 if (explain_p)
7066 inform (input_location,
7067 " template argument %qE does not match %qE", arg, parm);
7068 return unify_invalid (explain_p);
7069 }
7070
7071 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
7072 argument for TYPE, points to an unsuitable object.
7073
7074 Also adjust the type of the index in C++20 array subobject references. */
7075
7076 static bool
7077 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
7078 {
7079 switch (TREE_CODE (expr))
7080 {
7081 CASE_CONVERT:
7082 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
7083 complain);
7084
7085 case TARGET_EXPR:
7086 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
7087 complain);
7088
7089 case CONSTRUCTOR:
7090 {
7091 for (auto &e: CONSTRUCTOR_ELTS (expr))
7092 if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
7093 return true;
7094 }
7095 break;
7096
7097 case ADDR_EXPR:
7098 {
7099 tree decl = TREE_OPERAND (expr, 0);
7100
7101 if (cxx_dialect >= cxx20)
7102 while (TREE_CODE (decl) == COMPONENT_REF
7103 || TREE_CODE (decl) == ARRAY_REF)
7104 {
7105 tree &op = TREE_OPERAND (decl, 1);
7106 if (TREE_CODE (decl) == ARRAY_REF
7107 && TREE_CODE (op) == INTEGER_CST)
7108 /* Canonicalize array offsets to ptrdiff_t; how they were
7109 written doesn't matter for subobject identity. */
7110 op = fold_convert (ptrdiff_type_node, op);
7111 decl = TREE_OPERAND (decl, 0);
7112 }
7113
7114 if (!VAR_P (decl))
7115 {
7116 if (complain & tf_error)
7117 error_at (cp_expr_loc_or_input_loc (expr),
7118 "%qE is not a valid template argument of type %qT "
7119 "because %qE is not a variable", expr, type, decl);
7120 return true;
7121 }
7122 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7123 {
7124 if (complain & tf_error)
7125 error_at (cp_expr_loc_or_input_loc (expr),
7126 "%qE is not a valid template argument of type %qT "
7127 "in C++98 because %qD does not have external linkage",
7128 expr, type, decl);
7129 return true;
7130 }
7131 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7132 && decl_linkage (decl) == lk_none)
7133 {
7134 if (complain & tf_error)
7135 error_at (cp_expr_loc_or_input_loc (expr),
7136 "%qE is not a valid template argument of type %qT "
7137 "because %qD has no linkage", expr, type, decl);
7138 return true;
7139 }
7140 /* C++17: For a non-type template-parameter of reference or pointer
7141 type, the value of the constant expression shall not refer to (or
7142 for a pointer type, shall not be the address of):
7143 * a subobject (4.5),
7144 * a temporary object (15.2),
7145 * a string literal (5.13.5),
7146 * the result of a typeid expression (8.2.8), or
7147 * a predefined __func__ variable (11.4.1). */
7148 else if (DECL_ARTIFICIAL (decl))
7149 {
7150 if (complain & tf_error)
7151 error ("the address of %qD is not a valid template argument",
7152 decl);
7153 return true;
7154 }
7155 else if (cxx_dialect < cxx20
7156 && !(same_type_ignoring_top_level_qualifiers_p
7157 (strip_array_types (TREE_TYPE (type)),
7158 strip_array_types (TREE_TYPE (decl)))))
7159 {
7160 if (complain & tf_error)
7161 error ("the address of the %qT subobject of %qD is not a "
7162 "valid template argument", TREE_TYPE (type), decl);
7163 return true;
7164 }
7165 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7166 {
7167 if (complain & tf_error)
7168 error ("the address of %qD is not a valid template argument "
7169 "because it does not have static storage duration",
7170 decl);
7171 return true;
7172 }
7173 }
7174 break;
7175
7176 default:
7177 if (!INDIRECT_TYPE_P (type))
7178 /* We're only concerned about pointers and references here. */;
7179 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7180 /* Null pointer values are OK in C++11. */;
7181 else
7182 {
7183 if (VAR_P (expr))
7184 {
7185 if (complain & tf_error)
7186 error ("%qD is not a valid template argument "
7187 "because %qD is a variable, not the address of "
7188 "a variable", expr, expr);
7189 return true;
7190 }
7191 else
7192 {
7193 if (complain & tf_error)
7194 error ("%qE is not a valid template argument for %qT "
7195 "because it is not the address of a variable",
7196 expr, type);
7197 return true;
7198 }
7199 }
7200 }
7201 return false;
7202
7203 }
7204
7205 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7206 template argument EXPR. */
7207
7208 static tree
7209 create_template_parm_object (tree expr, tsubst_flags_t complain)
7210 {
7211 if (TREE_CODE (expr) == TARGET_EXPR)
7212 expr = TARGET_EXPR_INITIAL (expr);
7213
7214 if (!TREE_CONSTANT (expr))
7215 {
7216 if ((complain & tf_error)
7217 && require_rvalue_constant_expression (expr))
7218 cxx_constant_value (expr);
7219 return error_mark_node;
7220 }
7221 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7222 return error_mark_node;
7223
7224 /* This is no longer a compound literal. */
7225 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7226
7227 return get_template_parm_object (expr, mangle_template_parm_object (expr));
7228 }
7229
7230 /* The template arguments corresponding to template parameter objects of types
7231 that contain pointers to members. */
7232
7233 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7234
7235 /* Find or build an nttp object for (already-validated) EXPR with name
7236 NAME. */
7237
7238 tree
7239 get_template_parm_object (tree expr, tree name)
7240 {
7241 tree decl = get_global_binding (name);
7242 if (decl)
7243 return decl;
7244
7245 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7246 decl = create_temporary_var (type);
7247 DECL_NTTP_OBJECT_P (decl) = true;
7248 DECL_CONTEXT (decl) = NULL_TREE;
7249 TREE_STATIC (decl) = true;
7250 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7251 TREE_READONLY (decl) = true;
7252 DECL_NAME (decl) = name;
7253 SET_DECL_ASSEMBLER_NAME (decl, name);
7254 comdat_linkage (decl);
7255
7256 if (!zero_init_p (type))
7257 {
7258 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7259 lower_var_init before we're done mangling. So store the original
7260 value elsewhere. */
7261 tree copy = unshare_constructor (expr);
7262 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7263 }
7264
7265 pushdecl_top_level_and_finish (decl, expr);
7266
7267 return decl;
7268 }
7269
7270 /* Return the actual template argument corresponding to template parameter
7271 object VAR. */
7272
7273 tree
7274 tparm_object_argument (tree var)
7275 {
7276 if (zero_init_p (TREE_TYPE (var)))
7277 return DECL_INITIAL (var);
7278 return *(tparm_obj_values->get (var));
7279 }
7280
7281 /* Attempt to convert the non-type template parameter EXPR to the
7282 indicated TYPE. If the conversion is successful, return the
7283 converted value. If the conversion is unsuccessful, return
7284 NULL_TREE if we issued an error message, or error_mark_node if we
7285 did not. We issue error messages for out-and-out bad template
7286 parameters, but not simply because the conversion failed, since we
7287 might be just trying to do argument deduction. Both TYPE and EXPR
7288 must be non-dependent.
7289
7290 The conversion follows the special rules described in
7291 [temp.arg.nontype], and it is much more strict than an implicit
7292 conversion.
7293
7294 This function is called twice for each template argument (see
7295 lookup_template_class for a more accurate description of this
7296 problem). This means that we need to handle expressions which
7297 are not valid in a C++ source, but can be created from the
7298 first call (for instance, casts to perform conversions). These
7299 hacks can go away after we fix the double coercion problem. */
7300
7301 static tree
7302 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7303 {
7304 tree expr_type;
7305 location_t loc = cp_expr_loc_or_input_loc (expr);
7306
7307 /* Detect immediately string literals as invalid non-type argument.
7308 This special-case is not needed for correctness (we would easily
7309 catch this later), but only to provide better diagnostic for this
7310 common user mistake. As suggested by DR 100, we do not mention
7311 linkage issues in the diagnostic as this is not the point. */
7312 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7313 {
7314 if (complain & tf_error)
7315 error ("%qE is not a valid template argument for type %qT "
7316 "because string literals can never be used in this context",
7317 expr, type);
7318 return NULL_TREE;
7319 }
7320
7321 /* Add the ADDR_EXPR now for the benefit of
7322 value_dependent_expression_p. */
7323 if (TYPE_PTROBV_P (type)
7324 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7325 {
7326 expr = decay_conversion (expr, complain);
7327 if (expr == error_mark_node)
7328 return error_mark_node;
7329 }
7330
7331 /* If we are in a template, EXPR may be non-dependent, but still
7332 have a syntactic, rather than semantic, form. For example, EXPR
7333 might be a SCOPE_REF, rather than the VAR_DECL to which the
7334 SCOPE_REF refers. Preserving the qualifying scope is necessary
7335 so that access checking can be performed when the template is
7336 instantiated -- but here we need the resolved form so that we can
7337 convert the argument. */
7338 bool non_dep = false;
7339 if (TYPE_REF_OBJ_P (type)
7340 && has_value_dependent_address (expr))
7341 /* If we want the address and it's value-dependent, don't fold. */;
7342 else if (processing_template_decl
7343 && !instantiation_dependent_expression_p (expr))
7344 non_dep = true;
7345 if (error_operand_p (expr))
7346 return error_mark_node;
7347 expr_type = TREE_TYPE (expr);
7348
7349 /* If the argument is non-dependent, perform any conversions in
7350 non-dependent context as well. */
7351 processing_template_decl_sentinel s (non_dep);
7352 if (non_dep)
7353 expr = instantiate_non_dependent_expr_internal (expr, complain);
7354
7355 bool val_dep_p = value_dependent_expression_p (expr);
7356 if (val_dep_p)
7357 expr = canonicalize_expr_argument (expr, complain);
7358 else
7359 STRIP_ANY_LOCATION_WRAPPER (expr);
7360
7361 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7362 to a non-type argument of "nullptr". */
7363 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7364 expr = fold_simple (convert (type, expr));
7365
7366 /* In C++11, integral or enumeration non-type template arguments can be
7367 arbitrary constant expressions. Pointer and pointer to
7368 member arguments can be general constant expressions that evaluate
7369 to a null value, but otherwise still need to be of a specific form. */
7370 if (cxx_dialect >= cxx11)
7371 {
7372 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7373 /* A PTRMEM_CST is already constant, and a valid template
7374 argument for a parameter of pointer to member type, we just want
7375 to leave it in that form rather than lower it to a
7376 CONSTRUCTOR. */;
7377 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7378 || cxx_dialect >= cxx17)
7379 {
7380 /* C++17: A template-argument for a non-type template-parameter shall
7381 be a converted constant expression (8.20) of the type of the
7382 template-parameter. */
7383 expr = build_converted_constant_expr (type, expr, complain);
7384 if (expr == error_mark_node)
7385 /* Make sure we return NULL_TREE only if we have really issued
7386 an error, as described above. */
7387 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7388 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7389 {
7390 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7391 return expr;
7392 }
7393 expr = maybe_constant_value (expr, NULL_TREE,
7394 /*manifestly_const_eval=*/true);
7395 expr = convert_from_reference (expr);
7396 /* EXPR may have become value-dependent. */
7397 val_dep_p = value_dependent_expression_p (expr);
7398 }
7399 else if (TYPE_PTR_OR_PTRMEM_P (type))
7400 {
7401 tree folded = maybe_constant_value (expr, NULL_TREE,
7402 /*manifestly_const_eval=*/true);
7403 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7404 : null_member_pointer_value_p (folded))
7405 expr = folded;
7406 }
7407 }
7408
7409 if (TYPE_REF_P (type))
7410 expr = mark_lvalue_use (expr);
7411 else
7412 expr = mark_rvalue_use (expr);
7413
7414 /* HACK: Due to double coercion, we can get a
7415 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7416 which is the tree that we built on the first call (see
7417 below when coercing to reference to object or to reference to
7418 function). We just strip everything and get to the arg.
7419 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7420 for examples. */
7421 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7422 {
7423 /* Check this before we strip *& to avoid redundancy. */
7424 if (!mark_single_function (expr, complain))
7425 return error_mark_node;
7426
7427 tree probe_type, probe = expr;
7428 if (REFERENCE_REF_P (probe))
7429 probe = TREE_OPERAND (probe, 0);
7430 probe_type = TREE_TYPE (probe);
7431 if (TREE_CODE (probe) == NOP_EXPR)
7432 {
7433 /* ??? Maybe we could use convert_from_reference here, but we
7434 would need to relax its constraints because the NOP_EXPR
7435 could actually change the type to something more cv-qualified,
7436 and this is not folded by convert_from_reference. */
7437 tree addr = TREE_OPERAND (probe, 0);
7438 if (TYPE_REF_P (probe_type)
7439 && TREE_CODE (addr) == ADDR_EXPR
7440 && TYPE_PTR_P (TREE_TYPE (addr))
7441 && (same_type_ignoring_top_level_qualifiers_p
7442 (TREE_TYPE (probe_type),
7443 TREE_TYPE (TREE_TYPE (addr)))))
7444 {
7445 expr = TREE_OPERAND (addr, 0);
7446 expr_type = TREE_TYPE (probe_type);
7447 }
7448 }
7449 }
7450
7451 /* [temp.arg.nontype]/5, bullet 1
7452
7453 For a non-type template-parameter of integral or enumeration type,
7454 integral promotions (_conv.prom_) and integral conversions
7455 (_conv.integral_) are applied. */
7456 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7457 || TREE_CODE (type) == REAL_TYPE)
7458 {
7459 if (cxx_dialect < cxx11)
7460 {
7461 tree t = build_converted_constant_expr (type, expr, complain);
7462 t = maybe_constant_value (t);
7463 if (t != error_mark_node)
7464 expr = t;
7465 }
7466
7467 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7468 return error_mark_node;
7469
7470 /* Notice that there are constant expressions like '4 % 0' which
7471 do not fold into integer constants. */
7472 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7473 {
7474 if (complain & tf_error)
7475 {
7476 int errs = errorcount, warns = warningcount + werrorcount;
7477 if (!require_potential_constant_expression (expr))
7478 expr = error_mark_node;
7479 else
7480 expr = cxx_constant_value (expr);
7481 if (errorcount > errs || warningcount + werrorcount > warns)
7482 inform (loc, "in template argument for type %qT", type);
7483 if (expr == error_mark_node)
7484 return NULL_TREE;
7485 /* else cxx_constant_value complained but gave us
7486 a real constant, so go ahead. */
7487 if (!CONSTANT_CLASS_P (expr))
7488 {
7489 /* Some assemble time constant expressions like
7490 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7491 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7492 as we can emit them into .rodata initializers of
7493 variables, yet they can't fold into an INTEGER_CST at
7494 compile time. Refuse them here. */
7495 gcc_checking_assert (reduced_constant_expression_p (expr));
7496 error_at (loc, "template argument %qE for type %qT not "
7497 "a compile-time constant", expr, type);
7498 return NULL_TREE;
7499 }
7500 }
7501 else
7502 return NULL_TREE;
7503 }
7504
7505 /* Avoid typedef problems. */
7506 if (TREE_TYPE (expr) != type)
7507 expr = fold_convert (type, expr);
7508 }
7509 /* [temp.arg.nontype]/5, bullet 2
7510
7511 For a non-type template-parameter of type pointer to object,
7512 qualification conversions (_conv.qual_) and the array-to-pointer
7513 conversion (_conv.array_) are applied. */
7514 else if (TYPE_PTROBV_P (type))
7515 {
7516 tree decayed = expr;
7517
7518 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7519 decay_conversion or an explicit cast. If it's a problematic cast,
7520 we'll complain about it below. */
7521 if (TREE_CODE (expr) == NOP_EXPR)
7522 {
7523 tree probe = expr;
7524 STRIP_NOPS (probe);
7525 if (TREE_CODE (probe) == ADDR_EXPR
7526 && TYPE_PTR_P (TREE_TYPE (probe)))
7527 {
7528 expr = probe;
7529 expr_type = TREE_TYPE (expr);
7530 }
7531 }
7532
7533 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7534
7535 A template-argument for a non-type, non-template template-parameter
7536 shall be one of: [...]
7537
7538 -- the name of a non-type template-parameter;
7539 -- the address of an object or function with external linkage, [...]
7540 expressed as "& id-expression" where the & is optional if the name
7541 refers to a function or array, or if the corresponding
7542 template-parameter is a reference.
7543
7544 Here, we do not care about functions, as they are invalid anyway
7545 for a parameter of type pointer-to-object. */
7546
7547 if (val_dep_p)
7548 /* Non-type template parameters are OK. */
7549 ;
7550 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7551 /* Null pointer values are OK in C++11. */;
7552 else if (TREE_CODE (expr) != ADDR_EXPR
7553 && !INDIRECT_TYPE_P (expr_type))
7554 /* Other values, like integer constants, might be valid
7555 non-type arguments of some other type. */
7556 return error_mark_node;
7557 else if (invalid_tparm_referent_p (type, expr, complain))
7558 return NULL_TREE;
7559
7560 expr = decayed;
7561
7562 expr = perform_qualification_conversions (type, expr);
7563 if (expr == error_mark_node)
7564 return error_mark_node;
7565 }
7566 /* [temp.arg.nontype]/5, bullet 3
7567
7568 For a non-type template-parameter of type reference to object, no
7569 conversions apply. The type referred to by the reference may be more
7570 cv-qualified than the (otherwise identical) type of the
7571 template-argument. The template-parameter is bound directly to the
7572 template-argument, which must be an lvalue. */
7573 else if (TYPE_REF_OBJ_P (type))
7574 {
7575 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7576 expr_type))
7577 return error_mark_node;
7578
7579 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7580 {
7581 if (complain & tf_error)
7582 error ("%qE is not a valid template argument for type %qT "
7583 "because of conflicts in cv-qualification", expr, type);
7584 return NULL_TREE;
7585 }
7586
7587 if (!lvalue_p (expr))
7588 {
7589 if (complain & tf_error)
7590 error ("%qE is not a valid template argument for type %qT "
7591 "because it is not an lvalue", expr, type);
7592 return NULL_TREE;
7593 }
7594
7595 /* [temp.arg.nontype]/1
7596
7597 A template-argument for a non-type, non-template template-parameter
7598 shall be one of: [...]
7599
7600 -- the address of an object or function with external linkage. */
7601 if (INDIRECT_REF_P (expr)
7602 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7603 {
7604 expr = TREE_OPERAND (expr, 0);
7605 if (DECL_P (expr))
7606 {
7607 if (complain & tf_error)
7608 error ("%q#D is not a valid template argument for type %qT "
7609 "because a reference variable does not have a constant "
7610 "address", expr, type);
7611 return NULL_TREE;
7612 }
7613 }
7614
7615 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7616 /* OK, dependent reference. We don't want to ask whether a DECL is
7617 itself value-dependent, since what we want here is its address. */;
7618 else
7619 {
7620 expr = build_address (expr);
7621
7622 if (invalid_tparm_referent_p (type, expr, complain))
7623 return NULL_TREE;
7624 }
7625
7626 if (!same_type_p (type, TREE_TYPE (expr)))
7627 expr = build_nop (type, expr);
7628 }
7629 /* [temp.arg.nontype]/5, bullet 4
7630
7631 For a non-type template-parameter of type pointer to function, only
7632 the function-to-pointer conversion (_conv.func_) is applied. If the
7633 template-argument represents a set of overloaded functions (or a
7634 pointer to such), the matching function is selected from the set
7635 (_over.over_). */
7636 else if (TYPE_PTRFN_P (type))
7637 {
7638 /* If the argument is a template-id, we might not have enough
7639 context information to decay the pointer. */
7640 if (!type_unknown_p (expr_type))
7641 {
7642 expr = decay_conversion (expr, complain);
7643 if (expr == error_mark_node)
7644 return error_mark_node;
7645 }
7646
7647 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7648 /* Null pointer values are OK in C++11. */
7649 return perform_qualification_conversions (type, expr);
7650
7651 expr = convert_nontype_argument_function (type, expr, complain);
7652 if (!expr || expr == error_mark_node)
7653 return expr;
7654 }
7655 /* [temp.arg.nontype]/5, bullet 5
7656
7657 For a non-type template-parameter of type reference to function, no
7658 conversions apply. If the template-argument represents a set of
7659 overloaded functions, the matching function is selected from the set
7660 (_over.over_). */
7661 else if (TYPE_REFFN_P (type))
7662 {
7663 if (TREE_CODE (expr) == ADDR_EXPR)
7664 {
7665 if (complain & tf_error)
7666 {
7667 error ("%qE is not a valid template argument for type %qT "
7668 "because it is a pointer", expr, type);
7669 inform (input_location, "try using %qE instead",
7670 TREE_OPERAND (expr, 0));
7671 }
7672 return NULL_TREE;
7673 }
7674
7675 expr = convert_nontype_argument_function (type, expr, complain);
7676 if (!expr || expr == error_mark_node)
7677 return expr;
7678 }
7679 /* [temp.arg.nontype]/5, bullet 6
7680
7681 For a non-type template-parameter of type pointer to member function,
7682 no conversions apply. If the template-argument represents a set of
7683 overloaded member functions, the matching member function is selected
7684 from the set (_over.over_). */
7685 else if (TYPE_PTRMEMFUNC_P (type))
7686 {
7687 expr = instantiate_type (type, expr, tf_none);
7688 if (expr == error_mark_node)
7689 return error_mark_node;
7690
7691 /* [temp.arg.nontype] bullet 1 says the pointer to member
7692 expression must be a pointer-to-member constant. */
7693 if (!val_dep_p
7694 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7695 return NULL_TREE;
7696
7697 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7698 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7699 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7700 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7701 }
7702 /* [temp.arg.nontype]/5, bullet 7
7703
7704 For a non-type template-parameter of type pointer to data member,
7705 qualification conversions (_conv.qual_) are applied. */
7706 else if (TYPE_PTRDATAMEM_P (type))
7707 {
7708 /* [temp.arg.nontype] bullet 1 says the pointer to member
7709 expression must be a pointer-to-member constant. */
7710 if (!val_dep_p
7711 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7712 return NULL_TREE;
7713
7714 expr = perform_qualification_conversions (type, expr);
7715 if (expr == error_mark_node)
7716 return expr;
7717 }
7718 else if (NULLPTR_TYPE_P (type))
7719 {
7720 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7721 {
7722 if (complain & tf_error)
7723 error ("%qE is not a valid template argument for type %qT "
7724 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7725 return NULL_TREE;
7726 }
7727 return expr;
7728 }
7729 else if (CLASS_TYPE_P (type))
7730 {
7731 /* Replace the argument with a reference to the corresponding template
7732 parameter object. */
7733 if (!val_dep_p)
7734 expr = create_template_parm_object (expr, complain);
7735 if (expr == error_mark_node)
7736 return NULL_TREE;
7737 }
7738 /* A template non-type parameter must be one of the above. */
7739 else
7740 gcc_unreachable ();
7741
7742 /* Sanity check: did we actually convert the argument to the
7743 right type? */
7744 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7745 (type, TREE_TYPE (expr)));
7746 return convert_from_reference (expr);
7747 }
7748
7749 /* Subroutine of coerce_template_template_parms, which returns 1 if
7750 PARM_PARM and ARG_PARM match using the rule for the template
7751 parameters of template template parameters. Both PARM and ARG are
7752 template parameters; the rest of the arguments are the same as for
7753 coerce_template_template_parms.
7754 */
7755 static int
7756 coerce_template_template_parm (tree parm,
7757 tree arg,
7758 tsubst_flags_t complain,
7759 tree in_decl,
7760 tree outer_args)
7761 {
7762 if (arg == NULL_TREE || error_operand_p (arg)
7763 || parm == NULL_TREE || error_operand_p (parm))
7764 return 0;
7765
7766 if (TREE_CODE (arg) != TREE_CODE (parm))
7767 return 0;
7768
7769 switch (TREE_CODE (parm))
7770 {
7771 case TEMPLATE_DECL:
7772 /* We encounter instantiations of templates like
7773 template <template <template <class> class> class TT>
7774 class C; */
7775 {
7776 tree parmparm = DECL_TEMPLATE_PARMS (parm);
7777 tree argparm = DECL_TEMPLATE_PARMS (arg);
7778
7779 if (!coerce_template_template_parms
7780 (parmparm, argparm, complain, in_decl, outer_args))
7781 return 0;
7782 }
7783 /* Fall through. */
7784
7785 case TYPE_DECL:
7786 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7787 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7788 /* Argument is a parameter pack but parameter is not. */
7789 return 0;
7790 break;
7791
7792 case PARM_DECL:
7793 /* The tsubst call is used to handle cases such as
7794
7795 template <int> class C {};
7796 template <class T, template <T> class TT> class D {};
7797 D<int, C> d;
7798
7799 i.e. the parameter list of TT depends on earlier parameters. */
7800 if (!uses_template_parms (TREE_TYPE (arg)))
7801 {
7802 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7803 if (!uses_template_parms (t)
7804 && !same_type_p (t, TREE_TYPE (arg)))
7805 return 0;
7806 }
7807
7808 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7809 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7810 /* Argument is a parameter pack but parameter is not. */
7811 return 0;
7812
7813 break;
7814
7815 default:
7816 gcc_unreachable ();
7817 }
7818
7819 return 1;
7820 }
7821
7822 /* Coerce template argument list ARGLIST for use with template
7823 template-parameter TEMPL. */
7824
7825 static tree
7826 coerce_template_args_for_ttp (tree templ, tree arglist,
7827 tsubst_flags_t complain)
7828 {
7829 /* Consider an example where a template template parameter declared as
7830
7831 template <class T, class U = std::allocator<T> > class TT
7832
7833 The template parameter level of T and U are one level larger than
7834 of TT. To proper process the default argument of U, say when an
7835 instantiation `TT<int>' is seen, we need to build the full
7836 arguments containing {int} as the innermost level. Outer levels,
7837 available when not appearing as default template argument, can be
7838 obtained from the arguments of the enclosing template.
7839
7840 Suppose that TT is later substituted with std::vector. The above
7841 instantiation is `TT<int, std::allocator<T> >' with TT at
7842 level 1, and T at level 2, while the template arguments at level 1
7843 becomes {std::vector} and the inner level 2 is {int}. */
7844
7845 tree outer = DECL_CONTEXT (templ);
7846 if (outer)
7847 outer = generic_targs_for (outer);
7848 else if (current_template_parms)
7849 {
7850 /* This is an argument of the current template, so we haven't set
7851 DECL_CONTEXT yet. */
7852 tree relevant_template_parms;
7853
7854 /* Parameter levels that are greater than the level of the given
7855 template template parm are irrelevant. */
7856 relevant_template_parms = current_template_parms;
7857 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7858 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7859 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7860
7861 outer = template_parms_to_args (relevant_template_parms);
7862 }
7863
7864 if (outer)
7865 arglist = add_to_template_args (outer, arglist);
7866
7867 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7868 return coerce_template_parms (parmlist, arglist, templ, complain);
7869 }
7870
7871 /* A cache of template template parameters with match-all default
7872 arguments. */
7873 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7874
7875 /* T is a bound template template-parameter. Copy its arguments into default
7876 arguments of the template template-parameter's template parameters. */
7877
7878 static tree
7879 add_defaults_to_ttp (tree otmpl)
7880 {
7881 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7882 return *c;
7883
7884 tree ntmpl = copy_node (otmpl);
7885
7886 tree ntype = copy_node (TREE_TYPE (otmpl));
7887 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7888 TYPE_MAIN_VARIANT (ntype) = ntype;
7889 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7890 TYPE_NAME (ntype) = ntmpl;
7891 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7892
7893 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7894 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7895 TEMPLATE_PARM_DECL (idx) = ntmpl;
7896 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7897
7898 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7899 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7900 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7901 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7902 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7903 {
7904 tree o = TREE_VEC_ELT (vec, i);
7905 if (!template_parameter_pack_p (TREE_VALUE (o)))
7906 {
7907 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7908 TREE_PURPOSE (n) = any_targ_node;
7909 }
7910 }
7911
7912 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7913 return ntmpl;
7914 }
7915
7916 /* ARG is a bound potential template template-argument, and PARGS is a list
7917 of arguments for the corresponding template template-parameter. Adjust
7918 PARGS as appropriate for application to ARG's template, and if ARG is a
7919 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7920 arguments to the template template parameter. */
7921
7922 static tree
7923 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7924 {
7925 ++processing_template_decl;
7926 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7927 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7928 {
7929 /* When comparing two template template-parameters in partial ordering,
7930 rewrite the one currently being used as an argument to have default
7931 arguments for all parameters. */
7932 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7933 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7934 if (pargs != error_mark_node)
7935 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7936 TYPE_TI_ARGS (arg));
7937 }
7938 else
7939 {
7940 tree aparms
7941 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7942 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
7943 }
7944 --processing_template_decl;
7945 return pargs;
7946 }
7947
7948 /* Subroutine of unify for the case when PARM is a
7949 BOUND_TEMPLATE_TEMPLATE_PARM. */
7950
7951 static int
7952 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7953 bool explain_p)
7954 {
7955 tree parmvec = TYPE_TI_ARGS (parm);
7956 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7957
7958 /* The template template parm might be variadic and the argument
7959 not, so flatten both argument lists. */
7960 parmvec = expand_template_argument_pack (parmvec);
7961 argvec = expand_template_argument_pack (argvec);
7962
7963 if (flag_new_ttp)
7964 {
7965 /* In keeping with P0522R0, adjust P's template arguments
7966 to apply to A's template; then flatten it again. */
7967 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7968 nparmvec = expand_template_argument_pack (nparmvec);
7969
7970 if (unify (tparms, targs, nparmvec, argvec,
7971 UNIFY_ALLOW_NONE, explain_p))
7972 return 1;
7973
7974 /* If the P0522 adjustment eliminated a pack expansion, deduce
7975 empty packs. */
7976 if (flag_new_ttp
7977 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7978 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7979 DEDUCE_EXACT, /*sub*/true, explain_p))
7980 return 1;
7981 }
7982 else
7983 {
7984 /* Deduce arguments T, i from TT<T> or TT<i>.
7985 We check each element of PARMVEC and ARGVEC individually
7986 rather than the whole TREE_VEC since they can have
7987 different number of elements, which is allowed under N2555. */
7988
7989 int len = TREE_VEC_LENGTH (parmvec);
7990
7991 /* Check if the parameters end in a pack, making them
7992 variadic. */
7993 int parm_variadic_p = 0;
7994 if (len > 0
7995 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7996 parm_variadic_p = 1;
7997
7998 for (int i = 0; i < len - parm_variadic_p; ++i)
7999 /* If the template argument list of P contains a pack
8000 expansion that is not the last template argument, the
8001 entire template argument list is a non-deduced
8002 context. */
8003 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
8004 return unify_success (explain_p);
8005
8006 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
8007 return unify_too_few_arguments (explain_p,
8008 TREE_VEC_LENGTH (argvec), len);
8009
8010 for (int i = 0; i < len - parm_variadic_p; ++i)
8011 if (unify (tparms, targs,
8012 TREE_VEC_ELT (parmvec, i),
8013 TREE_VEC_ELT (argvec, i),
8014 UNIFY_ALLOW_NONE, explain_p))
8015 return 1;
8016
8017 if (parm_variadic_p
8018 && unify_pack_expansion (tparms, targs,
8019 parmvec, argvec,
8020 DEDUCE_EXACT,
8021 /*subr=*/true, explain_p))
8022 return 1;
8023 }
8024
8025 return 0;
8026 }
8027
8028 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
8029 template template parameters. Both PARM_PARMS and ARG_PARMS are
8030 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
8031 or PARM_DECL.
8032
8033 Consider the example:
8034 template <class T> class A;
8035 template<template <class U> class TT> class B;
8036
8037 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
8038 the parameters to A, and OUTER_ARGS contains A. */
8039
8040 static int
8041 coerce_template_template_parms (tree parm_parms_full,
8042 tree arg_parms_full,
8043 tsubst_flags_t complain,
8044 tree in_decl,
8045 tree outer_args)
8046 {
8047 int nparms, nargs, i;
8048 tree parm, arg;
8049 int variadic_p = 0;
8050
8051 tree parm_parms = INNERMOST_TEMPLATE_PARMS (parm_parms_full);
8052 tree arg_parms = INNERMOST_TEMPLATE_PARMS (arg_parms_full);
8053
8054 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
8055 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
8056
8057 nparms = TREE_VEC_LENGTH (parm_parms);
8058 nargs = TREE_VEC_LENGTH (arg_parms);
8059
8060 if (flag_new_ttp)
8061 {
8062 /* P0522R0: A template template-parameter P is at least as specialized as
8063 a template template-argument A if, given the following rewrite to two
8064 function templates, the function template corresponding to P is at
8065 least as specialized as the function template corresponding to A
8066 according to the partial ordering rules for function templates
8067 ([temp.func.order]). Given an invented class template X with the
8068 template parameter list of A (including default arguments):
8069
8070 * Each of the two function templates has the same template parameters,
8071 respectively, as P or A.
8072
8073 * Each function template has a single function parameter whose type is
8074 a specialization of X with template arguments corresponding to the
8075 template parameters from the respective function template where, for
8076 each template parameter PP in the template parameter list of the
8077 function template, a corresponding template argument AA is formed. If
8078 PP declares a parameter pack, then AA is the pack expansion
8079 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
8080
8081 If the rewrite produces an invalid type, then P is not at least as
8082 specialized as A. */
8083
8084 /* So coerce P's args to apply to A's parms, and then deduce between A's
8085 args and the converted args. If that succeeds, A is at least as
8086 specialized as P, so they match.*/
8087 processing_template_decl_sentinel ptds (/*reset*/false);
8088 ++processing_template_decl;
8089
8090 tree pargs = template_parms_level_to_args (parm_parms);
8091
8092 /* PARM, and thus the context in which we are passing ARG to it, may be
8093 at a deeper level than ARG; when trying to coerce to ARG_PARMS, we
8094 want to provide the right number of levels, so we reduce the number of
8095 levels in OUTER_ARGS before prepending them. This is most important
8096 when ARG is a namespace-scope template, as in alias-decl-ttp2.C.
8097
8098 ARG might also be deeper than PARM (ttp23). In that case, we include
8099 all of OUTER_ARGS. The missing levels seem potentially problematic,
8100 but I can't come up with a testcase that breaks. */
8101 if (int arg_outer_levs = TMPL_PARMS_DEPTH (arg_parms_full) - 1)
8102 {
8103 auto x = make_temp_override (TREE_VEC_LENGTH (outer_args));
8104 if (TMPL_ARGS_DEPTH (outer_args) > arg_outer_levs)
8105 TREE_VEC_LENGTH (outer_args) = arg_outer_levs;
8106 pargs = add_to_template_args (outer_args, pargs);
8107 }
8108
8109 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none);
8110 if (pargs != error_mark_node)
8111 {
8112 tree targs = make_tree_vec (nargs);
8113 tree aargs = template_parms_level_to_args (arg_parms);
8114 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
8115 /*explain*/false))
8116 return 1;
8117 }
8118 }
8119
8120 /* Determine whether we have a parameter pack at the end of the
8121 template template parameter's template parameter list. */
8122 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
8123 {
8124 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8125
8126 if (error_operand_p (parm))
8127 return 0;
8128
8129 switch (TREE_CODE (parm))
8130 {
8131 case TEMPLATE_DECL:
8132 case TYPE_DECL:
8133 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8134 variadic_p = 1;
8135 break;
8136
8137 case PARM_DECL:
8138 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8139 variadic_p = 1;
8140 break;
8141
8142 default:
8143 gcc_unreachable ();
8144 }
8145 }
8146
8147 if (nargs != nparms
8148 && !(variadic_p && nargs >= nparms - 1))
8149 return 0;
8150
8151 /* Check all of the template parameters except the parameter pack at
8152 the end (if any). */
8153 for (i = 0; i < nparms - variadic_p; ++i)
8154 {
8155 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8156 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8157 continue;
8158
8159 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8160 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8161
8162 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8163 outer_args))
8164 return 0;
8165
8166 }
8167
8168 if (variadic_p)
8169 {
8170 /* Check each of the template parameters in the template
8171 argument against the template parameter pack at the end of
8172 the template template parameter. */
8173 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8174 return 0;
8175
8176 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8177
8178 for (; i < nargs; ++i)
8179 {
8180 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8181 continue;
8182
8183 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8184
8185 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8186 outer_args))
8187 return 0;
8188 }
8189 }
8190
8191 return 1;
8192 }
8193
8194 /* Verifies that the deduced template arguments (in TARGS) for the
8195 template template parameters (in TPARMS) represent valid bindings,
8196 by comparing the template parameter list of each template argument
8197 to the template parameter list of its corresponding template
8198 template parameter, in accordance with DR150. This
8199 routine can only be called after all template arguments have been
8200 deduced. It will return TRUE if all of the template template
8201 parameter bindings are okay, FALSE otherwise. */
8202 bool
8203 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8204 {
8205 int i, ntparms = TREE_VEC_LENGTH (tparms);
8206 bool ret = true;
8207
8208 /* We're dealing with template parms in this process. */
8209 ++processing_template_decl;
8210
8211 targs = INNERMOST_TEMPLATE_ARGS (targs);
8212
8213 for (i = 0; i < ntparms; ++i)
8214 {
8215 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8216 tree targ = TREE_VEC_ELT (targs, i);
8217
8218 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8219 {
8220 tree packed_args = NULL_TREE;
8221 int idx, len = 1;
8222
8223 if (ARGUMENT_PACK_P (targ))
8224 {
8225 /* Look inside the argument pack. */
8226 packed_args = ARGUMENT_PACK_ARGS (targ);
8227 len = TREE_VEC_LENGTH (packed_args);
8228 }
8229
8230 for (idx = 0; idx < len; ++idx)
8231 {
8232 tree targ_parms = NULL_TREE;
8233
8234 if (packed_args)
8235 /* Extract the next argument from the argument
8236 pack. */
8237 targ = TREE_VEC_ELT (packed_args, idx);
8238
8239 if (PACK_EXPANSION_P (targ))
8240 /* Look at the pattern of the pack expansion. */
8241 targ = PACK_EXPANSION_PATTERN (targ);
8242
8243 /* Extract the template parameters from the template
8244 argument. */
8245 if (TREE_CODE (targ) == TEMPLATE_DECL)
8246 targ_parms = DECL_TEMPLATE_PARMS (targ);
8247 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8248 targ_parms = DECL_TEMPLATE_PARMS (TYPE_NAME (targ));
8249
8250 /* Verify that we can coerce the template template
8251 parameters from the template argument to the template
8252 parameter. This requires an exact match. */
8253 if (targ_parms
8254 && !coerce_template_template_parms
8255 (DECL_TEMPLATE_PARMS (tparm),
8256 targ_parms,
8257 tf_none,
8258 tparm,
8259 targs))
8260 {
8261 ret = false;
8262 goto out;
8263 }
8264 }
8265 }
8266 }
8267
8268 out:
8269
8270 --processing_template_decl;
8271 return ret;
8272 }
8273
8274 /* Since type attributes aren't mangled, we need to strip them from
8275 template type arguments. */
8276
8277 tree
8278 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8279 {
8280 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8281 return arg;
8282 bool removed_attributes = false;
8283 tree canon = strip_typedefs (arg, &removed_attributes);
8284 if (removed_attributes
8285 && (complain & tf_warning))
8286 warning (OPT_Wignored_attributes,
8287 "ignoring attributes on template argument %qT", arg);
8288 return canon;
8289 }
8290
8291 /* And from inside dependent non-type arguments like sizeof(Type). */
8292
8293 static tree
8294 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8295 {
8296 if (!arg || arg == error_mark_node)
8297 return arg;
8298 bool removed_attributes = false;
8299 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8300 if (removed_attributes
8301 && (complain & tf_warning))
8302 warning (OPT_Wignored_attributes,
8303 "ignoring attributes in template argument %qE", arg);
8304 return canon;
8305 }
8306
8307 /* A template declaration can be substituted for a constrained
8308 template template parameter only when the argument is no more
8309 constrained than the parameter. */
8310
8311 static bool
8312 is_compatible_template_arg (tree parm, tree arg)
8313 {
8314 tree parm_cons = get_constraints (parm);
8315
8316 /* For now, allow constrained template template arguments
8317 and unconstrained template template parameters. */
8318 if (parm_cons == NULL_TREE)
8319 return true;
8320
8321 /* If the template parameter is constrained, we need to rewrite its
8322 constraints in terms of the ARG's template parameters. This ensures
8323 that all of the template parameter types will have the same depth.
8324
8325 Note that this is only valid when coerce_template_template_parm is
8326 true for the innermost template parameters of PARM and ARG. In other
8327 words, because coercion is successful, this conversion will be valid. */
8328 tree new_args = NULL_TREE;
8329 if (parm_cons)
8330 {
8331 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8332 new_args = template_parms_level_to_args (aparms);
8333 ++processing_template_decl;
8334 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8335 tf_none, NULL_TREE);
8336 --processing_template_decl;
8337 if (parm_cons == error_mark_node)
8338 return false;
8339 }
8340
8341 return weakly_subsumes (parm_cons, arg);
8342 }
8343
8344 // Convert a placeholder argument into a binding to the original
8345 // parameter. The original parameter is saved as the TREE_TYPE of
8346 // ARG.
8347 static inline tree
8348 convert_wildcard_argument (tree parm, tree arg)
8349 {
8350 TREE_TYPE (arg) = parm;
8351 return arg;
8352 }
8353
8354 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8355 because one of them is dependent. But we need to represent the
8356 conversion for the benefit of cp_tree_equal. */
8357
8358 static tree
8359 maybe_convert_nontype_argument (tree type, tree arg)
8360 {
8361 /* Auto parms get no conversion. */
8362 if (type_uses_auto (type))
8363 return arg;
8364 /* We don't need or want to add this conversion now if we're going to use the
8365 argument for deduction. */
8366 if (value_dependent_expression_p (arg))
8367 return arg;
8368
8369 type = cv_unqualified (type);
8370 tree argtype = TREE_TYPE (arg);
8371 if (same_type_p (type, argtype))
8372 return arg;
8373
8374 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8375 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8376 return arg;
8377 }
8378
8379 /* Convert the indicated template ARG as necessary to match the
8380 indicated template PARM. Returns the converted ARG, or
8381 error_mark_node if the conversion was unsuccessful. Error and
8382 warning messages are issued under control of COMPLAIN. This
8383 conversion is for the Ith parameter in the parameter list. ARGS is
8384 the full set of template arguments deduced so far. */
8385
8386 static tree
8387 convert_template_argument (tree parm,
8388 tree arg,
8389 tree args,
8390 tsubst_flags_t complain,
8391 int i,
8392 tree in_decl)
8393 {
8394 tree orig_arg;
8395 tree val;
8396 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8397
8398 if (parm == error_mark_node || error_operand_p (arg))
8399 return error_mark_node;
8400
8401 /* Trivially convert placeholders. */
8402 if (TREE_CODE (arg) == WILDCARD_DECL)
8403 return convert_wildcard_argument (parm, arg);
8404
8405 if (arg == any_targ_node)
8406 return arg;
8407
8408 if (TREE_CODE (arg) == TREE_LIST
8409 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8410 {
8411 /* The template argument was the name of some
8412 member function. That's usually
8413 invalid, but static members are OK. In any
8414 case, grab the underlying fields/functions
8415 and issue an error later if required. */
8416 TREE_TYPE (arg) = unknown_type_node;
8417 }
8418
8419 orig_arg = arg;
8420
8421 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8422 requires_type = (TREE_CODE (parm) == TYPE_DECL
8423 || requires_tmpl_type);
8424
8425 /* When determining whether an argument pack expansion is a template,
8426 look at the pattern. */
8427 if (PACK_EXPANSION_P (arg))
8428 arg = PACK_EXPANSION_PATTERN (arg);
8429
8430 /* Deal with an injected-class-name used as a template template arg. */
8431 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8432 {
8433 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8434 if (TREE_CODE (t) == TEMPLATE_DECL)
8435 {
8436 if (cxx_dialect >= cxx11)
8437 /* OK under DR 1004. */;
8438 else if (complain & tf_warning_or_error)
8439 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8440 " used as template template argument", TYPE_NAME (arg));
8441 else if (flag_pedantic_errors)
8442 t = arg;
8443
8444 arg = t;
8445 }
8446 }
8447
8448 is_tmpl_type =
8449 ((TREE_CODE (arg) == TEMPLATE_DECL
8450 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8451 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8452 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8453 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8454
8455 if (is_tmpl_type
8456 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8457 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8458 arg = TYPE_STUB_DECL (arg);
8459
8460 is_type = TYPE_P (arg) || is_tmpl_type;
8461
8462 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8463 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8464 {
8465 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8466 {
8467 if (complain & tf_error)
8468 error ("invalid use of destructor %qE as a type", orig_arg);
8469 return error_mark_node;
8470 }
8471
8472 permerror (input_location,
8473 "to refer to a type member of a template parameter, "
8474 "use %<typename %E%>", orig_arg);
8475
8476 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8477 TREE_OPERAND (arg, 1),
8478 typename_type,
8479 complain);
8480 arg = orig_arg;
8481 is_type = 1;
8482 }
8483 if (is_type != requires_type)
8484 {
8485 if (in_decl)
8486 {
8487 if (complain & tf_error)
8488 {
8489 error ("type/value mismatch at argument %d in template "
8490 "parameter list for %qD",
8491 i + 1, in_decl);
8492 if (is_type)
8493 {
8494 /* The template argument is a type, but we're expecting
8495 an expression. */
8496 inform (input_location,
8497 " expected a constant of type %qT, got %qT",
8498 TREE_TYPE (parm),
8499 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8500 /* [temp.arg]/2: "In a template-argument, an ambiguity
8501 between a type-id and an expression is resolved to a
8502 type-id, regardless of the form of the corresponding
8503 template-parameter." So give the user a clue. */
8504 if (TREE_CODE (arg) == FUNCTION_TYPE)
8505 inform (input_location, " ambiguous template argument "
8506 "for non-type template parameter is treated as "
8507 "function type");
8508 }
8509 else if (requires_tmpl_type)
8510 inform (input_location,
8511 " expected a class template, got %qE", orig_arg);
8512 else
8513 inform (input_location,
8514 " expected a type, got %qE", orig_arg);
8515 }
8516 }
8517 return error_mark_node;
8518 }
8519 if (is_tmpl_type ^ requires_tmpl_type)
8520 {
8521 if (in_decl && (complain & tf_error))
8522 {
8523 error ("type/value mismatch at argument %d in template "
8524 "parameter list for %qD",
8525 i + 1, in_decl);
8526 if (is_tmpl_type)
8527 inform (input_location,
8528 " expected a type, got %qT", DECL_NAME (arg));
8529 else
8530 inform (input_location,
8531 " expected a class template, got %qT", orig_arg);
8532 }
8533 return error_mark_node;
8534 }
8535
8536 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8537 /* We already did the appropriate conversion when packing args. */
8538 val = orig_arg;
8539 else if (is_type)
8540 {
8541 if (requires_tmpl_type)
8542 {
8543 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8544 /* The number of argument required is not known yet.
8545 Just accept it for now. */
8546 val = orig_arg;
8547 else
8548 {
8549 tree parmparm = DECL_TEMPLATE_PARMS (parm);
8550 tree argparm;
8551
8552 /* Strip alias templates that are equivalent to another
8553 template. */
8554 arg = get_underlying_template (arg);
8555 argparm = DECL_TEMPLATE_PARMS (arg);
8556
8557 if (coerce_template_template_parms (parmparm, argparm,
8558 complain, in_decl,
8559 args))
8560 {
8561 val = arg;
8562
8563 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8564 TEMPLATE_DECL. */
8565 if (val != error_mark_node)
8566 {
8567 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8568 val = TREE_TYPE (val);
8569 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8570 val = make_pack_expansion (val, complain);
8571 }
8572 }
8573 else
8574 {
8575 if (in_decl && (complain & tf_error))
8576 {
8577 error ("type/value mismatch at argument %d in "
8578 "template parameter list for %qD",
8579 i + 1, in_decl);
8580 inform (input_location,
8581 " expected a template of type %qD, got %qT",
8582 parm, orig_arg);
8583 }
8584
8585 val = error_mark_node;
8586 }
8587
8588 // Check that the constraints are compatible before allowing the
8589 // substitution.
8590 if (val != error_mark_node)
8591 if (!is_compatible_template_arg (parm, arg))
8592 {
8593 if (in_decl && (complain & tf_error))
8594 {
8595 error ("constraint mismatch at argument %d in "
8596 "template parameter list for %qD",
8597 i + 1, in_decl);
8598 inform (input_location, " expected %qD but got %qD",
8599 parm, arg);
8600 }
8601 val = error_mark_node;
8602 }
8603 }
8604 }
8605 else
8606 val = orig_arg;
8607 /* We only form one instance of each template specialization.
8608 Therefore, if we use a non-canonical variant (i.e., a
8609 typedef), any future messages referring to the type will use
8610 the typedef, which is confusing if those future uses do not
8611 themselves also use the typedef. */
8612 if (TYPE_P (val))
8613 val = canonicalize_type_argument (val, complain);
8614 }
8615 else
8616 {
8617 tree t = TREE_TYPE (parm);
8618
8619 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8620 > TMPL_ARGS_DEPTH (args))
8621 /* We don't have enough levels of args to do any substitution. This
8622 can happen in the context of -fnew-ttp-matching. */;
8623 else if (tree a = type_uses_auto (t))
8624 {
8625 t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
8626 LOOKUP_IMPLICIT);
8627 if (t == error_mark_node)
8628 return error_mark_node;
8629 }
8630 else
8631 t = tsubst (t, args, complain, in_decl);
8632
8633 /* Perform array-to-pointer and function-to-pointer conversion
8634 as per [temp.param]/10. */
8635 t = type_decays_to (t);
8636
8637 if (invalid_nontype_parm_type_p (t, complain))
8638 return error_mark_node;
8639
8640 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8641 this non-type template parameter, as per [temp.param]/6. */
8642 t = cv_unqualified (t);
8643
8644 if (t != TREE_TYPE (parm))
8645 t = canonicalize_type_argument (t, complain);
8646
8647 if (!type_dependent_expression_p (orig_arg)
8648 && !uses_template_parms (t))
8649 /* We used to call digest_init here. However, digest_init
8650 will report errors, which we don't want when complain
8651 is zero. More importantly, digest_init will try too
8652 hard to convert things: for example, `0' should not be
8653 converted to pointer type at this point according to
8654 the standard. Accepting this is not merely an
8655 extension, since deciding whether or not these
8656 conversions can occur is part of determining which
8657 function template to call, or whether a given explicit
8658 argument specification is valid. */
8659 val = convert_nontype_argument (t, orig_arg, complain);
8660 else
8661 {
8662 val = canonicalize_expr_argument (orig_arg, complain);
8663 val = maybe_convert_nontype_argument (t, val);
8664 }
8665
8666
8667 if (val == NULL_TREE)
8668 val = error_mark_node;
8669 else if (val == error_mark_node && (complain & tf_error))
8670 error_at (cp_expr_loc_or_input_loc (orig_arg),
8671 "could not convert template argument %qE from %qT to %qT",
8672 orig_arg, TREE_TYPE (orig_arg), t);
8673
8674 if (INDIRECT_REF_P (val))
8675 {
8676 /* Reject template arguments that are references to built-in
8677 functions with no library fallbacks. */
8678 const_tree inner = TREE_OPERAND (val, 0);
8679 const_tree innertype = TREE_TYPE (inner);
8680 if (innertype
8681 && TYPE_REF_P (innertype)
8682 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8683 && TREE_OPERAND_LENGTH (inner) > 0
8684 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8685 return error_mark_node;
8686 }
8687
8688 if (TREE_CODE (val) == SCOPE_REF)
8689 {
8690 /* Strip typedefs from the SCOPE_REF. */
8691 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8692 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8693 complain);
8694 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8695 QUALIFIED_NAME_IS_TEMPLATE (val));
8696 }
8697 }
8698
8699 return val;
8700 }
8701
8702 /* Coerces the remaining template arguments in INNER_ARGS (from
8703 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8704 Returns the coerced argument pack. PARM_IDX is the position of this
8705 parameter in the template parameter list. ARGS is the original
8706 template argument list. */
8707 static tree
8708 coerce_template_parameter_pack (tree parms,
8709 int parm_idx,
8710 tree args,
8711 tree inner_args,
8712 int arg_idx,
8713 tree new_args,
8714 int* lost,
8715 tree in_decl,
8716 tsubst_flags_t complain)
8717 {
8718 tree parm = TREE_VEC_ELT (parms, parm_idx);
8719 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8720 tree packed_args;
8721 tree argument_pack;
8722 tree packed_parms = NULL_TREE;
8723
8724 if (arg_idx > nargs)
8725 arg_idx = nargs;
8726
8727 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8728 {
8729 /* When the template parameter is a non-type template parameter pack
8730 or template template parameter pack whose type or template
8731 parameters use parameter packs, we know exactly how many arguments
8732 we are looking for. Build a vector of the instantiated decls for
8733 these template parameters in PACKED_PARMS. */
8734 /* We can't use make_pack_expansion here because it would interpret a
8735 _DECL as a use rather than a declaration. */
8736 tree decl = TREE_VALUE (parm);
8737 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8738 PACK_EXPANSION_PATTERN (exp) = decl;
8739 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8740 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8741
8742 TREE_VEC_LENGTH (args)--;
8743 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8744 TREE_VEC_LENGTH (args)++;
8745
8746 if (packed_parms == error_mark_node)
8747 return error_mark_node;
8748
8749 /* If we're doing a partial instantiation of a member template,
8750 verify that all of the types used for the non-type
8751 template parameter pack are, in fact, valid for non-type
8752 template parameters. */
8753 if (arg_idx < nargs
8754 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8755 {
8756 int j, len = TREE_VEC_LENGTH (packed_parms);
8757 for (j = 0; j < len; ++j)
8758 {
8759 tree t = TREE_VEC_ELT (packed_parms, j);
8760 if (TREE_CODE (t) == PARM_DECL
8761 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8762 return error_mark_node;
8763 }
8764 /* We don't know how many args we have yet, just
8765 use the unconverted ones for now. */
8766 return NULL_TREE;
8767 }
8768
8769 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8770 }
8771 /* Check if we have a placeholder pack, which indicates we're
8772 in the context of a introduction list. In that case we want
8773 to match this pack to the single placeholder. */
8774 else if (arg_idx < nargs
8775 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8776 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8777 {
8778 nargs = arg_idx + 1;
8779 packed_args = make_tree_vec (1);
8780 }
8781 else
8782 packed_args = make_tree_vec (nargs - arg_idx);
8783
8784 /* Convert the remaining arguments, which will be a part of the
8785 parameter pack "parm". */
8786 int first_pack_arg = arg_idx;
8787 for (; arg_idx < nargs; ++arg_idx)
8788 {
8789 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8790 tree actual_parm = TREE_VALUE (parm);
8791 int pack_idx = arg_idx - first_pack_arg;
8792
8793 if (packed_parms)
8794 {
8795 /* Once we've packed as many args as we have types, stop. */
8796 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8797 break;
8798 else if (PACK_EXPANSION_P (arg))
8799 /* We don't know how many args we have yet, just
8800 use the unconverted ones for now. */
8801 return NULL_TREE;
8802 else
8803 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8804 }
8805
8806 if (arg == error_mark_node)
8807 {
8808 if (complain & tf_error)
8809 error ("template argument %d is invalid", arg_idx + 1);
8810 }
8811 else
8812 arg = convert_template_argument (actual_parm,
8813 arg, new_args, complain, parm_idx,
8814 in_decl);
8815 if (arg == error_mark_node)
8816 (*lost)++;
8817 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8818 }
8819
8820 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8821 && TREE_VEC_LENGTH (packed_args) > 0)
8822 {
8823 if (complain & tf_error)
8824 error ("wrong number of template arguments (%d, should be %d)",
8825 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8826 return error_mark_node;
8827 }
8828
8829 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8830 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8831 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8832 else
8833 {
8834 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8835 TREE_CONSTANT (argument_pack) = 1;
8836 }
8837
8838 ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
8839 if (CHECKING_P)
8840 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8841 TREE_VEC_LENGTH (packed_args));
8842 return argument_pack;
8843 }
8844
8845 /* Returns the number of pack expansions in the template argument vector
8846 ARGS. */
8847
8848 static int
8849 pack_expansion_args_count (tree args)
8850 {
8851 int i;
8852 int count = 0;
8853 if (args)
8854 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8855 {
8856 tree elt = TREE_VEC_ELT (args, i);
8857 if (elt && PACK_EXPANSION_P (elt))
8858 ++count;
8859 }
8860 return count;
8861 }
8862
8863 /* Convert all template arguments to their appropriate types, and
8864 return a vector containing the innermost resulting template
8865 arguments. If any error occurs, return error_mark_node. Error and
8866 warning messages are issued under control of COMPLAIN.
8867
8868 If PARMS represents all template parameters levels, this function
8869 returns a vector of vectors representing all the resulting argument
8870 levels. Note that in this case, only the innermost arguments are
8871 coerced because the outermost ones are supposed to have been coerced
8872 already. Otherwise, if PARMS represents only (the innermost) vector
8873 of parameters, this function returns a vector containing just the
8874 innermost resulting arguments.
8875
8876 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8877 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true,
8878 arguments not specified in ARGS must have default arguments which
8879 we'll use to fill in ARGS. */
8880
8881 tree
8882 coerce_template_parms (tree parms,
8883 tree args,
8884 tree in_decl,
8885 tsubst_flags_t complain,
8886 bool require_all_args /* = true */)
8887 {
8888 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8889 tree orig_inner_args;
8890 tree inner_args;
8891
8892 /* When used as a boolean value, indicates whether this is a
8893 variadic template parameter list. Since it's an int, we can also
8894 subtract it from nparms to get the number of non-variadic
8895 parameters. */
8896 int variadic_p = 0;
8897 int variadic_args_p = 0;
8898 int post_variadic_parms = 0;
8899
8900 /* Adjustment to nparms for fixed parameter packs. */
8901 int fixed_pack_adjust = 0;
8902 int fixed_packs = 0;
8903 int missing = 0;
8904
8905 /* Likewise for parameters with default arguments. */
8906 int default_p = 0;
8907
8908 if (args == error_mark_node)
8909 return error_mark_node;
8910
8911 bool return_full_args = false;
8912 if (TREE_CODE (parms) == TREE_LIST)
8913 {
8914 if (TMPL_PARMS_DEPTH (parms) > 1)
8915 {
8916 gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
8917 return_full_args = true;
8918 }
8919 parms = INNERMOST_TEMPLATE_PARMS (parms);
8920 }
8921
8922 nparms = TREE_VEC_LENGTH (parms);
8923
8924 /* Determine if there are any parameter packs or default arguments. */
8925 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8926 {
8927 tree parm = TREE_VEC_ELT (parms, parm_idx);
8928 if (variadic_p)
8929 ++post_variadic_parms;
8930 if (template_parameter_pack_p (TREE_VALUE (parm)))
8931 ++variadic_p;
8932 if (TREE_PURPOSE (parm))
8933 ++default_p;
8934 }
8935
8936 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8937 /* If there are no parameters that follow a parameter pack, we need to
8938 expand any argument packs so that we can deduce a parameter pack from
8939 some non-packed args followed by an argument pack, as in variadic85.C.
8940 If there are such parameters, we need to leave argument packs intact
8941 so the arguments are assigned properly. This can happen when dealing
8942 with a nested class inside a partial specialization of a class
8943 template, as in variadic92.C, or when deducing a template parameter pack
8944 from a sub-declarator, as in variadic114.C. */
8945 if (!post_variadic_parms)
8946 inner_args = expand_template_argument_pack (inner_args);
8947
8948 /* Count any pack expansion args. */
8949 variadic_args_p = pack_expansion_args_count (inner_args);
8950
8951 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8952 if ((nargs - variadic_args_p > nparms && !variadic_p)
8953 || (nargs < nparms - variadic_p
8954 && require_all_args
8955 && !variadic_args_p
8956 && (TREE_VEC_ELT (parms, nargs) != error_mark_node
8957 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
8958 {
8959 bad_nargs:
8960 if (complain & tf_error)
8961 {
8962 if (variadic_p || default_p)
8963 {
8964 nparms -= variadic_p + default_p;
8965 error ("wrong number of template arguments "
8966 "(%d, should be at least %d)", nargs, nparms);
8967 }
8968 else
8969 error ("wrong number of template arguments "
8970 "(%d, should be %d)", nargs, nparms);
8971
8972 if (in_decl)
8973 inform (DECL_SOURCE_LOCATION (in_decl),
8974 "provided for %qD", in_decl);
8975 }
8976
8977 return error_mark_node;
8978 }
8979 /* We can't pass a pack expansion to a non-pack parameter of an alias
8980 template (DR 1430). */
8981 else if (in_decl
8982 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8983 || concept_definition_p (in_decl))
8984 && variadic_args_p
8985 && nargs - variadic_args_p < nparms - variadic_p)
8986 {
8987 if (complain & tf_error)
8988 {
8989 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8990 {
8991 tree arg = TREE_VEC_ELT (inner_args, i);
8992 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8993
8994 if (PACK_EXPANSION_P (arg)
8995 && !template_parameter_pack_p (parm))
8996 {
8997 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8998 error_at (location_of (arg),
8999 "pack expansion argument for non-pack parameter "
9000 "%qD of alias template %qD", parm, in_decl);
9001 else
9002 error_at (location_of (arg),
9003 "pack expansion argument for non-pack parameter "
9004 "%qD of concept %qD", parm, in_decl);
9005 inform (DECL_SOURCE_LOCATION (parm), "declared here");
9006 goto found;
9007 }
9008 }
9009 gcc_unreachable ();
9010 found:;
9011 }
9012 return error_mark_node;
9013 }
9014
9015 /* We need to evaluate the template arguments, even though this
9016 template-id may be nested within a "sizeof". */
9017 cp_evaluated ev;
9018
9019 tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
9020 tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
9021 int pack_adjust = 0;
9022 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
9023 {
9024 tree arg;
9025 tree parm;
9026
9027 /* Get the Ith template parameter. */
9028 parm = TREE_VEC_ELT (parms, parm_idx);
9029
9030 if (parm == error_mark_node)
9031 {
9032 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
9033 continue;
9034 }
9035
9036 /* Calculate the next argument. */
9037 if (arg_idx < nargs)
9038 arg = TREE_VEC_ELT (inner_args, arg_idx);
9039 else
9040 arg = NULL_TREE;
9041
9042 if (template_parameter_pack_p (TREE_VALUE (parm))
9043 && (arg || require_all_args || !(complain & tf_partial))
9044 && !(arg && ARGUMENT_PACK_P (arg)))
9045 {
9046 /* Some arguments will be placed in the
9047 template parameter pack PARM. */
9048 arg = coerce_template_parameter_pack (parms, parm_idx, args,
9049 inner_args, arg_idx,
9050 new_args, &lost,
9051 in_decl, complain);
9052
9053 if (arg == NULL_TREE)
9054 {
9055 /* We don't know how many args we have yet, just use the
9056 unconverted (and still packed) ones for now. */
9057 new_inner_args = orig_inner_args;
9058 arg_idx = nargs;
9059 break;
9060 }
9061
9062 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
9063
9064 /* Store this argument. */
9065 if (arg == error_mark_node)
9066 {
9067 lost++;
9068 /* We are done with all of the arguments. */
9069 arg_idx = nargs;
9070 break;
9071 }
9072 else
9073 {
9074 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
9075 arg_idx += pack_adjust;
9076 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
9077 {
9078 ++fixed_packs;
9079 fixed_pack_adjust += pack_adjust;
9080 }
9081 }
9082
9083 continue;
9084 }
9085 else if (arg)
9086 {
9087 if (PACK_EXPANSION_P (arg))
9088 {
9089 /* "If every valid specialization of a variadic template
9090 requires an empty template parameter pack, the template is
9091 ill-formed, no diagnostic required." So check that the
9092 pattern works with this parameter. */
9093 tree pattern = PACK_EXPANSION_PATTERN (arg);
9094 tree conv = convert_template_argument (TREE_VALUE (parm),
9095 pattern, new_args,
9096 complain, parm_idx,
9097 in_decl);
9098 if (conv == error_mark_node)
9099 {
9100 if (complain & tf_error)
9101 inform (input_location, "so any instantiation with a "
9102 "non-empty parameter pack would be ill-formed");
9103 ++lost;
9104 }
9105 else if (TYPE_P (conv) && !TYPE_P (pattern))
9106 /* Recover from missing typename. */
9107 TREE_VEC_ELT (inner_args, arg_idx)
9108 = make_pack_expansion (conv, complain);
9109
9110 /* We don't know how many args we have yet, just
9111 use the unconverted ones for now. */
9112 new_inner_args = inner_args;
9113 arg_idx = nargs;
9114 break;
9115 }
9116 }
9117 else if (require_all_args)
9118 {
9119 /* There must be a default arg in this case. */
9120 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
9121 complain, in_decl);
9122 /* The position of the first default template argument,
9123 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
9124 Record that. */
9125 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9126 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9127 arg_idx - pack_adjust);
9128 }
9129 else
9130 break;
9131
9132 if (arg == error_mark_node)
9133 {
9134 if (complain & tf_error)
9135 error ("template argument %d is invalid", arg_idx + 1);
9136 }
9137 else if (!arg)
9138 {
9139 /* This can occur if there was an error in the template
9140 parameter list itself (which we would already have
9141 reported) that we are trying to recover from, e.g., a class
9142 template with a parameter list such as
9143 template<typename..., typename> (cpp0x/variadic150.C). */
9144 ++lost;
9145
9146 /* This can also happen with a fixed parameter pack (71834). */
9147 if (arg_idx >= nargs)
9148 ++missing;
9149 }
9150 else
9151 arg = convert_template_argument (TREE_VALUE (parm),
9152 arg, new_args, complain,
9153 parm_idx, in_decl);
9154
9155 if (arg == error_mark_node)
9156 lost++;
9157
9158 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9159 }
9160
9161 if (missing || arg_idx < nargs - variadic_args_p)
9162 {
9163 /* If we had fixed parameter packs, we didn't know how many arguments we
9164 actually needed earlier; now we do. */
9165 nparms += fixed_pack_adjust;
9166 variadic_p -= fixed_packs;
9167 goto bad_nargs;
9168 }
9169
9170 if (arg_idx < nargs)
9171 {
9172 /* We had some pack expansion arguments that will only work if the packs
9173 are empty, but wait until instantiation time to complain.
9174 See variadic-ttp3.C. */
9175
9176 /* Except that we can't provide empty packs to alias templates or
9177 concepts when there are no corresponding parameters. Basically,
9178 we can get here with this:
9179
9180 template<typename T> concept C = true;
9181
9182 template<typename... Args>
9183 requires C<Args...>
9184 void f();
9185
9186 When parsing C<Args...>, we try to form a concept check of
9187 C<?, Args...>. Without the extra check for substituting an empty
9188 pack past the last parameter, we can accept the check as valid.
9189
9190 FIXME: This may be valid for alias templates (but I doubt it).
9191
9192 FIXME: The error could be better also. */
9193 if (in_decl && concept_definition_p (in_decl))
9194 {
9195 if (complain & tf_error)
9196 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9197 "too many arguments");
9198 return error_mark_node;
9199 }
9200
9201 int len = nparms + (nargs - arg_idx);
9202 tree args = make_tree_vec (len);
9203 int i = 0;
9204 for (; i < nparms; ++i)
9205 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9206 for (; i < len; ++i, ++arg_idx)
9207 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9208 arg_idx - pack_adjust);
9209 new_inner_args = args;
9210 }
9211
9212 if (lost)
9213 {
9214 gcc_assert (!(complain & tf_error) || seen_error ());
9215 return error_mark_node;
9216 }
9217
9218 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9220 TREE_VEC_LENGTH (new_inner_args));
9221
9222 return return_full_args ? new_args : new_inner_args;
9223 }
9224
9225 /* Returns true if T is a wrapper to make a C++20 template parameter
9226 object const. */
9227
9228 static bool
9229 class_nttp_const_wrapper_p (tree t)
9230 {
9231 if (cxx_dialect < cxx20)
9232 return false;
9233 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9234 && CP_TYPE_CONST_P (TREE_TYPE (t))
9235 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9236 }
9237
9238 /* Returns 1 if template args OT and NT are equivalent. */
9239
9240 int
9241 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9242 {
9243 if (nt == ot)
9244 return 1;
9245 if (nt == NULL_TREE || ot == NULL_TREE)
9246 return false;
9247 if (nt == any_targ_node || ot == any_targ_node)
9248 return true;
9249
9250 if (class_nttp_const_wrapper_p (nt))
9251 nt = TREE_OPERAND (nt, 0);
9252 if (class_nttp_const_wrapper_p (ot))
9253 ot = TREE_OPERAND (ot, 0);
9254
9255 /* DR 1558: Don't treat an alias template specialization with dependent
9256 arguments as equivalent to its underlying type when used as a template
9257 argument; we need them to be distinct so that we substitute into the
9258 specialization arguments at instantiation time. And aliases can't be
9259 equivalent without being ==, so we don't need to look any deeper.
9260
9261 During partial ordering, however, we need to treat them normally so we can
9262 order uses of the same alias with different cv-qualification (79960). */
9263 auto cso = make_temp_override (comparing_dependent_aliases);
9264 if (!partial_order)
9265 ++comparing_dependent_aliases;
9266
9267 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9268 /* For member templates */
9269 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9270 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9271 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9272 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9273 PACK_EXPANSION_PATTERN (nt))
9274 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9275 PACK_EXPANSION_EXTRA_ARGS (nt)));
9276 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9277 return cp_tree_equal (ot, nt);
9278 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9279 gcc_unreachable ();
9280 else if (TYPE_P (nt) || TYPE_P (ot))
9281 {
9282 if (!(TYPE_P (nt) && TYPE_P (ot)))
9283 return false;
9284 return same_type_p (ot, nt);
9285 }
9286 else
9287 {
9288 /* Try to treat a template non-type argument that has been converted
9289 to the parameter type as equivalent to one that hasn't yet. */
9290 for (enum tree_code code1 = TREE_CODE (ot);
9291 CONVERT_EXPR_CODE_P (code1)
9292 || code1 == NON_LVALUE_EXPR;
9293 code1 = TREE_CODE (ot))
9294 ot = TREE_OPERAND (ot, 0);
9295
9296 for (enum tree_code code2 = TREE_CODE (nt);
9297 CONVERT_EXPR_CODE_P (code2)
9298 || code2 == NON_LVALUE_EXPR;
9299 code2 = TREE_CODE (nt))
9300 nt = TREE_OPERAND (nt, 0);
9301
9302 return cp_tree_equal (ot, nt);
9303 }
9304 }
9305
9306 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
9307 template arguments. Returns false otherwise, and updates OLDARG_PTR and
9308 NEWARG_PTR with the offending arguments if they are non-NULL. */
9309
9310 bool
9311 comp_template_args (tree oldargs, tree newargs,
9312 tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */,
9313 bool partial_order /* = false */)
9314 {
9315 if (oldargs == newargs)
9316 return true;
9317
9318 if (!oldargs || !newargs)
9319 return false;
9320
9321 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9322 return false;
9323
9324 for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9325 {
9326 tree nt = TREE_VEC_ELT (newargs, i);
9327 tree ot = TREE_VEC_ELT (oldargs, i);
9328
9329 if (! template_args_equal (ot, nt, partial_order))
9330 {
9331 if (oldarg_ptr != NULL)
9332 *oldarg_ptr = ot;
9333 if (newarg_ptr != NULL)
9334 *newarg_ptr = nt;
9335 return false;
9336 }
9337 }
9338 return true;
9339 }
9340
9341 inline bool
9342 comp_template_args_porder (tree oargs, tree nargs)
9343 {
9344 return comp_template_args (oargs, nargs, NULL, NULL, true);
9345 }
9346
9347 /* Implement a freelist interface for objects of type T.
9348
9349 Head is a separate object, rather than a regular member, so that we
9350 can define it as a GTY deletable pointer, which is highly
9351 desirable. A data member could be declared that way, but then the
9352 containing object would implicitly get GTY((user)), which would
9353 prevent us from instantiating freelists as global objects.
9354 Although this way we can create freelist global objects, they're
9355 such thin wrappers that instantiating temporaries at every use
9356 loses nothing and saves permanent storage for the freelist object.
9357
9358 Member functions next, anew, poison and reinit have default
9359 implementations that work for most of the types we're interested
9360 in, but if they don't work for some type, they should be explicitly
9361 specialized. See the comments before them for requirements, and
9362 the example specializations for the tree_list_freelist. */
9363 template <typename T>
9364 class freelist
9365 {
9366 /* Return the next object in a chain. We could just do type
9367 punning, but if we access the object with its underlying type, we
9368 avoid strict-aliasing trouble. This needs only work between
9369 poison and reinit. */
9370 static T *&next (T *obj) { return obj->next; }
9371
9372 /* Return a newly allocated, uninitialized or minimally-initialized
9373 object of type T. Any initialization performed by anew should
9374 either remain across the life of the object and the execution of
9375 poison, or be redone by reinit. */
9376 static T *anew () { return ggc_alloc<T> (); }
9377
9378 /* Optionally scribble all over the bits holding the object, so that
9379 they become (mostly?) uninitialized memory. This is called while
9380 preparing to make the object part of the free list. */
9381 static void poison (T *obj) {
9382 T *p ATTRIBUTE_UNUSED = obj;
9383 T **q ATTRIBUTE_UNUSED = &next (obj);
9384
9385 #ifdef ENABLE_GC_CHECKING
9386 /* Poison the data, to indicate the data is garbage. */
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9388 memset (p, 0xa5, sizeof (*p));
9389 #endif
9390 /* Let valgrind know the object is free. */
9391 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9392
9393 /* Let valgrind know the next portion of the object is available,
9394 but uninitialized. */
9395 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9396 }
9397
9398 /* Bring an object that underwent at least one lifecycle after anew
9399 and before the most recent free and poison, back to a usable
9400 state, reinitializing whatever is needed for it to be
9401 functionally equivalent to an object just allocated and returned
9402 by anew. This may poison or clear the next field, used by
9403 freelist housekeeping after poison was called. */
9404 static void reinit (T *obj) {
9405 T **q ATTRIBUTE_UNUSED = &next (obj);
9406
9407 #ifdef ENABLE_GC_CHECKING
9408 memset (q, 0xa5, sizeof (*q));
9409 #endif
9410 /* Let valgrind know the entire object is available, but
9411 uninitialized. */
9412 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9413 }
9414
9415 /* Reference a GTY-deletable pointer that points to the first object
9416 in the free list proper. */
9417 T *&head;
9418 public:
9419 /* Construct a freelist object chaining objects off of HEAD. */
9420 freelist (T *&head) : head(head) {}
9421
9422 /* Add OBJ to the free object list. The former head becomes OBJ's
9423 successor. */
9424 void free (T *obj)
9425 {
9426 poison (obj);
9427 next (obj) = head;
9428 head = obj;
9429 }
9430
9431 /* Take an object from the free list, if one is available, or
9432 allocate a new one. Objects taken from the free list should be
9433 regarded as filled with garbage, except for bits that are
9434 configured to be preserved across free and alloc. */
9435 T *alloc ()
9436 {
9437 if (head)
9438 {
9439 T *obj = head;
9440 head = next (head);
9441 reinit (obj);
9442 return obj;
9443 }
9444 else
9445 return anew ();
9446 }
9447 };
9448
9449 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9450 want to allocate a TREE_LIST using the usual interface, and ensure
9451 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9452 build_tree_list logic in reinit, so this could go out of sync. */
9453 template <>
9454 inline tree &
9455 freelist<tree_node>::next (tree obj)
9456 {
9457 return TREE_CHAIN (obj);
9458 }
9459 template <>
9460 inline tree
9461 freelist<tree_node>::anew ()
9462 {
9463 return build_tree_list (NULL, NULL);
9464 }
9465 template <>
9466 inline void
9467 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9468 {
9469 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9470 tree p ATTRIBUTE_UNUSED = obj;
9471 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9472 tree *q ATTRIBUTE_UNUSED = &next (obj);
9473
9474 #ifdef ENABLE_GC_CHECKING
9475 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9476
9477 /* Poison the data, to indicate the data is garbage. */
9478 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9479 memset (p, 0xa5, size);
9480 #endif
9481 /* Let valgrind know the object is free. */
9482 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9483 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9484 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9485 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9486
9487 #ifdef ENABLE_GC_CHECKING
9488 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9489 /* Keep TREE_CHAIN functional. */
9490 TREE_SET_CODE (obj, TREE_LIST);
9491 #else
9492 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9493 #endif
9494 }
9495 template <>
9496 inline void
9497 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9498 {
9499 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9500
9501 #ifdef ENABLE_GC_CHECKING
9502 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9503 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9504 memset (obj, 0, sizeof (tree_list));
9505 #endif
9506
9507 /* Let valgrind know the entire object is available, but
9508 uninitialized. */
9509 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9510
9511 #ifdef ENABLE_GC_CHECKING
9512 TREE_SET_CODE (obj, TREE_LIST);
9513 #else
9514 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9515 #endif
9516 }
9517
9518 /* Point to the first object in the TREE_LIST freelist. */
9519 static GTY((deletable)) tree tree_list_freelist_head;
9520 /* Return the/an actual TREE_LIST freelist. */
9521 static inline freelist<tree_node>
9522 tree_list_freelist ()
9523 {
9524 return tree_list_freelist_head;
9525 }
9526
9527 /* Point to the first object in the tinst_level freelist. */
9528 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9529 /* Return the/an actual tinst_level freelist. */
9530 static inline freelist<tinst_level>
9531 tinst_level_freelist ()
9532 {
9533 return tinst_level_freelist_head;
9534 }
9535
9536 /* Point to the first object in the pending_template freelist. */
9537 static GTY((deletable)) pending_template *pending_template_freelist_head;
9538 /* Return the/an actual pending_template freelist. */
9539 static inline freelist<pending_template>
9540 pending_template_freelist ()
9541 {
9542 return pending_template_freelist_head;
9543 }
9544
9545 /* Build the TREE_LIST object out of a split list, store it
9546 permanently, and return it. */
9547 tree
9548 tinst_level::to_list ()
9549 {
9550 gcc_assert (split_list_p ());
9551 tree ret = tree_list_freelist ().alloc ();
9552 TREE_PURPOSE (ret) = tldcl;
9553 TREE_VALUE (ret) = targs;
9554 tldcl = ret;
9555 targs = NULL;
9556 gcc_assert (tree_list_p ());
9557 return ret;
9558 }
9559
9560 const unsigned short tinst_level::refcount_infinity;
9561
9562 /* Increment OBJ's refcount unless it is already infinite. */
9563 static tinst_level *
9564 inc_refcount_use (tinst_level *obj)
9565 {
9566 if (obj && obj->refcount != tinst_level::refcount_infinity)
9567 ++obj->refcount;
9568 return obj;
9569 }
9570
9571 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9572 void
9573 tinst_level::free (tinst_level *obj)
9574 {
9575 if (obj->tree_list_p ())
9576 tree_list_freelist ().free (obj->get_node ());
9577 tinst_level_freelist ().free (obj);
9578 }
9579
9580 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9581 OBJ's DECL and OBJ, and start over with the tinst_level object that
9582 used to be referenced by OBJ's NEXT. */
9583 static void
9584 dec_refcount_use (tinst_level *obj)
9585 {
9586 while (obj
9587 && obj->refcount != tinst_level::refcount_infinity
9588 && !--obj->refcount)
9589 {
9590 tinst_level *next = obj->next;
9591 tinst_level::free (obj);
9592 obj = next;
9593 }
9594 }
9595
9596 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9597 and of the former PTR. Omitting the second argument is equivalent
9598 to passing (T*)NULL; this is allowed because passing the
9599 zero-valued integral constant NULL confuses type deduction and/or
9600 overload resolution. */
9601 template <typename T>
9602 static void
9603 set_refcount_ptr (T *& ptr, T *obj = NULL)
9604 {
9605 T *save = ptr;
9606 ptr = inc_refcount_use (obj);
9607 dec_refcount_use (save);
9608 }
9609
9610 static void
9611 add_pending_template (tree d)
9612 {
9613 tree ti = (TYPE_P (d)
9614 ? CLASSTYPE_TEMPLATE_INFO (d)
9615 : DECL_TEMPLATE_INFO (d));
9616 struct pending_template *pt;
9617 int level;
9618
9619 if (TI_PENDING_TEMPLATE_FLAG (ti))
9620 return;
9621
9622 /* We are called both from instantiate_decl, where we've already had a
9623 tinst_level pushed, and instantiate_template, where we haven't.
9624 Compensate. */
9625 gcc_assert (TREE_CODE (d) != TREE_LIST);
9626 level = !current_tinst_level
9627 || current_tinst_level->maybe_get_node () != d;
9628
9629 if (level)
9630 push_tinst_level (d);
9631
9632 pt = pending_template_freelist ().alloc ();
9633 pt->next = NULL;
9634 pt->tinst = NULL;
9635 set_refcount_ptr (pt->tinst, current_tinst_level);
9636 if (last_pending_template)
9637 last_pending_template->next = pt;
9638 else
9639 pending_templates = pt;
9640
9641 last_pending_template = pt;
9642
9643 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9644
9645 if (level)
9646 pop_tinst_level ();
9647 }
9648
9649
9650 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9651 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9652 documentation for TEMPLATE_ID_EXPR. */
9653
9654 tree
9655 lookup_template_function (tree fns, tree arglist)
9656 {
9657 if (fns == error_mark_node || arglist == error_mark_node)
9658 return error_mark_node;
9659
9660 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9661
9662 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9663 {
9664 error ("%q#D is not a function template", fns);
9665 return error_mark_node;
9666 }
9667
9668 if (BASELINK_P (fns))
9669 {
9670 fns = copy_node (fns);
9671 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9672 unknown_type_node,
9673 BASELINK_FUNCTIONS (fns),
9674 arglist);
9675 return fns;
9676 }
9677
9678 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9679 }
9680
9681 /* Within the scope of a template class S<T>, the name S gets bound
9682 (in build_self_reference) to a TYPE_DECL for the class, not a
9683 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9684 or one of its enclosing classes, and that type is a template,
9685 return the associated TEMPLATE_DECL. Otherwise, the original
9686 DECL is returned.
9687
9688 Also handle the case when DECL is a TREE_LIST of ambiguous
9689 injected-class-names from different bases. */
9690
9691 tree
9692 maybe_get_template_decl_from_type_decl (tree decl)
9693 {
9694 if (decl == NULL_TREE)
9695 return decl;
9696
9697 /* DR 176: A lookup that finds an injected-class-name (10.2
9698 [class.member.lookup]) can result in an ambiguity in certain cases
9699 (for example, if it is found in more than one base class). If all of
9700 the injected-class-names that are found refer to specializations of
9701 the same class template, and if the name is followed by a
9702 template-argument-list, the reference refers to the class template
9703 itself and not a specialization thereof, and is not ambiguous. */
9704 if (TREE_CODE (decl) == TREE_LIST)
9705 {
9706 tree t, tmpl = NULL_TREE;
9707 for (t = decl; t; t = TREE_CHAIN (t))
9708 {
9709 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9710 if (!tmpl)
9711 tmpl = elt;
9712 else if (tmpl != elt)
9713 break;
9714 }
9715 if (tmpl && t == NULL_TREE)
9716 return tmpl;
9717 else
9718 return decl;
9719 }
9720
9721 return (decl != NULL_TREE
9722 && DECL_SELF_REFERENCE_P (decl)
9723 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9724 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9725 }
9726
9727 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9728 parameters, find the desired type.
9729
9730 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9731
9732 IN_DECL, if non-NULL, is the template declaration we are trying to
9733 instantiate.
9734
9735 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9736 the class we are looking up.
9737
9738 Issue error and warning messages under control of COMPLAIN.
9739
9740 If the template class is really a local class in a template
9741 function, then the FUNCTION_CONTEXT is the function in which it is
9742 being instantiated.
9743
9744 ??? Note that this function is currently called *twice* for each
9745 template-id: the first time from the parser, while creating the
9746 incomplete type (finish_template_type), and the second type during the
9747 real instantiation (instantiate_template_class). This is surely something
9748 that we want to avoid. It also causes some problems with argument
9749 coercion (see convert_nontype_argument for more information on this). */
9750
9751 tree
9752 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9753 int entering_scope, tsubst_flags_t complain)
9754 {
9755 auto_timevar tv (TV_TEMPLATE_INST);
9756
9757 tree templ = NULL_TREE, parmlist;
9758 tree t;
9759 spec_entry **slot;
9760 spec_entry *entry;
9761 spec_entry elt;
9762 hashval_t hash;
9763
9764 if (identifier_p (d1))
9765 {
9766 tree value = innermost_non_namespace_value (d1);
9767 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9768 templ = value;
9769 else
9770 {
9771 if (context)
9772 push_decl_namespace (context);
9773 templ = lookup_name (d1);
9774 templ = maybe_get_template_decl_from_type_decl (templ);
9775 if (context)
9776 pop_decl_namespace ();
9777 }
9778 }
9779 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9780 {
9781 tree type = TREE_TYPE (d1);
9782
9783 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9784 an implicit typename for the second A. Deal with it. */
9785 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9786 type = TREE_TYPE (type);
9787
9788 if (CLASSTYPE_TEMPLATE_INFO (type))
9789 {
9790 templ = CLASSTYPE_TI_TEMPLATE (type);
9791 d1 = DECL_NAME (templ);
9792 }
9793 }
9794 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9795 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9796 {
9797 templ = TYPE_TI_TEMPLATE (d1);
9798 d1 = DECL_NAME (templ);
9799 }
9800 else if (DECL_TYPE_TEMPLATE_P (d1))
9801 {
9802 templ = d1;
9803 d1 = DECL_NAME (templ);
9804 }
9805 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9806 {
9807 templ = d1;
9808 d1 = DECL_NAME (templ);
9809 }
9810
9811 /* Issue an error message if we didn't find a template. */
9812 if (! templ)
9813 {
9814 if (complain & tf_error)
9815 error ("%qT is not a template", d1);
9816 return error_mark_node;
9817 }
9818
9819 if (TREE_CODE (templ) != TEMPLATE_DECL
9820 /* Make sure it's a user visible template, if it was named by
9821 the user. */
9822 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9823 && !PRIMARY_TEMPLATE_P (templ)))
9824 {
9825 if (complain & tf_error)
9826 {
9827 error ("non-template type %qT used as a template", d1);
9828 if (in_decl)
9829 error ("for template declaration %q+D", in_decl);
9830 }
9831 return error_mark_node;
9832 }
9833
9834 complain &= ~tf_user;
9835
9836 /* An alias that just changes the name of a template is equivalent to the
9837 other template, so if any of the arguments are pack expansions, strip
9838 the alias to avoid problems with a pack expansion passed to a non-pack
9839 alias template parameter (DR 1430). */
9840 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9841 templ = get_underlying_template (templ);
9842
9843 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9844 {
9845 tree parm;
9846 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9847 if (arglist2 == error_mark_node
9848 || (!uses_template_parms (arglist2)
9849 && check_instantiated_args (templ, arglist2, complain)))
9850 return error_mark_node;
9851
9852 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9853 return parm;
9854 }
9855 else
9856 {
9857 tree template_type = TREE_TYPE (templ);
9858 tree gen_tmpl;
9859 tree type_decl;
9860 tree found = NULL_TREE;
9861 int arg_depth;
9862 int parm_depth;
9863 int is_dependent_type;
9864 int use_partial_inst_tmpl = false;
9865
9866 if (template_type == error_mark_node)
9867 /* An error occurred while building the template TEMPL, and a
9868 diagnostic has most certainly been emitted for that
9869 already. Let's propagate that error. */
9870 return error_mark_node;
9871
9872 gen_tmpl = most_general_template (templ);
9873 if (modules_p ())
9874 lazy_load_pendings (gen_tmpl);
9875
9876 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9877 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9878 arg_depth = TMPL_ARGS_DEPTH (arglist);
9879
9880 if (arg_depth == 1 && parm_depth > 1)
9881 {
9882 /* We've been given an incomplete set of template arguments.
9883 For example, given:
9884
9885 template <class T> struct S1 {
9886 template <class U> struct S2 {};
9887 template <class U> struct S2<U*> {};
9888 };
9889
9890 we will be called with an ARGLIST of `U*', but the
9891 TEMPLATE will be `template <class T> template
9892 <class U> struct S1<T>::S2'. We must fill in the missing
9893 arguments. */
9894 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9895 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9896 arg_depth = TMPL_ARGS_DEPTH (arglist);
9897 }
9898
9899 /* Now we should have enough arguments. */
9900 gcc_assert (parm_depth == arg_depth);
9901
9902 /* From here on, we're only interested in the most general
9903 template. */
9904
9905 /* Shortcut looking up the current class scope again. */
9906 if (current_class_type)
9907 if (tree ti = CLASSTYPE_TEMPLATE_INFO (current_class_type))
9908 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
9909 && comp_template_args (arglist, TI_ARGS (ti)))
9910 return current_class_type;
9911
9912 /* Calculate the BOUND_ARGS. These will be the args that are
9913 actually tsubst'd into the definition to create the
9914 instantiation. */
9915 arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
9916
9917 if (arglist == error_mark_node)
9918 /* We were unable to bind the arguments. */
9919 return error_mark_node;
9920
9921 /* In the scope of a template class, explicit references to the
9922 template class refer to the type of the template, not any
9923 instantiation of it. For example, in:
9924
9925 template <class T> class C { void f(C<T>); }
9926
9927 the `C<T>' is just the same as `C'. Outside of the
9928 class, however, such a reference is an instantiation. */
9929 if (entering_scope
9930 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9931 || currently_open_class (template_type))
9932 {
9933 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9934
9935 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9936 return template_type;
9937 }
9938
9939 /* If we already have this specialization, return it. */
9940 elt.tmpl = gen_tmpl;
9941 elt.args = arglist;
9942 elt.spec = NULL_TREE;
9943 hash = spec_hasher::hash (&elt);
9944 entry = type_specializations->find_with_hash (&elt, hash);
9945
9946 if (entry)
9947 return entry->spec;
9948
9949 /* If the template's constraints are not satisfied,
9950 then we cannot form a valid type.
9951
9952 Note that the check is deferred until after the hash
9953 lookup. This prevents redundant checks on previously
9954 instantiated specializations. */
9955 if (flag_concepts
9956 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9957 && !constraints_satisfied_p (gen_tmpl, arglist))
9958 {
9959 if (complain & tf_error)
9960 {
9961 auto_diagnostic_group d;
9962 error ("template constraint failure for %qD", gen_tmpl);
9963 diagnose_constraints (input_location, gen_tmpl, arglist);
9964 }
9965 return error_mark_node;
9966 }
9967
9968 is_dependent_type = uses_template_parms (arglist);
9969
9970 /* If the deduced arguments are invalid, then the binding
9971 failed. */
9972 if (!is_dependent_type
9973 && check_instantiated_args (gen_tmpl,
9974 INNERMOST_TEMPLATE_ARGS (arglist),
9975 complain))
9976 return error_mark_node;
9977
9978 if (!is_dependent_type
9979 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9980 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9981 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9982 /* This occurs when the user has tried to define a tagged type
9983 in a scope that forbids it. We emitted an error during the
9984 parse. We didn't complete the bail out then, so here we
9985 are. */
9986 return error_mark_node;
9987
9988 context = DECL_CONTEXT (gen_tmpl);
9989 if (context && TYPE_P (context))
9990 {
9991 if (!uses_template_parms (DECL_CONTEXT (templ)))
9992 /* If the context of the partially instantiated template is
9993 already non-dependent, then we might as well use it. */
9994 context = DECL_CONTEXT (templ);
9995 else
9996 {
9997 context = tsubst_aggr_type (context, arglist,
9998 complain, in_decl, true);
9999 /* Try completing the enclosing context if it's not already so. */
10000 if (context != error_mark_node
10001 && !COMPLETE_TYPE_P (context))
10002 {
10003 context = complete_type (context);
10004 if (COMPLETE_TYPE_P (context))
10005 {
10006 /* Completion could have caused us to register the desired
10007 specialization already, so check the table again. */
10008 entry = type_specializations->find_with_hash (&elt, hash);
10009 if (entry)
10010 return entry->spec;
10011 }
10012 }
10013 }
10014 }
10015 else
10016 context = tsubst (context, arglist, complain, in_decl);
10017
10018 if (context == error_mark_node)
10019 return error_mark_node;
10020
10021 if (!context)
10022 context = global_namespace;
10023
10024 /* Create the type. */
10025 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10026 {
10027 /* The user referred to a specialization of an alias
10028 template represented by GEN_TMPL.
10029
10030 [temp.alias]/2 says:
10031
10032 When a template-id refers to the specialization of an
10033 alias template, it is equivalent to the associated
10034 type obtained by substitution of its
10035 template-arguments for the template-parameters in the
10036 type-id of the alias template. */
10037
10038 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
10039 /* Note that the call above (by indirectly calling
10040 register_specialization in tsubst_decl) registers the
10041 TYPE_DECL representing the specialization of the alias
10042 template. So next time someone substitutes ARGLIST for
10043 the template parms into the alias template (GEN_TMPL),
10044 she'll get that TYPE_DECL back. */
10045
10046 if (t == error_mark_node)
10047 return t;
10048 }
10049 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
10050 {
10051 if (!is_dependent_type)
10052 {
10053 set_current_access_from_decl (TYPE_NAME (template_type));
10054 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
10055 tsubst (ENUM_UNDERLYING_TYPE (template_type),
10056 arglist, complain, in_decl),
10057 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
10058 arglist, complain, in_decl),
10059 SCOPED_ENUM_P (template_type), NULL);
10060
10061 if (t == error_mark_node)
10062 return t;
10063 }
10064 else
10065 {
10066 /* We don't want to call start_enum for this type, since
10067 the values for the enumeration constants may involve
10068 template parameters. And, no one should be interested
10069 in the enumeration constants for such a type. */
10070 t = cxx_make_type (ENUMERAL_TYPE);
10071 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
10072 }
10073 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
10074 ENUM_FIXED_UNDERLYING_TYPE_P (t)
10075 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
10076 }
10077 else if (CLASS_TYPE_P (template_type))
10078 {
10079 /* Lambda closures are regenerated in tsubst_lambda_expr, not
10080 instantiated here. */
10081 gcc_assert (!LAMBDA_TYPE_P (template_type));
10082
10083 t = make_class_type (TREE_CODE (template_type));
10084 CLASSTYPE_DECLARED_CLASS (t)
10085 = CLASSTYPE_DECLARED_CLASS (template_type);
10086 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10087
10088 /* A local class. Make sure the decl gets registered properly. */
10089 if (context == current_function_decl)
10090 if (pushtag (DECL_NAME (gen_tmpl), t)
10091 == error_mark_node)
10092 return error_mark_node;
10093
10094 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10095 /* This instantiation is another name for the primary
10096 template type. Set the TYPE_CANONICAL field
10097 appropriately. */
10098 TYPE_CANONICAL (t) = template_type;
10099 else if (any_template_arguments_need_structural_equality_p (arglist))
10100 SET_TYPE_STRUCTURAL_EQUALITY (t);
10101 }
10102 else
10103 gcc_unreachable ();
10104
10105 /* If we called start_enum or pushtag above, this information
10106 will already be set up. */
10107 type_decl = TYPE_NAME (t);
10108 if (!type_decl)
10109 {
10110 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10111
10112 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10113 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10114 DECL_SOURCE_LOCATION (type_decl)
10115 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10116 }
10117
10118 set_instantiating_module (type_decl);
10119 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10120 of export flag. We want to propagate this because it might
10121 be a friend declaration that pushes a new hidden binding. */
10122 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10123
10124 if (CLASS_TYPE_P (template_type))
10125 {
10126 TREE_PRIVATE (type_decl)
10127 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10128 TREE_PROTECTED (type_decl)
10129 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10130 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10131 {
10132 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10133 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10134 }
10135 }
10136
10137 if (OVERLOAD_TYPE_P (t)
10138 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10139 {
10140 static const char *tags[] = {"abi_tag", "may_alias"};
10141
10142 for (unsigned ix = 0; ix != 2; ix++)
10143 {
10144 tree attributes
10145 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10146
10147 if (attributes)
10148 TYPE_ATTRIBUTES (t)
10149 = tree_cons (TREE_PURPOSE (attributes),
10150 TREE_VALUE (attributes),
10151 TYPE_ATTRIBUTES (t));
10152 }
10153 }
10154
10155 /* Let's consider the explicit specialization of a member
10156 of a class template specialization that is implicitly instantiated,
10157 e.g.:
10158 template<class T>
10159 struct S
10160 {
10161 template<class U> struct M {}; //#0
10162 };
10163
10164 template<>
10165 template<>
10166 struct S<int>::M<char> //#1
10167 {
10168 int i;
10169 };
10170 [temp.expl.spec]/4 says this is valid.
10171
10172 In this case, when we write:
10173 S<int>::M<char> m;
10174
10175 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10176 the one of #0.
10177
10178 When we encounter #1, we want to store the partial instantiation
10179 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10180
10181 For all cases other than this "explicit specialization of member of a
10182 class template", we just want to store the most general template into
10183 the CLASSTYPE_TI_TEMPLATE of M.
10184
10185 This case of "explicit specialization of member of a class template"
10186 only happens when:
10187 1/ the enclosing class is an instantiation of, and therefore not
10188 the same as, the context of the most general template, and
10189 2/ we aren't looking at the partial instantiation itself, i.e.
10190 the innermost arguments are not the same as the innermost parms of
10191 the most general template.
10192
10193 So it's only when 1/ and 2/ happens that we want to use the partial
10194 instantiation of the member template in lieu of its most general
10195 template. */
10196
10197 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10198 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10199 /* the enclosing class must be an instantiation... */
10200 && CLASS_TYPE_P (context)
10201 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10202 {
10203 TREE_VEC_LENGTH (arglist)--;
10204 ++processing_template_decl;
10205 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10206 tree partial_inst_args =
10207 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10208 arglist, complain, NULL_TREE);
10209 --processing_template_decl;
10210 TREE_VEC_LENGTH (arglist)++;
10211 if (partial_inst_args == error_mark_node)
10212 return error_mark_node;
10213 use_partial_inst_tmpl =
10214 /*...and we must not be looking at the partial instantiation
10215 itself. */
10216 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10217 partial_inst_args);
10218 }
10219
10220 if (!use_partial_inst_tmpl)
10221 /* This case is easy; there are no member templates involved. */
10222 found = gen_tmpl;
10223 else
10224 {
10225 /* This is a full instantiation of a member template. Find
10226 the partial instantiation of which this is an instance. */
10227
10228 /* Temporarily reduce by one the number of levels in the ARGLIST
10229 so as to avoid comparing the last set of arguments. */
10230 TREE_VEC_LENGTH (arglist)--;
10231 /* We don't use COMPLAIN in the following call because this isn't
10232 the immediate context of deduction. For instance, tf_partial
10233 could be set here as we might be at the beginning of template
10234 argument deduction when any explicitly specified template
10235 arguments are substituted into the function type. tf_partial
10236 could lead into trouble because we wouldn't find the partial
10237 instantiation that might have been created outside tf_partial
10238 context, because the levels of template parameters wouldn't
10239 match, because in a tf_partial context, tsubst doesn't reduce
10240 TEMPLATE_PARM_LEVEL. */
10241 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10242 TREE_VEC_LENGTH (arglist)++;
10243 /* FOUND is either a proper class type, or an alias
10244 template specialization. In the later case, it's a
10245 TYPE_DECL, resulting from the substituting of arguments
10246 for parameters in the TYPE_DECL of the alias template
10247 done earlier. So be careful while getting the template
10248 of FOUND. */
10249 found = (TREE_CODE (found) == TEMPLATE_DECL
10250 ? found
10251 : (TREE_CODE (found) == TYPE_DECL
10252 ? DECL_TI_TEMPLATE (found)
10253 : CLASSTYPE_TI_TEMPLATE (found)));
10254
10255 if (DECL_CLASS_TEMPLATE_P (found)
10256 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10257 {
10258 /* If this partial instantiation is specialized, we want to
10259 use it for hash table lookup. */
10260 elt.tmpl = found;
10261 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10262 hash = spec_hasher::hash (&elt);
10263 }
10264 }
10265
10266 /* Build template info for the new specialization. */
10267 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10268
10269 elt.spec = t;
10270 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10271 gcc_checking_assert (*slot == NULL);
10272 entry = ggc_alloc<spec_entry> ();
10273 *entry = elt;
10274 *slot = entry;
10275
10276 /* Note this use of the partial instantiation so we can check it
10277 later in maybe_process_partial_specialization. */
10278 DECL_TEMPLATE_INSTANTIATIONS (found)
10279 = tree_cons (arglist, t,
10280 DECL_TEMPLATE_INSTANTIATIONS (found));
10281
10282 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10283 && !uses_template_parms (current_nonlambda_scope ())
10284 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10285 /* Now that the type has been registered on the instantiations
10286 list, we set up the enumerators. Because the enumeration
10287 constants may involve the enumeration type itself, we make
10288 sure to register the type first, and then create the
10289 constants. That way, doing tsubst_expr for the enumeration
10290 constants won't result in recursive calls here; we'll find
10291 the instantiation and exit above. */
10292 tsubst_enum (template_type, t, arglist);
10293
10294 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10295 /* If the type makes use of template parameters, the
10296 code that generates debugging information will crash. */
10297 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10298
10299 /* Possibly limit visibility based on template args. */
10300 TREE_PUBLIC (type_decl) = 1;
10301 determine_visibility (type_decl);
10302
10303 inherit_targ_abi_tags (t);
10304
10305 return t;
10306 }
10307 }
10308
10309 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10310
10311 tree
10312 lookup_template_variable (tree templ, tree arglist)
10313 {
10314 if (flag_concepts && variable_concept_p (templ))
10315 return build_concept_check (templ, arglist, tf_none);
10316
10317 /* The type of the expression is NULL_TREE since the template-id could refer
10318 to an explicit or partial specialization. */
10319 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10320 }
10321
10322 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10323
10324 tree
10325 finish_template_variable (tree var, tsubst_flags_t complain)
10326 {
10327 tree templ = TREE_OPERAND (var, 0);
10328 tree arglist = TREE_OPERAND (var, 1);
10329
10330 tree parms = DECL_TEMPLATE_PARMS (templ);
10331 arglist = coerce_template_parms (parms, arglist, templ, complain);
10332 if (arglist == error_mark_node)
10333 return error_mark_node;
10334
10335 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10336 {
10337 if (complain & tf_error)
10338 {
10339 auto_diagnostic_group d;
10340 error ("use of invalid variable template %qE", var);
10341 diagnose_constraints (location_of (var), templ, arglist);
10342 }
10343 return error_mark_node;
10344 }
10345
10346 return instantiate_template (templ, arglist, complain);
10347 }
10348
10349 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10350 TARGS template args, and instantiate it if it's not dependent. */
10351
10352 tree
10353 lookup_and_finish_template_variable (tree templ, tree targs,
10354 tsubst_flags_t complain)
10355 {
10356 tree var = lookup_template_variable (templ, targs);
10357 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) == 1
10358 && !any_dependent_template_arguments_p (targs))
10359 {
10360 var = finish_template_variable (var, complain);
10361 mark_used (var);
10362 }
10363
10364 return convert_from_reference (var);
10365 }
10366
10367 /* If the set of template parameters PARMS contains a template parameter
10368 at the given LEVEL and INDEX, then return this parameter. Otherwise
10369 return NULL_TREE. */
10370
10371 static tree
10372 corresponding_template_parameter (tree parms, int level, int index)
10373 {
10374 while (TMPL_PARMS_DEPTH (parms) > level)
10375 parms = TREE_CHAIN (parms);
10376
10377 if (TMPL_PARMS_DEPTH (parms) != level
10378 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10379 return NULL_TREE;
10380
10381 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10382 /* As in template_parm_to_arg. */
10383 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10384 t = TREE_TYPE (t);
10385 else
10386 t = DECL_INITIAL (t);
10387
10388 gcc_assert (TEMPLATE_PARM_P (t));
10389 return t;
10390 }
10391
10392 /* Return the template parameter from PARMS that positionally corresponds
10393 to the template parameter PARM, or else return NULL_TREE. */
10394
10395 static tree
10396 corresponding_template_parameter (tree parms, tree parm)
10397 {
10398 int level, index;
10399 template_parm_level_and_index (parm, &level, &index);
10400 return corresponding_template_parameter (parms, level, index);
10401 }
10402
10403 \f
10404 struct pair_fn_data
10405 {
10406 tree_fn_t fn;
10407 tree_fn_t any_fn;
10408 void *data;
10409 /* True when we should also visit template parameters that occur in
10410 non-deduced contexts. */
10411 bool include_nondeduced_p;
10412 hash_set<tree> *visited;
10413 };
10414
10415 /* Called from for_each_template_parm via walk_tree. */
10416
10417 static tree
10418 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10419 {
10420 tree t = *tp;
10421 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10422 tree_fn_t fn = pfd->fn;
10423 void *data = pfd->data;
10424 tree result = NULL_TREE;
10425
10426 #define WALK_SUBTREE(NODE) \
10427 do \
10428 { \
10429 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10430 pfd->include_nondeduced_p, \
10431 pfd->any_fn); \
10432 if (result) goto out; \
10433 } \
10434 while (0)
10435
10436 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10437 return t;
10438
10439 if (TYPE_P (t)
10440 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10441 WALK_SUBTREE (TYPE_CONTEXT (t));
10442
10443 switch (TREE_CODE (t))
10444 {
10445 case RECORD_TYPE:
10446 if (TYPE_PTRMEMFUNC_P (t))
10447 break;
10448 /* Fall through. */
10449
10450 case UNION_TYPE:
10451 case ENUMERAL_TYPE:
10452 if (!TYPE_TEMPLATE_INFO (t))
10453 *walk_subtrees = 0;
10454 else
10455 WALK_SUBTREE (TYPE_TI_ARGS (t));
10456 break;
10457
10458 case INTEGER_TYPE:
10459 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10460 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10461 break;
10462
10463 case METHOD_TYPE:
10464 /* Since we're not going to walk subtrees, we have to do this
10465 explicitly here. */
10466 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10467 /* Fall through. */
10468
10469 case FUNCTION_TYPE:
10470 /* Check the return type. */
10471 WALK_SUBTREE (TREE_TYPE (t));
10472
10473 /* Check the parameter types. Since default arguments are not
10474 instantiated until they are needed, the TYPE_ARG_TYPES may
10475 contain expressions that involve template parameters. But,
10476 no-one should be looking at them yet. And, once they're
10477 instantiated, they don't contain template parameters, so
10478 there's no point in looking at them then, either. */
10479 {
10480 tree parm;
10481
10482 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10483 WALK_SUBTREE (TREE_VALUE (parm));
10484
10485 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10486 want walk_tree walking into them itself. */
10487 *walk_subtrees = 0;
10488 }
10489
10490 if (flag_noexcept_type)
10491 {
10492 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10493 if (spec)
10494 WALK_SUBTREE (TREE_PURPOSE (spec));
10495 }
10496 break;
10497
10498 case TYPEOF_TYPE:
10499 case DECLTYPE_TYPE:
10500 if (pfd->include_nondeduced_p
10501 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10502 pfd->visited,
10503 pfd->include_nondeduced_p,
10504 pfd->any_fn))
10505 return error_mark_node;
10506 *walk_subtrees = false;
10507 break;
10508
10509 case TRAIT_TYPE:
10510 if (pfd->include_nondeduced_p)
10511 {
10512 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
10513 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
10514 }
10515 *walk_subtrees = false;
10516 break;
10517
10518 case FUNCTION_DECL:
10519 case VAR_DECL:
10520 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10521 WALK_SUBTREE (DECL_TI_ARGS (t));
10522 break;
10523
10524 case PARM_DECL:
10525 WALK_SUBTREE (TREE_TYPE (t));
10526 break;
10527
10528 case CONST_DECL:
10529 if (DECL_TEMPLATE_PARM_P (t))
10530 WALK_SUBTREE (DECL_INITIAL (t));
10531 if (DECL_CONTEXT (t)
10532 && pfd->include_nondeduced_p)
10533 WALK_SUBTREE (DECL_CONTEXT (t));
10534 break;
10535
10536 case BOUND_TEMPLATE_TEMPLATE_PARM:
10537 /* Record template parameters such as `T' inside `TT<T>'. */
10538 WALK_SUBTREE (TYPE_TI_ARGS (t));
10539 /* Fall through. */
10540
10541 case TEMPLATE_TEMPLATE_PARM:
10542 case TEMPLATE_TYPE_PARM:
10543 case TEMPLATE_PARM_INDEX:
10544 if (fn && (*fn)(t, data))
10545 return t;
10546 else if (!fn)
10547 return t;
10548 break;
10549
10550 case TEMPLATE_DECL:
10551 /* A template template parameter is encountered. */
10552 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10553 WALK_SUBTREE (TREE_TYPE (t));
10554
10555 /* Already substituted template template parameter */
10556 *walk_subtrees = 0;
10557 break;
10558
10559 case TYPENAME_TYPE:
10560 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10561 partial instantiation. */
10562 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10563 *walk_subtrees = 0;
10564 break;
10565
10566 case INDIRECT_REF:
10567 case COMPONENT_REF:
10568 /* If there's no type, then this thing must be some expression
10569 involving template parameters. */
10570 if (!fn && !TREE_TYPE (t))
10571 return error_mark_node;
10572 break;
10573
10574 case CONSTRUCTOR:
10575 case TRAIT_EXPR:
10576 case PLUS_EXPR:
10577 case MULT_EXPR:
10578 case SCOPE_REF:
10579 /* These are non-deduced contexts. */
10580 if (!pfd->include_nondeduced_p)
10581 *walk_subtrees = 0;
10582 break;
10583
10584 case MODOP_EXPR:
10585 case CAST_EXPR:
10586 case IMPLICIT_CONV_EXPR:
10587 case REINTERPRET_CAST_EXPR:
10588 case CONST_CAST_EXPR:
10589 case STATIC_CAST_EXPR:
10590 case DYNAMIC_CAST_EXPR:
10591 case ARROW_EXPR:
10592 case DOTSTAR_EXPR:
10593 case TYPEID_EXPR:
10594 case PSEUDO_DTOR_EXPR:
10595 if (!fn)
10596 return error_mark_node;
10597 break;
10598
10599 default:
10600 break;
10601 }
10602
10603 #undef WALK_SUBTREE
10604
10605 /* We didn't find any template parameters we liked. */
10606 out:
10607 return result;
10608 }
10609
10610 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10611 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10612 call FN with the parameter and the DATA.
10613 If FN returns nonzero, the iteration is terminated, and
10614 for_each_template_parm returns 1. Otherwise, the iteration
10615 continues. If FN never returns a nonzero value, the value
10616 returned by for_each_template_parm is 0. If FN is NULL, it is
10617 considered to be the function which always returns 1.
10618
10619 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10620 parameters that occur in non-deduced contexts. When false, only
10621 visits those template parameters that can be deduced. */
10622
10623 static tree
10624 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10625 hash_set<tree> *visited,
10626 bool include_nondeduced_p,
10627 tree_fn_t any_fn)
10628 {
10629 struct pair_fn_data pfd;
10630 tree result;
10631
10632 /* Set up. */
10633 pfd.fn = fn;
10634 pfd.any_fn = any_fn;
10635 pfd.data = data;
10636 pfd.include_nondeduced_p = include_nondeduced_p;
10637
10638 /* Walk the tree. (Conceptually, we would like to walk without
10639 duplicates, but for_each_template_parm_r recursively calls
10640 for_each_template_parm, so we would need to reorganize a fair
10641 bit to use walk_tree_without_duplicates, so we keep our own
10642 visited list.) */
10643 if (visited)
10644 pfd.visited = visited;
10645 else
10646 pfd.visited = new hash_set<tree>;
10647 result = cp_walk_tree (&t,
10648 for_each_template_parm_r,
10649 &pfd,
10650 pfd.visited);
10651
10652 /* Clean up. */
10653 if (!visited)
10654 {
10655 delete pfd.visited;
10656 pfd.visited = 0;
10657 }
10658
10659 return result;
10660 }
10661
10662 struct find_template_parameter_info
10663 {
10664 explicit find_template_parameter_info (tree ctx_parms)
10665 : ctx_parms (ctx_parms),
10666 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10667 {}
10668
10669 hash_set<tree> visited;
10670 hash_set<tree> parms;
10671 tree parm_list = NULL_TREE;
10672 tree *parm_list_tail = &parm_list;
10673 tree ctx_parms;
10674 int max_depth;
10675 };
10676
10677 /* Appends the declaration of T to the list in DATA. */
10678
10679 static int
10680 keep_template_parm (tree t, void* data)
10681 {
10682 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10683
10684 /* Template parameters declared within the expression are not part of
10685 the parameter mapping. For example, in this concept:
10686
10687 template<typename T>
10688 concept C = requires { <expr> } -> same_as<int>;
10689
10690 the return specifier same_as<int> declares a new decltype parameter
10691 that must not be part of the parameter mapping. The same is true
10692 for generic lambda parameters, lambda template parameters, etc. */
10693 int level;
10694 int index;
10695 template_parm_level_and_index (t, &level, &index);
10696 if (level == 0 || level > ftpi->max_depth)
10697 return 0;
10698
10699 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10700 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10701 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10702 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10703
10704 /* This template parameter might be an argument to a cached dependent
10705 specalization that was formed earlier inside some other template, in
10706 which case the parameter is not among the ones that are in-scope.
10707 Look in CTX_PARMS to find the corresponding in-scope template
10708 parameter, and use it instead. */
10709 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10710 t = in_scope;
10711
10712 /* Arguments like const T yield parameters like const T. This means that
10713 a template-id like X<T, const T> would yield two distinct parameters:
10714 T and const T. Adjust types to their unqualified versions. */
10715 if (TYPE_P (t))
10716 t = TYPE_MAIN_VARIANT (t);
10717 if (!ftpi->parms.add (t))
10718 {
10719 /* Append T to PARM_LIST. */
10720 tree node = build_tree_list (NULL_TREE, t);
10721 *ftpi->parm_list_tail = node;
10722 ftpi->parm_list_tail = &TREE_CHAIN (node);
10723 }
10724
10725 /* Verify the parameter we found has a valid index. */
10726 if (flag_checking)
10727 {
10728 tree parms = ftpi->ctx_parms;
10729 while (TMPL_PARMS_DEPTH (parms) > level)
10730 parms = TREE_CHAIN (parms);
10731 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10732 gcc_assert (index < len);
10733 }
10734
10735 return 0;
10736 }
10737
10738 /* Ensure that we recursively examine certain terms that are not normally
10739 visited in for_each_template_parm_r. */
10740
10741 static int
10742 any_template_parm_r (tree t, void *data)
10743 {
10744 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10745
10746 #define WALK_SUBTREE(NODE) \
10747 do \
10748 { \
10749 for_each_template_parm (NODE, keep_template_parm, data, \
10750 &ftpi->visited, true, \
10751 any_template_parm_r); \
10752 } \
10753 while (0)
10754
10755 /* A mention of a member alias/typedef is a use of all of its template
10756 arguments, including those from the enclosing class, so we don't use
10757 alias_template_specialization_p here. */
10758 if (TYPE_P (t) && typedef_variant_p (t))
10759 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10760 WALK_SUBTREE (TI_ARGS (tinfo));
10761
10762 switch (TREE_CODE (t))
10763 {
10764 case TEMPLATE_TYPE_PARM:
10765 /* Type constraints of a placeholder type may contain parameters. */
10766 if (is_auto (t))
10767 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10768 WALK_SUBTREE (constr);
10769 break;
10770
10771 case TEMPLATE_ID_EXPR:
10772 /* Search through references to variable templates. */
10773 WALK_SUBTREE (TREE_OPERAND (t, 0));
10774 WALK_SUBTREE (TREE_OPERAND (t, 1));
10775 break;
10776
10777 case TEMPLATE_PARM_INDEX:
10778 WALK_SUBTREE (TREE_TYPE (t));
10779 break;
10780
10781 case TEMPLATE_DECL:
10782 /* If T is a member template that shares template parameters with
10783 ctx_parms, we need to mark all those parameters for mapping.
10784 To that end, it should suffice to just walk the DECL_CONTEXT of
10785 the template (assuming the template is not overly general). */
10786 WALK_SUBTREE (DECL_CONTEXT (t));
10787 break;
10788
10789 case LAMBDA_EXPR:
10790 {
10791 /* Look in the parms and body. */
10792 tree fn = lambda_function (t);
10793 WALK_SUBTREE (TREE_TYPE (fn));
10794 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10795 }
10796 break;
10797
10798 case IDENTIFIER_NODE:
10799 if (IDENTIFIER_CONV_OP_P (t))
10800 /* The conversion-type-id of a conversion operator may be dependent. */
10801 WALK_SUBTREE (TREE_TYPE (t));
10802 break;
10803
10804 case CONVERT_EXPR:
10805 if (is_dummy_object (t))
10806 WALK_SUBTREE (TREE_TYPE (t));
10807 break;
10808
10809 default:
10810 break;
10811 }
10812
10813 /* Keep walking. */
10814 return 0;
10815 }
10816
10817 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10818 are the template parameters in scope. */
10819
10820 tree
10821 find_template_parameters (tree t, tree ctx_parms)
10822 {
10823 if (!ctx_parms)
10824 return NULL_TREE;
10825
10826 find_template_parameter_info ftpi (ctx_parms);
10827 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10828 /*include_nondeduced*/true, any_template_parm_r);
10829 return ftpi.parm_list;
10830 }
10831
10832 /* Returns true if T depends on any template parameter. */
10833
10834 bool
10835 uses_template_parms (tree t)
10836 {
10837 if (t == NULL_TREE || t == error_mark_node)
10838 return false;
10839
10840 /* Namespaces can't depend on any template parameters. */
10841 if (TREE_CODE (t) == NAMESPACE_DECL)
10842 return false;
10843
10844 processing_template_decl_sentinel ptds (/*reset*/false);
10845 ++processing_template_decl;
10846
10847 if (TYPE_P (t))
10848 return dependent_type_p (t);
10849 else if (TREE_CODE (t) == TREE_VEC)
10850 return any_dependent_template_arguments_p (t);
10851 else if (TREE_CODE (t) == TREE_LIST)
10852 return (uses_template_parms (TREE_VALUE (t))
10853 || uses_template_parms (TREE_CHAIN (t)));
10854 else if (TREE_CODE (t) == TYPE_DECL)
10855 return dependent_type_p (TREE_TYPE (t));
10856 else
10857 return instantiation_dependent_expression_p (t);
10858 }
10859
10860 /* Returns true iff we're processing an incompletely instantiated function
10861 template. Useful instead of processing_template_decl because the latter
10862 is set to 0 during instantiate_non_dependent_expr. */
10863
10864 bool
10865 in_template_function (void)
10866 {
10867 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10868 the latter might get set for e.g. access checking during satisfaction. */
10869 tree fn = cfun ? cfun->decl : NULL_TREE;
10870 bool ret;
10871 ++processing_template_decl;
10872 ret = (fn && DECL_LANG_SPECIFIC (fn)
10873 && DECL_TEMPLATE_INFO (fn)
10874 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10875 --processing_template_decl;
10876 return ret;
10877 }
10878
10879 /* Returns true if T depends on any template parameter with level LEVEL. */
10880
10881 bool
10882 uses_template_parms_level (tree t, int level)
10883 {
10884 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10885 /*include_nondeduced_p=*/true);
10886 }
10887
10888 /* Returns true if the signature of DECL depends on any template parameter from
10889 its enclosing class. */
10890
10891 static bool
10892 uses_outer_template_parms (tree decl)
10893 {
10894 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10895 if (depth == 0)
10896 return false;
10897 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10898 &depth, NULL, /*include_nondeduced_p=*/true))
10899 return true;
10900 if (PRIMARY_TEMPLATE_P (decl)
10901 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
10902 {
10903 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
10904 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
10905 {
10906 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
10907 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
10908 if (TREE_CODE (parm) == PARM_DECL
10909 && for_each_template_parm (TREE_TYPE (parm),
10910 template_parm_outer_level,
10911 &depth, NULL, /*nondeduced*/true))
10912 return true;
10913 if (TREE_CODE (parm) == TEMPLATE_DECL
10914 && uses_outer_template_parms (parm))
10915 return true;
10916 if (defarg
10917 && for_each_template_parm (defarg, template_parm_outer_level,
10918 &depth, NULL, /*nondeduced*/true))
10919 return true;
10920 }
10921 }
10922 if (uses_outer_template_parms_in_constraints (decl))
10923 return true;
10924 return false;
10925 }
10926
10927 /* Returns true if the constraints of DECL depend on any template parameters
10928 from its enclosing scope. */
10929
10930 bool
10931 uses_outer_template_parms_in_constraints (tree decl)
10932 {
10933 tree ci = get_constraints (decl);
10934 if (ci)
10935 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10936 if (!ci)
10937 return false;
10938 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10939 if (depth == 0)
10940 return false;
10941 return for_each_template_parm (ci, template_parm_outer_level,
10942 &depth, NULL, /*nondeduced*/true);
10943 }
10944
10945 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10946 ill-formed translation unit, i.e. a variable or function that isn't
10947 usable in a constant expression. */
10948
10949 static inline bool
10950 neglectable_inst_p (tree d)
10951 {
10952 return (d && DECL_P (d)
10953 && !undeduced_auto_decl (d)
10954 && !(TREE_CODE (d) == FUNCTION_DECL
10955 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
10956 : decl_maybe_constant_var_p (d)));
10957 }
10958
10959 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10960 neglectable and instantiated from within an erroneous instantiation. */
10961
10962 static bool
10963 limit_bad_template_recursion (tree decl)
10964 {
10965 struct tinst_level *lev = current_tinst_level;
10966 int errs = errorcount + sorrycount;
10967 if (errs == 0 || !neglectable_inst_p (decl))
10968 return false;
10969
10970 /* Avoid instantiating members of an ill-formed class. */
10971 bool refuse
10972 = (DECL_CLASS_SCOPE_P (decl)
10973 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
10974
10975 if (!refuse)
10976 {
10977 for (; lev; lev = lev->next)
10978 if (neglectable_inst_p (lev->maybe_get_node ()))
10979 break;
10980 refuse = (lev && errs > lev->errors);
10981 }
10982
10983 if (refuse)
10984 {
10985 /* Don't warn about it not being defined. */
10986 suppress_warning (decl, OPT_Wunused);
10987 tree clone;
10988 FOR_EACH_CLONE (clone, decl)
10989 suppress_warning (clone, OPT_Wunused);
10990 }
10991 return refuse;
10992 }
10993
10994 static int tinst_depth;
10995 extern int max_tinst_depth;
10996 int depth_reached;
10997
10998 static GTY(()) struct tinst_level *last_error_tinst_level;
10999
11000 /* We're starting to instantiate D; record the template instantiation context
11001 at LOC for diagnostics and to restore it later. */
11002
11003 bool
11004 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
11005 {
11006 struct tinst_level *new_level;
11007
11008 if (tinst_depth >= max_tinst_depth)
11009 {
11010 /* Tell error.cc not to try to instantiate any templates. */
11011 at_eof = 2;
11012 fatal_error (input_location,
11013 "template instantiation depth exceeds maximum of %d"
11014 " (use %<-ftemplate-depth=%> to increase the maximum)",
11015 max_tinst_depth);
11016 return false;
11017 }
11018
11019 /* If the current instantiation caused problems, don't let it instantiate
11020 anything else. Do allow deduction substitution and decls usable in
11021 constant expressions. */
11022 if (!targs && limit_bad_template_recursion (tldcl))
11023 {
11024 /* Avoid no_linkage_errors and unused function (and all other)
11025 warnings for this decl. */
11026 suppress_warning (tldcl);
11027 return false;
11028 }
11029
11030 /* When not -quiet, dump template instantiations other than functions, since
11031 announce_function will take care of those. */
11032 if (!quiet_flag && !targs
11033 && TREE_CODE (tldcl) != TREE_LIST
11034 && TREE_CODE (tldcl) != FUNCTION_DECL)
11035 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
11036
11037 new_level = tinst_level_freelist ().alloc ();
11038 new_level->tldcl = tldcl;
11039 new_level->targs = targs;
11040 new_level->locus = loc;
11041 new_level->errors = errorcount + sorrycount;
11042 new_level->next = NULL;
11043 new_level->refcount = 0;
11044 new_level->path = new_level->visible = nullptr;
11045 set_refcount_ptr (new_level->next, current_tinst_level);
11046 set_refcount_ptr (current_tinst_level, new_level);
11047
11048 ++tinst_depth;
11049 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
11050 depth_reached = tinst_depth;
11051
11052 return true;
11053 }
11054
11055 /* We're starting substitution of TMPL<ARGS>; record the template
11056 substitution context for diagnostics and to restore it later. */
11057
11058 bool
11059 push_tinst_level (tree tmpl, tree args)
11060 {
11061 return push_tinst_level_loc (tmpl, args, input_location);
11062 }
11063
11064 /* We're starting to instantiate D; record INPUT_LOCATION and the
11065 template instantiation context for diagnostics and to restore it
11066 later. */
11067
11068 bool
11069 push_tinst_level (tree d)
11070 {
11071 return push_tinst_level_loc (d, input_location);
11072 }
11073
11074 /* Likewise, but record LOC as the program location. */
11075
11076 bool
11077 push_tinst_level_loc (tree d, location_t loc)
11078 {
11079 gcc_assert (TREE_CODE (d) != TREE_LIST);
11080 return push_tinst_level_loc (d, NULL, loc);
11081 }
11082
11083 /* We're done instantiating this template; return to the instantiation
11084 context. */
11085
11086 void
11087 pop_tinst_level (void)
11088 {
11089 /* Restore the filename and line number stashed away when we started
11090 this instantiation. */
11091 input_location = current_tinst_level->locus;
11092 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11093 --tinst_depth;
11094 }
11095
11096 /* We're instantiating a deferred template; restore the template
11097 instantiation context in which the instantiation was requested, which
11098 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11099
11100 static tree
11101 reopen_tinst_level (struct tinst_level *level)
11102 {
11103 struct tinst_level *t;
11104
11105 tinst_depth = 0;
11106 for (t = level; t; t = t->next)
11107 ++tinst_depth;
11108
11109 set_refcount_ptr (current_tinst_level, level);
11110 pop_tinst_level ();
11111 if (current_tinst_level)
11112 current_tinst_level->errors = errorcount+sorrycount;
11113 return level->maybe_get_node ();
11114 }
11115
11116 /* Returns the TINST_LEVEL which gives the original instantiation
11117 context. */
11118
11119 struct tinst_level *
11120 outermost_tinst_level (void)
11121 {
11122 struct tinst_level *level = current_tinst_level;
11123 if (level)
11124 while (level->next)
11125 level = level->next;
11126 return level;
11127 }
11128
11129 /* True iff T is a friend function declaration that is not itself a template
11130 and is not defined in a class template. */
11131
11132 bool
11133 non_templated_friend_p (tree t)
11134 {
11135 if (t && TREE_CODE (t) == FUNCTION_DECL
11136 && DECL_UNIQUE_FRIEND_P (t))
11137 {
11138 tree ti = DECL_TEMPLATE_INFO (t);
11139 if (!ti)
11140 return true;
11141 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */
11142 if (DECL_FRIEND_CONTEXT (t))
11143 return false;
11144 /* Non-templated friends in a class template are still represented with a
11145 TEMPLATE_DECL; check that its primary template is the befriending
11146 class. Note that DECL_PRIMARY_TEMPLATE is null for
11147 template <class T> friend A<T>::f(); */
11148 tree tmpl = TI_TEMPLATE (ti);
11149 tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
11150 return (primary && primary != tmpl);
11151 }
11152 else
11153 return false;
11154 }
11155
11156 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11157 vector of template arguments, as for tsubst.
11158
11159 Returns an appropriate tsubst'd friend declaration. */
11160
11161 static tree
11162 tsubst_friend_function (tree decl, tree args)
11163 {
11164 tree new_friend;
11165
11166 if (TREE_CODE (decl) == FUNCTION_DECL
11167 && DECL_TEMPLATE_INSTANTIATION (decl)
11168 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11169 /* This was a friend declared with an explicit template
11170 argument list, e.g.:
11171
11172 friend void f<>(T);
11173
11174 to indicate that f was a template instantiation, not a new
11175 function declaration. Now, we have to figure out what
11176 instantiation of what template. */
11177 {
11178 tree template_id, arglist, fns;
11179 tree new_args;
11180 tree tmpl;
11181 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11182
11183 /* Friend functions are looked up in the containing namespace scope.
11184 We must enter that scope, to avoid finding member functions of the
11185 current class with same name. */
11186 push_nested_namespace (ns);
11187 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11188 tf_warning_or_error, NULL_TREE);
11189 pop_nested_namespace (ns);
11190 arglist = tsubst (DECL_TI_ARGS (decl), args,
11191 tf_warning_or_error, NULL_TREE);
11192 template_id = lookup_template_function (fns, arglist);
11193
11194 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11195 tmpl = determine_specialization (template_id, new_friend,
11196 &new_args,
11197 /*need_member_template=*/0,
11198 TREE_VEC_LENGTH (args),
11199 tsk_none);
11200 return instantiate_template (tmpl, new_args, tf_error);
11201 }
11202
11203 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11204 if (new_friend == error_mark_node)
11205 return error_mark_node;
11206
11207 /* The NEW_FRIEND will look like an instantiation, to the
11208 compiler, but is not an instantiation from the point of view of
11209 the language. For example, we might have had:
11210
11211 template <class T> struct S {
11212 template <class U> friend void f(T, U);
11213 };
11214
11215 Then, in S<int>, template <class U> void f(int, U) is not an
11216 instantiation of anything. */
11217
11218 DECL_USE_TEMPLATE (new_friend) = 0;
11219 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11220 {
11221 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11222 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11223 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11224 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11225
11226 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11227 match in decls_match. */
11228 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11229 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11230 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11231 if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
11232 {
11233 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11234 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */
11235 tsubst_each_template_parm_constraints (parms, args,
11236 tf_warning_or_error);
11237 }
11238 }
11239
11240 /* The mangled name for the NEW_FRIEND is incorrect. The function
11241 is not a template instantiation and should not be mangled like
11242 one. Therefore, we forget the mangling here; we'll recompute it
11243 later if we need it. */
11244 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11245 {
11246 SET_DECL_RTL (new_friend, NULL);
11247 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11248 }
11249
11250 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11251 {
11252 tree old_decl;
11253 tree ns;
11254
11255 /* We must save some information from NEW_FRIEND before calling
11256 duplicate decls since that function will free NEW_FRIEND if
11257 possible. */
11258 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11259 tree new_friend_result_template_info = NULL_TREE;
11260 bool new_friend_is_defn =
11261 (new_friend_template_info
11262 && (DECL_INITIAL (DECL_TEMPLATE_RESULT
11263 (template_for_substitution (new_friend)))
11264 != NULL_TREE));
11265 tree not_tmpl = new_friend;
11266
11267 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11268 {
11269 /* This declaration is a `primary' template. */
11270 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11271
11272 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11273 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11274 }
11275 else if (!constraints_satisfied_p (new_friend))
11276 /* Only define a constrained hidden friend when satisfied. */
11277 return error_mark_node;
11278
11279 /* Inside pushdecl_namespace_level, we will push into the
11280 current namespace. However, the friend function should go
11281 into the namespace of the template. */
11282 ns = decl_namespace_context (new_friend);
11283 push_nested_namespace (ns);
11284 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11285 pop_nested_namespace (ns);
11286
11287 if (old_decl == error_mark_node)
11288 return error_mark_node;
11289
11290 if (old_decl != new_friend)
11291 {
11292 /* This new friend declaration matched an existing
11293 declaration. For example, given:
11294
11295 template <class T> void f(T);
11296 template <class U> class C {
11297 template <class T> friend void f(T) {}
11298 };
11299
11300 the friend declaration actually provides the definition
11301 of `f', once C has been instantiated for some type. So,
11302 old_decl will be the out-of-class template declaration,
11303 while new_friend is the in-class definition.
11304
11305 But, if `f' was called before this point, the
11306 instantiation of `f' will have DECL_TI_ARGS corresponding
11307 to `T' but not to `U', references to which might appear
11308 in the definition of `f'. Previously, the most general
11309 template for an instantiation of `f' was the out-of-class
11310 version; now it is the in-class version. Therefore, we
11311 run through all specialization of `f', adding to their
11312 DECL_TI_ARGS appropriately. In particular, they need a
11313 new set of outer arguments, corresponding to the
11314 arguments for this class instantiation.
11315
11316 The same situation can arise with something like this:
11317
11318 friend void f(int);
11319 template <class T> class C {
11320 friend void f(T) {}
11321 };
11322
11323 when `C<int>' is instantiated. Now, `f(int)' is defined
11324 in the class. */
11325
11326 if (!new_friend_is_defn)
11327 /* On the other hand, if the in-class declaration does
11328 *not* provide a definition, then we don't want to alter
11329 existing definitions. We can just leave everything
11330 alone. */
11331 ;
11332 else
11333 {
11334 tree new_template = TI_TEMPLATE (new_friend_template_info);
11335 tree new_args = TI_ARGS (new_friend_template_info);
11336
11337 /* Overwrite whatever template info was there before, if
11338 any, with the new template information pertaining to
11339 the declaration. */
11340 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11341
11342 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11343 {
11344 /* We should have called reregister_specialization in
11345 duplicate_decls. */
11346 gcc_assert (retrieve_specialization (new_template,
11347 new_args, 0)
11348 == old_decl);
11349
11350 /* Instantiate it if the global has already been used. */
11351 if (DECL_ODR_USED (old_decl))
11352 instantiate_decl (old_decl, /*defer_ok=*/true,
11353 /*expl_inst_class_mem_p=*/false);
11354 }
11355 else
11356 {
11357 tree t;
11358
11359 /* Indicate that the old function template is a partial
11360 instantiation. */
11361 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11362 = new_friend_result_template_info;
11363
11364 gcc_assert (new_template
11365 == most_general_template (new_template));
11366 gcc_assert (new_template != old_decl);
11367
11368 /* Reassign any specializations already in the hash table
11369 to the new more general template, and add the
11370 additional template args. */
11371 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11372 t != NULL_TREE;
11373 t = TREE_CHAIN (t))
11374 {
11375 tree spec = TREE_VALUE (t);
11376 spec_entry elt;
11377
11378 elt.tmpl = old_decl;
11379 elt.args = DECL_TI_ARGS (spec);
11380 elt.spec = NULL_TREE;
11381
11382 decl_specializations->remove_elt (&elt);
11383
11384 DECL_TI_ARGS (spec)
11385 = add_outermost_template_args (new_args,
11386 DECL_TI_ARGS (spec));
11387
11388 register_specialization
11389 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11390
11391 }
11392 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11393 }
11394 }
11395
11396 /* The information from NEW_FRIEND has been merged into OLD_DECL
11397 by duplicate_decls. */
11398 new_friend = old_decl;
11399 }
11400
11401 /* We've just introduced a namespace-scope function in the purview
11402 without necessarily having opened the enclosing namespace, so
11403 make sure the namespace is in the purview now too. */
11404 if (modules_p ()
11405 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
11406 && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
11407 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
11408 }
11409 else
11410 {
11411 tree context = DECL_CONTEXT (new_friend);
11412 bool dependent_p;
11413
11414 /* In the code
11415 template <class T> class C {
11416 template <class U> friend void C1<U>::f (); // case 1
11417 friend void C2<T>::f (); // case 2
11418 };
11419 we only need to make sure CONTEXT is a complete type for
11420 case 2. To distinguish between the two cases, we note that
11421 CONTEXT of case 1 remains dependent type after tsubst while
11422 this isn't true for case 2. */
11423 ++processing_template_decl;
11424 dependent_p = dependent_type_p (context);
11425 --processing_template_decl;
11426
11427 if (!dependent_p
11428 && !complete_type_or_else (context, NULL_TREE))
11429 return error_mark_node;
11430
11431 if (COMPLETE_TYPE_P (context))
11432 {
11433 tree fn = new_friend;
11434 /* do_friend adds the TEMPLATE_DECL for any member friend
11435 template even if it isn't a member template, i.e.
11436 template <class T> friend A<T>::f();
11437 Look through it in that case. */
11438 if (TREE_CODE (fn) == TEMPLATE_DECL
11439 && !PRIMARY_TEMPLATE_P (fn))
11440 fn = DECL_TEMPLATE_RESULT (fn);
11441 /* Check to see that the declaration is really present, and,
11442 possibly obtain an improved declaration. */
11443 fn = check_classfn (context, fn, NULL_TREE);
11444
11445 if (fn)
11446 new_friend = fn;
11447 }
11448 }
11449
11450 return new_friend;
11451 }
11452
11453 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11454 template arguments, as for tsubst.
11455
11456 Returns an appropriate tsubst'd friend type or error_mark_node on
11457 failure. */
11458
11459 static tree
11460 tsubst_friend_class (tree friend_tmpl, tree args)
11461 {
11462 tree tmpl;
11463
11464 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11465 {
11466 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11467 return TREE_TYPE (tmpl);
11468 }
11469
11470 tree context = CP_DECL_CONTEXT (friend_tmpl);
11471 if (TREE_CODE (context) == NAMESPACE_DECL)
11472 push_nested_namespace (context);
11473 else
11474 {
11475 context = tsubst (context, args, tf_error, NULL_TREE);
11476 push_nested_class (context);
11477 }
11478
11479 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11480 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11481
11482 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11483 {
11484 /* The friend template has already been declared. Just
11485 check to see that the declarations match, and install any new
11486 default parameters. We must tsubst the default parameters,
11487 of course. We only need the innermost template parameters
11488 because that is all that redeclare_class_template will look
11489 at. */
11490 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11491 > TMPL_ARGS_DEPTH (args))
11492 {
11493 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11494 args, tf_warning_or_error);
11495 tsubst_each_template_parm_constraints (parms, args,
11496 tf_warning_or_error);
11497 location_t saved_input_location = input_location;
11498 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11499 tree cons = get_constraints (tmpl);
11500 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11501 input_location = saved_input_location;
11502 }
11503 }
11504 else
11505 {
11506 /* The friend template has not already been declared. In this
11507 case, the instantiation of the template class will cause the
11508 injection of this template into the namespace scope. */
11509 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11510
11511 if (tmpl != error_mark_node)
11512 {
11513 /* The new TMPL is not an instantiation of anything, so we
11514 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11515 for the new type because that is supposed to be the
11516 corresponding template decl, i.e., TMPL. */
11517 DECL_USE_TEMPLATE (tmpl) = 0;
11518 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11519 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11520 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11521 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11522
11523 /* Substitute into and set the constraints on the new declaration. */
11524 if (tree ci = get_constraints (friend_tmpl))
11525 {
11526 ++processing_template_decl;
11527 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11528 DECL_FRIEND_CONTEXT (friend_tmpl));
11529 --processing_template_decl;
11530 set_constraints (tmpl, ci);
11531 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
11532 args, tf_warning_or_error);
11533 }
11534
11535 /* Inject this template into the enclosing namspace scope. */
11536 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11537 }
11538 }
11539
11540 if (TREE_CODE (context) == NAMESPACE_DECL)
11541 pop_nested_namespace (context);
11542 else
11543 pop_nested_class ();
11544
11545 return TREE_TYPE (tmpl);
11546 }
11547
11548 /* Returns zero if TYPE cannot be completed later due to circularity.
11549 Otherwise returns one. */
11550
11551 static int
11552 can_complete_type_without_circularity (tree type)
11553 {
11554 if (type == NULL_TREE || type == error_mark_node)
11555 return 0;
11556 else if (COMPLETE_TYPE_P (type))
11557 return 1;
11558 else if (TREE_CODE (type) == ARRAY_TYPE)
11559 return can_complete_type_without_circularity (TREE_TYPE (type));
11560 else if (CLASS_TYPE_P (type)
11561 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11562 return 0;
11563 else
11564 return 1;
11565 }
11566
11567 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11568 tsubst_flags_t, tree);
11569
11570 /* Instantiate the contract statement. */
11571
11572 static tree
11573 tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
11574 tree in_decl)
11575 {
11576 tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
11577 bool auto_p = type_uses_auto (type);
11578
11579 tree r = copy_node (t);
11580
11581 /* Rebuild the result variable. */
11582 if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
11583 {
11584 tree oldvar = POSTCONDITION_IDENTIFIER (t);
11585
11586 tree newvar = copy_node (oldvar);
11587 TREE_TYPE (newvar) = type;
11588 DECL_CONTEXT (newvar) = decl;
11589 POSTCONDITION_IDENTIFIER (r) = newvar;
11590
11591 /* Make sure the postcondition is valid. */
11592 location_t loc = DECL_SOURCE_LOCATION (oldvar);
11593 if (!auto_p)
11594 if (!check_postcondition_result (decl, type, loc))
11595 return invalidate_contract (r);
11596
11597 /* Make the variable available for lookup. */
11598 register_local_specialization (newvar, oldvar);
11599 }
11600
11601 /* Instantiate the condition. If the return type is undeduced, process
11602 the expression as if inside a template to avoid spurious type errors. */
11603 if (auto_p)
11604 ++processing_template_decl;
11605 ++processing_contract_condition;
11606 CONTRACT_CONDITION (r)
11607 = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
11608 --processing_contract_condition;
11609 if (auto_p)
11610 --processing_template_decl;
11611
11612 /* And the comment. */
11613 CONTRACT_COMMENT (r)
11614 = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
11615
11616 return r;
11617 }
11618
11619 /* Update T by instantiating its contract attribute. */
11620
11621 static void
11622 tsubst_contract_attribute (tree decl, tree t, tree args,
11623 tsubst_flags_t complain, tree in_decl)
11624 {
11625 /* For non-specializations, adjust the current declaration to the most general
11626 version of in_decl. Because we defer the instantiation of contracts as long
11627 as possible, they are still written in terms of the parameters (and return
11628 type) of the most general template. */
11629 tree tmpl = DECL_TI_TEMPLATE (in_decl);
11630 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
11631 in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
11632 local_specialization_stack specs (lss_copy);
11633 register_parameter_specializations (in_decl, decl);
11634
11635 /* Get the contract to be instantiated. */
11636 tree contract = CONTRACT_STATEMENT (t);
11637
11638 /* Use the complete set of template arguments for instantiation. The
11639 contract may not have been instantiated and still refer to outer levels
11640 of template parameters. */
11641 args = DECL_TI_ARGS (decl);
11642
11643 /* For member functions, make this available for semantic analysis. */
11644 tree save_ccp = current_class_ptr;
11645 tree save_ccr = current_class_ref;
11646 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11647 {
11648 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11649 tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
11650 inject_this_parameter (this_type, cp_type_quals (this_type));
11651 }
11652
11653 contract = tsubst_contract (decl, contract, args, complain, in_decl);
11654
11655 current_class_ptr = save_ccp;
11656 current_class_ref = save_ccr;
11657
11658 /* Rebuild the attribute. */
11659 TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
11660 }
11661
11662 /* Rebuild the attribute list for DECL, substituting into contracts
11663 as needed. */
11664
11665 void
11666 tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
11667 {
11668 tree list = copy_list (DECL_ATTRIBUTES (decl));
11669 for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
11670 {
11671 if (cxx_contract_attribute_p (attr))
11672 tsubst_contract_attribute (decl, attr, args, complain, in_decl);
11673 }
11674 DECL_ATTRIBUTES (decl) = list;
11675 }
11676
11677 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11678 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11679
11680 static tree
11681 tsubst_attribute (tree t, tree *decl_p, tree args,
11682 tsubst_flags_t complain, tree in_decl)
11683 {
11684 gcc_assert (ATTR_IS_DEPENDENT (t));
11685
11686 /* Note that contract attributes are never substituted from this function.
11687 Their instantiation is triggered by regenerate_from_template_decl when
11688 we instantiate the body of the function. */
11689
11690 tree val = TREE_VALUE (t);
11691 if (val == NULL_TREE)
11692 /* Nothing to do. */;
11693 else if ((flag_openmp || flag_openmp_simd)
11694 && is_attribute_p ("omp declare simd",
11695 get_attribute_name (t)))
11696 {
11697 tree clauses = TREE_VALUE (val);
11698 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11699 complain, in_decl);
11700 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11701 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11702 tree parms = DECL_ARGUMENTS (*decl_p);
11703 clauses
11704 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11705 if (clauses)
11706 val = build_tree_list (NULL_TREE, clauses);
11707 else
11708 val = NULL_TREE;
11709 }
11710 else if (flag_openmp
11711 && is_attribute_p ("omp declare variant base",
11712 get_attribute_name (t)))
11713 {
11714 ++cp_unevaluated_operand;
11715 tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
11716 --cp_unevaluated_operand;
11717 tree chain = TREE_CHAIN (val);
11718 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11719 tree ctx = copy_list (TREE_VALUE (val));
11720 tree simd = get_identifier ("simd");
11721 tree score = get_identifier (" score");
11722 tree condition = get_identifier ("condition");
11723 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11724 {
11725 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11726 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11727 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11728 {
11729 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11730 {
11731 tree clauses = TREE_VALUE (t2);
11732 clauses = tsubst_omp_clauses (clauses,
11733 C_ORT_OMP_DECLARE_SIMD, args,
11734 complain, in_decl);
11735 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11736 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11737 TREE_VALUE (t2) = clauses;
11738 }
11739 else
11740 {
11741 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11742 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11743 if (TREE_VALUE (t3))
11744 {
11745 bool allow_string
11746 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11747 && TREE_PURPOSE (t3) != score);
11748 tree v = TREE_VALUE (t3);
11749 if (TREE_CODE (v) == STRING_CST && allow_string)
11750 continue;
11751 v = tsubst_expr (v, args, complain, in_decl);
11752 v = fold_non_dependent_expr (v);
11753 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11754 || (TREE_PURPOSE (t3) == score
11755 ? TREE_CODE (v) != INTEGER_CST
11756 : !tree_fits_shwi_p (v)))
11757 {
11758 location_t loc
11759 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11760 match_loc);
11761 if (TREE_PURPOSE (t3) == score)
11762 error_at (loc, "score argument must be "
11763 "constant integer expression");
11764 else if (allow_string)
11765 error_at (loc, "property must be constant "
11766 "integer expression or string "
11767 "literal");
11768 else
11769 error_at (loc, "property must be constant "
11770 "integer expression");
11771 return NULL_TREE;
11772 }
11773 else if (TREE_PURPOSE (t3) == score
11774 && tree_int_cst_sgn (v) < 0)
11775 {
11776 location_t loc
11777 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11778 match_loc);
11779 error_at (loc, "score argument must be "
11780 "non-negative");
11781 return NULL_TREE;
11782 }
11783 TREE_VALUE (t3) = v;
11784 }
11785 }
11786 }
11787 }
11788 val = tree_cons (varid, ctx, chain);
11789 }
11790 /* If the first attribute argument is an identifier, don't
11791 pass it through tsubst. Attributes like mode, format,
11792 cleanup and several target specific attributes expect it
11793 unmodified. */
11794 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11795 {
11796 tree chain
11797 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
11798 if (chain != TREE_CHAIN (val))
11799 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11800 }
11801 else if (PACK_EXPANSION_P (val))
11802 {
11803 /* An attribute pack expansion. */
11804 tree purp = TREE_PURPOSE (t);
11805 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11806 if (pack == error_mark_node)
11807 return error_mark_node;
11808 int len = TREE_VEC_LENGTH (pack);
11809 tree list = NULL_TREE;
11810 tree *q = &list;
11811 for (int i = 0; i < len; ++i)
11812 {
11813 tree elt = TREE_VEC_ELT (pack, i);
11814 *q = build_tree_list (purp, elt);
11815 q = &TREE_CHAIN (*q);
11816 }
11817 return list;
11818 }
11819 else
11820 val = tsubst_expr (val, args, complain, in_decl);
11821
11822 if (val == error_mark_node)
11823 return error_mark_node;
11824 if (val != TREE_VALUE (t))
11825 return build_tree_list (TREE_PURPOSE (t), val);
11826 return t;
11827 }
11828
11829 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11830 unchanged or a new TREE_LIST chain. */
11831
11832 static tree
11833 tsubst_attributes (tree attributes, tree args,
11834 tsubst_flags_t complain, tree in_decl)
11835 {
11836 tree last_dep = NULL_TREE;
11837
11838 for (tree t = attributes; t; t = TREE_CHAIN (t))
11839 if (ATTR_IS_DEPENDENT (t))
11840 {
11841 last_dep = t;
11842 attributes = copy_list (attributes);
11843 break;
11844 }
11845
11846 if (last_dep)
11847 for (tree *p = &attributes; *p; )
11848 {
11849 tree t = *p;
11850 if (ATTR_IS_DEPENDENT (t))
11851 {
11852 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11853 if (subst != t)
11854 {
11855 *p = subst;
11856 while (*p)
11857 p = &TREE_CHAIN (*p);
11858 *p = TREE_CHAIN (t);
11859 continue;
11860 }
11861 }
11862 p = &TREE_CHAIN (*p);
11863 }
11864
11865 return attributes;
11866 }
11867
11868 /* Apply any attributes which had to be deferred until instantiation
11869 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11870 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11871 false on error. */
11872
11873 static bool
11874 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11875 tree args, tsubst_flags_t complain, tree in_decl)
11876 {
11877 tree t;
11878 tree *p;
11879
11880 if (attributes == NULL_TREE)
11881 return true;
11882
11883 if (DECL_P (*decl_p))
11884 {
11885 if (TREE_TYPE (*decl_p) == error_mark_node)
11886 return false;
11887 p = &DECL_ATTRIBUTES (*decl_p);
11888 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11889 to our attributes parameter. */
11890 gcc_assert (*p == attributes);
11891 }
11892 else
11893 {
11894 p = &TYPE_ATTRIBUTES (*decl_p);
11895 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11896 lookup_template_class_1, and should be preserved. */
11897 gcc_assert (*p != attributes);
11898 while (*p)
11899 p = &TREE_CHAIN (*p);
11900 }
11901
11902 /* save_template_attributes puts the dependent attributes at the beginning of
11903 the list; find the non-dependent ones. */
11904 for (t = attributes; t; t = TREE_CHAIN (t))
11905 if (!ATTR_IS_DEPENDENT (t))
11906 break;
11907 tree nondep = t;
11908
11909 /* Apply any non-dependent attributes. */
11910 *p = nondep;
11911
11912 if (nondep == attributes)
11913 return true;
11914
11915 /* And then any dependent ones. */
11916 tree late_attrs = NULL_TREE;
11917 tree *q = &late_attrs;
11918 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
11919 {
11920 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11921 if (*q == error_mark_node)
11922 return false;
11923 if (*q == t)
11924 {
11925 *q = copy_node (t);
11926 TREE_CHAIN (*q) = NULL_TREE;
11927 }
11928 while (*q)
11929 q = &TREE_CHAIN (*q);
11930 }
11931
11932 /* cplus_decl_attributes can add some attributes implicitly. For templates,
11933 those attributes should have been added already when those templates were
11934 parsed, and shouldn't be added based on from which context they are
11935 first time instantiated. */
11936 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
11937 auto o2 = make_temp_override (optimization_current_node,
11938 optimization_default_node);
11939 auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
11940 auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
11941 NULL);
11942 auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
11943
11944 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11945
11946 return true;
11947 }
11948
11949 /* The template TMPL is being instantiated with the template arguments TARGS.
11950 Perform the access checks that we deferred when parsing the template. */
11951
11952 static void
11953 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11954 {
11955 unsigned i;
11956 deferred_access_check *chk;
11957
11958 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11959 return;
11960
11961 if (vec<deferred_access_check, va_gc> *access_checks
11962 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11963 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11964 {
11965 tree decl = chk->decl;
11966 tree diag_decl = chk->diag_decl;
11967 tree type_scope = TREE_TYPE (chk->binfo);
11968
11969 if (uses_template_parms (type_scope))
11970 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11971
11972 /* Make access check error messages point to the location
11973 of the use of the typedef. */
11974 iloc_sentinel ils (chk->loc);
11975 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11976 decl, diag_decl, tf_warning_or_error);
11977 }
11978 }
11979
11980 tree
11981 instantiate_class_template (tree type)
11982 {
11983 auto_timevar tv (TV_TEMPLATE_INST);
11984
11985 tree templ, args, pattern, t, member;
11986 tree typedecl;
11987 tree pbinfo;
11988 tree base_list;
11989 unsigned int saved_maximum_field_alignment;
11990 tree fn_context;
11991
11992 if (type == error_mark_node)
11993 return error_mark_node;
11994
11995 if (COMPLETE_OR_OPEN_TYPE_P (type)
11996 || uses_template_parms (type))
11997 return type;
11998
11999 /* Figure out which template is being instantiated. */
12000 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
12001 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
12002
12003 /* Mark the type as in the process of being defined. */
12004 TYPE_BEING_DEFINED (type) = 1;
12005
12006 /* We may be in the middle of deferred access check. Disable
12007 it now. */
12008 deferring_access_check_sentinel acs (dk_no_deferred);
12009
12010 /* Determine what specialization of the original template to
12011 instantiate. */
12012 t = most_specialized_partial_spec (type, tf_warning_or_error);
12013 if (t == error_mark_node)
12014 return error_mark_node;
12015 else if (t)
12016 {
12017 /* This TYPE is actually an instantiation of a partial
12018 specialization. We replace the innermost set of ARGS with
12019 the arguments appropriate for substitution. For example,
12020 given:
12021
12022 template <class T> struct S {};
12023 template <class T> struct S<T*> {};
12024
12025 and supposing that we are instantiating S<int*>, ARGS will
12026 presently be {int*} -- but we need {int}. */
12027 pattern = TREE_TYPE (t);
12028 args = TREE_PURPOSE (t);
12029 }
12030 else
12031 {
12032 pattern = TREE_TYPE (templ);
12033 args = CLASSTYPE_TI_ARGS (type);
12034 }
12035
12036 /* If the template we're instantiating is incomplete, then clearly
12037 there's nothing we can do. */
12038 if (!COMPLETE_TYPE_P (pattern))
12039 {
12040 /* We can try again later. */
12041 TYPE_BEING_DEFINED (type) = 0;
12042 return type;
12043 }
12044
12045 /* If we've recursively instantiated too many templates, stop. */
12046 if (! push_tinst_level (type))
12047 return type;
12048
12049 int saved_unevaluated_operand = cp_unevaluated_operand;
12050 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12051
12052 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
12053 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
12054 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
12055 fn_context = error_mark_node;
12056 if (!fn_context)
12057 push_to_top_level ();
12058 else
12059 {
12060 cp_unevaluated_operand = 0;
12061 c_inhibit_evaluation_warnings = 0;
12062 }
12063 /* Use #pragma pack from the template context. */
12064 saved_maximum_field_alignment = maximum_field_alignment;
12065 maximum_field_alignment = TYPE_PRECISION (pattern);
12066
12067 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
12068
12069 /* Set the input location to the most specialized template definition.
12070 This is needed if tsubsting causes an error. */
12071 typedecl = TYPE_MAIN_DECL (pattern);
12072 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
12073 DECL_SOURCE_LOCATION (typedecl);
12074
12075 set_instantiating_module (TYPE_NAME (type));
12076
12077 TYPE_PACKED (type) = TYPE_PACKED (pattern);
12078 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
12079 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
12080 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
12081 if (ANON_AGGR_TYPE_P (pattern))
12082 SET_ANON_AGGR_TYPE_P (type);
12083 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
12084 {
12085 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
12086 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
12087 /* Adjust visibility for template arguments. */
12088 determine_visibility (TYPE_MAIN_DECL (type));
12089 }
12090 if (CLASS_TYPE_P (type))
12091 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
12092
12093 pbinfo = TYPE_BINFO (pattern);
12094
12095 /* We should never instantiate a nested class before its enclosing
12096 class; we need to look up the nested class by name before we can
12097 instantiate it, and that lookup should instantiate the enclosing
12098 class. */
12099 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
12100 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
12101
12102 base_list = NULL_TREE;
12103 /* Defer access checking while we substitute into the types named in
12104 the base-clause. */
12105 push_deferring_access_checks (dk_deferred);
12106 if (BINFO_N_BASE_BINFOS (pbinfo))
12107 {
12108 tree pbase_binfo;
12109 int i;
12110
12111 /* Substitute into each of the bases to determine the actual
12112 basetypes. */
12113 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
12114 {
12115 tree base;
12116 tree access = BINFO_BASE_ACCESS (pbinfo, i);
12117 tree expanded_bases = NULL_TREE;
12118 int idx, len = 1;
12119
12120 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
12121 {
12122 expanded_bases =
12123 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
12124 args, tf_error, NULL_TREE);
12125 if (expanded_bases == error_mark_node)
12126 continue;
12127
12128 len = TREE_VEC_LENGTH (expanded_bases);
12129 }
12130
12131 for (idx = 0; idx < len; idx++)
12132 {
12133 if (expanded_bases)
12134 /* Extract the already-expanded base class. */
12135 base = TREE_VEC_ELT (expanded_bases, idx);
12136 else
12137 /* Substitute to figure out the base class. */
12138 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
12139 NULL_TREE);
12140
12141 if (base == error_mark_node)
12142 continue;
12143
12144 base_list = tree_cons (access, base, base_list);
12145 if (BINFO_VIRTUAL_P (pbase_binfo))
12146 TREE_TYPE (base_list) = integer_type_node;
12147 }
12148 }
12149
12150 /* The list is now in reverse order; correct that. */
12151 base_list = nreverse (base_list);
12152 }
12153 /* Now call xref_basetypes to set up all the base-class
12154 information. */
12155 xref_basetypes (type, base_list);
12156
12157 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
12158 (int) ATTR_FLAG_TYPE_IN_PLACE,
12159 args, tf_error, NULL_TREE);
12160 fixup_attribute_variants (type);
12161
12162 /* Now that our base classes are set up, enter the scope of the
12163 class, so that name lookups into base classes, etc. will work
12164 correctly. This is precisely analogous to what we do in
12165 begin_class_definition when defining an ordinary non-template
12166 class, except we also need to push the enclosing classes. */
12167 push_nested_class (type);
12168
12169 /* Now check accessibility of the types named in its base-clause,
12170 relative to the scope of the class. */
12171 pop_to_parent_deferring_access_checks ();
12172
12173 /* A vector to hold members marked with attribute used. */
12174 auto_vec<tree> used;
12175
12176 /* Now members are processed in the order of declaration. */
12177 for (member = CLASSTYPE_DECL_LIST (pattern);
12178 member; member = TREE_CHAIN (member))
12179 {
12180 tree t = TREE_VALUE (member);
12181
12182 if (TREE_PURPOSE (member))
12183 {
12184 if (TYPE_P (t))
12185 {
12186 if (LAMBDA_TYPE_P (t))
12187 /* A closure type for a lambda in an NSDMI or default argument.
12188 Ignore it; it will be regenerated when needed. */
12189 continue;
12190
12191 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
12192 && TYPE_LANG_SPECIFIC (t)
12193 && CLASSTYPE_IS_TEMPLATE (t));
12194
12195 /* If the member is a class template, then -- even after
12196 substitution -- there may be dependent types in the
12197 template argument list for the class. We increment
12198 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
12199 that function will assume that no types are dependent
12200 when outside of a template. */
12201 if (class_template_p)
12202 ++processing_template_decl;
12203 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
12204 if (class_template_p)
12205 --processing_template_decl;
12206 if (newtag == error_mark_node)
12207 continue;
12208
12209 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
12210 {
12211 tree name = TYPE_IDENTIFIER (t);
12212
12213 if (class_template_p)
12214 /* Unfortunately, lookup_template_class sets
12215 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
12216 instantiation (i.e., for the type of a member
12217 template class nested within a template class.)
12218 This behavior is required for
12219 maybe_process_partial_specialization to work
12220 correctly, but is not accurate in this case;
12221 the TAG is not an instantiation of anything.
12222 (The corresponding TEMPLATE_DECL is an
12223 instantiation, but the TYPE is not.) */
12224 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
12225
12226 /* Now, install the tag. We don't use pushtag
12227 because that does too much work -- creating an
12228 implicit typedef, which we've already done. */
12229 set_identifier_type_value (name, TYPE_NAME (newtag));
12230 maybe_add_class_template_decl_list (type, newtag, false);
12231 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
12232 determine_visibility (TYPE_NAME (newtag));
12233 }
12234 }
12235 else if (DECL_DECLARES_FUNCTION_P (t))
12236 {
12237 tree r;
12238
12239 if (TREE_CODE (t) == TEMPLATE_DECL)
12240 ++processing_template_decl;
12241 r = tsubst (t, args, tf_error, NULL_TREE);
12242 if (TREE_CODE (t) == TEMPLATE_DECL)
12243 --processing_template_decl;
12244
12245 set_current_access_from_decl (r);
12246 finish_member_declaration (r);
12247 /* Instantiate members marked with attribute used. */
12248 if (r != error_mark_node && DECL_PRESERVE_P (r))
12249 used.safe_push (r);
12250 if (TREE_CODE (r) == FUNCTION_DECL
12251 && DECL_OMP_DECLARE_REDUCTION_P (r))
12252 cp_check_omp_declare_reduction (r);
12253 }
12254 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12255 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12256 /* A closure type for a lambda in an NSDMI or default argument.
12257 Ignore it; it will be regenerated when needed. */;
12258 else
12259 {
12260 /* Build new TYPE_FIELDS. */
12261 if (TREE_CODE (t) == STATIC_ASSERT)
12262 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE);
12263 else if (TREE_CODE (t) != CONST_DECL)
12264 {
12265 tree r;
12266 tree vec = NULL_TREE;
12267 int len = 1;
12268
12269 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12270 /* The file and line for this declaration, to
12271 assist in error message reporting. Since we
12272 called push_tinst_level above, we don't need to
12273 restore these. */
12274 input_location = DECL_SOURCE_LOCATION (t);
12275
12276 if (TREE_CODE (t) == TEMPLATE_DECL)
12277 ++processing_template_decl;
12278 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12279 if (TREE_CODE (t) == TEMPLATE_DECL)
12280 --processing_template_decl;
12281
12282 if (TREE_CODE (r) == TREE_VEC)
12283 {
12284 /* A capture pack became multiple fields. */
12285 vec = r;
12286 len = TREE_VEC_LENGTH (vec);
12287 }
12288
12289 for (int i = 0; i < len; ++i)
12290 {
12291 if (vec)
12292 r = TREE_VEC_ELT (vec, i);
12293 if (VAR_P (r))
12294 {
12295 /* In [temp.inst]:
12296
12297 [t]he initialization (and any associated
12298 side-effects) of a static data member does
12299 not occur unless the static data member is
12300 itself used in a way that requires the
12301 definition of the static data member to
12302 exist.
12303
12304 Therefore, we do not substitute into the
12305 initialized for the static data member here. */
12306 finish_static_data_member_decl
12307 (r,
12308 /*init=*/NULL_TREE,
12309 /*init_const_expr_p=*/false,
12310 /*asmspec_tree=*/NULL_TREE,
12311 /*flags=*/0);
12312 /* Instantiate members marked with attribute used. */
12313 if (r != error_mark_node && DECL_PRESERVE_P (r))
12314 used.safe_push (r);
12315 }
12316 else if (TREE_CODE (r) == FIELD_DECL)
12317 {
12318 /* Determine whether R has a valid type and can be
12319 completed later. If R is invalid, then its type
12320 is replaced by error_mark_node. */
12321 tree rtype = TREE_TYPE (r);
12322 if (can_complete_type_without_circularity (rtype))
12323 complete_type (rtype);
12324
12325 if (!complete_or_array_type_p (rtype))
12326 {
12327 /* If R's type couldn't be completed and
12328 it isn't a flexible array member (whose
12329 type is incomplete by definition) give
12330 an error. */
12331 cxx_incomplete_type_error (r, rtype);
12332 TREE_TYPE (r) = error_mark_node;
12333 }
12334 else if (TREE_CODE (rtype) == ARRAY_TYPE
12335 && TYPE_DOMAIN (rtype) == NULL_TREE
12336 && (TREE_CODE (type) == UNION_TYPE
12337 || TREE_CODE (type) == QUAL_UNION_TYPE))
12338 {
12339 error ("flexible array member %qD in union", r);
12340 TREE_TYPE (r) = error_mark_node;
12341 }
12342 else if (!verify_type_context (input_location,
12343 TCTX_FIELD, rtype))
12344 TREE_TYPE (r) = error_mark_node;
12345 }
12346
12347 /* If it is a TYPE_DECL for a class-scoped
12348 ENUMERAL_TYPE, such a thing will already have
12349 been added to the field list by tsubst_enum
12350 in finish_member_declaration case above. */
12351 if (!(TREE_CODE (r) == TYPE_DECL
12352 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12353 && DECL_ARTIFICIAL (r)))
12354 {
12355 set_current_access_from_decl (r);
12356 finish_member_declaration (r);
12357 }
12358 }
12359 }
12360 }
12361 }
12362 else
12363 {
12364 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12365 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12366 {
12367 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12368
12369 tree friend_type = t;
12370 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12371 {
12372 /* template <class T> friend class C; */
12373 friend_type = tsubst_friend_class (friend_type, args);
12374 }
12375 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12376 {
12377 /* template <class T> friend class C::D; */
12378 friend_type = tsubst (friend_type, args,
12379 tf_warning_or_error, NULL_TREE);
12380 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12381 friend_type = TREE_TYPE (friend_type);
12382 }
12383 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12384 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12385 {
12386 /* This could be either
12387
12388 friend class T::C;
12389
12390 when dependent_type_p is false or
12391
12392 template <class U> friend class T::C;
12393
12394 otherwise. */
12395 /* Bump processing_template_decl in case this is something like
12396 template <class T> friend struct A<T>::B. */
12397 ++processing_template_decl;
12398 friend_type = tsubst (friend_type, args,
12399 tf_warning_or_error, NULL_TREE);
12400 --processing_template_decl;
12401 }
12402 else if (uses_template_parms (friend_type))
12403 /* friend class C<T>; */
12404 friend_type = tsubst (friend_type, args,
12405 tf_warning_or_error, NULL_TREE);
12406
12407 /* Otherwise it's
12408
12409 friend class C;
12410
12411 where C is already declared or
12412
12413 friend class C<int>;
12414
12415 We don't have to do anything in these cases. */
12416
12417 if (friend_type != error_mark_node)
12418 make_friend_class (type, friend_type, /*complain=*/false);
12419 }
12420 else
12421 {
12422 /* Build new DECL_FRIENDLIST. */
12423 tree r;
12424
12425 /* The file and line for this declaration, to
12426 assist in error message reporting. Since we
12427 called push_tinst_level above, we don't need to
12428 restore these. */
12429 input_location = DECL_SOURCE_LOCATION (t);
12430
12431 if (TREE_CODE (t) == TEMPLATE_DECL)
12432 {
12433 ++processing_template_decl;
12434 push_deferring_access_checks (dk_no_check);
12435 }
12436
12437 r = tsubst_friend_function (t, args);
12438 add_friend (type, r, /*complain=*/false);
12439 if (TREE_CODE (t) == TEMPLATE_DECL)
12440 {
12441 pop_deferring_access_checks ();
12442 --processing_template_decl;
12443 }
12444 }
12445 }
12446 }
12447
12448 if (fn_context)
12449 {
12450 /* Restore these before substituting into the lambda capture
12451 initializers. */
12452 cp_unevaluated_operand = saved_unevaluated_operand;
12453 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12454 }
12455
12456 /* Set the file and line number information to whatever is given for
12457 the class itself. This puts error messages involving generated
12458 implicit functions at a predictable point, and the same point
12459 that would be used for non-template classes. */
12460 input_location = DECL_SOURCE_LOCATION (typedecl);
12461
12462 unreverse_member_declarations (type);
12463 finish_struct_1 (type);
12464 TYPE_BEING_DEFINED (type) = 0;
12465
12466 /* Remember if instantiating this class ran into errors, so we can avoid
12467 instantiating member functions in limit_bad_template_recursion. We set
12468 this flag even if the problem was in another instantiation triggered by
12469 this one, as that will likely also cause trouble for member functions. */
12470 if (errorcount + sorrycount > current_tinst_level->errors)
12471 CLASSTYPE_ERRONEOUS (type) = true;
12472
12473 /* We don't instantiate default arguments for member functions. 14.7.1:
12474
12475 The implicit instantiation of a class template specialization causes
12476 the implicit instantiation of the declarations, but not of the
12477 definitions or default arguments, of the class member functions,
12478 member classes, static data members and member templates.... */
12479
12480 perform_instantiation_time_access_checks (pattern, args);
12481 perform_deferred_access_checks (tf_warning_or_error);
12482
12483 /* Now that we've gone through all the members, instantiate those
12484 marked with attribute used. We must do this in the context of
12485 the class -- not the context we pushed from, as that might be
12486 inside a template and change the behaviour of mark_used. */
12487 for (tree x : used)
12488 mark_used (x);
12489
12490 pop_nested_class ();
12491 maximum_field_alignment = saved_maximum_field_alignment;
12492 if (!fn_context)
12493 pop_from_top_level ();
12494 pop_tinst_level ();
12495
12496 /* The vtable for a template class can be emitted in any translation
12497 unit in which the class is instantiated. When there is no key
12498 method, however, finish_struct_1 will already have added TYPE to
12499 the keyed_classes. */
12500 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12501 vec_safe_push (keyed_classes, type);
12502
12503 return type;
12504 }
12505
12506 tree
12507 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12508 {
12509 tree r;
12510
12511 if (!t)
12512 r = t;
12513 else if (TYPE_P (t))
12514 r = tsubst (t, args, complain, in_decl);
12515 else
12516 {
12517 if (!(complain & tf_warning))
12518 ++c_inhibit_evaluation_warnings;
12519 r = tsubst_expr (t, args, complain, in_decl);
12520 if (!(complain & tf_warning))
12521 --c_inhibit_evaluation_warnings;
12522 }
12523
12524 return r;
12525 }
12526
12527 /* Given a function parameter pack TMPL_PARM and some function parameters
12528 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12529 and set *SPEC_P to point at the next point in the list. */
12530
12531 tree
12532 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12533 {
12534 /* Collect all of the extra "packed" parameters into an
12535 argument pack. */
12536 tree argpack;
12537 tree spec_parm = *spec_p;
12538 int len;
12539
12540 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12541 if (tmpl_parm
12542 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12543 break;
12544
12545 spec_parm = *spec_p;
12546 if (len == 1 && DECL_PACK_P (spec_parm))
12547 {
12548 /* The instantiation is still a parameter pack; don't wrap it in a
12549 NONTYPE_ARGUMENT_PACK. */
12550 argpack = spec_parm;
12551 spec_parm = DECL_CHAIN (spec_parm);
12552 }
12553 else
12554 {
12555 /* Fill in PARMVEC with all of the parameters. */
12556 tree parmvec = make_tree_vec (len);
12557 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12558 for (int i = 0; i < len; i++)
12559 {
12560 tree elt = spec_parm;
12561 if (DECL_PACK_P (elt))
12562 elt = make_pack_expansion (elt);
12563 TREE_VEC_ELT (parmvec, i) = elt;
12564 spec_parm = DECL_CHAIN (spec_parm);
12565 }
12566
12567 /* Build the argument packs. */
12568 ARGUMENT_PACK_ARGS (argpack) = parmvec;
12569 }
12570 *spec_p = spec_parm;
12571
12572 return argpack;
12573 }
12574
12575 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12576 NONTYPE_ARGUMENT_PACK. */
12577
12578 static tree
12579 make_fnparm_pack (tree spec_parm)
12580 {
12581 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12582 }
12583
12584 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12585 pack expansion with no extra args, 2 if it has extra args, or 0
12586 if it is not a pack expansion. */
12587
12588 static int
12589 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12590 {
12591 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12592 /* We're being called before this happens in tsubst_pack_expansion. */
12593 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12594 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12595 if (i >= TREE_VEC_LENGTH (vec))
12596 return 0;
12597 tree elt = TREE_VEC_ELT (vec, i);
12598 if (DECL_P (elt))
12599 /* A decl pack is itself an expansion. */
12600 elt = TREE_TYPE (elt);
12601 if (!PACK_EXPANSION_P (elt))
12602 return 0;
12603 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12604 return 2;
12605 return 1;
12606 }
12607
12608
12609 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12610
12611 static tree
12612 make_argument_pack_select (tree arg_pack, unsigned index)
12613 {
12614 tree aps = make_node (ARGUMENT_PACK_SELECT);
12615
12616 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12617 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12618
12619 return aps;
12620 }
12621
12622 /* This is a subroutine of tsubst_pack_expansion.
12623
12624 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12625 mechanism to store the (non complete list of) arguments of the
12626 substitution and return a non substituted pack expansion, in order
12627 to wait for when we have enough arguments to really perform the
12628 substitution. */
12629
12630 static bool
12631 use_pack_expansion_extra_args_p (tree t,
12632 tree parm_packs,
12633 int arg_pack_len,
12634 bool has_empty_arg)
12635 {
12636 if (has_empty_arg
12637 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
12638 return true;
12639
12640 /* If one pack has an expansion and another pack has a normal
12641 argument or if one pack has an empty argument and an another
12642 one hasn't then tsubst_pack_expansion cannot perform the
12643 substitution and need to fall back on the
12644 PACK_EXPANSION_EXTRA mechanism. */
12645 if (parm_packs == NULL_TREE)
12646 return false;
12647 else if (has_empty_arg)
12648 {
12649 /* If all the actual packs are pack expansions, we can still
12650 subsitute directly. */
12651 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12652 {
12653 tree a = TREE_VALUE (p);
12654 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12655 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12656 a = ARGUMENT_PACK_ARGS (a);
12657 if (TREE_VEC_LENGTH (a) == 1)
12658 a = TREE_VEC_ELT (a, 0);
12659 if (PACK_EXPANSION_P (a))
12660 continue;
12661 return true;
12662 }
12663 return false;
12664 }
12665
12666 for (int i = 0 ; i < arg_pack_len; ++i)
12667 {
12668 bool has_expansion_arg = false;
12669 bool has_non_expansion_arg = false;
12670 for (tree parm_pack = parm_packs;
12671 parm_pack;
12672 parm_pack = TREE_CHAIN (parm_pack))
12673 {
12674 tree arg = TREE_VALUE (parm_pack);
12675
12676 int exp = argument_pack_element_is_expansion_p (arg, i);
12677 if (exp == 2)
12678 /* We can't substitute a pack expansion with extra args into
12679 our pattern. */
12680 return true;
12681 else if (exp)
12682 has_expansion_arg = true;
12683 else
12684 has_non_expansion_arg = true;
12685 }
12686
12687 if (has_expansion_arg && has_non_expansion_arg)
12688 {
12689 gcc_checking_assert (false);
12690 return true;
12691 }
12692 }
12693 return false;
12694 }
12695
12696 /* [temp.variadic]/6 says that:
12697
12698 The instantiation of a pack expansion [...]
12699 produces a list E1,E2, ..., En, where N is the number of elements
12700 in the pack expansion parameters.
12701
12702 This subroutine of tsubst_pack_expansion produces one of these Ei.
12703
12704 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12705 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12706 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12707 INDEX is the index 'i' of the element Ei to produce. ARGS,
12708 COMPLAIN, and IN_DECL are the same parameters as for the
12709 tsubst_pack_expansion function.
12710
12711 The function returns the resulting Ei upon successful completion,
12712 or error_mark_node.
12713
12714 Note that this function possibly modifies the ARGS parameter, so
12715 it's the responsibility of the caller to restore it. */
12716
12717 static tree
12718 gen_elem_of_pack_expansion_instantiation (tree pattern,
12719 tree parm_packs,
12720 unsigned index,
12721 tree args /* This parm gets
12722 modified. */,
12723 tsubst_flags_t complain,
12724 tree in_decl)
12725 {
12726 tree t;
12727 bool ith_elem_is_expansion = false;
12728
12729 /* For each parameter pack, change the substitution of the parameter
12730 pack to the ith argument in its argument pack, then expand the
12731 pattern. */
12732 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12733 {
12734 tree parm = TREE_PURPOSE (pack);
12735 tree arg_pack = TREE_VALUE (pack);
12736 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12737
12738 ith_elem_is_expansion |=
12739 argument_pack_element_is_expansion_p (arg_pack, index);
12740
12741 /* Select the Ith argument from the pack. */
12742 if (TREE_CODE (parm) == PARM_DECL
12743 || VAR_P (parm)
12744 || TREE_CODE (parm) == FIELD_DECL)
12745 {
12746 if (index == 0)
12747 {
12748 aps = make_argument_pack_select (arg_pack, index);
12749 if (!mark_used (parm, complain) && !(complain & tf_error))
12750 return error_mark_node;
12751 register_local_specialization (aps, parm);
12752 }
12753 else
12754 aps = retrieve_local_specialization (parm);
12755 }
12756 else
12757 {
12758 int idx, level;
12759 template_parm_level_and_index (parm, &level, &idx);
12760
12761 if (index == 0)
12762 {
12763 aps = make_argument_pack_select (arg_pack, index);
12764 /* Update the corresponding argument. */
12765 TMPL_ARG (args, level, idx) = aps;
12766 }
12767 else
12768 /* Re-use the ARGUMENT_PACK_SELECT. */
12769 aps = TMPL_ARG (args, level, idx);
12770 }
12771 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12772 }
12773
12774 /* Substitute into the PATTERN with the (possibly altered)
12775 arguments. */
12776 if (pattern == in_decl)
12777 /* Expanding a fixed parameter pack from
12778 coerce_template_parameter_pack. */
12779 t = tsubst_decl (pattern, args, complain);
12780 else if (pattern == error_mark_node)
12781 t = error_mark_node;
12782 else if (!TYPE_P (pattern))
12783 t = tsubst_expr (pattern, args, complain, in_decl);
12784 else
12785 {
12786 t = tsubst (pattern, args, complain, in_decl);
12787 if (is_auto (t) && !ith_elem_is_expansion)
12788 /* When expanding the fake auto... pack expansion from add_capture, we
12789 need to mark that the expansion is no longer a pack. */
12790 TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
12791 }
12792
12793 /* If the Ith argument pack element is a pack expansion, then
12794 the Ith element resulting from the substituting is going to
12795 be a pack expansion as well. */
12796 if (ith_elem_is_expansion)
12797 t = make_pack_expansion (t, complain);
12798
12799 return t;
12800 }
12801
12802 /* When the unexpanded parameter pack in a fold expression expands to an empty
12803 sequence, the value of the expression is as follows; the program is
12804 ill-formed if the operator is not listed in this table.
12805
12806 && true
12807 || false
12808 , void() */
12809
12810 tree
12811 expand_empty_fold (tree t, tsubst_flags_t complain)
12812 {
12813 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12814 if (!FOLD_EXPR_MODIFY_P (t))
12815 switch (code)
12816 {
12817 case TRUTH_ANDIF_EXPR:
12818 return boolean_true_node;
12819 case TRUTH_ORIF_EXPR:
12820 return boolean_false_node;
12821 case COMPOUND_EXPR:
12822 return void_node;
12823 default:
12824 break;
12825 }
12826
12827 if (complain & tf_error)
12828 error_at (location_of (t),
12829 "fold of empty expansion over %O", code);
12830 return error_mark_node;
12831 }
12832
12833 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12834 form an expression that combines the two terms using the
12835 operator of T. */
12836
12837 static tree
12838 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12839 {
12840 tree_code code = FOLD_EXPR_OP (t);
12841
12842 tree lookups = templated_operator_saved_lookups (t);
12843
12844 // Handle compound assignment operators.
12845 if (FOLD_EXPR_MODIFY_P (t))
12846 return build_x_modify_expr (input_location, left, code, right,
12847 lookups, complain);
12848
12849 warning_sentinel s(warn_parentheses);
12850 switch (code)
12851 {
12852 case COMPOUND_EXPR:
12853 return build_x_compound_expr (input_location, left, right,
12854 lookups, complain);
12855 default:
12856 return build_x_binary_op (input_location, code,
12857 left, TREE_CODE (left),
12858 right, TREE_CODE (right),
12859 lookups, /*overload=*/NULL,
12860 complain);
12861 }
12862 }
12863
12864 /* Substitute ARGS into the pack of a fold expression T. */
12865
12866 static inline tree
12867 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12868 {
12869 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12870 }
12871
12872 /* Substitute ARGS into the pack of a fold expression T. */
12873
12874 static inline tree
12875 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12876 {
12877 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
12878 }
12879
12880 /* Expand a PACK of arguments into a grouped as left fold.
12881 Given a pack containing elements A0, A1, ..., An and an
12882 operator @, this builds the expression:
12883
12884 ((A0 @ A1) @ A2) ... @ An
12885
12886 Note that PACK must not be empty.
12887
12888 The operator is defined by the original fold expression T. */
12889
12890 static tree
12891 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12892 {
12893 tree left = TREE_VEC_ELT (pack, 0);
12894 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12895 {
12896 tree right = TREE_VEC_ELT (pack, i);
12897 left = fold_expression (t, left, right, complain);
12898 }
12899 return left;
12900 }
12901
12902 /* Substitute into a unary left fold expression. */
12903
12904 static tree
12905 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12906 tree in_decl)
12907 {
12908 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12909 if (pack == error_mark_node)
12910 return error_mark_node;
12911 if (PACK_EXPANSION_P (pack))
12912 {
12913 tree r = copy_node (t);
12914 FOLD_EXPR_PACK (r) = pack;
12915 return r;
12916 }
12917 if (TREE_VEC_LENGTH (pack) == 0)
12918 return expand_empty_fold (t, complain);
12919 else
12920 return expand_left_fold (t, pack, complain);
12921 }
12922
12923 /* Substitute into a binary left fold expression.
12924
12925 Do ths by building a single (non-empty) vector of argumnts and
12926 building the expression from those elements. */
12927
12928 static tree
12929 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12930 tree in_decl)
12931 {
12932 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12933 if (pack == error_mark_node)
12934 return error_mark_node;
12935 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12936 if (init == error_mark_node)
12937 return error_mark_node;
12938
12939 if (PACK_EXPANSION_P (pack))
12940 {
12941 tree r = copy_node (t);
12942 FOLD_EXPR_PACK (r) = pack;
12943 FOLD_EXPR_INIT (r) = init;
12944 return r;
12945 }
12946
12947 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12948 TREE_VEC_ELT (vec, 0) = init;
12949 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12950 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12951
12952 return expand_left_fold (t, vec, complain);
12953 }
12954
12955 /* Expand a PACK of arguments into a grouped as right fold.
12956 Given a pack containing elementns A0, A1, ..., and an
12957 operator @, this builds the expression:
12958
12959 A0@ ... (An-2 @ (An-1 @ An))
12960
12961 Note that PACK must not be empty.
12962
12963 The operator is defined by the original fold expression T. */
12964
12965 tree
12966 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12967 {
12968 // Build the expression.
12969 int n = TREE_VEC_LENGTH (pack);
12970 tree right = TREE_VEC_ELT (pack, n - 1);
12971 for (--n; n != 0; --n)
12972 {
12973 tree left = TREE_VEC_ELT (pack, n - 1);
12974 right = fold_expression (t, left, right, complain);
12975 }
12976 return right;
12977 }
12978
12979 /* Substitute into a unary right fold expression. */
12980
12981 static tree
12982 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12983 tree in_decl)
12984 {
12985 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12986 if (pack == error_mark_node)
12987 return error_mark_node;
12988 if (PACK_EXPANSION_P (pack))
12989 {
12990 tree r = copy_node (t);
12991 FOLD_EXPR_PACK (r) = pack;
12992 return r;
12993 }
12994 if (TREE_VEC_LENGTH (pack) == 0)
12995 return expand_empty_fold (t, complain);
12996 else
12997 return expand_right_fold (t, pack, complain);
12998 }
12999
13000 /* Substitute into a binary right fold expression.
13001
13002 Do ths by building a single (non-empty) vector of arguments and
13003 building the expression from those elements. */
13004
13005 static tree
13006 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
13007 tree in_decl)
13008 {
13009 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
13010 if (pack == error_mark_node)
13011 return error_mark_node;
13012 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
13013 if (init == error_mark_node)
13014 return error_mark_node;
13015
13016 if (PACK_EXPANSION_P (pack))
13017 {
13018 tree r = copy_node (t);
13019 FOLD_EXPR_PACK (r) = pack;
13020 FOLD_EXPR_INIT (r) = init;
13021 return r;
13022 }
13023
13024 int n = TREE_VEC_LENGTH (pack);
13025 tree vec = make_tree_vec (n + 1);
13026 for (int i = 0; i < n; ++i)
13027 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
13028 TREE_VEC_ELT (vec, n) = init;
13029
13030 return expand_right_fold (t, vec, complain);
13031 }
13032
13033 /* Walk through the pattern of a pack expansion, adding everything in
13034 local_specializations to a list. */
13035
13036 class el_data
13037 {
13038 public:
13039 /* Set of variables declared within the pattern. */
13040 hash_set<tree> internal;
13041 /* Set of AST nodes that have been visited by the traversal. */
13042 hash_set<tree> visited;
13043 /* List of local_specializations used within the pattern. */
13044 tree extra;
13045 tsubst_flags_t complain;
13046 /* True iff we don't want to walk into unevaluated contexts. */
13047 bool skip_unevaluated_operands = false;
13048
13049 el_data (tsubst_flags_t c)
13050 : extra (NULL_TREE), complain (c) {}
13051 };
13052 static tree
13053 extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
13054 {
13055 el_data &data = *reinterpret_cast<el_data*>(data_);
13056 tree *extra = &data.extra;
13057 tsubst_flags_t complain = data.complain;
13058
13059 if (data.skip_unevaluated_operands
13060 && unevaluated_p (TREE_CODE (*tp)))
13061 {
13062 *walk_subtrees = 0;
13063 return NULL_TREE;
13064 }
13065
13066 if (TYPE_P (*tp) && typedef_variant_p (*tp))
13067 /* Remember local typedefs (85214). */
13068 tp = &TYPE_NAME (*tp);
13069
13070 if (TREE_CODE (*tp) == DECL_EXPR)
13071 {
13072 tree decl = DECL_EXPR_DECL (*tp);
13073 data.internal.add (decl);
13074 if (VAR_P (decl)
13075 && DECL_DECOMPOSITION_P (decl)
13076 && TREE_TYPE (decl) != error_mark_node)
13077 {
13078 gcc_assert (DECL_NAME (decl) == NULL_TREE);
13079 for (tree decl2 = DECL_CHAIN (decl);
13080 decl2
13081 && VAR_P (decl2)
13082 && DECL_DECOMPOSITION_P (decl2)
13083 && DECL_NAME (decl2)
13084 && TREE_TYPE (decl2) != error_mark_node;
13085 decl2 = DECL_CHAIN (decl2))
13086 {
13087 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
13088 data.internal.add (decl2);
13089 }
13090 }
13091 }
13092 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
13093 {
13094 /* Since we defer implicit capture, look in the parms and body. */
13095 tree fn = lambda_function (*tp);
13096 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
13097 &data.visited);
13098 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
13099 &data.visited);
13100 }
13101 else if (tree spec = retrieve_local_specialization (*tp))
13102 {
13103 if (data.internal.contains (*tp))
13104 /* Don't mess with variables declared within the pattern. */
13105 return NULL_TREE;
13106 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13107 {
13108 /* Maybe pull out the PARM_DECL for a partial instantiation. */
13109 tree args = ARGUMENT_PACK_ARGS (spec);
13110 if (TREE_VEC_LENGTH (args) == 1)
13111 {
13112 tree elt = TREE_VEC_ELT (args, 0);
13113 if (PACK_EXPANSION_P (elt))
13114 elt = PACK_EXPANSION_PATTERN (elt);
13115 if (DECL_PACK_P (elt))
13116 spec = elt;
13117 }
13118 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
13119 {
13120 /* Handle lambda capture here, since we aren't doing any
13121 substitution now, and so tsubst_copy won't call
13122 process_outer_var_ref. */
13123 tree args = ARGUMENT_PACK_ARGS (spec);
13124 int len = TREE_VEC_LENGTH (args);
13125 for (int i = 0; i < len; ++i)
13126 {
13127 tree arg = TREE_VEC_ELT (args, i);
13128 tree carg = arg;
13129 if (outer_automatic_var_p (arg))
13130 carg = process_outer_var_ref (arg, complain);
13131 if (carg != arg)
13132 {
13133 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
13134 proxies. */
13135 if (i == 0)
13136 {
13137 spec = copy_node (spec);
13138 args = copy_node (args);
13139 ARGUMENT_PACK_ARGS (spec) = args;
13140 register_local_specialization (spec, *tp);
13141 }
13142 TREE_VEC_ELT (args, i) = carg;
13143 }
13144 }
13145 }
13146 }
13147 if (outer_automatic_var_p (spec))
13148 spec = process_outer_var_ref (spec, complain);
13149 *extra = tree_cons (*tp, spec, *extra);
13150 }
13151 return NULL_TREE;
13152 }
13153 static tree
13154 extract_local_specs (tree pattern, tsubst_flags_t complain)
13155 {
13156 el_data data (complain);
13157 /* Walk the pattern twice, ignoring unevaluated operands the first time
13158 around, so that if a local specialization appears in both an evaluated
13159 and unevaluated context we prefer to process it in the evaluated context
13160 (since e.g. process_outer_var_ref is a no-op inside an unevaluated
13161 context). */
13162 data.skip_unevaluated_operands = true;
13163 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13164 data.skip_unevaluated_operands = false;
13165 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
13166 return data.extra;
13167 }
13168
13169 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
13170 for use in PACK_EXPANSION_EXTRA_ARGS. */
13171
13172 tree
13173 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
13174 {
13175 /* Make a copy of the extra arguments so that they won't get changed
13176 out from under us. */
13177 tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
13178 if (local_specializations)
13179 if (tree locals = extract_local_specs (pattern, complain))
13180 extra = tree_cons (NULL_TREE, extra, locals);
13181 return extra;
13182 }
13183
13184 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
13185 normal template args to ARGS. */
13186
13187 tree
13188 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
13189 {
13190 if (extra && TREE_CODE (extra) == TREE_LIST)
13191 {
13192 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
13193 {
13194 /* The partial instantiation involved local declarations collected in
13195 extract_local_specs; map from the general template to our local
13196 context. */
13197 tree gen = TREE_PURPOSE (elt);
13198 tree inst = TREE_VALUE (elt);
13199 if (DECL_P (inst))
13200 if (tree local = retrieve_local_specialization (inst))
13201 inst = local;
13202 /* else inst is already a full instantiation of the pack. */
13203 register_local_specialization (inst, gen);
13204 }
13205 gcc_assert (!TREE_PURPOSE (extra));
13206 extra = TREE_VALUE (extra);
13207 }
13208 if (uses_template_parms (extra))
13209 {
13210 /* This can happen after dependent substitution into a
13211 requires-expr or a lambda that uses constexpr if. */
13212 extra = tsubst_template_args (extra, args, complain, in_decl);
13213 args = add_outermost_template_args (args, extra);
13214 }
13215 else
13216 args = add_to_template_args (extra, args);
13217 return args;
13218 }
13219
13220 /* Substitute ARGS into T, which is an pack expansion
13221 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
13222 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
13223 (if only a partial substitution could be performed) or
13224 ERROR_MARK_NODE if there was an error. */
13225 tree
13226 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
13227 tree in_decl)
13228 {
13229 tree pattern;
13230 tree pack, packs = NULL_TREE;
13231 bool unsubstituted_packs = false;
13232 int i, len = -1;
13233 tree result;
13234 bool need_local_specializations = false;
13235 int levels;
13236
13237 gcc_assert (PACK_EXPANSION_P (t));
13238 pattern = PACK_EXPANSION_PATTERN (t);
13239
13240 /* Add in any args remembered from an earlier partial instantiation. */
13241 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
13242
13243 levels = TMPL_ARGS_DEPTH (args);
13244
13245 /* Determine the argument packs that will instantiate the parameter
13246 packs used in the expansion expression. While we're at it,
13247 compute the number of arguments to be expanded and make sure it
13248 is consistent. */
13249 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13250 pack = TREE_CHAIN (pack))
13251 {
13252 tree parm_pack = TREE_VALUE (pack);
13253 tree arg_pack = NULL_TREE;
13254 tree orig_arg = NULL_TREE;
13255 int level = 0;
13256
13257 if (TREE_CODE (parm_pack) == BASES)
13258 {
13259 gcc_assert (parm_pack == pattern);
13260 if (BASES_DIRECT (parm_pack))
13261 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
13262 args, complain,
13263 in_decl),
13264 complain);
13265 else
13266 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
13267 args, complain, in_decl),
13268 complain);
13269 }
13270 else if (builtin_pack_call_p (parm_pack))
13271 {
13272 if (parm_pack != pattern)
13273 {
13274 if (complain & tf_error)
13275 sorry ("%qE is not the entire pattern of the pack expansion",
13276 parm_pack);
13277 return error_mark_node;
13278 }
13279 return expand_builtin_pack_call (parm_pack, args,
13280 complain, in_decl);
13281 }
13282 else if (TREE_CODE (parm_pack) == PARM_DECL)
13283 {
13284 /* We know we have correct local_specializations if this
13285 expansion is at function scope, or if we're dealing with a
13286 local parameter in a requires expression; for the latter,
13287 tsubst_requires_expr set it up appropriately. */
13288 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13289 arg_pack = retrieve_local_specialization (parm_pack);
13290 else
13291 /* We can't rely on local_specializations for a parameter
13292 name used later in a function declaration (such as in a
13293 late-specified return type). Even if it exists, it might
13294 have the wrong value for a recursive call. */
13295 need_local_specializations = true;
13296
13297 if (!arg_pack)
13298 {
13299 /* This parameter pack was used in an unevaluated context. Just
13300 make a dummy decl, since it's only used for its type. */
13301 ++cp_unevaluated_operand;
13302 arg_pack = tsubst_decl (parm_pack, args, complain);
13303 --cp_unevaluated_operand;
13304 if (arg_pack && DECL_PACK_P (arg_pack))
13305 /* Partial instantiation of the parm_pack, we can't build
13306 up an argument pack yet. */
13307 arg_pack = NULL_TREE;
13308 else
13309 arg_pack = make_fnparm_pack (arg_pack);
13310 }
13311 else if (DECL_PACK_P (arg_pack))
13312 /* This argument pack isn't fully instantiated yet. */
13313 arg_pack = NULL_TREE;
13314 }
13315 else if (is_capture_proxy (parm_pack))
13316 {
13317 arg_pack = retrieve_local_specialization (parm_pack);
13318 if (DECL_PACK_P (arg_pack))
13319 arg_pack = NULL_TREE;
13320 }
13321 else
13322 {
13323 int idx;
13324 template_parm_level_and_index (parm_pack, &level, &idx);
13325 if (level <= levels)
13326 arg_pack = TMPL_ARG (args, level, idx);
13327
13328 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13329 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13330 arg_pack = NULL_TREE;
13331 }
13332
13333 orig_arg = arg_pack;
13334 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13335 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13336
13337 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13338 /* This can only happen if we forget to expand an argument
13339 pack somewhere else. Just return an error, silently. */
13340 {
13341 result = make_tree_vec (1);
13342 TREE_VEC_ELT (result, 0) = error_mark_node;
13343 return result;
13344 }
13345
13346 if (arg_pack)
13347 {
13348 int my_len =
13349 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13350
13351 /* Don't bother trying to do a partial substitution with
13352 incomplete packs; we'll try again after deduction. */
13353 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13354 return t;
13355
13356 if (len < 0)
13357 len = my_len;
13358 else if (len != my_len)
13359 {
13360 if (!(complain & tf_error))
13361 /* Fail quietly. */;
13362 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13363 error ("mismatched argument pack lengths while expanding %qT",
13364 pattern);
13365 else
13366 error ("mismatched argument pack lengths while expanding %qE",
13367 pattern);
13368 return error_mark_node;
13369 }
13370
13371 /* Keep track of the parameter packs and their corresponding
13372 argument packs. */
13373 packs = tree_cons (parm_pack, arg_pack, packs);
13374 TREE_TYPE (packs) = orig_arg;
13375 }
13376 else
13377 {
13378 /* We can't substitute for this parameter pack. We use a flag as
13379 well as the missing_level counter because function parameter
13380 packs don't have a level. */
13381 gcc_assert (processing_template_decl || is_auto (parm_pack));
13382 unsubstituted_packs = true;
13383 }
13384 }
13385
13386 /* If the expansion is just T..., return the matching argument pack, unless
13387 we need to call convert_from_reference on all the elements. This is an
13388 important optimization; see c++/68422. */
13389 if (!unsubstituted_packs
13390 && TREE_PURPOSE (packs) == pattern)
13391 {
13392 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13393
13394 /* If the argument pack is a single pack expansion, pull it out. */
13395 if (TREE_VEC_LENGTH (args) == 1
13396 && pack_expansion_args_count (args))
13397 return TREE_VEC_ELT (args, 0);
13398
13399 /* Types need no adjustment, nor does sizeof..., and if we still have
13400 some pack expansion args we won't do anything yet. */
13401 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13402 || PACK_EXPANSION_SIZEOF_P (t)
13403 || pack_expansion_args_count (args))
13404 return args;
13405 /* Also optimize expression pack expansions if we can tell that the
13406 elements won't have reference type. */
13407 tree type = TREE_TYPE (pattern);
13408 if (type && !TYPE_REF_P (type)
13409 && !PACK_EXPANSION_P (type)
13410 && !WILDCARD_TYPE_P (type))
13411 return args;
13412 /* Otherwise use the normal path so we get convert_from_reference. */
13413 }
13414
13415 /* We cannot expand this expansion expression, because we don't have
13416 all of the argument packs we need. */
13417 if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
13418 {
13419 /* We got some full packs, but we can't substitute them in until we
13420 have values for all the packs. So remember these until then. */
13421
13422 t = make_pack_expansion (pattern, complain);
13423 PACK_EXPANSION_EXTRA_ARGS (t)
13424 = build_extra_args (pattern, args, complain);
13425 return t;
13426 }
13427
13428 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13429 type, so create our own local specializations map; the current map is
13430 either NULL or (in the case of recursive unification) might have
13431 bindings that we don't want to use or alter. */
13432 local_specialization_stack lss (need_local_specializations
13433 ? lss_blank : lss_nop);
13434
13435 if (unsubstituted_packs)
13436 {
13437 /* There were no real arguments, we're just replacing a parameter
13438 pack with another version of itself. Substitute into the
13439 pattern and return a PACK_EXPANSION_*. The caller will need to
13440 deal with that. */
13441 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13442 result = tsubst_expr (pattern, args, complain, in_decl);
13443 else
13444 result = tsubst (pattern, args, complain, in_decl);
13445 result = make_pack_expansion (result, complain);
13446 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13447 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13448 if (PACK_EXPANSION_AUTO_P (t))
13449 {
13450 /* This is a fake auto... pack expansion created in add_capture with
13451 _PACKS that don't appear in the pattern. Copy one over. */
13452 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13453 pack = retrieve_local_specialization (TREE_VALUE (packs));
13454 gcc_checking_assert (DECL_PACK_P (pack));
13455 PACK_EXPANSION_PARAMETER_PACKS (result)
13456 = build_tree_list (NULL_TREE, pack);
13457 PACK_EXPANSION_AUTO_P (result) = true;
13458 }
13459 return result;
13460 }
13461
13462 gcc_assert (len >= 0);
13463
13464 /* For each argument in each argument pack, substitute into the
13465 pattern. */
13466 result = make_tree_vec (len);
13467 tree elem_args = copy_template_args (args);
13468 for (i = 0; i < len; ++i)
13469 {
13470 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13471 i,
13472 elem_args, complain,
13473 in_decl);
13474 TREE_VEC_ELT (result, i) = t;
13475 if (t == error_mark_node)
13476 {
13477 result = error_mark_node;
13478 break;
13479 }
13480 }
13481
13482 /* Update ARGS to restore the substitution from parameter packs to
13483 their argument packs. */
13484 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13485 {
13486 tree parm = TREE_PURPOSE (pack);
13487
13488 if (TREE_CODE (parm) == PARM_DECL
13489 || VAR_P (parm)
13490 || TREE_CODE (parm) == FIELD_DECL)
13491 register_local_specialization (TREE_TYPE (pack), parm);
13492 else
13493 {
13494 int idx, level;
13495
13496 if (TREE_VALUE (pack) == NULL_TREE)
13497 continue;
13498
13499 template_parm_level_and_index (parm, &level, &idx);
13500
13501 /* Update the corresponding argument. */
13502 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13503 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13504 TREE_TYPE (pack);
13505 else
13506 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13507 }
13508 }
13509
13510 /* If the dependent pack arguments were such that we end up with only a
13511 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13512 if (len == 1 && TREE_CODE (result) == TREE_VEC
13513 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13514 return TREE_VEC_ELT (result, 0);
13515
13516 return result;
13517 }
13518
13519 /* Make an argument pack out of the TREE_VEC VEC. */
13520
13521 static tree
13522 make_argument_pack (tree vec)
13523 {
13524 tree pack;
13525
13526 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13527 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13528 else
13529 {
13530 pack = make_node (NONTYPE_ARGUMENT_PACK);
13531 TREE_CONSTANT (pack) = 1;
13532 }
13533 ARGUMENT_PACK_ARGS (pack) = vec;
13534 return pack;
13535 }
13536
13537 /* Return an exact copy of template args T that can be modified
13538 independently. */
13539
13540 static tree
13541 copy_template_args (tree t)
13542 {
13543 if (t == error_mark_node)
13544 return t;
13545
13546 int len = TREE_VEC_LENGTH (t);
13547 tree new_vec = make_tree_vec (len);
13548
13549 for (int i = 0; i < len; ++i)
13550 {
13551 tree elt = TREE_VEC_ELT (t, i);
13552 if (elt && TREE_CODE (elt) == TREE_VEC)
13553 elt = copy_template_args (elt);
13554 TREE_VEC_ELT (new_vec, i) = elt;
13555 }
13556
13557 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13558 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13559
13560 return new_vec;
13561 }
13562
13563 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13564
13565 tree
13566 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13567 tree in_decl)
13568 {
13569 /* This flag is used only during deduction, and we don't expect to
13570 substitute such ARGUMENT_PACKs. */
13571 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
13572
13573 /* Substitute into each of the arguments. */
13574 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13575 args, complain, in_decl);
13576 if (pack_args == error_mark_node)
13577 return error_mark_node;
13578
13579 if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
13580 return orig_arg;
13581
13582 /* If we're substituting into a generic ARGUMENT_PACK for a variadic
13583 template parameter, we might be able to avoid allocating a new
13584 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
13585 if the substituted result is identical to it. */
13586 if (tree parm = template_arg_to_parm (orig_arg))
13587 {
13588 int level, index;
13589 template_parm_level_and_index (parm, &level, &index);
13590 if (TMPL_ARGS_DEPTH (args) >= level)
13591 if (tree arg = TMPL_ARG (args, level, index))
13592 if (TREE_CODE (arg) == TREE_CODE (orig_arg)
13593 && ARGUMENT_PACK_ARGS (arg) == pack_args)
13594 {
13595 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
13596 return arg;
13597 }
13598 }
13599
13600 tree new_arg;
13601 if (TYPE_P (orig_arg))
13602 {
13603 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13604 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13605 }
13606 else
13607 {
13608 new_arg = make_node (TREE_CODE (orig_arg));
13609 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13610 }
13611 ARGUMENT_PACK_ARGS (new_arg) = pack_args;
13612 return new_arg;
13613 }
13614
13615 /* Substitute ARGS into the vector or list of template arguments T. */
13616
13617 tree
13618 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13619 {
13620 if (t == error_mark_node)
13621 return error_mark_node;
13622
13623 /* In "sizeof(X<I>)" we need to evaluate "I". */
13624 cp_evaluated ev;
13625
13626 const int len = TREE_VEC_LENGTH (t);
13627 tree *elts = XALLOCAVEC (tree, len);
13628 int expanded_len_adjust = 0;
13629
13630 /* True iff the substituted result is identical to T. */
13631 bool const_subst_p = true;
13632
13633 for (int i = 0; i < len; i++)
13634 {
13635 tree orig_arg = TREE_VEC_ELT (t, i);
13636 tree new_arg;
13637
13638 if (!orig_arg)
13639 new_arg = NULL_TREE;
13640 else if (TREE_CODE (orig_arg) == TREE_VEC)
13641 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13642 else if (PACK_EXPANSION_P (orig_arg))
13643 {
13644 /* Substitute into an expansion expression. */
13645 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13646
13647 if (TREE_CODE (new_arg) == TREE_VEC)
13648 /* Add to the expanded length adjustment the number of
13649 expanded arguments. We subtract one from this
13650 measurement, because the argument pack expression
13651 itself is already counted as 1 in
13652 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13653 the argument pack is empty. */
13654 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13655 }
13656 else if (ARGUMENT_PACK_P (orig_arg))
13657 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13658 else
13659 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13660
13661 if (new_arg == error_mark_node)
13662 return error_mark_node;
13663
13664 elts[i] = new_arg;
13665 if (new_arg != orig_arg)
13666 const_subst_p = false;
13667 }
13668
13669 if (const_subst_p)
13670 return t;
13671
13672 tree maybe_reuse = NULL_TREE;
13673
13674 /* If ARGS and T are both multi-level, the substituted result may be
13675 identical to ARGS. */
13676 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
13677 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
13678 && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
13679 maybe_reuse = args;
13680 /* If T appears to be a vector of generic template arguments, the
13681 substituted result may be identical to the corresponding level
13682 from ARGS. */
13683 else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
13684 {
13685 int level, index;
13686 template_parm_level_and_index (parm, &level, &index);
13687 if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
13688 maybe_reuse = TMPL_ARGS_LEVEL (args, level);
13689 }
13690
13691 /* If the substituted result is identical to MAYBE_REUSE, return
13692 it and avoid allocating a new TREE_VEC, as an optimization. */
13693 if (maybe_reuse != NULL_TREE
13694 && TREE_VEC_LENGTH (maybe_reuse) == len
13695 && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
13696 return maybe_reuse;
13697
13698 /* If T consists of only a pack expansion for which substitution yielded
13699 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
13700 of effectively making a copy. */
13701 if (len == 1
13702 && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
13703 && TREE_CODE (elts[0]) == TREE_VEC)
13704 return elts[0];
13705
13706 /* Make space for the expanded arguments coming from template
13707 argument packs. */
13708 tree r = make_tree_vec (len + expanded_len_adjust);
13709 /* T can contain TREE_VECs. That happens if T contains the
13710 arguments for a member template.
13711 In that case each TREE_VEC in T represents a level of template
13712 arguments, and T won't carry any non defaulted argument count.
13713 It will rather be the nested TREE_VECs that will carry one.
13714 In other words, T carries a non defaulted argument count only
13715 if it doesn't contain any nested TREE_VEC. */
13716 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
13717 {
13718 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13719 count += expanded_len_adjust;
13720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
13721 }
13722
13723 int out = 0;
13724 for (int i = 0; i < len; i++)
13725 {
13726 tree orig_arg = TREE_VEC_ELT (t, i);
13727 if (orig_arg
13728 && PACK_EXPANSION_P (orig_arg)
13729 && TREE_CODE (elts[i]) == TREE_VEC)
13730 {
13731 /* Now expand the template argument pack "in place". */
13732 for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13733 TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
13734 }
13735 else
13736 {
13737 TREE_VEC_ELT (r, out) = elts[i];
13738 out++;
13739 }
13740 }
13741 gcc_assert (out == TREE_VEC_LENGTH (r));
13742
13743 return r;
13744 }
13745
13746 /* Substitute ARGS into one level PARMS of template parameters. */
13747
13748 static tree
13749 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13750 {
13751 if (parms == error_mark_node)
13752 return error_mark_node;
13753
13754 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13755
13756 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13757 {
13758 tree tuple = TREE_VEC_ELT (parms, i);
13759
13760 if (tuple == error_mark_node)
13761 continue;
13762
13763 TREE_VEC_ELT (new_vec, i) =
13764 tsubst_template_parm (tuple, args, complain);
13765 }
13766
13767 return new_vec;
13768 }
13769
13770 /* Return the result of substituting ARGS into the template parameters
13771 given by PARMS. If there are m levels of ARGS and m + n levels of
13772 PARMS, then the result will contain n levels of PARMS. For
13773 example, if PARMS is `template <class T> template <class U>
13774 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13775 result will be `template <int*, double, class V>'. */
13776
13777 static tree
13778 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13779 {
13780 tree r = NULL_TREE;
13781 tree* new_parms;
13782
13783 /* When substituting into a template, we must set
13784 PROCESSING_TEMPLATE_DECL as the template parameters may be
13785 dependent if they are based on one-another, and the dependency
13786 predicates are short-circuit outside of templates. */
13787 ++processing_template_decl;
13788
13789 for (new_parms = &r;
13790 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13791 new_parms = &(TREE_CHAIN (*new_parms)),
13792 parms = TREE_CHAIN (parms))
13793 {
13794 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13795 args, complain);
13796 *new_parms =
13797 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13798 - TMPL_ARGS_DEPTH (args)),
13799 new_vec, NULL_TREE);
13800 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13801 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13802 }
13803
13804 --processing_template_decl;
13805
13806 return r;
13807 }
13808
13809 /* Return the result of substituting ARGS into one template parameter
13810 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13811 parameter and which TREE_PURPOSE is the default argument of the
13812 template parameter. */
13813
13814 static tree
13815 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13816 {
13817 tree default_value, parm_decl;
13818
13819 if (args == NULL_TREE
13820 || t == NULL_TREE
13821 || t == error_mark_node)
13822 return t;
13823
13824 gcc_assert (TREE_CODE (t) == TREE_LIST);
13825
13826 default_value = TREE_PURPOSE (t);
13827 parm_decl = TREE_VALUE (t);
13828
13829 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13830 if (TREE_CODE (parm_decl) == PARM_DECL
13831 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13832 parm_decl = error_mark_node;
13833 default_value = tsubst_template_arg (default_value, args,
13834 complain, NULL_TREE);
13835
13836 tree r = build_tree_list (default_value, parm_decl);
13837 TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
13838 return r;
13839 }
13840
13841 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
13842 parameter in PARMS for sake of declaration matching. */
13843
13844 static void
13845 tsubst_each_template_parm_constraints (tree parms, tree args,
13846 tsubst_flags_t complain)
13847 {
13848 ++processing_template_decl;
13849 for (; parms; parms = TREE_CHAIN (parms))
13850 {
13851 tree level = TREE_VALUE (parms);
13852 for (tree parm : tree_vec_range (level))
13853 TEMPLATE_PARM_CONSTRAINTS (parm)
13854 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
13855 complain, NULL_TREE);
13856 }
13857 --processing_template_decl;
13858 }
13859
13860 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13861 type T. If T is not an aggregate or enumeration type, it is
13862 handled as if by tsubst. IN_DECL is as for tsubst. If
13863 ENTERING_SCOPE is nonzero, T is the context for a template which
13864 we are presently tsubst'ing. Return the substituted value. */
13865
13866 static tree
13867 tsubst_aggr_type (tree t,
13868 tree args,
13869 tsubst_flags_t complain,
13870 tree in_decl,
13871 int entering_scope)
13872 {
13873 if (t == NULL_TREE)
13874 return NULL_TREE;
13875
13876 /* Handle typedefs via tsubst so that they get consistently reused. */
13877 if (typedef_variant_p (t))
13878 {
13879 t = tsubst (t, args, complain, in_decl);
13880 if (t == error_mark_node)
13881 return error_mark_node;
13882
13883 /* The effect of entering_scope is that for a dependent specialization
13884 A<T>, lookup_template_class prefers to return A's primary template
13885 type instead of the implicit instantiation. So when entering_scope,
13886 we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
13887 taking advantage of the fact that lookup_template_class links the two
13888 types by setting TYPE_CANONICAL of the latter to the former. */
13889 if (entering_scope
13890 && CLASS_TYPE_P (t)
13891 && dependent_type_p (t)
13892 && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t)))
13893 t = TYPE_CANONICAL (t);
13894
13895 return t;
13896 }
13897
13898 switch (TREE_CODE (t))
13899 {
13900 case RECORD_TYPE:
13901 case ENUMERAL_TYPE:
13902 case UNION_TYPE:
13903 return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope);
13904
13905 default:
13906 return tsubst (t, args, complain, in_decl);
13907 }
13908 }
13909
13910 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
13911 and ENUMERAL_TYPE cases of tsubst. */
13912
13913 static tree
13914 tsubst_aggr_type_1 (tree t,
13915 tree args,
13916 tsubst_flags_t complain,
13917 tree in_decl,
13918 int entering_scope)
13919 {
13920 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13921 {
13922 tree argvec;
13923 tree r;
13924
13925 /* Figure out what arguments are appropriate for the
13926 type we are trying to find. For example, given:
13927
13928 template <class T> struct S;
13929 template <class T, class U> void f(T, U) { S<U> su; }
13930
13931 and supposing that we are instantiating f<int, double>,
13932 then our ARGS will be {int, double}, but, when looking up
13933 S we only want {double}. */
13934 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13935 complain, in_decl);
13936 if (argvec == error_mark_node)
13937 r = error_mark_node;
13938 else
13939 {
13940 r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
13941 entering_scope, complain);
13942 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
13943 }
13944
13945 return r;
13946 }
13947 else
13948 /* This is not a template type, so there's nothing to do. */
13949 return t;
13950 }
13951
13952 /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
13953 indexed in reverse order of the parameters. */
13954
13955 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
13956
13957 /* Return a reference to the vec* of defarg insts for FN. */
13958
13959 static vec<tree,va_gc> *&
13960 defarg_insts_for (tree fn)
13961 {
13962 if (!defarg_inst)
13963 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
13964 tree_vec_map in = { { fn }, nullptr };
13965 tree_vec_map **slot
13966 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
13967 if (!*slot)
13968 {
13969 *slot = ggc_alloc<tree_vec_map> ();
13970 **slot = in;
13971 }
13972 return (*slot)->to;
13973 }
13974
13975 /* Substitute into the default argument ARG (a default argument for
13976 FN), which has the indicated TYPE. */
13977
13978 tree
13979 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13980 tsubst_flags_t complain)
13981 {
13982 int errs = errorcount + sorrycount;
13983
13984 /* This can happen in invalid code. */
13985 if (TREE_CODE (arg) == DEFERRED_PARSE)
13986 return arg;
13987
13988 /* Shortcut {}. */
13989 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13990 && CONSTRUCTOR_NELTS (arg) == 0)
13991 return arg;
13992
13993 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13994 parm = chain_index (parmnum, parm);
13995 tree parmtype = TREE_TYPE (parm);
13996 if (DECL_BY_REFERENCE (parm))
13997 parmtype = TREE_TYPE (parmtype);
13998 if (parmtype == error_mark_node)
13999 return error_mark_node;
14000
14001 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
14002
14003 /* Remember the location of the pointer to the vec rather than the location
14004 of the particular element, in case the vec grows in tsubst_expr. */
14005 vec<tree,va_gc> *&defs = defarg_insts_for (fn);
14006 /* Index in reverse order to avoid allocating space for initial parameters
14007 that don't have default arguments. */
14008 unsigned ridx = list_length (parm);
14009 if (vec_safe_length (defs) < ridx)
14010 vec_safe_grow_cleared (defs, ridx);
14011 else if (tree inst = (*defs)[ridx - 1])
14012 return inst;
14013
14014 /* This default argument came from a template. Instantiate the
14015 default argument here, not in tsubst. In the case of
14016 something like:
14017
14018 template <class T>
14019 struct S {
14020 static T t();
14021 void f(T = t());
14022 };
14023
14024 we must be careful to do name lookup in the scope of S<T>,
14025 rather than in the current class. */
14026 push_to_top_level ();
14027 push_access_scope (fn);
14028 push_deferring_access_checks (dk_no_deferred);
14029 /* So in_immediate_context knows this is a default argument. */
14030 begin_scope (sk_function_parms, fn);
14031 start_lambda_scope (parm);
14032
14033 /* The default argument expression may cause implicitly defined
14034 member functions to be synthesized, which will result in garbage
14035 collection. We must treat this situation as if we were within
14036 the body of function so as to avoid collecting live data on the
14037 stack. */
14038 ++function_depth;
14039 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
14040 --function_depth;
14041
14042 finish_lambda_scope ();
14043
14044 /* Make sure the default argument is reasonable. */
14045 arg = check_default_argument (type, arg, complain);
14046
14047 if (errorcount+sorrycount > errs
14048 && (complain & tf_warning_or_error))
14049 inform (input_location,
14050 " when instantiating default argument for call to %qD", fn);
14051
14052 leave_scope ();
14053 pop_deferring_access_checks ();
14054 pop_access_scope (fn);
14055 pop_from_top_level ();
14056
14057 if (arg != error_mark_node && !cp_unevaluated_operand)
14058 (*defs)[ridx - 1] = arg;
14059
14060 return arg;
14061 }
14062
14063 /* Substitute into all the default arguments for FN. */
14064
14065 static void
14066 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
14067 {
14068 tree arg;
14069 tree tmpl_args;
14070
14071 tmpl_args = DECL_TI_ARGS (fn);
14072
14073 /* If this function is not yet instantiated, we certainly don't need
14074 its default arguments. */
14075 if (uses_template_parms (tmpl_args))
14076 return;
14077 /* Don't do this again for clones. */
14078 if (DECL_CLONED_FUNCTION_P (fn))
14079 return;
14080
14081 int i = 0;
14082 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
14083 arg;
14084 arg = TREE_CHAIN (arg), ++i)
14085 if (TREE_PURPOSE (arg))
14086 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
14087 TREE_VALUE (arg),
14088 TREE_PURPOSE (arg),
14089 complain);
14090 }
14091
14092 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
14093 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
14094
14095 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
14096
14097 void
14098 store_explicit_specifier (tree v, tree t)
14099 {
14100 if (!explicit_specifier_map)
14101 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
14102 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
14103 explicit_specifier_map->put (v, t);
14104 }
14105
14106 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
14107
14108 tree
14109 lookup_explicit_specifier (tree v)
14110 {
14111 return *explicit_specifier_map->get (v);
14112 }
14113
14114 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
14115 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
14116 are ARG_TYPES, and exception specification is RAISES, and otherwise is
14117 identical to T. */
14118
14119 static tree
14120 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
14121 tree raises, tsubst_flags_t complain)
14122 {
14123 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
14124
14125 tree new_type;
14126 if (TREE_CODE (t) == FUNCTION_TYPE)
14127 {
14128 new_type = build_function_type (return_type, arg_types);
14129 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
14130 }
14131 else
14132 {
14133 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14134 /* Don't pick up extra function qualifiers from the basetype. */
14135 r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
14136 if (! MAYBE_CLASS_TYPE_P (r))
14137 {
14138 /* [temp.deduct]
14139
14140 Type deduction may fail for any of the following
14141 reasons:
14142
14143 -- Attempting to create "pointer to member of T" when T
14144 is not a class type. */
14145 if (complain & tf_error)
14146 error ("creating pointer to member function of non-class type %qT",
14147 r);
14148 return error_mark_node;
14149 }
14150
14151 new_type = build_method_type_directly (r, return_type,
14152 TREE_CHAIN (arg_types));
14153 }
14154 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
14155
14156 cp_ref_qualifier rqual = type_memfn_rqual (t);
14157 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14158 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
14159 }
14160
14161 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
14162 each of its formal parameters. If there is a disagreement then rebuild
14163 DECL's function type according to its formal parameter types, as part of a
14164 resolution for Core issues 1001/1322. */
14165
14166 static void
14167 maybe_rebuild_function_decl_type (tree decl)
14168 {
14169 bool function_type_needs_rebuilding = false;
14170 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
14171 {
14172 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
14173 while (parm_type_list && parm_type_list != void_list_node)
14174 {
14175 tree parm_type = TREE_VALUE (parm_type_list);
14176 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14177 if (!same_type_p (parm_type, formal_parm_type_unqual))
14178 {
14179 function_type_needs_rebuilding = true;
14180 break;
14181 }
14182
14183 parm_list = DECL_CHAIN (parm_list);
14184 parm_type_list = TREE_CHAIN (parm_type_list);
14185 }
14186 }
14187
14188 if (!function_type_needs_rebuilding)
14189 return;
14190
14191 const tree fntype = TREE_TYPE (decl);
14192 tree parm_list = DECL_ARGUMENTS (decl);
14193 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
14194 tree new_parm_type_list = NULL_TREE;
14195 tree *q = &new_parm_type_list;
14196 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
14197 {
14198 *q = copy_node (old_parm_type_list);
14199 parm_list = DECL_CHAIN (parm_list);
14200 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14201 q = &TREE_CHAIN (*q);
14202 }
14203 while (old_parm_type_list && old_parm_type_list != void_list_node)
14204 {
14205 *q = copy_node (old_parm_type_list);
14206 tree *new_parm_type = &TREE_VALUE (*q);
14207 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
14208 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
14209 *new_parm_type = formal_parm_type_unqual;
14210
14211 parm_list = DECL_CHAIN (parm_list);
14212 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
14213 q = &TREE_CHAIN (*q);
14214 }
14215 if (old_parm_type_list == void_list_node)
14216 *q = void_list_node;
14217
14218 TREE_TYPE (decl)
14219 = rebuild_function_or_method_type (fntype,
14220 TREE_TYPE (fntype), new_parm_type_list,
14221 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
14222 }
14223
14224 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
14225
14226 static tree
14227 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
14228 tree lambda_fntype)
14229 {
14230 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
14231 hashval_t hash = 0;
14232 tree in_decl = t;
14233
14234 /* Nobody should be tsubst'ing into non-template functions. */
14235 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
14236 || DECL_LOCAL_DECL_P (t));
14237
14238 if (DECL_LOCAL_DECL_P (t))
14239 {
14240 if (tree spec = retrieve_local_specialization (t))
14241 return spec;
14242 }
14243 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
14244 {
14245 /* If T is not dependent, just return it. */
14246 if (!uses_template_parms (DECL_TI_ARGS (t))
14247 && !LAMBDA_FUNCTION_P (t))
14248 return t;
14249
14250 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */
14251 if (non_templated_friend_p (t))
14252 goto friend_case;
14253
14254 /* Calculate the most general template of which R is a
14255 specialization. */
14256 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
14257
14258 /* We're substituting a lambda function under tsubst_lambda_expr but not
14259 directly from it; find the matching function we're already inside.
14260 But don't do this if T is a generic lambda with a single level of
14261 template parms, as in that case we're doing a normal instantiation. */
14262 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
14263 && (!generic_lambda_fn_p (t)
14264 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
14265 return enclosing_instantiation_of (t);
14266
14267 /* Calculate the complete set of arguments used to
14268 specialize R. */
14269 argvec = tsubst_template_args (DECL_TI_ARGS
14270 (DECL_TEMPLATE_RESULT
14271 (DECL_TI_TEMPLATE (t))),
14272 args, complain, in_decl);
14273 if (argvec == error_mark_node)
14274 return error_mark_node;
14275
14276 /* Check to see if we already have this specialization. */
14277 if (!lambda_fntype)
14278 {
14279 hash = spec_hasher::hash (gen_tmpl, argvec);
14280 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
14281 /* The spec for these args might be a partial instantiation of the
14282 template, but here what we want is the FUNCTION_DECL. */
14283 return STRIP_TEMPLATE (spec);
14284 }
14285 }
14286 else
14287 {
14288 /* This special case arises when we have something like this:
14289
14290 template <class T> struct S {
14291 friend void f<int>(int, double);
14292 };
14293
14294 Here, the DECL_TI_TEMPLATE for the friend declaration
14295 will be an IDENTIFIER_NODE. We are being called from
14296 tsubst_friend_function, and we want only to create a
14297 new decl (R) with appropriate types so that we can call
14298 determine_specialization. */
14299 friend_case:
14300 gen_tmpl = NULL_TREE;
14301 argvec = NULL_TREE;
14302 }
14303
14304 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
14305 : NULL_TREE);
14306 tree ctx = closure ? closure : DECL_CONTEXT (t);
14307 bool member = ctx && TYPE_P (ctx);
14308
14309 if (member && !closure)
14310 ctx = tsubst_aggr_type (ctx, args,
14311 complain, t, /*entering_scope=*/1);
14312
14313 tree type = (lambda_fntype ? lambda_fntype
14314 : tsubst (TREE_TYPE (t), args,
14315 complain | tf_fndecl_type, in_decl));
14316 if (type == error_mark_node)
14317 return error_mark_node;
14318
14319 /* If we hit excessive deduction depth, the type is bogus even if
14320 it isn't error_mark_node, so don't build a decl. */
14321 if (excessive_deduction_depth)
14322 return error_mark_node;
14323
14324 /* We do NOT check for matching decls pushed separately at this
14325 point, as they may not represent instantiations of this
14326 template, and in any case are considered separate under the
14327 discrete model. */
14328 tree r = copy_decl (t);
14329 DECL_USE_TEMPLATE (r) = 0;
14330 TREE_TYPE (r) = type;
14331 /* Clear out the mangled name and RTL for the instantiation. */
14332 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14333 SET_DECL_RTL (r, NULL);
14334 /* Leave DECL_INITIAL set on deleted instantiations. */
14335 if (!DECL_DELETED_FN (r))
14336 DECL_INITIAL (r) = NULL_TREE;
14337 DECL_CONTEXT (r) = ctx;
14338 set_instantiating_module (r);
14339
14340 /* Handle explicit(dependent-expr). */
14341 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
14342 {
14343 tree spec = lookup_explicit_specifier (t);
14344 spec = tsubst_copy_and_build (spec, args, complain, in_decl);
14345 spec = build_explicit_specifier (spec, complain);
14346 if (spec == error_mark_node)
14347 return error_mark_node;
14348 if (instantiation_dependent_expression_p (spec))
14349 store_explicit_specifier (r, spec);
14350 else
14351 {
14352 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
14353 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
14354 }
14355 }
14356
14357 /* OpenMP UDRs have the only argument a reference to the declared
14358 type. We want to diagnose if the declared type is a reference,
14359 which is invalid, but as references to references are usually
14360 quietly merged, diagnose it here. */
14361 if (DECL_OMP_DECLARE_REDUCTION_P (t))
14362 {
14363 tree argtype
14364 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
14365 argtype = tsubst (argtype, args, complain, in_decl);
14366 if (TYPE_REF_P (argtype))
14367 error_at (DECL_SOURCE_LOCATION (t),
14368 "reference type %qT in "
14369 "%<#pragma omp declare reduction%>", argtype);
14370 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14371 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14372 argtype);
14373 }
14374
14375 if (member && DECL_CONV_FN_P (r))
14376 /* Type-conversion operator. Reconstruct the name, in
14377 case it's the name of one of the template's parameters. */
14378 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14379
14380 tree parms = DECL_ARGUMENTS (t);
14381 if (closure)
14382 parms = DECL_CHAIN (parms);
14383 parms = tsubst (parms, args, complain, t);
14384 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14385 DECL_CONTEXT (parm) = r;
14386 if (closure)
14387 {
14388 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14389 DECL_NAME (tparm) = closure_identifier;
14390 DECL_CHAIN (tparm) = parms;
14391 parms = tparm;
14392 }
14393 DECL_ARGUMENTS (r) = parms;
14394 DECL_RESULT (r) = NULL_TREE;
14395
14396 maybe_rebuild_function_decl_type (r);
14397
14398 TREE_STATIC (r) = 0;
14399 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14400 DECL_EXTERNAL (r) = 1;
14401 /* If this is an instantiation of a function with internal
14402 linkage, we already know what object file linkage will be
14403 assigned to the instantiation. */
14404 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14405 DECL_DEFER_OUTPUT (r) = 0;
14406 DECL_CHAIN (r) = NULL_TREE;
14407 DECL_PENDING_INLINE_INFO (r) = 0;
14408 DECL_PENDING_INLINE_P (r) = 0;
14409 DECL_SAVED_TREE (r) = NULL_TREE;
14410 DECL_STRUCT_FUNCTION (r) = NULL;
14411 TREE_USED (r) = 0;
14412 /* We'll re-clone as appropriate in instantiate_template. */
14413 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14414
14415 /* If we aren't complaining now, return on error before we register
14416 the specialization so that we'll complain eventually. */
14417 if ((complain & tf_error) == 0
14418 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14419 && !grok_op_properties (r, /*complain=*/false))
14420 return error_mark_node;
14421
14422 /* Associate the constraints directly with the instantiation. We
14423 don't substitute through the constraints; that's only done when
14424 they are checked. */
14425 if (tree ci = get_constraints (t))
14426 set_constraints (r, ci);
14427
14428 if (DECL_FRIEND_CONTEXT (t))
14429 SET_DECL_FRIEND_CONTEXT (r,
14430 tsubst (DECL_FRIEND_CONTEXT (t),
14431 args, complain, in_decl));
14432
14433 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14434 args, complain, in_decl))
14435 return error_mark_node;
14436
14437 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14438 this in the special friend case mentioned above where
14439 GEN_TMPL is NULL. */
14440 if (gen_tmpl && !closure)
14441 {
14442 DECL_TEMPLATE_INFO (r)
14443 = build_template_info (gen_tmpl, argvec);
14444 SET_DECL_IMPLICIT_INSTANTIATION (r);
14445
14446 tree new_r
14447 = register_specialization (r, gen_tmpl, argvec, false, hash);
14448 if (new_r != r)
14449 /* We instantiated this while substituting into
14450 the type earlier (template/friend54.C). */
14451 return new_r;
14452
14453 /* We're not supposed to instantiate default arguments
14454 until they are called, for a template. But, for a
14455 declaration like:
14456
14457 template <class T> void f ()
14458 { extern void g(int i = T()); }
14459
14460 we should do the substitution when the template is
14461 instantiated. We handle the member function case in
14462 instantiate_class_template since the default arguments
14463 might refer to other members of the class. */
14464 if (!member
14465 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14466 && !uses_template_parms (argvec))
14467 tsubst_default_arguments (r, complain);
14468 }
14469 else if (DECL_LOCAL_DECL_P (r))
14470 {
14471 if (!cp_unevaluated_operand)
14472 register_local_specialization (r, t);
14473 }
14474 else
14475 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14476
14477 /* Copy the list of befriending classes. */
14478 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14479 *friends;
14480 friends = &TREE_CHAIN (*friends))
14481 {
14482 *friends = copy_node (*friends);
14483 TREE_VALUE (*friends)
14484 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14485 }
14486
14487 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14488 {
14489 maybe_retrofit_in_chrg (r);
14490 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14491 return error_mark_node;
14492 /* If this is an instantiation of a member template, clone it.
14493 If it isn't, that'll be handled by
14494 clone_constructors_and_destructors. */
14495 if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
14496 clone_cdtor (r, /*update_methods=*/false);
14497 }
14498 else if ((complain & tf_error) != 0
14499 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14500 && !grok_op_properties (r, /*complain=*/true))
14501 return error_mark_node;
14502
14503 /* Possibly limit visibility based on template args. */
14504 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14505 if (DECL_VISIBILITY_SPECIFIED (t))
14506 {
14507 DECL_VISIBILITY_SPECIFIED (r) = 0;
14508 DECL_ATTRIBUTES (r)
14509 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14510 }
14511 determine_visibility (r);
14512 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14513 && !processing_template_decl)
14514 defaulted_late_check (r);
14515
14516 if (flag_openmp)
14517 if (tree attr = lookup_attribute ("omp declare variant base",
14518 DECL_ATTRIBUTES (r)))
14519 omp_declare_variant_finalize (r, attr);
14520
14521 return r;
14522 }
14523
14524 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14525
14526 static tree
14527 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14528 tree lambda_fntype)
14529 {
14530 /* We can get here when processing a member function template,
14531 member class template, or template template parameter. */
14532 tree decl = DECL_TEMPLATE_RESULT (t);
14533 tree in_decl = t;
14534 tree spec;
14535 tree tmpl_args;
14536 tree full_args;
14537 tree r;
14538 hashval_t hash = 0;
14539
14540 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14541 {
14542 /* Template template parameter is treated here. */
14543 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14544 if (new_type == error_mark_node)
14545 r = error_mark_node;
14546 /* If we get a real template back, return it. This can happen in
14547 the context of most_specialized_partial_spec. */
14548 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14549 r = new_type;
14550 else
14551 /* The new TEMPLATE_DECL was built in
14552 reduce_template_parm_level. */
14553 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14554 return r;
14555 }
14556
14557 if (!lambda_fntype)
14558 {
14559 /* We might already have an instance of this template.
14560 The ARGS are for the surrounding class type, so the
14561 full args contain the tsubst'd args for the context,
14562 plus the innermost args from the template decl. */
14563 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14564 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14565 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14566 /* Because this is a template, the arguments will still be
14567 dependent, even after substitution. If
14568 PROCESSING_TEMPLATE_DECL is not set, the dependency
14569 predicates will short-circuit. */
14570 ++processing_template_decl;
14571 full_args = tsubst_template_args (tmpl_args, args,
14572 complain, in_decl);
14573 --processing_template_decl;
14574 if (full_args == error_mark_node)
14575 return error_mark_node;
14576
14577 /* If this is a default template template argument,
14578 tsubst might not have changed anything. */
14579 if (full_args == tmpl_args)
14580 return t;
14581
14582 hash = spec_hasher::hash (t, full_args);
14583 spec = retrieve_specialization (t, full_args, hash);
14584 if (spec != NULL_TREE)
14585 {
14586 if (TYPE_P (spec))
14587 /* Type partial instantiations are stored as the type by
14588 lookup_template_class_1, not here as the template. */
14589 spec = CLASSTYPE_TI_TEMPLATE (spec);
14590 return spec;
14591 }
14592 }
14593
14594 /* Make a new template decl. It will be similar to the
14595 original, but will record the current template arguments.
14596 We also create a new function declaration, which is just
14597 like the old one, but points to this new template, rather
14598 than the old one. */
14599 r = copy_decl (t);
14600 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14601 DECL_CHAIN (r) = NULL_TREE;
14602
14603 // Build new template info linking to the original template decl.
14604 if (!lambda_fntype)
14605 {
14606 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14607 SET_DECL_IMPLICIT_INSTANTIATION (r);
14608 }
14609 else
14610 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14611
14612 /* The template parameters for this new template are all the
14613 template parameters for the old template, except the
14614 outermost level of parameters. */
14615 auto tparm_guard = make_temp_override (current_template_parms);
14616 DECL_TEMPLATE_PARMS (r)
14617 = current_template_parms
14618 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14619 complain);
14620
14621 bool class_p = false;
14622 tree inner = decl;
14623 ++processing_template_decl;
14624 if (TREE_CODE (inner) == FUNCTION_DECL)
14625 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14626 else
14627 {
14628 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14629 {
14630 class_p = true;
14631 inner = TREE_TYPE (inner);
14632 }
14633 if (class_p)
14634 inner = tsubst_aggr_type (inner, args, complain,
14635 in_decl, /*entering*/1);
14636 else
14637 inner = tsubst (inner, args, complain, in_decl);
14638 }
14639 --processing_template_decl;
14640 if (inner == error_mark_node)
14641 return error_mark_node;
14642
14643 if (class_p)
14644 {
14645 /* For a partial specialization, we need to keep pointing to
14646 the primary template. */
14647 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14648 CLASSTYPE_TI_TEMPLATE (inner) = r;
14649
14650 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14651 inner = TYPE_MAIN_DECL (inner);
14652 }
14653 else if (lambda_fntype)
14654 {
14655 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14656 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14657 }
14658 else
14659 {
14660 DECL_TI_TEMPLATE (inner) = r;
14661 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14662 }
14663
14664 DECL_TEMPLATE_RESULT (r) = inner;
14665 TREE_TYPE (r) = TREE_TYPE (inner);
14666 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14667
14668 if (modules_p ())
14669 {
14670 /* Propagate module information from the decl. */
14671 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14672 if (DECL_LANG_SPECIFIC (inner))
14673 /* If this is a constrained template, the above tsubst of
14674 inner can find the unconstrained template, which may have
14675 come from an import. This is ok, because we don't
14676 register this instantiation (see below). */
14677 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14678 || (TEMPLATE_PARMS_CONSTRAINTS
14679 (DECL_TEMPLATE_PARMS (t))));
14680 }
14681
14682 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14683 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14684
14685 if (PRIMARY_TEMPLATE_P (t))
14686 DECL_PRIMARY_TEMPLATE (r) = r;
14687
14688 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14689 /* Record this non-type partial instantiation. */
14690 register_specialization (r, t,
14691 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14692 false, hash);
14693
14694 return r;
14695 }
14696
14697 /* True if FN is the op() for a lambda in an uninstantiated template. */
14698
14699 bool
14700 lambda_fn_in_template_p (tree fn)
14701 {
14702 if (!fn || !LAMBDA_FUNCTION_P (fn))
14703 return false;
14704 tree closure = DECL_CONTEXT (fn);
14705 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14706 }
14707
14708 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14709 which the above is true. */
14710
14711 bool
14712 regenerated_lambda_fn_p (tree fn)
14713 {
14714 if (!fn || !LAMBDA_FUNCTION_P (fn))
14715 return false;
14716 tree closure = DECL_CONTEXT (fn);
14717 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14718 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
14719 }
14720
14721 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14722 If T is not a regenerated LAMBDA_EXPR, return T. */
14723
14724 tree
14725 most_general_lambda (tree t)
14726 {
14727 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14728 t = TI_TEMPLATE (ti);
14729 return t;
14730 }
14731
14732 /* Return the set of template arguments used to regenerate the lambda T
14733 from its most general lambda. */
14734
14735 tree
14736 lambda_regenerating_args (tree t)
14737 {
14738 if (LAMBDA_FUNCTION_P (t))
14739 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
14740 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
14741 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14742 return TI_ARGS (ti);
14743 else
14744 return NULL_TREE;
14745 }
14746
14747 /* We're instantiating a variable from template function TCTX. Return the
14748 corresponding current enclosing scope. We can match them up using
14749 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14750 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14751 the template definition in regenerate_decl_from_template. */
14752
14753 static tree
14754 enclosing_instantiation_of (tree tctx)
14755 {
14756 tree fn = current_function_decl;
14757
14758 /* We shouldn't ever need to do this for other artificial functions. */
14759 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
14760
14761 for (; fn; fn = decl_function_context (fn))
14762 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
14763 return fn;
14764 gcc_unreachable ();
14765 }
14766
14767 /* Substitute the ARGS into the T, which is a _DECL. Return the
14768 result of the substitution. Issue error and warning messages under
14769 control of COMPLAIN. */
14770
14771 static tree
14772 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14773 {
14774 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14775 location_t saved_loc;
14776 tree r = NULL_TREE;
14777 tree in_decl = t;
14778 hashval_t hash = 0;
14779
14780 /* Set the filename and linenumber to improve error-reporting. */
14781 saved_loc = input_location;
14782 input_location = DECL_SOURCE_LOCATION (t);
14783
14784 switch (TREE_CODE (t))
14785 {
14786 case TEMPLATE_DECL:
14787 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14788 break;
14789
14790 case FUNCTION_DECL:
14791 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14792 break;
14793
14794 case PARM_DECL:
14795 {
14796 tree type = NULL_TREE;
14797 int i, len = 1;
14798 tree expanded_types = NULL_TREE;
14799 tree prev_r = NULL_TREE;
14800 tree first_r = NULL_TREE;
14801
14802 if (DECL_PACK_P (t))
14803 {
14804 /* If there is a local specialization that isn't a
14805 parameter pack, it means that we're doing a "simple"
14806 substitution from inside tsubst_pack_expansion. Just
14807 return the local specialization (which will be a single
14808 parm). */
14809 tree spec = retrieve_local_specialization (t);
14810 if (spec
14811 && TREE_CODE (spec) == PARM_DECL
14812 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14813 RETURN (spec);
14814
14815 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14816 the parameters in this function parameter pack. */
14817 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14818 complain, in_decl);
14819 if (TREE_CODE (expanded_types) == TREE_VEC)
14820 {
14821 len = TREE_VEC_LENGTH (expanded_types);
14822
14823 /* Zero-length parameter packs are boring. Just substitute
14824 into the chain. */
14825 if (len == 0 && !cp_unevaluated_operand)
14826 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14827 TREE_CHAIN (t)));
14828 }
14829 else
14830 {
14831 /* All we did was update the type. Make a note of that. */
14832 type = expanded_types;
14833 expanded_types = NULL_TREE;
14834 }
14835 }
14836
14837 /* Loop through all of the parameters we'll build. When T is
14838 a function parameter pack, LEN is the number of expanded
14839 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14840 r = NULL_TREE;
14841 for (i = 0; i < len; ++i)
14842 {
14843 prev_r = r;
14844 r = copy_node (t);
14845 if (DECL_TEMPLATE_PARM_P (t))
14846 SET_DECL_TEMPLATE_PARM_P (r);
14847
14848 if (expanded_types)
14849 /* We're on the Ith parameter of the function parameter
14850 pack. */
14851 {
14852 /* Get the Ith type. */
14853 type = TREE_VEC_ELT (expanded_types, i);
14854
14855 /* Rename the parameter to include the index. */
14856 DECL_NAME (r)
14857 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14858 }
14859 else if (!type)
14860 /* We're dealing with a normal parameter. */
14861 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14862
14863 type = type_decays_to (type);
14864 TREE_TYPE (r) = type;
14865 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14866
14867 if (DECL_INITIAL (r))
14868 {
14869 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14870 DECL_INITIAL (r) = TREE_TYPE (r);
14871 else
14872 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14873 complain, in_decl);
14874 }
14875
14876 DECL_CONTEXT (r) = NULL_TREE;
14877
14878 if (!DECL_TEMPLATE_PARM_P (r))
14879 DECL_ARG_TYPE (r) = type_passed_as (type);
14880
14881 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14882 args, complain, in_decl))
14883 return error_mark_node;
14884
14885 /* Keep track of the first new parameter we
14886 generate. That's what will be returned to the
14887 caller. */
14888 if (!first_r)
14889 first_r = r;
14890
14891 /* Build a proper chain of parameters when substituting
14892 into a function parameter pack. */
14893 if (prev_r)
14894 DECL_CHAIN (prev_r) = r;
14895 }
14896
14897 /* If cp_unevaluated_operand is set, we're just looking for a
14898 single dummy parameter, so don't keep going. */
14899 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14900 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14901 complain, DECL_CHAIN (t));
14902
14903 /* FIRST_R contains the start of the chain we've built. */
14904 r = first_r;
14905 }
14906 break;
14907
14908 case FIELD_DECL:
14909 {
14910 tree type = NULL_TREE;
14911 tree vec = NULL_TREE;
14912 tree expanded_types = NULL_TREE;
14913 int len = 1;
14914
14915 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14916 {
14917 /* This field is a lambda capture pack. Return a TREE_VEC of
14918 the expanded fields to instantiate_class_template_1. */
14919 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14920 complain, in_decl);
14921 if (TREE_CODE (expanded_types) == TREE_VEC)
14922 {
14923 len = TREE_VEC_LENGTH (expanded_types);
14924 vec = make_tree_vec (len);
14925 }
14926 else
14927 {
14928 /* All we did was update the type. Make a note of that. */
14929 type = expanded_types;
14930 expanded_types = NULL_TREE;
14931 }
14932 }
14933
14934 for (int i = 0; i < len; ++i)
14935 {
14936 r = copy_decl (t);
14937 if (expanded_types)
14938 {
14939 type = TREE_VEC_ELT (expanded_types, i);
14940 DECL_NAME (r)
14941 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14942 }
14943 else if (!type)
14944 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14945
14946 if (type == error_mark_node)
14947 RETURN (error_mark_node);
14948 TREE_TYPE (r) = type;
14949 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14950
14951 if (DECL_C_BIT_FIELD (r))
14952 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14953 number of bits. */
14954 DECL_BIT_FIELD_REPRESENTATIVE (r)
14955 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14956 complain, in_decl);
14957 if (DECL_INITIAL (t))
14958 {
14959 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14960 NSDMI in perform_member_init. Still set DECL_INITIAL
14961 so that we know there is one. */
14962 DECL_INITIAL (r) = void_node;
14963 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14964 retrofit_lang_decl (r);
14965 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14966 }
14967 /* We don't have to set DECL_CONTEXT here; it is set by
14968 finish_member_declaration. */
14969 DECL_CHAIN (r) = NULL_TREE;
14970
14971 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14972 args, complain, in_decl))
14973 return error_mark_node;
14974
14975 if (vec)
14976 TREE_VEC_ELT (vec, i) = r;
14977 }
14978
14979 if (vec)
14980 r = vec;
14981 }
14982 break;
14983
14984 case USING_DECL:
14985 /* We reach here only for member using decls. We also need to check
14986 uses_template_parms because DECL_DEPENDENT_P is not set for a
14987 using-declaration that designates a member of the current
14988 instantiation (c++/53549). */
14989 if (DECL_DEPENDENT_P (t)
14990 || uses_template_parms (USING_DECL_SCOPE (t)))
14991 {
14992 /* True iff this using-decl was written as a pack expansion
14993 (and a pack appeared in its scope or name). If a pack
14994 appeared in both, we expand the packs separately and
14995 manually merge them. */
14996 bool variadic_p = false;
14997
14998 tree scope = USING_DECL_SCOPE (t);
14999 if (PACK_EXPANSION_P (scope))
15000 {
15001 scope = tsubst_pack_expansion (scope, args, complain, in_decl);
15002 variadic_p = true;
15003 }
15004 else
15005 scope = tsubst_copy (scope, args, complain, in_decl);
15006
15007 tree name = DECL_NAME (t);
15008 if (IDENTIFIER_CONV_OP_P (name)
15009 && PACK_EXPANSION_P (TREE_TYPE (name)))
15010 {
15011 name = tsubst_pack_expansion (TREE_TYPE (name), args,
15012 complain, in_decl);
15013 if (name == error_mark_node)
15014 {
15015 r = error_mark_node;
15016 break;
15017 }
15018 for (tree& elt : tree_vec_range (name))
15019 elt = make_conv_op_name (elt);
15020 variadic_p = true;
15021 }
15022 else
15023 name = tsubst_copy (name, args, complain, in_decl);
15024
15025 int len;
15026 if (!variadic_p)
15027 len = 1;
15028 else if (TREE_CODE (scope) == TREE_VEC
15029 && TREE_CODE (name) == TREE_VEC)
15030 {
15031 if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
15032 {
15033 error ("mismatched argument pack lengths (%d vs %d)",
15034 TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
15035 r = error_mark_node;
15036 break;
15037 }
15038 len = TREE_VEC_LENGTH (scope);
15039 }
15040 else if (TREE_CODE (scope) == TREE_VEC)
15041 len = TREE_VEC_LENGTH (scope);
15042 else /* TREE_CODE (name) == TREE_VEC */
15043 len = TREE_VEC_LENGTH (name);
15044
15045 r = make_tree_vec (len);
15046 for (int i = 0; i < len; ++i)
15047 {
15048 tree escope = (TREE_CODE (scope) == TREE_VEC
15049 ? TREE_VEC_ELT (scope, i)
15050 : scope);
15051 tree ename = (TREE_CODE (name) == TREE_VEC
15052 ? TREE_VEC_ELT (name, i)
15053 : name);
15054 tree elt = do_class_using_decl (escope, ename);
15055 if (!elt)
15056 {
15057 r = error_mark_node;
15058 break;
15059 }
15060 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
15061 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
15062 TREE_VEC_ELT (r, i) = elt;
15063 }
15064
15065 if (!variadic_p && r != error_mark_node)
15066 r = TREE_VEC_ELT (r, 0);
15067 }
15068 else
15069 {
15070 r = copy_node (t);
15071 DECL_CHAIN (r) = NULL_TREE;
15072 }
15073 break;
15074
15075 case TYPE_DECL:
15076 case VAR_DECL:
15077 {
15078 tree argvec = NULL_TREE;
15079 tree gen_tmpl = NULL_TREE;
15080 tree tmpl = NULL_TREE;
15081 tree type = NULL_TREE;
15082
15083 if (TREE_TYPE (t) == error_mark_node)
15084 RETURN (error_mark_node);
15085
15086 if (TREE_CODE (t) == TYPE_DECL
15087 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
15088 {
15089 /* If this is the canonical decl, we don't have to
15090 mess with instantiations, and often we can't (for
15091 typename, template type parms and such). Note that
15092 TYPE_NAME is not correct for the above test if
15093 we've copied the type for a typedef. */
15094 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15095 if (type == error_mark_node)
15096 RETURN (error_mark_node);
15097 r = TYPE_NAME (type);
15098 break;
15099 }
15100
15101 /* Check to see if we already have the specialization we
15102 need. */
15103 tree spec = NULL_TREE;
15104 bool local_p = false;
15105 tree ctx = DECL_CONTEXT (t);
15106 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
15107 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
15108 {
15109 local_p = false;
15110 if (DECL_CLASS_SCOPE_P (t))
15111 {
15112 ctx = tsubst_aggr_type (ctx, args,
15113 complain,
15114 in_decl, /*entering_scope=*/1);
15115 if (DECL_SELF_REFERENCE_P (t))
15116 /* The context and type of an injected-class-name are
15117 the same, so we don't need to substitute both. */
15118 type = ctx;
15119 /* If CTX is unchanged, then T is in fact the
15120 specialization we want. That situation occurs when
15121 referencing a static data member within in its own
15122 class. We can use pointer equality, rather than
15123 same_type_p, because DECL_CONTEXT is always
15124 canonical... */
15125 if (ctx == DECL_CONTEXT (t)
15126 /* ... unless T is a member template; in which
15127 case our caller can be willing to create a
15128 specialization of that template represented
15129 by T. */
15130 && !(DECL_TI_TEMPLATE (t)
15131 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
15132 spec = t;
15133 }
15134
15135 if (!spec)
15136 {
15137 tmpl = DECL_TI_TEMPLATE (t);
15138 gen_tmpl = most_general_template (tmpl);
15139 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
15140 if (argvec != error_mark_node
15141 && PRIMARY_TEMPLATE_P (gen_tmpl)
15142 && TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (argvec))
15143 /* We're fully specializing a template declaration, so
15144 we need to coerce the innermost arguments corresponding to
15145 the template. */
15146 argvec = (coerce_template_parms
15147 (DECL_TEMPLATE_PARMS (gen_tmpl),
15148 argvec, t, complain));
15149 if (argvec == error_mark_node)
15150 RETURN (error_mark_node);
15151 hash = spec_hasher::hash (gen_tmpl, argvec);
15152 spec = retrieve_specialization (gen_tmpl, argvec, hash);
15153 }
15154 }
15155 else
15156 {
15157 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
15158 /* Subsequent calls to pushdecl will fill this in. */
15159 ctx = NULL_TREE;
15160 /* A local variable. */
15161 local_p = true;
15162 /* Unless this is a reference to a static variable from an
15163 enclosing function, in which case we need to fill it in now. */
15164 if (TREE_STATIC (t))
15165 {
15166 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
15167 if (fn != current_function_decl)
15168 ctx = fn;
15169 }
15170 spec = retrieve_local_specialization (t);
15171 }
15172 /* If we already have the specialization we need, there is
15173 nothing more to do. */
15174 if (spec)
15175 {
15176 r = spec;
15177 break;
15178 }
15179
15180 /* Create a new node for the specialization we need. */
15181 if (type == NULL_TREE)
15182 {
15183 if (is_typedef_decl (t))
15184 type = DECL_ORIGINAL_TYPE (t);
15185 else
15186 type = TREE_TYPE (t);
15187 if (VAR_P (t)
15188 && VAR_HAD_UNKNOWN_BOUND (t)
15189 && type != error_mark_node)
15190 type = strip_array_domain (type);
15191 tsubst_flags_t tcomplain = complain;
15192 if (VAR_P (t))
15193 tcomplain |= tf_tst_ok;
15194 type = tsubst (type, args, tcomplain, in_decl);
15195 /* Substituting the type might have recursively instantiated this
15196 same alias (c++/86171). */
15197 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
15198 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
15199 {
15200 r = spec;
15201 break;
15202 }
15203 }
15204 r = copy_decl (t);
15205 if (VAR_P (r))
15206 {
15207 DECL_INITIALIZED_P (r) = 0;
15208 DECL_TEMPLATE_INSTANTIATED (r) = 0;
15209 if (type == error_mark_node)
15210 RETURN (error_mark_node);
15211 if (TREE_CODE (type) == FUNCTION_TYPE)
15212 {
15213 /* It may seem that this case cannot occur, since:
15214
15215 typedef void f();
15216 void g() { f x; }
15217
15218 declares a function, not a variable. However:
15219
15220 typedef void f();
15221 template <typename T> void g() { T t; }
15222 template void g<f>();
15223
15224 is an attempt to declare a variable with function
15225 type. */
15226 error ("variable %qD has function type",
15227 /* R is not yet sufficiently initialized, so we
15228 just use its name. */
15229 DECL_NAME (r));
15230 RETURN (error_mark_node);
15231 }
15232 type = complete_type (type);
15233 /* Wait until cp_finish_decl to set this again, to handle
15234 circular dependency (template/instantiate6.C). */
15235 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
15236 type = check_var_type (DECL_NAME (r), type,
15237 DECL_SOURCE_LOCATION (r));
15238 if (DECL_HAS_VALUE_EXPR_P (t))
15239 {
15240 tree ve = DECL_VALUE_EXPR (t);
15241 /* If the DECL_VALUE_EXPR is converted to the declared type,
15242 preserve the identity so that gimplify_type_sizes works. */
15243 bool nop = (TREE_CODE (ve) == NOP_EXPR);
15244 if (nop)
15245 ve = TREE_OPERAND (ve, 0);
15246 ve = tsubst_expr (ve, args, complain, in_decl);
15247 if (REFERENCE_REF_P (ve))
15248 {
15249 gcc_assert (TYPE_REF_P (type));
15250 ve = TREE_OPERAND (ve, 0);
15251 }
15252 if (nop)
15253 ve = build_nop (type, ve);
15254 else if (DECL_LANG_SPECIFIC (t)
15255 && DECL_OMP_PRIVATIZED_MEMBER (t)
15256 && TREE_CODE (ve) == COMPONENT_REF
15257 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
15258 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
15259 type = TREE_TYPE (ve);
15260 else
15261 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
15262 == TYPE_MAIN_VARIANT (type));
15263 SET_DECL_VALUE_EXPR (r, ve);
15264 }
15265 if (CP_DECL_THREAD_LOCAL_P (r)
15266 && !processing_template_decl)
15267 set_decl_tls_model (r, decl_default_tls_model (r));
15268 }
15269 else if (DECL_SELF_REFERENCE_P (t))
15270 SET_DECL_SELF_REFERENCE_P (r);
15271 TREE_TYPE (r) = type;
15272 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
15273 DECL_CONTEXT (r) = ctx;
15274 /* Clear out the mangled name and RTL for the instantiation. */
15275 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
15276 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
15277 SET_DECL_RTL (r, NULL);
15278 set_instantiating_module (r);
15279
15280 /* The initializer must not be expanded until it is required;
15281 see [temp.inst]. */
15282 DECL_INITIAL (r) = NULL_TREE;
15283 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
15284 if (VAR_P (r))
15285 {
15286 if (DECL_LANG_SPECIFIC (r))
15287 SET_DECL_DEPENDENT_INIT_P (r, false);
15288
15289 SET_DECL_MODE (r, VOIDmode);
15290
15291 /* Possibly limit visibility based on template args. */
15292 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
15293 if (DECL_VISIBILITY_SPECIFIED (t))
15294 {
15295 DECL_VISIBILITY_SPECIFIED (r) = 0;
15296 DECL_ATTRIBUTES (r)
15297 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
15298 }
15299 determine_visibility (r);
15300 }
15301
15302 if (!local_p)
15303 {
15304 /* A static data member declaration is always marked
15305 external when it is declared in-class, even if an
15306 initializer is present. We mimic the non-template
15307 processing here. */
15308 DECL_EXTERNAL (r) = 1;
15309 if (DECL_NAMESPACE_SCOPE_P (t))
15310 DECL_NOT_REALLY_EXTERN (r) = 1;
15311
15312 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
15313 SET_DECL_IMPLICIT_INSTANTIATION (r);
15314 if (!error_operand_p (r) || (complain & tf_error))
15315 register_specialization (r, gen_tmpl, argvec, false, hash);
15316 }
15317 else
15318 {
15319 if (DECL_LANG_SPECIFIC (r))
15320 DECL_TEMPLATE_INFO (r) = NULL_TREE;
15321 if (!cp_unevaluated_operand)
15322 register_local_specialization (r, t);
15323 }
15324
15325 DECL_CHAIN (r) = NULL_TREE;
15326
15327 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
15328 /*flags=*/0,
15329 args, complain, in_decl))
15330 return error_mark_node;
15331
15332 /* Preserve a typedef that names a type. */
15333 if (is_typedef_decl (r) && type != error_mark_node)
15334 {
15335 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
15336 set_underlying_type (r);
15337
15338 /* common_handle_aligned_attribute doesn't apply the alignment
15339 to DECL_ORIGINAL_TYPE. */
15340 if (TYPE_USER_ALIGN (TREE_TYPE (t)))
15341 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
15342 TYPE_ALIGN (TREE_TYPE (t)));
15343 }
15344
15345 layout_decl (r, 0);
15346 }
15347 break;
15348
15349 default:
15350 gcc_unreachable ();
15351 }
15352 #undef RETURN
15353
15354 out:
15355 /* Restore the file and line information. */
15356 input_location = saved_loc;
15357
15358 return r;
15359 }
15360
15361 /* Substitute into the complete parameter type list PARMS. */
15362
15363 tree
15364 tsubst_function_parms (tree parms,
15365 tree args,
15366 tsubst_flags_t complain,
15367 tree in_decl)
15368 {
15369 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
15370 }
15371
15372 /* Substitute into the ARG_TYPES of a function type.
15373 If END is a TREE_CHAIN, leave it and any following types
15374 un-substituted. */
15375
15376 static tree
15377 tsubst_arg_types (tree arg_types,
15378 tree args,
15379 tree end,
15380 tsubst_flags_t complain,
15381 tree in_decl)
15382 {
15383 tree type = NULL_TREE;
15384 int len = 1;
15385 tree expanded_args = NULL_TREE;
15386
15387 if (!arg_types || arg_types == void_list_node || arg_types == end)
15388 return arg_types;
15389
15390 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
15391 {
15392 /* For a pack expansion, perform substitution on the
15393 entire expression. Later on, we'll handle the arguments
15394 one-by-one. */
15395 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
15396 args, complain, in_decl);
15397
15398 if (TREE_CODE (expanded_args) == TREE_VEC)
15399 /* So that we'll spin through the parameters, one by one. */
15400 len = TREE_VEC_LENGTH (expanded_args);
15401 else
15402 {
15403 /* We only partially substituted into the parameter
15404 pack. Our type is TYPE_PACK_EXPANSION. */
15405 type = expanded_args;
15406 expanded_args = NULL_TREE;
15407 }
15408 }
15409 else
15410 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
15411
15412 /* Check if a substituted type is erroneous before substituting into
15413 the rest of the chain. */
15414 for (int i = 0; i < len; i++)
15415 {
15416 if (expanded_args)
15417 type = TREE_VEC_ELT (expanded_args, i);
15418
15419 if (type == error_mark_node)
15420 return error_mark_node;
15421 if (VOID_TYPE_P (type))
15422 {
15423 if (complain & tf_error)
15424 {
15425 error ("invalid parameter type %qT", type);
15426 if (in_decl)
15427 error ("in declaration %q+D", in_decl);
15428 }
15429 return error_mark_node;
15430 }
15431 }
15432
15433 /* We do not substitute into default arguments here. The standard
15434 mandates that they be instantiated only when needed, which is
15435 done in build_over_call. */
15436 tree default_arg = TREE_PURPOSE (arg_types);
15437
15438 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15439 since the new op() won't have any associated template arguments for us
15440 to refer to later. */
15441 if (lambda_fn_in_template_p (in_decl)
15442 || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
15443 && DECL_LOCAL_DECL_P (in_decl)))
15444 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl);
15445
15446 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15447 args, end, complain, in_decl);
15448 if (remaining_arg_types == error_mark_node)
15449 return error_mark_node;
15450
15451 for (int i = len-1; i >= 0; i--)
15452 {
15453 if (expanded_args)
15454 type = TREE_VEC_ELT (expanded_args, i);
15455
15456 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15457 top-level qualifiers as required. */
15458 type = cv_unqualified (type_decays_to (type));
15459
15460 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15461 {
15462 /* We've instantiated a template before its default arguments
15463 have been parsed. This can happen for a nested template
15464 class, and is not an error unless we require the default
15465 argument in a call of this function. */
15466 remaining_arg_types
15467 = tree_cons (default_arg, type, remaining_arg_types);
15468 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15469 remaining_arg_types);
15470 }
15471 else
15472 remaining_arg_types
15473 = hash_tree_cons (default_arg, type, remaining_arg_types);
15474 }
15475
15476 return remaining_arg_types;
15477 }
15478
15479 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15480 *not* handle the exception-specification for FNTYPE, because the
15481 initial substitution of explicitly provided template parameters
15482 during argument deduction forbids substitution into the
15483 exception-specification:
15484
15485 [temp.deduct]
15486
15487 All references in the function type of the function template to the
15488 corresponding template parameters are replaced by the specified tem-
15489 plate argument values. If a substitution in a template parameter or
15490 in the function type of the function template results in an invalid
15491 type, type deduction fails. [Note: The equivalent substitution in
15492 exception specifications is done only when the function is instanti-
15493 ated, at which point a program is ill-formed if the substitution
15494 results in an invalid type.] */
15495
15496 static tree
15497 tsubst_function_type (tree t,
15498 tree args,
15499 tsubst_flags_t complain,
15500 tree in_decl)
15501 {
15502 tree return_type;
15503 tree arg_types = NULL_TREE;
15504
15505 /* The TYPE_CONTEXT is not used for function/method types. */
15506 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15507
15508 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15509 failure. */
15510 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15511
15512 if (late_return_type_p)
15513 {
15514 /* Substitute the argument types. */
15515 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15516 complain, in_decl);
15517 if (arg_types == error_mark_node)
15518 return error_mark_node;
15519
15520 tree save_ccp = current_class_ptr;
15521 tree save_ccr = current_class_ref;
15522 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15523 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15524 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15525 if (do_inject)
15526 {
15527 /* DR 1207: 'this' is in scope in the trailing return type. */
15528 inject_this_parameter (this_type, cp_type_quals (this_type));
15529 }
15530
15531 /* Substitute the return type. */
15532 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15533
15534 if (do_inject)
15535 {
15536 current_class_ptr = save_ccp;
15537 current_class_ref = save_ccr;
15538 }
15539 }
15540 else
15541 /* Substitute the return type. */
15542 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15543
15544 if (return_type == error_mark_node)
15545 return error_mark_node;
15546 /* DR 486 clarifies that creation of a function type with an
15547 invalid return type is a deduction failure. */
15548 if (TREE_CODE (return_type) == ARRAY_TYPE
15549 || TREE_CODE (return_type) == FUNCTION_TYPE)
15550 {
15551 if (complain & tf_error)
15552 {
15553 if (TREE_CODE (return_type) == ARRAY_TYPE)
15554 error ("function returning an array");
15555 else
15556 error ("function returning a function");
15557 }
15558 return error_mark_node;
15559 }
15560
15561 if (!late_return_type_p)
15562 {
15563 /* Substitute the argument types. */
15564 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15565 complain, in_decl);
15566 if (arg_types == error_mark_node)
15567 return error_mark_node;
15568 }
15569
15570 /* Construct a new type node and return it. */
15571 return rebuild_function_or_method_type (t, return_type, arg_types,
15572 /*raises=*/NULL_TREE, complain);
15573 }
15574
15575 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15576 ARGS into that specification, and return the substituted
15577 specification. If there is no specification, return NULL_TREE. */
15578
15579 static tree
15580 tsubst_exception_specification (tree fntype,
15581 tree args,
15582 tsubst_flags_t complain,
15583 tree in_decl,
15584 bool defer_ok)
15585 {
15586 tree specs;
15587 tree new_specs;
15588
15589 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15590 new_specs = NULL_TREE;
15591 if (specs && TREE_PURPOSE (specs))
15592 {
15593 /* A noexcept-specifier. */
15594 tree expr = TREE_PURPOSE (specs);
15595 if (TREE_CODE (expr) == INTEGER_CST)
15596 new_specs = expr;
15597 else if (defer_ok)
15598 {
15599 /* Defer instantiation of noexcept-specifiers to avoid
15600 excessive instantiations (c++/49107). */
15601 new_specs = make_node (DEFERRED_NOEXCEPT);
15602 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15603 {
15604 /* We already partially instantiated this member template,
15605 so combine the new args with the old. */
15606 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15607 = DEFERRED_NOEXCEPT_PATTERN (expr);
15608 DEFERRED_NOEXCEPT_ARGS (new_specs)
15609 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15610 }
15611 else
15612 {
15613 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15614 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15615 }
15616 }
15617 else
15618 {
15619 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15620 {
15621 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15622 args);
15623 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15624 }
15625 new_specs = tsubst_copy_and_build (expr, args, complain, in_decl);
15626 }
15627 new_specs = build_noexcept_spec (new_specs, complain);
15628 /* We've instantiated a template before a noexcept-specifier
15629 contained therein has been parsed. This can happen for
15630 a nested template class:
15631
15632 struct S {
15633 template<typename> struct B { B() noexcept(...); };
15634 struct A : B<int> { ... use B() ... };
15635 };
15636
15637 where completing B<int> will trigger instantiating the
15638 noexcept, even though we only parse it at the end of S. */
15639 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
15640 {
15641 gcc_checking_assert (defer_ok);
15642 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
15643 }
15644 }
15645 else if (specs)
15646 {
15647 if (! TREE_VALUE (specs))
15648 new_specs = specs;
15649 else
15650 while (specs)
15651 {
15652 tree spec;
15653 int i, len = 1;
15654 tree expanded_specs = NULL_TREE;
15655
15656 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15657 {
15658 /* Expand the pack expansion type. */
15659 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15660 args, complain,
15661 in_decl);
15662
15663 if (expanded_specs == error_mark_node)
15664 return error_mark_node;
15665 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15666 len = TREE_VEC_LENGTH (expanded_specs);
15667 else
15668 {
15669 /* We're substituting into a member template, so
15670 we got a TYPE_PACK_EXPANSION back. Add that
15671 expansion and move on. */
15672 gcc_assert (TREE_CODE (expanded_specs)
15673 == TYPE_PACK_EXPANSION);
15674 new_specs = add_exception_specifier (new_specs,
15675 expanded_specs,
15676 complain);
15677 specs = TREE_CHAIN (specs);
15678 continue;
15679 }
15680 }
15681
15682 for (i = 0; i < len; ++i)
15683 {
15684 if (expanded_specs)
15685 spec = TREE_VEC_ELT (expanded_specs, i);
15686 else
15687 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15688 if (spec == error_mark_node)
15689 return spec;
15690 new_specs = add_exception_specifier (new_specs, spec,
15691 complain);
15692 }
15693
15694 specs = TREE_CHAIN (specs);
15695 }
15696 }
15697 return new_specs;
15698 }
15699
15700 /* Substitute through a TREE_LIST of types or expressions, handling pack
15701 expansions. */
15702
15703 tree
15704 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15705 {
15706 if (t == void_list_node)
15707 return t;
15708
15709 tree purpose = TREE_PURPOSE (t);
15710 tree purposevec = NULL_TREE;
15711 if (!purpose)
15712 ;
15713 else if (PACK_EXPANSION_P (purpose))
15714 {
15715 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15716 if (TREE_CODE (purpose) == TREE_VEC)
15717 purposevec = purpose;
15718 }
15719 else if (TYPE_P (purpose))
15720 purpose = tsubst (purpose, args, complain, in_decl);
15721 else
15722 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15723 if (purpose == error_mark_node || purposevec == error_mark_node)
15724 return error_mark_node;
15725
15726 tree value = TREE_VALUE (t);
15727 tree valuevec = NULL_TREE;
15728 if (!value)
15729 ;
15730 else if (PACK_EXPANSION_P (value))
15731 {
15732 value = tsubst_pack_expansion (value, args, complain, in_decl);
15733 if (TREE_CODE (value) == TREE_VEC)
15734 valuevec = value;
15735 }
15736 else if (TYPE_P (value))
15737 value = tsubst (value, args, complain, in_decl);
15738 else
15739 value = tsubst_copy_and_build (value, args, complain, in_decl);
15740 if (value == error_mark_node || valuevec == error_mark_node)
15741 return error_mark_node;
15742
15743 tree chain = TREE_CHAIN (t);
15744 if (!chain)
15745 ;
15746 else if (TREE_CODE (chain) == TREE_LIST)
15747 chain = tsubst_tree_list (chain, args, complain, in_decl);
15748 else if (TYPE_P (chain))
15749 chain = tsubst (chain, args, complain, in_decl);
15750 else
15751 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15752 if (chain == error_mark_node)
15753 return error_mark_node;
15754
15755 if (purpose == TREE_PURPOSE (t)
15756 && value == TREE_VALUE (t)
15757 && chain == TREE_CHAIN (t))
15758 return t;
15759
15760 int len;
15761 /* Determine the number of arguments. */
15762 if (purposevec)
15763 {
15764 len = TREE_VEC_LENGTH (purposevec);
15765 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15766 }
15767 else if (valuevec)
15768 len = TREE_VEC_LENGTH (valuevec);
15769 else
15770 len = 1;
15771
15772 for (int i = len; i-- > 0; )
15773 {
15774 if (purposevec)
15775 purpose = TREE_VEC_ELT (purposevec, i);
15776 if (valuevec)
15777 value = TREE_VEC_ELT (valuevec, i);
15778
15779 if (value && TYPE_P (value))
15780 chain = hash_tree_cons (purpose, value, chain);
15781 else
15782 chain = tree_cons (purpose, value, chain);
15783 }
15784
15785 return chain;
15786 }
15787
15788 /* Take the tree structure T and replace template parameters used
15789 therein with the argument vector ARGS. IN_DECL is an associated
15790 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15791 Issue error and warning messages under control of COMPLAIN. Note
15792 that we must be relatively non-tolerant of extensions here, in
15793 order to preserve conformance; if we allow substitutions that
15794 should not be allowed, we may allow argument deductions that should
15795 not succeed, and therefore report ambiguous overload situations
15796 where there are none. In theory, we could allow the substitution,
15797 but indicate that it should have failed, and allow our caller to
15798 make sure that the right thing happens, but we don't try to do this
15799 yet.
15800
15801 This function is used for dealing with types, decls and the like;
15802 for expressions, use tsubst_expr or tsubst_copy. */
15803
15804 tree
15805 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15806 {
15807 enum tree_code code;
15808 tree type, r = NULL_TREE;
15809
15810 if (t == NULL_TREE || t == error_mark_node
15811 || t == integer_type_node
15812 || t == void_type_node
15813 || t == char_type_node
15814 || t == unknown_type_node
15815 || TREE_CODE (t) == NAMESPACE_DECL
15816 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15817 return t;
15818
15819 if (DECL_P (t))
15820 return tsubst_decl (t, args, complain);
15821
15822 if (args == NULL_TREE)
15823 return t;
15824
15825 code = TREE_CODE (t);
15826
15827 gcc_assert (code != IDENTIFIER_NODE);
15828 type = TREE_TYPE (t);
15829
15830 gcc_assert (type != unknown_type_node);
15831
15832 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
15833 return d;
15834
15835 /* Reuse typedefs. We need to do this to handle dependent attributes,
15836 such as attribute aligned. */
15837 if (TYPE_P (t)
15838 && typedef_variant_p (t))
15839 {
15840 tree decl = TYPE_NAME (t);
15841
15842 if (alias_template_specialization_p (t, nt_opaque))
15843 {
15844 /* DECL represents an alias template and we want to
15845 instantiate it. */
15846 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15847 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15848 r = instantiate_alias_template (tmpl, gen_args, complain);
15849 }
15850 else if (DECL_CLASS_SCOPE_P (decl)
15851 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15852 && uses_template_parms (DECL_CONTEXT (decl)))
15853 {
15854 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15855 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15856 r = retrieve_specialization (tmpl, gen_args, 0);
15857 }
15858 else if (DECL_FUNCTION_SCOPE_P (decl)
15859 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15860 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15861 r = retrieve_local_specialization (decl);
15862 else
15863 /* The typedef is from a non-template context. */
15864 return t;
15865
15866 if (r)
15867 {
15868 r = TREE_TYPE (r);
15869 r = cp_build_qualified_type
15870 (r, cp_type_quals (t) | cp_type_quals (r),
15871 complain | tf_ignore_bad_quals);
15872 return r;
15873 }
15874 else
15875 {
15876 /* We don't have an instantiation yet, so drop the typedef. */
15877 int quals = cp_type_quals (t);
15878 t = DECL_ORIGINAL_TYPE (decl);
15879 t = cp_build_qualified_type (t, quals,
15880 complain | tf_ignore_bad_quals);
15881 }
15882 }
15883
15884 bool fndecl_type = (complain & tf_fndecl_type);
15885 complain &= ~tf_fndecl_type;
15886
15887 bool tst_ok = (complain & tf_tst_ok);
15888 complain &= ~tf_tst_ok;
15889
15890 if (type
15891 && code != TYPENAME_TYPE
15892 && code != TEMPLATE_TYPE_PARM
15893 && code != TEMPLATE_PARM_INDEX
15894 && code != IDENTIFIER_NODE
15895 && code != FUNCTION_TYPE
15896 && code != METHOD_TYPE)
15897 type = tsubst (type, args, complain, in_decl);
15898 if (type == error_mark_node)
15899 return error_mark_node;
15900
15901 switch (code)
15902 {
15903 case RECORD_TYPE:
15904 if (TYPE_PTRMEMFUNC_P (t))
15905 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
15906 /* Fall through. */
15907 case UNION_TYPE:
15908 case ENUMERAL_TYPE:
15909 return tsubst_aggr_type_1 (t, args, complain, in_decl,
15910 /*entering_scope=*/0);
15911
15912 case ERROR_MARK:
15913 case IDENTIFIER_NODE:
15914 case VOID_TYPE:
15915 case OPAQUE_TYPE:
15916 case REAL_TYPE:
15917 case COMPLEX_TYPE:
15918 case VECTOR_TYPE:
15919 case BOOLEAN_TYPE:
15920 case NULLPTR_TYPE:
15921 case LANG_TYPE:
15922 return t;
15923
15924 case INTEGER_TYPE:
15925 if (t == integer_type_node)
15926 return t;
15927
15928 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15929 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15930 return t;
15931
15932 {
15933 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15934
15935 max = tsubst_expr (omax, args, complain, in_decl);
15936
15937 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15938 needed. */
15939 if (TREE_CODE (max) == NOP_EXPR
15940 && TREE_SIDE_EFFECTS (omax)
15941 && !TREE_TYPE (max))
15942 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15943
15944 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15945 with TREE_SIDE_EFFECTS that indicates this is not an integral
15946 constant expression. */
15947 if (processing_template_decl
15948 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15949 {
15950 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15951 TREE_SIDE_EFFECTS (max) = 1;
15952 }
15953
15954 return compute_array_index_type (NULL_TREE, max, complain);
15955 }
15956
15957 case TEMPLATE_TYPE_PARM:
15958 if (template_placeholder_p (t))
15959 {
15960 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
15961 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
15962 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15963 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
15964
15965 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
15966 return make_template_placeholder (tmpl);
15967 else
15968 return t;
15969 }
15970 /* Fall through. */
15971 case TEMPLATE_TEMPLATE_PARM:
15972 case BOUND_TEMPLATE_TEMPLATE_PARM:
15973 case TEMPLATE_PARM_INDEX:
15974 {
15975 int idx;
15976 int level;
15977 int levels;
15978 tree arg = NULL_TREE;
15979
15980 r = NULL_TREE;
15981
15982 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15983 template_parm_level_and_index (t, &level, &idx);
15984
15985 levels = TMPL_ARGS_DEPTH (args);
15986 if (level <= levels
15987 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15988 {
15989 arg = TMPL_ARG (args, level, idx);
15990
15991 /* See through ARGUMENT_PACK_SELECT arguments. */
15992 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15993 arg = argument_pack_select_arg (arg);
15994 }
15995
15996 if (arg == error_mark_node)
15997 return error_mark_node;
15998 else if (arg != NULL_TREE)
15999 {
16000 if (ARGUMENT_PACK_P (arg))
16001 /* If ARG is an argument pack, we don't actually want to
16002 perform a substitution here, because substitutions
16003 for argument packs are only done
16004 element-by-element. We can get to this point when
16005 substituting the type of a non-type template
16006 parameter pack, when that type actually contains
16007 template parameter packs from an outer template, e.g.,
16008
16009 template<typename... Types> struct A {
16010 template<Types... Values> struct B { };
16011 }; */
16012 return t;
16013
16014 if (code == TEMPLATE_TYPE_PARM)
16015 {
16016 int quals;
16017
16018 /* When building concept checks for the purpose of
16019 deducing placeholders, we can end up with wildcards
16020 where types are expected. Adjust this to the deduced
16021 value. */
16022 if (TREE_CODE (arg) == WILDCARD_DECL)
16023 arg = TREE_TYPE (TREE_TYPE (arg));
16024
16025 gcc_assert (TYPE_P (arg));
16026
16027 quals = cp_type_quals (arg) | cp_type_quals (t);
16028
16029 return cp_build_qualified_type
16030 (arg, quals, complain | tf_ignore_bad_quals);
16031 }
16032 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
16033 {
16034 /* We are processing a type constructed from a
16035 template template parameter. */
16036 tree argvec = tsubst (TYPE_TI_ARGS (t),
16037 args, complain, in_decl);
16038 if (argvec == error_mark_node)
16039 return error_mark_node;
16040
16041 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
16042 || TREE_CODE (arg) == TEMPLATE_DECL
16043 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
16044
16045 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
16046 /* Consider this code:
16047
16048 template <template <class> class Template>
16049 struct Internal {
16050 template <class Arg> using Bind = Template<Arg>;
16051 };
16052
16053 template <template <class> class Template, class Arg>
16054 using Instantiate = Template<Arg>; //#0
16055
16056 template <template <class> class Template,
16057 class Argument>
16058 using Bind =
16059 Instantiate<Internal<Template>::template Bind,
16060 Argument>; //#1
16061
16062 When #1 is parsed, the
16063 BOUND_TEMPLATE_TEMPLATE_PARM representing the
16064 parameter `Template' in #0 matches the
16065 UNBOUND_CLASS_TEMPLATE representing the argument
16066 `Internal<Template>::template Bind'; We then want
16067 to assemble the type `Bind<Argument>' that can't
16068 be fully created right now, because
16069 `Internal<Template>' not being complete, the Bind
16070 template cannot be looked up in that context. So
16071 we need to "store" `Bind<Argument>' for later
16072 when the context of Bind becomes complete. Let's
16073 store that in a TYPENAME_TYPE. */
16074 return make_typename_type (TYPE_CONTEXT (arg),
16075 build_nt (TEMPLATE_ID_EXPR,
16076 TYPE_IDENTIFIER (arg),
16077 argvec),
16078 typename_type,
16079 complain);
16080
16081 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
16082 are resolving nested-types in the signature of a
16083 member function templates. Otherwise ARG is a
16084 TEMPLATE_DECL and is the real template to be
16085 instantiated. */
16086 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16087 arg = TYPE_NAME (arg);
16088
16089 r = lookup_template_class (arg,
16090 argvec, in_decl,
16091 DECL_CONTEXT (arg),
16092 /*entering_scope=*/0,
16093 complain);
16094 return cp_build_qualified_type
16095 (r, cp_type_quals (t) | cp_type_quals (r), complain);
16096 }
16097 else if (code == TEMPLATE_TEMPLATE_PARM)
16098 return arg;
16099 else
16100 /* TEMPLATE_PARM_INDEX. */
16101 return convert_from_reference (unshare_expr (arg));
16102 }
16103
16104 if (level == 1)
16105 /* This can happen during the attempted tsubst'ing in
16106 unify. This means that we don't yet have any information
16107 about the template parameter in question. */
16108 return t;
16109
16110 /* Early in template argument deduction substitution, we don't
16111 want to reduce the level of 'auto', or it will be confused
16112 with a normal template parm in subsequent deduction.
16113 Similarly, don't reduce the level of template parameters to
16114 avoid mismatches when deducing their types. */
16115 if (complain & tf_partial)
16116 return t;
16117
16118 /* If we get here, we must have been looking at a parm for a
16119 more deeply nested template. Make a new version of this
16120 template parameter, but with a lower level. */
16121 switch (code)
16122 {
16123 case TEMPLATE_TYPE_PARM:
16124 case TEMPLATE_TEMPLATE_PARM:
16125 case BOUND_TEMPLATE_TEMPLATE_PARM:
16126 if (cp_type_quals (t))
16127 {
16128 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
16129 r = cp_build_qualified_type
16130 (r, cp_type_quals (t),
16131 complain | (code == TEMPLATE_TYPE_PARM
16132 ? tf_ignore_bad_quals : 0));
16133 }
16134 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
16135 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
16136 && (r = (TEMPLATE_PARM_DESCENDANTS
16137 (TEMPLATE_TYPE_PARM_INDEX (t))))
16138 && (r = TREE_TYPE (r))
16139 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r))
16140 /* Break infinite recursion when substituting the constraints
16141 of a constrained placeholder. */;
16142 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
16143 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
16144 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
16145 r = TEMPLATE_PARM_DESCENDANTS (arg))
16146 && (TEMPLATE_PARM_LEVEL (r)
16147 == TEMPLATE_PARM_LEVEL (arg) - levels))
16148 /* Cache the simple case of lowering a type parameter. */
16149 r = TREE_TYPE (r);
16150 else
16151 {
16152 r = copy_type (t);
16153 TEMPLATE_TYPE_PARM_INDEX (r)
16154 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
16155 r, levels, args, complain);
16156 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
16157 TYPE_MAIN_VARIANT (r) = r;
16158 TYPE_POINTER_TO (r) = NULL_TREE;
16159 TYPE_REFERENCE_TO (r) = NULL_TREE;
16160
16161 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
16162 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
16163 /* Propagate constraints on placeholders since they are
16164 only instantiated during satisfaction. */
16165 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
16166
16167 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
16168 {
16169 tree tinfo = TYPE_TEMPLATE_INFO (t);
16170 /* We might need to substitute into the types of non-type
16171 template parameters. */
16172 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
16173 complain, in_decl);
16174 if (tmpl == error_mark_node)
16175 return error_mark_node;
16176 tree argvec = tsubst (TI_ARGS (tinfo), args,
16177 complain, in_decl);
16178 if (argvec == error_mark_node)
16179 return error_mark_node;
16180
16181 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
16182 = build_template_info (tmpl, argvec);
16183 }
16184
16185 if (TYPE_STRUCTURAL_EQUALITY_P (t))
16186 SET_TYPE_STRUCTURAL_EQUALITY (r);
16187 else
16188 TYPE_CANONICAL (r) = canonical_type_parameter (r);
16189 }
16190 break;
16191
16192 case TEMPLATE_PARM_INDEX:
16193 /* OK, now substitute the type of the non-type parameter. We
16194 couldn't do it earlier because it might be an auto parameter,
16195 and we wouldn't need to if we had an argument. */
16196 type = tsubst (type, args, complain, in_decl);
16197 if (type == error_mark_node)
16198 return error_mark_node;
16199 r = reduce_template_parm_level (t, type, levels, args, complain);
16200 break;
16201
16202 default:
16203 gcc_unreachable ();
16204 }
16205
16206 return r;
16207 }
16208
16209 case TREE_LIST:
16210 return tsubst_tree_list (t, args, complain, in_decl);
16211
16212 case TREE_BINFO:
16213 /* We should never be tsubsting a binfo. */
16214 gcc_unreachable ();
16215
16216 case TREE_VEC:
16217 /* A vector of template arguments. */
16218 gcc_assert (!type);
16219 return tsubst_template_args (t, args, complain, in_decl);
16220
16221 case POINTER_TYPE:
16222 case REFERENCE_TYPE:
16223 {
16224 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
16225 return t;
16226
16227 /* [temp.deduct]
16228
16229 Type deduction may fail for any of the following
16230 reasons:
16231
16232 -- Attempting to create a pointer to reference type.
16233 -- Attempting to create a reference to a reference type or
16234 a reference to void.
16235
16236 Core issue 106 says that creating a reference to a reference
16237 during instantiation is no longer a cause for failure. We
16238 only enforce this check in strict C++98 mode. */
16239 if ((TYPE_REF_P (type)
16240 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
16241 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
16242 {
16243 static location_t last_loc;
16244
16245 /* We keep track of the last time we issued this error
16246 message to avoid spewing a ton of messages during a
16247 single bad template instantiation. */
16248 if (complain & tf_error
16249 && last_loc != input_location)
16250 {
16251 if (VOID_TYPE_P (type))
16252 error ("forming reference to void");
16253 else if (code == POINTER_TYPE)
16254 error ("forming pointer to reference type %qT", type);
16255 else
16256 error ("forming reference to reference type %qT", type);
16257 last_loc = input_location;
16258 }
16259
16260 return error_mark_node;
16261 }
16262 else if (TREE_CODE (type) == FUNCTION_TYPE
16263 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
16264 || type_memfn_rqual (type) != REF_QUAL_NONE))
16265 {
16266 if (complain & tf_error)
16267 {
16268 if (code == POINTER_TYPE)
16269 error ("forming pointer to qualified function type %qT",
16270 type);
16271 else
16272 error ("forming reference to qualified function type %qT",
16273 type);
16274 }
16275 return error_mark_node;
16276 }
16277 else if (code == POINTER_TYPE)
16278 {
16279 r = build_pointer_type (type);
16280 if (TREE_CODE (type) == METHOD_TYPE)
16281 r = build_ptrmemfunc_type (r);
16282 }
16283 else if (TYPE_REF_P (type))
16284 /* In C++0x, during template argument substitution, when there is an
16285 attempt to create a reference to a reference type, reference
16286 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
16287
16288 "If a template-argument for a template-parameter T names a type
16289 that is a reference to a type A, an attempt to create the type
16290 'lvalue reference to cv T' creates the type 'lvalue reference to
16291 A,' while an attempt to create the type type rvalue reference to
16292 cv T' creates the type T"
16293 */
16294 r = cp_build_reference_type
16295 (TREE_TYPE (type),
16296 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
16297 else
16298 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
16299 r = cp_build_qualified_type (r, cp_type_quals (t), complain);
16300
16301 if (r != error_mark_node)
16302 /* Will this ever be needed for TYPE_..._TO values? */
16303 layout_type (r);
16304
16305 return r;
16306 }
16307 case OFFSET_TYPE:
16308 {
16309 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
16310 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
16311 {
16312 /* [temp.deduct]
16313
16314 Type deduction may fail for any of the following
16315 reasons:
16316
16317 -- Attempting to create "pointer to member of T" when T
16318 is not a class type. */
16319 if (complain & tf_error)
16320 error ("creating pointer to member of non-class type %qT", r);
16321 return error_mark_node;
16322 }
16323 if (TYPE_REF_P (type))
16324 {
16325 if (complain & tf_error)
16326 error ("creating pointer to member reference type %qT", type);
16327 return error_mark_node;
16328 }
16329 if (VOID_TYPE_P (type))
16330 {
16331 if (complain & tf_error)
16332 error ("creating pointer to member of type void");
16333 return error_mark_node;
16334 }
16335 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
16336 if (TREE_CODE (type) == FUNCTION_TYPE)
16337 {
16338 /* The type of the implicit object parameter gets its
16339 cv-qualifiers from the FUNCTION_TYPE. */
16340 tree memptr;
16341 tree method_type
16342 = build_memfn_type (type, r, type_memfn_quals (type),
16343 type_memfn_rqual (type));
16344 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
16345 return cp_build_qualified_type (memptr, cp_type_quals (t),
16346 complain);
16347 }
16348 else
16349 return cp_build_qualified_type (build_ptrmem_type (r, type),
16350 cp_type_quals (t),
16351 complain);
16352 }
16353 case FUNCTION_TYPE:
16354 case METHOD_TYPE:
16355 {
16356 tree fntype;
16357 tree specs;
16358 fntype = tsubst_function_type (t, args, complain, in_decl);
16359 if (fntype == error_mark_node)
16360 return error_mark_node;
16361
16362 /* Substitute the exception specification. */
16363 specs = tsubst_exception_specification (t, args, complain, in_decl,
16364 /*defer_ok*/fndecl_type);
16365 if (specs == error_mark_node)
16366 return error_mark_node;
16367 if (specs)
16368 fntype = build_exception_variant (fntype, specs);
16369 return fntype;
16370 }
16371 case ARRAY_TYPE:
16372 {
16373 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
16374 if (domain == error_mark_node)
16375 return error_mark_node;
16376
16377 /* As an optimization, we avoid regenerating the array type if
16378 it will obviously be the same as T. */
16379 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
16380 return t;
16381
16382 /* These checks should match the ones in create_array_type_for_decl.
16383
16384 [temp.deduct]
16385
16386 The deduction may fail for any of the following reasons:
16387
16388 -- Attempting to create an array with an element type that
16389 is void, a function type, or a reference type, or [DR337]
16390 an abstract class type. */
16391 if (VOID_TYPE_P (type)
16392 || TREE_CODE (type) == FUNCTION_TYPE
16393 || (TREE_CODE (type) == ARRAY_TYPE
16394 && TYPE_DOMAIN (type) == NULL_TREE)
16395 || TYPE_REF_P (type))
16396 {
16397 if (complain & tf_error)
16398 error ("creating array of %qT", type);
16399 return error_mark_node;
16400 }
16401
16402 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
16403 !(complain & tf_error)))
16404 return error_mark_node;
16405
16406 r = build_cplus_array_type (type, domain);
16407
16408 if (!valid_array_size_p (input_location, r, in_decl,
16409 (complain & tf_error)))
16410 return error_mark_node;
16411
16412 if (TYPE_USER_ALIGN (t))
16413 {
16414 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
16415 TYPE_USER_ALIGN (r) = 1;
16416 }
16417
16418 return r;
16419 }
16420
16421 case TYPENAME_TYPE:
16422 {
16423 tree ctx = TYPE_CONTEXT (t);
16424 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16425 {
16426 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
16427 if (ctx == error_mark_node
16428 || TREE_VEC_LENGTH (ctx) > 1)
16429 return error_mark_node;
16430 if (TREE_VEC_LENGTH (ctx) == 0)
16431 {
16432 if (complain & tf_error)
16433 error ("%qD is instantiated for an empty pack",
16434 TYPENAME_TYPE_FULLNAME (t));
16435 return error_mark_node;
16436 }
16437 ctx = TREE_VEC_ELT (ctx, 0);
16438 }
16439 else
16440 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
16441 /*entering_scope=*/1);
16442 if (ctx == error_mark_node)
16443 return error_mark_node;
16444
16445 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
16446 complain, in_decl);
16447 if (f == error_mark_node)
16448 return error_mark_node;
16449
16450 if (!MAYBE_CLASS_TYPE_P (ctx))
16451 {
16452 if (complain & tf_error)
16453 error ("%qT is not a class, struct, or union type", ctx);
16454 return error_mark_node;
16455 }
16456 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16457 {
16458 /* Normally, make_typename_type does not require that the CTX
16459 have complete type in order to allow things like:
16460
16461 template <class T> struct S { typename S<T>::X Y; };
16462
16463 But, such constructs have already been resolved by this
16464 point, so here CTX really should have complete type, unless
16465 it's a partial instantiation. */
16466 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16467 return error_mark_node;
16468 }
16469
16470 tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
16471 if (tst_ok)
16472 tcomplain |= tf_tst_ok;
16473 f = make_typename_type (ctx, f, typename_type, tcomplain);
16474 if (f == error_mark_node)
16475 return f;
16476 if (TREE_CODE (f) == TYPE_DECL)
16477 {
16478 complain |= tf_ignore_bad_quals;
16479 f = TREE_TYPE (f);
16480 }
16481
16482 if (TREE_CODE (f) != TYPENAME_TYPE)
16483 {
16484 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16485 {
16486 if (complain & tf_error)
16487 error ("%qT resolves to %qT, which is not an enumeration type",
16488 t, f);
16489 else
16490 return error_mark_node;
16491 }
16492 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16493 {
16494 if (complain & tf_error)
16495 error ("%qT resolves to %qT, which is not a class type",
16496 t, f);
16497 else
16498 return error_mark_node;
16499 }
16500 }
16501
16502 return cp_build_qualified_type
16503 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16504 }
16505
16506 case UNBOUND_CLASS_TEMPLATE:
16507 {
16508 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16509 in_decl, /*entering_scope=*/1);
16510 tree name = TYPE_IDENTIFIER (t);
16511 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16512
16513 if (ctx == error_mark_node || name == error_mark_node)
16514 return error_mark_node;
16515
16516 if (parm_list)
16517 parm_list = tsubst_template_parms (parm_list, args, complain);
16518 return make_unbound_class_template (ctx, name, parm_list, complain);
16519 }
16520
16521 case TYPEOF_TYPE:
16522 {
16523 tree type;
16524
16525 ++cp_unevaluated_operand;
16526 ++c_inhibit_evaluation_warnings;
16527
16528 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
16529
16530 --cp_unevaluated_operand;
16531 --c_inhibit_evaluation_warnings;
16532
16533 type = finish_typeof (type);
16534 return cp_build_qualified_type (type,
16535 cp_type_quals (t)
16536 | cp_type_quals (type),
16537 complain);
16538 }
16539
16540 case DECLTYPE_TYPE:
16541 {
16542 tree type;
16543
16544 ++cp_unevaluated_operand;
16545 ++c_inhibit_evaluation_warnings;
16546
16547 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16548 complain|tf_decltype, in_decl);
16549
16550 --cp_unevaluated_operand;
16551 --c_inhibit_evaluation_warnings;
16552
16553 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16554 type = lambda_capture_field_type (type,
16555 false /*explicit_init*/,
16556 DECLTYPE_FOR_REF_CAPTURE (t));
16557 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16558 type = lambda_proxy_type (type);
16559 else
16560 {
16561 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16562 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16563 && EXPR_P (type))
16564 /* In a template ~id could be either a complement expression
16565 or an unqualified-id naming a destructor; if instantiating
16566 it produces an expression, it's not an id-expression or
16567 member access. */
16568 id = false;
16569 type = finish_decltype_type (type, id, complain);
16570 }
16571 return cp_build_qualified_type (type,
16572 cp_type_quals (t)
16573 | cp_type_quals (type),
16574 complain | tf_ignore_bad_quals);
16575 }
16576
16577 case TRAIT_TYPE:
16578 {
16579 tree type1 = tsubst (TRAIT_TYPE_TYPE1 (t), args, complain, in_decl);
16580 tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
16581 type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2);
16582 return cp_build_qualified_type (type,
16583 cp_type_quals (t) | cp_type_quals (type),
16584 complain | tf_ignore_bad_quals);
16585 }
16586
16587 case TYPE_ARGUMENT_PACK:
16588 case NONTYPE_ARGUMENT_PACK:
16589 return tsubst_argument_pack (t, args, complain, in_decl);
16590
16591 case VOID_CST:
16592 case INTEGER_CST:
16593 case REAL_CST:
16594 case STRING_CST:
16595 case PLUS_EXPR:
16596 case MINUS_EXPR:
16597 case NEGATE_EXPR:
16598 case NOP_EXPR:
16599 case INDIRECT_REF:
16600 case ADDR_EXPR:
16601 case CALL_EXPR:
16602 case ARRAY_REF:
16603 case SCOPE_REF:
16604 /* We should use one of the expression tsubsts for these codes. */
16605 gcc_unreachable ();
16606
16607 default:
16608 sorry ("use of %qs in template", get_tree_code_name (code));
16609 return error_mark_node;
16610 }
16611 }
16612
16613 /* OLDFNS is a lookup set of member functions from some class template, and
16614 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
16615 of that class template. Return the subset of NEWFNS which are
16616 specializations of a function from OLDFNS. */
16617
16618 static tree
16619 filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
16620 {
16621 /* Record all member functions from the old lookup set OLDFNS into
16622 VISIBLE_SET. */
16623 hash_set<tree> visible_set;
16624 bool seen_dep_using = false;
16625 for (tree fn : lkp_range (oldfns))
16626 {
16627 if (TREE_CODE (fn) == USING_DECL)
16628 {
16629 /* Imprecisely handle dependent using-decl by keeping all members
16630 in the new lookup set that are defined in a base class, i.e.
16631 members that could plausibly have been introduced by this
16632 dependent using-decl.
16633 FIXME: Track which members are introduced by a dependent
16634 using-decl precisely, perhaps by performing another lookup
16635 from the substituted USING_DECL_SCOPE. */
16636 gcc_checking_assert (DECL_DEPENDENT_P (fn));
16637 seen_dep_using = true;
16638 }
16639 else
16640 visible_set.add (fn);
16641 }
16642
16643 /* Returns true iff (a less specialized version of) FN appeared in
16644 the old lookup set OLDFNS. */
16645 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
16646 if (DECL_CONTEXT (fn) != newtype)
16647 /* FN is a member function from a base class, introduced via a
16648 using-decl; if it might have been introduced by a dependent
16649 using-decl then just conservatively keep it, otherwise look
16650 in the old lookup set for FN exactly. */
16651 return seen_dep_using || visible_set.contains (fn);
16652 else if (TREE_CODE (fn) == TEMPLATE_DECL)
16653 /* FN is a member function template from the current class;
16654 look in the old lookup set for the TEMPLATE_DECL from which
16655 it was specialized. */
16656 return visible_set.contains (DECL_TI_TEMPLATE (fn));
16657 else
16658 /* FN is a non-template member function from the current class;
16659 look in the old lookup set for the FUNCTION_DECL from which
16660 it was specialized. */
16661 return visible_set.contains (DECL_TEMPLATE_RESULT
16662 (DECL_TI_TEMPLATE (fn)));
16663 };
16664
16665 bool lookup_changed_p = false;
16666 for (tree fn : lkp_range (newfns))
16667 if (!visible_p (fn))
16668 {
16669 lookup_changed_p = true;
16670 break;
16671 }
16672 if (!lookup_changed_p)
16673 return newfns;
16674
16675 /* Filter out from NEWFNS the member functions that weren't
16676 previously visible according to OLDFNS. */
16677 tree filtered_fns = NULL_TREE;
16678 unsigned filtered_size = 0;
16679 for (tree fn : lkp_range (newfns))
16680 if (visible_p (fn))
16681 {
16682 filtered_fns = lookup_add (fn, filtered_fns);
16683 filtered_size++;
16684 }
16685 gcc_checking_assert (seen_dep_using
16686 ? filtered_size >= visible_set.elements ()
16687 : filtered_size == visible_set.elements ());
16688
16689 return filtered_fns;
16690 }
16691
16692 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16693 expression on the left-hand side of the "." or "->" operator. We
16694 only do the lookup if we had a dependent BASELINK. Otherwise we
16695 adjust it onto the instantiated heirarchy. */
16696
16697 static tree
16698 tsubst_baselink (tree baselink, tree object_type,
16699 tree args, tsubst_flags_t complain, tree in_decl)
16700 {
16701 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16702 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16703 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16704
16705 tree optype = BASELINK_OPTYPE (baselink);
16706 optype = tsubst (optype, args, complain, in_decl);
16707
16708 tree template_args = NULL_TREE;
16709 bool template_id_p = false;
16710 tree fns = BASELINK_FUNCTIONS (baselink);
16711 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16712 {
16713 template_id_p = true;
16714 template_args = TREE_OPERAND (fns, 1);
16715 fns = TREE_OPERAND (fns, 0);
16716 if (template_args)
16717 template_args = tsubst_template_args (template_args, args,
16718 complain, in_decl);
16719 }
16720
16721 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16722 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16723 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
16724 || optype != BASELINK_OPTYPE (baselink));
16725
16726 if (dependent_p)
16727 {
16728 tree name = OVL_NAME (fns);
16729 if (IDENTIFIER_CONV_OP_P (name))
16730 name = make_conv_op_name (optype);
16731
16732 /* See maybe_dependent_member_ref. */
16733 if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
16734 {
16735 if (template_id_p)
16736 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
16737 template_args);
16738 return build_qualified_name (NULL_TREE, qualifying_scope, name,
16739 /* ::template */false);
16740 }
16741
16742 if (name == complete_dtor_identifier)
16743 /* Treat as-if non-dependent below. */
16744 dependent_p = false;
16745
16746 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
16747 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16748 complain);
16749 if (maybe_incomplete)
16750 {
16751 /* Filter out from the new lookup set those functions which didn't
16752 appear in the original lookup set (in a less specialized form).
16753 This is needed to preserve the consistency of member lookup
16754 performed in an incomplete-class context, within which
16755 later-declared members ought to remain invisible. */
16756 BASELINK_FUNCTIONS (baselink)
16757 = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
16758 binfo_type);
16759 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
16760 }
16761
16762 if (!baselink)
16763 {
16764 if ((complain & tf_error)
16765 && constructor_name_p (name, qualifying_scope))
16766 error ("cannot call constructor %<%T::%D%> directly",
16767 qualifying_scope, name);
16768 return error_mark_node;
16769 }
16770
16771 fns = BASELINK_FUNCTIONS (baselink);
16772 }
16773 else
16774 {
16775 /* We're going to overwrite pieces below, make a duplicate. */
16776 baselink = copy_node (baselink);
16777
16778 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16779 {
16780 /* The decl we found was from non-dependent scope, but we still need
16781 to update the binfos for the instantiated qualifying_scope. */
16782 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16783 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16784 ba_unique, nullptr, complain);
16785 }
16786 }
16787
16788 /* If lookup found a single function, mark it as used at this point.
16789 (If lookup found multiple functions the one selected later by
16790 overload resolution will be marked as used at that point.) */
16791 if (!template_id_p && !really_overloaded_fn (fns))
16792 {
16793 tree fn = OVL_FIRST (fns);
16794 bool ok = mark_used (fn, complain);
16795 if (!ok && !(complain & tf_error))
16796 return error_mark_node;
16797 if (ok && BASELINK_P (baselink))
16798 /* We might have instantiated an auto function. */
16799 TREE_TYPE (baselink) = TREE_TYPE (fn);
16800 }
16801
16802 if (BASELINK_P (baselink))
16803 {
16804 /* Add back the template arguments, if present. */
16805 if (template_id_p)
16806 BASELINK_FUNCTIONS (baselink)
16807 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16808
16809 /* Update the conversion operator type. */
16810 BASELINK_OPTYPE (baselink) = optype;
16811 }
16812
16813 if (!object_type)
16814 object_type = current_class_type;
16815
16816 if (qualified_p || !dependent_p)
16817 {
16818 baselink = adjust_result_of_qualified_name_lookup (baselink,
16819 qualifying_scope,
16820 object_type);
16821 if (!qualified_p)
16822 /* We need to call adjust_result_of_qualified_name_lookup in case the
16823 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16824 so that we still get virtual function binding. */
16825 BASELINK_QUALIFIED_P (baselink) = false;
16826 }
16827
16828 return baselink;
16829 }
16830
16831 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16832 true if the qualified-id will be a postfix-expression in-and-of
16833 itself; false if more of the postfix-expression follows the
16834 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16835 of "&". */
16836
16837 static tree
16838 tsubst_qualified_id (tree qualified_id, tree args,
16839 tsubst_flags_t complain, tree in_decl,
16840 bool done, bool address_p)
16841 {
16842 tree expr;
16843 tree scope;
16844 tree name;
16845 bool is_template;
16846 tree template_args;
16847 location_t loc = EXPR_LOCATION (qualified_id);
16848
16849 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16850
16851 /* Figure out what name to look up. */
16852 name = TREE_OPERAND (qualified_id, 1);
16853 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16854 {
16855 is_template = true;
16856 template_args = TREE_OPERAND (name, 1);
16857 if (template_args)
16858 template_args = tsubst_template_args (template_args, args,
16859 complain, in_decl);
16860 if (template_args == error_mark_node)
16861 return error_mark_node;
16862 name = TREE_OPERAND (name, 0);
16863 }
16864 else
16865 {
16866 is_template = false;
16867 template_args = NULL_TREE;
16868 }
16869
16870 /* Substitute into the qualifying scope. When there are no ARGS, we
16871 are just trying to simplify a non-dependent expression. In that
16872 case the qualifying scope may be dependent, and, in any case,
16873 substituting will not help. */
16874 scope = TREE_OPERAND (qualified_id, 0);
16875 if (args)
16876 {
16877 scope = tsubst (scope, args, complain, in_decl);
16878 expr = tsubst_copy (name, args, complain, in_decl);
16879 }
16880 else
16881 expr = name;
16882
16883 if (dependent_scope_p (scope))
16884 {
16885 if (TREE_CODE (expr) == SCOPE_REF)
16886 /* We built one in tsubst_baselink. */
16887 gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
16888 else
16889 {
16890 if (is_template)
16891 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
16892 template_args);
16893 expr = build_qualified_name (NULL_TREE, scope, expr,
16894 QUALIFIED_NAME_IS_TEMPLATE
16895 (qualified_id));
16896 }
16897 REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
16898 return expr;
16899 }
16900
16901 if (!BASELINK_P (name) && !DECL_P (expr))
16902 {
16903 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16904 {
16905 /* A BIT_NOT_EXPR is used to represent a destructor. */
16906 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16907 {
16908 error ("qualifying type %qT does not match destructor name ~%qT",
16909 scope, TREE_OPERAND (expr, 0));
16910 expr = error_mark_node;
16911 }
16912 else
16913 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16914 LOOK_want::NORMAL, false);
16915 }
16916 else
16917 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16918 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16919 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16920 {
16921 if (complain & tf_error)
16922 {
16923 error ("dependent-name %qE is parsed as a non-type, but "
16924 "instantiation yields a type", qualified_id);
16925 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16926 }
16927 return error_mark_node;
16928 }
16929 }
16930
16931 if (DECL_P (expr))
16932 {
16933 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16934 scope, complain))
16935 return error_mark_node;
16936 /* Remember that there was a reference to this entity. */
16937 if (!mark_used (expr, complain) && !(complain & tf_error))
16938 return error_mark_node;
16939 }
16940
16941 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16942 {
16943 if (complain & tf_error)
16944 qualified_name_lookup_error (scope,
16945 TREE_OPERAND (qualified_id, 1),
16946 expr, input_location);
16947 return error_mark_node;
16948 }
16949
16950 if (is_template)
16951 {
16952 /* We may be repeating a check already done during parsing, but
16953 if it was well-formed and passed then, it will pass again
16954 now, and if it didn't, we wouldn't have got here. The case
16955 we want to catch is when we couldn't tell then, and can now,
16956 namely when templ prior to substitution was an
16957 identifier. */
16958 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16959 return error_mark_node;
16960
16961 if (variable_template_p (expr))
16962 expr = lookup_and_finish_template_variable (expr, template_args,
16963 complain);
16964 else
16965 expr = lookup_template_function (expr, template_args);
16966 }
16967
16968 if (expr == error_mark_node && complain & tf_error)
16969 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16970 expr, input_location);
16971 else if (TYPE_P (scope))
16972 {
16973 expr = (adjust_result_of_qualified_name_lookup
16974 (expr, scope, current_nonlambda_class_type ()));
16975 expr = (finish_qualified_id_expr
16976 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16977 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16978 /*template_arg_p=*/false, complain));
16979 }
16980
16981 /* Expressions do not generally have reference type. */
16982 if (TREE_CODE (expr) != SCOPE_REF
16983 /* However, if we're about to form a pointer-to-member, we just
16984 want the referenced member referenced. */
16985 && TREE_CODE (expr) != OFFSET_REF)
16986 expr = convert_from_reference (expr);
16987
16988 if (REF_PARENTHESIZED_P (qualified_id))
16989 expr = force_paren_expr (expr);
16990
16991 expr = maybe_wrap_with_location (expr, loc);
16992
16993 return expr;
16994 }
16995
16996 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16997 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16998 for tsubst. */
16999
17000 static tree
17001 tsubst_init (tree init, tree decl, tree args,
17002 tsubst_flags_t complain, tree in_decl)
17003 {
17004 if (!init)
17005 return NULL_TREE;
17006
17007 init = tsubst_expr (init, args, complain, in_decl);
17008
17009 tree type = TREE_TYPE (decl);
17010
17011 if (!init && type != error_mark_node)
17012 {
17013 if (tree auto_node = type_uses_auto (type))
17014 {
17015 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17016 {
17017 if (complain & tf_error)
17018 error ("initializer for %q#D expands to an empty list "
17019 "of expressions", decl);
17020 return error_mark_node;
17021 }
17022 }
17023 else if (!dependent_type_p (type))
17024 {
17025 /* If we had an initializer but it
17026 instantiated to nothing,
17027 value-initialize the object. This will
17028 only occur when the initializer was a
17029 pack expansion where the parameter packs
17030 used in that expansion were of length
17031 zero. */
17032 init = build_value_init (type, complain);
17033 if (TREE_CODE (init) == AGGR_INIT_EXPR)
17034 init = get_target_expr (init, complain);
17035 if (TREE_CODE (init) == TARGET_EXPR)
17036 TARGET_EXPR_DIRECT_INIT_P (init) = true;
17037 }
17038 }
17039
17040 return init;
17041 }
17042
17043 /* If T is a reference to a dependent member of the current instantiation C and
17044 we are trying to refer to that member in a partial instantiation of C,
17045 return a SCOPE_REF; otherwise, return NULL_TREE.
17046
17047 This can happen when forming a C++17 deduction guide, as in PR96199. */
17048
17049 static tree
17050 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
17051 tree in_decl)
17052 {
17053 if (!(complain & tf_dguide))
17054 return NULL_TREE;
17055
17056 tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
17057 if (!decl || !DECL_P (decl))
17058 return NULL_TREE;
17059
17060 tree ctx = context_for_name_lookup (decl);
17061 if (!CLASS_TYPE_P (ctx))
17062 return NULL_TREE;
17063
17064 ctx = tsubst (ctx, args, complain, in_decl);
17065 if (!dependent_scope_p (ctx))
17066 return NULL_TREE;
17067
17068 if (TYPE_P (t))
17069 {
17070 if (typedef_variant_p (t))
17071 t = strip_typedefs (t);
17072 tree decl = TYPE_NAME (t);
17073 if (decl)
17074 decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
17075 if (!decl)
17076 return NULL_TREE;
17077 return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
17078 complain);
17079 }
17080
17081 tree name = DECL_NAME (t);
17082 tree fullname = name;
17083 if (instantiates_primary_template_p (t))
17084 {
17085 tree tinfo = get_template_info (t);
17086 name = DECL_NAME (TI_TEMPLATE (tinfo));
17087 tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
17088 targs = tsubst_template_args (targs, args, complain, in_decl);
17089 fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
17090 }
17091
17092 if (TREE_CODE (t) == TYPE_DECL)
17093 {
17094 if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
17095 && TYPE_NAME (TREE_TYPE (t)) == t)
17096 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
17097 scope, but it doesn't need to be rewritten again. */
17098 return NULL_TREE;
17099 tree type = build_typename_type (ctx, name, fullname, typename_type);
17100 return TYPE_NAME (type);
17101 }
17102 else if (DECL_TYPE_TEMPLATE_P (t))
17103 return make_unbound_class_template (ctx, name,
17104 NULL_TREE, complain);
17105 else
17106 return build_qualified_name (NULL_TREE, ctx, fullname,
17107 TREE_CODE (t) == TEMPLATE_DECL);
17108 }
17109
17110 /* Like tsubst, but deals with expressions. This function just replaces
17111 template parms; to finish processing the resultant expression, use
17112 tsubst_copy_and_build or tsubst_expr. */
17113
17114 static tree
17115 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17116 {
17117 enum tree_code code;
17118 tree r;
17119
17120 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
17121 return t;
17122
17123 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
17124 return d;
17125
17126 code = TREE_CODE (t);
17127
17128 switch (code)
17129 {
17130 case PARM_DECL:
17131 r = retrieve_local_specialization (t);
17132
17133 if (r == NULL_TREE)
17134 {
17135 /* We get here for a use of 'this' in an NSDMI. */
17136 if (DECL_NAME (t) == this_identifier && current_class_ptr)
17137 return current_class_ptr;
17138
17139 /* This can happen for a parameter name used later in a function
17140 declaration (such as in a late-specified return type). Just
17141 make a dummy decl, since it's only used for its type. */
17142 gcc_assert (cp_unevaluated_operand);
17143 r = tsubst_decl (t, args, complain);
17144 /* Give it the template pattern as its context; its true context
17145 hasn't been instantiated yet and this is good enough for
17146 mangling. */
17147 DECL_CONTEXT (r) = DECL_CONTEXT (t);
17148 }
17149
17150 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
17151 r = argument_pack_select_arg (r);
17152 if (!mark_used (r, complain) && !(complain & tf_error))
17153 return error_mark_node;
17154 return r;
17155
17156 case CONST_DECL:
17157 {
17158 tree enum_type;
17159 tree v;
17160
17161 if (DECL_TEMPLATE_PARM_P (t))
17162 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
17163 if (!uses_template_parms (DECL_CONTEXT (t)))
17164 return t;
17165
17166 /* Unfortunately, we cannot just call lookup_name here.
17167 Consider:
17168
17169 template <int I> int f() {
17170 enum E { a = I };
17171 struct S { void g() { E e = a; } };
17172 };
17173
17174 When we instantiate f<7>::S::g(), say, lookup_name is not
17175 clever enough to find f<7>::a. */
17176 enum_type
17177 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
17178 /*entering_scope=*/0);
17179
17180 for (v = TYPE_VALUES (enum_type);
17181 v != NULL_TREE;
17182 v = TREE_CHAIN (v))
17183 if (TREE_PURPOSE (v) == DECL_NAME (t))
17184 return TREE_VALUE (v);
17185
17186 /* We didn't find the name. That should never happen; if
17187 name-lookup found it during preliminary parsing, we
17188 should find it again here during instantiation. */
17189 gcc_unreachable ();
17190 }
17191 return t;
17192
17193 case FIELD_DECL:
17194 if (DECL_CONTEXT (t))
17195 {
17196 tree ctx;
17197
17198 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
17199 /*entering_scope=*/1);
17200 if (ctx != DECL_CONTEXT (t))
17201 {
17202 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
17203 if (!r)
17204 {
17205 if (complain & tf_error)
17206 error ("using invalid field %qD", t);
17207 return error_mark_node;
17208 }
17209 return r;
17210 }
17211 }
17212
17213 return t;
17214
17215 case VAR_DECL:
17216 case FUNCTION_DECL:
17217 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
17218 r = tsubst (t, args, complain, in_decl);
17219 else if (DECL_LOCAL_DECL_P (t))
17220 {
17221 /* Local specialization will usually have been created when
17222 we instantiated the DECL_EXPR_DECL. */
17223 r = retrieve_local_specialization (t);
17224 if (!r)
17225 {
17226 /* We're in a generic lambda referencing a local extern
17227 from an outer block-scope of a non-template. */
17228 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
17229 r = t;
17230 }
17231 }
17232 else if (local_variable_p (t)
17233 && uses_template_parms (DECL_CONTEXT (t)))
17234 {
17235 r = retrieve_local_specialization (t);
17236 if (r == NULL_TREE)
17237 {
17238 /* First try name lookup to find the instantiation. */
17239 r = lookup_name (DECL_NAME (t));
17240 if (r)
17241 {
17242 if (!VAR_P (r))
17243 {
17244 /* During error-recovery we may find a non-variable,
17245 even an OVERLOAD: just bail out and avoid ICEs and
17246 duplicate diagnostics (c++/62207). */
17247 gcc_assert (seen_error ());
17248 return error_mark_node;
17249 }
17250 if (!is_capture_proxy (r))
17251 {
17252 /* Make sure the one we found is the one we want. */
17253 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
17254 if (ctx != DECL_CONTEXT (r))
17255 r = NULL_TREE;
17256 }
17257 }
17258
17259 if (r)
17260 /* OK */;
17261 else
17262 {
17263 /* This can happen for a variable used in a
17264 late-specified return type of a local lambda, or for a
17265 local static or constant. Building a new VAR_DECL
17266 should be OK in all those cases. */
17267 r = tsubst_decl (t, args, complain);
17268 if (local_specializations)
17269 /* Avoid infinite recursion (79640). */
17270 register_local_specialization (r, t);
17271 if (decl_maybe_constant_var_p (r))
17272 {
17273 /* We can't call cp_finish_decl, so handle the
17274 initializer by hand. */
17275 tree init = tsubst_init (DECL_INITIAL (t), r, args,
17276 complain, in_decl);
17277 if (!processing_template_decl)
17278 init = maybe_constant_init (init);
17279 if (processing_template_decl
17280 ? potential_constant_expression (init)
17281 : reduced_constant_expression_p (init))
17282 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
17283 = TREE_CONSTANT (r) = true;
17284 DECL_INITIAL (r) = init;
17285 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
17286 TREE_TYPE (r)
17287 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
17288 complain, adc_variable_type);
17289 }
17290 gcc_assert (cp_unevaluated_operand
17291 || processing_contract_condition
17292 || TREE_STATIC (r)
17293 || decl_constant_var_p (r)
17294 || seen_error ());
17295 if (!processing_template_decl
17296 && !TREE_STATIC (r))
17297 r = process_outer_var_ref (r, complain);
17298 }
17299 /* Remember this for subsequent uses. */
17300 if (local_specializations)
17301 register_local_specialization (r, t);
17302 }
17303 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
17304 r = argument_pack_select_arg (r);
17305 }
17306 else
17307 r = t;
17308 if (!mark_used (r, complain))
17309 return error_mark_node;
17310 return r;
17311
17312 case NAMESPACE_DECL:
17313 return t;
17314
17315 case OVERLOAD:
17316 return t;
17317
17318 case BASELINK:
17319 return tsubst_baselink (t, current_nonlambda_class_type (),
17320 args, complain, in_decl);
17321
17322 case TEMPLATE_DECL:
17323 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
17324 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
17325 args, complain, in_decl);
17326 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
17327 return tsubst (t, args, complain, in_decl);
17328 else if (DECL_CLASS_SCOPE_P (t)
17329 && uses_template_parms (DECL_CONTEXT (t)))
17330 {
17331 /* Template template argument like the following example need
17332 special treatment:
17333
17334 template <template <class> class TT> struct C {};
17335 template <class T> struct D {
17336 template <class U> struct E {};
17337 C<E> c; // #1
17338 };
17339 D<int> d; // #2
17340
17341 We are processing the template argument `E' in #1 for
17342 the template instantiation #2. Originally, `E' is a
17343 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
17344 have to substitute this with one having context `D<int>'. */
17345
17346 tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
17347 in_decl, /*entering_scope=*/true);
17348 return lookup_field (context, DECL_NAME(t), 0, false);
17349 }
17350 else
17351 /* Ordinary template template argument. */
17352 return t;
17353
17354 case NON_LVALUE_EXPR:
17355 case VIEW_CONVERT_EXPR:
17356 {
17357 /* Handle location wrappers by substituting the wrapped node
17358 first, *then* reusing the resulting type. Doing the type
17359 first ensures that we handle template parameters and
17360 parameter pack expansions. */
17361 if (location_wrapper_p (t))
17362 {
17363 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
17364 complain, in_decl);
17365 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
17366 }
17367 tree op = TREE_OPERAND (t, 0);
17368 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
17369 if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
17370 {
17371 op = tsubst_copy (op, args, complain, in_decl);
17372 op = build1 (code, TREE_TYPE (op), op);
17373 REF_PARENTHESIZED_P (op) = true;
17374 return op;
17375 }
17376 /* We shouldn't see any other uses of these in templates
17377 (tsubst_copy_and_build handles C++20 tparm object wrappers). */
17378 gcc_unreachable ();
17379 }
17380
17381 case CAST_EXPR:
17382 case REINTERPRET_CAST_EXPR:
17383 case CONST_CAST_EXPR:
17384 case STATIC_CAST_EXPR:
17385 case DYNAMIC_CAST_EXPR:
17386 case IMPLICIT_CONV_EXPR:
17387 CASE_CONVERT:
17388 {
17389 tsubst_flags_t tcomplain = complain;
17390 if (code == CAST_EXPR)
17391 tcomplain |= tf_tst_ok;
17392 tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
17393 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17394 return build1 (code, type, op0);
17395 }
17396
17397 case BIT_CAST_EXPR:
17398 {
17399 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17400 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17401 r = build_min (BIT_CAST_EXPR, type, op0);
17402 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17403 return r;
17404 }
17405
17406 case SIZEOF_EXPR:
17407 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17408 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17409 {
17410 tree expanded, op = TREE_OPERAND (t, 0);
17411 int len = 0;
17412
17413 if (SIZEOF_EXPR_TYPE_P (t))
17414 op = TREE_TYPE (op);
17415
17416 ++cp_unevaluated_operand;
17417 ++c_inhibit_evaluation_warnings;
17418 /* We only want to compute the number of arguments. */
17419 if (PACK_EXPANSION_P (op))
17420 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
17421 else
17422 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
17423 args, complain, in_decl);
17424 --cp_unevaluated_operand;
17425 --c_inhibit_evaluation_warnings;
17426
17427 if (TREE_CODE (expanded) == TREE_VEC)
17428 {
17429 len = TREE_VEC_LENGTH (expanded);
17430 /* Set TREE_USED for the benefit of -Wunused. */
17431 for (int i = 0; i < len; i++)
17432 if (DECL_P (TREE_VEC_ELT (expanded, i)))
17433 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
17434 }
17435
17436 if (expanded == error_mark_node)
17437 return error_mark_node;
17438 else if (PACK_EXPANSION_P (expanded)
17439 || (TREE_CODE (expanded) == TREE_VEC
17440 && pack_expansion_args_count (expanded)))
17441
17442 {
17443 if (PACK_EXPANSION_P (expanded))
17444 /* OK. */;
17445 else if (TREE_VEC_LENGTH (expanded) == 1)
17446 expanded = TREE_VEC_ELT (expanded, 0);
17447 else
17448 expanded = make_argument_pack (expanded);
17449
17450 if (TYPE_P (expanded))
17451 return cxx_sizeof_or_alignof_type (input_location,
17452 expanded, SIZEOF_EXPR,
17453 false,
17454 complain & tf_error);
17455 else
17456 return cxx_sizeof_or_alignof_expr (input_location,
17457 expanded, SIZEOF_EXPR,
17458 false,
17459 complain & tf_error);
17460 }
17461 else
17462 return build_int_cst (size_type_node, len);
17463 }
17464 if (SIZEOF_EXPR_TYPE_P (t))
17465 {
17466 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
17467 args, complain, in_decl);
17468 r = build1 (NOP_EXPR, r, error_mark_node);
17469 r = build1 (SIZEOF_EXPR,
17470 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
17471 SIZEOF_EXPR_TYPE_P (r) = 1;
17472 return r;
17473 }
17474 /* Fall through */
17475
17476 case INDIRECT_REF:
17477 case NEGATE_EXPR:
17478 case TRUTH_NOT_EXPR:
17479 case BIT_NOT_EXPR:
17480 case ADDR_EXPR:
17481 case UNARY_PLUS_EXPR: /* Unary + */
17482 case ALIGNOF_EXPR:
17483 case AT_ENCODE_EXPR:
17484 case ARROW_EXPR:
17485 case THROW_EXPR:
17486 case TYPEID_EXPR:
17487 case REALPART_EXPR:
17488 case IMAGPART_EXPR:
17489 case PAREN_EXPR:
17490 {
17491 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17492 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17493 r = build1_loc (EXPR_LOCATION (t), code, type, op0);
17494 if (code == ALIGNOF_EXPR)
17495 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
17496 /* For addresses of immediate functions ensure we have EXPR_LOCATION
17497 set for possible later diagnostics. */
17498 if (code == ADDR_EXPR
17499 && EXPR_LOCATION (r) == UNKNOWN_LOCATION
17500 && TREE_CODE (op0) == FUNCTION_DECL
17501 && DECL_IMMEDIATE_FUNCTION_P (op0))
17502 SET_EXPR_LOCATION (r, input_location);
17503 return r;
17504 }
17505
17506 case EXCESS_PRECISION_EXPR:
17507 {
17508 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17509 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17510 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
17511 {
17512 gcc_checking_assert (same_type_p (type, TREE_TYPE (op0)));
17513 return op0;
17514 }
17515 return build1_loc (EXPR_LOCATION (t), code, type, op0);
17516 }
17517
17518 case COMPONENT_REF:
17519 {
17520 tree object;
17521 tree name;
17522
17523 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17524 name = TREE_OPERAND (t, 1);
17525 if (TREE_CODE (name) == BIT_NOT_EXPR)
17526 {
17527 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17528 complain, in_decl);
17529 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17530 }
17531 else if (TREE_CODE (name) == SCOPE_REF
17532 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
17533 {
17534 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
17535 complain, in_decl);
17536 name = TREE_OPERAND (name, 1);
17537 name = tsubst_copy (TREE_OPERAND (name, 0), args,
17538 complain, in_decl);
17539 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
17540 name = build_qualified_name (/*type=*/NULL_TREE,
17541 base, name,
17542 /*template_p=*/false);
17543 }
17544 else if (BASELINK_P (name))
17545 name = tsubst_baselink (name,
17546 non_reference (TREE_TYPE (object)),
17547 args, complain,
17548 in_decl);
17549 else
17550 name = tsubst_copy (name, args, complain, in_decl);
17551 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
17552 }
17553
17554 case PLUS_EXPR:
17555 case MINUS_EXPR:
17556 case MULT_EXPR:
17557 case TRUNC_DIV_EXPR:
17558 case CEIL_DIV_EXPR:
17559 case FLOOR_DIV_EXPR:
17560 case ROUND_DIV_EXPR:
17561 case EXACT_DIV_EXPR:
17562 case BIT_AND_EXPR:
17563 case BIT_IOR_EXPR:
17564 case BIT_XOR_EXPR:
17565 case TRUNC_MOD_EXPR:
17566 case FLOOR_MOD_EXPR:
17567 case TRUTH_ANDIF_EXPR:
17568 case TRUTH_ORIF_EXPR:
17569 case TRUTH_AND_EXPR:
17570 case TRUTH_OR_EXPR:
17571 case RSHIFT_EXPR:
17572 case LSHIFT_EXPR:
17573 case EQ_EXPR:
17574 case NE_EXPR:
17575 case MAX_EXPR:
17576 case MIN_EXPR:
17577 case LE_EXPR:
17578 case GE_EXPR:
17579 case LT_EXPR:
17580 case GT_EXPR:
17581 case COMPOUND_EXPR:
17582 case DOTSTAR_EXPR:
17583 case MEMBER_REF:
17584 case PREDECREMENT_EXPR:
17585 case PREINCREMENT_EXPR:
17586 case POSTDECREMENT_EXPR:
17587 case POSTINCREMENT_EXPR:
17588 {
17589 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17590 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17591 return build_nt (code, op0, op1);
17592 }
17593
17594 case SCOPE_REF:
17595 {
17596 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17597 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17598 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
17599 QUALIFIED_NAME_IS_TEMPLATE (t));
17600 }
17601
17602 case ARRAY_REF:
17603 {
17604 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17605 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17606 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
17607 }
17608
17609 case CALL_EXPR:
17610 {
17611 int n = VL_EXP_OPERAND_LENGTH (t);
17612 tree result = build_vl_exp (CALL_EXPR, n);
17613 int i;
17614 for (i = 0; i < n; i++)
17615 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17616 complain, in_decl);
17617 return result;
17618 }
17619
17620 case COND_EXPR:
17621 case MODOP_EXPR:
17622 case PSEUDO_DTOR_EXPR:
17623 case VEC_PERM_EXPR:
17624 {
17625 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17626 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17627 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17628 r = build_nt (code, op0, op1, op2);
17629 copy_warning (r, t);
17630 return r;
17631 }
17632
17633 case NEW_EXPR:
17634 {
17635 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17636 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17637 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17638 r = build_nt (code, op0, op1, op2);
17639 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17640 return r;
17641 }
17642
17643 case DELETE_EXPR:
17644 {
17645 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17646 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17647 r = build_nt (code, op0, op1);
17648 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17649 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17650 return r;
17651 }
17652
17653 case TEMPLATE_ID_EXPR:
17654 {
17655 /* Substituted template arguments */
17656 tree tmpl = TREE_OPERAND (t, 0);
17657 tree targs = TREE_OPERAND (t, 1);
17658
17659 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
17660 if (targs)
17661 targs = tsubst_template_args (targs, args, complain, in_decl);
17662
17663 if (variable_template_p (tmpl))
17664 return lookup_template_variable (tmpl, targs);
17665 else
17666 return lookup_template_function (tmpl, targs);
17667 }
17668
17669 case TREE_LIST:
17670 {
17671 tree purpose, value, chain;
17672
17673 if (t == void_list_node)
17674 return t;
17675
17676 purpose = TREE_PURPOSE (t);
17677 if (purpose)
17678 purpose = tsubst_copy (purpose, args, complain, in_decl);
17679 value = TREE_VALUE (t);
17680 if (value)
17681 value = tsubst_copy (value, args, complain, in_decl);
17682 chain = TREE_CHAIN (t);
17683 if (chain && chain != void_type_node)
17684 chain = tsubst_copy (chain, args, complain, in_decl);
17685 if (purpose == TREE_PURPOSE (t)
17686 && value == TREE_VALUE (t)
17687 && chain == TREE_CHAIN (t))
17688 return t;
17689 return tree_cons (purpose, value, chain);
17690 }
17691
17692 case RECORD_TYPE:
17693 case UNION_TYPE:
17694 case ENUMERAL_TYPE:
17695 case INTEGER_TYPE:
17696 case TEMPLATE_TYPE_PARM:
17697 case TEMPLATE_TEMPLATE_PARM:
17698 case BOUND_TEMPLATE_TEMPLATE_PARM:
17699 case TEMPLATE_PARM_INDEX:
17700 case POINTER_TYPE:
17701 case REFERENCE_TYPE:
17702 case OFFSET_TYPE:
17703 case FUNCTION_TYPE:
17704 case METHOD_TYPE:
17705 case ARRAY_TYPE:
17706 case TYPENAME_TYPE:
17707 case UNBOUND_CLASS_TEMPLATE:
17708 case TYPEOF_TYPE:
17709 case DECLTYPE_TYPE:
17710 case TYPE_DECL:
17711 return tsubst (t, args, complain, in_decl);
17712
17713 case USING_DECL:
17714 t = DECL_NAME (t);
17715 /* Fall through. */
17716 case IDENTIFIER_NODE:
17717 if (IDENTIFIER_CONV_OP_P (t))
17718 {
17719 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17720 return make_conv_op_name (new_type);
17721 }
17722 else
17723 return t;
17724
17725 case CONSTRUCTOR:
17726 /* This is handled by tsubst_copy_and_build. */
17727 gcc_unreachable ();
17728
17729 case VA_ARG_EXPR:
17730 {
17731 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17732 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17733 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17734 }
17735
17736 case CLEANUP_POINT_EXPR:
17737 /* We shouldn't have built any of these during initial template
17738 generation. Instead, they should be built during instantiation
17739 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17740 gcc_unreachable ();
17741
17742 case OFFSET_REF:
17743 {
17744 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17745 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17746 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17747 r = build2 (code, type, op0, op1);
17748 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17749 if (!mark_used (TREE_OPERAND (r, 1), complain)
17750 && !(complain & tf_error))
17751 return error_mark_node;
17752 return r;
17753 }
17754
17755 case EXPR_PACK_EXPANSION:
17756 error ("invalid use of pack expansion expression");
17757 return error_mark_node;
17758
17759 case NONTYPE_ARGUMENT_PACK:
17760 error ("use %<...%> to expand argument pack");
17761 return error_mark_node;
17762
17763 case VOID_CST:
17764 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17765 return t;
17766
17767 case INTEGER_CST:
17768 case REAL_CST:
17769 case COMPLEX_CST:
17770 case VECTOR_CST:
17771 {
17772 /* Instantiate any typedefs in the type. */
17773 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17774 r = fold_convert (type, t);
17775 gcc_assert (TREE_CODE (r) == code);
17776 return r;
17777 }
17778
17779 case STRING_CST:
17780 {
17781 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17782 r = t;
17783 if (type != TREE_TYPE (t))
17784 {
17785 r = copy_node (t);
17786 TREE_TYPE (r) = type;
17787 }
17788 return r;
17789 }
17790
17791 case PTRMEM_CST:
17792 /* These can sometimes show up in a partial instantiation, but never
17793 involve template parms. */
17794 gcc_assert (!uses_template_parms (t));
17795 return t;
17796
17797 case UNARY_LEFT_FOLD_EXPR:
17798 return tsubst_unary_left_fold (t, args, complain, in_decl);
17799 case UNARY_RIGHT_FOLD_EXPR:
17800 return tsubst_unary_right_fold (t, args, complain, in_decl);
17801 case BINARY_LEFT_FOLD_EXPR:
17802 return tsubst_binary_left_fold (t, args, complain, in_decl);
17803 case BINARY_RIGHT_FOLD_EXPR:
17804 return tsubst_binary_right_fold (t, args, complain, in_decl);
17805 case PREDICT_EXPR:
17806 return t;
17807
17808 case DEBUG_BEGIN_STMT:
17809 /* ??? There's no point in copying it for now, but maybe some
17810 day it will contain more information, such as a pointer back
17811 to the containing function, inlined copy or so. */
17812 return t;
17813
17814 case CO_AWAIT_EXPR:
17815 return tsubst_expr (t, args, complain, in_decl);
17816
17817 default:
17818 /* We shouldn't get here, but keep going if !flag_checking. */
17819 if (flag_checking)
17820 gcc_unreachable ();
17821 return t;
17822 }
17823 }
17824
17825 /* Helper function for tsubst_omp_clauses, used for instantiation of
17826 OMP_CLAUSE_DECL of clauses. */
17827
17828 static tree
17829 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17830 tree in_decl, tree *iterator_cache)
17831 {
17832 if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
17833 return decl;
17834
17835 /* Handle OpenMP iterators. */
17836 if (TREE_CODE (decl) == TREE_LIST
17837 && TREE_PURPOSE (decl)
17838 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17839 {
17840 tree ret;
17841 if (iterator_cache[0] == TREE_PURPOSE (decl))
17842 ret = iterator_cache[1];
17843 else
17844 {
17845 tree *tp = &ret;
17846 begin_scope (sk_omp, NULL);
17847 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17848 {
17849 *tp = copy_node (it);
17850 TREE_VEC_ELT (*tp, 0)
17851 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17852 DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
17853 pushdecl (TREE_VEC_ELT (*tp, 0));
17854 TREE_VEC_ELT (*tp, 1)
17855 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl);
17856 TREE_VEC_ELT (*tp, 2)
17857 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl);
17858 TREE_VEC_ELT (*tp, 3)
17859 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl);
17860 TREE_CHAIN (*tp) = NULL_TREE;
17861 tp = &TREE_CHAIN (*tp);
17862 }
17863 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17864 iterator_cache[0] = TREE_PURPOSE (decl);
17865 iterator_cache[1] = ret;
17866 }
17867 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17868 args, complain,
17869 in_decl, NULL));
17870 }
17871
17872 /* Handle an OpenMP array section represented as a TREE_LIST (or
17873 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend
17874 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
17875 TREE_LIST. We can handle it exactly the same as an array section
17876 (purpose, value, and a chain), even though the nomenclature
17877 (low_bound, length, etc) is different. */
17878 if (TREE_CODE (decl) == TREE_LIST)
17879 {
17880 tree low_bound
17881 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl);
17882 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl);
17883 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17884 in_decl, NULL);
17885 if (TREE_PURPOSE (decl) == low_bound
17886 && TREE_VALUE (decl) == length
17887 && TREE_CHAIN (decl) == chain)
17888 return decl;
17889 tree ret = tree_cons (low_bound, length, chain);
17890 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
17891 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
17892 return ret;
17893 }
17894 tree ret = tsubst_expr (decl, args, complain, in_decl);
17895 /* Undo convert_from_reference tsubst_expr could have called. */
17896 if (decl
17897 && REFERENCE_REF_P (ret)
17898 && !REFERENCE_REF_P (decl))
17899 ret = TREE_OPERAND (ret, 0);
17900 return ret;
17901 }
17902
17903 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17904
17905 static tree
17906 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17907 tree args, tsubst_flags_t complain, tree in_decl)
17908 {
17909 tree new_clauses = NULL_TREE, nc, oc;
17910 tree linear_no_step = NULL_TREE;
17911 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17912
17913 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17914 {
17915 nc = copy_node (oc);
17916 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17917 new_clauses = nc;
17918
17919 switch (OMP_CLAUSE_CODE (nc))
17920 {
17921 case OMP_CLAUSE_LASTPRIVATE:
17922 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17923 {
17924 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17925 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
17926 complain, in_decl);
17927 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17928 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17929 }
17930 /* FALLTHRU */
17931 case OMP_CLAUSE_PRIVATE:
17932 case OMP_CLAUSE_SHARED:
17933 case OMP_CLAUSE_FIRSTPRIVATE:
17934 case OMP_CLAUSE_COPYIN:
17935 case OMP_CLAUSE_COPYPRIVATE:
17936 case OMP_CLAUSE_UNIFORM:
17937 case OMP_CLAUSE_DEPEND:
17938 case OMP_CLAUSE_DOACROSS:
17939 case OMP_CLAUSE_AFFINITY:
17940 case OMP_CLAUSE_FROM:
17941 case OMP_CLAUSE_TO:
17942 case OMP_CLAUSE_MAP:
17943 case OMP_CLAUSE__CACHE_:
17944 case OMP_CLAUSE_NONTEMPORAL:
17945 case OMP_CLAUSE_USE_DEVICE_PTR:
17946 case OMP_CLAUSE_USE_DEVICE_ADDR:
17947 case OMP_CLAUSE_IS_DEVICE_PTR:
17948 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17949 case OMP_CLAUSE_INCLUSIVE:
17950 case OMP_CLAUSE_EXCLUSIVE:
17951 OMP_CLAUSE_DECL (nc)
17952 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17953 in_decl, iterator_cache);
17954 break;
17955 case OMP_CLAUSE_NUM_TEAMS:
17956 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
17957 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
17958 = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
17959 complain, in_decl);
17960 /* FALLTHRU */
17961 case OMP_CLAUSE_TILE:
17962 case OMP_CLAUSE_IF:
17963 case OMP_CLAUSE_NUM_THREADS:
17964 case OMP_CLAUSE_SCHEDULE:
17965 case OMP_CLAUSE_COLLAPSE:
17966 case OMP_CLAUSE_FINAL:
17967 case OMP_CLAUSE_DEVICE:
17968 case OMP_CLAUSE_DIST_SCHEDULE:
17969 case OMP_CLAUSE_THREAD_LIMIT:
17970 case OMP_CLAUSE_SAFELEN:
17971 case OMP_CLAUSE_SIMDLEN:
17972 case OMP_CLAUSE_NUM_TASKS:
17973 case OMP_CLAUSE_GRAINSIZE:
17974 case OMP_CLAUSE_PRIORITY:
17975 case OMP_CLAUSE_ORDERED:
17976 case OMP_CLAUSE_HINT:
17977 case OMP_CLAUSE_FILTER:
17978 case OMP_CLAUSE_NUM_GANGS:
17979 case OMP_CLAUSE_NUM_WORKERS:
17980 case OMP_CLAUSE_VECTOR_LENGTH:
17981 case OMP_CLAUSE_WORKER:
17982 case OMP_CLAUSE_VECTOR:
17983 case OMP_CLAUSE_ASYNC:
17984 case OMP_CLAUSE_WAIT:
17985 case OMP_CLAUSE_DETACH:
17986 OMP_CLAUSE_OPERAND (nc, 0)
17987 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
17988 break;
17989 case OMP_CLAUSE_REDUCTION:
17990 case OMP_CLAUSE_IN_REDUCTION:
17991 case OMP_CLAUSE_TASK_REDUCTION:
17992 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17993 {
17994 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17995 if (TREE_CODE (placeholder) == SCOPE_REF)
17996 {
17997 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17998 complain, in_decl);
17999 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
18000 = build_qualified_name (NULL_TREE, scope,
18001 TREE_OPERAND (placeholder, 1),
18002 false);
18003 }
18004 else
18005 gcc_assert (identifier_p (placeholder));
18006 }
18007 OMP_CLAUSE_DECL (nc)
18008 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18009 in_decl, NULL);
18010 break;
18011 case OMP_CLAUSE_GANG:
18012 case OMP_CLAUSE_ALIGNED:
18013 OMP_CLAUSE_DECL (nc)
18014 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18015 in_decl, NULL);
18016 OMP_CLAUSE_OPERAND (nc, 1)
18017 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
18018 break;
18019 case OMP_CLAUSE_ALLOCATE:
18020 OMP_CLAUSE_DECL (nc)
18021 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18022 in_decl, NULL);
18023 OMP_CLAUSE_OPERAND (nc, 1)
18024 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
18025 OMP_CLAUSE_OPERAND (nc, 2)
18026 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
18027 break;
18028 case OMP_CLAUSE_LINEAR:
18029 OMP_CLAUSE_DECL (nc)
18030 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
18031 in_decl, NULL);
18032 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
18033 {
18034 gcc_assert (!linear_no_step);
18035 linear_no_step = nc;
18036 }
18037 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
18038 OMP_CLAUSE_LINEAR_STEP (nc)
18039 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
18040 complain, in_decl, NULL);
18041 else
18042 OMP_CLAUSE_LINEAR_STEP (nc)
18043 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args,
18044 complain, in_decl);
18045 break;
18046 case OMP_CLAUSE_NOWAIT:
18047 case OMP_CLAUSE_DEFAULT:
18048 case OMP_CLAUSE_UNTIED:
18049 case OMP_CLAUSE_MERGEABLE:
18050 case OMP_CLAUSE_INBRANCH:
18051 case OMP_CLAUSE_NOTINBRANCH:
18052 case OMP_CLAUSE_PROC_BIND:
18053 case OMP_CLAUSE_FOR:
18054 case OMP_CLAUSE_PARALLEL:
18055 case OMP_CLAUSE_SECTIONS:
18056 case OMP_CLAUSE_TASKGROUP:
18057 case OMP_CLAUSE_NOGROUP:
18058 case OMP_CLAUSE_THREADS:
18059 case OMP_CLAUSE_SIMD:
18060 case OMP_CLAUSE_DEFAULTMAP:
18061 case OMP_CLAUSE_ORDER:
18062 case OMP_CLAUSE_BIND:
18063 case OMP_CLAUSE_INDEPENDENT:
18064 case OMP_CLAUSE_AUTO:
18065 case OMP_CLAUSE_SEQ:
18066 case OMP_CLAUSE_IF_PRESENT:
18067 case OMP_CLAUSE_FINALIZE:
18068 case OMP_CLAUSE_NOHOST:
18069 break;
18070 default:
18071 gcc_unreachable ();
18072 }
18073 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
18074 switch (OMP_CLAUSE_CODE (nc))
18075 {
18076 case OMP_CLAUSE_SHARED:
18077 case OMP_CLAUSE_PRIVATE:
18078 case OMP_CLAUSE_FIRSTPRIVATE:
18079 case OMP_CLAUSE_LASTPRIVATE:
18080 case OMP_CLAUSE_COPYPRIVATE:
18081 case OMP_CLAUSE_LINEAR:
18082 case OMP_CLAUSE_REDUCTION:
18083 case OMP_CLAUSE_IN_REDUCTION:
18084 case OMP_CLAUSE_TASK_REDUCTION:
18085 case OMP_CLAUSE_USE_DEVICE_PTR:
18086 case OMP_CLAUSE_USE_DEVICE_ADDR:
18087 case OMP_CLAUSE_IS_DEVICE_PTR:
18088 case OMP_CLAUSE_HAS_DEVICE_ADDR:
18089 case OMP_CLAUSE_INCLUSIVE:
18090 case OMP_CLAUSE_EXCLUSIVE:
18091 case OMP_CLAUSE_ALLOCATE:
18092 /* tsubst_expr on SCOPE_REF results in returning
18093 finish_non_static_data_member result. Undo that here. */
18094 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
18095 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
18096 == IDENTIFIER_NODE))
18097 {
18098 tree t = OMP_CLAUSE_DECL (nc);
18099 tree v = t;
18100 while (v)
18101 switch (TREE_CODE (v))
18102 {
18103 case COMPONENT_REF:
18104 case MEM_REF:
18105 case INDIRECT_REF:
18106 CASE_CONVERT:
18107 case POINTER_PLUS_EXPR:
18108 v = TREE_OPERAND (v, 0);
18109 continue;
18110 case PARM_DECL:
18111 if (DECL_CONTEXT (v) == current_function_decl
18112 && DECL_ARTIFICIAL (v)
18113 && DECL_NAME (v) == this_identifier)
18114 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
18115 /* FALLTHRU */
18116 default:
18117 v = NULL_TREE;
18118 break;
18119 }
18120 }
18121 else if (VAR_P (OMP_CLAUSE_DECL (oc))
18122 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
18123 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
18124 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
18125 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
18126 {
18127 tree decl = OMP_CLAUSE_DECL (nc);
18128 if (VAR_P (decl))
18129 {
18130 retrofit_lang_decl (decl);
18131 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
18132 }
18133 }
18134 break;
18135 default:
18136 break;
18137 }
18138 }
18139
18140 new_clauses = nreverse (new_clauses);
18141 if (ort != C_ORT_OMP_DECLARE_SIMD)
18142 {
18143 new_clauses = finish_omp_clauses (new_clauses, ort);
18144 if (linear_no_step)
18145 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
18146 if (nc == linear_no_step)
18147 {
18148 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
18149 break;
18150 }
18151 }
18152 return new_clauses;
18153 }
18154
18155 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
18156
18157 static tree
18158 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
18159 tree in_decl)
18160 {
18161 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
18162
18163 tree purpose, value, chain;
18164
18165 if (t == NULL)
18166 return t;
18167
18168 if (TREE_CODE (t) != TREE_LIST)
18169 return tsubst_copy_and_build (t, args, complain, in_decl);
18170
18171 if (t == void_list_node)
18172 return t;
18173
18174 purpose = TREE_PURPOSE (t);
18175 if (purpose)
18176 purpose = RECUR (purpose);
18177 value = TREE_VALUE (t);
18178 if (value)
18179 {
18180 if (TREE_CODE (value) != LABEL_DECL)
18181 value = RECUR (value);
18182 else
18183 {
18184 value = lookup_label (DECL_NAME (value));
18185 gcc_assert (TREE_CODE (value) == LABEL_DECL);
18186 TREE_USED (value) = 1;
18187 }
18188 }
18189 chain = TREE_CHAIN (t);
18190 if (chain && chain != void_type_node)
18191 chain = RECUR (chain);
18192 return tree_cons (purpose, value, chain);
18193 #undef RECUR
18194 }
18195
18196 /* Used to temporarily communicate the list of #pragma omp parallel
18197 clauses to #pragma omp for instantiation if they are combined
18198 together. */
18199
18200 static tree *omp_parallel_combined_clauses;
18201
18202 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
18203 tree *, unsigned int *);
18204
18205 /* Substitute one OMP_FOR iterator. */
18206
18207 static bool
18208 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
18209 tree initv, tree condv, tree incrv, tree *clauses,
18210 tree args, tsubst_flags_t complain, tree in_decl)
18211 {
18212 #define RECUR(NODE) \
18213 tsubst_expr ((NODE), args, complain, in_decl)
18214 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
18215 bool ret = false;
18216
18217 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
18218 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
18219
18220 decl = TREE_OPERAND (init, 0);
18221 init = TREE_OPERAND (init, 1);
18222 tree decl_expr = NULL_TREE;
18223 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
18224 if (range_for)
18225 {
18226 bool decomp = false;
18227 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
18228 {
18229 tree v = DECL_VALUE_EXPR (decl);
18230 if (TREE_CODE (v) == ARRAY_REF
18231 && VAR_P (TREE_OPERAND (v, 0))
18232 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
18233 {
18234 tree decomp_first = NULL_TREE;
18235 unsigned decomp_cnt = 0;
18236 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
18237 maybe_push_decl (d);
18238 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
18239 in_decl, &decomp_first, &decomp_cnt);
18240 decomp = true;
18241 if (d == error_mark_node)
18242 decl = error_mark_node;
18243 else
18244 for (unsigned int i = 0; i < decomp_cnt; i++)
18245 {
18246 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
18247 {
18248 tree v = build_nt (ARRAY_REF, d,
18249 size_int (decomp_cnt - i - 1),
18250 NULL_TREE, NULL_TREE);
18251 SET_DECL_VALUE_EXPR (decomp_first, v);
18252 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
18253 }
18254 fit_decomposition_lang_decl (decomp_first, d);
18255 decomp_first = DECL_CHAIN (decomp_first);
18256 }
18257 }
18258 }
18259 decl = tsubst_decl (decl, args, complain);
18260 if (!decomp)
18261 maybe_push_decl (decl);
18262 }
18263 else if (init && TREE_CODE (init) == DECL_EXPR)
18264 {
18265 /* We need to jump through some hoops to handle declarations in the
18266 init-statement, since we might need to handle auto deduction,
18267 but we need to keep control of initialization. */
18268 decl_expr = init;
18269 init = DECL_INITIAL (DECL_EXPR_DECL (init));
18270 decl = tsubst_decl (decl, args, complain);
18271 }
18272 else
18273 {
18274 if (TREE_CODE (decl) == SCOPE_REF)
18275 {
18276 decl = RECUR (decl);
18277 if (TREE_CODE (decl) == COMPONENT_REF)
18278 {
18279 tree v = decl;
18280 while (v)
18281 switch (TREE_CODE (v))
18282 {
18283 case COMPONENT_REF:
18284 case MEM_REF:
18285 case INDIRECT_REF:
18286 CASE_CONVERT:
18287 case POINTER_PLUS_EXPR:
18288 v = TREE_OPERAND (v, 0);
18289 continue;
18290 case PARM_DECL:
18291 if (DECL_CONTEXT (v) == current_function_decl
18292 && DECL_ARTIFICIAL (v)
18293 && DECL_NAME (v) == this_identifier)
18294 {
18295 decl = TREE_OPERAND (decl, 1);
18296 decl = omp_privatize_field (decl, false);
18297 }
18298 /* FALLTHRU */
18299 default:
18300 v = NULL_TREE;
18301 break;
18302 }
18303 }
18304 }
18305 else
18306 decl = RECUR (decl);
18307 }
18308 if (init && TREE_CODE (init) == TREE_VEC)
18309 {
18310 init = copy_node (init);
18311 TREE_VEC_ELT (init, 0)
18312 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
18313 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
18314 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
18315 }
18316 else
18317 init = RECUR (init);
18318
18319 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
18320 {
18321 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
18322 if (TREE_CODE (o) == TREE_LIST)
18323 TREE_VEC_ELT (orig_declv, i)
18324 = tree_cons (RECUR (TREE_PURPOSE (o)),
18325 RECUR (TREE_VALUE (o)),
18326 NULL_TREE);
18327 else
18328 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
18329 }
18330
18331 if (range_for)
18332 {
18333 tree this_pre_body = NULL_TREE;
18334 tree orig_init = NULL_TREE;
18335 tree orig_decl = NULL_TREE;
18336 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
18337 orig_init, cond, incr);
18338 if (orig_decl)
18339 {
18340 if (orig_declv == NULL_TREE)
18341 orig_declv = copy_node (declv);
18342 TREE_VEC_ELT (orig_declv, i) = orig_decl;
18343 ret = true;
18344 }
18345 else if (orig_declv)
18346 TREE_VEC_ELT (orig_declv, i) = decl;
18347 }
18348
18349 tree auto_node = type_uses_auto (TREE_TYPE (decl));
18350 if (!range_for && auto_node && init)
18351 TREE_TYPE (decl)
18352 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
18353
18354 gcc_assert (!type_dependent_expression_p (decl));
18355
18356 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
18357 {
18358 if (decl_expr)
18359 {
18360 /* Declare the variable, but don't let that initialize it. */
18361 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
18362 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
18363 RECUR (decl_expr);
18364 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
18365 }
18366
18367 if (!range_for)
18368 {
18369 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18370 if (COMPARISON_CLASS_P (cond)
18371 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
18372 {
18373 tree lhs = RECUR (TREE_OPERAND (cond, 0));
18374 tree rhs = copy_node (TREE_OPERAND (cond, 1));
18375 TREE_VEC_ELT (rhs, 0)
18376 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
18377 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
18378 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
18379 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
18380 lhs, rhs);
18381 }
18382 else
18383 cond = RECUR (cond);
18384 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18385 if (TREE_CODE (incr) == MODIFY_EXPR)
18386 {
18387 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18388 tree rhs = RECUR (TREE_OPERAND (incr, 1));
18389 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
18390 NOP_EXPR, rhs, NULL_TREE, complain);
18391 }
18392 else
18393 incr = RECUR (incr);
18394 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18395 TREE_VEC_ELT (orig_declv, i) = decl;
18396 }
18397 TREE_VEC_ELT (declv, i) = decl;
18398 TREE_VEC_ELT (initv, i) = init;
18399 TREE_VEC_ELT (condv, i) = cond;
18400 TREE_VEC_ELT (incrv, i) = incr;
18401 return ret;
18402 }
18403
18404 if (decl_expr)
18405 {
18406 /* Declare and initialize the variable. */
18407 RECUR (decl_expr);
18408 init = NULL_TREE;
18409 }
18410 else if (init)
18411 {
18412 tree *pc;
18413 int j;
18414 for (j = ((omp_parallel_combined_clauses == NULL
18415 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
18416 {
18417 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
18418 {
18419 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
18420 && OMP_CLAUSE_DECL (*pc) == decl)
18421 break;
18422 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
18423 && OMP_CLAUSE_DECL (*pc) == decl)
18424 {
18425 if (j)
18426 break;
18427 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18428 tree c = *pc;
18429 *pc = OMP_CLAUSE_CHAIN (c);
18430 OMP_CLAUSE_CHAIN (c) = *clauses;
18431 *clauses = c;
18432 }
18433 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
18434 && OMP_CLAUSE_DECL (*pc) == decl)
18435 {
18436 error ("iteration variable %qD should not be firstprivate",
18437 decl);
18438 *pc = OMP_CLAUSE_CHAIN (*pc);
18439 }
18440 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
18441 && OMP_CLAUSE_DECL (*pc) == decl)
18442 {
18443 error ("iteration variable %qD should not be reduction",
18444 decl);
18445 *pc = OMP_CLAUSE_CHAIN (*pc);
18446 }
18447 else
18448 pc = &OMP_CLAUSE_CHAIN (*pc);
18449 }
18450 if (*pc)
18451 break;
18452 }
18453 if (*pc == NULL_TREE)
18454 {
18455 tree c = build_omp_clause (input_location,
18456 TREE_CODE (t) == OMP_LOOP
18457 ? OMP_CLAUSE_LASTPRIVATE
18458 : OMP_CLAUSE_PRIVATE);
18459 OMP_CLAUSE_DECL (c) = decl;
18460 c = finish_omp_clauses (c, C_ORT_OMP);
18461 if (c)
18462 {
18463 OMP_CLAUSE_CHAIN (c) = *clauses;
18464 *clauses = c;
18465 }
18466 }
18467 }
18468 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
18469 if (COMPARISON_CLASS_P (cond))
18470 {
18471 tree op0 = RECUR (TREE_OPERAND (cond, 0));
18472 tree op1 = RECUR (TREE_OPERAND (cond, 1));
18473 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
18474 }
18475 else
18476 cond = RECUR (cond);
18477 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
18478 switch (TREE_CODE (incr))
18479 {
18480 case PREINCREMENT_EXPR:
18481 case PREDECREMENT_EXPR:
18482 case POSTINCREMENT_EXPR:
18483 case POSTDECREMENT_EXPR:
18484 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
18485 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
18486 break;
18487 case MODIFY_EXPR:
18488 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18489 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18490 {
18491 tree rhs = TREE_OPERAND (incr, 1);
18492 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18493 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18494 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18495 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18496 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18497 rhs0, rhs1));
18498 }
18499 else
18500 incr = RECUR (incr);
18501 break;
18502 case MODOP_EXPR:
18503 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
18504 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
18505 {
18506 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18507 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18508 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
18509 TREE_TYPE (decl), lhs,
18510 RECUR (TREE_OPERAND (incr, 2))));
18511 }
18512 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
18513 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
18514 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
18515 {
18516 tree rhs = TREE_OPERAND (incr, 2);
18517 tree lhs = RECUR (TREE_OPERAND (incr, 0));
18518 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
18519 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
18520 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
18521 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
18522 rhs0, rhs1));
18523 }
18524 else
18525 incr = RECUR (incr);
18526 break;
18527 default:
18528 incr = RECUR (incr);
18529 break;
18530 }
18531
18532 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
18533 TREE_VEC_ELT (orig_declv, i) = decl;
18534 TREE_VEC_ELT (declv, i) = decl;
18535 TREE_VEC_ELT (initv, i) = init;
18536 TREE_VEC_ELT (condv, i) = cond;
18537 TREE_VEC_ELT (incrv, i) = incr;
18538 return false;
18539 #undef RECUR
18540 }
18541
18542 /* Helper function of tsubst_expr, find OMP_TEAMS inside
18543 of OMP_TARGET's body. */
18544
18545 static tree
18546 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
18547 {
18548 *walk_subtrees = 0;
18549 switch (TREE_CODE (*tp))
18550 {
18551 case OMP_TEAMS:
18552 return *tp;
18553 case BIND_EXPR:
18554 case STATEMENT_LIST:
18555 *walk_subtrees = 1;
18556 break;
18557 default:
18558 break;
18559 }
18560 return NULL_TREE;
18561 }
18562
18563 /* Helper function for tsubst_expr. For decomposition declaration
18564 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18565 also the corresponding decls representing the identifiers
18566 of the decomposition declaration. Return DECL if successful
18567 or error_mark_node otherwise, set *FIRST to the first decl
18568 in the list chained through DECL_CHAIN and *CNT to the number
18569 of such decls. */
18570
18571 static tree
18572 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18573 tsubst_flags_t complain, tree in_decl, tree *first,
18574 unsigned int *cnt)
18575 {
18576 tree decl2, decl3, prev = decl;
18577 *cnt = 0;
18578 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18579 for (decl2 = DECL_CHAIN (pattern_decl);
18580 decl2
18581 && VAR_P (decl2)
18582 && DECL_DECOMPOSITION_P (decl2)
18583 && DECL_NAME (decl2);
18584 decl2 = DECL_CHAIN (decl2))
18585 {
18586 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
18587 {
18588 gcc_assert (errorcount);
18589 return error_mark_node;
18590 }
18591 (*cnt)++;
18592 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18593 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18594 tree v = DECL_VALUE_EXPR (decl2);
18595 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18596 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18597 decl3 = tsubst (decl2, args, complain, in_decl);
18598 SET_DECL_VALUE_EXPR (decl2, v);
18599 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18600 if (VAR_P (decl3))
18601 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18602 else
18603 {
18604 gcc_assert (errorcount);
18605 decl = error_mark_node;
18606 continue;
18607 }
18608 maybe_push_decl (decl3);
18609 if (error_operand_p (decl3))
18610 decl = error_mark_node;
18611 else if (decl != error_mark_node
18612 && DECL_CHAIN (decl3) != prev
18613 && decl != prev)
18614 {
18615 gcc_assert (errorcount);
18616 decl = error_mark_node;
18617 }
18618 else
18619 prev = decl3;
18620 }
18621 *first = prev;
18622 return decl;
18623 }
18624
18625 /* Return the proper local_specialization for init-capture pack DECL. */
18626
18627 static tree
18628 lookup_init_capture_pack (tree decl)
18629 {
18630 /* We handle normal pack captures by forwarding to the specialization of the
18631 captured parameter. We can't do that for pack init-captures; we need them
18632 to have their own local_specialization. We created the individual
18633 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18634 when we process the DECL_EXPR for the pack init-capture in the template.
18635 So, how do we find them? We don't know the capture proxy pack when
18636 building the individual resulting proxies, and we don't know the
18637 individual proxies when instantiating the pack. What we have in common is
18638 the FIELD_DECL.
18639
18640 So...when we instantiate the FIELD_DECL, we stick the result in
18641 local_specializations. Then at the DECL_EXPR we look up that result, see
18642 how many elements it has, synthesize the names, and look them up. */
18643
18644 tree cname = DECL_NAME (decl);
18645 tree val = DECL_VALUE_EXPR (decl);
18646 tree field = TREE_OPERAND (val, 1);
18647 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18648 tree fpack = retrieve_local_specialization (field);
18649 if (fpack == error_mark_node)
18650 return error_mark_node;
18651
18652 int len = 1;
18653 tree vec = NULL_TREE;
18654 tree r = NULL_TREE;
18655 if (TREE_CODE (fpack) == TREE_VEC)
18656 {
18657 len = TREE_VEC_LENGTH (fpack);
18658 vec = make_tree_vec (len);
18659 r = make_node (NONTYPE_ARGUMENT_PACK);
18660 ARGUMENT_PACK_ARGS (r) = vec;
18661 }
18662 for (int i = 0; i < len; ++i)
18663 {
18664 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18665 tree elt = lookup_name (ename);
18666 if (vec)
18667 TREE_VEC_ELT (vec, i) = elt;
18668 else
18669 r = elt;
18670 }
18671 return r;
18672 }
18673
18674 /* T is an operand of a template tree being substituted. Return whether
18675 T is dependent such that we should suppress some warnings that would
18676 make sense if the substituted expression were written directly, like
18677 template <int I> bool f() { return I == 2; }
18678 We don't want to warn when instantiating f that comparing two constants
18679 always has the same value.
18680
18681 This is a more limited concept of dependence than instantiation-dependent;
18682 here we don't care whether substitution could fail. */
18683
18684 static bool
18685 dependent_operand_p (tree t)
18686 {
18687 while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
18688 t = TREE_OPERAND (t, 0);
18689 ++processing_template_decl;
18690 bool r = (potential_constant_expression (t)
18691 ? value_dependent_expression_p (t)
18692 : type_dependent_expression_p (t));
18693 --processing_template_decl;
18694 return r;
18695 }
18696
18697 /* Like tsubst_copy for expressions, etc. but also does semantic
18698 processing. */
18699
18700 tree
18701 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18702 {
18703 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18704 #define RECUR(NODE) \
18705 tsubst_expr ((NODE), args, complain, in_decl)
18706
18707 tree stmt, tmp;
18708 tree r;
18709 location_t loc;
18710
18711 if (t == NULL_TREE || t == error_mark_node)
18712 return t;
18713
18714 loc = input_location;
18715 if (location_t eloc = cp_expr_location (t))
18716 input_location = eloc;
18717 if (STATEMENT_CODE_P (TREE_CODE (t)))
18718 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18719
18720 switch (TREE_CODE (t))
18721 {
18722 case STATEMENT_LIST:
18723 {
18724 for (tree stmt : tsi_range (t))
18725 RECUR (stmt);
18726 break;
18727 }
18728
18729 case CTOR_INITIALIZER:
18730 finish_mem_initializers (tsubst_initializer_list
18731 (TREE_OPERAND (t, 0), args));
18732 break;
18733
18734 case RETURN_EXPR:
18735 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18736 break;
18737
18738 case CO_RETURN_EXPR:
18739 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18740 break;
18741
18742 case CO_YIELD_EXPR:
18743 stmt = finish_co_yield_expr (input_location,
18744 RECUR (TREE_OPERAND (t, 0)));
18745 RETURN (stmt);
18746
18747 case CO_AWAIT_EXPR:
18748 stmt = finish_co_await_expr (input_location,
18749 RECUR (TREE_OPERAND (t, 0)));
18750 RETURN (stmt);
18751
18752 case EXPR_STMT:
18753 tmp = RECUR (EXPR_STMT_EXPR (t));
18754 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18755 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18756 else
18757 finish_expr_stmt (tmp);
18758 break;
18759
18760 case USING_STMT:
18761 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18762 break;
18763
18764 case PRECONDITION_STMT:
18765 case POSTCONDITION_STMT:
18766 gcc_unreachable ();
18767
18768 case ASSERTION_STMT:
18769 {
18770 r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
18771 if (r != error_mark_node)
18772 add_stmt (r);
18773 RETURN (r);
18774 }
18775 break;
18776
18777 case DECL_EXPR:
18778 {
18779 tree decl, pattern_decl;
18780 tree init;
18781
18782 pattern_decl = decl = DECL_EXPR_DECL (t);
18783 if (TREE_CODE (decl) == LABEL_DECL)
18784 finish_label_decl (DECL_NAME (decl));
18785 else if (TREE_CODE (decl) == USING_DECL)
18786 {
18787 tree scope = USING_DECL_SCOPE (decl);
18788 if (DECL_DEPENDENT_P (decl))
18789 {
18790 scope = tsubst (scope, args, complain, in_decl);
18791 if (!MAYBE_CLASS_TYPE_P (scope)
18792 && TREE_CODE (scope) != ENUMERAL_TYPE)
18793 {
18794 if (complain & tf_error)
18795 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18796 "class, namespace, or enumeration", scope);
18797 return error_mark_node;
18798 }
18799 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18800 }
18801 else
18802 {
18803 /* This is a non-dependent using-decl, and we'll have
18804 used the names it found during template parsing. We do
18805 not want to do the lookup again, because we might not
18806 find the things we found then. */
18807 gcc_checking_assert (scope == tsubst (scope, args,
18808 complain, in_decl));
18809 /* We still need to push the bindings so that we can look up
18810 this name later. */
18811 push_using_decl_bindings (DECL_NAME (decl),
18812 USING_DECL_DECLS (decl));
18813 }
18814 }
18815 else if (is_capture_proxy (decl)
18816 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18817 {
18818 /* We're in tsubst_lambda_expr, we've already inserted a new
18819 capture proxy, so look it up and register it. */
18820 tree inst;
18821 if (!DECL_PACK_P (decl))
18822 {
18823 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18824 LOOK_want::HIDDEN_LAMBDA);
18825 gcc_assert (inst != decl && is_capture_proxy (inst));
18826 }
18827 else if (is_normal_capture_proxy (decl))
18828 {
18829 inst = (retrieve_local_specialization
18830 (DECL_CAPTURED_VARIABLE (decl)));
18831 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18832 || DECL_PACK_P (inst));
18833 }
18834 else
18835 inst = lookup_init_capture_pack (decl);
18836
18837 register_local_specialization (inst, decl);
18838 break;
18839 }
18840 else if (DECL_PRETTY_FUNCTION_P (decl))
18841 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18842 DECL_NAME (decl),
18843 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18844 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18845 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18846 /* Don't copy the old closure; we'll create a new one in
18847 tsubst_lambda_expr. */
18848 break;
18849 else
18850 {
18851 init = DECL_INITIAL (decl);
18852 decl = tsubst (decl, args, complain, in_decl);
18853 if (decl != error_mark_node)
18854 {
18855 /* By marking the declaration as instantiated, we avoid
18856 trying to instantiate it. Since instantiate_decl can't
18857 handle local variables, and since we've already done
18858 all that needs to be done, that's the right thing to
18859 do. */
18860 if (VAR_P (decl))
18861 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18862 if (VAR_P (decl) && !DECL_NAME (decl)
18863 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18864 /* Anonymous aggregates are a special case. */
18865 finish_anon_union (decl);
18866 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18867 {
18868 DECL_CONTEXT (decl) = current_function_decl;
18869 if (DECL_NAME (decl) == this_identifier)
18870 {
18871 tree lam = DECL_CONTEXT (current_function_decl);
18872 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18873 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18874 }
18875 insert_capture_proxy (decl);
18876 }
18877 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18878 /* We already did a pushtag. */;
18879 else if (VAR_OR_FUNCTION_DECL_P (decl)
18880 && DECL_LOCAL_DECL_P (decl))
18881 {
18882 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18883 DECL_CONTEXT (decl) = NULL_TREE;
18884 decl = pushdecl (decl);
18885 if (TREE_CODE (decl) == FUNCTION_DECL
18886 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18887 && cp_check_omp_declare_reduction (decl))
18888 instantiate_body (pattern_decl, args, decl, true);
18889 }
18890 else
18891 {
18892 bool const_init = false;
18893 unsigned int cnt = 0;
18894 tree first = NULL_TREE, ndecl = error_mark_node;
18895 tree asmspec_tree = NULL_TREE;
18896 maybe_push_decl (decl);
18897
18898 if (VAR_P (decl)
18899 && DECL_LANG_SPECIFIC (decl)
18900 && DECL_OMP_PRIVATIZED_MEMBER (decl))
18901 break;
18902
18903 if (VAR_P (decl)
18904 && DECL_DECOMPOSITION_P (decl)
18905 && TREE_TYPE (pattern_decl) != error_mark_node)
18906 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18907 complain, in_decl, &first,
18908 &cnt);
18909
18910 init = tsubst_init (init, decl, args, complain, in_decl);
18911
18912 if (VAR_P (decl))
18913 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18914 (pattern_decl));
18915
18916 if (ndecl != error_mark_node)
18917 cp_maybe_mangle_decomp (ndecl, first, cnt);
18918
18919 /* In a non-template function, VLA type declarations are
18920 handled in grokdeclarator; for templates, handle them
18921 now. */
18922 predeclare_vla (decl);
18923
18924 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18925 {
18926 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18927 const char *asmspec = IDENTIFIER_POINTER (id);
18928 gcc_assert (asmspec[0] == '*');
18929 asmspec_tree
18930 = build_string (IDENTIFIER_LENGTH (id) - 1,
18931 asmspec + 1);
18932 TREE_TYPE (asmspec_tree) = char_array_type_node;
18933 }
18934
18935 cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
18936
18937 if (ndecl != error_mark_node)
18938 cp_finish_decomp (ndecl, first, cnt);
18939 }
18940 }
18941 }
18942
18943 break;
18944 }
18945
18946 case FOR_STMT:
18947 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18948 RECUR (FOR_INIT_STMT (t));
18949 finish_init_stmt (stmt);
18950 tmp = RECUR (FOR_COND (t));
18951 finish_for_cond (tmp, stmt, false, 0);
18952 tmp = RECUR (FOR_EXPR (t));
18953 finish_for_expr (tmp, stmt);
18954 {
18955 bool prev = note_iteration_stmt_body_start ();
18956 RECUR (FOR_BODY (t));
18957 note_iteration_stmt_body_end (prev);
18958 }
18959 finish_for_stmt (stmt);
18960 break;
18961
18962 case RANGE_FOR_STMT:
18963 {
18964 /* Construct another range_for, if this is not a final
18965 substitution (for inside a generic lambda of a
18966 template). Otherwise convert to a regular for. */
18967 tree decl, expr;
18968 stmt = (processing_template_decl
18969 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18970 : begin_for_stmt (NULL_TREE, NULL_TREE));
18971 RECUR (RANGE_FOR_INIT_STMT (t));
18972 decl = RANGE_FOR_DECL (t);
18973 decl = tsubst (decl, args, complain, in_decl);
18974 maybe_push_decl (decl);
18975 expr = RECUR (RANGE_FOR_EXPR (t));
18976
18977 tree decomp_first = NULL_TREE;
18978 unsigned decomp_cnt = 0;
18979 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18980 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18981 complain, in_decl,
18982 &decomp_first, &decomp_cnt);
18983
18984 if (processing_template_decl)
18985 {
18986 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18987 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18988 finish_range_for_decl (stmt, decl, expr);
18989 if (decomp_first && decl != error_mark_node)
18990 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18991 }
18992 else
18993 {
18994 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18995 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18996 stmt = cp_convert_range_for (stmt, decl, expr,
18997 decomp_first, decomp_cnt,
18998 RANGE_FOR_IVDEP (t), unroll);
18999 }
19000
19001 bool prev = note_iteration_stmt_body_start ();
19002 RECUR (RANGE_FOR_BODY (t));
19003 note_iteration_stmt_body_end (prev);
19004 finish_for_stmt (stmt);
19005 }
19006 break;
19007
19008 case WHILE_STMT:
19009 stmt = begin_while_stmt ();
19010 tmp = RECUR (WHILE_COND (t));
19011 finish_while_stmt_cond (tmp, stmt, false, 0);
19012 {
19013 bool prev = note_iteration_stmt_body_start ();
19014 RECUR (WHILE_BODY (t));
19015 note_iteration_stmt_body_end (prev);
19016 }
19017 finish_while_stmt (stmt);
19018 break;
19019
19020 case DO_STMT:
19021 stmt = begin_do_stmt ();
19022 {
19023 bool prev = note_iteration_stmt_body_start ();
19024 RECUR (DO_BODY (t));
19025 note_iteration_stmt_body_end (prev);
19026 }
19027 finish_do_body (stmt);
19028 tmp = RECUR (DO_COND (t));
19029 finish_do_stmt (tmp, stmt, false, 0);
19030 break;
19031
19032 case IF_STMT:
19033 stmt = begin_if_stmt ();
19034 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
19035 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
19036 if (IF_STMT_CONSTEXPR_P (t))
19037 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
19038 {
19039 tree cond = IF_COND (t);
19040 bool was_dep = dependent_operand_p (cond);
19041 cond = RECUR (cond);
19042 warning_sentinel s1(warn_address, was_dep);
19043 tmp = finish_if_stmt_cond (cond, stmt);
19044 }
19045 if (IF_STMT_CONSTEXPR_P (t)
19046 && instantiation_dependent_expression_p (tmp))
19047 {
19048 /* We're partially instantiating a generic lambda, but the condition
19049 of the constexpr if is still dependent. Don't substitute into the
19050 branches now, just remember the template arguments. */
19051 do_poplevel (IF_SCOPE (stmt));
19052 IF_COND (stmt) = IF_COND (t);
19053 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
19054 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
19055 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
19056 add_stmt (stmt);
19057 break;
19058 }
19059 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
19060 /* Don't instantiate the THEN_CLAUSE. */;
19061 else if (IF_STMT_CONSTEVAL_P (t))
19062 {
19063 bool save_in_consteval_if_p = in_consteval_if_p;
19064 in_consteval_if_p = true;
19065 RECUR (THEN_CLAUSE (t));
19066 in_consteval_if_p = save_in_consteval_if_p;
19067 }
19068 else
19069 {
19070 tree folded = fold_non_dependent_expr (tmp, complain);
19071 bool inhibit = integer_zerop (folded);
19072 if (inhibit)
19073 ++c_inhibit_evaluation_warnings;
19074 RECUR (THEN_CLAUSE (t));
19075 if (inhibit)
19076 --c_inhibit_evaluation_warnings;
19077 }
19078 finish_then_clause (stmt);
19079
19080 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
19081 /* Don't instantiate the ELSE_CLAUSE. */;
19082 else if (ELSE_CLAUSE (t))
19083 {
19084 tree folded = fold_non_dependent_expr (tmp, complain);
19085 bool inhibit = integer_nonzerop (folded);
19086 begin_else_clause (stmt);
19087 if (inhibit)
19088 ++c_inhibit_evaluation_warnings;
19089 RECUR (ELSE_CLAUSE (t));
19090 if (inhibit)
19091 --c_inhibit_evaluation_warnings;
19092 finish_else_clause (stmt);
19093 }
19094
19095 finish_if_stmt (stmt);
19096 break;
19097
19098 case BIND_EXPR:
19099 if (BIND_EXPR_BODY_BLOCK (t))
19100 stmt = begin_function_body ();
19101 else
19102 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
19103 ? BCS_TRY_BLOCK : 0);
19104
19105 RECUR (BIND_EXPR_BODY (t));
19106
19107 if (BIND_EXPR_BODY_BLOCK (t))
19108 finish_function_body (stmt);
19109 else
19110 finish_compound_stmt (stmt);
19111 break;
19112
19113 case BREAK_STMT:
19114 finish_break_stmt ();
19115 break;
19116
19117 case CONTINUE_STMT:
19118 finish_continue_stmt ();
19119 break;
19120
19121 case SWITCH_STMT:
19122 stmt = begin_switch_stmt ();
19123 tmp = RECUR (SWITCH_STMT_COND (t));
19124 finish_switch_cond (tmp, stmt);
19125 RECUR (SWITCH_STMT_BODY (t));
19126 finish_switch_stmt (stmt);
19127 break;
19128
19129 case CASE_LABEL_EXPR:
19130 {
19131 tree decl = CASE_LABEL (t);
19132 tree low = RECUR (CASE_LOW (t));
19133 tree high = RECUR (CASE_HIGH (t));
19134 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
19135 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
19136 {
19137 tree label = CASE_LABEL (l);
19138 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
19139 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
19140 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
19141 }
19142 }
19143 break;
19144
19145 case LABEL_EXPR:
19146 {
19147 tree decl = LABEL_EXPR_LABEL (t);
19148 tree label;
19149
19150 label = finish_label_stmt (DECL_NAME (decl));
19151 if (TREE_CODE (label) == LABEL_DECL)
19152 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
19153 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
19154 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
19155 }
19156 break;
19157
19158 case GOTO_EXPR:
19159 tmp = GOTO_DESTINATION (t);
19160 if (TREE_CODE (tmp) != LABEL_DECL)
19161 /* Computed goto's must be tsubst'd into. On the other hand,
19162 non-computed gotos must not be; the identifier in question
19163 will have no binding. */
19164 tmp = RECUR (tmp);
19165 else
19166 tmp = DECL_NAME (tmp);
19167 finish_goto_stmt (tmp);
19168 break;
19169
19170 case ASM_EXPR:
19171 {
19172 tree string = RECUR (ASM_STRING (t));
19173 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
19174 complain, in_decl);
19175 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
19176 complain, in_decl);
19177 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
19178 complain, in_decl);
19179 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
19180 complain, in_decl);
19181 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
19182 outputs, inputs, clobbers, labels,
19183 ASM_INLINE_P (t));
19184 tree asm_expr = tmp;
19185 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
19186 asm_expr = TREE_OPERAND (asm_expr, 0);
19187 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
19188 }
19189 break;
19190
19191 case TRY_BLOCK:
19192 if (CLEANUP_P (t))
19193 {
19194 stmt = begin_try_block ();
19195 RECUR (TRY_STMTS (t));
19196 finish_cleanup_try_block (stmt);
19197 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
19198 }
19199 else
19200 {
19201 tree compound_stmt = NULL_TREE;
19202
19203 if (FN_TRY_BLOCK_P (t))
19204 stmt = begin_function_try_block (&compound_stmt);
19205 else
19206 stmt = begin_try_block ();
19207
19208 RECUR (TRY_STMTS (t));
19209
19210 if (FN_TRY_BLOCK_P (t))
19211 finish_function_try_block (stmt);
19212 else
19213 finish_try_block (stmt);
19214
19215 RECUR (TRY_HANDLERS (t));
19216 if (FN_TRY_BLOCK_P (t))
19217 finish_function_handler_sequence (stmt, compound_stmt);
19218 else
19219 finish_handler_sequence (stmt);
19220 }
19221 break;
19222
19223 case HANDLER:
19224 {
19225 tree decl = HANDLER_PARMS (t);
19226
19227 if (decl)
19228 {
19229 decl = tsubst (decl, args, complain, in_decl);
19230 /* Prevent instantiate_decl from trying to instantiate
19231 this variable. We've already done all that needs to be
19232 done. */
19233 if (decl != error_mark_node)
19234 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
19235 }
19236 stmt = begin_handler ();
19237 finish_handler_parms (decl, stmt);
19238 RECUR (HANDLER_BODY (t));
19239 finish_handler (stmt);
19240 }
19241 break;
19242
19243 case TAG_DEFN:
19244 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
19245 if (CLASS_TYPE_P (tmp))
19246 {
19247 /* Local classes are not independent templates; they are
19248 instantiated along with their containing function. And this
19249 way we don't have to deal with pushing out of one local class
19250 to instantiate a member of another local class. */
19251 /* Closures are handled by the LAMBDA_EXPR. */
19252 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
19253 complete_type (tmp);
19254 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
19255 if ((VAR_P (fld)
19256 || (TREE_CODE (fld) == FUNCTION_DECL
19257 && !DECL_ARTIFICIAL (fld)))
19258 && DECL_TEMPLATE_INSTANTIATION (fld))
19259 instantiate_decl (fld, /*defer_ok=*/false,
19260 /*expl_inst_class=*/false);
19261 }
19262 break;
19263
19264 case STATIC_ASSERT:
19265 {
19266 tree condition;
19267
19268 ++c_inhibit_evaluation_warnings;
19269 condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
19270 complain, in_decl);
19271 --c_inhibit_evaluation_warnings;
19272
19273 finish_static_assert (condition,
19274 STATIC_ASSERT_MESSAGE (t),
19275 STATIC_ASSERT_SOURCE_LOCATION (t),
19276 /*member_p=*/false, /*show_expr_p=*/true);
19277 }
19278 break;
19279
19280 case OACC_KERNELS:
19281 case OACC_PARALLEL:
19282 case OACC_SERIAL:
19283 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
19284 in_decl);
19285 stmt = begin_omp_parallel ();
19286 RECUR (OMP_BODY (t));
19287 finish_omp_construct (TREE_CODE (t), stmt, tmp);
19288 break;
19289
19290 case OMP_PARALLEL:
19291 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
19292 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
19293 complain, in_decl);
19294 if (OMP_PARALLEL_COMBINED (t))
19295 omp_parallel_combined_clauses = &tmp;
19296 stmt = begin_omp_parallel ();
19297 RECUR (OMP_PARALLEL_BODY (t));
19298 gcc_assert (omp_parallel_combined_clauses == NULL);
19299 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
19300 = OMP_PARALLEL_COMBINED (t);
19301 pop_omp_privatization_clauses (r);
19302 break;
19303
19304 case OMP_TASK:
19305 if (OMP_TASK_BODY (t) == NULL_TREE)
19306 {
19307 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19308 complain, in_decl);
19309 t = copy_node (t);
19310 OMP_TASK_CLAUSES (t) = tmp;
19311 add_stmt (t);
19312 break;
19313 }
19314 r = push_omp_privatization_clauses (false);
19315 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
19316 complain, in_decl);
19317 stmt = begin_omp_task ();
19318 RECUR (OMP_TASK_BODY (t));
19319 finish_omp_task (tmp, stmt);
19320 pop_omp_privatization_clauses (r);
19321 break;
19322
19323 case OMP_FOR:
19324 case OMP_LOOP:
19325 case OMP_SIMD:
19326 case OMP_DISTRIBUTE:
19327 case OMP_TASKLOOP:
19328 case OACC_LOOP:
19329 {
19330 tree clauses, body, pre_body;
19331 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
19332 tree orig_declv = NULL_TREE;
19333 tree incrv = NULL_TREE;
19334 enum c_omp_region_type ort = C_ORT_OMP;
19335 bool any_range_for = false;
19336 int i;
19337
19338 if (TREE_CODE (t) == OACC_LOOP)
19339 ort = C_ORT_ACC;
19340
19341 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
19342 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
19343 in_decl);
19344 if (OMP_FOR_INIT (t) != NULL_TREE)
19345 {
19346 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19347 if (OMP_FOR_ORIG_DECLS (t))
19348 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19349 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19350 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19351 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
19352 }
19353
19354 keep_next_level (true);
19355 stmt = begin_omp_structured_block ();
19356
19357 pre_body = push_stmt_list ();
19358 RECUR (OMP_FOR_PRE_BODY (t));
19359 pre_body = pop_stmt_list (pre_body);
19360
19361 if (OMP_FOR_INIT (t) != NULL_TREE)
19362 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19363 any_range_for
19364 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
19365 condv, incrv, &clauses, args,
19366 complain, in_decl);
19367 omp_parallel_combined_clauses = NULL;
19368
19369 if (any_range_for)
19370 {
19371 gcc_assert (orig_declv);
19372 body = begin_omp_structured_block ();
19373 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
19374 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
19375 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
19376 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
19377 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
19378 TREE_VEC_ELT (declv, i));
19379 }
19380 else
19381 body = push_stmt_list ();
19382 RECUR (OMP_FOR_BODY (t));
19383 if (any_range_for)
19384 body = finish_omp_structured_block (body);
19385 else
19386 body = pop_stmt_list (body);
19387
19388 if (OMP_FOR_INIT (t) != NULL_TREE)
19389 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
19390 orig_declv, initv, condv, incrv, body, pre_body,
19391 NULL, clauses);
19392 else
19393 {
19394 t = make_node (TREE_CODE (t));
19395 TREE_TYPE (t) = void_type_node;
19396 OMP_FOR_BODY (t) = body;
19397 OMP_FOR_PRE_BODY (t) = pre_body;
19398 OMP_FOR_CLAUSES (t) = clauses;
19399 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
19400 add_stmt (t);
19401 }
19402
19403 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
19404 t));
19405 pop_omp_privatization_clauses (r);
19406 }
19407 break;
19408
19409 case OMP_SECTIONS:
19410 case OMP_MASKED:
19411 omp_parallel_combined_clauses = NULL;
19412 /* FALLTHRU */
19413 case OMP_SINGLE:
19414 case OMP_SCOPE:
19415 case OMP_TEAMS:
19416 case OMP_CRITICAL:
19417 case OMP_TASKGROUP:
19418 case OMP_SCAN:
19419 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
19420 && OMP_TEAMS_COMBINED (t));
19421 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
19422 in_decl);
19423 if (TREE_CODE (t) == OMP_TEAMS)
19424 {
19425 keep_next_level (true);
19426 stmt = begin_omp_structured_block ();
19427 RECUR (OMP_BODY (t));
19428 stmt = finish_omp_structured_block (stmt);
19429 }
19430 else
19431 {
19432 stmt = push_stmt_list ();
19433 RECUR (OMP_BODY (t));
19434 stmt = pop_stmt_list (stmt);
19435 }
19436
19437 if (TREE_CODE (t) == OMP_CRITICAL
19438 && tmp != NULL_TREE
19439 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
19440 {
19441 error_at (OMP_CLAUSE_LOCATION (tmp),
19442 "%<#pragma omp critical%> with %<hint%> clause requires "
19443 "a name, except when %<omp_sync_hint_none%> is used");
19444 RETURN (error_mark_node);
19445 }
19446 t = copy_node (t);
19447 OMP_BODY (t) = stmt;
19448 OMP_CLAUSES (t) = tmp;
19449 add_stmt (t);
19450 pop_omp_privatization_clauses (r);
19451 break;
19452
19453 case OMP_DEPOBJ:
19454 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
19455 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
19456 {
19457 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
19458 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
19459 {
19460 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
19461 args, complain, in_decl);
19462 if (tmp == NULL_TREE)
19463 tmp = error_mark_node;
19464 }
19465 else
19466 {
19467 kind = (enum omp_clause_depend_kind)
19468 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
19469 tmp = NULL_TREE;
19470 }
19471 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
19472 }
19473 else
19474 finish_omp_depobj (EXPR_LOCATION (t), r,
19475 OMP_CLAUSE_DEPEND_INVALID,
19476 OMP_DEPOBJ_CLAUSES (t));
19477 break;
19478
19479 case OACC_DATA:
19480 case OMP_TARGET_DATA:
19481 case OMP_TARGET:
19482 tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
19483 TREE_CODE (t) == OACC_DATA
19484 ? C_ORT_ACC
19485 : TREE_CODE (t) == OMP_TARGET
19486 ? C_ORT_OMP_TARGET : C_ORT_OMP,
19487 args, complain, in_decl);
19488 keep_next_level (true);
19489 stmt = begin_omp_structured_block ();
19490
19491 RECUR (OMP_BODY (t));
19492 stmt = finish_omp_structured_block (stmt);
19493
19494 t = copy_node (t);
19495 OMP_BODY (t) = stmt;
19496 OMP_CLAUSES (t) = tmp;
19497
19498 if (TREE_CODE (t) == OMP_TARGET)
19499 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
19500 &OMP_CLAUSES (t));
19501
19502 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
19503 {
19504 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
19505 if (teams)
19506 /* For combined target teams, ensure the num_teams and
19507 thread_limit clause expressions are evaluated on the host,
19508 before entering the target construct. */
19509 for (tree c = OMP_TEAMS_CLAUSES (teams);
19510 c; c = OMP_CLAUSE_CHAIN (c))
19511 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19512 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19513 for (int i = 0;
19514 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
19515 if (OMP_CLAUSE_OPERAND (c, i)
19516 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
19517 {
19518 tree expr = OMP_CLAUSE_OPERAND (c, i);
19519 expr = force_target_expr (TREE_TYPE (expr), expr,
19520 tf_none);
19521 if (expr == error_mark_node)
19522 continue;
19523 tmp = TARGET_EXPR_SLOT (expr);
19524 add_stmt (expr);
19525 OMP_CLAUSE_OPERAND (c, i) = expr;
19526 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19527 OMP_CLAUSE_FIRSTPRIVATE);
19528 OMP_CLAUSE_DECL (tc) = tmp;
19529 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
19530 OMP_TARGET_CLAUSES (t) = tc;
19531 }
19532 }
19533 add_stmt (t);
19534 break;
19535
19536 case OACC_DECLARE:
19537 t = copy_node (t);
19538 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
19539 complain, in_decl);
19540 OACC_DECLARE_CLAUSES (t) = tmp;
19541 add_stmt (t);
19542 break;
19543
19544 case OMP_TARGET_UPDATE:
19545 case OMP_TARGET_ENTER_DATA:
19546 case OMP_TARGET_EXIT_DATA:
19547 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
19548 complain, in_decl);
19549 t = copy_node (t);
19550 OMP_STANDALONE_CLAUSES (t) = tmp;
19551 add_stmt (t);
19552 break;
19553
19554 case OACC_CACHE:
19555 case OACC_ENTER_DATA:
19556 case OACC_EXIT_DATA:
19557 case OACC_UPDATE:
19558 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
19559 complain, in_decl);
19560 t = copy_node (t);
19561 OMP_STANDALONE_CLAUSES (t) = tmp;
19562 add_stmt (t);
19563 break;
19564
19565 case OMP_ORDERED:
19566 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
19567 complain, in_decl);
19568 if (OMP_BODY (t))
19569 {
19570 stmt = push_stmt_list ();
19571 RECUR (OMP_BODY (t));
19572 stmt = pop_stmt_list (stmt);
19573 }
19574 else
19575 stmt = NULL_TREE;
19576
19577 t = copy_node (t);
19578 OMP_BODY (t) = stmt;
19579 OMP_ORDERED_CLAUSES (t) = tmp;
19580 add_stmt (t);
19581 break;
19582
19583 case OMP_MASTER:
19584 omp_parallel_combined_clauses = NULL;
19585 /* FALLTHRU */
19586 case OMP_SECTION:
19587 stmt = push_stmt_list ();
19588 RECUR (OMP_BODY (t));
19589 stmt = pop_stmt_list (stmt);
19590
19591 t = copy_node (t);
19592 OMP_BODY (t) = stmt;
19593 add_stmt (t);
19594 break;
19595
19596 case OMP_ATOMIC:
19597 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
19598 tmp = NULL_TREE;
19599 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
19600 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
19601 complain, in_decl);
19602 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
19603 {
19604 tree op1 = TREE_OPERAND (t, 1);
19605 tree rhs1 = NULL_TREE;
19606 tree r = NULL_TREE;
19607 tree lhs, rhs;
19608 if (TREE_CODE (op1) == COMPOUND_EXPR)
19609 {
19610 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19611 op1 = TREE_OPERAND (op1, 1);
19612 }
19613 if (TREE_CODE (op1) == COND_EXPR)
19614 {
19615 gcc_assert (rhs1 == NULL_TREE);
19616 tree c = TREE_OPERAND (op1, 0);
19617 if (TREE_CODE (c) == MODIFY_EXPR)
19618 {
19619 r = RECUR (TREE_OPERAND (c, 0));
19620 c = TREE_OPERAND (c, 1);
19621 }
19622 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19623 rhs = RECUR (TREE_OPERAND (c, 1));
19624 lhs = RECUR (TREE_OPERAND (op1, 2));
19625 rhs1 = RECUR (TREE_OPERAND (op1, 1));
19626 }
19627 else
19628 {
19629 lhs = RECUR (TREE_OPERAND (op1, 0));
19630 rhs = RECUR (TREE_OPERAND (op1, 1));
19631 }
19632 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19633 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
19634 tmp, OMP_ATOMIC_MEMORY_ORDER (t),
19635 OMP_ATOMIC_WEAK (t));
19636 }
19637 else
19638 {
19639 tree op1 = TREE_OPERAND (t, 1);
19640 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19641 tree rhs1 = NULL_TREE, r = NULL_TREE;
19642 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19643 enum tree_code opcode = NOP_EXPR;
19644 if (code == OMP_ATOMIC_READ)
19645 {
19646 v = RECUR (TREE_OPERAND (op1, 0));
19647 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19648 }
19649 else if (code == OMP_ATOMIC_CAPTURE_OLD
19650 || code == OMP_ATOMIC_CAPTURE_NEW)
19651 {
19652 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19653 v = RECUR (TREE_OPERAND (op1, 0));
19654 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19655 if (TREE_CODE (op11) == COMPOUND_EXPR)
19656 {
19657 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19658 op11 = TREE_OPERAND (op11, 1);
19659 }
19660 if (TREE_CODE (op11) == COND_EXPR)
19661 {
19662 gcc_assert (rhs1 == NULL_TREE);
19663 tree c = TREE_OPERAND (op11, 0);
19664 if (TREE_CODE (c) == MODIFY_EXPR)
19665 {
19666 r = RECUR (TREE_OPERAND (c, 0));
19667 c = TREE_OPERAND (c, 1);
19668 }
19669 gcc_assert (TREE_CODE (c) == EQ_EXPR);
19670 rhs = RECUR (TREE_OPERAND (c, 1));
19671 lhs = RECUR (TREE_OPERAND (op11, 2));
19672 rhs1 = RECUR (TREE_OPERAND (op11, 1));
19673 }
19674 else
19675 {
19676 lhs = RECUR (TREE_OPERAND (op11, 0));
19677 rhs = RECUR (TREE_OPERAND (op11, 1));
19678 }
19679 opcode = TREE_CODE (op11);
19680 if (opcode == MODIFY_EXPR)
19681 opcode = NOP_EXPR;
19682 }
19683 else
19684 {
19685 code = OMP_ATOMIC;
19686 lhs = RECUR (TREE_OPERAND (op1, 0));
19687 rhs = RECUR (TREE_OPERAND (op1, 1));
19688 }
19689 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19690 lhs1, rhs1, r, tmp,
19691 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
19692 }
19693 break;
19694
19695 case TRANSACTION_EXPR:
19696 {
19697 int flags = 0;
19698 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19699 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19700
19701 if (TRANSACTION_EXPR_IS_STMT (t))
19702 {
19703 tree body = TRANSACTION_EXPR_BODY (t);
19704 tree noex = NULL_TREE;
19705 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19706 {
19707 noex = MUST_NOT_THROW_COND (body);
19708 if (noex == NULL_TREE)
19709 noex = boolean_true_node;
19710 body = TREE_OPERAND (body, 0);
19711 }
19712 stmt = begin_transaction_stmt (input_location, NULL, flags);
19713 RECUR (body);
19714 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19715 }
19716 else
19717 {
19718 stmt = build_transaction_expr (EXPR_LOCATION (t),
19719 RECUR (TRANSACTION_EXPR_BODY (t)),
19720 flags, NULL_TREE);
19721 RETURN (stmt);
19722 }
19723 }
19724 break;
19725
19726 case MUST_NOT_THROW_EXPR:
19727 {
19728 tree op0 = RECUR (TREE_OPERAND (t, 0));
19729 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19730 RETURN (build_must_not_throw_expr (op0, cond));
19731 }
19732
19733 case EXPR_PACK_EXPANSION:
19734 error ("invalid use of pack expansion expression");
19735 RETURN (error_mark_node);
19736
19737 case NONTYPE_ARGUMENT_PACK:
19738 error ("use %<...%> to expand argument pack");
19739 RETURN (error_mark_node);
19740
19741 case COMPOUND_EXPR:
19742 tmp = RECUR (TREE_OPERAND (t, 0));
19743 if (tmp == NULL_TREE)
19744 /* If the first operand was a statement, we're done with it. */
19745 RETURN (RECUR (TREE_OPERAND (t, 1)));
19746 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19747 RECUR (TREE_OPERAND (t, 1)),
19748 templated_operator_saved_lookups (t),
19749 complain));
19750
19751 case ANNOTATE_EXPR:
19752 tmp = RECUR (TREE_OPERAND (t, 0));
19753 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19754 TREE_TYPE (tmp), tmp,
19755 RECUR (TREE_OPERAND (t, 1)),
19756 RECUR (TREE_OPERAND (t, 2))));
19757
19758 case PREDICT_EXPR:
19759 RETURN (add_stmt (copy_node (t)));
19760
19761 default:
19762 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19763
19764 RETURN (tsubst_copy_and_build (t, args, complain, in_decl));
19765 }
19766
19767 RETURN (NULL_TREE);
19768 out:
19769 input_location = loc;
19770 return r;
19771 #undef RECUR
19772 #undef RETURN
19773 }
19774
19775 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19776 function. For description of the body see comment above
19777 cp_parser_omp_declare_reduction_exprs. */
19778
19779 static void
19780 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19781 {
19782 if (t == NULL_TREE || t == error_mark_node)
19783 return;
19784
19785 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19786
19787 tree_stmt_iterator tsi;
19788 int i;
19789 tree stmts[7];
19790 memset (stmts, 0, sizeof stmts);
19791 for (i = 0, tsi = tsi_start (t);
19792 i < 7 && !tsi_end_p (tsi);
19793 i++, tsi_next (&tsi))
19794 stmts[i] = tsi_stmt (tsi);
19795 gcc_assert (tsi_end_p (tsi));
19796
19797 if (i >= 3)
19798 {
19799 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19800 && TREE_CODE (stmts[1]) == DECL_EXPR);
19801 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19802 args, complain, in_decl);
19803 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19804 args, complain, in_decl);
19805 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19806 expect to be pushing it. */
19807 DECL_CONTEXT (omp_out) = current_function_decl;
19808 DECL_CONTEXT (omp_in) = current_function_decl;
19809 keep_next_level (true);
19810 tree block = begin_omp_structured_block ();
19811 tsubst_expr (stmts[2], args, complain, in_decl);
19812 block = finish_omp_structured_block (block);
19813 block = maybe_cleanup_point_expr_void (block);
19814 add_decl_expr (omp_out);
19815 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
19816 add_decl_expr (omp_in);
19817 finish_expr_stmt (block);
19818 }
19819 if (i >= 6)
19820 {
19821 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19822 && TREE_CODE (stmts[4]) == DECL_EXPR);
19823 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19824 args, complain, in_decl);
19825 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19826 args, complain, in_decl);
19827 DECL_CONTEXT (omp_priv) = current_function_decl;
19828 DECL_CONTEXT (omp_orig) = current_function_decl;
19829 keep_next_level (true);
19830 tree block = begin_omp_structured_block ();
19831 tsubst_expr (stmts[5], args, complain, in_decl);
19832 block = finish_omp_structured_block (block);
19833 block = maybe_cleanup_point_expr_void (block);
19834 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19835 add_decl_expr (omp_priv);
19836 add_decl_expr (omp_orig);
19837 finish_expr_stmt (block);
19838 if (i == 7)
19839 add_decl_expr (omp_orig);
19840 }
19841 }
19842
19843 /* T is a postfix-expression that is not being used in a function
19844 call. Return the substituted version of T. */
19845
19846 static tree
19847 tsubst_non_call_postfix_expression (tree t, tree args,
19848 tsubst_flags_t complain,
19849 tree in_decl)
19850 {
19851 if (TREE_CODE (t) == SCOPE_REF)
19852 t = tsubst_qualified_id (t, args, complain, in_decl,
19853 /*done=*/false, /*address_p=*/false);
19854 else
19855 t = tsubst_copy_and_build (t, args, complain, in_decl);
19856
19857 return t;
19858 }
19859
19860 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19861 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19862 dependent init-capture. */
19863
19864 static void
19865 prepend_one_capture (tree field, tree init, tree &list,
19866 tsubst_flags_t complain)
19867 {
19868 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19869 {
19870 tree type = NULL_TREE;
19871 if (!init)
19872 {
19873 if (complain & tf_error)
19874 error ("empty initializer in lambda init-capture");
19875 init = error_mark_node;
19876 }
19877 else if (TREE_CODE (init) == TREE_LIST)
19878 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19879 if (!type)
19880 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19881 TREE_TYPE (field) = type;
19882 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19883 }
19884 list = tree_cons (field, init, list);
19885 }
19886
19887 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19888 instantiation context. Instantiating a pack expansion containing a lambda
19889 might result in multiple lambdas all based on the same lambda in the
19890 template. */
19891
19892 tree
19893 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19894 {
19895 tree oldfn = lambda_function (t);
19896 in_decl = oldfn;
19897
19898 tree r = build_lambda_expr ();
19899
19900 LAMBDA_EXPR_LOCATION (r)
19901 = LAMBDA_EXPR_LOCATION (t);
19902 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19903 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19904 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19905 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19906 LAMBDA_EXPR_REGEN_INFO (r)
19907 = build_template_info (t, add_to_template_args (TI_ARGS (ti),
19908 preserve_args (args)));
19909 else
19910 LAMBDA_EXPR_REGEN_INFO (r)
19911 = build_template_info (t, preserve_args (args));
19912
19913 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19914 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19915
19916 vec<tree,va_gc>* field_packs = NULL;
19917
19918 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19919 cap = TREE_CHAIN (cap))
19920 {
19921 tree ofield = TREE_PURPOSE (cap);
19922 tree init = TREE_VALUE (cap);
19923 if (PACK_EXPANSION_P (init))
19924 init = tsubst_pack_expansion (init, args, complain, in_decl);
19925 else
19926 init = tsubst_copy_and_build (init, args, complain, in_decl);
19927
19928 if (init == error_mark_node)
19929 return error_mark_node;
19930
19931 if (init && TREE_CODE (init) == TREE_LIST)
19932 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19933
19934 if (!processing_template_decl
19935 && init && TREE_CODE (init) != TREE_VEC
19936 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19937 {
19938 /* For a VLA, simply tsubsting the field type won't work, we need to
19939 go through add_capture again. XXX do we want to do this for all
19940 captures? */
19941 tree name = (get_identifier
19942 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19943 tree ftype = TREE_TYPE (ofield);
19944 bool by_ref = (TYPE_REF_P (ftype)
19945 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19946 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19947 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19948 continue;
19949 }
19950
19951 if (PACK_EXPANSION_P (ofield))
19952 ofield = PACK_EXPANSION_PATTERN (ofield);
19953 tree field = tsubst_decl (ofield, args, complain);
19954
19955 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19956 {
19957 /* Remember these for when we've pushed local_specializations. */
19958 vec_safe_push (field_packs, ofield);
19959 vec_safe_push (field_packs, field);
19960 }
19961
19962 if (field == error_mark_node)
19963 return error_mark_node;
19964
19965 if (TREE_CODE (field) == TREE_VEC)
19966 {
19967 int len = TREE_VEC_LENGTH (field);
19968 gcc_assert (TREE_CODE (init) == TREE_VEC
19969 && TREE_VEC_LENGTH (init) == len);
19970 for (int i = 0; i < len; ++i)
19971 prepend_one_capture (TREE_VEC_ELT (field, i),
19972 TREE_VEC_ELT (init, i),
19973 LAMBDA_EXPR_CAPTURE_LIST (r),
19974 complain);
19975 }
19976 else
19977 {
19978 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19979 complain);
19980
19981 if (id_equal (DECL_NAME (field), "__this"))
19982 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19983 }
19984 }
19985
19986 tree type = begin_lambda_type (r);
19987 if (type == error_mark_node)
19988 return error_mark_node;
19989
19990 if (LAMBDA_EXPR_EXTRA_SCOPE (t))
19991 record_lambda_scope (r);
19992 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
19993 /* If we're pushed into another scope (PR105652), fix it. */
19994 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
19995 = TYPE_CONTEXT (TREE_TYPE (t));
19996 record_lambda_scope_discriminator (r);
19997
19998 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19999 determine_visibility (TYPE_NAME (type));
20000
20001 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
20002
20003 tree oldtmpl = (generic_lambda_fn_p (oldfn)
20004 ? DECL_TI_TEMPLATE (oldfn)
20005 : NULL_TREE);
20006
20007 tree fntype = static_fn_type (oldfn);
20008 if (oldtmpl)
20009 ++processing_template_decl;
20010 fntype = tsubst (fntype, args, complain, in_decl);
20011 if (oldtmpl)
20012 --processing_template_decl;
20013
20014 if (fntype == error_mark_node)
20015 r = error_mark_node;
20016 else
20017 {
20018 /* The body of a lambda-expression is not a subexpression of the
20019 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
20020 which would be skipped if cp_unevaluated_operand. */
20021 cp_evaluated ev;
20022
20023 /* Fix the type of 'this'. */
20024 fntype = build_memfn_type (fntype, type,
20025 type_memfn_quals (fntype),
20026 type_memfn_rqual (fntype));
20027 tree inst = (oldtmpl
20028 ? tsubst_template_decl (oldtmpl, args, complain, fntype)
20029 : tsubst_function_decl (oldfn, args, complain, fntype));
20030 if (inst == error_mark_node)
20031 {
20032 r = error_mark_node;
20033 goto out;
20034 }
20035 finish_member_declaration (inst);
20036 record_lambda_scope_sig_discriminator (r, inst);
20037
20038 tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
20039
20040 /* Let finish_function set this. */
20041 DECL_DECLARED_CONSTEXPR_P (fn) = false;
20042
20043 bool nested = cfun;
20044 if (nested)
20045 push_function_context ();
20046 else
20047 /* Still increment function_depth so that we don't GC in the
20048 middle of an expression. */
20049 ++function_depth;
20050
20051 local_specialization_stack s (lss_copy);
20052
20053 bool save_in_consteval_if_p = in_consteval_if_p;
20054 in_consteval_if_p = false;
20055
20056 tree body = start_lambda_function (fn, r);
20057
20058 /* Now record them for lookup_init_capture_pack. */
20059 int fplen = vec_safe_length (field_packs);
20060 for (int i = 0; i < fplen; )
20061 {
20062 tree pack = (*field_packs)[i++];
20063 tree inst = (*field_packs)[i++];
20064 register_local_specialization (inst, pack);
20065 }
20066 release_tree_vector (field_packs);
20067
20068 register_parameter_specializations (oldfn, fn);
20069
20070 if (oldtmpl)
20071 {
20072 /* We might not partially instantiate some parts of the function, so
20073 copy these flags from the original template. */
20074 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
20075 current_function_returns_value = ol->returns_value;
20076 current_function_returns_null = ol->returns_null;
20077 current_function_returns_abnormally = ol->returns_abnormally;
20078 current_function_infinite_loop = ol->infinite_loop;
20079 }
20080
20081 /* [temp.deduct] A lambda-expression appearing in a function type or a
20082 template parameter is not considered part of the immediate context for
20083 the purposes of template argument deduction. */
20084 complain = tf_warning_or_error;
20085
20086 tree saved = DECL_SAVED_TREE (oldfn);
20087 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
20088 /* We already have a body block from start_lambda_function, we don't
20089 need another to confuse NRV (91217). */
20090 saved = BIND_EXPR_BODY (saved);
20091
20092 tsubst_expr (saved, args, complain, r);
20093
20094 finish_lambda_function (body);
20095
20096 in_consteval_if_p = save_in_consteval_if_p;
20097
20098 if (nested)
20099 pop_function_context ();
20100 else
20101 --function_depth;
20102
20103 /* The capture list was built up in reverse order; fix that now. */
20104 LAMBDA_EXPR_CAPTURE_LIST (r)
20105 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
20106
20107 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
20108
20109 maybe_add_lambda_conv_op (type);
20110 }
20111
20112 out:
20113 finish_struct (type, /*attr*/NULL_TREE);
20114
20115 insert_pending_capture_proxies ();
20116
20117 return r;
20118 }
20119
20120 /* Subroutine of maybe_fold_fn_template_args. */
20121
20122 static bool
20123 fold_targs_r (tree targs, tsubst_flags_t complain)
20124 {
20125 int len = TREE_VEC_LENGTH (targs);
20126 for (int i = 0; i < len; ++i)
20127 {
20128 tree &elt = TREE_VEC_ELT (targs, i);
20129 if (!elt || TYPE_P (elt)
20130 || TREE_CODE (elt) == TEMPLATE_DECL)
20131 continue;
20132 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
20133 {
20134 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
20135 return false;
20136 }
20137 else if (/* We can only safely preevaluate scalar prvalues. */
20138 SCALAR_TYPE_P (TREE_TYPE (elt))
20139 && !glvalue_p (elt)
20140 && !TREE_CONSTANT (elt))
20141 {
20142 elt = cxx_constant_value (elt, complain);
20143 if (elt == error_mark_node)
20144 return false;
20145 }
20146 }
20147
20148 return true;
20149 }
20150
20151 /* Try to do constant evaluation of any explicit template arguments in FN
20152 before overload resolution, to get any errors only once. Return true iff
20153 we didn't have any problems folding. */
20154
20155 static bool
20156 maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
20157 {
20158 if (processing_template_decl || fn == NULL_TREE)
20159 return true;
20160 if (fn == error_mark_node)
20161 return false;
20162 if (TREE_CODE (fn) == OFFSET_REF
20163 || TREE_CODE (fn) == COMPONENT_REF)
20164 fn = TREE_OPERAND (fn, 1);
20165 if (BASELINK_P (fn))
20166 fn = BASELINK_FUNCTIONS (fn);
20167 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
20168 return true;
20169 tree targs = TREE_OPERAND (fn, 1);
20170 if (targs == NULL_TREE)
20171 return true;
20172 if (targs == error_mark_node)
20173 return false;
20174 return fold_targs_r (targs, complain);
20175 }
20176
20177 /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
20178 handling. */
20179
20180 static void
20181 tsubst_copy_and_build_call_args (tree t, tree args, tsubst_flags_t complain,
20182 tree in_decl, releasing_vec &call_args)
20183 {
20184 unsigned int nargs = call_expr_nargs (t);
20185 for (unsigned int i = 0; i < nargs; ++i)
20186 {
20187 tree arg = CALL_EXPR_ARG (t, i);
20188
20189 if (!PACK_EXPANSION_P (arg))
20190 vec_safe_push (call_args,
20191 tsubst_copy_and_build (arg, args, complain, in_decl));
20192 else
20193 {
20194 /* Expand the pack expansion and push each entry onto CALL_ARGS. */
20195 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20196 if (TREE_CODE (arg) == TREE_VEC)
20197 {
20198 unsigned int len, j;
20199
20200 len = TREE_VEC_LENGTH (arg);
20201 for (j = 0; j < len; ++j)
20202 {
20203 tree value = TREE_VEC_ELT (arg, j);
20204 if (value != NULL_TREE)
20205 value = convert_from_reference (value);
20206 vec_safe_push (call_args, value);
20207 }
20208 }
20209 else
20210 /* A partial substitution. Add one entry. */
20211 vec_safe_push (call_args, arg);
20212 }
20213 }
20214 }
20215
20216 /* Like tsubst but deals with expressions and performs semantic
20217 analysis. */
20218
20219 tree
20220 tsubst_copy_and_build (tree t,
20221 tree args,
20222 tsubst_flags_t complain,
20223 tree in_decl)
20224 {
20225 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
20226 #define RECUR(NODE) \
20227 tsubst_copy_and_build (NODE, args, complain, in_decl)
20228
20229 tree retval, op1;
20230 location_t save_loc;
20231
20232 if (t == NULL_TREE || t == error_mark_node)
20233 return t;
20234
20235 save_loc = input_location;
20236 if (location_t eloc = cp_expr_location (t))
20237 input_location = eloc;
20238
20239 /* N3276 decltype magic only applies to calls at the top level or on the
20240 right side of a comma. */
20241 tsubst_flags_t decltype_flag = (complain & tf_decltype);
20242 complain &= ~tf_decltype;
20243
20244 switch (TREE_CODE (t))
20245 {
20246 case USING_DECL:
20247 t = DECL_NAME (t);
20248 /* Fall through. */
20249 case IDENTIFIER_NODE:
20250 {
20251 tree decl;
20252 cp_id_kind idk;
20253 const char *error_msg;
20254
20255 if (IDENTIFIER_CONV_OP_P (t))
20256 {
20257 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20258 t = make_conv_op_name (new_type);
20259 }
20260
20261 /* Look up the name. */
20262 decl = lookup_name (t);
20263
20264 /* By convention, expressions use ERROR_MARK_NODE to indicate
20265 failure, not NULL_TREE. */
20266 if (decl == NULL_TREE)
20267 decl = error_mark_node;
20268
20269 decl = finish_id_expression (t, decl, NULL_TREE,
20270 &idk,
20271 /*i_c_e_p=*/false,
20272 /*allow_i_c_e_p=*/true,
20273 /*non_i_c_e_p=*/nullptr,
20274 /*template_p=*/false,
20275 /*done=*/true,
20276 /*address_p=*/false,
20277 /*template_arg_p=*/false,
20278 &error_msg,
20279 input_location);
20280 if (error_msg)
20281 error (error_msg);
20282 if (identifier_p (decl))
20283 {
20284 if (complain & tf_error)
20285 unqualified_name_lookup_error (decl);
20286 decl = error_mark_node;
20287 }
20288 RETURN (decl);
20289 }
20290
20291 case TEMPLATE_ID_EXPR:
20292 {
20293 tree object;
20294 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20295 complain, in_decl);
20296 tree targs = TREE_OPERAND (t, 1);
20297
20298 if (targs)
20299 targs = tsubst_template_args (targs, args, complain, in_decl);
20300 if (targs == error_mark_node)
20301 RETURN (error_mark_node);
20302
20303 if (TREE_CODE (templ) == SCOPE_REF)
20304 {
20305 tree name = TREE_OPERAND (templ, 1);
20306 tree tid = lookup_template_function (name, targs);
20307 TREE_OPERAND (templ, 1) = tid;
20308 RETURN (templ);
20309 }
20310
20311 if (concept_definition_p (templ))
20312 {
20313 tree check = build_concept_check (templ, targs, complain);
20314 if (check == error_mark_node)
20315 RETURN (error_mark_node);
20316
20317 tree id = unpack_concept_check (check);
20318
20319 /* If we built a function concept check, return the underlying
20320 template-id. So we can evaluate it as a function call. */
20321 if (function_concept_p (TREE_OPERAND (id, 0)))
20322 RETURN (id);
20323
20324 RETURN (check);
20325 }
20326
20327 if (variable_template_p (templ))
20328 {
20329 tree r = lookup_and_finish_template_variable (templ, targs,
20330 complain);
20331 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
20332 RETURN (r);
20333 }
20334
20335 if (TREE_CODE (templ) == COMPONENT_REF)
20336 {
20337 object = TREE_OPERAND (templ, 0);
20338 templ = TREE_OPERAND (templ, 1);
20339 }
20340 else
20341 object = NULL_TREE;
20342
20343 tree tid = lookup_template_function (templ, targs);
20344 protected_set_expr_location (tid, EXPR_LOCATION (t));
20345
20346 if (object)
20347 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
20348 object, tid, NULL_TREE));
20349 else if (identifier_p (templ))
20350 {
20351 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
20352 name lookup found nothing when parsing the template name. */
20353 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
20354 RETURN (tid);
20355 }
20356 else
20357 RETURN (baselink_for_fns (tid));
20358 }
20359
20360 case INDIRECT_REF:
20361 {
20362 tree r = RECUR (TREE_OPERAND (t, 0));
20363
20364 if (REFERENCE_REF_P (t))
20365 {
20366 /* A type conversion to reference type will be enclosed in
20367 such an indirect ref, but the substitution of the cast
20368 will have also added such an indirect ref. */
20369 r = convert_from_reference (r);
20370 }
20371 else
20372 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
20373 templated_operator_saved_lookups (t),
20374 complain|decltype_flag);
20375
20376 if (REF_PARENTHESIZED_P (t))
20377 r = force_paren_expr (r);
20378
20379 RETURN (r);
20380 }
20381
20382 case NOP_EXPR:
20383 {
20384 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20385 tree op0 = RECUR (TREE_OPERAND (t, 0));
20386 RETURN (build_nop (type, op0));
20387 }
20388
20389 case IMPLICIT_CONV_EXPR:
20390 {
20391 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20392 tree expr = RECUR (TREE_OPERAND (t, 0));
20393 if (dependent_type_p (type) || type_dependent_expression_p (expr))
20394 {
20395 retval = copy_node (t);
20396 TREE_TYPE (retval) = type;
20397 TREE_OPERAND (retval, 0) = expr;
20398 RETURN (retval);
20399 }
20400 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
20401 /* We'll pass this to convert_nontype_argument again, we don't need
20402 to actually perform any conversion here. */
20403 RETURN (expr);
20404 int flags = LOOKUP_IMPLICIT;
20405 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
20406 flags = LOOKUP_NORMAL;
20407 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
20408 flags |= LOOKUP_NO_NARROWING;
20409 RETURN (perform_implicit_conversion_flags (type, expr, complain,
20410 flags));
20411 }
20412
20413 case CONVERT_EXPR:
20414 {
20415 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20416 tree op0 = RECUR (TREE_OPERAND (t, 0));
20417 if (op0 == error_mark_node)
20418 RETURN (error_mark_node);
20419 RETURN (build1 (CONVERT_EXPR, type, op0));
20420 }
20421
20422 case CAST_EXPR:
20423 case REINTERPRET_CAST_EXPR:
20424 case CONST_CAST_EXPR:
20425 case DYNAMIC_CAST_EXPR:
20426 case STATIC_CAST_EXPR:
20427 {
20428 tree type;
20429 tree op, r = NULL_TREE;
20430
20431 tsubst_flags_t tcomplain = complain;
20432 if (TREE_CODE (t) == CAST_EXPR)
20433 tcomplain |= tf_tst_ok;
20434 type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
20435
20436 op = RECUR (TREE_OPERAND (t, 0));
20437
20438 warning_sentinel s(warn_useless_cast);
20439 warning_sentinel s2(warn_ignored_qualifiers);
20440 warning_sentinel s3(warn_int_in_bool_context);
20441 switch (TREE_CODE (t))
20442 {
20443 case CAST_EXPR:
20444 r = build_functional_cast (input_location, type, op, complain);
20445 break;
20446 case REINTERPRET_CAST_EXPR:
20447 r = build_reinterpret_cast (input_location, type, op, complain);
20448 break;
20449 case CONST_CAST_EXPR:
20450 r = build_const_cast (input_location, type, op, complain);
20451 break;
20452 case DYNAMIC_CAST_EXPR:
20453 r = build_dynamic_cast (input_location, type, op, complain);
20454 break;
20455 case STATIC_CAST_EXPR:
20456 r = build_static_cast (input_location, type, op, complain);
20457 if (IMPLICIT_RVALUE_P (t))
20458 set_implicit_rvalue_p (r);
20459 break;
20460 default:
20461 gcc_unreachable ();
20462 }
20463
20464 RETURN (r);
20465 }
20466
20467 case BIT_CAST_EXPR:
20468 {
20469 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20470 tree op0 = RECUR (TREE_OPERAND (t, 0));
20471 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
20472 }
20473
20474 case POSTDECREMENT_EXPR:
20475 case POSTINCREMENT_EXPR:
20476 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20477 args, complain, in_decl);
20478 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
20479 templated_operator_saved_lookups (t),
20480 complain|decltype_flag));
20481
20482 case PREDECREMENT_EXPR:
20483 case PREINCREMENT_EXPR:
20484 case NEGATE_EXPR:
20485 case BIT_NOT_EXPR:
20486 case ABS_EXPR:
20487 case TRUTH_NOT_EXPR:
20488 case UNARY_PLUS_EXPR: /* Unary + */
20489 case REALPART_EXPR:
20490 case IMAGPART_EXPR:
20491 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
20492 RECUR (TREE_OPERAND (t, 0)),
20493 templated_operator_saved_lookups (t),
20494 complain|decltype_flag));
20495
20496 case EXCESS_PRECISION_EXPR:
20497 {
20498 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20499 tree op0 = RECUR (TREE_OPERAND (t, 0));
20500 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
20501 RETURN (op0);
20502 RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
20503 type, op0));
20504 }
20505
20506 case FIX_TRUNC_EXPR:
20507 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
20508 gcc_unreachable ();
20509
20510 case ADDR_EXPR:
20511 op1 = TREE_OPERAND (t, 0);
20512 if (TREE_CODE (op1) == LABEL_DECL)
20513 RETURN (finish_label_address_expr (DECL_NAME (op1),
20514 EXPR_LOCATION (op1)));
20515 if (TREE_CODE (op1) == SCOPE_REF)
20516 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
20517 /*done=*/true, /*address_p=*/true);
20518 else
20519 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
20520 in_decl);
20521 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
20522 templated_operator_saved_lookups (t),
20523 complain|decltype_flag));
20524
20525 case PLUS_EXPR:
20526 case MINUS_EXPR:
20527 case MULT_EXPR:
20528 case TRUNC_DIV_EXPR:
20529 case CEIL_DIV_EXPR:
20530 case FLOOR_DIV_EXPR:
20531 case ROUND_DIV_EXPR:
20532 case EXACT_DIV_EXPR:
20533 case BIT_AND_EXPR:
20534 case BIT_IOR_EXPR:
20535 case BIT_XOR_EXPR:
20536 case TRUNC_MOD_EXPR:
20537 case FLOOR_MOD_EXPR:
20538 case TRUTH_ANDIF_EXPR:
20539 case TRUTH_ORIF_EXPR:
20540 case TRUTH_AND_EXPR:
20541 case TRUTH_OR_EXPR:
20542 case RSHIFT_EXPR:
20543 case LSHIFT_EXPR:
20544 case EQ_EXPR:
20545 case NE_EXPR:
20546 case MAX_EXPR:
20547 case MIN_EXPR:
20548 case LE_EXPR:
20549 case GE_EXPR:
20550 case LT_EXPR:
20551 case GT_EXPR:
20552 case SPACESHIP_EXPR:
20553 case MEMBER_REF:
20554 case DOTSTAR_EXPR:
20555 {
20556 /* If either OP0 or OP1 was value- or type-dependent, suppress
20557 warnings that depend on the range of the types involved. */
20558 tree op0 = TREE_OPERAND (t, 0);
20559 tree op1 = TREE_OPERAND (t, 1);
20560 const bool was_dep = (dependent_operand_p (op0)
20561 || dependent_operand_p (op1));
20562 op0 = RECUR (op0);
20563 op1 = RECUR (op1);
20564
20565 warning_sentinel s1(warn_type_limits, was_dep);
20566 warning_sentinel s2(warn_div_by_zero, was_dep);
20567 warning_sentinel s3(warn_logical_op, was_dep);
20568 warning_sentinel s4(warn_tautological_compare, was_dep);
20569 warning_sentinel s5(warn_address, was_dep);
20570
20571 tree r = build_x_binary_op
20572 (input_location, TREE_CODE (t),
20573 op0,
20574 (warning_suppressed_p (TREE_OPERAND (t, 0))
20575 ? ERROR_MARK
20576 : TREE_CODE (TREE_OPERAND (t, 0))),
20577 op1,
20578 (warning_suppressed_p (TREE_OPERAND (t, 1))
20579 ? ERROR_MARK
20580 : TREE_CODE (TREE_OPERAND (t, 1))),
20581 templated_operator_saved_lookups (t),
20582 /*overload=*/NULL,
20583 complain|decltype_flag);
20584 if (EXPR_P (r))
20585 copy_warning (r, t);
20586
20587 RETURN (r);
20588 }
20589
20590 case POINTER_PLUS_EXPR:
20591 {
20592 tree op0 = RECUR (TREE_OPERAND (t, 0));
20593 if (op0 == error_mark_node)
20594 RETURN (error_mark_node);
20595 tree op1 = RECUR (TREE_OPERAND (t, 1));
20596 if (op1 == error_mark_node)
20597 RETURN (error_mark_node);
20598 RETURN (fold_build_pointer_plus (op0, op1));
20599 }
20600
20601 case SCOPE_REF:
20602 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
20603 /*address_p=*/false));
20604
20605 case BASELINK:
20606 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
20607 args, complain, in_decl));
20608
20609 case ARRAY_REF:
20610 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20611 args, complain, in_decl);
20612 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
20613 && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
20614 == ovl_op_identifier (ARRAY_REF)))
20615 {
20616 tree c = TREE_OPERAND (t, 1);
20617 releasing_vec index_exp_list;
20618 tsubst_copy_and_build_call_args (c, args, complain, in_decl,
20619 index_exp_list);
20620
20621 tree r;
20622 if (vec_safe_length (index_exp_list) == 1
20623 && !PACK_EXPANSION_P (index_exp_list[0]))
20624 r = grok_array_decl (EXPR_LOCATION (t), op1,
20625 index_exp_list[0], NULL,
20626 complain | decltype_flag);
20627 else
20628 r = grok_array_decl (EXPR_LOCATION (t), op1,
20629 NULL_TREE, &index_exp_list,
20630 complain | decltype_flag);
20631 RETURN (r);
20632 }
20633 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
20634 RECUR (TREE_OPERAND (t, 1)),
20635 complain|decltype_flag));
20636
20637 case SIZEOF_EXPR:
20638 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
20639 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
20640 RETURN (tsubst_copy (t, args, complain, in_decl));
20641 /* Fall through */
20642
20643 case ALIGNOF_EXPR:
20644 {
20645 tree r;
20646
20647 op1 = TREE_OPERAND (t, 0);
20648 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
20649 op1 = TREE_TYPE (op1);
20650 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
20651 && ALIGNOF_EXPR_STD_P (t));
20652 if (!args)
20653 {
20654 /* When there are no ARGS, we are trying to evaluate a
20655 non-dependent expression from the parser. Trying to do
20656 the substitutions may not work. */
20657 if (!TYPE_P (op1))
20658 op1 = TREE_TYPE (op1);
20659 }
20660 else
20661 {
20662 ++cp_unevaluated_operand;
20663 ++c_inhibit_evaluation_warnings;
20664 if (TYPE_P (op1))
20665 op1 = tsubst (op1, args, complain, in_decl);
20666 else
20667 op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
20668 --cp_unevaluated_operand;
20669 --c_inhibit_evaluation_warnings;
20670 }
20671 if (TYPE_P (op1))
20672 r = cxx_sizeof_or_alignof_type (input_location,
20673 op1, TREE_CODE (t), std_alignof,
20674 complain & tf_error);
20675 else
20676 r = cxx_sizeof_or_alignof_expr (input_location,
20677 op1, TREE_CODE (t), std_alignof,
20678 complain & tf_error);
20679 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
20680 {
20681 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
20682 {
20683 if (!processing_template_decl && TYPE_P (op1))
20684 {
20685 r = build_min (SIZEOF_EXPR, size_type_node,
20686 build1 (NOP_EXPR, op1, error_mark_node));
20687 SIZEOF_EXPR_TYPE_P (r) = 1;
20688 }
20689 else
20690 r = build_min (SIZEOF_EXPR, size_type_node, op1);
20691 TREE_SIDE_EFFECTS (r) = 0;
20692 TREE_READONLY (r) = 1;
20693 }
20694 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
20695 }
20696 RETURN (r);
20697 }
20698
20699 case AT_ENCODE_EXPR:
20700 {
20701 op1 = TREE_OPERAND (t, 0);
20702 ++cp_unevaluated_operand;
20703 ++c_inhibit_evaluation_warnings;
20704 op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
20705 --cp_unevaluated_operand;
20706 --c_inhibit_evaluation_warnings;
20707 RETURN (objc_build_encode_expr (op1));
20708 }
20709
20710 case NOEXCEPT_EXPR:
20711 op1 = TREE_OPERAND (t, 0);
20712 ++cp_unevaluated_operand;
20713 ++c_inhibit_evaluation_warnings;
20714 ++cp_noexcept_operand;
20715 op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
20716 --cp_unevaluated_operand;
20717 --c_inhibit_evaluation_warnings;
20718 --cp_noexcept_operand;
20719 RETURN (finish_noexcept_expr (op1, complain));
20720
20721 case MODOP_EXPR:
20722 {
20723 warning_sentinel s(warn_div_by_zero);
20724 tree lhs = RECUR (TREE_OPERAND (t, 0));
20725 tree rhs = RECUR (TREE_OPERAND (t, 2));
20726
20727 tree r = build_x_modify_expr
20728 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
20729 templated_operator_saved_lookups (t),
20730 complain|decltype_flag);
20731 /* TREE_NO_WARNING must be set if either the expression was
20732 parenthesized or it uses an operator such as >>= rather
20733 than plain assignment. In the former case, it was already
20734 set and must be copied. In the latter case,
20735 build_x_modify_expr sets it and it must not be reset
20736 here. */
20737 if (warning_suppressed_p (t, OPT_Wparentheses))
20738 suppress_warning (r, OPT_Wparentheses);
20739
20740 RETURN (r);
20741 }
20742
20743 case ARROW_EXPR:
20744 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20745 args, complain, in_decl);
20746 /* Remember that there was a reference to this entity. */
20747 if (DECL_P (op1)
20748 && !mark_used (op1, complain) && !(complain & tf_error))
20749 RETURN (error_mark_node);
20750 RETURN (build_x_arrow (input_location, op1, complain));
20751
20752 case NEW_EXPR:
20753 {
20754 tree placement = RECUR (TREE_OPERAND (t, 0));
20755 tree init = RECUR (TREE_OPERAND (t, 3));
20756 vec<tree, va_gc> *placement_vec;
20757 vec<tree, va_gc> *init_vec;
20758 tree ret;
20759 location_t loc = EXPR_LOCATION (t);
20760
20761 if (placement == NULL_TREE)
20762 placement_vec = NULL;
20763 else if (placement == error_mark_node)
20764 RETURN (error_mark_node);
20765 else
20766 {
20767 placement_vec = make_tree_vector ();
20768 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20769 vec_safe_push (placement_vec, TREE_VALUE (placement));
20770 }
20771
20772 /* If there was an initializer in the original tree, but it
20773 instantiated to an empty list, then we should pass a
20774 non-NULL empty vector to tell build_new that it was an
20775 empty initializer() rather than no initializer. This can
20776 only happen when the initializer is a pack expansion whose
20777 parameter packs are of length zero. */
20778 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20779 init_vec = NULL;
20780 else if (init == error_mark_node)
20781 RETURN (error_mark_node);
20782 else
20783 {
20784 init_vec = make_tree_vector ();
20785 if (init == void_node)
20786 gcc_assert (init_vec != NULL);
20787 else
20788 {
20789 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20790 vec_safe_push (init_vec, TREE_VALUE (init));
20791 }
20792 }
20793
20794 /* Avoid passing an enclosing decl to valid_array_size_p. */
20795 in_decl = NULL_TREE;
20796
20797 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20798 tree op2 = RECUR (TREE_OPERAND (t, 2));
20799 ret = build_new (loc, &placement_vec, op1, op2,
20800 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20801 complain);
20802
20803 if (placement_vec != NULL)
20804 release_tree_vector (placement_vec);
20805 if (init_vec != NULL)
20806 release_tree_vector (init_vec);
20807
20808 RETURN (ret);
20809 }
20810
20811 case DELETE_EXPR:
20812 {
20813 tree op0 = RECUR (TREE_OPERAND (t, 0));
20814 tree op1 = RECUR (TREE_OPERAND (t, 1));
20815 RETURN (delete_sanity (input_location, op0, op1,
20816 DELETE_EXPR_USE_VEC (t),
20817 DELETE_EXPR_USE_GLOBAL (t),
20818 complain));
20819 }
20820
20821 case COMPOUND_EXPR:
20822 {
20823 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20824 complain & ~tf_decltype, in_decl);
20825 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20826 op0,
20827 RECUR (TREE_OPERAND (t, 1)),
20828 templated_operator_saved_lookups (t),
20829 complain|decltype_flag));
20830 }
20831
20832 case CALL_EXPR:
20833 {
20834 tree function;
20835 unsigned int nargs;
20836 bool qualified_p;
20837 bool koenig_p;
20838 tree ret;
20839
20840 function = CALL_EXPR_FN (t);
20841 /* Internal function with no arguments. */
20842 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20843 RETURN (t);
20844
20845 /* When we parsed the expression, we determined whether or
20846 not Koenig lookup should be performed. */
20847 koenig_p = KOENIG_LOOKUP_P (t);
20848 if (function == NULL_TREE)
20849 {
20850 koenig_p = false;
20851 qualified_p = false;
20852 }
20853 else if (TREE_CODE (function) == SCOPE_REF)
20854 {
20855 qualified_p = true;
20856 function = tsubst_qualified_id (function, args, complain, in_decl,
20857 /*done=*/false,
20858 /*address_p=*/false);
20859 }
20860 else if (koenig_p
20861 && (identifier_p (function)
20862 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20863 && identifier_p (TREE_OPERAND (function, 0)))))
20864 {
20865 /* Do nothing; calling tsubst_copy_and_build on an identifier
20866 would incorrectly perform unqualified lookup again.
20867
20868 Note that we can also have an IDENTIFIER_NODE if the earlier
20869 unqualified lookup found a dependent local extern declaration
20870 (as per finish_call_expr); in that case koenig_p will be false
20871 and we do want to do the lookup again to find the substituted
20872 declaration. */
20873 qualified_p = false;
20874
20875 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20876 /* Use tsubst_copy to substitute through the template arguments
20877 of the template-id without performing unqualified lookup of
20878 the template name. */
20879 function = tsubst_copy (function, args, complain, in_decl);
20880 }
20881 else
20882 {
20883 if (TREE_CODE (function) == COMPONENT_REF)
20884 {
20885 tree op = TREE_OPERAND (function, 1);
20886
20887 qualified_p = (TREE_CODE (op) == SCOPE_REF
20888 || (BASELINK_P (op)
20889 && BASELINK_QUALIFIED_P (op)));
20890 }
20891 else
20892 qualified_p = false;
20893
20894 if (TREE_CODE (function) == ADDR_EXPR
20895 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20896 /* Avoid error about taking the address of a constructor. */
20897 function = TREE_OPERAND (function, 0);
20898
20899 tsubst_flags_t subcomplain = complain;
20900 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20901 /* When KOENIG_P, we don't want to mark_used the callee before
20902 augmenting the overload set via ADL, so during this initial
20903 substitution we disable mark_used by setting tf_conv (68942). */
20904 subcomplain |= tf_conv;
20905 function = tsubst_copy_and_build (function, args, subcomplain, in_decl);
20906
20907 if (BASELINK_P (function))
20908 qualified_p = true;
20909 }
20910
20911 nargs = call_expr_nargs (t);
20912 releasing_vec call_args;
20913 tsubst_copy_and_build_call_args (t, args, complain, in_decl,
20914 call_args);
20915
20916 /* Stripped-down processing for a call in a thunk. Specifically, in
20917 the thunk template for a generic lambda. */
20918 if (call_from_lambda_thunk_p (t))
20919 {
20920 /* Now that we've expanded any packs, the number of call args
20921 might be different. */
20922 unsigned int cargs = call_args->length ();
20923 tree thisarg = NULL_TREE;
20924 if (TREE_CODE (function) == COMPONENT_REF)
20925 {
20926 thisarg = TREE_OPERAND (function, 0);
20927 if (TREE_CODE (thisarg) == INDIRECT_REF)
20928 thisarg = TREE_OPERAND (thisarg, 0);
20929 function = TREE_OPERAND (function, 1);
20930 if (TREE_CODE (function) == BASELINK)
20931 function = BASELINK_FUNCTIONS (function);
20932 }
20933 /* We aren't going to do normal overload resolution, so force the
20934 template-id to resolve. */
20935 function = resolve_nondeduced_context (function, complain);
20936 for (unsigned i = 0; i < cargs; ++i)
20937 {
20938 /* In a thunk, pass through args directly, without any
20939 conversions. */
20940 tree arg = (*call_args)[i];
20941 while (TREE_CODE (arg) != PARM_DECL)
20942 arg = TREE_OPERAND (arg, 0);
20943 (*call_args)[i] = arg;
20944 }
20945 if (thisarg)
20946 {
20947 /* If there are no other args, just push 'this'. */
20948 if (cargs == 0)
20949 vec_safe_push (call_args, thisarg);
20950 else
20951 {
20952 /* Otherwise, shift the other args over to make room. */
20953 tree last = (*call_args)[cargs - 1];
20954 vec_safe_push (call_args, last);
20955 for (int i = cargs - 1; i > 0; --i)
20956 (*call_args)[i] = (*call_args)[i - 1];
20957 (*call_args)[0] = thisarg;
20958 }
20959 }
20960 ret = build_call_a (function, call_args->length (),
20961 call_args->address ());
20962 /* The thunk location is not interesting. */
20963 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20964 CALL_FROM_THUNK_P (ret) = true;
20965 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20966 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20967
20968 RETURN (ret);
20969 }
20970
20971 /* We do not perform argument-dependent lookup if normal
20972 lookup finds a non-function, in accordance with the
20973 resolution of DR 218. */
20974 if (koenig_p
20975 && ((is_overloaded_fn (function)
20976 /* If lookup found a member function, the Koenig lookup is
20977 not appropriate, even if an unqualified-name was used
20978 to denote the function. */
20979 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20980 || identifier_p (function)
20981 /* C++20 P0846: Lookup found nothing. */
20982 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20983 && identifier_p (TREE_OPERAND (function, 0))))
20984 /* Only do this when substitution turns a dependent call
20985 into a non-dependent call. */
20986 && type_dependent_expression_p_push (t)
20987 && !any_type_dependent_arguments_p (call_args))
20988 function = perform_koenig_lookup (function, call_args, tf_none);
20989
20990 if (function != NULL_TREE
20991 && (identifier_p (function)
20992 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20993 && identifier_p (TREE_OPERAND (function, 0))
20994 && !any_dependent_template_arguments_p (TREE_OPERAND
20995 (function, 1))))
20996 && !any_type_dependent_arguments_p (call_args))
20997 {
20998 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
20999 if (template_id_p)
21000 function = TREE_OPERAND (function, 0);
21001 if (koenig_p && (complain & tf_warning_or_error))
21002 {
21003 /* For backwards compatibility and good diagnostics, try
21004 the unqualified lookup again if we aren't in SFINAE
21005 context. */
21006 tree unq = tsubst_copy_and_build (function, args,
21007 complain, in_decl);
21008 if (unq == error_mark_node)
21009 RETURN (error_mark_node);
21010
21011 if (unq != function)
21012 {
21013 char const *const msg
21014 = G_("%qD was not declared in this scope, "
21015 "and no declarations were found by "
21016 "argument-dependent lookup at the point "
21017 "of instantiation");
21018
21019 bool in_lambda = (current_class_type
21020 && LAMBDA_TYPE_P (current_class_type));
21021 /* In a lambda fn, we have to be careful to not
21022 introduce new this captures. Legacy code can't
21023 be using lambdas anyway, so it's ok to be
21024 stricter. Be strict with C++20 template-id ADL too.
21025 And be strict if we're already failing anyway. */
21026 bool strict = in_lambda || template_id_p || seen_error();
21027 bool diag = true;
21028 if (strict)
21029 error_at (cp_expr_loc_or_input_loc (t),
21030 msg, function);
21031 else
21032 diag = permerror (cp_expr_loc_or_input_loc (t),
21033 msg, function);
21034 if (diag)
21035 {
21036 tree fn = unq;
21037
21038 if (INDIRECT_REF_P (fn))
21039 fn = TREE_OPERAND (fn, 0);
21040 if (is_overloaded_fn (fn))
21041 fn = get_first_fn (fn);
21042
21043 if (!DECL_P (fn))
21044 /* Can't say anything more. */;
21045 else if (DECL_CLASS_SCOPE_P (fn))
21046 {
21047 location_t loc = cp_expr_loc_or_input_loc (t);
21048 inform (loc,
21049 "declarations in dependent base %qT are "
21050 "not found by unqualified lookup",
21051 DECL_CLASS_CONTEXT (fn));
21052 if (current_class_ptr)
21053 inform (loc,
21054 "use %<this->%D%> instead", function);
21055 else
21056 inform (loc,
21057 "use %<%T::%D%> instead",
21058 current_class_name, function);
21059 }
21060 else
21061 inform (DECL_SOURCE_LOCATION (fn),
21062 "%qD declared here, later in the "
21063 "translation unit", fn);
21064 if (strict)
21065 RETURN (error_mark_node);
21066 }
21067
21068 function = unq;
21069 }
21070 }
21071 if (identifier_p (function))
21072 {
21073 if (complain & tf_error)
21074 unqualified_name_lookup_error (function);
21075 RETURN (error_mark_node);
21076 }
21077 }
21078
21079 /* Remember that there was a reference to this entity. */
21080 if (function != NULL_TREE)
21081 {
21082 tree inner = function;
21083 if (TREE_CODE (inner) == ADDR_EXPR
21084 && TREE_CODE (TREE_OPERAND (inner, 0)) == FUNCTION_DECL)
21085 /* We should already have called mark_used when taking the
21086 address of this function, but do so again anyway to make
21087 sure it's odr-used: at worst this is a no-op, but if we
21088 obtained this FUNCTION_DECL as part of ahead-of-time overload
21089 resolution then that call to mark_used wouldn't have marked it
21090 odr-used yet (53164). */
21091 inner = TREE_OPERAND (inner, 0);
21092 if (DECL_P (inner)
21093 && !mark_used (inner, complain) && !(complain & tf_error))
21094 RETURN (error_mark_node);
21095 }
21096
21097 if (!maybe_fold_fn_template_args (function, complain))
21098 return error_mark_node;
21099
21100 /* Put back tf_decltype for the actual call. */
21101 complain |= decltype_flag;
21102
21103 if (function == NULL_TREE)
21104 switch (CALL_EXPR_IFN (t))
21105 {
21106 case IFN_LAUNDER:
21107 gcc_assert (nargs == 1);
21108 if (vec_safe_length (call_args) != 1)
21109 {
21110 error_at (cp_expr_loc_or_input_loc (t),
21111 "wrong number of arguments to "
21112 "%<__builtin_launder%>");
21113 ret = error_mark_node;
21114 }
21115 else
21116 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
21117 (*call_args)[0], complain);
21118 break;
21119
21120 case IFN_VEC_CONVERT:
21121 gcc_assert (nargs == 1);
21122 if (vec_safe_length (call_args) != 1)
21123 {
21124 error_at (cp_expr_loc_or_input_loc (t),
21125 "wrong number of arguments to "
21126 "%<__builtin_convertvector%>");
21127 ret = error_mark_node;
21128 break;
21129 }
21130 ret = cp_build_vec_convert ((*call_args)[0], input_location,
21131 tsubst (TREE_TYPE (t), args,
21132 complain, in_decl),
21133 complain);
21134 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
21135 RETURN (ret);
21136 break;
21137
21138 case IFN_SHUFFLEVECTOR:
21139 {
21140 ret = build_x_shufflevector (input_location, call_args,
21141 complain);
21142 if (ret != error_mark_node)
21143 RETURN (ret);
21144 break;
21145 }
21146
21147 case IFN_ASSUME:
21148 gcc_assert (nargs == 1);
21149 if (vec_safe_length (call_args) != 1)
21150 {
21151 error_at (cp_expr_loc_or_input_loc (t),
21152 "wrong number of arguments to "
21153 "%<assume%> attribute");
21154 ret = error_mark_node;
21155 }
21156 else
21157 {
21158 tree &arg = (*call_args)[0];
21159 if (!type_dependent_expression_p (arg))
21160 arg = contextual_conv_bool (arg, tf_warning_or_error);
21161 if (error_operand_p (arg))
21162 {
21163 ret = error_mark_node;
21164 break;
21165 }
21166 ret = build_assume_call (EXPR_LOCATION (t), arg);
21167 RETURN (ret);
21168 }
21169 break;
21170
21171 default:
21172 /* Unsupported internal function with arguments. */
21173 gcc_unreachable ();
21174 }
21175 else if (TREE_CODE (function) == OFFSET_REF
21176 || TREE_CODE (function) == DOTSTAR_EXPR
21177 || TREE_CODE (function) == MEMBER_REF)
21178 ret = build_offset_ref_call_from_tree (function, &call_args,
21179 complain);
21180 else if (TREE_CODE (function) == COMPONENT_REF)
21181 {
21182 tree instance = TREE_OPERAND (function, 0);
21183 tree fn = TREE_OPERAND (function, 1);
21184
21185 if (processing_template_decl
21186 && (type_dependent_expression_p (instance)
21187 || (!BASELINK_P (fn)
21188 && TREE_CODE (fn) != FIELD_DECL)
21189 || type_dependent_expression_p (fn)
21190 || any_type_dependent_arguments_p (call_args)))
21191 ret = build_min_nt_call_vec (function, call_args);
21192 else if (!BASELINK_P (fn))
21193 ret = finish_call_expr (function, &call_args,
21194 /*disallow_virtual=*/false,
21195 /*koenig_p=*/false,
21196 complain);
21197 else
21198 ret = (build_new_method_call
21199 (instance, fn,
21200 &call_args, NULL_TREE,
21201 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
21202 /*fn_p=*/NULL,
21203 complain));
21204 }
21205 else if (concept_check_p (function))
21206 {
21207 /* FUNCTION is a template-id referring to a concept definition. */
21208 tree id = unpack_concept_check (function);
21209 tree tmpl = TREE_OPERAND (id, 0);
21210 tree args = TREE_OPERAND (id, 1);
21211
21212 /* Calls to standard and variable concepts should have been
21213 previously diagnosed. */
21214 gcc_assert (function_concept_p (tmpl));
21215
21216 /* Ensure the result is wrapped as a call expression. */
21217 ret = build_concept_check (tmpl, args, tf_warning_or_error);
21218 }
21219 else
21220 ret = finish_call_expr (function, &call_args,
21221 /*disallow_virtual=*/qualified_p,
21222 koenig_p,
21223 complain);
21224
21225 if (ret != error_mark_node)
21226 {
21227 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
21228 bool ord = CALL_EXPR_ORDERED_ARGS (t);
21229 bool rev = CALL_EXPR_REVERSE_ARGS (t);
21230 if (op || ord || rev)
21231 if (tree call = extract_call_expr (ret))
21232 {
21233 CALL_EXPR_OPERATOR_SYNTAX (call) = op;
21234 CALL_EXPR_ORDERED_ARGS (call) = ord;
21235 CALL_EXPR_REVERSE_ARGS (call) = rev;
21236 }
21237 if (warning_suppressed_p (t, OPT_Wpessimizing_move))
21238 /* This also suppresses -Wredundant-move. */
21239 suppress_warning (ret, OPT_Wpessimizing_move);
21240 }
21241
21242 RETURN (ret);
21243 }
21244
21245 case COND_EXPR:
21246 {
21247 tree cond = RECUR (TREE_OPERAND (t, 0));
21248 cond = mark_rvalue_use (cond);
21249 tree folded_cond = fold_non_dependent_expr (cond, complain);
21250 tree exp1, exp2;
21251
21252 if (TREE_CODE (folded_cond) == INTEGER_CST)
21253 {
21254 if (integer_zerop (folded_cond))
21255 {
21256 ++c_inhibit_evaluation_warnings;
21257 exp1 = RECUR (TREE_OPERAND (t, 1));
21258 --c_inhibit_evaluation_warnings;
21259 exp2 = RECUR (TREE_OPERAND (t, 2));
21260 }
21261 else
21262 {
21263 exp1 = RECUR (TREE_OPERAND (t, 1));
21264 ++c_inhibit_evaluation_warnings;
21265 exp2 = RECUR (TREE_OPERAND (t, 2));
21266 --c_inhibit_evaluation_warnings;
21267 }
21268 cond = folded_cond;
21269 }
21270 else
21271 {
21272 exp1 = RECUR (TREE_OPERAND (t, 1));
21273 exp2 = RECUR (TREE_OPERAND (t, 2));
21274 }
21275
21276 warning_sentinel s(warn_duplicated_branches);
21277 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
21278 cond, exp1, exp2, complain));
21279 }
21280
21281 case PSEUDO_DTOR_EXPR:
21282 {
21283 tree op0 = RECUR (TREE_OPERAND (t, 0));
21284 tree op1 = RECUR (TREE_OPERAND (t, 1));
21285 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
21286 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
21287 input_location));
21288 }
21289
21290 case TREE_LIST:
21291 RETURN (tsubst_tree_list (t, args, complain, in_decl));
21292
21293 case COMPONENT_REF:
21294 {
21295 tree object;
21296 tree object_type;
21297 tree member;
21298 tree r;
21299
21300 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
21301 args, complain, in_decl);
21302 /* Remember that there was a reference to this entity. */
21303 if (DECL_P (object)
21304 && !mark_used (object, complain) && !(complain & tf_error))
21305 RETURN (error_mark_node);
21306 object_type = TREE_TYPE (object);
21307
21308 member = TREE_OPERAND (t, 1);
21309 if (BASELINK_P (member))
21310 member = tsubst_baselink (member,
21311 non_reference (TREE_TYPE (object)),
21312 args, complain, in_decl);
21313 else
21314 member = tsubst_copy (member, args, complain, in_decl);
21315 if (member == error_mark_node)
21316 RETURN (error_mark_node);
21317
21318 if (object_type && TYPE_PTRMEMFUNC_P (object_type)
21319 && TREE_CODE (member) == FIELD_DECL)
21320 {
21321 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
21322 RETURN (r);
21323 }
21324 else if (TREE_CODE (member) == FIELD_DECL)
21325 {
21326 r = finish_non_static_data_member (member, object, NULL_TREE,
21327 complain);
21328 if (TREE_CODE (r) == COMPONENT_REF)
21329 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
21330 RETURN (r);
21331 }
21332 else if (type_dependent_expression_p (object))
21333 /* We can't do much here. */;
21334 else if (!CLASS_TYPE_P (object_type))
21335 {
21336 if (scalarish_type_p (object_type))
21337 {
21338 tree s = NULL_TREE;
21339 tree dtor = member;
21340
21341 if (TREE_CODE (dtor) == SCOPE_REF)
21342 {
21343 s = TREE_OPERAND (dtor, 0);
21344 dtor = TREE_OPERAND (dtor, 1);
21345 }
21346 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
21347 {
21348 dtor = TREE_OPERAND (dtor, 0);
21349 if (TYPE_P (dtor))
21350 RETURN (finish_pseudo_destructor_expr
21351 (object, s, dtor, input_location));
21352 }
21353 }
21354 }
21355 else if (TREE_CODE (member) == SCOPE_REF
21356 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
21357 {
21358 /* Lookup the template functions now that we know what the
21359 scope is. */
21360 tree scope = TREE_OPERAND (member, 0);
21361 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
21362 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
21363 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
21364 /*complain=*/false);
21365 if (BASELINK_P (member))
21366 {
21367 BASELINK_FUNCTIONS (member)
21368 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
21369 args);
21370 member = (adjust_result_of_qualified_name_lookup
21371 (member, BINFO_TYPE (BASELINK_BINFO (member)),
21372 object_type));
21373 }
21374 else
21375 {
21376 qualified_name_lookup_error (scope, tmpl, member,
21377 input_location);
21378 RETURN (error_mark_node);
21379 }
21380 }
21381 else if (TREE_CODE (member) == SCOPE_REF
21382 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
21383 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
21384 {
21385 if (complain & tf_error)
21386 {
21387 if (TYPE_P (TREE_OPERAND (member, 0)))
21388 error ("%qT is not a class or namespace",
21389 TREE_OPERAND (member, 0));
21390 else
21391 error ("%qD is not a class or namespace",
21392 TREE_OPERAND (member, 0));
21393 }
21394 RETURN (error_mark_node);
21395 }
21396
21397 r = finish_class_member_access_expr (object, member,
21398 /*template_p=*/false,
21399 complain);
21400 if (TREE_CODE (r) == COMPONENT_REF)
21401 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
21402 RETURN (r);
21403 }
21404
21405 case THROW_EXPR:
21406 RETURN (build_throw
21407 (input_location, RECUR (TREE_OPERAND (t, 0))));
21408
21409 case CONSTRUCTOR:
21410 {
21411 vec<constructor_elt, va_gc> *n;
21412 constructor_elt *ce;
21413 unsigned HOST_WIDE_INT idx;
21414 bool process_index_p;
21415 int newlen;
21416 bool need_copy_p = false;
21417 tree r;
21418
21419 tsubst_flags_t tcomplain = complain;
21420 if (COMPOUND_LITERAL_P (t))
21421 tcomplain |= tf_tst_ok;
21422 tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
21423 if (type == error_mark_node)
21424 RETURN (error_mark_node);
21425
21426 /* We do not want to process the index of aggregate
21427 initializers as they are identifier nodes which will be
21428 looked up by digest_init. */
21429 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
21430
21431 if (null_member_pointer_value_p (t))
21432 {
21433 gcc_assert (same_type_p (type, TREE_TYPE (t)));
21434 RETURN (t);
21435 }
21436
21437 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
21438 newlen = vec_safe_length (n);
21439 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
21440 {
21441 if (ce->index && process_index_p
21442 /* An identifier index is looked up in the type
21443 being initialized, not the current scope. */
21444 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
21445 ce->index = RECUR (ce->index);
21446
21447 if (PACK_EXPANSION_P (ce->value))
21448 {
21449 /* Substitute into the pack expansion. */
21450 ce->value = tsubst_pack_expansion (ce->value, args, complain,
21451 in_decl);
21452
21453 if (ce->value == error_mark_node
21454 || PACK_EXPANSION_P (ce->value))
21455 ;
21456 else if (TREE_VEC_LENGTH (ce->value) == 1)
21457 /* Just move the argument into place. */
21458 ce->value = TREE_VEC_ELT (ce->value, 0);
21459 else
21460 {
21461 /* Update the length of the final CONSTRUCTOR
21462 arguments vector, and note that we will need to
21463 copy.*/
21464 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
21465 need_copy_p = true;
21466 }
21467 }
21468 else
21469 ce->value = RECUR (ce->value);
21470 }
21471
21472 if (need_copy_p)
21473 {
21474 vec<constructor_elt, va_gc> *old_n = n;
21475
21476 vec_alloc (n, newlen);
21477 FOR_EACH_VEC_ELT (*old_n, idx, ce)
21478 {
21479 if (TREE_CODE (ce->value) == TREE_VEC)
21480 {
21481 int i, len = TREE_VEC_LENGTH (ce->value);
21482 for (i = 0; i < len; ++i)
21483 CONSTRUCTOR_APPEND_ELT (n, 0,
21484 TREE_VEC_ELT (ce->value, i));
21485 }
21486 else
21487 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
21488 }
21489 }
21490
21491 r = build_constructor (init_list_type_node, n);
21492 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
21493 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
21494 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
21495
21496 if (TREE_HAS_CONSTRUCTOR (t))
21497 {
21498 fcl_t cl = fcl_functional;
21499 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
21500 cl = fcl_c99;
21501 RETURN (finish_compound_literal (type, r, complain, cl));
21502 }
21503
21504 TREE_TYPE (r) = type;
21505 RETURN (r);
21506 }
21507
21508 case TYPEID_EXPR:
21509 {
21510 tree operand_0 = TREE_OPERAND (t, 0);
21511 if (TYPE_P (operand_0))
21512 {
21513 operand_0 = tsubst (operand_0, args, complain, in_decl);
21514 RETURN (get_typeid (operand_0, complain));
21515 }
21516 else
21517 {
21518 operand_0 = RECUR (operand_0);
21519 RETURN (build_typeid (operand_0, complain));
21520 }
21521 }
21522
21523 case VAR_DECL:
21524 if (!args)
21525 RETURN (t);
21526 /* Fall through */
21527
21528 case PARM_DECL:
21529 {
21530 tree r = tsubst_copy (t, args, complain, in_decl);
21531 /* ??? We're doing a subset of finish_id_expression here. */
21532 if (tree wrap = maybe_get_tls_wrapper_call (r))
21533 /* Replace an evaluated use of the thread_local variable with
21534 a call to its wrapper. */
21535 r = wrap;
21536 else if (outer_automatic_var_p (r))
21537 r = process_outer_var_ref (r, complain);
21538
21539 if (!TYPE_REF_P (TREE_TYPE (t)))
21540 /* If the original type was a reference, we'll be wrapped in
21541 the appropriate INDIRECT_REF. */
21542 r = convert_from_reference (r);
21543 RETURN (r);
21544 }
21545
21546 case VA_ARG_EXPR:
21547 {
21548 tree op0 = RECUR (TREE_OPERAND (t, 0));
21549 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21550 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
21551 }
21552
21553 case OFFSETOF_EXPR:
21554 {
21555 tree object_ptr
21556 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
21557 complain, in_decl);
21558 RETURN (finish_offsetof (object_ptr,
21559 RECUR (TREE_OPERAND (t, 0)),
21560 EXPR_LOCATION (t)));
21561 }
21562
21563 case ADDRESSOF_EXPR:
21564 RETURN (cp_build_addressof (EXPR_LOCATION (t),
21565 RECUR (TREE_OPERAND (t, 0)), complain));
21566
21567 case TRAIT_EXPR:
21568 {
21569 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
21570 complain, in_decl);
21571 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
21572 complain, in_decl);
21573 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
21574 TRAIT_EXPR_KIND (t), type1, type2));
21575 }
21576
21577 case STMT_EXPR:
21578 {
21579 tree old_stmt_expr = cur_stmt_expr;
21580 tree stmt_expr = begin_stmt_expr ();
21581
21582 cur_stmt_expr = stmt_expr;
21583 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
21584 stmt_expr = finish_stmt_expr (stmt_expr, false);
21585 cur_stmt_expr = old_stmt_expr;
21586
21587 /* If the resulting list of expression statement is empty,
21588 fold it further into void_node. */
21589 if (empty_expr_stmt_p (stmt_expr))
21590 stmt_expr = void_node;
21591
21592 RETURN (stmt_expr);
21593 }
21594
21595 case LAMBDA_EXPR:
21596 {
21597 if (complain & tf_partial)
21598 {
21599 /* We don't have a full set of template arguments yet; don't touch
21600 the lambda at all. */
21601 gcc_assert (processing_template_decl);
21602 return t;
21603 }
21604 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
21605
21606 RETURN (build_lambda_object (r));
21607 }
21608
21609 case TRANSACTION_EXPR:
21610 RETURN (tsubst_expr (t, args, complain, in_decl));
21611
21612 case PAREN_EXPR:
21613 if (REF_PARENTHESIZED_P (t))
21614 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
21615 else
21616 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */
21617 {
21618 tree op0 = RECUR (TREE_OPERAND (t, 0));
21619 RETURN (build1_loc (input_location, PAREN_EXPR,
21620 TREE_TYPE (op0), op0));
21621 }
21622
21623 case VEC_PERM_EXPR:
21624 {
21625 tree op0 = RECUR (TREE_OPERAND (t, 0));
21626 tree op1 = RECUR (TREE_OPERAND (t, 1));
21627 tree op2 = RECUR (TREE_OPERAND (t, 2));
21628 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
21629 complain));
21630 }
21631
21632 case REQUIRES_EXPR:
21633 {
21634 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
21635 RETURN (r);
21636 }
21637
21638 case RANGE_EXPR:
21639 /* No need to substitute further, a RANGE_EXPR will always be built
21640 with constant operands. */
21641 RETURN (t);
21642
21643 case NON_LVALUE_EXPR:
21644 case VIEW_CONVERT_EXPR:
21645 {
21646 tree op = RECUR (TREE_OPERAND (t, 0));
21647
21648 if (location_wrapper_p (t))
21649 /* We need to do this here as well as in tsubst_copy so we get the
21650 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
21651 RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
21652
21653 gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
21654 if (REF_PARENTHESIZED_P (t))
21655 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
21656 RETURN (finish_parenthesized_expr (op));
21657
21658 /* Otherwise, we're dealing with a wrapper to make a C++20 template
21659 parameter object const. */
21660 if (TREE_TYPE (op) == NULL_TREE
21661 || !CP_TYPE_CONST_P (TREE_TYPE (op)))
21662 {
21663 /* The template argument is not const, presumably because
21664 it is still dependent, and so not the const template parm
21665 object. */
21666 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
21667 if (TREE_CODE (op) == CONSTRUCTOR
21668 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
21669 {
21670 /* Don't add a wrapper to these. */
21671 op = copy_node (op);
21672 TREE_TYPE (op) = type;
21673 }
21674 else
21675 /* Do add a wrapper otherwise (in particular, if op is
21676 another TEMPLATE_PARM_INDEX). */
21677 op = build1 (VIEW_CONVERT_EXPR, type, op);
21678 }
21679 RETURN (op);
21680 }
21681
21682 default:
21683 /* Handle Objective-C++ constructs, if appropriate. */
21684 {
21685 tree subst
21686 = objcp_tsubst_copy_and_build (t, args, complain, in_decl);
21687 if (subst)
21688 RETURN (subst);
21689 }
21690 RETURN (tsubst_copy (t, args, complain, in_decl));
21691 }
21692
21693 #undef RECUR
21694 #undef RETURN
21695 out:
21696 input_location = save_loc;
21697 return retval;
21698 }
21699
21700 /* Verify that the instantiated ARGS are valid. For type arguments,
21701 make sure that the type's linkage is ok. For non-type arguments,
21702 make sure they are constants if they are integral or enumerations.
21703 Emit an error under control of COMPLAIN, and return TRUE on error. */
21704
21705 static bool
21706 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
21707 {
21708 if (dependent_template_arg_p (t))
21709 return false;
21710 if (ARGUMENT_PACK_P (t))
21711 {
21712 tree vec = ARGUMENT_PACK_ARGS (t);
21713 int len = TREE_VEC_LENGTH (vec);
21714 bool result = false;
21715 int i;
21716
21717 for (i = 0; i < len; ++i)
21718 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
21719 result = true;
21720 return result;
21721 }
21722 else if (TYPE_P (t))
21723 {
21724 /* [basic.link]: A name with no linkage (notably, the name
21725 of a class or enumeration declared in a local scope)
21726 shall not be used to declare an entity with linkage.
21727 This implies that names with no linkage cannot be used as
21728 template arguments
21729
21730 DR 757 relaxes this restriction for C++0x. */
21731 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
21732 : no_linkage_check (t, /*relaxed_p=*/false));
21733
21734 if (nt)
21735 {
21736 /* DR 488 makes use of a type with no linkage cause
21737 type deduction to fail. */
21738 if (complain & tf_error)
21739 {
21740 if (TYPE_UNNAMED_P (nt))
21741 error ("%qT is/uses unnamed type", t);
21742 else
21743 error ("template argument for %qD uses local type %qT",
21744 tmpl, t);
21745 }
21746 return true;
21747 }
21748 /* In order to avoid all sorts of complications, we do not
21749 allow variably-modified types as template arguments. */
21750 else if (variably_modified_type_p (t, NULL_TREE))
21751 {
21752 if (complain & tf_error)
21753 error ("%qT is a variably modified type", t);
21754 return true;
21755 }
21756 }
21757 /* Class template and alias template arguments should be OK. */
21758 else if (DECL_TYPE_TEMPLATE_P (t))
21759 ;
21760 /* A non-type argument of integral or enumerated type must be a
21761 constant. */
21762 else if (TREE_TYPE (t)
21763 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
21764 && !REFERENCE_REF_P (t)
21765 && !TREE_CONSTANT (t))
21766 {
21767 if (complain & tf_error)
21768 error ("integral expression %qE is not constant", t);
21769 return true;
21770 }
21771 return false;
21772 }
21773
21774 static bool
21775 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
21776 {
21777 int ix, len = DECL_NTPARMS (tmpl);
21778 bool result = false;
21779
21780 for (ix = 0; ix != len; ix++)
21781 {
21782 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
21783 result = true;
21784 }
21785 if (result && (complain & tf_error))
21786 error (" trying to instantiate %qD", tmpl);
21787 return result;
21788 }
21789
21790 /* We're out of SFINAE context now, so generate diagnostics for the access
21791 errors we saw earlier when instantiating D from TMPL and ARGS. */
21792
21793 static void
21794 recheck_decl_substitution (tree d, tree tmpl, tree args)
21795 {
21796 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
21797 tree type = TREE_TYPE (pattern);
21798 location_t loc = input_location;
21799
21800 push_access_scope (d);
21801 push_deferring_access_checks (dk_no_deferred);
21802 input_location = DECL_SOURCE_LOCATION (pattern);
21803 tsubst (type, args, tf_warning_or_error, d);
21804 input_location = loc;
21805 pop_deferring_access_checks ();
21806 pop_access_scope (d);
21807 }
21808
21809 /* Instantiate the indicated variable, function, or alias template TMPL with
21810 the template arguments in TARG_PTR. */
21811
21812 tree
21813 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21814 {
21815 auto_timevar tv (TV_TEMPLATE_INST);
21816
21817 tree targ_ptr = orig_args;
21818 tree fndecl;
21819 tree gen_tmpl;
21820 tree spec;
21821 bool access_ok = true;
21822
21823 if (tmpl == error_mark_node)
21824 return error_mark_node;
21825
21826 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
21827
21828 if (modules_p ())
21829 lazy_load_pendings (tmpl);
21830
21831 /* If this function is a clone, handle it specially. */
21832 if (DECL_CLONED_FUNCTION_P (tmpl))
21833 {
21834 tree spec;
21835 tree clone;
21836
21837 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21838 DECL_CLONED_FUNCTION. */
21839 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
21840 targ_ptr, complain);
21841 if (spec == error_mark_node)
21842 return error_mark_node;
21843
21844 /* Look for the clone. */
21845 FOR_EACH_CLONE (clone, spec)
21846 if (DECL_NAME (clone) == DECL_NAME (tmpl))
21847 return clone;
21848 /* We should always have found the clone by now. */
21849 gcc_unreachable ();
21850 return NULL_TREE;
21851 }
21852
21853 if (targ_ptr == error_mark_node)
21854 return error_mark_node;
21855
21856 /* Check to see if we already have this specialization. */
21857 gen_tmpl = most_general_template (tmpl);
21858 if (TMPL_ARGS_DEPTH (targ_ptr)
21859 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
21860 /* targ_ptr only has the innermost template args, so add the outer ones
21861 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21862 the case of a non-dependent call within a template definition). */
21863 targ_ptr = (add_outermost_template_args
21864 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
21865 targ_ptr));
21866
21867 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21868 but it doesn't seem to be on the hot path. */
21869 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
21870
21871 gcc_checking_assert (tmpl == gen_tmpl
21872 || ((fndecl
21873 = retrieve_specialization (tmpl, orig_args, 0))
21874 == spec)
21875 || fndecl == NULL_TREE);
21876
21877 if (spec != NULL_TREE)
21878 {
21879 if (FNDECL_HAS_ACCESS_ERRORS (spec))
21880 {
21881 if (complain & tf_error)
21882 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
21883 return error_mark_node;
21884 }
21885 return spec;
21886 }
21887
21888 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
21889 complain))
21890 return error_mark_node;
21891
21892 /* We are building a FUNCTION_DECL, during which the access of its
21893 parameters and return types have to be checked. However this
21894 FUNCTION_DECL which is the desired context for access checking
21895 is not built yet. We solve this chicken-and-egg problem by
21896 deferring all checks until we have the FUNCTION_DECL. */
21897 push_deferring_access_checks (dk_deferred);
21898
21899 /* Instantiation of the function happens in the context of the function
21900 template, not the context of the overload resolution we're doing. */
21901 push_to_top_level ();
21902 /* If there are dependent arguments, e.g. because we're doing partial
21903 ordering, make sure processing_template_decl stays set. */
21904 if (uses_template_parms (targ_ptr))
21905 ++processing_template_decl;
21906 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21907 {
21908 tree ctx;
21909 if (!uses_template_parms (DECL_CONTEXT (tmpl)))
21910 /* If the context of the partially instantiated template is
21911 already non-dependent, then we might as well use it. */
21912 ctx = DECL_CONTEXT (tmpl);
21913 else
21914 ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
21915 complain, gen_tmpl, true);
21916 push_nested_class (ctx);
21917 }
21918
21919 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
21920
21921 fndecl = NULL_TREE;
21922 if (VAR_P (pattern))
21923 {
21924 /* We need to determine if we're using a partial or explicit
21925 specialization now, because the type of the variable could be
21926 different. */
21927 tree tid = lookup_template_variable (tmpl, targ_ptr);
21928 tree elt = most_specialized_partial_spec (tid, complain);
21929 if (elt == error_mark_node)
21930 pattern = error_mark_node;
21931 else if (elt)
21932 {
21933 tree partial_tmpl = TREE_VALUE (elt);
21934 tree partial_args = TREE_PURPOSE (elt);
21935 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21936 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21937 }
21938 }
21939
21940 /* Substitute template parameters to obtain the specialization. */
21941 if (fndecl == NULL_TREE)
21942 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21943 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21944 pop_nested_class ();
21945 pop_from_top_level ();
21946
21947 if (fndecl == error_mark_node)
21948 {
21949 pop_deferring_access_checks ();
21950 return error_mark_node;
21951 }
21952
21953 /* The DECL_TI_TEMPLATE should always be the immediate parent
21954 template, not the most general template. */
21955 DECL_TI_TEMPLATE (fndecl) = tmpl;
21956 DECL_TI_ARGS (fndecl) = targ_ptr;
21957
21958 set_instantiating_module (fndecl);
21959
21960 /* Now we know the specialization, compute access previously
21961 deferred. Do no access control for inheriting constructors,
21962 as we already checked access for the inherited constructor. */
21963 if (!(flag_new_inheriting_ctors
21964 && DECL_INHERITED_CTOR (fndecl)))
21965 {
21966 push_access_scope (fndecl);
21967 if (!perform_deferred_access_checks (complain))
21968 access_ok = false;
21969 pop_access_scope (fndecl);
21970 }
21971 pop_deferring_access_checks ();
21972
21973 /* If we've just instantiated the main entry point for a function,
21974 instantiate all the alternate entry points as well. We do this
21975 by cloning the instantiation of the main entry point, not by
21976 instantiating the template clones. */
21977 if (tree chain = DECL_CHAIN (gen_tmpl))
21978 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21979 clone_cdtor (fndecl, /*update_methods=*/false);
21980
21981 if (!access_ok)
21982 {
21983 if (!(complain & tf_error))
21984 {
21985 /* Remember to reinstantiate when we're out of SFINAE so the user
21986 can see the errors. */
21987 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21988 }
21989 return error_mark_node;
21990 }
21991
21992 return fndecl;
21993 }
21994
21995 /* Instantiate the alias template TMPL with ARGS. Also push a template
21996 instantiation level, which instantiate_template doesn't do because
21997 functions and variables have sufficient context established by the
21998 callers. */
21999
22000 static tree
22001 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
22002 {
22003 if (tmpl == error_mark_node || args == error_mark_node)
22004 return error_mark_node;
22005
22006 args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
22007 args, tmpl, complain);
22008
22009 /* FIXME check for satisfaction in check_instantiated_args. */
22010 if (flag_concepts
22011 && !any_dependent_template_arguments_p (args)
22012 && !constraints_satisfied_p (tmpl, args))
22013 {
22014 if (complain & tf_error)
22015 {
22016 auto_diagnostic_group d;
22017 error ("template constraint failure for %qD", tmpl);
22018 diagnose_constraints (input_location, tmpl, args);
22019 }
22020 return error_mark_node;
22021 }
22022
22023 if (!push_tinst_level (tmpl, args))
22024 return error_mark_node;
22025 tree r = instantiate_template (tmpl, args, complain);
22026 pop_tinst_level ();
22027
22028 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
22029 {
22030 /* An alias template specialization can be dependent
22031 even if its underlying type is not. */
22032 TYPE_DEPENDENT_P (d) = true;
22033 TYPE_DEPENDENT_P_VALID (d) = true;
22034 /* Sometimes a dependent alias spec is equivalent to its expansion,
22035 sometimes not. So always use structural_comptypes. */
22036 SET_TYPE_STRUCTURAL_EQUALITY (d);
22037 }
22038
22039 return r;
22040 }
22041
22042 /* PARM is a template parameter pack for FN. Returns true iff
22043 PARM is used in a deducible way in the argument list of FN. */
22044
22045 static bool
22046 pack_deducible_p (tree parm, tree fn)
22047 {
22048 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
22049 for (; t; t = TREE_CHAIN (t))
22050 {
22051 tree type = TREE_VALUE (t);
22052 tree packs;
22053 if (!PACK_EXPANSION_P (type))
22054 continue;
22055 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
22056 packs; packs = TREE_CHAIN (packs))
22057 if (template_args_equal (TREE_VALUE (packs), parm))
22058 {
22059 /* The template parameter pack is used in a function parameter
22060 pack. If this is the end of the parameter list, the
22061 template parameter pack is deducible. */
22062 if (TREE_CHAIN (t) == void_list_node)
22063 return true;
22064 else
22065 /* Otherwise, not. Well, it could be deduced from
22066 a non-pack parameter, but doing so would end up with
22067 a deduction mismatch, so don't bother. */
22068 return false;
22069 }
22070 }
22071 /* The template parameter pack isn't used in any function parameter
22072 packs, but it might be used deeper, e.g. tuple<Args...>. */
22073 return true;
22074 }
22075
22076 /* Subroutine of fn_type_unification: check non-dependent parms for
22077 convertibility. */
22078
22079 static int
22080 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
22081 tree fn, unification_kind_t strict, int flags,
22082 struct conversion **convs, bool explain_p)
22083 {
22084 /* Non-constructor methods need to leave a conversion for 'this', which
22085 isn't included in nargs here. */
22086 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22087 && !DECL_CONSTRUCTOR_P (fn));
22088
22089 for (unsigned ia = 0;
22090 parms && parms != void_list_node && ia < nargs; )
22091 {
22092 tree parm = TREE_VALUE (parms);
22093
22094 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22095 && (!TREE_CHAIN (parms)
22096 || TREE_CHAIN (parms) == void_list_node))
22097 /* For a function parameter pack that occurs at the end of the
22098 parameter-declaration-list, the type A of each remaining
22099 argument of the call is compared with the type P of the
22100 declarator-id of the function parameter pack. */
22101 break;
22102
22103 parms = TREE_CHAIN (parms);
22104
22105 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22106 /* For a function parameter pack that does not occur at the
22107 end of the parameter-declaration-list, the type of the
22108 parameter pack is a non-deduced context. */
22109 continue;
22110
22111 if (!uses_template_parms (parm))
22112 {
22113 tree arg = args[ia];
22114 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
22115 int lflags = conv_flags (ia, nargs, fn, arg, flags);
22116
22117 if (check_non_deducible_conversion (parm, arg, strict, lflags,
22118 conv_p, explain_p))
22119 return 1;
22120 }
22121
22122 ++ia;
22123 }
22124
22125 return 0;
22126 }
22127
22128 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
22129 NARGS elements of the arguments that are being used when calling
22130 it. TARGS is a vector into which the deduced template arguments
22131 are placed.
22132
22133 Returns either a FUNCTION_DECL for the matching specialization of FN or
22134 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
22135 true, diagnostics will be printed to explain why it failed.
22136
22137 If FN is a conversion operator, or we are trying to produce a specific
22138 specialization, RETURN_TYPE is the return type desired.
22139
22140 The EXPLICIT_TARGS are explicit template arguments provided via a
22141 template-id.
22142
22143 The parameter STRICT is one of:
22144
22145 DEDUCE_CALL:
22146 We are deducing arguments for a function call, as in
22147 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
22148 deducing arguments for a call to the result of a conversion
22149 function template, as in [over.call.object].
22150
22151 DEDUCE_CONV:
22152 We are deducing arguments for a conversion function, as in
22153 [temp.deduct.conv].
22154
22155 DEDUCE_EXACT:
22156 We are deducing arguments when doing an explicit instantiation
22157 as in [temp.explicit], when determining an explicit specialization
22158 as in [temp.expl.spec], or when taking the address of a function
22159 template, as in [temp.deduct.funcaddr]. */
22160
22161 tree
22162 fn_type_unification (tree fn,
22163 tree explicit_targs,
22164 tree targs,
22165 const tree *args,
22166 unsigned int nargs,
22167 tree return_type,
22168 unification_kind_t strict,
22169 int flags,
22170 struct conversion **convs,
22171 bool explain_p,
22172 bool decltype_p)
22173 {
22174 tree parms;
22175 tree fntype;
22176 tree decl = NULL_TREE;
22177 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22178 bool ok;
22179 static int deduction_depth;
22180 /* type_unification_real will pass back any access checks from default
22181 template argument substitution. */
22182 vec<deferred_access_check, va_gc> *checks = NULL;
22183 /* We don't have all the template args yet. */
22184 bool incomplete = true;
22185
22186 tree orig_fn = fn;
22187 if (flag_new_inheriting_ctors)
22188 fn = strip_inheriting_ctors (fn);
22189
22190 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
22191 tree r = error_mark_node;
22192
22193 tree full_targs = targs;
22194 if (TMPL_ARGS_DEPTH (targs)
22195 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
22196 full_targs = (add_outermost_template_args
22197 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
22198 targs));
22199
22200 if (decltype_p)
22201 complain |= tf_decltype;
22202
22203 /* In C++0x, it's possible to have a function template whose type depends
22204 on itself recursively. This is most obvious with decltype, but can also
22205 occur with enumeration scope (c++/48969). So we need to catch infinite
22206 recursion and reject the substitution at deduction time; this function
22207 will return error_mark_node for any repeated substitution.
22208
22209 This also catches excessive recursion such as when f<N> depends on
22210 f<N-1> across all integers, and returns error_mark_node for all the
22211 substitutions back up to the initial one.
22212
22213 This is, of course, not reentrant. */
22214 if (excessive_deduction_depth)
22215 return error_mark_node;
22216 ++deduction_depth;
22217
22218 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
22219
22220 fntype = TREE_TYPE (fn);
22221 if (explicit_targs)
22222 {
22223 /* [temp.deduct]
22224
22225 The specified template arguments must match the template
22226 parameters in kind (i.e., type, nontype, template), and there
22227 must not be more arguments than there are parameters;
22228 otherwise type deduction fails.
22229
22230 Nontype arguments must match the types of the corresponding
22231 nontype template parameters, or must be convertible to the
22232 types of the corresponding nontype parameters as specified in
22233 _temp.arg.nontype_, otherwise type deduction fails.
22234
22235 All references in the function type of the function template
22236 to the corresponding template parameters are replaced by the
22237 specified template argument values. If a substitution in a
22238 template parameter or in the function type of the function
22239 template results in an invalid type, type deduction fails. */
22240 int i, len = TREE_VEC_LENGTH (tparms);
22241 location_t loc = input_location;
22242 incomplete = false;
22243
22244 if (explicit_targs == error_mark_node)
22245 goto fail;
22246
22247 if (TMPL_ARGS_DEPTH (explicit_targs)
22248 < TMPL_ARGS_DEPTH (full_targs))
22249 explicit_targs = add_outermost_template_args (full_targs,
22250 explicit_targs);
22251
22252 /* Adjust any explicit template arguments before entering the
22253 substitution context. */
22254 explicit_targs
22255 = (coerce_template_parms (tparms, explicit_targs, fn,
22256 complain|tf_partial,
22257 /*require_all_args=*/false));
22258 if (explicit_targs == error_mark_node)
22259 goto fail;
22260
22261 /* Substitute the explicit args into the function type. This is
22262 necessary so that, for instance, explicitly declared function
22263 arguments can match null pointed constants. If we were given
22264 an incomplete set of explicit args, we must not do semantic
22265 processing during substitution as we could create partial
22266 instantiations. */
22267 for (i = 0; i < len; i++)
22268 {
22269 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
22270 bool parameter_pack = false;
22271 tree targ = TREE_VEC_ELT (explicit_targs, i);
22272
22273 /* Dig out the actual parm. */
22274 if (TREE_CODE (parm) == TYPE_DECL
22275 || TREE_CODE (parm) == TEMPLATE_DECL)
22276 {
22277 parm = TREE_TYPE (parm);
22278 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
22279 }
22280 else if (TREE_CODE (parm) == PARM_DECL)
22281 {
22282 parm = DECL_INITIAL (parm);
22283 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
22284 }
22285
22286 if (targ == NULL_TREE)
22287 /* No explicit argument for this template parameter. */
22288 incomplete = true;
22289 else if (parameter_pack && pack_deducible_p (parm, fn))
22290 {
22291 /* Mark the argument pack as "incomplete". We could
22292 still deduce more arguments during unification.
22293 We remove this mark in type_unification_real. */
22294 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
22295 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
22296 = ARGUMENT_PACK_ARGS (targ);
22297
22298 /* We have some incomplete argument packs. */
22299 incomplete = true;
22300 }
22301 }
22302
22303 if (incomplete)
22304 {
22305 if (!push_tinst_level (fn, explicit_targs))
22306 {
22307 excessive_deduction_depth = true;
22308 goto fail;
22309 }
22310 ++processing_template_decl;
22311 input_location = DECL_SOURCE_LOCATION (fn);
22312 /* Ignore any access checks; we'll see them again in
22313 instantiate_template and they might have the wrong
22314 access path at this point. */
22315 push_deferring_access_checks (dk_deferred);
22316 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
22317 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
22318 pop_deferring_access_checks ();
22319 input_location = loc;
22320 --processing_template_decl;
22321 pop_tinst_level ();
22322
22323 if (fntype == error_mark_node)
22324 goto fail;
22325 }
22326
22327 /* Place the explicitly specified arguments in TARGS. */
22328 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
22329 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
22330 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
22331 if (!incomplete && CHECKING_P
22332 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22333 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
22334 (targs, NUM_TMPL_ARGS (explicit_targs));
22335 }
22336
22337 if (return_type && strict != DEDUCE_CALL)
22338 {
22339 tree *new_args = XALLOCAVEC (tree, nargs + 1);
22340 new_args[0] = return_type;
22341 memcpy (new_args + 1, args, nargs * sizeof (tree));
22342 args = new_args;
22343 ++nargs;
22344 }
22345
22346 if (!incomplete)
22347 goto deduced;
22348
22349 /* Never do unification on the 'this' parameter. */
22350 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
22351
22352 if (return_type && strict == DEDUCE_CALL)
22353 {
22354 /* We're deducing for a call to the result of a template conversion
22355 function. The parms we really want are in return_type. */
22356 if (INDIRECT_TYPE_P (return_type))
22357 return_type = TREE_TYPE (return_type);
22358 parms = TYPE_ARG_TYPES (return_type);
22359 }
22360 else if (return_type)
22361 {
22362 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
22363 }
22364
22365 /* We allow incomplete unification without an error message here
22366 because the standard doesn't seem to explicitly prohibit it. Our
22367 callers must be ready to deal with unification failures in any
22368 event. */
22369
22370 /* If we aren't explaining yet, push tinst context so we can see where
22371 any errors (e.g. from class instantiations triggered by instantiation
22372 of default template arguments) come from. If we are explaining, this
22373 context is redundant. */
22374 if (!explain_p && !push_tinst_level (fn, targs))
22375 {
22376 excessive_deduction_depth = true;
22377 goto fail;
22378 }
22379
22380 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22381 full_targs, parms, args, nargs, /*subr=*/0,
22382 strict, &checks, explain_p);
22383 if (!explain_p)
22384 pop_tinst_level ();
22385 if (!ok)
22386 goto fail;
22387
22388 /* Now that we have bindings for all of the template arguments,
22389 ensure that the arguments deduced for the template template
22390 parameters have compatible template parameter lists. We cannot
22391 check this property before we have deduced all template
22392 arguments, because the template parameter types of a template
22393 template parameter might depend on prior template parameters
22394 deduced after the template template parameter. The following
22395 ill-formed example illustrates this issue:
22396
22397 template<typename T, template<T> class C> void f(C<5>, T);
22398
22399 template<int N> struct X {};
22400
22401 void g() {
22402 f(X<5>(), 5l); // error: template argument deduction fails
22403 }
22404
22405 The template parameter list of 'C' depends on the template type
22406 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
22407 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
22408 time that we deduce 'C'. */
22409 if (!template_template_parm_bindings_ok_p
22410 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
22411 {
22412 unify_inconsistent_template_template_parameters (explain_p);
22413 goto fail;
22414 }
22415
22416 deduced:
22417
22418 /* CWG2369: Check satisfaction before non-deducible conversions. */
22419 if (!constraints_satisfied_p (fn, targs))
22420 {
22421 if (explain_p)
22422 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
22423 goto fail;
22424 }
22425
22426 /* DR 1391: All parameters have args, now check non-dependent parms for
22427 convertibility. We don't do this if all args were explicitly specified,
22428 as the standard says that we substitute explicit args immediately. */
22429 if (incomplete
22430 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
22431 convs, explain_p))
22432 goto fail;
22433
22434 /* All is well so far. Now, check:
22435
22436 [temp.deduct]
22437
22438 When all template arguments have been deduced, all uses of
22439 template parameters in nondeduced contexts are replaced with
22440 the corresponding deduced argument values. If the
22441 substitution results in an invalid type, as described above,
22442 type deduction fails. */
22443 if (!push_tinst_level (fn, targs))
22444 {
22445 excessive_deduction_depth = true;
22446 goto fail;
22447 }
22448
22449 /* Also collect access checks from the instantiation. */
22450 reopen_deferring_access_checks (checks);
22451
22452 decl = instantiate_template (fn, targs, complain);
22453
22454 checks = get_deferred_access_checks ();
22455 pop_deferring_access_checks ();
22456
22457 pop_tinst_level ();
22458
22459 if (decl == error_mark_node)
22460 goto fail;
22461
22462 /* Now perform any access checks encountered during substitution. */
22463 push_access_scope (decl);
22464 ok = perform_access_checks (checks, complain);
22465 pop_access_scope (decl);
22466 if (!ok)
22467 goto fail;
22468
22469 /* If we're looking for an exact match, check that what we got
22470 is indeed an exact match. It might not be if some template
22471 parameters are used in non-deduced contexts. But don't check
22472 for an exact match if we have dependent template arguments;
22473 in that case we're doing partial ordering, and we already know
22474 that we have two candidates that will provide the actual type. */
22475 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
22476 {
22477 tree substed = TREE_TYPE (decl);
22478 unsigned int i;
22479
22480 tree sarg
22481 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
22482 if (return_type)
22483 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
22484 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
22485 if (!same_type_p (args[i], TREE_VALUE (sarg)))
22486 {
22487 unify_type_mismatch (explain_p, args[i],
22488 TREE_VALUE (sarg));
22489 goto fail;
22490 }
22491 if ((i < nargs || sarg)
22492 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
22493 doesn't contain the trailing void, and conv fns are always (). */
22494 && !DECL_CONV_FN_P (decl))
22495 {
22496 unsigned nsargs = i + list_length (sarg);
22497 unify_arity (explain_p, nargs, nsargs);
22498 goto fail;
22499 }
22500 }
22501
22502 /* After doing deduction with the inherited constructor, actually return an
22503 instantiation of the inheriting constructor. */
22504 if (orig_fn != fn)
22505 decl = instantiate_template (orig_fn, targs, complain);
22506
22507 r = decl;
22508
22509 fail:
22510 --deduction_depth;
22511 if (excessive_deduction_depth)
22512 {
22513 if (deduction_depth == 0)
22514 /* Reset once we're all the way out. */
22515 excessive_deduction_depth = false;
22516 }
22517
22518 return r;
22519 }
22520
22521 /* Returns true iff PARM is a forwarding reference in the context of
22522 template argument deduction for TMPL. */
22523
22524 static bool
22525 forwarding_reference_p (tree parm, tree tmpl)
22526 {
22527 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
22528 cv-unqualified template parameter ..." */
22529 if (TYPE_REF_P (parm)
22530 && TYPE_REF_IS_RVALUE (parm)
22531 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
22532 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
22533 {
22534 parm = TREE_TYPE (parm);
22535 /* [temp.deduct.call], "... that does not represent a template parameter
22536 of a class template (during class template argument deduction)." */
22537 if (tmpl
22538 && deduction_guide_p (tmpl)
22539 && DECL_ARTIFICIAL (tmpl))
22540 {
22541 /* Since the template parameters of a synthesized guide consist of
22542 the template parameters of the class template followed by those of
22543 the constructor (if any), we can tell if PARM represents a template
22544 parameter of the class template by comparing its index with the
22545 arity of the class template. */
22546 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
22547 if (TEMPLATE_TYPE_IDX (parm)
22548 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
22549 return false;
22550 }
22551 return true;
22552 }
22553 return false;
22554 }
22555
22556 /* Adjust types before performing type deduction, as described in
22557 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
22558 sections are symmetric. PARM is the type of a function parameter
22559 or the return type of the conversion function. ARG is the type of
22560 the argument passed to the call, or the type of the value
22561 initialized with the result of the conversion function.
22562 ARG_EXPR is the original argument expression, which may be null. */
22563
22564 static int
22565 maybe_adjust_types_for_deduction (tree tparms,
22566 unification_kind_t strict,
22567 tree* parm,
22568 tree* arg,
22569 tree arg_expr)
22570 {
22571 int result = 0;
22572
22573 switch (strict)
22574 {
22575 case DEDUCE_CALL:
22576 break;
22577
22578 case DEDUCE_CONV:
22579 /* Swap PARM and ARG throughout the remainder of this
22580 function; the handling is precisely symmetric since PARM
22581 will initialize ARG rather than vice versa. */
22582 std::swap (parm, arg);
22583 break;
22584
22585 case DEDUCE_EXACT:
22586 /* Core issue #873: Do the DR606 thing (see below) for these cases,
22587 too, but here handle it by stripping the reference from PARM
22588 rather than by adding it to ARG. */
22589 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22590 && TYPE_REF_P (*arg)
22591 && !TYPE_REF_IS_RVALUE (*arg))
22592 *parm = TREE_TYPE (*parm);
22593 /* Nothing else to do in this case. */
22594 return 0;
22595
22596 default:
22597 gcc_unreachable ();
22598 }
22599
22600 if (!TYPE_REF_P (*parm))
22601 {
22602 /* [temp.deduct.call]
22603
22604 If P is not a reference type:
22605
22606 --If A is an array type, the pointer type produced by the
22607 array-to-pointer standard conversion (_conv.array_) is
22608 used in place of A for type deduction; otherwise,
22609
22610 --If A is a function type, the pointer type produced by
22611 the function-to-pointer standard conversion
22612 (_conv.func_) is used in place of A for type deduction;
22613 otherwise,
22614
22615 --If A is a cv-qualified type, the top level
22616 cv-qualifiers of A's type are ignored for type
22617 deduction. */
22618 if (TREE_CODE (*arg) == ARRAY_TYPE)
22619 *arg = build_pointer_type (TREE_TYPE (*arg));
22620 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
22621 *arg = build_pointer_type (*arg);
22622 else
22623 *arg = TYPE_MAIN_VARIANT (*arg);
22624 }
22625
22626 /* [temp.deduct.call], "If P is a forwarding reference and the argument is
22627 an lvalue, the type 'lvalue reference to A' is used in place of A for
22628 type deduction." */
22629 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
22630 && (arg_expr ? lvalue_p (arg_expr)
22631 /* try_one_overload doesn't provide an arg_expr, but
22632 functions are always lvalues. */
22633 : TREE_CODE (*arg) == FUNCTION_TYPE))
22634 *arg = build_reference_type (*arg);
22635
22636 /* [temp.deduct.call]
22637
22638 If P is a cv-qualified type, the top level cv-qualifiers
22639 of P's type are ignored for type deduction. If P is a
22640 reference type, the type referred to by P is used for
22641 type deduction. */
22642 *parm = TYPE_MAIN_VARIANT (*parm);
22643 if (TYPE_REF_P (*parm))
22644 {
22645 *parm = TREE_TYPE (*parm);
22646 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22647 }
22648
22649 /* DR 322. For conversion deduction, remove a reference type on parm
22650 too (which has been swapped into ARG). */
22651 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
22652 *arg = TREE_TYPE (*arg);
22653
22654 return result;
22655 }
22656
22657 /* Subroutine of fn_type_unification. PARM is a function parameter of a
22658 template which doesn't contain any deducible template parameters; check if
22659 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
22660 unify_one_argument. */
22661
22662 static int
22663 check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
22664 int flags, struct conversion **conv_p,
22665 bool explain_p)
22666 {
22667 tree type;
22668
22669 if (!TYPE_P (arg))
22670 type = TREE_TYPE (arg);
22671 else
22672 type = arg;
22673
22674 if (same_type_p (parm, type))
22675 return unify_success (explain_p);
22676
22677 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
22678 if (strict == DEDUCE_CONV)
22679 {
22680 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
22681 return unify_success (explain_p);
22682 }
22683 else if (strict == DEDUCE_CALL)
22684 {
22685 bool ok = false;
22686 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
22687 if (conv_p)
22688 /* Avoid recalculating this in add_function_candidate. */
22689 ok = (*conv_p
22690 = good_conversion (parm, type, conv_arg, flags, complain));
22691 else
22692 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
22693 if (ok)
22694 return unify_success (explain_p);
22695 }
22696
22697 if (strict == DEDUCE_EXACT)
22698 return unify_type_mismatch (explain_p, parm, arg);
22699 else
22700 return unify_arg_conversion (explain_p, parm, type, arg);
22701 }
22702
22703 static bool uses_deducible_template_parms (tree type);
22704
22705 /* Returns true iff the expression EXPR is one from which a template
22706 argument can be deduced. In other words, if it's an undecorated
22707 use of a template non-type parameter. */
22708
22709 static bool
22710 deducible_expression (tree expr)
22711 {
22712 /* Strip implicit conversions and implicit INDIRECT_REFs. */
22713 while (CONVERT_EXPR_P (expr)
22714 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
22715 || REFERENCE_REF_P (expr))
22716 expr = TREE_OPERAND (expr, 0);
22717 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
22718 }
22719
22720 /* Returns true iff the array domain DOMAIN uses a template parameter in a
22721 deducible way; that is, if it has a max value of <PARM> - 1. */
22722
22723 static bool
22724 deducible_array_bound (tree domain)
22725 {
22726 if (domain == NULL_TREE)
22727 return false;
22728
22729 tree max = TYPE_MAX_VALUE (domain);
22730 if (TREE_CODE (max) != MINUS_EXPR)
22731 return false;
22732
22733 return deducible_expression (TREE_OPERAND (max, 0));
22734 }
22735
22736 /* Returns true iff the template arguments ARGS use a template parameter
22737 in a deducible way. */
22738
22739 static bool
22740 deducible_template_args (tree args)
22741 {
22742 for (tree elt : tree_vec_range (args))
22743 {
22744 bool deducible;
22745 if (ARGUMENT_PACK_P (elt))
22746 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
22747 else
22748 {
22749 if (PACK_EXPANSION_P (elt))
22750 elt = PACK_EXPANSION_PATTERN (elt);
22751 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
22752 deducible = true;
22753 else if (TYPE_P (elt))
22754 deducible = uses_deducible_template_parms (elt);
22755 else
22756 deducible = deducible_expression (elt);
22757 }
22758 if (deducible)
22759 return true;
22760 }
22761 return false;
22762 }
22763
22764 /* Returns true iff TYPE contains any deducible references to template
22765 parameters, as per 14.8.2.5. */
22766
22767 static bool
22768 uses_deducible_template_parms (tree type)
22769 {
22770 if (PACK_EXPANSION_P (type))
22771 type = PACK_EXPANSION_PATTERN (type);
22772
22773 /* T
22774 cv-list T
22775 TT<T>
22776 TT<i>
22777 TT<> */
22778 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22779 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22780 return true;
22781
22782 /* T*
22783 T&
22784 T&& */
22785 if (INDIRECT_TYPE_P (type))
22786 return uses_deducible_template_parms (TREE_TYPE (type));
22787
22788 /* T[integer-constant ]
22789 type [i] */
22790 if (TREE_CODE (type) == ARRAY_TYPE)
22791 return (uses_deducible_template_parms (TREE_TYPE (type))
22792 || deducible_array_bound (TYPE_DOMAIN (type)));
22793
22794 /* T type ::*
22795 type T::*
22796 T T::*
22797 T (type ::*)()
22798 type (T::*)()
22799 type (type ::*)(T)
22800 type (T::*)(T)
22801 T (type ::*)(T)
22802 T (T::*)()
22803 T (T::*)(T) */
22804 if (TYPE_PTRMEM_P (type))
22805 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
22806 || (uses_deducible_template_parms
22807 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
22808
22809 /* template-name <T> (where template-name refers to a class template)
22810 template-name <i> (where template-name refers to a class template) */
22811 if (CLASS_TYPE_P (type)
22812 && CLASSTYPE_TEMPLATE_INFO (type)
22813 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
22814 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
22815 (CLASSTYPE_TI_ARGS (type)));
22816
22817 /* type (T)
22818 T()
22819 T(T) */
22820 if (FUNC_OR_METHOD_TYPE_P (type))
22821 {
22822 if (uses_deducible_template_parms (TREE_TYPE (type)))
22823 return true;
22824 tree parm = TYPE_ARG_TYPES (type);
22825 if (TREE_CODE (type) == METHOD_TYPE)
22826 parm = TREE_CHAIN (parm);
22827 for (; parm; parm = TREE_CHAIN (parm))
22828 if (uses_deducible_template_parms (TREE_VALUE (parm)))
22829 return true;
22830 if (flag_noexcept_type
22831 && TYPE_RAISES_EXCEPTIONS (type)
22832 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
22833 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
22834 return true;
22835 }
22836
22837 return false;
22838 }
22839
22840 /* Subroutine of type_unification_real and unify_pack_expansion to
22841 handle unification of a single P/A pair. Parameters are as
22842 for those functions. */
22843
22844 static int
22845 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
22846 int subr, unification_kind_t strict,
22847 bool explain_p)
22848 {
22849 tree arg_expr = NULL_TREE;
22850 int arg_strict;
22851
22852 if (arg == error_mark_node || parm == error_mark_node)
22853 return unify_invalid (explain_p);
22854 if (arg == unknown_type_node)
22855 /* We can't deduce anything from this, but we might get all the
22856 template args from other function args. */
22857 return unify_success (explain_p);
22858
22859 /* Implicit conversions (Clause 4) will be performed on a function
22860 argument to convert it to the type of the corresponding function
22861 parameter if the parameter type contains no template-parameters that
22862 participate in template argument deduction. */
22863 if (strict != DEDUCE_EXACT
22864 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
22865 /* For function parameters with no deducible template parameters,
22866 just return. We'll check non-dependent conversions later. */
22867 return unify_success (explain_p);
22868
22869 switch (strict)
22870 {
22871 case DEDUCE_CALL:
22872 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
22873 | UNIFY_ALLOW_MORE_CV_QUAL
22874 | UNIFY_ALLOW_DERIVED);
22875 break;
22876
22877 case DEDUCE_CONV:
22878 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
22879 break;
22880
22881 case DEDUCE_EXACT:
22882 arg_strict = UNIFY_ALLOW_NONE;
22883 break;
22884
22885 default:
22886 gcc_unreachable ();
22887 }
22888
22889 /* We only do these transformations if this is the top-level
22890 parameter_type_list in a call or declaration matching; in other
22891 situations (nested function declarators, template argument lists) we
22892 won't be comparing a type to an expression, and we don't do any type
22893 adjustments. */
22894 if (!subr)
22895 {
22896 if (!TYPE_P (arg))
22897 {
22898 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
22899 if (type_unknown_p (arg))
22900 {
22901 /* [temp.deduct.type] A template-argument can be
22902 deduced from a pointer to function or pointer
22903 to member function argument if the set of
22904 overloaded functions does not contain function
22905 templates and at most one of a set of
22906 overloaded functions provides a unique
22907 match. */
22908 resolve_overloaded_unification (tparms, targs, parm,
22909 arg, strict,
22910 arg_strict, explain_p);
22911 /* If a unique match was not found, this is a
22912 non-deduced context, so we still succeed. */
22913 return unify_success (explain_p);
22914 }
22915
22916 arg_expr = arg;
22917 arg = unlowered_expr_type (arg);
22918 if (arg == error_mark_node)
22919 return unify_invalid (explain_p);
22920 }
22921
22922 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
22923 &parm, &arg, arg_expr);
22924 }
22925 else
22926 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
22927 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
22928 return unify_template_argument_mismatch (explain_p, parm, arg);
22929
22930 /* For deduction from an init-list we need the actual list. */
22931 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
22932 arg = arg_expr;
22933 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
22934 }
22935
22936 /* for_each_template_parm callback that always returns 0. */
22937
22938 static int
22939 zero_r (tree, void *)
22940 {
22941 return 0;
22942 }
22943
22944 /* for_each_template_parm any_fn callback to handle deduction of a template
22945 type argument from the type of an array bound. */
22946
22947 static int
22948 array_deduction_r (tree t, void *data)
22949 {
22950 tree_pair_p d = (tree_pair_p)data;
22951 tree &tparms = d->purpose;
22952 tree &targs = d->value;
22953
22954 if (TREE_CODE (t) == ARRAY_TYPE)
22955 if (tree dom = TYPE_DOMAIN (t))
22956 if (tree max = TYPE_MAX_VALUE (dom))
22957 {
22958 if (TREE_CODE (max) == MINUS_EXPR)
22959 max = TREE_OPERAND (max, 0);
22960 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
22961 unify (tparms, targs, TREE_TYPE (max), size_type_node,
22962 UNIFY_ALLOW_NONE, /*explain*/false);
22963 }
22964
22965 /* Keep walking. */
22966 return 0;
22967 }
22968
22969 /* Try to deduce any not-yet-deduced template type arguments from the type of
22970 an array bound. This is handled separately from unify because 14.8.2.5 says
22971 "The type of a type parameter is only deduced from an array bound if it is
22972 not otherwise deduced." */
22973
22974 static void
22975 try_array_deduction (tree tparms, tree targs, tree parm)
22976 {
22977 tree_pair_s data = { tparms, targs };
22978 hash_set<tree> visited;
22979 for_each_template_parm (parm, zero_r, &data, &visited,
22980 /*nondeduced*/false, array_deduction_r);
22981 }
22982
22983 /* Most parms like fn_type_unification.
22984
22985 If SUBR is 1, we're being called recursively (to unify the
22986 arguments of a function or method parameter of a function
22987 template).
22988
22989 CHECKS is a pointer to a vector of access checks encountered while
22990 substituting default template arguments. */
22991
22992 static int
22993 type_unification_real (tree tparms,
22994 tree full_targs,
22995 tree xparms,
22996 const tree *xargs,
22997 unsigned int xnargs,
22998 int subr,
22999 unification_kind_t strict,
23000 vec<deferred_access_check, va_gc> **checks,
23001 bool explain_p)
23002 {
23003 tree parm, arg;
23004 int i;
23005 int ntparms = TREE_VEC_LENGTH (tparms);
23006 int saw_undeduced = 0;
23007 tree parms;
23008 const tree *args;
23009 unsigned int nargs;
23010 unsigned int ia;
23011
23012 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
23013 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
23014 gcc_assert (ntparms > 0);
23015
23016 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
23017
23018 /* Reset the number of non-defaulted template arguments contained
23019 in TARGS. */
23020 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
23021
23022 again:
23023 parms = xparms;
23024 args = xargs;
23025 nargs = xnargs;
23026
23027 /* Only fn_type_unification cares about terminal void. */
23028 if (nargs && args[nargs-1] == void_type_node)
23029 --nargs;
23030
23031 ia = 0;
23032 while (parms && parms != void_list_node
23033 && ia < nargs)
23034 {
23035 parm = TREE_VALUE (parms);
23036
23037 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
23038 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
23039 /* For a function parameter pack that occurs at the end of the
23040 parameter-declaration-list, the type A of each remaining
23041 argument of the call is compared with the type P of the
23042 declarator-id of the function parameter pack. */
23043 break;
23044
23045 parms = TREE_CHAIN (parms);
23046
23047 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
23048 /* For a function parameter pack that does not occur at the
23049 end of the parameter-declaration-list, the type of the
23050 parameter pack is a non-deduced context. */
23051 continue;
23052
23053 arg = args[ia];
23054 ++ia;
23055
23056 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
23057 explain_p))
23058 return 1;
23059 }
23060
23061 if (parms
23062 && parms != void_list_node
23063 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
23064 {
23065 /* Unify the remaining arguments with the pack expansion type. */
23066 tree argvec;
23067 tree parmvec = make_tree_vec (1);
23068
23069 /* Allocate a TREE_VEC and copy in all of the arguments */
23070 argvec = make_tree_vec (nargs - ia);
23071 for (i = 0; ia < nargs; ++ia, ++i)
23072 TREE_VEC_ELT (argvec, i) = args[ia];
23073
23074 /* Copy the parameter into parmvec. */
23075 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
23076 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
23077 /*subr=*/subr, explain_p))
23078 return 1;
23079
23080 /* Advance to the end of the list of parameters. */
23081 parms = TREE_CHAIN (parms);
23082 }
23083
23084 /* Fail if we've reached the end of the parm list, and more args
23085 are present, and the parm list isn't variadic. */
23086 if (ia < nargs && parms == void_list_node)
23087 return unify_too_many_arguments (explain_p, nargs, ia);
23088 /* Fail if parms are left and they don't have default values and
23089 they aren't all deduced as empty packs (c++/57397). This is
23090 consistent with sufficient_parms_p. */
23091 if (parms && parms != void_list_node
23092 && TREE_PURPOSE (parms) == NULL_TREE)
23093 {
23094 unsigned int count = nargs;
23095 tree p = parms;
23096 bool type_pack_p;
23097 do
23098 {
23099 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
23100 if (!type_pack_p)
23101 count++;
23102 p = TREE_CHAIN (p);
23103 }
23104 while (p && p != void_list_node);
23105 if (count != nargs)
23106 return unify_too_few_arguments (explain_p, ia, count,
23107 type_pack_p);
23108 }
23109
23110 if (!subr)
23111 {
23112 tsubst_flags_t complain = (explain_p
23113 ? tf_warning_or_error
23114 : tf_none);
23115 bool tried_array_deduction = (cxx_dialect < cxx17);
23116
23117 for (i = 0; i < ntparms; i++)
23118 {
23119 tree targ = TREE_VEC_ELT (targs, i);
23120 tree tparm = TREE_VEC_ELT (tparms, i);
23121
23122 /* Clear the "incomplete" flags on all argument packs now so that
23123 substituting them into later default arguments works. */
23124 if (targ && ARGUMENT_PACK_P (targ))
23125 {
23126 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
23127 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
23128 }
23129
23130 if (targ || tparm == error_mark_node)
23131 continue;
23132 tparm = TREE_VALUE (tparm);
23133
23134 if (TREE_CODE (tparm) == TYPE_DECL
23135 && !tried_array_deduction)
23136 {
23137 try_array_deduction (tparms, targs, xparms);
23138 tried_array_deduction = true;
23139 if (TREE_VEC_ELT (targs, i))
23140 continue;
23141 }
23142
23143 /* If this is an undeduced nontype parameter that depends on
23144 a type parameter, try another pass; its type may have been
23145 deduced from a later argument than the one from which
23146 this parameter can be deduced. */
23147 if (TREE_CODE (tparm) == PARM_DECL
23148 && !is_auto (TREE_TYPE (tparm))
23149 && uses_template_parms (TREE_TYPE (tparm))
23150 && saw_undeduced < 2)
23151 {
23152 saw_undeduced = 1;
23153 continue;
23154 }
23155
23156 /* Core issue #226 (C++0x) [temp.deduct]:
23157
23158 If a template argument has not been deduced, its
23159 default template argument, if any, is used.
23160
23161 When we are in C++98 mode, TREE_PURPOSE will either
23162 be NULL_TREE or ERROR_MARK_NODE, so we do not need
23163 to explicitly check cxx_dialect here. */
23164 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
23165 /* OK, there is a default argument. Wait until after the
23166 conversion check to do substitution. */
23167 continue;
23168
23169 /* If the type parameter is a parameter pack, then it will
23170 be deduced to an empty parameter pack. */
23171 if (template_parameter_pack_p (tparm))
23172 {
23173 tree arg;
23174
23175 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
23176 {
23177 arg = make_node (NONTYPE_ARGUMENT_PACK);
23178 TREE_CONSTANT (arg) = 1;
23179 }
23180 else
23181 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
23182
23183 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
23184
23185 TREE_VEC_ELT (targs, i) = arg;
23186 continue;
23187 }
23188
23189 return unify_parameter_deduction_failure (explain_p, tparm);
23190 }
23191
23192 /* Now substitute into the default template arguments. */
23193 for (i = 0; i < ntparms; i++)
23194 {
23195 tree targ = TREE_VEC_ELT (targs, i);
23196 tree tparm = TREE_VEC_ELT (tparms, i);
23197
23198 if (targ || tparm == error_mark_node)
23199 continue;
23200 tree parm = TREE_VALUE (tparm);
23201 tree arg = TREE_PURPOSE (tparm);
23202 reopen_deferring_access_checks (*checks);
23203 location_t save_loc = input_location;
23204 if (DECL_P (parm))
23205 input_location = DECL_SOURCE_LOCATION (parm);
23206
23207 if (saw_undeduced == 1
23208 && TREE_CODE (parm) == PARM_DECL
23209 && !is_auto (TREE_TYPE (parm))
23210 && uses_template_parms (TREE_TYPE (parm)))
23211 {
23212 /* The type of this non-type parameter depends on undeduced
23213 parameters. Don't try to use its default argument yet,
23214 since we might deduce an argument for it on the next pass,
23215 but do check whether the arguments we already have cause
23216 substitution failure, so that that happens before we try
23217 later default arguments (78489). */
23218 ++processing_template_decl;
23219 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
23220 NULL_TREE);
23221 --processing_template_decl;
23222 if (type == error_mark_node)
23223 arg = error_mark_node;
23224 else
23225 arg = NULL_TREE;
23226 }
23227 else
23228 {
23229 /* Even if the call is happening in template context, getting
23230 here means it's non-dependent, and a default argument is
23231 considered a separate definition under [temp.decls], so we can
23232 do this substitution without processing_template_decl. This
23233 is important if the default argument contains something that
23234 might be instantiation-dependent like access (87480). */
23235 processing_template_decl_sentinel s;
23236 tree substed = NULL_TREE;
23237 if (saw_undeduced == 1)
23238 {
23239 /* First instatiate in template context, in case we still
23240 depend on undeduced template parameters. */
23241 ++processing_template_decl;
23242 substed = tsubst_template_arg (arg, full_targs, complain,
23243 NULL_TREE);
23244 --processing_template_decl;
23245 if (substed != error_mark_node
23246 && !uses_template_parms (substed))
23247 /* We replaced all the tparms, substitute again out of
23248 template context. */
23249 substed = NULL_TREE;
23250 }
23251 if (!substed)
23252 substed = tsubst_template_arg (arg, full_targs, complain,
23253 NULL_TREE);
23254
23255 if (!uses_template_parms (substed))
23256 arg = convert_template_argument (parm, substed, full_targs,
23257 complain, i, NULL_TREE);
23258 else if (saw_undeduced == 1)
23259 arg = NULL_TREE;
23260 else
23261 arg = error_mark_node;
23262 }
23263
23264 input_location = save_loc;
23265 *checks = get_deferred_access_checks ();
23266 pop_deferring_access_checks ();
23267
23268 if (arg == error_mark_node)
23269 return 1;
23270 else if (arg)
23271 {
23272 TREE_VEC_ELT (targs, i) = arg;
23273 /* The position of the first default template argument,
23274 is also the number of non-defaulted arguments in TARGS.
23275 Record that. */
23276 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23277 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
23278 }
23279 }
23280
23281 if (saw_undeduced++ == 1)
23282 goto again;
23283 }
23284
23285 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
23286 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
23287
23288 return unify_success (explain_p);
23289 }
23290
23291 /* Subroutine of type_unification_real. Args are like the variables
23292 at the call site. ARG is an overloaded function (or template-id);
23293 we try deducing template args from each of the overloads, and if
23294 only one succeeds, we go with that. Modifies TARGS and returns
23295 true on success. */
23296
23297 static bool
23298 resolve_overloaded_unification (tree tparms,
23299 tree targs,
23300 tree parm,
23301 tree arg,
23302 unification_kind_t strict,
23303 int sub_strict,
23304 bool explain_p)
23305 {
23306 tree tempargs = copy_node (targs);
23307 int good = 0;
23308 tree goodfn = NULL_TREE;
23309 bool addr_p;
23310
23311 if (TREE_CODE (arg) == ADDR_EXPR)
23312 {
23313 arg = TREE_OPERAND (arg, 0);
23314 addr_p = true;
23315 }
23316 else
23317 addr_p = false;
23318
23319 if (TREE_CODE (arg) == COMPONENT_REF)
23320 /* Handle `&x' where `x' is some static or non-static member
23321 function name. */
23322 arg = TREE_OPERAND (arg, 1);
23323
23324 if (TREE_CODE (arg) == OFFSET_REF)
23325 arg = TREE_OPERAND (arg, 1);
23326
23327 /* Strip baselink information. */
23328 if (BASELINK_P (arg))
23329 arg = BASELINK_FUNCTIONS (arg);
23330
23331 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
23332 {
23333 /* If we got some explicit template args, we need to plug them into
23334 the affected templates before we try to unify, in case the
23335 explicit args will completely resolve the templates in question. */
23336
23337 int ok = 0;
23338 tree expl_subargs = TREE_OPERAND (arg, 1);
23339 arg = TREE_OPERAND (arg, 0);
23340
23341 for (lkp_iterator iter (arg); iter; ++iter)
23342 {
23343 tree fn = *iter;
23344 tree subargs, elem;
23345
23346 if (TREE_CODE (fn) != TEMPLATE_DECL)
23347 continue;
23348
23349 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23350 expl_subargs, NULL_TREE, tf_none);
23351 if (subargs != error_mark_node
23352 && !any_dependent_template_arguments_p (subargs))
23353 {
23354 fn = instantiate_template (fn, subargs, tf_none);
23355 if (!constraints_satisfied_p (fn))
23356 continue;
23357 if (undeduced_auto_decl (fn))
23358 {
23359 /* Instantiate the function to deduce its return type. */
23360 ++function_depth;
23361 instantiate_decl (fn, /*defer*/false, /*class*/false);
23362 --function_depth;
23363 }
23364
23365 if (flag_noexcept_type)
23366 maybe_instantiate_noexcept (fn, tf_none);
23367
23368 elem = TREE_TYPE (fn);
23369 if (try_one_overload (tparms, targs, tempargs, parm,
23370 elem, strict, sub_strict, addr_p, explain_p)
23371 && (!goodfn || !same_type_p (goodfn, elem)))
23372 {
23373 goodfn = elem;
23374 ++good;
23375 }
23376 }
23377 else if (subargs)
23378 ++ok;
23379 }
23380 /* If no templates (or more than one) are fully resolved by the
23381 explicit arguments, this template-id is a non-deduced context; it
23382 could still be OK if we deduce all template arguments for the
23383 enclosing call through other arguments. */
23384 if (good != 1)
23385 good = ok;
23386 }
23387 else if (!OVL_P (arg))
23388 /* If ARG is, for example, "(0, &f)" then its type will be unknown
23389 -- but the deduction does not succeed because the expression is
23390 not just the function on its own. */
23391 return false;
23392 else
23393 for (lkp_iterator iter (arg); iter; ++iter)
23394 {
23395 tree fn = *iter;
23396 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
23397 strict, sub_strict, addr_p, explain_p)
23398 && (!goodfn || !decls_match (goodfn, fn)))
23399 {
23400 goodfn = fn;
23401 ++good;
23402 }
23403 }
23404
23405 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23406 to function or pointer to member function argument if the set of
23407 overloaded functions does not contain function templates and at most
23408 one of a set of overloaded functions provides a unique match.
23409
23410 So if we found multiple possibilities, we return success but don't
23411 deduce anything. */
23412
23413 if (good == 1)
23414 {
23415 int i = TREE_VEC_LENGTH (targs);
23416 for (; i--; )
23417 if (TREE_VEC_ELT (tempargs, i))
23418 {
23419 tree old = TREE_VEC_ELT (targs, i);
23420 tree new_ = TREE_VEC_ELT (tempargs, i);
23421 if (new_ && old && ARGUMENT_PACK_P (old)
23422 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
23423 /* Don't forget explicit template arguments in a pack. */
23424 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
23425 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
23426 TREE_VEC_ELT (targs, i) = new_;
23427 }
23428 }
23429 if (good)
23430 return true;
23431
23432 return false;
23433 }
23434
23435 /* Core DR 115: In contexts where deduction is done and fails, or in
23436 contexts where deduction is not done, if a template argument list is
23437 specified and it, along with any default template arguments, identifies
23438 a single function template specialization, then the template-id is an
23439 lvalue for the function template specialization. */
23440
23441 tree
23442 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
23443 {
23444 tree expr, offset, baselink;
23445 bool addr;
23446
23447 if (!type_unknown_p (orig_expr))
23448 return orig_expr;
23449
23450 expr = orig_expr;
23451 addr = false;
23452 offset = NULL_TREE;
23453 baselink = NULL_TREE;
23454
23455 if (TREE_CODE (expr) == ADDR_EXPR)
23456 {
23457 expr = TREE_OPERAND (expr, 0);
23458 addr = true;
23459 }
23460 if (TREE_CODE (expr) == OFFSET_REF)
23461 {
23462 offset = expr;
23463 expr = TREE_OPERAND (expr, 1);
23464 }
23465 if (BASELINK_P (expr))
23466 {
23467 baselink = expr;
23468 expr = BASELINK_FUNCTIONS (expr);
23469 }
23470
23471 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
23472 {
23473 int good = 0;
23474 tree goodfn = NULL_TREE;
23475
23476 /* If we got some explicit template args, we need to plug them into
23477 the affected templates before we try to unify, in case the
23478 explicit args will completely resolve the templates in question. */
23479
23480 tree expl_subargs = TREE_OPERAND (expr, 1);
23481 tree arg = TREE_OPERAND (expr, 0);
23482 tree badfn = NULL_TREE;
23483 tree badargs = NULL_TREE;
23484
23485 for (lkp_iterator iter (arg); iter; ++iter)
23486 {
23487 tree fn = *iter;
23488 tree subargs, elem;
23489
23490 if (TREE_CODE (fn) != TEMPLATE_DECL)
23491 continue;
23492
23493 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
23494 expl_subargs, NULL_TREE, tf_none);
23495 if (subargs != error_mark_node
23496 && !any_dependent_template_arguments_p (subargs))
23497 {
23498 elem = instantiate_template (fn, subargs, tf_none);
23499 if (elem == error_mark_node)
23500 {
23501 badfn = fn;
23502 badargs = subargs;
23503 }
23504 else if (elem && (!goodfn || !decls_match (goodfn, elem))
23505 && constraints_satisfied_p (elem))
23506 {
23507 goodfn = elem;
23508 ++good;
23509 }
23510 }
23511 }
23512 if (good == 1)
23513 {
23514 mark_used (goodfn);
23515 expr = goodfn;
23516 if (baselink)
23517 expr = build_baselink (BASELINK_BINFO (baselink),
23518 BASELINK_ACCESS_BINFO (baselink),
23519 expr, BASELINK_OPTYPE (baselink));
23520 if (offset)
23521 {
23522 tree base
23523 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
23524 expr = build_offset_ref (base, expr, addr, complain);
23525 }
23526 if (addr)
23527 expr = cp_build_addr_expr (expr, complain);
23528 return expr;
23529 }
23530 else if (good == 0 && badargs && (complain & tf_error))
23531 /* There were no good options and at least one bad one, so let the
23532 user know what the problem is. */
23533 instantiate_template (badfn, badargs, complain);
23534 }
23535 return orig_expr;
23536 }
23537
23538 /* As above, but error out if the expression remains overloaded. */
23539
23540 tree
23541 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
23542 {
23543 exp = resolve_nondeduced_context (exp, complain);
23544 if (type_unknown_p (exp))
23545 {
23546 if (complain & tf_error)
23547 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
23548 return error_mark_node;
23549 }
23550 return exp;
23551 }
23552
23553 /* Subroutine of resolve_overloaded_unification; does deduction for a single
23554 overload. Fills TARGS with any deduced arguments, or error_mark_node if
23555 different overloads deduce different arguments for a given parm.
23556 ADDR_P is true if the expression for which deduction is being
23557 performed was of the form "& fn" rather than simply "fn".
23558
23559 Returns 1 on success. */
23560
23561 static int
23562 try_one_overload (tree tparms,
23563 tree orig_targs,
23564 tree targs,
23565 tree parm,
23566 tree arg,
23567 unification_kind_t strict,
23568 int sub_strict,
23569 bool addr_p,
23570 bool explain_p)
23571 {
23572 int nargs;
23573 tree tempargs;
23574 int i;
23575
23576 if (arg == error_mark_node)
23577 return 0;
23578
23579 /* [temp.deduct.type] A template-argument can be deduced from a pointer
23580 to function or pointer to member function argument if the set of
23581 overloaded functions does not contain function templates and at most
23582 one of a set of overloaded functions provides a unique match.
23583
23584 So if this is a template, just return success. */
23585
23586 if (uses_template_parms (arg))
23587 return 1;
23588
23589 if (TREE_CODE (arg) == METHOD_TYPE)
23590 arg = build_ptrmemfunc_type (build_pointer_type (arg));
23591 else if (addr_p)
23592 arg = build_pointer_type (arg);
23593
23594 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
23595 &parm, &arg, NULL_TREE);
23596
23597 /* We don't copy orig_targs for this because if we have already deduced
23598 some template args from previous args, unify would complain when we
23599 try to deduce a template parameter for the same argument, even though
23600 there isn't really a conflict. */
23601 nargs = TREE_VEC_LENGTH (targs);
23602 tempargs = make_tree_vec (nargs);
23603
23604 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
23605 return 0;
23606
23607 /* First make sure we didn't deduce anything that conflicts with
23608 explicitly specified args. */
23609 for (i = nargs; i--; )
23610 {
23611 tree elt = TREE_VEC_ELT (tempargs, i);
23612 tree oldelt = TREE_VEC_ELT (orig_targs, i);
23613
23614 if (!elt)
23615 /*NOP*/;
23616 else if (uses_template_parms (elt))
23617 /* Since we're unifying against ourselves, we will fill in
23618 template args used in the function parm list with our own
23619 template parms. Discard them. */
23620 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
23621 else if (oldelt && ARGUMENT_PACK_P (oldelt))
23622 {
23623 /* Check that the argument at each index of the deduced argument pack
23624 is equivalent to the corresponding explicitly specified argument.
23625 We may have deduced more arguments than were explicitly specified,
23626 and that's OK. */
23627
23628 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
23629 that's wrong if we deduce the same argument pack from multiple
23630 function arguments: it's only incomplete the first time. */
23631
23632 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
23633 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
23634
23635 if (TREE_VEC_LENGTH (deduced_pack)
23636 < TREE_VEC_LENGTH (explicit_pack))
23637 return 0;
23638
23639 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
23640 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
23641 TREE_VEC_ELT (deduced_pack, j)))
23642 return 0;
23643 }
23644 else if (oldelt && !template_args_equal (oldelt, elt))
23645 return 0;
23646 }
23647
23648 for (i = nargs; i--; )
23649 {
23650 tree elt = TREE_VEC_ELT (tempargs, i);
23651
23652 if (elt)
23653 TREE_VEC_ELT (targs, i) = elt;
23654 }
23655
23656 return 1;
23657 }
23658
23659 /* PARM is a template class (perhaps with unbound template
23660 parameters). ARG is a fully instantiated type. If ARG can be
23661 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
23662 TARGS are as for unify. */
23663
23664 static tree
23665 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
23666 bool explain_p)
23667 {
23668 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23669 return NULL_TREE;
23670 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23671 /* Matches anything. */;
23672 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
23673 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
23674 return NULL_TREE;
23675
23676 /* We need to make a new template argument vector for the call to
23677 unify. If we used TARGS, we'd clutter it up with the result of
23678 the attempted unification, even if this class didn't work out.
23679 We also don't want to commit ourselves to all the unifications
23680 we've already done, since unification is supposed to be done on
23681 an argument-by-argument basis. In other words, consider the
23682 following pathological case:
23683
23684 template <int I, int J, int K>
23685 struct S {};
23686
23687 template <int I, int J>
23688 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
23689
23690 template <int I, int J, int K>
23691 void f(S<I, J, K>, S<I, I, I>);
23692
23693 void g() {
23694 S<0, 0, 0> s0;
23695 S<0, 1, 2> s2;
23696
23697 f(s0, s2);
23698 }
23699
23700 Now, by the time we consider the unification involving `s2', we
23701 already know that we must have `f<0, 0, 0>'. But, even though
23702 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
23703 because there are two ways to unify base classes of S<0, 1, 2>
23704 with S<I, I, I>. If we kept the already deduced knowledge, we
23705 would reject the possibility I=1. */
23706 targs = copy_template_args (targs);
23707 for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
23708 targ = NULL_TREE;
23709
23710 int err;
23711 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23712 err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
23713 else
23714 err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23715 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p);
23716
23717 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
23718 for (tree level : tree_vec_range (targs))
23719 ggc_free (level);
23720 ggc_free (targs);
23721
23722 return err ? NULL_TREE : arg;
23723 }
23724
23725 /* Given a template type PARM and a class type ARG, find the unique
23726 base type in ARG that is an instance of PARM. We do not examine
23727 ARG itself; only its base-classes. If there is not exactly one
23728 appropriate base class, return NULL_TREE. PARM may be the type of
23729 a partial specialization, as well as a plain template type. Used
23730 by unify. */
23731
23732 static enum template_base_result
23733 get_template_base (tree tparms, tree targs, tree parm, tree arg,
23734 bool explain_p, tree *result)
23735 {
23736 tree rval = NULL_TREE;
23737 tree binfo;
23738
23739 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
23740
23741 binfo = TYPE_BINFO (complete_type (arg));
23742 if (!binfo)
23743 {
23744 /* The type could not be completed. */
23745 *result = NULL_TREE;
23746 return tbr_incomplete_type;
23747 }
23748
23749 /* Walk in inheritance graph order. The search order is not
23750 important, and this avoids multiple walks of virtual bases. */
23751 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
23752 {
23753 tree r = try_class_unification (tparms, targs, parm,
23754 BINFO_TYPE (binfo), explain_p);
23755
23756 if (r)
23757 {
23758 /* If there is more than one satisfactory baseclass, then:
23759
23760 [temp.deduct.call]
23761
23762 If they yield more than one possible deduced A, the type
23763 deduction fails.
23764
23765 applies. */
23766 if (rval && !same_type_p (r, rval))
23767 {
23768 /* [temp.deduct.call]/4.3: If there is a class C that is a
23769 (direct or indirect) base class of D and derived (directly or
23770 indirectly) from a class B and that would be a valid deduced
23771 A, the deduced A cannot be B or pointer to B, respectively. */
23772 if (DERIVED_FROM_P (r, rval))
23773 /* Ignore r. */
23774 continue;
23775 else if (DERIVED_FROM_P (rval, r))
23776 /* Ignore rval. */;
23777 else
23778 {
23779 *result = NULL_TREE;
23780 return tbr_ambiguous_baseclass;
23781 }
23782 }
23783
23784 rval = r;
23785 }
23786 }
23787
23788 *result = rval;
23789 return tbr_success;
23790 }
23791
23792 /* Returns the level of DECL, which declares a template parameter. */
23793
23794 static int
23795 template_decl_level (tree decl)
23796 {
23797 switch (TREE_CODE (decl))
23798 {
23799 case TYPE_DECL:
23800 case TEMPLATE_DECL:
23801 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
23802
23803 case PARM_DECL:
23804 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
23805
23806 default:
23807 gcc_unreachable ();
23808 }
23809 return 0;
23810 }
23811
23812 /* Decide whether ARG can be unified with PARM, considering only the
23813 cv-qualifiers of each type, given STRICT as documented for unify.
23814 Returns nonzero iff the unification is OK on that basis. */
23815
23816 static int
23817 check_cv_quals_for_unify (int strict, tree arg, tree parm)
23818 {
23819 int arg_quals = cp_type_quals (arg);
23820 int parm_quals = cp_type_quals (parm);
23821
23822 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23823 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23824 {
23825 /* Although a CVR qualifier is ignored when being applied to a
23826 substituted template parameter ([8.3.2]/1 for example), that
23827 does not allow us to unify "const T" with "int&" because both
23828 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23829 It is ok when we're allowing additional CV qualifiers
23830 at the outer level [14.8.2.1]/3,1st bullet. */
23831 if ((TYPE_REF_P (arg)
23832 || FUNC_OR_METHOD_TYPE_P (arg))
23833 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
23834 return 0;
23835
23836 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
23837 && (parm_quals & TYPE_QUAL_RESTRICT))
23838 return 0;
23839 }
23840
23841 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23842 && (arg_quals & parm_quals) != parm_quals)
23843 return 0;
23844
23845 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
23846 && (parm_quals & arg_quals) != arg_quals)
23847 return 0;
23848
23849 return 1;
23850 }
23851
23852 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23853 void
23854 template_parm_level_and_index (tree parm, int* level, int* index)
23855 {
23856 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23857 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23858 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23859 {
23860 *index = TEMPLATE_TYPE_IDX (parm);
23861 *level = TEMPLATE_TYPE_LEVEL (parm);
23862 }
23863 else
23864 {
23865 *index = TEMPLATE_PARM_IDX (parm);
23866 *level = TEMPLATE_PARM_LEVEL (parm);
23867 }
23868 }
23869
23870 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23871 do { \
23872 if (unify (TP, TA, P, A, S, EP)) \
23873 return 1; \
23874 } while (0)
23875
23876 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23877 expansion at the end of PACKED_PARMS. Returns 0 if the type
23878 deduction succeeds, 1 otherwise. STRICT is the same as in
23879 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23880 function call argument list. We'll need to adjust the arguments to make them
23881 types. SUBR tells us if this is from a recursive call to
23882 type_unification_real, or for comparing two template argument
23883 lists. */
23884
23885 static int
23886 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
23887 tree packed_args, unification_kind_t strict,
23888 bool subr, bool explain_p)
23889 {
23890 tree parm
23891 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
23892 tree pattern = PACK_EXPANSION_PATTERN (parm);
23893 tree pack, packs = NULL_TREE;
23894 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
23895
23896 /* Add in any args remembered from an earlier partial instantiation. */
23897 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
23898 int levels = TMPL_ARGS_DEPTH (targs);
23899
23900 packed_args = expand_template_argument_pack (packed_args);
23901
23902 int len = TREE_VEC_LENGTH (packed_args);
23903
23904 /* Determine the parameter packs we will be deducing from the
23905 pattern, and record their current deductions. */
23906 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
23907 pack; pack = TREE_CHAIN (pack))
23908 {
23909 tree parm_pack = TREE_VALUE (pack);
23910 int idx, level;
23911
23912 /* Only template parameter packs can be deduced, not e.g. function
23913 parameter packs or __bases or __integer_pack. */
23914 if (!TEMPLATE_PARM_P (parm_pack))
23915 continue;
23916
23917 /* Determine the index and level of this parameter pack. */
23918 template_parm_level_and_index (parm_pack, &level, &idx);
23919 if (level > levels)
23920 continue;
23921
23922 /* Keep track of the parameter packs and their corresponding
23923 argument packs. */
23924 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
23925 TREE_TYPE (packs) = make_tree_vec (len - start);
23926 }
23927
23928 /* Loop through all of the arguments that have not yet been
23929 unified and unify each with the pattern. */
23930 for (i = start; i < len; i++)
23931 {
23932 tree parm;
23933 bool any_explicit = false;
23934 tree arg = TREE_VEC_ELT (packed_args, i);
23935
23936 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23937 or the element of its argument pack at the current index if
23938 this argument was explicitly specified. */
23939 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23940 {
23941 int idx, level;
23942 tree arg, pargs;
23943 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23944
23945 arg = NULL_TREE;
23946 if (TREE_VALUE (pack)
23947 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
23948 && (i - start < TREE_VEC_LENGTH (pargs)))
23949 {
23950 any_explicit = true;
23951 arg = TREE_VEC_ELT (pargs, i - start);
23952 }
23953 TMPL_ARG (targs, level, idx) = arg;
23954 }
23955
23956 /* If we had explicit template arguments, substitute them into the
23957 pattern before deduction. */
23958 if (any_explicit)
23959 {
23960 /* Some arguments might still be unspecified or dependent. */
23961 bool dependent;
23962 ++processing_template_decl;
23963 dependent = any_dependent_template_arguments_p (targs);
23964 if (!dependent)
23965 --processing_template_decl;
23966 parm = tsubst (pattern, targs,
23967 explain_p ? tf_warning_or_error : tf_none,
23968 NULL_TREE);
23969 if (dependent)
23970 --processing_template_decl;
23971 if (parm == error_mark_node)
23972 return 1;
23973 }
23974 else
23975 parm = pattern;
23976
23977 /* Unify the pattern with the current argument. */
23978 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23979 explain_p))
23980 return 1;
23981
23982 /* For each parameter pack, collect the deduced value. */
23983 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23984 {
23985 int idx, level;
23986 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23987
23988 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23989 TMPL_ARG (targs, level, idx);
23990 }
23991 }
23992
23993 /* Verify that the results of unification with the parameter packs
23994 produce results consistent with what we've seen before, and make
23995 the deduced argument packs available. */
23996 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23997 {
23998 tree old_pack = TREE_VALUE (pack);
23999 tree new_args = TREE_TYPE (pack);
24000 int i, len = TREE_VEC_LENGTH (new_args);
24001 int idx, level;
24002 bool nondeduced_p = false;
24003
24004 /* By default keep the original deduced argument pack.
24005 If necessary, more specific code is going to update the
24006 resulting deduced argument later down in this function. */
24007 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
24008 TMPL_ARG (targs, level, idx) = old_pack;
24009
24010 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
24011 actually deduce anything. */
24012 for (i = 0; i < len && !nondeduced_p; ++i)
24013 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
24014 nondeduced_p = true;
24015 if (nondeduced_p)
24016 continue;
24017
24018 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
24019 {
24020 /* If we had fewer function args than explicit template args,
24021 just use the explicits. */
24022 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24023 int explicit_len = TREE_VEC_LENGTH (explicit_args);
24024 if (len < explicit_len)
24025 new_args = explicit_args;
24026 }
24027
24028 if (!old_pack)
24029 {
24030 tree result;
24031 /* Build the deduced *_ARGUMENT_PACK. */
24032 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
24033 {
24034 result = make_node (NONTYPE_ARGUMENT_PACK);
24035 TREE_CONSTANT (result) = 1;
24036 }
24037 else
24038 result = cxx_make_type (TYPE_ARGUMENT_PACK);
24039
24040 ARGUMENT_PACK_ARGS (result) = new_args;
24041
24042 /* Note the deduced argument packs for this parameter
24043 pack. */
24044 TMPL_ARG (targs, level, idx) = result;
24045 }
24046 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
24047 && (ARGUMENT_PACK_ARGS (old_pack)
24048 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
24049 {
24050 /* We only had the explicitly-provided arguments before, but
24051 now we have a complete set of arguments. */
24052 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
24053
24054 ARGUMENT_PACK_ARGS (old_pack) = new_args;
24055 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
24056 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
24057 }
24058 else
24059 {
24060 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
24061 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
24062 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
24063 /* During template argument deduction for the aggregate deduction
24064 candidate, the number of elements in a trailing parameter pack
24065 is only deduced from the number of remaining function
24066 arguments if it is not otherwise deduced. */
24067 if (cxx_dialect >= cxx20
24068 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
24069 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
24070 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
24071 if (!comp_template_args (old_args, new_args,
24072 &bad_old_arg, &bad_new_arg))
24073 /* Inconsistent unification of this parameter pack. */
24074 return unify_parameter_pack_inconsistent (explain_p,
24075 bad_old_arg,
24076 bad_new_arg);
24077 }
24078 }
24079
24080 return unify_success (explain_p);
24081 }
24082
24083 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
24084 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
24085 parameters and return value are as for unify. */
24086
24087 static int
24088 unify_array_domain (tree tparms, tree targs,
24089 tree parm_dom, tree arg_dom,
24090 bool explain_p)
24091 {
24092 tree parm_max;
24093 tree arg_max;
24094 bool parm_cst;
24095 bool arg_cst;
24096
24097 /* Our representation of array types uses "N - 1" as the
24098 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
24099 not an integer constant. We cannot unify arbitrarily
24100 complex expressions, so we eliminate the MINUS_EXPRs
24101 here. */
24102 parm_max = TYPE_MAX_VALUE (parm_dom);
24103 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
24104 if (!parm_cst)
24105 {
24106 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
24107 parm_max = TREE_OPERAND (parm_max, 0);
24108 }
24109 arg_max = TYPE_MAX_VALUE (arg_dom);
24110 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
24111 if (!arg_cst)
24112 {
24113 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
24114 trying to unify the type of a variable with the type
24115 of a template parameter. For example:
24116
24117 template <unsigned int N>
24118 void f (char (&) [N]);
24119 int g();
24120 void h(int i) {
24121 char a[g(i)];
24122 f(a);
24123 }
24124
24125 Here, the type of the ARG will be "int [g(i)]", and
24126 may be a SAVE_EXPR, etc. */
24127 if (TREE_CODE (arg_max) != MINUS_EXPR)
24128 return unify_vla_arg (explain_p, arg_dom);
24129 arg_max = TREE_OPERAND (arg_max, 0);
24130 }
24131
24132 /* If only one of the bounds used a MINUS_EXPR, compensate
24133 by adding one to the other bound. */
24134 if (parm_cst && !arg_cst)
24135 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
24136 integer_type_node,
24137 parm_max,
24138 integer_one_node);
24139 else if (arg_cst && !parm_cst)
24140 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
24141 integer_type_node,
24142 arg_max,
24143 integer_one_node);
24144
24145 return unify (tparms, targs, parm_max, arg_max,
24146 UNIFY_ALLOW_INTEGER, explain_p);
24147 }
24148
24149 /* Returns whether T, a P or A in unify, is a type, template or expression. */
24150
24151 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
24152
24153 static pa_kind_t
24154 pa_kind (tree t)
24155 {
24156 if (PACK_EXPANSION_P (t))
24157 t = PACK_EXPANSION_PATTERN (t);
24158 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
24159 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
24160 || DECL_TYPE_TEMPLATE_P (t))
24161 return pa_tmpl;
24162 else if (TYPE_P (t))
24163 return pa_type;
24164 else
24165 return pa_expr;
24166 }
24167
24168 /* Deduce the value of template parameters. TPARMS is the (innermost)
24169 set of template parameters to a template. TARGS is the bindings
24170 for those template parameters, as determined thus far; TARGS may
24171 include template arguments for outer levels of template parameters
24172 as well. PARM is a parameter to a template function, or a
24173 subcomponent of that parameter; ARG is the corresponding argument.
24174 This function attempts to match PARM with ARG in a manner
24175 consistent with the existing assignments in TARGS. If more values
24176 are deduced, then TARGS is updated.
24177
24178 Returns 0 if the type deduction succeeds, 1 otherwise. The
24179 parameter STRICT is a bitwise or of the following flags:
24180
24181 UNIFY_ALLOW_NONE:
24182 Require an exact match between PARM and ARG.
24183 UNIFY_ALLOW_MORE_CV_QUAL:
24184 Allow the deduced ARG to be more cv-qualified (by qualification
24185 conversion) than ARG.
24186 UNIFY_ALLOW_LESS_CV_QUAL:
24187 Allow the deduced ARG to be less cv-qualified than ARG.
24188 UNIFY_ALLOW_DERIVED:
24189 Allow the deduced ARG to be a template base class of ARG,
24190 or a pointer to a template base class of the type pointed to by
24191 ARG.
24192 UNIFY_ALLOW_INTEGER:
24193 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
24194 case for more information.
24195 UNIFY_ALLOW_OUTER_LEVEL:
24196 This is the outermost level of a deduction. Used to determine validity
24197 of qualification conversions. A valid qualification conversion must
24198 have const qualified pointers leading up to the inner type which
24199 requires additional CV quals, except at the outer level, where const
24200 is not required [conv.qual]. It would be normal to set this flag in
24201 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
24202 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
24203 This is the outermost level of a deduction, and PARM can be more CV
24204 qualified at this point.
24205 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
24206 This is the outermost level of a deduction, and PARM can be less CV
24207 qualified at this point. */
24208
24209 static int
24210 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
24211 bool explain_p)
24212 {
24213 int idx;
24214 tree targ;
24215 tree tparm;
24216 int strict_in = strict;
24217 tsubst_flags_t complain = (explain_p
24218 ? tf_warning_or_error
24219 : tf_none);
24220
24221 /* I don't think this will do the right thing with respect to types.
24222 But the only case I've seen it in so far has been array bounds, where
24223 signedness is the only information lost, and I think that will be
24224 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
24225 finish_id_expression_1, and are also OK. */
24226 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
24227 parm = TREE_OPERAND (parm, 0);
24228
24229 if (arg == error_mark_node)
24230 return unify_invalid (explain_p);
24231 if (arg == unknown_type_node
24232 || arg == init_list_type_node)
24233 /* We can't deduce anything from this, but we might get all the
24234 template args from other function args. */
24235 return unify_success (explain_p);
24236
24237 if (parm == any_targ_node || arg == any_targ_node)
24238 return unify_success (explain_p);
24239
24240 /* If PARM uses template parameters, then we can't bail out here,
24241 even if ARG == PARM, since we won't record unifications for the
24242 template parameters. We might need them if we're trying to
24243 figure out which of two things is more specialized. */
24244 if (arg == parm && !uses_template_parms (parm))
24245 return unify_success (explain_p);
24246
24247 /* Handle init lists early, so the rest of the function can assume
24248 we're dealing with a type. */
24249 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
24250 {
24251 tree elttype;
24252 tree orig_parm = parm;
24253
24254 if (!is_std_init_list (parm)
24255 && TREE_CODE (parm) != ARRAY_TYPE)
24256 /* We can only deduce from an initializer list argument if the
24257 parameter is std::initializer_list or an array; otherwise this
24258 is a non-deduced context. */
24259 return unify_success (explain_p);
24260
24261 if (TREE_CODE (parm) == ARRAY_TYPE)
24262 elttype = TREE_TYPE (parm);
24263 else
24264 {
24265 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
24266 /* Deduction is defined in terms of a single type, so just punt
24267 on the (bizarre) std::initializer_list<T...>. */
24268 if (PACK_EXPANSION_P (elttype))
24269 return unify_success (explain_p);
24270 }
24271
24272 if (strict != DEDUCE_EXACT
24273 && TYPE_P (elttype)
24274 && !uses_deducible_template_parms (elttype))
24275 /* If ELTTYPE has no deducible template parms, skip deduction from
24276 the list elements. */;
24277 else
24278 for (auto &e: CONSTRUCTOR_ELTS (arg))
24279 {
24280 tree elt = e.value;
24281 int elt_strict = strict;
24282
24283 if (elt == error_mark_node)
24284 return unify_invalid (explain_p);
24285
24286 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
24287 {
24288 tree type = TREE_TYPE (elt);
24289 if (type == error_mark_node)
24290 return unify_invalid (explain_p);
24291 /* It should only be possible to get here for a call. */
24292 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
24293 elt_strict |= maybe_adjust_types_for_deduction
24294 (tparms, DEDUCE_CALL, &elttype, &type, elt);
24295 elt = type;
24296 }
24297
24298 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
24299 explain_p);
24300 }
24301
24302 if (TREE_CODE (parm) == ARRAY_TYPE
24303 && deducible_array_bound (TYPE_DOMAIN (parm)))
24304 {
24305 /* Also deduce from the length of the initializer list. */
24306 tree max = size_int (CONSTRUCTOR_NELTS (arg));
24307 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
24308 if (idx == error_mark_node)
24309 return unify_invalid (explain_p);
24310 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
24311 idx, explain_p);
24312 }
24313
24314 /* If the std::initializer_list<T> deduction worked, replace the
24315 deduced A with std::initializer_list<A>. */
24316 if (orig_parm != parm)
24317 {
24318 idx = TEMPLATE_TYPE_IDX (orig_parm);
24319 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24320 targ = listify (targ);
24321 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
24322 }
24323 return unify_success (explain_p);
24324 }
24325
24326 /* If parm and arg aren't the same kind of thing (template, type, or
24327 expression), fail early. */
24328 if (pa_kind (parm) != pa_kind (arg))
24329 return unify_invalid (explain_p);
24330
24331 /* Immediately reject some pairs that won't unify because of
24332 cv-qualification mismatches. */
24333 if (TREE_CODE (arg) == TREE_CODE (parm)
24334 && TYPE_P (arg)
24335 /* It is the elements of the array which hold the cv quals of an array
24336 type, and the elements might be template type parms. We'll check
24337 when we recurse. */
24338 && TREE_CODE (arg) != ARRAY_TYPE
24339 /* We check the cv-qualifiers when unifying with template type
24340 parameters below. We want to allow ARG `const T' to unify with
24341 PARM `T' for example, when computing which of two templates
24342 is more specialized, for example. */
24343 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
24344 && !check_cv_quals_for_unify (strict_in, arg, parm))
24345 return unify_cv_qual_mismatch (explain_p, parm, arg);
24346
24347 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
24348 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
24349 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
24350 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
24351 strict &= ~UNIFY_ALLOW_DERIVED;
24352 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
24353 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
24354
24355 switch (TREE_CODE (parm))
24356 {
24357 case TYPENAME_TYPE:
24358 case SCOPE_REF:
24359 case UNBOUND_CLASS_TEMPLATE:
24360 /* In a type which contains a nested-name-specifier, template
24361 argument values cannot be deduced for template parameters used
24362 within the nested-name-specifier. */
24363 return unify_success (explain_p);
24364
24365 case TEMPLATE_TYPE_PARM:
24366 case TEMPLATE_TEMPLATE_PARM:
24367 case BOUND_TEMPLATE_TEMPLATE_PARM:
24368 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24369 if (error_operand_p (tparm))
24370 return unify_invalid (explain_p);
24371
24372 if (TEMPLATE_TYPE_LEVEL (parm)
24373 != template_decl_level (tparm))
24374 /* The PARM is not one we're trying to unify. Just check
24375 to see if it matches ARG. */
24376 {
24377 if (TREE_CODE (arg) == TREE_CODE (parm)
24378 && (is_auto (parm) ? is_auto (arg)
24379 : same_type_p (parm, arg)))
24380 return unify_success (explain_p);
24381 else
24382 return unify_type_mismatch (explain_p, parm, arg);
24383 }
24384 idx = TEMPLATE_TYPE_IDX (parm);
24385 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24386 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
24387 if (error_operand_p (tparm))
24388 return unify_invalid (explain_p);
24389
24390 /* Check for mixed types and values. */
24391 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
24392 && TREE_CODE (tparm) != TYPE_DECL)
24393 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24394 && TREE_CODE (tparm) != TEMPLATE_DECL))
24395 gcc_unreachable ();
24396
24397 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24398 {
24399 if ((strict_in & UNIFY_ALLOW_DERIVED)
24400 && CLASS_TYPE_P (arg))
24401 {
24402 /* First try to match ARG directly. */
24403 tree t = try_class_unification (tparms, targs, parm, arg,
24404 explain_p);
24405 if (!t)
24406 {
24407 /* Otherwise, look for a suitable base of ARG, as below. */
24408 enum template_base_result r;
24409 r = get_template_base (tparms, targs, parm, arg,
24410 explain_p, &t);
24411 if (!t)
24412 return unify_no_common_base (explain_p, r, parm, arg);
24413 arg = t;
24414 }
24415 }
24416 /* ARG must be constructed from a template class or a template
24417 template parameter. */
24418 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
24419 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
24420 return unify_template_deduction_failure (explain_p, parm, arg);
24421
24422 /* Deduce arguments T, i from TT<T> or TT<i>. */
24423 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
24424 return 1;
24425
24426 arg = TYPE_TI_TEMPLATE (arg);
24427 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
24428 /* If the template is a template template parameter, use the
24429 TEMPLATE_TEMPLATE_PARM for matching. */
24430 arg = TREE_TYPE (arg);
24431
24432 /* Fall through to deduce template name. */
24433 }
24434
24435 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
24436 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
24437 {
24438 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
24439
24440 /* Simple cases: Value already set, does match or doesn't. */
24441 if (targ != NULL_TREE && template_args_equal (targ, arg))
24442 return unify_success (explain_p);
24443 else if (targ)
24444 return unify_inconsistency (explain_p, parm, targ, arg);
24445 }
24446 else
24447 {
24448 /* If PARM is `const T' and ARG is only `int', we don't have
24449 a match unless we are allowing additional qualification.
24450 If ARG is `const int' and PARM is just `T' that's OK;
24451 that binds `const int' to `T'. */
24452 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
24453 arg, parm))
24454 return unify_cv_qual_mismatch (explain_p, parm, arg);
24455
24456 /* Consider the case where ARG is `const volatile int' and
24457 PARM is `const T'. Then, T should be `volatile int'. */
24458 arg = cp_build_qualified_type
24459 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
24460 if (arg == error_mark_node)
24461 return unify_invalid (explain_p);
24462
24463 /* Simple cases: Value already set, does match or doesn't. */
24464 if (targ != NULL_TREE && same_type_p (targ, arg))
24465 return unify_success (explain_p);
24466 else if (targ)
24467 return unify_inconsistency (explain_p, parm, targ, arg);
24468
24469 /* Make sure that ARG is not a variable-sized array. (Note
24470 that were talking about variable-sized arrays (like
24471 `int[n]'), rather than arrays of unknown size (like
24472 `int[]').) We'll get very confused by such a type since
24473 the bound of the array is not constant, and therefore
24474 not mangleable. Besides, such types are not allowed in
24475 ISO C++, so we can do as we please here. We do allow
24476 them for 'auto' deduction, since that isn't ABI-exposed. */
24477 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
24478 return unify_vla_arg (explain_p, arg);
24479
24480 /* Strip typedefs as in convert_template_argument. */
24481 arg = canonicalize_type_argument (arg, tf_none);
24482 }
24483
24484 /* If ARG is a parameter pack or an expansion, we cannot unify
24485 against it unless PARM is also a parameter pack. */
24486 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24487 && !template_parameter_pack_p (parm))
24488 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24489
24490 /* If the argument deduction results is a METHOD_TYPE,
24491 then there is a problem.
24492 METHOD_TYPE doesn't map to any real C++ type the result of
24493 the deduction cannot be of that type. */
24494 if (TREE_CODE (arg) == METHOD_TYPE)
24495 return unify_method_type_error (explain_p, arg);
24496
24497 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24498 return unify_success (explain_p);
24499
24500 case TEMPLATE_PARM_INDEX:
24501 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
24502 if (error_operand_p (tparm))
24503 return unify_invalid (explain_p);
24504
24505 if (TEMPLATE_PARM_LEVEL (parm)
24506 != template_decl_level (tparm))
24507 {
24508 /* The PARM is not one we're trying to unify. Just check
24509 to see if it matches ARG. */
24510 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
24511 && cp_tree_equal (parm, arg));
24512 if (result)
24513 unify_expression_unequal (explain_p, parm, arg);
24514 return result;
24515 }
24516
24517 idx = TEMPLATE_PARM_IDX (parm);
24518 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
24519
24520 if (targ)
24521 {
24522 if ((strict & UNIFY_ALLOW_INTEGER)
24523 && TREE_TYPE (targ) && TREE_TYPE (arg)
24524 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
24525 /* We're deducing from an array bound, the type doesn't matter. */
24526 arg = fold_convert (TREE_TYPE (targ), arg);
24527 int x = !cp_tree_equal (targ, arg);
24528 if (x)
24529 unify_inconsistency (explain_p, parm, targ, arg);
24530 return x;
24531 }
24532
24533 /* [temp.deduct.type] If, in the declaration of a function template
24534 with a non-type template-parameter, the non-type
24535 template-parameter is used in an expression in the function
24536 parameter-list and, if the corresponding template-argument is
24537 deduced, the template-argument type shall match the type of the
24538 template-parameter exactly, except that a template-argument
24539 deduced from an array bound may be of any integral type.
24540 The non-type parameter might use already deduced type parameters. */
24541 tparm = TREE_TYPE (parm);
24542 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
24543 /* We don't have enough levels of args to do any substitution. This
24544 can happen in the context of -fnew-ttp-matching. */;
24545 else
24546 {
24547 ++processing_template_decl;
24548 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
24549 --processing_template_decl;
24550
24551 if (tree a = type_uses_auto (tparm))
24552 {
24553 tparm = do_auto_deduction (tparm, arg, a,
24554 complain, adc_unify, targs);
24555 if (tparm == error_mark_node)
24556 return 1;
24557 }
24558 }
24559
24560 if (!TREE_TYPE (arg)
24561 || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
24562 /* Template-parameter dependent expression. Just accept it for now.
24563 It will later be processed in convert_template_argument. */
24564 ;
24565 else if (same_type_ignoring_top_level_qualifiers_p
24566 (non_reference (TREE_TYPE (arg)),
24567 non_reference (tparm)))
24568 /* OK. Ignore top-level quals here because a class-type template
24569 parameter object is const. */;
24570 else if ((strict & UNIFY_ALLOW_INTEGER)
24571 && CP_INTEGRAL_TYPE_P (tparm))
24572 /* Convert the ARG to the type of PARM; the deduced non-type
24573 template argument must exactly match the types of the
24574 corresponding parameter. */
24575 arg = fold (build_nop (tparm, arg));
24576 else if (uses_template_parms (tparm))
24577 {
24578 /* We haven't deduced the type of this parameter yet. */
24579 if (cxx_dialect >= cxx17
24580 /* We deduce from array bounds in try_array_deduction. */
24581 && !(strict & UNIFY_ALLOW_INTEGER)
24582 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
24583 {
24584 /* Deduce it from the non-type argument. As above, ignore
24585 top-level quals here too. */
24586 tree atype = cv_unqualified (TREE_TYPE (arg));
24587 RECUR_AND_CHECK_FAILURE (tparms, targs,
24588 tparm, atype,
24589 UNIFY_ALLOW_NONE, explain_p);
24590 /* Now check whether the type of this parameter is still
24591 dependent, and give up if so. */
24592 ++processing_template_decl;
24593 tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
24594 --processing_template_decl;
24595 if (uses_template_parms (tparm))
24596 return unify_success (explain_p);
24597 }
24598 else
24599 /* Try again later. */
24600 return unify_success (explain_p);
24601 }
24602 else
24603 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
24604
24605 /* If ARG is a parameter pack or an expansion, we cannot unify
24606 against it unless PARM is also a parameter pack. */
24607 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
24608 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
24609 return unify_parameter_pack_mismatch (explain_p, parm, arg);
24610
24611 {
24612 bool removed_attr = false;
24613 arg = strip_typedefs_expr (arg, &removed_attr);
24614 }
24615 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
24616 return unify_success (explain_p);
24617
24618 case PTRMEM_CST:
24619 {
24620 /* A pointer-to-member constant can be unified only with
24621 another constant. */
24622 if (TREE_CODE (arg) != PTRMEM_CST)
24623 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
24624
24625 /* Just unify the class member. It would be useless (and possibly
24626 wrong, depending on the strict flags) to unify also
24627 PTRMEM_CST_CLASS, because we want to be sure that both parm and
24628 arg refer to the same variable, even if through different
24629 classes. For instance:
24630
24631 struct A { int x; };
24632 struct B : A { };
24633
24634 Unification of &A::x and &B::x must succeed. */
24635 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
24636 PTRMEM_CST_MEMBER (arg), strict, explain_p);
24637 }
24638
24639 case POINTER_TYPE:
24640 {
24641 if (!TYPE_PTR_P (arg))
24642 return unify_type_mismatch (explain_p, parm, arg);
24643
24644 /* [temp.deduct.call]
24645
24646 A can be another pointer or pointer to member type that can
24647 be converted to the deduced A via a qualification
24648 conversion (_conv.qual_).
24649
24650 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
24651 This will allow for additional cv-qualification of the
24652 pointed-to types if appropriate. */
24653
24654 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
24655 /* The derived-to-base conversion only persists through one
24656 level of pointers. */
24657 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
24658
24659 return unify (tparms, targs, TREE_TYPE (parm),
24660 TREE_TYPE (arg), strict, explain_p);
24661 }
24662
24663 case REFERENCE_TYPE:
24664 if (!TYPE_REF_P (arg))
24665 return unify_type_mismatch (explain_p, parm, arg);
24666 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24667 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
24668
24669 case ARRAY_TYPE:
24670 if (TREE_CODE (arg) != ARRAY_TYPE)
24671 return unify_type_mismatch (explain_p, parm, arg);
24672 if ((TYPE_DOMAIN (parm) == NULL_TREE)
24673 != (TYPE_DOMAIN (arg) == NULL_TREE))
24674 return unify_type_mismatch (explain_p, parm, arg);
24675 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24676 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
24677 if (TYPE_DOMAIN (parm) != NULL_TREE)
24678 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
24679 TYPE_DOMAIN (arg), explain_p);
24680 return unify_success (explain_p);
24681
24682 case REAL_TYPE:
24683 case COMPLEX_TYPE:
24684 case VECTOR_TYPE:
24685 case INTEGER_TYPE:
24686 case BOOLEAN_TYPE:
24687 case ENUMERAL_TYPE:
24688 case VOID_TYPE:
24689 case OPAQUE_TYPE:
24690 case NULLPTR_TYPE:
24691 if (TREE_CODE (arg) != TREE_CODE (parm))
24692 return unify_type_mismatch (explain_p, parm, arg);
24693
24694 /* We have already checked cv-qualification at the top of the
24695 function. */
24696 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
24697 return unify_type_mismatch (explain_p, parm, arg);
24698
24699 /* As far as unification is concerned, this wins. Later checks
24700 will invalidate it if necessary. */
24701 return unify_success (explain_p);
24702
24703 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
24704 /* Type INTEGER_CST can come from ordinary constant template args. */
24705 case INTEGER_CST:
24706 while (CONVERT_EXPR_P (arg))
24707 arg = TREE_OPERAND (arg, 0);
24708
24709 if (TREE_CODE (arg) != INTEGER_CST)
24710 return unify_template_argument_mismatch (explain_p, parm, arg);
24711 return (tree_int_cst_equal (parm, arg)
24712 ? unify_success (explain_p)
24713 : unify_template_argument_mismatch (explain_p, parm, arg));
24714
24715 case TREE_VEC:
24716 {
24717 int i, len, argslen;
24718 int parm_variadic_p = 0;
24719
24720 if (TREE_CODE (arg) != TREE_VEC)
24721 return unify_template_argument_mismatch (explain_p, parm, arg);
24722
24723 len = TREE_VEC_LENGTH (parm);
24724 argslen = TREE_VEC_LENGTH (arg);
24725
24726 /* Check for pack expansions in the parameters. */
24727 for (i = 0; i < len; ++i)
24728 {
24729 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
24730 {
24731 if (i == len - 1)
24732 /* We can unify against something with a trailing
24733 parameter pack. */
24734 parm_variadic_p = 1;
24735 else
24736 /* [temp.deduct.type]/9: If the template argument list of
24737 P contains a pack expansion that is not the last
24738 template argument, the entire template argument list
24739 is a non-deduced context. */
24740 return unify_success (explain_p);
24741 }
24742 }
24743
24744 /* If we don't have enough arguments to satisfy the parameters
24745 (not counting the pack expression at the end), or we have
24746 too many arguments for a parameter list that doesn't end in
24747 a pack expression, we can't unify. */
24748 if (parm_variadic_p
24749 ? argslen < len - parm_variadic_p
24750 : argslen != len)
24751 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
24752
24753 /* Unify all of the parameters that precede the (optional)
24754 pack expression. */
24755 for (i = 0; i < len - parm_variadic_p; ++i)
24756 {
24757 RECUR_AND_CHECK_FAILURE (tparms, targs,
24758 TREE_VEC_ELT (parm, i),
24759 TREE_VEC_ELT (arg, i),
24760 UNIFY_ALLOW_NONE, explain_p);
24761 }
24762 if (parm_variadic_p)
24763 return unify_pack_expansion (tparms, targs, parm, arg,
24764 DEDUCE_EXACT,
24765 /*subr=*/true, explain_p);
24766 return unify_success (explain_p);
24767 }
24768
24769 case RECORD_TYPE:
24770 case UNION_TYPE:
24771 if (TREE_CODE (arg) != TREE_CODE (parm))
24772 return unify_type_mismatch (explain_p, parm, arg);
24773
24774 if (TYPE_PTRMEMFUNC_P (parm))
24775 {
24776 if (!TYPE_PTRMEMFUNC_P (arg))
24777 return unify_type_mismatch (explain_p, parm, arg);
24778
24779 return unify (tparms, targs,
24780 TYPE_PTRMEMFUNC_FN_TYPE (parm),
24781 TYPE_PTRMEMFUNC_FN_TYPE (arg),
24782 strict, explain_p);
24783 }
24784 else if (TYPE_PTRMEMFUNC_P (arg))
24785 return unify_type_mismatch (explain_p, parm, arg);
24786
24787 if (CLASSTYPE_TEMPLATE_INFO (parm))
24788 {
24789 tree t = NULL_TREE;
24790
24791 if (strict_in & UNIFY_ALLOW_DERIVED)
24792 {
24793 /* First, we try to unify the PARM and ARG directly. */
24794 t = try_class_unification (tparms, targs,
24795 parm, arg, explain_p);
24796
24797 if (!t)
24798 {
24799 /* Fallback to the special case allowed in
24800 [temp.deduct.call]:
24801
24802 If P is a class, and P has the form
24803 template-id, then A can be a derived class of
24804 the deduced A. Likewise, if P is a pointer to
24805 a class of the form template-id, A can be a
24806 pointer to a derived class pointed to by the
24807 deduced A. */
24808 enum template_base_result r;
24809 r = get_template_base (tparms, targs, parm, arg,
24810 explain_p, &t);
24811
24812 if (!t)
24813 {
24814 /* Don't give the derived diagnostic if we're
24815 already dealing with the same template. */
24816 bool same_template
24817 = (CLASSTYPE_TEMPLATE_INFO (arg)
24818 && (CLASSTYPE_TI_TEMPLATE (parm)
24819 == CLASSTYPE_TI_TEMPLATE (arg)));
24820 return unify_no_common_base (explain_p && !same_template,
24821 r, parm, arg);
24822 }
24823 }
24824 }
24825 else if (CLASSTYPE_TEMPLATE_INFO (arg)
24826 && (CLASSTYPE_TI_TEMPLATE (parm)
24827 == CLASSTYPE_TI_TEMPLATE (arg)))
24828 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24829 Then, we should unify `int' and `U'. */
24830 t = arg;
24831 else
24832 /* There's no chance of unification succeeding. */
24833 return unify_type_mismatch (explain_p, parm, arg);
24834
24835 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
24836 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
24837 }
24838 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
24839 return unify_type_mismatch (explain_p, parm, arg);
24840 return unify_success (explain_p);
24841
24842 case METHOD_TYPE:
24843 case FUNCTION_TYPE:
24844 {
24845 unsigned int nargs;
24846 tree *args;
24847 tree a;
24848 unsigned int i;
24849
24850 if (TREE_CODE (arg) != TREE_CODE (parm))
24851 return unify_type_mismatch (explain_p, parm, arg);
24852
24853 /* CV qualifications for methods can never be deduced, they must
24854 match exactly. We need to check them explicitly here,
24855 because type_unification_real treats them as any other
24856 cv-qualified parameter. */
24857 if (TREE_CODE (parm) == METHOD_TYPE
24858 && (!check_cv_quals_for_unify
24859 (UNIFY_ALLOW_NONE,
24860 class_of_this_parm (arg),
24861 class_of_this_parm (parm))))
24862 return unify_cv_qual_mismatch (explain_p, parm, arg);
24863 if (TREE_CODE (arg) == FUNCTION_TYPE
24864 && type_memfn_quals (parm) != type_memfn_quals (arg))
24865 return unify_cv_qual_mismatch (explain_p, parm, arg);
24866 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
24867 return unify_type_mismatch (explain_p, parm, arg);
24868
24869 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
24870 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
24871
24872 nargs = list_length (TYPE_ARG_TYPES (arg));
24873 args = XALLOCAVEC (tree, nargs);
24874 for (a = TYPE_ARG_TYPES (arg), i = 0;
24875 a != NULL_TREE && a != void_list_node;
24876 a = TREE_CHAIN (a), ++i)
24877 args[i] = TREE_VALUE (a);
24878 nargs = i;
24879
24880 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
24881 args, nargs, 1, DEDUCE_EXACT,
24882 NULL, explain_p))
24883 return 1;
24884
24885 if (flag_noexcept_type)
24886 {
24887 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
24888 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
24889 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
24890 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
24891 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
24892 && uses_template_parms (TREE_PURPOSE (pspec)))
24893 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
24894 TREE_PURPOSE (aspec),
24895 UNIFY_ALLOW_NONE, explain_p);
24896 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
24897 return unify_type_mismatch (explain_p, parm, arg);
24898 }
24899
24900 return 0;
24901 }
24902
24903 case OFFSET_TYPE:
24904 /* Unify a pointer to member with a pointer to member function, which
24905 deduces the type of the member as a function type. */
24906 if (TYPE_PTRMEMFUNC_P (arg))
24907 {
24908 /* Check top-level cv qualifiers */
24909 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
24910 return unify_cv_qual_mismatch (explain_p, parm, arg);
24911
24912 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24913 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
24914 UNIFY_ALLOW_NONE, explain_p);
24915
24916 /* Determine the type of the function we are unifying against. */
24917 tree fntype = static_fn_type (arg);
24918
24919 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
24920 }
24921
24922 if (TREE_CODE (arg) != OFFSET_TYPE)
24923 return unify_type_mismatch (explain_p, parm, arg);
24924 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24925 TYPE_OFFSET_BASETYPE (arg),
24926 UNIFY_ALLOW_NONE, explain_p);
24927 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24928 strict, explain_p);
24929
24930 case CONST_DECL:
24931 if (DECL_TEMPLATE_PARM_P (parm))
24932 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
24933 if (arg != scalar_constant_value (parm))
24934 return unify_template_argument_mismatch (explain_p, parm, arg);
24935 return unify_success (explain_p);
24936
24937 case FIELD_DECL:
24938 case TEMPLATE_DECL:
24939 /* Matched cases are handled by the ARG == PARM test above. */
24940 return unify_template_argument_mismatch (explain_p, parm, arg);
24941
24942 case VAR_DECL:
24943 /* We might get a variable as a non-type template argument in parm if the
24944 corresponding parameter is type-dependent. Make any necessary
24945 adjustments based on whether arg is a reference. */
24946 if (CONSTANT_CLASS_P (arg))
24947 parm = fold_non_dependent_expr (parm, complain);
24948 else if (REFERENCE_REF_P (arg))
24949 {
24950 tree sub = TREE_OPERAND (arg, 0);
24951 STRIP_NOPS (sub);
24952 if (TREE_CODE (sub) == ADDR_EXPR)
24953 arg = TREE_OPERAND (sub, 0);
24954 }
24955 /* Now use the normal expression code to check whether they match. */
24956 goto expr;
24957
24958 case TYPE_ARGUMENT_PACK:
24959 case NONTYPE_ARGUMENT_PACK:
24960 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
24961 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
24962
24963 case TYPEOF_TYPE:
24964 case DECLTYPE_TYPE:
24965 case TRAIT_TYPE:
24966 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24967 or TRAIT_TYPE nodes. */
24968 return unify_success (explain_p);
24969
24970 case ERROR_MARK:
24971 /* Unification fails if we hit an error node. */
24972 return unify_invalid (explain_p);
24973
24974 case INDIRECT_REF:
24975 if (REFERENCE_REF_P (parm))
24976 {
24977 bool pexp = PACK_EXPANSION_P (arg);
24978 if (pexp)
24979 arg = PACK_EXPANSION_PATTERN (arg);
24980 if (REFERENCE_REF_P (arg))
24981 arg = TREE_OPERAND (arg, 0);
24982 if (pexp)
24983 arg = make_pack_expansion (arg, complain);
24984 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24985 strict, explain_p);
24986 }
24987 /* FALLTHRU */
24988
24989 default:
24990 /* An unresolved overload is a nondeduced context. */
24991 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24992 return unify_success (explain_p);
24993 gcc_assert (EXPR_P (parm)
24994 || TREE_CODE (parm) == CONSTRUCTOR
24995 || TREE_CODE (parm) == TRAIT_EXPR);
24996 expr:
24997 /* We must be looking at an expression. This can happen with
24998 something like:
24999
25000 template <int I>
25001 void foo(S<I>, S<I + 2>);
25002
25003 or
25004
25005 template<typename T>
25006 void foo(A<T, T{}>);
25007
25008 This is a "non-deduced context":
25009
25010 [deduct.type]
25011
25012 The non-deduced contexts are:
25013
25014 --A non-type template argument or an array bound in which
25015 a subexpression references a template parameter.
25016
25017 In these cases, we assume deduction succeeded, but don't
25018 actually infer any unifications. */
25019
25020 if (!uses_template_parms (parm)
25021 && !template_args_equal (parm, arg))
25022 return unify_expression_unequal (explain_p, parm, arg);
25023 else
25024 return unify_success (explain_p);
25025 }
25026 }
25027 #undef RECUR_AND_CHECK_FAILURE
25028 \f
25029 /* Note that DECL can be defined in this translation unit, if
25030 required. */
25031
25032 static void
25033 mark_definable (tree decl)
25034 {
25035 tree clone;
25036 DECL_NOT_REALLY_EXTERN (decl) = 1;
25037 FOR_EACH_CLONE (clone, decl)
25038 DECL_NOT_REALLY_EXTERN (clone) = 1;
25039 }
25040
25041 /* Called if RESULT is explicitly instantiated, or is a member of an
25042 explicitly instantiated class. */
25043
25044 void
25045 mark_decl_instantiated (tree result, int extern_p)
25046 {
25047 SET_DECL_EXPLICIT_INSTANTIATION (result);
25048
25049 /* If this entity has already been written out, it's too late to
25050 make any modifications. */
25051 if (TREE_ASM_WRITTEN (result))
25052 return;
25053
25054 /* consteval functions are never emitted. */
25055 if (TREE_CODE (result) == FUNCTION_DECL
25056 && DECL_IMMEDIATE_FUNCTION_P (result))
25057 return;
25058
25059 /* For anonymous namespace we don't need to do anything. */
25060 if (decl_internal_context_p (result))
25061 {
25062 gcc_assert (!TREE_PUBLIC (result));
25063 return;
25064 }
25065
25066 if (TREE_CODE (result) != FUNCTION_DECL)
25067 /* The TREE_PUBLIC flag for function declarations will have been
25068 set correctly by tsubst. */
25069 TREE_PUBLIC (result) = 1;
25070
25071 if (extern_p)
25072 {
25073 DECL_EXTERNAL (result) = 1;
25074 DECL_NOT_REALLY_EXTERN (result) = 0;
25075 }
25076 else
25077 {
25078 mark_definable (result);
25079 mark_needed (result);
25080 /* Always make artificials weak. */
25081 if (DECL_ARTIFICIAL (result) && flag_weak)
25082 comdat_linkage (result);
25083 /* For WIN32 we also want to put explicit instantiations in
25084 linkonce sections. */
25085 else if (TREE_PUBLIC (result))
25086 maybe_make_one_only (result);
25087 if (TREE_CODE (result) == FUNCTION_DECL
25088 && DECL_TEMPLATE_INSTANTIATED (result))
25089 /* If the function has already been instantiated, clear DECL_EXTERNAL,
25090 since start_preparsed_function wouldn't have if we had an earlier
25091 extern explicit instantiation. */
25092 DECL_EXTERNAL (result) = 0;
25093 }
25094
25095 /* If EXTERN_P, then this function will not be emitted -- unless
25096 followed by an explicit instantiation, at which point its linkage
25097 will be adjusted. If !EXTERN_P, then this function will be
25098 emitted here. In neither circumstance do we want
25099 import_export_decl to adjust the linkage. */
25100 DECL_INTERFACE_KNOWN (result) = 1;
25101 }
25102
25103 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
25104 important template arguments. If any are missing, we check whether
25105 they're important by using error_mark_node for substituting into any
25106 args that were used for partial ordering (the ones between ARGS and END)
25107 and seeing if it bubbles up. */
25108
25109 static bool
25110 check_undeduced_parms (tree targs, tree args, tree end)
25111 {
25112 bool found = false;
25113 for (tree& targ : tree_vec_range (targs))
25114 if (targ == NULL_TREE)
25115 {
25116 found = true;
25117 targ = error_mark_node;
25118 }
25119 if (found)
25120 {
25121 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
25122 if (substed == error_mark_node)
25123 return true;
25124 }
25125 return false;
25126 }
25127
25128 /* Given two function templates PAT1 and PAT2, return:
25129
25130 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
25131 -1 if PAT2 is more specialized than PAT1.
25132 0 if neither is more specialized.
25133
25134 LEN indicates the number of parameters we should consider
25135 (defaulted parameters should not be considered).
25136
25137 The 1998 std underspecified function template partial ordering, and
25138 DR214 addresses the issue. We take pairs of arguments, one from
25139 each of the templates, and deduce them against each other. One of
25140 the templates will be more specialized if all the *other*
25141 template's arguments deduce against its arguments and at least one
25142 of its arguments *does* *not* deduce against the other template's
25143 corresponding argument. Deduction is done as for class templates.
25144 The arguments used in deduction have reference and top level cv
25145 qualifiers removed. Iff both arguments were originally reference
25146 types *and* deduction succeeds in both directions, an lvalue reference
25147 wins against an rvalue reference and otherwise the template
25148 with the more cv-qualified argument wins for that pairing (if
25149 neither is more cv-qualified, they both are equal). Unlike regular
25150 deduction, after all the arguments have been deduced in this way,
25151 we do *not* verify the deduced template argument values can be
25152 substituted into non-deduced contexts.
25153
25154 The logic can be a bit confusing here, because we look at deduce1 and
25155 targs1 to see if pat2 is at least as specialized, and vice versa; if we
25156 can find template arguments for pat1 to make arg1 look like arg2, that
25157 means that arg2 is at least as specialized as arg1. */
25158
25159 int
25160 more_specialized_fn (tree pat1, tree pat2, int len)
25161 {
25162 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
25163 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
25164 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
25165 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
25166 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
25167 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
25168 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
25169 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
25170 tree origs1, origs2;
25171 bool lose1 = false;
25172 bool lose2 = false;
25173
25174 /* Remove the this parameter from non-static member functions. If
25175 one is a non-static member function and the other is not a static
25176 member function, remove the first parameter from that function
25177 also. This situation occurs for operator functions where we
25178 locate both a member function (with this pointer) and non-member
25179 operator (with explicit first operand). */
25180 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
25181 {
25182 len--; /* LEN is the number of significant arguments for DECL1 */
25183 args1 = TREE_CHAIN (args1);
25184 if (!DECL_STATIC_FUNCTION_P (decl2))
25185 args2 = TREE_CHAIN (args2);
25186 }
25187 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
25188 {
25189 args2 = TREE_CHAIN (args2);
25190 if (!DECL_STATIC_FUNCTION_P (decl1))
25191 {
25192 len--;
25193 args1 = TREE_CHAIN (args1);
25194 }
25195 }
25196
25197 /* If only one is a conversion operator, they are unordered. */
25198 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
25199 return 0;
25200
25201 /* Consider the return type for a conversion function */
25202 if (DECL_CONV_FN_P (decl1))
25203 {
25204 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
25205 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
25206 len++;
25207 }
25208
25209 processing_template_decl++;
25210
25211 origs1 = args1;
25212 origs2 = args2;
25213
25214 while (len--
25215 /* Stop when an ellipsis is seen. */
25216 && args1 != NULL_TREE && args2 != NULL_TREE)
25217 {
25218 tree arg1 = TREE_VALUE (args1);
25219 tree arg2 = TREE_VALUE (args2);
25220 int deduce1, deduce2;
25221 int quals1 = -1;
25222 int quals2 = -1;
25223 int ref1 = 0;
25224 int ref2 = 0;
25225
25226 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25227 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25228 {
25229 /* When both arguments are pack expansions, we need only
25230 unify the patterns themselves. */
25231 arg1 = PACK_EXPANSION_PATTERN (arg1);
25232 arg2 = PACK_EXPANSION_PATTERN (arg2);
25233
25234 /* This is the last comparison we need to do. */
25235 len = 0;
25236 }
25237
25238 if (TYPE_REF_P (arg1))
25239 {
25240 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
25241 arg1 = TREE_TYPE (arg1);
25242 quals1 = cp_type_quals (arg1);
25243 }
25244
25245 if (TYPE_REF_P (arg2))
25246 {
25247 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
25248 arg2 = TREE_TYPE (arg2);
25249 quals2 = cp_type_quals (arg2);
25250 }
25251
25252 arg1 = TYPE_MAIN_VARIANT (arg1);
25253 arg2 = TYPE_MAIN_VARIANT (arg2);
25254
25255 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
25256 {
25257 int i, len2 = remaining_arguments (args2);
25258 tree parmvec = make_tree_vec (1);
25259 tree argvec = make_tree_vec (len2);
25260 tree ta = args2;
25261
25262 /* Setup the parameter vector, which contains only ARG1. */
25263 TREE_VEC_ELT (parmvec, 0) = arg1;
25264
25265 /* Setup the argument vector, which contains the remaining
25266 arguments. */
25267 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
25268 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25269
25270 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
25271 argvec, DEDUCE_EXACT,
25272 /*subr=*/true, /*explain_p=*/false)
25273 == 0);
25274
25275 /* We cannot deduce in the other direction, because ARG1 is
25276 a pack expansion but ARG2 is not. */
25277 deduce2 = 0;
25278 }
25279 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25280 {
25281 int i, len1 = remaining_arguments (args1);
25282 tree parmvec = make_tree_vec (1);
25283 tree argvec = make_tree_vec (len1);
25284 tree ta = args1;
25285
25286 /* Setup the parameter vector, which contains only ARG1. */
25287 TREE_VEC_ELT (parmvec, 0) = arg2;
25288
25289 /* Setup the argument vector, which contains the remaining
25290 arguments. */
25291 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
25292 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
25293
25294 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
25295 argvec, DEDUCE_EXACT,
25296 /*subr=*/true, /*explain_p=*/false)
25297 == 0);
25298
25299 /* We cannot deduce in the other direction, because ARG2 is
25300 a pack expansion but ARG1 is not.*/
25301 deduce1 = 0;
25302 }
25303
25304 else
25305 {
25306 /* The normal case, where neither argument is a pack
25307 expansion. */
25308 deduce1 = (unify (tparms1, targs1, arg1, arg2,
25309 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25310 == 0);
25311 deduce2 = (unify (tparms2, targs2, arg2, arg1,
25312 UNIFY_ALLOW_NONE, /*explain_p=*/false)
25313 == 0);
25314 }
25315
25316 /* If we couldn't deduce arguments for tparms1 to make arg1 match
25317 arg2, then arg2 is not as specialized as arg1. */
25318 if (!deduce1)
25319 lose2 = true;
25320 if (!deduce2)
25321 lose1 = true;
25322
25323 /* "If, for a given type, deduction succeeds in both directions
25324 (i.e., the types are identical after the transformations above)
25325 and both P and A were reference types (before being replaced with
25326 the type referred to above):
25327 - if the type from the argument template was an lvalue reference and
25328 the type from the parameter template was not, the argument type is
25329 considered to be more specialized than the other; otherwise,
25330 - if the type from the argument template is more cv-qualified
25331 than the type from the parameter template (as described above),
25332 the argument type is considered to be more specialized than the other;
25333 otherwise,
25334 - neither type is more specialized than the other." */
25335
25336 if (deduce1 && deduce2)
25337 {
25338 if (ref1 && ref2 && ref1 != ref2)
25339 {
25340 if (ref1 > ref2)
25341 lose1 = true;
25342 else
25343 lose2 = true;
25344 }
25345 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
25346 {
25347 if ((quals1 & quals2) == quals2)
25348 lose2 = true;
25349 if ((quals1 & quals2) == quals1)
25350 lose1 = true;
25351 }
25352 }
25353
25354 if (lose1 && lose2)
25355 /* We've failed to deduce something in either direction.
25356 These must be unordered. */
25357 break;
25358
25359 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
25360 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
25361 /* We have already processed all of the arguments in our
25362 handing of the pack expansion type. */
25363 len = 0;
25364
25365 args1 = TREE_CHAIN (args1);
25366 args2 = TREE_CHAIN (args2);
25367 }
25368
25369 /* "In most cases, all template parameters must have values in order for
25370 deduction to succeed, but for partial ordering purposes a template
25371 parameter may remain without a value provided it is not used in the
25372 types being used for partial ordering."
25373
25374 Thus, if we are missing any of the targs1 we need to substitute into
25375 origs1, then pat2 is not as specialized as pat1. This can happen when
25376 there is a nondeduced context. */
25377 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
25378 lose2 = true;
25379 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
25380 lose1 = true;
25381
25382 processing_template_decl--;
25383
25384 /* If both deductions succeed, the partial ordering selects the more
25385 constrained template. */
25386 /* P2113: If the corresponding template-parameters of the
25387 template-parameter-lists are not equivalent ([temp.over.link]) or if
25388 the function parameters that positionally correspond between the two
25389 templates are not of the same type, neither template is more
25390 specialized than the other. */
25391 if (!lose1 && !lose2
25392 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
25393 DECL_TEMPLATE_PARMS (pat2))
25394 && compparms (origs1, origs2))
25395 {
25396 int winner = more_constrained (decl1, decl2);
25397 if (winner > 0)
25398 lose2 = true;
25399 else if (winner < 0)
25400 lose1 = true;
25401 }
25402
25403 /* All things being equal, if the next argument is a pack expansion
25404 for one function but not for the other, prefer the
25405 non-variadic function. FIXME this is bogus; see c++/41958. */
25406 if (lose1 == lose2
25407 && args1 && TREE_VALUE (args1)
25408 && args2 && TREE_VALUE (args2))
25409 {
25410 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
25411 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
25412 }
25413
25414 if (lose1 == lose2)
25415 return 0;
25416 else if (!lose1)
25417 return 1;
25418 else
25419 return -1;
25420 }
25421
25422 /* Determine which of two partial specializations of TMPL is more
25423 specialized.
25424
25425 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
25426 to the first partial specialization. The TREE_PURPOSE is the
25427 innermost set of template parameters for the partial
25428 specialization. PAT2 is similar, but for the second template.
25429
25430 Return 1 if the first partial specialization is more specialized;
25431 -1 if the second is more specialized; 0 if neither is more
25432 specialized.
25433
25434 See [temp.class.order] for information about determining which of
25435 two templates is more specialized. */
25436
25437 static int
25438 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
25439 {
25440 tree targs;
25441 int winner = 0;
25442 bool any_deductions = false;
25443
25444 tree tmpl1 = TREE_VALUE (pat1);
25445 tree tmpl2 = TREE_VALUE (pat2);
25446 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
25447 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
25448
25449 /* Just like what happens for functions, if we are ordering between
25450 different template specializations, we may encounter dependent
25451 types in the arguments, and we need our dependency check functions
25452 to behave correctly. */
25453 ++processing_template_decl;
25454 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
25455 if (targs)
25456 {
25457 --winner;
25458 any_deductions = true;
25459 }
25460
25461 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
25462 if (targs)
25463 {
25464 ++winner;
25465 any_deductions = true;
25466 }
25467 --processing_template_decl;
25468
25469 /* If both deductions succeed, the partial ordering selects the more
25470 constrained template. */
25471 if (!winner && any_deductions)
25472 winner = more_constrained (tmpl1, tmpl2);
25473
25474 /* In the case of a tie where at least one of the templates
25475 has a parameter pack at the end, the template with the most
25476 non-packed parameters wins. */
25477 if (winner == 0
25478 && any_deductions
25479 && (template_args_variadic_p (TREE_PURPOSE (pat1))
25480 || template_args_variadic_p (TREE_PURPOSE (pat2))))
25481 {
25482 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
25483 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
25484 int len1 = TREE_VEC_LENGTH (args1);
25485 int len2 = TREE_VEC_LENGTH (args2);
25486
25487 /* We don't count the pack expansion at the end. */
25488 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
25489 --len1;
25490 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
25491 --len2;
25492
25493 if (len1 > len2)
25494 return 1;
25495 else if (len1 < len2)
25496 return -1;
25497 }
25498
25499 return winner;
25500 }
25501
25502 /* Return the template arguments that will produce the function signature
25503 DECL from the function template FN, with the explicit template
25504 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
25505 also match. Return NULL_TREE if no satisfactory arguments could be
25506 found. */
25507
25508 static tree
25509 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
25510 {
25511 int ntparms = DECL_NTPARMS (fn);
25512 tree targs = make_tree_vec (ntparms);
25513 tree decl_type = TREE_TYPE (decl);
25514 tree decl_arg_types;
25515 tree *args;
25516 unsigned int nargs, ix;
25517 tree arg;
25518
25519 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
25520
25521 /* Never do unification on the 'this' parameter. */
25522 decl_arg_types = skip_artificial_parms_for (decl,
25523 TYPE_ARG_TYPES (decl_type));
25524
25525 nargs = list_length (decl_arg_types);
25526 args = XALLOCAVEC (tree, nargs);
25527 for (arg = decl_arg_types, ix = 0;
25528 arg != NULL_TREE;
25529 arg = TREE_CHAIN (arg), ++ix)
25530 args[ix] = TREE_VALUE (arg);
25531
25532 if (fn_type_unification (fn, explicit_args, targs,
25533 args, ix,
25534 (check_rettype || DECL_CONV_FN_P (fn)
25535 ? TREE_TYPE (decl_type) : NULL_TREE),
25536 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
25537 /*explain_p=*/false,
25538 /*decltype*/false)
25539 == error_mark_node)
25540 return NULL_TREE;
25541
25542 return targs;
25543 }
25544
25545 /* Return the innermost template arguments that, when applied to a partial
25546 specialization SPEC_TMPL of TMPL, yield the ARGS.
25547
25548 For example, suppose we have:
25549
25550 template <class T, class U> struct S {};
25551 template <class T> struct S<T*, int> {};
25552
25553 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
25554 partial specialization and the ARGS will be {double*, int}. The resulting
25555 vector will be {double}, indicating that `T' is bound to `double'. */
25556
25557 static tree
25558 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
25559 {
25560 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
25561 tree spec_args
25562 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
25563 int i, ntparms = TREE_VEC_LENGTH (tparms);
25564 tree deduced_args;
25565 tree innermost_deduced_args;
25566
25567 innermost_deduced_args = make_tree_vec (ntparms);
25568 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25569 {
25570 deduced_args = copy_node (args);
25571 SET_TMPL_ARGS_LEVEL (deduced_args,
25572 TMPL_ARGS_DEPTH (deduced_args),
25573 innermost_deduced_args);
25574 }
25575 else
25576 deduced_args = innermost_deduced_args;
25577
25578 bool tried_array_deduction = (cxx_dialect < cxx17);
25579 again:
25580 if (unify (tparms, deduced_args,
25581 INNERMOST_TEMPLATE_ARGS (spec_args),
25582 INNERMOST_TEMPLATE_ARGS (args),
25583 UNIFY_ALLOW_NONE, /*explain_p=*/false))
25584 return NULL_TREE;
25585
25586 for (i = 0; i < ntparms; ++i)
25587 if (! TREE_VEC_ELT (innermost_deduced_args, i))
25588 {
25589 if (!tried_array_deduction)
25590 {
25591 try_array_deduction (tparms, innermost_deduced_args,
25592 INNERMOST_TEMPLATE_ARGS (spec_args));
25593 tried_array_deduction = true;
25594 if (TREE_VEC_ELT (innermost_deduced_args, i))
25595 goto again;
25596 }
25597 return NULL_TREE;
25598 }
25599
25600 if (!push_tinst_level (spec_tmpl, deduced_args))
25601 {
25602 excessive_deduction_depth = true;
25603 return NULL_TREE;
25604 }
25605
25606 /* Verify that nondeduced template arguments agree with the type
25607 obtained from argument deduction.
25608
25609 For example:
25610
25611 struct A { typedef int X; };
25612 template <class T, class U> struct C {};
25613 template <class T> struct C<T, typename T::X> {};
25614
25615 Then with the instantiation `C<A, int>', we can deduce that
25616 `T' is `A' but unify () does not check whether `typename T::X'
25617 is `int'. */
25618 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
25619
25620 if (spec_args != error_mark_node)
25621 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
25622 INNERMOST_TEMPLATE_ARGS (spec_args),
25623 tmpl, tf_none, false);
25624
25625 pop_tinst_level ();
25626
25627 if (spec_args == error_mark_node
25628 /* We only need to check the innermost arguments; the other
25629 arguments will always agree. */
25630 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
25631 INNERMOST_TEMPLATE_ARGS (args)))
25632 return NULL_TREE;
25633
25634 /* Now that we have bindings for all of the template arguments,
25635 ensure that the arguments deduced for the template template
25636 parameters have compatible template parameter lists. See the use
25637 of template_template_parm_bindings_ok_p in fn_type_unification
25638 for more information. */
25639 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
25640 return NULL_TREE;
25641
25642 return deduced_args;
25643 }
25644
25645 // Compare two function templates T1 and T2 by deducing bindings
25646 // from one against the other. If both deductions succeed, compare
25647 // constraints to see which is more constrained.
25648 static int
25649 more_specialized_inst (tree t1, tree t2)
25650 {
25651 int fate = 0;
25652 int count = 0;
25653
25654 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
25655 {
25656 --fate;
25657 ++count;
25658 }
25659
25660 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
25661 {
25662 ++fate;
25663 ++count;
25664 }
25665
25666 // If both deductions succeed, then one may be more constrained.
25667 if (count == 2 && fate == 0)
25668 fate = more_constrained (t1, t2);
25669
25670 return fate;
25671 }
25672
25673 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
25674 Return the TREE_LIST node with the most specialized template, if
25675 any. If there is no most specialized template, the error_mark_node
25676 is returned.
25677
25678 Note that this function does not look at, or modify, the
25679 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
25680 returned is one of the elements of INSTANTIATIONS, callers may
25681 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
25682 and retrieve it from the value returned. */
25683
25684 tree
25685 most_specialized_instantiation (tree templates)
25686 {
25687 tree fn, champ;
25688
25689 ++processing_template_decl;
25690
25691 champ = templates;
25692 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
25693 {
25694 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
25695 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
25696 if (fate == -1)
25697 champ = fn;
25698 else if (!fate)
25699 {
25700 /* Equally specialized, move to next function. If there
25701 is no next function, nothing's most specialized. */
25702 fn = TREE_CHAIN (fn);
25703 champ = fn;
25704 if (!fn)
25705 break;
25706 }
25707 }
25708
25709 if (champ)
25710 /* Now verify that champ is better than everything earlier in the
25711 instantiation list. */
25712 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
25713 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
25714 {
25715 champ = NULL_TREE;
25716 break;
25717 }
25718 }
25719
25720 processing_template_decl--;
25721
25722 if (!champ)
25723 return error_mark_node;
25724
25725 return champ;
25726 }
25727
25728 /* If DECL is a specialization of some template, return the most
25729 general such template. Otherwise, returns NULL_TREE.
25730
25731 For example, given:
25732
25733 template <class T> struct S { template <class U> void f(U); };
25734
25735 if TMPL is `template <class U> void S<int>::f(U)' this will return
25736 the full template. This function will not trace past partial
25737 specializations, however. For example, given in addition:
25738
25739 template <class T> struct S<T*> { template <class U> void f(U); };
25740
25741 if TMPL is `template <class U> void S<int*>::f(U)' this will return
25742 `template <class T> template <class U> S<T*>::f(U)'. */
25743
25744 tree
25745 most_general_template (tree decl)
25746 {
25747 if (TREE_CODE (decl) != TEMPLATE_DECL)
25748 {
25749 if (tree tinfo = get_template_info (decl))
25750 decl = TI_TEMPLATE (tinfo);
25751 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
25752 template friend, or a FIELD_DECL for a capture pack. */
25753 if (TREE_CODE (decl) != TEMPLATE_DECL)
25754 return NULL_TREE;
25755 }
25756
25757 /* Look for more and more general templates. */
25758 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
25759 {
25760 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
25761 (See cp-tree.h for details.) */
25762 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
25763 break;
25764
25765 if (CLASS_TYPE_P (TREE_TYPE (decl))
25766 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
25767 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
25768 break;
25769
25770 /* Stop if we run into an explicitly specialized class template. */
25771 if (!DECL_NAMESPACE_SCOPE_P (decl)
25772 && DECL_CONTEXT (decl)
25773 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
25774 break;
25775
25776 decl = DECL_TI_TEMPLATE (decl);
25777 }
25778
25779 return decl;
25780 }
25781
25782 /* Return the most specialized of the template partial specializations
25783 which can produce TARGET, a specialization of some class or variable
25784 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
25785 a TEMPLATE_DECL node corresponding to the partial specialization, while
25786 the TREE_PURPOSE is the set of template arguments that must be
25787 substituted into the template pattern in order to generate TARGET.
25788
25789 If the choice of partial specialization is ambiguous, a diagnostic
25790 is issued, and the error_mark_node is returned. If there are no
25791 partial specializations matching TARGET, then NULL_TREE is
25792 returned, indicating that the primary template should be used. */
25793
25794 tree
25795 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
25796 {
25797 tree list = NULL_TREE;
25798 tree t;
25799 tree champ;
25800 int fate;
25801 bool ambiguous_p;
25802 tree outer_args = NULL_TREE;
25803 tree tmpl, args;
25804
25805 tree decl;
25806 if (TYPE_P (target))
25807 {
25808 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
25809 tmpl = TI_TEMPLATE (tinfo);
25810 args = TI_ARGS (tinfo);
25811 decl = TYPE_NAME (target);
25812 }
25813 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
25814 {
25815 tmpl = TREE_OPERAND (target, 0);
25816 args = TREE_OPERAND (target, 1);
25817 decl = DECL_TEMPLATE_RESULT (tmpl);
25818 }
25819 else if (VAR_P (target))
25820 {
25821 tree tinfo = DECL_TEMPLATE_INFO (target);
25822 tmpl = TI_TEMPLATE (tinfo);
25823 args = TI_ARGS (tinfo);
25824 decl = target;
25825 }
25826 else
25827 gcc_unreachable ();
25828
25829 push_access_scope_guard pas (decl);
25830 deferring_access_check_sentinel acs (dk_no_deferred);
25831
25832 tree main_tmpl = most_general_template (tmpl);
25833
25834 /* For determining which partial specialization to use, only the
25835 innermost args are interesting. */
25836 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25837 {
25838 outer_args = strip_innermost_template_args (args, 1);
25839 args = INNERMOST_TEMPLATE_ARGS (args);
25840 }
25841
25842 /* The caller hasn't called push_to_top_level yet, but we need
25843 get_partial_spec_bindings to be done in non-template context so that we'll
25844 fully resolve everything. */
25845 processing_template_decl_sentinel ptds;
25846
25847 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
25848 {
25849 const tree ospec_tmpl = TREE_VALUE (t);
25850
25851 tree spec_tmpl;
25852 if (outer_args)
25853 {
25854 /* Substitute in the template args from the enclosing class. */
25855 ++processing_template_decl;
25856 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
25857 --processing_template_decl;
25858 if (spec_tmpl == error_mark_node)
25859 return error_mark_node;
25860 }
25861 else
25862 spec_tmpl = ospec_tmpl;
25863
25864 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
25865 if (spec_args)
25866 {
25867 if (outer_args)
25868 spec_args = add_to_template_args (outer_args, spec_args);
25869
25870 /* Keep the candidate only if the constraints are satisfied,
25871 or if we're not compiling with concepts. */
25872 if (!flag_concepts
25873 || constraints_satisfied_p (ospec_tmpl, spec_args))
25874 {
25875 list = tree_cons (spec_args, ospec_tmpl, list);
25876 TREE_TYPE (list) = TREE_TYPE (t);
25877 }
25878 }
25879 }
25880
25881 if (! list)
25882 return NULL_TREE;
25883
25884 ambiguous_p = false;
25885 t = list;
25886 champ = t;
25887 t = TREE_CHAIN (t);
25888 for (; t; t = TREE_CHAIN (t))
25889 {
25890 fate = more_specialized_partial_spec (tmpl, champ, t);
25891 if (fate == 1)
25892 ;
25893 else
25894 {
25895 if (fate == 0)
25896 {
25897 t = TREE_CHAIN (t);
25898 if (! t)
25899 {
25900 ambiguous_p = true;
25901 break;
25902 }
25903 }
25904 champ = t;
25905 }
25906 }
25907
25908 if (!ambiguous_p)
25909 for (t = list; t && t != champ; t = TREE_CHAIN (t))
25910 {
25911 fate = more_specialized_partial_spec (tmpl, champ, t);
25912 if (fate != 1)
25913 {
25914 ambiguous_p = true;
25915 break;
25916 }
25917 }
25918
25919 if (ambiguous_p)
25920 {
25921 const char *str;
25922 char *spaces = NULL;
25923 if (!(complain & tf_error))
25924 return error_mark_node;
25925 if (TYPE_P (target))
25926 error ("ambiguous template instantiation for %q#T", target);
25927 else
25928 error ("ambiguous template instantiation for %q#D", target);
25929 str = ngettext ("candidate is:", "candidates are:", list_length (list));
25930 for (t = list; t; t = TREE_CHAIN (t))
25931 {
25932 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
25933 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
25934 "%s %#qS", spaces ? spaces : str, subst);
25935 spaces = spaces ? spaces : get_spaces (str);
25936 }
25937 free (spaces);
25938 return error_mark_node;
25939 }
25940
25941 return champ;
25942 }
25943
25944 /* Explicitly instantiate DECL. */
25945
25946 void
25947 do_decl_instantiation (tree decl, tree storage)
25948 {
25949 tree result = NULL_TREE;
25950 int extern_p = 0;
25951
25952 if (!decl || decl == error_mark_node)
25953 /* An error occurred, for which grokdeclarator has already issued
25954 an appropriate message. */
25955 return;
25956 else if (! DECL_LANG_SPECIFIC (decl))
25957 {
25958 error ("explicit instantiation of non-template %q#D", decl);
25959 return;
25960 }
25961 else if (DECL_DECLARED_CONCEPT_P (decl))
25962 {
25963 if (VAR_P (decl))
25964 error ("explicit instantiation of variable concept %q#D", decl);
25965 else
25966 error ("explicit instantiation of function concept %q#D", decl);
25967 return;
25968 }
25969
25970 bool var_templ = (DECL_TEMPLATE_INFO (decl)
25971 && variable_template_p (DECL_TI_TEMPLATE (decl)));
25972
25973 if (VAR_P (decl) && !var_templ)
25974 {
25975 /* There is an asymmetry here in the way VAR_DECLs and
25976 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25977 the latter, the DECL we get back will be marked as a
25978 template instantiation, and the appropriate
25979 DECL_TEMPLATE_INFO will be set up. This does not happen for
25980 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25981 should handle VAR_DECLs as it currently handles
25982 FUNCTION_DECLs. */
25983 if (!DECL_CLASS_SCOPE_P (decl))
25984 {
25985 error ("%qD is not a static data member of a class template", decl);
25986 return;
25987 }
25988 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25989 if (!result || !VAR_P (result))
25990 {
25991 error ("no matching template for %qD found", decl);
25992 return;
25993 }
25994 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25995 {
25996 error ("type %qT for explicit instantiation %qD does not match "
25997 "declared type %qT", TREE_TYPE (result), decl,
25998 TREE_TYPE (decl));
25999 return;
26000 }
26001 }
26002 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
26003 {
26004 error ("explicit instantiation of %q#D", decl);
26005 return;
26006 }
26007 else
26008 result = decl;
26009
26010 /* Check for various error cases. Note that if the explicit
26011 instantiation is valid the RESULT will currently be marked as an
26012 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
26013 until we get here. */
26014
26015 if (DECL_TEMPLATE_SPECIALIZATION (result))
26016 {
26017 /* DR 259 [temp.spec].
26018
26019 Both an explicit instantiation and a declaration of an explicit
26020 specialization shall not appear in a program unless the explicit
26021 instantiation follows a declaration of the explicit specialization.
26022
26023 For a given set of template parameters, if an explicit
26024 instantiation of a template appears after a declaration of an
26025 explicit specialization for that template, the explicit
26026 instantiation has no effect. */
26027 return;
26028 }
26029 else if (DECL_EXPLICIT_INSTANTIATION (result))
26030 {
26031 /* [temp.spec]
26032
26033 No program shall explicitly instantiate any template more
26034 than once.
26035
26036 We check DECL_NOT_REALLY_EXTERN so as not to complain when
26037 the first instantiation was `extern' and the second is not,
26038 and EXTERN_P for the opposite case. */
26039 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
26040 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
26041 /* If an "extern" explicit instantiation follows an ordinary
26042 explicit instantiation, the template is instantiated. */
26043 if (extern_p)
26044 return;
26045 }
26046 else if (!DECL_IMPLICIT_INSTANTIATION (result))
26047 {
26048 error ("no matching template for %qD found", result);
26049 return;
26050 }
26051 else if (!DECL_TEMPLATE_INFO (result))
26052 {
26053 permerror (input_location, "explicit instantiation of non-template %q#D", result);
26054 return;
26055 }
26056
26057 if (storage == NULL_TREE)
26058 ;
26059 else if (storage == ridpointers[(int) RID_EXTERN])
26060 {
26061 if (cxx_dialect == cxx98)
26062 pedwarn (input_location, OPT_Wpedantic,
26063 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
26064 "instantiations");
26065 extern_p = 1;
26066 }
26067 else
26068 error ("storage class %qD applied to template instantiation", storage);
26069
26070 check_explicit_instantiation_namespace (result);
26071 mark_decl_instantiated (result, extern_p);
26072 if (! extern_p)
26073 instantiate_decl (result, /*defer_ok=*/true,
26074 /*expl_inst_class_mem_p=*/false);
26075 }
26076
26077 static void
26078 mark_class_instantiated (tree t, int extern_p)
26079 {
26080 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
26081 SET_CLASSTYPE_INTERFACE_KNOWN (t);
26082 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
26083 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
26084 if (! extern_p)
26085 {
26086 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
26087 rest_of_type_compilation (t, 1);
26088 }
26089 }
26090
26091 /* Perform an explicit instantiation of template class T. STORAGE, if
26092 non-null, is the RID for extern, inline or static. COMPLAIN is
26093 nonzero if this is called from the parser, zero if called recursively,
26094 since the standard is unclear (as detailed below). */
26095
26096 void
26097 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
26098 {
26099 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
26100 {
26101 if (tree ti = TYPE_TEMPLATE_INFO (t))
26102 error ("explicit instantiation of non-class template %qD",
26103 TI_TEMPLATE (ti));
26104 else
26105 error ("explicit instantiation of non-template type %qT", t);
26106 return;
26107 }
26108
26109 complete_type (t);
26110
26111 if (!COMPLETE_TYPE_P (t))
26112 {
26113 if (complain & tf_error)
26114 error ("explicit instantiation of %q#T before definition of template",
26115 t);
26116 return;
26117 }
26118
26119 /* At most one of these will be true. */
26120 bool extern_p = false;
26121 bool nomem_p = false;
26122 bool static_p = false;
26123
26124 if (storage != NULL_TREE)
26125 {
26126 if (storage == ridpointers[(int) RID_EXTERN])
26127 {
26128 if (cxx_dialect == cxx98)
26129 pedwarn (input_location, OPT_Wpedantic,
26130 "ISO C++ 1998 forbids the use of %<extern%> on "
26131 "explicit instantiations");
26132 }
26133 else
26134 pedwarn (input_location, OPT_Wpedantic,
26135 "ISO C++ forbids the use of %qE"
26136 " on explicit instantiations", storage);
26137
26138 if (storage == ridpointers[(int) RID_INLINE])
26139 nomem_p = true;
26140 else if (storage == ridpointers[(int) RID_EXTERN])
26141 extern_p = true;
26142 else if (storage == ridpointers[(int) RID_STATIC])
26143 static_p = true;
26144 else
26145 error ("storage class %qD applied to template instantiation",
26146 storage);
26147 }
26148
26149 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
26150 /* DR 259 [temp.spec].
26151
26152 Both an explicit instantiation and a declaration of an explicit
26153 specialization shall not appear in a program unless the
26154 explicit instantiation follows a declaration of the explicit
26155 specialization.
26156
26157 For a given set of template parameters, if an explicit
26158 instantiation of a template appears after a declaration of an
26159 explicit specialization for that template, the explicit
26160 instantiation has no effect. */
26161 return;
26162
26163 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
26164 {
26165 /* We've already instantiated the template. */
26166
26167 /* [temp.spec]
26168
26169 No program shall explicitly instantiate any template more
26170 than once.
26171
26172 If EXTERN_P then this is ok. */
26173 if (!extern_p && (complain & tf_error))
26174 permerror (input_location,
26175 "duplicate explicit instantiation of %q#T", t);
26176
26177 return;
26178 }
26179
26180 check_explicit_instantiation_namespace (TYPE_NAME (t));
26181 mark_class_instantiated (t, extern_p);
26182
26183 if (nomem_p)
26184 return;
26185
26186 /* In contrast to implicit instantiation, where only the
26187 declarations, and not the definitions, of members are
26188 instantiated, we have here:
26189
26190 [temp.explicit]
26191
26192 An explicit instantiation that names a class template
26193 specialization is also an explicit instantiation of the same
26194 kind (declaration or definition) of each of its members (not
26195 including members inherited from base classes and members
26196 that are templates) that has not been previously explicitly
26197 specialized in the translation unit containing the explicit
26198 instantiation, provided that the associated constraints, if
26199 any, of that member are satisfied by the template arguments
26200 of the explicit instantiation. */
26201 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
26202 if ((VAR_P (fld)
26203 || (TREE_CODE (fld) == FUNCTION_DECL
26204 && !static_p
26205 && user_provided_p (fld)))
26206 && DECL_TEMPLATE_INSTANTIATION (fld)
26207 && constraints_satisfied_p (fld))
26208 {
26209 mark_decl_instantiated (fld, extern_p);
26210 if (! extern_p)
26211 instantiate_decl (fld, /*defer_ok=*/true,
26212 /*expl_inst_class_mem_p=*/true);
26213 }
26214 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
26215 {
26216 tree type = TREE_TYPE (fld);
26217
26218 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26219 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
26220 do_type_instantiation (type, storage, 0);
26221 }
26222 }
26223
26224 /* Given a function DECL, which is a specialization of TMPL, modify
26225 DECL to be a re-instantiation of TMPL with the same template
26226 arguments. TMPL should be the template into which tsubst'ing
26227 should occur for DECL, not the most general template.
26228
26229 One reason for doing this is a scenario like this:
26230
26231 template <class T>
26232 void f(const T&, int i);
26233
26234 void g() { f(3, 7); }
26235
26236 template <class T>
26237 void f(const T& t, const int i) { }
26238
26239 Note that when the template is first instantiated, with
26240 instantiate_template, the resulting DECL will have no name for the
26241 first parameter, and the wrong type for the second. So, when we go
26242 to instantiate the DECL, we regenerate it. */
26243
26244 static void
26245 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
26246 {
26247 /* The arguments used to instantiate DECL, from the most general
26248 template. */
26249 tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
26250
26251 /* Make sure that we can see identifiers, and compute access correctly. */
26252 push_access_scope (decl);
26253
26254 if (TREE_CODE (decl) == FUNCTION_DECL)
26255 {
26256 tree specs;
26257 int args_depth;
26258 int parms_depth;
26259
26260 /* Use the source location of the definition. */
26261 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
26262
26263 args_depth = TMPL_ARGS_DEPTH (args);
26264 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
26265 if (args_depth > parms_depth)
26266 args = get_innermost_template_args (args, parms_depth);
26267
26268 /* Instantiate a dynamic exception-specification. noexcept will be
26269 handled below. */
26270 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
26271 if (TREE_VALUE (raises))
26272 {
26273 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
26274 args, tf_error, NULL_TREE,
26275 /*defer_ok*/false);
26276 if (specs && specs != error_mark_node)
26277 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
26278 specs);
26279 }
26280
26281 /* Merge parameter declarations. */
26282 if (tree pattern_parm
26283 = skip_artificial_parms_for (code_pattern,
26284 DECL_ARGUMENTS (code_pattern)))
26285 {
26286 tree *p = &DECL_ARGUMENTS (decl);
26287 for (int skip = num_artificial_parms_for (decl); skip; --skip)
26288 p = &DECL_CHAIN (*p);
26289 *p = tsubst_decl (pattern_parm, args, tf_error);
26290 for (tree t = *p; t; t = DECL_CHAIN (t))
26291 DECL_CONTEXT (t) = decl;
26292 }
26293
26294 if (DECL_CONTRACTS (decl))
26295 {
26296 /* If we're regenerating a specialization, the contracts will have
26297 been copied from the most general template. Replace those with
26298 the ones from the actual specialization. */
26299 tree tmpl = DECL_TI_TEMPLATE (decl);
26300 if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
26301 {
26302 remove_contract_attributes (decl);
26303 copy_contract_attributes (decl, code_pattern);
26304 }
26305
26306 tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
26307 }
26308
26309 /* Merge additional specifiers from the CODE_PATTERN. */
26310 if (DECL_DECLARED_INLINE_P (code_pattern)
26311 && !DECL_DECLARED_INLINE_P (decl))
26312 DECL_DECLARED_INLINE_P (decl) = 1;
26313
26314 maybe_instantiate_noexcept (decl, tf_error);
26315 }
26316 else if (VAR_P (decl))
26317 {
26318 start_lambda_scope (decl);
26319 DECL_INITIAL (decl) =
26320 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
26321 tf_error, DECL_TI_TEMPLATE (decl));
26322 finish_lambda_scope ();
26323 if (VAR_HAD_UNKNOWN_BOUND (decl))
26324 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
26325 tf_error, DECL_TI_TEMPLATE (decl));
26326 }
26327 else
26328 gcc_unreachable ();
26329
26330 pop_access_scope (decl);
26331 }
26332
26333 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
26334 substituted to get DECL. */
26335
26336 tree
26337 template_for_substitution (tree decl)
26338 {
26339 tree tmpl = DECL_TI_TEMPLATE (decl);
26340
26341 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
26342 for the instantiation. This is not always the most general
26343 template. Consider, for example:
26344
26345 template <class T>
26346 struct S { template <class U> void f();
26347 template <> void f<int>(); };
26348
26349 and an instantiation of S<double>::f<int>. We want TD to be the
26350 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
26351 while (/* An instantiation cannot have a definition, so we need a
26352 more general template. */
26353 DECL_TEMPLATE_INSTANTIATION (tmpl)
26354 /* We must also deal with friend templates. Given:
26355
26356 template <class T> struct S {
26357 template <class U> friend void f() {};
26358 };
26359
26360 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
26361 so far as the language is concerned, but that's still
26362 where we get the pattern for the instantiation from. On
26363 other hand, if the definition comes outside the class, say:
26364
26365 template <class T> struct S {
26366 template <class U> friend void f();
26367 };
26368 template <class U> friend void f() {}
26369
26370 we don't need to look any further. That's what the check for
26371 DECL_INITIAL is for. */
26372 || (TREE_CODE (decl) == FUNCTION_DECL
26373 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
26374 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
26375 {
26376 /* The present template, TD, should not be a definition. If it
26377 were a definition, we should be using it! Note that we
26378 cannot restructure the loop to just keep going until we find
26379 a template with a definition, since that might go too far if
26380 a specialization was declared, but not defined. */
26381
26382 /* Fetch the more general template. */
26383 tmpl = DECL_TI_TEMPLATE (tmpl);
26384 }
26385
26386 return tmpl;
26387 }
26388
26389 /* Returns true if we need to instantiate this template instance even if we
26390 know we aren't going to emit it. */
26391
26392 bool
26393 always_instantiate_p (tree decl)
26394 {
26395 /* We always instantiate inline functions so that we can inline them. An
26396 explicit instantiation declaration prohibits implicit instantiation of
26397 non-inline functions. With high levels of optimization, we would
26398 normally inline non-inline functions -- but we're not allowed to do
26399 that for "extern template" functions. Therefore, we check
26400 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
26401 return ((TREE_CODE (decl) == FUNCTION_DECL
26402 && (DECL_DECLARED_INLINE_P (decl)
26403 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
26404 /* And we need to instantiate static data members so that
26405 their initializers are available in integral constant
26406 expressions. */
26407 || (VAR_P (decl)
26408 && decl_maybe_constant_var_p (decl)));
26409 }
26410
26411 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
26412 instantiate it now, modifying TREE_TYPE (fn). Returns false on
26413 error, true otherwise. */
26414
26415 bool
26416 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
26417 {
26418 if (fn == error_mark_node)
26419 return false;
26420
26421 /* Don't instantiate a noexcept-specification from template context. */
26422 if (processing_template_decl
26423 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
26424 return true;
26425
26426 tree fntype = TREE_TYPE (fn);
26427 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
26428
26429 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
26430 && DECL_MAYBE_DELETED (fn))
26431 {
26432 if (fn == current_function_decl)
26433 /* We're in start_preparsed_function, keep going. */
26434 return true;
26435
26436 ++function_depth;
26437 maybe_synthesize_method (fn);
26438 --function_depth;
26439 return !DECL_DELETED_FN (fn);
26440 }
26441
26442 if (!spec || !TREE_PURPOSE (spec))
26443 return true;
26444
26445 tree noex = TREE_PURPOSE (spec);
26446 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26447 && TREE_CODE (noex) != DEFERRED_PARSE)
26448 return true;
26449
26450 tree orig_fn = NULL_TREE;
26451 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
26452 its FUNCTION_DECL for the rest of this function -- push_access_scope
26453 doesn't accept TEMPLATE_DECLs. */
26454 if (DECL_FUNCTION_TEMPLATE_P (fn))
26455 {
26456 orig_fn = fn;
26457 fn = DECL_TEMPLATE_RESULT (fn);
26458 }
26459
26460 if (DECL_CLONED_FUNCTION_P (fn))
26461 {
26462 tree prime = DECL_CLONED_FUNCTION (fn);
26463 if (!maybe_instantiate_noexcept (prime, complain))
26464 return false;
26465 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
26466 }
26467 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
26468 {
26469 static hash_set<tree>* fns = new hash_set<tree>;
26470 bool added = false;
26471 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
26472 {
26473 spec = get_defaulted_eh_spec (fn, complain);
26474 if (spec == error_mark_node)
26475 /* This might have failed because of an unparsed DMI, so
26476 let's try again later. */
26477 return false;
26478 }
26479 else if (!(added = !fns->add (fn)))
26480 {
26481 /* If hash_set::add returns true, the element was already there. */
26482 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
26483 DECL_SOURCE_LOCATION (fn));
26484 error_at (loc,
26485 "exception specification of %qD depends on itself",
26486 fn);
26487 spec = noexcept_false_spec;
26488 }
26489 else if (push_tinst_level (fn))
26490 {
26491 push_to_top_level ();
26492 push_access_scope (fn);
26493 push_deferring_access_checks (dk_no_deferred);
26494 input_location = DECL_SOURCE_LOCATION (fn);
26495
26496 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26497 && !DECL_LOCAL_DECL_P (fn))
26498 {
26499 /* If needed, set current_class_ptr for the benefit of
26500 tsubst_copy/PARM_DECL. */
26501 tree this_parm = DECL_ARGUMENTS (fn);
26502 current_class_ptr = NULL_TREE;
26503 current_class_ref = cp_build_fold_indirect_ref (this_parm);
26504 current_class_ptr = this_parm;
26505 }
26506
26507 /* If this function is represented by a TEMPLATE_DECL, then
26508 the deferred noexcept-specification might still contain
26509 dependent types, even after substitution. And we need the
26510 dependency check functions to work in build_noexcept_spec. */
26511 if (orig_fn)
26512 ++processing_template_decl;
26513
26514 /* Do deferred instantiation of the noexcept-specifier. */
26515 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
26516 DEFERRED_NOEXCEPT_ARGS (noex),
26517 tf_warning_or_error, fn);
26518
26519 /* Build up the noexcept-specification. */
26520 spec = build_noexcept_spec (noex, tf_warning_or_error);
26521
26522 if (orig_fn)
26523 --processing_template_decl;
26524
26525 pop_deferring_access_checks ();
26526 pop_access_scope (fn);
26527 pop_tinst_level ();
26528 pop_from_top_level ();
26529 }
26530 else
26531 spec = noexcept_false_spec;
26532
26533 if (added)
26534 fns->remove (fn);
26535 }
26536
26537 if (spec == error_mark_node)
26538 {
26539 /* This failed with a hard error, so let's go with false. */
26540 gcc_assert (seen_error ());
26541 spec = noexcept_false_spec;
26542 }
26543
26544 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
26545 if (orig_fn)
26546 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
26547
26548 return true;
26549 }
26550
26551 /* We're starting to process the function INST, an instantiation of PATTERN;
26552 add their parameters to local_specializations. */
26553
26554 void
26555 register_parameter_specializations (tree pattern, tree inst)
26556 {
26557 tree tmpl_parm = DECL_ARGUMENTS (pattern);
26558 tree spec_parm = DECL_ARGUMENTS (inst);
26559 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
26560 {
26561 register_local_specialization (spec_parm, tmpl_parm);
26562 spec_parm = skip_artificial_parms_for (inst, spec_parm);
26563 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
26564 }
26565 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
26566 {
26567 if (!DECL_PACK_P (tmpl_parm))
26568 {
26569 register_local_specialization (spec_parm, tmpl_parm);
26570 spec_parm = DECL_CHAIN (spec_parm);
26571 }
26572 else
26573 {
26574 /* Register the (value) argument pack as a specialization of
26575 TMPL_PARM, then move on. */
26576 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
26577 register_local_specialization (argpack, tmpl_parm);
26578 }
26579 }
26580 gcc_assert (!spec_parm);
26581 }
26582
26583 /* Instantiate the body of D using PATTERN with ARGS. We have
26584 already determined PATTERN is the correct template to use.
26585 NESTED_P is true if this is a nested function, in which case
26586 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
26587
26588 static void
26589 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
26590 {
26591 tree td = NULL_TREE;
26592 tree code_pattern = pattern;
26593
26594 if (!nested_p)
26595 {
26596 td = pattern;
26597 code_pattern = DECL_TEMPLATE_RESULT (td);
26598 }
26599 else
26600 /* Only OMP reductions are nested. */
26601 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
26602
26603 vec<tree> omp_privatization_save;
26604 if (current_function_decl)
26605 save_omp_privatization_clauses (omp_privatization_save);
26606
26607 bool push_to_top
26608 = !(current_function_decl
26609 && !LAMBDA_FUNCTION_P (d)
26610 && decl_function_context (d) == current_function_decl);
26611
26612 if (push_to_top)
26613 push_to_top_level ();
26614 else
26615 {
26616 gcc_assert (!processing_template_decl);
26617 push_function_context ();
26618 cp_unevaluated_operand = 0;
26619 c_inhibit_evaluation_warnings = 0;
26620 }
26621
26622 if (VAR_P (d))
26623 {
26624 /* The variable might be a lambda's extra scope, and that
26625 lambda's visibility depends on D's. */
26626 maybe_commonize_var (d);
26627 determine_visibility (d);
26628 }
26629
26630 /* Mark D as instantiated so that recursive calls to
26631 instantiate_decl do not try to instantiate it again. */
26632 DECL_TEMPLATE_INSTANTIATED (d) = 1;
26633
26634 if (td)
26635 /* Regenerate the declaration in case the template has been modified
26636 by a subsequent redeclaration. */
26637 regenerate_decl_from_template (d, td, args);
26638
26639 /* We already set the file and line above. Reset them now in case
26640 they changed as a result of calling regenerate_decl_from_template. */
26641 input_location = DECL_SOURCE_LOCATION (d);
26642
26643 if (VAR_P (d))
26644 {
26645 /* Clear out DECL_RTL; whatever was there before may not be right
26646 since we've reset the type of the declaration. */
26647 SET_DECL_RTL (d, NULL);
26648 DECL_IN_AGGR_P (d) = 0;
26649
26650 /* The initializer is placed in DECL_INITIAL by
26651 regenerate_decl_from_template so we don't need to
26652 push/pop_access_scope again here. Pull it out so that
26653 cp_finish_decl can process it. */
26654 bool const_init = false;
26655 tree init = DECL_INITIAL (d);
26656 DECL_INITIAL (d) = NULL_TREE;
26657 DECL_INITIALIZED_P (d) = 0;
26658
26659 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
26660 initializer. That function will defer actual emission until
26661 we have a chance to determine linkage. */
26662 DECL_EXTERNAL (d) = 0;
26663
26664 /* Enter the scope of D so that access-checking works correctly. */
26665 bool enter_context = DECL_CLASS_SCOPE_P (d);
26666 if (enter_context)
26667 push_nested_class (DECL_CONTEXT (d));
26668
26669 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26670 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
26671
26672 if (enter_context)
26673 pop_nested_class ();
26674 }
26675 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
26676 synthesize_method (d);
26677 else if (TREE_CODE (d) == FUNCTION_DECL)
26678 {
26679 /* Set up the list of local specializations. */
26680 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
26681 tree block = NULL_TREE;
26682
26683 /* Set up context. */
26684 if (nested_p)
26685 block = push_stmt_list ();
26686 else
26687 {
26688 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
26689
26690 perform_instantiation_time_access_checks (code_pattern, args);
26691 }
26692
26693 /* Create substitution entries for the parameters. */
26694 register_parameter_specializations (code_pattern, d);
26695
26696 /* Substitute into the body of the function. */
26697 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
26698 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
26699 tf_warning_or_error, d);
26700 else
26701 {
26702 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
26703 tf_warning_or_error, DECL_TI_TEMPLATE (d));
26704
26705 /* Set the current input_location to the end of the function
26706 so that finish_function knows where we are. */
26707 input_location
26708 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
26709
26710 /* Remember if we saw an infinite loop in the template. */
26711 current_function_infinite_loop
26712 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
26713 }
26714
26715 /* Finish the function. */
26716 if (nested_p)
26717 DECL_SAVED_TREE (d) = pop_stmt_list (block);
26718 else
26719 {
26720 d = finish_function (/*inline_p=*/false);
26721 expand_or_defer_fn (d);
26722 }
26723
26724 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
26725 cp_check_omp_declare_reduction (d);
26726 }
26727
26728 /* We're not deferring instantiation any more. */
26729 if (!nested_p)
26730 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
26731
26732 if (push_to_top)
26733 pop_from_top_level ();
26734 else
26735 pop_function_context ();
26736
26737 if (current_function_decl)
26738 restore_omp_privatization_clauses (omp_privatization_save);
26739 }
26740
26741 /* Produce the definition of D, a _DECL generated from a template. If
26742 DEFER_OK is true, then we don't have to actually do the
26743 instantiation now; we just have to do it sometime. Normally it is
26744 an error if this is an explicit instantiation but D is undefined.
26745 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
26746 instantiated class template. */
26747
26748 tree
26749 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
26750 {
26751 tree tmpl = DECL_TI_TEMPLATE (d);
26752 tree gen_args;
26753 tree args;
26754 tree td;
26755 tree code_pattern;
26756 tree spec;
26757 tree gen_tmpl;
26758 bool pattern_defined;
26759 location_t saved_loc = input_location;
26760 int saved_unevaluated_operand = cp_unevaluated_operand;
26761 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26762 bool external_p;
26763 bool deleted_p;
26764
26765 /* This function should only be used to instantiate templates for
26766 functions and static member variables. */
26767 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
26768
26769 /* A concept is never instantiated. */
26770 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
26771
26772 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
26773
26774 if (modules_p ())
26775 /* We may have a pending instantiation of D itself. */
26776 lazy_load_pendings (d);
26777
26778 /* Variables are never deferred; if instantiation is required, they
26779 are instantiated right away. That allows for better code in the
26780 case that an expression refers to the value of the variable --
26781 if the variable has a constant value the referring expression can
26782 take advantage of that fact. */
26783 if (VAR_P (d))
26784 defer_ok = false;
26785
26786 /* Don't instantiate cloned functions. Instead, instantiate the
26787 functions they cloned. */
26788 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
26789 d = DECL_CLONED_FUNCTION (d);
26790
26791 if (DECL_TEMPLATE_INSTANTIATED (d)
26792 || TREE_TYPE (d) == error_mark_node
26793 || (TREE_CODE (d) == FUNCTION_DECL
26794 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
26795 || DECL_TEMPLATE_SPECIALIZATION (d))
26796 /* D has already been instantiated or explicitly specialized, so
26797 there's nothing for us to do here.
26798
26799 It might seem reasonable to check whether or not D is an explicit
26800 instantiation, and, if so, stop here. But when an explicit
26801 instantiation is deferred until the end of the compilation,
26802 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
26803 the instantiation. */
26804 return d;
26805
26806 /* Check to see whether we know that this template will be
26807 instantiated in some other file, as with "extern template"
26808 extension. */
26809 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
26810
26811 /* In general, we do not instantiate such templates. */
26812 if (external_p && !always_instantiate_p (d))
26813 return d;
26814
26815 gen_tmpl = most_general_template (tmpl);
26816 gen_args = DECL_TI_ARGS (d);
26817
26818 /* We should already have the extra args. */
26819 gcc_checking_assert (tmpl == gen_tmpl
26820 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
26821 == TMPL_ARGS_DEPTH (gen_args)));
26822 /* And what's in the hash table should match D. */
26823 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
26824 == d
26825 || spec == NULL_TREE);
26826
26827 /* This needs to happen before any tsubsting. */
26828 if (! push_tinst_level (d))
26829 return d;
26830
26831 auto_timevar tv (TV_TEMPLATE_INST);
26832
26833 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
26834 for the instantiation. */
26835 td = template_for_substitution (d);
26836 args = gen_args;
26837
26838 if (variable_template_specialization_p (d))
26839 {
26840 /* Look up an explicit specialization, if any. */
26841 tree elt = most_specialized_partial_spec (d, tf_warning_or_error);
26842 if (elt && elt != error_mark_node)
26843 {
26844 td = TREE_VALUE (elt);
26845 args = TREE_PURPOSE (elt);
26846 }
26847 }
26848
26849 code_pattern = DECL_TEMPLATE_RESULT (td);
26850
26851 /* We should never be trying to instantiate a member of a class
26852 template or partial specialization. */
26853 gcc_assert (d != code_pattern);
26854
26855 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
26856 || DECL_TEMPLATE_SPECIALIZATION (td))
26857 /* In the case of a friend template whose definition is provided
26858 outside the class, we may have too many arguments. Drop the
26859 ones we don't need. The same is true for specializations. */
26860 args = get_innermost_template_args
26861 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
26862
26863 if (TREE_CODE (d) == FUNCTION_DECL)
26864 {
26865 deleted_p = DECL_DELETED_FN (code_pattern);
26866 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
26867 && DECL_INITIAL (code_pattern) != error_mark_node)
26868 || DECL_DEFAULTED_FN (code_pattern)
26869 || deleted_p);
26870 }
26871 else
26872 {
26873 deleted_p = false;
26874 if (DECL_CLASS_SCOPE_P (code_pattern))
26875 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
26876 else
26877 pattern_defined = ! DECL_EXTERNAL (code_pattern);
26878 }
26879
26880 /* We may be in the middle of deferred access check. Disable it now. */
26881 push_deferring_access_checks (dk_no_deferred);
26882
26883 /* Unless an explicit instantiation directive has already determined
26884 the linkage of D, remember that a definition is available for
26885 this entity. */
26886 if (pattern_defined
26887 && !DECL_INTERFACE_KNOWN (d)
26888 && !DECL_NOT_REALLY_EXTERN (d))
26889 mark_definable (d);
26890
26891 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
26892 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
26893 input_location = DECL_SOURCE_LOCATION (d);
26894
26895 /* If D is a member of an explicitly instantiated class template,
26896 and no definition is available, treat it like an implicit
26897 instantiation. */
26898 if (!pattern_defined && expl_inst_class_mem_p
26899 && DECL_EXPLICIT_INSTANTIATION (d))
26900 {
26901 /* Leave linkage flags alone on instantiations with anonymous
26902 visibility. */
26903 if (TREE_PUBLIC (d))
26904 {
26905 DECL_NOT_REALLY_EXTERN (d) = 0;
26906 DECL_INTERFACE_KNOWN (d) = 0;
26907 }
26908 SET_DECL_IMPLICIT_INSTANTIATION (d);
26909 }
26910
26911 /* Defer all other templates, unless we have been explicitly
26912 forbidden from doing so. */
26913 if (/* If there is no definition, we cannot instantiate the
26914 template. */
26915 ! pattern_defined
26916 /* If it's OK to postpone instantiation, do so. */
26917 || defer_ok
26918 /* If this is a static data member that will be defined
26919 elsewhere, we don't want to instantiate the entire data
26920 member, but we do want to instantiate the initializer so that
26921 we can substitute that elsewhere. */
26922 || (external_p && VAR_P (d))
26923 /* Handle here a deleted function too, avoid generating
26924 its body (c++/61080). */
26925 || deleted_p)
26926 {
26927 /* The definition of the static data member is now required so
26928 we must substitute the initializer. */
26929 if (VAR_P (d)
26930 && !DECL_INITIAL (d)
26931 && DECL_INITIAL (code_pattern))
26932 {
26933 tree ns;
26934 tree init;
26935 bool const_init = false;
26936 bool enter_context = DECL_CLASS_SCOPE_P (d);
26937
26938 ns = decl_namespace_context (d);
26939 push_nested_namespace (ns);
26940 if (enter_context)
26941 push_nested_class (DECL_CONTEXT (d));
26942 init = tsubst_expr (DECL_INITIAL (code_pattern),
26943 args,
26944 tf_warning_or_error, NULL_TREE);
26945 /* If instantiating the initializer involved instantiating this
26946 again, don't call cp_finish_decl twice. */
26947 if (!DECL_INITIAL (d))
26948 {
26949 /* Make sure the initializer is still constant, in case of
26950 circular dependency (template/instantiate6.C). */
26951 const_init
26952 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26953 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26954 /*asmspec_tree=*/NULL_TREE, 0);
26955 }
26956 if (enter_context)
26957 pop_nested_class ();
26958 pop_nested_namespace (ns);
26959 }
26960
26961 /* We restore the source position here because it's used by
26962 add_pending_template. */
26963 input_location = saved_loc;
26964
26965 if (at_eof && !pattern_defined
26966 && DECL_EXPLICIT_INSTANTIATION (d)
26967 && DECL_NOT_REALLY_EXTERN (d))
26968 /* [temp.explicit]
26969
26970 The definition of a non-exported function template, a
26971 non-exported member function template, or a non-exported
26972 member function or static data member of a class template
26973 shall be present in every translation unit in which it is
26974 explicitly instantiated. */
26975 permerror (input_location, "explicit instantiation of %qD "
26976 "but no definition available", d);
26977
26978 /* If we're in unevaluated context, we just wanted to get the
26979 constant value; this isn't an odr use, so don't queue
26980 a full instantiation. */
26981 if (!cp_unevaluated_operand
26982 /* ??? Historically, we have instantiated inline functions, even
26983 when marked as "extern template". */
26984 && !(external_p && VAR_P (d)))
26985 add_pending_template (d);
26986 }
26987 else
26988 {
26989 set_instantiating_module (d);
26990 if (variable_template_p (gen_tmpl))
26991 note_variable_template_instantiation (d);
26992 instantiate_body (td, args, d, false);
26993 }
26994
26995 pop_deferring_access_checks ();
26996 pop_tinst_level ();
26997 input_location = saved_loc;
26998 cp_unevaluated_operand = saved_unevaluated_operand;
26999 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27000
27001 return d;
27002 }
27003
27004 /* Run through the list of templates that we wish we could
27005 instantiate, and instantiate any we can. RETRIES is the
27006 number of times we retry pending template instantiation. */
27007
27008 void
27009 instantiate_pending_templates (int retries)
27010 {
27011 int reconsider;
27012 location_t saved_loc = input_location;
27013
27014 /* Instantiating templates may trigger vtable generation. This in turn
27015 may require further template instantiations. We place a limit here
27016 to avoid infinite loop. */
27017 if (pending_templates && retries >= max_tinst_depth)
27018 {
27019 tree decl = pending_templates->tinst->maybe_get_node ();
27020
27021 fatal_error (input_location,
27022 "template instantiation depth exceeds maximum of %d"
27023 " instantiating %q+D, possibly from virtual table generation"
27024 " (use %<-ftemplate-depth=%> to increase the maximum)",
27025 max_tinst_depth, decl);
27026 if (TREE_CODE (decl) == FUNCTION_DECL)
27027 /* Pretend that we defined it. */
27028 DECL_INITIAL (decl) = error_mark_node;
27029 return;
27030 }
27031
27032 do
27033 {
27034 struct pending_template **t = &pending_templates;
27035 struct pending_template *last = NULL;
27036 reconsider = 0;
27037 while (*t)
27038 {
27039 tree instantiation = reopen_tinst_level ((*t)->tinst);
27040 bool complete = false;
27041
27042 if (TYPE_P (instantiation))
27043 {
27044 if (!COMPLETE_TYPE_P (instantiation))
27045 {
27046 instantiate_class_template (instantiation);
27047 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
27048 for (tree fld = TYPE_FIELDS (instantiation);
27049 fld; fld = TREE_CHAIN (fld))
27050 if ((VAR_P (fld)
27051 || (TREE_CODE (fld) == FUNCTION_DECL
27052 && !DECL_ARTIFICIAL (fld)))
27053 && DECL_TEMPLATE_INSTANTIATION (fld))
27054 instantiate_decl (fld,
27055 /*defer_ok=*/false,
27056 /*expl_inst_class_mem_p=*/false);
27057
27058 if (COMPLETE_TYPE_P (instantiation))
27059 reconsider = 1;
27060 }
27061
27062 complete = COMPLETE_TYPE_P (instantiation);
27063 }
27064 else
27065 {
27066 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
27067 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
27068 {
27069 instantiation
27070 = instantiate_decl (instantiation,
27071 /*defer_ok=*/false,
27072 /*expl_inst_class_mem_p=*/false);
27073 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
27074 reconsider = 1;
27075 }
27076
27077 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
27078 || DECL_TEMPLATE_INSTANTIATED (instantiation));
27079 }
27080
27081 if (complete)
27082 {
27083 /* If INSTANTIATION has been instantiated, then we don't
27084 need to consider it again in the future. */
27085 struct pending_template *drop = *t;
27086 *t = (*t)->next;
27087 set_refcount_ptr (drop->tinst);
27088 pending_template_freelist ().free (drop);
27089 }
27090 else
27091 {
27092 last = *t;
27093 t = &(*t)->next;
27094 }
27095 tinst_depth = 0;
27096 set_refcount_ptr (current_tinst_level);
27097 }
27098 last_pending_template = last;
27099 }
27100 while (reconsider);
27101
27102 input_location = saved_loc;
27103 }
27104
27105 /* Substitute ARGVEC into T, which is a list of initializers for
27106 either base class or a non-static data member. The TREE_PURPOSEs
27107 are DECLs, and the TREE_VALUEs are the initializer values. Used by
27108 instantiate_decl. */
27109
27110 static tree
27111 tsubst_initializer_list (tree t, tree argvec)
27112 {
27113 tree inits = NULL_TREE;
27114 tree target_ctor = error_mark_node;
27115
27116 for (; t; t = TREE_CHAIN (t))
27117 {
27118 tree decl;
27119 tree init;
27120 tree expanded_bases = NULL_TREE;
27121 tree expanded_arguments = NULL_TREE;
27122 int i, len = 1;
27123
27124 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
27125 {
27126 tree expr;
27127 tree arg;
27128
27129 /* Expand the base class expansion type into separate base
27130 classes. */
27131 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
27132 tf_warning_or_error,
27133 NULL_TREE);
27134 if (expanded_bases == error_mark_node)
27135 continue;
27136
27137 /* We'll be building separate TREE_LISTs of arguments for
27138 each base. */
27139 len = TREE_VEC_LENGTH (expanded_bases);
27140 expanded_arguments = make_tree_vec (len);
27141 for (i = 0; i < len; i++)
27142 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
27143
27144 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
27145 expand each argument in the TREE_VALUE of t. */
27146 expr = make_node (EXPR_PACK_EXPANSION);
27147 PACK_EXPANSION_LOCAL_P (expr) = true;
27148 PACK_EXPANSION_PARAMETER_PACKS (expr) =
27149 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
27150
27151 if (TREE_VALUE (t) == void_type_node)
27152 /* VOID_TYPE_NODE is used to indicate
27153 value-initialization. */
27154 {
27155 for (i = 0; i < len; i++)
27156 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
27157 }
27158 else
27159 {
27160 /* Substitute parameter packs into each argument in the
27161 TREE_LIST. */
27162 in_base_initializer = 1;
27163 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
27164 {
27165 tree expanded_exprs;
27166
27167 /* Expand the argument. */
27168 tree value;
27169 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27170 value = TREE_VALUE (arg);
27171 else
27172 {
27173 value = expr;
27174 PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
27175 }
27176 expanded_exprs
27177 = tsubst_pack_expansion (value, argvec,
27178 tf_warning_or_error,
27179 NULL_TREE);
27180 if (expanded_exprs == error_mark_node)
27181 continue;
27182
27183 /* Prepend each of the expanded expressions to the
27184 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
27185 for (i = 0; i < len; i++)
27186 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
27187 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
27188 TREE_VEC_ELT (expanded_arguments, i)
27189 = tree_cons (NULL_TREE,
27190 TREE_VEC_ELT (expanded_exprs, j),
27191 TREE_VEC_ELT (expanded_arguments, i));
27192 else
27193 TREE_VEC_ELT (expanded_arguments, i)
27194 = tree_cons (NULL_TREE,
27195 TREE_VEC_ELT (expanded_exprs, i),
27196 TREE_VEC_ELT (expanded_arguments, i));
27197 }
27198 in_base_initializer = 0;
27199
27200 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
27201 since we built them backwards. */
27202 for (i = 0; i < len; i++)
27203 {
27204 TREE_VEC_ELT (expanded_arguments, i) =
27205 nreverse (TREE_VEC_ELT (expanded_arguments, i));
27206 }
27207 }
27208 }
27209
27210 for (i = 0; i < len; ++i)
27211 {
27212 if (expanded_bases)
27213 {
27214 decl = TREE_VEC_ELT (expanded_bases, i);
27215 decl = expand_member_init (decl);
27216 init = TREE_VEC_ELT (expanded_arguments, i);
27217 }
27218 else
27219 {
27220 tree tmp;
27221 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
27222 tf_warning_or_error, NULL_TREE);
27223
27224 decl = expand_member_init (decl);
27225 if (decl && !DECL_P (decl))
27226 in_base_initializer = 1;
27227
27228 init = TREE_VALUE (t);
27229 tmp = init;
27230 if (init != void_type_node)
27231 init = tsubst_expr (init, argvec,
27232 tf_warning_or_error, NULL_TREE);
27233 if (init == NULL_TREE && tmp != NULL_TREE)
27234 /* If we had an initializer but it instantiated to nothing,
27235 value-initialize the object. This will only occur when
27236 the initializer was a pack expansion where the parameter
27237 packs used in that expansion were of length zero. */
27238 init = void_type_node;
27239 in_base_initializer = 0;
27240 }
27241
27242 if (target_ctor != error_mark_node
27243 && init != error_mark_node)
27244 {
27245 error ("mem-initializer for %qD follows constructor delegation",
27246 decl);
27247 return inits;
27248 }
27249 /* Look for a target constructor. */
27250 if (init != error_mark_node
27251 && decl && CLASS_TYPE_P (decl)
27252 && same_type_p (decl, current_class_type))
27253 {
27254 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
27255 if (inits)
27256 {
27257 error ("constructor delegation follows mem-initializer for %qD",
27258 TREE_PURPOSE (inits));
27259 continue;
27260 }
27261 target_ctor = init;
27262 }
27263
27264 if (decl)
27265 {
27266 init = build_tree_list (decl, init);
27267 /* Carry over the dummy TREE_TYPE node containing the source
27268 location. */
27269 TREE_TYPE (init) = TREE_TYPE (t);
27270 TREE_CHAIN (init) = inits;
27271 inits = init;
27272 }
27273 }
27274 }
27275 return inits;
27276 }
27277
27278 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
27279 is the instantiation (which should have been created with
27280 start_enum) and ARGS are the template arguments to use. */
27281
27282 static void
27283 tsubst_enum (tree tag, tree newtag, tree args)
27284 {
27285 tree e;
27286
27287 if (SCOPED_ENUM_P (newtag))
27288 begin_scope (sk_scoped_enum, newtag);
27289
27290 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
27291 {
27292 tree value;
27293 tree decl = TREE_VALUE (e);
27294
27295 /* Note that in a template enum, the TREE_VALUE is the
27296 CONST_DECL, not the corresponding INTEGER_CST. */
27297 value = tsubst_expr (DECL_INITIAL (decl),
27298 args, tf_warning_or_error, NULL_TREE);
27299
27300 /* Give this enumeration constant the correct access. */
27301 set_current_access_from_decl (decl);
27302
27303 /* Actually build the enumerator itself. Here we're assuming that
27304 enumerators can't have dependent attributes. */
27305 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
27306 DECL_ATTRIBUTES (decl),
27307 DECL_SOURCE_LOCATION (decl));
27308 /* Attribute deprecated without an argument isn't sticky: it'll
27309 melt into a tree flag, so we need to propagate the flag here,
27310 since we just created a new enumerator. */
27311 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
27312 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
27313 }
27314
27315 if (SCOPED_ENUM_P (newtag))
27316 finish_scope ();
27317
27318 finish_enum_value_list (newtag);
27319 finish_enum (newtag);
27320
27321 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
27322 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
27323 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
27324 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
27325 }
27326
27327 /* DECL is a FUNCTION_DECL that is a template specialization. Return
27328 its type -- but without substituting the innermost set of template
27329 arguments. So, innermost set of template parameters will appear in
27330 the type. */
27331
27332 tree
27333 get_mostly_instantiated_function_type (tree decl)
27334 {
27335 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
27336 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
27337 }
27338
27339 /* Return truthvalue if we're processing a template different from
27340 the last one involved in diagnostics. */
27341 bool
27342 problematic_instantiation_changed (void)
27343 {
27344 return current_tinst_level != last_error_tinst_level;
27345 }
27346
27347 /* Remember current template involved in diagnostics. */
27348 void
27349 record_last_problematic_instantiation (void)
27350 {
27351 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
27352 }
27353
27354 struct tinst_level *
27355 current_instantiation (void)
27356 {
27357 return current_tinst_level;
27358 }
27359
27360 /* Return TRUE if current_function_decl is being instantiated, false
27361 otherwise. */
27362
27363 bool
27364 instantiating_current_function_p (void)
27365 {
27366 return (current_instantiation ()
27367 && (current_instantiation ()->maybe_get_node ()
27368 == current_function_decl));
27369 }
27370
27371 /* [temp.param] Check that template non-type parm TYPE is of an allowable
27372 type. Return false for ok, true for disallowed. Issue error and
27373 inform messages under control of COMPLAIN. */
27374
27375 static bool
27376 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
27377 {
27378 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
27379 return false;
27380 else if (TYPE_PTR_P (type))
27381 return false;
27382 else if (TYPE_REF_P (type)
27383 && !TYPE_REF_IS_RVALUE (type))
27384 return false;
27385 else if (TYPE_PTRMEM_P (type))
27386 return false;
27387 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
27388 {
27389 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
27390 {
27391 if (complain & tf_error)
27392 error ("non-type template parameters of deduced class type only "
27393 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27394 return true;
27395 }
27396 return false;
27397 }
27398 else if (TREE_CODE (type) == NULLPTR_TYPE)
27399 return false;
27400 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
27401 && cxx_dialect < cxx11)
27402 /* Fall through; before C++11 alias templates, a bound ttp
27403 always instantiates into a class type. */;
27404 else if (WILDCARD_TYPE_P (type))
27405 /* Any other wildcard type not already handled above is allowed. */
27406 return false;
27407 else if (TREE_CODE (type) == COMPLEX_TYPE)
27408 /* Fall through. */;
27409 else if (VOID_TYPE_P (type))
27410 /* Fall through. */;
27411 else if (cxx_dialect >= cxx20)
27412 {
27413 if (dependent_type_p (type))
27414 return false;
27415 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
27416 return true;
27417 if (structural_type_p (type))
27418 return false;
27419 if (complain & tf_error)
27420 {
27421 auto_diagnostic_group d;
27422 error ("%qT is not a valid type for a template non-type "
27423 "parameter because it is not structural", type);
27424 structural_type_p (type, true);
27425 }
27426 return true;
27427 }
27428 else if (CLASS_TYPE_P (type))
27429 {
27430 if (complain & tf_error)
27431 error ("non-type template parameters of class type only available "
27432 "with %<-std=c++20%> or %<-std=gnu++20%>");
27433 return true;
27434 }
27435
27436 if (complain & tf_error)
27437 {
27438 if (type == error_mark_node)
27439 inform (input_location, "invalid template non-type parameter");
27440 else
27441 error ("%q#T is not a valid type for a template non-type parameter",
27442 type);
27443 }
27444 return true;
27445 }
27446
27447 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */
27448
27449 static bool
27450 value_dependent_noexcept_spec_p (tree type)
27451 {
27452 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
27453 if (tree noex = TREE_PURPOSE (spec))
27454 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
27455 affect overload resolution and treating it as dependent breaks
27456 things. Same for an unparsed noexcept expression. */
27457 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
27458 && TREE_CODE (noex) != DEFERRED_PARSE
27459 && value_dependent_expression_p (noex))
27460 return true;
27461
27462 return false;
27463 }
27464
27465 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
27466 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
27467
27468 static bool
27469 dependent_type_p_r (tree type)
27470 {
27471 tree scope;
27472
27473 /* [temp.dep.type]
27474
27475 A type is dependent if it is:
27476
27477 -- a template parameter. Template template parameters are types
27478 for us (since TYPE_P holds true for them) so we handle
27479 them here. */
27480 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27481 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
27482 return true;
27483 /* -- a qualified-id with a nested-name-specifier which contains a
27484 class-name that names a dependent type or whose unqualified-id
27485 names a dependent type. */
27486 if (TREE_CODE (type) == TYPENAME_TYPE)
27487 return true;
27488
27489 /* An alias template specialization can be dependent even if the
27490 resulting type is not. */
27491 if (dependent_alias_template_spec_p (type, nt_transparent))
27492 return true;
27493
27494 /* -- a cv-qualified type where the cv-unqualified type is
27495 dependent.
27496 No code is necessary for this bullet; the code below handles
27497 cv-qualified types, and we don't want to strip aliases with
27498 TYPE_MAIN_VARIANT because of DR 1558. */
27499 /* -- a compound type constructed from any dependent type. */
27500 if (TYPE_PTRMEM_P (type))
27501 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
27502 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
27503 (type)));
27504 else if (INDIRECT_TYPE_P (type))
27505 return dependent_type_p (TREE_TYPE (type));
27506 else if (FUNC_OR_METHOD_TYPE_P (type))
27507 {
27508 tree arg_type;
27509
27510 if (dependent_type_p (TREE_TYPE (type)))
27511 return true;
27512 for (arg_type = TYPE_ARG_TYPES (type);
27513 arg_type;
27514 arg_type = TREE_CHAIN (arg_type))
27515 if (dependent_type_p (TREE_VALUE (arg_type)))
27516 return true;
27517 if (cxx_dialect >= cxx17
27518 && value_dependent_noexcept_spec_p (type))
27519 /* A value-dependent noexcept-specifier makes the type dependent. */
27520 return true;
27521 return false;
27522 }
27523 /* -- an array type constructed from any dependent type or whose
27524 size is specified by a constant expression that is
27525 value-dependent.
27526
27527 We checked for type- and value-dependence of the bounds in
27528 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
27529 if (TREE_CODE (type) == ARRAY_TYPE)
27530 {
27531 if (TYPE_DOMAIN (type)
27532 && dependent_type_p (TYPE_DOMAIN (type)))
27533 return true;
27534 return dependent_type_p (TREE_TYPE (type));
27535 }
27536
27537 /* -- a template-id in which either the template name is a template
27538 parameter ... */
27539 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
27540 return true;
27541 /* ... or any of the template arguments is a dependent type or
27542 an expression that is type-dependent or value-dependent. */
27543 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
27544 && (any_dependent_template_arguments_p
27545 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
27546 return true;
27547
27548 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
27549 dependent; if the argument of the `typeof' expression is not
27550 type-dependent, then it should already been have resolved. */
27551 if (TREE_CODE (type) == TYPEOF_TYPE
27552 || TREE_CODE (type) == DECLTYPE_TYPE
27553 || TREE_CODE (type) == TRAIT_TYPE)
27554 return true;
27555
27556 /* A template argument pack is dependent if any of its packed
27557 arguments are. */
27558 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
27559 {
27560 tree args = ARGUMENT_PACK_ARGS (type);
27561 for (tree arg : tree_vec_range (args))
27562 if (dependent_template_arg_p (arg))
27563 return true;
27564 }
27565
27566 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
27567 be template parameters. */
27568 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
27569 return true;
27570
27571 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
27572 return true;
27573
27574 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
27575 return true;
27576
27577 /* The standard does not specifically mention types that are local
27578 to template functions or local classes, but they should be
27579 considered dependent too. For example:
27580
27581 template <int I> void f() {
27582 enum E { a = I };
27583 S<sizeof (E)> s;
27584 }
27585
27586 The size of `E' cannot be known until the value of `I' has been
27587 determined. Therefore, `E' must be considered dependent. */
27588 scope = TYPE_CONTEXT (type);
27589 if (scope && TYPE_P (scope))
27590 return dependent_type_p (scope);
27591 /* Don't use type_dependent_expression_p here, as it can lead
27592 to infinite recursion trying to determine whether a lambda
27593 nested in a lambda is dependent (c++/47687). */
27594 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
27595 && DECL_LANG_SPECIFIC (scope)
27596 && DECL_TEMPLATE_INFO (scope)
27597 && (any_dependent_template_arguments_p
27598 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
27599 return true;
27600
27601 /* Other types are non-dependent. */
27602 return false;
27603 }
27604
27605 /* Returns TRUE if TYPE is dependent, in the sense of
27606 [temp.dep.type]. Note that a NULL type is considered dependent. */
27607
27608 bool
27609 dependent_type_p (tree type)
27610 {
27611 /* If there are no template parameters in scope, then there can't be
27612 any dependent types. */
27613 if (!processing_template_decl)
27614 {
27615 /* If we are not processing a template, then nobody should be
27616 providing us with a dependent type. */
27617 gcc_assert (type);
27618 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
27619 return false;
27620 }
27621
27622 /* If the type is NULL, we have not computed a type for the entity
27623 in question; in that case, the type is dependent. */
27624 if (!type)
27625 return true;
27626
27627 /* Erroneous types can be considered non-dependent. */
27628 if (type == error_mark_node)
27629 return false;
27630
27631 /* If we have not already computed the appropriate value for TYPE,
27632 do so now. */
27633 if (!TYPE_DEPENDENT_P_VALID (type))
27634 {
27635 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
27636 TYPE_DEPENDENT_P_VALID (type) = 1;
27637 }
27638
27639 return TYPE_DEPENDENT_P (type);
27640 }
27641
27642 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
27643 lookup. In other words, a dependent type that is not the current
27644 instantiation. */
27645
27646 bool
27647 dependent_scope_p (tree scope)
27648 {
27649 return (scope && TYPE_P (scope) && dependent_type_p (scope)
27650 && !currently_open_class (scope));
27651 }
27652
27653 /* True if we might find more declarations in SCOPE during instantiation than
27654 we can when parsing the template. */
27655
27656 bool
27657 dependentish_scope_p (tree scope)
27658 {
27659 return dependent_scope_p (scope) || any_dependent_bases_p (scope);
27660 }
27661
27662 /* T is a SCOPE_REF. Return whether it represents a non-static member of
27663 an unknown base of 'this' (and is therefore instantiation-dependent). */
27664
27665 static bool
27666 unknown_base_ref_p (tree t)
27667 {
27668 if (!current_class_ptr)
27669 return false;
27670
27671 tree mem = TREE_OPERAND (t, 1);
27672 if (shared_member_p (mem))
27673 return false;
27674
27675 tree cur = current_nonlambda_class_type ();
27676 if (!any_dependent_bases_p (cur))
27677 return false;
27678
27679 tree ctx = TREE_OPERAND (t, 0);
27680 if (DERIVED_FROM_P (ctx, cur))
27681 return false;
27682
27683 return true;
27684 }
27685
27686 /* T is a SCOPE_REF; return whether we need to consider it
27687 instantiation-dependent so that we can check access at instantiation
27688 time even though we know which member it resolves to. */
27689
27690 static bool
27691 instantiation_dependent_scope_ref_p (tree t)
27692 {
27693 if (DECL_P (TREE_OPERAND (t, 1))
27694 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
27695 && !dependent_scope_p (TREE_OPERAND (t, 0))
27696 && !unknown_base_ref_p (t)
27697 && accessible_in_template_p (TREE_OPERAND (t, 0),
27698 TREE_OPERAND (t, 1)))
27699 return false;
27700 else
27701 return true;
27702 }
27703
27704 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
27705 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
27706 expression. */
27707
27708 /* Note that this predicate is not appropriate for general expressions;
27709 only constant expressions (that satisfy potential_constant_expression)
27710 can be tested for value dependence. */
27711
27712 bool
27713 value_dependent_expression_p (tree expression)
27714 {
27715 if (!processing_template_decl || expression == NULL_TREE)
27716 return false;
27717
27718 /* A type-dependent expression is also value-dependent. */
27719 if (type_dependent_expression_p (expression))
27720 return true;
27721
27722 switch (TREE_CODE (expression))
27723 {
27724 case BASELINK:
27725 /* A dependent member function of the current instantiation. */
27726 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
27727
27728 case FUNCTION_DECL:
27729 /* A dependent member function of the current instantiation. */
27730 if (DECL_CLASS_SCOPE_P (expression)
27731 && dependent_type_p (DECL_CONTEXT (expression)))
27732 return true;
27733 break;
27734
27735 case IDENTIFIER_NODE:
27736 /* A name that has not been looked up -- must be dependent. */
27737 return true;
27738
27739 case TEMPLATE_PARM_INDEX:
27740 /* A non-type template parm. */
27741 return true;
27742
27743 case CONST_DECL:
27744 /* A non-type template parm. */
27745 if (DECL_TEMPLATE_PARM_P (expression))
27746 return true;
27747 return value_dependent_expression_p (DECL_INITIAL (expression));
27748
27749 case VAR_DECL:
27750 /* A constant with literal type and is initialized
27751 with an expression that is value-dependent. */
27752 if (DECL_DEPENDENT_INIT_P (expression)
27753 /* FIXME cp_finish_decl doesn't fold reference initializers. */
27754 || TYPE_REF_P (TREE_TYPE (expression)))
27755 return true;
27756 if (DECL_HAS_VALUE_EXPR_P (expression))
27757 {
27758 tree value_expr = DECL_VALUE_EXPR (expression);
27759 if (value_dependent_expression_p (value_expr)
27760 /* __PRETTY_FUNCTION__ inside a template function is dependent
27761 on the name of the function. */
27762 || (DECL_PRETTY_FUNCTION_P (expression)
27763 /* It might be used in a template, but not a template
27764 function, in which case its DECL_VALUE_EXPR will be
27765 "top level". */
27766 && value_expr == error_mark_node))
27767 return true;
27768 }
27769 return false;
27770
27771 case DYNAMIC_CAST_EXPR:
27772 case STATIC_CAST_EXPR:
27773 case CONST_CAST_EXPR:
27774 case REINTERPRET_CAST_EXPR:
27775 case CAST_EXPR:
27776 case IMPLICIT_CONV_EXPR:
27777 /* These expressions are value-dependent if the type to which
27778 the cast occurs is dependent or the expression being casted
27779 is value-dependent. */
27780 {
27781 tree type = TREE_TYPE (expression);
27782
27783 if (dependent_type_p (type))
27784 return true;
27785
27786 /* A functional cast has a list of operands. */
27787 expression = TREE_OPERAND (expression, 0);
27788 if (!expression)
27789 {
27790 /* If there are no operands, it must be an expression such
27791 as "int()". This should not happen for aggregate types
27792 because it would form non-constant expressions. */
27793 gcc_assert (cxx_dialect >= cxx11
27794 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
27795
27796 return false;
27797 }
27798
27799 if (TREE_CODE (expression) == TREE_LIST)
27800 return any_value_dependent_elements_p (expression);
27801
27802 if (TREE_CODE (type) == REFERENCE_TYPE
27803 && has_value_dependent_address (expression))
27804 return true;
27805
27806 return value_dependent_expression_p (expression);
27807 }
27808
27809 case SIZEOF_EXPR:
27810 if (SIZEOF_EXPR_TYPE_P (expression))
27811 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
27812 /* FALLTHRU */
27813 case ALIGNOF_EXPR:
27814 case TYPEID_EXPR:
27815 /* A `sizeof' expression is value-dependent if the operand is
27816 type-dependent or is a pack expansion. */
27817 expression = TREE_OPERAND (expression, 0);
27818 if (PACK_EXPANSION_P (expression))
27819 return true;
27820 else if (TYPE_P (expression))
27821 return dependent_type_p (expression);
27822 return instantiation_dependent_uneval_expression_p (expression);
27823
27824 case AT_ENCODE_EXPR:
27825 /* An 'encode' expression is value-dependent if the operand is
27826 type-dependent. */
27827 expression = TREE_OPERAND (expression, 0);
27828 return dependent_type_p (expression);
27829
27830 case NOEXCEPT_EXPR:
27831 expression = TREE_OPERAND (expression, 0);
27832 return instantiation_dependent_uneval_expression_p (expression);
27833
27834 case SCOPE_REF:
27835 /* All instantiation-dependent expressions should also be considered
27836 value-dependent. */
27837 return instantiation_dependent_scope_ref_p (expression);
27838
27839 case COMPONENT_REF:
27840 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
27841 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
27842
27843 case NONTYPE_ARGUMENT_PACK:
27844 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
27845 is value-dependent. */
27846 for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
27847 if (value_dependent_expression_p (arg))
27848 return true;
27849 return false;
27850
27851 case TRAIT_EXPR:
27852 {
27853 tree type2 = TRAIT_EXPR_TYPE2 (expression);
27854
27855 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
27856 return true;
27857
27858 if (!type2)
27859 return false;
27860
27861 if (TREE_CODE (type2) != TREE_LIST)
27862 return dependent_type_p (type2);
27863
27864 for (; type2; type2 = TREE_CHAIN (type2))
27865 if (dependent_type_p (TREE_VALUE (type2)))
27866 return true;
27867
27868 return false;
27869 }
27870
27871 case MODOP_EXPR:
27872 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27873 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
27874
27875 case ARRAY_REF:
27876 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27877 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
27878
27879 case ADDR_EXPR:
27880 {
27881 tree op = TREE_OPERAND (expression, 0);
27882 return (value_dependent_expression_p (op)
27883 || has_value_dependent_address (op));
27884 }
27885
27886 case REQUIRES_EXPR:
27887 /* Treat all requires-expressions as value-dependent so
27888 we don't try to fold them. */
27889 return true;
27890
27891 case TYPE_REQ:
27892 return dependent_type_p (TREE_OPERAND (expression, 0));
27893
27894 case CALL_EXPR:
27895 {
27896 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
27897 return true;
27898 tree fn = get_callee_fndecl (expression);
27899 int i, nargs;
27900 nargs = call_expr_nargs (expression);
27901 for (i = 0; i < nargs; ++i)
27902 {
27903 tree op = CALL_EXPR_ARG (expression, i);
27904 /* In a call to a constexpr member function, look through the
27905 implicit ADDR_EXPR on the object argument so that it doesn't
27906 cause the call to be considered value-dependent. We also
27907 look through it in potential_constant_expression. */
27908 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
27909 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
27910 && TREE_CODE (op) == ADDR_EXPR)
27911 op = TREE_OPERAND (op, 0);
27912 if (value_dependent_expression_p (op))
27913 return true;
27914 }
27915 return false;
27916 }
27917
27918 case TEMPLATE_ID_EXPR:
27919 return concept_definition_p (TREE_OPERAND (expression, 0))
27920 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
27921
27922 case CONSTRUCTOR:
27923 {
27924 unsigned ix;
27925 tree val;
27926 if (dependent_type_p (TREE_TYPE (expression)))
27927 return true;
27928 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
27929 if (value_dependent_expression_p (val))
27930 return true;
27931 return false;
27932 }
27933
27934 case STMT_EXPR:
27935 /* Treat a GNU statement expression as dependent to avoid crashing
27936 under instantiate_non_dependent_expr; it can't be constant. */
27937 return true;
27938
27939 case NEW_EXPR:
27940 case VEC_NEW_EXPR:
27941 /* The second operand is a type, which type_dependent_expression_p
27942 (and therefore value_dependent_expression_p) doesn't want to see. */
27943 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
27944 || value_dependent_expression_p (TREE_OPERAND (expression, 2))
27945 || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
27946
27947 default:
27948 /* A constant expression is value-dependent if any subexpression is
27949 value-dependent. */
27950 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
27951 {
27952 case tcc_reference:
27953 case tcc_unary:
27954 case tcc_comparison:
27955 case tcc_binary:
27956 case tcc_expression:
27957 case tcc_vl_exp:
27958 {
27959 int i, len = cp_tree_operand_length (expression);
27960
27961 for (i = 0; i < len; i++)
27962 {
27963 tree t = TREE_OPERAND (expression, i);
27964
27965 /* In some cases, some of the operands may be missing.
27966 (For example, in the case of PREDECREMENT_EXPR, the
27967 amount to increment by may be missing.) That doesn't
27968 make the expression dependent. */
27969 if (t && value_dependent_expression_p (t))
27970 return true;
27971 }
27972 }
27973 break;
27974 default:
27975 break;
27976 }
27977 break;
27978 }
27979
27980 /* The expression is not value-dependent. */
27981 return false;
27982 }
27983
27984 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27985 [temp.dep.expr]. Note that an expression with no type is
27986 considered dependent. Other parts of the compiler arrange for an
27987 expression with type-dependent subexpressions to have no type, so
27988 this function doesn't have to be fully recursive. */
27989
27990 bool
27991 type_dependent_expression_p (tree expression)
27992 {
27993 if (!processing_template_decl)
27994 return false;
27995
27996 if (expression == NULL_TREE || expression == error_mark_node)
27997 return false;
27998
27999 gcc_checking_assert (!TYPE_P (expression));
28000
28001 STRIP_ANY_LOCATION_WRAPPER (expression);
28002
28003 /* An unresolved name is always dependent. */
28004 if (identifier_p (expression)
28005 || TREE_CODE (expression) == USING_DECL
28006 || TREE_CODE (expression) == WILDCARD_DECL)
28007 return true;
28008
28009 /* A lambda-expression in template context is dependent. dependent_type_p is
28010 true for a lambda in the scope of a class or function template, but that
28011 doesn't cover all template contexts, like a default template argument. */
28012 if (TREE_CODE (expression) == LAMBDA_EXPR)
28013 return true;
28014
28015 /* A fold expression is type-dependent. */
28016 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
28017 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
28018 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
28019 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
28020 return true;
28021
28022 /* Some expression forms are never type-dependent. */
28023 if (TREE_CODE (expression) == SIZEOF_EXPR
28024 || TREE_CODE (expression) == ALIGNOF_EXPR
28025 || TREE_CODE (expression) == AT_ENCODE_EXPR
28026 || TREE_CODE (expression) == NOEXCEPT_EXPR
28027 || TREE_CODE (expression) == TRAIT_EXPR
28028 || TREE_CODE (expression) == TYPEID_EXPR
28029 || TREE_CODE (expression) == DELETE_EXPR
28030 || TREE_CODE (expression) == VEC_DELETE_EXPR
28031 || TREE_CODE (expression) == THROW_EXPR
28032 || TREE_CODE (expression) == REQUIRES_EXPR)
28033 return false;
28034
28035 /* The types of these expressions depends only on the type to which
28036 the cast occurs. */
28037 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
28038 || TREE_CODE (expression) == STATIC_CAST_EXPR
28039 || TREE_CODE (expression) == CONST_CAST_EXPR
28040 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
28041 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
28042 || TREE_CODE (expression) == CAST_EXPR)
28043 return dependent_type_p (TREE_TYPE (expression));
28044
28045 /* The types of these expressions depends only on the type created
28046 by the expression. */
28047 if (TREE_CODE (expression) == NEW_EXPR
28048 || TREE_CODE (expression) == VEC_NEW_EXPR)
28049 {
28050 /* For NEW_EXPR tree nodes created inside a template, either
28051 the object type itself or a TREE_LIST may appear as the
28052 operand 1. */
28053 tree type = TREE_OPERAND (expression, 1);
28054 if (TREE_CODE (type) == TREE_LIST)
28055 /* This is an array type. We need to check array dimensions
28056 as well. */
28057 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
28058 || value_dependent_expression_p
28059 (TREE_OPERAND (TREE_VALUE (type), 1));
28060 /* Array type whose dimension has to be deduced. */
28061 else if (TREE_CODE (type) == ARRAY_TYPE
28062 && TREE_OPERAND (expression, 2) == NULL_TREE)
28063 return true;
28064 else
28065 return dependent_type_p (type);
28066 }
28067
28068 if (TREE_CODE (expression) == SCOPE_REF)
28069 {
28070 tree scope = TREE_OPERAND (expression, 0);
28071 tree name = TREE_OPERAND (expression, 1);
28072
28073 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
28074 contains an identifier associated by name lookup with one or more
28075 declarations declared with a dependent type, or...a
28076 nested-name-specifier or qualified-id that names a member of an
28077 unknown specialization. */
28078 return (type_dependent_expression_p (name)
28079 || dependent_scope_p (scope));
28080 }
28081
28082 if (TREE_CODE (expression) == TEMPLATE_DECL
28083 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
28084 return uses_outer_template_parms (expression);
28085
28086 if (TREE_CODE (expression) == STMT_EXPR)
28087 expression = stmt_expr_value_expr (expression);
28088
28089 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
28090 {
28091 for (auto &elt : CONSTRUCTOR_ELTS (expression))
28092 if (type_dependent_expression_p (elt.value))
28093 return true;
28094 return false;
28095 }
28096
28097 /* A static data member of the current instantiation with incomplete
28098 array type is type-dependent, as the definition and specializations
28099 can have different bounds. */
28100 if (VAR_P (expression)
28101 && DECL_CLASS_SCOPE_P (expression)
28102 && dependent_type_p (DECL_CONTEXT (expression))
28103 && VAR_HAD_UNKNOWN_BOUND (expression))
28104 return true;
28105
28106 /* An array of unknown bound depending on a variadic parameter, eg:
28107
28108 template<typename... Args>
28109 void foo (Args... args)
28110 {
28111 int arr[] = { args... };
28112 }
28113
28114 template<int... vals>
28115 void bar ()
28116 {
28117 int arr[] = { vals... };
28118 }
28119
28120 If the array has no length and has an initializer, it must be that
28121 we couldn't determine its length in cp_complete_array_type because
28122 it is dependent. */
28123 if (((VAR_P (expression) && DECL_INITIAL (expression))
28124 || COMPOUND_LITERAL_P (expression))
28125 && TREE_TYPE (expression) != NULL_TREE
28126 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
28127 && !TYPE_DOMAIN (TREE_TYPE (expression)))
28128 return true;
28129
28130 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */
28131 if (BASELINK_P (expression))
28132 {
28133 if (BASELINK_OPTYPE (expression)
28134 && dependent_type_p (BASELINK_OPTYPE (expression)))
28135 return true;
28136 expression = BASELINK_FUNCTIONS (expression);
28137 }
28138
28139 /* A function or variable template-id is type-dependent if it has any
28140 dependent template arguments. */
28141 if (VAR_OR_FUNCTION_DECL_P (expression)
28142 && DECL_LANG_SPECIFIC (expression)
28143 && DECL_TEMPLATE_INFO (expression))
28144 {
28145 /* Consider the innermost template arguments, since those are the ones
28146 that come from the template-id; the template arguments for the
28147 enclosing class do not make it type-dependent unless they are used in
28148 the type of the decl. */
28149 if (instantiates_primary_template_p (expression)
28150 && (any_dependent_template_arguments_p
28151 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
28152 return true;
28153 }
28154
28155 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
28156 type-dependent. Checking this is important for functions with auto return
28157 type, which looks like a dependent type. */
28158 if (TREE_CODE (expression) == FUNCTION_DECL
28159 && !(DECL_CLASS_SCOPE_P (expression)
28160 && dependent_type_p (DECL_CONTEXT (expression)))
28161 && !(DECL_LANG_SPECIFIC (expression)
28162 && DECL_UNIQUE_FRIEND_P (expression)
28163 && (!DECL_FRIEND_CONTEXT (expression)
28164 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
28165 && !DECL_LOCAL_DECL_P (expression))
28166 {
28167 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
28168 || undeduced_auto_decl (expression));
28169 return false;
28170 }
28171
28172 /* Otherwise, its constraints could still depend on outer template parameters
28173 from its (dependent) scope. */
28174 if (TREE_CODE (expression) == FUNCTION_DECL
28175 /* As an optimization, check this cheaper sufficient condition first.
28176 (At this point we've established that we're looking at a member of
28177 a dependent class, so it makes sense to start treating say undeduced
28178 auto as dependent.) */
28179 && !dependent_type_p (TREE_TYPE (expression))
28180 && uses_outer_template_parms_in_constraints (expression))
28181 return true;
28182
28183 /* Always dependent, on the number of arguments if nothing else. */
28184 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
28185 return true;
28186
28187 if (TREE_TYPE (expression) == unknown_type_node)
28188 {
28189 if (TREE_CODE (expression) == ADDR_EXPR)
28190 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
28191 if (TREE_CODE (expression) == COMPONENT_REF
28192 || TREE_CODE (expression) == OFFSET_REF)
28193 {
28194 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
28195 return true;
28196 expression = TREE_OPERAND (expression, 1);
28197 if (identifier_p (expression))
28198 return false;
28199 }
28200 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
28201 if (TREE_CODE (expression) == SCOPE_REF)
28202 return false;
28203
28204 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
28205 if (TREE_CODE (expression) == CO_AWAIT_EXPR
28206 || TREE_CODE (expression) == CO_YIELD_EXPR)
28207 return true;
28208
28209 if (BASELINK_P (expression))
28210 {
28211 if (BASELINK_OPTYPE (expression)
28212 && dependent_type_p (BASELINK_OPTYPE (expression)))
28213 return true;
28214 expression = BASELINK_FUNCTIONS (expression);
28215 }
28216
28217 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
28218 {
28219 if (any_dependent_template_arguments_p
28220 (TREE_OPERAND (expression, 1)))
28221 return true;
28222 expression = TREE_OPERAND (expression, 0);
28223 if (identifier_p (expression))
28224 return true;
28225 }
28226
28227 gcc_assert (OVL_P (expression));
28228
28229 for (lkp_iterator iter (expression); iter; ++iter)
28230 if (type_dependent_expression_p (*iter))
28231 return true;
28232
28233 return false;
28234 }
28235
28236 /* The type of a non-type template parm declared with a placeholder type
28237 depends on the corresponding template argument, even though
28238 placeholders are not normally considered dependent. */
28239 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
28240 && is_auto (TREE_TYPE (expression)))
28241 return true;
28242
28243 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
28244
28245 /* Dependent type attributes might not have made it from the decl to
28246 the type yet. */
28247 if (DECL_P (expression)
28248 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
28249 return true;
28250
28251 return (dependent_type_p (TREE_TYPE (expression)));
28252 }
28253
28254 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
28255 type-dependent if the expression refers to a member of the current
28256 instantiation and the type of the referenced member is dependent, or the
28257 class member access expression refers to a member of an unknown
28258 specialization.
28259
28260 This function returns true if the OBJECT in such a class member access
28261 expression is of an unknown specialization. */
28262
28263 bool
28264 type_dependent_object_expression_p (tree object)
28265 {
28266 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
28267 dependent. */
28268 if (TREE_CODE (object) == IDENTIFIER_NODE)
28269 return true;
28270 tree scope = TREE_TYPE (object);
28271 return (!scope || dependent_scope_p (scope));
28272 }
28273
28274 /* walk_tree callback function for instantiation_dependent_expression_p,
28275 below. Returns non-zero if a dependent subexpression is found. */
28276
28277 static tree
28278 instantiation_dependent_r (tree *tp, int *walk_subtrees,
28279 void * /*data*/)
28280 {
28281 if (TYPE_P (*tp))
28282 {
28283 /* We don't have to worry about decltype currently because decltype
28284 of an instantiation-dependent expr is a dependent type. This
28285 might change depending on the resolution of DR 1172. */
28286 *walk_subtrees = false;
28287 return NULL_TREE;
28288 }
28289 enum tree_code code = TREE_CODE (*tp);
28290 switch (code)
28291 {
28292 /* Don't treat an argument list as dependent just because it has no
28293 TREE_TYPE. */
28294 case TREE_LIST:
28295 case TREE_VEC:
28296 case NONTYPE_ARGUMENT_PACK:
28297 return NULL_TREE;
28298
28299 case TEMPLATE_PARM_INDEX:
28300 if (dependent_type_p (TREE_TYPE (*tp)))
28301 return *tp;
28302 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
28303 return *tp;
28304 /* We'll check value-dependence separately. */
28305 return NULL_TREE;
28306
28307 /* Handle expressions with type operands. */
28308 case SIZEOF_EXPR:
28309 case ALIGNOF_EXPR:
28310 case TYPEID_EXPR:
28311 case AT_ENCODE_EXPR:
28312 {
28313 tree op = TREE_OPERAND (*tp, 0);
28314 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
28315 op = TREE_TYPE (op);
28316 if (TYPE_P (op))
28317 {
28318 if (dependent_type_p (op))
28319 return *tp;
28320 else
28321 {
28322 *walk_subtrees = false;
28323 return NULL_TREE;
28324 }
28325 }
28326 break;
28327 }
28328
28329 case COMPONENT_REF:
28330 if (identifier_p (TREE_OPERAND (*tp, 1)))
28331 /* In a template, finish_class_member_access_expr creates a
28332 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
28333 type-dependent, so that we can check access control at
28334 instantiation time (PR 42277). See also Core issue 1273. */
28335 return *tp;
28336 break;
28337
28338 case SCOPE_REF:
28339 if (instantiation_dependent_scope_ref_p (*tp))
28340 return *tp;
28341 else
28342 break;
28343
28344 /* Treat statement-expressions as dependent. */
28345 case BIND_EXPR:
28346 return *tp;
28347
28348 /* Treat requires-expressions as dependent. */
28349 case REQUIRES_EXPR:
28350 return *tp;
28351
28352 case CONSTRUCTOR:
28353 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
28354 return *tp;
28355 break;
28356
28357 case TEMPLATE_DECL:
28358 case FUNCTION_DECL:
28359 /* Before C++17, a noexcept-specifier isn't part of the function type
28360 so it doesn't affect type dependence, but we still want to consider it
28361 for instantiation dependence. */
28362 if (cxx_dialect < cxx17
28363 && DECL_DECLARES_FUNCTION_P (*tp)
28364 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
28365 return *tp;
28366 break;
28367
28368 default:
28369 break;
28370 }
28371
28372 if (type_dependent_expression_p (*tp))
28373 return *tp;
28374 else
28375 return NULL_TREE;
28376 }
28377
28378 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
28379 sense defined by the ABI:
28380
28381 "An expression is instantiation-dependent if it is type-dependent
28382 or value-dependent, or it has a subexpression that is type-dependent
28383 or value-dependent."
28384
28385 Except don't actually check value-dependence for unevaluated expressions,
28386 because in sizeof(i) we don't care about the value of i. Checking
28387 type-dependence will in turn check value-dependence of array bounds/template
28388 arguments as needed. */
28389
28390 bool
28391 instantiation_dependent_uneval_expression_p (tree expression)
28392 {
28393 tree result;
28394
28395 if (!processing_template_decl)
28396 return false;
28397
28398 if (expression == error_mark_node)
28399 return false;
28400
28401 result = cp_walk_tree_without_duplicates (&expression,
28402 instantiation_dependent_r, NULL);
28403 return result != NULL_TREE;
28404 }
28405
28406 /* As above, but also check value-dependence of the expression as a whole. */
28407
28408 bool
28409 instantiation_dependent_expression_p (tree expression)
28410 {
28411 return (instantiation_dependent_uneval_expression_p (expression)
28412 || (processing_template_decl
28413 && potential_constant_expression (expression)
28414 && value_dependent_expression_p (expression)));
28415 }
28416
28417 /* Like type_dependent_expression_p, but it also works while not processing
28418 a template definition, i.e. during substitution or mangling. */
28419
28420 bool
28421 type_dependent_expression_p_push (tree expr)
28422 {
28423 bool b;
28424 ++processing_template_decl;
28425 b = type_dependent_expression_p (expr);
28426 --processing_template_decl;
28427 return b;
28428 }
28429
28430 /* Returns TRUE if ARGS contains a type-dependent expression. */
28431
28432 bool
28433 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
28434 {
28435 unsigned int i;
28436 tree arg;
28437
28438 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
28439 {
28440 if (type_dependent_expression_p (arg))
28441 return true;
28442 }
28443 return false;
28444 }
28445
28446 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28447 expressions) contains any type-dependent expressions. */
28448
28449 bool
28450 any_type_dependent_elements_p (const_tree list)
28451 {
28452 for (; list; list = TREE_CHAIN (list))
28453 if (type_dependent_expression_p (TREE_VALUE (list)))
28454 return true;
28455
28456 return false;
28457 }
28458
28459 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
28460 expressions) contains any value-dependent expressions. */
28461
28462 bool
28463 any_value_dependent_elements_p (const_tree list)
28464 {
28465 for (; list; list = TREE_CHAIN (list))
28466 if (value_dependent_expression_p (TREE_VALUE (list)))
28467 return true;
28468
28469 return false;
28470 }
28471
28472 /* Returns TRUE if the ARG (a template argument) is dependent. */
28473
28474 bool
28475 dependent_template_arg_p (tree arg)
28476 {
28477 if (!processing_template_decl)
28478 return false;
28479
28480 /* Assume a template argument that was wrongly written by the user
28481 is dependent. This is consistent with what
28482 any_dependent_template_arguments_p [that calls this function]
28483 does. */
28484 if (!arg || arg == error_mark_node)
28485 return true;
28486
28487 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
28488 arg = argument_pack_select_arg (arg);
28489
28490 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
28491 return true;
28492 if (TREE_CODE (arg) == TEMPLATE_DECL)
28493 {
28494 if (DECL_TEMPLATE_PARM_P (arg))
28495 return true;
28496 /* A member template of a dependent class is not necessarily
28497 type-dependent, but it is a dependent template argument because it
28498 will be a member of an unknown specialization to that template. */
28499 tree scope = CP_DECL_CONTEXT (arg);
28500 return TYPE_P (scope) && dependent_type_p (scope);
28501 }
28502 else if (ARGUMENT_PACK_P (arg))
28503 {
28504 tree args = ARGUMENT_PACK_ARGS (arg);
28505 for (tree arg : tree_vec_range (args))
28506 if (dependent_template_arg_p (arg))
28507 return true;
28508 return false;
28509 }
28510 else if (TYPE_P (arg))
28511 return dependent_type_p (arg);
28512 else
28513 return value_dependent_expression_p (arg);
28514 }
28515
28516 /* Identify any expressions that use function parms. */
28517
28518 static tree
28519 find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
28520 {
28521 tree t = *tp;
28522 if (TREE_CODE (t) == PARM_DECL)
28523 {
28524 *walk_subtrees = 0;
28525 return t;
28526 }
28527 return NULL_TREE;
28528 }
28529
28530 /* Returns true if a type specialization formed using the template
28531 arguments ARGS needs to use structural equality. */
28532
28533 bool
28534 any_template_arguments_need_structural_equality_p (tree args)
28535 {
28536 int i;
28537 int j;
28538
28539 if (!args)
28540 return false;
28541 if (args == error_mark_node)
28542 return true;
28543
28544 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28545 {
28546 tree level = TMPL_ARGS_LEVEL (args, i + 1);
28547 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28548 {
28549 tree arg = TREE_VEC_ELT (level, j);
28550 tree packed_args = NULL_TREE;
28551 int k, len = 1;
28552
28553 if (ARGUMENT_PACK_P (arg))
28554 {
28555 /* Look inside the argument pack. */
28556 packed_args = ARGUMENT_PACK_ARGS (arg);
28557 len = TREE_VEC_LENGTH (packed_args);
28558 }
28559
28560 for (k = 0; k < len; ++k)
28561 {
28562 if (packed_args)
28563 arg = TREE_VEC_ELT (packed_args, k);
28564
28565 if (error_operand_p (arg))
28566 return true;
28567 else if (TREE_CODE (arg) == TEMPLATE_DECL)
28568 continue;
28569 else if (arg == any_targ_node)
28570 /* An any_targ_node argument (added by add_defaults_to_ttp)
28571 makes the corresponding specialization not canonicalizable,
28572 since template_args_equal always return true for it. We
28573 may see this when called from bind_template_template_parm. */
28574 return true;
28575 /* Checking current_function_decl because this structural
28576 comparison is only necessary for redeclaration. */
28577 else if (!current_function_decl
28578 && dependent_template_arg_p (arg)
28579 && (cp_walk_tree_without_duplicates
28580 (&arg, find_parm_usage_r, NULL)))
28581 /* The identity of a class template specialization that uses
28582 a function parameter depends on the identity of the function.
28583 And if this specialization appeared in the trailing return
28584 type thereof, we don't know the identity of the function
28585 (e.g. if it's a redeclaration or a new function) until we
28586 form its signature and go through duplicate_decls. Thus
28587 it's unsafe to decide on a canonical type now (which depends
28588 on the DECL_CONTEXT of the function parameter, which can get
28589 mutated after the fact by duplicate_decls), so just require
28590 structural equality in this case (PR52830). */
28591 return true;
28592 }
28593 }
28594 }
28595
28596 return false;
28597 }
28598
28599 /* Returns true if ARGS (a collection of template arguments) contains
28600 any dependent arguments. */
28601
28602 bool
28603 any_dependent_template_arguments_p (const_tree args)
28604 {
28605 int i;
28606 int j;
28607
28608 if (!args)
28609 return false;
28610 if (args == error_mark_node)
28611 return true;
28612
28613 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28614 {
28615 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
28616 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28617 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
28618 return true;
28619 }
28620
28621 return false;
28622 }
28623
28624 /* Returns true if ARGS contains any errors. */
28625
28626 bool
28627 any_erroneous_template_args_p (const_tree args)
28628 {
28629 int i;
28630 int j;
28631
28632 if (args == error_mark_node)
28633 return true;
28634
28635 if (args && TREE_CODE (args) != TREE_VEC)
28636 {
28637 if (tree ti = get_template_info (args))
28638 args = TI_ARGS (ti);
28639 else
28640 args = NULL_TREE;
28641 }
28642
28643 if (!args)
28644 return false;
28645
28646 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
28647 {
28648 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
28649 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
28650 if (error_operand_p (TREE_VEC_ELT (level, j)))
28651 return true;
28652 }
28653
28654 return false;
28655 }
28656
28657 /* Returns TRUE if the template TMPL is type-dependent. */
28658
28659 bool
28660 dependent_template_p (tree tmpl)
28661 {
28662 if (TREE_CODE (tmpl) == OVERLOAD)
28663 {
28664 for (lkp_iterator iter (tmpl); iter; ++iter)
28665 if (dependent_template_p (*iter))
28666 return true;
28667 return false;
28668 }
28669
28670 /* Template template parameters are dependent. */
28671 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
28672 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
28673 return true;
28674 /* So are names that have not been looked up. */
28675 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
28676 return true;
28677 return false;
28678 }
28679
28680 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
28681
28682 bool
28683 dependent_template_id_p (tree tmpl, tree args)
28684 {
28685 return (dependent_template_p (tmpl)
28686 || any_dependent_template_arguments_p (args));
28687 }
28688
28689 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
28690 are dependent. */
28691
28692 bool
28693 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
28694 {
28695 int i;
28696
28697 if (!processing_template_decl)
28698 return false;
28699
28700 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
28701 {
28702 tree decl = TREE_VEC_ELT (declv, i);
28703 tree init = TREE_VEC_ELT (initv, i);
28704 tree cond = TREE_VEC_ELT (condv, i);
28705 tree incr = TREE_VEC_ELT (incrv, i);
28706
28707 if (type_dependent_expression_p (decl)
28708 || TREE_CODE (decl) == SCOPE_REF)
28709 return true;
28710
28711 if (init && type_dependent_expression_p (init))
28712 return true;
28713
28714 if (cond == global_namespace)
28715 return true;
28716
28717 if (type_dependent_expression_p (cond))
28718 return true;
28719
28720 if (COMPARISON_CLASS_P (cond)
28721 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
28722 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
28723 return true;
28724
28725 if (TREE_CODE (incr) == MODOP_EXPR)
28726 {
28727 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
28728 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
28729 return true;
28730 }
28731 else if (type_dependent_expression_p (incr))
28732 return true;
28733 else if (TREE_CODE (incr) == MODIFY_EXPR)
28734 {
28735 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
28736 return true;
28737 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
28738 {
28739 tree t = TREE_OPERAND (incr, 1);
28740 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
28741 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
28742 return true;
28743
28744 /* If this loop has a class iterator with != comparison
28745 with increment other than i++/++i/i--/--i, make sure the
28746 increment is constant. */
28747 if (CLASS_TYPE_P (TREE_TYPE (decl))
28748 && TREE_CODE (cond) == NE_EXPR)
28749 {
28750 if (TREE_OPERAND (t, 0) == decl)
28751 t = TREE_OPERAND (t, 1);
28752 else
28753 t = TREE_OPERAND (t, 0);
28754 if (TREE_CODE (t) != INTEGER_CST)
28755 return true;
28756 }
28757 }
28758 }
28759 }
28760
28761 return false;
28762 }
28763
28764 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
28765 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
28766 no such TYPE can be found. Note that this function peers inside
28767 uninstantiated templates and therefore should be used only in
28768 extremely limited situations. ONLY_CURRENT_P restricts this
28769 peering to the currently open classes hierarchy (which is required
28770 when comparing types). */
28771
28772 tree
28773 resolve_typename_type (tree type, bool only_current_p)
28774 {
28775 tree scope;
28776 tree name;
28777 tree decl;
28778 int quals;
28779 tree pushed_scope;
28780 tree result;
28781
28782 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
28783
28784 scope = TYPE_CONTEXT (type);
28785 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
28786 gcc_checking_assert (uses_template_parms (scope));
28787
28788 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
28789 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
28790 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
28791 representing the typedef. In that case TYPE_IDENTIFIER (type) is
28792 not the non-qualified identifier of the TYPENAME_TYPE anymore.
28793 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
28794 the TYPENAME_TYPE instead, we avoid messing up with a possible
28795 typedef variant case. */
28796 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
28797
28798 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
28799 it first before we can figure out what NAME refers to. */
28800 if (TREE_CODE (scope) == TYPENAME_TYPE)
28801 {
28802 if (TYPENAME_IS_RESOLVING_P (scope))
28803 /* Given a class template A with a dependent base with nested type C,
28804 typedef typename A::C::C C will land us here, as trying to resolve
28805 the initial A::C leads to the local C typedef, which leads back to
28806 A::C::C. So we break the recursion now. */
28807 return type;
28808 else
28809 scope = resolve_typename_type (scope, only_current_p);
28810 }
28811 /* If we don't know what SCOPE refers to, then we cannot resolve the
28812 TYPENAME_TYPE. */
28813 if (!CLASS_TYPE_P (scope))
28814 return type;
28815 /* If this is a typedef, we don't want to look inside (c++/11987). */
28816 if (typedef_variant_p (type))
28817 return type;
28818 /* If SCOPE isn't the template itself, it will not have a valid
28819 TYPE_FIELDS list. */
28820 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
28821 /* scope is either the template itself or a compatible instantiation
28822 like X<T>, so look up the name in the original template. */
28823 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
28824 /* If scope has no fields, it can't be a current instantiation. Check this
28825 before currently_open_class to avoid infinite recursion (71515). */
28826 if (!TYPE_FIELDS (scope))
28827 return type;
28828 /* If the SCOPE is not the current instantiation, there's no reason
28829 to look inside it. */
28830 if (only_current_p && !currently_open_class (scope))
28831 return type;
28832 /* Enter the SCOPE so that name lookup will be resolved as if we
28833 were in the class definition. In particular, SCOPE will no
28834 longer be considered a dependent type. */
28835 pushed_scope = push_scope (scope);
28836 /* Look up the declaration. */
28837 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
28838 tf_warning_or_error);
28839
28840 result = NULL_TREE;
28841
28842 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
28843 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
28844 tree fullname = TYPENAME_TYPE_FULLNAME (type);
28845 if (!decl)
28846 /*nop*/;
28847 else if (identifier_p (fullname)
28848 && TREE_CODE (decl) == TYPE_DECL)
28849 {
28850 result = TREE_TYPE (decl);
28851 if (result == error_mark_node)
28852 result = NULL_TREE;
28853 }
28854 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
28855 && DECL_CLASS_TEMPLATE_P (decl))
28856 {
28857 /* Obtain the template and the arguments. */
28858 tree tmpl = TREE_OPERAND (fullname, 0);
28859 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
28860 {
28861 /* We get here with a plain identifier because a previous tentative
28862 parse of the nested-name-specifier as part of a ptr-operator saw
28863 ::template X<A>. The use of ::template is necessary in a
28864 ptr-operator, but wrong in a declarator-id.
28865
28866 [temp.names]: In a qualified-id of a declarator-id, the keyword
28867 template shall not appear at the top level. */
28868 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
28869 "keyword %<template%> not allowed in declarator-id");
28870 tmpl = decl;
28871 }
28872 tree args = TREE_OPERAND (fullname, 1);
28873 /* Instantiate the template. */
28874 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
28875 /*entering_scope=*/true,
28876 tf_error | tf_user);
28877 if (result == error_mark_node)
28878 result = NULL_TREE;
28879 }
28880
28881 /* Leave the SCOPE. */
28882 if (pushed_scope)
28883 pop_scope (pushed_scope);
28884
28885 /* If we failed to resolve it, return the original typename. */
28886 if (!result)
28887 return type;
28888
28889 /* If lookup found a typename type, resolve that too. */
28890 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
28891 {
28892 /* Ill-formed programs can cause infinite recursion here, so we
28893 must catch that. */
28894 TYPENAME_IS_RESOLVING_P (result) = 1;
28895 result = resolve_typename_type (result, only_current_p);
28896 TYPENAME_IS_RESOLVING_P (result) = 0;
28897 }
28898
28899 /* Qualify the resulting type. */
28900 quals = cp_type_quals (type);
28901 if (quals)
28902 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
28903
28904 return result;
28905 }
28906
28907 /* EXPR is an expression which is not type-dependent. Return a proxy
28908 for EXPR that can be used to compute the types of larger
28909 expressions containing EXPR. */
28910
28911 tree
28912 build_non_dependent_expr (tree expr)
28913 {
28914 tree orig_expr = expr;
28915 tree inner_expr;
28916
28917 /* When checking, try to get a constant value for all non-dependent
28918 expressions in order to expose bugs in *_dependent_expression_p
28919 and constexpr. This can affect code generation, see PR70704, so
28920 only do this for -fchecking=2. */
28921 if (flag_checking > 1
28922 && cxx_dialect >= cxx11
28923 /* Don't do this during nsdmi parsing as it can lead to
28924 unexpected recursive instantiations. */
28925 && !parsing_nsdmi ()
28926 /* Don't do this during concept processing either and for
28927 the same reason. */
28928 && !processing_constraint_expression_p ())
28929 fold_non_dependent_expr (expr, tf_none);
28930
28931 STRIP_ANY_LOCATION_WRAPPER (expr);
28932
28933 /* Preserve OVERLOADs; the functions must be available to resolve
28934 types. */
28935 inner_expr = expr;
28936 if (TREE_CODE (inner_expr) == STMT_EXPR)
28937 inner_expr = stmt_expr_value_expr (inner_expr);
28938 if (TREE_CODE (inner_expr) == ADDR_EXPR)
28939 inner_expr = TREE_OPERAND (inner_expr, 0);
28940 if (TREE_CODE (inner_expr) == COMPONENT_REF)
28941 inner_expr = TREE_OPERAND (inner_expr, 1);
28942 if (is_overloaded_fn (inner_expr)
28943 || TREE_CODE (inner_expr) == OFFSET_REF)
28944 return orig_expr;
28945 /* There is no need to return a proxy for a variable, parameter
28946 or enumerator. */
28947 if (VAR_P (expr) || TREE_CODE (expr) == PARM_DECL
28948 || TREE_CODE (expr) == CONST_DECL)
28949 return orig_expr;
28950 /* Preserve string constants; conversions from string constants to
28951 "char *" are allowed, even though normally a "const char *"
28952 cannot be used to initialize a "char *". */
28953 if (TREE_CODE (expr) == STRING_CST)
28954 return orig_expr;
28955 /* Preserve void and arithmetic constants, as an optimization -- there is no
28956 reason to create a new node. */
28957 if (TREE_CODE (expr) == VOID_CST
28958 || TREE_CODE (expr) == INTEGER_CST
28959 || TREE_CODE (expr) == REAL_CST)
28960 return orig_expr;
28961 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28962 There is at least one place where we want to know that a
28963 particular expression is a throw-expression: when checking a ?:
28964 expression, there are special rules if the second or third
28965 argument is a throw-expression. */
28966 if (TREE_CODE (expr) == THROW_EXPR)
28967 return orig_expr;
28968
28969 /* Don't wrap an initializer list, we need to be able to look inside. */
28970 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
28971 return orig_expr;
28972
28973 /* Don't wrap a dummy object, we need to be able to test for it. */
28974 if (is_dummy_object (expr))
28975 return orig_expr;
28976
28977 if (TREE_CODE (expr) == COND_EXPR)
28978 return build3 (COND_EXPR,
28979 TREE_TYPE (expr),
28980 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
28981 (TREE_OPERAND (expr, 1)
28982 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
28983 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
28984 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
28985 if (TREE_CODE (expr) == COMPOUND_EXPR)
28986 return build2 (COMPOUND_EXPR,
28987 TREE_TYPE (expr),
28988 TREE_OPERAND (expr, 0),
28989 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
28990
28991 /* If the type is unknown, it can't really be non-dependent */
28992 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
28993
28994 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28995 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
28996 TREE_TYPE (expr), expr);
28997 }
28998
28999 /* ARGS is a vector of expressions as arguments to a function call.
29000 Replace the arguments with equivalent non-dependent expressions.
29001 This modifies ARGS in place. */
29002
29003 void
29004 make_args_non_dependent (vec<tree, va_gc> *args)
29005 {
29006 unsigned int ix;
29007 tree arg;
29008
29009 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
29010 {
29011 tree newarg = build_non_dependent_expr (arg);
29012 if (newarg != arg)
29013 (*args)[ix] = newarg;
29014 }
29015 }
29016
29017 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
29018 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
29019 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */
29020
29021 static tree
29022 make_auto_1 (tree name, bool set_canonical, int level = -1)
29023 {
29024 if (level == -1)
29025 level = current_template_depth + 1;
29026 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
29027 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
29028 TYPE_STUB_DECL (au) = TYPE_NAME (au);
29029 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
29030 (0, level, level, TYPE_NAME (au), NULL_TREE);
29031 if (set_canonical)
29032 TYPE_CANONICAL (au) = canonical_type_parameter (au);
29033 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
29034 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
29035 if (name == decltype_auto_identifier)
29036 AUTO_IS_DECLTYPE (au) = true;
29037
29038 return au;
29039 }
29040
29041 tree
29042 make_decltype_auto (void)
29043 {
29044 return make_auto_1 (decltype_auto_identifier, true);
29045 }
29046
29047 tree
29048 make_auto (void)
29049 {
29050 return make_auto_1 (auto_identifier, true);
29051 }
29052
29053 /* Return a C++17 deduction placeholder for class template TMPL.
29054 There are represented as an 'auto' with the special level 0 and
29055 CLASS_PLACEHOLDER_TEMPLATE set. */
29056
29057 tree
29058 make_template_placeholder (tree tmpl)
29059 {
29060 tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
29061 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
29062 /* Our canonical type depends on the placeholder. */
29063 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29064 return t;
29065 }
29066
29067 /* True iff T is a C++17 class template deduction placeholder. */
29068
29069 bool
29070 template_placeholder_p (tree t)
29071 {
29072 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
29073 }
29074
29075 /* Make a "constrained auto" type-specifier. This is an auto or
29076 decltype(auto) type with constraints that must be associated after
29077 deduction. The constraint is formed from the given concept CON
29078 and its optional sequence of template arguments ARGS.
29079
29080 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
29081
29082 static tree
29083 make_constrained_placeholder_type (tree type, tree con, tree args)
29084 {
29085 /* Build the constraint. */
29086 tree tmpl = DECL_TI_TEMPLATE (con);
29087 tree expr = tmpl;
29088 if (TREE_CODE (con) == FUNCTION_DECL)
29089 expr = ovl_make (tmpl);
29090 ++processing_template_decl;
29091 expr = build_concept_check (expr, type, args, tf_warning_or_error);
29092 --processing_template_decl;
29093
29094 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
29095 = build_tree_list (current_template_parms, expr);
29096
29097 /* Our canonical type depends on the constraint. */
29098 TYPE_CANONICAL (type) = canonical_type_parameter (type);
29099
29100 /* Attach the constraint to the type declaration. */
29101 return TYPE_NAME (type);
29102 }
29103
29104 /* Make a "constrained auto" type-specifier. */
29105
29106 tree
29107 make_constrained_auto (tree con, tree args)
29108 {
29109 tree type = make_auto_1 (auto_identifier, false);
29110 return make_constrained_placeholder_type (type, con, args);
29111 }
29112
29113 /* Make a "constrained decltype(auto)" type-specifier. */
29114
29115 tree
29116 make_constrained_decltype_auto (tree con, tree args)
29117 {
29118 tree type = make_auto_1 (decltype_auto_identifier, false);
29119 return make_constrained_placeholder_type (type, con, args);
29120 }
29121
29122 /* Returns true if the placeholder type constraint T has any dependent
29123 (explicit) template arguments. */
29124
29125 static bool
29126 placeholder_type_constraint_dependent_p (tree t)
29127 {
29128 tree id = unpack_concept_check (t);
29129 tree args = TREE_OPERAND (id, 1);
29130 tree first = TREE_VEC_ELT (args, 0);
29131 if (ARGUMENT_PACK_P (first))
29132 {
29133 args = expand_template_argument_pack (args);
29134 first = TREE_VEC_ELT (args, 0);
29135 }
29136 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
29137 || is_auto (first));
29138 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
29139 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
29140 return true;
29141 return false;
29142 }
29143
29144 /* Build and return a concept definition. Like other templates, the
29145 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
29146 the TEMPLATE_DECL. */
29147
29148 tree
29149 finish_concept_definition (cp_expr id, tree init, tree attrs)
29150 {
29151 gcc_assert (identifier_p (id));
29152 gcc_assert (processing_template_decl);
29153
29154 location_t loc = id.get_location();
29155
29156 /* A concept-definition shall not have associated constraints. */
29157 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
29158 {
29159 error_at (loc, "a concept cannot be constrained");
29160 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
29161 }
29162
29163 /* A concept-definition shall appear in namespace scope. Templates
29164 aren't allowed in block scope, so we only need to check for class
29165 scope. */
29166 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
29167 {
29168 error_at (loc, "concept %qE not in namespace scope", *id);
29169 return error_mark_node;
29170 }
29171
29172 if (current_template_depth > 1)
29173 {
29174 error_at (loc, "concept %qE has multiple template parameter lists", *id);
29175 return error_mark_node;
29176 }
29177
29178 /* Initially build the concept declaration; its type is bool. */
29179 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
29180 DECL_CONTEXT (decl) = current_scope ();
29181 DECL_INITIAL (decl) = init;
29182
29183 if (attrs)
29184 cplus_decl_attributes (&decl, attrs, 0);
29185
29186 set_originating_module (decl, false);
29187
29188 /* Push the enclosing template. */
29189 return push_template_decl (decl);
29190 }
29191
29192 /* Given type ARG, return std::initializer_list<ARG>. */
29193
29194 static tree
29195 listify (tree arg)
29196 {
29197 tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
29198
29199 if (std_init_list == error_mark_node
29200 || !DECL_CLASS_TEMPLATE_P (std_init_list))
29201 {
29202 gcc_rich_location richloc (input_location);
29203 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
29204 error_at (&richloc,
29205 "deducing from brace-enclosed initializer list"
29206 " requires %<#include <initializer_list>%>");
29207
29208 return error_mark_node;
29209 }
29210 tree argvec = make_tree_vec (1);
29211 TREE_VEC_ELT (argvec, 0) = arg;
29212
29213 return lookup_template_class (std_init_list, argvec, NULL_TREE,
29214 NULL_TREE, 0, tf_warning_or_error);
29215 }
29216
29217 /* Replace auto in TYPE with std::initializer_list<auto>. */
29218
29219 static tree
29220 listify_autos (tree type, tree auto_node)
29221 {
29222 tree init_auto = listify (strip_top_quals (auto_node));
29223 tree argvec = make_tree_vec (1);
29224 TREE_VEC_ELT (argvec, 0) = init_auto;
29225 if (processing_template_decl)
29226 argvec = add_to_template_args (current_template_args (), argvec);
29227 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
29228 }
29229
29230 /* Hash traits for hashing possibly constrained 'auto'
29231 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
29232
29233 struct auto_hash : default_hash_traits<tree>
29234 {
29235 static inline hashval_t hash (tree);
29236 static inline bool equal (tree, tree);
29237 };
29238
29239 /* Hash the 'auto' T. */
29240
29241 inline hashval_t
29242 auto_hash::hash (tree t)
29243 {
29244 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
29245 /* Matching constrained-type-specifiers denote the same template
29246 parameter, so hash the constraint. */
29247 return hash_placeholder_constraint (c);
29248 else
29249 /* But unconstrained autos are all separate, so just hash the pointer. */
29250 return iterative_hash_object (t, 0);
29251 }
29252
29253 /* Compare two 'auto's. */
29254
29255 inline bool
29256 auto_hash::equal (tree t1, tree t2)
29257 {
29258 if (t1 == t2)
29259 return true;
29260
29261 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
29262 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
29263
29264 /* Two unconstrained autos are distinct. */
29265 if (!c1 || !c2)
29266 return false;
29267
29268 return equivalent_placeholder_constraints (c1, c2);
29269 }
29270
29271 /* for_each_template_parm callback for extract_autos: if t is a (possibly
29272 constrained) auto, add it to the vector. */
29273
29274 static int
29275 extract_autos_r (tree t, void *data)
29276 {
29277 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
29278 if (is_auto (t) && !template_placeholder_p (t))
29279 {
29280 /* All the autos were built with index 0; fix that up now. */
29281 tree *p = hash.find_slot (t, INSERT);
29282 int idx;
29283 if (*p)
29284 /* If this is a repeated constrained-type-specifier, use the index we
29285 chose before. */
29286 idx = TEMPLATE_TYPE_IDX (*p);
29287 else
29288 {
29289 /* Otherwise this is new, so use the current count. */
29290 *p = t;
29291 idx = hash.elements () - 1;
29292 }
29293 if (idx != TEMPLATE_TYPE_IDX (t))
29294 {
29295 gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0);
29296 gcc_checking_assert (TYPE_CANONICAL (t) != t);
29297 TEMPLATE_TYPE_IDX (t) = idx;
29298 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29299 }
29300 }
29301
29302 /* Always keep walking. */
29303 return 0;
29304 }
29305
29306 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
29307 says they can appear anywhere in the type. */
29308
29309 static tree
29310 extract_autos (tree type)
29311 {
29312 hash_set<tree> visited;
29313 hash_table<auto_hash> hash (2);
29314
29315 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
29316
29317 tree tree_vec = make_tree_vec (hash.elements());
29318 for (tree elt : hash)
29319 {
29320 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
29321 TREE_VEC_ELT (tree_vec, i)
29322 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
29323 }
29324
29325 return tree_vec;
29326 }
29327
29328 /* The stem for deduction guide names. */
29329 const char *const dguide_base = "__dguide_";
29330
29331 /* Return the name for a deduction guide for class template TMPL. */
29332
29333 tree
29334 dguide_name (tree tmpl)
29335 {
29336 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
29337 tree tname = TYPE_IDENTIFIER (type);
29338 char *buf = (char *) alloca (1 + strlen (dguide_base)
29339 + IDENTIFIER_LENGTH (tname));
29340 memcpy (buf, dguide_base, strlen (dguide_base));
29341 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
29342 IDENTIFIER_LENGTH (tname) + 1);
29343 tree dname = get_identifier (buf);
29344 TREE_TYPE (dname) = type;
29345 return dname;
29346 }
29347
29348 /* True if NAME is the name of a deduction guide. */
29349
29350 bool
29351 dguide_name_p (tree name)
29352 {
29353 return (TREE_CODE (name) == IDENTIFIER_NODE
29354 && TREE_TYPE (name)
29355 && startswith (IDENTIFIER_POINTER (name), dguide_base));
29356 }
29357
29358 /* True if FN is a deduction guide. */
29359
29360 bool
29361 deduction_guide_p (const_tree fn)
29362 {
29363 if (DECL_P (fn))
29364 if (tree name = DECL_NAME (fn))
29365 return dguide_name_p (name);
29366 return false;
29367 }
29368
29369 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
29370
29371 bool
29372 copy_guide_p (const_tree fn)
29373 {
29374 gcc_assert (deduction_guide_p (fn));
29375 if (!DECL_ARTIFICIAL (fn))
29376 return false;
29377 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
29378 return (TREE_CHAIN (parms) == void_list_node
29379 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
29380 }
29381
29382 /* True if FN is a guide generated from a constructor template. */
29383
29384 bool
29385 template_guide_p (const_tree fn)
29386 {
29387 gcc_assert (deduction_guide_p (fn));
29388 if (!DECL_ARTIFICIAL (fn))
29389 return false;
29390 tree tmpl = DECL_TI_TEMPLATE (fn);
29391 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
29392 return PRIMARY_TEMPLATE_P (org);
29393 return false;
29394 }
29395
29396 /* True if FN is an aggregate initialization guide or the copy deduction
29397 guide. */
29398
29399 bool
29400 builtin_guide_p (const_tree fn)
29401 {
29402 if (!deduction_guide_p (fn))
29403 return false;
29404 if (!DECL_ARTIFICIAL (fn))
29405 /* Explicitly declared. */
29406 return false;
29407 if (DECL_ABSTRACT_ORIGIN (fn))
29408 /* Derived from a constructor. */
29409 return false;
29410 return true;
29411 }
29412
29413 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
29414 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
29415 template parameter types. Note that the handling of template template
29416 parameters relies on current_template_parms being set appropriately for the
29417 new template. */
29418
29419 static tree
29420 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
29421 tree tsubst_args, tsubst_flags_t complain)
29422 {
29423 if (olddecl == error_mark_node)
29424 return error_mark_node;
29425
29426 tree oldidx = get_template_parm_index (olddecl);
29427
29428 tree newtype;
29429 if (TREE_CODE (olddecl) == TYPE_DECL
29430 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29431 {
29432 tree oldtype = TREE_TYPE (olddecl);
29433 newtype = cxx_make_type (TREE_CODE (oldtype));
29434 TYPE_MAIN_VARIANT (newtype) = newtype;
29435 }
29436 else
29437 {
29438 newtype = TREE_TYPE (olddecl);
29439 if (type_uses_auto (newtype))
29440 {
29441 // Substitute once to fix references to other template parameters.
29442 newtype = tsubst (newtype, tsubst_args,
29443 complain|tf_partial, NULL_TREE);
29444 // Now substitute again to reduce the level of the auto.
29445 newtype = tsubst (newtype, current_template_args (),
29446 complain, NULL_TREE);
29447 }
29448 else
29449 newtype = tsubst (newtype, tsubst_args,
29450 complain, NULL_TREE);
29451 }
29452
29453 tree newdecl
29454 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
29455 DECL_NAME (olddecl), newtype);
29456 SET_DECL_TEMPLATE_PARM_P (newdecl);
29457
29458 tree newidx;
29459 if (TREE_CODE (olddecl) == TYPE_DECL
29460 || TREE_CODE (olddecl) == TEMPLATE_DECL)
29461 {
29462 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
29463 = build_template_parm_index (index, level, level,
29464 newdecl, newtype);
29465 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29466 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29467 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
29468
29469 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
29470 {
29471 DECL_TEMPLATE_RESULT (newdecl)
29472 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
29473 DECL_NAME (olddecl), newtype);
29474 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
29475 // First create a copy (ttargs) of tsubst_args with an
29476 // additional level for the template template parameter's own
29477 // template parameters (ttparms).
29478 tree ttparms = (INNERMOST_TEMPLATE_PARMS
29479 (DECL_TEMPLATE_PARMS (olddecl)));
29480 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
29481 tree ttargs = make_tree_vec (depth + 1);
29482 for (int i = 0; i < depth; ++i)
29483 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
29484 TREE_VEC_ELT (ttargs, depth)
29485 = template_parms_level_to_args (ttparms);
29486 // Substitute ttargs into ttparms to fix references to
29487 // other template parameters.
29488 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29489 complain|tf_partial);
29490 // Now substitute again with args based on tparms, to reduce
29491 // the level of the ttparms.
29492 ttargs = current_template_args ();
29493 ttparms = tsubst_template_parms_level (ttparms, ttargs,
29494 complain);
29495 // Finally, tack the adjusted parms onto tparms.
29496 ttparms = tree_cons (size_int (level + 1), ttparms,
29497 copy_node (current_template_parms));
29498 // As with all template template parms, the parameter list captured
29499 // by this template template parm that corresponds to its own level
29500 // should be empty. This avoids infinite recursion when structurally
29501 // comparing two such rewritten template template parms (PR102479).
29502 gcc_assert (!TREE_VEC_LENGTH
29503 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
29504 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
29505 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
29506 // All done.
29507 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
29508 }
29509
29510 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
29511 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
29512 else
29513 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
29514 }
29515 else
29516 {
29517 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
29518 tree newconst
29519 = build_decl (DECL_SOURCE_LOCATION (oldconst),
29520 TREE_CODE (oldconst),
29521 DECL_NAME (oldconst), newtype);
29522 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
29523 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
29524 SET_DECL_TEMPLATE_PARM_P (newconst);
29525 newidx = build_template_parm_index (index, level, level,
29526 newconst, newtype);
29527 TEMPLATE_PARM_PARAMETER_PACK (newidx)
29528 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
29529 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
29530 }
29531
29532 return newdecl;
29533 }
29534
29535 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
29536 template parameter. */
29537
29538 static tree
29539 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
29540 tree targs, unsigned targs_index, tsubst_flags_t complain)
29541 {
29542 tree olddecl = TREE_VALUE (oldelt);
29543 tree newdecl = rewrite_template_parm (olddecl, index, level,
29544 targs, complain);
29545 if (newdecl == error_mark_node)
29546 return error_mark_node;
29547 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
29548 targs, complain, NULL_TREE);
29549 tree list = build_tree_list (newdef, newdecl);
29550 TEMPLATE_PARM_CONSTRAINTS (list)
29551 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
29552 targs, complain, NULL_TREE);
29553 int depth = TMPL_ARGS_DEPTH (targs);
29554 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
29555 return list;
29556 }
29557
29558 /* Returns a C++17 class deduction guide template based on the constructor
29559 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
29560 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
29561 aggregate initialization guide. OUTER_ARGS are the template arguments
29562 for the enclosing scope of the class. */
29563
29564 static tree
29565 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
29566 {
29567 tree tparms, targs, fparms, fargs, ci;
29568 bool memtmpl = false;
29569 bool explicit_p;
29570 location_t loc;
29571 tree fn_tmpl = NULL_TREE;
29572
29573 if (outer_args)
29574 {
29575 ++processing_template_decl;
29576 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
29577 --processing_template_decl;
29578 }
29579
29580 if (!DECL_DECLARES_FUNCTION_P (ctor))
29581 {
29582 if (TYPE_P (ctor))
29583 {
29584 bool copy_p = TYPE_REF_P (ctor);
29585 if (copy_p)
29586 fparms = tree_cons (NULL_TREE, type, void_list_node);
29587 else
29588 fparms = void_list_node;
29589 }
29590 else if (TREE_CODE (ctor) == TREE_LIST)
29591 fparms = ctor;
29592 else
29593 gcc_unreachable ();
29594
29595 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
29596 tparms = DECL_TEMPLATE_PARMS (ctmpl);
29597 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
29598 ci = NULL_TREE;
29599 fargs = NULL_TREE;
29600 loc = DECL_SOURCE_LOCATION (ctmpl);
29601 explicit_p = false;
29602 }
29603 else
29604 {
29605 ++processing_template_decl;
29606 bool ok = true;
29607
29608 complain |= tf_dguide;
29609
29610 fn_tmpl
29611 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
29612 : DECL_TI_TEMPLATE (ctor));
29613 if (outer_args)
29614 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
29615 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
29616
29617 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
29618 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
29619 fully specialized args for the enclosing class. Strip those off, as
29620 the deduction guide won't have those template parameters. */
29621 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
29622 TMPL_PARMS_DEPTH (tparms));
29623 /* Discard the 'this' parameter. */
29624 fparms = FUNCTION_ARG_CHAIN (ctor);
29625 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
29626 ci = get_constraints (ctor);
29627 loc = DECL_SOURCE_LOCATION (ctor);
29628 explicit_p = DECL_NONCONVERTING_P (ctor);
29629
29630 if (PRIMARY_TEMPLATE_P (fn_tmpl))
29631 {
29632 memtmpl = true;
29633
29634 /* For a member template constructor, we need to flatten the two
29635 template parameter lists into one, and then adjust the function
29636 signature accordingly. This gets...complicated. */
29637 tree save_parms = current_template_parms;
29638
29639 /* For a member template we should have two levels of parms/args, one
29640 for the class and one for the constructor. We stripped
29641 specialized args for further enclosing classes above. */
29642 const int depth = 2;
29643 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
29644
29645 /* Template args for translating references to the two-level template
29646 parameters into references to the one-level template parameters we
29647 are creating. */
29648 tree tsubst_args = copy_node (targs);
29649 TMPL_ARGS_LEVEL (tsubst_args, depth)
29650 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
29651
29652 /* Template parms for the constructor template. */
29653 tree ftparms = TREE_VALUE (tparms);
29654 unsigned flen = TREE_VEC_LENGTH (ftparms);
29655 /* Template parms for the class template. */
29656 tparms = TREE_CHAIN (tparms);
29657 tree ctparms = TREE_VALUE (tparms);
29658 unsigned clen = TREE_VEC_LENGTH (ctparms);
29659 /* Template parms for the deduction guide start as a copy of the
29660 template parms for the class. We set current_template_parms for
29661 lookup_template_class_1. */
29662 current_template_parms = tparms = copy_node (tparms);
29663 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
29664 for (unsigned i = 0; i < clen; ++i)
29665 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
29666
29667 /* Now we need to rewrite the constructor parms to append them to the
29668 class parms. */
29669 for (unsigned i = 0; i < flen; ++i)
29670 {
29671 unsigned index = i + clen;
29672 unsigned level = 1;
29673 tree oldelt = TREE_VEC_ELT (ftparms, i);
29674 tree newelt
29675 = rewrite_tparm_list (oldelt, index, level,
29676 tsubst_args, i, complain);
29677 if (newelt == error_mark_node)
29678 ok = false;
29679 TREE_VEC_ELT (new_vec, index) = newelt;
29680 }
29681
29682 /* Now we have a final set of template parms to substitute into the
29683 function signature. */
29684 targs = template_parms_to_args (tparms);
29685 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
29686 complain, ctor);
29687 if (fparms == error_mark_node)
29688 ok = false;
29689 if (ci)
29690 {
29691 if (outer_args)
29692 /* FIXME: We'd like to avoid substituting outer template
29693 arguments into the constraint ahead of time, but the
29694 construction of tsubst_args assumes that outer arguments
29695 are already substituted in. */
29696 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
29697 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
29698 }
29699
29700 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
29701 cp_unevaluated_operand. */
29702 cp_evaluated ev;
29703 fargs = tsubst (fargs, tsubst_args, complain, ctor);
29704 current_template_parms = save_parms;
29705 }
29706 else
29707 {
29708 /* Substitute in the same arguments to rewrite class members into
29709 references to members of an unknown specialization. */
29710 cp_evaluated ev;
29711 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
29712 fargs = tsubst (fargs, targs, complain, ctor);
29713 if (ci)
29714 {
29715 if (outer_args)
29716 /* FIXME: As above. */
29717 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
29718 ci = tsubst_constraint_info (ci, targs, complain, ctor);
29719 }
29720 }
29721
29722 --processing_template_decl;
29723 if (!ok)
29724 return error_mark_node;
29725 }
29726
29727 if (!memtmpl)
29728 {
29729 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
29730 tparms = copy_node (tparms);
29731 INNERMOST_TEMPLATE_PARMS (tparms)
29732 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
29733 }
29734
29735 tree fntype = build_function_type (type, fparms);
29736 tree ded_fn = build_lang_decl_loc (loc,
29737 FUNCTION_DECL,
29738 dguide_name (type), fntype);
29739 DECL_ARGUMENTS (ded_fn) = fargs;
29740 DECL_ARTIFICIAL (ded_fn) = true;
29741 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
29742 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
29743 DECL_ARTIFICIAL (ded_tmpl) = true;
29744 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
29745 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
29746 if (DECL_P (ctor))
29747 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
29748 if (ci)
29749 set_constraints (ded_tmpl, ci);
29750
29751 return ded_tmpl;
29752 }
29753
29754 /* Add to LIST the member types for the reshaped initializer CTOR. */
29755
29756 static tree
29757 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
29758 {
29759 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
29760 tree idx, val; unsigned i;
29761 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
29762 {
29763 tree ftype = elt ? elt : TREE_TYPE (idx);
29764 if (BRACE_ENCLOSED_INITIALIZER_P (val)
29765 && CONSTRUCTOR_BRACES_ELIDED_P (val))
29766 {
29767 tree subelt = NULL_TREE;
29768 if (TREE_CODE (ftype) == ARRAY_TYPE)
29769 subelt = TREE_TYPE (ftype);
29770 list = collect_ctor_idx_types (val, list, subelt);
29771 continue;
29772 }
29773 tree arg = NULL_TREE;
29774 if (i == v->length() - 1
29775 && PACK_EXPANSION_P (ftype))
29776 /* Give the trailing pack expansion parameter a default argument to
29777 match aggregate initialization behavior, even if we deduce the
29778 length of the pack separately to more than we have initializers. */
29779 arg = build_constructor (init_list_type_node, NULL);
29780 /* if ei is of array type and xi is a braced-init-list or string literal,
29781 Ti is an rvalue reference to the declared type of ei */
29782 STRIP_ANY_LOCATION_WRAPPER (val);
29783 if (TREE_CODE (ftype) == ARRAY_TYPE
29784 && (BRACE_ENCLOSED_INITIALIZER_P (val)
29785 || TREE_CODE (val) == STRING_CST))
29786 {
29787 if (TREE_CODE (val) == STRING_CST)
29788 ftype = cp_build_qualified_type
29789 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
29790 ftype = (cp_build_reference_type
29791 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
29792 }
29793 list = tree_cons (arg, ftype, list);
29794 }
29795
29796 return list;
29797 }
29798
29799 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
29800
29801 static bool
29802 is_spec_or_derived (tree etype, tree tmpl)
29803 {
29804 if (!etype || !CLASS_TYPE_P (etype))
29805 return false;
29806
29807 etype = cv_unqualified (etype);
29808 tree type = TREE_TYPE (tmpl);
29809 tree tparms = (INNERMOST_TEMPLATE_PARMS
29810 (DECL_TEMPLATE_PARMS (tmpl)));
29811 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29812 int err = unify (tparms, targs, type, etype,
29813 UNIFY_ALLOW_DERIVED, /*explain*/false);
29814 ggc_free (targs);
29815 return !err;
29816 }
29817
29818 static tree alias_ctad_tweaks (tree, tree);
29819
29820 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
29821 INIT. */
29822
29823 static tree
29824 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
29825 {
29826 if (cxx_dialect < cxx20)
29827 return NULL_TREE;
29828
29829 if (init == NULL_TREE)
29830 return NULL_TREE;
29831
29832 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29833 {
29834 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29835 tree tinfo = get_template_info (under);
29836 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
29837 return alias_ctad_tweaks (tmpl, guide);
29838 return NULL_TREE;
29839 }
29840
29841 /* We might be creating a guide for a class member template, e.g.,
29842
29843 template<typename U> struct A {
29844 template<typename T> struct B { T t; };
29845 };
29846
29847 At this point, A will have been instantiated. Below, we need to
29848 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
29849 const bool member_template_p
29850 = (DECL_TEMPLATE_INFO (tmpl)
29851 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
29852 tree type = TREE_TYPE (tmpl);
29853 tree template_type = (member_template_p
29854 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
29855 : type);
29856 if (!CP_AGGREGATE_TYPE_P (template_type))
29857 return NULL_TREE;
29858
29859 /* No aggregate candidate for copy-initialization. */
29860 if (args->length() == 1)
29861 {
29862 tree val = (*args)[0];
29863 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
29864 return NULL_TREE;
29865 }
29866
29867 /* If we encounter a problem, we just won't add the candidate. */
29868 tsubst_flags_t complain = tf_none;
29869
29870 tree parms = NULL_TREE;
29871 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29872 {
29873 init = reshape_init (template_type, init, complain);
29874 if (init == error_mark_node)
29875 return NULL_TREE;
29876 parms = collect_ctor_idx_types (init, parms);
29877 /* If we're creating a deduction guide for a member class template,
29878 we've used the original template pattern type for the reshape_init
29879 above; this is done because we want PARMS to be a template parameter
29880 type, something that can be deduced when used as a function template
29881 parameter. At this point the outer class template has already been
29882 partially instantiated (we deferred the deduction until the enclosing
29883 scope is non-dependent). Therefore we have to partially instantiate
29884 PARMS, so that its template level is properly reduced and we don't get
29885 mismatches when deducing types using the guide with PARMS. */
29886 if (member_template_p)
29887 {
29888 ++processing_template_decl;
29889 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
29890 --processing_template_decl;
29891 }
29892 }
29893 else if (TREE_CODE (init) == TREE_LIST)
29894 {
29895 int len = list_length (init);
29896 for (tree field = TYPE_FIELDS (type);
29897 len;
29898 --len, field = DECL_CHAIN (field))
29899 {
29900 field = next_aggregate_field (field);
29901 if (!field)
29902 return NULL_TREE;
29903 tree ftype = finish_decltype_type (field, true, complain);
29904 parms = tree_cons (NULL_TREE, ftype, parms);
29905 }
29906 }
29907 else
29908 /* Aggregate initialization doesn't apply to an initializer expression. */
29909 return NULL_TREE;
29910
29911 if (parms)
29912 {
29913 tree last = parms;
29914 parms = nreverse (parms);
29915 TREE_CHAIN (last) = void_list_node;
29916 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
29917 return guide;
29918 }
29919
29920 return NULL_TREE;
29921 }
29922
29923 /* UGUIDES are the deduction guides for the underlying template of alias
29924 template TMPL; adjust them to be deduction guides for TMPL. */
29925
29926 static tree
29927 alias_ctad_tweaks (tree tmpl, tree uguides)
29928 {
29929 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
29930 class type (9.2.8.2) where the template-name names an alias template A,
29931 the defining-type-id of A must be of the form
29932
29933 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29934
29935 as specified in 9.2.8.2. The guides of A are the set of functions or
29936 function templates formed as follows. For each function or function
29937 template f in the guides of the template named by the simple-template-id
29938 of the defining-type-id, the template arguments of the return type of f
29939 are deduced from the defining-type-id of A according to the process in
29940 13.10.2.5 with the exception that deduction does not fail if not all
29941 template arguments are deduced. Let g denote the result of substituting
29942 these deductions into f. If substitution succeeds, form a function or
29943 function template f' with the following properties and add it to the set
29944 of guides of A:
29945
29946 * The function type of f' is the function type of g.
29947
29948 * If f is a function template, f' is a function template whose template
29949 parameter list consists of all the template parameters of A (including
29950 their default template arguments) that appear in the above deductions or
29951 (recursively) in their default template arguments, followed by the
29952 template parameters of f that were not deduced (including their default
29953 template arguments), otherwise f' is not a function template.
29954
29955 * The associated constraints (13.5.2) are the conjunction of the
29956 associated constraints of g and a constraint that is satisfied if and only
29957 if the arguments of A are deducible (see below) from the return type.
29958
29959 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29960 be so as well.
29961
29962 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29963 considered to be so as well.
29964
29965 * The explicit-specifier of f' is the explicit-specifier of g (if
29966 any). */
29967
29968 /* This implementation differs from the above in two significant ways:
29969
29970 1) We include all template parameters of A, not just some.
29971 2) The added constraint is same_type instead of deducible.
29972
29973 I believe that while it's probably possible to construct a testcase that
29974 behaves differently with this simplification, it should have the same
29975 effect for real uses. Including all template parameters means that we
29976 deduce all parameters of A when resolving the call, so when we're in the
29977 constraint we don't need to deduce them again, we can just check whether
29978 the deduction produced the desired result. */
29979
29980 tsubst_flags_t complain = tf_warning_or_error;
29981 tree atype = TREE_TYPE (tmpl);
29982 tree aguides = NULL_TREE;
29983 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
29984 unsigned natparms = TREE_VEC_LENGTH (atparms);
29985 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29986 for (ovl_iterator iter (uguides); iter; ++iter)
29987 {
29988 tree f = *iter;
29989 tree in_decl = f;
29990 location_t loc = DECL_SOURCE_LOCATION (f);
29991 tree ret = TREE_TYPE (TREE_TYPE (f));
29992 tree fprime = f;
29993 if (TREE_CODE (f) == TEMPLATE_DECL)
29994 {
29995 processing_template_decl_sentinel ptds (/*reset*/false);
29996 ++processing_template_decl;
29997
29998 /* Deduce template arguments for f from the type-id of A. */
29999 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
30000 unsigned len = TREE_VEC_LENGTH (ftparms);
30001 tree targs = make_tree_vec (len);
30002 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
30003 if (err)
30004 continue;
30005
30006 /* The number of parms for f' is the number of parms for A plus
30007 non-deduced parms of f. */
30008 unsigned ndlen = 0;
30009 unsigned j;
30010 for (unsigned i = 0; i < len; ++i)
30011 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30012 ++ndlen;
30013 tree gtparms = make_tree_vec (natparms + ndlen);
30014
30015 /* Set current_template_parms as in build_deduction_guide. */
30016 auto ctp = make_temp_override (current_template_parms);
30017 current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl));
30018 TREE_VALUE (current_template_parms) = gtparms;
30019
30020 /* First copy over the parms of A. */
30021 for (j = 0; j < natparms; ++j)
30022 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
30023 /* Now rewrite the non-deduced parms of f. */
30024 for (unsigned i = 0; ndlen && i < len; ++i)
30025 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30026 {
30027 --ndlen;
30028 unsigned index = j++;
30029 unsigned level = 1;
30030 tree oldlist = TREE_VEC_ELT (ftparms, i);
30031 tree list = rewrite_tparm_list (oldlist, index, level,
30032 targs, i, complain);
30033 TREE_VEC_ELT (gtparms, index) = list;
30034 }
30035 gtparms = build_tree_list (size_one_node, gtparms);
30036
30037 /* Substitute the deduced arguments plus the rewritten template
30038 parameters into f to get g. This covers the type, copyness,
30039 guideness, and explicit-specifier. */
30040 tree g;
30041 {
30042 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
30043 if cp_unevaluated_operand. */
30044 cp_evaluated ev;
30045 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
30046 }
30047 if (g == error_mark_node)
30048 continue;
30049 DECL_USE_TEMPLATE (g) = 0;
30050 fprime = build_template_decl (g, gtparms, false);
30051 DECL_TEMPLATE_RESULT (fprime) = g;
30052 TREE_TYPE (fprime) = TREE_TYPE (g);
30053 tree gtargs = template_parms_to_args (gtparms);
30054 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
30055 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
30056
30057 /* Substitute the associated constraints. */
30058 tree ci = get_constraints (f);
30059 if (ci)
30060 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
30061 if (ci == error_mark_node)
30062 continue;
30063
30064 /* Add a constraint that the return type matches the instantiation of
30065 A with the same template arguments. */
30066 ret = TREE_TYPE (TREE_TYPE (fprime));
30067 if (!same_type_p (atype, ret)
30068 /* FIXME this should mean they don't compare as equivalent. */
30069 || dependent_alias_template_spec_p (atype, nt_opaque))
30070 {
30071 tree same = finish_trait_expr (loc, CPTK_IS_SAME, atype, ret);
30072 ci = append_constraint (ci, same);
30073 }
30074
30075 if (ci)
30076 {
30077 remove_constraints (fprime);
30078 set_constraints (fprime, ci);
30079 }
30080 }
30081 else
30082 {
30083 /* For a non-template deduction guide, if the arguments of A aren't
30084 deducible from the return type, don't add the candidate. */
30085 tree targs = make_tree_vec (natparms);
30086 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
30087 for (unsigned i = 0; !err && i < natparms; ++i)
30088 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
30089 err = true;
30090 if (err)
30091 continue;
30092 }
30093
30094 aguides = lookup_add (fprime, aguides);
30095 }
30096
30097 return aguides;
30098 }
30099
30100 /* Return artificial deduction guides built from the constructors of class
30101 template TMPL. */
30102
30103 static tree
30104 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
30105 {
30106 tree outer_args = outer_template_args (tmpl);
30107 tree type = TREE_TYPE (most_general_template (tmpl));
30108
30109 tree cands = NULL_TREE;
30110
30111 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
30112 {
30113 /* Skip inherited constructors. */
30114 if (iter.using_p ())
30115 continue;
30116
30117 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
30118 cands = lookup_add (guide, cands);
30119 }
30120
30121 /* Add implicit default constructor deduction guide. */
30122 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
30123 {
30124 tree guide = build_deduction_guide (type, type, outer_args,
30125 complain);
30126 cands = lookup_add (guide, cands);
30127 }
30128
30129 /* Add copy guide. */
30130 {
30131 tree gtype = build_reference_type (type);
30132 tree guide = build_deduction_guide (type, gtype, outer_args,
30133 complain);
30134 cands = lookup_add (guide, cands);
30135 }
30136
30137 return cands;
30138 }
30139
30140 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
30141
30142 /* Return the non-aggregate deduction guides for deducible template TMPL. The
30143 aggregate candidate is added separately because it depends on the
30144 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
30145 guide. */
30146
30147 static tree
30148 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
30149 {
30150 tree guides = NULL_TREE;
30151 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30152 {
30153 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30154 tree tinfo = get_template_info (under);
30155 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
30156 complain);
30157 }
30158 else
30159 {
30160 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
30161 dguide_name (tmpl),
30162 LOOK_want::NORMAL, /*complain*/false);
30163 if (guides == error_mark_node)
30164 guides = NULL_TREE;
30165 else
30166 any_dguides_p = true;
30167 }
30168
30169 /* Cache the deduction guides for a template. We also remember the result of
30170 lookup, and rebuild everything if it changes; should be very rare. */
30171 tree_pair_p cache = NULL;
30172 if (tree_pair_p &r
30173 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
30174 {
30175 cache = r;
30176 if (cache->purpose == guides)
30177 return cache->value;
30178 }
30179 else
30180 {
30181 r = cache = ggc_cleared_alloc<tree_pair_s> ();
30182 cache->purpose = guides;
30183 }
30184
30185 tree cands = NULL_TREE;
30186 if (DECL_ALIAS_TEMPLATE_P (tmpl))
30187 cands = alias_ctad_tweaks (tmpl, guides);
30188 else
30189 {
30190 cands = ctor_deduction_guides_for (tmpl, complain);
30191 for (ovl_iterator it (guides); it; ++it)
30192 cands = lookup_add (*it, cands);
30193 }
30194
30195 cache->value = cands;
30196 return cands;
30197 }
30198
30199 /* Return whether TMPL is a (class template argument-) deducible template. */
30200
30201 bool
30202 ctad_template_p (tree tmpl)
30203 {
30204 /* A deducible template is either a class template or is an alias template
30205 whose defining-type-id is of the form
30206
30207 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
30208
30209 where the nested-name-specifier (if any) is non-dependent and the
30210 template-name of the simple-template-id names a deducible template. */
30211
30212 if (DECL_CLASS_TEMPLATE_P (tmpl)
30213 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30214 return true;
30215 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
30216 return false;
30217 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
30218 if (tree tinfo = get_template_info (orig))
30219 return ctad_template_p (TI_TEMPLATE (tinfo));
30220 return false;
30221 }
30222
30223 /* Deduce template arguments for the class template placeholder PTYPE for
30224 template TMPL based on the initializer INIT, and return the resulting
30225 type. */
30226
30227 static tree
30228 do_class_deduction (tree ptype, tree tmpl, tree init,
30229 int flags, tsubst_flags_t complain)
30230 {
30231 /* We should have handled this in the caller. */
30232 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30233 return ptype;
30234
30235 /* If the class was erroneous, don't try to deduce, because that
30236 can generate a lot of diagnostic. */
30237 if (TREE_TYPE (tmpl)
30238 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
30239 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
30240 return ptype;
30241
30242 /* Wait until the enclosing scope is non-dependent. */
30243 if (DECL_CLASS_SCOPE_P (tmpl)
30244 && dependent_type_p (DECL_CONTEXT (tmpl)))
30245 return ptype;
30246
30247 /* Initializing one placeholder from another. */
30248 if (init
30249 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
30250 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
30251 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
30252 == TEMPLATE_PARM_INDEX)))
30253 && is_auto (TREE_TYPE (init))
30254 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
30255 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
30256
30257 if (!ctad_template_p (tmpl))
30258 {
30259 if (complain & tf_error)
30260 error ("non-deducible template %qT used without template arguments", tmpl);
30261 return error_mark_node;
30262 }
30263 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
30264 {
30265 if (complain & tf_error)
30266 {
30267 /* Be permissive with equivalent alias templates. */
30268 tree u = get_underlying_template (tmpl);
30269 diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
30270 bool complained
30271 = emit_diagnostic (dk, input_location, 0,
30272 "alias template deduction only available "
30273 "with %<-std=c++20%> or %<-std=gnu++20%>");
30274 if (u == tmpl)
30275 return error_mark_node;
30276 else if (complained)
30277 {
30278 inform (input_location, "use %qD directly instead", u);
30279 tmpl = u;
30280 }
30281 }
30282 else
30283 return error_mark_node;
30284 }
30285
30286 /* Wait until the initializer is non-dependent. */
30287 if (type_dependent_expression_p (init))
30288 return ptype;
30289
30290 /* Don't bother with the alias rules for an equivalent template. */
30291 tmpl = get_underlying_template (tmpl);
30292
30293 tree type = TREE_TYPE (tmpl);
30294
30295 bool try_list_cand = false;
30296 bool list_init_p = false;
30297
30298 releasing_vec rv_args = NULL;
30299 vec<tree,va_gc> *&args = *&rv_args;
30300 if (init == NULL_TREE)
30301 args = make_tree_vector ();
30302 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
30303 {
30304 list_init_p = true;
30305 try_list_cand = true;
30306 if (CONSTRUCTOR_NELTS (init) == 1
30307 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
30308 {
30309 /* As an exception, the first phase in 16.3.1.7 (considering the
30310 initializer list as a single argument) is omitted if the
30311 initializer list consists of a single expression of type cv U,
30312 where U is a specialization of C or a class derived from a
30313 specialization of C. */
30314 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
30315 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
30316 try_list_cand = false;
30317 }
30318 if (try_list_cand || is_std_init_list (type))
30319 args = make_tree_vector_single (init);
30320 else
30321 args = make_tree_vector_from_ctor (init);
30322 }
30323 else if (TREE_CODE (init) == TREE_LIST)
30324 args = make_tree_vector_from_list (init);
30325 else
30326 args = make_tree_vector_single (init);
30327
30328 /* Do this now to avoid problems with erroneous args later on. */
30329 args = resolve_args (args, complain);
30330 if (args == NULL)
30331 return error_mark_node;
30332
30333 bool any_dguides_p = false;
30334 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
30335 if (cands == error_mark_node)
30336 return error_mark_node;
30337
30338 /* Prune explicit deduction guides in copy-initialization context (but
30339 not copy-list-initialization). */
30340 bool elided = false;
30341 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
30342 {
30343 for (lkp_iterator iter (cands); !elided && iter; ++iter)
30344 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30345 elided = true;
30346
30347 if (elided)
30348 {
30349 /* Found a nonconverting guide, prune the candidates. */
30350 tree pruned = NULL_TREE;
30351 for (lkp_iterator iter (cands); iter; ++iter)
30352 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
30353 pruned = lookup_add (*iter, pruned);
30354
30355 cands = pruned;
30356 }
30357 }
30358
30359 if (!any_dguides_p)
30360 if (tree guide = maybe_aggr_guide (tmpl, init, args))
30361 cands = lookup_add (guide, cands);
30362
30363 tree fndecl = error_mark_node;
30364
30365 /* If this is list-initialization and the class has a list guide, first
30366 try deducing from the list as a single argument, as [over.match.list]. */
30367 if (try_list_cand)
30368 {
30369 tree list_cands = NULL_TREE;
30370 for (tree dg : lkp_range (cands))
30371 if (is_list_ctor (dg))
30372 list_cands = lookup_add (dg, list_cands);
30373 if (list_cands)
30374 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
30375 if (fndecl == error_mark_node)
30376 {
30377 /* That didn't work, now try treating the list as a sequence of
30378 arguments. */
30379 release_tree_vector (args);
30380 args = make_tree_vector_from_ctor (init);
30381 args = resolve_args (args, complain);
30382 if (args == NULL)
30383 return error_mark_node;
30384 }
30385 }
30386
30387 if (elided && !cands)
30388 {
30389 error ("cannot deduce template arguments for copy-initialization"
30390 " of %qT, as it has no non-explicit deduction guides or "
30391 "user-declared constructors", type);
30392 return error_mark_node;
30393 }
30394 else if (!cands && fndecl == error_mark_node)
30395 {
30396 error ("cannot deduce template arguments of %qT, as it has no viable "
30397 "deduction guides", type);
30398 return error_mark_node;
30399 }
30400
30401 if (fndecl == error_mark_node)
30402 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
30403
30404 if (fndecl == error_mark_node)
30405 {
30406 if (complain & tf_warning_or_error)
30407 {
30408 error ("class template argument deduction failed:");
30409 perform_dguide_overload_resolution (cands, args, complain);
30410 if (elided)
30411 inform (input_location, "explicit deduction guides not considered "
30412 "for copy-initialization");
30413 }
30414 return error_mark_node;
30415 }
30416 /* [over.match.list]/1: In copy-list-initialization, if an explicit
30417 constructor is chosen, the initialization is ill-formed. */
30418 else if (flags & LOOKUP_ONLYCONVERTING)
30419 {
30420 if (DECL_NONCONVERTING_P (fndecl))
30421 {
30422 if (complain & tf_warning_or_error)
30423 {
30424 // TODO: Pass down location from cp_finish_decl.
30425 error ("class template argument deduction for %qT failed: "
30426 "explicit deduction guide selected in "
30427 "copy-list-initialization", type);
30428 inform (DECL_SOURCE_LOCATION (fndecl),
30429 "explicit deduction guide declared here");
30430
30431 }
30432 return error_mark_node;
30433 }
30434 }
30435
30436 /* If CTAD succeeded but the type doesn't have any explicit deduction
30437 guides, this deduction might not be what the user intended. */
30438 if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
30439 {
30440 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
30441 || global_dc->dc_warn_system_headers)
30442 && warning (OPT_Wctad_maybe_unsupported,
30443 "%qT may not intend to support class template argument "
30444 "deduction", type))
30445 inform (input_location, "add a deduction guide to suppress this "
30446 "warning");
30447 }
30448
30449 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
30450 cp_type_quals (ptype));
30451 }
30452
30453 /* Return true if INIT is an unparenthesized id-expression or an
30454 unparenthesized class member access. Used for the argument of
30455 decltype(auto). */
30456
30457 bool
30458 unparenthesized_id_or_class_member_access_p (tree init)
30459 {
30460 STRIP_ANY_LOCATION_WRAPPER (init);
30461
30462 /* We need to be able to tell '(r)' and 'r' apart (when it's of
30463 reference type). Only the latter is an id-expression. */
30464 if (REFERENCE_REF_P (init)
30465 && !REF_PARENTHESIZED_P (init))
30466 init = TREE_OPERAND (init, 0);
30467 return (DECL_P (init)
30468 || ((TREE_CODE (init) == COMPONENT_REF
30469 || TREE_CODE (init) == SCOPE_REF)
30470 && !REF_PARENTHESIZED_P (init)));
30471 }
30472
30473 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
30474 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
30475 The CONTEXT determines the context in which auto deduction is performed
30476 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
30477
30478 OUTER_TARGS is used during template argument deduction (context == adc_unify)
30479 to properly substitute the result. It's also used in the adc_unify and
30480 adc_requirement contexts to communicate the necessary template arguments
30481 to satisfaction. OUTER_TARGS is ignored in other contexts.
30482
30483 For partial-concept-ids, extra args may be appended to the list of deduced
30484 template arguments prior to determining constraint satisfaction. */
30485
30486 tree
30487 do_auto_deduction (tree type, tree init, tree auto_node,
30488 tsubst_flags_t complain, auto_deduction_context context,
30489 tree outer_targs, int flags)
30490 {
30491 if (init == error_mark_node)
30492 return error_mark_node;
30493
30494 if (init && type_dependent_expression_p (init)
30495 && context != adc_unify)
30496 /* Defining a subset of type-dependent expressions that we can deduce
30497 from ahead of time isn't worth the trouble. */
30498 return type;
30499
30500 /* Similarly, we can't deduce from another undeduced decl. */
30501 if (init && undeduced_auto_decl (init))
30502 return type;
30503
30504 /* We may be doing a partial substitution, but we still want to replace
30505 auto_node. */
30506 complain &= ~tf_partial;
30507
30508 /* In C++23, we must deduce the type to int&& for code like
30509 decltype(auto) f(int&& x) { return (x); }
30510 or
30511 auto&& f(int x) { return x; }
30512 so we use treat_lvalue_as_rvalue_p. But don't do it for
30513 decltype(auto) f(int x) { return x; }
30514 where we should deduce 'int' rather than 'int&&'; transmogrifying
30515 INIT to an rvalue would break that. */
30516 tree r;
30517 if (cxx_dialect >= cxx23
30518 && context == adc_return_type
30519 && (!AUTO_IS_DECLTYPE (auto_node)
30520 || !unparenthesized_id_or_class_member_access_p (init))
30521 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
30522 /*return*/true)))
30523 init = r;
30524
30525 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
30526 /* C++17 class template argument deduction. */
30527 return do_class_deduction (type, tmpl, init, flags, complain);
30528
30529 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
30530 /* Nothing we can do with this, even in deduction context. */
30531 return type;
30532
30533 location_t loc = cp_expr_loc_or_input_loc (init);
30534
30535 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
30536 with either a new invented type template parameter U or, if the
30537 initializer is a braced-init-list (8.5.4), with
30538 std::initializer_list<U>. */
30539 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30540 {
30541 if (!DIRECT_LIST_INIT_P (init))
30542 type = listify_autos (type, auto_node);
30543 else if (CONSTRUCTOR_NELTS (init) == 1)
30544 init = CONSTRUCTOR_ELT (init, 0)->value;
30545 else
30546 {
30547 if (complain & tf_warning_or_error)
30548 {
30549 if (permerror (loc, "direct-list-initialization of "
30550 "%<auto%> requires exactly one element"))
30551 inform (loc,
30552 "for deduction to %<std::initializer_list%>, use copy-"
30553 "list-initialization (i.e. add %<=%> before the %<{%>)");
30554 }
30555 type = listify_autos (type, auto_node);
30556 }
30557 }
30558
30559 if (type == error_mark_node)
30560 return error_mark_node;
30561
30562 if (BRACE_ENCLOSED_INITIALIZER_P (init))
30563 {
30564 /* We don't recurse here because we can't deduce from a nested
30565 initializer_list. */
30566 if (CONSTRUCTOR_ELTS (init))
30567 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
30568 elt.value = resolve_nondeduced_context (elt.value, complain);
30569 }
30570 else
30571 init = resolve_nondeduced_context (init, complain);
30572
30573 tree targs;
30574 if (context == adc_decomp_type
30575 && auto_node == type
30576 && init != error_mark_node
30577 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
30578 {
30579 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
30580 and initializer has array type, deduce cv-qualified array type. */
30581 targs = make_tree_vec (1);
30582 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
30583 }
30584 else if (AUTO_IS_DECLTYPE (auto_node))
30585 {
30586 const bool id = unparenthesized_id_or_class_member_access_p (init);
30587 tree deduced = finish_decltype_type (init, id, complain);
30588 deduced = canonicalize_type_argument (deduced, complain);
30589 if (deduced == error_mark_node)
30590 return error_mark_node;
30591 targs = make_tree_vec (1);
30592 TREE_VEC_ELT (targs, 0) = deduced;
30593 }
30594 else
30595 {
30596 if (error_operand_p (init))
30597 return error_mark_node;
30598
30599 tree parms = build_tree_list (NULL_TREE, type);
30600 tree tparms;
30601
30602 if (flag_concepts_ts)
30603 tparms = extract_autos (type);
30604 else
30605 {
30606 tparms = make_tree_vec (1);
30607 TREE_VEC_ELT (tparms, 0)
30608 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
30609 }
30610
30611 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
30612 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
30613 DEDUCE_CALL,
30614 NULL, /*explain_p=*/false);
30615 if (val > 0)
30616 {
30617 if (processing_template_decl)
30618 /* Try again at instantiation time. */
30619 return type;
30620 if (type && type != error_mark_node
30621 && (complain & tf_error))
30622 /* If type is error_mark_node a diagnostic must have been
30623 emitted by now. Also, having a mention to '<type error>'
30624 in the diagnostic is not really useful to the user. */
30625 {
30626 if (cfun
30627 && FNDECL_USED_AUTO (current_function_decl)
30628 && (auto_node
30629 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
30630 && LAMBDA_FUNCTION_P (current_function_decl))
30631 error_at (loc, "unable to deduce lambda return type from %qE",
30632 init);
30633 else
30634 error_at (loc, "unable to deduce %qT from %qE", type, init);
30635 type_unification_real (tparms, targs, parms, &init, 1, 0,
30636 DEDUCE_CALL,
30637 NULL, /*explain_p=*/true);
30638 }
30639 return error_mark_node;
30640 }
30641 }
30642
30643 /* Check any placeholder constraints against the deduced type. */
30644 if (processing_template_decl && context == adc_unify)
30645 /* Constraints will be checked after deduction. */;
30646 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
30647 {
30648 if (processing_template_decl)
30649 {
30650 gcc_checking_assert (context == adc_variable_type
30651 || context == adc_return_type
30652 || context == adc_decomp_type);
30653 gcc_checking_assert (!type_dependent_expression_p (init));
30654 /* If the constraint is dependent, we need to wait until
30655 instantiation time to resolve the placeholder. */
30656 if (placeholder_type_constraint_dependent_p (constr))
30657 return type;
30658 }
30659
30660 if (context == adc_return_type
30661 || context == adc_variable_type
30662 || context == adc_decomp_type)
30663 if (tree fn = current_function_decl)
30664 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
30665 {
30666 outer_targs = DECL_TEMPLATE_INFO (fn)
30667 ? DECL_TI_ARGS (fn) : NULL_TREE;
30668 if (LAMBDA_FUNCTION_P (fn))
30669 {
30670 /* As in satisfy_declaration_constraints. */
30671 tree regen_args = lambda_regenerating_args (fn);
30672 if (outer_targs)
30673 outer_targs = add_to_template_args (regen_args, outer_targs);
30674 else
30675 outer_targs = regen_args;
30676 }
30677 }
30678
30679 tree full_targs = add_to_template_args (outer_targs, targs);
30680
30681 /* HACK: Compensate for callers not always communicating all levels of
30682 outer template arguments by filling in the outermost missing levels
30683 with dummy levels before checking satisfaction. We'll still crash
30684 if the constraint depends on a template argument belonging to one of
30685 these missing levels, but this hack otherwise allows us to handle a
30686 large subset of possible constraints (including all non-dependent
30687 constraints). */
30688 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
30689 - TMPL_ARGS_DEPTH (full_targs)))
30690 {
30691 tree dummy_levels = make_tree_vec (missing_levels);
30692 for (int i = 0; i < missing_levels; ++i)
30693 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
30694 full_targs = add_to_template_args (dummy_levels, full_targs);
30695 }
30696
30697 if (!constraints_satisfied_p (auto_node, full_targs))
30698 {
30699 if (complain & tf_warning_or_error)
30700 {
30701 auto_diagnostic_group d;
30702 switch (context)
30703 {
30704 case adc_unspecified:
30705 case adc_unify:
30706 error_at (loc, "placeholder constraints not satisfied");
30707 break;
30708 case adc_variable_type:
30709 case adc_decomp_type:
30710 error_at (loc, "deduced initializer does not satisfy "
30711 "placeholder constraints");
30712 break;
30713 case adc_return_type:
30714 error_at (loc, "deduced return type does not satisfy "
30715 "placeholder constraints");
30716 break;
30717 case adc_requirement:
30718 error_at (loc, "deduced expression type does not satisfy "
30719 "placeholder constraints");
30720 break;
30721 }
30722 diagnose_constraints (loc, auto_node, full_targs);
30723 }
30724 return error_mark_node;
30725 }
30726 }
30727
30728 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
30729 /* The outer template arguments are already substituted into type
30730 (but we still may have used them for constraint checking above). */;
30731 else if (context == adc_unify)
30732 targs = add_to_template_args (outer_targs, targs);
30733 else if (processing_template_decl)
30734 targs = add_to_template_args (current_template_args (), targs);
30735 return tsubst (type, targs, complain, NULL_TREE);
30736 }
30737
30738 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
30739 result. */
30740
30741 tree
30742 splice_late_return_type (tree type, tree late_return_type)
30743 {
30744 if (late_return_type)
30745 {
30746 gcc_assert (is_auto (type) || seen_error ());
30747 return late_return_type;
30748 }
30749
30750 if (tree auto_node = find_type_usage (type, is_auto))
30751 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
30752 {
30753 /* In an abbreviated function template we didn't know we were dealing
30754 with a function template when we saw the auto return type, so rebuild
30755 the return type using an auto with the correct level. */
30756 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
30757 tree auto_vec = make_tree_vec (1);
30758 TREE_VEC_ELT (auto_vec, 0) = new_auto;
30759 tree targs = add_outermost_template_args (current_template_args (),
30760 auto_vec);
30761 /* Also rebuild the constraint info in terms of the new auto. */
30762 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
30763 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
30764 = build_tree_list (current_template_parms,
30765 tsubst_constraint (TREE_VALUE (ci), targs,
30766 tf_none, NULL_TREE));
30767 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
30768 return tsubst (type, targs, tf_none, NULL_TREE);
30769 }
30770 return type;
30771 }
30772
30773 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
30774 'decltype(auto)' or a deduced class template. */
30775
30776 bool
30777 is_auto (const_tree type)
30778 {
30779 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
30780 && (TYPE_IDENTIFIER (type) == auto_identifier
30781 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
30782 return true;
30783 else
30784 return false;
30785 }
30786
30787 /* for_each_template_parm callback for type_uses_auto. */
30788
30789 int
30790 is_auto_r (tree tp, void */*data*/)
30791 {
30792 return is_auto (tp);
30793 }
30794
30795 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
30796 a use of `auto'. Returns NULL_TREE otherwise. */
30797
30798 tree
30799 type_uses_auto (tree type)
30800 {
30801 if (type == NULL_TREE)
30802 return NULL_TREE;
30803 else if (flag_concepts_ts)
30804 {
30805 /* The Concepts TS allows multiple autos in one type-specifier; just
30806 return the first one we find, do_auto_deduction will collect all of
30807 them. */
30808 if (uses_template_parms (type))
30809 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
30810 /*visited*/NULL, /*nondeduced*/false);
30811 else
30812 return NULL_TREE;
30813 }
30814 else
30815 return find_type_usage (type, is_auto);
30816 }
30817
30818 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
30819 concepts are enabled, auto is acceptable in template arguments, but
30820 only when TEMPL identifies a template class. Return TRUE if any
30821 such errors were reported. */
30822
30823 bool
30824 check_auto_in_tmpl_args (tree tmpl, tree args)
30825 {
30826 if (!flag_concepts_ts)
30827 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
30828 have already been rejected by the parser more generally. */
30829 return false;
30830
30831 /* If there were previous errors, nevermind. */
30832 if (!args || TREE_CODE (args) != TREE_VEC)
30833 return false;
30834
30835 /* If TMPL is an identifier, we're parsing and we can't tell yet
30836 whether TMPL is supposed to be a type, a function or a variable.
30837 We'll only be able to tell during template substitution, so we
30838 expect to be called again then. If concepts are enabled and we
30839 know we have a type, we're ok. */
30840 if (identifier_p (tmpl)
30841 || (DECL_P (tmpl)
30842 && (DECL_TYPE_TEMPLATE_P (tmpl)
30843 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
30844 return false;
30845
30846 /* Quickly search for any occurrences of auto; usually there won't
30847 be any, and then we'll avoid allocating the vector. */
30848 if (!type_uses_auto (args))
30849 return false;
30850
30851 bool errors = false;
30852
30853 tree vec = extract_autos (args);
30854 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
30855 {
30856 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
30857 error_at (DECL_SOURCE_LOCATION (xauto),
30858 "invalid use of %qT in template argument", xauto);
30859 errors = true;
30860 }
30861
30862 return errors;
30863 }
30864
30865 /* Recursively walk over && expressions searching for EXPR. Return a reference
30866 to that expression. */
30867
30868 static tree *find_template_requirement (tree *t, tree key)
30869 {
30870 if (*t == key)
30871 return t;
30872 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
30873 {
30874 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
30875 return p;
30876 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
30877 return p;
30878 }
30879 return 0;
30880 }
30881
30882 /* Convert the generic type parameters in PARM that match the types given in the
30883 range [START_IDX, END_IDX) from the current_template_parms into generic type
30884 packs. */
30885
30886 tree
30887 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
30888 {
30889 tree current = current_template_parms;
30890 int depth = TMPL_PARMS_DEPTH (current);
30891 current = INNERMOST_TEMPLATE_PARMS (current);
30892 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
30893
30894 for (int i = 0; i < start_idx; ++i)
30895 TREE_VEC_ELT (replacement, i)
30896 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
30897
30898 for (int i = start_idx; i < end_idx; ++i)
30899 {
30900 /* Create a distinct parameter pack type from the current parm and add it
30901 to the replacement args to tsubst below into the generic function
30902 parameter. */
30903 tree node = TREE_VEC_ELT (current, i);
30904 tree o = TREE_TYPE (TREE_VALUE (node));
30905 tree t = copy_type (o);
30906 TEMPLATE_TYPE_PARM_INDEX (t)
30907 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
30908 t, 0, 0, tf_none);
30909 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
30910 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
30911 TYPE_MAIN_VARIANT (t) = t;
30912 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
30913 TYPE_CANONICAL (t) = canonical_type_parameter (t);
30914 TREE_VEC_ELT (replacement, i) = t;
30915
30916 /* Replace the current template parameter with new pack. */
30917 TREE_VALUE (node) = TREE_CHAIN (t);
30918
30919 /* Surgically adjust the associated constraint of adjusted parameter
30920 and it's corresponding contribution to the current template
30921 requirements. */
30922 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
30923 {
30924 tree id = unpack_concept_check (constr);
30925 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
30926 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
30927 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
30928
30929 /* If there was a constraint, we also need to replace that in
30930 the template requirements, which we've already built. */
30931 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
30932 reqs = find_template_requirement (reqs, constr);
30933 *reqs = fold;
30934 }
30935 }
30936
30937 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
30938 TREE_VEC_ELT (replacement, i)
30939 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
30940
30941 /* If there are more levels then build up the replacement with the outer
30942 template parms. */
30943 if (depth > 1)
30944 replacement = add_to_template_args (template_parms_to_args
30945 (TREE_CHAIN (current_template_parms)),
30946 replacement);
30947
30948 return tsubst (parm, replacement, tf_none, NULL_TREE);
30949 }
30950
30951 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
30952 0..N-1. */
30953
30954 void
30955 declare_integer_pack (void)
30956 {
30957 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
30958 build_function_type_list (integer_type_node,
30959 integer_type_node,
30960 NULL_TREE),
30961 NULL_TREE, ECF_CONST);
30962 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
30963 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
30964 CP_BUILT_IN_INTEGER_PACK);
30965 }
30966
30967 /* Walk the decl or type specialization table calling FN on each
30968 entry. */
30969
30970 void
30971 walk_specializations (bool decls_p,
30972 void (*fn) (bool decls_p, spec_entry *entry, void *data),
30973 void *data)
30974 {
30975 spec_hash_table *table = decls_p ? decl_specializations
30976 : type_specializations;
30977 spec_hash_table::iterator end (table->end ());
30978 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
30979 fn (decls_p, *iter, data);
30980 }
30981
30982 /* Lookup the specialization of *ELT, in the decl or type
30983 specialization table. Return the SPEC that's already there, or
30984 NULL if nothing. */
30985
30986 tree
30987 match_mergeable_specialization (bool decl_p, spec_entry *elt)
30988 {
30989 hash_table<spec_hasher> *specializations
30990 = decl_p ? decl_specializations : type_specializations;
30991 hashval_t hash = spec_hasher::hash (elt);
30992 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
30993
30994 if (slot)
30995 return (*slot)->spec;
30996
30997 return NULL_TREE;
30998 }
30999
31000 /* Return flags encoding whether SPEC is on the instantiation and/or
31001 specialization lists of TMPL. */
31002
31003 unsigned
31004 get_mergeable_specialization_flags (tree tmpl, tree decl)
31005 {
31006 unsigned flags = 0;
31007
31008 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
31009 inst; inst = TREE_CHAIN (inst))
31010 if (TREE_VALUE (inst) == decl)
31011 {
31012 flags |= 1;
31013 break;
31014 }
31015
31016 if (CLASS_TYPE_P (TREE_TYPE (decl))
31017 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
31018 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
31019 /* Only need to search if DECL is a partial specialization. */
31020 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
31021 part; part = TREE_CHAIN (part))
31022 if (TREE_VALUE (part) == decl)
31023 {
31024 flags |= 2;
31025 break;
31026 }
31027
31028 return flags;
31029 }
31030
31031 /* Add a new specialization described by SPEC. DECL is the
31032 maybe-template decl and FLAGS is as returned from
31033 get_mergeable_specialization_flags. */
31034
31035 void
31036 add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
31037 tree decl, unsigned flags)
31038 {
31039 hashval_t hash = spec_hasher::hash (elt);
31040 if (decl_p)
31041 {
31042 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
31043
31044 gcc_checking_assert (!*slot);
31045 auto entry = ggc_alloc<spec_entry> ();
31046 *entry = *elt;
31047 *slot = entry;
31048
31049 if (alias_p)
31050 {
31051 elt->spec = TREE_TYPE (elt->spec);
31052 gcc_checking_assert (elt->spec);
31053 }
31054 }
31055
31056 if (!decl_p || alias_p)
31057 {
31058 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
31059
31060 /* We don't distinguish different constrained partial type
31061 specializations, so there could be duplicates. Everything else
31062 must be new. */
31063 if (!(flags & 2 && *slot))
31064 {
31065 gcc_checking_assert (!*slot);
31066
31067 auto entry = ggc_alloc<spec_entry> ();
31068 *entry = *elt;
31069 *slot = entry;
31070 }
31071 }
31072
31073 if (flags & 1)
31074 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
31075 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
31076
31077 if (flags & 2)
31078 {
31079 /* A partial specialization. */
31080 tree cons = tree_cons (elt->args, decl,
31081 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
31082 TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
31083 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
31084 }
31085 }
31086
31087 /* Set up the hash tables for template instantiations. */
31088
31089 void
31090 init_template_processing (void)
31091 {
31092 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
31093 type_specializations = hash_table<spec_hasher>::create_ggc (37);
31094
31095 if (cxx_dialect >= cxx11)
31096 declare_integer_pack ();
31097 }
31098
31099 /* Print stats about the template hash tables for -fstats. */
31100
31101 void
31102 print_template_statistics (void)
31103 {
31104 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
31105 "%f collisions\n", (long) decl_specializations->size (),
31106 (long) decl_specializations->elements (),
31107 decl_specializations->collisions ());
31108 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
31109 "%f collisions\n", (long) type_specializations->size (),
31110 (long) type_specializations->elements (),
31111 type_specializations->collisions ());
31112 }
31113
31114 #if CHECKING_P
31115
31116 namespace selftest {
31117
31118 /* Verify that build_non_dependent_expr () works, for various expressions,
31119 and that location wrappers don't affect the results. */
31120
31121 static void
31122 test_build_non_dependent_expr ()
31123 {
31124 location_t loc = BUILTINS_LOCATION;
31125
31126 /* Verify constants, without and with location wrappers. */
31127 tree int_cst = build_int_cst (integer_type_node, 42);
31128 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
31129
31130 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
31131 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
31132 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
31133
31134 tree string_lit = build_string (4, "foo");
31135 TREE_TYPE (string_lit) = char_array_type_node;
31136 string_lit = fix_string_type (string_lit);
31137 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
31138
31139 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
31140 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
31141 ASSERT_EQ (wrapped_string_lit,
31142 build_non_dependent_expr (wrapped_string_lit));
31143 }
31144
31145 /* Verify that type_dependent_expression_p () works correctly, even
31146 in the presence of location wrapper nodes. */
31147
31148 static void
31149 test_type_dependent_expression_p ()
31150 {
31151 location_t loc = BUILTINS_LOCATION;
31152
31153 tree name = get_identifier ("foo");
31154
31155 /* If no templates are involved, nothing is type-dependent. */
31156 gcc_assert (!processing_template_decl);
31157 ASSERT_FALSE (type_dependent_expression_p (name));
31158
31159 ++processing_template_decl;
31160
31161 /* Within a template, an unresolved name is always type-dependent. */
31162 ASSERT_TRUE (type_dependent_expression_p (name));
31163
31164 /* Ensure it copes with NULL_TREE and errors. */
31165 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
31166 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
31167
31168 /* A USING_DECL in a template should be type-dependent, even if wrapped
31169 with a location wrapper (PR c++/83799). */
31170 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
31171 TREE_TYPE (using_decl) = integer_type_node;
31172 ASSERT_TRUE (type_dependent_expression_p (using_decl));
31173 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
31174 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
31175 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
31176
31177 --processing_template_decl;
31178 }
31179
31180 /* Run all of the selftests within this file. */
31181
31182 void
31183 cp_pt_cc_tests ()
31184 {
31185 test_build_non_dependent_expr ();
31186 test_type_dependent_expression_p ();
31187 }
31188
31189 } // namespace selftest
31190
31191 #endif /* #if CHECKING_P */
31192
31193 #include "gt-cp-pt.h"